xfs_inode.c 97 KB

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