xfs_inode.c 88 KB

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