qgroup.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767
  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_accounting() 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 (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  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, marked as inconsistent");
  311. }
  312. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  313. ptr);
  314. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  315. goto next1;
  316. }
  317. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  318. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  319. goto next1;
  320. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  321. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  322. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  323. btrfs_err(fs_info, "inconsistent qgroup config");
  324. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  325. }
  326. if (!qgroup) {
  327. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  328. if (IS_ERR(qgroup)) {
  329. ret = PTR_ERR(qgroup);
  330. goto out;
  331. }
  332. }
  333. switch (found_key.type) {
  334. case BTRFS_QGROUP_INFO_KEY: {
  335. struct btrfs_qgroup_info_item *ptr;
  336. ptr = btrfs_item_ptr(l, slot,
  337. struct btrfs_qgroup_info_item);
  338. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  339. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  340. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  341. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  342. /* generation currently unused */
  343. break;
  344. }
  345. case BTRFS_QGROUP_LIMIT_KEY: {
  346. struct btrfs_qgroup_limit_item *ptr;
  347. ptr = btrfs_item_ptr(l, slot,
  348. struct btrfs_qgroup_limit_item);
  349. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  350. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  351. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  352. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  353. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  354. break;
  355. }
  356. }
  357. next1:
  358. ret = btrfs_next_item(quota_root, path);
  359. if (ret < 0)
  360. goto out;
  361. if (ret)
  362. break;
  363. }
  364. btrfs_release_path(path);
  365. /*
  366. * pass 2: read all qgroup relations
  367. */
  368. key.objectid = 0;
  369. key.type = BTRFS_QGROUP_RELATION_KEY;
  370. key.offset = 0;
  371. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  372. if (ret)
  373. goto out;
  374. while (1) {
  375. slot = path->slots[0];
  376. l = path->nodes[0];
  377. btrfs_item_key_to_cpu(l, &found_key, slot);
  378. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  379. goto next2;
  380. if (found_key.objectid > found_key.offset) {
  381. /* parent <- member, not needed to build config */
  382. /* FIXME should we omit the key completely? */
  383. goto next2;
  384. }
  385. ret = add_relation_rb(fs_info, found_key.objectid,
  386. found_key.offset);
  387. if (ret == -ENOENT) {
  388. btrfs_warn(fs_info,
  389. "orphan qgroup relation 0x%llx->0x%llx",
  390. found_key.objectid, found_key.offset);
  391. ret = 0; /* ignore the error */
  392. }
  393. if (ret)
  394. goto out;
  395. next2:
  396. ret = btrfs_next_item(quota_root, path);
  397. if (ret < 0)
  398. goto out;
  399. if (ret)
  400. break;
  401. }
  402. out:
  403. fs_info->qgroup_flags |= flags;
  404. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  405. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  406. else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  407. ret >= 0)
  408. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  409. btrfs_free_path(path);
  410. if (ret < 0) {
  411. ulist_free(fs_info->qgroup_ulist);
  412. fs_info->qgroup_ulist = NULL;
  413. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  414. }
  415. return ret < 0 ? ret : 0;
  416. }
  417. /*
  418. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  419. * first two are in single-threaded paths.And for the third one, we have set
  420. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  421. * up the in-memory structures without qgroup_lock held.
  422. */
  423. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  424. {
  425. struct rb_node *n;
  426. struct btrfs_qgroup *qgroup;
  427. while ((n = rb_first(&fs_info->qgroup_tree))) {
  428. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  429. rb_erase(n, &fs_info->qgroup_tree);
  430. __del_qgroup_rb(qgroup);
  431. }
  432. /*
  433. * we call btrfs_free_qgroup_config() when umounting
  434. * filesystem and disabling quota, so we set qgroup_ulist
  435. * to be null here to avoid double free.
  436. */
  437. ulist_free(fs_info->qgroup_ulist);
  438. fs_info->qgroup_ulist = NULL;
  439. }
  440. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  441. struct btrfs_root *quota_root,
  442. u64 src, u64 dst)
  443. {
  444. int ret;
  445. struct btrfs_path *path;
  446. struct btrfs_key key;
  447. path = btrfs_alloc_path();
  448. if (!path)
  449. return -ENOMEM;
  450. key.objectid = src;
  451. key.type = BTRFS_QGROUP_RELATION_KEY;
  452. key.offset = dst;
  453. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  454. btrfs_mark_buffer_dirty(path->nodes[0]);
  455. btrfs_free_path(path);
  456. return ret;
  457. }
  458. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  459. struct btrfs_root *quota_root,
  460. u64 src, u64 dst)
  461. {
  462. int ret;
  463. struct btrfs_path *path;
  464. struct btrfs_key key;
  465. path = btrfs_alloc_path();
  466. if (!path)
  467. return -ENOMEM;
  468. key.objectid = src;
  469. key.type = BTRFS_QGROUP_RELATION_KEY;
  470. key.offset = dst;
  471. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  472. if (ret < 0)
  473. goto out;
  474. if (ret > 0) {
  475. ret = -ENOENT;
  476. goto out;
  477. }
  478. ret = btrfs_del_item(trans, quota_root, path);
  479. out:
  480. btrfs_free_path(path);
  481. return ret;
  482. }
  483. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  484. struct btrfs_root *quota_root, u64 qgroupid)
  485. {
  486. int ret;
  487. struct btrfs_path *path;
  488. struct btrfs_qgroup_info_item *qgroup_info;
  489. struct btrfs_qgroup_limit_item *qgroup_limit;
  490. struct extent_buffer *leaf;
  491. struct btrfs_key key;
  492. if (btrfs_is_testing(quota_root->fs_info))
  493. return 0;
  494. path = btrfs_alloc_path();
  495. if (!path)
  496. return -ENOMEM;
  497. key.objectid = 0;
  498. key.type = BTRFS_QGROUP_INFO_KEY;
  499. key.offset = qgroupid;
  500. /*
  501. * Avoid a transaction abort by catching -EEXIST here. In that
  502. * case, we proceed by re-initializing the existing structure
  503. * on disk.
  504. */
  505. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  506. sizeof(*qgroup_info));
  507. if (ret && ret != -EEXIST)
  508. goto out;
  509. leaf = path->nodes[0];
  510. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  511. struct btrfs_qgroup_info_item);
  512. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  513. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  514. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  515. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  516. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  517. btrfs_mark_buffer_dirty(leaf);
  518. btrfs_release_path(path);
  519. key.type = BTRFS_QGROUP_LIMIT_KEY;
  520. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  521. sizeof(*qgroup_limit));
  522. if (ret && ret != -EEXIST)
  523. goto out;
  524. leaf = path->nodes[0];
  525. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  526. struct btrfs_qgroup_limit_item);
  527. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  528. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  529. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  530. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  531. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  532. btrfs_mark_buffer_dirty(leaf);
  533. ret = 0;
  534. out:
  535. btrfs_free_path(path);
  536. return ret;
  537. }
  538. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  539. struct btrfs_root *quota_root, u64 qgroupid)
  540. {
  541. int ret;
  542. struct btrfs_path *path;
  543. struct btrfs_key key;
  544. path = btrfs_alloc_path();
  545. if (!path)
  546. return -ENOMEM;
  547. key.objectid = 0;
  548. key.type = BTRFS_QGROUP_INFO_KEY;
  549. key.offset = qgroupid;
  550. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  551. if (ret < 0)
  552. goto out;
  553. if (ret > 0) {
  554. ret = -ENOENT;
  555. goto out;
  556. }
  557. ret = btrfs_del_item(trans, quota_root, path);
  558. if (ret)
  559. goto out;
  560. btrfs_release_path(path);
  561. key.type = BTRFS_QGROUP_LIMIT_KEY;
  562. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  563. if (ret < 0)
  564. goto out;
  565. if (ret > 0) {
  566. ret = -ENOENT;
  567. goto out;
  568. }
  569. ret = btrfs_del_item(trans, quota_root, path);
  570. out:
  571. btrfs_free_path(path);
  572. return ret;
  573. }
  574. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  575. struct btrfs_root *root,
  576. struct btrfs_qgroup *qgroup)
  577. {
  578. struct btrfs_path *path;
  579. struct btrfs_key key;
  580. struct extent_buffer *l;
  581. struct btrfs_qgroup_limit_item *qgroup_limit;
  582. int ret;
  583. int slot;
  584. key.objectid = 0;
  585. key.type = BTRFS_QGROUP_LIMIT_KEY;
  586. key.offset = qgroup->qgroupid;
  587. path = btrfs_alloc_path();
  588. if (!path)
  589. return -ENOMEM;
  590. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  591. if (ret > 0)
  592. ret = -ENOENT;
  593. if (ret)
  594. goto out;
  595. l = path->nodes[0];
  596. slot = path->slots[0];
  597. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  598. btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
  599. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
  600. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
  601. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
  602. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
  603. btrfs_mark_buffer_dirty(l);
  604. out:
  605. btrfs_free_path(path);
  606. return ret;
  607. }
  608. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  609. struct btrfs_root *root,
  610. struct btrfs_qgroup *qgroup)
  611. {
  612. struct btrfs_path *path;
  613. struct btrfs_key key;
  614. struct extent_buffer *l;
  615. struct btrfs_qgroup_info_item *qgroup_info;
  616. int ret;
  617. int slot;
  618. if (btrfs_is_testing(root->fs_info))
  619. return 0;
  620. key.objectid = 0;
  621. key.type = BTRFS_QGROUP_INFO_KEY;
  622. key.offset = qgroup->qgroupid;
  623. path = btrfs_alloc_path();
  624. if (!path)
  625. return -ENOMEM;
  626. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  627. if (ret > 0)
  628. ret = -ENOENT;
  629. if (ret)
  630. goto out;
  631. l = path->nodes[0];
  632. slot = path->slots[0];
  633. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  634. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  635. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  636. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  637. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  638. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  639. btrfs_mark_buffer_dirty(l);
  640. out:
  641. btrfs_free_path(path);
  642. return ret;
  643. }
  644. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  645. struct btrfs_fs_info *fs_info,
  646. struct btrfs_root *root)
  647. {
  648. struct btrfs_path *path;
  649. struct btrfs_key key;
  650. struct extent_buffer *l;
  651. struct btrfs_qgroup_status_item *ptr;
  652. int ret;
  653. int slot;
  654. key.objectid = 0;
  655. key.type = BTRFS_QGROUP_STATUS_KEY;
  656. key.offset = 0;
  657. path = btrfs_alloc_path();
  658. if (!path)
  659. return -ENOMEM;
  660. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  661. if (ret > 0)
  662. ret = -ENOENT;
  663. if (ret)
  664. goto out;
  665. l = path->nodes[0];
  666. slot = path->slots[0];
  667. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  668. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  669. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  670. btrfs_set_qgroup_status_rescan(l, ptr,
  671. fs_info->qgroup_rescan_progress.objectid);
  672. btrfs_mark_buffer_dirty(l);
  673. out:
  674. btrfs_free_path(path);
  675. return ret;
  676. }
  677. /*
  678. * called with qgroup_lock held
  679. */
  680. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  681. struct btrfs_root *root)
  682. {
  683. struct btrfs_path *path;
  684. struct btrfs_key key;
  685. struct extent_buffer *leaf = NULL;
  686. int ret;
  687. int nr = 0;
  688. path = btrfs_alloc_path();
  689. if (!path)
  690. return -ENOMEM;
  691. path->leave_spinning = 1;
  692. key.objectid = 0;
  693. key.offset = 0;
  694. key.type = 0;
  695. while (1) {
  696. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  697. if (ret < 0)
  698. goto out;
  699. leaf = path->nodes[0];
  700. nr = btrfs_header_nritems(leaf);
  701. if (!nr)
  702. break;
  703. /*
  704. * delete the leaf one by one
  705. * since the whole tree is going
  706. * to be deleted.
  707. */
  708. path->slots[0] = 0;
  709. ret = btrfs_del_items(trans, root, path, 0, nr);
  710. if (ret)
  711. goto out;
  712. btrfs_release_path(path);
  713. }
  714. ret = 0;
  715. out:
  716. set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
  717. btrfs_free_path(path);
  718. return ret;
  719. }
  720. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  721. struct btrfs_fs_info *fs_info)
  722. {
  723. struct btrfs_root *quota_root;
  724. struct btrfs_root *tree_root = fs_info->tree_root;
  725. struct btrfs_path *path = NULL;
  726. struct btrfs_qgroup_status_item *ptr;
  727. struct extent_buffer *leaf;
  728. struct btrfs_key key;
  729. struct btrfs_key found_key;
  730. struct btrfs_qgroup *qgroup = NULL;
  731. int ret = 0;
  732. int slot;
  733. mutex_lock(&fs_info->qgroup_ioctl_lock);
  734. if (fs_info->quota_root) {
  735. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  736. goto out;
  737. }
  738. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  739. if (!fs_info->qgroup_ulist) {
  740. ret = -ENOMEM;
  741. goto out;
  742. }
  743. /*
  744. * initially create the quota tree
  745. */
  746. quota_root = btrfs_create_tree(trans, fs_info,
  747. BTRFS_QUOTA_TREE_OBJECTID);
  748. if (IS_ERR(quota_root)) {
  749. ret = PTR_ERR(quota_root);
  750. goto out;
  751. }
  752. path = btrfs_alloc_path();
  753. if (!path) {
  754. ret = -ENOMEM;
  755. goto out_free_root;
  756. }
  757. key.objectid = 0;
  758. key.type = BTRFS_QGROUP_STATUS_KEY;
  759. key.offset = 0;
  760. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  761. sizeof(*ptr));
  762. if (ret)
  763. goto out_free_path;
  764. leaf = path->nodes[0];
  765. ptr = btrfs_item_ptr(leaf, path->slots[0],
  766. struct btrfs_qgroup_status_item);
  767. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  768. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  769. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  770. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  771. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  772. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  773. btrfs_mark_buffer_dirty(leaf);
  774. key.objectid = 0;
  775. key.type = BTRFS_ROOT_REF_KEY;
  776. key.offset = 0;
  777. btrfs_release_path(path);
  778. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  779. if (ret > 0)
  780. goto out_add_root;
  781. if (ret < 0)
  782. goto out_free_path;
  783. while (1) {
  784. slot = path->slots[0];
  785. leaf = path->nodes[0];
  786. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  787. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  788. ret = add_qgroup_item(trans, quota_root,
  789. found_key.offset);
  790. if (ret)
  791. goto out_free_path;
  792. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  793. if (IS_ERR(qgroup)) {
  794. ret = PTR_ERR(qgroup);
  795. goto out_free_path;
  796. }
  797. }
  798. ret = btrfs_next_item(tree_root, path);
  799. if (ret < 0)
  800. goto out_free_path;
  801. if (ret)
  802. break;
  803. }
  804. out_add_root:
  805. btrfs_release_path(path);
  806. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  807. if (ret)
  808. goto out_free_path;
  809. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  810. if (IS_ERR(qgroup)) {
  811. ret = PTR_ERR(qgroup);
  812. goto out_free_path;
  813. }
  814. spin_lock(&fs_info->qgroup_lock);
  815. fs_info->quota_root = quota_root;
  816. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  817. spin_unlock(&fs_info->qgroup_lock);
  818. out_free_path:
  819. btrfs_free_path(path);
  820. out_free_root:
  821. if (ret) {
  822. free_extent_buffer(quota_root->node);
  823. free_extent_buffer(quota_root->commit_root);
  824. kfree(quota_root);
  825. }
  826. out:
  827. if (ret) {
  828. ulist_free(fs_info->qgroup_ulist);
  829. fs_info->qgroup_ulist = NULL;
  830. }
  831. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  832. return ret;
  833. }
  834. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  835. struct btrfs_fs_info *fs_info)
  836. {
  837. struct btrfs_root *tree_root = fs_info->tree_root;
  838. struct btrfs_root *quota_root;
  839. int ret = 0;
  840. mutex_lock(&fs_info->qgroup_ioctl_lock);
  841. if (!fs_info->quota_root)
  842. goto out;
  843. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  844. set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
  845. btrfs_qgroup_wait_for_completion(fs_info, false);
  846. spin_lock(&fs_info->qgroup_lock);
  847. quota_root = fs_info->quota_root;
  848. fs_info->quota_root = NULL;
  849. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  850. spin_unlock(&fs_info->qgroup_lock);
  851. btrfs_free_qgroup_config(fs_info);
  852. ret = btrfs_clean_quota_tree(trans, quota_root);
  853. if (ret)
  854. goto out;
  855. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  856. if (ret)
  857. goto out;
  858. list_del(&quota_root->dirty_list);
  859. btrfs_tree_lock(quota_root->node);
  860. clean_tree_block(trans, tree_root->fs_info, quota_root->node);
  861. btrfs_tree_unlock(quota_root->node);
  862. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  863. free_extent_buffer(quota_root->node);
  864. free_extent_buffer(quota_root->commit_root);
  865. kfree(quota_root);
  866. out:
  867. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  868. return ret;
  869. }
  870. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  871. struct btrfs_qgroup *qgroup)
  872. {
  873. if (list_empty(&qgroup->dirty))
  874. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  875. }
  876. /*
  877. * The easy accounting, if we are adding/removing the only ref for an extent
  878. * then this qgroup and all of the parent qgroups get their reference and
  879. * exclusive counts adjusted.
  880. *
  881. * Caller should hold fs_info->qgroup_lock.
  882. */
  883. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  884. struct ulist *tmp, u64 ref_root,
  885. u64 num_bytes, int sign)
  886. {
  887. struct btrfs_qgroup *qgroup;
  888. struct btrfs_qgroup_list *glist;
  889. struct ulist_node *unode;
  890. struct ulist_iterator uiter;
  891. int ret = 0;
  892. qgroup = find_qgroup_rb(fs_info, ref_root);
  893. if (!qgroup)
  894. goto out;
  895. qgroup->rfer += sign * num_bytes;
  896. qgroup->rfer_cmpr += sign * num_bytes;
  897. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  898. qgroup->excl += sign * num_bytes;
  899. qgroup->excl_cmpr += sign * num_bytes;
  900. if (sign > 0)
  901. qgroup->reserved -= num_bytes;
  902. qgroup_dirty(fs_info, qgroup);
  903. /* Get all of the parent groups that contain this qgroup */
  904. list_for_each_entry(glist, &qgroup->groups, next_group) {
  905. ret = ulist_add(tmp, glist->group->qgroupid,
  906. ptr_to_u64(glist->group), GFP_ATOMIC);
  907. if (ret < 0)
  908. goto out;
  909. }
  910. /* Iterate all of the parents and adjust their reference counts */
  911. ULIST_ITER_INIT(&uiter);
  912. while ((unode = ulist_next(tmp, &uiter))) {
  913. qgroup = u64_to_ptr(unode->aux);
  914. qgroup->rfer += sign * num_bytes;
  915. qgroup->rfer_cmpr += sign * num_bytes;
  916. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  917. qgroup->excl += sign * num_bytes;
  918. if (sign > 0)
  919. qgroup->reserved -= num_bytes;
  920. qgroup->excl_cmpr += sign * num_bytes;
  921. qgroup_dirty(fs_info, qgroup);
  922. /* Add any parents of the parents */
  923. list_for_each_entry(glist, &qgroup->groups, next_group) {
  924. ret = ulist_add(tmp, glist->group->qgroupid,
  925. ptr_to_u64(glist->group), GFP_ATOMIC);
  926. if (ret < 0)
  927. goto out;
  928. }
  929. }
  930. ret = 0;
  931. out:
  932. return ret;
  933. }
  934. /*
  935. * Quick path for updating qgroup with only excl refs.
  936. *
  937. * In that case, just update all parent will be enough.
  938. * Or we needs to do a full rescan.
  939. * Caller should also hold fs_info->qgroup_lock.
  940. *
  941. * Return 0 for quick update, return >0 for need to full rescan
  942. * and mark INCONSISTENT flag.
  943. * Return < 0 for other error.
  944. */
  945. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  946. struct ulist *tmp, u64 src, u64 dst,
  947. int sign)
  948. {
  949. struct btrfs_qgroup *qgroup;
  950. int ret = 1;
  951. int err = 0;
  952. qgroup = find_qgroup_rb(fs_info, src);
  953. if (!qgroup)
  954. goto out;
  955. if (qgroup->excl == qgroup->rfer) {
  956. ret = 0;
  957. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  958. qgroup->excl, sign);
  959. if (err < 0) {
  960. ret = err;
  961. goto out;
  962. }
  963. }
  964. out:
  965. if (ret)
  966. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  967. return ret;
  968. }
  969. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  970. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  971. {
  972. struct btrfs_root *quota_root;
  973. struct btrfs_qgroup *parent;
  974. struct btrfs_qgroup *member;
  975. struct btrfs_qgroup_list *list;
  976. struct ulist *tmp;
  977. int ret = 0;
  978. /* Check the level of src and dst first */
  979. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  980. return -EINVAL;
  981. tmp = ulist_alloc(GFP_NOFS);
  982. if (!tmp)
  983. return -ENOMEM;
  984. mutex_lock(&fs_info->qgroup_ioctl_lock);
  985. quota_root = fs_info->quota_root;
  986. if (!quota_root) {
  987. ret = -EINVAL;
  988. goto out;
  989. }
  990. member = find_qgroup_rb(fs_info, src);
  991. parent = find_qgroup_rb(fs_info, dst);
  992. if (!member || !parent) {
  993. ret = -EINVAL;
  994. goto out;
  995. }
  996. /* check if such qgroup relation exist firstly */
  997. list_for_each_entry(list, &member->groups, next_group) {
  998. if (list->group == parent) {
  999. ret = -EEXIST;
  1000. goto out;
  1001. }
  1002. }
  1003. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  1004. if (ret)
  1005. goto out;
  1006. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  1007. if (ret) {
  1008. del_qgroup_relation_item(trans, quota_root, src, dst);
  1009. goto out;
  1010. }
  1011. spin_lock(&fs_info->qgroup_lock);
  1012. ret = add_relation_rb(quota_root->fs_info, src, dst);
  1013. if (ret < 0) {
  1014. spin_unlock(&fs_info->qgroup_lock);
  1015. goto out;
  1016. }
  1017. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1018. spin_unlock(&fs_info->qgroup_lock);
  1019. out:
  1020. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1021. ulist_free(tmp);
  1022. return ret;
  1023. }
  1024. int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1025. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1026. {
  1027. struct btrfs_root *quota_root;
  1028. struct btrfs_qgroup *parent;
  1029. struct btrfs_qgroup *member;
  1030. struct btrfs_qgroup_list *list;
  1031. struct ulist *tmp;
  1032. int ret = 0;
  1033. int err;
  1034. tmp = ulist_alloc(GFP_NOFS);
  1035. if (!tmp)
  1036. return -ENOMEM;
  1037. quota_root = fs_info->quota_root;
  1038. if (!quota_root) {
  1039. ret = -EINVAL;
  1040. goto out;
  1041. }
  1042. member = find_qgroup_rb(fs_info, src);
  1043. parent = find_qgroup_rb(fs_info, dst);
  1044. if (!member || !parent) {
  1045. ret = -EINVAL;
  1046. goto out;
  1047. }
  1048. /* check if such qgroup relation exist firstly */
  1049. list_for_each_entry(list, &member->groups, next_group) {
  1050. if (list->group == parent)
  1051. goto exist;
  1052. }
  1053. ret = -ENOENT;
  1054. goto out;
  1055. exist:
  1056. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1057. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1058. if (err && !ret)
  1059. ret = err;
  1060. spin_lock(&fs_info->qgroup_lock);
  1061. del_relation_rb(fs_info, src, dst);
  1062. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1063. spin_unlock(&fs_info->qgroup_lock);
  1064. out:
  1065. ulist_free(tmp);
  1066. return ret;
  1067. }
  1068. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1069. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1070. {
  1071. int ret = 0;
  1072. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1073. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1074. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1075. return ret;
  1076. }
  1077. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1078. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1079. {
  1080. struct btrfs_root *quota_root;
  1081. struct btrfs_qgroup *qgroup;
  1082. int ret = 0;
  1083. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1084. quota_root = fs_info->quota_root;
  1085. if (!quota_root) {
  1086. ret = -EINVAL;
  1087. goto out;
  1088. }
  1089. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1090. if (qgroup) {
  1091. ret = -EEXIST;
  1092. goto out;
  1093. }
  1094. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1095. if (ret)
  1096. goto out;
  1097. spin_lock(&fs_info->qgroup_lock);
  1098. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1099. spin_unlock(&fs_info->qgroup_lock);
  1100. if (IS_ERR(qgroup))
  1101. ret = PTR_ERR(qgroup);
  1102. out:
  1103. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1104. return ret;
  1105. }
  1106. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1107. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1108. {
  1109. struct btrfs_root *quota_root;
  1110. struct btrfs_qgroup *qgroup;
  1111. struct btrfs_qgroup_list *list;
  1112. int ret = 0;
  1113. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1114. quota_root = fs_info->quota_root;
  1115. if (!quota_root) {
  1116. ret = -EINVAL;
  1117. goto out;
  1118. }
  1119. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1120. if (!qgroup) {
  1121. ret = -ENOENT;
  1122. goto out;
  1123. } else {
  1124. /* check if there are no children of this qgroup */
  1125. if (!list_empty(&qgroup->members)) {
  1126. ret = -EBUSY;
  1127. goto out;
  1128. }
  1129. }
  1130. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1131. while (!list_empty(&qgroup->groups)) {
  1132. list = list_first_entry(&qgroup->groups,
  1133. struct btrfs_qgroup_list, next_group);
  1134. ret = __del_qgroup_relation(trans, fs_info,
  1135. qgroupid,
  1136. list->group->qgroupid);
  1137. if (ret)
  1138. goto out;
  1139. }
  1140. spin_lock(&fs_info->qgroup_lock);
  1141. del_qgroup_rb(quota_root->fs_info, qgroupid);
  1142. spin_unlock(&fs_info->qgroup_lock);
  1143. out:
  1144. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1145. return ret;
  1146. }
  1147. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1148. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1149. struct btrfs_qgroup_limit *limit)
  1150. {
  1151. struct btrfs_root *quota_root;
  1152. struct btrfs_qgroup *qgroup;
  1153. int ret = 0;
  1154. /* Sometimes we would want to clear the limit on this qgroup.
  1155. * To meet this requirement, we treat the -1 as a special value
  1156. * which tell kernel to clear the limit on this qgroup.
  1157. */
  1158. const u64 CLEAR_VALUE = -1;
  1159. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1160. quota_root = fs_info->quota_root;
  1161. if (!quota_root) {
  1162. ret = -EINVAL;
  1163. goto out;
  1164. }
  1165. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1166. if (!qgroup) {
  1167. ret = -ENOENT;
  1168. goto out;
  1169. }
  1170. spin_lock(&fs_info->qgroup_lock);
  1171. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1172. if (limit->max_rfer == CLEAR_VALUE) {
  1173. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1174. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1175. qgroup->max_rfer = 0;
  1176. } else {
  1177. qgroup->max_rfer = limit->max_rfer;
  1178. }
  1179. }
  1180. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1181. if (limit->max_excl == CLEAR_VALUE) {
  1182. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1183. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1184. qgroup->max_excl = 0;
  1185. } else {
  1186. qgroup->max_excl = limit->max_excl;
  1187. }
  1188. }
  1189. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1190. if (limit->rsv_rfer == CLEAR_VALUE) {
  1191. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1192. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1193. qgroup->rsv_rfer = 0;
  1194. } else {
  1195. qgroup->rsv_rfer = limit->rsv_rfer;
  1196. }
  1197. }
  1198. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1199. if (limit->rsv_excl == CLEAR_VALUE) {
  1200. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1201. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1202. qgroup->rsv_excl = 0;
  1203. } else {
  1204. qgroup->rsv_excl = limit->rsv_excl;
  1205. }
  1206. }
  1207. qgroup->lim_flags |= limit->flags;
  1208. spin_unlock(&fs_info->qgroup_lock);
  1209. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1210. if (ret) {
  1211. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1212. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1213. qgroupid);
  1214. }
  1215. out:
  1216. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1217. return ret;
  1218. }
  1219. int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
  1220. struct btrfs_fs_info *fs_info)
  1221. {
  1222. struct btrfs_qgroup_extent_record *record;
  1223. struct btrfs_delayed_ref_root *delayed_refs;
  1224. struct rb_node *node;
  1225. u64 qgroup_to_skip;
  1226. int ret = 0;
  1227. delayed_refs = &trans->transaction->delayed_refs;
  1228. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1229. /*
  1230. * No need to do lock, since this function will only be called in
  1231. * btrfs_commit_transaction().
  1232. */
  1233. node = rb_first(&delayed_refs->dirty_extent_root);
  1234. while (node) {
  1235. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1236. node);
  1237. ret = btrfs_find_all_roots(NULL, fs_info, record->bytenr, 0,
  1238. &record->old_roots);
  1239. if (ret < 0)
  1240. break;
  1241. if (qgroup_to_skip)
  1242. ulist_del(record->old_roots, qgroup_to_skip, 0);
  1243. node = rb_next(node);
  1244. }
  1245. return ret;
  1246. }
  1247. int btrfs_qgroup_insert_dirty_extent_nolock(struct btrfs_fs_info *fs_info,
  1248. struct btrfs_delayed_ref_root *delayed_refs,
  1249. struct btrfs_qgroup_extent_record *record)
  1250. {
  1251. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1252. struct rb_node *parent_node = NULL;
  1253. struct btrfs_qgroup_extent_record *entry;
  1254. u64 bytenr = record->bytenr;
  1255. assert_spin_locked(&delayed_refs->lock);
  1256. trace_btrfs_qgroup_insert_dirty_extent(fs_info, record);
  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 1;
  1267. }
  1268. rb_link_node(&record->node, parent_node, p);
  1269. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1270. return 0;
  1271. }
  1272. int btrfs_qgroup_insert_dirty_extent(struct btrfs_trans_handle *trans,
  1273. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  1274. gfp_t gfp_flag)
  1275. {
  1276. struct btrfs_qgroup_extent_record *record;
  1277. struct btrfs_delayed_ref_root *delayed_refs;
  1278. int ret;
  1279. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
  1280. || bytenr == 0 || num_bytes == 0)
  1281. return 0;
  1282. if (WARN_ON(trans == NULL))
  1283. return -EINVAL;
  1284. record = kmalloc(sizeof(*record), gfp_flag);
  1285. if (!record)
  1286. return -ENOMEM;
  1287. delayed_refs = &trans->transaction->delayed_refs;
  1288. record->bytenr = bytenr;
  1289. record->num_bytes = num_bytes;
  1290. record->old_roots = NULL;
  1291. spin_lock(&delayed_refs->lock);
  1292. ret = btrfs_qgroup_insert_dirty_extent_nolock(fs_info, delayed_refs,
  1293. record);
  1294. spin_unlock(&delayed_refs->lock);
  1295. if (ret > 0)
  1296. kfree(record);
  1297. return 0;
  1298. }
  1299. #define UPDATE_NEW 0
  1300. #define UPDATE_OLD 1
  1301. /*
  1302. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1303. */
  1304. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1305. struct ulist *roots, struct ulist *tmp,
  1306. struct ulist *qgroups, u64 seq, int update_old)
  1307. {
  1308. struct ulist_node *unode;
  1309. struct ulist_iterator uiter;
  1310. struct ulist_node *tmp_unode;
  1311. struct ulist_iterator tmp_uiter;
  1312. struct btrfs_qgroup *qg;
  1313. int ret = 0;
  1314. if (!roots)
  1315. return 0;
  1316. ULIST_ITER_INIT(&uiter);
  1317. while ((unode = ulist_next(roots, &uiter))) {
  1318. qg = find_qgroup_rb(fs_info, unode->val);
  1319. if (!qg)
  1320. continue;
  1321. ulist_reinit(tmp);
  1322. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1323. GFP_ATOMIC);
  1324. if (ret < 0)
  1325. return ret;
  1326. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1327. if (ret < 0)
  1328. return ret;
  1329. ULIST_ITER_INIT(&tmp_uiter);
  1330. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1331. struct btrfs_qgroup_list *glist;
  1332. qg = u64_to_ptr(tmp_unode->aux);
  1333. if (update_old)
  1334. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1335. else
  1336. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1337. list_for_each_entry(glist, &qg->groups, next_group) {
  1338. ret = ulist_add(qgroups, glist->group->qgroupid,
  1339. ptr_to_u64(glist->group),
  1340. GFP_ATOMIC);
  1341. if (ret < 0)
  1342. return ret;
  1343. ret = ulist_add(tmp, glist->group->qgroupid,
  1344. ptr_to_u64(glist->group),
  1345. GFP_ATOMIC);
  1346. if (ret < 0)
  1347. return ret;
  1348. }
  1349. }
  1350. }
  1351. return 0;
  1352. }
  1353. /*
  1354. * Update qgroup rfer/excl counters.
  1355. * Rfer update is easy, codes can explain themselves.
  1356. *
  1357. * Excl update is tricky, the update is split into 2 part.
  1358. * Part 1: Possible exclusive <-> sharing detect:
  1359. * | A | !A |
  1360. * -------------------------------------
  1361. * B | * | - |
  1362. * -------------------------------------
  1363. * !B | + | ** |
  1364. * -------------------------------------
  1365. *
  1366. * Conditions:
  1367. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1368. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1369. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1370. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  1371. *
  1372. * Results:
  1373. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1374. * *: Definitely not changed. **: Possible unchanged.
  1375. *
  1376. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1377. *
  1378. * To make the logic clear, we first use condition A and B to split
  1379. * combination into 4 results.
  1380. *
  1381. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1382. * only on variant maybe 0.
  1383. *
  1384. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1385. * again(2x2).
  1386. * But this time we don't need to consider other things, the codes and logic
  1387. * is easy to understand now.
  1388. */
  1389. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1390. struct ulist *qgroups,
  1391. u64 nr_old_roots,
  1392. u64 nr_new_roots,
  1393. u64 num_bytes, u64 seq)
  1394. {
  1395. struct ulist_node *unode;
  1396. struct ulist_iterator uiter;
  1397. struct btrfs_qgroup *qg;
  1398. u64 cur_new_count, cur_old_count;
  1399. ULIST_ITER_INIT(&uiter);
  1400. while ((unode = ulist_next(qgroups, &uiter))) {
  1401. bool dirty = false;
  1402. qg = u64_to_ptr(unode->aux);
  1403. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1404. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1405. trace_qgroup_update_counters(fs_info, qg->qgroupid,
  1406. cur_old_count, cur_new_count);
  1407. /* Rfer update part */
  1408. if (cur_old_count == 0 && cur_new_count > 0) {
  1409. qg->rfer += num_bytes;
  1410. qg->rfer_cmpr += num_bytes;
  1411. dirty = true;
  1412. }
  1413. if (cur_old_count > 0 && cur_new_count == 0) {
  1414. qg->rfer -= num_bytes;
  1415. qg->rfer_cmpr -= num_bytes;
  1416. dirty = true;
  1417. }
  1418. /* Excl update part */
  1419. /* Exclusive/none -> shared case */
  1420. if (cur_old_count == nr_old_roots &&
  1421. cur_new_count < nr_new_roots) {
  1422. /* Exclusive -> shared */
  1423. if (cur_old_count != 0) {
  1424. qg->excl -= num_bytes;
  1425. qg->excl_cmpr -= num_bytes;
  1426. dirty = true;
  1427. }
  1428. }
  1429. /* Shared -> exclusive/none case */
  1430. if (cur_old_count < nr_old_roots &&
  1431. cur_new_count == nr_new_roots) {
  1432. /* Shared->exclusive */
  1433. if (cur_new_count != 0) {
  1434. qg->excl += num_bytes;
  1435. qg->excl_cmpr += num_bytes;
  1436. dirty = true;
  1437. }
  1438. }
  1439. /* Exclusive/none -> exclusive/none case */
  1440. if (cur_old_count == nr_old_roots &&
  1441. cur_new_count == nr_new_roots) {
  1442. if (cur_old_count == 0) {
  1443. /* None -> exclusive/none */
  1444. if (cur_new_count != 0) {
  1445. /* None -> exclusive */
  1446. qg->excl += num_bytes;
  1447. qg->excl_cmpr += num_bytes;
  1448. dirty = true;
  1449. }
  1450. /* None -> none, nothing changed */
  1451. } else {
  1452. /* Exclusive -> exclusive/none */
  1453. if (cur_new_count == 0) {
  1454. /* Exclusive -> none */
  1455. qg->excl -= num_bytes;
  1456. qg->excl_cmpr -= num_bytes;
  1457. dirty = true;
  1458. }
  1459. /* Exclusive -> exclusive, nothing changed */
  1460. }
  1461. }
  1462. if (dirty)
  1463. qgroup_dirty(fs_info, qg);
  1464. }
  1465. return 0;
  1466. }
  1467. int
  1468. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1469. struct btrfs_fs_info *fs_info,
  1470. u64 bytenr, u64 num_bytes,
  1471. struct ulist *old_roots, struct ulist *new_roots)
  1472. {
  1473. struct ulist *qgroups = NULL;
  1474. struct ulist *tmp = NULL;
  1475. u64 seq;
  1476. u64 nr_new_roots = 0;
  1477. u64 nr_old_roots = 0;
  1478. int ret = 0;
  1479. if (new_roots)
  1480. nr_new_roots = new_roots->nnodes;
  1481. if (old_roots)
  1482. nr_old_roots = old_roots->nnodes;
  1483. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1484. goto out_free;
  1485. BUG_ON(!fs_info->quota_root);
  1486. trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
  1487. nr_old_roots, nr_new_roots);
  1488. qgroups = ulist_alloc(GFP_NOFS);
  1489. if (!qgroups) {
  1490. ret = -ENOMEM;
  1491. goto out_free;
  1492. }
  1493. tmp = ulist_alloc(GFP_NOFS);
  1494. if (!tmp) {
  1495. ret = -ENOMEM;
  1496. goto out_free;
  1497. }
  1498. mutex_lock(&fs_info->qgroup_rescan_lock);
  1499. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1500. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1501. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1502. ret = 0;
  1503. goto out_free;
  1504. }
  1505. }
  1506. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1507. spin_lock(&fs_info->qgroup_lock);
  1508. seq = fs_info->qgroup_seq;
  1509. /* Update old refcnts using old_roots */
  1510. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1511. UPDATE_OLD);
  1512. if (ret < 0)
  1513. goto out;
  1514. /* Update new refcnts using new_roots */
  1515. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1516. UPDATE_NEW);
  1517. if (ret < 0)
  1518. goto out;
  1519. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1520. num_bytes, seq);
  1521. /*
  1522. * Bump qgroup_seq to avoid seq overlap
  1523. */
  1524. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1525. out:
  1526. spin_unlock(&fs_info->qgroup_lock);
  1527. out_free:
  1528. ulist_free(tmp);
  1529. ulist_free(qgroups);
  1530. ulist_free(old_roots);
  1531. ulist_free(new_roots);
  1532. return ret;
  1533. }
  1534. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1535. struct btrfs_fs_info *fs_info)
  1536. {
  1537. struct btrfs_qgroup_extent_record *record;
  1538. struct btrfs_delayed_ref_root *delayed_refs;
  1539. struct ulist *new_roots = NULL;
  1540. struct rb_node *node;
  1541. u64 qgroup_to_skip;
  1542. int ret = 0;
  1543. delayed_refs = &trans->transaction->delayed_refs;
  1544. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1545. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1546. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1547. node);
  1548. trace_btrfs_qgroup_account_extents(fs_info, record);
  1549. if (!ret) {
  1550. /*
  1551. * Use (u64)-1 as time_seq to do special search, which
  1552. * doesn't lock tree or delayed_refs and search current
  1553. * root. It's safe inside commit_transaction().
  1554. */
  1555. ret = btrfs_find_all_roots(trans, fs_info,
  1556. record->bytenr, (u64)-1, &new_roots);
  1557. if (ret < 0)
  1558. goto cleanup;
  1559. if (qgroup_to_skip)
  1560. ulist_del(new_roots, qgroup_to_skip, 0);
  1561. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1562. record->bytenr, record->num_bytes,
  1563. record->old_roots, new_roots);
  1564. record->old_roots = NULL;
  1565. new_roots = NULL;
  1566. }
  1567. cleanup:
  1568. ulist_free(record->old_roots);
  1569. ulist_free(new_roots);
  1570. new_roots = NULL;
  1571. rb_erase(node, &delayed_refs->dirty_extent_root);
  1572. kfree(record);
  1573. }
  1574. return ret;
  1575. }
  1576. /*
  1577. * called from commit_transaction. Writes all changed qgroups to disk.
  1578. */
  1579. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1580. struct btrfs_fs_info *fs_info)
  1581. {
  1582. struct btrfs_root *quota_root = fs_info->quota_root;
  1583. int ret = 0;
  1584. int start_rescan_worker = 0;
  1585. if (!quota_root)
  1586. goto out;
  1587. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
  1588. test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1589. start_rescan_worker = 1;
  1590. if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1591. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1592. if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
  1593. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1594. spin_lock(&fs_info->qgroup_lock);
  1595. while (!list_empty(&fs_info->dirty_qgroups)) {
  1596. struct btrfs_qgroup *qgroup;
  1597. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1598. struct btrfs_qgroup, dirty);
  1599. list_del_init(&qgroup->dirty);
  1600. spin_unlock(&fs_info->qgroup_lock);
  1601. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1602. if (ret)
  1603. fs_info->qgroup_flags |=
  1604. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1605. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1606. if (ret)
  1607. fs_info->qgroup_flags |=
  1608. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1609. spin_lock(&fs_info->qgroup_lock);
  1610. }
  1611. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1612. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1613. else
  1614. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1615. spin_unlock(&fs_info->qgroup_lock);
  1616. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1617. if (ret)
  1618. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1619. if (!ret && start_rescan_worker) {
  1620. ret = qgroup_rescan_init(fs_info, 0, 1);
  1621. if (!ret) {
  1622. qgroup_rescan_zero_tracking(fs_info);
  1623. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1624. &fs_info->qgroup_rescan_work);
  1625. }
  1626. ret = 0;
  1627. }
  1628. out:
  1629. return ret;
  1630. }
  1631. /*
  1632. * Copy the accounting information between qgroups. This is necessary
  1633. * when a snapshot or a subvolume is created. Throwing an error will
  1634. * cause a transaction abort so we take extra care here to only error
  1635. * when a readonly fs is a reasonable outcome.
  1636. */
  1637. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1638. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1639. struct btrfs_qgroup_inherit *inherit)
  1640. {
  1641. int ret = 0;
  1642. int i;
  1643. u64 *i_qgroups;
  1644. struct btrfs_root *quota_root = fs_info->quota_root;
  1645. struct btrfs_qgroup *srcgroup;
  1646. struct btrfs_qgroup *dstgroup;
  1647. u32 level_size = 0;
  1648. u64 nums;
  1649. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1650. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1651. goto out;
  1652. if (!quota_root) {
  1653. ret = -EINVAL;
  1654. goto out;
  1655. }
  1656. if (inherit) {
  1657. i_qgroups = (u64 *)(inherit + 1);
  1658. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1659. 2 * inherit->num_excl_copies;
  1660. for (i = 0; i < nums; ++i) {
  1661. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1662. /*
  1663. * Zero out invalid groups so we can ignore
  1664. * them later.
  1665. */
  1666. if (!srcgroup ||
  1667. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  1668. *i_qgroups = 0ULL;
  1669. ++i_qgroups;
  1670. }
  1671. }
  1672. /*
  1673. * create a tracking group for the subvol itself
  1674. */
  1675. ret = add_qgroup_item(trans, quota_root, objectid);
  1676. if (ret)
  1677. goto out;
  1678. if (srcid) {
  1679. struct btrfs_root *srcroot;
  1680. struct btrfs_key srckey;
  1681. srckey.objectid = srcid;
  1682. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1683. srckey.offset = (u64)-1;
  1684. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1685. if (IS_ERR(srcroot)) {
  1686. ret = PTR_ERR(srcroot);
  1687. goto out;
  1688. }
  1689. rcu_read_lock();
  1690. level_size = srcroot->nodesize;
  1691. rcu_read_unlock();
  1692. }
  1693. /*
  1694. * add qgroup to all inherited groups
  1695. */
  1696. if (inherit) {
  1697. i_qgroups = (u64 *)(inherit + 1);
  1698. for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
  1699. if (*i_qgroups == 0)
  1700. continue;
  1701. ret = add_qgroup_relation_item(trans, quota_root,
  1702. objectid, *i_qgroups);
  1703. if (ret && ret != -EEXIST)
  1704. goto out;
  1705. ret = add_qgroup_relation_item(trans, quota_root,
  1706. *i_qgroups, objectid);
  1707. if (ret && ret != -EEXIST)
  1708. goto out;
  1709. }
  1710. ret = 0;
  1711. }
  1712. spin_lock(&fs_info->qgroup_lock);
  1713. dstgroup = add_qgroup_rb(fs_info, objectid);
  1714. if (IS_ERR(dstgroup)) {
  1715. ret = PTR_ERR(dstgroup);
  1716. goto unlock;
  1717. }
  1718. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1719. dstgroup->lim_flags = inherit->lim.flags;
  1720. dstgroup->max_rfer = inherit->lim.max_rfer;
  1721. dstgroup->max_excl = inherit->lim.max_excl;
  1722. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1723. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1724. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1725. if (ret) {
  1726. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1727. btrfs_info(fs_info,
  1728. "unable to update quota limit for %llu",
  1729. dstgroup->qgroupid);
  1730. goto unlock;
  1731. }
  1732. }
  1733. if (srcid) {
  1734. srcgroup = find_qgroup_rb(fs_info, srcid);
  1735. if (!srcgroup)
  1736. goto unlock;
  1737. /*
  1738. * We call inherit after we clone the root in order to make sure
  1739. * our counts don't go crazy, so at this point the only
  1740. * difference between the two roots should be the root node.
  1741. */
  1742. dstgroup->rfer = srcgroup->rfer;
  1743. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1744. dstgroup->excl = level_size;
  1745. dstgroup->excl_cmpr = level_size;
  1746. srcgroup->excl = level_size;
  1747. srcgroup->excl_cmpr = level_size;
  1748. /* inherit the limit info */
  1749. dstgroup->lim_flags = srcgroup->lim_flags;
  1750. dstgroup->max_rfer = srcgroup->max_rfer;
  1751. dstgroup->max_excl = srcgroup->max_excl;
  1752. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1753. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1754. qgroup_dirty(fs_info, dstgroup);
  1755. qgroup_dirty(fs_info, srcgroup);
  1756. }
  1757. if (!inherit)
  1758. goto unlock;
  1759. i_qgroups = (u64 *)(inherit + 1);
  1760. for (i = 0; i < inherit->num_qgroups; ++i) {
  1761. if (*i_qgroups) {
  1762. ret = add_relation_rb(quota_root->fs_info, objectid,
  1763. *i_qgroups);
  1764. if (ret)
  1765. goto unlock;
  1766. }
  1767. ++i_qgroups;
  1768. }
  1769. for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
  1770. struct btrfs_qgroup *src;
  1771. struct btrfs_qgroup *dst;
  1772. if (!i_qgroups[0] || !i_qgroups[1])
  1773. continue;
  1774. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1775. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1776. if (!src || !dst) {
  1777. ret = -EINVAL;
  1778. goto unlock;
  1779. }
  1780. dst->rfer = src->rfer - level_size;
  1781. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1782. }
  1783. for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
  1784. struct btrfs_qgroup *src;
  1785. struct btrfs_qgroup *dst;
  1786. if (!i_qgroups[0] || !i_qgroups[1])
  1787. continue;
  1788. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1789. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1790. if (!src || !dst) {
  1791. ret = -EINVAL;
  1792. goto unlock;
  1793. }
  1794. dst->excl = src->excl + level_size;
  1795. dst->excl_cmpr = src->excl_cmpr + level_size;
  1796. }
  1797. unlock:
  1798. spin_unlock(&fs_info->qgroup_lock);
  1799. out:
  1800. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1801. return ret;
  1802. }
  1803. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1804. {
  1805. struct btrfs_root *quota_root;
  1806. struct btrfs_qgroup *qgroup;
  1807. struct btrfs_fs_info *fs_info = root->fs_info;
  1808. u64 ref_root = root->root_key.objectid;
  1809. int ret = 0;
  1810. struct ulist_node *unode;
  1811. struct ulist_iterator uiter;
  1812. if (!is_fstree(ref_root))
  1813. return 0;
  1814. if (num_bytes == 0)
  1815. return 0;
  1816. spin_lock(&fs_info->qgroup_lock);
  1817. quota_root = fs_info->quota_root;
  1818. if (!quota_root)
  1819. goto out;
  1820. qgroup = find_qgroup_rb(fs_info, ref_root);
  1821. if (!qgroup)
  1822. goto out;
  1823. /*
  1824. * in a first step, we check all affected qgroups if any limits would
  1825. * be exceeded
  1826. */
  1827. ulist_reinit(fs_info->qgroup_ulist);
  1828. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1829. (uintptr_t)qgroup, GFP_ATOMIC);
  1830. if (ret < 0)
  1831. goto out;
  1832. ULIST_ITER_INIT(&uiter);
  1833. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1834. struct btrfs_qgroup *qg;
  1835. struct btrfs_qgroup_list *glist;
  1836. qg = u64_to_ptr(unode->aux);
  1837. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1838. qg->reserved + (s64)qg->rfer + num_bytes >
  1839. qg->max_rfer) {
  1840. ret = -EDQUOT;
  1841. goto out;
  1842. }
  1843. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1844. qg->reserved + (s64)qg->excl + num_bytes >
  1845. qg->max_excl) {
  1846. ret = -EDQUOT;
  1847. goto out;
  1848. }
  1849. list_for_each_entry(glist, &qg->groups, next_group) {
  1850. ret = ulist_add(fs_info->qgroup_ulist,
  1851. glist->group->qgroupid,
  1852. (uintptr_t)glist->group, GFP_ATOMIC);
  1853. if (ret < 0)
  1854. goto out;
  1855. }
  1856. }
  1857. ret = 0;
  1858. /*
  1859. * no limits exceeded, now record the reservation into all qgroups
  1860. */
  1861. ULIST_ITER_INIT(&uiter);
  1862. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1863. struct btrfs_qgroup *qg;
  1864. qg = u64_to_ptr(unode->aux);
  1865. qg->reserved += num_bytes;
  1866. }
  1867. out:
  1868. spin_unlock(&fs_info->qgroup_lock);
  1869. return ret;
  1870. }
  1871. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  1872. u64 ref_root, u64 num_bytes)
  1873. {
  1874. struct btrfs_root *quota_root;
  1875. struct btrfs_qgroup *qgroup;
  1876. struct ulist_node *unode;
  1877. struct ulist_iterator uiter;
  1878. int ret = 0;
  1879. if (!is_fstree(ref_root))
  1880. return;
  1881. if (num_bytes == 0)
  1882. return;
  1883. spin_lock(&fs_info->qgroup_lock);
  1884. quota_root = fs_info->quota_root;
  1885. if (!quota_root)
  1886. goto out;
  1887. qgroup = find_qgroup_rb(fs_info, ref_root);
  1888. if (!qgroup)
  1889. goto out;
  1890. ulist_reinit(fs_info->qgroup_ulist);
  1891. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1892. (uintptr_t)qgroup, GFP_ATOMIC);
  1893. if (ret < 0)
  1894. goto out;
  1895. ULIST_ITER_INIT(&uiter);
  1896. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1897. struct btrfs_qgroup *qg;
  1898. struct btrfs_qgroup_list *glist;
  1899. qg = u64_to_ptr(unode->aux);
  1900. qg->reserved -= num_bytes;
  1901. list_for_each_entry(glist, &qg->groups, next_group) {
  1902. ret = ulist_add(fs_info->qgroup_ulist,
  1903. glist->group->qgroupid,
  1904. (uintptr_t)glist->group, GFP_ATOMIC);
  1905. if (ret < 0)
  1906. goto out;
  1907. }
  1908. }
  1909. out:
  1910. spin_unlock(&fs_info->qgroup_lock);
  1911. }
  1912. static inline void qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1913. {
  1914. return btrfs_qgroup_free_refroot(root->fs_info, root->objectid,
  1915. num_bytes);
  1916. }
  1917. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1918. {
  1919. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1920. return;
  1921. btrfs_err(trans->fs_info,
  1922. "qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x",
  1923. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1924. (u32)(trans->delayed_ref_elem.seq >> 32),
  1925. (u32)trans->delayed_ref_elem.seq);
  1926. BUG();
  1927. }
  1928. /*
  1929. * Check if the leaf is the last leaf. Which means all node pointers
  1930. * are at their last position.
  1931. */
  1932. static bool is_last_leaf(struct btrfs_path *path)
  1933. {
  1934. int i;
  1935. for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
  1936. if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
  1937. return false;
  1938. }
  1939. return true;
  1940. }
  1941. /*
  1942. * returns < 0 on error, 0 when more leafs are to be scanned.
  1943. * returns 1 when done.
  1944. */
  1945. static int
  1946. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1947. struct btrfs_trans_handle *trans)
  1948. {
  1949. struct btrfs_key found;
  1950. struct extent_buffer *scratch_leaf = NULL;
  1951. struct ulist *roots = NULL;
  1952. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  1953. u64 num_bytes;
  1954. bool done;
  1955. int slot;
  1956. int ret;
  1957. mutex_lock(&fs_info->qgroup_rescan_lock);
  1958. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1959. &fs_info->qgroup_rescan_progress,
  1960. path, 1, 0);
  1961. btrfs_debug(fs_info,
  1962. "current progress key (%llu %u %llu), search_slot ret %d",
  1963. fs_info->qgroup_rescan_progress.objectid,
  1964. fs_info->qgroup_rescan_progress.type,
  1965. fs_info->qgroup_rescan_progress.offset, ret);
  1966. if (ret) {
  1967. /*
  1968. * The rescan is about to end, we will not be scanning any
  1969. * further blocks. We cannot unset the RESCAN flag here, because
  1970. * we want to commit the transaction if everything went well.
  1971. * To make the live accounting work in this phase, we set our
  1972. * scan progress pointer such that every real extent objectid
  1973. * will be smaller.
  1974. */
  1975. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1976. btrfs_release_path(path);
  1977. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1978. return ret;
  1979. }
  1980. done = is_last_leaf(path);
  1981. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1982. btrfs_header_nritems(path->nodes[0]) - 1);
  1983. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1984. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1985. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  1986. if (!scratch_leaf) {
  1987. ret = -ENOMEM;
  1988. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1989. goto out;
  1990. }
  1991. extent_buffer_get(scratch_leaf);
  1992. btrfs_tree_read_lock(scratch_leaf);
  1993. btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
  1994. slot = path->slots[0];
  1995. btrfs_release_path(path);
  1996. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1997. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1998. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1999. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2000. found.type != BTRFS_METADATA_ITEM_KEY)
  2001. continue;
  2002. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2003. num_bytes = fs_info->extent_root->nodesize;
  2004. else
  2005. num_bytes = found.offset;
  2006. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2007. &roots);
  2008. if (ret < 0)
  2009. goto out;
  2010. /* For rescan, just pass old_roots as NULL */
  2011. ret = btrfs_qgroup_account_extent(trans, fs_info,
  2012. found.objectid, num_bytes, NULL, roots);
  2013. if (ret < 0)
  2014. goto out;
  2015. }
  2016. out:
  2017. if (scratch_leaf) {
  2018. btrfs_tree_read_unlock_blocking(scratch_leaf);
  2019. free_extent_buffer(scratch_leaf);
  2020. }
  2021. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2022. if (done && !ret)
  2023. ret = 1;
  2024. return ret;
  2025. }
  2026. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2027. {
  2028. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2029. qgroup_rescan_work);
  2030. struct btrfs_path *path;
  2031. struct btrfs_trans_handle *trans = NULL;
  2032. int err = -ENOMEM;
  2033. int ret = 0;
  2034. path = btrfs_alloc_path();
  2035. if (!path)
  2036. goto out;
  2037. err = 0;
  2038. while (!err && !btrfs_fs_closing(fs_info)) {
  2039. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2040. if (IS_ERR(trans)) {
  2041. err = PTR_ERR(trans);
  2042. break;
  2043. }
  2044. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  2045. err = -EINTR;
  2046. } else {
  2047. err = qgroup_rescan_leaf(fs_info, path, trans);
  2048. }
  2049. if (err > 0)
  2050. btrfs_commit_transaction(trans, fs_info->fs_root);
  2051. else
  2052. btrfs_end_transaction(trans, fs_info->fs_root);
  2053. }
  2054. out:
  2055. btrfs_free_path(path);
  2056. mutex_lock(&fs_info->qgroup_rescan_lock);
  2057. if (!btrfs_fs_closing(fs_info))
  2058. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2059. if (err > 0 &&
  2060. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2061. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2062. } else if (err < 0) {
  2063. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2064. }
  2065. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2066. /*
  2067. * only update status, since the previous part has already updated the
  2068. * qgroup info.
  2069. */
  2070. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2071. if (IS_ERR(trans)) {
  2072. err = PTR_ERR(trans);
  2073. btrfs_err(fs_info,
  2074. "fail to start transaction for status update: %d\n",
  2075. err);
  2076. goto done;
  2077. }
  2078. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2079. if (ret < 0) {
  2080. err = ret;
  2081. btrfs_err(fs_info, "fail to update qgroup status: %d", err);
  2082. }
  2083. btrfs_end_transaction(trans, fs_info->quota_root);
  2084. if (btrfs_fs_closing(fs_info)) {
  2085. btrfs_info(fs_info, "qgroup scan paused");
  2086. } else if (err >= 0) {
  2087. btrfs_info(fs_info, "qgroup scan completed%s",
  2088. err > 0 ? " (inconsistency flag cleared)" : "");
  2089. } else {
  2090. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2091. }
  2092. done:
  2093. mutex_lock(&fs_info->qgroup_rescan_lock);
  2094. fs_info->qgroup_rescan_running = false;
  2095. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2096. complete_all(&fs_info->qgroup_rescan_completion);
  2097. }
  2098. /*
  2099. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2100. * memory required for the rescan context.
  2101. */
  2102. static int
  2103. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2104. int init_flags)
  2105. {
  2106. int ret = 0;
  2107. if (!init_flags &&
  2108. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2109. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2110. ret = -EINVAL;
  2111. goto err;
  2112. }
  2113. mutex_lock(&fs_info->qgroup_rescan_lock);
  2114. spin_lock(&fs_info->qgroup_lock);
  2115. if (init_flags) {
  2116. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2117. ret = -EINPROGRESS;
  2118. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2119. ret = -EINVAL;
  2120. if (ret) {
  2121. spin_unlock(&fs_info->qgroup_lock);
  2122. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2123. goto err;
  2124. }
  2125. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2126. }
  2127. memset(&fs_info->qgroup_rescan_progress, 0,
  2128. sizeof(fs_info->qgroup_rescan_progress));
  2129. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2130. init_completion(&fs_info->qgroup_rescan_completion);
  2131. fs_info->qgroup_rescan_running = true;
  2132. spin_unlock(&fs_info->qgroup_lock);
  2133. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2134. memset(&fs_info->qgroup_rescan_work, 0,
  2135. sizeof(fs_info->qgroup_rescan_work));
  2136. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2137. btrfs_qgroup_rescan_helper,
  2138. btrfs_qgroup_rescan_worker, NULL, NULL);
  2139. if (ret) {
  2140. err:
  2141. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2142. return ret;
  2143. }
  2144. return 0;
  2145. }
  2146. static void
  2147. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2148. {
  2149. struct rb_node *n;
  2150. struct btrfs_qgroup *qgroup;
  2151. spin_lock(&fs_info->qgroup_lock);
  2152. /* clear all current qgroup tracking information */
  2153. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2154. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2155. qgroup->rfer = 0;
  2156. qgroup->rfer_cmpr = 0;
  2157. qgroup->excl = 0;
  2158. qgroup->excl_cmpr = 0;
  2159. }
  2160. spin_unlock(&fs_info->qgroup_lock);
  2161. }
  2162. int
  2163. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2164. {
  2165. int ret = 0;
  2166. struct btrfs_trans_handle *trans;
  2167. ret = qgroup_rescan_init(fs_info, 0, 1);
  2168. if (ret)
  2169. return ret;
  2170. /*
  2171. * We have set the rescan_progress to 0, which means no more
  2172. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2173. * However, btrfs_qgroup_account_ref may be right after its call
  2174. * to btrfs_find_all_roots, in which case it would still do the
  2175. * accounting.
  2176. * To solve this, we're committing the transaction, which will
  2177. * ensure we run all delayed refs and only after that, we are
  2178. * going to clear all tracking information for a clean start.
  2179. */
  2180. trans = btrfs_join_transaction(fs_info->fs_root);
  2181. if (IS_ERR(trans)) {
  2182. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2183. return PTR_ERR(trans);
  2184. }
  2185. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2186. if (ret) {
  2187. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2188. return ret;
  2189. }
  2190. qgroup_rescan_zero_tracking(fs_info);
  2191. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2192. &fs_info->qgroup_rescan_work);
  2193. return 0;
  2194. }
  2195. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  2196. bool interruptible)
  2197. {
  2198. int running;
  2199. int ret = 0;
  2200. mutex_lock(&fs_info->qgroup_rescan_lock);
  2201. spin_lock(&fs_info->qgroup_lock);
  2202. running = fs_info->qgroup_rescan_running;
  2203. spin_unlock(&fs_info->qgroup_lock);
  2204. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2205. if (!running)
  2206. return 0;
  2207. if (interruptible)
  2208. ret = wait_for_completion_interruptible(
  2209. &fs_info->qgroup_rescan_completion);
  2210. else
  2211. wait_for_completion(&fs_info->qgroup_rescan_completion);
  2212. return ret;
  2213. }
  2214. /*
  2215. * this is only called from open_ctree where we're still single threaded, thus
  2216. * locking is omitted here.
  2217. */
  2218. void
  2219. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2220. {
  2221. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2222. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2223. &fs_info->qgroup_rescan_work);
  2224. }
  2225. /*
  2226. * Reserve qgroup space for range [start, start + len).
  2227. *
  2228. * This function will either reserve space from related qgroups or doing
  2229. * nothing if the range is already reserved.
  2230. *
  2231. * Return 0 for successful reserve
  2232. * Return <0 for error (including -EQUOT)
  2233. *
  2234. * NOTE: this function may sleep for memory allocation.
  2235. */
  2236. int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
  2237. {
  2238. struct btrfs_root *root = BTRFS_I(inode)->root;
  2239. struct extent_changeset changeset;
  2240. struct ulist_node *unode;
  2241. struct ulist_iterator uiter;
  2242. int ret;
  2243. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2244. !is_fstree(root->objectid) || len == 0)
  2245. return 0;
  2246. changeset.bytes_changed = 0;
  2247. changeset.range_changed = ulist_alloc(GFP_NOFS);
  2248. ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2249. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2250. trace_btrfs_qgroup_reserve_data(inode, start, len,
  2251. changeset.bytes_changed,
  2252. QGROUP_RESERVE);
  2253. if (ret < 0)
  2254. goto cleanup;
  2255. ret = qgroup_reserve(root, changeset.bytes_changed);
  2256. if (ret < 0)
  2257. goto cleanup;
  2258. ulist_free(changeset.range_changed);
  2259. return ret;
  2260. cleanup:
  2261. /* cleanup already reserved ranges */
  2262. ULIST_ITER_INIT(&uiter);
  2263. while ((unode = ulist_next(changeset.range_changed, &uiter)))
  2264. clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
  2265. unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
  2266. GFP_NOFS);
  2267. ulist_free(changeset.range_changed);
  2268. return ret;
  2269. }
  2270. static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
  2271. int free)
  2272. {
  2273. struct extent_changeset changeset;
  2274. int trace_op = QGROUP_RELEASE;
  2275. int ret;
  2276. changeset.bytes_changed = 0;
  2277. changeset.range_changed = ulist_alloc(GFP_NOFS);
  2278. if (!changeset.range_changed)
  2279. return -ENOMEM;
  2280. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2281. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2282. if (ret < 0)
  2283. goto out;
  2284. if (free) {
  2285. qgroup_free(BTRFS_I(inode)->root, changeset.bytes_changed);
  2286. trace_op = QGROUP_FREE;
  2287. }
  2288. trace_btrfs_qgroup_release_data(inode, start, len,
  2289. changeset.bytes_changed, trace_op);
  2290. out:
  2291. ulist_free(changeset.range_changed);
  2292. return ret;
  2293. }
  2294. /*
  2295. * Free a reserved space range from io_tree and related qgroups
  2296. *
  2297. * Should be called when a range of pages get invalidated before reaching disk.
  2298. * Or for error cleanup case.
  2299. *
  2300. * For data written to disk, use btrfs_qgroup_release_data().
  2301. *
  2302. * NOTE: This function may sleep for memory allocation.
  2303. */
  2304. int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
  2305. {
  2306. return __btrfs_qgroup_release_data(inode, start, len, 1);
  2307. }
  2308. /*
  2309. * Release a reserved space range from io_tree only.
  2310. *
  2311. * Should be called when a range of pages get written to disk and corresponding
  2312. * FILE_EXTENT is inserted into corresponding root.
  2313. *
  2314. * Since new qgroup accounting framework will only update qgroup numbers at
  2315. * commit_transaction() time, its reserved space shouldn't be freed from
  2316. * related qgroups.
  2317. *
  2318. * But we should release the range from io_tree, to allow further write to be
  2319. * COWed.
  2320. *
  2321. * NOTE: This function may sleep for memory allocation.
  2322. */
  2323. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
  2324. {
  2325. return __btrfs_qgroup_release_data(inode, start, len, 0);
  2326. }
  2327. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes)
  2328. {
  2329. int ret;
  2330. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2331. !is_fstree(root->objectid) || num_bytes == 0)
  2332. return 0;
  2333. BUG_ON(num_bytes != round_down(num_bytes, root->nodesize));
  2334. ret = qgroup_reserve(root, num_bytes);
  2335. if (ret < 0)
  2336. return ret;
  2337. atomic_add(num_bytes, &root->qgroup_meta_rsv);
  2338. return ret;
  2339. }
  2340. void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
  2341. {
  2342. int reserved;
  2343. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2344. !is_fstree(root->objectid))
  2345. return;
  2346. reserved = atomic_xchg(&root->qgroup_meta_rsv, 0);
  2347. if (reserved == 0)
  2348. return;
  2349. qgroup_free(root, reserved);
  2350. }
  2351. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
  2352. {
  2353. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2354. !is_fstree(root->objectid))
  2355. return;
  2356. BUG_ON(num_bytes != round_down(num_bytes, root->nodesize));
  2357. WARN_ON(atomic_read(&root->qgroup_meta_rsv) < num_bytes);
  2358. atomic_sub(num_bytes, &root->qgroup_meta_rsv);
  2359. qgroup_free(root, num_bytes);
  2360. }
  2361. /*
  2362. * Check qgroup reserved space leaking, normally at destroy inode
  2363. * time
  2364. */
  2365. void btrfs_qgroup_check_reserved_leak(struct inode *inode)
  2366. {
  2367. struct extent_changeset changeset;
  2368. struct ulist_node *unode;
  2369. struct ulist_iterator iter;
  2370. int ret;
  2371. changeset.bytes_changed = 0;
  2372. changeset.range_changed = ulist_alloc(GFP_NOFS);
  2373. if (WARN_ON(!changeset.range_changed))
  2374. return;
  2375. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  2376. EXTENT_QGROUP_RESERVED, &changeset);
  2377. WARN_ON(ret < 0);
  2378. if (WARN_ON(changeset.bytes_changed)) {
  2379. ULIST_ITER_INIT(&iter);
  2380. while ((unode = ulist_next(changeset.range_changed, &iter))) {
  2381. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2382. "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
  2383. inode->i_ino, unode->val, unode->aux);
  2384. }
  2385. qgroup_free(BTRFS_I(inode)->root, changeset.bytes_changed);
  2386. }
  2387. ulist_free(changeset.range_changed);
  2388. }