jfs_xtree.c 97 KB

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