jfs_xtree.c 97 KB

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