jfs_dtree.c 100 KB

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