refcounttree.c 109 KB

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