refcounttree.c 112 KB

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