qgroup.c 53 KB

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