refcounttree.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * refcounttree.c
  5. *
  6. * Copyright (C) 2009 Oracle. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/sort.h>
  18. #define MLOG_MASK_PREFIX ML_REFCOUNT
  19. #include <cluster/masklog.h>
  20. #include "ocfs2.h"
  21. #include "inode.h"
  22. #include "alloc.h"
  23. #include "suballoc.h"
  24. #include "journal.h"
  25. #include "uptodate.h"
  26. #include "super.h"
  27. #include "buffer_head_io.h"
  28. #include "blockcheck.h"
  29. #include "refcounttree.h"
  30. #include "sysfile.h"
  31. #include "dlmglue.h"
  32. #include "extent_map.h"
  33. #include "aops.h"
  34. #include "xattr.h"
  35. #include <linux/bio.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/gfp.h>
  38. #include <linux/slab.h>
  39. #include <linux/writeback.h>
  40. #include <linux/pagevec.h>
  41. #include <linux/swap.h>
  42. struct ocfs2_cow_context {
  43. struct inode *inode;
  44. u32 cow_start;
  45. u32 cow_len;
  46. struct ocfs2_extent_tree data_et;
  47. struct ocfs2_refcount_tree *ref_tree;
  48. struct buffer_head *ref_root_bh;
  49. struct ocfs2_alloc_context *meta_ac;
  50. struct ocfs2_alloc_context *data_ac;
  51. struct ocfs2_cached_dealloc_ctxt dealloc;
  52. void *cow_object;
  53. struct ocfs2_post_refcount *post_refcount;
  54. int extra_credits;
  55. int (*get_clusters)(struct ocfs2_cow_context *context,
  56. u32 v_cluster, u32 *p_cluster,
  57. u32 *num_clusters,
  58. unsigned int *extent_flags);
  59. int (*cow_duplicate_clusters)(handle_t *handle,
  60. struct ocfs2_cow_context *context,
  61. u32 cpos, u32 old_cluster,
  62. u32 new_cluster, u32 new_len);
  63. };
  64. static inline struct ocfs2_refcount_tree *
  65. cache_info_to_refcount(struct ocfs2_caching_info *ci)
  66. {
  67. return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
  68. }
  69. static int ocfs2_validate_refcount_block(struct super_block *sb,
  70. struct buffer_head *bh)
  71. {
  72. int rc;
  73. struct ocfs2_refcount_block *rb =
  74. (struct ocfs2_refcount_block *)bh->b_data;
  75. mlog(0, "Validating refcount block %llu\n",
  76. (unsigned long long)bh->b_blocknr);
  77. BUG_ON(!buffer_uptodate(bh));
  78. /*
  79. * If the ecc fails, we return the error but otherwise
  80. * leave the filesystem running. We know any error is
  81. * local to this block.
  82. */
  83. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
  84. if (rc) {
  85. mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
  86. (unsigned long long)bh->b_blocknr);
  87. return rc;
  88. }
  89. if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
  90. ocfs2_error(sb,
  91. "Refcount block #%llu has bad signature %.*s",
  92. (unsigned long long)bh->b_blocknr, 7,
  93. rb->rf_signature);
  94. return -EINVAL;
  95. }
  96. if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
  97. ocfs2_error(sb,
  98. "Refcount block #%llu has an invalid rf_blkno "
  99. "of %llu",
  100. (unsigned long long)bh->b_blocknr,
  101. (unsigned long long)le64_to_cpu(rb->rf_blkno));
  102. return -EINVAL;
  103. }
  104. if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  105. ocfs2_error(sb,
  106. "Refcount block #%llu has an invalid "
  107. "rf_fs_generation of #%u",
  108. (unsigned long long)bh->b_blocknr,
  109. le32_to_cpu(rb->rf_fs_generation));
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
  115. u64 rb_blkno,
  116. struct buffer_head **bh)
  117. {
  118. int rc;
  119. struct buffer_head *tmp = *bh;
  120. rc = ocfs2_read_block(ci, rb_blkno, &tmp,
  121. ocfs2_validate_refcount_block);
  122. /* If ocfs2_read_block() got us a new bh, pass it up. */
  123. if (!rc && !*bh)
  124. *bh = tmp;
  125. return rc;
  126. }
  127. static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
  128. {
  129. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  130. return rf->rf_blkno;
  131. }
  132. static struct super_block *
  133. ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
  134. {
  135. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  136. return rf->rf_sb;
  137. }
  138. static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
  139. {
  140. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  141. spin_lock(&rf->rf_lock);
  142. }
  143. static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
  144. {
  145. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  146. spin_unlock(&rf->rf_lock);
  147. }
  148. static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
  149. {
  150. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  151. mutex_lock(&rf->rf_io_mutex);
  152. }
  153. static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
  154. {
  155. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  156. mutex_unlock(&rf->rf_io_mutex);
  157. }
  158. static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
  159. .co_owner = ocfs2_refcount_cache_owner,
  160. .co_get_super = ocfs2_refcount_cache_get_super,
  161. .co_cache_lock = ocfs2_refcount_cache_lock,
  162. .co_cache_unlock = ocfs2_refcount_cache_unlock,
  163. .co_io_lock = ocfs2_refcount_cache_io_lock,
  164. .co_io_unlock = ocfs2_refcount_cache_io_unlock,
  165. };
  166. static struct ocfs2_refcount_tree *
  167. ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
  168. {
  169. struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
  170. struct ocfs2_refcount_tree *tree = NULL;
  171. while (n) {
  172. tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
  173. if (blkno < tree->rf_blkno)
  174. n = n->rb_left;
  175. else if (blkno > tree->rf_blkno)
  176. n = n->rb_right;
  177. else
  178. return tree;
  179. }
  180. return NULL;
  181. }
  182. /* osb_lock is already locked. */
  183. static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
  184. struct ocfs2_refcount_tree *new)
  185. {
  186. u64 rf_blkno = new->rf_blkno;
  187. struct rb_node *parent = NULL;
  188. struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
  189. struct ocfs2_refcount_tree *tmp;
  190. while (*p) {
  191. parent = *p;
  192. tmp = rb_entry(parent, struct ocfs2_refcount_tree,
  193. rf_node);
  194. if (rf_blkno < tmp->rf_blkno)
  195. p = &(*p)->rb_left;
  196. else if (rf_blkno > tmp->rf_blkno)
  197. p = &(*p)->rb_right;
  198. else {
  199. /* This should never happen! */
  200. mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
  201. (unsigned long long)rf_blkno);
  202. BUG();
  203. }
  204. }
  205. rb_link_node(&new->rf_node, parent, p);
  206. rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
  207. }
  208. static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
  209. {
  210. ocfs2_metadata_cache_exit(&tree->rf_ci);
  211. ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
  212. ocfs2_lock_res_free(&tree->rf_lockres);
  213. kfree(tree);
  214. }
  215. static inline void
  216. ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
  217. struct ocfs2_refcount_tree *tree)
  218. {
  219. rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
  220. if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
  221. osb->osb_ref_tree_lru = NULL;
  222. }
  223. static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
  224. struct ocfs2_refcount_tree *tree)
  225. {
  226. spin_lock(&osb->osb_lock);
  227. ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
  228. spin_unlock(&osb->osb_lock);
  229. }
  230. void ocfs2_kref_remove_refcount_tree(struct kref *kref)
  231. {
  232. struct ocfs2_refcount_tree *tree =
  233. container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
  234. ocfs2_free_refcount_tree(tree);
  235. }
  236. static inline void
  237. ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
  238. {
  239. kref_get(&tree->rf_getcnt);
  240. }
  241. static inline void
  242. ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
  243. {
  244. kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
  245. }
  246. static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
  247. struct super_block *sb)
  248. {
  249. ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
  250. mutex_init(&new->rf_io_mutex);
  251. new->rf_sb = sb;
  252. spin_lock_init(&new->rf_lock);
  253. }
  254. static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
  255. struct ocfs2_refcount_tree *new,
  256. u64 rf_blkno, u32 generation)
  257. {
  258. init_rwsem(&new->rf_sem);
  259. ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
  260. rf_blkno, generation);
  261. }
  262. static struct ocfs2_refcount_tree*
  263. ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
  264. {
  265. struct ocfs2_refcount_tree *new;
  266. new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
  267. if (!new)
  268. return NULL;
  269. new->rf_blkno = rf_blkno;
  270. kref_init(&new->rf_getcnt);
  271. ocfs2_init_refcount_tree_ci(new, osb->sb);
  272. return new;
  273. }
  274. static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
  275. struct ocfs2_refcount_tree **ret_tree)
  276. {
  277. int ret = 0;
  278. struct ocfs2_refcount_tree *tree, *new = NULL;
  279. struct buffer_head *ref_root_bh = NULL;
  280. struct ocfs2_refcount_block *ref_rb;
  281. spin_lock(&osb->osb_lock);
  282. if (osb->osb_ref_tree_lru &&
  283. osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
  284. tree = osb->osb_ref_tree_lru;
  285. else
  286. tree = ocfs2_find_refcount_tree(osb, rf_blkno);
  287. if (tree)
  288. goto out;
  289. spin_unlock(&osb->osb_lock);
  290. new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
  291. if (!new) {
  292. ret = -ENOMEM;
  293. mlog_errno(ret);
  294. return ret;
  295. }
  296. /*
  297. * We need the generation to create the refcount tree lock and since
  298. * it isn't changed during the tree modification, we are safe here to
  299. * read without protection.
  300. * We also have to purge the cache after we create the lock since the
  301. * refcount block may have the stale data. It can only be trusted when
  302. * we hold the refcount lock.
  303. */
  304. ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
  305. if (ret) {
  306. mlog_errno(ret);
  307. ocfs2_metadata_cache_exit(&new->rf_ci);
  308. kfree(new);
  309. return ret;
  310. }
  311. ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  312. new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
  313. ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
  314. new->rf_generation);
  315. ocfs2_metadata_cache_purge(&new->rf_ci);
  316. spin_lock(&osb->osb_lock);
  317. tree = ocfs2_find_refcount_tree(osb, rf_blkno);
  318. if (tree)
  319. goto out;
  320. ocfs2_insert_refcount_tree(osb, new);
  321. tree = new;
  322. new = NULL;
  323. out:
  324. *ret_tree = tree;
  325. osb->osb_ref_tree_lru = tree;
  326. spin_unlock(&osb->osb_lock);
  327. if (new)
  328. ocfs2_free_refcount_tree(new);
  329. brelse(ref_root_bh);
  330. return ret;
  331. }
  332. static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
  333. {
  334. int ret;
  335. struct buffer_head *di_bh = NULL;
  336. struct ocfs2_dinode *di;
  337. ret = ocfs2_read_inode_block(inode, &di_bh);
  338. if (ret) {
  339. mlog_errno(ret);
  340. goto out;
  341. }
  342. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  343. di = (struct ocfs2_dinode *)di_bh->b_data;
  344. *ref_blkno = le64_to_cpu(di->i_refcount_loc);
  345. brelse(di_bh);
  346. out:
  347. return ret;
  348. }
  349. static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
  350. struct ocfs2_refcount_tree *tree, int rw)
  351. {
  352. int ret;
  353. ret = ocfs2_refcount_lock(tree, rw);
  354. if (ret) {
  355. mlog_errno(ret);
  356. goto out;
  357. }
  358. if (rw)
  359. down_write(&tree->rf_sem);
  360. else
  361. down_read(&tree->rf_sem);
  362. out:
  363. return ret;
  364. }
  365. /*
  366. * Lock the refcount tree pointed by ref_blkno and return the tree.
  367. * In most case, we lock the tree and read the refcount block.
  368. * So read it here if the caller really needs it.
  369. *
  370. * If the tree has been re-created by other node, it will free the
  371. * old one and re-create it.
  372. */
  373. int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
  374. u64 ref_blkno, int rw,
  375. struct ocfs2_refcount_tree **ret_tree,
  376. struct buffer_head **ref_bh)
  377. {
  378. int ret, delete_tree = 0;
  379. struct ocfs2_refcount_tree *tree = NULL;
  380. struct buffer_head *ref_root_bh = NULL;
  381. struct ocfs2_refcount_block *rb;
  382. again:
  383. ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
  384. if (ret) {
  385. mlog_errno(ret);
  386. return ret;
  387. }
  388. ocfs2_refcount_tree_get(tree);
  389. ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
  390. if (ret) {
  391. mlog_errno(ret);
  392. ocfs2_refcount_tree_put(tree);
  393. goto out;
  394. }
  395. ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
  396. &ref_root_bh);
  397. if (ret) {
  398. mlog_errno(ret);
  399. ocfs2_unlock_refcount_tree(osb, tree, rw);
  400. ocfs2_refcount_tree_put(tree);
  401. goto out;
  402. }
  403. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  404. /*
  405. * If the refcount block has been freed and re-created, we may need
  406. * to recreate the refcount tree also.
  407. *
  408. * Here we just remove the tree from the rb-tree, and the last
  409. * kref holder will unlock and delete this refcount_tree.
  410. * Then we goto "again" and ocfs2_get_refcount_tree will create
  411. * the new refcount tree for us.
  412. */
  413. if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
  414. if (!tree->rf_removed) {
  415. ocfs2_erase_refcount_tree_from_list(osb, tree);
  416. tree->rf_removed = 1;
  417. delete_tree = 1;
  418. }
  419. ocfs2_unlock_refcount_tree(osb, tree, rw);
  420. /*
  421. * We get an extra reference when we create the refcount
  422. * tree, so another put will destroy it.
  423. */
  424. if (delete_tree)
  425. ocfs2_refcount_tree_put(tree);
  426. brelse(ref_root_bh);
  427. ref_root_bh = NULL;
  428. goto again;
  429. }
  430. *ret_tree = tree;
  431. if (ref_bh) {
  432. *ref_bh = ref_root_bh;
  433. ref_root_bh = NULL;
  434. }
  435. out:
  436. brelse(ref_root_bh);
  437. return ret;
  438. }
  439. int ocfs2_lock_refcount_tree_by_inode(struct inode *inode, int rw,
  440. struct ocfs2_refcount_tree **ret_tree,
  441. struct buffer_head **ref_bh)
  442. {
  443. int ret;
  444. u64 ref_blkno;
  445. ret = ocfs2_get_refcount_block(inode, &ref_blkno);
  446. if (ret) {
  447. mlog_errno(ret);
  448. return ret;
  449. }
  450. return ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno,
  451. rw, ret_tree, ref_bh);
  452. }
  453. void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
  454. struct ocfs2_refcount_tree *tree, int rw)
  455. {
  456. if (rw)
  457. up_write(&tree->rf_sem);
  458. else
  459. up_read(&tree->rf_sem);
  460. ocfs2_refcount_unlock(tree, rw);
  461. ocfs2_refcount_tree_put(tree);
  462. }
  463. void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
  464. {
  465. struct rb_node *node;
  466. struct ocfs2_refcount_tree *tree;
  467. struct rb_root *root = &osb->osb_rf_lock_tree;
  468. while ((node = rb_last(root)) != NULL) {
  469. tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
  470. mlog(0, "Purge tree %llu\n",
  471. (unsigned long long) tree->rf_blkno);
  472. rb_erase(&tree->rf_node, root);
  473. ocfs2_free_refcount_tree(tree);
  474. }
  475. }
  476. /*
  477. * Create a refcount tree for an inode.
  478. * We take for granted that the inode is already locked.
  479. */
  480. static int ocfs2_create_refcount_tree(struct inode *inode,
  481. struct buffer_head *di_bh)
  482. {
  483. int ret;
  484. handle_t *handle = NULL;
  485. struct ocfs2_alloc_context *meta_ac = NULL;
  486. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  487. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  488. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  489. struct buffer_head *new_bh = NULL;
  490. struct ocfs2_refcount_block *rb;
  491. struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
  492. u16 suballoc_bit_start;
  493. u32 num_got;
  494. u64 first_blkno;
  495. BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
  496. mlog(0, "create tree for inode %lu\n", inode->i_ino);
  497. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  498. if (ret) {
  499. mlog_errno(ret);
  500. goto out;
  501. }
  502. handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
  503. if (IS_ERR(handle)) {
  504. ret = PTR_ERR(handle);
  505. mlog_errno(ret);
  506. goto out;
  507. }
  508. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  509. OCFS2_JOURNAL_ACCESS_WRITE);
  510. if (ret) {
  511. mlog_errno(ret);
  512. goto out_commit;
  513. }
  514. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  515. &suballoc_bit_start, &num_got,
  516. &first_blkno);
  517. if (ret) {
  518. mlog_errno(ret);
  519. goto out_commit;
  520. }
  521. new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
  522. if (!new_tree) {
  523. ret = -ENOMEM;
  524. mlog_errno(ret);
  525. goto out_commit;
  526. }
  527. new_bh = sb_getblk(inode->i_sb, first_blkno);
  528. ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
  529. ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
  530. OCFS2_JOURNAL_ACCESS_CREATE);
  531. if (ret) {
  532. mlog_errno(ret);
  533. goto out_commit;
  534. }
  535. /* Initialize ocfs2_refcount_block. */
  536. rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  537. memset(rb, 0, inode->i_sb->s_blocksize);
  538. strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
  539. rb->rf_suballoc_slot = cpu_to_le16(osb->slot_num);
  540. rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  541. rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
  542. rb->rf_blkno = cpu_to_le64(first_blkno);
  543. rb->rf_count = cpu_to_le32(1);
  544. rb->rf_records.rl_count =
  545. cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
  546. spin_lock(&osb->osb_lock);
  547. rb->rf_generation = osb->s_next_generation++;
  548. spin_unlock(&osb->osb_lock);
  549. ocfs2_journal_dirty(handle, new_bh);
  550. spin_lock(&oi->ip_lock);
  551. oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
  552. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  553. di->i_refcount_loc = cpu_to_le64(first_blkno);
  554. spin_unlock(&oi->ip_lock);
  555. mlog(0, "created tree for inode %lu, refblock %llu\n",
  556. inode->i_ino, (unsigned long long)first_blkno);
  557. ocfs2_journal_dirty(handle, di_bh);
  558. /*
  559. * We have to init the tree lock here since it will use
  560. * the generation number to create it.
  561. */
  562. new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
  563. ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
  564. new_tree->rf_generation);
  565. spin_lock(&osb->osb_lock);
  566. tree = ocfs2_find_refcount_tree(osb, first_blkno);
  567. /*
  568. * We've just created a new refcount tree in this block. If
  569. * we found a refcount tree on the ocfs2_super, it must be
  570. * one we just deleted. We free the old tree before
  571. * inserting the new tree.
  572. */
  573. BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
  574. if (tree)
  575. ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
  576. ocfs2_insert_refcount_tree(osb, new_tree);
  577. spin_unlock(&osb->osb_lock);
  578. new_tree = NULL;
  579. if (tree)
  580. ocfs2_refcount_tree_put(tree);
  581. out_commit:
  582. ocfs2_commit_trans(osb, handle);
  583. out:
  584. if (new_tree) {
  585. ocfs2_metadata_cache_exit(&new_tree->rf_ci);
  586. kfree(new_tree);
  587. }
  588. brelse(new_bh);
  589. if (meta_ac)
  590. ocfs2_free_alloc_context(meta_ac);
  591. return ret;
  592. }
  593. static int ocfs2_set_refcount_tree(struct inode *inode,
  594. struct buffer_head *di_bh,
  595. u64 refcount_loc)
  596. {
  597. int ret;
  598. handle_t *handle = NULL;
  599. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  600. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  601. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  602. struct buffer_head *ref_root_bh = NULL;
  603. struct ocfs2_refcount_block *rb;
  604. struct ocfs2_refcount_tree *ref_tree;
  605. BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
  606. ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
  607. &ref_tree, &ref_root_bh);
  608. if (ret) {
  609. mlog_errno(ret);
  610. return ret;
  611. }
  612. handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
  613. if (IS_ERR(handle)) {
  614. ret = PTR_ERR(handle);
  615. mlog_errno(ret);
  616. goto out;
  617. }
  618. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  619. OCFS2_JOURNAL_ACCESS_WRITE);
  620. if (ret) {
  621. mlog_errno(ret);
  622. goto out_commit;
  623. }
  624. ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
  625. OCFS2_JOURNAL_ACCESS_WRITE);
  626. if (ret) {
  627. mlog_errno(ret);
  628. goto out_commit;
  629. }
  630. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  631. le32_add_cpu(&rb->rf_count, 1);
  632. ocfs2_journal_dirty(handle, ref_root_bh);
  633. spin_lock(&oi->ip_lock);
  634. oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
  635. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  636. di->i_refcount_loc = cpu_to_le64(refcount_loc);
  637. spin_unlock(&oi->ip_lock);
  638. ocfs2_journal_dirty(handle, di_bh);
  639. out_commit:
  640. ocfs2_commit_trans(osb, handle);
  641. out:
  642. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  643. brelse(ref_root_bh);
  644. return ret;
  645. }
  646. int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
  647. {
  648. int ret, delete_tree = 0;
  649. handle_t *handle = NULL;
  650. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  651. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  652. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  653. struct ocfs2_refcount_block *rb;
  654. struct inode *alloc_inode = NULL;
  655. struct buffer_head *alloc_bh = NULL;
  656. struct buffer_head *blk_bh = NULL;
  657. struct ocfs2_refcount_tree *ref_tree;
  658. int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
  659. u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
  660. u16 bit = 0;
  661. if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
  662. return 0;
  663. BUG_ON(!ref_blkno);
  664. ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
  665. if (ret) {
  666. mlog_errno(ret);
  667. return ret;
  668. }
  669. rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
  670. /*
  671. * If we are the last user, we need to free the block.
  672. * So lock the allocator ahead.
  673. */
  674. if (le32_to_cpu(rb->rf_count) == 1) {
  675. blk = le64_to_cpu(rb->rf_blkno);
  676. bit = le16_to_cpu(rb->rf_suballoc_bit);
  677. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  678. alloc_inode = ocfs2_get_system_file_inode(osb,
  679. EXTENT_ALLOC_SYSTEM_INODE,
  680. le16_to_cpu(rb->rf_suballoc_slot));
  681. if (!alloc_inode) {
  682. ret = -ENOMEM;
  683. mlog_errno(ret);
  684. goto out;
  685. }
  686. mutex_lock(&alloc_inode->i_mutex);
  687. ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
  688. if (ret) {
  689. mlog_errno(ret);
  690. goto out_mutex;
  691. }
  692. credits += OCFS2_SUBALLOC_FREE;
  693. }
  694. handle = ocfs2_start_trans(osb, credits);
  695. if (IS_ERR(handle)) {
  696. ret = PTR_ERR(handle);
  697. mlog_errno(ret);
  698. goto out_unlock;
  699. }
  700. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  701. OCFS2_JOURNAL_ACCESS_WRITE);
  702. if (ret) {
  703. mlog_errno(ret);
  704. goto out_commit;
  705. }
  706. ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
  707. OCFS2_JOURNAL_ACCESS_WRITE);
  708. if (ret) {
  709. mlog_errno(ret);
  710. goto out_commit;
  711. }
  712. spin_lock(&oi->ip_lock);
  713. oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
  714. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  715. di->i_refcount_loc = 0;
  716. spin_unlock(&oi->ip_lock);
  717. ocfs2_journal_dirty(handle, di_bh);
  718. le32_add_cpu(&rb->rf_count , -1);
  719. ocfs2_journal_dirty(handle, blk_bh);
  720. if (!rb->rf_count) {
  721. delete_tree = 1;
  722. ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
  723. ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
  724. alloc_bh, bit, bg_blkno, 1);
  725. if (ret)
  726. mlog_errno(ret);
  727. }
  728. out_commit:
  729. ocfs2_commit_trans(osb, handle);
  730. out_unlock:
  731. if (alloc_inode) {
  732. ocfs2_inode_unlock(alloc_inode, 1);
  733. brelse(alloc_bh);
  734. }
  735. out_mutex:
  736. if (alloc_inode) {
  737. mutex_unlock(&alloc_inode->i_mutex);
  738. iput(alloc_inode);
  739. }
  740. out:
  741. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  742. if (delete_tree)
  743. ocfs2_refcount_tree_put(ref_tree);
  744. brelse(blk_bh);
  745. return ret;
  746. }
  747. static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
  748. struct buffer_head *ref_leaf_bh,
  749. u64 cpos, unsigned int len,
  750. struct ocfs2_refcount_rec *ret_rec,
  751. int *index)
  752. {
  753. int i = 0;
  754. struct ocfs2_refcount_block *rb =
  755. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  756. struct ocfs2_refcount_rec *rec = NULL;
  757. for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
  758. rec = &rb->rf_records.rl_recs[i];
  759. if (le64_to_cpu(rec->r_cpos) +
  760. le32_to_cpu(rec->r_clusters) <= cpos)
  761. continue;
  762. else if (le64_to_cpu(rec->r_cpos) > cpos)
  763. break;
  764. /* ok, cpos fail in this rec. Just return. */
  765. if (ret_rec)
  766. *ret_rec = *rec;
  767. goto out;
  768. }
  769. if (ret_rec) {
  770. /* We meet with a hole here, so fake the rec. */
  771. ret_rec->r_cpos = cpu_to_le64(cpos);
  772. ret_rec->r_refcount = 0;
  773. if (i < le16_to_cpu(rb->rf_records.rl_used) &&
  774. le64_to_cpu(rec->r_cpos) < cpos + len)
  775. ret_rec->r_clusters =
  776. cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
  777. else
  778. ret_rec->r_clusters = cpu_to_le32(len);
  779. }
  780. out:
  781. *index = i;
  782. }
  783. /*
  784. * Try to remove refcount tree. The mechanism is:
  785. * 1) Check whether i_clusters == 0, if no, exit.
  786. * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
  787. * 3) Check whether we have inline xattr stored outside, if yes, exit.
  788. * 4) Remove the tree.
  789. */
  790. int ocfs2_try_remove_refcount_tree(struct inode *inode,
  791. struct buffer_head *di_bh)
  792. {
  793. int ret;
  794. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  795. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  796. down_write(&oi->ip_xattr_sem);
  797. down_write(&oi->ip_alloc_sem);
  798. if (oi->ip_clusters)
  799. goto out;
  800. if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
  801. goto out;
  802. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
  803. ocfs2_has_inline_xattr_value_outside(inode, di))
  804. goto out;
  805. ret = ocfs2_remove_refcount_tree(inode, di_bh);
  806. if (ret)
  807. mlog_errno(ret);
  808. out:
  809. up_write(&oi->ip_alloc_sem);
  810. up_write(&oi->ip_xattr_sem);
  811. return 0;
  812. }
  813. /*
  814. * Given a cpos and len, try to find the refcount record which contains cpos.
  815. * 1. If cpos can be found in one refcount record, return the record.
  816. * 2. If cpos can't be found, return a fake record which start from cpos
  817. * and end at a small value between cpos+len and start of the next record.
  818. * This fake record has r_refcount = 0.
  819. */
  820. static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
  821. struct buffer_head *ref_root_bh,
  822. u64 cpos, unsigned int len,
  823. struct ocfs2_refcount_rec *ret_rec,
  824. int *index,
  825. struct buffer_head **ret_bh)
  826. {
  827. int ret = 0, i, found;
  828. u32 low_cpos;
  829. struct ocfs2_extent_list *el;
  830. struct ocfs2_extent_rec *tmp, *rec = NULL;
  831. struct ocfs2_extent_block *eb;
  832. struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
  833. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  834. struct ocfs2_refcount_block *rb =
  835. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  836. if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
  837. ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
  838. ret_rec, index);
  839. *ret_bh = ref_root_bh;
  840. get_bh(ref_root_bh);
  841. return 0;
  842. }
  843. el = &rb->rf_list;
  844. low_cpos = cpos & OCFS2_32BIT_POS_MASK;
  845. if (el->l_tree_depth) {
  846. ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
  847. if (ret) {
  848. mlog_errno(ret);
  849. goto out;
  850. }
  851. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  852. el = &eb->h_list;
  853. if (el->l_tree_depth) {
  854. ocfs2_error(sb,
  855. "refcount tree %llu has non zero tree "
  856. "depth in leaf btree tree block %llu\n",
  857. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  858. (unsigned long long)eb_bh->b_blocknr);
  859. ret = -EROFS;
  860. goto out;
  861. }
  862. }
  863. found = 0;
  864. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  865. rec = &el->l_recs[i];
  866. if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
  867. found = 1;
  868. break;
  869. }
  870. }
  871. /* adjust len when we have ocfs2_extent_rec after it. */
  872. if (found && i < le16_to_cpu(el->l_next_free_rec) - 1) {
  873. tmp = &el->l_recs[i+1];
  874. if (le32_to_cpu(tmp->e_cpos) < cpos + len)
  875. len = le32_to_cpu(tmp->e_cpos) - cpos;
  876. }
  877. ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
  878. &ref_leaf_bh);
  879. if (ret) {
  880. mlog_errno(ret);
  881. goto out;
  882. }
  883. ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
  884. ret_rec, index);
  885. *ret_bh = ref_leaf_bh;
  886. out:
  887. brelse(eb_bh);
  888. return ret;
  889. }
  890. enum ocfs2_ref_rec_contig {
  891. REF_CONTIG_NONE = 0,
  892. REF_CONTIG_LEFT,
  893. REF_CONTIG_RIGHT,
  894. REF_CONTIG_LEFTRIGHT,
  895. };
  896. static enum ocfs2_ref_rec_contig
  897. ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
  898. int index)
  899. {
  900. if ((rb->rf_records.rl_recs[index].r_refcount ==
  901. rb->rf_records.rl_recs[index + 1].r_refcount) &&
  902. (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
  903. le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
  904. le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
  905. return REF_CONTIG_RIGHT;
  906. return REF_CONTIG_NONE;
  907. }
  908. static enum ocfs2_ref_rec_contig
  909. ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
  910. int index)
  911. {
  912. enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
  913. if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
  914. ret = ocfs2_refcount_rec_adjacent(rb, index);
  915. if (index > 0) {
  916. enum ocfs2_ref_rec_contig tmp;
  917. tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
  918. if (tmp == REF_CONTIG_RIGHT) {
  919. if (ret == REF_CONTIG_RIGHT)
  920. ret = REF_CONTIG_LEFTRIGHT;
  921. else
  922. ret = REF_CONTIG_LEFT;
  923. }
  924. }
  925. return ret;
  926. }
  927. static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
  928. int index)
  929. {
  930. BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
  931. rb->rf_records.rl_recs[index+1].r_refcount);
  932. le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
  933. le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
  934. if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
  935. memmove(&rb->rf_records.rl_recs[index + 1],
  936. &rb->rf_records.rl_recs[index + 2],
  937. sizeof(struct ocfs2_refcount_rec) *
  938. (le16_to_cpu(rb->rf_records.rl_used) - index - 2));
  939. memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
  940. 0, sizeof(struct ocfs2_refcount_rec));
  941. le16_add_cpu(&rb->rf_records.rl_used, -1);
  942. }
  943. /*
  944. * Merge the refcount rec if we are contiguous with the adjacent recs.
  945. */
  946. static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
  947. int index)
  948. {
  949. enum ocfs2_ref_rec_contig contig =
  950. ocfs2_refcount_rec_contig(rb, index);
  951. if (contig == REF_CONTIG_NONE)
  952. return;
  953. if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
  954. BUG_ON(index == 0);
  955. index--;
  956. }
  957. ocfs2_rotate_refcount_rec_left(rb, index);
  958. if (contig == REF_CONTIG_LEFTRIGHT)
  959. ocfs2_rotate_refcount_rec_left(rb, index);
  960. }
  961. /*
  962. * Change the refcount indexed by "index" in ref_bh.
  963. * If refcount reaches 0, remove it.
  964. */
  965. static int ocfs2_change_refcount_rec(handle_t *handle,
  966. struct ocfs2_caching_info *ci,
  967. struct buffer_head *ref_leaf_bh,
  968. int index, int change)
  969. {
  970. int ret;
  971. struct ocfs2_refcount_block *rb =
  972. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  973. struct ocfs2_refcount_list *rl = &rb->rf_records;
  974. struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
  975. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  976. OCFS2_JOURNAL_ACCESS_WRITE);
  977. if (ret) {
  978. mlog_errno(ret);
  979. goto out;
  980. }
  981. mlog(0, "change index %d, old count %u, change %d\n", index,
  982. le32_to_cpu(rec->r_refcount), change);
  983. le32_add_cpu(&rec->r_refcount, change);
  984. if (!rec->r_refcount) {
  985. if (index != le16_to_cpu(rl->rl_used) - 1) {
  986. memmove(rec, rec + 1,
  987. (le16_to_cpu(rl->rl_used) - index - 1) *
  988. sizeof(struct ocfs2_refcount_rec));
  989. memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
  990. 0, sizeof(struct ocfs2_refcount_rec));
  991. }
  992. le16_add_cpu(&rl->rl_used, -1);
  993. } else
  994. ocfs2_refcount_rec_merge(rb, index);
  995. ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
  996. if (ret)
  997. mlog_errno(ret);
  998. out:
  999. return ret;
  1000. }
  1001. static int ocfs2_expand_inline_ref_root(handle_t *handle,
  1002. struct ocfs2_caching_info *ci,
  1003. struct buffer_head *ref_root_bh,
  1004. struct buffer_head **ref_leaf_bh,
  1005. struct ocfs2_alloc_context *meta_ac)
  1006. {
  1007. int ret;
  1008. u16 suballoc_bit_start;
  1009. u32 num_got;
  1010. u64 blkno;
  1011. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1012. struct buffer_head *new_bh = NULL;
  1013. struct ocfs2_refcount_block *new_rb;
  1014. struct ocfs2_refcount_block *root_rb =
  1015. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1016. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1017. OCFS2_JOURNAL_ACCESS_WRITE);
  1018. if (ret) {
  1019. mlog_errno(ret);
  1020. goto out;
  1021. }
  1022. ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
  1023. &suballoc_bit_start, &num_got,
  1024. &blkno);
  1025. if (ret) {
  1026. mlog_errno(ret);
  1027. goto out;
  1028. }
  1029. new_bh = sb_getblk(sb, blkno);
  1030. if (new_bh == NULL) {
  1031. ret = -EIO;
  1032. mlog_errno(ret);
  1033. goto out;
  1034. }
  1035. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  1036. ret = ocfs2_journal_access_rb(handle, ci, new_bh,
  1037. OCFS2_JOURNAL_ACCESS_CREATE);
  1038. if (ret) {
  1039. mlog_errno(ret);
  1040. goto out;
  1041. }
  1042. /*
  1043. * Initialize ocfs2_refcount_block.
  1044. * It should contain the same information as the old root.
  1045. * so just memcpy it and change the corresponding field.
  1046. */
  1047. memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
  1048. new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  1049. new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
  1050. new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1051. new_rb->rf_blkno = cpu_to_le64(blkno);
  1052. new_rb->rf_cpos = cpu_to_le32(0);
  1053. new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
  1054. new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
  1055. ocfs2_journal_dirty(handle, new_bh);
  1056. /* Now change the root. */
  1057. memset(&root_rb->rf_list, 0, sb->s_blocksize -
  1058. offsetof(struct ocfs2_refcount_block, rf_list));
  1059. root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
  1060. root_rb->rf_clusters = cpu_to_le32(1);
  1061. root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
  1062. root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  1063. root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  1064. root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
  1065. ocfs2_journal_dirty(handle, ref_root_bh);
  1066. mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
  1067. le16_to_cpu(new_rb->rf_records.rl_used));
  1068. *ref_leaf_bh = new_bh;
  1069. new_bh = NULL;
  1070. out:
  1071. brelse(new_bh);
  1072. return ret;
  1073. }
  1074. static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
  1075. struct ocfs2_refcount_rec *next)
  1076. {
  1077. if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
  1078. ocfs2_get_ref_rec_low_cpos(next))
  1079. return 1;
  1080. return 0;
  1081. }
  1082. static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
  1083. {
  1084. const struct ocfs2_refcount_rec *l = a, *r = b;
  1085. u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
  1086. u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
  1087. if (l_cpos > r_cpos)
  1088. return 1;
  1089. if (l_cpos < r_cpos)
  1090. return -1;
  1091. return 0;
  1092. }
  1093. static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
  1094. {
  1095. const struct ocfs2_refcount_rec *l = a, *r = b;
  1096. u64 l_cpos = le64_to_cpu(l->r_cpos);
  1097. u64 r_cpos = le64_to_cpu(r->r_cpos);
  1098. if (l_cpos > r_cpos)
  1099. return 1;
  1100. if (l_cpos < r_cpos)
  1101. return -1;
  1102. return 0;
  1103. }
  1104. static void swap_refcount_rec(void *a, void *b, int size)
  1105. {
  1106. struct ocfs2_refcount_rec *l = a, *r = b, tmp;
  1107. tmp = *(struct ocfs2_refcount_rec *)l;
  1108. *(struct ocfs2_refcount_rec *)l =
  1109. *(struct ocfs2_refcount_rec *)r;
  1110. *(struct ocfs2_refcount_rec *)r = tmp;
  1111. }
  1112. /*
  1113. * The refcount cpos are ordered by their 64bit cpos,
  1114. * But we will use the low 32 bit to be the e_cpos in the b-tree.
  1115. * So we need to make sure that this pos isn't intersected with others.
  1116. *
  1117. * Note: The refcount block is already sorted by their low 32 bit cpos,
  1118. * So just try the middle pos first, and we will exit when we find
  1119. * the good position.
  1120. */
  1121. static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
  1122. u32 *split_pos, int *split_index)
  1123. {
  1124. int num_used = le16_to_cpu(rl->rl_used);
  1125. int delta, middle = num_used / 2;
  1126. for (delta = 0; delta < middle; delta++) {
  1127. /* Let's check delta earlier than middle */
  1128. if (ocfs2_refcount_rec_no_intersect(
  1129. &rl->rl_recs[middle - delta - 1],
  1130. &rl->rl_recs[middle - delta])) {
  1131. *split_index = middle - delta;
  1132. break;
  1133. }
  1134. /* For even counts, don't walk off the end */
  1135. if ((middle + delta + 1) == num_used)
  1136. continue;
  1137. /* Now try delta past middle */
  1138. if (ocfs2_refcount_rec_no_intersect(
  1139. &rl->rl_recs[middle + delta],
  1140. &rl->rl_recs[middle + delta + 1])) {
  1141. *split_index = middle + delta + 1;
  1142. break;
  1143. }
  1144. }
  1145. if (delta >= middle)
  1146. return -ENOSPC;
  1147. *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
  1148. return 0;
  1149. }
  1150. static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
  1151. struct buffer_head *new_bh,
  1152. u32 *split_cpos)
  1153. {
  1154. int split_index = 0, num_moved, ret;
  1155. u32 cpos = 0;
  1156. struct ocfs2_refcount_block *rb =
  1157. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1158. struct ocfs2_refcount_list *rl = &rb->rf_records;
  1159. struct ocfs2_refcount_block *new_rb =
  1160. (struct ocfs2_refcount_block *)new_bh->b_data;
  1161. struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
  1162. mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
  1163. (unsigned long long)ref_leaf_bh->b_blocknr,
  1164. le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
  1165. /*
  1166. * XXX: Improvement later.
  1167. * If we know all the high 32 bit cpos is the same, no need to sort.
  1168. *
  1169. * In order to make the whole process safe, we do:
  1170. * 1. sort the entries by their low 32 bit cpos first so that we can
  1171. * find the split cpos easily.
  1172. * 2. call ocfs2_insert_extent to insert the new refcount block.
  1173. * 3. move the refcount rec to the new block.
  1174. * 4. sort the entries by their 64 bit cpos.
  1175. * 5. dirty the new_rb and rb.
  1176. */
  1177. sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  1178. sizeof(struct ocfs2_refcount_rec),
  1179. cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
  1180. ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
  1181. if (ret) {
  1182. mlog_errno(ret);
  1183. return ret;
  1184. }
  1185. new_rb->rf_cpos = cpu_to_le32(cpos);
  1186. /* move refcount records starting from split_index to the new block. */
  1187. num_moved = le16_to_cpu(rl->rl_used) - split_index;
  1188. memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
  1189. num_moved * sizeof(struct ocfs2_refcount_rec));
  1190. /*ok, remove the entries we just moved over to the other block. */
  1191. memset(&rl->rl_recs[split_index], 0,
  1192. num_moved * sizeof(struct ocfs2_refcount_rec));
  1193. /* change old and new rl_used accordingly. */
  1194. le16_add_cpu(&rl->rl_used, -num_moved);
  1195. new_rl->rl_used = cpu_to_le32(num_moved);
  1196. sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  1197. sizeof(struct ocfs2_refcount_rec),
  1198. cmp_refcount_rec_by_cpos, swap_refcount_rec);
  1199. sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
  1200. sizeof(struct ocfs2_refcount_rec),
  1201. cmp_refcount_rec_by_cpos, swap_refcount_rec);
  1202. *split_cpos = cpos;
  1203. return 0;
  1204. }
  1205. static int ocfs2_new_leaf_refcount_block(handle_t *handle,
  1206. struct ocfs2_caching_info *ci,
  1207. struct buffer_head *ref_root_bh,
  1208. struct buffer_head *ref_leaf_bh,
  1209. struct ocfs2_alloc_context *meta_ac)
  1210. {
  1211. int ret;
  1212. u16 suballoc_bit_start;
  1213. u32 num_got, new_cpos;
  1214. u64 blkno;
  1215. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1216. struct ocfs2_refcount_block *root_rb =
  1217. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1218. struct buffer_head *new_bh = NULL;
  1219. struct ocfs2_refcount_block *new_rb;
  1220. struct ocfs2_extent_tree ref_et;
  1221. BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
  1222. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1223. OCFS2_JOURNAL_ACCESS_WRITE);
  1224. if (ret) {
  1225. mlog_errno(ret);
  1226. goto out;
  1227. }
  1228. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1229. OCFS2_JOURNAL_ACCESS_WRITE);
  1230. if (ret) {
  1231. mlog_errno(ret);
  1232. goto out;
  1233. }
  1234. ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
  1235. &suballoc_bit_start, &num_got,
  1236. &blkno);
  1237. if (ret) {
  1238. mlog_errno(ret);
  1239. goto out;
  1240. }
  1241. new_bh = sb_getblk(sb, blkno);
  1242. if (new_bh == NULL) {
  1243. ret = -EIO;
  1244. mlog_errno(ret);
  1245. goto out;
  1246. }
  1247. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  1248. ret = ocfs2_journal_access_rb(handle, ci, new_bh,
  1249. OCFS2_JOURNAL_ACCESS_CREATE);
  1250. if (ret) {
  1251. mlog_errno(ret);
  1252. goto out;
  1253. }
  1254. /* Initialize ocfs2_refcount_block. */
  1255. new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  1256. memset(new_rb, 0, sb->s_blocksize);
  1257. strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
  1258. new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
  1259. new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1260. new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  1261. new_rb->rf_blkno = cpu_to_le64(blkno);
  1262. new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
  1263. new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
  1264. new_rb->rf_records.rl_count =
  1265. cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
  1266. new_rb->rf_generation = root_rb->rf_generation;
  1267. ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
  1268. if (ret) {
  1269. mlog_errno(ret);
  1270. goto out;
  1271. }
  1272. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1273. ocfs2_journal_dirty(handle, new_bh);
  1274. ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
  1275. mlog(0, "insert new leaf block %llu at %u\n",
  1276. (unsigned long long)new_bh->b_blocknr, new_cpos);
  1277. /* Insert the new leaf block with the specific offset cpos. */
  1278. ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
  1279. 1, 0, meta_ac);
  1280. if (ret)
  1281. mlog_errno(ret);
  1282. out:
  1283. brelse(new_bh);
  1284. return ret;
  1285. }
  1286. static int ocfs2_expand_refcount_tree(handle_t *handle,
  1287. struct ocfs2_caching_info *ci,
  1288. struct buffer_head *ref_root_bh,
  1289. struct buffer_head *ref_leaf_bh,
  1290. struct ocfs2_alloc_context *meta_ac)
  1291. {
  1292. int ret;
  1293. struct buffer_head *expand_bh = NULL;
  1294. if (ref_root_bh == ref_leaf_bh) {
  1295. /*
  1296. * the old root bh hasn't been expanded to a b-tree,
  1297. * so expand it first.
  1298. */
  1299. ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
  1300. &expand_bh, meta_ac);
  1301. if (ret) {
  1302. mlog_errno(ret);
  1303. goto out;
  1304. }
  1305. } else {
  1306. expand_bh = ref_leaf_bh;
  1307. get_bh(expand_bh);
  1308. }
  1309. /* Now add a new refcount block into the tree.*/
  1310. ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
  1311. expand_bh, meta_ac);
  1312. if (ret)
  1313. mlog_errno(ret);
  1314. out:
  1315. brelse(expand_bh);
  1316. return ret;
  1317. }
  1318. /*
  1319. * Adjust the extent rec in b-tree representing ref_leaf_bh.
  1320. *
  1321. * Only called when we have inserted a new refcount rec at index 0
  1322. * which means ocfs2_extent_rec.e_cpos may need some change.
  1323. */
  1324. static int ocfs2_adjust_refcount_rec(handle_t *handle,
  1325. struct ocfs2_caching_info *ci,
  1326. struct buffer_head *ref_root_bh,
  1327. struct buffer_head *ref_leaf_bh,
  1328. struct ocfs2_refcount_rec *rec)
  1329. {
  1330. int ret = 0, i;
  1331. u32 new_cpos, old_cpos;
  1332. struct ocfs2_path *path = NULL;
  1333. struct ocfs2_extent_tree et;
  1334. struct ocfs2_refcount_block *rb =
  1335. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1336. struct ocfs2_extent_list *el;
  1337. if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
  1338. goto out;
  1339. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1340. old_cpos = le32_to_cpu(rb->rf_cpos);
  1341. new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
  1342. if (old_cpos <= new_cpos)
  1343. goto out;
  1344. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  1345. path = ocfs2_new_path_from_et(&et);
  1346. if (!path) {
  1347. ret = -ENOMEM;
  1348. mlog_errno(ret);
  1349. goto out;
  1350. }
  1351. ret = ocfs2_find_path(ci, path, old_cpos);
  1352. if (ret) {
  1353. mlog_errno(ret);
  1354. goto out;
  1355. }
  1356. /*
  1357. * 2 more credits, one for the leaf refcount block, one for
  1358. * the extent block contains the extent rec.
  1359. */
  1360. ret = ocfs2_extend_trans(handle, handle->h_buffer_credits + 2);
  1361. if (ret < 0) {
  1362. mlog_errno(ret);
  1363. goto out;
  1364. }
  1365. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1366. OCFS2_JOURNAL_ACCESS_WRITE);
  1367. if (ret < 0) {
  1368. mlog_errno(ret);
  1369. goto out;
  1370. }
  1371. ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
  1372. OCFS2_JOURNAL_ACCESS_WRITE);
  1373. if (ret < 0) {
  1374. mlog_errno(ret);
  1375. goto out;
  1376. }
  1377. /* change the leaf extent block first. */
  1378. el = path_leaf_el(path);
  1379. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
  1380. if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
  1381. break;
  1382. BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
  1383. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  1384. /* change the r_cpos in the leaf block. */
  1385. rb->rf_cpos = cpu_to_le32(new_cpos);
  1386. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  1387. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1388. out:
  1389. ocfs2_free_path(path);
  1390. return ret;
  1391. }
  1392. static int ocfs2_insert_refcount_rec(handle_t *handle,
  1393. struct ocfs2_caching_info *ci,
  1394. struct buffer_head *ref_root_bh,
  1395. struct buffer_head *ref_leaf_bh,
  1396. struct ocfs2_refcount_rec *rec,
  1397. int index,
  1398. struct ocfs2_alloc_context *meta_ac)
  1399. {
  1400. int ret;
  1401. struct ocfs2_refcount_block *rb =
  1402. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1403. struct ocfs2_refcount_list *rf_list = &rb->rf_records;
  1404. struct buffer_head *new_bh = NULL;
  1405. BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
  1406. if (rf_list->rl_used == rf_list->rl_count) {
  1407. u64 cpos = le64_to_cpu(rec->r_cpos);
  1408. u32 len = le32_to_cpu(rec->r_clusters);
  1409. ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
  1410. ref_leaf_bh, meta_ac);
  1411. if (ret) {
  1412. mlog_errno(ret);
  1413. goto out;
  1414. }
  1415. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1416. cpos, len, NULL, &index,
  1417. &new_bh);
  1418. if (ret) {
  1419. mlog_errno(ret);
  1420. goto out;
  1421. }
  1422. ref_leaf_bh = new_bh;
  1423. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1424. rf_list = &rb->rf_records;
  1425. }
  1426. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1427. OCFS2_JOURNAL_ACCESS_WRITE);
  1428. if (ret) {
  1429. mlog_errno(ret);
  1430. goto out;
  1431. }
  1432. if (index < le16_to_cpu(rf_list->rl_used))
  1433. memmove(&rf_list->rl_recs[index + 1],
  1434. &rf_list->rl_recs[index],
  1435. (le16_to_cpu(rf_list->rl_used) - index) *
  1436. sizeof(struct ocfs2_refcount_rec));
  1437. mlog(0, "insert refcount record start %llu, len %u, count %u "
  1438. "to leaf block %llu at index %d\n",
  1439. (unsigned long long)le64_to_cpu(rec->r_cpos),
  1440. le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
  1441. (unsigned long long)ref_leaf_bh->b_blocknr, index);
  1442. rf_list->rl_recs[index] = *rec;
  1443. le16_add_cpu(&rf_list->rl_used, 1);
  1444. ocfs2_refcount_rec_merge(rb, index);
  1445. ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
  1446. if (ret) {
  1447. mlog_errno(ret);
  1448. goto out;
  1449. }
  1450. if (index == 0) {
  1451. ret = ocfs2_adjust_refcount_rec(handle, ci,
  1452. ref_root_bh,
  1453. ref_leaf_bh, rec);
  1454. if (ret)
  1455. mlog_errno(ret);
  1456. }
  1457. out:
  1458. brelse(new_bh);
  1459. return ret;
  1460. }
  1461. /*
  1462. * Split the refcount_rec indexed by "index" in ref_leaf_bh.
  1463. * This is much simple than our b-tree code.
  1464. * split_rec is the new refcount rec we want to insert.
  1465. * If split_rec->r_refcount > 0, we are changing the refcount(in case we
  1466. * increase refcount or decrease a refcount to non-zero).
  1467. * If split_rec->r_refcount == 0, we are punching a hole in current refcount
  1468. * rec( in case we decrease a refcount to zero).
  1469. */
  1470. static int ocfs2_split_refcount_rec(handle_t *handle,
  1471. struct ocfs2_caching_info *ci,
  1472. struct buffer_head *ref_root_bh,
  1473. struct buffer_head *ref_leaf_bh,
  1474. struct ocfs2_refcount_rec *split_rec,
  1475. int index,
  1476. struct ocfs2_alloc_context *meta_ac,
  1477. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1478. {
  1479. int ret, recs_need;
  1480. u32 len;
  1481. struct ocfs2_refcount_block *rb =
  1482. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1483. struct ocfs2_refcount_list *rf_list = &rb->rf_records;
  1484. struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
  1485. struct ocfs2_refcount_rec *tail_rec = NULL;
  1486. struct buffer_head *new_bh = NULL;
  1487. BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
  1488. mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
  1489. le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
  1490. le64_to_cpu(split_rec->r_cpos),
  1491. le32_to_cpu(split_rec->r_clusters));
  1492. /*
  1493. * If we just need to split the header or tail clusters,
  1494. * no more recs are needed, just split is OK.
  1495. * Otherwise we at least need one new recs.
  1496. */
  1497. if (!split_rec->r_refcount &&
  1498. (split_rec->r_cpos == orig_rec->r_cpos ||
  1499. le64_to_cpu(split_rec->r_cpos) +
  1500. le32_to_cpu(split_rec->r_clusters) ==
  1501. le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
  1502. recs_need = 0;
  1503. else
  1504. recs_need = 1;
  1505. /*
  1506. * We need one more rec if we split in the middle and the new rec have
  1507. * some refcount in it.
  1508. */
  1509. if (split_rec->r_refcount &&
  1510. (split_rec->r_cpos != orig_rec->r_cpos &&
  1511. le64_to_cpu(split_rec->r_cpos) +
  1512. le32_to_cpu(split_rec->r_clusters) !=
  1513. le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
  1514. recs_need++;
  1515. /* If the leaf block don't have enough record, expand it. */
  1516. if (le16_to_cpu(rf_list->rl_used) + recs_need > rf_list->rl_count) {
  1517. struct ocfs2_refcount_rec tmp_rec;
  1518. u64 cpos = le64_to_cpu(orig_rec->r_cpos);
  1519. len = le32_to_cpu(orig_rec->r_clusters);
  1520. ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
  1521. ref_leaf_bh, meta_ac);
  1522. if (ret) {
  1523. mlog_errno(ret);
  1524. goto out;
  1525. }
  1526. /*
  1527. * We have to re-get it since now cpos may be moved to
  1528. * another leaf block.
  1529. */
  1530. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1531. cpos, len, &tmp_rec, &index,
  1532. &new_bh);
  1533. if (ret) {
  1534. mlog_errno(ret);
  1535. goto out;
  1536. }
  1537. ref_leaf_bh = new_bh;
  1538. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1539. rf_list = &rb->rf_records;
  1540. orig_rec = &rf_list->rl_recs[index];
  1541. }
  1542. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1543. OCFS2_JOURNAL_ACCESS_WRITE);
  1544. if (ret) {
  1545. mlog_errno(ret);
  1546. goto out;
  1547. }
  1548. /*
  1549. * We have calculated out how many new records we need and store
  1550. * in recs_need, so spare enough space first by moving the records
  1551. * after "index" to the end.
  1552. */
  1553. if (index != le16_to_cpu(rf_list->rl_used) - 1)
  1554. memmove(&rf_list->rl_recs[index + 1 + recs_need],
  1555. &rf_list->rl_recs[index + 1],
  1556. (le16_to_cpu(rf_list->rl_used) - index - 1) *
  1557. sizeof(struct ocfs2_refcount_rec));
  1558. len = (le64_to_cpu(orig_rec->r_cpos) +
  1559. le32_to_cpu(orig_rec->r_clusters)) -
  1560. (le64_to_cpu(split_rec->r_cpos) +
  1561. le32_to_cpu(split_rec->r_clusters));
  1562. /*
  1563. * If we have "len", the we will split in the tail and move it
  1564. * to the end of the space we have just spared.
  1565. */
  1566. if (len) {
  1567. tail_rec = &rf_list->rl_recs[index + recs_need];
  1568. memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
  1569. le64_add_cpu(&tail_rec->r_cpos,
  1570. le32_to_cpu(tail_rec->r_clusters) - len);
  1571. tail_rec->r_clusters = le32_to_cpu(len);
  1572. }
  1573. /*
  1574. * If the split pos isn't the same as the original one, we need to
  1575. * split in the head.
  1576. *
  1577. * Note: We have the chance that split_rec.r_refcount = 0,
  1578. * recs_need = 0 and len > 0, which means we just cut the head from
  1579. * the orig_rec and in that case we have done some modification in
  1580. * orig_rec above, so the check for r_cpos is faked.
  1581. */
  1582. if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
  1583. len = le64_to_cpu(split_rec->r_cpos) -
  1584. le64_to_cpu(orig_rec->r_cpos);
  1585. orig_rec->r_clusters = cpu_to_le32(len);
  1586. index++;
  1587. }
  1588. le16_add_cpu(&rf_list->rl_used, recs_need);
  1589. if (split_rec->r_refcount) {
  1590. rf_list->rl_recs[index] = *split_rec;
  1591. mlog(0, "insert refcount record start %llu, len %u, count %u "
  1592. "to leaf block %llu at index %d\n",
  1593. (unsigned long long)le64_to_cpu(split_rec->r_cpos),
  1594. le32_to_cpu(split_rec->r_clusters),
  1595. le32_to_cpu(split_rec->r_refcount),
  1596. (unsigned long long)ref_leaf_bh->b_blocknr, index);
  1597. ocfs2_refcount_rec_merge(rb, index);
  1598. }
  1599. ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
  1600. if (ret)
  1601. mlog_errno(ret);
  1602. out:
  1603. brelse(new_bh);
  1604. return ret;
  1605. }
  1606. static int __ocfs2_increase_refcount(handle_t *handle,
  1607. struct ocfs2_caching_info *ci,
  1608. struct buffer_head *ref_root_bh,
  1609. u64 cpos, u32 len,
  1610. struct ocfs2_alloc_context *meta_ac,
  1611. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1612. {
  1613. int ret = 0, index;
  1614. struct buffer_head *ref_leaf_bh = NULL;
  1615. struct ocfs2_refcount_rec rec;
  1616. unsigned int set_len = 0;
  1617. mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
  1618. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1619. (unsigned long long)cpos, len);
  1620. while (len) {
  1621. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1622. cpos, len, &rec, &index,
  1623. &ref_leaf_bh);
  1624. if (ret) {
  1625. mlog_errno(ret);
  1626. goto out;
  1627. }
  1628. set_len = le32_to_cpu(rec.r_clusters);
  1629. /*
  1630. * Here we may meet with 3 situations:
  1631. *
  1632. * 1. If we find an already existing record, and the length
  1633. * is the same, cool, we just need to increase the r_refcount
  1634. * and it is OK.
  1635. * 2. If we find a hole, just insert it with r_refcount = 1.
  1636. * 3. If we are in the middle of one extent record, split
  1637. * it.
  1638. */
  1639. if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
  1640. set_len <= len) {
  1641. mlog(0, "increase refcount rec, start %llu, len %u, "
  1642. "count %u\n", (unsigned long long)cpos, set_len,
  1643. le32_to_cpu(rec.r_refcount));
  1644. ret = ocfs2_change_refcount_rec(handle, ci,
  1645. ref_leaf_bh, index, 1);
  1646. if (ret) {
  1647. mlog_errno(ret);
  1648. goto out;
  1649. }
  1650. } else if (!rec.r_refcount) {
  1651. rec.r_refcount = cpu_to_le32(1);
  1652. mlog(0, "insert refcount rec, start %llu, len %u\n",
  1653. (unsigned long long)le64_to_cpu(rec.r_cpos),
  1654. set_len);
  1655. ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
  1656. ref_leaf_bh,
  1657. &rec, index, meta_ac);
  1658. if (ret) {
  1659. mlog_errno(ret);
  1660. goto out;
  1661. }
  1662. } else {
  1663. set_len = min((u64)(cpos + len),
  1664. le64_to_cpu(rec.r_cpos) + set_len) - cpos;
  1665. rec.r_cpos = cpu_to_le64(cpos);
  1666. rec.r_clusters = cpu_to_le32(set_len);
  1667. le32_add_cpu(&rec.r_refcount, 1);
  1668. mlog(0, "split refcount rec, start %llu, "
  1669. "len %u, count %u\n",
  1670. (unsigned long long)le64_to_cpu(rec.r_cpos),
  1671. set_len, le32_to_cpu(rec.r_refcount));
  1672. ret = ocfs2_split_refcount_rec(handle, ci,
  1673. ref_root_bh, ref_leaf_bh,
  1674. &rec, index,
  1675. meta_ac, dealloc);
  1676. if (ret) {
  1677. mlog_errno(ret);
  1678. goto out;
  1679. }
  1680. }
  1681. cpos += set_len;
  1682. len -= set_len;
  1683. brelse(ref_leaf_bh);
  1684. ref_leaf_bh = NULL;
  1685. }
  1686. out:
  1687. brelse(ref_leaf_bh);
  1688. return ret;
  1689. }
  1690. static int ocfs2_remove_refcount_extent(handle_t *handle,
  1691. struct ocfs2_caching_info *ci,
  1692. struct buffer_head *ref_root_bh,
  1693. struct buffer_head *ref_leaf_bh,
  1694. struct ocfs2_alloc_context *meta_ac,
  1695. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1696. {
  1697. int ret;
  1698. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1699. struct ocfs2_refcount_block *rb =
  1700. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1701. struct ocfs2_extent_tree et;
  1702. BUG_ON(rb->rf_records.rl_used);
  1703. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  1704. ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
  1705. 1, meta_ac, dealloc);
  1706. if (ret) {
  1707. mlog_errno(ret);
  1708. goto out;
  1709. }
  1710. ocfs2_remove_from_cache(ci, ref_leaf_bh);
  1711. /*
  1712. * add the freed block to the dealloc so that it will be freed
  1713. * when we run dealloc.
  1714. */
  1715. ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
  1716. le16_to_cpu(rb->rf_suballoc_slot),
  1717. le64_to_cpu(rb->rf_blkno),
  1718. le16_to_cpu(rb->rf_suballoc_bit));
  1719. if (ret) {
  1720. mlog_errno(ret);
  1721. goto out;
  1722. }
  1723. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1724. OCFS2_JOURNAL_ACCESS_WRITE);
  1725. if (ret) {
  1726. mlog_errno(ret);
  1727. goto out;
  1728. }
  1729. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1730. le32_add_cpu(&rb->rf_clusters, -1);
  1731. /*
  1732. * check whether we need to restore the root refcount block if
  1733. * there is no leaf extent block at atll.
  1734. */
  1735. if (!rb->rf_list.l_next_free_rec) {
  1736. BUG_ON(rb->rf_clusters);
  1737. mlog(0, "reset refcount tree root %llu to be a record block.\n",
  1738. (unsigned long long)ref_root_bh->b_blocknr);
  1739. rb->rf_flags = 0;
  1740. rb->rf_parent = 0;
  1741. rb->rf_cpos = 0;
  1742. memset(&rb->rf_records, 0, sb->s_blocksize -
  1743. offsetof(struct ocfs2_refcount_block, rf_records));
  1744. rb->rf_records.rl_count =
  1745. cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
  1746. }
  1747. ocfs2_journal_dirty(handle, ref_root_bh);
  1748. out:
  1749. return ret;
  1750. }
  1751. static int ocfs2_decrease_refcount_rec(handle_t *handle,
  1752. struct ocfs2_caching_info *ci,
  1753. struct buffer_head *ref_root_bh,
  1754. struct buffer_head *ref_leaf_bh,
  1755. int index, u64 cpos, unsigned int len,
  1756. struct ocfs2_alloc_context *meta_ac,
  1757. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1758. {
  1759. int ret;
  1760. struct ocfs2_refcount_block *rb =
  1761. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1762. struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
  1763. BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
  1764. BUG_ON(cpos + len >
  1765. le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
  1766. if (cpos == le64_to_cpu(rec->r_cpos) &&
  1767. len == le32_to_cpu(rec->r_clusters))
  1768. ret = ocfs2_change_refcount_rec(handle, ci,
  1769. ref_leaf_bh, index, -1);
  1770. else {
  1771. struct ocfs2_refcount_rec split = *rec;
  1772. split.r_cpos = cpu_to_le64(cpos);
  1773. split.r_clusters = cpu_to_le32(len);
  1774. le32_add_cpu(&split.r_refcount, -1);
  1775. mlog(0, "split refcount rec, start %llu, "
  1776. "len %u, count %u, original start %llu, len %u\n",
  1777. (unsigned long long)le64_to_cpu(split.r_cpos),
  1778. len, le32_to_cpu(split.r_refcount),
  1779. (unsigned long long)le64_to_cpu(rec->r_cpos),
  1780. le32_to_cpu(rec->r_clusters));
  1781. ret = ocfs2_split_refcount_rec(handle, ci,
  1782. ref_root_bh, ref_leaf_bh,
  1783. &split, index,
  1784. meta_ac, dealloc);
  1785. }
  1786. if (ret) {
  1787. mlog_errno(ret);
  1788. goto out;
  1789. }
  1790. /* Remove the leaf refcount block if it contains no refcount record. */
  1791. if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
  1792. ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
  1793. ref_leaf_bh, meta_ac,
  1794. dealloc);
  1795. if (ret)
  1796. mlog_errno(ret);
  1797. }
  1798. out:
  1799. return ret;
  1800. }
  1801. static int __ocfs2_decrease_refcount(handle_t *handle,
  1802. struct ocfs2_caching_info *ci,
  1803. struct buffer_head *ref_root_bh,
  1804. u64 cpos, u32 len,
  1805. struct ocfs2_alloc_context *meta_ac,
  1806. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1807. int delete)
  1808. {
  1809. int ret = 0, index = 0;
  1810. struct ocfs2_refcount_rec rec;
  1811. unsigned int r_count = 0, r_len;
  1812. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1813. struct buffer_head *ref_leaf_bh = NULL;
  1814. mlog(0, "Tree owner %llu, decrease refcount start %llu, "
  1815. "len %u, delete %u\n",
  1816. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1817. (unsigned long long)cpos, len, delete);
  1818. while (len) {
  1819. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1820. cpos, len, &rec, &index,
  1821. &ref_leaf_bh);
  1822. if (ret) {
  1823. mlog_errno(ret);
  1824. goto out;
  1825. }
  1826. r_count = le32_to_cpu(rec.r_refcount);
  1827. BUG_ON(r_count == 0);
  1828. if (!delete)
  1829. BUG_ON(r_count > 1);
  1830. r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
  1831. le32_to_cpu(rec.r_clusters)) - cpos;
  1832. ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
  1833. ref_leaf_bh, index,
  1834. cpos, r_len,
  1835. meta_ac, dealloc);
  1836. if (ret) {
  1837. mlog_errno(ret);
  1838. goto out;
  1839. }
  1840. if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
  1841. ret = ocfs2_cache_cluster_dealloc(dealloc,
  1842. ocfs2_clusters_to_blocks(sb, cpos),
  1843. r_len);
  1844. if (ret) {
  1845. mlog_errno(ret);
  1846. goto out;
  1847. }
  1848. }
  1849. cpos += r_len;
  1850. len -= r_len;
  1851. brelse(ref_leaf_bh);
  1852. ref_leaf_bh = NULL;
  1853. }
  1854. out:
  1855. brelse(ref_leaf_bh);
  1856. return ret;
  1857. }
  1858. /* Caller must hold refcount tree lock. */
  1859. int ocfs2_decrease_refcount(struct inode *inode,
  1860. handle_t *handle, u32 cpos, u32 len,
  1861. struct ocfs2_alloc_context *meta_ac,
  1862. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1863. int delete)
  1864. {
  1865. int ret;
  1866. u64 ref_blkno;
  1867. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1868. struct buffer_head *ref_root_bh = NULL;
  1869. struct ocfs2_refcount_tree *tree;
  1870. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  1871. ret = ocfs2_get_refcount_block(inode, &ref_blkno);
  1872. if (ret) {
  1873. mlog_errno(ret);
  1874. goto out;
  1875. }
  1876. ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
  1877. if (ret) {
  1878. mlog_errno(ret);
  1879. goto out;
  1880. }
  1881. ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
  1882. &ref_root_bh);
  1883. if (ret) {
  1884. mlog_errno(ret);
  1885. goto out;
  1886. }
  1887. ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
  1888. cpos, len, meta_ac, dealloc, delete);
  1889. if (ret)
  1890. mlog_errno(ret);
  1891. out:
  1892. brelse(ref_root_bh);
  1893. return ret;
  1894. }
  1895. /*
  1896. * Mark the already-existing extent at cpos as refcounted for len clusters.
  1897. * This adds the refcount extent flag.
  1898. *
  1899. * If the existing extent is larger than the request, initiate a
  1900. * split. An attempt will be made at merging with adjacent extents.
  1901. *
  1902. * The caller is responsible for passing down meta_ac if we'll need it.
  1903. */
  1904. static int ocfs2_mark_extent_refcounted(struct inode *inode,
  1905. struct ocfs2_extent_tree *et,
  1906. handle_t *handle, u32 cpos,
  1907. u32 len, u32 phys,
  1908. struct ocfs2_alloc_context *meta_ac,
  1909. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1910. {
  1911. int ret;
  1912. mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
  1913. inode->i_ino, cpos, len, phys);
  1914. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  1915. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  1916. "tree, but the feature bit is not set in the "
  1917. "super block.", inode->i_ino);
  1918. ret = -EROFS;
  1919. goto out;
  1920. }
  1921. ret = ocfs2_change_extent_flag(handle, et, cpos,
  1922. len, phys, meta_ac, dealloc,
  1923. OCFS2_EXT_REFCOUNTED, 0);
  1924. if (ret)
  1925. mlog_errno(ret);
  1926. out:
  1927. return ret;
  1928. }
  1929. /*
  1930. * Given some contiguous physical clusters, calculate what we need
  1931. * for modifying their refcount.
  1932. */
  1933. static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
  1934. struct ocfs2_caching_info *ci,
  1935. struct buffer_head *ref_root_bh,
  1936. u64 start_cpos,
  1937. u32 clusters,
  1938. int *meta_add,
  1939. int *credits)
  1940. {
  1941. int ret = 0, index, ref_blocks = 0, recs_add = 0;
  1942. u64 cpos = start_cpos;
  1943. struct ocfs2_refcount_block *rb;
  1944. struct ocfs2_refcount_rec rec;
  1945. struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
  1946. u32 len;
  1947. mlog(0, "start_cpos %llu, clusters %u\n",
  1948. (unsigned long long)start_cpos, clusters);
  1949. while (clusters) {
  1950. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1951. cpos, clusters, &rec,
  1952. &index, &ref_leaf_bh);
  1953. if (ret) {
  1954. mlog_errno(ret);
  1955. goto out;
  1956. }
  1957. if (ref_leaf_bh != prev_bh) {
  1958. /*
  1959. * Now we encounter a new leaf block, so calculate
  1960. * whether we need to extend the old leaf.
  1961. */
  1962. if (prev_bh) {
  1963. rb = (struct ocfs2_refcount_block *)
  1964. prev_bh->b_data;
  1965. if (le64_to_cpu(rb->rf_records.rl_used) +
  1966. recs_add >
  1967. le16_to_cpu(rb->rf_records.rl_count))
  1968. ref_blocks++;
  1969. }
  1970. recs_add = 0;
  1971. *credits += 1;
  1972. brelse(prev_bh);
  1973. prev_bh = ref_leaf_bh;
  1974. get_bh(prev_bh);
  1975. }
  1976. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1977. mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
  1978. "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
  1979. recs_add, (unsigned long long)cpos, clusters,
  1980. (unsigned long long)le64_to_cpu(rec.r_cpos),
  1981. le32_to_cpu(rec.r_clusters),
  1982. le32_to_cpu(rec.r_refcount), index);
  1983. len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
  1984. le32_to_cpu(rec.r_clusters)) - cpos;
  1985. /*
  1986. * If the refcount rec already exist, cool. We just need
  1987. * to check whether there is a split. Otherwise we just need
  1988. * to increase the refcount.
  1989. * If we will insert one, increases recs_add.
  1990. *
  1991. * We record all the records which will be inserted to the
  1992. * same refcount block, so that we can tell exactly whether
  1993. * we need a new refcount block or not.
  1994. */
  1995. if (rec.r_refcount) {
  1996. /* Check whether we need a split at the beginning. */
  1997. if (cpos == start_cpos &&
  1998. cpos != le64_to_cpu(rec.r_cpos))
  1999. recs_add++;
  2000. /* Check whether we need a split in the end. */
  2001. if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
  2002. le32_to_cpu(rec.r_clusters))
  2003. recs_add++;
  2004. } else
  2005. recs_add++;
  2006. brelse(ref_leaf_bh);
  2007. ref_leaf_bh = NULL;
  2008. clusters -= len;
  2009. cpos += len;
  2010. }
  2011. if (prev_bh) {
  2012. rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
  2013. if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
  2014. le16_to_cpu(rb->rf_records.rl_count))
  2015. ref_blocks++;
  2016. *credits += 1;
  2017. }
  2018. if (!ref_blocks)
  2019. goto out;
  2020. mlog(0, "we need ref_blocks %d\n", ref_blocks);
  2021. *meta_add += ref_blocks;
  2022. *credits += ref_blocks;
  2023. /*
  2024. * So we may need ref_blocks to insert into the tree.
  2025. * That also means we need to change the b-tree and add that number
  2026. * of records since we never merge them.
  2027. * We need one more block for expansion since the new created leaf
  2028. * block is also full and needs split.
  2029. */
  2030. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  2031. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
  2032. struct ocfs2_extent_tree et;
  2033. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  2034. *meta_add += ocfs2_extend_meta_needed(et.et_root_el);
  2035. *credits += ocfs2_calc_extend_credits(sb,
  2036. et.et_root_el,
  2037. ref_blocks);
  2038. } else {
  2039. *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  2040. *meta_add += 1;
  2041. }
  2042. out:
  2043. brelse(ref_leaf_bh);
  2044. brelse(prev_bh);
  2045. return ret;
  2046. }
  2047. /*
  2048. * For refcount tree, we will decrease some contiguous clusters
  2049. * refcount count, so just go through it to see how many blocks
  2050. * we gonna touch and whether we need to create new blocks.
  2051. *
  2052. * Normally the refcount blocks store these refcount should be
  2053. * continguous also, so that we can get the number easily.
  2054. * As for meta_ac, we will at most add split 2 refcount record and
  2055. * 2 more refcount block, so just check it in a rough way.
  2056. *
  2057. * Caller must hold refcount tree lock.
  2058. */
  2059. int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
  2060. struct buffer_head *di_bh,
  2061. u64 phys_blkno,
  2062. u32 clusters,
  2063. int *credits,
  2064. struct ocfs2_alloc_context **meta_ac)
  2065. {
  2066. int ret, ref_blocks = 0;
  2067. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2068. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2069. struct buffer_head *ref_root_bh = NULL;
  2070. struct ocfs2_refcount_tree *tree;
  2071. u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
  2072. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  2073. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  2074. "tree, but the feature bit is not set in the "
  2075. "super block.", inode->i_ino);
  2076. ret = -EROFS;
  2077. goto out;
  2078. }
  2079. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  2080. ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
  2081. le64_to_cpu(di->i_refcount_loc), &tree);
  2082. if (ret) {
  2083. mlog_errno(ret);
  2084. goto out;
  2085. }
  2086. ret = ocfs2_read_refcount_block(&tree->rf_ci,
  2087. le64_to_cpu(di->i_refcount_loc),
  2088. &ref_root_bh);
  2089. if (ret) {
  2090. mlog_errno(ret);
  2091. goto out;
  2092. }
  2093. ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
  2094. &tree->rf_ci,
  2095. ref_root_bh,
  2096. start_cpos, clusters,
  2097. &ref_blocks, credits);
  2098. if (ret) {
  2099. mlog_errno(ret);
  2100. goto out;
  2101. }
  2102. mlog(0, "reserve new metadata %d, credits = %d\n",
  2103. ref_blocks, *credits);
  2104. if (ref_blocks) {
  2105. ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
  2106. ref_blocks, meta_ac);
  2107. if (ret)
  2108. mlog_errno(ret);
  2109. }
  2110. out:
  2111. brelse(ref_root_bh);
  2112. return ret;
  2113. }
  2114. #define MAX_CONTIG_BYTES 1048576
  2115. static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
  2116. {
  2117. return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
  2118. }
  2119. static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
  2120. {
  2121. return ~(ocfs2_cow_contig_clusters(sb) - 1);
  2122. }
  2123. /*
  2124. * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
  2125. * find an offset (start + (n * contig_clusters)) that is closest to cpos
  2126. * while still being less than or equal to it.
  2127. *
  2128. * The goal is to break the extent at a multiple of contig_clusters.
  2129. */
  2130. static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
  2131. unsigned int start,
  2132. unsigned int cpos)
  2133. {
  2134. BUG_ON(start > cpos);
  2135. return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
  2136. }
  2137. /*
  2138. * Given a cluster count of len, pad it out so that it is a multiple
  2139. * of contig_clusters.
  2140. */
  2141. static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
  2142. unsigned int len)
  2143. {
  2144. unsigned int padded =
  2145. (len + (ocfs2_cow_contig_clusters(sb) - 1)) &
  2146. ocfs2_cow_contig_mask(sb);
  2147. /* Did we wrap? */
  2148. if (padded < len)
  2149. padded = UINT_MAX;
  2150. return padded;
  2151. }
  2152. /*
  2153. * Calculate out the start and number of virtual clusters we need to to CoW.
  2154. *
  2155. * cpos is vitual start cluster position we want to do CoW in a
  2156. * file and write_len is the cluster length.
  2157. * max_cpos is the place where we want to stop CoW intentionally.
  2158. *
  2159. * Normal we will start CoW from the beginning of extent record cotaining cpos.
  2160. * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
  2161. * get good I/O from the resulting extent tree.
  2162. */
  2163. static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
  2164. struct ocfs2_extent_list *el,
  2165. u32 cpos,
  2166. u32 write_len,
  2167. u32 max_cpos,
  2168. u32 *cow_start,
  2169. u32 *cow_len)
  2170. {
  2171. int ret = 0;
  2172. int tree_height = le16_to_cpu(el->l_tree_depth), i;
  2173. struct buffer_head *eb_bh = NULL;
  2174. struct ocfs2_extent_block *eb = NULL;
  2175. struct ocfs2_extent_rec *rec;
  2176. unsigned int want_clusters, rec_end = 0;
  2177. int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
  2178. int leaf_clusters;
  2179. BUG_ON(cpos + write_len > max_cpos);
  2180. if (tree_height > 0) {
  2181. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
  2182. if (ret) {
  2183. mlog_errno(ret);
  2184. goto out;
  2185. }
  2186. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2187. el = &eb->h_list;
  2188. if (el->l_tree_depth) {
  2189. ocfs2_error(inode->i_sb,
  2190. "Inode %lu has non zero tree depth in "
  2191. "leaf block %llu\n", inode->i_ino,
  2192. (unsigned long long)eb_bh->b_blocknr);
  2193. ret = -EROFS;
  2194. goto out;
  2195. }
  2196. }
  2197. *cow_len = 0;
  2198. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  2199. rec = &el->l_recs[i];
  2200. if (ocfs2_is_empty_extent(rec)) {
  2201. mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
  2202. "index %d\n", inode->i_ino, i);
  2203. continue;
  2204. }
  2205. if (le32_to_cpu(rec->e_cpos) +
  2206. le16_to_cpu(rec->e_leaf_clusters) <= cpos)
  2207. continue;
  2208. if (*cow_len == 0) {
  2209. /*
  2210. * We should find a refcounted record in the
  2211. * first pass.
  2212. */
  2213. BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
  2214. *cow_start = le32_to_cpu(rec->e_cpos);
  2215. }
  2216. /*
  2217. * If we encounter a hole, a non-refcounted record or
  2218. * pass the max_cpos, stop the search.
  2219. */
  2220. if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
  2221. (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
  2222. (max_cpos <= le32_to_cpu(rec->e_cpos)))
  2223. break;
  2224. leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
  2225. rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
  2226. if (rec_end > max_cpos) {
  2227. rec_end = max_cpos;
  2228. leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
  2229. }
  2230. /*
  2231. * How many clusters do we actually need from
  2232. * this extent? First we see how many we actually
  2233. * need to complete the write. If that's smaller
  2234. * than contig_clusters, we try for contig_clusters.
  2235. */
  2236. if (!*cow_len)
  2237. want_clusters = write_len;
  2238. else
  2239. want_clusters = (cpos + write_len) -
  2240. (*cow_start + *cow_len);
  2241. if (want_clusters < contig_clusters)
  2242. want_clusters = contig_clusters;
  2243. /*
  2244. * If the write does not cover the whole extent, we
  2245. * need to calculate how we're going to split the extent.
  2246. * We try to do it on contig_clusters boundaries.
  2247. *
  2248. * Any extent smaller than contig_clusters will be
  2249. * CoWed in its entirety.
  2250. */
  2251. if (leaf_clusters <= contig_clusters)
  2252. *cow_len += leaf_clusters;
  2253. else if (*cow_len || (*cow_start == cpos)) {
  2254. /*
  2255. * This extent needs to be CoW'd from its
  2256. * beginning, so all we have to do is compute
  2257. * how many clusters to grab. We align
  2258. * want_clusters to the edge of contig_clusters
  2259. * to get better I/O.
  2260. */
  2261. want_clusters = ocfs2_cow_align_length(inode->i_sb,
  2262. want_clusters);
  2263. if (leaf_clusters < want_clusters)
  2264. *cow_len += leaf_clusters;
  2265. else
  2266. *cow_len += want_clusters;
  2267. } else if ((*cow_start + contig_clusters) >=
  2268. (cpos + write_len)) {
  2269. /*
  2270. * Breaking off contig_clusters at the front
  2271. * of the extent will cover our write. That's
  2272. * easy.
  2273. */
  2274. *cow_len = contig_clusters;
  2275. } else if ((rec_end - cpos) <= contig_clusters) {
  2276. /*
  2277. * Breaking off contig_clusters at the tail of
  2278. * this extent will cover cpos.
  2279. */
  2280. *cow_start = rec_end - contig_clusters;
  2281. *cow_len = contig_clusters;
  2282. } else if ((rec_end - cpos) <= want_clusters) {
  2283. /*
  2284. * While we can't fit the entire write in this
  2285. * extent, we know that the write goes from cpos
  2286. * to the end of the extent. Break that off.
  2287. * We try to break it at some multiple of
  2288. * contig_clusters from the front of the extent.
  2289. * Failing that (ie, cpos is within
  2290. * contig_clusters of the front), we'll CoW the
  2291. * entire extent.
  2292. */
  2293. *cow_start = ocfs2_cow_align_start(inode->i_sb,
  2294. *cow_start, cpos);
  2295. *cow_len = rec_end - *cow_start;
  2296. } else {
  2297. /*
  2298. * Ok, the entire write lives in the middle of
  2299. * this extent. Let's try to slice the extent up
  2300. * nicely. Optimally, our CoW region starts at
  2301. * m*contig_clusters from the beginning of the
  2302. * extent and goes for n*contig_clusters,
  2303. * covering the entire write.
  2304. */
  2305. *cow_start = ocfs2_cow_align_start(inode->i_sb,
  2306. *cow_start, cpos);
  2307. want_clusters = (cpos + write_len) - *cow_start;
  2308. want_clusters = ocfs2_cow_align_length(inode->i_sb,
  2309. want_clusters);
  2310. if (*cow_start + want_clusters <= rec_end)
  2311. *cow_len = want_clusters;
  2312. else
  2313. *cow_len = rec_end - *cow_start;
  2314. }
  2315. /* Have we covered our entire write yet? */
  2316. if ((*cow_start + *cow_len) >= (cpos + write_len))
  2317. break;
  2318. /*
  2319. * If we reach the end of the extent block and don't get enough
  2320. * clusters, continue with the next extent block if possible.
  2321. */
  2322. if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
  2323. eb && eb->h_next_leaf_blk) {
  2324. brelse(eb_bh);
  2325. eb_bh = NULL;
  2326. ret = ocfs2_read_extent_block(INODE_CACHE(inode),
  2327. le64_to_cpu(eb->h_next_leaf_blk),
  2328. &eb_bh);
  2329. if (ret) {
  2330. mlog_errno(ret);
  2331. goto out;
  2332. }
  2333. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2334. el = &eb->h_list;
  2335. i = -1;
  2336. }
  2337. }
  2338. out:
  2339. brelse(eb_bh);
  2340. return ret;
  2341. }
  2342. /*
  2343. * Prepare meta_ac, data_ac and calculate credits when we want to add some
  2344. * num_clusters in data_tree "et" and change the refcount for the old
  2345. * clusters(starting form p_cluster) in the refcount tree.
  2346. *
  2347. * Note:
  2348. * 1. since we may split the old tree, so we at most will need num_clusters + 2
  2349. * more new leaf records.
  2350. * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
  2351. * just give data_ac = NULL.
  2352. */
  2353. static int ocfs2_lock_refcount_allocators(struct super_block *sb,
  2354. u32 p_cluster, u32 num_clusters,
  2355. struct ocfs2_extent_tree *et,
  2356. struct ocfs2_caching_info *ref_ci,
  2357. struct buffer_head *ref_root_bh,
  2358. struct ocfs2_alloc_context **meta_ac,
  2359. struct ocfs2_alloc_context **data_ac,
  2360. int *credits)
  2361. {
  2362. int ret = 0, meta_add = 0;
  2363. int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
  2364. if (num_free_extents < 0) {
  2365. ret = num_free_extents;
  2366. mlog_errno(ret);
  2367. goto out;
  2368. }
  2369. if (num_free_extents < num_clusters + 2)
  2370. meta_add =
  2371. ocfs2_extend_meta_needed(et->et_root_el);
  2372. *credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
  2373. num_clusters + 2);
  2374. ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
  2375. p_cluster, num_clusters,
  2376. &meta_add, credits);
  2377. if (ret) {
  2378. mlog_errno(ret);
  2379. goto out;
  2380. }
  2381. mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
  2382. meta_add, num_clusters, *credits);
  2383. ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
  2384. meta_ac);
  2385. if (ret) {
  2386. mlog_errno(ret);
  2387. goto out;
  2388. }
  2389. if (data_ac) {
  2390. ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
  2391. data_ac);
  2392. if (ret)
  2393. mlog_errno(ret);
  2394. }
  2395. out:
  2396. if (ret) {
  2397. if (*meta_ac) {
  2398. ocfs2_free_alloc_context(*meta_ac);
  2399. *meta_ac = NULL;
  2400. }
  2401. }
  2402. return ret;
  2403. }
  2404. static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
  2405. {
  2406. BUG_ON(buffer_dirty(bh));
  2407. clear_buffer_mapped(bh);
  2408. return 0;
  2409. }
  2410. static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
  2411. struct ocfs2_cow_context *context,
  2412. u32 cpos, u32 old_cluster,
  2413. u32 new_cluster, u32 new_len)
  2414. {
  2415. int ret = 0, partial;
  2416. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2417. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  2418. u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
  2419. struct page *page;
  2420. pgoff_t page_index;
  2421. unsigned int from, to;
  2422. loff_t offset, end, map_end;
  2423. struct address_space *mapping = context->inode->i_mapping;
  2424. mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
  2425. new_cluster, new_len, cpos);
  2426. offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
  2427. end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
  2428. while (offset < end) {
  2429. page_index = offset >> PAGE_CACHE_SHIFT;
  2430. map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
  2431. if (map_end > end)
  2432. map_end = end;
  2433. /* from, to is the offset within the page. */
  2434. from = offset & (PAGE_CACHE_SIZE - 1);
  2435. to = PAGE_CACHE_SIZE;
  2436. if (map_end & (PAGE_CACHE_SIZE - 1))
  2437. to = map_end & (PAGE_CACHE_SIZE - 1);
  2438. page = grab_cache_page(mapping, page_index);
  2439. /* This page can't be dirtied before we CoW it out. */
  2440. BUG_ON(PageDirty(page));
  2441. if (!PageUptodate(page)) {
  2442. ret = block_read_full_page(page, ocfs2_get_block);
  2443. if (ret) {
  2444. mlog_errno(ret);
  2445. goto unlock;
  2446. }
  2447. lock_page(page);
  2448. }
  2449. if (page_has_buffers(page)) {
  2450. ret = walk_page_buffers(handle, page_buffers(page),
  2451. from, to, &partial,
  2452. ocfs2_clear_cow_buffer);
  2453. if (ret) {
  2454. mlog_errno(ret);
  2455. goto unlock;
  2456. }
  2457. }
  2458. ocfs2_map_and_dirty_page(context->inode,
  2459. handle, from, to,
  2460. page, 0, &new_block);
  2461. mark_page_accessed(page);
  2462. unlock:
  2463. unlock_page(page);
  2464. page_cache_release(page);
  2465. page = NULL;
  2466. offset = map_end;
  2467. if (ret)
  2468. break;
  2469. }
  2470. return ret;
  2471. }
  2472. static int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
  2473. struct ocfs2_cow_context *context,
  2474. u32 cpos, u32 old_cluster,
  2475. u32 new_cluster, u32 new_len)
  2476. {
  2477. int ret = 0;
  2478. struct super_block *sb = context->inode->i_sb;
  2479. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2480. int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
  2481. u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
  2482. u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
  2483. struct ocfs2_super *osb = OCFS2_SB(sb);
  2484. struct buffer_head *old_bh = NULL;
  2485. struct buffer_head *new_bh = NULL;
  2486. mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster,
  2487. new_cluster, new_len);
  2488. for (i = 0; i < blocks; i++, old_block++, new_block++) {
  2489. new_bh = sb_getblk(osb->sb, new_block);
  2490. if (new_bh == NULL) {
  2491. ret = -EIO;
  2492. mlog_errno(ret);
  2493. break;
  2494. }
  2495. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  2496. ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
  2497. if (ret) {
  2498. mlog_errno(ret);
  2499. break;
  2500. }
  2501. ret = ocfs2_journal_access(handle, ci, new_bh,
  2502. OCFS2_JOURNAL_ACCESS_CREATE);
  2503. if (ret) {
  2504. mlog_errno(ret);
  2505. break;
  2506. }
  2507. memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
  2508. ret = ocfs2_journal_dirty(handle, new_bh);
  2509. if (ret) {
  2510. mlog_errno(ret);
  2511. break;
  2512. }
  2513. brelse(new_bh);
  2514. brelse(old_bh);
  2515. new_bh = NULL;
  2516. old_bh = NULL;
  2517. }
  2518. brelse(new_bh);
  2519. brelse(old_bh);
  2520. return ret;
  2521. }
  2522. static int ocfs2_clear_ext_refcount(handle_t *handle,
  2523. struct ocfs2_extent_tree *et,
  2524. u32 cpos, u32 p_cluster, u32 len,
  2525. unsigned int ext_flags,
  2526. struct ocfs2_alloc_context *meta_ac,
  2527. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2528. {
  2529. int ret, index;
  2530. struct ocfs2_extent_rec replace_rec;
  2531. struct ocfs2_path *path = NULL;
  2532. struct ocfs2_extent_list *el;
  2533. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2534. u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
  2535. mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
  2536. (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
  2537. memset(&replace_rec, 0, sizeof(replace_rec));
  2538. replace_rec.e_cpos = cpu_to_le32(cpos);
  2539. replace_rec.e_leaf_clusters = cpu_to_le16(len);
  2540. replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
  2541. p_cluster));
  2542. replace_rec.e_flags = ext_flags;
  2543. replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
  2544. path = ocfs2_new_path_from_et(et);
  2545. if (!path) {
  2546. ret = -ENOMEM;
  2547. mlog_errno(ret);
  2548. goto out;
  2549. }
  2550. ret = ocfs2_find_path(et->et_ci, path, cpos);
  2551. if (ret) {
  2552. mlog_errno(ret);
  2553. goto out;
  2554. }
  2555. el = path_leaf_el(path);
  2556. index = ocfs2_search_extent_list(el, cpos);
  2557. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  2558. ocfs2_error(sb,
  2559. "Inode %llu has an extent at cpos %u which can no "
  2560. "longer be found.\n",
  2561. (unsigned long long)ino, cpos);
  2562. ret = -EROFS;
  2563. goto out;
  2564. }
  2565. ret = ocfs2_split_extent(handle, et, path, index,
  2566. &replace_rec, meta_ac, dealloc);
  2567. if (ret)
  2568. mlog_errno(ret);
  2569. out:
  2570. ocfs2_free_path(path);
  2571. return ret;
  2572. }
  2573. static int ocfs2_replace_clusters(handle_t *handle,
  2574. struct ocfs2_cow_context *context,
  2575. u32 cpos, u32 old,
  2576. u32 new, u32 len,
  2577. unsigned int ext_flags)
  2578. {
  2579. int ret;
  2580. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2581. u64 ino = ocfs2_metadata_cache_owner(ci);
  2582. mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
  2583. (unsigned long long)ino, cpos, old, new, len, ext_flags);
  2584. /*If the old clusters is unwritten, no need to duplicate. */
  2585. if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  2586. ret = context->cow_duplicate_clusters(handle, context, cpos,
  2587. old, new, len);
  2588. if (ret) {
  2589. mlog_errno(ret);
  2590. goto out;
  2591. }
  2592. }
  2593. ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
  2594. cpos, new, len, ext_flags,
  2595. context->meta_ac, &context->dealloc);
  2596. if (ret)
  2597. mlog_errno(ret);
  2598. out:
  2599. return ret;
  2600. }
  2601. static int ocfs2_cow_sync_writeback(struct super_block *sb,
  2602. struct ocfs2_cow_context *context,
  2603. u32 cpos, u32 num_clusters)
  2604. {
  2605. int ret = 0;
  2606. loff_t offset, end, map_end;
  2607. pgoff_t page_index;
  2608. struct page *page;
  2609. if (ocfs2_should_order_data(context->inode))
  2610. return 0;
  2611. offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
  2612. end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
  2613. ret = filemap_fdatawrite_range(context->inode->i_mapping,
  2614. offset, end - 1);
  2615. if (ret < 0) {
  2616. mlog_errno(ret);
  2617. return ret;
  2618. }
  2619. while (offset < end) {
  2620. page_index = offset >> PAGE_CACHE_SHIFT;
  2621. map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
  2622. if (map_end > end)
  2623. map_end = end;
  2624. page = grab_cache_page(context->inode->i_mapping, page_index);
  2625. BUG_ON(!page);
  2626. wait_on_page_writeback(page);
  2627. if (PageError(page)) {
  2628. ret = -EIO;
  2629. mlog_errno(ret);
  2630. } else
  2631. mark_page_accessed(page);
  2632. unlock_page(page);
  2633. page_cache_release(page);
  2634. page = NULL;
  2635. offset = map_end;
  2636. if (ret)
  2637. break;
  2638. }
  2639. return ret;
  2640. }
  2641. static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
  2642. u32 v_cluster, u32 *p_cluster,
  2643. u32 *num_clusters,
  2644. unsigned int *extent_flags)
  2645. {
  2646. return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
  2647. num_clusters, extent_flags);
  2648. }
  2649. static int ocfs2_make_clusters_writable(struct super_block *sb,
  2650. struct ocfs2_cow_context *context,
  2651. u32 cpos, u32 p_cluster,
  2652. u32 num_clusters, unsigned int e_flags)
  2653. {
  2654. int ret, delete, index, credits = 0;
  2655. u32 new_bit, new_len;
  2656. unsigned int set_len;
  2657. struct ocfs2_super *osb = OCFS2_SB(sb);
  2658. handle_t *handle;
  2659. struct buffer_head *ref_leaf_bh = NULL;
  2660. struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
  2661. struct ocfs2_refcount_rec rec;
  2662. mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
  2663. cpos, p_cluster, num_clusters, e_flags);
  2664. ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
  2665. &context->data_et,
  2666. ref_ci,
  2667. context->ref_root_bh,
  2668. &context->meta_ac,
  2669. &context->data_ac, &credits);
  2670. if (ret) {
  2671. mlog_errno(ret);
  2672. return ret;
  2673. }
  2674. if (context->post_refcount)
  2675. credits += context->post_refcount->credits;
  2676. credits += context->extra_credits;
  2677. handle = ocfs2_start_trans(osb, credits);
  2678. if (IS_ERR(handle)) {
  2679. ret = PTR_ERR(handle);
  2680. mlog_errno(ret);
  2681. goto out;
  2682. }
  2683. while (num_clusters) {
  2684. ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
  2685. p_cluster, num_clusters,
  2686. &rec, &index, &ref_leaf_bh);
  2687. if (ret) {
  2688. mlog_errno(ret);
  2689. goto out_commit;
  2690. }
  2691. BUG_ON(!rec.r_refcount);
  2692. set_len = min((u64)p_cluster + num_clusters,
  2693. le64_to_cpu(rec.r_cpos) +
  2694. le32_to_cpu(rec.r_clusters)) - p_cluster;
  2695. /*
  2696. * There are many different situation here.
  2697. * 1. If refcount == 1, remove the flag and don't COW.
  2698. * 2. If refcount > 1, allocate clusters.
  2699. * Here we may not allocate r_len once at a time, so continue
  2700. * until we reach num_clusters.
  2701. */
  2702. if (le32_to_cpu(rec.r_refcount) == 1) {
  2703. delete = 0;
  2704. ret = ocfs2_clear_ext_refcount(handle,
  2705. &context->data_et,
  2706. cpos, p_cluster,
  2707. set_len, e_flags,
  2708. context->meta_ac,
  2709. &context->dealloc);
  2710. if (ret) {
  2711. mlog_errno(ret);
  2712. goto out_commit;
  2713. }
  2714. } else {
  2715. delete = 1;
  2716. ret = __ocfs2_claim_clusters(osb, handle,
  2717. context->data_ac,
  2718. 1, set_len,
  2719. &new_bit, &new_len);
  2720. if (ret) {
  2721. mlog_errno(ret);
  2722. goto out_commit;
  2723. }
  2724. ret = ocfs2_replace_clusters(handle, context,
  2725. cpos, p_cluster, new_bit,
  2726. new_len, e_flags);
  2727. if (ret) {
  2728. mlog_errno(ret);
  2729. goto out_commit;
  2730. }
  2731. set_len = new_len;
  2732. }
  2733. ret = __ocfs2_decrease_refcount(handle, ref_ci,
  2734. context->ref_root_bh,
  2735. p_cluster, set_len,
  2736. context->meta_ac,
  2737. &context->dealloc, delete);
  2738. if (ret) {
  2739. mlog_errno(ret);
  2740. goto out_commit;
  2741. }
  2742. cpos += set_len;
  2743. p_cluster += set_len;
  2744. num_clusters -= set_len;
  2745. brelse(ref_leaf_bh);
  2746. ref_leaf_bh = NULL;
  2747. }
  2748. /* handle any post_cow action. */
  2749. if (context->post_refcount && context->post_refcount->func) {
  2750. ret = context->post_refcount->func(context->inode, handle,
  2751. context->post_refcount->para);
  2752. if (ret) {
  2753. mlog_errno(ret);
  2754. goto out_commit;
  2755. }
  2756. }
  2757. /*
  2758. * Here we should write the new page out first if we are
  2759. * in write-back mode.
  2760. */
  2761. if (context->get_clusters == ocfs2_di_get_clusters) {
  2762. ret = ocfs2_cow_sync_writeback(sb, context, cpos, num_clusters);
  2763. if (ret)
  2764. mlog_errno(ret);
  2765. }
  2766. out_commit:
  2767. ocfs2_commit_trans(osb, handle);
  2768. out:
  2769. if (context->data_ac) {
  2770. ocfs2_free_alloc_context(context->data_ac);
  2771. context->data_ac = NULL;
  2772. }
  2773. if (context->meta_ac) {
  2774. ocfs2_free_alloc_context(context->meta_ac);
  2775. context->meta_ac = NULL;
  2776. }
  2777. brelse(ref_leaf_bh);
  2778. return ret;
  2779. }
  2780. static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
  2781. {
  2782. int ret = 0;
  2783. struct inode *inode = context->inode;
  2784. u32 cow_start = context->cow_start, cow_len = context->cow_len;
  2785. u32 p_cluster, num_clusters;
  2786. unsigned int ext_flags;
  2787. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2788. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  2789. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  2790. "tree, but the feature bit is not set in the "
  2791. "super block.", inode->i_ino);
  2792. return -EROFS;
  2793. }
  2794. ocfs2_init_dealloc_ctxt(&context->dealloc);
  2795. while (cow_len) {
  2796. ret = context->get_clusters(context, cow_start, &p_cluster,
  2797. &num_clusters, &ext_flags);
  2798. if (ret) {
  2799. mlog_errno(ret);
  2800. break;
  2801. }
  2802. BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
  2803. if (cow_len < num_clusters)
  2804. num_clusters = cow_len;
  2805. ret = ocfs2_make_clusters_writable(inode->i_sb, context,
  2806. cow_start, p_cluster,
  2807. num_clusters, ext_flags);
  2808. if (ret) {
  2809. mlog_errno(ret);
  2810. break;
  2811. }
  2812. cow_len -= num_clusters;
  2813. cow_start += num_clusters;
  2814. }
  2815. if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
  2816. ocfs2_schedule_truncate_log_flush(osb, 1);
  2817. ocfs2_run_deallocs(osb, &context->dealloc);
  2818. }
  2819. return ret;
  2820. }
  2821. /*
  2822. * Starting at cpos, try to CoW write_len clusters. Don't CoW
  2823. * past max_cpos. This will stop when it runs into a hole or an
  2824. * unrefcounted extent.
  2825. */
  2826. static int ocfs2_refcount_cow_hunk(struct inode *inode,
  2827. struct buffer_head *di_bh,
  2828. u32 cpos, u32 write_len, u32 max_cpos)
  2829. {
  2830. int ret;
  2831. u32 cow_start = 0, cow_len = 0;
  2832. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2833. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2834. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2835. struct buffer_head *ref_root_bh = NULL;
  2836. struct ocfs2_refcount_tree *ref_tree;
  2837. struct ocfs2_cow_context *context = NULL;
  2838. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  2839. ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
  2840. cpos, write_len, max_cpos,
  2841. &cow_start, &cow_len);
  2842. if (ret) {
  2843. mlog_errno(ret);
  2844. goto out;
  2845. }
  2846. mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
  2847. "cow_len %u\n", inode->i_ino,
  2848. cpos, write_len, cow_start, cow_len);
  2849. BUG_ON(cow_len == 0);
  2850. context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
  2851. if (!context) {
  2852. ret = -ENOMEM;
  2853. mlog_errno(ret);
  2854. goto out;
  2855. }
  2856. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  2857. 1, &ref_tree, &ref_root_bh);
  2858. if (ret) {
  2859. mlog_errno(ret);
  2860. goto out;
  2861. }
  2862. context->inode = inode;
  2863. context->cow_start = cow_start;
  2864. context->cow_len = cow_len;
  2865. context->ref_tree = ref_tree;
  2866. context->ref_root_bh = ref_root_bh;
  2867. context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
  2868. context->get_clusters = ocfs2_di_get_clusters;
  2869. ocfs2_init_dinode_extent_tree(&context->data_et,
  2870. INODE_CACHE(inode), di_bh);
  2871. ret = ocfs2_replace_cow(context);
  2872. if (ret)
  2873. mlog_errno(ret);
  2874. /*
  2875. * truncate the extent map here since no matter whether we meet with
  2876. * any error during the action, we shouldn't trust cached extent map
  2877. * any more.
  2878. */
  2879. ocfs2_extent_map_trunc(inode, cow_start);
  2880. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  2881. brelse(ref_root_bh);
  2882. out:
  2883. kfree(context);
  2884. return ret;
  2885. }
  2886. /*
  2887. * CoW any and all clusters between cpos and cpos+write_len.
  2888. * Don't CoW past max_cpos. If this returns successfully, all
  2889. * clusters between cpos and cpos+write_len are safe to modify.
  2890. */
  2891. int ocfs2_refcount_cow(struct inode *inode,
  2892. struct buffer_head *di_bh,
  2893. u32 cpos, u32 write_len, u32 max_cpos)
  2894. {
  2895. int ret = 0;
  2896. u32 p_cluster, num_clusters;
  2897. unsigned int ext_flags;
  2898. while (write_len) {
  2899. ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
  2900. &num_clusters, &ext_flags);
  2901. if (ret) {
  2902. mlog_errno(ret);
  2903. break;
  2904. }
  2905. if (write_len < num_clusters)
  2906. num_clusters = write_len;
  2907. if (ext_flags & OCFS2_EXT_REFCOUNTED) {
  2908. ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
  2909. num_clusters, max_cpos);
  2910. if (ret) {
  2911. mlog_errno(ret);
  2912. break;
  2913. }
  2914. }
  2915. write_len -= num_clusters;
  2916. cpos += num_clusters;
  2917. }
  2918. return ret;
  2919. }
  2920. static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
  2921. u32 v_cluster, u32 *p_cluster,
  2922. u32 *num_clusters,
  2923. unsigned int *extent_flags)
  2924. {
  2925. struct inode *inode = context->inode;
  2926. struct ocfs2_xattr_value_root *xv = context->cow_object;
  2927. return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
  2928. num_clusters, &xv->xr_list,
  2929. extent_flags);
  2930. }
  2931. /*
  2932. * Given a xattr value root, calculate the most meta/credits we need for
  2933. * refcount tree change if we truncate it to 0.
  2934. */
  2935. int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
  2936. struct ocfs2_caching_info *ref_ci,
  2937. struct buffer_head *ref_root_bh,
  2938. struct ocfs2_xattr_value_root *xv,
  2939. int *meta_add, int *credits)
  2940. {
  2941. int ret = 0, index, ref_blocks = 0;
  2942. u32 p_cluster, num_clusters;
  2943. u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
  2944. struct ocfs2_refcount_block *rb;
  2945. struct ocfs2_refcount_rec rec;
  2946. struct buffer_head *ref_leaf_bh = NULL;
  2947. while (cpos < clusters) {
  2948. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  2949. &num_clusters, &xv->xr_list,
  2950. NULL);
  2951. if (ret) {
  2952. mlog_errno(ret);
  2953. goto out;
  2954. }
  2955. cpos += num_clusters;
  2956. while (num_clusters) {
  2957. ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
  2958. p_cluster, num_clusters,
  2959. &rec, &index,
  2960. &ref_leaf_bh);
  2961. if (ret) {
  2962. mlog_errno(ret);
  2963. goto out;
  2964. }
  2965. BUG_ON(!rec.r_refcount);
  2966. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  2967. /*
  2968. * We really don't know whether the other clusters is in
  2969. * this refcount block or not, so just take the worst
  2970. * case that all the clusters are in this block and each
  2971. * one will split a refcount rec, so totally we need
  2972. * clusters * 2 new refcount rec.
  2973. */
  2974. if (le64_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
  2975. le16_to_cpu(rb->rf_records.rl_count))
  2976. ref_blocks++;
  2977. *credits += 1;
  2978. brelse(ref_leaf_bh);
  2979. ref_leaf_bh = NULL;
  2980. if (num_clusters <= le32_to_cpu(rec.r_clusters))
  2981. break;
  2982. else
  2983. num_clusters -= le32_to_cpu(rec.r_clusters);
  2984. p_cluster += num_clusters;
  2985. }
  2986. }
  2987. *meta_add += ref_blocks;
  2988. if (!ref_blocks)
  2989. goto out;
  2990. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  2991. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
  2992. *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  2993. else {
  2994. struct ocfs2_extent_tree et;
  2995. ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
  2996. *credits += ocfs2_calc_extend_credits(inode->i_sb,
  2997. et.et_root_el,
  2998. ref_blocks);
  2999. }
  3000. out:
  3001. brelse(ref_leaf_bh);
  3002. return ret;
  3003. }
  3004. /*
  3005. * Do CoW for xattr.
  3006. */
  3007. int ocfs2_refcount_cow_xattr(struct inode *inode,
  3008. struct ocfs2_dinode *di,
  3009. struct ocfs2_xattr_value_buf *vb,
  3010. struct ocfs2_refcount_tree *ref_tree,
  3011. struct buffer_head *ref_root_bh,
  3012. u32 cpos, u32 write_len,
  3013. struct ocfs2_post_refcount *post)
  3014. {
  3015. int ret;
  3016. struct ocfs2_xattr_value_root *xv = vb->vb_xv;
  3017. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3018. struct ocfs2_cow_context *context = NULL;
  3019. u32 cow_start, cow_len;
  3020. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  3021. ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
  3022. cpos, write_len, UINT_MAX,
  3023. &cow_start, &cow_len);
  3024. if (ret) {
  3025. mlog_errno(ret);
  3026. goto out;
  3027. }
  3028. BUG_ON(cow_len == 0);
  3029. context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
  3030. if (!context) {
  3031. ret = -ENOMEM;
  3032. mlog_errno(ret);
  3033. goto out;
  3034. }
  3035. context->inode = inode;
  3036. context->cow_start = cow_start;
  3037. context->cow_len = cow_len;
  3038. context->ref_tree = ref_tree;
  3039. context->ref_root_bh = ref_root_bh;;
  3040. context->cow_object = xv;
  3041. context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
  3042. /* We need the extra credits for duplicate_clusters by jbd. */
  3043. context->extra_credits =
  3044. ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
  3045. context->get_clusters = ocfs2_xattr_value_get_clusters;
  3046. context->post_refcount = post;
  3047. ocfs2_init_xattr_value_extent_tree(&context->data_et,
  3048. INODE_CACHE(inode), vb);
  3049. ret = ocfs2_replace_cow(context);
  3050. if (ret)
  3051. mlog_errno(ret);
  3052. out:
  3053. kfree(context);
  3054. return ret;
  3055. }
  3056. /*
  3057. * Insert a new extent into refcount tree and mark a extent rec
  3058. * as refcounted in the dinode tree.
  3059. */
  3060. int ocfs2_add_refcount_flag(struct inode *inode,
  3061. struct ocfs2_extent_tree *data_et,
  3062. struct ocfs2_caching_info *ref_ci,
  3063. struct buffer_head *ref_root_bh,
  3064. u32 cpos, u32 p_cluster, u32 num_clusters,
  3065. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3066. struct ocfs2_post_refcount *post)
  3067. {
  3068. int ret;
  3069. handle_t *handle;
  3070. int credits = 1, ref_blocks = 0;
  3071. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3072. struct ocfs2_alloc_context *meta_ac = NULL;
  3073. ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
  3074. ref_ci, ref_root_bh,
  3075. p_cluster, num_clusters,
  3076. &ref_blocks, &credits);
  3077. if (ret) {
  3078. mlog_errno(ret);
  3079. goto out;
  3080. }
  3081. mlog(0, "reserve new metadata %d, credits = %d\n",
  3082. ref_blocks, credits);
  3083. if (ref_blocks) {
  3084. ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
  3085. ref_blocks, &meta_ac);
  3086. if (ret) {
  3087. mlog_errno(ret);
  3088. goto out;
  3089. }
  3090. }
  3091. if (post)
  3092. credits += post->credits;
  3093. handle = ocfs2_start_trans(osb, credits);
  3094. if (IS_ERR(handle)) {
  3095. ret = PTR_ERR(handle);
  3096. mlog_errno(ret);
  3097. goto out;
  3098. }
  3099. ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
  3100. cpos, num_clusters, p_cluster,
  3101. meta_ac, dealloc);
  3102. if (ret) {
  3103. mlog_errno(ret);
  3104. goto out_commit;
  3105. }
  3106. ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
  3107. p_cluster, num_clusters,
  3108. meta_ac, dealloc);
  3109. if (ret) {
  3110. mlog_errno(ret);
  3111. goto out_commit;
  3112. }
  3113. if (post && post->func) {
  3114. ret = post->func(inode, handle, post->para);
  3115. if (ret)
  3116. mlog_errno(ret);
  3117. }
  3118. out_commit:
  3119. ocfs2_commit_trans(osb, handle);
  3120. out:
  3121. if (meta_ac)
  3122. ocfs2_free_alloc_context(meta_ac);
  3123. return ret;
  3124. }
  3125. static int ocfs2_change_ctime(struct inode *inode,
  3126. struct buffer_head *di_bh)
  3127. {
  3128. int ret;
  3129. handle_t *handle;
  3130. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3131. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
  3132. OCFS2_INODE_UPDATE_CREDITS);
  3133. if (IS_ERR(handle)) {
  3134. ret = PTR_ERR(handle);
  3135. mlog_errno(ret);
  3136. goto out;
  3137. }
  3138. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  3139. OCFS2_JOURNAL_ACCESS_WRITE);
  3140. if (ret) {
  3141. mlog_errno(ret);
  3142. goto out_commit;
  3143. }
  3144. inode->i_ctime = CURRENT_TIME;
  3145. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  3146. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  3147. ocfs2_journal_dirty(handle, di_bh);
  3148. out_commit:
  3149. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  3150. out:
  3151. return ret;
  3152. }
  3153. static int ocfs2_attach_refcount_tree(struct inode *inode,
  3154. struct buffer_head *di_bh)
  3155. {
  3156. int ret, data_changed = 0;
  3157. struct buffer_head *ref_root_bh = NULL;
  3158. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3159. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3160. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3161. struct ocfs2_refcount_tree *ref_tree;
  3162. unsigned int ext_flags;
  3163. loff_t size;
  3164. u32 cpos, num_clusters, clusters, p_cluster;
  3165. struct ocfs2_cached_dealloc_ctxt dealloc;
  3166. struct ocfs2_extent_tree di_et;
  3167. ocfs2_init_dealloc_ctxt(&dealloc);
  3168. if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
  3169. ret = ocfs2_create_refcount_tree(inode, di_bh);
  3170. if (ret) {
  3171. mlog_errno(ret);
  3172. goto out;
  3173. }
  3174. }
  3175. BUG_ON(!di->i_refcount_loc);
  3176. ret = ocfs2_lock_refcount_tree(osb,
  3177. le64_to_cpu(di->i_refcount_loc), 1,
  3178. &ref_tree, &ref_root_bh);
  3179. if (ret) {
  3180. mlog_errno(ret);
  3181. goto out;
  3182. }
  3183. ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
  3184. size = i_size_read(inode);
  3185. clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
  3186. cpos = 0;
  3187. while (cpos < clusters) {
  3188. ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
  3189. &num_clusters, &ext_flags);
  3190. if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
  3191. ret = ocfs2_add_refcount_flag(inode, &di_et,
  3192. &ref_tree->rf_ci,
  3193. ref_root_bh, cpos,
  3194. p_cluster, num_clusters,
  3195. &dealloc, NULL);
  3196. if (ret) {
  3197. mlog_errno(ret);
  3198. goto unlock;
  3199. }
  3200. data_changed = 1;
  3201. }
  3202. cpos += num_clusters;
  3203. }
  3204. if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
  3205. ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
  3206. &ref_tree->rf_ci,
  3207. ref_root_bh,
  3208. &dealloc);
  3209. if (ret) {
  3210. mlog_errno(ret);
  3211. goto unlock;
  3212. }
  3213. }
  3214. if (data_changed) {
  3215. ret = ocfs2_change_ctime(inode, di_bh);
  3216. if (ret)
  3217. mlog_errno(ret);
  3218. }
  3219. unlock:
  3220. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3221. brelse(ref_root_bh);
  3222. if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
  3223. ocfs2_schedule_truncate_log_flush(osb, 1);
  3224. ocfs2_run_deallocs(osb, &dealloc);
  3225. }
  3226. out:
  3227. /*
  3228. * Empty the extent map so that we may get the right extent
  3229. * record from the disk.
  3230. */
  3231. ocfs2_extent_map_trunc(inode, 0);
  3232. return ret;
  3233. }
  3234. static int ocfs2_add_refcounted_extent(struct inode *inode,
  3235. struct ocfs2_extent_tree *et,
  3236. struct ocfs2_caching_info *ref_ci,
  3237. struct buffer_head *ref_root_bh,
  3238. u32 cpos, u32 p_cluster, u32 num_clusters,
  3239. unsigned int ext_flags,
  3240. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3241. {
  3242. int ret;
  3243. handle_t *handle;
  3244. int credits = 0;
  3245. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3246. struct ocfs2_alloc_context *meta_ac = NULL;
  3247. ret = ocfs2_lock_refcount_allocators(inode->i_sb,
  3248. p_cluster, num_clusters,
  3249. et, ref_ci,
  3250. ref_root_bh, &meta_ac,
  3251. NULL, &credits);
  3252. if (ret) {
  3253. mlog_errno(ret);
  3254. goto out;
  3255. }
  3256. handle = ocfs2_start_trans(osb, credits);
  3257. if (IS_ERR(handle)) {
  3258. ret = PTR_ERR(handle);
  3259. mlog_errno(ret);
  3260. goto out;
  3261. }
  3262. ret = ocfs2_insert_extent(handle, et, cpos,
  3263. cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
  3264. p_cluster)),
  3265. num_clusters, ext_flags, meta_ac);
  3266. if (ret) {
  3267. mlog_errno(ret);
  3268. goto out_commit;
  3269. }
  3270. ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
  3271. p_cluster, num_clusters,
  3272. meta_ac, dealloc);
  3273. if (ret)
  3274. mlog_errno(ret);
  3275. out_commit:
  3276. ocfs2_commit_trans(osb, handle);
  3277. out:
  3278. if (meta_ac)
  3279. ocfs2_free_alloc_context(meta_ac);
  3280. return ret;
  3281. }
  3282. static int ocfs2_duplicate_extent_list(struct inode *s_inode,
  3283. struct inode *t_inode,
  3284. struct buffer_head *t_bh,
  3285. struct ocfs2_caching_info *ref_ci,
  3286. struct buffer_head *ref_root_bh,
  3287. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3288. {
  3289. int ret = 0;
  3290. u32 p_cluster, num_clusters, clusters, cpos;
  3291. loff_t size;
  3292. unsigned int ext_flags;
  3293. struct ocfs2_extent_tree et;
  3294. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
  3295. size = i_size_read(s_inode);
  3296. clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
  3297. cpos = 0;
  3298. while (cpos < clusters) {
  3299. ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
  3300. &num_clusters, &ext_flags);
  3301. if (p_cluster) {
  3302. ret = ocfs2_add_refcounted_extent(t_inode, &et,
  3303. ref_ci, ref_root_bh,
  3304. cpos, p_cluster,
  3305. num_clusters,
  3306. ext_flags,
  3307. dealloc);
  3308. if (ret) {
  3309. mlog_errno(ret);
  3310. goto out;
  3311. }
  3312. }
  3313. cpos += num_clusters;
  3314. }
  3315. out:
  3316. return ret;
  3317. }
  3318. /*
  3319. * change the new file's attributes to the src.
  3320. *
  3321. * reflink creates a snapshot of a file, that means the attributes
  3322. * must be identical except for three exceptions - nlink, ino, and ctime.
  3323. */
  3324. static int ocfs2_complete_reflink(struct inode *s_inode,
  3325. struct buffer_head *s_bh,
  3326. struct inode *t_inode,
  3327. struct buffer_head *t_bh)
  3328. {
  3329. int ret;
  3330. handle_t *handle;
  3331. struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
  3332. struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
  3333. loff_t size = i_size_read(s_inode);
  3334. handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
  3335. OCFS2_INODE_UPDATE_CREDITS);
  3336. if (IS_ERR(handle)) {
  3337. ret = PTR_ERR(handle);
  3338. mlog_errno(ret);
  3339. return ret;
  3340. }
  3341. ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
  3342. OCFS2_JOURNAL_ACCESS_WRITE);
  3343. if (ret) {
  3344. mlog_errno(ret);
  3345. goto out_commit;
  3346. }
  3347. spin_lock(&OCFS2_I(t_inode)->ip_lock);
  3348. OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
  3349. OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
  3350. OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
  3351. spin_unlock(&OCFS2_I(t_inode)->ip_lock);
  3352. i_size_write(t_inode, size);
  3353. di->i_xattr_inline_size = s_di->i_xattr_inline_size;
  3354. di->i_clusters = s_di->i_clusters;
  3355. di->i_size = s_di->i_size;
  3356. di->i_dyn_features = s_di->i_dyn_features;
  3357. di->i_attr = s_di->i_attr;
  3358. di->i_uid = s_di->i_uid;
  3359. di->i_gid = s_di->i_gid;
  3360. di->i_mode = s_di->i_mode;
  3361. /*
  3362. * update time.
  3363. * we want mtime to appear identical to the source and update ctime.
  3364. */
  3365. t_inode->i_ctime = CURRENT_TIME;
  3366. di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
  3367. di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
  3368. t_inode->i_mtime = s_inode->i_mtime;
  3369. di->i_mtime = s_di->i_mtime;
  3370. di->i_mtime_nsec = s_di->i_mtime_nsec;
  3371. ocfs2_journal_dirty(handle, t_bh);
  3372. out_commit:
  3373. ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
  3374. return ret;
  3375. }
  3376. static int ocfs2_create_reflink_node(struct inode *s_inode,
  3377. struct buffer_head *s_bh,
  3378. struct inode *t_inode,
  3379. struct buffer_head *t_bh)
  3380. {
  3381. int ret;
  3382. struct buffer_head *ref_root_bh = NULL;
  3383. struct ocfs2_cached_dealloc_ctxt dealloc;
  3384. struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
  3385. struct ocfs2_refcount_block *rb;
  3386. struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
  3387. struct ocfs2_refcount_tree *ref_tree;
  3388. ocfs2_init_dealloc_ctxt(&dealloc);
  3389. ret = ocfs2_set_refcount_tree(t_inode, t_bh,
  3390. le64_to_cpu(di->i_refcount_loc));
  3391. if (ret) {
  3392. mlog_errno(ret);
  3393. goto out;
  3394. }
  3395. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  3396. 1, &ref_tree, &ref_root_bh);
  3397. if (ret) {
  3398. mlog_errno(ret);
  3399. goto out;
  3400. }
  3401. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  3402. ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
  3403. &ref_tree->rf_ci, ref_root_bh,
  3404. &dealloc);
  3405. if (ret) {
  3406. mlog_errno(ret);
  3407. goto out_unlock_refcount;
  3408. }
  3409. ret = ocfs2_complete_reflink(s_inode, s_bh, t_inode, t_bh);
  3410. if (ret)
  3411. mlog_errno(ret);
  3412. out_unlock_refcount:
  3413. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3414. brelse(ref_root_bh);
  3415. out:
  3416. if (ocfs2_dealloc_has_cluster(&dealloc)) {
  3417. ocfs2_schedule_truncate_log_flush(osb, 1);
  3418. ocfs2_run_deallocs(osb, &dealloc);
  3419. }
  3420. return ret;
  3421. }