jfs_dtree.c 100 KB

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