xfs_inode.c 97 KB

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