jfs_dtree.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * jfs_dtree.c: directory B+-tree manager
  20. *
  21. * B+-tree with variable length key directory:
  22. *
  23. * each directory page is structured as an array of 32-byte
  24. * directory entry slots initialized as a freelist
  25. * to avoid search/compaction of free space at insertion.
  26. * when an entry is inserted, a number of slots are allocated
  27. * from the freelist as required to store variable length data
  28. * of the entry; when the entry is deleted, slots of the entry
  29. * are returned to freelist.
  30. *
  31. * leaf entry stores full name as key and file serial number
  32. * (aka inode number) as data.
  33. * internal/router entry stores sufffix compressed name
  34. * as key and simple extent descriptor as data.
  35. *
  36. * each directory page maintains a sorted entry index table
  37. * which stores the start slot index of sorted entries
  38. * to allow binary search on the table.
  39. *
  40. * directory starts as a root/leaf page in on-disk inode
  41. * inline data area.
  42. * when it becomes full, it starts a leaf of a external extent
  43. * of length of 1 block. each time the first leaf becomes full,
  44. * it is extended rather than split (its size is doubled),
  45. * until its length becoms 4 KBytes, from then the extent is split
  46. * with new 4 Kbyte extent when it becomes full
  47. * to reduce external fragmentation of small directories.
  48. *
  49. * blah, blah, blah, for linear scan of directory in pieces by
  50. * readdir().
  51. *
  52. *
  53. * case-insensitive directory file system
  54. *
  55. * names are stored in case-sensitive way in leaf entry.
  56. * but stored, searched and compared in case-insensitive (uppercase) order
  57. * (i.e., both search key and entry key are folded for search/compare):
  58. * (note that case-sensitive order is BROKEN in storage, e.g.,
  59. * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
  60. *
  61. * entries which folds to the same key makes up a equivalent class
  62. * whose members are stored as contiguous cluster (may cross page boundary)
  63. * but whose order is arbitrary and acts as duplicate, e.g.,
  64. * abc, Abc, aBc, abC)
  65. *
  66. * once match is found at leaf, requires scan forward/backward
  67. * either for, in case-insensitive search, duplicate
  68. * or for, in case-sensitive search, for exact match
  69. *
  70. * router entry must be created/stored in case-insensitive way
  71. * in internal entry:
  72. * (right most key of left page and left most key of right page
  73. * are folded, and its suffix compression is propagated as router
  74. * key in parent)
  75. * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
  76. * should be made the router key for the split)
  77. *
  78. * case-insensitive search:
  79. *
  80. * fold search key;
  81. *
  82. * case-insensitive search of B-tree:
  83. * for internal entry, router key is already folded;
  84. * for leaf entry, fold the entry key before comparison.
  85. *
  86. * if (leaf entry case-insensitive match found)
  87. * if (next entry satisfies case-insensitive match)
  88. * return EDUPLICATE;
  89. * if (prev entry satisfies case-insensitive match)
  90. * return EDUPLICATE;
  91. * return match;
  92. * else
  93. * return no match;
  94. *
  95. * serialization:
  96. * target directory inode lock is being held on entry/exit
  97. * of all main directory service routines.
  98. *
  99. * log based recovery:
  100. */
  101. #include <linux/fs.h>
  102. #include <linux/quotaops.h>
  103. #include "jfs_incore.h"
  104. #include "jfs_superblock.h"
  105. #include "jfs_filsys.h"
  106. #include "jfs_metapage.h"
  107. #include "jfs_dmap.h"
  108. #include "jfs_unicode.h"
  109. #include "jfs_debug.h"
  110. /* dtree split parameter */
  111. struct dtsplit {
  112. struct metapage *mp;
  113. s16 index;
  114. s16 nslot;
  115. struct component_name *key;
  116. ddata_t *data;
  117. struct pxdlist *pxdlist;
  118. };
  119. #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
  120. /* get page buffer for specified block address */
  121. #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
  122. {\
  123. BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
  124. if (!(RC))\
  125. {\
  126. if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
  127. ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
  128. {\
  129. BT_PUTPAGE(MP);\
  130. jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
  131. MP = NULL;\
  132. RC = -EIO;\
  133. }\
  134. }\
  135. }
  136. /* for consistency */
  137. #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
  138. #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
  139. BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
  140. /*
  141. * forward references
  142. */
  143. static int dtSplitUp(tid_t tid, struct inode *ip,
  144. struct dtsplit * split, struct btstack * btstack);
  145. static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
  146. struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
  147. static int dtExtendPage(tid_t tid, struct inode *ip,
  148. struct dtsplit * split, struct btstack * btstack);
  149. static int dtSplitRoot(tid_t tid, struct inode *ip,
  150. struct dtsplit * split, struct metapage ** rmpp);
  151. static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
  152. dtpage_t * fp, struct btstack * btstack);
  153. static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
  154. static int dtReadFirst(struct inode *ip, struct btstack * btstack);
  155. static int dtReadNext(struct inode *ip,
  156. loff_t * offset, struct btstack * btstack);
  157. static int dtCompare(struct component_name * key, dtpage_t * p, int si);
  158. static int ciCompare(struct component_name * key, dtpage_t * p, int si,
  159. int flag);
  160. static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
  161. int flag);
  162. static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
  163. int ri, struct component_name * key, int flag);
  164. static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
  165. ddata_t * data, struct dt_lock **);
  166. static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
  167. struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
  168. int do_index);
  169. static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
  170. static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
  171. static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
  172. #define ciToUpper(c) UniStrupr((c)->name)
  173. /*
  174. * read_index_page()
  175. *
  176. * Reads a page of a directory's index table.
  177. * Having metadata mapped into the directory inode's address space
  178. * presents a multitude of problems. We avoid this by mapping to
  179. * the absolute address space outside of the *_metapage routines
  180. */
  181. static struct metapage *read_index_page(struct inode *inode, s64 blkno)
  182. {
  183. int rc;
  184. s64 xaddr;
  185. int xflag;
  186. s32 xlen;
  187. rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
  188. if (rc || (xaddr == 0))
  189. return NULL;
  190. return read_metapage(inode, xaddr, PSIZE, 1);
  191. }
  192. /*
  193. * get_index_page()
  194. *
  195. * Same as get_index_page(), but get's a new page without reading
  196. */
  197. static struct metapage *get_index_page(struct inode *inode, s64 blkno)
  198. {
  199. int rc;
  200. s64 xaddr;
  201. int xflag;
  202. s32 xlen;
  203. rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
  204. if (rc || (xaddr == 0))
  205. return NULL;
  206. return get_metapage(inode, xaddr, PSIZE, 1);
  207. }
  208. /*
  209. * find_index()
  210. *
  211. * Returns dtree page containing directory table entry for specified
  212. * index and pointer to its entry.
  213. *
  214. * mp must be released by caller.
  215. */
  216. static struct dir_table_slot *find_index(struct inode *ip, u32 index,
  217. struct metapage ** mp, s64 *lblock)
  218. {
  219. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  220. s64 blkno;
  221. s64 offset;
  222. int page_offset;
  223. struct dir_table_slot *slot;
  224. static int maxWarnings = 10;
  225. if (index < 2) {
  226. if (maxWarnings) {
  227. jfs_warn("find_entry called with index = %d", index);
  228. maxWarnings--;
  229. }
  230. return NULL;
  231. }
  232. if (index >= jfs_ip->next_index) {
  233. jfs_warn("find_entry called with index >= next_index");
  234. return NULL;
  235. }
  236. if (jfs_dirtable_inline(ip)) {
  237. /*
  238. * Inline directory table
  239. */
  240. *mp = NULL;
  241. slot = &jfs_ip->i_dirtable[index - 2];
  242. } else {
  243. offset = (index - 2) * sizeof(struct dir_table_slot);
  244. page_offset = offset & (PSIZE - 1);
  245. blkno = ((offset + 1) >> L2PSIZE) <<
  246. JFS_SBI(ip->i_sb)->l2nbperpage;
  247. if (*mp && (*lblock != blkno)) {
  248. release_metapage(*mp);
  249. *mp = NULL;
  250. }
  251. if (*mp == 0) {
  252. *lblock = blkno;
  253. *mp = read_index_page(ip, blkno);
  254. }
  255. if (*mp == 0) {
  256. jfs_err("free_index: error reading directory table");
  257. return NULL;
  258. }
  259. slot =
  260. (struct dir_table_slot *) ((char *) (*mp)->data +
  261. page_offset);
  262. }
  263. return slot;
  264. }
  265. static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
  266. u32 index)
  267. {
  268. struct tlock *tlck;
  269. struct linelock *llck;
  270. struct lv *lv;
  271. tlck = txLock(tid, ip, mp, tlckDATA);
  272. llck = (struct linelock *) tlck->lock;
  273. if (llck->index >= llck->maxcnt)
  274. llck = txLinelock(llck);
  275. lv = &llck->lv[llck->index];
  276. /*
  277. * Linelock slot size is twice the size of directory table
  278. * slot size. 512 entries per page.
  279. */
  280. lv->offset = ((index - 2) & 511) >> 1;
  281. lv->length = 1;
  282. llck->index++;
  283. }
  284. /*
  285. * add_index()
  286. *
  287. * Adds an entry to the directory index table. This is used to provide
  288. * each directory entry with a persistent index in which to resume
  289. * directory traversals
  290. */
  291. static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
  292. {
  293. struct super_block *sb = ip->i_sb;
  294. struct jfs_sb_info *sbi = JFS_SBI(sb);
  295. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  296. u64 blkno;
  297. struct dir_table_slot *dirtab_slot;
  298. u32 index;
  299. struct linelock *llck;
  300. struct lv *lv;
  301. struct metapage *mp;
  302. s64 offset;
  303. uint page_offset;
  304. struct tlock *tlck;
  305. s64 xaddr;
  306. ASSERT(DO_INDEX(ip));
  307. if (jfs_ip->next_index < 2) {
  308. jfs_warn("add_index: next_index = %d. Resetting!",
  309. jfs_ip->next_index);
  310. jfs_ip->next_index = 2;
  311. }
  312. index = jfs_ip->next_index++;
  313. if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
  314. /*
  315. * i_size reflects size of index table, or 8 bytes per entry.
  316. */
  317. ip->i_size = (loff_t) (index - 1) << 3;
  318. /*
  319. * dir table fits inline within inode
  320. */
  321. dirtab_slot = &jfs_ip->i_dirtable[index-2];
  322. dirtab_slot->flag = DIR_INDEX_VALID;
  323. dirtab_slot->slot = slot;
  324. DTSaddress(dirtab_slot, bn);
  325. set_cflag(COMMIT_Dirtable, ip);
  326. return index;
  327. }
  328. if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
  329. struct dir_table_slot temp_table[12];
  330. /*
  331. * It's time to move the inline table to an external
  332. * page and begin to build the xtree
  333. */
  334. if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage) ||
  335. dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
  336. goto clean_up; /* No space */
  337. /*
  338. * Save the table, we're going to overwrite it with the
  339. * xtree root
  340. */
  341. memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
  342. /*
  343. * Initialize empty x-tree
  344. */
  345. xtInitRoot(tid, ip);
  346. /*
  347. * Allocate the first block & add it to the xtree
  348. */
  349. if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
  350. /* This really shouldn't fail */
  351. jfs_warn("add_index: xtInsert failed!");
  352. memcpy(&jfs_ip->i_dirtable, temp_table,
  353. sizeof (temp_table));
  354. goto clean_up;
  355. }
  356. ip->i_size = PSIZE;
  357. if ((mp = get_index_page(ip, 0)) == 0) {
  358. jfs_err("add_index: get_metapage failed!");
  359. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  360. memcpy(&jfs_ip->i_dirtable, temp_table,
  361. sizeof (temp_table));
  362. goto clean_up;
  363. }
  364. tlck = txLock(tid, ip, mp, tlckDATA);
  365. llck = (struct linelock *) & tlck->lock;
  366. ASSERT(llck->index == 0);
  367. lv = &llck->lv[0];
  368. lv->offset = 0;
  369. lv->length = 6; /* tlckDATA slot size is 16 bytes */
  370. llck->index++;
  371. memcpy(mp->data, temp_table, sizeof(temp_table));
  372. mark_metapage_dirty(mp);
  373. release_metapage(mp);
  374. /*
  375. * Logging is now directed by xtree tlocks
  376. */
  377. clear_cflag(COMMIT_Dirtable, ip);
  378. }
  379. offset = (index - 2) * sizeof(struct dir_table_slot);
  380. page_offset = offset & (PSIZE - 1);
  381. blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
  382. if (page_offset == 0) {
  383. /*
  384. * This will be the beginning of a new page
  385. */
  386. xaddr = 0;
  387. if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
  388. jfs_warn("add_index: xtInsert failed!");
  389. goto clean_up;
  390. }
  391. ip->i_size += PSIZE;
  392. if ((mp = get_index_page(ip, blkno)))
  393. memset(mp->data, 0, PSIZE); /* Just looks better */
  394. else
  395. xtTruncate(tid, ip, offset, COMMIT_PWMAP);
  396. } else
  397. mp = read_index_page(ip, blkno);
  398. if (mp == 0) {
  399. jfs_err("add_index: get/read_metapage failed!");
  400. goto clean_up;
  401. }
  402. lock_index(tid, ip, mp, index);
  403. dirtab_slot =
  404. (struct dir_table_slot *) ((char *) mp->data + page_offset);
  405. dirtab_slot->flag = DIR_INDEX_VALID;
  406. dirtab_slot->slot = slot;
  407. DTSaddress(dirtab_slot, bn);
  408. mark_metapage_dirty(mp);
  409. release_metapage(mp);
  410. return index;
  411. clean_up:
  412. jfs_ip->next_index--;
  413. return 0;
  414. }
  415. /*
  416. * free_index()
  417. *
  418. * Marks an entry to the directory index table as free.
  419. */
  420. static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
  421. {
  422. struct dir_table_slot *dirtab_slot;
  423. s64 lblock;
  424. struct metapage *mp = NULL;
  425. dirtab_slot = find_index(ip, index, &mp, &lblock);
  426. if (dirtab_slot == 0)
  427. return;
  428. dirtab_slot->flag = DIR_INDEX_FREE;
  429. dirtab_slot->slot = dirtab_slot->addr1 = 0;
  430. dirtab_slot->addr2 = cpu_to_le32(next);
  431. if (mp) {
  432. lock_index(tid, ip, mp, index);
  433. mark_metapage_dirty(mp);
  434. release_metapage(mp);
  435. } else
  436. set_cflag(COMMIT_Dirtable, ip);
  437. }
  438. /*
  439. * modify_index()
  440. *
  441. * Changes an entry in the directory index table
  442. */
  443. static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
  444. int slot, struct metapage ** mp, u64 *lblock)
  445. {
  446. struct dir_table_slot *dirtab_slot;
  447. dirtab_slot = find_index(ip, index, mp, lblock);
  448. if (dirtab_slot == 0)
  449. return;
  450. DTSaddress(dirtab_slot, bn);
  451. dirtab_slot->slot = slot;
  452. if (*mp) {
  453. lock_index(tid, ip, *mp, index);
  454. mark_metapage_dirty(*mp);
  455. } else
  456. set_cflag(COMMIT_Dirtable, ip);
  457. }
  458. /*
  459. * read_index()
  460. *
  461. * reads a directory table slot
  462. */
  463. static int read_index(struct inode *ip, u32 index,
  464. struct dir_table_slot * dirtab_slot)
  465. {
  466. s64 lblock;
  467. struct metapage *mp = NULL;
  468. struct dir_table_slot *slot;
  469. slot = find_index(ip, index, &mp, &lblock);
  470. if (slot == 0) {
  471. return -EIO;
  472. }
  473. memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
  474. if (mp)
  475. release_metapage(mp);
  476. return 0;
  477. }
  478. /*
  479. * dtSearch()
  480. *
  481. * function:
  482. * Search for the entry with specified key
  483. *
  484. * parameter:
  485. *
  486. * return: 0 - search result on stack, leaf page pinned;
  487. * errno - I/O error
  488. */
  489. int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
  490. struct btstack * btstack, int flag)
  491. {
  492. int rc = 0;
  493. int cmp = 1; /* init for empty page */
  494. s64 bn;
  495. struct metapage *mp;
  496. dtpage_t *p;
  497. s8 *stbl;
  498. int base, index, lim;
  499. struct btframe *btsp;
  500. pxd_t *pxd;
  501. int psize = 288; /* initial in-line directory */
  502. ino_t inumber;
  503. struct component_name ciKey;
  504. struct super_block *sb = ip->i_sb;
  505. ciKey.name =
  506. (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
  507. GFP_NOFS);
  508. if (ciKey.name == 0) {
  509. rc = -ENOMEM;
  510. goto dtSearch_Exit2;
  511. }
  512. /* uppercase search key for c-i directory */
  513. UniStrcpy(ciKey.name, key->name);
  514. ciKey.namlen = key->namlen;
  515. /* only uppercase if case-insensitive support is on */
  516. if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
  517. ciToUpper(&ciKey);
  518. }
  519. BT_CLR(btstack); /* reset stack */
  520. /* init level count for max pages to split */
  521. btstack->nsplit = 1;
  522. /*
  523. * search down tree from root:
  524. *
  525. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  526. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  527. *
  528. * if entry with search key K is not found
  529. * internal page search find the entry with largest key Ki
  530. * less than K which point to the child page to search;
  531. * leaf page search find the entry with smallest key Kj
  532. * greater than K so that the returned index is the position of
  533. * the entry to be shifted right for insertion of new entry.
  534. * for empty tree, search key is greater than any key of the tree.
  535. *
  536. * by convention, root bn = 0.
  537. */
  538. for (bn = 0;;) {
  539. /* get/pin the page to search */
  540. DT_GETPAGE(ip, bn, mp, psize, p, rc);
  541. if (rc)
  542. goto dtSearch_Exit1;
  543. /* get sorted entry table of the page */
  544. stbl = DT_GETSTBL(p);
  545. /*
  546. * binary search with search key K on the current page.
  547. */
  548. for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
  549. index = base + (lim >> 1);
  550. if (p->header.flag & BT_LEAF) {
  551. /* uppercase leaf name to compare */
  552. cmp =
  553. ciCompare(&ciKey, p, stbl[index],
  554. JFS_SBI(sb)->mntflag);
  555. } else {
  556. /* router key is in uppercase */
  557. cmp = dtCompare(&ciKey, p, stbl[index]);
  558. }
  559. if (cmp == 0) {
  560. /*
  561. * search hit
  562. */
  563. /* search hit - leaf page:
  564. * return the entry found
  565. */
  566. if (p->header.flag & BT_LEAF) {
  567. inumber = le32_to_cpu(
  568. ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
  569. /*
  570. * search for JFS_LOOKUP
  571. */
  572. if (flag == JFS_LOOKUP) {
  573. *data = inumber;
  574. rc = 0;
  575. goto out;
  576. }
  577. /*
  578. * search for JFS_CREATE
  579. */
  580. if (flag == JFS_CREATE) {
  581. *data = inumber;
  582. rc = -EEXIST;
  583. goto out;
  584. }
  585. /*
  586. * search for JFS_REMOVE or JFS_RENAME
  587. */
  588. if ((flag == JFS_REMOVE ||
  589. flag == JFS_RENAME) &&
  590. *data != inumber) {
  591. rc = -ESTALE;
  592. goto out;
  593. }
  594. /*
  595. * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
  596. */
  597. /* save search result */
  598. *data = inumber;
  599. btsp = btstack->top;
  600. btsp->bn = bn;
  601. btsp->index = index;
  602. btsp->mp = mp;
  603. rc = 0;
  604. goto dtSearch_Exit1;
  605. }
  606. /* search hit - internal page:
  607. * descend/search its child page
  608. */
  609. goto getChild;
  610. }
  611. if (cmp > 0) {
  612. base = index + 1;
  613. --lim;
  614. }
  615. }
  616. /*
  617. * search miss
  618. *
  619. * base is the smallest index with key (Kj) greater than
  620. * search key (K) and may be zero or (maxindex + 1) index.
  621. */
  622. /*
  623. * search miss - leaf page
  624. *
  625. * return location of entry (base) where new entry with
  626. * search key K is to be inserted.
  627. */
  628. if (p->header.flag & BT_LEAF) {
  629. /*
  630. * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
  631. */
  632. if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
  633. flag == JFS_RENAME) {
  634. rc = -ENOENT;
  635. goto out;
  636. }
  637. /*
  638. * search for JFS_CREATE|JFS_FINDDIR:
  639. *
  640. * save search result
  641. */
  642. *data = 0;
  643. btsp = btstack->top;
  644. btsp->bn = bn;
  645. btsp->index = base;
  646. btsp->mp = mp;
  647. rc = 0;
  648. goto dtSearch_Exit1;
  649. }
  650. /*
  651. * search miss - internal page
  652. *
  653. * if base is non-zero, decrement base by one to get the parent
  654. * entry of the child page to search.
  655. */
  656. index = base ? base - 1 : base;
  657. /*
  658. * go down to child page
  659. */
  660. getChild:
  661. /* update max. number of pages to split */
  662. if (BT_STACK_FULL(btstack)) {
  663. /* Something's corrupted, mark filesytem dirty so
  664. * chkdsk will fix it.
  665. */
  666. jfs_error(sb, "stack overrun in dtSearch!");
  667. BT_STACK_DUMP(btstack);
  668. rc = -EIO;
  669. goto out;
  670. }
  671. btstack->nsplit++;
  672. /* push (bn, index) of the parent page/entry */
  673. BT_PUSH(btstack, bn, index);
  674. /* get the child page block number */
  675. pxd = (pxd_t *) & p->slot[stbl[index]];
  676. bn = addressPXD(pxd);
  677. psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
  678. /* unpin the parent page */
  679. DT_PUTPAGE(mp);
  680. }
  681. out:
  682. DT_PUTPAGE(mp);
  683. dtSearch_Exit1:
  684. kfree(ciKey.name);
  685. dtSearch_Exit2:
  686. return rc;
  687. }
  688. /*
  689. * dtInsert()
  690. *
  691. * function: insert an entry to directory tree
  692. *
  693. * parameter:
  694. *
  695. * return: 0 - success;
  696. * errno - failure;
  697. */
  698. int dtInsert(tid_t tid, struct inode *ip,
  699. struct component_name * name, ino_t * fsn, struct btstack * btstack)
  700. {
  701. int rc = 0;
  702. struct metapage *mp; /* meta-page buffer */
  703. dtpage_t *p; /* base B+-tree index page */
  704. s64 bn;
  705. int index;
  706. struct dtsplit split; /* split information */
  707. ddata_t data;
  708. struct dt_lock *dtlck;
  709. int n;
  710. struct tlock *tlck;
  711. struct lv *lv;
  712. /*
  713. * retrieve search result
  714. *
  715. * dtSearch() returns (leaf page pinned, index at which to insert).
  716. * n.b. dtSearch() may return index of (maxindex + 1) of
  717. * the full page.
  718. */
  719. DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
  720. /*
  721. * insert entry for new key
  722. */
  723. if (DO_INDEX(ip)) {
  724. if (JFS_IP(ip)->next_index == DIREND) {
  725. DT_PUTPAGE(mp);
  726. return -EMLINK;
  727. }
  728. n = NDTLEAF(name->namlen);
  729. data.leaf.tid = tid;
  730. data.leaf.ip = ip;
  731. } else {
  732. n = NDTLEAF_LEGACY(name->namlen);
  733. data.leaf.ip = NULL; /* signifies legacy directory format */
  734. }
  735. data.leaf.ino = *fsn;
  736. /*
  737. * leaf page does not have enough room for new entry:
  738. *
  739. * extend/split the leaf page;
  740. *
  741. * dtSplitUp() will insert the entry and unpin the leaf page.
  742. */
  743. if (n > p->header.freecnt) {
  744. split.mp = mp;
  745. split.index = index;
  746. split.nslot = n;
  747. split.key = name;
  748. split.data = &data;
  749. rc = dtSplitUp(tid, ip, &split, btstack);
  750. return rc;
  751. }
  752. /*
  753. * leaf page does have enough room for new entry:
  754. *
  755. * insert the new data entry into the leaf page;
  756. */
  757. BT_MARK_DIRTY(mp, ip);
  758. /*
  759. * acquire a transaction lock on the leaf page
  760. */
  761. tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
  762. dtlck = (struct dt_lock *) & tlck->lock;
  763. ASSERT(dtlck->index == 0);
  764. lv = & dtlck->lv[0];
  765. /* linelock header */
  766. lv->offset = 0;
  767. lv->length = 1;
  768. dtlck->index++;
  769. dtInsertEntry(p, index, name, &data, &dtlck);
  770. /* linelock stbl of non-root leaf page */
  771. if (!(p->header.flag & BT_ROOT)) {
  772. if (dtlck->index >= dtlck->maxcnt)
  773. dtlck = (struct dt_lock *) txLinelock(dtlck);
  774. lv = & dtlck->lv[dtlck->index];
  775. n = index >> L2DTSLOTSIZE;
  776. lv->offset = p->header.stblindex + n;
  777. lv->length =
  778. ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
  779. dtlck->index++;
  780. }
  781. /* unpin the leaf page */
  782. DT_PUTPAGE(mp);
  783. return 0;
  784. }
  785. /*
  786. * dtSplitUp()
  787. *
  788. * function: propagate insertion bottom up;
  789. *
  790. * parameter:
  791. *
  792. * return: 0 - success;
  793. * errno - failure;
  794. * leaf page unpinned;
  795. */
  796. static int dtSplitUp(tid_t tid,
  797. struct inode *ip, struct dtsplit * split, struct btstack * btstack)
  798. {
  799. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  800. int rc = 0;
  801. struct metapage *smp;
  802. dtpage_t *sp; /* split page */
  803. struct metapage *rmp;
  804. dtpage_t *rp; /* new right page split from sp */
  805. pxd_t rpxd; /* new right page extent descriptor */
  806. struct metapage *lmp;
  807. dtpage_t *lp; /* left child page */
  808. int skip; /* index of entry of insertion */
  809. struct btframe *parent; /* parent page entry on traverse stack */
  810. s64 xaddr, nxaddr;
  811. int xlen, xsize;
  812. struct pxdlist pxdlist;
  813. pxd_t *pxd;
  814. struct component_name key = { 0, NULL };
  815. ddata_t *data = split->data;
  816. int n;
  817. struct dt_lock *dtlck;
  818. struct tlock *tlck;
  819. struct lv *lv;
  820. int quota_allocation = 0;
  821. /* get split page */
  822. smp = split->mp;
  823. sp = DT_PAGE(ip, smp);
  824. key.name =
  825. (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
  826. GFP_NOFS);
  827. if (key.name == 0) {
  828. DT_PUTPAGE(smp);
  829. rc = -ENOMEM;
  830. goto dtSplitUp_Exit;
  831. }
  832. /*
  833. * split leaf page
  834. *
  835. * The split routines insert the new entry, and
  836. * acquire txLock as appropriate.
  837. */
  838. /*
  839. * split root leaf page:
  840. */
  841. if (sp->header.flag & BT_ROOT) {
  842. /*
  843. * allocate a single extent child page
  844. */
  845. xlen = 1;
  846. n = sbi->bsize >> L2DTSLOTSIZE;
  847. n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
  848. n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
  849. if (n <= split->nslot)
  850. xlen++;
  851. if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
  852. DT_PUTPAGE(smp);
  853. goto freeKeyName;
  854. }
  855. pxdlist.maxnpxd = 1;
  856. pxdlist.npxd = 0;
  857. pxd = &pxdlist.pxd[0];
  858. PXDaddress(pxd, xaddr);
  859. PXDlength(pxd, xlen);
  860. split->pxdlist = &pxdlist;
  861. rc = dtSplitRoot(tid, ip, split, &rmp);
  862. if (rc)
  863. dbFree(ip, xaddr, xlen);
  864. else
  865. DT_PUTPAGE(rmp);
  866. DT_PUTPAGE(smp);
  867. goto freeKeyName;
  868. }
  869. /*
  870. * extend first leaf page
  871. *
  872. * extend the 1st extent if less than buffer page size
  873. * (dtExtendPage() reurns leaf page unpinned)
  874. */
  875. pxd = &sp->header.self;
  876. xlen = lengthPXD(pxd);
  877. xsize = xlen << sbi->l2bsize;
  878. if (xsize < PSIZE) {
  879. xaddr = addressPXD(pxd);
  880. n = xsize >> L2DTSLOTSIZE;
  881. n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
  882. if ((n + sp->header.freecnt) <= split->nslot)
  883. n = xlen + (xlen << 1);
  884. else
  885. n = xlen;
  886. /* Allocate blocks to quota. */
  887. if (DQUOT_ALLOC_BLOCK(ip, n)) {
  888. rc = -EDQUOT;
  889. goto extendOut;
  890. }
  891. quota_allocation += n;
  892. if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
  893. (s64) n, &nxaddr)))
  894. goto extendOut;
  895. pxdlist.maxnpxd = 1;
  896. pxdlist.npxd = 0;
  897. pxd = &pxdlist.pxd[0];
  898. PXDaddress(pxd, nxaddr)
  899. PXDlength(pxd, xlen + n);
  900. split->pxdlist = &pxdlist;
  901. if ((rc = dtExtendPage(tid, ip, split, btstack))) {
  902. nxaddr = addressPXD(pxd);
  903. if (xaddr != nxaddr) {
  904. /* free relocated extent */
  905. xlen = lengthPXD(pxd);
  906. dbFree(ip, nxaddr, (s64) xlen);
  907. } else {
  908. /* free extended delta */
  909. xlen = lengthPXD(pxd) - n;
  910. xaddr = addressPXD(pxd) + xlen;
  911. dbFree(ip, xaddr, (s64) n);
  912. }
  913. }
  914. extendOut:
  915. DT_PUTPAGE(smp);
  916. goto freeKeyName;
  917. }
  918. /*
  919. * split leaf page <sp> into <sp> and a new right page <rp>.
  920. *
  921. * return <rp> pinned and its extent descriptor <rpxd>
  922. */
  923. /*
  924. * allocate new directory page extent and
  925. * new index page(s) to cover page split(s)
  926. *
  927. * allocation hint: ?
  928. */
  929. n = btstack->nsplit;
  930. pxdlist.maxnpxd = pxdlist.npxd = 0;
  931. xlen = sbi->nbperpage;
  932. for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
  933. if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
  934. PXDaddress(pxd, xaddr);
  935. PXDlength(pxd, xlen);
  936. pxdlist.maxnpxd++;
  937. continue;
  938. }
  939. DT_PUTPAGE(smp);
  940. /* undo allocation */
  941. goto splitOut;
  942. }
  943. split->pxdlist = &pxdlist;
  944. if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
  945. DT_PUTPAGE(smp);
  946. /* undo allocation */
  947. goto splitOut;
  948. }
  949. /*
  950. * propagate up the router entry for the leaf page just split
  951. *
  952. * insert a router entry for the new page into the parent page,
  953. * propagate the insert/split up the tree by walking back the stack
  954. * of (bn of parent page, index of child page entry in parent page)
  955. * that were traversed during the search for the page that split.
  956. *
  957. * the propagation of insert/split up the tree stops if the root
  958. * splits or the page inserted into doesn't have to split to hold
  959. * the new entry.
  960. *
  961. * the parent entry for the split page remains the same, and
  962. * a new entry is inserted at its right with the first key and
  963. * block number of the new right page.
  964. *
  965. * There are a maximum of 4 pages pinned at any time:
  966. * two children, left parent and right parent (when the parent splits).
  967. * keep the child pages pinned while working on the parent.
  968. * make sure that all pins are released at exit.
  969. */
  970. while ((parent = BT_POP(btstack)) != NULL) {
  971. /* parent page specified by stack frame <parent> */
  972. /* keep current child pages (<lp>, <rp>) pinned */
  973. lmp = smp;
  974. lp = sp;
  975. /*
  976. * insert router entry in parent for new right child page <rp>
  977. */
  978. /* get the parent page <sp> */
  979. DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
  980. if (rc) {
  981. DT_PUTPAGE(lmp);
  982. DT_PUTPAGE(rmp);
  983. goto splitOut;
  984. }
  985. /*
  986. * The new key entry goes ONE AFTER the index of parent entry,
  987. * because the split was to the right.
  988. */
  989. skip = parent->index + 1;
  990. /*
  991. * compute the key for the router entry
  992. *
  993. * key suffix compression:
  994. * for internal pages that have leaf pages as children,
  995. * retain only what's needed to distinguish between
  996. * the new entry and the entry on the page to its left.
  997. * If the keys compare equal, retain the entire key.
  998. *
  999. * note that compression is performed only at computing
  1000. * router key at the lowest internal level.
  1001. * further compression of the key between pairs of higher
  1002. * level internal pages loses too much information and
  1003. * the search may fail.
  1004. * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
  1005. * results in two adjacent parent entries (a)(xx).
  1006. * if split occurs between these two entries, and
  1007. * if compression is applied, the router key of parent entry
  1008. * of right page (x) will divert search for x into right
  1009. * subtree and miss x in the left subtree.)
  1010. *
  1011. * the entire key must be retained for the next-to-leftmost
  1012. * internal key at any level of the tree, or search may fail
  1013. * (e.g., ?)
  1014. */
  1015. switch (rp->header.flag & BT_TYPE) {
  1016. case BT_LEAF:
  1017. /*
  1018. * compute the length of prefix for suffix compression
  1019. * between last entry of left page and first entry
  1020. * of right page
  1021. */
  1022. if ((sp->header.flag & BT_ROOT && skip > 1) ||
  1023. sp->header.prev != 0 || skip > 1) {
  1024. /* compute uppercase router prefix key */
  1025. rc = ciGetLeafPrefixKey(lp,
  1026. lp->header.nextindex-1,
  1027. rp, 0, &key,
  1028. sbi->mntflag);
  1029. if (rc) {
  1030. DT_PUTPAGE(lmp);
  1031. DT_PUTPAGE(rmp);
  1032. DT_PUTPAGE(smp);
  1033. goto splitOut;
  1034. }
  1035. } else {
  1036. /* next to leftmost entry of
  1037. lowest internal level */
  1038. /* compute uppercase router key */
  1039. dtGetKey(rp, 0, &key, sbi->mntflag);
  1040. key.name[key.namlen] = 0;
  1041. if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
  1042. ciToUpper(&key);
  1043. }
  1044. n = NDTINTERNAL(key.namlen);
  1045. break;
  1046. case BT_INTERNAL:
  1047. dtGetKey(rp, 0, &key, sbi->mntflag);
  1048. n = NDTINTERNAL(key.namlen);
  1049. break;
  1050. default:
  1051. jfs_err("dtSplitUp(): UFO!");
  1052. break;
  1053. }
  1054. /* unpin left child page */
  1055. DT_PUTPAGE(lmp);
  1056. /*
  1057. * compute the data for the router entry
  1058. */
  1059. data->xd = rpxd; /* child page xd */
  1060. /*
  1061. * parent page is full - split the parent page
  1062. */
  1063. if (n > sp->header.freecnt) {
  1064. /* init for parent page split */
  1065. split->mp = smp;
  1066. split->index = skip; /* index at insert */
  1067. split->nslot = n;
  1068. split->key = &key;
  1069. /* split->data = data; */
  1070. /* unpin right child page */
  1071. DT_PUTPAGE(rmp);
  1072. /* The split routines insert the new entry,
  1073. * acquire txLock as appropriate.
  1074. * return <rp> pinned and its block number <rbn>.
  1075. */
  1076. rc = (sp->header.flag & BT_ROOT) ?
  1077. dtSplitRoot(tid, ip, split, &rmp) :
  1078. dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
  1079. if (rc) {
  1080. DT_PUTPAGE(smp);
  1081. goto splitOut;
  1082. }
  1083. /* smp and rmp are pinned */
  1084. }
  1085. /*
  1086. * parent page is not full - insert router entry in parent page
  1087. */
  1088. else {
  1089. BT_MARK_DIRTY(smp, ip);
  1090. /*
  1091. * acquire a transaction lock on the parent page
  1092. */
  1093. tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
  1094. dtlck = (struct dt_lock *) & tlck->lock;
  1095. ASSERT(dtlck->index == 0);
  1096. lv = & dtlck->lv[0];
  1097. /* linelock header */
  1098. lv->offset = 0;
  1099. lv->length = 1;
  1100. dtlck->index++;
  1101. /* linelock stbl of non-root parent page */
  1102. if (!(sp->header.flag & BT_ROOT)) {
  1103. lv++;
  1104. n = skip >> L2DTSLOTSIZE;
  1105. lv->offset = sp->header.stblindex + n;
  1106. lv->length =
  1107. ((sp->header.nextindex -
  1108. 1) >> L2DTSLOTSIZE) - n + 1;
  1109. dtlck->index++;
  1110. }
  1111. dtInsertEntry(sp, skip, &key, data, &dtlck);
  1112. /* exit propagate up */
  1113. break;
  1114. }
  1115. }
  1116. /* unpin current split and its right page */
  1117. DT_PUTPAGE(smp);
  1118. DT_PUTPAGE(rmp);
  1119. /*
  1120. * free remaining extents allocated for split
  1121. */
  1122. splitOut:
  1123. n = pxdlist.npxd;
  1124. pxd = &pxdlist.pxd[n];
  1125. for (; n < pxdlist.maxnpxd; n++, pxd++)
  1126. dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
  1127. freeKeyName:
  1128. kfree(key.name);
  1129. /* Rollback quota allocation */
  1130. if (rc && quota_allocation)
  1131. DQUOT_FREE_BLOCK(ip, quota_allocation);
  1132. dtSplitUp_Exit:
  1133. return rc;
  1134. }
  1135. /*
  1136. * dtSplitPage()
  1137. *
  1138. * function: Split a non-root page of a btree.
  1139. *
  1140. * parameter:
  1141. *
  1142. * return: 0 - success;
  1143. * errno - failure;
  1144. * return split and new page pinned;
  1145. */
  1146. static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
  1147. struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
  1148. {
  1149. int rc = 0;
  1150. struct metapage *smp;
  1151. dtpage_t *sp;
  1152. struct metapage *rmp;
  1153. dtpage_t *rp; /* new right page allocated */
  1154. s64 rbn; /* new right page block number */
  1155. struct metapage *mp;
  1156. dtpage_t *p;
  1157. s64 nextbn;
  1158. struct pxdlist *pxdlist;
  1159. pxd_t *pxd;
  1160. int skip, nextindex, half, left, nxt, off, si;
  1161. struct ldtentry *ldtentry;
  1162. struct idtentry *idtentry;
  1163. u8 *stbl;
  1164. struct dtslot *f;
  1165. int fsi, stblsize;
  1166. int n;
  1167. struct dt_lock *sdtlck, *rdtlck;
  1168. struct tlock *tlck;
  1169. struct dt_lock *dtlck;
  1170. struct lv *slv, *rlv, *lv;
  1171. /* get split page */
  1172. smp = split->mp;
  1173. sp = DT_PAGE(ip, smp);
  1174. /*
  1175. * allocate the new right page for the split
  1176. */
  1177. pxdlist = split->pxdlist;
  1178. pxd = &pxdlist->pxd[pxdlist->npxd];
  1179. pxdlist->npxd++;
  1180. rbn = addressPXD(pxd);
  1181. rmp = get_metapage(ip, rbn, PSIZE, 1);
  1182. if (rmp == NULL)
  1183. return -EIO;
  1184. /* Allocate blocks to quota. */
  1185. if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
  1186. release_metapage(rmp);
  1187. return -EDQUOT;
  1188. }
  1189. jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
  1190. BT_MARK_DIRTY(rmp, ip);
  1191. /*
  1192. * acquire a transaction lock on the new right page
  1193. */
  1194. tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
  1195. rdtlck = (struct dt_lock *) & tlck->lock;
  1196. rp = (dtpage_t *) rmp->data;
  1197. *rpp = rp;
  1198. rp->header.self = *pxd;
  1199. BT_MARK_DIRTY(smp, ip);
  1200. /*
  1201. * acquire a transaction lock on the split page
  1202. *
  1203. * action:
  1204. */
  1205. tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
  1206. sdtlck = (struct dt_lock *) & tlck->lock;
  1207. /* linelock header of split page */
  1208. ASSERT(sdtlck->index == 0);
  1209. slv = & sdtlck->lv[0];
  1210. slv->offset = 0;
  1211. slv->length = 1;
  1212. sdtlck->index++;
  1213. /*
  1214. * initialize/update sibling pointers between sp and rp
  1215. */
  1216. nextbn = le64_to_cpu(sp->header.next);
  1217. rp->header.next = cpu_to_le64(nextbn);
  1218. rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
  1219. sp->header.next = cpu_to_le64(rbn);
  1220. /*
  1221. * initialize new right page
  1222. */
  1223. rp->header.flag = sp->header.flag;
  1224. /* compute sorted entry table at start of extent data area */
  1225. rp->header.nextindex = 0;
  1226. rp->header.stblindex = 1;
  1227. n = PSIZE >> L2DTSLOTSIZE;
  1228. rp->header.maxslot = n;
  1229. stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
  1230. /* init freelist */
  1231. fsi = rp->header.stblindex + stblsize;
  1232. rp->header.freelist = fsi;
  1233. rp->header.freecnt = rp->header.maxslot - fsi;
  1234. /*
  1235. * sequential append at tail: append without split
  1236. *
  1237. * If splitting the last page on a level because of appending
  1238. * a entry to it (skip is maxentry), it's likely that the access is
  1239. * sequential. Adding an empty page on the side of the level is less
  1240. * work and can push the fill factor much higher than normal.
  1241. * If we're wrong it's no big deal, we'll just do the split the right
  1242. * way next time.
  1243. * (It may look like it's equally easy to do a similar hack for
  1244. * reverse sorted data, that is, split the tree left,
  1245. * but it's not. Be my guest.)
  1246. */
  1247. if (nextbn == 0 && split->index == sp->header.nextindex) {
  1248. /* linelock header + stbl (first slot) of new page */
  1249. rlv = & rdtlck->lv[rdtlck->index];
  1250. rlv->offset = 0;
  1251. rlv->length = 2;
  1252. rdtlck->index++;
  1253. /*
  1254. * initialize freelist of new right page
  1255. */
  1256. f = &rp->slot[fsi];
  1257. for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
  1258. f->next = fsi;
  1259. f->next = -1;
  1260. /* insert entry at the first entry of the new right page */
  1261. dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
  1262. goto out;
  1263. }
  1264. /*
  1265. * non-sequential insert (at possibly middle page)
  1266. */
  1267. /*
  1268. * update prev pointer of previous right sibling page;
  1269. */
  1270. if (nextbn != 0) {
  1271. DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  1272. if (rc) {
  1273. discard_metapage(rmp);
  1274. return rc;
  1275. }
  1276. BT_MARK_DIRTY(mp, ip);
  1277. /*
  1278. * acquire a transaction lock on the next page
  1279. */
  1280. tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
  1281. jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
  1282. tlck, ip, mp);
  1283. dtlck = (struct dt_lock *) & tlck->lock;
  1284. /* linelock header of previous right sibling page */
  1285. lv = & dtlck->lv[dtlck->index];
  1286. lv->offset = 0;
  1287. lv->length = 1;
  1288. dtlck->index++;
  1289. p->header.prev = cpu_to_le64(rbn);
  1290. DT_PUTPAGE(mp);
  1291. }
  1292. /*
  1293. * split the data between the split and right pages.
  1294. */
  1295. skip = split->index;
  1296. half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
  1297. left = 0;
  1298. /*
  1299. * compute fill factor for split pages
  1300. *
  1301. * <nxt> traces the next entry to move to rp
  1302. * <off> traces the next entry to stay in sp
  1303. */
  1304. stbl = (u8 *) & sp->slot[sp->header.stblindex];
  1305. nextindex = sp->header.nextindex;
  1306. for (nxt = off = 0; nxt < nextindex; ++off) {
  1307. if (off == skip)
  1308. /* check for fill factor with new entry size */
  1309. n = split->nslot;
  1310. else {
  1311. si = stbl[nxt];
  1312. switch (sp->header.flag & BT_TYPE) {
  1313. case BT_LEAF:
  1314. ldtentry = (struct ldtentry *) & sp->slot[si];
  1315. if (DO_INDEX(ip))
  1316. n = NDTLEAF(ldtentry->namlen);
  1317. else
  1318. n = NDTLEAF_LEGACY(ldtentry->
  1319. namlen);
  1320. break;
  1321. case BT_INTERNAL:
  1322. idtentry = (struct idtentry *) & sp->slot[si];
  1323. n = NDTINTERNAL(idtentry->namlen);
  1324. break;
  1325. default:
  1326. break;
  1327. }
  1328. ++nxt; /* advance to next entry to move in sp */
  1329. }
  1330. left += n;
  1331. if (left >= half)
  1332. break;
  1333. }
  1334. /* <nxt> poins to the 1st entry to move */
  1335. /*
  1336. * move entries to right page
  1337. *
  1338. * dtMoveEntry() initializes rp and reserves entry for insertion
  1339. *
  1340. * split page moved out entries are linelocked;
  1341. * new/right page moved in entries are linelocked;
  1342. */
  1343. /* linelock header + stbl of new right page */
  1344. rlv = & rdtlck->lv[rdtlck->index];
  1345. rlv->offset = 0;
  1346. rlv->length = 5;
  1347. rdtlck->index++;
  1348. dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
  1349. sp->header.nextindex = nxt;
  1350. /*
  1351. * finalize freelist of new right page
  1352. */
  1353. fsi = rp->header.freelist;
  1354. f = &rp->slot[fsi];
  1355. for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
  1356. f->next = fsi;
  1357. f->next = -1;
  1358. /*
  1359. * Update directory index table for entries now in right page
  1360. */
  1361. if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
  1362. s64 lblock;
  1363. mp = NULL;
  1364. stbl = DT_GETSTBL(rp);
  1365. for (n = 0; n < rp->header.nextindex; n++) {
  1366. ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
  1367. modify_index(tid, ip, le32_to_cpu(ldtentry->index),
  1368. rbn, n, &mp, &lblock);
  1369. }
  1370. if (mp)
  1371. release_metapage(mp);
  1372. }
  1373. /*
  1374. * the skipped index was on the left page,
  1375. */
  1376. if (skip <= off) {
  1377. /* insert the new entry in the split page */
  1378. dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
  1379. /* linelock stbl of split page */
  1380. if (sdtlck->index >= sdtlck->maxcnt)
  1381. sdtlck = (struct dt_lock *) txLinelock(sdtlck);
  1382. slv = & sdtlck->lv[sdtlck->index];
  1383. n = skip >> L2DTSLOTSIZE;
  1384. slv->offset = sp->header.stblindex + n;
  1385. slv->length =
  1386. ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
  1387. sdtlck->index++;
  1388. }
  1389. /*
  1390. * the skipped index was on the right page,
  1391. */
  1392. else {
  1393. /* adjust the skip index to reflect the new position */
  1394. skip -= nxt;
  1395. /* insert the new entry in the right page */
  1396. dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
  1397. }
  1398. out:
  1399. *rmpp = rmp;
  1400. *rpxdp = *pxd;
  1401. return rc;
  1402. }
  1403. /*
  1404. * dtExtendPage()
  1405. *
  1406. * function: extend 1st/only directory leaf page
  1407. *
  1408. * parameter:
  1409. *
  1410. * return: 0 - success;
  1411. * errno - failure;
  1412. * return extended page pinned;
  1413. */
  1414. static int dtExtendPage(tid_t tid,
  1415. struct inode *ip, struct dtsplit * split, struct btstack * btstack)
  1416. {
  1417. struct super_block *sb = ip->i_sb;
  1418. int rc;
  1419. struct metapage *smp, *pmp, *mp;
  1420. dtpage_t *sp, *pp;
  1421. struct pxdlist *pxdlist;
  1422. pxd_t *pxd, *tpxd;
  1423. int xlen, xsize;
  1424. int newstblindex, newstblsize;
  1425. int oldstblindex, oldstblsize;
  1426. int fsi, last;
  1427. struct dtslot *f;
  1428. struct btframe *parent;
  1429. int n;
  1430. struct dt_lock *dtlck;
  1431. s64 xaddr, txaddr;
  1432. struct tlock *tlck;
  1433. struct pxd_lock *pxdlock;
  1434. struct lv *lv;
  1435. uint type;
  1436. struct ldtentry *ldtentry;
  1437. u8 *stbl;
  1438. /* get page to extend */
  1439. smp = split->mp;
  1440. sp = DT_PAGE(ip, smp);
  1441. /* get parent/root page */
  1442. parent = BT_POP(btstack);
  1443. DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
  1444. if (rc)
  1445. return (rc);
  1446. /*
  1447. * extend the extent
  1448. */
  1449. pxdlist = split->pxdlist;
  1450. pxd = &pxdlist->pxd[pxdlist->npxd];
  1451. pxdlist->npxd++;
  1452. xaddr = addressPXD(pxd);
  1453. tpxd = &sp->header.self;
  1454. txaddr = addressPXD(tpxd);
  1455. /* in-place extension */
  1456. if (xaddr == txaddr) {
  1457. type = tlckEXTEND;
  1458. }
  1459. /* relocation */
  1460. else {
  1461. type = tlckNEW;
  1462. /* save moved extent descriptor for later free */
  1463. tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
  1464. pxdlock = (struct pxd_lock *) & tlck->lock;
  1465. pxdlock->flag = mlckFREEPXD;
  1466. pxdlock->pxd = sp->header.self;
  1467. pxdlock->index = 1;
  1468. /*
  1469. * Update directory index table to reflect new page address
  1470. */
  1471. if (DO_INDEX(ip)) {
  1472. s64 lblock;
  1473. mp = NULL;
  1474. stbl = DT_GETSTBL(sp);
  1475. for (n = 0; n < sp->header.nextindex; n++) {
  1476. ldtentry =
  1477. (struct ldtentry *) & sp->slot[stbl[n]];
  1478. modify_index(tid, ip,
  1479. le32_to_cpu(ldtentry->index),
  1480. xaddr, n, &mp, &lblock);
  1481. }
  1482. if (mp)
  1483. release_metapage(mp);
  1484. }
  1485. }
  1486. /*
  1487. * extend the page
  1488. */
  1489. sp->header.self = *pxd;
  1490. jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
  1491. BT_MARK_DIRTY(smp, ip);
  1492. /*
  1493. * acquire a transaction lock on the extended/leaf page
  1494. */
  1495. tlck = txLock(tid, ip, smp, tlckDTREE | type);
  1496. dtlck = (struct dt_lock *) & tlck->lock;
  1497. lv = & dtlck->lv[0];
  1498. /* update buffer extent descriptor of extended page */
  1499. xlen = lengthPXD(pxd);
  1500. xsize = xlen << JFS_SBI(sb)->l2bsize;
  1501. #ifdef _STILL_TO_PORT
  1502. bmSetXD(smp, xaddr, xsize);
  1503. #endif /* _STILL_TO_PORT */
  1504. /*
  1505. * copy old stbl to new stbl at start of extended area
  1506. */
  1507. oldstblindex = sp->header.stblindex;
  1508. oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
  1509. newstblindex = sp->header.maxslot;
  1510. n = xsize >> L2DTSLOTSIZE;
  1511. newstblsize = (n + 31) >> L2DTSLOTSIZE;
  1512. memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
  1513. sp->header.nextindex);
  1514. /*
  1515. * in-line extension: linelock old area of extended page
  1516. */
  1517. if (type == tlckEXTEND) {
  1518. /* linelock header */
  1519. lv->offset = 0;
  1520. lv->length = 1;
  1521. dtlck->index++;
  1522. lv++;
  1523. /* linelock new stbl of extended page */
  1524. lv->offset = newstblindex;
  1525. lv->length = newstblsize;
  1526. }
  1527. /*
  1528. * relocation: linelock whole relocated area
  1529. */
  1530. else {
  1531. lv->offset = 0;
  1532. lv->length = sp->header.maxslot + newstblsize;
  1533. }
  1534. dtlck->index++;
  1535. sp->header.maxslot = n;
  1536. sp->header.stblindex = newstblindex;
  1537. /* sp->header.nextindex remains the same */
  1538. /*
  1539. * add old stbl region at head of freelist
  1540. */
  1541. fsi = oldstblindex;
  1542. f = &sp->slot[fsi];
  1543. last = sp->header.freelist;
  1544. for (n = 0; n < oldstblsize; n++, fsi++, f++) {
  1545. f->next = last;
  1546. last = fsi;
  1547. }
  1548. sp->header.freelist = last;
  1549. sp->header.freecnt += oldstblsize;
  1550. /*
  1551. * append free region of newly extended area at tail of freelist
  1552. */
  1553. /* init free region of newly extended area */
  1554. fsi = n = newstblindex + newstblsize;
  1555. f = &sp->slot[fsi];
  1556. for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
  1557. f->next = fsi;
  1558. f->next = -1;
  1559. /* append new free region at tail of old freelist */
  1560. fsi = sp->header.freelist;
  1561. if (fsi == -1)
  1562. sp->header.freelist = n;
  1563. else {
  1564. do {
  1565. f = &sp->slot[fsi];
  1566. fsi = f->next;
  1567. } while (fsi != -1);
  1568. f->next = n;
  1569. }
  1570. sp->header.freecnt += sp->header.maxslot - n;
  1571. /*
  1572. * insert the new entry
  1573. */
  1574. dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
  1575. BT_MARK_DIRTY(pmp, ip);
  1576. /*
  1577. * linelock any freeslots residing in old extent
  1578. */
  1579. if (type == tlckEXTEND) {
  1580. n = sp->header.maxslot >> 2;
  1581. if (sp->header.freelist < n)
  1582. dtLinelockFreelist(sp, n, &dtlck);
  1583. }
  1584. /*
  1585. * update parent entry on the parent/root page
  1586. */
  1587. /*
  1588. * acquire a transaction lock on the parent/root page
  1589. */
  1590. tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
  1591. dtlck = (struct dt_lock *) & tlck->lock;
  1592. lv = & dtlck->lv[dtlck->index];
  1593. /* linelock parent entry - 1st slot */
  1594. lv->offset = 1;
  1595. lv->length = 1;
  1596. dtlck->index++;
  1597. /* update the parent pxd for page extension */
  1598. tpxd = (pxd_t *) & pp->slot[1];
  1599. *tpxd = *pxd;
  1600. DT_PUTPAGE(pmp);
  1601. return 0;
  1602. }
  1603. /*
  1604. * dtSplitRoot()
  1605. *
  1606. * function:
  1607. * split the full root page into
  1608. * original/root/split page and new right page
  1609. * i.e., root remains fixed in tree anchor (inode) and
  1610. * the root is copied to a single new right child page
  1611. * since root page << non-root page, and
  1612. * the split root page contains a single entry for the
  1613. * new right child page.
  1614. *
  1615. * parameter:
  1616. *
  1617. * return: 0 - success;
  1618. * errno - failure;
  1619. * return new page pinned;
  1620. */
  1621. static int dtSplitRoot(tid_t tid,
  1622. struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
  1623. {
  1624. struct super_block *sb = ip->i_sb;
  1625. struct metapage *smp;
  1626. dtroot_t *sp;
  1627. struct metapage *rmp;
  1628. dtpage_t *rp;
  1629. s64 rbn;
  1630. int xlen;
  1631. int xsize;
  1632. struct dtslot *f;
  1633. s8 *stbl;
  1634. int fsi, stblsize, n;
  1635. struct idtentry *s;
  1636. pxd_t *ppxd;
  1637. struct pxdlist *pxdlist;
  1638. pxd_t *pxd;
  1639. struct dt_lock *dtlck;
  1640. struct tlock *tlck;
  1641. struct lv *lv;
  1642. /* get split root page */
  1643. smp = split->mp;
  1644. sp = &JFS_IP(ip)->i_dtroot;
  1645. /*
  1646. * allocate/initialize a single (right) child page
  1647. *
  1648. * N.B. at first split, a one (or two) block to fit new entry
  1649. * is allocated; at subsequent split, a full page is allocated;
  1650. */
  1651. pxdlist = split->pxdlist;
  1652. pxd = &pxdlist->pxd[pxdlist->npxd];
  1653. pxdlist->npxd++;
  1654. rbn = addressPXD(pxd);
  1655. xlen = lengthPXD(pxd);
  1656. xsize = xlen << JFS_SBI(sb)->l2bsize;
  1657. rmp = get_metapage(ip, rbn, xsize, 1);
  1658. if (!rmp)
  1659. return -EIO;
  1660. rp = rmp->data;
  1661. /* Allocate blocks to quota. */
  1662. if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
  1663. release_metapage(rmp);
  1664. return -EDQUOT;
  1665. }
  1666. BT_MARK_DIRTY(rmp, ip);
  1667. /*
  1668. * acquire a transaction lock on the new right page
  1669. */
  1670. tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
  1671. dtlck = (struct dt_lock *) & tlck->lock;
  1672. rp->header.flag =
  1673. (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
  1674. rp->header.self = *pxd;
  1675. /* initialize sibling pointers */
  1676. rp->header.next = 0;
  1677. rp->header.prev = 0;
  1678. /*
  1679. * move in-line root page into new right page extent
  1680. */
  1681. /* linelock header + copied entries + new stbl (1st slot) in new page */
  1682. ASSERT(dtlck->index == 0);
  1683. lv = & dtlck->lv[0];
  1684. lv->offset = 0;
  1685. lv->length = 10; /* 1 + 8 + 1 */
  1686. dtlck->index++;
  1687. n = xsize >> L2DTSLOTSIZE;
  1688. rp->header.maxslot = n;
  1689. stblsize = (n + 31) >> L2DTSLOTSIZE;
  1690. /* copy old stbl to new stbl at start of extended area */
  1691. rp->header.stblindex = DTROOTMAXSLOT;
  1692. stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
  1693. memcpy(stbl, sp->header.stbl, sp->header.nextindex);
  1694. rp->header.nextindex = sp->header.nextindex;
  1695. /* copy old data area to start of new data area */
  1696. memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
  1697. /*
  1698. * append free region of newly extended area at tail of freelist
  1699. */
  1700. /* init free region of newly extended area */
  1701. fsi = n = DTROOTMAXSLOT + stblsize;
  1702. f = &rp->slot[fsi];
  1703. for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
  1704. f->next = fsi;
  1705. f->next = -1;
  1706. /* append new free region at tail of old freelist */
  1707. fsi = sp->header.freelist;
  1708. if (fsi == -1)
  1709. rp->header.freelist = n;
  1710. else {
  1711. rp->header.freelist = fsi;
  1712. do {
  1713. f = &rp->slot[fsi];
  1714. fsi = f->next;
  1715. } while (fsi != -1);
  1716. f->next = n;
  1717. }
  1718. rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
  1719. /*
  1720. * Update directory index table for entries now in right page
  1721. */
  1722. if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
  1723. s64 lblock;
  1724. struct metapage *mp = NULL;
  1725. struct ldtentry *ldtentry;
  1726. stbl = DT_GETSTBL(rp);
  1727. for (n = 0; n < rp->header.nextindex; n++) {
  1728. ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
  1729. modify_index(tid, ip, le32_to_cpu(ldtentry->index),
  1730. rbn, n, &mp, &lblock);
  1731. }
  1732. if (mp)
  1733. release_metapage(mp);
  1734. }
  1735. /*
  1736. * insert the new entry into the new right/child page
  1737. * (skip index in the new right page will not change)
  1738. */
  1739. dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
  1740. /*
  1741. * reset parent/root page
  1742. *
  1743. * set the 1st entry offset to 0, which force the left-most key
  1744. * at any level of the tree to be less than any search key.
  1745. *
  1746. * The btree comparison code guarantees that the left-most key on any
  1747. * level of the tree is never used, so it doesn't need to be filled in.
  1748. */
  1749. BT_MARK_DIRTY(smp, ip);
  1750. /*
  1751. * acquire a transaction lock on the root page (in-memory inode)
  1752. */
  1753. tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
  1754. dtlck = (struct dt_lock *) & tlck->lock;
  1755. /* linelock root */
  1756. ASSERT(dtlck->index == 0);
  1757. lv = & dtlck->lv[0];
  1758. lv->offset = 0;
  1759. lv->length = DTROOTMAXSLOT;
  1760. dtlck->index++;
  1761. /* update page header of root */
  1762. if (sp->header.flag & BT_LEAF) {
  1763. sp->header.flag &= ~BT_LEAF;
  1764. sp->header.flag |= BT_INTERNAL;
  1765. }
  1766. /* init the first entry */
  1767. s = (struct idtentry *) & sp->slot[DTENTRYSTART];
  1768. ppxd = (pxd_t *) s;
  1769. *ppxd = *pxd;
  1770. s->next = -1;
  1771. s->namlen = 0;
  1772. stbl = sp->header.stbl;
  1773. stbl[0] = DTENTRYSTART;
  1774. sp->header.nextindex = 1;
  1775. /* init freelist */
  1776. fsi = DTENTRYSTART + 1;
  1777. f = &sp->slot[fsi];
  1778. /* init free region of remaining area */
  1779. for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
  1780. f->next = fsi;
  1781. f->next = -1;
  1782. sp->header.freelist = DTENTRYSTART + 1;
  1783. sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
  1784. *rmpp = rmp;
  1785. return 0;
  1786. }
  1787. /*
  1788. * dtDelete()
  1789. *
  1790. * function: delete the entry(s) referenced by a key.
  1791. *
  1792. * parameter:
  1793. *
  1794. * return:
  1795. */
  1796. int dtDelete(tid_t tid,
  1797. struct inode *ip, struct component_name * key, ino_t * ino, int flag)
  1798. {
  1799. int rc = 0;
  1800. s64 bn;
  1801. struct metapage *mp, *imp;
  1802. dtpage_t *p;
  1803. int index;
  1804. struct btstack btstack;
  1805. struct dt_lock *dtlck;
  1806. struct tlock *tlck;
  1807. struct lv *lv;
  1808. int i;
  1809. struct ldtentry *ldtentry;
  1810. u8 *stbl;
  1811. u32 table_index, next_index;
  1812. struct metapage *nmp;
  1813. dtpage_t *np;
  1814. /*
  1815. * search for the entry to delete:
  1816. *
  1817. * dtSearch() returns (leaf page pinned, index at which to delete).
  1818. */
  1819. if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
  1820. return rc;
  1821. /* retrieve search result */
  1822. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1823. /*
  1824. * We need to find put the index of the next entry into the
  1825. * directory index table in order to resume a readdir from this
  1826. * entry.
  1827. */
  1828. if (DO_INDEX(ip)) {
  1829. stbl = DT_GETSTBL(p);
  1830. ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
  1831. table_index = le32_to_cpu(ldtentry->index);
  1832. if (index == (p->header.nextindex - 1)) {
  1833. /*
  1834. * Last entry in this leaf page
  1835. */
  1836. if ((p->header.flag & BT_ROOT)
  1837. || (p->header.next == 0))
  1838. next_index = -1;
  1839. else {
  1840. /* Read next leaf page */
  1841. DT_GETPAGE(ip, le64_to_cpu(p->header.next),
  1842. nmp, PSIZE, np, rc);
  1843. if (rc)
  1844. next_index = -1;
  1845. else {
  1846. stbl = DT_GETSTBL(np);
  1847. ldtentry =
  1848. (struct ldtentry *) & np->
  1849. slot[stbl[0]];
  1850. next_index =
  1851. le32_to_cpu(ldtentry->index);
  1852. DT_PUTPAGE(nmp);
  1853. }
  1854. }
  1855. } else {
  1856. ldtentry =
  1857. (struct ldtentry *) & p->slot[stbl[index + 1]];
  1858. next_index = le32_to_cpu(ldtentry->index);
  1859. }
  1860. free_index(tid, ip, table_index, next_index);
  1861. }
  1862. /*
  1863. * the leaf page becomes empty, delete the page
  1864. */
  1865. if (p->header.nextindex == 1) {
  1866. /* delete empty page */
  1867. rc = dtDeleteUp(tid, ip, mp, p, &btstack);
  1868. }
  1869. /*
  1870. * the leaf page has other entries remaining:
  1871. *
  1872. * delete the entry from the leaf page.
  1873. */
  1874. else {
  1875. BT_MARK_DIRTY(mp, ip);
  1876. /*
  1877. * acquire a transaction lock on the leaf page
  1878. */
  1879. tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
  1880. dtlck = (struct dt_lock *) & tlck->lock;
  1881. /*
  1882. * Do not assume that dtlck->index will be zero. During a
  1883. * rename within a directory, this transaction may have
  1884. * modified this page already when adding the new entry.
  1885. */
  1886. /* linelock header */
  1887. if (dtlck->index >= dtlck->maxcnt)
  1888. dtlck = (struct dt_lock *) txLinelock(dtlck);
  1889. lv = & dtlck->lv[dtlck->index];
  1890. lv->offset = 0;
  1891. lv->length = 1;
  1892. dtlck->index++;
  1893. /* linelock stbl of non-root leaf page */
  1894. if (!(p->header.flag & BT_ROOT)) {
  1895. if (dtlck->index >= dtlck->maxcnt)
  1896. dtlck = (struct dt_lock *) txLinelock(dtlck);
  1897. lv = & dtlck->lv[dtlck->index];
  1898. i = index >> L2DTSLOTSIZE;
  1899. lv->offset = p->header.stblindex + i;
  1900. lv->length =
  1901. ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
  1902. i + 1;
  1903. dtlck->index++;
  1904. }
  1905. /* free the leaf entry */
  1906. dtDeleteEntry(p, index, &dtlck);
  1907. /*
  1908. * Update directory index table for entries moved in stbl
  1909. */
  1910. if (DO_INDEX(ip) && index < p->header.nextindex) {
  1911. s64 lblock;
  1912. imp = NULL;
  1913. stbl = DT_GETSTBL(p);
  1914. for (i = index; i < p->header.nextindex; i++) {
  1915. ldtentry =
  1916. (struct ldtentry *) & p->slot[stbl[i]];
  1917. modify_index(tid, ip,
  1918. le32_to_cpu(ldtentry->index),
  1919. bn, i, &imp, &lblock);
  1920. }
  1921. if (imp)
  1922. release_metapage(imp);
  1923. }
  1924. DT_PUTPAGE(mp);
  1925. }
  1926. return rc;
  1927. }
  1928. /*
  1929. * dtDeleteUp()
  1930. *
  1931. * function:
  1932. * free empty pages as propagating deletion up the tree
  1933. *
  1934. * parameter:
  1935. *
  1936. * return:
  1937. */
  1938. static int dtDeleteUp(tid_t tid, struct inode *ip,
  1939. struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
  1940. {
  1941. int rc = 0;
  1942. struct metapage *mp;
  1943. dtpage_t *p;
  1944. int index, nextindex;
  1945. int xlen;
  1946. struct btframe *parent;
  1947. struct dt_lock *dtlck;
  1948. struct tlock *tlck;
  1949. struct lv *lv;
  1950. struct pxd_lock *pxdlock;
  1951. int i;
  1952. /*
  1953. * keep the root leaf page which has become empty
  1954. */
  1955. if (BT_IS_ROOT(fmp)) {
  1956. /*
  1957. * reset the root
  1958. *
  1959. * dtInitRoot() acquires txlock on the root
  1960. */
  1961. dtInitRoot(tid, ip, PARENT(ip));
  1962. DT_PUTPAGE(fmp);
  1963. return 0;
  1964. }
  1965. /*
  1966. * free the non-root leaf page
  1967. */
  1968. /*
  1969. * acquire a transaction lock on the page
  1970. *
  1971. * write FREEXTENT|NOREDOPAGE log record
  1972. * N.B. linelock is overlaid as freed extent descriptor, and
  1973. * the buffer page is freed;
  1974. */
  1975. tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
  1976. pxdlock = (struct pxd_lock *) & tlck->lock;
  1977. pxdlock->flag = mlckFREEPXD;
  1978. pxdlock->pxd = fp->header.self;
  1979. pxdlock->index = 1;
  1980. /* update sibling pointers */
  1981. if ((rc = dtRelink(tid, ip, fp))) {
  1982. BT_PUTPAGE(fmp);
  1983. return rc;
  1984. }
  1985. xlen = lengthPXD(&fp->header.self);
  1986. /* Free quota allocation. */
  1987. DQUOT_FREE_BLOCK(ip, xlen);
  1988. /* free/invalidate its buffer page */
  1989. discard_metapage(fmp);
  1990. /*
  1991. * propagate page deletion up the directory tree
  1992. *
  1993. * If the delete from the parent page makes it empty,
  1994. * continue all the way up the tree.
  1995. * stop if the root page is reached (which is never deleted) or
  1996. * if the entry deletion does not empty the page.
  1997. */
  1998. while ((parent = BT_POP(btstack)) != NULL) {
  1999. /* pin the parent page <sp> */
  2000. DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
  2001. if (rc)
  2002. return rc;
  2003. /*
  2004. * free the extent of the child page deleted
  2005. */
  2006. index = parent->index;
  2007. /*
  2008. * delete the entry for the child page from parent
  2009. */
  2010. nextindex = p->header.nextindex;
  2011. /*
  2012. * the parent has the single entry being deleted:
  2013. *
  2014. * free the parent page which has become empty.
  2015. */
  2016. if (nextindex == 1) {
  2017. /*
  2018. * keep the root internal page which has become empty
  2019. */
  2020. if (p->header.flag & BT_ROOT) {
  2021. /*
  2022. * reset the root
  2023. *
  2024. * dtInitRoot() acquires txlock on the root
  2025. */
  2026. dtInitRoot(tid, ip, PARENT(ip));
  2027. DT_PUTPAGE(mp);
  2028. return 0;
  2029. }
  2030. /*
  2031. * free the parent page
  2032. */
  2033. else {
  2034. /*
  2035. * acquire a transaction lock on the page
  2036. *
  2037. * write FREEXTENT|NOREDOPAGE log record
  2038. */
  2039. tlck =
  2040. txMaplock(tid, ip,
  2041. tlckDTREE | tlckFREE);
  2042. pxdlock = (struct pxd_lock *) & tlck->lock;
  2043. pxdlock->flag = mlckFREEPXD;
  2044. pxdlock->pxd = p->header.self;
  2045. pxdlock->index = 1;
  2046. /* update sibling pointers */
  2047. if ((rc = dtRelink(tid, ip, p))) {
  2048. DT_PUTPAGE(mp);
  2049. return rc;
  2050. }
  2051. xlen = lengthPXD(&p->header.self);
  2052. /* Free quota allocation */
  2053. DQUOT_FREE_BLOCK(ip, xlen);
  2054. /* free/invalidate its buffer page */
  2055. discard_metapage(mp);
  2056. /* propagate up */
  2057. continue;
  2058. }
  2059. }
  2060. /*
  2061. * the parent has other entries remaining:
  2062. *
  2063. * delete the router entry from the parent page.
  2064. */
  2065. BT_MARK_DIRTY(mp, ip);
  2066. /*
  2067. * acquire a transaction lock on the page
  2068. *
  2069. * action: router entry deletion
  2070. */
  2071. tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
  2072. dtlck = (struct dt_lock *) & tlck->lock;
  2073. /* linelock header */
  2074. if (dtlck->index >= dtlck->maxcnt)
  2075. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2076. lv = & dtlck->lv[dtlck->index];
  2077. lv->offset = 0;
  2078. lv->length = 1;
  2079. dtlck->index++;
  2080. /* linelock stbl of non-root leaf page */
  2081. if (!(p->header.flag & BT_ROOT)) {
  2082. if (dtlck->index < dtlck->maxcnt)
  2083. lv++;
  2084. else {
  2085. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2086. lv = & dtlck->lv[0];
  2087. }
  2088. i = index >> L2DTSLOTSIZE;
  2089. lv->offset = p->header.stblindex + i;
  2090. lv->length =
  2091. ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
  2092. i + 1;
  2093. dtlck->index++;
  2094. }
  2095. /* free the router entry */
  2096. dtDeleteEntry(p, index, &dtlck);
  2097. /* reset key of new leftmost entry of level (for consistency) */
  2098. if (index == 0 &&
  2099. ((p->header.flag & BT_ROOT) || p->header.prev == 0))
  2100. dtTruncateEntry(p, 0, &dtlck);
  2101. /* unpin the parent page */
  2102. DT_PUTPAGE(mp);
  2103. /* exit propagation up */
  2104. break;
  2105. }
  2106. return 0;
  2107. }
  2108. #ifdef _NOTYET
  2109. /*
  2110. * NAME: dtRelocate()
  2111. *
  2112. * FUNCTION: relocate dtpage (internal or leaf) of directory;
  2113. * This function is mainly used by defragfs utility.
  2114. */
  2115. int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
  2116. s64 nxaddr)
  2117. {
  2118. int rc = 0;
  2119. struct metapage *mp, *pmp, *lmp, *rmp;
  2120. dtpage_t *p, *pp, *rp = 0, *lp= 0;
  2121. s64 bn;
  2122. int index;
  2123. struct btstack btstack;
  2124. pxd_t *pxd;
  2125. s64 oxaddr, nextbn, prevbn;
  2126. int xlen, xsize;
  2127. struct tlock *tlck;
  2128. struct dt_lock *dtlck;
  2129. struct pxd_lock *pxdlock;
  2130. s8 *stbl;
  2131. struct lv *lv;
  2132. oxaddr = addressPXD(opxd);
  2133. xlen = lengthPXD(opxd);
  2134. jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
  2135. (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
  2136. xlen);
  2137. /*
  2138. * 1. get the internal parent dtpage covering
  2139. * router entry for the tartget page to be relocated;
  2140. */
  2141. rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
  2142. if (rc)
  2143. return rc;
  2144. /* retrieve search result */
  2145. DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2146. jfs_info("dtRelocate: parent router entry validated.");
  2147. /*
  2148. * 2. relocate the target dtpage
  2149. */
  2150. /* read in the target page from src extent */
  2151. DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
  2152. if (rc) {
  2153. /* release the pinned parent page */
  2154. DT_PUTPAGE(pmp);
  2155. return rc;
  2156. }
  2157. /*
  2158. * read in sibling pages if any to update sibling pointers;
  2159. */
  2160. rmp = NULL;
  2161. if (p->header.next) {
  2162. nextbn = le64_to_cpu(p->header.next);
  2163. DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
  2164. if (rc) {
  2165. DT_PUTPAGE(mp);
  2166. DT_PUTPAGE(pmp);
  2167. return (rc);
  2168. }
  2169. }
  2170. lmp = NULL;
  2171. if (p->header.prev) {
  2172. prevbn = le64_to_cpu(p->header.prev);
  2173. DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
  2174. if (rc) {
  2175. DT_PUTPAGE(mp);
  2176. DT_PUTPAGE(pmp);
  2177. if (rmp)
  2178. DT_PUTPAGE(rmp);
  2179. return (rc);
  2180. }
  2181. }
  2182. /* at this point, all xtpages to be updated are in memory */
  2183. /*
  2184. * update sibling pointers of sibling dtpages if any;
  2185. */
  2186. if (lmp) {
  2187. tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
  2188. dtlck = (struct dt_lock *) & tlck->lock;
  2189. /* linelock header */
  2190. ASSERT(dtlck->index == 0);
  2191. lv = & dtlck->lv[0];
  2192. lv->offset = 0;
  2193. lv->length = 1;
  2194. dtlck->index++;
  2195. lp->header.next = cpu_to_le64(nxaddr);
  2196. DT_PUTPAGE(lmp);
  2197. }
  2198. if (rmp) {
  2199. tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
  2200. dtlck = (struct dt_lock *) & tlck->lock;
  2201. /* linelock header */
  2202. ASSERT(dtlck->index == 0);
  2203. lv = & dtlck->lv[0];
  2204. lv->offset = 0;
  2205. lv->length = 1;
  2206. dtlck->index++;
  2207. rp->header.prev = cpu_to_le64(nxaddr);
  2208. DT_PUTPAGE(rmp);
  2209. }
  2210. /*
  2211. * update the target dtpage to be relocated
  2212. *
  2213. * write LOG_REDOPAGE of LOG_NEW type for dst page
  2214. * for the whole target page (logredo() will apply
  2215. * after image and update bmap for allocation of the
  2216. * dst extent), and update bmap for allocation of
  2217. * the dst extent;
  2218. */
  2219. tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
  2220. dtlck = (struct dt_lock *) & tlck->lock;
  2221. /* linelock header */
  2222. ASSERT(dtlck->index == 0);
  2223. lv = & dtlck->lv[0];
  2224. /* update the self address in the dtpage header */
  2225. pxd = &p->header.self;
  2226. PXDaddress(pxd, nxaddr);
  2227. /* the dst page is the same as the src page, i.e.,
  2228. * linelock for afterimage of the whole page;
  2229. */
  2230. lv->offset = 0;
  2231. lv->length = p->header.maxslot;
  2232. dtlck->index++;
  2233. /* update the buffer extent descriptor of the dtpage */
  2234. xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2235. #ifdef _STILL_TO_PORT
  2236. bmSetXD(mp, nxaddr, xsize);
  2237. #endif /* _STILL_TO_PORT */
  2238. /* unpin the relocated page */
  2239. DT_PUTPAGE(mp);
  2240. jfs_info("dtRelocate: target dtpage relocated.");
  2241. /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
  2242. * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
  2243. * will also force a bmap update ).
  2244. */
  2245. /*
  2246. * 3. acquire maplock for the source extent to be freed;
  2247. */
  2248. /* for dtpage relocation, write a LOG_NOREDOPAGE record
  2249. * for the source dtpage (logredo() will init NoRedoPage
  2250. * filter and will also update bmap for free of the source
  2251. * dtpage), and upadte bmap for free of the source dtpage;
  2252. */
  2253. tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
  2254. pxdlock = (struct pxd_lock *) & tlck->lock;
  2255. pxdlock->flag = mlckFREEPXD;
  2256. PXDaddress(&pxdlock->pxd, oxaddr);
  2257. PXDlength(&pxdlock->pxd, xlen);
  2258. pxdlock->index = 1;
  2259. /*
  2260. * 4. update the parent router entry for relocation;
  2261. *
  2262. * acquire tlck for the parent entry covering the target dtpage;
  2263. * write LOG_REDOPAGE to apply after image only;
  2264. */
  2265. jfs_info("dtRelocate: update parent router entry.");
  2266. tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
  2267. dtlck = (struct dt_lock *) & tlck->lock;
  2268. lv = & dtlck->lv[dtlck->index];
  2269. /* update the PXD with the new address */
  2270. stbl = DT_GETSTBL(pp);
  2271. pxd = (pxd_t *) & pp->slot[stbl[index]];
  2272. PXDaddress(pxd, nxaddr);
  2273. lv->offset = stbl[index];
  2274. lv->length = 1;
  2275. dtlck->index++;
  2276. /* unpin the parent dtpage */
  2277. DT_PUTPAGE(pmp);
  2278. return rc;
  2279. }
  2280. /*
  2281. * NAME: dtSearchNode()
  2282. *
  2283. * FUNCTION: Search for an dtpage containing a specified address
  2284. * This function is mainly used by defragfs utility.
  2285. *
  2286. * NOTE: Search result on stack, the found page is pinned at exit.
  2287. * The result page must be an internal dtpage.
  2288. * lmxaddr give the address of the left most page of the
  2289. * dtree level, in which the required dtpage resides.
  2290. */
  2291. static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
  2292. struct btstack * btstack)
  2293. {
  2294. int rc = 0;
  2295. s64 bn;
  2296. struct metapage *mp;
  2297. dtpage_t *p;
  2298. int psize = 288; /* initial in-line directory */
  2299. s8 *stbl;
  2300. int i;
  2301. pxd_t *pxd;
  2302. struct btframe *btsp;
  2303. BT_CLR(btstack); /* reset stack */
  2304. /*
  2305. * descend tree to the level with specified leftmost page
  2306. *
  2307. * by convention, root bn = 0.
  2308. */
  2309. for (bn = 0;;) {
  2310. /* get/pin the page to search */
  2311. DT_GETPAGE(ip, bn, mp, psize, p, rc);
  2312. if (rc)
  2313. return rc;
  2314. /* does the xaddr of leftmost page of the levevl
  2315. * matches levevl search key ?
  2316. */
  2317. if (p->header.flag & BT_ROOT) {
  2318. if (lmxaddr == 0)
  2319. break;
  2320. } else if (addressPXD(&p->header.self) == lmxaddr)
  2321. break;
  2322. /*
  2323. * descend down to leftmost child page
  2324. */
  2325. if (p->header.flag & BT_LEAF) {
  2326. DT_PUTPAGE(mp);
  2327. return -ESTALE;
  2328. }
  2329. /* get the leftmost entry */
  2330. stbl = DT_GETSTBL(p);
  2331. pxd = (pxd_t *) & p->slot[stbl[0]];
  2332. /* get the child page block address */
  2333. bn = addressPXD(pxd);
  2334. psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
  2335. /* unpin the parent page */
  2336. DT_PUTPAGE(mp);
  2337. }
  2338. /*
  2339. * search each page at the current levevl
  2340. */
  2341. loop:
  2342. stbl = DT_GETSTBL(p);
  2343. for (i = 0; i < p->header.nextindex; i++) {
  2344. pxd = (pxd_t *) & p->slot[stbl[i]];
  2345. /* found the specified router entry */
  2346. if (addressPXD(pxd) == addressPXD(kpxd) &&
  2347. lengthPXD(pxd) == lengthPXD(kpxd)) {
  2348. btsp = btstack->top;
  2349. btsp->bn = bn;
  2350. btsp->index = i;
  2351. btsp->mp = mp;
  2352. return 0;
  2353. }
  2354. }
  2355. /* get the right sibling page if any */
  2356. if (p->header.next)
  2357. bn = le64_to_cpu(p->header.next);
  2358. else {
  2359. DT_PUTPAGE(mp);
  2360. return -ESTALE;
  2361. }
  2362. /* unpin current page */
  2363. DT_PUTPAGE(mp);
  2364. /* get the right sibling page */
  2365. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2366. if (rc)
  2367. return rc;
  2368. goto loop;
  2369. }
  2370. #endif /* _NOTYET */
  2371. /*
  2372. * dtRelink()
  2373. *
  2374. * function:
  2375. * link around a freed page.
  2376. *
  2377. * parameter:
  2378. * fp: page to be freed
  2379. *
  2380. * return:
  2381. */
  2382. static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
  2383. {
  2384. int rc;
  2385. struct metapage *mp;
  2386. s64 nextbn, prevbn;
  2387. struct tlock *tlck;
  2388. struct dt_lock *dtlck;
  2389. struct lv *lv;
  2390. nextbn = le64_to_cpu(p->header.next);
  2391. prevbn = le64_to_cpu(p->header.prev);
  2392. /* update prev pointer of the next page */
  2393. if (nextbn != 0) {
  2394. DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  2395. if (rc)
  2396. return rc;
  2397. BT_MARK_DIRTY(mp, ip);
  2398. /*
  2399. * acquire a transaction lock on the next page
  2400. *
  2401. * action: update prev pointer;
  2402. */
  2403. tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
  2404. jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
  2405. tlck, ip, mp);
  2406. dtlck = (struct dt_lock *) & tlck->lock;
  2407. /* linelock header */
  2408. if (dtlck->index >= dtlck->maxcnt)
  2409. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2410. lv = & dtlck->lv[dtlck->index];
  2411. lv->offset = 0;
  2412. lv->length = 1;
  2413. dtlck->index++;
  2414. p->header.prev = cpu_to_le64(prevbn);
  2415. DT_PUTPAGE(mp);
  2416. }
  2417. /* update next pointer of the previous page */
  2418. if (prevbn != 0) {
  2419. DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
  2420. if (rc)
  2421. return rc;
  2422. BT_MARK_DIRTY(mp, ip);
  2423. /*
  2424. * acquire a transaction lock on the prev page
  2425. *
  2426. * action: update next pointer;
  2427. */
  2428. tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
  2429. jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
  2430. tlck, ip, mp);
  2431. dtlck = (struct dt_lock *) & tlck->lock;
  2432. /* linelock header */
  2433. if (dtlck->index >= dtlck->maxcnt)
  2434. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2435. lv = & dtlck->lv[dtlck->index];
  2436. lv->offset = 0;
  2437. lv->length = 1;
  2438. dtlck->index++;
  2439. p->header.next = cpu_to_le64(nextbn);
  2440. DT_PUTPAGE(mp);
  2441. }
  2442. return 0;
  2443. }
  2444. /*
  2445. * dtInitRoot()
  2446. *
  2447. * initialize directory root (inline in inode)
  2448. */
  2449. void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
  2450. {
  2451. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2452. dtroot_t *p;
  2453. int fsi;
  2454. struct dtslot *f;
  2455. struct tlock *tlck;
  2456. struct dt_lock *dtlck;
  2457. struct lv *lv;
  2458. u16 xflag_save;
  2459. /*
  2460. * If this was previously an non-empty directory, we need to remove
  2461. * the old directory table.
  2462. */
  2463. if (DO_INDEX(ip)) {
  2464. if (!jfs_dirtable_inline(ip)) {
  2465. struct tblock *tblk = tid_to_tblock(tid);
  2466. /*
  2467. * We're playing games with the tid's xflag. If
  2468. * we're removing a regular file, the file's xtree
  2469. * is committed with COMMIT_PMAP, but we always
  2470. * commit the directories xtree with COMMIT_PWMAP.
  2471. */
  2472. xflag_save = tblk->xflag;
  2473. tblk->xflag = 0;
  2474. /*
  2475. * xtTruncate isn't guaranteed to fully truncate
  2476. * the xtree. The caller needs to check i_size
  2477. * after committing the transaction to see if
  2478. * additional truncation is needed. The
  2479. * COMMIT_Stale flag tells caller that we
  2480. * initiated the truncation.
  2481. */
  2482. xtTruncate(tid, ip, 0, COMMIT_PWMAP);
  2483. set_cflag(COMMIT_Stale, ip);
  2484. tblk->xflag = xflag_save;
  2485. } else
  2486. ip->i_size = 1;
  2487. jfs_ip->next_index = 2;
  2488. } else
  2489. ip->i_size = IDATASIZE;
  2490. /*
  2491. * acquire a transaction lock on the root
  2492. *
  2493. * action: directory initialization;
  2494. */
  2495. tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
  2496. tlckDTREE | tlckENTRY | tlckBTROOT);
  2497. dtlck = (struct dt_lock *) & tlck->lock;
  2498. /* linelock root */
  2499. ASSERT(dtlck->index == 0);
  2500. lv = & dtlck->lv[0];
  2501. lv->offset = 0;
  2502. lv->length = DTROOTMAXSLOT;
  2503. dtlck->index++;
  2504. p = &jfs_ip->i_dtroot;
  2505. p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
  2506. p->header.nextindex = 0;
  2507. /* init freelist */
  2508. fsi = 1;
  2509. f = &p->slot[fsi];
  2510. /* init data area of root */
  2511. for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
  2512. f->next = fsi;
  2513. f->next = -1;
  2514. p->header.freelist = 1;
  2515. p->header.freecnt = 8;
  2516. /* init '..' entry */
  2517. p->header.idotdot = cpu_to_le32(idotdot);
  2518. return;
  2519. }
  2520. /*
  2521. * add_missing_indices()
  2522. *
  2523. * function: Fix dtree page in which one or more entries has an invalid index.
  2524. * fsck.jfs should really fix this, but it currently does not.
  2525. * Called from jfs_readdir when bad index is detected.
  2526. */
  2527. static void add_missing_indices(struct inode *inode, s64 bn)
  2528. {
  2529. struct ldtentry *d;
  2530. struct dt_lock *dtlck;
  2531. int i;
  2532. uint index;
  2533. struct lv *lv;
  2534. struct metapage *mp;
  2535. dtpage_t *p;
  2536. int rc;
  2537. s8 *stbl;
  2538. tid_t tid;
  2539. struct tlock *tlck;
  2540. tid = txBegin(inode->i_sb, 0);
  2541. DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
  2542. if (rc) {
  2543. printk(KERN_ERR "DT_GETPAGE failed!\n");
  2544. goto end;
  2545. }
  2546. BT_MARK_DIRTY(mp, inode);
  2547. ASSERT(p->header.flag & BT_LEAF);
  2548. tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
  2549. if (BT_IS_ROOT(mp))
  2550. tlck->type |= tlckBTROOT;
  2551. dtlck = (struct dt_lock *) &tlck->lock;
  2552. stbl = DT_GETSTBL(p);
  2553. for (i = 0; i < p->header.nextindex; i++) {
  2554. d = (struct ldtentry *) &p->slot[stbl[i]];
  2555. index = le32_to_cpu(d->index);
  2556. if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
  2557. d->index = cpu_to_le32(add_index(tid, inode, bn, i));
  2558. if (dtlck->index >= dtlck->maxcnt)
  2559. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2560. lv = &dtlck->lv[dtlck->index];
  2561. lv->offset = stbl[i];
  2562. lv->length = 1;
  2563. dtlck->index++;
  2564. }
  2565. }
  2566. DT_PUTPAGE(mp);
  2567. (void) txCommit(tid, 1, &inode, 0);
  2568. end:
  2569. txEnd(tid);
  2570. }
  2571. /*
  2572. * Buffer to hold directory entry info while traversing a dtree page
  2573. * before being fed to the filldir function
  2574. */
  2575. struct jfs_dirent {
  2576. loff_t position;
  2577. int ino;
  2578. u16 name_len;
  2579. char name[0];
  2580. };
  2581. /*
  2582. * function to determine next variable-sized jfs_dirent in buffer
  2583. */
  2584. static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
  2585. {
  2586. return (struct jfs_dirent *)
  2587. ((char *)dirent +
  2588. ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
  2589. sizeof (loff_t) - 1) &
  2590. ~(sizeof (loff_t) - 1)));
  2591. }
  2592. /*
  2593. * jfs_readdir()
  2594. *
  2595. * function: read directory entries sequentially
  2596. * from the specified entry offset
  2597. *
  2598. * parameter:
  2599. *
  2600. * return: offset = (pn, index) of start entry
  2601. * of next jfs_readdir()/dtRead()
  2602. */
  2603. int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  2604. {
  2605. struct inode *ip = filp->f_dentry->d_inode;
  2606. struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
  2607. int rc = 0;
  2608. loff_t dtpos; /* legacy OS/2 style position */
  2609. struct dtoffset {
  2610. s16 pn;
  2611. s16 index;
  2612. s32 unused;
  2613. } *dtoffset = (struct dtoffset *) &dtpos;
  2614. s64 bn;
  2615. struct metapage *mp;
  2616. dtpage_t *p;
  2617. int index;
  2618. s8 *stbl;
  2619. struct btstack btstack;
  2620. int i, next;
  2621. struct ldtentry *d;
  2622. struct dtslot *t;
  2623. int d_namleft, len, outlen;
  2624. unsigned long dirent_buf;
  2625. char *name_ptr;
  2626. u32 dir_index;
  2627. int do_index = 0;
  2628. uint loop_count = 0;
  2629. struct jfs_dirent *jfs_dirent;
  2630. int jfs_dirents;
  2631. int overflow, fix_page, page_fixed = 0;
  2632. static int unique_pos = 2; /* If we can't fix broken index */
  2633. if (filp->f_pos == DIREND)
  2634. return 0;
  2635. if (DO_INDEX(ip)) {
  2636. /*
  2637. * persistent index is stored in directory entries.
  2638. * Special cases: 0 = .
  2639. * 1 = ..
  2640. * -1 = End of directory
  2641. */
  2642. do_index = 1;
  2643. dir_index = (u32) filp->f_pos;
  2644. if (dir_index > 1) {
  2645. struct dir_table_slot dirtab_slot;
  2646. if (dtEmpty(ip) ||
  2647. (dir_index >= JFS_IP(ip)->next_index)) {
  2648. /* Stale position. Directory has shrunk */
  2649. filp->f_pos = DIREND;
  2650. return 0;
  2651. }
  2652. repeat:
  2653. rc = read_index(ip, dir_index, &dirtab_slot);
  2654. if (rc) {
  2655. filp->f_pos = DIREND;
  2656. return rc;
  2657. }
  2658. if (dirtab_slot.flag == DIR_INDEX_FREE) {
  2659. if (loop_count++ > JFS_IP(ip)->next_index) {
  2660. jfs_err("jfs_readdir detected "
  2661. "infinite loop!");
  2662. filp->f_pos = DIREND;
  2663. return 0;
  2664. }
  2665. dir_index = le32_to_cpu(dirtab_slot.addr2);
  2666. if (dir_index == -1) {
  2667. filp->f_pos = DIREND;
  2668. return 0;
  2669. }
  2670. goto repeat;
  2671. }
  2672. bn = addressDTS(&dirtab_slot);
  2673. index = dirtab_slot.slot;
  2674. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2675. if (rc) {
  2676. filp->f_pos = DIREND;
  2677. return 0;
  2678. }
  2679. if (p->header.flag & BT_INTERNAL) {
  2680. jfs_err("jfs_readdir: bad index table");
  2681. DT_PUTPAGE(mp);
  2682. filp->f_pos = -1;
  2683. return 0;
  2684. }
  2685. } else {
  2686. if (dir_index == 0) {
  2687. /*
  2688. * self "."
  2689. */
  2690. filp->f_pos = 0;
  2691. if (filldir(dirent, ".", 1, 0, ip->i_ino,
  2692. DT_DIR))
  2693. return 0;
  2694. }
  2695. /*
  2696. * parent ".."
  2697. */
  2698. filp->f_pos = 1;
  2699. if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
  2700. return 0;
  2701. /*
  2702. * Find first entry of left-most leaf
  2703. */
  2704. if (dtEmpty(ip)) {
  2705. filp->f_pos = DIREND;
  2706. return 0;
  2707. }
  2708. if ((rc = dtReadFirst(ip, &btstack)))
  2709. return rc;
  2710. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2711. }
  2712. } else {
  2713. /*
  2714. * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
  2715. *
  2716. * pn = index = 0: First entry "."
  2717. * pn = 0; index = 1: Second entry ".."
  2718. * pn > 0: Real entries, pn=1 -> leftmost page
  2719. * pn = index = -1: No more entries
  2720. */
  2721. dtpos = filp->f_pos;
  2722. if (dtpos == 0) {
  2723. /* build "." entry */
  2724. if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
  2725. DT_DIR))
  2726. return 0;
  2727. dtoffset->index = 1;
  2728. filp->f_pos = dtpos;
  2729. }
  2730. if (dtoffset->pn == 0) {
  2731. if (dtoffset->index == 1) {
  2732. /* build ".." entry */
  2733. if (filldir(dirent, "..", 2, filp->f_pos,
  2734. PARENT(ip), DT_DIR))
  2735. return 0;
  2736. } else {
  2737. jfs_err("jfs_readdir called with "
  2738. "invalid offset!");
  2739. }
  2740. dtoffset->pn = 1;
  2741. dtoffset->index = 0;
  2742. filp->f_pos = dtpos;
  2743. }
  2744. if (dtEmpty(ip)) {
  2745. filp->f_pos = DIREND;
  2746. return 0;
  2747. }
  2748. if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
  2749. jfs_err("jfs_readdir: unexpected rc = %d "
  2750. "from dtReadNext", rc);
  2751. filp->f_pos = DIREND;
  2752. return 0;
  2753. }
  2754. /* get start leaf page and index */
  2755. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2756. /* offset beyond directory eof ? */
  2757. if (bn < 0) {
  2758. filp->f_pos = DIREND;
  2759. return 0;
  2760. }
  2761. }
  2762. dirent_buf = __get_free_page(GFP_KERNEL);
  2763. if (dirent_buf == 0) {
  2764. DT_PUTPAGE(mp);
  2765. jfs_warn("jfs_readdir: __get_free_page failed!");
  2766. filp->f_pos = DIREND;
  2767. return -ENOMEM;
  2768. }
  2769. while (1) {
  2770. jfs_dirent = (struct jfs_dirent *) dirent_buf;
  2771. jfs_dirents = 0;
  2772. overflow = fix_page = 0;
  2773. stbl = DT_GETSTBL(p);
  2774. for (i = index; i < p->header.nextindex; i++) {
  2775. d = (struct ldtentry *) & p->slot[stbl[i]];
  2776. if (((long) jfs_dirent + d->namlen + 1) >
  2777. (dirent_buf + PAGE_SIZE)) {
  2778. /* DBCS codepages could overrun dirent_buf */
  2779. index = i;
  2780. overflow = 1;
  2781. break;
  2782. }
  2783. d_namleft = d->namlen;
  2784. name_ptr = jfs_dirent->name;
  2785. jfs_dirent->ino = le32_to_cpu(d->inumber);
  2786. if (do_index) {
  2787. len = min(d_namleft, DTLHDRDATALEN);
  2788. jfs_dirent->position = le32_to_cpu(d->index);
  2789. /*
  2790. * d->index should always be valid, but it
  2791. * isn't. fsck.jfs doesn't create the
  2792. * directory index for the lost+found
  2793. * directory. Rather than let it go,
  2794. * we can try to fix it.
  2795. */
  2796. if ((jfs_dirent->position < 2) ||
  2797. (jfs_dirent->position >=
  2798. JFS_IP(ip)->next_index)) {
  2799. if (!page_fixed && !isReadOnly(ip)) {
  2800. fix_page = 1;
  2801. /*
  2802. * setting overflow and setting
  2803. * index to i will cause the
  2804. * same page to be processed
  2805. * again starting here
  2806. */
  2807. overflow = 1;
  2808. index = i;
  2809. break;
  2810. }
  2811. jfs_dirent->position = unique_pos++;
  2812. }
  2813. } else {
  2814. jfs_dirent->position = dtpos;
  2815. len = min(d_namleft, DTLHDRDATALEN_LEGACY);
  2816. }
  2817. /* copy the name of head/only segment */
  2818. outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
  2819. codepage);
  2820. jfs_dirent->name_len = outlen;
  2821. /* copy name in the additional segment(s) */
  2822. next = d->next;
  2823. while (next >= 0) {
  2824. t = (struct dtslot *) & p->slot[next];
  2825. name_ptr += outlen;
  2826. d_namleft -= len;
  2827. /* Sanity Check */
  2828. if (d_namleft == 0) {
  2829. jfs_error(ip->i_sb,
  2830. "JFS:Dtree error: ino = "
  2831. "%ld, bn=%Ld, index = %d",
  2832. (long)ip->i_ino,
  2833. (long long)bn,
  2834. i);
  2835. goto skip_one;
  2836. }
  2837. len = min(d_namleft, DTSLOTDATALEN);
  2838. outlen = jfs_strfromUCS_le(name_ptr, t->name,
  2839. len, codepage);
  2840. jfs_dirent->name_len += outlen;
  2841. next = t->next;
  2842. }
  2843. jfs_dirents++;
  2844. jfs_dirent = next_jfs_dirent(jfs_dirent);
  2845. skip_one:
  2846. if (!do_index)
  2847. dtoffset->index++;
  2848. }
  2849. if (!overflow) {
  2850. /* Point to next leaf page */
  2851. if (p->header.flag & BT_ROOT)
  2852. bn = 0;
  2853. else {
  2854. bn = le64_to_cpu(p->header.next);
  2855. index = 0;
  2856. /* update offset (pn:index) for new page */
  2857. if (!do_index) {
  2858. dtoffset->pn++;
  2859. dtoffset->index = 0;
  2860. }
  2861. }
  2862. page_fixed = 0;
  2863. }
  2864. /* unpin previous leaf page */
  2865. DT_PUTPAGE(mp);
  2866. jfs_dirent = (struct jfs_dirent *) dirent_buf;
  2867. while (jfs_dirents--) {
  2868. filp->f_pos = jfs_dirent->position;
  2869. if (filldir(dirent, jfs_dirent->name,
  2870. jfs_dirent->name_len, filp->f_pos,
  2871. jfs_dirent->ino, DT_UNKNOWN))
  2872. goto out;
  2873. jfs_dirent = next_jfs_dirent(jfs_dirent);
  2874. }
  2875. if (fix_page) {
  2876. add_missing_indices(ip, bn);
  2877. page_fixed = 1;
  2878. }
  2879. if (!overflow && (bn == 0)) {
  2880. filp->f_pos = DIREND;
  2881. break;
  2882. }
  2883. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2884. if (rc) {
  2885. free_page(dirent_buf);
  2886. return rc;
  2887. }
  2888. }
  2889. out:
  2890. free_page(dirent_buf);
  2891. return rc;
  2892. }
  2893. /*
  2894. * dtReadFirst()
  2895. *
  2896. * function: get the leftmost page of the directory
  2897. */
  2898. static int dtReadFirst(struct inode *ip, struct btstack * btstack)
  2899. {
  2900. int rc = 0;
  2901. s64 bn;
  2902. int psize = 288; /* initial in-line directory */
  2903. struct metapage *mp;
  2904. dtpage_t *p;
  2905. s8 *stbl;
  2906. struct btframe *btsp;
  2907. pxd_t *xd;
  2908. BT_CLR(btstack); /* reset stack */
  2909. /*
  2910. * descend leftmost path of the tree
  2911. *
  2912. * by convention, root bn = 0.
  2913. */
  2914. for (bn = 0;;) {
  2915. DT_GETPAGE(ip, bn, mp, psize, p, rc);
  2916. if (rc)
  2917. return rc;
  2918. /*
  2919. * leftmost leaf page
  2920. */
  2921. if (p->header.flag & BT_LEAF) {
  2922. /* return leftmost entry */
  2923. btsp = btstack->top;
  2924. btsp->bn = bn;
  2925. btsp->index = 0;
  2926. btsp->mp = mp;
  2927. return 0;
  2928. }
  2929. /*
  2930. * descend down to leftmost child page
  2931. */
  2932. if (BT_STACK_FULL(btstack)) {
  2933. DT_PUTPAGE(mp);
  2934. jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
  2935. BT_STACK_DUMP(btstack);
  2936. return -EIO;
  2937. }
  2938. /* push (bn, index) of the parent page/entry */
  2939. BT_PUSH(btstack, bn, 0);
  2940. /* get the leftmost entry */
  2941. stbl = DT_GETSTBL(p);
  2942. xd = (pxd_t *) & p->slot[stbl[0]];
  2943. /* get the child page block address */
  2944. bn = addressPXD(xd);
  2945. psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
  2946. /* unpin the parent page */
  2947. DT_PUTPAGE(mp);
  2948. }
  2949. }
  2950. /*
  2951. * dtReadNext()
  2952. *
  2953. * function: get the page of the specified offset (pn:index)
  2954. *
  2955. * return: if (offset > eof), bn = -1;
  2956. *
  2957. * note: if index > nextindex of the target leaf page,
  2958. * start with 1st entry of next leaf page;
  2959. */
  2960. static int dtReadNext(struct inode *ip, loff_t * offset,
  2961. struct btstack * btstack)
  2962. {
  2963. int rc = 0;
  2964. struct dtoffset {
  2965. s16 pn;
  2966. s16 index;
  2967. s32 unused;
  2968. } *dtoffset = (struct dtoffset *) offset;
  2969. s64 bn;
  2970. struct metapage *mp;
  2971. dtpage_t *p;
  2972. int index;
  2973. int pn;
  2974. s8 *stbl;
  2975. struct btframe *btsp, *parent;
  2976. pxd_t *xd;
  2977. /*
  2978. * get leftmost leaf page pinned
  2979. */
  2980. if ((rc = dtReadFirst(ip, btstack)))
  2981. return rc;
  2982. /* get leaf page */
  2983. DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
  2984. /* get the start offset (pn:index) */
  2985. pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
  2986. index = dtoffset->index;
  2987. /* start at leftmost page ? */
  2988. if (pn == 0) {
  2989. /* offset beyond eof ? */
  2990. if (index < p->header.nextindex)
  2991. goto out;
  2992. if (p->header.flag & BT_ROOT) {
  2993. bn = -1;
  2994. goto out;
  2995. }
  2996. /* start with 1st entry of next leaf page */
  2997. dtoffset->pn++;
  2998. dtoffset->index = index = 0;
  2999. goto a;
  3000. }
  3001. /* start at non-leftmost page: scan parent pages for large pn */
  3002. if (p->header.flag & BT_ROOT) {
  3003. bn = -1;
  3004. goto out;
  3005. }
  3006. /* start after next leaf page ? */
  3007. if (pn > 1)
  3008. goto b;
  3009. /* get leaf page pn = 1 */
  3010. a:
  3011. bn = le64_to_cpu(p->header.next);
  3012. /* unpin leaf page */
  3013. DT_PUTPAGE(mp);
  3014. /* offset beyond eof ? */
  3015. if (bn == 0) {
  3016. bn = -1;
  3017. goto out;
  3018. }
  3019. goto c;
  3020. /*
  3021. * scan last internal page level to get target leaf page
  3022. */
  3023. b:
  3024. /* unpin leftmost leaf page */
  3025. DT_PUTPAGE(mp);
  3026. /* get left most parent page */
  3027. btsp = btstack->top;
  3028. parent = btsp - 1;
  3029. bn = parent->bn;
  3030. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3031. if (rc)
  3032. return rc;
  3033. /* scan parent pages at last internal page level */
  3034. while (pn >= p->header.nextindex) {
  3035. pn -= p->header.nextindex;
  3036. /* get next parent page address */
  3037. bn = le64_to_cpu(p->header.next);
  3038. /* unpin current parent page */
  3039. DT_PUTPAGE(mp);
  3040. /* offset beyond eof ? */
  3041. if (bn == 0) {
  3042. bn = -1;
  3043. goto out;
  3044. }
  3045. /* get next parent page */
  3046. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3047. if (rc)
  3048. return rc;
  3049. /* update parent page stack frame */
  3050. parent->bn = bn;
  3051. }
  3052. /* get leaf page address */
  3053. stbl = DT_GETSTBL(p);
  3054. xd = (pxd_t *) & p->slot[stbl[pn]];
  3055. bn = addressPXD(xd);
  3056. /* unpin parent page */
  3057. DT_PUTPAGE(mp);
  3058. /*
  3059. * get target leaf page
  3060. */
  3061. c:
  3062. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3063. if (rc)
  3064. return rc;
  3065. /*
  3066. * leaf page has been completed:
  3067. * start with 1st entry of next leaf page
  3068. */
  3069. if (index >= p->header.nextindex) {
  3070. bn = le64_to_cpu(p->header.next);
  3071. /* unpin leaf page */
  3072. DT_PUTPAGE(mp);
  3073. /* offset beyond eof ? */
  3074. if (bn == 0) {
  3075. bn = -1;
  3076. goto out;
  3077. }
  3078. /* get next leaf page */
  3079. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3080. if (rc)
  3081. return rc;
  3082. /* start with 1st entry of next leaf page */
  3083. dtoffset->pn++;
  3084. dtoffset->index = 0;
  3085. }
  3086. out:
  3087. /* return target leaf page pinned */
  3088. btsp = btstack->top;
  3089. btsp->bn = bn;
  3090. btsp->index = dtoffset->index;
  3091. btsp->mp = mp;
  3092. return 0;
  3093. }
  3094. /*
  3095. * dtCompare()
  3096. *
  3097. * function: compare search key with an internal entry
  3098. *
  3099. * return:
  3100. * < 0 if k is < record
  3101. * = 0 if k is = record
  3102. * > 0 if k is > record
  3103. */
  3104. static int dtCompare(struct component_name * key, /* search key */
  3105. dtpage_t * p, /* directory page */
  3106. int si)
  3107. { /* entry slot index */
  3108. wchar_t *kname;
  3109. __le16 *name;
  3110. int klen, namlen, len, rc;
  3111. struct idtentry *ih;
  3112. struct dtslot *t;
  3113. /*
  3114. * force the left-most key on internal pages, at any level of
  3115. * the tree, to be less than any search key.
  3116. * this obviates having to update the leftmost key on an internal
  3117. * page when the user inserts a new key in the tree smaller than
  3118. * anything that has been stored.
  3119. *
  3120. * (? if/when dtSearch() narrows down to 1st entry (index = 0),
  3121. * at any internal page at any level of the tree,
  3122. * it descends to child of the entry anyway -
  3123. * ? make the entry as min size dummy entry)
  3124. *
  3125. * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
  3126. * return (1);
  3127. */
  3128. kname = key->name;
  3129. klen = key->namlen;
  3130. ih = (struct idtentry *) & p->slot[si];
  3131. si = ih->next;
  3132. name = ih->name;
  3133. namlen = ih->namlen;
  3134. len = min(namlen, DTIHDRDATALEN);
  3135. /* compare with head/only segment */
  3136. len = min(klen, len);
  3137. if ((rc = UniStrncmp_le(kname, name, len)))
  3138. return rc;
  3139. klen -= len;
  3140. namlen -= len;
  3141. /* compare with additional segment(s) */
  3142. kname += len;
  3143. while (klen > 0 && namlen > 0) {
  3144. /* compare with next name segment */
  3145. t = (struct dtslot *) & p->slot[si];
  3146. len = min(namlen, DTSLOTDATALEN);
  3147. len = min(klen, len);
  3148. name = t->name;
  3149. if ((rc = UniStrncmp_le(kname, name, len)))
  3150. return rc;
  3151. klen -= len;
  3152. namlen -= len;
  3153. kname += len;
  3154. si = t->next;
  3155. }
  3156. return (klen - namlen);
  3157. }
  3158. /*
  3159. * ciCompare()
  3160. *
  3161. * function: compare search key with an (leaf/internal) entry
  3162. *
  3163. * return:
  3164. * < 0 if k is < record
  3165. * = 0 if k is = record
  3166. * > 0 if k is > record
  3167. */
  3168. static int ciCompare(struct component_name * key, /* search key */
  3169. dtpage_t * p, /* directory page */
  3170. int si, /* entry slot index */
  3171. int flag)
  3172. {
  3173. wchar_t *kname, x;
  3174. __le16 *name;
  3175. int klen, namlen, len, rc;
  3176. struct ldtentry *lh;
  3177. struct idtentry *ih;
  3178. struct dtslot *t;
  3179. int i;
  3180. /*
  3181. * force the left-most key on internal pages, at any level of
  3182. * the tree, to be less than any search key.
  3183. * this obviates having to update the leftmost key on an internal
  3184. * page when the user inserts a new key in the tree smaller than
  3185. * anything that has been stored.
  3186. *
  3187. * (? if/when dtSearch() narrows down to 1st entry (index = 0),
  3188. * at any internal page at any level of the tree,
  3189. * it descends to child of the entry anyway -
  3190. * ? make the entry as min size dummy entry)
  3191. *
  3192. * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
  3193. * return (1);
  3194. */
  3195. kname = key->name;
  3196. klen = key->namlen;
  3197. /*
  3198. * leaf page entry
  3199. */
  3200. if (p->header.flag & BT_LEAF) {
  3201. lh = (struct ldtentry *) & p->slot[si];
  3202. si = lh->next;
  3203. name = lh->name;
  3204. namlen = lh->namlen;
  3205. if (flag & JFS_DIR_INDEX)
  3206. len = min(namlen, DTLHDRDATALEN);
  3207. else
  3208. len = min(namlen, DTLHDRDATALEN_LEGACY);
  3209. }
  3210. /*
  3211. * internal page entry
  3212. */
  3213. else {
  3214. ih = (struct idtentry *) & p->slot[si];
  3215. si = ih->next;
  3216. name = ih->name;
  3217. namlen = ih->namlen;
  3218. len = min(namlen, DTIHDRDATALEN);
  3219. }
  3220. /* compare with head/only segment */
  3221. len = min(klen, len);
  3222. for (i = 0; i < len; i++, kname++, name++) {
  3223. /* only uppercase if case-insensitive support is on */
  3224. if ((flag & JFS_OS2) == JFS_OS2)
  3225. x = UniToupper(le16_to_cpu(*name));
  3226. else
  3227. x = le16_to_cpu(*name);
  3228. if ((rc = *kname - x))
  3229. return rc;
  3230. }
  3231. klen -= len;
  3232. namlen -= len;
  3233. /* compare with additional segment(s) */
  3234. while (klen > 0 && namlen > 0) {
  3235. /* compare with next name segment */
  3236. t = (struct dtslot *) & p->slot[si];
  3237. len = min(namlen, DTSLOTDATALEN);
  3238. len = min(klen, len);
  3239. name = t->name;
  3240. for (i = 0; i < len; i++, kname++, name++) {
  3241. /* only uppercase if case-insensitive support is on */
  3242. if ((flag & JFS_OS2) == JFS_OS2)
  3243. x = UniToupper(le16_to_cpu(*name));
  3244. else
  3245. x = le16_to_cpu(*name);
  3246. if ((rc = *kname - x))
  3247. return rc;
  3248. }
  3249. klen -= len;
  3250. namlen -= len;
  3251. si = t->next;
  3252. }
  3253. return (klen - namlen);
  3254. }
  3255. /*
  3256. * ciGetLeafPrefixKey()
  3257. *
  3258. * function: compute prefix of suffix compression
  3259. * from two adjacent leaf entries
  3260. * across page boundary
  3261. *
  3262. * return: non-zero on error
  3263. *
  3264. */
  3265. static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
  3266. int ri, struct component_name * key, int flag)
  3267. {
  3268. int klen, namlen;
  3269. wchar_t *pl, *pr, *kname;
  3270. struct component_name lkey;
  3271. struct component_name rkey;
  3272. lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
  3273. GFP_KERNEL);
  3274. if (lkey.name == NULL)
  3275. return -ENOSPC;
  3276. rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
  3277. GFP_KERNEL);
  3278. if (rkey.name == NULL) {
  3279. kfree(lkey.name);
  3280. return -ENOSPC;
  3281. }
  3282. /* get left and right key */
  3283. dtGetKey(lp, li, &lkey, flag);
  3284. lkey.name[lkey.namlen] = 0;
  3285. if ((flag & JFS_OS2) == JFS_OS2)
  3286. ciToUpper(&lkey);
  3287. dtGetKey(rp, ri, &rkey, flag);
  3288. rkey.name[rkey.namlen] = 0;
  3289. if ((flag & JFS_OS2) == JFS_OS2)
  3290. ciToUpper(&rkey);
  3291. /* compute prefix */
  3292. klen = 0;
  3293. kname = key->name;
  3294. namlen = min(lkey.namlen, rkey.namlen);
  3295. for (pl = lkey.name, pr = rkey.name;
  3296. namlen; pl++, pr++, namlen--, klen++, kname++) {
  3297. *kname = *pr;
  3298. if (*pl != *pr) {
  3299. key->namlen = klen + 1;
  3300. goto free_names;
  3301. }
  3302. }
  3303. /* l->namlen <= r->namlen since l <= r */
  3304. if (lkey.namlen < rkey.namlen) {
  3305. *kname = *pr;
  3306. key->namlen = klen + 1;
  3307. } else /* l->namelen == r->namelen */
  3308. key->namlen = klen;
  3309. free_names:
  3310. kfree(lkey.name);
  3311. kfree(rkey.name);
  3312. return 0;
  3313. }
  3314. /*
  3315. * dtGetKey()
  3316. *
  3317. * function: get key of the entry
  3318. */
  3319. static void dtGetKey(dtpage_t * p, int i, /* entry index */
  3320. struct component_name * key, int flag)
  3321. {
  3322. int si;
  3323. s8 *stbl;
  3324. struct ldtentry *lh;
  3325. struct idtentry *ih;
  3326. struct dtslot *t;
  3327. int namlen, len;
  3328. wchar_t *kname;
  3329. __le16 *name;
  3330. /* get entry */
  3331. stbl = DT_GETSTBL(p);
  3332. si = stbl[i];
  3333. if (p->header.flag & BT_LEAF) {
  3334. lh = (struct ldtentry *) & p->slot[si];
  3335. si = lh->next;
  3336. namlen = lh->namlen;
  3337. name = lh->name;
  3338. if (flag & JFS_DIR_INDEX)
  3339. len = min(namlen, DTLHDRDATALEN);
  3340. else
  3341. len = min(namlen, DTLHDRDATALEN_LEGACY);
  3342. } else {
  3343. ih = (struct idtentry *) & p->slot[si];
  3344. si = ih->next;
  3345. namlen = ih->namlen;
  3346. name = ih->name;
  3347. len = min(namlen, DTIHDRDATALEN);
  3348. }
  3349. key->namlen = namlen;
  3350. kname = key->name;
  3351. /*
  3352. * move head/only segment
  3353. */
  3354. UniStrncpy_from_le(kname, name, len);
  3355. /*
  3356. * move additional segment(s)
  3357. */
  3358. while (si >= 0) {
  3359. /* get next segment */
  3360. t = &p->slot[si];
  3361. kname += len;
  3362. namlen -= len;
  3363. len = min(namlen, DTSLOTDATALEN);
  3364. UniStrncpy_from_le(kname, t->name, len);
  3365. si = t->next;
  3366. }
  3367. }
  3368. /*
  3369. * dtInsertEntry()
  3370. *
  3371. * function: allocate free slot(s) and
  3372. * write a leaf/internal entry
  3373. *
  3374. * return: entry slot index
  3375. */
  3376. static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
  3377. ddata_t * data, struct dt_lock ** dtlock)
  3378. {
  3379. struct dtslot *h, *t;
  3380. struct ldtentry *lh = NULL;
  3381. struct idtentry *ih = NULL;
  3382. int hsi, fsi, klen, len, nextindex;
  3383. wchar_t *kname;
  3384. __le16 *name;
  3385. s8 *stbl;
  3386. pxd_t *xd;
  3387. struct dt_lock *dtlck = *dtlock;
  3388. struct lv *lv;
  3389. int xsi, n;
  3390. s64 bn = 0;
  3391. struct metapage *mp = NULL;
  3392. klen = key->namlen;
  3393. kname = key->name;
  3394. /* allocate a free slot */
  3395. hsi = fsi = p->header.freelist;
  3396. h = &p->slot[fsi];
  3397. p->header.freelist = h->next;
  3398. --p->header.freecnt;
  3399. /* open new linelock */
  3400. if (dtlck->index >= dtlck->maxcnt)
  3401. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3402. lv = & dtlck->lv[dtlck->index];
  3403. lv->offset = hsi;
  3404. /* write head/only segment */
  3405. if (p->header.flag & BT_LEAF) {
  3406. lh = (struct ldtentry *) h;
  3407. lh->next = h->next;
  3408. lh->inumber = cpu_to_le32(data->leaf.ino);
  3409. lh->namlen = klen;
  3410. name = lh->name;
  3411. if (data->leaf.ip) {
  3412. len = min(klen, DTLHDRDATALEN);
  3413. if (!(p->header.flag & BT_ROOT))
  3414. bn = addressPXD(&p->header.self);
  3415. lh->index = cpu_to_le32(add_index(data->leaf.tid,
  3416. data->leaf.ip,
  3417. bn, index));
  3418. } else
  3419. len = min(klen, DTLHDRDATALEN_LEGACY);
  3420. } else {
  3421. ih = (struct idtentry *) h;
  3422. ih->next = h->next;
  3423. xd = (pxd_t *) ih;
  3424. *xd = data->xd;
  3425. ih->namlen = klen;
  3426. name = ih->name;
  3427. len = min(klen, DTIHDRDATALEN);
  3428. }
  3429. UniStrncpy_to_le(name, kname, len);
  3430. n = 1;
  3431. xsi = hsi;
  3432. /* write additional segment(s) */
  3433. t = h;
  3434. klen -= len;
  3435. while (klen) {
  3436. /* get free slot */
  3437. fsi = p->header.freelist;
  3438. t = &p->slot[fsi];
  3439. p->header.freelist = t->next;
  3440. --p->header.freecnt;
  3441. /* is next slot contiguous ? */
  3442. if (fsi != xsi + 1) {
  3443. /* close current linelock */
  3444. lv->length = n;
  3445. dtlck->index++;
  3446. /* open new linelock */
  3447. if (dtlck->index < dtlck->maxcnt)
  3448. lv++;
  3449. else {
  3450. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3451. lv = & dtlck->lv[0];
  3452. }
  3453. lv->offset = fsi;
  3454. n = 0;
  3455. }
  3456. kname += len;
  3457. len = min(klen, DTSLOTDATALEN);
  3458. UniStrncpy_to_le(t->name, kname, len);
  3459. n++;
  3460. xsi = fsi;
  3461. klen -= len;
  3462. }
  3463. /* close current linelock */
  3464. lv->length = n;
  3465. dtlck->index++;
  3466. *dtlock = dtlck;
  3467. /* terminate last/only segment */
  3468. if (h == t) {
  3469. /* single segment entry */
  3470. if (p->header.flag & BT_LEAF)
  3471. lh->next = -1;
  3472. else
  3473. ih->next = -1;
  3474. } else
  3475. /* multi-segment entry */
  3476. t->next = -1;
  3477. /* if insert into middle, shift right succeeding entries in stbl */
  3478. stbl = DT_GETSTBL(p);
  3479. nextindex = p->header.nextindex;
  3480. if (index < nextindex) {
  3481. memmove(stbl + index + 1, stbl + index, nextindex - index);
  3482. if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
  3483. s64 lblock;
  3484. /*
  3485. * Need to update slot number for entries that moved
  3486. * in the stbl
  3487. */
  3488. mp = NULL;
  3489. for (n = index + 1; n <= nextindex; n++) {
  3490. lh = (struct ldtentry *) & (p->slot[stbl[n]]);
  3491. modify_index(data->leaf.tid, data->leaf.ip,
  3492. le32_to_cpu(lh->index), bn, n,
  3493. &mp, &lblock);
  3494. }
  3495. if (mp)
  3496. release_metapage(mp);
  3497. }
  3498. }
  3499. stbl[index] = hsi;
  3500. /* advance next available entry index of stbl */
  3501. ++p->header.nextindex;
  3502. }
  3503. /*
  3504. * dtMoveEntry()
  3505. *
  3506. * function: move entries from split/left page to new/right page
  3507. *
  3508. * nextindex of dst page and freelist/freecnt of both pages
  3509. * are updated.
  3510. */
  3511. static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
  3512. struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
  3513. int do_index)
  3514. {
  3515. int ssi, next; /* src slot index */
  3516. int di; /* dst entry index */
  3517. int dsi; /* dst slot index */
  3518. s8 *sstbl, *dstbl; /* sorted entry table */
  3519. int snamlen, len;
  3520. struct ldtentry *slh, *dlh = NULL;
  3521. struct idtentry *sih, *dih = NULL;
  3522. struct dtslot *h, *s, *d;
  3523. struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
  3524. struct lv *slv, *dlv;
  3525. int xssi, ns, nd;
  3526. int sfsi;
  3527. sstbl = (s8 *) & sp->slot[sp->header.stblindex];
  3528. dstbl = (s8 *) & dp->slot[dp->header.stblindex];
  3529. dsi = dp->header.freelist; /* first (whole page) free slot */
  3530. sfsi = sp->header.freelist;
  3531. /* linelock destination entry slot */
  3532. dlv = & ddtlck->lv[ddtlck->index];
  3533. dlv->offset = dsi;
  3534. /* linelock source entry slot */
  3535. slv = & sdtlck->lv[sdtlck->index];
  3536. slv->offset = sstbl[si];
  3537. xssi = slv->offset - 1;
  3538. /*
  3539. * move entries
  3540. */
  3541. ns = nd = 0;
  3542. for (di = 0; si < sp->header.nextindex; si++, di++) {
  3543. ssi = sstbl[si];
  3544. dstbl[di] = dsi;
  3545. /* is next slot contiguous ? */
  3546. if (ssi != xssi + 1) {
  3547. /* close current linelock */
  3548. slv->length = ns;
  3549. sdtlck->index++;
  3550. /* open new linelock */
  3551. if (sdtlck->index < sdtlck->maxcnt)
  3552. slv++;
  3553. else {
  3554. sdtlck = (struct dt_lock *) txLinelock(sdtlck);
  3555. slv = & sdtlck->lv[0];
  3556. }
  3557. slv->offset = ssi;
  3558. ns = 0;
  3559. }
  3560. /*
  3561. * move head/only segment of an entry
  3562. */
  3563. /* get dst slot */
  3564. h = d = &dp->slot[dsi];
  3565. /* get src slot and move */
  3566. s = &sp->slot[ssi];
  3567. if (sp->header.flag & BT_LEAF) {
  3568. /* get source entry */
  3569. slh = (struct ldtentry *) s;
  3570. dlh = (struct ldtentry *) h;
  3571. snamlen = slh->namlen;
  3572. if (do_index) {
  3573. len = min(snamlen, DTLHDRDATALEN);
  3574. dlh->index = slh->index; /* little-endian */
  3575. } else
  3576. len = min(snamlen, DTLHDRDATALEN_LEGACY);
  3577. memcpy(dlh, slh, 6 + len * 2);
  3578. next = slh->next;
  3579. /* update dst head/only segment next field */
  3580. dsi++;
  3581. dlh->next = dsi;
  3582. } else {
  3583. sih = (struct idtentry *) s;
  3584. snamlen = sih->namlen;
  3585. len = min(snamlen, DTIHDRDATALEN);
  3586. dih = (struct idtentry *) h;
  3587. memcpy(dih, sih, 10 + len * 2);
  3588. next = sih->next;
  3589. dsi++;
  3590. dih->next = dsi;
  3591. }
  3592. /* free src head/only segment */
  3593. s->next = sfsi;
  3594. s->cnt = 1;
  3595. sfsi = ssi;
  3596. ns++;
  3597. nd++;
  3598. xssi = ssi;
  3599. /*
  3600. * move additional segment(s) of the entry
  3601. */
  3602. snamlen -= len;
  3603. while ((ssi = next) >= 0) {
  3604. /* is next slot contiguous ? */
  3605. if (ssi != xssi + 1) {
  3606. /* close current linelock */
  3607. slv->length = ns;
  3608. sdtlck->index++;
  3609. /* open new linelock */
  3610. if (sdtlck->index < sdtlck->maxcnt)
  3611. slv++;
  3612. else {
  3613. sdtlck =
  3614. (struct dt_lock *)
  3615. txLinelock(sdtlck);
  3616. slv = & sdtlck->lv[0];
  3617. }
  3618. slv->offset = ssi;
  3619. ns = 0;
  3620. }
  3621. /* get next source segment */
  3622. s = &sp->slot[ssi];
  3623. /* get next destination free slot */
  3624. d++;
  3625. len = min(snamlen, DTSLOTDATALEN);
  3626. UniStrncpy_le(d->name, s->name, len);
  3627. ns++;
  3628. nd++;
  3629. xssi = ssi;
  3630. dsi++;
  3631. d->next = dsi;
  3632. /* free source segment */
  3633. next = s->next;
  3634. s->next = sfsi;
  3635. s->cnt = 1;
  3636. sfsi = ssi;
  3637. snamlen -= len;
  3638. } /* end while */
  3639. /* terminate dst last/only segment */
  3640. if (h == d) {
  3641. /* single segment entry */
  3642. if (dp->header.flag & BT_LEAF)
  3643. dlh->next = -1;
  3644. else
  3645. dih->next = -1;
  3646. } else
  3647. /* multi-segment entry */
  3648. d->next = -1;
  3649. } /* end for */
  3650. /* close current linelock */
  3651. slv->length = ns;
  3652. sdtlck->index++;
  3653. *sdtlock = sdtlck;
  3654. dlv->length = nd;
  3655. ddtlck->index++;
  3656. *ddtlock = ddtlck;
  3657. /* update source header */
  3658. sp->header.freelist = sfsi;
  3659. sp->header.freecnt += nd;
  3660. /* update destination header */
  3661. dp->header.nextindex = di;
  3662. dp->header.freelist = dsi;
  3663. dp->header.freecnt -= nd;
  3664. }
  3665. /*
  3666. * dtDeleteEntry()
  3667. *
  3668. * function: free a (leaf/internal) entry
  3669. *
  3670. * log freelist header, stbl, and each segment slot of entry
  3671. * (even though last/only segment next field is modified,
  3672. * physical image logging requires all segment slots of
  3673. * the entry logged to avoid applying previous updates
  3674. * to the same slots)
  3675. */
  3676. static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
  3677. {
  3678. int fsi; /* free entry slot index */
  3679. s8 *stbl;
  3680. struct dtslot *t;
  3681. int si, freecnt;
  3682. struct dt_lock *dtlck = *dtlock;
  3683. struct lv *lv;
  3684. int xsi, n;
  3685. /* get free entry slot index */
  3686. stbl = DT_GETSTBL(p);
  3687. fsi = stbl[fi];
  3688. /* open new linelock */
  3689. if (dtlck->index >= dtlck->maxcnt)
  3690. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3691. lv = & dtlck->lv[dtlck->index];
  3692. lv->offset = fsi;
  3693. /* get the head/only segment */
  3694. t = &p->slot[fsi];
  3695. if (p->header.flag & BT_LEAF)
  3696. si = ((struct ldtentry *) t)->next;
  3697. else
  3698. si = ((struct idtentry *) t)->next;
  3699. t->next = si;
  3700. t->cnt = 1;
  3701. n = freecnt = 1;
  3702. xsi = fsi;
  3703. /* find the last/only segment */
  3704. while (si >= 0) {
  3705. /* is next slot contiguous ? */
  3706. if (si != xsi + 1) {
  3707. /* close current linelock */
  3708. lv->length = n;
  3709. dtlck->index++;
  3710. /* open new linelock */
  3711. if (dtlck->index < dtlck->maxcnt)
  3712. lv++;
  3713. else {
  3714. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3715. lv = & dtlck->lv[0];
  3716. }
  3717. lv->offset = si;
  3718. n = 0;
  3719. }
  3720. n++;
  3721. xsi = si;
  3722. freecnt++;
  3723. t = &p->slot[si];
  3724. t->cnt = 1;
  3725. si = t->next;
  3726. }
  3727. /* close current linelock */
  3728. lv->length = n;
  3729. dtlck->index++;
  3730. *dtlock = dtlck;
  3731. /* update freelist */
  3732. t->next = p->header.freelist;
  3733. p->header.freelist = fsi;
  3734. p->header.freecnt += freecnt;
  3735. /* if delete from middle,
  3736. * shift left the succedding entries in the stbl
  3737. */
  3738. si = p->header.nextindex;
  3739. if (fi < si - 1)
  3740. memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
  3741. p->header.nextindex--;
  3742. }
  3743. /*
  3744. * dtTruncateEntry()
  3745. *
  3746. * function: truncate a (leaf/internal) entry
  3747. *
  3748. * log freelist header, stbl, and each segment slot of entry
  3749. * (even though last/only segment next field is modified,
  3750. * physical image logging requires all segment slots of
  3751. * the entry logged to avoid applying previous updates
  3752. * to the same slots)
  3753. */
  3754. static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
  3755. {
  3756. int tsi; /* truncate entry slot index */
  3757. s8 *stbl;
  3758. struct dtslot *t;
  3759. int si, freecnt;
  3760. struct dt_lock *dtlck = *dtlock;
  3761. struct lv *lv;
  3762. int fsi, xsi, n;
  3763. /* get free entry slot index */
  3764. stbl = DT_GETSTBL(p);
  3765. tsi = stbl[ti];
  3766. /* open new linelock */
  3767. if (dtlck->index >= dtlck->maxcnt)
  3768. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3769. lv = & dtlck->lv[dtlck->index];
  3770. lv->offset = tsi;
  3771. /* get the head/only segment */
  3772. t = &p->slot[tsi];
  3773. ASSERT(p->header.flag & BT_INTERNAL);
  3774. ((struct idtentry *) t)->namlen = 0;
  3775. si = ((struct idtentry *) t)->next;
  3776. ((struct idtentry *) t)->next = -1;
  3777. n = 1;
  3778. freecnt = 0;
  3779. fsi = si;
  3780. xsi = tsi;
  3781. /* find the last/only segment */
  3782. while (si >= 0) {
  3783. /* is next slot contiguous ? */
  3784. if (si != xsi + 1) {
  3785. /* close current linelock */
  3786. lv->length = n;
  3787. dtlck->index++;
  3788. /* open new linelock */
  3789. if (dtlck->index < dtlck->maxcnt)
  3790. lv++;
  3791. else {
  3792. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3793. lv = & dtlck->lv[0];
  3794. }
  3795. lv->offset = si;
  3796. n = 0;
  3797. }
  3798. n++;
  3799. xsi = si;
  3800. freecnt++;
  3801. t = &p->slot[si];
  3802. t->cnt = 1;
  3803. si = t->next;
  3804. }
  3805. /* close current linelock */
  3806. lv->length = n;
  3807. dtlck->index++;
  3808. *dtlock = dtlck;
  3809. /* update freelist */
  3810. if (freecnt == 0)
  3811. return;
  3812. t->next = p->header.freelist;
  3813. p->header.freelist = fsi;
  3814. p->header.freecnt += freecnt;
  3815. }
  3816. /*
  3817. * dtLinelockFreelist()
  3818. */
  3819. static void dtLinelockFreelist(dtpage_t * p, /* directory page */
  3820. int m, /* max slot index */
  3821. struct dt_lock ** dtlock)
  3822. {
  3823. int fsi; /* free entry slot index */
  3824. struct dtslot *t;
  3825. int si;
  3826. struct dt_lock *dtlck = *dtlock;
  3827. struct lv *lv;
  3828. int xsi, n;
  3829. /* get free entry slot index */
  3830. fsi = p->header.freelist;
  3831. /* open new linelock */
  3832. if (dtlck->index >= dtlck->maxcnt)
  3833. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3834. lv = & dtlck->lv[dtlck->index];
  3835. lv->offset = fsi;
  3836. n = 1;
  3837. xsi = fsi;
  3838. t = &p->slot[fsi];
  3839. si = t->next;
  3840. /* find the last/only segment */
  3841. while (si < m && si >= 0) {
  3842. /* is next slot contiguous ? */
  3843. if (si != xsi + 1) {
  3844. /* close current linelock */
  3845. lv->length = n;
  3846. dtlck->index++;
  3847. /* open new linelock */
  3848. if (dtlck->index < dtlck->maxcnt)
  3849. lv++;
  3850. else {
  3851. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3852. lv = & dtlck->lv[0];
  3853. }
  3854. lv->offset = si;
  3855. n = 0;
  3856. }
  3857. n++;
  3858. xsi = si;
  3859. t = &p->slot[si];
  3860. si = t->next;
  3861. }
  3862. /* close current linelock */
  3863. lv->length = n;
  3864. dtlck->index++;
  3865. *dtlock = dtlck;
  3866. }
  3867. /*
  3868. * NAME: dtModify
  3869. *
  3870. * FUNCTION: Modify the inode number part of a directory entry
  3871. *
  3872. * PARAMETERS:
  3873. * tid - Transaction id
  3874. * ip - Inode of parent directory
  3875. * key - Name of entry to be modified
  3876. * orig_ino - Original inode number expected in entry
  3877. * new_ino - New inode number to put into entry
  3878. * flag - JFS_RENAME
  3879. *
  3880. * RETURNS:
  3881. * -ESTALE - If entry found does not match orig_ino passed in
  3882. * -ENOENT - If no entry can be found to match key
  3883. * 0 - If successfully modified entry
  3884. */
  3885. int dtModify(tid_t tid, struct inode *ip,
  3886. struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
  3887. {
  3888. int rc;
  3889. s64 bn;
  3890. struct metapage *mp;
  3891. dtpage_t *p;
  3892. int index;
  3893. struct btstack btstack;
  3894. struct tlock *tlck;
  3895. struct dt_lock *dtlck;
  3896. struct lv *lv;
  3897. s8 *stbl;
  3898. int entry_si; /* entry slot index */
  3899. struct ldtentry *entry;
  3900. /*
  3901. * search for the entry to modify:
  3902. *
  3903. * dtSearch() returns (leaf page pinned, index at which to modify).
  3904. */
  3905. if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
  3906. return rc;
  3907. /* retrieve search result */
  3908. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  3909. BT_MARK_DIRTY(mp, ip);
  3910. /*
  3911. * acquire a transaction lock on the leaf page of named entry
  3912. */
  3913. tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
  3914. dtlck = (struct dt_lock *) & tlck->lock;
  3915. /* get slot index of the entry */
  3916. stbl = DT_GETSTBL(p);
  3917. entry_si = stbl[index];
  3918. /* linelock entry */
  3919. ASSERT(dtlck->index == 0);
  3920. lv = & dtlck->lv[0];
  3921. lv->offset = entry_si;
  3922. lv->length = 1;
  3923. dtlck->index++;
  3924. /* get the head/only segment */
  3925. entry = (struct ldtentry *) & p->slot[entry_si];
  3926. /* substitute the inode number of the entry */
  3927. entry->inumber = cpu_to_le32(new_ino);
  3928. /* unpin the leaf page */
  3929. DT_PUTPAGE(mp);
  3930. return 0;
  3931. }
  3932. #ifdef _JFS_DEBUG_DTREE
  3933. /*
  3934. * dtDisplayTree()
  3935. *
  3936. * function: traverse forward
  3937. */
  3938. int dtDisplayTree(struct inode *ip)
  3939. {
  3940. int rc;
  3941. struct metapage *mp;
  3942. dtpage_t *p;
  3943. s64 bn, pbn;
  3944. int index, lastindex, v, h;
  3945. pxd_t *xd;
  3946. struct btstack btstack;
  3947. struct btframe *btsp;
  3948. struct btframe *parent;
  3949. u8 *stbl;
  3950. int psize = 256;
  3951. printk("display B+-tree.\n");
  3952. /* clear stack */
  3953. btsp = btstack.stack;
  3954. /*
  3955. * start with root
  3956. *
  3957. * root resides in the inode
  3958. */
  3959. bn = 0;
  3960. v = h = 0;
  3961. /*
  3962. * first access of each page:
  3963. */
  3964. newPage:
  3965. DT_GETPAGE(ip, bn, mp, psize, p, rc);
  3966. if (rc)
  3967. return rc;
  3968. /* process entries forward from first index */
  3969. index = 0;
  3970. lastindex = p->header.nextindex - 1;
  3971. if (p->header.flag & BT_INTERNAL) {
  3972. /*
  3973. * first access of each internal page
  3974. */
  3975. printf("internal page ");
  3976. dtDisplayPage(ip, bn, p);
  3977. goto getChild;
  3978. } else { /* (p->header.flag & BT_LEAF) */
  3979. /*
  3980. * first access of each leaf page
  3981. */
  3982. printf("leaf page ");
  3983. dtDisplayPage(ip, bn, p);
  3984. /*
  3985. * process leaf page entries
  3986. *
  3987. for ( ; index <= lastindex; index++)
  3988. {
  3989. }
  3990. */
  3991. /* unpin the leaf page */
  3992. DT_PUTPAGE(mp);
  3993. }
  3994. /*
  3995. * go back up to the parent page
  3996. */
  3997. getParent:
  3998. /* pop/restore parent entry for the current child page */
  3999. if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
  4000. /* current page must have been root */
  4001. return;
  4002. /*
  4003. * parent page scan completed
  4004. */
  4005. if ((index = parent->index) == (lastindex = parent->lastindex)) {
  4006. /* go back up to the parent page */
  4007. goto getParent;
  4008. }
  4009. /*
  4010. * parent page has entries remaining
  4011. */
  4012. /* get back the parent page */
  4013. bn = parent->bn;
  4014. /* v = parent->level; */
  4015. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  4016. if (rc)
  4017. return rc;
  4018. /* get next parent entry */
  4019. index++;
  4020. /*
  4021. * internal page: go down to child page of current entry
  4022. */
  4023. getChild:
  4024. /* push/save current parent entry for the child page */
  4025. btsp->bn = pbn = bn;
  4026. btsp->index = index;
  4027. btsp->lastindex = lastindex;
  4028. /* btsp->level = v; */
  4029. /* btsp->node = h; */
  4030. ++btsp;
  4031. /* get current entry for the child page */
  4032. stbl = DT_GETSTBL(p);
  4033. xd = (pxd_t *) & p->slot[stbl[index]];
  4034. /*
  4035. * first access of each internal entry:
  4036. */
  4037. /* get child page */
  4038. bn = addressPXD(xd);
  4039. psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
  4040. printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
  4041. v++;
  4042. h = index;
  4043. /* release parent page */
  4044. DT_PUTPAGE(mp);
  4045. /* process the child page */
  4046. goto newPage;
  4047. }
  4048. /*
  4049. * dtDisplayPage()
  4050. *
  4051. * function: display page
  4052. */
  4053. int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
  4054. {
  4055. int rc;
  4056. struct metapage *mp;
  4057. struct ldtentry *lh;
  4058. struct idtentry *ih;
  4059. pxd_t *xd;
  4060. int i, j;
  4061. u8 *stbl;
  4062. wchar_t name[JFS_NAME_MAX + 1];
  4063. struct component_name key = { 0, name };
  4064. int freepage = 0;
  4065. if (p == NULL) {
  4066. freepage = 1;
  4067. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  4068. if (rc)
  4069. return rc;
  4070. }
  4071. /* display page control */
  4072. printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
  4073. bn, p->header.flag, p->header.nextindex);
  4074. /* display entries */
  4075. stbl = DT_GETSTBL(p);
  4076. for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
  4077. dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
  4078. key.name[key.namlen] = '\0';
  4079. if (p->header.flag & BT_LEAF) {
  4080. lh = (struct ldtentry *) & p->slot[stbl[i]];
  4081. printf("\t[%d] %s:%d", i, key.name,
  4082. le32_to_cpu(lh->inumber));
  4083. } else {
  4084. ih = (struct idtentry *) & p->slot[stbl[i]];
  4085. xd = (pxd_t *) ih;
  4086. bn = addressPXD(xd);
  4087. printf("\t[%d] %s:0x%Lx", i, key.name, bn);
  4088. }
  4089. if (j == 4) {
  4090. printf("\n");
  4091. j = 0;
  4092. }
  4093. }
  4094. printf("\n");
  4095. if (freepage)
  4096. DT_PUTPAGE(mp);
  4097. return 0;
  4098. }
  4099. #endif /* _JFS_DEBUG_DTREE */