jfs_dtree.c 100 KB

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