refcounttree.c 120 KB

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