xfs_inode.c 97 KB

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