qgroup.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826
  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. */
  79. u64 old_refcnt;
  80. u64 new_refcnt;
  81. };
  82. /*
  83. * glue structure to represent the relations between qgroups.
  84. */
  85. struct btrfs_qgroup_list {
  86. struct list_head next_group;
  87. struct list_head next_member;
  88. struct btrfs_qgroup *group;
  89. struct btrfs_qgroup *member;
  90. };
  91. #define ptr_to_u64(x) ((u64)(uintptr_t)x)
  92. #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
  93. static int
  94. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  95. int init_flags);
  96. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  97. /* must be called with qgroup_ioctl_lock held */
  98. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  99. u64 qgroupid)
  100. {
  101. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  102. struct btrfs_qgroup *qgroup;
  103. while (n) {
  104. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  105. if (qgroup->qgroupid < qgroupid)
  106. n = n->rb_left;
  107. else if (qgroup->qgroupid > qgroupid)
  108. n = n->rb_right;
  109. else
  110. return qgroup;
  111. }
  112. return NULL;
  113. }
  114. /* must be called with qgroup_lock held */
  115. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  116. u64 qgroupid)
  117. {
  118. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  119. struct rb_node *parent = NULL;
  120. struct btrfs_qgroup *qgroup;
  121. while (*p) {
  122. parent = *p;
  123. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  124. if (qgroup->qgroupid < qgroupid)
  125. p = &(*p)->rb_left;
  126. else if (qgroup->qgroupid > qgroupid)
  127. p = &(*p)->rb_right;
  128. else
  129. return qgroup;
  130. }
  131. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  132. if (!qgroup)
  133. return ERR_PTR(-ENOMEM);
  134. qgroup->qgroupid = qgroupid;
  135. INIT_LIST_HEAD(&qgroup->groups);
  136. INIT_LIST_HEAD(&qgroup->members);
  137. INIT_LIST_HEAD(&qgroup->dirty);
  138. rb_link_node(&qgroup->node, parent, p);
  139. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  140. return qgroup;
  141. }
  142. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  143. {
  144. struct btrfs_qgroup_list *list;
  145. list_del(&qgroup->dirty);
  146. while (!list_empty(&qgroup->groups)) {
  147. list = list_first_entry(&qgroup->groups,
  148. struct btrfs_qgroup_list, next_group);
  149. list_del(&list->next_group);
  150. list_del(&list->next_member);
  151. kfree(list);
  152. }
  153. while (!list_empty(&qgroup->members)) {
  154. list = list_first_entry(&qgroup->members,
  155. struct btrfs_qgroup_list, next_member);
  156. list_del(&list->next_group);
  157. list_del(&list->next_member);
  158. kfree(list);
  159. }
  160. kfree(qgroup);
  161. }
  162. /* must be called with qgroup_lock held */
  163. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  164. {
  165. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  166. if (!qgroup)
  167. return -ENOENT;
  168. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  169. __del_qgroup_rb(qgroup);
  170. return 0;
  171. }
  172. /* must be called with qgroup_lock held */
  173. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  174. u64 memberid, u64 parentid)
  175. {
  176. struct btrfs_qgroup *member;
  177. struct btrfs_qgroup *parent;
  178. struct btrfs_qgroup_list *list;
  179. member = find_qgroup_rb(fs_info, memberid);
  180. parent = find_qgroup_rb(fs_info, parentid);
  181. if (!member || !parent)
  182. return -ENOENT;
  183. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  184. if (!list)
  185. return -ENOMEM;
  186. list->group = parent;
  187. list->member = member;
  188. list_add_tail(&list->next_group, &member->groups);
  189. list_add_tail(&list->next_member, &parent->members);
  190. return 0;
  191. }
  192. /* must be called with qgroup_lock held */
  193. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  194. u64 memberid, u64 parentid)
  195. {
  196. struct btrfs_qgroup *member;
  197. struct btrfs_qgroup *parent;
  198. struct btrfs_qgroup_list *list;
  199. member = find_qgroup_rb(fs_info, memberid);
  200. parent = find_qgroup_rb(fs_info, parentid);
  201. if (!member || !parent)
  202. return -ENOENT;
  203. list_for_each_entry(list, &member->groups, next_group) {
  204. if (list->group == parent) {
  205. list_del(&list->next_group);
  206. list_del(&list->next_member);
  207. kfree(list);
  208. return 0;
  209. }
  210. }
  211. return -ENOENT;
  212. }
  213. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  214. int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
  215. u64 rfer, u64 excl)
  216. {
  217. struct btrfs_qgroup *qgroup;
  218. qgroup = find_qgroup_rb(fs_info, qgroupid);
  219. if (!qgroup)
  220. return -EINVAL;
  221. if (qgroup->rfer != rfer || qgroup->excl != excl)
  222. return -EINVAL;
  223. return 0;
  224. }
  225. #endif
  226. /*
  227. * The full config is read in one go, only called from open_ctree()
  228. * It doesn't use any locking, as at this point we're still single-threaded
  229. */
  230. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  231. {
  232. struct btrfs_key key;
  233. struct btrfs_key found_key;
  234. struct btrfs_root *quota_root = fs_info->quota_root;
  235. struct btrfs_path *path = NULL;
  236. struct extent_buffer *l;
  237. int slot;
  238. int ret = 0;
  239. u64 flags = 0;
  240. u64 rescan_progress = 0;
  241. if (!fs_info->quota_enabled)
  242. return 0;
  243. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  244. if (!fs_info->qgroup_ulist) {
  245. ret = -ENOMEM;
  246. goto out;
  247. }
  248. path = btrfs_alloc_path();
  249. if (!path) {
  250. ret = -ENOMEM;
  251. goto out;
  252. }
  253. /* default this to quota off, in case no status key is found */
  254. fs_info->qgroup_flags = 0;
  255. /*
  256. * pass 1: read status, all qgroup infos and limits
  257. */
  258. key.objectid = 0;
  259. key.type = 0;
  260. key.offset = 0;
  261. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  262. if (ret)
  263. goto out;
  264. while (1) {
  265. struct btrfs_qgroup *qgroup;
  266. slot = path->slots[0];
  267. l = path->nodes[0];
  268. btrfs_item_key_to_cpu(l, &found_key, slot);
  269. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  270. struct btrfs_qgroup_status_item *ptr;
  271. ptr = btrfs_item_ptr(l, slot,
  272. struct btrfs_qgroup_status_item);
  273. if (btrfs_qgroup_status_version(l, ptr) !=
  274. BTRFS_QGROUP_STATUS_VERSION) {
  275. btrfs_err(fs_info,
  276. "old qgroup version, quota disabled");
  277. goto out;
  278. }
  279. if (btrfs_qgroup_status_generation(l, ptr) !=
  280. fs_info->generation) {
  281. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  282. btrfs_err(fs_info,
  283. "qgroup generation mismatch, "
  284. "marked as inconsistent");
  285. }
  286. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  287. ptr);
  288. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  289. goto next1;
  290. }
  291. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  292. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  293. goto next1;
  294. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  295. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  296. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  297. btrfs_err(fs_info, "inconsitent qgroup config");
  298. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  299. }
  300. if (!qgroup) {
  301. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  302. if (IS_ERR(qgroup)) {
  303. ret = PTR_ERR(qgroup);
  304. goto out;
  305. }
  306. }
  307. switch (found_key.type) {
  308. case BTRFS_QGROUP_INFO_KEY: {
  309. struct btrfs_qgroup_info_item *ptr;
  310. ptr = btrfs_item_ptr(l, slot,
  311. struct btrfs_qgroup_info_item);
  312. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  313. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  314. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  315. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  316. /* generation currently unused */
  317. break;
  318. }
  319. case BTRFS_QGROUP_LIMIT_KEY: {
  320. struct btrfs_qgroup_limit_item *ptr;
  321. ptr = btrfs_item_ptr(l, slot,
  322. struct btrfs_qgroup_limit_item);
  323. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  324. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  325. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  326. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  327. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  328. break;
  329. }
  330. }
  331. next1:
  332. ret = btrfs_next_item(quota_root, path);
  333. if (ret < 0)
  334. goto out;
  335. if (ret)
  336. break;
  337. }
  338. btrfs_release_path(path);
  339. /*
  340. * pass 2: read all qgroup relations
  341. */
  342. key.objectid = 0;
  343. key.type = BTRFS_QGROUP_RELATION_KEY;
  344. key.offset = 0;
  345. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  346. if (ret)
  347. goto out;
  348. while (1) {
  349. slot = path->slots[0];
  350. l = path->nodes[0];
  351. btrfs_item_key_to_cpu(l, &found_key, slot);
  352. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  353. goto next2;
  354. if (found_key.objectid > found_key.offset) {
  355. /* parent <- member, not needed to build config */
  356. /* FIXME should we omit the key completely? */
  357. goto next2;
  358. }
  359. ret = add_relation_rb(fs_info, found_key.objectid,
  360. found_key.offset);
  361. if (ret == -ENOENT) {
  362. btrfs_warn(fs_info,
  363. "orphan qgroup relation 0x%llx->0x%llx",
  364. found_key.objectid, found_key.offset);
  365. ret = 0; /* ignore the error */
  366. }
  367. if (ret)
  368. goto out;
  369. next2:
  370. ret = btrfs_next_item(quota_root, path);
  371. if (ret < 0)
  372. goto out;
  373. if (ret)
  374. break;
  375. }
  376. out:
  377. fs_info->qgroup_flags |= flags;
  378. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  379. fs_info->quota_enabled = 0;
  380. fs_info->pending_quota_state = 0;
  381. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  382. ret >= 0) {
  383. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  384. }
  385. btrfs_free_path(path);
  386. if (ret < 0) {
  387. ulist_free(fs_info->qgroup_ulist);
  388. fs_info->qgroup_ulist = NULL;
  389. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  390. }
  391. return ret < 0 ? ret : 0;
  392. }
  393. /*
  394. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  395. * first two are in single-threaded paths.And for the third one, we have set
  396. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  397. * up the in-memory structures without qgroup_lock held.
  398. */
  399. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  400. {
  401. struct rb_node *n;
  402. struct btrfs_qgroup *qgroup;
  403. while ((n = rb_first(&fs_info->qgroup_tree))) {
  404. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  405. rb_erase(n, &fs_info->qgroup_tree);
  406. __del_qgroup_rb(qgroup);
  407. }
  408. /*
  409. * we call btrfs_free_qgroup_config() when umounting
  410. * filesystem and disabling quota, so we set qgroup_ulit
  411. * to be null here to avoid double free.
  412. */
  413. ulist_free(fs_info->qgroup_ulist);
  414. fs_info->qgroup_ulist = NULL;
  415. }
  416. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  417. struct btrfs_root *quota_root,
  418. u64 src, u64 dst)
  419. {
  420. int ret;
  421. struct btrfs_path *path;
  422. struct btrfs_key key;
  423. path = btrfs_alloc_path();
  424. if (!path)
  425. return -ENOMEM;
  426. key.objectid = src;
  427. key.type = BTRFS_QGROUP_RELATION_KEY;
  428. key.offset = dst;
  429. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  430. btrfs_mark_buffer_dirty(path->nodes[0]);
  431. btrfs_free_path(path);
  432. return ret;
  433. }
  434. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  435. struct btrfs_root *quota_root,
  436. u64 src, u64 dst)
  437. {
  438. int ret;
  439. struct btrfs_path *path;
  440. struct btrfs_key key;
  441. path = btrfs_alloc_path();
  442. if (!path)
  443. return -ENOMEM;
  444. key.objectid = src;
  445. key.type = BTRFS_QGROUP_RELATION_KEY;
  446. key.offset = dst;
  447. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  448. if (ret < 0)
  449. goto out;
  450. if (ret > 0) {
  451. ret = -ENOENT;
  452. goto out;
  453. }
  454. ret = btrfs_del_item(trans, quota_root, path);
  455. out:
  456. btrfs_free_path(path);
  457. return ret;
  458. }
  459. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  460. struct btrfs_root *quota_root, u64 qgroupid)
  461. {
  462. int ret;
  463. struct btrfs_path *path;
  464. struct btrfs_qgroup_info_item *qgroup_info;
  465. struct btrfs_qgroup_limit_item *qgroup_limit;
  466. struct extent_buffer *leaf;
  467. struct btrfs_key key;
  468. if (btrfs_test_is_dummy_root(quota_root))
  469. return 0;
  470. path = btrfs_alloc_path();
  471. if (!path)
  472. return -ENOMEM;
  473. key.objectid = 0;
  474. key.type = BTRFS_QGROUP_INFO_KEY;
  475. key.offset = qgroupid;
  476. /*
  477. * Avoid a transaction abort by catching -EEXIST here. In that
  478. * case, we proceed by re-initializing the existing structure
  479. * on disk.
  480. */
  481. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  482. sizeof(*qgroup_info));
  483. if (ret && ret != -EEXIST)
  484. goto out;
  485. leaf = path->nodes[0];
  486. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  487. struct btrfs_qgroup_info_item);
  488. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  489. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  490. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  491. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  492. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  493. btrfs_mark_buffer_dirty(leaf);
  494. btrfs_release_path(path);
  495. key.type = BTRFS_QGROUP_LIMIT_KEY;
  496. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  497. sizeof(*qgroup_limit));
  498. if (ret && ret != -EEXIST)
  499. goto out;
  500. leaf = path->nodes[0];
  501. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  502. struct btrfs_qgroup_limit_item);
  503. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  504. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  505. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  506. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  507. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  508. btrfs_mark_buffer_dirty(leaf);
  509. ret = 0;
  510. out:
  511. btrfs_free_path(path);
  512. return ret;
  513. }
  514. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  515. struct btrfs_root *quota_root, u64 qgroupid)
  516. {
  517. int ret;
  518. struct btrfs_path *path;
  519. struct btrfs_key key;
  520. path = btrfs_alloc_path();
  521. if (!path)
  522. return -ENOMEM;
  523. key.objectid = 0;
  524. key.type = BTRFS_QGROUP_INFO_KEY;
  525. key.offset = qgroupid;
  526. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  527. if (ret < 0)
  528. goto out;
  529. if (ret > 0) {
  530. ret = -ENOENT;
  531. goto out;
  532. }
  533. ret = btrfs_del_item(trans, quota_root, path);
  534. if (ret)
  535. goto out;
  536. btrfs_release_path(path);
  537. key.type = BTRFS_QGROUP_LIMIT_KEY;
  538. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  539. if (ret < 0)
  540. goto out;
  541. if (ret > 0) {
  542. ret = -ENOENT;
  543. goto out;
  544. }
  545. ret = btrfs_del_item(trans, quota_root, path);
  546. out:
  547. btrfs_free_path(path);
  548. return ret;
  549. }
  550. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  551. struct btrfs_root *root, u64 qgroupid,
  552. u64 flags, u64 max_rfer, u64 max_excl,
  553. u64 rsv_rfer, u64 rsv_excl)
  554. {
  555. struct btrfs_path *path;
  556. struct btrfs_key key;
  557. struct extent_buffer *l;
  558. struct btrfs_qgroup_limit_item *qgroup_limit;
  559. int ret;
  560. int slot;
  561. key.objectid = 0;
  562. key.type = BTRFS_QGROUP_LIMIT_KEY;
  563. key.offset = qgroupid;
  564. path = btrfs_alloc_path();
  565. if (!path)
  566. return -ENOMEM;
  567. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  568. if (ret > 0)
  569. ret = -ENOENT;
  570. if (ret)
  571. goto out;
  572. l = path->nodes[0];
  573. slot = path->slots[0];
  574. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  575. btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
  576. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
  577. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
  578. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
  579. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
  580. btrfs_mark_buffer_dirty(l);
  581. out:
  582. btrfs_free_path(path);
  583. return ret;
  584. }
  585. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  586. struct btrfs_root *root,
  587. struct btrfs_qgroup *qgroup)
  588. {
  589. struct btrfs_path *path;
  590. struct btrfs_key key;
  591. struct extent_buffer *l;
  592. struct btrfs_qgroup_info_item *qgroup_info;
  593. int ret;
  594. int slot;
  595. if (btrfs_test_is_dummy_root(root))
  596. return 0;
  597. key.objectid = 0;
  598. key.type = BTRFS_QGROUP_INFO_KEY;
  599. key.offset = qgroup->qgroupid;
  600. path = btrfs_alloc_path();
  601. if (!path)
  602. return -ENOMEM;
  603. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  604. if (ret > 0)
  605. ret = -ENOENT;
  606. if (ret)
  607. goto out;
  608. l = path->nodes[0];
  609. slot = path->slots[0];
  610. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  611. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  612. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  613. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  614. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  615. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  616. btrfs_mark_buffer_dirty(l);
  617. out:
  618. btrfs_free_path(path);
  619. return ret;
  620. }
  621. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  622. struct btrfs_fs_info *fs_info,
  623. struct btrfs_root *root)
  624. {
  625. struct btrfs_path *path;
  626. struct btrfs_key key;
  627. struct extent_buffer *l;
  628. struct btrfs_qgroup_status_item *ptr;
  629. int ret;
  630. int slot;
  631. key.objectid = 0;
  632. key.type = BTRFS_QGROUP_STATUS_KEY;
  633. key.offset = 0;
  634. path = btrfs_alloc_path();
  635. if (!path)
  636. return -ENOMEM;
  637. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  638. if (ret > 0)
  639. ret = -ENOENT;
  640. if (ret)
  641. goto out;
  642. l = path->nodes[0];
  643. slot = path->slots[0];
  644. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  645. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  646. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  647. btrfs_set_qgroup_status_rescan(l, ptr,
  648. fs_info->qgroup_rescan_progress.objectid);
  649. btrfs_mark_buffer_dirty(l);
  650. out:
  651. btrfs_free_path(path);
  652. return ret;
  653. }
  654. /*
  655. * called with qgroup_lock held
  656. */
  657. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  658. struct btrfs_root *root)
  659. {
  660. struct btrfs_path *path;
  661. struct btrfs_key key;
  662. struct extent_buffer *leaf = NULL;
  663. int ret;
  664. int nr = 0;
  665. path = btrfs_alloc_path();
  666. if (!path)
  667. return -ENOMEM;
  668. path->leave_spinning = 1;
  669. key.objectid = 0;
  670. key.offset = 0;
  671. key.type = 0;
  672. while (1) {
  673. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  674. if (ret < 0)
  675. goto out;
  676. leaf = path->nodes[0];
  677. nr = btrfs_header_nritems(leaf);
  678. if (!nr)
  679. break;
  680. /*
  681. * delete the leaf one by one
  682. * since the whole tree is going
  683. * to be deleted.
  684. */
  685. path->slots[0] = 0;
  686. ret = btrfs_del_items(trans, root, path, 0, nr);
  687. if (ret)
  688. goto out;
  689. btrfs_release_path(path);
  690. }
  691. ret = 0;
  692. out:
  693. root->fs_info->pending_quota_state = 0;
  694. btrfs_free_path(path);
  695. return ret;
  696. }
  697. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  698. struct btrfs_fs_info *fs_info)
  699. {
  700. struct btrfs_root *quota_root;
  701. struct btrfs_root *tree_root = fs_info->tree_root;
  702. struct btrfs_path *path = NULL;
  703. struct btrfs_qgroup_status_item *ptr;
  704. struct extent_buffer *leaf;
  705. struct btrfs_key key;
  706. struct btrfs_key found_key;
  707. struct btrfs_qgroup *qgroup = NULL;
  708. int ret = 0;
  709. int slot;
  710. mutex_lock(&fs_info->qgroup_ioctl_lock);
  711. if (fs_info->quota_root) {
  712. fs_info->pending_quota_state = 1;
  713. goto out;
  714. }
  715. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  716. if (!fs_info->qgroup_ulist) {
  717. ret = -ENOMEM;
  718. goto out;
  719. }
  720. /*
  721. * initially create the quota tree
  722. */
  723. quota_root = btrfs_create_tree(trans, fs_info,
  724. BTRFS_QUOTA_TREE_OBJECTID);
  725. if (IS_ERR(quota_root)) {
  726. ret = PTR_ERR(quota_root);
  727. goto out;
  728. }
  729. path = btrfs_alloc_path();
  730. if (!path) {
  731. ret = -ENOMEM;
  732. goto out_free_root;
  733. }
  734. key.objectid = 0;
  735. key.type = BTRFS_QGROUP_STATUS_KEY;
  736. key.offset = 0;
  737. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  738. sizeof(*ptr));
  739. if (ret)
  740. goto out_free_path;
  741. leaf = path->nodes[0];
  742. ptr = btrfs_item_ptr(leaf, path->slots[0],
  743. struct btrfs_qgroup_status_item);
  744. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  745. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  746. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  747. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  748. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  749. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  750. btrfs_mark_buffer_dirty(leaf);
  751. key.objectid = 0;
  752. key.type = BTRFS_ROOT_REF_KEY;
  753. key.offset = 0;
  754. btrfs_release_path(path);
  755. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  756. if (ret > 0)
  757. goto out_add_root;
  758. if (ret < 0)
  759. goto out_free_path;
  760. while (1) {
  761. slot = path->slots[0];
  762. leaf = path->nodes[0];
  763. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  764. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  765. ret = add_qgroup_item(trans, quota_root,
  766. found_key.offset);
  767. if (ret)
  768. goto out_free_path;
  769. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  770. if (IS_ERR(qgroup)) {
  771. ret = PTR_ERR(qgroup);
  772. goto out_free_path;
  773. }
  774. }
  775. ret = btrfs_next_item(tree_root, path);
  776. if (ret < 0)
  777. goto out_free_path;
  778. if (ret)
  779. break;
  780. }
  781. out_add_root:
  782. btrfs_release_path(path);
  783. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  784. if (ret)
  785. goto out_free_path;
  786. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  787. if (IS_ERR(qgroup)) {
  788. ret = PTR_ERR(qgroup);
  789. goto out_free_path;
  790. }
  791. spin_lock(&fs_info->qgroup_lock);
  792. fs_info->quota_root = quota_root;
  793. fs_info->pending_quota_state = 1;
  794. spin_unlock(&fs_info->qgroup_lock);
  795. out_free_path:
  796. btrfs_free_path(path);
  797. out_free_root:
  798. if (ret) {
  799. free_extent_buffer(quota_root->node);
  800. free_extent_buffer(quota_root->commit_root);
  801. kfree(quota_root);
  802. }
  803. out:
  804. if (ret) {
  805. ulist_free(fs_info->qgroup_ulist);
  806. fs_info->qgroup_ulist = NULL;
  807. }
  808. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  809. return ret;
  810. }
  811. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  812. struct btrfs_fs_info *fs_info)
  813. {
  814. struct btrfs_root *tree_root = fs_info->tree_root;
  815. struct btrfs_root *quota_root;
  816. int ret = 0;
  817. mutex_lock(&fs_info->qgroup_ioctl_lock);
  818. if (!fs_info->quota_root)
  819. goto out;
  820. spin_lock(&fs_info->qgroup_lock);
  821. fs_info->quota_enabled = 0;
  822. fs_info->pending_quota_state = 0;
  823. quota_root = fs_info->quota_root;
  824. fs_info->quota_root = NULL;
  825. spin_unlock(&fs_info->qgroup_lock);
  826. btrfs_free_qgroup_config(fs_info);
  827. ret = btrfs_clean_quota_tree(trans, quota_root);
  828. if (ret)
  829. goto out;
  830. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  831. if (ret)
  832. goto out;
  833. list_del(&quota_root->dirty_list);
  834. btrfs_tree_lock(quota_root->node);
  835. clean_tree_block(trans, tree_root, quota_root->node);
  836. btrfs_tree_unlock(quota_root->node);
  837. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  838. free_extent_buffer(quota_root->node);
  839. free_extent_buffer(quota_root->commit_root);
  840. kfree(quota_root);
  841. out:
  842. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  843. return ret;
  844. }
  845. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  846. struct btrfs_qgroup *qgroup)
  847. {
  848. if (list_empty(&qgroup->dirty))
  849. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  850. }
  851. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  852. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  853. {
  854. struct btrfs_root *quota_root;
  855. struct btrfs_qgroup *parent;
  856. struct btrfs_qgroup *member;
  857. struct btrfs_qgroup_list *list;
  858. int ret = 0;
  859. mutex_lock(&fs_info->qgroup_ioctl_lock);
  860. quota_root = fs_info->quota_root;
  861. if (!quota_root) {
  862. ret = -EINVAL;
  863. goto out;
  864. }
  865. member = find_qgroup_rb(fs_info, src);
  866. parent = find_qgroup_rb(fs_info, dst);
  867. if (!member || !parent) {
  868. ret = -EINVAL;
  869. goto out;
  870. }
  871. /* check if such qgroup relation exist firstly */
  872. list_for_each_entry(list, &member->groups, next_group) {
  873. if (list->group == parent) {
  874. ret = -EEXIST;
  875. goto out;
  876. }
  877. }
  878. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  879. if (ret)
  880. goto out;
  881. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  882. if (ret) {
  883. del_qgroup_relation_item(trans, quota_root, src, dst);
  884. goto out;
  885. }
  886. spin_lock(&fs_info->qgroup_lock);
  887. ret = add_relation_rb(quota_root->fs_info, src, dst);
  888. spin_unlock(&fs_info->qgroup_lock);
  889. out:
  890. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  891. return ret;
  892. }
  893. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  894. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  895. {
  896. struct btrfs_root *quota_root;
  897. struct btrfs_qgroup *parent;
  898. struct btrfs_qgroup *member;
  899. struct btrfs_qgroup_list *list;
  900. int ret = 0;
  901. int err;
  902. mutex_lock(&fs_info->qgroup_ioctl_lock);
  903. quota_root = fs_info->quota_root;
  904. if (!quota_root) {
  905. ret = -EINVAL;
  906. goto out;
  907. }
  908. member = find_qgroup_rb(fs_info, src);
  909. parent = find_qgroup_rb(fs_info, dst);
  910. if (!member || !parent) {
  911. ret = -EINVAL;
  912. goto out;
  913. }
  914. /* check if such qgroup relation exist firstly */
  915. list_for_each_entry(list, &member->groups, next_group) {
  916. if (list->group == parent)
  917. goto exist;
  918. }
  919. ret = -ENOENT;
  920. goto out;
  921. exist:
  922. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  923. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  924. if (err && !ret)
  925. ret = err;
  926. spin_lock(&fs_info->qgroup_lock);
  927. del_relation_rb(fs_info, src, dst);
  928. spin_unlock(&fs_info->qgroup_lock);
  929. out:
  930. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  931. return ret;
  932. }
  933. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  934. struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
  935. {
  936. struct btrfs_root *quota_root;
  937. struct btrfs_qgroup *qgroup;
  938. int ret = 0;
  939. mutex_lock(&fs_info->qgroup_ioctl_lock);
  940. quota_root = fs_info->quota_root;
  941. if (!quota_root) {
  942. ret = -EINVAL;
  943. goto out;
  944. }
  945. qgroup = find_qgroup_rb(fs_info, qgroupid);
  946. if (qgroup) {
  947. ret = -EEXIST;
  948. goto out;
  949. }
  950. ret = add_qgroup_item(trans, quota_root, qgroupid);
  951. if (ret)
  952. goto out;
  953. spin_lock(&fs_info->qgroup_lock);
  954. qgroup = add_qgroup_rb(fs_info, qgroupid);
  955. spin_unlock(&fs_info->qgroup_lock);
  956. if (IS_ERR(qgroup))
  957. ret = PTR_ERR(qgroup);
  958. out:
  959. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  960. return ret;
  961. }
  962. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  963. struct btrfs_fs_info *fs_info, u64 qgroupid)
  964. {
  965. struct btrfs_root *quota_root;
  966. struct btrfs_qgroup *qgroup;
  967. int ret = 0;
  968. mutex_lock(&fs_info->qgroup_ioctl_lock);
  969. quota_root = fs_info->quota_root;
  970. if (!quota_root) {
  971. ret = -EINVAL;
  972. goto out;
  973. }
  974. qgroup = find_qgroup_rb(fs_info, qgroupid);
  975. if (!qgroup) {
  976. ret = -ENOENT;
  977. goto out;
  978. } else {
  979. /* check if there are no relations to this qgroup */
  980. if (!list_empty(&qgroup->groups) ||
  981. !list_empty(&qgroup->members)) {
  982. ret = -EBUSY;
  983. goto out;
  984. }
  985. }
  986. ret = del_qgroup_item(trans, quota_root, qgroupid);
  987. spin_lock(&fs_info->qgroup_lock);
  988. del_qgroup_rb(quota_root->fs_info, qgroupid);
  989. spin_unlock(&fs_info->qgroup_lock);
  990. out:
  991. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  992. return ret;
  993. }
  994. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  995. struct btrfs_fs_info *fs_info, u64 qgroupid,
  996. struct btrfs_qgroup_limit *limit)
  997. {
  998. struct btrfs_root *quota_root;
  999. struct btrfs_qgroup *qgroup;
  1000. int ret = 0;
  1001. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1002. quota_root = fs_info->quota_root;
  1003. if (!quota_root) {
  1004. ret = -EINVAL;
  1005. goto out;
  1006. }
  1007. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1008. if (!qgroup) {
  1009. ret = -ENOENT;
  1010. goto out;
  1011. }
  1012. ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
  1013. limit->flags, limit->max_rfer,
  1014. limit->max_excl, limit->rsv_rfer,
  1015. limit->rsv_excl);
  1016. if (ret) {
  1017. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1018. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1019. qgroupid);
  1020. }
  1021. spin_lock(&fs_info->qgroup_lock);
  1022. qgroup->lim_flags = limit->flags;
  1023. qgroup->max_rfer = limit->max_rfer;
  1024. qgroup->max_excl = limit->max_excl;
  1025. qgroup->rsv_rfer = limit->rsv_rfer;
  1026. qgroup->rsv_excl = limit->rsv_excl;
  1027. spin_unlock(&fs_info->qgroup_lock);
  1028. out:
  1029. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1030. return ret;
  1031. }
  1032. static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
  1033. struct btrfs_qgroup_operation *oper2)
  1034. {
  1035. /*
  1036. * Ignore seq and type here, we're looking for any operation
  1037. * at all related to this extent on that root.
  1038. */
  1039. if (oper1->bytenr < oper2->bytenr)
  1040. return -1;
  1041. if (oper1->bytenr > oper2->bytenr)
  1042. return 1;
  1043. if (oper1->ref_root < oper2->ref_root)
  1044. return -1;
  1045. if (oper1->ref_root > oper2->ref_root)
  1046. return 1;
  1047. return 0;
  1048. }
  1049. static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
  1050. struct btrfs_qgroup_operation *oper)
  1051. {
  1052. struct rb_node *n;
  1053. struct btrfs_qgroup_operation *cur;
  1054. int cmp;
  1055. spin_lock(&fs_info->qgroup_op_lock);
  1056. n = fs_info->qgroup_op_tree.rb_node;
  1057. while (n) {
  1058. cur = rb_entry(n, struct btrfs_qgroup_operation, n);
  1059. cmp = comp_oper_exist(cur, oper);
  1060. if (cmp < 0) {
  1061. n = n->rb_right;
  1062. } else if (cmp) {
  1063. n = n->rb_left;
  1064. } else {
  1065. spin_unlock(&fs_info->qgroup_op_lock);
  1066. return -EEXIST;
  1067. }
  1068. }
  1069. spin_unlock(&fs_info->qgroup_op_lock);
  1070. return 0;
  1071. }
  1072. static int comp_oper(struct btrfs_qgroup_operation *oper1,
  1073. struct btrfs_qgroup_operation *oper2)
  1074. {
  1075. if (oper1->bytenr < oper2->bytenr)
  1076. return -1;
  1077. if (oper1->bytenr > oper2->bytenr)
  1078. return 1;
  1079. if (oper1->seq < oper2->seq)
  1080. return -1;
  1081. if (oper1->seq > oper2->seq)
  1082. return 1;
  1083. if (oper1->ref_root < oper2->ref_root)
  1084. return -1;
  1085. if (oper1->ref_root > oper2->ref_root)
  1086. return 1;
  1087. if (oper1->type < oper2->type)
  1088. return -1;
  1089. if (oper1->type > oper2->type)
  1090. return 1;
  1091. return 0;
  1092. }
  1093. static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
  1094. struct btrfs_qgroup_operation *oper)
  1095. {
  1096. struct rb_node **p;
  1097. struct rb_node *parent = NULL;
  1098. struct btrfs_qgroup_operation *cur;
  1099. int cmp;
  1100. spin_lock(&fs_info->qgroup_op_lock);
  1101. p = &fs_info->qgroup_op_tree.rb_node;
  1102. while (*p) {
  1103. parent = *p;
  1104. cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
  1105. cmp = comp_oper(cur, oper);
  1106. if (cmp < 0) {
  1107. p = &(*p)->rb_right;
  1108. } else if (cmp) {
  1109. p = &(*p)->rb_left;
  1110. } else {
  1111. spin_unlock(&fs_info->qgroup_op_lock);
  1112. return -EEXIST;
  1113. }
  1114. }
  1115. rb_link_node(&oper->n, parent, p);
  1116. rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
  1117. spin_unlock(&fs_info->qgroup_op_lock);
  1118. return 0;
  1119. }
  1120. /*
  1121. * Record a quota operation for processing later on.
  1122. * @trans: the transaction we are adding the delayed op to.
  1123. * @fs_info: the fs_info for this fs.
  1124. * @ref_root: the root of the reference we are acting on,
  1125. * @bytenr: the bytenr we are acting on.
  1126. * @num_bytes: the number of bytes in the reference.
  1127. * @type: the type of operation this is.
  1128. * @mod_seq: do we need to get a sequence number for looking up roots.
  1129. *
  1130. * We just add it to our trans qgroup_ref_list and carry on and process these
  1131. * operations in order at some later point. If the reference root isn't a fs
  1132. * root then we don't bother with doing anything.
  1133. *
  1134. * MUST BE HOLDING THE REF LOCK.
  1135. */
  1136. int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
  1137. struct btrfs_fs_info *fs_info, u64 ref_root,
  1138. u64 bytenr, u64 num_bytes,
  1139. enum btrfs_qgroup_operation_type type, int mod_seq)
  1140. {
  1141. struct btrfs_qgroup_operation *oper;
  1142. int ret;
  1143. if (!is_fstree(ref_root) || !fs_info->quota_enabled)
  1144. return 0;
  1145. oper = kmalloc(sizeof(*oper), GFP_NOFS);
  1146. if (!oper)
  1147. return -ENOMEM;
  1148. oper->ref_root = ref_root;
  1149. oper->bytenr = bytenr;
  1150. oper->num_bytes = num_bytes;
  1151. oper->type = type;
  1152. oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
  1153. INIT_LIST_HEAD(&oper->elem.list);
  1154. oper->elem.seq = 0;
  1155. trace_btrfs_qgroup_record_ref(oper);
  1156. if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
  1157. /*
  1158. * If any operation for this bytenr/ref_root combo
  1159. * exists, then we know it's not exclusively owned and
  1160. * shouldn't be queued up.
  1161. *
  1162. * This also catches the case where we have a cloned
  1163. * extent that gets queued up multiple times during
  1164. * drop snapshot.
  1165. */
  1166. if (qgroup_oper_exists(fs_info, oper)) {
  1167. kfree(oper);
  1168. return 0;
  1169. }
  1170. }
  1171. ret = insert_qgroup_oper(fs_info, oper);
  1172. if (ret) {
  1173. /* Shouldn't happen so have an assert for developers */
  1174. ASSERT(0);
  1175. kfree(oper);
  1176. return ret;
  1177. }
  1178. list_add_tail(&oper->list, &trans->qgroup_ref_list);
  1179. if (mod_seq)
  1180. btrfs_get_tree_mod_seq(fs_info, &oper->elem);
  1181. return 0;
  1182. }
  1183. /*
  1184. * The easy accounting, if we are adding/removing the only ref for an extent
  1185. * then this qgroup and all of the parent qgroups get their refrence and
  1186. * exclusive counts adjusted.
  1187. */
  1188. static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  1189. struct btrfs_qgroup_operation *oper)
  1190. {
  1191. struct btrfs_qgroup *qgroup;
  1192. struct ulist *tmp;
  1193. struct btrfs_qgroup_list *glist;
  1194. struct ulist_node *unode;
  1195. struct ulist_iterator uiter;
  1196. int sign = 0;
  1197. int ret = 0;
  1198. tmp = ulist_alloc(GFP_NOFS);
  1199. if (!tmp)
  1200. return -ENOMEM;
  1201. spin_lock(&fs_info->qgroup_lock);
  1202. if (!fs_info->quota_root)
  1203. goto out;
  1204. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1205. if (!qgroup)
  1206. goto out;
  1207. switch (oper->type) {
  1208. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1209. sign = 1;
  1210. break;
  1211. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1212. sign = -1;
  1213. break;
  1214. default:
  1215. ASSERT(0);
  1216. }
  1217. qgroup->rfer += sign * oper->num_bytes;
  1218. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1219. WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
  1220. qgroup->excl += sign * oper->num_bytes;
  1221. qgroup->excl_cmpr += sign * oper->num_bytes;
  1222. qgroup_dirty(fs_info, qgroup);
  1223. /* Get all of the parent groups that contain this qgroup */
  1224. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1225. ret = ulist_add(tmp, glist->group->qgroupid,
  1226. ptr_to_u64(glist->group), GFP_ATOMIC);
  1227. if (ret < 0)
  1228. goto out;
  1229. }
  1230. /* Iterate all of the parents and adjust their reference counts */
  1231. ULIST_ITER_INIT(&uiter);
  1232. while ((unode = ulist_next(tmp, &uiter))) {
  1233. qgroup = u64_to_ptr(unode->aux);
  1234. qgroup->rfer += sign * oper->num_bytes;
  1235. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1236. WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
  1237. qgroup->excl += sign * oper->num_bytes;
  1238. qgroup->excl_cmpr += sign * oper->num_bytes;
  1239. qgroup_dirty(fs_info, qgroup);
  1240. /* Add any parents of the parents */
  1241. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1242. ret = ulist_add(tmp, glist->group->qgroupid,
  1243. ptr_to_u64(glist->group), GFP_ATOMIC);
  1244. if (ret < 0)
  1245. goto out;
  1246. }
  1247. }
  1248. ret = 0;
  1249. out:
  1250. spin_unlock(&fs_info->qgroup_lock);
  1251. ulist_free(tmp);
  1252. return ret;
  1253. }
  1254. /*
  1255. * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
  1256. * properly.
  1257. */
  1258. static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
  1259. u64 root_to_skip, struct ulist *tmp,
  1260. struct ulist *roots, struct ulist *qgroups,
  1261. u64 seq, int *old_roots, int rescan)
  1262. {
  1263. struct ulist_node *unode;
  1264. struct ulist_iterator uiter;
  1265. struct ulist_node *tmp_unode;
  1266. struct ulist_iterator tmp_uiter;
  1267. struct btrfs_qgroup *qg;
  1268. int ret;
  1269. ULIST_ITER_INIT(&uiter);
  1270. while ((unode = ulist_next(roots, &uiter))) {
  1271. /* We don't count our current root here */
  1272. if (unode->val == root_to_skip)
  1273. continue;
  1274. qg = find_qgroup_rb(fs_info, unode->val);
  1275. if (!qg)
  1276. continue;
  1277. /*
  1278. * We could have a pending removal of this same ref so we may
  1279. * not have actually found our ref root when doing
  1280. * btrfs_find_all_roots, so we need to keep track of how many
  1281. * old roots we find in case we removed ours and added a
  1282. * different one at the same time. I don't think this could
  1283. * happen in practice but that sort of thinking leads to pain
  1284. * and suffering and to the dark side.
  1285. */
  1286. (*old_roots)++;
  1287. ulist_reinit(tmp);
  1288. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1289. GFP_ATOMIC);
  1290. if (ret < 0)
  1291. return ret;
  1292. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1293. if (ret < 0)
  1294. return ret;
  1295. ULIST_ITER_INIT(&tmp_uiter);
  1296. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1297. struct btrfs_qgroup_list *glist;
  1298. qg = u64_to_ptr(tmp_unode->aux);
  1299. /*
  1300. * We use this sequence number to keep from having to
  1301. * run the whole list and 0 out the refcnt every time.
  1302. * We basically use sequnce as the known 0 count and
  1303. * then add 1 everytime we see a qgroup. This is how we
  1304. * get how many of the roots actually point up to the
  1305. * upper level qgroups in order to determine exclusive
  1306. * counts.
  1307. *
  1308. * For rescan we want to set old_refcnt to seq so our
  1309. * exclusive calculations end up correct.
  1310. */
  1311. if (rescan)
  1312. qg->old_refcnt = seq;
  1313. else if (qg->old_refcnt < seq)
  1314. qg->old_refcnt = seq + 1;
  1315. else
  1316. qg->old_refcnt++;
  1317. if (qg->new_refcnt < seq)
  1318. qg->new_refcnt = seq + 1;
  1319. else
  1320. qg->new_refcnt++;
  1321. list_for_each_entry(glist, &qg->groups, next_group) {
  1322. ret = ulist_add(qgroups, glist->group->qgroupid,
  1323. ptr_to_u64(glist->group),
  1324. GFP_ATOMIC);
  1325. if (ret < 0)
  1326. return ret;
  1327. ret = ulist_add(tmp, glist->group->qgroupid,
  1328. ptr_to_u64(glist->group),
  1329. GFP_ATOMIC);
  1330. if (ret < 0)
  1331. return ret;
  1332. }
  1333. }
  1334. }
  1335. return 0;
  1336. }
  1337. /*
  1338. * We need to walk forward in our operation tree and account for any roots that
  1339. * were deleted after we made this operation.
  1340. */
  1341. static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
  1342. struct btrfs_qgroup_operation *oper,
  1343. struct ulist *tmp,
  1344. struct ulist *qgroups, u64 seq,
  1345. int *old_roots)
  1346. {
  1347. struct ulist_node *unode;
  1348. struct ulist_iterator uiter;
  1349. struct btrfs_qgroup *qg;
  1350. struct btrfs_qgroup_operation *tmp_oper;
  1351. struct rb_node *n;
  1352. int ret;
  1353. ulist_reinit(tmp);
  1354. /*
  1355. * We only walk forward in the tree since we're only interested in
  1356. * removals that happened _after_ our operation.
  1357. */
  1358. spin_lock(&fs_info->qgroup_op_lock);
  1359. n = rb_next(&oper->n);
  1360. spin_unlock(&fs_info->qgroup_op_lock);
  1361. if (!n)
  1362. return 0;
  1363. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1364. while (tmp_oper->bytenr == oper->bytenr) {
  1365. /*
  1366. * If it's not a removal we don't care, additions work out
  1367. * properly with our refcnt tracking.
  1368. */
  1369. if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
  1370. tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
  1371. goto next;
  1372. qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
  1373. if (!qg)
  1374. goto next;
  1375. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1376. GFP_ATOMIC);
  1377. if (ret) {
  1378. if (ret < 0)
  1379. return ret;
  1380. /*
  1381. * We only want to increase old_roots if this qgroup is
  1382. * not already in the list of qgroups. If it is already
  1383. * there then that means it must have been re-added or
  1384. * the delete will be discarded because we had an
  1385. * existing ref that we haven't looked up yet. In this
  1386. * case we don't want to increase old_roots. So if ret
  1387. * == 1 then we know that this is the first time we've
  1388. * seen this qgroup and we can bump the old_roots.
  1389. */
  1390. (*old_roots)++;
  1391. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
  1392. GFP_ATOMIC);
  1393. if (ret < 0)
  1394. return ret;
  1395. }
  1396. next:
  1397. spin_lock(&fs_info->qgroup_op_lock);
  1398. n = rb_next(&tmp_oper->n);
  1399. spin_unlock(&fs_info->qgroup_op_lock);
  1400. if (!n)
  1401. break;
  1402. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1403. }
  1404. /* Ok now process the qgroups we found */
  1405. ULIST_ITER_INIT(&uiter);
  1406. while ((unode = ulist_next(tmp, &uiter))) {
  1407. struct btrfs_qgroup_list *glist;
  1408. qg = u64_to_ptr(unode->aux);
  1409. if (qg->old_refcnt < seq)
  1410. qg->old_refcnt = seq + 1;
  1411. else
  1412. qg->old_refcnt++;
  1413. if (qg->new_refcnt < seq)
  1414. qg->new_refcnt = seq + 1;
  1415. else
  1416. qg->new_refcnt++;
  1417. list_for_each_entry(glist, &qg->groups, next_group) {
  1418. ret = ulist_add(qgroups, glist->group->qgroupid,
  1419. ptr_to_u64(glist->group), GFP_ATOMIC);
  1420. if (ret < 0)
  1421. return ret;
  1422. ret = ulist_add(tmp, glist->group->qgroupid,
  1423. ptr_to_u64(glist->group), GFP_ATOMIC);
  1424. if (ret < 0)
  1425. return ret;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. /* Add refcnt for the newly added reference. */
  1431. static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
  1432. struct btrfs_qgroup_operation *oper,
  1433. struct btrfs_qgroup *qgroup,
  1434. struct ulist *tmp, struct ulist *qgroups,
  1435. u64 seq)
  1436. {
  1437. struct ulist_node *unode;
  1438. struct ulist_iterator uiter;
  1439. struct btrfs_qgroup *qg;
  1440. int ret;
  1441. ulist_reinit(tmp);
  1442. ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
  1443. GFP_ATOMIC);
  1444. if (ret < 0)
  1445. return ret;
  1446. ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
  1447. GFP_ATOMIC);
  1448. if (ret < 0)
  1449. return ret;
  1450. ULIST_ITER_INIT(&uiter);
  1451. while ((unode = ulist_next(tmp, &uiter))) {
  1452. struct btrfs_qgroup_list *glist;
  1453. qg = u64_to_ptr(unode->aux);
  1454. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1455. if (qg->new_refcnt < seq)
  1456. qg->new_refcnt = seq + 1;
  1457. else
  1458. qg->new_refcnt++;
  1459. } else {
  1460. if (qg->old_refcnt < seq)
  1461. qg->old_refcnt = seq + 1;
  1462. else
  1463. qg->old_refcnt++;
  1464. }
  1465. list_for_each_entry(glist, &qg->groups, next_group) {
  1466. ret = ulist_add(tmp, glist->group->qgroupid,
  1467. ptr_to_u64(glist->group), GFP_ATOMIC);
  1468. if (ret < 0)
  1469. return ret;
  1470. ret = ulist_add(qgroups, glist->group->qgroupid,
  1471. ptr_to_u64(glist->group), GFP_ATOMIC);
  1472. if (ret < 0)
  1473. return ret;
  1474. }
  1475. }
  1476. return 0;
  1477. }
  1478. /*
  1479. * This adjusts the counters for all referenced qgroups if need be.
  1480. */
  1481. static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
  1482. u64 root_to_skip, u64 num_bytes,
  1483. struct ulist *qgroups, u64 seq,
  1484. int old_roots, int new_roots, int rescan)
  1485. {
  1486. struct ulist_node *unode;
  1487. struct ulist_iterator uiter;
  1488. struct btrfs_qgroup *qg;
  1489. u64 cur_new_count, cur_old_count;
  1490. ULIST_ITER_INIT(&uiter);
  1491. while ((unode = ulist_next(qgroups, &uiter))) {
  1492. bool dirty = false;
  1493. qg = u64_to_ptr(unode->aux);
  1494. /*
  1495. * Wasn't referenced before but is now, add to the reference
  1496. * counters.
  1497. */
  1498. if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
  1499. qg->rfer += num_bytes;
  1500. qg->rfer_cmpr += num_bytes;
  1501. dirty = true;
  1502. }
  1503. /*
  1504. * Was referenced before but isn't now, subtract from the
  1505. * reference counters.
  1506. */
  1507. if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
  1508. qg->rfer -= num_bytes;
  1509. qg->rfer_cmpr -= num_bytes;
  1510. dirty = true;
  1511. }
  1512. if (qg->old_refcnt < seq)
  1513. cur_old_count = 0;
  1514. else
  1515. cur_old_count = qg->old_refcnt - seq;
  1516. if (qg->new_refcnt < seq)
  1517. cur_new_count = 0;
  1518. else
  1519. cur_new_count = qg->new_refcnt - seq;
  1520. /*
  1521. * If our refcount was the same as the roots previously but our
  1522. * new count isn't the same as the number of roots now then we
  1523. * went from having a exclusive reference on this range to not.
  1524. */
  1525. if (old_roots && cur_old_count == old_roots &&
  1526. (cur_new_count != new_roots || new_roots == 0)) {
  1527. WARN_ON(cur_new_count != new_roots && new_roots == 0);
  1528. qg->excl -= num_bytes;
  1529. qg->excl_cmpr -= num_bytes;
  1530. dirty = true;
  1531. }
  1532. /*
  1533. * If we didn't reference all the roots before but now we do we
  1534. * have an exclusive reference to this range.
  1535. */
  1536. if ((!old_roots || (old_roots && cur_old_count != old_roots))
  1537. && cur_new_count == new_roots) {
  1538. qg->excl += num_bytes;
  1539. qg->excl_cmpr += num_bytes;
  1540. dirty = true;
  1541. }
  1542. if (dirty)
  1543. qgroup_dirty(fs_info, qg);
  1544. }
  1545. return 0;
  1546. }
  1547. /*
  1548. * If we removed a data extent and there were other references for that bytenr
  1549. * then we need to lookup all referenced roots to make sure we still don't
  1550. * reference this bytenr. If we do then we can just discard this operation.
  1551. */
  1552. static int check_existing_refs(struct btrfs_trans_handle *trans,
  1553. struct btrfs_fs_info *fs_info,
  1554. struct btrfs_qgroup_operation *oper)
  1555. {
  1556. struct ulist *roots = NULL;
  1557. struct ulist_node *unode;
  1558. struct ulist_iterator uiter;
  1559. int ret = 0;
  1560. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1561. oper->elem.seq, &roots);
  1562. if (ret < 0)
  1563. return ret;
  1564. ret = 0;
  1565. ULIST_ITER_INIT(&uiter);
  1566. while ((unode = ulist_next(roots, &uiter))) {
  1567. if (unode->val == oper->ref_root) {
  1568. ret = 1;
  1569. break;
  1570. }
  1571. }
  1572. ulist_free(roots);
  1573. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1574. return ret;
  1575. }
  1576. /*
  1577. * If we share a reference across multiple roots then we may need to adjust
  1578. * various qgroups referenced and exclusive counters. The basic premise is this
  1579. *
  1580. * 1) We have seq to represent a 0 count. Instead of looping through all of the
  1581. * qgroups and resetting their refcount to 0 we just constantly bump this
  1582. * sequence number to act as the base reference count. This means that if
  1583. * anybody is equal to or below this sequence they were never referenced. We
  1584. * jack this sequence up by the number of roots we found each time in order to
  1585. * make sure we don't have any overlap.
  1586. *
  1587. * 2) We first search all the roots that reference the area _except_ the root
  1588. * we're acting on currently. This makes up the old_refcnt of all the qgroups
  1589. * before.
  1590. *
  1591. * 3) We walk all of the qgroups referenced by the root we are currently acting
  1592. * on, and will either adjust old_refcnt in the case of a removal or the
  1593. * new_refcnt in the case of an addition.
  1594. *
  1595. * 4) Finally we walk all the qgroups that are referenced by this range
  1596. * including the root we are acting on currently. We will adjust the counters
  1597. * based on the number of roots we had and will have after this operation.
  1598. *
  1599. * Take this example as an illustration
  1600. *
  1601. * [qgroup 1/0]
  1602. * / | \
  1603. * [qg 0/0] [qg 0/1] [qg 0/2]
  1604. * \ | /
  1605. * [ extent ]
  1606. *
  1607. * Say we are adding a reference that is covered by qg 0/0. The first step
  1608. * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
  1609. * old_roots being 2. Because it is adding new_roots will be 1. We then go
  1610. * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
  1611. * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
  1612. * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
  1613. * reference and thus must add the size to the referenced bytes. Everything
  1614. * else is the same so nothing else changes.
  1615. */
  1616. static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
  1617. struct btrfs_fs_info *fs_info,
  1618. struct btrfs_qgroup_operation *oper)
  1619. {
  1620. struct ulist *roots = NULL;
  1621. struct ulist *qgroups, *tmp;
  1622. struct btrfs_qgroup *qgroup;
  1623. struct seq_list elem = {};
  1624. u64 seq;
  1625. int old_roots = 0;
  1626. int new_roots = 0;
  1627. int ret = 0;
  1628. if (oper->elem.seq) {
  1629. ret = check_existing_refs(trans, fs_info, oper);
  1630. if (ret < 0)
  1631. return ret;
  1632. if (ret)
  1633. return 0;
  1634. }
  1635. qgroups = ulist_alloc(GFP_NOFS);
  1636. if (!qgroups)
  1637. return -ENOMEM;
  1638. tmp = ulist_alloc(GFP_NOFS);
  1639. if (!tmp) {
  1640. ulist_free(qgroups);
  1641. return -ENOMEM;
  1642. }
  1643. btrfs_get_tree_mod_seq(fs_info, &elem);
  1644. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
  1645. &roots);
  1646. btrfs_put_tree_mod_seq(fs_info, &elem);
  1647. if (ret < 0) {
  1648. ulist_free(qgroups);
  1649. ulist_free(tmp);
  1650. return ret;
  1651. }
  1652. spin_lock(&fs_info->qgroup_lock);
  1653. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1654. if (!qgroup)
  1655. goto out;
  1656. seq = fs_info->qgroup_seq;
  1657. /*
  1658. * So roots is the list of all the roots currently pointing at the
  1659. * bytenr, including the ref we are adding if we are adding, or not if
  1660. * we are removing a ref. So we pass in the ref_root to skip that root
  1661. * in our calculations. We set old_refnct and new_refcnt cause who the
  1662. * hell knows what everything looked like before, and it doesn't matter
  1663. * except...
  1664. */
  1665. ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
  1666. seq, &old_roots, 0);
  1667. if (ret < 0)
  1668. goto out;
  1669. /*
  1670. * Now adjust the refcounts of the qgroups that care about this
  1671. * reference, either the old_count in the case of removal or new_count
  1672. * in the case of an addition.
  1673. */
  1674. ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
  1675. seq);
  1676. if (ret < 0)
  1677. goto out;
  1678. /*
  1679. * ...in the case of removals. If we had a removal before we got around
  1680. * to processing this operation then we need to find that guy and count
  1681. * his references as if they really existed so we don't end up screwing
  1682. * up the exclusive counts. Then whenever we go to process the delete
  1683. * everything will be grand and we can account for whatever exclusive
  1684. * changes need to be made there. We also have to pass in old_roots so
  1685. * we have an accurate count of the roots as it pertains to this
  1686. * operations view of the world.
  1687. */
  1688. ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
  1689. &old_roots);
  1690. if (ret < 0)
  1691. goto out;
  1692. /*
  1693. * We are adding our root, need to adjust up the number of roots,
  1694. * otherwise old_roots is the number of roots we want.
  1695. */
  1696. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1697. new_roots = old_roots + 1;
  1698. } else {
  1699. new_roots = old_roots;
  1700. old_roots++;
  1701. }
  1702. fs_info->qgroup_seq += old_roots + 1;
  1703. /*
  1704. * And now the magic happens, bless Arne for having a pretty elegant
  1705. * solution for this.
  1706. */
  1707. qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
  1708. qgroups, seq, old_roots, new_roots, 0);
  1709. out:
  1710. spin_unlock(&fs_info->qgroup_lock);
  1711. ulist_free(qgroups);
  1712. ulist_free(roots);
  1713. ulist_free(tmp);
  1714. return ret;
  1715. }
  1716. /*
  1717. * Process a reference to a shared subtree. This type of operation is
  1718. * queued during snapshot removal when we encounter extents which are
  1719. * shared between more than one root.
  1720. */
  1721. static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
  1722. struct btrfs_fs_info *fs_info,
  1723. struct btrfs_qgroup_operation *oper)
  1724. {
  1725. struct ulist *roots = NULL;
  1726. struct ulist_node *unode;
  1727. struct ulist_iterator uiter;
  1728. struct btrfs_qgroup_list *glist;
  1729. struct ulist *parents;
  1730. int ret = 0;
  1731. int err;
  1732. struct btrfs_qgroup *qg;
  1733. u64 root_obj = 0;
  1734. struct seq_list elem = {};
  1735. parents = ulist_alloc(GFP_NOFS);
  1736. if (!parents)
  1737. return -ENOMEM;
  1738. btrfs_get_tree_mod_seq(fs_info, &elem);
  1739. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1740. elem.seq, &roots);
  1741. btrfs_put_tree_mod_seq(fs_info, &elem);
  1742. if (ret < 0)
  1743. goto out;
  1744. if (roots->nnodes != 1)
  1745. goto out;
  1746. ULIST_ITER_INIT(&uiter);
  1747. unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
  1748. /*
  1749. * If we find our ref root then that means all refs
  1750. * this extent has to the root have not yet been
  1751. * deleted. In that case, we do nothing and let the
  1752. * last ref for this bytenr drive our update.
  1753. *
  1754. * This can happen for example if an extent is
  1755. * referenced multiple times in a snapshot (clone,
  1756. * etc). If we are in the middle of snapshot removal,
  1757. * queued updates for such an extent will find the
  1758. * root if we have not yet finished removing the
  1759. * snapshot.
  1760. */
  1761. if (unode->val == oper->ref_root)
  1762. goto out;
  1763. root_obj = unode->val;
  1764. BUG_ON(!root_obj);
  1765. spin_lock(&fs_info->qgroup_lock);
  1766. qg = find_qgroup_rb(fs_info, root_obj);
  1767. if (!qg)
  1768. goto out_unlock;
  1769. qg->excl += oper->num_bytes;
  1770. qg->excl_cmpr += oper->num_bytes;
  1771. qgroup_dirty(fs_info, qg);
  1772. /*
  1773. * Adjust counts for parent groups. First we find all
  1774. * parents, then in the 2nd loop we do the adjustment
  1775. * while adding parents of the parents to our ulist.
  1776. */
  1777. list_for_each_entry(glist, &qg->groups, next_group) {
  1778. err = ulist_add(parents, glist->group->qgroupid,
  1779. ptr_to_u64(glist->group), GFP_ATOMIC);
  1780. if (err < 0) {
  1781. ret = err;
  1782. goto out_unlock;
  1783. }
  1784. }
  1785. ULIST_ITER_INIT(&uiter);
  1786. while ((unode = ulist_next(parents, &uiter))) {
  1787. qg = u64_to_ptr(unode->aux);
  1788. qg->excl += oper->num_bytes;
  1789. qg->excl_cmpr += oper->num_bytes;
  1790. qgroup_dirty(fs_info, qg);
  1791. /* Add any parents of the parents */
  1792. list_for_each_entry(glist, &qg->groups, next_group) {
  1793. err = ulist_add(parents, glist->group->qgroupid,
  1794. ptr_to_u64(glist->group), GFP_ATOMIC);
  1795. if (err < 0) {
  1796. ret = err;
  1797. goto out_unlock;
  1798. }
  1799. }
  1800. }
  1801. out_unlock:
  1802. spin_unlock(&fs_info->qgroup_lock);
  1803. out:
  1804. ulist_free(roots);
  1805. ulist_free(parents);
  1806. return ret;
  1807. }
  1808. /*
  1809. * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
  1810. * from the fs. First, all roots referencing the extent are searched, and
  1811. * then the space is accounted accordingly to the different roots. The
  1812. * accounting algorithm works in 3 steps documented inline.
  1813. */
  1814. static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
  1815. struct btrfs_fs_info *fs_info,
  1816. struct btrfs_qgroup_operation *oper)
  1817. {
  1818. int ret = 0;
  1819. if (!fs_info->quota_enabled)
  1820. return 0;
  1821. BUG_ON(!fs_info->quota_root);
  1822. mutex_lock(&fs_info->qgroup_rescan_lock);
  1823. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1824. if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
  1825. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1826. return 0;
  1827. }
  1828. }
  1829. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1830. ASSERT(is_fstree(oper->ref_root));
  1831. trace_btrfs_qgroup_account(oper);
  1832. switch (oper->type) {
  1833. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1834. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1835. ret = qgroup_excl_accounting(fs_info, oper);
  1836. break;
  1837. case BTRFS_QGROUP_OPER_ADD_SHARED:
  1838. case BTRFS_QGROUP_OPER_SUB_SHARED:
  1839. ret = qgroup_shared_accounting(trans, fs_info, oper);
  1840. break;
  1841. case BTRFS_QGROUP_OPER_SUB_SUBTREE:
  1842. ret = qgroup_subtree_accounting(trans, fs_info, oper);
  1843. break;
  1844. default:
  1845. ASSERT(0);
  1846. }
  1847. return ret;
  1848. }
  1849. /*
  1850. * Needs to be called everytime we run delayed refs, even if there is an error
  1851. * in order to cleanup outstanding operations.
  1852. */
  1853. int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
  1854. struct btrfs_fs_info *fs_info)
  1855. {
  1856. struct btrfs_qgroup_operation *oper;
  1857. int ret = 0;
  1858. while (!list_empty(&trans->qgroup_ref_list)) {
  1859. oper = list_first_entry(&trans->qgroup_ref_list,
  1860. struct btrfs_qgroup_operation, list);
  1861. list_del_init(&oper->list);
  1862. if (!ret || !trans->aborted)
  1863. ret = btrfs_qgroup_account(trans, fs_info, oper);
  1864. spin_lock(&fs_info->qgroup_op_lock);
  1865. rb_erase(&oper->n, &fs_info->qgroup_op_tree);
  1866. spin_unlock(&fs_info->qgroup_op_lock);
  1867. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1868. kfree(oper);
  1869. }
  1870. return ret;
  1871. }
  1872. /*
  1873. * called from commit_transaction. Writes all changed qgroups to disk.
  1874. */
  1875. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1876. struct btrfs_fs_info *fs_info)
  1877. {
  1878. struct btrfs_root *quota_root = fs_info->quota_root;
  1879. int ret = 0;
  1880. int start_rescan_worker = 0;
  1881. if (!quota_root)
  1882. goto out;
  1883. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1884. start_rescan_worker = 1;
  1885. fs_info->quota_enabled = fs_info->pending_quota_state;
  1886. spin_lock(&fs_info->qgroup_lock);
  1887. while (!list_empty(&fs_info->dirty_qgroups)) {
  1888. struct btrfs_qgroup *qgroup;
  1889. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1890. struct btrfs_qgroup, dirty);
  1891. list_del_init(&qgroup->dirty);
  1892. spin_unlock(&fs_info->qgroup_lock);
  1893. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1894. if (ret)
  1895. fs_info->qgroup_flags |=
  1896. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1897. spin_lock(&fs_info->qgroup_lock);
  1898. }
  1899. if (fs_info->quota_enabled)
  1900. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1901. else
  1902. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1903. spin_unlock(&fs_info->qgroup_lock);
  1904. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1905. if (ret)
  1906. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1907. if (!ret && start_rescan_worker) {
  1908. ret = qgroup_rescan_init(fs_info, 0, 1);
  1909. if (!ret) {
  1910. qgroup_rescan_zero_tracking(fs_info);
  1911. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1912. &fs_info->qgroup_rescan_work);
  1913. }
  1914. ret = 0;
  1915. }
  1916. out:
  1917. return ret;
  1918. }
  1919. /*
  1920. * copy the acounting information between qgroups. This is necessary when a
  1921. * snapshot or a subvolume is created
  1922. */
  1923. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1924. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1925. struct btrfs_qgroup_inherit *inherit)
  1926. {
  1927. int ret = 0;
  1928. int i;
  1929. u64 *i_qgroups;
  1930. struct btrfs_root *quota_root = fs_info->quota_root;
  1931. struct btrfs_qgroup *srcgroup;
  1932. struct btrfs_qgroup *dstgroup;
  1933. u32 level_size = 0;
  1934. u64 nums;
  1935. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1936. if (!fs_info->quota_enabled)
  1937. goto out;
  1938. if (!quota_root) {
  1939. ret = -EINVAL;
  1940. goto out;
  1941. }
  1942. if (inherit) {
  1943. i_qgroups = (u64 *)(inherit + 1);
  1944. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1945. 2 * inherit->num_excl_copies;
  1946. for (i = 0; i < nums; ++i) {
  1947. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1948. if (!srcgroup) {
  1949. ret = -EINVAL;
  1950. goto out;
  1951. }
  1952. ++i_qgroups;
  1953. }
  1954. }
  1955. /*
  1956. * create a tracking group for the subvol itself
  1957. */
  1958. ret = add_qgroup_item(trans, quota_root, objectid);
  1959. if (ret)
  1960. goto out;
  1961. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1962. ret = update_qgroup_limit_item(trans, quota_root, objectid,
  1963. inherit->lim.flags,
  1964. inherit->lim.max_rfer,
  1965. inherit->lim.max_excl,
  1966. inherit->lim.rsv_rfer,
  1967. inherit->lim.rsv_excl);
  1968. if (ret)
  1969. goto out;
  1970. }
  1971. if (srcid) {
  1972. struct btrfs_root *srcroot;
  1973. struct btrfs_key srckey;
  1974. srckey.objectid = srcid;
  1975. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1976. srckey.offset = (u64)-1;
  1977. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1978. if (IS_ERR(srcroot)) {
  1979. ret = PTR_ERR(srcroot);
  1980. goto out;
  1981. }
  1982. rcu_read_lock();
  1983. level_size = srcroot->nodesize;
  1984. rcu_read_unlock();
  1985. }
  1986. /*
  1987. * add qgroup to all inherited groups
  1988. */
  1989. if (inherit) {
  1990. i_qgroups = (u64 *)(inherit + 1);
  1991. for (i = 0; i < inherit->num_qgroups; ++i) {
  1992. ret = add_qgroup_relation_item(trans, quota_root,
  1993. objectid, *i_qgroups);
  1994. if (ret)
  1995. goto out;
  1996. ret = add_qgroup_relation_item(trans, quota_root,
  1997. *i_qgroups, objectid);
  1998. if (ret)
  1999. goto out;
  2000. ++i_qgroups;
  2001. }
  2002. }
  2003. spin_lock(&fs_info->qgroup_lock);
  2004. dstgroup = add_qgroup_rb(fs_info, objectid);
  2005. if (IS_ERR(dstgroup)) {
  2006. ret = PTR_ERR(dstgroup);
  2007. goto unlock;
  2008. }
  2009. if (srcid) {
  2010. srcgroup = find_qgroup_rb(fs_info, srcid);
  2011. if (!srcgroup)
  2012. goto unlock;
  2013. /*
  2014. * We call inherit after we clone the root in order to make sure
  2015. * our counts don't go crazy, so at this point the only
  2016. * difference between the two roots should be the root node.
  2017. */
  2018. dstgroup->rfer = srcgroup->rfer;
  2019. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  2020. dstgroup->excl = level_size;
  2021. dstgroup->excl_cmpr = level_size;
  2022. srcgroup->excl = level_size;
  2023. srcgroup->excl_cmpr = level_size;
  2024. qgroup_dirty(fs_info, dstgroup);
  2025. qgroup_dirty(fs_info, srcgroup);
  2026. }
  2027. if (!inherit)
  2028. goto unlock;
  2029. i_qgroups = (u64 *)(inherit + 1);
  2030. for (i = 0; i < inherit->num_qgroups; ++i) {
  2031. ret = add_relation_rb(quota_root->fs_info, objectid,
  2032. *i_qgroups);
  2033. if (ret)
  2034. goto unlock;
  2035. ++i_qgroups;
  2036. }
  2037. for (i = 0; i < inherit->num_ref_copies; ++i) {
  2038. struct btrfs_qgroup *src;
  2039. struct btrfs_qgroup *dst;
  2040. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2041. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2042. if (!src || !dst) {
  2043. ret = -EINVAL;
  2044. goto unlock;
  2045. }
  2046. dst->rfer = src->rfer - level_size;
  2047. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  2048. i_qgroups += 2;
  2049. }
  2050. for (i = 0; i < inherit->num_excl_copies; ++i) {
  2051. struct btrfs_qgroup *src;
  2052. struct btrfs_qgroup *dst;
  2053. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2054. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2055. if (!src || !dst) {
  2056. ret = -EINVAL;
  2057. goto unlock;
  2058. }
  2059. dst->excl = src->excl + level_size;
  2060. dst->excl_cmpr = src->excl_cmpr + level_size;
  2061. i_qgroups += 2;
  2062. }
  2063. unlock:
  2064. spin_unlock(&fs_info->qgroup_lock);
  2065. out:
  2066. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2067. return ret;
  2068. }
  2069. /*
  2070. * reserve some space for a qgroup and all its parents. The reservation takes
  2071. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  2072. * accounting. If not enough space is available, EDQUOT is returned.
  2073. * We assume that the requested space is new for all qgroups.
  2074. */
  2075. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  2076. {
  2077. struct btrfs_root *quota_root;
  2078. struct btrfs_qgroup *qgroup;
  2079. struct btrfs_fs_info *fs_info = root->fs_info;
  2080. u64 ref_root = root->root_key.objectid;
  2081. int ret = 0;
  2082. struct ulist_node *unode;
  2083. struct ulist_iterator uiter;
  2084. if (!is_fstree(ref_root))
  2085. return 0;
  2086. if (num_bytes == 0)
  2087. return 0;
  2088. spin_lock(&fs_info->qgroup_lock);
  2089. quota_root = fs_info->quota_root;
  2090. if (!quota_root)
  2091. goto out;
  2092. qgroup = find_qgroup_rb(fs_info, ref_root);
  2093. if (!qgroup)
  2094. goto out;
  2095. /*
  2096. * in a first step, we check all affected qgroups if any limits would
  2097. * be exceeded
  2098. */
  2099. ulist_reinit(fs_info->qgroup_ulist);
  2100. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2101. (uintptr_t)qgroup, GFP_ATOMIC);
  2102. if (ret < 0)
  2103. goto out;
  2104. ULIST_ITER_INIT(&uiter);
  2105. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2106. struct btrfs_qgroup *qg;
  2107. struct btrfs_qgroup_list *glist;
  2108. qg = u64_to_ptr(unode->aux);
  2109. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2110. qg->reserved + (s64)qg->rfer + num_bytes >
  2111. qg->max_rfer) {
  2112. ret = -EDQUOT;
  2113. goto out;
  2114. }
  2115. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2116. qg->reserved + (s64)qg->excl + num_bytes >
  2117. qg->max_excl) {
  2118. ret = -EDQUOT;
  2119. goto out;
  2120. }
  2121. list_for_each_entry(glist, &qg->groups, next_group) {
  2122. ret = ulist_add(fs_info->qgroup_ulist,
  2123. glist->group->qgroupid,
  2124. (uintptr_t)glist->group, GFP_ATOMIC);
  2125. if (ret < 0)
  2126. goto out;
  2127. }
  2128. }
  2129. ret = 0;
  2130. /*
  2131. * no limits exceeded, now record the reservation into all qgroups
  2132. */
  2133. ULIST_ITER_INIT(&uiter);
  2134. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2135. struct btrfs_qgroup *qg;
  2136. qg = u64_to_ptr(unode->aux);
  2137. qg->reserved += num_bytes;
  2138. }
  2139. out:
  2140. spin_unlock(&fs_info->qgroup_lock);
  2141. return ret;
  2142. }
  2143. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  2144. {
  2145. struct btrfs_root *quota_root;
  2146. struct btrfs_qgroup *qgroup;
  2147. struct btrfs_fs_info *fs_info = root->fs_info;
  2148. struct ulist_node *unode;
  2149. struct ulist_iterator uiter;
  2150. u64 ref_root = root->root_key.objectid;
  2151. int ret = 0;
  2152. if (!is_fstree(ref_root))
  2153. return;
  2154. if (num_bytes == 0)
  2155. return;
  2156. spin_lock(&fs_info->qgroup_lock);
  2157. quota_root = fs_info->quota_root;
  2158. if (!quota_root)
  2159. goto out;
  2160. qgroup = find_qgroup_rb(fs_info, ref_root);
  2161. if (!qgroup)
  2162. goto out;
  2163. ulist_reinit(fs_info->qgroup_ulist);
  2164. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2165. (uintptr_t)qgroup, GFP_ATOMIC);
  2166. if (ret < 0)
  2167. goto out;
  2168. ULIST_ITER_INIT(&uiter);
  2169. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2170. struct btrfs_qgroup *qg;
  2171. struct btrfs_qgroup_list *glist;
  2172. qg = u64_to_ptr(unode->aux);
  2173. qg->reserved -= num_bytes;
  2174. list_for_each_entry(glist, &qg->groups, next_group) {
  2175. ret = ulist_add(fs_info->qgroup_ulist,
  2176. glist->group->qgroupid,
  2177. (uintptr_t)glist->group, GFP_ATOMIC);
  2178. if (ret < 0)
  2179. goto out;
  2180. }
  2181. }
  2182. out:
  2183. spin_unlock(&fs_info->qgroup_lock);
  2184. }
  2185. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  2186. {
  2187. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  2188. return;
  2189. btrfs_err(trans->root->fs_info,
  2190. "qgroups not uptodate in trans handle %p: list is%s empty, "
  2191. "seq is %#x.%x",
  2192. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2193. (u32)(trans->delayed_ref_elem.seq >> 32),
  2194. (u32)trans->delayed_ref_elem.seq);
  2195. BUG();
  2196. }
  2197. /*
  2198. * returns < 0 on error, 0 when more leafs are to be scanned.
  2199. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  2200. */
  2201. static int
  2202. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2203. struct btrfs_trans_handle *trans, struct ulist *qgroups,
  2204. struct ulist *tmp, struct extent_buffer *scratch_leaf)
  2205. {
  2206. struct btrfs_key found;
  2207. struct ulist *roots = NULL;
  2208. struct seq_list tree_mod_seq_elem = {};
  2209. u64 num_bytes;
  2210. u64 seq;
  2211. int new_roots;
  2212. int slot;
  2213. int ret;
  2214. path->leave_spinning = 1;
  2215. mutex_lock(&fs_info->qgroup_rescan_lock);
  2216. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2217. &fs_info->qgroup_rescan_progress,
  2218. path, 1, 0);
  2219. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  2220. fs_info->qgroup_rescan_progress.objectid,
  2221. fs_info->qgroup_rescan_progress.type,
  2222. fs_info->qgroup_rescan_progress.offset, ret);
  2223. if (ret) {
  2224. /*
  2225. * The rescan is about to end, we will not be scanning any
  2226. * further blocks. We cannot unset the RESCAN flag here, because
  2227. * we want to commit the transaction if everything went well.
  2228. * To make the live accounting work in this phase, we set our
  2229. * scan progress pointer such that every real extent objectid
  2230. * will be smaller.
  2231. */
  2232. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2233. btrfs_release_path(path);
  2234. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2235. return ret;
  2236. }
  2237. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2238. btrfs_header_nritems(path->nodes[0]) - 1);
  2239. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2240. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2241. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  2242. slot = path->slots[0];
  2243. btrfs_release_path(path);
  2244. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2245. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2246. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2247. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2248. found.type != BTRFS_METADATA_ITEM_KEY)
  2249. continue;
  2250. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2251. num_bytes = fs_info->extent_root->nodesize;
  2252. else
  2253. num_bytes = found.offset;
  2254. ulist_reinit(qgroups);
  2255. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2256. &roots);
  2257. if (ret < 0)
  2258. goto out;
  2259. spin_lock(&fs_info->qgroup_lock);
  2260. seq = fs_info->qgroup_seq;
  2261. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  2262. new_roots = 0;
  2263. ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
  2264. seq, &new_roots, 1);
  2265. if (ret < 0) {
  2266. spin_unlock(&fs_info->qgroup_lock);
  2267. ulist_free(roots);
  2268. goto out;
  2269. }
  2270. ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
  2271. seq, 0, new_roots, 1);
  2272. if (ret < 0) {
  2273. spin_unlock(&fs_info->qgroup_lock);
  2274. ulist_free(roots);
  2275. goto out;
  2276. }
  2277. spin_unlock(&fs_info->qgroup_lock);
  2278. ulist_free(roots);
  2279. }
  2280. out:
  2281. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2282. return ret;
  2283. }
  2284. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2285. {
  2286. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2287. qgroup_rescan_work);
  2288. struct btrfs_path *path;
  2289. struct btrfs_trans_handle *trans = NULL;
  2290. struct ulist *tmp = NULL, *qgroups = NULL;
  2291. struct extent_buffer *scratch_leaf = NULL;
  2292. int err = -ENOMEM;
  2293. path = btrfs_alloc_path();
  2294. if (!path)
  2295. goto out;
  2296. qgroups = ulist_alloc(GFP_NOFS);
  2297. if (!qgroups)
  2298. goto out;
  2299. tmp = ulist_alloc(GFP_NOFS);
  2300. if (!tmp)
  2301. goto out;
  2302. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  2303. if (!scratch_leaf)
  2304. goto out;
  2305. err = 0;
  2306. while (!err) {
  2307. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2308. if (IS_ERR(trans)) {
  2309. err = PTR_ERR(trans);
  2310. break;
  2311. }
  2312. if (!fs_info->quota_enabled) {
  2313. err = -EINTR;
  2314. } else {
  2315. err = qgroup_rescan_leaf(fs_info, path, trans,
  2316. qgroups, tmp, scratch_leaf);
  2317. }
  2318. if (err > 0)
  2319. btrfs_commit_transaction(trans, fs_info->fs_root);
  2320. else
  2321. btrfs_end_transaction(trans, fs_info->fs_root);
  2322. }
  2323. out:
  2324. kfree(scratch_leaf);
  2325. ulist_free(qgroups);
  2326. ulist_free(tmp);
  2327. btrfs_free_path(path);
  2328. mutex_lock(&fs_info->qgroup_rescan_lock);
  2329. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2330. if (err == 2 &&
  2331. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2332. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2333. } else if (err < 0) {
  2334. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2335. }
  2336. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2337. if (err >= 0) {
  2338. btrfs_info(fs_info, "qgroup scan completed%s",
  2339. err == 2 ? " (inconsistency flag cleared)" : "");
  2340. } else {
  2341. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2342. }
  2343. complete_all(&fs_info->qgroup_rescan_completion);
  2344. }
  2345. /*
  2346. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2347. * memory required for the rescan context.
  2348. */
  2349. static int
  2350. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2351. int init_flags)
  2352. {
  2353. int ret = 0;
  2354. if (!init_flags &&
  2355. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2356. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2357. ret = -EINVAL;
  2358. goto err;
  2359. }
  2360. mutex_lock(&fs_info->qgroup_rescan_lock);
  2361. spin_lock(&fs_info->qgroup_lock);
  2362. if (init_flags) {
  2363. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2364. ret = -EINPROGRESS;
  2365. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2366. ret = -EINVAL;
  2367. if (ret) {
  2368. spin_unlock(&fs_info->qgroup_lock);
  2369. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2370. goto err;
  2371. }
  2372. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2373. }
  2374. memset(&fs_info->qgroup_rescan_progress, 0,
  2375. sizeof(fs_info->qgroup_rescan_progress));
  2376. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2377. spin_unlock(&fs_info->qgroup_lock);
  2378. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2379. init_completion(&fs_info->qgroup_rescan_completion);
  2380. memset(&fs_info->qgroup_rescan_work, 0,
  2381. sizeof(fs_info->qgroup_rescan_work));
  2382. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2383. btrfs_qgroup_rescan_helper,
  2384. btrfs_qgroup_rescan_worker, NULL, NULL);
  2385. if (ret) {
  2386. err:
  2387. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2388. return ret;
  2389. }
  2390. return 0;
  2391. }
  2392. static void
  2393. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2394. {
  2395. struct rb_node *n;
  2396. struct btrfs_qgroup *qgroup;
  2397. spin_lock(&fs_info->qgroup_lock);
  2398. /* clear all current qgroup tracking information */
  2399. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2400. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2401. qgroup->rfer = 0;
  2402. qgroup->rfer_cmpr = 0;
  2403. qgroup->excl = 0;
  2404. qgroup->excl_cmpr = 0;
  2405. }
  2406. spin_unlock(&fs_info->qgroup_lock);
  2407. }
  2408. int
  2409. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2410. {
  2411. int ret = 0;
  2412. struct btrfs_trans_handle *trans;
  2413. ret = qgroup_rescan_init(fs_info, 0, 1);
  2414. if (ret)
  2415. return ret;
  2416. /*
  2417. * We have set the rescan_progress to 0, which means no more
  2418. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2419. * However, btrfs_qgroup_account_ref may be right after its call
  2420. * to btrfs_find_all_roots, in which case it would still do the
  2421. * accounting.
  2422. * To solve this, we're committing the transaction, which will
  2423. * ensure we run all delayed refs and only after that, we are
  2424. * going to clear all tracking information for a clean start.
  2425. */
  2426. trans = btrfs_join_transaction(fs_info->fs_root);
  2427. if (IS_ERR(trans)) {
  2428. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2429. return PTR_ERR(trans);
  2430. }
  2431. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2432. if (ret) {
  2433. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2434. return ret;
  2435. }
  2436. qgroup_rescan_zero_tracking(fs_info);
  2437. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2438. &fs_info->qgroup_rescan_work);
  2439. return 0;
  2440. }
  2441. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  2442. {
  2443. int running;
  2444. int ret = 0;
  2445. mutex_lock(&fs_info->qgroup_rescan_lock);
  2446. spin_lock(&fs_info->qgroup_lock);
  2447. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2448. spin_unlock(&fs_info->qgroup_lock);
  2449. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2450. if (running)
  2451. ret = wait_for_completion_interruptible(
  2452. &fs_info->qgroup_rescan_completion);
  2453. return ret;
  2454. }
  2455. /*
  2456. * this is only called from open_ctree where we're still single threaded, thus
  2457. * locking is omitted here.
  2458. */
  2459. void
  2460. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2461. {
  2462. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2463. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2464. &fs_info->qgroup_rescan_work);
  2465. }