jfs_xtree.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2005
  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_xtree.c: extent allocation descriptor B+-tree manager
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/module.h>
  23. #include <linux/quotaops.h>
  24. #include <linux/seq_file.h>
  25. #include "jfs_incore.h"
  26. #include "jfs_filsys.h"
  27. #include "jfs_metapage.h"
  28. #include "jfs_dmap.h"
  29. #include "jfs_dinode.h"
  30. #include "jfs_superblock.h"
  31. #include "jfs_debug.h"
  32. /*
  33. * xtree local flag
  34. */
  35. #define XT_INSERT 0x00000001
  36. /*
  37. * xtree key/entry comparison: extent offset
  38. *
  39. * return:
  40. * -1: k < start of extent
  41. * 0: start_of_extent <= k <= end_of_extent
  42. * 1: k > end_of_extent
  43. */
  44. #define XT_CMP(CMP, K, X, OFFSET64)\
  45. {\
  46. OFFSET64 = offsetXAD(X);\
  47. (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
  48. ((K) < OFFSET64) ? -1 : 0;\
  49. }
  50. /* write a xad entry */
  51. #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
  52. {\
  53. (XAD)->flag = (FLAG);\
  54. XADoffset((XAD), (OFF));\
  55. XADlength((XAD), (LEN));\
  56. XADaddress((XAD), (ADDR));\
  57. }
  58. #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
  59. /* get page buffer for specified block address */
  60. /* ToDo: Replace this ugly macro with a function */
  61. #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
  62. do { \
  63. BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot); \
  64. if (!(RC)) { \
  65. if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) || \
  66. (le16_to_cpu((P)->header.nextindex) > \
  67. le16_to_cpu((P)->header.maxentry)) || \
  68. (le16_to_cpu((P)->header.maxentry) > \
  69. (((BN) == 0) ? XTROOTMAXSLOT : PSIZE >> L2XTSLOTSIZE))) { \
  70. jfs_error((IP)->i_sb, \
  71. "XT_GETPAGE: xtree page corrupt\n"); \
  72. BT_PUTPAGE(MP); \
  73. MP = NULL; \
  74. RC = -EIO; \
  75. } \
  76. } \
  77. } while (0)
  78. /* for consistency */
  79. #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
  80. #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
  81. BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
  82. /* xtree entry parameter descriptor */
  83. struct xtsplit {
  84. struct metapage *mp;
  85. s16 index;
  86. u8 flag;
  87. s64 off;
  88. s64 addr;
  89. int len;
  90. struct pxdlist *pxdlist;
  91. };
  92. /*
  93. * statistics
  94. */
  95. #ifdef CONFIG_JFS_STATISTICS
  96. static struct {
  97. uint search;
  98. uint fastSearch;
  99. uint split;
  100. } xtStat;
  101. #endif
  102. /*
  103. * forward references
  104. */
  105. static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
  106. struct btstack * btstack, int flag);
  107. static int xtSplitUp(tid_t tid,
  108. struct inode *ip,
  109. struct xtsplit * split, struct btstack * btstack);
  110. static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
  111. struct metapage ** rmpp, s64 * rbnp);
  112. static int xtSplitRoot(tid_t tid, struct inode *ip,
  113. struct xtsplit * split, struct metapage ** rmpp);
  114. #ifdef _STILL_TO_PORT
  115. static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
  116. xtpage_t * fp, struct btstack * btstack);
  117. static int xtSearchNode(struct inode *ip,
  118. xad_t * xad,
  119. int *cmpp, struct btstack * btstack, int flag);
  120. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
  121. #endif /* _STILL_TO_PORT */
  122. /*
  123. * xtLookup()
  124. *
  125. * function: map a single page into a physical extent;
  126. */
  127. int xtLookup(struct inode *ip, s64 lstart,
  128. s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
  129. {
  130. int rc = 0;
  131. struct btstack btstack;
  132. int cmp;
  133. s64 bn;
  134. struct metapage *mp;
  135. xtpage_t *p;
  136. int index;
  137. xad_t *xad;
  138. s64 next, size, xoff, xend;
  139. int xlen;
  140. s64 xaddr;
  141. *paddr = 0;
  142. *plen = llen;
  143. if (!no_check) {
  144. /* is lookup offset beyond eof ? */
  145. size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  146. JFS_SBI(ip->i_sb)->l2bsize;
  147. if (lstart >= size)
  148. return 0;
  149. }
  150. /*
  151. * search for the xad entry covering the logical extent
  152. */
  153. //search:
  154. if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
  155. jfs_err("xtLookup: xtSearch returned %d", rc);
  156. return rc;
  157. }
  158. /*
  159. * compute the physical extent covering logical extent
  160. *
  161. * N.B. search may have failed (e.g., hole in sparse file),
  162. * and returned the index of the next entry.
  163. */
  164. /* retrieve search result */
  165. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  166. /* is xad found covering start of logical extent ?
  167. * lstart is a page start address,
  168. * i.e., lstart cannot start in a hole;
  169. */
  170. if (cmp) {
  171. if (next)
  172. *plen = min(next - lstart, llen);
  173. goto out;
  174. }
  175. /*
  176. * lxd covered by xad
  177. */
  178. xad = &p->xad[index];
  179. xoff = offsetXAD(xad);
  180. xlen = lengthXAD(xad);
  181. xend = xoff + xlen;
  182. xaddr = addressXAD(xad);
  183. /* initialize new pxd */
  184. *pflag = xad->flag;
  185. *paddr = xaddr + (lstart - xoff);
  186. /* a page must be fully covered by an xad */
  187. *plen = min(xend - lstart, llen);
  188. out:
  189. XT_PUTPAGE(mp);
  190. return rc;
  191. }
  192. /*
  193. * xtSearch()
  194. *
  195. * function: search for the xad entry covering specified offset.
  196. *
  197. * parameters:
  198. * ip - file object;
  199. * xoff - extent offset;
  200. * nextp - address of next extent (if any) for search miss
  201. * cmpp - comparison result:
  202. * btstack - traverse stack;
  203. * flag - search process flag (XT_INSERT);
  204. *
  205. * returns:
  206. * btstack contains (bn, index) of search path traversed to the entry.
  207. * *cmpp is set to result of comparison with the entry returned.
  208. * the page containing the entry is pinned at exit.
  209. */
  210. static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
  211. int *cmpp, struct btstack * btstack, int flag)
  212. {
  213. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  214. int rc = 0;
  215. int cmp = 1; /* init for empty page */
  216. s64 bn; /* block number */
  217. struct metapage *mp; /* page buffer */
  218. xtpage_t *p; /* page */
  219. xad_t *xad;
  220. int base, index, lim, btindex;
  221. struct btframe *btsp;
  222. int nsplit = 0; /* number of pages to split */
  223. s64 t64;
  224. s64 next = 0;
  225. INCREMENT(xtStat.search);
  226. BT_CLR(btstack);
  227. btstack->nsplit = 0;
  228. /*
  229. * search down tree from root:
  230. *
  231. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  232. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  233. *
  234. * if entry with search key K is not found
  235. * internal page search find the entry with largest key Ki
  236. * less than K which point to the child page to search;
  237. * leaf page search find the entry with smallest key Kj
  238. * greater than K so that the returned index is the position of
  239. * the entry to be shifted right for insertion of new entry.
  240. * for empty tree, search key is greater than any key of the tree.
  241. *
  242. * by convention, root bn = 0.
  243. */
  244. for (bn = 0;;) {
  245. /* get/pin the page to search */
  246. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  247. if (rc)
  248. return rc;
  249. /* try sequential access heuristics with the previous
  250. * access entry in target leaf page:
  251. * once search narrowed down into the target leaf,
  252. * key must either match an entry in the leaf or
  253. * key entry does not exist in the tree;
  254. */
  255. //fastSearch:
  256. if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
  257. (p->header.flag & BT_LEAF) &&
  258. (index = jfs_ip->btindex) <
  259. le16_to_cpu(p->header.nextindex)) {
  260. xad = &p->xad[index];
  261. t64 = offsetXAD(xad);
  262. if (xoff < t64 + lengthXAD(xad)) {
  263. if (xoff >= t64) {
  264. *cmpp = 0;
  265. goto out;
  266. }
  267. /* stop sequential access heuristics */
  268. goto binarySearch;
  269. } else { /* (t64 + lengthXAD(xad)) <= xoff */
  270. /* try next sequential entry */
  271. index++;
  272. if (index <
  273. le16_to_cpu(p->header.nextindex)) {
  274. xad++;
  275. t64 = offsetXAD(xad);
  276. if (xoff < t64 + lengthXAD(xad)) {
  277. if (xoff >= t64) {
  278. *cmpp = 0;
  279. goto out;
  280. }
  281. /* miss: key falls between
  282. * previous and this entry
  283. */
  284. *cmpp = 1;
  285. next = t64;
  286. goto out;
  287. }
  288. /* (xoff >= t64 + lengthXAD(xad));
  289. * matching entry may be further out:
  290. * stop heuristic search
  291. */
  292. /* stop sequential access heuristics */
  293. goto binarySearch;
  294. }
  295. /* (index == p->header.nextindex);
  296. * miss: key entry does not exist in
  297. * the target leaf/tree
  298. */
  299. *cmpp = 1;
  300. goto out;
  301. }
  302. /*
  303. * if hit, return index of the entry found, and
  304. * if miss, where new entry with search key is
  305. * to be inserted;
  306. */
  307. out:
  308. /* compute number of pages to split */
  309. if (flag & XT_INSERT) {
  310. if (p->header.nextindex == /* little-endian */
  311. p->header.maxentry)
  312. nsplit++;
  313. else
  314. nsplit = 0;
  315. btstack->nsplit = nsplit;
  316. }
  317. /* save search result */
  318. btsp = btstack->top;
  319. btsp->bn = bn;
  320. btsp->index = index;
  321. btsp->mp = mp;
  322. /* update sequential access heuristics */
  323. jfs_ip->btindex = index;
  324. if (nextp)
  325. *nextp = next;
  326. INCREMENT(xtStat.fastSearch);
  327. return 0;
  328. }
  329. /* well, ... full search now */
  330. binarySearch:
  331. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  332. /*
  333. * binary search with search key K on the current page
  334. */
  335. for (base = XTENTRYSTART; lim; lim >>= 1) {
  336. index = base + (lim >> 1);
  337. XT_CMP(cmp, xoff, &p->xad[index], t64);
  338. if (cmp == 0) {
  339. /*
  340. * search hit
  341. */
  342. /* search hit - leaf page:
  343. * return the entry found
  344. */
  345. if (p->header.flag & BT_LEAF) {
  346. *cmpp = cmp;
  347. /* compute number of pages to split */
  348. if (flag & XT_INSERT) {
  349. if (p->header.nextindex ==
  350. p->header.maxentry)
  351. nsplit++;
  352. else
  353. nsplit = 0;
  354. btstack->nsplit = nsplit;
  355. }
  356. /* save search result */
  357. btsp = btstack->top;
  358. btsp->bn = bn;
  359. btsp->index = index;
  360. btsp->mp = mp;
  361. /* init sequential access heuristics */
  362. btindex = jfs_ip->btindex;
  363. if (index == btindex ||
  364. index == btindex + 1)
  365. jfs_ip->btorder = BT_SEQUENTIAL;
  366. else
  367. jfs_ip->btorder = BT_RANDOM;
  368. jfs_ip->btindex = index;
  369. return 0;
  370. }
  371. /* search hit - internal page:
  372. * descend/search its child page
  373. */
  374. if (index < le16_to_cpu(p->header.nextindex)-1)
  375. next = offsetXAD(&p->xad[index + 1]);
  376. goto next;
  377. }
  378. if (cmp > 0) {
  379. base = index + 1;
  380. --lim;
  381. }
  382. }
  383. /*
  384. * search miss
  385. *
  386. * base is the smallest index with key (Kj) greater than
  387. * search key (K) and may be zero or maxentry index.
  388. */
  389. if (base < le16_to_cpu(p->header.nextindex))
  390. next = offsetXAD(&p->xad[base]);
  391. /*
  392. * search miss - leaf page:
  393. *
  394. * return location of entry (base) where new entry with
  395. * search key K is to be inserted.
  396. */
  397. if (p->header.flag & BT_LEAF) {
  398. *cmpp = cmp;
  399. /* compute number of pages to split */
  400. if (flag & XT_INSERT) {
  401. if (p->header.nextindex ==
  402. p->header.maxentry)
  403. nsplit++;
  404. else
  405. nsplit = 0;
  406. btstack->nsplit = nsplit;
  407. }
  408. /* save search result */
  409. btsp = btstack->top;
  410. btsp->bn = bn;
  411. btsp->index = base;
  412. btsp->mp = mp;
  413. /* init sequential access heuristics */
  414. btindex = jfs_ip->btindex;
  415. if (base == btindex || base == btindex + 1)
  416. jfs_ip->btorder = BT_SEQUENTIAL;
  417. else
  418. jfs_ip->btorder = BT_RANDOM;
  419. jfs_ip->btindex = base;
  420. if (nextp)
  421. *nextp = next;
  422. return 0;
  423. }
  424. /*
  425. * search miss - non-leaf page:
  426. *
  427. * if base is non-zero, decrement base by one to get the parent
  428. * entry of the child page to search.
  429. */
  430. index = base ? base - 1 : base;
  431. /*
  432. * go down to child page
  433. */
  434. next:
  435. /* update number of pages to split */
  436. if (p->header.nextindex == p->header.maxentry)
  437. nsplit++;
  438. else
  439. nsplit = 0;
  440. /* push (bn, index) of the parent page/entry */
  441. if (BT_STACK_FULL(btstack)) {
  442. jfs_error(ip->i_sb, "stack overrun!\n");
  443. XT_PUTPAGE(mp);
  444. return -EIO;
  445. }
  446. BT_PUSH(btstack, bn, index);
  447. /* get the child page block number */
  448. bn = addressXAD(&p->xad[index]);
  449. /* unpin the parent page */
  450. XT_PUTPAGE(mp);
  451. }
  452. }
  453. /*
  454. * xtInsert()
  455. *
  456. * function:
  457. *
  458. * parameter:
  459. * tid - transaction id;
  460. * ip - file object;
  461. * xflag - extent flag (XAD_NOTRECORDED):
  462. * xoff - extent offset;
  463. * xlen - extent length;
  464. * xaddrp - extent address pointer (in/out):
  465. * if (*xaddrp)
  466. * caller allocated data extent at *xaddrp;
  467. * else
  468. * allocate data extent and return its xaddr;
  469. * flag -
  470. *
  471. * return:
  472. */
  473. int xtInsert(tid_t tid, /* transaction id */
  474. struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
  475. int flag)
  476. {
  477. int rc = 0;
  478. s64 xaddr, hint;
  479. struct metapage *mp; /* meta-page buffer */
  480. xtpage_t *p; /* base B+-tree index page */
  481. s64 bn;
  482. int index, nextindex;
  483. struct btstack btstack; /* traverse stack */
  484. struct xtsplit split; /* split information */
  485. xad_t *xad;
  486. int cmp;
  487. s64 next;
  488. struct tlock *tlck;
  489. struct xtlock *xtlck;
  490. jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  491. /*
  492. * search for the entry location at which to insert:
  493. *
  494. * xtFastSearch() and xtSearch() both returns (leaf page
  495. * pinned, index at which to insert).
  496. * n.b. xtSearch() may return index of maxentry of
  497. * the full page.
  498. */
  499. if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
  500. return rc;
  501. /* retrieve search result */
  502. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  503. /* This test must follow XT_GETSEARCH since mp must be valid if
  504. * we branch to out: */
  505. if ((cmp == 0) || (next && (xlen > next - xoff))) {
  506. rc = -EEXIST;
  507. goto out;
  508. }
  509. /*
  510. * allocate data extent requested
  511. *
  512. * allocation hint: last xad
  513. */
  514. if ((xaddr = *xaddrp) == 0) {
  515. if (index > XTENTRYSTART) {
  516. xad = &p->xad[index - 1];
  517. hint = addressXAD(xad) + lengthXAD(xad) - 1;
  518. } else
  519. hint = 0;
  520. if ((rc = dquot_alloc_block(ip, xlen)))
  521. goto out;
  522. if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
  523. dquot_free_block(ip, xlen);
  524. goto out;
  525. }
  526. }
  527. /*
  528. * insert entry for new extent
  529. */
  530. xflag |= XAD_NEW;
  531. /*
  532. * if the leaf page is full, split the page and
  533. * propagate up the router entry for the new page from split
  534. *
  535. * The xtSplitUp() will insert the entry and unpin the leaf page.
  536. */
  537. nextindex = le16_to_cpu(p->header.nextindex);
  538. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  539. split.mp = mp;
  540. split.index = index;
  541. split.flag = xflag;
  542. split.off = xoff;
  543. split.len = xlen;
  544. split.addr = xaddr;
  545. split.pxdlist = NULL;
  546. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  547. /* undo data extent allocation */
  548. if (*xaddrp == 0) {
  549. dbFree(ip, xaddr, (s64) xlen);
  550. dquot_free_block(ip, xlen);
  551. }
  552. return rc;
  553. }
  554. *xaddrp = xaddr;
  555. return 0;
  556. }
  557. /*
  558. * insert the new entry into the leaf page
  559. */
  560. /*
  561. * acquire a transaction lock on the leaf page;
  562. *
  563. * action: xad insertion/extension;
  564. */
  565. BT_MARK_DIRTY(mp, ip);
  566. /* if insert into middle, shift right remaining entries. */
  567. if (index < nextindex)
  568. memmove(&p->xad[index + 1], &p->xad[index],
  569. (nextindex - index) * sizeof(xad_t));
  570. /* insert the new entry: mark the entry NEW */
  571. xad = &p->xad[index];
  572. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  573. /* advance next available entry index */
  574. le16_add_cpu(&p->header.nextindex, 1);
  575. /* Don't log it if there are no links to the file */
  576. if (!test_cflag(COMMIT_Nolink, ip)) {
  577. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  578. xtlck = (struct xtlock *) & tlck->lock;
  579. xtlck->lwm.offset =
  580. (xtlck->lwm.offset) ? min(index,
  581. (int)xtlck->lwm.offset) : index;
  582. xtlck->lwm.length =
  583. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  584. }
  585. *xaddrp = xaddr;
  586. out:
  587. /* unpin the leaf page */
  588. XT_PUTPAGE(mp);
  589. return rc;
  590. }
  591. /*
  592. * xtSplitUp()
  593. *
  594. * function:
  595. * split full pages as propagating insertion up the tree
  596. *
  597. * parameter:
  598. * tid - transaction id;
  599. * ip - file object;
  600. * split - entry parameter descriptor;
  601. * btstack - traverse stack from xtSearch()
  602. *
  603. * return:
  604. */
  605. static int
  606. xtSplitUp(tid_t tid,
  607. struct inode *ip, struct xtsplit * split, struct btstack * btstack)
  608. {
  609. int rc = 0;
  610. struct metapage *smp;
  611. xtpage_t *sp; /* split page */
  612. struct metapage *rmp;
  613. s64 rbn; /* new right page block number */
  614. struct metapage *rcmp;
  615. xtpage_t *rcp; /* right child page */
  616. s64 rcbn; /* right child page block number */
  617. int skip; /* index of entry of insertion */
  618. int nextindex; /* next available entry index of p */
  619. struct btframe *parent; /* parent page entry on traverse stack */
  620. xad_t *xad;
  621. s64 xaddr;
  622. int xlen;
  623. int nsplit; /* number of pages split */
  624. struct pxdlist pxdlist;
  625. pxd_t *pxd;
  626. struct tlock *tlck;
  627. struct xtlock *xtlck;
  628. smp = split->mp;
  629. sp = XT_PAGE(ip, smp);
  630. /* is inode xtree root extension/inline EA area free ? */
  631. if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
  632. (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
  633. (JFS_IP(ip)->mode2 & INLINEEA)) {
  634. sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
  635. JFS_IP(ip)->mode2 &= ~INLINEEA;
  636. BT_MARK_DIRTY(smp, ip);
  637. /*
  638. * acquire a transaction lock on the leaf page;
  639. *
  640. * action: xad insertion/extension;
  641. */
  642. /* if insert into middle, shift right remaining entries. */
  643. skip = split->index;
  644. nextindex = le16_to_cpu(sp->header.nextindex);
  645. if (skip < nextindex)
  646. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  647. (nextindex - skip) * sizeof(xad_t));
  648. /* insert the new entry: mark the entry NEW */
  649. xad = &sp->xad[skip];
  650. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  651. split->addr);
  652. /* advance next available entry index */
  653. le16_add_cpu(&sp->header.nextindex, 1);
  654. /* Don't log it if there are no links to the file */
  655. if (!test_cflag(COMMIT_Nolink, ip)) {
  656. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  657. xtlck = (struct xtlock *) & tlck->lock;
  658. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  659. min(skip, (int)xtlck->lwm.offset) : skip;
  660. xtlck->lwm.length =
  661. le16_to_cpu(sp->header.nextindex) -
  662. xtlck->lwm.offset;
  663. }
  664. return 0;
  665. }
  666. /*
  667. * allocate new index blocks to cover index page split(s)
  668. *
  669. * allocation hint: ?
  670. */
  671. if (split->pxdlist == NULL) {
  672. nsplit = btstack->nsplit;
  673. split->pxdlist = &pxdlist;
  674. pxdlist.maxnpxd = pxdlist.npxd = 0;
  675. pxd = &pxdlist.pxd[0];
  676. xlen = JFS_SBI(ip->i_sb)->nbperpage;
  677. for (; nsplit > 0; nsplit--, pxd++) {
  678. if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
  679. == 0) {
  680. PXDaddress(pxd, xaddr);
  681. PXDlength(pxd, xlen);
  682. pxdlist.maxnpxd++;
  683. continue;
  684. }
  685. /* undo allocation */
  686. XT_PUTPAGE(smp);
  687. return rc;
  688. }
  689. }
  690. /*
  691. * Split leaf page <sp> into <sp> and a new right page <rp>.
  692. *
  693. * The split routines insert the new entry into the leaf page,
  694. * and acquire txLock as appropriate.
  695. * return <rp> pinned and its block number <rpbn>.
  696. */
  697. rc = (sp->header.flag & BT_ROOT) ?
  698. xtSplitRoot(tid, ip, split, &rmp) :
  699. xtSplitPage(tid, ip, split, &rmp, &rbn);
  700. XT_PUTPAGE(smp);
  701. if (rc)
  702. return -EIO;
  703. /*
  704. * propagate up the router entry for the leaf page just split
  705. *
  706. * insert a router entry for the new page into the parent page,
  707. * propagate the insert/split up the tree by walking back the stack
  708. * of (bn of parent page, index of child page entry in parent page)
  709. * that were traversed during the search for the page that split.
  710. *
  711. * the propagation of insert/split up the tree stops if the root
  712. * splits or the page inserted into doesn't have to split to hold
  713. * the new entry.
  714. *
  715. * the parent entry for the split page remains the same, and
  716. * a new entry is inserted at its right with the first key and
  717. * block number of the new right page.
  718. *
  719. * There are a maximum of 3 pages pinned at any time:
  720. * right child, left parent and right parent (when the parent splits)
  721. * to keep the child page pinned while working on the parent.
  722. * make sure that all pins are released at exit.
  723. */
  724. while ((parent = BT_POP(btstack)) != NULL) {
  725. /* parent page specified by stack frame <parent> */
  726. /* keep current child pages <rcp> pinned */
  727. rcmp = rmp;
  728. rcbn = rbn;
  729. rcp = XT_PAGE(ip, rcmp);
  730. /*
  731. * insert router entry in parent for new right child page <rp>
  732. */
  733. /* get/pin the parent page <sp> */
  734. XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
  735. if (rc) {
  736. XT_PUTPAGE(rcmp);
  737. return rc;
  738. }
  739. /*
  740. * The new key entry goes ONE AFTER the index of parent entry,
  741. * because the split was to the right.
  742. */
  743. skip = parent->index + 1;
  744. /*
  745. * split or shift right remaining entries of the parent page
  746. */
  747. nextindex = le16_to_cpu(sp->header.nextindex);
  748. /*
  749. * parent page is full - split the parent page
  750. */
  751. if (nextindex == le16_to_cpu(sp->header.maxentry)) {
  752. /* init for parent page split */
  753. split->mp = smp;
  754. split->index = skip; /* index at insert */
  755. split->flag = XAD_NEW;
  756. split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
  757. split->len = JFS_SBI(ip->i_sb)->nbperpage;
  758. split->addr = rcbn;
  759. /* unpin previous right child page */
  760. XT_PUTPAGE(rcmp);
  761. /* The split routines insert the new entry,
  762. * and acquire txLock as appropriate.
  763. * return <rp> pinned and its block number <rpbn>.
  764. */
  765. rc = (sp->header.flag & BT_ROOT) ?
  766. xtSplitRoot(tid, ip, split, &rmp) :
  767. xtSplitPage(tid, ip, split, &rmp, &rbn);
  768. if (rc) {
  769. XT_PUTPAGE(smp);
  770. return rc;
  771. }
  772. XT_PUTPAGE(smp);
  773. /* keep new child page <rp> pinned */
  774. }
  775. /*
  776. * parent page is not full - insert in parent page
  777. */
  778. else {
  779. /*
  780. * insert router entry in parent for the right child
  781. * page from the first entry of the right child page:
  782. */
  783. /*
  784. * acquire a transaction lock on the parent page;
  785. *
  786. * action: router xad insertion;
  787. */
  788. BT_MARK_DIRTY(smp, ip);
  789. /*
  790. * if insert into middle, shift right remaining entries
  791. */
  792. if (skip < nextindex)
  793. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  794. (nextindex -
  795. skip) << L2XTSLOTSIZE);
  796. /* insert the router entry */
  797. xad = &sp->xad[skip];
  798. XT_PUTENTRY(xad, XAD_NEW,
  799. offsetXAD(&rcp->xad[XTENTRYSTART]),
  800. JFS_SBI(ip->i_sb)->nbperpage, rcbn);
  801. /* advance next available entry index. */
  802. le16_add_cpu(&sp->header.nextindex, 1);
  803. /* Don't log it if there are no links to the file */
  804. if (!test_cflag(COMMIT_Nolink, ip)) {
  805. tlck = txLock(tid, ip, smp,
  806. tlckXTREE | tlckGROW);
  807. xtlck = (struct xtlock *) & tlck->lock;
  808. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  809. min(skip, (int)xtlck->lwm.offset) : skip;
  810. xtlck->lwm.length =
  811. le16_to_cpu(sp->header.nextindex) -
  812. xtlck->lwm.offset;
  813. }
  814. /* unpin parent page */
  815. XT_PUTPAGE(smp);
  816. /* exit propagate up */
  817. break;
  818. }
  819. }
  820. /* unpin current right page */
  821. XT_PUTPAGE(rmp);
  822. return 0;
  823. }
  824. /*
  825. * xtSplitPage()
  826. *
  827. * function:
  828. * split a full non-root page into
  829. * original/split/left page and new right page
  830. * i.e., the original/split page remains as left page.
  831. *
  832. * parameter:
  833. * int tid,
  834. * struct inode *ip,
  835. * struct xtsplit *split,
  836. * struct metapage **rmpp,
  837. * u64 *rbnp,
  838. *
  839. * return:
  840. * Pointer to page in which to insert or NULL on error.
  841. */
  842. static int
  843. xtSplitPage(tid_t tid, struct inode *ip,
  844. struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
  845. {
  846. int rc = 0;
  847. struct metapage *smp;
  848. xtpage_t *sp;
  849. struct metapage *rmp;
  850. xtpage_t *rp; /* new right page allocated */
  851. s64 rbn; /* new right page block number */
  852. struct metapage *mp;
  853. xtpage_t *p;
  854. s64 nextbn;
  855. int skip, maxentry, middle, righthalf, n;
  856. xad_t *xad;
  857. struct pxdlist *pxdlist;
  858. pxd_t *pxd;
  859. struct tlock *tlck;
  860. struct xtlock *sxtlck = NULL, *rxtlck = NULL;
  861. int quota_allocation = 0;
  862. smp = split->mp;
  863. sp = XT_PAGE(ip, smp);
  864. INCREMENT(xtStat.split);
  865. pxdlist = split->pxdlist;
  866. pxd = &pxdlist->pxd[pxdlist->npxd];
  867. pxdlist->npxd++;
  868. rbn = addressPXD(pxd);
  869. /* Allocate blocks to quota. */
  870. rc = dquot_alloc_block(ip, lengthPXD(pxd));
  871. if (rc)
  872. goto clean_up;
  873. quota_allocation += lengthPXD(pxd);
  874. /*
  875. * allocate the new right page for the split
  876. */
  877. rmp = get_metapage(ip, rbn, PSIZE, 1);
  878. if (rmp == NULL) {
  879. rc = -EIO;
  880. goto clean_up;
  881. }
  882. jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
  883. BT_MARK_DIRTY(rmp, ip);
  884. /*
  885. * action: new page;
  886. */
  887. rp = (xtpage_t *) rmp->data;
  888. rp->header.self = *pxd;
  889. rp->header.flag = sp->header.flag & BT_TYPE;
  890. rp->header.maxentry = sp->header.maxentry; /* little-endian */
  891. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  892. BT_MARK_DIRTY(smp, ip);
  893. /* Don't log it if there are no links to the file */
  894. if (!test_cflag(COMMIT_Nolink, ip)) {
  895. /*
  896. * acquire a transaction lock on the new right page;
  897. */
  898. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  899. rxtlck = (struct xtlock *) & tlck->lock;
  900. rxtlck->lwm.offset = XTENTRYSTART;
  901. /*
  902. * acquire a transaction lock on the split page
  903. */
  904. tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
  905. sxtlck = (struct xtlock *) & tlck->lock;
  906. }
  907. /*
  908. * initialize/update sibling pointers of <sp> and <rp>
  909. */
  910. nextbn = le64_to_cpu(sp->header.next);
  911. rp->header.next = cpu_to_le64(nextbn);
  912. rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
  913. sp->header.next = cpu_to_le64(rbn);
  914. skip = split->index;
  915. /*
  916. * sequential append at tail (after last entry of last page)
  917. *
  918. * if splitting the last page on a level because of appending
  919. * a entry to it (skip is maxentry), it's likely that the access is
  920. * sequential. adding an empty page on the side of the level is less
  921. * work and can push the fill factor much higher than normal.
  922. * if we're wrong it's no big deal - we will do the split the right
  923. * way next time.
  924. * (it may look like it's equally easy to do a similar hack for
  925. * reverse sorted data, that is, split the tree left, but it's not.
  926. * Be my guest.)
  927. */
  928. if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
  929. /*
  930. * acquire a transaction lock on the new/right page;
  931. *
  932. * action: xad insertion;
  933. */
  934. /* insert entry at the first entry of the new right page */
  935. xad = &rp->xad[XTENTRYSTART];
  936. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  937. split->addr);
  938. rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  939. if (!test_cflag(COMMIT_Nolink, ip)) {
  940. /* rxtlck->lwm.offset = XTENTRYSTART; */
  941. rxtlck->lwm.length = 1;
  942. }
  943. *rmpp = rmp;
  944. *rbnp = rbn;
  945. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  946. return 0;
  947. }
  948. /*
  949. * non-sequential insert (at possibly middle page)
  950. */
  951. /*
  952. * update previous pointer of old next/right page of <sp>
  953. */
  954. if (nextbn != 0) {
  955. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  956. if (rc) {
  957. XT_PUTPAGE(rmp);
  958. goto clean_up;
  959. }
  960. BT_MARK_DIRTY(mp, ip);
  961. /*
  962. * acquire a transaction lock on the next page;
  963. *
  964. * action:sibling pointer update;
  965. */
  966. if (!test_cflag(COMMIT_Nolink, ip))
  967. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  968. p->header.prev = cpu_to_le64(rbn);
  969. /* sibling page may have been updated previously, or
  970. * it may be updated later;
  971. */
  972. XT_PUTPAGE(mp);
  973. }
  974. /*
  975. * split the data between the split and new/right pages
  976. */
  977. maxentry = le16_to_cpu(sp->header.maxentry);
  978. middle = maxentry >> 1;
  979. righthalf = maxentry - middle;
  980. /*
  981. * skip index in old split/left page - insert into left page:
  982. */
  983. if (skip <= middle) {
  984. /* move right half of split page to the new right page */
  985. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  986. righthalf << L2XTSLOTSIZE);
  987. /* shift right tail of left half to make room for new entry */
  988. if (skip < middle)
  989. memmove(&sp->xad[skip + 1], &sp->xad[skip],
  990. (middle - skip) << L2XTSLOTSIZE);
  991. /* insert new entry */
  992. xad = &sp->xad[skip];
  993. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  994. split->addr);
  995. /* update page header */
  996. sp->header.nextindex = cpu_to_le16(middle + 1);
  997. if (!test_cflag(COMMIT_Nolink, ip)) {
  998. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  999. min(skip, (int)sxtlck->lwm.offset) : skip;
  1000. }
  1001. rp->header.nextindex =
  1002. cpu_to_le16(XTENTRYSTART + righthalf);
  1003. }
  1004. /*
  1005. * skip index in new right page - insert into right page:
  1006. */
  1007. else {
  1008. /* move left head of right half to right page */
  1009. n = skip - middle;
  1010. memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
  1011. n << L2XTSLOTSIZE);
  1012. /* insert new entry */
  1013. n += XTENTRYSTART;
  1014. xad = &rp->xad[n];
  1015. XT_PUTENTRY(xad, split->flag, split->off, split->len,
  1016. split->addr);
  1017. /* move right tail of right half to right page */
  1018. if (skip < maxentry)
  1019. memmove(&rp->xad[n + 1], &sp->xad[skip],
  1020. (maxentry - skip) << L2XTSLOTSIZE);
  1021. /* update page header */
  1022. sp->header.nextindex = cpu_to_le16(middle);
  1023. if (!test_cflag(COMMIT_Nolink, ip)) {
  1024. sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
  1025. min(middle, (int)sxtlck->lwm.offset) : middle;
  1026. }
  1027. rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
  1028. righthalf + 1);
  1029. }
  1030. if (!test_cflag(COMMIT_Nolink, ip)) {
  1031. sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
  1032. sxtlck->lwm.offset;
  1033. /* rxtlck->lwm.offset = XTENTRYSTART; */
  1034. rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1035. XTENTRYSTART;
  1036. }
  1037. *rmpp = rmp;
  1038. *rbnp = rbn;
  1039. jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
  1040. return rc;
  1041. clean_up:
  1042. /* Rollback quota allocation. */
  1043. if (quota_allocation)
  1044. dquot_free_block(ip, quota_allocation);
  1045. return (rc);
  1046. }
  1047. /*
  1048. * xtSplitRoot()
  1049. *
  1050. * function:
  1051. * split the full root page into original/root/split page and new
  1052. * right page
  1053. * i.e., root remains fixed in tree anchor (inode) and the root is
  1054. * copied to a single new right child page since root page <<
  1055. * non-root page, and the split root page contains a single entry
  1056. * for the new right child page.
  1057. *
  1058. * parameter:
  1059. * int tid,
  1060. * struct inode *ip,
  1061. * struct xtsplit *split,
  1062. * struct metapage **rmpp)
  1063. *
  1064. * return:
  1065. * Pointer to page in which to insert or NULL on error.
  1066. */
  1067. static int
  1068. xtSplitRoot(tid_t tid,
  1069. struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
  1070. {
  1071. xtpage_t *sp;
  1072. struct metapage *rmp;
  1073. xtpage_t *rp;
  1074. s64 rbn;
  1075. int skip, nextindex;
  1076. xad_t *xad;
  1077. pxd_t *pxd;
  1078. struct pxdlist *pxdlist;
  1079. struct tlock *tlck;
  1080. struct xtlock *xtlck;
  1081. int rc;
  1082. sp = &JFS_IP(ip)->i_xtroot;
  1083. INCREMENT(xtStat.split);
  1084. /*
  1085. * allocate a single (right) child page
  1086. */
  1087. pxdlist = split->pxdlist;
  1088. pxd = &pxdlist->pxd[pxdlist->npxd];
  1089. pxdlist->npxd++;
  1090. rbn = addressPXD(pxd);
  1091. rmp = get_metapage(ip, rbn, PSIZE, 1);
  1092. if (rmp == NULL)
  1093. return -EIO;
  1094. /* Allocate blocks to quota. */
  1095. rc = dquot_alloc_block(ip, lengthPXD(pxd));
  1096. if (rc) {
  1097. release_metapage(rmp);
  1098. return rc;
  1099. }
  1100. jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
  1101. /*
  1102. * acquire a transaction lock on the new right page;
  1103. *
  1104. * action: new page;
  1105. */
  1106. BT_MARK_DIRTY(rmp, ip);
  1107. rp = (xtpage_t *) rmp->data;
  1108. rp->header.flag =
  1109. (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
  1110. rp->header.self = *pxd;
  1111. rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  1112. rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
  1113. /* initialize sibling pointers */
  1114. rp->header.next = 0;
  1115. rp->header.prev = 0;
  1116. /*
  1117. * copy the in-line root page into new right page extent
  1118. */
  1119. nextindex = le16_to_cpu(sp->header.maxentry);
  1120. memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
  1121. (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
  1122. /*
  1123. * insert the new entry into the new right/child page
  1124. * (skip index in the new right page will not change)
  1125. */
  1126. skip = split->index;
  1127. /* if insert into middle, shift right remaining entries */
  1128. if (skip != nextindex)
  1129. memmove(&rp->xad[skip + 1], &rp->xad[skip],
  1130. (nextindex - skip) * sizeof(xad_t));
  1131. xad = &rp->xad[skip];
  1132. XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
  1133. /* update page header */
  1134. rp->header.nextindex = cpu_to_le16(nextindex + 1);
  1135. if (!test_cflag(COMMIT_Nolink, ip)) {
  1136. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
  1137. xtlck = (struct xtlock *) & tlck->lock;
  1138. xtlck->lwm.offset = XTENTRYSTART;
  1139. xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
  1140. XTENTRYSTART;
  1141. }
  1142. /*
  1143. * reset the root
  1144. *
  1145. * init root with the single entry for the new right page
  1146. * set the 1st entry offset to 0, which force the left-most key
  1147. * at any level of the tree to be less than any search key.
  1148. */
  1149. /*
  1150. * acquire a transaction lock on the root page (in-memory inode);
  1151. *
  1152. * action: root split;
  1153. */
  1154. BT_MARK_DIRTY(split->mp, ip);
  1155. xad = &sp->xad[XTENTRYSTART];
  1156. XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
  1157. /* update page header of root */
  1158. sp->header.flag &= ~BT_LEAF;
  1159. sp->header.flag |= BT_INTERNAL;
  1160. sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
  1161. if (!test_cflag(COMMIT_Nolink, ip)) {
  1162. tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
  1163. xtlck = (struct xtlock *) & tlck->lock;
  1164. xtlck->lwm.offset = XTENTRYSTART;
  1165. xtlck->lwm.length = 1;
  1166. }
  1167. *rmpp = rmp;
  1168. jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
  1169. return 0;
  1170. }
  1171. /*
  1172. * xtExtend()
  1173. *
  1174. * function: extend in-place;
  1175. *
  1176. * note: existing extent may or may not have been committed.
  1177. * caller is responsible for pager buffer cache update, and
  1178. * working block allocation map update;
  1179. * update pmap: alloc whole extended extent;
  1180. */
  1181. int xtExtend(tid_t tid, /* transaction id */
  1182. struct inode *ip, s64 xoff, /* delta extent offset */
  1183. s32 xlen, /* delta extent length */
  1184. int flag)
  1185. {
  1186. int rc = 0;
  1187. int cmp;
  1188. struct metapage *mp; /* meta-page buffer */
  1189. xtpage_t *p; /* base B+-tree index page */
  1190. s64 bn;
  1191. int index, nextindex, len;
  1192. struct btstack btstack; /* traverse stack */
  1193. struct xtsplit split; /* split information */
  1194. xad_t *xad;
  1195. s64 xaddr;
  1196. struct tlock *tlck;
  1197. struct xtlock *xtlck = NULL;
  1198. jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
  1199. /* there must exist extent to be extended */
  1200. if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
  1201. return rc;
  1202. /* retrieve search result */
  1203. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1204. if (cmp != 0) {
  1205. XT_PUTPAGE(mp);
  1206. jfs_error(ip->i_sb, "xtSearch did not find extent\n");
  1207. return -EIO;
  1208. }
  1209. /* extension must be contiguous */
  1210. xad = &p->xad[index];
  1211. if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
  1212. XT_PUTPAGE(mp);
  1213. jfs_error(ip->i_sb, "extension is not contiguous\n");
  1214. return -EIO;
  1215. }
  1216. /*
  1217. * acquire a transaction lock on the leaf page;
  1218. *
  1219. * action: xad insertion/extension;
  1220. */
  1221. BT_MARK_DIRTY(mp, ip);
  1222. if (!test_cflag(COMMIT_Nolink, ip)) {
  1223. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1224. xtlck = (struct xtlock *) & tlck->lock;
  1225. }
  1226. /* extend will overflow extent ? */
  1227. xlen = lengthXAD(xad) + xlen;
  1228. if ((len = xlen - MAXXLEN) <= 0)
  1229. goto extendOld;
  1230. /*
  1231. * extent overflow: insert entry for new extent
  1232. */
  1233. //insertNew:
  1234. xoff = offsetXAD(xad) + MAXXLEN;
  1235. xaddr = addressXAD(xad) + MAXXLEN;
  1236. nextindex = le16_to_cpu(p->header.nextindex);
  1237. /*
  1238. * if the leaf page is full, insert the new entry and
  1239. * propagate up the router entry for the new page from split
  1240. *
  1241. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1242. */
  1243. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1244. /* xtSpliUp() unpins leaf pages */
  1245. split.mp = mp;
  1246. split.index = index + 1;
  1247. split.flag = XAD_NEW;
  1248. split.off = xoff; /* split offset */
  1249. split.len = len;
  1250. split.addr = xaddr;
  1251. split.pxdlist = NULL;
  1252. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1253. return rc;
  1254. /* get back old page */
  1255. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1256. if (rc)
  1257. return rc;
  1258. /*
  1259. * if leaf root has been split, original root has been
  1260. * copied to new child page, i.e., original entry now
  1261. * resides on the new child page;
  1262. */
  1263. if (p->header.flag & BT_INTERNAL) {
  1264. ASSERT(p->header.nextindex ==
  1265. cpu_to_le16(XTENTRYSTART + 1));
  1266. xad = &p->xad[XTENTRYSTART];
  1267. bn = addressXAD(xad);
  1268. XT_PUTPAGE(mp);
  1269. /* get new child page */
  1270. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1271. if (rc)
  1272. return rc;
  1273. BT_MARK_DIRTY(mp, ip);
  1274. if (!test_cflag(COMMIT_Nolink, ip)) {
  1275. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1276. xtlck = (struct xtlock *) & tlck->lock;
  1277. }
  1278. }
  1279. }
  1280. /*
  1281. * insert the new entry into the leaf page
  1282. */
  1283. else {
  1284. /* insert the new entry: mark the entry NEW */
  1285. xad = &p->xad[index + 1];
  1286. XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
  1287. /* advance next available entry index */
  1288. le16_add_cpu(&p->header.nextindex, 1);
  1289. }
  1290. /* get back old entry */
  1291. xad = &p->xad[index];
  1292. xlen = MAXXLEN;
  1293. /*
  1294. * extend old extent
  1295. */
  1296. extendOld:
  1297. XADlength(xad, xlen);
  1298. if (!(xad->flag & XAD_NEW))
  1299. xad->flag |= XAD_EXTENDED;
  1300. if (!test_cflag(COMMIT_Nolink, ip)) {
  1301. xtlck->lwm.offset =
  1302. (xtlck->lwm.offset) ? min(index,
  1303. (int)xtlck->lwm.offset) : index;
  1304. xtlck->lwm.length =
  1305. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  1306. }
  1307. /* unpin the leaf page */
  1308. XT_PUTPAGE(mp);
  1309. return rc;
  1310. }
  1311. #ifdef _NOTYET
  1312. /*
  1313. * xtTailgate()
  1314. *
  1315. * function: split existing 'tail' extent
  1316. * (split offset >= start offset of tail extent), and
  1317. * relocate and extend the split tail half;
  1318. *
  1319. * note: existing extent may or may not have been committed.
  1320. * caller is responsible for pager buffer cache update, and
  1321. * working block allocation map update;
  1322. * update pmap: free old split tail extent, alloc new extent;
  1323. */
  1324. int xtTailgate(tid_t tid, /* transaction id */
  1325. struct inode *ip, s64 xoff, /* split/new extent offset */
  1326. s32 xlen, /* new extent length */
  1327. s64 xaddr, /* new extent address */
  1328. int flag)
  1329. {
  1330. int rc = 0;
  1331. int cmp;
  1332. struct metapage *mp; /* meta-page buffer */
  1333. xtpage_t *p; /* base B+-tree index page */
  1334. s64 bn;
  1335. int index, nextindex, llen, rlen;
  1336. struct btstack btstack; /* traverse stack */
  1337. struct xtsplit split; /* split information */
  1338. xad_t *xad;
  1339. struct tlock *tlck;
  1340. struct xtlock *xtlck = 0;
  1341. struct tlock *mtlck;
  1342. struct maplock *pxdlock;
  1343. /*
  1344. printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
  1345. (ulong)xoff, xlen, (ulong)xaddr);
  1346. */
  1347. /* there must exist extent to be tailgated */
  1348. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
  1349. return rc;
  1350. /* retrieve search result */
  1351. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1352. if (cmp != 0) {
  1353. XT_PUTPAGE(mp);
  1354. jfs_error(ip->i_sb, "couldn't find extent\n");
  1355. return -EIO;
  1356. }
  1357. /* entry found must be last entry */
  1358. nextindex = le16_to_cpu(p->header.nextindex);
  1359. if (index != nextindex - 1) {
  1360. XT_PUTPAGE(mp);
  1361. jfs_error(ip->i_sb, "the entry found is not the last entry\n");
  1362. return -EIO;
  1363. }
  1364. BT_MARK_DIRTY(mp, ip);
  1365. /*
  1366. * acquire tlock of the leaf page containing original entry
  1367. */
  1368. if (!test_cflag(COMMIT_Nolink, ip)) {
  1369. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1370. xtlck = (struct xtlock *) & tlck->lock;
  1371. }
  1372. /* completely replace extent ? */
  1373. xad = &p->xad[index];
  1374. /*
  1375. printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
  1376. (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
  1377. */
  1378. if ((llen = xoff - offsetXAD(xad)) == 0)
  1379. goto updateOld;
  1380. /*
  1381. * partially replace extent: insert entry for new extent
  1382. */
  1383. //insertNew:
  1384. /*
  1385. * if the leaf page is full, insert the new entry and
  1386. * propagate up the router entry for the new page from split
  1387. *
  1388. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1389. */
  1390. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1391. /* xtSpliUp() unpins leaf pages */
  1392. split.mp = mp;
  1393. split.index = index + 1;
  1394. split.flag = XAD_NEW;
  1395. split.off = xoff; /* split offset */
  1396. split.len = xlen;
  1397. split.addr = xaddr;
  1398. split.pxdlist = NULL;
  1399. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1400. return rc;
  1401. /* get back old page */
  1402. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1403. if (rc)
  1404. return rc;
  1405. /*
  1406. * if leaf root has been split, original root has been
  1407. * copied to new child page, i.e., original entry now
  1408. * resides on the new child page;
  1409. */
  1410. if (p->header.flag & BT_INTERNAL) {
  1411. ASSERT(p->header.nextindex ==
  1412. cpu_to_le16(XTENTRYSTART + 1));
  1413. xad = &p->xad[XTENTRYSTART];
  1414. bn = addressXAD(xad);
  1415. XT_PUTPAGE(mp);
  1416. /* get new child page */
  1417. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1418. if (rc)
  1419. return rc;
  1420. BT_MARK_DIRTY(mp, ip);
  1421. if (!test_cflag(COMMIT_Nolink, ip)) {
  1422. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1423. xtlck = (struct xtlock *) & tlck->lock;
  1424. }
  1425. }
  1426. }
  1427. /*
  1428. * insert the new entry into the leaf page
  1429. */
  1430. else {
  1431. /* insert the new entry: mark the entry NEW */
  1432. xad = &p->xad[index + 1];
  1433. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1434. /* advance next available entry index */
  1435. le16_add_cpu(&p->header.nextindex, 1);
  1436. }
  1437. /* get back old XAD */
  1438. xad = &p->xad[index];
  1439. /*
  1440. * truncate/relocate old extent at split offset
  1441. */
  1442. updateOld:
  1443. /* update dmap for old/committed/truncated extent */
  1444. rlen = lengthXAD(xad) - llen;
  1445. if (!(xad->flag & XAD_NEW)) {
  1446. /* free from PWMAP at commit */
  1447. if (!test_cflag(COMMIT_Nolink, ip)) {
  1448. mtlck = txMaplock(tid, ip, tlckMAP);
  1449. pxdlock = (struct maplock *) & mtlck->lock;
  1450. pxdlock->flag = mlckFREEPXD;
  1451. PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
  1452. PXDlength(&pxdlock->pxd, rlen);
  1453. pxdlock->index = 1;
  1454. }
  1455. } else
  1456. /* free from WMAP */
  1457. dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
  1458. if (llen)
  1459. /* truncate */
  1460. XADlength(xad, llen);
  1461. else
  1462. /* replace */
  1463. XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
  1464. if (!test_cflag(COMMIT_Nolink, ip)) {
  1465. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1466. min(index, (int)xtlck->lwm.offset) : index;
  1467. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  1468. xtlck->lwm.offset;
  1469. }
  1470. /* unpin the leaf page */
  1471. XT_PUTPAGE(mp);
  1472. return rc;
  1473. }
  1474. #endif /* _NOTYET */
  1475. /*
  1476. * xtUpdate()
  1477. *
  1478. * function: update XAD;
  1479. *
  1480. * update extent for allocated_but_not_recorded or
  1481. * compressed extent;
  1482. *
  1483. * parameter:
  1484. * nxad - new XAD;
  1485. * logical extent of the specified XAD must be completely
  1486. * contained by an existing XAD;
  1487. */
  1488. int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
  1489. { /* new XAD */
  1490. int rc = 0;
  1491. int cmp;
  1492. struct metapage *mp; /* meta-page buffer */
  1493. xtpage_t *p; /* base B+-tree index page */
  1494. s64 bn;
  1495. int index0, index, newindex, nextindex;
  1496. struct btstack btstack; /* traverse stack */
  1497. struct xtsplit split; /* split information */
  1498. xad_t *xad, *lxad, *rxad;
  1499. int xflag;
  1500. s64 nxoff, xoff;
  1501. int nxlen, xlen, lxlen, rxlen;
  1502. s64 nxaddr, xaddr;
  1503. struct tlock *tlck;
  1504. struct xtlock *xtlck = NULL;
  1505. int newpage = 0;
  1506. /* there must exist extent to be tailgated */
  1507. nxoff = offsetXAD(nxad);
  1508. nxlen = lengthXAD(nxad);
  1509. nxaddr = addressXAD(nxad);
  1510. if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
  1511. return rc;
  1512. /* retrieve search result */
  1513. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1514. if (cmp != 0) {
  1515. XT_PUTPAGE(mp);
  1516. jfs_error(ip->i_sb, "Could not find extent\n");
  1517. return -EIO;
  1518. }
  1519. BT_MARK_DIRTY(mp, ip);
  1520. /*
  1521. * acquire tlock of the leaf page containing original entry
  1522. */
  1523. if (!test_cflag(COMMIT_Nolink, ip)) {
  1524. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1525. xtlck = (struct xtlock *) & tlck->lock;
  1526. }
  1527. xad = &p->xad[index0];
  1528. xflag = xad->flag;
  1529. xoff = offsetXAD(xad);
  1530. xlen = lengthXAD(xad);
  1531. xaddr = addressXAD(xad);
  1532. /* nXAD must be completely contained within XAD */
  1533. if ((xoff > nxoff) ||
  1534. (nxoff + nxlen > xoff + xlen)) {
  1535. XT_PUTPAGE(mp);
  1536. jfs_error(ip->i_sb,
  1537. "nXAD in not completely contained within XAD\n");
  1538. return -EIO;
  1539. }
  1540. index = index0;
  1541. newindex = index + 1;
  1542. nextindex = le16_to_cpu(p->header.nextindex);
  1543. #ifdef _JFS_WIP_NOCOALESCE
  1544. if (xoff < nxoff)
  1545. goto updateRight;
  1546. /*
  1547. * replace XAD with nXAD
  1548. */
  1549. replace: /* (nxoff == xoff) */
  1550. if (nxlen == xlen) {
  1551. /* replace XAD with nXAD:recorded */
  1552. *xad = *nxad;
  1553. xad->flag = xflag & ~XAD_NOTRECORDED;
  1554. goto out;
  1555. } else /* (nxlen < xlen) */
  1556. goto updateLeft;
  1557. #endif /* _JFS_WIP_NOCOALESCE */
  1558. /* #ifdef _JFS_WIP_COALESCE */
  1559. if (xoff < nxoff)
  1560. goto coalesceRight;
  1561. /*
  1562. * coalesce with left XAD
  1563. */
  1564. //coalesceLeft: /* (xoff == nxoff) */
  1565. /* is XAD first entry of page ? */
  1566. if (index == XTENTRYSTART)
  1567. goto replace;
  1568. /* is nXAD logically and physically contiguous with lXAD ? */
  1569. lxad = &p->xad[index - 1];
  1570. lxlen = lengthXAD(lxad);
  1571. if (!(lxad->flag & XAD_NOTRECORDED) &&
  1572. (nxoff == offsetXAD(lxad) + lxlen) &&
  1573. (nxaddr == addressXAD(lxad) + lxlen) &&
  1574. (lxlen + nxlen < MAXXLEN)) {
  1575. /* extend right lXAD */
  1576. index0 = index - 1;
  1577. XADlength(lxad, lxlen + nxlen);
  1578. /* If we just merged two extents together, need to make sure the
  1579. * right extent gets logged. If the left one is marked XAD_NEW,
  1580. * then we know it will be logged. Otherwise, mark as
  1581. * XAD_EXTENDED
  1582. */
  1583. if (!(lxad->flag & XAD_NEW))
  1584. lxad->flag |= XAD_EXTENDED;
  1585. if (xlen > nxlen) {
  1586. /* truncate XAD */
  1587. XADoffset(xad, xoff + nxlen);
  1588. XADlength(xad, xlen - nxlen);
  1589. XADaddress(xad, xaddr + nxlen);
  1590. goto out;
  1591. } else { /* (xlen == nxlen) */
  1592. /* remove XAD */
  1593. if (index < nextindex - 1)
  1594. memmove(&p->xad[index], &p->xad[index + 1],
  1595. (nextindex - index -
  1596. 1) << L2XTSLOTSIZE);
  1597. p->header.nextindex =
  1598. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1599. 1);
  1600. index = index0;
  1601. newindex = index + 1;
  1602. nextindex = le16_to_cpu(p->header.nextindex);
  1603. xoff = nxoff = offsetXAD(lxad);
  1604. xlen = nxlen = lxlen + nxlen;
  1605. xaddr = nxaddr = addressXAD(lxad);
  1606. goto coalesceRight;
  1607. }
  1608. }
  1609. /*
  1610. * replace XAD with nXAD
  1611. */
  1612. replace: /* (nxoff == xoff) */
  1613. if (nxlen == xlen) {
  1614. /* replace XAD with nXAD:recorded */
  1615. *xad = *nxad;
  1616. xad->flag = xflag & ~XAD_NOTRECORDED;
  1617. goto coalesceRight;
  1618. } else /* (nxlen < xlen) */
  1619. goto updateLeft;
  1620. /*
  1621. * coalesce with right XAD
  1622. */
  1623. coalesceRight: /* (xoff <= nxoff) */
  1624. /* is XAD last entry of page ? */
  1625. if (newindex == nextindex) {
  1626. if (xoff == nxoff)
  1627. goto out;
  1628. goto updateRight;
  1629. }
  1630. /* is nXAD logically and physically contiguous with rXAD ? */
  1631. rxad = &p->xad[index + 1];
  1632. rxlen = lengthXAD(rxad);
  1633. if (!(rxad->flag & XAD_NOTRECORDED) &&
  1634. (nxoff + nxlen == offsetXAD(rxad)) &&
  1635. (nxaddr + nxlen == addressXAD(rxad)) &&
  1636. (rxlen + nxlen < MAXXLEN)) {
  1637. /* extend left rXAD */
  1638. XADoffset(rxad, nxoff);
  1639. XADlength(rxad, rxlen + nxlen);
  1640. XADaddress(rxad, nxaddr);
  1641. /* If we just merged two extents together, need to make sure
  1642. * the left extent gets logged. If the right one is marked
  1643. * XAD_NEW, then we know it will be logged. Otherwise, mark as
  1644. * XAD_EXTENDED
  1645. */
  1646. if (!(rxad->flag & XAD_NEW))
  1647. rxad->flag |= XAD_EXTENDED;
  1648. if (xlen > nxlen)
  1649. /* truncate XAD */
  1650. XADlength(xad, xlen - nxlen);
  1651. else { /* (xlen == nxlen) */
  1652. /* remove XAD */
  1653. memmove(&p->xad[index], &p->xad[index + 1],
  1654. (nextindex - index - 1) << L2XTSLOTSIZE);
  1655. p->header.nextindex =
  1656. cpu_to_le16(le16_to_cpu(p->header.nextindex) -
  1657. 1);
  1658. }
  1659. goto out;
  1660. } else if (xoff == nxoff)
  1661. goto out;
  1662. if (xoff >= nxoff) {
  1663. XT_PUTPAGE(mp);
  1664. jfs_error(ip->i_sb, "xoff >= nxoff\n");
  1665. return -EIO;
  1666. }
  1667. /* #endif _JFS_WIP_COALESCE */
  1668. /*
  1669. * split XAD into (lXAD, nXAD):
  1670. *
  1671. * |---nXAD--->
  1672. * --|----------XAD----------|--
  1673. * |-lXAD-|
  1674. */
  1675. updateRight: /* (xoff < nxoff) */
  1676. /* truncate old XAD as lXAD:not_recorded */
  1677. xad = &p->xad[index];
  1678. XADlength(xad, nxoff - xoff);
  1679. /* insert nXAD:recorded */
  1680. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1681. /* xtSpliUp() unpins leaf pages */
  1682. split.mp = mp;
  1683. split.index = newindex;
  1684. split.flag = xflag & ~XAD_NOTRECORDED;
  1685. split.off = nxoff;
  1686. split.len = nxlen;
  1687. split.addr = nxaddr;
  1688. split.pxdlist = NULL;
  1689. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1690. return rc;
  1691. /* get back old page */
  1692. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1693. if (rc)
  1694. return rc;
  1695. /*
  1696. * if leaf root has been split, original root has been
  1697. * copied to new child page, i.e., original entry now
  1698. * resides on the new child page;
  1699. */
  1700. if (p->header.flag & BT_INTERNAL) {
  1701. ASSERT(p->header.nextindex ==
  1702. cpu_to_le16(XTENTRYSTART + 1));
  1703. xad = &p->xad[XTENTRYSTART];
  1704. bn = addressXAD(xad);
  1705. XT_PUTPAGE(mp);
  1706. /* get new child page */
  1707. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1708. if (rc)
  1709. return rc;
  1710. BT_MARK_DIRTY(mp, ip);
  1711. if (!test_cflag(COMMIT_Nolink, ip)) {
  1712. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1713. xtlck = (struct xtlock *) & tlck->lock;
  1714. }
  1715. } else {
  1716. /* is nXAD on new page ? */
  1717. if (newindex >
  1718. (le16_to_cpu(p->header.maxentry) >> 1)) {
  1719. newindex =
  1720. newindex -
  1721. le16_to_cpu(p->header.nextindex) +
  1722. XTENTRYSTART;
  1723. newpage = 1;
  1724. }
  1725. }
  1726. } else {
  1727. /* if insert into middle, shift right remaining entries */
  1728. if (newindex < nextindex)
  1729. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  1730. (nextindex - newindex) << L2XTSLOTSIZE);
  1731. /* insert the entry */
  1732. xad = &p->xad[newindex];
  1733. *xad = *nxad;
  1734. xad->flag = xflag & ~XAD_NOTRECORDED;
  1735. /* advance next available entry index. */
  1736. p->header.nextindex =
  1737. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1738. }
  1739. /*
  1740. * does nXAD force 3-way split ?
  1741. *
  1742. * |---nXAD--->|
  1743. * --|----------XAD-------------|--
  1744. * |-lXAD-| |-rXAD -|
  1745. */
  1746. if (nxoff + nxlen == xoff + xlen)
  1747. goto out;
  1748. /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
  1749. if (newpage) {
  1750. /* close out old page */
  1751. if (!test_cflag(COMMIT_Nolink, ip)) {
  1752. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1753. min(index0, (int)xtlck->lwm.offset) : index0;
  1754. xtlck->lwm.length =
  1755. le16_to_cpu(p->header.nextindex) -
  1756. xtlck->lwm.offset;
  1757. }
  1758. bn = le64_to_cpu(p->header.next);
  1759. XT_PUTPAGE(mp);
  1760. /* get new right page */
  1761. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1762. if (rc)
  1763. return rc;
  1764. BT_MARK_DIRTY(mp, ip);
  1765. if (!test_cflag(COMMIT_Nolink, ip)) {
  1766. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  1767. xtlck = (struct xtlock *) & tlck->lock;
  1768. }
  1769. index0 = index = newindex;
  1770. } else
  1771. index++;
  1772. newindex = index + 1;
  1773. nextindex = le16_to_cpu(p->header.nextindex);
  1774. xlen = xlen - (nxoff - xoff);
  1775. xoff = nxoff;
  1776. xaddr = nxaddr;
  1777. /* recompute split pages */
  1778. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1779. XT_PUTPAGE(mp);
  1780. if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
  1781. return rc;
  1782. /* retrieve search result */
  1783. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
  1784. if (cmp != 0) {
  1785. XT_PUTPAGE(mp);
  1786. jfs_error(ip->i_sb, "xtSearch failed\n");
  1787. return -EIO;
  1788. }
  1789. if (index0 != index) {
  1790. XT_PUTPAGE(mp);
  1791. jfs_error(ip->i_sb, "unexpected value of index\n");
  1792. return -EIO;
  1793. }
  1794. }
  1795. /*
  1796. * split XAD into (nXAD, rXAD)
  1797. *
  1798. * ---nXAD---|
  1799. * --|----------XAD----------|--
  1800. * |-rXAD-|
  1801. */
  1802. updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
  1803. /* update old XAD with nXAD:recorded */
  1804. xad = &p->xad[index];
  1805. *xad = *nxad;
  1806. xad->flag = xflag & ~XAD_NOTRECORDED;
  1807. /* insert rXAD:not_recorded */
  1808. xoff = xoff + nxlen;
  1809. xlen = xlen - nxlen;
  1810. xaddr = xaddr + nxlen;
  1811. if (nextindex == le16_to_cpu(p->header.maxentry)) {
  1812. /*
  1813. printf("xtUpdate.updateLeft.split p:0x%p\n", p);
  1814. */
  1815. /* xtSpliUp() unpins leaf pages */
  1816. split.mp = mp;
  1817. split.index = newindex;
  1818. split.flag = xflag;
  1819. split.off = xoff;
  1820. split.len = xlen;
  1821. split.addr = xaddr;
  1822. split.pxdlist = NULL;
  1823. if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
  1824. return rc;
  1825. /* get back old page */
  1826. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1827. if (rc)
  1828. return rc;
  1829. /*
  1830. * if leaf root has been split, original root has been
  1831. * copied to new child page, i.e., original entry now
  1832. * resides on the new child page;
  1833. */
  1834. if (p->header.flag & BT_INTERNAL) {
  1835. ASSERT(p->header.nextindex ==
  1836. cpu_to_le16(XTENTRYSTART + 1));
  1837. xad = &p->xad[XTENTRYSTART];
  1838. bn = addressXAD(xad);
  1839. XT_PUTPAGE(mp);
  1840. /* get new child page */
  1841. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  1842. if (rc)
  1843. return rc;
  1844. BT_MARK_DIRTY(mp, ip);
  1845. if (!test_cflag(COMMIT_Nolink, ip)) {
  1846. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  1847. xtlck = (struct xtlock *) & tlck->lock;
  1848. }
  1849. }
  1850. } else {
  1851. /* if insert into middle, shift right remaining entries */
  1852. if (newindex < nextindex)
  1853. memmove(&p->xad[newindex + 1], &p->xad[newindex],
  1854. (nextindex - newindex) << L2XTSLOTSIZE);
  1855. /* insert the entry */
  1856. xad = &p->xad[newindex];
  1857. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  1858. /* advance next available entry index. */
  1859. p->header.nextindex =
  1860. cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
  1861. }
  1862. out:
  1863. if (!test_cflag(COMMIT_Nolink, ip)) {
  1864. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  1865. min(index0, (int)xtlck->lwm.offset) : index0;
  1866. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  1867. xtlck->lwm.offset;
  1868. }
  1869. /* unpin the leaf page */
  1870. XT_PUTPAGE(mp);
  1871. return rc;
  1872. }
  1873. /*
  1874. * xtAppend()
  1875. *
  1876. * function: grow in append mode from contiguous region specified ;
  1877. *
  1878. * parameter:
  1879. * tid - transaction id;
  1880. * ip - file object;
  1881. * xflag - extent flag:
  1882. * xoff - extent offset;
  1883. * maxblocks - max extent length;
  1884. * xlen - extent length (in/out);
  1885. * xaddrp - extent address pointer (in/out):
  1886. * flag -
  1887. *
  1888. * return:
  1889. */
  1890. int xtAppend(tid_t tid, /* transaction id */
  1891. struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
  1892. s32 * xlenp, /* (in/out) */
  1893. s64 * xaddrp, /* (in/out) */
  1894. int flag)
  1895. {
  1896. int rc = 0;
  1897. struct metapage *mp; /* meta-page buffer */
  1898. xtpage_t *p; /* base B+-tree index page */
  1899. s64 bn, xaddr;
  1900. int index, nextindex;
  1901. struct btstack btstack; /* traverse stack */
  1902. struct xtsplit split; /* split information */
  1903. xad_t *xad;
  1904. int cmp;
  1905. struct tlock *tlck;
  1906. struct xtlock *xtlck;
  1907. int nsplit, nblocks, xlen;
  1908. struct pxdlist pxdlist;
  1909. pxd_t *pxd;
  1910. s64 next;
  1911. xaddr = *xaddrp;
  1912. xlen = *xlenp;
  1913. jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
  1914. (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
  1915. /*
  1916. * search for the entry location at which to insert:
  1917. *
  1918. * xtFastSearch() and xtSearch() both returns (leaf page
  1919. * pinned, index at which to insert).
  1920. * n.b. xtSearch() may return index of maxentry of
  1921. * the full page.
  1922. */
  1923. if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
  1924. return rc;
  1925. /* retrieve search result */
  1926. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  1927. if (cmp == 0) {
  1928. rc = -EEXIST;
  1929. goto out;
  1930. }
  1931. if (next)
  1932. xlen = min(xlen, (int)(next - xoff));
  1933. //insert:
  1934. /*
  1935. * insert entry for new extent
  1936. */
  1937. xflag |= XAD_NEW;
  1938. /*
  1939. * if the leaf page is full, split the page and
  1940. * propagate up the router entry for the new page from split
  1941. *
  1942. * The xtSplitUp() will insert the entry and unpin the leaf page.
  1943. */
  1944. nextindex = le16_to_cpu(p->header.nextindex);
  1945. if (nextindex < le16_to_cpu(p->header.maxentry))
  1946. goto insertLeaf;
  1947. /*
  1948. * allocate new index blocks to cover index page split(s)
  1949. */
  1950. nsplit = btstack.nsplit;
  1951. split.pxdlist = &pxdlist;
  1952. pxdlist.maxnpxd = pxdlist.npxd = 0;
  1953. pxd = &pxdlist.pxd[0];
  1954. nblocks = JFS_SBI(ip->i_sb)->nbperpage;
  1955. for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
  1956. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
  1957. PXDaddress(pxd, xaddr);
  1958. PXDlength(pxd, nblocks);
  1959. pxdlist.maxnpxd++;
  1960. continue;
  1961. }
  1962. /* undo allocation */
  1963. goto out;
  1964. }
  1965. xlen = min(xlen, maxblocks);
  1966. /*
  1967. * allocate data extent requested
  1968. */
  1969. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  1970. goto out;
  1971. split.mp = mp;
  1972. split.index = index;
  1973. split.flag = xflag;
  1974. split.off = xoff;
  1975. split.len = xlen;
  1976. split.addr = xaddr;
  1977. if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
  1978. /* undo data extent allocation */
  1979. dbFree(ip, *xaddrp, (s64) * xlenp);
  1980. return rc;
  1981. }
  1982. *xaddrp = xaddr;
  1983. *xlenp = xlen;
  1984. return 0;
  1985. /*
  1986. * insert the new entry into the leaf page
  1987. */
  1988. insertLeaf:
  1989. /*
  1990. * allocate data extent requested
  1991. */
  1992. if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
  1993. goto out;
  1994. BT_MARK_DIRTY(mp, ip);
  1995. /*
  1996. * acquire a transaction lock on the leaf page;
  1997. *
  1998. * action: xad insertion/extension;
  1999. */
  2000. tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
  2001. xtlck = (struct xtlock *) & tlck->lock;
  2002. /* insert the new entry: mark the entry NEW */
  2003. xad = &p->xad[index];
  2004. XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
  2005. /* advance next available entry index */
  2006. le16_add_cpu(&p->header.nextindex, 1);
  2007. xtlck->lwm.offset =
  2008. (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
  2009. xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
  2010. xtlck->lwm.offset;
  2011. *xaddrp = xaddr;
  2012. *xlenp = xlen;
  2013. out:
  2014. /* unpin the leaf page */
  2015. XT_PUTPAGE(mp);
  2016. return rc;
  2017. }
  2018. #ifdef _STILL_TO_PORT
  2019. /* - TBD for defragmentaion/reorganization -
  2020. *
  2021. * xtDelete()
  2022. *
  2023. * function:
  2024. * delete the entry with the specified key.
  2025. *
  2026. * N.B.: whole extent of the entry is assumed to be deleted.
  2027. *
  2028. * parameter:
  2029. *
  2030. * return:
  2031. * ENOENT: if the entry is not found.
  2032. *
  2033. * exception:
  2034. */
  2035. int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
  2036. {
  2037. int rc = 0;
  2038. struct btstack btstack;
  2039. int cmp;
  2040. s64 bn;
  2041. struct metapage *mp;
  2042. xtpage_t *p;
  2043. int index, nextindex;
  2044. struct tlock *tlck;
  2045. struct xtlock *xtlck;
  2046. /*
  2047. * find the matching entry; xtSearch() pins the page
  2048. */
  2049. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
  2050. return rc;
  2051. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  2052. if (cmp) {
  2053. /* unpin the leaf page */
  2054. XT_PUTPAGE(mp);
  2055. return -ENOENT;
  2056. }
  2057. /*
  2058. * delete the entry from the leaf page
  2059. */
  2060. nextindex = le16_to_cpu(p->header.nextindex);
  2061. le16_add_cpu(&p->header.nextindex, -1);
  2062. /*
  2063. * if the leaf page bocome empty, free the page
  2064. */
  2065. if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
  2066. return (xtDeleteUp(tid, ip, mp, p, &btstack));
  2067. BT_MARK_DIRTY(mp, ip);
  2068. /*
  2069. * acquire a transaction lock on the leaf page;
  2070. *
  2071. * action:xad deletion;
  2072. */
  2073. tlck = txLock(tid, ip, mp, tlckXTREE);
  2074. xtlck = (struct xtlock *) & tlck->lock;
  2075. xtlck->lwm.offset =
  2076. (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
  2077. /* if delete from middle, shift left/compact the remaining entries */
  2078. if (index < nextindex - 1)
  2079. memmove(&p->xad[index], &p->xad[index + 1],
  2080. (nextindex - index - 1) * sizeof(xad_t));
  2081. XT_PUTPAGE(mp);
  2082. return 0;
  2083. }
  2084. /* - TBD for defragmentaion/reorganization -
  2085. *
  2086. * xtDeleteUp()
  2087. *
  2088. * function:
  2089. * free empty pages as propagating deletion up the tree
  2090. *
  2091. * parameter:
  2092. *
  2093. * return:
  2094. */
  2095. static int
  2096. xtDeleteUp(tid_t tid, struct inode *ip,
  2097. struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
  2098. {
  2099. int rc = 0;
  2100. struct metapage *mp;
  2101. xtpage_t *p;
  2102. int index, nextindex;
  2103. s64 xaddr;
  2104. int xlen;
  2105. struct btframe *parent;
  2106. struct tlock *tlck;
  2107. struct xtlock *xtlck;
  2108. /*
  2109. * keep root leaf page which has become empty
  2110. */
  2111. if (fp->header.flag & BT_ROOT) {
  2112. /* keep the root page */
  2113. fp->header.flag &= ~BT_INTERNAL;
  2114. fp->header.flag |= BT_LEAF;
  2115. fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2116. /* XT_PUTPAGE(fmp); */
  2117. return 0;
  2118. }
  2119. /*
  2120. * free non-root leaf page
  2121. */
  2122. if ((rc = xtRelink(tid, ip, fp))) {
  2123. XT_PUTPAGE(fmp);
  2124. return rc;
  2125. }
  2126. xaddr = addressPXD(&fp->header.self);
  2127. xlen = lengthPXD(&fp->header.self);
  2128. /* free the page extent */
  2129. dbFree(ip, xaddr, (s64) xlen);
  2130. /* free the buffer page */
  2131. discard_metapage(fmp);
  2132. /*
  2133. * propagate page deletion up the index tree
  2134. *
  2135. * If the delete from the parent page makes it empty,
  2136. * continue all the way up the tree.
  2137. * stop if the root page is reached (which is never deleted) or
  2138. * if the entry deletion does not empty the page.
  2139. */
  2140. while ((parent = BT_POP(btstack)) != NULL) {
  2141. /* get/pin the parent page <sp> */
  2142. XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
  2143. if (rc)
  2144. return rc;
  2145. index = parent->index;
  2146. /* delete the entry for the freed child page from parent.
  2147. */
  2148. nextindex = le16_to_cpu(p->header.nextindex);
  2149. /*
  2150. * the parent has the single entry being deleted:
  2151. * free the parent page which has become empty.
  2152. */
  2153. if (nextindex == 1) {
  2154. if (p->header.flag & BT_ROOT) {
  2155. /* keep the root page */
  2156. p->header.flag &= ~BT_INTERNAL;
  2157. p->header.flag |= BT_LEAF;
  2158. p->header.nextindex =
  2159. cpu_to_le16(XTENTRYSTART);
  2160. /* XT_PUTPAGE(mp); */
  2161. break;
  2162. } else {
  2163. /* free the parent page */
  2164. if ((rc = xtRelink(tid, ip, p)))
  2165. return rc;
  2166. xaddr = addressPXD(&p->header.self);
  2167. /* free the page extent */
  2168. dbFree(ip, xaddr,
  2169. (s64) JFS_SBI(ip->i_sb)->nbperpage);
  2170. /* unpin/free the buffer page */
  2171. discard_metapage(mp);
  2172. /* propagate up */
  2173. continue;
  2174. }
  2175. }
  2176. /*
  2177. * the parent has other entries remaining:
  2178. * delete the router entry from the parent page.
  2179. */
  2180. else {
  2181. BT_MARK_DIRTY(mp, ip);
  2182. /*
  2183. * acquire a transaction lock on the leaf page;
  2184. *
  2185. * action:xad deletion;
  2186. */
  2187. tlck = txLock(tid, ip, mp, tlckXTREE);
  2188. xtlck = (struct xtlock *) & tlck->lock;
  2189. xtlck->lwm.offset =
  2190. (xtlck->lwm.offset) ? min(index,
  2191. xtlck->lwm.
  2192. offset) : index;
  2193. /* if delete from middle,
  2194. * shift left/compact the remaining entries in the page
  2195. */
  2196. if (index < nextindex - 1)
  2197. memmove(&p->xad[index], &p->xad[index + 1],
  2198. (nextindex - index -
  2199. 1) << L2XTSLOTSIZE);
  2200. le16_add_cpu(&p->header.nextindex, -1);
  2201. jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
  2202. (ulong) parent->bn, index);
  2203. }
  2204. /* unpin the parent page */
  2205. XT_PUTPAGE(mp);
  2206. /* exit propagation up */
  2207. break;
  2208. }
  2209. return 0;
  2210. }
  2211. /*
  2212. * NAME: xtRelocate()
  2213. *
  2214. * FUNCTION: relocate xtpage or data extent of regular file;
  2215. * This function is mainly used by defragfs utility.
  2216. *
  2217. * NOTE: This routine does not have the logic to handle
  2218. * uncommitted allocated extent. The caller should call
  2219. * txCommit() to commit all the allocation before call
  2220. * this routine.
  2221. */
  2222. int
  2223. xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
  2224. s64 nxaddr, /* new xaddr */
  2225. int xtype)
  2226. { /* extent type: XTPAGE or DATAEXT */
  2227. int rc = 0;
  2228. struct tblock *tblk;
  2229. struct tlock *tlck;
  2230. struct xtlock *xtlck;
  2231. struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
  2232. xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
  2233. xad_t *xad;
  2234. pxd_t *pxd;
  2235. s64 xoff, xsize;
  2236. int xlen;
  2237. s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
  2238. cbuf_t *cp;
  2239. s64 offset, nbytes, nbrd, pno;
  2240. int nb, npages, nblks;
  2241. s64 bn;
  2242. int cmp;
  2243. int index;
  2244. struct pxd_lock *pxdlock;
  2245. struct btstack btstack; /* traverse stack */
  2246. xtype = xtype & EXTENT_TYPE;
  2247. xoff = offsetXAD(oxad);
  2248. oxaddr = addressXAD(oxad);
  2249. xlen = lengthXAD(oxad);
  2250. /* validate extent offset */
  2251. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2252. if (offset >= ip->i_size)
  2253. return -ESTALE; /* stale extent */
  2254. jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
  2255. xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
  2256. /*
  2257. * 1. get and validate the parent xtpage/xad entry
  2258. * covering the source extent to be relocated;
  2259. */
  2260. if (xtype == DATAEXT) {
  2261. /* search in leaf entry */
  2262. rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
  2263. if (rc)
  2264. return rc;
  2265. /* retrieve search result */
  2266. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2267. if (cmp) {
  2268. XT_PUTPAGE(pmp);
  2269. return -ESTALE;
  2270. }
  2271. /* validate for exact match with a single entry */
  2272. xad = &pp->xad[index];
  2273. if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
  2274. XT_PUTPAGE(pmp);
  2275. return -ESTALE;
  2276. }
  2277. } else { /* (xtype == XTPAGE) */
  2278. /* search in internal entry */
  2279. rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
  2280. if (rc)
  2281. return rc;
  2282. /* retrieve search result */
  2283. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2284. if (cmp) {
  2285. XT_PUTPAGE(pmp);
  2286. return -ESTALE;
  2287. }
  2288. /* xtSearchNode() validated for exact match with a single entry
  2289. */
  2290. xad = &pp->xad[index];
  2291. }
  2292. jfs_info("xtRelocate: parent xad entry validated.");
  2293. /*
  2294. * 2. relocate the extent
  2295. */
  2296. if (xtype == DATAEXT) {
  2297. /* if the extent is allocated-but-not-recorded
  2298. * there is no real data to be moved in this extent,
  2299. */
  2300. if (xad->flag & XAD_NOTRECORDED)
  2301. goto out;
  2302. else
  2303. /* release xtpage for cmRead()/xtLookup() */
  2304. XT_PUTPAGE(pmp);
  2305. /*
  2306. * cmRelocate()
  2307. *
  2308. * copy target data pages to be relocated;
  2309. *
  2310. * data extent must start at page boundary and
  2311. * multiple of page size (except the last data extent);
  2312. * read in each page of the source data extent into cbuf,
  2313. * update the cbuf extent descriptor of the page to be
  2314. * homeward bound to new dst data extent
  2315. * copy the data from the old extent to new extent.
  2316. * copy is essential for compressed files to avoid problems
  2317. * that can arise if there was a change in compression
  2318. * algorithms.
  2319. * it is a good strategy because it may disrupt cache
  2320. * policy to keep the pages in memory afterwards.
  2321. */
  2322. offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
  2323. assert((offset & CM_OFFSET) == 0);
  2324. nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2325. pno = offset >> CM_L2BSIZE;
  2326. npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
  2327. /*
  2328. npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
  2329. (offset >> CM_L2BSIZE) + 1;
  2330. */
  2331. sxaddr = oxaddr;
  2332. dxaddr = nxaddr;
  2333. /* process the request one cache buffer at a time */
  2334. for (nbrd = 0; nbrd < nbytes; nbrd += nb,
  2335. offset += nb, pno++, npages--) {
  2336. /* compute page size */
  2337. nb = min(nbytes - nbrd, CM_BSIZE);
  2338. /* get the cache buffer of the page */
  2339. if (rc = cmRead(ip, offset, npages, &cp))
  2340. break;
  2341. assert(addressPXD(&cp->cm_pxd) == sxaddr);
  2342. assert(!cp->cm_modified);
  2343. /* bind buffer with the new extent address */
  2344. nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
  2345. cmSetXD(ip, cp, pno, dxaddr, nblks);
  2346. /* release the cbuf, mark it as modified */
  2347. cmPut(cp, true);
  2348. dxaddr += nblks;
  2349. sxaddr += nblks;
  2350. }
  2351. /* get back parent page */
  2352. if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
  2353. return rc;
  2354. XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
  2355. jfs_info("xtRelocate: target data extent relocated.");
  2356. } else { /* (xtype == XTPAGE) */
  2357. /*
  2358. * read in the target xtpage from the source extent;
  2359. */
  2360. XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
  2361. if (rc) {
  2362. XT_PUTPAGE(pmp);
  2363. return rc;
  2364. }
  2365. /*
  2366. * read in sibling pages if any to update sibling pointers;
  2367. */
  2368. rmp = NULL;
  2369. if (p->header.next) {
  2370. nextbn = le64_to_cpu(p->header.next);
  2371. XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
  2372. if (rc) {
  2373. XT_PUTPAGE(pmp);
  2374. XT_PUTPAGE(mp);
  2375. return (rc);
  2376. }
  2377. }
  2378. lmp = NULL;
  2379. if (p->header.prev) {
  2380. prevbn = le64_to_cpu(p->header.prev);
  2381. XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
  2382. if (rc) {
  2383. XT_PUTPAGE(pmp);
  2384. XT_PUTPAGE(mp);
  2385. if (rmp)
  2386. XT_PUTPAGE(rmp);
  2387. return (rc);
  2388. }
  2389. }
  2390. /* at this point, all xtpages to be updated are in memory */
  2391. /*
  2392. * update sibling pointers of sibling xtpages if any;
  2393. */
  2394. if (lmp) {
  2395. BT_MARK_DIRTY(lmp, ip);
  2396. tlck = txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
  2397. lp->header.next = cpu_to_le64(nxaddr);
  2398. XT_PUTPAGE(lmp);
  2399. }
  2400. if (rmp) {
  2401. BT_MARK_DIRTY(rmp, ip);
  2402. tlck = txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
  2403. rp->header.prev = cpu_to_le64(nxaddr);
  2404. XT_PUTPAGE(rmp);
  2405. }
  2406. /*
  2407. * update the target xtpage to be relocated
  2408. *
  2409. * update the self address of the target page
  2410. * and write to destination extent;
  2411. * redo image covers the whole xtpage since it is new page
  2412. * to the destination extent;
  2413. * update of bmap for the free of source extent
  2414. * of the target xtpage itself:
  2415. * update of bmap for the allocation of destination extent
  2416. * of the target xtpage itself:
  2417. * update of bmap for the extents covered by xad entries in
  2418. * the target xtpage is not necessary since they are not
  2419. * updated;
  2420. * if not committed before this relocation,
  2421. * target page may contain XAD_NEW entries which must
  2422. * be scanned for bmap update (logredo() always
  2423. * scan xtpage REDOPAGE image for bmap update);
  2424. * if committed before this relocation (tlckRELOCATE),
  2425. * scan may be skipped by commit() and logredo();
  2426. */
  2427. BT_MARK_DIRTY(mp, ip);
  2428. /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
  2429. tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
  2430. xtlck = (struct xtlock *) & tlck->lock;
  2431. /* update the self address in the xtpage header */
  2432. pxd = &p->header.self;
  2433. PXDaddress(pxd, nxaddr);
  2434. /* linelock for the after image of the whole page */
  2435. xtlck->lwm.length =
  2436. le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
  2437. /* update the buffer extent descriptor of target xtpage */
  2438. xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
  2439. bmSetXD(mp, nxaddr, xsize);
  2440. /* unpin the target page to new homeward bound */
  2441. XT_PUTPAGE(mp);
  2442. jfs_info("xtRelocate: target xtpage relocated.");
  2443. }
  2444. /*
  2445. * 3. acquire maplock for the source extent to be freed;
  2446. *
  2447. * acquire a maplock saving the src relocated extent address;
  2448. * to free of the extent at commit time;
  2449. */
  2450. out:
  2451. /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
  2452. * free PXD of the source data extent (logredo() will update
  2453. * bmap for free of source data extent), and update bmap for
  2454. * free of the source data extent;
  2455. */
  2456. if (xtype == DATAEXT)
  2457. tlck = txMaplock(tid, ip, tlckMAP);
  2458. /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
  2459. * for the source xtpage (logredo() will init NoRedoPage
  2460. * filter and will also update bmap for free of the source
  2461. * xtpage), and update bmap for free of the source xtpage;
  2462. * N.B. We use tlckMAP instead of tlkcXTREE because there
  2463. * is no buffer associated with this lock since the buffer
  2464. * has been redirected to the target location.
  2465. */
  2466. else /* (xtype == XTPAGE) */
  2467. tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
  2468. pxdlock = (struct pxd_lock *) & tlck->lock;
  2469. pxdlock->flag = mlckFREEPXD;
  2470. PXDaddress(&pxdlock->pxd, oxaddr);
  2471. PXDlength(&pxdlock->pxd, xlen);
  2472. pxdlock->index = 1;
  2473. /*
  2474. * 4. update the parent xad entry for relocation;
  2475. *
  2476. * acquire tlck for the parent entry with XAD_NEW as entry
  2477. * update which will write LOG_REDOPAGE and update bmap for
  2478. * allocation of XAD_NEW destination extent;
  2479. */
  2480. jfs_info("xtRelocate: update parent xad entry.");
  2481. BT_MARK_DIRTY(pmp, ip);
  2482. tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
  2483. xtlck = (struct xtlock *) & tlck->lock;
  2484. /* update the XAD with the new destination extent; */
  2485. xad = &pp->xad[index];
  2486. xad->flag |= XAD_NEW;
  2487. XADaddress(xad, nxaddr);
  2488. xtlck->lwm.offset = min(index, xtlck->lwm.offset);
  2489. xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
  2490. xtlck->lwm.offset;
  2491. /* unpin the parent xtpage */
  2492. XT_PUTPAGE(pmp);
  2493. return rc;
  2494. }
  2495. /*
  2496. * xtSearchNode()
  2497. *
  2498. * function: search for the internal xad entry covering specified extent.
  2499. * This function is mainly used by defragfs utility.
  2500. *
  2501. * parameters:
  2502. * ip - file object;
  2503. * xad - extent to find;
  2504. * cmpp - comparison result:
  2505. * btstack - traverse stack;
  2506. * flag - search process flag;
  2507. *
  2508. * returns:
  2509. * btstack contains (bn, index) of search path traversed to the entry.
  2510. * *cmpp is set to result of comparison with the entry returned.
  2511. * the page containing the entry is pinned at exit.
  2512. */
  2513. static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
  2514. int *cmpp, struct btstack * btstack, int flag)
  2515. {
  2516. int rc = 0;
  2517. s64 xoff, xaddr;
  2518. int xlen;
  2519. int cmp = 1; /* init for empty page */
  2520. s64 bn; /* block number */
  2521. struct metapage *mp; /* meta-page buffer */
  2522. xtpage_t *p; /* page */
  2523. int base, index, lim;
  2524. struct btframe *btsp;
  2525. s64 t64;
  2526. BT_CLR(btstack);
  2527. xoff = offsetXAD(xad);
  2528. xlen = lengthXAD(xad);
  2529. xaddr = addressXAD(xad);
  2530. /*
  2531. * search down tree from root:
  2532. *
  2533. * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
  2534. * internal page, child page Pi contains entry with k, Ki <= K < Kj.
  2535. *
  2536. * if entry with search key K is not found
  2537. * internal page search find the entry with largest key Ki
  2538. * less than K which point to the child page to search;
  2539. * leaf page search find the entry with smallest key Kj
  2540. * greater than K so that the returned index is the position of
  2541. * the entry to be shifted right for insertion of new entry.
  2542. * for empty tree, search key is greater than any key of the tree.
  2543. *
  2544. * by convention, root bn = 0.
  2545. */
  2546. for (bn = 0;;) {
  2547. /* get/pin the page to search */
  2548. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2549. if (rc)
  2550. return rc;
  2551. if (p->header.flag & BT_LEAF) {
  2552. XT_PUTPAGE(mp);
  2553. return -ESTALE;
  2554. }
  2555. lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  2556. /*
  2557. * binary search with search key K on the current page
  2558. */
  2559. for (base = XTENTRYSTART; lim; lim >>= 1) {
  2560. index = base + (lim >> 1);
  2561. XT_CMP(cmp, xoff, &p->xad[index], t64);
  2562. if (cmp == 0) {
  2563. /*
  2564. * search hit
  2565. *
  2566. * verify for exact match;
  2567. */
  2568. if (xaddr == addressXAD(&p->xad[index]) &&
  2569. xoff == offsetXAD(&p->xad[index])) {
  2570. *cmpp = cmp;
  2571. /* save search result */
  2572. btsp = btstack->top;
  2573. btsp->bn = bn;
  2574. btsp->index = index;
  2575. btsp->mp = mp;
  2576. return 0;
  2577. }
  2578. /* descend/search its child page */
  2579. goto next;
  2580. }
  2581. if (cmp > 0) {
  2582. base = index + 1;
  2583. --lim;
  2584. }
  2585. }
  2586. /*
  2587. * search miss - non-leaf page:
  2588. *
  2589. * base is the smallest index with key (Kj) greater than
  2590. * search key (K) and may be zero or maxentry index.
  2591. * if base is non-zero, decrement base by one to get the parent
  2592. * entry of the child page to search.
  2593. */
  2594. index = base ? base - 1 : base;
  2595. /*
  2596. * go down to child page
  2597. */
  2598. next:
  2599. /* get the child page block number */
  2600. bn = addressXAD(&p->xad[index]);
  2601. /* unpin the parent page */
  2602. XT_PUTPAGE(mp);
  2603. }
  2604. }
  2605. /*
  2606. * xtRelink()
  2607. *
  2608. * function:
  2609. * link around a freed page.
  2610. *
  2611. * Parameter:
  2612. * int tid,
  2613. * struct inode *ip,
  2614. * xtpage_t *p)
  2615. *
  2616. * returns:
  2617. */
  2618. static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
  2619. {
  2620. int rc = 0;
  2621. struct metapage *mp;
  2622. s64 nextbn, prevbn;
  2623. struct tlock *tlck;
  2624. nextbn = le64_to_cpu(p->header.next);
  2625. prevbn = le64_to_cpu(p->header.prev);
  2626. /* update prev pointer of the next page */
  2627. if (nextbn != 0) {
  2628. XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
  2629. if (rc)
  2630. return rc;
  2631. /*
  2632. * acquire a transaction lock on the page;
  2633. *
  2634. * action: update prev pointer;
  2635. */
  2636. BT_MARK_DIRTY(mp, ip);
  2637. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2638. /* the page may already have been tlock'd */
  2639. p->header.prev = cpu_to_le64(prevbn);
  2640. XT_PUTPAGE(mp);
  2641. }
  2642. /* update next pointer of the previous page */
  2643. if (prevbn != 0) {
  2644. XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
  2645. if (rc)
  2646. return rc;
  2647. /*
  2648. * acquire a transaction lock on the page;
  2649. *
  2650. * action: update next pointer;
  2651. */
  2652. BT_MARK_DIRTY(mp, ip);
  2653. tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
  2654. /* the page may already have been tlock'd */
  2655. p->header.next = le64_to_cpu(nextbn);
  2656. XT_PUTPAGE(mp);
  2657. }
  2658. return 0;
  2659. }
  2660. #endif /* _STILL_TO_PORT */
  2661. /*
  2662. * xtInitRoot()
  2663. *
  2664. * initialize file root (inline in inode)
  2665. */
  2666. void xtInitRoot(tid_t tid, struct inode *ip)
  2667. {
  2668. xtpage_t *p;
  2669. /*
  2670. * acquire a transaction lock on the root
  2671. *
  2672. * action:
  2673. */
  2674. txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
  2675. tlckXTREE | tlckNEW);
  2676. p = &JFS_IP(ip)->i_xtroot;
  2677. p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
  2678. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2679. if (S_ISDIR(ip->i_mode))
  2680. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
  2681. else {
  2682. p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
  2683. ip->i_size = 0;
  2684. }
  2685. return;
  2686. }
  2687. /*
  2688. * We can run into a deadlock truncating a file with a large number of
  2689. * xtree pages (large fragmented file). A robust fix would entail a
  2690. * reservation system where we would reserve a number of metadata pages
  2691. * and tlocks which we would be guaranteed without a deadlock. Without
  2692. * this, a partial fix is to limit number of metadata pages we will lock
  2693. * in a single transaction. Currently we will truncate the file so that
  2694. * no more than 50 leaf pages will be locked. The caller of xtTruncate
  2695. * will be responsible for ensuring that the current transaction gets
  2696. * committed, and that subsequent transactions are created to truncate
  2697. * the file further if needed.
  2698. */
  2699. #define MAX_TRUNCATE_LEAVES 50
  2700. /*
  2701. * xtTruncate()
  2702. *
  2703. * function:
  2704. * traverse for truncation logging backward bottom up;
  2705. * terminate at the last extent entry at the current subtree
  2706. * root page covering new down size.
  2707. * truncation may occur within the last extent entry.
  2708. *
  2709. * parameter:
  2710. * int tid,
  2711. * struct inode *ip,
  2712. * s64 newsize,
  2713. * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
  2714. *
  2715. * return:
  2716. *
  2717. * note:
  2718. * PWMAP:
  2719. * 1. truncate (non-COMMIT_NOLINK file)
  2720. * by jfs_truncate() or jfs_open(O_TRUNC):
  2721. * xtree is updated;
  2722. * 2. truncate index table of directory when last entry removed
  2723. * map update via tlock at commit time;
  2724. * PMAP:
  2725. * Call xtTruncate_pmap instead
  2726. * WMAP:
  2727. * 1. remove (free zero link count) on last reference release
  2728. * (pmap has been freed at commit zero link count);
  2729. * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
  2730. * xtree is updated;
  2731. * map update directly at truncation time;
  2732. *
  2733. * if (DELETE)
  2734. * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
  2735. * else if (TRUNCATE)
  2736. * must write LOG_NOREDOPAGE for deleted index page;
  2737. *
  2738. * pages may already have been tlocked by anonymous transactions
  2739. * during file growth (i.e., write) before truncation;
  2740. *
  2741. * except last truncated entry, deleted entries remains as is
  2742. * in the page (nextindex is updated) for other use
  2743. * (e.g., log/update allocation map): this avoid copying the page
  2744. * info but delay free of pages;
  2745. *
  2746. */
  2747. s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
  2748. {
  2749. int rc = 0;
  2750. s64 teof;
  2751. struct metapage *mp;
  2752. xtpage_t *p;
  2753. s64 bn;
  2754. int index, nextindex;
  2755. xad_t *xad;
  2756. s64 xoff, xaddr;
  2757. int xlen, len, freexlen;
  2758. struct btstack btstack;
  2759. struct btframe *parent;
  2760. struct tblock *tblk = NULL;
  2761. struct tlock *tlck = NULL;
  2762. struct xtlock *xtlck = NULL;
  2763. struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
  2764. struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
  2765. s64 nfreed;
  2766. int freed, log;
  2767. int locked_leaves = 0;
  2768. /* save object truncation type */
  2769. if (tid) {
  2770. tblk = tid_to_tblock(tid);
  2771. tblk->xflag |= flag;
  2772. }
  2773. nfreed = 0;
  2774. flag &= COMMIT_MAP;
  2775. assert(flag != COMMIT_PMAP);
  2776. if (flag == COMMIT_PWMAP)
  2777. log = 1;
  2778. else {
  2779. log = 0;
  2780. xadlock.flag = mlckFREEXADLIST;
  2781. xadlock.index = 1;
  2782. }
  2783. /*
  2784. * if the newsize is not an integral number of pages,
  2785. * the file between newsize and next page boundary will
  2786. * be cleared.
  2787. * if truncating into a file hole, it will cause
  2788. * a full block to be allocated for the logical block.
  2789. */
  2790. /*
  2791. * release page blocks of truncated region <teof, eof>
  2792. *
  2793. * free the data blocks from the leaf index blocks.
  2794. * delete the parent index entries corresponding to
  2795. * the freed child data/index blocks.
  2796. * free the index blocks themselves which aren't needed
  2797. * in new sized file.
  2798. *
  2799. * index blocks are updated only if the blocks are to be
  2800. * retained in the new sized file.
  2801. * if type is PMAP, the data and index pages are NOT
  2802. * freed, and the data and index blocks are NOT freed
  2803. * from working map.
  2804. * (this will allow continued access of data/index of
  2805. * temporary file (zerolink count file truncated to zero-length)).
  2806. */
  2807. teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
  2808. JFS_SBI(ip->i_sb)->l2bsize;
  2809. /* clear stack */
  2810. BT_CLR(&btstack);
  2811. /*
  2812. * start with root
  2813. *
  2814. * root resides in the inode
  2815. */
  2816. bn = 0;
  2817. /*
  2818. * first access of each page:
  2819. */
  2820. getPage:
  2821. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  2822. if (rc)
  2823. return rc;
  2824. /* process entries backward from last index */
  2825. index = le16_to_cpu(p->header.nextindex) - 1;
  2826. /* Since this is the rightmost page at this level, and we may have
  2827. * already freed a page that was formerly to the right, let's make
  2828. * sure that the next pointer is zero.
  2829. */
  2830. if (p->header.next) {
  2831. if (log)
  2832. /*
  2833. * Make sure this change to the header is logged.
  2834. * If we really truncate this leaf, the flag
  2835. * will be changed to tlckTRUNCATE
  2836. */
  2837. tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
  2838. BT_MARK_DIRTY(mp, ip);
  2839. p->header.next = 0;
  2840. }
  2841. if (p->header.flag & BT_INTERNAL)
  2842. goto getChild;
  2843. /*
  2844. * leaf page
  2845. */
  2846. freed = 0;
  2847. /* does region covered by leaf page precede Teof ? */
  2848. xad = &p->xad[index];
  2849. xoff = offsetXAD(xad);
  2850. xlen = lengthXAD(xad);
  2851. if (teof >= xoff + xlen) {
  2852. XT_PUTPAGE(mp);
  2853. goto getParent;
  2854. }
  2855. /* (re)acquire tlock of the leaf page */
  2856. if (log) {
  2857. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  2858. /*
  2859. * We need to limit the size of the transaction
  2860. * to avoid exhausting pagecache & tlocks
  2861. */
  2862. XT_PUTPAGE(mp);
  2863. newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  2864. goto getParent;
  2865. }
  2866. tlck = txLock(tid, ip, mp, tlckXTREE);
  2867. tlck->type = tlckXTREE | tlckTRUNCATE;
  2868. xtlck = (struct xtlock *) & tlck->lock;
  2869. xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
  2870. }
  2871. BT_MARK_DIRTY(mp, ip);
  2872. /*
  2873. * scan backward leaf page entries
  2874. */
  2875. for (; index >= XTENTRYSTART; index--) {
  2876. xad = &p->xad[index];
  2877. xoff = offsetXAD(xad);
  2878. xlen = lengthXAD(xad);
  2879. xaddr = addressXAD(xad);
  2880. /*
  2881. * The "data" for a directory is indexed by the block
  2882. * device's address space. This metadata must be invalidated
  2883. * here
  2884. */
  2885. if (S_ISDIR(ip->i_mode) && (teof == 0))
  2886. invalidate_xad_metapages(ip, *xad);
  2887. /*
  2888. * entry beyond eof: continue scan of current page
  2889. * xad
  2890. * ---|---=======------->
  2891. * eof
  2892. */
  2893. if (teof < xoff) {
  2894. nfreed += xlen;
  2895. continue;
  2896. }
  2897. /*
  2898. * (xoff <= teof): last entry to be deleted from page;
  2899. * If other entries remain in page: keep and update the page.
  2900. */
  2901. /*
  2902. * eof == entry_start: delete the entry
  2903. * xad
  2904. * -------|=======------->
  2905. * eof
  2906. *
  2907. */
  2908. if (teof == xoff) {
  2909. nfreed += xlen;
  2910. if (index == XTENTRYSTART)
  2911. break;
  2912. nextindex = index;
  2913. }
  2914. /*
  2915. * eof within the entry: truncate the entry.
  2916. * xad
  2917. * -------===|===------->
  2918. * eof
  2919. */
  2920. else if (teof < xoff + xlen) {
  2921. /* update truncated entry */
  2922. len = teof - xoff;
  2923. freexlen = xlen - len;
  2924. XADlength(xad, len);
  2925. /* save pxd of truncated extent in tlck */
  2926. xaddr += len;
  2927. if (log) { /* COMMIT_PWMAP */
  2928. xtlck->lwm.offset = (xtlck->lwm.offset) ?
  2929. min(index, (int)xtlck->lwm.offset) : index;
  2930. xtlck->lwm.length = index + 1 -
  2931. xtlck->lwm.offset;
  2932. xtlck->twm.offset = index;
  2933. pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  2934. pxdlock->flag = mlckFREEPXD;
  2935. PXDaddress(&pxdlock->pxd, xaddr);
  2936. PXDlength(&pxdlock->pxd, freexlen);
  2937. }
  2938. /* free truncated extent */
  2939. else { /* COMMIT_WMAP */
  2940. pxdlock = (struct pxd_lock *) & xadlock;
  2941. pxdlock->flag = mlckFREEPXD;
  2942. PXDaddress(&pxdlock->pxd, xaddr);
  2943. PXDlength(&pxdlock->pxd, freexlen);
  2944. txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
  2945. /* reset map lock */
  2946. xadlock.flag = mlckFREEXADLIST;
  2947. }
  2948. /* current entry is new last entry; */
  2949. nextindex = index + 1;
  2950. nfreed += freexlen;
  2951. }
  2952. /*
  2953. * eof beyond the entry:
  2954. * xad
  2955. * -------=======---|--->
  2956. * eof
  2957. */
  2958. else { /* (xoff + xlen < teof) */
  2959. nextindex = index + 1;
  2960. }
  2961. if (nextindex < le16_to_cpu(p->header.nextindex)) {
  2962. if (!log) { /* COMMIT_WAMP */
  2963. xadlock.xdlist = &p->xad[nextindex];
  2964. xadlock.count =
  2965. le16_to_cpu(p->header.nextindex) -
  2966. nextindex;
  2967. txFreeMap(ip, (struct maplock *) & xadlock,
  2968. NULL, COMMIT_WMAP);
  2969. }
  2970. p->header.nextindex = cpu_to_le16(nextindex);
  2971. }
  2972. XT_PUTPAGE(mp);
  2973. /* assert(freed == 0); */
  2974. goto getParent;
  2975. } /* end scan of leaf page entries */
  2976. freed = 1;
  2977. /*
  2978. * leaf page become empty: free the page if type != PMAP
  2979. */
  2980. if (log) { /* COMMIT_PWMAP */
  2981. /* txCommit() with tlckFREE:
  2982. * free data extents covered by leaf [XTENTRYSTART:hwm);
  2983. * invalidate leaf if COMMIT_PWMAP;
  2984. * if (TRUNCATE), will write LOG_NOREDOPAGE;
  2985. */
  2986. tlck->type = tlckXTREE | tlckFREE;
  2987. } else { /* COMMIT_WAMP */
  2988. /* free data extents covered by leaf */
  2989. xadlock.xdlist = &p->xad[XTENTRYSTART];
  2990. xadlock.count =
  2991. le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
  2992. txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
  2993. }
  2994. if (p->header.flag & BT_ROOT) {
  2995. p->header.flag &= ~BT_INTERNAL;
  2996. p->header.flag |= BT_LEAF;
  2997. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  2998. XT_PUTPAGE(mp); /* debug */
  2999. goto out;
  3000. } else {
  3001. if (log) { /* COMMIT_PWMAP */
  3002. /* page will be invalidated at tx completion
  3003. */
  3004. XT_PUTPAGE(mp);
  3005. } else { /* COMMIT_WMAP */
  3006. if (mp->lid)
  3007. lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
  3008. /* invalidate empty leaf page */
  3009. discard_metapage(mp);
  3010. }
  3011. }
  3012. /*
  3013. * the leaf page become empty: delete the parent entry
  3014. * for the leaf page if the parent page is to be kept
  3015. * in the new sized file.
  3016. */
  3017. /*
  3018. * go back up to the parent page
  3019. */
  3020. getParent:
  3021. /* pop/restore parent entry for the current child page */
  3022. if ((parent = BT_POP(&btstack)) == NULL)
  3023. /* current page must have been root */
  3024. goto out;
  3025. /* get back the parent page */
  3026. bn = parent->bn;
  3027. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3028. if (rc)
  3029. return rc;
  3030. index = parent->index;
  3031. /*
  3032. * child page was not empty:
  3033. */
  3034. if (freed == 0) {
  3035. /* has any entry deleted from parent ? */
  3036. if (index < le16_to_cpu(p->header.nextindex) - 1) {
  3037. /* (re)acquire tlock on the parent page */
  3038. if (log) { /* COMMIT_PWMAP */
  3039. /* txCommit() with tlckTRUNCATE:
  3040. * free child extents covered by parent [);
  3041. */
  3042. tlck = txLock(tid, ip, mp, tlckXTREE);
  3043. xtlck = (struct xtlock *) & tlck->lock;
  3044. if (!(tlck->type & tlckTRUNCATE)) {
  3045. xtlck->hwm.offset =
  3046. le16_to_cpu(p->header.
  3047. nextindex) - 1;
  3048. tlck->type =
  3049. tlckXTREE | tlckTRUNCATE;
  3050. }
  3051. } else { /* COMMIT_WMAP */
  3052. /* free child extents covered by parent */
  3053. xadlock.xdlist = &p->xad[index + 1];
  3054. xadlock.count =
  3055. le16_to_cpu(p->header.nextindex) -
  3056. index - 1;
  3057. txFreeMap(ip, (struct maplock *) & xadlock,
  3058. NULL, COMMIT_WMAP);
  3059. }
  3060. BT_MARK_DIRTY(mp, ip);
  3061. p->header.nextindex = cpu_to_le16(index + 1);
  3062. }
  3063. XT_PUTPAGE(mp);
  3064. goto getParent;
  3065. }
  3066. /*
  3067. * child page was empty:
  3068. */
  3069. nfreed += lengthXAD(&p->xad[index]);
  3070. /*
  3071. * During working map update, child page's tlock must be handled
  3072. * before parent's. This is because the parent's tlock will cause
  3073. * the child's disk space to be marked available in the wmap, so
  3074. * it's important that the child page be released by that time.
  3075. *
  3076. * ToDo: tlocks should be on doubly-linked list, so we can
  3077. * quickly remove it and add it to the end.
  3078. */
  3079. /*
  3080. * Move parent page's tlock to the end of the tid's tlock list
  3081. */
  3082. if (log && mp->lid && (tblk->last != mp->lid) &&
  3083. lid_to_tlock(mp->lid)->tid) {
  3084. lid_t lid = mp->lid;
  3085. struct tlock *prev;
  3086. tlck = lid_to_tlock(lid);
  3087. if (tblk->next == lid)
  3088. tblk->next = tlck->next;
  3089. else {
  3090. for (prev = lid_to_tlock(tblk->next);
  3091. prev->next != lid;
  3092. prev = lid_to_tlock(prev->next)) {
  3093. assert(prev->next);
  3094. }
  3095. prev->next = tlck->next;
  3096. }
  3097. lid_to_tlock(tblk->last)->next = lid;
  3098. tlck->next = 0;
  3099. tblk->last = lid;
  3100. }
  3101. /*
  3102. * parent page become empty: free the page
  3103. */
  3104. if (index == XTENTRYSTART) {
  3105. if (log) { /* COMMIT_PWMAP */
  3106. /* txCommit() with tlckFREE:
  3107. * free child extents covered by parent;
  3108. * invalidate parent if COMMIT_PWMAP;
  3109. */
  3110. tlck = txLock(tid, ip, mp, tlckXTREE);
  3111. xtlck = (struct xtlock *) & tlck->lock;
  3112. xtlck->hwm.offset =
  3113. le16_to_cpu(p->header.nextindex) - 1;
  3114. tlck->type = tlckXTREE | tlckFREE;
  3115. } else { /* COMMIT_WMAP */
  3116. /* free child extents covered by parent */
  3117. xadlock.xdlist = &p->xad[XTENTRYSTART];
  3118. xadlock.count =
  3119. le16_to_cpu(p->header.nextindex) -
  3120. XTENTRYSTART;
  3121. txFreeMap(ip, (struct maplock *) & xadlock, NULL,
  3122. COMMIT_WMAP);
  3123. }
  3124. BT_MARK_DIRTY(mp, ip);
  3125. if (p->header.flag & BT_ROOT) {
  3126. p->header.flag &= ~BT_INTERNAL;
  3127. p->header.flag |= BT_LEAF;
  3128. p->header.nextindex = cpu_to_le16(XTENTRYSTART);
  3129. if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
  3130. /*
  3131. * Shrink root down to allow inline
  3132. * EA (otherwise fsck complains)
  3133. */
  3134. p->header.maxentry =
  3135. cpu_to_le16(XTROOTINITSLOT);
  3136. JFS_IP(ip)->mode2 |= INLINEEA;
  3137. }
  3138. XT_PUTPAGE(mp); /* debug */
  3139. goto out;
  3140. } else {
  3141. if (log) { /* COMMIT_PWMAP */
  3142. /* page will be invalidated at tx completion
  3143. */
  3144. XT_PUTPAGE(mp);
  3145. } else { /* COMMIT_WMAP */
  3146. if (mp->lid)
  3147. lid_to_tlock(mp->lid)->flag |=
  3148. tlckFREELOCK;
  3149. /* invalidate parent page */
  3150. discard_metapage(mp);
  3151. }
  3152. /* parent has become empty and freed:
  3153. * go back up to its parent page
  3154. */
  3155. /* freed = 1; */
  3156. goto getParent;
  3157. }
  3158. }
  3159. /*
  3160. * parent page still has entries for front region;
  3161. */
  3162. else {
  3163. /* try truncate region covered by preceding entry
  3164. * (process backward)
  3165. */
  3166. index--;
  3167. /* go back down to the child page corresponding
  3168. * to the entry
  3169. */
  3170. goto getChild;
  3171. }
  3172. /*
  3173. * internal page: go down to child page of current entry
  3174. */
  3175. getChild:
  3176. /* save current parent entry for the child page */
  3177. if (BT_STACK_FULL(&btstack)) {
  3178. jfs_error(ip->i_sb, "stack overrun!\n");
  3179. XT_PUTPAGE(mp);
  3180. return -EIO;
  3181. }
  3182. BT_PUSH(&btstack, bn, index);
  3183. /* get child page */
  3184. xad = &p->xad[index];
  3185. bn = addressXAD(xad);
  3186. /*
  3187. * first access of each internal entry:
  3188. */
  3189. /* release parent page */
  3190. XT_PUTPAGE(mp);
  3191. /* process the child page */
  3192. goto getPage;
  3193. out:
  3194. /*
  3195. * update file resource stat
  3196. */
  3197. /* set size
  3198. */
  3199. if (S_ISDIR(ip->i_mode) && !newsize)
  3200. ip->i_size = 1; /* fsck hates zero-length directories */
  3201. else
  3202. ip->i_size = newsize;
  3203. /* update quota allocation to reflect freed blocks */
  3204. dquot_free_block(ip, nfreed);
  3205. /*
  3206. * free tlock of invalidated pages
  3207. */
  3208. if (flag == COMMIT_WMAP)
  3209. txFreelock(ip);
  3210. return newsize;
  3211. }
  3212. /*
  3213. * xtTruncate_pmap()
  3214. *
  3215. * function:
  3216. * Perform truncate to zero length for deleted file, leaving the
  3217. * the xtree and working map untouched. This allows the file to
  3218. * be accessed via open file handles, while the delete of the file
  3219. * is committed to disk.
  3220. *
  3221. * parameter:
  3222. * tid_t tid,
  3223. * struct inode *ip,
  3224. * s64 committed_size)
  3225. *
  3226. * return: new committed size
  3227. *
  3228. * note:
  3229. *
  3230. * To avoid deadlock by holding too many transaction locks, the
  3231. * truncation may be broken up into multiple transactions.
  3232. * The committed_size keeps track of part of the file has been
  3233. * freed from the pmaps.
  3234. */
  3235. s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
  3236. {
  3237. s64 bn;
  3238. struct btstack btstack;
  3239. int cmp;
  3240. int index;
  3241. int locked_leaves = 0;
  3242. struct metapage *mp;
  3243. xtpage_t *p;
  3244. struct btframe *parent;
  3245. int rc;
  3246. struct tblock *tblk;
  3247. struct tlock *tlck = NULL;
  3248. xad_t *xad;
  3249. int xlen;
  3250. s64 xoff;
  3251. struct xtlock *xtlck = NULL;
  3252. /* save object truncation type */
  3253. tblk = tid_to_tblock(tid);
  3254. tblk->xflag |= COMMIT_PMAP;
  3255. /* clear stack */
  3256. BT_CLR(&btstack);
  3257. if (committed_size) {
  3258. xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
  3259. rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
  3260. if (rc)
  3261. return rc;
  3262. XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
  3263. if (cmp != 0) {
  3264. XT_PUTPAGE(mp);
  3265. jfs_error(ip->i_sb, "did not find extent\n");
  3266. return -EIO;
  3267. }
  3268. } else {
  3269. /*
  3270. * start with root
  3271. *
  3272. * root resides in the inode
  3273. */
  3274. bn = 0;
  3275. /*
  3276. * first access of each page:
  3277. */
  3278. getPage:
  3279. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3280. if (rc)
  3281. return rc;
  3282. /* process entries backward from last index */
  3283. index = le16_to_cpu(p->header.nextindex) - 1;
  3284. if (p->header.flag & BT_INTERNAL)
  3285. goto getChild;
  3286. }
  3287. /*
  3288. * leaf page
  3289. */
  3290. if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
  3291. /*
  3292. * We need to limit the size of the transaction
  3293. * to avoid exhausting pagecache & tlocks
  3294. */
  3295. xad = &p->xad[index];
  3296. xoff = offsetXAD(xad);
  3297. xlen = lengthXAD(xad);
  3298. XT_PUTPAGE(mp);
  3299. return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
  3300. }
  3301. tlck = txLock(tid, ip, mp, tlckXTREE);
  3302. tlck->type = tlckXTREE | tlckFREE;
  3303. xtlck = (struct xtlock *) & tlck->lock;
  3304. xtlck->hwm.offset = index;
  3305. XT_PUTPAGE(mp);
  3306. /*
  3307. * go back up to the parent page
  3308. */
  3309. getParent:
  3310. /* pop/restore parent entry for the current child page */
  3311. if ((parent = BT_POP(&btstack)) == NULL)
  3312. /* current page must have been root */
  3313. goto out;
  3314. /* get back the parent page */
  3315. bn = parent->bn;
  3316. XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
  3317. if (rc)
  3318. return rc;
  3319. index = parent->index;
  3320. /*
  3321. * parent page become empty: free the page
  3322. */
  3323. if (index == XTENTRYSTART) {
  3324. /* txCommit() with tlckFREE:
  3325. * free child extents covered by parent;
  3326. * invalidate parent if COMMIT_PWMAP;
  3327. */
  3328. tlck = txLock(tid, ip, mp, tlckXTREE);
  3329. xtlck = (struct xtlock *) & tlck->lock;
  3330. xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
  3331. tlck->type = tlckXTREE | tlckFREE;
  3332. XT_PUTPAGE(mp);
  3333. if (p->header.flag & BT_ROOT) {
  3334. goto out;
  3335. } else {
  3336. goto getParent;
  3337. }
  3338. }
  3339. /*
  3340. * parent page still has entries for front region;
  3341. */
  3342. else
  3343. index--;
  3344. /*
  3345. * internal page: go down to child page of current entry
  3346. */
  3347. getChild:
  3348. /* save current parent entry for the child page */
  3349. if (BT_STACK_FULL(&btstack)) {
  3350. jfs_error(ip->i_sb, "stack overrun!\n");
  3351. XT_PUTPAGE(mp);
  3352. return -EIO;
  3353. }
  3354. BT_PUSH(&btstack, bn, index);
  3355. /* get child page */
  3356. xad = &p->xad[index];
  3357. bn = addressXAD(xad);
  3358. /*
  3359. * first access of each internal entry:
  3360. */
  3361. /* release parent page */
  3362. XT_PUTPAGE(mp);
  3363. /* process the child page */
  3364. goto getPage;
  3365. out:
  3366. return 0;
  3367. }
  3368. #ifdef CONFIG_JFS_STATISTICS
  3369. static int jfs_xtstat_proc_show(struct seq_file *m, void *v)
  3370. {
  3371. seq_printf(m,
  3372. "JFS Xtree statistics\n"
  3373. "====================\n"
  3374. "searches = %d\n"
  3375. "fast searches = %d\n"
  3376. "splits = %d\n",
  3377. xtStat.search,
  3378. xtStat.fastSearch,
  3379. xtStat.split);
  3380. return 0;
  3381. }
  3382. static int jfs_xtstat_proc_open(struct inode *inode, struct file *file)
  3383. {
  3384. return single_open(file, jfs_xtstat_proc_show, NULL);
  3385. }
  3386. const struct file_operations jfs_xtstat_proc_fops = {
  3387. .owner = THIS_MODULE,
  3388. .open = jfs_xtstat_proc_open,
  3389. .read = seq_read,
  3390. .llseek = seq_lseek,
  3391. .release = single_release,
  3392. };
  3393. #endif