qgroup.c 77 KB

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