jfs_dtree.c 94 KB

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