qgroup.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  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. /*
  46. * one struct for each qgroup, organized in fs_info->qgroup_tree.
  47. */
  48. struct btrfs_qgroup {
  49. u64 qgroupid;
  50. /*
  51. * state
  52. */
  53. u64 rfer; /* referenced */
  54. u64 rfer_cmpr; /* referenced compressed */
  55. u64 excl; /* exclusive */
  56. u64 excl_cmpr; /* exclusive compressed */
  57. /*
  58. * limits
  59. */
  60. u64 lim_flags; /* which limits are set */
  61. u64 max_rfer;
  62. u64 max_excl;
  63. u64 rsv_rfer;
  64. u64 rsv_excl;
  65. /*
  66. * reservation tracking
  67. */
  68. u64 reserved;
  69. /*
  70. * lists
  71. */
  72. struct list_head groups; /* groups this group is member of */
  73. struct list_head members; /* groups that are members of this group */
  74. struct list_head dirty; /* dirty groups */
  75. struct rb_node node; /* tree of qgroups */
  76. /*
  77. * temp variables for accounting operations
  78. * Refer to qgroup_shared_accouting() for details.
  79. */
  80. u64 old_refcnt;
  81. u64 new_refcnt;
  82. };
  83. static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
  84. int mod)
  85. {
  86. if (qg->old_refcnt < seq)
  87. qg->old_refcnt = seq;
  88. qg->old_refcnt += mod;
  89. }
  90. static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
  91. int mod)
  92. {
  93. if (qg->new_refcnt < seq)
  94. qg->new_refcnt = seq;
  95. qg->new_refcnt += mod;
  96. }
  97. static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
  98. {
  99. if (qg->old_refcnt < seq)
  100. return 0;
  101. return qg->old_refcnt - seq;
  102. }
  103. static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
  104. {
  105. if (qg->new_refcnt < seq)
  106. return 0;
  107. return qg->new_refcnt - seq;
  108. }
  109. /*
  110. * glue structure to represent the relations between qgroups.
  111. */
  112. struct btrfs_qgroup_list {
  113. struct list_head next_group;
  114. struct list_head next_member;
  115. struct btrfs_qgroup *group;
  116. struct btrfs_qgroup *member;
  117. };
  118. #define ptr_to_u64(x) ((u64)(uintptr_t)x)
  119. #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
  120. static int
  121. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  122. int init_flags);
  123. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  124. /* must be called with qgroup_ioctl_lock held */
  125. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  126. u64 qgroupid)
  127. {
  128. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  129. struct btrfs_qgroup *qgroup;
  130. while (n) {
  131. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  132. if (qgroup->qgroupid < qgroupid)
  133. n = n->rb_left;
  134. else if (qgroup->qgroupid > qgroupid)
  135. n = n->rb_right;
  136. else
  137. return qgroup;
  138. }
  139. return NULL;
  140. }
  141. /* must be called with qgroup_lock held */
  142. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  143. u64 qgroupid)
  144. {
  145. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  146. struct rb_node *parent = NULL;
  147. struct btrfs_qgroup *qgroup;
  148. while (*p) {
  149. parent = *p;
  150. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  151. if (qgroup->qgroupid < qgroupid)
  152. p = &(*p)->rb_left;
  153. else if (qgroup->qgroupid > qgroupid)
  154. p = &(*p)->rb_right;
  155. else
  156. return qgroup;
  157. }
  158. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  159. if (!qgroup)
  160. return ERR_PTR(-ENOMEM);
  161. qgroup->qgroupid = qgroupid;
  162. INIT_LIST_HEAD(&qgroup->groups);
  163. INIT_LIST_HEAD(&qgroup->members);
  164. INIT_LIST_HEAD(&qgroup->dirty);
  165. rb_link_node(&qgroup->node, parent, p);
  166. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  167. return qgroup;
  168. }
  169. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  170. {
  171. struct btrfs_qgroup_list *list;
  172. list_del(&qgroup->dirty);
  173. while (!list_empty(&qgroup->groups)) {
  174. list = list_first_entry(&qgroup->groups,
  175. struct btrfs_qgroup_list, next_group);
  176. list_del(&list->next_group);
  177. list_del(&list->next_member);
  178. kfree(list);
  179. }
  180. while (!list_empty(&qgroup->members)) {
  181. list = list_first_entry(&qgroup->members,
  182. struct btrfs_qgroup_list, next_member);
  183. list_del(&list->next_group);
  184. list_del(&list->next_member);
  185. kfree(list);
  186. }
  187. kfree(qgroup);
  188. }
  189. /* must be called with qgroup_lock held */
  190. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  191. {
  192. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  193. if (!qgroup)
  194. return -ENOENT;
  195. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  196. __del_qgroup_rb(qgroup);
  197. return 0;
  198. }
  199. /* must be called with qgroup_lock held */
  200. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  201. u64 memberid, u64 parentid)
  202. {
  203. struct btrfs_qgroup *member;
  204. struct btrfs_qgroup *parent;
  205. struct btrfs_qgroup_list *list;
  206. member = find_qgroup_rb(fs_info, memberid);
  207. parent = find_qgroup_rb(fs_info, parentid);
  208. if (!member || !parent)
  209. return -ENOENT;
  210. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  211. if (!list)
  212. return -ENOMEM;
  213. list->group = parent;
  214. list->member = member;
  215. list_add_tail(&list->next_group, &member->groups);
  216. list_add_tail(&list->next_member, &parent->members);
  217. return 0;
  218. }
  219. /* must be called with qgroup_lock held */
  220. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  221. u64 memberid, u64 parentid)
  222. {
  223. struct btrfs_qgroup *member;
  224. struct btrfs_qgroup *parent;
  225. struct btrfs_qgroup_list *list;
  226. member = find_qgroup_rb(fs_info, memberid);
  227. parent = find_qgroup_rb(fs_info, parentid);
  228. if (!member || !parent)
  229. return -ENOENT;
  230. list_for_each_entry(list, &member->groups, next_group) {
  231. if (list->group == parent) {
  232. list_del(&list->next_group);
  233. list_del(&list->next_member);
  234. kfree(list);
  235. return 0;
  236. }
  237. }
  238. return -ENOENT;
  239. }
  240. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  241. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  242. u64 rfer, u64 excl)
  243. {
  244. struct btrfs_qgroup *qgroup;
  245. qgroup = find_qgroup_rb(fs_info, qgroupid);
  246. if (!qgroup)
  247. return -EINVAL;
  248. if (qgroup->rfer != rfer || qgroup->excl != excl)
  249. return -EINVAL;
  250. return 0;
  251. }
  252. #endif
  253. /*
  254. * The full config is read in one go, only called from open_ctree()
  255. * It doesn't use any locking, as at this point we're still single-threaded
  256. */
  257. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  258. {
  259. struct btrfs_key key;
  260. struct btrfs_key found_key;
  261. struct btrfs_root *quota_root = fs_info->quota_root;
  262. struct btrfs_path *path = NULL;
  263. struct extent_buffer *l;
  264. int slot;
  265. int ret = 0;
  266. u64 flags = 0;
  267. u64 rescan_progress = 0;
  268. if (!fs_info->quota_enabled)
  269. return 0;
  270. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  271. if (!fs_info->qgroup_ulist) {
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. path = btrfs_alloc_path();
  276. if (!path) {
  277. ret = -ENOMEM;
  278. goto out;
  279. }
  280. /* default this to quota off, in case no status key is found */
  281. fs_info->qgroup_flags = 0;
  282. /*
  283. * pass 1: read status, all qgroup infos and limits
  284. */
  285. key.objectid = 0;
  286. key.type = 0;
  287. key.offset = 0;
  288. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  289. if (ret)
  290. goto out;
  291. while (1) {
  292. struct btrfs_qgroup *qgroup;
  293. slot = path->slots[0];
  294. l = path->nodes[0];
  295. btrfs_item_key_to_cpu(l, &found_key, slot);
  296. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  297. struct btrfs_qgroup_status_item *ptr;
  298. ptr = btrfs_item_ptr(l, slot,
  299. struct btrfs_qgroup_status_item);
  300. if (btrfs_qgroup_status_version(l, ptr) !=
  301. BTRFS_QGROUP_STATUS_VERSION) {
  302. btrfs_err(fs_info,
  303. "old qgroup version, quota disabled");
  304. goto out;
  305. }
  306. if (btrfs_qgroup_status_generation(l, ptr) !=
  307. fs_info->generation) {
  308. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  309. btrfs_err(fs_info,
  310. "qgroup generation mismatch, "
  311. "marked as inconsistent");
  312. }
  313. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  314. ptr);
  315. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  316. goto next1;
  317. }
  318. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  319. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  320. goto next1;
  321. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  322. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  323. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  324. btrfs_err(fs_info, "inconsitent qgroup config");
  325. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  326. }
  327. if (!qgroup) {
  328. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  329. if (IS_ERR(qgroup)) {
  330. ret = PTR_ERR(qgroup);
  331. goto out;
  332. }
  333. }
  334. switch (found_key.type) {
  335. case BTRFS_QGROUP_INFO_KEY: {
  336. struct btrfs_qgroup_info_item *ptr;
  337. ptr = btrfs_item_ptr(l, slot,
  338. struct btrfs_qgroup_info_item);
  339. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  340. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  341. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  342. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  343. /* generation currently unused */
  344. break;
  345. }
  346. case BTRFS_QGROUP_LIMIT_KEY: {
  347. struct btrfs_qgroup_limit_item *ptr;
  348. ptr = btrfs_item_ptr(l, slot,
  349. struct btrfs_qgroup_limit_item);
  350. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  351. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  352. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  353. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  354. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  355. break;
  356. }
  357. }
  358. next1:
  359. ret = btrfs_next_item(quota_root, path);
  360. if (ret < 0)
  361. goto out;
  362. if (ret)
  363. break;
  364. }
  365. btrfs_release_path(path);
  366. /*
  367. * pass 2: read all qgroup relations
  368. */
  369. key.objectid = 0;
  370. key.type = BTRFS_QGROUP_RELATION_KEY;
  371. key.offset = 0;
  372. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  373. if (ret)
  374. goto out;
  375. while (1) {
  376. slot = path->slots[0];
  377. l = path->nodes[0];
  378. btrfs_item_key_to_cpu(l, &found_key, slot);
  379. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  380. goto next2;
  381. if (found_key.objectid > found_key.offset) {
  382. /* parent <- member, not needed to build config */
  383. /* FIXME should we omit the key completely? */
  384. goto next2;
  385. }
  386. ret = add_relation_rb(fs_info, found_key.objectid,
  387. found_key.offset);
  388. if (ret == -ENOENT) {
  389. btrfs_warn(fs_info,
  390. "orphan qgroup relation 0x%llx->0x%llx",
  391. found_key.objectid, found_key.offset);
  392. ret = 0; /* ignore the error */
  393. }
  394. if (ret)
  395. goto out;
  396. next2:
  397. ret = btrfs_next_item(quota_root, path);
  398. if (ret < 0)
  399. goto out;
  400. if (ret)
  401. break;
  402. }
  403. out:
  404. fs_info->qgroup_flags |= flags;
  405. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  406. fs_info->quota_enabled = 0;
  407. fs_info->pending_quota_state = 0;
  408. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  409. ret >= 0) {
  410. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  411. }
  412. btrfs_free_path(path);
  413. if (ret < 0) {
  414. ulist_free(fs_info->qgroup_ulist);
  415. fs_info->qgroup_ulist = NULL;
  416. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  417. }
  418. return ret < 0 ? ret : 0;
  419. }
  420. /*
  421. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  422. * first two are in single-threaded paths.And for the third one, we have set
  423. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  424. * up the in-memory structures without qgroup_lock held.
  425. */
  426. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  427. {
  428. struct rb_node *n;
  429. struct btrfs_qgroup *qgroup;
  430. while ((n = rb_first(&fs_info->qgroup_tree))) {
  431. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  432. rb_erase(n, &fs_info->qgroup_tree);
  433. __del_qgroup_rb(qgroup);
  434. }
  435. /*
  436. * we call btrfs_free_qgroup_config() when umounting
  437. * filesystem and disabling quota, so we set qgroup_ulit
  438. * to be null here to avoid double free.
  439. */
  440. ulist_free(fs_info->qgroup_ulist);
  441. fs_info->qgroup_ulist = NULL;
  442. }
  443. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  444. struct btrfs_root *quota_root,
  445. u64 src, u64 dst)
  446. {
  447. int ret;
  448. struct btrfs_path *path;
  449. struct btrfs_key key;
  450. path = btrfs_alloc_path();
  451. if (!path)
  452. return -ENOMEM;
  453. key.objectid = src;
  454. key.type = BTRFS_QGROUP_RELATION_KEY;
  455. key.offset = dst;
  456. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  457. btrfs_mark_buffer_dirty(path->nodes[0]);
  458. btrfs_free_path(path);
  459. return ret;
  460. }
  461. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  462. struct btrfs_root *quota_root,
  463. u64 src, u64 dst)
  464. {
  465. int ret;
  466. struct btrfs_path *path;
  467. struct btrfs_key key;
  468. path = btrfs_alloc_path();
  469. if (!path)
  470. return -ENOMEM;
  471. key.objectid = src;
  472. key.type = BTRFS_QGROUP_RELATION_KEY;
  473. key.offset = dst;
  474. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  475. if (ret < 0)
  476. goto out;
  477. if (ret > 0) {
  478. ret = -ENOENT;
  479. goto out;
  480. }
  481. ret = btrfs_del_item(trans, quota_root, path);
  482. out:
  483. btrfs_free_path(path);
  484. return ret;
  485. }
  486. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  487. struct btrfs_root *quota_root, u64 qgroupid)
  488. {
  489. int ret;
  490. struct btrfs_path *path;
  491. struct btrfs_qgroup_info_item *qgroup_info;
  492. struct btrfs_qgroup_limit_item *qgroup_limit;
  493. struct extent_buffer *leaf;
  494. struct btrfs_key key;
  495. if (btrfs_test_is_dummy_root(quota_root))
  496. return 0;
  497. path = btrfs_alloc_path();
  498. if (!path)
  499. return -ENOMEM;
  500. key.objectid = 0;
  501. key.type = BTRFS_QGROUP_INFO_KEY;
  502. key.offset = qgroupid;
  503. /*
  504. * Avoid a transaction abort by catching -EEXIST here. In that
  505. * case, we proceed by re-initializing the existing structure
  506. * on disk.
  507. */
  508. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  509. sizeof(*qgroup_info));
  510. if (ret && ret != -EEXIST)
  511. goto out;
  512. leaf = path->nodes[0];
  513. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  514. struct btrfs_qgroup_info_item);
  515. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  516. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  517. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  518. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  519. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  520. btrfs_mark_buffer_dirty(leaf);
  521. btrfs_release_path(path);
  522. key.type = BTRFS_QGROUP_LIMIT_KEY;
  523. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  524. sizeof(*qgroup_limit));
  525. if (ret && ret != -EEXIST)
  526. goto out;
  527. leaf = path->nodes[0];
  528. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  529. struct btrfs_qgroup_limit_item);
  530. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  531. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  532. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  533. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  534. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  535. btrfs_mark_buffer_dirty(leaf);
  536. ret = 0;
  537. out:
  538. btrfs_free_path(path);
  539. return ret;
  540. }
  541. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  542. struct btrfs_root *quota_root, u64 qgroupid)
  543. {
  544. int ret;
  545. struct btrfs_path *path;
  546. struct btrfs_key key;
  547. path = btrfs_alloc_path();
  548. if (!path)
  549. return -ENOMEM;
  550. key.objectid = 0;
  551. key.type = BTRFS_QGROUP_INFO_KEY;
  552. key.offset = qgroupid;
  553. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  554. if (ret < 0)
  555. goto out;
  556. if (ret > 0) {
  557. ret = -ENOENT;
  558. goto out;
  559. }
  560. ret = btrfs_del_item(trans, quota_root, path);
  561. if (ret)
  562. goto out;
  563. btrfs_release_path(path);
  564. key.type = BTRFS_QGROUP_LIMIT_KEY;
  565. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  566. if (ret < 0)
  567. goto out;
  568. if (ret > 0) {
  569. ret = -ENOENT;
  570. goto out;
  571. }
  572. ret = btrfs_del_item(trans, quota_root, path);
  573. out:
  574. btrfs_free_path(path);
  575. return ret;
  576. }
  577. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  578. struct btrfs_root *root,
  579. struct btrfs_qgroup *qgroup)
  580. {
  581. struct btrfs_path *path;
  582. struct btrfs_key key;
  583. struct extent_buffer *l;
  584. struct btrfs_qgroup_limit_item *qgroup_limit;
  585. int ret;
  586. int slot;
  587. key.objectid = 0;
  588. key.type = BTRFS_QGROUP_LIMIT_KEY;
  589. key.offset = qgroup->qgroupid;
  590. path = btrfs_alloc_path();
  591. if (!path)
  592. return -ENOMEM;
  593. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  594. if (ret > 0)
  595. ret = -ENOENT;
  596. if (ret)
  597. goto out;
  598. l = path->nodes[0];
  599. slot = path->slots[0];
  600. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  601. btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
  602. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
  603. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
  604. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
  605. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
  606. btrfs_mark_buffer_dirty(l);
  607. out:
  608. btrfs_free_path(path);
  609. return ret;
  610. }
  611. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  612. struct btrfs_root *root,
  613. struct btrfs_qgroup *qgroup)
  614. {
  615. struct btrfs_path *path;
  616. struct btrfs_key key;
  617. struct extent_buffer *l;
  618. struct btrfs_qgroup_info_item *qgroup_info;
  619. int ret;
  620. int slot;
  621. if (btrfs_test_is_dummy_root(root))
  622. return 0;
  623. key.objectid = 0;
  624. key.type = BTRFS_QGROUP_INFO_KEY;
  625. key.offset = qgroup->qgroupid;
  626. path = btrfs_alloc_path();
  627. if (!path)
  628. return -ENOMEM;
  629. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  630. if (ret > 0)
  631. ret = -ENOENT;
  632. if (ret)
  633. goto out;
  634. l = path->nodes[0];
  635. slot = path->slots[0];
  636. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  637. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  638. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  639. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  640. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  641. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  642. btrfs_mark_buffer_dirty(l);
  643. out:
  644. btrfs_free_path(path);
  645. return ret;
  646. }
  647. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  648. struct btrfs_fs_info *fs_info,
  649. struct btrfs_root *root)
  650. {
  651. struct btrfs_path *path;
  652. struct btrfs_key key;
  653. struct extent_buffer *l;
  654. struct btrfs_qgroup_status_item *ptr;
  655. int ret;
  656. int slot;
  657. key.objectid = 0;
  658. key.type = BTRFS_QGROUP_STATUS_KEY;
  659. key.offset = 0;
  660. path = btrfs_alloc_path();
  661. if (!path)
  662. return -ENOMEM;
  663. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  664. if (ret > 0)
  665. ret = -ENOENT;
  666. if (ret)
  667. goto out;
  668. l = path->nodes[0];
  669. slot = path->slots[0];
  670. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  671. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  672. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  673. btrfs_set_qgroup_status_rescan(l, ptr,
  674. fs_info->qgroup_rescan_progress.objectid);
  675. btrfs_mark_buffer_dirty(l);
  676. out:
  677. btrfs_free_path(path);
  678. return ret;
  679. }
  680. /*
  681. * called with qgroup_lock held
  682. */
  683. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  684. struct btrfs_root *root)
  685. {
  686. struct btrfs_path *path;
  687. struct btrfs_key key;
  688. struct extent_buffer *leaf = NULL;
  689. int ret;
  690. int nr = 0;
  691. path = btrfs_alloc_path();
  692. if (!path)
  693. return -ENOMEM;
  694. path->leave_spinning = 1;
  695. key.objectid = 0;
  696. key.offset = 0;
  697. key.type = 0;
  698. while (1) {
  699. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  700. if (ret < 0)
  701. goto out;
  702. leaf = path->nodes[0];
  703. nr = btrfs_header_nritems(leaf);
  704. if (!nr)
  705. break;
  706. /*
  707. * delete the leaf one by one
  708. * since the whole tree is going
  709. * to be deleted.
  710. */
  711. path->slots[0] = 0;
  712. ret = btrfs_del_items(trans, root, path, 0, nr);
  713. if (ret)
  714. goto out;
  715. btrfs_release_path(path);
  716. }
  717. ret = 0;
  718. out:
  719. root->fs_info->pending_quota_state = 0;
  720. btrfs_free_path(path);
  721. return ret;
  722. }
  723. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  724. struct btrfs_fs_info *fs_info)
  725. {
  726. struct btrfs_root *quota_root;
  727. struct btrfs_root *tree_root = fs_info->tree_root;
  728. struct btrfs_path *path = NULL;
  729. struct btrfs_qgroup_status_item *ptr;
  730. struct extent_buffer *leaf;
  731. struct btrfs_key key;
  732. struct btrfs_key found_key;
  733. struct btrfs_qgroup *qgroup = NULL;
  734. int ret = 0;
  735. int slot;
  736. mutex_lock(&fs_info->qgroup_ioctl_lock);
  737. if (fs_info->quota_root) {
  738. fs_info->pending_quota_state = 1;
  739. goto out;
  740. }
  741. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  742. if (!fs_info->qgroup_ulist) {
  743. ret = -ENOMEM;
  744. goto out;
  745. }
  746. /*
  747. * initially create the quota tree
  748. */
  749. quota_root = btrfs_create_tree(trans, fs_info,
  750. BTRFS_QUOTA_TREE_OBJECTID);
  751. if (IS_ERR(quota_root)) {
  752. ret = PTR_ERR(quota_root);
  753. goto out;
  754. }
  755. path = btrfs_alloc_path();
  756. if (!path) {
  757. ret = -ENOMEM;
  758. goto out_free_root;
  759. }
  760. key.objectid = 0;
  761. key.type = BTRFS_QGROUP_STATUS_KEY;
  762. key.offset = 0;
  763. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  764. sizeof(*ptr));
  765. if (ret)
  766. goto out_free_path;
  767. leaf = path->nodes[0];
  768. ptr = btrfs_item_ptr(leaf, path->slots[0],
  769. struct btrfs_qgroup_status_item);
  770. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  771. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  772. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  773. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  774. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  775. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  776. btrfs_mark_buffer_dirty(leaf);
  777. key.objectid = 0;
  778. key.type = BTRFS_ROOT_REF_KEY;
  779. key.offset = 0;
  780. btrfs_release_path(path);
  781. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  782. if (ret > 0)
  783. goto out_add_root;
  784. if (ret < 0)
  785. goto out_free_path;
  786. while (1) {
  787. slot = path->slots[0];
  788. leaf = path->nodes[0];
  789. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  790. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  791. ret = add_qgroup_item(trans, quota_root,
  792. found_key.offset);
  793. if (ret)
  794. goto out_free_path;
  795. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  796. if (IS_ERR(qgroup)) {
  797. ret = PTR_ERR(qgroup);
  798. goto out_free_path;
  799. }
  800. }
  801. ret = btrfs_next_item(tree_root, path);
  802. if (ret < 0)
  803. goto out_free_path;
  804. if (ret)
  805. break;
  806. }
  807. out_add_root:
  808. btrfs_release_path(path);
  809. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  810. if (ret)
  811. goto out_free_path;
  812. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  813. if (IS_ERR(qgroup)) {
  814. ret = PTR_ERR(qgroup);
  815. goto out_free_path;
  816. }
  817. spin_lock(&fs_info->qgroup_lock);
  818. fs_info->quota_root = quota_root;
  819. fs_info->pending_quota_state = 1;
  820. spin_unlock(&fs_info->qgroup_lock);
  821. out_free_path:
  822. btrfs_free_path(path);
  823. out_free_root:
  824. if (ret) {
  825. free_extent_buffer(quota_root->node);
  826. free_extent_buffer(quota_root->commit_root);
  827. kfree(quota_root);
  828. }
  829. out:
  830. if (ret) {
  831. ulist_free(fs_info->qgroup_ulist);
  832. fs_info->qgroup_ulist = NULL;
  833. }
  834. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  835. return ret;
  836. }
  837. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  838. struct btrfs_fs_info *fs_info)
  839. {
  840. struct btrfs_root *tree_root = fs_info->tree_root;
  841. struct btrfs_root *quota_root;
  842. int ret = 0;
  843. mutex_lock(&fs_info->qgroup_ioctl_lock);
  844. if (!fs_info->quota_root)
  845. goto out;
  846. spin_lock(&fs_info->qgroup_lock);
  847. fs_info->quota_enabled = 0;
  848. fs_info->pending_quota_state = 0;
  849. quota_root = fs_info->quota_root;
  850. fs_info->quota_root = NULL;
  851. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  852. spin_unlock(&fs_info->qgroup_lock);
  853. btrfs_free_qgroup_config(fs_info);
  854. ret = btrfs_clean_quota_tree(trans, quota_root);
  855. if (ret)
  856. goto out;
  857. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  858. if (ret)
  859. goto out;
  860. list_del(&quota_root->dirty_list);
  861. btrfs_tree_lock(quota_root->node);
  862. clean_tree_block(trans, tree_root->fs_info, quota_root->node);
  863. btrfs_tree_unlock(quota_root->node);
  864. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  865. free_extent_buffer(quota_root->node);
  866. free_extent_buffer(quota_root->commit_root);
  867. kfree(quota_root);
  868. out:
  869. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  870. return ret;
  871. }
  872. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  873. struct btrfs_qgroup *qgroup)
  874. {
  875. if (list_empty(&qgroup->dirty))
  876. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  877. }
  878. /*
  879. * The easy accounting, if we are adding/removing the only ref for an extent
  880. * then this qgroup and all of the parent qgroups get their refrence and
  881. * exclusive counts adjusted.
  882. *
  883. * Caller should hold fs_info->qgroup_lock.
  884. */
  885. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  886. struct ulist *tmp, u64 ref_root,
  887. u64 num_bytes, int sign)
  888. {
  889. struct btrfs_qgroup *qgroup;
  890. struct btrfs_qgroup_list *glist;
  891. struct ulist_node *unode;
  892. struct ulist_iterator uiter;
  893. int ret = 0;
  894. qgroup = find_qgroup_rb(fs_info, ref_root);
  895. if (!qgroup)
  896. goto out;
  897. qgroup->rfer += sign * num_bytes;
  898. qgroup->rfer_cmpr += sign * num_bytes;
  899. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  900. qgroup->excl += sign * num_bytes;
  901. qgroup->excl_cmpr += sign * num_bytes;
  902. if (sign > 0)
  903. qgroup->reserved -= num_bytes;
  904. qgroup_dirty(fs_info, qgroup);
  905. /* Get all of the parent groups that contain this qgroup */
  906. list_for_each_entry(glist, &qgroup->groups, next_group) {
  907. ret = ulist_add(tmp, glist->group->qgroupid,
  908. ptr_to_u64(glist->group), GFP_ATOMIC);
  909. if (ret < 0)
  910. goto out;
  911. }
  912. /* Iterate all of the parents and adjust their reference counts */
  913. ULIST_ITER_INIT(&uiter);
  914. while ((unode = ulist_next(tmp, &uiter))) {
  915. qgroup = u64_to_ptr(unode->aux);
  916. qgroup->rfer += sign * num_bytes;
  917. qgroup->rfer_cmpr += sign * num_bytes;
  918. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  919. qgroup->excl += sign * num_bytes;
  920. if (sign > 0)
  921. qgroup->reserved -= num_bytes;
  922. qgroup->excl_cmpr += sign * num_bytes;
  923. qgroup_dirty(fs_info, qgroup);
  924. /* Add any parents of the parents */
  925. list_for_each_entry(glist, &qgroup->groups, next_group) {
  926. ret = ulist_add(tmp, glist->group->qgroupid,
  927. ptr_to_u64(glist->group), GFP_ATOMIC);
  928. if (ret < 0)
  929. goto out;
  930. }
  931. }
  932. ret = 0;
  933. out:
  934. return ret;
  935. }
  936. /*
  937. * Quick path for updating qgroup with only excl refs.
  938. *
  939. * In that case, just update all parent will be enough.
  940. * Or we needs to do a full rescan.
  941. * Caller should also hold fs_info->qgroup_lock.
  942. *
  943. * Return 0 for quick update, return >0 for need to full rescan
  944. * and mark INCONSISTENT flag.
  945. * Return < 0 for other error.
  946. */
  947. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  948. struct ulist *tmp, u64 src, u64 dst,
  949. int sign)
  950. {
  951. struct btrfs_qgroup *qgroup;
  952. int ret = 1;
  953. int err = 0;
  954. qgroup = find_qgroup_rb(fs_info, src);
  955. if (!qgroup)
  956. goto out;
  957. if (qgroup->excl == qgroup->rfer) {
  958. ret = 0;
  959. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  960. qgroup->excl, sign);
  961. if (err < 0) {
  962. ret = err;
  963. goto out;
  964. }
  965. }
  966. out:
  967. if (ret)
  968. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  969. return ret;
  970. }
  971. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  972. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  973. {
  974. struct btrfs_root *quota_root;
  975. struct btrfs_qgroup *parent;
  976. struct btrfs_qgroup *member;
  977. struct btrfs_qgroup_list *list;
  978. struct ulist *tmp;
  979. int ret = 0;
  980. /* Check the level of src and dst first */
  981. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  982. return -EINVAL;
  983. tmp = ulist_alloc(GFP_NOFS);
  984. if (!tmp)
  985. return -ENOMEM;
  986. mutex_lock(&fs_info->qgroup_ioctl_lock);
  987. quota_root = fs_info->quota_root;
  988. if (!quota_root) {
  989. ret = -EINVAL;
  990. goto out;
  991. }
  992. member = find_qgroup_rb(fs_info, src);
  993. parent = find_qgroup_rb(fs_info, dst);
  994. if (!member || !parent) {
  995. ret = -EINVAL;
  996. goto out;
  997. }
  998. /* check if such qgroup relation exist firstly */
  999. list_for_each_entry(list, &member->groups, next_group) {
  1000. if (list->group == parent) {
  1001. ret = -EEXIST;
  1002. goto out;
  1003. }
  1004. }
  1005. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  1006. if (ret)
  1007. goto out;
  1008. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  1009. if (ret) {
  1010. del_qgroup_relation_item(trans, quota_root, src, dst);
  1011. goto out;
  1012. }
  1013. spin_lock(&fs_info->qgroup_lock);
  1014. ret = add_relation_rb(quota_root->fs_info, src, dst);
  1015. if (ret < 0) {
  1016. spin_unlock(&fs_info->qgroup_lock);
  1017. goto out;
  1018. }
  1019. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1020. spin_unlock(&fs_info->qgroup_lock);
  1021. out:
  1022. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1023. ulist_free(tmp);
  1024. return ret;
  1025. }
  1026. int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1027. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1028. {
  1029. struct btrfs_root *quota_root;
  1030. struct btrfs_qgroup *parent;
  1031. struct btrfs_qgroup *member;
  1032. struct btrfs_qgroup_list *list;
  1033. struct ulist *tmp;
  1034. int ret = 0;
  1035. int err;
  1036. tmp = ulist_alloc(GFP_NOFS);
  1037. if (!tmp)
  1038. return -ENOMEM;
  1039. quota_root = fs_info->quota_root;
  1040. if (!quota_root) {
  1041. ret = -EINVAL;
  1042. goto out;
  1043. }
  1044. member = find_qgroup_rb(fs_info, src);
  1045. parent = find_qgroup_rb(fs_info, dst);
  1046. if (!member || !parent) {
  1047. ret = -EINVAL;
  1048. goto out;
  1049. }
  1050. /* check if such qgroup relation exist firstly */
  1051. list_for_each_entry(list, &member->groups, next_group) {
  1052. if (list->group == parent)
  1053. goto exist;
  1054. }
  1055. ret = -ENOENT;
  1056. goto out;
  1057. exist:
  1058. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1059. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1060. if (err && !ret)
  1061. ret = err;
  1062. spin_lock(&fs_info->qgroup_lock);
  1063. del_relation_rb(fs_info, src, dst);
  1064. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1065. spin_unlock(&fs_info->qgroup_lock);
  1066. out:
  1067. ulist_free(tmp);
  1068. return ret;
  1069. }
  1070. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1071. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1072. {
  1073. int ret = 0;
  1074. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1075. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1076. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1077. return ret;
  1078. }
  1079. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1080. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1081. {
  1082. struct btrfs_root *quota_root;
  1083. struct btrfs_qgroup *qgroup;
  1084. int ret = 0;
  1085. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1086. quota_root = fs_info->quota_root;
  1087. if (!quota_root) {
  1088. ret = -EINVAL;
  1089. goto out;
  1090. }
  1091. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1092. if (qgroup) {
  1093. ret = -EEXIST;
  1094. goto out;
  1095. }
  1096. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1097. if (ret)
  1098. goto out;
  1099. spin_lock(&fs_info->qgroup_lock);
  1100. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1101. spin_unlock(&fs_info->qgroup_lock);
  1102. if (IS_ERR(qgroup))
  1103. ret = PTR_ERR(qgroup);
  1104. out:
  1105. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1106. return ret;
  1107. }
  1108. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1109. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1110. {
  1111. struct btrfs_root *quota_root;
  1112. struct btrfs_qgroup *qgroup;
  1113. struct btrfs_qgroup_list *list;
  1114. int ret = 0;
  1115. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1116. quota_root = fs_info->quota_root;
  1117. if (!quota_root) {
  1118. ret = -EINVAL;
  1119. goto out;
  1120. }
  1121. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1122. if (!qgroup) {
  1123. ret = -ENOENT;
  1124. goto out;
  1125. } else {
  1126. /* check if there are no children of this qgroup */
  1127. if (!list_empty(&qgroup->members)) {
  1128. ret = -EBUSY;
  1129. goto out;
  1130. }
  1131. }
  1132. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1133. while (!list_empty(&qgroup->groups)) {
  1134. list = list_first_entry(&qgroup->groups,
  1135. struct btrfs_qgroup_list, next_group);
  1136. ret = __del_qgroup_relation(trans, fs_info,
  1137. qgroupid,
  1138. list->group->qgroupid);
  1139. if (ret)
  1140. goto out;
  1141. }
  1142. spin_lock(&fs_info->qgroup_lock);
  1143. del_qgroup_rb(quota_root->fs_info, qgroupid);
  1144. spin_unlock(&fs_info->qgroup_lock);
  1145. out:
  1146. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1147. return ret;
  1148. }
  1149. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1150. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1151. struct btrfs_qgroup_limit *limit)
  1152. {
  1153. struct btrfs_root *quota_root;
  1154. struct btrfs_qgroup *qgroup;
  1155. int ret = 0;
  1156. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1157. quota_root = fs_info->quota_root;
  1158. if (!quota_root) {
  1159. ret = -EINVAL;
  1160. goto out;
  1161. }
  1162. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1163. if (!qgroup) {
  1164. ret = -ENOENT;
  1165. goto out;
  1166. }
  1167. spin_lock(&fs_info->qgroup_lock);
  1168. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
  1169. qgroup->max_rfer = limit->max_rfer;
  1170. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
  1171. qgroup->max_excl = limit->max_excl;
  1172. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER)
  1173. qgroup->rsv_rfer = limit->rsv_rfer;
  1174. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL)
  1175. qgroup->rsv_excl = limit->rsv_excl;
  1176. qgroup->lim_flags |= limit->flags;
  1177. spin_unlock(&fs_info->qgroup_lock);
  1178. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1179. if (ret) {
  1180. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1181. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1182. qgroupid);
  1183. }
  1184. out:
  1185. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1186. return ret;
  1187. }
  1188. int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
  1189. struct btrfs_fs_info *fs_info)
  1190. {
  1191. struct btrfs_qgroup_extent_record *record;
  1192. struct btrfs_delayed_ref_root *delayed_refs;
  1193. struct rb_node *node;
  1194. u64 qgroup_to_skip;
  1195. int ret = 0;
  1196. delayed_refs = &trans->transaction->delayed_refs;
  1197. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1198. /*
  1199. * No need to do lock, since this function will only be called in
  1200. * btrfs_commmit_transaction().
  1201. */
  1202. node = rb_first(&delayed_refs->dirty_extent_root);
  1203. while (node) {
  1204. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1205. node);
  1206. ret = btrfs_find_all_roots(NULL, fs_info, record->bytenr, 0,
  1207. &record->old_roots);
  1208. if (ret < 0)
  1209. break;
  1210. if (qgroup_to_skip)
  1211. ulist_del(record->old_roots, qgroup_to_skip, 0);
  1212. node = rb_next(node);
  1213. }
  1214. return ret;
  1215. }
  1216. struct btrfs_qgroup_extent_record
  1217. *btrfs_qgroup_insert_dirty_extent(struct btrfs_delayed_ref_root *delayed_refs,
  1218. struct btrfs_qgroup_extent_record *record)
  1219. {
  1220. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1221. struct rb_node *parent_node = NULL;
  1222. struct btrfs_qgroup_extent_record *entry;
  1223. u64 bytenr = record->bytenr;
  1224. while (*p) {
  1225. parent_node = *p;
  1226. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1227. node);
  1228. if (bytenr < entry->bytenr)
  1229. p = &(*p)->rb_left;
  1230. else if (bytenr > entry->bytenr)
  1231. p = &(*p)->rb_right;
  1232. else
  1233. return entry;
  1234. }
  1235. rb_link_node(&record->node, parent_node, p);
  1236. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1237. return NULL;
  1238. }
  1239. #define UPDATE_NEW 0
  1240. #define UPDATE_OLD 1
  1241. /*
  1242. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1243. */
  1244. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1245. struct ulist *roots, struct ulist *tmp,
  1246. struct ulist *qgroups, u64 seq, int update_old)
  1247. {
  1248. struct ulist_node *unode;
  1249. struct ulist_iterator uiter;
  1250. struct ulist_node *tmp_unode;
  1251. struct ulist_iterator tmp_uiter;
  1252. struct btrfs_qgroup *qg;
  1253. int ret = 0;
  1254. if (!roots)
  1255. return 0;
  1256. ULIST_ITER_INIT(&uiter);
  1257. while ((unode = ulist_next(roots, &uiter))) {
  1258. qg = find_qgroup_rb(fs_info, unode->val);
  1259. if (!qg)
  1260. continue;
  1261. ulist_reinit(tmp);
  1262. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1263. GFP_ATOMIC);
  1264. if (ret < 0)
  1265. return ret;
  1266. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1267. if (ret < 0)
  1268. return ret;
  1269. ULIST_ITER_INIT(&tmp_uiter);
  1270. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1271. struct btrfs_qgroup_list *glist;
  1272. qg = u64_to_ptr(tmp_unode->aux);
  1273. if (update_old)
  1274. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1275. else
  1276. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1277. list_for_each_entry(glist, &qg->groups, next_group) {
  1278. ret = ulist_add(qgroups, glist->group->qgroupid,
  1279. ptr_to_u64(glist->group),
  1280. GFP_ATOMIC);
  1281. if (ret < 0)
  1282. return ret;
  1283. ret = ulist_add(tmp, glist->group->qgroupid,
  1284. ptr_to_u64(glist->group),
  1285. GFP_ATOMIC);
  1286. if (ret < 0)
  1287. return ret;
  1288. }
  1289. }
  1290. }
  1291. return 0;
  1292. }
  1293. /*
  1294. * Update qgroup rfer/excl counters.
  1295. * Rfer update is easy, codes can explain themselves.
  1296. *
  1297. * Excl update is tricky, the update is split into 2 part.
  1298. * Part 1: Possible exclusive <-> sharing detect:
  1299. * | A | !A |
  1300. * -------------------------------------
  1301. * B | * | - |
  1302. * -------------------------------------
  1303. * !B | + | ** |
  1304. * -------------------------------------
  1305. *
  1306. * Conditions:
  1307. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1308. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1309. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1310. * !B: cur_new_roots == nr_new_roots (possible exclsuive now)
  1311. *
  1312. * Results:
  1313. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1314. * *: Definitely not changed. **: Possible unchanged.
  1315. *
  1316. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1317. *
  1318. * To make the logic clear, we first use condition A and B to split
  1319. * combination into 4 results.
  1320. *
  1321. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1322. * only on variant maybe 0.
  1323. *
  1324. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1325. * again(2x2).
  1326. * But this time we don't need to consider other things, the codes and logic
  1327. * is easy to understand now.
  1328. */
  1329. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1330. struct ulist *qgroups,
  1331. u64 nr_old_roots,
  1332. u64 nr_new_roots,
  1333. u64 num_bytes, u64 seq)
  1334. {
  1335. struct ulist_node *unode;
  1336. struct ulist_iterator uiter;
  1337. struct btrfs_qgroup *qg;
  1338. u64 cur_new_count, cur_old_count;
  1339. ULIST_ITER_INIT(&uiter);
  1340. while ((unode = ulist_next(qgroups, &uiter))) {
  1341. bool dirty = false;
  1342. qg = u64_to_ptr(unode->aux);
  1343. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1344. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1345. /* Rfer update part */
  1346. if (cur_old_count == 0 && cur_new_count > 0) {
  1347. qg->rfer += num_bytes;
  1348. qg->rfer_cmpr += num_bytes;
  1349. dirty = true;
  1350. }
  1351. if (cur_old_count > 0 && cur_new_count == 0) {
  1352. qg->rfer -= num_bytes;
  1353. qg->rfer_cmpr -= num_bytes;
  1354. dirty = true;
  1355. }
  1356. /* Excl update part */
  1357. /* Exclusive/none -> shared case */
  1358. if (cur_old_count == nr_old_roots &&
  1359. cur_new_count < nr_new_roots) {
  1360. /* Exclusive -> shared */
  1361. if (cur_old_count != 0) {
  1362. qg->excl -= num_bytes;
  1363. qg->excl_cmpr -= num_bytes;
  1364. dirty = true;
  1365. }
  1366. }
  1367. /* Shared -> exclusive/none case */
  1368. if (cur_old_count < nr_old_roots &&
  1369. cur_new_count == nr_new_roots) {
  1370. /* Shared->exclusive */
  1371. if (cur_new_count != 0) {
  1372. qg->excl += num_bytes;
  1373. qg->excl_cmpr += num_bytes;
  1374. dirty = true;
  1375. }
  1376. }
  1377. /* Exclusive/none -> exclusive/none case */
  1378. if (cur_old_count == nr_old_roots &&
  1379. cur_new_count == nr_new_roots) {
  1380. if (cur_old_count == 0) {
  1381. /* None -> exclusive/none */
  1382. if (cur_new_count != 0) {
  1383. /* None -> exclusive */
  1384. qg->excl += num_bytes;
  1385. qg->excl_cmpr += num_bytes;
  1386. dirty = true;
  1387. }
  1388. /* None -> none, nothing changed */
  1389. } else {
  1390. /* Exclusive -> exclusive/none */
  1391. if (cur_new_count == 0) {
  1392. /* Exclusive -> none */
  1393. qg->excl -= num_bytes;
  1394. qg->excl_cmpr -= num_bytes;
  1395. dirty = true;
  1396. }
  1397. /* Exclusive -> exclusive, nothing changed */
  1398. }
  1399. }
  1400. if (dirty)
  1401. qgroup_dirty(fs_info, qg);
  1402. }
  1403. return 0;
  1404. }
  1405. int
  1406. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1407. struct btrfs_fs_info *fs_info,
  1408. u64 bytenr, u64 num_bytes,
  1409. struct ulist *old_roots, struct ulist *new_roots)
  1410. {
  1411. struct ulist *qgroups = NULL;
  1412. struct ulist *tmp = NULL;
  1413. u64 seq;
  1414. u64 nr_new_roots = 0;
  1415. u64 nr_old_roots = 0;
  1416. int ret = 0;
  1417. if (new_roots)
  1418. nr_new_roots = new_roots->nnodes;
  1419. if (old_roots)
  1420. nr_old_roots = old_roots->nnodes;
  1421. if (!fs_info->quota_enabled)
  1422. goto out_free;
  1423. BUG_ON(!fs_info->quota_root);
  1424. qgroups = ulist_alloc(GFP_NOFS);
  1425. if (!qgroups) {
  1426. ret = -ENOMEM;
  1427. goto out_free;
  1428. }
  1429. tmp = ulist_alloc(GFP_NOFS);
  1430. if (!tmp) {
  1431. ret = -ENOMEM;
  1432. goto out_free;
  1433. }
  1434. mutex_lock(&fs_info->qgroup_rescan_lock);
  1435. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1436. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1437. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1438. ret = 0;
  1439. goto out_free;
  1440. }
  1441. }
  1442. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1443. spin_lock(&fs_info->qgroup_lock);
  1444. seq = fs_info->qgroup_seq;
  1445. /* Update old refcnts using old_roots */
  1446. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1447. UPDATE_OLD);
  1448. if (ret < 0)
  1449. goto out;
  1450. /* Update new refcnts using new_roots */
  1451. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1452. UPDATE_NEW);
  1453. if (ret < 0)
  1454. goto out;
  1455. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1456. num_bytes, seq);
  1457. /*
  1458. * Bump qgroup_seq to avoid seq overlap
  1459. */
  1460. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1461. out:
  1462. spin_unlock(&fs_info->qgroup_lock);
  1463. out_free:
  1464. ulist_free(tmp);
  1465. ulist_free(qgroups);
  1466. ulist_free(old_roots);
  1467. ulist_free(new_roots);
  1468. return ret;
  1469. }
  1470. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1471. struct btrfs_fs_info *fs_info)
  1472. {
  1473. struct btrfs_qgroup_extent_record *record;
  1474. struct btrfs_delayed_ref_root *delayed_refs;
  1475. struct ulist *new_roots = NULL;
  1476. struct rb_node *node;
  1477. u64 qgroup_to_skip;
  1478. int ret = 0;
  1479. delayed_refs = &trans->transaction->delayed_refs;
  1480. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1481. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1482. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1483. node);
  1484. if (!ret) {
  1485. /*
  1486. * Use (u64)-1 as time_seq to do special search, which
  1487. * doesn't lock tree or delayed_refs and search current
  1488. * root. It's safe inside commit_transaction().
  1489. */
  1490. ret = btrfs_find_all_roots(trans, fs_info,
  1491. record->bytenr, (u64)-1, &new_roots);
  1492. if (ret < 0)
  1493. goto cleanup;
  1494. if (qgroup_to_skip)
  1495. ulist_del(new_roots, qgroup_to_skip, 0);
  1496. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1497. record->bytenr, record->num_bytes,
  1498. record->old_roots, new_roots);
  1499. record->old_roots = NULL;
  1500. new_roots = NULL;
  1501. }
  1502. cleanup:
  1503. ulist_free(record->old_roots);
  1504. ulist_free(new_roots);
  1505. new_roots = NULL;
  1506. rb_erase(node, &delayed_refs->dirty_extent_root);
  1507. kfree(record);
  1508. }
  1509. return ret;
  1510. }
  1511. /*
  1512. * called from commit_transaction. Writes all changed qgroups to disk.
  1513. */
  1514. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1515. struct btrfs_fs_info *fs_info)
  1516. {
  1517. struct btrfs_root *quota_root = fs_info->quota_root;
  1518. int ret = 0;
  1519. int start_rescan_worker = 0;
  1520. if (!quota_root)
  1521. goto out;
  1522. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1523. start_rescan_worker = 1;
  1524. fs_info->quota_enabled = fs_info->pending_quota_state;
  1525. spin_lock(&fs_info->qgroup_lock);
  1526. while (!list_empty(&fs_info->dirty_qgroups)) {
  1527. struct btrfs_qgroup *qgroup;
  1528. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1529. struct btrfs_qgroup, dirty);
  1530. list_del_init(&qgroup->dirty);
  1531. spin_unlock(&fs_info->qgroup_lock);
  1532. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1533. if (ret)
  1534. fs_info->qgroup_flags |=
  1535. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1536. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1537. if (ret)
  1538. fs_info->qgroup_flags |=
  1539. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1540. spin_lock(&fs_info->qgroup_lock);
  1541. }
  1542. if (fs_info->quota_enabled)
  1543. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1544. else
  1545. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1546. spin_unlock(&fs_info->qgroup_lock);
  1547. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1548. if (ret)
  1549. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1550. if (!ret && start_rescan_worker) {
  1551. ret = qgroup_rescan_init(fs_info, 0, 1);
  1552. if (!ret) {
  1553. qgroup_rescan_zero_tracking(fs_info);
  1554. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1555. &fs_info->qgroup_rescan_work);
  1556. }
  1557. ret = 0;
  1558. }
  1559. out:
  1560. return ret;
  1561. }
  1562. /*
  1563. * copy the acounting information between qgroups. This is necessary when a
  1564. * snapshot or a subvolume is created
  1565. */
  1566. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1567. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1568. struct btrfs_qgroup_inherit *inherit)
  1569. {
  1570. int ret = 0;
  1571. int i;
  1572. u64 *i_qgroups;
  1573. struct btrfs_root *quota_root = fs_info->quota_root;
  1574. struct btrfs_qgroup *srcgroup;
  1575. struct btrfs_qgroup *dstgroup;
  1576. u32 level_size = 0;
  1577. u64 nums;
  1578. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1579. if (!fs_info->quota_enabled)
  1580. goto out;
  1581. if (!quota_root) {
  1582. ret = -EINVAL;
  1583. goto out;
  1584. }
  1585. if (inherit) {
  1586. i_qgroups = (u64 *)(inherit + 1);
  1587. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1588. 2 * inherit->num_excl_copies;
  1589. for (i = 0; i < nums; ++i) {
  1590. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1591. if (!srcgroup) {
  1592. ret = -EINVAL;
  1593. goto out;
  1594. }
  1595. if ((srcgroup->qgroupid >> 48) <= (objectid >> 48)) {
  1596. ret = -EINVAL;
  1597. goto out;
  1598. }
  1599. ++i_qgroups;
  1600. }
  1601. }
  1602. /*
  1603. * create a tracking group for the subvol itself
  1604. */
  1605. ret = add_qgroup_item(trans, quota_root, objectid);
  1606. if (ret)
  1607. goto out;
  1608. if (srcid) {
  1609. struct btrfs_root *srcroot;
  1610. struct btrfs_key srckey;
  1611. srckey.objectid = srcid;
  1612. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1613. srckey.offset = (u64)-1;
  1614. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1615. if (IS_ERR(srcroot)) {
  1616. ret = PTR_ERR(srcroot);
  1617. goto out;
  1618. }
  1619. rcu_read_lock();
  1620. level_size = srcroot->nodesize;
  1621. rcu_read_unlock();
  1622. }
  1623. /*
  1624. * add qgroup to all inherited groups
  1625. */
  1626. if (inherit) {
  1627. i_qgroups = (u64 *)(inherit + 1);
  1628. for (i = 0; i < inherit->num_qgroups; ++i) {
  1629. ret = add_qgroup_relation_item(trans, quota_root,
  1630. objectid, *i_qgroups);
  1631. if (ret)
  1632. goto out;
  1633. ret = add_qgroup_relation_item(trans, quota_root,
  1634. *i_qgroups, objectid);
  1635. if (ret)
  1636. goto out;
  1637. ++i_qgroups;
  1638. }
  1639. }
  1640. spin_lock(&fs_info->qgroup_lock);
  1641. dstgroup = add_qgroup_rb(fs_info, objectid);
  1642. if (IS_ERR(dstgroup)) {
  1643. ret = PTR_ERR(dstgroup);
  1644. goto unlock;
  1645. }
  1646. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1647. dstgroup->lim_flags = inherit->lim.flags;
  1648. dstgroup->max_rfer = inherit->lim.max_rfer;
  1649. dstgroup->max_excl = inherit->lim.max_excl;
  1650. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1651. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1652. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1653. if (ret) {
  1654. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1655. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1656. dstgroup->qgroupid);
  1657. goto unlock;
  1658. }
  1659. }
  1660. if (srcid) {
  1661. srcgroup = find_qgroup_rb(fs_info, srcid);
  1662. if (!srcgroup)
  1663. goto unlock;
  1664. /*
  1665. * We call inherit after we clone the root in order to make sure
  1666. * our counts don't go crazy, so at this point the only
  1667. * difference between the two roots should be the root node.
  1668. */
  1669. dstgroup->rfer = srcgroup->rfer;
  1670. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1671. dstgroup->excl = level_size;
  1672. dstgroup->excl_cmpr = level_size;
  1673. srcgroup->excl = level_size;
  1674. srcgroup->excl_cmpr = level_size;
  1675. /* inherit the limit info */
  1676. dstgroup->lim_flags = srcgroup->lim_flags;
  1677. dstgroup->max_rfer = srcgroup->max_rfer;
  1678. dstgroup->max_excl = srcgroup->max_excl;
  1679. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1680. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1681. qgroup_dirty(fs_info, dstgroup);
  1682. qgroup_dirty(fs_info, srcgroup);
  1683. }
  1684. if (!inherit)
  1685. goto unlock;
  1686. i_qgroups = (u64 *)(inherit + 1);
  1687. for (i = 0; i < inherit->num_qgroups; ++i) {
  1688. ret = add_relation_rb(quota_root->fs_info, objectid,
  1689. *i_qgroups);
  1690. if (ret)
  1691. goto unlock;
  1692. ++i_qgroups;
  1693. }
  1694. for (i = 0; i < inherit->num_ref_copies; ++i) {
  1695. struct btrfs_qgroup *src;
  1696. struct btrfs_qgroup *dst;
  1697. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1698. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1699. if (!src || !dst) {
  1700. ret = -EINVAL;
  1701. goto unlock;
  1702. }
  1703. dst->rfer = src->rfer - level_size;
  1704. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1705. i_qgroups += 2;
  1706. }
  1707. for (i = 0; i < inherit->num_excl_copies; ++i) {
  1708. struct btrfs_qgroup *src;
  1709. struct btrfs_qgroup *dst;
  1710. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1711. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1712. if (!src || !dst) {
  1713. ret = -EINVAL;
  1714. goto unlock;
  1715. }
  1716. dst->excl = src->excl + level_size;
  1717. dst->excl_cmpr = src->excl_cmpr + level_size;
  1718. i_qgroups += 2;
  1719. }
  1720. unlock:
  1721. spin_unlock(&fs_info->qgroup_lock);
  1722. out:
  1723. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1724. return ret;
  1725. }
  1726. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1727. {
  1728. struct btrfs_root *quota_root;
  1729. struct btrfs_qgroup *qgroup;
  1730. struct btrfs_fs_info *fs_info = root->fs_info;
  1731. u64 ref_root = root->root_key.objectid;
  1732. int ret = 0;
  1733. struct ulist_node *unode;
  1734. struct ulist_iterator uiter;
  1735. if (!is_fstree(ref_root))
  1736. return 0;
  1737. if (num_bytes == 0)
  1738. return 0;
  1739. spin_lock(&fs_info->qgroup_lock);
  1740. quota_root = fs_info->quota_root;
  1741. if (!quota_root)
  1742. goto out;
  1743. qgroup = find_qgroup_rb(fs_info, ref_root);
  1744. if (!qgroup)
  1745. goto out;
  1746. /*
  1747. * in a first step, we check all affected qgroups if any limits would
  1748. * be exceeded
  1749. */
  1750. ulist_reinit(fs_info->qgroup_ulist);
  1751. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1752. (uintptr_t)qgroup, GFP_ATOMIC);
  1753. if (ret < 0)
  1754. goto out;
  1755. ULIST_ITER_INIT(&uiter);
  1756. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1757. struct btrfs_qgroup *qg;
  1758. struct btrfs_qgroup_list *glist;
  1759. qg = u64_to_ptr(unode->aux);
  1760. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1761. qg->reserved + (s64)qg->rfer + num_bytes >
  1762. qg->max_rfer) {
  1763. ret = -EDQUOT;
  1764. goto out;
  1765. }
  1766. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1767. qg->reserved + (s64)qg->excl + num_bytes >
  1768. qg->max_excl) {
  1769. ret = -EDQUOT;
  1770. goto out;
  1771. }
  1772. list_for_each_entry(glist, &qg->groups, next_group) {
  1773. ret = ulist_add(fs_info->qgroup_ulist,
  1774. glist->group->qgroupid,
  1775. (uintptr_t)glist->group, GFP_ATOMIC);
  1776. if (ret < 0)
  1777. goto out;
  1778. }
  1779. }
  1780. ret = 0;
  1781. /*
  1782. * no limits exceeded, now record the reservation into all qgroups
  1783. */
  1784. ULIST_ITER_INIT(&uiter);
  1785. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1786. struct btrfs_qgroup *qg;
  1787. qg = u64_to_ptr(unode->aux);
  1788. qg->reserved += num_bytes;
  1789. }
  1790. out:
  1791. spin_unlock(&fs_info->qgroup_lock);
  1792. return ret;
  1793. }
  1794. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1795. {
  1796. struct btrfs_root *quota_root;
  1797. struct btrfs_qgroup *qgroup;
  1798. struct btrfs_fs_info *fs_info = root->fs_info;
  1799. struct ulist_node *unode;
  1800. struct ulist_iterator uiter;
  1801. u64 ref_root = root->root_key.objectid;
  1802. int ret = 0;
  1803. if (!is_fstree(ref_root))
  1804. return;
  1805. if (num_bytes == 0)
  1806. return;
  1807. spin_lock(&fs_info->qgroup_lock);
  1808. quota_root = fs_info->quota_root;
  1809. if (!quota_root)
  1810. goto out;
  1811. qgroup = find_qgroup_rb(fs_info, ref_root);
  1812. if (!qgroup)
  1813. goto out;
  1814. ulist_reinit(fs_info->qgroup_ulist);
  1815. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1816. (uintptr_t)qgroup, GFP_ATOMIC);
  1817. if (ret < 0)
  1818. goto out;
  1819. ULIST_ITER_INIT(&uiter);
  1820. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1821. struct btrfs_qgroup *qg;
  1822. struct btrfs_qgroup_list *glist;
  1823. qg = u64_to_ptr(unode->aux);
  1824. qg->reserved -= num_bytes;
  1825. list_for_each_entry(glist, &qg->groups, next_group) {
  1826. ret = ulist_add(fs_info->qgroup_ulist,
  1827. glist->group->qgroupid,
  1828. (uintptr_t)glist->group, GFP_ATOMIC);
  1829. if (ret < 0)
  1830. goto out;
  1831. }
  1832. }
  1833. out:
  1834. spin_unlock(&fs_info->qgroup_lock);
  1835. }
  1836. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1837. {
  1838. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1839. return;
  1840. btrfs_err(trans->root->fs_info,
  1841. "qgroups not uptodate in trans handle %p: list is%s empty, "
  1842. "seq is %#x.%x",
  1843. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1844. (u32)(trans->delayed_ref_elem.seq >> 32),
  1845. (u32)trans->delayed_ref_elem.seq);
  1846. BUG();
  1847. }
  1848. /*
  1849. * returns < 0 on error, 0 when more leafs are to be scanned.
  1850. * returns 1 when done.
  1851. */
  1852. static int
  1853. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1854. struct btrfs_trans_handle *trans,
  1855. struct extent_buffer *scratch_leaf)
  1856. {
  1857. struct btrfs_key found;
  1858. struct ulist *roots = NULL;
  1859. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1860. u64 num_bytes;
  1861. int slot;
  1862. int ret;
  1863. path->leave_spinning = 1;
  1864. mutex_lock(&fs_info->qgroup_rescan_lock);
  1865. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1866. &fs_info->qgroup_rescan_progress,
  1867. path, 1, 0);
  1868. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  1869. fs_info->qgroup_rescan_progress.objectid,
  1870. fs_info->qgroup_rescan_progress.type,
  1871. fs_info->qgroup_rescan_progress.offset, ret);
  1872. if (ret) {
  1873. /*
  1874. * The rescan is about to end, we will not be scanning any
  1875. * further blocks. We cannot unset the RESCAN flag here, because
  1876. * we want to commit the transaction if everything went well.
  1877. * To make the live accounting work in this phase, we set our
  1878. * scan progress pointer such that every real extent objectid
  1879. * will be smaller.
  1880. */
  1881. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1882. btrfs_release_path(path);
  1883. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1884. return ret;
  1885. }
  1886. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1887. btrfs_header_nritems(path->nodes[0]) - 1);
  1888. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1889. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1890. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  1891. slot = path->slots[0];
  1892. btrfs_release_path(path);
  1893. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1894. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1895. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1896. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  1897. found.type != BTRFS_METADATA_ITEM_KEY)
  1898. continue;
  1899. if (found.type == BTRFS_METADATA_ITEM_KEY)
  1900. num_bytes = fs_info->extent_root->nodesize;
  1901. else
  1902. num_bytes = found.offset;
  1903. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  1904. &roots);
  1905. if (ret < 0)
  1906. goto out;
  1907. /* For rescan, just pass old_roots as NULL */
  1908. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1909. found.objectid, num_bytes, NULL, roots);
  1910. if (ret < 0)
  1911. goto out;
  1912. }
  1913. out:
  1914. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1915. return ret;
  1916. }
  1917. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  1918. {
  1919. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  1920. qgroup_rescan_work);
  1921. struct btrfs_path *path;
  1922. struct btrfs_trans_handle *trans = NULL;
  1923. struct extent_buffer *scratch_leaf = NULL;
  1924. int err = -ENOMEM;
  1925. int ret = 0;
  1926. path = btrfs_alloc_path();
  1927. if (!path)
  1928. goto out;
  1929. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  1930. if (!scratch_leaf)
  1931. goto out;
  1932. err = 0;
  1933. while (!err) {
  1934. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  1935. if (IS_ERR(trans)) {
  1936. err = PTR_ERR(trans);
  1937. break;
  1938. }
  1939. if (!fs_info->quota_enabled) {
  1940. err = -EINTR;
  1941. } else {
  1942. err = qgroup_rescan_leaf(fs_info, path, trans,
  1943. scratch_leaf);
  1944. }
  1945. if (err > 0)
  1946. btrfs_commit_transaction(trans, fs_info->fs_root);
  1947. else
  1948. btrfs_end_transaction(trans, fs_info->fs_root);
  1949. }
  1950. out:
  1951. kfree(scratch_leaf);
  1952. btrfs_free_path(path);
  1953. mutex_lock(&fs_info->qgroup_rescan_lock);
  1954. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1955. if (err > 0 &&
  1956. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  1957. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1958. } else if (err < 0) {
  1959. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1960. }
  1961. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1962. /*
  1963. * only update status, since the previous part has alreay updated the
  1964. * qgroup info.
  1965. */
  1966. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  1967. if (IS_ERR(trans)) {
  1968. err = PTR_ERR(trans);
  1969. btrfs_err(fs_info,
  1970. "fail to start transaction for status update: %d\n",
  1971. err);
  1972. goto done;
  1973. }
  1974. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  1975. if (ret < 0) {
  1976. err = ret;
  1977. btrfs_err(fs_info, "fail to update qgroup status: %d\n", err);
  1978. }
  1979. btrfs_end_transaction(trans, fs_info->quota_root);
  1980. if (err >= 0) {
  1981. btrfs_info(fs_info, "qgroup scan completed%s",
  1982. err > 0 ? " (inconsistency flag cleared)" : "");
  1983. } else {
  1984. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  1985. }
  1986. done:
  1987. complete_all(&fs_info->qgroup_rescan_completion);
  1988. }
  1989. /*
  1990. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  1991. * memory required for the rescan context.
  1992. */
  1993. static int
  1994. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  1995. int init_flags)
  1996. {
  1997. int ret = 0;
  1998. if (!init_flags &&
  1999. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2000. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2001. ret = -EINVAL;
  2002. goto err;
  2003. }
  2004. mutex_lock(&fs_info->qgroup_rescan_lock);
  2005. spin_lock(&fs_info->qgroup_lock);
  2006. if (init_flags) {
  2007. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2008. ret = -EINPROGRESS;
  2009. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2010. ret = -EINVAL;
  2011. if (ret) {
  2012. spin_unlock(&fs_info->qgroup_lock);
  2013. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2014. goto err;
  2015. }
  2016. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2017. }
  2018. memset(&fs_info->qgroup_rescan_progress, 0,
  2019. sizeof(fs_info->qgroup_rescan_progress));
  2020. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2021. spin_unlock(&fs_info->qgroup_lock);
  2022. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2023. init_completion(&fs_info->qgroup_rescan_completion);
  2024. memset(&fs_info->qgroup_rescan_work, 0,
  2025. sizeof(fs_info->qgroup_rescan_work));
  2026. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2027. btrfs_qgroup_rescan_helper,
  2028. btrfs_qgroup_rescan_worker, NULL, NULL);
  2029. if (ret) {
  2030. err:
  2031. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2032. return ret;
  2033. }
  2034. return 0;
  2035. }
  2036. static void
  2037. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2038. {
  2039. struct rb_node *n;
  2040. struct btrfs_qgroup *qgroup;
  2041. spin_lock(&fs_info->qgroup_lock);
  2042. /* clear all current qgroup tracking information */
  2043. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2044. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2045. qgroup->rfer = 0;
  2046. qgroup->rfer_cmpr = 0;
  2047. qgroup->excl = 0;
  2048. qgroup->excl_cmpr = 0;
  2049. }
  2050. spin_unlock(&fs_info->qgroup_lock);
  2051. }
  2052. int
  2053. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2054. {
  2055. int ret = 0;
  2056. struct btrfs_trans_handle *trans;
  2057. ret = qgroup_rescan_init(fs_info, 0, 1);
  2058. if (ret)
  2059. return ret;
  2060. /*
  2061. * We have set the rescan_progress to 0, which means no more
  2062. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2063. * However, btrfs_qgroup_account_ref may be right after its call
  2064. * to btrfs_find_all_roots, in which case it would still do the
  2065. * accounting.
  2066. * To solve this, we're committing the transaction, which will
  2067. * ensure we run all delayed refs and only after that, we are
  2068. * going to clear all tracking information for a clean start.
  2069. */
  2070. trans = btrfs_join_transaction(fs_info->fs_root);
  2071. if (IS_ERR(trans)) {
  2072. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2073. return PTR_ERR(trans);
  2074. }
  2075. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2076. if (ret) {
  2077. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2078. return ret;
  2079. }
  2080. qgroup_rescan_zero_tracking(fs_info);
  2081. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2082. &fs_info->qgroup_rescan_work);
  2083. return 0;
  2084. }
  2085. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  2086. {
  2087. int running;
  2088. int ret = 0;
  2089. mutex_lock(&fs_info->qgroup_rescan_lock);
  2090. spin_lock(&fs_info->qgroup_lock);
  2091. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2092. spin_unlock(&fs_info->qgroup_lock);
  2093. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2094. if (running)
  2095. ret = wait_for_completion_interruptible(
  2096. &fs_info->qgroup_rescan_completion);
  2097. return ret;
  2098. }
  2099. /*
  2100. * this is only called from open_ctree where we're still single threaded, thus
  2101. * locking is omitted here.
  2102. */
  2103. void
  2104. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2105. {
  2106. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2107. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2108. &fs_info->qgroup_rescan_work);
  2109. }