qgroup.c 53 KB

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