xfs_inode.c 100 KB

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