refcounttree.c 120 KB

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