qgroup.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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. #include "qgroup.h"
  34. /* TODO XXX FIXME
  35. * - subvol delete -> delete when ref goes to 0? delete limits also?
  36. * - reorganize keys
  37. * - compressed
  38. * - sync
  39. * - copy also limits on subvol creation
  40. * - limit
  41. * - caches fuer ulists
  42. * - performance benchmarks
  43. * - check all ioctl parameters
  44. */
  45. /*
  46. * one struct for each qgroup, organized in fs_info->qgroup_tree.
  47. */
  48. struct btrfs_qgroup {
  49. u64 qgroupid;
  50. /*
  51. * state
  52. */
  53. u64 rfer; /* referenced */
  54. u64 rfer_cmpr; /* referenced compressed */
  55. u64 excl; /* exclusive */
  56. u64 excl_cmpr; /* exclusive compressed */
  57. /*
  58. * limits
  59. */
  60. u64 lim_flags; /* which limits are set */
  61. u64 max_rfer;
  62. u64 max_excl;
  63. u64 rsv_rfer;
  64. u64 rsv_excl;
  65. /*
  66. * reservation tracking
  67. */
  68. u64 reserved;
  69. /*
  70. * lists
  71. */
  72. struct list_head groups; /* groups this group is member of */
  73. struct list_head members; /* groups that are members of this group */
  74. struct list_head dirty; /* dirty groups */
  75. struct rb_node node; /* tree of qgroups */
  76. /*
  77. * temp variables for accounting operations
  78. * Refer to qgroup_shared_accouting() for details.
  79. */
  80. u64 old_refcnt;
  81. u64 new_refcnt;
  82. };
  83. static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
  84. int mod)
  85. {
  86. if (qg->old_refcnt < seq)
  87. qg->old_refcnt = seq;
  88. qg->old_refcnt += mod;
  89. }
  90. static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
  91. int mod)
  92. {
  93. if (qg->new_refcnt < seq)
  94. qg->new_refcnt = seq;
  95. qg->new_refcnt += mod;
  96. }
  97. static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
  98. {
  99. if (qg->old_refcnt < seq)
  100. return 0;
  101. return qg->old_refcnt - seq;
  102. }
  103. static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
  104. {
  105. if (qg->new_refcnt < seq)
  106. return 0;
  107. return qg->new_refcnt - seq;
  108. }
  109. /*
  110. * glue structure to represent the relations between qgroups.
  111. */
  112. struct btrfs_qgroup_list {
  113. struct list_head next_group;
  114. struct list_head next_member;
  115. struct btrfs_qgroup *group;
  116. struct btrfs_qgroup *member;
  117. };
  118. #define ptr_to_u64(x) ((u64)(uintptr_t)x)
  119. #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
  120. static int
  121. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  122. int init_flags);
  123. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  124. /* must be called with qgroup_ioctl_lock held */
  125. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  126. u64 qgroupid)
  127. {
  128. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  129. struct btrfs_qgroup *qgroup;
  130. while (n) {
  131. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  132. if (qgroup->qgroupid < qgroupid)
  133. n = n->rb_left;
  134. else if (qgroup->qgroupid > qgroupid)
  135. n = n->rb_right;
  136. else
  137. return qgroup;
  138. }
  139. return NULL;
  140. }
  141. /* must be called with qgroup_lock held */
  142. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  143. u64 qgroupid)
  144. {
  145. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  146. struct rb_node *parent = NULL;
  147. struct btrfs_qgroup *qgroup;
  148. while (*p) {
  149. parent = *p;
  150. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  151. if (qgroup->qgroupid < qgroupid)
  152. p = &(*p)->rb_left;
  153. else if (qgroup->qgroupid > qgroupid)
  154. p = &(*p)->rb_right;
  155. else
  156. return qgroup;
  157. }
  158. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  159. if (!qgroup)
  160. return ERR_PTR(-ENOMEM);
  161. qgroup->qgroupid = qgroupid;
  162. INIT_LIST_HEAD(&qgroup->groups);
  163. INIT_LIST_HEAD(&qgroup->members);
  164. INIT_LIST_HEAD(&qgroup->dirty);
  165. rb_link_node(&qgroup->node, parent, p);
  166. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  167. return qgroup;
  168. }
  169. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  170. {
  171. struct btrfs_qgroup_list *list;
  172. list_del(&qgroup->dirty);
  173. while (!list_empty(&qgroup->groups)) {
  174. list = list_first_entry(&qgroup->groups,
  175. struct btrfs_qgroup_list, next_group);
  176. list_del(&list->next_group);
  177. list_del(&list->next_member);
  178. kfree(list);
  179. }
  180. while (!list_empty(&qgroup->members)) {
  181. list = list_first_entry(&qgroup->members,
  182. struct btrfs_qgroup_list, next_member);
  183. list_del(&list->next_group);
  184. list_del(&list->next_member);
  185. kfree(list);
  186. }
  187. kfree(qgroup);
  188. }
  189. /* must be called with qgroup_lock held */
  190. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  191. {
  192. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  193. if (!qgroup)
  194. return -ENOENT;
  195. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  196. __del_qgroup_rb(qgroup);
  197. return 0;
  198. }
  199. /* must be called with qgroup_lock held */
  200. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  201. u64 memberid, u64 parentid)
  202. {
  203. struct btrfs_qgroup *member;
  204. struct btrfs_qgroup *parent;
  205. struct btrfs_qgroup_list *list;
  206. member = find_qgroup_rb(fs_info, memberid);
  207. parent = find_qgroup_rb(fs_info, parentid);
  208. if (!member || !parent)
  209. return -ENOENT;
  210. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  211. if (!list)
  212. return -ENOMEM;
  213. list->group = parent;
  214. list->member = member;
  215. list_add_tail(&list->next_group, &member->groups);
  216. list_add_tail(&list->next_member, &parent->members);
  217. return 0;
  218. }
  219. /* must be called with qgroup_lock held */
  220. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  221. u64 memberid, u64 parentid)
  222. {
  223. struct btrfs_qgroup *member;
  224. struct btrfs_qgroup *parent;
  225. struct btrfs_qgroup_list *list;
  226. member = find_qgroup_rb(fs_info, memberid);
  227. parent = find_qgroup_rb(fs_info, parentid);
  228. if (!member || !parent)
  229. return -ENOENT;
  230. list_for_each_entry(list, &member->groups, next_group) {
  231. if (list->group == parent) {
  232. list_del(&list->next_group);
  233. list_del(&list->next_member);
  234. kfree(list);
  235. return 0;
  236. }
  237. }
  238. return -ENOENT;
  239. }
  240. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  241. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  242. u64 rfer, u64 excl)
  243. {
  244. struct btrfs_qgroup *qgroup;
  245. qgroup = find_qgroup_rb(fs_info, qgroupid);
  246. if (!qgroup)
  247. return -EINVAL;
  248. if (qgroup->rfer != rfer || qgroup->excl != excl)
  249. return -EINVAL;
  250. return 0;
  251. }
  252. #endif
  253. /*
  254. * The full config is read in one go, only called from open_ctree()
  255. * It doesn't use any locking, as at this point we're still single-threaded
  256. */
  257. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  258. {
  259. struct btrfs_key key;
  260. struct btrfs_key found_key;
  261. struct btrfs_root *quota_root = fs_info->quota_root;
  262. struct btrfs_path *path = NULL;
  263. struct extent_buffer *l;
  264. int slot;
  265. int ret = 0;
  266. u64 flags = 0;
  267. u64 rescan_progress = 0;
  268. if (!fs_info->quota_enabled)
  269. return 0;
  270. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  271. if (!fs_info->qgroup_ulist) {
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. path = btrfs_alloc_path();
  276. if (!path) {
  277. ret = -ENOMEM;
  278. goto out;
  279. }
  280. /* default this to quota off, in case no status key is found */
  281. fs_info->qgroup_flags = 0;
  282. /*
  283. * pass 1: read status, all qgroup infos and limits
  284. */
  285. key.objectid = 0;
  286. key.type = 0;
  287. key.offset = 0;
  288. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  289. if (ret)
  290. goto out;
  291. while (1) {
  292. struct btrfs_qgroup *qgroup;
  293. slot = path->slots[0];
  294. l = path->nodes[0];
  295. btrfs_item_key_to_cpu(l, &found_key, slot);
  296. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  297. struct btrfs_qgroup_status_item *ptr;
  298. ptr = btrfs_item_ptr(l, slot,
  299. struct btrfs_qgroup_status_item);
  300. if (btrfs_qgroup_status_version(l, ptr) !=
  301. BTRFS_QGROUP_STATUS_VERSION) {
  302. btrfs_err(fs_info,
  303. "old qgroup version, quota disabled");
  304. goto out;
  305. }
  306. if (btrfs_qgroup_status_generation(l, ptr) !=
  307. fs_info->generation) {
  308. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  309. btrfs_err(fs_info,
  310. "qgroup generation mismatch, "
  311. "marked as inconsistent");
  312. }
  313. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  314. ptr);
  315. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  316. goto next1;
  317. }
  318. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  319. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  320. goto next1;
  321. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  322. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  323. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  324. btrfs_err(fs_info, "inconsistent qgroup config");
  325. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  326. }
  327. if (!qgroup) {
  328. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  329. if (IS_ERR(qgroup)) {
  330. ret = PTR_ERR(qgroup);
  331. goto out;
  332. }
  333. }
  334. switch (found_key.type) {
  335. case BTRFS_QGROUP_INFO_KEY: {
  336. struct btrfs_qgroup_info_item *ptr;
  337. ptr = btrfs_item_ptr(l, slot,
  338. struct btrfs_qgroup_info_item);
  339. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  340. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  341. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  342. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  343. /* generation currently unused */
  344. break;
  345. }
  346. case BTRFS_QGROUP_LIMIT_KEY: {
  347. struct btrfs_qgroup_limit_item *ptr;
  348. ptr = btrfs_item_ptr(l, slot,
  349. struct btrfs_qgroup_limit_item);
  350. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  351. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  352. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  353. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  354. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  355. break;
  356. }
  357. }
  358. next1:
  359. ret = btrfs_next_item(quota_root, path);
  360. if (ret < 0)
  361. goto out;
  362. if (ret)
  363. break;
  364. }
  365. btrfs_release_path(path);
  366. /*
  367. * pass 2: read all qgroup relations
  368. */
  369. key.objectid = 0;
  370. key.type = BTRFS_QGROUP_RELATION_KEY;
  371. key.offset = 0;
  372. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  373. if (ret)
  374. goto out;
  375. while (1) {
  376. slot = path->slots[0];
  377. l = path->nodes[0];
  378. btrfs_item_key_to_cpu(l, &found_key, slot);
  379. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  380. goto next2;
  381. if (found_key.objectid > found_key.offset) {
  382. /* parent <- member, not needed to build config */
  383. /* FIXME should we omit the key completely? */
  384. goto next2;
  385. }
  386. ret = add_relation_rb(fs_info, found_key.objectid,
  387. found_key.offset);
  388. if (ret == -ENOENT) {
  389. btrfs_warn(fs_info,
  390. "orphan qgroup relation 0x%llx->0x%llx",
  391. found_key.objectid, found_key.offset);
  392. ret = 0; /* ignore the error */
  393. }
  394. if (ret)
  395. goto out;
  396. next2:
  397. ret = btrfs_next_item(quota_root, path);
  398. if (ret < 0)
  399. goto out;
  400. if (ret)
  401. break;
  402. }
  403. out:
  404. fs_info->qgroup_flags |= flags;
  405. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  406. fs_info->quota_enabled = 0;
  407. fs_info->pending_quota_state = 0;
  408. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  409. ret >= 0) {
  410. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  411. }
  412. btrfs_free_path(path);
  413. if (ret < 0) {
  414. ulist_free(fs_info->qgroup_ulist);
  415. fs_info->qgroup_ulist = NULL;
  416. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  417. }
  418. return ret < 0 ? ret : 0;
  419. }
  420. /*
  421. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  422. * first two are in single-threaded paths.And for the third one, we have set
  423. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  424. * up the in-memory structures without qgroup_lock held.
  425. */
  426. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  427. {
  428. struct rb_node *n;
  429. struct btrfs_qgroup *qgroup;
  430. while ((n = rb_first(&fs_info->qgroup_tree))) {
  431. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  432. rb_erase(n, &fs_info->qgroup_tree);
  433. __del_qgroup_rb(qgroup);
  434. }
  435. /*
  436. * we call btrfs_free_qgroup_config() when umounting
  437. * filesystem and disabling quota, so we set qgroup_ulit
  438. * to be null here to avoid double free.
  439. */
  440. ulist_free(fs_info->qgroup_ulist);
  441. fs_info->qgroup_ulist = NULL;
  442. }
  443. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  444. struct btrfs_root *quota_root,
  445. u64 src, u64 dst)
  446. {
  447. int ret;
  448. struct btrfs_path *path;
  449. struct btrfs_key key;
  450. path = btrfs_alloc_path();
  451. if (!path)
  452. return -ENOMEM;
  453. key.objectid = src;
  454. key.type = BTRFS_QGROUP_RELATION_KEY;
  455. key.offset = dst;
  456. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  457. btrfs_mark_buffer_dirty(path->nodes[0]);
  458. btrfs_free_path(path);
  459. return ret;
  460. }
  461. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  462. struct btrfs_root *quota_root,
  463. u64 src, u64 dst)
  464. {
  465. int ret;
  466. struct btrfs_path *path;
  467. struct btrfs_key key;
  468. path = btrfs_alloc_path();
  469. if (!path)
  470. return -ENOMEM;
  471. key.objectid = src;
  472. key.type = BTRFS_QGROUP_RELATION_KEY;
  473. key.offset = dst;
  474. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  475. if (ret < 0)
  476. goto out;
  477. if (ret > 0) {
  478. ret = -ENOENT;
  479. goto out;
  480. }
  481. ret = btrfs_del_item(trans, quota_root, path);
  482. out:
  483. btrfs_free_path(path);
  484. return ret;
  485. }
  486. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  487. struct btrfs_root *quota_root, u64 qgroupid)
  488. {
  489. int ret;
  490. struct btrfs_path *path;
  491. struct btrfs_qgroup_info_item *qgroup_info;
  492. struct btrfs_qgroup_limit_item *qgroup_limit;
  493. struct extent_buffer *leaf;
  494. struct btrfs_key key;
  495. if (btrfs_test_is_dummy_root(quota_root))
  496. return 0;
  497. path = btrfs_alloc_path();
  498. if (!path)
  499. return -ENOMEM;
  500. key.objectid = 0;
  501. key.type = BTRFS_QGROUP_INFO_KEY;
  502. key.offset = qgroupid;
  503. /*
  504. * Avoid a transaction abort by catching -EEXIST here. In that
  505. * case, we proceed by re-initializing the existing structure
  506. * on disk.
  507. */
  508. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  509. sizeof(*qgroup_info));
  510. if (ret && ret != -EEXIST)
  511. goto out;
  512. leaf = path->nodes[0];
  513. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  514. struct btrfs_qgroup_info_item);
  515. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  516. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  517. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  518. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  519. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  520. btrfs_mark_buffer_dirty(leaf);
  521. btrfs_release_path(path);
  522. key.type = BTRFS_QGROUP_LIMIT_KEY;
  523. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  524. sizeof(*qgroup_limit));
  525. if (ret && ret != -EEXIST)
  526. goto out;
  527. leaf = path->nodes[0];
  528. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  529. struct btrfs_qgroup_limit_item);
  530. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  531. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  532. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  533. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  534. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  535. btrfs_mark_buffer_dirty(leaf);
  536. ret = 0;
  537. out:
  538. btrfs_free_path(path);
  539. return ret;
  540. }
  541. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  542. struct btrfs_root *quota_root, u64 qgroupid)
  543. {
  544. int ret;
  545. struct btrfs_path *path;
  546. struct btrfs_key key;
  547. path = btrfs_alloc_path();
  548. if (!path)
  549. return -ENOMEM;
  550. key.objectid = 0;
  551. key.type = BTRFS_QGROUP_INFO_KEY;
  552. key.offset = qgroupid;
  553. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  554. if (ret < 0)
  555. goto out;
  556. if (ret > 0) {
  557. ret = -ENOENT;
  558. goto out;
  559. }
  560. ret = btrfs_del_item(trans, quota_root, path);
  561. if (ret)
  562. goto out;
  563. btrfs_release_path(path);
  564. key.type = BTRFS_QGROUP_LIMIT_KEY;
  565. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  566. if (ret < 0)
  567. goto out;
  568. if (ret > 0) {
  569. ret = -ENOENT;
  570. goto out;
  571. }
  572. ret = btrfs_del_item(trans, quota_root, path);
  573. out:
  574. btrfs_free_path(path);
  575. return ret;
  576. }
  577. static int update_qgroup_limit_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_limit_item *qgroup_limit;
  585. int ret;
  586. int slot;
  587. key.objectid = 0;
  588. key.type = BTRFS_QGROUP_LIMIT_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_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  601. btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
  602. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
  603. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
  604. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
  605. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
  606. btrfs_mark_buffer_dirty(l);
  607. out:
  608. btrfs_free_path(path);
  609. return ret;
  610. }
  611. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  612. struct btrfs_root *root,
  613. struct btrfs_qgroup *qgroup)
  614. {
  615. struct btrfs_path *path;
  616. struct btrfs_key key;
  617. struct extent_buffer *l;
  618. struct btrfs_qgroup_info_item *qgroup_info;
  619. int ret;
  620. int slot;
  621. if (btrfs_test_is_dummy_root(root))
  622. return 0;
  623. key.objectid = 0;
  624. key.type = BTRFS_QGROUP_INFO_KEY;
  625. key.offset = qgroup->qgroupid;
  626. path = btrfs_alloc_path();
  627. if (!path)
  628. return -ENOMEM;
  629. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  630. if (ret > 0)
  631. ret = -ENOENT;
  632. if (ret)
  633. goto out;
  634. l = path->nodes[0];
  635. slot = path->slots[0];
  636. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  637. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  638. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  639. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  640. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  641. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  642. btrfs_mark_buffer_dirty(l);
  643. out:
  644. btrfs_free_path(path);
  645. return ret;
  646. }
  647. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  648. struct btrfs_fs_info *fs_info,
  649. struct btrfs_root *root)
  650. {
  651. struct btrfs_path *path;
  652. struct btrfs_key key;
  653. struct extent_buffer *l;
  654. struct btrfs_qgroup_status_item *ptr;
  655. int ret;
  656. int slot;
  657. key.objectid = 0;
  658. key.type = BTRFS_QGROUP_STATUS_KEY;
  659. key.offset = 0;
  660. path = btrfs_alloc_path();
  661. if (!path)
  662. return -ENOMEM;
  663. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  664. if (ret > 0)
  665. ret = -ENOENT;
  666. if (ret)
  667. goto out;
  668. l = path->nodes[0];
  669. slot = path->slots[0];
  670. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  671. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  672. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  673. btrfs_set_qgroup_status_rescan(l, ptr,
  674. fs_info->qgroup_rescan_progress.objectid);
  675. btrfs_mark_buffer_dirty(l);
  676. out:
  677. btrfs_free_path(path);
  678. return ret;
  679. }
  680. /*
  681. * called with qgroup_lock held
  682. */
  683. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  684. struct btrfs_root *root)
  685. {
  686. struct btrfs_path *path;
  687. struct btrfs_key key;
  688. struct extent_buffer *leaf = NULL;
  689. int ret;
  690. int nr = 0;
  691. path = btrfs_alloc_path();
  692. if (!path)
  693. return -ENOMEM;
  694. path->leave_spinning = 1;
  695. key.objectid = 0;
  696. key.offset = 0;
  697. key.type = 0;
  698. while (1) {
  699. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  700. if (ret < 0)
  701. goto out;
  702. leaf = path->nodes[0];
  703. nr = btrfs_header_nritems(leaf);
  704. if (!nr)
  705. break;
  706. /*
  707. * delete the leaf one by one
  708. * since the whole tree is going
  709. * to be deleted.
  710. */
  711. path->slots[0] = 0;
  712. ret = btrfs_del_items(trans, root, path, 0, nr);
  713. if (ret)
  714. goto out;
  715. btrfs_release_path(path);
  716. }
  717. ret = 0;
  718. out:
  719. root->fs_info->pending_quota_state = 0;
  720. btrfs_free_path(path);
  721. return ret;
  722. }
  723. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  724. struct btrfs_fs_info *fs_info)
  725. {
  726. struct btrfs_root *quota_root;
  727. struct btrfs_root *tree_root = fs_info->tree_root;
  728. struct btrfs_path *path = NULL;
  729. struct btrfs_qgroup_status_item *ptr;
  730. struct extent_buffer *leaf;
  731. struct btrfs_key key;
  732. struct btrfs_key found_key;
  733. struct btrfs_qgroup *qgroup = NULL;
  734. int ret = 0;
  735. int slot;
  736. mutex_lock(&fs_info->qgroup_ioctl_lock);
  737. if (fs_info->quota_root) {
  738. fs_info->pending_quota_state = 1;
  739. goto out;
  740. }
  741. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  742. if (!fs_info->qgroup_ulist) {
  743. ret = -ENOMEM;
  744. goto out;
  745. }
  746. /*
  747. * initially create the quota tree
  748. */
  749. quota_root = btrfs_create_tree(trans, fs_info,
  750. BTRFS_QUOTA_TREE_OBJECTID);
  751. if (IS_ERR(quota_root)) {
  752. ret = PTR_ERR(quota_root);
  753. goto out;
  754. }
  755. path = btrfs_alloc_path();
  756. if (!path) {
  757. ret = -ENOMEM;
  758. goto out_free_root;
  759. }
  760. key.objectid = 0;
  761. key.type = BTRFS_QGROUP_STATUS_KEY;
  762. key.offset = 0;
  763. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  764. sizeof(*ptr));
  765. if (ret)
  766. goto out_free_path;
  767. leaf = path->nodes[0];
  768. ptr = btrfs_item_ptr(leaf, path->slots[0],
  769. struct btrfs_qgroup_status_item);
  770. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  771. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  772. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  773. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  774. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  775. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  776. btrfs_mark_buffer_dirty(leaf);
  777. key.objectid = 0;
  778. key.type = BTRFS_ROOT_REF_KEY;
  779. key.offset = 0;
  780. btrfs_release_path(path);
  781. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  782. if (ret > 0)
  783. goto out_add_root;
  784. if (ret < 0)
  785. goto out_free_path;
  786. while (1) {
  787. slot = path->slots[0];
  788. leaf = path->nodes[0];
  789. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  790. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  791. ret = add_qgroup_item(trans, quota_root,
  792. found_key.offset);
  793. if (ret)
  794. goto out_free_path;
  795. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  796. if (IS_ERR(qgroup)) {
  797. ret = PTR_ERR(qgroup);
  798. goto out_free_path;
  799. }
  800. }
  801. ret = btrfs_next_item(tree_root, path);
  802. if (ret < 0)
  803. goto out_free_path;
  804. if (ret)
  805. break;
  806. }
  807. out_add_root:
  808. btrfs_release_path(path);
  809. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  810. if (ret)
  811. goto out_free_path;
  812. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  813. if (IS_ERR(qgroup)) {
  814. ret = PTR_ERR(qgroup);
  815. goto out_free_path;
  816. }
  817. spin_lock(&fs_info->qgroup_lock);
  818. fs_info->quota_root = quota_root;
  819. fs_info->pending_quota_state = 1;
  820. spin_unlock(&fs_info->qgroup_lock);
  821. out_free_path:
  822. btrfs_free_path(path);
  823. out_free_root:
  824. if (ret) {
  825. free_extent_buffer(quota_root->node);
  826. free_extent_buffer(quota_root->commit_root);
  827. kfree(quota_root);
  828. }
  829. out:
  830. if (ret) {
  831. ulist_free(fs_info->qgroup_ulist);
  832. fs_info->qgroup_ulist = NULL;
  833. }
  834. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  835. return ret;
  836. }
  837. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  838. struct btrfs_fs_info *fs_info)
  839. {
  840. struct btrfs_root *tree_root = fs_info->tree_root;
  841. struct btrfs_root *quota_root;
  842. int ret = 0;
  843. mutex_lock(&fs_info->qgroup_ioctl_lock);
  844. if (!fs_info->quota_root)
  845. goto out;
  846. spin_lock(&fs_info->qgroup_lock);
  847. fs_info->quota_enabled = 0;
  848. fs_info->pending_quota_state = 0;
  849. quota_root = fs_info->quota_root;
  850. fs_info->quota_root = NULL;
  851. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  852. spin_unlock(&fs_info->qgroup_lock);
  853. btrfs_free_qgroup_config(fs_info);
  854. ret = btrfs_clean_quota_tree(trans, quota_root);
  855. if (ret)
  856. goto out;
  857. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  858. if (ret)
  859. goto out;
  860. list_del(&quota_root->dirty_list);
  861. btrfs_tree_lock(quota_root->node);
  862. clean_tree_block(trans, tree_root->fs_info, quota_root->node);
  863. btrfs_tree_unlock(quota_root->node);
  864. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  865. free_extent_buffer(quota_root->node);
  866. free_extent_buffer(quota_root->commit_root);
  867. kfree(quota_root);
  868. out:
  869. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  870. return ret;
  871. }
  872. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  873. struct btrfs_qgroup *qgroup)
  874. {
  875. if (list_empty(&qgroup->dirty))
  876. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  877. }
  878. /*
  879. * The easy accounting, if we are adding/removing the only ref for an extent
  880. * then this qgroup and all of the parent qgroups get their refrence and
  881. * exclusive counts adjusted.
  882. *
  883. * Caller should hold fs_info->qgroup_lock.
  884. */
  885. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  886. struct ulist *tmp, u64 ref_root,
  887. u64 num_bytes, int sign)
  888. {
  889. struct btrfs_qgroup *qgroup;
  890. struct btrfs_qgroup_list *glist;
  891. struct ulist_node *unode;
  892. struct ulist_iterator uiter;
  893. int ret = 0;
  894. qgroup = find_qgroup_rb(fs_info, ref_root);
  895. if (!qgroup)
  896. goto out;
  897. qgroup->rfer += sign * num_bytes;
  898. qgroup->rfer_cmpr += sign * num_bytes;
  899. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  900. qgroup->excl += sign * num_bytes;
  901. qgroup->excl_cmpr += sign * num_bytes;
  902. if (sign > 0)
  903. qgroup->reserved -= num_bytes;
  904. qgroup_dirty(fs_info, qgroup);
  905. /* Get all of the parent groups that contain this qgroup */
  906. list_for_each_entry(glist, &qgroup->groups, next_group) {
  907. ret = ulist_add(tmp, glist->group->qgroupid,
  908. ptr_to_u64(glist->group), GFP_ATOMIC);
  909. if (ret < 0)
  910. goto out;
  911. }
  912. /* Iterate all of the parents and adjust their reference counts */
  913. ULIST_ITER_INIT(&uiter);
  914. while ((unode = ulist_next(tmp, &uiter))) {
  915. qgroup = u64_to_ptr(unode->aux);
  916. qgroup->rfer += sign * num_bytes;
  917. qgroup->rfer_cmpr += sign * num_bytes;
  918. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  919. qgroup->excl += sign * num_bytes;
  920. if (sign > 0)
  921. qgroup->reserved -= num_bytes;
  922. qgroup->excl_cmpr += sign * num_bytes;
  923. qgroup_dirty(fs_info, qgroup);
  924. /* Add any parents of the parents */
  925. list_for_each_entry(glist, &qgroup->groups, next_group) {
  926. ret = ulist_add(tmp, glist->group->qgroupid,
  927. ptr_to_u64(glist->group), GFP_ATOMIC);
  928. if (ret < 0)
  929. goto out;
  930. }
  931. }
  932. ret = 0;
  933. out:
  934. return ret;
  935. }
  936. /*
  937. * Quick path for updating qgroup with only excl refs.
  938. *
  939. * In that case, just update all parent will be enough.
  940. * Or we needs to do a full rescan.
  941. * Caller should also hold fs_info->qgroup_lock.
  942. *
  943. * Return 0 for quick update, return >0 for need to full rescan
  944. * and mark INCONSISTENT flag.
  945. * Return < 0 for other error.
  946. */
  947. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  948. struct ulist *tmp, u64 src, u64 dst,
  949. int sign)
  950. {
  951. struct btrfs_qgroup *qgroup;
  952. int ret = 1;
  953. int err = 0;
  954. qgroup = find_qgroup_rb(fs_info, src);
  955. if (!qgroup)
  956. goto out;
  957. if (qgroup->excl == qgroup->rfer) {
  958. ret = 0;
  959. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  960. qgroup->excl, sign);
  961. if (err < 0) {
  962. ret = err;
  963. goto out;
  964. }
  965. }
  966. out:
  967. if (ret)
  968. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  969. return ret;
  970. }
  971. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  972. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  973. {
  974. struct btrfs_root *quota_root;
  975. struct btrfs_qgroup *parent;
  976. struct btrfs_qgroup *member;
  977. struct btrfs_qgroup_list *list;
  978. struct ulist *tmp;
  979. int ret = 0;
  980. /* Check the level of src and dst first */
  981. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  982. return -EINVAL;
  983. tmp = ulist_alloc(GFP_NOFS);
  984. if (!tmp)
  985. return -ENOMEM;
  986. mutex_lock(&fs_info->qgroup_ioctl_lock);
  987. quota_root = fs_info->quota_root;
  988. if (!quota_root) {
  989. ret = -EINVAL;
  990. goto out;
  991. }
  992. member = find_qgroup_rb(fs_info, src);
  993. parent = find_qgroup_rb(fs_info, dst);
  994. if (!member || !parent) {
  995. ret = -EINVAL;
  996. goto out;
  997. }
  998. /* check if such qgroup relation exist firstly */
  999. list_for_each_entry(list, &member->groups, next_group) {
  1000. if (list->group == parent) {
  1001. ret = -EEXIST;
  1002. goto out;
  1003. }
  1004. }
  1005. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  1006. if (ret)
  1007. goto out;
  1008. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  1009. if (ret) {
  1010. del_qgroup_relation_item(trans, quota_root, src, dst);
  1011. goto out;
  1012. }
  1013. spin_lock(&fs_info->qgroup_lock);
  1014. ret = add_relation_rb(quota_root->fs_info, src, dst);
  1015. if (ret < 0) {
  1016. spin_unlock(&fs_info->qgroup_lock);
  1017. goto out;
  1018. }
  1019. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1020. spin_unlock(&fs_info->qgroup_lock);
  1021. out:
  1022. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1023. ulist_free(tmp);
  1024. return ret;
  1025. }
  1026. int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1027. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1028. {
  1029. struct btrfs_root *quota_root;
  1030. struct btrfs_qgroup *parent;
  1031. struct btrfs_qgroup *member;
  1032. struct btrfs_qgroup_list *list;
  1033. struct ulist *tmp;
  1034. int ret = 0;
  1035. int err;
  1036. tmp = ulist_alloc(GFP_NOFS);
  1037. if (!tmp)
  1038. return -ENOMEM;
  1039. quota_root = fs_info->quota_root;
  1040. if (!quota_root) {
  1041. ret = -EINVAL;
  1042. goto out;
  1043. }
  1044. member = find_qgroup_rb(fs_info, src);
  1045. parent = find_qgroup_rb(fs_info, dst);
  1046. if (!member || !parent) {
  1047. ret = -EINVAL;
  1048. goto out;
  1049. }
  1050. /* check if such qgroup relation exist firstly */
  1051. list_for_each_entry(list, &member->groups, next_group) {
  1052. if (list->group == parent)
  1053. goto exist;
  1054. }
  1055. ret = -ENOENT;
  1056. goto out;
  1057. exist:
  1058. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1059. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1060. if (err && !ret)
  1061. ret = err;
  1062. spin_lock(&fs_info->qgroup_lock);
  1063. del_relation_rb(fs_info, src, dst);
  1064. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1065. spin_unlock(&fs_info->qgroup_lock);
  1066. out:
  1067. ulist_free(tmp);
  1068. return ret;
  1069. }
  1070. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1071. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1072. {
  1073. int ret = 0;
  1074. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1075. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1076. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1077. return ret;
  1078. }
  1079. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1080. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1081. {
  1082. struct btrfs_root *quota_root;
  1083. struct btrfs_qgroup *qgroup;
  1084. int ret = 0;
  1085. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1086. quota_root = fs_info->quota_root;
  1087. if (!quota_root) {
  1088. ret = -EINVAL;
  1089. goto out;
  1090. }
  1091. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1092. if (qgroup) {
  1093. ret = -EEXIST;
  1094. goto out;
  1095. }
  1096. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1097. if (ret)
  1098. goto out;
  1099. spin_lock(&fs_info->qgroup_lock);
  1100. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1101. spin_unlock(&fs_info->qgroup_lock);
  1102. if (IS_ERR(qgroup))
  1103. ret = PTR_ERR(qgroup);
  1104. out:
  1105. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1106. return ret;
  1107. }
  1108. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1109. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1110. {
  1111. struct btrfs_root *quota_root;
  1112. struct btrfs_qgroup *qgroup;
  1113. struct btrfs_qgroup_list *list;
  1114. int ret = 0;
  1115. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1116. quota_root = fs_info->quota_root;
  1117. if (!quota_root) {
  1118. ret = -EINVAL;
  1119. goto out;
  1120. }
  1121. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1122. if (!qgroup) {
  1123. ret = -ENOENT;
  1124. goto out;
  1125. } else {
  1126. /* check if there are no children of this qgroup */
  1127. if (!list_empty(&qgroup->members)) {
  1128. ret = -EBUSY;
  1129. goto out;
  1130. }
  1131. }
  1132. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1133. while (!list_empty(&qgroup->groups)) {
  1134. list = list_first_entry(&qgroup->groups,
  1135. struct btrfs_qgroup_list, next_group);
  1136. ret = __del_qgroup_relation(trans, fs_info,
  1137. qgroupid,
  1138. list->group->qgroupid);
  1139. if (ret)
  1140. goto out;
  1141. }
  1142. spin_lock(&fs_info->qgroup_lock);
  1143. del_qgroup_rb(quota_root->fs_info, qgroupid);
  1144. spin_unlock(&fs_info->qgroup_lock);
  1145. out:
  1146. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1147. return ret;
  1148. }
  1149. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1150. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1151. struct btrfs_qgroup_limit *limit)
  1152. {
  1153. struct btrfs_root *quota_root;
  1154. struct btrfs_qgroup *qgroup;
  1155. int ret = 0;
  1156. /* Sometimes we would want to clear the limit on this qgroup.
  1157. * To meet this requirement, we treat the -1 as a special value
  1158. * which tell kernel to clear the limit on this qgroup.
  1159. */
  1160. const u64 CLEAR_VALUE = -1;
  1161. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1162. quota_root = fs_info->quota_root;
  1163. if (!quota_root) {
  1164. ret = -EINVAL;
  1165. goto out;
  1166. }
  1167. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1168. if (!qgroup) {
  1169. ret = -ENOENT;
  1170. goto out;
  1171. }
  1172. spin_lock(&fs_info->qgroup_lock);
  1173. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1174. if (limit->max_rfer == CLEAR_VALUE) {
  1175. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1176. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1177. qgroup->max_rfer = 0;
  1178. } else {
  1179. qgroup->max_rfer = limit->max_rfer;
  1180. }
  1181. }
  1182. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1183. if (limit->max_excl == CLEAR_VALUE) {
  1184. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1185. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1186. qgroup->max_excl = 0;
  1187. } else {
  1188. qgroup->max_excl = limit->max_excl;
  1189. }
  1190. }
  1191. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1192. if (limit->rsv_rfer == CLEAR_VALUE) {
  1193. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1194. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1195. qgroup->rsv_rfer = 0;
  1196. } else {
  1197. qgroup->rsv_rfer = limit->rsv_rfer;
  1198. }
  1199. }
  1200. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1201. if (limit->rsv_excl == CLEAR_VALUE) {
  1202. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1203. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1204. qgroup->rsv_excl = 0;
  1205. } else {
  1206. qgroup->rsv_excl = limit->rsv_excl;
  1207. }
  1208. }
  1209. qgroup->lim_flags |= limit->flags;
  1210. spin_unlock(&fs_info->qgroup_lock);
  1211. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1212. if (ret) {
  1213. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1214. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1215. qgroupid);
  1216. }
  1217. out:
  1218. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1219. return ret;
  1220. }
  1221. int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
  1222. struct btrfs_fs_info *fs_info)
  1223. {
  1224. struct btrfs_qgroup_extent_record *record;
  1225. struct btrfs_delayed_ref_root *delayed_refs;
  1226. struct rb_node *node;
  1227. u64 qgroup_to_skip;
  1228. int ret = 0;
  1229. delayed_refs = &trans->transaction->delayed_refs;
  1230. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1231. /*
  1232. * No need to do lock, since this function will only be called in
  1233. * btrfs_commmit_transaction().
  1234. */
  1235. node = rb_first(&delayed_refs->dirty_extent_root);
  1236. while (node) {
  1237. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1238. node);
  1239. ret = btrfs_find_all_roots(NULL, fs_info, record->bytenr, 0,
  1240. &record->old_roots);
  1241. if (ret < 0)
  1242. break;
  1243. if (qgroup_to_skip)
  1244. ulist_del(record->old_roots, qgroup_to_skip, 0);
  1245. node = rb_next(node);
  1246. }
  1247. return ret;
  1248. }
  1249. struct btrfs_qgroup_extent_record
  1250. *btrfs_qgroup_insert_dirty_extent(struct btrfs_delayed_ref_root *delayed_refs,
  1251. struct btrfs_qgroup_extent_record *record)
  1252. {
  1253. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1254. struct rb_node *parent_node = NULL;
  1255. struct btrfs_qgroup_extent_record *entry;
  1256. u64 bytenr = record->bytenr;
  1257. while (*p) {
  1258. parent_node = *p;
  1259. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1260. node);
  1261. if (bytenr < entry->bytenr)
  1262. p = &(*p)->rb_left;
  1263. else if (bytenr > entry->bytenr)
  1264. p = &(*p)->rb_right;
  1265. else
  1266. return entry;
  1267. }
  1268. rb_link_node(&record->node, parent_node, p);
  1269. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1270. return NULL;
  1271. }
  1272. #define UPDATE_NEW 0
  1273. #define UPDATE_OLD 1
  1274. /*
  1275. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1276. */
  1277. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1278. struct ulist *roots, struct ulist *tmp,
  1279. struct ulist *qgroups, u64 seq, int update_old)
  1280. {
  1281. struct ulist_node *unode;
  1282. struct ulist_iterator uiter;
  1283. struct ulist_node *tmp_unode;
  1284. struct ulist_iterator tmp_uiter;
  1285. struct btrfs_qgroup *qg;
  1286. int ret = 0;
  1287. if (!roots)
  1288. return 0;
  1289. ULIST_ITER_INIT(&uiter);
  1290. while ((unode = ulist_next(roots, &uiter))) {
  1291. qg = find_qgroup_rb(fs_info, unode->val);
  1292. if (!qg)
  1293. continue;
  1294. ulist_reinit(tmp);
  1295. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1296. GFP_ATOMIC);
  1297. if (ret < 0)
  1298. return ret;
  1299. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1300. if (ret < 0)
  1301. return ret;
  1302. ULIST_ITER_INIT(&tmp_uiter);
  1303. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1304. struct btrfs_qgroup_list *glist;
  1305. qg = u64_to_ptr(tmp_unode->aux);
  1306. if (update_old)
  1307. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1308. else
  1309. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1310. list_for_each_entry(glist, &qg->groups, next_group) {
  1311. ret = ulist_add(qgroups, glist->group->qgroupid,
  1312. ptr_to_u64(glist->group),
  1313. GFP_ATOMIC);
  1314. if (ret < 0)
  1315. return ret;
  1316. ret = ulist_add(tmp, glist->group->qgroupid,
  1317. ptr_to_u64(glist->group),
  1318. GFP_ATOMIC);
  1319. if (ret < 0)
  1320. return ret;
  1321. }
  1322. }
  1323. }
  1324. return 0;
  1325. }
  1326. /*
  1327. * Update qgroup rfer/excl counters.
  1328. * Rfer update is easy, codes can explain themselves.
  1329. *
  1330. * Excl update is tricky, the update is split into 2 part.
  1331. * Part 1: Possible exclusive <-> sharing detect:
  1332. * | A | !A |
  1333. * -------------------------------------
  1334. * B | * | - |
  1335. * -------------------------------------
  1336. * !B | + | ** |
  1337. * -------------------------------------
  1338. *
  1339. * Conditions:
  1340. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1341. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1342. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1343. * !B: cur_new_roots == nr_new_roots (possible exclsuive now)
  1344. *
  1345. * Results:
  1346. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1347. * *: Definitely not changed. **: Possible unchanged.
  1348. *
  1349. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1350. *
  1351. * To make the logic clear, we first use condition A and B to split
  1352. * combination into 4 results.
  1353. *
  1354. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1355. * only on variant maybe 0.
  1356. *
  1357. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1358. * again(2x2).
  1359. * But this time we don't need to consider other things, the codes and logic
  1360. * is easy to understand now.
  1361. */
  1362. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1363. struct ulist *qgroups,
  1364. u64 nr_old_roots,
  1365. u64 nr_new_roots,
  1366. u64 num_bytes, u64 seq)
  1367. {
  1368. struct ulist_node *unode;
  1369. struct ulist_iterator uiter;
  1370. struct btrfs_qgroup *qg;
  1371. u64 cur_new_count, cur_old_count;
  1372. ULIST_ITER_INIT(&uiter);
  1373. while ((unode = ulist_next(qgroups, &uiter))) {
  1374. bool dirty = false;
  1375. qg = u64_to_ptr(unode->aux);
  1376. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1377. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1378. /* Rfer update part */
  1379. if (cur_old_count == 0 && cur_new_count > 0) {
  1380. qg->rfer += num_bytes;
  1381. qg->rfer_cmpr += num_bytes;
  1382. dirty = true;
  1383. }
  1384. if (cur_old_count > 0 && cur_new_count == 0) {
  1385. qg->rfer -= num_bytes;
  1386. qg->rfer_cmpr -= num_bytes;
  1387. dirty = true;
  1388. }
  1389. /* Excl update part */
  1390. /* Exclusive/none -> shared case */
  1391. if (cur_old_count == nr_old_roots &&
  1392. cur_new_count < nr_new_roots) {
  1393. /* Exclusive -> shared */
  1394. if (cur_old_count != 0) {
  1395. qg->excl -= num_bytes;
  1396. qg->excl_cmpr -= num_bytes;
  1397. dirty = true;
  1398. }
  1399. }
  1400. /* Shared -> exclusive/none case */
  1401. if (cur_old_count < nr_old_roots &&
  1402. cur_new_count == nr_new_roots) {
  1403. /* Shared->exclusive */
  1404. if (cur_new_count != 0) {
  1405. qg->excl += num_bytes;
  1406. qg->excl_cmpr += num_bytes;
  1407. dirty = true;
  1408. }
  1409. }
  1410. /* Exclusive/none -> exclusive/none case */
  1411. if (cur_old_count == nr_old_roots &&
  1412. cur_new_count == nr_new_roots) {
  1413. if (cur_old_count == 0) {
  1414. /* None -> exclusive/none */
  1415. if (cur_new_count != 0) {
  1416. /* None -> exclusive */
  1417. qg->excl += num_bytes;
  1418. qg->excl_cmpr += num_bytes;
  1419. dirty = true;
  1420. }
  1421. /* None -> none, nothing changed */
  1422. } else {
  1423. /* Exclusive -> exclusive/none */
  1424. if (cur_new_count == 0) {
  1425. /* Exclusive -> none */
  1426. qg->excl -= num_bytes;
  1427. qg->excl_cmpr -= num_bytes;
  1428. dirty = true;
  1429. }
  1430. /* Exclusive -> exclusive, nothing changed */
  1431. }
  1432. }
  1433. /* For exclusive extent, free its reserved bytes too */
  1434. if (nr_old_roots == 0 && nr_new_roots == 1 &&
  1435. cur_new_count == nr_new_roots)
  1436. qg->reserved -= num_bytes;
  1437. if (dirty)
  1438. qgroup_dirty(fs_info, qg);
  1439. }
  1440. return 0;
  1441. }
  1442. int
  1443. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1444. struct btrfs_fs_info *fs_info,
  1445. u64 bytenr, u64 num_bytes,
  1446. struct ulist *old_roots, struct ulist *new_roots)
  1447. {
  1448. struct ulist *qgroups = NULL;
  1449. struct ulist *tmp = NULL;
  1450. u64 seq;
  1451. u64 nr_new_roots = 0;
  1452. u64 nr_old_roots = 0;
  1453. int ret = 0;
  1454. if (new_roots)
  1455. nr_new_roots = new_roots->nnodes;
  1456. if (old_roots)
  1457. nr_old_roots = old_roots->nnodes;
  1458. if (!fs_info->quota_enabled)
  1459. goto out_free;
  1460. BUG_ON(!fs_info->quota_root);
  1461. qgroups = ulist_alloc(GFP_NOFS);
  1462. if (!qgroups) {
  1463. ret = -ENOMEM;
  1464. goto out_free;
  1465. }
  1466. tmp = ulist_alloc(GFP_NOFS);
  1467. if (!tmp) {
  1468. ret = -ENOMEM;
  1469. goto out_free;
  1470. }
  1471. mutex_lock(&fs_info->qgroup_rescan_lock);
  1472. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1473. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1474. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1475. ret = 0;
  1476. goto out_free;
  1477. }
  1478. }
  1479. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1480. spin_lock(&fs_info->qgroup_lock);
  1481. seq = fs_info->qgroup_seq;
  1482. /* Update old refcnts using old_roots */
  1483. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1484. UPDATE_OLD);
  1485. if (ret < 0)
  1486. goto out;
  1487. /* Update new refcnts using new_roots */
  1488. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1489. UPDATE_NEW);
  1490. if (ret < 0)
  1491. goto out;
  1492. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1493. num_bytes, seq);
  1494. /*
  1495. * Bump qgroup_seq to avoid seq overlap
  1496. */
  1497. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1498. out:
  1499. spin_unlock(&fs_info->qgroup_lock);
  1500. out_free:
  1501. ulist_free(tmp);
  1502. ulist_free(qgroups);
  1503. ulist_free(old_roots);
  1504. ulist_free(new_roots);
  1505. return ret;
  1506. }
  1507. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1508. struct btrfs_fs_info *fs_info)
  1509. {
  1510. struct btrfs_qgroup_extent_record *record;
  1511. struct btrfs_delayed_ref_root *delayed_refs;
  1512. struct ulist *new_roots = NULL;
  1513. struct rb_node *node;
  1514. u64 qgroup_to_skip;
  1515. int ret = 0;
  1516. delayed_refs = &trans->transaction->delayed_refs;
  1517. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1518. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1519. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1520. node);
  1521. if (!ret) {
  1522. /*
  1523. * Use (u64)-1 as time_seq to do special search, which
  1524. * doesn't lock tree or delayed_refs and search current
  1525. * root. It's safe inside commit_transaction().
  1526. */
  1527. ret = btrfs_find_all_roots(trans, fs_info,
  1528. record->bytenr, (u64)-1, &new_roots);
  1529. if (ret < 0)
  1530. goto cleanup;
  1531. if (qgroup_to_skip)
  1532. ulist_del(new_roots, qgroup_to_skip, 0);
  1533. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1534. record->bytenr, record->num_bytes,
  1535. record->old_roots, new_roots);
  1536. record->old_roots = NULL;
  1537. new_roots = NULL;
  1538. }
  1539. cleanup:
  1540. ulist_free(record->old_roots);
  1541. ulist_free(new_roots);
  1542. new_roots = NULL;
  1543. rb_erase(node, &delayed_refs->dirty_extent_root);
  1544. kfree(record);
  1545. }
  1546. return ret;
  1547. }
  1548. /*
  1549. * called from commit_transaction. Writes all changed qgroups to disk.
  1550. */
  1551. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1552. struct btrfs_fs_info *fs_info)
  1553. {
  1554. struct btrfs_root *quota_root = fs_info->quota_root;
  1555. int ret = 0;
  1556. int start_rescan_worker = 0;
  1557. if (!quota_root)
  1558. goto out;
  1559. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1560. start_rescan_worker = 1;
  1561. fs_info->quota_enabled = fs_info->pending_quota_state;
  1562. spin_lock(&fs_info->qgroup_lock);
  1563. while (!list_empty(&fs_info->dirty_qgroups)) {
  1564. struct btrfs_qgroup *qgroup;
  1565. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1566. struct btrfs_qgroup, dirty);
  1567. list_del_init(&qgroup->dirty);
  1568. spin_unlock(&fs_info->qgroup_lock);
  1569. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1570. if (ret)
  1571. fs_info->qgroup_flags |=
  1572. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1573. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1574. if (ret)
  1575. fs_info->qgroup_flags |=
  1576. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1577. spin_lock(&fs_info->qgroup_lock);
  1578. }
  1579. if (fs_info->quota_enabled)
  1580. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1581. else
  1582. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1583. spin_unlock(&fs_info->qgroup_lock);
  1584. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1585. if (ret)
  1586. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1587. if (!ret && start_rescan_worker) {
  1588. ret = qgroup_rescan_init(fs_info, 0, 1);
  1589. if (!ret) {
  1590. qgroup_rescan_zero_tracking(fs_info);
  1591. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1592. &fs_info->qgroup_rescan_work);
  1593. }
  1594. ret = 0;
  1595. }
  1596. out:
  1597. return ret;
  1598. }
  1599. /*
  1600. * copy the acounting information between qgroups. This is necessary when a
  1601. * snapshot or a subvolume is created
  1602. */
  1603. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1604. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1605. struct btrfs_qgroup_inherit *inherit)
  1606. {
  1607. int ret = 0;
  1608. int i;
  1609. u64 *i_qgroups;
  1610. struct btrfs_root *quota_root = fs_info->quota_root;
  1611. struct btrfs_qgroup *srcgroup;
  1612. struct btrfs_qgroup *dstgroup;
  1613. u32 level_size = 0;
  1614. u64 nums;
  1615. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1616. if (!fs_info->quota_enabled)
  1617. goto out;
  1618. if (!quota_root) {
  1619. ret = -EINVAL;
  1620. goto out;
  1621. }
  1622. if (inherit) {
  1623. i_qgroups = (u64 *)(inherit + 1);
  1624. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1625. 2 * inherit->num_excl_copies;
  1626. for (i = 0; i < nums; ++i) {
  1627. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1628. if (!srcgroup) {
  1629. ret = -EINVAL;
  1630. goto out;
  1631. }
  1632. if ((srcgroup->qgroupid >> 48) <= (objectid >> 48)) {
  1633. ret = -EINVAL;
  1634. goto out;
  1635. }
  1636. ++i_qgroups;
  1637. }
  1638. }
  1639. /*
  1640. * create a tracking group for the subvol itself
  1641. */
  1642. ret = add_qgroup_item(trans, quota_root, objectid);
  1643. if (ret)
  1644. goto out;
  1645. if (srcid) {
  1646. struct btrfs_root *srcroot;
  1647. struct btrfs_key srckey;
  1648. srckey.objectid = srcid;
  1649. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1650. srckey.offset = (u64)-1;
  1651. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1652. if (IS_ERR(srcroot)) {
  1653. ret = PTR_ERR(srcroot);
  1654. goto out;
  1655. }
  1656. rcu_read_lock();
  1657. level_size = srcroot->nodesize;
  1658. rcu_read_unlock();
  1659. }
  1660. /*
  1661. * add qgroup to all inherited groups
  1662. */
  1663. if (inherit) {
  1664. i_qgroups = (u64 *)(inherit + 1);
  1665. for (i = 0; i < inherit->num_qgroups; ++i) {
  1666. ret = add_qgroup_relation_item(trans, quota_root,
  1667. objectid, *i_qgroups);
  1668. if (ret)
  1669. goto out;
  1670. ret = add_qgroup_relation_item(trans, quota_root,
  1671. *i_qgroups, objectid);
  1672. if (ret)
  1673. goto out;
  1674. ++i_qgroups;
  1675. }
  1676. }
  1677. spin_lock(&fs_info->qgroup_lock);
  1678. dstgroup = add_qgroup_rb(fs_info, objectid);
  1679. if (IS_ERR(dstgroup)) {
  1680. ret = PTR_ERR(dstgroup);
  1681. goto unlock;
  1682. }
  1683. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1684. dstgroup->lim_flags = inherit->lim.flags;
  1685. dstgroup->max_rfer = inherit->lim.max_rfer;
  1686. dstgroup->max_excl = inherit->lim.max_excl;
  1687. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1688. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1689. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1690. if (ret) {
  1691. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1692. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1693. dstgroup->qgroupid);
  1694. goto unlock;
  1695. }
  1696. }
  1697. if (srcid) {
  1698. srcgroup = find_qgroup_rb(fs_info, srcid);
  1699. if (!srcgroup)
  1700. goto unlock;
  1701. /*
  1702. * We call inherit after we clone the root in order to make sure
  1703. * our counts don't go crazy, so at this point the only
  1704. * difference between the two roots should be the root node.
  1705. */
  1706. dstgroup->rfer = srcgroup->rfer;
  1707. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1708. dstgroup->excl = level_size;
  1709. dstgroup->excl_cmpr = level_size;
  1710. srcgroup->excl = level_size;
  1711. srcgroup->excl_cmpr = level_size;
  1712. /* inherit the limit info */
  1713. dstgroup->lim_flags = srcgroup->lim_flags;
  1714. dstgroup->max_rfer = srcgroup->max_rfer;
  1715. dstgroup->max_excl = srcgroup->max_excl;
  1716. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1717. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1718. qgroup_dirty(fs_info, dstgroup);
  1719. qgroup_dirty(fs_info, srcgroup);
  1720. }
  1721. if (!inherit)
  1722. goto unlock;
  1723. i_qgroups = (u64 *)(inherit + 1);
  1724. for (i = 0; i < inherit->num_qgroups; ++i) {
  1725. ret = add_relation_rb(quota_root->fs_info, objectid,
  1726. *i_qgroups);
  1727. if (ret)
  1728. goto unlock;
  1729. ++i_qgroups;
  1730. }
  1731. for (i = 0; i < inherit->num_ref_copies; ++i) {
  1732. struct btrfs_qgroup *src;
  1733. struct btrfs_qgroup *dst;
  1734. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1735. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1736. if (!src || !dst) {
  1737. ret = -EINVAL;
  1738. goto unlock;
  1739. }
  1740. dst->rfer = src->rfer - level_size;
  1741. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1742. i_qgroups += 2;
  1743. }
  1744. for (i = 0; i < inherit->num_excl_copies; ++i) {
  1745. struct btrfs_qgroup *src;
  1746. struct btrfs_qgroup *dst;
  1747. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1748. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1749. if (!src || !dst) {
  1750. ret = -EINVAL;
  1751. goto unlock;
  1752. }
  1753. dst->excl = src->excl + level_size;
  1754. dst->excl_cmpr = src->excl_cmpr + level_size;
  1755. i_qgroups += 2;
  1756. }
  1757. unlock:
  1758. spin_unlock(&fs_info->qgroup_lock);
  1759. out:
  1760. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1761. return ret;
  1762. }
  1763. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1764. {
  1765. struct btrfs_root *quota_root;
  1766. struct btrfs_qgroup *qgroup;
  1767. struct btrfs_fs_info *fs_info = root->fs_info;
  1768. u64 ref_root = root->root_key.objectid;
  1769. int ret = 0;
  1770. struct ulist_node *unode;
  1771. struct ulist_iterator uiter;
  1772. if (!is_fstree(ref_root))
  1773. return 0;
  1774. if (num_bytes == 0)
  1775. return 0;
  1776. spin_lock(&fs_info->qgroup_lock);
  1777. quota_root = fs_info->quota_root;
  1778. if (!quota_root)
  1779. goto out;
  1780. qgroup = find_qgroup_rb(fs_info, ref_root);
  1781. if (!qgroup)
  1782. goto out;
  1783. /*
  1784. * in a first step, we check all affected qgroups if any limits would
  1785. * be exceeded
  1786. */
  1787. ulist_reinit(fs_info->qgroup_ulist);
  1788. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1789. (uintptr_t)qgroup, GFP_ATOMIC);
  1790. if (ret < 0)
  1791. goto out;
  1792. ULIST_ITER_INIT(&uiter);
  1793. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1794. struct btrfs_qgroup *qg;
  1795. struct btrfs_qgroup_list *glist;
  1796. qg = u64_to_ptr(unode->aux);
  1797. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1798. qg->reserved + (s64)qg->rfer + num_bytes >
  1799. qg->max_rfer) {
  1800. ret = -EDQUOT;
  1801. goto out;
  1802. }
  1803. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1804. qg->reserved + (s64)qg->excl + num_bytes >
  1805. qg->max_excl) {
  1806. ret = -EDQUOT;
  1807. goto out;
  1808. }
  1809. list_for_each_entry(glist, &qg->groups, next_group) {
  1810. ret = ulist_add(fs_info->qgroup_ulist,
  1811. glist->group->qgroupid,
  1812. (uintptr_t)glist->group, GFP_ATOMIC);
  1813. if (ret < 0)
  1814. goto out;
  1815. }
  1816. }
  1817. ret = 0;
  1818. /*
  1819. * no limits exceeded, now record the reservation into all qgroups
  1820. */
  1821. ULIST_ITER_INIT(&uiter);
  1822. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1823. struct btrfs_qgroup *qg;
  1824. qg = u64_to_ptr(unode->aux);
  1825. qg->reserved += num_bytes;
  1826. }
  1827. out:
  1828. spin_unlock(&fs_info->qgroup_lock);
  1829. return ret;
  1830. }
  1831. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1832. {
  1833. struct btrfs_root *quota_root;
  1834. struct btrfs_qgroup *qgroup;
  1835. struct btrfs_fs_info *fs_info = root->fs_info;
  1836. struct ulist_node *unode;
  1837. struct ulist_iterator uiter;
  1838. u64 ref_root = root->root_key.objectid;
  1839. int ret = 0;
  1840. if (!is_fstree(ref_root))
  1841. return;
  1842. if (num_bytes == 0)
  1843. return;
  1844. spin_lock(&fs_info->qgroup_lock);
  1845. quota_root = fs_info->quota_root;
  1846. if (!quota_root)
  1847. goto out;
  1848. qgroup = find_qgroup_rb(fs_info, ref_root);
  1849. if (!qgroup)
  1850. goto out;
  1851. ulist_reinit(fs_info->qgroup_ulist);
  1852. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1853. (uintptr_t)qgroup, GFP_ATOMIC);
  1854. if (ret < 0)
  1855. goto out;
  1856. ULIST_ITER_INIT(&uiter);
  1857. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1858. struct btrfs_qgroup *qg;
  1859. struct btrfs_qgroup_list *glist;
  1860. qg = u64_to_ptr(unode->aux);
  1861. qg->reserved -= num_bytes;
  1862. list_for_each_entry(glist, &qg->groups, next_group) {
  1863. ret = ulist_add(fs_info->qgroup_ulist,
  1864. glist->group->qgroupid,
  1865. (uintptr_t)glist->group, GFP_ATOMIC);
  1866. if (ret < 0)
  1867. goto out;
  1868. }
  1869. }
  1870. out:
  1871. spin_unlock(&fs_info->qgroup_lock);
  1872. }
  1873. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1874. {
  1875. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1876. return;
  1877. btrfs_err(trans->root->fs_info,
  1878. "qgroups not uptodate in trans handle %p: list is%s empty, "
  1879. "seq is %#x.%x",
  1880. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1881. (u32)(trans->delayed_ref_elem.seq >> 32),
  1882. (u32)trans->delayed_ref_elem.seq);
  1883. BUG();
  1884. }
  1885. /*
  1886. * returns < 0 on error, 0 when more leafs are to be scanned.
  1887. * returns 1 when done.
  1888. */
  1889. static int
  1890. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1891. struct btrfs_trans_handle *trans,
  1892. struct extent_buffer *scratch_leaf)
  1893. {
  1894. struct btrfs_key found;
  1895. struct ulist *roots = NULL;
  1896. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1897. u64 num_bytes;
  1898. int slot;
  1899. int ret;
  1900. path->leave_spinning = 1;
  1901. mutex_lock(&fs_info->qgroup_rescan_lock);
  1902. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1903. &fs_info->qgroup_rescan_progress,
  1904. path, 1, 0);
  1905. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  1906. fs_info->qgroup_rescan_progress.objectid,
  1907. fs_info->qgroup_rescan_progress.type,
  1908. fs_info->qgroup_rescan_progress.offset, ret);
  1909. if (ret) {
  1910. /*
  1911. * The rescan is about to end, we will not be scanning any
  1912. * further blocks. We cannot unset the RESCAN flag here, because
  1913. * we want to commit the transaction if everything went well.
  1914. * To make the live accounting work in this phase, we set our
  1915. * scan progress pointer such that every real extent objectid
  1916. * will be smaller.
  1917. */
  1918. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1919. btrfs_release_path(path);
  1920. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1921. return ret;
  1922. }
  1923. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1924. btrfs_header_nritems(path->nodes[0]) - 1);
  1925. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1926. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1927. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  1928. slot = path->slots[0];
  1929. btrfs_release_path(path);
  1930. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1931. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1932. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1933. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  1934. found.type != BTRFS_METADATA_ITEM_KEY)
  1935. continue;
  1936. if (found.type == BTRFS_METADATA_ITEM_KEY)
  1937. num_bytes = fs_info->extent_root->nodesize;
  1938. else
  1939. num_bytes = found.offset;
  1940. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  1941. &roots);
  1942. if (ret < 0)
  1943. goto out;
  1944. /* For rescan, just pass old_roots as NULL */
  1945. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1946. found.objectid, num_bytes, NULL, roots);
  1947. if (ret < 0)
  1948. goto out;
  1949. }
  1950. out:
  1951. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1952. return ret;
  1953. }
  1954. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  1955. {
  1956. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  1957. qgroup_rescan_work);
  1958. struct btrfs_path *path;
  1959. struct btrfs_trans_handle *trans = NULL;
  1960. struct extent_buffer *scratch_leaf = NULL;
  1961. int err = -ENOMEM;
  1962. int ret = 0;
  1963. path = btrfs_alloc_path();
  1964. if (!path)
  1965. goto out;
  1966. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  1967. if (!scratch_leaf)
  1968. goto out;
  1969. err = 0;
  1970. while (!err) {
  1971. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  1972. if (IS_ERR(trans)) {
  1973. err = PTR_ERR(trans);
  1974. break;
  1975. }
  1976. if (!fs_info->quota_enabled) {
  1977. err = -EINTR;
  1978. } else {
  1979. err = qgroup_rescan_leaf(fs_info, path, trans,
  1980. scratch_leaf);
  1981. }
  1982. if (err > 0)
  1983. btrfs_commit_transaction(trans, fs_info->fs_root);
  1984. else
  1985. btrfs_end_transaction(trans, fs_info->fs_root);
  1986. }
  1987. out:
  1988. kfree(scratch_leaf);
  1989. btrfs_free_path(path);
  1990. mutex_lock(&fs_info->qgroup_rescan_lock);
  1991. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1992. if (err > 0 &&
  1993. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  1994. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1995. } else if (err < 0) {
  1996. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1997. }
  1998. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1999. /*
  2000. * only update status, since the previous part has alreay updated the
  2001. * qgroup info.
  2002. */
  2003. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2004. if (IS_ERR(trans)) {
  2005. err = PTR_ERR(trans);
  2006. btrfs_err(fs_info,
  2007. "fail to start transaction for status update: %d\n",
  2008. err);
  2009. goto done;
  2010. }
  2011. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2012. if (ret < 0) {
  2013. err = ret;
  2014. btrfs_err(fs_info, "fail to update qgroup status: %d\n", err);
  2015. }
  2016. btrfs_end_transaction(trans, fs_info->quota_root);
  2017. if (err >= 0) {
  2018. btrfs_info(fs_info, "qgroup scan completed%s",
  2019. err > 0 ? " (inconsistency flag cleared)" : "");
  2020. } else {
  2021. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2022. }
  2023. done:
  2024. complete_all(&fs_info->qgroup_rescan_completion);
  2025. }
  2026. /*
  2027. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2028. * memory required for the rescan context.
  2029. */
  2030. static int
  2031. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2032. int init_flags)
  2033. {
  2034. int ret = 0;
  2035. if (!init_flags &&
  2036. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2037. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2038. ret = -EINVAL;
  2039. goto err;
  2040. }
  2041. mutex_lock(&fs_info->qgroup_rescan_lock);
  2042. spin_lock(&fs_info->qgroup_lock);
  2043. if (init_flags) {
  2044. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2045. ret = -EINPROGRESS;
  2046. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2047. ret = -EINVAL;
  2048. if (ret) {
  2049. spin_unlock(&fs_info->qgroup_lock);
  2050. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2051. goto err;
  2052. }
  2053. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2054. }
  2055. memset(&fs_info->qgroup_rescan_progress, 0,
  2056. sizeof(fs_info->qgroup_rescan_progress));
  2057. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2058. spin_unlock(&fs_info->qgroup_lock);
  2059. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2060. init_completion(&fs_info->qgroup_rescan_completion);
  2061. memset(&fs_info->qgroup_rescan_work, 0,
  2062. sizeof(fs_info->qgroup_rescan_work));
  2063. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2064. btrfs_qgroup_rescan_helper,
  2065. btrfs_qgroup_rescan_worker, NULL, NULL);
  2066. if (ret) {
  2067. err:
  2068. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2069. return ret;
  2070. }
  2071. return 0;
  2072. }
  2073. static void
  2074. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2075. {
  2076. struct rb_node *n;
  2077. struct btrfs_qgroup *qgroup;
  2078. spin_lock(&fs_info->qgroup_lock);
  2079. /* clear all current qgroup tracking information */
  2080. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2081. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2082. qgroup->rfer = 0;
  2083. qgroup->rfer_cmpr = 0;
  2084. qgroup->excl = 0;
  2085. qgroup->excl_cmpr = 0;
  2086. }
  2087. spin_unlock(&fs_info->qgroup_lock);
  2088. }
  2089. int
  2090. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2091. {
  2092. int ret = 0;
  2093. struct btrfs_trans_handle *trans;
  2094. ret = qgroup_rescan_init(fs_info, 0, 1);
  2095. if (ret)
  2096. return ret;
  2097. /*
  2098. * We have set the rescan_progress to 0, which means no more
  2099. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2100. * However, btrfs_qgroup_account_ref may be right after its call
  2101. * to btrfs_find_all_roots, in which case it would still do the
  2102. * accounting.
  2103. * To solve this, we're committing the transaction, which will
  2104. * ensure we run all delayed refs and only after that, we are
  2105. * going to clear all tracking information for a clean start.
  2106. */
  2107. trans = btrfs_join_transaction(fs_info->fs_root);
  2108. if (IS_ERR(trans)) {
  2109. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2110. return PTR_ERR(trans);
  2111. }
  2112. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2113. if (ret) {
  2114. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2115. return ret;
  2116. }
  2117. qgroup_rescan_zero_tracking(fs_info);
  2118. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2119. &fs_info->qgroup_rescan_work);
  2120. return 0;
  2121. }
  2122. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  2123. {
  2124. int running;
  2125. int ret = 0;
  2126. mutex_lock(&fs_info->qgroup_rescan_lock);
  2127. spin_lock(&fs_info->qgroup_lock);
  2128. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2129. spin_unlock(&fs_info->qgroup_lock);
  2130. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2131. if (running)
  2132. ret = wait_for_completion_interruptible(
  2133. &fs_info->qgroup_rescan_completion);
  2134. return ret;
  2135. }
  2136. /*
  2137. * this is only called from open_ctree where we're still single threaded, thus
  2138. * locking is omitted here.
  2139. */
  2140. void
  2141. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2142. {
  2143. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2144. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2145. &fs_info->qgroup_rescan_work);
  2146. }