xfs_inode.c 87 KB

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