qgroup.c 50 KB

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