jfs_dtree.c 101 KB

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