refcounttree.c 112 KB

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