qgroup.c 133 KB

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