xfs_inode.c 128 KB

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