xfs_inode.c 92 KB

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