xfs_inode.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050
  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. #ifdef DEBUG
  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. int
  942. xfs_iread(
  943. xfs_mount_t *mp,
  944. xfs_trans_t *tp,
  945. xfs_inode_t *ip,
  946. uint iget_flags)
  947. {
  948. xfs_buf_t *bp;
  949. xfs_dinode_t *dip;
  950. int error;
  951. /*
  952. * Fill in the location information in the in-core inode.
  953. */
  954. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
  955. if (error)
  956. return error;
  957. /*
  958. * Get pointers to the on-disk inode and the buffer containing it.
  959. */
  960. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
  961. if (error)
  962. return error;
  963. /* even unallocated inodes are verified */
  964. if (!xfs_dinode_verify(mp, ip, dip)) {
  965. xfs_alert(mp, "%s: validation failed for inode %lld failed",
  966. __func__, ip->i_ino);
  967. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
  968. error = XFS_ERROR(EFSCORRUPTED);
  969. goto out_brelse;
  970. }
  971. /*
  972. * If the on-disk inode is already linked to a directory
  973. * entry, copy all of the inode into the in-core inode.
  974. * xfs_iformat() handles copying in the inode format
  975. * specific information.
  976. * Otherwise, just get the truly permanent information.
  977. */
  978. if (dip->di_mode) {
  979. xfs_dinode_from_disk(&ip->i_d, dip);
  980. error = xfs_iformat(ip, dip);
  981. if (error) {
  982. #ifdef DEBUG
  983. xfs_alert(mp, "%s: xfs_iformat() returned error %d",
  984. __func__, error);
  985. #endif /* DEBUG */
  986. goto out_brelse;
  987. }
  988. } else {
  989. /*
  990. * Partial initialisation of the in-core inode. Just the bits
  991. * that xfs_ialloc won't overwrite or relies on being correct.
  992. */
  993. ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
  994. ip->i_d.di_version = dip->di_version;
  995. ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
  996. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  997. if (dip->di_version == 3) {
  998. ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
  999. uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
  1000. }
  1001. /*
  1002. * Make sure to pull in the mode here as well in
  1003. * case the inode is released without being used.
  1004. * This ensures that xfs_inactive() will see that
  1005. * the inode is already free and not try to mess
  1006. * with the uninitialized part of it.
  1007. */
  1008. ip->i_d.di_mode = 0;
  1009. }
  1010. /*
  1011. * The inode format changed when we moved the link count and
  1012. * made it 32 bits long. If this is an old format inode,
  1013. * convert it in memory to look like a new one. If it gets
  1014. * flushed to disk we will convert back before flushing or
  1015. * logging it. We zero out the new projid field and the old link
  1016. * count field. We'll handle clearing the pad field (the remains
  1017. * of the old uuid field) when we actually convert the inode to
  1018. * the new format. We don't change the version number so that we
  1019. * can distinguish this from a real new format inode.
  1020. */
  1021. if (ip->i_d.di_version == 1) {
  1022. ip->i_d.di_nlink = ip->i_d.di_onlink;
  1023. ip->i_d.di_onlink = 0;
  1024. xfs_set_projid(ip, 0);
  1025. }
  1026. ip->i_delayed_blks = 0;
  1027. /*
  1028. * Mark the buffer containing the inode as something to keep
  1029. * around for a while. This helps to keep recently accessed
  1030. * meta-data in-core longer.
  1031. */
  1032. xfs_buf_set_ref(bp, XFS_INO_REF);
  1033. /*
  1034. * Use xfs_trans_brelse() to release the buffer containing the
  1035. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  1036. * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
  1037. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  1038. * will only release the buffer if it is not dirty within the
  1039. * transaction. It will be OK to release the buffer in this case,
  1040. * because inodes on disk are never destroyed and we will be
  1041. * locking the new in-core inode before putting it in the hash
  1042. * table where other processes can find it. Thus we don't have
  1043. * to worry about the inode being changed just because we released
  1044. * the buffer.
  1045. */
  1046. out_brelse:
  1047. xfs_trans_brelse(tp, bp);
  1048. return error;
  1049. }
  1050. /*
  1051. * Read in extents from a btree-format inode.
  1052. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  1053. */
  1054. int
  1055. xfs_iread_extents(
  1056. xfs_trans_t *tp,
  1057. xfs_inode_t *ip,
  1058. int whichfork)
  1059. {
  1060. int error;
  1061. xfs_ifork_t *ifp;
  1062. xfs_extnum_t nextents;
  1063. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  1064. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  1065. ip->i_mount);
  1066. return XFS_ERROR(EFSCORRUPTED);
  1067. }
  1068. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  1069. ifp = XFS_IFORK_PTR(ip, whichfork);
  1070. /*
  1071. * We know that the size is valid (it's checked in iformat_btree)
  1072. */
  1073. ifp->if_bytes = ifp->if_real_bytes = 0;
  1074. ifp->if_flags |= XFS_IFEXTENTS;
  1075. xfs_iext_add(ifp, 0, nextents);
  1076. error = xfs_bmap_read_extents(tp, ip, whichfork);
  1077. if (error) {
  1078. xfs_iext_destroy(ifp);
  1079. ifp->if_flags &= ~XFS_IFEXTENTS;
  1080. return error;
  1081. }
  1082. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  1083. return 0;
  1084. }
  1085. /*
  1086. * Allocate an inode on disk and return a copy of its in-core version.
  1087. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  1088. * appropriately within the inode. The uid and gid for the inode are
  1089. * set according to the contents of the given cred structure.
  1090. *
  1091. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  1092. * has a free inode available, call xfs_iget() to obtain the in-core
  1093. * version of the allocated inode. Finally, fill in the inode and
  1094. * log its initial contents. In this case, ialloc_context would be
  1095. * set to NULL.
  1096. *
  1097. * If xfs_dialloc() does not have an available inode, it will replenish
  1098. * its supply by doing an allocation. Since we can only do one
  1099. * allocation within a transaction without deadlocks, we must commit
  1100. * the current transaction before returning the inode itself.
  1101. * In this case, therefore, we will set ialloc_context and return.
  1102. * The caller should then commit the current transaction, start a new
  1103. * transaction, and call xfs_ialloc() again to actually get the inode.
  1104. *
  1105. * To ensure that some other process does not grab the inode that
  1106. * was allocated during the first call to xfs_ialloc(), this routine
  1107. * also returns the [locked] bp pointing to the head of the freelist
  1108. * as ialloc_context. The caller should hold this buffer across
  1109. * the commit and pass it back into this routine on the second call.
  1110. *
  1111. * If we are allocating quota inodes, we do not have a parent inode
  1112. * to attach to or associate with (i.e. pip == NULL) because they
  1113. * are not linked into the directory structure - they are attached
  1114. * directly to the superblock - and so have no parent.
  1115. */
  1116. int
  1117. xfs_ialloc(
  1118. xfs_trans_t *tp,
  1119. xfs_inode_t *pip,
  1120. umode_t mode,
  1121. xfs_nlink_t nlink,
  1122. xfs_dev_t rdev,
  1123. prid_t prid,
  1124. int okalloc,
  1125. xfs_buf_t **ialloc_context,
  1126. xfs_inode_t **ipp)
  1127. {
  1128. struct xfs_mount *mp = tp->t_mountp;
  1129. xfs_ino_t ino;
  1130. xfs_inode_t *ip;
  1131. uint flags;
  1132. int error;
  1133. timespec_t tv;
  1134. int filestreams = 0;
  1135. /*
  1136. * Call the space management code to pick
  1137. * the on-disk inode to be allocated.
  1138. */
  1139. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  1140. ialloc_context, &ino);
  1141. if (error)
  1142. return error;
  1143. if (*ialloc_context || ino == NULLFSINO) {
  1144. *ipp = NULL;
  1145. return 0;
  1146. }
  1147. ASSERT(*ialloc_context == NULL);
  1148. /*
  1149. * Get the in-core inode with the lock held exclusively.
  1150. * This is because we're setting fields here we need
  1151. * to prevent others from looking at until we're done.
  1152. */
  1153. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  1154. XFS_ILOCK_EXCL, &ip);
  1155. if (error)
  1156. return error;
  1157. ASSERT(ip != NULL);
  1158. ip->i_d.di_mode = mode;
  1159. ip->i_d.di_onlink = 0;
  1160. ip->i_d.di_nlink = nlink;
  1161. ASSERT(ip->i_d.di_nlink == nlink);
  1162. ip->i_d.di_uid = current_fsuid();
  1163. ip->i_d.di_gid = current_fsgid();
  1164. xfs_set_projid(ip, prid);
  1165. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  1166. /*
  1167. * If the superblock version is up to where we support new format
  1168. * inodes and this is currently an old format inode, then change
  1169. * the inode version number now. This way we only do the conversion
  1170. * here rather than here and in the flush/logging code.
  1171. */
  1172. if (xfs_sb_version_hasnlink(&mp->m_sb) &&
  1173. ip->i_d.di_version == 1) {
  1174. ip->i_d.di_version = 2;
  1175. /*
  1176. * We've already zeroed the old link count, the projid field,
  1177. * and the pad field.
  1178. */
  1179. }
  1180. /*
  1181. * Project ids won't be stored on disk if we are using a version 1 inode.
  1182. */
  1183. if ((prid != 0) && (ip->i_d.di_version == 1))
  1184. xfs_bump_ino_vers2(tp, ip);
  1185. if (pip && XFS_INHERIT_GID(pip)) {
  1186. ip->i_d.di_gid = pip->i_d.di_gid;
  1187. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  1188. ip->i_d.di_mode |= S_ISGID;
  1189. }
  1190. }
  1191. /*
  1192. * If the group ID of the new file does not match the effective group
  1193. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  1194. * (and only if the irix_sgid_inherit compatibility variable is set).
  1195. */
  1196. if ((irix_sgid_inherit) &&
  1197. (ip->i_d.di_mode & S_ISGID) &&
  1198. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  1199. ip->i_d.di_mode &= ~S_ISGID;
  1200. }
  1201. ip->i_d.di_size = 0;
  1202. ip->i_d.di_nextents = 0;
  1203. ASSERT(ip->i_d.di_nblocks == 0);
  1204. nanotime(&tv);
  1205. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  1206. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  1207. ip->i_d.di_atime = ip->i_d.di_mtime;
  1208. ip->i_d.di_ctime = ip->i_d.di_mtime;
  1209. /*
  1210. * di_gen will have been taken care of in xfs_iread.
  1211. */
  1212. ip->i_d.di_extsize = 0;
  1213. ip->i_d.di_dmevmask = 0;
  1214. ip->i_d.di_dmstate = 0;
  1215. ip->i_d.di_flags = 0;
  1216. if (ip->i_d.di_version == 3) {
  1217. ASSERT(ip->i_d.di_ino == ino);
  1218. ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
  1219. ip->i_d.di_crc = 0;
  1220. ip->i_d.di_changecount = 1;
  1221. ip->i_d.di_lsn = 0;
  1222. ip->i_d.di_flags2 = 0;
  1223. memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
  1224. ip->i_d.di_crtime = ip->i_d.di_mtime;
  1225. }
  1226. flags = XFS_ILOG_CORE;
  1227. switch (mode & S_IFMT) {
  1228. case S_IFIFO:
  1229. case S_IFCHR:
  1230. case S_IFBLK:
  1231. case S_IFSOCK:
  1232. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  1233. ip->i_df.if_u2.if_rdev = rdev;
  1234. ip->i_df.if_flags = 0;
  1235. flags |= XFS_ILOG_DEV;
  1236. break;
  1237. case S_IFREG:
  1238. /*
  1239. * we can't set up filestreams until after the VFS inode
  1240. * is set up properly.
  1241. */
  1242. if (pip && xfs_inode_is_filestream(pip))
  1243. filestreams = 1;
  1244. /* fall through */
  1245. case S_IFDIR:
  1246. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  1247. uint di_flags = 0;
  1248. if (S_ISDIR(mode)) {
  1249. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1250. di_flags |= XFS_DIFLAG_RTINHERIT;
  1251. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1252. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  1253. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1254. }
  1255. } else if (S_ISREG(mode)) {
  1256. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1257. di_flags |= XFS_DIFLAG_REALTIME;
  1258. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1259. di_flags |= XFS_DIFLAG_EXTSIZE;
  1260. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1261. }
  1262. }
  1263. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1264. xfs_inherit_noatime)
  1265. di_flags |= XFS_DIFLAG_NOATIME;
  1266. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1267. xfs_inherit_nodump)
  1268. di_flags |= XFS_DIFLAG_NODUMP;
  1269. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1270. xfs_inherit_sync)
  1271. di_flags |= XFS_DIFLAG_SYNC;
  1272. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1273. xfs_inherit_nosymlinks)
  1274. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1275. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1276. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1277. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  1278. xfs_inherit_nodefrag)
  1279. di_flags |= XFS_DIFLAG_NODEFRAG;
  1280. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  1281. di_flags |= XFS_DIFLAG_FILESTREAM;
  1282. ip->i_d.di_flags |= di_flags;
  1283. }
  1284. /* FALLTHROUGH */
  1285. case S_IFLNK:
  1286. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1287. ip->i_df.if_flags = XFS_IFEXTENTS;
  1288. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1289. ip->i_df.if_u1.if_extents = NULL;
  1290. break;
  1291. default:
  1292. ASSERT(0);
  1293. }
  1294. /*
  1295. * Attribute fork settings for new inode.
  1296. */
  1297. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1298. ip->i_d.di_anextents = 0;
  1299. /*
  1300. * Log the new values stuffed into the inode.
  1301. */
  1302. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1303. xfs_trans_log_inode(tp, ip, flags);
  1304. /* now that we have an i_mode we can setup inode ops and unlock */
  1305. xfs_setup_inode(ip);
  1306. /* now we have set up the vfs inode we can associate the filestream */
  1307. if (filestreams) {
  1308. error = xfs_filestream_associate(pip, ip);
  1309. if (error < 0)
  1310. return -error;
  1311. if (!error)
  1312. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1313. }
  1314. *ipp = ip;
  1315. return 0;
  1316. }
  1317. /*
  1318. * Free up the underlying blocks past new_size. The new size must be smaller
  1319. * than the current size. This routine can be used both for the attribute and
  1320. * data fork, and does not modify the inode size, which is left to the caller.
  1321. *
  1322. * The transaction passed to this routine must have made a permanent log
  1323. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1324. * given transaction and start new ones, so make sure everything involved in
  1325. * the transaction is tidy before calling here. Some transaction will be
  1326. * returned to the caller to be committed. The incoming transaction must
  1327. * already include the inode, and both inode locks must be held exclusively.
  1328. * The inode must also be "held" within the transaction. On return the inode
  1329. * will be "held" within the returned transaction. This routine does NOT
  1330. * require any disk space to be reserved for it within the transaction.
  1331. *
  1332. * If we get an error, we must return with the inode locked and linked into the
  1333. * current transaction. This keeps things simple for the higher level code,
  1334. * because it always knows that the inode is locked and held in the transaction
  1335. * that returns to it whether errors occur or not. We don't mark the inode
  1336. * dirty on error so that transactions can be easily aborted if possible.
  1337. */
  1338. int
  1339. xfs_itruncate_extents(
  1340. struct xfs_trans **tpp,
  1341. struct xfs_inode *ip,
  1342. int whichfork,
  1343. xfs_fsize_t new_size)
  1344. {
  1345. struct xfs_mount *mp = ip->i_mount;
  1346. struct xfs_trans *tp = *tpp;
  1347. struct xfs_trans *ntp;
  1348. xfs_bmap_free_t free_list;
  1349. xfs_fsblock_t first_block;
  1350. xfs_fileoff_t first_unmap_block;
  1351. xfs_fileoff_t last_block;
  1352. xfs_filblks_t unmap_len;
  1353. int committed;
  1354. int error = 0;
  1355. int done = 0;
  1356. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1357. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1358. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1359. ASSERT(new_size <= XFS_ISIZE(ip));
  1360. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1361. ASSERT(ip->i_itemp != NULL);
  1362. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1363. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1364. trace_xfs_itruncate_extents_start(ip, new_size);
  1365. /*
  1366. * Since it is possible for space to become allocated beyond
  1367. * the end of the file (in a crash where the space is allocated
  1368. * but the inode size is not yet updated), simply remove any
  1369. * blocks which show up between the new EOF and the maximum
  1370. * possible file size. If the first block to be removed is
  1371. * beyond the maximum file size (ie it is the same as last_block),
  1372. * then there is nothing to do.
  1373. */
  1374. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1375. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1376. if (first_unmap_block == last_block)
  1377. return 0;
  1378. ASSERT(first_unmap_block < last_block);
  1379. unmap_len = last_block - first_unmap_block + 1;
  1380. while (!done) {
  1381. xfs_bmap_init(&free_list, &first_block);
  1382. error = xfs_bunmapi(tp, ip,
  1383. first_unmap_block, unmap_len,
  1384. xfs_bmapi_aflag(whichfork),
  1385. XFS_ITRUNC_MAX_EXTENTS,
  1386. &first_block, &free_list,
  1387. &done);
  1388. if (error)
  1389. goto out_bmap_cancel;
  1390. /*
  1391. * Duplicate the transaction that has the permanent
  1392. * reservation and commit the old transaction.
  1393. */
  1394. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1395. if (committed)
  1396. xfs_trans_ijoin(tp, ip, 0);
  1397. if (error)
  1398. goto out_bmap_cancel;
  1399. if (committed) {
  1400. /*
  1401. * Mark the inode dirty so it will be logged and
  1402. * moved forward in the log as part of every commit.
  1403. */
  1404. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1405. }
  1406. ntp = xfs_trans_dup(tp);
  1407. error = xfs_trans_commit(tp, 0);
  1408. tp = ntp;
  1409. xfs_trans_ijoin(tp, ip, 0);
  1410. if (error)
  1411. goto out;
  1412. /*
  1413. * Transaction commit worked ok so we can drop the extra ticket
  1414. * reference that we gained in xfs_trans_dup()
  1415. */
  1416. xfs_log_ticket_put(tp->t_ticket);
  1417. error = xfs_trans_reserve(tp, 0,
  1418. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1419. XFS_TRANS_PERM_LOG_RES,
  1420. XFS_ITRUNCATE_LOG_COUNT);
  1421. if (error)
  1422. goto out;
  1423. }
  1424. /*
  1425. * Always re-log the inode so that our permanent transaction can keep
  1426. * on rolling it forward in the log.
  1427. */
  1428. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1429. trace_xfs_itruncate_extents_end(ip, new_size);
  1430. out:
  1431. *tpp = tp;
  1432. return error;
  1433. out_bmap_cancel:
  1434. /*
  1435. * If the bunmapi call encounters an error, return to the caller where
  1436. * the transaction can be properly aborted. We just need to make sure
  1437. * we're not holding any resources that we were not when we came in.
  1438. */
  1439. xfs_bmap_cancel(&free_list);
  1440. goto out;
  1441. }
  1442. /*
  1443. * This is called when the inode's link count goes to 0.
  1444. * We place the on-disk inode on a list in the AGI. It
  1445. * will be pulled from this list when the inode is freed.
  1446. */
  1447. int
  1448. xfs_iunlink(
  1449. xfs_trans_t *tp,
  1450. xfs_inode_t *ip)
  1451. {
  1452. xfs_mount_t *mp;
  1453. xfs_agi_t *agi;
  1454. xfs_dinode_t *dip;
  1455. xfs_buf_t *agibp;
  1456. xfs_buf_t *ibp;
  1457. xfs_agino_t agino;
  1458. short bucket_index;
  1459. int offset;
  1460. int error;
  1461. ASSERT(ip->i_d.di_nlink == 0);
  1462. ASSERT(ip->i_d.di_mode != 0);
  1463. mp = tp->t_mountp;
  1464. /*
  1465. * Get the agi buffer first. It ensures lock ordering
  1466. * on the list.
  1467. */
  1468. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1469. if (error)
  1470. return error;
  1471. agi = XFS_BUF_TO_AGI(agibp);
  1472. /*
  1473. * Get the index into the agi hash table for the
  1474. * list this inode will go on.
  1475. */
  1476. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1477. ASSERT(agino != 0);
  1478. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1479. ASSERT(agi->agi_unlinked[bucket_index]);
  1480. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1481. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1482. /*
  1483. * There is already another inode in the bucket we need
  1484. * to add ourselves to. Add us at the front of the list.
  1485. * Here we put the head pointer into our next pointer,
  1486. * and then we fall through to point the head at us.
  1487. */
  1488. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1489. 0, 0);
  1490. if (error)
  1491. return error;
  1492. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1493. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1494. offset = ip->i_imap.im_boffset +
  1495. offsetof(xfs_dinode_t, di_next_unlinked);
  1496. xfs_trans_inode_buf(tp, ibp);
  1497. xfs_trans_log_buf(tp, ibp, offset,
  1498. (offset + sizeof(xfs_agino_t) - 1));
  1499. xfs_inobp_check(mp, ibp);
  1500. }
  1501. /*
  1502. * Point the bucket head pointer at the inode being inserted.
  1503. */
  1504. ASSERT(agino != 0);
  1505. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1506. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1507. (sizeof(xfs_agino_t) * bucket_index);
  1508. xfs_trans_log_buf(tp, agibp, offset,
  1509. (offset + sizeof(xfs_agino_t) - 1));
  1510. return 0;
  1511. }
  1512. /*
  1513. * Pull the on-disk inode from the AGI unlinked list.
  1514. */
  1515. STATIC int
  1516. xfs_iunlink_remove(
  1517. xfs_trans_t *tp,
  1518. xfs_inode_t *ip)
  1519. {
  1520. xfs_ino_t next_ino;
  1521. xfs_mount_t *mp;
  1522. xfs_agi_t *agi;
  1523. xfs_dinode_t *dip;
  1524. xfs_buf_t *agibp;
  1525. xfs_buf_t *ibp;
  1526. xfs_agnumber_t agno;
  1527. xfs_agino_t agino;
  1528. xfs_agino_t next_agino;
  1529. xfs_buf_t *last_ibp;
  1530. xfs_dinode_t *last_dip = NULL;
  1531. short bucket_index;
  1532. int offset, last_offset = 0;
  1533. int error;
  1534. mp = tp->t_mountp;
  1535. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1536. /*
  1537. * Get the agi buffer first. It ensures lock ordering
  1538. * on the list.
  1539. */
  1540. error = xfs_read_agi(mp, tp, agno, &agibp);
  1541. if (error)
  1542. return error;
  1543. agi = XFS_BUF_TO_AGI(agibp);
  1544. /*
  1545. * Get the index into the agi hash table for the
  1546. * list this inode will go on.
  1547. */
  1548. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1549. ASSERT(agino != 0);
  1550. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1551. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1552. ASSERT(agi->agi_unlinked[bucket_index]);
  1553. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1554. /*
  1555. * We're at the head of the list. Get the inode's on-disk
  1556. * buffer to see if there is anyone after us on the list.
  1557. * Only modify our next pointer if it is not already NULLAGINO.
  1558. * This saves us the overhead of dealing with the buffer when
  1559. * there is no need to change it.
  1560. */
  1561. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1562. 0, 0);
  1563. if (error) {
  1564. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1565. __func__, error);
  1566. return error;
  1567. }
  1568. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1569. ASSERT(next_agino != 0);
  1570. if (next_agino != NULLAGINO) {
  1571. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1572. offset = ip->i_imap.im_boffset +
  1573. offsetof(xfs_dinode_t, di_next_unlinked);
  1574. xfs_trans_inode_buf(tp, ibp);
  1575. xfs_trans_log_buf(tp, ibp, offset,
  1576. (offset + sizeof(xfs_agino_t) - 1));
  1577. xfs_inobp_check(mp, ibp);
  1578. } else {
  1579. xfs_trans_brelse(tp, ibp);
  1580. }
  1581. /*
  1582. * Point the bucket head pointer at the next inode.
  1583. */
  1584. ASSERT(next_agino != 0);
  1585. ASSERT(next_agino != agino);
  1586. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1587. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1588. (sizeof(xfs_agino_t) * bucket_index);
  1589. xfs_trans_log_buf(tp, agibp, offset,
  1590. (offset + sizeof(xfs_agino_t) - 1));
  1591. } else {
  1592. /*
  1593. * We need to search the list for the inode being freed.
  1594. */
  1595. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1596. last_ibp = NULL;
  1597. while (next_agino != agino) {
  1598. struct xfs_imap imap;
  1599. if (last_ibp)
  1600. xfs_trans_brelse(tp, last_ibp);
  1601. imap.im_blkno = 0;
  1602. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1603. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1604. if (error) {
  1605. xfs_warn(mp,
  1606. "%s: xfs_imap returned error %d.",
  1607. __func__, error);
  1608. return error;
  1609. }
  1610. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1611. &last_ibp, 0, 0);
  1612. if (error) {
  1613. xfs_warn(mp,
  1614. "%s: xfs_imap_to_bp returned error %d.",
  1615. __func__, error);
  1616. return error;
  1617. }
  1618. last_offset = imap.im_boffset;
  1619. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1620. ASSERT(next_agino != NULLAGINO);
  1621. ASSERT(next_agino != 0);
  1622. }
  1623. /*
  1624. * Now last_ibp points to the buffer previous to us on the
  1625. * unlinked list. Pull us from the list.
  1626. */
  1627. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1628. 0, 0);
  1629. if (error) {
  1630. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1631. __func__, error);
  1632. return error;
  1633. }
  1634. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1635. ASSERT(next_agino != 0);
  1636. ASSERT(next_agino != agino);
  1637. if (next_agino != NULLAGINO) {
  1638. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1639. offset = ip->i_imap.im_boffset +
  1640. offsetof(xfs_dinode_t, di_next_unlinked);
  1641. xfs_trans_inode_buf(tp, ibp);
  1642. xfs_trans_log_buf(tp, ibp, offset,
  1643. (offset + sizeof(xfs_agino_t) - 1));
  1644. xfs_inobp_check(mp, ibp);
  1645. } else {
  1646. xfs_trans_brelse(tp, ibp);
  1647. }
  1648. /*
  1649. * Point the previous inode on the list to the next inode.
  1650. */
  1651. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1652. ASSERT(next_agino != 0);
  1653. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1654. xfs_trans_inode_buf(tp, last_ibp);
  1655. xfs_trans_log_buf(tp, last_ibp, offset,
  1656. (offset + sizeof(xfs_agino_t) - 1));
  1657. xfs_inobp_check(mp, last_ibp);
  1658. }
  1659. return 0;
  1660. }
  1661. /*
  1662. * A big issue when freeing the inode cluster is is that we _cannot_ skip any
  1663. * inodes that are in memory - they all must be marked stale and attached to
  1664. * the cluster buffer.
  1665. */
  1666. STATIC int
  1667. xfs_ifree_cluster(
  1668. xfs_inode_t *free_ip,
  1669. xfs_trans_t *tp,
  1670. xfs_ino_t inum)
  1671. {
  1672. xfs_mount_t *mp = free_ip->i_mount;
  1673. int blks_per_cluster;
  1674. int nbufs;
  1675. int ninodes;
  1676. int i, j;
  1677. xfs_daddr_t blkno;
  1678. xfs_buf_t *bp;
  1679. xfs_inode_t *ip;
  1680. xfs_inode_log_item_t *iip;
  1681. xfs_log_item_t *lip;
  1682. struct xfs_perag *pag;
  1683. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1684. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1685. blks_per_cluster = 1;
  1686. ninodes = mp->m_sb.sb_inopblock;
  1687. nbufs = XFS_IALLOC_BLOCKS(mp);
  1688. } else {
  1689. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1690. mp->m_sb.sb_blocksize;
  1691. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1692. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1693. }
  1694. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1695. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1696. XFS_INO_TO_AGBNO(mp, inum));
  1697. /*
  1698. * We obtain and lock the backing buffer first in the process
  1699. * here, as we have to ensure that any dirty inode that we
  1700. * can't get the flush lock on is attached to the buffer.
  1701. * If we scan the in-memory inodes first, then buffer IO can
  1702. * complete before we get a lock on it, and hence we may fail
  1703. * to mark all the active inodes on the buffer stale.
  1704. */
  1705. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1706. mp->m_bsize * blks_per_cluster,
  1707. XBF_UNMAPPED);
  1708. if (!bp)
  1709. return ENOMEM;
  1710. /*
  1711. * This buffer may not have been correctly initialised as we
  1712. * didn't read it from disk. That's not important because we are
  1713. * only using to mark the buffer as stale in the log, and to
  1714. * attach stale cached inodes on it. That means it will never be
  1715. * dispatched for IO. If it is, we want to know about it, and we
  1716. * want it to fail. We can acheive this by adding a write
  1717. * verifier to the buffer.
  1718. */
  1719. bp->b_ops = &xfs_inode_buf_ops;
  1720. /*
  1721. * Walk the inodes already attached to the buffer and mark them
  1722. * stale. These will all have the flush locks held, so an
  1723. * in-memory inode walk can't lock them. By marking them all
  1724. * stale first, we will not attempt to lock them in the loop
  1725. * below as the XFS_ISTALE flag will be set.
  1726. */
  1727. lip = bp->b_fspriv;
  1728. while (lip) {
  1729. if (lip->li_type == XFS_LI_INODE) {
  1730. iip = (xfs_inode_log_item_t *)lip;
  1731. ASSERT(iip->ili_logged == 1);
  1732. lip->li_cb = xfs_istale_done;
  1733. xfs_trans_ail_copy_lsn(mp->m_ail,
  1734. &iip->ili_flush_lsn,
  1735. &iip->ili_item.li_lsn);
  1736. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1737. }
  1738. lip = lip->li_bio_list;
  1739. }
  1740. /*
  1741. * For each inode in memory attempt to add it to the inode
  1742. * buffer and set it up for being staled on buffer IO
  1743. * completion. This is safe as we've locked out tail pushing
  1744. * and flushing by locking the buffer.
  1745. *
  1746. * We have already marked every inode that was part of a
  1747. * transaction stale above, which means there is no point in
  1748. * even trying to lock them.
  1749. */
  1750. for (i = 0; i < ninodes; i++) {
  1751. retry:
  1752. rcu_read_lock();
  1753. ip = radix_tree_lookup(&pag->pag_ici_root,
  1754. XFS_INO_TO_AGINO(mp, (inum + i)));
  1755. /* Inode not in memory, nothing to do */
  1756. if (!ip) {
  1757. rcu_read_unlock();
  1758. continue;
  1759. }
  1760. /*
  1761. * because this is an RCU protected lookup, we could
  1762. * find a recently freed or even reallocated inode
  1763. * during the lookup. We need to check under the
  1764. * i_flags_lock for a valid inode here. Skip it if it
  1765. * is not valid, the wrong inode or stale.
  1766. */
  1767. spin_lock(&ip->i_flags_lock);
  1768. if (ip->i_ino != inum + i ||
  1769. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1770. spin_unlock(&ip->i_flags_lock);
  1771. rcu_read_unlock();
  1772. continue;
  1773. }
  1774. spin_unlock(&ip->i_flags_lock);
  1775. /*
  1776. * Don't try to lock/unlock the current inode, but we
  1777. * _cannot_ skip the other inodes that we did not find
  1778. * in the list attached to the buffer and are not
  1779. * already marked stale. If we can't lock it, back off
  1780. * and retry.
  1781. */
  1782. if (ip != free_ip &&
  1783. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1784. rcu_read_unlock();
  1785. delay(1);
  1786. goto retry;
  1787. }
  1788. rcu_read_unlock();
  1789. xfs_iflock(ip);
  1790. xfs_iflags_set(ip, XFS_ISTALE);
  1791. /*
  1792. * we don't need to attach clean inodes or those only
  1793. * with unlogged changes (which we throw away, anyway).
  1794. */
  1795. iip = ip->i_itemp;
  1796. if (!iip || xfs_inode_clean(ip)) {
  1797. ASSERT(ip != free_ip);
  1798. xfs_ifunlock(ip);
  1799. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1800. continue;
  1801. }
  1802. iip->ili_last_fields = iip->ili_fields;
  1803. iip->ili_fields = 0;
  1804. iip->ili_logged = 1;
  1805. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  1806. &iip->ili_item.li_lsn);
  1807. xfs_buf_attach_iodone(bp, xfs_istale_done,
  1808. &iip->ili_item);
  1809. if (ip != free_ip)
  1810. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1811. }
  1812. xfs_trans_stale_inode_buf(tp, bp);
  1813. xfs_trans_binval(tp, bp);
  1814. }
  1815. xfs_perag_put(pag);
  1816. return 0;
  1817. }
  1818. /*
  1819. * This is called to return an inode to the inode free list.
  1820. * The inode should already be truncated to 0 length and have
  1821. * no pages associated with it. This routine also assumes that
  1822. * the inode is already a part of the transaction.
  1823. *
  1824. * The on-disk copy of the inode will have been added to the list
  1825. * of unlinked inodes in the AGI. We need to remove the inode from
  1826. * that list atomically with respect to freeing it here.
  1827. */
  1828. int
  1829. xfs_ifree(
  1830. xfs_trans_t *tp,
  1831. xfs_inode_t *ip,
  1832. xfs_bmap_free_t *flist)
  1833. {
  1834. int error;
  1835. int delete;
  1836. xfs_ino_t first_ino;
  1837. xfs_dinode_t *dip;
  1838. xfs_buf_t *ibp;
  1839. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1840. ASSERT(ip->i_d.di_nlink == 0);
  1841. ASSERT(ip->i_d.di_nextents == 0);
  1842. ASSERT(ip->i_d.di_anextents == 0);
  1843. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  1844. ASSERT(ip->i_d.di_nblocks == 0);
  1845. /*
  1846. * Pull the on-disk inode from the AGI unlinked list.
  1847. */
  1848. error = xfs_iunlink_remove(tp, ip);
  1849. if (error != 0) {
  1850. return error;
  1851. }
  1852. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  1853. if (error != 0) {
  1854. return error;
  1855. }
  1856. ip->i_d.di_mode = 0; /* mark incore inode as free */
  1857. ip->i_d.di_flags = 0;
  1858. ip->i_d.di_dmevmask = 0;
  1859. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  1860. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1861. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1862. /*
  1863. * Bump the generation count so no one will be confused
  1864. * by reincarnations of this inode.
  1865. */
  1866. ip->i_d.di_gen++;
  1867. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1868. error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
  1869. 0, 0);
  1870. if (error)
  1871. return error;
  1872. /*
  1873. * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
  1874. * from picking up this inode when it is reclaimed (its incore state
  1875. * initialzed but not flushed to disk yet). The in-core di_mode is
  1876. * already cleared and a corresponding transaction logged.
  1877. * The hack here just synchronizes the in-core to on-disk
  1878. * di_mode value in advance before the actual inode sync to disk.
  1879. * This is OK because the inode is already unlinked and would never
  1880. * change its di_mode again for this inode generation.
  1881. * This is a temporary hack that would require a proper fix
  1882. * in the future.
  1883. */
  1884. dip->di_mode = 0;
  1885. if (delete) {
  1886. error = xfs_ifree_cluster(ip, tp, first_ino);
  1887. }
  1888. return error;
  1889. }
  1890. /*
  1891. * Reallocate the space for if_broot based on the number of records
  1892. * being added or deleted as indicated in rec_diff. Move the records
  1893. * and pointers in if_broot to fit the new size. When shrinking this
  1894. * will eliminate holes between the records and pointers created by
  1895. * the caller. When growing this will create holes to be filled in
  1896. * by the caller.
  1897. *
  1898. * The caller must not request to add more records than would fit in
  1899. * the on-disk inode root. If the if_broot is currently NULL, then
  1900. * if we adding records one will be allocated. The caller must also
  1901. * not request that the number of records go below zero, although
  1902. * it can go to zero.
  1903. *
  1904. * ip -- the inode whose if_broot area is changing
  1905. * ext_diff -- the change in the number of records, positive or negative,
  1906. * requested for the if_broot array.
  1907. */
  1908. void
  1909. xfs_iroot_realloc(
  1910. xfs_inode_t *ip,
  1911. int rec_diff,
  1912. int whichfork)
  1913. {
  1914. struct xfs_mount *mp = ip->i_mount;
  1915. int cur_max;
  1916. xfs_ifork_t *ifp;
  1917. struct xfs_btree_block *new_broot;
  1918. int new_max;
  1919. size_t new_size;
  1920. char *np;
  1921. char *op;
  1922. /*
  1923. * Handle the degenerate case quietly.
  1924. */
  1925. if (rec_diff == 0) {
  1926. return;
  1927. }
  1928. ifp = XFS_IFORK_PTR(ip, whichfork);
  1929. if (rec_diff > 0) {
  1930. /*
  1931. * If there wasn't any memory allocated before, just
  1932. * allocate it now and get out.
  1933. */
  1934. if (ifp->if_broot_bytes == 0) {
  1935. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
  1936. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1937. ifp->if_broot_bytes = (int)new_size;
  1938. return;
  1939. }
  1940. /*
  1941. * If there is already an existing if_broot, then we need
  1942. * to realloc() it and shift the pointers to their new
  1943. * location. The records don't change location because
  1944. * they are kept butted up against the btree block header.
  1945. */
  1946. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1947. new_max = cur_max + rec_diff;
  1948. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  1949. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  1950. XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
  1951. KM_SLEEP | KM_NOFS);
  1952. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1953. ifp->if_broot_bytes);
  1954. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1955. (int)new_size);
  1956. ifp->if_broot_bytes = (int)new_size;
  1957. ASSERT(ifp->if_broot_bytes <=
  1958. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
  1959. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  1960. return;
  1961. }
  1962. /*
  1963. * rec_diff is less than 0. In this case, we are shrinking the
  1964. * if_broot buffer. It must already exist. If we go to zero
  1965. * records, just get rid of the root and clear the status bit.
  1966. */
  1967. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  1968. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1969. new_max = cur_max + rec_diff;
  1970. ASSERT(new_max >= 0);
  1971. if (new_max > 0)
  1972. new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
  1973. else
  1974. new_size = 0;
  1975. if (new_size > 0) {
  1976. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1977. /*
  1978. * First copy over the btree block header.
  1979. */
  1980. memcpy(new_broot, ifp->if_broot,
  1981. XFS_BMBT_BLOCK_LEN(ip->i_mount));
  1982. } else {
  1983. new_broot = NULL;
  1984. ifp->if_flags &= ~XFS_IFBROOT;
  1985. }
  1986. /*
  1987. * Only copy the records and pointers if there are any.
  1988. */
  1989. if (new_max > 0) {
  1990. /*
  1991. * First copy the records.
  1992. */
  1993. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  1994. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  1995. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  1996. /*
  1997. * Then copy the pointers.
  1998. */
  1999. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  2000. ifp->if_broot_bytes);
  2001. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  2002. (int)new_size);
  2003. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  2004. }
  2005. kmem_free(ifp->if_broot);
  2006. ifp->if_broot = new_broot;
  2007. ifp->if_broot_bytes = (int)new_size;
  2008. ASSERT(ifp->if_broot_bytes <=
  2009. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
  2010. return;
  2011. }
  2012. /*
  2013. * This is called when the amount of space needed for if_data
  2014. * is increased or decreased. The change in size is indicated by
  2015. * the number of bytes that need to be added or deleted in the
  2016. * byte_diff parameter.
  2017. *
  2018. * If the amount of space needed has decreased below the size of the
  2019. * inline buffer, then switch to using the inline buffer. Otherwise,
  2020. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2021. * to what is needed.
  2022. *
  2023. * ip -- the inode whose if_data area is changing
  2024. * byte_diff -- the change in the number of bytes, positive or negative,
  2025. * requested for the if_data array.
  2026. */
  2027. void
  2028. xfs_idata_realloc(
  2029. xfs_inode_t *ip,
  2030. int byte_diff,
  2031. int whichfork)
  2032. {
  2033. xfs_ifork_t *ifp;
  2034. int new_size;
  2035. int real_size;
  2036. if (byte_diff == 0) {
  2037. return;
  2038. }
  2039. ifp = XFS_IFORK_PTR(ip, whichfork);
  2040. new_size = (int)ifp->if_bytes + byte_diff;
  2041. ASSERT(new_size >= 0);
  2042. if (new_size == 0) {
  2043. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2044. kmem_free(ifp->if_u1.if_data);
  2045. }
  2046. ifp->if_u1.if_data = NULL;
  2047. real_size = 0;
  2048. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  2049. /*
  2050. * If the valid extents/data can fit in if_inline_ext/data,
  2051. * copy them from the malloc'd vector and free it.
  2052. */
  2053. if (ifp->if_u1.if_data == NULL) {
  2054. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2055. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2056. ASSERT(ifp->if_real_bytes != 0);
  2057. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  2058. new_size);
  2059. kmem_free(ifp->if_u1.if_data);
  2060. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2061. }
  2062. real_size = 0;
  2063. } else {
  2064. /*
  2065. * Stuck with malloc/realloc.
  2066. * For inline data, the underlying buffer must be
  2067. * a multiple of 4 bytes in size so that it can be
  2068. * logged and stay on word boundaries. We enforce
  2069. * that here.
  2070. */
  2071. real_size = roundup(new_size, 4);
  2072. if (ifp->if_u1.if_data == NULL) {
  2073. ASSERT(ifp->if_real_bytes == 0);
  2074. ifp->if_u1.if_data = kmem_alloc(real_size,
  2075. KM_SLEEP | KM_NOFS);
  2076. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2077. /*
  2078. * Only do the realloc if the underlying size
  2079. * is really changing.
  2080. */
  2081. if (ifp->if_real_bytes != real_size) {
  2082. ifp->if_u1.if_data =
  2083. kmem_realloc(ifp->if_u1.if_data,
  2084. real_size,
  2085. ifp->if_real_bytes,
  2086. KM_SLEEP | KM_NOFS);
  2087. }
  2088. } else {
  2089. ASSERT(ifp->if_real_bytes == 0);
  2090. ifp->if_u1.if_data = kmem_alloc(real_size,
  2091. KM_SLEEP | KM_NOFS);
  2092. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  2093. ifp->if_bytes);
  2094. }
  2095. }
  2096. ifp->if_real_bytes = real_size;
  2097. ifp->if_bytes = new_size;
  2098. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2099. }
  2100. void
  2101. xfs_idestroy_fork(
  2102. xfs_inode_t *ip,
  2103. int whichfork)
  2104. {
  2105. xfs_ifork_t *ifp;
  2106. ifp = XFS_IFORK_PTR(ip, whichfork);
  2107. if (ifp->if_broot != NULL) {
  2108. kmem_free(ifp->if_broot);
  2109. ifp->if_broot = NULL;
  2110. }
  2111. /*
  2112. * If the format is local, then we can't have an extents
  2113. * array so just look for an inline data array. If we're
  2114. * not local then we may or may not have an extents list,
  2115. * so check and free it up if we do.
  2116. */
  2117. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  2118. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  2119. (ifp->if_u1.if_data != NULL)) {
  2120. ASSERT(ifp->if_real_bytes != 0);
  2121. kmem_free(ifp->if_u1.if_data);
  2122. ifp->if_u1.if_data = NULL;
  2123. ifp->if_real_bytes = 0;
  2124. }
  2125. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  2126. ((ifp->if_flags & XFS_IFEXTIREC) ||
  2127. ((ifp->if_u1.if_extents != NULL) &&
  2128. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  2129. ASSERT(ifp->if_real_bytes != 0);
  2130. xfs_iext_destroy(ifp);
  2131. }
  2132. ASSERT(ifp->if_u1.if_extents == NULL ||
  2133. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  2134. ASSERT(ifp->if_real_bytes == 0);
  2135. if (whichfork == XFS_ATTR_FORK) {
  2136. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  2137. ip->i_afp = NULL;
  2138. }
  2139. }
  2140. /*
  2141. * This is called to unpin an inode. The caller must have the inode locked
  2142. * in at least shared mode so that the buffer cannot be subsequently pinned
  2143. * once someone is waiting for it to be unpinned.
  2144. */
  2145. static void
  2146. xfs_iunpin(
  2147. struct xfs_inode *ip)
  2148. {
  2149. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2150. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2151. /* Give the log a push to start the unpinning I/O */
  2152. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2153. }
  2154. static void
  2155. __xfs_iunpin_wait(
  2156. struct xfs_inode *ip)
  2157. {
  2158. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2159. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2160. xfs_iunpin(ip);
  2161. do {
  2162. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2163. if (xfs_ipincount(ip))
  2164. io_schedule();
  2165. } while (xfs_ipincount(ip));
  2166. finish_wait(wq, &wait.wait);
  2167. }
  2168. void
  2169. xfs_iunpin_wait(
  2170. struct xfs_inode *ip)
  2171. {
  2172. if (xfs_ipincount(ip))
  2173. __xfs_iunpin_wait(ip);
  2174. }
  2175. /*
  2176. * xfs_iextents_copy()
  2177. *
  2178. * This is called to copy the REAL extents (as opposed to the delayed
  2179. * allocation extents) from the inode into the given buffer. It
  2180. * returns the number of bytes copied into the buffer.
  2181. *
  2182. * If there are no delayed allocation extents, then we can just
  2183. * memcpy() the extents into the buffer. Otherwise, we need to
  2184. * examine each extent in turn and skip those which are delayed.
  2185. */
  2186. int
  2187. xfs_iextents_copy(
  2188. xfs_inode_t *ip,
  2189. xfs_bmbt_rec_t *dp,
  2190. int whichfork)
  2191. {
  2192. int copied;
  2193. int i;
  2194. xfs_ifork_t *ifp;
  2195. int nrecs;
  2196. xfs_fsblock_t start_block;
  2197. ifp = XFS_IFORK_PTR(ip, whichfork);
  2198. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2199. ASSERT(ifp->if_bytes > 0);
  2200. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2201. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  2202. ASSERT(nrecs > 0);
  2203. /*
  2204. * There are some delayed allocation extents in the
  2205. * inode, so copy the extents one at a time and skip
  2206. * the delayed ones. There must be at least one
  2207. * non-delayed extent.
  2208. */
  2209. copied = 0;
  2210. for (i = 0; i < nrecs; i++) {
  2211. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  2212. start_block = xfs_bmbt_get_startblock(ep);
  2213. if (isnullstartblock(start_block)) {
  2214. /*
  2215. * It's a delayed allocation extent, so skip it.
  2216. */
  2217. continue;
  2218. }
  2219. /* Translate to on disk format */
  2220. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  2221. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  2222. dp++;
  2223. copied++;
  2224. }
  2225. ASSERT(copied != 0);
  2226. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  2227. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  2228. }
  2229. /*
  2230. * Each of the following cases stores data into the same region
  2231. * of the on-disk inode, so only one of them can be valid at
  2232. * any given time. While it is possible to have conflicting formats
  2233. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  2234. * in EXTENTS format, this can only happen when the fork has
  2235. * changed formats after being modified but before being flushed.
  2236. * In these cases, the format always takes precedence, because the
  2237. * format indicates the current state of the fork.
  2238. */
  2239. /*ARGSUSED*/
  2240. STATIC void
  2241. xfs_iflush_fork(
  2242. xfs_inode_t *ip,
  2243. xfs_dinode_t *dip,
  2244. xfs_inode_log_item_t *iip,
  2245. int whichfork,
  2246. xfs_buf_t *bp)
  2247. {
  2248. char *cp;
  2249. xfs_ifork_t *ifp;
  2250. xfs_mount_t *mp;
  2251. static const short brootflag[2] =
  2252. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  2253. static const short dataflag[2] =
  2254. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  2255. static const short extflag[2] =
  2256. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  2257. if (!iip)
  2258. return;
  2259. ifp = XFS_IFORK_PTR(ip, whichfork);
  2260. /*
  2261. * This can happen if we gave up in iformat in an error path,
  2262. * for the attribute fork.
  2263. */
  2264. if (!ifp) {
  2265. ASSERT(whichfork == XFS_ATTR_FORK);
  2266. return;
  2267. }
  2268. cp = XFS_DFORK_PTR(dip, whichfork);
  2269. mp = ip->i_mount;
  2270. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2271. case XFS_DINODE_FMT_LOCAL:
  2272. if ((iip->ili_fields & dataflag[whichfork]) &&
  2273. (ifp->if_bytes > 0)) {
  2274. ASSERT(ifp->if_u1.if_data != NULL);
  2275. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2276. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2277. }
  2278. break;
  2279. case XFS_DINODE_FMT_EXTENTS:
  2280. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2281. !(iip->ili_fields & extflag[whichfork]));
  2282. if ((iip->ili_fields & extflag[whichfork]) &&
  2283. (ifp->if_bytes > 0)) {
  2284. ASSERT(xfs_iext_get_ext(ifp, 0));
  2285. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2286. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2287. whichfork);
  2288. }
  2289. break;
  2290. case XFS_DINODE_FMT_BTREE:
  2291. if ((iip->ili_fields & brootflag[whichfork]) &&
  2292. (ifp->if_broot_bytes > 0)) {
  2293. ASSERT(ifp->if_broot != NULL);
  2294. ASSERT(ifp->if_broot_bytes <=
  2295. (XFS_IFORK_SIZE(ip, whichfork) +
  2296. XFS_BROOT_SIZE_ADJ(ip)));
  2297. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  2298. (xfs_bmdr_block_t *)cp,
  2299. XFS_DFORK_SIZE(dip, mp, whichfork));
  2300. }
  2301. break;
  2302. case XFS_DINODE_FMT_DEV:
  2303. if (iip->ili_fields & XFS_ILOG_DEV) {
  2304. ASSERT(whichfork == XFS_DATA_FORK);
  2305. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  2306. }
  2307. break;
  2308. case XFS_DINODE_FMT_UUID:
  2309. if (iip->ili_fields & XFS_ILOG_UUID) {
  2310. ASSERT(whichfork == XFS_DATA_FORK);
  2311. memcpy(XFS_DFORK_DPTR(dip),
  2312. &ip->i_df.if_u2.if_uuid,
  2313. sizeof(uuid_t));
  2314. }
  2315. break;
  2316. default:
  2317. ASSERT(0);
  2318. break;
  2319. }
  2320. }
  2321. STATIC int
  2322. xfs_iflush_cluster(
  2323. xfs_inode_t *ip,
  2324. xfs_buf_t *bp)
  2325. {
  2326. xfs_mount_t *mp = ip->i_mount;
  2327. struct xfs_perag *pag;
  2328. unsigned long first_index, mask;
  2329. unsigned long inodes_per_cluster;
  2330. int ilist_size;
  2331. xfs_inode_t **ilist;
  2332. xfs_inode_t *iq;
  2333. int nr_found;
  2334. int clcount = 0;
  2335. int bufwasdelwri;
  2336. int i;
  2337. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2338. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2339. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2340. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2341. if (!ilist)
  2342. goto out_put;
  2343. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2344. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2345. rcu_read_lock();
  2346. /* really need a gang lookup range call here */
  2347. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2348. first_index, inodes_per_cluster);
  2349. if (nr_found == 0)
  2350. goto out_free;
  2351. for (i = 0; i < nr_found; i++) {
  2352. iq = ilist[i];
  2353. if (iq == ip)
  2354. continue;
  2355. /*
  2356. * because this is an RCU protected lookup, we could find a
  2357. * recently freed or even reallocated inode during the lookup.
  2358. * We need to check under the i_flags_lock for a valid inode
  2359. * here. Skip it if it is not valid or the wrong inode.
  2360. */
  2361. spin_lock(&ip->i_flags_lock);
  2362. if (!ip->i_ino ||
  2363. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2364. spin_unlock(&ip->i_flags_lock);
  2365. continue;
  2366. }
  2367. spin_unlock(&ip->i_flags_lock);
  2368. /*
  2369. * Do an un-protected check to see if the inode is dirty and
  2370. * is a candidate for flushing. These checks will be repeated
  2371. * later after the appropriate locks are acquired.
  2372. */
  2373. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2374. continue;
  2375. /*
  2376. * Try to get locks. If any are unavailable or it is pinned,
  2377. * then this inode cannot be flushed and is skipped.
  2378. */
  2379. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2380. continue;
  2381. if (!xfs_iflock_nowait(iq)) {
  2382. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2383. continue;
  2384. }
  2385. if (xfs_ipincount(iq)) {
  2386. xfs_ifunlock(iq);
  2387. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2388. continue;
  2389. }
  2390. /*
  2391. * arriving here means that this inode can be flushed. First
  2392. * re-check that it's dirty before flushing.
  2393. */
  2394. if (!xfs_inode_clean(iq)) {
  2395. int error;
  2396. error = xfs_iflush_int(iq, bp);
  2397. if (error) {
  2398. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2399. goto cluster_corrupt_out;
  2400. }
  2401. clcount++;
  2402. } else {
  2403. xfs_ifunlock(iq);
  2404. }
  2405. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2406. }
  2407. if (clcount) {
  2408. XFS_STATS_INC(xs_icluster_flushcnt);
  2409. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2410. }
  2411. out_free:
  2412. rcu_read_unlock();
  2413. kmem_free(ilist);
  2414. out_put:
  2415. xfs_perag_put(pag);
  2416. return 0;
  2417. cluster_corrupt_out:
  2418. /*
  2419. * Corruption detected in the clustering loop. Invalidate the
  2420. * inode buffer and shut down the filesystem.
  2421. */
  2422. rcu_read_unlock();
  2423. /*
  2424. * Clean up the buffer. If it was delwri, just release it --
  2425. * brelse can handle it with no problems. If not, shut down the
  2426. * filesystem before releasing the buffer.
  2427. */
  2428. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2429. if (bufwasdelwri)
  2430. xfs_buf_relse(bp);
  2431. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2432. if (!bufwasdelwri) {
  2433. /*
  2434. * Just like incore_relse: if we have b_iodone functions,
  2435. * mark the buffer as an error and call them. Otherwise
  2436. * mark it as stale and brelse.
  2437. */
  2438. if (bp->b_iodone) {
  2439. XFS_BUF_UNDONE(bp);
  2440. xfs_buf_stale(bp);
  2441. xfs_buf_ioerror(bp, EIO);
  2442. xfs_buf_ioend(bp, 0);
  2443. } else {
  2444. xfs_buf_stale(bp);
  2445. xfs_buf_relse(bp);
  2446. }
  2447. }
  2448. /*
  2449. * Unlocks the flush lock
  2450. */
  2451. xfs_iflush_abort(iq, false);
  2452. kmem_free(ilist);
  2453. xfs_perag_put(pag);
  2454. return XFS_ERROR(EFSCORRUPTED);
  2455. }
  2456. /*
  2457. * Flush dirty inode metadata into the backing buffer.
  2458. *
  2459. * The caller must have the inode lock and the inode flush lock held. The
  2460. * inode lock will still be held upon return to the caller, and the inode
  2461. * flush lock will be released after the inode has reached the disk.
  2462. *
  2463. * The caller must write out the buffer returned in *bpp and release it.
  2464. */
  2465. int
  2466. xfs_iflush(
  2467. struct xfs_inode *ip,
  2468. struct xfs_buf **bpp)
  2469. {
  2470. struct xfs_mount *mp = ip->i_mount;
  2471. struct xfs_buf *bp;
  2472. struct xfs_dinode *dip;
  2473. int error;
  2474. XFS_STATS_INC(xs_iflush_count);
  2475. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2476. ASSERT(xfs_isiflocked(ip));
  2477. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2478. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2479. *bpp = NULL;
  2480. xfs_iunpin_wait(ip);
  2481. /*
  2482. * For stale inodes we cannot rely on the backing buffer remaining
  2483. * stale in cache for the remaining life of the stale inode and so
  2484. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2485. * inodes below. We have to check this after ensuring the inode is
  2486. * unpinned so that it is safe to reclaim the stale inode after the
  2487. * flush call.
  2488. */
  2489. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2490. xfs_ifunlock(ip);
  2491. return 0;
  2492. }
  2493. /*
  2494. * This may have been unpinned because the filesystem is shutting
  2495. * down forcibly. If that's the case we must not write this inode
  2496. * to disk, because the log record didn't make it to disk.
  2497. *
  2498. * We also have to remove the log item from the AIL in this case,
  2499. * as we wait for an empty AIL as part of the unmount process.
  2500. */
  2501. if (XFS_FORCED_SHUTDOWN(mp)) {
  2502. error = XFS_ERROR(EIO);
  2503. goto abort_out;
  2504. }
  2505. /*
  2506. * Get the buffer containing the on-disk inode.
  2507. */
  2508. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2509. 0);
  2510. if (error || !bp) {
  2511. xfs_ifunlock(ip);
  2512. return error;
  2513. }
  2514. /*
  2515. * First flush out the inode that xfs_iflush was called with.
  2516. */
  2517. error = xfs_iflush_int(ip, bp);
  2518. if (error)
  2519. goto corrupt_out;
  2520. /*
  2521. * If the buffer is pinned then push on the log now so we won't
  2522. * get stuck waiting in the write for too long.
  2523. */
  2524. if (xfs_buf_ispinned(bp))
  2525. xfs_log_force(mp, 0);
  2526. /*
  2527. * inode clustering:
  2528. * see if other inodes can be gathered into this write
  2529. */
  2530. error = xfs_iflush_cluster(ip, bp);
  2531. if (error)
  2532. goto cluster_corrupt_out;
  2533. *bpp = bp;
  2534. return 0;
  2535. corrupt_out:
  2536. xfs_buf_relse(bp);
  2537. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2538. cluster_corrupt_out:
  2539. error = XFS_ERROR(EFSCORRUPTED);
  2540. abort_out:
  2541. /*
  2542. * Unlocks the flush lock
  2543. */
  2544. xfs_iflush_abort(ip, false);
  2545. return error;
  2546. }
  2547. STATIC int
  2548. xfs_iflush_int(
  2549. struct xfs_inode *ip,
  2550. struct xfs_buf *bp)
  2551. {
  2552. struct xfs_inode_log_item *iip = ip->i_itemp;
  2553. struct xfs_dinode *dip;
  2554. struct xfs_mount *mp = ip->i_mount;
  2555. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2556. ASSERT(xfs_isiflocked(ip));
  2557. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2558. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2559. ASSERT(iip != NULL && iip->ili_fields != 0);
  2560. /* set *dip = inode's place in the buffer */
  2561. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2562. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2563. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2564. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2565. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2566. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2567. goto corrupt_out;
  2568. }
  2569. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2570. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2571. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2572. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2573. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2574. goto corrupt_out;
  2575. }
  2576. if (S_ISREG(ip->i_d.di_mode)) {
  2577. if (XFS_TEST_ERROR(
  2578. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2579. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2580. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2581. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2582. "%s: Bad regular inode %Lu, ptr 0x%p",
  2583. __func__, ip->i_ino, ip);
  2584. goto corrupt_out;
  2585. }
  2586. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2587. if (XFS_TEST_ERROR(
  2588. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2589. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2590. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2591. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2592. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2593. "%s: Bad directory inode %Lu, ptr 0x%p",
  2594. __func__, ip->i_ino, ip);
  2595. goto corrupt_out;
  2596. }
  2597. }
  2598. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2599. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2600. XFS_RANDOM_IFLUSH_5)) {
  2601. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2602. "%s: detected corrupt incore inode %Lu, "
  2603. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2604. __func__, ip->i_ino,
  2605. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2606. ip->i_d.di_nblocks, ip);
  2607. goto corrupt_out;
  2608. }
  2609. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2610. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2611. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2612. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2613. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2614. goto corrupt_out;
  2615. }
  2616. /*
  2617. * bump the flush iteration count, used to detect flushes which
  2618. * postdate a log record during recovery. This is redundant as we now
  2619. * log every change and hence this can't happen. Still, it doesn't hurt.
  2620. */
  2621. ip->i_d.di_flushiter++;
  2622. /*
  2623. * Copy the dirty parts of the inode into the on-disk
  2624. * inode. We always copy out the core of the inode,
  2625. * because if the inode is dirty at all the core must
  2626. * be.
  2627. */
  2628. xfs_dinode_to_disk(dip, &ip->i_d);
  2629. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2630. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2631. ip->i_d.di_flushiter = 0;
  2632. /*
  2633. * If this is really an old format inode and the superblock version
  2634. * has not been updated to support only new format inodes, then
  2635. * convert back to the old inode format. If the superblock version
  2636. * has been updated, then make the conversion permanent.
  2637. */
  2638. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2639. if (ip->i_d.di_version == 1) {
  2640. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2641. /*
  2642. * Convert it back.
  2643. */
  2644. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2645. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2646. } else {
  2647. /*
  2648. * The superblock version has already been bumped,
  2649. * so just make the conversion to the new inode
  2650. * format permanent.
  2651. */
  2652. ip->i_d.di_version = 2;
  2653. dip->di_version = 2;
  2654. ip->i_d.di_onlink = 0;
  2655. dip->di_onlink = 0;
  2656. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2657. memset(&(dip->di_pad[0]), 0,
  2658. sizeof(dip->di_pad));
  2659. ASSERT(xfs_get_projid(ip) == 0);
  2660. }
  2661. }
  2662. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2663. if (XFS_IFORK_Q(ip))
  2664. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2665. xfs_inobp_check(mp, bp);
  2666. /*
  2667. * We've recorded everything logged in the inode, so we'd like to clear
  2668. * the ili_fields bits so we don't log and flush things unnecessarily.
  2669. * However, we can't stop logging all this information until the data
  2670. * we've copied into the disk buffer is written to disk. If we did we
  2671. * might overwrite the copy of the inode in the log with all the data
  2672. * after re-logging only part of it, and in the face of a crash we
  2673. * wouldn't have all the data we need to recover.
  2674. *
  2675. * What we do is move the bits to the ili_last_fields field. When
  2676. * logging the inode, these bits are moved back to the ili_fields field.
  2677. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2678. * know that the information those bits represent is permanently on
  2679. * disk. As long as the flush completes before the inode is logged
  2680. * again, then both ili_fields and ili_last_fields will be cleared.
  2681. *
  2682. * We can play with the ili_fields bits here, because the inode lock
  2683. * must be held exclusively in order to set bits there and the flush
  2684. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2685. * done routine can tell whether or not to look in the AIL. Also, store
  2686. * the current LSN of the inode so that we can tell whether the item has
  2687. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2688. * need the AIL lock, because it is a 64 bit value that cannot be read
  2689. * atomically.
  2690. */
  2691. iip->ili_last_fields = iip->ili_fields;
  2692. iip->ili_fields = 0;
  2693. iip->ili_logged = 1;
  2694. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2695. &iip->ili_item.li_lsn);
  2696. /*
  2697. * Attach the function xfs_iflush_done to the inode's
  2698. * buffer. This will remove the inode from the AIL
  2699. * and unlock the inode's flush lock when the inode is
  2700. * completely written to disk.
  2701. */
  2702. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2703. /* update the lsn in the on disk inode if required */
  2704. if (ip->i_d.di_version == 3)
  2705. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  2706. /* generate the checksum. */
  2707. xfs_dinode_calc_crc(mp, dip);
  2708. ASSERT(bp->b_fspriv != NULL);
  2709. ASSERT(bp->b_iodone != NULL);
  2710. return 0;
  2711. corrupt_out:
  2712. return XFS_ERROR(EFSCORRUPTED);
  2713. }
  2714. /*
  2715. * Return a pointer to the extent record at file index idx.
  2716. */
  2717. xfs_bmbt_rec_host_t *
  2718. xfs_iext_get_ext(
  2719. xfs_ifork_t *ifp, /* inode fork pointer */
  2720. xfs_extnum_t idx) /* index of target extent */
  2721. {
  2722. ASSERT(idx >= 0);
  2723. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  2724. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  2725. return ifp->if_u1.if_ext_irec->er_extbuf;
  2726. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2727. xfs_ext_irec_t *erp; /* irec pointer */
  2728. int erp_idx = 0; /* irec index */
  2729. xfs_extnum_t page_idx = idx; /* ext index in target list */
  2730. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2731. return &erp->er_extbuf[page_idx];
  2732. } else if (ifp->if_bytes) {
  2733. return &ifp->if_u1.if_extents[idx];
  2734. } else {
  2735. return NULL;
  2736. }
  2737. }
  2738. /*
  2739. * Insert new item(s) into the extent records for incore inode
  2740. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  2741. */
  2742. void
  2743. xfs_iext_insert(
  2744. xfs_inode_t *ip, /* incore inode pointer */
  2745. xfs_extnum_t idx, /* starting index of new items */
  2746. xfs_extnum_t count, /* number of inserted items */
  2747. xfs_bmbt_irec_t *new, /* items to insert */
  2748. int state) /* type of extent conversion */
  2749. {
  2750. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2751. xfs_extnum_t i; /* extent record index */
  2752. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  2753. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2754. xfs_iext_add(ifp, idx, count);
  2755. for (i = idx; i < idx + count; i++, new++)
  2756. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  2757. }
  2758. /*
  2759. * This is called when the amount of space required for incore file
  2760. * extents needs to be increased. The ext_diff parameter stores the
  2761. * number of new extents being added and the idx parameter contains
  2762. * the extent index where the new extents will be added. If the new
  2763. * extents are being appended, then we just need to (re)allocate and
  2764. * initialize the space. Otherwise, if the new extents are being
  2765. * inserted into the middle of the existing entries, a bit more work
  2766. * is required to make room for the new extents to be inserted. The
  2767. * caller is responsible for filling in the new extent entries upon
  2768. * return.
  2769. */
  2770. void
  2771. xfs_iext_add(
  2772. xfs_ifork_t *ifp, /* inode fork pointer */
  2773. xfs_extnum_t idx, /* index to begin adding exts */
  2774. int ext_diff) /* number of extents to add */
  2775. {
  2776. int byte_diff; /* new bytes being added */
  2777. int new_size; /* size of extents after adding */
  2778. xfs_extnum_t nextents; /* number of extents in file */
  2779. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2780. ASSERT((idx >= 0) && (idx <= nextents));
  2781. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  2782. new_size = ifp->if_bytes + byte_diff;
  2783. /*
  2784. * If the new number of extents (nextents + ext_diff)
  2785. * fits inside the inode, then continue to use the inline
  2786. * extent buffer.
  2787. */
  2788. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  2789. if (idx < nextents) {
  2790. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  2791. &ifp->if_u2.if_inline_ext[idx],
  2792. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2793. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  2794. }
  2795. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2796. ifp->if_real_bytes = 0;
  2797. }
  2798. /*
  2799. * Otherwise use a linear (direct) extent list.
  2800. * If the extents are currently inside the inode,
  2801. * xfs_iext_realloc_direct will switch us from
  2802. * inline to direct extent allocation mode.
  2803. */
  2804. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  2805. xfs_iext_realloc_direct(ifp, new_size);
  2806. if (idx < nextents) {
  2807. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  2808. &ifp->if_u1.if_extents[idx],
  2809. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2810. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  2811. }
  2812. }
  2813. /* Indirection array */
  2814. else {
  2815. xfs_ext_irec_t *erp;
  2816. int erp_idx = 0;
  2817. int page_idx = idx;
  2818. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  2819. if (ifp->if_flags & XFS_IFEXTIREC) {
  2820. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  2821. } else {
  2822. xfs_iext_irec_init(ifp);
  2823. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2824. erp = ifp->if_u1.if_ext_irec;
  2825. }
  2826. /* Extents fit in target extent page */
  2827. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  2828. if (page_idx < erp->er_extcount) {
  2829. memmove(&erp->er_extbuf[page_idx + ext_diff],
  2830. &erp->er_extbuf[page_idx],
  2831. (erp->er_extcount - page_idx) *
  2832. sizeof(xfs_bmbt_rec_t));
  2833. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  2834. }
  2835. erp->er_extcount += ext_diff;
  2836. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2837. }
  2838. /* Insert a new extent page */
  2839. else if (erp) {
  2840. xfs_iext_add_indirect_multi(ifp,
  2841. erp_idx, page_idx, ext_diff);
  2842. }
  2843. /*
  2844. * If extent(s) are being appended to the last page in
  2845. * the indirection array and the new extent(s) don't fit
  2846. * in the page, then erp is NULL and erp_idx is set to
  2847. * the next index needed in the indirection array.
  2848. */
  2849. else {
  2850. int count = ext_diff;
  2851. while (count) {
  2852. erp = xfs_iext_irec_new(ifp, erp_idx);
  2853. erp->er_extcount = count;
  2854. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  2855. if (count) {
  2856. erp_idx++;
  2857. }
  2858. }
  2859. }
  2860. }
  2861. ifp->if_bytes = new_size;
  2862. }
  2863. /*
  2864. * This is called when incore extents are being added to the indirection
  2865. * array and the new extents do not fit in the target extent list. The
  2866. * erp_idx parameter contains the irec index for the target extent list
  2867. * in the indirection array, and the idx parameter contains the extent
  2868. * index within the list. The number of extents being added is stored
  2869. * in the count parameter.
  2870. *
  2871. * |-------| |-------|
  2872. * | | | | idx - number of extents before idx
  2873. * | idx | | count |
  2874. * | | | | count - number of extents being inserted at idx
  2875. * |-------| |-------|
  2876. * | count | | nex2 | nex2 - number of extents after idx + count
  2877. * |-------| |-------|
  2878. */
  2879. void
  2880. xfs_iext_add_indirect_multi(
  2881. xfs_ifork_t *ifp, /* inode fork pointer */
  2882. int erp_idx, /* target extent irec index */
  2883. xfs_extnum_t idx, /* index within target list */
  2884. int count) /* new extents being added */
  2885. {
  2886. int byte_diff; /* new bytes being added */
  2887. xfs_ext_irec_t *erp; /* pointer to irec entry */
  2888. xfs_extnum_t ext_diff; /* number of extents to add */
  2889. xfs_extnum_t ext_cnt; /* new extents still needed */
  2890. xfs_extnum_t nex2; /* extents after idx + count */
  2891. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  2892. int nlists; /* number of irec's (lists) */
  2893. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2894. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2895. nex2 = erp->er_extcount - idx;
  2896. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2897. /*
  2898. * Save second part of target extent list
  2899. * (all extents past */
  2900. if (nex2) {
  2901. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2902. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  2903. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  2904. erp->er_extcount -= nex2;
  2905. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  2906. memset(&erp->er_extbuf[idx], 0, byte_diff);
  2907. }
  2908. /*
  2909. * Add the new extents to the end of the target
  2910. * list, then allocate new irec record(s) and
  2911. * extent buffer(s) as needed to store the rest
  2912. * of the new extents.
  2913. */
  2914. ext_cnt = count;
  2915. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  2916. if (ext_diff) {
  2917. erp->er_extcount += ext_diff;
  2918. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2919. ext_cnt -= ext_diff;
  2920. }
  2921. while (ext_cnt) {
  2922. erp_idx++;
  2923. erp = xfs_iext_irec_new(ifp, erp_idx);
  2924. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  2925. erp->er_extcount = ext_diff;
  2926. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2927. ext_cnt -= ext_diff;
  2928. }
  2929. /* Add nex2 extents back to indirection array */
  2930. if (nex2) {
  2931. xfs_extnum_t ext_avail;
  2932. int i;
  2933. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2934. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  2935. i = 0;
  2936. /*
  2937. * If nex2 extents fit in the current page, append
  2938. * nex2_ep after the new extents.
  2939. */
  2940. if (nex2 <= ext_avail) {
  2941. i = erp->er_extcount;
  2942. }
  2943. /*
  2944. * Otherwise, check if space is available in the
  2945. * next page.
  2946. */
  2947. else if ((erp_idx < nlists - 1) &&
  2948. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  2949. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  2950. erp_idx++;
  2951. erp++;
  2952. /* Create a hole for nex2 extents */
  2953. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  2954. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  2955. }
  2956. /*
  2957. * Final choice, create a new extent page for
  2958. * nex2 extents.
  2959. */
  2960. else {
  2961. erp_idx++;
  2962. erp = xfs_iext_irec_new(ifp, erp_idx);
  2963. }
  2964. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  2965. kmem_free(nex2_ep);
  2966. erp->er_extcount += nex2;
  2967. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  2968. }
  2969. }
  2970. /*
  2971. * This is called when the amount of space required for incore file
  2972. * extents needs to be decreased. The ext_diff parameter stores the
  2973. * number of extents to be removed and the idx parameter contains
  2974. * the extent index where the extents will be removed from.
  2975. *
  2976. * If the amount of space needed has decreased below the linear
  2977. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  2978. * extent array. Otherwise, use kmem_realloc() to adjust the
  2979. * size to what is needed.
  2980. */
  2981. void
  2982. xfs_iext_remove(
  2983. xfs_inode_t *ip, /* incore inode pointer */
  2984. xfs_extnum_t idx, /* index to begin removing exts */
  2985. int ext_diff, /* number of extents to remove */
  2986. int state) /* type of extent conversion */
  2987. {
  2988. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2989. xfs_extnum_t nextents; /* number of extents in file */
  2990. int new_size; /* size of extents after removal */
  2991. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  2992. ASSERT(ext_diff > 0);
  2993. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2994. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  2995. if (new_size == 0) {
  2996. xfs_iext_destroy(ifp);
  2997. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2998. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  2999. } else if (ifp->if_real_bytes) {
  3000. xfs_iext_remove_direct(ifp, idx, ext_diff);
  3001. } else {
  3002. xfs_iext_remove_inline(ifp, idx, ext_diff);
  3003. }
  3004. ifp->if_bytes = new_size;
  3005. }
  3006. /*
  3007. * This removes ext_diff extents from the inline buffer, beginning
  3008. * at extent index idx.
  3009. */
  3010. void
  3011. xfs_iext_remove_inline(
  3012. xfs_ifork_t *ifp, /* inode fork pointer */
  3013. xfs_extnum_t idx, /* index to begin removing exts */
  3014. int ext_diff) /* number of extents to remove */
  3015. {
  3016. int nextents; /* number of extents in file */
  3017. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3018. ASSERT(idx < XFS_INLINE_EXTS);
  3019. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3020. ASSERT(((nextents - ext_diff) > 0) &&
  3021. (nextents - ext_diff) < XFS_INLINE_EXTS);
  3022. if (idx + ext_diff < nextents) {
  3023. memmove(&ifp->if_u2.if_inline_ext[idx],
  3024. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  3025. (nextents - (idx + ext_diff)) *
  3026. sizeof(xfs_bmbt_rec_t));
  3027. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  3028. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3029. } else {
  3030. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  3031. ext_diff * sizeof(xfs_bmbt_rec_t));
  3032. }
  3033. }
  3034. /*
  3035. * This removes ext_diff extents from a linear (direct) extent list,
  3036. * beginning at extent index idx. If the extents are being removed
  3037. * from the end of the list (ie. truncate) then we just need to re-
  3038. * allocate the list to remove the extra space. Otherwise, if the
  3039. * extents are being removed from the middle of the existing extent
  3040. * entries, then we first need to move the extent records beginning
  3041. * at idx + ext_diff up in the list to overwrite the records being
  3042. * removed, then remove the extra space via kmem_realloc.
  3043. */
  3044. void
  3045. xfs_iext_remove_direct(
  3046. xfs_ifork_t *ifp, /* inode fork pointer */
  3047. xfs_extnum_t idx, /* index to begin removing exts */
  3048. int ext_diff) /* number of extents to remove */
  3049. {
  3050. xfs_extnum_t nextents; /* number of extents in file */
  3051. int new_size; /* size of extents after removal */
  3052. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3053. new_size = ifp->if_bytes -
  3054. (ext_diff * sizeof(xfs_bmbt_rec_t));
  3055. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3056. if (new_size == 0) {
  3057. xfs_iext_destroy(ifp);
  3058. return;
  3059. }
  3060. /* Move extents up in the list (if needed) */
  3061. if (idx + ext_diff < nextents) {
  3062. memmove(&ifp->if_u1.if_extents[idx],
  3063. &ifp->if_u1.if_extents[idx + ext_diff],
  3064. (nextents - (idx + ext_diff)) *
  3065. sizeof(xfs_bmbt_rec_t));
  3066. }
  3067. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  3068. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3069. /*
  3070. * Reallocate the direct extent list. If the extents
  3071. * will fit inside the inode then xfs_iext_realloc_direct
  3072. * will switch from direct to inline extent allocation
  3073. * mode for us.
  3074. */
  3075. xfs_iext_realloc_direct(ifp, new_size);
  3076. ifp->if_bytes = new_size;
  3077. }
  3078. /*
  3079. * This is called when incore extents are being removed from the
  3080. * indirection array and the extents being removed span multiple extent
  3081. * buffers. The idx parameter contains the file extent index where we
  3082. * want to begin removing extents, and the count parameter contains
  3083. * how many extents need to be removed.
  3084. *
  3085. * |-------| |-------|
  3086. * | nex1 | | | nex1 - number of extents before idx
  3087. * |-------| | count |
  3088. * | | | | count - number of extents being removed at idx
  3089. * | count | |-------|
  3090. * | | | nex2 | nex2 - number of extents after idx + count
  3091. * |-------| |-------|
  3092. */
  3093. void
  3094. xfs_iext_remove_indirect(
  3095. xfs_ifork_t *ifp, /* inode fork pointer */
  3096. xfs_extnum_t idx, /* index to begin removing extents */
  3097. int count) /* number of extents to remove */
  3098. {
  3099. xfs_ext_irec_t *erp; /* indirection array pointer */
  3100. int erp_idx = 0; /* indirection array index */
  3101. xfs_extnum_t ext_cnt; /* extents left to remove */
  3102. xfs_extnum_t ext_diff; /* extents to remove in current list */
  3103. xfs_extnum_t nex1; /* number of extents before idx */
  3104. xfs_extnum_t nex2; /* extents after idx + count */
  3105. int page_idx = idx; /* index in target extent list */
  3106. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3107. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3108. ASSERT(erp != NULL);
  3109. nex1 = page_idx;
  3110. ext_cnt = count;
  3111. while (ext_cnt) {
  3112. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  3113. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  3114. /*
  3115. * Check for deletion of entire list;
  3116. * xfs_iext_irec_remove() updates extent offsets.
  3117. */
  3118. if (ext_diff == erp->er_extcount) {
  3119. xfs_iext_irec_remove(ifp, erp_idx);
  3120. ext_cnt -= ext_diff;
  3121. nex1 = 0;
  3122. if (ext_cnt) {
  3123. ASSERT(erp_idx < ifp->if_real_bytes /
  3124. XFS_IEXT_BUFSZ);
  3125. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3126. nex1 = 0;
  3127. continue;
  3128. } else {
  3129. break;
  3130. }
  3131. }
  3132. /* Move extents up (if needed) */
  3133. if (nex2) {
  3134. memmove(&erp->er_extbuf[nex1],
  3135. &erp->er_extbuf[nex1 + ext_diff],
  3136. nex2 * sizeof(xfs_bmbt_rec_t));
  3137. }
  3138. /* Zero out rest of page */
  3139. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  3140. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  3141. /* Update remaining counters */
  3142. erp->er_extcount -= ext_diff;
  3143. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  3144. ext_cnt -= ext_diff;
  3145. nex1 = 0;
  3146. erp_idx++;
  3147. erp++;
  3148. }
  3149. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  3150. xfs_iext_irec_compact(ifp);
  3151. }
  3152. /*
  3153. * Create, destroy, or resize a linear (direct) block of extents.
  3154. */
  3155. void
  3156. xfs_iext_realloc_direct(
  3157. xfs_ifork_t *ifp, /* inode fork pointer */
  3158. int new_size) /* new size of extents */
  3159. {
  3160. int rnew_size; /* real new size of extents */
  3161. rnew_size = new_size;
  3162. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  3163. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  3164. (new_size != ifp->if_real_bytes)));
  3165. /* Free extent records */
  3166. if (new_size == 0) {
  3167. xfs_iext_destroy(ifp);
  3168. }
  3169. /* Resize direct extent list and zero any new bytes */
  3170. else if (ifp->if_real_bytes) {
  3171. /* Check if extents will fit inside the inode */
  3172. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  3173. xfs_iext_direct_to_inline(ifp, new_size /
  3174. (uint)sizeof(xfs_bmbt_rec_t));
  3175. ifp->if_bytes = new_size;
  3176. return;
  3177. }
  3178. if (!is_power_of_2(new_size)){
  3179. rnew_size = roundup_pow_of_two(new_size);
  3180. }
  3181. if (rnew_size != ifp->if_real_bytes) {
  3182. ifp->if_u1.if_extents =
  3183. kmem_realloc(ifp->if_u1.if_extents,
  3184. rnew_size,
  3185. ifp->if_real_bytes, KM_NOFS);
  3186. }
  3187. if (rnew_size > ifp->if_real_bytes) {
  3188. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  3189. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  3190. rnew_size - ifp->if_real_bytes);
  3191. }
  3192. }
  3193. /*
  3194. * Switch from the inline extent buffer to a direct
  3195. * extent list. Be sure to include the inline extent
  3196. * bytes in new_size.
  3197. */
  3198. else {
  3199. new_size += ifp->if_bytes;
  3200. if (!is_power_of_2(new_size)) {
  3201. rnew_size = roundup_pow_of_two(new_size);
  3202. }
  3203. xfs_iext_inline_to_direct(ifp, rnew_size);
  3204. }
  3205. ifp->if_real_bytes = rnew_size;
  3206. ifp->if_bytes = new_size;
  3207. }
  3208. /*
  3209. * Switch from linear (direct) extent records to inline buffer.
  3210. */
  3211. void
  3212. xfs_iext_direct_to_inline(
  3213. xfs_ifork_t *ifp, /* inode fork pointer */
  3214. xfs_extnum_t nextents) /* number of extents in file */
  3215. {
  3216. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3217. ASSERT(nextents <= XFS_INLINE_EXTS);
  3218. /*
  3219. * The inline buffer was zeroed when we switched
  3220. * from inline to direct extent allocation mode,
  3221. * so we don't need to clear it here.
  3222. */
  3223. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  3224. nextents * sizeof(xfs_bmbt_rec_t));
  3225. kmem_free(ifp->if_u1.if_extents);
  3226. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3227. ifp->if_real_bytes = 0;
  3228. }
  3229. /*
  3230. * Switch from inline buffer to linear (direct) extent records.
  3231. * new_size should already be rounded up to the next power of 2
  3232. * by the caller (when appropriate), so use new_size as it is.
  3233. * However, since new_size may be rounded up, we can't update
  3234. * if_bytes here. It is the caller's responsibility to update
  3235. * if_bytes upon return.
  3236. */
  3237. void
  3238. xfs_iext_inline_to_direct(
  3239. xfs_ifork_t *ifp, /* inode fork pointer */
  3240. int new_size) /* number of extents in file */
  3241. {
  3242. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  3243. memset(ifp->if_u1.if_extents, 0, new_size);
  3244. if (ifp->if_bytes) {
  3245. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  3246. ifp->if_bytes);
  3247. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3248. sizeof(xfs_bmbt_rec_t));
  3249. }
  3250. ifp->if_real_bytes = new_size;
  3251. }
  3252. /*
  3253. * Resize an extent indirection array to new_size bytes.
  3254. */
  3255. STATIC void
  3256. xfs_iext_realloc_indirect(
  3257. xfs_ifork_t *ifp, /* inode fork pointer */
  3258. int new_size) /* new indirection array size */
  3259. {
  3260. int nlists; /* number of irec's (ex lists) */
  3261. int size; /* current indirection array size */
  3262. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3263. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3264. size = nlists * sizeof(xfs_ext_irec_t);
  3265. ASSERT(ifp->if_real_bytes);
  3266. ASSERT((new_size >= 0) && (new_size != size));
  3267. if (new_size == 0) {
  3268. xfs_iext_destroy(ifp);
  3269. } else {
  3270. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  3271. kmem_realloc(ifp->if_u1.if_ext_irec,
  3272. new_size, size, KM_NOFS);
  3273. }
  3274. }
  3275. /*
  3276. * Switch from indirection array to linear (direct) extent allocations.
  3277. */
  3278. STATIC void
  3279. xfs_iext_indirect_to_direct(
  3280. xfs_ifork_t *ifp) /* inode fork pointer */
  3281. {
  3282. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3283. xfs_extnum_t nextents; /* number of extents in file */
  3284. int size; /* size of file extents */
  3285. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3286. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3287. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3288. size = nextents * sizeof(xfs_bmbt_rec_t);
  3289. xfs_iext_irec_compact_pages(ifp);
  3290. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  3291. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  3292. kmem_free(ifp->if_u1.if_ext_irec);
  3293. ifp->if_flags &= ~XFS_IFEXTIREC;
  3294. ifp->if_u1.if_extents = ep;
  3295. ifp->if_bytes = size;
  3296. if (nextents < XFS_LINEAR_EXTS) {
  3297. xfs_iext_realloc_direct(ifp, size);
  3298. }
  3299. }
  3300. /*
  3301. * Free incore file extents.
  3302. */
  3303. void
  3304. xfs_iext_destroy(
  3305. xfs_ifork_t *ifp) /* inode fork pointer */
  3306. {
  3307. if (ifp->if_flags & XFS_IFEXTIREC) {
  3308. int erp_idx;
  3309. int nlists;
  3310. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3311. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3312. xfs_iext_irec_remove(ifp, erp_idx);
  3313. }
  3314. ifp->if_flags &= ~XFS_IFEXTIREC;
  3315. } else if (ifp->if_real_bytes) {
  3316. kmem_free(ifp->if_u1.if_extents);
  3317. } else if (ifp->if_bytes) {
  3318. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3319. sizeof(xfs_bmbt_rec_t));
  3320. }
  3321. ifp->if_u1.if_extents = NULL;
  3322. ifp->if_real_bytes = 0;
  3323. ifp->if_bytes = 0;
  3324. }
  3325. /*
  3326. * Return a pointer to the extent record for file system block bno.
  3327. */
  3328. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3329. xfs_iext_bno_to_ext(
  3330. xfs_ifork_t *ifp, /* inode fork pointer */
  3331. xfs_fileoff_t bno, /* block number to search for */
  3332. xfs_extnum_t *idxp) /* index of target extent */
  3333. {
  3334. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3335. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3336. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3337. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3338. int high; /* upper boundary in search */
  3339. xfs_extnum_t idx = 0; /* index of target extent */
  3340. int low; /* lower boundary in search */
  3341. xfs_extnum_t nextents; /* number of file extents */
  3342. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3343. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3344. if (nextents == 0) {
  3345. *idxp = 0;
  3346. return NULL;
  3347. }
  3348. low = 0;
  3349. if (ifp->if_flags & XFS_IFEXTIREC) {
  3350. /* Find target extent list */
  3351. int erp_idx = 0;
  3352. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3353. base = erp->er_extbuf;
  3354. high = erp->er_extcount - 1;
  3355. } else {
  3356. base = ifp->if_u1.if_extents;
  3357. high = nextents - 1;
  3358. }
  3359. /* Binary search extent records */
  3360. while (low <= high) {
  3361. idx = (low + high) >> 1;
  3362. ep = base + idx;
  3363. startoff = xfs_bmbt_get_startoff(ep);
  3364. blockcount = xfs_bmbt_get_blockcount(ep);
  3365. if (bno < startoff) {
  3366. high = idx - 1;
  3367. } else if (bno >= startoff + blockcount) {
  3368. low = idx + 1;
  3369. } else {
  3370. /* Convert back to file-based extent index */
  3371. if (ifp->if_flags & XFS_IFEXTIREC) {
  3372. idx += erp->er_extoff;
  3373. }
  3374. *idxp = idx;
  3375. return ep;
  3376. }
  3377. }
  3378. /* Convert back to file-based extent index */
  3379. if (ifp->if_flags & XFS_IFEXTIREC) {
  3380. idx += erp->er_extoff;
  3381. }
  3382. if (bno >= startoff + blockcount) {
  3383. if (++idx == nextents) {
  3384. ep = NULL;
  3385. } else {
  3386. ep = xfs_iext_get_ext(ifp, idx);
  3387. }
  3388. }
  3389. *idxp = idx;
  3390. return ep;
  3391. }
  3392. /*
  3393. * Return a pointer to the indirection array entry containing the
  3394. * extent record for filesystem block bno. Store the index of the
  3395. * target irec in *erp_idxp.
  3396. */
  3397. xfs_ext_irec_t * /* pointer to found extent record */
  3398. xfs_iext_bno_to_irec(
  3399. xfs_ifork_t *ifp, /* inode fork pointer */
  3400. xfs_fileoff_t bno, /* block number to search for */
  3401. int *erp_idxp) /* irec index of target ext list */
  3402. {
  3403. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3404. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3405. int erp_idx; /* indirection array index */
  3406. int nlists; /* number of extent irec's (lists) */
  3407. int high; /* binary search upper limit */
  3408. int low; /* binary search lower limit */
  3409. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3410. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3411. erp_idx = 0;
  3412. low = 0;
  3413. high = nlists - 1;
  3414. while (low <= high) {
  3415. erp_idx = (low + high) >> 1;
  3416. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3417. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3418. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3419. high = erp_idx - 1;
  3420. } else if (erp_next && bno >=
  3421. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3422. low = erp_idx + 1;
  3423. } else {
  3424. break;
  3425. }
  3426. }
  3427. *erp_idxp = erp_idx;
  3428. return erp;
  3429. }
  3430. /*
  3431. * Return a pointer to the indirection array entry containing the
  3432. * extent record at file extent index *idxp. Store the index of the
  3433. * target irec in *erp_idxp and store the page index of the target
  3434. * extent record in *idxp.
  3435. */
  3436. xfs_ext_irec_t *
  3437. xfs_iext_idx_to_irec(
  3438. xfs_ifork_t *ifp, /* inode fork pointer */
  3439. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3440. int *erp_idxp, /* pointer to target irec */
  3441. int realloc) /* new bytes were just added */
  3442. {
  3443. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3444. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3445. int erp_idx; /* indirection array index */
  3446. int nlists; /* number of irec's (ex lists) */
  3447. int high; /* binary search upper limit */
  3448. int low; /* binary search lower limit */
  3449. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3450. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3451. ASSERT(page_idx >= 0);
  3452. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  3453. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  3454. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3455. erp_idx = 0;
  3456. low = 0;
  3457. high = nlists - 1;
  3458. /* Binary search extent irec's */
  3459. while (low <= high) {
  3460. erp_idx = (low + high) >> 1;
  3461. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3462. prev = erp_idx > 0 ? erp - 1 : NULL;
  3463. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3464. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3465. high = erp_idx - 1;
  3466. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3467. (page_idx == erp->er_extoff + erp->er_extcount &&
  3468. !realloc)) {
  3469. low = erp_idx + 1;
  3470. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3471. erp->er_extcount == XFS_LINEAR_EXTS) {
  3472. ASSERT(realloc);
  3473. page_idx = 0;
  3474. erp_idx++;
  3475. erp = erp_idx < nlists ? erp + 1 : NULL;
  3476. break;
  3477. } else {
  3478. page_idx -= erp->er_extoff;
  3479. break;
  3480. }
  3481. }
  3482. *idxp = page_idx;
  3483. *erp_idxp = erp_idx;
  3484. return(erp);
  3485. }
  3486. /*
  3487. * Allocate and initialize an indirection array once the space needed
  3488. * for incore extents increases above XFS_IEXT_BUFSZ.
  3489. */
  3490. void
  3491. xfs_iext_irec_init(
  3492. xfs_ifork_t *ifp) /* inode fork pointer */
  3493. {
  3494. xfs_ext_irec_t *erp; /* indirection array pointer */
  3495. xfs_extnum_t nextents; /* number of extents in file */
  3496. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3497. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3498. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3499. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3500. if (nextents == 0) {
  3501. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3502. } else if (!ifp->if_real_bytes) {
  3503. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3504. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3505. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3506. }
  3507. erp->er_extbuf = ifp->if_u1.if_extents;
  3508. erp->er_extcount = nextents;
  3509. erp->er_extoff = 0;
  3510. ifp->if_flags |= XFS_IFEXTIREC;
  3511. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3512. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3513. ifp->if_u1.if_ext_irec = erp;
  3514. return;
  3515. }
  3516. /*
  3517. * Allocate and initialize a new entry in the indirection array.
  3518. */
  3519. xfs_ext_irec_t *
  3520. xfs_iext_irec_new(
  3521. xfs_ifork_t *ifp, /* inode fork pointer */
  3522. int erp_idx) /* index for new irec */
  3523. {
  3524. xfs_ext_irec_t *erp; /* indirection array pointer */
  3525. int i; /* loop counter */
  3526. int nlists; /* number of irec's (ex lists) */
  3527. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3528. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3529. /* Resize indirection array */
  3530. xfs_iext_realloc_indirect(ifp, ++nlists *
  3531. sizeof(xfs_ext_irec_t));
  3532. /*
  3533. * Move records down in the array so the
  3534. * new page can use erp_idx.
  3535. */
  3536. erp = ifp->if_u1.if_ext_irec;
  3537. for (i = nlists - 1; i > erp_idx; i--) {
  3538. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3539. }
  3540. ASSERT(i == erp_idx);
  3541. /* Initialize new extent record */
  3542. erp = ifp->if_u1.if_ext_irec;
  3543. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3544. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3545. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3546. erp[erp_idx].er_extcount = 0;
  3547. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3548. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3549. return (&erp[erp_idx]);
  3550. }
  3551. /*
  3552. * Remove a record from the indirection array.
  3553. */
  3554. void
  3555. xfs_iext_irec_remove(
  3556. xfs_ifork_t *ifp, /* inode fork pointer */
  3557. int erp_idx) /* irec index to remove */
  3558. {
  3559. xfs_ext_irec_t *erp; /* indirection array pointer */
  3560. int i; /* loop counter */
  3561. int nlists; /* number of irec's (ex lists) */
  3562. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3563. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3564. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3565. if (erp->er_extbuf) {
  3566. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3567. -erp->er_extcount);
  3568. kmem_free(erp->er_extbuf);
  3569. }
  3570. /* Compact extent records */
  3571. erp = ifp->if_u1.if_ext_irec;
  3572. for (i = erp_idx; i < nlists - 1; i++) {
  3573. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  3574. }
  3575. /*
  3576. * Manually free the last extent record from the indirection
  3577. * array. A call to xfs_iext_realloc_indirect() with a size
  3578. * of zero would result in a call to xfs_iext_destroy() which
  3579. * would in turn call this function again, creating a nasty
  3580. * infinite loop.
  3581. */
  3582. if (--nlists) {
  3583. xfs_iext_realloc_indirect(ifp,
  3584. nlists * sizeof(xfs_ext_irec_t));
  3585. } else {
  3586. kmem_free(ifp->if_u1.if_ext_irec);
  3587. }
  3588. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3589. }
  3590. /*
  3591. * This is called to clean up large amounts of unused memory allocated
  3592. * by the indirection array. Before compacting anything though, verify
  3593. * that the indirection array is still needed and switch back to the
  3594. * linear extent list (or even the inline buffer) if possible. The
  3595. * compaction policy is as follows:
  3596. *
  3597. * Full Compaction: Extents fit into a single page (or inline buffer)
  3598. * Partial Compaction: Extents occupy less than 50% of allocated space
  3599. * No Compaction: Extents occupy at least 50% of allocated space
  3600. */
  3601. void
  3602. xfs_iext_irec_compact(
  3603. xfs_ifork_t *ifp) /* inode fork pointer */
  3604. {
  3605. xfs_extnum_t nextents; /* number of extents in file */
  3606. int nlists; /* number of irec's (ex lists) */
  3607. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3608. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3609. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3610. if (nextents == 0) {
  3611. xfs_iext_destroy(ifp);
  3612. } else if (nextents <= XFS_INLINE_EXTS) {
  3613. xfs_iext_indirect_to_direct(ifp);
  3614. xfs_iext_direct_to_inline(ifp, nextents);
  3615. } else if (nextents <= XFS_LINEAR_EXTS) {
  3616. xfs_iext_indirect_to_direct(ifp);
  3617. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  3618. xfs_iext_irec_compact_pages(ifp);
  3619. }
  3620. }
  3621. /*
  3622. * Combine extents from neighboring extent pages.
  3623. */
  3624. void
  3625. xfs_iext_irec_compact_pages(
  3626. xfs_ifork_t *ifp) /* inode fork pointer */
  3627. {
  3628. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  3629. int erp_idx = 0; /* indirection array index */
  3630. int nlists; /* number of irec's (ex lists) */
  3631. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3632. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3633. while (erp_idx < nlists - 1) {
  3634. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3635. erp_next = erp + 1;
  3636. if (erp_next->er_extcount <=
  3637. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  3638. memcpy(&erp->er_extbuf[erp->er_extcount],
  3639. erp_next->er_extbuf, erp_next->er_extcount *
  3640. sizeof(xfs_bmbt_rec_t));
  3641. erp->er_extcount += erp_next->er_extcount;
  3642. /*
  3643. * Free page before removing extent record
  3644. * so er_extoffs don't get modified in
  3645. * xfs_iext_irec_remove.
  3646. */
  3647. kmem_free(erp_next->er_extbuf);
  3648. erp_next->er_extbuf = NULL;
  3649. xfs_iext_irec_remove(ifp, erp_idx + 1);
  3650. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3651. } else {
  3652. erp_idx++;
  3653. }
  3654. }
  3655. }
  3656. /*
  3657. * This is called to update the er_extoff field in the indirection
  3658. * array when extents have been added or removed from one of the
  3659. * extent lists. erp_idx contains the irec index to begin updating
  3660. * at and ext_diff contains the number of extents that were added
  3661. * or removed.
  3662. */
  3663. void
  3664. xfs_iext_irec_update_extoffs(
  3665. xfs_ifork_t *ifp, /* inode fork pointer */
  3666. int erp_idx, /* irec index to update */
  3667. int ext_diff) /* number of new extents */
  3668. {
  3669. int i; /* loop counter */
  3670. int nlists; /* number of irec's (ex lists */
  3671. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3672. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3673. for (i = erp_idx; i < nlists; i++) {
  3674. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  3675. }
  3676. }
  3677. /*
  3678. * Test whether it is appropriate to check an inode for and free post EOF
  3679. * blocks. The 'force' parameter determines whether we should also consider
  3680. * regular files that are marked preallocated or append-only.
  3681. */
  3682. bool
  3683. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  3684. {
  3685. /* prealloc/delalloc exists only on regular files */
  3686. if (!S_ISREG(ip->i_d.di_mode))
  3687. return false;
  3688. /*
  3689. * Zero sized files with no cached pages and delalloc blocks will not
  3690. * have speculative prealloc/delalloc blocks to remove.
  3691. */
  3692. if (VFS_I(ip)->i_size == 0 &&
  3693. VN_CACHED(VFS_I(ip)) == 0 &&
  3694. ip->i_delayed_blks == 0)
  3695. return false;
  3696. /* If we haven't read in the extent list, then don't do it now. */
  3697. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  3698. return false;
  3699. /*
  3700. * Do not free real preallocated or append-only files unless the file
  3701. * has delalloc blocks and we are forced to remove them.
  3702. */
  3703. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  3704. if (!force || ip->i_delayed_blks == 0)
  3705. return false;
  3706. return true;
  3707. }