jfs_xtree.c 98 KB

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