qgroup.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  1. /*
  2. * Copyright (C) 2011 STRATO. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/writeback.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/btrfs.h>
  26. #include "ctree.h"
  27. #include "transaction.h"
  28. #include "disk-io.h"
  29. #include "locking.h"
  30. #include "ulist.h"
  31. #include "backref.h"
  32. #include "extent_io.h"
  33. #include "qgroup.h"
  34. /* TODO XXX FIXME
  35. * - subvol delete -> delete when ref goes to 0? delete limits also?
  36. * - reorganize keys
  37. * - compressed
  38. * - sync
  39. * - copy also limits on subvol creation
  40. * - limit
  41. * - caches fuer ulists
  42. * - performance benchmarks
  43. * - check all ioctl parameters
  44. */
  45. static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
  46. int mod)
  47. {
  48. if (qg->old_refcnt < seq)
  49. qg->old_refcnt = seq;
  50. qg->old_refcnt += mod;
  51. }
  52. static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
  53. int mod)
  54. {
  55. if (qg->new_refcnt < seq)
  56. qg->new_refcnt = seq;
  57. qg->new_refcnt += mod;
  58. }
  59. static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
  60. {
  61. if (qg->old_refcnt < seq)
  62. return 0;
  63. return qg->old_refcnt - seq;
  64. }
  65. static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
  66. {
  67. if (qg->new_refcnt < seq)
  68. return 0;
  69. return qg->new_refcnt - seq;
  70. }
  71. /*
  72. * glue structure to represent the relations between qgroups.
  73. */
  74. struct btrfs_qgroup_list {
  75. struct list_head next_group;
  76. struct list_head next_member;
  77. struct btrfs_qgroup *group;
  78. struct btrfs_qgroup *member;
  79. };
  80. static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
  81. {
  82. return (u64)(uintptr_t)qg;
  83. }
  84. static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
  85. {
  86. return (struct btrfs_qgroup *)(uintptr_t)n->aux;
  87. }
  88. static int
  89. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  90. int init_flags);
  91. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  92. /* must be called with qgroup_ioctl_lock held */
  93. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  94. u64 qgroupid)
  95. {
  96. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  97. struct btrfs_qgroup *qgroup;
  98. while (n) {
  99. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  100. if (qgroup->qgroupid < qgroupid)
  101. n = n->rb_left;
  102. else if (qgroup->qgroupid > qgroupid)
  103. n = n->rb_right;
  104. else
  105. return qgroup;
  106. }
  107. return NULL;
  108. }
  109. /* must be called with qgroup_lock held */
  110. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  111. u64 qgroupid)
  112. {
  113. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  114. struct rb_node *parent = NULL;
  115. struct btrfs_qgroup *qgroup;
  116. while (*p) {
  117. parent = *p;
  118. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  119. if (qgroup->qgroupid < qgroupid)
  120. p = &(*p)->rb_left;
  121. else if (qgroup->qgroupid > qgroupid)
  122. p = &(*p)->rb_right;
  123. else
  124. return qgroup;
  125. }
  126. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  127. if (!qgroup)
  128. return ERR_PTR(-ENOMEM);
  129. qgroup->qgroupid = qgroupid;
  130. INIT_LIST_HEAD(&qgroup->groups);
  131. INIT_LIST_HEAD(&qgroup->members);
  132. INIT_LIST_HEAD(&qgroup->dirty);
  133. rb_link_node(&qgroup->node, parent, p);
  134. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  135. return qgroup;
  136. }
  137. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  138. {
  139. struct btrfs_qgroup_list *list;
  140. list_del(&qgroup->dirty);
  141. while (!list_empty(&qgroup->groups)) {
  142. list = list_first_entry(&qgroup->groups,
  143. struct btrfs_qgroup_list, next_group);
  144. list_del(&list->next_group);
  145. list_del(&list->next_member);
  146. kfree(list);
  147. }
  148. while (!list_empty(&qgroup->members)) {
  149. list = list_first_entry(&qgroup->members,
  150. struct btrfs_qgroup_list, next_member);
  151. list_del(&list->next_group);
  152. list_del(&list->next_member);
  153. kfree(list);
  154. }
  155. kfree(qgroup);
  156. }
  157. /* must be called with qgroup_lock held */
  158. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  159. {
  160. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  161. if (!qgroup)
  162. return -ENOENT;
  163. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  164. __del_qgroup_rb(qgroup);
  165. return 0;
  166. }
  167. /* must be called with qgroup_lock held */
  168. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  169. u64 memberid, u64 parentid)
  170. {
  171. struct btrfs_qgroup *member;
  172. struct btrfs_qgroup *parent;
  173. struct btrfs_qgroup_list *list;
  174. member = find_qgroup_rb(fs_info, memberid);
  175. parent = find_qgroup_rb(fs_info, parentid);
  176. if (!member || !parent)
  177. return -ENOENT;
  178. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  179. if (!list)
  180. return -ENOMEM;
  181. list->group = parent;
  182. list->member = member;
  183. list_add_tail(&list->next_group, &member->groups);
  184. list_add_tail(&list->next_member, &parent->members);
  185. return 0;
  186. }
  187. /* must be called with qgroup_lock held */
  188. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  189. u64 memberid, u64 parentid)
  190. {
  191. struct btrfs_qgroup *member;
  192. struct btrfs_qgroup *parent;
  193. struct btrfs_qgroup_list *list;
  194. member = find_qgroup_rb(fs_info, memberid);
  195. parent = find_qgroup_rb(fs_info, parentid);
  196. if (!member || !parent)
  197. return -ENOENT;
  198. list_for_each_entry(list, &member->groups, next_group) {
  199. if (list->group == parent) {
  200. list_del(&list->next_group);
  201. list_del(&list->next_member);
  202. kfree(list);
  203. return 0;
  204. }
  205. }
  206. return -ENOENT;
  207. }
  208. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  209. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  210. u64 rfer, u64 excl)
  211. {
  212. struct btrfs_qgroup *qgroup;
  213. qgroup = find_qgroup_rb(fs_info, qgroupid);
  214. if (!qgroup)
  215. return -EINVAL;
  216. if (qgroup->rfer != rfer || qgroup->excl != excl)
  217. return -EINVAL;
  218. return 0;
  219. }
  220. #endif
  221. /*
  222. * The full config is read in one go, only called from open_ctree()
  223. * It doesn't use any locking, as at this point we're still single-threaded
  224. */
  225. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  226. {
  227. struct btrfs_key key;
  228. struct btrfs_key found_key;
  229. struct btrfs_root *quota_root = fs_info->quota_root;
  230. struct btrfs_path *path = NULL;
  231. struct extent_buffer *l;
  232. int slot;
  233. int ret = 0;
  234. u64 flags = 0;
  235. u64 rescan_progress = 0;
  236. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  237. return 0;
  238. fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
  239. if (!fs_info->qgroup_ulist) {
  240. ret = -ENOMEM;
  241. goto out;
  242. }
  243. path = btrfs_alloc_path();
  244. if (!path) {
  245. ret = -ENOMEM;
  246. goto out;
  247. }
  248. /* default this to quota off, in case no status key is found */
  249. fs_info->qgroup_flags = 0;
  250. /*
  251. * pass 1: read status, all qgroup infos and limits
  252. */
  253. key.objectid = 0;
  254. key.type = 0;
  255. key.offset = 0;
  256. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  257. if (ret)
  258. goto out;
  259. while (1) {
  260. struct btrfs_qgroup *qgroup;
  261. slot = path->slots[0];
  262. l = path->nodes[0];
  263. btrfs_item_key_to_cpu(l, &found_key, slot);
  264. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  265. struct btrfs_qgroup_status_item *ptr;
  266. ptr = btrfs_item_ptr(l, slot,
  267. struct btrfs_qgroup_status_item);
  268. if (btrfs_qgroup_status_version(l, ptr) !=
  269. BTRFS_QGROUP_STATUS_VERSION) {
  270. btrfs_err(fs_info,
  271. "old qgroup version, quota disabled");
  272. goto out;
  273. }
  274. if (btrfs_qgroup_status_generation(l, ptr) !=
  275. fs_info->generation) {
  276. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  277. btrfs_err(fs_info,
  278. "qgroup generation mismatch, marked as inconsistent");
  279. }
  280. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  281. ptr);
  282. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  283. goto next1;
  284. }
  285. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  286. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  287. goto next1;
  288. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  289. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  290. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  291. btrfs_err(fs_info, "inconsistent qgroup config");
  292. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  293. }
  294. if (!qgroup) {
  295. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  296. if (IS_ERR(qgroup)) {
  297. ret = PTR_ERR(qgroup);
  298. goto out;
  299. }
  300. }
  301. switch (found_key.type) {
  302. case BTRFS_QGROUP_INFO_KEY: {
  303. struct btrfs_qgroup_info_item *ptr;
  304. ptr = btrfs_item_ptr(l, slot,
  305. struct btrfs_qgroup_info_item);
  306. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  307. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  308. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  309. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  310. /* generation currently unused */
  311. break;
  312. }
  313. case BTRFS_QGROUP_LIMIT_KEY: {
  314. struct btrfs_qgroup_limit_item *ptr;
  315. ptr = btrfs_item_ptr(l, slot,
  316. struct btrfs_qgroup_limit_item);
  317. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  318. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  319. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  320. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  321. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  322. break;
  323. }
  324. }
  325. next1:
  326. ret = btrfs_next_item(quota_root, path);
  327. if (ret < 0)
  328. goto out;
  329. if (ret)
  330. break;
  331. }
  332. btrfs_release_path(path);
  333. /*
  334. * pass 2: read all qgroup relations
  335. */
  336. key.objectid = 0;
  337. key.type = BTRFS_QGROUP_RELATION_KEY;
  338. key.offset = 0;
  339. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  340. if (ret)
  341. goto out;
  342. while (1) {
  343. slot = path->slots[0];
  344. l = path->nodes[0];
  345. btrfs_item_key_to_cpu(l, &found_key, slot);
  346. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  347. goto next2;
  348. if (found_key.objectid > found_key.offset) {
  349. /* parent <- member, not needed to build config */
  350. /* FIXME should we omit the key completely? */
  351. goto next2;
  352. }
  353. ret = add_relation_rb(fs_info, found_key.objectid,
  354. found_key.offset);
  355. if (ret == -ENOENT) {
  356. btrfs_warn(fs_info,
  357. "orphan qgroup relation 0x%llx->0x%llx",
  358. found_key.objectid, found_key.offset);
  359. ret = 0; /* ignore the error */
  360. }
  361. if (ret)
  362. goto out;
  363. next2:
  364. ret = btrfs_next_item(quota_root, path);
  365. if (ret < 0)
  366. goto out;
  367. if (ret)
  368. break;
  369. }
  370. out:
  371. fs_info->qgroup_flags |= flags;
  372. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  373. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  374. else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  375. ret >= 0)
  376. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  377. btrfs_free_path(path);
  378. if (ret < 0) {
  379. ulist_free(fs_info->qgroup_ulist);
  380. fs_info->qgroup_ulist = NULL;
  381. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  382. }
  383. return ret < 0 ? ret : 0;
  384. }
  385. /*
  386. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  387. * first two are in single-threaded paths.And for the third one, we have set
  388. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  389. * up the in-memory structures without qgroup_lock held.
  390. */
  391. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  392. {
  393. struct rb_node *n;
  394. struct btrfs_qgroup *qgroup;
  395. while ((n = rb_first(&fs_info->qgroup_tree))) {
  396. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  397. rb_erase(n, &fs_info->qgroup_tree);
  398. __del_qgroup_rb(qgroup);
  399. }
  400. /*
  401. * we call btrfs_free_qgroup_config() when umounting
  402. * filesystem and disabling quota, so we set qgroup_ulist
  403. * to be null here to avoid double free.
  404. */
  405. ulist_free(fs_info->qgroup_ulist);
  406. fs_info->qgroup_ulist = NULL;
  407. }
  408. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  409. struct btrfs_root *quota_root,
  410. u64 src, u64 dst)
  411. {
  412. int ret;
  413. struct btrfs_path *path;
  414. struct btrfs_key key;
  415. path = btrfs_alloc_path();
  416. if (!path)
  417. return -ENOMEM;
  418. key.objectid = src;
  419. key.type = BTRFS_QGROUP_RELATION_KEY;
  420. key.offset = dst;
  421. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  422. btrfs_mark_buffer_dirty(path->nodes[0]);
  423. btrfs_free_path(path);
  424. return ret;
  425. }
  426. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  427. struct btrfs_root *quota_root,
  428. u64 src, u64 dst)
  429. {
  430. int ret;
  431. struct btrfs_path *path;
  432. struct btrfs_key key;
  433. path = btrfs_alloc_path();
  434. if (!path)
  435. return -ENOMEM;
  436. key.objectid = src;
  437. key.type = BTRFS_QGROUP_RELATION_KEY;
  438. key.offset = dst;
  439. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  440. if (ret < 0)
  441. goto out;
  442. if (ret > 0) {
  443. ret = -ENOENT;
  444. goto out;
  445. }
  446. ret = btrfs_del_item(trans, quota_root, path);
  447. out:
  448. btrfs_free_path(path);
  449. return ret;
  450. }
  451. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  452. struct btrfs_root *quota_root, u64 qgroupid)
  453. {
  454. int ret;
  455. struct btrfs_path *path;
  456. struct btrfs_qgroup_info_item *qgroup_info;
  457. struct btrfs_qgroup_limit_item *qgroup_limit;
  458. struct extent_buffer *leaf;
  459. struct btrfs_key key;
  460. if (btrfs_is_testing(quota_root->fs_info))
  461. return 0;
  462. path = btrfs_alloc_path();
  463. if (!path)
  464. return -ENOMEM;
  465. key.objectid = 0;
  466. key.type = BTRFS_QGROUP_INFO_KEY;
  467. key.offset = qgroupid;
  468. /*
  469. * Avoid a transaction abort by catching -EEXIST here. In that
  470. * case, we proceed by re-initializing the existing structure
  471. * on disk.
  472. */
  473. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  474. sizeof(*qgroup_info));
  475. if (ret && ret != -EEXIST)
  476. goto out;
  477. leaf = path->nodes[0];
  478. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  479. struct btrfs_qgroup_info_item);
  480. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  481. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  482. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  483. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  484. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  485. btrfs_mark_buffer_dirty(leaf);
  486. btrfs_release_path(path);
  487. key.type = BTRFS_QGROUP_LIMIT_KEY;
  488. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  489. sizeof(*qgroup_limit));
  490. if (ret && ret != -EEXIST)
  491. goto out;
  492. leaf = path->nodes[0];
  493. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  494. struct btrfs_qgroup_limit_item);
  495. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  496. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  497. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  498. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  499. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  500. btrfs_mark_buffer_dirty(leaf);
  501. ret = 0;
  502. out:
  503. btrfs_free_path(path);
  504. return ret;
  505. }
  506. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  507. struct btrfs_root *quota_root, u64 qgroupid)
  508. {
  509. int ret;
  510. struct btrfs_path *path;
  511. struct btrfs_key key;
  512. path = btrfs_alloc_path();
  513. if (!path)
  514. return -ENOMEM;
  515. key.objectid = 0;
  516. key.type = BTRFS_QGROUP_INFO_KEY;
  517. key.offset = qgroupid;
  518. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  519. if (ret < 0)
  520. goto out;
  521. if (ret > 0) {
  522. ret = -ENOENT;
  523. goto out;
  524. }
  525. ret = btrfs_del_item(trans, quota_root, path);
  526. if (ret)
  527. goto out;
  528. btrfs_release_path(path);
  529. key.type = BTRFS_QGROUP_LIMIT_KEY;
  530. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  531. if (ret < 0)
  532. goto out;
  533. if (ret > 0) {
  534. ret = -ENOENT;
  535. goto out;
  536. }
  537. ret = btrfs_del_item(trans, quota_root, path);
  538. out:
  539. btrfs_free_path(path);
  540. return ret;
  541. }
  542. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  543. struct btrfs_root *root,
  544. struct btrfs_qgroup *qgroup)
  545. {
  546. struct btrfs_path *path;
  547. struct btrfs_key key;
  548. struct extent_buffer *l;
  549. struct btrfs_qgroup_limit_item *qgroup_limit;
  550. int ret;
  551. int slot;
  552. key.objectid = 0;
  553. key.type = BTRFS_QGROUP_LIMIT_KEY;
  554. key.offset = qgroup->qgroupid;
  555. path = btrfs_alloc_path();
  556. if (!path)
  557. return -ENOMEM;
  558. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  559. if (ret > 0)
  560. ret = -ENOENT;
  561. if (ret)
  562. goto out;
  563. l = path->nodes[0];
  564. slot = path->slots[0];
  565. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  566. btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
  567. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
  568. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
  569. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
  570. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
  571. btrfs_mark_buffer_dirty(l);
  572. out:
  573. btrfs_free_path(path);
  574. return ret;
  575. }
  576. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  577. struct btrfs_root *root,
  578. struct btrfs_qgroup *qgroup)
  579. {
  580. struct btrfs_path *path;
  581. struct btrfs_key key;
  582. struct extent_buffer *l;
  583. struct btrfs_qgroup_info_item *qgroup_info;
  584. int ret;
  585. int slot;
  586. if (btrfs_is_testing(root->fs_info))
  587. return 0;
  588. key.objectid = 0;
  589. key.type = BTRFS_QGROUP_INFO_KEY;
  590. key.offset = qgroup->qgroupid;
  591. path = btrfs_alloc_path();
  592. if (!path)
  593. return -ENOMEM;
  594. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  595. if (ret > 0)
  596. ret = -ENOENT;
  597. if (ret)
  598. goto out;
  599. l = path->nodes[0];
  600. slot = path->slots[0];
  601. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  602. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  603. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  604. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  605. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  606. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  607. btrfs_mark_buffer_dirty(l);
  608. out:
  609. btrfs_free_path(path);
  610. return ret;
  611. }
  612. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  613. struct btrfs_fs_info *fs_info,
  614. struct btrfs_root *root)
  615. {
  616. struct btrfs_path *path;
  617. struct btrfs_key key;
  618. struct extent_buffer *l;
  619. struct btrfs_qgroup_status_item *ptr;
  620. int ret;
  621. int slot;
  622. key.objectid = 0;
  623. key.type = BTRFS_QGROUP_STATUS_KEY;
  624. key.offset = 0;
  625. path = btrfs_alloc_path();
  626. if (!path)
  627. return -ENOMEM;
  628. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  629. if (ret > 0)
  630. ret = -ENOENT;
  631. if (ret)
  632. goto out;
  633. l = path->nodes[0];
  634. slot = path->slots[0];
  635. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  636. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  637. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  638. btrfs_set_qgroup_status_rescan(l, ptr,
  639. fs_info->qgroup_rescan_progress.objectid);
  640. btrfs_mark_buffer_dirty(l);
  641. out:
  642. btrfs_free_path(path);
  643. return ret;
  644. }
  645. /*
  646. * called with qgroup_lock held
  647. */
  648. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  649. struct btrfs_root *root)
  650. {
  651. struct btrfs_path *path;
  652. struct btrfs_key key;
  653. struct extent_buffer *leaf = NULL;
  654. int ret;
  655. int nr = 0;
  656. path = btrfs_alloc_path();
  657. if (!path)
  658. return -ENOMEM;
  659. path->leave_spinning = 1;
  660. key.objectid = 0;
  661. key.offset = 0;
  662. key.type = 0;
  663. while (1) {
  664. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  665. if (ret < 0)
  666. goto out;
  667. leaf = path->nodes[0];
  668. nr = btrfs_header_nritems(leaf);
  669. if (!nr)
  670. break;
  671. /*
  672. * delete the leaf one by one
  673. * since the whole tree is going
  674. * to be deleted.
  675. */
  676. path->slots[0] = 0;
  677. ret = btrfs_del_items(trans, root, path, 0, nr);
  678. if (ret)
  679. goto out;
  680. btrfs_release_path(path);
  681. }
  682. ret = 0;
  683. out:
  684. set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
  685. btrfs_free_path(path);
  686. return ret;
  687. }
  688. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  689. struct btrfs_fs_info *fs_info)
  690. {
  691. struct btrfs_root *quota_root;
  692. struct btrfs_root *tree_root = fs_info->tree_root;
  693. struct btrfs_path *path = NULL;
  694. struct btrfs_qgroup_status_item *ptr;
  695. struct extent_buffer *leaf;
  696. struct btrfs_key key;
  697. struct btrfs_key found_key;
  698. struct btrfs_qgroup *qgroup = NULL;
  699. int ret = 0;
  700. int slot;
  701. mutex_lock(&fs_info->qgroup_ioctl_lock);
  702. if (fs_info->quota_root) {
  703. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  704. goto out;
  705. }
  706. fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
  707. if (!fs_info->qgroup_ulist) {
  708. ret = -ENOMEM;
  709. goto out;
  710. }
  711. /*
  712. * initially create the quota tree
  713. */
  714. quota_root = btrfs_create_tree(trans, fs_info,
  715. BTRFS_QUOTA_TREE_OBJECTID);
  716. if (IS_ERR(quota_root)) {
  717. ret = PTR_ERR(quota_root);
  718. goto out;
  719. }
  720. path = btrfs_alloc_path();
  721. if (!path) {
  722. ret = -ENOMEM;
  723. goto out_free_root;
  724. }
  725. key.objectid = 0;
  726. key.type = BTRFS_QGROUP_STATUS_KEY;
  727. key.offset = 0;
  728. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  729. sizeof(*ptr));
  730. if (ret)
  731. goto out_free_path;
  732. leaf = path->nodes[0];
  733. ptr = btrfs_item_ptr(leaf, path->slots[0],
  734. struct btrfs_qgroup_status_item);
  735. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  736. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  737. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  738. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  739. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  740. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  741. btrfs_mark_buffer_dirty(leaf);
  742. key.objectid = 0;
  743. key.type = BTRFS_ROOT_REF_KEY;
  744. key.offset = 0;
  745. btrfs_release_path(path);
  746. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  747. if (ret > 0)
  748. goto out_add_root;
  749. if (ret < 0)
  750. goto out_free_path;
  751. while (1) {
  752. slot = path->slots[0];
  753. leaf = path->nodes[0];
  754. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  755. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  756. ret = add_qgroup_item(trans, quota_root,
  757. found_key.offset);
  758. if (ret)
  759. goto out_free_path;
  760. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  761. if (IS_ERR(qgroup)) {
  762. ret = PTR_ERR(qgroup);
  763. goto out_free_path;
  764. }
  765. }
  766. ret = btrfs_next_item(tree_root, path);
  767. if (ret < 0)
  768. goto out_free_path;
  769. if (ret)
  770. break;
  771. }
  772. out_add_root:
  773. btrfs_release_path(path);
  774. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  775. if (ret)
  776. goto out_free_path;
  777. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  778. if (IS_ERR(qgroup)) {
  779. ret = PTR_ERR(qgroup);
  780. goto out_free_path;
  781. }
  782. spin_lock(&fs_info->qgroup_lock);
  783. fs_info->quota_root = quota_root;
  784. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  785. spin_unlock(&fs_info->qgroup_lock);
  786. out_free_path:
  787. btrfs_free_path(path);
  788. out_free_root:
  789. if (ret) {
  790. free_extent_buffer(quota_root->node);
  791. free_extent_buffer(quota_root->commit_root);
  792. kfree(quota_root);
  793. }
  794. out:
  795. if (ret) {
  796. ulist_free(fs_info->qgroup_ulist);
  797. fs_info->qgroup_ulist = NULL;
  798. }
  799. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  800. return ret;
  801. }
  802. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  803. struct btrfs_fs_info *fs_info)
  804. {
  805. struct btrfs_root *tree_root = fs_info->tree_root;
  806. struct btrfs_root *quota_root;
  807. int ret = 0;
  808. mutex_lock(&fs_info->qgroup_ioctl_lock);
  809. if (!fs_info->quota_root)
  810. goto out;
  811. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  812. set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
  813. btrfs_qgroup_wait_for_completion(fs_info, false);
  814. spin_lock(&fs_info->qgroup_lock);
  815. quota_root = fs_info->quota_root;
  816. fs_info->quota_root = NULL;
  817. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  818. spin_unlock(&fs_info->qgroup_lock);
  819. btrfs_free_qgroup_config(fs_info);
  820. ret = btrfs_clean_quota_tree(trans, quota_root);
  821. if (ret)
  822. goto out;
  823. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  824. if (ret)
  825. goto out;
  826. list_del(&quota_root->dirty_list);
  827. btrfs_tree_lock(quota_root->node);
  828. clean_tree_block(fs_info, quota_root->node);
  829. btrfs_tree_unlock(quota_root->node);
  830. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  831. free_extent_buffer(quota_root->node);
  832. free_extent_buffer(quota_root->commit_root);
  833. kfree(quota_root);
  834. out:
  835. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  836. return ret;
  837. }
  838. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  839. struct btrfs_qgroup *qgroup)
  840. {
  841. if (list_empty(&qgroup->dirty))
  842. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  843. }
  844. static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
  845. struct btrfs_qgroup *qgroup,
  846. u64 num_bytes)
  847. {
  848. btrfs_warn(fs_info,
  849. "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
  850. qgroup->qgroupid, qgroup->reserved, num_bytes);
  851. qgroup->reserved = 0;
  852. }
  853. /*
  854. * The easy accounting, if we are adding/removing the only ref for an extent
  855. * then this qgroup and all of the parent qgroups get their reference and
  856. * exclusive counts adjusted.
  857. *
  858. * Caller should hold fs_info->qgroup_lock.
  859. */
  860. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  861. struct ulist *tmp, u64 ref_root,
  862. u64 num_bytes, int sign)
  863. {
  864. struct btrfs_qgroup *qgroup;
  865. struct btrfs_qgroup_list *glist;
  866. struct ulist_node *unode;
  867. struct ulist_iterator uiter;
  868. int ret = 0;
  869. qgroup = find_qgroup_rb(fs_info, ref_root);
  870. if (!qgroup)
  871. goto out;
  872. qgroup->rfer += sign * num_bytes;
  873. qgroup->rfer_cmpr += sign * num_bytes;
  874. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  875. qgroup->excl += sign * num_bytes;
  876. qgroup->excl_cmpr += sign * num_bytes;
  877. if (sign > 0) {
  878. trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
  879. if (WARN_ON(qgroup->reserved < num_bytes))
  880. report_reserved_underflow(fs_info, qgroup, num_bytes);
  881. else
  882. qgroup->reserved -= num_bytes;
  883. }
  884. qgroup_dirty(fs_info, qgroup);
  885. /* Get all of the parent groups that contain this qgroup */
  886. list_for_each_entry(glist, &qgroup->groups, next_group) {
  887. ret = ulist_add(tmp, glist->group->qgroupid,
  888. qgroup_to_aux(glist->group), GFP_ATOMIC);
  889. if (ret < 0)
  890. goto out;
  891. }
  892. /* Iterate all of the parents and adjust their reference counts */
  893. ULIST_ITER_INIT(&uiter);
  894. while ((unode = ulist_next(tmp, &uiter))) {
  895. qgroup = unode_aux_to_qgroup(unode);
  896. qgroup->rfer += sign * num_bytes;
  897. qgroup->rfer_cmpr += sign * num_bytes;
  898. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  899. qgroup->excl += sign * num_bytes;
  900. if (sign > 0) {
  901. trace_qgroup_update_reserve(fs_info, qgroup,
  902. -(s64)num_bytes);
  903. if (WARN_ON(qgroup->reserved < num_bytes))
  904. report_reserved_underflow(fs_info, qgroup,
  905. num_bytes);
  906. else
  907. qgroup->reserved -= num_bytes;
  908. }
  909. qgroup->excl_cmpr += sign * num_bytes;
  910. qgroup_dirty(fs_info, qgroup);
  911. /* Add any parents of the parents */
  912. list_for_each_entry(glist, &qgroup->groups, next_group) {
  913. ret = ulist_add(tmp, glist->group->qgroupid,
  914. qgroup_to_aux(glist->group), GFP_ATOMIC);
  915. if (ret < 0)
  916. goto out;
  917. }
  918. }
  919. ret = 0;
  920. out:
  921. return ret;
  922. }
  923. /*
  924. * Quick path for updating qgroup with only excl refs.
  925. *
  926. * In that case, just update all parent will be enough.
  927. * Or we needs to do a full rescan.
  928. * Caller should also hold fs_info->qgroup_lock.
  929. *
  930. * Return 0 for quick update, return >0 for need to full rescan
  931. * and mark INCONSISTENT flag.
  932. * Return < 0 for other error.
  933. */
  934. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  935. struct ulist *tmp, u64 src, u64 dst,
  936. int sign)
  937. {
  938. struct btrfs_qgroup *qgroup;
  939. int ret = 1;
  940. int err = 0;
  941. qgroup = find_qgroup_rb(fs_info, src);
  942. if (!qgroup)
  943. goto out;
  944. if (qgroup->excl == qgroup->rfer) {
  945. ret = 0;
  946. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  947. qgroup->excl, sign);
  948. if (err < 0) {
  949. ret = err;
  950. goto out;
  951. }
  952. }
  953. out:
  954. if (ret)
  955. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  956. return ret;
  957. }
  958. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  959. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  960. {
  961. struct btrfs_root *quota_root;
  962. struct btrfs_qgroup *parent;
  963. struct btrfs_qgroup *member;
  964. struct btrfs_qgroup_list *list;
  965. struct ulist *tmp;
  966. int ret = 0;
  967. /* Check the level of src and dst first */
  968. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  969. return -EINVAL;
  970. tmp = ulist_alloc(GFP_KERNEL);
  971. if (!tmp)
  972. return -ENOMEM;
  973. mutex_lock(&fs_info->qgroup_ioctl_lock);
  974. quota_root = fs_info->quota_root;
  975. if (!quota_root) {
  976. ret = -EINVAL;
  977. goto out;
  978. }
  979. member = find_qgroup_rb(fs_info, src);
  980. parent = find_qgroup_rb(fs_info, dst);
  981. if (!member || !parent) {
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. /* check if such qgroup relation exist firstly */
  986. list_for_each_entry(list, &member->groups, next_group) {
  987. if (list->group == parent) {
  988. ret = -EEXIST;
  989. goto out;
  990. }
  991. }
  992. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  993. if (ret)
  994. goto out;
  995. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  996. if (ret) {
  997. del_qgroup_relation_item(trans, quota_root, src, dst);
  998. goto out;
  999. }
  1000. spin_lock(&fs_info->qgroup_lock);
  1001. ret = add_relation_rb(fs_info, src, dst);
  1002. if (ret < 0) {
  1003. spin_unlock(&fs_info->qgroup_lock);
  1004. goto out;
  1005. }
  1006. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1007. spin_unlock(&fs_info->qgroup_lock);
  1008. out:
  1009. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1010. ulist_free(tmp);
  1011. return ret;
  1012. }
  1013. static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1014. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1015. {
  1016. struct btrfs_root *quota_root;
  1017. struct btrfs_qgroup *parent;
  1018. struct btrfs_qgroup *member;
  1019. struct btrfs_qgroup_list *list;
  1020. struct ulist *tmp;
  1021. int ret = 0;
  1022. int err;
  1023. tmp = ulist_alloc(GFP_KERNEL);
  1024. if (!tmp)
  1025. return -ENOMEM;
  1026. quota_root = fs_info->quota_root;
  1027. if (!quota_root) {
  1028. ret = -EINVAL;
  1029. goto out;
  1030. }
  1031. member = find_qgroup_rb(fs_info, src);
  1032. parent = find_qgroup_rb(fs_info, dst);
  1033. if (!member || !parent) {
  1034. ret = -EINVAL;
  1035. goto out;
  1036. }
  1037. /* check if such qgroup relation exist firstly */
  1038. list_for_each_entry(list, &member->groups, next_group) {
  1039. if (list->group == parent)
  1040. goto exist;
  1041. }
  1042. ret = -ENOENT;
  1043. goto out;
  1044. exist:
  1045. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1046. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1047. if (err && !ret)
  1048. ret = err;
  1049. spin_lock(&fs_info->qgroup_lock);
  1050. del_relation_rb(fs_info, src, dst);
  1051. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1052. spin_unlock(&fs_info->qgroup_lock);
  1053. out:
  1054. ulist_free(tmp);
  1055. return ret;
  1056. }
  1057. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1058. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1059. {
  1060. int ret = 0;
  1061. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1062. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1063. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1064. return ret;
  1065. }
  1066. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1067. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1068. {
  1069. struct btrfs_root *quota_root;
  1070. struct btrfs_qgroup *qgroup;
  1071. int ret = 0;
  1072. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1073. quota_root = fs_info->quota_root;
  1074. if (!quota_root) {
  1075. ret = -EINVAL;
  1076. goto out;
  1077. }
  1078. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1079. if (qgroup) {
  1080. ret = -EEXIST;
  1081. goto out;
  1082. }
  1083. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1084. if (ret)
  1085. goto out;
  1086. spin_lock(&fs_info->qgroup_lock);
  1087. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1088. spin_unlock(&fs_info->qgroup_lock);
  1089. if (IS_ERR(qgroup))
  1090. ret = PTR_ERR(qgroup);
  1091. out:
  1092. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1093. return ret;
  1094. }
  1095. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1096. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1097. {
  1098. struct btrfs_root *quota_root;
  1099. struct btrfs_qgroup *qgroup;
  1100. struct btrfs_qgroup_list *list;
  1101. int ret = 0;
  1102. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1103. quota_root = fs_info->quota_root;
  1104. if (!quota_root) {
  1105. ret = -EINVAL;
  1106. goto out;
  1107. }
  1108. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1109. if (!qgroup) {
  1110. ret = -ENOENT;
  1111. goto out;
  1112. } else {
  1113. /* check if there are no children of this qgroup */
  1114. if (!list_empty(&qgroup->members)) {
  1115. ret = -EBUSY;
  1116. goto out;
  1117. }
  1118. }
  1119. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1120. while (!list_empty(&qgroup->groups)) {
  1121. list = list_first_entry(&qgroup->groups,
  1122. struct btrfs_qgroup_list, next_group);
  1123. ret = __del_qgroup_relation(trans, fs_info,
  1124. qgroupid,
  1125. list->group->qgroupid);
  1126. if (ret)
  1127. goto out;
  1128. }
  1129. spin_lock(&fs_info->qgroup_lock);
  1130. del_qgroup_rb(fs_info, qgroupid);
  1131. spin_unlock(&fs_info->qgroup_lock);
  1132. out:
  1133. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1134. return ret;
  1135. }
  1136. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1137. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1138. struct btrfs_qgroup_limit *limit)
  1139. {
  1140. struct btrfs_root *quota_root;
  1141. struct btrfs_qgroup *qgroup;
  1142. int ret = 0;
  1143. /* Sometimes we would want to clear the limit on this qgroup.
  1144. * To meet this requirement, we treat the -1 as a special value
  1145. * which tell kernel to clear the limit on this qgroup.
  1146. */
  1147. const u64 CLEAR_VALUE = -1;
  1148. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1149. quota_root = fs_info->quota_root;
  1150. if (!quota_root) {
  1151. ret = -EINVAL;
  1152. goto out;
  1153. }
  1154. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1155. if (!qgroup) {
  1156. ret = -ENOENT;
  1157. goto out;
  1158. }
  1159. spin_lock(&fs_info->qgroup_lock);
  1160. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1161. if (limit->max_rfer == CLEAR_VALUE) {
  1162. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1163. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1164. qgroup->max_rfer = 0;
  1165. } else {
  1166. qgroup->max_rfer = limit->max_rfer;
  1167. }
  1168. }
  1169. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1170. if (limit->max_excl == CLEAR_VALUE) {
  1171. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1172. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1173. qgroup->max_excl = 0;
  1174. } else {
  1175. qgroup->max_excl = limit->max_excl;
  1176. }
  1177. }
  1178. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1179. if (limit->rsv_rfer == CLEAR_VALUE) {
  1180. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1181. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1182. qgroup->rsv_rfer = 0;
  1183. } else {
  1184. qgroup->rsv_rfer = limit->rsv_rfer;
  1185. }
  1186. }
  1187. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1188. if (limit->rsv_excl == CLEAR_VALUE) {
  1189. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1190. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1191. qgroup->rsv_excl = 0;
  1192. } else {
  1193. qgroup->rsv_excl = limit->rsv_excl;
  1194. }
  1195. }
  1196. qgroup->lim_flags |= limit->flags;
  1197. spin_unlock(&fs_info->qgroup_lock);
  1198. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1199. if (ret) {
  1200. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1201. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1202. qgroupid);
  1203. }
  1204. out:
  1205. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1206. return ret;
  1207. }
  1208. int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
  1209. struct btrfs_fs_info *fs_info)
  1210. {
  1211. struct btrfs_qgroup_extent_record *record;
  1212. struct btrfs_delayed_ref_root *delayed_refs;
  1213. struct rb_node *node;
  1214. u64 qgroup_to_skip;
  1215. int ret = 0;
  1216. delayed_refs = &trans->transaction->delayed_refs;
  1217. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1218. /*
  1219. * No need to do lock, since this function will only be called in
  1220. * btrfs_commit_transaction().
  1221. */
  1222. node = rb_first(&delayed_refs->dirty_extent_root);
  1223. while (node) {
  1224. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1225. node);
  1226. if (WARN_ON(!record->old_roots))
  1227. ret = btrfs_find_all_roots(NULL, fs_info,
  1228. record->bytenr, 0, &record->old_roots);
  1229. if (ret < 0)
  1230. break;
  1231. if (qgroup_to_skip)
  1232. ulist_del(record->old_roots, qgroup_to_skip, 0);
  1233. node = rb_next(node);
  1234. }
  1235. return ret;
  1236. }
  1237. int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
  1238. struct btrfs_delayed_ref_root *delayed_refs,
  1239. struct btrfs_qgroup_extent_record *record)
  1240. {
  1241. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1242. struct rb_node *parent_node = NULL;
  1243. struct btrfs_qgroup_extent_record *entry;
  1244. u64 bytenr = record->bytenr;
  1245. assert_spin_locked(&delayed_refs->lock);
  1246. trace_btrfs_qgroup_trace_extent(fs_info, record);
  1247. while (*p) {
  1248. parent_node = *p;
  1249. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1250. node);
  1251. if (bytenr < entry->bytenr)
  1252. p = &(*p)->rb_left;
  1253. else if (bytenr > entry->bytenr)
  1254. p = &(*p)->rb_right;
  1255. else
  1256. return 1;
  1257. }
  1258. rb_link_node(&record->node, parent_node, p);
  1259. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1260. return 0;
  1261. }
  1262. int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
  1263. struct btrfs_qgroup_extent_record *qrecord)
  1264. {
  1265. struct ulist *old_root;
  1266. u64 bytenr = qrecord->bytenr;
  1267. int ret;
  1268. ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
  1269. if (ret < 0)
  1270. return ret;
  1271. /*
  1272. * Here we don't need to get the lock of
  1273. * trans->transaction->delayed_refs, since inserted qrecord won't
  1274. * be deleted, only qrecord->node may be modified (new qrecord insert)
  1275. *
  1276. * So modifying qrecord->old_roots is safe here
  1277. */
  1278. qrecord->old_roots = old_root;
  1279. return 0;
  1280. }
  1281. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
  1282. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  1283. gfp_t gfp_flag)
  1284. {
  1285. struct btrfs_qgroup_extent_record *record;
  1286. struct btrfs_delayed_ref_root *delayed_refs;
  1287. int ret;
  1288. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
  1289. || bytenr == 0 || num_bytes == 0)
  1290. return 0;
  1291. if (WARN_ON(trans == NULL))
  1292. return -EINVAL;
  1293. record = kmalloc(sizeof(*record), gfp_flag);
  1294. if (!record)
  1295. return -ENOMEM;
  1296. delayed_refs = &trans->transaction->delayed_refs;
  1297. record->bytenr = bytenr;
  1298. record->num_bytes = num_bytes;
  1299. record->old_roots = NULL;
  1300. spin_lock(&delayed_refs->lock);
  1301. ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
  1302. spin_unlock(&delayed_refs->lock);
  1303. if (ret > 0) {
  1304. kfree(record);
  1305. return 0;
  1306. }
  1307. return btrfs_qgroup_trace_extent_post(fs_info, record);
  1308. }
  1309. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  1310. struct btrfs_fs_info *fs_info,
  1311. struct extent_buffer *eb)
  1312. {
  1313. int nr = btrfs_header_nritems(eb);
  1314. int i, extent_type, ret;
  1315. struct btrfs_key key;
  1316. struct btrfs_file_extent_item *fi;
  1317. u64 bytenr, num_bytes;
  1318. /* We can be called directly from walk_up_proc() */
  1319. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1320. return 0;
  1321. for (i = 0; i < nr; i++) {
  1322. btrfs_item_key_to_cpu(eb, &key, i);
  1323. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1324. continue;
  1325. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  1326. /* filter out non qgroup-accountable extents */
  1327. extent_type = btrfs_file_extent_type(eb, fi);
  1328. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1329. continue;
  1330. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  1331. if (!bytenr)
  1332. continue;
  1333. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  1334. ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
  1335. num_bytes, GFP_NOFS);
  1336. if (ret)
  1337. return ret;
  1338. }
  1339. return 0;
  1340. }
  1341. /*
  1342. * Walk up the tree from the bottom, freeing leaves and any interior
  1343. * nodes which have had all slots visited. If a node (leaf or
  1344. * interior) is freed, the node above it will have it's slot
  1345. * incremented. The root node will never be freed.
  1346. *
  1347. * At the end of this function, we should have a path which has all
  1348. * slots incremented to the next position for a search. If we need to
  1349. * read a new node it will be NULL and the node above it will have the
  1350. * correct slot selected for a later read.
  1351. *
  1352. * If we increment the root nodes slot counter past the number of
  1353. * elements, 1 is returned to signal completion of the search.
  1354. */
  1355. static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
  1356. {
  1357. int level = 0;
  1358. int nr, slot;
  1359. struct extent_buffer *eb;
  1360. if (root_level == 0)
  1361. return 1;
  1362. while (level <= root_level) {
  1363. eb = path->nodes[level];
  1364. nr = btrfs_header_nritems(eb);
  1365. path->slots[level]++;
  1366. slot = path->slots[level];
  1367. if (slot >= nr || level == 0) {
  1368. /*
  1369. * Don't free the root - we will detect this
  1370. * condition after our loop and return a
  1371. * positive value for caller to stop walking the tree.
  1372. */
  1373. if (level != root_level) {
  1374. btrfs_tree_unlock_rw(eb, path->locks[level]);
  1375. path->locks[level] = 0;
  1376. free_extent_buffer(eb);
  1377. path->nodes[level] = NULL;
  1378. path->slots[level] = 0;
  1379. }
  1380. } else {
  1381. /*
  1382. * We have a valid slot to walk back down
  1383. * from. Stop here so caller can process these
  1384. * new nodes.
  1385. */
  1386. break;
  1387. }
  1388. level++;
  1389. }
  1390. eb = path->nodes[root_level];
  1391. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  1392. return 1;
  1393. return 0;
  1394. }
  1395. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  1396. struct btrfs_root *root,
  1397. struct extent_buffer *root_eb,
  1398. u64 root_gen, int root_level)
  1399. {
  1400. struct btrfs_fs_info *fs_info = root->fs_info;
  1401. int ret = 0;
  1402. int level;
  1403. struct extent_buffer *eb = root_eb;
  1404. struct btrfs_path *path = NULL;
  1405. BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
  1406. BUG_ON(root_eb == NULL);
  1407. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1408. return 0;
  1409. if (!extent_buffer_uptodate(root_eb)) {
  1410. ret = btrfs_read_buffer(root_eb, root_gen);
  1411. if (ret)
  1412. goto out;
  1413. }
  1414. if (root_level == 0) {
  1415. ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
  1416. goto out;
  1417. }
  1418. path = btrfs_alloc_path();
  1419. if (!path)
  1420. return -ENOMEM;
  1421. /*
  1422. * Walk down the tree. Missing extent blocks are filled in as
  1423. * we go. Metadata is accounted every time we read a new
  1424. * extent block.
  1425. *
  1426. * When we reach a leaf, we account for file extent items in it,
  1427. * walk back up the tree (adjusting slot pointers as we go)
  1428. * and restart the search process.
  1429. */
  1430. extent_buffer_get(root_eb); /* For path */
  1431. path->nodes[root_level] = root_eb;
  1432. path->slots[root_level] = 0;
  1433. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  1434. walk_down:
  1435. level = root_level;
  1436. while (level >= 0) {
  1437. if (path->nodes[level] == NULL) {
  1438. int parent_slot;
  1439. u64 child_gen;
  1440. u64 child_bytenr;
  1441. /*
  1442. * We need to get child blockptr/gen from parent before
  1443. * we can read it.
  1444. */
  1445. eb = path->nodes[level + 1];
  1446. parent_slot = path->slots[level + 1];
  1447. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  1448. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  1449. eb = read_tree_block(fs_info, child_bytenr, child_gen);
  1450. if (IS_ERR(eb)) {
  1451. ret = PTR_ERR(eb);
  1452. goto out;
  1453. } else if (!extent_buffer_uptodate(eb)) {
  1454. free_extent_buffer(eb);
  1455. ret = -EIO;
  1456. goto out;
  1457. }
  1458. path->nodes[level] = eb;
  1459. path->slots[level] = 0;
  1460. btrfs_tree_read_lock(eb);
  1461. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1462. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  1463. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  1464. child_bytenr,
  1465. fs_info->nodesize,
  1466. GFP_NOFS);
  1467. if (ret)
  1468. goto out;
  1469. }
  1470. if (level == 0) {
  1471. ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
  1472. path->nodes[level]);
  1473. if (ret)
  1474. goto out;
  1475. /* Nonzero return here means we completed our search */
  1476. ret = adjust_slots_upwards(path, root_level);
  1477. if (ret)
  1478. break;
  1479. /* Restart search with new slots */
  1480. goto walk_down;
  1481. }
  1482. level--;
  1483. }
  1484. ret = 0;
  1485. out:
  1486. btrfs_free_path(path);
  1487. return ret;
  1488. }
  1489. #define UPDATE_NEW 0
  1490. #define UPDATE_OLD 1
  1491. /*
  1492. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1493. */
  1494. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1495. struct ulist *roots, struct ulist *tmp,
  1496. struct ulist *qgroups, u64 seq, int update_old)
  1497. {
  1498. struct ulist_node *unode;
  1499. struct ulist_iterator uiter;
  1500. struct ulist_node *tmp_unode;
  1501. struct ulist_iterator tmp_uiter;
  1502. struct btrfs_qgroup *qg;
  1503. int ret = 0;
  1504. if (!roots)
  1505. return 0;
  1506. ULIST_ITER_INIT(&uiter);
  1507. while ((unode = ulist_next(roots, &uiter))) {
  1508. qg = find_qgroup_rb(fs_info, unode->val);
  1509. if (!qg)
  1510. continue;
  1511. ulist_reinit(tmp);
  1512. ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
  1513. GFP_ATOMIC);
  1514. if (ret < 0)
  1515. return ret;
  1516. ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
  1517. if (ret < 0)
  1518. return ret;
  1519. ULIST_ITER_INIT(&tmp_uiter);
  1520. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1521. struct btrfs_qgroup_list *glist;
  1522. qg = unode_aux_to_qgroup(tmp_unode);
  1523. if (update_old)
  1524. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1525. else
  1526. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1527. list_for_each_entry(glist, &qg->groups, next_group) {
  1528. ret = ulist_add(qgroups, glist->group->qgroupid,
  1529. qgroup_to_aux(glist->group),
  1530. GFP_ATOMIC);
  1531. if (ret < 0)
  1532. return ret;
  1533. ret = ulist_add(tmp, glist->group->qgroupid,
  1534. qgroup_to_aux(glist->group),
  1535. GFP_ATOMIC);
  1536. if (ret < 0)
  1537. return ret;
  1538. }
  1539. }
  1540. }
  1541. return 0;
  1542. }
  1543. /*
  1544. * Update qgroup rfer/excl counters.
  1545. * Rfer update is easy, codes can explain themselves.
  1546. *
  1547. * Excl update is tricky, the update is split into 2 part.
  1548. * Part 1: Possible exclusive <-> sharing detect:
  1549. * | A | !A |
  1550. * -------------------------------------
  1551. * B | * | - |
  1552. * -------------------------------------
  1553. * !B | + | ** |
  1554. * -------------------------------------
  1555. *
  1556. * Conditions:
  1557. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1558. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1559. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1560. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  1561. *
  1562. * Results:
  1563. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1564. * *: Definitely not changed. **: Possible unchanged.
  1565. *
  1566. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1567. *
  1568. * To make the logic clear, we first use condition A and B to split
  1569. * combination into 4 results.
  1570. *
  1571. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1572. * only on variant maybe 0.
  1573. *
  1574. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1575. * again(2x2).
  1576. * But this time we don't need to consider other things, the codes and logic
  1577. * is easy to understand now.
  1578. */
  1579. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1580. struct ulist *qgroups,
  1581. u64 nr_old_roots,
  1582. u64 nr_new_roots,
  1583. u64 num_bytes, u64 seq)
  1584. {
  1585. struct ulist_node *unode;
  1586. struct ulist_iterator uiter;
  1587. struct btrfs_qgroup *qg;
  1588. u64 cur_new_count, cur_old_count;
  1589. ULIST_ITER_INIT(&uiter);
  1590. while ((unode = ulist_next(qgroups, &uiter))) {
  1591. bool dirty = false;
  1592. qg = unode_aux_to_qgroup(unode);
  1593. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1594. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1595. trace_qgroup_update_counters(fs_info, qg->qgroupid,
  1596. cur_old_count, cur_new_count);
  1597. /* Rfer update part */
  1598. if (cur_old_count == 0 && cur_new_count > 0) {
  1599. qg->rfer += num_bytes;
  1600. qg->rfer_cmpr += num_bytes;
  1601. dirty = true;
  1602. }
  1603. if (cur_old_count > 0 && cur_new_count == 0) {
  1604. qg->rfer -= num_bytes;
  1605. qg->rfer_cmpr -= num_bytes;
  1606. dirty = true;
  1607. }
  1608. /* Excl update part */
  1609. /* Exclusive/none -> shared case */
  1610. if (cur_old_count == nr_old_roots &&
  1611. cur_new_count < nr_new_roots) {
  1612. /* Exclusive -> shared */
  1613. if (cur_old_count != 0) {
  1614. qg->excl -= num_bytes;
  1615. qg->excl_cmpr -= num_bytes;
  1616. dirty = true;
  1617. }
  1618. }
  1619. /* Shared -> exclusive/none case */
  1620. if (cur_old_count < nr_old_roots &&
  1621. cur_new_count == nr_new_roots) {
  1622. /* Shared->exclusive */
  1623. if (cur_new_count != 0) {
  1624. qg->excl += num_bytes;
  1625. qg->excl_cmpr += num_bytes;
  1626. dirty = true;
  1627. }
  1628. }
  1629. /* Exclusive/none -> exclusive/none case */
  1630. if (cur_old_count == nr_old_roots &&
  1631. cur_new_count == nr_new_roots) {
  1632. if (cur_old_count == 0) {
  1633. /* None -> exclusive/none */
  1634. if (cur_new_count != 0) {
  1635. /* None -> exclusive */
  1636. qg->excl += num_bytes;
  1637. qg->excl_cmpr += num_bytes;
  1638. dirty = true;
  1639. }
  1640. /* None -> none, nothing changed */
  1641. } else {
  1642. /* Exclusive -> exclusive/none */
  1643. if (cur_new_count == 0) {
  1644. /* Exclusive -> none */
  1645. qg->excl -= num_bytes;
  1646. qg->excl_cmpr -= num_bytes;
  1647. dirty = true;
  1648. }
  1649. /* Exclusive -> exclusive, nothing changed */
  1650. }
  1651. }
  1652. if (dirty)
  1653. qgroup_dirty(fs_info, qg);
  1654. }
  1655. return 0;
  1656. }
  1657. int
  1658. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1659. struct btrfs_fs_info *fs_info,
  1660. u64 bytenr, u64 num_bytes,
  1661. struct ulist *old_roots, struct ulist *new_roots)
  1662. {
  1663. struct ulist *qgroups = NULL;
  1664. struct ulist *tmp = NULL;
  1665. u64 seq;
  1666. u64 nr_new_roots = 0;
  1667. u64 nr_old_roots = 0;
  1668. int ret = 0;
  1669. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1670. return 0;
  1671. if (new_roots)
  1672. nr_new_roots = new_roots->nnodes;
  1673. if (old_roots)
  1674. nr_old_roots = old_roots->nnodes;
  1675. BUG_ON(!fs_info->quota_root);
  1676. trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
  1677. nr_old_roots, nr_new_roots);
  1678. qgroups = ulist_alloc(GFP_NOFS);
  1679. if (!qgroups) {
  1680. ret = -ENOMEM;
  1681. goto out_free;
  1682. }
  1683. tmp = ulist_alloc(GFP_NOFS);
  1684. if (!tmp) {
  1685. ret = -ENOMEM;
  1686. goto out_free;
  1687. }
  1688. mutex_lock(&fs_info->qgroup_rescan_lock);
  1689. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1690. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1691. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1692. ret = 0;
  1693. goto out_free;
  1694. }
  1695. }
  1696. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1697. spin_lock(&fs_info->qgroup_lock);
  1698. seq = fs_info->qgroup_seq;
  1699. /* Update old refcnts using old_roots */
  1700. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1701. UPDATE_OLD);
  1702. if (ret < 0)
  1703. goto out;
  1704. /* Update new refcnts using new_roots */
  1705. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1706. UPDATE_NEW);
  1707. if (ret < 0)
  1708. goto out;
  1709. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1710. num_bytes, seq);
  1711. /*
  1712. * Bump qgroup_seq to avoid seq overlap
  1713. */
  1714. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1715. out:
  1716. spin_unlock(&fs_info->qgroup_lock);
  1717. out_free:
  1718. ulist_free(tmp);
  1719. ulist_free(qgroups);
  1720. ulist_free(old_roots);
  1721. ulist_free(new_roots);
  1722. return ret;
  1723. }
  1724. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1725. struct btrfs_fs_info *fs_info)
  1726. {
  1727. struct btrfs_qgroup_extent_record *record;
  1728. struct btrfs_delayed_ref_root *delayed_refs;
  1729. struct ulist *new_roots = NULL;
  1730. struct rb_node *node;
  1731. u64 qgroup_to_skip;
  1732. int ret = 0;
  1733. delayed_refs = &trans->transaction->delayed_refs;
  1734. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1735. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1736. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1737. node);
  1738. trace_btrfs_qgroup_account_extents(fs_info, record);
  1739. if (!ret) {
  1740. /*
  1741. * Use SEQ_LAST as time_seq to do special search, which
  1742. * doesn't lock tree or delayed_refs and search current
  1743. * root. It's safe inside commit_transaction().
  1744. */
  1745. ret = btrfs_find_all_roots(trans, fs_info,
  1746. record->bytenr, SEQ_LAST, &new_roots);
  1747. if (ret < 0)
  1748. goto cleanup;
  1749. if (qgroup_to_skip)
  1750. ulist_del(new_roots, qgroup_to_skip, 0);
  1751. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1752. record->bytenr, record->num_bytes,
  1753. record->old_roots, new_roots);
  1754. record->old_roots = NULL;
  1755. new_roots = NULL;
  1756. }
  1757. cleanup:
  1758. ulist_free(record->old_roots);
  1759. ulist_free(new_roots);
  1760. new_roots = NULL;
  1761. rb_erase(node, &delayed_refs->dirty_extent_root);
  1762. kfree(record);
  1763. }
  1764. return ret;
  1765. }
  1766. /*
  1767. * called from commit_transaction. Writes all changed qgroups to disk.
  1768. */
  1769. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1770. struct btrfs_fs_info *fs_info)
  1771. {
  1772. struct btrfs_root *quota_root = fs_info->quota_root;
  1773. int ret = 0;
  1774. int start_rescan_worker = 0;
  1775. if (!quota_root)
  1776. goto out;
  1777. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
  1778. test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1779. start_rescan_worker = 1;
  1780. if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1781. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1782. if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
  1783. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1784. spin_lock(&fs_info->qgroup_lock);
  1785. while (!list_empty(&fs_info->dirty_qgroups)) {
  1786. struct btrfs_qgroup *qgroup;
  1787. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1788. struct btrfs_qgroup, dirty);
  1789. list_del_init(&qgroup->dirty);
  1790. spin_unlock(&fs_info->qgroup_lock);
  1791. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1792. if (ret)
  1793. fs_info->qgroup_flags |=
  1794. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1795. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1796. if (ret)
  1797. fs_info->qgroup_flags |=
  1798. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1799. spin_lock(&fs_info->qgroup_lock);
  1800. }
  1801. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1802. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1803. else
  1804. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1805. spin_unlock(&fs_info->qgroup_lock);
  1806. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1807. if (ret)
  1808. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1809. if (!ret && start_rescan_worker) {
  1810. ret = qgroup_rescan_init(fs_info, 0, 1);
  1811. if (!ret) {
  1812. qgroup_rescan_zero_tracking(fs_info);
  1813. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1814. &fs_info->qgroup_rescan_work);
  1815. }
  1816. ret = 0;
  1817. }
  1818. out:
  1819. return ret;
  1820. }
  1821. /*
  1822. * Copy the accounting information between qgroups. This is necessary
  1823. * when a snapshot or a subvolume is created. Throwing an error will
  1824. * cause a transaction abort so we take extra care here to only error
  1825. * when a readonly fs is a reasonable outcome.
  1826. */
  1827. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1828. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1829. struct btrfs_qgroup_inherit *inherit)
  1830. {
  1831. int ret = 0;
  1832. int i;
  1833. u64 *i_qgroups;
  1834. struct btrfs_root *quota_root = fs_info->quota_root;
  1835. struct btrfs_qgroup *srcgroup;
  1836. struct btrfs_qgroup *dstgroup;
  1837. u32 level_size = 0;
  1838. u64 nums;
  1839. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1840. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1841. goto out;
  1842. if (!quota_root) {
  1843. ret = -EINVAL;
  1844. goto out;
  1845. }
  1846. if (inherit) {
  1847. i_qgroups = (u64 *)(inherit + 1);
  1848. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1849. 2 * inherit->num_excl_copies;
  1850. for (i = 0; i < nums; ++i) {
  1851. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1852. /*
  1853. * Zero out invalid groups so we can ignore
  1854. * them later.
  1855. */
  1856. if (!srcgroup ||
  1857. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  1858. *i_qgroups = 0ULL;
  1859. ++i_qgroups;
  1860. }
  1861. }
  1862. /*
  1863. * create a tracking group for the subvol itself
  1864. */
  1865. ret = add_qgroup_item(trans, quota_root, objectid);
  1866. if (ret)
  1867. goto out;
  1868. if (srcid) {
  1869. struct btrfs_root *srcroot;
  1870. struct btrfs_key srckey;
  1871. srckey.objectid = srcid;
  1872. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1873. srckey.offset = (u64)-1;
  1874. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1875. if (IS_ERR(srcroot)) {
  1876. ret = PTR_ERR(srcroot);
  1877. goto out;
  1878. }
  1879. level_size = fs_info->nodesize;
  1880. }
  1881. /*
  1882. * add qgroup to all inherited groups
  1883. */
  1884. if (inherit) {
  1885. i_qgroups = (u64 *)(inherit + 1);
  1886. for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
  1887. if (*i_qgroups == 0)
  1888. continue;
  1889. ret = add_qgroup_relation_item(trans, quota_root,
  1890. objectid, *i_qgroups);
  1891. if (ret && ret != -EEXIST)
  1892. goto out;
  1893. ret = add_qgroup_relation_item(trans, quota_root,
  1894. *i_qgroups, objectid);
  1895. if (ret && ret != -EEXIST)
  1896. goto out;
  1897. }
  1898. ret = 0;
  1899. }
  1900. spin_lock(&fs_info->qgroup_lock);
  1901. dstgroup = add_qgroup_rb(fs_info, objectid);
  1902. if (IS_ERR(dstgroup)) {
  1903. ret = PTR_ERR(dstgroup);
  1904. goto unlock;
  1905. }
  1906. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1907. dstgroup->lim_flags = inherit->lim.flags;
  1908. dstgroup->max_rfer = inherit->lim.max_rfer;
  1909. dstgroup->max_excl = inherit->lim.max_excl;
  1910. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1911. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1912. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1913. if (ret) {
  1914. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1915. btrfs_info(fs_info,
  1916. "unable to update quota limit for %llu",
  1917. dstgroup->qgroupid);
  1918. goto unlock;
  1919. }
  1920. }
  1921. if (srcid) {
  1922. srcgroup = find_qgroup_rb(fs_info, srcid);
  1923. if (!srcgroup)
  1924. goto unlock;
  1925. /*
  1926. * We call inherit after we clone the root in order to make sure
  1927. * our counts don't go crazy, so at this point the only
  1928. * difference between the two roots should be the root node.
  1929. */
  1930. dstgroup->rfer = srcgroup->rfer;
  1931. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1932. dstgroup->excl = level_size;
  1933. dstgroup->excl_cmpr = level_size;
  1934. srcgroup->excl = level_size;
  1935. srcgroup->excl_cmpr = level_size;
  1936. /* inherit the limit info */
  1937. dstgroup->lim_flags = srcgroup->lim_flags;
  1938. dstgroup->max_rfer = srcgroup->max_rfer;
  1939. dstgroup->max_excl = srcgroup->max_excl;
  1940. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1941. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1942. qgroup_dirty(fs_info, dstgroup);
  1943. qgroup_dirty(fs_info, srcgroup);
  1944. }
  1945. if (!inherit)
  1946. goto unlock;
  1947. i_qgroups = (u64 *)(inherit + 1);
  1948. for (i = 0; i < inherit->num_qgroups; ++i) {
  1949. if (*i_qgroups) {
  1950. ret = add_relation_rb(fs_info, objectid, *i_qgroups);
  1951. if (ret)
  1952. goto unlock;
  1953. }
  1954. ++i_qgroups;
  1955. }
  1956. for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
  1957. struct btrfs_qgroup *src;
  1958. struct btrfs_qgroup *dst;
  1959. if (!i_qgroups[0] || !i_qgroups[1])
  1960. continue;
  1961. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1962. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1963. if (!src || !dst) {
  1964. ret = -EINVAL;
  1965. goto unlock;
  1966. }
  1967. dst->rfer = src->rfer - level_size;
  1968. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1969. }
  1970. for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
  1971. struct btrfs_qgroup *src;
  1972. struct btrfs_qgroup *dst;
  1973. if (!i_qgroups[0] || !i_qgroups[1])
  1974. continue;
  1975. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1976. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1977. if (!src || !dst) {
  1978. ret = -EINVAL;
  1979. goto unlock;
  1980. }
  1981. dst->excl = src->excl + level_size;
  1982. dst->excl_cmpr = src->excl_cmpr + level_size;
  1983. }
  1984. unlock:
  1985. spin_unlock(&fs_info->qgroup_lock);
  1986. out:
  1987. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1988. return ret;
  1989. }
  1990. static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
  1991. {
  1992. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1993. qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
  1994. return false;
  1995. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1996. qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
  1997. return false;
  1998. return true;
  1999. }
  2000. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
  2001. {
  2002. struct btrfs_root *quota_root;
  2003. struct btrfs_qgroup *qgroup;
  2004. struct btrfs_fs_info *fs_info = root->fs_info;
  2005. u64 ref_root = root->root_key.objectid;
  2006. int ret = 0;
  2007. int retried = 0;
  2008. struct ulist_node *unode;
  2009. struct ulist_iterator uiter;
  2010. if (!is_fstree(ref_root))
  2011. return 0;
  2012. if (num_bytes == 0)
  2013. return 0;
  2014. retry:
  2015. spin_lock(&fs_info->qgroup_lock);
  2016. quota_root = fs_info->quota_root;
  2017. if (!quota_root)
  2018. goto out;
  2019. qgroup = find_qgroup_rb(fs_info, ref_root);
  2020. if (!qgroup)
  2021. goto out;
  2022. /*
  2023. * in a first step, we check all affected qgroups if any limits would
  2024. * be exceeded
  2025. */
  2026. ulist_reinit(fs_info->qgroup_ulist);
  2027. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2028. (uintptr_t)qgroup, GFP_ATOMIC);
  2029. if (ret < 0)
  2030. goto out;
  2031. ULIST_ITER_INIT(&uiter);
  2032. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2033. struct btrfs_qgroup *qg;
  2034. struct btrfs_qgroup_list *glist;
  2035. qg = unode_aux_to_qgroup(unode);
  2036. if (enforce && !qgroup_check_limits(qg, num_bytes)) {
  2037. /*
  2038. * Commit the tree and retry, since we may have
  2039. * deletions which would free up space.
  2040. */
  2041. if (!retried && qg->reserved > 0) {
  2042. struct btrfs_trans_handle *trans;
  2043. spin_unlock(&fs_info->qgroup_lock);
  2044. ret = btrfs_start_delalloc_inodes(root, 0);
  2045. if (ret)
  2046. return ret;
  2047. btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
  2048. trans = btrfs_join_transaction(root);
  2049. if (IS_ERR(trans))
  2050. return PTR_ERR(trans);
  2051. ret = btrfs_commit_transaction(trans);
  2052. if (ret)
  2053. return ret;
  2054. retried++;
  2055. goto retry;
  2056. }
  2057. ret = -EDQUOT;
  2058. goto out;
  2059. }
  2060. list_for_each_entry(glist, &qg->groups, next_group) {
  2061. ret = ulist_add(fs_info->qgroup_ulist,
  2062. glist->group->qgroupid,
  2063. (uintptr_t)glist->group, GFP_ATOMIC);
  2064. if (ret < 0)
  2065. goto out;
  2066. }
  2067. }
  2068. ret = 0;
  2069. /*
  2070. * no limits exceeded, now record the reservation into all qgroups
  2071. */
  2072. ULIST_ITER_INIT(&uiter);
  2073. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2074. struct btrfs_qgroup *qg;
  2075. qg = unode_aux_to_qgroup(unode);
  2076. trace_qgroup_update_reserve(fs_info, qg, num_bytes);
  2077. qg->reserved += num_bytes;
  2078. }
  2079. out:
  2080. spin_unlock(&fs_info->qgroup_lock);
  2081. return ret;
  2082. }
  2083. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  2084. u64 ref_root, u64 num_bytes)
  2085. {
  2086. struct btrfs_root *quota_root;
  2087. struct btrfs_qgroup *qgroup;
  2088. struct ulist_node *unode;
  2089. struct ulist_iterator uiter;
  2090. int ret = 0;
  2091. if (!is_fstree(ref_root))
  2092. return;
  2093. if (num_bytes == 0)
  2094. return;
  2095. spin_lock(&fs_info->qgroup_lock);
  2096. quota_root = fs_info->quota_root;
  2097. if (!quota_root)
  2098. goto out;
  2099. qgroup = find_qgroup_rb(fs_info, ref_root);
  2100. if (!qgroup)
  2101. goto out;
  2102. ulist_reinit(fs_info->qgroup_ulist);
  2103. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2104. (uintptr_t)qgroup, GFP_ATOMIC);
  2105. if (ret < 0)
  2106. goto out;
  2107. ULIST_ITER_INIT(&uiter);
  2108. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2109. struct btrfs_qgroup *qg;
  2110. struct btrfs_qgroup_list *glist;
  2111. qg = unode_aux_to_qgroup(unode);
  2112. trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
  2113. if (WARN_ON(qg->reserved < num_bytes))
  2114. report_reserved_underflow(fs_info, qg, num_bytes);
  2115. else
  2116. qg->reserved -= num_bytes;
  2117. list_for_each_entry(glist, &qg->groups, next_group) {
  2118. ret = ulist_add(fs_info->qgroup_ulist,
  2119. glist->group->qgroupid,
  2120. (uintptr_t)glist->group, GFP_ATOMIC);
  2121. if (ret < 0)
  2122. goto out;
  2123. }
  2124. }
  2125. out:
  2126. spin_unlock(&fs_info->qgroup_lock);
  2127. }
  2128. /*
  2129. * returns < 0 on error, 0 when more leafs are to be scanned.
  2130. * returns 1 when done.
  2131. */
  2132. static int
  2133. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2134. struct btrfs_trans_handle *trans)
  2135. {
  2136. struct btrfs_key found;
  2137. struct extent_buffer *scratch_leaf = NULL;
  2138. struct ulist *roots = NULL;
  2139. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  2140. u64 num_bytes;
  2141. int slot;
  2142. int ret;
  2143. mutex_lock(&fs_info->qgroup_rescan_lock);
  2144. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2145. &fs_info->qgroup_rescan_progress,
  2146. path, 1, 0);
  2147. btrfs_debug(fs_info,
  2148. "current progress key (%llu %u %llu), search_slot ret %d",
  2149. fs_info->qgroup_rescan_progress.objectid,
  2150. fs_info->qgroup_rescan_progress.type,
  2151. fs_info->qgroup_rescan_progress.offset, ret);
  2152. if (ret) {
  2153. /*
  2154. * The rescan is about to end, we will not be scanning any
  2155. * further blocks. We cannot unset the RESCAN flag here, because
  2156. * we want to commit the transaction if everything went well.
  2157. * To make the live accounting work in this phase, we set our
  2158. * scan progress pointer such that every real extent objectid
  2159. * will be smaller.
  2160. */
  2161. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2162. btrfs_release_path(path);
  2163. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2164. return ret;
  2165. }
  2166. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2167. btrfs_header_nritems(path->nodes[0]) - 1);
  2168. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2169. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2170. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  2171. if (!scratch_leaf) {
  2172. ret = -ENOMEM;
  2173. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2174. goto out;
  2175. }
  2176. extent_buffer_get(scratch_leaf);
  2177. btrfs_tree_read_lock(scratch_leaf);
  2178. btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
  2179. slot = path->slots[0];
  2180. btrfs_release_path(path);
  2181. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2182. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2183. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2184. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2185. found.type != BTRFS_METADATA_ITEM_KEY)
  2186. continue;
  2187. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2188. num_bytes = fs_info->nodesize;
  2189. else
  2190. num_bytes = found.offset;
  2191. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2192. &roots);
  2193. if (ret < 0)
  2194. goto out;
  2195. /* For rescan, just pass old_roots as NULL */
  2196. ret = btrfs_qgroup_account_extent(trans, fs_info,
  2197. found.objectid, num_bytes, NULL, roots);
  2198. if (ret < 0)
  2199. goto out;
  2200. }
  2201. out:
  2202. if (scratch_leaf) {
  2203. btrfs_tree_read_unlock_blocking(scratch_leaf);
  2204. free_extent_buffer(scratch_leaf);
  2205. }
  2206. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2207. return ret;
  2208. }
  2209. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2210. {
  2211. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2212. qgroup_rescan_work);
  2213. struct btrfs_path *path;
  2214. struct btrfs_trans_handle *trans = NULL;
  2215. int err = -ENOMEM;
  2216. int ret = 0;
  2217. path = btrfs_alloc_path();
  2218. if (!path)
  2219. goto out;
  2220. err = 0;
  2221. while (!err && !btrfs_fs_closing(fs_info)) {
  2222. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2223. if (IS_ERR(trans)) {
  2224. err = PTR_ERR(trans);
  2225. break;
  2226. }
  2227. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  2228. err = -EINTR;
  2229. } else {
  2230. err = qgroup_rescan_leaf(fs_info, path, trans);
  2231. }
  2232. if (err > 0)
  2233. btrfs_commit_transaction(trans);
  2234. else
  2235. btrfs_end_transaction(trans);
  2236. }
  2237. out:
  2238. btrfs_free_path(path);
  2239. mutex_lock(&fs_info->qgroup_rescan_lock);
  2240. if (!btrfs_fs_closing(fs_info))
  2241. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2242. if (err > 0 &&
  2243. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2244. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2245. } else if (err < 0) {
  2246. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2247. }
  2248. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2249. /*
  2250. * only update status, since the previous part has already updated the
  2251. * qgroup info.
  2252. */
  2253. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2254. if (IS_ERR(trans)) {
  2255. err = PTR_ERR(trans);
  2256. btrfs_err(fs_info,
  2257. "fail to start transaction for status update: %d\n",
  2258. err);
  2259. goto done;
  2260. }
  2261. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2262. if (ret < 0) {
  2263. err = ret;
  2264. btrfs_err(fs_info, "fail to update qgroup status: %d", err);
  2265. }
  2266. btrfs_end_transaction(trans);
  2267. if (btrfs_fs_closing(fs_info)) {
  2268. btrfs_info(fs_info, "qgroup scan paused");
  2269. } else if (err >= 0) {
  2270. btrfs_info(fs_info, "qgroup scan completed%s",
  2271. err > 0 ? " (inconsistency flag cleared)" : "");
  2272. } else {
  2273. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2274. }
  2275. done:
  2276. mutex_lock(&fs_info->qgroup_rescan_lock);
  2277. fs_info->qgroup_rescan_running = false;
  2278. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2279. complete_all(&fs_info->qgroup_rescan_completion);
  2280. }
  2281. /*
  2282. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2283. * memory required for the rescan context.
  2284. */
  2285. static int
  2286. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2287. int init_flags)
  2288. {
  2289. int ret = 0;
  2290. if (!init_flags &&
  2291. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2292. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2293. ret = -EINVAL;
  2294. goto err;
  2295. }
  2296. mutex_lock(&fs_info->qgroup_rescan_lock);
  2297. spin_lock(&fs_info->qgroup_lock);
  2298. if (init_flags) {
  2299. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2300. ret = -EINPROGRESS;
  2301. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2302. ret = -EINVAL;
  2303. if (ret) {
  2304. spin_unlock(&fs_info->qgroup_lock);
  2305. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2306. goto err;
  2307. }
  2308. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2309. }
  2310. memset(&fs_info->qgroup_rescan_progress, 0,
  2311. sizeof(fs_info->qgroup_rescan_progress));
  2312. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2313. init_completion(&fs_info->qgroup_rescan_completion);
  2314. fs_info->qgroup_rescan_running = true;
  2315. spin_unlock(&fs_info->qgroup_lock);
  2316. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2317. memset(&fs_info->qgroup_rescan_work, 0,
  2318. sizeof(fs_info->qgroup_rescan_work));
  2319. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2320. btrfs_qgroup_rescan_helper,
  2321. btrfs_qgroup_rescan_worker, NULL, NULL);
  2322. if (ret) {
  2323. err:
  2324. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2325. return ret;
  2326. }
  2327. return 0;
  2328. }
  2329. static void
  2330. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2331. {
  2332. struct rb_node *n;
  2333. struct btrfs_qgroup *qgroup;
  2334. spin_lock(&fs_info->qgroup_lock);
  2335. /* clear all current qgroup tracking information */
  2336. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2337. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2338. qgroup->rfer = 0;
  2339. qgroup->rfer_cmpr = 0;
  2340. qgroup->excl = 0;
  2341. qgroup->excl_cmpr = 0;
  2342. }
  2343. spin_unlock(&fs_info->qgroup_lock);
  2344. }
  2345. int
  2346. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2347. {
  2348. int ret = 0;
  2349. struct btrfs_trans_handle *trans;
  2350. ret = qgroup_rescan_init(fs_info, 0, 1);
  2351. if (ret)
  2352. return ret;
  2353. /*
  2354. * We have set the rescan_progress to 0, which means no more
  2355. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2356. * However, btrfs_qgroup_account_ref may be right after its call
  2357. * to btrfs_find_all_roots, in which case it would still do the
  2358. * accounting.
  2359. * To solve this, we're committing the transaction, which will
  2360. * ensure we run all delayed refs and only after that, we are
  2361. * going to clear all tracking information for a clean start.
  2362. */
  2363. trans = btrfs_join_transaction(fs_info->fs_root);
  2364. if (IS_ERR(trans)) {
  2365. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2366. return PTR_ERR(trans);
  2367. }
  2368. ret = btrfs_commit_transaction(trans);
  2369. if (ret) {
  2370. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2371. return ret;
  2372. }
  2373. qgroup_rescan_zero_tracking(fs_info);
  2374. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2375. &fs_info->qgroup_rescan_work);
  2376. return 0;
  2377. }
  2378. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  2379. bool interruptible)
  2380. {
  2381. int running;
  2382. int ret = 0;
  2383. mutex_lock(&fs_info->qgroup_rescan_lock);
  2384. spin_lock(&fs_info->qgroup_lock);
  2385. running = fs_info->qgroup_rescan_running;
  2386. spin_unlock(&fs_info->qgroup_lock);
  2387. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2388. if (!running)
  2389. return 0;
  2390. if (interruptible)
  2391. ret = wait_for_completion_interruptible(
  2392. &fs_info->qgroup_rescan_completion);
  2393. else
  2394. wait_for_completion(&fs_info->qgroup_rescan_completion);
  2395. return ret;
  2396. }
  2397. /*
  2398. * this is only called from open_ctree where we're still single threaded, thus
  2399. * locking is omitted here.
  2400. */
  2401. void
  2402. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2403. {
  2404. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2405. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2406. &fs_info->qgroup_rescan_work);
  2407. }
  2408. /*
  2409. * Reserve qgroup space for range [start, start + len).
  2410. *
  2411. * This function will either reserve space from related qgroups or doing
  2412. * nothing if the range is already reserved.
  2413. *
  2414. * Return 0 for successful reserve
  2415. * Return <0 for error (including -EQUOT)
  2416. *
  2417. * NOTE: this function may sleep for memory allocation.
  2418. */
  2419. int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
  2420. {
  2421. struct btrfs_root *root = BTRFS_I(inode)->root;
  2422. struct extent_changeset changeset;
  2423. struct ulist_node *unode;
  2424. struct ulist_iterator uiter;
  2425. int ret;
  2426. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2427. !is_fstree(root->objectid) || len == 0)
  2428. return 0;
  2429. changeset.bytes_changed = 0;
  2430. ulist_init(&changeset.range_changed);
  2431. ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2432. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2433. trace_btrfs_qgroup_reserve_data(inode, start, len,
  2434. changeset.bytes_changed,
  2435. QGROUP_RESERVE);
  2436. if (ret < 0)
  2437. goto cleanup;
  2438. ret = qgroup_reserve(root, changeset.bytes_changed, true);
  2439. if (ret < 0)
  2440. goto cleanup;
  2441. ulist_release(&changeset.range_changed);
  2442. return ret;
  2443. cleanup:
  2444. /* cleanup already reserved ranges */
  2445. ULIST_ITER_INIT(&uiter);
  2446. while ((unode = ulist_next(&changeset.range_changed, &uiter)))
  2447. clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
  2448. unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
  2449. GFP_NOFS);
  2450. ulist_release(&changeset.range_changed);
  2451. return ret;
  2452. }
  2453. static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
  2454. int free)
  2455. {
  2456. struct extent_changeset changeset;
  2457. int trace_op = QGROUP_RELEASE;
  2458. int ret;
  2459. changeset.bytes_changed = 0;
  2460. ulist_init(&changeset.range_changed);
  2461. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2462. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2463. if (ret < 0)
  2464. goto out;
  2465. if (free)
  2466. trace_op = QGROUP_FREE;
  2467. trace_btrfs_qgroup_release_data(inode, start, len,
  2468. changeset.bytes_changed, trace_op);
  2469. if (free)
  2470. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2471. BTRFS_I(inode)->root->objectid,
  2472. changeset.bytes_changed);
  2473. out:
  2474. ulist_release(&changeset.range_changed);
  2475. return ret;
  2476. }
  2477. /*
  2478. * Free a reserved space range from io_tree and related qgroups
  2479. *
  2480. * Should be called when a range of pages get invalidated before reaching disk.
  2481. * Or for error cleanup case.
  2482. *
  2483. * For data written to disk, use btrfs_qgroup_release_data().
  2484. *
  2485. * NOTE: This function may sleep for memory allocation.
  2486. */
  2487. int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
  2488. {
  2489. return __btrfs_qgroup_release_data(inode, start, len, 1);
  2490. }
  2491. /*
  2492. * Release a reserved space range from io_tree only.
  2493. *
  2494. * Should be called when a range of pages get written to disk and corresponding
  2495. * FILE_EXTENT is inserted into corresponding root.
  2496. *
  2497. * Since new qgroup accounting framework will only update qgroup numbers at
  2498. * commit_transaction() time, its reserved space shouldn't be freed from
  2499. * related qgroups.
  2500. *
  2501. * But we should release the range from io_tree, to allow further write to be
  2502. * COWed.
  2503. *
  2504. * NOTE: This function may sleep for memory allocation.
  2505. */
  2506. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
  2507. {
  2508. return __btrfs_qgroup_release_data(inode, start, len, 0);
  2509. }
  2510. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  2511. bool enforce)
  2512. {
  2513. struct btrfs_fs_info *fs_info = root->fs_info;
  2514. int ret;
  2515. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2516. !is_fstree(root->objectid) || num_bytes == 0)
  2517. return 0;
  2518. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2519. trace_qgroup_meta_reserve(root, (s64)num_bytes);
  2520. ret = qgroup_reserve(root, num_bytes, enforce);
  2521. if (ret < 0)
  2522. return ret;
  2523. atomic64_add(num_bytes, &root->qgroup_meta_rsv);
  2524. return ret;
  2525. }
  2526. void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
  2527. {
  2528. struct btrfs_fs_info *fs_info = root->fs_info;
  2529. u64 reserved;
  2530. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2531. !is_fstree(root->objectid))
  2532. return;
  2533. reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
  2534. if (reserved == 0)
  2535. return;
  2536. trace_qgroup_meta_reserve(root, -(s64)reserved);
  2537. btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
  2538. }
  2539. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
  2540. {
  2541. struct btrfs_fs_info *fs_info = root->fs_info;
  2542. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2543. !is_fstree(root->objectid))
  2544. return;
  2545. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2546. WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
  2547. atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
  2548. trace_qgroup_meta_reserve(root, -(s64)num_bytes);
  2549. btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
  2550. }
  2551. /*
  2552. * Check qgroup reserved space leaking, normally at destroy inode
  2553. * time
  2554. */
  2555. void btrfs_qgroup_check_reserved_leak(struct inode *inode)
  2556. {
  2557. struct extent_changeset changeset;
  2558. struct ulist_node *unode;
  2559. struct ulist_iterator iter;
  2560. int ret;
  2561. changeset.bytes_changed = 0;
  2562. ulist_init(&changeset.range_changed);
  2563. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  2564. EXTENT_QGROUP_RESERVED, &changeset);
  2565. WARN_ON(ret < 0);
  2566. if (WARN_ON(changeset.bytes_changed)) {
  2567. ULIST_ITER_INIT(&iter);
  2568. while ((unode = ulist_next(&changeset.range_changed, &iter))) {
  2569. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2570. "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
  2571. inode->i_ino, unode->val, unode->aux);
  2572. }
  2573. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2574. BTRFS_I(inode)->root->objectid,
  2575. changeset.bytes_changed);
  2576. }
  2577. ulist_release(&changeset.range_changed);
  2578. }