xfs_inode.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785
  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 "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_imap.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_trans_priv.h"
  27. #include "xfs_sb.h"
  28. #include "xfs_ag.h"
  29. #include "xfs_dir2.h"
  30. #include "xfs_dmapi.h"
  31. #include "xfs_mount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_inode_item.h"
  41. #include "xfs_btree.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_ialloc.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_error.h"
  47. #include "xfs_utils.h"
  48. #include "xfs_dir2_trace.h"
  49. #include "xfs_quota.h"
  50. #include "xfs_acl.h"
  51. #include "xfs_filestream.h"
  52. kmem_zone_t *xfs_ifork_zone;
  53. kmem_zone_t *xfs_inode_zone;
  54. kmem_zone_t *xfs_chashlist_zone;
  55. /*
  56. * Used in xfs_itruncate(). This is the maximum number of extents
  57. * freed from a file in a single transaction.
  58. */
  59. #define XFS_ITRUNC_MAX_EXTENTS 2
  60. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  61. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  62. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  63. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  64. #ifdef DEBUG
  65. /*
  66. * Make sure that the extents in the given memory buffer
  67. * are valid.
  68. */
  69. STATIC void
  70. xfs_validate_extents(
  71. xfs_ifork_t *ifp,
  72. int nrecs,
  73. xfs_exntfmt_t fmt)
  74. {
  75. xfs_bmbt_irec_t irec;
  76. xfs_bmbt_rec_host_t rec;
  77. int i;
  78. for (i = 0; i < nrecs; i++) {
  79. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  80. rec.l0 = get_unaligned(&ep->l0);
  81. rec.l1 = get_unaligned(&ep->l1);
  82. xfs_bmbt_get_all(&rec, &irec);
  83. if (fmt == XFS_EXTFMT_NOSTATE)
  84. ASSERT(irec.br_state == XFS_EXT_NORM);
  85. }
  86. }
  87. #else /* DEBUG */
  88. #define xfs_validate_extents(ifp, nrecs, fmt)
  89. #endif /* DEBUG */
  90. /*
  91. * Check that none of the inode's in the buffer have a next
  92. * unlinked field of 0.
  93. */
  94. #if defined(DEBUG)
  95. void
  96. xfs_inobp_check(
  97. xfs_mount_t *mp,
  98. xfs_buf_t *bp)
  99. {
  100. int i;
  101. int j;
  102. xfs_dinode_t *dip;
  103. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  104. for (i = 0; i < j; i++) {
  105. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  106. i * mp->m_sb.sb_inodesize);
  107. if (!dip->di_next_unlinked) {
  108. xfs_fs_cmn_err(CE_ALERT, mp,
  109. "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
  110. bp);
  111. ASSERT(dip->di_next_unlinked);
  112. }
  113. }
  114. }
  115. #endif
  116. /*
  117. * This routine is called to map an inode number within a file
  118. * system to the buffer containing the on-disk version of the
  119. * inode. It returns a pointer to the buffer containing the
  120. * on-disk inode in the bpp parameter, and in the dip parameter
  121. * it returns a pointer to the on-disk inode within that buffer.
  122. *
  123. * If a non-zero error is returned, then the contents of bpp and
  124. * dipp are undefined.
  125. *
  126. * Use xfs_imap() to determine the size and location of the
  127. * buffer to read from disk.
  128. */
  129. STATIC int
  130. xfs_inotobp(
  131. xfs_mount_t *mp,
  132. xfs_trans_t *tp,
  133. xfs_ino_t ino,
  134. xfs_dinode_t **dipp,
  135. xfs_buf_t **bpp,
  136. int *offset)
  137. {
  138. int di_ok;
  139. xfs_imap_t imap;
  140. xfs_buf_t *bp;
  141. int error;
  142. xfs_dinode_t *dip;
  143. /*
  144. * Call the space management code to find the location of the
  145. * inode on disk.
  146. */
  147. imap.im_blkno = 0;
  148. error = xfs_imap(mp, tp, ino, &imap, XFS_IMAP_LOOKUP);
  149. if (error != 0) {
  150. cmn_err(CE_WARN,
  151. "xfs_inotobp: xfs_imap() returned an "
  152. "error %d on %s. Returning error.", error, mp->m_fsname);
  153. return error;
  154. }
  155. /*
  156. * If the inode number maps to a block outside the bounds of the
  157. * file system then return NULL rather than calling read_buf
  158. * and panicing when we get an error from the driver.
  159. */
  160. if ((imap.im_blkno + imap.im_len) >
  161. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  162. cmn_err(CE_WARN,
  163. "xfs_inotobp: inode number (%llu + %d) maps to a block outside the bounds "
  164. "of the file system %s. Returning EINVAL.",
  165. (unsigned long long)imap.im_blkno,
  166. imap.im_len, mp->m_fsname);
  167. return XFS_ERROR(EINVAL);
  168. }
  169. /*
  170. * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
  171. * default to just a read_buf() call.
  172. */
  173. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
  174. (int)imap.im_len, XFS_BUF_LOCK, &bp);
  175. if (error) {
  176. cmn_err(CE_WARN,
  177. "xfs_inotobp: xfs_trans_read_buf() returned an "
  178. "error %d on %s. Returning error.", error, mp->m_fsname);
  179. return error;
  180. }
  181. dip = (xfs_dinode_t *)xfs_buf_offset(bp, 0);
  182. di_ok =
  183. INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
  184. XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
  185. if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
  186. XFS_RANDOM_ITOBP_INOTOBP))) {
  187. XFS_CORRUPTION_ERROR("xfs_inotobp", XFS_ERRLEVEL_LOW, mp, dip);
  188. xfs_trans_brelse(tp, bp);
  189. cmn_err(CE_WARN,
  190. "xfs_inotobp: XFS_TEST_ERROR() returned an "
  191. "error on %s. Returning EFSCORRUPTED.", mp->m_fsname);
  192. return XFS_ERROR(EFSCORRUPTED);
  193. }
  194. xfs_inobp_check(mp, bp);
  195. /*
  196. * Set *dipp to point to the on-disk inode in the buffer.
  197. */
  198. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  199. *bpp = bp;
  200. *offset = imap.im_boffset;
  201. return 0;
  202. }
  203. /*
  204. * This routine is called to map an inode to the buffer containing
  205. * the on-disk version of the inode. It returns a pointer to the
  206. * buffer containing the on-disk inode in the bpp parameter, and in
  207. * the dip parameter it returns a pointer to the on-disk inode within
  208. * that buffer.
  209. *
  210. * If a non-zero error is returned, then the contents of bpp and
  211. * dipp are undefined.
  212. *
  213. * If the inode is new and has not yet been initialized, use xfs_imap()
  214. * to determine the size and location of the buffer to read from disk.
  215. * If the inode has already been mapped to its buffer and read in once,
  216. * then use the mapping information stored in the inode rather than
  217. * calling xfs_imap(). This allows us to avoid the overhead of looking
  218. * at the inode btree for small block file systems (see xfs_dilocate()).
  219. * We can tell whether the inode has been mapped in before by comparing
  220. * its disk block address to 0. Only uninitialized inodes will have
  221. * 0 for the disk block address.
  222. */
  223. int
  224. xfs_itobp(
  225. xfs_mount_t *mp,
  226. xfs_trans_t *tp,
  227. xfs_inode_t *ip,
  228. xfs_dinode_t **dipp,
  229. xfs_buf_t **bpp,
  230. xfs_daddr_t bno,
  231. uint imap_flags)
  232. {
  233. xfs_imap_t imap;
  234. xfs_buf_t *bp;
  235. int error;
  236. int i;
  237. int ni;
  238. if (ip->i_blkno == (xfs_daddr_t)0) {
  239. /*
  240. * Call the space management code to find the location of the
  241. * inode on disk.
  242. */
  243. imap.im_blkno = bno;
  244. if ((error = xfs_imap(mp, tp, ip->i_ino, &imap,
  245. XFS_IMAP_LOOKUP | imap_flags)))
  246. return error;
  247. /*
  248. * If the inode number maps to a block outside the bounds
  249. * of the file system then return NULL rather than calling
  250. * read_buf and panicing when we get an error from the
  251. * driver.
  252. */
  253. if ((imap.im_blkno + imap.im_len) >
  254. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  255. #ifdef DEBUG
  256. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
  257. "(imap.im_blkno (0x%llx) "
  258. "+ imap.im_len (0x%llx)) > "
  259. " XFS_FSB_TO_BB(mp, "
  260. "mp->m_sb.sb_dblocks) (0x%llx)",
  261. (unsigned long long) imap.im_blkno,
  262. (unsigned long long) imap.im_len,
  263. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  264. #endif /* DEBUG */
  265. return XFS_ERROR(EINVAL);
  266. }
  267. /*
  268. * Fill in the fields in the inode that will be used to
  269. * map the inode to its buffer from now on.
  270. */
  271. ip->i_blkno = imap.im_blkno;
  272. ip->i_len = imap.im_len;
  273. ip->i_boffset = imap.im_boffset;
  274. } else {
  275. /*
  276. * We've already mapped the inode once, so just use the
  277. * mapping that we saved the first time.
  278. */
  279. imap.im_blkno = ip->i_blkno;
  280. imap.im_len = ip->i_len;
  281. imap.im_boffset = ip->i_boffset;
  282. }
  283. ASSERT(bno == 0 || bno == imap.im_blkno);
  284. /*
  285. * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
  286. * default to just a read_buf() call.
  287. */
  288. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
  289. (int)imap.im_len, XFS_BUF_LOCK, &bp);
  290. if (error) {
  291. #ifdef DEBUG
  292. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
  293. "xfs_trans_read_buf() returned error %d, "
  294. "imap.im_blkno 0x%llx, imap.im_len 0x%llx",
  295. error, (unsigned long long) imap.im_blkno,
  296. (unsigned long long) imap.im_len);
  297. #endif /* DEBUG */
  298. return error;
  299. }
  300. /*
  301. * Validate the magic number and version of every inode in the buffer
  302. * (if DEBUG kernel) or the first inode in the buffer, otherwise.
  303. * No validation is done here in userspace (xfs_repair).
  304. */
  305. #if !defined(__KERNEL__)
  306. ni = 0;
  307. #elif defined(DEBUG)
  308. ni = BBTOB(imap.im_len) >> mp->m_sb.sb_inodelog;
  309. #else /* usual case */
  310. ni = 1;
  311. #endif
  312. for (i = 0; i < ni; i++) {
  313. int di_ok;
  314. xfs_dinode_t *dip;
  315. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  316. (i << mp->m_sb.sb_inodelog));
  317. di_ok = INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
  318. XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
  319. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  320. XFS_ERRTAG_ITOBP_INOTOBP,
  321. XFS_RANDOM_ITOBP_INOTOBP))) {
  322. if (imap_flags & XFS_IMAP_BULKSTAT) {
  323. xfs_trans_brelse(tp, bp);
  324. return XFS_ERROR(EINVAL);
  325. }
  326. #ifdef DEBUG
  327. cmn_err(CE_ALERT,
  328. "Device %s - bad inode magic/vsn "
  329. "daddr %lld #%d (magic=%x)",
  330. XFS_BUFTARG_NAME(mp->m_ddev_targp),
  331. (unsigned long long)imap.im_blkno, i,
  332. INT_GET(dip->di_core.di_magic, ARCH_CONVERT));
  333. #endif
  334. XFS_CORRUPTION_ERROR("xfs_itobp", XFS_ERRLEVEL_HIGH,
  335. mp, dip);
  336. xfs_trans_brelse(tp, bp);
  337. return XFS_ERROR(EFSCORRUPTED);
  338. }
  339. }
  340. xfs_inobp_check(mp, bp);
  341. /*
  342. * Mark the buffer as an inode buffer now that it looks good
  343. */
  344. XFS_BUF_SET_VTYPE(bp, B_FS_INO);
  345. /*
  346. * Set *dipp to point to the on-disk inode in the buffer.
  347. */
  348. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  349. *bpp = bp;
  350. return 0;
  351. }
  352. /*
  353. * Move inode type and inode format specific information from the
  354. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  355. * this means set if_rdev to the proper value. For files, directories,
  356. * and symlinks this means to bring in the in-line data or extent
  357. * pointers. For a file in B-tree format, only the root is immediately
  358. * brought in-core. The rest will be in-lined in if_extents when it
  359. * is first referenced (see xfs_iread_extents()).
  360. */
  361. STATIC int
  362. xfs_iformat(
  363. xfs_inode_t *ip,
  364. xfs_dinode_t *dip)
  365. {
  366. xfs_attr_shortform_t *atp;
  367. int size;
  368. int error;
  369. xfs_fsize_t di_size;
  370. ip->i_df.if_ext_max =
  371. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  372. error = 0;
  373. if (unlikely(
  374. INT_GET(dip->di_core.di_nextents, ARCH_CONVERT) +
  375. INT_GET(dip->di_core.di_anextents, ARCH_CONVERT) >
  376. INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT))) {
  377. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  378. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  379. (unsigned long long)ip->i_ino,
  380. (int)(INT_GET(dip->di_core.di_nextents, ARCH_CONVERT)
  381. + INT_GET(dip->di_core.di_anextents, ARCH_CONVERT)),
  382. (unsigned long long)
  383. INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT));
  384. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  385. ip->i_mount, dip);
  386. return XFS_ERROR(EFSCORRUPTED);
  387. }
  388. if (unlikely(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT) > ip->i_mount->m_sb.sb_inodesize)) {
  389. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  390. "corrupt dinode %Lu, forkoff = 0x%x.",
  391. (unsigned long long)ip->i_ino,
  392. (int)(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT)));
  393. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  394. ip->i_mount, dip);
  395. return XFS_ERROR(EFSCORRUPTED);
  396. }
  397. switch (ip->i_d.di_mode & S_IFMT) {
  398. case S_IFIFO:
  399. case S_IFCHR:
  400. case S_IFBLK:
  401. case S_IFSOCK:
  402. if (unlikely(INT_GET(dip->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_DEV)) {
  403. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  404. ip->i_mount, dip);
  405. return XFS_ERROR(EFSCORRUPTED);
  406. }
  407. ip->i_d.di_size = 0;
  408. ip->i_size = 0;
  409. ip->i_df.if_u2.if_rdev = INT_GET(dip->di_u.di_dev, ARCH_CONVERT);
  410. break;
  411. case S_IFREG:
  412. case S_IFLNK:
  413. case S_IFDIR:
  414. switch (INT_GET(dip->di_core.di_format, ARCH_CONVERT)) {
  415. case XFS_DINODE_FMT_LOCAL:
  416. /*
  417. * no local regular files yet
  418. */
  419. if (unlikely((INT_GET(dip->di_core.di_mode, ARCH_CONVERT) & S_IFMT) == S_IFREG)) {
  420. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  421. "corrupt inode %Lu "
  422. "(local format for regular file).",
  423. (unsigned long long) ip->i_ino);
  424. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  425. XFS_ERRLEVEL_LOW,
  426. ip->i_mount, dip);
  427. return XFS_ERROR(EFSCORRUPTED);
  428. }
  429. di_size = INT_GET(dip->di_core.di_size, ARCH_CONVERT);
  430. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  431. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  432. "corrupt inode %Lu "
  433. "(bad size %Ld for local inode).",
  434. (unsigned long long) ip->i_ino,
  435. (long long) di_size);
  436. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  437. XFS_ERRLEVEL_LOW,
  438. ip->i_mount, dip);
  439. return XFS_ERROR(EFSCORRUPTED);
  440. }
  441. size = (int)di_size;
  442. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  443. break;
  444. case XFS_DINODE_FMT_EXTENTS:
  445. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  446. break;
  447. case XFS_DINODE_FMT_BTREE:
  448. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  449. break;
  450. default:
  451. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  452. ip->i_mount);
  453. return XFS_ERROR(EFSCORRUPTED);
  454. }
  455. break;
  456. default:
  457. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  458. return XFS_ERROR(EFSCORRUPTED);
  459. }
  460. if (error) {
  461. return error;
  462. }
  463. if (!XFS_DFORK_Q(dip))
  464. return 0;
  465. ASSERT(ip->i_afp == NULL);
  466. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  467. ip->i_afp->if_ext_max =
  468. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  469. switch (INT_GET(dip->di_core.di_aformat, ARCH_CONVERT)) {
  470. case XFS_DINODE_FMT_LOCAL:
  471. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  472. size = be16_to_cpu(atp->hdr.totsize);
  473. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  474. break;
  475. case XFS_DINODE_FMT_EXTENTS:
  476. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  477. break;
  478. case XFS_DINODE_FMT_BTREE:
  479. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  480. break;
  481. default:
  482. error = XFS_ERROR(EFSCORRUPTED);
  483. break;
  484. }
  485. if (error) {
  486. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  487. ip->i_afp = NULL;
  488. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  489. }
  490. return error;
  491. }
  492. /*
  493. * The file is in-lined in the on-disk inode.
  494. * If it fits into if_inline_data, then copy
  495. * it there, otherwise allocate a buffer for it
  496. * and copy the data there. Either way, set
  497. * if_data to point at the data.
  498. * If we allocate a buffer for the data, make
  499. * sure that its size is a multiple of 4 and
  500. * record the real size in i_real_bytes.
  501. */
  502. STATIC int
  503. xfs_iformat_local(
  504. xfs_inode_t *ip,
  505. xfs_dinode_t *dip,
  506. int whichfork,
  507. int size)
  508. {
  509. xfs_ifork_t *ifp;
  510. int real_size;
  511. /*
  512. * If the size is unreasonable, then something
  513. * is wrong and we just bail out rather than crash in
  514. * kmem_alloc() or memcpy() below.
  515. */
  516. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  517. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  518. "corrupt inode %Lu "
  519. "(bad size %d for local fork, size = %d).",
  520. (unsigned long long) ip->i_ino, size,
  521. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  522. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  523. ip->i_mount, dip);
  524. return XFS_ERROR(EFSCORRUPTED);
  525. }
  526. ifp = XFS_IFORK_PTR(ip, whichfork);
  527. real_size = 0;
  528. if (size == 0)
  529. ifp->if_u1.if_data = NULL;
  530. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  531. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  532. else {
  533. real_size = roundup(size, 4);
  534. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  535. }
  536. ifp->if_bytes = size;
  537. ifp->if_real_bytes = real_size;
  538. if (size)
  539. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  540. ifp->if_flags &= ~XFS_IFEXTENTS;
  541. ifp->if_flags |= XFS_IFINLINE;
  542. return 0;
  543. }
  544. /*
  545. * The file consists of a set of extents all
  546. * of which fit into the on-disk inode.
  547. * If there are few enough extents to fit into
  548. * the if_inline_ext, then copy them there.
  549. * Otherwise allocate a buffer for them and copy
  550. * them into it. Either way, set if_extents
  551. * to point at the extents.
  552. */
  553. STATIC int
  554. xfs_iformat_extents(
  555. xfs_inode_t *ip,
  556. xfs_dinode_t *dip,
  557. int whichfork)
  558. {
  559. xfs_bmbt_rec_t *dp;
  560. xfs_ifork_t *ifp;
  561. int nex;
  562. int size;
  563. int i;
  564. ifp = XFS_IFORK_PTR(ip, whichfork);
  565. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  566. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  567. /*
  568. * If the number of extents is unreasonable, then something
  569. * is wrong and we just bail out rather than crash in
  570. * kmem_alloc() or memcpy() below.
  571. */
  572. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  573. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  574. "corrupt inode %Lu ((a)extents = %d).",
  575. (unsigned long long) ip->i_ino, nex);
  576. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  577. ip->i_mount, dip);
  578. return XFS_ERROR(EFSCORRUPTED);
  579. }
  580. ifp->if_real_bytes = 0;
  581. if (nex == 0)
  582. ifp->if_u1.if_extents = NULL;
  583. else if (nex <= XFS_INLINE_EXTS)
  584. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  585. else
  586. xfs_iext_add(ifp, 0, nex);
  587. ifp->if_bytes = size;
  588. if (size) {
  589. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  590. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  591. for (i = 0; i < nex; i++, dp++) {
  592. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  593. ep->l0 = be64_to_cpu(get_unaligned(&dp->l0));
  594. ep->l1 = be64_to_cpu(get_unaligned(&dp->l1));
  595. }
  596. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  597. if (whichfork != XFS_DATA_FORK ||
  598. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  599. if (unlikely(xfs_check_nostate_extents(
  600. ifp, 0, nex))) {
  601. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  602. XFS_ERRLEVEL_LOW,
  603. ip->i_mount);
  604. return XFS_ERROR(EFSCORRUPTED);
  605. }
  606. }
  607. ifp->if_flags |= XFS_IFEXTENTS;
  608. return 0;
  609. }
  610. /*
  611. * The file has too many extents to fit into
  612. * the inode, so they are in B-tree format.
  613. * Allocate a buffer for the root of the B-tree
  614. * and copy the root into it. The i_extents
  615. * field will remain NULL until all of the
  616. * extents are read in (when they are needed).
  617. */
  618. STATIC int
  619. xfs_iformat_btree(
  620. xfs_inode_t *ip,
  621. xfs_dinode_t *dip,
  622. int whichfork)
  623. {
  624. xfs_bmdr_block_t *dfp;
  625. xfs_ifork_t *ifp;
  626. /* REFERENCED */
  627. int nrecs;
  628. int size;
  629. ifp = XFS_IFORK_PTR(ip, whichfork);
  630. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  631. size = XFS_BMAP_BROOT_SPACE(dfp);
  632. nrecs = XFS_BMAP_BROOT_NUMRECS(dfp);
  633. /*
  634. * blow out if -- fork has less extents than can fit in
  635. * fork (fork shouldn't be a btree format), root btree
  636. * block has more records than can fit into the fork,
  637. * or the number of extents is greater than the number of
  638. * blocks.
  639. */
  640. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
  641. || XFS_BMDR_SPACE_CALC(nrecs) >
  642. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
  643. || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  644. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  645. "corrupt inode %Lu (btree).",
  646. (unsigned long long) ip->i_ino);
  647. XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  648. ip->i_mount);
  649. return XFS_ERROR(EFSCORRUPTED);
  650. }
  651. ifp->if_broot_bytes = size;
  652. ifp->if_broot = kmem_alloc(size, KM_SLEEP);
  653. ASSERT(ifp->if_broot != NULL);
  654. /*
  655. * Copy and convert from the on-disk structure
  656. * to the in-memory structure.
  657. */
  658. xfs_bmdr_to_bmbt(dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  659. ifp->if_broot, size);
  660. ifp->if_flags &= ~XFS_IFEXTENTS;
  661. ifp->if_flags |= XFS_IFBROOT;
  662. return 0;
  663. }
  664. /*
  665. * xfs_xlate_dinode_core - translate an xfs_inode_core_t between ondisk
  666. * and native format
  667. *
  668. * buf = on-disk representation
  669. * dip = native representation
  670. * dir = direction - +ve -> disk to native
  671. * -ve -> native to disk
  672. */
  673. void
  674. xfs_xlate_dinode_core(
  675. xfs_caddr_t buf,
  676. xfs_dinode_core_t *dip,
  677. int dir)
  678. {
  679. xfs_dinode_core_t *buf_core = (xfs_dinode_core_t *)buf;
  680. xfs_dinode_core_t *mem_core = (xfs_dinode_core_t *)dip;
  681. xfs_arch_t arch = ARCH_CONVERT;
  682. ASSERT(dir);
  683. INT_XLATE(buf_core->di_magic, mem_core->di_magic, dir, arch);
  684. INT_XLATE(buf_core->di_mode, mem_core->di_mode, dir, arch);
  685. INT_XLATE(buf_core->di_version, mem_core->di_version, dir, arch);
  686. INT_XLATE(buf_core->di_format, mem_core->di_format, dir, arch);
  687. INT_XLATE(buf_core->di_onlink, mem_core->di_onlink, dir, arch);
  688. INT_XLATE(buf_core->di_uid, mem_core->di_uid, dir, arch);
  689. INT_XLATE(buf_core->di_gid, mem_core->di_gid, dir, arch);
  690. INT_XLATE(buf_core->di_nlink, mem_core->di_nlink, dir, arch);
  691. INT_XLATE(buf_core->di_projid, mem_core->di_projid, dir, arch);
  692. if (dir > 0) {
  693. memcpy(mem_core->di_pad, buf_core->di_pad,
  694. sizeof(buf_core->di_pad));
  695. } else {
  696. memcpy(buf_core->di_pad, mem_core->di_pad,
  697. sizeof(buf_core->di_pad));
  698. }
  699. INT_XLATE(buf_core->di_flushiter, mem_core->di_flushiter, dir, arch);
  700. INT_XLATE(buf_core->di_atime.t_sec, mem_core->di_atime.t_sec,
  701. dir, arch);
  702. INT_XLATE(buf_core->di_atime.t_nsec, mem_core->di_atime.t_nsec,
  703. dir, arch);
  704. INT_XLATE(buf_core->di_mtime.t_sec, mem_core->di_mtime.t_sec,
  705. dir, arch);
  706. INT_XLATE(buf_core->di_mtime.t_nsec, mem_core->di_mtime.t_nsec,
  707. dir, arch);
  708. INT_XLATE(buf_core->di_ctime.t_sec, mem_core->di_ctime.t_sec,
  709. dir, arch);
  710. INT_XLATE(buf_core->di_ctime.t_nsec, mem_core->di_ctime.t_nsec,
  711. dir, arch);
  712. INT_XLATE(buf_core->di_size, mem_core->di_size, dir, arch);
  713. INT_XLATE(buf_core->di_nblocks, mem_core->di_nblocks, dir, arch);
  714. INT_XLATE(buf_core->di_extsize, mem_core->di_extsize, dir, arch);
  715. INT_XLATE(buf_core->di_nextents, mem_core->di_nextents, dir, arch);
  716. INT_XLATE(buf_core->di_anextents, mem_core->di_anextents, dir, arch);
  717. INT_XLATE(buf_core->di_forkoff, mem_core->di_forkoff, dir, arch);
  718. INT_XLATE(buf_core->di_aformat, mem_core->di_aformat, dir, arch);
  719. INT_XLATE(buf_core->di_dmevmask, mem_core->di_dmevmask, dir, arch);
  720. INT_XLATE(buf_core->di_dmstate, mem_core->di_dmstate, dir, arch);
  721. INT_XLATE(buf_core->di_flags, mem_core->di_flags, dir, arch);
  722. INT_XLATE(buf_core->di_gen, mem_core->di_gen, dir, arch);
  723. }
  724. STATIC uint
  725. _xfs_dic2xflags(
  726. __uint16_t di_flags)
  727. {
  728. uint flags = 0;
  729. if (di_flags & XFS_DIFLAG_ANY) {
  730. if (di_flags & XFS_DIFLAG_REALTIME)
  731. flags |= XFS_XFLAG_REALTIME;
  732. if (di_flags & XFS_DIFLAG_PREALLOC)
  733. flags |= XFS_XFLAG_PREALLOC;
  734. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  735. flags |= XFS_XFLAG_IMMUTABLE;
  736. if (di_flags & XFS_DIFLAG_APPEND)
  737. flags |= XFS_XFLAG_APPEND;
  738. if (di_flags & XFS_DIFLAG_SYNC)
  739. flags |= XFS_XFLAG_SYNC;
  740. if (di_flags & XFS_DIFLAG_NOATIME)
  741. flags |= XFS_XFLAG_NOATIME;
  742. if (di_flags & XFS_DIFLAG_NODUMP)
  743. flags |= XFS_XFLAG_NODUMP;
  744. if (di_flags & XFS_DIFLAG_RTINHERIT)
  745. flags |= XFS_XFLAG_RTINHERIT;
  746. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  747. flags |= XFS_XFLAG_PROJINHERIT;
  748. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  749. flags |= XFS_XFLAG_NOSYMLINKS;
  750. if (di_flags & XFS_DIFLAG_EXTSIZE)
  751. flags |= XFS_XFLAG_EXTSIZE;
  752. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  753. flags |= XFS_XFLAG_EXTSZINHERIT;
  754. if (di_flags & XFS_DIFLAG_NODEFRAG)
  755. flags |= XFS_XFLAG_NODEFRAG;
  756. if (di_flags & XFS_DIFLAG_FILESTREAM)
  757. flags |= XFS_XFLAG_FILESTREAM;
  758. }
  759. return flags;
  760. }
  761. uint
  762. xfs_ip2xflags(
  763. xfs_inode_t *ip)
  764. {
  765. xfs_dinode_core_t *dic = &ip->i_d;
  766. return _xfs_dic2xflags(dic->di_flags) |
  767. (XFS_CFORK_Q(dic) ? XFS_XFLAG_HASATTR : 0);
  768. }
  769. uint
  770. xfs_dic2xflags(
  771. xfs_dinode_core_t *dic)
  772. {
  773. return _xfs_dic2xflags(INT_GET(dic->di_flags, ARCH_CONVERT)) |
  774. (XFS_CFORK_Q_DISK(dic) ? XFS_XFLAG_HASATTR : 0);
  775. }
  776. /*
  777. * Given a mount structure and an inode number, return a pointer
  778. * to a newly allocated in-core inode corresponding to the given
  779. * inode number.
  780. *
  781. * Initialize the inode's attributes and extent pointers if it
  782. * already has them (it will not if the inode has no links).
  783. */
  784. int
  785. xfs_iread(
  786. xfs_mount_t *mp,
  787. xfs_trans_t *tp,
  788. xfs_ino_t ino,
  789. xfs_inode_t **ipp,
  790. xfs_daddr_t bno,
  791. uint imap_flags)
  792. {
  793. xfs_buf_t *bp;
  794. xfs_dinode_t *dip;
  795. xfs_inode_t *ip;
  796. int error;
  797. ASSERT(xfs_inode_zone != NULL);
  798. ip = kmem_zone_zalloc(xfs_inode_zone, KM_SLEEP);
  799. ip->i_ino = ino;
  800. ip->i_mount = mp;
  801. spin_lock_init(&ip->i_flags_lock);
  802. /*
  803. * Get pointer's to the on-disk inode and the buffer containing it.
  804. * If the inode number refers to a block outside the file system
  805. * then xfs_itobp() will return NULL. In this case we should
  806. * return NULL as well. Set i_blkno to 0 so that xfs_itobp() will
  807. * know that this is a new incore inode.
  808. */
  809. error = xfs_itobp(mp, tp, ip, &dip, &bp, bno, imap_flags);
  810. if (error) {
  811. kmem_zone_free(xfs_inode_zone, ip);
  812. return error;
  813. }
  814. /*
  815. * Initialize inode's trace buffers.
  816. * Do this before xfs_iformat in case it adds entries.
  817. */
  818. #ifdef XFS_BMAP_TRACE
  819. ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_SLEEP);
  820. #endif
  821. #ifdef XFS_BMBT_TRACE
  822. ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_SLEEP);
  823. #endif
  824. #ifdef XFS_RW_TRACE
  825. ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_SLEEP);
  826. #endif
  827. #ifdef XFS_ILOCK_TRACE
  828. ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_SLEEP);
  829. #endif
  830. #ifdef XFS_DIR2_TRACE
  831. ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_SLEEP);
  832. #endif
  833. /*
  834. * If we got something that isn't an inode it means someone
  835. * (nfs or dmi) has a stale handle.
  836. */
  837. if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT) != XFS_DINODE_MAGIC) {
  838. kmem_zone_free(xfs_inode_zone, ip);
  839. xfs_trans_brelse(tp, bp);
  840. #ifdef DEBUG
  841. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  842. "dip->di_core.di_magic (0x%x) != "
  843. "XFS_DINODE_MAGIC (0x%x)",
  844. INT_GET(dip->di_core.di_magic, ARCH_CONVERT),
  845. XFS_DINODE_MAGIC);
  846. #endif /* DEBUG */
  847. return XFS_ERROR(EINVAL);
  848. }
  849. /*
  850. * If the on-disk inode is already linked to a directory
  851. * entry, copy all of the inode into the in-core inode.
  852. * xfs_iformat() handles copying in the inode format
  853. * specific information.
  854. * Otherwise, just get the truly permanent information.
  855. */
  856. if (dip->di_core.di_mode) {
  857. xfs_xlate_dinode_core((xfs_caddr_t)&dip->di_core,
  858. &(ip->i_d), 1);
  859. error = xfs_iformat(ip, dip);
  860. if (error) {
  861. kmem_zone_free(xfs_inode_zone, ip);
  862. xfs_trans_brelse(tp, bp);
  863. #ifdef DEBUG
  864. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  865. "xfs_iformat() returned error %d",
  866. error);
  867. #endif /* DEBUG */
  868. return error;
  869. }
  870. } else {
  871. ip->i_d.di_magic = INT_GET(dip->di_core.di_magic, ARCH_CONVERT);
  872. ip->i_d.di_version = INT_GET(dip->di_core.di_version, ARCH_CONVERT);
  873. ip->i_d.di_gen = INT_GET(dip->di_core.di_gen, ARCH_CONVERT);
  874. ip->i_d.di_flushiter = INT_GET(dip->di_core.di_flushiter, ARCH_CONVERT);
  875. /*
  876. * Make sure to pull in the mode here as well in
  877. * case the inode is released without being used.
  878. * This ensures that xfs_inactive() will see that
  879. * the inode is already free and not try to mess
  880. * with the uninitialized part of it.
  881. */
  882. ip->i_d.di_mode = 0;
  883. /*
  884. * Initialize the per-fork minima and maxima for a new
  885. * inode here. xfs_iformat will do it for old inodes.
  886. */
  887. ip->i_df.if_ext_max =
  888. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  889. }
  890. INIT_LIST_HEAD(&ip->i_reclaim);
  891. /*
  892. * The inode format changed when we moved the link count and
  893. * made it 32 bits long. If this is an old format inode,
  894. * convert it in memory to look like a new one. If it gets
  895. * flushed to disk we will convert back before flushing or
  896. * logging it. We zero out the new projid field and the old link
  897. * count field. We'll handle clearing the pad field (the remains
  898. * of the old uuid field) when we actually convert the inode to
  899. * the new format. We don't change the version number so that we
  900. * can distinguish this from a real new format inode.
  901. */
  902. if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  903. ip->i_d.di_nlink = ip->i_d.di_onlink;
  904. ip->i_d.di_onlink = 0;
  905. ip->i_d.di_projid = 0;
  906. }
  907. ip->i_delayed_blks = 0;
  908. ip->i_size = ip->i_d.di_size;
  909. /*
  910. * Mark the buffer containing the inode as something to keep
  911. * around for a while. This helps to keep recently accessed
  912. * meta-data in-core longer.
  913. */
  914. XFS_BUF_SET_REF(bp, XFS_INO_REF);
  915. /*
  916. * Use xfs_trans_brelse() to release the buffer containing the
  917. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  918. * in xfs_itobp() above. If tp is NULL, this is just a normal
  919. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  920. * will only release the buffer if it is not dirty within the
  921. * transaction. It will be OK to release the buffer in this case,
  922. * because inodes on disk are never destroyed and we will be
  923. * locking the new in-core inode before putting it in the hash
  924. * table where other processes can find it. Thus we don't have
  925. * to worry about the inode being changed just because we released
  926. * the buffer.
  927. */
  928. xfs_trans_brelse(tp, bp);
  929. *ipp = ip;
  930. return 0;
  931. }
  932. /*
  933. * Read in extents from a btree-format inode.
  934. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  935. */
  936. int
  937. xfs_iread_extents(
  938. xfs_trans_t *tp,
  939. xfs_inode_t *ip,
  940. int whichfork)
  941. {
  942. int error;
  943. xfs_ifork_t *ifp;
  944. xfs_extnum_t nextents;
  945. size_t size;
  946. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  947. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  948. ip->i_mount);
  949. return XFS_ERROR(EFSCORRUPTED);
  950. }
  951. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  952. size = nextents * sizeof(xfs_bmbt_rec_t);
  953. ifp = XFS_IFORK_PTR(ip, whichfork);
  954. /*
  955. * We know that the size is valid (it's checked in iformat_btree)
  956. */
  957. ifp->if_lastex = NULLEXTNUM;
  958. ifp->if_bytes = ifp->if_real_bytes = 0;
  959. ifp->if_flags |= XFS_IFEXTENTS;
  960. xfs_iext_add(ifp, 0, nextents);
  961. error = xfs_bmap_read_extents(tp, ip, whichfork);
  962. if (error) {
  963. xfs_iext_destroy(ifp);
  964. ifp->if_flags &= ~XFS_IFEXTENTS;
  965. return error;
  966. }
  967. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  968. return 0;
  969. }
  970. /*
  971. * Allocate an inode on disk and return a copy of its in-core version.
  972. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  973. * appropriately within the inode. The uid and gid for the inode are
  974. * set according to the contents of the given cred structure.
  975. *
  976. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  977. * has a free inode available, call xfs_iget()
  978. * to obtain the in-core version of the allocated inode. Finally,
  979. * fill in the inode and log its initial contents. In this case,
  980. * ialloc_context would be set to NULL and call_again set to false.
  981. *
  982. * If xfs_dialloc() does not have an available inode,
  983. * it will replenish its supply by doing an allocation. Since we can
  984. * only do one allocation within a transaction without deadlocks, we
  985. * must commit the current transaction before returning the inode itself.
  986. * In this case, therefore, we will set call_again to true and return.
  987. * The caller should then commit the current transaction, start a new
  988. * transaction, and call xfs_ialloc() again to actually get the inode.
  989. *
  990. * To ensure that some other process does not grab the inode that
  991. * was allocated during the first call to xfs_ialloc(), this routine
  992. * also returns the [locked] bp pointing to the head of the freelist
  993. * as ialloc_context. The caller should hold this buffer across
  994. * the commit and pass it back into this routine on the second call.
  995. *
  996. * If we are allocating quota inodes, we do not have a parent inode
  997. * to attach to or associate with (i.e. pip == NULL) because they
  998. * are not linked into the directory structure - they are attached
  999. * directly to the superblock - and so have no parent.
  1000. */
  1001. int
  1002. xfs_ialloc(
  1003. xfs_trans_t *tp,
  1004. xfs_inode_t *pip,
  1005. mode_t mode,
  1006. xfs_nlink_t nlink,
  1007. xfs_dev_t rdev,
  1008. cred_t *cr,
  1009. xfs_prid_t prid,
  1010. int okalloc,
  1011. xfs_buf_t **ialloc_context,
  1012. boolean_t *call_again,
  1013. xfs_inode_t **ipp)
  1014. {
  1015. xfs_ino_t ino;
  1016. xfs_inode_t *ip;
  1017. bhv_vnode_t *vp;
  1018. uint flags;
  1019. int error;
  1020. /*
  1021. * Call the space management code to pick
  1022. * the on-disk inode to be allocated.
  1023. */
  1024. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  1025. ialloc_context, call_again, &ino);
  1026. if (error != 0) {
  1027. return error;
  1028. }
  1029. if (*call_again || ino == NULLFSINO) {
  1030. *ipp = NULL;
  1031. return 0;
  1032. }
  1033. ASSERT(*ialloc_context == NULL);
  1034. /*
  1035. * Get the in-core inode with the lock held exclusively.
  1036. * This is because we're setting fields here we need
  1037. * to prevent others from looking at until we're done.
  1038. */
  1039. error = xfs_trans_iget(tp->t_mountp, tp, ino,
  1040. XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
  1041. if (error != 0) {
  1042. return error;
  1043. }
  1044. ASSERT(ip != NULL);
  1045. vp = XFS_ITOV(ip);
  1046. ip->i_d.di_mode = (__uint16_t)mode;
  1047. ip->i_d.di_onlink = 0;
  1048. ip->i_d.di_nlink = nlink;
  1049. ASSERT(ip->i_d.di_nlink == nlink);
  1050. ip->i_d.di_uid = current_fsuid(cr);
  1051. ip->i_d.di_gid = current_fsgid(cr);
  1052. ip->i_d.di_projid = prid;
  1053. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  1054. /*
  1055. * If the superblock version is up to where we support new format
  1056. * inodes and this is currently an old format inode, then change
  1057. * the inode version number now. This way we only do the conversion
  1058. * here rather than here and in the flush/logging code.
  1059. */
  1060. if (XFS_SB_VERSION_HASNLINK(&tp->t_mountp->m_sb) &&
  1061. ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  1062. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  1063. /*
  1064. * We've already zeroed the old link count, the projid field,
  1065. * and the pad field.
  1066. */
  1067. }
  1068. /*
  1069. * Project ids won't be stored on disk if we are using a version 1 inode.
  1070. */
  1071. if ((prid != 0) && (ip->i_d.di_version == XFS_DINODE_VERSION_1))
  1072. xfs_bump_ino_vers2(tp, ip);
  1073. if (pip && XFS_INHERIT_GID(pip, vp->v_vfsp)) {
  1074. ip->i_d.di_gid = pip->i_d.di_gid;
  1075. if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) {
  1076. ip->i_d.di_mode |= S_ISGID;
  1077. }
  1078. }
  1079. /*
  1080. * If the group ID of the new file does not match the effective group
  1081. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  1082. * (and only if the irix_sgid_inherit compatibility variable is set).
  1083. */
  1084. if ((irix_sgid_inherit) &&
  1085. (ip->i_d.di_mode & S_ISGID) &&
  1086. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  1087. ip->i_d.di_mode &= ~S_ISGID;
  1088. }
  1089. ip->i_d.di_size = 0;
  1090. ip->i_size = 0;
  1091. ip->i_d.di_nextents = 0;
  1092. ASSERT(ip->i_d.di_nblocks == 0);
  1093. xfs_ichgtime(ip, XFS_ICHGTIME_CHG|XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD);
  1094. /*
  1095. * di_gen will have been taken care of in xfs_iread.
  1096. */
  1097. ip->i_d.di_extsize = 0;
  1098. ip->i_d.di_dmevmask = 0;
  1099. ip->i_d.di_dmstate = 0;
  1100. ip->i_d.di_flags = 0;
  1101. flags = XFS_ILOG_CORE;
  1102. switch (mode & S_IFMT) {
  1103. case S_IFIFO:
  1104. case S_IFCHR:
  1105. case S_IFBLK:
  1106. case S_IFSOCK:
  1107. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  1108. ip->i_df.if_u2.if_rdev = rdev;
  1109. ip->i_df.if_flags = 0;
  1110. flags |= XFS_ILOG_DEV;
  1111. break;
  1112. case S_IFREG:
  1113. if (pip && xfs_inode_is_filestream(pip)) {
  1114. error = xfs_filestream_associate(pip, ip);
  1115. if (error < 0)
  1116. return -error;
  1117. if (!error)
  1118. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1119. }
  1120. /* fall through */
  1121. case S_IFDIR:
  1122. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  1123. uint di_flags = 0;
  1124. if ((mode & S_IFMT) == S_IFDIR) {
  1125. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1126. di_flags |= XFS_DIFLAG_RTINHERIT;
  1127. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1128. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  1129. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1130. }
  1131. } else if ((mode & S_IFMT) == S_IFREG) {
  1132. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) {
  1133. di_flags |= XFS_DIFLAG_REALTIME;
  1134. ip->i_iocore.io_flags |= XFS_IOCORE_RT;
  1135. }
  1136. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1137. di_flags |= XFS_DIFLAG_EXTSIZE;
  1138. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1139. }
  1140. }
  1141. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1142. xfs_inherit_noatime)
  1143. di_flags |= XFS_DIFLAG_NOATIME;
  1144. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1145. xfs_inherit_nodump)
  1146. di_flags |= XFS_DIFLAG_NODUMP;
  1147. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1148. xfs_inherit_sync)
  1149. di_flags |= XFS_DIFLAG_SYNC;
  1150. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1151. xfs_inherit_nosymlinks)
  1152. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1153. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1154. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1155. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  1156. xfs_inherit_nodefrag)
  1157. di_flags |= XFS_DIFLAG_NODEFRAG;
  1158. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  1159. di_flags |= XFS_DIFLAG_FILESTREAM;
  1160. ip->i_d.di_flags |= di_flags;
  1161. }
  1162. /* FALLTHROUGH */
  1163. case S_IFLNK:
  1164. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1165. ip->i_df.if_flags = XFS_IFEXTENTS;
  1166. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1167. ip->i_df.if_u1.if_extents = NULL;
  1168. break;
  1169. default:
  1170. ASSERT(0);
  1171. }
  1172. /*
  1173. * Attribute fork settings for new inode.
  1174. */
  1175. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1176. ip->i_d.di_anextents = 0;
  1177. /*
  1178. * Log the new values stuffed into the inode.
  1179. */
  1180. xfs_trans_log_inode(tp, ip, flags);
  1181. /* now that we have an i_mode we can setup inode ops and unlock */
  1182. bhv_vfs_init_vnode(XFS_MTOVFS(tp->t_mountp), vp, XFS_ITOBHV(ip), 1);
  1183. *ipp = ip;
  1184. return 0;
  1185. }
  1186. /*
  1187. * Check to make sure that there are no blocks allocated to the
  1188. * file beyond the size of the file. We don't check this for
  1189. * files with fixed size extents or real time extents, but we
  1190. * at least do it for regular files.
  1191. */
  1192. #ifdef DEBUG
  1193. void
  1194. xfs_isize_check(
  1195. xfs_mount_t *mp,
  1196. xfs_inode_t *ip,
  1197. xfs_fsize_t isize)
  1198. {
  1199. xfs_fileoff_t map_first;
  1200. int nimaps;
  1201. xfs_bmbt_irec_t imaps[2];
  1202. if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
  1203. return;
  1204. if (ip->i_d.di_flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_EXTSIZE))
  1205. return;
  1206. nimaps = 2;
  1207. map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  1208. /*
  1209. * The filesystem could be shutting down, so bmapi may return
  1210. * an error.
  1211. */
  1212. if (xfs_bmapi(NULL, ip, map_first,
  1213. (XFS_B_TO_FSB(mp,
  1214. (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
  1215. map_first),
  1216. XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
  1217. NULL, NULL))
  1218. return;
  1219. ASSERT(nimaps == 1);
  1220. ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
  1221. }
  1222. #endif /* DEBUG */
  1223. /*
  1224. * Calculate the last possible buffered byte in a file. This must
  1225. * include data that was buffered beyond the EOF by the write code.
  1226. * This also needs to deal with overflowing the xfs_fsize_t type
  1227. * which can happen for sizes near the limit.
  1228. *
  1229. * We also need to take into account any blocks beyond the EOF. It
  1230. * may be the case that they were buffered by a write which failed.
  1231. * In that case the pages will still be in memory, but the inode size
  1232. * will never have been updated.
  1233. */
  1234. xfs_fsize_t
  1235. xfs_file_last_byte(
  1236. xfs_inode_t *ip)
  1237. {
  1238. xfs_mount_t *mp;
  1239. xfs_fsize_t last_byte;
  1240. xfs_fileoff_t last_block;
  1241. xfs_fileoff_t size_last_block;
  1242. int error;
  1243. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE | MR_ACCESS));
  1244. mp = ip->i_mount;
  1245. /*
  1246. * Only check for blocks beyond the EOF if the extents have
  1247. * been read in. This eliminates the need for the inode lock,
  1248. * and it also saves us from looking when it really isn't
  1249. * necessary.
  1250. */
  1251. if (ip->i_df.if_flags & XFS_IFEXTENTS) {
  1252. error = xfs_bmap_last_offset(NULL, ip, &last_block,
  1253. XFS_DATA_FORK);
  1254. if (error) {
  1255. last_block = 0;
  1256. }
  1257. } else {
  1258. last_block = 0;
  1259. }
  1260. size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size);
  1261. last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
  1262. last_byte = XFS_FSB_TO_B(mp, last_block);
  1263. if (last_byte < 0) {
  1264. return XFS_MAXIOFFSET(mp);
  1265. }
  1266. last_byte += (1 << mp->m_writeio_log);
  1267. if (last_byte < 0) {
  1268. return XFS_MAXIOFFSET(mp);
  1269. }
  1270. return last_byte;
  1271. }
  1272. #if defined(XFS_RW_TRACE)
  1273. STATIC void
  1274. xfs_itrunc_trace(
  1275. int tag,
  1276. xfs_inode_t *ip,
  1277. int flag,
  1278. xfs_fsize_t new_size,
  1279. xfs_off_t toss_start,
  1280. xfs_off_t toss_finish)
  1281. {
  1282. if (ip->i_rwtrace == NULL) {
  1283. return;
  1284. }
  1285. ktrace_enter(ip->i_rwtrace,
  1286. (void*)((long)tag),
  1287. (void*)ip,
  1288. (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
  1289. (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
  1290. (void*)((long)flag),
  1291. (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
  1292. (void*)(unsigned long)(new_size & 0xffffffff),
  1293. (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
  1294. (void*)(unsigned long)(toss_start & 0xffffffff),
  1295. (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
  1296. (void*)(unsigned long)(toss_finish & 0xffffffff),
  1297. (void*)(unsigned long)current_cpu(),
  1298. (void*)(unsigned long)current_pid(),
  1299. (void*)NULL,
  1300. (void*)NULL,
  1301. (void*)NULL);
  1302. }
  1303. #else
  1304. #define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
  1305. #endif
  1306. /*
  1307. * Start the truncation of the file to new_size. The new size
  1308. * must be smaller than the current size. This routine will
  1309. * clear the buffer and page caches of file data in the removed
  1310. * range, and xfs_itruncate_finish() will remove the underlying
  1311. * disk blocks.
  1312. *
  1313. * The inode must have its I/O lock locked EXCLUSIVELY, and it
  1314. * must NOT have the inode lock held at all. This is because we're
  1315. * calling into the buffer/page cache code and we can't hold the
  1316. * inode lock when we do so.
  1317. *
  1318. * We need to wait for any direct I/Os in flight to complete before we
  1319. * proceed with the truncate. This is needed to prevent the extents
  1320. * being read or written by the direct I/Os from being removed while the
  1321. * I/O is in flight as there is no other method of synchronising
  1322. * direct I/O with the truncate operation. Also, because we hold
  1323. * the IOLOCK in exclusive mode, we prevent new direct I/Os from being
  1324. * started until the truncate completes and drops the lock. Essentially,
  1325. * the vn_iowait() call forms an I/O barrier that provides strict ordering
  1326. * between direct I/Os and the truncate operation.
  1327. *
  1328. * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
  1329. * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
  1330. * in the case that the caller is locking things out of order and
  1331. * may not be able to call xfs_itruncate_finish() with the inode lock
  1332. * held without dropping the I/O lock. If the caller must drop the
  1333. * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
  1334. * must be called again with all the same restrictions as the initial
  1335. * call.
  1336. */
  1337. int
  1338. xfs_itruncate_start(
  1339. xfs_inode_t *ip,
  1340. uint flags,
  1341. xfs_fsize_t new_size)
  1342. {
  1343. xfs_fsize_t last_byte;
  1344. xfs_off_t toss_start;
  1345. xfs_mount_t *mp;
  1346. bhv_vnode_t *vp;
  1347. int error = 0;
  1348. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
  1349. ASSERT((new_size == 0) || (new_size <= ip->i_size));
  1350. ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
  1351. (flags == XFS_ITRUNC_MAYBE));
  1352. mp = ip->i_mount;
  1353. vp = XFS_ITOV(ip);
  1354. vn_iowait(vp); /* wait for the completion of any pending DIOs */
  1355. /*
  1356. * Call toss_pages or flushinval_pages to get rid of pages
  1357. * overlapping the region being removed. We have to use
  1358. * the less efficient flushinval_pages in the case that the
  1359. * caller may not be able to finish the truncate without
  1360. * dropping the inode's I/O lock. Make sure
  1361. * to catch any pages brought in by buffers overlapping
  1362. * the EOF by searching out beyond the isize by our
  1363. * block size. We round new_size up to a block boundary
  1364. * so that we don't toss things on the same block as
  1365. * new_size but before it.
  1366. *
  1367. * Before calling toss_page or flushinval_pages, make sure to
  1368. * call remapf() over the same region if the file is mapped.
  1369. * This frees up mapped file references to the pages in the
  1370. * given range and for the flushinval_pages case it ensures
  1371. * that we get the latest mapped changes flushed out.
  1372. */
  1373. toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1374. toss_start = XFS_FSB_TO_B(mp, toss_start);
  1375. if (toss_start < 0) {
  1376. /*
  1377. * The place to start tossing is beyond our maximum
  1378. * file size, so there is no way that the data extended
  1379. * out there.
  1380. */
  1381. return 0;
  1382. }
  1383. last_byte = xfs_file_last_byte(ip);
  1384. xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
  1385. last_byte);
  1386. if (last_byte > toss_start) {
  1387. if (flags & XFS_ITRUNC_DEFINITE) {
  1388. bhv_vop_toss_pages(vp, toss_start, -1, FI_REMAPF_LOCKED);
  1389. } else {
  1390. error = bhv_vop_flushinval_pages(vp, toss_start, -1, FI_REMAPF_LOCKED);
  1391. }
  1392. }
  1393. #ifdef DEBUG
  1394. if (new_size == 0) {
  1395. ASSERT(VN_CACHED(vp) == 0);
  1396. }
  1397. #endif
  1398. return error;
  1399. }
  1400. /*
  1401. * Shrink the file to the given new_size. The new
  1402. * size must be smaller than the current size.
  1403. * This will free up the underlying blocks
  1404. * in the removed range after a call to xfs_itruncate_start()
  1405. * or xfs_atruncate_start().
  1406. *
  1407. * The transaction passed to this routine must have made
  1408. * a permanent log reservation of at least XFS_ITRUNCATE_LOG_RES.
  1409. * This routine may commit the given transaction and
  1410. * start new ones, so make sure everything involved in
  1411. * the transaction is tidy before calling here.
  1412. * Some transaction will be returned to the caller to be
  1413. * committed. The incoming transaction must already include
  1414. * the inode, and both inode locks must be held exclusively.
  1415. * The inode must also be "held" within the transaction. On
  1416. * return the inode will be "held" within the returned transaction.
  1417. * This routine does NOT require any disk space to be reserved
  1418. * for it within the transaction.
  1419. *
  1420. * The fork parameter must be either xfs_attr_fork or xfs_data_fork,
  1421. * and it indicates the fork which is to be truncated. For the
  1422. * attribute fork we only support truncation to size 0.
  1423. *
  1424. * We use the sync parameter to indicate whether or not the first
  1425. * transaction we perform might have to be synchronous. For the attr fork,
  1426. * it needs to be so if the unlink of the inode is not yet known to be
  1427. * permanent in the log. This keeps us from freeing and reusing the
  1428. * blocks of the attribute fork before the unlink of the inode becomes
  1429. * permanent.
  1430. *
  1431. * For the data fork, we normally have to run synchronously if we're
  1432. * being called out of the inactive path or we're being called
  1433. * out of the create path where we're truncating an existing file.
  1434. * Either way, the truncate needs to be sync so blocks don't reappear
  1435. * in the file with altered data in case of a crash. wsync filesystems
  1436. * can run the first case async because anything that shrinks the inode
  1437. * has to run sync so by the time we're called here from inactive, the
  1438. * inode size is permanently set to 0.
  1439. *
  1440. * Calls from the truncate path always need to be sync unless we're
  1441. * in a wsync filesystem and the file has already been unlinked.
  1442. *
  1443. * The caller is responsible for correctly setting the sync parameter.
  1444. * It gets too hard for us to guess here which path we're being called
  1445. * out of just based on inode state.
  1446. */
  1447. int
  1448. xfs_itruncate_finish(
  1449. xfs_trans_t **tp,
  1450. xfs_inode_t *ip,
  1451. xfs_fsize_t new_size,
  1452. int fork,
  1453. int sync)
  1454. {
  1455. xfs_fsblock_t first_block;
  1456. xfs_fileoff_t first_unmap_block;
  1457. xfs_fileoff_t last_block;
  1458. xfs_filblks_t unmap_len=0;
  1459. xfs_mount_t *mp;
  1460. xfs_trans_t *ntp;
  1461. int done;
  1462. int committed;
  1463. xfs_bmap_free_t free_list;
  1464. int error;
  1465. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
  1466. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
  1467. ASSERT((new_size == 0) || (new_size <= ip->i_size));
  1468. ASSERT(*tp != NULL);
  1469. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  1470. ASSERT(ip->i_transp == *tp);
  1471. ASSERT(ip->i_itemp != NULL);
  1472. ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
  1473. ntp = *tp;
  1474. mp = (ntp)->t_mountp;
  1475. ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
  1476. /*
  1477. * We only support truncating the entire attribute fork.
  1478. */
  1479. if (fork == XFS_ATTR_FORK) {
  1480. new_size = 0LL;
  1481. }
  1482. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1483. xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
  1484. /*
  1485. * The first thing we do is set the size to new_size permanently
  1486. * on disk. This way we don't have to worry about anyone ever
  1487. * being able to look at the data being freed even in the face
  1488. * of a crash. What we're getting around here is the case where
  1489. * we free a block, it is allocated to another file, it is written
  1490. * to, and then we crash. If the new data gets written to the
  1491. * file but the log buffers containing the free and reallocation
  1492. * don't, then we'd end up with garbage in the blocks being freed.
  1493. * As long as we make the new_size permanent before actually
  1494. * freeing any blocks it doesn't matter if they get writtten to.
  1495. *
  1496. * The callers must signal into us whether or not the size
  1497. * setting here must be synchronous. There are a few cases
  1498. * where it doesn't have to be synchronous. Those cases
  1499. * occur if the file is unlinked and we know the unlink is
  1500. * permanent or if the blocks being truncated are guaranteed
  1501. * to be beyond the inode eof (regardless of the link count)
  1502. * and the eof value is permanent. Both of these cases occur
  1503. * only on wsync-mounted filesystems. In those cases, we're
  1504. * guaranteed that no user will ever see the data in the blocks
  1505. * that are being truncated so the truncate can run async.
  1506. * In the free beyond eof case, the file may wind up with
  1507. * more blocks allocated to it than it needs if we crash
  1508. * and that won't get fixed until the next time the file
  1509. * is re-opened and closed but that's ok as that shouldn't
  1510. * be too many blocks.
  1511. *
  1512. * However, we can't just make all wsync xactions run async
  1513. * because there's one call out of the create path that needs
  1514. * to run sync where it's truncating an existing file to size
  1515. * 0 whose size is > 0.
  1516. *
  1517. * It's probably possible to come up with a test in this
  1518. * routine that would correctly distinguish all the above
  1519. * cases from the values of the function parameters and the
  1520. * inode state but for sanity's sake, I've decided to let the
  1521. * layers above just tell us. It's simpler to correctly figure
  1522. * out in the layer above exactly under what conditions we
  1523. * can run async and I think it's easier for others read and
  1524. * follow the logic in case something has to be changed.
  1525. * cscope is your friend -- rcc.
  1526. *
  1527. * The attribute fork is much simpler.
  1528. *
  1529. * For the attribute fork we allow the caller to tell us whether
  1530. * the unlink of the inode that led to this call is yet permanent
  1531. * in the on disk log. If it is not and we will be freeing extents
  1532. * in this inode then we make the first transaction synchronous
  1533. * to make sure that the unlink is permanent by the time we free
  1534. * the blocks.
  1535. */
  1536. if (fork == XFS_DATA_FORK) {
  1537. if (ip->i_d.di_nextents > 0) {
  1538. /*
  1539. * If we are not changing the file size then do
  1540. * not update the on-disk file size - we may be
  1541. * called from xfs_inactive_free_eofblocks(). If we
  1542. * update the on-disk file size and then the system
  1543. * crashes before the contents of the file are
  1544. * flushed to disk then the files may be full of
  1545. * holes (ie NULL files bug).
  1546. */
  1547. if (ip->i_size != new_size) {
  1548. ip->i_d.di_size = new_size;
  1549. ip->i_size = new_size;
  1550. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1551. }
  1552. }
  1553. } else if (sync) {
  1554. ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
  1555. if (ip->i_d.di_anextents > 0)
  1556. xfs_trans_set_sync(ntp);
  1557. }
  1558. ASSERT(fork == XFS_DATA_FORK ||
  1559. (fork == XFS_ATTR_FORK &&
  1560. ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
  1561. (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
  1562. /*
  1563. * Since it is possible for space to become allocated beyond
  1564. * the end of the file (in a crash where the space is allocated
  1565. * but the inode size is not yet updated), simply remove any
  1566. * blocks which show up between the new EOF and the maximum
  1567. * possible file size. If the first block to be removed is
  1568. * beyond the maximum file size (ie it is the same as last_block),
  1569. * then there is nothing to do.
  1570. */
  1571. last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1572. ASSERT(first_unmap_block <= last_block);
  1573. done = 0;
  1574. if (last_block == first_unmap_block) {
  1575. done = 1;
  1576. } else {
  1577. unmap_len = last_block - first_unmap_block + 1;
  1578. }
  1579. while (!done) {
  1580. /*
  1581. * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
  1582. * will tell us whether it freed the entire range or
  1583. * not. If this is a synchronous mount (wsync),
  1584. * then we can tell bunmapi to keep all the
  1585. * transactions asynchronous since the unlink
  1586. * transaction that made this inode inactive has
  1587. * already hit the disk. There's no danger of
  1588. * the freed blocks being reused, there being a
  1589. * crash, and the reused blocks suddenly reappearing
  1590. * in this file with garbage in them once recovery
  1591. * runs.
  1592. */
  1593. XFS_BMAP_INIT(&free_list, &first_block);
  1594. error = XFS_BUNMAPI(mp, ntp, &ip->i_iocore,
  1595. first_unmap_block, unmap_len,
  1596. XFS_BMAPI_AFLAG(fork) |
  1597. (sync ? 0 : XFS_BMAPI_ASYNC),
  1598. XFS_ITRUNC_MAX_EXTENTS,
  1599. &first_block, &free_list,
  1600. NULL, &done);
  1601. if (error) {
  1602. /*
  1603. * If the bunmapi call encounters an error,
  1604. * return to the caller where the transaction
  1605. * can be properly aborted. We just need to
  1606. * make sure we're not holding any resources
  1607. * that we were not when we came in.
  1608. */
  1609. xfs_bmap_cancel(&free_list);
  1610. return error;
  1611. }
  1612. /*
  1613. * Duplicate the transaction that has the permanent
  1614. * reservation and commit the old transaction.
  1615. */
  1616. error = xfs_bmap_finish(tp, &free_list, &committed);
  1617. ntp = *tp;
  1618. if (error) {
  1619. /*
  1620. * If the bmap finish call encounters an error,
  1621. * return to the caller where the transaction
  1622. * can be properly aborted. We just need to
  1623. * make sure we're not holding any resources
  1624. * that we were not when we came in.
  1625. *
  1626. * Aborting from this point might lose some
  1627. * blocks in the file system, but oh well.
  1628. */
  1629. xfs_bmap_cancel(&free_list);
  1630. if (committed) {
  1631. /*
  1632. * If the passed in transaction committed
  1633. * in xfs_bmap_finish(), then we want to
  1634. * add the inode to this one before returning.
  1635. * This keeps things simple for the higher
  1636. * level code, because it always knows that
  1637. * the inode is locked and held in the
  1638. * transaction that returns to it whether
  1639. * errors occur or not. We don't mark the
  1640. * inode dirty so that this transaction can
  1641. * be easily aborted if possible.
  1642. */
  1643. xfs_trans_ijoin(ntp, ip,
  1644. XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1645. xfs_trans_ihold(ntp, ip);
  1646. }
  1647. return error;
  1648. }
  1649. if (committed) {
  1650. /*
  1651. * The first xact was committed,
  1652. * so add the inode to the new one.
  1653. * Mark it dirty so it will be logged
  1654. * and moved forward in the log as
  1655. * part of every commit.
  1656. */
  1657. xfs_trans_ijoin(ntp, ip,
  1658. XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1659. xfs_trans_ihold(ntp, ip);
  1660. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1661. }
  1662. ntp = xfs_trans_dup(ntp);
  1663. (void) xfs_trans_commit(*tp, 0);
  1664. *tp = ntp;
  1665. error = xfs_trans_reserve(ntp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1666. XFS_TRANS_PERM_LOG_RES,
  1667. XFS_ITRUNCATE_LOG_COUNT);
  1668. /*
  1669. * Add the inode being truncated to the next chained
  1670. * transaction.
  1671. */
  1672. xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1673. xfs_trans_ihold(ntp, ip);
  1674. if (error)
  1675. return (error);
  1676. }
  1677. /*
  1678. * Only update the size in the case of the data fork, but
  1679. * always re-log the inode so that our permanent transaction
  1680. * can keep on rolling it forward in the log.
  1681. */
  1682. if (fork == XFS_DATA_FORK) {
  1683. xfs_isize_check(mp, ip, new_size);
  1684. /*
  1685. * If we are not changing the file size then do
  1686. * not update the on-disk file size - we may be
  1687. * called from xfs_inactive_free_eofblocks(). If we
  1688. * update the on-disk file size and then the system
  1689. * crashes before the contents of the file are
  1690. * flushed to disk then the files may be full of
  1691. * holes (ie NULL files bug).
  1692. */
  1693. if (ip->i_size != new_size) {
  1694. ip->i_d.di_size = new_size;
  1695. ip->i_size = new_size;
  1696. }
  1697. }
  1698. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1699. ASSERT((new_size != 0) ||
  1700. (fork == XFS_ATTR_FORK) ||
  1701. (ip->i_delayed_blks == 0));
  1702. ASSERT((new_size != 0) ||
  1703. (fork == XFS_ATTR_FORK) ||
  1704. (ip->i_d.di_nextents == 0));
  1705. xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
  1706. return 0;
  1707. }
  1708. /*
  1709. * xfs_igrow_start
  1710. *
  1711. * Do the first part of growing a file: zero any data in the last
  1712. * block that is beyond the old EOF. We need to do this before
  1713. * the inode is joined to the transaction to modify the i_size.
  1714. * That way we can drop the inode lock and call into the buffer
  1715. * cache to get the buffer mapping the EOF.
  1716. */
  1717. int
  1718. xfs_igrow_start(
  1719. xfs_inode_t *ip,
  1720. xfs_fsize_t new_size,
  1721. cred_t *credp)
  1722. {
  1723. int error;
  1724. ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
  1725. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
  1726. ASSERT(new_size > ip->i_size);
  1727. /*
  1728. * Zero any pages that may have been created by
  1729. * xfs_write_file() beyond the end of the file
  1730. * and any blocks between the old and new file sizes.
  1731. */
  1732. error = xfs_zero_eof(XFS_ITOV(ip), &ip->i_iocore, new_size,
  1733. ip->i_size);
  1734. return error;
  1735. }
  1736. /*
  1737. * xfs_igrow_finish
  1738. *
  1739. * This routine is called to extend the size of a file.
  1740. * The inode must have both the iolock and the ilock locked
  1741. * for update and it must be a part of the current transaction.
  1742. * The xfs_igrow_start() function must have been called previously.
  1743. * If the change_flag is not zero, the inode change timestamp will
  1744. * be updated.
  1745. */
  1746. void
  1747. xfs_igrow_finish(
  1748. xfs_trans_t *tp,
  1749. xfs_inode_t *ip,
  1750. xfs_fsize_t new_size,
  1751. int change_flag)
  1752. {
  1753. ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
  1754. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
  1755. ASSERT(ip->i_transp == tp);
  1756. ASSERT(new_size > ip->i_size);
  1757. /*
  1758. * Update the file size. Update the inode change timestamp
  1759. * if change_flag set.
  1760. */
  1761. ip->i_d.di_size = new_size;
  1762. ip->i_size = new_size;
  1763. if (change_flag)
  1764. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  1765. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1766. }
  1767. /*
  1768. * This is called when the inode's link count goes to 0.
  1769. * We place the on-disk inode on a list in the AGI. It
  1770. * will be pulled from this list when the inode is freed.
  1771. */
  1772. int
  1773. xfs_iunlink(
  1774. xfs_trans_t *tp,
  1775. xfs_inode_t *ip)
  1776. {
  1777. xfs_mount_t *mp;
  1778. xfs_agi_t *agi;
  1779. xfs_dinode_t *dip;
  1780. xfs_buf_t *agibp;
  1781. xfs_buf_t *ibp;
  1782. xfs_agnumber_t agno;
  1783. xfs_daddr_t agdaddr;
  1784. xfs_agino_t agino;
  1785. short bucket_index;
  1786. int offset;
  1787. int error;
  1788. int agi_ok;
  1789. ASSERT(ip->i_d.di_nlink == 0);
  1790. ASSERT(ip->i_d.di_mode != 0);
  1791. ASSERT(ip->i_transp == tp);
  1792. mp = tp->t_mountp;
  1793. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1794. agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
  1795. /*
  1796. * Get the agi buffer first. It ensures lock ordering
  1797. * on the list.
  1798. */
  1799. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
  1800. XFS_FSS_TO_BB(mp, 1), 0, &agibp);
  1801. if (error) {
  1802. return error;
  1803. }
  1804. /*
  1805. * Validate the magic number of the agi block.
  1806. */
  1807. agi = XFS_BUF_TO_AGI(agibp);
  1808. agi_ok =
  1809. be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
  1810. XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
  1811. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK,
  1812. XFS_RANDOM_IUNLINK))) {
  1813. XFS_CORRUPTION_ERROR("xfs_iunlink", XFS_ERRLEVEL_LOW, mp, agi);
  1814. xfs_trans_brelse(tp, agibp);
  1815. return XFS_ERROR(EFSCORRUPTED);
  1816. }
  1817. /*
  1818. * Get the index into the agi hash table for the
  1819. * list this inode will go on.
  1820. */
  1821. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1822. ASSERT(agino != 0);
  1823. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1824. ASSERT(agi->agi_unlinked[bucket_index]);
  1825. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1826. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) {
  1827. /*
  1828. * There is already another inode in the bucket we need
  1829. * to add ourselves to. Add us at the front of the list.
  1830. * Here we put the head pointer into our next pointer,
  1831. * and then we fall through to point the head at us.
  1832. */
  1833. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
  1834. if (error) {
  1835. return error;
  1836. }
  1837. ASSERT(INT_GET(dip->di_next_unlinked, ARCH_CONVERT) == NULLAGINO);
  1838. ASSERT(dip->di_next_unlinked);
  1839. /* both on-disk, don't endian flip twice */
  1840. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1841. offset = ip->i_boffset +
  1842. offsetof(xfs_dinode_t, di_next_unlinked);
  1843. xfs_trans_inode_buf(tp, ibp);
  1844. xfs_trans_log_buf(tp, ibp, offset,
  1845. (offset + sizeof(xfs_agino_t) - 1));
  1846. xfs_inobp_check(mp, ibp);
  1847. }
  1848. /*
  1849. * Point the bucket head pointer at the inode being inserted.
  1850. */
  1851. ASSERT(agino != 0);
  1852. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1853. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1854. (sizeof(xfs_agino_t) * bucket_index);
  1855. xfs_trans_log_buf(tp, agibp, offset,
  1856. (offset + sizeof(xfs_agino_t) - 1));
  1857. return 0;
  1858. }
  1859. /*
  1860. * Pull the on-disk inode from the AGI unlinked list.
  1861. */
  1862. STATIC int
  1863. xfs_iunlink_remove(
  1864. xfs_trans_t *tp,
  1865. xfs_inode_t *ip)
  1866. {
  1867. xfs_ino_t next_ino;
  1868. xfs_mount_t *mp;
  1869. xfs_agi_t *agi;
  1870. xfs_dinode_t *dip;
  1871. xfs_buf_t *agibp;
  1872. xfs_buf_t *ibp;
  1873. xfs_agnumber_t agno;
  1874. xfs_daddr_t agdaddr;
  1875. xfs_agino_t agino;
  1876. xfs_agino_t next_agino;
  1877. xfs_buf_t *last_ibp;
  1878. xfs_dinode_t *last_dip = NULL;
  1879. short bucket_index;
  1880. int offset, last_offset = 0;
  1881. int error;
  1882. int agi_ok;
  1883. /*
  1884. * First pull the on-disk inode from the AGI unlinked list.
  1885. */
  1886. mp = tp->t_mountp;
  1887. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1888. agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
  1889. /*
  1890. * Get the agi buffer first. It ensures lock ordering
  1891. * on the list.
  1892. */
  1893. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
  1894. XFS_FSS_TO_BB(mp, 1), 0, &agibp);
  1895. if (error) {
  1896. cmn_err(CE_WARN,
  1897. "xfs_iunlink_remove: xfs_trans_read_buf() returned an error %d on %s. Returning error.",
  1898. error, mp->m_fsname);
  1899. return error;
  1900. }
  1901. /*
  1902. * Validate the magic number of the agi block.
  1903. */
  1904. agi = XFS_BUF_TO_AGI(agibp);
  1905. agi_ok =
  1906. be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
  1907. XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
  1908. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK_REMOVE,
  1909. XFS_RANDOM_IUNLINK_REMOVE))) {
  1910. XFS_CORRUPTION_ERROR("xfs_iunlink_remove", XFS_ERRLEVEL_LOW,
  1911. mp, agi);
  1912. xfs_trans_brelse(tp, agibp);
  1913. cmn_err(CE_WARN,
  1914. "xfs_iunlink_remove: XFS_TEST_ERROR() returned an error on %s. Returning EFSCORRUPTED.",
  1915. mp->m_fsname);
  1916. return XFS_ERROR(EFSCORRUPTED);
  1917. }
  1918. /*
  1919. * Get the index into the agi hash table for the
  1920. * list this inode will go on.
  1921. */
  1922. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1923. ASSERT(agino != 0);
  1924. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1925. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO);
  1926. ASSERT(agi->agi_unlinked[bucket_index]);
  1927. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1928. /*
  1929. * We're at the head of the list. Get the inode's
  1930. * on-disk buffer to see if there is anyone after us
  1931. * on the list. Only modify our next pointer if it
  1932. * is not already NULLAGINO. This saves us the overhead
  1933. * of dealing with the buffer when there is no need to
  1934. * change it.
  1935. */
  1936. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
  1937. if (error) {
  1938. cmn_err(CE_WARN,
  1939. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  1940. error, mp->m_fsname);
  1941. return error;
  1942. }
  1943. next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
  1944. ASSERT(next_agino != 0);
  1945. if (next_agino != NULLAGINO) {
  1946. INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
  1947. offset = ip->i_boffset +
  1948. offsetof(xfs_dinode_t, di_next_unlinked);
  1949. xfs_trans_inode_buf(tp, ibp);
  1950. xfs_trans_log_buf(tp, ibp, offset,
  1951. (offset + sizeof(xfs_agino_t) - 1));
  1952. xfs_inobp_check(mp, ibp);
  1953. } else {
  1954. xfs_trans_brelse(tp, ibp);
  1955. }
  1956. /*
  1957. * Point the bucket head pointer at the next inode.
  1958. */
  1959. ASSERT(next_agino != 0);
  1960. ASSERT(next_agino != agino);
  1961. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1962. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1963. (sizeof(xfs_agino_t) * bucket_index);
  1964. xfs_trans_log_buf(tp, agibp, offset,
  1965. (offset + sizeof(xfs_agino_t) - 1));
  1966. } else {
  1967. /*
  1968. * We need to search the list for the inode being freed.
  1969. */
  1970. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1971. last_ibp = NULL;
  1972. while (next_agino != agino) {
  1973. /*
  1974. * If the last inode wasn't the one pointing to
  1975. * us, then release its buffer since we're not
  1976. * going to do anything with it.
  1977. */
  1978. if (last_ibp != NULL) {
  1979. xfs_trans_brelse(tp, last_ibp);
  1980. }
  1981. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1982. error = xfs_inotobp(mp, tp, next_ino, &last_dip,
  1983. &last_ibp, &last_offset);
  1984. if (error) {
  1985. cmn_err(CE_WARN,
  1986. "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.",
  1987. error, mp->m_fsname);
  1988. return error;
  1989. }
  1990. next_agino = INT_GET(last_dip->di_next_unlinked, ARCH_CONVERT);
  1991. ASSERT(next_agino != NULLAGINO);
  1992. ASSERT(next_agino != 0);
  1993. }
  1994. /*
  1995. * Now last_ibp points to the buffer previous to us on
  1996. * the unlinked list. Pull us from the list.
  1997. */
  1998. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
  1999. if (error) {
  2000. cmn_err(CE_WARN,
  2001. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  2002. error, mp->m_fsname);
  2003. return error;
  2004. }
  2005. next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
  2006. ASSERT(next_agino != 0);
  2007. ASSERT(next_agino != agino);
  2008. if (next_agino != NULLAGINO) {
  2009. INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
  2010. offset = ip->i_boffset +
  2011. offsetof(xfs_dinode_t, di_next_unlinked);
  2012. xfs_trans_inode_buf(tp, ibp);
  2013. xfs_trans_log_buf(tp, ibp, offset,
  2014. (offset + sizeof(xfs_agino_t) - 1));
  2015. xfs_inobp_check(mp, ibp);
  2016. } else {
  2017. xfs_trans_brelse(tp, ibp);
  2018. }
  2019. /*
  2020. * Point the previous inode on the list to the next inode.
  2021. */
  2022. INT_SET(last_dip->di_next_unlinked, ARCH_CONVERT, next_agino);
  2023. ASSERT(next_agino != 0);
  2024. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  2025. xfs_trans_inode_buf(tp, last_ibp);
  2026. xfs_trans_log_buf(tp, last_ibp, offset,
  2027. (offset + sizeof(xfs_agino_t) - 1));
  2028. xfs_inobp_check(mp, last_ibp);
  2029. }
  2030. return 0;
  2031. }
  2032. STATIC_INLINE int xfs_inode_clean(xfs_inode_t *ip)
  2033. {
  2034. return (((ip->i_itemp == NULL) ||
  2035. !(ip->i_itemp->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
  2036. (ip->i_update_core == 0));
  2037. }
  2038. STATIC void
  2039. xfs_ifree_cluster(
  2040. xfs_inode_t *free_ip,
  2041. xfs_trans_t *tp,
  2042. xfs_ino_t inum)
  2043. {
  2044. xfs_mount_t *mp = free_ip->i_mount;
  2045. int blks_per_cluster;
  2046. int nbufs;
  2047. int ninodes;
  2048. int i, j, found, pre_flushed;
  2049. xfs_daddr_t blkno;
  2050. xfs_buf_t *bp;
  2051. xfs_ihash_t *ih;
  2052. xfs_inode_t *ip, **ip_found;
  2053. xfs_inode_log_item_t *iip;
  2054. xfs_log_item_t *lip;
  2055. SPLDECL(s);
  2056. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  2057. blks_per_cluster = 1;
  2058. ninodes = mp->m_sb.sb_inopblock;
  2059. nbufs = XFS_IALLOC_BLOCKS(mp);
  2060. } else {
  2061. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  2062. mp->m_sb.sb_blocksize;
  2063. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  2064. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  2065. }
  2066. ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
  2067. for (j = 0; j < nbufs; j++, inum += ninodes) {
  2068. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  2069. XFS_INO_TO_AGBNO(mp, inum));
  2070. /*
  2071. * Look for each inode in memory and attempt to lock it,
  2072. * we can be racing with flush and tail pushing here.
  2073. * any inode we get the locks on, add to an array of
  2074. * inode items to process later.
  2075. *
  2076. * The get the buffer lock, we could beat a flush
  2077. * or tail pushing thread to the lock here, in which
  2078. * case they will go looking for the inode buffer
  2079. * and fail, we need some other form of interlock
  2080. * here.
  2081. */
  2082. found = 0;
  2083. for (i = 0; i < ninodes; i++) {
  2084. ih = XFS_IHASH(mp, inum + i);
  2085. read_lock(&ih->ih_lock);
  2086. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  2087. if (ip->i_ino == inum + i)
  2088. break;
  2089. }
  2090. /* Inode not in memory or we found it already,
  2091. * nothing to do
  2092. */
  2093. if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) {
  2094. read_unlock(&ih->ih_lock);
  2095. continue;
  2096. }
  2097. if (xfs_inode_clean(ip)) {
  2098. read_unlock(&ih->ih_lock);
  2099. continue;
  2100. }
  2101. /* If we can get the locks then add it to the
  2102. * list, otherwise by the time we get the bp lock
  2103. * below it will already be attached to the
  2104. * inode buffer.
  2105. */
  2106. /* This inode will already be locked - by us, lets
  2107. * keep it that way.
  2108. */
  2109. if (ip == free_ip) {
  2110. if (xfs_iflock_nowait(ip)) {
  2111. xfs_iflags_set(ip, XFS_ISTALE);
  2112. if (xfs_inode_clean(ip)) {
  2113. xfs_ifunlock(ip);
  2114. } else {
  2115. ip_found[found++] = ip;
  2116. }
  2117. }
  2118. read_unlock(&ih->ih_lock);
  2119. continue;
  2120. }
  2121. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2122. if (xfs_iflock_nowait(ip)) {
  2123. xfs_iflags_set(ip, XFS_ISTALE);
  2124. if (xfs_inode_clean(ip)) {
  2125. xfs_ifunlock(ip);
  2126. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2127. } else {
  2128. ip_found[found++] = ip;
  2129. }
  2130. } else {
  2131. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2132. }
  2133. }
  2134. read_unlock(&ih->ih_lock);
  2135. }
  2136. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  2137. mp->m_bsize * blks_per_cluster,
  2138. XFS_BUF_LOCK);
  2139. pre_flushed = 0;
  2140. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  2141. while (lip) {
  2142. if (lip->li_type == XFS_LI_INODE) {
  2143. iip = (xfs_inode_log_item_t *)lip;
  2144. ASSERT(iip->ili_logged == 1);
  2145. lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
  2146. AIL_LOCK(mp,s);
  2147. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  2148. AIL_UNLOCK(mp, s);
  2149. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  2150. pre_flushed++;
  2151. }
  2152. lip = lip->li_bio_list;
  2153. }
  2154. for (i = 0; i < found; i++) {
  2155. ip = ip_found[i];
  2156. iip = ip->i_itemp;
  2157. if (!iip) {
  2158. ip->i_update_core = 0;
  2159. xfs_ifunlock(ip);
  2160. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2161. continue;
  2162. }
  2163. iip->ili_last_fields = iip->ili_format.ilf_fields;
  2164. iip->ili_format.ilf_fields = 0;
  2165. iip->ili_logged = 1;
  2166. AIL_LOCK(mp,s);
  2167. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  2168. AIL_UNLOCK(mp, s);
  2169. xfs_buf_attach_iodone(bp,
  2170. (void(*)(xfs_buf_t*,xfs_log_item_t*))
  2171. xfs_istale_done, (xfs_log_item_t *)iip);
  2172. if (ip != free_ip) {
  2173. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2174. }
  2175. }
  2176. if (found || pre_flushed)
  2177. xfs_trans_stale_inode_buf(tp, bp);
  2178. xfs_trans_binval(tp, bp);
  2179. }
  2180. kmem_free(ip_found, ninodes * sizeof(xfs_inode_t *));
  2181. }
  2182. /*
  2183. * This is called to return an inode to the inode free list.
  2184. * The inode should already be truncated to 0 length and have
  2185. * no pages associated with it. This routine also assumes that
  2186. * the inode is already a part of the transaction.
  2187. *
  2188. * The on-disk copy of the inode will have been added to the list
  2189. * of unlinked inodes in the AGI. We need to remove the inode from
  2190. * that list atomically with respect to freeing it here.
  2191. */
  2192. int
  2193. xfs_ifree(
  2194. xfs_trans_t *tp,
  2195. xfs_inode_t *ip,
  2196. xfs_bmap_free_t *flist)
  2197. {
  2198. int error;
  2199. int delete;
  2200. xfs_ino_t first_ino;
  2201. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  2202. ASSERT(ip->i_transp == tp);
  2203. ASSERT(ip->i_d.di_nlink == 0);
  2204. ASSERT(ip->i_d.di_nextents == 0);
  2205. ASSERT(ip->i_d.di_anextents == 0);
  2206. ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
  2207. ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
  2208. ASSERT(ip->i_d.di_nblocks == 0);
  2209. /*
  2210. * Pull the on-disk inode from the AGI unlinked list.
  2211. */
  2212. error = xfs_iunlink_remove(tp, ip);
  2213. if (error != 0) {
  2214. return error;
  2215. }
  2216. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2217. if (error != 0) {
  2218. return error;
  2219. }
  2220. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2221. ip->i_d.di_flags = 0;
  2222. ip->i_d.di_dmevmask = 0;
  2223. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2224. ip->i_df.if_ext_max =
  2225. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  2226. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2227. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2228. /*
  2229. * Bump the generation count so no one will be confused
  2230. * by reincarnations of this inode.
  2231. */
  2232. ip->i_d.di_gen++;
  2233. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2234. if (delete) {
  2235. xfs_ifree_cluster(ip, tp, first_ino);
  2236. }
  2237. return 0;
  2238. }
  2239. /*
  2240. * Reallocate the space for if_broot based on the number of records
  2241. * being added or deleted as indicated in rec_diff. Move the records
  2242. * and pointers in if_broot to fit the new size. When shrinking this
  2243. * will eliminate holes between the records and pointers created by
  2244. * the caller. When growing this will create holes to be filled in
  2245. * by the caller.
  2246. *
  2247. * The caller must not request to add more records than would fit in
  2248. * the on-disk inode root. If the if_broot is currently NULL, then
  2249. * if we adding records one will be allocated. The caller must also
  2250. * not request that the number of records go below zero, although
  2251. * it can go to zero.
  2252. *
  2253. * ip -- the inode whose if_broot area is changing
  2254. * ext_diff -- the change in the number of records, positive or negative,
  2255. * requested for the if_broot array.
  2256. */
  2257. void
  2258. xfs_iroot_realloc(
  2259. xfs_inode_t *ip,
  2260. int rec_diff,
  2261. int whichfork)
  2262. {
  2263. int cur_max;
  2264. xfs_ifork_t *ifp;
  2265. xfs_bmbt_block_t *new_broot;
  2266. int new_max;
  2267. size_t new_size;
  2268. char *np;
  2269. char *op;
  2270. /*
  2271. * Handle the degenerate case quietly.
  2272. */
  2273. if (rec_diff == 0) {
  2274. return;
  2275. }
  2276. ifp = XFS_IFORK_PTR(ip, whichfork);
  2277. if (rec_diff > 0) {
  2278. /*
  2279. * If there wasn't any memory allocated before, just
  2280. * allocate it now and get out.
  2281. */
  2282. if (ifp->if_broot_bytes == 0) {
  2283. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  2284. ifp->if_broot = (xfs_bmbt_block_t*)kmem_alloc(new_size,
  2285. KM_SLEEP);
  2286. ifp->if_broot_bytes = (int)new_size;
  2287. return;
  2288. }
  2289. /*
  2290. * If there is already an existing if_broot, then we need
  2291. * to realloc() it and shift the pointers to their new
  2292. * location. The records don't change location because
  2293. * they are kept butted up against the btree block header.
  2294. */
  2295. cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
  2296. new_max = cur_max + rec_diff;
  2297. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2298. ifp->if_broot = (xfs_bmbt_block_t *)
  2299. kmem_realloc(ifp->if_broot,
  2300. new_size,
  2301. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  2302. KM_SLEEP);
  2303. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2304. ifp->if_broot_bytes);
  2305. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2306. (int)new_size);
  2307. ifp->if_broot_bytes = (int)new_size;
  2308. ASSERT(ifp->if_broot_bytes <=
  2309. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2310. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  2311. return;
  2312. }
  2313. /*
  2314. * rec_diff is less than 0. In this case, we are shrinking the
  2315. * if_broot buffer. It must already exist. If we go to zero
  2316. * records, just get rid of the root and clear the status bit.
  2317. */
  2318. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  2319. cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
  2320. new_max = cur_max + rec_diff;
  2321. ASSERT(new_max >= 0);
  2322. if (new_max > 0)
  2323. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2324. else
  2325. new_size = 0;
  2326. if (new_size > 0) {
  2327. new_broot = (xfs_bmbt_block_t *)kmem_alloc(new_size, KM_SLEEP);
  2328. /*
  2329. * First copy over the btree block header.
  2330. */
  2331. memcpy(new_broot, ifp->if_broot, sizeof(xfs_bmbt_block_t));
  2332. } else {
  2333. new_broot = NULL;
  2334. ifp->if_flags &= ~XFS_IFBROOT;
  2335. }
  2336. /*
  2337. * Only copy the records and pointers if there are any.
  2338. */
  2339. if (new_max > 0) {
  2340. /*
  2341. * First copy the records.
  2342. */
  2343. op = (char *)XFS_BMAP_BROOT_REC_ADDR(ifp->if_broot, 1,
  2344. ifp->if_broot_bytes);
  2345. np = (char *)XFS_BMAP_BROOT_REC_ADDR(new_broot, 1,
  2346. (int)new_size);
  2347. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  2348. /*
  2349. * Then copy the pointers.
  2350. */
  2351. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2352. ifp->if_broot_bytes);
  2353. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(new_broot, 1,
  2354. (int)new_size);
  2355. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  2356. }
  2357. kmem_free(ifp->if_broot, ifp->if_broot_bytes);
  2358. ifp->if_broot = new_broot;
  2359. ifp->if_broot_bytes = (int)new_size;
  2360. ASSERT(ifp->if_broot_bytes <=
  2361. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2362. return;
  2363. }
  2364. /*
  2365. * This is called when the amount of space needed for if_data
  2366. * is increased or decreased. The change in size is indicated by
  2367. * the number of bytes that need to be added or deleted in the
  2368. * byte_diff parameter.
  2369. *
  2370. * If the amount of space needed has decreased below the size of the
  2371. * inline buffer, then switch to using the inline buffer. Otherwise,
  2372. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2373. * to what is needed.
  2374. *
  2375. * ip -- the inode whose if_data area is changing
  2376. * byte_diff -- the change in the number of bytes, positive or negative,
  2377. * requested for the if_data array.
  2378. */
  2379. void
  2380. xfs_idata_realloc(
  2381. xfs_inode_t *ip,
  2382. int byte_diff,
  2383. int whichfork)
  2384. {
  2385. xfs_ifork_t *ifp;
  2386. int new_size;
  2387. int real_size;
  2388. if (byte_diff == 0) {
  2389. return;
  2390. }
  2391. ifp = XFS_IFORK_PTR(ip, whichfork);
  2392. new_size = (int)ifp->if_bytes + byte_diff;
  2393. ASSERT(new_size >= 0);
  2394. if (new_size == 0) {
  2395. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2396. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2397. }
  2398. ifp->if_u1.if_data = NULL;
  2399. real_size = 0;
  2400. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  2401. /*
  2402. * If the valid extents/data can fit in if_inline_ext/data,
  2403. * copy them from the malloc'd vector and free it.
  2404. */
  2405. if (ifp->if_u1.if_data == NULL) {
  2406. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2407. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2408. ASSERT(ifp->if_real_bytes != 0);
  2409. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  2410. new_size);
  2411. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2412. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2413. }
  2414. real_size = 0;
  2415. } else {
  2416. /*
  2417. * Stuck with malloc/realloc.
  2418. * For inline data, the underlying buffer must be
  2419. * a multiple of 4 bytes in size so that it can be
  2420. * logged and stay on word boundaries. We enforce
  2421. * that here.
  2422. */
  2423. real_size = roundup(new_size, 4);
  2424. if (ifp->if_u1.if_data == NULL) {
  2425. ASSERT(ifp->if_real_bytes == 0);
  2426. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2427. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2428. /*
  2429. * Only do the realloc if the underlying size
  2430. * is really changing.
  2431. */
  2432. if (ifp->if_real_bytes != real_size) {
  2433. ifp->if_u1.if_data =
  2434. kmem_realloc(ifp->if_u1.if_data,
  2435. real_size,
  2436. ifp->if_real_bytes,
  2437. KM_SLEEP);
  2438. }
  2439. } else {
  2440. ASSERT(ifp->if_real_bytes == 0);
  2441. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2442. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  2443. ifp->if_bytes);
  2444. }
  2445. }
  2446. ifp->if_real_bytes = real_size;
  2447. ifp->if_bytes = new_size;
  2448. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2449. }
  2450. /*
  2451. * Map inode to disk block and offset.
  2452. *
  2453. * mp -- the mount point structure for the current file system
  2454. * tp -- the current transaction
  2455. * ino -- the inode number of the inode to be located
  2456. * imap -- this structure is filled in with the information necessary
  2457. * to retrieve the given inode from disk
  2458. * flags -- flags to pass to xfs_dilocate indicating whether or not
  2459. * lookups in the inode btree were OK or not
  2460. */
  2461. int
  2462. xfs_imap(
  2463. xfs_mount_t *mp,
  2464. xfs_trans_t *tp,
  2465. xfs_ino_t ino,
  2466. xfs_imap_t *imap,
  2467. uint flags)
  2468. {
  2469. xfs_fsblock_t fsbno;
  2470. int len;
  2471. int off;
  2472. int error;
  2473. fsbno = imap->im_blkno ?
  2474. XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK;
  2475. error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags);
  2476. if (error != 0) {
  2477. return error;
  2478. }
  2479. imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno);
  2480. imap->im_len = XFS_FSB_TO_BB(mp, len);
  2481. imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno);
  2482. imap->im_ioffset = (ushort)off;
  2483. imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog);
  2484. return 0;
  2485. }
  2486. void
  2487. xfs_idestroy_fork(
  2488. xfs_inode_t *ip,
  2489. int whichfork)
  2490. {
  2491. xfs_ifork_t *ifp;
  2492. ifp = XFS_IFORK_PTR(ip, whichfork);
  2493. if (ifp->if_broot != NULL) {
  2494. kmem_free(ifp->if_broot, ifp->if_broot_bytes);
  2495. ifp->if_broot = NULL;
  2496. }
  2497. /*
  2498. * If the format is local, then we can't have an extents
  2499. * array so just look for an inline data array. If we're
  2500. * not local then we may or may not have an extents list,
  2501. * so check and free it up if we do.
  2502. */
  2503. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  2504. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  2505. (ifp->if_u1.if_data != NULL)) {
  2506. ASSERT(ifp->if_real_bytes != 0);
  2507. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2508. ifp->if_u1.if_data = NULL;
  2509. ifp->if_real_bytes = 0;
  2510. }
  2511. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  2512. ((ifp->if_flags & XFS_IFEXTIREC) ||
  2513. ((ifp->if_u1.if_extents != NULL) &&
  2514. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  2515. ASSERT(ifp->if_real_bytes != 0);
  2516. xfs_iext_destroy(ifp);
  2517. }
  2518. ASSERT(ifp->if_u1.if_extents == NULL ||
  2519. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  2520. ASSERT(ifp->if_real_bytes == 0);
  2521. if (whichfork == XFS_ATTR_FORK) {
  2522. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  2523. ip->i_afp = NULL;
  2524. }
  2525. }
  2526. /*
  2527. * This is called free all the memory associated with an inode.
  2528. * It must free the inode itself and any buffers allocated for
  2529. * if_extents/if_data and if_broot. It must also free the lock
  2530. * associated with the inode.
  2531. */
  2532. void
  2533. xfs_idestroy(
  2534. xfs_inode_t *ip)
  2535. {
  2536. switch (ip->i_d.di_mode & S_IFMT) {
  2537. case S_IFREG:
  2538. case S_IFDIR:
  2539. case S_IFLNK:
  2540. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  2541. break;
  2542. }
  2543. if (ip->i_afp)
  2544. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  2545. mrfree(&ip->i_lock);
  2546. mrfree(&ip->i_iolock);
  2547. freesema(&ip->i_flock);
  2548. #ifdef XFS_BMAP_TRACE
  2549. ktrace_free(ip->i_xtrace);
  2550. #endif
  2551. #ifdef XFS_BMBT_TRACE
  2552. ktrace_free(ip->i_btrace);
  2553. #endif
  2554. #ifdef XFS_RW_TRACE
  2555. ktrace_free(ip->i_rwtrace);
  2556. #endif
  2557. #ifdef XFS_ILOCK_TRACE
  2558. ktrace_free(ip->i_lock_trace);
  2559. #endif
  2560. #ifdef XFS_DIR2_TRACE
  2561. ktrace_free(ip->i_dir_trace);
  2562. #endif
  2563. if (ip->i_itemp) {
  2564. /*
  2565. * Only if we are shutting down the fs will we see an
  2566. * inode still in the AIL. If it is there, we should remove
  2567. * it to prevent a use-after-free from occurring.
  2568. */
  2569. xfs_mount_t *mp = ip->i_mount;
  2570. xfs_log_item_t *lip = &ip->i_itemp->ili_item;
  2571. int s;
  2572. ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
  2573. XFS_FORCED_SHUTDOWN(ip->i_mount));
  2574. if (lip->li_flags & XFS_LI_IN_AIL) {
  2575. AIL_LOCK(mp, s);
  2576. if (lip->li_flags & XFS_LI_IN_AIL)
  2577. xfs_trans_delete_ail(mp, lip, s);
  2578. else
  2579. AIL_UNLOCK(mp, s);
  2580. }
  2581. xfs_inode_item_destroy(ip);
  2582. }
  2583. kmem_zone_free(xfs_inode_zone, ip);
  2584. }
  2585. /*
  2586. * Increment the pin count of the given buffer.
  2587. * This value is protected by ipinlock spinlock in the mount structure.
  2588. */
  2589. void
  2590. xfs_ipin(
  2591. xfs_inode_t *ip)
  2592. {
  2593. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  2594. atomic_inc(&ip->i_pincount);
  2595. }
  2596. /*
  2597. * Decrement the pin count of the given inode, and wake up
  2598. * anyone in xfs_iwait_unpin() if the count goes to 0. The
  2599. * inode must have been previously pinned with a call to xfs_ipin().
  2600. */
  2601. void
  2602. xfs_iunpin(
  2603. xfs_inode_t *ip)
  2604. {
  2605. ASSERT(atomic_read(&ip->i_pincount) > 0);
  2606. if (atomic_dec_and_lock(&ip->i_pincount, &ip->i_flags_lock)) {
  2607. /*
  2608. * If the inode is currently being reclaimed, the link between
  2609. * the bhv_vnode and the xfs_inode will be broken after the
  2610. * XFS_IRECLAIM* flag is set. Hence, if these flags are not
  2611. * set, then we can move forward and mark the linux inode dirty
  2612. * knowing that it is still valid as it won't freed until after
  2613. * the bhv_vnode<->xfs_inode link is broken in xfs_reclaim. The
  2614. * i_flags_lock is used to synchronise the setting of the
  2615. * XFS_IRECLAIM* flags and the breaking of the link, and so we
  2616. * can execute atomically w.r.t to reclaim by holding this lock
  2617. * here.
  2618. *
  2619. * However, we still need to issue the unpin wakeup call as the
  2620. * inode reclaim may be blocked waiting for the inode to become
  2621. * unpinned.
  2622. */
  2623. if (!__xfs_iflags_test(ip, XFS_IRECLAIM|XFS_IRECLAIMABLE)) {
  2624. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  2625. struct inode *inode = NULL;
  2626. BUG_ON(vp == NULL);
  2627. inode = vn_to_inode(vp);
  2628. BUG_ON(inode->i_state & I_CLEAR);
  2629. /* make sync come back and flush this inode */
  2630. if (!(inode->i_state & (I_NEW|I_FREEING)))
  2631. mark_inode_dirty_sync(inode);
  2632. }
  2633. spin_unlock(&ip->i_flags_lock);
  2634. wake_up(&ip->i_ipin_wait);
  2635. }
  2636. }
  2637. /*
  2638. * This is called to wait for the given inode to be unpinned.
  2639. * It will sleep until this happens. The caller must have the
  2640. * inode locked in at least shared mode so that the buffer cannot
  2641. * be subsequently pinned once someone is waiting for it to be
  2642. * unpinned.
  2643. */
  2644. STATIC void
  2645. xfs_iunpin_wait(
  2646. xfs_inode_t *ip)
  2647. {
  2648. xfs_inode_log_item_t *iip;
  2649. xfs_lsn_t lsn;
  2650. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE | MR_ACCESS));
  2651. if (atomic_read(&ip->i_pincount) == 0) {
  2652. return;
  2653. }
  2654. iip = ip->i_itemp;
  2655. if (iip && iip->ili_last_lsn) {
  2656. lsn = iip->ili_last_lsn;
  2657. } else {
  2658. lsn = (xfs_lsn_t)0;
  2659. }
  2660. /*
  2661. * Give the log a push so we don't wait here too long.
  2662. */
  2663. xfs_log_force(ip->i_mount, lsn, XFS_LOG_FORCE);
  2664. wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
  2665. }
  2666. /*
  2667. * xfs_iextents_copy()
  2668. *
  2669. * This is called to copy the REAL extents (as opposed to the delayed
  2670. * allocation extents) from the inode into the given buffer. It
  2671. * returns the number of bytes copied into the buffer.
  2672. *
  2673. * If there are no delayed allocation extents, then we can just
  2674. * memcpy() the extents into the buffer. Otherwise, we need to
  2675. * examine each extent in turn and skip those which are delayed.
  2676. */
  2677. int
  2678. xfs_iextents_copy(
  2679. xfs_inode_t *ip,
  2680. xfs_bmbt_rec_t *dp,
  2681. int whichfork)
  2682. {
  2683. int copied;
  2684. int i;
  2685. xfs_ifork_t *ifp;
  2686. int nrecs;
  2687. xfs_fsblock_t start_block;
  2688. ifp = XFS_IFORK_PTR(ip, whichfork);
  2689. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  2690. ASSERT(ifp->if_bytes > 0);
  2691. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2692. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  2693. ASSERT(nrecs > 0);
  2694. /*
  2695. * There are some delayed allocation extents in the
  2696. * inode, so copy the extents one at a time and skip
  2697. * the delayed ones. There must be at least one
  2698. * non-delayed extent.
  2699. */
  2700. copied = 0;
  2701. for (i = 0; i < nrecs; i++) {
  2702. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  2703. start_block = xfs_bmbt_get_startblock(ep);
  2704. if (ISNULLSTARTBLOCK(start_block)) {
  2705. /*
  2706. * It's a delayed allocation extent, so skip it.
  2707. */
  2708. continue;
  2709. }
  2710. /* Translate to on disk format */
  2711. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  2712. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  2713. dp++;
  2714. copied++;
  2715. }
  2716. ASSERT(copied != 0);
  2717. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  2718. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  2719. }
  2720. /*
  2721. * Each of the following cases stores data into the same region
  2722. * of the on-disk inode, so only one of them can be valid at
  2723. * any given time. While it is possible to have conflicting formats
  2724. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  2725. * in EXTENTS format, this can only happen when the fork has
  2726. * changed formats after being modified but before being flushed.
  2727. * In these cases, the format always takes precedence, because the
  2728. * format indicates the current state of the fork.
  2729. */
  2730. /*ARGSUSED*/
  2731. STATIC int
  2732. xfs_iflush_fork(
  2733. xfs_inode_t *ip,
  2734. xfs_dinode_t *dip,
  2735. xfs_inode_log_item_t *iip,
  2736. int whichfork,
  2737. xfs_buf_t *bp)
  2738. {
  2739. char *cp;
  2740. xfs_ifork_t *ifp;
  2741. xfs_mount_t *mp;
  2742. #ifdef XFS_TRANS_DEBUG
  2743. int first;
  2744. #endif
  2745. static const short brootflag[2] =
  2746. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  2747. static const short dataflag[2] =
  2748. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  2749. static const short extflag[2] =
  2750. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  2751. if (iip == NULL)
  2752. return 0;
  2753. ifp = XFS_IFORK_PTR(ip, whichfork);
  2754. /*
  2755. * This can happen if we gave up in iformat in an error path,
  2756. * for the attribute fork.
  2757. */
  2758. if (ifp == NULL) {
  2759. ASSERT(whichfork == XFS_ATTR_FORK);
  2760. return 0;
  2761. }
  2762. cp = XFS_DFORK_PTR(dip, whichfork);
  2763. mp = ip->i_mount;
  2764. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2765. case XFS_DINODE_FMT_LOCAL:
  2766. if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
  2767. (ifp->if_bytes > 0)) {
  2768. ASSERT(ifp->if_u1.if_data != NULL);
  2769. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2770. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2771. }
  2772. break;
  2773. case XFS_DINODE_FMT_EXTENTS:
  2774. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2775. !(iip->ili_format.ilf_fields & extflag[whichfork]));
  2776. ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) ||
  2777. (ifp->if_bytes == 0));
  2778. ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) ||
  2779. (ifp->if_bytes > 0));
  2780. if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
  2781. (ifp->if_bytes > 0)) {
  2782. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2783. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2784. whichfork);
  2785. }
  2786. break;
  2787. case XFS_DINODE_FMT_BTREE:
  2788. if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
  2789. (ifp->if_broot_bytes > 0)) {
  2790. ASSERT(ifp->if_broot != NULL);
  2791. ASSERT(ifp->if_broot_bytes <=
  2792. (XFS_IFORK_SIZE(ip, whichfork) +
  2793. XFS_BROOT_SIZE_ADJ));
  2794. xfs_bmbt_to_bmdr(ifp->if_broot, ifp->if_broot_bytes,
  2795. (xfs_bmdr_block_t *)cp,
  2796. XFS_DFORK_SIZE(dip, mp, whichfork));
  2797. }
  2798. break;
  2799. case XFS_DINODE_FMT_DEV:
  2800. if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
  2801. ASSERT(whichfork == XFS_DATA_FORK);
  2802. INT_SET(dip->di_u.di_dev, ARCH_CONVERT, ip->i_df.if_u2.if_rdev);
  2803. }
  2804. break;
  2805. case XFS_DINODE_FMT_UUID:
  2806. if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
  2807. ASSERT(whichfork == XFS_DATA_FORK);
  2808. memcpy(&dip->di_u.di_muuid, &ip->i_df.if_u2.if_uuid,
  2809. sizeof(uuid_t));
  2810. }
  2811. break;
  2812. default:
  2813. ASSERT(0);
  2814. break;
  2815. }
  2816. return 0;
  2817. }
  2818. /*
  2819. * xfs_iflush() will write a modified inode's changes out to the
  2820. * inode's on disk home. The caller must have the inode lock held
  2821. * in at least shared mode and the inode flush semaphore must be
  2822. * held as well. The inode lock will still be held upon return from
  2823. * the call and the caller is free to unlock it.
  2824. * The inode flush lock will be unlocked when the inode reaches the disk.
  2825. * The flags indicate how the inode's buffer should be written out.
  2826. */
  2827. int
  2828. xfs_iflush(
  2829. xfs_inode_t *ip,
  2830. uint flags)
  2831. {
  2832. xfs_inode_log_item_t *iip;
  2833. xfs_buf_t *bp;
  2834. xfs_dinode_t *dip;
  2835. xfs_mount_t *mp;
  2836. int error;
  2837. /* REFERENCED */
  2838. xfs_chash_t *ch;
  2839. xfs_inode_t *iq;
  2840. int clcount; /* count of inodes clustered */
  2841. int bufwasdelwri;
  2842. enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
  2843. SPLDECL(s);
  2844. XFS_STATS_INC(xs_iflush_count);
  2845. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  2846. ASSERT(issemalocked(&(ip->i_flock)));
  2847. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2848. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  2849. iip = ip->i_itemp;
  2850. mp = ip->i_mount;
  2851. /*
  2852. * If the inode isn't dirty, then just release the inode
  2853. * flush lock and do nothing.
  2854. */
  2855. if ((ip->i_update_core == 0) &&
  2856. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  2857. ASSERT((iip != NULL) ?
  2858. !(iip->ili_item.li_flags & XFS_LI_IN_AIL) : 1);
  2859. xfs_ifunlock(ip);
  2860. return 0;
  2861. }
  2862. /*
  2863. * We can't flush the inode until it is unpinned, so
  2864. * wait for it. We know noone new can pin it, because
  2865. * we are holding the inode lock shared and you need
  2866. * to hold it exclusively to pin the inode.
  2867. */
  2868. xfs_iunpin_wait(ip);
  2869. /*
  2870. * This may have been unpinned because the filesystem is shutting
  2871. * down forcibly. If that's the case we must not write this inode
  2872. * to disk, because the log record didn't make it to disk!
  2873. */
  2874. if (XFS_FORCED_SHUTDOWN(mp)) {
  2875. ip->i_update_core = 0;
  2876. if (iip)
  2877. iip->ili_format.ilf_fields = 0;
  2878. xfs_ifunlock(ip);
  2879. return XFS_ERROR(EIO);
  2880. }
  2881. /*
  2882. * Get the buffer containing the on-disk inode.
  2883. */
  2884. error = xfs_itobp(mp, NULL, ip, &dip, &bp, 0, 0);
  2885. if (error) {
  2886. xfs_ifunlock(ip);
  2887. return error;
  2888. }
  2889. /*
  2890. * Decide how buffer will be flushed out. This is done before
  2891. * the call to xfs_iflush_int because this field is zeroed by it.
  2892. */
  2893. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  2894. /*
  2895. * Flush out the inode buffer according to the directions
  2896. * of the caller. In the cases where the caller has given
  2897. * us a choice choose the non-delwri case. This is because
  2898. * the inode is in the AIL and we need to get it out soon.
  2899. */
  2900. switch (flags) {
  2901. case XFS_IFLUSH_SYNC:
  2902. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2903. flags = 0;
  2904. break;
  2905. case XFS_IFLUSH_ASYNC:
  2906. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2907. flags = INT_ASYNC;
  2908. break;
  2909. case XFS_IFLUSH_DELWRI:
  2910. flags = INT_DELWRI;
  2911. break;
  2912. default:
  2913. ASSERT(0);
  2914. flags = 0;
  2915. break;
  2916. }
  2917. } else {
  2918. switch (flags) {
  2919. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2920. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2921. case XFS_IFLUSH_DELWRI:
  2922. flags = INT_DELWRI;
  2923. break;
  2924. case XFS_IFLUSH_ASYNC:
  2925. flags = INT_ASYNC;
  2926. break;
  2927. case XFS_IFLUSH_SYNC:
  2928. flags = 0;
  2929. break;
  2930. default:
  2931. ASSERT(0);
  2932. flags = 0;
  2933. break;
  2934. }
  2935. }
  2936. /*
  2937. * First flush out the inode that xfs_iflush was called with.
  2938. */
  2939. error = xfs_iflush_int(ip, bp);
  2940. if (error) {
  2941. goto corrupt_out;
  2942. }
  2943. /*
  2944. * inode clustering:
  2945. * see if other inodes can be gathered into this write
  2946. */
  2947. ip->i_chash->chl_buf = bp;
  2948. ch = XFS_CHASH(mp, ip->i_blkno);
  2949. s = mutex_spinlock(&ch->ch_lock);
  2950. clcount = 0;
  2951. for (iq = ip->i_cnext; iq != ip; iq = iq->i_cnext) {
  2952. /*
  2953. * Do an un-protected check to see if the inode is dirty and
  2954. * is a candidate for flushing. These checks will be repeated
  2955. * later after the appropriate locks are acquired.
  2956. */
  2957. iip = iq->i_itemp;
  2958. if ((iq->i_update_core == 0) &&
  2959. ((iip == NULL) ||
  2960. !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
  2961. xfs_ipincount(iq) == 0) {
  2962. continue;
  2963. }
  2964. /*
  2965. * Try to get locks. If any are unavailable,
  2966. * then this inode cannot be flushed and is skipped.
  2967. */
  2968. /* get inode locks (just i_lock) */
  2969. if (xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) {
  2970. /* get inode flush lock */
  2971. if (xfs_iflock_nowait(iq)) {
  2972. /* check if pinned */
  2973. if (xfs_ipincount(iq) == 0) {
  2974. /* arriving here means that
  2975. * this inode can be flushed.
  2976. * first re-check that it's
  2977. * dirty
  2978. */
  2979. iip = iq->i_itemp;
  2980. if ((iq->i_update_core != 0)||
  2981. ((iip != NULL) &&
  2982. (iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  2983. clcount++;
  2984. error = xfs_iflush_int(iq, bp);
  2985. if (error) {
  2986. xfs_iunlock(iq,
  2987. XFS_ILOCK_SHARED);
  2988. goto cluster_corrupt_out;
  2989. }
  2990. } else {
  2991. xfs_ifunlock(iq);
  2992. }
  2993. } else {
  2994. xfs_ifunlock(iq);
  2995. }
  2996. }
  2997. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2998. }
  2999. }
  3000. mutex_spinunlock(&ch->ch_lock, s);
  3001. if (clcount) {
  3002. XFS_STATS_INC(xs_icluster_flushcnt);
  3003. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  3004. }
  3005. /*
  3006. * If the buffer is pinned then push on the log so we won't
  3007. * get stuck waiting in the write for too long.
  3008. */
  3009. if (XFS_BUF_ISPINNED(bp)){
  3010. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  3011. }
  3012. if (flags & INT_DELWRI) {
  3013. xfs_bdwrite(mp, bp);
  3014. } else if (flags & INT_ASYNC) {
  3015. xfs_bawrite(mp, bp);
  3016. } else {
  3017. error = xfs_bwrite(mp, bp);
  3018. }
  3019. return error;
  3020. corrupt_out:
  3021. xfs_buf_relse(bp);
  3022. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  3023. xfs_iflush_abort(ip);
  3024. /*
  3025. * Unlocks the flush lock
  3026. */
  3027. return XFS_ERROR(EFSCORRUPTED);
  3028. cluster_corrupt_out:
  3029. /* Corruption detected in the clustering loop. Invalidate the
  3030. * inode buffer and shut down the filesystem.
  3031. */
  3032. mutex_spinunlock(&ch->ch_lock, s);
  3033. /*
  3034. * Clean up the buffer. If it was B_DELWRI, just release it --
  3035. * brelse can handle it with no problems. If not, shut down the
  3036. * filesystem before releasing the buffer.
  3037. */
  3038. if ((bufwasdelwri= XFS_BUF_ISDELAYWRITE(bp))) {
  3039. xfs_buf_relse(bp);
  3040. }
  3041. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  3042. if(!bufwasdelwri) {
  3043. /*
  3044. * Just like incore_relse: if we have b_iodone functions,
  3045. * mark the buffer as an error and call them. Otherwise
  3046. * mark it as stale and brelse.
  3047. */
  3048. if (XFS_BUF_IODONE_FUNC(bp)) {
  3049. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  3050. XFS_BUF_UNDONE(bp);
  3051. XFS_BUF_STALE(bp);
  3052. XFS_BUF_SHUT(bp);
  3053. XFS_BUF_ERROR(bp,EIO);
  3054. xfs_biodone(bp);
  3055. } else {
  3056. XFS_BUF_STALE(bp);
  3057. xfs_buf_relse(bp);
  3058. }
  3059. }
  3060. xfs_iflush_abort(iq);
  3061. /*
  3062. * Unlocks the flush lock
  3063. */
  3064. return XFS_ERROR(EFSCORRUPTED);
  3065. }
  3066. STATIC int
  3067. xfs_iflush_int(
  3068. xfs_inode_t *ip,
  3069. xfs_buf_t *bp)
  3070. {
  3071. xfs_inode_log_item_t *iip;
  3072. xfs_dinode_t *dip;
  3073. xfs_mount_t *mp;
  3074. #ifdef XFS_TRANS_DEBUG
  3075. int first;
  3076. #endif
  3077. SPLDECL(s);
  3078. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  3079. ASSERT(issemalocked(&(ip->i_flock)));
  3080. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  3081. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  3082. iip = ip->i_itemp;
  3083. mp = ip->i_mount;
  3084. /*
  3085. * If the inode isn't dirty, then just release the inode
  3086. * flush lock and do nothing.
  3087. */
  3088. if ((ip->i_update_core == 0) &&
  3089. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  3090. xfs_ifunlock(ip);
  3091. return 0;
  3092. }
  3093. /* set *dip = inode's place in the buffer */
  3094. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset);
  3095. /*
  3096. * Clear i_update_core before copying out the data.
  3097. * This is for coordination with our timestamp updates
  3098. * that don't hold the inode lock. They will always
  3099. * update the timestamps BEFORE setting i_update_core,
  3100. * so if we clear i_update_core after they set it we
  3101. * are guaranteed to see their updates to the timestamps.
  3102. * I believe that this depends on strongly ordered memory
  3103. * semantics, but we have that. We use the SYNCHRONIZE
  3104. * macro to make sure that the compiler does not reorder
  3105. * the i_update_core access below the data copy below.
  3106. */
  3107. ip->i_update_core = 0;
  3108. SYNCHRONIZE();
  3109. /*
  3110. * Make sure to get the latest atime from the Linux inode.
  3111. */
  3112. xfs_synchronize_atime(ip);
  3113. if (XFS_TEST_ERROR(INT_GET(dip->di_core.di_magic,ARCH_CONVERT) != XFS_DINODE_MAGIC,
  3114. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  3115. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3116. "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  3117. ip->i_ino, (int) INT_GET(dip->di_core.di_magic, ARCH_CONVERT), dip);
  3118. goto corrupt_out;
  3119. }
  3120. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  3121. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  3122. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3123. "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  3124. ip->i_ino, ip, ip->i_d.di_magic);
  3125. goto corrupt_out;
  3126. }
  3127. if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  3128. if (XFS_TEST_ERROR(
  3129. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3130. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  3131. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  3132. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3133. "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
  3134. ip->i_ino, ip);
  3135. goto corrupt_out;
  3136. }
  3137. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  3138. if (XFS_TEST_ERROR(
  3139. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3140. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3141. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  3142. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  3143. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3144. "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
  3145. ip->i_ino, ip);
  3146. goto corrupt_out;
  3147. }
  3148. }
  3149. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3150. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3151. XFS_RANDOM_IFLUSH_5)) {
  3152. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3153. "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
  3154. ip->i_ino,
  3155. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3156. ip->i_d.di_nblocks,
  3157. ip);
  3158. goto corrupt_out;
  3159. }
  3160. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3161. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3162. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3163. "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3164. ip->i_ino, ip->i_d.di_forkoff, ip);
  3165. goto corrupt_out;
  3166. }
  3167. /*
  3168. * bump the flush iteration count, used to detect flushes which
  3169. * postdate a log record during recovery.
  3170. */
  3171. ip->i_d.di_flushiter++;
  3172. /*
  3173. * Copy the dirty parts of the inode into the on-disk
  3174. * inode. We always copy out the core of the inode,
  3175. * because if the inode is dirty at all the core must
  3176. * be.
  3177. */
  3178. xfs_xlate_dinode_core((xfs_caddr_t)&(dip->di_core), &(ip->i_d), -1);
  3179. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3180. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3181. ip->i_d.di_flushiter = 0;
  3182. /*
  3183. * If this is really an old format inode and the superblock version
  3184. * has not been updated to support only new format inodes, then
  3185. * convert back to the old inode format. If the superblock version
  3186. * has been updated, then make the conversion permanent.
  3187. */
  3188. ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
  3189. XFS_SB_VERSION_HASNLINK(&mp->m_sb));
  3190. if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  3191. if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
  3192. /*
  3193. * Convert it back.
  3194. */
  3195. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  3196. INT_SET(dip->di_core.di_onlink, ARCH_CONVERT, ip->i_d.di_nlink);
  3197. } else {
  3198. /*
  3199. * The superblock version has already been bumped,
  3200. * so just make the conversion to the new inode
  3201. * format permanent.
  3202. */
  3203. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  3204. INT_SET(dip->di_core.di_version, ARCH_CONVERT, XFS_DINODE_VERSION_2);
  3205. ip->i_d.di_onlink = 0;
  3206. dip->di_core.di_onlink = 0;
  3207. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  3208. memset(&(dip->di_core.di_pad[0]), 0,
  3209. sizeof(dip->di_core.di_pad));
  3210. ASSERT(ip->i_d.di_projid == 0);
  3211. }
  3212. }
  3213. if (xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp) == EFSCORRUPTED) {
  3214. goto corrupt_out;
  3215. }
  3216. if (XFS_IFORK_Q(ip)) {
  3217. /*
  3218. * The only error from xfs_iflush_fork is on the data fork.
  3219. */
  3220. (void) xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  3221. }
  3222. xfs_inobp_check(mp, bp);
  3223. /*
  3224. * We've recorded everything logged in the inode, so we'd
  3225. * like to clear the ilf_fields bits so we don't log and
  3226. * flush things unnecessarily. However, we can't stop
  3227. * logging all this information until the data we've copied
  3228. * into the disk buffer is written to disk. If we did we might
  3229. * overwrite the copy of the inode in the log with all the
  3230. * data after re-logging only part of it, and in the face of
  3231. * a crash we wouldn't have all the data we need to recover.
  3232. *
  3233. * What we do is move the bits to the ili_last_fields field.
  3234. * When logging the inode, these bits are moved back to the
  3235. * ilf_fields field. In the xfs_iflush_done() routine we
  3236. * clear ili_last_fields, since we know that the information
  3237. * those bits represent is permanently on disk. As long as
  3238. * the flush completes before the inode is logged again, then
  3239. * both ilf_fields and ili_last_fields will be cleared.
  3240. *
  3241. * We can play with the ilf_fields bits here, because the inode
  3242. * lock must be held exclusively in order to set bits there
  3243. * and the flush lock protects the ili_last_fields bits.
  3244. * Set ili_logged so the flush done
  3245. * routine can tell whether or not to look in the AIL.
  3246. * Also, store the current LSN of the inode so that we can tell
  3247. * whether the item has moved in the AIL from xfs_iflush_done().
  3248. * In order to read the lsn we need the AIL lock, because
  3249. * it is a 64 bit value that cannot be read atomically.
  3250. */
  3251. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  3252. iip->ili_last_fields = iip->ili_format.ilf_fields;
  3253. iip->ili_format.ilf_fields = 0;
  3254. iip->ili_logged = 1;
  3255. ASSERT(sizeof(xfs_lsn_t) == 8); /* don't lock if it shrinks */
  3256. AIL_LOCK(mp,s);
  3257. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  3258. AIL_UNLOCK(mp, s);
  3259. /*
  3260. * Attach the function xfs_iflush_done to the inode's
  3261. * buffer. This will remove the inode from the AIL
  3262. * and unlock the inode's flush lock when the inode is
  3263. * completely written to disk.
  3264. */
  3265. xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
  3266. xfs_iflush_done, (xfs_log_item_t *)iip);
  3267. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  3268. ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
  3269. } else {
  3270. /*
  3271. * We're flushing an inode which is not in the AIL and has
  3272. * not been logged but has i_update_core set. For this
  3273. * case we can use a B_DELWRI flush and immediately drop
  3274. * the inode flush lock because we can avoid the whole
  3275. * AIL state thing. It's OK to drop the flush lock now,
  3276. * because we've already locked the buffer and to do anything
  3277. * you really need both.
  3278. */
  3279. if (iip != NULL) {
  3280. ASSERT(iip->ili_logged == 0);
  3281. ASSERT(iip->ili_last_fields == 0);
  3282. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  3283. }
  3284. xfs_ifunlock(ip);
  3285. }
  3286. return 0;
  3287. corrupt_out:
  3288. return XFS_ERROR(EFSCORRUPTED);
  3289. }
  3290. /*
  3291. * Flush all inactive inodes in mp.
  3292. */
  3293. void
  3294. xfs_iflush_all(
  3295. xfs_mount_t *mp)
  3296. {
  3297. xfs_inode_t *ip;
  3298. bhv_vnode_t *vp;
  3299. again:
  3300. XFS_MOUNT_ILOCK(mp);
  3301. ip = mp->m_inodes;
  3302. if (ip == NULL)
  3303. goto out;
  3304. do {
  3305. /* Make sure we skip markers inserted by sync */
  3306. if (ip->i_mount == NULL) {
  3307. ip = ip->i_mnext;
  3308. continue;
  3309. }
  3310. vp = XFS_ITOV_NULL(ip);
  3311. if (!vp) {
  3312. XFS_MOUNT_IUNLOCK(mp);
  3313. xfs_finish_reclaim(ip, 0, XFS_IFLUSH_ASYNC);
  3314. goto again;
  3315. }
  3316. ASSERT(vn_count(vp) == 0);
  3317. ip = ip->i_mnext;
  3318. } while (ip != mp->m_inodes);
  3319. out:
  3320. XFS_MOUNT_IUNLOCK(mp);
  3321. }
  3322. /*
  3323. * xfs_iaccess: check accessibility of inode for mode.
  3324. */
  3325. int
  3326. xfs_iaccess(
  3327. xfs_inode_t *ip,
  3328. mode_t mode,
  3329. cred_t *cr)
  3330. {
  3331. int error;
  3332. mode_t orgmode = mode;
  3333. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  3334. if (mode & S_IWUSR) {
  3335. umode_t imode = inode->i_mode;
  3336. if (IS_RDONLY(inode) &&
  3337. (S_ISREG(imode) || S_ISDIR(imode) || S_ISLNK(imode)))
  3338. return XFS_ERROR(EROFS);
  3339. if (IS_IMMUTABLE(inode))
  3340. return XFS_ERROR(EACCES);
  3341. }
  3342. /*
  3343. * If there's an Access Control List it's used instead of
  3344. * the mode bits.
  3345. */
  3346. if ((error = _ACL_XFS_IACCESS(ip, mode, cr)) != -1)
  3347. return error ? XFS_ERROR(error) : 0;
  3348. if (current_fsuid(cr) != ip->i_d.di_uid) {
  3349. mode >>= 3;
  3350. if (!in_group_p((gid_t)ip->i_d.di_gid))
  3351. mode >>= 3;
  3352. }
  3353. /*
  3354. * If the DACs are ok we don't need any capability check.
  3355. */
  3356. if ((ip->i_d.di_mode & mode) == mode)
  3357. return 0;
  3358. /*
  3359. * Read/write DACs are always overridable.
  3360. * Executable DACs are overridable if at least one exec bit is set.
  3361. */
  3362. if (!(orgmode & S_IXUSR) ||
  3363. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  3364. if (capable_cred(cr, CAP_DAC_OVERRIDE))
  3365. return 0;
  3366. if ((orgmode == S_IRUSR) ||
  3367. (S_ISDIR(inode->i_mode) && (!(orgmode & S_IWUSR)))) {
  3368. if (capable_cred(cr, CAP_DAC_READ_SEARCH))
  3369. return 0;
  3370. #ifdef NOISE
  3371. cmn_err(CE_NOTE, "Ick: mode=%o, orgmode=%o", mode, orgmode);
  3372. #endif /* NOISE */
  3373. return XFS_ERROR(EACCES);
  3374. }
  3375. return XFS_ERROR(EACCES);
  3376. }
  3377. /*
  3378. * xfs_iroundup: round up argument to next power of two
  3379. */
  3380. uint
  3381. xfs_iroundup(
  3382. uint v)
  3383. {
  3384. int i;
  3385. uint m;
  3386. if ((v & (v - 1)) == 0)
  3387. return v;
  3388. ASSERT((v & 0x80000000) == 0);
  3389. if ((v & (v + 1)) == 0)
  3390. return v + 1;
  3391. for (i = 0, m = 1; i < 31; i++, m <<= 1) {
  3392. if (v & m)
  3393. continue;
  3394. v |= m;
  3395. if ((v & (v + 1)) == 0)
  3396. return v + 1;
  3397. }
  3398. ASSERT(0);
  3399. return( 0 );
  3400. }
  3401. #ifdef XFS_ILOCK_TRACE
  3402. ktrace_t *xfs_ilock_trace_buf;
  3403. void
  3404. xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
  3405. {
  3406. ktrace_enter(ip->i_lock_trace,
  3407. (void *)ip,
  3408. (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
  3409. (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
  3410. (void *)ra, /* caller of ilock */
  3411. (void *)(unsigned long)current_cpu(),
  3412. (void *)(unsigned long)current_pid(),
  3413. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3414. }
  3415. #endif
  3416. /*
  3417. * Return a pointer to the extent record at file index idx.
  3418. */
  3419. xfs_bmbt_rec_host_t *
  3420. xfs_iext_get_ext(
  3421. xfs_ifork_t *ifp, /* inode fork pointer */
  3422. xfs_extnum_t idx) /* index of target extent */
  3423. {
  3424. ASSERT(idx >= 0);
  3425. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  3426. return ifp->if_u1.if_ext_irec->er_extbuf;
  3427. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  3428. xfs_ext_irec_t *erp; /* irec pointer */
  3429. int erp_idx = 0; /* irec index */
  3430. xfs_extnum_t page_idx = idx; /* ext index in target list */
  3431. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3432. return &erp->er_extbuf[page_idx];
  3433. } else if (ifp->if_bytes) {
  3434. return &ifp->if_u1.if_extents[idx];
  3435. } else {
  3436. return NULL;
  3437. }
  3438. }
  3439. /*
  3440. * Insert new item(s) into the extent records for incore inode
  3441. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  3442. */
  3443. void
  3444. xfs_iext_insert(
  3445. xfs_ifork_t *ifp, /* inode fork pointer */
  3446. xfs_extnum_t idx, /* starting index of new items */
  3447. xfs_extnum_t count, /* number of inserted items */
  3448. xfs_bmbt_irec_t *new) /* items to insert */
  3449. {
  3450. xfs_extnum_t i; /* extent record index */
  3451. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3452. xfs_iext_add(ifp, idx, count);
  3453. for (i = idx; i < idx + count; i++, new++)
  3454. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  3455. }
  3456. /*
  3457. * This is called when the amount of space required for incore file
  3458. * extents needs to be increased. The ext_diff parameter stores the
  3459. * number of new extents being added and the idx parameter contains
  3460. * the extent index where the new extents will be added. If the new
  3461. * extents are being appended, then we just need to (re)allocate and
  3462. * initialize the space. Otherwise, if the new extents are being
  3463. * inserted into the middle of the existing entries, a bit more work
  3464. * is required to make room for the new extents to be inserted. The
  3465. * caller is responsible for filling in the new extent entries upon
  3466. * return.
  3467. */
  3468. void
  3469. xfs_iext_add(
  3470. xfs_ifork_t *ifp, /* inode fork pointer */
  3471. xfs_extnum_t idx, /* index to begin adding exts */
  3472. int ext_diff) /* number of extents to add */
  3473. {
  3474. int byte_diff; /* new bytes being added */
  3475. int new_size; /* size of extents after adding */
  3476. xfs_extnum_t nextents; /* number of extents in file */
  3477. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3478. ASSERT((idx >= 0) && (idx <= nextents));
  3479. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  3480. new_size = ifp->if_bytes + byte_diff;
  3481. /*
  3482. * If the new number of extents (nextents + ext_diff)
  3483. * fits inside the inode, then continue to use the inline
  3484. * extent buffer.
  3485. */
  3486. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  3487. if (idx < nextents) {
  3488. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  3489. &ifp->if_u2.if_inline_ext[idx],
  3490. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  3491. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  3492. }
  3493. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3494. ifp->if_real_bytes = 0;
  3495. ifp->if_lastex = nextents + ext_diff;
  3496. }
  3497. /*
  3498. * Otherwise use a linear (direct) extent list.
  3499. * If the extents are currently inside the inode,
  3500. * xfs_iext_realloc_direct will switch us from
  3501. * inline to direct extent allocation mode.
  3502. */
  3503. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  3504. xfs_iext_realloc_direct(ifp, new_size);
  3505. if (idx < nextents) {
  3506. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  3507. &ifp->if_u1.if_extents[idx],
  3508. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  3509. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  3510. }
  3511. }
  3512. /* Indirection array */
  3513. else {
  3514. xfs_ext_irec_t *erp;
  3515. int erp_idx = 0;
  3516. int page_idx = idx;
  3517. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  3518. if (ifp->if_flags & XFS_IFEXTIREC) {
  3519. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  3520. } else {
  3521. xfs_iext_irec_init(ifp);
  3522. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3523. erp = ifp->if_u1.if_ext_irec;
  3524. }
  3525. /* Extents fit in target extent page */
  3526. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  3527. if (page_idx < erp->er_extcount) {
  3528. memmove(&erp->er_extbuf[page_idx + ext_diff],
  3529. &erp->er_extbuf[page_idx],
  3530. (erp->er_extcount - page_idx) *
  3531. sizeof(xfs_bmbt_rec_t));
  3532. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  3533. }
  3534. erp->er_extcount += ext_diff;
  3535. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3536. }
  3537. /* Insert a new extent page */
  3538. else if (erp) {
  3539. xfs_iext_add_indirect_multi(ifp,
  3540. erp_idx, page_idx, ext_diff);
  3541. }
  3542. /*
  3543. * If extent(s) are being appended to the last page in
  3544. * the indirection array and the new extent(s) don't fit
  3545. * in the page, then erp is NULL and erp_idx is set to
  3546. * the next index needed in the indirection array.
  3547. */
  3548. else {
  3549. int count = ext_diff;
  3550. while (count) {
  3551. erp = xfs_iext_irec_new(ifp, erp_idx);
  3552. erp->er_extcount = count;
  3553. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  3554. if (count) {
  3555. erp_idx++;
  3556. }
  3557. }
  3558. }
  3559. }
  3560. ifp->if_bytes = new_size;
  3561. }
  3562. /*
  3563. * This is called when incore extents are being added to the indirection
  3564. * array and the new extents do not fit in the target extent list. The
  3565. * erp_idx parameter contains the irec index for the target extent list
  3566. * in the indirection array, and the idx parameter contains the extent
  3567. * index within the list. The number of extents being added is stored
  3568. * in the count parameter.
  3569. *
  3570. * |-------| |-------|
  3571. * | | | | idx - number of extents before idx
  3572. * | idx | | count |
  3573. * | | | | count - number of extents being inserted at idx
  3574. * |-------| |-------|
  3575. * | count | | nex2 | nex2 - number of extents after idx + count
  3576. * |-------| |-------|
  3577. */
  3578. void
  3579. xfs_iext_add_indirect_multi(
  3580. xfs_ifork_t *ifp, /* inode fork pointer */
  3581. int erp_idx, /* target extent irec index */
  3582. xfs_extnum_t idx, /* index within target list */
  3583. int count) /* new extents being added */
  3584. {
  3585. int byte_diff; /* new bytes being added */
  3586. xfs_ext_irec_t *erp; /* pointer to irec entry */
  3587. xfs_extnum_t ext_diff; /* number of extents to add */
  3588. xfs_extnum_t ext_cnt; /* new extents still needed */
  3589. xfs_extnum_t nex2; /* extents after idx + count */
  3590. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  3591. int nlists; /* number of irec's (lists) */
  3592. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3593. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3594. nex2 = erp->er_extcount - idx;
  3595. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3596. /*
  3597. * Save second part of target extent list
  3598. * (all extents past */
  3599. if (nex2) {
  3600. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  3601. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_SLEEP);
  3602. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  3603. erp->er_extcount -= nex2;
  3604. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  3605. memset(&erp->er_extbuf[idx], 0, byte_diff);
  3606. }
  3607. /*
  3608. * Add the new extents to the end of the target
  3609. * list, then allocate new irec record(s) and
  3610. * extent buffer(s) as needed to store the rest
  3611. * of the new extents.
  3612. */
  3613. ext_cnt = count;
  3614. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  3615. if (ext_diff) {
  3616. erp->er_extcount += ext_diff;
  3617. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3618. ext_cnt -= ext_diff;
  3619. }
  3620. while (ext_cnt) {
  3621. erp_idx++;
  3622. erp = xfs_iext_irec_new(ifp, erp_idx);
  3623. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  3624. erp->er_extcount = ext_diff;
  3625. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3626. ext_cnt -= ext_diff;
  3627. }
  3628. /* Add nex2 extents back to indirection array */
  3629. if (nex2) {
  3630. xfs_extnum_t ext_avail;
  3631. int i;
  3632. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  3633. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  3634. i = 0;
  3635. /*
  3636. * If nex2 extents fit in the current page, append
  3637. * nex2_ep after the new extents.
  3638. */
  3639. if (nex2 <= ext_avail) {
  3640. i = erp->er_extcount;
  3641. }
  3642. /*
  3643. * Otherwise, check if space is available in the
  3644. * next page.
  3645. */
  3646. else if ((erp_idx < nlists - 1) &&
  3647. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  3648. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  3649. erp_idx++;
  3650. erp++;
  3651. /* Create a hole for nex2 extents */
  3652. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  3653. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  3654. }
  3655. /*
  3656. * Final choice, create a new extent page for
  3657. * nex2 extents.
  3658. */
  3659. else {
  3660. erp_idx++;
  3661. erp = xfs_iext_irec_new(ifp, erp_idx);
  3662. }
  3663. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  3664. kmem_free(nex2_ep, byte_diff);
  3665. erp->er_extcount += nex2;
  3666. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  3667. }
  3668. }
  3669. /*
  3670. * This is called when the amount of space required for incore file
  3671. * extents needs to be decreased. The ext_diff parameter stores the
  3672. * number of extents to be removed and the idx parameter contains
  3673. * the extent index where the extents will be removed from.
  3674. *
  3675. * If the amount of space needed has decreased below the linear
  3676. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  3677. * extent array. Otherwise, use kmem_realloc() to adjust the
  3678. * size to what is needed.
  3679. */
  3680. void
  3681. xfs_iext_remove(
  3682. xfs_ifork_t *ifp, /* inode fork pointer */
  3683. xfs_extnum_t idx, /* index to begin removing exts */
  3684. int ext_diff) /* number of extents to remove */
  3685. {
  3686. xfs_extnum_t nextents; /* number of extents in file */
  3687. int new_size; /* size of extents after removal */
  3688. ASSERT(ext_diff > 0);
  3689. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3690. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  3691. if (new_size == 0) {
  3692. xfs_iext_destroy(ifp);
  3693. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  3694. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  3695. } else if (ifp->if_real_bytes) {
  3696. xfs_iext_remove_direct(ifp, idx, ext_diff);
  3697. } else {
  3698. xfs_iext_remove_inline(ifp, idx, ext_diff);
  3699. }
  3700. ifp->if_bytes = new_size;
  3701. }
  3702. /*
  3703. * This removes ext_diff extents from the inline buffer, beginning
  3704. * at extent index idx.
  3705. */
  3706. void
  3707. xfs_iext_remove_inline(
  3708. xfs_ifork_t *ifp, /* inode fork pointer */
  3709. xfs_extnum_t idx, /* index to begin removing exts */
  3710. int ext_diff) /* number of extents to remove */
  3711. {
  3712. int nextents; /* number of extents in file */
  3713. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3714. ASSERT(idx < XFS_INLINE_EXTS);
  3715. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3716. ASSERT(((nextents - ext_diff) > 0) &&
  3717. (nextents - ext_diff) < XFS_INLINE_EXTS);
  3718. if (idx + ext_diff < nextents) {
  3719. memmove(&ifp->if_u2.if_inline_ext[idx],
  3720. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  3721. (nextents - (idx + ext_diff)) *
  3722. sizeof(xfs_bmbt_rec_t));
  3723. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  3724. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3725. } else {
  3726. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  3727. ext_diff * sizeof(xfs_bmbt_rec_t));
  3728. }
  3729. }
  3730. /*
  3731. * This removes ext_diff extents from a linear (direct) extent list,
  3732. * beginning at extent index idx. If the extents are being removed
  3733. * from the end of the list (ie. truncate) then we just need to re-
  3734. * allocate the list to remove the extra space. Otherwise, if the
  3735. * extents are being removed from the middle of the existing extent
  3736. * entries, then we first need to move the extent records beginning
  3737. * at idx + ext_diff up in the list to overwrite the records being
  3738. * removed, then remove the extra space via kmem_realloc.
  3739. */
  3740. void
  3741. xfs_iext_remove_direct(
  3742. xfs_ifork_t *ifp, /* inode fork pointer */
  3743. xfs_extnum_t idx, /* index to begin removing exts */
  3744. int ext_diff) /* number of extents to remove */
  3745. {
  3746. xfs_extnum_t nextents; /* number of extents in file */
  3747. int new_size; /* size of extents after removal */
  3748. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3749. new_size = ifp->if_bytes -
  3750. (ext_diff * sizeof(xfs_bmbt_rec_t));
  3751. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3752. if (new_size == 0) {
  3753. xfs_iext_destroy(ifp);
  3754. return;
  3755. }
  3756. /* Move extents up in the list (if needed) */
  3757. if (idx + ext_diff < nextents) {
  3758. memmove(&ifp->if_u1.if_extents[idx],
  3759. &ifp->if_u1.if_extents[idx + ext_diff],
  3760. (nextents - (idx + ext_diff)) *
  3761. sizeof(xfs_bmbt_rec_t));
  3762. }
  3763. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  3764. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3765. /*
  3766. * Reallocate the direct extent list. If the extents
  3767. * will fit inside the inode then xfs_iext_realloc_direct
  3768. * will switch from direct to inline extent allocation
  3769. * mode for us.
  3770. */
  3771. xfs_iext_realloc_direct(ifp, new_size);
  3772. ifp->if_bytes = new_size;
  3773. }
  3774. /*
  3775. * This is called when incore extents are being removed from the
  3776. * indirection array and the extents being removed span multiple extent
  3777. * buffers. The idx parameter contains the file extent index where we
  3778. * want to begin removing extents, and the count parameter contains
  3779. * how many extents need to be removed.
  3780. *
  3781. * |-------| |-------|
  3782. * | nex1 | | | nex1 - number of extents before idx
  3783. * |-------| | count |
  3784. * | | | | count - number of extents being removed at idx
  3785. * | count | |-------|
  3786. * | | | nex2 | nex2 - number of extents after idx + count
  3787. * |-------| |-------|
  3788. */
  3789. void
  3790. xfs_iext_remove_indirect(
  3791. xfs_ifork_t *ifp, /* inode fork pointer */
  3792. xfs_extnum_t idx, /* index to begin removing extents */
  3793. int count) /* number of extents to remove */
  3794. {
  3795. xfs_ext_irec_t *erp; /* indirection array pointer */
  3796. int erp_idx = 0; /* indirection array index */
  3797. xfs_extnum_t ext_cnt; /* extents left to remove */
  3798. xfs_extnum_t ext_diff; /* extents to remove in current list */
  3799. xfs_extnum_t nex1; /* number of extents before idx */
  3800. xfs_extnum_t nex2; /* extents after idx + count */
  3801. int nlists; /* entries in indirection array */
  3802. int page_idx = idx; /* index in target extent list */
  3803. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3804. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3805. ASSERT(erp != NULL);
  3806. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3807. nex1 = page_idx;
  3808. ext_cnt = count;
  3809. while (ext_cnt) {
  3810. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  3811. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  3812. /*
  3813. * Check for deletion of entire list;
  3814. * xfs_iext_irec_remove() updates extent offsets.
  3815. */
  3816. if (ext_diff == erp->er_extcount) {
  3817. xfs_iext_irec_remove(ifp, erp_idx);
  3818. ext_cnt -= ext_diff;
  3819. nex1 = 0;
  3820. if (ext_cnt) {
  3821. ASSERT(erp_idx < ifp->if_real_bytes /
  3822. XFS_IEXT_BUFSZ);
  3823. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3824. nex1 = 0;
  3825. continue;
  3826. } else {
  3827. break;
  3828. }
  3829. }
  3830. /* Move extents up (if needed) */
  3831. if (nex2) {
  3832. memmove(&erp->er_extbuf[nex1],
  3833. &erp->er_extbuf[nex1 + ext_diff],
  3834. nex2 * sizeof(xfs_bmbt_rec_t));
  3835. }
  3836. /* Zero out rest of page */
  3837. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  3838. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  3839. /* Update remaining counters */
  3840. erp->er_extcount -= ext_diff;
  3841. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  3842. ext_cnt -= ext_diff;
  3843. nex1 = 0;
  3844. erp_idx++;
  3845. erp++;
  3846. }
  3847. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  3848. xfs_iext_irec_compact(ifp);
  3849. }
  3850. /*
  3851. * Create, destroy, or resize a linear (direct) block of extents.
  3852. */
  3853. void
  3854. xfs_iext_realloc_direct(
  3855. xfs_ifork_t *ifp, /* inode fork pointer */
  3856. int new_size) /* new size of extents */
  3857. {
  3858. int rnew_size; /* real new size of extents */
  3859. rnew_size = new_size;
  3860. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  3861. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  3862. (new_size != ifp->if_real_bytes)));
  3863. /* Free extent records */
  3864. if (new_size == 0) {
  3865. xfs_iext_destroy(ifp);
  3866. }
  3867. /* Resize direct extent list and zero any new bytes */
  3868. else if (ifp->if_real_bytes) {
  3869. /* Check if extents will fit inside the inode */
  3870. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  3871. xfs_iext_direct_to_inline(ifp, new_size /
  3872. (uint)sizeof(xfs_bmbt_rec_t));
  3873. ifp->if_bytes = new_size;
  3874. return;
  3875. }
  3876. if (!is_power_of_2(new_size)){
  3877. rnew_size = xfs_iroundup(new_size);
  3878. }
  3879. if (rnew_size != ifp->if_real_bytes) {
  3880. ifp->if_u1.if_extents =
  3881. kmem_realloc(ifp->if_u1.if_extents,
  3882. rnew_size,
  3883. ifp->if_real_bytes,
  3884. KM_SLEEP);
  3885. }
  3886. if (rnew_size > ifp->if_real_bytes) {
  3887. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  3888. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  3889. rnew_size - ifp->if_real_bytes);
  3890. }
  3891. }
  3892. /*
  3893. * Switch from the inline extent buffer to a direct
  3894. * extent list. Be sure to include the inline extent
  3895. * bytes in new_size.
  3896. */
  3897. else {
  3898. new_size += ifp->if_bytes;
  3899. if (!is_power_of_2(new_size)) {
  3900. rnew_size = xfs_iroundup(new_size);
  3901. }
  3902. xfs_iext_inline_to_direct(ifp, rnew_size);
  3903. }
  3904. ifp->if_real_bytes = rnew_size;
  3905. ifp->if_bytes = new_size;
  3906. }
  3907. /*
  3908. * Switch from linear (direct) extent records to inline buffer.
  3909. */
  3910. void
  3911. xfs_iext_direct_to_inline(
  3912. xfs_ifork_t *ifp, /* inode fork pointer */
  3913. xfs_extnum_t nextents) /* number of extents in file */
  3914. {
  3915. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3916. ASSERT(nextents <= XFS_INLINE_EXTS);
  3917. /*
  3918. * The inline buffer was zeroed when we switched
  3919. * from inline to direct extent allocation mode,
  3920. * so we don't need to clear it here.
  3921. */
  3922. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  3923. nextents * sizeof(xfs_bmbt_rec_t));
  3924. kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
  3925. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3926. ifp->if_real_bytes = 0;
  3927. }
  3928. /*
  3929. * Switch from inline buffer to linear (direct) extent records.
  3930. * new_size should already be rounded up to the next power of 2
  3931. * by the caller (when appropriate), so use new_size as it is.
  3932. * However, since new_size may be rounded up, we can't update
  3933. * if_bytes here. It is the caller's responsibility to update
  3934. * if_bytes upon return.
  3935. */
  3936. void
  3937. xfs_iext_inline_to_direct(
  3938. xfs_ifork_t *ifp, /* inode fork pointer */
  3939. int new_size) /* number of extents in file */
  3940. {
  3941. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_SLEEP);
  3942. memset(ifp->if_u1.if_extents, 0, new_size);
  3943. if (ifp->if_bytes) {
  3944. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  3945. ifp->if_bytes);
  3946. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3947. sizeof(xfs_bmbt_rec_t));
  3948. }
  3949. ifp->if_real_bytes = new_size;
  3950. }
  3951. /*
  3952. * Resize an extent indirection array to new_size bytes.
  3953. */
  3954. void
  3955. xfs_iext_realloc_indirect(
  3956. xfs_ifork_t *ifp, /* inode fork pointer */
  3957. int new_size) /* new indirection array size */
  3958. {
  3959. int nlists; /* number of irec's (ex lists) */
  3960. int size; /* current indirection array size */
  3961. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3962. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3963. size = nlists * sizeof(xfs_ext_irec_t);
  3964. ASSERT(ifp->if_real_bytes);
  3965. ASSERT((new_size >= 0) && (new_size != size));
  3966. if (new_size == 0) {
  3967. xfs_iext_destroy(ifp);
  3968. } else {
  3969. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  3970. kmem_realloc(ifp->if_u1.if_ext_irec,
  3971. new_size, size, KM_SLEEP);
  3972. }
  3973. }
  3974. /*
  3975. * Switch from indirection array to linear (direct) extent allocations.
  3976. */
  3977. void
  3978. xfs_iext_indirect_to_direct(
  3979. xfs_ifork_t *ifp) /* inode fork pointer */
  3980. {
  3981. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3982. xfs_extnum_t nextents; /* number of extents in file */
  3983. int size; /* size of file extents */
  3984. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3985. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3986. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3987. size = nextents * sizeof(xfs_bmbt_rec_t);
  3988. xfs_iext_irec_compact_full(ifp);
  3989. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  3990. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  3991. kmem_free(ifp->if_u1.if_ext_irec, sizeof(xfs_ext_irec_t));
  3992. ifp->if_flags &= ~XFS_IFEXTIREC;
  3993. ifp->if_u1.if_extents = ep;
  3994. ifp->if_bytes = size;
  3995. if (nextents < XFS_LINEAR_EXTS) {
  3996. xfs_iext_realloc_direct(ifp, size);
  3997. }
  3998. }
  3999. /*
  4000. * Free incore file extents.
  4001. */
  4002. void
  4003. xfs_iext_destroy(
  4004. xfs_ifork_t *ifp) /* inode fork pointer */
  4005. {
  4006. if (ifp->if_flags & XFS_IFEXTIREC) {
  4007. int erp_idx;
  4008. int nlists;
  4009. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4010. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  4011. xfs_iext_irec_remove(ifp, erp_idx);
  4012. }
  4013. ifp->if_flags &= ~XFS_IFEXTIREC;
  4014. } else if (ifp->if_real_bytes) {
  4015. kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
  4016. } else if (ifp->if_bytes) {
  4017. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  4018. sizeof(xfs_bmbt_rec_t));
  4019. }
  4020. ifp->if_u1.if_extents = NULL;
  4021. ifp->if_real_bytes = 0;
  4022. ifp->if_bytes = 0;
  4023. }
  4024. /*
  4025. * Return a pointer to the extent record for file system block bno.
  4026. */
  4027. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  4028. xfs_iext_bno_to_ext(
  4029. xfs_ifork_t *ifp, /* inode fork pointer */
  4030. xfs_fileoff_t bno, /* block number to search for */
  4031. xfs_extnum_t *idxp) /* index of target extent */
  4032. {
  4033. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  4034. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  4035. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  4036. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  4037. int high; /* upper boundary in search */
  4038. xfs_extnum_t idx = 0; /* index of target extent */
  4039. int low; /* lower boundary in search */
  4040. xfs_extnum_t nextents; /* number of file extents */
  4041. xfs_fileoff_t startoff = 0; /* start offset of extent */
  4042. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4043. if (nextents == 0) {
  4044. *idxp = 0;
  4045. return NULL;
  4046. }
  4047. low = 0;
  4048. if (ifp->if_flags & XFS_IFEXTIREC) {
  4049. /* Find target extent list */
  4050. int erp_idx = 0;
  4051. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  4052. base = erp->er_extbuf;
  4053. high = erp->er_extcount - 1;
  4054. } else {
  4055. base = ifp->if_u1.if_extents;
  4056. high = nextents - 1;
  4057. }
  4058. /* Binary search extent records */
  4059. while (low <= high) {
  4060. idx = (low + high) >> 1;
  4061. ep = base + idx;
  4062. startoff = xfs_bmbt_get_startoff(ep);
  4063. blockcount = xfs_bmbt_get_blockcount(ep);
  4064. if (bno < startoff) {
  4065. high = idx - 1;
  4066. } else if (bno >= startoff + blockcount) {
  4067. low = idx + 1;
  4068. } else {
  4069. /* Convert back to file-based extent index */
  4070. if (ifp->if_flags & XFS_IFEXTIREC) {
  4071. idx += erp->er_extoff;
  4072. }
  4073. *idxp = idx;
  4074. return ep;
  4075. }
  4076. }
  4077. /* Convert back to file-based extent index */
  4078. if (ifp->if_flags & XFS_IFEXTIREC) {
  4079. idx += erp->er_extoff;
  4080. }
  4081. if (bno >= startoff + blockcount) {
  4082. if (++idx == nextents) {
  4083. ep = NULL;
  4084. } else {
  4085. ep = xfs_iext_get_ext(ifp, idx);
  4086. }
  4087. }
  4088. *idxp = idx;
  4089. return ep;
  4090. }
  4091. /*
  4092. * Return a pointer to the indirection array entry containing the
  4093. * extent record for filesystem block bno. Store the index of the
  4094. * target irec in *erp_idxp.
  4095. */
  4096. xfs_ext_irec_t * /* pointer to found extent record */
  4097. xfs_iext_bno_to_irec(
  4098. xfs_ifork_t *ifp, /* inode fork pointer */
  4099. xfs_fileoff_t bno, /* block number to search for */
  4100. int *erp_idxp) /* irec index of target ext list */
  4101. {
  4102. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  4103. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  4104. int erp_idx; /* indirection array index */
  4105. int nlists; /* number of extent irec's (lists) */
  4106. int high; /* binary search upper limit */
  4107. int low; /* binary search lower limit */
  4108. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4109. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4110. erp_idx = 0;
  4111. low = 0;
  4112. high = nlists - 1;
  4113. while (low <= high) {
  4114. erp_idx = (low + high) >> 1;
  4115. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4116. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  4117. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  4118. high = erp_idx - 1;
  4119. } else if (erp_next && bno >=
  4120. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  4121. low = erp_idx + 1;
  4122. } else {
  4123. break;
  4124. }
  4125. }
  4126. *erp_idxp = erp_idx;
  4127. return erp;
  4128. }
  4129. /*
  4130. * Return a pointer to the indirection array entry containing the
  4131. * extent record at file extent index *idxp. Store the index of the
  4132. * target irec in *erp_idxp and store the page index of the target
  4133. * extent record in *idxp.
  4134. */
  4135. xfs_ext_irec_t *
  4136. xfs_iext_idx_to_irec(
  4137. xfs_ifork_t *ifp, /* inode fork pointer */
  4138. xfs_extnum_t *idxp, /* extent index (file -> page) */
  4139. int *erp_idxp, /* pointer to target irec */
  4140. int realloc) /* new bytes were just added */
  4141. {
  4142. xfs_ext_irec_t *prev; /* pointer to previous irec */
  4143. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  4144. int erp_idx; /* indirection array index */
  4145. int nlists; /* number of irec's (ex lists) */
  4146. int high; /* binary search upper limit */
  4147. int low; /* binary search lower limit */
  4148. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  4149. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4150. ASSERT(page_idx >= 0 && page_idx <=
  4151. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
  4152. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4153. erp_idx = 0;
  4154. low = 0;
  4155. high = nlists - 1;
  4156. /* Binary search extent irec's */
  4157. while (low <= high) {
  4158. erp_idx = (low + high) >> 1;
  4159. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4160. prev = erp_idx > 0 ? erp - 1 : NULL;
  4161. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  4162. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  4163. high = erp_idx - 1;
  4164. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  4165. (page_idx == erp->er_extoff + erp->er_extcount &&
  4166. !realloc)) {
  4167. low = erp_idx + 1;
  4168. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  4169. erp->er_extcount == XFS_LINEAR_EXTS) {
  4170. ASSERT(realloc);
  4171. page_idx = 0;
  4172. erp_idx++;
  4173. erp = erp_idx < nlists ? erp + 1 : NULL;
  4174. break;
  4175. } else {
  4176. page_idx -= erp->er_extoff;
  4177. break;
  4178. }
  4179. }
  4180. *idxp = page_idx;
  4181. *erp_idxp = erp_idx;
  4182. return(erp);
  4183. }
  4184. /*
  4185. * Allocate and initialize an indirection array once the space needed
  4186. * for incore extents increases above XFS_IEXT_BUFSZ.
  4187. */
  4188. void
  4189. xfs_iext_irec_init(
  4190. xfs_ifork_t *ifp) /* inode fork pointer */
  4191. {
  4192. xfs_ext_irec_t *erp; /* indirection array pointer */
  4193. xfs_extnum_t nextents; /* number of extents in file */
  4194. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  4195. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4196. ASSERT(nextents <= XFS_LINEAR_EXTS);
  4197. erp = (xfs_ext_irec_t *)
  4198. kmem_alloc(sizeof(xfs_ext_irec_t), KM_SLEEP);
  4199. if (nextents == 0) {
  4200. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
  4201. } else if (!ifp->if_real_bytes) {
  4202. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  4203. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  4204. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  4205. }
  4206. erp->er_extbuf = ifp->if_u1.if_extents;
  4207. erp->er_extcount = nextents;
  4208. erp->er_extoff = 0;
  4209. ifp->if_flags |= XFS_IFEXTIREC;
  4210. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  4211. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  4212. ifp->if_u1.if_ext_irec = erp;
  4213. return;
  4214. }
  4215. /*
  4216. * Allocate and initialize a new entry in the indirection array.
  4217. */
  4218. xfs_ext_irec_t *
  4219. xfs_iext_irec_new(
  4220. xfs_ifork_t *ifp, /* inode fork pointer */
  4221. int erp_idx) /* index for new irec */
  4222. {
  4223. xfs_ext_irec_t *erp; /* indirection array pointer */
  4224. int i; /* loop counter */
  4225. int nlists; /* number of irec's (ex lists) */
  4226. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4227. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4228. /* Resize indirection array */
  4229. xfs_iext_realloc_indirect(ifp, ++nlists *
  4230. sizeof(xfs_ext_irec_t));
  4231. /*
  4232. * Move records down in the array so the
  4233. * new page can use erp_idx.
  4234. */
  4235. erp = ifp->if_u1.if_ext_irec;
  4236. for (i = nlists - 1; i > erp_idx; i--) {
  4237. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  4238. }
  4239. ASSERT(i == erp_idx);
  4240. /* Initialize new extent record */
  4241. erp = ifp->if_u1.if_ext_irec;
  4242. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
  4243. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  4244. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  4245. erp[erp_idx].er_extcount = 0;
  4246. erp[erp_idx].er_extoff = erp_idx > 0 ?
  4247. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  4248. return (&erp[erp_idx]);
  4249. }
  4250. /*
  4251. * Remove a record from the indirection array.
  4252. */
  4253. void
  4254. xfs_iext_irec_remove(
  4255. xfs_ifork_t *ifp, /* inode fork pointer */
  4256. int erp_idx) /* irec index to remove */
  4257. {
  4258. xfs_ext_irec_t *erp; /* indirection array pointer */
  4259. int i; /* loop counter */
  4260. int nlists; /* number of irec's (ex lists) */
  4261. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4262. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4263. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4264. if (erp->er_extbuf) {
  4265. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  4266. -erp->er_extcount);
  4267. kmem_free(erp->er_extbuf, XFS_IEXT_BUFSZ);
  4268. }
  4269. /* Compact extent records */
  4270. erp = ifp->if_u1.if_ext_irec;
  4271. for (i = erp_idx; i < nlists - 1; i++) {
  4272. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  4273. }
  4274. /*
  4275. * Manually free the last extent record from the indirection
  4276. * array. A call to xfs_iext_realloc_indirect() with a size
  4277. * of zero would result in a call to xfs_iext_destroy() which
  4278. * would in turn call this function again, creating a nasty
  4279. * infinite loop.
  4280. */
  4281. if (--nlists) {
  4282. xfs_iext_realloc_indirect(ifp,
  4283. nlists * sizeof(xfs_ext_irec_t));
  4284. } else {
  4285. kmem_free(ifp->if_u1.if_ext_irec,
  4286. sizeof(xfs_ext_irec_t));
  4287. }
  4288. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  4289. }
  4290. /*
  4291. * This is called to clean up large amounts of unused memory allocated
  4292. * by the indirection array. Before compacting anything though, verify
  4293. * that the indirection array is still needed and switch back to the
  4294. * linear extent list (or even the inline buffer) if possible. The
  4295. * compaction policy is as follows:
  4296. *
  4297. * Full Compaction: Extents fit into a single page (or inline buffer)
  4298. * Full Compaction: Extents occupy less than 10% of allocated space
  4299. * Partial Compaction: Extents occupy > 10% and < 50% of allocated space
  4300. * No Compaction: Extents occupy at least 50% of allocated space
  4301. */
  4302. void
  4303. xfs_iext_irec_compact(
  4304. xfs_ifork_t *ifp) /* inode fork pointer */
  4305. {
  4306. xfs_extnum_t nextents; /* number of extents in file */
  4307. int nlists; /* number of irec's (ex lists) */
  4308. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4309. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4310. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4311. if (nextents == 0) {
  4312. xfs_iext_destroy(ifp);
  4313. } else if (nextents <= XFS_INLINE_EXTS) {
  4314. xfs_iext_indirect_to_direct(ifp);
  4315. xfs_iext_direct_to_inline(ifp, nextents);
  4316. } else if (nextents <= XFS_LINEAR_EXTS) {
  4317. xfs_iext_indirect_to_direct(ifp);
  4318. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 3) {
  4319. xfs_iext_irec_compact_full(ifp);
  4320. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  4321. xfs_iext_irec_compact_pages(ifp);
  4322. }
  4323. }
  4324. /*
  4325. * Combine extents from neighboring extent pages.
  4326. */
  4327. void
  4328. xfs_iext_irec_compact_pages(
  4329. xfs_ifork_t *ifp) /* inode fork pointer */
  4330. {
  4331. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  4332. int erp_idx = 0; /* indirection array index */
  4333. int nlists; /* number of irec's (ex lists) */
  4334. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4335. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4336. while (erp_idx < nlists - 1) {
  4337. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4338. erp_next = erp + 1;
  4339. if (erp_next->er_extcount <=
  4340. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  4341. memmove(&erp->er_extbuf[erp->er_extcount],
  4342. erp_next->er_extbuf, erp_next->er_extcount *
  4343. sizeof(xfs_bmbt_rec_t));
  4344. erp->er_extcount += erp_next->er_extcount;
  4345. /*
  4346. * Free page before removing extent record
  4347. * so er_extoffs don't get modified in
  4348. * xfs_iext_irec_remove.
  4349. */
  4350. kmem_free(erp_next->er_extbuf, XFS_IEXT_BUFSZ);
  4351. erp_next->er_extbuf = NULL;
  4352. xfs_iext_irec_remove(ifp, erp_idx + 1);
  4353. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4354. } else {
  4355. erp_idx++;
  4356. }
  4357. }
  4358. }
  4359. /*
  4360. * Fully compact the extent records managed by the indirection array.
  4361. */
  4362. void
  4363. xfs_iext_irec_compact_full(
  4364. xfs_ifork_t *ifp) /* inode fork pointer */
  4365. {
  4366. xfs_bmbt_rec_host_t *ep, *ep_next; /* extent record pointers */
  4367. xfs_ext_irec_t *erp, *erp_next; /* extent irec pointers */
  4368. int erp_idx = 0; /* extent irec index */
  4369. int ext_avail; /* empty entries in ex list */
  4370. int ext_diff; /* number of exts to add */
  4371. int nlists; /* number of irec's (ex lists) */
  4372. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4373. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4374. erp = ifp->if_u1.if_ext_irec;
  4375. ep = &erp->er_extbuf[erp->er_extcount];
  4376. erp_next = erp + 1;
  4377. ep_next = erp_next->er_extbuf;
  4378. while (erp_idx < nlists - 1) {
  4379. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  4380. ext_diff = MIN(ext_avail, erp_next->er_extcount);
  4381. memcpy(ep, ep_next, ext_diff * sizeof(xfs_bmbt_rec_t));
  4382. erp->er_extcount += ext_diff;
  4383. erp_next->er_extcount -= ext_diff;
  4384. /* Remove next page */
  4385. if (erp_next->er_extcount == 0) {
  4386. /*
  4387. * Free page before removing extent record
  4388. * so er_extoffs don't get modified in
  4389. * xfs_iext_irec_remove.
  4390. */
  4391. kmem_free(erp_next->er_extbuf,
  4392. erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
  4393. erp_next->er_extbuf = NULL;
  4394. xfs_iext_irec_remove(ifp, erp_idx + 1);
  4395. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4396. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4397. /* Update next page */
  4398. } else {
  4399. /* Move rest of page up to become next new page */
  4400. memmove(erp_next->er_extbuf, ep_next,
  4401. erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
  4402. ep_next = erp_next->er_extbuf;
  4403. memset(&ep_next[erp_next->er_extcount], 0,
  4404. (XFS_LINEAR_EXTS - erp_next->er_extcount) *
  4405. sizeof(xfs_bmbt_rec_t));
  4406. }
  4407. if (erp->er_extcount == XFS_LINEAR_EXTS) {
  4408. erp_idx++;
  4409. if (erp_idx < nlists)
  4410. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4411. else
  4412. break;
  4413. }
  4414. ep = &erp->er_extbuf[erp->er_extcount];
  4415. erp_next = erp + 1;
  4416. ep_next = erp_next->er_extbuf;
  4417. }
  4418. }
  4419. /*
  4420. * This is called to update the er_extoff field in the indirection
  4421. * array when extents have been added or removed from one of the
  4422. * extent lists. erp_idx contains the irec index to begin updating
  4423. * at and ext_diff contains the number of extents that were added
  4424. * or removed.
  4425. */
  4426. void
  4427. xfs_iext_irec_update_extoffs(
  4428. xfs_ifork_t *ifp, /* inode fork pointer */
  4429. int erp_idx, /* irec index to update */
  4430. int ext_diff) /* number of new extents */
  4431. {
  4432. int i; /* loop counter */
  4433. int nlists; /* number of irec's (ex lists */
  4434. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4435. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4436. for (i = erp_idx; i < nlists; i++) {
  4437. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  4438. }
  4439. }