qgroup.c 67 KB

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