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