qgroup.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097
  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. set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
  685. btrfs_free_path(path);
  686. return ret;
  687. }
  688. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  689. struct btrfs_fs_info *fs_info)
  690. {
  691. struct btrfs_root *quota_root;
  692. struct btrfs_root *tree_root = fs_info->tree_root;
  693. struct btrfs_path *path = NULL;
  694. struct btrfs_qgroup_status_item *ptr;
  695. struct extent_buffer *leaf;
  696. struct btrfs_key key;
  697. struct btrfs_key found_key;
  698. struct btrfs_qgroup *qgroup = NULL;
  699. int ret = 0;
  700. int slot;
  701. mutex_lock(&fs_info->qgroup_ioctl_lock);
  702. if (fs_info->quota_root) {
  703. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  704. goto out;
  705. }
  706. fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
  707. if (!fs_info->qgroup_ulist) {
  708. ret = -ENOMEM;
  709. goto out;
  710. }
  711. /*
  712. * initially create the quota tree
  713. */
  714. quota_root = btrfs_create_tree(trans, fs_info,
  715. BTRFS_QUOTA_TREE_OBJECTID);
  716. if (IS_ERR(quota_root)) {
  717. ret = PTR_ERR(quota_root);
  718. goto out;
  719. }
  720. path = btrfs_alloc_path();
  721. if (!path) {
  722. ret = -ENOMEM;
  723. goto out_free_root;
  724. }
  725. key.objectid = 0;
  726. key.type = BTRFS_QGROUP_STATUS_KEY;
  727. key.offset = 0;
  728. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  729. sizeof(*ptr));
  730. if (ret)
  731. goto out_free_path;
  732. leaf = path->nodes[0];
  733. ptr = btrfs_item_ptr(leaf, path->slots[0],
  734. struct btrfs_qgroup_status_item);
  735. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  736. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  737. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  738. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  739. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  740. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  741. btrfs_mark_buffer_dirty(leaf);
  742. key.objectid = 0;
  743. key.type = BTRFS_ROOT_REF_KEY;
  744. key.offset = 0;
  745. btrfs_release_path(path);
  746. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  747. if (ret > 0)
  748. goto out_add_root;
  749. if (ret < 0)
  750. goto out_free_path;
  751. while (1) {
  752. slot = path->slots[0];
  753. leaf = path->nodes[0];
  754. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  755. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  756. ret = add_qgroup_item(trans, quota_root,
  757. found_key.offset);
  758. if (ret)
  759. goto out_free_path;
  760. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  761. if (IS_ERR(qgroup)) {
  762. ret = PTR_ERR(qgroup);
  763. goto out_free_path;
  764. }
  765. }
  766. ret = btrfs_next_item(tree_root, path);
  767. if (ret < 0)
  768. goto out_free_path;
  769. if (ret)
  770. break;
  771. }
  772. out_add_root:
  773. btrfs_release_path(path);
  774. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  775. if (ret)
  776. goto out_free_path;
  777. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  778. if (IS_ERR(qgroup)) {
  779. ret = PTR_ERR(qgroup);
  780. goto out_free_path;
  781. }
  782. spin_lock(&fs_info->qgroup_lock);
  783. fs_info->quota_root = quota_root;
  784. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  785. spin_unlock(&fs_info->qgroup_lock);
  786. out_free_path:
  787. btrfs_free_path(path);
  788. out_free_root:
  789. if (ret) {
  790. free_extent_buffer(quota_root->node);
  791. free_extent_buffer(quota_root->commit_root);
  792. kfree(quota_root);
  793. }
  794. out:
  795. if (ret) {
  796. ulist_free(fs_info->qgroup_ulist);
  797. fs_info->qgroup_ulist = NULL;
  798. }
  799. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  800. return ret;
  801. }
  802. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  803. struct btrfs_fs_info *fs_info)
  804. {
  805. struct btrfs_root *quota_root;
  806. int ret = 0;
  807. mutex_lock(&fs_info->qgroup_ioctl_lock);
  808. if (!fs_info->quota_root)
  809. goto out;
  810. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  811. set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
  812. btrfs_qgroup_wait_for_completion(fs_info, false);
  813. spin_lock(&fs_info->qgroup_lock);
  814. quota_root = fs_info->quota_root;
  815. fs_info->quota_root = NULL;
  816. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  817. spin_unlock(&fs_info->qgroup_lock);
  818. btrfs_free_qgroup_config(fs_info);
  819. ret = btrfs_clean_quota_tree(trans, quota_root);
  820. if (ret)
  821. goto out;
  822. ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
  823. if (ret)
  824. goto out;
  825. list_del(&quota_root->dirty_list);
  826. btrfs_tree_lock(quota_root->node);
  827. clean_tree_block(fs_info, quota_root->node);
  828. btrfs_tree_unlock(quota_root->node);
  829. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  830. free_extent_buffer(quota_root->node);
  831. free_extent_buffer(quota_root->commit_root);
  832. kfree(quota_root);
  833. out:
  834. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  835. return ret;
  836. }
  837. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  838. struct btrfs_qgroup *qgroup)
  839. {
  840. if (list_empty(&qgroup->dirty))
  841. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  842. }
  843. static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
  844. struct btrfs_qgroup *qgroup,
  845. u64 num_bytes)
  846. {
  847. #ifdef CONFIG_BTRFS_DEBUG
  848. WARN_ON(qgroup->reserved < num_bytes);
  849. btrfs_debug(fs_info,
  850. "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
  851. qgroup->qgroupid, qgroup->reserved, num_bytes);
  852. #endif
  853. qgroup->reserved = 0;
  854. }
  855. /*
  856. * The easy accounting, if we are adding/removing the only ref for an extent
  857. * then this qgroup and all of the parent qgroups get their reference and
  858. * exclusive counts adjusted.
  859. *
  860. * Caller should hold fs_info->qgroup_lock.
  861. */
  862. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  863. struct ulist *tmp, u64 ref_root,
  864. u64 num_bytes, int sign)
  865. {
  866. struct btrfs_qgroup *qgroup;
  867. struct btrfs_qgroup_list *glist;
  868. struct ulist_node *unode;
  869. struct ulist_iterator uiter;
  870. int ret = 0;
  871. qgroup = find_qgroup_rb(fs_info, ref_root);
  872. if (!qgroup)
  873. goto out;
  874. qgroup->rfer += sign * num_bytes;
  875. qgroup->rfer_cmpr += sign * num_bytes;
  876. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  877. qgroup->excl += sign * num_bytes;
  878. qgroup->excl_cmpr += sign * num_bytes;
  879. if (sign > 0) {
  880. trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
  881. if (qgroup->reserved < num_bytes)
  882. report_reserved_underflow(fs_info, qgroup, num_bytes);
  883. else
  884. qgroup->reserved -= num_bytes;
  885. }
  886. qgroup_dirty(fs_info, qgroup);
  887. /* Get all of the parent groups that contain this qgroup */
  888. list_for_each_entry(glist, &qgroup->groups, next_group) {
  889. ret = ulist_add(tmp, glist->group->qgroupid,
  890. qgroup_to_aux(glist->group), GFP_ATOMIC);
  891. if (ret < 0)
  892. goto out;
  893. }
  894. /* Iterate all of the parents and adjust their reference counts */
  895. ULIST_ITER_INIT(&uiter);
  896. while ((unode = ulist_next(tmp, &uiter))) {
  897. qgroup = unode_aux_to_qgroup(unode);
  898. qgroup->rfer += sign * num_bytes;
  899. qgroup->rfer_cmpr += sign * num_bytes;
  900. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  901. qgroup->excl += sign * num_bytes;
  902. if (sign > 0) {
  903. trace_qgroup_update_reserve(fs_info, qgroup,
  904. -(s64)num_bytes);
  905. if (qgroup->reserved < num_bytes)
  906. report_reserved_underflow(fs_info, qgroup,
  907. num_bytes);
  908. else
  909. qgroup->reserved -= num_bytes;
  910. }
  911. qgroup->excl_cmpr += sign * num_bytes;
  912. qgroup_dirty(fs_info, qgroup);
  913. /* Add any parents of the parents */
  914. list_for_each_entry(glist, &qgroup->groups, next_group) {
  915. ret = ulist_add(tmp, glist->group->qgroupid,
  916. qgroup_to_aux(glist->group), GFP_ATOMIC);
  917. if (ret < 0)
  918. goto out;
  919. }
  920. }
  921. ret = 0;
  922. out:
  923. return ret;
  924. }
  925. /*
  926. * Quick path for updating qgroup with only excl refs.
  927. *
  928. * In that case, just update all parent will be enough.
  929. * Or we needs to do a full rescan.
  930. * Caller should also hold fs_info->qgroup_lock.
  931. *
  932. * Return 0 for quick update, return >0 for need to full rescan
  933. * and mark INCONSISTENT flag.
  934. * Return < 0 for other error.
  935. */
  936. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  937. struct ulist *tmp, u64 src, u64 dst,
  938. int sign)
  939. {
  940. struct btrfs_qgroup *qgroup;
  941. int ret = 1;
  942. int err = 0;
  943. qgroup = find_qgroup_rb(fs_info, src);
  944. if (!qgroup)
  945. goto out;
  946. if (qgroup->excl == qgroup->rfer) {
  947. ret = 0;
  948. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  949. qgroup->excl, sign);
  950. if (err < 0) {
  951. ret = err;
  952. goto out;
  953. }
  954. }
  955. out:
  956. if (ret)
  957. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  958. return ret;
  959. }
  960. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  961. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  962. {
  963. struct btrfs_root *quota_root;
  964. struct btrfs_qgroup *parent;
  965. struct btrfs_qgroup *member;
  966. struct btrfs_qgroup_list *list;
  967. struct ulist *tmp;
  968. int ret = 0;
  969. /* Check the level of src and dst first */
  970. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  971. return -EINVAL;
  972. tmp = ulist_alloc(GFP_KERNEL);
  973. if (!tmp)
  974. return -ENOMEM;
  975. mutex_lock(&fs_info->qgroup_ioctl_lock);
  976. quota_root = fs_info->quota_root;
  977. if (!quota_root) {
  978. ret = -EINVAL;
  979. goto out;
  980. }
  981. member = find_qgroup_rb(fs_info, src);
  982. parent = find_qgroup_rb(fs_info, dst);
  983. if (!member || !parent) {
  984. ret = -EINVAL;
  985. goto out;
  986. }
  987. /* check if such qgroup relation exist firstly */
  988. list_for_each_entry(list, &member->groups, next_group) {
  989. if (list->group == parent) {
  990. ret = -EEXIST;
  991. goto out;
  992. }
  993. }
  994. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  995. if (ret)
  996. goto out;
  997. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  998. if (ret) {
  999. del_qgroup_relation_item(trans, quota_root, src, dst);
  1000. goto out;
  1001. }
  1002. spin_lock(&fs_info->qgroup_lock);
  1003. ret = add_relation_rb(fs_info, src, dst);
  1004. if (ret < 0) {
  1005. spin_unlock(&fs_info->qgroup_lock);
  1006. goto out;
  1007. }
  1008. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1009. spin_unlock(&fs_info->qgroup_lock);
  1010. out:
  1011. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1012. ulist_free(tmp);
  1013. return ret;
  1014. }
  1015. static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1016. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1017. {
  1018. struct btrfs_root *quota_root;
  1019. struct btrfs_qgroup *parent;
  1020. struct btrfs_qgroup *member;
  1021. struct btrfs_qgroup_list *list;
  1022. struct ulist *tmp;
  1023. int ret = 0;
  1024. int err;
  1025. tmp = ulist_alloc(GFP_KERNEL);
  1026. if (!tmp)
  1027. return -ENOMEM;
  1028. quota_root = fs_info->quota_root;
  1029. if (!quota_root) {
  1030. ret = -EINVAL;
  1031. goto out;
  1032. }
  1033. member = find_qgroup_rb(fs_info, src);
  1034. parent = find_qgroup_rb(fs_info, dst);
  1035. if (!member || !parent) {
  1036. ret = -EINVAL;
  1037. goto out;
  1038. }
  1039. /* check if such qgroup relation exist firstly */
  1040. list_for_each_entry(list, &member->groups, next_group) {
  1041. if (list->group == parent)
  1042. goto exist;
  1043. }
  1044. ret = -ENOENT;
  1045. goto out;
  1046. exist:
  1047. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1048. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1049. if (err && !ret)
  1050. ret = err;
  1051. spin_lock(&fs_info->qgroup_lock);
  1052. del_relation_rb(fs_info, src, dst);
  1053. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1054. spin_unlock(&fs_info->qgroup_lock);
  1055. out:
  1056. ulist_free(tmp);
  1057. return ret;
  1058. }
  1059. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1060. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1061. {
  1062. int ret = 0;
  1063. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1064. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1065. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1066. return ret;
  1067. }
  1068. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1069. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1070. {
  1071. struct btrfs_root *quota_root;
  1072. struct btrfs_qgroup *qgroup;
  1073. int ret = 0;
  1074. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1075. quota_root = fs_info->quota_root;
  1076. if (!quota_root) {
  1077. ret = -EINVAL;
  1078. goto out;
  1079. }
  1080. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1081. if (qgroup) {
  1082. ret = -EEXIST;
  1083. goto out;
  1084. }
  1085. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1086. if (ret)
  1087. goto out;
  1088. spin_lock(&fs_info->qgroup_lock);
  1089. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1090. spin_unlock(&fs_info->qgroup_lock);
  1091. if (IS_ERR(qgroup))
  1092. ret = PTR_ERR(qgroup);
  1093. out:
  1094. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1095. return ret;
  1096. }
  1097. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1098. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1099. {
  1100. struct btrfs_root *quota_root;
  1101. struct btrfs_qgroup *qgroup;
  1102. struct btrfs_qgroup_list *list;
  1103. int ret = 0;
  1104. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1105. quota_root = fs_info->quota_root;
  1106. if (!quota_root) {
  1107. ret = -EINVAL;
  1108. goto out;
  1109. }
  1110. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1111. if (!qgroup) {
  1112. ret = -ENOENT;
  1113. goto out;
  1114. } else {
  1115. /* check if there are no children of this qgroup */
  1116. if (!list_empty(&qgroup->members)) {
  1117. ret = -EBUSY;
  1118. goto out;
  1119. }
  1120. }
  1121. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1122. while (!list_empty(&qgroup->groups)) {
  1123. list = list_first_entry(&qgroup->groups,
  1124. struct btrfs_qgroup_list, next_group);
  1125. ret = __del_qgroup_relation(trans, fs_info,
  1126. qgroupid,
  1127. list->group->qgroupid);
  1128. if (ret)
  1129. goto out;
  1130. }
  1131. spin_lock(&fs_info->qgroup_lock);
  1132. del_qgroup_rb(fs_info, qgroupid);
  1133. spin_unlock(&fs_info->qgroup_lock);
  1134. out:
  1135. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1136. return ret;
  1137. }
  1138. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1139. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1140. struct btrfs_qgroup_limit *limit)
  1141. {
  1142. struct btrfs_root *quota_root;
  1143. struct btrfs_qgroup *qgroup;
  1144. int ret = 0;
  1145. /* Sometimes we would want to clear the limit on this qgroup.
  1146. * To meet this requirement, we treat the -1 as a special value
  1147. * which tell kernel to clear the limit on this qgroup.
  1148. */
  1149. const u64 CLEAR_VALUE = -1;
  1150. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1151. quota_root = fs_info->quota_root;
  1152. if (!quota_root) {
  1153. ret = -EINVAL;
  1154. goto out;
  1155. }
  1156. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1157. if (!qgroup) {
  1158. ret = -ENOENT;
  1159. goto out;
  1160. }
  1161. spin_lock(&fs_info->qgroup_lock);
  1162. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1163. if (limit->max_rfer == CLEAR_VALUE) {
  1164. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1165. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1166. qgroup->max_rfer = 0;
  1167. } else {
  1168. qgroup->max_rfer = limit->max_rfer;
  1169. }
  1170. }
  1171. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1172. if (limit->max_excl == CLEAR_VALUE) {
  1173. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1174. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1175. qgroup->max_excl = 0;
  1176. } else {
  1177. qgroup->max_excl = limit->max_excl;
  1178. }
  1179. }
  1180. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1181. if (limit->rsv_rfer == CLEAR_VALUE) {
  1182. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1183. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1184. qgroup->rsv_rfer = 0;
  1185. } else {
  1186. qgroup->rsv_rfer = limit->rsv_rfer;
  1187. }
  1188. }
  1189. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1190. if (limit->rsv_excl == CLEAR_VALUE) {
  1191. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1192. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1193. qgroup->rsv_excl = 0;
  1194. } else {
  1195. qgroup->rsv_excl = limit->rsv_excl;
  1196. }
  1197. }
  1198. qgroup->lim_flags |= limit->flags;
  1199. spin_unlock(&fs_info->qgroup_lock);
  1200. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1201. if (ret) {
  1202. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1203. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1204. qgroupid);
  1205. }
  1206. out:
  1207. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1208. return ret;
  1209. }
  1210. int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
  1211. struct btrfs_delayed_ref_root *delayed_refs,
  1212. struct btrfs_qgroup_extent_record *record)
  1213. {
  1214. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1215. struct rb_node *parent_node = NULL;
  1216. struct btrfs_qgroup_extent_record *entry;
  1217. u64 bytenr = record->bytenr;
  1218. assert_spin_locked(&delayed_refs->lock);
  1219. trace_btrfs_qgroup_trace_extent(fs_info, record);
  1220. while (*p) {
  1221. parent_node = *p;
  1222. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1223. node);
  1224. if (bytenr < entry->bytenr)
  1225. p = &(*p)->rb_left;
  1226. else if (bytenr > entry->bytenr)
  1227. p = &(*p)->rb_right;
  1228. else
  1229. return 1;
  1230. }
  1231. rb_link_node(&record->node, parent_node, p);
  1232. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1233. return 0;
  1234. }
  1235. int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
  1236. struct btrfs_qgroup_extent_record *qrecord)
  1237. {
  1238. struct ulist *old_root;
  1239. u64 bytenr = qrecord->bytenr;
  1240. int ret;
  1241. ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
  1242. if (ret < 0)
  1243. return ret;
  1244. /*
  1245. * Here we don't need to get the lock of
  1246. * trans->transaction->delayed_refs, since inserted qrecord won't
  1247. * be deleted, only qrecord->node may be modified (new qrecord insert)
  1248. *
  1249. * So modifying qrecord->old_roots is safe here
  1250. */
  1251. qrecord->old_roots = old_root;
  1252. return 0;
  1253. }
  1254. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
  1255. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  1256. gfp_t gfp_flag)
  1257. {
  1258. struct btrfs_qgroup_extent_record *record;
  1259. struct btrfs_delayed_ref_root *delayed_refs;
  1260. int ret;
  1261. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
  1262. || bytenr == 0 || num_bytes == 0)
  1263. return 0;
  1264. if (WARN_ON(trans == NULL))
  1265. return -EINVAL;
  1266. record = kmalloc(sizeof(*record), gfp_flag);
  1267. if (!record)
  1268. return -ENOMEM;
  1269. delayed_refs = &trans->transaction->delayed_refs;
  1270. record->bytenr = bytenr;
  1271. record->num_bytes = num_bytes;
  1272. record->old_roots = NULL;
  1273. spin_lock(&delayed_refs->lock);
  1274. ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
  1275. spin_unlock(&delayed_refs->lock);
  1276. if (ret > 0) {
  1277. kfree(record);
  1278. return 0;
  1279. }
  1280. return btrfs_qgroup_trace_extent_post(fs_info, record);
  1281. }
  1282. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  1283. struct btrfs_fs_info *fs_info,
  1284. struct extent_buffer *eb)
  1285. {
  1286. int nr = btrfs_header_nritems(eb);
  1287. int i, extent_type, ret;
  1288. struct btrfs_key key;
  1289. struct btrfs_file_extent_item *fi;
  1290. u64 bytenr, num_bytes;
  1291. /* We can be called directly from walk_up_proc() */
  1292. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1293. return 0;
  1294. for (i = 0; i < nr; i++) {
  1295. btrfs_item_key_to_cpu(eb, &key, i);
  1296. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1297. continue;
  1298. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  1299. /* filter out non qgroup-accountable extents */
  1300. extent_type = btrfs_file_extent_type(eb, fi);
  1301. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1302. continue;
  1303. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  1304. if (!bytenr)
  1305. continue;
  1306. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  1307. ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
  1308. num_bytes, GFP_NOFS);
  1309. if (ret)
  1310. return ret;
  1311. }
  1312. cond_resched();
  1313. return 0;
  1314. }
  1315. /*
  1316. * Walk up the tree from the bottom, freeing leaves and any interior
  1317. * nodes which have had all slots visited. If a node (leaf or
  1318. * interior) is freed, the node above it will have it's slot
  1319. * incremented. The root node will never be freed.
  1320. *
  1321. * At the end of this function, we should have a path which has all
  1322. * slots incremented to the next position for a search. If we need to
  1323. * read a new node it will be NULL and the node above it will have the
  1324. * correct slot selected for a later read.
  1325. *
  1326. * If we increment the root nodes slot counter past the number of
  1327. * elements, 1 is returned to signal completion of the search.
  1328. */
  1329. static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
  1330. {
  1331. int level = 0;
  1332. int nr, slot;
  1333. struct extent_buffer *eb;
  1334. if (root_level == 0)
  1335. return 1;
  1336. while (level <= root_level) {
  1337. eb = path->nodes[level];
  1338. nr = btrfs_header_nritems(eb);
  1339. path->slots[level]++;
  1340. slot = path->slots[level];
  1341. if (slot >= nr || level == 0) {
  1342. /*
  1343. * Don't free the root - we will detect this
  1344. * condition after our loop and return a
  1345. * positive value for caller to stop walking the tree.
  1346. */
  1347. if (level != root_level) {
  1348. btrfs_tree_unlock_rw(eb, path->locks[level]);
  1349. path->locks[level] = 0;
  1350. free_extent_buffer(eb);
  1351. path->nodes[level] = NULL;
  1352. path->slots[level] = 0;
  1353. }
  1354. } else {
  1355. /*
  1356. * We have a valid slot to walk back down
  1357. * from. Stop here so caller can process these
  1358. * new nodes.
  1359. */
  1360. break;
  1361. }
  1362. level++;
  1363. }
  1364. eb = path->nodes[root_level];
  1365. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  1366. return 1;
  1367. return 0;
  1368. }
  1369. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  1370. struct btrfs_root *root,
  1371. struct extent_buffer *root_eb,
  1372. u64 root_gen, int root_level)
  1373. {
  1374. struct btrfs_fs_info *fs_info = root->fs_info;
  1375. int ret = 0;
  1376. int level;
  1377. struct extent_buffer *eb = root_eb;
  1378. struct btrfs_path *path = NULL;
  1379. BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
  1380. BUG_ON(root_eb == NULL);
  1381. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1382. return 0;
  1383. if (!extent_buffer_uptodate(root_eb)) {
  1384. ret = btrfs_read_buffer(root_eb, root_gen);
  1385. if (ret)
  1386. goto out;
  1387. }
  1388. if (root_level == 0) {
  1389. ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
  1390. goto out;
  1391. }
  1392. path = btrfs_alloc_path();
  1393. if (!path)
  1394. return -ENOMEM;
  1395. /*
  1396. * Walk down the tree. Missing extent blocks are filled in as
  1397. * we go. Metadata is accounted every time we read a new
  1398. * extent block.
  1399. *
  1400. * When we reach a leaf, we account for file extent items in it,
  1401. * walk back up the tree (adjusting slot pointers as we go)
  1402. * and restart the search process.
  1403. */
  1404. extent_buffer_get(root_eb); /* For path */
  1405. path->nodes[root_level] = root_eb;
  1406. path->slots[root_level] = 0;
  1407. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  1408. walk_down:
  1409. level = root_level;
  1410. while (level >= 0) {
  1411. if (path->nodes[level] == NULL) {
  1412. int parent_slot;
  1413. u64 child_gen;
  1414. u64 child_bytenr;
  1415. /*
  1416. * We need to get child blockptr/gen from parent before
  1417. * we can read it.
  1418. */
  1419. eb = path->nodes[level + 1];
  1420. parent_slot = path->slots[level + 1];
  1421. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  1422. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  1423. eb = read_tree_block(fs_info, child_bytenr, child_gen);
  1424. if (IS_ERR(eb)) {
  1425. ret = PTR_ERR(eb);
  1426. goto out;
  1427. } else if (!extent_buffer_uptodate(eb)) {
  1428. free_extent_buffer(eb);
  1429. ret = -EIO;
  1430. goto out;
  1431. }
  1432. path->nodes[level] = eb;
  1433. path->slots[level] = 0;
  1434. btrfs_tree_read_lock(eb);
  1435. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1436. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  1437. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  1438. child_bytenr,
  1439. fs_info->nodesize,
  1440. GFP_NOFS);
  1441. if (ret)
  1442. goto out;
  1443. }
  1444. if (level == 0) {
  1445. ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
  1446. path->nodes[level]);
  1447. if (ret)
  1448. goto out;
  1449. /* Nonzero return here means we completed our search */
  1450. ret = adjust_slots_upwards(path, root_level);
  1451. if (ret)
  1452. break;
  1453. /* Restart search with new slots */
  1454. goto walk_down;
  1455. }
  1456. level--;
  1457. }
  1458. ret = 0;
  1459. out:
  1460. btrfs_free_path(path);
  1461. return ret;
  1462. }
  1463. #define UPDATE_NEW 0
  1464. #define UPDATE_OLD 1
  1465. /*
  1466. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1467. */
  1468. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1469. struct ulist *roots, struct ulist *tmp,
  1470. struct ulist *qgroups, u64 seq, int update_old)
  1471. {
  1472. struct ulist_node *unode;
  1473. struct ulist_iterator uiter;
  1474. struct ulist_node *tmp_unode;
  1475. struct ulist_iterator tmp_uiter;
  1476. struct btrfs_qgroup *qg;
  1477. int ret = 0;
  1478. if (!roots)
  1479. return 0;
  1480. ULIST_ITER_INIT(&uiter);
  1481. while ((unode = ulist_next(roots, &uiter))) {
  1482. qg = find_qgroup_rb(fs_info, unode->val);
  1483. if (!qg)
  1484. continue;
  1485. ulist_reinit(tmp);
  1486. ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
  1487. GFP_ATOMIC);
  1488. if (ret < 0)
  1489. return ret;
  1490. ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
  1491. if (ret < 0)
  1492. return ret;
  1493. ULIST_ITER_INIT(&tmp_uiter);
  1494. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1495. struct btrfs_qgroup_list *glist;
  1496. qg = unode_aux_to_qgroup(tmp_unode);
  1497. if (update_old)
  1498. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1499. else
  1500. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1501. list_for_each_entry(glist, &qg->groups, next_group) {
  1502. ret = ulist_add(qgroups, glist->group->qgroupid,
  1503. qgroup_to_aux(glist->group),
  1504. GFP_ATOMIC);
  1505. if (ret < 0)
  1506. return ret;
  1507. ret = ulist_add(tmp, glist->group->qgroupid,
  1508. qgroup_to_aux(glist->group),
  1509. GFP_ATOMIC);
  1510. if (ret < 0)
  1511. return ret;
  1512. }
  1513. }
  1514. }
  1515. return 0;
  1516. }
  1517. /*
  1518. * Update qgroup rfer/excl counters.
  1519. * Rfer update is easy, codes can explain themselves.
  1520. *
  1521. * Excl update is tricky, the update is split into 2 part.
  1522. * Part 1: Possible exclusive <-> sharing detect:
  1523. * | A | !A |
  1524. * -------------------------------------
  1525. * B | * | - |
  1526. * -------------------------------------
  1527. * !B | + | ** |
  1528. * -------------------------------------
  1529. *
  1530. * Conditions:
  1531. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1532. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1533. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1534. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  1535. *
  1536. * Results:
  1537. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1538. * *: Definitely not changed. **: Possible unchanged.
  1539. *
  1540. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1541. *
  1542. * To make the logic clear, we first use condition A and B to split
  1543. * combination into 4 results.
  1544. *
  1545. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1546. * only on variant maybe 0.
  1547. *
  1548. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1549. * again(2x2).
  1550. * But this time we don't need to consider other things, the codes and logic
  1551. * is easy to understand now.
  1552. */
  1553. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1554. struct ulist *qgroups,
  1555. u64 nr_old_roots,
  1556. u64 nr_new_roots,
  1557. u64 num_bytes, u64 seq)
  1558. {
  1559. struct ulist_node *unode;
  1560. struct ulist_iterator uiter;
  1561. struct btrfs_qgroup *qg;
  1562. u64 cur_new_count, cur_old_count;
  1563. ULIST_ITER_INIT(&uiter);
  1564. while ((unode = ulist_next(qgroups, &uiter))) {
  1565. bool dirty = false;
  1566. qg = unode_aux_to_qgroup(unode);
  1567. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1568. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1569. trace_qgroup_update_counters(fs_info, qg->qgroupid,
  1570. cur_old_count, cur_new_count);
  1571. /* Rfer update part */
  1572. if (cur_old_count == 0 && cur_new_count > 0) {
  1573. qg->rfer += num_bytes;
  1574. qg->rfer_cmpr += num_bytes;
  1575. dirty = true;
  1576. }
  1577. if (cur_old_count > 0 && cur_new_count == 0) {
  1578. qg->rfer -= num_bytes;
  1579. qg->rfer_cmpr -= num_bytes;
  1580. dirty = true;
  1581. }
  1582. /* Excl update part */
  1583. /* Exclusive/none -> shared case */
  1584. if (cur_old_count == nr_old_roots &&
  1585. cur_new_count < nr_new_roots) {
  1586. /* Exclusive -> shared */
  1587. if (cur_old_count != 0) {
  1588. qg->excl -= num_bytes;
  1589. qg->excl_cmpr -= num_bytes;
  1590. dirty = true;
  1591. }
  1592. }
  1593. /* Shared -> exclusive/none case */
  1594. if (cur_old_count < nr_old_roots &&
  1595. cur_new_count == nr_new_roots) {
  1596. /* Shared->exclusive */
  1597. if (cur_new_count != 0) {
  1598. qg->excl += num_bytes;
  1599. qg->excl_cmpr += num_bytes;
  1600. dirty = true;
  1601. }
  1602. }
  1603. /* Exclusive/none -> exclusive/none case */
  1604. if (cur_old_count == nr_old_roots &&
  1605. cur_new_count == nr_new_roots) {
  1606. if (cur_old_count == 0) {
  1607. /* None -> exclusive/none */
  1608. if (cur_new_count != 0) {
  1609. /* None -> exclusive */
  1610. qg->excl += num_bytes;
  1611. qg->excl_cmpr += num_bytes;
  1612. dirty = true;
  1613. }
  1614. /* None -> none, nothing changed */
  1615. } else {
  1616. /* Exclusive -> exclusive/none */
  1617. if (cur_new_count == 0) {
  1618. /* Exclusive -> none */
  1619. qg->excl -= num_bytes;
  1620. qg->excl_cmpr -= num_bytes;
  1621. dirty = true;
  1622. }
  1623. /* Exclusive -> exclusive, nothing changed */
  1624. }
  1625. }
  1626. if (dirty)
  1627. qgroup_dirty(fs_info, qg);
  1628. }
  1629. return 0;
  1630. }
  1631. /*
  1632. * Check if the @roots potentially is a list of fs tree roots
  1633. *
  1634. * Return 0 for definitely not a fs/subvol tree roots ulist
  1635. * Return 1 for possible fs/subvol tree roots in the list (considering an empty
  1636. * one as well)
  1637. */
  1638. static int maybe_fs_roots(struct ulist *roots)
  1639. {
  1640. struct ulist_node *unode;
  1641. struct ulist_iterator uiter;
  1642. /* Empty one, still possible for fs roots */
  1643. if (!roots || roots->nnodes == 0)
  1644. return 1;
  1645. ULIST_ITER_INIT(&uiter);
  1646. unode = ulist_next(roots, &uiter);
  1647. if (!unode)
  1648. return 1;
  1649. /*
  1650. * If it contains fs tree roots, then it must belong to fs/subvol
  1651. * trees.
  1652. * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
  1653. */
  1654. return is_fstree(unode->val);
  1655. }
  1656. int
  1657. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1658. struct btrfs_fs_info *fs_info,
  1659. u64 bytenr, u64 num_bytes,
  1660. struct ulist *old_roots, struct ulist *new_roots)
  1661. {
  1662. struct ulist *qgroups = NULL;
  1663. struct ulist *tmp = NULL;
  1664. u64 seq;
  1665. u64 nr_new_roots = 0;
  1666. u64 nr_old_roots = 0;
  1667. int ret = 0;
  1668. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1669. return 0;
  1670. if (new_roots) {
  1671. if (!maybe_fs_roots(new_roots))
  1672. goto out_free;
  1673. nr_new_roots = new_roots->nnodes;
  1674. }
  1675. if (old_roots) {
  1676. if (!maybe_fs_roots(old_roots))
  1677. goto out_free;
  1678. nr_old_roots = old_roots->nnodes;
  1679. }
  1680. /* Quick exit, either not fs tree roots, or won't affect any qgroup */
  1681. if (nr_old_roots == 0 && nr_new_roots == 0)
  1682. goto out_free;
  1683. BUG_ON(!fs_info->quota_root);
  1684. trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
  1685. nr_old_roots, nr_new_roots);
  1686. qgroups = ulist_alloc(GFP_NOFS);
  1687. if (!qgroups) {
  1688. ret = -ENOMEM;
  1689. goto out_free;
  1690. }
  1691. tmp = ulist_alloc(GFP_NOFS);
  1692. if (!tmp) {
  1693. ret = -ENOMEM;
  1694. goto out_free;
  1695. }
  1696. mutex_lock(&fs_info->qgroup_rescan_lock);
  1697. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1698. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1699. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1700. ret = 0;
  1701. goto out_free;
  1702. }
  1703. }
  1704. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1705. spin_lock(&fs_info->qgroup_lock);
  1706. seq = fs_info->qgroup_seq;
  1707. /* Update old refcnts using old_roots */
  1708. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1709. UPDATE_OLD);
  1710. if (ret < 0)
  1711. goto out;
  1712. /* Update new refcnts using new_roots */
  1713. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1714. UPDATE_NEW);
  1715. if (ret < 0)
  1716. goto out;
  1717. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1718. num_bytes, seq);
  1719. /*
  1720. * Bump qgroup_seq to avoid seq overlap
  1721. */
  1722. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1723. out:
  1724. spin_unlock(&fs_info->qgroup_lock);
  1725. out_free:
  1726. ulist_free(tmp);
  1727. ulist_free(qgroups);
  1728. ulist_free(old_roots);
  1729. ulist_free(new_roots);
  1730. return ret;
  1731. }
  1732. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1733. struct btrfs_fs_info *fs_info)
  1734. {
  1735. struct btrfs_qgroup_extent_record *record;
  1736. struct btrfs_delayed_ref_root *delayed_refs;
  1737. struct ulist *new_roots = NULL;
  1738. struct rb_node *node;
  1739. u64 qgroup_to_skip;
  1740. int ret = 0;
  1741. delayed_refs = &trans->transaction->delayed_refs;
  1742. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1743. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1744. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1745. node);
  1746. trace_btrfs_qgroup_account_extents(fs_info, record);
  1747. if (!ret) {
  1748. /*
  1749. * Old roots should be searched when inserting qgroup
  1750. * extent record
  1751. */
  1752. if (WARN_ON(!record->old_roots)) {
  1753. /* Search commit root to find old_roots */
  1754. ret = btrfs_find_all_roots(NULL, fs_info,
  1755. record->bytenr, 0,
  1756. &record->old_roots);
  1757. if (ret < 0)
  1758. goto cleanup;
  1759. }
  1760. /*
  1761. * Use SEQ_LAST as time_seq to do special search, which
  1762. * doesn't lock tree or delayed_refs and search current
  1763. * root. It's safe inside commit_transaction().
  1764. */
  1765. ret = btrfs_find_all_roots(trans, fs_info,
  1766. record->bytenr, SEQ_LAST, &new_roots);
  1767. if (ret < 0)
  1768. goto cleanup;
  1769. if (qgroup_to_skip) {
  1770. ulist_del(new_roots, qgroup_to_skip, 0);
  1771. ulist_del(record->old_roots, qgroup_to_skip,
  1772. 0);
  1773. }
  1774. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1775. record->bytenr, record->num_bytes,
  1776. record->old_roots, new_roots);
  1777. record->old_roots = NULL;
  1778. new_roots = NULL;
  1779. }
  1780. cleanup:
  1781. ulist_free(record->old_roots);
  1782. ulist_free(new_roots);
  1783. new_roots = NULL;
  1784. rb_erase(node, &delayed_refs->dirty_extent_root);
  1785. kfree(record);
  1786. }
  1787. return ret;
  1788. }
  1789. /*
  1790. * called from commit_transaction. Writes all changed qgroups to disk.
  1791. */
  1792. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1793. struct btrfs_fs_info *fs_info)
  1794. {
  1795. struct btrfs_root *quota_root = fs_info->quota_root;
  1796. int ret = 0;
  1797. int start_rescan_worker = 0;
  1798. if (!quota_root)
  1799. goto out;
  1800. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
  1801. test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1802. start_rescan_worker = 1;
  1803. if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1804. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1805. if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
  1806. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1807. spin_lock(&fs_info->qgroup_lock);
  1808. while (!list_empty(&fs_info->dirty_qgroups)) {
  1809. struct btrfs_qgroup *qgroup;
  1810. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1811. struct btrfs_qgroup, dirty);
  1812. list_del_init(&qgroup->dirty);
  1813. spin_unlock(&fs_info->qgroup_lock);
  1814. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1815. if (ret)
  1816. fs_info->qgroup_flags |=
  1817. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1818. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1819. if (ret)
  1820. fs_info->qgroup_flags |=
  1821. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1822. spin_lock(&fs_info->qgroup_lock);
  1823. }
  1824. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1825. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1826. else
  1827. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1828. spin_unlock(&fs_info->qgroup_lock);
  1829. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1830. if (ret)
  1831. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1832. if (!ret && start_rescan_worker) {
  1833. ret = qgroup_rescan_init(fs_info, 0, 1);
  1834. if (!ret) {
  1835. qgroup_rescan_zero_tracking(fs_info);
  1836. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1837. &fs_info->qgroup_rescan_work);
  1838. }
  1839. ret = 0;
  1840. }
  1841. out:
  1842. return ret;
  1843. }
  1844. /*
  1845. * Copy the accounting information between qgroups. This is necessary
  1846. * when a snapshot or a subvolume is created. Throwing an error will
  1847. * cause a transaction abort so we take extra care here to only error
  1848. * when a readonly fs is a reasonable outcome.
  1849. */
  1850. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1851. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1852. struct btrfs_qgroup_inherit *inherit)
  1853. {
  1854. int ret = 0;
  1855. int i;
  1856. u64 *i_qgroups;
  1857. struct btrfs_root *quota_root = fs_info->quota_root;
  1858. struct btrfs_qgroup *srcgroup;
  1859. struct btrfs_qgroup *dstgroup;
  1860. u32 level_size = 0;
  1861. u64 nums;
  1862. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1863. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1864. goto out;
  1865. if (!quota_root) {
  1866. ret = -EINVAL;
  1867. goto out;
  1868. }
  1869. if (inherit) {
  1870. i_qgroups = (u64 *)(inherit + 1);
  1871. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1872. 2 * inherit->num_excl_copies;
  1873. for (i = 0; i < nums; ++i) {
  1874. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1875. /*
  1876. * Zero out invalid groups so we can ignore
  1877. * them later.
  1878. */
  1879. if (!srcgroup ||
  1880. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  1881. *i_qgroups = 0ULL;
  1882. ++i_qgroups;
  1883. }
  1884. }
  1885. /*
  1886. * create a tracking group for the subvol itself
  1887. */
  1888. ret = add_qgroup_item(trans, quota_root, objectid);
  1889. if (ret)
  1890. goto out;
  1891. if (srcid) {
  1892. struct btrfs_root *srcroot;
  1893. struct btrfs_key srckey;
  1894. srckey.objectid = srcid;
  1895. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1896. srckey.offset = (u64)-1;
  1897. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1898. if (IS_ERR(srcroot)) {
  1899. ret = PTR_ERR(srcroot);
  1900. goto out;
  1901. }
  1902. level_size = fs_info->nodesize;
  1903. }
  1904. /*
  1905. * add qgroup to all inherited groups
  1906. */
  1907. if (inherit) {
  1908. i_qgroups = (u64 *)(inherit + 1);
  1909. for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
  1910. if (*i_qgroups == 0)
  1911. continue;
  1912. ret = add_qgroup_relation_item(trans, quota_root,
  1913. objectid, *i_qgroups);
  1914. if (ret && ret != -EEXIST)
  1915. goto out;
  1916. ret = add_qgroup_relation_item(trans, quota_root,
  1917. *i_qgroups, objectid);
  1918. if (ret && ret != -EEXIST)
  1919. goto out;
  1920. }
  1921. ret = 0;
  1922. }
  1923. spin_lock(&fs_info->qgroup_lock);
  1924. dstgroup = add_qgroup_rb(fs_info, objectid);
  1925. if (IS_ERR(dstgroup)) {
  1926. ret = PTR_ERR(dstgroup);
  1927. goto unlock;
  1928. }
  1929. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1930. dstgroup->lim_flags = inherit->lim.flags;
  1931. dstgroup->max_rfer = inherit->lim.max_rfer;
  1932. dstgroup->max_excl = inherit->lim.max_excl;
  1933. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1934. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1935. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1936. if (ret) {
  1937. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1938. btrfs_info(fs_info,
  1939. "unable to update quota limit for %llu",
  1940. dstgroup->qgroupid);
  1941. goto unlock;
  1942. }
  1943. }
  1944. if (srcid) {
  1945. srcgroup = find_qgroup_rb(fs_info, srcid);
  1946. if (!srcgroup)
  1947. goto unlock;
  1948. /*
  1949. * We call inherit after we clone the root in order to make sure
  1950. * our counts don't go crazy, so at this point the only
  1951. * difference between the two roots should be the root node.
  1952. */
  1953. dstgroup->rfer = srcgroup->rfer;
  1954. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1955. dstgroup->excl = level_size;
  1956. dstgroup->excl_cmpr = level_size;
  1957. srcgroup->excl = level_size;
  1958. srcgroup->excl_cmpr = level_size;
  1959. /* inherit the limit info */
  1960. dstgroup->lim_flags = srcgroup->lim_flags;
  1961. dstgroup->max_rfer = srcgroup->max_rfer;
  1962. dstgroup->max_excl = srcgroup->max_excl;
  1963. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1964. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1965. qgroup_dirty(fs_info, dstgroup);
  1966. qgroup_dirty(fs_info, srcgroup);
  1967. }
  1968. if (!inherit)
  1969. goto unlock;
  1970. i_qgroups = (u64 *)(inherit + 1);
  1971. for (i = 0; i < inherit->num_qgroups; ++i) {
  1972. if (*i_qgroups) {
  1973. ret = add_relation_rb(fs_info, objectid, *i_qgroups);
  1974. if (ret)
  1975. goto unlock;
  1976. }
  1977. ++i_qgroups;
  1978. }
  1979. for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
  1980. struct btrfs_qgroup *src;
  1981. struct btrfs_qgroup *dst;
  1982. if (!i_qgroups[0] || !i_qgroups[1])
  1983. continue;
  1984. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1985. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1986. if (!src || !dst) {
  1987. ret = -EINVAL;
  1988. goto unlock;
  1989. }
  1990. dst->rfer = src->rfer - level_size;
  1991. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1992. }
  1993. for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
  1994. struct btrfs_qgroup *src;
  1995. struct btrfs_qgroup *dst;
  1996. if (!i_qgroups[0] || !i_qgroups[1])
  1997. continue;
  1998. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1999. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2000. if (!src || !dst) {
  2001. ret = -EINVAL;
  2002. goto unlock;
  2003. }
  2004. dst->excl = src->excl + level_size;
  2005. dst->excl_cmpr = src->excl_cmpr + level_size;
  2006. }
  2007. unlock:
  2008. spin_unlock(&fs_info->qgroup_lock);
  2009. out:
  2010. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2011. return ret;
  2012. }
  2013. static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
  2014. {
  2015. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2016. qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
  2017. return false;
  2018. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2019. qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
  2020. return false;
  2021. return true;
  2022. }
  2023. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
  2024. {
  2025. struct btrfs_root *quota_root;
  2026. struct btrfs_qgroup *qgroup;
  2027. struct btrfs_fs_info *fs_info = root->fs_info;
  2028. u64 ref_root = root->root_key.objectid;
  2029. int ret = 0;
  2030. int retried = 0;
  2031. struct ulist_node *unode;
  2032. struct ulist_iterator uiter;
  2033. if (!is_fstree(ref_root))
  2034. return 0;
  2035. if (num_bytes == 0)
  2036. return 0;
  2037. if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
  2038. capable(CAP_SYS_RESOURCE))
  2039. enforce = false;
  2040. retry:
  2041. spin_lock(&fs_info->qgroup_lock);
  2042. quota_root = fs_info->quota_root;
  2043. if (!quota_root)
  2044. goto out;
  2045. qgroup = find_qgroup_rb(fs_info, ref_root);
  2046. if (!qgroup)
  2047. goto out;
  2048. /*
  2049. * in a first step, we check all affected qgroups if any limits would
  2050. * be exceeded
  2051. */
  2052. ulist_reinit(fs_info->qgroup_ulist);
  2053. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2054. (uintptr_t)qgroup, GFP_ATOMIC);
  2055. if (ret < 0)
  2056. goto out;
  2057. ULIST_ITER_INIT(&uiter);
  2058. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2059. struct btrfs_qgroup *qg;
  2060. struct btrfs_qgroup_list *glist;
  2061. qg = unode_aux_to_qgroup(unode);
  2062. if (enforce && !qgroup_check_limits(qg, num_bytes)) {
  2063. /*
  2064. * Commit the tree and retry, since we may have
  2065. * deletions which would free up space.
  2066. */
  2067. if (!retried && qg->reserved > 0) {
  2068. struct btrfs_trans_handle *trans;
  2069. spin_unlock(&fs_info->qgroup_lock);
  2070. ret = btrfs_start_delalloc_inodes(root, 0);
  2071. if (ret)
  2072. return ret;
  2073. btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
  2074. trans = btrfs_join_transaction(root);
  2075. if (IS_ERR(trans))
  2076. return PTR_ERR(trans);
  2077. ret = btrfs_commit_transaction(trans);
  2078. if (ret)
  2079. return ret;
  2080. retried++;
  2081. goto retry;
  2082. }
  2083. ret = -EDQUOT;
  2084. goto out;
  2085. }
  2086. list_for_each_entry(glist, &qg->groups, next_group) {
  2087. ret = ulist_add(fs_info->qgroup_ulist,
  2088. glist->group->qgroupid,
  2089. (uintptr_t)glist->group, GFP_ATOMIC);
  2090. if (ret < 0)
  2091. goto out;
  2092. }
  2093. }
  2094. ret = 0;
  2095. /*
  2096. * no limits exceeded, now record the reservation into all qgroups
  2097. */
  2098. ULIST_ITER_INIT(&uiter);
  2099. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2100. struct btrfs_qgroup *qg;
  2101. qg = unode_aux_to_qgroup(unode);
  2102. trace_qgroup_update_reserve(fs_info, qg, num_bytes);
  2103. qg->reserved += num_bytes;
  2104. }
  2105. out:
  2106. spin_unlock(&fs_info->qgroup_lock);
  2107. return ret;
  2108. }
  2109. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  2110. u64 ref_root, u64 num_bytes)
  2111. {
  2112. struct btrfs_root *quota_root;
  2113. struct btrfs_qgroup *qgroup;
  2114. struct ulist_node *unode;
  2115. struct ulist_iterator uiter;
  2116. int ret = 0;
  2117. if (!is_fstree(ref_root))
  2118. return;
  2119. if (num_bytes == 0)
  2120. return;
  2121. spin_lock(&fs_info->qgroup_lock);
  2122. quota_root = fs_info->quota_root;
  2123. if (!quota_root)
  2124. goto out;
  2125. qgroup = find_qgroup_rb(fs_info, ref_root);
  2126. if (!qgroup)
  2127. goto out;
  2128. ulist_reinit(fs_info->qgroup_ulist);
  2129. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2130. (uintptr_t)qgroup, GFP_ATOMIC);
  2131. if (ret < 0)
  2132. goto out;
  2133. ULIST_ITER_INIT(&uiter);
  2134. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2135. struct btrfs_qgroup *qg;
  2136. struct btrfs_qgroup_list *glist;
  2137. qg = unode_aux_to_qgroup(unode);
  2138. trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
  2139. if (qg->reserved < num_bytes)
  2140. report_reserved_underflow(fs_info, qg, num_bytes);
  2141. else
  2142. qg->reserved -= num_bytes;
  2143. list_for_each_entry(glist, &qg->groups, next_group) {
  2144. ret = ulist_add(fs_info->qgroup_ulist,
  2145. glist->group->qgroupid,
  2146. (uintptr_t)glist->group, GFP_ATOMIC);
  2147. if (ret < 0)
  2148. goto out;
  2149. }
  2150. }
  2151. out:
  2152. spin_unlock(&fs_info->qgroup_lock);
  2153. }
  2154. /*
  2155. * returns < 0 on error, 0 when more leafs are to be scanned.
  2156. * returns 1 when done.
  2157. */
  2158. static int
  2159. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2160. struct btrfs_trans_handle *trans)
  2161. {
  2162. struct btrfs_key found;
  2163. struct extent_buffer *scratch_leaf = NULL;
  2164. struct ulist *roots = NULL;
  2165. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  2166. u64 num_bytes;
  2167. int slot;
  2168. int ret;
  2169. mutex_lock(&fs_info->qgroup_rescan_lock);
  2170. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2171. &fs_info->qgroup_rescan_progress,
  2172. path, 1, 0);
  2173. btrfs_debug(fs_info,
  2174. "current progress key (%llu %u %llu), search_slot ret %d",
  2175. fs_info->qgroup_rescan_progress.objectid,
  2176. fs_info->qgroup_rescan_progress.type,
  2177. fs_info->qgroup_rescan_progress.offset, ret);
  2178. if (ret) {
  2179. /*
  2180. * The rescan is about to end, we will not be scanning any
  2181. * further blocks. We cannot unset the RESCAN flag here, because
  2182. * we want to commit the transaction if everything went well.
  2183. * To make the live accounting work in this phase, we set our
  2184. * scan progress pointer such that every real extent objectid
  2185. * will be smaller.
  2186. */
  2187. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2188. btrfs_release_path(path);
  2189. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2190. return ret;
  2191. }
  2192. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2193. btrfs_header_nritems(path->nodes[0]) - 1);
  2194. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2195. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2196. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  2197. if (!scratch_leaf) {
  2198. ret = -ENOMEM;
  2199. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2200. goto out;
  2201. }
  2202. extent_buffer_get(scratch_leaf);
  2203. btrfs_tree_read_lock(scratch_leaf);
  2204. btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
  2205. slot = path->slots[0];
  2206. btrfs_release_path(path);
  2207. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2208. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2209. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2210. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2211. found.type != BTRFS_METADATA_ITEM_KEY)
  2212. continue;
  2213. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2214. num_bytes = fs_info->nodesize;
  2215. else
  2216. num_bytes = found.offset;
  2217. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2218. &roots);
  2219. if (ret < 0)
  2220. goto out;
  2221. /* For rescan, just pass old_roots as NULL */
  2222. ret = btrfs_qgroup_account_extent(trans, fs_info,
  2223. found.objectid, num_bytes, NULL, roots);
  2224. if (ret < 0)
  2225. goto out;
  2226. }
  2227. out:
  2228. if (scratch_leaf) {
  2229. btrfs_tree_read_unlock_blocking(scratch_leaf);
  2230. free_extent_buffer(scratch_leaf);
  2231. }
  2232. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2233. return ret;
  2234. }
  2235. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2236. {
  2237. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2238. qgroup_rescan_work);
  2239. struct btrfs_path *path;
  2240. struct btrfs_trans_handle *trans = NULL;
  2241. int err = -ENOMEM;
  2242. int ret = 0;
  2243. path = btrfs_alloc_path();
  2244. if (!path)
  2245. goto out;
  2246. err = 0;
  2247. while (!err && !btrfs_fs_closing(fs_info)) {
  2248. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2249. if (IS_ERR(trans)) {
  2250. err = PTR_ERR(trans);
  2251. break;
  2252. }
  2253. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  2254. err = -EINTR;
  2255. } else {
  2256. err = qgroup_rescan_leaf(fs_info, path, trans);
  2257. }
  2258. if (err > 0)
  2259. btrfs_commit_transaction(trans);
  2260. else
  2261. btrfs_end_transaction(trans);
  2262. }
  2263. out:
  2264. btrfs_free_path(path);
  2265. mutex_lock(&fs_info->qgroup_rescan_lock);
  2266. if (!btrfs_fs_closing(fs_info))
  2267. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2268. if (err > 0 &&
  2269. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2270. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2271. } else if (err < 0) {
  2272. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2273. }
  2274. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2275. /*
  2276. * only update status, since the previous part has already updated the
  2277. * qgroup info.
  2278. */
  2279. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2280. if (IS_ERR(trans)) {
  2281. err = PTR_ERR(trans);
  2282. btrfs_err(fs_info,
  2283. "fail to start transaction for status update: %d",
  2284. err);
  2285. goto done;
  2286. }
  2287. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2288. if (ret < 0) {
  2289. err = ret;
  2290. btrfs_err(fs_info, "fail to update qgroup status: %d", err);
  2291. }
  2292. btrfs_end_transaction(trans);
  2293. if (btrfs_fs_closing(fs_info)) {
  2294. btrfs_info(fs_info, "qgroup scan paused");
  2295. } else if (err >= 0) {
  2296. btrfs_info(fs_info, "qgroup scan completed%s",
  2297. err > 0 ? " (inconsistency flag cleared)" : "");
  2298. } else {
  2299. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2300. }
  2301. done:
  2302. mutex_lock(&fs_info->qgroup_rescan_lock);
  2303. fs_info->qgroup_rescan_running = false;
  2304. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2305. complete_all(&fs_info->qgroup_rescan_completion);
  2306. }
  2307. /*
  2308. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2309. * memory required for the rescan context.
  2310. */
  2311. static int
  2312. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2313. int init_flags)
  2314. {
  2315. int ret = 0;
  2316. if (!init_flags &&
  2317. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2318. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2319. ret = -EINVAL;
  2320. goto err;
  2321. }
  2322. mutex_lock(&fs_info->qgroup_rescan_lock);
  2323. spin_lock(&fs_info->qgroup_lock);
  2324. if (init_flags) {
  2325. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2326. ret = -EINPROGRESS;
  2327. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2328. ret = -EINVAL;
  2329. if (ret) {
  2330. spin_unlock(&fs_info->qgroup_lock);
  2331. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2332. goto err;
  2333. }
  2334. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2335. }
  2336. memset(&fs_info->qgroup_rescan_progress, 0,
  2337. sizeof(fs_info->qgroup_rescan_progress));
  2338. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2339. init_completion(&fs_info->qgroup_rescan_completion);
  2340. fs_info->qgroup_rescan_running = true;
  2341. spin_unlock(&fs_info->qgroup_lock);
  2342. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2343. memset(&fs_info->qgroup_rescan_work, 0,
  2344. sizeof(fs_info->qgroup_rescan_work));
  2345. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2346. btrfs_qgroup_rescan_helper,
  2347. btrfs_qgroup_rescan_worker, NULL, NULL);
  2348. if (ret) {
  2349. err:
  2350. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2351. return ret;
  2352. }
  2353. return 0;
  2354. }
  2355. static void
  2356. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2357. {
  2358. struct rb_node *n;
  2359. struct btrfs_qgroup *qgroup;
  2360. spin_lock(&fs_info->qgroup_lock);
  2361. /* clear all current qgroup tracking information */
  2362. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2363. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2364. qgroup->rfer = 0;
  2365. qgroup->rfer_cmpr = 0;
  2366. qgroup->excl = 0;
  2367. qgroup->excl_cmpr = 0;
  2368. }
  2369. spin_unlock(&fs_info->qgroup_lock);
  2370. }
  2371. int
  2372. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2373. {
  2374. int ret = 0;
  2375. struct btrfs_trans_handle *trans;
  2376. ret = qgroup_rescan_init(fs_info, 0, 1);
  2377. if (ret)
  2378. return ret;
  2379. /*
  2380. * We have set the rescan_progress to 0, which means no more
  2381. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2382. * However, btrfs_qgroup_account_ref may be right after its call
  2383. * to btrfs_find_all_roots, in which case it would still do the
  2384. * accounting.
  2385. * To solve this, we're committing the transaction, which will
  2386. * ensure we run all delayed refs and only after that, we are
  2387. * going to clear all tracking information for a clean start.
  2388. */
  2389. trans = btrfs_join_transaction(fs_info->fs_root);
  2390. if (IS_ERR(trans)) {
  2391. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2392. return PTR_ERR(trans);
  2393. }
  2394. ret = btrfs_commit_transaction(trans);
  2395. if (ret) {
  2396. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2397. return ret;
  2398. }
  2399. qgroup_rescan_zero_tracking(fs_info);
  2400. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2401. &fs_info->qgroup_rescan_work);
  2402. return 0;
  2403. }
  2404. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  2405. bool interruptible)
  2406. {
  2407. int running;
  2408. int ret = 0;
  2409. mutex_lock(&fs_info->qgroup_rescan_lock);
  2410. spin_lock(&fs_info->qgroup_lock);
  2411. running = fs_info->qgroup_rescan_running;
  2412. spin_unlock(&fs_info->qgroup_lock);
  2413. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2414. if (!running)
  2415. return 0;
  2416. if (interruptible)
  2417. ret = wait_for_completion_interruptible(
  2418. &fs_info->qgroup_rescan_completion);
  2419. else
  2420. wait_for_completion(&fs_info->qgroup_rescan_completion);
  2421. return ret;
  2422. }
  2423. /*
  2424. * this is only called from open_ctree where we're still single threaded, thus
  2425. * locking is omitted here.
  2426. */
  2427. void
  2428. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2429. {
  2430. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2431. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2432. &fs_info->qgroup_rescan_work);
  2433. }
  2434. /*
  2435. * Reserve qgroup space for range [start, start + len).
  2436. *
  2437. * This function will either reserve space from related qgroups or doing
  2438. * nothing if the range is already reserved.
  2439. *
  2440. * Return 0 for successful reserve
  2441. * Return <0 for error (including -EQUOT)
  2442. *
  2443. * NOTE: this function may sleep for memory allocation.
  2444. * if btrfs_qgroup_reserve_data() is called multiple times with
  2445. * same @reserved, caller must ensure when error happens it's OK
  2446. * to free *ALL* reserved space.
  2447. */
  2448. int btrfs_qgroup_reserve_data(struct inode *inode,
  2449. struct extent_changeset **reserved_ret, u64 start,
  2450. u64 len)
  2451. {
  2452. struct btrfs_root *root = BTRFS_I(inode)->root;
  2453. struct ulist_node *unode;
  2454. struct ulist_iterator uiter;
  2455. struct extent_changeset *reserved;
  2456. u64 orig_reserved;
  2457. u64 to_reserve;
  2458. int ret;
  2459. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2460. !is_fstree(root->objectid) || len == 0)
  2461. return 0;
  2462. /* @reserved parameter is mandatory for qgroup */
  2463. if (WARN_ON(!reserved_ret))
  2464. return -EINVAL;
  2465. if (!*reserved_ret) {
  2466. *reserved_ret = extent_changeset_alloc();
  2467. if (!*reserved_ret)
  2468. return -ENOMEM;
  2469. }
  2470. reserved = *reserved_ret;
  2471. /* Record already reserved space */
  2472. orig_reserved = reserved->bytes_changed;
  2473. ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2474. start + len -1, EXTENT_QGROUP_RESERVED, reserved);
  2475. /* Newly reserved space */
  2476. to_reserve = reserved->bytes_changed - orig_reserved;
  2477. trace_btrfs_qgroup_reserve_data(inode, start, len,
  2478. to_reserve, QGROUP_RESERVE);
  2479. if (ret < 0)
  2480. goto cleanup;
  2481. ret = qgroup_reserve(root, to_reserve, true);
  2482. if (ret < 0)
  2483. goto cleanup;
  2484. return ret;
  2485. cleanup:
  2486. /* cleanup *ALL* already reserved ranges */
  2487. ULIST_ITER_INIT(&uiter);
  2488. while ((unode = ulist_next(&reserved->range_changed, &uiter)))
  2489. clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
  2490. unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
  2491. GFP_NOFS);
  2492. extent_changeset_release(reserved);
  2493. return ret;
  2494. }
  2495. /* Free ranges specified by @reserved, normally in error path */
  2496. static int qgroup_free_reserved_data(struct inode *inode,
  2497. struct extent_changeset *reserved, u64 start, u64 len)
  2498. {
  2499. struct btrfs_root *root = BTRFS_I(inode)->root;
  2500. struct ulist_node *unode;
  2501. struct ulist_iterator uiter;
  2502. struct extent_changeset changeset;
  2503. int freed = 0;
  2504. int ret;
  2505. extent_changeset_init(&changeset);
  2506. len = round_up(start + len, root->fs_info->sectorsize);
  2507. start = round_down(start, root->fs_info->sectorsize);
  2508. ULIST_ITER_INIT(&uiter);
  2509. while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
  2510. u64 range_start = unode->val;
  2511. /* unode->aux is the inclusive end */
  2512. u64 range_len = unode->aux - range_start + 1;
  2513. u64 free_start;
  2514. u64 free_len;
  2515. extent_changeset_release(&changeset);
  2516. /* Only free range in range [start, start + len) */
  2517. if (range_start >= start + len ||
  2518. range_start + range_len <= start)
  2519. continue;
  2520. free_start = max(range_start, start);
  2521. free_len = min(start + len, range_start + range_len) -
  2522. free_start;
  2523. /*
  2524. * TODO: To also modify reserved->ranges_reserved to reflect
  2525. * the modification.
  2526. *
  2527. * However as long as we free qgroup reserved according to
  2528. * EXTENT_QGROUP_RESERVED, we won't double free.
  2529. * So not need to rush.
  2530. */
  2531. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  2532. free_start, free_start + free_len - 1,
  2533. EXTENT_QGROUP_RESERVED, &changeset);
  2534. if (ret < 0)
  2535. goto out;
  2536. freed += changeset.bytes_changed;
  2537. }
  2538. btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed);
  2539. ret = freed;
  2540. out:
  2541. extent_changeset_release(&changeset);
  2542. return ret;
  2543. }
  2544. static int __btrfs_qgroup_release_data(struct inode *inode,
  2545. struct extent_changeset *reserved, u64 start, u64 len,
  2546. int free)
  2547. {
  2548. struct extent_changeset changeset;
  2549. int trace_op = QGROUP_RELEASE;
  2550. int ret;
  2551. /* In release case, we shouldn't have @reserved */
  2552. WARN_ON(!free && reserved);
  2553. if (free && reserved)
  2554. return qgroup_free_reserved_data(inode, reserved, start, len);
  2555. extent_changeset_init(&changeset);
  2556. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2557. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2558. if (ret < 0)
  2559. goto out;
  2560. if (free)
  2561. trace_op = QGROUP_FREE;
  2562. trace_btrfs_qgroup_release_data(inode, start, len,
  2563. changeset.bytes_changed, trace_op);
  2564. if (free)
  2565. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2566. BTRFS_I(inode)->root->objectid,
  2567. changeset.bytes_changed);
  2568. ret = changeset.bytes_changed;
  2569. out:
  2570. extent_changeset_release(&changeset);
  2571. return ret;
  2572. }
  2573. /*
  2574. * Free a reserved space range from io_tree and related qgroups
  2575. *
  2576. * Should be called when a range of pages get invalidated before reaching disk.
  2577. * Or for error cleanup case.
  2578. * if @reserved is given, only reserved range in [@start, @start + @len) will
  2579. * be freed.
  2580. *
  2581. * For data written to disk, use btrfs_qgroup_release_data().
  2582. *
  2583. * NOTE: This function may sleep for memory allocation.
  2584. */
  2585. int btrfs_qgroup_free_data(struct inode *inode,
  2586. struct extent_changeset *reserved, u64 start, u64 len)
  2587. {
  2588. return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
  2589. }
  2590. /*
  2591. * Release a reserved space range from io_tree only.
  2592. *
  2593. * Should be called when a range of pages get written to disk and corresponding
  2594. * FILE_EXTENT is inserted into corresponding root.
  2595. *
  2596. * Since new qgroup accounting framework will only update qgroup numbers at
  2597. * commit_transaction() time, its reserved space shouldn't be freed from
  2598. * related qgroups.
  2599. *
  2600. * But we should release the range from io_tree, to allow further write to be
  2601. * COWed.
  2602. *
  2603. * NOTE: This function may sleep for memory allocation.
  2604. */
  2605. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
  2606. {
  2607. return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
  2608. }
  2609. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  2610. bool enforce)
  2611. {
  2612. struct btrfs_fs_info *fs_info = root->fs_info;
  2613. int ret;
  2614. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2615. !is_fstree(root->objectid) || num_bytes == 0)
  2616. return 0;
  2617. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2618. trace_qgroup_meta_reserve(root, (s64)num_bytes);
  2619. ret = qgroup_reserve(root, num_bytes, enforce);
  2620. if (ret < 0)
  2621. return ret;
  2622. atomic64_add(num_bytes, &root->qgroup_meta_rsv);
  2623. return ret;
  2624. }
  2625. void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
  2626. {
  2627. struct btrfs_fs_info *fs_info = root->fs_info;
  2628. u64 reserved;
  2629. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2630. !is_fstree(root->objectid))
  2631. return;
  2632. reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
  2633. if (reserved == 0)
  2634. return;
  2635. trace_qgroup_meta_reserve(root, -(s64)reserved);
  2636. btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
  2637. }
  2638. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
  2639. {
  2640. struct btrfs_fs_info *fs_info = root->fs_info;
  2641. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2642. !is_fstree(root->objectid))
  2643. return;
  2644. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2645. WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
  2646. atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
  2647. trace_qgroup_meta_reserve(root, -(s64)num_bytes);
  2648. btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
  2649. }
  2650. /*
  2651. * Check qgroup reserved space leaking, normally at destroy inode
  2652. * time
  2653. */
  2654. void btrfs_qgroup_check_reserved_leak(struct inode *inode)
  2655. {
  2656. struct extent_changeset changeset;
  2657. struct ulist_node *unode;
  2658. struct ulist_iterator iter;
  2659. int ret;
  2660. extent_changeset_init(&changeset);
  2661. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  2662. EXTENT_QGROUP_RESERVED, &changeset);
  2663. WARN_ON(ret < 0);
  2664. if (WARN_ON(changeset.bytes_changed)) {
  2665. ULIST_ITER_INIT(&iter);
  2666. while ((unode = ulist_next(&changeset.range_changed, &iter))) {
  2667. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2668. "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
  2669. inode->i_ino, unode->val, unode->aux);
  2670. }
  2671. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2672. BTRFS_I(inode)->root->objectid,
  2673. changeset.bytes_changed);
  2674. }
  2675. extent_changeset_release(&changeset);
  2676. }