jfs_xtree.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the 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 to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * jfs_xtree.c: extent allocation descriptor B+-tree manager
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/quotaops.h>
  23. #include "jfs_incore.h"
  24. #include "jfs_filsys.h"
  25. #include "jfs_metapage.h"
  26. #include "jfs_dmap.h"
  27. #include "jfs_dinode.h"
  28. #include "jfs_superblock.h"
  29. #include "jfs_debug.h"
  30. /*
  31. * xtree local flag
  32. */
  33. #define XT_INSERT 0x00000001
  34. /*
  35. * xtree key/entry comparison: extent offset
  36. *
  37. * return:
  38. * -1: k < start of extent
  39. * 0: start_of_extent <= k <= end_of_extent
  40. * 1: k > end_of_extent
  41. */
  42. #define XT_CMP(CMP, K, X, OFFSET64)\
  43. {\
  44. OFFSET64 = offsetXAD(X);\
  45. (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
  46. ((K) < OFFSET64) ? -1 : 0;\
  47. }
  48. /* write a xad entry */
  49. #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
  50. {\
  51. (XAD)->flag = (FLAG);\
  52. XADoffset((XAD), (OFF));\
  53. XADlength((XAD), (LEN));\
  54. XADaddress((XAD), (ADDR));\
  55. }
  56. #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
  57. /* get page buffer for specified block address */
  58. /* ToDo: Replace this ugly macro with a function */
  59. #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
  60. {\
  61. BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot)\
  62. if (!(RC))\
  63. {\
  64. if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) ||\
  65. (le16_to_cpu((P)->header.nextindex) > le16_to_cpu((P)->header.maxentry)) ||\
  66. (le16_to_cpu((P)->header.maxentry) > (((BN)==0)?XTROOTMAXSLOT:PSIZE>>L2XTSLOTSIZE)))\
  67. {\
  68. jfs_error((IP)->i_sb, "XT_GETPAGE: xtree page corrupt");\
  69. BT_PUTPAGE(MP);\
  70. MP = NULL;\
  71. RC = -EIO;\
  72. }\
  73. }\
  74. }
  75. /* for consistency */
  76. #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
  77. #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
  78. BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
  79. /* xtree entry parameter descriptor */
  80. struct xtsplit {
  81. struct metapage *mp;
  82. s16 index;
  83. u8 flag;
  84. s64 off;
  85. s64 addr;
  86. int len;
  87. struct pxdlist *pxdlist;
  88. };
  89. /*
  90. * statistics
  91. */
  92. #ifdef CONFIG_JFS_STATISTICS
  93. static struct {
  94. uint search;
  95. uint fastSearch;
  96. uint split;
  97. } xtStat;
  98. #endif
  99. /*
  100. * forward references
  101. */
  102. static int xtSearch(struct inode *ip,
  103. s64 xoff, int *cmpp, struct btstack * btstack, int flag);
  104. static int xtSplitUp(tid_t tid,
  105. struct inode *ip,
  106. struct xtsplit * split, struct btstack * btstack);
  107. static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
  108. struct metapage ** rmpp, s64 * rbnp);
  109. static int xtSplitRoot(tid_t tid, struct inode *ip,
  110. struct xtsplit * split, struct metapage ** rmpp);
  111. #ifdef _STILL_TO_PORT
  112. static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
  113. xtpage_t * fp, struct btstack * btstack);
  114. static int xtSearchNode(struct inode *ip,
  115. xad_t * xad,
  116. int *cmpp, struct btstack * btstack, int flag);
  117. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
  118. #endif /* _STILL_TO_PORT */
  119. /* External references */
  120. /*
  121. * debug control
  122. */
  123. /* #define _JFS_DEBUG_XTREE 1 */
  124. /*
  125. * xtLookup()
  126. *
  127. * function: map a single page into a physical extent;
  128. */
  129. int xtLookup(struct inode *ip, s64 lstart,
  130. s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
  131. {
  132. int rc = 0;
  133. struct btstack btstack;
  134. int cmp;
  135. s64 bn;
  136. struct metapage *mp;
  137. xtpage_t *p;
  138. int index;
  139. xad_t *xad;
  140. s64 size, xoff, xend;
  141. int xlen;
  142. s64 xaddr;
  143. *plen = 0;
  144. if (!no_check) {
  145. /* is lookup offset beyond eof ? */
  146. size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  147. JFS_SBI(ip->i_sb)->l2bsize;
  148. if (lstart >= size) {
  149. jfs_err("xtLookup: lstart (0x%lx) >= size (0x%lx)",
  150. (ulong) lstart, (ulong) size);
  151. return 0;
  152. }
  153. }
  154. /*
  155. * search for the xad entry covering the logical extent
  156. */
  157. //search:
  158. if ((rc = xtSearch(ip, lstart, &cmp, &btstack, 0))) {
  159. jfs_err("xtLookup: xtSearch returned %d", rc);
  160. return rc;
  161. }
  162. /*
  163. * compute the physical extent covering logical extent
  164. *
  165. * N.B. search may have failed (e.g., hole in sparse file),
  166. * and returned the index of the next entry.
  167. */
  168. /* retrieve search result */
  169. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  170. /* is xad found covering start of logical extent ?
  171. * lstart is a page start address,
  172. * i.e., lstart cannot start in a hole;
  173. */
  174. if (cmp)
  175. goto out;
  176. /*
  177. * lxd covered by xad
  178. */
  179. xad = &p->xad[index];
  180. xoff = offsetXAD(xad);
  181. xlen = lengthXAD(xad);
  182. xend = xoff + xlen;
  183. xaddr = addressXAD(xad);
  184. /* initialize new pxd */
  185. *pflag = xad->flag;
  186. *paddr = xaddr + (lstart - xoff);
  187. /* a page must be fully covered by an xad */
  188. *plen = min(xend - lstart, llen);
  189. out:
  190. XT_PUTPAGE(mp);
  191. return rc;
  192. }
  193. /*
  194. * xtLookupList()
  195. *
  196. * function: map a single logical extent into a list of physical extent;
  197. *
  198. * parameter:
  199. * struct inode *ip,
  200. * struct lxdlist *lxdlist, lxd list (in)
  201. * struct xadlist *xadlist, xad list (in/out)
  202. * int flag)
  203. *
  204. * coverage of lxd by xad under assumption of
  205. * . lxd's are ordered and disjoint.
  206. * . xad's are ordered and disjoint.
  207. *
  208. * return:
  209. * 0: success
  210. *
  211. * note: a page being written (even a single byte) is backed fully,
  212. * except the last page which is only backed with blocks
  213. * required to cover the last byte;
  214. * the extent backing a page is fully contained within an xad;
  215. */
  216. int xtLookupList(struct inode *ip, struct lxdlist * lxdlist,
  217. struct xadlist * xadlist, int flag)
  218. {
  219. int rc = 0;
  220. struct btstack btstack;
  221. int cmp;
  222. s64 bn;
  223. struct metapage *mp;
  224. xtpage_t *p;
  225. int index;
  226. lxd_t *lxd;
  227. xad_t *xad, *pxd;
  228. s64 size, lstart, lend, xstart, xend, pstart;
  229. s64 llen, xlen, plen;
  230. s64 xaddr, paddr;
  231. int nlxd, npxd, maxnpxd;
  232. npxd = xadlist->nxad = 0;
  233. maxnpxd = xadlist->maxnxad;
  234. pxd = xadlist->xad;
  235. nlxd = lxdlist->nlxd;
  236. lxd = lxdlist->lxd;
  237. lstart = offsetLXD(lxd);
  238. llen = lengthLXD(lxd);
  239. lend = lstart + llen;
  240. size = (ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  241. JFS_SBI(ip->i_sb)->l2bsize;
  242. /*
  243. * search for the xad entry covering the logical extent
  244. */
  245. search:
  246. if (lstart >= size)
  247. return 0;
  248. if ((rc = xtSearch(ip, lstart, &cmp, &btstack, 0)))
  249. return rc;
  250. /*
  251. * compute the physical extent covering logical extent
  252. *
  253. * N.B. search may have failed (e.g., hole in sparse file),
  254. * and returned the index of the next entry.
  255. */
  256. //map:
  257. /* retrieve search result */
  258. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  259. /* is xad on the next sibling page ? */
  260. if (index == le16_to_cpu(p->header.nextindex)) {
  261. if (p->header.flag & BT_ROOT)
  262. goto mapend;
  263. if ((bn = le64_to_cpu(p->header.next)) == 0)
  264. goto mapend;
  265. XT_PUTPAGE(mp);
  266. /* get next sibling page */
  267. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  268. if (rc)
  269. return rc;
  270. index = XTENTRYSTART;
  271. }
  272. xad = &p->xad[index];
  273. /*
  274. * is lxd covered by xad ?
  275. */
  276. compare:
  277. xstart = offsetXAD(xad);
  278. xlen = lengthXAD(xad);
  279. xend = xstart + xlen;
  280. xaddr = addressXAD(xad);
  281. compare1:
  282. if (xstart < lstart)
  283. goto compare2;
  284. /* (lstart <= xstart) */
  285. /* lxd is NOT covered by xad */
  286. if (lend <= xstart) {
  287. /*
  288. * get next lxd
  289. */
  290. if (--nlxd == 0)
  291. goto mapend;
  292. lxd++;
  293. lstart = offsetLXD(lxd);
  294. llen = lengthLXD(lxd);
  295. lend = lstart + llen;
  296. if (lstart >= size)
  297. goto mapend;
  298. /* compare with the current xad */
  299. goto compare1;
  300. }
  301. /* lxd is covered by xad */
  302. else { /* (xstart < lend) */
  303. /* initialize new pxd */
  304. pstart = xstart;
  305. plen = min(lend - xstart, xlen);
  306. paddr = xaddr;
  307. goto cover;
  308. }
  309. /* (xstart < lstart) */
  310. compare2:
  311. /* lxd is covered by xad */
  312. if (lstart < xend) {
  313. /* initialize new pxd */
  314. pstart = lstart;
  315. plen = min(xend - lstart, llen);
  316. paddr = xaddr + (lstart - xstart);
  317. goto cover;
  318. }
  319. /* lxd is NOT covered by xad */
  320. else { /* (xend <= lstart) */
  321. /*
  322. * get next xad
  323. *
  324. * linear search next xad covering lxd on
  325. * the current xad page, and then tree search
  326. */
  327. if (index == le16_to_cpu(p->header.nextindex) - 1) {
  328. if (p->header.flag & BT_ROOT)
  329. goto mapend;
  330. XT_PUTPAGE(mp);
  331. goto search;
  332. } else {
  333. index++;
  334. xad++;
  335. /* compare with new xad */
  336. goto compare;
  337. }
  338. }
  339. /*
  340. * lxd is covered by xad and a new pxd has been initialized
  341. * (lstart <= xstart < lend) or (xstart < lstart < xend)
  342. */
  343. cover:
  344. /* finalize pxd corresponding to current xad */
  345. XT_PUTENTRY(pxd, xad->flag, pstart, plen, paddr);
  346. if (++npxd >= maxnpxd)
  347. goto mapend;
  348. pxd++;
  349. /*
  350. * lxd is fully covered by xad
  351. */
  352. if (lend <= xend) {
  353. /*
  354. * get next lxd
  355. */
  356. if (--nlxd == 0)
  357. goto mapend;
  358. lxd++;
  359. lstart = offsetLXD(lxd);
  360. llen = lengthLXD(lxd);
  361. lend = lstart + llen;
  362. if (lstart >= size)
  363. goto mapend;
  364. /*
  365. * test for old xad covering new lxd
  366. * (old xstart < new lstart)
  367. */
  368. goto compare2;
  369. }
  370. /*
  371. * lxd is partially covered by xad
  372. */
  373. else { /* (xend < lend) */
  374. /*
  375. * get next xad
  376. *
  377. * linear search next xad covering lxd on
  378. * the current xad page, and then next xad page search
  379. */
  380. if (index == le16_to_cpu(p->header.nextindex) - 1) {
  381. if (p->header.flag & BT_ROOT)
  382. goto mapend;
  383. if ((bn = le64_to_cpu(p->header.next)) == 0)
  384. goto mapend;
  385. XT_PUTPAGE(mp);
  386. /* get next sibling page */
  387. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  388. if (rc)
  389. return rc;
  390. index = XTENTRYSTART;
  391. xad = &p->xad[index];
  392. } else {
  393. index++;
  394. xad++;
  395. }
  396. /*
  397. * test for new xad covering old lxd
  398. * (old lstart < new xstart)
  399. */
  400. goto compare;
  401. }
  402. mapend:
  403. xadlist->nxad = npxd;
  404. //out:
  405. XT_PUTPAGE(mp);
  406. return rc;
  407. }
  408. /*
  409. * xtSearch()
  410. *
  411. * function: search for the xad entry covering specified offset.
  412. *
  413. * parameters:
  414. * ip - file object;
  415. * xoff - extent offset;
  416. * cmpp - comparison result:
  417. * btstack - traverse stack;
  418. * flag - search process flag (XT_INSERT);
  419. *
  420. * returns:
  421. * btstack contains (bn, index) of search path traversed to the entry.
  422. * *cmpp is set to result of comparison with the entry returned.
  423. * the page containing the entry is pinned at exit.
  424. */
  425. static int xtSearch(struct inode *ip, s64 xoff, /* offset of extent */
  426. int *cmpp, struct btstack * btstack, int flag)
  427. {
  428. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  429. int rc = 0;
  430. int cmp = 1; /* init for empty page */
  431. s64 bn; /* block number */
  432. struct metapage *mp; /* page buffer */
  433. xtpage_t *p; /* page */
  434. xad_t *xad;
  435. int base, index, lim, btindex;
  436. struct btframe *btsp;
  437. int nsplit = 0; /* number of pages to split */
  438. s64 t64;
  439. INCREMENT(xtStat.search);
  440. BT_CLR(btstack);
  441. btstack->nsplit = 0;
  442. /*
  443. * search down tree from root:
  444. *
  445. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  446. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  447. *
  448. * if entry with search key K is not found
  449. * internal page search find the entry with largest key Ki
  450. * less than K which point to the child page to search;
  451. * leaf page search find the entry with smallest key Kj
  452. * greater than K so that the returned index is the position of
  453. * the entry to be shifted right for insertion of new entry.
  454. * for empty tree, search key is greater than any key of the tree.
  455. *
  456. * by convention, root bn = 0.
  457. */
  458. for (bn = 0;;) {
  459. /* get/pin the page to search */
  460. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  461. if (rc)
  462. return rc;
  463. /* try sequential access heuristics with the previous
  464. * access entry in target leaf page:
  465. * once search narrowed down into the target leaf,
  466. * key must either match an entry in the leaf or
  467. * key entry does not exist in the tree;
  468. */
  469. //fastSearch:
  470. if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
  471. (p->header.flag & BT_LEAF) &&
  472. (index = jfs_ip->btindex) <
  473. le16_to_cpu(p->header.nextindex)) {
  474. xad = &p->xad[index];
  475. t64 = offsetXAD(xad);
  476. if (xoff < t64 + lengthXAD(xad)) {
  477. if (xoff >= t64) {
  478. *cmpp = 0;
  479. goto out;
  480. }
  481. /* stop sequential access heuristics */
  482. goto binarySearch;
  483. } else { /* (t64 + lengthXAD(xad)) <= xoff */
  484. /* try next sequential entry */
  485. index++;
  486. if (index <
  487. le16_to_cpu(p->header.nextindex)) {
  488. xad++;
  489. t64 = offsetXAD(xad);
  490. if (xoff < t64 + lengthXAD(xad)) {
  491. if (xoff >= t64) {
  492. *cmpp = 0;
  493. goto out;
  494. }
  495. /* miss: key falls between
  496. * previous and this entry
  497. */
  498. *cmpp = 1;
  499. goto out;
  500. }
  501. /* (xoff >= t64 + lengthXAD(xad));
  502. * matching entry may be further out:
  503. * stop heuristic search
  504. */
  505. /* stop sequential access heuristics */
  506. goto binarySearch;
  507. }
  508. /* (index == p->header.nextindex);
  509. * miss: key entry does not exist in
  510. * the target leaf/tree
  511. */
  512. *cmpp = 1;
  513. goto out;
  514. }
  515. /*
  516. * if hit, return index of the entry found, and
  517. * if miss, where new entry with search key is
  518. * to be inserted;
  519. */
  520. out:
  521. /* compute number of pages to split */
  522. if (flag & XT_INSERT) {
  523. if (p->header.nextindex == /* little-endian */
  524. p->header.maxentry)
  525. nsplit++;
  526. else
  527. nsplit = 0;
  528. btstack->nsplit = nsplit;
  529. }
  530. /* save search result */
  531. btsp = btstack->top;
  532. btsp->bn = bn;
  533. btsp->index = index;
  534. btsp->mp = mp;
  535. /* update sequential access heuristics */
  536. jfs_ip->btindex = index;
  537. INCREMENT(xtStat.fastSearch);
  538. return 0;
  539. }
  540. /* well, ... full search now */
  541. binarySearch:
  542. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  543. /*
  544. * binary search with search key K on the current page
  545. */
  546. for (base = XTENTRYSTART; lim; lim >>= 1) {
  547. index = base + (lim >> 1);
  548. XT_CMP(cmp, xoff, &p->xad[index], t64);
  549. if (cmp == 0) {
  550. /*
  551. * search hit
  552. */
  553. /* search hit - leaf page:
  554. * return the entry found
  555. */
  556. if (p->header.flag & BT_LEAF) {
  557. *cmpp = cmp;
  558. /* compute number of pages to split */
  559. if (flag & XT_INSERT) {
  560. if (p->header.nextindex ==
  561. p->header.maxentry)
  562. nsplit++;
  563. else
  564. nsplit = 0;
  565. btstack->nsplit = nsplit;
  566. }
  567. /* save search result */
  568. btsp = btstack->top;
  569. btsp->bn = bn;
  570. btsp->index = index;
  571. btsp->mp = mp;
  572. /* init sequential access heuristics */
  573. btindex = jfs_ip->btindex;
  574. if (index == btindex ||
  575. index == btindex + 1)
  576. jfs_ip->btorder = BT_SEQUENTIAL;
  577. else
  578. jfs_ip->btorder = BT_RANDOM;
  579. jfs_ip->btindex = index;
  580. return 0;
  581. }
  582. /* search hit - internal page:
  583. * descend/search its child page
  584. */
  585. goto next;
  586. }
  587. if (cmp > 0) {
  588. base = index + 1;
  589. --lim;
  590. }
  591. }
  592. /*
  593. * search miss
  594. *
  595. * base is the smallest index with key (Kj) greater than
  596. * search key (K) and may be zero or maxentry index.
  597. */
  598. /*
  599. * search miss - leaf page:
  600. *
  601. * return location of entry (base) where new entry with
  602. * search key K is to be inserted.
  603. */
  604. if (p->header.flag & BT_LEAF) {
  605. *cmpp = cmp;
  606. /* compute number of pages to split */
  607. if (flag & XT_INSERT) {
  608. if (p->header.nextindex ==
  609. p->header.maxentry)
  610. nsplit++;
  611. else
  612. nsplit = 0;
  613. btstack->nsplit = nsplit;
  614. }
  615. /* save search result */
  616. btsp = btstack->top;
  617. btsp->bn = bn;
  618. btsp->index = base;
  619. btsp->mp = mp;
  620. /* init sequential access heuristics */
  621. btindex = jfs_ip->btindex;
  622. if (base == btindex || base == btindex + 1)
  623. jfs_ip->btorder = BT_SEQUENTIAL;
  624. else
  625. jfs_ip->btorder = BT_RANDOM;
  626. jfs_ip->btindex = base;
  627. return 0;
  628. }
  629. /*
  630. * search miss - non-leaf page:
  631. *
  632. * if base is non-zero, decrement base by one to get the parent
  633. * entry of the child page to search.
  634. */
  635. index = base ? base - 1 : base;
  636. /*
  637. * go down to child page
  638. */
  639. next:
  640. /* update number of pages to split */
  641. if (p->header.nextindex == p->header.maxentry)
  642. nsplit++;
  643. else
  644. nsplit = 0;
  645. /* push (bn, index) of the parent page/entry */
  646. BT_PUSH(btstack, bn, index);
  647. /* get the child page block number */
  648. bn = addressXAD(&p->xad[index]);
  649. /* unpin the parent page */
  650. XT_PUTPAGE(mp);
  651. }
  652. }
  653. /*
  654. * xtInsert()
  655. *
  656. * function:
  657. *
  658. * parameter:
  659. * tid - transaction id;
  660. * ip - file object;
  661. * xflag - extent flag (XAD_NOTRECORDED):
  662. * xoff - extent offset;
  663. * xlen - extent length;
  664. * xaddrp - extent address pointer (in/out):
  665. * if (*xaddrp)
  666. * caller allocated data extent at *xaddrp;
  667. * else
  668. * allocate data extent and return its xaddr;
  669. * flag -
  670. *
  671. * return:
  672. */
  673. int xtInsert(tid_t tid, /* transaction id */
  674. struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
  675. int flag)
  676. {
  677. int rc = 0;
  678. s64 xaddr, hint;
  679. struct metapage *mp; /* meta-page buffer */
  680. xtpage_t *p; /* base B+-tree index page */
  681. s64 bn;
  682. int index, nextindex;
  683. struct btstack btstack; /* traverse stack */
  684. struct xtsplit split; /* split information */
  685. xad_t *xad;
  686. int cmp;
  687. struct tlock *tlck;
  688. struct xtlock *xtlck;
  689. jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  690. /*
  691. * search for the entry location at which to insert:
  692. *
  693. * xtFastSearch() and xtSearch() both returns (leaf page
  694. * pinned, index at which to insert).
  695. * n.b. xtSearch() may return index of maxentry of
  696. * the full page.
  697. */
  698. if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
  699. return rc;
  700. /* retrieve search result */
  701. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  702. /* This test must follow XT_GETSEARCH since mp must be valid if
  703. * we branch to out: */
  704. if (cmp == 0) {
  705. rc = -EEXIST;
  706. goto out;
  707. }
  708. /*
  709. * allocate data extent requested
  710. *
  711. * allocation hint: last xad
  712. */
  713. if ((xaddr = *xaddrp) == 0) {
  714. if (index > XTENTRYSTART) {
  715. xad = &p->xad[index - 1];
  716. hint = addressXAD(xad) + lengthXAD(xad) - 1;
  717. } else
  718. hint = 0;
  719. if ((rc = DQUOT_ALLOC_BLOCK(ip, xlen)))
  720. goto out;
  721. if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
  722. DQUOT_FREE_BLOCK(ip, xlen);
  723. goto out;
  724. }
  725. }
  726. /*
  727. * insert entry for new extent
  728. */
  729. xflag |= XAD_NEW;
  730. /*
  731. * if the leaf page is full, split the page and
  732. * propagate up the router entry for the new page from split
  733. *
  734. * The xtSplitUp() will insert the entry and unpin the leaf page.
  735. */
  736. nextindex = le16_to_cpu(p->header.nextindex);
  737. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  738. split.mp = mp;
  739. split.index = index;
  740. split.flag = xflag;
  741. split.off = xoff;
  742. split.len = xlen;
  743. split.addr = xaddr;
  744. split.pxdlist = NULL;
  745. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  746. /* undo data extent allocation */
  747. if (*xaddrp == 0) {
  748. dbFree(ip, xaddr, (s64) xlen);
  749. DQUOT_FREE_BLOCK(ip, xlen);
  750. }
  751. return rc;
  752. }
  753. *xaddrp = xaddr;
  754. return 0;
  755. }
  756. /*
  757. * insert the new entry into the leaf page
  758. */
  759. /*
  760. * acquire a transaction lock on the leaf page;
  761. *
  762. * action: xad insertion/extension;
  763. */
  764. BT_MARK_DIRTY(mp, ip);
  765. /* if insert into middle, shift right remaining entries. */
  766. if (index < nextindex)
  767. memmove(&p->xad[index + 1], &p->xad[index],
  768. (nextindex - index) * sizeof(xad_t));
  769. /* insert the new entry: mark the entry NEW */
  770. xad = &p->xad[index];
  771. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  772. /* advance next available entry index */
  773. p->header.nextindex =
  774. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  775. /* Don't log it if there are no links to the file */
  776. if (!test_cflag(COMMIT_Nolink, ip)) {
  777. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  778. xtlck = (struct xtlock *) & tlck->lock;
  779. xtlck->lwm.offset =
  780. (xtlck->lwm.offset) ? min(index,
  781. (int)xtlck->lwm.offset) : index;
  782. xtlck->lwm.length =
  783. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  784. }
  785. *xaddrp = xaddr;
  786. out:
  787. /* unpin the leaf page */
  788. XT_PUTPAGE(mp);
  789. return rc;
  790. }
  791. /*
  792. * xtSplitUp()
  793. *
  794. * function:
  795. * split full pages as propagating insertion up the tree
  796. *
  797. * parameter:
  798. * tid - transaction id;
  799. * ip - file object;
  800. * split - entry parameter descriptor;
  801. * btstack - traverse stack from xtSearch()
  802. *
  803. * return:
  804. */
  805. static int
  806. xtSplitUp(tid_t tid,
  807. struct inode *ip, struct xtsplit * split, struct btstack * btstack)
  808. {
  809. int rc = 0;
  810. struct metapage *smp;
  811. xtpage_t *sp; /* split page */
  812. struct metapage *rmp;
  813. s64 rbn; /* new right page block number */
  814. struct metapage *rcmp;
  815. xtpage_t *rcp; /* right child page */
  816. s64 rcbn; /* right child page block number */
  817. int skip; /* index of entry of insertion */
  818. int nextindex; /* next available entry index of p */
  819. struct btframe *parent; /* parent page entry on traverse stack */
  820. xad_t *xad;
  821. s64 xaddr;
  822. int xlen;
  823. int nsplit; /* number of pages split */
  824. struct pxdlist pxdlist;
  825. pxd_t *pxd;
  826. struct tlock *tlck;
  827. struct xtlock *xtlck;
  828. smp = split->mp;
  829. sp = XT_PAGE(ip, smp);
  830. /* is inode xtree root extension/inline EA area free ? */
  831. if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
  832. (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
  833. (JFS_IP(ip)->mode2 & INLINEEA)) {
  834. sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
  835. JFS_IP(ip)->mode2 &= ~INLINEEA;
  836. BT_MARK_DIRTY(smp, ip);
  837. /*
  838. * acquire a transaction lock on the leaf page;
  839. *
  840. * action: xad insertion/extension;
  841. */
  842. /* if insert into middle, shift right remaining entries. */
  843. skip = split->index;
  844. nextindex = le16_to_cpu(sp->header.nextindex);
  845. if (skip < nextindex)
  846. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  847. (nextindex - skip) * sizeof(xad_t));
  848. /* insert the new entry: mark the entry NEW */
  849. xad = &sp->xad[skip];
  850. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  851. split->addr);
  852. /* advance next available entry index */
  853. sp->header.nextindex =
  854. cpu_to_le16(le16_to_cpu(sp->header.nextindex) + 1);
  855. /* Don't log it if there are no links to the file */
  856. if (!test_cflag(COMMIT_Nolink, ip)) {
  857. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  858. xtlck = (struct xtlock *) & tlck->lock;
  859. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  860. min(skip, (int)xtlck->lwm.offset) : skip;
  861. xtlck->lwm.length =
  862. le16_to_cpu(sp->header.nextindex) -
  863. xtlck->lwm.offset;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * allocate new index blocks to cover index page split(s)
  869. *
  870. * allocation hint: ?
  871. */
  872. if (split->pxdlist == NULL) {
  873. nsplit = btstack->nsplit;
  874. split->pxdlist = &pxdlist;
  875. pxdlist.maxnpxd = pxdlist.npxd = 0;
  876. pxd = &pxdlist.pxd[0];
  877. xlen = JFS_SBI(ip->i_sb)->nbperpage;
  878. for (; nsplit > 0; nsplit--, pxd++) {
  879. if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
  880. == 0) {
  881. PXDaddress(pxd, xaddr);
  882. PXDlength(pxd, xlen);
  883. pxdlist.maxnpxd++;
  884. continue;
  885. }
  886. /* undo allocation */
  887. XT_PUTPAGE(smp);
  888. return rc;
  889. }
  890. }
  891. /*
  892. * Split leaf page <sp> into <sp> and a new right page <rp>.
  893. *
  894. * The split routines insert the new entry into the leaf page,
  895. * and acquire txLock as appropriate.
  896. * return <rp> pinned and its block number <rpbn>.
  897. */
  898. rc = (sp->header.flag & BT_ROOT) ?
  899. xtSplitRoot(tid, ip, split, &rmp) :
  900. xtSplitPage(tid, ip, split, &rmp, &rbn);
  901. XT_PUTPAGE(smp);
  902. if (rc)
  903. return -EIO;
  904. /*
  905. * propagate up the router entry for the leaf page just split
  906. *
  907. * insert a router entry for the new page into the parent page,
  908. * propagate the insert/split up the tree by walking back the stack
  909. * of (bn of parent page, index of child page entry in parent page)
  910. * that were traversed during the search for the page that split.
  911. *
  912. * the propagation of insert/split up the tree stops if the root
  913. * splits or the page inserted into doesn't have to split to hold
  914. * the new entry.
  915. *
  916. * the parent entry for the split page remains the same, and
  917. * a new entry is inserted at its right with the first key and
  918. * block number of the new right page.
  919. *
  920. * There are a maximum of 3 pages pinned at any time:
  921. * right child, left parent and right parent (when the parent splits)
  922. * to keep the child page pinned while working on the parent.
  923. * make sure that all pins are released at exit.
  924. */
  925. while ((parent = BT_POP(btstack)) != NULL) {
  926. /* parent page specified by stack frame <parent> */
  927. /* keep current child pages <rcp> pinned */
  928. rcmp = rmp;
  929. rcbn = rbn;
  930. rcp = XT_PAGE(ip, rcmp);
  931. /*
  932. * insert router entry in parent for new right child page <rp>
  933. */
  934. /* get/pin the parent page <sp> */
  935. XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
  936. if (rc) {
  937. XT_PUTPAGE(rcmp);
  938. return rc;
  939. }
  940. /*
  941. * The new key entry goes ONE AFTER the index of parent entry,
  942. * because the split was to the right.
  943. */
  944. skip = parent->index + 1;
  945. /*
  946. * split or shift right remaining entries of the parent page
  947. */
  948. nextindex = le16_to_cpu(sp->header.nextindex);
  949. /*
  950. * parent page is full - split the parent page
  951. */
  952. if (nextindex == le16_to_cpu(sp->header.maxentry)) {
  953. /* init for parent page split */
  954. split->mp = smp;
  955. split->index = skip; /* index at insert */
  956. split->flag = XAD_NEW;
  957. split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
  958. split->len = JFS_SBI(ip->i_sb)->nbperpage;
  959. split->addr = rcbn;
  960. /* unpin previous right child page */
  961. XT_PUTPAGE(rcmp);
  962. /* The split routines insert the new entry,
  963. * and acquire txLock as appropriate.
  964. * return <rp> pinned and its block number <rpbn>.
  965. */
  966. rc = (sp->header.flag & BT_ROOT) ?
  967. xtSplitRoot(tid, ip, split, &rmp) :
  968. xtSplitPage(tid, ip, split, &rmp, &rbn);
  969. if (rc) {
  970. XT_PUTPAGE(smp);
  971. return rc;
  972. }
  973. XT_PUTPAGE(smp);
  974. /* keep new child page <rp> pinned */
  975. }
  976. /*
  977. * parent page is not full - insert in parent page
  978. */
  979. else {
  980. /*
  981. * insert router entry in parent for the right child
  982. * page from the first entry of the right child page:
  983. */
  984. /*
  985. * acquire a transaction lock on the parent page;
  986. *
  987. * action: router xad insertion;
  988. */
  989. BT_MARK_DIRTY(smp, ip);
  990. /*
  991. * if insert into middle, shift right remaining entries
  992. */
  993. if (skip < nextindex)
  994. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  995. (nextindex -
  996. skip) << L2XTSLOTSIZE);
  997. /* insert the router entry */
  998. xad = &sp->xad[skip];
  999. XT_PUTENTRY(xad, XAD_NEW,
  1000. offsetXAD(&rcp->xad[XTENTRYSTART]),
  1001. JFS_SBI(ip->i_sb)->nbperpage, rcbn);
  1002. /* advance next available entry index. */
  1003. sp->header.nextindex =
  1004. cpu_to_le16(le16_to_cpu(sp->header.nextindex) +
  1005. 1);
  1006. /* Don't log it if there are no links to the file */
  1007. if (!test_cflag(COMMIT_Nolink, ip)) {
  1008. tlck = txLock(tid, ip, smp,
  1009. tlckXTREE | tlckGROW);
  1010. xtlck = (struct xtlock *) & tlck->lock;
  1011. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1012. min(skip, (int)xtlck->lwm.offset) : skip;
  1013. xtlck->lwm.length =
  1014. le16_to_cpu(sp->header.nextindex) -
  1015. xtlck->lwm.offset;
  1016. }
  1017. /* unpin parent page */
  1018. XT_PUTPAGE(smp);
  1019. /* exit propagate up */
  1020. break;
  1021. }
  1022. }
  1023. /* unpin current right page */
  1024. XT_PUTPAGE(rmp);
  1025. return 0;
  1026. }
  1027. /*
  1028. * xtSplitPage()
  1029. *
  1030. * function:
  1031. * split a full non-root page into
  1032. * original/split/left page and new right page
  1033. * i.e., the original/split page remains as left page.
  1034. *
  1035. * parameter:
  1036. * int tid,
  1037. * struct inode *ip,
  1038. * struct xtsplit *split,
  1039. * struct metapage **rmpp,
  1040. * u64 *rbnp,
  1041. *
  1042. * return:
  1043. * Pointer to page in which to insert or NULL on error.
  1044. */
  1045. static int
  1046. xtSplitPage(tid_t tid, struct inode *ip,
  1047. struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
  1048. {
  1049. int rc = 0;
  1050. struct metapage *smp;
  1051. xtpage_t *sp;
  1052. struct metapage *rmp;
  1053. xtpage_t *rp; /* new right page allocated */
  1054. s64 rbn; /* new right page block number */
  1055. struct metapage *mp;
  1056. xtpage_t *p;
  1057. s64 nextbn;
  1058. int skip, maxentry, middle, righthalf, n;
  1059. xad_t *xad;
  1060. struct pxdlist *pxdlist;
  1061. pxd_t *pxd;
  1062. struct tlock *tlck;
  1063. struct xtlock *sxtlck = NULL, *rxtlck = NULL;
  1064. int quota_allocation = 0;
  1065. smp = split->mp;
  1066. sp = XT_PAGE(ip, smp);
  1067. INCREMENT(xtStat.split);
  1068. pxdlist = split->pxdlist;
  1069. pxd = &pxdlist->pxd[pxdlist->npxd];
  1070. pxdlist->npxd++;
  1071. rbn = addressPXD(pxd);
  1072. /* Allocate blocks to quota. */
  1073. if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
  1074. rc = -EDQUOT;
  1075. goto clean_up;
  1076. }
  1077. quota_allocation += lengthPXD(pxd);
  1078. /*
  1079. * allocate the new right page for the split
  1080. */
  1081. rmp = get_metapage(ip, rbn, PSIZE, 1);
  1082. if (rmp == NULL) {
  1083. rc = -EIO;
  1084. goto clean_up;
  1085. }
  1086. jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
  1087. BT_MARK_DIRTY(rmp, ip);
  1088. /*
  1089. * action: new page;
  1090. */
  1091. rp = (xtpage_t *) rmp->data;
  1092. rp->header.self = *pxd;
  1093. rp->header.flag = sp->header.flag & BT_TYPE;
  1094. rp->header.maxentry = sp->header.maxentry; /* little-endian */
  1095. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  1096. BT_MARK_DIRTY(smp, ip);
  1097. /* Don't log it if there are no links to the file */
  1098. if (!test_cflag(COMMIT_Nolink, ip)) {
  1099. /*
  1100. * acquire a transaction lock on the new right page;
  1101. */
  1102. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  1103. rxtlck = (struct xtlock *) & tlck->lock;
  1104. rxtlck->lwm.offset = XTENTRYSTART;
  1105. /*
  1106. * acquire a transaction lock on the split page
  1107. */
  1108. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  1109. sxtlck = (struct xtlock *) & tlck->lock;
  1110. }
  1111. /*
  1112. * initialize/update sibling pointers of <sp> and <rp>
  1113. */
  1114. nextbn = le64_to_cpu(sp->header.next);
  1115. rp->header.next = cpu_to_le64(nextbn);
  1116. rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
  1117. sp->header.next = cpu_to_le64(rbn);
  1118. skip = split->index;
  1119. /*
  1120. * sequential append at tail (after last entry of last page)
  1121. *
  1122. * if splitting the last page on a level because of appending
  1123. * a entry to it (skip is maxentry), it's likely that the access is
  1124. * sequential. adding an empty page on the side of the level is less
  1125. * work and can push the fill factor much higher than normal.
  1126. * if we're wrong it's no big deal - we will do the split the right
  1127. * way next time.
  1128. * (it may look like it's equally easy to do a similar hack for
  1129. * reverse sorted data, that is, split the tree left, but it's not.
  1130. * Be my guest.)
  1131. */
  1132. if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
  1133. /*
  1134. * acquire a transaction lock on the new/right page;
  1135. *
  1136. * action: xad insertion;
  1137. */
  1138. /* insert entry at the first entry of the new right page */
  1139. xad = &rp->xad[XTENTRYSTART];
  1140. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  1141. split->addr);
  1142. rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  1143. if (!test_cflag(COMMIT_Nolink, ip)) {
  1144. /* rxtlck->lwm.offset = XTENTRYSTART; */
  1145. rxtlck->lwm.length = 1;
  1146. }
  1147. *rmpp = rmp;
  1148. *rbnp = rbn;
  1149. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  1150. return 0;
  1151. }
  1152. /*
  1153. * non-sequential insert (at possibly middle page)
  1154. */
  1155. /*
  1156. * update previous pointer of old next/right page of <sp>
  1157. */
  1158. if (nextbn != 0) {
  1159. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  1160. if (rc) {
  1161. XT_PUTPAGE(rmp);
  1162. goto clean_up;
  1163. }
  1164. BT_MARK_DIRTY(mp, ip);
  1165. /*
  1166. * acquire a transaction lock on the next page;
  1167. *
  1168. * action:sibling pointer update;
  1169. */
  1170. if (!test_cflag(COMMIT_Nolink, ip))
  1171. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  1172. p->header.prev = cpu_to_le64(rbn);
  1173. /* sibling page may have been updated previously, or
  1174. * it may be updated later;
  1175. */
  1176. XT_PUTPAGE(mp);
  1177. }
  1178. /*
  1179. * split the data between the split and new/right pages
  1180. */
  1181. maxentry = le16_to_cpu(sp->header.maxentry);
  1182. middle = maxentry >> 1;
  1183. righthalf = maxentry - middle;
  1184. /*
  1185. * skip index in old split/left page - insert into left page:
  1186. */
  1187. if (skip <= middle) {
  1188. /* move right half of split page to the new right page */
  1189. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  1190. righthalf << L2XTSLOTSIZE);
  1191. /* shift right tail of left half to make room for new entry */
  1192. if (skip < middle)
  1193. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  1194. (middle - skip) << L2XTSLOTSIZE);
  1195. /* insert new entry */
  1196. xad = &sp->xad[skip];
  1197. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  1198. split->addr);
  1199. /* update page header */
  1200. sp->header.nextindex = cpu_to_le16(middle + 1);
  1201. if (!test_cflag(COMMIT_Nolink, ip)) {
  1202. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  1203. min(skip, (int)sxtlck->lwm.offset) : skip;
  1204. }
  1205. rp->header.nextindex =
  1206. cpu_to_le16(XTENTRYSTART + righthalf);
  1207. }
  1208. /*
  1209. * skip index in new right page - insert into right page:
  1210. */
  1211. else {
  1212. /* move left head of right half to right page */
  1213. n = skip - middle;
  1214. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  1215. n << L2XTSLOTSIZE);
  1216. /* insert new entry */
  1217. n += XTENTRYSTART;
  1218. xad = &rp->xad[n];
  1219. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  1220. split->addr);
  1221. /* move right tail of right half to right page */
  1222. if (skip < maxentry)
  1223. memmove(&rp->xad[n + 1], &sp->xad[skip],
  1224. (maxentry - skip) << L2XTSLOTSIZE);
  1225. /* update page header */
  1226. sp->header.nextindex = cpu_to_le16(middle);
  1227. if (!test_cflag(COMMIT_Nolink, ip)) {
  1228. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  1229. min(middle, (int)sxtlck->lwm.offset) : middle;
  1230. }
  1231. rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
  1232. righthalf + 1);
  1233. }
  1234. if (!test_cflag(COMMIT_Nolink, ip)) {
  1235. sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
  1236. sxtlck->lwm.offset;
  1237. /* rxtlck->lwm.offset = XTENTRYSTART; */
  1238. rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1239. XTENTRYSTART;
  1240. }
  1241. *rmpp = rmp;
  1242. *rbnp = rbn;
  1243. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  1244. return rc;
  1245. clean_up:
  1246. /* Rollback quota allocation. */
  1247. if (quota_allocation)
  1248. DQUOT_FREE_BLOCK(ip, quota_allocation);
  1249. return (rc);
  1250. }
  1251. /*
  1252. * xtSplitRoot()
  1253. *
  1254. * function:
  1255. * split the full root page into
  1256. * original/root/split page and new right page
  1257. * i.e., root remains fixed in tree anchor (inode) and
  1258. * the root is copied to a single new right child page
  1259. * since root page << non-root page, and
  1260. * the split root page contains a single entry for the
  1261. * new right child page.
  1262. *
  1263. * parameter:
  1264. * int tid,
  1265. * struct inode *ip,
  1266. * struct xtsplit *split,
  1267. * struct metapage **rmpp)
  1268. *
  1269. * return:
  1270. * Pointer to page in which to insert or NULL on error.
  1271. */
  1272. static int
  1273. xtSplitRoot(tid_t tid,
  1274. struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
  1275. {
  1276. xtpage_t *sp;
  1277. struct metapage *rmp;
  1278. xtpage_t *rp;
  1279. s64 rbn;
  1280. int skip, nextindex;
  1281. xad_t *xad;
  1282. pxd_t *pxd;
  1283. struct pxdlist *pxdlist;
  1284. struct tlock *tlck;
  1285. struct xtlock *xtlck;
  1286. sp = &JFS_IP(ip)->i_xtroot;
  1287. INCREMENT(xtStat.split);
  1288. /*
  1289. * allocate a single (right) child page
  1290. */
  1291. pxdlist = split->pxdlist;
  1292. pxd = &pxdlist->pxd[pxdlist->npxd];
  1293. pxdlist->npxd++;
  1294. rbn = addressPXD(pxd);
  1295. rmp = get_metapage(ip, rbn, PSIZE, 1);
  1296. if (rmp == NULL)
  1297. return -EIO;
  1298. /* Allocate blocks to quota. */
  1299. if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
  1300. release_metapage(rmp);
  1301. return -EDQUOT;
  1302. }
  1303. jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
  1304. /*
  1305. * acquire a transaction lock on the new right page;
  1306. *
  1307. * action: new page;
  1308. */
  1309. BT_MARK_DIRTY(rmp, ip);
  1310. rp = (xtpage_t *) rmp->data;
  1311. rp->header.flag =
  1312. (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
  1313. rp->header.self = *pxd;
  1314. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  1315. rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
  1316. /* initialize sibling pointers */
  1317. rp->header.next = 0;
  1318. rp->header.prev = 0;
  1319. /*
  1320. * copy the in-line root page into new right page extent
  1321. */
  1322. nextindex = le16_to_cpu(sp->header.maxentry);
  1323. memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
  1324. (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
  1325. /*
  1326. * insert the new entry into the new right/child page
  1327. * (skip index in the new right page will not change)
  1328. */
  1329. skip = split->index;
  1330. /* if insert into middle, shift right remaining entries */
  1331. if (skip != nextindex)
  1332. memmove(&rp->xad[skip + 1], &rp->xad[skip],
  1333. (nextindex - skip) * sizeof(xad_t));
  1334. xad = &rp->xad[skip];
  1335. XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
  1336. /* update page header */
  1337. rp->header.nextindex = cpu_to_le16(nextindex + 1);
  1338. if (!test_cflag(COMMIT_Nolink, ip)) {
  1339. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  1340. xtlck = (struct xtlock *) & tlck->lock;
  1341. xtlck->lwm.offset = XTENTRYSTART;
  1342. xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1343. XTENTRYSTART;
  1344. }
  1345. /*
  1346. * reset the root
  1347. *
  1348. * init root with the single entry for the new right page
  1349. * set the 1st entry offset to 0, which force the left-most key
  1350. * at any level of the tree to be less than any search key.
  1351. */
  1352. /*
  1353. * acquire a transaction lock on the root page (in-memory inode);
  1354. *
  1355. * action: root split;
  1356. */
  1357. BT_MARK_DIRTY(split->mp, ip);
  1358. xad = &sp->xad[XTENTRYSTART];
  1359. XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
  1360. /* update page header of root */
  1361. sp->header.flag &= ~BT_LEAF;
  1362. sp->header.flag |= BT_INTERNAL;
  1363. sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  1364. if (!test_cflag(COMMIT_Nolink, ip)) {
  1365. tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
  1366. xtlck = (struct xtlock *) & tlck->lock;
  1367. xtlck->lwm.offset = XTENTRYSTART;
  1368. xtlck->lwm.length = 1;
  1369. }
  1370. *rmpp = rmp;
  1371. jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
  1372. return 0;
  1373. }
  1374. /*
  1375. * xtExtend()
  1376. *
  1377. * function: extend in-place;
  1378. *
  1379. * note: existing extent may or may not have been committed.
  1380. * caller is responsible for pager buffer cache update, and
  1381. * working block allocation map update;
  1382. * update pmap: alloc whole extended extent;
  1383. */
  1384. int xtExtend(tid_t tid, /* transaction id */
  1385. struct inode *ip, s64 xoff, /* delta extent offset */
  1386. s32 xlen, /* delta extent length */
  1387. int flag)
  1388. {
  1389. int rc = 0;
  1390. int cmp;
  1391. struct metapage *mp; /* meta-page buffer */
  1392. xtpage_t *p; /* base B+-tree index page */
  1393. s64 bn;
  1394. int index, nextindex, len;
  1395. struct btstack btstack; /* traverse stack */
  1396. struct xtsplit split; /* split information */
  1397. xad_t *xad;
  1398. s64 xaddr;
  1399. struct tlock *tlck;
  1400. struct xtlock *xtlck = NULL;
  1401. jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  1402. /* there must exist extent to be extended */
  1403. if ((rc = xtSearch(ip, xoff - 1, &cmp, &btstack, XT_INSERT)))
  1404. return rc;
  1405. /* retrieve search result */
  1406. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1407. if (cmp != 0) {
  1408. XT_PUTPAGE(mp);
  1409. jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
  1410. return -EIO;
  1411. }
  1412. /* extension must be contiguous */
  1413. xad = &p->xad[index];
  1414. if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
  1415. XT_PUTPAGE(mp);
  1416. jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
  1417. return -EIO;
  1418. }
  1419. /*
  1420. * acquire a transaction lock on the leaf page;
  1421. *
  1422. * action: xad insertion/extension;
  1423. */
  1424. BT_MARK_DIRTY(mp, ip);
  1425. if (!test_cflag(COMMIT_Nolink, ip)) {
  1426. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1427. xtlck = (struct xtlock *) & tlck->lock;
  1428. }
  1429. /* extend will overflow extent ? */
  1430. xlen = lengthXAD(xad) + xlen;
  1431. if ((len = xlen - MAXXLEN) <= 0)
  1432. goto extendOld;
  1433. /*
  1434. * extent overflow: insert entry for new extent
  1435. */
  1436. //insertNew:
  1437. xoff = offsetXAD(xad) + MAXXLEN;
  1438. xaddr = addressXAD(xad) + MAXXLEN;
  1439. nextindex = le16_to_cpu(p->header.nextindex);
  1440. /*
  1441. * if the leaf page is full, insert the new entry and
  1442. * propagate up the router entry for the new page from split
  1443. *
  1444. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1445. */
  1446. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1447. /* xtSpliUp() unpins leaf pages */
  1448. split.mp = mp;
  1449. split.index = index + 1;
  1450. split.flag = XAD_NEW;
  1451. split.off = xoff; /* split offset */
  1452. split.len = len;
  1453. split.addr = xaddr;
  1454. split.pxdlist = NULL;
  1455. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1456. return rc;
  1457. /* get back old page */
  1458. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1459. if (rc)
  1460. return rc;
  1461. /*
  1462. * if leaf root has been split, original root has been
  1463. * copied to new child page, i.e., original entry now
  1464. * resides on the new child page;
  1465. */
  1466. if (p->header.flag & BT_INTERNAL) {
  1467. ASSERT(p->header.nextindex ==
  1468. cpu_to_le16(XTENTRYSTART + 1));
  1469. xad = &p->xad[XTENTRYSTART];
  1470. bn = addressXAD(xad);
  1471. XT_PUTPAGE(mp);
  1472. /* get new child page */
  1473. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1474. if (rc)
  1475. return rc;
  1476. BT_MARK_DIRTY(mp, ip);
  1477. if (!test_cflag(COMMIT_Nolink, ip)) {
  1478. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1479. xtlck = (struct xtlock *) & tlck->lock;
  1480. }
  1481. }
  1482. }
  1483. /*
  1484. * insert the new entry into the leaf page
  1485. */
  1486. else {
  1487. /* insert the new entry: mark the entry NEW */
  1488. xad = &p->xad[index + 1];
  1489. XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
  1490. /* advance next available entry index */
  1491. p->header.nextindex =
  1492. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1493. }
  1494. /* get back old entry */
  1495. xad = &p->xad[index];
  1496. xlen = MAXXLEN;
  1497. /*
  1498. * extend old extent
  1499. */
  1500. extendOld:
  1501. XADlength(xad, xlen);
  1502. if (!(xad->flag & XAD_NEW))
  1503. xad->flag |= XAD_EXTENDED;
  1504. if (!test_cflag(COMMIT_Nolink, ip)) {
  1505. xtlck->lwm.offset =
  1506. (xtlck->lwm.offset) ? min(index,
  1507. (int)xtlck->lwm.offset) : index;
  1508. xtlck->lwm.length =
  1509. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  1510. }
  1511. /* unpin the leaf page */
  1512. XT_PUTPAGE(mp);
  1513. return rc;
  1514. }
  1515. #ifdef _NOTYET
  1516. /*
  1517. * xtTailgate()
  1518. *
  1519. * function: split existing 'tail' extent
  1520. * (split offset >= start offset of tail extent), and
  1521. * relocate and extend the split tail half;
  1522. *
  1523. * note: existing extent may or may not have been committed.
  1524. * caller is responsible for pager buffer cache update, and
  1525. * working block allocation map update;
  1526. * update pmap: free old split tail extent, alloc new extent;
  1527. */
  1528. int xtTailgate(tid_t tid, /* transaction id */
  1529. struct inode *ip, s64 xoff, /* split/new extent offset */
  1530. s32 xlen, /* new extent length */
  1531. s64 xaddr, /* new extent address */
  1532. int flag)
  1533. {
  1534. int rc = 0;
  1535. int cmp;
  1536. struct metapage *mp; /* meta-page buffer */
  1537. xtpage_t *p; /* base B+-tree index page */
  1538. s64 bn;
  1539. int index, nextindex, llen, rlen;
  1540. struct btstack btstack; /* traverse stack */
  1541. struct xtsplit split; /* split information */
  1542. xad_t *xad;
  1543. struct tlock *tlck;
  1544. struct xtlock *xtlck = 0;
  1545. struct tlock *mtlck;
  1546. struct maplock *pxdlock;
  1547. /*
  1548. printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
  1549. (ulong)xoff, xlen, (ulong)xaddr);
  1550. */
  1551. /* there must exist extent to be tailgated */
  1552. if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
  1553. return rc;
  1554. /* retrieve search result */
  1555. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1556. if (cmp != 0) {
  1557. XT_PUTPAGE(mp);
  1558. jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
  1559. return -EIO;
  1560. }
  1561. /* entry found must be last entry */
  1562. nextindex = le16_to_cpu(p->header.nextindex);
  1563. if (index != nextindex - 1) {
  1564. XT_PUTPAGE(mp);
  1565. jfs_error(ip->i_sb,
  1566. "xtTailgate: the entry found is not the last entry");
  1567. return -EIO;
  1568. }
  1569. BT_MARK_DIRTY(mp, ip);
  1570. /*
  1571. * acquire tlock of the leaf page containing original entry
  1572. */
  1573. if (!test_cflag(COMMIT_Nolink, ip)) {
  1574. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1575. xtlck = (struct xtlock *) & tlck->lock;
  1576. }
  1577. /* completely replace extent ? */
  1578. xad = &p->xad[index];
  1579. /*
  1580. printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
  1581. (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
  1582. */
  1583. if ((llen = xoff - offsetXAD(xad)) == 0)
  1584. goto updateOld;
  1585. /*
  1586. * partially replace extent: insert entry for new extent
  1587. */
  1588. //insertNew:
  1589. /*
  1590. * if the leaf page is full, insert the new entry and
  1591. * propagate up the router entry for the new page from split
  1592. *
  1593. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1594. */
  1595. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1596. /* xtSpliUp() unpins leaf pages */
  1597. split.mp = mp;
  1598. split.index = index + 1;
  1599. split.flag = XAD_NEW;
  1600. split.off = xoff; /* split offset */
  1601. split.len = xlen;
  1602. split.addr = xaddr;
  1603. split.pxdlist = NULL;
  1604. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1605. return rc;
  1606. /* get back old page */
  1607. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1608. if (rc)
  1609. return rc;
  1610. /*
  1611. * if leaf root has been split, original root has been
  1612. * copied to new child page, i.e., original entry now
  1613. * resides on the new child page;
  1614. */
  1615. if (p->header.flag & BT_INTERNAL) {
  1616. ASSERT(p->header.nextindex ==
  1617. cpu_to_le16(XTENTRYSTART + 1));
  1618. xad = &p->xad[XTENTRYSTART];
  1619. bn = addressXAD(xad);
  1620. XT_PUTPAGE(mp);
  1621. /* get new child page */
  1622. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1623. if (rc)
  1624. return rc;
  1625. BT_MARK_DIRTY(mp, ip);
  1626. if (!test_cflag(COMMIT_Nolink, ip)) {
  1627. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1628. xtlck = (struct xtlock *) & tlck->lock;
  1629. }
  1630. }
  1631. }
  1632. /*
  1633. * insert the new entry into the leaf page
  1634. */
  1635. else {
  1636. /* insert the new entry: mark the entry NEW */
  1637. xad = &p->xad[index + 1];
  1638. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1639. /* advance next available entry index */
  1640. p->header.nextindex =
  1641. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1642. }
  1643. /* get back old XAD */
  1644. xad = &p->xad[index];
  1645. /*
  1646. * truncate/relocate old extent at split offset
  1647. */
  1648. updateOld:
  1649. /* update dmap for old/committed/truncated extent */
  1650. rlen = lengthXAD(xad) - llen;
  1651. if (!(xad->flag & XAD_NEW)) {
  1652. /* free from PWMAP at commit */
  1653. if (!test_cflag(COMMIT_Nolink, ip)) {
  1654. mtlck = txMaplock(tid, ip, tlckMAP);
  1655. pxdlock = (struct maplock *) & mtlck->lock;
  1656. pxdlock->flag = mlckFREEPXD;
  1657. PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
  1658. PXDlength(&pxdlock->pxd, rlen);
  1659. pxdlock->index = 1;
  1660. }
  1661. } else
  1662. /* free from WMAP */
  1663. dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
  1664. if (llen)
  1665. /* truncate */
  1666. XADlength(xad, llen);
  1667. else
  1668. /* replace */
  1669. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1670. if (!test_cflag(COMMIT_Nolink, ip)) {
  1671. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1672. min(index, (int)xtlck->lwm.offset) : index;
  1673. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  1674. xtlck->lwm.offset;
  1675. }
  1676. /* unpin the leaf page */
  1677. XT_PUTPAGE(mp);
  1678. return rc;
  1679. }
  1680. #endif /* _NOTYET */
  1681. /*
  1682. * xtUpdate()
  1683. *
  1684. * function: update XAD;
  1685. *
  1686. * update extent for allocated_but_not_recorded or
  1687. * compressed extent;
  1688. *
  1689. * parameter:
  1690. * nxad - new XAD;
  1691. * logical extent of the specified XAD must be completely
  1692. * contained by an existing XAD;
  1693. */
  1694. int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
  1695. { /* new XAD */
  1696. int rc = 0;
  1697. int cmp;
  1698. struct metapage *mp; /* meta-page buffer */
  1699. xtpage_t *p; /* base B+-tree index page */
  1700. s64 bn;
  1701. int index0, index, newindex, nextindex;
  1702. struct btstack btstack; /* traverse stack */
  1703. struct xtsplit split; /* split information */
  1704. xad_t *xad, *lxad, *rxad;
  1705. int xflag;
  1706. s64 nxoff, xoff;
  1707. int nxlen, xlen, lxlen, rxlen;
  1708. s64 nxaddr, xaddr;
  1709. struct tlock *tlck;
  1710. struct xtlock *xtlck = NULL;
  1711. int newpage = 0;
  1712. /* there must exist extent to be tailgated */
  1713. nxoff = offsetXAD(nxad);
  1714. nxlen = lengthXAD(nxad);
  1715. nxaddr = addressXAD(nxad);
  1716. if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
  1717. return rc;
  1718. /* retrieve search result */
  1719. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1720. if (cmp != 0) {
  1721. XT_PUTPAGE(mp);
  1722. jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
  1723. return -EIO;
  1724. }
  1725. BT_MARK_DIRTY(mp, ip);
  1726. /*
  1727. * acquire tlock of the leaf page containing original entry
  1728. */
  1729. if (!test_cflag(COMMIT_Nolink, ip)) {
  1730. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1731. xtlck = (struct xtlock *) & tlck->lock;
  1732. }
  1733. xad = &p->xad[index0];
  1734. xflag = xad->flag;
  1735. xoff = offsetXAD(xad);
  1736. xlen = lengthXAD(xad);
  1737. xaddr = addressXAD(xad);
  1738. /* nXAD must be completely contained within XAD */
  1739. if ((xoff > nxoff) ||
  1740. (nxoff + nxlen > xoff + xlen)) {
  1741. XT_PUTPAGE(mp);
  1742. jfs_error(ip->i_sb,
  1743. "xtUpdate: nXAD in not completely contained within XAD");
  1744. return -EIO;
  1745. }
  1746. index = index0;
  1747. newindex = index + 1;
  1748. nextindex = le16_to_cpu(p->header.nextindex);
  1749. #ifdef _JFS_WIP_NOCOALESCE
  1750. if (xoff < nxoff)
  1751. goto updateRight;
  1752. /*
  1753. * replace XAD with nXAD
  1754. */
  1755. replace: /* (nxoff == xoff) */
  1756. if (nxlen == xlen) {
  1757. /* replace XAD with nXAD:recorded */
  1758. *xad = *nxad;
  1759. xad->flag = xflag & ~XAD_NOTRECORDED;
  1760. goto out;
  1761. } else /* (nxlen < xlen) */
  1762. goto updateLeft;
  1763. #endif /* _JFS_WIP_NOCOALESCE */
  1764. /* #ifdef _JFS_WIP_COALESCE */
  1765. if (xoff < nxoff)
  1766. goto coalesceRight;
  1767. /*
  1768. * coalesce with left XAD
  1769. */
  1770. //coalesceLeft: /* (xoff == nxoff) */
  1771. /* is XAD first entry of page ? */
  1772. if (index == XTENTRYSTART)
  1773. goto replace;
  1774. /* is nXAD logically and physically contiguous with lXAD ? */
  1775. lxad = &p->xad[index - 1];
  1776. lxlen = lengthXAD(lxad);
  1777. if (!(lxad->flag & XAD_NOTRECORDED) &&
  1778. (nxoff == offsetXAD(lxad) + lxlen) &&
  1779. (nxaddr == addressXAD(lxad) + lxlen) &&
  1780. (lxlen + nxlen < MAXXLEN)) {
  1781. /* extend right lXAD */
  1782. index0 = index - 1;
  1783. XADlength(lxad, lxlen + nxlen);
  1784. /* If we just merged two extents together, need to make sure the
  1785. * right extent gets logged. If the left one is marked XAD_NEW,
  1786. * then we know it will be logged. Otherwise, mark as
  1787. * XAD_EXTENDED
  1788. */
  1789. if (!(lxad->flag & XAD_NEW))
  1790. lxad->flag |= XAD_EXTENDED;
  1791. if (xlen > nxlen) {
  1792. /* truncate XAD */
  1793. XADoffset(xad, xoff + nxlen);
  1794. XADlength(xad, xlen - nxlen);
  1795. XADaddress(xad, xaddr + nxlen);
  1796. goto out;
  1797. } else { /* (xlen == nxlen) */
  1798. /* remove XAD */
  1799. if (index < nextindex - 1)
  1800. memmove(&p->xad[index], &p->xad[index + 1],
  1801. (nextindex - index -
  1802. 1) << L2XTSLOTSIZE);
  1803. p->header.nextindex =
  1804. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1805. 1);
  1806. index = index0;
  1807. newindex = index + 1;
  1808. nextindex = le16_to_cpu(p->header.nextindex);
  1809. xoff = nxoff = offsetXAD(lxad);
  1810. xlen = nxlen = lxlen + nxlen;
  1811. xaddr = nxaddr = addressXAD(lxad);
  1812. goto coalesceRight;
  1813. }
  1814. }
  1815. /*
  1816. * replace XAD with nXAD
  1817. */
  1818. replace: /* (nxoff == xoff) */
  1819. if (nxlen == xlen) {
  1820. /* replace XAD with nXAD:recorded */
  1821. *xad = *nxad;
  1822. xad->flag = xflag & ~XAD_NOTRECORDED;
  1823. goto coalesceRight;
  1824. } else /* (nxlen < xlen) */
  1825. goto updateLeft;
  1826. /*
  1827. * coalesce with right XAD
  1828. */
  1829. coalesceRight: /* (xoff <= nxoff) */
  1830. /* is XAD last entry of page ? */
  1831. if (newindex == nextindex) {
  1832. if (xoff == nxoff)
  1833. goto out;
  1834. goto updateRight;
  1835. }
  1836. /* is nXAD logically and physically contiguous with rXAD ? */
  1837. rxad = &p->xad[index + 1];
  1838. rxlen = lengthXAD(rxad);
  1839. if (!(rxad->flag & XAD_NOTRECORDED) &&
  1840. (nxoff + nxlen == offsetXAD(rxad)) &&
  1841. (nxaddr + nxlen == addressXAD(rxad)) &&
  1842. (rxlen + nxlen < MAXXLEN)) {
  1843. /* extend left rXAD */
  1844. XADoffset(rxad, nxoff);
  1845. XADlength(rxad, rxlen + nxlen);
  1846. XADaddress(rxad, nxaddr);
  1847. /* If we just merged two extents together, need to make sure
  1848. * the left extent gets logged. If the right one is marked
  1849. * XAD_NEW, then we know it will be logged. Otherwise, mark as
  1850. * XAD_EXTENDED
  1851. */
  1852. if (!(rxad->flag & XAD_NEW))
  1853. rxad->flag |= XAD_EXTENDED;
  1854. if (xlen > nxlen)
  1855. /* truncate XAD */
  1856. XADlength(xad, xlen - nxlen);
  1857. else { /* (xlen == nxlen) */
  1858. /* remove XAD */
  1859. memmove(&p->xad[index], &p->xad[index + 1],
  1860. (nextindex - index - 1) << L2XTSLOTSIZE);
  1861. p->header.nextindex =
  1862. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1863. 1);
  1864. }
  1865. goto out;
  1866. } else if (xoff == nxoff)
  1867. goto out;
  1868. if (xoff >= nxoff) {
  1869. XT_PUTPAGE(mp);
  1870. jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
  1871. return -EIO;
  1872. }
  1873. /* #endif _JFS_WIP_COALESCE */
  1874. /*
  1875. * split XAD into (lXAD, nXAD):
  1876. *
  1877. * |---nXAD--->
  1878. * --|----------XAD----------|--
  1879. * |-lXAD-|
  1880. */
  1881. updateRight: /* (xoff < nxoff) */
  1882. /* truncate old XAD as lXAD:not_recorded */
  1883. xad = &p->xad[index];
  1884. XADlength(xad, nxoff - xoff);
  1885. /* insert nXAD:recorded */
  1886. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1887. /* xtSpliUp() unpins leaf pages */
  1888. split.mp = mp;
  1889. split.index = newindex;
  1890. split.flag = xflag & ~XAD_NOTRECORDED;
  1891. split.off = nxoff;
  1892. split.len = nxlen;
  1893. split.addr = nxaddr;
  1894. split.pxdlist = NULL;
  1895. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1896. return rc;
  1897. /* get back old page */
  1898. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1899. if (rc)
  1900. return rc;
  1901. /*
  1902. * if leaf root has been split, original root has been
  1903. * copied to new child page, i.e., original entry now
  1904. * resides on the new child page;
  1905. */
  1906. if (p->header.flag & BT_INTERNAL) {
  1907. ASSERT(p->header.nextindex ==
  1908. cpu_to_le16(XTENTRYSTART + 1));
  1909. xad = &p->xad[XTENTRYSTART];
  1910. bn = addressXAD(xad);
  1911. XT_PUTPAGE(mp);
  1912. /* get new child page */
  1913. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1914. if (rc)
  1915. return rc;
  1916. BT_MARK_DIRTY(mp, ip);
  1917. if (!test_cflag(COMMIT_Nolink, ip)) {
  1918. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1919. xtlck = (struct xtlock *) & tlck->lock;
  1920. }
  1921. } else {
  1922. /* is nXAD on new page ? */
  1923. if (newindex >
  1924. (le16_to_cpu(p->header.maxentry) >> 1)) {
  1925. newindex =
  1926. newindex -
  1927. le16_to_cpu(p->header.nextindex) +
  1928. XTENTRYSTART;
  1929. newpage = 1;
  1930. }
  1931. }
  1932. } else {
  1933. /* if insert into middle, shift right remaining entries */
  1934. if (newindex < nextindex)
  1935. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  1936. (nextindex - newindex) << L2XTSLOTSIZE);
  1937. /* insert the entry */
  1938. xad = &p->xad[newindex];
  1939. *xad = *nxad;
  1940. xad->flag = xflag & ~XAD_NOTRECORDED;
  1941. /* advance next available entry index. */
  1942. p->header.nextindex =
  1943. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1944. }
  1945. /*
  1946. * does nXAD force 3-way split ?
  1947. *
  1948. * |---nXAD--->|
  1949. * --|----------XAD-------------|--
  1950. * |-lXAD-| |-rXAD -|
  1951. */
  1952. if (nxoff + nxlen == xoff + xlen)
  1953. goto out;
  1954. /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
  1955. if (newpage) {
  1956. /* close out old page */
  1957. if (!test_cflag(COMMIT_Nolink, ip)) {
  1958. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1959. min(index0, (int)xtlck->lwm.offset) : index0;
  1960. xtlck->lwm.length =
  1961. le16_to_cpu(p->header.nextindex) -
  1962. xtlck->lwm.offset;
  1963. }
  1964. bn = le64_to_cpu(p->header.next);
  1965. XT_PUTPAGE(mp);
  1966. /* get new right page */
  1967. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1968. if (rc)
  1969. return rc;
  1970. BT_MARK_DIRTY(mp, ip);
  1971. if (!test_cflag(COMMIT_Nolink, ip)) {
  1972. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1973. xtlck = (struct xtlock *) & tlck->lock;
  1974. }
  1975. index0 = index = newindex;
  1976. } else
  1977. index++;
  1978. newindex = index + 1;
  1979. nextindex = le16_to_cpu(p->header.nextindex);
  1980. xlen = xlen - (nxoff - xoff);
  1981. xoff = nxoff;
  1982. xaddr = nxaddr;
  1983. /* recompute split pages */
  1984. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1985. XT_PUTPAGE(mp);
  1986. if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
  1987. return rc;
  1988. /* retrieve search result */
  1989. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1990. if (cmp != 0) {
  1991. XT_PUTPAGE(mp);
  1992. jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
  1993. return -EIO;
  1994. }
  1995. if (index0 != index) {
  1996. XT_PUTPAGE(mp);
  1997. jfs_error(ip->i_sb,
  1998. "xtUpdate: unexpected value of index");
  1999. return -EIO;
  2000. }
  2001. }
  2002. /*
  2003. * split XAD into (nXAD, rXAD)
  2004. *
  2005. * ---nXAD---|
  2006. * --|----------XAD----------|--
  2007. * |-rXAD-|
  2008. */
  2009. updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
  2010. /* update old XAD with nXAD:recorded */
  2011. xad = &p->xad[index];
  2012. *xad = *nxad;
  2013. xad->flag = xflag & ~XAD_NOTRECORDED;
  2014. /* insert rXAD:not_recorded */
  2015. xoff = xoff + nxlen;
  2016. xlen = xlen - nxlen;
  2017. xaddr = xaddr + nxlen;
  2018. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  2019. /*
  2020. printf("xtUpdate.updateLeft.split p:0x%p\n", p);
  2021. */
  2022. /* xtSpliUp() unpins leaf pages */
  2023. split.mp = mp;
  2024. split.index = newindex;
  2025. split.flag = xflag;
  2026. split.off = xoff;
  2027. split.len = xlen;
  2028. split.addr = xaddr;
  2029. split.pxdlist = NULL;
  2030. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  2031. return rc;
  2032. /* get back old page */
  2033. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2034. if (rc)
  2035. return rc;
  2036. /*
  2037. * if leaf root has been split, original root has been
  2038. * copied to new child page, i.e., original entry now
  2039. * resides on the new child page;
  2040. */
  2041. if (p->header.flag & BT_INTERNAL) {
  2042. ASSERT(p->header.nextindex ==
  2043. cpu_to_le16(XTENTRYSTART + 1));
  2044. xad = &p->xad[XTENTRYSTART];
  2045. bn = addressXAD(xad);
  2046. XT_PUTPAGE(mp);
  2047. /* get new child page */
  2048. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2049. if (rc)
  2050. return rc;
  2051. BT_MARK_DIRTY(mp, ip);
  2052. if (!test_cflag(COMMIT_Nolink, ip)) {
  2053. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  2054. xtlck = (struct xtlock *) & tlck->lock;
  2055. }
  2056. }
  2057. } else {
  2058. /* if insert into middle, shift right remaining entries */
  2059. if (newindex < nextindex)
  2060. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  2061. (nextindex - newindex) << L2XTSLOTSIZE);
  2062. /* insert the entry */
  2063. xad = &p->xad[newindex];
  2064. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  2065. /* advance next available entry index. */
  2066. p->header.nextindex =
  2067. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  2068. }
  2069. out:
  2070. if (!test_cflag(COMMIT_Nolink, ip)) {
  2071. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  2072. min(index0, (int)xtlck->lwm.offset) : index0;
  2073. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  2074. xtlck->lwm.offset;
  2075. }
  2076. /* unpin the leaf page */
  2077. XT_PUTPAGE(mp);
  2078. return rc;
  2079. }
  2080. /*
  2081. * xtAppend()
  2082. *
  2083. * function: grow in append mode from contiguous region specified ;
  2084. *
  2085. * parameter:
  2086. * tid - transaction id;
  2087. * ip - file object;
  2088. * xflag - extent flag:
  2089. * xoff - extent offset;
  2090. * maxblocks - max extent length;
  2091. * xlen - extent length (in/out);
  2092. * xaddrp - extent address pointer (in/out):
  2093. * flag -
  2094. *
  2095. * return:
  2096. */
  2097. int xtAppend(tid_t tid, /* transaction id */
  2098. struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
  2099. s32 * xlenp, /* (in/out) */
  2100. s64 * xaddrp, /* (in/out) */
  2101. int flag)
  2102. {
  2103. int rc = 0;
  2104. struct metapage *mp; /* meta-page buffer */
  2105. xtpage_t *p; /* base B+-tree index page */
  2106. s64 bn, xaddr;
  2107. int index, nextindex;
  2108. struct btstack btstack; /* traverse stack */
  2109. struct xtsplit split; /* split information */
  2110. xad_t *xad;
  2111. int cmp;
  2112. struct tlock *tlck;
  2113. struct xtlock *xtlck;
  2114. int nsplit, nblocks, xlen;
  2115. struct pxdlist pxdlist;
  2116. pxd_t *pxd;
  2117. xaddr = *xaddrp;
  2118. xlen = *xlenp;
  2119. jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
  2120. (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
  2121. /*
  2122. * search for the entry location at which to insert:
  2123. *
  2124. * xtFastSearch() and xtSearch() both returns (leaf page
  2125. * pinned, index at which to insert).
  2126. * n.b. xtSearch() may return index of maxentry of
  2127. * the full page.
  2128. */
  2129. if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
  2130. return rc;
  2131. /* retrieve search result */
  2132. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2133. if (cmp == 0) {
  2134. rc = -EEXIST;
  2135. goto out;
  2136. }
  2137. //insert:
  2138. /*
  2139. * insert entry for new extent
  2140. */
  2141. xflag |= XAD_NEW;
  2142. /*
  2143. * if the leaf page is full, split the page and
  2144. * propagate up the router entry for the new page from split
  2145. *
  2146. * The xtSplitUp() will insert the entry and unpin the leaf page.
  2147. */
  2148. nextindex = le16_to_cpu(p->header.nextindex);
  2149. if (nextindex < le16_to_cpu(p->header.maxentry))
  2150. goto insertLeaf;
  2151. /*
  2152. * allocate new index blocks to cover index page split(s)
  2153. */
  2154. nsplit = btstack.nsplit;
  2155. split.pxdlist = &pxdlist;
  2156. pxdlist.maxnpxd = pxdlist.npxd = 0;
  2157. pxd = &pxdlist.pxd[0];
  2158. nblocks = JFS_SBI(ip->i_sb)->nbperpage;
  2159. for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
  2160. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
  2161. PXDaddress(pxd, xaddr);
  2162. PXDlength(pxd, nblocks);
  2163. pxdlist.maxnpxd++;
  2164. continue;
  2165. }
  2166. /* undo allocation */
  2167. goto out;
  2168. }
  2169. xlen = min(xlen, maxblocks);
  2170. /*
  2171. * allocate data extent requested
  2172. */
  2173. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  2174. goto out;
  2175. split.mp = mp;
  2176. split.index = index;
  2177. split.flag = xflag;
  2178. split.off = xoff;
  2179. split.len = xlen;
  2180. split.addr = xaddr;
  2181. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  2182. /* undo data extent allocation */
  2183. dbFree(ip, *xaddrp, (s64) * xlenp);
  2184. return rc;
  2185. }
  2186. *xaddrp = xaddr;
  2187. *xlenp = xlen;
  2188. return 0;
  2189. /*
  2190. * insert the new entry into the leaf page
  2191. */
  2192. insertLeaf:
  2193. /*
  2194. * allocate data extent requested
  2195. */
  2196. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  2197. goto out;
  2198. BT_MARK_DIRTY(mp, ip);
  2199. /*
  2200. * acquire a transaction lock on the leaf page;
  2201. *
  2202. * action: xad insertion/extension;
  2203. */
  2204. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  2205. xtlck = (struct xtlock *) & tlck->lock;
  2206. /* insert the new entry: mark the entry NEW */
  2207. xad = &p->xad[index];
  2208. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  2209. /* advance next available entry index */
  2210. p->header.nextindex =
  2211. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  2212. xtlck->lwm.offset =
  2213. (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
  2214. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  2215. xtlck->lwm.offset;
  2216. *xaddrp = xaddr;
  2217. *xlenp = xlen;
  2218. out:
  2219. /* unpin the leaf page */
  2220. XT_PUTPAGE(mp);
  2221. return rc;
  2222. }
  2223. #ifdef _STILL_TO_PORT
  2224. /* - TBD for defragmentaion/reorganization -
  2225. *
  2226. * xtDelete()
  2227. *
  2228. * function:
  2229. * delete the entry with the specified key.
  2230. *
  2231. * N.B.: whole extent of the entry is assumed to be deleted.
  2232. *
  2233. * parameter:
  2234. *
  2235. * return:
  2236. * ENOENT: if the entry is not found.
  2237. *
  2238. * exception:
  2239. */
  2240. int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
  2241. {
  2242. int rc = 0;
  2243. struct btstack btstack;
  2244. int cmp;
  2245. s64 bn;
  2246. struct metapage *mp;
  2247. xtpage_t *p;
  2248. int index, nextindex;
  2249. struct tlock *tlck;
  2250. struct xtlock *xtlck;
  2251. /*
  2252. * find the matching entry; xtSearch() pins the page
  2253. */
  2254. if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
  2255. return rc;
  2256. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2257. if (cmp) {
  2258. /* unpin the leaf page */
  2259. XT_PUTPAGE(mp);
  2260. return -ENOENT;
  2261. }
  2262. /*
  2263. * delete the entry from the leaf page
  2264. */
  2265. nextindex = le16_to_cpu(p->header.nextindex);
  2266. p->header.nextindex =
  2267. cpu_to_le16(le16_to_cpu(p->header.nextindex) - 1);
  2268. /*
  2269. * if the leaf page bocome empty, free the page
  2270. */
  2271. if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
  2272. return (xtDeleteUp(tid, ip, mp, p, &btstack));
  2273. BT_MARK_DIRTY(mp, ip);
  2274. /*
  2275. * acquire a transaction lock on the leaf page;
  2276. *
  2277. * action:xad deletion;
  2278. */
  2279. tlck = txLock(tid, ip, mp, tlckXTREE);
  2280. xtlck = (struct xtlock *) & tlck->lock;
  2281. xtlck->lwm.offset =
  2282. (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
  2283. /* if delete from middle, shift left/compact the remaining entries */
  2284. if (index < nextindex - 1)
  2285. memmove(&p->xad[index], &p->xad[index + 1],
  2286. (nextindex - index - 1) * sizeof(xad_t));
  2287. XT_PUTPAGE(mp);
  2288. return 0;
  2289. }
  2290. /* - TBD for defragmentaion/reorganization -
  2291. *
  2292. * xtDeleteUp()
  2293. *
  2294. * function:
  2295. * free empty pages as propagating deletion up the tree
  2296. *
  2297. * parameter:
  2298. *
  2299. * return:
  2300. */
  2301. static int
  2302. xtDeleteUp(tid_t tid, struct inode *ip,
  2303. struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
  2304. {
  2305. int rc = 0;
  2306. struct metapage *mp;
  2307. xtpage_t *p;
  2308. int index, nextindex;
  2309. s64 xaddr;
  2310. int xlen;
  2311. struct btframe *parent;
  2312. struct tlock *tlck;
  2313. struct xtlock *xtlck;
  2314. /*
  2315. * keep root leaf page which has become empty
  2316. */
  2317. if (fp->header.flag & BT_ROOT) {
  2318. /* keep the root page */
  2319. fp->header.flag &= ~BT_INTERNAL;
  2320. fp->header.flag |= BT_LEAF;
  2321. fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2322. /* XT_PUTPAGE(fmp); */
  2323. return 0;
  2324. }
  2325. /*
  2326. * free non-root leaf page
  2327. */
  2328. if ((rc = xtRelink(tid, ip, fp))) {
  2329. XT_PUTPAGE(fmp);
  2330. return rc;
  2331. }
  2332. xaddr = addressPXD(&fp->header.self);
  2333. xlen = lengthPXD(&fp->header.self);
  2334. /* free the page extent */
  2335. dbFree(ip, xaddr, (s64) xlen);
  2336. /* free the buffer page */
  2337. discard_metapage(fmp);
  2338. /*
  2339. * propagate page deletion up the index tree
  2340. *
  2341. * If the delete from the parent page makes it empty,
  2342. * continue all the way up the tree.
  2343. * stop if the root page is reached (which is never deleted) or
  2344. * if the entry deletion does not empty the page.
  2345. */
  2346. while ((parent = BT_POP(btstack)) != NULL) {
  2347. /* get/pin the parent page <sp> */
  2348. XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
  2349. if (rc)
  2350. return rc;
  2351. index = parent->index;
  2352. /* delete the entry for the freed child page from parent.
  2353. */
  2354. nextindex = le16_to_cpu(p->header.nextindex);
  2355. /*
  2356. * the parent has the single entry being deleted:
  2357. * free the parent page which has become empty.
  2358. */
  2359. if (nextindex == 1) {
  2360. if (p->header.flag & BT_ROOT) {
  2361. /* keep the root page */
  2362. p->header.flag &= ~BT_INTERNAL;
  2363. p->header.flag |= BT_LEAF;
  2364. p->header.nextindex =
  2365. cpu_to_le16(XTENTRYSTART);
  2366. /* XT_PUTPAGE(mp); */
  2367. break;
  2368. } else {
  2369. /* free the parent page */
  2370. if ((rc = xtRelink(tid, ip, p)))
  2371. return rc;
  2372. xaddr = addressPXD(&p->header.self);
  2373. /* free the page extent */
  2374. dbFree(ip, xaddr,
  2375. (s64) JFS_SBI(ip->i_sb)->nbperpage);
  2376. /* unpin/free the buffer page */
  2377. discard_metapage(mp);
  2378. /* propagate up */
  2379. continue;
  2380. }
  2381. }
  2382. /*
  2383. * the parent has other entries remaining:
  2384. * delete the router entry from the parent page.
  2385. */
  2386. else {
  2387. BT_MARK_DIRTY(mp, ip);
  2388. /*
  2389. * acquire a transaction lock on the leaf page;
  2390. *
  2391. * action:xad deletion;
  2392. */
  2393. tlck = txLock(tid, ip, mp, tlckXTREE);
  2394. xtlck = (struct xtlock *) & tlck->lock;
  2395. xtlck->lwm.offset =
  2396. (xtlck->lwm.offset) ? min(index,
  2397. xtlck->lwm.
  2398. offset) : index;
  2399. /* if delete from middle,
  2400. * shift left/compact the remaining entries in the page
  2401. */
  2402. if (index < nextindex - 1)
  2403. memmove(&p->xad[index], &p->xad[index + 1],
  2404. (nextindex - index -
  2405. 1) << L2XTSLOTSIZE);
  2406. p->header.nextindex =
  2407. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  2408. 1);
  2409. jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
  2410. (ulong) parent->bn, index);
  2411. }
  2412. /* unpin the parent page */
  2413. XT_PUTPAGE(mp);
  2414. /* exit propagation up */
  2415. break;
  2416. }
  2417. return 0;
  2418. }
  2419. /*
  2420. * NAME: xtRelocate()
  2421. *
  2422. * FUNCTION: relocate xtpage or data extent of regular file;
  2423. * This function is mainly used by defragfs utility.
  2424. *
  2425. * NOTE: This routine does not have the logic to handle
  2426. * uncommitted allocated extent. The caller should call
  2427. * txCommit() to commit all the allocation before call
  2428. * this routine.
  2429. */
  2430. int
  2431. xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
  2432. s64 nxaddr, /* new xaddr */
  2433. int xtype)
  2434. { /* extent type: XTPAGE or DATAEXT */
  2435. int rc = 0;
  2436. struct tblock *tblk;
  2437. struct tlock *tlck;
  2438. struct xtlock *xtlck;
  2439. struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
  2440. xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
  2441. xad_t *xad;
  2442. pxd_t *pxd;
  2443. s64 xoff, xsize;
  2444. int xlen;
  2445. s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
  2446. cbuf_t *cp;
  2447. s64 offset, nbytes, nbrd, pno;
  2448. int nb, npages, nblks;
  2449. s64 bn;
  2450. int cmp;
  2451. int index;
  2452. struct pxd_lock *pxdlock;
  2453. struct btstack btstack; /* traverse stack */
  2454. xtype = xtype & EXTENT_TYPE;
  2455. xoff = offsetXAD(oxad);
  2456. oxaddr = addressXAD(oxad);
  2457. xlen = lengthXAD(oxad);
  2458. /* validate extent offset */
  2459. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2460. if (offset >= ip->i_size)
  2461. return -ESTALE; /* stale extent */
  2462. jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
  2463. xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
  2464. /*
  2465. * 1. get and validate the parent xtpage/xad entry
  2466. * covering the source extent to be relocated;
  2467. */
  2468. if (xtype == DATAEXT) {
  2469. /* search in leaf entry */
  2470. rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
  2471. if (rc)
  2472. return rc;
  2473. /* retrieve search result */
  2474. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2475. if (cmp) {
  2476. XT_PUTPAGE(pmp);
  2477. return -ESTALE;
  2478. }
  2479. /* validate for exact match with a single entry */
  2480. xad = &pp->xad[index];
  2481. if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
  2482. XT_PUTPAGE(pmp);
  2483. return -ESTALE;
  2484. }
  2485. } else { /* (xtype == XTPAGE) */
  2486. /* search in internal entry */
  2487. rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
  2488. if (rc)
  2489. return rc;
  2490. /* retrieve search result */
  2491. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2492. if (cmp) {
  2493. XT_PUTPAGE(pmp);
  2494. return -ESTALE;
  2495. }
  2496. /* xtSearchNode() validated for exact match with a single entry
  2497. */
  2498. xad = &pp->xad[index];
  2499. }
  2500. jfs_info("xtRelocate: parent xad entry validated.");
  2501. /*
  2502. * 2. relocate the extent
  2503. */
  2504. if (xtype == DATAEXT) {
  2505. /* if the extent is allocated-but-not-recorded
  2506. * there is no real data to be moved in this extent,
  2507. */
  2508. if (xad->flag & XAD_NOTRECORDED)
  2509. goto out;
  2510. else
  2511. /* release xtpage for cmRead()/xtLookup() */
  2512. XT_PUTPAGE(pmp);
  2513. /*
  2514. * cmRelocate()
  2515. *
  2516. * copy target data pages to be relocated;
  2517. *
  2518. * data extent must start at page boundary and
  2519. * multiple of page size (except the last data extent);
  2520. * read in each page of the source data extent into cbuf,
  2521. * update the cbuf extent descriptor of the page to be
  2522. * homeward bound to new dst data extent
  2523. * copy the data from the old extent to new extent.
  2524. * copy is essential for compressed files to avoid problems
  2525. * that can arise if there was a change in compression
  2526. * algorithms.
  2527. * it is a good strategy because it may disrupt cache
  2528. * policy to keep the pages in memory afterwards.
  2529. */
  2530. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2531. assert((offset & CM_OFFSET) == 0);
  2532. nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2533. pno = offset >> CM_L2BSIZE;
  2534. npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
  2535. /*
  2536. npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
  2537. (offset >> CM_L2BSIZE) + 1;
  2538. */
  2539. sxaddr = oxaddr;
  2540. dxaddr = nxaddr;
  2541. /* process the request one cache buffer at a time */
  2542. for (nbrd = 0; nbrd < nbytes; nbrd += nb,
  2543. offset += nb, pno++, npages--) {
  2544. /* compute page size */
  2545. nb = min(nbytes - nbrd, CM_BSIZE);
  2546. /* get the cache buffer of the page */
  2547. if (rc = cmRead(ip, offset, npages, &cp))
  2548. break;
  2549. assert(addressPXD(&cp->cm_pxd) == sxaddr);
  2550. assert(!cp->cm_modified);
  2551. /* bind buffer with the new extent address */
  2552. nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
  2553. cmSetXD(ip, cp, pno, dxaddr, nblks);
  2554. /* release the cbuf, mark it as modified */
  2555. cmPut(cp, TRUE);
  2556. dxaddr += nblks;
  2557. sxaddr += nblks;
  2558. }
  2559. /* get back parent page */
  2560. if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
  2561. return rc;
  2562. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2563. jfs_info("xtRelocate: target data extent relocated.");
  2564. } else { /* (xtype == XTPAGE) */
  2565. /*
  2566. * read in the target xtpage from the source extent;
  2567. */
  2568. XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
  2569. if (rc) {
  2570. XT_PUTPAGE(pmp);
  2571. return rc;
  2572. }
  2573. /*
  2574. * read in sibling pages if any to update sibling pointers;
  2575. */
  2576. rmp = NULL;
  2577. if (p->header.next) {
  2578. nextbn = le64_to_cpu(p->header.next);
  2579. XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
  2580. if (rc) {
  2581. XT_PUTPAGE(pmp);
  2582. XT_PUTPAGE(mp);
  2583. return (rc);
  2584. }
  2585. }
  2586. lmp = NULL;
  2587. if (p->header.prev) {
  2588. prevbn = le64_to_cpu(p->header.prev);
  2589. XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
  2590. if (rc) {
  2591. XT_PUTPAGE(pmp);
  2592. XT_PUTPAGE(mp);
  2593. if (rmp)
  2594. XT_PUTPAGE(rmp);
  2595. return (rc);
  2596. }
  2597. }
  2598. /* at this point, all xtpages to be updated are in memory */
  2599. /*
  2600. * update sibling pointers of sibling xtpages if any;
  2601. */
  2602. if (lmp) {
  2603. BT_MARK_DIRTY(lmp, ip);
  2604. tlck =
  2605. txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
  2606. lp->header.next = cpu_to_le64(nxaddr);
  2607. XT_PUTPAGE(lmp);
  2608. }
  2609. if (rmp) {
  2610. BT_MARK_DIRTY(rmp, ip);
  2611. tlck =
  2612. txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
  2613. rp->header.prev = cpu_to_le64(nxaddr);
  2614. XT_PUTPAGE(rmp);
  2615. }
  2616. /*
  2617. * update the target xtpage to be relocated
  2618. *
  2619. * update the self address of the target page
  2620. * and write to destination extent;
  2621. * redo image covers the whole xtpage since it is new page
  2622. * to the destination extent;
  2623. * update of bmap for the free of source extent
  2624. * of the target xtpage itself:
  2625. * update of bmap for the allocation of destination extent
  2626. * of the target xtpage itself:
  2627. * update of bmap for the extents covered by xad entries in
  2628. * the target xtpage is not necessary since they are not
  2629. * updated;
  2630. * if not committed before this relocation,
  2631. * target page may contain XAD_NEW entries which must
  2632. * be scanned for bmap update (logredo() always
  2633. * scan xtpage REDOPAGE image for bmap update);
  2634. * if committed before this relocation (tlckRELOCATE),
  2635. * scan may be skipped by commit() and logredo();
  2636. */
  2637. BT_MARK_DIRTY(mp, ip);
  2638. /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
  2639. tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
  2640. xtlck = (struct xtlock *) & tlck->lock;
  2641. /* update the self address in the xtpage header */
  2642. pxd = &p->header.self;
  2643. PXDaddress(pxd, nxaddr);
  2644. /* linelock for the after image of the whole page */
  2645. xtlck->lwm.length =
  2646. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  2647. /* update the buffer extent descriptor of target xtpage */
  2648. xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2649. bmSetXD(mp, nxaddr, xsize);
  2650. /* unpin the target page to new homeward bound */
  2651. XT_PUTPAGE(mp);
  2652. jfs_info("xtRelocate: target xtpage relocated.");
  2653. }
  2654. /*
  2655. * 3. acquire maplock for the source extent to be freed;
  2656. *
  2657. * acquire a maplock saving the src relocated extent address;
  2658. * to free of the extent at commit time;
  2659. */
  2660. out:
  2661. /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
  2662. * free PXD of the source data extent (logredo() will update
  2663. * bmap for free of source data extent), and update bmap for
  2664. * free of the source data extent;
  2665. */
  2666. if (xtype == DATAEXT)
  2667. tlck = txMaplock(tid, ip, tlckMAP);
  2668. /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
  2669. * for the source xtpage (logredo() will init NoRedoPage
  2670. * filter and will also update bmap for free of the source
  2671. * xtpage), and update bmap for free of the source xtpage;
  2672. * N.B. We use tlckMAP instead of tlkcXTREE because there
  2673. * is no buffer associated with this lock since the buffer
  2674. * has been redirected to the target location.
  2675. */
  2676. else /* (xtype == XTPAGE) */
  2677. tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
  2678. pxdlock = (struct pxd_lock *) & tlck->lock;
  2679. pxdlock->flag = mlckFREEPXD;
  2680. PXDaddress(&pxdlock->pxd, oxaddr);
  2681. PXDlength(&pxdlock->pxd, xlen);
  2682. pxdlock->index = 1;
  2683. /*
  2684. * 4. update the parent xad entry for relocation;
  2685. *
  2686. * acquire tlck for the parent entry with XAD_NEW as entry
  2687. * update which will write LOG_REDOPAGE and update bmap for
  2688. * allocation of XAD_NEW destination extent;
  2689. */
  2690. jfs_info("xtRelocate: update parent xad entry.");
  2691. BT_MARK_DIRTY(pmp, ip);
  2692. tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
  2693. xtlck = (struct xtlock *) & tlck->lock;
  2694. /* update the XAD with the new destination extent; */
  2695. xad = &pp->xad[index];
  2696. xad->flag |= XAD_NEW;
  2697. XADaddress(xad, nxaddr);
  2698. xtlck->lwm.offset = min(index, xtlck->lwm.offset);
  2699. xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
  2700. xtlck->lwm.offset;
  2701. /* unpin the parent xtpage */
  2702. XT_PUTPAGE(pmp);
  2703. return rc;
  2704. }
  2705. /*
  2706. * xtSearchNode()
  2707. *
  2708. * function: search for the internal xad entry covering specified extent.
  2709. * This function is mainly used by defragfs utility.
  2710. *
  2711. * parameters:
  2712. * ip - file object;
  2713. * xad - extent to find;
  2714. * cmpp - comparison result:
  2715. * btstack - traverse stack;
  2716. * flag - search process flag;
  2717. *
  2718. * returns:
  2719. * btstack contains (bn, index) of search path traversed to the entry.
  2720. * *cmpp is set to result of comparison with the entry returned.
  2721. * the page containing the entry is pinned at exit.
  2722. */
  2723. static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
  2724. int *cmpp, struct btstack * btstack, int flag)
  2725. {
  2726. int rc = 0;
  2727. s64 xoff, xaddr;
  2728. int xlen;
  2729. int cmp = 1; /* init for empty page */
  2730. s64 bn; /* block number */
  2731. struct metapage *mp; /* meta-page buffer */
  2732. xtpage_t *p; /* page */
  2733. int base, index, lim;
  2734. struct btframe *btsp;
  2735. s64 t64;
  2736. BT_CLR(btstack);
  2737. xoff = offsetXAD(xad);
  2738. xlen = lengthXAD(xad);
  2739. xaddr = addressXAD(xad);
  2740. /*
  2741. * search down tree from root:
  2742. *
  2743. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  2744. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  2745. *
  2746. * if entry with search key K is not found
  2747. * internal page search find the entry with largest key Ki
  2748. * less than K which point to the child page to search;
  2749. * leaf page search find the entry with smallest key Kj
  2750. * greater than K so that the returned index is the position of
  2751. * the entry to be shifted right for insertion of new entry.
  2752. * for empty tree, search key is greater than any key of the tree.
  2753. *
  2754. * by convention, root bn = 0.
  2755. */
  2756. for (bn = 0;;) {
  2757. /* get/pin the page to search */
  2758. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2759. if (rc)
  2760. return rc;
  2761. if (p->header.flag & BT_LEAF) {
  2762. XT_PUTPAGE(mp);
  2763. return -ESTALE;
  2764. }
  2765. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  2766. /*
  2767. * binary search with search key K on the current page
  2768. */
  2769. for (base = XTENTRYSTART; lim; lim >>= 1) {
  2770. index = base + (lim >> 1);
  2771. XT_CMP(cmp, xoff, &p->xad[index], t64);
  2772. if (cmp == 0) {
  2773. /*
  2774. * search hit
  2775. *
  2776. * verify for exact match;
  2777. */
  2778. if (xaddr == addressXAD(&p->xad[index]) &&
  2779. xoff == offsetXAD(&p->xad[index])) {
  2780. *cmpp = cmp;
  2781. /* save search result */
  2782. btsp = btstack->top;
  2783. btsp->bn = bn;
  2784. btsp->index = index;
  2785. btsp->mp = mp;
  2786. return 0;
  2787. }
  2788. /* descend/search its child page */
  2789. goto next;
  2790. }
  2791. if (cmp > 0) {
  2792. base = index + 1;
  2793. --lim;
  2794. }
  2795. }
  2796. /*
  2797. * search miss - non-leaf page:
  2798. *
  2799. * base is the smallest index with key (Kj) greater than
  2800. * search key (K) and may be zero or maxentry index.
  2801. * if base is non-zero, decrement base by one to get the parent
  2802. * entry of the child page to search.
  2803. */
  2804. index = base ? base - 1 : base;
  2805. /*
  2806. * go down to child page
  2807. */
  2808. next:
  2809. /* get the child page block number */
  2810. bn = addressXAD(&p->xad[index]);
  2811. /* unpin the parent page */
  2812. XT_PUTPAGE(mp);
  2813. }
  2814. }
  2815. /*
  2816. * xtRelink()
  2817. *
  2818. * function:
  2819. * link around a freed page.
  2820. *
  2821. * Parameter:
  2822. * int tid,
  2823. * struct inode *ip,
  2824. * xtpage_t *p)
  2825. *
  2826. * returns:
  2827. */
  2828. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
  2829. {
  2830. int rc = 0;
  2831. struct metapage *mp;
  2832. s64 nextbn, prevbn;
  2833. struct tlock *tlck;
  2834. nextbn = le64_to_cpu(p->header.next);
  2835. prevbn = le64_to_cpu(p->header.prev);
  2836. /* update prev pointer of the next page */
  2837. if (nextbn != 0) {
  2838. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  2839. if (rc)
  2840. return rc;
  2841. /*
  2842. * acquire a transaction lock on the page;
  2843. *
  2844. * action: update prev pointer;
  2845. */
  2846. BT_MARK_DIRTY(mp, ip);
  2847. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2848. /* the page may already have been tlock'd */
  2849. p->header.prev = cpu_to_le64(prevbn);
  2850. XT_PUTPAGE(mp);
  2851. }
  2852. /* update next pointer of the previous page */
  2853. if (prevbn != 0) {
  2854. XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
  2855. if (rc)
  2856. return rc;
  2857. /*
  2858. * acquire a transaction lock on the page;
  2859. *
  2860. * action: update next pointer;
  2861. */
  2862. BT_MARK_DIRTY(mp, ip);
  2863. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2864. /* the page may already have been tlock'd */
  2865. p->header.next = le64_to_cpu(nextbn);
  2866. XT_PUTPAGE(mp);
  2867. }
  2868. return 0;
  2869. }
  2870. #endif /* _STILL_TO_PORT */
  2871. /*
  2872. * xtInitRoot()
  2873. *
  2874. * initialize file root (inline in inode)
  2875. */
  2876. void xtInitRoot(tid_t tid, struct inode *ip)
  2877. {
  2878. xtpage_t *p;
  2879. /*
  2880. * acquire a transaction lock on the root
  2881. *
  2882. * action:
  2883. */
  2884. txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
  2885. tlckXTREE | tlckNEW);
  2886. p = &JFS_IP(ip)->i_xtroot;
  2887. p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
  2888. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2889. if (S_ISDIR(ip->i_mode))
  2890. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
  2891. else {
  2892. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
  2893. ip->i_size = 0;
  2894. }
  2895. return;
  2896. }
  2897. /*
  2898. * We can run into a deadlock truncating a file with a large number of
  2899. * xtree pages (large fragmented file). A robust fix would entail a
  2900. * reservation system where we would reserve a number of metadata pages
  2901. * and tlocks which we would be guaranteed without a deadlock. Without
  2902. * this, a partial fix is to limit number of metadata pages we will lock
  2903. * in a single transaction. Currently we will truncate the file so that
  2904. * no more than 50 leaf pages will be locked. The caller of xtTruncate
  2905. * will be responsible for ensuring that the current transaction gets
  2906. * committed, and that subsequent transactions are created to truncate
  2907. * the file further if needed.
  2908. */
  2909. #define MAX_TRUNCATE_LEAVES 50
  2910. /*
  2911. * xtTruncate()
  2912. *
  2913. * function:
  2914. * traverse for truncation logging backward bottom up;
  2915. * terminate at the last extent entry at the current subtree
  2916. * root page covering new down size.
  2917. * truncation may occur within the last extent entry.
  2918. *
  2919. * parameter:
  2920. * int tid,
  2921. * struct inode *ip,
  2922. * s64 newsize,
  2923. * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
  2924. *
  2925. * return:
  2926. *
  2927. * note:
  2928. * PWMAP:
  2929. * 1. truncate (non-COMMIT_NOLINK file)
  2930. * by jfs_truncate() or jfs_open(O_TRUNC):
  2931. * xtree is updated;
  2932. * 2. truncate index table of directory when last entry removed
  2933. * map update via tlock at commit time;
  2934. * PMAP:
  2935. * Call xtTruncate_pmap instead
  2936. * WMAP:
  2937. * 1. remove (free zero link count) on last reference release
  2938. * (pmap has been freed at commit zero link count);
  2939. * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
  2940. * xtree is updated;
  2941. * map update directly at truncation time;
  2942. *
  2943. * if (DELETE)
  2944. * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
  2945. * else if (TRUNCATE)
  2946. * must write LOG_NOREDOPAGE for deleted index page;
  2947. *
  2948. * pages may already have been tlocked by anonymous transactions
  2949. * during file growth (i.e., write) before truncation;
  2950. *
  2951. * except last truncated entry, deleted entries remains as is
  2952. * in the page (nextindex is updated) for other use
  2953. * (e.g., log/update allocation map): this avoid copying the page
  2954. * info but delay free of pages;
  2955. *
  2956. */
  2957. s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
  2958. {
  2959. int rc = 0;
  2960. s64 teof;
  2961. struct metapage *mp;
  2962. xtpage_t *p;
  2963. s64 bn;
  2964. int index, nextindex;
  2965. xad_t *xad;
  2966. s64 xoff, xaddr;
  2967. int xlen, len, freexlen;
  2968. struct btstack btstack;
  2969. struct btframe *parent;
  2970. struct tblock *tblk = NULL;
  2971. struct tlock *tlck = NULL;
  2972. struct xtlock *xtlck = NULL;
  2973. struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
  2974. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  2975. s64 nfreed;
  2976. int freed, log;
  2977. int locked_leaves = 0;
  2978. /* save object truncation type */
  2979. if (tid) {
  2980. tblk = tid_to_tblock(tid);
  2981. tblk->xflag |= flag;
  2982. }
  2983. nfreed = 0;
  2984. flag &= COMMIT_MAP;
  2985. assert(flag != COMMIT_PMAP);
  2986. if (flag == COMMIT_PWMAP)
  2987. log = 1;
  2988. else {
  2989. log = 0;
  2990. xadlock.flag = mlckFREEXADLIST;
  2991. xadlock.index = 1;
  2992. }
  2993. /*
  2994. * if the newsize is not an integral number of pages,
  2995. * the file between newsize and next page boundary will
  2996. * be cleared.
  2997. * if truncating into a file hole, it will cause
  2998. * a full block to be allocated for the logical block.
  2999. */
  3000. /*
  3001. * release page blocks of truncated region <teof, eof>
  3002. *
  3003. * free the data blocks from the leaf index blocks.
  3004. * delete the parent index entries corresponding to
  3005. * the freed child data/index blocks.
  3006. * free the index blocks themselves which aren't needed
  3007. * in new sized file.
  3008. *
  3009. * index blocks are updated only if the blocks are to be
  3010. * retained in the new sized file.
  3011. * if type is PMAP, the data and index pages are NOT
  3012. * freed, and the data and index blocks are NOT freed
  3013. * from working map.
  3014. * (this will allow continued access of data/index of
  3015. * temporary file (zerolink count file truncated to zero-length)).
  3016. */
  3017. teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  3018. JFS_SBI(ip->i_sb)->l2bsize;
  3019. /* clear stack */
  3020. BT_CLR(&btstack);
  3021. /*
  3022. * start with root
  3023. *
  3024. * root resides in the inode
  3025. */
  3026. bn = 0;
  3027. /*
  3028. * first access of each page:
  3029. */
  3030. getPage:
  3031. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3032. if (rc)
  3033. return rc;
  3034. /* process entries backward from last index */
  3035. index = le16_to_cpu(p->header.nextindex) - 1;
  3036. if (p->header.flag & BT_INTERNAL)
  3037. goto getChild;
  3038. /*
  3039. * leaf page
  3040. */
  3041. /* Since this is the rightmost leaf, and we may have already freed
  3042. * a page that was formerly to the right, let's make sure that the
  3043. * next pointer is zero.
  3044. */
  3045. if (p->header.next) {
  3046. if (log)
  3047. /*
  3048. * Make sure this change to the header is logged.
  3049. * If we really truncate this leaf, the flag
  3050. * will be changed to tlckTRUNCATE
  3051. */
  3052. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  3053. BT_MARK_DIRTY(mp, ip);
  3054. p->header.next = 0;
  3055. }
  3056. freed = 0;
  3057. /* does region covered by leaf page precede Teof ? */
  3058. xad = &p->xad[index];
  3059. xoff = offsetXAD(xad);
  3060. xlen = lengthXAD(xad);
  3061. if (teof >= xoff + xlen) {
  3062. XT_PUTPAGE(mp);
  3063. goto getParent;
  3064. }
  3065. /* (re)acquire tlock of the leaf page */
  3066. if (log) {
  3067. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  3068. /*
  3069. * We need to limit the size of the transaction
  3070. * to avoid exhausting pagecache & tlocks
  3071. */
  3072. XT_PUTPAGE(mp);
  3073. newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  3074. goto getParent;
  3075. }
  3076. tlck = txLock(tid, ip, mp, tlckXTREE);
  3077. tlck->type = tlckXTREE | tlckTRUNCATE;
  3078. xtlck = (struct xtlock *) & tlck->lock;
  3079. xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
  3080. }
  3081. BT_MARK_DIRTY(mp, ip);
  3082. /*
  3083. * scan backward leaf page entries
  3084. */
  3085. for (; index >= XTENTRYSTART; index--) {
  3086. xad = &p->xad[index];
  3087. xoff = offsetXAD(xad);
  3088. xlen = lengthXAD(xad);
  3089. xaddr = addressXAD(xad);
  3090. /*
  3091. * The "data" for a directory is indexed by the block
  3092. * device's address space. This metadata must be invalidated
  3093. * here
  3094. */
  3095. if (S_ISDIR(ip->i_mode) && (teof == 0))
  3096. invalidate_xad_metapages(ip, *xad);
  3097. /*
  3098. * entry beyond eof: continue scan of current page
  3099. * xad
  3100. * ---|---=======------->
  3101. * eof
  3102. */
  3103. if (teof < xoff) {
  3104. nfreed += xlen;
  3105. continue;
  3106. }
  3107. /*
  3108. * (xoff <= teof): last entry to be deleted from page;
  3109. * If other entries remain in page: keep and update the page.
  3110. */
  3111. /*
  3112. * eof == entry_start: delete the entry
  3113. * xad
  3114. * -------|=======------->
  3115. * eof
  3116. *
  3117. */
  3118. if (teof == xoff) {
  3119. nfreed += xlen;
  3120. if (index == XTENTRYSTART)
  3121. break;
  3122. nextindex = index;
  3123. }
  3124. /*
  3125. * eof within the entry: truncate the entry.
  3126. * xad
  3127. * -------===|===------->
  3128. * eof
  3129. */
  3130. else if (teof < xoff + xlen) {
  3131. /* update truncated entry */
  3132. len = teof - xoff;
  3133. freexlen = xlen - len;
  3134. XADlength(xad, len);
  3135. /* save pxd of truncated extent in tlck */
  3136. xaddr += len;
  3137. if (log) { /* COMMIT_PWMAP */
  3138. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  3139. min(index, (int)xtlck->lwm.offset) : index;
  3140. xtlck->lwm.length = index + 1 -
  3141. xtlck->lwm.offset;
  3142. xtlck->twm.offset = index;
  3143. pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  3144. pxdlock->flag = mlckFREEPXD;
  3145. PXDaddress(&pxdlock->pxd, xaddr);
  3146. PXDlength(&pxdlock->pxd, freexlen);
  3147. }
  3148. /* free truncated extent */
  3149. else { /* COMMIT_WMAP */
  3150. pxdlock = (struct pxd_lock *) & xadlock;
  3151. pxdlock->flag = mlckFREEPXD;
  3152. PXDaddress(&pxdlock->pxd, xaddr);
  3153. PXDlength(&pxdlock->pxd, freexlen);
  3154. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  3155. /* reset map lock */
  3156. xadlock.flag = mlckFREEXADLIST;
  3157. }
  3158. /* current entry is new last entry; */
  3159. nextindex = index + 1;
  3160. nfreed += freexlen;
  3161. }
  3162. /*
  3163. * eof beyond the entry:
  3164. * xad
  3165. * -------=======---|--->
  3166. * eof
  3167. */
  3168. else { /* (xoff + xlen < teof) */
  3169. nextindex = index + 1;
  3170. }
  3171. if (nextindex < le16_to_cpu(p->header.nextindex)) {
  3172. if (!log) { /* COMMIT_WAMP */
  3173. xadlock.xdlist = &p->xad[nextindex];
  3174. xadlock.count =
  3175. le16_to_cpu(p->header.nextindex) -
  3176. nextindex;
  3177. txFreeMap(ip, (struct maplock *) & xadlock,
  3178. NULL, COMMIT_WMAP);
  3179. }
  3180. p->header.nextindex = cpu_to_le16(nextindex);
  3181. }
  3182. XT_PUTPAGE(mp);
  3183. /* assert(freed == 0); */
  3184. goto getParent;
  3185. } /* end scan of leaf page entries */
  3186. freed = 1;
  3187. /*
  3188. * leaf page become empty: free the page if type != PMAP
  3189. */
  3190. if (log) { /* COMMIT_PWMAP */
  3191. /* txCommit() with tlckFREE:
  3192. * free data extents covered by leaf [XTENTRYSTART:hwm);
  3193. * invalidate leaf if COMMIT_PWMAP;
  3194. * if (TRUNCATE), will write LOG_NOREDOPAGE;
  3195. */
  3196. tlck->type = tlckXTREE | tlckFREE;
  3197. } else { /* COMMIT_WAMP */
  3198. /* free data extents covered by leaf */
  3199. xadlock.xdlist = &p->xad[XTENTRYSTART];
  3200. xadlock.count =
  3201. le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  3202. txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
  3203. }
  3204. if (p->header.flag & BT_ROOT) {
  3205. p->header.flag &= ~BT_INTERNAL;
  3206. p->header.flag |= BT_LEAF;
  3207. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  3208. XT_PUTPAGE(mp); /* debug */
  3209. goto out;
  3210. } else {
  3211. if (log) { /* COMMIT_PWMAP */
  3212. /* page will be invalidated at tx completion
  3213. */
  3214. XT_PUTPAGE(mp);
  3215. } else { /* COMMIT_WMAP */
  3216. if (mp->lid)
  3217. lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
  3218. /* invalidate empty leaf page */
  3219. discard_metapage(mp);
  3220. }
  3221. }
  3222. /*
  3223. * the leaf page become empty: delete the parent entry
  3224. * for the leaf page if the parent page is to be kept
  3225. * in the new sized file.
  3226. */
  3227. /*
  3228. * go back up to the parent page
  3229. */
  3230. getParent:
  3231. /* pop/restore parent entry for the current child page */
  3232. if ((parent = BT_POP(&btstack)) == NULL)
  3233. /* current page must have been root */
  3234. goto out;
  3235. /* get back the parent page */
  3236. bn = parent->bn;
  3237. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3238. if (rc)
  3239. return rc;
  3240. index = parent->index;
  3241. /*
  3242. * child page was not empty:
  3243. */
  3244. if (freed == 0) {
  3245. /* has any entry deleted from parent ? */
  3246. if (index < le16_to_cpu(p->header.nextindex) - 1) {
  3247. /* (re)acquire tlock on the parent page */
  3248. if (log) { /* COMMIT_PWMAP */
  3249. /* txCommit() with tlckTRUNCATE:
  3250. * free child extents covered by parent [);
  3251. */
  3252. tlck = txLock(tid, ip, mp, tlckXTREE);
  3253. xtlck = (struct xtlock *) & tlck->lock;
  3254. if (!(tlck->type & tlckTRUNCATE)) {
  3255. xtlck->hwm.offset =
  3256. le16_to_cpu(p->header.
  3257. nextindex) - 1;
  3258. tlck->type =
  3259. tlckXTREE | tlckTRUNCATE;
  3260. }
  3261. } else { /* COMMIT_WMAP */
  3262. /* free child extents covered by parent */
  3263. xadlock.xdlist = &p->xad[index + 1];
  3264. xadlock.count =
  3265. le16_to_cpu(p->header.nextindex) -
  3266. index - 1;
  3267. txFreeMap(ip, (struct maplock *) & xadlock,
  3268. NULL, COMMIT_WMAP);
  3269. }
  3270. BT_MARK_DIRTY(mp, ip);
  3271. p->header.nextindex = cpu_to_le16(index + 1);
  3272. }
  3273. XT_PUTPAGE(mp);
  3274. goto getParent;
  3275. }
  3276. /*
  3277. * child page was empty:
  3278. */
  3279. nfreed += lengthXAD(&p->xad[index]);
  3280. /*
  3281. * During working map update, child page's tlock must be handled
  3282. * before parent's. This is because the parent's tlock will cause
  3283. * the child's disk space to be marked available in the wmap, so
  3284. * it's important that the child page be released by that time.
  3285. *
  3286. * ToDo: tlocks should be on doubly-linked list, so we can
  3287. * quickly remove it and add it to the end.
  3288. */
  3289. /*
  3290. * Move parent page's tlock to the end of the tid's tlock list
  3291. */
  3292. if (log && mp->lid && (tblk->last != mp->lid) &&
  3293. lid_to_tlock(mp->lid)->tid) {
  3294. lid_t lid = mp->lid;
  3295. struct tlock *prev;
  3296. tlck = lid_to_tlock(lid);
  3297. if (tblk->next == lid)
  3298. tblk->next = tlck->next;
  3299. else {
  3300. for (prev = lid_to_tlock(tblk->next);
  3301. prev->next != lid;
  3302. prev = lid_to_tlock(prev->next)) {
  3303. assert(prev->next);
  3304. }
  3305. prev->next = tlck->next;
  3306. }
  3307. lid_to_tlock(tblk->last)->next = lid;
  3308. tlck->next = 0;
  3309. tblk->last = lid;
  3310. }
  3311. /*
  3312. * parent page become empty: free the page
  3313. */
  3314. if (index == XTENTRYSTART) {
  3315. if (log) { /* COMMIT_PWMAP */
  3316. /* txCommit() with tlckFREE:
  3317. * free child extents covered by parent;
  3318. * invalidate parent if COMMIT_PWMAP;
  3319. */
  3320. tlck = txLock(tid, ip, mp, tlckXTREE);
  3321. xtlck = (struct xtlock *) & tlck->lock;
  3322. xtlck->hwm.offset =
  3323. le16_to_cpu(p->header.nextindex) - 1;
  3324. tlck->type = tlckXTREE | tlckFREE;
  3325. } else { /* COMMIT_WMAP */
  3326. /* free child extents covered by parent */
  3327. xadlock.xdlist = &p->xad[XTENTRYSTART];
  3328. xadlock.count =
  3329. le16_to_cpu(p->header.nextindex) -
  3330. XTENTRYSTART;
  3331. txFreeMap(ip, (struct maplock *) & xadlock, NULL,
  3332. COMMIT_WMAP);
  3333. }
  3334. BT_MARK_DIRTY(mp, ip);
  3335. if (p->header.flag & BT_ROOT) {
  3336. p->header.flag &= ~BT_INTERNAL;
  3337. p->header.flag |= BT_LEAF;
  3338. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  3339. if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
  3340. /*
  3341. * Shrink root down to allow inline
  3342. * EA (otherwise fsck complains)
  3343. */
  3344. p->header.maxentry =
  3345. cpu_to_le16(XTROOTINITSLOT);
  3346. JFS_IP(ip)->mode2 |= INLINEEA;
  3347. }
  3348. XT_PUTPAGE(mp); /* debug */
  3349. goto out;
  3350. } else {
  3351. if (log) { /* COMMIT_PWMAP */
  3352. /* page will be invalidated at tx completion
  3353. */
  3354. XT_PUTPAGE(mp);
  3355. } else { /* COMMIT_WMAP */
  3356. if (mp->lid)
  3357. lid_to_tlock(mp->lid)->flag |=
  3358. tlckFREELOCK;
  3359. /* invalidate parent page */
  3360. discard_metapage(mp);
  3361. }
  3362. /* parent has become empty and freed:
  3363. * go back up to its parent page
  3364. */
  3365. /* freed = 1; */
  3366. goto getParent;
  3367. }
  3368. }
  3369. /*
  3370. * parent page still has entries for front region;
  3371. */
  3372. else {
  3373. /* try truncate region covered by preceding entry
  3374. * (process backward)
  3375. */
  3376. index--;
  3377. /* go back down to the child page corresponding
  3378. * to the entry
  3379. */
  3380. goto getChild;
  3381. }
  3382. /*
  3383. * internal page: go down to child page of current entry
  3384. */
  3385. getChild:
  3386. /* save current parent entry for the child page */
  3387. BT_PUSH(&btstack, bn, index);
  3388. /* get child page */
  3389. xad = &p->xad[index];
  3390. bn = addressXAD(xad);
  3391. /*
  3392. * first access of each internal entry:
  3393. */
  3394. /* release parent page */
  3395. XT_PUTPAGE(mp);
  3396. /* process the child page */
  3397. goto getPage;
  3398. out:
  3399. /*
  3400. * update file resource stat
  3401. */
  3402. /* set size
  3403. */
  3404. if (S_ISDIR(ip->i_mode) && !newsize)
  3405. ip->i_size = 1; /* fsck hates zero-length directories */
  3406. else
  3407. ip->i_size = newsize;
  3408. /* update quota allocation to reflect freed blocks */
  3409. DQUOT_FREE_BLOCK(ip, nfreed);
  3410. /*
  3411. * free tlock of invalidated pages
  3412. */
  3413. if (flag == COMMIT_WMAP)
  3414. txFreelock(ip);
  3415. return newsize;
  3416. }
  3417. /*
  3418. * xtTruncate_pmap()
  3419. *
  3420. * function:
  3421. * Perform truncate to zero lenghth for deleted file, leaving the
  3422. * the xtree and working map untouched. This allows the file to
  3423. * be accessed via open file handles, while the delete of the file
  3424. * is committed to disk.
  3425. *
  3426. * parameter:
  3427. * tid_t tid,
  3428. * struct inode *ip,
  3429. * s64 committed_size)
  3430. *
  3431. * return: new committed size
  3432. *
  3433. * note:
  3434. *
  3435. * To avoid deadlock by holding too many transaction locks, the
  3436. * truncation may be broken up into multiple transactions.
  3437. * The committed_size keeps track of part of the file has been
  3438. * freed from the pmaps.
  3439. */
  3440. s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
  3441. {
  3442. s64 bn;
  3443. struct btstack btstack;
  3444. int cmp;
  3445. int index;
  3446. int locked_leaves = 0;
  3447. struct metapage *mp;
  3448. xtpage_t *p;
  3449. struct btframe *parent;
  3450. int rc;
  3451. struct tblock *tblk;
  3452. struct tlock *tlck = NULL;
  3453. xad_t *xad;
  3454. int xlen;
  3455. s64 xoff;
  3456. struct xtlock *xtlck = NULL;
  3457. /* save object truncation type */
  3458. tblk = tid_to_tblock(tid);
  3459. tblk->xflag |= COMMIT_PMAP;
  3460. /* clear stack */
  3461. BT_CLR(&btstack);
  3462. if (committed_size) {
  3463. xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
  3464. rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
  3465. if (rc)
  3466. return rc;
  3467. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  3468. if (cmp != 0) {
  3469. XT_PUTPAGE(mp);
  3470. jfs_error(ip->i_sb,
  3471. "xtTruncate_pmap: did not find extent");
  3472. return -EIO;
  3473. }
  3474. } else {
  3475. /*
  3476. * start with root
  3477. *
  3478. * root resides in the inode
  3479. */
  3480. bn = 0;
  3481. /*
  3482. * first access of each page:
  3483. */
  3484. getPage:
  3485. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3486. if (rc)
  3487. return rc;
  3488. /* process entries backward from last index */
  3489. index = le16_to_cpu(p->header.nextindex) - 1;
  3490. if (p->header.flag & BT_INTERNAL)
  3491. goto getChild;
  3492. }
  3493. /*
  3494. * leaf page
  3495. */
  3496. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  3497. /*
  3498. * We need to limit the size of the transaction
  3499. * to avoid exhausting pagecache & tlocks
  3500. */
  3501. xad = &p->xad[index];
  3502. xoff = offsetXAD(xad);
  3503. xlen = lengthXAD(xad);
  3504. XT_PUTPAGE(mp);
  3505. return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  3506. }
  3507. tlck = txLock(tid, ip, mp, tlckXTREE);
  3508. tlck->type = tlckXTREE | tlckFREE;
  3509. xtlck = (struct xtlock *) & tlck->lock;
  3510. xtlck->hwm.offset = index;
  3511. XT_PUTPAGE(mp);
  3512. /*
  3513. * go back up to the parent page
  3514. */
  3515. getParent:
  3516. /* pop/restore parent entry for the current child page */
  3517. if ((parent = BT_POP(&btstack)) == NULL)
  3518. /* current page must have been root */
  3519. goto out;
  3520. /* get back the parent page */
  3521. bn = parent->bn;
  3522. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3523. if (rc)
  3524. return rc;
  3525. index = parent->index;
  3526. /*
  3527. * parent page become empty: free the page
  3528. */
  3529. if (index == XTENTRYSTART) {
  3530. /* txCommit() with tlckFREE:
  3531. * free child extents covered by parent;
  3532. * invalidate parent if COMMIT_PWMAP;
  3533. */
  3534. tlck = txLock(tid, ip, mp, tlckXTREE);
  3535. xtlck = (struct xtlock *) & tlck->lock;
  3536. xtlck->hwm.offset =
  3537. le16_to_cpu(p->header.nextindex) - 1;
  3538. tlck->type = tlckXTREE | tlckFREE;
  3539. XT_PUTPAGE(mp);
  3540. if (p->header.flag & BT_ROOT) {
  3541. goto out;
  3542. } else {
  3543. goto getParent;
  3544. }
  3545. }
  3546. /*
  3547. * parent page still has entries for front region;
  3548. */
  3549. else
  3550. index--;
  3551. /*
  3552. * internal page: go down to child page of current entry
  3553. */
  3554. getChild:
  3555. /* save current parent entry for the child page */
  3556. BT_PUSH(&btstack, bn, index);
  3557. /* get child page */
  3558. xad = &p->xad[index];
  3559. bn = addressXAD(xad);
  3560. /*
  3561. * first access of each internal entry:
  3562. */
  3563. /* release parent page */
  3564. XT_PUTPAGE(mp);
  3565. /* process the child page */
  3566. goto getPage;
  3567. out:
  3568. return 0;
  3569. }
  3570. #ifdef _JFS_DEBUG_XTREE
  3571. /*
  3572. * xtDisplayTree()
  3573. *
  3574. * function: traverse forward
  3575. */
  3576. int xtDisplayTree(struct inode *ip)
  3577. {
  3578. int rc = 0;
  3579. struct metapage *mp;
  3580. xtpage_t *p;
  3581. s64 bn, pbn;
  3582. int index, lastindex, v, h;
  3583. xad_t *xad;
  3584. struct btstack btstack;
  3585. struct btframe *btsp;
  3586. struct btframe *parent;
  3587. printk("display B+-tree.\n");
  3588. /* clear stack */
  3589. btsp = btstack.stack;
  3590. /*
  3591. * start with root
  3592. *
  3593. * root resides in the inode
  3594. */
  3595. bn = 0;
  3596. v = h = 0;
  3597. /*
  3598. * first access of each page:
  3599. */
  3600. getPage:
  3601. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3602. if (rc)
  3603. return rc;
  3604. /* process entries forward from first index */
  3605. index = XTENTRYSTART;
  3606. lastindex = le16_to_cpu(p->header.nextindex) - 1;
  3607. if (p->header.flag & BT_INTERNAL) {
  3608. /*
  3609. * first access of each internal page
  3610. */
  3611. goto getChild;
  3612. } else { /* (p->header.flag & BT_LEAF) */
  3613. /*
  3614. * first access of each leaf page
  3615. */
  3616. printf("leaf page ");
  3617. xtDisplayPage(ip, bn, p);
  3618. /* unpin the leaf page */
  3619. XT_PUTPAGE(mp);
  3620. }
  3621. /*
  3622. * go back up to the parent page
  3623. */
  3624. getParent:
  3625. /* pop/restore parent entry for the current child page */
  3626. if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
  3627. /* current page must have been root */
  3628. return;
  3629. /*
  3630. * parent page scan completed
  3631. */
  3632. if ((index = parent->index) == (lastindex = parent->lastindex)) {
  3633. /* go back up to the parent page */
  3634. goto getParent;
  3635. }
  3636. /*
  3637. * parent page has entries remaining
  3638. */
  3639. /* get back the parent page */
  3640. bn = parent->bn;
  3641. /* v = parent->level; */
  3642. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3643. if (rc)
  3644. return rc;
  3645. /* get next parent entry */
  3646. index++;
  3647. /*
  3648. * internal page: go down to child page of current entry
  3649. */
  3650. getChild:
  3651. /* push/save current parent entry for the child page */
  3652. btsp->bn = pbn = bn;
  3653. btsp->index = index;
  3654. btsp->lastindex = lastindex;
  3655. /* btsp->level = v; */
  3656. /* btsp->node = h; */
  3657. ++btsp;
  3658. /* get child page */
  3659. xad = &p->xad[index];
  3660. bn = addressXAD(xad);
  3661. /*
  3662. * first access of each internal entry:
  3663. */
  3664. /* release parent page */
  3665. XT_PUTPAGE(mp);
  3666. printk("traverse down 0x%lx[%d]->0x%lx\n", (ulong) pbn, index,
  3667. (ulong) bn);
  3668. v++;
  3669. h = index;
  3670. /* process the child page */
  3671. goto getPage;
  3672. }
  3673. /*
  3674. * xtDisplayPage()
  3675. *
  3676. * function: display page
  3677. */
  3678. int xtDisplayPage(struct inode *ip, s64 bn, xtpage_t * p)
  3679. {
  3680. int rc = 0;
  3681. xad_t *xad;
  3682. s64 xaddr, xoff;
  3683. int xlen, i, j;
  3684. /* display page control */
  3685. printf("bn:0x%lx flag:0x%x nextindex:%d\n",
  3686. (ulong) bn, p->header.flag,
  3687. le16_to_cpu(p->header.nextindex));
  3688. /* display entries */
  3689. xad = &p->xad[XTENTRYSTART];
  3690. for (i = XTENTRYSTART, j = 1; i < le16_to_cpu(p->header.nextindex);
  3691. i++, xad++, j++) {
  3692. xoff = offsetXAD(xad);
  3693. xaddr = addressXAD(xad);
  3694. xlen = lengthXAD(xad);
  3695. printf("\t[%d] 0x%lx:0x%lx(0x%x)", i, (ulong) xoff,
  3696. (ulong) xaddr, xlen);
  3697. if (j == 4) {
  3698. printf("\n");
  3699. j = 0;
  3700. }
  3701. }
  3702. printf("\n");
  3703. }
  3704. #endif /* _JFS_DEBUG_XTREE */
  3705. #ifdef _JFS_WIP
  3706. /*
  3707. * xtGather()
  3708. *
  3709. * function:
  3710. * traverse for allocation acquiring tlock at commit time
  3711. * (vs at the time of update) logging backward top down
  3712. *
  3713. * note:
  3714. * problem - establishing that all new allocation have been
  3715. * processed both for append and random write in sparse file
  3716. * at the current entry at the current subtree root page
  3717. *
  3718. */
  3719. int xtGather(btree_t *t)
  3720. {
  3721. int rc = 0;
  3722. xtpage_t *p;
  3723. u64 bn;
  3724. int index;
  3725. btentry_t *e;
  3726. struct btstack btstack;
  3727. struct btsf *parent;
  3728. /* clear stack */
  3729. BT_CLR(&btstack);
  3730. /*
  3731. * start with root
  3732. *
  3733. * root resides in the inode
  3734. */
  3735. bn = 0;
  3736. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3737. if (rc)
  3738. return rc;
  3739. /* new root is NOT pointed by a new entry
  3740. if (p->header.flag & NEW)
  3741. allocate new page lock;
  3742. write a NEWPAGE log;
  3743. */
  3744. dopage:
  3745. /*
  3746. * first access of each page:
  3747. */
  3748. /* process entries backward from last index */
  3749. index = le16_to_cpu(p->header.nextindex) - 1;
  3750. if (p->header.flag & BT_LEAF) {
  3751. /*
  3752. * first access of each leaf page
  3753. */
  3754. /* process leaf page entries backward */
  3755. for (; index >= XTENTRYSTART; index--) {
  3756. e = &p->xad[index];
  3757. /*
  3758. * if newpage, log NEWPAGE.
  3759. *
  3760. if (e->flag & XAD_NEW) {
  3761. nfound =+ entry->length;
  3762. update current page lock for the entry;
  3763. newpage(entry);
  3764. *
  3765. * if moved, log move.
  3766. *
  3767. } else if (e->flag & XAD_MOVED) {
  3768. reset flag;
  3769. update current page lock for the entry;
  3770. }
  3771. */
  3772. }
  3773. /* unpin the leaf page */
  3774. XT_PUTPAGE(mp);
  3775. /*
  3776. * go back up to the parent page
  3777. */
  3778. getParent:
  3779. /* restore parent entry for the current child page */
  3780. if ((parent = BT_POP(&btstack)) == NULL)
  3781. /* current page must have been root */
  3782. return 0;
  3783. if ((index = parent->index) == XTENTRYSTART) {
  3784. /*
  3785. * parent page scan completed
  3786. */
  3787. /* go back up to the parent page */
  3788. goto getParent;
  3789. } else {
  3790. /*
  3791. * parent page has entries remaining
  3792. */
  3793. /* get back the parent page */
  3794. bn = parent->bn;
  3795. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3796. if (rc)
  3797. return -EIO;
  3798. /* first subroot page which
  3799. * covers all new allocated blocks
  3800. * itself not new/modified.
  3801. * (if modified from split of descendent,
  3802. * go down path of split page)
  3803. if (nfound == nnew &&
  3804. !(p->header.flag & (NEW | MOD)))
  3805. exit scan;
  3806. */
  3807. /* process parent page entries backward */
  3808. index--;
  3809. }
  3810. } else {
  3811. /*
  3812. * first access of each internal page
  3813. */
  3814. }
  3815. /*
  3816. * internal page: go down to child page of current entry
  3817. */
  3818. /* save current parent entry for the child page */
  3819. BT_PUSH(&btstack, bn, index);
  3820. /* get current entry for the child page */
  3821. e = &p->xad[index];
  3822. /*
  3823. * first access of each internal entry:
  3824. */
  3825. /*
  3826. * if new entry, log btree_tnewentry.
  3827. *
  3828. if (e->flag & XAD_NEW)
  3829. update parent page lock for the entry;
  3830. */
  3831. /* release parent page */
  3832. XT_PUTPAGE(mp);
  3833. /* get child page */
  3834. bn = e->bn;
  3835. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3836. if (rc)
  3837. return rc;
  3838. /*
  3839. * first access of each non-root page:
  3840. */
  3841. /*
  3842. * if new, log btree_newpage.
  3843. *
  3844. if (p->header.flag & NEW)
  3845. allocate new page lock;
  3846. write a NEWPAGE log (next, prev);
  3847. */
  3848. /* process the child page */
  3849. goto dopage;
  3850. out:
  3851. return 0;
  3852. }
  3853. #endif /* _JFS_WIP */
  3854. #ifdef CONFIG_JFS_STATISTICS
  3855. int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
  3856. int *eof, void *data)
  3857. {
  3858. int len = 0;
  3859. off_t begin;
  3860. len += sprintf(buffer,
  3861. "JFS Xtree statistics\n"
  3862. "====================\n"
  3863. "searches = %d\n"
  3864. "fast searches = %d\n"
  3865. "splits = %d\n",
  3866. xtStat.search,
  3867. xtStat.fastSearch,
  3868. xtStat.split);
  3869. begin = offset;
  3870. *start = buffer + begin;
  3871. len -= begin;
  3872. if (len > length)
  3873. len = length;
  3874. else
  3875. *eof = 1;
  3876. if (len < 0)
  3877. len = 0;
  3878. return len;
  3879. }
  3880. #endif