xfs_inode.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_trans_priv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_inode_item.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_error.h"
  42. #include "xfs_utils.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_filestream.h"
  45. #include "xfs_vnodeops.h"
  46. #include "xfs_cksum.h"
  47. #include "xfs_trace.h"
  48. #include "xfs_icache.h"
  49. kmem_zone_t *xfs_ifork_zone;
  50. kmem_zone_t *xfs_inode_zone;
  51. /*
  52. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  53. * freed from a file in a single transaction.
  54. */
  55. #define XFS_ITRUNC_MAX_EXTENTS 2
  56. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  57. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  58. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  59. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  60. /*
  61. * helper function to extract extent size hint from inode
  62. */
  63. xfs_extlen_t
  64. xfs_get_extsz_hint(
  65. struct xfs_inode *ip)
  66. {
  67. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  68. return ip->i_d.di_extsize;
  69. if (XFS_IS_REALTIME_INODE(ip))
  70. return ip->i_mount->m_sb.sb_rextsize;
  71. return 0;
  72. }
  73. /*
  74. * This is a wrapper routine around the xfs_ilock() routine used to centralize
  75. * some grungy code. It is used in places that wish to lock the inode solely
  76. * for reading the extents. The reason these places can't just call
  77. * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
  78. * extents from disk for a file in b-tree format. If the inode is in b-tree
  79. * format, then we need to lock the inode exclusively until the extents are read
  80. * in. Locking it exclusively all the time would limit our parallelism
  81. * unnecessarily, though. What we do instead is check to see if the extents
  82. * have been read in yet, and only lock the inode exclusively if they have not.
  83. *
  84. * The function returns a value which should be given to the corresponding
  85. * xfs_iunlock_map_shared(). This value is the mode in which the lock was
  86. * actually taken.
  87. */
  88. uint
  89. xfs_ilock_map_shared(
  90. xfs_inode_t *ip)
  91. {
  92. uint lock_mode;
  93. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  94. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  95. lock_mode = XFS_ILOCK_EXCL;
  96. } else {
  97. lock_mode = XFS_ILOCK_SHARED;
  98. }
  99. xfs_ilock(ip, lock_mode);
  100. return lock_mode;
  101. }
  102. /*
  103. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  104. * All it does is call xfs_iunlock() with the given lock_mode.
  105. */
  106. void
  107. xfs_iunlock_map_shared(
  108. xfs_inode_t *ip,
  109. unsigned int lock_mode)
  110. {
  111. xfs_iunlock(ip, lock_mode);
  112. }
  113. /*
  114. * The xfs inode contains 2 locks: a multi-reader lock called the
  115. * i_iolock and a multi-reader lock called the i_lock. This routine
  116. * allows either or both of the locks to be obtained.
  117. *
  118. * The 2 locks should always be ordered so that the IO lock is
  119. * obtained first in order to prevent deadlock.
  120. *
  121. * ip -- the inode being locked
  122. * lock_flags -- this parameter indicates the inode's locks
  123. * to be locked. It can be:
  124. * XFS_IOLOCK_SHARED,
  125. * XFS_IOLOCK_EXCL,
  126. * XFS_ILOCK_SHARED,
  127. * XFS_ILOCK_EXCL,
  128. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  129. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  130. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  131. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  132. */
  133. void
  134. xfs_ilock(
  135. xfs_inode_t *ip,
  136. uint lock_flags)
  137. {
  138. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  139. /*
  140. * You can't set both SHARED and EXCL for the same lock,
  141. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  142. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  143. */
  144. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  145. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  146. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  147. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  148. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  149. if (lock_flags & XFS_IOLOCK_EXCL)
  150. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  151. else if (lock_flags & XFS_IOLOCK_SHARED)
  152. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  153. if (lock_flags & XFS_ILOCK_EXCL)
  154. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  155. else if (lock_flags & XFS_ILOCK_SHARED)
  156. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  157. }
  158. /*
  159. * This is just like xfs_ilock(), except that the caller
  160. * is guaranteed not to sleep. It returns 1 if it gets
  161. * the requested locks and 0 otherwise. If the IO lock is
  162. * obtained but the inode lock cannot be, then the IO lock
  163. * is dropped before returning.
  164. *
  165. * ip -- the inode being locked
  166. * lock_flags -- this parameter indicates the inode's locks to be
  167. * to be locked. See the comment for xfs_ilock() for a list
  168. * of valid values.
  169. */
  170. int
  171. xfs_ilock_nowait(
  172. xfs_inode_t *ip,
  173. uint lock_flags)
  174. {
  175. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  176. /*
  177. * You can't set both SHARED and EXCL for the same lock,
  178. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  179. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  180. */
  181. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  182. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  183. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  184. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  185. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  186. if (lock_flags & XFS_IOLOCK_EXCL) {
  187. if (!mrtryupdate(&ip->i_iolock))
  188. goto out;
  189. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  190. if (!mrtryaccess(&ip->i_iolock))
  191. goto out;
  192. }
  193. if (lock_flags & XFS_ILOCK_EXCL) {
  194. if (!mrtryupdate(&ip->i_lock))
  195. goto out_undo_iolock;
  196. } else if (lock_flags & XFS_ILOCK_SHARED) {
  197. if (!mrtryaccess(&ip->i_lock))
  198. goto out_undo_iolock;
  199. }
  200. return 1;
  201. out_undo_iolock:
  202. if (lock_flags & XFS_IOLOCK_EXCL)
  203. mrunlock_excl(&ip->i_iolock);
  204. else if (lock_flags & XFS_IOLOCK_SHARED)
  205. mrunlock_shared(&ip->i_iolock);
  206. out:
  207. return 0;
  208. }
  209. /*
  210. * xfs_iunlock() is used to drop the inode locks acquired with
  211. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  212. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  213. * that we know which locks to drop.
  214. *
  215. * ip -- the inode being unlocked
  216. * lock_flags -- this parameter indicates the inode's locks to be
  217. * to be unlocked. See the comment for xfs_ilock() for a list
  218. * of valid values for this parameter.
  219. *
  220. */
  221. void
  222. xfs_iunlock(
  223. xfs_inode_t *ip,
  224. uint lock_flags)
  225. {
  226. /*
  227. * You can't set both SHARED and EXCL for the same lock,
  228. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  229. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  230. */
  231. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  232. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  233. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  234. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  235. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  236. ASSERT(lock_flags != 0);
  237. if (lock_flags & XFS_IOLOCK_EXCL)
  238. mrunlock_excl(&ip->i_iolock);
  239. else if (lock_flags & XFS_IOLOCK_SHARED)
  240. mrunlock_shared(&ip->i_iolock);
  241. if (lock_flags & XFS_ILOCK_EXCL)
  242. mrunlock_excl(&ip->i_lock);
  243. else if (lock_flags & XFS_ILOCK_SHARED)
  244. mrunlock_shared(&ip->i_lock);
  245. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  246. }
  247. /*
  248. * give up write locks. the i/o lock cannot be held nested
  249. * if it is being demoted.
  250. */
  251. void
  252. xfs_ilock_demote(
  253. xfs_inode_t *ip,
  254. uint lock_flags)
  255. {
  256. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  257. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  258. if (lock_flags & XFS_ILOCK_EXCL)
  259. mrdemote(&ip->i_lock);
  260. if (lock_flags & XFS_IOLOCK_EXCL)
  261. mrdemote(&ip->i_iolock);
  262. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  263. }
  264. #if defined(DEBUG) || defined(XFS_WARN)
  265. int
  266. xfs_isilocked(
  267. xfs_inode_t *ip,
  268. uint lock_flags)
  269. {
  270. if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
  271. if (!(lock_flags & XFS_ILOCK_SHARED))
  272. return !!ip->i_lock.mr_writer;
  273. return rwsem_is_locked(&ip->i_lock.mr_lock);
  274. }
  275. if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
  276. if (!(lock_flags & XFS_IOLOCK_SHARED))
  277. return !!ip->i_iolock.mr_writer;
  278. return rwsem_is_locked(&ip->i_iolock.mr_lock);
  279. }
  280. ASSERT(0);
  281. return 0;
  282. }
  283. #endif
  284. void
  285. __xfs_iflock(
  286. struct xfs_inode *ip)
  287. {
  288. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
  289. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
  290. do {
  291. prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  292. if (xfs_isiflocked(ip))
  293. io_schedule();
  294. } while (!xfs_iflock_nowait(ip));
  295. finish_wait(wq, &wait.wait);
  296. }
  297. #ifdef DEBUG
  298. /*
  299. * Make sure that the extents in the given memory buffer
  300. * are valid.
  301. */
  302. STATIC void
  303. xfs_validate_extents(
  304. xfs_ifork_t *ifp,
  305. int nrecs,
  306. xfs_exntfmt_t fmt)
  307. {
  308. xfs_bmbt_irec_t irec;
  309. xfs_bmbt_rec_host_t rec;
  310. int i;
  311. for (i = 0; i < nrecs; i++) {
  312. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  313. rec.l0 = get_unaligned(&ep->l0);
  314. rec.l1 = get_unaligned(&ep->l1);
  315. xfs_bmbt_get_all(&rec, &irec);
  316. if (fmt == XFS_EXTFMT_NOSTATE)
  317. ASSERT(irec.br_state == XFS_EXT_NORM);
  318. }
  319. }
  320. #else /* DEBUG */
  321. #define xfs_validate_extents(ifp, nrecs, fmt)
  322. #endif /* DEBUG */
  323. /*
  324. * Check that none of the inode's in the buffer have a next
  325. * unlinked field of 0.
  326. */
  327. #if defined(DEBUG)
  328. void
  329. xfs_inobp_check(
  330. xfs_mount_t *mp,
  331. xfs_buf_t *bp)
  332. {
  333. int i;
  334. int j;
  335. xfs_dinode_t *dip;
  336. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  337. for (i = 0; i < j; i++) {
  338. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  339. i * mp->m_sb.sb_inodesize);
  340. if (!dip->di_next_unlinked) {
  341. xfs_alert(mp,
  342. "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
  343. bp);
  344. ASSERT(dip->di_next_unlinked);
  345. }
  346. }
  347. }
  348. #endif
  349. static void
  350. xfs_inode_buf_verify(
  351. struct xfs_buf *bp)
  352. {
  353. struct xfs_mount *mp = bp->b_target->bt_mount;
  354. int i;
  355. int ni;
  356. /*
  357. * Validate the magic number and version of every inode in the buffer
  358. */
  359. ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
  360. for (i = 0; i < ni; i++) {
  361. int di_ok;
  362. xfs_dinode_t *dip;
  363. dip = (struct xfs_dinode *)xfs_buf_offset(bp,
  364. (i << mp->m_sb.sb_inodelog));
  365. di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
  366. XFS_DINODE_GOOD_VERSION(dip->di_version);
  367. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  368. XFS_ERRTAG_ITOBP_INOTOBP,
  369. XFS_RANDOM_ITOBP_INOTOBP))) {
  370. xfs_buf_ioerror(bp, EFSCORRUPTED);
  371. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
  372. mp, dip);
  373. #ifdef DEBUG
  374. xfs_emerg(mp,
  375. "bad inode magic/vsn daddr %lld #%d (magic=%x)",
  376. (unsigned long long)bp->b_bn, i,
  377. be16_to_cpu(dip->di_magic));
  378. ASSERT(0);
  379. #endif
  380. }
  381. }
  382. xfs_inobp_check(mp, bp);
  383. }
  384. static void
  385. xfs_inode_buf_read_verify(
  386. struct xfs_buf *bp)
  387. {
  388. xfs_inode_buf_verify(bp);
  389. }
  390. static void
  391. xfs_inode_buf_write_verify(
  392. struct xfs_buf *bp)
  393. {
  394. xfs_inode_buf_verify(bp);
  395. }
  396. const struct xfs_buf_ops xfs_inode_buf_ops = {
  397. .verify_read = xfs_inode_buf_read_verify,
  398. .verify_write = xfs_inode_buf_write_verify,
  399. };
  400. /*
  401. * This routine is called to map an inode to the buffer containing the on-disk
  402. * version of the inode. It returns a pointer to the buffer containing the
  403. * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
  404. * pointer to the on-disk inode within that buffer.
  405. *
  406. * If a non-zero error is returned, then the contents of bpp and dipp are
  407. * undefined.
  408. */
  409. int
  410. xfs_imap_to_bp(
  411. struct xfs_mount *mp,
  412. struct xfs_trans *tp,
  413. struct xfs_imap *imap,
  414. struct xfs_dinode **dipp,
  415. struct xfs_buf **bpp,
  416. uint buf_flags,
  417. uint iget_flags)
  418. {
  419. struct xfs_buf *bp;
  420. int error;
  421. buf_flags |= XBF_UNMAPPED;
  422. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  423. (int)imap->im_len, buf_flags, &bp,
  424. &xfs_inode_buf_ops);
  425. if (error) {
  426. if (error == EAGAIN) {
  427. ASSERT(buf_flags & XBF_TRYLOCK);
  428. return error;
  429. }
  430. if (error == EFSCORRUPTED &&
  431. (iget_flags & XFS_IGET_UNTRUSTED))
  432. return XFS_ERROR(EINVAL);
  433. xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
  434. __func__, error);
  435. return error;
  436. }
  437. *bpp = bp;
  438. *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
  439. return 0;
  440. }
  441. /*
  442. * Move inode type and inode format specific information from the
  443. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  444. * this means set if_rdev to the proper value. For files, directories,
  445. * and symlinks this means to bring in the in-line data or extent
  446. * pointers. For a file in B-tree format, only the root is immediately
  447. * brought in-core. The rest will be in-lined in if_extents when it
  448. * is first referenced (see xfs_iread_extents()).
  449. */
  450. STATIC int
  451. xfs_iformat(
  452. xfs_inode_t *ip,
  453. xfs_dinode_t *dip)
  454. {
  455. xfs_attr_shortform_t *atp;
  456. int size;
  457. int error = 0;
  458. xfs_fsize_t di_size;
  459. if (unlikely(be32_to_cpu(dip->di_nextents) +
  460. be16_to_cpu(dip->di_anextents) >
  461. be64_to_cpu(dip->di_nblocks))) {
  462. xfs_warn(ip->i_mount,
  463. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  464. (unsigned long long)ip->i_ino,
  465. (int)(be32_to_cpu(dip->di_nextents) +
  466. be16_to_cpu(dip->di_anextents)),
  467. (unsigned long long)
  468. be64_to_cpu(dip->di_nblocks));
  469. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  470. ip->i_mount, dip);
  471. return XFS_ERROR(EFSCORRUPTED);
  472. }
  473. if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  474. xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
  475. (unsigned long long)ip->i_ino,
  476. dip->di_forkoff);
  477. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  478. ip->i_mount, dip);
  479. return XFS_ERROR(EFSCORRUPTED);
  480. }
  481. if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
  482. !ip->i_mount->m_rtdev_targp)) {
  483. xfs_warn(ip->i_mount,
  484. "corrupt dinode %Lu, has realtime flag set.",
  485. ip->i_ino);
  486. XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
  487. XFS_ERRLEVEL_LOW, ip->i_mount, dip);
  488. return XFS_ERROR(EFSCORRUPTED);
  489. }
  490. switch (ip->i_d.di_mode & S_IFMT) {
  491. case S_IFIFO:
  492. case S_IFCHR:
  493. case S_IFBLK:
  494. case S_IFSOCK:
  495. if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
  496. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  497. ip->i_mount, dip);
  498. return XFS_ERROR(EFSCORRUPTED);
  499. }
  500. ip->i_d.di_size = 0;
  501. ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
  502. break;
  503. case S_IFREG:
  504. case S_IFLNK:
  505. case S_IFDIR:
  506. switch (dip->di_format) {
  507. case XFS_DINODE_FMT_LOCAL:
  508. /*
  509. * no local regular files yet
  510. */
  511. if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
  512. xfs_warn(ip->i_mount,
  513. "corrupt inode %Lu (local format for regular file).",
  514. (unsigned long long) ip->i_ino);
  515. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  516. XFS_ERRLEVEL_LOW,
  517. ip->i_mount, dip);
  518. return XFS_ERROR(EFSCORRUPTED);
  519. }
  520. di_size = be64_to_cpu(dip->di_size);
  521. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  522. xfs_warn(ip->i_mount,
  523. "corrupt inode %Lu (bad size %Ld for local inode).",
  524. (unsigned long long) ip->i_ino,
  525. (long long) di_size);
  526. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  527. XFS_ERRLEVEL_LOW,
  528. ip->i_mount, dip);
  529. return XFS_ERROR(EFSCORRUPTED);
  530. }
  531. size = (int)di_size;
  532. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  533. break;
  534. case XFS_DINODE_FMT_EXTENTS:
  535. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  536. break;
  537. case XFS_DINODE_FMT_BTREE:
  538. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  539. break;
  540. default:
  541. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  542. ip->i_mount);
  543. return XFS_ERROR(EFSCORRUPTED);
  544. }
  545. break;
  546. default:
  547. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  548. return XFS_ERROR(EFSCORRUPTED);
  549. }
  550. if (error) {
  551. return error;
  552. }
  553. if (!XFS_DFORK_Q(dip))
  554. return 0;
  555. ASSERT(ip->i_afp == NULL);
  556. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
  557. switch (dip->di_aformat) {
  558. case XFS_DINODE_FMT_LOCAL:
  559. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  560. size = be16_to_cpu(atp->hdr.totsize);
  561. if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
  562. xfs_warn(ip->i_mount,
  563. "corrupt inode %Lu (bad attr fork size %Ld).",
  564. (unsigned long long) ip->i_ino,
  565. (long long) size);
  566. XFS_CORRUPTION_ERROR("xfs_iformat(8)",
  567. XFS_ERRLEVEL_LOW,
  568. ip->i_mount, dip);
  569. return XFS_ERROR(EFSCORRUPTED);
  570. }
  571. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  572. break;
  573. case XFS_DINODE_FMT_EXTENTS:
  574. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  575. break;
  576. case XFS_DINODE_FMT_BTREE:
  577. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  578. break;
  579. default:
  580. error = XFS_ERROR(EFSCORRUPTED);
  581. break;
  582. }
  583. if (error) {
  584. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  585. ip->i_afp = NULL;
  586. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  587. }
  588. return error;
  589. }
  590. /*
  591. * The file is in-lined in the on-disk inode.
  592. * If it fits into if_inline_data, then copy
  593. * it there, otherwise allocate a buffer for it
  594. * and copy the data there. Either way, set
  595. * if_data to point at the data.
  596. * If we allocate a buffer for the data, make
  597. * sure that its size is a multiple of 4 and
  598. * record the real size in i_real_bytes.
  599. */
  600. STATIC int
  601. xfs_iformat_local(
  602. xfs_inode_t *ip,
  603. xfs_dinode_t *dip,
  604. int whichfork,
  605. int size)
  606. {
  607. xfs_ifork_t *ifp;
  608. int real_size;
  609. /*
  610. * If the size is unreasonable, then something
  611. * is wrong and we just bail out rather than crash in
  612. * kmem_alloc() or memcpy() below.
  613. */
  614. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  615. xfs_warn(ip->i_mount,
  616. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  617. (unsigned long long) ip->i_ino, size,
  618. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  619. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  620. ip->i_mount, dip);
  621. return XFS_ERROR(EFSCORRUPTED);
  622. }
  623. ifp = XFS_IFORK_PTR(ip, whichfork);
  624. real_size = 0;
  625. if (size == 0)
  626. ifp->if_u1.if_data = NULL;
  627. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  628. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  629. else {
  630. real_size = roundup(size, 4);
  631. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
  632. }
  633. ifp->if_bytes = size;
  634. ifp->if_real_bytes = real_size;
  635. if (size)
  636. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  637. ifp->if_flags &= ~XFS_IFEXTENTS;
  638. ifp->if_flags |= XFS_IFINLINE;
  639. return 0;
  640. }
  641. /*
  642. * The file consists of a set of extents all
  643. * of which fit into the on-disk inode.
  644. * If there are few enough extents to fit into
  645. * the if_inline_ext, then copy them there.
  646. * Otherwise allocate a buffer for them and copy
  647. * them into it. Either way, set if_extents
  648. * to point at the extents.
  649. */
  650. STATIC int
  651. xfs_iformat_extents(
  652. xfs_inode_t *ip,
  653. xfs_dinode_t *dip,
  654. int whichfork)
  655. {
  656. xfs_bmbt_rec_t *dp;
  657. xfs_ifork_t *ifp;
  658. int nex;
  659. int size;
  660. int i;
  661. ifp = XFS_IFORK_PTR(ip, whichfork);
  662. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  663. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  664. /*
  665. * If the number of extents is unreasonable, then something
  666. * is wrong and we just bail out rather than crash in
  667. * kmem_alloc() or memcpy() below.
  668. */
  669. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  670. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  671. (unsigned long long) ip->i_ino, nex);
  672. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  673. ip->i_mount, dip);
  674. return XFS_ERROR(EFSCORRUPTED);
  675. }
  676. ifp->if_real_bytes = 0;
  677. if (nex == 0)
  678. ifp->if_u1.if_extents = NULL;
  679. else if (nex <= XFS_INLINE_EXTS)
  680. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  681. else
  682. xfs_iext_add(ifp, 0, nex);
  683. ifp->if_bytes = size;
  684. if (size) {
  685. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  686. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  687. for (i = 0; i < nex; i++, dp++) {
  688. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  689. ep->l0 = get_unaligned_be64(&dp->l0);
  690. ep->l1 = get_unaligned_be64(&dp->l1);
  691. }
  692. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  693. if (whichfork != XFS_DATA_FORK ||
  694. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  695. if (unlikely(xfs_check_nostate_extents(
  696. ifp, 0, nex))) {
  697. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  698. XFS_ERRLEVEL_LOW,
  699. ip->i_mount);
  700. return XFS_ERROR(EFSCORRUPTED);
  701. }
  702. }
  703. ifp->if_flags |= XFS_IFEXTENTS;
  704. return 0;
  705. }
  706. /*
  707. * The file has too many extents to fit into
  708. * the inode, so they are in B-tree format.
  709. * Allocate a buffer for the root of the B-tree
  710. * and copy the root into it. The i_extents
  711. * field will remain NULL until all of the
  712. * extents are read in (when they are needed).
  713. */
  714. STATIC int
  715. xfs_iformat_btree(
  716. xfs_inode_t *ip,
  717. xfs_dinode_t *dip,
  718. int whichfork)
  719. {
  720. struct xfs_mount *mp = ip->i_mount;
  721. xfs_bmdr_block_t *dfp;
  722. xfs_ifork_t *ifp;
  723. /* REFERENCED */
  724. int nrecs;
  725. int size;
  726. ifp = XFS_IFORK_PTR(ip, whichfork);
  727. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  728. size = XFS_BMAP_BROOT_SPACE(mp, dfp);
  729. nrecs = be16_to_cpu(dfp->bb_numrecs);
  730. /*
  731. * blow out if -- fork has less extents than can fit in
  732. * fork (fork shouldn't be a btree format), root btree
  733. * block has more records than can fit into the fork,
  734. * or the number of extents is greater than the number of
  735. * blocks.
  736. */
  737. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  738. XFS_IFORK_MAXEXT(ip, whichfork) ||
  739. XFS_BMDR_SPACE_CALC(nrecs) >
  740. XFS_DFORK_SIZE(dip, mp, whichfork) ||
  741. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  742. xfs_warn(mp, "corrupt inode %Lu (btree).",
  743. (unsigned long long) ip->i_ino);
  744. XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  745. mp, dip);
  746. return XFS_ERROR(EFSCORRUPTED);
  747. }
  748. ifp->if_broot_bytes = size;
  749. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  750. ASSERT(ifp->if_broot != NULL);
  751. /*
  752. * Copy and convert from the on-disk structure
  753. * to the in-memory structure.
  754. */
  755. xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  756. ifp->if_broot, size);
  757. ifp->if_flags &= ~XFS_IFEXTENTS;
  758. ifp->if_flags |= XFS_IFBROOT;
  759. return 0;
  760. }
  761. STATIC void
  762. xfs_dinode_from_disk(
  763. xfs_icdinode_t *to,
  764. xfs_dinode_t *from)
  765. {
  766. to->di_magic = be16_to_cpu(from->di_magic);
  767. to->di_mode = be16_to_cpu(from->di_mode);
  768. to->di_version = from ->di_version;
  769. to->di_format = from->di_format;
  770. to->di_onlink = be16_to_cpu(from->di_onlink);
  771. to->di_uid = be32_to_cpu(from->di_uid);
  772. to->di_gid = be32_to_cpu(from->di_gid);
  773. to->di_nlink = be32_to_cpu(from->di_nlink);
  774. to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
  775. to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
  776. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  777. to->di_flushiter = be16_to_cpu(from->di_flushiter);
  778. to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
  779. to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
  780. to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
  781. to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
  782. to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
  783. to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
  784. to->di_size = be64_to_cpu(from->di_size);
  785. to->di_nblocks = be64_to_cpu(from->di_nblocks);
  786. to->di_extsize = be32_to_cpu(from->di_extsize);
  787. to->di_nextents = be32_to_cpu(from->di_nextents);
  788. to->di_anextents = be16_to_cpu(from->di_anextents);
  789. to->di_forkoff = from->di_forkoff;
  790. to->di_aformat = from->di_aformat;
  791. to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
  792. to->di_dmstate = be16_to_cpu(from->di_dmstate);
  793. to->di_flags = be16_to_cpu(from->di_flags);
  794. to->di_gen = be32_to_cpu(from->di_gen);
  795. if (to->di_version == 3) {
  796. to->di_changecount = be64_to_cpu(from->di_changecount);
  797. to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
  798. to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
  799. to->di_flags2 = be64_to_cpu(from->di_flags2);
  800. to->di_ino = be64_to_cpu(from->di_ino);
  801. to->di_lsn = be64_to_cpu(from->di_lsn);
  802. memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
  803. uuid_copy(&to->di_uuid, &from->di_uuid);
  804. }
  805. }
  806. void
  807. xfs_dinode_to_disk(
  808. xfs_dinode_t *to,
  809. xfs_icdinode_t *from)
  810. {
  811. to->di_magic = cpu_to_be16(from->di_magic);
  812. to->di_mode = cpu_to_be16(from->di_mode);
  813. to->di_version = from ->di_version;
  814. to->di_format = from->di_format;
  815. to->di_onlink = cpu_to_be16(from->di_onlink);
  816. to->di_uid = cpu_to_be32(from->di_uid);
  817. to->di_gid = cpu_to_be32(from->di_gid);
  818. to->di_nlink = cpu_to_be32(from->di_nlink);
  819. to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
  820. to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
  821. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  822. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  823. to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
  824. to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
  825. to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
  826. to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
  827. to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
  828. to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
  829. to->di_size = cpu_to_be64(from->di_size);
  830. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  831. to->di_extsize = cpu_to_be32(from->di_extsize);
  832. to->di_nextents = cpu_to_be32(from->di_nextents);
  833. to->di_anextents = cpu_to_be16(from->di_anextents);
  834. to->di_forkoff = from->di_forkoff;
  835. to->di_aformat = from->di_aformat;
  836. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  837. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  838. to->di_flags = cpu_to_be16(from->di_flags);
  839. to->di_gen = cpu_to_be32(from->di_gen);
  840. if (from->di_version == 3) {
  841. to->di_changecount = cpu_to_be64(from->di_changecount);
  842. to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
  843. to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
  844. to->di_flags2 = cpu_to_be64(from->di_flags2);
  845. to->di_ino = cpu_to_be64(from->di_ino);
  846. to->di_lsn = cpu_to_be64(from->di_lsn);
  847. memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
  848. uuid_copy(&to->di_uuid, &from->di_uuid);
  849. }
  850. }
  851. STATIC uint
  852. _xfs_dic2xflags(
  853. __uint16_t di_flags)
  854. {
  855. uint flags = 0;
  856. if (di_flags & XFS_DIFLAG_ANY) {
  857. if (di_flags & XFS_DIFLAG_REALTIME)
  858. flags |= XFS_XFLAG_REALTIME;
  859. if (di_flags & XFS_DIFLAG_PREALLOC)
  860. flags |= XFS_XFLAG_PREALLOC;
  861. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  862. flags |= XFS_XFLAG_IMMUTABLE;
  863. if (di_flags & XFS_DIFLAG_APPEND)
  864. flags |= XFS_XFLAG_APPEND;
  865. if (di_flags & XFS_DIFLAG_SYNC)
  866. flags |= XFS_XFLAG_SYNC;
  867. if (di_flags & XFS_DIFLAG_NOATIME)
  868. flags |= XFS_XFLAG_NOATIME;
  869. if (di_flags & XFS_DIFLAG_NODUMP)
  870. flags |= XFS_XFLAG_NODUMP;
  871. if (di_flags & XFS_DIFLAG_RTINHERIT)
  872. flags |= XFS_XFLAG_RTINHERIT;
  873. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  874. flags |= XFS_XFLAG_PROJINHERIT;
  875. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  876. flags |= XFS_XFLAG_NOSYMLINKS;
  877. if (di_flags & XFS_DIFLAG_EXTSIZE)
  878. flags |= XFS_XFLAG_EXTSIZE;
  879. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  880. flags |= XFS_XFLAG_EXTSZINHERIT;
  881. if (di_flags & XFS_DIFLAG_NODEFRAG)
  882. flags |= XFS_XFLAG_NODEFRAG;
  883. if (di_flags & XFS_DIFLAG_FILESTREAM)
  884. flags |= XFS_XFLAG_FILESTREAM;
  885. }
  886. return flags;
  887. }
  888. uint
  889. xfs_ip2xflags(
  890. xfs_inode_t *ip)
  891. {
  892. xfs_icdinode_t *dic = &ip->i_d;
  893. return _xfs_dic2xflags(dic->di_flags) |
  894. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  895. }
  896. uint
  897. xfs_dic2xflags(
  898. xfs_dinode_t *dip)
  899. {
  900. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  901. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  902. }
  903. static bool
  904. xfs_dinode_verify(
  905. struct xfs_mount *mp,
  906. struct xfs_inode *ip,
  907. struct xfs_dinode *dip)
  908. {
  909. if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
  910. return false;
  911. /* only version 3 or greater inodes are extensively verified here */
  912. if (dip->di_version < 3)
  913. return true;
  914. if (!xfs_sb_version_hascrc(&mp->m_sb))
  915. return false;
  916. if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
  917. offsetof(struct xfs_dinode, di_crc)))
  918. return false;
  919. if (be64_to_cpu(dip->di_ino) != ip->i_ino)
  920. return false;
  921. if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
  922. return false;
  923. return true;
  924. }
  925. void
  926. xfs_dinode_calc_crc(
  927. struct xfs_mount *mp,
  928. struct xfs_dinode *dip)
  929. {
  930. __uint32_t crc;
  931. if (dip->di_version < 3)
  932. return;
  933. ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
  934. crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
  935. offsetof(struct xfs_dinode, di_crc));
  936. dip->di_crc = xfs_end_cksum(crc);
  937. }
  938. /*
  939. * Read the disk inode attributes into the in-core inode structure.
  940. *
  941. * If we are initialising a new inode and we are not utilising the
  942. * XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new inode core
  943. * with a random generation number. If we are keeping inodes around, we need to
  944. * read the inode cluster to get the existing generation number off disk.
  945. */
  946. int
  947. xfs_iread(
  948. xfs_mount_t *mp,
  949. xfs_trans_t *tp,
  950. xfs_inode_t *ip,
  951. uint iget_flags)
  952. {
  953. xfs_buf_t *bp;
  954. xfs_dinode_t *dip;
  955. int error;
  956. /*
  957. * Fill in the location information in the in-core inode.
  958. */
  959. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
  960. if (error)
  961. return error;
  962. /* shortcut IO on inode allocation if possible */
  963. if ((iget_flags & XFS_IGET_CREATE) &&
  964. !(mp->m_flags & XFS_MOUNT_IKEEP)) {
  965. /* initialise the on-disk inode core */
  966. memset(&ip->i_d, 0, sizeof(ip->i_d));
  967. ip->i_d.di_magic = XFS_DINODE_MAGIC;
  968. ip->i_d.di_gen = prandom_u32();
  969. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  970. ip->i_d.di_version = 3;
  971. ip->i_d.di_ino = ip->i_ino;
  972. uuid_copy(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid);
  973. } else
  974. ip->i_d.di_version = 2;
  975. return 0;
  976. }
  977. /*
  978. * Get pointers to the on-disk inode and the buffer containing it.
  979. */
  980. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
  981. if (error)
  982. return error;
  983. /* even unallocated inodes are verified */
  984. if (!xfs_dinode_verify(mp, ip, dip)) {
  985. xfs_alert(mp, "%s: validation failed for inode %lld failed",
  986. __func__, ip->i_ino);
  987. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
  988. error = XFS_ERROR(EFSCORRUPTED);
  989. goto out_brelse;
  990. }
  991. /*
  992. * If the on-disk inode is already linked to a directory
  993. * entry, copy all of the inode into the in-core inode.
  994. * xfs_iformat() handles copying in the inode format
  995. * specific information.
  996. * Otherwise, just get the truly permanent information.
  997. */
  998. if (dip->di_mode) {
  999. xfs_dinode_from_disk(&ip->i_d, dip);
  1000. error = xfs_iformat(ip, dip);
  1001. if (error) {
  1002. #ifdef DEBUG
  1003. xfs_alert(mp, "%s: xfs_iformat() returned error %d",
  1004. __func__, error);
  1005. #endif /* DEBUG */
  1006. goto out_brelse;
  1007. }
  1008. } else {
  1009. /*
  1010. * Partial initialisation of the in-core inode. Just the bits
  1011. * that xfs_ialloc won't overwrite or relies on being correct.
  1012. */
  1013. ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
  1014. ip->i_d.di_version = dip->di_version;
  1015. ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
  1016. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  1017. if (dip->di_version == 3) {
  1018. ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
  1019. uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
  1020. }
  1021. /*
  1022. * Make sure to pull in the mode here as well in
  1023. * case the inode is released without being used.
  1024. * This ensures that xfs_inactive() will see that
  1025. * the inode is already free and not try to mess
  1026. * with the uninitialized part of it.
  1027. */
  1028. ip->i_d.di_mode = 0;
  1029. }
  1030. /*
  1031. * The inode format changed when we moved the link count and
  1032. * made it 32 bits long. If this is an old format inode,
  1033. * convert it in memory to look like a new one. If it gets
  1034. * flushed to disk we will convert back before flushing or
  1035. * logging it. We zero out the new projid field and the old link
  1036. * count field. We'll handle clearing the pad field (the remains
  1037. * of the old uuid field) when we actually convert the inode to
  1038. * the new format. We don't change the version number so that we
  1039. * can distinguish this from a real new format inode.
  1040. */
  1041. if (ip->i_d.di_version == 1) {
  1042. ip->i_d.di_nlink = ip->i_d.di_onlink;
  1043. ip->i_d.di_onlink = 0;
  1044. xfs_set_projid(ip, 0);
  1045. }
  1046. ip->i_delayed_blks = 0;
  1047. /*
  1048. * Mark the buffer containing the inode as something to keep
  1049. * around for a while. This helps to keep recently accessed
  1050. * meta-data in-core longer.
  1051. */
  1052. xfs_buf_set_ref(bp, XFS_INO_REF);
  1053. /*
  1054. * Use xfs_trans_brelse() to release the buffer containing the on-disk
  1055. * inode, because it was acquired with xfs_trans_read_buf() in
  1056. * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
  1057. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  1058. * will only release the buffer if it is not dirty within the
  1059. * transaction. It will be OK to release the buffer in this case,
  1060. * because inodes on disk are never destroyed and we will be locking the
  1061. * new in-core inode before putting it in the cache where other
  1062. * processes can find it. Thus we don't have to worry about the inode
  1063. * being changed just because we released the buffer.
  1064. */
  1065. out_brelse:
  1066. xfs_trans_brelse(tp, bp);
  1067. return error;
  1068. }
  1069. /*
  1070. * Read in extents from a btree-format inode.
  1071. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  1072. */
  1073. int
  1074. xfs_iread_extents(
  1075. xfs_trans_t *tp,
  1076. xfs_inode_t *ip,
  1077. int whichfork)
  1078. {
  1079. int error;
  1080. xfs_ifork_t *ifp;
  1081. xfs_extnum_t nextents;
  1082. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  1083. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  1084. ip->i_mount);
  1085. return XFS_ERROR(EFSCORRUPTED);
  1086. }
  1087. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  1088. ifp = XFS_IFORK_PTR(ip, whichfork);
  1089. /*
  1090. * We know that the size is valid (it's checked in iformat_btree)
  1091. */
  1092. ifp->if_bytes = ifp->if_real_bytes = 0;
  1093. ifp->if_flags |= XFS_IFEXTENTS;
  1094. xfs_iext_add(ifp, 0, nextents);
  1095. error = xfs_bmap_read_extents(tp, ip, whichfork);
  1096. if (error) {
  1097. xfs_iext_destroy(ifp);
  1098. ifp->if_flags &= ~XFS_IFEXTENTS;
  1099. return error;
  1100. }
  1101. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  1102. return 0;
  1103. }
  1104. /*
  1105. * Allocate an inode on disk and return a copy of its in-core version.
  1106. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  1107. * appropriately within the inode. The uid and gid for the inode are
  1108. * set according to the contents of the given cred structure.
  1109. *
  1110. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  1111. * has a free inode available, call xfs_iget() to obtain the in-core
  1112. * version of the allocated inode. Finally, fill in the inode and
  1113. * log its initial contents. In this case, ialloc_context would be
  1114. * set to NULL.
  1115. *
  1116. * If xfs_dialloc() does not have an available inode, it will replenish
  1117. * its supply by doing an allocation. Since we can only do one
  1118. * allocation within a transaction without deadlocks, we must commit
  1119. * the current transaction before returning the inode itself.
  1120. * In this case, therefore, we will set ialloc_context and return.
  1121. * The caller should then commit the current transaction, start a new
  1122. * transaction, and call xfs_ialloc() again to actually get the inode.
  1123. *
  1124. * To ensure that some other process does not grab the inode that
  1125. * was allocated during the first call to xfs_ialloc(), this routine
  1126. * also returns the [locked] bp pointing to the head of the freelist
  1127. * as ialloc_context. The caller should hold this buffer across
  1128. * the commit and pass it back into this routine on the second call.
  1129. *
  1130. * If we are allocating quota inodes, we do not have a parent inode
  1131. * to attach to or associate with (i.e. pip == NULL) because they
  1132. * are not linked into the directory structure - they are attached
  1133. * directly to the superblock - and so have no parent.
  1134. */
  1135. int
  1136. xfs_ialloc(
  1137. xfs_trans_t *tp,
  1138. xfs_inode_t *pip,
  1139. umode_t mode,
  1140. xfs_nlink_t nlink,
  1141. xfs_dev_t rdev,
  1142. prid_t prid,
  1143. int okalloc,
  1144. xfs_buf_t **ialloc_context,
  1145. xfs_inode_t **ipp)
  1146. {
  1147. struct xfs_mount *mp = tp->t_mountp;
  1148. xfs_ino_t ino;
  1149. xfs_inode_t *ip;
  1150. uint flags;
  1151. int error;
  1152. timespec_t tv;
  1153. int filestreams = 0;
  1154. /*
  1155. * Call the space management code to pick
  1156. * the on-disk inode to be allocated.
  1157. */
  1158. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  1159. ialloc_context, &ino);
  1160. if (error)
  1161. return error;
  1162. if (*ialloc_context || ino == NULLFSINO) {
  1163. *ipp = NULL;
  1164. return 0;
  1165. }
  1166. ASSERT(*ialloc_context == NULL);
  1167. /*
  1168. * Get the in-core inode with the lock held exclusively.
  1169. * This is because we're setting fields here we need
  1170. * to prevent others from looking at until we're done.
  1171. */
  1172. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  1173. XFS_ILOCK_EXCL, &ip);
  1174. if (error)
  1175. return error;
  1176. ASSERT(ip != NULL);
  1177. ip->i_d.di_mode = mode;
  1178. ip->i_d.di_onlink = 0;
  1179. ip->i_d.di_nlink = nlink;
  1180. ASSERT(ip->i_d.di_nlink == nlink);
  1181. ip->i_d.di_uid = current_fsuid();
  1182. ip->i_d.di_gid = current_fsgid();
  1183. xfs_set_projid(ip, prid);
  1184. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  1185. /*
  1186. * If the superblock version is up to where we support new format
  1187. * inodes and this is currently an old format inode, then change
  1188. * the inode version number now. This way we only do the conversion
  1189. * here rather than here and in the flush/logging code.
  1190. */
  1191. if (xfs_sb_version_hasnlink(&mp->m_sb) &&
  1192. ip->i_d.di_version == 1) {
  1193. ip->i_d.di_version = 2;
  1194. /*
  1195. * We've already zeroed the old link count, the projid field,
  1196. * and the pad field.
  1197. */
  1198. }
  1199. /*
  1200. * Project ids won't be stored on disk if we are using a version 1 inode.
  1201. */
  1202. if ((prid != 0) && (ip->i_d.di_version == 1))
  1203. xfs_bump_ino_vers2(tp, ip);
  1204. if (pip && XFS_INHERIT_GID(pip)) {
  1205. ip->i_d.di_gid = pip->i_d.di_gid;
  1206. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  1207. ip->i_d.di_mode |= S_ISGID;
  1208. }
  1209. }
  1210. /*
  1211. * If the group ID of the new file does not match the effective group
  1212. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  1213. * (and only if the irix_sgid_inherit compatibility variable is set).
  1214. */
  1215. if ((irix_sgid_inherit) &&
  1216. (ip->i_d.di_mode & S_ISGID) &&
  1217. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  1218. ip->i_d.di_mode &= ~S_ISGID;
  1219. }
  1220. ip->i_d.di_size = 0;
  1221. ip->i_d.di_nextents = 0;
  1222. ASSERT(ip->i_d.di_nblocks == 0);
  1223. nanotime(&tv);
  1224. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  1225. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  1226. ip->i_d.di_atime = ip->i_d.di_mtime;
  1227. ip->i_d.di_ctime = ip->i_d.di_mtime;
  1228. /*
  1229. * di_gen will have been taken care of in xfs_iread.
  1230. */
  1231. ip->i_d.di_extsize = 0;
  1232. ip->i_d.di_dmevmask = 0;
  1233. ip->i_d.di_dmstate = 0;
  1234. ip->i_d.di_flags = 0;
  1235. if (ip->i_d.di_version == 3) {
  1236. ASSERT(ip->i_d.di_ino == ino);
  1237. ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
  1238. ip->i_d.di_crc = 0;
  1239. ip->i_d.di_changecount = 1;
  1240. ip->i_d.di_lsn = 0;
  1241. ip->i_d.di_flags2 = 0;
  1242. memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
  1243. ip->i_d.di_crtime = ip->i_d.di_mtime;
  1244. }
  1245. flags = XFS_ILOG_CORE;
  1246. switch (mode & S_IFMT) {
  1247. case S_IFIFO:
  1248. case S_IFCHR:
  1249. case S_IFBLK:
  1250. case S_IFSOCK:
  1251. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  1252. ip->i_df.if_u2.if_rdev = rdev;
  1253. ip->i_df.if_flags = 0;
  1254. flags |= XFS_ILOG_DEV;
  1255. break;
  1256. case S_IFREG:
  1257. /*
  1258. * we can't set up filestreams until after the VFS inode
  1259. * is set up properly.
  1260. */
  1261. if (pip && xfs_inode_is_filestream(pip))
  1262. filestreams = 1;
  1263. /* fall through */
  1264. case S_IFDIR:
  1265. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  1266. uint di_flags = 0;
  1267. if (S_ISDIR(mode)) {
  1268. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1269. di_flags |= XFS_DIFLAG_RTINHERIT;
  1270. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1271. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  1272. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1273. }
  1274. } else if (S_ISREG(mode)) {
  1275. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1276. di_flags |= XFS_DIFLAG_REALTIME;
  1277. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1278. di_flags |= XFS_DIFLAG_EXTSIZE;
  1279. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1280. }
  1281. }
  1282. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1283. xfs_inherit_noatime)
  1284. di_flags |= XFS_DIFLAG_NOATIME;
  1285. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1286. xfs_inherit_nodump)
  1287. di_flags |= XFS_DIFLAG_NODUMP;
  1288. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1289. xfs_inherit_sync)
  1290. di_flags |= XFS_DIFLAG_SYNC;
  1291. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1292. xfs_inherit_nosymlinks)
  1293. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1294. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1295. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1296. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  1297. xfs_inherit_nodefrag)
  1298. di_flags |= XFS_DIFLAG_NODEFRAG;
  1299. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  1300. di_flags |= XFS_DIFLAG_FILESTREAM;
  1301. ip->i_d.di_flags |= di_flags;
  1302. }
  1303. /* FALLTHROUGH */
  1304. case S_IFLNK:
  1305. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1306. ip->i_df.if_flags = XFS_IFEXTENTS;
  1307. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1308. ip->i_df.if_u1.if_extents = NULL;
  1309. break;
  1310. default:
  1311. ASSERT(0);
  1312. }
  1313. /*
  1314. * Attribute fork settings for new inode.
  1315. */
  1316. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1317. ip->i_d.di_anextents = 0;
  1318. /*
  1319. * Log the new values stuffed into the inode.
  1320. */
  1321. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1322. xfs_trans_log_inode(tp, ip, flags);
  1323. /* now that we have an i_mode we can setup inode ops and unlock */
  1324. xfs_setup_inode(ip);
  1325. /* now we have set up the vfs inode we can associate the filestream */
  1326. if (filestreams) {
  1327. error = xfs_filestream_associate(pip, ip);
  1328. if (error < 0)
  1329. return -error;
  1330. if (!error)
  1331. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1332. }
  1333. *ipp = ip;
  1334. return 0;
  1335. }
  1336. /*
  1337. * Free up the underlying blocks past new_size. The new size must be smaller
  1338. * than the current size. This routine can be used both for the attribute and
  1339. * data fork, and does not modify the inode size, which is left to the caller.
  1340. *
  1341. * The transaction passed to this routine must have made a permanent log
  1342. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1343. * given transaction and start new ones, so make sure everything involved in
  1344. * the transaction is tidy before calling here. Some transaction will be
  1345. * returned to the caller to be committed. The incoming transaction must
  1346. * already include the inode, and both inode locks must be held exclusively.
  1347. * The inode must also be "held" within the transaction. On return the inode
  1348. * will be "held" within the returned transaction. This routine does NOT
  1349. * require any disk space to be reserved for it within the transaction.
  1350. *
  1351. * If we get an error, we must return with the inode locked and linked into the
  1352. * current transaction. This keeps things simple for the higher level code,
  1353. * because it always knows that the inode is locked and held in the transaction
  1354. * that returns to it whether errors occur or not. We don't mark the inode
  1355. * dirty on error so that transactions can be easily aborted if possible.
  1356. */
  1357. int
  1358. xfs_itruncate_extents(
  1359. struct xfs_trans **tpp,
  1360. struct xfs_inode *ip,
  1361. int whichfork,
  1362. xfs_fsize_t new_size)
  1363. {
  1364. struct xfs_mount *mp = ip->i_mount;
  1365. struct xfs_trans *tp = *tpp;
  1366. struct xfs_trans *ntp;
  1367. xfs_bmap_free_t free_list;
  1368. xfs_fsblock_t first_block;
  1369. xfs_fileoff_t first_unmap_block;
  1370. xfs_fileoff_t last_block;
  1371. xfs_filblks_t unmap_len;
  1372. int committed;
  1373. int error = 0;
  1374. int done = 0;
  1375. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1376. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1377. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1378. ASSERT(new_size <= XFS_ISIZE(ip));
  1379. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1380. ASSERT(ip->i_itemp != NULL);
  1381. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1382. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1383. trace_xfs_itruncate_extents_start(ip, new_size);
  1384. /*
  1385. * Since it is possible for space to become allocated beyond
  1386. * the end of the file (in a crash where the space is allocated
  1387. * but the inode size is not yet updated), simply remove any
  1388. * blocks which show up between the new EOF and the maximum
  1389. * possible file size. If the first block to be removed is
  1390. * beyond the maximum file size (ie it is the same as last_block),
  1391. * then there is nothing to do.
  1392. */
  1393. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1394. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1395. if (first_unmap_block == last_block)
  1396. return 0;
  1397. ASSERT(first_unmap_block < last_block);
  1398. unmap_len = last_block - first_unmap_block + 1;
  1399. while (!done) {
  1400. xfs_bmap_init(&free_list, &first_block);
  1401. error = xfs_bunmapi(tp, ip,
  1402. first_unmap_block, unmap_len,
  1403. xfs_bmapi_aflag(whichfork),
  1404. XFS_ITRUNC_MAX_EXTENTS,
  1405. &first_block, &free_list,
  1406. &done);
  1407. if (error)
  1408. goto out_bmap_cancel;
  1409. /*
  1410. * Duplicate the transaction that has the permanent
  1411. * reservation and commit the old transaction.
  1412. */
  1413. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1414. if (committed)
  1415. xfs_trans_ijoin(tp, ip, 0);
  1416. if (error)
  1417. goto out_bmap_cancel;
  1418. if (committed) {
  1419. /*
  1420. * Mark the inode dirty so it will be logged and
  1421. * moved forward in the log as part of every commit.
  1422. */
  1423. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1424. }
  1425. ntp = xfs_trans_dup(tp);
  1426. error = xfs_trans_commit(tp, 0);
  1427. tp = ntp;
  1428. xfs_trans_ijoin(tp, ip, 0);
  1429. if (error)
  1430. goto out;
  1431. /*
  1432. * Transaction commit worked ok so we can drop the extra ticket
  1433. * reference that we gained in xfs_trans_dup()
  1434. */
  1435. xfs_log_ticket_put(tp->t_ticket);
  1436. error = xfs_trans_reserve(tp, 0,
  1437. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1438. XFS_TRANS_PERM_LOG_RES,
  1439. XFS_ITRUNCATE_LOG_COUNT);
  1440. if (error)
  1441. goto out;
  1442. }
  1443. /*
  1444. * Always re-log the inode so that our permanent transaction can keep
  1445. * on rolling it forward in the log.
  1446. */
  1447. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1448. trace_xfs_itruncate_extents_end(ip, new_size);
  1449. out:
  1450. *tpp = tp;
  1451. return error;
  1452. out_bmap_cancel:
  1453. /*
  1454. * If the bunmapi call encounters an error, return to the caller where
  1455. * the transaction can be properly aborted. We just need to make sure
  1456. * we're not holding any resources that we were not when we came in.
  1457. */
  1458. xfs_bmap_cancel(&free_list);
  1459. goto out;
  1460. }
  1461. /*
  1462. * This is called when the inode's link count goes to 0.
  1463. * We place the on-disk inode on a list in the AGI. It
  1464. * will be pulled from this list when the inode is freed.
  1465. */
  1466. int
  1467. xfs_iunlink(
  1468. xfs_trans_t *tp,
  1469. xfs_inode_t *ip)
  1470. {
  1471. xfs_mount_t *mp;
  1472. xfs_agi_t *agi;
  1473. xfs_dinode_t *dip;
  1474. xfs_buf_t *agibp;
  1475. xfs_buf_t *ibp;
  1476. xfs_agino_t agino;
  1477. short bucket_index;
  1478. int offset;
  1479. int error;
  1480. ASSERT(ip->i_d.di_nlink == 0);
  1481. ASSERT(ip->i_d.di_mode != 0);
  1482. mp = tp->t_mountp;
  1483. /*
  1484. * Get the agi buffer first. It ensures lock ordering
  1485. * on the list.
  1486. */
  1487. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1488. if (error)
  1489. return error;
  1490. agi = XFS_BUF_TO_AGI(agibp);
  1491. /*
  1492. * Get the index into the agi hash table for the
  1493. * list this inode will go on.
  1494. */
  1495. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1496. ASSERT(agino != 0);
  1497. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1498. ASSERT(agi->agi_unlinked[bucket_index]);
  1499. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1500. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1501. /*
  1502. * There is already another inode in the bucket we need
  1503. * to add ourselves to. Add us at the front of the list.
  1504. * Here we put the head pointer into our next pointer,
  1505. * and then we fall through to point the head at us.
  1506. */
  1507. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1508. 0, 0);
  1509. if (error)
  1510. return error;
  1511. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1512. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1513. offset = ip->i_imap.im_boffset +
  1514. offsetof(xfs_dinode_t, di_next_unlinked);
  1515. /* need to recalc the inode CRC if appropriate */
  1516. xfs_dinode_calc_crc(mp, dip);
  1517. xfs_trans_inode_buf(tp, ibp);
  1518. xfs_trans_log_buf(tp, ibp, offset,
  1519. (offset + sizeof(xfs_agino_t) - 1));
  1520. xfs_inobp_check(mp, ibp);
  1521. }
  1522. /*
  1523. * Point the bucket head pointer at the inode being inserted.
  1524. */
  1525. ASSERT(agino != 0);
  1526. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1527. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1528. (sizeof(xfs_agino_t) * bucket_index);
  1529. xfs_trans_log_buf(tp, agibp, offset,
  1530. (offset + sizeof(xfs_agino_t) - 1));
  1531. return 0;
  1532. }
  1533. /*
  1534. * Pull the on-disk inode from the AGI unlinked list.
  1535. */
  1536. STATIC int
  1537. xfs_iunlink_remove(
  1538. xfs_trans_t *tp,
  1539. xfs_inode_t *ip)
  1540. {
  1541. xfs_ino_t next_ino;
  1542. xfs_mount_t *mp;
  1543. xfs_agi_t *agi;
  1544. xfs_dinode_t *dip;
  1545. xfs_buf_t *agibp;
  1546. xfs_buf_t *ibp;
  1547. xfs_agnumber_t agno;
  1548. xfs_agino_t agino;
  1549. xfs_agino_t next_agino;
  1550. xfs_buf_t *last_ibp;
  1551. xfs_dinode_t *last_dip = NULL;
  1552. short bucket_index;
  1553. int offset, last_offset = 0;
  1554. int error;
  1555. mp = tp->t_mountp;
  1556. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1557. /*
  1558. * Get the agi buffer first. It ensures lock ordering
  1559. * on the list.
  1560. */
  1561. error = xfs_read_agi(mp, tp, agno, &agibp);
  1562. if (error)
  1563. return error;
  1564. agi = XFS_BUF_TO_AGI(agibp);
  1565. /*
  1566. * Get the index into the agi hash table for the
  1567. * list this inode will go on.
  1568. */
  1569. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1570. ASSERT(agino != 0);
  1571. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1572. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1573. ASSERT(agi->agi_unlinked[bucket_index]);
  1574. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1575. /*
  1576. * We're at the head of the list. Get the inode's on-disk
  1577. * buffer to see if there is anyone after us on the list.
  1578. * Only modify our next pointer if it is not already NULLAGINO.
  1579. * This saves us the overhead of dealing with the buffer when
  1580. * there is no need to change it.
  1581. */
  1582. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1583. 0, 0);
  1584. if (error) {
  1585. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1586. __func__, error);
  1587. return error;
  1588. }
  1589. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1590. ASSERT(next_agino != 0);
  1591. if (next_agino != NULLAGINO) {
  1592. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1593. offset = ip->i_imap.im_boffset +
  1594. offsetof(xfs_dinode_t, di_next_unlinked);
  1595. /* need to recalc the inode CRC if appropriate */
  1596. xfs_dinode_calc_crc(mp, dip);
  1597. xfs_trans_inode_buf(tp, ibp);
  1598. xfs_trans_log_buf(tp, ibp, offset,
  1599. (offset + sizeof(xfs_agino_t) - 1));
  1600. xfs_inobp_check(mp, ibp);
  1601. } else {
  1602. xfs_trans_brelse(tp, ibp);
  1603. }
  1604. /*
  1605. * Point the bucket head pointer at the next inode.
  1606. */
  1607. ASSERT(next_agino != 0);
  1608. ASSERT(next_agino != agino);
  1609. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1610. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1611. (sizeof(xfs_agino_t) * bucket_index);
  1612. xfs_trans_log_buf(tp, agibp, offset,
  1613. (offset + sizeof(xfs_agino_t) - 1));
  1614. } else {
  1615. /*
  1616. * We need to search the list for the inode being freed.
  1617. */
  1618. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1619. last_ibp = NULL;
  1620. while (next_agino != agino) {
  1621. struct xfs_imap imap;
  1622. if (last_ibp)
  1623. xfs_trans_brelse(tp, last_ibp);
  1624. imap.im_blkno = 0;
  1625. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1626. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1627. if (error) {
  1628. xfs_warn(mp,
  1629. "%s: xfs_imap returned error %d.",
  1630. __func__, error);
  1631. return error;
  1632. }
  1633. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1634. &last_ibp, 0, 0);
  1635. if (error) {
  1636. xfs_warn(mp,
  1637. "%s: xfs_imap_to_bp returned error %d.",
  1638. __func__, error);
  1639. return error;
  1640. }
  1641. last_offset = imap.im_boffset;
  1642. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1643. ASSERT(next_agino != NULLAGINO);
  1644. ASSERT(next_agino != 0);
  1645. }
  1646. /*
  1647. * Now last_ibp points to the buffer previous to us on the
  1648. * unlinked list. Pull us from the list.
  1649. */
  1650. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1651. 0, 0);
  1652. if (error) {
  1653. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1654. __func__, error);
  1655. return error;
  1656. }
  1657. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1658. ASSERT(next_agino != 0);
  1659. ASSERT(next_agino != agino);
  1660. if (next_agino != NULLAGINO) {
  1661. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1662. offset = ip->i_imap.im_boffset +
  1663. offsetof(xfs_dinode_t, di_next_unlinked);
  1664. /* need to recalc the inode CRC if appropriate */
  1665. xfs_dinode_calc_crc(mp, dip);
  1666. xfs_trans_inode_buf(tp, ibp);
  1667. xfs_trans_log_buf(tp, ibp, offset,
  1668. (offset + sizeof(xfs_agino_t) - 1));
  1669. xfs_inobp_check(mp, ibp);
  1670. } else {
  1671. xfs_trans_brelse(tp, ibp);
  1672. }
  1673. /*
  1674. * Point the previous inode on the list to the next inode.
  1675. */
  1676. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1677. ASSERT(next_agino != 0);
  1678. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1679. /* need to recalc the inode CRC if appropriate */
  1680. xfs_dinode_calc_crc(mp, last_dip);
  1681. xfs_trans_inode_buf(tp, last_ibp);
  1682. xfs_trans_log_buf(tp, last_ibp, offset,
  1683. (offset + sizeof(xfs_agino_t) - 1));
  1684. xfs_inobp_check(mp, last_ibp);
  1685. }
  1686. return 0;
  1687. }
  1688. /*
  1689. * A big issue when freeing the inode cluster is is that we _cannot_ skip any
  1690. * inodes that are in memory - they all must be marked stale and attached to
  1691. * the cluster buffer.
  1692. */
  1693. STATIC int
  1694. xfs_ifree_cluster(
  1695. xfs_inode_t *free_ip,
  1696. xfs_trans_t *tp,
  1697. xfs_ino_t inum)
  1698. {
  1699. xfs_mount_t *mp = free_ip->i_mount;
  1700. int blks_per_cluster;
  1701. int nbufs;
  1702. int ninodes;
  1703. int i, j;
  1704. xfs_daddr_t blkno;
  1705. xfs_buf_t *bp;
  1706. xfs_inode_t *ip;
  1707. xfs_inode_log_item_t *iip;
  1708. xfs_log_item_t *lip;
  1709. struct xfs_perag *pag;
  1710. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1711. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1712. blks_per_cluster = 1;
  1713. ninodes = mp->m_sb.sb_inopblock;
  1714. nbufs = XFS_IALLOC_BLOCKS(mp);
  1715. } else {
  1716. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1717. mp->m_sb.sb_blocksize;
  1718. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1719. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1720. }
  1721. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1722. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1723. XFS_INO_TO_AGBNO(mp, inum));
  1724. /*
  1725. * We obtain and lock the backing buffer first in the process
  1726. * here, as we have to ensure that any dirty inode that we
  1727. * can't get the flush lock on is attached to the buffer.
  1728. * If we scan the in-memory inodes first, then buffer IO can
  1729. * complete before we get a lock on it, and hence we may fail
  1730. * to mark all the active inodes on the buffer stale.
  1731. */
  1732. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1733. mp->m_bsize * blks_per_cluster,
  1734. XBF_UNMAPPED);
  1735. if (!bp)
  1736. return ENOMEM;
  1737. /*
  1738. * This buffer may not have been correctly initialised as we
  1739. * didn't read it from disk. That's not important because we are
  1740. * only using to mark the buffer as stale in the log, and to
  1741. * attach stale cached inodes on it. That means it will never be
  1742. * dispatched for IO. If it is, we want to know about it, and we
  1743. * want it to fail. We can acheive this by adding a write
  1744. * verifier to the buffer.
  1745. */
  1746. bp->b_ops = &xfs_inode_buf_ops;
  1747. /*
  1748. * Walk the inodes already attached to the buffer and mark them
  1749. * stale. These will all have the flush locks held, so an
  1750. * in-memory inode walk can't lock them. By marking them all
  1751. * stale first, we will not attempt to lock them in the loop
  1752. * below as the XFS_ISTALE flag will be set.
  1753. */
  1754. lip = bp->b_fspriv;
  1755. while (lip) {
  1756. if (lip->li_type == XFS_LI_INODE) {
  1757. iip = (xfs_inode_log_item_t *)lip;
  1758. ASSERT(iip->ili_logged == 1);
  1759. lip->li_cb = xfs_istale_done;
  1760. xfs_trans_ail_copy_lsn(mp->m_ail,
  1761. &iip->ili_flush_lsn,
  1762. &iip->ili_item.li_lsn);
  1763. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1764. }
  1765. lip = lip->li_bio_list;
  1766. }
  1767. /*
  1768. * For each inode in memory attempt to add it to the inode
  1769. * buffer and set it up for being staled on buffer IO
  1770. * completion. This is safe as we've locked out tail pushing
  1771. * and flushing by locking the buffer.
  1772. *
  1773. * We have already marked every inode that was part of a
  1774. * transaction stale above, which means there is no point in
  1775. * even trying to lock them.
  1776. */
  1777. for (i = 0; i < ninodes; i++) {
  1778. retry:
  1779. rcu_read_lock();
  1780. ip = radix_tree_lookup(&pag->pag_ici_root,
  1781. XFS_INO_TO_AGINO(mp, (inum + i)));
  1782. /* Inode not in memory, nothing to do */
  1783. if (!ip) {
  1784. rcu_read_unlock();
  1785. continue;
  1786. }
  1787. /*
  1788. * because this is an RCU protected lookup, we could
  1789. * find a recently freed or even reallocated inode
  1790. * during the lookup. We need to check under the
  1791. * i_flags_lock for a valid inode here. Skip it if it
  1792. * is not valid, the wrong inode or stale.
  1793. */
  1794. spin_lock(&ip->i_flags_lock);
  1795. if (ip->i_ino != inum + i ||
  1796. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1797. spin_unlock(&ip->i_flags_lock);
  1798. rcu_read_unlock();
  1799. continue;
  1800. }
  1801. spin_unlock(&ip->i_flags_lock);
  1802. /*
  1803. * Don't try to lock/unlock the current inode, but we
  1804. * _cannot_ skip the other inodes that we did not find
  1805. * in the list attached to the buffer and are not
  1806. * already marked stale. If we can't lock it, back off
  1807. * and retry.
  1808. */
  1809. if (ip != free_ip &&
  1810. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1811. rcu_read_unlock();
  1812. delay(1);
  1813. goto retry;
  1814. }
  1815. rcu_read_unlock();
  1816. xfs_iflock(ip);
  1817. xfs_iflags_set(ip, XFS_ISTALE);
  1818. /*
  1819. * we don't need to attach clean inodes or those only
  1820. * with unlogged changes (which we throw away, anyway).
  1821. */
  1822. iip = ip->i_itemp;
  1823. if (!iip || xfs_inode_clean(ip)) {
  1824. ASSERT(ip != free_ip);
  1825. xfs_ifunlock(ip);
  1826. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1827. continue;
  1828. }
  1829. iip->ili_last_fields = iip->ili_fields;
  1830. iip->ili_fields = 0;
  1831. iip->ili_logged = 1;
  1832. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  1833. &iip->ili_item.li_lsn);
  1834. xfs_buf_attach_iodone(bp, xfs_istale_done,
  1835. &iip->ili_item);
  1836. if (ip != free_ip)
  1837. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1838. }
  1839. xfs_trans_stale_inode_buf(tp, bp);
  1840. xfs_trans_binval(tp, bp);
  1841. }
  1842. xfs_perag_put(pag);
  1843. return 0;
  1844. }
  1845. /*
  1846. * This is called to return an inode to the inode free list.
  1847. * The inode should already be truncated to 0 length and have
  1848. * no pages associated with it. This routine also assumes that
  1849. * the inode is already a part of the transaction.
  1850. *
  1851. * The on-disk copy of the inode will have been added to the list
  1852. * of unlinked inodes in the AGI. We need to remove the inode from
  1853. * that list atomically with respect to freeing it here.
  1854. */
  1855. int
  1856. xfs_ifree(
  1857. xfs_trans_t *tp,
  1858. xfs_inode_t *ip,
  1859. xfs_bmap_free_t *flist)
  1860. {
  1861. int error;
  1862. int delete;
  1863. xfs_ino_t first_ino;
  1864. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1865. ASSERT(ip->i_d.di_nlink == 0);
  1866. ASSERT(ip->i_d.di_nextents == 0);
  1867. ASSERT(ip->i_d.di_anextents == 0);
  1868. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  1869. ASSERT(ip->i_d.di_nblocks == 0);
  1870. /*
  1871. * Pull the on-disk inode from the AGI unlinked list.
  1872. */
  1873. error = xfs_iunlink_remove(tp, ip);
  1874. if (error)
  1875. return error;
  1876. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  1877. if (error)
  1878. return error;
  1879. ip->i_d.di_mode = 0; /* mark incore inode as free */
  1880. ip->i_d.di_flags = 0;
  1881. ip->i_d.di_dmevmask = 0;
  1882. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  1883. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1884. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1885. /*
  1886. * Bump the generation count so no one will be confused
  1887. * by reincarnations of this inode.
  1888. */
  1889. ip->i_d.di_gen++;
  1890. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1891. if (delete)
  1892. error = xfs_ifree_cluster(ip, tp, first_ino);
  1893. return error;
  1894. }
  1895. /*
  1896. * Reallocate the space for if_broot based on the number of records
  1897. * being added or deleted as indicated in rec_diff. Move the records
  1898. * and pointers in if_broot to fit the new size. When shrinking this
  1899. * will eliminate holes between the records and pointers created by
  1900. * the caller. When growing this will create holes to be filled in
  1901. * by the caller.
  1902. *
  1903. * The caller must not request to add more records than would fit in
  1904. * the on-disk inode root. If the if_broot is currently NULL, then
  1905. * if we adding records one will be allocated. The caller must also
  1906. * not request that the number of records go below zero, although
  1907. * it can go to zero.
  1908. *
  1909. * ip -- the inode whose if_broot area is changing
  1910. * ext_diff -- the change in the number of records, positive or negative,
  1911. * requested for the if_broot array.
  1912. */
  1913. void
  1914. xfs_iroot_realloc(
  1915. xfs_inode_t *ip,
  1916. int rec_diff,
  1917. int whichfork)
  1918. {
  1919. struct xfs_mount *mp = ip->i_mount;
  1920. int cur_max;
  1921. xfs_ifork_t *ifp;
  1922. struct xfs_btree_block *new_broot;
  1923. int new_max;
  1924. size_t new_size;
  1925. char *np;
  1926. char *op;
  1927. /*
  1928. * Handle the degenerate case quietly.
  1929. */
  1930. if (rec_diff == 0) {
  1931. return;
  1932. }
  1933. ifp = XFS_IFORK_PTR(ip, whichfork);
  1934. if (rec_diff > 0) {
  1935. /*
  1936. * If there wasn't any memory allocated before, just
  1937. * allocate it now and get out.
  1938. */
  1939. if (ifp->if_broot_bytes == 0) {
  1940. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  1941. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1942. ifp->if_broot_bytes = (int)new_size;
  1943. return;
  1944. }
  1945. /*
  1946. * If there is already an existing if_broot, then we need
  1947. * to realloc() it and shift the pointers to their new
  1948. * location. The records don't change location because
  1949. * they are kept butted up against the btree block header.
  1950. */
  1951. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1952. new_max = cur_max + rec_diff;
  1953. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  1954. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  1955. XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
  1956. KM_SLEEP | KM_NOFS);
  1957. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1958. ifp->if_broot_bytes);
  1959. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1960. (int)new_size);
  1961. ifp->if_broot_bytes = (int)new_size;
  1962. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  1963. XFS_IFORK_SIZE(ip, whichfork));
  1964. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  1965. return;
  1966. }
  1967. /*
  1968. * rec_diff is less than 0. In this case, we are shrinking the
  1969. * if_broot buffer. It must already exist. If we go to zero
  1970. * records, just get rid of the root and clear the status bit.
  1971. */
  1972. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  1973. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1974. new_max = cur_max + rec_diff;
  1975. ASSERT(new_max >= 0);
  1976. if (new_max > 0)
  1977. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  1978. else
  1979. new_size = 0;
  1980. if (new_size > 0) {
  1981. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1982. /*
  1983. * First copy over the btree block header.
  1984. */
  1985. memcpy(new_broot, ifp->if_broot,
  1986. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  1987. } else {
  1988. new_broot = NULL;
  1989. ifp->if_flags &= ~XFS_IFBROOT;
  1990. }
  1991. /*
  1992. * Only copy the records and pointers if there are any.
  1993. */
  1994. if (new_max > 0) {
  1995. /*
  1996. * First copy the records.
  1997. */
  1998. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  1999. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  2000. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  2001. /*
  2002. * Then copy the pointers.
  2003. */
  2004. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  2005. ifp->if_broot_bytes);
  2006. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  2007. (int)new_size);
  2008. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  2009. }
  2010. kmem_free(ifp->if_broot);
  2011. ifp->if_broot = new_broot;
  2012. ifp->if_broot_bytes = (int)new_size;
  2013. if (ifp->if_broot)
  2014. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  2015. XFS_IFORK_SIZE(ip, whichfork));
  2016. return;
  2017. }
  2018. /*
  2019. * This is called when the amount of space needed for if_data
  2020. * is increased or decreased. The change in size is indicated by
  2021. * the number of bytes that need to be added or deleted in the
  2022. * byte_diff parameter.
  2023. *
  2024. * If the amount of space needed has decreased below the size of the
  2025. * inline buffer, then switch to using the inline buffer. Otherwise,
  2026. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2027. * to what is needed.
  2028. *
  2029. * ip -- the inode whose if_data area is changing
  2030. * byte_diff -- the change in the number of bytes, positive or negative,
  2031. * requested for the if_data array.
  2032. */
  2033. void
  2034. xfs_idata_realloc(
  2035. xfs_inode_t *ip,
  2036. int byte_diff,
  2037. int whichfork)
  2038. {
  2039. xfs_ifork_t *ifp;
  2040. int new_size;
  2041. int real_size;
  2042. if (byte_diff == 0) {
  2043. return;
  2044. }
  2045. ifp = XFS_IFORK_PTR(ip, whichfork);
  2046. new_size = (int)ifp->if_bytes + byte_diff;
  2047. ASSERT(new_size >= 0);
  2048. if (new_size == 0) {
  2049. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2050. kmem_free(ifp->if_u1.if_data);
  2051. }
  2052. ifp->if_u1.if_data = NULL;
  2053. real_size = 0;
  2054. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  2055. /*
  2056. * If the valid extents/data can fit in if_inline_ext/data,
  2057. * copy them from the malloc'd vector and free it.
  2058. */
  2059. if (ifp->if_u1.if_data == NULL) {
  2060. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2061. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2062. ASSERT(ifp->if_real_bytes != 0);
  2063. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  2064. new_size);
  2065. kmem_free(ifp->if_u1.if_data);
  2066. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2067. }
  2068. real_size = 0;
  2069. } else {
  2070. /*
  2071. * Stuck with malloc/realloc.
  2072. * For inline data, the underlying buffer must be
  2073. * a multiple of 4 bytes in size so that it can be
  2074. * logged and stay on word boundaries. We enforce
  2075. * that here.
  2076. */
  2077. real_size = roundup(new_size, 4);
  2078. if (ifp->if_u1.if_data == NULL) {
  2079. ASSERT(ifp->if_real_bytes == 0);
  2080. ifp->if_u1.if_data = kmem_alloc(real_size,
  2081. KM_SLEEP | KM_NOFS);
  2082. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2083. /*
  2084. * Only do the realloc if the underlying size
  2085. * is really changing.
  2086. */
  2087. if (ifp->if_real_bytes != real_size) {
  2088. ifp->if_u1.if_data =
  2089. kmem_realloc(ifp->if_u1.if_data,
  2090. real_size,
  2091. ifp->if_real_bytes,
  2092. KM_SLEEP | KM_NOFS);
  2093. }
  2094. } else {
  2095. ASSERT(ifp->if_real_bytes == 0);
  2096. ifp->if_u1.if_data = kmem_alloc(real_size,
  2097. KM_SLEEP | KM_NOFS);
  2098. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  2099. ifp->if_bytes);
  2100. }
  2101. }
  2102. ifp->if_real_bytes = real_size;
  2103. ifp->if_bytes = new_size;
  2104. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2105. }
  2106. void
  2107. xfs_idestroy_fork(
  2108. xfs_inode_t *ip,
  2109. int whichfork)
  2110. {
  2111. xfs_ifork_t *ifp;
  2112. ifp = XFS_IFORK_PTR(ip, whichfork);
  2113. if (ifp->if_broot != NULL) {
  2114. kmem_free(ifp->if_broot);
  2115. ifp->if_broot = NULL;
  2116. }
  2117. /*
  2118. * If the format is local, then we can't have an extents
  2119. * array so just look for an inline data array. If we're
  2120. * not local then we may or may not have an extents list,
  2121. * so check and free it up if we do.
  2122. */
  2123. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  2124. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  2125. (ifp->if_u1.if_data != NULL)) {
  2126. ASSERT(ifp->if_real_bytes != 0);
  2127. kmem_free(ifp->if_u1.if_data);
  2128. ifp->if_u1.if_data = NULL;
  2129. ifp->if_real_bytes = 0;
  2130. }
  2131. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  2132. ((ifp->if_flags & XFS_IFEXTIREC) ||
  2133. ((ifp->if_u1.if_extents != NULL) &&
  2134. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  2135. ASSERT(ifp->if_real_bytes != 0);
  2136. xfs_iext_destroy(ifp);
  2137. }
  2138. ASSERT(ifp->if_u1.if_extents == NULL ||
  2139. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  2140. ASSERT(ifp->if_real_bytes == 0);
  2141. if (whichfork == XFS_ATTR_FORK) {
  2142. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  2143. ip->i_afp = NULL;
  2144. }
  2145. }
  2146. /*
  2147. * This is called to unpin an inode. The caller must have the inode locked
  2148. * in at least shared mode so that the buffer cannot be subsequently pinned
  2149. * once someone is waiting for it to be unpinned.
  2150. */
  2151. static void
  2152. xfs_iunpin(
  2153. struct xfs_inode *ip)
  2154. {
  2155. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2156. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2157. /* Give the log a push to start the unpinning I/O */
  2158. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2159. }
  2160. static void
  2161. __xfs_iunpin_wait(
  2162. struct xfs_inode *ip)
  2163. {
  2164. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2165. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2166. xfs_iunpin(ip);
  2167. do {
  2168. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2169. if (xfs_ipincount(ip))
  2170. io_schedule();
  2171. } while (xfs_ipincount(ip));
  2172. finish_wait(wq, &wait.wait);
  2173. }
  2174. void
  2175. xfs_iunpin_wait(
  2176. struct xfs_inode *ip)
  2177. {
  2178. if (xfs_ipincount(ip))
  2179. __xfs_iunpin_wait(ip);
  2180. }
  2181. /*
  2182. * xfs_iextents_copy()
  2183. *
  2184. * This is called to copy the REAL extents (as opposed to the delayed
  2185. * allocation extents) from the inode into the given buffer. It
  2186. * returns the number of bytes copied into the buffer.
  2187. *
  2188. * If there are no delayed allocation extents, then we can just
  2189. * memcpy() the extents into the buffer. Otherwise, we need to
  2190. * examine each extent in turn and skip those which are delayed.
  2191. */
  2192. int
  2193. xfs_iextents_copy(
  2194. xfs_inode_t *ip,
  2195. xfs_bmbt_rec_t *dp,
  2196. int whichfork)
  2197. {
  2198. int copied;
  2199. int i;
  2200. xfs_ifork_t *ifp;
  2201. int nrecs;
  2202. xfs_fsblock_t start_block;
  2203. ifp = XFS_IFORK_PTR(ip, whichfork);
  2204. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2205. ASSERT(ifp->if_bytes > 0);
  2206. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2207. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  2208. ASSERT(nrecs > 0);
  2209. /*
  2210. * There are some delayed allocation extents in the
  2211. * inode, so copy the extents one at a time and skip
  2212. * the delayed ones. There must be at least one
  2213. * non-delayed extent.
  2214. */
  2215. copied = 0;
  2216. for (i = 0; i < nrecs; i++) {
  2217. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  2218. start_block = xfs_bmbt_get_startblock(ep);
  2219. if (isnullstartblock(start_block)) {
  2220. /*
  2221. * It's a delayed allocation extent, so skip it.
  2222. */
  2223. continue;
  2224. }
  2225. /* Translate to on disk format */
  2226. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  2227. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  2228. dp++;
  2229. copied++;
  2230. }
  2231. ASSERT(copied != 0);
  2232. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  2233. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  2234. }
  2235. /*
  2236. * Each of the following cases stores data into the same region
  2237. * of the on-disk inode, so only one of them can be valid at
  2238. * any given time. While it is possible to have conflicting formats
  2239. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  2240. * in EXTENTS format, this can only happen when the fork has
  2241. * changed formats after being modified but before being flushed.
  2242. * In these cases, the format always takes precedence, because the
  2243. * format indicates the current state of the fork.
  2244. */
  2245. /*ARGSUSED*/
  2246. STATIC void
  2247. xfs_iflush_fork(
  2248. xfs_inode_t *ip,
  2249. xfs_dinode_t *dip,
  2250. xfs_inode_log_item_t *iip,
  2251. int whichfork,
  2252. xfs_buf_t *bp)
  2253. {
  2254. char *cp;
  2255. xfs_ifork_t *ifp;
  2256. xfs_mount_t *mp;
  2257. static const short brootflag[2] =
  2258. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  2259. static const short dataflag[2] =
  2260. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  2261. static const short extflag[2] =
  2262. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  2263. if (!iip)
  2264. return;
  2265. ifp = XFS_IFORK_PTR(ip, whichfork);
  2266. /*
  2267. * This can happen if we gave up in iformat in an error path,
  2268. * for the attribute fork.
  2269. */
  2270. if (!ifp) {
  2271. ASSERT(whichfork == XFS_ATTR_FORK);
  2272. return;
  2273. }
  2274. cp = XFS_DFORK_PTR(dip, whichfork);
  2275. mp = ip->i_mount;
  2276. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2277. case XFS_DINODE_FMT_LOCAL:
  2278. if ((iip->ili_fields & dataflag[whichfork]) &&
  2279. (ifp->if_bytes > 0)) {
  2280. ASSERT(ifp->if_u1.if_data != NULL);
  2281. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2282. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2283. }
  2284. break;
  2285. case XFS_DINODE_FMT_EXTENTS:
  2286. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2287. !(iip->ili_fields & extflag[whichfork]));
  2288. if ((iip->ili_fields & extflag[whichfork]) &&
  2289. (ifp->if_bytes > 0)) {
  2290. ASSERT(xfs_iext_get_ext(ifp, 0));
  2291. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2292. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2293. whichfork);
  2294. }
  2295. break;
  2296. case XFS_DINODE_FMT_BTREE:
  2297. if ((iip->ili_fields & brootflag[whichfork]) &&
  2298. (ifp->if_broot_bytes > 0)) {
  2299. ASSERT(ifp->if_broot != NULL);
  2300. ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
  2301. XFS_IFORK_SIZE(ip, whichfork));
  2302. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  2303. (xfs_bmdr_block_t *)cp,
  2304. XFS_DFORK_SIZE(dip, mp, whichfork));
  2305. }
  2306. break;
  2307. case XFS_DINODE_FMT_DEV:
  2308. if (iip->ili_fields & XFS_ILOG_DEV) {
  2309. ASSERT(whichfork == XFS_DATA_FORK);
  2310. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  2311. }
  2312. break;
  2313. case XFS_DINODE_FMT_UUID:
  2314. if (iip->ili_fields & XFS_ILOG_UUID) {
  2315. ASSERT(whichfork == XFS_DATA_FORK);
  2316. memcpy(XFS_DFORK_DPTR(dip),
  2317. &ip->i_df.if_u2.if_uuid,
  2318. sizeof(uuid_t));
  2319. }
  2320. break;
  2321. default:
  2322. ASSERT(0);
  2323. break;
  2324. }
  2325. }
  2326. STATIC int
  2327. xfs_iflush_cluster(
  2328. xfs_inode_t *ip,
  2329. xfs_buf_t *bp)
  2330. {
  2331. xfs_mount_t *mp = ip->i_mount;
  2332. struct xfs_perag *pag;
  2333. unsigned long first_index, mask;
  2334. unsigned long inodes_per_cluster;
  2335. int ilist_size;
  2336. xfs_inode_t **ilist;
  2337. xfs_inode_t *iq;
  2338. int nr_found;
  2339. int clcount = 0;
  2340. int bufwasdelwri;
  2341. int i;
  2342. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2343. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2344. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2345. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2346. if (!ilist)
  2347. goto out_put;
  2348. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2349. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2350. rcu_read_lock();
  2351. /* really need a gang lookup range call here */
  2352. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2353. first_index, inodes_per_cluster);
  2354. if (nr_found == 0)
  2355. goto out_free;
  2356. for (i = 0; i < nr_found; i++) {
  2357. iq = ilist[i];
  2358. if (iq == ip)
  2359. continue;
  2360. /*
  2361. * because this is an RCU protected lookup, we could find a
  2362. * recently freed or even reallocated inode during the lookup.
  2363. * We need to check under the i_flags_lock for a valid inode
  2364. * here. Skip it if it is not valid or the wrong inode.
  2365. */
  2366. spin_lock(&ip->i_flags_lock);
  2367. if (!ip->i_ino ||
  2368. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2369. spin_unlock(&ip->i_flags_lock);
  2370. continue;
  2371. }
  2372. spin_unlock(&ip->i_flags_lock);
  2373. /*
  2374. * Do an un-protected check to see if the inode is dirty and
  2375. * is a candidate for flushing. These checks will be repeated
  2376. * later after the appropriate locks are acquired.
  2377. */
  2378. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2379. continue;
  2380. /*
  2381. * Try to get locks. If any are unavailable or it is pinned,
  2382. * then this inode cannot be flushed and is skipped.
  2383. */
  2384. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2385. continue;
  2386. if (!xfs_iflock_nowait(iq)) {
  2387. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2388. continue;
  2389. }
  2390. if (xfs_ipincount(iq)) {
  2391. xfs_ifunlock(iq);
  2392. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2393. continue;
  2394. }
  2395. /*
  2396. * arriving here means that this inode can be flushed. First
  2397. * re-check that it's dirty before flushing.
  2398. */
  2399. if (!xfs_inode_clean(iq)) {
  2400. int error;
  2401. error = xfs_iflush_int(iq, bp);
  2402. if (error) {
  2403. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2404. goto cluster_corrupt_out;
  2405. }
  2406. clcount++;
  2407. } else {
  2408. xfs_ifunlock(iq);
  2409. }
  2410. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2411. }
  2412. if (clcount) {
  2413. XFS_STATS_INC(xs_icluster_flushcnt);
  2414. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2415. }
  2416. out_free:
  2417. rcu_read_unlock();
  2418. kmem_free(ilist);
  2419. out_put:
  2420. xfs_perag_put(pag);
  2421. return 0;
  2422. cluster_corrupt_out:
  2423. /*
  2424. * Corruption detected in the clustering loop. Invalidate the
  2425. * inode buffer and shut down the filesystem.
  2426. */
  2427. rcu_read_unlock();
  2428. /*
  2429. * Clean up the buffer. If it was delwri, just release it --
  2430. * brelse can handle it with no problems. If not, shut down the
  2431. * filesystem before releasing the buffer.
  2432. */
  2433. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2434. if (bufwasdelwri)
  2435. xfs_buf_relse(bp);
  2436. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2437. if (!bufwasdelwri) {
  2438. /*
  2439. * Just like incore_relse: if we have b_iodone functions,
  2440. * mark the buffer as an error and call them. Otherwise
  2441. * mark it as stale and brelse.
  2442. */
  2443. if (bp->b_iodone) {
  2444. XFS_BUF_UNDONE(bp);
  2445. xfs_buf_stale(bp);
  2446. xfs_buf_ioerror(bp, EIO);
  2447. xfs_buf_ioend(bp, 0);
  2448. } else {
  2449. xfs_buf_stale(bp);
  2450. xfs_buf_relse(bp);
  2451. }
  2452. }
  2453. /*
  2454. * Unlocks the flush lock
  2455. */
  2456. xfs_iflush_abort(iq, false);
  2457. kmem_free(ilist);
  2458. xfs_perag_put(pag);
  2459. return XFS_ERROR(EFSCORRUPTED);
  2460. }
  2461. /*
  2462. * Flush dirty inode metadata into the backing buffer.
  2463. *
  2464. * The caller must have the inode lock and the inode flush lock held. The
  2465. * inode lock will still be held upon return to the caller, and the inode
  2466. * flush lock will be released after the inode has reached the disk.
  2467. *
  2468. * The caller must write out the buffer returned in *bpp and release it.
  2469. */
  2470. int
  2471. xfs_iflush(
  2472. struct xfs_inode *ip,
  2473. struct xfs_buf **bpp)
  2474. {
  2475. struct xfs_mount *mp = ip->i_mount;
  2476. struct xfs_buf *bp;
  2477. struct xfs_dinode *dip;
  2478. int error;
  2479. XFS_STATS_INC(xs_iflush_count);
  2480. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2481. ASSERT(xfs_isiflocked(ip));
  2482. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2483. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2484. *bpp = NULL;
  2485. xfs_iunpin_wait(ip);
  2486. /*
  2487. * For stale inodes we cannot rely on the backing buffer remaining
  2488. * stale in cache for the remaining life of the stale inode and so
  2489. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2490. * inodes below. We have to check this after ensuring the inode is
  2491. * unpinned so that it is safe to reclaim the stale inode after the
  2492. * flush call.
  2493. */
  2494. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2495. xfs_ifunlock(ip);
  2496. return 0;
  2497. }
  2498. /*
  2499. * This may have been unpinned because the filesystem is shutting
  2500. * down forcibly. If that's the case we must not write this inode
  2501. * to disk, because the log record didn't make it to disk.
  2502. *
  2503. * We also have to remove the log item from the AIL in this case,
  2504. * as we wait for an empty AIL as part of the unmount process.
  2505. */
  2506. if (XFS_FORCED_SHUTDOWN(mp)) {
  2507. error = XFS_ERROR(EIO);
  2508. goto abort_out;
  2509. }
  2510. /*
  2511. * Get the buffer containing the on-disk inode.
  2512. */
  2513. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2514. 0);
  2515. if (error || !bp) {
  2516. xfs_ifunlock(ip);
  2517. return error;
  2518. }
  2519. /*
  2520. * First flush out the inode that xfs_iflush was called with.
  2521. */
  2522. error = xfs_iflush_int(ip, bp);
  2523. if (error)
  2524. goto corrupt_out;
  2525. /*
  2526. * If the buffer is pinned then push on the log now so we won't
  2527. * get stuck waiting in the write for too long.
  2528. */
  2529. if (xfs_buf_ispinned(bp))
  2530. xfs_log_force(mp, 0);
  2531. /*
  2532. * inode clustering:
  2533. * see if other inodes can be gathered into this write
  2534. */
  2535. error = xfs_iflush_cluster(ip, bp);
  2536. if (error)
  2537. goto cluster_corrupt_out;
  2538. *bpp = bp;
  2539. return 0;
  2540. corrupt_out:
  2541. xfs_buf_relse(bp);
  2542. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2543. cluster_corrupt_out:
  2544. error = XFS_ERROR(EFSCORRUPTED);
  2545. abort_out:
  2546. /*
  2547. * Unlocks the flush lock
  2548. */
  2549. xfs_iflush_abort(ip, false);
  2550. return error;
  2551. }
  2552. STATIC int
  2553. xfs_iflush_int(
  2554. struct xfs_inode *ip,
  2555. struct xfs_buf *bp)
  2556. {
  2557. struct xfs_inode_log_item *iip = ip->i_itemp;
  2558. struct xfs_dinode *dip;
  2559. struct xfs_mount *mp = ip->i_mount;
  2560. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2561. ASSERT(xfs_isiflocked(ip));
  2562. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2563. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2564. ASSERT(iip != NULL && iip->ili_fields != 0);
  2565. /* set *dip = inode's place in the buffer */
  2566. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2567. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2568. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2569. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2570. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2571. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2572. goto corrupt_out;
  2573. }
  2574. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2575. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2576. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2577. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2578. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2579. goto corrupt_out;
  2580. }
  2581. if (S_ISREG(ip->i_d.di_mode)) {
  2582. if (XFS_TEST_ERROR(
  2583. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2584. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2585. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2586. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2587. "%s: Bad regular inode %Lu, ptr 0x%p",
  2588. __func__, ip->i_ino, ip);
  2589. goto corrupt_out;
  2590. }
  2591. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2592. if (XFS_TEST_ERROR(
  2593. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2594. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2595. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2596. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2597. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2598. "%s: Bad directory inode %Lu, ptr 0x%p",
  2599. __func__, ip->i_ino, ip);
  2600. goto corrupt_out;
  2601. }
  2602. }
  2603. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2604. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2605. XFS_RANDOM_IFLUSH_5)) {
  2606. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2607. "%s: detected corrupt incore inode %Lu, "
  2608. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2609. __func__, ip->i_ino,
  2610. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2611. ip->i_d.di_nblocks, ip);
  2612. goto corrupt_out;
  2613. }
  2614. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2615. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2616. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2617. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2618. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2619. goto corrupt_out;
  2620. }
  2621. /*
  2622. * bump the flush iteration count, used to detect flushes which
  2623. * postdate a log record during recovery. This is redundant as we now
  2624. * log every change and hence this can't happen. Still, it doesn't hurt.
  2625. */
  2626. ip->i_d.di_flushiter++;
  2627. /*
  2628. * Copy the dirty parts of the inode into the on-disk
  2629. * inode. We always copy out the core of the inode,
  2630. * because if the inode is dirty at all the core must
  2631. * be.
  2632. */
  2633. xfs_dinode_to_disk(dip, &ip->i_d);
  2634. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2635. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2636. ip->i_d.di_flushiter = 0;
  2637. /*
  2638. * If this is really an old format inode and the superblock version
  2639. * has not been updated to support only new format inodes, then
  2640. * convert back to the old inode format. If the superblock version
  2641. * has been updated, then make the conversion permanent.
  2642. */
  2643. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2644. if (ip->i_d.di_version == 1) {
  2645. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2646. /*
  2647. * Convert it back.
  2648. */
  2649. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2650. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2651. } else {
  2652. /*
  2653. * The superblock version has already been bumped,
  2654. * so just make the conversion to the new inode
  2655. * format permanent.
  2656. */
  2657. ip->i_d.di_version = 2;
  2658. dip->di_version = 2;
  2659. ip->i_d.di_onlink = 0;
  2660. dip->di_onlink = 0;
  2661. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2662. memset(&(dip->di_pad[0]), 0,
  2663. sizeof(dip->di_pad));
  2664. ASSERT(xfs_get_projid(ip) == 0);
  2665. }
  2666. }
  2667. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2668. if (XFS_IFORK_Q(ip))
  2669. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2670. xfs_inobp_check(mp, bp);
  2671. /*
  2672. * We've recorded everything logged in the inode, so we'd like to clear
  2673. * the ili_fields bits so we don't log and flush things unnecessarily.
  2674. * However, we can't stop logging all this information until the data
  2675. * we've copied into the disk buffer is written to disk. If we did we
  2676. * might overwrite the copy of the inode in the log with all the data
  2677. * after re-logging only part of it, and in the face of a crash we
  2678. * wouldn't have all the data we need to recover.
  2679. *
  2680. * What we do is move the bits to the ili_last_fields field. When
  2681. * logging the inode, these bits are moved back to the ili_fields field.
  2682. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2683. * know that the information those bits represent is permanently on
  2684. * disk. As long as the flush completes before the inode is logged
  2685. * again, then both ili_fields and ili_last_fields will be cleared.
  2686. *
  2687. * We can play with the ili_fields bits here, because the inode lock
  2688. * must be held exclusively in order to set bits there and the flush
  2689. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2690. * done routine can tell whether or not to look in the AIL. Also, store
  2691. * the current LSN of the inode so that we can tell whether the item has
  2692. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2693. * need the AIL lock, because it is a 64 bit value that cannot be read
  2694. * atomically.
  2695. */
  2696. iip->ili_last_fields = iip->ili_fields;
  2697. iip->ili_fields = 0;
  2698. iip->ili_logged = 1;
  2699. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2700. &iip->ili_item.li_lsn);
  2701. /*
  2702. * Attach the function xfs_iflush_done to the inode's
  2703. * buffer. This will remove the inode from the AIL
  2704. * and unlock the inode's flush lock when the inode is
  2705. * completely written to disk.
  2706. */
  2707. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2708. /* update the lsn in the on disk inode if required */
  2709. if (ip->i_d.di_version == 3)
  2710. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  2711. /* generate the checksum. */
  2712. xfs_dinode_calc_crc(mp, dip);
  2713. ASSERT(bp->b_fspriv != NULL);
  2714. ASSERT(bp->b_iodone != NULL);
  2715. return 0;
  2716. corrupt_out:
  2717. return XFS_ERROR(EFSCORRUPTED);
  2718. }
  2719. /*
  2720. * Return a pointer to the extent record at file index idx.
  2721. */
  2722. xfs_bmbt_rec_host_t *
  2723. xfs_iext_get_ext(
  2724. xfs_ifork_t *ifp, /* inode fork pointer */
  2725. xfs_extnum_t idx) /* index of target extent */
  2726. {
  2727. ASSERT(idx >= 0);
  2728. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  2729. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  2730. return ifp->if_u1.if_ext_irec->er_extbuf;
  2731. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2732. xfs_ext_irec_t *erp; /* irec pointer */
  2733. int erp_idx = 0; /* irec index */
  2734. xfs_extnum_t page_idx = idx; /* ext index in target list */
  2735. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2736. return &erp->er_extbuf[page_idx];
  2737. } else if (ifp->if_bytes) {
  2738. return &ifp->if_u1.if_extents[idx];
  2739. } else {
  2740. return NULL;
  2741. }
  2742. }
  2743. /*
  2744. * Insert new item(s) into the extent records for incore inode
  2745. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  2746. */
  2747. void
  2748. xfs_iext_insert(
  2749. xfs_inode_t *ip, /* incore inode pointer */
  2750. xfs_extnum_t idx, /* starting index of new items */
  2751. xfs_extnum_t count, /* number of inserted items */
  2752. xfs_bmbt_irec_t *new, /* items to insert */
  2753. int state) /* type of extent conversion */
  2754. {
  2755. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2756. xfs_extnum_t i; /* extent record index */
  2757. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  2758. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2759. xfs_iext_add(ifp, idx, count);
  2760. for (i = idx; i < idx + count; i++, new++)
  2761. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  2762. }
  2763. /*
  2764. * This is called when the amount of space required for incore file
  2765. * extents needs to be increased. The ext_diff parameter stores the
  2766. * number of new extents being added and the idx parameter contains
  2767. * the extent index where the new extents will be added. If the new
  2768. * extents are being appended, then we just need to (re)allocate and
  2769. * initialize the space. Otherwise, if the new extents are being
  2770. * inserted into the middle of the existing entries, a bit more work
  2771. * is required to make room for the new extents to be inserted. The
  2772. * caller is responsible for filling in the new extent entries upon
  2773. * return.
  2774. */
  2775. void
  2776. xfs_iext_add(
  2777. xfs_ifork_t *ifp, /* inode fork pointer */
  2778. xfs_extnum_t idx, /* index to begin adding exts */
  2779. int ext_diff) /* number of extents to add */
  2780. {
  2781. int byte_diff; /* new bytes being added */
  2782. int new_size; /* size of extents after adding */
  2783. xfs_extnum_t nextents; /* number of extents in file */
  2784. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2785. ASSERT((idx >= 0) && (idx <= nextents));
  2786. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  2787. new_size = ifp->if_bytes + byte_diff;
  2788. /*
  2789. * If the new number of extents (nextents + ext_diff)
  2790. * fits inside the inode, then continue to use the inline
  2791. * extent buffer.
  2792. */
  2793. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  2794. if (idx < nextents) {
  2795. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  2796. &ifp->if_u2.if_inline_ext[idx],
  2797. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2798. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  2799. }
  2800. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2801. ifp->if_real_bytes = 0;
  2802. }
  2803. /*
  2804. * Otherwise use a linear (direct) extent list.
  2805. * If the extents are currently inside the inode,
  2806. * xfs_iext_realloc_direct will switch us from
  2807. * inline to direct extent allocation mode.
  2808. */
  2809. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  2810. xfs_iext_realloc_direct(ifp, new_size);
  2811. if (idx < nextents) {
  2812. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  2813. &ifp->if_u1.if_extents[idx],
  2814. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2815. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  2816. }
  2817. }
  2818. /* Indirection array */
  2819. else {
  2820. xfs_ext_irec_t *erp;
  2821. int erp_idx = 0;
  2822. int page_idx = idx;
  2823. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  2824. if (ifp->if_flags & XFS_IFEXTIREC) {
  2825. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  2826. } else {
  2827. xfs_iext_irec_init(ifp);
  2828. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2829. erp = ifp->if_u1.if_ext_irec;
  2830. }
  2831. /* Extents fit in target extent page */
  2832. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  2833. if (page_idx < erp->er_extcount) {
  2834. memmove(&erp->er_extbuf[page_idx + ext_diff],
  2835. &erp->er_extbuf[page_idx],
  2836. (erp->er_extcount - page_idx) *
  2837. sizeof(xfs_bmbt_rec_t));
  2838. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  2839. }
  2840. erp->er_extcount += ext_diff;
  2841. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2842. }
  2843. /* Insert a new extent page */
  2844. else if (erp) {
  2845. xfs_iext_add_indirect_multi(ifp,
  2846. erp_idx, page_idx, ext_diff);
  2847. }
  2848. /*
  2849. * If extent(s) are being appended to the last page in
  2850. * the indirection array and the new extent(s) don't fit
  2851. * in the page, then erp is NULL and erp_idx is set to
  2852. * the next index needed in the indirection array.
  2853. */
  2854. else {
  2855. int count = ext_diff;
  2856. while (count) {
  2857. erp = xfs_iext_irec_new(ifp, erp_idx);
  2858. erp->er_extcount = count;
  2859. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  2860. if (count) {
  2861. erp_idx++;
  2862. }
  2863. }
  2864. }
  2865. }
  2866. ifp->if_bytes = new_size;
  2867. }
  2868. /*
  2869. * This is called when incore extents are being added to the indirection
  2870. * array and the new extents do not fit in the target extent list. The
  2871. * erp_idx parameter contains the irec index for the target extent list
  2872. * in the indirection array, and the idx parameter contains the extent
  2873. * index within the list. The number of extents being added is stored
  2874. * in the count parameter.
  2875. *
  2876. * |-------| |-------|
  2877. * | | | | idx - number of extents before idx
  2878. * | idx | | count |
  2879. * | | | | count - number of extents being inserted at idx
  2880. * |-------| |-------|
  2881. * | count | | nex2 | nex2 - number of extents after idx + count
  2882. * |-------| |-------|
  2883. */
  2884. void
  2885. xfs_iext_add_indirect_multi(
  2886. xfs_ifork_t *ifp, /* inode fork pointer */
  2887. int erp_idx, /* target extent irec index */
  2888. xfs_extnum_t idx, /* index within target list */
  2889. int count) /* new extents being added */
  2890. {
  2891. int byte_diff; /* new bytes being added */
  2892. xfs_ext_irec_t *erp; /* pointer to irec entry */
  2893. xfs_extnum_t ext_diff; /* number of extents to add */
  2894. xfs_extnum_t ext_cnt; /* new extents still needed */
  2895. xfs_extnum_t nex2; /* extents after idx + count */
  2896. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  2897. int nlists; /* number of irec's (lists) */
  2898. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2899. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2900. nex2 = erp->er_extcount - idx;
  2901. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2902. /*
  2903. * Save second part of target extent list
  2904. * (all extents past */
  2905. if (nex2) {
  2906. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2907. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  2908. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  2909. erp->er_extcount -= nex2;
  2910. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  2911. memset(&erp->er_extbuf[idx], 0, byte_diff);
  2912. }
  2913. /*
  2914. * Add the new extents to the end of the target
  2915. * list, then allocate new irec record(s) and
  2916. * extent buffer(s) as needed to store the rest
  2917. * of the new extents.
  2918. */
  2919. ext_cnt = count;
  2920. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  2921. if (ext_diff) {
  2922. erp->er_extcount += ext_diff;
  2923. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2924. ext_cnt -= ext_diff;
  2925. }
  2926. while (ext_cnt) {
  2927. erp_idx++;
  2928. erp = xfs_iext_irec_new(ifp, erp_idx);
  2929. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  2930. erp->er_extcount = ext_diff;
  2931. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2932. ext_cnt -= ext_diff;
  2933. }
  2934. /* Add nex2 extents back to indirection array */
  2935. if (nex2) {
  2936. xfs_extnum_t ext_avail;
  2937. int i;
  2938. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2939. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  2940. i = 0;
  2941. /*
  2942. * If nex2 extents fit in the current page, append
  2943. * nex2_ep after the new extents.
  2944. */
  2945. if (nex2 <= ext_avail) {
  2946. i = erp->er_extcount;
  2947. }
  2948. /*
  2949. * Otherwise, check if space is available in the
  2950. * next page.
  2951. */
  2952. else if ((erp_idx < nlists - 1) &&
  2953. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  2954. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  2955. erp_idx++;
  2956. erp++;
  2957. /* Create a hole for nex2 extents */
  2958. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  2959. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  2960. }
  2961. /*
  2962. * Final choice, create a new extent page for
  2963. * nex2 extents.
  2964. */
  2965. else {
  2966. erp_idx++;
  2967. erp = xfs_iext_irec_new(ifp, erp_idx);
  2968. }
  2969. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  2970. kmem_free(nex2_ep);
  2971. erp->er_extcount += nex2;
  2972. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  2973. }
  2974. }
  2975. /*
  2976. * This is called when the amount of space required for incore file
  2977. * extents needs to be decreased. The ext_diff parameter stores the
  2978. * number of extents to be removed and the idx parameter contains
  2979. * the extent index where the extents will be removed from.
  2980. *
  2981. * If the amount of space needed has decreased below the linear
  2982. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  2983. * extent array. Otherwise, use kmem_realloc() to adjust the
  2984. * size to what is needed.
  2985. */
  2986. void
  2987. xfs_iext_remove(
  2988. xfs_inode_t *ip, /* incore inode pointer */
  2989. xfs_extnum_t idx, /* index to begin removing exts */
  2990. int ext_diff, /* number of extents to remove */
  2991. int state) /* type of extent conversion */
  2992. {
  2993. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2994. xfs_extnum_t nextents; /* number of extents in file */
  2995. int new_size; /* size of extents after removal */
  2996. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  2997. ASSERT(ext_diff > 0);
  2998. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2999. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  3000. if (new_size == 0) {
  3001. xfs_iext_destroy(ifp);
  3002. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  3003. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  3004. } else if (ifp->if_real_bytes) {
  3005. xfs_iext_remove_direct(ifp, idx, ext_diff);
  3006. } else {
  3007. xfs_iext_remove_inline(ifp, idx, ext_diff);
  3008. }
  3009. ifp->if_bytes = new_size;
  3010. }
  3011. /*
  3012. * This removes ext_diff extents from the inline buffer, beginning
  3013. * at extent index idx.
  3014. */
  3015. void
  3016. xfs_iext_remove_inline(
  3017. xfs_ifork_t *ifp, /* inode fork pointer */
  3018. xfs_extnum_t idx, /* index to begin removing exts */
  3019. int ext_diff) /* number of extents to remove */
  3020. {
  3021. int nextents; /* number of extents in file */
  3022. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3023. ASSERT(idx < XFS_INLINE_EXTS);
  3024. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3025. ASSERT(((nextents - ext_diff) > 0) &&
  3026. (nextents - ext_diff) < XFS_INLINE_EXTS);
  3027. if (idx + ext_diff < nextents) {
  3028. memmove(&ifp->if_u2.if_inline_ext[idx],
  3029. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  3030. (nextents - (idx + ext_diff)) *
  3031. sizeof(xfs_bmbt_rec_t));
  3032. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  3033. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3034. } else {
  3035. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  3036. ext_diff * sizeof(xfs_bmbt_rec_t));
  3037. }
  3038. }
  3039. /*
  3040. * This removes ext_diff extents from a linear (direct) extent list,
  3041. * beginning at extent index idx. If the extents are being removed
  3042. * from the end of the list (ie. truncate) then we just need to re-
  3043. * allocate the list to remove the extra space. Otherwise, if the
  3044. * extents are being removed from the middle of the existing extent
  3045. * entries, then we first need to move the extent records beginning
  3046. * at idx + ext_diff up in the list to overwrite the records being
  3047. * removed, then remove the extra space via kmem_realloc.
  3048. */
  3049. void
  3050. xfs_iext_remove_direct(
  3051. xfs_ifork_t *ifp, /* inode fork pointer */
  3052. xfs_extnum_t idx, /* index to begin removing exts */
  3053. int ext_diff) /* number of extents to remove */
  3054. {
  3055. xfs_extnum_t nextents; /* number of extents in file */
  3056. int new_size; /* size of extents after removal */
  3057. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3058. new_size = ifp->if_bytes -
  3059. (ext_diff * sizeof(xfs_bmbt_rec_t));
  3060. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3061. if (new_size == 0) {
  3062. xfs_iext_destroy(ifp);
  3063. return;
  3064. }
  3065. /* Move extents up in the list (if needed) */
  3066. if (idx + ext_diff < nextents) {
  3067. memmove(&ifp->if_u1.if_extents[idx],
  3068. &ifp->if_u1.if_extents[idx + ext_diff],
  3069. (nextents - (idx + ext_diff)) *
  3070. sizeof(xfs_bmbt_rec_t));
  3071. }
  3072. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  3073. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3074. /*
  3075. * Reallocate the direct extent list. If the extents
  3076. * will fit inside the inode then xfs_iext_realloc_direct
  3077. * will switch from direct to inline extent allocation
  3078. * mode for us.
  3079. */
  3080. xfs_iext_realloc_direct(ifp, new_size);
  3081. ifp->if_bytes = new_size;
  3082. }
  3083. /*
  3084. * This is called when incore extents are being removed from the
  3085. * indirection array and the extents being removed span multiple extent
  3086. * buffers. The idx parameter contains the file extent index where we
  3087. * want to begin removing extents, and the count parameter contains
  3088. * how many extents need to be removed.
  3089. *
  3090. * |-------| |-------|
  3091. * | nex1 | | | nex1 - number of extents before idx
  3092. * |-------| | count |
  3093. * | | | | count - number of extents being removed at idx
  3094. * | count | |-------|
  3095. * | | | nex2 | nex2 - number of extents after idx + count
  3096. * |-------| |-------|
  3097. */
  3098. void
  3099. xfs_iext_remove_indirect(
  3100. xfs_ifork_t *ifp, /* inode fork pointer */
  3101. xfs_extnum_t idx, /* index to begin removing extents */
  3102. int count) /* number of extents to remove */
  3103. {
  3104. xfs_ext_irec_t *erp; /* indirection array pointer */
  3105. int erp_idx = 0; /* indirection array index */
  3106. xfs_extnum_t ext_cnt; /* extents left to remove */
  3107. xfs_extnum_t ext_diff; /* extents to remove in current list */
  3108. xfs_extnum_t nex1; /* number of extents before idx */
  3109. xfs_extnum_t nex2; /* extents after idx + count */
  3110. int page_idx = idx; /* index in target extent list */
  3111. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3112. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3113. ASSERT(erp != NULL);
  3114. nex1 = page_idx;
  3115. ext_cnt = count;
  3116. while (ext_cnt) {
  3117. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  3118. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  3119. /*
  3120. * Check for deletion of entire list;
  3121. * xfs_iext_irec_remove() updates extent offsets.
  3122. */
  3123. if (ext_diff == erp->er_extcount) {
  3124. xfs_iext_irec_remove(ifp, erp_idx);
  3125. ext_cnt -= ext_diff;
  3126. nex1 = 0;
  3127. if (ext_cnt) {
  3128. ASSERT(erp_idx < ifp->if_real_bytes /
  3129. XFS_IEXT_BUFSZ);
  3130. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3131. nex1 = 0;
  3132. continue;
  3133. } else {
  3134. break;
  3135. }
  3136. }
  3137. /* Move extents up (if needed) */
  3138. if (nex2) {
  3139. memmove(&erp->er_extbuf[nex1],
  3140. &erp->er_extbuf[nex1 + ext_diff],
  3141. nex2 * sizeof(xfs_bmbt_rec_t));
  3142. }
  3143. /* Zero out rest of page */
  3144. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  3145. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  3146. /* Update remaining counters */
  3147. erp->er_extcount -= ext_diff;
  3148. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  3149. ext_cnt -= ext_diff;
  3150. nex1 = 0;
  3151. erp_idx++;
  3152. erp++;
  3153. }
  3154. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  3155. xfs_iext_irec_compact(ifp);
  3156. }
  3157. /*
  3158. * Create, destroy, or resize a linear (direct) block of extents.
  3159. */
  3160. void
  3161. xfs_iext_realloc_direct(
  3162. xfs_ifork_t *ifp, /* inode fork pointer */
  3163. int new_size) /* new size of extents */
  3164. {
  3165. int rnew_size; /* real new size of extents */
  3166. rnew_size = new_size;
  3167. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  3168. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  3169. (new_size != ifp->if_real_bytes)));
  3170. /* Free extent records */
  3171. if (new_size == 0) {
  3172. xfs_iext_destroy(ifp);
  3173. }
  3174. /* Resize direct extent list and zero any new bytes */
  3175. else if (ifp->if_real_bytes) {
  3176. /* Check if extents will fit inside the inode */
  3177. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  3178. xfs_iext_direct_to_inline(ifp, new_size /
  3179. (uint)sizeof(xfs_bmbt_rec_t));
  3180. ifp->if_bytes = new_size;
  3181. return;
  3182. }
  3183. if (!is_power_of_2(new_size)){
  3184. rnew_size = roundup_pow_of_two(new_size);
  3185. }
  3186. if (rnew_size != ifp->if_real_bytes) {
  3187. ifp->if_u1.if_extents =
  3188. kmem_realloc(ifp->if_u1.if_extents,
  3189. rnew_size,
  3190. ifp->if_real_bytes, KM_NOFS);
  3191. }
  3192. if (rnew_size > ifp->if_real_bytes) {
  3193. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  3194. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  3195. rnew_size - ifp->if_real_bytes);
  3196. }
  3197. }
  3198. /*
  3199. * Switch from the inline extent buffer to a direct
  3200. * extent list. Be sure to include the inline extent
  3201. * bytes in new_size.
  3202. */
  3203. else {
  3204. new_size += ifp->if_bytes;
  3205. if (!is_power_of_2(new_size)) {
  3206. rnew_size = roundup_pow_of_two(new_size);
  3207. }
  3208. xfs_iext_inline_to_direct(ifp, rnew_size);
  3209. }
  3210. ifp->if_real_bytes = rnew_size;
  3211. ifp->if_bytes = new_size;
  3212. }
  3213. /*
  3214. * Switch from linear (direct) extent records to inline buffer.
  3215. */
  3216. void
  3217. xfs_iext_direct_to_inline(
  3218. xfs_ifork_t *ifp, /* inode fork pointer */
  3219. xfs_extnum_t nextents) /* number of extents in file */
  3220. {
  3221. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3222. ASSERT(nextents <= XFS_INLINE_EXTS);
  3223. /*
  3224. * The inline buffer was zeroed when we switched
  3225. * from inline to direct extent allocation mode,
  3226. * so we don't need to clear it here.
  3227. */
  3228. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  3229. nextents * sizeof(xfs_bmbt_rec_t));
  3230. kmem_free(ifp->if_u1.if_extents);
  3231. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3232. ifp->if_real_bytes = 0;
  3233. }
  3234. /*
  3235. * Switch from inline buffer to linear (direct) extent records.
  3236. * new_size should already be rounded up to the next power of 2
  3237. * by the caller (when appropriate), so use new_size as it is.
  3238. * However, since new_size may be rounded up, we can't update
  3239. * if_bytes here. It is the caller's responsibility to update
  3240. * if_bytes upon return.
  3241. */
  3242. void
  3243. xfs_iext_inline_to_direct(
  3244. xfs_ifork_t *ifp, /* inode fork pointer */
  3245. int new_size) /* number of extents in file */
  3246. {
  3247. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  3248. memset(ifp->if_u1.if_extents, 0, new_size);
  3249. if (ifp->if_bytes) {
  3250. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  3251. ifp->if_bytes);
  3252. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3253. sizeof(xfs_bmbt_rec_t));
  3254. }
  3255. ifp->if_real_bytes = new_size;
  3256. }
  3257. /*
  3258. * Resize an extent indirection array to new_size bytes.
  3259. */
  3260. STATIC void
  3261. xfs_iext_realloc_indirect(
  3262. xfs_ifork_t *ifp, /* inode fork pointer */
  3263. int new_size) /* new indirection array size */
  3264. {
  3265. int nlists; /* number of irec's (ex lists) */
  3266. int size; /* current indirection array size */
  3267. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3268. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3269. size = nlists * sizeof(xfs_ext_irec_t);
  3270. ASSERT(ifp->if_real_bytes);
  3271. ASSERT((new_size >= 0) && (new_size != size));
  3272. if (new_size == 0) {
  3273. xfs_iext_destroy(ifp);
  3274. } else {
  3275. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  3276. kmem_realloc(ifp->if_u1.if_ext_irec,
  3277. new_size, size, KM_NOFS);
  3278. }
  3279. }
  3280. /*
  3281. * Switch from indirection array to linear (direct) extent allocations.
  3282. */
  3283. STATIC void
  3284. xfs_iext_indirect_to_direct(
  3285. xfs_ifork_t *ifp) /* inode fork pointer */
  3286. {
  3287. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3288. xfs_extnum_t nextents; /* number of extents in file */
  3289. int size; /* size of file extents */
  3290. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3291. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3292. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3293. size = nextents * sizeof(xfs_bmbt_rec_t);
  3294. xfs_iext_irec_compact_pages(ifp);
  3295. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  3296. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  3297. kmem_free(ifp->if_u1.if_ext_irec);
  3298. ifp->if_flags &= ~XFS_IFEXTIREC;
  3299. ifp->if_u1.if_extents = ep;
  3300. ifp->if_bytes = size;
  3301. if (nextents < XFS_LINEAR_EXTS) {
  3302. xfs_iext_realloc_direct(ifp, size);
  3303. }
  3304. }
  3305. /*
  3306. * Free incore file extents.
  3307. */
  3308. void
  3309. xfs_iext_destroy(
  3310. xfs_ifork_t *ifp) /* inode fork pointer */
  3311. {
  3312. if (ifp->if_flags & XFS_IFEXTIREC) {
  3313. int erp_idx;
  3314. int nlists;
  3315. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3316. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3317. xfs_iext_irec_remove(ifp, erp_idx);
  3318. }
  3319. ifp->if_flags &= ~XFS_IFEXTIREC;
  3320. } else if (ifp->if_real_bytes) {
  3321. kmem_free(ifp->if_u1.if_extents);
  3322. } else if (ifp->if_bytes) {
  3323. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3324. sizeof(xfs_bmbt_rec_t));
  3325. }
  3326. ifp->if_u1.if_extents = NULL;
  3327. ifp->if_real_bytes = 0;
  3328. ifp->if_bytes = 0;
  3329. }
  3330. /*
  3331. * Return a pointer to the extent record for file system block bno.
  3332. */
  3333. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3334. xfs_iext_bno_to_ext(
  3335. xfs_ifork_t *ifp, /* inode fork pointer */
  3336. xfs_fileoff_t bno, /* block number to search for */
  3337. xfs_extnum_t *idxp) /* index of target extent */
  3338. {
  3339. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3340. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3341. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3342. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3343. int high; /* upper boundary in search */
  3344. xfs_extnum_t idx = 0; /* index of target extent */
  3345. int low; /* lower boundary in search */
  3346. xfs_extnum_t nextents; /* number of file extents */
  3347. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3348. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3349. if (nextents == 0) {
  3350. *idxp = 0;
  3351. return NULL;
  3352. }
  3353. low = 0;
  3354. if (ifp->if_flags & XFS_IFEXTIREC) {
  3355. /* Find target extent list */
  3356. int erp_idx = 0;
  3357. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3358. base = erp->er_extbuf;
  3359. high = erp->er_extcount - 1;
  3360. } else {
  3361. base = ifp->if_u1.if_extents;
  3362. high = nextents - 1;
  3363. }
  3364. /* Binary search extent records */
  3365. while (low <= high) {
  3366. idx = (low + high) >> 1;
  3367. ep = base + idx;
  3368. startoff = xfs_bmbt_get_startoff(ep);
  3369. blockcount = xfs_bmbt_get_blockcount(ep);
  3370. if (bno < startoff) {
  3371. high = idx - 1;
  3372. } else if (bno >= startoff + blockcount) {
  3373. low = idx + 1;
  3374. } else {
  3375. /* Convert back to file-based extent index */
  3376. if (ifp->if_flags & XFS_IFEXTIREC) {
  3377. idx += erp->er_extoff;
  3378. }
  3379. *idxp = idx;
  3380. return ep;
  3381. }
  3382. }
  3383. /* Convert back to file-based extent index */
  3384. if (ifp->if_flags & XFS_IFEXTIREC) {
  3385. idx += erp->er_extoff;
  3386. }
  3387. if (bno >= startoff + blockcount) {
  3388. if (++idx == nextents) {
  3389. ep = NULL;
  3390. } else {
  3391. ep = xfs_iext_get_ext(ifp, idx);
  3392. }
  3393. }
  3394. *idxp = idx;
  3395. return ep;
  3396. }
  3397. /*
  3398. * Return a pointer to the indirection array entry containing the
  3399. * extent record for filesystem block bno. Store the index of the
  3400. * target irec in *erp_idxp.
  3401. */
  3402. xfs_ext_irec_t * /* pointer to found extent record */
  3403. xfs_iext_bno_to_irec(
  3404. xfs_ifork_t *ifp, /* inode fork pointer */
  3405. xfs_fileoff_t bno, /* block number to search for */
  3406. int *erp_idxp) /* irec index of target ext list */
  3407. {
  3408. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3409. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3410. int erp_idx; /* indirection array index */
  3411. int nlists; /* number of extent irec's (lists) */
  3412. int high; /* binary search upper limit */
  3413. int low; /* binary search lower limit */
  3414. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3415. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3416. erp_idx = 0;
  3417. low = 0;
  3418. high = nlists - 1;
  3419. while (low <= high) {
  3420. erp_idx = (low + high) >> 1;
  3421. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3422. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3423. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3424. high = erp_idx - 1;
  3425. } else if (erp_next && bno >=
  3426. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3427. low = erp_idx + 1;
  3428. } else {
  3429. break;
  3430. }
  3431. }
  3432. *erp_idxp = erp_idx;
  3433. return erp;
  3434. }
  3435. /*
  3436. * Return a pointer to the indirection array entry containing the
  3437. * extent record at file extent index *idxp. Store the index of the
  3438. * target irec in *erp_idxp and store the page index of the target
  3439. * extent record in *idxp.
  3440. */
  3441. xfs_ext_irec_t *
  3442. xfs_iext_idx_to_irec(
  3443. xfs_ifork_t *ifp, /* inode fork pointer */
  3444. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3445. int *erp_idxp, /* pointer to target irec */
  3446. int realloc) /* new bytes were just added */
  3447. {
  3448. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3449. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3450. int erp_idx; /* indirection array index */
  3451. int nlists; /* number of irec's (ex lists) */
  3452. int high; /* binary search upper limit */
  3453. int low; /* binary search lower limit */
  3454. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3455. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3456. ASSERT(page_idx >= 0);
  3457. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  3458. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  3459. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3460. erp_idx = 0;
  3461. low = 0;
  3462. high = nlists - 1;
  3463. /* Binary search extent irec's */
  3464. while (low <= high) {
  3465. erp_idx = (low + high) >> 1;
  3466. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3467. prev = erp_idx > 0 ? erp - 1 : NULL;
  3468. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3469. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3470. high = erp_idx - 1;
  3471. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3472. (page_idx == erp->er_extoff + erp->er_extcount &&
  3473. !realloc)) {
  3474. low = erp_idx + 1;
  3475. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3476. erp->er_extcount == XFS_LINEAR_EXTS) {
  3477. ASSERT(realloc);
  3478. page_idx = 0;
  3479. erp_idx++;
  3480. erp = erp_idx < nlists ? erp + 1 : NULL;
  3481. break;
  3482. } else {
  3483. page_idx -= erp->er_extoff;
  3484. break;
  3485. }
  3486. }
  3487. *idxp = page_idx;
  3488. *erp_idxp = erp_idx;
  3489. return(erp);
  3490. }
  3491. /*
  3492. * Allocate and initialize an indirection array once the space needed
  3493. * for incore extents increases above XFS_IEXT_BUFSZ.
  3494. */
  3495. void
  3496. xfs_iext_irec_init(
  3497. xfs_ifork_t *ifp) /* inode fork pointer */
  3498. {
  3499. xfs_ext_irec_t *erp; /* indirection array pointer */
  3500. xfs_extnum_t nextents; /* number of extents in file */
  3501. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3502. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3503. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3504. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3505. if (nextents == 0) {
  3506. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3507. } else if (!ifp->if_real_bytes) {
  3508. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3509. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3510. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3511. }
  3512. erp->er_extbuf = ifp->if_u1.if_extents;
  3513. erp->er_extcount = nextents;
  3514. erp->er_extoff = 0;
  3515. ifp->if_flags |= XFS_IFEXTIREC;
  3516. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3517. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3518. ifp->if_u1.if_ext_irec = erp;
  3519. return;
  3520. }
  3521. /*
  3522. * Allocate and initialize a new entry in the indirection array.
  3523. */
  3524. xfs_ext_irec_t *
  3525. xfs_iext_irec_new(
  3526. xfs_ifork_t *ifp, /* inode fork pointer */
  3527. int erp_idx) /* index for new irec */
  3528. {
  3529. xfs_ext_irec_t *erp; /* indirection array pointer */
  3530. int i; /* loop counter */
  3531. int nlists; /* number of irec's (ex lists) */
  3532. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3533. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3534. /* Resize indirection array */
  3535. xfs_iext_realloc_indirect(ifp, ++nlists *
  3536. sizeof(xfs_ext_irec_t));
  3537. /*
  3538. * Move records down in the array so the
  3539. * new page can use erp_idx.
  3540. */
  3541. erp = ifp->if_u1.if_ext_irec;
  3542. for (i = nlists - 1; i > erp_idx; i--) {
  3543. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3544. }
  3545. ASSERT(i == erp_idx);
  3546. /* Initialize new extent record */
  3547. erp = ifp->if_u1.if_ext_irec;
  3548. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3549. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3550. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3551. erp[erp_idx].er_extcount = 0;
  3552. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3553. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3554. return (&erp[erp_idx]);
  3555. }
  3556. /*
  3557. * Remove a record from the indirection array.
  3558. */
  3559. void
  3560. xfs_iext_irec_remove(
  3561. xfs_ifork_t *ifp, /* inode fork pointer */
  3562. int erp_idx) /* irec index to remove */
  3563. {
  3564. xfs_ext_irec_t *erp; /* indirection array pointer */
  3565. int i; /* loop counter */
  3566. int nlists; /* number of irec's (ex lists) */
  3567. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3568. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3569. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3570. if (erp->er_extbuf) {
  3571. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3572. -erp->er_extcount);
  3573. kmem_free(erp->er_extbuf);
  3574. }
  3575. /* Compact extent records */
  3576. erp = ifp->if_u1.if_ext_irec;
  3577. for (i = erp_idx; i < nlists - 1; i++) {
  3578. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  3579. }
  3580. /*
  3581. * Manually free the last extent record from the indirection
  3582. * array. A call to xfs_iext_realloc_indirect() with a size
  3583. * of zero would result in a call to xfs_iext_destroy() which
  3584. * would in turn call this function again, creating a nasty
  3585. * infinite loop.
  3586. */
  3587. if (--nlists) {
  3588. xfs_iext_realloc_indirect(ifp,
  3589. nlists * sizeof(xfs_ext_irec_t));
  3590. } else {
  3591. kmem_free(ifp->if_u1.if_ext_irec);
  3592. }
  3593. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3594. }
  3595. /*
  3596. * This is called to clean up large amounts of unused memory allocated
  3597. * by the indirection array. Before compacting anything though, verify
  3598. * that the indirection array is still needed and switch back to the
  3599. * linear extent list (or even the inline buffer) if possible. The
  3600. * compaction policy is as follows:
  3601. *
  3602. * Full Compaction: Extents fit into a single page (or inline buffer)
  3603. * Partial Compaction: Extents occupy less than 50% of allocated space
  3604. * No Compaction: Extents occupy at least 50% of allocated space
  3605. */
  3606. void
  3607. xfs_iext_irec_compact(
  3608. xfs_ifork_t *ifp) /* inode fork pointer */
  3609. {
  3610. xfs_extnum_t nextents; /* number of extents in file */
  3611. int nlists; /* number of irec's (ex lists) */
  3612. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3613. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3614. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3615. if (nextents == 0) {
  3616. xfs_iext_destroy(ifp);
  3617. } else if (nextents <= XFS_INLINE_EXTS) {
  3618. xfs_iext_indirect_to_direct(ifp);
  3619. xfs_iext_direct_to_inline(ifp, nextents);
  3620. } else if (nextents <= XFS_LINEAR_EXTS) {
  3621. xfs_iext_indirect_to_direct(ifp);
  3622. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  3623. xfs_iext_irec_compact_pages(ifp);
  3624. }
  3625. }
  3626. /*
  3627. * Combine extents from neighboring extent pages.
  3628. */
  3629. void
  3630. xfs_iext_irec_compact_pages(
  3631. xfs_ifork_t *ifp) /* inode fork pointer */
  3632. {
  3633. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  3634. int erp_idx = 0; /* indirection array index */
  3635. int nlists; /* number of irec's (ex lists) */
  3636. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3637. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3638. while (erp_idx < nlists - 1) {
  3639. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3640. erp_next = erp + 1;
  3641. if (erp_next->er_extcount <=
  3642. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  3643. memcpy(&erp->er_extbuf[erp->er_extcount],
  3644. erp_next->er_extbuf, erp_next->er_extcount *
  3645. sizeof(xfs_bmbt_rec_t));
  3646. erp->er_extcount += erp_next->er_extcount;
  3647. /*
  3648. * Free page before removing extent record
  3649. * so er_extoffs don't get modified in
  3650. * xfs_iext_irec_remove.
  3651. */
  3652. kmem_free(erp_next->er_extbuf);
  3653. erp_next->er_extbuf = NULL;
  3654. xfs_iext_irec_remove(ifp, erp_idx + 1);
  3655. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3656. } else {
  3657. erp_idx++;
  3658. }
  3659. }
  3660. }
  3661. /*
  3662. * This is called to update the er_extoff field in the indirection
  3663. * array when extents have been added or removed from one of the
  3664. * extent lists. erp_idx contains the irec index to begin updating
  3665. * at and ext_diff contains the number of extents that were added
  3666. * or removed.
  3667. */
  3668. void
  3669. xfs_iext_irec_update_extoffs(
  3670. xfs_ifork_t *ifp, /* inode fork pointer */
  3671. int erp_idx, /* irec index to update */
  3672. int ext_diff) /* number of new extents */
  3673. {
  3674. int i; /* loop counter */
  3675. int nlists; /* number of irec's (ex lists */
  3676. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3677. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3678. for (i = erp_idx; i < nlists; i++) {
  3679. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  3680. }
  3681. }
  3682. /*
  3683. * Test whether it is appropriate to check an inode for and free post EOF
  3684. * blocks. The 'force' parameter determines whether we should also consider
  3685. * regular files that are marked preallocated or append-only.
  3686. */
  3687. bool
  3688. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  3689. {
  3690. /* prealloc/delalloc exists only on regular files */
  3691. if (!S_ISREG(ip->i_d.di_mode))
  3692. return false;
  3693. /*
  3694. * Zero sized files with no cached pages and delalloc blocks will not
  3695. * have speculative prealloc/delalloc blocks to remove.
  3696. */
  3697. if (VFS_I(ip)->i_size == 0 &&
  3698. VN_CACHED(VFS_I(ip)) == 0 &&
  3699. ip->i_delayed_blks == 0)
  3700. return false;
  3701. /* If we haven't read in the extent list, then don't do it now. */
  3702. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  3703. return false;
  3704. /*
  3705. * Do not free real preallocated or append-only files unless the file
  3706. * has delalloc blocks and we are forced to remove them.
  3707. */
  3708. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  3709. if (!force || ip->i_delayed_blks == 0)
  3710. return false;
  3711. return true;
  3712. }