jfs_dtree.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287
  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 void 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;
  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. d = (struct ldtentry *) &p->slot[stbl[i]];
  2296. index = le32_to_cpu(d->index);
  2297. if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
  2298. d->index = cpu_to_le32(add_index(tid, inode, bn, i));
  2299. if (dtlck->index >= dtlck->maxcnt)
  2300. dtlck = (struct dt_lock *) txLinelock(dtlck);
  2301. lv = &dtlck->lv[dtlck->index];
  2302. lv->offset = stbl[i];
  2303. lv->length = 1;
  2304. dtlck->index++;
  2305. }
  2306. }
  2307. DT_PUTPAGE(mp);
  2308. (void) txCommit(tid, 1, &inode, 0);
  2309. end:
  2310. txEnd(tid);
  2311. }
  2312. /*
  2313. * Buffer to hold directory entry info while traversing a dtree page
  2314. * before being fed to the filldir function
  2315. */
  2316. struct jfs_dirent {
  2317. loff_t position;
  2318. int ino;
  2319. u16 name_len;
  2320. char name[];
  2321. };
  2322. /*
  2323. * function to determine next variable-sized jfs_dirent in buffer
  2324. */
  2325. static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
  2326. {
  2327. return (struct jfs_dirent *)
  2328. ((char *)dirent +
  2329. ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
  2330. sizeof (loff_t) - 1) &
  2331. ~(sizeof (loff_t) - 1)));
  2332. }
  2333. /*
  2334. * jfs_readdir()
  2335. *
  2336. * function: read directory entries sequentially
  2337. * from the specified entry offset
  2338. *
  2339. * parameter:
  2340. *
  2341. * return: offset = (pn, index) of start entry
  2342. * of next jfs_readdir()/dtRead()
  2343. */
  2344. int jfs_readdir(struct file *file, struct dir_context *ctx)
  2345. {
  2346. struct inode *ip = file_inode(file);
  2347. struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
  2348. int rc = 0;
  2349. loff_t dtpos; /* legacy OS/2 style position */
  2350. struct dtoffset {
  2351. s16 pn;
  2352. s16 index;
  2353. s32 unused;
  2354. } *dtoffset = (struct dtoffset *) &dtpos;
  2355. s64 bn;
  2356. struct metapage *mp;
  2357. dtpage_t *p;
  2358. int index;
  2359. s8 *stbl;
  2360. struct btstack btstack;
  2361. int i, next;
  2362. struct ldtentry *d;
  2363. struct dtslot *t;
  2364. int d_namleft, len, outlen;
  2365. unsigned long dirent_buf;
  2366. char *name_ptr;
  2367. u32 dir_index;
  2368. int do_index = 0;
  2369. uint loop_count = 0;
  2370. struct jfs_dirent *jfs_dirent;
  2371. int jfs_dirents;
  2372. int overflow, fix_page, page_fixed = 0;
  2373. static int unique_pos = 2; /* If we can't fix broken index */
  2374. if (ctx->pos == DIREND)
  2375. return 0;
  2376. if (DO_INDEX(ip)) {
  2377. /*
  2378. * persistent index is stored in directory entries.
  2379. * Special cases: 0 = .
  2380. * 1 = ..
  2381. * -1 = End of directory
  2382. */
  2383. do_index = 1;
  2384. dir_index = (u32) ctx->pos;
  2385. /*
  2386. * NFSv4 reserves cookies 1 and 2 for . and .. so the value
  2387. * we return to the vfs is one greater than the one we use
  2388. * internally.
  2389. */
  2390. if (dir_index)
  2391. dir_index--;
  2392. if (dir_index > 1) {
  2393. struct dir_table_slot dirtab_slot;
  2394. if (dtEmpty(ip) ||
  2395. (dir_index >= JFS_IP(ip)->next_index)) {
  2396. /* Stale position. Directory has shrunk */
  2397. ctx->pos = DIREND;
  2398. return 0;
  2399. }
  2400. repeat:
  2401. rc = read_index(ip, dir_index, &dirtab_slot);
  2402. if (rc) {
  2403. ctx->pos = DIREND;
  2404. return rc;
  2405. }
  2406. if (dirtab_slot.flag == DIR_INDEX_FREE) {
  2407. if (loop_count++ > JFS_IP(ip)->next_index) {
  2408. jfs_err("jfs_readdir detected infinite loop!");
  2409. ctx->pos = DIREND;
  2410. return 0;
  2411. }
  2412. dir_index = le32_to_cpu(dirtab_slot.addr2);
  2413. if (dir_index == -1) {
  2414. ctx->pos = DIREND;
  2415. return 0;
  2416. }
  2417. goto repeat;
  2418. }
  2419. bn = addressDTS(&dirtab_slot);
  2420. index = dirtab_slot.slot;
  2421. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2422. if (rc) {
  2423. ctx->pos = DIREND;
  2424. return 0;
  2425. }
  2426. if (p->header.flag & BT_INTERNAL) {
  2427. jfs_err("jfs_readdir: bad index table");
  2428. DT_PUTPAGE(mp);
  2429. ctx->pos = DIREND;
  2430. return 0;
  2431. }
  2432. } else {
  2433. if (dir_index == 0) {
  2434. /*
  2435. * self "."
  2436. */
  2437. ctx->pos = 1;
  2438. if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
  2439. return 0;
  2440. }
  2441. /*
  2442. * parent ".."
  2443. */
  2444. ctx->pos = 2;
  2445. if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
  2446. return 0;
  2447. /*
  2448. * Find first entry of left-most leaf
  2449. */
  2450. if (dtEmpty(ip)) {
  2451. ctx->pos = DIREND;
  2452. return 0;
  2453. }
  2454. if ((rc = dtReadFirst(ip, &btstack)))
  2455. return rc;
  2456. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2457. }
  2458. } else {
  2459. /*
  2460. * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
  2461. *
  2462. * pn = 0; index = 1: First entry "."
  2463. * pn = 0; index = 2: Second entry ".."
  2464. * pn > 0: Real entries, pn=1 -> leftmost page
  2465. * pn = index = -1: No more entries
  2466. */
  2467. dtpos = ctx->pos;
  2468. if (dtpos < 2) {
  2469. /* build "." entry */
  2470. ctx->pos = 1;
  2471. if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
  2472. return 0;
  2473. dtoffset->index = 2;
  2474. ctx->pos = dtpos;
  2475. }
  2476. if (dtoffset->pn == 0) {
  2477. if (dtoffset->index == 2) {
  2478. /* build ".." entry */
  2479. if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
  2480. return 0;
  2481. } else {
  2482. jfs_err("jfs_readdir called with invalid offset!");
  2483. }
  2484. dtoffset->pn = 1;
  2485. dtoffset->index = 0;
  2486. ctx->pos = dtpos;
  2487. }
  2488. if (dtEmpty(ip)) {
  2489. ctx->pos = DIREND;
  2490. return 0;
  2491. }
  2492. if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
  2493. jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
  2494. rc);
  2495. ctx->pos = DIREND;
  2496. return 0;
  2497. }
  2498. /* get start leaf page and index */
  2499. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2500. /* offset beyond directory eof ? */
  2501. if (bn < 0) {
  2502. ctx->pos = DIREND;
  2503. return 0;
  2504. }
  2505. }
  2506. dirent_buf = __get_free_page(GFP_KERNEL);
  2507. if (dirent_buf == 0) {
  2508. DT_PUTPAGE(mp);
  2509. jfs_warn("jfs_readdir: __get_free_page failed!");
  2510. ctx->pos = DIREND;
  2511. return -ENOMEM;
  2512. }
  2513. while (1) {
  2514. jfs_dirent = (struct jfs_dirent *) dirent_buf;
  2515. jfs_dirents = 0;
  2516. overflow = fix_page = 0;
  2517. stbl = DT_GETSTBL(p);
  2518. for (i = index; i < p->header.nextindex; i++) {
  2519. if (stbl[i] < 0 || stbl[i] > 127) {
  2520. jfs_err("JFS: Invalid stbl[%d] = %d for inode %ld, block = %lld",
  2521. i, stbl[i], (long)ip->i_ino, (long long)bn);
  2522. free_page(dirent_buf);
  2523. DT_PUTPAGE(mp);
  2524. return -EIO;
  2525. }
  2526. d = (struct ldtentry *) & p->slot[stbl[i]];
  2527. if (((long) jfs_dirent + d->namlen + 1) >
  2528. (dirent_buf + PAGE_SIZE)) {
  2529. /* DBCS codepages could overrun dirent_buf */
  2530. index = i;
  2531. overflow = 1;
  2532. break;
  2533. }
  2534. d_namleft = d->namlen;
  2535. name_ptr = jfs_dirent->name;
  2536. jfs_dirent->ino = le32_to_cpu(d->inumber);
  2537. if (do_index) {
  2538. len = min(d_namleft, DTLHDRDATALEN);
  2539. jfs_dirent->position = le32_to_cpu(d->index);
  2540. /*
  2541. * d->index should always be valid, but it
  2542. * isn't. fsck.jfs doesn't create the
  2543. * directory index for the lost+found
  2544. * directory. Rather than let it go,
  2545. * we can try to fix it.
  2546. */
  2547. if ((jfs_dirent->position < 2) ||
  2548. (jfs_dirent->position >=
  2549. JFS_IP(ip)->next_index)) {
  2550. if (!page_fixed && !isReadOnly(ip)) {
  2551. fix_page = 1;
  2552. /*
  2553. * setting overflow and setting
  2554. * index to i will cause the
  2555. * same page to be processed
  2556. * again starting here
  2557. */
  2558. overflow = 1;
  2559. index = i;
  2560. break;
  2561. }
  2562. jfs_dirent->position = unique_pos++;
  2563. }
  2564. /*
  2565. * We add 1 to the index because we may
  2566. * use a value of 2 internally, and NFSv4
  2567. * doesn't like that.
  2568. */
  2569. jfs_dirent->position++;
  2570. } else {
  2571. jfs_dirent->position = dtpos;
  2572. len = min(d_namleft, DTLHDRDATALEN_LEGACY);
  2573. }
  2574. /* copy the name of head/only segment */
  2575. outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
  2576. codepage);
  2577. jfs_dirent->name_len = outlen;
  2578. /* copy name in the additional segment(s) */
  2579. next = d->next;
  2580. while (next >= 0) {
  2581. t = (struct dtslot *) & p->slot[next];
  2582. name_ptr += outlen;
  2583. d_namleft -= len;
  2584. /* Sanity Check */
  2585. if (d_namleft == 0) {
  2586. jfs_error(ip->i_sb,
  2587. "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
  2588. (long)ip->i_ino,
  2589. (long long)bn,
  2590. i);
  2591. goto skip_one;
  2592. }
  2593. len = min(d_namleft, DTSLOTDATALEN);
  2594. outlen = jfs_strfromUCS_le(name_ptr, t->name,
  2595. len, codepage);
  2596. jfs_dirent->name_len += outlen;
  2597. next = t->next;
  2598. }
  2599. jfs_dirents++;
  2600. jfs_dirent = next_jfs_dirent(jfs_dirent);
  2601. skip_one:
  2602. if (!do_index)
  2603. dtoffset->index++;
  2604. }
  2605. if (!overflow) {
  2606. /* Point to next leaf page */
  2607. if (p->header.flag & BT_ROOT)
  2608. bn = 0;
  2609. else {
  2610. bn = le64_to_cpu(p->header.next);
  2611. index = 0;
  2612. /* update offset (pn:index) for new page */
  2613. if (!do_index) {
  2614. dtoffset->pn++;
  2615. dtoffset->index = 0;
  2616. }
  2617. }
  2618. page_fixed = 0;
  2619. }
  2620. /* unpin previous leaf page */
  2621. DT_PUTPAGE(mp);
  2622. jfs_dirent = (struct jfs_dirent *) dirent_buf;
  2623. while (jfs_dirents--) {
  2624. ctx->pos = jfs_dirent->position;
  2625. if (!dir_emit(ctx, jfs_dirent->name,
  2626. jfs_dirent->name_len,
  2627. jfs_dirent->ino, DT_UNKNOWN))
  2628. goto out;
  2629. jfs_dirent = next_jfs_dirent(jfs_dirent);
  2630. }
  2631. if (fix_page) {
  2632. add_missing_indices(ip, bn);
  2633. page_fixed = 1;
  2634. }
  2635. if (!overflow && (bn == 0)) {
  2636. ctx->pos = DIREND;
  2637. break;
  2638. }
  2639. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2640. if (rc) {
  2641. free_page(dirent_buf);
  2642. return rc;
  2643. }
  2644. }
  2645. out:
  2646. free_page(dirent_buf);
  2647. return rc;
  2648. }
  2649. /*
  2650. * dtReadFirst()
  2651. *
  2652. * function: get the leftmost page of the directory
  2653. */
  2654. static int dtReadFirst(struct inode *ip, struct btstack * btstack)
  2655. {
  2656. int rc = 0;
  2657. s64 bn;
  2658. int psize = 288; /* initial in-line directory */
  2659. struct metapage *mp;
  2660. dtpage_t *p;
  2661. s8 *stbl;
  2662. struct btframe *btsp;
  2663. pxd_t *xd;
  2664. BT_CLR(btstack); /* reset stack */
  2665. /*
  2666. * descend leftmost path of the tree
  2667. *
  2668. * by convention, root bn = 0.
  2669. */
  2670. for (bn = 0;;) {
  2671. DT_GETPAGE(ip, bn, mp, psize, p, rc);
  2672. if (rc)
  2673. return rc;
  2674. /*
  2675. * leftmost leaf page
  2676. */
  2677. if (p->header.flag & BT_LEAF) {
  2678. /* return leftmost entry */
  2679. btsp = btstack->top;
  2680. btsp->bn = bn;
  2681. btsp->index = 0;
  2682. btsp->mp = mp;
  2683. return 0;
  2684. }
  2685. /*
  2686. * descend down to leftmost child page
  2687. */
  2688. if (BT_STACK_FULL(btstack)) {
  2689. DT_PUTPAGE(mp);
  2690. jfs_error(ip->i_sb, "btstack overrun\n");
  2691. BT_STACK_DUMP(btstack);
  2692. return -EIO;
  2693. }
  2694. /* push (bn, index) of the parent page/entry */
  2695. BT_PUSH(btstack, bn, 0);
  2696. /* get the leftmost entry */
  2697. stbl = DT_GETSTBL(p);
  2698. if (stbl[0] < 0 || stbl[0] > 127) {
  2699. DT_PUTPAGE(mp);
  2700. jfs_error(ip->i_sb, "stbl[0] out of bound\n");
  2701. return -EIO;
  2702. }
  2703. xd = (pxd_t *) & p->slot[stbl[0]];
  2704. /* get the child page block address */
  2705. bn = addressPXD(xd);
  2706. psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
  2707. /* unpin the parent page */
  2708. DT_PUTPAGE(mp);
  2709. }
  2710. }
  2711. /*
  2712. * dtReadNext()
  2713. *
  2714. * function: get the page of the specified offset (pn:index)
  2715. *
  2716. * return: if (offset > eof), bn = -1;
  2717. *
  2718. * note: if index > nextindex of the target leaf page,
  2719. * start with 1st entry of next leaf page;
  2720. */
  2721. static int dtReadNext(struct inode *ip, loff_t * offset,
  2722. struct btstack * btstack)
  2723. {
  2724. int rc = 0;
  2725. struct dtoffset {
  2726. s16 pn;
  2727. s16 index;
  2728. s32 unused;
  2729. } *dtoffset = (struct dtoffset *) offset;
  2730. s64 bn;
  2731. struct metapage *mp;
  2732. dtpage_t *p;
  2733. int index;
  2734. int pn;
  2735. s8 *stbl;
  2736. struct btframe *btsp, *parent;
  2737. pxd_t *xd;
  2738. /*
  2739. * get leftmost leaf page pinned
  2740. */
  2741. if ((rc = dtReadFirst(ip, btstack)))
  2742. return rc;
  2743. /* get leaf page */
  2744. DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
  2745. /* get the start offset (pn:index) */
  2746. pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
  2747. index = dtoffset->index;
  2748. /* start at leftmost page ? */
  2749. if (pn == 0) {
  2750. /* offset beyond eof ? */
  2751. if (index < p->header.nextindex)
  2752. goto out;
  2753. if (p->header.flag & BT_ROOT) {
  2754. bn = -1;
  2755. goto out;
  2756. }
  2757. /* start with 1st entry of next leaf page */
  2758. dtoffset->pn++;
  2759. dtoffset->index = index = 0;
  2760. goto a;
  2761. }
  2762. /* start at non-leftmost page: scan parent pages for large pn */
  2763. if (p->header.flag & BT_ROOT) {
  2764. bn = -1;
  2765. goto out;
  2766. }
  2767. /* start after next leaf page ? */
  2768. if (pn > 1)
  2769. goto b;
  2770. /* get leaf page pn = 1 */
  2771. a:
  2772. bn = le64_to_cpu(p->header.next);
  2773. /* unpin leaf page */
  2774. DT_PUTPAGE(mp);
  2775. /* offset beyond eof ? */
  2776. if (bn == 0) {
  2777. bn = -1;
  2778. goto out;
  2779. }
  2780. goto c;
  2781. /*
  2782. * scan last internal page level to get target leaf page
  2783. */
  2784. b:
  2785. /* unpin leftmost leaf page */
  2786. DT_PUTPAGE(mp);
  2787. /* get left most parent page */
  2788. btsp = btstack->top;
  2789. parent = btsp - 1;
  2790. bn = parent->bn;
  2791. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2792. if (rc)
  2793. return rc;
  2794. /* scan parent pages at last internal page level */
  2795. while (pn >= p->header.nextindex) {
  2796. pn -= p->header.nextindex;
  2797. /* get next parent page address */
  2798. bn = le64_to_cpu(p->header.next);
  2799. /* unpin current parent page */
  2800. DT_PUTPAGE(mp);
  2801. /* offset beyond eof ? */
  2802. if (bn == 0) {
  2803. bn = -1;
  2804. goto out;
  2805. }
  2806. /* get next parent page */
  2807. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2808. if (rc)
  2809. return rc;
  2810. /* update parent page stack frame */
  2811. parent->bn = bn;
  2812. }
  2813. /* get leaf page address */
  2814. stbl = DT_GETSTBL(p);
  2815. xd = (pxd_t *) & p->slot[stbl[pn]];
  2816. bn = addressPXD(xd);
  2817. /* unpin parent page */
  2818. DT_PUTPAGE(mp);
  2819. /*
  2820. * get target leaf page
  2821. */
  2822. c:
  2823. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2824. if (rc)
  2825. return rc;
  2826. /*
  2827. * leaf page has been completed:
  2828. * start with 1st entry of next leaf page
  2829. */
  2830. if (index >= p->header.nextindex) {
  2831. bn = le64_to_cpu(p->header.next);
  2832. /* unpin leaf page */
  2833. DT_PUTPAGE(mp);
  2834. /* offset beyond eof ? */
  2835. if (bn == 0) {
  2836. bn = -1;
  2837. goto out;
  2838. }
  2839. /* get next leaf page */
  2840. DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2841. if (rc)
  2842. return rc;
  2843. /* start with 1st entry of next leaf page */
  2844. dtoffset->pn++;
  2845. dtoffset->index = 0;
  2846. }
  2847. out:
  2848. /* return target leaf page pinned */
  2849. btsp = btstack->top;
  2850. btsp->bn = bn;
  2851. btsp->index = dtoffset->index;
  2852. btsp->mp = mp;
  2853. return 0;
  2854. }
  2855. /*
  2856. * dtCompare()
  2857. *
  2858. * function: compare search key with an internal entry
  2859. *
  2860. * return:
  2861. * < 0 if k is < record
  2862. * = 0 if k is = record
  2863. * > 0 if k is > record
  2864. */
  2865. static int dtCompare(struct component_name * key, /* search key */
  2866. dtpage_t * p, /* directory page */
  2867. int si)
  2868. { /* entry slot index */
  2869. wchar_t *kname;
  2870. __le16 *name;
  2871. int klen, namlen, len, rc;
  2872. struct idtentry *ih;
  2873. struct dtslot *t;
  2874. /*
  2875. * force the left-most key on internal pages, at any level of
  2876. * the tree, to be less than any search key.
  2877. * this obviates having to update the leftmost key on an internal
  2878. * page when the user inserts a new key in the tree smaller than
  2879. * anything that has been stored.
  2880. *
  2881. * (? if/when dtSearch() narrows down to 1st entry (index = 0),
  2882. * at any internal page at any level of the tree,
  2883. * it descends to child of the entry anyway -
  2884. * ? make the entry as min size dummy entry)
  2885. *
  2886. * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
  2887. * return (1);
  2888. */
  2889. kname = key->name;
  2890. klen = key->namlen;
  2891. ih = (struct idtentry *) & p->slot[si];
  2892. si = ih->next;
  2893. name = ih->name;
  2894. namlen = ih->namlen;
  2895. len = min(namlen, DTIHDRDATALEN);
  2896. /* compare with head/only segment */
  2897. len = min(klen, len);
  2898. if ((rc = UniStrncmp_le(kname, name, len)))
  2899. return rc;
  2900. klen -= len;
  2901. namlen -= len;
  2902. /* compare with additional segment(s) */
  2903. kname += len;
  2904. while (klen > 0 && namlen > 0) {
  2905. /* compare with next name segment */
  2906. t = (struct dtslot *) & p->slot[si];
  2907. len = min(namlen, DTSLOTDATALEN);
  2908. len = min(klen, len);
  2909. name = t->name;
  2910. if ((rc = UniStrncmp_le(kname, name, len)))
  2911. return rc;
  2912. klen -= len;
  2913. namlen -= len;
  2914. kname += len;
  2915. si = t->next;
  2916. }
  2917. return (klen - namlen);
  2918. }
  2919. /*
  2920. * ciCompare()
  2921. *
  2922. * function: compare search key with an (leaf/internal) entry
  2923. *
  2924. * return:
  2925. * < 0 if k is < record
  2926. * = 0 if k is = record
  2927. * > 0 if k is > record
  2928. */
  2929. static int ciCompare(struct component_name * key, /* search key */
  2930. dtpage_t * p, /* directory page */
  2931. int si, /* entry slot index */
  2932. int flag)
  2933. {
  2934. wchar_t *kname, x;
  2935. __le16 *name;
  2936. int klen, namlen, len, rc;
  2937. struct ldtentry *lh;
  2938. struct idtentry *ih;
  2939. struct dtslot *t;
  2940. int i;
  2941. /*
  2942. * force the left-most key on internal pages, at any level of
  2943. * the tree, to be less than any search key.
  2944. * this obviates having to update the leftmost key on an internal
  2945. * page when the user inserts a new key in the tree smaller than
  2946. * anything that has been stored.
  2947. *
  2948. * (? if/when dtSearch() narrows down to 1st entry (index = 0),
  2949. * at any internal page at any level of the tree,
  2950. * it descends to child of the entry anyway -
  2951. * ? make the entry as min size dummy entry)
  2952. *
  2953. * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
  2954. * return (1);
  2955. */
  2956. kname = key->name;
  2957. klen = key->namlen;
  2958. /*
  2959. * leaf page entry
  2960. */
  2961. if (p->header.flag & BT_LEAF) {
  2962. lh = (struct ldtentry *) & p->slot[si];
  2963. si = lh->next;
  2964. name = lh->name;
  2965. namlen = lh->namlen;
  2966. if (flag & JFS_DIR_INDEX)
  2967. len = min(namlen, DTLHDRDATALEN);
  2968. else
  2969. len = min(namlen, DTLHDRDATALEN_LEGACY);
  2970. }
  2971. /*
  2972. * internal page entry
  2973. */
  2974. else {
  2975. ih = (struct idtentry *) & p->slot[si];
  2976. si = ih->next;
  2977. name = ih->name;
  2978. namlen = ih->namlen;
  2979. len = min(namlen, DTIHDRDATALEN);
  2980. }
  2981. /* compare with head/only segment */
  2982. len = min(klen, len);
  2983. for (i = 0; i < len; i++, kname++, name++) {
  2984. /* only uppercase if case-insensitive support is on */
  2985. if ((flag & JFS_OS2) == JFS_OS2)
  2986. x = UniToupper(le16_to_cpu(*name));
  2987. else
  2988. x = le16_to_cpu(*name);
  2989. if ((rc = *kname - x))
  2990. return rc;
  2991. }
  2992. klen -= len;
  2993. namlen -= len;
  2994. /* compare with additional segment(s) */
  2995. while (klen > 0 && namlen > 0) {
  2996. /* compare with next name segment */
  2997. t = (struct dtslot *) & p->slot[si];
  2998. len = min(namlen, DTSLOTDATALEN);
  2999. len = min(klen, len);
  3000. name = t->name;
  3001. for (i = 0; i < len; i++, kname++, name++) {
  3002. /* only uppercase if case-insensitive support is on */
  3003. if ((flag & JFS_OS2) == JFS_OS2)
  3004. x = UniToupper(le16_to_cpu(*name));
  3005. else
  3006. x = le16_to_cpu(*name);
  3007. if ((rc = *kname - x))
  3008. return rc;
  3009. }
  3010. klen -= len;
  3011. namlen -= len;
  3012. si = t->next;
  3013. }
  3014. return (klen - namlen);
  3015. }
  3016. /*
  3017. * ciGetLeafPrefixKey()
  3018. *
  3019. * function: compute prefix of suffix compression
  3020. * from two adjacent leaf entries
  3021. * across page boundary
  3022. *
  3023. * return: non-zero on error
  3024. *
  3025. */
  3026. static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
  3027. int ri, struct component_name * key, int flag)
  3028. {
  3029. int klen, namlen;
  3030. wchar_t *pl, *pr, *kname;
  3031. struct component_name lkey;
  3032. struct component_name rkey;
  3033. lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
  3034. GFP_KERNEL);
  3035. if (lkey.name == NULL)
  3036. return -ENOMEM;
  3037. rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
  3038. GFP_KERNEL);
  3039. if (rkey.name == NULL) {
  3040. kfree(lkey.name);
  3041. return -ENOMEM;
  3042. }
  3043. /* get left and right key */
  3044. dtGetKey(lp, li, &lkey, flag);
  3045. lkey.name[lkey.namlen] = 0;
  3046. if ((flag & JFS_OS2) == JFS_OS2)
  3047. ciToUpper(&lkey);
  3048. dtGetKey(rp, ri, &rkey, flag);
  3049. rkey.name[rkey.namlen] = 0;
  3050. if ((flag & JFS_OS2) == JFS_OS2)
  3051. ciToUpper(&rkey);
  3052. /* compute prefix */
  3053. klen = 0;
  3054. kname = key->name;
  3055. namlen = min(lkey.namlen, rkey.namlen);
  3056. for (pl = lkey.name, pr = rkey.name;
  3057. namlen; pl++, pr++, namlen--, klen++, kname++) {
  3058. *kname = *pr;
  3059. if (*pl != *pr) {
  3060. key->namlen = klen + 1;
  3061. goto free_names;
  3062. }
  3063. }
  3064. /* l->namlen <= r->namlen since l <= r */
  3065. if (lkey.namlen < rkey.namlen) {
  3066. *kname = *pr;
  3067. key->namlen = klen + 1;
  3068. } else /* l->namelen == r->namelen */
  3069. key->namlen = klen;
  3070. free_names:
  3071. kfree(lkey.name);
  3072. kfree(rkey.name);
  3073. return 0;
  3074. }
  3075. /*
  3076. * dtGetKey()
  3077. *
  3078. * function: get key of the entry
  3079. */
  3080. static void dtGetKey(dtpage_t * p, int i, /* entry index */
  3081. struct component_name * key, int flag)
  3082. {
  3083. int si;
  3084. s8 *stbl;
  3085. struct ldtentry *lh;
  3086. struct idtentry *ih;
  3087. struct dtslot *t;
  3088. int namlen, len;
  3089. wchar_t *kname;
  3090. __le16 *name;
  3091. /* get entry */
  3092. stbl = DT_GETSTBL(p);
  3093. si = stbl[i];
  3094. if (p->header.flag & BT_LEAF) {
  3095. lh = (struct ldtentry *) & p->slot[si];
  3096. si = lh->next;
  3097. namlen = lh->namlen;
  3098. name = lh->name;
  3099. if (flag & JFS_DIR_INDEX)
  3100. len = min(namlen, DTLHDRDATALEN);
  3101. else
  3102. len = min(namlen, DTLHDRDATALEN_LEGACY);
  3103. } else {
  3104. ih = (struct idtentry *) & p->slot[si];
  3105. si = ih->next;
  3106. namlen = ih->namlen;
  3107. name = ih->name;
  3108. len = min(namlen, DTIHDRDATALEN);
  3109. }
  3110. key->namlen = namlen;
  3111. kname = key->name;
  3112. /*
  3113. * move head/only segment
  3114. */
  3115. UniStrncpy_from_le(kname, name, len);
  3116. /*
  3117. * move additional segment(s)
  3118. */
  3119. while (si >= 0) {
  3120. /* get next segment */
  3121. t = &p->slot[si];
  3122. kname += len;
  3123. namlen -= len;
  3124. len = min(namlen, DTSLOTDATALEN);
  3125. UniStrncpy_from_le(kname, t->name, len);
  3126. si = t->next;
  3127. }
  3128. }
  3129. /*
  3130. * dtInsertEntry()
  3131. *
  3132. * function: allocate free slot(s) and
  3133. * write a leaf/internal entry
  3134. *
  3135. * return: entry slot index
  3136. */
  3137. static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
  3138. ddata_t * data, struct dt_lock ** dtlock)
  3139. {
  3140. struct dtslot *h, *t;
  3141. struct ldtentry *lh = NULL;
  3142. struct idtentry *ih = NULL;
  3143. int hsi, fsi, klen, len, nextindex;
  3144. wchar_t *kname;
  3145. __le16 *name;
  3146. s8 *stbl;
  3147. pxd_t *xd;
  3148. struct dt_lock *dtlck = *dtlock;
  3149. struct lv *lv;
  3150. int xsi, n;
  3151. s64 bn = 0;
  3152. struct metapage *mp = NULL;
  3153. klen = key->namlen;
  3154. kname = key->name;
  3155. /* allocate a free slot */
  3156. hsi = fsi = p->header.freelist;
  3157. h = &p->slot[fsi];
  3158. p->header.freelist = h->next;
  3159. --p->header.freecnt;
  3160. /* open new linelock */
  3161. if (dtlck->index >= dtlck->maxcnt)
  3162. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3163. lv = & dtlck->lv[dtlck->index];
  3164. lv->offset = hsi;
  3165. /* write head/only segment */
  3166. if (p->header.flag & BT_LEAF) {
  3167. lh = (struct ldtentry *) h;
  3168. lh->next = h->next;
  3169. lh->inumber = cpu_to_le32(data->leaf.ino);
  3170. lh->namlen = klen;
  3171. name = lh->name;
  3172. if (data->leaf.ip) {
  3173. len = min(klen, DTLHDRDATALEN);
  3174. if (!(p->header.flag & BT_ROOT))
  3175. bn = addressPXD(&p->header.self);
  3176. lh->index = cpu_to_le32(add_index(data->leaf.tid,
  3177. data->leaf.ip,
  3178. bn, index));
  3179. } else
  3180. len = min(klen, DTLHDRDATALEN_LEGACY);
  3181. } else {
  3182. ih = (struct idtentry *) h;
  3183. ih->next = h->next;
  3184. xd = (pxd_t *) ih;
  3185. *xd = data->xd;
  3186. ih->namlen = klen;
  3187. name = ih->name;
  3188. len = min(klen, DTIHDRDATALEN);
  3189. }
  3190. UniStrncpy_to_le(name, kname, len);
  3191. n = 1;
  3192. xsi = hsi;
  3193. /* write additional segment(s) */
  3194. t = h;
  3195. klen -= len;
  3196. while (klen) {
  3197. /* get free slot */
  3198. fsi = p->header.freelist;
  3199. t = &p->slot[fsi];
  3200. p->header.freelist = t->next;
  3201. --p->header.freecnt;
  3202. /* is next slot contiguous ? */
  3203. if (fsi != xsi + 1) {
  3204. /* close current linelock */
  3205. lv->length = n;
  3206. dtlck->index++;
  3207. /* open new linelock */
  3208. if (dtlck->index < dtlck->maxcnt)
  3209. lv++;
  3210. else {
  3211. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3212. lv = & dtlck->lv[0];
  3213. }
  3214. lv->offset = fsi;
  3215. n = 0;
  3216. }
  3217. kname += len;
  3218. len = min(klen, DTSLOTDATALEN);
  3219. UniStrncpy_to_le(t->name, kname, len);
  3220. n++;
  3221. xsi = fsi;
  3222. klen -= len;
  3223. }
  3224. /* close current linelock */
  3225. lv->length = n;
  3226. dtlck->index++;
  3227. *dtlock = dtlck;
  3228. /* terminate last/only segment */
  3229. if (h == t) {
  3230. /* single segment entry */
  3231. if (p->header.flag & BT_LEAF)
  3232. lh->next = -1;
  3233. else
  3234. ih->next = -1;
  3235. } else
  3236. /* multi-segment entry */
  3237. t->next = -1;
  3238. /* if insert into middle, shift right succeeding entries in stbl */
  3239. stbl = DT_GETSTBL(p);
  3240. nextindex = p->header.nextindex;
  3241. if (index < nextindex) {
  3242. memmove(stbl + index + 1, stbl + index, nextindex - index);
  3243. if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
  3244. s64 lblock;
  3245. /*
  3246. * Need to update slot number for entries that moved
  3247. * in the stbl
  3248. */
  3249. mp = NULL;
  3250. for (n = index + 1; n <= nextindex; n++) {
  3251. lh = (struct ldtentry *) & (p->slot[stbl[n]]);
  3252. modify_index(data->leaf.tid, data->leaf.ip,
  3253. le32_to_cpu(lh->index), bn, n,
  3254. &mp, &lblock);
  3255. }
  3256. if (mp)
  3257. release_metapage(mp);
  3258. }
  3259. }
  3260. stbl[index] = hsi;
  3261. /* advance next available entry index of stbl */
  3262. ++p->header.nextindex;
  3263. }
  3264. /*
  3265. * dtMoveEntry()
  3266. *
  3267. * function: move entries from split/left page to new/right page
  3268. *
  3269. * nextindex of dst page and freelist/freecnt of both pages
  3270. * are updated.
  3271. */
  3272. static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
  3273. struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
  3274. int do_index)
  3275. {
  3276. int ssi, next; /* src slot index */
  3277. int di; /* dst entry index */
  3278. int dsi; /* dst slot index */
  3279. s8 *sstbl, *dstbl; /* sorted entry table */
  3280. int snamlen, len;
  3281. struct ldtentry *slh, *dlh = NULL;
  3282. struct idtentry *sih, *dih = NULL;
  3283. struct dtslot *h, *s, *d;
  3284. struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
  3285. struct lv *slv, *dlv;
  3286. int xssi, ns, nd;
  3287. int sfsi;
  3288. sstbl = (s8 *) & sp->slot[sp->header.stblindex];
  3289. dstbl = (s8 *) & dp->slot[dp->header.stblindex];
  3290. dsi = dp->header.freelist; /* first (whole page) free slot */
  3291. sfsi = sp->header.freelist;
  3292. /* linelock destination entry slot */
  3293. dlv = & ddtlck->lv[ddtlck->index];
  3294. dlv->offset = dsi;
  3295. /* linelock source entry slot */
  3296. slv = & sdtlck->lv[sdtlck->index];
  3297. slv->offset = sstbl[si];
  3298. xssi = slv->offset - 1;
  3299. /*
  3300. * move entries
  3301. */
  3302. ns = nd = 0;
  3303. for (di = 0; si < sp->header.nextindex; si++, di++) {
  3304. ssi = sstbl[si];
  3305. dstbl[di] = dsi;
  3306. /* is next slot contiguous ? */
  3307. if (ssi != xssi + 1) {
  3308. /* close current linelock */
  3309. slv->length = ns;
  3310. sdtlck->index++;
  3311. /* open new linelock */
  3312. if (sdtlck->index < sdtlck->maxcnt)
  3313. slv++;
  3314. else {
  3315. sdtlck = (struct dt_lock *) txLinelock(sdtlck);
  3316. slv = & sdtlck->lv[0];
  3317. }
  3318. slv->offset = ssi;
  3319. ns = 0;
  3320. }
  3321. /*
  3322. * move head/only segment of an entry
  3323. */
  3324. /* get dst slot */
  3325. h = d = &dp->slot[dsi];
  3326. /* get src slot and move */
  3327. s = &sp->slot[ssi];
  3328. if (sp->header.flag & BT_LEAF) {
  3329. /* get source entry */
  3330. slh = (struct ldtentry *) s;
  3331. dlh = (struct ldtentry *) h;
  3332. snamlen = slh->namlen;
  3333. if (do_index) {
  3334. len = min(snamlen, DTLHDRDATALEN);
  3335. dlh->index = slh->index; /* little-endian */
  3336. } else
  3337. len = min(snamlen, DTLHDRDATALEN_LEGACY);
  3338. memcpy(dlh, slh, 6 + len * 2);
  3339. next = slh->next;
  3340. /* update dst head/only segment next field */
  3341. dsi++;
  3342. dlh->next = dsi;
  3343. } else {
  3344. sih = (struct idtentry *) s;
  3345. snamlen = sih->namlen;
  3346. len = min(snamlen, DTIHDRDATALEN);
  3347. dih = (struct idtentry *) h;
  3348. memcpy(dih, sih, 10 + len * 2);
  3349. next = sih->next;
  3350. dsi++;
  3351. dih->next = dsi;
  3352. }
  3353. /* free src head/only segment */
  3354. s->next = sfsi;
  3355. s->cnt = 1;
  3356. sfsi = ssi;
  3357. ns++;
  3358. nd++;
  3359. xssi = ssi;
  3360. /*
  3361. * move additional segment(s) of the entry
  3362. */
  3363. snamlen -= len;
  3364. while ((ssi = next) >= 0) {
  3365. /* is next slot contiguous ? */
  3366. if (ssi != xssi + 1) {
  3367. /* close current linelock */
  3368. slv->length = ns;
  3369. sdtlck->index++;
  3370. /* open new linelock */
  3371. if (sdtlck->index < sdtlck->maxcnt)
  3372. slv++;
  3373. else {
  3374. sdtlck =
  3375. (struct dt_lock *)
  3376. txLinelock(sdtlck);
  3377. slv = & sdtlck->lv[0];
  3378. }
  3379. slv->offset = ssi;
  3380. ns = 0;
  3381. }
  3382. /* get next source segment */
  3383. s = &sp->slot[ssi];
  3384. /* get next destination free slot */
  3385. d++;
  3386. len = min(snamlen, DTSLOTDATALEN);
  3387. UniStrncpy_le(d->name, s->name, len);
  3388. ns++;
  3389. nd++;
  3390. xssi = ssi;
  3391. dsi++;
  3392. d->next = dsi;
  3393. /* free source segment */
  3394. next = s->next;
  3395. s->next = sfsi;
  3396. s->cnt = 1;
  3397. sfsi = ssi;
  3398. snamlen -= len;
  3399. } /* end while */
  3400. /* terminate dst last/only segment */
  3401. if (h == d) {
  3402. /* single segment entry */
  3403. if (dp->header.flag & BT_LEAF)
  3404. dlh->next = -1;
  3405. else
  3406. dih->next = -1;
  3407. } else
  3408. /* multi-segment entry */
  3409. d->next = -1;
  3410. } /* end for */
  3411. /* close current linelock */
  3412. slv->length = ns;
  3413. sdtlck->index++;
  3414. *sdtlock = sdtlck;
  3415. dlv->length = nd;
  3416. ddtlck->index++;
  3417. *ddtlock = ddtlck;
  3418. /* update source header */
  3419. sp->header.freelist = sfsi;
  3420. sp->header.freecnt += nd;
  3421. /* update destination header */
  3422. dp->header.nextindex = di;
  3423. dp->header.freelist = dsi;
  3424. dp->header.freecnt -= nd;
  3425. }
  3426. /*
  3427. * dtDeleteEntry()
  3428. *
  3429. * function: free a (leaf/internal) entry
  3430. *
  3431. * log freelist header, stbl, and each segment slot of entry
  3432. * (even though last/only segment next field is modified,
  3433. * physical image logging requires all segment slots of
  3434. * the entry logged to avoid applying previous updates
  3435. * to the same slots)
  3436. */
  3437. static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
  3438. {
  3439. int fsi; /* free entry slot index */
  3440. s8 *stbl;
  3441. struct dtslot *t;
  3442. int si, freecnt;
  3443. struct dt_lock *dtlck = *dtlock;
  3444. struct lv *lv;
  3445. int xsi, n;
  3446. /* get free entry slot index */
  3447. stbl = DT_GETSTBL(p);
  3448. fsi = stbl[fi];
  3449. /* open new linelock */
  3450. if (dtlck->index >= dtlck->maxcnt)
  3451. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3452. lv = & dtlck->lv[dtlck->index];
  3453. lv->offset = fsi;
  3454. /* get the head/only segment */
  3455. t = &p->slot[fsi];
  3456. if (p->header.flag & BT_LEAF)
  3457. si = ((struct ldtentry *) t)->next;
  3458. else
  3459. si = ((struct idtentry *) t)->next;
  3460. t->next = si;
  3461. t->cnt = 1;
  3462. n = freecnt = 1;
  3463. xsi = fsi;
  3464. /* find the last/only segment */
  3465. while (si >= 0) {
  3466. /* is next slot contiguous ? */
  3467. if (si != xsi + 1) {
  3468. /* close current linelock */
  3469. lv->length = n;
  3470. dtlck->index++;
  3471. /* open new linelock */
  3472. if (dtlck->index < dtlck->maxcnt)
  3473. lv++;
  3474. else {
  3475. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3476. lv = & dtlck->lv[0];
  3477. }
  3478. lv->offset = si;
  3479. n = 0;
  3480. }
  3481. n++;
  3482. xsi = si;
  3483. freecnt++;
  3484. t = &p->slot[si];
  3485. t->cnt = 1;
  3486. si = t->next;
  3487. }
  3488. /* close current linelock */
  3489. lv->length = n;
  3490. dtlck->index++;
  3491. *dtlock = dtlck;
  3492. /* update freelist */
  3493. t->next = p->header.freelist;
  3494. p->header.freelist = fsi;
  3495. p->header.freecnt += freecnt;
  3496. /* if delete from middle,
  3497. * shift left the succedding entries in the stbl
  3498. */
  3499. si = p->header.nextindex;
  3500. if (fi < si - 1)
  3501. memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
  3502. p->header.nextindex--;
  3503. }
  3504. /*
  3505. * dtTruncateEntry()
  3506. *
  3507. * function: truncate a (leaf/internal) entry
  3508. *
  3509. * log freelist header, stbl, and each segment slot of entry
  3510. * (even though last/only segment next field is modified,
  3511. * physical image logging requires all segment slots of
  3512. * the entry logged to avoid applying previous updates
  3513. * to the same slots)
  3514. */
  3515. static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
  3516. {
  3517. int tsi; /* truncate entry slot index */
  3518. s8 *stbl;
  3519. struct dtslot *t;
  3520. int si, freecnt;
  3521. struct dt_lock *dtlck = *dtlock;
  3522. struct lv *lv;
  3523. int fsi, xsi, n;
  3524. /* get free entry slot index */
  3525. stbl = DT_GETSTBL(p);
  3526. tsi = stbl[ti];
  3527. /* open new linelock */
  3528. if (dtlck->index >= dtlck->maxcnt)
  3529. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3530. lv = & dtlck->lv[dtlck->index];
  3531. lv->offset = tsi;
  3532. /* get the head/only segment */
  3533. t = &p->slot[tsi];
  3534. ASSERT(p->header.flag & BT_INTERNAL);
  3535. ((struct idtentry *) t)->namlen = 0;
  3536. si = ((struct idtentry *) t)->next;
  3537. ((struct idtentry *) t)->next = -1;
  3538. n = 1;
  3539. freecnt = 0;
  3540. fsi = si;
  3541. xsi = tsi;
  3542. /* find the last/only segment */
  3543. while (si >= 0) {
  3544. /* is next slot contiguous ? */
  3545. if (si != xsi + 1) {
  3546. /* close current linelock */
  3547. lv->length = n;
  3548. dtlck->index++;
  3549. /* open new linelock */
  3550. if (dtlck->index < dtlck->maxcnt)
  3551. lv++;
  3552. else {
  3553. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3554. lv = & dtlck->lv[0];
  3555. }
  3556. lv->offset = si;
  3557. n = 0;
  3558. }
  3559. n++;
  3560. xsi = si;
  3561. freecnt++;
  3562. t = &p->slot[si];
  3563. t->cnt = 1;
  3564. si = t->next;
  3565. }
  3566. /* close current linelock */
  3567. lv->length = n;
  3568. dtlck->index++;
  3569. *dtlock = dtlck;
  3570. /* update freelist */
  3571. if (freecnt == 0)
  3572. return;
  3573. t->next = p->header.freelist;
  3574. p->header.freelist = fsi;
  3575. p->header.freecnt += freecnt;
  3576. }
  3577. /*
  3578. * dtLinelockFreelist()
  3579. */
  3580. static void dtLinelockFreelist(dtpage_t * p, /* directory page */
  3581. int m, /* max slot index */
  3582. struct dt_lock ** dtlock)
  3583. {
  3584. int fsi; /* free entry slot index */
  3585. struct dtslot *t;
  3586. int si;
  3587. struct dt_lock *dtlck = *dtlock;
  3588. struct lv *lv;
  3589. int xsi, n;
  3590. /* get free entry slot index */
  3591. fsi = p->header.freelist;
  3592. /* open new linelock */
  3593. if (dtlck->index >= dtlck->maxcnt)
  3594. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3595. lv = & dtlck->lv[dtlck->index];
  3596. lv->offset = fsi;
  3597. n = 1;
  3598. xsi = fsi;
  3599. t = &p->slot[fsi];
  3600. si = t->next;
  3601. /* find the last/only segment */
  3602. while (si < m && si >= 0) {
  3603. /* is next slot contiguous ? */
  3604. if (si != xsi + 1) {
  3605. /* close current linelock */
  3606. lv->length = n;
  3607. dtlck->index++;
  3608. /* open new linelock */
  3609. if (dtlck->index < dtlck->maxcnt)
  3610. lv++;
  3611. else {
  3612. dtlck = (struct dt_lock *) txLinelock(dtlck);
  3613. lv = & dtlck->lv[0];
  3614. }
  3615. lv->offset = si;
  3616. n = 0;
  3617. }
  3618. n++;
  3619. xsi = si;
  3620. t = &p->slot[si];
  3621. si = t->next;
  3622. }
  3623. /* close current linelock */
  3624. lv->length = n;
  3625. dtlck->index++;
  3626. *dtlock = dtlck;
  3627. }
  3628. /*
  3629. * NAME: dtModify
  3630. *
  3631. * FUNCTION: Modify the inode number part of a directory entry
  3632. *
  3633. * PARAMETERS:
  3634. * tid - Transaction id
  3635. * ip - Inode of parent directory
  3636. * key - Name of entry to be modified
  3637. * orig_ino - Original inode number expected in entry
  3638. * new_ino - New inode number to put into entry
  3639. * flag - JFS_RENAME
  3640. *
  3641. * RETURNS:
  3642. * -ESTALE - If entry found does not match orig_ino passed in
  3643. * -ENOENT - If no entry can be found to match key
  3644. * 0 - If successfully modified entry
  3645. */
  3646. int dtModify(tid_t tid, struct inode *ip,
  3647. struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
  3648. {
  3649. int rc;
  3650. s64 bn;
  3651. struct metapage *mp;
  3652. dtpage_t *p;
  3653. int index;
  3654. struct btstack btstack;
  3655. struct tlock *tlck;
  3656. struct dt_lock *dtlck;
  3657. struct lv *lv;
  3658. s8 *stbl;
  3659. int entry_si; /* entry slot index */
  3660. struct ldtentry *entry;
  3661. /*
  3662. * search for the entry to modify:
  3663. *
  3664. * dtSearch() returns (leaf page pinned, index at which to modify).
  3665. */
  3666. if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
  3667. return rc;
  3668. /* retrieve search result */
  3669. DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  3670. BT_MARK_DIRTY(mp, ip);
  3671. /*
  3672. * acquire a transaction lock on the leaf page of named entry
  3673. */
  3674. tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
  3675. dtlck = (struct dt_lock *) & tlck->lock;
  3676. /* get slot index of the entry */
  3677. stbl = DT_GETSTBL(p);
  3678. entry_si = stbl[index];
  3679. /* linelock entry */
  3680. ASSERT(dtlck->index == 0);
  3681. lv = & dtlck->lv[0];
  3682. lv->offset = entry_si;
  3683. lv->length = 1;
  3684. dtlck->index++;
  3685. /* get the head/only segment */
  3686. entry = (struct ldtentry *) & p->slot[entry_si];
  3687. /* substitute the inode number of the entry */
  3688. entry->inumber = cpu_to_le32(new_ino);
  3689. /* unpin the leaf page */
  3690. DT_PUTPAGE(mp);
  3691. return 0;
  3692. }