refcounttree.c 112 KB

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