refcounttree.c 113 KB

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