xfs_inode.c 97 KB

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