qgroup.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 STRATO. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/writeback.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/slab.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/btrfs.h>
  13. #include <linux/sched/mm.h>
  14. #include "ctree.h"
  15. #include "transaction.h"
  16. #include "disk-io.h"
  17. #include "locking.h"
  18. #include "ulist.h"
  19. #include "backref.h"
  20. #include "extent_io.h"
  21. #include "qgroup.h"
  22. #include "block-group.h"
  23. #include "sysfs.h"
  24. #include "tree-mod-log.h"
  25. #include "fs.h"
  26. #include "accessors.h"
  27. #include "extent-tree.h"
  28. #include "root-tree.h"
  29. #include "tree-checker.h"
  30. enum btrfs_qgroup_mode btrfs_qgroup_mode(const struct btrfs_fs_info *fs_info)
  31. {
  32. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  33. return BTRFS_QGROUP_MODE_DISABLED;
  34. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
  35. return BTRFS_QGROUP_MODE_SIMPLE;
  36. return BTRFS_QGROUP_MODE_FULL;
  37. }
  38. bool btrfs_qgroup_enabled(const struct btrfs_fs_info *fs_info)
  39. {
  40. return btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_DISABLED;
  41. }
  42. bool btrfs_qgroup_full_accounting(const struct btrfs_fs_info *fs_info)
  43. {
  44. return btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL;
  45. }
  46. /*
  47. * Helpers to access qgroup reservation
  48. *
  49. * Callers should ensure the lock context and type are valid
  50. */
  51. static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
  52. {
  53. u64 ret = 0;
  54. int i;
  55. for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
  56. ret += qgroup->rsv.values[i];
  57. return ret;
  58. }
  59. #ifdef CONFIG_BTRFS_DEBUG
  60. static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
  61. {
  62. if (type == BTRFS_QGROUP_RSV_DATA)
  63. return "data";
  64. if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
  65. return "meta_pertrans";
  66. if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
  67. return "meta_prealloc";
  68. return NULL;
  69. }
  70. #endif
  71. static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
  72. struct btrfs_qgroup *qgroup, u64 num_bytes,
  73. enum btrfs_qgroup_rsv_type type)
  74. {
  75. trace_btrfs_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
  76. qgroup->rsv.values[type] += num_bytes;
  77. }
  78. static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
  79. struct btrfs_qgroup *qgroup, u64 num_bytes,
  80. enum btrfs_qgroup_rsv_type type)
  81. {
  82. trace_btrfs_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
  83. if (qgroup->rsv.values[type] >= num_bytes) {
  84. qgroup->rsv.values[type] -= num_bytes;
  85. return;
  86. }
  87. #ifdef CONFIG_BTRFS_DEBUG
  88. WARN_RATELIMIT(1,
  89. "qgroup %llu %s reserved space underflow, have %llu to free %llu",
  90. qgroup->qgroupid, qgroup_rsv_type_str(type),
  91. qgroup->rsv.values[type], num_bytes);
  92. #endif
  93. qgroup->rsv.values[type] = 0;
  94. }
  95. static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
  96. struct btrfs_qgroup *dest,
  97. const struct btrfs_qgroup *src)
  98. {
  99. int i;
  100. for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
  101. qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
  102. }
  103. static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
  104. struct btrfs_qgroup *dest,
  105. const struct btrfs_qgroup *src)
  106. {
  107. int i;
  108. for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
  109. qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
  110. }
  111. static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
  112. int mod)
  113. {
  114. if (qg->old_refcnt < seq)
  115. qg->old_refcnt = seq;
  116. qg->old_refcnt += mod;
  117. }
  118. static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
  119. int mod)
  120. {
  121. if (qg->new_refcnt < seq)
  122. qg->new_refcnt = seq;
  123. qg->new_refcnt += mod;
  124. }
  125. static inline u64 btrfs_qgroup_get_old_refcnt(const struct btrfs_qgroup *qg, u64 seq)
  126. {
  127. if (qg->old_refcnt < seq)
  128. return 0;
  129. return qg->old_refcnt - seq;
  130. }
  131. static inline u64 btrfs_qgroup_get_new_refcnt(const struct btrfs_qgroup *qg, u64 seq)
  132. {
  133. if (qg->new_refcnt < seq)
  134. return 0;
  135. return qg->new_refcnt - seq;
  136. }
  137. static int
  138. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  139. int init_flags);
  140. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  141. static int btrfs_qgroup_qgroupid_key_cmp(const void *key, const struct rb_node *node)
  142. {
  143. const u64 *qgroupid = key;
  144. const struct btrfs_qgroup *qgroup = rb_entry(node, struct btrfs_qgroup, node);
  145. if (qgroup->qgroupid < *qgroupid)
  146. return -1;
  147. else if (qgroup->qgroupid > *qgroupid)
  148. return 1;
  149. return 0;
  150. }
  151. /* must be called with qgroup_ioctl_lock held */
  152. static struct btrfs_qgroup *find_qgroup_rb(const struct btrfs_fs_info *fs_info,
  153. u64 qgroupid)
  154. {
  155. struct rb_node *node;
  156. node = rb_find(&qgroupid, &fs_info->qgroup_tree, btrfs_qgroup_qgroupid_key_cmp);
  157. return rb_entry_safe(node, struct btrfs_qgroup, node);
  158. }
  159. static int btrfs_qgroup_qgroupid_cmp(struct rb_node *new, const struct rb_node *existing)
  160. {
  161. const struct btrfs_qgroup *new_qgroup = rb_entry(new, struct btrfs_qgroup, node);
  162. return btrfs_qgroup_qgroupid_key_cmp(&new_qgroup->qgroupid, existing);
  163. }
  164. /*
  165. * Add qgroup to the filesystem's qgroup tree.
  166. *
  167. * Must be called with qgroup_lock held and @prealloc preallocated.
  168. *
  169. * The control on the lifespan of @prealloc would be transferred to this
  170. * function, thus caller should no longer touch @prealloc.
  171. */
  172. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  173. struct btrfs_qgroup *prealloc,
  174. u64 qgroupid)
  175. {
  176. struct rb_node *node;
  177. /* Caller must have pre-allocated @prealloc. */
  178. ASSERT(prealloc);
  179. prealloc->qgroupid = qgroupid;
  180. node = rb_find_add(&prealloc->node, &fs_info->qgroup_tree, btrfs_qgroup_qgroupid_cmp);
  181. if (node) {
  182. kfree(prealloc);
  183. return rb_entry(node, struct btrfs_qgroup, node);
  184. }
  185. INIT_LIST_HEAD(&prealloc->groups);
  186. INIT_LIST_HEAD(&prealloc->members);
  187. INIT_LIST_HEAD(&prealloc->dirty);
  188. INIT_LIST_HEAD(&prealloc->iterator);
  189. INIT_LIST_HEAD(&prealloc->nested_iterator);
  190. return prealloc;
  191. }
  192. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  193. {
  194. struct btrfs_qgroup_list *list;
  195. list_del(&qgroup->dirty);
  196. while (!list_empty(&qgroup->groups)) {
  197. list = list_first_entry(&qgroup->groups,
  198. struct btrfs_qgroup_list, next_group);
  199. list_del(&list->next_group);
  200. list_del(&list->next_member);
  201. kfree(list);
  202. }
  203. while (!list_empty(&qgroup->members)) {
  204. list = list_first_entry(&qgroup->members,
  205. struct btrfs_qgroup_list, next_member);
  206. list_del(&list->next_group);
  207. list_del(&list->next_member);
  208. kfree(list);
  209. }
  210. }
  211. /* must be called with qgroup_lock held */
  212. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  213. {
  214. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  215. if (!qgroup)
  216. return -ENOENT;
  217. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  218. __del_qgroup_rb(qgroup);
  219. return 0;
  220. }
  221. /*
  222. * Add relation specified by two qgroups.
  223. *
  224. * Must be called with qgroup_lock held, the ownership of @prealloc is
  225. * transferred to this function and caller should not touch it anymore.
  226. *
  227. * Return: 0 on success
  228. * -ENOENT if one of the qgroups is NULL
  229. * <0 other errors
  230. */
  231. static int __add_relation_rb(struct btrfs_qgroup_list *prealloc,
  232. struct btrfs_qgroup *member,
  233. struct btrfs_qgroup *parent)
  234. {
  235. if (!member || !parent) {
  236. kfree(prealloc);
  237. return -ENOENT;
  238. }
  239. prealloc->group = parent;
  240. prealloc->member = member;
  241. list_add_tail(&prealloc->next_group, &member->groups);
  242. list_add_tail(&prealloc->next_member, &parent->members);
  243. return 0;
  244. }
  245. /*
  246. * Add relation specified by two qgroup ids.
  247. *
  248. * Must be called with qgroup_lock held.
  249. *
  250. * Return: 0 on success
  251. * -ENOENT if one of the ids does not exist
  252. * <0 other errors
  253. */
  254. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  255. struct btrfs_qgroup_list *prealloc,
  256. u64 memberid, u64 parentid)
  257. {
  258. struct btrfs_qgroup *member;
  259. struct btrfs_qgroup *parent;
  260. member = find_qgroup_rb(fs_info, memberid);
  261. parent = find_qgroup_rb(fs_info, parentid);
  262. return __add_relation_rb(prealloc, member, parent);
  263. }
  264. /* Must be called with qgroup_lock held */
  265. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  266. u64 memberid, u64 parentid)
  267. {
  268. struct btrfs_qgroup *member;
  269. struct btrfs_qgroup *parent;
  270. struct btrfs_qgroup_list *list;
  271. member = find_qgroup_rb(fs_info, memberid);
  272. parent = find_qgroup_rb(fs_info, parentid);
  273. if (!member || !parent)
  274. return -ENOENT;
  275. list_for_each_entry(list, &member->groups, next_group) {
  276. if (list->group == parent) {
  277. list_del(&list->next_group);
  278. list_del(&list->next_member);
  279. kfree(list);
  280. return 0;
  281. }
  282. }
  283. return -ENOENT;
  284. }
  285. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  286. int btrfs_verify_qgroup_counts(const struct btrfs_fs_info *fs_info, u64 qgroupid,
  287. u64 rfer, u64 excl)
  288. {
  289. struct btrfs_qgroup *qgroup;
  290. qgroup = find_qgroup_rb(fs_info, qgroupid);
  291. if (!qgroup)
  292. return -EINVAL;
  293. if (qgroup->rfer != rfer || qgroup->excl != excl)
  294. return -EINVAL;
  295. return 0;
  296. }
  297. #endif
  298. static bool squota_check_parent_usage(struct btrfs_fs_info *fs_info, struct btrfs_qgroup *parent)
  299. {
  300. u64 excl_sum = 0;
  301. u64 rfer_sum = 0;
  302. u64 excl_cmpr_sum = 0;
  303. u64 rfer_cmpr_sum = 0;
  304. struct btrfs_qgroup_list *glist;
  305. int nr_members = 0;
  306. bool mismatch;
  307. if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
  308. return false;
  309. if (btrfs_qgroup_level(parent->qgroupid) == 0)
  310. return false;
  311. /* Eligible parent qgroup. Squota; level > 0; empty members list. */
  312. list_for_each_entry(glist, &parent->members, next_member) {
  313. excl_sum += glist->member->excl;
  314. rfer_sum += glist->member->rfer;
  315. excl_cmpr_sum += glist->member->excl_cmpr;
  316. rfer_cmpr_sum += glist->member->rfer_cmpr;
  317. nr_members++;
  318. }
  319. mismatch = (parent->excl != excl_sum || parent->rfer != rfer_sum ||
  320. parent->excl_cmpr != excl_cmpr_sum || parent->rfer_cmpr != rfer_cmpr_sum);
  321. WARN(mismatch,
  322. "parent squota qgroup %hu/%llu has mismatched usage from its %d members. "
  323. "%llu %llu %llu %llu vs %llu %llu %llu %llu\n",
  324. btrfs_qgroup_level(parent->qgroupid),
  325. btrfs_qgroup_subvolid(parent->qgroupid), nr_members, parent->excl,
  326. parent->rfer, parent->excl_cmpr, parent->rfer_cmpr, excl_sum,
  327. rfer_sum, excl_cmpr_sum, rfer_cmpr_sum);
  328. return mismatch;
  329. }
  330. __printf(2, 3)
  331. static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info, const char *fmt, ...)
  332. {
  333. const u64 old_flags = fs_info->qgroup_flags;
  334. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  335. return;
  336. fs_info->qgroup_flags |= (BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT |
  337. BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
  338. BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
  339. if (!(old_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)) {
  340. struct va_format vaf;
  341. va_list args;
  342. va_start(args, fmt);
  343. vaf.fmt = fmt;
  344. vaf.va = &args;
  345. btrfs_warn_rl(fs_info, "qgroup marked inconsistent, %pV", &vaf);
  346. va_end(args);
  347. }
  348. }
  349. static void qgroup_read_enable_gen(struct btrfs_fs_info *fs_info,
  350. struct extent_buffer *leaf, int slot,
  351. struct btrfs_qgroup_status_item *ptr)
  352. {
  353. ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
  354. ASSERT(btrfs_item_size(leaf, slot) >= sizeof(*ptr));
  355. fs_info->qgroup_enable_gen = btrfs_qgroup_status_enable_gen(leaf, ptr);
  356. }
  357. /*
  358. * The full config is read in one go, only called from open_ctree()
  359. * It doesn't use any locking, as at this point we're still single-threaded
  360. */
  361. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  362. {
  363. struct btrfs_key key;
  364. struct btrfs_key found_key;
  365. struct btrfs_root *quota_root = fs_info->quota_root;
  366. struct btrfs_path *path = NULL;
  367. struct extent_buffer *l;
  368. int slot;
  369. int ret = 0;
  370. u64 flags = 0;
  371. u64 rescan_progress = 0;
  372. if (!fs_info->quota_root)
  373. return 0;
  374. path = btrfs_alloc_path();
  375. if (!path) {
  376. ret = -ENOMEM;
  377. goto out;
  378. }
  379. ret = btrfs_sysfs_add_qgroups(fs_info);
  380. if (ret < 0)
  381. goto out;
  382. /* default this to quota off, in case no status key is found */
  383. fs_info->qgroup_flags = 0;
  384. /*
  385. * pass 1: read status, all qgroup infos and limits
  386. */
  387. key.objectid = 0;
  388. key.type = 0;
  389. key.offset = 0;
  390. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  391. if (ret)
  392. goto out;
  393. while (1) {
  394. struct btrfs_qgroup *qgroup;
  395. slot = path->slots[0];
  396. l = path->nodes[0];
  397. btrfs_item_key_to_cpu(l, &found_key, slot);
  398. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  399. struct btrfs_qgroup_status_item *ptr;
  400. ptr = btrfs_item_ptr(l, slot,
  401. struct btrfs_qgroup_status_item);
  402. if (btrfs_qgroup_status_version(l, ptr) !=
  403. BTRFS_QGROUP_STATUS_VERSION) {
  404. btrfs_err(fs_info,
  405. "old qgroup version, quota disabled");
  406. goto out;
  407. }
  408. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, ptr);
  409. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
  410. qgroup_read_enable_gen(fs_info, l, slot, ptr);
  411. else if (btrfs_qgroup_status_generation(l, ptr) != fs_info->generation)
  412. qgroup_mark_inconsistent(fs_info, "qgroup generation mismatch");
  413. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  414. goto next1;
  415. }
  416. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  417. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  418. goto next1;
  419. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  420. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  421. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY))
  422. qgroup_mark_inconsistent(fs_info, "inconsistent qgroup config");
  423. if (!qgroup) {
  424. struct btrfs_qgroup *prealloc;
  425. struct btrfs_root *tree_root = fs_info->tree_root;
  426. prealloc = kzalloc_obj(*prealloc);
  427. if (!prealloc) {
  428. ret = -ENOMEM;
  429. goto out;
  430. }
  431. qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
  432. /*
  433. * If a qgroup exists for a subvolume ID, it is possible
  434. * that subvolume has been deleted, in which case
  435. * reusing that ID would lead to incorrect accounting.
  436. *
  437. * Ensure that we skip any such subvol ids.
  438. *
  439. * We don't need to lock because this is only called
  440. * during mount before we start doing things like creating
  441. * subvolumes.
  442. */
  443. if (btrfs_is_fstree(qgroup->qgroupid) &&
  444. qgroup->qgroupid > tree_root->free_objectid)
  445. /*
  446. * Don't need to check against BTRFS_LAST_FREE_OBJECTID,
  447. * as it will get checked on the next call to
  448. * btrfs_get_free_objectid.
  449. */
  450. tree_root->free_objectid = qgroup->qgroupid + 1;
  451. }
  452. ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
  453. if (ret < 0)
  454. goto out;
  455. switch (found_key.type) {
  456. case BTRFS_QGROUP_INFO_KEY: {
  457. struct btrfs_qgroup_info_item *ptr;
  458. ptr = btrfs_item_ptr(l, slot,
  459. struct btrfs_qgroup_info_item);
  460. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  461. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  462. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  463. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  464. /* generation currently unused */
  465. break;
  466. }
  467. case BTRFS_QGROUP_LIMIT_KEY: {
  468. struct btrfs_qgroup_limit_item *ptr;
  469. ptr = btrfs_item_ptr(l, slot,
  470. struct btrfs_qgroup_limit_item);
  471. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  472. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  473. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  474. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  475. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  476. break;
  477. }
  478. }
  479. next1:
  480. ret = btrfs_next_item(quota_root, path);
  481. if (ret < 0)
  482. goto out;
  483. if (ret)
  484. break;
  485. }
  486. btrfs_release_path(path);
  487. /*
  488. * pass 2: read all qgroup relations
  489. */
  490. key.objectid = 0;
  491. key.type = BTRFS_QGROUP_RELATION_KEY;
  492. key.offset = 0;
  493. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  494. if (ret)
  495. goto out;
  496. while (1) {
  497. struct btrfs_qgroup_list *list = NULL;
  498. slot = path->slots[0];
  499. l = path->nodes[0];
  500. btrfs_item_key_to_cpu(l, &found_key, slot);
  501. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  502. goto next2;
  503. if (found_key.objectid > found_key.offset) {
  504. /* parent <- member, not needed to build config */
  505. /* FIXME should we omit the key completely? */
  506. goto next2;
  507. }
  508. list = kzalloc_obj(*list);
  509. if (!list) {
  510. ret = -ENOMEM;
  511. goto out;
  512. }
  513. ret = add_relation_rb(fs_info, list, found_key.objectid,
  514. found_key.offset);
  515. list = NULL;
  516. if (ret == -ENOENT) {
  517. btrfs_warn(fs_info,
  518. "orphan qgroup relation 0x%llx->0x%llx",
  519. found_key.objectid, found_key.offset);
  520. ret = 0; /* ignore the error */
  521. }
  522. if (ret)
  523. goto out;
  524. next2:
  525. ret = btrfs_next_item(quota_root, path);
  526. if (ret < 0)
  527. goto out;
  528. if (ret)
  529. break;
  530. }
  531. out:
  532. btrfs_free_path(path);
  533. fs_info->qgroup_flags |= flags;
  534. if (ret >= 0) {
  535. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)
  536. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  537. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  538. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  539. } else {
  540. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  541. btrfs_sysfs_del_qgroups(fs_info);
  542. }
  543. return ret < 0 ? ret : 0;
  544. }
  545. /*
  546. * Called in close_ctree() when quota is still enabled. This verifies we don't
  547. * leak some reserved space.
  548. *
  549. * Return false if no reserved space is left.
  550. * Return true if some reserved space is leaked.
  551. */
  552. bool btrfs_check_quota_leak(const struct btrfs_fs_info *fs_info)
  553. {
  554. struct rb_node *node;
  555. bool ret = false;
  556. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
  557. return ret;
  558. /*
  559. * Since we're unmounting, there is no race and no need to grab qgroup
  560. * lock. And here we don't go post-order to provide a more user
  561. * friendly sorted result.
  562. */
  563. for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
  564. struct btrfs_qgroup *qgroup;
  565. int i;
  566. qgroup = rb_entry(node, struct btrfs_qgroup, node);
  567. for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
  568. if (qgroup->rsv.values[i]) {
  569. ret = true;
  570. btrfs_warn(fs_info,
  571. "qgroup %hu/%llu has unreleased space, type %d rsv %llu",
  572. btrfs_qgroup_level(qgroup->qgroupid),
  573. btrfs_qgroup_subvolid(qgroup->qgroupid),
  574. i, qgroup->rsv.values[i]);
  575. }
  576. }
  577. }
  578. return ret;
  579. }
  580. /*
  581. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  582. * first two are in single-threaded paths.
  583. */
  584. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  585. {
  586. struct rb_node *n;
  587. struct btrfs_qgroup *qgroup;
  588. /*
  589. * btrfs_quota_disable() can be called concurrently with
  590. * btrfs_qgroup_rescan() -> qgroup_rescan_zero_tracking(), so take the
  591. * lock.
  592. */
  593. spin_lock(&fs_info->qgroup_lock);
  594. while ((n = rb_first(&fs_info->qgroup_tree))) {
  595. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  596. rb_erase(n, &fs_info->qgroup_tree);
  597. __del_qgroup_rb(qgroup);
  598. spin_unlock(&fs_info->qgroup_lock);
  599. btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
  600. kfree(qgroup);
  601. spin_lock(&fs_info->qgroup_lock);
  602. }
  603. spin_unlock(&fs_info->qgroup_lock);
  604. btrfs_sysfs_del_qgroups(fs_info);
  605. }
  606. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
  607. u64 dst)
  608. {
  609. struct btrfs_root *quota_root = trans->fs_info->quota_root;
  610. BTRFS_PATH_AUTO_FREE(path);
  611. struct btrfs_key key;
  612. path = btrfs_alloc_path();
  613. if (!path)
  614. return -ENOMEM;
  615. key.objectid = src;
  616. key.type = BTRFS_QGROUP_RELATION_KEY;
  617. key.offset = dst;
  618. return btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  619. }
  620. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
  621. u64 dst)
  622. {
  623. int ret;
  624. struct btrfs_root *quota_root = trans->fs_info->quota_root;
  625. BTRFS_PATH_AUTO_FREE(path);
  626. struct btrfs_key key;
  627. path = btrfs_alloc_path();
  628. if (!path)
  629. return -ENOMEM;
  630. key.objectid = src;
  631. key.type = BTRFS_QGROUP_RELATION_KEY;
  632. key.offset = dst;
  633. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  634. if (ret < 0)
  635. return ret;
  636. if (ret > 0)
  637. return -ENOENT;
  638. return btrfs_del_item(trans, quota_root, path);
  639. }
  640. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  641. struct btrfs_root *quota_root, u64 qgroupid)
  642. {
  643. int ret;
  644. BTRFS_PATH_AUTO_FREE(path);
  645. struct btrfs_qgroup_info_item *qgroup_info;
  646. struct btrfs_qgroup_limit_item *qgroup_limit;
  647. struct extent_buffer *leaf;
  648. struct btrfs_key key;
  649. if (btrfs_is_testing(quota_root->fs_info))
  650. return 0;
  651. path = btrfs_alloc_path();
  652. if (!path)
  653. return -ENOMEM;
  654. key.objectid = 0;
  655. key.type = BTRFS_QGROUP_INFO_KEY;
  656. key.offset = qgroupid;
  657. /*
  658. * Avoid a transaction abort by catching -EEXIST here. In that
  659. * case, we proceed by re-initializing the existing structure
  660. * on disk.
  661. */
  662. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  663. sizeof(*qgroup_info));
  664. if (ret && ret != -EEXIST)
  665. return ret;
  666. leaf = path->nodes[0];
  667. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  668. struct btrfs_qgroup_info_item);
  669. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  670. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  671. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  672. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  673. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  674. btrfs_release_path(path);
  675. key.type = BTRFS_QGROUP_LIMIT_KEY;
  676. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  677. sizeof(*qgroup_limit));
  678. if (ret && ret != -EEXIST)
  679. return ret;
  680. leaf = path->nodes[0];
  681. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  682. struct btrfs_qgroup_limit_item);
  683. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  684. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  685. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  686. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  687. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  688. return 0;
  689. }
  690. static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
  691. {
  692. int ret;
  693. struct btrfs_root *quota_root = trans->fs_info->quota_root;
  694. BTRFS_PATH_AUTO_FREE(path);
  695. struct btrfs_key key;
  696. path = btrfs_alloc_path();
  697. if (!path)
  698. return -ENOMEM;
  699. key.objectid = 0;
  700. key.type = BTRFS_QGROUP_INFO_KEY;
  701. key.offset = qgroupid;
  702. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  703. if (ret < 0)
  704. return ret;
  705. if (ret > 0)
  706. return -ENOENT;
  707. ret = btrfs_del_item(trans, quota_root, path);
  708. if (ret)
  709. return ret;
  710. btrfs_release_path(path);
  711. key.type = BTRFS_QGROUP_LIMIT_KEY;
  712. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  713. if (ret < 0)
  714. return ret;
  715. if (ret > 0)
  716. return -ENOENT;
  717. return btrfs_del_item(trans, quota_root, path);
  718. }
  719. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  720. struct btrfs_qgroup *qgroup)
  721. {
  722. struct btrfs_root *quota_root = trans->fs_info->quota_root;
  723. BTRFS_PATH_AUTO_FREE(path);
  724. struct btrfs_key key;
  725. struct extent_buffer *l;
  726. struct btrfs_qgroup_limit_item *qgroup_limit;
  727. int ret;
  728. int slot;
  729. key.objectid = 0;
  730. key.type = BTRFS_QGROUP_LIMIT_KEY;
  731. key.offset = qgroup->qgroupid;
  732. path = btrfs_alloc_path();
  733. if (!path)
  734. return -ENOMEM;
  735. ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
  736. if (ret > 0)
  737. ret = -ENOENT;
  738. if (ret)
  739. return ret;
  740. l = path->nodes[0];
  741. slot = path->slots[0];
  742. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  743. btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
  744. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
  745. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
  746. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
  747. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
  748. return ret;
  749. }
  750. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  751. struct btrfs_qgroup *qgroup)
  752. {
  753. struct btrfs_fs_info *fs_info = trans->fs_info;
  754. struct btrfs_root *quota_root = fs_info->quota_root;
  755. BTRFS_PATH_AUTO_FREE(path);
  756. struct btrfs_key key;
  757. struct extent_buffer *l;
  758. struct btrfs_qgroup_info_item *qgroup_info;
  759. int ret;
  760. int slot;
  761. if (btrfs_is_testing(fs_info))
  762. return 0;
  763. key.objectid = 0;
  764. key.type = BTRFS_QGROUP_INFO_KEY;
  765. key.offset = qgroup->qgroupid;
  766. path = btrfs_alloc_path();
  767. if (!path)
  768. return -ENOMEM;
  769. ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
  770. if (ret > 0)
  771. ret = -ENOENT;
  772. if (ret)
  773. return ret;
  774. l = path->nodes[0];
  775. slot = path->slots[0];
  776. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  777. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  778. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  779. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  780. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  781. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  782. return ret;
  783. }
  784. static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
  785. {
  786. struct btrfs_fs_info *fs_info = trans->fs_info;
  787. struct btrfs_root *quota_root = fs_info->quota_root;
  788. BTRFS_PATH_AUTO_FREE(path);
  789. struct btrfs_key key;
  790. struct extent_buffer *l;
  791. struct btrfs_qgroup_status_item *ptr;
  792. int ret;
  793. int slot;
  794. key.objectid = 0;
  795. key.type = BTRFS_QGROUP_STATUS_KEY;
  796. key.offset = 0;
  797. path = btrfs_alloc_path();
  798. if (!path)
  799. return -ENOMEM;
  800. ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
  801. if (ret > 0)
  802. ret = -ENOENT;
  803. if (ret)
  804. return ret;
  805. l = path->nodes[0];
  806. slot = path->slots[0];
  807. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  808. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags &
  809. BTRFS_QGROUP_STATUS_FLAGS_MASK);
  810. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  811. btrfs_set_qgroup_status_rescan(l, ptr,
  812. fs_info->qgroup_rescan_progress.objectid);
  813. return ret;
  814. }
  815. /*
  816. * called with qgroup_lock held
  817. */
  818. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  819. struct btrfs_root *root)
  820. {
  821. BTRFS_PATH_AUTO_FREE(path);
  822. struct btrfs_key key;
  823. struct extent_buffer *leaf = NULL;
  824. int ret;
  825. int nr = 0;
  826. path = btrfs_alloc_path();
  827. if (!path)
  828. return -ENOMEM;
  829. key.objectid = 0;
  830. key.type = 0;
  831. key.offset = 0;
  832. while (1) {
  833. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  834. if (ret < 0)
  835. return ret;
  836. leaf = path->nodes[0];
  837. nr = btrfs_header_nritems(leaf);
  838. if (!nr)
  839. break;
  840. /*
  841. * delete the leaf one by one
  842. * since the whole tree is going
  843. * to be deleted.
  844. */
  845. path->slots[0] = 0;
  846. ret = btrfs_del_items(trans, root, path, 0, nr);
  847. if (ret)
  848. return ret;
  849. btrfs_release_path(path);
  850. }
  851. return 0;
  852. }
  853. int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
  854. struct btrfs_ioctl_quota_ctl_args *quota_ctl_args)
  855. {
  856. struct btrfs_root *quota_root;
  857. struct btrfs_root *tree_root = fs_info->tree_root;
  858. struct btrfs_path *path = NULL;
  859. struct btrfs_qgroup_status_item *ptr;
  860. struct extent_buffer *leaf;
  861. struct btrfs_key key;
  862. struct btrfs_key found_key;
  863. struct btrfs_qgroup *qgroup = NULL;
  864. struct btrfs_qgroup *prealloc = NULL;
  865. struct btrfs_trans_handle *trans = NULL;
  866. const bool simple = (quota_ctl_args->cmd == BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA);
  867. int ret = 0;
  868. int slot;
  869. /*
  870. * We need to have subvol_sem write locked, to prevent races between
  871. * concurrent tasks trying to enable quotas, because we will unlock
  872. * and relock qgroup_ioctl_lock before setting fs_info->quota_root
  873. * and before setting BTRFS_FS_QUOTA_ENABLED.
  874. */
  875. lockdep_assert_held_write(&fs_info->subvol_sem);
  876. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  877. btrfs_err(fs_info,
  878. "qgroups are currently unsupported in extent tree v2");
  879. return -EINVAL;
  880. }
  881. mutex_lock(&fs_info->qgroup_ioctl_lock);
  882. if (fs_info->quota_root)
  883. goto out;
  884. ret = btrfs_sysfs_add_qgroups(fs_info);
  885. if (ret < 0)
  886. goto out;
  887. /*
  888. * Unlock qgroup_ioctl_lock before starting the transaction. This is to
  889. * avoid lock acquisition inversion problems (reported by lockdep) between
  890. * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
  891. * start a transaction.
  892. * After we started the transaction lock qgroup_ioctl_lock again and
  893. * check if someone else created the quota root in the meanwhile. If so,
  894. * just return success and release the transaction handle.
  895. *
  896. * Also we don't need to worry about someone else calling
  897. * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
  898. * that function returns 0 (success) when the sysfs entries already exist.
  899. */
  900. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  901. /*
  902. * 1 for quota root item
  903. * 1 for BTRFS_QGROUP_STATUS item
  904. *
  905. * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
  906. * per subvolume. However those are not currently reserved since it
  907. * would be a lot of overkill.
  908. */
  909. trans = btrfs_start_transaction(tree_root, 2);
  910. mutex_lock(&fs_info->qgroup_ioctl_lock);
  911. if (IS_ERR(trans)) {
  912. ret = PTR_ERR(trans);
  913. trans = NULL;
  914. goto out;
  915. }
  916. if (fs_info->quota_root)
  917. goto out;
  918. /*
  919. * initially create the quota tree
  920. */
  921. quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
  922. if (IS_ERR(quota_root)) {
  923. ret = PTR_ERR(quota_root);
  924. btrfs_abort_transaction(trans, ret);
  925. goto out;
  926. }
  927. path = btrfs_alloc_path();
  928. if (unlikely(!path)) {
  929. ret = -ENOMEM;
  930. btrfs_abort_transaction(trans, ret);
  931. goto out_free_root;
  932. }
  933. key.objectid = 0;
  934. key.type = BTRFS_QGROUP_STATUS_KEY;
  935. key.offset = 0;
  936. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  937. sizeof(*ptr));
  938. if (unlikely(ret)) {
  939. btrfs_abort_transaction(trans, ret);
  940. goto out_free_path;
  941. }
  942. leaf = path->nodes[0];
  943. ptr = btrfs_item_ptr(leaf, path->slots[0],
  944. struct btrfs_qgroup_status_item);
  945. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  946. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  947. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON;
  948. if (simple) {
  949. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
  950. btrfs_set_fs_incompat(fs_info, SIMPLE_QUOTA);
  951. btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid);
  952. } else {
  953. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  954. }
  955. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &
  956. BTRFS_QGROUP_STATUS_FLAGS_MASK);
  957. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  958. key.objectid = 0;
  959. key.type = BTRFS_ROOT_REF_KEY;
  960. key.offset = 0;
  961. btrfs_release_path(path);
  962. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  963. if (ret > 0)
  964. goto out_add_root;
  965. if (unlikely(ret < 0)) {
  966. btrfs_abort_transaction(trans, ret);
  967. goto out_free_path;
  968. }
  969. while (1) {
  970. slot = path->slots[0];
  971. leaf = path->nodes[0];
  972. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  973. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  974. /* Release locks on tree_root before we access quota_root */
  975. btrfs_release_path(path);
  976. /* We should not have a stray @prealloc pointer. */
  977. ASSERT(prealloc == NULL);
  978. prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
  979. if (unlikely(!prealloc)) {
  980. ret = -ENOMEM;
  981. btrfs_abort_transaction(trans, ret);
  982. goto out_free_path;
  983. }
  984. ret = add_qgroup_item(trans, quota_root,
  985. found_key.offset);
  986. if (unlikely(ret)) {
  987. btrfs_abort_transaction(trans, ret);
  988. goto out_free_path;
  989. }
  990. qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
  991. prealloc = NULL;
  992. ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
  993. if (unlikely(ret < 0)) {
  994. btrfs_abort_transaction(trans, ret);
  995. goto out_free_path;
  996. }
  997. ret = btrfs_search_slot_for_read(tree_root, &found_key,
  998. path, 1, 0);
  999. if (unlikely(ret < 0)) {
  1000. btrfs_abort_transaction(trans, ret);
  1001. goto out_free_path;
  1002. }
  1003. if (ret > 0) {
  1004. /*
  1005. * Shouldn't happen because the key should still
  1006. * be there (return 0), but in case it does it
  1007. * means we have reached the end of the tree -
  1008. * there are no more leaves with items that have
  1009. * a key greater than or equals to @found_key,
  1010. * so just stop the search loop.
  1011. */
  1012. break;
  1013. }
  1014. }
  1015. ret = btrfs_next_item(tree_root, path);
  1016. if (unlikely(ret < 0)) {
  1017. btrfs_abort_transaction(trans, ret);
  1018. goto out_free_path;
  1019. }
  1020. if (ret)
  1021. break;
  1022. }
  1023. out_add_root:
  1024. btrfs_release_path(path);
  1025. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  1026. if (unlikely(ret)) {
  1027. btrfs_abort_transaction(trans, ret);
  1028. goto out_free_path;
  1029. }
  1030. ASSERT(prealloc == NULL);
  1031. prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
  1032. if (!prealloc) {
  1033. ret = -ENOMEM;
  1034. goto out_free_path;
  1035. }
  1036. qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
  1037. prealloc = NULL;
  1038. ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
  1039. if (unlikely(ret < 0)) {
  1040. btrfs_abort_transaction(trans, ret);
  1041. goto out_free_path;
  1042. }
  1043. fs_info->qgroup_enable_gen = trans->transid;
  1044. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1045. /*
  1046. * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
  1047. * a deadlock with tasks concurrently doing other qgroup operations, such
  1048. * adding/removing qgroups or adding/deleting qgroup relations for example,
  1049. * because all qgroup operations first start or join a transaction and then
  1050. * lock the qgroup_ioctl_lock mutex.
  1051. * We are safe from a concurrent task trying to enable quotas, by calling
  1052. * this function, since we are serialized by fs_info->subvol_sem.
  1053. */
  1054. ret = btrfs_commit_transaction(trans);
  1055. trans = NULL;
  1056. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1057. if (ret)
  1058. goto out_free_path;
  1059. /*
  1060. * Set quota enabled flag after committing the transaction, to avoid
  1061. * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
  1062. * creation.
  1063. */
  1064. spin_lock(&fs_info->qgroup_lock);
  1065. fs_info->quota_root = quota_root;
  1066. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1067. spin_unlock(&fs_info->qgroup_lock);
  1068. /* Skip rescan for simple qgroups. */
  1069. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  1070. goto out_free_path;
  1071. ret = qgroup_rescan_init(fs_info, 0, 1);
  1072. if (!ret) {
  1073. qgroup_rescan_zero_tracking(fs_info);
  1074. fs_info->qgroup_rescan_running = true;
  1075. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1076. &fs_info->qgroup_rescan_work);
  1077. } else {
  1078. /*
  1079. * We have set both BTRFS_FS_QUOTA_ENABLED and
  1080. * BTRFS_QGROUP_STATUS_FLAG_ON, so we can only fail with
  1081. * -EINPROGRESS. That can happen because someone started the
  1082. * rescan worker by calling quota rescan ioctl before we
  1083. * attempted to initialize the rescan worker. Failure due to
  1084. * quotas disabled in the meanwhile is not possible, because
  1085. * we are holding a write lock on fs_info->subvol_sem, which
  1086. * is also acquired when disabling quotas.
  1087. * Ignore such error, and any other error would need to undo
  1088. * everything we did in the transaction we just committed.
  1089. */
  1090. ASSERT(ret == -EINPROGRESS);
  1091. ret = 0;
  1092. }
  1093. out_free_path:
  1094. btrfs_free_path(path);
  1095. out_free_root:
  1096. if (ret)
  1097. btrfs_put_root(quota_root);
  1098. out:
  1099. if (ret)
  1100. btrfs_sysfs_del_qgroups(fs_info);
  1101. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1102. if (ret && trans)
  1103. btrfs_end_transaction(trans);
  1104. else if (trans)
  1105. ret = btrfs_end_transaction(trans);
  1106. kfree(prealloc);
  1107. return ret;
  1108. }
  1109. /*
  1110. * It is possible to have outstanding ordered extents which reserved bytes
  1111. * before we disabled. We need to fully flush delalloc, ordered extents, and a
  1112. * commit to ensure that we don't leak such reservations, only to have them
  1113. * come back if we re-enable.
  1114. *
  1115. * - enable simple quotas
  1116. * - reserve space
  1117. * - release it, store rsv_bytes in OE
  1118. * - disable quotas
  1119. * - enable simple quotas (qgroup rsv are all 0)
  1120. * - OE finishes
  1121. * - run delayed refs
  1122. * - free rsv_bytes, resulting in miscounting or even underflow
  1123. */
  1124. static int flush_reservations(struct btrfs_fs_info *fs_info)
  1125. {
  1126. int ret;
  1127. ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
  1128. if (ret)
  1129. return ret;
  1130. btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
  1131. return btrfs_commit_current_transaction(fs_info->tree_root);
  1132. }
  1133. int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
  1134. {
  1135. struct btrfs_root *quota_root = NULL;
  1136. struct btrfs_trans_handle *trans = NULL;
  1137. int ret = 0;
  1138. /*
  1139. * We need to have subvol_sem write locked to prevent races with
  1140. * snapshot creation.
  1141. */
  1142. lockdep_assert_held_write(&fs_info->subvol_sem);
  1143. /*
  1144. * Relocation will mess with backrefs, so make sure we have the
  1145. * cleaner_mutex held to protect us from relocate.
  1146. */
  1147. lockdep_assert_held(&fs_info->cleaner_mutex);
  1148. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1149. if (!fs_info->quota_root)
  1150. goto out;
  1151. /*
  1152. * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
  1153. * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
  1154. * to lock that mutex while holding a transaction handle and the rescan
  1155. * worker needs to commit a transaction.
  1156. */
  1157. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1158. /*
  1159. * Request qgroup rescan worker to complete and wait for it. This wait
  1160. * must be done before transaction start for quota disable since it may
  1161. * deadlock with transaction by the qgroup rescan worker.
  1162. */
  1163. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1164. btrfs_qgroup_wait_for_completion(fs_info, false);
  1165. /*
  1166. * We have nothing held here and no trans handle, just return the error
  1167. * if there is one and set back the quota enabled bit since we didn't
  1168. * actually disable quotas.
  1169. */
  1170. ret = flush_reservations(fs_info);
  1171. if (ret) {
  1172. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1173. return ret;
  1174. }
  1175. /*
  1176. * 1 For the root item
  1177. *
  1178. * We should also reserve enough items for the quota tree deletion in
  1179. * btrfs_clean_quota_tree but this is not done.
  1180. *
  1181. * Also, we must always start a transaction without holding the mutex
  1182. * qgroup_ioctl_lock, see btrfs_quota_enable().
  1183. */
  1184. trans = btrfs_start_transaction(fs_info->tree_root, 1);
  1185. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1186. if (IS_ERR(trans)) {
  1187. ret = PTR_ERR(trans);
  1188. trans = NULL;
  1189. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1190. goto out;
  1191. }
  1192. if (!fs_info->quota_root)
  1193. goto out;
  1194. spin_lock(&fs_info->qgroup_lock);
  1195. quota_root = fs_info->quota_root;
  1196. fs_info->quota_root = NULL;
  1197. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1198. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
  1199. fs_info->qgroup_drop_subtree_thres = BTRFS_QGROUP_DROP_SUBTREE_THRES_DEFAULT;
  1200. spin_unlock(&fs_info->qgroup_lock);
  1201. btrfs_free_qgroup_config(fs_info);
  1202. ret = btrfs_clean_quota_tree(trans, quota_root);
  1203. if (unlikely(ret)) {
  1204. btrfs_abort_transaction(trans, ret);
  1205. goto out;
  1206. }
  1207. ret = btrfs_del_root(trans, &quota_root->root_key);
  1208. if (unlikely(ret)) {
  1209. btrfs_abort_transaction(trans, ret);
  1210. goto out;
  1211. }
  1212. spin_lock(&fs_info->trans_lock);
  1213. list_del(&quota_root->dirty_list);
  1214. spin_unlock(&fs_info->trans_lock);
  1215. btrfs_tree_lock(quota_root->node);
  1216. btrfs_clear_buffer_dirty(trans, quota_root->node);
  1217. btrfs_tree_unlock(quota_root->node);
  1218. ret = btrfs_free_tree_block(trans, btrfs_root_id(quota_root),
  1219. quota_root->node, 0, 1);
  1220. if (ret < 0)
  1221. btrfs_abort_transaction(trans, ret);
  1222. out:
  1223. btrfs_put_root(quota_root);
  1224. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1225. if (ret && trans)
  1226. btrfs_end_transaction(trans);
  1227. else if (trans)
  1228. ret = btrfs_commit_transaction(trans);
  1229. return ret;
  1230. }
  1231. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  1232. struct btrfs_qgroup *qgroup)
  1233. {
  1234. if (list_empty(&qgroup->dirty))
  1235. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  1236. }
  1237. static void qgroup_iterator_add(struct list_head *head, struct btrfs_qgroup *qgroup)
  1238. {
  1239. if (!list_empty(&qgroup->iterator))
  1240. return;
  1241. list_add_tail(&qgroup->iterator, head);
  1242. }
  1243. static void qgroup_iterator_clean(struct list_head *head)
  1244. {
  1245. while (!list_empty(head)) {
  1246. struct btrfs_qgroup *qgroup;
  1247. qgroup = list_first_entry(head, struct btrfs_qgroup, iterator);
  1248. list_del_init(&qgroup->iterator);
  1249. }
  1250. }
  1251. /*
  1252. * The easy accounting, we're updating qgroup relationship whose child qgroup
  1253. * only has exclusive extents.
  1254. *
  1255. * In this case, all exclusive extents will also be exclusive for parent, so
  1256. * excl/rfer just get added/removed.
  1257. *
  1258. * So is qgroup reservation space, which should also be added/removed to
  1259. * parent.
  1260. * Or when child tries to release reservation space, parent will underflow its
  1261. * reservation (for relationship adding case).
  1262. *
  1263. * Caller should hold fs_info->qgroup_lock.
  1264. */
  1265. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, u64 ref_root,
  1266. struct btrfs_qgroup *src, int sign)
  1267. {
  1268. struct btrfs_qgroup *qgroup;
  1269. LIST_HEAD(qgroup_list);
  1270. u64 num_bytes = src->excl;
  1271. u64 num_bytes_cmpr = src->excl_cmpr;
  1272. int ret = 0;
  1273. qgroup = find_qgroup_rb(fs_info, ref_root);
  1274. if (!qgroup)
  1275. goto out;
  1276. qgroup_iterator_add(&qgroup_list, qgroup);
  1277. list_for_each_entry(qgroup, &qgroup_list, iterator) {
  1278. struct btrfs_qgroup_list *glist;
  1279. qgroup->rfer += sign * num_bytes;
  1280. qgroup->rfer_cmpr += sign * num_bytes_cmpr;
  1281. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  1282. WARN_ON(sign < 0 && qgroup->excl_cmpr < num_bytes_cmpr);
  1283. qgroup->excl += sign * num_bytes;
  1284. qgroup->excl_cmpr += sign * num_bytes_cmpr;
  1285. if (sign > 0)
  1286. qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
  1287. else
  1288. qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
  1289. qgroup_dirty(fs_info, qgroup);
  1290. /* Append parent qgroups to @qgroup_list. */
  1291. list_for_each_entry(glist, &qgroup->groups, next_group)
  1292. qgroup_iterator_add(&qgroup_list, glist->group);
  1293. }
  1294. ret = 0;
  1295. out:
  1296. qgroup_iterator_clean(&qgroup_list);
  1297. return ret;
  1298. }
  1299. /*
  1300. * Quick path for updating qgroup with only excl refs.
  1301. *
  1302. * In that case, just update all parent will be enough.
  1303. * Or we needs to do a full rescan.
  1304. * Caller should also hold fs_info->qgroup_lock.
  1305. *
  1306. * Return 0 for quick update, return >0 for need to full rescan
  1307. * and mark INCONSISTENT flag.
  1308. * Return < 0 for other error.
  1309. */
  1310. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  1311. u64 src, u64 dst, int sign)
  1312. {
  1313. struct btrfs_qgroup *qgroup;
  1314. int ret = 1;
  1315. qgroup = find_qgroup_rb(fs_info, src);
  1316. if (!qgroup)
  1317. goto out;
  1318. if (qgroup->excl == qgroup->rfer) {
  1319. ret = __qgroup_excl_accounting(fs_info, dst, qgroup, sign);
  1320. if (ret < 0)
  1321. goto out;
  1322. ret = 0;
  1323. }
  1324. out:
  1325. if (ret)
  1326. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1327. return ret;
  1328. }
  1329. /*
  1330. * Add relation between @src and @dst qgroup. The @prealloc is allocated by the
  1331. * callers and transferred here (either used or freed on error).
  1332. */
  1333. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst,
  1334. struct btrfs_qgroup_list *prealloc)
  1335. {
  1336. struct btrfs_fs_info *fs_info = trans->fs_info;
  1337. struct btrfs_qgroup *parent;
  1338. struct btrfs_qgroup *member;
  1339. struct btrfs_qgroup_list *list;
  1340. int ret = 0;
  1341. ASSERT(prealloc);
  1342. /* Check the level of src and dst first */
  1343. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst)) {
  1344. kfree(prealloc);
  1345. return -EINVAL;
  1346. }
  1347. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1348. if (!fs_info->quota_root) {
  1349. ret = -ENOTCONN;
  1350. goto out;
  1351. }
  1352. member = find_qgroup_rb(fs_info, src);
  1353. parent = find_qgroup_rb(fs_info, dst);
  1354. if (!member || !parent) {
  1355. ret = -EINVAL;
  1356. goto out;
  1357. }
  1358. /* check if such qgroup relation exist firstly */
  1359. list_for_each_entry(list, &member->groups, next_group) {
  1360. if (list->group == parent) {
  1361. ret = -EEXIST;
  1362. goto out;
  1363. }
  1364. }
  1365. ret = add_qgroup_relation_item(trans, src, dst);
  1366. if (ret)
  1367. goto out;
  1368. ret = add_qgroup_relation_item(trans, dst, src);
  1369. if (ret) {
  1370. del_qgroup_relation_item(trans, src, dst);
  1371. goto out;
  1372. }
  1373. spin_lock(&fs_info->qgroup_lock);
  1374. ret = __add_relation_rb(prealloc, member, parent);
  1375. prealloc = NULL;
  1376. if (ret < 0) {
  1377. spin_unlock(&fs_info->qgroup_lock);
  1378. goto out;
  1379. }
  1380. ret = quick_update_accounting(fs_info, src, dst, 1);
  1381. squota_check_parent_usage(fs_info, parent);
  1382. spin_unlock(&fs_info->qgroup_lock);
  1383. out:
  1384. kfree(prealloc);
  1385. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1386. return ret;
  1387. }
  1388. static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
  1389. u64 dst)
  1390. {
  1391. struct btrfs_fs_info *fs_info = trans->fs_info;
  1392. struct btrfs_qgroup *parent;
  1393. struct btrfs_qgroup *member;
  1394. struct btrfs_qgroup_list *list;
  1395. bool found = false;
  1396. int ret = 0;
  1397. int ret2;
  1398. if (!fs_info->quota_root)
  1399. return -ENOTCONN;
  1400. member = find_qgroup_rb(fs_info, src);
  1401. parent = find_qgroup_rb(fs_info, dst);
  1402. /*
  1403. * The parent/member pair doesn't exist, then try to delete the dead
  1404. * relation items only.
  1405. */
  1406. if (!member || !parent)
  1407. goto delete_item;
  1408. /* check if such qgroup relation exist firstly */
  1409. list_for_each_entry(list, &member->groups, next_group) {
  1410. if (list->group == parent) {
  1411. found = true;
  1412. break;
  1413. }
  1414. }
  1415. delete_item:
  1416. ret = del_qgroup_relation_item(trans, src, dst);
  1417. if (ret < 0 && ret != -ENOENT)
  1418. return ret;
  1419. ret2 = del_qgroup_relation_item(trans, dst, src);
  1420. if (ret2 < 0 && ret2 != -ENOENT)
  1421. return ret2;
  1422. /* At least one deletion succeeded, return 0 */
  1423. if (!ret || !ret2)
  1424. ret = 0;
  1425. if (found) {
  1426. spin_lock(&fs_info->qgroup_lock);
  1427. del_relation_rb(fs_info, src, dst);
  1428. ret = quick_update_accounting(fs_info, src, dst, -1);
  1429. ASSERT(parent);
  1430. squota_check_parent_usage(fs_info, parent);
  1431. spin_unlock(&fs_info->qgroup_lock);
  1432. }
  1433. return ret;
  1434. }
  1435. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
  1436. u64 dst)
  1437. {
  1438. struct btrfs_fs_info *fs_info = trans->fs_info;
  1439. int ret = 0;
  1440. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1441. ret = __del_qgroup_relation(trans, src, dst);
  1442. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1443. return ret;
  1444. }
  1445. int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  1446. {
  1447. struct btrfs_fs_info *fs_info = trans->fs_info;
  1448. struct btrfs_root *quota_root;
  1449. struct btrfs_qgroup *qgroup;
  1450. struct btrfs_qgroup *prealloc = NULL;
  1451. int ret = 0;
  1452. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1453. if (!fs_info->quota_root) {
  1454. ret = -ENOTCONN;
  1455. goto out;
  1456. }
  1457. quota_root = fs_info->quota_root;
  1458. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1459. if (qgroup) {
  1460. ret = -EEXIST;
  1461. goto out;
  1462. }
  1463. prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
  1464. if (!prealloc) {
  1465. ret = -ENOMEM;
  1466. goto out;
  1467. }
  1468. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1469. if (ret)
  1470. goto out;
  1471. spin_lock(&fs_info->qgroup_lock);
  1472. qgroup = add_qgroup_rb(fs_info, prealloc, qgroupid);
  1473. spin_unlock(&fs_info->qgroup_lock);
  1474. prealloc = NULL;
  1475. ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
  1476. out:
  1477. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1478. kfree(prealloc);
  1479. return ret;
  1480. }
  1481. static bool can_delete_parent_qgroup(struct btrfs_qgroup *qgroup)
  1482. {
  1483. ASSERT(btrfs_qgroup_level(qgroup->qgroupid));
  1484. return list_empty(&qgroup->members);
  1485. }
  1486. /*
  1487. * Return true if we can delete the squota qgroup and false otherwise.
  1488. *
  1489. * Rules for whether we can delete:
  1490. *
  1491. * A subvolume qgroup can be removed iff the subvolume is fully deleted, which
  1492. * is iff there is 0 usage in the qgroup.
  1493. *
  1494. * A higher level qgroup can be removed iff it has no members.
  1495. * Note: We audit its usage to warn on inconsitencies without blocking deletion.
  1496. */
  1497. static bool can_delete_squota_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup *qgroup)
  1498. {
  1499. ASSERT(btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
  1500. if (btrfs_qgroup_level(qgroup->qgroupid) > 0) {
  1501. squota_check_parent_usage(fs_info, qgroup);
  1502. return can_delete_parent_qgroup(qgroup);
  1503. }
  1504. return !(qgroup->rfer || qgroup->excl || qgroup->rfer_cmpr || qgroup->excl_cmpr);
  1505. }
  1506. /*
  1507. * Return 0 if we can not delete the qgroup (not empty or has children etc).
  1508. * Return >0 if we can delete the qgroup.
  1509. * Return <0 for other errors during tree search.
  1510. */
  1511. static int can_delete_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup *qgroup)
  1512. {
  1513. struct btrfs_key key;
  1514. BTRFS_PATH_AUTO_FREE(path);
  1515. /* Since squotas cannot be inconsistent, they have special rules for deletion. */
  1516. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  1517. return can_delete_squota_qgroup(fs_info, qgroup);
  1518. /* For higher level qgroup, we can only delete it if it has no child. */
  1519. if (btrfs_qgroup_level(qgroup->qgroupid))
  1520. return can_delete_parent_qgroup(qgroup);
  1521. /*
  1522. * For level-0 qgroups, we can only delete it if it has no subvolume
  1523. * for it.
  1524. * This means even a subvolume is unlinked but not yet fully dropped,
  1525. * we can not delete the qgroup.
  1526. */
  1527. key.objectid = qgroup->qgroupid;
  1528. key.type = BTRFS_ROOT_ITEM_KEY;
  1529. key.offset = -1ULL;
  1530. path = btrfs_alloc_path();
  1531. if (!path)
  1532. return -ENOMEM;
  1533. /*
  1534. * The @ret from btrfs_find_root() exactly matches our definition for
  1535. * the return value, thus can be returned directly.
  1536. */
  1537. return btrfs_find_root(fs_info->tree_root, &key, path, NULL, NULL);
  1538. }
  1539. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  1540. {
  1541. struct btrfs_fs_info *fs_info = trans->fs_info;
  1542. struct btrfs_qgroup *qgroup;
  1543. struct btrfs_qgroup_list *list;
  1544. int ret = 0;
  1545. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1546. if (!fs_info->quota_root) {
  1547. ret = -ENOTCONN;
  1548. goto out;
  1549. }
  1550. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1551. if (!qgroup) {
  1552. ret = -ENOENT;
  1553. goto out;
  1554. }
  1555. ret = can_delete_qgroup(fs_info, qgroup);
  1556. if (ret < 0)
  1557. goto out;
  1558. if (ret == 0) {
  1559. ret = -EBUSY;
  1560. goto out;
  1561. }
  1562. /* Check if there are no children of this qgroup */
  1563. if (!list_empty(&qgroup->members)) {
  1564. ret = -EBUSY;
  1565. goto out;
  1566. }
  1567. ret = del_qgroup_item(trans, qgroupid);
  1568. if (ret && ret != -ENOENT)
  1569. goto out;
  1570. while (!list_empty(&qgroup->groups)) {
  1571. list = list_first_entry(&qgroup->groups,
  1572. struct btrfs_qgroup_list, next_group);
  1573. ret = __del_qgroup_relation(trans, qgroupid,
  1574. list->group->qgroupid);
  1575. if (ret)
  1576. goto out;
  1577. }
  1578. spin_lock(&fs_info->qgroup_lock);
  1579. /*
  1580. * Warn on reserved space. The subvolume should has no child nor
  1581. * corresponding subvolume.
  1582. * Thus its reserved space should all be zero, no matter if qgroup
  1583. * is consistent or the mode.
  1584. */
  1585. if (qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] ||
  1586. qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] ||
  1587. qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]) {
  1588. DEBUG_WARN();
  1589. btrfs_warn_rl(fs_info,
  1590. "to be deleted qgroup %u/%llu has non-zero numbers, data %llu meta prealloc %llu meta pertrans %llu",
  1591. btrfs_qgroup_level(qgroup->qgroupid),
  1592. btrfs_qgroup_subvolid(qgroup->qgroupid),
  1593. qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA],
  1594. qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC],
  1595. qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]);
  1596. }
  1597. /*
  1598. * The same for rfer/excl numbers, but that's only if our qgroup is
  1599. * consistent and if it's in regular qgroup mode.
  1600. * For simple mode it's not as accurate thus we can hit non-zero values
  1601. * very frequently.
  1602. */
  1603. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL &&
  1604. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)) {
  1605. if (qgroup->rfer || qgroup->excl ||
  1606. qgroup->rfer_cmpr || qgroup->excl_cmpr) {
  1607. DEBUG_WARN();
  1608. qgroup_mark_inconsistent(fs_info,
  1609. "to be deleted qgroup %u/%llu has non-zero numbers, rfer %llu rfer_cmpr %llu excl %llu excl_cmpr %llu",
  1610. btrfs_qgroup_level(qgroup->qgroupid),
  1611. btrfs_qgroup_subvolid(qgroup->qgroupid),
  1612. qgroup->rfer, qgroup->rfer_cmpr,
  1613. qgroup->excl, qgroup->excl_cmpr);
  1614. }
  1615. }
  1616. del_qgroup_rb(fs_info, qgroupid);
  1617. spin_unlock(&fs_info->qgroup_lock);
  1618. /*
  1619. * Remove the qgroup from sysfs now without holding the qgroup_lock
  1620. * spinlock, since the sysfs_remove_group() function needs to take
  1621. * the mutex kernfs_mutex through kernfs_remove_by_name_ns().
  1622. */
  1623. btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
  1624. kfree(qgroup);
  1625. out:
  1626. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1627. return ret;
  1628. }
  1629. int btrfs_qgroup_cleanup_dropped_subvolume(struct btrfs_fs_info *fs_info, u64 subvolid)
  1630. {
  1631. struct btrfs_trans_handle *trans;
  1632. int ret;
  1633. if (!btrfs_is_fstree(subvolid) || !btrfs_qgroup_enabled(fs_info) ||
  1634. !fs_info->quota_root)
  1635. return 0;
  1636. /*
  1637. * Commit current transaction to make sure all the rfer/excl numbers
  1638. * get updated.
  1639. */
  1640. ret = btrfs_commit_current_transaction(fs_info->quota_root);
  1641. if (ret < 0)
  1642. return ret;
  1643. /* Start new trans to delete the qgroup info and limit items. */
  1644. trans = btrfs_start_transaction(fs_info->quota_root, 2);
  1645. if (IS_ERR(trans))
  1646. return PTR_ERR(trans);
  1647. ret = btrfs_remove_qgroup(trans, subvolid);
  1648. btrfs_end_transaction(trans);
  1649. /*
  1650. * It's squota and the subvolume still has numbers needed for future
  1651. * accounting, in this case we can not delete it. Just skip it.
  1652. *
  1653. * Or the qgroup is already removed by a qgroup rescan. For both cases we're
  1654. * safe to ignore them.
  1655. */
  1656. if (ret == -EBUSY || ret == -ENOENT)
  1657. ret = 0;
  1658. return ret;
  1659. }
  1660. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
  1661. struct btrfs_qgroup_limit *limit)
  1662. {
  1663. struct btrfs_fs_info *fs_info = trans->fs_info;
  1664. struct btrfs_qgroup *qgroup;
  1665. int ret = 0;
  1666. /* Sometimes we would want to clear the limit on this qgroup.
  1667. * To meet this requirement, we treat the -1 as a special value
  1668. * which tell kernel to clear the limit on this qgroup.
  1669. */
  1670. const u64 CLEAR_VALUE = -1;
  1671. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1672. if (!fs_info->quota_root) {
  1673. ret = -ENOTCONN;
  1674. goto out;
  1675. }
  1676. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1677. if (!qgroup) {
  1678. ret = -ENOENT;
  1679. goto out;
  1680. }
  1681. spin_lock(&fs_info->qgroup_lock);
  1682. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1683. if (limit->max_rfer == CLEAR_VALUE) {
  1684. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1685. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1686. qgroup->max_rfer = 0;
  1687. } else {
  1688. qgroup->max_rfer = limit->max_rfer;
  1689. }
  1690. }
  1691. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1692. if (limit->max_excl == CLEAR_VALUE) {
  1693. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1694. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1695. qgroup->max_excl = 0;
  1696. } else {
  1697. qgroup->max_excl = limit->max_excl;
  1698. }
  1699. }
  1700. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1701. if (limit->rsv_rfer == CLEAR_VALUE) {
  1702. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1703. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1704. qgroup->rsv_rfer = 0;
  1705. } else {
  1706. qgroup->rsv_rfer = limit->rsv_rfer;
  1707. }
  1708. }
  1709. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1710. if (limit->rsv_excl == CLEAR_VALUE) {
  1711. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1712. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1713. qgroup->rsv_excl = 0;
  1714. } else {
  1715. qgroup->rsv_excl = limit->rsv_excl;
  1716. }
  1717. }
  1718. qgroup->lim_flags |= limit->flags;
  1719. spin_unlock(&fs_info->qgroup_lock);
  1720. ret = update_qgroup_limit_item(trans, qgroup);
  1721. if (ret)
  1722. qgroup_mark_inconsistent(fs_info, "qgroup item update error %d", ret);
  1723. out:
  1724. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1725. return ret;
  1726. }
  1727. /*
  1728. * Inform qgroup to trace one dirty extent, its info is recorded in @record.
  1729. * So qgroup can account it at transaction committing time.
  1730. *
  1731. * No lock version, caller must acquire delayed ref lock and allocated memory,
  1732. * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
  1733. *
  1734. * Return 0 for success insert
  1735. * Return >0 for existing record, caller can free @record safely.
  1736. * Return <0 for insertion failure, caller can free @record safely.
  1737. */
  1738. int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
  1739. struct btrfs_delayed_ref_root *delayed_refs,
  1740. struct btrfs_qgroup_extent_record *record,
  1741. u64 bytenr)
  1742. {
  1743. struct btrfs_qgroup_extent_record *existing, *ret;
  1744. const unsigned long index = (bytenr >> fs_info->sectorsize_bits);
  1745. if (!btrfs_qgroup_full_accounting(fs_info))
  1746. return 1;
  1747. #if BITS_PER_LONG == 32
  1748. if (bytenr >= MAX_LFS_FILESIZE) {
  1749. btrfs_err_rl(fs_info,
  1750. "qgroup record for extent at %llu is beyond 32bit page cache and xarray index limit",
  1751. bytenr);
  1752. btrfs_err_32bit_limit(fs_info);
  1753. return -EOVERFLOW;
  1754. }
  1755. #endif
  1756. trace_btrfs_qgroup_trace_extent(fs_info, record, bytenr);
  1757. xa_lock(&delayed_refs->dirty_extents);
  1758. existing = xa_load(&delayed_refs->dirty_extents, index);
  1759. if (existing) {
  1760. if (record->data_rsv && !existing->data_rsv) {
  1761. existing->data_rsv = record->data_rsv;
  1762. existing->data_rsv_refroot = record->data_rsv_refroot;
  1763. }
  1764. xa_unlock(&delayed_refs->dirty_extents);
  1765. return 1;
  1766. }
  1767. ret = __xa_store(&delayed_refs->dirty_extents, index, record, GFP_ATOMIC);
  1768. xa_unlock(&delayed_refs->dirty_extents);
  1769. if (xa_is_err(ret)) {
  1770. qgroup_mark_inconsistent(fs_info, "xarray insert error: %d", xa_err(ret));
  1771. return xa_err(ret);
  1772. }
  1773. return 0;
  1774. }
  1775. /*
  1776. * Post handler after qgroup_trace_extent_nolock().
  1777. *
  1778. * NOTE: Current qgroup does the expensive backref walk at transaction
  1779. * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
  1780. * new transaction.
  1781. * This is designed to allow btrfs_find_all_roots() to get correct new_roots
  1782. * result.
  1783. *
  1784. * However for old_roots there is no need to do backref walk at that time,
  1785. * since we search commit roots to walk backref and result will always be
  1786. * correct.
  1787. *
  1788. * Due to the nature of no lock version, we can't do backref there.
  1789. * So we must call btrfs_qgroup_trace_extent_post() after exiting
  1790. * spinlock context.
  1791. *
  1792. * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
  1793. * using current root, then we can move all expensive backref walk out of
  1794. * transaction committing, but not now as qgroup accounting will be wrong again.
  1795. */
  1796. int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
  1797. struct btrfs_qgroup_extent_record *qrecord,
  1798. u64 bytenr)
  1799. {
  1800. struct btrfs_fs_info *fs_info = trans->fs_info;
  1801. struct btrfs_backref_walk_ctx ctx = {
  1802. .bytenr = bytenr,
  1803. .fs_info = fs_info,
  1804. };
  1805. int ret;
  1806. if (!btrfs_qgroup_full_accounting(fs_info))
  1807. return 0;
  1808. /*
  1809. * We are always called in a context where we are already holding a
  1810. * transaction handle. Often we are called when adding a data delayed
  1811. * reference from btrfs_truncate_inode_items() (truncating or unlinking),
  1812. * in which case we will be holding a write lock on extent buffer from a
  1813. * subvolume tree. In this case we can't allow btrfs_find_all_roots() to
  1814. * acquire fs_info->commit_root_sem, because that is a higher level lock
  1815. * that must be acquired before locking any extent buffers.
  1816. *
  1817. * So we want btrfs_find_all_roots() to not acquire the commit_root_sem
  1818. * but we can't pass it a non-NULL transaction handle, because otherwise
  1819. * it would not use commit roots and would lock extent buffers, causing
  1820. * a deadlock if it ends up trying to read lock the same extent buffer
  1821. * that was previously write locked at btrfs_truncate_inode_items().
  1822. *
  1823. * So pass a NULL transaction handle to btrfs_find_all_roots() and
  1824. * explicitly tell it to not acquire the commit_root_sem - if we are
  1825. * holding a transaction handle we don't need its protection.
  1826. */
  1827. ASSERT(trans != NULL);
  1828. if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
  1829. return 0;
  1830. ret = btrfs_find_all_roots(&ctx, true);
  1831. if (ret < 0) {
  1832. qgroup_mark_inconsistent(fs_info,
  1833. "error accounting new delayed refs extent: %d", ret);
  1834. return 0;
  1835. }
  1836. /*
  1837. * Here we don't need to get the lock of
  1838. * trans->transaction->delayed_refs, since inserted qrecord won't
  1839. * be deleted, only qrecord->node may be modified (new qrecord insert)
  1840. *
  1841. * So modifying qrecord->old_roots is safe here
  1842. */
  1843. qrecord->old_roots = ctx.roots;
  1844. return 0;
  1845. }
  1846. /*
  1847. * Inform qgroup to trace one dirty extent, specified by @bytenr and
  1848. * @num_bytes.
  1849. * So qgroup can account it at commit trans time.
  1850. *
  1851. * Better encapsulated version, with memory allocation and backref walk for
  1852. * commit roots.
  1853. * So this can sleep.
  1854. *
  1855. * Return 0 if the operation is done.
  1856. * Return <0 for error, like memory allocation failure or invalid parameter
  1857. * (NULL trans)
  1858. */
  1859. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
  1860. u64 num_bytes)
  1861. {
  1862. struct btrfs_fs_info *fs_info = trans->fs_info;
  1863. struct btrfs_qgroup_extent_record *record;
  1864. struct btrfs_delayed_ref_root *delayed_refs = &trans->transaction->delayed_refs;
  1865. const unsigned long index = (bytenr >> fs_info->sectorsize_bits);
  1866. int ret;
  1867. if (!btrfs_qgroup_full_accounting(fs_info) || bytenr == 0 || num_bytes == 0)
  1868. return 0;
  1869. record = kzalloc_obj(*record, GFP_NOFS);
  1870. if (!record)
  1871. return -ENOMEM;
  1872. if (xa_reserve(&delayed_refs->dirty_extents, index, GFP_NOFS)) {
  1873. kfree(record);
  1874. return -ENOMEM;
  1875. }
  1876. record->num_bytes = num_bytes;
  1877. ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record, bytenr);
  1878. if (ret) {
  1879. /* Clean up if insertion fails or item exists. */
  1880. xa_release(&delayed_refs->dirty_extents, index);
  1881. kfree(record);
  1882. return 0;
  1883. }
  1884. return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
  1885. }
  1886. /*
  1887. * Inform qgroup to trace all leaf items of data
  1888. *
  1889. * Return 0 for success
  1890. * Return <0 for error(ENOMEM)
  1891. */
  1892. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  1893. struct extent_buffer *eb)
  1894. {
  1895. struct btrfs_fs_info *fs_info = trans->fs_info;
  1896. int nr = btrfs_header_nritems(eb);
  1897. int i, extent_type, ret;
  1898. struct btrfs_key key;
  1899. struct btrfs_file_extent_item *fi;
  1900. u64 bytenr, num_bytes;
  1901. /* We can be called directly from walk_up_proc() */
  1902. if (!btrfs_qgroup_full_accounting(fs_info))
  1903. return 0;
  1904. for (i = 0; i < nr; i++) {
  1905. btrfs_item_key_to_cpu(eb, &key, i);
  1906. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1907. continue;
  1908. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  1909. /* filter out non qgroup-accountable extents */
  1910. extent_type = btrfs_file_extent_type(eb, fi);
  1911. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1912. continue;
  1913. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  1914. if (!bytenr)
  1915. continue;
  1916. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  1917. ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes);
  1918. if (ret)
  1919. return ret;
  1920. }
  1921. cond_resched();
  1922. return 0;
  1923. }
  1924. /*
  1925. * Walk up the tree from the bottom, freeing leaves and any interior
  1926. * nodes which have had all slots visited. If a node (leaf or
  1927. * interior) is freed, the node above it will have it's slot
  1928. * incremented. The root node will never be freed.
  1929. *
  1930. * At the end of this function, we should have a path which has all
  1931. * slots incremented to the next position for a search. If we need to
  1932. * read a new node it will be NULL and the node above it will have the
  1933. * correct slot selected for a later read.
  1934. *
  1935. * If we increment the root nodes slot counter past the number of
  1936. * elements, 1 is returned to signal completion of the search.
  1937. */
  1938. static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
  1939. {
  1940. int level = 0;
  1941. int nr, slot;
  1942. struct extent_buffer *eb;
  1943. if (root_level == 0)
  1944. return 1;
  1945. while (level <= root_level) {
  1946. eb = path->nodes[level];
  1947. nr = btrfs_header_nritems(eb);
  1948. path->slots[level]++;
  1949. slot = path->slots[level];
  1950. if (slot >= nr || level == 0) {
  1951. /*
  1952. * Don't free the root - we will detect this
  1953. * condition after our loop and return a
  1954. * positive value for caller to stop walking the tree.
  1955. */
  1956. if (level != root_level) {
  1957. btrfs_tree_unlock_rw(eb, path->locks[level]);
  1958. path->locks[level] = 0;
  1959. free_extent_buffer(eb);
  1960. path->nodes[level] = NULL;
  1961. path->slots[level] = 0;
  1962. }
  1963. } else {
  1964. /*
  1965. * We have a valid slot to walk back down
  1966. * from. Stop here so caller can process these
  1967. * new nodes.
  1968. */
  1969. break;
  1970. }
  1971. level++;
  1972. }
  1973. eb = path->nodes[root_level];
  1974. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  1975. return 1;
  1976. return 0;
  1977. }
  1978. /*
  1979. * Helper function to trace a subtree tree block swap.
  1980. *
  1981. * The swap will happen in highest tree block, but there may be a lot of
  1982. * tree blocks involved.
  1983. *
  1984. * For example:
  1985. * OO = Old tree blocks
  1986. * NN = New tree blocks allocated during balance
  1987. *
  1988. * File tree (257) Reloc tree for 257
  1989. * L2 OO NN
  1990. * / \ / \
  1991. * L1 OO OO (a) OO NN (a)
  1992. * / \ / \ / \ / \
  1993. * L0 OO OO OO OO OO OO NN NN
  1994. * (b) (c) (b) (c)
  1995. *
  1996. * When calling qgroup_trace_extent_swap(), we will pass:
  1997. * @src_eb = OO(a)
  1998. * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
  1999. * @dst_level = 0
  2000. * @root_level = 1
  2001. *
  2002. * In that case, qgroup_trace_extent_swap() will search from OO(a) to
  2003. * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
  2004. *
  2005. * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
  2006. *
  2007. * 1) Tree search from @src_eb
  2008. * It should acts as a simplified btrfs_search_slot().
  2009. * The key for search can be extracted from @dst_path->nodes[dst_level]
  2010. * (first key).
  2011. *
  2012. * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
  2013. * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
  2014. * They should be marked during previous (@dst_level = 1) iteration.
  2015. *
  2016. * 3) Mark file extents in leaves dirty
  2017. * We don't have good way to pick out new file extents only.
  2018. * So we still follow the old method by scanning all file extents in
  2019. * the leave.
  2020. *
  2021. * This function can free us from keeping two paths, thus later we only need
  2022. * to care about how to iterate all new tree blocks in reloc tree.
  2023. */
  2024. static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
  2025. struct extent_buffer *src_eb,
  2026. struct btrfs_path *dst_path,
  2027. int dst_level, int root_level,
  2028. bool trace_leaf)
  2029. {
  2030. struct btrfs_key key;
  2031. BTRFS_PATH_AUTO_FREE(src_path);
  2032. struct btrfs_fs_info *fs_info = trans->fs_info;
  2033. u32 nodesize = fs_info->nodesize;
  2034. int cur_level = root_level;
  2035. int ret;
  2036. BUG_ON(dst_level > root_level);
  2037. /* Level mismatch */
  2038. if (btrfs_header_level(src_eb) != root_level)
  2039. return -EINVAL;
  2040. src_path = btrfs_alloc_path();
  2041. if (!src_path)
  2042. return -ENOMEM;
  2043. if (dst_level)
  2044. btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
  2045. else
  2046. btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
  2047. /* For src_path */
  2048. refcount_inc(&src_eb->refs);
  2049. src_path->nodes[root_level] = src_eb;
  2050. src_path->slots[root_level] = dst_path->slots[root_level];
  2051. src_path->locks[root_level] = 0;
  2052. /* A simplified version of btrfs_search_slot() */
  2053. while (cur_level >= dst_level) {
  2054. struct btrfs_key src_key;
  2055. struct btrfs_key dst_key;
  2056. if (src_path->nodes[cur_level] == NULL) {
  2057. struct extent_buffer *eb;
  2058. int parent_slot;
  2059. eb = src_path->nodes[cur_level + 1];
  2060. parent_slot = src_path->slots[cur_level + 1];
  2061. eb = btrfs_read_node_slot(eb, parent_slot);
  2062. if (IS_ERR(eb))
  2063. return PTR_ERR(eb);
  2064. src_path->nodes[cur_level] = eb;
  2065. btrfs_tree_read_lock(eb);
  2066. src_path->locks[cur_level] = BTRFS_READ_LOCK;
  2067. }
  2068. src_path->slots[cur_level] = dst_path->slots[cur_level];
  2069. if (cur_level) {
  2070. btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
  2071. &dst_key, dst_path->slots[cur_level]);
  2072. btrfs_node_key_to_cpu(src_path->nodes[cur_level],
  2073. &src_key, src_path->slots[cur_level]);
  2074. } else {
  2075. btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
  2076. &dst_key, dst_path->slots[cur_level]);
  2077. btrfs_item_key_to_cpu(src_path->nodes[cur_level],
  2078. &src_key, src_path->slots[cur_level]);
  2079. }
  2080. /* Content mismatch, something went wrong */
  2081. if (btrfs_comp_cpu_keys(&dst_key, &src_key))
  2082. return -ENOENT;
  2083. cur_level--;
  2084. }
  2085. /*
  2086. * Now both @dst_path and @src_path have been populated, record the tree
  2087. * blocks for qgroup accounting.
  2088. */
  2089. ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
  2090. nodesize);
  2091. if (ret < 0)
  2092. return ret;
  2093. ret = btrfs_qgroup_trace_extent(trans, dst_path->nodes[dst_level]->start,
  2094. nodesize);
  2095. if (ret < 0)
  2096. return ret;
  2097. /* Record leaf file extents */
  2098. if (dst_level == 0 && trace_leaf) {
  2099. ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
  2100. if (ret < 0)
  2101. return ret;
  2102. ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
  2103. }
  2104. return ret;
  2105. }
  2106. /*
  2107. * Helper function to do recursive generation-aware depth-first search, to
  2108. * locate all new tree blocks in a subtree of reloc tree.
  2109. *
  2110. * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
  2111. * reloc tree
  2112. * L2 NN (a)
  2113. * / \
  2114. * L1 OO NN (b)
  2115. * / \ / \
  2116. * L0 OO OO OO NN
  2117. * (c) (d)
  2118. * If we pass:
  2119. * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
  2120. * @cur_level = 1
  2121. * @root_level = 1
  2122. *
  2123. * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
  2124. * above tree blocks along with their counter parts in file tree.
  2125. * While during search, old tree blocks OO(c) will be skipped as tree block swap
  2126. * won't affect OO(c).
  2127. */
  2128. static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
  2129. struct extent_buffer *src_eb,
  2130. struct btrfs_path *dst_path,
  2131. int cur_level, int root_level,
  2132. u64 last_snapshot, bool trace_leaf)
  2133. {
  2134. struct btrfs_fs_info *fs_info = trans->fs_info;
  2135. struct extent_buffer *eb;
  2136. bool need_cleanup = false;
  2137. int ret = 0;
  2138. int i;
  2139. /* Level sanity check */
  2140. if (unlikely(cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
  2141. root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
  2142. root_level < cur_level)) {
  2143. btrfs_err_rl(fs_info,
  2144. "%s: bad levels, cur_level=%d root_level=%d",
  2145. __func__, cur_level, root_level);
  2146. return -EUCLEAN;
  2147. }
  2148. /* Read the tree block if needed */
  2149. if (dst_path->nodes[cur_level] == NULL) {
  2150. int parent_slot;
  2151. u64 child_gen;
  2152. /*
  2153. * dst_path->nodes[root_level] must be initialized before
  2154. * calling this function.
  2155. */
  2156. if (unlikely(cur_level == root_level)) {
  2157. btrfs_err_rl(fs_info,
  2158. "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
  2159. __func__, root_level, root_level, cur_level);
  2160. return -EUCLEAN;
  2161. }
  2162. /*
  2163. * We need to get child blockptr/gen from parent before we can
  2164. * read it.
  2165. */
  2166. eb = dst_path->nodes[cur_level + 1];
  2167. parent_slot = dst_path->slots[cur_level + 1];
  2168. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  2169. /* This node is old, no need to trace */
  2170. if (child_gen < last_snapshot)
  2171. return ret;
  2172. eb = btrfs_read_node_slot(eb, parent_slot);
  2173. if (IS_ERR(eb))
  2174. return PTR_ERR(eb);
  2175. dst_path->nodes[cur_level] = eb;
  2176. dst_path->slots[cur_level] = 0;
  2177. btrfs_tree_read_lock(eb);
  2178. dst_path->locks[cur_level] = BTRFS_READ_LOCK;
  2179. need_cleanup = true;
  2180. }
  2181. /* Now record this tree block and its counter part for qgroups */
  2182. ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
  2183. root_level, trace_leaf);
  2184. if (ret < 0)
  2185. goto cleanup;
  2186. eb = dst_path->nodes[cur_level];
  2187. if (cur_level > 0) {
  2188. /* Iterate all child tree blocks */
  2189. for (i = 0; i < btrfs_header_nritems(eb); i++) {
  2190. /* Skip old tree blocks as they won't be swapped */
  2191. if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
  2192. continue;
  2193. dst_path->slots[cur_level] = i;
  2194. /* Recursive call (at most 7 times) */
  2195. ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
  2196. dst_path, cur_level - 1, root_level,
  2197. last_snapshot, trace_leaf);
  2198. if (ret < 0)
  2199. goto cleanup;
  2200. }
  2201. }
  2202. cleanup:
  2203. if (need_cleanup) {
  2204. /* Clean up */
  2205. btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
  2206. dst_path->locks[cur_level]);
  2207. free_extent_buffer(dst_path->nodes[cur_level]);
  2208. dst_path->nodes[cur_level] = NULL;
  2209. dst_path->slots[cur_level] = 0;
  2210. dst_path->locks[cur_level] = 0;
  2211. }
  2212. return ret;
  2213. }
  2214. static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
  2215. struct extent_buffer *src_eb,
  2216. struct extent_buffer *dst_eb,
  2217. u64 last_snapshot, bool trace_leaf)
  2218. {
  2219. struct btrfs_fs_info *fs_info = trans->fs_info;
  2220. struct btrfs_path *dst_path = NULL;
  2221. int level;
  2222. int ret;
  2223. if (!btrfs_qgroup_full_accounting(fs_info))
  2224. return 0;
  2225. /* Wrong parameter order */
  2226. if (unlikely(btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb))) {
  2227. btrfs_err_rl(fs_info,
  2228. "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
  2229. btrfs_header_generation(src_eb),
  2230. btrfs_header_generation(dst_eb));
  2231. return -EUCLEAN;
  2232. }
  2233. if (unlikely(!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb))) {
  2234. ret = -EIO;
  2235. goto out;
  2236. }
  2237. level = btrfs_header_level(dst_eb);
  2238. dst_path = btrfs_alloc_path();
  2239. if (!dst_path) {
  2240. ret = -ENOMEM;
  2241. goto out;
  2242. }
  2243. /* For dst_path */
  2244. refcount_inc(&dst_eb->refs);
  2245. dst_path->nodes[level] = dst_eb;
  2246. dst_path->slots[level] = 0;
  2247. dst_path->locks[level] = 0;
  2248. /* Do the generation aware breadth-first search */
  2249. ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
  2250. level, last_snapshot, trace_leaf);
  2251. if (ret < 0)
  2252. goto out;
  2253. ret = 0;
  2254. out:
  2255. btrfs_free_path(dst_path);
  2256. if (ret < 0)
  2257. qgroup_mark_inconsistent(fs_info, "%s error: %d", __func__, ret);
  2258. return ret;
  2259. }
  2260. /*
  2261. * Inform qgroup to trace a whole subtree, including all its child tree
  2262. * blocks and data.
  2263. * The root tree block is specified by @root_eb.
  2264. *
  2265. * Normally used by relocation(tree block swap) and subvolume deletion.
  2266. *
  2267. * Return 0 for success
  2268. * Return <0 for error(ENOMEM or tree search error)
  2269. */
  2270. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  2271. struct extent_buffer *root_eb,
  2272. u64 root_gen, int root_level)
  2273. {
  2274. struct btrfs_fs_info *fs_info = trans->fs_info;
  2275. int ret = 0;
  2276. int level;
  2277. u8 drop_subptree_thres;
  2278. struct extent_buffer *eb = root_eb;
  2279. BTRFS_PATH_AUTO_FREE(path);
  2280. ASSERT(0 <= root_level && root_level < BTRFS_MAX_LEVEL);
  2281. ASSERT(root_eb != NULL);
  2282. if (!btrfs_qgroup_full_accounting(fs_info))
  2283. return 0;
  2284. spin_lock(&fs_info->qgroup_lock);
  2285. drop_subptree_thres = fs_info->qgroup_drop_subtree_thres;
  2286. spin_unlock(&fs_info->qgroup_lock);
  2287. /*
  2288. * This function only gets called for snapshot drop, if we hit a high
  2289. * node here, it means we are going to change ownership for quite a lot
  2290. * of extents, which will greatly slow down btrfs_commit_transaction().
  2291. *
  2292. * So here if we find a high tree here, we just skip the accounting and
  2293. * mark qgroup inconsistent.
  2294. */
  2295. if (root_level >= drop_subptree_thres) {
  2296. qgroup_mark_inconsistent(fs_info, "subtree level reached threshold");
  2297. return 0;
  2298. }
  2299. if (!extent_buffer_uptodate(root_eb)) {
  2300. struct btrfs_tree_parent_check check = {
  2301. .transid = root_gen,
  2302. .level = root_level
  2303. };
  2304. ret = btrfs_read_extent_buffer(root_eb, &check);
  2305. if (ret)
  2306. return ret;
  2307. }
  2308. if (root_level == 0)
  2309. return btrfs_qgroup_trace_leaf_items(trans, root_eb);
  2310. path = btrfs_alloc_path();
  2311. if (!path)
  2312. return -ENOMEM;
  2313. /*
  2314. * Walk down the tree. Missing extent blocks are filled in as
  2315. * we go. Metadata is accounted every time we read a new
  2316. * extent block.
  2317. *
  2318. * When we reach a leaf, we account for file extent items in it,
  2319. * walk back up the tree (adjusting slot pointers as we go)
  2320. * and restart the search process.
  2321. */
  2322. refcount_inc(&root_eb->refs); /* For path */
  2323. path->nodes[root_level] = root_eb;
  2324. path->slots[root_level] = 0;
  2325. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  2326. walk_down:
  2327. level = root_level;
  2328. while (level >= 0) {
  2329. if (path->nodes[level] == NULL) {
  2330. int parent_slot;
  2331. u64 child_bytenr;
  2332. /*
  2333. * We need to get child blockptr from parent before we
  2334. * can read it.
  2335. */
  2336. eb = path->nodes[level + 1];
  2337. parent_slot = path->slots[level + 1];
  2338. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  2339. eb = btrfs_read_node_slot(eb, parent_slot);
  2340. if (IS_ERR(eb))
  2341. return PTR_ERR(eb);
  2342. path->nodes[level] = eb;
  2343. path->slots[level] = 0;
  2344. btrfs_tree_read_lock(eb);
  2345. path->locks[level] = BTRFS_READ_LOCK;
  2346. ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
  2347. fs_info->nodesize);
  2348. if (ret)
  2349. return ret;
  2350. }
  2351. if (level == 0) {
  2352. ret = btrfs_qgroup_trace_leaf_items(trans,
  2353. path->nodes[level]);
  2354. if (ret)
  2355. return ret;
  2356. /* Nonzero return here means we completed our search */
  2357. ret = adjust_slots_upwards(path, root_level);
  2358. if (ret)
  2359. break;
  2360. /* Restart search with new slots */
  2361. goto walk_down;
  2362. }
  2363. level--;
  2364. }
  2365. return 0;
  2366. }
  2367. static void qgroup_iterator_nested_add(struct list_head *head, struct btrfs_qgroup *qgroup)
  2368. {
  2369. if (!list_empty(&qgroup->nested_iterator))
  2370. return;
  2371. list_add_tail(&qgroup->nested_iterator, head);
  2372. }
  2373. static void qgroup_iterator_nested_clean(struct list_head *head)
  2374. {
  2375. while (!list_empty(head)) {
  2376. struct btrfs_qgroup *qgroup;
  2377. qgroup = list_first_entry(head, struct btrfs_qgroup, nested_iterator);
  2378. list_del_init(&qgroup->nested_iterator);
  2379. }
  2380. }
  2381. #define UPDATE_NEW 0
  2382. #define UPDATE_OLD 1
  2383. /*
  2384. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  2385. */
  2386. static void qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  2387. struct ulist *roots, struct list_head *qgroups,
  2388. u64 seq, bool update_old)
  2389. {
  2390. struct ulist_node *unode;
  2391. struct ulist_iterator uiter;
  2392. struct btrfs_qgroup *qg;
  2393. if (!roots)
  2394. return;
  2395. ULIST_ITER_INIT(&uiter);
  2396. while ((unode = ulist_next(roots, &uiter))) {
  2397. LIST_HEAD(tmp);
  2398. qg = find_qgroup_rb(fs_info, unode->val);
  2399. if (!qg)
  2400. continue;
  2401. qgroup_iterator_nested_add(qgroups, qg);
  2402. qgroup_iterator_add(&tmp, qg);
  2403. list_for_each_entry(qg, &tmp, iterator) {
  2404. struct btrfs_qgroup_list *glist;
  2405. if (update_old)
  2406. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  2407. else
  2408. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  2409. list_for_each_entry(glist, &qg->groups, next_group) {
  2410. qgroup_iterator_nested_add(qgroups, glist->group);
  2411. qgroup_iterator_add(&tmp, glist->group);
  2412. }
  2413. }
  2414. qgroup_iterator_clean(&tmp);
  2415. }
  2416. }
  2417. /*
  2418. * Update qgroup rfer/excl counters.
  2419. * Rfer update is easy, codes can explain themselves.
  2420. *
  2421. * Excl update is tricky, the update is split into 2 parts.
  2422. * Part 1: Possible exclusive <-> sharing detect:
  2423. * | A | !A |
  2424. * -------------------------------------
  2425. * B | * | - |
  2426. * -------------------------------------
  2427. * !B | + | ** |
  2428. * -------------------------------------
  2429. *
  2430. * Conditions:
  2431. * A: cur_old_roots < nr_old_roots (not exclusive before)
  2432. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  2433. * B: cur_new_roots < nr_new_roots (not exclusive now)
  2434. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  2435. *
  2436. * Results:
  2437. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  2438. * *: Definitely not changed. **: Possible unchanged.
  2439. *
  2440. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  2441. *
  2442. * To make the logic clear, we first use condition A and B to split
  2443. * combination into 4 results.
  2444. *
  2445. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  2446. * only on variant maybe 0.
  2447. *
  2448. * Lastly, check result **, since there are 2 variants maybe 0, split them
  2449. * again(2x2).
  2450. * But this time we don't need to consider other things, the codes and logic
  2451. * is easy to understand now.
  2452. */
  2453. static void qgroup_update_counters(struct btrfs_fs_info *fs_info,
  2454. struct list_head *qgroups, u64 nr_old_roots,
  2455. u64 nr_new_roots, u64 num_bytes, u64 seq)
  2456. {
  2457. struct btrfs_qgroup *qg;
  2458. list_for_each_entry(qg, qgroups, nested_iterator) {
  2459. u64 cur_new_count, cur_old_count;
  2460. bool dirty = false;
  2461. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  2462. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  2463. trace_btrfs_qgroup_update_counters(fs_info, qg, cur_old_count,
  2464. cur_new_count);
  2465. /* Rfer update part */
  2466. if (cur_old_count == 0 && cur_new_count > 0) {
  2467. qg->rfer += num_bytes;
  2468. qg->rfer_cmpr += num_bytes;
  2469. dirty = true;
  2470. }
  2471. if (cur_old_count > 0 && cur_new_count == 0) {
  2472. qg->rfer -= num_bytes;
  2473. qg->rfer_cmpr -= num_bytes;
  2474. dirty = true;
  2475. }
  2476. /* Excl update part */
  2477. /* Exclusive/none -> shared case */
  2478. if (cur_old_count == nr_old_roots &&
  2479. cur_new_count < nr_new_roots) {
  2480. /* Exclusive -> shared */
  2481. if (cur_old_count != 0) {
  2482. qg->excl -= num_bytes;
  2483. qg->excl_cmpr -= num_bytes;
  2484. dirty = true;
  2485. }
  2486. }
  2487. /* Shared -> exclusive/none case */
  2488. if (cur_old_count < nr_old_roots &&
  2489. cur_new_count == nr_new_roots) {
  2490. /* Shared->exclusive */
  2491. if (cur_new_count != 0) {
  2492. qg->excl += num_bytes;
  2493. qg->excl_cmpr += num_bytes;
  2494. dirty = true;
  2495. }
  2496. }
  2497. /* Exclusive/none -> exclusive/none case */
  2498. if (cur_old_count == nr_old_roots &&
  2499. cur_new_count == nr_new_roots) {
  2500. if (cur_old_count == 0) {
  2501. /* None -> exclusive/none */
  2502. if (cur_new_count != 0) {
  2503. /* None -> exclusive */
  2504. qg->excl += num_bytes;
  2505. qg->excl_cmpr += num_bytes;
  2506. dirty = true;
  2507. }
  2508. /* None -> none, nothing changed */
  2509. } else {
  2510. /* Exclusive -> exclusive/none */
  2511. if (cur_new_count == 0) {
  2512. /* Exclusive -> none */
  2513. qg->excl -= num_bytes;
  2514. qg->excl_cmpr -= num_bytes;
  2515. dirty = true;
  2516. }
  2517. /* Exclusive -> exclusive, nothing changed */
  2518. }
  2519. }
  2520. if (dirty)
  2521. qgroup_dirty(fs_info, qg);
  2522. }
  2523. }
  2524. /*
  2525. * Check if the @roots potentially is a list of fs tree roots
  2526. *
  2527. * Return 0 for definitely not a fs/subvol tree roots ulist
  2528. * Return 1 for possible fs/subvol tree roots in the list (considering an empty
  2529. * one as well)
  2530. */
  2531. static int maybe_fs_roots(struct ulist *roots)
  2532. {
  2533. struct ulist_node *unode;
  2534. struct ulist_iterator uiter;
  2535. /* Empty one, still possible for fs roots */
  2536. if (!roots || roots->nnodes == 0)
  2537. return 1;
  2538. ULIST_ITER_INIT(&uiter);
  2539. unode = ulist_next(roots, &uiter);
  2540. if (!unode)
  2541. return 1;
  2542. /*
  2543. * If it contains fs tree roots, then it must belong to fs/subvol
  2544. * trees.
  2545. * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
  2546. */
  2547. return btrfs_is_fstree(unode->val);
  2548. }
  2549. int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
  2550. u64 num_bytes, struct ulist *old_roots,
  2551. struct ulist *new_roots)
  2552. {
  2553. struct btrfs_fs_info *fs_info = trans->fs_info;
  2554. LIST_HEAD(qgroups);
  2555. u64 seq;
  2556. u64 nr_new_roots = 0;
  2557. u64 nr_old_roots = 0;
  2558. int ret = 0;
  2559. /*
  2560. * If quotas get disabled meanwhile, the resources need to be freed and
  2561. * we can't just exit here.
  2562. */
  2563. if (!btrfs_qgroup_full_accounting(fs_info) ||
  2564. fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
  2565. goto out_free;
  2566. if (new_roots) {
  2567. if (!maybe_fs_roots(new_roots))
  2568. goto out_free;
  2569. nr_new_roots = new_roots->nnodes;
  2570. }
  2571. if (old_roots) {
  2572. if (!maybe_fs_roots(old_roots))
  2573. goto out_free;
  2574. nr_old_roots = old_roots->nnodes;
  2575. }
  2576. /* Quick exit, either not fs tree roots, or won't affect any qgroup */
  2577. if (nr_old_roots == 0 && nr_new_roots == 0)
  2578. goto out_free;
  2579. trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
  2580. num_bytes, nr_old_roots, nr_new_roots);
  2581. mutex_lock(&fs_info->qgroup_rescan_lock);
  2582. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  2583. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  2584. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2585. ret = 0;
  2586. goto out_free;
  2587. }
  2588. }
  2589. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2590. spin_lock(&fs_info->qgroup_lock);
  2591. seq = fs_info->qgroup_seq;
  2592. /* Update old refcnts using old_roots */
  2593. qgroup_update_refcnt(fs_info, old_roots, &qgroups, seq, UPDATE_OLD);
  2594. /* Update new refcnts using new_roots */
  2595. qgroup_update_refcnt(fs_info, new_roots, &qgroups, seq, UPDATE_NEW);
  2596. qgroup_update_counters(fs_info, &qgroups, nr_old_roots, nr_new_roots,
  2597. num_bytes, seq);
  2598. /*
  2599. * We're done using the iterator, release all its qgroups while holding
  2600. * fs_info->qgroup_lock so that we don't race with btrfs_remove_qgroup()
  2601. * and trigger use-after-free accesses to qgroups.
  2602. */
  2603. qgroup_iterator_nested_clean(&qgroups);
  2604. /*
  2605. * Bump qgroup_seq to avoid seq overlap
  2606. */
  2607. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  2608. spin_unlock(&fs_info->qgroup_lock);
  2609. out_free:
  2610. ulist_free(old_roots);
  2611. ulist_free(new_roots);
  2612. return ret;
  2613. }
  2614. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
  2615. {
  2616. struct btrfs_fs_info *fs_info = trans->fs_info;
  2617. struct btrfs_qgroup_extent_record *record;
  2618. struct btrfs_delayed_ref_root *delayed_refs;
  2619. struct ulist *new_roots = NULL;
  2620. unsigned long index;
  2621. u64 num_dirty_extents = 0;
  2622. u64 qgroup_to_skip;
  2623. int ret = 0;
  2624. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  2625. return 0;
  2626. delayed_refs = &trans->transaction->delayed_refs;
  2627. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  2628. xa_for_each(&delayed_refs->dirty_extents, index, record) {
  2629. const u64 bytenr = (((u64)index) << fs_info->sectorsize_bits);
  2630. num_dirty_extents++;
  2631. trace_btrfs_qgroup_account_extents(fs_info, record, bytenr);
  2632. if (!ret && !(fs_info->qgroup_flags &
  2633. BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)) {
  2634. struct btrfs_backref_walk_ctx ctx = { 0 };
  2635. ctx.bytenr = bytenr;
  2636. ctx.fs_info = fs_info;
  2637. /*
  2638. * Old roots should be searched when inserting qgroup
  2639. * extent record.
  2640. *
  2641. * But for INCONSISTENT (NO_ACCOUNTING) -> rescan case,
  2642. * we may have some record inserted during
  2643. * NO_ACCOUNTING (thus no old_roots populated), but
  2644. * later we start rescan, which clears NO_ACCOUNTING,
  2645. * leaving some inserted records without old_roots
  2646. * populated.
  2647. *
  2648. * Those cases are rare and should not cause too much
  2649. * time spent during commit_transaction().
  2650. */
  2651. if (!record->old_roots) {
  2652. /* Search commit root to find old_roots */
  2653. ret = btrfs_find_all_roots(&ctx, false);
  2654. if (ret < 0)
  2655. goto cleanup;
  2656. record->old_roots = ctx.roots;
  2657. ctx.roots = NULL;
  2658. }
  2659. /*
  2660. * Use BTRFS_SEQ_LAST as time_seq to do special search,
  2661. * which doesn't lock tree or delayed_refs and search
  2662. * current root. It's safe inside commit_transaction().
  2663. */
  2664. ctx.trans = trans;
  2665. ctx.time_seq = BTRFS_SEQ_LAST;
  2666. ret = btrfs_find_all_roots(&ctx, false);
  2667. if (ret < 0)
  2668. goto cleanup;
  2669. new_roots = ctx.roots;
  2670. if (qgroup_to_skip) {
  2671. ulist_del(new_roots, qgroup_to_skip, 0);
  2672. ulist_del(record->old_roots, qgroup_to_skip,
  2673. 0);
  2674. }
  2675. ret = btrfs_qgroup_account_extent(trans, bytenr,
  2676. record->num_bytes,
  2677. record->old_roots,
  2678. new_roots);
  2679. record->old_roots = NULL;
  2680. new_roots = NULL;
  2681. }
  2682. /* Free the reserved data space */
  2683. btrfs_qgroup_free_refroot(fs_info,
  2684. record->data_rsv_refroot,
  2685. record->data_rsv,
  2686. BTRFS_QGROUP_RSV_DATA);
  2687. cleanup:
  2688. ulist_free(record->old_roots);
  2689. ulist_free(new_roots);
  2690. new_roots = NULL;
  2691. xa_erase(&delayed_refs->dirty_extents, index);
  2692. kfree(record);
  2693. }
  2694. trace_btrfs_qgroup_num_dirty_extents(fs_info, trans->transid, num_dirty_extents);
  2695. return ret;
  2696. }
  2697. /*
  2698. * Writes all changed qgroups to disk.
  2699. * Called by the transaction commit path and the qgroup assign ioctl.
  2700. */
  2701. int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
  2702. {
  2703. struct btrfs_fs_info *fs_info = trans->fs_info;
  2704. int ret = 0;
  2705. /*
  2706. * In case we are called from the qgroup assign ioctl, assert that we
  2707. * are holding the qgroup_ioctl_lock, otherwise we can race with a quota
  2708. * disable operation (ioctl) and access a freed quota root.
  2709. */
  2710. if (trans->transaction->state != TRANS_STATE_COMMIT_DOING)
  2711. lockdep_assert_held(&fs_info->qgroup_ioctl_lock);
  2712. if (!fs_info->quota_root)
  2713. return ret;
  2714. spin_lock(&fs_info->qgroup_lock);
  2715. while (!list_empty(&fs_info->dirty_qgroups)) {
  2716. struct btrfs_qgroup *qgroup;
  2717. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  2718. struct btrfs_qgroup, dirty);
  2719. list_del_init(&qgroup->dirty);
  2720. spin_unlock(&fs_info->qgroup_lock);
  2721. ret = update_qgroup_info_item(trans, qgroup);
  2722. if (ret)
  2723. qgroup_mark_inconsistent(fs_info,
  2724. "qgroup info item update error %d", ret);
  2725. ret = update_qgroup_limit_item(trans, qgroup);
  2726. if (ret)
  2727. qgroup_mark_inconsistent(fs_info,
  2728. "qgroup limit item update error %d", ret);
  2729. spin_lock(&fs_info->qgroup_lock);
  2730. }
  2731. if (btrfs_qgroup_enabled(fs_info))
  2732. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  2733. else
  2734. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  2735. spin_unlock(&fs_info->qgroup_lock);
  2736. ret = update_qgroup_status_item(trans);
  2737. if (ret)
  2738. qgroup_mark_inconsistent(fs_info,
  2739. "qgroup status item update error %d", ret);
  2740. return ret;
  2741. }
  2742. int btrfs_qgroup_check_inherit(struct btrfs_fs_info *fs_info,
  2743. struct btrfs_qgroup_inherit *inherit,
  2744. size_t size)
  2745. {
  2746. if (inherit->flags & ~BTRFS_QGROUP_INHERIT_FLAGS_SUPP)
  2747. return -EOPNOTSUPP;
  2748. if (size < sizeof(*inherit) || size > PAGE_SIZE)
  2749. return -EINVAL;
  2750. /*
  2751. * In the past we allowed btrfs_qgroup_inherit to specify to copy
  2752. * rfer/excl numbers directly from other qgroups. This behavior has
  2753. * been disabled in userspace for a very long time, but here we should
  2754. * also disable it in kernel, as this behavior is known to mark qgroup
  2755. * inconsistent, and a rescan would wipe out the changes anyway.
  2756. *
  2757. * Reject any btrfs_qgroup_inherit with num_ref_copies or num_excl_copies.
  2758. */
  2759. if (inherit->num_ref_copies > 0 || inherit->num_excl_copies > 0)
  2760. return -EINVAL;
  2761. if (size != struct_size(inherit, qgroups, inherit->num_qgroups))
  2762. return -EINVAL;
  2763. /*
  2764. * Skip the inherit source qgroups check if qgroup is not enabled.
  2765. * Qgroup can still be later enabled causing problems, but in that case
  2766. * btrfs_qgroup_inherit() would just ignore those invalid ones.
  2767. */
  2768. if (!btrfs_qgroup_enabled(fs_info))
  2769. return 0;
  2770. /*
  2771. * Now check all the remaining qgroups, they should all:
  2772. *
  2773. * - Exist
  2774. * - Be higher level qgroups.
  2775. */
  2776. for (int i = 0; i < inherit->num_qgroups; i++) {
  2777. struct btrfs_qgroup *qgroup;
  2778. u64 qgroupid = inherit->qgroups[i];
  2779. if (btrfs_qgroup_level(qgroupid) == 0)
  2780. return -EINVAL;
  2781. spin_lock(&fs_info->qgroup_lock);
  2782. qgroup = find_qgroup_rb(fs_info, qgroupid);
  2783. if (!qgroup) {
  2784. spin_unlock(&fs_info->qgroup_lock);
  2785. return -ENOENT;
  2786. }
  2787. spin_unlock(&fs_info->qgroup_lock);
  2788. }
  2789. return 0;
  2790. }
  2791. static int qgroup_auto_inherit(struct btrfs_fs_info *fs_info,
  2792. u64 inode_rootid,
  2793. struct btrfs_qgroup_inherit **inherit)
  2794. {
  2795. int i = 0;
  2796. u64 num_qgroups = 0;
  2797. struct btrfs_qgroup *inode_qg;
  2798. struct btrfs_qgroup_list *qg_list;
  2799. struct btrfs_qgroup_inherit *res;
  2800. size_t struct_sz;
  2801. u64 *qgids;
  2802. if (*inherit)
  2803. return -EEXIST;
  2804. inode_qg = find_qgroup_rb(fs_info, inode_rootid);
  2805. if (!inode_qg)
  2806. return -ENOENT;
  2807. num_qgroups = list_count_nodes(&inode_qg->groups);
  2808. if (!num_qgroups)
  2809. return 0;
  2810. struct_sz = struct_size(res, qgroups, num_qgroups);
  2811. if (struct_sz == SIZE_MAX)
  2812. return -ERANGE;
  2813. res = kzalloc(struct_sz, GFP_NOFS);
  2814. if (!res)
  2815. return -ENOMEM;
  2816. res->num_qgroups = num_qgroups;
  2817. qgids = res->qgroups;
  2818. list_for_each_entry(qg_list, &inode_qg->groups, next_group)
  2819. qgids[i++] = qg_list->group->qgroupid;
  2820. *inherit = res;
  2821. return 0;
  2822. }
  2823. /*
  2824. * Check if we can skip rescan when inheriting qgroups. If @src has a single
  2825. * @parent, and that @parent is owning all its bytes exclusively, we can skip
  2826. * the full rescan, by just adding nodesize to the @parent's excl/rfer.
  2827. *
  2828. * Return <0 for fatal errors (like srcid/parentid has no qgroup).
  2829. * Return 0 if a quick inherit is done.
  2830. * Return >0 if a quick inherit is not possible, and a full rescan is needed.
  2831. */
  2832. static int qgroup_snapshot_quick_inherit(struct btrfs_fs_info *fs_info,
  2833. u64 srcid, u64 parentid)
  2834. {
  2835. struct btrfs_qgroup *src;
  2836. struct btrfs_qgroup *parent;
  2837. struct btrfs_qgroup *qgroup;
  2838. struct btrfs_qgroup_list *list;
  2839. LIST_HEAD(qgroup_list);
  2840. const u32 nodesize = fs_info->nodesize;
  2841. int nr_parents = 0;
  2842. if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_FULL)
  2843. return 0;
  2844. src = find_qgroup_rb(fs_info, srcid);
  2845. if (!src)
  2846. return -ENOENT;
  2847. parent = find_qgroup_rb(fs_info, parentid);
  2848. if (!parent)
  2849. return -ENOENT;
  2850. /*
  2851. * Source has no parent qgroup, but our new qgroup would have one.
  2852. * Qgroup numbers would become inconsistent.
  2853. */
  2854. if (list_empty(&src->groups))
  2855. return 1;
  2856. list_for_each_entry(list, &src->groups, next_group) {
  2857. /* The parent is not the same, quick update is not possible. */
  2858. if (list->group->qgroupid != parentid)
  2859. return 1;
  2860. nr_parents++;
  2861. /*
  2862. * More than one parent qgroup, we can't be sure about accounting
  2863. * consistency.
  2864. */
  2865. if (nr_parents > 1)
  2866. return 1;
  2867. }
  2868. /*
  2869. * The parent is not exclusively owning all its bytes. We're not sure
  2870. * if the source has any bytes not fully owned by the parent.
  2871. */
  2872. if (parent->excl != parent->rfer)
  2873. return 1;
  2874. qgroup_iterator_add(&qgroup_list, parent);
  2875. list_for_each_entry(qgroup, &qgroup_list, iterator) {
  2876. qgroup->rfer += nodesize;
  2877. qgroup->rfer_cmpr += nodesize;
  2878. qgroup->excl += nodesize;
  2879. qgroup->excl_cmpr += nodesize;
  2880. qgroup_dirty(fs_info, qgroup);
  2881. /* Append parent qgroups to @qgroup_list. */
  2882. list_for_each_entry(list, &qgroup->groups, next_group)
  2883. qgroup_iterator_add(&qgroup_list, list->group);
  2884. }
  2885. qgroup_iterator_clean(&qgroup_list);
  2886. return 0;
  2887. }
  2888. /*
  2889. * Copy the accounting information between qgroups. This is necessary
  2890. * when a snapshot or a subvolume is created. Throwing an error will
  2891. * cause a transaction abort so we take extra care here to only error
  2892. * when a readonly fs is a reasonable outcome.
  2893. */
  2894. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
  2895. u64 objectid, u64 inode_rootid,
  2896. struct btrfs_qgroup_inherit *inherit)
  2897. {
  2898. int ret = 0;
  2899. u64 *i_qgroups;
  2900. bool committing = false;
  2901. struct btrfs_fs_info *fs_info = trans->fs_info;
  2902. struct btrfs_root *quota_root;
  2903. struct btrfs_qgroup *srcgroup;
  2904. struct btrfs_qgroup *dstgroup;
  2905. struct btrfs_qgroup *prealloc;
  2906. struct btrfs_qgroup_list **qlist_prealloc = NULL;
  2907. bool free_inherit = false;
  2908. bool need_rescan = false;
  2909. u32 level_size = 0;
  2910. u64 nums;
  2911. if (!btrfs_qgroup_enabled(fs_info))
  2912. return 0;
  2913. prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
  2914. if (!prealloc)
  2915. return -ENOMEM;
  2916. /*
  2917. * There are only two callers of this function.
  2918. *
  2919. * One in create_subvol() in the ioctl context, which needs to hold
  2920. * the qgroup_ioctl_lock.
  2921. *
  2922. * The other one in create_pending_snapshot() where no other qgroup
  2923. * code can modify the fs as they all need to either start a new trans
  2924. * or hold a trans handler, thus we don't need to hold
  2925. * qgroup_ioctl_lock.
  2926. * This would avoid long and complex lock chain and make lockdep happy.
  2927. */
  2928. spin_lock(&fs_info->trans_lock);
  2929. if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
  2930. committing = true;
  2931. spin_unlock(&fs_info->trans_lock);
  2932. if (!committing)
  2933. mutex_lock(&fs_info->qgroup_ioctl_lock);
  2934. quota_root = fs_info->quota_root;
  2935. if (!quota_root) {
  2936. ret = -EINVAL;
  2937. goto out;
  2938. }
  2939. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE && !inherit) {
  2940. ret = qgroup_auto_inherit(fs_info, inode_rootid, &inherit);
  2941. if (ret)
  2942. goto out;
  2943. free_inherit = true;
  2944. }
  2945. if (inherit) {
  2946. i_qgroups = (u64 *)(inherit + 1);
  2947. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  2948. 2 * inherit->num_excl_copies;
  2949. for (int i = 0; i < nums; i++) {
  2950. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  2951. /*
  2952. * Zero out invalid groups so we can ignore
  2953. * them later.
  2954. */
  2955. if (!srcgroup ||
  2956. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  2957. *i_qgroups = 0ULL;
  2958. ++i_qgroups;
  2959. }
  2960. }
  2961. /*
  2962. * create a tracking group for the subvol itself
  2963. */
  2964. ret = add_qgroup_item(trans, quota_root, objectid);
  2965. if (ret)
  2966. goto out;
  2967. /*
  2968. * add qgroup to all inherited groups
  2969. */
  2970. if (inherit) {
  2971. i_qgroups = (u64 *)(inherit + 1);
  2972. for (int i = 0; i < inherit->num_qgroups; i++, i_qgroups++) {
  2973. if (*i_qgroups == 0)
  2974. continue;
  2975. ret = add_qgroup_relation_item(trans, objectid,
  2976. *i_qgroups);
  2977. if (ret && ret != -EEXIST)
  2978. goto out;
  2979. ret = add_qgroup_relation_item(trans, *i_qgroups,
  2980. objectid);
  2981. if (ret && ret != -EEXIST)
  2982. goto out;
  2983. }
  2984. ret = 0;
  2985. qlist_prealloc = kzalloc_objs(struct btrfs_qgroup_list *,
  2986. inherit->num_qgroups, GFP_NOFS);
  2987. if (!qlist_prealloc) {
  2988. ret = -ENOMEM;
  2989. goto out;
  2990. }
  2991. for (int i = 0; i < inherit->num_qgroups; i++) {
  2992. qlist_prealloc[i] = kzalloc_obj(struct btrfs_qgroup_list,
  2993. GFP_NOFS);
  2994. if (!qlist_prealloc[i]) {
  2995. ret = -ENOMEM;
  2996. goto out;
  2997. }
  2998. }
  2999. }
  3000. spin_lock(&fs_info->qgroup_lock);
  3001. dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);
  3002. prealloc = NULL;
  3003. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  3004. dstgroup->lim_flags = inherit->lim.flags;
  3005. dstgroup->max_rfer = inherit->lim.max_rfer;
  3006. dstgroup->max_excl = inherit->lim.max_excl;
  3007. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  3008. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  3009. qgroup_dirty(fs_info, dstgroup);
  3010. }
  3011. if (srcid && btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL) {
  3012. srcgroup = find_qgroup_rb(fs_info, srcid);
  3013. if (!srcgroup)
  3014. goto unlock;
  3015. /*
  3016. * We call inherit after we clone the root in order to make sure
  3017. * our counts don't go crazy, so at this point the only
  3018. * difference between the two roots should be the root node.
  3019. */
  3020. level_size = fs_info->nodesize;
  3021. dstgroup->rfer = srcgroup->rfer;
  3022. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  3023. dstgroup->excl = level_size;
  3024. dstgroup->excl_cmpr = level_size;
  3025. srcgroup->excl = level_size;
  3026. srcgroup->excl_cmpr = level_size;
  3027. /* inherit the limit info */
  3028. dstgroup->lim_flags = srcgroup->lim_flags;
  3029. dstgroup->max_rfer = srcgroup->max_rfer;
  3030. dstgroup->max_excl = srcgroup->max_excl;
  3031. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  3032. dstgroup->rsv_excl = srcgroup->rsv_excl;
  3033. qgroup_dirty(fs_info, dstgroup);
  3034. qgroup_dirty(fs_info, srcgroup);
  3035. /*
  3036. * If the source qgroup has parent but the new one doesn't,
  3037. * we need a full rescan.
  3038. */
  3039. if (!inherit && !list_empty(&srcgroup->groups))
  3040. need_rescan = true;
  3041. }
  3042. if (!inherit)
  3043. goto unlock;
  3044. i_qgroups = (u64 *)(inherit + 1);
  3045. for (int i = 0; i < inherit->num_qgroups; i++) {
  3046. if (*i_qgroups) {
  3047. ret = add_relation_rb(fs_info, qlist_prealloc[i], objectid,
  3048. *i_qgroups);
  3049. qlist_prealloc[i] = NULL;
  3050. if (ret)
  3051. goto unlock;
  3052. }
  3053. if (srcid) {
  3054. /* Check if we can do a quick inherit. */
  3055. ret = qgroup_snapshot_quick_inherit(fs_info, srcid, *i_qgroups);
  3056. if (ret < 0)
  3057. goto unlock;
  3058. if (ret > 0)
  3059. need_rescan = true;
  3060. ret = 0;
  3061. }
  3062. ++i_qgroups;
  3063. }
  3064. for (int i = 0; i < inherit->num_ref_copies; i++, i_qgroups += 2) {
  3065. struct btrfs_qgroup *src;
  3066. struct btrfs_qgroup *dst;
  3067. if (!i_qgroups[0] || !i_qgroups[1])
  3068. continue;
  3069. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  3070. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  3071. if (!src || !dst) {
  3072. ret = -EINVAL;
  3073. goto unlock;
  3074. }
  3075. dst->rfer = src->rfer - level_size;
  3076. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  3077. /* Manually tweaking numbers certainly needs a rescan */
  3078. need_rescan = true;
  3079. }
  3080. for (int i = 0; i < inherit->num_excl_copies; i++, i_qgroups += 2) {
  3081. struct btrfs_qgroup *src;
  3082. struct btrfs_qgroup *dst;
  3083. if (!i_qgroups[0] || !i_qgroups[1])
  3084. continue;
  3085. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  3086. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  3087. if (!src || !dst) {
  3088. ret = -EINVAL;
  3089. goto unlock;
  3090. }
  3091. dst->excl = src->excl + level_size;
  3092. dst->excl_cmpr = src->excl_cmpr + level_size;
  3093. need_rescan = true;
  3094. }
  3095. unlock:
  3096. spin_unlock(&fs_info->qgroup_lock);
  3097. if (!ret)
  3098. ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
  3099. out:
  3100. if (!committing)
  3101. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  3102. if (need_rescan)
  3103. qgroup_mark_inconsistent(fs_info, "qgroup inherit needs a rescan");
  3104. if (qlist_prealloc) {
  3105. for (int i = 0; i < inherit->num_qgroups; i++)
  3106. kfree(qlist_prealloc[i]);
  3107. kfree(qlist_prealloc);
  3108. }
  3109. if (free_inherit)
  3110. kfree(inherit);
  3111. kfree(prealloc);
  3112. return ret;
  3113. }
  3114. static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
  3115. {
  3116. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  3117. qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
  3118. return false;
  3119. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  3120. qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
  3121. return false;
  3122. return true;
  3123. }
  3124. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
  3125. enum btrfs_qgroup_rsv_type type)
  3126. {
  3127. struct btrfs_qgroup *qgroup;
  3128. struct btrfs_fs_info *fs_info = root->fs_info;
  3129. u64 ref_root = btrfs_root_id(root);
  3130. int ret = 0;
  3131. LIST_HEAD(qgroup_list);
  3132. if (!btrfs_is_fstree(ref_root))
  3133. return 0;
  3134. if (num_bytes == 0)
  3135. return 0;
  3136. if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
  3137. capable(CAP_SYS_RESOURCE))
  3138. enforce = false;
  3139. spin_lock(&fs_info->qgroup_lock);
  3140. if (!fs_info->quota_root)
  3141. goto out;
  3142. qgroup = find_qgroup_rb(fs_info, ref_root);
  3143. if (!qgroup)
  3144. goto out;
  3145. qgroup_iterator_add(&qgroup_list, qgroup);
  3146. list_for_each_entry(qgroup, &qgroup_list, iterator) {
  3147. struct btrfs_qgroup_list *glist;
  3148. if (enforce && !qgroup_check_limits(qgroup, num_bytes)) {
  3149. ret = -EDQUOT;
  3150. goto out;
  3151. }
  3152. list_for_each_entry(glist, &qgroup->groups, next_group)
  3153. qgroup_iterator_add(&qgroup_list, glist->group);
  3154. }
  3155. ret = 0;
  3156. /*
  3157. * no limits exceeded, now record the reservation into all qgroups
  3158. */
  3159. list_for_each_entry(qgroup, &qgroup_list, iterator)
  3160. qgroup_rsv_add(fs_info, qgroup, num_bytes, type);
  3161. out:
  3162. qgroup_iterator_clean(&qgroup_list);
  3163. spin_unlock(&fs_info->qgroup_lock);
  3164. return ret;
  3165. }
  3166. /*
  3167. * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
  3168. * qgroup).
  3169. *
  3170. * Will handle all higher level qgroup too.
  3171. *
  3172. * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
  3173. * This special case is only used for META_PERTRANS type.
  3174. */
  3175. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  3176. u64 ref_root, u64 num_bytes,
  3177. enum btrfs_qgroup_rsv_type type)
  3178. {
  3179. struct btrfs_qgroup *qgroup;
  3180. LIST_HEAD(qgroup_list);
  3181. if (!btrfs_is_fstree(ref_root))
  3182. return;
  3183. if (num_bytes == 0)
  3184. return;
  3185. if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
  3186. WARN(1, "%s: Invalid type to free", __func__);
  3187. return;
  3188. }
  3189. spin_lock(&fs_info->qgroup_lock);
  3190. if (!fs_info->quota_root)
  3191. goto out;
  3192. qgroup = find_qgroup_rb(fs_info, ref_root);
  3193. if (!qgroup)
  3194. goto out;
  3195. if (num_bytes == (u64)-1)
  3196. /*
  3197. * We're freeing all pertrans rsv, get reserved value from
  3198. * level 0 qgroup as real num_bytes to free.
  3199. */
  3200. num_bytes = qgroup->rsv.values[type];
  3201. qgroup_iterator_add(&qgroup_list, qgroup);
  3202. list_for_each_entry(qgroup, &qgroup_list, iterator) {
  3203. struct btrfs_qgroup_list *glist;
  3204. qgroup_rsv_release(fs_info, qgroup, num_bytes, type);
  3205. list_for_each_entry(glist, &qgroup->groups, next_group) {
  3206. qgroup_iterator_add(&qgroup_list, glist->group);
  3207. }
  3208. }
  3209. out:
  3210. qgroup_iterator_clean(&qgroup_list);
  3211. spin_unlock(&fs_info->qgroup_lock);
  3212. }
  3213. /*
  3214. * Check if the leaf is the last leaf. Which means all node pointers
  3215. * are at their last position.
  3216. */
  3217. static bool is_last_leaf(struct btrfs_path *path)
  3218. {
  3219. int i;
  3220. for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
  3221. if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
  3222. return false;
  3223. }
  3224. return true;
  3225. }
  3226. /*
  3227. * returns < 0 on error, 0 when more leafs are to be scanned.
  3228. * returns 1 when done.
  3229. */
  3230. static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
  3231. struct btrfs_path *path)
  3232. {
  3233. struct btrfs_fs_info *fs_info = trans->fs_info;
  3234. struct btrfs_root *extent_root;
  3235. struct btrfs_key found;
  3236. struct extent_buffer *scratch_leaf = NULL;
  3237. u64 num_bytes;
  3238. bool done;
  3239. int slot;
  3240. int ret;
  3241. if (!btrfs_qgroup_full_accounting(fs_info))
  3242. return 1;
  3243. mutex_lock(&fs_info->qgroup_rescan_lock);
  3244. extent_root = btrfs_extent_root(fs_info,
  3245. fs_info->qgroup_rescan_progress.objectid);
  3246. if (unlikely(!extent_root)) {
  3247. btrfs_err(fs_info,
  3248. "missing extent root for extent at bytenr %llu",
  3249. fs_info->qgroup_rescan_progress.objectid);
  3250. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3251. return -EUCLEAN;
  3252. }
  3253. ret = btrfs_search_slot_for_read(extent_root,
  3254. &fs_info->qgroup_rescan_progress,
  3255. path, 1, 0);
  3256. btrfs_debug(fs_info,
  3257. "current progress key " BTRFS_KEY_FMT ", search_slot ret %d",
  3258. BTRFS_KEY_FMT_VALUE(&fs_info->qgroup_rescan_progress), ret);
  3259. if (ret) {
  3260. /*
  3261. * The rescan is about to end, we will not be scanning any
  3262. * further blocks. We cannot unset the RESCAN flag here, because
  3263. * we want to commit the transaction if everything went well.
  3264. * To make the live accounting work in this phase, we set our
  3265. * scan progress pointer such that every real extent objectid
  3266. * will be smaller.
  3267. */
  3268. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  3269. btrfs_release_path(path);
  3270. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3271. return ret;
  3272. }
  3273. done = is_last_leaf(path);
  3274. btrfs_item_key_to_cpu(path->nodes[0], &found,
  3275. btrfs_header_nritems(path->nodes[0]) - 1);
  3276. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  3277. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  3278. if (!scratch_leaf) {
  3279. ret = -ENOMEM;
  3280. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3281. goto out;
  3282. }
  3283. slot = path->slots[0];
  3284. btrfs_release_path(path);
  3285. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3286. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  3287. struct btrfs_backref_walk_ctx ctx = { 0 };
  3288. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  3289. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  3290. found.type != BTRFS_METADATA_ITEM_KEY)
  3291. continue;
  3292. if (found.type == BTRFS_METADATA_ITEM_KEY)
  3293. num_bytes = fs_info->nodesize;
  3294. else
  3295. num_bytes = found.offset;
  3296. ctx.bytenr = found.objectid;
  3297. ctx.fs_info = fs_info;
  3298. ret = btrfs_find_all_roots(&ctx, false);
  3299. if (ret < 0)
  3300. goto out;
  3301. /* For rescan, just pass old_roots as NULL */
  3302. ret = btrfs_qgroup_account_extent(trans, found.objectid,
  3303. num_bytes, NULL, ctx.roots);
  3304. if (ret < 0)
  3305. goto out;
  3306. }
  3307. out:
  3308. if (scratch_leaf)
  3309. free_extent_buffer(scratch_leaf);
  3310. if (done && !ret) {
  3311. ret = 1;
  3312. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  3313. }
  3314. return ret;
  3315. }
  3316. static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
  3317. {
  3318. if (btrfs_fs_closing(fs_info))
  3319. return true;
  3320. if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
  3321. return true;
  3322. if (!btrfs_qgroup_enabled(fs_info))
  3323. return true;
  3324. if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN)
  3325. return true;
  3326. return false;
  3327. }
  3328. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  3329. {
  3330. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  3331. qgroup_rescan_work);
  3332. struct btrfs_path *path;
  3333. struct btrfs_trans_handle *trans = NULL;
  3334. int ret = 0;
  3335. bool stopped = false;
  3336. bool did_leaf_rescans = false;
  3337. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
  3338. return;
  3339. path = btrfs_alloc_path();
  3340. if (!path) {
  3341. ret = -ENOMEM;
  3342. goto out;
  3343. }
  3344. /*
  3345. * Rescan should only search for commit root, and any later difference
  3346. * should be recorded by qgroup
  3347. */
  3348. path->search_commit_root = true;
  3349. path->skip_locking = true;
  3350. while (!ret && !(stopped = rescan_should_stop(fs_info))) {
  3351. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  3352. if (IS_ERR(trans)) {
  3353. ret = PTR_ERR(trans);
  3354. break;
  3355. }
  3356. ret = qgroup_rescan_leaf(trans, path);
  3357. did_leaf_rescans = true;
  3358. if (ret > 0)
  3359. btrfs_commit_transaction(trans);
  3360. else
  3361. btrfs_end_transaction(trans);
  3362. }
  3363. out:
  3364. btrfs_free_path(path);
  3365. mutex_lock(&fs_info->qgroup_rescan_lock);
  3366. if (ret > 0 &&
  3367. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  3368. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  3369. } else if (ret < 0 || stopped) {
  3370. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  3371. }
  3372. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3373. /*
  3374. * Only update status, since the previous part has already updated the
  3375. * qgroup info, and only if we did any actual work. This also prevents
  3376. * race with a concurrent quota disable, which has already set
  3377. * fs_info->quota_root to NULL and cleared BTRFS_FS_QUOTA_ENABLED at
  3378. * btrfs_quota_disable().
  3379. */
  3380. if (did_leaf_rescans) {
  3381. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  3382. if (IS_ERR(trans)) {
  3383. ret = PTR_ERR(trans);
  3384. trans = NULL;
  3385. btrfs_err(fs_info,
  3386. "fail to start transaction for status update: %d",
  3387. ret);
  3388. }
  3389. } else {
  3390. trans = NULL;
  3391. }
  3392. mutex_lock(&fs_info->qgroup_rescan_lock);
  3393. if (!stopped ||
  3394. fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN)
  3395. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  3396. if (trans) {
  3397. int ret2 = update_qgroup_status_item(trans);
  3398. if (ret2 < 0) {
  3399. ret = ret2;
  3400. btrfs_err(fs_info, "fail to update qgroup status: %d", ret);
  3401. }
  3402. }
  3403. fs_info->qgroup_rescan_running = false;
  3404. fs_info->qgroup_flags &= ~BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN;
  3405. complete_all(&fs_info->qgroup_rescan_completion);
  3406. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3407. if (!trans)
  3408. return;
  3409. btrfs_end_transaction(trans);
  3410. if (stopped) {
  3411. btrfs_info(fs_info, "qgroup scan paused");
  3412. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN) {
  3413. btrfs_info(fs_info, "qgroup scan cancelled");
  3414. } else if (ret >= 0) {
  3415. btrfs_info(fs_info, "qgroup scan completed%s",
  3416. ret > 0 ? " (inconsistency flag cleared)" : "");
  3417. } else {
  3418. btrfs_err(fs_info, "qgroup scan failed with %d", ret);
  3419. }
  3420. }
  3421. /*
  3422. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  3423. * memory required for the rescan context.
  3424. */
  3425. static int
  3426. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  3427. int init_flags)
  3428. {
  3429. int ret = 0;
  3430. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE) {
  3431. btrfs_warn(fs_info, "qgroup rescan init failed, running in simple mode");
  3432. return -EINVAL;
  3433. }
  3434. if (!init_flags) {
  3435. /* we're resuming qgroup rescan at mount time */
  3436. if (!(fs_info->qgroup_flags &
  3437. BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
  3438. btrfs_debug(fs_info,
  3439. "qgroup rescan init failed, qgroup rescan is not queued");
  3440. ret = -EINVAL;
  3441. } else if (!(fs_info->qgroup_flags &
  3442. BTRFS_QGROUP_STATUS_FLAG_ON)) {
  3443. btrfs_debug(fs_info,
  3444. "qgroup rescan init failed, qgroup is not enabled");
  3445. ret = -ENOTCONN;
  3446. }
  3447. if (ret)
  3448. return ret;
  3449. }
  3450. mutex_lock(&fs_info->qgroup_rescan_lock);
  3451. if (init_flags) {
  3452. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3453. ret = -EINPROGRESS;
  3454. } else if (!(fs_info->qgroup_flags &
  3455. BTRFS_QGROUP_STATUS_FLAG_ON)) {
  3456. btrfs_debug(fs_info,
  3457. "qgroup rescan init failed, qgroup is not enabled");
  3458. ret = -ENOTCONN;
  3459. } else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
  3460. /* Quota disable is in progress */
  3461. ret = -EBUSY;
  3462. }
  3463. if (ret) {
  3464. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3465. return ret;
  3466. }
  3467. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  3468. }
  3469. memset(&fs_info->qgroup_rescan_progress, 0,
  3470. sizeof(fs_info->qgroup_rescan_progress));
  3471. fs_info->qgroup_flags &= ~(BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
  3472. BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
  3473. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  3474. init_completion(&fs_info->qgroup_rescan_completion);
  3475. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3476. btrfs_init_work(&fs_info->qgroup_rescan_work,
  3477. btrfs_qgroup_rescan_worker, NULL);
  3478. return 0;
  3479. }
  3480. static void
  3481. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  3482. {
  3483. struct rb_node *n;
  3484. struct btrfs_qgroup *qgroup;
  3485. spin_lock(&fs_info->qgroup_lock);
  3486. /* clear all current qgroup tracking information */
  3487. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  3488. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  3489. qgroup->rfer = 0;
  3490. qgroup->rfer_cmpr = 0;
  3491. qgroup->excl = 0;
  3492. qgroup->excl_cmpr = 0;
  3493. qgroup_dirty(fs_info, qgroup);
  3494. }
  3495. spin_unlock(&fs_info->qgroup_lock);
  3496. }
  3497. int
  3498. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  3499. {
  3500. int ret = 0;
  3501. ret = qgroup_rescan_init(fs_info, 0, 1);
  3502. if (ret)
  3503. return ret;
  3504. /*
  3505. * We have set the rescan_progress to 0, which means no more
  3506. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  3507. * However, btrfs_qgroup_account_ref may be right after its call
  3508. * to btrfs_find_all_roots, in which case it would still do the
  3509. * accounting.
  3510. * To solve this, we're committing the transaction, which will
  3511. * ensure we run all delayed refs and only after that, we are
  3512. * going to clear all tracking information for a clean start.
  3513. */
  3514. ret = btrfs_commit_current_transaction(fs_info->fs_root);
  3515. if (ret) {
  3516. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  3517. return ret;
  3518. }
  3519. qgroup_rescan_zero_tracking(fs_info);
  3520. mutex_lock(&fs_info->qgroup_rescan_lock);
  3521. /*
  3522. * The rescan worker is only for full accounting qgroups, check if it's
  3523. * enabled as it is pointless to queue it otherwise. A concurrent quota
  3524. * disable may also have just cleared BTRFS_FS_QUOTA_ENABLED.
  3525. */
  3526. if (btrfs_qgroup_full_accounting(fs_info)) {
  3527. fs_info->qgroup_rescan_running = true;
  3528. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  3529. &fs_info->qgroup_rescan_work);
  3530. } else {
  3531. ret = -ENOTCONN;
  3532. }
  3533. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3534. return ret;
  3535. }
  3536. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  3537. bool interruptible)
  3538. {
  3539. int running;
  3540. int ret = 0;
  3541. mutex_lock(&fs_info->qgroup_rescan_lock);
  3542. running = fs_info->qgroup_rescan_running;
  3543. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3544. if (!running)
  3545. return 0;
  3546. if (interruptible)
  3547. ret = wait_for_completion_interruptible(
  3548. &fs_info->qgroup_rescan_completion);
  3549. else
  3550. wait_for_completion(&fs_info->qgroup_rescan_completion);
  3551. return ret;
  3552. }
  3553. /*
  3554. * this is only called from open_ctree where we're still single threaded, thus
  3555. * locking is omitted here.
  3556. */
  3557. void
  3558. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  3559. {
  3560. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3561. mutex_lock(&fs_info->qgroup_rescan_lock);
  3562. fs_info->qgroup_rescan_running = true;
  3563. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  3564. &fs_info->qgroup_rescan_work);
  3565. mutex_unlock(&fs_info->qgroup_rescan_lock);
  3566. }
  3567. }
  3568. #define rbtree_iterate_from_safe(node, next, start) \
  3569. for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
  3570. static int qgroup_unreserve_range(struct btrfs_inode *inode,
  3571. struct extent_changeset *reserved, u64 start,
  3572. u64 len)
  3573. {
  3574. struct rb_node *node;
  3575. struct rb_node *next;
  3576. struct ulist_node *entry;
  3577. int ret = 0;
  3578. node = reserved->range_changed.root.rb_node;
  3579. if (!node)
  3580. return 0;
  3581. while (node) {
  3582. entry = rb_entry(node, struct ulist_node, rb_node);
  3583. if (entry->val < start)
  3584. node = node->rb_right;
  3585. else
  3586. node = node->rb_left;
  3587. }
  3588. if (entry->val > start && rb_prev(&entry->rb_node))
  3589. entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
  3590. rb_node);
  3591. rbtree_iterate_from_safe(node, next, &entry->rb_node) {
  3592. u64 entry_start;
  3593. u64 entry_end;
  3594. u64 entry_len;
  3595. int clear_ret;
  3596. entry = rb_entry(node, struct ulist_node, rb_node);
  3597. entry_start = entry->val;
  3598. entry_end = entry->aux;
  3599. entry_len = entry_end - entry_start + 1;
  3600. if (entry_start >= start + len)
  3601. break;
  3602. if (entry_start + entry_len <= start)
  3603. continue;
  3604. /*
  3605. * Now the entry is in [start, start + len), revert the
  3606. * EXTENT_QGROUP_RESERVED bit.
  3607. */
  3608. clear_ret = btrfs_clear_extent_bit(&inode->io_tree, entry_start, entry_end,
  3609. EXTENT_QGROUP_RESERVED, NULL);
  3610. if (!ret && clear_ret < 0)
  3611. ret = clear_ret;
  3612. ulist_del(&reserved->range_changed, entry->val, entry->aux);
  3613. if (likely(reserved->bytes_changed >= entry_len)) {
  3614. reserved->bytes_changed -= entry_len;
  3615. } else {
  3616. WARN_ON(1);
  3617. reserved->bytes_changed = 0;
  3618. }
  3619. }
  3620. return ret;
  3621. }
  3622. /*
  3623. * Try to free some space for qgroup.
  3624. *
  3625. * For qgroup, there are only 3 ways to free qgroup space:
  3626. * - Flush nodatacow write
  3627. * Any nodatacow write will free its reserved data space at run_delalloc_range().
  3628. * In theory, we should only flush nodatacow inodes, but it's not yet
  3629. * possible, so we need to flush the whole root.
  3630. *
  3631. * - Wait for ordered extents
  3632. * When ordered extents are finished, their reserved metadata is finally
  3633. * converted to per_trans status, which can be freed by later commit
  3634. * transaction.
  3635. *
  3636. * - Commit transaction
  3637. * This would free the meta_per_trans space.
  3638. * In theory this shouldn't provide much space, but any more qgroup space
  3639. * is needed.
  3640. */
  3641. static int try_flush_qgroup(struct btrfs_root *root)
  3642. {
  3643. int ret;
  3644. /* Can't hold an open transaction or we run the risk of deadlocking. */
  3645. ASSERT(current->journal_info == NULL);
  3646. if (WARN_ON(current->journal_info))
  3647. return 0;
  3648. /*
  3649. * We don't want to run flush again and again, so if there is a running
  3650. * one, we won't try to start a new flush, but exit directly.
  3651. */
  3652. if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
  3653. wait_event(root->qgroup_flush_wait,
  3654. !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
  3655. return 0;
  3656. }
  3657. ret = btrfs_start_delalloc_snapshot(root, true);
  3658. if (ret < 0)
  3659. goto out;
  3660. btrfs_wait_ordered_extents(root, U64_MAX, NULL);
  3661. /*
  3662. * After waiting for ordered extents run delayed iputs in order to free
  3663. * space from unlinked files before committing the current transaction,
  3664. * as ordered extents may have been holding the last reference of an
  3665. * inode and they add a delayed iput when they complete.
  3666. */
  3667. btrfs_run_delayed_iputs(root->fs_info);
  3668. btrfs_wait_on_delayed_iputs(root->fs_info);
  3669. ret = btrfs_commit_current_transaction(root);
  3670. out:
  3671. clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
  3672. wake_up(&root->qgroup_flush_wait);
  3673. return ret;
  3674. }
  3675. static int qgroup_reserve_data(struct btrfs_inode *inode,
  3676. struct extent_changeset **reserved_ret, u64 start,
  3677. u64 len)
  3678. {
  3679. struct btrfs_root *root = inode->root;
  3680. struct extent_changeset *reserved;
  3681. bool new_reserved = false;
  3682. u64 orig_reserved;
  3683. u64 to_reserve;
  3684. int ret;
  3685. if (btrfs_qgroup_mode(root->fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
  3686. !btrfs_is_fstree(btrfs_root_id(root)) || len == 0)
  3687. return 0;
  3688. /* @reserved parameter is mandatory for qgroup */
  3689. if (WARN_ON(!reserved_ret))
  3690. return -EINVAL;
  3691. if (!*reserved_ret) {
  3692. new_reserved = true;
  3693. *reserved_ret = extent_changeset_alloc();
  3694. if (!*reserved_ret)
  3695. return -ENOMEM;
  3696. }
  3697. reserved = *reserved_ret;
  3698. /* Record already reserved space */
  3699. orig_reserved = reserved->bytes_changed;
  3700. ret = btrfs_set_record_extent_bits(&inode->io_tree, start,
  3701. start + len - 1, EXTENT_QGROUP_RESERVED,
  3702. reserved);
  3703. /* Newly reserved space */
  3704. to_reserve = reserved->bytes_changed - orig_reserved;
  3705. trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
  3706. to_reserve, QGROUP_RESERVE);
  3707. if (ret < 0)
  3708. goto out;
  3709. ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
  3710. if (ret < 0)
  3711. goto cleanup;
  3712. return ret;
  3713. cleanup:
  3714. qgroup_unreserve_range(inode, reserved, start, len);
  3715. out:
  3716. if (new_reserved) {
  3717. extent_changeset_free(reserved);
  3718. *reserved_ret = NULL;
  3719. }
  3720. return ret;
  3721. }
  3722. /*
  3723. * Reserve qgroup space for range [start, start + len).
  3724. *
  3725. * This function will either reserve space from related qgroups or do nothing
  3726. * if the range is already reserved.
  3727. *
  3728. * Return 0 for successful reservation
  3729. * Return <0 for error (including -EQUOT)
  3730. *
  3731. * NOTE: This function may sleep for memory allocation, dirty page flushing and
  3732. * commit transaction. So caller should not hold any dirty page locked.
  3733. */
  3734. int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
  3735. struct extent_changeset **reserved_ret, u64 start,
  3736. u64 len)
  3737. {
  3738. int ret;
  3739. ret = qgroup_reserve_data(inode, reserved_ret, start, len);
  3740. if (ret <= 0 && ret != -EDQUOT)
  3741. return ret;
  3742. ret = try_flush_qgroup(inode->root);
  3743. if (ret < 0)
  3744. return ret;
  3745. return qgroup_reserve_data(inode, reserved_ret, start, len);
  3746. }
  3747. /* Free ranges specified by @reserved, normally in error path */
  3748. static int qgroup_free_reserved_data(struct btrfs_inode *inode,
  3749. struct extent_changeset *reserved,
  3750. u64 start, u64 len, u64 *freed_ret)
  3751. {
  3752. struct btrfs_root *root = inode->root;
  3753. struct ulist_node *unode;
  3754. struct ulist_iterator uiter;
  3755. struct extent_changeset changeset;
  3756. u64 freed = 0;
  3757. int ret;
  3758. extent_changeset_init(&changeset);
  3759. len = round_up(start + len, root->fs_info->sectorsize);
  3760. start = round_down(start, root->fs_info->sectorsize);
  3761. ULIST_ITER_INIT(&uiter);
  3762. while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
  3763. u64 range_start = unode->val;
  3764. /* unode->aux is the inclusive end */
  3765. u64 range_len = unode->aux - range_start + 1;
  3766. u64 free_start;
  3767. u64 free_len;
  3768. extent_changeset_release(&changeset);
  3769. /* Only free range in range [start, start + len) */
  3770. if (range_start >= start + len ||
  3771. range_start + range_len <= start)
  3772. continue;
  3773. free_start = max(range_start, start);
  3774. free_len = min(start + len, range_start + range_len) -
  3775. free_start;
  3776. /*
  3777. * TODO: To also modify reserved->ranges_reserved to reflect
  3778. * the modification.
  3779. *
  3780. * However as long as we free qgroup reserved according to
  3781. * EXTENT_QGROUP_RESERVED, we won't double free.
  3782. * So not need to rush.
  3783. */
  3784. ret = btrfs_clear_record_extent_bits(&inode->io_tree, free_start,
  3785. free_start + free_len - 1,
  3786. EXTENT_QGROUP_RESERVED,
  3787. &changeset);
  3788. if (ret < 0)
  3789. goto out;
  3790. freed += changeset.bytes_changed;
  3791. }
  3792. btrfs_qgroup_free_refroot(root->fs_info, btrfs_root_id(root), freed,
  3793. BTRFS_QGROUP_RSV_DATA);
  3794. if (freed_ret)
  3795. *freed_ret = freed;
  3796. ret = 0;
  3797. out:
  3798. extent_changeset_release(&changeset);
  3799. return ret;
  3800. }
  3801. static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
  3802. struct extent_changeset *reserved, u64 start, u64 len,
  3803. u64 *released, int free)
  3804. {
  3805. struct extent_changeset changeset;
  3806. int trace_op = QGROUP_RELEASE;
  3807. int ret;
  3808. if (btrfs_qgroup_mode(inode->root->fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
  3809. return btrfs_clear_record_extent_bits(&inode->io_tree, start,
  3810. start + len - 1,
  3811. EXTENT_QGROUP_RESERVED, NULL);
  3812. }
  3813. /* In release case, we shouldn't have @reserved */
  3814. WARN_ON(!free && reserved);
  3815. if (free && reserved)
  3816. return qgroup_free_reserved_data(inode, reserved, start, len, released);
  3817. extent_changeset_init(&changeset);
  3818. ret = btrfs_clear_record_extent_bits(&inode->io_tree, start, start + len - 1,
  3819. EXTENT_QGROUP_RESERVED, &changeset);
  3820. if (ret < 0)
  3821. goto out;
  3822. if (free)
  3823. trace_op = QGROUP_FREE;
  3824. trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
  3825. changeset.bytes_changed, trace_op);
  3826. if (free)
  3827. btrfs_qgroup_free_refroot(inode->root->fs_info,
  3828. btrfs_root_id(inode->root),
  3829. changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
  3830. if (released)
  3831. *released = changeset.bytes_changed;
  3832. out:
  3833. extent_changeset_release(&changeset);
  3834. return ret;
  3835. }
  3836. /*
  3837. * Free a reserved space range from io_tree and related qgroups
  3838. *
  3839. * Should be called when a range of pages get invalidated before reaching disk.
  3840. * Or for error cleanup case.
  3841. * if @reserved is given, only reserved range in [@start, @start + @len) will
  3842. * be freed.
  3843. *
  3844. * For data written to disk, use btrfs_qgroup_release_data().
  3845. *
  3846. * NOTE: This function may sleep for memory allocation.
  3847. */
  3848. int btrfs_qgroup_free_data(struct btrfs_inode *inode,
  3849. struct extent_changeset *reserved,
  3850. u64 start, u64 len, u64 *freed)
  3851. {
  3852. return __btrfs_qgroup_release_data(inode, reserved, start, len, freed, 1);
  3853. }
  3854. /*
  3855. * Release a reserved space range from io_tree only.
  3856. *
  3857. * Should be called when a range of pages get written to disk and corresponding
  3858. * FILE_EXTENT is inserted into corresponding root.
  3859. *
  3860. * Since new qgroup accounting framework will only update qgroup numbers at
  3861. * commit_transaction() time, its reserved space shouldn't be freed from
  3862. * related qgroups.
  3863. *
  3864. * But we should release the range from io_tree, to allow further write to be
  3865. * COWed.
  3866. *
  3867. * NOTE: This function may sleep for memory allocation.
  3868. */
  3869. int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len, u64 *released)
  3870. {
  3871. return __btrfs_qgroup_release_data(inode, NULL, start, len, released, 0);
  3872. }
  3873. static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
  3874. enum btrfs_qgroup_rsv_type type)
  3875. {
  3876. if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
  3877. type != BTRFS_QGROUP_RSV_META_PERTRANS)
  3878. return;
  3879. if (num_bytes == 0)
  3880. return;
  3881. spin_lock(&root->qgroup_meta_rsv_lock);
  3882. if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
  3883. root->qgroup_meta_rsv_prealloc += num_bytes;
  3884. else
  3885. root->qgroup_meta_rsv_pertrans += num_bytes;
  3886. spin_unlock(&root->qgroup_meta_rsv_lock);
  3887. }
  3888. static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
  3889. enum btrfs_qgroup_rsv_type type)
  3890. {
  3891. if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
  3892. type != BTRFS_QGROUP_RSV_META_PERTRANS)
  3893. return 0;
  3894. if (num_bytes == 0)
  3895. return 0;
  3896. spin_lock(&root->qgroup_meta_rsv_lock);
  3897. if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
  3898. num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
  3899. num_bytes);
  3900. root->qgroup_meta_rsv_prealloc -= num_bytes;
  3901. } else {
  3902. num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
  3903. num_bytes);
  3904. root->qgroup_meta_rsv_pertrans -= num_bytes;
  3905. }
  3906. spin_unlock(&root->qgroup_meta_rsv_lock);
  3907. return num_bytes;
  3908. }
  3909. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  3910. enum btrfs_qgroup_rsv_type type, bool enforce)
  3911. {
  3912. struct btrfs_fs_info *fs_info = root->fs_info;
  3913. int ret;
  3914. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
  3915. !btrfs_is_fstree(btrfs_root_id(root)) || num_bytes == 0)
  3916. return 0;
  3917. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  3918. trace_btrfs_qgroup_meta_reserve(root, (s64)num_bytes, type);
  3919. ret = qgroup_reserve(root, num_bytes, enforce, type);
  3920. if (ret < 0)
  3921. return ret;
  3922. /*
  3923. * Record what we have reserved into root.
  3924. *
  3925. * To avoid quota disabled->enabled underflow.
  3926. * In that case, we may try to free space we haven't reserved
  3927. * (since quota was disabled), so record what we reserved into root.
  3928. * And ensure later release won't underflow this number.
  3929. */
  3930. add_root_meta_rsv(root, num_bytes, type);
  3931. return ret;
  3932. }
  3933. int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  3934. enum btrfs_qgroup_rsv_type type, bool enforce,
  3935. bool noflush)
  3936. {
  3937. int ret;
  3938. ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
  3939. if ((ret <= 0 && ret != -EDQUOT) || noflush)
  3940. return ret;
  3941. ret = try_flush_qgroup(root);
  3942. if (ret < 0)
  3943. return ret;
  3944. return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
  3945. }
  3946. /*
  3947. * Per-transaction meta reservation should be all freed at transaction commit
  3948. * time
  3949. */
  3950. void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
  3951. {
  3952. struct btrfs_fs_info *fs_info = root->fs_info;
  3953. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
  3954. !btrfs_is_fstree(btrfs_root_id(root)))
  3955. return;
  3956. /* TODO: Update trace point to handle such free */
  3957. trace_btrfs_qgroup_meta_free_all_pertrans(root);
  3958. /* Special value -1 means to free all reserved space */
  3959. btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(root), (u64)-1,
  3960. BTRFS_QGROUP_RSV_META_PERTRANS);
  3961. }
  3962. void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
  3963. enum btrfs_qgroup_rsv_type type)
  3964. {
  3965. struct btrfs_fs_info *fs_info = root->fs_info;
  3966. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
  3967. !btrfs_is_fstree(btrfs_root_id(root)))
  3968. return;
  3969. /*
  3970. * reservation for META_PREALLOC can happen before quota is enabled,
  3971. * which can lead to underflow.
  3972. * Here ensure we will only free what we really have reserved.
  3973. */
  3974. num_bytes = sub_root_meta_rsv(root, num_bytes, type);
  3975. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  3976. trace_btrfs_qgroup_meta_reserve(root, -(s64)num_bytes, type);
  3977. btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(root), num_bytes, type);
  3978. }
  3979. static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
  3980. int num_bytes)
  3981. {
  3982. struct btrfs_qgroup *qgroup;
  3983. LIST_HEAD(qgroup_list);
  3984. if (num_bytes == 0)
  3985. return;
  3986. if (!fs_info->quota_root)
  3987. return;
  3988. spin_lock(&fs_info->qgroup_lock);
  3989. qgroup = find_qgroup_rb(fs_info, ref_root);
  3990. if (!qgroup)
  3991. goto out;
  3992. qgroup_iterator_add(&qgroup_list, qgroup);
  3993. list_for_each_entry(qgroup, &qgroup_list, iterator) {
  3994. struct btrfs_qgroup_list *glist;
  3995. qgroup_rsv_release(fs_info, qgroup, num_bytes,
  3996. BTRFS_QGROUP_RSV_META_PREALLOC);
  3997. if (!sb_rdonly(fs_info->sb))
  3998. qgroup_rsv_add(fs_info, qgroup, num_bytes,
  3999. BTRFS_QGROUP_RSV_META_PERTRANS);
  4000. list_for_each_entry(glist, &qgroup->groups, next_group)
  4001. qgroup_iterator_add(&qgroup_list, glist->group);
  4002. }
  4003. out:
  4004. qgroup_iterator_clean(&qgroup_list);
  4005. spin_unlock(&fs_info->qgroup_lock);
  4006. }
  4007. /*
  4008. * Convert @num_bytes of META_PREALLOCATED reservation to META_PERTRANS.
  4009. *
  4010. * This is called when preallocated meta reservation needs to be used.
  4011. * Normally after btrfs_join_transaction() call.
  4012. */
  4013. void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
  4014. {
  4015. struct btrfs_fs_info *fs_info = root->fs_info;
  4016. if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
  4017. !btrfs_is_fstree(btrfs_root_id(root)))
  4018. return;
  4019. /* Same as btrfs_qgroup_free_meta_prealloc() */
  4020. num_bytes = sub_root_meta_rsv(root, num_bytes,
  4021. BTRFS_QGROUP_RSV_META_PREALLOC);
  4022. trace_btrfs_qgroup_meta_convert(root, num_bytes);
  4023. qgroup_convert_meta(fs_info, btrfs_root_id(root), num_bytes);
  4024. if (!sb_rdonly(fs_info->sb))
  4025. add_root_meta_rsv(root, num_bytes, BTRFS_QGROUP_RSV_META_PERTRANS);
  4026. }
  4027. /*
  4028. * Check qgroup reserved space leaking, normally at destroy inode
  4029. * time
  4030. */
  4031. void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
  4032. {
  4033. struct extent_changeset changeset;
  4034. struct ulist_node *unode;
  4035. struct ulist_iterator iter;
  4036. int ret;
  4037. extent_changeset_init(&changeset);
  4038. ret = btrfs_clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
  4039. EXTENT_QGROUP_RESERVED, &changeset);
  4040. WARN_ON(ret < 0);
  4041. if (WARN_ON(changeset.bytes_changed)) {
  4042. ULIST_ITER_INIT(&iter);
  4043. while ((unode = ulist_next(&changeset.range_changed, &iter))) {
  4044. btrfs_warn(inode->root->fs_info,
  4045. "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
  4046. btrfs_ino(inode), unode->val, unode->aux);
  4047. }
  4048. btrfs_qgroup_free_refroot(inode->root->fs_info,
  4049. btrfs_root_id(inode->root),
  4050. changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
  4051. }
  4052. extent_changeset_release(&changeset);
  4053. }
  4054. void btrfs_qgroup_init_swapped_blocks(
  4055. struct btrfs_qgroup_swapped_blocks *swapped_blocks)
  4056. {
  4057. int i;
  4058. spin_lock_init(&swapped_blocks->lock);
  4059. for (i = 0; i < BTRFS_MAX_LEVEL; i++)
  4060. swapped_blocks->blocks[i] = RB_ROOT;
  4061. swapped_blocks->swapped = false;
  4062. }
  4063. /*
  4064. * Delete all swapped blocks record of @root.
  4065. * Every record here means we skipped a full subtree scan for qgroup.
  4066. *
  4067. * Gets called when committing one transaction.
  4068. */
  4069. void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
  4070. {
  4071. struct btrfs_qgroup_swapped_blocks *swapped_blocks;
  4072. int i;
  4073. swapped_blocks = &root->swapped_blocks;
  4074. spin_lock(&swapped_blocks->lock);
  4075. if (!swapped_blocks->swapped)
  4076. goto out;
  4077. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  4078. struct rb_root *cur_root = &swapped_blocks->blocks[i];
  4079. struct btrfs_qgroup_swapped_block *entry;
  4080. struct btrfs_qgroup_swapped_block *next;
  4081. rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
  4082. node)
  4083. kfree(entry);
  4084. swapped_blocks->blocks[i] = RB_ROOT;
  4085. }
  4086. swapped_blocks->swapped = false;
  4087. out:
  4088. spin_unlock(&swapped_blocks->lock);
  4089. }
  4090. static int qgroup_swapped_block_bytenr_key_cmp(const void *key, const struct rb_node *node)
  4091. {
  4092. const u64 *bytenr = key;
  4093. const struct btrfs_qgroup_swapped_block *block = rb_entry(node,
  4094. struct btrfs_qgroup_swapped_block, node);
  4095. if (block->subvol_bytenr < *bytenr)
  4096. return -1;
  4097. else if (block->subvol_bytenr > *bytenr)
  4098. return 1;
  4099. return 0;
  4100. }
  4101. static int qgroup_swapped_block_bytenr_cmp(struct rb_node *new, const struct rb_node *existing)
  4102. {
  4103. const struct btrfs_qgroup_swapped_block *new_block = rb_entry(new,
  4104. struct btrfs_qgroup_swapped_block, node);
  4105. return qgroup_swapped_block_bytenr_key_cmp(&new_block->subvol_bytenr, existing);
  4106. }
  4107. /*
  4108. * Add subtree roots record into @subvol_root.
  4109. *
  4110. * @subvol_root: tree root of the subvolume tree get swapped
  4111. * @bg: block group under balance
  4112. * @subvol_parent/slot: pointer to the subtree root in subvolume tree
  4113. * @reloc_parent/slot: pointer to the subtree root in reloc tree
  4114. * BOTH POINTERS ARE BEFORE TREE SWAP
  4115. * @last_snapshot: last snapshot generation of the subvolume tree
  4116. */
  4117. int btrfs_qgroup_add_swapped_blocks(struct btrfs_root *subvol_root,
  4118. struct btrfs_block_group *bg,
  4119. struct extent_buffer *subvol_parent, int subvol_slot,
  4120. struct extent_buffer *reloc_parent, int reloc_slot,
  4121. u64 last_snapshot)
  4122. {
  4123. struct btrfs_fs_info *fs_info = subvol_root->fs_info;
  4124. struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
  4125. struct btrfs_qgroup_swapped_block *block;
  4126. struct rb_node *node;
  4127. int level = btrfs_header_level(subvol_parent) - 1;
  4128. int ret = 0;
  4129. if (!btrfs_qgroup_full_accounting(fs_info))
  4130. return 0;
  4131. if (unlikely(btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
  4132. btrfs_node_ptr_generation(reloc_parent, reloc_slot))) {
  4133. btrfs_err_rl(fs_info,
  4134. "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
  4135. __func__,
  4136. btrfs_node_ptr_generation(subvol_parent, subvol_slot),
  4137. btrfs_node_ptr_generation(reloc_parent, reloc_slot));
  4138. return -EUCLEAN;
  4139. }
  4140. block = kmalloc_obj(*block, GFP_NOFS);
  4141. if (!block) {
  4142. ret = -ENOMEM;
  4143. goto out;
  4144. }
  4145. /*
  4146. * @reloc_parent/slot is still before swap, while @block is going to
  4147. * record the bytenr after swap, so we do the swap here.
  4148. */
  4149. block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
  4150. block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
  4151. reloc_slot);
  4152. block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
  4153. block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
  4154. subvol_slot);
  4155. block->last_snapshot = last_snapshot;
  4156. block->level = level;
  4157. /*
  4158. * If we have bg == NULL, we're called from btrfs_recover_relocation(),
  4159. * no one else can modify tree blocks thus we qgroup will not change
  4160. * no matter the value of trace_leaf.
  4161. */
  4162. if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
  4163. block->trace_leaf = true;
  4164. else
  4165. block->trace_leaf = false;
  4166. btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
  4167. /* Insert @block into @blocks */
  4168. spin_lock(&blocks->lock);
  4169. node = rb_find_add(&block->node, &blocks->blocks[level], qgroup_swapped_block_bytenr_cmp);
  4170. if (node) {
  4171. struct btrfs_qgroup_swapped_block *entry;
  4172. entry = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
  4173. if (entry->subvol_generation != block->subvol_generation ||
  4174. entry->reloc_bytenr != block->reloc_bytenr ||
  4175. entry->reloc_generation != block->reloc_generation) {
  4176. /*
  4177. * Duplicated but mismatch entry found. Shouldn't happen.
  4178. * Marking qgroup inconsistent should be enough for end
  4179. * users.
  4180. */
  4181. DEBUG_WARN("duplicated but mismatched entry found");
  4182. ret = -EEXIST;
  4183. }
  4184. kfree(block);
  4185. goto out_unlock;
  4186. }
  4187. blocks->swapped = true;
  4188. out_unlock:
  4189. spin_unlock(&blocks->lock);
  4190. out:
  4191. if (ret < 0)
  4192. qgroup_mark_inconsistent(fs_info, "%s error: %d", __func__, ret);
  4193. return ret;
  4194. }
  4195. /*
  4196. * Check if the tree block is a subtree root, and if so do the needed
  4197. * delayed subtree trace for qgroup.
  4198. *
  4199. * This is called during btrfs_cow_block().
  4200. */
  4201. int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
  4202. struct btrfs_root *root,
  4203. struct extent_buffer *subvol_eb)
  4204. {
  4205. struct btrfs_fs_info *fs_info = root->fs_info;
  4206. struct btrfs_tree_parent_check check = { 0 };
  4207. struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
  4208. struct btrfs_qgroup_swapped_block AUTO_KFREE(block);
  4209. struct extent_buffer *reloc_eb = NULL;
  4210. struct rb_node *node;
  4211. bool swapped = false;
  4212. int level = btrfs_header_level(subvol_eb);
  4213. int ret = 0;
  4214. int i;
  4215. if (!btrfs_qgroup_full_accounting(fs_info))
  4216. return 0;
  4217. if (!btrfs_is_fstree(btrfs_root_id(root)) || !root->reloc_root)
  4218. return 0;
  4219. spin_lock(&blocks->lock);
  4220. if (!blocks->swapped) {
  4221. spin_unlock(&blocks->lock);
  4222. return 0;
  4223. }
  4224. node = rb_find(&subvol_eb->start, &blocks->blocks[level],
  4225. qgroup_swapped_block_bytenr_key_cmp);
  4226. if (!node) {
  4227. spin_unlock(&blocks->lock);
  4228. goto out;
  4229. }
  4230. block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
  4231. /* Found one, remove it from @blocks first and update blocks->swapped */
  4232. rb_erase(&block->node, &blocks->blocks[level]);
  4233. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  4234. if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
  4235. swapped = true;
  4236. break;
  4237. }
  4238. }
  4239. blocks->swapped = swapped;
  4240. spin_unlock(&blocks->lock);
  4241. check.level = block->level;
  4242. check.transid = block->reloc_generation;
  4243. check.has_first_key = true;
  4244. memcpy(&check.first_key, &block->first_key, sizeof(check.first_key));
  4245. /* Read out reloc subtree root */
  4246. reloc_eb = read_tree_block(fs_info, block->reloc_bytenr, &check);
  4247. if (IS_ERR(reloc_eb)) {
  4248. ret = PTR_ERR(reloc_eb);
  4249. reloc_eb = NULL;
  4250. goto free_out;
  4251. }
  4252. if (unlikely(!extent_buffer_uptodate(reloc_eb))) {
  4253. ret = -EIO;
  4254. goto free_out;
  4255. }
  4256. ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
  4257. block->last_snapshot, block->trace_leaf);
  4258. free_out:
  4259. free_extent_buffer(reloc_eb);
  4260. out:
  4261. if (ret < 0) {
  4262. qgroup_mark_inconsistent(fs_info,
  4263. "failed to account subtree at bytenr %llu: %d",
  4264. subvol_eb->start, ret);
  4265. }
  4266. return ret;
  4267. }
  4268. void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
  4269. {
  4270. struct btrfs_qgroup_extent_record *entry;
  4271. unsigned long index;
  4272. xa_for_each(&trans->delayed_refs.dirty_extents, index, entry) {
  4273. ulist_free(entry->old_roots);
  4274. kfree(entry);
  4275. }
  4276. xa_destroy(&trans->delayed_refs.dirty_extents);
  4277. }
  4278. int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
  4279. const struct btrfs_squota_delta *delta)
  4280. {
  4281. int ret;
  4282. struct btrfs_qgroup *qgroup;
  4283. struct btrfs_qgroup *qg;
  4284. LIST_HEAD(qgroup_list);
  4285. u64 root = delta->root;
  4286. u64 num_bytes = delta->num_bytes;
  4287. const int sign = (delta->is_inc ? 1 : -1);
  4288. if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
  4289. return 0;
  4290. if (!btrfs_is_fstree(root))
  4291. return 0;
  4292. /* If the extent predates enabling quotas, don't count it. */
  4293. if (delta->generation < fs_info->qgroup_enable_gen)
  4294. return 0;
  4295. spin_lock(&fs_info->qgroup_lock);
  4296. qgroup = find_qgroup_rb(fs_info, root);
  4297. if (!qgroup) {
  4298. ret = -ENOENT;
  4299. goto out;
  4300. }
  4301. ret = 0;
  4302. qgroup_iterator_add(&qgroup_list, qgroup);
  4303. list_for_each_entry(qg, &qgroup_list, iterator) {
  4304. struct btrfs_qgroup_list *glist;
  4305. qg->excl += num_bytes * sign;
  4306. qg->rfer += num_bytes * sign;
  4307. qgroup_dirty(fs_info, qg);
  4308. list_for_each_entry(glist, &qg->groups, next_group)
  4309. qgroup_iterator_add(&qgroup_list, glist->group);
  4310. }
  4311. qgroup_iterator_clean(&qgroup_list);
  4312. out:
  4313. spin_unlock(&fs_info->qgroup_lock);
  4314. return ret;
  4315. }