qgroup.c 53 KB

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