qgroup.c 53 KB

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