jfs_dtree.c 93 KB

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