jfs_xtree.c 92 KB

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