qgroup.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  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. */
  79. u64 old_refcnt;
  80. u64 new_refcnt;
  81. };
  82. /*
  83. * glue structure to represent the relations between qgroups.
  84. */
  85. struct btrfs_qgroup_list {
  86. struct list_head next_group;
  87. struct list_head next_member;
  88. struct btrfs_qgroup *group;
  89. struct btrfs_qgroup *member;
  90. };
  91. #define ptr_to_u64(x) ((u64)(uintptr_t)x)
  92. #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
  93. static int
  94. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  95. int init_flags);
  96. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  97. /* must be called with qgroup_ioctl_lock held */
  98. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  99. u64 qgroupid)
  100. {
  101. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  102. struct btrfs_qgroup *qgroup;
  103. while (n) {
  104. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  105. if (qgroup->qgroupid < qgroupid)
  106. n = n->rb_left;
  107. else if (qgroup->qgroupid > qgroupid)
  108. n = n->rb_right;
  109. else
  110. return qgroup;
  111. }
  112. return NULL;
  113. }
  114. /* must be called with qgroup_lock held */
  115. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  116. u64 qgroupid)
  117. {
  118. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  119. struct rb_node *parent = NULL;
  120. struct btrfs_qgroup *qgroup;
  121. while (*p) {
  122. parent = *p;
  123. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  124. if (qgroup->qgroupid < qgroupid)
  125. p = &(*p)->rb_left;
  126. else if (qgroup->qgroupid > qgroupid)
  127. p = &(*p)->rb_right;
  128. else
  129. return qgroup;
  130. }
  131. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  132. if (!qgroup)
  133. return ERR_PTR(-ENOMEM);
  134. qgroup->qgroupid = qgroupid;
  135. INIT_LIST_HEAD(&qgroup->groups);
  136. INIT_LIST_HEAD(&qgroup->members);
  137. INIT_LIST_HEAD(&qgroup->dirty);
  138. rb_link_node(&qgroup->node, parent, p);
  139. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  140. return qgroup;
  141. }
  142. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  143. {
  144. struct btrfs_qgroup_list *list;
  145. list_del(&qgroup->dirty);
  146. while (!list_empty(&qgroup->groups)) {
  147. list = list_first_entry(&qgroup->groups,
  148. struct btrfs_qgroup_list, next_group);
  149. list_del(&list->next_group);
  150. list_del(&list->next_member);
  151. kfree(list);
  152. }
  153. while (!list_empty(&qgroup->members)) {
  154. list = list_first_entry(&qgroup->members,
  155. struct btrfs_qgroup_list, next_member);
  156. list_del(&list->next_group);
  157. list_del(&list->next_member);
  158. kfree(list);
  159. }
  160. kfree(qgroup);
  161. }
  162. /* must be called with qgroup_lock held */
  163. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  164. {
  165. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  166. if (!qgroup)
  167. return -ENOENT;
  168. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  169. __del_qgroup_rb(qgroup);
  170. return 0;
  171. }
  172. /* must be called with qgroup_lock held */
  173. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  174. u64 memberid, u64 parentid)
  175. {
  176. struct btrfs_qgroup *member;
  177. struct btrfs_qgroup *parent;
  178. struct btrfs_qgroup_list *list;
  179. member = find_qgroup_rb(fs_info, memberid);
  180. parent = find_qgroup_rb(fs_info, parentid);
  181. if (!member || !parent)
  182. return -ENOENT;
  183. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  184. if (!list)
  185. return -ENOMEM;
  186. list->group = parent;
  187. list->member = member;
  188. list_add_tail(&list->next_group, &member->groups);
  189. list_add_tail(&list->next_member, &parent->members);
  190. return 0;
  191. }
  192. /* must be called with qgroup_lock held */
  193. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  194. u64 memberid, u64 parentid)
  195. {
  196. struct btrfs_qgroup *member;
  197. struct btrfs_qgroup *parent;
  198. struct btrfs_qgroup_list *list;
  199. member = find_qgroup_rb(fs_info, memberid);
  200. parent = find_qgroup_rb(fs_info, parentid);
  201. if (!member || !parent)
  202. return -ENOENT;
  203. list_for_each_entry(list, &member->groups, next_group) {
  204. if (list->group == parent) {
  205. list_del(&list->next_group);
  206. list_del(&list->next_member);
  207. kfree(list);
  208. return 0;
  209. }
  210. }
  211. return -ENOENT;
  212. }
  213. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  214. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  215. u64 rfer, u64 excl)
  216. {
  217. struct btrfs_qgroup *qgroup;
  218. qgroup = find_qgroup_rb(fs_info, qgroupid);
  219. if (!qgroup)
  220. return -EINVAL;
  221. if (qgroup->rfer != rfer || qgroup->excl != excl)
  222. return -EINVAL;
  223. return 0;
  224. }
  225. #endif
  226. /*
  227. * The full config is read in one go, only called from open_ctree()
  228. * It doesn't use any locking, as at this point we're still single-threaded
  229. */
  230. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  231. {
  232. struct btrfs_key key;
  233. struct btrfs_key found_key;
  234. struct btrfs_root *quota_root = fs_info->quota_root;
  235. struct btrfs_path *path = NULL;
  236. struct extent_buffer *l;
  237. int slot;
  238. int ret = 0;
  239. u64 flags = 0;
  240. u64 rescan_progress = 0;
  241. if (!fs_info->quota_enabled)
  242. return 0;
  243. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  244. if (!fs_info->qgroup_ulist) {
  245. ret = -ENOMEM;
  246. goto out;
  247. }
  248. path = btrfs_alloc_path();
  249. if (!path) {
  250. ret = -ENOMEM;
  251. goto out;
  252. }
  253. /* default this to quota off, in case no status key is found */
  254. fs_info->qgroup_flags = 0;
  255. /*
  256. * pass 1: read status, all qgroup infos and limits
  257. */
  258. key.objectid = 0;
  259. key.type = 0;
  260. key.offset = 0;
  261. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  262. if (ret)
  263. goto out;
  264. while (1) {
  265. struct btrfs_qgroup *qgroup;
  266. slot = path->slots[0];
  267. l = path->nodes[0];
  268. btrfs_item_key_to_cpu(l, &found_key, slot);
  269. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  270. struct btrfs_qgroup_status_item *ptr;
  271. ptr = btrfs_item_ptr(l, slot,
  272. struct btrfs_qgroup_status_item);
  273. if (btrfs_qgroup_status_version(l, ptr) !=
  274. BTRFS_QGROUP_STATUS_VERSION) {
  275. btrfs_err(fs_info,
  276. "old qgroup version, quota disabled");
  277. goto out;
  278. }
  279. if (btrfs_qgroup_status_generation(l, ptr) !=
  280. fs_info->generation) {
  281. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  282. btrfs_err(fs_info,
  283. "qgroup generation mismatch, "
  284. "marked as inconsistent");
  285. }
  286. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  287. ptr);
  288. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  289. goto next1;
  290. }
  291. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  292. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  293. goto next1;
  294. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  295. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  296. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  297. btrfs_err(fs_info, "inconsitent qgroup config");
  298. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  299. }
  300. if (!qgroup) {
  301. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  302. if (IS_ERR(qgroup)) {
  303. ret = PTR_ERR(qgroup);
  304. goto out;
  305. }
  306. }
  307. switch (found_key.type) {
  308. case BTRFS_QGROUP_INFO_KEY: {
  309. struct btrfs_qgroup_info_item *ptr;
  310. ptr = btrfs_item_ptr(l, slot,
  311. struct btrfs_qgroup_info_item);
  312. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  313. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  314. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  315. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  316. /* generation currently unused */
  317. break;
  318. }
  319. case BTRFS_QGROUP_LIMIT_KEY: {
  320. struct btrfs_qgroup_limit_item *ptr;
  321. ptr = btrfs_item_ptr(l, slot,
  322. struct btrfs_qgroup_limit_item);
  323. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  324. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  325. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  326. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  327. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  328. break;
  329. }
  330. }
  331. next1:
  332. ret = btrfs_next_item(quota_root, path);
  333. if (ret < 0)
  334. goto out;
  335. if (ret)
  336. break;
  337. }
  338. btrfs_release_path(path);
  339. /*
  340. * pass 2: read all qgroup relations
  341. */
  342. key.objectid = 0;
  343. key.type = BTRFS_QGROUP_RELATION_KEY;
  344. key.offset = 0;
  345. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  346. if (ret)
  347. goto out;
  348. while (1) {
  349. slot = path->slots[0];
  350. l = path->nodes[0];
  351. btrfs_item_key_to_cpu(l, &found_key, slot);
  352. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  353. goto next2;
  354. if (found_key.objectid > found_key.offset) {
  355. /* parent <- member, not needed to build config */
  356. /* FIXME should we omit the key completely? */
  357. goto next2;
  358. }
  359. ret = add_relation_rb(fs_info, found_key.objectid,
  360. found_key.offset);
  361. if (ret == -ENOENT) {
  362. btrfs_warn(fs_info,
  363. "orphan qgroup relation 0x%llx->0x%llx",
  364. found_key.objectid, found_key.offset);
  365. ret = 0; /* ignore the error */
  366. }
  367. if (ret)
  368. goto out;
  369. next2:
  370. ret = btrfs_next_item(quota_root, path);
  371. if (ret < 0)
  372. goto out;
  373. if (ret)
  374. break;
  375. }
  376. out:
  377. fs_info->qgroup_flags |= flags;
  378. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  379. fs_info->quota_enabled = 0;
  380. fs_info->pending_quota_state = 0;
  381. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  382. ret >= 0) {
  383. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  384. }
  385. btrfs_free_path(path);
  386. if (ret < 0) {
  387. ulist_free(fs_info->qgroup_ulist);
  388. fs_info->qgroup_ulist = NULL;
  389. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  390. }
  391. return ret < 0 ? ret : 0;
  392. }
  393. /*
  394. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  395. * first two are in single-threaded paths.And for the third one, we have set
  396. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  397. * up the in-memory structures without qgroup_lock held.
  398. */
  399. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  400. {
  401. struct rb_node *n;
  402. struct btrfs_qgroup *qgroup;
  403. while ((n = rb_first(&fs_info->qgroup_tree))) {
  404. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  405. rb_erase(n, &fs_info->qgroup_tree);
  406. __del_qgroup_rb(qgroup);
  407. }
  408. /*
  409. * we call btrfs_free_qgroup_config() when umounting
  410. * filesystem and disabling quota, so we set qgroup_ulit
  411. * to be null here to avoid double free.
  412. */
  413. ulist_free(fs_info->qgroup_ulist);
  414. fs_info->qgroup_ulist = NULL;
  415. }
  416. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  417. struct btrfs_root *quota_root,
  418. u64 src, u64 dst)
  419. {
  420. int ret;
  421. struct btrfs_path *path;
  422. struct btrfs_key key;
  423. path = btrfs_alloc_path();
  424. if (!path)
  425. return -ENOMEM;
  426. key.objectid = src;
  427. key.type = BTRFS_QGROUP_RELATION_KEY;
  428. key.offset = dst;
  429. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  430. btrfs_mark_buffer_dirty(path->nodes[0]);
  431. btrfs_free_path(path);
  432. return ret;
  433. }
  434. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  435. struct btrfs_root *quota_root,
  436. u64 src, u64 dst)
  437. {
  438. int ret;
  439. struct btrfs_path *path;
  440. struct btrfs_key key;
  441. path = btrfs_alloc_path();
  442. if (!path)
  443. return -ENOMEM;
  444. key.objectid = src;
  445. key.type = BTRFS_QGROUP_RELATION_KEY;
  446. key.offset = dst;
  447. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  448. if (ret < 0)
  449. goto out;
  450. if (ret > 0) {
  451. ret = -ENOENT;
  452. goto out;
  453. }
  454. ret = btrfs_del_item(trans, quota_root, path);
  455. out:
  456. btrfs_free_path(path);
  457. return ret;
  458. }
  459. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  460. struct btrfs_root *quota_root, u64 qgroupid)
  461. {
  462. int ret;
  463. struct btrfs_path *path;
  464. struct btrfs_qgroup_info_item *qgroup_info;
  465. struct btrfs_qgroup_limit_item *qgroup_limit;
  466. struct extent_buffer *leaf;
  467. struct btrfs_key key;
  468. if (btrfs_test_is_dummy_root(quota_root))
  469. return 0;
  470. path = btrfs_alloc_path();
  471. if (!path)
  472. return -ENOMEM;
  473. key.objectid = 0;
  474. key.type = BTRFS_QGROUP_INFO_KEY;
  475. key.offset = qgroupid;
  476. /*
  477. * Avoid a transaction abort by catching -EEXIST here. In that
  478. * case, we proceed by re-initializing the existing structure
  479. * on disk.
  480. */
  481. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  482. sizeof(*qgroup_info));
  483. if (ret && ret != -EEXIST)
  484. goto out;
  485. leaf = path->nodes[0];
  486. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  487. struct btrfs_qgroup_info_item);
  488. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  489. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  490. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  491. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  492. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  493. btrfs_mark_buffer_dirty(leaf);
  494. btrfs_release_path(path);
  495. key.type = BTRFS_QGROUP_LIMIT_KEY;
  496. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  497. sizeof(*qgroup_limit));
  498. if (ret && ret != -EEXIST)
  499. goto out;
  500. leaf = path->nodes[0];
  501. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  502. struct btrfs_qgroup_limit_item);
  503. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  504. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  505. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  506. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  507. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  508. btrfs_mark_buffer_dirty(leaf);
  509. ret = 0;
  510. out:
  511. btrfs_free_path(path);
  512. return ret;
  513. }
  514. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  515. struct btrfs_root *quota_root, u64 qgroupid)
  516. {
  517. int ret;
  518. struct btrfs_path *path;
  519. struct btrfs_key key;
  520. path = btrfs_alloc_path();
  521. if (!path)
  522. return -ENOMEM;
  523. key.objectid = 0;
  524. key.type = BTRFS_QGROUP_INFO_KEY;
  525. key.offset = qgroupid;
  526. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  527. if (ret < 0)
  528. goto out;
  529. if (ret > 0) {
  530. ret = -ENOENT;
  531. goto out;
  532. }
  533. ret = btrfs_del_item(trans, quota_root, path);
  534. if (ret)
  535. goto out;
  536. btrfs_release_path(path);
  537. key.type = BTRFS_QGROUP_LIMIT_KEY;
  538. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  539. if (ret < 0)
  540. goto out;
  541. if (ret > 0) {
  542. ret = -ENOENT;
  543. goto out;
  544. }
  545. ret = btrfs_del_item(trans, quota_root, path);
  546. out:
  547. btrfs_free_path(path);
  548. return ret;
  549. }
  550. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  551. struct btrfs_root *root, u64 qgroupid,
  552. u64 flags, u64 max_rfer, u64 max_excl,
  553. u64 rsv_rfer, u64 rsv_excl)
  554. {
  555. struct btrfs_path *path;
  556. struct btrfs_key key;
  557. struct extent_buffer *l;
  558. struct btrfs_qgroup_limit_item *qgroup_limit;
  559. int ret;
  560. int slot;
  561. key.objectid = 0;
  562. key.type = BTRFS_QGROUP_LIMIT_KEY;
  563. key.offset = qgroupid;
  564. path = btrfs_alloc_path();
  565. if (!path)
  566. return -ENOMEM;
  567. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  568. if (ret > 0)
  569. ret = -ENOENT;
  570. if (ret)
  571. goto out;
  572. l = path->nodes[0];
  573. slot = path->slots[0];
  574. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  575. btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
  576. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
  577. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
  578. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
  579. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
  580. btrfs_mark_buffer_dirty(l);
  581. out:
  582. btrfs_free_path(path);
  583. return ret;
  584. }
  585. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  586. struct btrfs_root *root,
  587. struct btrfs_qgroup *qgroup)
  588. {
  589. struct btrfs_path *path;
  590. struct btrfs_key key;
  591. struct extent_buffer *l;
  592. struct btrfs_qgroup_info_item *qgroup_info;
  593. int ret;
  594. int slot;
  595. if (btrfs_test_is_dummy_root(root))
  596. return 0;
  597. key.objectid = 0;
  598. key.type = BTRFS_QGROUP_INFO_KEY;
  599. key.offset = qgroup->qgroupid;
  600. path = btrfs_alloc_path();
  601. if (!path)
  602. return -ENOMEM;
  603. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  604. if (ret > 0)
  605. ret = -ENOENT;
  606. if (ret)
  607. goto out;
  608. l = path->nodes[0];
  609. slot = path->slots[0];
  610. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  611. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  612. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  613. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  614. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  615. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  616. btrfs_mark_buffer_dirty(l);
  617. out:
  618. btrfs_free_path(path);
  619. return ret;
  620. }
  621. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  622. struct btrfs_fs_info *fs_info,
  623. struct btrfs_root *root)
  624. {
  625. struct btrfs_path *path;
  626. struct btrfs_key key;
  627. struct extent_buffer *l;
  628. struct btrfs_qgroup_status_item *ptr;
  629. int ret;
  630. int slot;
  631. key.objectid = 0;
  632. key.type = BTRFS_QGROUP_STATUS_KEY;
  633. key.offset = 0;
  634. path = btrfs_alloc_path();
  635. if (!path)
  636. return -ENOMEM;
  637. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  638. if (ret > 0)
  639. ret = -ENOENT;
  640. if (ret)
  641. goto out;
  642. l = path->nodes[0];
  643. slot = path->slots[0];
  644. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  645. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  646. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  647. btrfs_set_qgroup_status_rescan(l, ptr,
  648. fs_info->qgroup_rescan_progress.objectid);
  649. btrfs_mark_buffer_dirty(l);
  650. out:
  651. btrfs_free_path(path);
  652. return ret;
  653. }
  654. /*
  655. * called with qgroup_lock held
  656. */
  657. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  658. struct btrfs_root *root)
  659. {
  660. struct btrfs_path *path;
  661. struct btrfs_key key;
  662. struct extent_buffer *leaf = NULL;
  663. int ret;
  664. int nr = 0;
  665. path = btrfs_alloc_path();
  666. if (!path)
  667. return -ENOMEM;
  668. path->leave_spinning = 1;
  669. key.objectid = 0;
  670. key.offset = 0;
  671. key.type = 0;
  672. while (1) {
  673. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  674. if (ret < 0)
  675. goto out;
  676. leaf = path->nodes[0];
  677. nr = btrfs_header_nritems(leaf);
  678. if (!nr)
  679. break;
  680. /*
  681. * delete the leaf one by one
  682. * since the whole tree is going
  683. * to be deleted.
  684. */
  685. path->slots[0] = 0;
  686. ret = btrfs_del_items(trans, root, path, 0, nr);
  687. if (ret)
  688. goto out;
  689. btrfs_release_path(path);
  690. }
  691. ret = 0;
  692. out:
  693. root->fs_info->pending_quota_state = 0;
  694. btrfs_free_path(path);
  695. return ret;
  696. }
  697. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  698. struct btrfs_fs_info *fs_info)
  699. {
  700. struct btrfs_root *quota_root;
  701. struct btrfs_root *tree_root = fs_info->tree_root;
  702. struct btrfs_path *path = NULL;
  703. struct btrfs_qgroup_status_item *ptr;
  704. struct extent_buffer *leaf;
  705. struct btrfs_key key;
  706. struct btrfs_key found_key;
  707. struct btrfs_qgroup *qgroup = NULL;
  708. int ret = 0;
  709. int slot;
  710. mutex_lock(&fs_info->qgroup_ioctl_lock);
  711. if (fs_info->quota_root) {
  712. fs_info->pending_quota_state = 1;
  713. goto out;
  714. }
  715. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  716. if (!fs_info->qgroup_ulist) {
  717. ret = -ENOMEM;
  718. goto out;
  719. }
  720. /*
  721. * initially create the quota tree
  722. */
  723. quota_root = btrfs_create_tree(trans, fs_info,
  724. BTRFS_QUOTA_TREE_OBJECTID);
  725. if (IS_ERR(quota_root)) {
  726. ret = PTR_ERR(quota_root);
  727. goto out;
  728. }
  729. path = btrfs_alloc_path();
  730. if (!path) {
  731. ret = -ENOMEM;
  732. goto out_free_root;
  733. }
  734. key.objectid = 0;
  735. key.type = BTRFS_QGROUP_STATUS_KEY;
  736. key.offset = 0;
  737. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  738. sizeof(*ptr));
  739. if (ret)
  740. goto out_free_path;
  741. leaf = path->nodes[0];
  742. ptr = btrfs_item_ptr(leaf, path->slots[0],
  743. struct btrfs_qgroup_status_item);
  744. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  745. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  746. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  747. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  748. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  749. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  750. btrfs_mark_buffer_dirty(leaf);
  751. key.objectid = 0;
  752. key.type = BTRFS_ROOT_REF_KEY;
  753. key.offset = 0;
  754. btrfs_release_path(path);
  755. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  756. if (ret > 0)
  757. goto out_add_root;
  758. if (ret < 0)
  759. goto out_free_path;
  760. while (1) {
  761. slot = path->slots[0];
  762. leaf = path->nodes[0];
  763. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  764. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  765. ret = add_qgroup_item(trans, quota_root,
  766. found_key.offset);
  767. if (ret)
  768. goto out_free_path;
  769. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  770. if (IS_ERR(qgroup)) {
  771. ret = PTR_ERR(qgroup);
  772. goto out_free_path;
  773. }
  774. }
  775. ret = btrfs_next_item(tree_root, path);
  776. if (ret < 0)
  777. goto out_free_path;
  778. if (ret)
  779. break;
  780. }
  781. out_add_root:
  782. btrfs_release_path(path);
  783. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  784. if (ret)
  785. goto out_free_path;
  786. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  787. if (IS_ERR(qgroup)) {
  788. ret = PTR_ERR(qgroup);
  789. goto out_free_path;
  790. }
  791. spin_lock(&fs_info->qgroup_lock);
  792. fs_info->quota_root = quota_root;
  793. fs_info->pending_quota_state = 1;
  794. spin_unlock(&fs_info->qgroup_lock);
  795. out_free_path:
  796. btrfs_free_path(path);
  797. out_free_root:
  798. if (ret) {
  799. free_extent_buffer(quota_root->node);
  800. free_extent_buffer(quota_root->commit_root);
  801. kfree(quota_root);
  802. }
  803. out:
  804. if (ret) {
  805. ulist_free(fs_info->qgroup_ulist);
  806. fs_info->qgroup_ulist = NULL;
  807. }
  808. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  809. return ret;
  810. }
  811. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  812. struct btrfs_fs_info *fs_info)
  813. {
  814. struct btrfs_root *tree_root = fs_info->tree_root;
  815. struct btrfs_root *quota_root;
  816. int ret = 0;
  817. mutex_lock(&fs_info->qgroup_ioctl_lock);
  818. if (!fs_info->quota_root)
  819. goto out;
  820. spin_lock(&fs_info->qgroup_lock);
  821. fs_info->quota_enabled = 0;
  822. fs_info->pending_quota_state = 0;
  823. quota_root = fs_info->quota_root;
  824. fs_info->quota_root = NULL;
  825. spin_unlock(&fs_info->qgroup_lock);
  826. btrfs_free_qgroup_config(fs_info);
  827. ret = btrfs_clean_quota_tree(trans, quota_root);
  828. if (ret)
  829. goto out;
  830. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  831. if (ret)
  832. goto out;
  833. list_del(&quota_root->dirty_list);
  834. btrfs_tree_lock(quota_root->node);
  835. clean_tree_block(trans, tree_root, quota_root->node);
  836. btrfs_tree_unlock(quota_root->node);
  837. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  838. free_extent_buffer(quota_root->node);
  839. free_extent_buffer(quota_root->commit_root);
  840. kfree(quota_root);
  841. out:
  842. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  843. return ret;
  844. }
  845. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  846. struct btrfs_qgroup *qgroup)
  847. {
  848. if (list_empty(&qgroup->dirty))
  849. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  850. }
  851. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  852. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  853. {
  854. struct btrfs_root *quota_root;
  855. struct btrfs_qgroup *parent;
  856. struct btrfs_qgroup *member;
  857. struct btrfs_qgroup_list *list;
  858. int ret = 0;
  859. mutex_lock(&fs_info->qgroup_ioctl_lock);
  860. quota_root = fs_info->quota_root;
  861. if (!quota_root) {
  862. ret = -EINVAL;
  863. goto out;
  864. }
  865. member = find_qgroup_rb(fs_info, src);
  866. parent = find_qgroup_rb(fs_info, dst);
  867. if (!member || !parent) {
  868. ret = -EINVAL;
  869. goto out;
  870. }
  871. /* check if such qgroup relation exist firstly */
  872. list_for_each_entry(list, &member->groups, next_group) {
  873. if (list->group == parent) {
  874. ret = -EEXIST;
  875. goto out;
  876. }
  877. }
  878. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  879. if (ret)
  880. goto out;
  881. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  882. if (ret) {
  883. del_qgroup_relation_item(trans, quota_root, src, dst);
  884. goto out;
  885. }
  886. spin_lock(&fs_info->qgroup_lock);
  887. ret = add_relation_rb(quota_root->fs_info, src, dst);
  888. spin_unlock(&fs_info->qgroup_lock);
  889. out:
  890. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  891. return ret;
  892. }
  893. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  894. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  895. {
  896. struct btrfs_root *quota_root;
  897. struct btrfs_qgroup *parent;
  898. struct btrfs_qgroup *member;
  899. struct btrfs_qgroup_list *list;
  900. int ret = 0;
  901. int err;
  902. mutex_lock(&fs_info->qgroup_ioctl_lock);
  903. quota_root = fs_info->quota_root;
  904. if (!quota_root) {
  905. ret = -EINVAL;
  906. goto out;
  907. }
  908. member = find_qgroup_rb(fs_info, src);
  909. parent = find_qgroup_rb(fs_info, dst);
  910. if (!member || !parent) {
  911. ret = -EINVAL;
  912. goto out;
  913. }
  914. /* check if such qgroup relation exist firstly */
  915. list_for_each_entry(list, &member->groups, next_group) {
  916. if (list->group == parent)
  917. goto exist;
  918. }
  919. ret = -ENOENT;
  920. goto out;
  921. exist:
  922. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  923. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  924. if (err && !ret)
  925. ret = err;
  926. spin_lock(&fs_info->qgroup_lock);
  927. del_relation_rb(fs_info, src, dst);
  928. spin_unlock(&fs_info->qgroup_lock);
  929. out:
  930. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  931. return ret;
  932. }
  933. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  934. struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
  935. {
  936. struct btrfs_root *quota_root;
  937. struct btrfs_qgroup *qgroup;
  938. int ret = 0;
  939. mutex_lock(&fs_info->qgroup_ioctl_lock);
  940. quota_root = fs_info->quota_root;
  941. if (!quota_root) {
  942. ret = -EINVAL;
  943. goto out;
  944. }
  945. qgroup = find_qgroup_rb(fs_info, qgroupid);
  946. if (qgroup) {
  947. ret = -EEXIST;
  948. goto out;
  949. }
  950. ret = add_qgroup_item(trans, quota_root, qgroupid);
  951. if (ret)
  952. goto out;
  953. spin_lock(&fs_info->qgroup_lock);
  954. qgroup = add_qgroup_rb(fs_info, qgroupid);
  955. spin_unlock(&fs_info->qgroup_lock);
  956. if (IS_ERR(qgroup))
  957. ret = PTR_ERR(qgroup);
  958. out:
  959. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  960. return ret;
  961. }
  962. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  963. struct btrfs_fs_info *fs_info, u64 qgroupid)
  964. {
  965. struct btrfs_root *quota_root;
  966. struct btrfs_qgroup *qgroup;
  967. int ret = 0;
  968. mutex_lock(&fs_info->qgroup_ioctl_lock);
  969. quota_root = fs_info->quota_root;
  970. if (!quota_root) {
  971. ret = -EINVAL;
  972. goto out;
  973. }
  974. qgroup = find_qgroup_rb(fs_info, qgroupid);
  975. if (!qgroup) {
  976. ret = -ENOENT;
  977. goto out;
  978. } else {
  979. /* check if there are no relations to this qgroup */
  980. if (!list_empty(&qgroup->groups) ||
  981. !list_empty(&qgroup->members)) {
  982. ret = -EBUSY;
  983. goto out;
  984. }
  985. }
  986. ret = del_qgroup_item(trans, quota_root, qgroupid);
  987. spin_lock(&fs_info->qgroup_lock);
  988. del_qgroup_rb(quota_root->fs_info, qgroupid);
  989. spin_unlock(&fs_info->qgroup_lock);
  990. out:
  991. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  992. return ret;
  993. }
  994. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  995. struct btrfs_fs_info *fs_info, u64 qgroupid,
  996. struct btrfs_qgroup_limit *limit)
  997. {
  998. struct btrfs_root *quota_root;
  999. struct btrfs_qgroup *qgroup;
  1000. int ret = 0;
  1001. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1002. quota_root = fs_info->quota_root;
  1003. if (!quota_root) {
  1004. ret = -EINVAL;
  1005. goto out;
  1006. }
  1007. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1008. if (!qgroup) {
  1009. ret = -ENOENT;
  1010. goto out;
  1011. }
  1012. ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
  1013. limit->flags, limit->max_rfer,
  1014. limit->max_excl, limit->rsv_rfer,
  1015. limit->rsv_excl);
  1016. if (ret) {
  1017. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1018. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1019. qgroupid);
  1020. }
  1021. spin_lock(&fs_info->qgroup_lock);
  1022. qgroup->lim_flags = limit->flags;
  1023. qgroup->max_rfer = limit->max_rfer;
  1024. qgroup->max_excl = limit->max_excl;
  1025. qgroup->rsv_rfer = limit->rsv_rfer;
  1026. qgroup->rsv_excl = limit->rsv_excl;
  1027. spin_unlock(&fs_info->qgroup_lock);
  1028. out:
  1029. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1030. return ret;
  1031. }
  1032. static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
  1033. struct btrfs_qgroup_operation *oper2)
  1034. {
  1035. /*
  1036. * Ignore seq and type here, we're looking for any operation
  1037. * at all related to this extent on that root.
  1038. */
  1039. if (oper1->bytenr < oper2->bytenr)
  1040. return -1;
  1041. if (oper1->bytenr > oper2->bytenr)
  1042. return 1;
  1043. if (oper1->ref_root < oper2->ref_root)
  1044. return -1;
  1045. if (oper1->ref_root > oper2->ref_root)
  1046. return 1;
  1047. return 0;
  1048. }
  1049. static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
  1050. struct btrfs_qgroup_operation *oper)
  1051. {
  1052. struct rb_node *n;
  1053. struct btrfs_qgroup_operation *cur;
  1054. int cmp;
  1055. spin_lock(&fs_info->qgroup_op_lock);
  1056. n = fs_info->qgroup_op_tree.rb_node;
  1057. while (n) {
  1058. cur = rb_entry(n, struct btrfs_qgroup_operation, n);
  1059. cmp = comp_oper_exist(cur, oper);
  1060. if (cmp < 0) {
  1061. n = n->rb_right;
  1062. } else if (cmp) {
  1063. n = n->rb_left;
  1064. } else {
  1065. spin_unlock(&fs_info->qgroup_op_lock);
  1066. return -EEXIST;
  1067. }
  1068. }
  1069. spin_unlock(&fs_info->qgroup_op_lock);
  1070. return 0;
  1071. }
  1072. static int comp_oper(struct btrfs_qgroup_operation *oper1,
  1073. struct btrfs_qgroup_operation *oper2)
  1074. {
  1075. if (oper1->bytenr < oper2->bytenr)
  1076. return -1;
  1077. if (oper1->bytenr > oper2->bytenr)
  1078. return 1;
  1079. if (oper1->seq < oper2->seq)
  1080. return -1;
  1081. if (oper1->seq > oper2->seq)
  1082. return -1;
  1083. if (oper1->ref_root < oper2->ref_root)
  1084. return -1;
  1085. if (oper1->ref_root > oper2->ref_root)
  1086. return 1;
  1087. if (oper1->type < oper2->type)
  1088. return -1;
  1089. if (oper1->type > oper2->type)
  1090. return 1;
  1091. return 0;
  1092. }
  1093. static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
  1094. struct btrfs_qgroup_operation *oper)
  1095. {
  1096. struct rb_node **p;
  1097. struct rb_node *parent = NULL;
  1098. struct btrfs_qgroup_operation *cur;
  1099. int cmp;
  1100. spin_lock(&fs_info->qgroup_op_lock);
  1101. p = &fs_info->qgroup_op_tree.rb_node;
  1102. while (*p) {
  1103. parent = *p;
  1104. cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
  1105. cmp = comp_oper(cur, oper);
  1106. if (cmp < 0) {
  1107. p = &(*p)->rb_right;
  1108. } else if (cmp) {
  1109. p = &(*p)->rb_left;
  1110. } else {
  1111. spin_unlock(&fs_info->qgroup_op_lock);
  1112. return -EEXIST;
  1113. }
  1114. }
  1115. rb_link_node(&oper->n, parent, p);
  1116. rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
  1117. spin_unlock(&fs_info->qgroup_op_lock);
  1118. return 0;
  1119. }
  1120. /*
  1121. * Record a quota operation for processing later on.
  1122. * @trans: the transaction we are adding the delayed op to.
  1123. * @fs_info: the fs_info for this fs.
  1124. * @ref_root: the root of the reference we are acting on,
  1125. * @bytenr: the bytenr we are acting on.
  1126. * @num_bytes: the number of bytes in the reference.
  1127. * @type: the type of operation this is.
  1128. * @mod_seq: do we need to get a sequence number for looking up roots.
  1129. *
  1130. * We just add it to our trans qgroup_ref_list and carry on and process these
  1131. * operations in order at some later point. If the reference root isn't a fs
  1132. * root then we don't bother with doing anything.
  1133. *
  1134. * MUST BE HOLDING THE REF LOCK.
  1135. */
  1136. int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
  1137. struct btrfs_fs_info *fs_info, u64 ref_root,
  1138. u64 bytenr, u64 num_bytes,
  1139. enum btrfs_qgroup_operation_type type, int mod_seq)
  1140. {
  1141. struct btrfs_qgroup_operation *oper;
  1142. int ret;
  1143. if (!is_fstree(ref_root) || !fs_info->quota_enabled)
  1144. return 0;
  1145. oper = kmalloc(sizeof(*oper), GFP_NOFS);
  1146. if (!oper)
  1147. return -ENOMEM;
  1148. oper->ref_root = ref_root;
  1149. oper->bytenr = bytenr;
  1150. oper->num_bytes = num_bytes;
  1151. oper->type = type;
  1152. oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
  1153. INIT_LIST_HEAD(&oper->elem.list);
  1154. oper->elem.seq = 0;
  1155. trace_btrfs_qgroup_record_ref(oper);
  1156. if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
  1157. /*
  1158. * If any operation for this bytenr/ref_root combo
  1159. * exists, then we know it's not exclusively owned and
  1160. * shouldn't be queued up.
  1161. *
  1162. * This also catches the case where we have a cloned
  1163. * extent that gets queued up multiple times during
  1164. * drop snapshot.
  1165. */
  1166. if (qgroup_oper_exists(fs_info, oper)) {
  1167. kfree(oper);
  1168. return 0;
  1169. }
  1170. }
  1171. ret = insert_qgroup_oper(fs_info, oper);
  1172. if (ret) {
  1173. /* Shouldn't happen so have an assert for developers */
  1174. ASSERT(0);
  1175. kfree(oper);
  1176. return ret;
  1177. }
  1178. list_add_tail(&oper->list, &trans->qgroup_ref_list);
  1179. if (mod_seq)
  1180. btrfs_get_tree_mod_seq(fs_info, &oper->elem);
  1181. return 0;
  1182. }
  1183. /*
  1184. * The easy accounting, if we are adding/removing the only ref for an extent
  1185. * then this qgroup and all of the parent qgroups get their refrence and
  1186. * exclusive counts adjusted.
  1187. */
  1188. static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  1189. struct btrfs_qgroup_operation *oper)
  1190. {
  1191. struct btrfs_qgroup *qgroup;
  1192. struct ulist *tmp;
  1193. struct btrfs_qgroup_list *glist;
  1194. struct ulist_node *unode;
  1195. struct ulist_iterator uiter;
  1196. int sign = 0;
  1197. int ret = 0;
  1198. tmp = ulist_alloc(GFP_NOFS);
  1199. if (!tmp)
  1200. return -ENOMEM;
  1201. spin_lock(&fs_info->qgroup_lock);
  1202. if (!fs_info->quota_root)
  1203. goto out;
  1204. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1205. if (!qgroup)
  1206. goto out;
  1207. switch (oper->type) {
  1208. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1209. sign = 1;
  1210. break;
  1211. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1212. sign = -1;
  1213. break;
  1214. default:
  1215. ASSERT(0);
  1216. }
  1217. qgroup->rfer += sign * oper->num_bytes;
  1218. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1219. WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
  1220. qgroup->excl += sign * oper->num_bytes;
  1221. qgroup->excl_cmpr += sign * oper->num_bytes;
  1222. qgroup_dirty(fs_info, qgroup);
  1223. /* Get all of the parent groups that contain this qgroup */
  1224. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1225. ret = ulist_add(tmp, glist->group->qgroupid,
  1226. ptr_to_u64(glist->group), GFP_ATOMIC);
  1227. if (ret < 0)
  1228. goto out;
  1229. }
  1230. /* Iterate all of the parents and adjust their reference counts */
  1231. ULIST_ITER_INIT(&uiter);
  1232. while ((unode = ulist_next(tmp, &uiter))) {
  1233. qgroup = u64_to_ptr(unode->aux);
  1234. qgroup->rfer += sign * oper->num_bytes;
  1235. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1236. qgroup->excl += sign * oper->num_bytes;
  1237. if (sign < 0)
  1238. WARN_ON(qgroup->excl < oper->num_bytes);
  1239. qgroup->excl_cmpr += sign * oper->num_bytes;
  1240. qgroup_dirty(fs_info, qgroup);
  1241. /* Add any parents of the parents */
  1242. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1243. ret = ulist_add(tmp, glist->group->qgroupid,
  1244. ptr_to_u64(glist->group), GFP_ATOMIC);
  1245. if (ret < 0)
  1246. goto out;
  1247. }
  1248. }
  1249. ret = 0;
  1250. out:
  1251. spin_unlock(&fs_info->qgroup_lock);
  1252. ulist_free(tmp);
  1253. return ret;
  1254. }
  1255. /*
  1256. * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
  1257. * properly.
  1258. */
  1259. static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
  1260. u64 root_to_skip, struct ulist *tmp,
  1261. struct ulist *roots, struct ulist *qgroups,
  1262. u64 seq, int *old_roots, int rescan)
  1263. {
  1264. struct ulist_node *unode;
  1265. struct ulist_iterator uiter;
  1266. struct ulist_node *tmp_unode;
  1267. struct ulist_iterator tmp_uiter;
  1268. struct btrfs_qgroup *qg;
  1269. int ret;
  1270. ULIST_ITER_INIT(&uiter);
  1271. while ((unode = ulist_next(roots, &uiter))) {
  1272. /* We don't count our current root here */
  1273. if (unode->val == root_to_skip)
  1274. continue;
  1275. qg = find_qgroup_rb(fs_info, unode->val);
  1276. if (!qg)
  1277. continue;
  1278. /*
  1279. * We could have a pending removal of this same ref so we may
  1280. * not have actually found our ref root when doing
  1281. * btrfs_find_all_roots, so we need to keep track of how many
  1282. * old roots we find in case we removed ours and added a
  1283. * different one at the same time. I don't think this could
  1284. * happen in practice but that sort of thinking leads to pain
  1285. * and suffering and to the dark side.
  1286. */
  1287. (*old_roots)++;
  1288. ulist_reinit(tmp);
  1289. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1290. GFP_ATOMIC);
  1291. if (ret < 0)
  1292. return ret;
  1293. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1294. if (ret < 0)
  1295. return ret;
  1296. ULIST_ITER_INIT(&tmp_uiter);
  1297. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1298. struct btrfs_qgroup_list *glist;
  1299. qg = u64_to_ptr(tmp_unode->aux);
  1300. /*
  1301. * We use this sequence number to keep from having to
  1302. * run the whole list and 0 out the refcnt every time.
  1303. * We basically use sequnce as the known 0 count and
  1304. * then add 1 everytime we see a qgroup. This is how we
  1305. * get how many of the roots actually point up to the
  1306. * upper level qgroups in order to determine exclusive
  1307. * counts.
  1308. *
  1309. * For rescan we want to set old_refcnt to seq so our
  1310. * exclusive calculations end up correct.
  1311. */
  1312. if (rescan)
  1313. qg->old_refcnt = seq;
  1314. else if (qg->old_refcnt < seq)
  1315. qg->old_refcnt = seq + 1;
  1316. else
  1317. qg->old_refcnt++;
  1318. if (qg->new_refcnt < seq)
  1319. qg->new_refcnt = seq + 1;
  1320. else
  1321. qg->new_refcnt++;
  1322. list_for_each_entry(glist, &qg->groups, next_group) {
  1323. ret = ulist_add(qgroups, glist->group->qgroupid,
  1324. ptr_to_u64(glist->group),
  1325. GFP_ATOMIC);
  1326. if (ret < 0)
  1327. return ret;
  1328. ret = ulist_add(tmp, glist->group->qgroupid,
  1329. ptr_to_u64(glist->group),
  1330. GFP_ATOMIC);
  1331. if (ret < 0)
  1332. return ret;
  1333. }
  1334. }
  1335. }
  1336. return 0;
  1337. }
  1338. /*
  1339. * We need to walk forward in our operation tree and account for any roots that
  1340. * were deleted after we made this operation.
  1341. */
  1342. static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
  1343. struct btrfs_qgroup_operation *oper,
  1344. struct ulist *tmp,
  1345. struct ulist *qgroups, u64 seq,
  1346. int *old_roots)
  1347. {
  1348. struct ulist_node *unode;
  1349. struct ulist_iterator uiter;
  1350. struct btrfs_qgroup *qg;
  1351. struct btrfs_qgroup_operation *tmp_oper;
  1352. struct rb_node *n;
  1353. int ret;
  1354. ulist_reinit(tmp);
  1355. /*
  1356. * We only walk forward in the tree since we're only interested in
  1357. * removals that happened _after_ our operation.
  1358. */
  1359. spin_lock(&fs_info->qgroup_op_lock);
  1360. n = rb_next(&oper->n);
  1361. spin_unlock(&fs_info->qgroup_op_lock);
  1362. if (!n)
  1363. return 0;
  1364. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1365. while (tmp_oper->bytenr == oper->bytenr) {
  1366. /*
  1367. * If it's not a removal we don't care, additions work out
  1368. * properly with our refcnt tracking.
  1369. */
  1370. if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
  1371. tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
  1372. goto next;
  1373. qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
  1374. if (!qg)
  1375. goto next;
  1376. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1377. GFP_ATOMIC);
  1378. if (ret) {
  1379. if (ret < 0)
  1380. return ret;
  1381. /*
  1382. * We only want to increase old_roots if this qgroup is
  1383. * not already in the list of qgroups. If it is already
  1384. * there then that means it must have been re-added or
  1385. * the delete will be discarded because we had an
  1386. * existing ref that we haven't looked up yet. In this
  1387. * case we don't want to increase old_roots. So if ret
  1388. * == 1 then we know that this is the first time we've
  1389. * seen this qgroup and we can bump the old_roots.
  1390. */
  1391. (*old_roots)++;
  1392. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
  1393. GFP_ATOMIC);
  1394. if (ret < 0)
  1395. return ret;
  1396. }
  1397. next:
  1398. spin_lock(&fs_info->qgroup_op_lock);
  1399. n = rb_next(&tmp_oper->n);
  1400. spin_unlock(&fs_info->qgroup_op_lock);
  1401. if (!n)
  1402. break;
  1403. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1404. }
  1405. /* Ok now process the qgroups we found */
  1406. ULIST_ITER_INIT(&uiter);
  1407. while ((unode = ulist_next(tmp, &uiter))) {
  1408. struct btrfs_qgroup_list *glist;
  1409. qg = u64_to_ptr(unode->aux);
  1410. if (qg->old_refcnt < seq)
  1411. qg->old_refcnt = seq + 1;
  1412. else
  1413. qg->old_refcnt++;
  1414. if (qg->new_refcnt < seq)
  1415. qg->new_refcnt = seq + 1;
  1416. else
  1417. qg->new_refcnt++;
  1418. list_for_each_entry(glist, &qg->groups, next_group) {
  1419. ret = ulist_add(qgroups, glist->group->qgroupid,
  1420. ptr_to_u64(glist->group), GFP_ATOMIC);
  1421. if (ret < 0)
  1422. return ret;
  1423. ret = ulist_add(tmp, glist->group->qgroupid,
  1424. ptr_to_u64(glist->group), GFP_ATOMIC);
  1425. if (ret < 0)
  1426. return ret;
  1427. }
  1428. }
  1429. return 0;
  1430. }
  1431. /* Add refcnt for the newly added reference. */
  1432. static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
  1433. struct btrfs_qgroup_operation *oper,
  1434. struct btrfs_qgroup *qgroup,
  1435. struct ulist *tmp, struct ulist *qgroups,
  1436. u64 seq)
  1437. {
  1438. struct ulist_node *unode;
  1439. struct ulist_iterator uiter;
  1440. struct btrfs_qgroup *qg;
  1441. int ret;
  1442. ulist_reinit(tmp);
  1443. ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
  1444. GFP_ATOMIC);
  1445. if (ret < 0)
  1446. return ret;
  1447. ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
  1448. GFP_ATOMIC);
  1449. if (ret < 0)
  1450. return ret;
  1451. ULIST_ITER_INIT(&uiter);
  1452. while ((unode = ulist_next(tmp, &uiter))) {
  1453. struct btrfs_qgroup_list *glist;
  1454. qg = u64_to_ptr(unode->aux);
  1455. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1456. if (qg->new_refcnt < seq)
  1457. qg->new_refcnt = seq + 1;
  1458. else
  1459. qg->new_refcnt++;
  1460. } else {
  1461. if (qg->old_refcnt < seq)
  1462. qg->old_refcnt = seq + 1;
  1463. else
  1464. qg->old_refcnt++;
  1465. }
  1466. list_for_each_entry(glist, &qg->groups, next_group) {
  1467. ret = ulist_add(tmp, glist->group->qgroupid,
  1468. ptr_to_u64(glist->group), GFP_ATOMIC);
  1469. if (ret < 0)
  1470. return ret;
  1471. ret = ulist_add(qgroups, glist->group->qgroupid,
  1472. ptr_to_u64(glist->group), GFP_ATOMIC);
  1473. if (ret < 0)
  1474. return ret;
  1475. }
  1476. }
  1477. return 0;
  1478. }
  1479. /*
  1480. * This adjusts the counters for all referenced qgroups if need be.
  1481. */
  1482. static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
  1483. u64 root_to_skip, u64 num_bytes,
  1484. struct ulist *qgroups, u64 seq,
  1485. int old_roots, int new_roots, int rescan)
  1486. {
  1487. struct ulist_node *unode;
  1488. struct ulist_iterator uiter;
  1489. struct btrfs_qgroup *qg;
  1490. u64 cur_new_count, cur_old_count;
  1491. ULIST_ITER_INIT(&uiter);
  1492. while ((unode = ulist_next(qgroups, &uiter))) {
  1493. bool dirty = false;
  1494. qg = u64_to_ptr(unode->aux);
  1495. /*
  1496. * Wasn't referenced before but is now, add to the reference
  1497. * counters.
  1498. */
  1499. if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
  1500. qg->rfer += num_bytes;
  1501. qg->rfer_cmpr += num_bytes;
  1502. dirty = true;
  1503. }
  1504. /*
  1505. * Was referenced before but isn't now, subtract from the
  1506. * reference counters.
  1507. */
  1508. if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
  1509. qg->rfer -= num_bytes;
  1510. qg->rfer_cmpr -= num_bytes;
  1511. dirty = true;
  1512. }
  1513. if (qg->old_refcnt < seq)
  1514. cur_old_count = 0;
  1515. else
  1516. cur_old_count = qg->old_refcnt - seq;
  1517. if (qg->new_refcnt < seq)
  1518. cur_new_count = 0;
  1519. else
  1520. cur_new_count = qg->new_refcnt - seq;
  1521. /*
  1522. * If our refcount was the same as the roots previously but our
  1523. * new count isn't the same as the number of roots now then we
  1524. * went from having a exclusive reference on this range to not.
  1525. */
  1526. if (old_roots && cur_old_count == old_roots &&
  1527. (cur_new_count != new_roots || new_roots == 0)) {
  1528. WARN_ON(cur_new_count != new_roots && new_roots == 0);
  1529. qg->excl -= num_bytes;
  1530. qg->excl_cmpr -= num_bytes;
  1531. dirty = true;
  1532. }
  1533. /*
  1534. * If we didn't reference all the roots before but now we do we
  1535. * have an exclusive reference to this range.
  1536. */
  1537. if ((!old_roots || (old_roots && cur_old_count != old_roots))
  1538. && cur_new_count == new_roots) {
  1539. qg->excl += num_bytes;
  1540. qg->excl_cmpr += num_bytes;
  1541. dirty = true;
  1542. }
  1543. if (dirty)
  1544. qgroup_dirty(fs_info, qg);
  1545. }
  1546. return 0;
  1547. }
  1548. /*
  1549. * If we removed a data extent and there were other references for that bytenr
  1550. * then we need to lookup all referenced roots to make sure we still don't
  1551. * reference this bytenr. If we do then we can just discard this operation.
  1552. */
  1553. static int check_existing_refs(struct btrfs_trans_handle *trans,
  1554. struct btrfs_fs_info *fs_info,
  1555. struct btrfs_qgroup_operation *oper)
  1556. {
  1557. struct ulist *roots = NULL;
  1558. struct ulist_node *unode;
  1559. struct ulist_iterator uiter;
  1560. int ret = 0;
  1561. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1562. oper->elem.seq, &roots);
  1563. if (ret < 0)
  1564. return ret;
  1565. ret = 0;
  1566. ULIST_ITER_INIT(&uiter);
  1567. while ((unode = ulist_next(roots, &uiter))) {
  1568. if (unode->val == oper->ref_root) {
  1569. ret = 1;
  1570. break;
  1571. }
  1572. }
  1573. ulist_free(roots);
  1574. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1575. return ret;
  1576. }
  1577. /*
  1578. * If we share a reference across multiple roots then we may need to adjust
  1579. * various qgroups referenced and exclusive counters. The basic premise is this
  1580. *
  1581. * 1) We have seq to represent a 0 count. Instead of looping through all of the
  1582. * qgroups and resetting their refcount to 0 we just constantly bump this
  1583. * sequence number to act as the base reference count. This means that if
  1584. * anybody is equal to or below this sequence they were never referenced. We
  1585. * jack this sequence up by the number of roots we found each time in order to
  1586. * make sure we don't have any overlap.
  1587. *
  1588. * 2) We first search all the roots that reference the area _except_ the root
  1589. * we're acting on currently. This makes up the old_refcnt of all the qgroups
  1590. * before.
  1591. *
  1592. * 3) We walk all of the qgroups referenced by the root we are currently acting
  1593. * on, and will either adjust old_refcnt in the case of a removal or the
  1594. * new_refcnt in the case of an addition.
  1595. *
  1596. * 4) Finally we walk all the qgroups that are referenced by this range
  1597. * including the root we are acting on currently. We will adjust the counters
  1598. * based on the number of roots we had and will have after this operation.
  1599. *
  1600. * Take this example as an illustration
  1601. *
  1602. * [qgroup 1/0]
  1603. * / | \
  1604. * [qg 0/0] [qg 0/1] [qg 0/2]
  1605. * \ | /
  1606. * [ extent ]
  1607. *
  1608. * Say we are adding a reference that is covered by qg 0/0. The first step
  1609. * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
  1610. * old_roots being 2. Because it is adding new_roots will be 1. We then go
  1611. * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
  1612. * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
  1613. * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
  1614. * reference and thus must add the size to the referenced bytes. Everything
  1615. * else is the same so nothing else changes.
  1616. */
  1617. static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
  1618. struct btrfs_fs_info *fs_info,
  1619. struct btrfs_qgroup_operation *oper)
  1620. {
  1621. struct ulist *roots = NULL;
  1622. struct ulist *qgroups, *tmp;
  1623. struct btrfs_qgroup *qgroup;
  1624. struct seq_list elem = {};
  1625. u64 seq;
  1626. int old_roots = 0;
  1627. int new_roots = 0;
  1628. int ret = 0;
  1629. if (oper->elem.seq) {
  1630. ret = check_existing_refs(trans, fs_info, oper);
  1631. if (ret < 0)
  1632. return ret;
  1633. if (ret)
  1634. return 0;
  1635. }
  1636. qgroups = ulist_alloc(GFP_NOFS);
  1637. if (!qgroups)
  1638. return -ENOMEM;
  1639. tmp = ulist_alloc(GFP_NOFS);
  1640. if (!tmp) {
  1641. ulist_free(qgroups);
  1642. return -ENOMEM;
  1643. }
  1644. btrfs_get_tree_mod_seq(fs_info, &elem);
  1645. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
  1646. &roots);
  1647. btrfs_put_tree_mod_seq(fs_info, &elem);
  1648. if (ret < 0) {
  1649. ulist_free(qgroups);
  1650. ulist_free(tmp);
  1651. return ret;
  1652. }
  1653. spin_lock(&fs_info->qgroup_lock);
  1654. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1655. if (!qgroup)
  1656. goto out;
  1657. seq = fs_info->qgroup_seq;
  1658. /*
  1659. * So roots is the list of all the roots currently pointing at the
  1660. * bytenr, including the ref we are adding if we are adding, or not if
  1661. * we are removing a ref. So we pass in the ref_root to skip that root
  1662. * in our calculations. We set old_refnct and new_refcnt cause who the
  1663. * hell knows what everything looked like before, and it doesn't matter
  1664. * except...
  1665. */
  1666. ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
  1667. seq, &old_roots, 0);
  1668. if (ret < 0)
  1669. goto out;
  1670. /*
  1671. * Now adjust the refcounts of the qgroups that care about this
  1672. * reference, either the old_count in the case of removal or new_count
  1673. * in the case of an addition.
  1674. */
  1675. ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
  1676. seq);
  1677. if (ret < 0)
  1678. goto out;
  1679. /*
  1680. * ...in the case of removals. If we had a removal before we got around
  1681. * to processing this operation then we need to find that guy and count
  1682. * his references as if they really existed so we don't end up screwing
  1683. * up the exclusive counts. Then whenever we go to process the delete
  1684. * everything will be grand and we can account for whatever exclusive
  1685. * changes need to be made there. We also have to pass in old_roots so
  1686. * we have an accurate count of the roots as it pertains to this
  1687. * operations view of the world.
  1688. */
  1689. ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
  1690. &old_roots);
  1691. if (ret < 0)
  1692. goto out;
  1693. /*
  1694. * We are adding our root, need to adjust up the number of roots,
  1695. * otherwise old_roots is the number of roots we want.
  1696. */
  1697. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1698. new_roots = old_roots + 1;
  1699. } else {
  1700. new_roots = old_roots;
  1701. old_roots++;
  1702. }
  1703. fs_info->qgroup_seq += old_roots + 1;
  1704. /*
  1705. * And now the magic happens, bless Arne for having a pretty elegant
  1706. * solution for this.
  1707. */
  1708. qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
  1709. qgroups, seq, old_roots, new_roots, 0);
  1710. out:
  1711. spin_unlock(&fs_info->qgroup_lock);
  1712. ulist_free(qgroups);
  1713. ulist_free(roots);
  1714. ulist_free(tmp);
  1715. return ret;
  1716. }
  1717. /*
  1718. * Process a reference to a shared subtree. This type of operation is
  1719. * queued during snapshot removal when we encounter extents which are
  1720. * shared between more than one root.
  1721. */
  1722. static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
  1723. struct btrfs_fs_info *fs_info,
  1724. struct btrfs_qgroup_operation *oper)
  1725. {
  1726. struct ulist *roots = NULL;
  1727. struct ulist_node *unode;
  1728. struct ulist_iterator uiter;
  1729. struct btrfs_qgroup_list *glist;
  1730. struct ulist *parents;
  1731. int ret = 0;
  1732. int err;
  1733. struct btrfs_qgroup *qg;
  1734. u64 root_obj = 0;
  1735. struct seq_list elem = {};
  1736. parents = ulist_alloc(GFP_NOFS);
  1737. if (!parents)
  1738. return -ENOMEM;
  1739. btrfs_get_tree_mod_seq(fs_info, &elem);
  1740. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1741. elem.seq, &roots);
  1742. btrfs_put_tree_mod_seq(fs_info, &elem);
  1743. if (ret < 0)
  1744. goto out;
  1745. if (roots->nnodes != 1)
  1746. goto out;
  1747. ULIST_ITER_INIT(&uiter);
  1748. unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
  1749. /*
  1750. * If we find our ref root then that means all refs
  1751. * this extent has to the root have not yet been
  1752. * deleted. In that case, we do nothing and let the
  1753. * last ref for this bytenr drive our update.
  1754. *
  1755. * This can happen for example if an extent is
  1756. * referenced multiple times in a snapshot (clone,
  1757. * etc). If we are in the middle of snapshot removal,
  1758. * queued updates for such an extent will find the
  1759. * root if we have not yet finished removing the
  1760. * snapshot.
  1761. */
  1762. if (unode->val == oper->ref_root)
  1763. goto out;
  1764. root_obj = unode->val;
  1765. BUG_ON(!root_obj);
  1766. spin_lock(&fs_info->qgroup_lock);
  1767. qg = find_qgroup_rb(fs_info, root_obj);
  1768. if (!qg)
  1769. goto out_unlock;
  1770. qg->excl += oper->num_bytes;
  1771. qg->excl_cmpr += oper->num_bytes;
  1772. qgroup_dirty(fs_info, qg);
  1773. /*
  1774. * Adjust counts for parent groups. First we find all
  1775. * parents, then in the 2nd loop we do the adjustment
  1776. * while adding parents of the parents to our ulist.
  1777. */
  1778. list_for_each_entry(glist, &qg->groups, next_group) {
  1779. err = ulist_add(parents, glist->group->qgroupid,
  1780. ptr_to_u64(glist->group), GFP_ATOMIC);
  1781. if (err < 0) {
  1782. ret = err;
  1783. goto out_unlock;
  1784. }
  1785. }
  1786. ULIST_ITER_INIT(&uiter);
  1787. while ((unode = ulist_next(parents, &uiter))) {
  1788. qg = u64_to_ptr(unode->aux);
  1789. qg->excl += oper->num_bytes;
  1790. qg->excl_cmpr += oper->num_bytes;
  1791. qgroup_dirty(fs_info, qg);
  1792. /* Add any parents of the parents */
  1793. list_for_each_entry(glist, &qg->groups, next_group) {
  1794. err = ulist_add(parents, glist->group->qgroupid,
  1795. ptr_to_u64(glist->group), GFP_ATOMIC);
  1796. if (err < 0) {
  1797. ret = err;
  1798. goto out_unlock;
  1799. }
  1800. }
  1801. }
  1802. out_unlock:
  1803. spin_unlock(&fs_info->qgroup_lock);
  1804. out:
  1805. ulist_free(roots);
  1806. ulist_free(parents);
  1807. return ret;
  1808. }
  1809. /*
  1810. * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
  1811. * from the fs. First, all roots referencing the extent are searched, and
  1812. * then the space is accounted accordingly to the different roots. The
  1813. * accounting algorithm works in 3 steps documented inline.
  1814. */
  1815. static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
  1816. struct btrfs_fs_info *fs_info,
  1817. struct btrfs_qgroup_operation *oper)
  1818. {
  1819. int ret = 0;
  1820. if (!fs_info->quota_enabled)
  1821. return 0;
  1822. BUG_ON(!fs_info->quota_root);
  1823. mutex_lock(&fs_info->qgroup_rescan_lock);
  1824. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1825. if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
  1826. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1827. return 0;
  1828. }
  1829. }
  1830. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1831. ASSERT(is_fstree(oper->ref_root));
  1832. trace_btrfs_qgroup_account(oper);
  1833. switch (oper->type) {
  1834. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1835. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1836. ret = qgroup_excl_accounting(fs_info, oper);
  1837. break;
  1838. case BTRFS_QGROUP_OPER_ADD_SHARED:
  1839. case BTRFS_QGROUP_OPER_SUB_SHARED:
  1840. ret = qgroup_shared_accounting(trans, fs_info, oper);
  1841. break;
  1842. case BTRFS_QGROUP_OPER_SUB_SUBTREE:
  1843. ret = qgroup_subtree_accounting(trans, fs_info, oper);
  1844. break;
  1845. default:
  1846. ASSERT(0);
  1847. }
  1848. return ret;
  1849. }
  1850. /*
  1851. * Needs to be called everytime we run delayed refs, even if there is an error
  1852. * in order to cleanup outstanding operations.
  1853. */
  1854. int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
  1855. struct btrfs_fs_info *fs_info)
  1856. {
  1857. struct btrfs_qgroup_operation *oper;
  1858. int ret = 0;
  1859. while (!list_empty(&trans->qgroup_ref_list)) {
  1860. oper = list_first_entry(&trans->qgroup_ref_list,
  1861. struct btrfs_qgroup_operation, list);
  1862. list_del_init(&oper->list);
  1863. if (!ret || !trans->aborted)
  1864. ret = btrfs_qgroup_account(trans, fs_info, oper);
  1865. spin_lock(&fs_info->qgroup_op_lock);
  1866. rb_erase(&oper->n, &fs_info->qgroup_op_tree);
  1867. spin_unlock(&fs_info->qgroup_op_lock);
  1868. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1869. kfree(oper);
  1870. }
  1871. return ret;
  1872. }
  1873. /*
  1874. * called from commit_transaction. Writes all changed qgroups to disk.
  1875. */
  1876. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1877. struct btrfs_fs_info *fs_info)
  1878. {
  1879. struct btrfs_root *quota_root = fs_info->quota_root;
  1880. int ret = 0;
  1881. int start_rescan_worker = 0;
  1882. if (!quota_root)
  1883. goto out;
  1884. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1885. start_rescan_worker = 1;
  1886. fs_info->quota_enabled = fs_info->pending_quota_state;
  1887. spin_lock(&fs_info->qgroup_lock);
  1888. while (!list_empty(&fs_info->dirty_qgroups)) {
  1889. struct btrfs_qgroup *qgroup;
  1890. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1891. struct btrfs_qgroup, dirty);
  1892. list_del_init(&qgroup->dirty);
  1893. spin_unlock(&fs_info->qgroup_lock);
  1894. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1895. if (ret)
  1896. fs_info->qgroup_flags |=
  1897. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1898. spin_lock(&fs_info->qgroup_lock);
  1899. }
  1900. if (fs_info->quota_enabled)
  1901. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1902. else
  1903. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1904. spin_unlock(&fs_info->qgroup_lock);
  1905. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1906. if (ret)
  1907. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1908. if (!ret && start_rescan_worker) {
  1909. ret = qgroup_rescan_init(fs_info, 0, 1);
  1910. if (!ret) {
  1911. qgroup_rescan_zero_tracking(fs_info);
  1912. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1913. &fs_info->qgroup_rescan_work);
  1914. }
  1915. ret = 0;
  1916. }
  1917. out:
  1918. return ret;
  1919. }
  1920. /*
  1921. * copy the acounting information between qgroups. This is necessary when a
  1922. * snapshot or a subvolume is created
  1923. */
  1924. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1925. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1926. struct btrfs_qgroup_inherit *inherit)
  1927. {
  1928. int ret = 0;
  1929. int i;
  1930. u64 *i_qgroups;
  1931. struct btrfs_root *quota_root = fs_info->quota_root;
  1932. struct btrfs_qgroup *srcgroup;
  1933. struct btrfs_qgroup *dstgroup;
  1934. u32 level_size = 0;
  1935. u64 nums;
  1936. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1937. if (!fs_info->quota_enabled)
  1938. goto out;
  1939. if (!quota_root) {
  1940. ret = -EINVAL;
  1941. goto out;
  1942. }
  1943. if (inherit) {
  1944. i_qgroups = (u64 *)(inherit + 1);
  1945. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1946. 2 * inherit->num_excl_copies;
  1947. for (i = 0; i < nums; ++i) {
  1948. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1949. if (!srcgroup) {
  1950. ret = -EINVAL;
  1951. goto out;
  1952. }
  1953. ++i_qgroups;
  1954. }
  1955. }
  1956. /*
  1957. * create a tracking group for the subvol itself
  1958. */
  1959. ret = add_qgroup_item(trans, quota_root, objectid);
  1960. if (ret)
  1961. goto out;
  1962. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1963. ret = update_qgroup_limit_item(trans, quota_root, objectid,
  1964. inherit->lim.flags,
  1965. inherit->lim.max_rfer,
  1966. inherit->lim.max_excl,
  1967. inherit->lim.rsv_rfer,
  1968. inherit->lim.rsv_excl);
  1969. if (ret)
  1970. goto out;
  1971. }
  1972. if (srcid) {
  1973. struct btrfs_root *srcroot;
  1974. struct btrfs_key srckey;
  1975. srckey.objectid = srcid;
  1976. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1977. srckey.offset = (u64)-1;
  1978. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1979. if (IS_ERR(srcroot)) {
  1980. ret = PTR_ERR(srcroot);
  1981. goto out;
  1982. }
  1983. rcu_read_lock();
  1984. level_size = srcroot->nodesize;
  1985. rcu_read_unlock();
  1986. }
  1987. /*
  1988. * add qgroup to all inherited groups
  1989. */
  1990. if (inherit) {
  1991. i_qgroups = (u64 *)(inherit + 1);
  1992. for (i = 0; i < inherit->num_qgroups; ++i) {
  1993. ret = add_qgroup_relation_item(trans, quota_root,
  1994. objectid, *i_qgroups);
  1995. if (ret)
  1996. goto out;
  1997. ret = add_qgroup_relation_item(trans, quota_root,
  1998. *i_qgroups, objectid);
  1999. if (ret)
  2000. goto out;
  2001. ++i_qgroups;
  2002. }
  2003. }
  2004. spin_lock(&fs_info->qgroup_lock);
  2005. dstgroup = add_qgroup_rb(fs_info, objectid);
  2006. if (IS_ERR(dstgroup)) {
  2007. ret = PTR_ERR(dstgroup);
  2008. goto unlock;
  2009. }
  2010. if (srcid) {
  2011. srcgroup = find_qgroup_rb(fs_info, srcid);
  2012. if (!srcgroup)
  2013. goto unlock;
  2014. /*
  2015. * We call inherit after we clone the root in order to make sure
  2016. * our counts don't go crazy, so at this point the only
  2017. * difference between the two roots should be the root node.
  2018. */
  2019. dstgroup->rfer = srcgroup->rfer;
  2020. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  2021. dstgroup->excl = level_size;
  2022. dstgroup->excl_cmpr = level_size;
  2023. srcgroup->excl = level_size;
  2024. srcgroup->excl_cmpr = level_size;
  2025. qgroup_dirty(fs_info, dstgroup);
  2026. qgroup_dirty(fs_info, srcgroup);
  2027. }
  2028. if (!inherit)
  2029. goto unlock;
  2030. i_qgroups = (u64 *)(inherit + 1);
  2031. for (i = 0; i < inherit->num_qgroups; ++i) {
  2032. ret = add_relation_rb(quota_root->fs_info, objectid,
  2033. *i_qgroups);
  2034. if (ret)
  2035. goto unlock;
  2036. ++i_qgroups;
  2037. }
  2038. for (i = 0; i < inherit->num_ref_copies; ++i) {
  2039. struct btrfs_qgroup *src;
  2040. struct btrfs_qgroup *dst;
  2041. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2042. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2043. if (!src || !dst) {
  2044. ret = -EINVAL;
  2045. goto unlock;
  2046. }
  2047. dst->rfer = src->rfer - level_size;
  2048. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  2049. i_qgroups += 2;
  2050. }
  2051. for (i = 0; i < inherit->num_excl_copies; ++i) {
  2052. struct btrfs_qgroup *src;
  2053. struct btrfs_qgroup *dst;
  2054. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2055. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2056. if (!src || !dst) {
  2057. ret = -EINVAL;
  2058. goto unlock;
  2059. }
  2060. dst->excl = src->excl + level_size;
  2061. dst->excl_cmpr = src->excl_cmpr + level_size;
  2062. i_qgroups += 2;
  2063. }
  2064. unlock:
  2065. spin_unlock(&fs_info->qgroup_lock);
  2066. out:
  2067. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2068. return ret;
  2069. }
  2070. /*
  2071. * reserve some space for a qgroup and all its parents. The reservation takes
  2072. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  2073. * accounting. If not enough space is available, EDQUOT is returned.
  2074. * We assume that the requested space is new for all qgroups.
  2075. */
  2076. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  2077. {
  2078. struct btrfs_root *quota_root;
  2079. struct btrfs_qgroup *qgroup;
  2080. struct btrfs_fs_info *fs_info = root->fs_info;
  2081. u64 ref_root = root->root_key.objectid;
  2082. int ret = 0;
  2083. struct ulist_node *unode;
  2084. struct ulist_iterator uiter;
  2085. if (!is_fstree(ref_root))
  2086. return 0;
  2087. if (num_bytes == 0)
  2088. return 0;
  2089. spin_lock(&fs_info->qgroup_lock);
  2090. quota_root = fs_info->quota_root;
  2091. if (!quota_root)
  2092. goto out;
  2093. qgroup = find_qgroup_rb(fs_info, ref_root);
  2094. if (!qgroup)
  2095. goto out;
  2096. /*
  2097. * in a first step, we check all affected qgroups if any limits would
  2098. * be exceeded
  2099. */
  2100. ulist_reinit(fs_info->qgroup_ulist);
  2101. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2102. (uintptr_t)qgroup, GFP_ATOMIC);
  2103. if (ret < 0)
  2104. goto out;
  2105. ULIST_ITER_INIT(&uiter);
  2106. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2107. struct btrfs_qgroup *qg;
  2108. struct btrfs_qgroup_list *glist;
  2109. qg = u64_to_ptr(unode->aux);
  2110. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2111. qg->reserved + (s64)qg->rfer + num_bytes >
  2112. qg->max_rfer) {
  2113. ret = -EDQUOT;
  2114. goto out;
  2115. }
  2116. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2117. qg->reserved + (s64)qg->excl + num_bytes >
  2118. qg->max_excl) {
  2119. ret = -EDQUOT;
  2120. goto out;
  2121. }
  2122. list_for_each_entry(glist, &qg->groups, next_group) {
  2123. ret = ulist_add(fs_info->qgroup_ulist,
  2124. glist->group->qgroupid,
  2125. (uintptr_t)glist->group, GFP_ATOMIC);
  2126. if (ret < 0)
  2127. goto out;
  2128. }
  2129. }
  2130. ret = 0;
  2131. /*
  2132. * no limits exceeded, now record the reservation into all qgroups
  2133. */
  2134. ULIST_ITER_INIT(&uiter);
  2135. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2136. struct btrfs_qgroup *qg;
  2137. qg = u64_to_ptr(unode->aux);
  2138. qg->reserved += num_bytes;
  2139. }
  2140. out:
  2141. spin_unlock(&fs_info->qgroup_lock);
  2142. return ret;
  2143. }
  2144. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  2145. {
  2146. struct btrfs_root *quota_root;
  2147. struct btrfs_qgroup *qgroup;
  2148. struct btrfs_fs_info *fs_info = root->fs_info;
  2149. struct ulist_node *unode;
  2150. struct ulist_iterator uiter;
  2151. u64 ref_root = root->root_key.objectid;
  2152. int ret = 0;
  2153. if (!is_fstree(ref_root))
  2154. return;
  2155. if (num_bytes == 0)
  2156. return;
  2157. spin_lock(&fs_info->qgroup_lock);
  2158. quota_root = fs_info->quota_root;
  2159. if (!quota_root)
  2160. goto out;
  2161. qgroup = find_qgroup_rb(fs_info, ref_root);
  2162. if (!qgroup)
  2163. goto out;
  2164. ulist_reinit(fs_info->qgroup_ulist);
  2165. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2166. (uintptr_t)qgroup, GFP_ATOMIC);
  2167. if (ret < 0)
  2168. goto out;
  2169. ULIST_ITER_INIT(&uiter);
  2170. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2171. struct btrfs_qgroup *qg;
  2172. struct btrfs_qgroup_list *glist;
  2173. qg = u64_to_ptr(unode->aux);
  2174. qg->reserved -= num_bytes;
  2175. list_for_each_entry(glist, &qg->groups, next_group) {
  2176. ret = ulist_add(fs_info->qgroup_ulist,
  2177. glist->group->qgroupid,
  2178. (uintptr_t)glist->group, GFP_ATOMIC);
  2179. if (ret < 0)
  2180. goto out;
  2181. }
  2182. }
  2183. out:
  2184. spin_unlock(&fs_info->qgroup_lock);
  2185. }
  2186. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  2187. {
  2188. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  2189. return;
  2190. btrfs_err(trans->root->fs_info,
  2191. "qgroups not uptodate in trans handle %p: list is%s empty, "
  2192. "seq is %#x.%x",
  2193. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2194. (u32)(trans->delayed_ref_elem.seq >> 32),
  2195. (u32)trans->delayed_ref_elem.seq);
  2196. BUG();
  2197. }
  2198. /*
  2199. * returns < 0 on error, 0 when more leafs are to be scanned.
  2200. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  2201. */
  2202. static int
  2203. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2204. struct btrfs_trans_handle *trans, struct ulist *qgroups,
  2205. struct ulist *tmp, struct extent_buffer *scratch_leaf)
  2206. {
  2207. struct btrfs_key found;
  2208. struct ulist *roots = NULL;
  2209. struct seq_list tree_mod_seq_elem = {};
  2210. u64 num_bytes;
  2211. u64 seq;
  2212. int new_roots;
  2213. int slot;
  2214. int ret;
  2215. path->leave_spinning = 1;
  2216. mutex_lock(&fs_info->qgroup_rescan_lock);
  2217. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2218. &fs_info->qgroup_rescan_progress,
  2219. path, 1, 0);
  2220. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  2221. fs_info->qgroup_rescan_progress.objectid,
  2222. fs_info->qgroup_rescan_progress.type,
  2223. fs_info->qgroup_rescan_progress.offset, ret);
  2224. if (ret) {
  2225. /*
  2226. * The rescan is about to end, we will not be scanning any
  2227. * further blocks. We cannot unset the RESCAN flag here, because
  2228. * we want to commit the transaction if everything went well.
  2229. * To make the live accounting work in this phase, we set our
  2230. * scan progress pointer such that every real extent objectid
  2231. * will be smaller.
  2232. */
  2233. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2234. btrfs_release_path(path);
  2235. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2236. return ret;
  2237. }
  2238. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2239. btrfs_header_nritems(path->nodes[0]) - 1);
  2240. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2241. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2242. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  2243. slot = path->slots[0];
  2244. btrfs_release_path(path);
  2245. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2246. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2247. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2248. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2249. found.type != BTRFS_METADATA_ITEM_KEY)
  2250. continue;
  2251. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2252. num_bytes = fs_info->extent_root->nodesize;
  2253. else
  2254. num_bytes = found.offset;
  2255. ulist_reinit(qgroups);
  2256. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2257. &roots);
  2258. if (ret < 0)
  2259. goto out;
  2260. spin_lock(&fs_info->qgroup_lock);
  2261. seq = fs_info->qgroup_seq;
  2262. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  2263. new_roots = 0;
  2264. ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
  2265. seq, &new_roots, 1);
  2266. if (ret < 0) {
  2267. spin_unlock(&fs_info->qgroup_lock);
  2268. ulist_free(roots);
  2269. goto out;
  2270. }
  2271. ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
  2272. seq, 0, new_roots, 1);
  2273. if (ret < 0) {
  2274. spin_unlock(&fs_info->qgroup_lock);
  2275. ulist_free(roots);
  2276. goto out;
  2277. }
  2278. spin_unlock(&fs_info->qgroup_lock);
  2279. ulist_free(roots);
  2280. }
  2281. out:
  2282. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2283. return ret;
  2284. }
  2285. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2286. {
  2287. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2288. qgroup_rescan_work);
  2289. struct btrfs_path *path;
  2290. struct btrfs_trans_handle *trans = NULL;
  2291. struct ulist *tmp = NULL, *qgroups = NULL;
  2292. struct extent_buffer *scratch_leaf = NULL;
  2293. int err = -ENOMEM;
  2294. path = btrfs_alloc_path();
  2295. if (!path)
  2296. goto out;
  2297. qgroups = ulist_alloc(GFP_NOFS);
  2298. if (!qgroups)
  2299. goto out;
  2300. tmp = ulist_alloc(GFP_NOFS);
  2301. if (!tmp)
  2302. goto out;
  2303. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  2304. if (!scratch_leaf)
  2305. goto out;
  2306. err = 0;
  2307. while (!err) {
  2308. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2309. if (IS_ERR(trans)) {
  2310. err = PTR_ERR(trans);
  2311. break;
  2312. }
  2313. if (!fs_info->quota_enabled) {
  2314. err = -EINTR;
  2315. } else {
  2316. err = qgroup_rescan_leaf(fs_info, path, trans,
  2317. qgroups, tmp, scratch_leaf);
  2318. }
  2319. if (err > 0)
  2320. btrfs_commit_transaction(trans, fs_info->fs_root);
  2321. else
  2322. btrfs_end_transaction(trans, fs_info->fs_root);
  2323. }
  2324. out:
  2325. kfree(scratch_leaf);
  2326. ulist_free(qgroups);
  2327. ulist_free(tmp);
  2328. btrfs_free_path(path);
  2329. mutex_lock(&fs_info->qgroup_rescan_lock);
  2330. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2331. if (err == 2 &&
  2332. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2333. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2334. } else if (err < 0) {
  2335. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2336. }
  2337. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2338. if (err >= 0) {
  2339. btrfs_info(fs_info, "qgroup scan completed%s",
  2340. err == 2 ? " (inconsistency flag cleared)" : "");
  2341. } else {
  2342. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2343. }
  2344. complete_all(&fs_info->qgroup_rescan_completion);
  2345. }
  2346. /*
  2347. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2348. * memory required for the rescan context.
  2349. */
  2350. static int
  2351. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2352. int init_flags)
  2353. {
  2354. int ret = 0;
  2355. if (!init_flags &&
  2356. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2357. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2358. ret = -EINVAL;
  2359. goto err;
  2360. }
  2361. mutex_lock(&fs_info->qgroup_rescan_lock);
  2362. spin_lock(&fs_info->qgroup_lock);
  2363. if (init_flags) {
  2364. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2365. ret = -EINPROGRESS;
  2366. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2367. ret = -EINVAL;
  2368. if (ret) {
  2369. spin_unlock(&fs_info->qgroup_lock);
  2370. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2371. goto err;
  2372. }
  2373. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2374. }
  2375. memset(&fs_info->qgroup_rescan_progress, 0,
  2376. sizeof(fs_info->qgroup_rescan_progress));
  2377. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2378. spin_unlock(&fs_info->qgroup_lock);
  2379. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2380. init_completion(&fs_info->qgroup_rescan_completion);
  2381. memset(&fs_info->qgroup_rescan_work, 0,
  2382. sizeof(fs_info->qgroup_rescan_work));
  2383. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2384. btrfs_qgroup_rescan_helper,
  2385. btrfs_qgroup_rescan_worker, NULL, NULL);
  2386. if (ret) {
  2387. err:
  2388. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2389. return ret;
  2390. }
  2391. return 0;
  2392. }
  2393. static void
  2394. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2395. {
  2396. struct rb_node *n;
  2397. struct btrfs_qgroup *qgroup;
  2398. spin_lock(&fs_info->qgroup_lock);
  2399. /* clear all current qgroup tracking information */
  2400. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2401. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2402. qgroup->rfer = 0;
  2403. qgroup->rfer_cmpr = 0;
  2404. qgroup->excl = 0;
  2405. qgroup->excl_cmpr = 0;
  2406. }
  2407. spin_unlock(&fs_info->qgroup_lock);
  2408. }
  2409. int
  2410. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2411. {
  2412. int ret = 0;
  2413. struct btrfs_trans_handle *trans;
  2414. ret = qgroup_rescan_init(fs_info, 0, 1);
  2415. if (ret)
  2416. return ret;
  2417. /*
  2418. * We have set the rescan_progress to 0, which means no more
  2419. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2420. * However, btrfs_qgroup_account_ref may be right after its call
  2421. * to btrfs_find_all_roots, in which case it would still do the
  2422. * accounting.
  2423. * To solve this, we're committing the transaction, which will
  2424. * ensure we run all delayed refs and only after that, we are
  2425. * going to clear all tracking information for a clean start.
  2426. */
  2427. trans = btrfs_join_transaction(fs_info->fs_root);
  2428. if (IS_ERR(trans)) {
  2429. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2430. return PTR_ERR(trans);
  2431. }
  2432. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2433. if (ret) {
  2434. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2435. return ret;
  2436. }
  2437. qgroup_rescan_zero_tracking(fs_info);
  2438. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2439. &fs_info->qgroup_rescan_work);
  2440. return 0;
  2441. }
  2442. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  2443. {
  2444. int running;
  2445. int ret = 0;
  2446. mutex_lock(&fs_info->qgroup_rescan_lock);
  2447. spin_lock(&fs_info->qgroup_lock);
  2448. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2449. spin_unlock(&fs_info->qgroup_lock);
  2450. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2451. if (running)
  2452. ret = wait_for_completion_interruptible(
  2453. &fs_info->qgroup_rescan_completion);
  2454. return ret;
  2455. }
  2456. /*
  2457. * this is only called from open_ctree where we're still single threaded, thus
  2458. * locking is omitted here.
  2459. */
  2460. void
  2461. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2462. {
  2463. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2464. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2465. &fs_info->qgroup_rescan_work);
  2466. }