qgroup.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /*
  2. * Copyright (C) 2011 STRATO. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/writeback.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/btrfs.h>
  26. #include "ctree.h"
  27. #include "transaction.h"
  28. #include "disk-io.h"
  29. #include "locking.h"
  30. #include "ulist.h"
  31. #include "backref.h"
  32. #include "extent_io.h"
  33. /* TODO XXX FIXME
  34. * - subvol delete -> delete when ref goes to 0? delete limits also?
  35. * - reorganize keys
  36. * - compressed
  37. * - sync
  38. * - copy also limits on subvol creation
  39. * - limit
  40. * - caches fuer ulists
  41. * - performance benchmarks
  42. * - check all ioctl parameters
  43. */
  44. /*
  45. * one struct for each qgroup, organized in fs_info->qgroup_tree.
  46. */
  47. struct btrfs_qgroup {
  48. u64 qgroupid;
  49. /*
  50. * state
  51. */
  52. u64 rfer; /* referenced */
  53. u64 rfer_cmpr; /* referenced compressed */
  54. u64 excl; /* exclusive */
  55. u64 excl_cmpr; /* exclusive compressed */
  56. /*
  57. * limits
  58. */
  59. u64 lim_flags; /* which limits are set */
  60. u64 max_rfer;
  61. u64 max_excl;
  62. u64 rsv_rfer;
  63. u64 rsv_excl;
  64. /*
  65. * reservation tracking
  66. */
  67. u64 reserved;
  68. /*
  69. * lists
  70. */
  71. struct list_head groups; /* groups this group is member of */
  72. struct list_head members; /* groups that are members of this group */
  73. struct list_head dirty; /* dirty groups */
  74. struct rb_node node; /* tree of qgroups */
  75. /*
  76. * temp variables for accounting operations
  77. */
  78. u64 tag;
  79. u64 refcnt;
  80. };
  81. /*
  82. * glue structure to represent the relations between qgroups.
  83. */
  84. struct btrfs_qgroup_list {
  85. struct list_head next_group;
  86. struct list_head next_member;
  87. struct btrfs_qgroup *group;
  88. struct btrfs_qgroup *member;
  89. };
  90. struct qgroup_rescan {
  91. struct btrfs_work work;
  92. struct btrfs_fs_info *fs_info;
  93. };
  94. static void qgroup_rescan_start(struct btrfs_fs_info *fs_info,
  95. struct qgroup_rescan *qscan);
  96. /* must be called with qgroup_ioctl_lock held */
  97. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  98. u64 qgroupid)
  99. {
  100. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  101. struct btrfs_qgroup *qgroup;
  102. while (n) {
  103. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  104. if (qgroup->qgroupid < qgroupid)
  105. n = n->rb_left;
  106. else if (qgroup->qgroupid > qgroupid)
  107. n = n->rb_right;
  108. else
  109. return qgroup;
  110. }
  111. return NULL;
  112. }
  113. /* must be called with qgroup_lock held */
  114. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  115. u64 qgroupid)
  116. {
  117. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  118. struct rb_node *parent = NULL;
  119. struct btrfs_qgroup *qgroup;
  120. while (*p) {
  121. parent = *p;
  122. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  123. if (qgroup->qgroupid < qgroupid)
  124. p = &(*p)->rb_left;
  125. else if (qgroup->qgroupid > qgroupid)
  126. p = &(*p)->rb_right;
  127. else
  128. return qgroup;
  129. }
  130. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  131. if (!qgroup)
  132. return ERR_PTR(-ENOMEM);
  133. qgroup->qgroupid = qgroupid;
  134. INIT_LIST_HEAD(&qgroup->groups);
  135. INIT_LIST_HEAD(&qgroup->members);
  136. INIT_LIST_HEAD(&qgroup->dirty);
  137. rb_link_node(&qgroup->node, parent, p);
  138. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  139. return qgroup;
  140. }
  141. /* must be called with qgroup_lock held */
  142. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  143. {
  144. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  145. struct btrfs_qgroup_list *list;
  146. if (!qgroup)
  147. return -ENOENT;
  148. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  149. list_del(&qgroup->dirty);
  150. while (!list_empty(&qgroup->groups)) {
  151. list = list_first_entry(&qgroup->groups,
  152. struct btrfs_qgroup_list, next_group);
  153. list_del(&list->next_group);
  154. list_del(&list->next_member);
  155. kfree(list);
  156. }
  157. while (!list_empty(&qgroup->members)) {
  158. list = list_first_entry(&qgroup->members,
  159. struct btrfs_qgroup_list, next_member);
  160. list_del(&list->next_group);
  161. list_del(&list->next_member);
  162. kfree(list);
  163. }
  164. kfree(qgroup);
  165. return 0;
  166. }
  167. /* must be called with qgroup_lock held */
  168. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  169. u64 memberid, u64 parentid)
  170. {
  171. struct btrfs_qgroup *member;
  172. struct btrfs_qgroup *parent;
  173. struct btrfs_qgroup_list *list;
  174. member = find_qgroup_rb(fs_info, memberid);
  175. parent = find_qgroup_rb(fs_info, parentid);
  176. if (!member || !parent)
  177. return -ENOENT;
  178. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  179. if (!list)
  180. return -ENOMEM;
  181. list->group = parent;
  182. list->member = member;
  183. list_add_tail(&list->next_group, &member->groups);
  184. list_add_tail(&list->next_member, &parent->members);
  185. return 0;
  186. }
  187. /* must be called with qgroup_lock held */
  188. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  189. u64 memberid, u64 parentid)
  190. {
  191. struct btrfs_qgroup *member;
  192. struct btrfs_qgroup *parent;
  193. struct btrfs_qgroup_list *list;
  194. member = find_qgroup_rb(fs_info, memberid);
  195. parent = find_qgroup_rb(fs_info, parentid);
  196. if (!member || !parent)
  197. return -ENOENT;
  198. list_for_each_entry(list, &member->groups, next_group) {
  199. if (list->group == parent) {
  200. list_del(&list->next_group);
  201. list_del(&list->next_member);
  202. kfree(list);
  203. return 0;
  204. }
  205. }
  206. return -ENOENT;
  207. }
  208. /*
  209. * The full config is read in one go, only called from open_ctree()
  210. * It doesn't use any locking, as at this point we're still single-threaded
  211. */
  212. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  213. {
  214. struct btrfs_key key;
  215. struct btrfs_key found_key;
  216. struct btrfs_root *quota_root = fs_info->quota_root;
  217. struct btrfs_path *path = NULL;
  218. struct extent_buffer *l;
  219. int slot;
  220. int ret = 0;
  221. u64 flags = 0;
  222. if (!fs_info->quota_enabled)
  223. return 0;
  224. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  225. if (!fs_info->qgroup_ulist) {
  226. ret = -ENOMEM;
  227. goto out;
  228. }
  229. path = btrfs_alloc_path();
  230. if (!path) {
  231. ret = -ENOMEM;
  232. goto out;
  233. }
  234. /* default this to quota off, in case no status key is found */
  235. fs_info->qgroup_flags = 0;
  236. /*
  237. * pass 1: read status, all qgroup infos and limits
  238. */
  239. key.objectid = 0;
  240. key.type = 0;
  241. key.offset = 0;
  242. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  243. if (ret)
  244. goto out;
  245. while (1) {
  246. struct btrfs_qgroup *qgroup;
  247. slot = path->slots[0];
  248. l = path->nodes[0];
  249. btrfs_item_key_to_cpu(l, &found_key, slot);
  250. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  251. struct btrfs_qgroup_status_item *ptr;
  252. ptr = btrfs_item_ptr(l, slot,
  253. struct btrfs_qgroup_status_item);
  254. if (btrfs_qgroup_status_version(l, ptr) !=
  255. BTRFS_QGROUP_STATUS_VERSION) {
  256. printk(KERN_ERR
  257. "btrfs: old qgroup version, quota disabled\n");
  258. goto out;
  259. }
  260. if (btrfs_qgroup_status_generation(l, ptr) !=
  261. fs_info->generation) {
  262. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  263. printk(KERN_ERR
  264. "btrfs: qgroup generation mismatch, "
  265. "marked as inconsistent\n");
  266. }
  267. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  268. ptr);
  269. fs_info->qgroup_rescan_progress.objectid =
  270. btrfs_qgroup_status_rescan(l, ptr);
  271. if (fs_info->qgroup_flags &
  272. BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  273. struct qgroup_rescan *qscan =
  274. kmalloc(sizeof(*qscan), GFP_NOFS);
  275. if (!qscan) {
  276. ret = -ENOMEM;
  277. goto out;
  278. }
  279. fs_info->qgroup_rescan_progress.type = 0;
  280. fs_info->qgroup_rescan_progress.offset = 0;
  281. qgroup_rescan_start(fs_info, qscan);
  282. }
  283. goto next1;
  284. }
  285. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  286. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  287. goto next1;
  288. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  289. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  290. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  291. printk(KERN_ERR "btrfs: inconsitent qgroup config\n");
  292. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  293. }
  294. if (!qgroup) {
  295. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  296. if (IS_ERR(qgroup)) {
  297. ret = PTR_ERR(qgroup);
  298. goto out;
  299. }
  300. }
  301. switch (found_key.type) {
  302. case BTRFS_QGROUP_INFO_KEY: {
  303. struct btrfs_qgroup_info_item *ptr;
  304. ptr = btrfs_item_ptr(l, slot,
  305. struct btrfs_qgroup_info_item);
  306. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  307. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  308. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  309. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  310. /* generation currently unused */
  311. break;
  312. }
  313. case BTRFS_QGROUP_LIMIT_KEY: {
  314. struct btrfs_qgroup_limit_item *ptr;
  315. ptr = btrfs_item_ptr(l, slot,
  316. struct btrfs_qgroup_limit_item);
  317. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  318. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  319. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  320. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  321. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  322. break;
  323. }
  324. }
  325. next1:
  326. ret = btrfs_next_item(quota_root, path);
  327. if (ret < 0)
  328. goto out;
  329. if (ret)
  330. break;
  331. }
  332. btrfs_release_path(path);
  333. /*
  334. * pass 2: read all qgroup relations
  335. */
  336. key.objectid = 0;
  337. key.type = BTRFS_QGROUP_RELATION_KEY;
  338. key.offset = 0;
  339. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  340. if (ret)
  341. goto out;
  342. while (1) {
  343. slot = path->slots[0];
  344. l = path->nodes[0];
  345. btrfs_item_key_to_cpu(l, &found_key, slot);
  346. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  347. goto next2;
  348. if (found_key.objectid > found_key.offset) {
  349. /* parent <- member, not needed to build config */
  350. /* FIXME should we omit the key completely? */
  351. goto next2;
  352. }
  353. ret = add_relation_rb(fs_info, found_key.objectid,
  354. found_key.offset);
  355. if (ret == -ENOENT) {
  356. printk(KERN_WARNING
  357. "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
  358. (unsigned long long)found_key.objectid,
  359. (unsigned long long)found_key.offset);
  360. ret = 0; /* ignore the error */
  361. }
  362. if (ret)
  363. goto out;
  364. next2:
  365. ret = btrfs_next_item(quota_root, path);
  366. if (ret < 0)
  367. goto out;
  368. if (ret)
  369. break;
  370. }
  371. out:
  372. fs_info->qgroup_flags |= flags;
  373. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  374. fs_info->quota_enabled = 0;
  375. fs_info->pending_quota_state = 0;
  376. }
  377. btrfs_free_path(path);
  378. if (ret)
  379. ulist_free(fs_info->qgroup_ulist);
  380. return ret < 0 ? ret : 0;
  381. }
  382. /*
  383. * This is only called from close_ctree() or open_ctree(), both in single-
  384. * treaded paths. Clean up the in-memory structures. No locking needed.
  385. */
  386. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  387. {
  388. struct rb_node *n;
  389. struct btrfs_qgroup *qgroup;
  390. struct btrfs_qgroup_list *list;
  391. while ((n = rb_first(&fs_info->qgroup_tree))) {
  392. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  393. rb_erase(n, &fs_info->qgroup_tree);
  394. while (!list_empty(&qgroup->groups)) {
  395. list = list_first_entry(&qgroup->groups,
  396. struct btrfs_qgroup_list,
  397. next_group);
  398. list_del(&list->next_group);
  399. list_del(&list->next_member);
  400. kfree(list);
  401. }
  402. while (!list_empty(&qgroup->members)) {
  403. list = list_first_entry(&qgroup->members,
  404. struct btrfs_qgroup_list,
  405. next_member);
  406. list_del(&list->next_group);
  407. list_del(&list->next_member);
  408. kfree(list);
  409. }
  410. kfree(qgroup);
  411. }
  412. ulist_free(fs_info->qgroup_ulist);
  413. }
  414. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  415. struct btrfs_root *quota_root,
  416. u64 src, u64 dst)
  417. {
  418. int ret;
  419. struct btrfs_path *path;
  420. struct btrfs_key key;
  421. path = btrfs_alloc_path();
  422. if (!path)
  423. return -ENOMEM;
  424. key.objectid = src;
  425. key.type = BTRFS_QGROUP_RELATION_KEY;
  426. key.offset = dst;
  427. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  428. btrfs_mark_buffer_dirty(path->nodes[0]);
  429. btrfs_free_path(path);
  430. return ret;
  431. }
  432. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  433. struct btrfs_root *quota_root,
  434. u64 src, u64 dst)
  435. {
  436. int ret;
  437. struct btrfs_path *path;
  438. struct btrfs_key key;
  439. path = btrfs_alloc_path();
  440. if (!path)
  441. return -ENOMEM;
  442. key.objectid = src;
  443. key.type = BTRFS_QGROUP_RELATION_KEY;
  444. key.offset = dst;
  445. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  446. if (ret < 0)
  447. goto out;
  448. if (ret > 0) {
  449. ret = -ENOENT;
  450. goto out;
  451. }
  452. ret = btrfs_del_item(trans, quota_root, path);
  453. out:
  454. btrfs_free_path(path);
  455. return ret;
  456. }
  457. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  458. struct btrfs_root *quota_root, u64 qgroupid)
  459. {
  460. int ret;
  461. struct btrfs_path *path;
  462. struct btrfs_qgroup_info_item *qgroup_info;
  463. struct btrfs_qgroup_limit_item *qgroup_limit;
  464. struct extent_buffer *leaf;
  465. struct btrfs_key key;
  466. path = btrfs_alloc_path();
  467. if (!path)
  468. return -ENOMEM;
  469. key.objectid = 0;
  470. key.type = BTRFS_QGROUP_INFO_KEY;
  471. key.offset = qgroupid;
  472. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  473. sizeof(*qgroup_info));
  474. if (ret)
  475. goto out;
  476. leaf = path->nodes[0];
  477. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  478. struct btrfs_qgroup_info_item);
  479. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  480. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  481. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  482. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  483. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  484. btrfs_mark_buffer_dirty(leaf);
  485. btrfs_release_path(path);
  486. key.type = BTRFS_QGROUP_LIMIT_KEY;
  487. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  488. sizeof(*qgroup_limit));
  489. if (ret)
  490. goto out;
  491. leaf = path->nodes[0];
  492. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  493. struct btrfs_qgroup_limit_item);
  494. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  495. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  496. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  497. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  498. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  499. btrfs_mark_buffer_dirty(leaf);
  500. ret = 0;
  501. out:
  502. btrfs_free_path(path);
  503. return ret;
  504. }
  505. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  506. struct btrfs_root *quota_root, u64 qgroupid)
  507. {
  508. int ret;
  509. struct btrfs_path *path;
  510. struct btrfs_key key;
  511. path = btrfs_alloc_path();
  512. if (!path)
  513. return -ENOMEM;
  514. key.objectid = 0;
  515. key.type = BTRFS_QGROUP_INFO_KEY;
  516. key.offset = qgroupid;
  517. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  518. if (ret < 0)
  519. goto out;
  520. if (ret > 0) {
  521. ret = -ENOENT;
  522. goto out;
  523. }
  524. ret = btrfs_del_item(trans, quota_root, path);
  525. if (ret)
  526. goto out;
  527. btrfs_release_path(path);
  528. key.type = BTRFS_QGROUP_LIMIT_KEY;
  529. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  530. if (ret < 0)
  531. goto out;
  532. if (ret > 0) {
  533. ret = -ENOENT;
  534. goto out;
  535. }
  536. ret = btrfs_del_item(trans, quota_root, path);
  537. out:
  538. btrfs_free_path(path);
  539. return ret;
  540. }
  541. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  542. struct btrfs_root *root, u64 qgroupid,
  543. u64 flags, u64 max_rfer, u64 max_excl,
  544. u64 rsv_rfer, u64 rsv_excl)
  545. {
  546. struct btrfs_path *path;
  547. struct btrfs_key key;
  548. struct extent_buffer *l;
  549. struct btrfs_qgroup_limit_item *qgroup_limit;
  550. int ret;
  551. int slot;
  552. key.objectid = 0;
  553. key.type = BTRFS_QGROUP_LIMIT_KEY;
  554. key.offset = qgroupid;
  555. path = btrfs_alloc_path();
  556. if (!path)
  557. return -ENOMEM;
  558. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  559. if (ret > 0)
  560. ret = -ENOENT;
  561. if (ret)
  562. goto out;
  563. l = path->nodes[0];
  564. slot = path->slots[0];
  565. qgroup_limit = btrfs_item_ptr(l, path->slots[0],
  566. struct btrfs_qgroup_limit_item);
  567. btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
  568. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
  569. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
  570. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
  571. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
  572. btrfs_mark_buffer_dirty(l);
  573. out:
  574. btrfs_free_path(path);
  575. return ret;
  576. }
  577. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  578. struct btrfs_root *root,
  579. struct btrfs_qgroup *qgroup)
  580. {
  581. struct btrfs_path *path;
  582. struct btrfs_key key;
  583. struct extent_buffer *l;
  584. struct btrfs_qgroup_info_item *qgroup_info;
  585. int ret;
  586. int slot;
  587. key.objectid = 0;
  588. key.type = BTRFS_QGROUP_INFO_KEY;
  589. key.offset = qgroup->qgroupid;
  590. path = btrfs_alloc_path();
  591. if (!path)
  592. return -ENOMEM;
  593. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  594. if (ret > 0)
  595. ret = -ENOENT;
  596. if (ret)
  597. goto out;
  598. l = path->nodes[0];
  599. slot = path->slots[0];
  600. qgroup_info = btrfs_item_ptr(l, path->slots[0],
  601. struct btrfs_qgroup_info_item);
  602. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  603. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  604. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  605. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  606. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  607. btrfs_mark_buffer_dirty(l);
  608. out:
  609. btrfs_free_path(path);
  610. return ret;
  611. }
  612. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  613. struct btrfs_fs_info *fs_info,
  614. struct btrfs_root *root)
  615. {
  616. struct btrfs_path *path;
  617. struct btrfs_key key;
  618. struct extent_buffer *l;
  619. struct btrfs_qgroup_status_item *ptr;
  620. int ret;
  621. int slot;
  622. key.objectid = 0;
  623. key.type = BTRFS_QGROUP_STATUS_KEY;
  624. key.offset = 0;
  625. path = btrfs_alloc_path();
  626. if (!path)
  627. return -ENOMEM;
  628. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  629. if (ret > 0)
  630. ret = -ENOENT;
  631. if (ret)
  632. goto out;
  633. l = path->nodes[0];
  634. slot = path->slots[0];
  635. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  636. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  637. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  638. btrfs_set_qgroup_status_rescan(l, ptr,
  639. fs_info->qgroup_rescan_progress.objectid);
  640. btrfs_mark_buffer_dirty(l);
  641. out:
  642. btrfs_free_path(path);
  643. return ret;
  644. }
  645. /*
  646. * called with qgroup_lock held
  647. */
  648. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  649. struct btrfs_root *root)
  650. {
  651. struct btrfs_path *path;
  652. struct btrfs_key key;
  653. struct extent_buffer *leaf = NULL;
  654. int ret;
  655. int nr = 0;
  656. path = btrfs_alloc_path();
  657. if (!path)
  658. return -ENOMEM;
  659. path->leave_spinning = 1;
  660. key.objectid = 0;
  661. key.offset = 0;
  662. key.type = 0;
  663. while (1) {
  664. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  665. if (ret < 0)
  666. goto out;
  667. leaf = path->nodes[0];
  668. nr = btrfs_header_nritems(leaf);
  669. if (!nr)
  670. break;
  671. /*
  672. * delete the leaf one by one
  673. * since the whole tree is going
  674. * to be deleted.
  675. */
  676. path->slots[0] = 0;
  677. ret = btrfs_del_items(trans, root, path, 0, nr);
  678. if (ret)
  679. goto out;
  680. btrfs_release_path(path);
  681. }
  682. ret = 0;
  683. out:
  684. root->fs_info->pending_quota_state = 0;
  685. btrfs_free_path(path);
  686. return ret;
  687. }
  688. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  689. struct btrfs_fs_info *fs_info)
  690. {
  691. struct btrfs_root *quota_root;
  692. struct btrfs_root *tree_root = fs_info->tree_root;
  693. struct btrfs_path *path = NULL;
  694. struct btrfs_qgroup_status_item *ptr;
  695. struct extent_buffer *leaf;
  696. struct btrfs_key key;
  697. struct btrfs_key found_key;
  698. struct btrfs_qgroup *qgroup = NULL;
  699. int ret = 0;
  700. int slot;
  701. mutex_lock(&fs_info->qgroup_ioctl_lock);
  702. if (fs_info->quota_root) {
  703. fs_info->pending_quota_state = 1;
  704. goto out;
  705. }
  706. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  707. if (!fs_info->qgroup_ulist) {
  708. ret = -ENOMEM;
  709. goto out;
  710. }
  711. /*
  712. * initially create the quota tree
  713. */
  714. quota_root = btrfs_create_tree(trans, fs_info,
  715. BTRFS_QUOTA_TREE_OBJECTID);
  716. if (IS_ERR(quota_root)) {
  717. ret = PTR_ERR(quota_root);
  718. goto out;
  719. }
  720. path = btrfs_alloc_path();
  721. if (!path) {
  722. ret = -ENOMEM;
  723. goto out_free_root;
  724. }
  725. key.objectid = 0;
  726. key.type = BTRFS_QGROUP_STATUS_KEY;
  727. key.offset = 0;
  728. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  729. sizeof(*ptr));
  730. if (ret)
  731. goto out_free_path;
  732. leaf = path->nodes[0];
  733. ptr = btrfs_item_ptr(leaf, path->slots[0],
  734. struct btrfs_qgroup_status_item);
  735. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  736. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  737. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  738. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  739. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  740. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  741. btrfs_mark_buffer_dirty(leaf);
  742. key.objectid = 0;
  743. key.type = BTRFS_ROOT_REF_KEY;
  744. key.offset = 0;
  745. btrfs_release_path(path);
  746. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  747. if (ret > 0)
  748. goto out_add_root;
  749. if (ret < 0)
  750. goto out_free_path;
  751. while (1) {
  752. slot = path->slots[0];
  753. leaf = path->nodes[0];
  754. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  755. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  756. ret = add_qgroup_item(trans, quota_root,
  757. found_key.offset);
  758. if (ret)
  759. goto out_free_path;
  760. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  761. if (IS_ERR(qgroup)) {
  762. ret = PTR_ERR(qgroup);
  763. goto out_free_path;
  764. }
  765. }
  766. ret = btrfs_next_item(tree_root, path);
  767. if (ret < 0)
  768. goto out_free_path;
  769. if (ret)
  770. break;
  771. }
  772. out_add_root:
  773. btrfs_release_path(path);
  774. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  775. if (ret)
  776. goto out_free_path;
  777. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  778. if (IS_ERR(qgroup)) {
  779. ret = PTR_ERR(qgroup);
  780. goto out_free_path;
  781. }
  782. spin_lock(&fs_info->qgroup_lock);
  783. fs_info->quota_root = quota_root;
  784. fs_info->pending_quota_state = 1;
  785. spin_unlock(&fs_info->qgroup_lock);
  786. out_free_path:
  787. btrfs_free_path(path);
  788. out_free_root:
  789. if (ret) {
  790. free_extent_buffer(quota_root->node);
  791. free_extent_buffer(quota_root->commit_root);
  792. kfree(quota_root);
  793. }
  794. out:
  795. if (ret)
  796. ulist_free(fs_info->qgroup_ulist);
  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. mutex_lock(&fs_info->qgroup_rescan_lock);
  1243. spin_lock(&fs_info->qgroup_lock);
  1244. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1245. if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
  1246. ret = 0;
  1247. goto unlock;
  1248. }
  1249. }
  1250. quota_root = fs_info->quota_root;
  1251. if (!quota_root)
  1252. goto unlock;
  1253. qgroup = find_qgroup_rb(fs_info, ref_root);
  1254. if (!qgroup)
  1255. goto unlock;
  1256. /*
  1257. * step 1: for each old ref, visit all nodes once and inc refcnt
  1258. */
  1259. ulist_reinit(fs_info->qgroup_ulist);
  1260. seq = fs_info->qgroup_seq;
  1261. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1262. ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
  1263. seq);
  1264. if (ret)
  1265. goto unlock;
  1266. /*
  1267. * step 2: walk from the new root
  1268. */
  1269. ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
  1270. seq, sgn, node->num_bytes, qgroup);
  1271. if (ret)
  1272. goto unlock;
  1273. /*
  1274. * step 3: walk again from old refs
  1275. */
  1276. ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
  1277. seq, sgn, node->num_bytes);
  1278. if (ret)
  1279. goto unlock;
  1280. unlock:
  1281. spin_unlock(&fs_info->qgroup_lock);
  1282. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1283. ulist_free(roots);
  1284. return ret;
  1285. }
  1286. /*
  1287. * called from commit_transaction. Writes all changed qgroups to disk.
  1288. */
  1289. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1290. struct btrfs_fs_info *fs_info)
  1291. {
  1292. struct btrfs_root *quota_root = fs_info->quota_root;
  1293. int ret = 0;
  1294. int start_rescan_worker = 0;
  1295. if (!quota_root)
  1296. goto out;
  1297. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1298. start_rescan_worker = 1;
  1299. fs_info->quota_enabled = fs_info->pending_quota_state;
  1300. spin_lock(&fs_info->qgroup_lock);
  1301. while (!list_empty(&fs_info->dirty_qgroups)) {
  1302. struct btrfs_qgroup *qgroup;
  1303. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1304. struct btrfs_qgroup, dirty);
  1305. list_del_init(&qgroup->dirty);
  1306. spin_unlock(&fs_info->qgroup_lock);
  1307. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1308. if (ret)
  1309. fs_info->qgroup_flags |=
  1310. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1311. spin_lock(&fs_info->qgroup_lock);
  1312. }
  1313. if (fs_info->quota_enabled)
  1314. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1315. else
  1316. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1317. spin_unlock(&fs_info->qgroup_lock);
  1318. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1319. if (ret)
  1320. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1321. if (!ret && start_rescan_worker) {
  1322. ret = btrfs_qgroup_rescan(fs_info);
  1323. if (ret)
  1324. pr_err("btrfs: start rescan quota failed: %d\n", ret);
  1325. ret = 0;
  1326. }
  1327. out:
  1328. return ret;
  1329. }
  1330. /*
  1331. * copy the acounting information between qgroups. This is necessary when a
  1332. * snapshot or a subvolume is created
  1333. */
  1334. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1335. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1336. struct btrfs_qgroup_inherit *inherit)
  1337. {
  1338. int ret = 0;
  1339. int i;
  1340. u64 *i_qgroups;
  1341. struct btrfs_root *quota_root = fs_info->quota_root;
  1342. struct btrfs_qgroup *srcgroup;
  1343. struct btrfs_qgroup *dstgroup;
  1344. u32 level_size = 0;
  1345. u64 nums;
  1346. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1347. if (!fs_info->quota_enabled)
  1348. goto out;
  1349. if (!quota_root) {
  1350. ret = -EINVAL;
  1351. goto out;
  1352. }
  1353. if (inherit) {
  1354. i_qgroups = (u64 *)(inherit + 1);
  1355. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1356. 2 * inherit->num_excl_copies;
  1357. for (i = 0; i < nums; ++i) {
  1358. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1359. if (!srcgroup) {
  1360. ret = -EINVAL;
  1361. goto out;
  1362. }
  1363. ++i_qgroups;
  1364. }
  1365. }
  1366. /*
  1367. * create a tracking group for the subvol itself
  1368. */
  1369. ret = add_qgroup_item(trans, quota_root, objectid);
  1370. if (ret)
  1371. goto out;
  1372. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1373. ret = update_qgroup_limit_item(trans, quota_root, objectid,
  1374. inherit->lim.flags,
  1375. inherit->lim.max_rfer,
  1376. inherit->lim.max_excl,
  1377. inherit->lim.rsv_rfer,
  1378. inherit->lim.rsv_excl);
  1379. if (ret)
  1380. goto out;
  1381. }
  1382. if (srcid) {
  1383. struct btrfs_root *srcroot;
  1384. struct btrfs_key srckey;
  1385. int srcroot_level;
  1386. srckey.objectid = srcid;
  1387. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1388. srckey.offset = (u64)-1;
  1389. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1390. if (IS_ERR(srcroot)) {
  1391. ret = PTR_ERR(srcroot);
  1392. goto out;
  1393. }
  1394. rcu_read_lock();
  1395. srcroot_level = btrfs_header_level(srcroot->node);
  1396. level_size = btrfs_level_size(srcroot, srcroot_level);
  1397. rcu_read_unlock();
  1398. }
  1399. /*
  1400. * add qgroup to all inherited groups
  1401. */
  1402. if (inherit) {
  1403. i_qgroups = (u64 *)(inherit + 1);
  1404. for (i = 0; i < inherit->num_qgroups; ++i) {
  1405. ret = add_qgroup_relation_item(trans, quota_root,
  1406. objectid, *i_qgroups);
  1407. if (ret)
  1408. goto out;
  1409. ret = add_qgroup_relation_item(trans, quota_root,
  1410. *i_qgroups, objectid);
  1411. if (ret)
  1412. goto out;
  1413. ++i_qgroups;
  1414. }
  1415. }
  1416. spin_lock(&fs_info->qgroup_lock);
  1417. dstgroup = add_qgroup_rb(fs_info, objectid);
  1418. if (IS_ERR(dstgroup)) {
  1419. ret = PTR_ERR(dstgroup);
  1420. goto unlock;
  1421. }
  1422. if (srcid) {
  1423. srcgroup = find_qgroup_rb(fs_info, srcid);
  1424. if (!srcgroup)
  1425. goto unlock;
  1426. dstgroup->rfer = srcgroup->rfer - level_size;
  1427. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
  1428. srcgroup->excl = level_size;
  1429. srcgroup->excl_cmpr = level_size;
  1430. qgroup_dirty(fs_info, dstgroup);
  1431. qgroup_dirty(fs_info, srcgroup);
  1432. }
  1433. if (!inherit)
  1434. goto unlock;
  1435. i_qgroups = (u64 *)(inherit + 1);
  1436. for (i = 0; i < inherit->num_qgroups; ++i) {
  1437. ret = add_relation_rb(quota_root->fs_info, objectid,
  1438. *i_qgroups);
  1439. if (ret)
  1440. goto unlock;
  1441. ++i_qgroups;
  1442. }
  1443. for (i = 0; i < inherit->num_ref_copies; ++i) {
  1444. struct btrfs_qgroup *src;
  1445. struct btrfs_qgroup *dst;
  1446. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1447. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1448. if (!src || !dst) {
  1449. ret = -EINVAL;
  1450. goto unlock;
  1451. }
  1452. dst->rfer = src->rfer - level_size;
  1453. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1454. i_qgroups += 2;
  1455. }
  1456. for (i = 0; i < inherit->num_excl_copies; ++i) {
  1457. struct btrfs_qgroup *src;
  1458. struct btrfs_qgroup *dst;
  1459. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1460. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1461. if (!src || !dst) {
  1462. ret = -EINVAL;
  1463. goto unlock;
  1464. }
  1465. dst->excl = src->excl + level_size;
  1466. dst->excl_cmpr = src->excl_cmpr + level_size;
  1467. i_qgroups += 2;
  1468. }
  1469. unlock:
  1470. spin_unlock(&fs_info->qgroup_lock);
  1471. out:
  1472. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1473. return ret;
  1474. }
  1475. /*
  1476. * reserve some space for a qgroup and all its parents. The reservation takes
  1477. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  1478. * accounting. If not enough space is available, EDQUOT is returned.
  1479. * We assume that the requested space is new for all qgroups.
  1480. */
  1481. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1482. {
  1483. struct btrfs_root *quota_root;
  1484. struct btrfs_qgroup *qgroup;
  1485. struct btrfs_fs_info *fs_info = root->fs_info;
  1486. u64 ref_root = root->root_key.objectid;
  1487. int ret = 0;
  1488. struct ulist_node *unode;
  1489. struct ulist_iterator uiter;
  1490. if (!is_fstree(ref_root))
  1491. return 0;
  1492. if (num_bytes == 0)
  1493. return 0;
  1494. spin_lock(&fs_info->qgroup_lock);
  1495. quota_root = fs_info->quota_root;
  1496. if (!quota_root)
  1497. goto out;
  1498. qgroup = find_qgroup_rb(fs_info, ref_root);
  1499. if (!qgroup)
  1500. goto out;
  1501. /*
  1502. * in a first step, we check all affected qgroups if any limits would
  1503. * be exceeded
  1504. */
  1505. ulist_reinit(fs_info->qgroup_ulist);
  1506. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1507. (uintptr_t)qgroup, GFP_ATOMIC);
  1508. if (ret < 0)
  1509. goto out;
  1510. ULIST_ITER_INIT(&uiter);
  1511. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1512. struct btrfs_qgroup *qg;
  1513. struct btrfs_qgroup_list *glist;
  1514. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1515. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1516. qg->reserved + (s64)qg->rfer + num_bytes >
  1517. qg->max_rfer) {
  1518. ret = -EDQUOT;
  1519. goto out;
  1520. }
  1521. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1522. qg->reserved + (s64)qg->excl + num_bytes >
  1523. qg->max_excl) {
  1524. ret = -EDQUOT;
  1525. goto out;
  1526. }
  1527. list_for_each_entry(glist, &qg->groups, next_group) {
  1528. ret = ulist_add(fs_info->qgroup_ulist,
  1529. glist->group->qgroupid,
  1530. (uintptr_t)glist->group, GFP_ATOMIC);
  1531. if (ret < 0)
  1532. goto out;
  1533. }
  1534. }
  1535. ret = 0;
  1536. /*
  1537. * no limits exceeded, now record the reservation into all qgroups
  1538. */
  1539. ULIST_ITER_INIT(&uiter);
  1540. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1541. struct btrfs_qgroup *qg;
  1542. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1543. qg->reserved += num_bytes;
  1544. }
  1545. out:
  1546. spin_unlock(&fs_info->qgroup_lock);
  1547. return ret;
  1548. }
  1549. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1550. {
  1551. struct btrfs_root *quota_root;
  1552. struct btrfs_qgroup *qgroup;
  1553. struct btrfs_fs_info *fs_info = root->fs_info;
  1554. struct ulist_node *unode;
  1555. struct ulist_iterator uiter;
  1556. u64 ref_root = root->root_key.objectid;
  1557. int ret = 0;
  1558. if (!is_fstree(ref_root))
  1559. return;
  1560. if (num_bytes == 0)
  1561. return;
  1562. spin_lock(&fs_info->qgroup_lock);
  1563. quota_root = fs_info->quota_root;
  1564. if (!quota_root)
  1565. goto out;
  1566. qgroup = find_qgroup_rb(fs_info, ref_root);
  1567. if (!qgroup)
  1568. goto out;
  1569. ulist_reinit(fs_info->qgroup_ulist);
  1570. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1571. (uintptr_t)qgroup, GFP_ATOMIC);
  1572. if (ret < 0)
  1573. goto out;
  1574. ULIST_ITER_INIT(&uiter);
  1575. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1576. struct btrfs_qgroup *qg;
  1577. struct btrfs_qgroup_list *glist;
  1578. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1579. qg->reserved -= num_bytes;
  1580. list_for_each_entry(glist, &qg->groups, next_group) {
  1581. ret = ulist_add(fs_info->qgroup_ulist,
  1582. glist->group->qgroupid,
  1583. (uintptr_t)glist->group, GFP_ATOMIC);
  1584. if (ret < 0)
  1585. goto out;
  1586. }
  1587. }
  1588. out:
  1589. spin_unlock(&fs_info->qgroup_lock);
  1590. }
  1591. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1592. {
  1593. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1594. return;
  1595. pr_err("btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x\n",
  1596. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1597. (u32)(trans->delayed_ref_elem.seq >> 32),
  1598. (u32)trans->delayed_ref_elem.seq);
  1599. BUG();
  1600. }
  1601. /*
  1602. * returns < 0 on error, 0 when more leafs are to be scanned.
  1603. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  1604. */
  1605. static int
  1606. qgroup_rescan_leaf(struct qgroup_rescan *qscan, struct btrfs_path *path,
  1607. struct btrfs_trans_handle *trans, struct ulist *tmp,
  1608. struct extent_buffer *scratch_leaf)
  1609. {
  1610. struct btrfs_key found;
  1611. struct btrfs_fs_info *fs_info = qscan->fs_info;
  1612. struct ulist *roots = NULL;
  1613. struct ulist_node *unode;
  1614. struct ulist_iterator uiter;
  1615. struct seq_list tree_mod_seq_elem = {};
  1616. u64 seq;
  1617. int slot;
  1618. int ret;
  1619. path->leave_spinning = 1;
  1620. mutex_lock(&fs_info->qgroup_rescan_lock);
  1621. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1622. &fs_info->qgroup_rescan_progress,
  1623. path, 1, 0);
  1624. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  1625. (unsigned long long)fs_info->qgroup_rescan_progress.objectid,
  1626. fs_info->qgroup_rescan_progress.type,
  1627. (unsigned long long)fs_info->qgroup_rescan_progress.offset,
  1628. ret);
  1629. if (ret) {
  1630. /*
  1631. * The rescan is about to end, we will not be scanning any
  1632. * further blocks. We cannot unset the RESCAN flag here, because
  1633. * we want to commit the transaction if everything went well.
  1634. * To make the live accounting work in this phase, we set our
  1635. * scan progress pointer such that every real extent objectid
  1636. * will be smaller.
  1637. */
  1638. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1639. btrfs_release_path(path);
  1640. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1641. return ret;
  1642. }
  1643. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1644. btrfs_header_nritems(path->nodes[0]) - 1);
  1645. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1646. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1647. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  1648. slot = path->slots[0];
  1649. btrfs_release_path(path);
  1650. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1651. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1652. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1653. if (found.type != BTRFS_EXTENT_ITEM_KEY)
  1654. continue;
  1655. ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
  1656. tree_mod_seq_elem.seq, &roots);
  1657. if (ret < 0)
  1658. goto out;
  1659. spin_lock(&fs_info->qgroup_lock);
  1660. seq = fs_info->qgroup_seq;
  1661. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1662. ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
  1663. if (ret) {
  1664. spin_unlock(&fs_info->qgroup_lock);
  1665. ulist_free(roots);
  1666. goto out;
  1667. }
  1668. /*
  1669. * step2 of btrfs_qgroup_account_ref works from a single root,
  1670. * we're doing all at once here.
  1671. */
  1672. ulist_reinit(tmp);
  1673. ULIST_ITER_INIT(&uiter);
  1674. while ((unode = ulist_next(roots, &uiter))) {
  1675. struct btrfs_qgroup *qg;
  1676. qg = find_qgroup_rb(fs_info, unode->val);
  1677. if (!qg)
  1678. continue;
  1679. ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
  1680. GFP_ATOMIC);
  1681. if (ret < 0) {
  1682. spin_unlock(&fs_info->qgroup_lock);
  1683. ulist_free(roots);
  1684. goto out;
  1685. }
  1686. }
  1687. /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
  1688. ULIST_ITER_INIT(&uiter);
  1689. while ((unode = ulist_next(tmp, &uiter))) {
  1690. struct btrfs_qgroup *qg;
  1691. struct btrfs_qgroup_list *glist;
  1692. qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
  1693. qg->rfer += found.offset;
  1694. qg->rfer_cmpr += found.offset;
  1695. WARN_ON(qg->tag >= seq);
  1696. if (qg->refcnt - seq == roots->nnodes) {
  1697. qg->excl += found.offset;
  1698. qg->excl_cmpr += found.offset;
  1699. }
  1700. qgroup_dirty(fs_info, qg);
  1701. list_for_each_entry(glist, &qg->groups, next_group) {
  1702. ret = ulist_add(tmp, glist->group->qgroupid,
  1703. (uintptr_t)glist->group,
  1704. GFP_ATOMIC);
  1705. if (ret < 0) {
  1706. spin_unlock(&fs_info->qgroup_lock);
  1707. ulist_free(roots);
  1708. goto out;
  1709. }
  1710. }
  1711. }
  1712. spin_unlock(&fs_info->qgroup_lock);
  1713. ulist_free(roots);
  1714. ret = 0;
  1715. }
  1716. out:
  1717. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1718. return ret;
  1719. }
  1720. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  1721. {
  1722. struct qgroup_rescan *qscan = container_of(work, struct qgroup_rescan,
  1723. work);
  1724. struct btrfs_path *path;
  1725. struct btrfs_trans_handle *trans = NULL;
  1726. struct btrfs_fs_info *fs_info = qscan->fs_info;
  1727. struct ulist *tmp = NULL;
  1728. struct extent_buffer *scratch_leaf = NULL;
  1729. int err = -ENOMEM;
  1730. path = btrfs_alloc_path();
  1731. if (!path)
  1732. goto out;
  1733. tmp = ulist_alloc(GFP_NOFS);
  1734. if (!tmp)
  1735. goto out;
  1736. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  1737. if (!scratch_leaf)
  1738. goto out;
  1739. err = 0;
  1740. while (!err) {
  1741. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  1742. if (IS_ERR(trans)) {
  1743. err = PTR_ERR(trans);
  1744. break;
  1745. }
  1746. if (!fs_info->quota_enabled) {
  1747. err = -EINTR;
  1748. } else {
  1749. err = qgroup_rescan_leaf(qscan, path, trans,
  1750. tmp, scratch_leaf);
  1751. }
  1752. if (err > 0)
  1753. btrfs_commit_transaction(trans, fs_info->fs_root);
  1754. else
  1755. btrfs_end_transaction(trans, fs_info->fs_root);
  1756. }
  1757. out:
  1758. kfree(scratch_leaf);
  1759. ulist_free(tmp);
  1760. btrfs_free_path(path);
  1761. kfree(qscan);
  1762. mutex_lock(&fs_info->qgroup_rescan_lock);
  1763. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1764. if (err == 2 &&
  1765. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  1766. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1767. } else if (err < 0) {
  1768. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1769. }
  1770. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1771. if (err >= 0) {
  1772. pr_info("btrfs: qgroup scan completed%s\n",
  1773. err == 2 ? " (inconsistency flag cleared)" : "");
  1774. } else {
  1775. pr_err("btrfs: qgroup scan failed with %d\n", err);
  1776. }
  1777. complete_all(&fs_info->qgroup_rescan_completion);
  1778. }
  1779. static void
  1780. qgroup_rescan_start(struct btrfs_fs_info *fs_info, struct qgroup_rescan *qscan)
  1781. {
  1782. memset(&qscan->work, 0, sizeof(qscan->work));
  1783. qscan->work.func = btrfs_qgroup_rescan_worker;
  1784. qscan->fs_info = fs_info;
  1785. pr_info("btrfs: qgroup scan started\n");
  1786. btrfs_queue_worker(&fs_info->qgroup_rescan_workers, &qscan->work);
  1787. }
  1788. int
  1789. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  1790. {
  1791. int ret = 0;
  1792. struct rb_node *n;
  1793. struct btrfs_qgroup *qgroup;
  1794. struct qgroup_rescan *qscan = kmalloc(sizeof(*qscan), GFP_NOFS);
  1795. if (!qscan)
  1796. return -ENOMEM;
  1797. mutex_lock(&fs_info->qgroup_rescan_lock);
  1798. spin_lock(&fs_info->qgroup_lock);
  1799. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  1800. ret = -EINPROGRESS;
  1801. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  1802. ret = -EINVAL;
  1803. if (ret) {
  1804. spin_unlock(&fs_info->qgroup_lock);
  1805. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1806. kfree(qscan);
  1807. return ret;
  1808. }
  1809. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1810. memset(&fs_info->qgroup_rescan_progress, 0,
  1811. sizeof(fs_info->qgroup_rescan_progress));
  1812. init_completion(&fs_info->qgroup_rescan_completion);
  1813. /* clear all current qgroup tracking information */
  1814. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  1815. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  1816. qgroup->rfer = 0;
  1817. qgroup->rfer_cmpr = 0;
  1818. qgroup->excl = 0;
  1819. qgroup->excl_cmpr = 0;
  1820. }
  1821. spin_unlock(&fs_info->qgroup_lock);
  1822. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1823. qgroup_rescan_start(fs_info, qscan);
  1824. return 0;
  1825. }
  1826. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  1827. {
  1828. int running;
  1829. int ret = 0;
  1830. mutex_lock(&fs_info->qgroup_rescan_lock);
  1831. spin_lock(&fs_info->qgroup_lock);
  1832. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1833. spin_unlock(&fs_info->qgroup_lock);
  1834. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1835. if (running)
  1836. ret = wait_for_completion_interruptible(
  1837. &fs_info->qgroup_rescan_completion);
  1838. return ret;
  1839. }