xfs_inode.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_attr_sf.h"
  32. #include "xfs_attr.h"
  33. #include "xfs_trans_space.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_inode_item.h"
  37. #include "xfs_ialloc.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_bmap_util.h"
  40. #include "xfs_error.h"
  41. #include "xfs_quota.h"
  42. #include "xfs_filestream.h"
  43. #include "xfs_cksum.h"
  44. #include "xfs_trace.h"
  45. #include "xfs_icache.h"
  46. #include "xfs_symlink.h"
  47. #include "xfs_trans_priv.h"
  48. #include "xfs_log.h"
  49. #include "xfs_bmap_btree.h"
  50. kmem_zone_t *xfs_inode_zone;
  51. /*
  52. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  53. * freed from a file in a single transaction.
  54. */
  55. #define XFS_ITRUNC_MAX_EXTENTS 2
  56. STATIC int xfs_iflush_int(struct xfs_inode *, struct xfs_buf *);
  57. STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
  58. STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
  59. /*
  60. * helper function to extract extent size hint from inode
  61. */
  62. xfs_extlen_t
  63. xfs_get_extsz_hint(
  64. struct xfs_inode *ip)
  65. {
  66. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  67. return ip->i_d.di_extsize;
  68. if (XFS_IS_REALTIME_INODE(ip))
  69. return ip->i_mount->m_sb.sb_rextsize;
  70. return 0;
  71. }
  72. /*
  73. * These two are wrapper routines around the xfs_ilock() routine used to
  74. * centralize some grungy code. They are used in places that wish to lock the
  75. * inode solely for reading the extents. The reason these places can't just
  76. * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
  77. * bringing in of the extents from disk for a file in b-tree format. If the
  78. * inode is in b-tree format, then we need to lock the inode exclusively until
  79. * the extents are read in. Locking it exclusively all the time would limit
  80. * our parallelism unnecessarily, though. What we do instead is check to see
  81. * if the extents have been read in yet, and only lock the inode exclusively
  82. * if they have not.
  83. *
  84. * The functions return a value which should be given to the corresponding
  85. * xfs_iunlock() call.
  86. */
  87. uint
  88. xfs_ilock_data_map_shared(
  89. struct xfs_inode *ip)
  90. {
  91. uint lock_mode = XFS_ILOCK_SHARED;
  92. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
  93. (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
  94. lock_mode = XFS_ILOCK_EXCL;
  95. xfs_ilock(ip, lock_mode);
  96. return lock_mode;
  97. }
  98. uint
  99. xfs_ilock_attr_map_shared(
  100. struct xfs_inode *ip)
  101. {
  102. uint lock_mode = XFS_ILOCK_SHARED;
  103. if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
  104. (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
  105. lock_mode = XFS_ILOCK_EXCL;
  106. xfs_ilock(ip, lock_mode);
  107. return lock_mode;
  108. }
  109. /*
  110. * The xfs inode contains 3 multi-reader locks: the i_iolock the i_mmap_lock and
  111. * the i_lock. This routine allows various combinations of the locks to be
  112. * obtained.
  113. *
  114. * The 3 locks should always be ordered so that the IO lock is obtained first,
  115. * the mmap lock second and the ilock last in order to prevent deadlock.
  116. *
  117. * Basic locking order:
  118. *
  119. * i_iolock -> i_mmap_lock -> page_lock -> i_ilock
  120. *
  121. * mmap_sem locking order:
  122. *
  123. * i_iolock -> page lock -> mmap_sem
  124. * mmap_sem -> i_mmap_lock -> page_lock
  125. *
  126. * The difference in mmap_sem locking order mean that we cannot hold the
  127. * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
  128. * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
  129. * in get_user_pages() to map the user pages into the kernel address space for
  130. * direct IO. Similarly the i_iolock cannot be taken inside a page fault because
  131. * page faults already hold the mmap_sem.
  132. *
  133. * Hence to serialise fully against both syscall and mmap based IO, we need to
  134. * take both the i_iolock and the i_mmap_lock. These locks should *only* be both
  135. * taken in places where we need to invalidate the page cache in a race
  136. * free manner (e.g. truncate, hole punch and other extent manipulation
  137. * functions).
  138. */
  139. void
  140. xfs_ilock(
  141. xfs_inode_t *ip,
  142. uint lock_flags)
  143. {
  144. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  145. /*
  146. * You can't set both SHARED and EXCL for the same lock,
  147. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  148. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  149. */
  150. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  151. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  152. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  153. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  154. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  155. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  156. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  157. if (lock_flags & XFS_IOLOCK_EXCL)
  158. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  159. else if (lock_flags & XFS_IOLOCK_SHARED)
  160. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  161. if (lock_flags & XFS_MMAPLOCK_EXCL)
  162. mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
  163. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  164. mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
  165. if (lock_flags & XFS_ILOCK_EXCL)
  166. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  167. else if (lock_flags & XFS_ILOCK_SHARED)
  168. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  169. }
  170. /*
  171. * This is just like xfs_ilock(), except that the caller
  172. * is guaranteed not to sleep. It returns 1 if it gets
  173. * the requested locks and 0 otherwise. If the IO lock is
  174. * obtained but the inode lock cannot be, then the IO lock
  175. * is dropped before returning.
  176. *
  177. * ip -- the inode being locked
  178. * lock_flags -- this parameter indicates the inode's locks to be
  179. * to be locked. See the comment for xfs_ilock() for a list
  180. * of valid values.
  181. */
  182. int
  183. xfs_ilock_nowait(
  184. xfs_inode_t *ip,
  185. uint lock_flags)
  186. {
  187. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  188. /*
  189. * You can't set both SHARED and EXCL for the same lock,
  190. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  191. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  192. */
  193. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  194. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  195. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  196. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  197. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  198. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  199. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  200. if (lock_flags & XFS_IOLOCK_EXCL) {
  201. if (!mrtryupdate(&ip->i_iolock))
  202. goto out;
  203. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  204. if (!mrtryaccess(&ip->i_iolock))
  205. goto out;
  206. }
  207. if (lock_flags & XFS_MMAPLOCK_EXCL) {
  208. if (!mrtryupdate(&ip->i_mmaplock))
  209. goto out_undo_iolock;
  210. } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
  211. if (!mrtryaccess(&ip->i_mmaplock))
  212. goto out_undo_iolock;
  213. }
  214. if (lock_flags & XFS_ILOCK_EXCL) {
  215. if (!mrtryupdate(&ip->i_lock))
  216. goto out_undo_mmaplock;
  217. } else if (lock_flags & XFS_ILOCK_SHARED) {
  218. if (!mrtryaccess(&ip->i_lock))
  219. goto out_undo_mmaplock;
  220. }
  221. return 1;
  222. out_undo_mmaplock:
  223. if (lock_flags & XFS_MMAPLOCK_EXCL)
  224. mrunlock_excl(&ip->i_mmaplock);
  225. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  226. mrunlock_shared(&ip->i_mmaplock);
  227. out_undo_iolock:
  228. if (lock_flags & XFS_IOLOCK_EXCL)
  229. mrunlock_excl(&ip->i_iolock);
  230. else if (lock_flags & XFS_IOLOCK_SHARED)
  231. mrunlock_shared(&ip->i_iolock);
  232. out:
  233. return 0;
  234. }
  235. /*
  236. * xfs_iunlock() is used to drop the inode locks acquired with
  237. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  238. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  239. * that we know which locks to drop.
  240. *
  241. * ip -- the inode being unlocked
  242. * lock_flags -- this parameter indicates the inode's locks to be
  243. * to be unlocked. See the comment for xfs_ilock() for a list
  244. * of valid values for this parameter.
  245. *
  246. */
  247. void
  248. xfs_iunlock(
  249. xfs_inode_t *ip,
  250. uint lock_flags)
  251. {
  252. /*
  253. * You can't set both SHARED and EXCL for the same lock,
  254. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  255. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  256. */
  257. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  258. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  259. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  260. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  261. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  262. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  263. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  264. ASSERT(lock_flags != 0);
  265. if (lock_flags & XFS_IOLOCK_EXCL)
  266. mrunlock_excl(&ip->i_iolock);
  267. else if (lock_flags & XFS_IOLOCK_SHARED)
  268. mrunlock_shared(&ip->i_iolock);
  269. if (lock_flags & XFS_MMAPLOCK_EXCL)
  270. mrunlock_excl(&ip->i_mmaplock);
  271. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  272. mrunlock_shared(&ip->i_mmaplock);
  273. if (lock_flags & XFS_ILOCK_EXCL)
  274. mrunlock_excl(&ip->i_lock);
  275. else if (lock_flags & XFS_ILOCK_SHARED)
  276. mrunlock_shared(&ip->i_lock);
  277. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  278. }
  279. /*
  280. * give up write locks. the i/o lock cannot be held nested
  281. * if it is being demoted.
  282. */
  283. void
  284. xfs_ilock_demote(
  285. xfs_inode_t *ip,
  286. uint lock_flags)
  287. {
  288. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
  289. ASSERT((lock_flags &
  290. ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  291. if (lock_flags & XFS_ILOCK_EXCL)
  292. mrdemote(&ip->i_lock);
  293. if (lock_flags & XFS_MMAPLOCK_EXCL)
  294. mrdemote(&ip->i_mmaplock);
  295. if (lock_flags & XFS_IOLOCK_EXCL)
  296. mrdemote(&ip->i_iolock);
  297. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  298. }
  299. #if defined(DEBUG) || defined(XFS_WARN)
  300. int
  301. xfs_isilocked(
  302. xfs_inode_t *ip,
  303. uint lock_flags)
  304. {
  305. if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
  306. if (!(lock_flags & XFS_ILOCK_SHARED))
  307. return !!ip->i_lock.mr_writer;
  308. return rwsem_is_locked(&ip->i_lock.mr_lock);
  309. }
  310. if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
  311. if (!(lock_flags & XFS_MMAPLOCK_SHARED))
  312. return !!ip->i_mmaplock.mr_writer;
  313. return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
  314. }
  315. if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
  316. if (!(lock_flags & XFS_IOLOCK_SHARED))
  317. return !!ip->i_iolock.mr_writer;
  318. return rwsem_is_locked(&ip->i_iolock.mr_lock);
  319. }
  320. ASSERT(0);
  321. return 0;
  322. }
  323. #endif
  324. #ifdef DEBUG
  325. int xfs_locked_n;
  326. int xfs_small_retries;
  327. int xfs_middle_retries;
  328. int xfs_lots_retries;
  329. int xfs_lock_delays;
  330. #endif
  331. /*
  332. * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
  333. * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
  334. * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
  335. * errors and warnings.
  336. */
  337. #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
  338. static bool
  339. xfs_lockdep_subclass_ok(
  340. int subclass)
  341. {
  342. return subclass < MAX_LOCKDEP_SUBCLASSES;
  343. }
  344. #else
  345. #define xfs_lockdep_subclass_ok(subclass) (true)
  346. #endif
  347. /*
  348. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  349. * value. This can be called for any type of inode lock combination, including
  350. * parent locking. Care must be taken to ensure we don't overrun the subclass
  351. * storage fields in the class mask we build.
  352. */
  353. static inline int
  354. xfs_lock_inumorder(int lock_mode, int subclass)
  355. {
  356. int class = 0;
  357. ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
  358. XFS_ILOCK_RTSUM)));
  359. ASSERT(xfs_lockdep_subclass_ok(subclass));
  360. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  361. ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
  362. ASSERT(xfs_lockdep_subclass_ok(subclass +
  363. XFS_IOLOCK_PARENT_VAL));
  364. class += subclass << XFS_IOLOCK_SHIFT;
  365. if (lock_mode & XFS_IOLOCK_PARENT)
  366. class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
  367. }
  368. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  369. ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
  370. class += subclass << XFS_MMAPLOCK_SHIFT;
  371. }
  372. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
  373. ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
  374. class += subclass << XFS_ILOCK_SHIFT;
  375. }
  376. return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
  377. }
  378. /*
  379. * The following routine will lock n inodes in exclusive mode. We assume the
  380. * caller calls us with the inodes in i_ino order.
  381. *
  382. * We need to detect deadlock where an inode that we lock is in the AIL and we
  383. * start waiting for another inode that is locked by a thread in a long running
  384. * transaction (such as truncate). This can result in deadlock since the long
  385. * running trans might need to wait for the inode we just locked in order to
  386. * push the tail and free space in the log.
  387. *
  388. * xfs_lock_inodes() can only be used to lock one type of lock at a time -
  389. * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  390. * lock more than one at a time, lockdep will report false positives saying we
  391. * have violated locking orders.
  392. */
  393. void
  394. xfs_lock_inodes(
  395. xfs_inode_t **ips,
  396. int inodes,
  397. uint lock_mode)
  398. {
  399. int attempts = 0, i, j, try_lock;
  400. xfs_log_item_t *lp;
  401. /*
  402. * Currently supports between 2 and 5 inodes with exclusive locking. We
  403. * support an arbitrary depth of locking here, but absolute limits on
  404. * inodes depend on the the type of locking and the limits placed by
  405. * lockdep annotations in xfs_lock_inumorder. These are all checked by
  406. * the asserts.
  407. */
  408. ASSERT(ips && inodes >= 2 && inodes <= 5);
  409. ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
  410. XFS_ILOCK_EXCL));
  411. ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
  412. XFS_ILOCK_SHARED)));
  413. ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
  414. inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
  415. ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
  416. inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
  417. ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
  418. inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
  419. if (lock_mode & XFS_IOLOCK_EXCL) {
  420. ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
  421. } else if (lock_mode & XFS_MMAPLOCK_EXCL)
  422. ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
  423. try_lock = 0;
  424. i = 0;
  425. again:
  426. for (; i < inodes; i++) {
  427. ASSERT(ips[i]);
  428. if (i && (ips[i] == ips[i - 1])) /* Already locked */
  429. continue;
  430. /*
  431. * If try_lock is not set yet, make sure all locked inodes are
  432. * not in the AIL. If any are, set try_lock to be used later.
  433. */
  434. if (!try_lock) {
  435. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  436. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  437. if (lp && (lp->li_flags & XFS_LI_IN_AIL))
  438. try_lock++;
  439. }
  440. }
  441. /*
  442. * If any of the previous locks we have locked is in the AIL,
  443. * we must TRY to get the second and subsequent locks. If
  444. * we can't get any, we must release all we have
  445. * and try again.
  446. */
  447. if (!try_lock) {
  448. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  449. continue;
  450. }
  451. /* try_lock means we have an inode locked that is in the AIL. */
  452. ASSERT(i != 0);
  453. if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
  454. continue;
  455. /*
  456. * Unlock all previous guys and try again. xfs_iunlock will try
  457. * to push the tail if the inode is in the AIL.
  458. */
  459. attempts++;
  460. for (j = i - 1; j >= 0; j--) {
  461. /*
  462. * Check to see if we've already unlocked this one. Not
  463. * the first one going back, and the inode ptr is the
  464. * same.
  465. */
  466. if (j != (i - 1) && ips[j] == ips[j + 1])
  467. continue;
  468. xfs_iunlock(ips[j], lock_mode);
  469. }
  470. if ((attempts % 5) == 0) {
  471. delay(1); /* Don't just spin the CPU */
  472. #ifdef DEBUG
  473. xfs_lock_delays++;
  474. #endif
  475. }
  476. i = 0;
  477. try_lock = 0;
  478. goto again;
  479. }
  480. #ifdef DEBUG
  481. if (attempts) {
  482. if (attempts < 5) xfs_small_retries++;
  483. else if (attempts < 100) xfs_middle_retries++;
  484. else xfs_lots_retries++;
  485. } else {
  486. xfs_locked_n++;
  487. }
  488. #endif
  489. }
  490. /*
  491. * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
  492. * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  493. * lock more than one at a time, lockdep will report false positives saying we
  494. * have violated locking orders.
  495. */
  496. void
  497. xfs_lock_two_inodes(
  498. xfs_inode_t *ip0,
  499. xfs_inode_t *ip1,
  500. uint lock_mode)
  501. {
  502. xfs_inode_t *temp;
  503. int attempts = 0;
  504. xfs_log_item_t *lp;
  505. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  506. ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
  507. ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
  508. } else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
  509. ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
  510. ASSERT(ip0->i_ino != ip1->i_ino);
  511. if (ip0->i_ino > ip1->i_ino) {
  512. temp = ip0;
  513. ip0 = ip1;
  514. ip1 = temp;
  515. }
  516. again:
  517. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  518. /*
  519. * If the first lock we have locked is in the AIL, we must TRY to get
  520. * the second lock. If we can't get it, we must release the first one
  521. * and try again.
  522. */
  523. lp = (xfs_log_item_t *)ip0->i_itemp;
  524. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  525. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  526. xfs_iunlock(ip0, lock_mode);
  527. if ((++attempts % 5) == 0)
  528. delay(1); /* Don't just spin the CPU */
  529. goto again;
  530. }
  531. } else {
  532. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  533. }
  534. }
  535. void
  536. __xfs_iflock(
  537. struct xfs_inode *ip)
  538. {
  539. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
  540. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
  541. do {
  542. prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  543. if (xfs_isiflocked(ip))
  544. io_schedule();
  545. } while (!xfs_iflock_nowait(ip));
  546. finish_wait(wq, &wait.wait);
  547. }
  548. STATIC uint
  549. _xfs_dic2xflags(
  550. __uint16_t di_flags,
  551. uint64_t di_flags2,
  552. bool has_attr)
  553. {
  554. uint flags = 0;
  555. if (di_flags & XFS_DIFLAG_ANY) {
  556. if (di_flags & XFS_DIFLAG_REALTIME)
  557. flags |= FS_XFLAG_REALTIME;
  558. if (di_flags & XFS_DIFLAG_PREALLOC)
  559. flags |= FS_XFLAG_PREALLOC;
  560. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  561. flags |= FS_XFLAG_IMMUTABLE;
  562. if (di_flags & XFS_DIFLAG_APPEND)
  563. flags |= FS_XFLAG_APPEND;
  564. if (di_flags & XFS_DIFLAG_SYNC)
  565. flags |= FS_XFLAG_SYNC;
  566. if (di_flags & XFS_DIFLAG_NOATIME)
  567. flags |= FS_XFLAG_NOATIME;
  568. if (di_flags & XFS_DIFLAG_NODUMP)
  569. flags |= FS_XFLAG_NODUMP;
  570. if (di_flags & XFS_DIFLAG_RTINHERIT)
  571. flags |= FS_XFLAG_RTINHERIT;
  572. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  573. flags |= FS_XFLAG_PROJINHERIT;
  574. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  575. flags |= FS_XFLAG_NOSYMLINKS;
  576. if (di_flags & XFS_DIFLAG_EXTSIZE)
  577. flags |= FS_XFLAG_EXTSIZE;
  578. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  579. flags |= FS_XFLAG_EXTSZINHERIT;
  580. if (di_flags & XFS_DIFLAG_NODEFRAG)
  581. flags |= FS_XFLAG_NODEFRAG;
  582. if (di_flags & XFS_DIFLAG_FILESTREAM)
  583. flags |= FS_XFLAG_FILESTREAM;
  584. }
  585. if (di_flags2 & XFS_DIFLAG2_ANY) {
  586. if (di_flags2 & XFS_DIFLAG2_DAX)
  587. flags |= FS_XFLAG_DAX;
  588. }
  589. if (has_attr)
  590. flags |= FS_XFLAG_HASATTR;
  591. return flags;
  592. }
  593. uint
  594. xfs_ip2xflags(
  595. struct xfs_inode *ip)
  596. {
  597. struct xfs_icdinode *dic = &ip->i_d;
  598. return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
  599. }
  600. uint
  601. xfs_dic2xflags(
  602. struct xfs_dinode *dip)
  603. {
  604. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags),
  605. be64_to_cpu(dip->di_flags2), XFS_DFORK_Q(dip));
  606. }
  607. /*
  608. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  609. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  610. * ci_name->name will point to a the actual name (caller must free) or
  611. * will be set to NULL if an exact match is found.
  612. */
  613. int
  614. xfs_lookup(
  615. xfs_inode_t *dp,
  616. struct xfs_name *name,
  617. xfs_inode_t **ipp,
  618. struct xfs_name *ci_name)
  619. {
  620. xfs_ino_t inum;
  621. int error;
  622. trace_xfs_lookup(dp, name);
  623. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  624. return -EIO;
  625. xfs_ilock(dp, XFS_IOLOCK_SHARED);
  626. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  627. if (error)
  628. goto out_unlock;
  629. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  630. if (error)
  631. goto out_free_name;
  632. xfs_iunlock(dp, XFS_IOLOCK_SHARED);
  633. return 0;
  634. out_free_name:
  635. if (ci_name)
  636. kmem_free(ci_name->name);
  637. out_unlock:
  638. xfs_iunlock(dp, XFS_IOLOCK_SHARED);
  639. *ipp = NULL;
  640. return error;
  641. }
  642. /*
  643. * Allocate an inode on disk and return a copy of its in-core version.
  644. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  645. * appropriately within the inode. The uid and gid for the inode are
  646. * set according to the contents of the given cred structure.
  647. *
  648. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  649. * has a free inode available, call xfs_iget() to obtain the in-core
  650. * version of the allocated inode. Finally, fill in the inode and
  651. * log its initial contents. In this case, ialloc_context would be
  652. * set to NULL.
  653. *
  654. * If xfs_dialloc() does not have an available inode, it will replenish
  655. * its supply by doing an allocation. Since we can only do one
  656. * allocation within a transaction without deadlocks, we must commit
  657. * the current transaction before returning the inode itself.
  658. * In this case, therefore, we will set ialloc_context and return.
  659. * The caller should then commit the current transaction, start a new
  660. * transaction, and call xfs_ialloc() again to actually get the inode.
  661. *
  662. * To ensure that some other process does not grab the inode that
  663. * was allocated during the first call to xfs_ialloc(), this routine
  664. * also returns the [locked] bp pointing to the head of the freelist
  665. * as ialloc_context. The caller should hold this buffer across
  666. * the commit and pass it back into this routine on the second call.
  667. *
  668. * If we are allocating quota inodes, we do not have a parent inode
  669. * to attach to or associate with (i.e. pip == NULL) because they
  670. * are not linked into the directory structure - they are attached
  671. * directly to the superblock - and so have no parent.
  672. */
  673. int
  674. xfs_ialloc(
  675. xfs_trans_t *tp,
  676. xfs_inode_t *pip,
  677. umode_t mode,
  678. xfs_nlink_t nlink,
  679. xfs_dev_t rdev,
  680. prid_t prid,
  681. int okalloc,
  682. xfs_buf_t **ialloc_context,
  683. xfs_inode_t **ipp)
  684. {
  685. struct xfs_mount *mp = tp->t_mountp;
  686. xfs_ino_t ino;
  687. xfs_inode_t *ip;
  688. uint flags;
  689. int error;
  690. struct timespec tv;
  691. struct inode *inode;
  692. /*
  693. * Call the space management code to pick
  694. * the on-disk inode to be allocated.
  695. */
  696. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  697. ialloc_context, &ino);
  698. if (error)
  699. return error;
  700. if (*ialloc_context || ino == NULLFSINO) {
  701. *ipp = NULL;
  702. return 0;
  703. }
  704. ASSERT(*ialloc_context == NULL);
  705. /*
  706. * Get the in-core inode with the lock held exclusively.
  707. * This is because we're setting fields here we need
  708. * to prevent others from looking at until we're done.
  709. */
  710. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  711. XFS_ILOCK_EXCL, &ip);
  712. if (error)
  713. return error;
  714. ASSERT(ip != NULL);
  715. inode = VFS_I(ip);
  716. /*
  717. * We always convert v1 inodes to v2 now - we only support filesystems
  718. * with >= v2 inode capability, so there is no reason for ever leaving
  719. * an inode in v1 format.
  720. */
  721. if (ip->i_d.di_version == 1)
  722. ip->i_d.di_version = 2;
  723. ip->i_d.di_mode = mode;
  724. set_nlink(inode, nlink);
  725. ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
  726. ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
  727. xfs_set_projid(ip, prid);
  728. if (pip && XFS_INHERIT_GID(pip)) {
  729. ip->i_d.di_gid = pip->i_d.di_gid;
  730. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  731. ip->i_d.di_mode |= S_ISGID;
  732. }
  733. }
  734. /*
  735. * If the group ID of the new file does not match the effective group
  736. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  737. * (and only if the irix_sgid_inherit compatibility variable is set).
  738. */
  739. if ((irix_sgid_inherit) &&
  740. (ip->i_d.di_mode & S_ISGID) &&
  741. (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
  742. ip->i_d.di_mode &= ~S_ISGID;
  743. }
  744. ip->i_d.di_size = 0;
  745. ip->i_d.di_nextents = 0;
  746. ASSERT(ip->i_d.di_nblocks == 0);
  747. tv = current_fs_time(mp->m_super);
  748. inode->i_mtime = tv;
  749. inode->i_atime = tv;
  750. inode->i_ctime = tv;
  751. ip->i_d.di_extsize = 0;
  752. ip->i_d.di_dmevmask = 0;
  753. ip->i_d.di_dmstate = 0;
  754. ip->i_d.di_flags = 0;
  755. if (ip->i_d.di_version == 3) {
  756. ip->i_d.di_changecount = 1;
  757. ip->i_d.di_flags2 = 0;
  758. ip->i_d.di_crtime.t_sec = (__int32_t)tv.tv_sec;
  759. ip->i_d.di_crtime.t_nsec = (__int32_t)tv.tv_nsec;
  760. }
  761. flags = XFS_ILOG_CORE;
  762. switch (mode & S_IFMT) {
  763. case S_IFIFO:
  764. case S_IFCHR:
  765. case S_IFBLK:
  766. case S_IFSOCK:
  767. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  768. ip->i_df.if_u2.if_rdev = rdev;
  769. ip->i_df.if_flags = 0;
  770. flags |= XFS_ILOG_DEV;
  771. break;
  772. case S_IFREG:
  773. case S_IFDIR:
  774. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  775. uint64_t di_flags2 = 0;
  776. uint di_flags = 0;
  777. if (S_ISDIR(mode)) {
  778. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  779. di_flags |= XFS_DIFLAG_RTINHERIT;
  780. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  781. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  782. ip->i_d.di_extsize = pip->i_d.di_extsize;
  783. }
  784. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  785. di_flags |= XFS_DIFLAG_PROJINHERIT;
  786. } else if (S_ISREG(mode)) {
  787. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  788. di_flags |= XFS_DIFLAG_REALTIME;
  789. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  790. di_flags |= XFS_DIFLAG_EXTSIZE;
  791. ip->i_d.di_extsize = pip->i_d.di_extsize;
  792. }
  793. }
  794. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  795. xfs_inherit_noatime)
  796. di_flags |= XFS_DIFLAG_NOATIME;
  797. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  798. xfs_inherit_nodump)
  799. di_flags |= XFS_DIFLAG_NODUMP;
  800. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  801. xfs_inherit_sync)
  802. di_flags |= XFS_DIFLAG_SYNC;
  803. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  804. xfs_inherit_nosymlinks)
  805. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  806. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  807. xfs_inherit_nodefrag)
  808. di_flags |= XFS_DIFLAG_NODEFRAG;
  809. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  810. di_flags |= XFS_DIFLAG_FILESTREAM;
  811. if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
  812. di_flags2 |= XFS_DIFLAG2_DAX;
  813. ip->i_d.di_flags |= di_flags;
  814. ip->i_d.di_flags2 |= di_flags2;
  815. }
  816. /* FALLTHROUGH */
  817. case S_IFLNK:
  818. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  819. ip->i_df.if_flags = XFS_IFEXTENTS;
  820. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  821. ip->i_df.if_u1.if_extents = NULL;
  822. break;
  823. default:
  824. ASSERT(0);
  825. }
  826. /*
  827. * Attribute fork settings for new inode.
  828. */
  829. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  830. ip->i_d.di_anextents = 0;
  831. /*
  832. * Log the new values stuffed into the inode.
  833. */
  834. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  835. xfs_trans_log_inode(tp, ip, flags);
  836. /* now that we have an i_mode we can setup the inode structure */
  837. xfs_setup_inode(ip);
  838. *ipp = ip;
  839. return 0;
  840. }
  841. /*
  842. * Allocates a new inode from disk and return a pointer to the
  843. * incore copy. This routine will internally commit the current
  844. * transaction and allocate a new one if the Space Manager needed
  845. * to do an allocation to replenish the inode free-list.
  846. *
  847. * This routine is designed to be called from xfs_create and
  848. * xfs_create_dir.
  849. *
  850. */
  851. int
  852. xfs_dir_ialloc(
  853. xfs_trans_t **tpp, /* input: current transaction;
  854. output: may be a new transaction. */
  855. xfs_inode_t *dp, /* directory within whose allocate
  856. the inode. */
  857. umode_t mode,
  858. xfs_nlink_t nlink,
  859. xfs_dev_t rdev,
  860. prid_t prid, /* project id */
  861. int okalloc, /* ok to allocate new space */
  862. xfs_inode_t **ipp, /* pointer to inode; it will be
  863. locked. */
  864. int *committed)
  865. {
  866. xfs_trans_t *tp;
  867. xfs_inode_t *ip;
  868. xfs_buf_t *ialloc_context = NULL;
  869. int code;
  870. void *dqinfo;
  871. uint tflags;
  872. tp = *tpp;
  873. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  874. /*
  875. * xfs_ialloc will return a pointer to an incore inode if
  876. * the Space Manager has an available inode on the free
  877. * list. Otherwise, it will do an allocation and replenish
  878. * the freelist. Since we can only do one allocation per
  879. * transaction without deadlocks, we will need to commit the
  880. * current transaction and start a new one. We will then
  881. * need to call xfs_ialloc again to get the inode.
  882. *
  883. * If xfs_ialloc did an allocation to replenish the freelist,
  884. * it returns the bp containing the head of the freelist as
  885. * ialloc_context. We will hold a lock on it across the
  886. * transaction commit so that no other process can steal
  887. * the inode(s) that we've just allocated.
  888. */
  889. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
  890. &ialloc_context, &ip);
  891. /*
  892. * Return an error if we were unable to allocate a new inode.
  893. * This should only happen if we run out of space on disk or
  894. * encounter a disk error.
  895. */
  896. if (code) {
  897. *ipp = NULL;
  898. return code;
  899. }
  900. if (!ialloc_context && !ip) {
  901. *ipp = NULL;
  902. return -ENOSPC;
  903. }
  904. /*
  905. * If the AGI buffer is non-NULL, then we were unable to get an
  906. * inode in one operation. We need to commit the current
  907. * transaction and call xfs_ialloc() again. It is guaranteed
  908. * to succeed the second time.
  909. */
  910. if (ialloc_context) {
  911. /*
  912. * Normally, xfs_trans_commit releases all the locks.
  913. * We call bhold to hang on to the ialloc_context across
  914. * the commit. Holding this buffer prevents any other
  915. * processes from doing any allocations in this
  916. * allocation group.
  917. */
  918. xfs_trans_bhold(tp, ialloc_context);
  919. /*
  920. * We want the quota changes to be associated with the next
  921. * transaction, NOT this one. So, detach the dqinfo from this
  922. * and attach it to the next transaction.
  923. */
  924. dqinfo = NULL;
  925. tflags = 0;
  926. if (tp->t_dqinfo) {
  927. dqinfo = (void *)tp->t_dqinfo;
  928. tp->t_dqinfo = NULL;
  929. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  930. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  931. }
  932. code = xfs_trans_roll(&tp, 0);
  933. if (committed != NULL)
  934. *committed = 1;
  935. /*
  936. * Re-attach the quota info that we detached from prev trx.
  937. */
  938. if (dqinfo) {
  939. tp->t_dqinfo = dqinfo;
  940. tp->t_flags |= tflags;
  941. }
  942. if (code) {
  943. xfs_buf_relse(ialloc_context);
  944. *tpp = tp;
  945. *ipp = NULL;
  946. return code;
  947. }
  948. xfs_trans_bjoin(tp, ialloc_context);
  949. /*
  950. * Call ialloc again. Since we've locked out all
  951. * other allocations in this allocation group,
  952. * this call should always succeed.
  953. */
  954. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
  955. okalloc, &ialloc_context, &ip);
  956. /*
  957. * If we get an error at this point, return to the caller
  958. * so that the current transaction can be aborted.
  959. */
  960. if (code) {
  961. *tpp = tp;
  962. *ipp = NULL;
  963. return code;
  964. }
  965. ASSERT(!ialloc_context && ip);
  966. } else {
  967. if (committed != NULL)
  968. *committed = 0;
  969. }
  970. *ipp = ip;
  971. *tpp = tp;
  972. return 0;
  973. }
  974. /*
  975. * Decrement the link count on an inode & log the change. If this causes the
  976. * link count to go to zero, move the inode to AGI unlinked list so that it can
  977. * be freed when the last active reference goes away via xfs_inactive().
  978. */
  979. int /* error */
  980. xfs_droplink(
  981. xfs_trans_t *tp,
  982. xfs_inode_t *ip)
  983. {
  984. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  985. drop_nlink(VFS_I(ip));
  986. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  987. if (VFS_I(ip)->i_nlink)
  988. return 0;
  989. return xfs_iunlink(tp, ip);
  990. }
  991. /*
  992. * Increment the link count on an inode & log the change.
  993. */
  994. int
  995. xfs_bumplink(
  996. xfs_trans_t *tp,
  997. xfs_inode_t *ip)
  998. {
  999. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  1000. ASSERT(ip->i_d.di_version > 1);
  1001. inc_nlink(VFS_I(ip));
  1002. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1003. return 0;
  1004. }
  1005. int
  1006. xfs_create(
  1007. xfs_inode_t *dp,
  1008. struct xfs_name *name,
  1009. umode_t mode,
  1010. xfs_dev_t rdev,
  1011. xfs_inode_t **ipp)
  1012. {
  1013. int is_dir = S_ISDIR(mode);
  1014. struct xfs_mount *mp = dp->i_mount;
  1015. struct xfs_inode *ip = NULL;
  1016. struct xfs_trans *tp = NULL;
  1017. int error;
  1018. xfs_bmap_free_t free_list;
  1019. xfs_fsblock_t first_block;
  1020. bool unlock_dp_on_error = false;
  1021. prid_t prid;
  1022. struct xfs_dquot *udqp = NULL;
  1023. struct xfs_dquot *gdqp = NULL;
  1024. struct xfs_dquot *pdqp = NULL;
  1025. struct xfs_trans_res *tres;
  1026. uint resblks;
  1027. trace_xfs_create(dp, name);
  1028. if (XFS_FORCED_SHUTDOWN(mp))
  1029. return -EIO;
  1030. prid = xfs_get_initial_prid(dp);
  1031. /*
  1032. * Make sure that we have allocated dquot(s) on disk.
  1033. */
  1034. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1035. xfs_kgid_to_gid(current_fsgid()), prid,
  1036. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1037. &udqp, &gdqp, &pdqp);
  1038. if (error)
  1039. return error;
  1040. if (is_dir) {
  1041. rdev = 0;
  1042. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1043. tres = &M_RES(mp)->tr_mkdir;
  1044. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1045. } else {
  1046. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1047. tres = &M_RES(mp)->tr_create;
  1048. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1049. }
  1050. /*
  1051. * Initially assume that the file does not exist and
  1052. * reserve the resources for that case. If that is not
  1053. * the case we'll drop the one we have and get a more
  1054. * appropriate transaction later.
  1055. */
  1056. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1057. if (error == -ENOSPC) {
  1058. /* flush outstanding delalloc blocks and retry */
  1059. xfs_flush_inodes(mp);
  1060. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1061. }
  1062. if (error == -ENOSPC) {
  1063. /* No space at all so try a "no-allocation" reservation */
  1064. resblks = 0;
  1065. error = xfs_trans_reserve(tp, tres, 0, 0);
  1066. }
  1067. if (error)
  1068. goto out_trans_cancel;
  1069. xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
  1070. XFS_IOLOCK_PARENT | XFS_ILOCK_PARENT);
  1071. unlock_dp_on_error = true;
  1072. xfs_bmap_init(&free_list, &first_block);
  1073. /*
  1074. * Reserve disk quota and the inode.
  1075. */
  1076. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1077. pdqp, resblks, 1, 0);
  1078. if (error)
  1079. goto out_trans_cancel;
  1080. if (!resblks) {
  1081. error = xfs_dir_canenter(tp, dp, name);
  1082. if (error)
  1083. goto out_trans_cancel;
  1084. }
  1085. /*
  1086. * A newly created regular or special file just has one directory
  1087. * entry pointing to them, but a directory also the "." entry
  1088. * pointing to itself.
  1089. */
  1090. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  1091. prid, resblks > 0, &ip, NULL);
  1092. if (error)
  1093. goto out_trans_cancel;
  1094. /*
  1095. * Now we join the directory inode to the transaction. We do not do it
  1096. * earlier because xfs_dir_ialloc might commit the previous transaction
  1097. * (and release all the locks). An error from here on will result in
  1098. * the transaction cancel unlocking dp so don't do it explicitly in the
  1099. * error path.
  1100. */
  1101. xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1102. unlock_dp_on_error = false;
  1103. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1104. &first_block, &free_list, resblks ?
  1105. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1106. if (error) {
  1107. ASSERT(error != -ENOSPC);
  1108. goto out_trans_cancel;
  1109. }
  1110. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1111. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1112. if (is_dir) {
  1113. error = xfs_dir_init(tp, ip, dp);
  1114. if (error)
  1115. goto out_bmap_cancel;
  1116. error = xfs_bumplink(tp, dp);
  1117. if (error)
  1118. goto out_bmap_cancel;
  1119. }
  1120. /*
  1121. * If this is a synchronous mount, make sure that the
  1122. * create transaction goes to disk before returning to
  1123. * the user.
  1124. */
  1125. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1126. xfs_trans_set_sync(tp);
  1127. /*
  1128. * Attach the dquot(s) to the inodes and modify them incore.
  1129. * These ids of the inode couldn't have changed since the new
  1130. * inode has been locked ever since it was created.
  1131. */
  1132. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1133. error = xfs_bmap_finish(&tp, &free_list, NULL);
  1134. if (error)
  1135. goto out_bmap_cancel;
  1136. error = xfs_trans_commit(tp);
  1137. if (error)
  1138. goto out_release_inode;
  1139. xfs_qm_dqrele(udqp);
  1140. xfs_qm_dqrele(gdqp);
  1141. xfs_qm_dqrele(pdqp);
  1142. *ipp = ip;
  1143. return 0;
  1144. out_bmap_cancel:
  1145. xfs_bmap_cancel(&free_list);
  1146. out_trans_cancel:
  1147. xfs_trans_cancel(tp);
  1148. out_release_inode:
  1149. /*
  1150. * Wait until after the current transaction is aborted to finish the
  1151. * setup of the inode and release the inode. This prevents recursive
  1152. * transactions and deadlocks from xfs_inactive.
  1153. */
  1154. if (ip) {
  1155. xfs_finish_inode_setup(ip);
  1156. IRELE(ip);
  1157. }
  1158. xfs_qm_dqrele(udqp);
  1159. xfs_qm_dqrele(gdqp);
  1160. xfs_qm_dqrele(pdqp);
  1161. if (unlock_dp_on_error)
  1162. xfs_iunlock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1163. return error;
  1164. }
  1165. int
  1166. xfs_create_tmpfile(
  1167. struct xfs_inode *dp,
  1168. struct dentry *dentry,
  1169. umode_t mode,
  1170. struct xfs_inode **ipp)
  1171. {
  1172. struct xfs_mount *mp = dp->i_mount;
  1173. struct xfs_inode *ip = NULL;
  1174. struct xfs_trans *tp = NULL;
  1175. int error;
  1176. prid_t prid;
  1177. struct xfs_dquot *udqp = NULL;
  1178. struct xfs_dquot *gdqp = NULL;
  1179. struct xfs_dquot *pdqp = NULL;
  1180. struct xfs_trans_res *tres;
  1181. uint resblks;
  1182. if (XFS_FORCED_SHUTDOWN(mp))
  1183. return -EIO;
  1184. prid = xfs_get_initial_prid(dp);
  1185. /*
  1186. * Make sure that we have allocated dquot(s) on disk.
  1187. */
  1188. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1189. xfs_kgid_to_gid(current_fsgid()), prid,
  1190. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1191. &udqp, &gdqp, &pdqp);
  1192. if (error)
  1193. return error;
  1194. resblks = XFS_IALLOC_SPACE_RES(mp);
  1195. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
  1196. tres = &M_RES(mp)->tr_create_tmpfile;
  1197. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1198. if (error == -ENOSPC) {
  1199. /* No space at all so try a "no-allocation" reservation */
  1200. resblks = 0;
  1201. error = xfs_trans_reserve(tp, tres, 0, 0);
  1202. }
  1203. if (error)
  1204. goto out_trans_cancel;
  1205. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1206. pdqp, resblks, 1, 0);
  1207. if (error)
  1208. goto out_trans_cancel;
  1209. error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
  1210. prid, resblks > 0, &ip, NULL);
  1211. if (error)
  1212. goto out_trans_cancel;
  1213. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1214. xfs_trans_set_sync(tp);
  1215. /*
  1216. * Attach the dquot(s) to the inodes and modify them incore.
  1217. * These ids of the inode couldn't have changed since the new
  1218. * inode has been locked ever since it was created.
  1219. */
  1220. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1221. error = xfs_iunlink(tp, ip);
  1222. if (error)
  1223. goto out_trans_cancel;
  1224. error = xfs_trans_commit(tp);
  1225. if (error)
  1226. goto out_release_inode;
  1227. xfs_qm_dqrele(udqp);
  1228. xfs_qm_dqrele(gdqp);
  1229. xfs_qm_dqrele(pdqp);
  1230. *ipp = ip;
  1231. return 0;
  1232. out_trans_cancel:
  1233. xfs_trans_cancel(tp);
  1234. out_release_inode:
  1235. /*
  1236. * Wait until after the current transaction is aborted to finish the
  1237. * setup of the inode and release the inode. This prevents recursive
  1238. * transactions and deadlocks from xfs_inactive.
  1239. */
  1240. if (ip) {
  1241. xfs_finish_inode_setup(ip);
  1242. IRELE(ip);
  1243. }
  1244. xfs_qm_dqrele(udqp);
  1245. xfs_qm_dqrele(gdqp);
  1246. xfs_qm_dqrele(pdqp);
  1247. return error;
  1248. }
  1249. int
  1250. xfs_link(
  1251. xfs_inode_t *tdp,
  1252. xfs_inode_t *sip,
  1253. struct xfs_name *target_name)
  1254. {
  1255. xfs_mount_t *mp = tdp->i_mount;
  1256. xfs_trans_t *tp;
  1257. int error;
  1258. xfs_bmap_free_t free_list;
  1259. xfs_fsblock_t first_block;
  1260. int resblks;
  1261. trace_xfs_link(tdp, target_name);
  1262. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1263. if (XFS_FORCED_SHUTDOWN(mp))
  1264. return -EIO;
  1265. error = xfs_qm_dqattach(sip, 0);
  1266. if (error)
  1267. goto std_return;
  1268. error = xfs_qm_dqattach(tdp, 0);
  1269. if (error)
  1270. goto std_return;
  1271. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1272. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1273. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
  1274. if (error == -ENOSPC) {
  1275. resblks = 0;
  1276. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
  1277. }
  1278. if (error)
  1279. goto error_return;
  1280. xfs_ilock(tdp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
  1281. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1282. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1283. xfs_trans_ijoin(tp, tdp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1284. /*
  1285. * If we are using project inheritance, we only allow hard link
  1286. * creation in our tree when the project IDs are the same; else
  1287. * the tree quota mechanism could be circumvented.
  1288. */
  1289. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1290. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1291. error = -EXDEV;
  1292. goto error_return;
  1293. }
  1294. if (!resblks) {
  1295. error = xfs_dir_canenter(tp, tdp, target_name);
  1296. if (error)
  1297. goto error_return;
  1298. }
  1299. xfs_bmap_init(&free_list, &first_block);
  1300. /*
  1301. * Handle initial link state of O_TMPFILE inode
  1302. */
  1303. if (VFS_I(sip)->i_nlink == 0) {
  1304. error = xfs_iunlink_remove(tp, sip);
  1305. if (error)
  1306. goto error_return;
  1307. }
  1308. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1309. &first_block, &free_list, resblks);
  1310. if (error)
  1311. goto error_return;
  1312. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1313. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1314. error = xfs_bumplink(tp, sip);
  1315. if (error)
  1316. goto error_return;
  1317. /*
  1318. * If this is a synchronous mount, make sure that the
  1319. * link transaction goes to disk before returning to
  1320. * the user.
  1321. */
  1322. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1323. xfs_trans_set_sync(tp);
  1324. error = xfs_bmap_finish(&tp, &free_list, NULL);
  1325. if (error) {
  1326. xfs_bmap_cancel(&free_list);
  1327. goto error_return;
  1328. }
  1329. return xfs_trans_commit(tp);
  1330. error_return:
  1331. xfs_trans_cancel(tp);
  1332. std_return:
  1333. return error;
  1334. }
  1335. /*
  1336. * Free up the underlying blocks past new_size. The new size must be smaller
  1337. * than the current size. This routine can be used both for the attribute and
  1338. * data fork, and does not modify the inode size, which is left to the caller.
  1339. *
  1340. * The transaction passed to this routine must have made a permanent log
  1341. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1342. * given transaction and start new ones, so make sure everything involved in
  1343. * the transaction is tidy before calling here. Some transaction will be
  1344. * returned to the caller to be committed. The incoming transaction must
  1345. * already include the inode, and both inode locks must be held exclusively.
  1346. * The inode must also be "held" within the transaction. On return the inode
  1347. * will be "held" within the returned transaction. This routine does NOT
  1348. * require any disk space to be reserved for it within the transaction.
  1349. *
  1350. * If we get an error, we must return with the inode locked and linked into the
  1351. * current transaction. This keeps things simple for the higher level code,
  1352. * because it always knows that the inode is locked and held in the transaction
  1353. * that returns to it whether errors occur or not. We don't mark the inode
  1354. * dirty on error so that transactions can be easily aborted if possible.
  1355. */
  1356. int
  1357. xfs_itruncate_extents(
  1358. struct xfs_trans **tpp,
  1359. struct xfs_inode *ip,
  1360. int whichfork,
  1361. xfs_fsize_t new_size)
  1362. {
  1363. struct xfs_mount *mp = ip->i_mount;
  1364. struct xfs_trans *tp = *tpp;
  1365. xfs_bmap_free_t free_list;
  1366. xfs_fsblock_t first_block;
  1367. xfs_fileoff_t first_unmap_block;
  1368. xfs_fileoff_t last_block;
  1369. xfs_filblks_t unmap_len;
  1370. int error = 0;
  1371. int done = 0;
  1372. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1373. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1374. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1375. ASSERT(new_size <= XFS_ISIZE(ip));
  1376. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1377. ASSERT(ip->i_itemp != NULL);
  1378. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1379. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1380. trace_xfs_itruncate_extents_start(ip, new_size);
  1381. /*
  1382. * Since it is possible for space to become allocated beyond
  1383. * the end of the file (in a crash where the space is allocated
  1384. * but the inode size is not yet updated), simply remove any
  1385. * blocks which show up between the new EOF and the maximum
  1386. * possible file size. If the first block to be removed is
  1387. * beyond the maximum file size (ie it is the same as last_block),
  1388. * then there is nothing to do.
  1389. */
  1390. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1391. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1392. if (first_unmap_block == last_block)
  1393. return 0;
  1394. ASSERT(first_unmap_block < last_block);
  1395. unmap_len = last_block - first_unmap_block + 1;
  1396. while (!done) {
  1397. xfs_bmap_init(&free_list, &first_block);
  1398. error = xfs_bunmapi(tp, ip,
  1399. first_unmap_block, unmap_len,
  1400. xfs_bmapi_aflag(whichfork),
  1401. XFS_ITRUNC_MAX_EXTENTS,
  1402. &first_block, &free_list,
  1403. &done);
  1404. if (error)
  1405. goto out_bmap_cancel;
  1406. /*
  1407. * Duplicate the transaction that has the permanent
  1408. * reservation and commit the old transaction.
  1409. */
  1410. error = xfs_bmap_finish(&tp, &free_list, ip);
  1411. if (error)
  1412. goto out_bmap_cancel;
  1413. error = xfs_trans_roll(&tp, ip);
  1414. if (error)
  1415. goto out;
  1416. }
  1417. /*
  1418. * Always re-log the inode so that our permanent transaction can keep
  1419. * on rolling it forward in the log.
  1420. */
  1421. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1422. trace_xfs_itruncate_extents_end(ip, new_size);
  1423. out:
  1424. *tpp = tp;
  1425. return error;
  1426. out_bmap_cancel:
  1427. /*
  1428. * If the bunmapi call encounters an error, return to the caller where
  1429. * the transaction can be properly aborted. We just need to make sure
  1430. * we're not holding any resources that we were not when we came in.
  1431. */
  1432. xfs_bmap_cancel(&free_list);
  1433. goto out;
  1434. }
  1435. int
  1436. xfs_release(
  1437. xfs_inode_t *ip)
  1438. {
  1439. xfs_mount_t *mp = ip->i_mount;
  1440. int error;
  1441. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1442. return 0;
  1443. /* If this is a read-only mount, don't do this (would generate I/O) */
  1444. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1445. return 0;
  1446. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1447. int truncated;
  1448. /*
  1449. * If we previously truncated this file and removed old data
  1450. * in the process, we want to initiate "early" writeout on
  1451. * the last close. This is an attempt to combat the notorious
  1452. * NULL files problem which is particularly noticeable from a
  1453. * truncate down, buffered (re-)write (delalloc), followed by
  1454. * a crash. What we are effectively doing here is
  1455. * significantly reducing the time window where we'd otherwise
  1456. * be exposed to that problem.
  1457. */
  1458. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1459. if (truncated) {
  1460. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1461. if (ip->i_delayed_blks > 0) {
  1462. error = filemap_flush(VFS_I(ip)->i_mapping);
  1463. if (error)
  1464. return error;
  1465. }
  1466. }
  1467. }
  1468. if (VFS_I(ip)->i_nlink == 0)
  1469. return 0;
  1470. if (xfs_can_free_eofblocks(ip, false)) {
  1471. /*
  1472. * If we can't get the iolock just skip truncating the blocks
  1473. * past EOF because we could deadlock with the mmap_sem
  1474. * otherwise. We'll get another chance to drop them once the
  1475. * last reference to the inode is dropped, so we'll never leak
  1476. * blocks permanently.
  1477. *
  1478. * Further, check if the inode is being opened, written and
  1479. * closed frequently and we have delayed allocation blocks
  1480. * outstanding (e.g. streaming writes from the NFS server),
  1481. * truncating the blocks past EOF will cause fragmentation to
  1482. * occur.
  1483. *
  1484. * In this case don't do the truncation, either, but we have to
  1485. * be careful how we detect this case. Blocks beyond EOF show
  1486. * up as i_delayed_blks even when the inode is clean, so we
  1487. * need to truncate them away first before checking for a dirty
  1488. * release. Hence on the first dirty close we will still remove
  1489. * the speculative allocation, but after that we will leave it
  1490. * in place.
  1491. */
  1492. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1493. return 0;
  1494. error = xfs_free_eofblocks(mp, ip, true);
  1495. if (error && error != -EAGAIN)
  1496. return error;
  1497. /* delalloc blocks after truncation means it really is dirty */
  1498. if (ip->i_delayed_blks)
  1499. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1500. }
  1501. return 0;
  1502. }
  1503. /*
  1504. * xfs_inactive_truncate
  1505. *
  1506. * Called to perform a truncate when an inode becomes unlinked.
  1507. */
  1508. STATIC int
  1509. xfs_inactive_truncate(
  1510. struct xfs_inode *ip)
  1511. {
  1512. struct xfs_mount *mp = ip->i_mount;
  1513. struct xfs_trans *tp;
  1514. int error;
  1515. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1516. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1517. if (error) {
  1518. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1519. xfs_trans_cancel(tp);
  1520. return error;
  1521. }
  1522. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1523. xfs_trans_ijoin(tp, ip, 0);
  1524. /*
  1525. * Log the inode size first to prevent stale data exposure in the event
  1526. * of a system crash before the truncate completes. See the related
  1527. * comment in xfs_setattr_size() for details.
  1528. */
  1529. ip->i_d.di_size = 0;
  1530. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1531. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1532. if (error)
  1533. goto error_trans_cancel;
  1534. ASSERT(ip->i_d.di_nextents == 0);
  1535. error = xfs_trans_commit(tp);
  1536. if (error)
  1537. goto error_unlock;
  1538. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1539. return 0;
  1540. error_trans_cancel:
  1541. xfs_trans_cancel(tp);
  1542. error_unlock:
  1543. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1544. return error;
  1545. }
  1546. /*
  1547. * xfs_inactive_ifree()
  1548. *
  1549. * Perform the inode free when an inode is unlinked.
  1550. */
  1551. STATIC int
  1552. xfs_inactive_ifree(
  1553. struct xfs_inode *ip)
  1554. {
  1555. xfs_bmap_free_t free_list;
  1556. xfs_fsblock_t first_block;
  1557. struct xfs_mount *mp = ip->i_mount;
  1558. struct xfs_trans *tp;
  1559. int error;
  1560. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1561. /*
  1562. * The ifree transaction might need to allocate blocks for record
  1563. * insertion to the finobt. We don't want to fail here at ENOSPC, so
  1564. * allow ifree to dip into the reserved block pool if necessary.
  1565. *
  1566. * Freeing large sets of inodes generally means freeing inode chunks,
  1567. * directory and file data blocks, so this should be relatively safe.
  1568. * Only under severe circumstances should it be possible to free enough
  1569. * inodes to exhaust the reserve block pool via finobt expansion while
  1570. * at the same time not creating free space in the filesystem.
  1571. *
  1572. * Send a warning if the reservation does happen to fail, as the inode
  1573. * now remains allocated and sits on the unlinked list until the fs is
  1574. * repaired.
  1575. */
  1576. tp->t_flags |= XFS_TRANS_RESERVE;
  1577. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
  1578. XFS_IFREE_SPACE_RES(mp), 0);
  1579. if (error) {
  1580. if (error == -ENOSPC) {
  1581. xfs_warn_ratelimited(mp,
  1582. "Failed to remove inode(s) from unlinked list. "
  1583. "Please free space, unmount and run xfs_repair.");
  1584. } else {
  1585. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1586. }
  1587. xfs_trans_cancel(tp);
  1588. return error;
  1589. }
  1590. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1591. xfs_trans_ijoin(tp, ip, 0);
  1592. xfs_bmap_init(&free_list, &first_block);
  1593. error = xfs_ifree(tp, ip, &free_list);
  1594. if (error) {
  1595. /*
  1596. * If we fail to free the inode, shut down. The cancel
  1597. * might do that, we need to make sure. Otherwise the
  1598. * inode might be lost for a long time or forever.
  1599. */
  1600. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1601. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1602. __func__, error);
  1603. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1604. }
  1605. xfs_trans_cancel(tp);
  1606. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1607. return error;
  1608. }
  1609. /*
  1610. * Credit the quota account(s). The inode is gone.
  1611. */
  1612. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1613. /*
  1614. * Just ignore errors at this point. There is nothing we can do except
  1615. * to try to keep going. Make sure it's not a silent error.
  1616. */
  1617. error = xfs_bmap_finish(&tp, &free_list, NULL);
  1618. if (error) {
  1619. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  1620. __func__, error);
  1621. xfs_bmap_cancel(&free_list);
  1622. }
  1623. error = xfs_trans_commit(tp);
  1624. if (error)
  1625. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1626. __func__, error);
  1627. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1628. return 0;
  1629. }
  1630. /*
  1631. * xfs_inactive
  1632. *
  1633. * This is called when the vnode reference count for the vnode
  1634. * goes to zero. If the file has been unlinked, then it must
  1635. * now be truncated. Also, we clear all of the read-ahead state
  1636. * kept for the inode here since the file is now closed.
  1637. */
  1638. void
  1639. xfs_inactive(
  1640. xfs_inode_t *ip)
  1641. {
  1642. struct xfs_mount *mp;
  1643. int error;
  1644. int truncate = 0;
  1645. /*
  1646. * If the inode is already free, then there can be nothing
  1647. * to clean up here.
  1648. */
  1649. if (ip->i_d.di_mode == 0) {
  1650. ASSERT(ip->i_df.if_real_bytes == 0);
  1651. ASSERT(ip->i_df.if_broot_bytes == 0);
  1652. return;
  1653. }
  1654. mp = ip->i_mount;
  1655. /* If this is a read-only mount, don't do this (would generate I/O) */
  1656. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1657. return;
  1658. if (VFS_I(ip)->i_nlink != 0) {
  1659. /*
  1660. * force is true because we are evicting an inode from the
  1661. * cache. Post-eof blocks must be freed, lest we end up with
  1662. * broken free space accounting.
  1663. */
  1664. if (xfs_can_free_eofblocks(ip, true))
  1665. xfs_free_eofblocks(mp, ip, false);
  1666. return;
  1667. }
  1668. if (S_ISREG(ip->i_d.di_mode) &&
  1669. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1670. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  1671. truncate = 1;
  1672. error = xfs_qm_dqattach(ip, 0);
  1673. if (error)
  1674. return;
  1675. if (S_ISLNK(ip->i_d.di_mode))
  1676. error = xfs_inactive_symlink(ip);
  1677. else if (truncate)
  1678. error = xfs_inactive_truncate(ip);
  1679. if (error)
  1680. return;
  1681. /*
  1682. * If there are attributes associated with the file then blow them away
  1683. * now. The code calls a routine that recursively deconstructs the
  1684. * attribute fork. If also blows away the in-core attribute fork.
  1685. */
  1686. if (XFS_IFORK_Q(ip)) {
  1687. error = xfs_attr_inactive(ip);
  1688. if (error)
  1689. return;
  1690. }
  1691. ASSERT(!ip->i_afp);
  1692. ASSERT(ip->i_d.di_anextents == 0);
  1693. ASSERT(ip->i_d.di_forkoff == 0);
  1694. /*
  1695. * Free the inode.
  1696. */
  1697. error = xfs_inactive_ifree(ip);
  1698. if (error)
  1699. return;
  1700. /*
  1701. * Release the dquots held by inode, if any.
  1702. */
  1703. xfs_qm_dqdetach(ip);
  1704. }
  1705. /*
  1706. * This is called when the inode's link count goes to 0 or we are creating a
  1707. * tmpfile via O_TMPFILE. In the case of a tmpfile, @ignore_linkcount will be
  1708. * set to true as the link count is dropped to zero by the VFS after we've
  1709. * created the file successfully, so we have to add it to the unlinked list
  1710. * while the link count is non-zero.
  1711. *
  1712. * We place the on-disk inode on a list in the AGI. It will be pulled from this
  1713. * list when the inode is freed.
  1714. */
  1715. STATIC int
  1716. xfs_iunlink(
  1717. struct xfs_trans *tp,
  1718. struct xfs_inode *ip)
  1719. {
  1720. xfs_mount_t *mp = tp->t_mountp;
  1721. xfs_agi_t *agi;
  1722. xfs_dinode_t *dip;
  1723. xfs_buf_t *agibp;
  1724. xfs_buf_t *ibp;
  1725. xfs_agino_t agino;
  1726. short bucket_index;
  1727. int offset;
  1728. int error;
  1729. ASSERT(ip->i_d.di_mode != 0);
  1730. /*
  1731. * Get the agi buffer first. It ensures lock ordering
  1732. * on the list.
  1733. */
  1734. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1735. if (error)
  1736. return error;
  1737. agi = XFS_BUF_TO_AGI(agibp);
  1738. /*
  1739. * Get the index into the agi hash table for the
  1740. * list this inode will go on.
  1741. */
  1742. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1743. ASSERT(agino != 0);
  1744. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1745. ASSERT(agi->agi_unlinked[bucket_index]);
  1746. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1747. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1748. /*
  1749. * There is already another inode in the bucket we need
  1750. * to add ourselves to. Add us at the front of the list.
  1751. * Here we put the head pointer into our next pointer,
  1752. * and then we fall through to point the head at us.
  1753. */
  1754. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1755. 0, 0);
  1756. if (error)
  1757. return error;
  1758. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1759. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1760. offset = ip->i_imap.im_boffset +
  1761. offsetof(xfs_dinode_t, di_next_unlinked);
  1762. /* need to recalc the inode CRC if appropriate */
  1763. xfs_dinode_calc_crc(mp, dip);
  1764. xfs_trans_inode_buf(tp, ibp);
  1765. xfs_trans_log_buf(tp, ibp, offset,
  1766. (offset + sizeof(xfs_agino_t) - 1));
  1767. xfs_inobp_check(mp, ibp);
  1768. }
  1769. /*
  1770. * Point the bucket head pointer at the inode being inserted.
  1771. */
  1772. ASSERT(agino != 0);
  1773. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1774. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1775. (sizeof(xfs_agino_t) * bucket_index);
  1776. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1777. xfs_trans_log_buf(tp, agibp, offset,
  1778. (offset + sizeof(xfs_agino_t) - 1));
  1779. return 0;
  1780. }
  1781. /*
  1782. * Pull the on-disk inode from the AGI unlinked list.
  1783. */
  1784. STATIC int
  1785. xfs_iunlink_remove(
  1786. xfs_trans_t *tp,
  1787. xfs_inode_t *ip)
  1788. {
  1789. xfs_ino_t next_ino;
  1790. xfs_mount_t *mp;
  1791. xfs_agi_t *agi;
  1792. xfs_dinode_t *dip;
  1793. xfs_buf_t *agibp;
  1794. xfs_buf_t *ibp;
  1795. xfs_agnumber_t agno;
  1796. xfs_agino_t agino;
  1797. xfs_agino_t next_agino;
  1798. xfs_buf_t *last_ibp;
  1799. xfs_dinode_t *last_dip = NULL;
  1800. short bucket_index;
  1801. int offset, last_offset = 0;
  1802. int error;
  1803. mp = tp->t_mountp;
  1804. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1805. /*
  1806. * Get the agi buffer first. It ensures lock ordering
  1807. * on the list.
  1808. */
  1809. error = xfs_read_agi(mp, tp, agno, &agibp);
  1810. if (error)
  1811. return error;
  1812. agi = XFS_BUF_TO_AGI(agibp);
  1813. /*
  1814. * Get the index into the agi hash table for the
  1815. * list this inode will go on.
  1816. */
  1817. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1818. ASSERT(agino != 0);
  1819. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1820. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1821. ASSERT(agi->agi_unlinked[bucket_index]);
  1822. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1823. /*
  1824. * We're at the head of the list. Get the inode's on-disk
  1825. * buffer to see if there is anyone after us on the list.
  1826. * Only modify our next pointer if it is not already NULLAGINO.
  1827. * This saves us the overhead of dealing with the buffer when
  1828. * there is no need to change it.
  1829. */
  1830. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1831. 0, 0);
  1832. if (error) {
  1833. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1834. __func__, error);
  1835. return error;
  1836. }
  1837. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1838. ASSERT(next_agino != 0);
  1839. if (next_agino != NULLAGINO) {
  1840. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1841. offset = ip->i_imap.im_boffset +
  1842. offsetof(xfs_dinode_t, di_next_unlinked);
  1843. /* need to recalc the inode CRC if appropriate */
  1844. xfs_dinode_calc_crc(mp, dip);
  1845. xfs_trans_inode_buf(tp, ibp);
  1846. xfs_trans_log_buf(tp, ibp, offset,
  1847. (offset + sizeof(xfs_agino_t) - 1));
  1848. xfs_inobp_check(mp, ibp);
  1849. } else {
  1850. xfs_trans_brelse(tp, ibp);
  1851. }
  1852. /*
  1853. * Point the bucket head pointer at the next inode.
  1854. */
  1855. ASSERT(next_agino != 0);
  1856. ASSERT(next_agino != agino);
  1857. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1858. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1859. (sizeof(xfs_agino_t) * bucket_index);
  1860. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1861. xfs_trans_log_buf(tp, agibp, offset,
  1862. (offset + sizeof(xfs_agino_t) - 1));
  1863. } else {
  1864. /*
  1865. * We need to search the list for the inode being freed.
  1866. */
  1867. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1868. last_ibp = NULL;
  1869. while (next_agino != agino) {
  1870. struct xfs_imap imap;
  1871. if (last_ibp)
  1872. xfs_trans_brelse(tp, last_ibp);
  1873. imap.im_blkno = 0;
  1874. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1875. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1876. if (error) {
  1877. xfs_warn(mp,
  1878. "%s: xfs_imap returned error %d.",
  1879. __func__, error);
  1880. return error;
  1881. }
  1882. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1883. &last_ibp, 0, 0);
  1884. if (error) {
  1885. xfs_warn(mp,
  1886. "%s: xfs_imap_to_bp returned error %d.",
  1887. __func__, error);
  1888. return error;
  1889. }
  1890. last_offset = imap.im_boffset;
  1891. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1892. ASSERT(next_agino != NULLAGINO);
  1893. ASSERT(next_agino != 0);
  1894. }
  1895. /*
  1896. * Now last_ibp points to the buffer previous to us on the
  1897. * unlinked list. Pull us from the list.
  1898. */
  1899. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1900. 0, 0);
  1901. if (error) {
  1902. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1903. __func__, error);
  1904. return error;
  1905. }
  1906. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1907. ASSERT(next_agino != 0);
  1908. ASSERT(next_agino != agino);
  1909. if (next_agino != NULLAGINO) {
  1910. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1911. offset = ip->i_imap.im_boffset +
  1912. offsetof(xfs_dinode_t, di_next_unlinked);
  1913. /* need to recalc the inode CRC if appropriate */
  1914. xfs_dinode_calc_crc(mp, dip);
  1915. xfs_trans_inode_buf(tp, ibp);
  1916. xfs_trans_log_buf(tp, ibp, offset,
  1917. (offset + sizeof(xfs_agino_t) - 1));
  1918. xfs_inobp_check(mp, ibp);
  1919. } else {
  1920. xfs_trans_brelse(tp, ibp);
  1921. }
  1922. /*
  1923. * Point the previous inode on the list to the next inode.
  1924. */
  1925. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1926. ASSERT(next_agino != 0);
  1927. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1928. /* need to recalc the inode CRC if appropriate */
  1929. xfs_dinode_calc_crc(mp, last_dip);
  1930. xfs_trans_inode_buf(tp, last_ibp);
  1931. xfs_trans_log_buf(tp, last_ibp, offset,
  1932. (offset + sizeof(xfs_agino_t) - 1));
  1933. xfs_inobp_check(mp, last_ibp);
  1934. }
  1935. return 0;
  1936. }
  1937. /*
  1938. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1939. * inodes that are in memory - they all must be marked stale and attached to
  1940. * the cluster buffer.
  1941. */
  1942. STATIC int
  1943. xfs_ifree_cluster(
  1944. xfs_inode_t *free_ip,
  1945. xfs_trans_t *tp,
  1946. struct xfs_icluster *xic)
  1947. {
  1948. xfs_mount_t *mp = free_ip->i_mount;
  1949. int blks_per_cluster;
  1950. int inodes_per_cluster;
  1951. int nbufs;
  1952. int i, j;
  1953. int ioffset;
  1954. xfs_daddr_t blkno;
  1955. xfs_buf_t *bp;
  1956. xfs_inode_t *ip;
  1957. xfs_inode_log_item_t *iip;
  1958. xfs_log_item_t *lip;
  1959. struct xfs_perag *pag;
  1960. xfs_ino_t inum;
  1961. inum = xic->first_ino;
  1962. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1963. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1964. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  1965. nbufs = mp->m_ialloc_blks / blks_per_cluster;
  1966. for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
  1967. /*
  1968. * The allocation bitmap tells us which inodes of the chunk were
  1969. * physically allocated. Skip the cluster if an inode falls into
  1970. * a sparse region.
  1971. */
  1972. ioffset = inum - xic->first_ino;
  1973. if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
  1974. ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
  1975. continue;
  1976. }
  1977. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1978. XFS_INO_TO_AGBNO(mp, inum));
  1979. /*
  1980. * We obtain and lock the backing buffer first in the process
  1981. * here, as we have to ensure that any dirty inode that we
  1982. * can't get the flush lock on is attached to the buffer.
  1983. * If we scan the in-memory inodes first, then buffer IO can
  1984. * complete before we get a lock on it, and hence we may fail
  1985. * to mark all the active inodes on the buffer stale.
  1986. */
  1987. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1988. mp->m_bsize * blks_per_cluster,
  1989. XBF_UNMAPPED);
  1990. if (!bp)
  1991. return -ENOMEM;
  1992. /*
  1993. * This buffer may not have been correctly initialised as we
  1994. * didn't read it from disk. That's not important because we are
  1995. * only using to mark the buffer as stale in the log, and to
  1996. * attach stale cached inodes on it. That means it will never be
  1997. * dispatched for IO. If it is, we want to know about it, and we
  1998. * want it to fail. We can acheive this by adding a write
  1999. * verifier to the buffer.
  2000. */
  2001. bp->b_ops = &xfs_inode_buf_ops;
  2002. /*
  2003. * Walk the inodes already attached to the buffer and mark them
  2004. * stale. These will all have the flush locks held, so an
  2005. * in-memory inode walk can't lock them. By marking them all
  2006. * stale first, we will not attempt to lock them in the loop
  2007. * below as the XFS_ISTALE flag will be set.
  2008. */
  2009. lip = bp->b_fspriv;
  2010. while (lip) {
  2011. if (lip->li_type == XFS_LI_INODE) {
  2012. iip = (xfs_inode_log_item_t *)lip;
  2013. ASSERT(iip->ili_logged == 1);
  2014. lip->li_cb = xfs_istale_done;
  2015. xfs_trans_ail_copy_lsn(mp->m_ail,
  2016. &iip->ili_flush_lsn,
  2017. &iip->ili_item.li_lsn);
  2018. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  2019. }
  2020. lip = lip->li_bio_list;
  2021. }
  2022. /*
  2023. * For each inode in memory attempt to add it to the inode
  2024. * buffer and set it up for being staled on buffer IO
  2025. * completion. This is safe as we've locked out tail pushing
  2026. * and flushing by locking the buffer.
  2027. *
  2028. * We have already marked every inode that was part of a
  2029. * transaction stale above, which means there is no point in
  2030. * even trying to lock them.
  2031. */
  2032. for (i = 0; i < inodes_per_cluster; i++) {
  2033. retry:
  2034. rcu_read_lock();
  2035. ip = radix_tree_lookup(&pag->pag_ici_root,
  2036. XFS_INO_TO_AGINO(mp, (inum + i)));
  2037. /* Inode not in memory, nothing to do */
  2038. if (!ip) {
  2039. rcu_read_unlock();
  2040. continue;
  2041. }
  2042. /*
  2043. * because this is an RCU protected lookup, we could
  2044. * find a recently freed or even reallocated inode
  2045. * during the lookup. We need to check under the
  2046. * i_flags_lock for a valid inode here. Skip it if it
  2047. * is not valid, the wrong inode or stale.
  2048. */
  2049. spin_lock(&ip->i_flags_lock);
  2050. if (ip->i_ino != inum + i ||
  2051. __xfs_iflags_test(ip, XFS_ISTALE)) {
  2052. spin_unlock(&ip->i_flags_lock);
  2053. rcu_read_unlock();
  2054. continue;
  2055. }
  2056. spin_unlock(&ip->i_flags_lock);
  2057. /*
  2058. * Don't try to lock/unlock the current inode, but we
  2059. * _cannot_ skip the other inodes that we did not find
  2060. * in the list attached to the buffer and are not
  2061. * already marked stale. If we can't lock it, back off
  2062. * and retry.
  2063. */
  2064. if (ip != free_ip &&
  2065. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2066. rcu_read_unlock();
  2067. delay(1);
  2068. goto retry;
  2069. }
  2070. rcu_read_unlock();
  2071. xfs_iflock(ip);
  2072. xfs_iflags_set(ip, XFS_ISTALE);
  2073. /*
  2074. * we don't need to attach clean inodes or those only
  2075. * with unlogged changes (which we throw away, anyway).
  2076. */
  2077. iip = ip->i_itemp;
  2078. if (!iip || xfs_inode_clean(ip)) {
  2079. ASSERT(ip != free_ip);
  2080. xfs_ifunlock(ip);
  2081. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2082. continue;
  2083. }
  2084. iip->ili_last_fields = iip->ili_fields;
  2085. iip->ili_fields = 0;
  2086. iip->ili_fsync_fields = 0;
  2087. iip->ili_logged = 1;
  2088. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2089. &iip->ili_item.li_lsn);
  2090. xfs_buf_attach_iodone(bp, xfs_istale_done,
  2091. &iip->ili_item);
  2092. if (ip != free_ip)
  2093. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2094. }
  2095. xfs_trans_stale_inode_buf(tp, bp);
  2096. xfs_trans_binval(tp, bp);
  2097. }
  2098. xfs_perag_put(pag);
  2099. return 0;
  2100. }
  2101. /*
  2102. * This is called to return an inode to the inode free list.
  2103. * The inode should already be truncated to 0 length and have
  2104. * no pages associated with it. This routine also assumes that
  2105. * the inode is already a part of the transaction.
  2106. *
  2107. * The on-disk copy of the inode will have been added to the list
  2108. * of unlinked inodes in the AGI. We need to remove the inode from
  2109. * that list atomically with respect to freeing it here.
  2110. */
  2111. int
  2112. xfs_ifree(
  2113. xfs_trans_t *tp,
  2114. xfs_inode_t *ip,
  2115. xfs_bmap_free_t *flist)
  2116. {
  2117. int error;
  2118. struct xfs_icluster xic = { 0 };
  2119. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2120. ASSERT(VFS_I(ip)->i_nlink == 0);
  2121. ASSERT(ip->i_d.di_nextents == 0);
  2122. ASSERT(ip->i_d.di_anextents == 0);
  2123. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  2124. ASSERT(ip->i_d.di_nblocks == 0);
  2125. /*
  2126. * Pull the on-disk inode from the AGI unlinked list.
  2127. */
  2128. error = xfs_iunlink_remove(tp, ip);
  2129. if (error)
  2130. return error;
  2131. error = xfs_difree(tp, ip->i_ino, flist, &xic);
  2132. if (error)
  2133. return error;
  2134. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2135. ip->i_d.di_flags = 0;
  2136. ip->i_d.di_dmevmask = 0;
  2137. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2138. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2139. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2140. /*
  2141. * Bump the generation count so no one will be confused
  2142. * by reincarnations of this inode.
  2143. */
  2144. VFS_I(ip)->i_generation++;
  2145. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2146. if (xic.deleted)
  2147. error = xfs_ifree_cluster(ip, tp, &xic);
  2148. return error;
  2149. }
  2150. /*
  2151. * This is called to unpin an inode. The caller must have the inode locked
  2152. * in at least shared mode so that the buffer cannot be subsequently pinned
  2153. * once someone is waiting for it to be unpinned.
  2154. */
  2155. static void
  2156. xfs_iunpin(
  2157. struct xfs_inode *ip)
  2158. {
  2159. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2160. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2161. /* Give the log a push to start the unpinning I/O */
  2162. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2163. }
  2164. static void
  2165. __xfs_iunpin_wait(
  2166. struct xfs_inode *ip)
  2167. {
  2168. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2169. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2170. xfs_iunpin(ip);
  2171. do {
  2172. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2173. if (xfs_ipincount(ip))
  2174. io_schedule();
  2175. } while (xfs_ipincount(ip));
  2176. finish_wait(wq, &wait.wait);
  2177. }
  2178. void
  2179. xfs_iunpin_wait(
  2180. struct xfs_inode *ip)
  2181. {
  2182. if (xfs_ipincount(ip))
  2183. __xfs_iunpin_wait(ip);
  2184. }
  2185. /*
  2186. * Removing an inode from the namespace involves removing the directory entry
  2187. * and dropping the link count on the inode. Removing the directory entry can
  2188. * result in locking an AGF (directory blocks were freed) and removing a link
  2189. * count can result in placing the inode on an unlinked list which results in
  2190. * locking an AGI.
  2191. *
  2192. * The big problem here is that we have an ordering constraint on AGF and AGI
  2193. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2194. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2195. * removes the inode from the unlinked list, requiring that we lock the AGI
  2196. * first, and then freeing the inode can result in an inode chunk being freed
  2197. * and hence freeing disk space requiring that we lock an AGF.
  2198. *
  2199. * Hence the ordering that is imposed by other parts of the code is AGI before
  2200. * AGF. This means we cannot remove the directory entry before we drop the inode
  2201. * reference count and put it on the unlinked list as this results in a lock
  2202. * order of AGF then AGI, and this can deadlock against inode allocation and
  2203. * freeing. Therefore we must drop the link counts before we remove the
  2204. * directory entry.
  2205. *
  2206. * This is still safe from a transactional point of view - it is not until we
  2207. * get to xfs_bmap_finish() that we have the possibility of multiple
  2208. * transactions in this operation. Hence as long as we remove the directory
  2209. * entry and drop the link count in the first transaction of the remove
  2210. * operation, there are no transactional constraints on the ordering here.
  2211. */
  2212. int
  2213. xfs_remove(
  2214. xfs_inode_t *dp,
  2215. struct xfs_name *name,
  2216. xfs_inode_t *ip)
  2217. {
  2218. xfs_mount_t *mp = dp->i_mount;
  2219. xfs_trans_t *tp = NULL;
  2220. int is_dir = S_ISDIR(ip->i_d.di_mode);
  2221. int error = 0;
  2222. xfs_bmap_free_t free_list;
  2223. xfs_fsblock_t first_block;
  2224. uint resblks;
  2225. trace_xfs_remove(dp, name);
  2226. if (XFS_FORCED_SHUTDOWN(mp))
  2227. return -EIO;
  2228. error = xfs_qm_dqattach(dp, 0);
  2229. if (error)
  2230. goto std_return;
  2231. error = xfs_qm_dqattach(ip, 0);
  2232. if (error)
  2233. goto std_return;
  2234. if (is_dir)
  2235. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2236. else
  2237. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2238. /*
  2239. * We try to get the real space reservation first,
  2240. * allowing for directory btree deletion(s) implying
  2241. * possible bmap insert(s). If we can't get the space
  2242. * reservation then we use 0 instead, and avoid the bmap
  2243. * btree insert(s) in the directory code by, if the bmap
  2244. * insert tries to happen, instead trimming the LAST
  2245. * block from the directory.
  2246. */
  2247. resblks = XFS_REMOVE_SPACE_RES(mp);
  2248. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
  2249. if (error == -ENOSPC) {
  2250. resblks = 0;
  2251. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
  2252. }
  2253. if (error) {
  2254. ASSERT(error != -ENOSPC);
  2255. goto out_trans_cancel;
  2256. }
  2257. xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
  2258. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  2259. xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  2260. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2261. /*
  2262. * If we're removing a directory perform some additional validation.
  2263. */
  2264. if (is_dir) {
  2265. ASSERT(VFS_I(ip)->i_nlink >= 2);
  2266. if (VFS_I(ip)->i_nlink != 2) {
  2267. error = -ENOTEMPTY;
  2268. goto out_trans_cancel;
  2269. }
  2270. if (!xfs_dir_isempty(ip)) {
  2271. error = -ENOTEMPTY;
  2272. goto out_trans_cancel;
  2273. }
  2274. /* Drop the link from ip's "..". */
  2275. error = xfs_droplink(tp, dp);
  2276. if (error)
  2277. goto out_trans_cancel;
  2278. /* Drop the "." link from ip to self. */
  2279. error = xfs_droplink(tp, ip);
  2280. if (error)
  2281. goto out_trans_cancel;
  2282. } else {
  2283. /*
  2284. * When removing a non-directory we need to log the parent
  2285. * inode here. For a directory this is done implicitly
  2286. * by the xfs_droplink call for the ".." entry.
  2287. */
  2288. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2289. }
  2290. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2291. /* Drop the link from dp to ip. */
  2292. error = xfs_droplink(tp, ip);
  2293. if (error)
  2294. goto out_trans_cancel;
  2295. xfs_bmap_init(&free_list, &first_block);
  2296. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2297. &first_block, &free_list, resblks);
  2298. if (error) {
  2299. ASSERT(error != -ENOENT);
  2300. goto out_bmap_cancel;
  2301. }
  2302. /*
  2303. * If this is a synchronous mount, make sure that the
  2304. * remove transaction goes to disk before returning to
  2305. * the user.
  2306. */
  2307. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2308. xfs_trans_set_sync(tp);
  2309. error = xfs_bmap_finish(&tp, &free_list, NULL);
  2310. if (error)
  2311. goto out_bmap_cancel;
  2312. error = xfs_trans_commit(tp);
  2313. if (error)
  2314. goto std_return;
  2315. if (is_dir && xfs_inode_is_filestream(ip))
  2316. xfs_filestream_deassociate(ip);
  2317. return 0;
  2318. out_bmap_cancel:
  2319. xfs_bmap_cancel(&free_list);
  2320. out_trans_cancel:
  2321. xfs_trans_cancel(tp);
  2322. std_return:
  2323. return error;
  2324. }
  2325. /*
  2326. * Enter all inodes for a rename transaction into a sorted array.
  2327. */
  2328. #define __XFS_SORT_INODES 5
  2329. STATIC void
  2330. xfs_sort_for_rename(
  2331. struct xfs_inode *dp1, /* in: old (source) directory inode */
  2332. struct xfs_inode *dp2, /* in: new (target) directory inode */
  2333. struct xfs_inode *ip1, /* in: inode of old entry */
  2334. struct xfs_inode *ip2, /* in: inode of new entry */
  2335. struct xfs_inode *wip, /* in: whiteout inode */
  2336. struct xfs_inode **i_tab,/* out: sorted array of inodes */
  2337. int *num_inodes) /* in/out: inodes in array */
  2338. {
  2339. int i, j;
  2340. ASSERT(*num_inodes == __XFS_SORT_INODES);
  2341. memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
  2342. /*
  2343. * i_tab contains a list of pointers to inodes. We initialize
  2344. * the table here & we'll sort it. We will then use it to
  2345. * order the acquisition of the inode locks.
  2346. *
  2347. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2348. */
  2349. i = 0;
  2350. i_tab[i++] = dp1;
  2351. i_tab[i++] = dp2;
  2352. i_tab[i++] = ip1;
  2353. if (ip2)
  2354. i_tab[i++] = ip2;
  2355. if (wip)
  2356. i_tab[i++] = wip;
  2357. *num_inodes = i;
  2358. /*
  2359. * Sort the elements via bubble sort. (Remember, there are at
  2360. * most 5 elements to sort, so this is adequate.)
  2361. */
  2362. for (i = 0; i < *num_inodes; i++) {
  2363. for (j = 1; j < *num_inodes; j++) {
  2364. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2365. struct xfs_inode *temp = i_tab[j];
  2366. i_tab[j] = i_tab[j-1];
  2367. i_tab[j-1] = temp;
  2368. }
  2369. }
  2370. }
  2371. }
  2372. static int
  2373. xfs_finish_rename(
  2374. struct xfs_trans *tp,
  2375. struct xfs_bmap_free *free_list)
  2376. {
  2377. int error;
  2378. /*
  2379. * If this is a synchronous mount, make sure that the rename transaction
  2380. * goes to disk before returning to the user.
  2381. */
  2382. if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2383. xfs_trans_set_sync(tp);
  2384. error = xfs_bmap_finish(&tp, free_list, NULL);
  2385. if (error) {
  2386. xfs_bmap_cancel(free_list);
  2387. xfs_trans_cancel(tp);
  2388. return error;
  2389. }
  2390. return xfs_trans_commit(tp);
  2391. }
  2392. /*
  2393. * xfs_cross_rename()
  2394. *
  2395. * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
  2396. */
  2397. STATIC int
  2398. xfs_cross_rename(
  2399. struct xfs_trans *tp,
  2400. struct xfs_inode *dp1,
  2401. struct xfs_name *name1,
  2402. struct xfs_inode *ip1,
  2403. struct xfs_inode *dp2,
  2404. struct xfs_name *name2,
  2405. struct xfs_inode *ip2,
  2406. struct xfs_bmap_free *free_list,
  2407. xfs_fsblock_t *first_block,
  2408. int spaceres)
  2409. {
  2410. int error = 0;
  2411. int ip1_flags = 0;
  2412. int ip2_flags = 0;
  2413. int dp2_flags = 0;
  2414. /* Swap inode number for dirent in first parent */
  2415. error = xfs_dir_replace(tp, dp1, name1,
  2416. ip2->i_ino,
  2417. first_block, free_list, spaceres);
  2418. if (error)
  2419. goto out_trans_abort;
  2420. /* Swap inode number for dirent in second parent */
  2421. error = xfs_dir_replace(tp, dp2, name2,
  2422. ip1->i_ino,
  2423. first_block, free_list, spaceres);
  2424. if (error)
  2425. goto out_trans_abort;
  2426. /*
  2427. * If we're renaming one or more directories across different parents,
  2428. * update the respective ".." entries (and link counts) to match the new
  2429. * parents.
  2430. */
  2431. if (dp1 != dp2) {
  2432. dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2433. if (S_ISDIR(ip2->i_d.di_mode)) {
  2434. error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
  2435. dp1->i_ino, first_block,
  2436. free_list, spaceres);
  2437. if (error)
  2438. goto out_trans_abort;
  2439. /* transfer ip2 ".." reference to dp1 */
  2440. if (!S_ISDIR(ip1->i_d.di_mode)) {
  2441. error = xfs_droplink(tp, dp2);
  2442. if (error)
  2443. goto out_trans_abort;
  2444. error = xfs_bumplink(tp, dp1);
  2445. if (error)
  2446. goto out_trans_abort;
  2447. }
  2448. /*
  2449. * Although ip1 isn't changed here, userspace needs
  2450. * to be warned about the change, so that applications
  2451. * relying on it (like backup ones), will properly
  2452. * notify the change
  2453. */
  2454. ip1_flags |= XFS_ICHGTIME_CHG;
  2455. ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2456. }
  2457. if (S_ISDIR(ip1->i_d.di_mode)) {
  2458. error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
  2459. dp2->i_ino, first_block,
  2460. free_list, spaceres);
  2461. if (error)
  2462. goto out_trans_abort;
  2463. /* transfer ip1 ".." reference to dp2 */
  2464. if (!S_ISDIR(ip2->i_d.di_mode)) {
  2465. error = xfs_droplink(tp, dp1);
  2466. if (error)
  2467. goto out_trans_abort;
  2468. error = xfs_bumplink(tp, dp2);
  2469. if (error)
  2470. goto out_trans_abort;
  2471. }
  2472. /*
  2473. * Although ip2 isn't changed here, userspace needs
  2474. * to be warned about the change, so that applications
  2475. * relying on it (like backup ones), will properly
  2476. * notify the change
  2477. */
  2478. ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2479. ip2_flags |= XFS_ICHGTIME_CHG;
  2480. }
  2481. }
  2482. if (ip1_flags) {
  2483. xfs_trans_ichgtime(tp, ip1, ip1_flags);
  2484. xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
  2485. }
  2486. if (ip2_flags) {
  2487. xfs_trans_ichgtime(tp, ip2, ip2_flags);
  2488. xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
  2489. }
  2490. if (dp2_flags) {
  2491. xfs_trans_ichgtime(tp, dp2, dp2_flags);
  2492. xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
  2493. }
  2494. xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2495. xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
  2496. return xfs_finish_rename(tp, free_list);
  2497. out_trans_abort:
  2498. xfs_bmap_cancel(free_list);
  2499. xfs_trans_cancel(tp);
  2500. return error;
  2501. }
  2502. /*
  2503. * xfs_rename_alloc_whiteout()
  2504. *
  2505. * Return a referenced, unlinked, unlocked inode that that can be used as a
  2506. * whiteout in a rename transaction. We use a tmpfile inode here so that if we
  2507. * crash between allocating the inode and linking it into the rename transaction
  2508. * recovery will free the inode and we won't leak it.
  2509. */
  2510. static int
  2511. xfs_rename_alloc_whiteout(
  2512. struct xfs_inode *dp,
  2513. struct xfs_inode **wip)
  2514. {
  2515. struct xfs_inode *tmpfile;
  2516. int error;
  2517. error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
  2518. if (error)
  2519. return error;
  2520. /*
  2521. * Prepare the tmpfile inode as if it were created through the VFS.
  2522. * Otherwise, the link increment paths will complain about nlink 0->1.
  2523. * Drop the link count as done by d_tmpfile(), complete the inode setup
  2524. * and flag it as linkable.
  2525. */
  2526. drop_nlink(VFS_I(tmpfile));
  2527. xfs_finish_inode_setup(tmpfile);
  2528. VFS_I(tmpfile)->i_state |= I_LINKABLE;
  2529. *wip = tmpfile;
  2530. return 0;
  2531. }
  2532. /*
  2533. * xfs_rename
  2534. */
  2535. int
  2536. xfs_rename(
  2537. struct xfs_inode *src_dp,
  2538. struct xfs_name *src_name,
  2539. struct xfs_inode *src_ip,
  2540. struct xfs_inode *target_dp,
  2541. struct xfs_name *target_name,
  2542. struct xfs_inode *target_ip,
  2543. unsigned int flags)
  2544. {
  2545. struct xfs_mount *mp = src_dp->i_mount;
  2546. struct xfs_trans *tp;
  2547. struct xfs_bmap_free free_list;
  2548. xfs_fsblock_t first_block;
  2549. struct xfs_inode *wip = NULL; /* whiteout inode */
  2550. struct xfs_inode *inodes[__XFS_SORT_INODES];
  2551. int num_inodes = __XFS_SORT_INODES;
  2552. bool new_parent = (src_dp != target_dp);
  2553. bool src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
  2554. int spaceres;
  2555. int error;
  2556. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2557. if ((flags & RENAME_EXCHANGE) && !target_ip)
  2558. return -EINVAL;
  2559. /*
  2560. * If we are doing a whiteout operation, allocate the whiteout inode
  2561. * we will be placing at the target and ensure the type is set
  2562. * appropriately.
  2563. */
  2564. if (flags & RENAME_WHITEOUT) {
  2565. ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
  2566. error = xfs_rename_alloc_whiteout(target_dp, &wip);
  2567. if (error)
  2568. return error;
  2569. /* setup target dirent info as whiteout */
  2570. src_name->type = XFS_DIR3_FT_CHRDEV;
  2571. }
  2572. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
  2573. inodes, &num_inodes);
  2574. tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
  2575. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2576. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
  2577. if (error == -ENOSPC) {
  2578. spaceres = 0;
  2579. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
  2580. }
  2581. if (error)
  2582. goto out_trans_cancel;
  2583. /*
  2584. * Attach the dquots to the inodes
  2585. */
  2586. error = xfs_qm_vop_rename_dqattach(inodes);
  2587. if (error)
  2588. goto out_trans_cancel;
  2589. /*
  2590. * Lock all the participating inodes. Depending upon whether
  2591. * the target_name exists in the target directory, and
  2592. * whether the target directory is the same as the source
  2593. * directory, we can lock from 2 to 4 inodes.
  2594. */
  2595. if (!new_parent)
  2596. xfs_ilock(src_dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
  2597. else
  2598. xfs_lock_two_inodes(src_dp, target_dp,
  2599. XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
  2600. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2601. /*
  2602. * Join all the inodes to the transaction. From this point on,
  2603. * we can rely on either trans_commit or trans_cancel to unlock
  2604. * them.
  2605. */
  2606. xfs_trans_ijoin(tp, src_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  2607. if (new_parent)
  2608. xfs_trans_ijoin(tp, target_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  2609. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2610. if (target_ip)
  2611. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2612. if (wip)
  2613. xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
  2614. /*
  2615. * If we are using project inheritance, we only allow renames
  2616. * into our tree when the project IDs are the same; else the
  2617. * tree quota mechanism would be circumvented.
  2618. */
  2619. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2620. (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
  2621. error = -EXDEV;
  2622. goto out_trans_cancel;
  2623. }
  2624. xfs_bmap_init(&free_list, &first_block);
  2625. /* RENAME_EXCHANGE is unique from here on. */
  2626. if (flags & RENAME_EXCHANGE)
  2627. return xfs_cross_rename(tp, src_dp, src_name, src_ip,
  2628. target_dp, target_name, target_ip,
  2629. &free_list, &first_block, spaceres);
  2630. /*
  2631. * Set up the target.
  2632. */
  2633. if (target_ip == NULL) {
  2634. /*
  2635. * If there's no space reservation, check the entry will
  2636. * fit before actually inserting it.
  2637. */
  2638. if (!spaceres) {
  2639. error = xfs_dir_canenter(tp, target_dp, target_name);
  2640. if (error)
  2641. goto out_trans_cancel;
  2642. }
  2643. /*
  2644. * If target does not exist and the rename crosses
  2645. * directories, adjust the target directory link count
  2646. * to account for the ".." reference from the new entry.
  2647. */
  2648. error = xfs_dir_createname(tp, target_dp, target_name,
  2649. src_ip->i_ino, &first_block,
  2650. &free_list, spaceres);
  2651. if (error)
  2652. goto out_bmap_cancel;
  2653. xfs_trans_ichgtime(tp, target_dp,
  2654. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2655. if (new_parent && src_is_directory) {
  2656. error = xfs_bumplink(tp, target_dp);
  2657. if (error)
  2658. goto out_bmap_cancel;
  2659. }
  2660. } else { /* target_ip != NULL */
  2661. /*
  2662. * If target exists and it's a directory, check that both
  2663. * target and source are directories and that target can be
  2664. * destroyed, or that neither is a directory.
  2665. */
  2666. if (S_ISDIR(target_ip->i_d.di_mode)) {
  2667. /*
  2668. * Make sure target dir is empty.
  2669. */
  2670. if (!(xfs_dir_isempty(target_ip)) ||
  2671. (VFS_I(target_ip)->i_nlink > 2)) {
  2672. error = -EEXIST;
  2673. goto out_trans_cancel;
  2674. }
  2675. }
  2676. /*
  2677. * Link the source inode under the target name.
  2678. * If the source inode is a directory and we are moving
  2679. * it across directories, its ".." entry will be
  2680. * inconsistent until we replace that down below.
  2681. *
  2682. * In case there is already an entry with the same
  2683. * name at the destination directory, remove it first.
  2684. */
  2685. error = xfs_dir_replace(tp, target_dp, target_name,
  2686. src_ip->i_ino,
  2687. &first_block, &free_list, spaceres);
  2688. if (error)
  2689. goto out_bmap_cancel;
  2690. xfs_trans_ichgtime(tp, target_dp,
  2691. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2692. /*
  2693. * Decrement the link count on the target since the target
  2694. * dir no longer points to it.
  2695. */
  2696. error = xfs_droplink(tp, target_ip);
  2697. if (error)
  2698. goto out_bmap_cancel;
  2699. if (src_is_directory) {
  2700. /*
  2701. * Drop the link from the old "." entry.
  2702. */
  2703. error = xfs_droplink(tp, target_ip);
  2704. if (error)
  2705. goto out_bmap_cancel;
  2706. }
  2707. } /* target_ip != NULL */
  2708. /*
  2709. * Remove the source.
  2710. */
  2711. if (new_parent && src_is_directory) {
  2712. /*
  2713. * Rewrite the ".." entry to point to the new
  2714. * directory.
  2715. */
  2716. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2717. target_dp->i_ino,
  2718. &first_block, &free_list, spaceres);
  2719. ASSERT(error != -EEXIST);
  2720. if (error)
  2721. goto out_bmap_cancel;
  2722. }
  2723. /*
  2724. * We always want to hit the ctime on the source inode.
  2725. *
  2726. * This isn't strictly required by the standards since the source
  2727. * inode isn't really being changed, but old unix file systems did
  2728. * it and some incremental backup programs won't work without it.
  2729. */
  2730. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2731. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2732. /*
  2733. * Adjust the link count on src_dp. This is necessary when
  2734. * renaming a directory, either within one parent when
  2735. * the target existed, or across two parent directories.
  2736. */
  2737. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2738. /*
  2739. * Decrement link count on src_directory since the
  2740. * entry that's moved no longer points to it.
  2741. */
  2742. error = xfs_droplink(tp, src_dp);
  2743. if (error)
  2744. goto out_bmap_cancel;
  2745. }
  2746. /*
  2747. * For whiteouts, we only need to update the source dirent with the
  2748. * inode number of the whiteout inode rather than removing it
  2749. * altogether.
  2750. */
  2751. if (wip) {
  2752. error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
  2753. &first_block, &free_list, spaceres);
  2754. } else
  2755. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  2756. &first_block, &free_list, spaceres);
  2757. if (error)
  2758. goto out_bmap_cancel;
  2759. /*
  2760. * For whiteouts, we need to bump the link count on the whiteout inode.
  2761. * This means that failures all the way up to this point leave the inode
  2762. * on the unlinked list and so cleanup is a simple matter of dropping
  2763. * the remaining reference to it. If we fail here after bumping the link
  2764. * count, we're shutting down the filesystem so we'll never see the
  2765. * intermediate state on disk.
  2766. */
  2767. if (wip) {
  2768. ASSERT(VFS_I(wip)->i_nlink == 0);
  2769. error = xfs_bumplink(tp, wip);
  2770. if (error)
  2771. goto out_bmap_cancel;
  2772. error = xfs_iunlink_remove(tp, wip);
  2773. if (error)
  2774. goto out_bmap_cancel;
  2775. xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
  2776. /*
  2777. * Now we have a real link, clear the "I'm a tmpfile" state
  2778. * flag from the inode so it doesn't accidentally get misused in
  2779. * future.
  2780. */
  2781. VFS_I(wip)->i_state &= ~I_LINKABLE;
  2782. }
  2783. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2784. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  2785. if (new_parent)
  2786. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  2787. error = xfs_finish_rename(tp, &free_list);
  2788. if (wip)
  2789. IRELE(wip);
  2790. return error;
  2791. out_bmap_cancel:
  2792. xfs_bmap_cancel(&free_list);
  2793. out_trans_cancel:
  2794. xfs_trans_cancel(tp);
  2795. if (wip)
  2796. IRELE(wip);
  2797. return error;
  2798. }
  2799. STATIC int
  2800. xfs_iflush_cluster(
  2801. xfs_inode_t *ip,
  2802. xfs_buf_t *bp)
  2803. {
  2804. xfs_mount_t *mp = ip->i_mount;
  2805. struct xfs_perag *pag;
  2806. unsigned long first_index, mask;
  2807. unsigned long inodes_per_cluster;
  2808. int ilist_size;
  2809. xfs_inode_t **ilist;
  2810. xfs_inode_t *iq;
  2811. int nr_found;
  2812. int clcount = 0;
  2813. int bufwasdelwri;
  2814. int i;
  2815. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2816. inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  2817. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2818. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2819. if (!ilist)
  2820. goto out_put;
  2821. mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
  2822. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2823. rcu_read_lock();
  2824. /* really need a gang lookup range call here */
  2825. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2826. first_index, inodes_per_cluster);
  2827. if (nr_found == 0)
  2828. goto out_free;
  2829. for (i = 0; i < nr_found; i++) {
  2830. iq = ilist[i];
  2831. if (iq == ip)
  2832. continue;
  2833. /*
  2834. * because this is an RCU protected lookup, we could find a
  2835. * recently freed or even reallocated inode during the lookup.
  2836. * We need to check under the i_flags_lock for a valid inode
  2837. * here. Skip it if it is not valid or the wrong inode.
  2838. */
  2839. spin_lock(&ip->i_flags_lock);
  2840. if (!ip->i_ino ||
  2841. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2842. spin_unlock(&ip->i_flags_lock);
  2843. continue;
  2844. }
  2845. spin_unlock(&ip->i_flags_lock);
  2846. /*
  2847. * Do an un-protected check to see if the inode is dirty and
  2848. * is a candidate for flushing. These checks will be repeated
  2849. * later after the appropriate locks are acquired.
  2850. */
  2851. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2852. continue;
  2853. /*
  2854. * Try to get locks. If any are unavailable or it is pinned,
  2855. * then this inode cannot be flushed and is skipped.
  2856. */
  2857. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2858. continue;
  2859. if (!xfs_iflock_nowait(iq)) {
  2860. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2861. continue;
  2862. }
  2863. if (xfs_ipincount(iq)) {
  2864. xfs_ifunlock(iq);
  2865. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2866. continue;
  2867. }
  2868. /*
  2869. * arriving here means that this inode can be flushed. First
  2870. * re-check that it's dirty before flushing.
  2871. */
  2872. if (!xfs_inode_clean(iq)) {
  2873. int error;
  2874. error = xfs_iflush_int(iq, bp);
  2875. if (error) {
  2876. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2877. goto cluster_corrupt_out;
  2878. }
  2879. clcount++;
  2880. } else {
  2881. xfs_ifunlock(iq);
  2882. }
  2883. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2884. }
  2885. if (clcount) {
  2886. XFS_STATS_INC(mp, xs_icluster_flushcnt);
  2887. XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
  2888. }
  2889. out_free:
  2890. rcu_read_unlock();
  2891. kmem_free(ilist);
  2892. out_put:
  2893. xfs_perag_put(pag);
  2894. return 0;
  2895. cluster_corrupt_out:
  2896. /*
  2897. * Corruption detected in the clustering loop. Invalidate the
  2898. * inode buffer and shut down the filesystem.
  2899. */
  2900. rcu_read_unlock();
  2901. /*
  2902. * Clean up the buffer. If it was delwri, just release it --
  2903. * brelse can handle it with no problems. If not, shut down the
  2904. * filesystem before releasing the buffer.
  2905. */
  2906. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2907. if (bufwasdelwri)
  2908. xfs_buf_relse(bp);
  2909. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2910. if (!bufwasdelwri) {
  2911. /*
  2912. * Just like incore_relse: if we have b_iodone functions,
  2913. * mark the buffer as an error and call them. Otherwise
  2914. * mark it as stale and brelse.
  2915. */
  2916. if (bp->b_iodone) {
  2917. XFS_BUF_UNDONE(bp);
  2918. xfs_buf_stale(bp);
  2919. xfs_buf_ioerror(bp, -EIO);
  2920. xfs_buf_ioend(bp);
  2921. } else {
  2922. xfs_buf_stale(bp);
  2923. xfs_buf_relse(bp);
  2924. }
  2925. }
  2926. /*
  2927. * Unlocks the flush lock
  2928. */
  2929. xfs_iflush_abort(iq, false);
  2930. kmem_free(ilist);
  2931. xfs_perag_put(pag);
  2932. return -EFSCORRUPTED;
  2933. }
  2934. /*
  2935. * Flush dirty inode metadata into the backing buffer.
  2936. *
  2937. * The caller must have the inode lock and the inode flush lock held. The
  2938. * inode lock will still be held upon return to the caller, and the inode
  2939. * flush lock will be released after the inode has reached the disk.
  2940. *
  2941. * The caller must write out the buffer returned in *bpp and release it.
  2942. */
  2943. int
  2944. xfs_iflush(
  2945. struct xfs_inode *ip,
  2946. struct xfs_buf **bpp)
  2947. {
  2948. struct xfs_mount *mp = ip->i_mount;
  2949. struct xfs_buf *bp;
  2950. struct xfs_dinode *dip;
  2951. int error;
  2952. XFS_STATS_INC(mp, xs_iflush_count);
  2953. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2954. ASSERT(xfs_isiflocked(ip));
  2955. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2956. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2957. *bpp = NULL;
  2958. xfs_iunpin_wait(ip);
  2959. /*
  2960. * For stale inodes we cannot rely on the backing buffer remaining
  2961. * stale in cache for the remaining life of the stale inode and so
  2962. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2963. * inodes below. We have to check this after ensuring the inode is
  2964. * unpinned so that it is safe to reclaim the stale inode after the
  2965. * flush call.
  2966. */
  2967. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2968. xfs_ifunlock(ip);
  2969. return 0;
  2970. }
  2971. /*
  2972. * This may have been unpinned because the filesystem is shutting
  2973. * down forcibly. If that's the case we must not write this inode
  2974. * to disk, because the log record didn't make it to disk.
  2975. *
  2976. * We also have to remove the log item from the AIL in this case,
  2977. * as we wait for an empty AIL as part of the unmount process.
  2978. */
  2979. if (XFS_FORCED_SHUTDOWN(mp)) {
  2980. error = -EIO;
  2981. goto abort_out;
  2982. }
  2983. /*
  2984. * Get the buffer containing the on-disk inode.
  2985. */
  2986. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2987. 0);
  2988. if (error || !bp) {
  2989. xfs_ifunlock(ip);
  2990. return error;
  2991. }
  2992. /*
  2993. * First flush out the inode that xfs_iflush was called with.
  2994. */
  2995. error = xfs_iflush_int(ip, bp);
  2996. if (error)
  2997. goto corrupt_out;
  2998. /*
  2999. * If the buffer is pinned then push on the log now so we won't
  3000. * get stuck waiting in the write for too long.
  3001. */
  3002. if (xfs_buf_ispinned(bp))
  3003. xfs_log_force(mp, 0);
  3004. /*
  3005. * inode clustering:
  3006. * see if other inodes can be gathered into this write
  3007. */
  3008. error = xfs_iflush_cluster(ip, bp);
  3009. if (error)
  3010. goto cluster_corrupt_out;
  3011. *bpp = bp;
  3012. return 0;
  3013. corrupt_out:
  3014. xfs_buf_relse(bp);
  3015. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  3016. cluster_corrupt_out:
  3017. error = -EFSCORRUPTED;
  3018. abort_out:
  3019. /*
  3020. * Unlocks the flush lock
  3021. */
  3022. xfs_iflush_abort(ip, false);
  3023. return error;
  3024. }
  3025. STATIC int
  3026. xfs_iflush_int(
  3027. struct xfs_inode *ip,
  3028. struct xfs_buf *bp)
  3029. {
  3030. struct xfs_inode_log_item *iip = ip->i_itemp;
  3031. struct xfs_dinode *dip;
  3032. struct xfs_mount *mp = ip->i_mount;
  3033. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  3034. ASSERT(xfs_isiflocked(ip));
  3035. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  3036. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  3037. ASSERT(iip != NULL && iip->ili_fields != 0);
  3038. ASSERT(ip->i_d.di_version > 1);
  3039. /* set *dip = inode's place in the buffer */
  3040. dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
  3041. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  3042. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  3043. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3044. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  3045. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  3046. goto corrupt_out;
  3047. }
  3048. if (S_ISREG(ip->i_d.di_mode)) {
  3049. if (XFS_TEST_ERROR(
  3050. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3051. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  3052. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  3053. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3054. "%s: Bad regular inode %Lu, ptr 0x%p",
  3055. __func__, ip->i_ino, ip);
  3056. goto corrupt_out;
  3057. }
  3058. } else if (S_ISDIR(ip->i_d.di_mode)) {
  3059. if (XFS_TEST_ERROR(
  3060. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3061. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3062. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  3063. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  3064. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3065. "%s: Bad directory inode %Lu, ptr 0x%p",
  3066. __func__, ip->i_ino, ip);
  3067. goto corrupt_out;
  3068. }
  3069. }
  3070. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3071. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3072. XFS_RANDOM_IFLUSH_5)) {
  3073. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3074. "%s: detected corrupt incore inode %Lu, "
  3075. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  3076. __func__, ip->i_ino,
  3077. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3078. ip->i_d.di_nblocks, ip);
  3079. goto corrupt_out;
  3080. }
  3081. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3082. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3083. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3084. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3085. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  3086. goto corrupt_out;
  3087. }
  3088. /*
  3089. * Inode item log recovery for v2 inodes are dependent on the
  3090. * di_flushiter count for correct sequencing. We bump the flush
  3091. * iteration count so we can detect flushes which postdate a log record
  3092. * during recovery. This is redundant as we now log every change and
  3093. * hence this can't happen but we need to still do it to ensure
  3094. * backwards compatibility with old kernels that predate logging all
  3095. * inode changes.
  3096. */
  3097. if (ip->i_d.di_version < 3)
  3098. ip->i_d.di_flushiter++;
  3099. /*
  3100. * Copy the dirty parts of the inode into the on-disk inode. We always
  3101. * copy out the core of the inode, because if the inode is dirty at all
  3102. * the core must be.
  3103. */
  3104. xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
  3105. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3106. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3107. ip->i_d.di_flushiter = 0;
  3108. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  3109. if (XFS_IFORK_Q(ip))
  3110. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  3111. xfs_inobp_check(mp, bp);
  3112. /*
  3113. * We've recorded everything logged in the inode, so we'd like to clear
  3114. * the ili_fields bits so we don't log and flush things unnecessarily.
  3115. * However, we can't stop logging all this information until the data
  3116. * we've copied into the disk buffer is written to disk. If we did we
  3117. * might overwrite the copy of the inode in the log with all the data
  3118. * after re-logging only part of it, and in the face of a crash we
  3119. * wouldn't have all the data we need to recover.
  3120. *
  3121. * What we do is move the bits to the ili_last_fields field. When
  3122. * logging the inode, these bits are moved back to the ili_fields field.
  3123. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  3124. * know that the information those bits represent is permanently on
  3125. * disk. As long as the flush completes before the inode is logged
  3126. * again, then both ili_fields and ili_last_fields will be cleared.
  3127. *
  3128. * We can play with the ili_fields bits here, because the inode lock
  3129. * must be held exclusively in order to set bits there and the flush
  3130. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  3131. * done routine can tell whether or not to look in the AIL. Also, store
  3132. * the current LSN of the inode so that we can tell whether the item has
  3133. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  3134. * need the AIL lock, because it is a 64 bit value that cannot be read
  3135. * atomically.
  3136. */
  3137. iip->ili_last_fields = iip->ili_fields;
  3138. iip->ili_fields = 0;
  3139. iip->ili_fsync_fields = 0;
  3140. iip->ili_logged = 1;
  3141. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  3142. &iip->ili_item.li_lsn);
  3143. /*
  3144. * Attach the function xfs_iflush_done to the inode's
  3145. * buffer. This will remove the inode from the AIL
  3146. * and unlock the inode's flush lock when the inode is
  3147. * completely written to disk.
  3148. */
  3149. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  3150. /* generate the checksum. */
  3151. xfs_dinode_calc_crc(mp, dip);
  3152. ASSERT(bp->b_fspriv != NULL);
  3153. ASSERT(bp->b_iodone != NULL);
  3154. return 0;
  3155. corrupt_out:
  3156. return -EFSCORRUPTED;
  3157. }