jfs_dtree.c 100 KB

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