qgroup.c 73 KB

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