qgroup.c 85 KB

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