xfs_inode.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321
  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_dinode.h"
  45. #include "xfs_filestream.h"
  46. #include "xfs_cksum.h"
  47. #include "xfs_trace.h"
  48. #include "xfs_icache.h"
  49. #include "xfs_symlink.h"
  50. #include "xfs_trans_priv.h"
  51. #include "xfs_log.h"
  52. #include "xfs_bmap_btree.h"
  53. kmem_zone_t *xfs_inode_zone;
  54. /*
  55. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  56. * freed from a file in a single transaction.
  57. */
  58. #define XFS_ITRUNC_MAX_EXTENTS 2
  59. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_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);
  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. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1051. prid = xfs_get_projid(dp);
  1052. else
  1053. prid = XFS_PROJID_DEFAULT;
  1054. /*
  1055. * Make sure that we have allocated dquot(s) on disk.
  1056. */
  1057. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1058. xfs_kgid_to_gid(current_fsgid()), prid,
  1059. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1060. &udqp, &gdqp, &pdqp);
  1061. if (error)
  1062. return error;
  1063. if (is_dir) {
  1064. rdev = 0;
  1065. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1066. tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
  1067. tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
  1068. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1069. } else {
  1070. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1071. tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
  1072. tres.tr_logcount = XFS_CREATE_LOG_COUNT;
  1073. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1074. }
  1075. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1076. /*
  1077. * Initially assume that the file does not exist and
  1078. * reserve the resources for that case. If that is not
  1079. * the case we'll drop the one we have and get a more
  1080. * appropriate transaction later.
  1081. */
  1082. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  1083. error = xfs_trans_reserve(tp, &tres, resblks, 0);
  1084. if (error == ENOSPC) {
  1085. /* flush outstanding delalloc blocks and retry */
  1086. xfs_flush_inodes(mp);
  1087. error = xfs_trans_reserve(tp, &tres, resblks, 0);
  1088. }
  1089. if (error == ENOSPC) {
  1090. /* No space at all so try a "no-allocation" reservation */
  1091. resblks = 0;
  1092. error = xfs_trans_reserve(tp, &tres, 0, 0);
  1093. }
  1094. if (error) {
  1095. cancel_flags = 0;
  1096. goto out_trans_cancel;
  1097. }
  1098. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1099. unlock_dp_on_error = true;
  1100. xfs_bmap_init(&free_list, &first_block);
  1101. /*
  1102. * Reserve disk quota and the inode.
  1103. */
  1104. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1105. pdqp, resblks, 1, 0);
  1106. if (error)
  1107. goto out_trans_cancel;
  1108. error = xfs_dir_canenter(tp, dp, name, resblks);
  1109. if (error)
  1110. goto out_trans_cancel;
  1111. /*
  1112. * A newly created regular or special file just has one directory
  1113. * entry pointing to them, but a directory also the "." entry
  1114. * pointing to itself.
  1115. */
  1116. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  1117. prid, resblks > 0, &ip, &committed);
  1118. if (error) {
  1119. if (error == ENOSPC)
  1120. goto out_trans_cancel;
  1121. goto out_trans_abort;
  1122. }
  1123. /*
  1124. * Now we join the directory inode to the transaction. We do not do it
  1125. * earlier because xfs_dir_ialloc might commit the previous transaction
  1126. * (and release all the locks). An error from here on will result in
  1127. * the transaction cancel unlocking dp so don't do it explicitly in the
  1128. * error path.
  1129. */
  1130. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1131. unlock_dp_on_error = false;
  1132. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1133. &first_block, &free_list, resblks ?
  1134. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1135. if (error) {
  1136. ASSERT(error != ENOSPC);
  1137. goto out_trans_abort;
  1138. }
  1139. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1140. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1141. if (is_dir) {
  1142. error = xfs_dir_init(tp, ip, dp);
  1143. if (error)
  1144. goto out_bmap_cancel;
  1145. error = xfs_bumplink(tp, dp);
  1146. if (error)
  1147. goto out_bmap_cancel;
  1148. }
  1149. /*
  1150. * If this is a synchronous mount, make sure that the
  1151. * create transaction goes to disk before returning to
  1152. * the user.
  1153. */
  1154. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1155. xfs_trans_set_sync(tp);
  1156. /*
  1157. * Attach the dquot(s) to the inodes and modify them incore.
  1158. * These ids of the inode couldn't have changed since the new
  1159. * inode has been locked ever since it was created.
  1160. */
  1161. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1162. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1163. if (error)
  1164. goto out_bmap_cancel;
  1165. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1166. if (error)
  1167. goto out_release_inode;
  1168. xfs_qm_dqrele(udqp);
  1169. xfs_qm_dqrele(gdqp);
  1170. xfs_qm_dqrele(pdqp);
  1171. *ipp = ip;
  1172. return 0;
  1173. out_bmap_cancel:
  1174. xfs_bmap_cancel(&free_list);
  1175. out_trans_abort:
  1176. cancel_flags |= XFS_TRANS_ABORT;
  1177. out_trans_cancel:
  1178. xfs_trans_cancel(tp, cancel_flags);
  1179. out_release_inode:
  1180. /*
  1181. * Wait until after the current transaction is aborted to
  1182. * release the inode. This prevents recursive transactions
  1183. * and deadlocks from xfs_inactive.
  1184. */
  1185. if (ip)
  1186. IRELE(ip);
  1187. xfs_qm_dqrele(udqp);
  1188. xfs_qm_dqrele(gdqp);
  1189. xfs_qm_dqrele(pdqp);
  1190. if (unlock_dp_on_error)
  1191. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1192. return error;
  1193. }
  1194. int
  1195. xfs_link(
  1196. xfs_inode_t *tdp,
  1197. xfs_inode_t *sip,
  1198. struct xfs_name *target_name)
  1199. {
  1200. xfs_mount_t *mp = tdp->i_mount;
  1201. xfs_trans_t *tp;
  1202. int error;
  1203. xfs_bmap_free_t free_list;
  1204. xfs_fsblock_t first_block;
  1205. int cancel_flags;
  1206. int committed;
  1207. int resblks;
  1208. trace_xfs_link(tdp, target_name);
  1209. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1210. if (XFS_FORCED_SHUTDOWN(mp))
  1211. return XFS_ERROR(EIO);
  1212. error = xfs_qm_dqattach(sip, 0);
  1213. if (error)
  1214. goto std_return;
  1215. error = xfs_qm_dqattach(tdp, 0);
  1216. if (error)
  1217. goto std_return;
  1218. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1219. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1220. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1221. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
  1222. if (error == ENOSPC) {
  1223. resblks = 0;
  1224. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
  1225. }
  1226. if (error) {
  1227. cancel_flags = 0;
  1228. goto error_return;
  1229. }
  1230. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1231. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1232. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1233. /*
  1234. * If we are using project inheritance, we only allow hard link
  1235. * creation in our tree when the project IDs are the same; else
  1236. * the tree quota mechanism could be circumvented.
  1237. */
  1238. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1239. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1240. error = XFS_ERROR(EXDEV);
  1241. goto error_return;
  1242. }
  1243. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1244. if (error)
  1245. goto error_return;
  1246. xfs_bmap_init(&free_list, &first_block);
  1247. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1248. &first_block, &free_list, resblks);
  1249. if (error)
  1250. goto abort_return;
  1251. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1252. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1253. error = xfs_bumplink(tp, sip);
  1254. if (error)
  1255. goto abort_return;
  1256. /*
  1257. * If this is a synchronous mount, make sure that the
  1258. * link transaction goes to disk before returning to
  1259. * the user.
  1260. */
  1261. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1262. xfs_trans_set_sync(tp);
  1263. }
  1264. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1265. if (error) {
  1266. xfs_bmap_cancel(&free_list);
  1267. goto abort_return;
  1268. }
  1269. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1270. abort_return:
  1271. cancel_flags |= XFS_TRANS_ABORT;
  1272. error_return:
  1273. xfs_trans_cancel(tp, cancel_flags);
  1274. std_return:
  1275. return error;
  1276. }
  1277. /*
  1278. * Free up the underlying blocks past new_size. The new size must be smaller
  1279. * than the current size. This routine can be used both for the attribute and
  1280. * data fork, and does not modify the inode size, which is left to the caller.
  1281. *
  1282. * The transaction passed to this routine must have made a permanent log
  1283. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1284. * given transaction and start new ones, so make sure everything involved in
  1285. * the transaction is tidy before calling here. Some transaction will be
  1286. * returned to the caller to be committed. The incoming transaction must
  1287. * already include the inode, and both inode locks must be held exclusively.
  1288. * The inode must also be "held" within the transaction. On return the inode
  1289. * will be "held" within the returned transaction. This routine does NOT
  1290. * require any disk space to be reserved for it within the transaction.
  1291. *
  1292. * If we get an error, we must return with the inode locked and linked into the
  1293. * current transaction. This keeps things simple for the higher level code,
  1294. * because it always knows that the inode is locked and held in the transaction
  1295. * that returns to it whether errors occur or not. We don't mark the inode
  1296. * dirty on error so that transactions can be easily aborted if possible.
  1297. */
  1298. int
  1299. xfs_itruncate_extents(
  1300. struct xfs_trans **tpp,
  1301. struct xfs_inode *ip,
  1302. int whichfork,
  1303. xfs_fsize_t new_size)
  1304. {
  1305. struct xfs_mount *mp = ip->i_mount;
  1306. struct xfs_trans *tp = *tpp;
  1307. struct xfs_trans *ntp;
  1308. xfs_bmap_free_t free_list;
  1309. xfs_fsblock_t first_block;
  1310. xfs_fileoff_t first_unmap_block;
  1311. xfs_fileoff_t last_block;
  1312. xfs_filblks_t unmap_len;
  1313. int committed;
  1314. int error = 0;
  1315. int done = 0;
  1316. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1317. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1318. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1319. ASSERT(new_size <= XFS_ISIZE(ip));
  1320. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1321. ASSERT(ip->i_itemp != NULL);
  1322. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1323. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1324. trace_xfs_itruncate_extents_start(ip, new_size);
  1325. /*
  1326. * Since it is possible for space to become allocated beyond
  1327. * the end of the file (in a crash where the space is allocated
  1328. * but the inode size is not yet updated), simply remove any
  1329. * blocks which show up between the new EOF and the maximum
  1330. * possible file size. If the first block to be removed is
  1331. * beyond the maximum file size (ie it is the same as last_block),
  1332. * then there is nothing to do.
  1333. */
  1334. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1335. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1336. if (first_unmap_block == last_block)
  1337. return 0;
  1338. ASSERT(first_unmap_block < last_block);
  1339. unmap_len = last_block - first_unmap_block + 1;
  1340. while (!done) {
  1341. xfs_bmap_init(&free_list, &first_block);
  1342. error = xfs_bunmapi(tp, ip,
  1343. first_unmap_block, unmap_len,
  1344. xfs_bmapi_aflag(whichfork),
  1345. XFS_ITRUNC_MAX_EXTENTS,
  1346. &first_block, &free_list,
  1347. &done);
  1348. if (error)
  1349. goto out_bmap_cancel;
  1350. /*
  1351. * Duplicate the transaction that has the permanent
  1352. * reservation and commit the old transaction.
  1353. */
  1354. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1355. if (committed)
  1356. xfs_trans_ijoin(tp, ip, 0);
  1357. if (error)
  1358. goto out_bmap_cancel;
  1359. if (committed) {
  1360. /*
  1361. * Mark the inode dirty so it will be logged and
  1362. * moved forward in the log as part of every commit.
  1363. */
  1364. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1365. }
  1366. ntp = xfs_trans_dup(tp);
  1367. error = xfs_trans_commit(tp, 0);
  1368. tp = ntp;
  1369. xfs_trans_ijoin(tp, ip, 0);
  1370. if (error)
  1371. goto out;
  1372. /*
  1373. * Transaction commit worked ok so we can drop the extra ticket
  1374. * reference that we gained in xfs_trans_dup()
  1375. */
  1376. xfs_log_ticket_put(tp->t_ticket);
  1377. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1378. if (error)
  1379. goto out;
  1380. }
  1381. /*
  1382. * Always re-log the inode so that our permanent transaction can keep
  1383. * on rolling it forward in the log.
  1384. */
  1385. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1386. trace_xfs_itruncate_extents_end(ip, new_size);
  1387. out:
  1388. *tpp = tp;
  1389. return error;
  1390. out_bmap_cancel:
  1391. /*
  1392. * If the bunmapi call encounters an error, return to the caller where
  1393. * the transaction can be properly aborted. We just need to make sure
  1394. * we're not holding any resources that we were not when we came in.
  1395. */
  1396. xfs_bmap_cancel(&free_list);
  1397. goto out;
  1398. }
  1399. int
  1400. xfs_release(
  1401. xfs_inode_t *ip)
  1402. {
  1403. xfs_mount_t *mp = ip->i_mount;
  1404. int error;
  1405. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1406. return 0;
  1407. /* If this is a read-only mount, don't do this (would generate I/O) */
  1408. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1409. return 0;
  1410. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1411. int truncated;
  1412. /*
  1413. * If we are using filestreams, and we have an unlinked
  1414. * file that we are processing the last close on, then nothing
  1415. * will be able to reopen and write to this file. Purge this
  1416. * inode from the filestreams cache so that it doesn't delay
  1417. * teardown of the inode.
  1418. */
  1419. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1420. xfs_filestream_deassociate(ip);
  1421. /*
  1422. * If we previously truncated this file and removed old data
  1423. * in the process, we want to initiate "early" writeout on
  1424. * the last close. This is an attempt to combat the notorious
  1425. * NULL files problem which is particularly noticeable from a
  1426. * truncate down, buffered (re-)write (delalloc), followed by
  1427. * a crash. What we are effectively doing here is
  1428. * significantly reducing the time window where we'd otherwise
  1429. * be exposed to that problem.
  1430. */
  1431. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1432. if (truncated) {
  1433. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1434. if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
  1435. error = -filemap_flush(VFS_I(ip)->i_mapping);
  1436. if (error)
  1437. return error;
  1438. }
  1439. }
  1440. }
  1441. if (ip->i_d.di_nlink == 0)
  1442. return 0;
  1443. if (xfs_can_free_eofblocks(ip, false)) {
  1444. /*
  1445. * If we can't get the iolock just skip truncating the blocks
  1446. * past EOF because we could deadlock with the mmap_sem
  1447. * otherwise. We'll get another chance to drop them once the
  1448. * last reference to the inode is dropped, so we'll never leak
  1449. * blocks permanently.
  1450. *
  1451. * Further, check if the inode is being opened, written and
  1452. * closed frequently and we have delayed allocation blocks
  1453. * outstanding (e.g. streaming writes from the NFS server),
  1454. * truncating the blocks past EOF will cause fragmentation to
  1455. * occur.
  1456. *
  1457. * In this case don't do the truncation, either, but we have to
  1458. * be careful how we detect this case. Blocks beyond EOF show
  1459. * up as i_delayed_blks even when the inode is clean, so we
  1460. * need to truncate them away first before checking for a dirty
  1461. * release. Hence on the first dirty close we will still remove
  1462. * the speculative allocation, but after that we will leave it
  1463. * in place.
  1464. */
  1465. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1466. return 0;
  1467. error = xfs_free_eofblocks(mp, ip, true);
  1468. if (error && error != EAGAIN)
  1469. return error;
  1470. /* delalloc blocks after truncation means it really is dirty */
  1471. if (ip->i_delayed_blks)
  1472. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1473. }
  1474. return 0;
  1475. }
  1476. /*
  1477. * xfs_inactive_truncate
  1478. *
  1479. * Called to perform a truncate when an inode becomes unlinked.
  1480. */
  1481. STATIC int
  1482. xfs_inactive_truncate(
  1483. struct xfs_inode *ip)
  1484. {
  1485. struct xfs_mount *mp = ip->i_mount;
  1486. struct xfs_trans *tp;
  1487. int error;
  1488. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1489. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1490. if (error) {
  1491. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1492. xfs_trans_cancel(tp, 0);
  1493. return error;
  1494. }
  1495. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1496. xfs_trans_ijoin(tp, ip, 0);
  1497. /*
  1498. * Log the inode size first to prevent stale data exposure in the event
  1499. * of a system crash before the truncate completes. See the related
  1500. * comment in xfs_setattr_size() for details.
  1501. */
  1502. ip->i_d.di_size = 0;
  1503. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1504. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1505. if (error)
  1506. goto error_trans_cancel;
  1507. ASSERT(ip->i_d.di_nextents == 0);
  1508. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1509. if (error)
  1510. goto error_unlock;
  1511. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1512. return 0;
  1513. error_trans_cancel:
  1514. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1515. error_unlock:
  1516. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1517. return error;
  1518. }
  1519. /*
  1520. * xfs_inactive_ifree()
  1521. *
  1522. * Perform the inode free when an inode is unlinked.
  1523. */
  1524. STATIC int
  1525. xfs_inactive_ifree(
  1526. struct xfs_inode *ip)
  1527. {
  1528. xfs_bmap_free_t free_list;
  1529. xfs_fsblock_t first_block;
  1530. int committed;
  1531. struct xfs_mount *mp = ip->i_mount;
  1532. struct xfs_trans *tp;
  1533. int error;
  1534. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1535. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0);
  1536. if (error) {
  1537. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1538. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  1539. return error;
  1540. }
  1541. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1542. xfs_trans_ijoin(tp, ip, 0);
  1543. xfs_bmap_init(&free_list, &first_block);
  1544. error = xfs_ifree(tp, ip, &free_list);
  1545. if (error) {
  1546. /*
  1547. * If we fail to free the inode, shut down. The cancel
  1548. * might do that, we need to make sure. Otherwise the
  1549. * inode might be lost for a long time or forever.
  1550. */
  1551. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1552. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1553. __func__, error);
  1554. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1555. }
  1556. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1557. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1558. return error;
  1559. }
  1560. /*
  1561. * Credit the quota account(s). The inode is gone.
  1562. */
  1563. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1564. /*
  1565. * Just ignore errors at this point. There is nothing we can
  1566. * do except to try to keep going. Make sure it's not a silent
  1567. * error.
  1568. */
  1569. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1570. if (error)
  1571. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  1572. __func__, error);
  1573. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1574. if (error)
  1575. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1576. __func__, error);
  1577. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1578. return 0;
  1579. }
  1580. /*
  1581. * xfs_inactive
  1582. *
  1583. * This is called when the vnode reference count for the vnode
  1584. * goes to zero. If the file has been unlinked, then it must
  1585. * now be truncated. Also, we clear all of the read-ahead state
  1586. * kept for the inode here since the file is now closed.
  1587. */
  1588. void
  1589. xfs_inactive(
  1590. xfs_inode_t *ip)
  1591. {
  1592. struct xfs_mount *mp;
  1593. int error;
  1594. int truncate = 0;
  1595. /*
  1596. * If the inode is already free, then there can be nothing
  1597. * to clean up here.
  1598. */
  1599. if (ip->i_d.di_mode == 0) {
  1600. ASSERT(ip->i_df.if_real_bytes == 0);
  1601. ASSERT(ip->i_df.if_broot_bytes == 0);
  1602. return;
  1603. }
  1604. mp = ip->i_mount;
  1605. /* If this is a read-only mount, don't do this (would generate I/O) */
  1606. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1607. return;
  1608. if (ip->i_d.di_nlink != 0) {
  1609. /*
  1610. * force is true because we are evicting an inode from the
  1611. * cache. Post-eof blocks must be freed, lest we end up with
  1612. * broken free space accounting.
  1613. */
  1614. if (xfs_can_free_eofblocks(ip, true))
  1615. xfs_free_eofblocks(mp, ip, false);
  1616. return;
  1617. }
  1618. if (S_ISREG(ip->i_d.di_mode) &&
  1619. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1620. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  1621. truncate = 1;
  1622. error = xfs_qm_dqattach(ip, 0);
  1623. if (error)
  1624. return;
  1625. if (S_ISLNK(ip->i_d.di_mode))
  1626. error = xfs_inactive_symlink(ip);
  1627. else if (truncate)
  1628. error = xfs_inactive_truncate(ip);
  1629. if (error)
  1630. return;
  1631. /*
  1632. * If there are attributes associated with the file then blow them away
  1633. * now. The code calls a routine that recursively deconstructs the
  1634. * attribute fork. We need to just commit the current transaction
  1635. * because we can't use it for xfs_attr_inactive().
  1636. */
  1637. if (ip->i_d.di_anextents > 0) {
  1638. ASSERT(ip->i_d.di_forkoff != 0);
  1639. error = xfs_attr_inactive(ip);
  1640. if (error)
  1641. return;
  1642. }
  1643. if (ip->i_afp)
  1644. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1645. ASSERT(ip->i_d.di_anextents == 0);
  1646. /*
  1647. * Free the inode.
  1648. */
  1649. error = xfs_inactive_ifree(ip);
  1650. if (error)
  1651. return;
  1652. /*
  1653. * Release the dquots held by inode, if any.
  1654. */
  1655. xfs_qm_dqdetach(ip);
  1656. }
  1657. /*
  1658. * This is called when the inode's link count goes to 0.
  1659. * We place the on-disk inode on a list in the AGI. It
  1660. * will be pulled from this list when the inode is freed.
  1661. */
  1662. int
  1663. xfs_iunlink(
  1664. xfs_trans_t *tp,
  1665. xfs_inode_t *ip)
  1666. {
  1667. xfs_mount_t *mp;
  1668. xfs_agi_t *agi;
  1669. xfs_dinode_t *dip;
  1670. xfs_buf_t *agibp;
  1671. xfs_buf_t *ibp;
  1672. xfs_agino_t agino;
  1673. short bucket_index;
  1674. int offset;
  1675. int error;
  1676. ASSERT(ip->i_d.di_nlink == 0);
  1677. ASSERT(ip->i_d.di_mode != 0);
  1678. mp = tp->t_mountp;
  1679. /*
  1680. * Get the agi buffer first. It ensures lock ordering
  1681. * on the list.
  1682. */
  1683. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1684. if (error)
  1685. return error;
  1686. agi = XFS_BUF_TO_AGI(agibp);
  1687. /*
  1688. * Get the index into the agi hash table for the
  1689. * list this inode will go on.
  1690. */
  1691. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1692. ASSERT(agino != 0);
  1693. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1694. ASSERT(agi->agi_unlinked[bucket_index]);
  1695. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1696. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1697. /*
  1698. * There is already another inode in the bucket we need
  1699. * to add ourselves to. Add us at the front of the list.
  1700. * Here we put the head pointer into our next pointer,
  1701. * and then we fall through to point the head at us.
  1702. */
  1703. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1704. 0, 0);
  1705. if (error)
  1706. return error;
  1707. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1708. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1709. offset = ip->i_imap.im_boffset +
  1710. offsetof(xfs_dinode_t, di_next_unlinked);
  1711. /* need to recalc the inode CRC if appropriate */
  1712. xfs_dinode_calc_crc(mp, dip);
  1713. xfs_trans_inode_buf(tp, ibp);
  1714. xfs_trans_log_buf(tp, ibp, offset,
  1715. (offset + sizeof(xfs_agino_t) - 1));
  1716. xfs_inobp_check(mp, ibp);
  1717. }
  1718. /*
  1719. * Point the bucket head pointer at the inode being inserted.
  1720. */
  1721. ASSERT(agino != 0);
  1722. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1723. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1724. (sizeof(xfs_agino_t) * bucket_index);
  1725. xfs_trans_log_buf(tp, agibp, offset,
  1726. (offset + sizeof(xfs_agino_t) - 1));
  1727. return 0;
  1728. }
  1729. /*
  1730. * Pull the on-disk inode from the AGI unlinked list.
  1731. */
  1732. STATIC int
  1733. xfs_iunlink_remove(
  1734. xfs_trans_t *tp,
  1735. xfs_inode_t *ip)
  1736. {
  1737. xfs_ino_t next_ino;
  1738. xfs_mount_t *mp;
  1739. xfs_agi_t *agi;
  1740. xfs_dinode_t *dip;
  1741. xfs_buf_t *agibp;
  1742. xfs_buf_t *ibp;
  1743. xfs_agnumber_t agno;
  1744. xfs_agino_t agino;
  1745. xfs_agino_t next_agino;
  1746. xfs_buf_t *last_ibp;
  1747. xfs_dinode_t *last_dip = NULL;
  1748. short bucket_index;
  1749. int offset, last_offset = 0;
  1750. int error;
  1751. mp = tp->t_mountp;
  1752. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1753. /*
  1754. * Get the agi buffer first. It ensures lock ordering
  1755. * on the list.
  1756. */
  1757. error = xfs_read_agi(mp, tp, agno, &agibp);
  1758. if (error)
  1759. return error;
  1760. agi = XFS_BUF_TO_AGI(agibp);
  1761. /*
  1762. * Get the index into the agi hash table for the
  1763. * list this inode will go on.
  1764. */
  1765. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1766. ASSERT(agino != 0);
  1767. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1768. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1769. ASSERT(agi->agi_unlinked[bucket_index]);
  1770. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1771. /*
  1772. * We're at the head of the list. Get the inode's on-disk
  1773. * buffer to see if there is anyone after us on the list.
  1774. * Only modify our next pointer if it is not already NULLAGINO.
  1775. * This saves us the overhead of dealing with the buffer when
  1776. * there is no need to change it.
  1777. */
  1778. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1779. 0, 0);
  1780. if (error) {
  1781. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1782. __func__, error);
  1783. return error;
  1784. }
  1785. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1786. ASSERT(next_agino != 0);
  1787. if (next_agino != NULLAGINO) {
  1788. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1789. offset = ip->i_imap.im_boffset +
  1790. offsetof(xfs_dinode_t, di_next_unlinked);
  1791. /* need to recalc the inode CRC if appropriate */
  1792. xfs_dinode_calc_crc(mp, dip);
  1793. xfs_trans_inode_buf(tp, ibp);
  1794. xfs_trans_log_buf(tp, ibp, offset,
  1795. (offset + sizeof(xfs_agino_t) - 1));
  1796. xfs_inobp_check(mp, ibp);
  1797. } else {
  1798. xfs_trans_brelse(tp, ibp);
  1799. }
  1800. /*
  1801. * Point the bucket head pointer at the next inode.
  1802. */
  1803. ASSERT(next_agino != 0);
  1804. ASSERT(next_agino != agino);
  1805. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1806. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1807. (sizeof(xfs_agino_t) * bucket_index);
  1808. xfs_trans_log_buf(tp, agibp, offset,
  1809. (offset + sizeof(xfs_agino_t) - 1));
  1810. } else {
  1811. /*
  1812. * We need to search the list for the inode being freed.
  1813. */
  1814. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1815. last_ibp = NULL;
  1816. while (next_agino != agino) {
  1817. struct xfs_imap imap;
  1818. if (last_ibp)
  1819. xfs_trans_brelse(tp, last_ibp);
  1820. imap.im_blkno = 0;
  1821. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1822. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1823. if (error) {
  1824. xfs_warn(mp,
  1825. "%s: xfs_imap returned error %d.",
  1826. __func__, error);
  1827. return error;
  1828. }
  1829. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1830. &last_ibp, 0, 0);
  1831. if (error) {
  1832. xfs_warn(mp,
  1833. "%s: xfs_imap_to_bp returned error %d.",
  1834. __func__, error);
  1835. return error;
  1836. }
  1837. last_offset = imap.im_boffset;
  1838. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1839. ASSERT(next_agino != NULLAGINO);
  1840. ASSERT(next_agino != 0);
  1841. }
  1842. /*
  1843. * Now last_ibp points to the buffer previous to us on the
  1844. * unlinked list. Pull us from the list.
  1845. */
  1846. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1847. 0, 0);
  1848. if (error) {
  1849. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1850. __func__, error);
  1851. return error;
  1852. }
  1853. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1854. ASSERT(next_agino != 0);
  1855. ASSERT(next_agino != agino);
  1856. if (next_agino != NULLAGINO) {
  1857. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1858. offset = ip->i_imap.im_boffset +
  1859. offsetof(xfs_dinode_t, di_next_unlinked);
  1860. /* need to recalc the inode CRC if appropriate */
  1861. xfs_dinode_calc_crc(mp, dip);
  1862. xfs_trans_inode_buf(tp, ibp);
  1863. xfs_trans_log_buf(tp, ibp, offset,
  1864. (offset + sizeof(xfs_agino_t) - 1));
  1865. xfs_inobp_check(mp, ibp);
  1866. } else {
  1867. xfs_trans_brelse(tp, ibp);
  1868. }
  1869. /*
  1870. * Point the previous inode on the list to the next inode.
  1871. */
  1872. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1873. ASSERT(next_agino != 0);
  1874. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1875. /* need to recalc the inode CRC if appropriate */
  1876. xfs_dinode_calc_crc(mp, last_dip);
  1877. xfs_trans_inode_buf(tp, last_ibp);
  1878. xfs_trans_log_buf(tp, last_ibp, offset,
  1879. (offset + sizeof(xfs_agino_t) - 1));
  1880. xfs_inobp_check(mp, last_ibp);
  1881. }
  1882. return 0;
  1883. }
  1884. /*
  1885. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1886. * inodes that are in memory - they all must be marked stale and attached to
  1887. * the cluster buffer.
  1888. */
  1889. STATIC int
  1890. xfs_ifree_cluster(
  1891. xfs_inode_t *free_ip,
  1892. xfs_trans_t *tp,
  1893. xfs_ino_t inum)
  1894. {
  1895. xfs_mount_t *mp = free_ip->i_mount;
  1896. int blks_per_cluster;
  1897. int inodes_per_cluster;
  1898. int nbufs;
  1899. int i, j;
  1900. xfs_daddr_t blkno;
  1901. xfs_buf_t *bp;
  1902. xfs_inode_t *ip;
  1903. xfs_inode_log_item_t *iip;
  1904. xfs_log_item_t *lip;
  1905. struct xfs_perag *pag;
  1906. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1907. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1908. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  1909. nbufs = mp->m_ialloc_blks / blks_per_cluster;
  1910. for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
  1911. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1912. XFS_INO_TO_AGBNO(mp, inum));
  1913. /*
  1914. * We obtain and lock the backing buffer first in the process
  1915. * here, as we have to ensure that any dirty inode that we
  1916. * can't get the flush lock on is attached to the buffer.
  1917. * If we scan the in-memory inodes first, then buffer IO can
  1918. * complete before we get a lock on it, and hence we may fail
  1919. * to mark all the active inodes on the buffer stale.
  1920. */
  1921. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1922. mp->m_bsize * blks_per_cluster,
  1923. XBF_UNMAPPED);
  1924. if (!bp)
  1925. return ENOMEM;
  1926. /*
  1927. * This buffer may not have been correctly initialised as we
  1928. * didn't read it from disk. That's not important because we are
  1929. * only using to mark the buffer as stale in the log, and to
  1930. * attach stale cached inodes on it. That means it will never be
  1931. * dispatched for IO. If it is, we want to know about it, and we
  1932. * want it to fail. We can acheive this by adding a write
  1933. * verifier to the buffer.
  1934. */
  1935. bp->b_ops = &xfs_inode_buf_ops;
  1936. /*
  1937. * Walk the inodes already attached to the buffer and mark them
  1938. * stale. These will all have the flush locks held, so an
  1939. * in-memory inode walk can't lock them. By marking them all
  1940. * stale first, we will not attempt to lock them in the loop
  1941. * below as the XFS_ISTALE flag will be set.
  1942. */
  1943. lip = bp->b_fspriv;
  1944. while (lip) {
  1945. if (lip->li_type == XFS_LI_INODE) {
  1946. iip = (xfs_inode_log_item_t *)lip;
  1947. ASSERT(iip->ili_logged == 1);
  1948. lip->li_cb = xfs_istale_done;
  1949. xfs_trans_ail_copy_lsn(mp->m_ail,
  1950. &iip->ili_flush_lsn,
  1951. &iip->ili_item.li_lsn);
  1952. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1953. }
  1954. lip = lip->li_bio_list;
  1955. }
  1956. /*
  1957. * For each inode in memory attempt to add it to the inode
  1958. * buffer and set it up for being staled on buffer IO
  1959. * completion. This is safe as we've locked out tail pushing
  1960. * and flushing by locking the buffer.
  1961. *
  1962. * We have already marked every inode that was part of a
  1963. * transaction stale above, which means there is no point in
  1964. * even trying to lock them.
  1965. */
  1966. for (i = 0; i < inodes_per_cluster; i++) {
  1967. retry:
  1968. rcu_read_lock();
  1969. ip = radix_tree_lookup(&pag->pag_ici_root,
  1970. XFS_INO_TO_AGINO(mp, (inum + i)));
  1971. /* Inode not in memory, nothing to do */
  1972. if (!ip) {
  1973. rcu_read_unlock();
  1974. continue;
  1975. }
  1976. /*
  1977. * because this is an RCU protected lookup, we could
  1978. * find a recently freed or even reallocated inode
  1979. * during the lookup. We need to check under the
  1980. * i_flags_lock for a valid inode here. Skip it if it
  1981. * is not valid, the wrong inode or stale.
  1982. */
  1983. spin_lock(&ip->i_flags_lock);
  1984. if (ip->i_ino != inum + i ||
  1985. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1986. spin_unlock(&ip->i_flags_lock);
  1987. rcu_read_unlock();
  1988. continue;
  1989. }
  1990. spin_unlock(&ip->i_flags_lock);
  1991. /*
  1992. * Don't try to lock/unlock the current inode, but we
  1993. * _cannot_ skip the other inodes that we did not find
  1994. * in the list attached to the buffer and are not
  1995. * already marked stale. If we can't lock it, back off
  1996. * and retry.
  1997. */
  1998. if (ip != free_ip &&
  1999. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2000. rcu_read_unlock();
  2001. delay(1);
  2002. goto retry;
  2003. }
  2004. rcu_read_unlock();
  2005. xfs_iflock(ip);
  2006. xfs_iflags_set(ip, XFS_ISTALE);
  2007. /*
  2008. * we don't need to attach clean inodes or those only
  2009. * with unlogged changes (which we throw away, anyway).
  2010. */
  2011. iip = ip->i_itemp;
  2012. if (!iip || xfs_inode_clean(ip)) {
  2013. ASSERT(ip != free_ip);
  2014. xfs_ifunlock(ip);
  2015. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2016. continue;
  2017. }
  2018. iip->ili_last_fields = iip->ili_fields;
  2019. iip->ili_fields = 0;
  2020. iip->ili_logged = 1;
  2021. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2022. &iip->ili_item.li_lsn);
  2023. xfs_buf_attach_iodone(bp, xfs_istale_done,
  2024. &iip->ili_item);
  2025. if (ip != free_ip)
  2026. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2027. }
  2028. xfs_trans_stale_inode_buf(tp, bp);
  2029. xfs_trans_binval(tp, bp);
  2030. }
  2031. xfs_perag_put(pag);
  2032. return 0;
  2033. }
  2034. /*
  2035. * This is called to return an inode to the inode free list.
  2036. * The inode should already be truncated to 0 length and have
  2037. * no pages associated with it. This routine also assumes that
  2038. * the inode is already a part of the transaction.
  2039. *
  2040. * The on-disk copy of the inode will have been added to the list
  2041. * of unlinked inodes in the AGI. We need to remove the inode from
  2042. * that list atomically with respect to freeing it here.
  2043. */
  2044. int
  2045. xfs_ifree(
  2046. xfs_trans_t *tp,
  2047. xfs_inode_t *ip,
  2048. xfs_bmap_free_t *flist)
  2049. {
  2050. int error;
  2051. int delete;
  2052. xfs_ino_t first_ino;
  2053. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2054. ASSERT(ip->i_d.di_nlink == 0);
  2055. ASSERT(ip->i_d.di_nextents == 0);
  2056. ASSERT(ip->i_d.di_anextents == 0);
  2057. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  2058. ASSERT(ip->i_d.di_nblocks == 0);
  2059. /*
  2060. * Pull the on-disk inode from the AGI unlinked list.
  2061. */
  2062. error = xfs_iunlink_remove(tp, ip);
  2063. if (error)
  2064. return error;
  2065. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2066. if (error)
  2067. return error;
  2068. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2069. ip->i_d.di_flags = 0;
  2070. ip->i_d.di_dmevmask = 0;
  2071. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2072. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2073. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2074. /*
  2075. * Bump the generation count so no one will be confused
  2076. * by reincarnations of this inode.
  2077. */
  2078. ip->i_d.di_gen++;
  2079. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2080. if (delete)
  2081. error = xfs_ifree_cluster(ip, tp, first_ino);
  2082. return error;
  2083. }
  2084. /*
  2085. * This is called to unpin an inode. The caller must have the inode locked
  2086. * in at least shared mode so that the buffer cannot be subsequently pinned
  2087. * once someone is waiting for it to be unpinned.
  2088. */
  2089. static void
  2090. xfs_iunpin(
  2091. struct xfs_inode *ip)
  2092. {
  2093. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2094. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2095. /* Give the log a push to start the unpinning I/O */
  2096. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2097. }
  2098. static void
  2099. __xfs_iunpin_wait(
  2100. struct xfs_inode *ip)
  2101. {
  2102. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2103. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2104. xfs_iunpin(ip);
  2105. do {
  2106. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2107. if (xfs_ipincount(ip))
  2108. io_schedule();
  2109. } while (xfs_ipincount(ip));
  2110. finish_wait(wq, &wait.wait);
  2111. }
  2112. void
  2113. xfs_iunpin_wait(
  2114. struct xfs_inode *ip)
  2115. {
  2116. if (xfs_ipincount(ip))
  2117. __xfs_iunpin_wait(ip);
  2118. }
  2119. /*
  2120. * Removing an inode from the namespace involves removing the directory entry
  2121. * and dropping the link count on the inode. Removing the directory entry can
  2122. * result in locking an AGF (directory blocks were freed) and removing a link
  2123. * count can result in placing the inode on an unlinked list which results in
  2124. * locking an AGI.
  2125. *
  2126. * The big problem here is that we have an ordering constraint on AGF and AGI
  2127. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2128. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2129. * removes the inode from the unlinked list, requiring that we lock the AGI
  2130. * first, and then freeing the inode can result in an inode chunk being freed
  2131. * and hence freeing disk space requiring that we lock an AGF.
  2132. *
  2133. * Hence the ordering that is imposed by other parts of the code is AGI before
  2134. * AGF. This means we cannot remove the directory entry before we drop the inode
  2135. * reference count and put it on the unlinked list as this results in a lock
  2136. * order of AGF then AGI, and this can deadlock against inode allocation and
  2137. * freeing. Therefore we must drop the link counts before we remove the
  2138. * directory entry.
  2139. *
  2140. * This is still safe from a transactional point of view - it is not until we
  2141. * get to xfs_bmap_finish() that we have the possibility of multiple
  2142. * transactions in this operation. Hence as long as we remove the directory
  2143. * entry and drop the link count in the first transaction of the remove
  2144. * operation, there are no transactional constraints on the ordering here.
  2145. */
  2146. int
  2147. xfs_remove(
  2148. xfs_inode_t *dp,
  2149. struct xfs_name *name,
  2150. xfs_inode_t *ip)
  2151. {
  2152. xfs_mount_t *mp = dp->i_mount;
  2153. xfs_trans_t *tp = NULL;
  2154. int is_dir = S_ISDIR(ip->i_d.di_mode);
  2155. int error = 0;
  2156. xfs_bmap_free_t free_list;
  2157. xfs_fsblock_t first_block;
  2158. int cancel_flags;
  2159. int committed;
  2160. int link_zero;
  2161. uint resblks;
  2162. uint log_count;
  2163. trace_xfs_remove(dp, name);
  2164. if (XFS_FORCED_SHUTDOWN(mp))
  2165. return XFS_ERROR(EIO);
  2166. error = xfs_qm_dqattach(dp, 0);
  2167. if (error)
  2168. goto std_return;
  2169. error = xfs_qm_dqattach(ip, 0);
  2170. if (error)
  2171. goto std_return;
  2172. if (is_dir) {
  2173. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2174. log_count = XFS_DEFAULT_LOG_COUNT;
  2175. } else {
  2176. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2177. log_count = XFS_REMOVE_LOG_COUNT;
  2178. }
  2179. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2180. /*
  2181. * We try to get the real space reservation first,
  2182. * allowing for directory btree deletion(s) implying
  2183. * possible bmap insert(s). If we can't get the space
  2184. * reservation then we use 0 instead, and avoid the bmap
  2185. * btree insert(s) in the directory code by, if the bmap
  2186. * insert tries to happen, instead trimming the LAST
  2187. * block from the directory.
  2188. */
  2189. resblks = XFS_REMOVE_SPACE_RES(mp);
  2190. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
  2191. if (error == ENOSPC) {
  2192. resblks = 0;
  2193. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
  2194. }
  2195. if (error) {
  2196. ASSERT(error != ENOSPC);
  2197. cancel_flags = 0;
  2198. goto out_trans_cancel;
  2199. }
  2200. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  2201. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2202. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2203. /*
  2204. * If we're removing a directory perform some additional validation.
  2205. */
  2206. cancel_flags |= XFS_TRANS_ABORT;
  2207. if (is_dir) {
  2208. ASSERT(ip->i_d.di_nlink >= 2);
  2209. if (ip->i_d.di_nlink != 2) {
  2210. error = XFS_ERROR(ENOTEMPTY);
  2211. goto out_trans_cancel;
  2212. }
  2213. if (!xfs_dir_isempty(ip)) {
  2214. error = XFS_ERROR(ENOTEMPTY);
  2215. goto out_trans_cancel;
  2216. }
  2217. /* Drop the link from ip's "..". */
  2218. error = xfs_droplink(tp, dp);
  2219. if (error)
  2220. goto out_trans_cancel;
  2221. /* Drop the "." link from ip to self. */
  2222. error = xfs_droplink(tp, ip);
  2223. if (error)
  2224. goto out_trans_cancel;
  2225. } else {
  2226. /*
  2227. * When removing a non-directory we need to log the parent
  2228. * inode here. For a directory this is done implicitly
  2229. * by the xfs_droplink call for the ".." entry.
  2230. */
  2231. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2232. }
  2233. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2234. /* Drop the link from dp to ip. */
  2235. error = xfs_droplink(tp, ip);
  2236. if (error)
  2237. goto out_trans_cancel;
  2238. /* Determine if this is the last link while the inode is locked */
  2239. link_zero = (ip->i_d.di_nlink == 0);
  2240. xfs_bmap_init(&free_list, &first_block);
  2241. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2242. &first_block, &free_list, resblks);
  2243. if (error) {
  2244. ASSERT(error != ENOENT);
  2245. goto out_bmap_cancel;
  2246. }
  2247. /*
  2248. * If this is a synchronous mount, make sure that the
  2249. * remove transaction goes to disk before returning to
  2250. * the user.
  2251. */
  2252. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2253. xfs_trans_set_sync(tp);
  2254. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2255. if (error)
  2256. goto out_bmap_cancel;
  2257. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2258. if (error)
  2259. goto std_return;
  2260. /*
  2261. * If we are using filestreams, kill the stream association.
  2262. * If the file is still open it may get a new one but that
  2263. * will get killed on last close in xfs_close() so we don't
  2264. * have to worry about that.
  2265. */
  2266. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  2267. xfs_filestream_deassociate(ip);
  2268. return 0;
  2269. out_bmap_cancel:
  2270. xfs_bmap_cancel(&free_list);
  2271. out_trans_cancel:
  2272. xfs_trans_cancel(tp, cancel_flags);
  2273. std_return:
  2274. return error;
  2275. }
  2276. /*
  2277. * Enter all inodes for a rename transaction into a sorted array.
  2278. */
  2279. STATIC void
  2280. xfs_sort_for_rename(
  2281. xfs_inode_t *dp1, /* in: old (source) directory inode */
  2282. xfs_inode_t *dp2, /* in: new (target) directory inode */
  2283. xfs_inode_t *ip1, /* in: inode of old entry */
  2284. xfs_inode_t *ip2, /* in: inode of new entry, if it
  2285. already exists, NULL otherwise. */
  2286. xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
  2287. int *num_inodes) /* out: number of inodes in array */
  2288. {
  2289. xfs_inode_t *temp;
  2290. int i, j;
  2291. /*
  2292. * i_tab contains a list of pointers to inodes. We initialize
  2293. * the table here & we'll sort it. We will then use it to
  2294. * order the acquisition of the inode locks.
  2295. *
  2296. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2297. */
  2298. i_tab[0] = dp1;
  2299. i_tab[1] = dp2;
  2300. i_tab[2] = ip1;
  2301. if (ip2) {
  2302. *num_inodes = 4;
  2303. i_tab[3] = ip2;
  2304. } else {
  2305. *num_inodes = 3;
  2306. i_tab[3] = NULL;
  2307. }
  2308. /*
  2309. * Sort the elements via bubble sort. (Remember, there are at
  2310. * most 4 elements to sort, so this is adequate.)
  2311. */
  2312. for (i = 0; i < *num_inodes; i++) {
  2313. for (j = 1; j < *num_inodes; j++) {
  2314. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2315. temp = i_tab[j];
  2316. i_tab[j] = i_tab[j-1];
  2317. i_tab[j-1] = temp;
  2318. }
  2319. }
  2320. }
  2321. }
  2322. /*
  2323. * xfs_rename
  2324. */
  2325. int
  2326. xfs_rename(
  2327. xfs_inode_t *src_dp,
  2328. struct xfs_name *src_name,
  2329. xfs_inode_t *src_ip,
  2330. xfs_inode_t *target_dp,
  2331. struct xfs_name *target_name,
  2332. xfs_inode_t *target_ip)
  2333. {
  2334. xfs_trans_t *tp = NULL;
  2335. xfs_mount_t *mp = src_dp->i_mount;
  2336. int new_parent; /* moving to a new dir */
  2337. int src_is_directory; /* src_name is a directory */
  2338. int error;
  2339. xfs_bmap_free_t free_list;
  2340. xfs_fsblock_t first_block;
  2341. int cancel_flags;
  2342. int committed;
  2343. xfs_inode_t *inodes[4];
  2344. int spaceres;
  2345. int num_inodes;
  2346. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2347. new_parent = (src_dp != target_dp);
  2348. src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
  2349. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
  2350. inodes, &num_inodes);
  2351. xfs_bmap_init(&free_list, &first_block);
  2352. tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
  2353. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2354. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2355. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
  2356. if (error == ENOSPC) {
  2357. spaceres = 0;
  2358. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
  2359. }
  2360. if (error) {
  2361. xfs_trans_cancel(tp, 0);
  2362. goto std_return;
  2363. }
  2364. /*
  2365. * Attach the dquots to the inodes
  2366. */
  2367. error = xfs_qm_vop_rename_dqattach(inodes);
  2368. if (error) {
  2369. xfs_trans_cancel(tp, cancel_flags);
  2370. goto std_return;
  2371. }
  2372. /*
  2373. * Lock all the participating inodes. Depending upon whether
  2374. * the target_name exists in the target directory, and
  2375. * whether the target directory is the same as the source
  2376. * directory, we can lock from 2 to 4 inodes.
  2377. */
  2378. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2379. /*
  2380. * Join all the inodes to the transaction. From this point on,
  2381. * we can rely on either trans_commit or trans_cancel to unlock
  2382. * them.
  2383. */
  2384. xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
  2385. if (new_parent)
  2386. xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
  2387. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2388. if (target_ip)
  2389. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2390. /*
  2391. * If we are using project inheritance, we only allow renames
  2392. * into our tree when the project IDs are the same; else the
  2393. * tree quota mechanism would be circumvented.
  2394. */
  2395. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2396. (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
  2397. error = XFS_ERROR(EXDEV);
  2398. goto error_return;
  2399. }
  2400. /*
  2401. * Set up the target.
  2402. */
  2403. if (target_ip == NULL) {
  2404. /*
  2405. * If there's no space reservation, check the entry will
  2406. * fit before actually inserting it.
  2407. */
  2408. error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
  2409. if (error)
  2410. goto error_return;
  2411. /*
  2412. * If target does not exist and the rename crosses
  2413. * directories, adjust the target directory link count
  2414. * to account for the ".." reference from the new entry.
  2415. */
  2416. error = xfs_dir_createname(tp, target_dp, target_name,
  2417. src_ip->i_ino, &first_block,
  2418. &free_list, spaceres);
  2419. if (error == ENOSPC)
  2420. goto error_return;
  2421. if (error)
  2422. goto abort_return;
  2423. xfs_trans_ichgtime(tp, target_dp,
  2424. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2425. if (new_parent && src_is_directory) {
  2426. error = xfs_bumplink(tp, target_dp);
  2427. if (error)
  2428. goto abort_return;
  2429. }
  2430. } else { /* target_ip != NULL */
  2431. /*
  2432. * If target exists and it's a directory, check that both
  2433. * target and source are directories and that target can be
  2434. * destroyed, or that neither is a directory.
  2435. */
  2436. if (S_ISDIR(target_ip->i_d.di_mode)) {
  2437. /*
  2438. * Make sure target dir is empty.
  2439. */
  2440. if (!(xfs_dir_isempty(target_ip)) ||
  2441. (target_ip->i_d.di_nlink > 2)) {
  2442. error = XFS_ERROR(EEXIST);
  2443. goto error_return;
  2444. }
  2445. }
  2446. /*
  2447. * Link the source inode under the target name.
  2448. * If the source inode is a directory and we are moving
  2449. * it across directories, its ".." entry will be
  2450. * inconsistent until we replace that down below.
  2451. *
  2452. * In case there is already an entry with the same
  2453. * name at the destination directory, remove it first.
  2454. */
  2455. error = xfs_dir_replace(tp, target_dp, target_name,
  2456. src_ip->i_ino,
  2457. &first_block, &free_list, spaceres);
  2458. if (error)
  2459. goto abort_return;
  2460. xfs_trans_ichgtime(tp, target_dp,
  2461. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2462. /*
  2463. * Decrement the link count on the target since the target
  2464. * dir no longer points to it.
  2465. */
  2466. error = xfs_droplink(tp, target_ip);
  2467. if (error)
  2468. goto abort_return;
  2469. if (src_is_directory) {
  2470. /*
  2471. * Drop the link from the old "." entry.
  2472. */
  2473. error = xfs_droplink(tp, target_ip);
  2474. if (error)
  2475. goto abort_return;
  2476. }
  2477. } /* target_ip != NULL */
  2478. /*
  2479. * Remove the source.
  2480. */
  2481. if (new_parent && src_is_directory) {
  2482. /*
  2483. * Rewrite the ".." entry to point to the new
  2484. * directory.
  2485. */
  2486. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2487. target_dp->i_ino,
  2488. &first_block, &free_list, spaceres);
  2489. ASSERT(error != EEXIST);
  2490. if (error)
  2491. goto abort_return;
  2492. }
  2493. /*
  2494. * We always want to hit the ctime on the source inode.
  2495. *
  2496. * This isn't strictly required by the standards since the source
  2497. * inode isn't really being changed, but old unix file systems did
  2498. * it and some incremental backup programs won't work without it.
  2499. */
  2500. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2501. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2502. /*
  2503. * Adjust the link count on src_dp. This is necessary when
  2504. * renaming a directory, either within one parent when
  2505. * the target existed, or across two parent directories.
  2506. */
  2507. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2508. /*
  2509. * Decrement link count on src_directory since the
  2510. * entry that's moved no longer points to it.
  2511. */
  2512. error = xfs_droplink(tp, src_dp);
  2513. if (error)
  2514. goto abort_return;
  2515. }
  2516. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  2517. &first_block, &free_list, spaceres);
  2518. if (error)
  2519. goto abort_return;
  2520. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2521. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  2522. if (new_parent)
  2523. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  2524. /*
  2525. * If this is a synchronous mount, make sure that the
  2526. * rename transaction goes to disk before returning to
  2527. * the user.
  2528. */
  2529. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2530. xfs_trans_set_sync(tp);
  2531. }
  2532. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2533. if (error) {
  2534. xfs_bmap_cancel(&free_list);
  2535. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2536. XFS_TRANS_ABORT));
  2537. goto std_return;
  2538. }
  2539. /*
  2540. * trans_commit will unlock src_ip, target_ip & decrement
  2541. * the vnode references.
  2542. */
  2543. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2544. abort_return:
  2545. cancel_flags |= XFS_TRANS_ABORT;
  2546. error_return:
  2547. xfs_bmap_cancel(&free_list);
  2548. xfs_trans_cancel(tp, cancel_flags);
  2549. std_return:
  2550. return error;
  2551. }
  2552. STATIC int
  2553. xfs_iflush_cluster(
  2554. xfs_inode_t *ip,
  2555. xfs_buf_t *bp)
  2556. {
  2557. xfs_mount_t *mp = ip->i_mount;
  2558. struct xfs_perag *pag;
  2559. unsigned long first_index, mask;
  2560. unsigned long inodes_per_cluster;
  2561. int ilist_size;
  2562. xfs_inode_t **ilist;
  2563. xfs_inode_t *iq;
  2564. int nr_found;
  2565. int clcount = 0;
  2566. int bufwasdelwri;
  2567. int i;
  2568. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2569. inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  2570. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2571. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2572. if (!ilist)
  2573. goto out_put;
  2574. mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
  2575. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2576. rcu_read_lock();
  2577. /* really need a gang lookup range call here */
  2578. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2579. first_index, inodes_per_cluster);
  2580. if (nr_found == 0)
  2581. goto out_free;
  2582. for (i = 0; i < nr_found; i++) {
  2583. iq = ilist[i];
  2584. if (iq == ip)
  2585. continue;
  2586. /*
  2587. * because this is an RCU protected lookup, we could find a
  2588. * recently freed or even reallocated inode during the lookup.
  2589. * We need to check under the i_flags_lock for a valid inode
  2590. * here. Skip it if it is not valid or the wrong inode.
  2591. */
  2592. spin_lock(&ip->i_flags_lock);
  2593. if (!ip->i_ino ||
  2594. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2595. spin_unlock(&ip->i_flags_lock);
  2596. continue;
  2597. }
  2598. spin_unlock(&ip->i_flags_lock);
  2599. /*
  2600. * Do an un-protected check to see if the inode is dirty and
  2601. * is a candidate for flushing. These checks will be repeated
  2602. * later after the appropriate locks are acquired.
  2603. */
  2604. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2605. continue;
  2606. /*
  2607. * Try to get locks. If any are unavailable or it is pinned,
  2608. * then this inode cannot be flushed and is skipped.
  2609. */
  2610. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2611. continue;
  2612. if (!xfs_iflock_nowait(iq)) {
  2613. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2614. continue;
  2615. }
  2616. if (xfs_ipincount(iq)) {
  2617. xfs_ifunlock(iq);
  2618. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2619. continue;
  2620. }
  2621. /*
  2622. * arriving here means that this inode can be flushed. First
  2623. * re-check that it's dirty before flushing.
  2624. */
  2625. if (!xfs_inode_clean(iq)) {
  2626. int error;
  2627. error = xfs_iflush_int(iq, bp);
  2628. if (error) {
  2629. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2630. goto cluster_corrupt_out;
  2631. }
  2632. clcount++;
  2633. } else {
  2634. xfs_ifunlock(iq);
  2635. }
  2636. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2637. }
  2638. if (clcount) {
  2639. XFS_STATS_INC(xs_icluster_flushcnt);
  2640. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2641. }
  2642. out_free:
  2643. rcu_read_unlock();
  2644. kmem_free(ilist);
  2645. out_put:
  2646. xfs_perag_put(pag);
  2647. return 0;
  2648. cluster_corrupt_out:
  2649. /*
  2650. * Corruption detected in the clustering loop. Invalidate the
  2651. * inode buffer and shut down the filesystem.
  2652. */
  2653. rcu_read_unlock();
  2654. /*
  2655. * Clean up the buffer. If it was delwri, just release it --
  2656. * brelse can handle it with no problems. If not, shut down the
  2657. * filesystem before releasing the buffer.
  2658. */
  2659. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2660. if (bufwasdelwri)
  2661. xfs_buf_relse(bp);
  2662. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2663. if (!bufwasdelwri) {
  2664. /*
  2665. * Just like incore_relse: if we have b_iodone functions,
  2666. * mark the buffer as an error and call them. Otherwise
  2667. * mark it as stale and brelse.
  2668. */
  2669. if (bp->b_iodone) {
  2670. XFS_BUF_UNDONE(bp);
  2671. xfs_buf_stale(bp);
  2672. xfs_buf_ioerror(bp, EIO);
  2673. xfs_buf_ioend(bp, 0);
  2674. } else {
  2675. xfs_buf_stale(bp);
  2676. xfs_buf_relse(bp);
  2677. }
  2678. }
  2679. /*
  2680. * Unlocks the flush lock
  2681. */
  2682. xfs_iflush_abort(iq, false);
  2683. kmem_free(ilist);
  2684. xfs_perag_put(pag);
  2685. return XFS_ERROR(EFSCORRUPTED);
  2686. }
  2687. /*
  2688. * Flush dirty inode metadata into the backing buffer.
  2689. *
  2690. * The caller must have the inode lock and the inode flush lock held. The
  2691. * inode lock will still be held upon return to the caller, and the inode
  2692. * flush lock will be released after the inode has reached the disk.
  2693. *
  2694. * The caller must write out the buffer returned in *bpp and release it.
  2695. */
  2696. int
  2697. xfs_iflush(
  2698. struct xfs_inode *ip,
  2699. struct xfs_buf **bpp)
  2700. {
  2701. struct xfs_mount *mp = ip->i_mount;
  2702. struct xfs_buf *bp;
  2703. struct xfs_dinode *dip;
  2704. int error;
  2705. XFS_STATS_INC(xs_iflush_count);
  2706. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2707. ASSERT(xfs_isiflocked(ip));
  2708. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2709. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2710. *bpp = NULL;
  2711. xfs_iunpin_wait(ip);
  2712. /*
  2713. * For stale inodes we cannot rely on the backing buffer remaining
  2714. * stale in cache for the remaining life of the stale inode and so
  2715. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2716. * inodes below. We have to check this after ensuring the inode is
  2717. * unpinned so that it is safe to reclaim the stale inode after the
  2718. * flush call.
  2719. */
  2720. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2721. xfs_ifunlock(ip);
  2722. return 0;
  2723. }
  2724. /*
  2725. * This may have been unpinned because the filesystem is shutting
  2726. * down forcibly. If that's the case we must not write this inode
  2727. * to disk, because the log record didn't make it to disk.
  2728. *
  2729. * We also have to remove the log item from the AIL in this case,
  2730. * as we wait for an empty AIL as part of the unmount process.
  2731. */
  2732. if (XFS_FORCED_SHUTDOWN(mp)) {
  2733. error = XFS_ERROR(EIO);
  2734. goto abort_out;
  2735. }
  2736. /*
  2737. * Get the buffer containing the on-disk inode.
  2738. */
  2739. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2740. 0);
  2741. if (error || !bp) {
  2742. xfs_ifunlock(ip);
  2743. return error;
  2744. }
  2745. /*
  2746. * First flush out the inode that xfs_iflush was called with.
  2747. */
  2748. error = xfs_iflush_int(ip, bp);
  2749. if (error)
  2750. goto corrupt_out;
  2751. /*
  2752. * If the buffer is pinned then push on the log now so we won't
  2753. * get stuck waiting in the write for too long.
  2754. */
  2755. if (xfs_buf_ispinned(bp))
  2756. xfs_log_force(mp, 0);
  2757. /*
  2758. * inode clustering:
  2759. * see if other inodes can be gathered into this write
  2760. */
  2761. error = xfs_iflush_cluster(ip, bp);
  2762. if (error)
  2763. goto cluster_corrupt_out;
  2764. *bpp = bp;
  2765. return 0;
  2766. corrupt_out:
  2767. xfs_buf_relse(bp);
  2768. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2769. cluster_corrupt_out:
  2770. error = XFS_ERROR(EFSCORRUPTED);
  2771. abort_out:
  2772. /*
  2773. * Unlocks the flush lock
  2774. */
  2775. xfs_iflush_abort(ip, false);
  2776. return error;
  2777. }
  2778. STATIC int
  2779. xfs_iflush_int(
  2780. struct xfs_inode *ip,
  2781. struct xfs_buf *bp)
  2782. {
  2783. struct xfs_inode_log_item *iip = ip->i_itemp;
  2784. struct xfs_dinode *dip;
  2785. struct xfs_mount *mp = ip->i_mount;
  2786. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2787. ASSERT(xfs_isiflocked(ip));
  2788. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2789. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2790. ASSERT(iip != NULL && iip->ili_fields != 0);
  2791. /* set *dip = inode's place in the buffer */
  2792. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2793. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2794. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2795. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2796. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2797. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2798. goto corrupt_out;
  2799. }
  2800. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2801. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2802. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2803. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2804. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2805. goto corrupt_out;
  2806. }
  2807. if (S_ISREG(ip->i_d.di_mode)) {
  2808. if (XFS_TEST_ERROR(
  2809. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2810. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2811. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2812. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2813. "%s: Bad regular inode %Lu, ptr 0x%p",
  2814. __func__, ip->i_ino, ip);
  2815. goto corrupt_out;
  2816. }
  2817. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2818. if (XFS_TEST_ERROR(
  2819. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2820. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2821. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2822. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2823. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2824. "%s: Bad directory inode %Lu, ptr 0x%p",
  2825. __func__, ip->i_ino, ip);
  2826. goto corrupt_out;
  2827. }
  2828. }
  2829. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2830. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2831. XFS_RANDOM_IFLUSH_5)) {
  2832. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2833. "%s: detected corrupt incore inode %Lu, "
  2834. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2835. __func__, ip->i_ino,
  2836. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2837. ip->i_d.di_nblocks, ip);
  2838. goto corrupt_out;
  2839. }
  2840. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2841. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2842. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2843. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2844. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2845. goto corrupt_out;
  2846. }
  2847. /*
  2848. * Inode item log recovery for v1/v2 inodes are dependent on the
  2849. * di_flushiter count for correct sequencing. We bump the flush
  2850. * iteration count so we can detect flushes which postdate a log record
  2851. * during recovery. This is redundant as we now log every change and
  2852. * hence this can't happen but we need to still do it to ensure
  2853. * backwards compatibility with old kernels that predate logging all
  2854. * inode changes.
  2855. */
  2856. if (ip->i_d.di_version < 3)
  2857. ip->i_d.di_flushiter++;
  2858. /*
  2859. * Copy the dirty parts of the inode into the on-disk
  2860. * inode. We always copy out the core of the inode,
  2861. * because if the inode is dirty at all the core must
  2862. * be.
  2863. */
  2864. xfs_dinode_to_disk(dip, &ip->i_d);
  2865. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2866. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2867. ip->i_d.di_flushiter = 0;
  2868. /*
  2869. * If this is really an old format inode and the superblock version
  2870. * has not been updated to support only new format inodes, then
  2871. * convert back to the old inode format. If the superblock version
  2872. * has been updated, then make the conversion permanent.
  2873. */
  2874. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2875. if (ip->i_d.di_version == 1) {
  2876. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2877. /*
  2878. * Convert it back.
  2879. */
  2880. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2881. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2882. } else {
  2883. /*
  2884. * The superblock version has already been bumped,
  2885. * so just make the conversion to the new inode
  2886. * format permanent.
  2887. */
  2888. ip->i_d.di_version = 2;
  2889. dip->di_version = 2;
  2890. ip->i_d.di_onlink = 0;
  2891. dip->di_onlink = 0;
  2892. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2893. memset(&(dip->di_pad[0]), 0,
  2894. sizeof(dip->di_pad));
  2895. ASSERT(xfs_get_projid(ip) == 0);
  2896. }
  2897. }
  2898. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2899. if (XFS_IFORK_Q(ip))
  2900. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2901. xfs_inobp_check(mp, bp);
  2902. /*
  2903. * We've recorded everything logged in the inode, so we'd like to clear
  2904. * the ili_fields bits so we don't log and flush things unnecessarily.
  2905. * However, we can't stop logging all this information until the data
  2906. * we've copied into the disk buffer is written to disk. If we did we
  2907. * might overwrite the copy of the inode in the log with all the data
  2908. * after re-logging only part of it, and in the face of a crash we
  2909. * wouldn't have all the data we need to recover.
  2910. *
  2911. * What we do is move the bits to the ili_last_fields field. When
  2912. * logging the inode, these bits are moved back to the ili_fields field.
  2913. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2914. * know that the information those bits represent is permanently on
  2915. * disk. As long as the flush completes before the inode is logged
  2916. * again, then both ili_fields and ili_last_fields will be cleared.
  2917. *
  2918. * We can play with the ili_fields bits here, because the inode lock
  2919. * must be held exclusively in order to set bits there and the flush
  2920. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2921. * done routine can tell whether or not to look in the AIL. Also, store
  2922. * the current LSN of the inode so that we can tell whether the item has
  2923. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2924. * need the AIL lock, because it is a 64 bit value that cannot be read
  2925. * atomically.
  2926. */
  2927. iip->ili_last_fields = iip->ili_fields;
  2928. iip->ili_fields = 0;
  2929. iip->ili_logged = 1;
  2930. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2931. &iip->ili_item.li_lsn);
  2932. /*
  2933. * Attach the function xfs_iflush_done to the inode's
  2934. * buffer. This will remove the inode from the AIL
  2935. * and unlock the inode's flush lock when the inode is
  2936. * completely written to disk.
  2937. */
  2938. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2939. /* update the lsn in the on disk inode if required */
  2940. if (ip->i_d.di_version == 3)
  2941. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  2942. /* generate the checksum. */
  2943. xfs_dinode_calc_crc(mp, dip);
  2944. ASSERT(bp->b_fspriv != NULL);
  2945. ASSERT(bp->b_iodone != NULL);
  2946. return 0;
  2947. corrupt_out:
  2948. return XFS_ERROR(EFSCORRUPTED);
  2949. }