qgroup.c 71 KB

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