jfs_dtree.c 100 KB

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