refcounttree.c 113 KB

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