jfs_xtree.c 104 KB

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