xfs_inode.c 92 KB

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