12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Copyright (C) 2011 STRATO. All rights reserved.
- */
- #include <linux/sched.h>
- #include <linux/pagemap.h>
- #include <linux/writeback.h>
- #include <linux/blkdev.h>
- #include <linux/rbtree.h>
- #include <linux/slab.h>
- #include <linux/workqueue.h>
- #include <linux/btrfs.h>
- #include <linux/sizes.h>
- #include "ctree.h"
- #include "transaction.h"
- #include "disk-io.h"
- #include "locking.h"
- #include "ulist.h"
- #include "backref.h"
- #include "extent_io.h"
- #include "qgroup.h"
- /* TODO XXX FIXME
- * - subvol delete -> delete when ref goes to 0? delete limits also?
- * - reorganize keys
- * - compressed
- * - sync
- * - copy also limits on subvol creation
- * - limit
- * - caches fuer ulists
- * - performance benchmarks
- * - check all ioctl parameters
- */
- /*
- * Helpers to access qgroup reservation
- *
- * Callers should ensure the lock context and type are valid
- */
- static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
- {
- u64 ret = 0;
- int i;
- for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
- ret += qgroup->rsv.values[i];
- return ret;
- }
- #ifdef CONFIG_BTRFS_DEBUG
- static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
- {
- if (type == BTRFS_QGROUP_RSV_DATA)
- return "data";
- if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
- return "meta_pertrans";
- if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
- return "meta_prealloc";
- return NULL;
- }
- #endif
- static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup *qgroup, u64 num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
- qgroup->rsv.values[type] += num_bytes;
- }
- static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup *qgroup, u64 num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
- if (qgroup->rsv.values[type] >= num_bytes) {
- qgroup->rsv.values[type] -= num_bytes;
- return;
- }
- #ifdef CONFIG_BTRFS_DEBUG
- WARN_RATELIMIT(1,
- "qgroup %llu %s reserved space underflow, have %llu to free %llu",
- qgroup->qgroupid, qgroup_rsv_type_str(type),
- qgroup->rsv.values[type], num_bytes);
- #endif
- qgroup->rsv.values[type] = 0;
- }
- static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup *dest,
- struct btrfs_qgroup *src)
- {
- int i;
- for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
- qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
- }
- static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup *dest,
- struct btrfs_qgroup *src)
- {
- int i;
- for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
- qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
- }
- static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
- int mod)
- {
- if (qg->old_refcnt < seq)
- qg->old_refcnt = seq;
- qg->old_refcnt += mod;
- }
- static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
- int mod)
- {
- if (qg->new_refcnt < seq)
- qg->new_refcnt = seq;
- qg->new_refcnt += mod;
- }
- static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
- {
- if (qg->old_refcnt < seq)
- return 0;
- return qg->old_refcnt - seq;
- }
- static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
- {
- if (qg->new_refcnt < seq)
- return 0;
- return qg->new_refcnt - seq;
- }
- /*
- * glue structure to represent the relations between qgroups.
- */
- struct btrfs_qgroup_list {
- struct list_head next_group;
- struct list_head next_member;
- struct btrfs_qgroup *group;
- struct btrfs_qgroup *member;
- };
- static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
- {
- return (u64)(uintptr_t)qg;
- }
- static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
- {
- return (struct btrfs_qgroup *)(uintptr_t)n->aux;
- }
- static int
- qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
- int init_flags);
- static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
- /* must be called with qgroup_ioctl_lock held */
- static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
- u64 qgroupid)
- {
- struct rb_node *n = fs_info->qgroup_tree.rb_node;
- struct btrfs_qgroup *qgroup;
- while (n) {
- qgroup = rb_entry(n, struct btrfs_qgroup, node);
- if (qgroup->qgroupid < qgroupid)
- n = n->rb_left;
- else if (qgroup->qgroupid > qgroupid)
- n = n->rb_right;
- else
- return qgroup;
- }
- return NULL;
- }
- /* must be called with qgroup_lock held */
- static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
- u64 qgroupid)
- {
- struct rb_node **p = &fs_info->qgroup_tree.rb_node;
- struct rb_node *parent = NULL;
- struct btrfs_qgroup *qgroup;
- while (*p) {
- parent = *p;
- qgroup = rb_entry(parent, struct btrfs_qgroup, node);
- if (qgroup->qgroupid < qgroupid)
- p = &(*p)->rb_left;
- else if (qgroup->qgroupid > qgroupid)
- p = &(*p)->rb_right;
- else
- return qgroup;
- }
- qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
- if (!qgroup)
- return ERR_PTR(-ENOMEM);
- qgroup->qgroupid = qgroupid;
- INIT_LIST_HEAD(&qgroup->groups);
- INIT_LIST_HEAD(&qgroup->members);
- INIT_LIST_HEAD(&qgroup->dirty);
- rb_link_node(&qgroup->node, parent, p);
- rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
- return qgroup;
- }
- static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
- {
- struct btrfs_qgroup_list *list;
- list_del(&qgroup->dirty);
- while (!list_empty(&qgroup->groups)) {
- list = list_first_entry(&qgroup->groups,
- struct btrfs_qgroup_list, next_group);
- list_del(&list->next_group);
- list_del(&list->next_member);
- kfree(list);
- }
- while (!list_empty(&qgroup->members)) {
- list = list_first_entry(&qgroup->members,
- struct btrfs_qgroup_list, next_member);
- list_del(&list->next_group);
- list_del(&list->next_member);
- kfree(list);
- }
- kfree(qgroup);
- }
- /* must be called with qgroup_lock held */
- static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
- {
- struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
- if (!qgroup)
- return -ENOENT;
- rb_erase(&qgroup->node, &fs_info->qgroup_tree);
- __del_qgroup_rb(qgroup);
- return 0;
- }
- /* must be called with qgroup_lock held */
- static int add_relation_rb(struct btrfs_fs_info *fs_info,
- u64 memberid, u64 parentid)
- {
- struct btrfs_qgroup *member;
- struct btrfs_qgroup *parent;
- struct btrfs_qgroup_list *list;
- member = find_qgroup_rb(fs_info, memberid);
- parent = find_qgroup_rb(fs_info, parentid);
- if (!member || !parent)
- return -ENOENT;
- list = kzalloc(sizeof(*list), GFP_ATOMIC);
- if (!list)
- return -ENOMEM;
- list->group = parent;
- list->member = member;
- list_add_tail(&list->next_group, &member->groups);
- list_add_tail(&list->next_member, &parent->members);
- return 0;
- }
- /* must be called with qgroup_lock held */
- static int del_relation_rb(struct btrfs_fs_info *fs_info,
- u64 memberid, u64 parentid)
- {
- struct btrfs_qgroup *member;
- struct btrfs_qgroup *parent;
- struct btrfs_qgroup_list *list;
- member = find_qgroup_rb(fs_info, memberid);
- parent = find_qgroup_rb(fs_info, parentid);
- if (!member || !parent)
- return -ENOENT;
- list_for_each_entry(list, &member->groups, next_group) {
- if (list->group == parent) {
- list_del(&list->next_group);
- list_del(&list->next_member);
- kfree(list);
- return 0;
- }
- }
- return -ENOENT;
- }
- #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
- int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
- u64 rfer, u64 excl)
- {
- struct btrfs_qgroup *qgroup;
- qgroup = find_qgroup_rb(fs_info, qgroupid);
- if (!qgroup)
- return -EINVAL;
- if (qgroup->rfer != rfer || qgroup->excl != excl)
- return -EINVAL;
- return 0;
- }
- #endif
- /*
- * The full config is read in one go, only called from open_ctree()
- * It doesn't use any locking, as at this point we're still single-threaded
- */
- int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
- {
- struct btrfs_key key;
- struct btrfs_key found_key;
- struct btrfs_root *quota_root = fs_info->quota_root;
- struct btrfs_path *path = NULL;
- struct extent_buffer *l;
- int slot;
- int ret = 0;
- u64 flags = 0;
- u64 rescan_progress = 0;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- return 0;
- fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
- if (!fs_info->qgroup_ulist) {
- ret = -ENOMEM;
- goto out;
- }
- path = btrfs_alloc_path();
- if (!path) {
- ret = -ENOMEM;
- goto out;
- }
- /* default this to quota off, in case no status key is found */
- fs_info->qgroup_flags = 0;
- /*
- * pass 1: read status, all qgroup infos and limits
- */
- key.objectid = 0;
- key.type = 0;
- key.offset = 0;
- ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
- if (ret)
- goto out;
- while (1) {
- struct btrfs_qgroup *qgroup;
- slot = path->slots[0];
- l = path->nodes[0];
- btrfs_item_key_to_cpu(l, &found_key, slot);
- if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
- struct btrfs_qgroup_status_item *ptr;
- ptr = btrfs_item_ptr(l, slot,
- struct btrfs_qgroup_status_item);
- if (btrfs_qgroup_status_version(l, ptr) !=
- BTRFS_QGROUP_STATUS_VERSION) {
- btrfs_err(fs_info,
- "old qgroup version, quota disabled");
- goto out;
- }
- if (btrfs_qgroup_status_generation(l, ptr) !=
- fs_info->generation) {
- flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- btrfs_err(fs_info,
- "qgroup generation mismatch, marked as inconsistent");
- }
- fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
- ptr);
- rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
- goto next1;
- }
- if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
- found_key.type != BTRFS_QGROUP_LIMIT_KEY)
- goto next1;
- qgroup = find_qgroup_rb(fs_info, found_key.offset);
- if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
- (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
- btrfs_err(fs_info, "inconsistent qgroup config");
- flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- }
- if (!qgroup) {
- qgroup = add_qgroup_rb(fs_info, found_key.offset);
- if (IS_ERR(qgroup)) {
- ret = PTR_ERR(qgroup);
- goto out;
- }
- }
- switch (found_key.type) {
- case BTRFS_QGROUP_INFO_KEY: {
- struct btrfs_qgroup_info_item *ptr;
- ptr = btrfs_item_ptr(l, slot,
- struct btrfs_qgroup_info_item);
- qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
- qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
- qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
- qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
- /* generation currently unused */
- break;
- }
- case BTRFS_QGROUP_LIMIT_KEY: {
- struct btrfs_qgroup_limit_item *ptr;
- ptr = btrfs_item_ptr(l, slot,
- struct btrfs_qgroup_limit_item);
- qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
- qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
- qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
- qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
- qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
- break;
- }
- }
- next1:
- ret = btrfs_next_item(quota_root, path);
- if (ret < 0)
- goto out;
- if (ret)
- break;
- }
- btrfs_release_path(path);
- /*
- * pass 2: read all qgroup relations
- */
- key.objectid = 0;
- key.type = BTRFS_QGROUP_RELATION_KEY;
- key.offset = 0;
- ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
- if (ret)
- goto out;
- while (1) {
- slot = path->slots[0];
- l = path->nodes[0];
- btrfs_item_key_to_cpu(l, &found_key, slot);
- if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
- goto next2;
- if (found_key.objectid > found_key.offset) {
- /* parent <- member, not needed to build config */
- /* FIXME should we omit the key completely? */
- goto next2;
- }
- ret = add_relation_rb(fs_info, found_key.objectid,
- found_key.offset);
- if (ret == -ENOENT) {
- btrfs_warn(fs_info,
- "orphan qgroup relation 0x%llx->0x%llx",
- found_key.objectid, found_key.offset);
- ret = 0; /* ignore the error */
- }
- if (ret)
- goto out;
- next2:
- ret = btrfs_next_item(quota_root, path);
- if (ret < 0)
- goto out;
- if (ret)
- break;
- }
- out:
- fs_info->qgroup_flags |= flags;
- if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
- clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
- else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
- ret >= 0)
- ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
- btrfs_free_path(path);
- if (ret < 0) {
- ulist_free(fs_info->qgroup_ulist);
- fs_info->qgroup_ulist = NULL;
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
- }
- return ret < 0 ? ret : 0;
- }
- /*
- * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
- * first two are in single-threaded paths.And for the third one, we have set
- * quota_root to be null with qgroup_lock held before, so it is safe to clean
- * up the in-memory structures without qgroup_lock held.
- */
- void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
- {
- struct rb_node *n;
- struct btrfs_qgroup *qgroup;
- while ((n = rb_first(&fs_info->qgroup_tree))) {
- qgroup = rb_entry(n, struct btrfs_qgroup, node);
- rb_erase(n, &fs_info->qgroup_tree);
- __del_qgroup_rb(qgroup);
- }
- /*
- * we call btrfs_free_qgroup_config() when umounting
- * filesystem and disabling quota, so we set qgroup_ulist
- * to be null here to avoid double free.
- */
- ulist_free(fs_info->qgroup_ulist);
- fs_info->qgroup_ulist = NULL;
- }
- static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
- u64 dst)
- {
- int ret;
- struct btrfs_root *quota_root = trans->fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- key.objectid = src;
- key.type = BTRFS_QGROUP_RELATION_KEY;
- key.offset = dst;
- ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
- btrfs_mark_buffer_dirty(path->nodes[0]);
- btrfs_free_path(path);
- return ret;
- }
- static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
- u64 dst)
- {
- int ret;
- struct btrfs_root *quota_root = trans->fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- key.objectid = src;
- key.type = BTRFS_QGROUP_RELATION_KEY;
- key.offset = dst;
- ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
- if (ret < 0)
- goto out;
- if (ret > 0) {
- ret = -ENOENT;
- goto out;
- }
- ret = btrfs_del_item(trans, quota_root, path);
- out:
- btrfs_free_path(path);
- return ret;
- }
- static int add_qgroup_item(struct btrfs_trans_handle *trans,
- struct btrfs_root *quota_root, u64 qgroupid)
- {
- int ret;
- struct btrfs_path *path;
- struct btrfs_qgroup_info_item *qgroup_info;
- struct btrfs_qgroup_limit_item *qgroup_limit;
- struct extent_buffer *leaf;
- struct btrfs_key key;
- if (btrfs_is_testing(quota_root->fs_info))
- return 0;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- key.objectid = 0;
- key.type = BTRFS_QGROUP_INFO_KEY;
- key.offset = qgroupid;
- /*
- * Avoid a transaction abort by catching -EEXIST here. In that
- * case, we proceed by re-initializing the existing structure
- * on disk.
- */
- ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
- sizeof(*qgroup_info));
- if (ret && ret != -EEXIST)
- goto out;
- leaf = path->nodes[0];
- qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_qgroup_info_item);
- btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
- btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
- btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
- btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
- btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
- btrfs_mark_buffer_dirty(leaf);
- btrfs_release_path(path);
- key.type = BTRFS_QGROUP_LIMIT_KEY;
- ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
- sizeof(*qgroup_limit));
- if (ret && ret != -EEXIST)
- goto out;
- leaf = path->nodes[0];
- qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_qgroup_limit_item);
- btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
- btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
- btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
- btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
- btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
- btrfs_mark_buffer_dirty(leaf);
- ret = 0;
- out:
- btrfs_free_path(path);
- return ret;
- }
- static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
- {
- int ret;
- struct btrfs_root *quota_root = trans->fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- key.objectid = 0;
- key.type = BTRFS_QGROUP_INFO_KEY;
- key.offset = qgroupid;
- ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
- if (ret < 0)
- goto out;
- if (ret > 0) {
- ret = -ENOENT;
- goto out;
- }
- ret = btrfs_del_item(trans, quota_root, path);
- if (ret)
- goto out;
- btrfs_release_path(path);
- key.type = BTRFS_QGROUP_LIMIT_KEY;
- ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
- if (ret < 0)
- goto out;
- if (ret > 0) {
- ret = -ENOENT;
- goto out;
- }
- ret = btrfs_del_item(trans, quota_root, path);
- out:
- btrfs_free_path(path);
- return ret;
- }
- static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
- struct btrfs_qgroup *qgroup)
- {
- struct btrfs_root *quota_root = trans->fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- struct extent_buffer *l;
- struct btrfs_qgroup_limit_item *qgroup_limit;
- int ret;
- int slot;
- key.objectid = 0;
- key.type = BTRFS_QGROUP_LIMIT_KEY;
- key.offset = qgroup->qgroupid;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
- if (ret > 0)
- ret = -ENOENT;
- if (ret)
- goto out;
- l = path->nodes[0];
- slot = path->slots[0];
- qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
- btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
- btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
- btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
- btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
- btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
- btrfs_mark_buffer_dirty(l);
- out:
- btrfs_free_path(path);
- return ret;
- }
- static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
- struct btrfs_qgroup *qgroup)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root = fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- struct extent_buffer *l;
- struct btrfs_qgroup_info_item *qgroup_info;
- int ret;
- int slot;
- if (btrfs_is_testing(fs_info))
- return 0;
- key.objectid = 0;
- key.type = BTRFS_QGROUP_INFO_KEY;
- key.offset = qgroup->qgroupid;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
- if (ret > 0)
- ret = -ENOENT;
- if (ret)
- goto out;
- l = path->nodes[0];
- slot = path->slots[0];
- qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
- btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
- btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
- btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
- btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
- btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
- btrfs_mark_buffer_dirty(l);
- out:
- btrfs_free_path(path);
- return ret;
- }
- static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root = fs_info->quota_root;
- struct btrfs_path *path;
- struct btrfs_key key;
- struct extent_buffer *l;
- struct btrfs_qgroup_status_item *ptr;
- int ret;
- int slot;
- key.objectid = 0;
- key.type = BTRFS_QGROUP_STATUS_KEY;
- key.offset = 0;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
- if (ret > 0)
- ret = -ENOENT;
- if (ret)
- goto out;
- l = path->nodes[0];
- slot = path->slots[0];
- ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
- btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
- btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
- btrfs_set_qgroup_status_rescan(l, ptr,
- fs_info->qgroup_rescan_progress.objectid);
- btrfs_mark_buffer_dirty(l);
- out:
- btrfs_free_path(path);
- return ret;
- }
- /*
- * called with qgroup_lock held
- */
- static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
- struct btrfs_root *root)
- {
- struct btrfs_path *path;
- struct btrfs_key key;
- struct extent_buffer *leaf = NULL;
- int ret;
- int nr = 0;
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- path->leave_spinning = 1;
- key.objectid = 0;
- key.offset = 0;
- key.type = 0;
- while (1) {
- ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
- if (ret < 0)
- goto out;
- leaf = path->nodes[0];
- nr = btrfs_header_nritems(leaf);
- if (!nr)
- break;
- /*
- * delete the leaf one by one
- * since the whole tree is going
- * to be deleted.
- */
- path->slots[0] = 0;
- ret = btrfs_del_items(trans, root, path, 0, nr);
- if (ret)
- goto out;
- btrfs_release_path(path);
- }
- ret = 0;
- out:
- btrfs_free_path(path);
- return ret;
- }
- int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
- {
- struct btrfs_root *quota_root;
- struct btrfs_root *tree_root = fs_info->tree_root;
- struct btrfs_path *path = NULL;
- struct btrfs_qgroup_status_item *ptr;
- struct extent_buffer *leaf;
- struct btrfs_key key;
- struct btrfs_key found_key;
- struct btrfs_qgroup *qgroup = NULL;
- struct btrfs_trans_handle *trans = NULL;
- int ret = 0;
- int slot;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- if (fs_info->quota_root)
- goto out;
- /*
- * 1 for quota root item
- * 1 for BTRFS_QGROUP_STATUS item
- *
- * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
- * per subvolume. However those are not currently reserved since it
- * would be a lot of overkill.
- */
- trans = btrfs_start_transaction(tree_root, 2);
- if (IS_ERR(trans)) {
- ret = PTR_ERR(trans);
- trans = NULL;
- goto out;
- }
- fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
- if (!fs_info->qgroup_ulist) {
- ret = -ENOMEM;
- btrfs_abort_transaction(trans, ret);
- goto out;
- }
- /*
- * initially create the quota tree
- */
- quota_root = btrfs_create_tree(trans, fs_info,
- BTRFS_QUOTA_TREE_OBJECTID);
- if (IS_ERR(quota_root)) {
- ret = PTR_ERR(quota_root);
- btrfs_abort_transaction(trans, ret);
- goto out;
- }
- path = btrfs_alloc_path();
- if (!path) {
- ret = -ENOMEM;
- btrfs_abort_transaction(trans, ret);
- goto out_free_root;
- }
- key.objectid = 0;
- key.type = BTRFS_QGROUP_STATUS_KEY;
- key.offset = 0;
- ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
- sizeof(*ptr));
- if (ret) {
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- leaf = path->nodes[0];
- ptr = btrfs_item_ptr(leaf, path->slots[0],
- struct btrfs_qgroup_status_item);
- btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
- btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
- fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
- BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
- btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
- btrfs_mark_buffer_dirty(leaf);
- key.objectid = 0;
- key.type = BTRFS_ROOT_REF_KEY;
- key.offset = 0;
- btrfs_release_path(path);
- ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
- if (ret > 0)
- goto out_add_root;
- if (ret < 0) {
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- while (1) {
- slot = path->slots[0];
- leaf = path->nodes[0];
- btrfs_item_key_to_cpu(leaf, &found_key, slot);
- if (found_key.type == BTRFS_ROOT_REF_KEY) {
- ret = add_qgroup_item(trans, quota_root,
- found_key.offset);
- if (ret) {
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- qgroup = add_qgroup_rb(fs_info, found_key.offset);
- if (IS_ERR(qgroup)) {
- ret = PTR_ERR(qgroup);
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- }
- ret = btrfs_next_item(tree_root, path);
- if (ret < 0) {
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- if (ret)
- break;
- }
- out_add_root:
- btrfs_release_path(path);
- ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
- if (ret) {
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
- if (IS_ERR(qgroup)) {
- ret = PTR_ERR(qgroup);
- btrfs_abort_transaction(trans, ret);
- goto out_free_path;
- }
- ret = btrfs_commit_transaction(trans);
- trans = NULL;
- if (ret)
- goto out_free_path;
- /*
- * Set quota enabled flag after committing the transaction, to avoid
- * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
- * creation.
- */
- spin_lock(&fs_info->qgroup_lock);
- fs_info->quota_root = quota_root;
- set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
- spin_unlock(&fs_info->qgroup_lock);
- ret = qgroup_rescan_init(fs_info, 0, 1);
- if (!ret) {
- qgroup_rescan_zero_tracking(fs_info);
- btrfs_queue_work(fs_info->qgroup_rescan_workers,
- &fs_info->qgroup_rescan_work);
- }
- out_free_path:
- btrfs_free_path(path);
- out_free_root:
- if (ret) {
- free_extent_buffer(quota_root->node);
- free_extent_buffer(quota_root->commit_root);
- kfree(quota_root);
- }
- out:
- if (ret) {
- ulist_free(fs_info->qgroup_ulist);
- fs_info->qgroup_ulist = NULL;
- if (trans)
- btrfs_end_transaction(trans);
- }
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
- {
- struct btrfs_root *quota_root;
- struct btrfs_trans_handle *trans = NULL;
- int ret = 0;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- if (!fs_info->quota_root)
- goto out;
- /*
- * 1 For the root item
- *
- * We should also reserve enough items for the quota tree deletion in
- * btrfs_clean_quota_tree but this is not done.
- */
- trans = btrfs_start_transaction(fs_info->tree_root, 1);
- if (IS_ERR(trans)) {
- ret = PTR_ERR(trans);
- goto out;
- }
- clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
- btrfs_qgroup_wait_for_completion(fs_info, false);
- spin_lock(&fs_info->qgroup_lock);
- quota_root = fs_info->quota_root;
- fs_info->quota_root = NULL;
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
- spin_unlock(&fs_info->qgroup_lock);
- btrfs_free_qgroup_config(fs_info);
- ret = btrfs_clean_quota_tree(trans, quota_root);
- if (ret) {
- btrfs_abort_transaction(trans, ret);
- goto end_trans;
- }
- ret = btrfs_del_root(trans, "a_root->root_key);
- if (ret) {
- btrfs_abort_transaction(trans, ret);
- goto end_trans;
- }
- list_del("a_root->dirty_list);
- btrfs_tree_lock(quota_root->node);
- clean_tree_block(fs_info, quota_root->node);
- btrfs_tree_unlock(quota_root->node);
- btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
- free_extent_buffer(quota_root->node);
- free_extent_buffer(quota_root->commit_root);
- kfree(quota_root);
- end_trans:
- ret = btrfs_end_transaction(trans);
- out:
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- static void qgroup_dirty(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup *qgroup)
- {
- if (list_empty(&qgroup->dirty))
- list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
- }
- /*
- * The easy accounting, we're updating qgroup relationship whose child qgroup
- * only has exclusive extents.
- *
- * In this case, all exclsuive extents will also be exlusive for parent, so
- * excl/rfer just get added/removed.
- *
- * So is qgroup reservation space, which should also be added/removed to
- * parent.
- * Or when child tries to release reservation space, parent will underflow its
- * reservation (for relationship adding case).
- *
- * Caller should hold fs_info->qgroup_lock.
- */
- static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
- struct ulist *tmp, u64 ref_root,
- struct btrfs_qgroup *src, int sign)
- {
- struct btrfs_qgroup *qgroup;
- struct btrfs_qgroup_list *glist;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- u64 num_bytes = src->excl;
- int ret = 0;
- qgroup = find_qgroup_rb(fs_info, ref_root);
- if (!qgroup)
- goto out;
- qgroup->rfer += sign * num_bytes;
- qgroup->rfer_cmpr += sign * num_bytes;
- WARN_ON(sign < 0 && qgroup->excl < num_bytes);
- qgroup->excl += sign * num_bytes;
- qgroup->excl_cmpr += sign * num_bytes;
- if (sign > 0)
- qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
- else
- qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
- qgroup_dirty(fs_info, qgroup);
- /* Get all of the parent groups that contain this qgroup */
- list_for_each_entry(glist, &qgroup->groups, next_group) {
- ret = ulist_add(tmp, glist->group->qgroupid,
- qgroup_to_aux(glist->group), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- }
- /* Iterate all of the parents and adjust their reference counts */
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(tmp, &uiter))) {
- qgroup = unode_aux_to_qgroup(unode);
- qgroup->rfer += sign * num_bytes;
- qgroup->rfer_cmpr += sign * num_bytes;
- WARN_ON(sign < 0 && qgroup->excl < num_bytes);
- qgroup->excl += sign * num_bytes;
- if (sign > 0)
- qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
- else
- qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
- qgroup->excl_cmpr += sign * num_bytes;
- qgroup_dirty(fs_info, qgroup);
- /* Add any parents of the parents */
- list_for_each_entry(glist, &qgroup->groups, next_group) {
- ret = ulist_add(tmp, glist->group->qgroupid,
- qgroup_to_aux(glist->group), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- }
- }
- ret = 0;
- out:
- return ret;
- }
- /*
- * Quick path for updating qgroup with only excl refs.
- *
- * In that case, just update all parent will be enough.
- * Or we needs to do a full rescan.
- * Caller should also hold fs_info->qgroup_lock.
- *
- * Return 0 for quick update, return >0 for need to full rescan
- * and mark INCONSISTENT flag.
- * Return < 0 for other error.
- */
- static int quick_update_accounting(struct btrfs_fs_info *fs_info,
- struct ulist *tmp, u64 src, u64 dst,
- int sign)
- {
- struct btrfs_qgroup *qgroup;
- int ret = 1;
- int err = 0;
- qgroup = find_qgroup_rb(fs_info, src);
- if (!qgroup)
- goto out;
- if (qgroup->excl == qgroup->rfer) {
- ret = 0;
- err = __qgroup_excl_accounting(fs_info, tmp, dst,
- qgroup, sign);
- if (err < 0) {
- ret = err;
- goto out;
- }
- }
- out:
- if (ret)
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- return ret;
- }
- int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
- u64 dst)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *parent;
- struct btrfs_qgroup *member;
- struct btrfs_qgroup_list *list;
- struct ulist *tmp;
- int ret = 0;
- /* Check the level of src and dst first */
- if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
- return -EINVAL;
- tmp = ulist_alloc(GFP_KERNEL);
- if (!tmp)
- return -ENOMEM;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- member = find_qgroup_rb(fs_info, src);
- parent = find_qgroup_rb(fs_info, dst);
- if (!member || !parent) {
- ret = -EINVAL;
- goto out;
- }
- /* check if such qgroup relation exist firstly */
- list_for_each_entry(list, &member->groups, next_group) {
- if (list->group == parent) {
- ret = -EEXIST;
- goto out;
- }
- }
- ret = add_qgroup_relation_item(trans, src, dst);
- if (ret)
- goto out;
- ret = add_qgroup_relation_item(trans, dst, src);
- if (ret) {
- del_qgroup_relation_item(trans, src, dst);
- goto out;
- }
- spin_lock(&fs_info->qgroup_lock);
- ret = add_relation_rb(fs_info, src, dst);
- if (ret < 0) {
- spin_unlock(&fs_info->qgroup_lock);
- goto out;
- }
- ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
- spin_unlock(&fs_info->qgroup_lock);
- out:
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- ulist_free(tmp);
- return ret;
- }
- static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
- u64 dst)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *parent;
- struct btrfs_qgroup *member;
- struct btrfs_qgroup_list *list;
- struct ulist *tmp;
- int ret = 0;
- int err;
- tmp = ulist_alloc(GFP_KERNEL);
- if (!tmp)
- return -ENOMEM;
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- member = find_qgroup_rb(fs_info, src);
- parent = find_qgroup_rb(fs_info, dst);
- if (!member || !parent) {
- ret = -EINVAL;
- goto out;
- }
- /* check if such qgroup relation exist firstly */
- list_for_each_entry(list, &member->groups, next_group) {
- if (list->group == parent)
- goto exist;
- }
- ret = -ENOENT;
- goto out;
- exist:
- ret = del_qgroup_relation_item(trans, src, dst);
- err = del_qgroup_relation_item(trans, dst, src);
- if (err && !ret)
- ret = err;
- spin_lock(&fs_info->qgroup_lock);
- del_relation_rb(fs_info, src, dst);
- ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
- spin_unlock(&fs_info->qgroup_lock);
- out:
- ulist_free(tmp);
- return ret;
- }
- int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
- u64 dst)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- int ret = 0;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- ret = __del_qgroup_relation(trans, src, dst);
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *qgroup;
- int ret = 0;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- qgroup = find_qgroup_rb(fs_info, qgroupid);
- if (qgroup) {
- ret = -EEXIST;
- goto out;
- }
- ret = add_qgroup_item(trans, quota_root, qgroupid);
- if (ret)
- goto out;
- spin_lock(&fs_info->qgroup_lock);
- qgroup = add_qgroup_rb(fs_info, qgroupid);
- spin_unlock(&fs_info->qgroup_lock);
- if (IS_ERR(qgroup))
- ret = PTR_ERR(qgroup);
- out:
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *qgroup;
- struct btrfs_qgroup_list *list;
- int ret = 0;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- qgroup = find_qgroup_rb(fs_info, qgroupid);
- if (!qgroup) {
- ret = -ENOENT;
- goto out;
- } else {
- /* check if there are no children of this qgroup */
- if (!list_empty(&qgroup->members)) {
- ret = -EBUSY;
- goto out;
- }
- }
- ret = del_qgroup_item(trans, qgroupid);
- if (ret && ret != -ENOENT)
- goto out;
- while (!list_empty(&qgroup->groups)) {
- list = list_first_entry(&qgroup->groups,
- struct btrfs_qgroup_list, next_group);
- ret = __del_qgroup_relation(trans, qgroupid,
- list->group->qgroupid);
- if (ret)
- goto out;
- }
- spin_lock(&fs_info->qgroup_lock);
- del_qgroup_rb(fs_info, qgroupid);
- spin_unlock(&fs_info->qgroup_lock);
- out:
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
- struct btrfs_qgroup_limit *limit)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *qgroup;
- int ret = 0;
- /* Sometimes we would want to clear the limit on this qgroup.
- * To meet this requirement, we treat the -1 as a special value
- * which tell kernel to clear the limit on this qgroup.
- */
- const u64 CLEAR_VALUE = -1;
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- qgroup = find_qgroup_rb(fs_info, qgroupid);
- if (!qgroup) {
- ret = -ENOENT;
- goto out;
- }
- spin_lock(&fs_info->qgroup_lock);
- if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
- if (limit->max_rfer == CLEAR_VALUE) {
- qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
- limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
- qgroup->max_rfer = 0;
- } else {
- qgroup->max_rfer = limit->max_rfer;
- }
- }
- if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
- if (limit->max_excl == CLEAR_VALUE) {
- qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
- limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
- qgroup->max_excl = 0;
- } else {
- qgroup->max_excl = limit->max_excl;
- }
- }
- if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
- if (limit->rsv_rfer == CLEAR_VALUE) {
- qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
- limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
- qgroup->rsv_rfer = 0;
- } else {
- qgroup->rsv_rfer = limit->rsv_rfer;
- }
- }
- if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
- if (limit->rsv_excl == CLEAR_VALUE) {
- qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
- limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
- qgroup->rsv_excl = 0;
- } else {
- qgroup->rsv_excl = limit->rsv_excl;
- }
- }
- qgroup->lim_flags |= limit->flags;
- spin_unlock(&fs_info->qgroup_lock);
- ret = update_qgroup_limit_item(trans, qgroup);
- if (ret) {
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- btrfs_info(fs_info, "unable to update quota limit for %llu",
- qgroupid);
- }
- out:
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
- struct btrfs_delayed_ref_root *delayed_refs,
- struct btrfs_qgroup_extent_record *record)
- {
- struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
- struct rb_node *parent_node = NULL;
- struct btrfs_qgroup_extent_record *entry;
- u64 bytenr = record->bytenr;
- lockdep_assert_held(&delayed_refs->lock);
- trace_btrfs_qgroup_trace_extent(fs_info, record);
- while (*p) {
- parent_node = *p;
- entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
- node);
- if (bytenr < entry->bytenr)
- p = &(*p)->rb_left;
- else if (bytenr > entry->bytenr)
- p = &(*p)->rb_right;
- else
- return 1;
- }
- rb_link_node(&record->node, parent_node, p);
- rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
- return 0;
- }
- int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
- struct btrfs_qgroup_extent_record *qrecord)
- {
- struct ulist *old_root;
- u64 bytenr = qrecord->bytenr;
- int ret;
- ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
- if (ret < 0) {
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- btrfs_warn(fs_info,
- "error accounting new delayed refs extent (err code: %d), quota inconsistent",
- ret);
- return 0;
- }
- /*
- * Here we don't need to get the lock of
- * trans->transaction->delayed_refs, since inserted qrecord won't
- * be deleted, only qrecord->node may be modified (new qrecord insert)
- *
- * So modifying qrecord->old_roots is safe here
- */
- qrecord->old_roots = old_root;
- return 0;
- }
- int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
- u64 num_bytes, gfp_t gfp_flag)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_qgroup_extent_record *record;
- struct btrfs_delayed_ref_root *delayed_refs;
- int ret;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
- || bytenr == 0 || num_bytes == 0)
- return 0;
- record = kmalloc(sizeof(*record), gfp_flag);
- if (!record)
- return -ENOMEM;
- delayed_refs = &trans->transaction->delayed_refs;
- record->bytenr = bytenr;
- record->num_bytes = num_bytes;
- record->old_roots = NULL;
- spin_lock(&delayed_refs->lock);
- ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
- spin_unlock(&delayed_refs->lock);
- if (ret > 0) {
- kfree(record);
- return 0;
- }
- return btrfs_qgroup_trace_extent_post(fs_info, record);
- }
- int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
- struct extent_buffer *eb)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- int nr = btrfs_header_nritems(eb);
- int i, extent_type, ret;
- struct btrfs_key key;
- struct btrfs_file_extent_item *fi;
- u64 bytenr, num_bytes;
- /* We can be called directly from walk_up_proc() */
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- return 0;
- for (i = 0; i < nr; i++) {
- btrfs_item_key_to_cpu(eb, &key, i);
- if (key.type != BTRFS_EXTENT_DATA_KEY)
- continue;
- fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
- /* filter out non qgroup-accountable extents */
- extent_type = btrfs_file_extent_type(eb, fi);
- if (extent_type == BTRFS_FILE_EXTENT_INLINE)
- continue;
- bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
- if (!bytenr)
- continue;
- num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
- ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
- GFP_NOFS);
- if (ret)
- return ret;
- }
- cond_resched();
- return 0;
- }
- /*
- * Walk up the tree from the bottom, freeing leaves and any interior
- * nodes which have had all slots visited. If a node (leaf or
- * interior) is freed, the node above it will have it's slot
- * incremented. The root node will never be freed.
- *
- * At the end of this function, we should have a path which has all
- * slots incremented to the next position for a search. If we need to
- * read a new node it will be NULL and the node above it will have the
- * correct slot selected for a later read.
- *
- * If we increment the root nodes slot counter past the number of
- * elements, 1 is returned to signal completion of the search.
- */
- static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
- {
- int level = 0;
- int nr, slot;
- struct extent_buffer *eb;
- if (root_level == 0)
- return 1;
- while (level <= root_level) {
- eb = path->nodes[level];
- nr = btrfs_header_nritems(eb);
- path->slots[level]++;
- slot = path->slots[level];
- if (slot >= nr || level == 0) {
- /*
- * Don't free the root - we will detect this
- * condition after our loop and return a
- * positive value for caller to stop walking the tree.
- */
- if (level != root_level) {
- btrfs_tree_unlock_rw(eb, path->locks[level]);
- path->locks[level] = 0;
- free_extent_buffer(eb);
- path->nodes[level] = NULL;
- path->slots[level] = 0;
- }
- } else {
- /*
- * We have a valid slot to walk back down
- * from. Stop here so caller can process these
- * new nodes.
- */
- break;
- }
- level++;
- }
- eb = path->nodes[root_level];
- if (path->slots[root_level] >= btrfs_header_nritems(eb))
- return 1;
- return 0;
- }
- int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
- struct extent_buffer *root_eb,
- u64 root_gen, int root_level)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- int ret = 0;
- int level;
- struct extent_buffer *eb = root_eb;
- struct btrfs_path *path = NULL;
- BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
- BUG_ON(root_eb == NULL);
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- return 0;
- if (!extent_buffer_uptodate(root_eb)) {
- ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
- if (ret)
- goto out;
- }
- if (root_level == 0) {
- ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
- goto out;
- }
- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
- /*
- * Walk down the tree. Missing extent blocks are filled in as
- * we go. Metadata is accounted every time we read a new
- * extent block.
- *
- * When we reach a leaf, we account for file extent items in it,
- * walk back up the tree (adjusting slot pointers as we go)
- * and restart the search process.
- */
- extent_buffer_get(root_eb); /* For path */
- path->nodes[root_level] = root_eb;
- path->slots[root_level] = 0;
- path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
- walk_down:
- level = root_level;
- while (level >= 0) {
- if (path->nodes[level] == NULL) {
- struct btrfs_key first_key;
- int parent_slot;
- u64 child_gen;
- u64 child_bytenr;
- /*
- * We need to get child blockptr/gen from parent before
- * we can read it.
- */
- eb = path->nodes[level + 1];
- parent_slot = path->slots[level + 1];
- child_bytenr = btrfs_node_blockptr(eb, parent_slot);
- child_gen = btrfs_node_ptr_generation(eb, parent_slot);
- btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
- eb = read_tree_block(fs_info, child_bytenr, child_gen,
- level, &first_key);
- if (IS_ERR(eb)) {
- ret = PTR_ERR(eb);
- goto out;
- } else if (!extent_buffer_uptodate(eb)) {
- free_extent_buffer(eb);
- ret = -EIO;
- goto out;
- }
- path->nodes[level] = eb;
- path->slots[level] = 0;
- btrfs_tree_read_lock(eb);
- btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
- path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
- ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
- fs_info->nodesize,
- GFP_NOFS);
- if (ret)
- goto out;
- }
- if (level == 0) {
- ret = btrfs_qgroup_trace_leaf_items(trans,
- path->nodes[level]);
- if (ret)
- goto out;
- /* Nonzero return here means we completed our search */
- ret = adjust_slots_upwards(path, root_level);
- if (ret)
- break;
- /* Restart search with new slots */
- goto walk_down;
- }
- level--;
- }
- ret = 0;
- out:
- btrfs_free_path(path);
- return ret;
- }
- #define UPDATE_NEW 0
- #define UPDATE_OLD 1
- /*
- * Walk all of the roots that points to the bytenr and adjust their refcnts.
- */
- static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
- struct ulist *roots, struct ulist *tmp,
- struct ulist *qgroups, u64 seq, int update_old)
- {
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- struct ulist_node *tmp_unode;
- struct ulist_iterator tmp_uiter;
- struct btrfs_qgroup *qg;
- int ret = 0;
- if (!roots)
- return 0;
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(roots, &uiter))) {
- qg = find_qgroup_rb(fs_info, unode->val);
- if (!qg)
- continue;
- ulist_reinit(tmp);
- ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
- GFP_ATOMIC);
- if (ret < 0)
- return ret;
- ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
- if (ret < 0)
- return ret;
- ULIST_ITER_INIT(&tmp_uiter);
- while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
- struct btrfs_qgroup_list *glist;
- qg = unode_aux_to_qgroup(tmp_unode);
- if (update_old)
- btrfs_qgroup_update_old_refcnt(qg, seq, 1);
- else
- btrfs_qgroup_update_new_refcnt(qg, seq, 1);
- list_for_each_entry(glist, &qg->groups, next_group) {
- ret = ulist_add(qgroups, glist->group->qgroupid,
- qgroup_to_aux(glist->group),
- GFP_ATOMIC);
- if (ret < 0)
- return ret;
- ret = ulist_add(tmp, glist->group->qgroupid,
- qgroup_to_aux(glist->group),
- GFP_ATOMIC);
- if (ret < 0)
- return ret;
- }
- }
- }
- return 0;
- }
- /*
- * Update qgroup rfer/excl counters.
- * Rfer update is easy, codes can explain themselves.
- *
- * Excl update is tricky, the update is split into 2 part.
- * Part 1: Possible exclusive <-> sharing detect:
- * | A | !A |
- * -------------------------------------
- * B | * | - |
- * -------------------------------------
- * !B | + | ** |
- * -------------------------------------
- *
- * Conditions:
- * A: cur_old_roots < nr_old_roots (not exclusive before)
- * !A: cur_old_roots == nr_old_roots (possible exclusive before)
- * B: cur_new_roots < nr_new_roots (not exclusive now)
- * !B: cur_new_roots == nr_new_roots (possible exclusive now)
- *
- * Results:
- * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
- * *: Definitely not changed. **: Possible unchanged.
- *
- * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
- *
- * To make the logic clear, we first use condition A and B to split
- * combination into 4 results.
- *
- * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
- * only on variant maybe 0.
- *
- * Lastly, check result **, since there are 2 variants maybe 0, split them
- * again(2x2).
- * But this time we don't need to consider other things, the codes and logic
- * is easy to understand now.
- */
- static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
- struct ulist *qgroups,
- u64 nr_old_roots,
- u64 nr_new_roots,
- u64 num_bytes, u64 seq)
- {
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- struct btrfs_qgroup *qg;
- u64 cur_new_count, cur_old_count;
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(qgroups, &uiter))) {
- bool dirty = false;
- qg = unode_aux_to_qgroup(unode);
- cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
- cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
- trace_qgroup_update_counters(fs_info, qg, cur_old_count,
- cur_new_count);
- /* Rfer update part */
- if (cur_old_count == 0 && cur_new_count > 0) {
- qg->rfer += num_bytes;
- qg->rfer_cmpr += num_bytes;
- dirty = true;
- }
- if (cur_old_count > 0 && cur_new_count == 0) {
- qg->rfer -= num_bytes;
- qg->rfer_cmpr -= num_bytes;
- dirty = true;
- }
- /* Excl update part */
- /* Exclusive/none -> shared case */
- if (cur_old_count == nr_old_roots &&
- cur_new_count < nr_new_roots) {
- /* Exclusive -> shared */
- if (cur_old_count != 0) {
- qg->excl -= num_bytes;
- qg->excl_cmpr -= num_bytes;
- dirty = true;
- }
- }
- /* Shared -> exclusive/none case */
- if (cur_old_count < nr_old_roots &&
- cur_new_count == nr_new_roots) {
- /* Shared->exclusive */
- if (cur_new_count != 0) {
- qg->excl += num_bytes;
- qg->excl_cmpr += num_bytes;
- dirty = true;
- }
- }
- /* Exclusive/none -> exclusive/none case */
- if (cur_old_count == nr_old_roots &&
- cur_new_count == nr_new_roots) {
- if (cur_old_count == 0) {
- /* None -> exclusive/none */
- if (cur_new_count != 0) {
- /* None -> exclusive */
- qg->excl += num_bytes;
- qg->excl_cmpr += num_bytes;
- dirty = true;
- }
- /* None -> none, nothing changed */
- } else {
- /* Exclusive -> exclusive/none */
- if (cur_new_count == 0) {
- /* Exclusive -> none */
- qg->excl -= num_bytes;
- qg->excl_cmpr -= num_bytes;
- dirty = true;
- }
- /* Exclusive -> exclusive, nothing changed */
- }
- }
- if (dirty)
- qgroup_dirty(fs_info, qg);
- }
- return 0;
- }
- /*
- * Check if the @roots potentially is a list of fs tree roots
- *
- * Return 0 for definitely not a fs/subvol tree roots ulist
- * Return 1 for possible fs/subvol tree roots in the list (considering an empty
- * one as well)
- */
- static int maybe_fs_roots(struct ulist *roots)
- {
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- /* Empty one, still possible for fs roots */
- if (!roots || roots->nnodes == 0)
- return 1;
- ULIST_ITER_INIT(&uiter);
- unode = ulist_next(roots, &uiter);
- if (!unode)
- return 1;
- /*
- * If it contains fs tree roots, then it must belong to fs/subvol
- * trees.
- * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
- */
- return is_fstree(unode->val);
- }
- int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
- u64 num_bytes, struct ulist *old_roots,
- struct ulist *new_roots)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct ulist *qgroups = NULL;
- struct ulist *tmp = NULL;
- u64 seq;
- u64 nr_new_roots = 0;
- u64 nr_old_roots = 0;
- int ret = 0;
- /*
- * If quotas get disabled meanwhile, the resouces need to be freed and
- * we can't just exit here.
- */
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- goto out_free;
- if (new_roots) {
- if (!maybe_fs_roots(new_roots))
- goto out_free;
- nr_new_roots = new_roots->nnodes;
- }
- if (old_roots) {
- if (!maybe_fs_roots(old_roots))
- goto out_free;
- nr_old_roots = old_roots->nnodes;
- }
- /* Quick exit, either not fs tree roots, or won't affect any qgroup */
- if (nr_old_roots == 0 && nr_new_roots == 0)
- goto out_free;
- BUG_ON(!fs_info->quota_root);
- trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
- num_bytes, nr_old_roots, nr_new_roots);
- qgroups = ulist_alloc(GFP_NOFS);
- if (!qgroups) {
- ret = -ENOMEM;
- goto out_free;
- }
- tmp = ulist_alloc(GFP_NOFS);
- if (!tmp) {
- ret = -ENOMEM;
- goto out_free;
- }
- mutex_lock(&fs_info->qgroup_rescan_lock);
- if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
- if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- ret = 0;
- goto out_free;
- }
- }
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- spin_lock(&fs_info->qgroup_lock);
- seq = fs_info->qgroup_seq;
- /* Update old refcnts using old_roots */
- ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
- UPDATE_OLD);
- if (ret < 0)
- goto out;
- /* Update new refcnts using new_roots */
- ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
- UPDATE_NEW);
- if (ret < 0)
- goto out;
- qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
- num_bytes, seq);
- /*
- * Bump qgroup_seq to avoid seq overlap
- */
- fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
- out:
- spin_unlock(&fs_info->qgroup_lock);
- out_free:
- ulist_free(tmp);
- ulist_free(qgroups);
- ulist_free(old_roots);
- ulist_free(new_roots);
- return ret;
- }
- int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_qgroup_extent_record *record;
- struct btrfs_delayed_ref_root *delayed_refs;
- struct ulist *new_roots = NULL;
- struct rb_node *node;
- u64 qgroup_to_skip;
- int ret = 0;
- delayed_refs = &trans->transaction->delayed_refs;
- qgroup_to_skip = delayed_refs->qgroup_to_skip;
- while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
- record = rb_entry(node, struct btrfs_qgroup_extent_record,
- node);
- trace_btrfs_qgroup_account_extents(fs_info, record);
- if (!ret) {
- /*
- * Old roots should be searched when inserting qgroup
- * extent record
- */
- if (WARN_ON(!record->old_roots)) {
- /* Search commit root to find old_roots */
- ret = btrfs_find_all_roots(NULL, fs_info,
- record->bytenr, 0,
- &record->old_roots, false);
- if (ret < 0)
- goto cleanup;
- }
- /*
- * Use SEQ_LAST as time_seq to do special search, which
- * doesn't lock tree or delayed_refs and search current
- * root. It's safe inside commit_transaction().
- */
- ret = btrfs_find_all_roots(trans, fs_info,
- record->bytenr, SEQ_LAST, &new_roots, false);
- if (ret < 0)
- goto cleanup;
- if (qgroup_to_skip) {
- ulist_del(new_roots, qgroup_to_skip, 0);
- ulist_del(record->old_roots, qgroup_to_skip,
- 0);
- }
- ret = btrfs_qgroup_account_extent(trans, record->bytenr,
- record->num_bytes,
- record->old_roots,
- new_roots);
- record->old_roots = NULL;
- new_roots = NULL;
- }
- cleanup:
- ulist_free(record->old_roots);
- ulist_free(new_roots);
- new_roots = NULL;
- rb_erase(node, &delayed_refs->dirty_extent_root);
- kfree(record);
- }
- return ret;
- }
- /*
- * called from commit_transaction. Writes all changed qgroups to disk.
- */
- int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root = fs_info->quota_root;
- int ret = 0;
- if (!quota_root)
- return ret;
- spin_lock(&fs_info->qgroup_lock);
- while (!list_empty(&fs_info->dirty_qgroups)) {
- struct btrfs_qgroup *qgroup;
- qgroup = list_first_entry(&fs_info->dirty_qgroups,
- struct btrfs_qgroup, dirty);
- list_del_init(&qgroup->dirty);
- spin_unlock(&fs_info->qgroup_lock);
- ret = update_qgroup_info_item(trans, qgroup);
- if (ret)
- fs_info->qgroup_flags |=
- BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- ret = update_qgroup_limit_item(trans, qgroup);
- if (ret)
- fs_info->qgroup_flags |=
- BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- spin_lock(&fs_info->qgroup_lock);
- }
- if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
- else
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
- spin_unlock(&fs_info->qgroup_lock);
- ret = update_qgroup_status_item(trans);
- if (ret)
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- return ret;
- }
- /*
- * Copy the accounting information between qgroups. This is necessary
- * when a snapshot or a subvolume is created. Throwing an error will
- * cause a transaction abort so we take extra care here to only error
- * when a readonly fs is a reasonable outcome.
- */
- int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
- u64 objectid, struct btrfs_qgroup_inherit *inherit)
- {
- int ret = 0;
- int i;
- u64 *i_qgroups;
- bool committing = false;
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *srcgroup;
- struct btrfs_qgroup *dstgroup;
- u32 level_size = 0;
- u64 nums;
- /*
- * There are only two callers of this function.
- *
- * One in create_subvol() in the ioctl context, which needs to hold
- * the qgroup_ioctl_lock.
- *
- * The other one in create_pending_snapshot() where no other qgroup
- * code can modify the fs as they all need to either start a new trans
- * or hold a trans handler, thus we don't need to hold
- * qgroup_ioctl_lock.
- * This would avoid long and complex lock chain and make lockdep happy.
- */
- spin_lock(&fs_info->trans_lock);
- if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
- committing = true;
- spin_unlock(&fs_info->trans_lock);
- if (!committing)
- mutex_lock(&fs_info->qgroup_ioctl_lock);
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
- goto out;
- quota_root = fs_info->quota_root;
- if (!quota_root) {
- ret = -EINVAL;
- goto out;
- }
- if (inherit) {
- i_qgroups = (u64 *)(inherit + 1);
- nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
- 2 * inherit->num_excl_copies;
- for (i = 0; i < nums; ++i) {
- srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
- /*
- * Zero out invalid groups so we can ignore
- * them later.
- */
- if (!srcgroup ||
- ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
- *i_qgroups = 0ULL;
- ++i_qgroups;
- }
- }
- /*
- * create a tracking group for the subvol itself
- */
- ret = add_qgroup_item(trans, quota_root, objectid);
- if (ret)
- goto out;
- /*
- * add qgroup to all inherited groups
- */
- if (inherit) {
- i_qgroups = (u64 *)(inherit + 1);
- for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
- if (*i_qgroups == 0)
- continue;
- ret = add_qgroup_relation_item(trans, objectid,
- *i_qgroups);
- if (ret && ret != -EEXIST)
- goto out;
- ret = add_qgroup_relation_item(trans, *i_qgroups,
- objectid);
- if (ret && ret != -EEXIST)
- goto out;
- }
- ret = 0;
- }
- spin_lock(&fs_info->qgroup_lock);
- dstgroup = add_qgroup_rb(fs_info, objectid);
- if (IS_ERR(dstgroup)) {
- ret = PTR_ERR(dstgroup);
- goto unlock;
- }
- if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
- dstgroup->lim_flags = inherit->lim.flags;
- dstgroup->max_rfer = inherit->lim.max_rfer;
- dstgroup->max_excl = inherit->lim.max_excl;
- dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
- dstgroup->rsv_excl = inherit->lim.rsv_excl;
- ret = update_qgroup_limit_item(trans, dstgroup);
- if (ret) {
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- btrfs_info(fs_info,
- "unable to update quota limit for %llu",
- dstgroup->qgroupid);
- goto unlock;
- }
- }
- if (srcid) {
- srcgroup = find_qgroup_rb(fs_info, srcid);
- if (!srcgroup)
- goto unlock;
- /*
- * We call inherit after we clone the root in order to make sure
- * our counts don't go crazy, so at this point the only
- * difference between the two roots should be the root node.
- */
- level_size = fs_info->nodesize;
- dstgroup->rfer = srcgroup->rfer;
- dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
- dstgroup->excl = level_size;
- dstgroup->excl_cmpr = level_size;
- srcgroup->excl = level_size;
- srcgroup->excl_cmpr = level_size;
- /* inherit the limit info */
- dstgroup->lim_flags = srcgroup->lim_flags;
- dstgroup->max_rfer = srcgroup->max_rfer;
- dstgroup->max_excl = srcgroup->max_excl;
- dstgroup->rsv_rfer = srcgroup->rsv_rfer;
- dstgroup->rsv_excl = srcgroup->rsv_excl;
- qgroup_dirty(fs_info, dstgroup);
- qgroup_dirty(fs_info, srcgroup);
- }
- if (!inherit)
- goto unlock;
- i_qgroups = (u64 *)(inherit + 1);
- for (i = 0; i < inherit->num_qgroups; ++i) {
- if (*i_qgroups) {
- ret = add_relation_rb(fs_info, objectid, *i_qgroups);
- if (ret)
- goto unlock;
- }
- ++i_qgroups;
- }
- for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
- struct btrfs_qgroup *src;
- struct btrfs_qgroup *dst;
- if (!i_qgroups[0] || !i_qgroups[1])
- continue;
- src = find_qgroup_rb(fs_info, i_qgroups[0]);
- dst = find_qgroup_rb(fs_info, i_qgroups[1]);
- if (!src || !dst) {
- ret = -EINVAL;
- goto unlock;
- }
- dst->rfer = src->rfer - level_size;
- dst->rfer_cmpr = src->rfer_cmpr - level_size;
- }
- for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
- struct btrfs_qgroup *src;
- struct btrfs_qgroup *dst;
- if (!i_qgroups[0] || !i_qgroups[1])
- continue;
- src = find_qgroup_rb(fs_info, i_qgroups[0]);
- dst = find_qgroup_rb(fs_info, i_qgroups[1]);
- if (!src || !dst) {
- ret = -EINVAL;
- goto unlock;
- }
- dst->excl = src->excl + level_size;
- dst->excl_cmpr = src->excl_cmpr + level_size;
- }
- unlock:
- spin_unlock(&fs_info->qgroup_lock);
- out:
- if (!committing)
- mutex_unlock(&fs_info->qgroup_ioctl_lock);
- return ret;
- }
- /*
- * Two limits to commit transaction in advance.
- *
- * For RATIO, it will be 1/RATIO of the remaining limit as threshold.
- * For SIZE, it will be in byte unit as threshold.
- */
- #define QGROUP_FREE_RATIO 32
- #define QGROUP_FREE_SIZE SZ_32M
- static bool qgroup_check_limits(struct btrfs_fs_info *fs_info,
- const struct btrfs_qgroup *qg, u64 num_bytes)
- {
- u64 free;
- u64 threshold;
- if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
- qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
- return false;
- if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
- qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
- return false;
- /*
- * Even if we passed the check, it's better to check if reservation
- * for meta_pertrans is pushing us near limit.
- * If there is too much pertrans reservation or it's near the limit,
- * let's try commit transaction to free some, using transaction_kthread
- */
- if ((qg->lim_flags & (BTRFS_QGROUP_LIMIT_MAX_RFER |
- BTRFS_QGROUP_LIMIT_MAX_EXCL))) {
- if (qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
- free = qg->max_excl - qgroup_rsv_total(qg) - qg->excl;
- threshold = min_t(u64, qg->max_excl / QGROUP_FREE_RATIO,
- QGROUP_FREE_SIZE);
- } else {
- free = qg->max_rfer - qgroup_rsv_total(qg) - qg->rfer;
- threshold = min_t(u64, qg->max_rfer / QGROUP_FREE_RATIO,
- QGROUP_FREE_SIZE);
- }
- /*
- * Use transaction_kthread to commit transaction, so we no
- * longer need to bother nested transaction nor lock context.
- */
- if (free < threshold)
- btrfs_commit_transaction_locksafe(fs_info);
- }
- return true;
- }
- static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
- enum btrfs_qgroup_rsv_type type)
- {
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *qgroup;
- struct btrfs_fs_info *fs_info = root->fs_info;
- u64 ref_root = root->root_key.objectid;
- int ret = 0;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- if (!is_fstree(ref_root))
- return 0;
- if (num_bytes == 0)
- return 0;
- if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
- capable(CAP_SYS_RESOURCE))
- enforce = false;
- spin_lock(&fs_info->qgroup_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root)
- goto out;
- qgroup = find_qgroup_rb(fs_info, ref_root);
- if (!qgroup)
- goto out;
- /*
- * in a first step, we check all affected qgroups if any limits would
- * be exceeded
- */
- ulist_reinit(fs_info->qgroup_ulist);
- ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
- qgroup_to_aux(qgroup), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
- struct btrfs_qgroup *qg;
- struct btrfs_qgroup_list *glist;
- qg = unode_aux_to_qgroup(unode);
- if (enforce && !qgroup_check_limits(fs_info, qg, num_bytes)) {
- ret = -EDQUOT;
- goto out;
- }
- list_for_each_entry(glist, &qg->groups, next_group) {
- ret = ulist_add(fs_info->qgroup_ulist,
- glist->group->qgroupid,
- qgroup_to_aux(glist->group), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- }
- }
- ret = 0;
- /*
- * no limits exceeded, now record the reservation into all qgroups
- */
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
- struct btrfs_qgroup *qg;
- qg = unode_aux_to_qgroup(unode);
- trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
- qgroup_rsv_add(fs_info, qg, num_bytes, type);
- }
- out:
- spin_unlock(&fs_info->qgroup_lock);
- return ret;
- }
- /*
- * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
- * qgroup).
- *
- * Will handle all higher level qgroup too.
- *
- * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
- * This special case is only used for META_PERTRANS type.
- */
- void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
- u64 ref_root, u64 num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- struct btrfs_root *quota_root;
- struct btrfs_qgroup *qgroup;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- int ret = 0;
- if (!is_fstree(ref_root))
- return;
- if (num_bytes == 0)
- return;
- if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
- WARN(1, "%s: Invalid type to free", __func__);
- return;
- }
- spin_lock(&fs_info->qgroup_lock);
- quota_root = fs_info->quota_root;
- if (!quota_root)
- goto out;
- qgroup = find_qgroup_rb(fs_info, ref_root);
- if (!qgroup)
- goto out;
- if (num_bytes == (u64)-1)
- /*
- * We're freeing all pertrans rsv, get reserved value from
- * level 0 qgroup as real num_bytes to free.
- */
- num_bytes = qgroup->rsv.values[type];
- ulist_reinit(fs_info->qgroup_ulist);
- ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
- qgroup_to_aux(qgroup), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
- struct btrfs_qgroup *qg;
- struct btrfs_qgroup_list *glist;
- qg = unode_aux_to_qgroup(unode);
- trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
- qgroup_rsv_release(fs_info, qg, num_bytes, type);
- list_for_each_entry(glist, &qg->groups, next_group) {
- ret = ulist_add(fs_info->qgroup_ulist,
- glist->group->qgroupid,
- qgroup_to_aux(glist->group), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- }
- }
- out:
- spin_unlock(&fs_info->qgroup_lock);
- }
- /*
- * Check if the leaf is the last leaf. Which means all node pointers
- * are at their last position.
- */
- static bool is_last_leaf(struct btrfs_path *path)
- {
- int i;
- for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
- if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
- return false;
- }
- return true;
- }
- /*
- * returns < 0 on error, 0 when more leafs are to be scanned.
- * returns 1 when done.
- */
- static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
- struct btrfs_path *path)
- {
- struct btrfs_fs_info *fs_info = trans->fs_info;
- struct btrfs_key found;
- struct extent_buffer *scratch_leaf = NULL;
- struct ulist *roots = NULL;
- u64 num_bytes;
- bool done;
- int slot;
- int ret;
- mutex_lock(&fs_info->qgroup_rescan_lock);
- ret = btrfs_search_slot_for_read(fs_info->extent_root,
- &fs_info->qgroup_rescan_progress,
- path, 1, 0);
- btrfs_debug(fs_info,
- "current progress key (%llu %u %llu), search_slot ret %d",
- fs_info->qgroup_rescan_progress.objectid,
- fs_info->qgroup_rescan_progress.type,
- fs_info->qgroup_rescan_progress.offset, ret);
- if (ret) {
- /*
- * The rescan is about to end, we will not be scanning any
- * further blocks. We cannot unset the RESCAN flag here, because
- * we want to commit the transaction if everything went well.
- * To make the live accounting work in this phase, we set our
- * scan progress pointer such that every real extent objectid
- * will be smaller.
- */
- fs_info->qgroup_rescan_progress.objectid = (u64)-1;
- btrfs_release_path(path);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- return ret;
- }
- done = is_last_leaf(path);
- btrfs_item_key_to_cpu(path->nodes[0], &found,
- btrfs_header_nritems(path->nodes[0]) - 1);
- fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
- scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
- if (!scratch_leaf) {
- ret = -ENOMEM;
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- goto out;
- }
- extent_buffer_get(scratch_leaf);
- btrfs_tree_read_lock(scratch_leaf);
- btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
- slot = path->slots[0];
- btrfs_release_path(path);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
- btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
- if (found.type != BTRFS_EXTENT_ITEM_KEY &&
- found.type != BTRFS_METADATA_ITEM_KEY)
- continue;
- if (found.type == BTRFS_METADATA_ITEM_KEY)
- num_bytes = fs_info->nodesize;
- else
- num_bytes = found.offset;
- ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
- &roots, false);
- if (ret < 0)
- goto out;
- /* For rescan, just pass old_roots as NULL */
- ret = btrfs_qgroup_account_extent(trans, found.objectid,
- num_bytes, NULL, roots);
- if (ret < 0)
- goto out;
- }
- out:
- if (scratch_leaf) {
- btrfs_tree_read_unlock_blocking(scratch_leaf);
- free_extent_buffer(scratch_leaf);
- }
- if (done && !ret) {
- ret = 1;
- fs_info->qgroup_rescan_progress.objectid = (u64)-1;
- }
- return ret;
- }
- static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
- {
- struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
- qgroup_rescan_work);
- struct btrfs_path *path;
- struct btrfs_trans_handle *trans = NULL;
- int err = -ENOMEM;
- int ret = 0;
- path = btrfs_alloc_path();
- if (!path)
- goto out;
- /*
- * Rescan should only search for commit root, and any later difference
- * should be recorded by qgroup
- */
- path->search_commit_root = 1;
- path->skip_locking = 1;
- err = 0;
- while (!err && !btrfs_fs_closing(fs_info)) {
- trans = btrfs_start_transaction(fs_info->fs_root, 0);
- if (IS_ERR(trans)) {
- err = PTR_ERR(trans);
- break;
- }
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
- err = -EINTR;
- } else {
- err = qgroup_rescan_leaf(trans, path);
- }
- if (err > 0)
- btrfs_commit_transaction(trans);
- else
- btrfs_end_transaction(trans);
- }
- out:
- btrfs_free_path(path);
- mutex_lock(&fs_info->qgroup_rescan_lock);
- if (err > 0 &&
- fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- } else if (err < 0) {
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
- }
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- /*
- * only update status, since the previous part has already updated the
- * qgroup info.
- */
- trans = btrfs_start_transaction(fs_info->quota_root, 1);
- if (IS_ERR(trans)) {
- err = PTR_ERR(trans);
- trans = NULL;
- btrfs_err(fs_info,
- "fail to start transaction for status update: %d",
- err);
- }
- mutex_lock(&fs_info->qgroup_rescan_lock);
- if (!btrfs_fs_closing(fs_info))
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
- if (trans) {
- ret = update_qgroup_status_item(trans);
- if (ret < 0) {
- err = ret;
- btrfs_err(fs_info, "fail to update qgroup status: %d",
- err);
- }
- }
- fs_info->qgroup_rescan_running = false;
- complete_all(&fs_info->qgroup_rescan_completion);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- if (!trans)
- return;
- btrfs_end_transaction(trans);
- if (btrfs_fs_closing(fs_info)) {
- btrfs_info(fs_info, "qgroup scan paused");
- } else if (err >= 0) {
- btrfs_info(fs_info, "qgroup scan completed%s",
- err > 0 ? " (inconsistency flag cleared)" : "");
- } else {
- btrfs_err(fs_info, "qgroup scan failed with %d", err);
- }
- }
- /*
- * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
- * memory required for the rescan context.
- */
- static int
- qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
- int init_flags)
- {
- int ret = 0;
- if (!init_flags) {
- /* we're resuming qgroup rescan at mount time */
- if (!(fs_info->qgroup_flags &
- BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
- btrfs_warn(fs_info,
- "qgroup rescan init failed, qgroup rescan is not queued");
- ret = -EINVAL;
- } else if (!(fs_info->qgroup_flags &
- BTRFS_QGROUP_STATUS_FLAG_ON)) {
- btrfs_warn(fs_info,
- "qgroup rescan init failed, qgroup is not enabled");
- ret = -EINVAL;
- }
- if (ret)
- return ret;
- }
- mutex_lock(&fs_info->qgroup_rescan_lock);
- spin_lock(&fs_info->qgroup_lock);
- if (init_flags) {
- if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
- btrfs_warn(fs_info,
- "qgroup rescan is already in progress");
- ret = -EINPROGRESS;
- } else if (!(fs_info->qgroup_flags &
- BTRFS_QGROUP_STATUS_FLAG_ON)) {
- btrfs_warn(fs_info,
- "qgroup rescan init failed, qgroup is not enabled");
- ret = -EINVAL;
- }
- if (ret) {
- spin_unlock(&fs_info->qgroup_lock);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- return ret;
- }
- fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
- }
- memset(&fs_info->qgroup_rescan_progress, 0,
- sizeof(fs_info->qgroup_rescan_progress));
- fs_info->qgroup_rescan_progress.objectid = progress_objectid;
- init_completion(&fs_info->qgroup_rescan_completion);
- fs_info->qgroup_rescan_running = true;
- spin_unlock(&fs_info->qgroup_lock);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- memset(&fs_info->qgroup_rescan_work, 0,
- sizeof(fs_info->qgroup_rescan_work));
- btrfs_init_work(&fs_info->qgroup_rescan_work,
- btrfs_qgroup_rescan_helper,
- btrfs_qgroup_rescan_worker, NULL, NULL);
- return 0;
- }
- static void
- qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
- {
- struct rb_node *n;
- struct btrfs_qgroup *qgroup;
- spin_lock(&fs_info->qgroup_lock);
- /* clear all current qgroup tracking information */
- for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
- qgroup = rb_entry(n, struct btrfs_qgroup, node);
- qgroup->rfer = 0;
- qgroup->rfer_cmpr = 0;
- qgroup->excl = 0;
- qgroup->excl_cmpr = 0;
- qgroup_dirty(fs_info, qgroup);
- }
- spin_unlock(&fs_info->qgroup_lock);
- }
- int
- btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
- {
- int ret = 0;
- struct btrfs_trans_handle *trans;
- ret = qgroup_rescan_init(fs_info, 0, 1);
- if (ret)
- return ret;
- /*
- * We have set the rescan_progress to 0, which means no more
- * delayed refs will be accounted by btrfs_qgroup_account_ref.
- * However, btrfs_qgroup_account_ref may be right after its call
- * to btrfs_find_all_roots, in which case it would still do the
- * accounting.
- * To solve this, we're committing the transaction, which will
- * ensure we run all delayed refs and only after that, we are
- * going to clear all tracking information for a clean start.
- */
- trans = btrfs_join_transaction(fs_info->fs_root);
- if (IS_ERR(trans)) {
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
- return PTR_ERR(trans);
- }
- ret = btrfs_commit_transaction(trans);
- if (ret) {
- fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
- return ret;
- }
- qgroup_rescan_zero_tracking(fs_info);
- btrfs_queue_work(fs_info->qgroup_rescan_workers,
- &fs_info->qgroup_rescan_work);
- return 0;
- }
- int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
- bool interruptible)
- {
- int running;
- int ret = 0;
- mutex_lock(&fs_info->qgroup_rescan_lock);
- spin_lock(&fs_info->qgroup_lock);
- running = fs_info->qgroup_rescan_running;
- spin_unlock(&fs_info->qgroup_lock);
- mutex_unlock(&fs_info->qgroup_rescan_lock);
- if (!running)
- return 0;
- if (interruptible)
- ret = wait_for_completion_interruptible(
- &fs_info->qgroup_rescan_completion);
- else
- wait_for_completion(&fs_info->qgroup_rescan_completion);
- return ret;
- }
- /*
- * this is only called from open_ctree where we're still single threaded, thus
- * locking is omitted here.
- */
- void
- btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
- {
- if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
- btrfs_queue_work(fs_info->qgroup_rescan_workers,
- &fs_info->qgroup_rescan_work);
- }
- /*
- * Reserve qgroup space for range [start, start + len).
- *
- * This function will either reserve space from related qgroups or doing
- * nothing if the range is already reserved.
- *
- * Return 0 for successful reserve
- * Return <0 for error (including -EQUOT)
- *
- * NOTE: this function may sleep for memory allocation.
- * if btrfs_qgroup_reserve_data() is called multiple times with
- * same @reserved, caller must ensure when error happens it's OK
- * to free *ALL* reserved space.
- */
- int btrfs_qgroup_reserve_data(struct inode *inode,
- struct extent_changeset **reserved_ret, u64 start,
- u64 len)
- {
- struct btrfs_root *root = BTRFS_I(inode)->root;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- struct extent_changeset *reserved;
- u64 orig_reserved;
- u64 to_reserve;
- int ret;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
- !is_fstree(root->objectid) || len == 0)
- return 0;
- /* @reserved parameter is mandatory for qgroup */
- if (WARN_ON(!reserved_ret))
- return -EINVAL;
- if (!*reserved_ret) {
- *reserved_ret = extent_changeset_alloc();
- if (!*reserved_ret)
- return -ENOMEM;
- }
- reserved = *reserved_ret;
- /* Record already reserved space */
- orig_reserved = reserved->bytes_changed;
- ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
- start + len -1, EXTENT_QGROUP_RESERVED, reserved);
- /* Newly reserved space */
- to_reserve = reserved->bytes_changed - orig_reserved;
- trace_btrfs_qgroup_reserve_data(inode, start, len,
- to_reserve, QGROUP_RESERVE);
- if (ret < 0)
- goto cleanup;
- ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
- if (ret < 0)
- goto cleanup;
- return ret;
- cleanup:
- /* cleanup *ALL* already reserved ranges */
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(&reserved->range_changed, &uiter)))
- clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
- unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
- /* Also free data bytes of already reserved one */
- btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid,
- orig_reserved, BTRFS_QGROUP_RSV_DATA);
- extent_changeset_release(reserved);
- return ret;
- }
- /* Free ranges specified by @reserved, normally in error path */
- static int qgroup_free_reserved_data(struct inode *inode,
- struct extent_changeset *reserved, u64 start, u64 len)
- {
- struct btrfs_root *root = BTRFS_I(inode)->root;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- struct extent_changeset changeset;
- int freed = 0;
- int ret;
- extent_changeset_init(&changeset);
- len = round_up(start + len, root->fs_info->sectorsize);
- start = round_down(start, root->fs_info->sectorsize);
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
- u64 range_start = unode->val;
- /* unode->aux is the inclusive end */
- u64 range_len = unode->aux - range_start + 1;
- u64 free_start;
- u64 free_len;
- extent_changeset_release(&changeset);
- /* Only free range in range [start, start + len) */
- if (range_start >= start + len ||
- range_start + range_len <= start)
- continue;
- free_start = max(range_start, start);
- free_len = min(start + len, range_start + range_len) -
- free_start;
- /*
- * TODO: To also modify reserved->ranges_reserved to reflect
- * the modification.
- *
- * However as long as we free qgroup reserved according to
- * EXTENT_QGROUP_RESERVED, we won't double free.
- * So not need to rush.
- */
- ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree,
- free_start, free_start + free_len - 1,
- EXTENT_QGROUP_RESERVED, &changeset);
- if (ret < 0)
- goto out;
- freed += changeset.bytes_changed;
- }
- btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
- BTRFS_QGROUP_RSV_DATA);
- ret = freed;
- out:
- extent_changeset_release(&changeset);
- return ret;
- }
- static int __btrfs_qgroup_release_data(struct inode *inode,
- struct extent_changeset *reserved, u64 start, u64 len,
- int free)
- {
- struct extent_changeset changeset;
- int trace_op = QGROUP_RELEASE;
- int ret;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED,
- &BTRFS_I(inode)->root->fs_info->flags))
- return 0;
- /* In release case, we shouldn't have @reserved */
- WARN_ON(!free && reserved);
- if (free && reserved)
- return qgroup_free_reserved_data(inode, reserved, start, len);
- extent_changeset_init(&changeset);
- ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
- start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
- if (ret < 0)
- goto out;
- if (free)
- trace_op = QGROUP_FREE;
- trace_btrfs_qgroup_release_data(inode, start, len,
- changeset.bytes_changed, trace_op);
- if (free)
- btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
- BTRFS_I(inode)->root->objectid,
- changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
- ret = changeset.bytes_changed;
- out:
- extent_changeset_release(&changeset);
- return ret;
- }
- /*
- * Free a reserved space range from io_tree and related qgroups
- *
- * Should be called when a range of pages get invalidated before reaching disk.
- * Or for error cleanup case.
- * if @reserved is given, only reserved range in [@start, @start + @len) will
- * be freed.
- *
- * For data written to disk, use btrfs_qgroup_release_data().
- *
- * NOTE: This function may sleep for memory allocation.
- */
- int btrfs_qgroup_free_data(struct inode *inode,
- struct extent_changeset *reserved, u64 start, u64 len)
- {
- return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
- }
- /*
- * Release a reserved space range from io_tree only.
- *
- * Should be called when a range of pages get written to disk and corresponding
- * FILE_EXTENT is inserted into corresponding root.
- *
- * Since new qgroup accounting framework will only update qgroup numbers at
- * commit_transaction() time, its reserved space shouldn't be freed from
- * related qgroups.
- *
- * But we should release the range from io_tree, to allow further write to be
- * COWed.
- *
- * NOTE: This function may sleep for memory allocation.
- */
- int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
- {
- return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
- }
- static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
- type != BTRFS_QGROUP_RSV_META_PERTRANS)
- return;
- if (num_bytes == 0)
- return;
- spin_lock(&root->qgroup_meta_rsv_lock);
- if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
- root->qgroup_meta_rsv_prealloc += num_bytes;
- else
- root->qgroup_meta_rsv_pertrans += num_bytes;
- spin_unlock(&root->qgroup_meta_rsv_lock);
- }
- static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
- type != BTRFS_QGROUP_RSV_META_PERTRANS)
- return 0;
- if (num_bytes == 0)
- return 0;
- spin_lock(&root->qgroup_meta_rsv_lock);
- if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
- num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
- num_bytes);
- root->qgroup_meta_rsv_prealloc -= num_bytes;
- } else {
- num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
- num_bytes);
- root->qgroup_meta_rsv_pertrans -= num_bytes;
- }
- spin_unlock(&root->qgroup_meta_rsv_lock);
- return num_bytes;
- }
- int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
- enum btrfs_qgroup_rsv_type type, bool enforce)
- {
- struct btrfs_fs_info *fs_info = root->fs_info;
- int ret;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
- !is_fstree(root->objectid) || num_bytes == 0)
- return 0;
- BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
- trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
- ret = qgroup_reserve(root, num_bytes, enforce, type);
- if (ret < 0)
- return ret;
- /*
- * Record what we have reserved into root.
- *
- * To avoid quota disabled->enabled underflow.
- * In that case, we may try to free space we haven't reserved
- * (since quota was disabled), so record what we reserved into root.
- * And ensure later release won't underflow this number.
- */
- add_root_meta_rsv(root, num_bytes, type);
- return ret;
- }
- void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
- {
- struct btrfs_fs_info *fs_info = root->fs_info;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
- !is_fstree(root->objectid))
- return;
- /* TODO: Update trace point to handle such free */
- trace_qgroup_meta_free_all_pertrans(root);
- /* Special value -1 means to free all reserved space */
- btrfs_qgroup_free_refroot(fs_info, root->objectid, (u64)-1,
- BTRFS_QGROUP_RSV_META_PERTRANS);
- }
- void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
- enum btrfs_qgroup_rsv_type type)
- {
- struct btrfs_fs_info *fs_info = root->fs_info;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
- !is_fstree(root->objectid))
- return;
- /*
- * reservation for META_PREALLOC can happen before quota is enabled,
- * which can lead to underflow.
- * Here ensure we will only free what we really have reserved.
- */
- num_bytes = sub_root_meta_rsv(root, num_bytes, type);
- BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
- trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
- btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes, type);
- }
- static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
- int num_bytes)
- {
- struct btrfs_root *quota_root = fs_info->quota_root;
- struct btrfs_qgroup *qgroup;
- struct ulist_node *unode;
- struct ulist_iterator uiter;
- int ret = 0;
- if (num_bytes == 0)
- return;
- if (!quota_root)
- return;
- spin_lock(&fs_info->qgroup_lock);
- qgroup = find_qgroup_rb(fs_info, ref_root);
- if (!qgroup)
- goto out;
- ulist_reinit(fs_info->qgroup_ulist);
- ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
- qgroup_to_aux(qgroup), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- ULIST_ITER_INIT(&uiter);
- while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
- struct btrfs_qgroup *qg;
- struct btrfs_qgroup_list *glist;
- qg = unode_aux_to_qgroup(unode);
- qgroup_rsv_release(fs_info, qg, num_bytes,
- BTRFS_QGROUP_RSV_META_PREALLOC);
- qgroup_rsv_add(fs_info, qg, num_bytes,
- BTRFS_QGROUP_RSV_META_PERTRANS);
- list_for_each_entry(glist, &qg->groups, next_group) {
- ret = ulist_add(fs_info->qgroup_ulist,
- glist->group->qgroupid,
- qgroup_to_aux(glist->group), GFP_ATOMIC);
- if (ret < 0)
- goto out;
- }
- }
- out:
- spin_unlock(&fs_info->qgroup_lock);
- }
- void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
- {
- struct btrfs_fs_info *fs_info = root->fs_info;
- if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
- !is_fstree(root->objectid))
- return;
- /* Same as btrfs_qgroup_free_meta_prealloc() */
- num_bytes = sub_root_meta_rsv(root, num_bytes,
- BTRFS_QGROUP_RSV_META_PREALLOC);
- trace_qgroup_meta_convert(root, num_bytes);
- qgroup_convert_meta(fs_info, root->objectid, num_bytes);
- }
- /*
- * Check qgroup reserved space leaking, normally at destroy inode
- * time
- */
- void btrfs_qgroup_check_reserved_leak(struct inode *inode)
- {
- struct extent_changeset changeset;
- struct ulist_node *unode;
- struct ulist_iterator iter;
- int ret;
- extent_changeset_init(&changeset);
- ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
- EXTENT_QGROUP_RESERVED, &changeset);
- WARN_ON(ret < 0);
- if (WARN_ON(changeset.bytes_changed)) {
- ULIST_ITER_INIT(&iter);
- while ((unode = ulist_next(&changeset.range_changed, &iter))) {
- btrfs_warn(BTRFS_I(inode)->root->fs_info,
- "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
- inode->i_ino, unode->val, unode->aux);
- }
- btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
- BTRFS_I(inode)->root->objectid,
- changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
- }
- extent_changeset_release(&changeset);
- }
|