qgroup.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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. /* TODO XXX FIXME
  34. * - subvol delete -> delete when ref goes to 0? delete limits also?
  35. * - reorganize keys
  36. * - compressed
  37. * - sync
  38. * - copy also limits on subvol creation
  39. * - limit
  40. * - caches fuer ulists
  41. * - performance benchmarks
  42. * - check all ioctl parameters
  43. */
  44. /*
  45. * one struct for each qgroup, organized in fs_info->qgroup_tree.
  46. */
  47. struct btrfs_qgroup {
  48. u64 qgroupid;
  49. /*
  50. * state
  51. */
  52. u64 rfer; /* referenced */
  53. u64 rfer_cmpr; /* referenced compressed */
  54. u64 excl; /* exclusive */
  55. u64 excl_cmpr; /* exclusive compressed */
  56. /*
  57. * limits
  58. */
  59. u64 lim_flags; /* which limits are set */
  60. u64 max_rfer;
  61. u64 max_excl;
  62. u64 rsv_rfer;
  63. u64 rsv_excl;
  64. /*
  65. * reservation tracking
  66. */
  67. u64 reserved;
  68. /*
  69. * lists
  70. */
  71. struct list_head groups; /* groups this group is member of */
  72. struct list_head members; /* groups that are members of this group */
  73. struct list_head dirty; /* dirty groups */
  74. struct rb_node node; /* tree of qgroups */
  75. /*
  76. * temp variables for accounting operations
  77. */
  78. u64 tag;
  79. u64 refcnt;
  80. };
  81. /*
  82. * glue structure to represent the relations between qgroups.
  83. */
  84. struct btrfs_qgroup_list {
  85. struct list_head next_group;
  86. struct list_head next_member;
  87. struct btrfs_qgroup *group;
  88. struct btrfs_qgroup *member;
  89. };
  90. static int
  91. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  92. int init_flags);
  93. static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
  94. /* must be called with qgroup_ioctl_lock held */
  95. static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
  96. u64 qgroupid)
  97. {
  98. struct rb_node *n = fs_info->qgroup_tree.rb_node;
  99. struct btrfs_qgroup *qgroup;
  100. while (n) {
  101. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  102. if (qgroup->qgroupid < qgroupid)
  103. n = n->rb_left;
  104. else if (qgroup->qgroupid > qgroupid)
  105. n = n->rb_right;
  106. else
  107. return qgroup;
  108. }
  109. return NULL;
  110. }
  111. /* must be called with qgroup_lock held */
  112. static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
  113. u64 qgroupid)
  114. {
  115. struct rb_node **p = &fs_info->qgroup_tree.rb_node;
  116. struct rb_node *parent = NULL;
  117. struct btrfs_qgroup *qgroup;
  118. while (*p) {
  119. parent = *p;
  120. qgroup = rb_entry(parent, struct btrfs_qgroup, node);
  121. if (qgroup->qgroupid < qgroupid)
  122. p = &(*p)->rb_left;
  123. else if (qgroup->qgroupid > qgroupid)
  124. p = &(*p)->rb_right;
  125. else
  126. return qgroup;
  127. }
  128. qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
  129. if (!qgroup)
  130. return ERR_PTR(-ENOMEM);
  131. qgroup->qgroupid = qgroupid;
  132. INIT_LIST_HEAD(&qgroup->groups);
  133. INIT_LIST_HEAD(&qgroup->members);
  134. INIT_LIST_HEAD(&qgroup->dirty);
  135. rb_link_node(&qgroup->node, parent, p);
  136. rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
  137. return qgroup;
  138. }
  139. static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
  140. {
  141. struct btrfs_qgroup_list *list;
  142. list_del(&qgroup->dirty);
  143. while (!list_empty(&qgroup->groups)) {
  144. list = list_first_entry(&qgroup->groups,
  145. struct btrfs_qgroup_list, next_group);
  146. list_del(&list->next_group);
  147. list_del(&list->next_member);
  148. kfree(list);
  149. }
  150. while (!list_empty(&qgroup->members)) {
  151. list = list_first_entry(&qgroup->members,
  152. struct btrfs_qgroup_list, next_member);
  153. list_del(&list->next_group);
  154. list_del(&list->next_member);
  155. kfree(list);
  156. }
  157. kfree(qgroup);
  158. }
  159. /* must be called with qgroup_lock held */
  160. static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
  161. {
  162. struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
  163. if (!qgroup)
  164. return -ENOENT;
  165. rb_erase(&qgroup->node, &fs_info->qgroup_tree);
  166. __del_qgroup_rb(qgroup);
  167. return 0;
  168. }
  169. /* must be called with qgroup_lock held */
  170. static int add_relation_rb(struct btrfs_fs_info *fs_info,
  171. u64 memberid, u64 parentid)
  172. {
  173. struct btrfs_qgroup *member;
  174. struct btrfs_qgroup *parent;
  175. struct btrfs_qgroup_list *list;
  176. member = find_qgroup_rb(fs_info, memberid);
  177. parent = find_qgroup_rb(fs_info, parentid);
  178. if (!member || !parent)
  179. return -ENOENT;
  180. list = kzalloc(sizeof(*list), GFP_ATOMIC);
  181. if (!list)
  182. return -ENOMEM;
  183. list->group = parent;
  184. list->member = member;
  185. list_add_tail(&list->next_group, &member->groups);
  186. list_add_tail(&list->next_member, &parent->members);
  187. return 0;
  188. }
  189. /* must be called with qgroup_lock held */
  190. static int del_relation_rb(struct btrfs_fs_info *fs_info,
  191. u64 memberid, u64 parentid)
  192. {
  193. struct btrfs_qgroup *member;
  194. struct btrfs_qgroup *parent;
  195. struct btrfs_qgroup_list *list;
  196. member = find_qgroup_rb(fs_info, memberid);
  197. parent = find_qgroup_rb(fs_info, parentid);
  198. if (!member || !parent)
  199. return -ENOENT;
  200. list_for_each_entry(list, &member->groups, next_group) {
  201. if (list->group == parent) {
  202. list_del(&list->next_group);
  203. list_del(&list->next_member);
  204. kfree(list);
  205. return 0;
  206. }
  207. }
  208. return -ENOENT;
  209. }
  210. /*
  211. * The full config is read in one go, only called from open_ctree()
  212. * It doesn't use any locking, as at this point we're still single-threaded
  213. */
  214. int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
  215. {
  216. struct btrfs_key key;
  217. struct btrfs_key found_key;
  218. struct btrfs_root *quota_root = fs_info->quota_root;
  219. struct btrfs_path *path = NULL;
  220. struct extent_buffer *l;
  221. int slot;
  222. int ret = 0;
  223. u64 flags = 0;
  224. u64 rescan_progress = 0;
  225. if (!fs_info->quota_enabled)
  226. return 0;
  227. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  228. if (!fs_info->qgroup_ulist) {
  229. ret = -ENOMEM;
  230. goto out;
  231. }
  232. path = btrfs_alloc_path();
  233. if (!path) {
  234. ret = -ENOMEM;
  235. goto out;
  236. }
  237. /* default this to quota off, in case no status key is found */
  238. fs_info->qgroup_flags = 0;
  239. /*
  240. * pass 1: read status, all qgroup infos and limits
  241. */
  242. key.objectid = 0;
  243. key.type = 0;
  244. key.offset = 0;
  245. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
  246. if (ret)
  247. goto out;
  248. while (1) {
  249. struct btrfs_qgroup *qgroup;
  250. slot = path->slots[0];
  251. l = path->nodes[0];
  252. btrfs_item_key_to_cpu(l, &found_key, slot);
  253. if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
  254. struct btrfs_qgroup_status_item *ptr;
  255. ptr = btrfs_item_ptr(l, slot,
  256. struct btrfs_qgroup_status_item);
  257. if (btrfs_qgroup_status_version(l, ptr) !=
  258. BTRFS_QGROUP_STATUS_VERSION) {
  259. btrfs_err(fs_info,
  260. "old qgroup version, quota disabled");
  261. goto out;
  262. }
  263. if (btrfs_qgroup_status_generation(l, ptr) !=
  264. fs_info->generation) {
  265. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  266. btrfs_err(fs_info,
  267. "qgroup generation mismatch, "
  268. "marked as inconsistent");
  269. }
  270. fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
  271. ptr);
  272. rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
  273. goto next1;
  274. }
  275. if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
  276. found_key.type != BTRFS_QGROUP_LIMIT_KEY)
  277. goto next1;
  278. qgroup = find_qgroup_rb(fs_info, found_key.offset);
  279. if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
  280. (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
  281. btrfs_err(fs_info, "inconsitent qgroup config");
  282. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  283. }
  284. if (!qgroup) {
  285. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  286. if (IS_ERR(qgroup)) {
  287. ret = PTR_ERR(qgroup);
  288. goto out;
  289. }
  290. }
  291. switch (found_key.type) {
  292. case BTRFS_QGROUP_INFO_KEY: {
  293. struct btrfs_qgroup_info_item *ptr;
  294. ptr = btrfs_item_ptr(l, slot,
  295. struct btrfs_qgroup_info_item);
  296. qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
  297. qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
  298. qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
  299. qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
  300. /* generation currently unused */
  301. break;
  302. }
  303. case BTRFS_QGROUP_LIMIT_KEY: {
  304. struct btrfs_qgroup_limit_item *ptr;
  305. ptr = btrfs_item_ptr(l, slot,
  306. struct btrfs_qgroup_limit_item);
  307. qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
  308. qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
  309. qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
  310. qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
  311. qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
  312. break;
  313. }
  314. }
  315. next1:
  316. ret = btrfs_next_item(quota_root, path);
  317. if (ret < 0)
  318. goto out;
  319. if (ret)
  320. break;
  321. }
  322. btrfs_release_path(path);
  323. /*
  324. * pass 2: read all qgroup relations
  325. */
  326. key.objectid = 0;
  327. key.type = BTRFS_QGROUP_RELATION_KEY;
  328. key.offset = 0;
  329. ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
  330. if (ret)
  331. goto out;
  332. while (1) {
  333. slot = path->slots[0];
  334. l = path->nodes[0];
  335. btrfs_item_key_to_cpu(l, &found_key, slot);
  336. if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
  337. goto next2;
  338. if (found_key.objectid > found_key.offset) {
  339. /* parent <- member, not needed to build config */
  340. /* FIXME should we omit the key completely? */
  341. goto next2;
  342. }
  343. ret = add_relation_rb(fs_info, found_key.objectid,
  344. found_key.offset);
  345. if (ret == -ENOENT) {
  346. btrfs_warn(fs_info,
  347. "orphan qgroup relation 0x%llx->0x%llx",
  348. found_key.objectid, found_key.offset);
  349. ret = 0; /* ignore the error */
  350. }
  351. if (ret)
  352. goto out;
  353. next2:
  354. ret = btrfs_next_item(quota_root, path);
  355. if (ret < 0)
  356. goto out;
  357. if (ret)
  358. break;
  359. }
  360. out:
  361. fs_info->qgroup_flags |= flags;
  362. if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
  363. fs_info->quota_enabled = 0;
  364. fs_info->pending_quota_state = 0;
  365. } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
  366. ret >= 0) {
  367. ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
  368. }
  369. btrfs_free_path(path);
  370. if (ret < 0) {
  371. ulist_free(fs_info->qgroup_ulist);
  372. fs_info->qgroup_ulist = NULL;
  373. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  374. }
  375. return ret < 0 ? ret : 0;
  376. }
  377. /*
  378. * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
  379. * first two are in single-threaded paths.And for the third one, we have set
  380. * quota_root to be null with qgroup_lock held before, so it is safe to clean
  381. * up the in-memory structures without qgroup_lock held.
  382. */
  383. void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
  384. {
  385. struct rb_node *n;
  386. struct btrfs_qgroup *qgroup;
  387. while ((n = rb_first(&fs_info->qgroup_tree))) {
  388. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  389. rb_erase(n, &fs_info->qgroup_tree);
  390. __del_qgroup_rb(qgroup);
  391. }
  392. /*
  393. * we call btrfs_free_qgroup_config() when umounting
  394. * filesystem and disabling quota, so we set qgroup_ulit
  395. * to be null here to avoid double free.
  396. */
  397. ulist_free(fs_info->qgroup_ulist);
  398. fs_info->qgroup_ulist = NULL;
  399. }
  400. static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
  401. struct btrfs_root *quota_root,
  402. u64 src, u64 dst)
  403. {
  404. int ret;
  405. struct btrfs_path *path;
  406. struct btrfs_key key;
  407. path = btrfs_alloc_path();
  408. if (!path)
  409. return -ENOMEM;
  410. key.objectid = src;
  411. key.type = BTRFS_QGROUP_RELATION_KEY;
  412. key.offset = dst;
  413. ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
  414. btrfs_mark_buffer_dirty(path->nodes[0]);
  415. btrfs_free_path(path);
  416. return ret;
  417. }
  418. static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
  419. struct btrfs_root *quota_root,
  420. u64 src, u64 dst)
  421. {
  422. int ret;
  423. struct btrfs_path *path;
  424. struct btrfs_key key;
  425. path = btrfs_alloc_path();
  426. if (!path)
  427. return -ENOMEM;
  428. key.objectid = src;
  429. key.type = BTRFS_QGROUP_RELATION_KEY;
  430. key.offset = dst;
  431. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  432. if (ret < 0)
  433. goto out;
  434. if (ret > 0) {
  435. ret = -ENOENT;
  436. goto out;
  437. }
  438. ret = btrfs_del_item(trans, quota_root, path);
  439. out:
  440. btrfs_free_path(path);
  441. return ret;
  442. }
  443. static int add_qgroup_item(struct btrfs_trans_handle *trans,
  444. struct btrfs_root *quota_root, u64 qgroupid)
  445. {
  446. int ret;
  447. struct btrfs_path *path;
  448. struct btrfs_qgroup_info_item *qgroup_info;
  449. struct btrfs_qgroup_limit_item *qgroup_limit;
  450. struct extent_buffer *leaf;
  451. struct btrfs_key key;
  452. path = btrfs_alloc_path();
  453. if (!path)
  454. return -ENOMEM;
  455. key.objectid = 0;
  456. key.type = BTRFS_QGROUP_INFO_KEY;
  457. key.offset = qgroupid;
  458. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  459. sizeof(*qgroup_info));
  460. if (ret)
  461. goto out;
  462. leaf = path->nodes[0];
  463. qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
  464. struct btrfs_qgroup_info_item);
  465. btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
  466. btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
  467. btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
  468. btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
  469. btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
  470. btrfs_mark_buffer_dirty(leaf);
  471. btrfs_release_path(path);
  472. key.type = BTRFS_QGROUP_LIMIT_KEY;
  473. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  474. sizeof(*qgroup_limit));
  475. if (ret)
  476. goto out;
  477. leaf = path->nodes[0];
  478. qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
  479. struct btrfs_qgroup_limit_item);
  480. btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
  481. btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
  482. btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
  483. btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
  484. btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
  485. btrfs_mark_buffer_dirty(leaf);
  486. ret = 0;
  487. out:
  488. btrfs_free_path(path);
  489. return ret;
  490. }
  491. static int del_qgroup_item(struct btrfs_trans_handle *trans,
  492. struct btrfs_root *quota_root, u64 qgroupid)
  493. {
  494. int ret;
  495. struct btrfs_path *path;
  496. struct btrfs_key key;
  497. path = btrfs_alloc_path();
  498. if (!path)
  499. return -ENOMEM;
  500. key.objectid = 0;
  501. key.type = BTRFS_QGROUP_INFO_KEY;
  502. key.offset = qgroupid;
  503. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  504. if (ret < 0)
  505. goto out;
  506. if (ret > 0) {
  507. ret = -ENOENT;
  508. goto out;
  509. }
  510. ret = btrfs_del_item(trans, quota_root, path);
  511. if (ret)
  512. goto out;
  513. btrfs_release_path(path);
  514. key.type = BTRFS_QGROUP_LIMIT_KEY;
  515. ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
  516. if (ret < 0)
  517. goto out;
  518. if (ret > 0) {
  519. ret = -ENOENT;
  520. goto out;
  521. }
  522. ret = btrfs_del_item(trans, quota_root, path);
  523. out:
  524. btrfs_free_path(path);
  525. return ret;
  526. }
  527. static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
  528. struct btrfs_root *root, u64 qgroupid,
  529. u64 flags, u64 max_rfer, u64 max_excl,
  530. u64 rsv_rfer, u64 rsv_excl)
  531. {
  532. struct btrfs_path *path;
  533. struct btrfs_key key;
  534. struct extent_buffer *l;
  535. struct btrfs_qgroup_limit_item *qgroup_limit;
  536. int ret;
  537. int slot;
  538. key.objectid = 0;
  539. key.type = BTRFS_QGROUP_LIMIT_KEY;
  540. key.offset = qgroupid;
  541. path = btrfs_alloc_path();
  542. if (!path)
  543. return -ENOMEM;
  544. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  545. if (ret > 0)
  546. ret = -ENOENT;
  547. if (ret)
  548. goto out;
  549. l = path->nodes[0];
  550. slot = path->slots[0];
  551. qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
  552. btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
  553. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
  554. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
  555. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
  556. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
  557. btrfs_mark_buffer_dirty(l);
  558. out:
  559. btrfs_free_path(path);
  560. return ret;
  561. }
  562. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  563. struct btrfs_root *root,
  564. struct btrfs_qgroup *qgroup)
  565. {
  566. struct btrfs_path *path;
  567. struct btrfs_key key;
  568. struct extent_buffer *l;
  569. struct btrfs_qgroup_info_item *qgroup_info;
  570. int ret;
  571. int slot;
  572. key.objectid = 0;
  573. key.type = BTRFS_QGROUP_INFO_KEY;
  574. key.offset = qgroup->qgroupid;
  575. path = btrfs_alloc_path();
  576. if (!path)
  577. return -ENOMEM;
  578. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  579. if (ret > 0)
  580. ret = -ENOENT;
  581. if (ret)
  582. goto out;
  583. l = path->nodes[0];
  584. slot = path->slots[0];
  585. qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
  586. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  587. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  588. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  589. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  590. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  591. btrfs_mark_buffer_dirty(l);
  592. out:
  593. btrfs_free_path(path);
  594. return ret;
  595. }
  596. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  597. struct btrfs_fs_info *fs_info,
  598. struct btrfs_root *root)
  599. {
  600. struct btrfs_path *path;
  601. struct btrfs_key key;
  602. struct extent_buffer *l;
  603. struct btrfs_qgroup_status_item *ptr;
  604. int ret;
  605. int slot;
  606. key.objectid = 0;
  607. key.type = BTRFS_QGROUP_STATUS_KEY;
  608. key.offset = 0;
  609. path = btrfs_alloc_path();
  610. if (!path)
  611. return -ENOMEM;
  612. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  613. if (ret > 0)
  614. ret = -ENOENT;
  615. if (ret)
  616. goto out;
  617. l = path->nodes[0];
  618. slot = path->slots[0];
  619. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  620. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  621. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  622. btrfs_set_qgroup_status_rescan(l, ptr,
  623. fs_info->qgroup_rescan_progress.objectid);
  624. btrfs_mark_buffer_dirty(l);
  625. out:
  626. btrfs_free_path(path);
  627. return ret;
  628. }
  629. /*
  630. * called with qgroup_lock held
  631. */
  632. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  633. struct btrfs_root *root)
  634. {
  635. struct btrfs_path *path;
  636. struct btrfs_key key;
  637. struct extent_buffer *leaf = NULL;
  638. int ret;
  639. int nr = 0;
  640. path = btrfs_alloc_path();
  641. if (!path)
  642. return -ENOMEM;
  643. path->leave_spinning = 1;
  644. key.objectid = 0;
  645. key.offset = 0;
  646. key.type = 0;
  647. while (1) {
  648. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  649. if (ret < 0)
  650. goto out;
  651. leaf = path->nodes[0];
  652. nr = btrfs_header_nritems(leaf);
  653. if (!nr)
  654. break;
  655. /*
  656. * delete the leaf one by one
  657. * since the whole tree is going
  658. * to be deleted.
  659. */
  660. path->slots[0] = 0;
  661. ret = btrfs_del_items(trans, root, path, 0, nr);
  662. if (ret)
  663. goto out;
  664. btrfs_release_path(path);
  665. }
  666. ret = 0;
  667. out:
  668. root->fs_info->pending_quota_state = 0;
  669. btrfs_free_path(path);
  670. return ret;
  671. }
  672. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  673. struct btrfs_fs_info *fs_info)
  674. {
  675. struct btrfs_root *quota_root;
  676. struct btrfs_root *tree_root = fs_info->tree_root;
  677. struct btrfs_path *path = NULL;
  678. struct btrfs_qgroup_status_item *ptr;
  679. struct extent_buffer *leaf;
  680. struct btrfs_key key;
  681. struct btrfs_key found_key;
  682. struct btrfs_qgroup *qgroup = NULL;
  683. int ret = 0;
  684. int slot;
  685. mutex_lock(&fs_info->qgroup_ioctl_lock);
  686. if (fs_info->quota_root) {
  687. fs_info->pending_quota_state = 1;
  688. goto out;
  689. }
  690. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  691. if (!fs_info->qgroup_ulist) {
  692. ret = -ENOMEM;
  693. goto out;
  694. }
  695. /*
  696. * initially create the quota tree
  697. */
  698. quota_root = btrfs_create_tree(trans, fs_info,
  699. BTRFS_QUOTA_TREE_OBJECTID);
  700. if (IS_ERR(quota_root)) {
  701. ret = PTR_ERR(quota_root);
  702. goto out;
  703. }
  704. path = btrfs_alloc_path();
  705. if (!path) {
  706. ret = -ENOMEM;
  707. goto out_free_root;
  708. }
  709. key.objectid = 0;
  710. key.type = BTRFS_QGROUP_STATUS_KEY;
  711. key.offset = 0;
  712. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  713. sizeof(*ptr));
  714. if (ret)
  715. goto out_free_path;
  716. leaf = path->nodes[0];
  717. ptr = btrfs_item_ptr(leaf, path->slots[0],
  718. struct btrfs_qgroup_status_item);
  719. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  720. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  721. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  722. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  723. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  724. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  725. btrfs_mark_buffer_dirty(leaf);
  726. key.objectid = 0;
  727. key.type = BTRFS_ROOT_REF_KEY;
  728. key.offset = 0;
  729. btrfs_release_path(path);
  730. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  731. if (ret > 0)
  732. goto out_add_root;
  733. if (ret < 0)
  734. goto out_free_path;
  735. while (1) {
  736. slot = path->slots[0];
  737. leaf = path->nodes[0];
  738. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  739. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  740. ret = add_qgroup_item(trans, quota_root,
  741. found_key.offset);
  742. if (ret)
  743. goto out_free_path;
  744. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  745. if (IS_ERR(qgroup)) {
  746. ret = PTR_ERR(qgroup);
  747. goto out_free_path;
  748. }
  749. }
  750. ret = btrfs_next_item(tree_root, path);
  751. if (ret < 0)
  752. goto out_free_path;
  753. if (ret)
  754. break;
  755. }
  756. out_add_root:
  757. btrfs_release_path(path);
  758. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  759. if (ret)
  760. goto out_free_path;
  761. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  762. if (IS_ERR(qgroup)) {
  763. ret = PTR_ERR(qgroup);
  764. goto out_free_path;
  765. }
  766. spin_lock(&fs_info->qgroup_lock);
  767. fs_info->quota_root = quota_root;
  768. fs_info->pending_quota_state = 1;
  769. spin_unlock(&fs_info->qgroup_lock);
  770. out_free_path:
  771. btrfs_free_path(path);
  772. out_free_root:
  773. if (ret) {
  774. free_extent_buffer(quota_root->node);
  775. free_extent_buffer(quota_root->commit_root);
  776. kfree(quota_root);
  777. }
  778. out:
  779. if (ret) {
  780. ulist_free(fs_info->qgroup_ulist);
  781. fs_info->qgroup_ulist = NULL;
  782. }
  783. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  784. return ret;
  785. }
  786. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  787. struct btrfs_fs_info *fs_info)
  788. {
  789. struct btrfs_root *tree_root = fs_info->tree_root;
  790. struct btrfs_root *quota_root;
  791. int ret = 0;
  792. mutex_lock(&fs_info->qgroup_ioctl_lock);
  793. if (!fs_info->quota_root)
  794. goto out;
  795. spin_lock(&fs_info->qgroup_lock);
  796. fs_info->quota_enabled = 0;
  797. fs_info->pending_quota_state = 0;
  798. quota_root = fs_info->quota_root;
  799. fs_info->quota_root = NULL;
  800. spin_unlock(&fs_info->qgroup_lock);
  801. btrfs_free_qgroup_config(fs_info);
  802. ret = btrfs_clean_quota_tree(trans, quota_root);
  803. if (ret)
  804. goto out;
  805. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  806. if (ret)
  807. goto out;
  808. list_del(&quota_root->dirty_list);
  809. btrfs_tree_lock(quota_root->node);
  810. clean_tree_block(trans, tree_root, quota_root->node);
  811. btrfs_tree_unlock(quota_root->node);
  812. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  813. free_extent_buffer(quota_root->node);
  814. free_extent_buffer(quota_root->commit_root);
  815. kfree(quota_root);
  816. out:
  817. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  818. return ret;
  819. }
  820. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  821. struct btrfs_qgroup *qgroup)
  822. {
  823. if (list_empty(&qgroup->dirty))
  824. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  825. }
  826. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  827. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  828. {
  829. struct btrfs_root *quota_root;
  830. struct btrfs_qgroup *parent;
  831. struct btrfs_qgroup *member;
  832. struct btrfs_qgroup_list *list;
  833. int ret = 0;
  834. mutex_lock(&fs_info->qgroup_ioctl_lock);
  835. quota_root = fs_info->quota_root;
  836. if (!quota_root) {
  837. ret = -EINVAL;
  838. goto out;
  839. }
  840. member = find_qgroup_rb(fs_info, src);
  841. parent = find_qgroup_rb(fs_info, dst);
  842. if (!member || !parent) {
  843. ret = -EINVAL;
  844. goto out;
  845. }
  846. /* check if such qgroup relation exist firstly */
  847. list_for_each_entry(list, &member->groups, next_group) {
  848. if (list->group == parent) {
  849. ret = -EEXIST;
  850. goto out;
  851. }
  852. }
  853. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  854. if (ret)
  855. goto out;
  856. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  857. if (ret) {
  858. del_qgroup_relation_item(trans, quota_root, src, dst);
  859. goto out;
  860. }
  861. spin_lock(&fs_info->qgroup_lock);
  862. ret = add_relation_rb(quota_root->fs_info, src, dst);
  863. spin_unlock(&fs_info->qgroup_lock);
  864. out:
  865. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  866. return ret;
  867. }
  868. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  869. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  870. {
  871. struct btrfs_root *quota_root;
  872. struct btrfs_qgroup *parent;
  873. struct btrfs_qgroup *member;
  874. struct btrfs_qgroup_list *list;
  875. int ret = 0;
  876. int err;
  877. mutex_lock(&fs_info->qgroup_ioctl_lock);
  878. quota_root = fs_info->quota_root;
  879. if (!quota_root) {
  880. ret = -EINVAL;
  881. goto out;
  882. }
  883. member = find_qgroup_rb(fs_info, src);
  884. parent = find_qgroup_rb(fs_info, dst);
  885. if (!member || !parent) {
  886. ret = -EINVAL;
  887. goto out;
  888. }
  889. /* check if such qgroup relation exist firstly */
  890. list_for_each_entry(list, &member->groups, next_group) {
  891. if (list->group == parent)
  892. goto exist;
  893. }
  894. ret = -ENOENT;
  895. goto out;
  896. exist:
  897. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  898. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  899. if (err && !ret)
  900. ret = err;
  901. spin_lock(&fs_info->qgroup_lock);
  902. del_relation_rb(fs_info, src, dst);
  903. spin_unlock(&fs_info->qgroup_lock);
  904. out:
  905. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  906. return ret;
  907. }
  908. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  909. struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
  910. {
  911. struct btrfs_root *quota_root;
  912. struct btrfs_qgroup *qgroup;
  913. int ret = 0;
  914. mutex_lock(&fs_info->qgroup_ioctl_lock);
  915. quota_root = fs_info->quota_root;
  916. if (!quota_root) {
  917. ret = -EINVAL;
  918. goto out;
  919. }
  920. qgroup = find_qgroup_rb(fs_info, qgroupid);
  921. if (qgroup) {
  922. ret = -EEXIST;
  923. goto out;
  924. }
  925. ret = add_qgroup_item(trans, quota_root, qgroupid);
  926. if (ret)
  927. goto out;
  928. spin_lock(&fs_info->qgroup_lock);
  929. qgroup = add_qgroup_rb(fs_info, qgroupid);
  930. spin_unlock(&fs_info->qgroup_lock);
  931. if (IS_ERR(qgroup))
  932. ret = PTR_ERR(qgroup);
  933. out:
  934. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  935. return ret;
  936. }
  937. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  938. struct btrfs_fs_info *fs_info, u64 qgroupid)
  939. {
  940. struct btrfs_root *quota_root;
  941. struct btrfs_qgroup *qgroup;
  942. int ret = 0;
  943. mutex_lock(&fs_info->qgroup_ioctl_lock);
  944. quota_root = fs_info->quota_root;
  945. if (!quota_root) {
  946. ret = -EINVAL;
  947. goto out;
  948. }
  949. qgroup = find_qgroup_rb(fs_info, qgroupid);
  950. if (!qgroup) {
  951. ret = -ENOENT;
  952. goto out;
  953. } else {
  954. /* check if there are no relations to this qgroup */
  955. if (!list_empty(&qgroup->groups) ||
  956. !list_empty(&qgroup->members)) {
  957. ret = -EBUSY;
  958. goto out;
  959. }
  960. }
  961. ret = del_qgroup_item(trans, quota_root, qgroupid);
  962. spin_lock(&fs_info->qgroup_lock);
  963. del_qgroup_rb(quota_root->fs_info, qgroupid);
  964. spin_unlock(&fs_info->qgroup_lock);
  965. out:
  966. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  967. return ret;
  968. }
  969. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  970. struct btrfs_fs_info *fs_info, u64 qgroupid,
  971. struct btrfs_qgroup_limit *limit)
  972. {
  973. struct btrfs_root *quota_root;
  974. struct btrfs_qgroup *qgroup;
  975. int ret = 0;
  976. mutex_lock(&fs_info->qgroup_ioctl_lock);
  977. quota_root = fs_info->quota_root;
  978. if (!quota_root) {
  979. ret = -EINVAL;
  980. goto out;
  981. }
  982. qgroup = find_qgroup_rb(fs_info, qgroupid);
  983. if (!qgroup) {
  984. ret = -ENOENT;
  985. goto out;
  986. }
  987. ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
  988. limit->flags, limit->max_rfer,
  989. limit->max_excl, limit->rsv_rfer,
  990. limit->rsv_excl);
  991. if (ret) {
  992. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  993. btrfs_info(fs_info, "unable to update quota limit for %llu",
  994. qgroupid);
  995. }
  996. spin_lock(&fs_info->qgroup_lock);
  997. qgroup->lim_flags = limit->flags;
  998. qgroup->max_rfer = limit->max_rfer;
  999. qgroup->max_excl = limit->max_excl;
  1000. qgroup->rsv_rfer = limit->rsv_rfer;
  1001. qgroup->rsv_excl = limit->rsv_excl;
  1002. spin_unlock(&fs_info->qgroup_lock);
  1003. out:
  1004. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1005. return ret;
  1006. }
  1007. /*
  1008. * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
  1009. * the modification into a list that's later used by btrfs_end_transaction to
  1010. * pass the recorded modifications on to btrfs_qgroup_account_ref.
  1011. */
  1012. int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
  1013. struct btrfs_delayed_ref_node *node,
  1014. struct btrfs_delayed_extent_op *extent_op)
  1015. {
  1016. struct qgroup_update *u;
  1017. BUG_ON(!trans->delayed_ref_elem.seq);
  1018. u = kmalloc(sizeof(*u), GFP_NOFS);
  1019. if (!u)
  1020. return -ENOMEM;
  1021. u->node = node;
  1022. u->extent_op = extent_op;
  1023. list_add_tail(&u->list, &trans->qgroup_ref_list);
  1024. return 0;
  1025. }
  1026. static int qgroup_account_ref_step1(struct btrfs_fs_info *fs_info,
  1027. struct ulist *roots, struct ulist *tmp,
  1028. u64 seq)
  1029. {
  1030. struct ulist_node *unode;
  1031. struct ulist_iterator uiter;
  1032. struct ulist_node *tmp_unode;
  1033. struct ulist_iterator tmp_uiter;
  1034. struct btrfs_qgroup *qg;
  1035. int ret;
  1036. ULIST_ITER_INIT(&uiter);
  1037. while ((unode = ulist_next(roots, &uiter))) {
  1038. qg = find_qgroup_rb(fs_info, unode->val);
  1039. if (!qg)
  1040. continue;
  1041. ulist_reinit(tmp);
  1042. /* XXX id not needed */
  1043. ret = ulist_add(tmp, qg->qgroupid,
  1044. (u64)(uintptr_t)qg, GFP_ATOMIC);
  1045. if (ret < 0)
  1046. return ret;
  1047. ULIST_ITER_INIT(&tmp_uiter);
  1048. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1049. struct btrfs_qgroup_list *glist;
  1050. qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
  1051. if (qg->refcnt < seq)
  1052. qg->refcnt = seq + 1;
  1053. else
  1054. ++qg->refcnt;
  1055. list_for_each_entry(glist, &qg->groups, next_group) {
  1056. ret = ulist_add(tmp, glist->group->qgroupid,
  1057. (u64)(uintptr_t)glist->group,
  1058. GFP_ATOMIC);
  1059. if (ret < 0)
  1060. return ret;
  1061. }
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. static int qgroup_account_ref_step2(struct btrfs_fs_info *fs_info,
  1067. struct ulist *roots, struct ulist *tmp,
  1068. u64 seq, int sgn, u64 num_bytes,
  1069. struct btrfs_qgroup *qgroup)
  1070. {
  1071. struct ulist_node *unode;
  1072. struct ulist_iterator uiter;
  1073. struct btrfs_qgroup *qg;
  1074. struct btrfs_qgroup_list *glist;
  1075. int ret;
  1076. ulist_reinit(tmp);
  1077. ret = ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
  1078. if (ret < 0)
  1079. return ret;
  1080. ULIST_ITER_INIT(&uiter);
  1081. while ((unode = ulist_next(tmp, &uiter))) {
  1082. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1083. if (qg->refcnt < seq) {
  1084. /* not visited by step 1 */
  1085. qg->rfer += sgn * num_bytes;
  1086. qg->rfer_cmpr += sgn * num_bytes;
  1087. if (roots->nnodes == 0) {
  1088. qg->excl += sgn * num_bytes;
  1089. qg->excl_cmpr += sgn * num_bytes;
  1090. }
  1091. qgroup_dirty(fs_info, qg);
  1092. }
  1093. WARN_ON(qg->tag >= seq);
  1094. qg->tag = seq;
  1095. list_for_each_entry(glist, &qg->groups, next_group) {
  1096. ret = ulist_add(tmp, glist->group->qgroupid,
  1097. (uintptr_t)glist->group, GFP_ATOMIC);
  1098. if (ret < 0)
  1099. return ret;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. static int qgroup_account_ref_step3(struct btrfs_fs_info *fs_info,
  1105. struct ulist *roots, struct ulist *tmp,
  1106. u64 seq, int sgn, u64 num_bytes)
  1107. {
  1108. struct ulist_node *unode;
  1109. struct ulist_iterator uiter;
  1110. struct btrfs_qgroup *qg;
  1111. struct ulist_node *tmp_unode;
  1112. struct ulist_iterator tmp_uiter;
  1113. int ret;
  1114. ULIST_ITER_INIT(&uiter);
  1115. while ((unode = ulist_next(roots, &uiter))) {
  1116. qg = find_qgroup_rb(fs_info, unode->val);
  1117. if (!qg)
  1118. continue;
  1119. ulist_reinit(tmp);
  1120. ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
  1121. if (ret < 0)
  1122. return ret;
  1123. ULIST_ITER_INIT(&tmp_uiter);
  1124. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1125. struct btrfs_qgroup_list *glist;
  1126. qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
  1127. if (qg->tag == seq)
  1128. continue;
  1129. if (qg->refcnt - seq == roots->nnodes) {
  1130. qg->excl -= sgn * num_bytes;
  1131. qg->excl_cmpr -= sgn * num_bytes;
  1132. qgroup_dirty(fs_info, qg);
  1133. }
  1134. list_for_each_entry(glist, &qg->groups, next_group) {
  1135. ret = ulist_add(tmp, glist->group->qgroupid,
  1136. (uintptr_t)glist->group,
  1137. GFP_ATOMIC);
  1138. if (ret < 0)
  1139. return ret;
  1140. }
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. /*
  1146. * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
  1147. * from the fs. First, all roots referencing the extent are searched, and
  1148. * then the space is accounted accordingly to the different roots. The
  1149. * accounting algorithm works in 3 steps documented inline.
  1150. */
  1151. int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
  1152. struct btrfs_fs_info *fs_info,
  1153. struct btrfs_delayed_ref_node *node,
  1154. struct btrfs_delayed_extent_op *extent_op)
  1155. {
  1156. struct btrfs_root *quota_root;
  1157. u64 ref_root;
  1158. struct btrfs_qgroup *qgroup;
  1159. struct ulist *roots = NULL;
  1160. u64 seq;
  1161. int ret = 0;
  1162. int sgn;
  1163. if (!fs_info->quota_enabled)
  1164. return 0;
  1165. BUG_ON(!fs_info->quota_root);
  1166. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1167. node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1168. struct btrfs_delayed_tree_ref *ref;
  1169. ref = btrfs_delayed_node_to_tree_ref(node);
  1170. ref_root = ref->root;
  1171. } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1172. node->type == BTRFS_SHARED_DATA_REF_KEY) {
  1173. struct btrfs_delayed_data_ref *ref;
  1174. ref = btrfs_delayed_node_to_data_ref(node);
  1175. ref_root = ref->root;
  1176. } else {
  1177. BUG();
  1178. }
  1179. if (!is_fstree(ref_root)) {
  1180. /*
  1181. * non-fs-trees are not being accounted
  1182. */
  1183. return 0;
  1184. }
  1185. switch (node->action) {
  1186. case BTRFS_ADD_DELAYED_REF:
  1187. case BTRFS_ADD_DELAYED_EXTENT:
  1188. sgn = 1;
  1189. seq = btrfs_tree_mod_seq_prev(node->seq);
  1190. break;
  1191. case BTRFS_DROP_DELAYED_REF:
  1192. sgn = -1;
  1193. seq = node->seq;
  1194. break;
  1195. case BTRFS_UPDATE_DELAYED_HEAD:
  1196. return 0;
  1197. default:
  1198. BUG();
  1199. }
  1200. mutex_lock(&fs_info->qgroup_rescan_lock);
  1201. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1202. if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
  1203. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1204. return 0;
  1205. }
  1206. }
  1207. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1208. /*
  1209. * the delayed ref sequence number we pass depends on the direction of
  1210. * the operation. for add operations, we pass
  1211. * tree_mod_log_prev_seq(node->seq) to skip
  1212. * the delayed ref's current sequence number, because we need the state
  1213. * of the tree before the add operation. for delete operations, we pass
  1214. * (node->seq) to include the delayed ref's current sequence number,
  1215. * because we need the state of the tree after the delete operation.
  1216. */
  1217. ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, seq, &roots);
  1218. if (ret < 0)
  1219. return ret;
  1220. spin_lock(&fs_info->qgroup_lock);
  1221. quota_root = fs_info->quota_root;
  1222. if (!quota_root)
  1223. goto unlock;
  1224. qgroup = find_qgroup_rb(fs_info, ref_root);
  1225. if (!qgroup)
  1226. goto unlock;
  1227. /*
  1228. * step 1: for each old ref, visit all nodes once and inc refcnt
  1229. */
  1230. ulist_reinit(fs_info->qgroup_ulist);
  1231. seq = fs_info->qgroup_seq;
  1232. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1233. ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
  1234. seq);
  1235. if (ret)
  1236. goto unlock;
  1237. /*
  1238. * step 2: walk from the new root
  1239. */
  1240. ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
  1241. seq, sgn, node->num_bytes, qgroup);
  1242. if (ret)
  1243. goto unlock;
  1244. /*
  1245. * step 3: walk again from old refs
  1246. */
  1247. ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
  1248. seq, sgn, node->num_bytes);
  1249. if (ret)
  1250. goto unlock;
  1251. unlock:
  1252. spin_unlock(&fs_info->qgroup_lock);
  1253. ulist_free(roots);
  1254. return ret;
  1255. }
  1256. /*
  1257. * called from commit_transaction. Writes all changed qgroups to disk.
  1258. */
  1259. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1260. struct btrfs_fs_info *fs_info)
  1261. {
  1262. struct btrfs_root *quota_root = fs_info->quota_root;
  1263. int ret = 0;
  1264. int start_rescan_worker = 0;
  1265. if (!quota_root)
  1266. goto out;
  1267. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1268. start_rescan_worker = 1;
  1269. fs_info->quota_enabled = fs_info->pending_quota_state;
  1270. spin_lock(&fs_info->qgroup_lock);
  1271. while (!list_empty(&fs_info->dirty_qgroups)) {
  1272. struct btrfs_qgroup *qgroup;
  1273. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1274. struct btrfs_qgroup, dirty);
  1275. list_del_init(&qgroup->dirty);
  1276. spin_unlock(&fs_info->qgroup_lock);
  1277. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1278. if (ret)
  1279. fs_info->qgroup_flags |=
  1280. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1281. spin_lock(&fs_info->qgroup_lock);
  1282. }
  1283. if (fs_info->quota_enabled)
  1284. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1285. else
  1286. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1287. spin_unlock(&fs_info->qgroup_lock);
  1288. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1289. if (ret)
  1290. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1291. if (!ret && start_rescan_worker) {
  1292. ret = qgroup_rescan_init(fs_info, 0, 1);
  1293. if (!ret) {
  1294. qgroup_rescan_zero_tracking(fs_info);
  1295. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1296. &fs_info->qgroup_rescan_work);
  1297. }
  1298. ret = 0;
  1299. }
  1300. out:
  1301. return ret;
  1302. }
  1303. /*
  1304. * copy the acounting information between qgroups. This is necessary when a
  1305. * snapshot or a subvolume is created
  1306. */
  1307. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1308. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1309. struct btrfs_qgroup_inherit *inherit)
  1310. {
  1311. int ret = 0;
  1312. int i;
  1313. u64 *i_qgroups;
  1314. struct btrfs_root *quota_root = fs_info->quota_root;
  1315. struct btrfs_qgroup *srcgroup;
  1316. struct btrfs_qgroup *dstgroup;
  1317. u32 level_size = 0;
  1318. u64 nums;
  1319. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1320. if (!fs_info->quota_enabled)
  1321. goto out;
  1322. if (!quota_root) {
  1323. ret = -EINVAL;
  1324. goto out;
  1325. }
  1326. if (inherit) {
  1327. i_qgroups = (u64 *)(inherit + 1);
  1328. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1329. 2 * inherit->num_excl_copies;
  1330. for (i = 0; i < nums; ++i) {
  1331. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1332. if (!srcgroup) {
  1333. ret = -EINVAL;
  1334. goto out;
  1335. }
  1336. ++i_qgroups;
  1337. }
  1338. }
  1339. /*
  1340. * create a tracking group for the subvol itself
  1341. */
  1342. ret = add_qgroup_item(trans, quota_root, objectid);
  1343. if (ret)
  1344. goto out;
  1345. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1346. ret = update_qgroup_limit_item(trans, quota_root, objectid,
  1347. inherit->lim.flags,
  1348. inherit->lim.max_rfer,
  1349. inherit->lim.max_excl,
  1350. inherit->lim.rsv_rfer,
  1351. inherit->lim.rsv_excl);
  1352. if (ret)
  1353. goto out;
  1354. }
  1355. if (srcid) {
  1356. struct btrfs_root *srcroot;
  1357. struct btrfs_key srckey;
  1358. int srcroot_level;
  1359. srckey.objectid = srcid;
  1360. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1361. srckey.offset = (u64)-1;
  1362. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1363. if (IS_ERR(srcroot)) {
  1364. ret = PTR_ERR(srcroot);
  1365. goto out;
  1366. }
  1367. rcu_read_lock();
  1368. srcroot_level = btrfs_header_level(srcroot->node);
  1369. level_size = btrfs_level_size(srcroot, srcroot_level);
  1370. rcu_read_unlock();
  1371. }
  1372. /*
  1373. * add qgroup to all inherited groups
  1374. */
  1375. if (inherit) {
  1376. i_qgroups = (u64 *)(inherit + 1);
  1377. for (i = 0; i < inherit->num_qgroups; ++i) {
  1378. ret = add_qgroup_relation_item(trans, quota_root,
  1379. objectid, *i_qgroups);
  1380. if (ret)
  1381. goto out;
  1382. ret = add_qgroup_relation_item(trans, quota_root,
  1383. *i_qgroups, objectid);
  1384. if (ret)
  1385. goto out;
  1386. ++i_qgroups;
  1387. }
  1388. }
  1389. spin_lock(&fs_info->qgroup_lock);
  1390. dstgroup = add_qgroup_rb(fs_info, objectid);
  1391. if (IS_ERR(dstgroup)) {
  1392. ret = PTR_ERR(dstgroup);
  1393. goto unlock;
  1394. }
  1395. if (srcid) {
  1396. srcgroup = find_qgroup_rb(fs_info, srcid);
  1397. if (!srcgroup)
  1398. goto unlock;
  1399. dstgroup->rfer = srcgroup->rfer - level_size;
  1400. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
  1401. srcgroup->excl = level_size;
  1402. srcgroup->excl_cmpr = level_size;
  1403. qgroup_dirty(fs_info, dstgroup);
  1404. qgroup_dirty(fs_info, srcgroup);
  1405. }
  1406. if (!inherit)
  1407. goto unlock;
  1408. i_qgroups = (u64 *)(inherit + 1);
  1409. for (i = 0; i < inherit->num_qgroups; ++i) {
  1410. ret = add_relation_rb(quota_root->fs_info, objectid,
  1411. *i_qgroups);
  1412. if (ret)
  1413. goto unlock;
  1414. ++i_qgroups;
  1415. }
  1416. for (i = 0; i < inherit->num_ref_copies; ++i) {
  1417. struct btrfs_qgroup *src;
  1418. struct btrfs_qgroup *dst;
  1419. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1420. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1421. if (!src || !dst) {
  1422. ret = -EINVAL;
  1423. goto unlock;
  1424. }
  1425. dst->rfer = src->rfer - level_size;
  1426. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1427. i_qgroups += 2;
  1428. }
  1429. for (i = 0; i < inherit->num_excl_copies; ++i) {
  1430. struct btrfs_qgroup *src;
  1431. struct btrfs_qgroup *dst;
  1432. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1433. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1434. if (!src || !dst) {
  1435. ret = -EINVAL;
  1436. goto unlock;
  1437. }
  1438. dst->excl = src->excl + level_size;
  1439. dst->excl_cmpr = src->excl_cmpr + level_size;
  1440. i_qgroups += 2;
  1441. }
  1442. unlock:
  1443. spin_unlock(&fs_info->qgroup_lock);
  1444. out:
  1445. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1446. return ret;
  1447. }
  1448. /*
  1449. * reserve some space for a qgroup and all its parents. The reservation takes
  1450. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  1451. * accounting. If not enough space is available, EDQUOT is returned.
  1452. * We assume that the requested space is new for all qgroups.
  1453. */
  1454. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1455. {
  1456. struct btrfs_root *quota_root;
  1457. struct btrfs_qgroup *qgroup;
  1458. struct btrfs_fs_info *fs_info = root->fs_info;
  1459. u64 ref_root = root->root_key.objectid;
  1460. int ret = 0;
  1461. struct ulist_node *unode;
  1462. struct ulist_iterator uiter;
  1463. if (!is_fstree(ref_root))
  1464. return 0;
  1465. if (num_bytes == 0)
  1466. return 0;
  1467. spin_lock(&fs_info->qgroup_lock);
  1468. quota_root = fs_info->quota_root;
  1469. if (!quota_root)
  1470. goto out;
  1471. qgroup = find_qgroup_rb(fs_info, ref_root);
  1472. if (!qgroup)
  1473. goto out;
  1474. /*
  1475. * in a first step, we check all affected qgroups if any limits would
  1476. * be exceeded
  1477. */
  1478. ulist_reinit(fs_info->qgroup_ulist);
  1479. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1480. (uintptr_t)qgroup, GFP_ATOMIC);
  1481. if (ret < 0)
  1482. goto out;
  1483. ULIST_ITER_INIT(&uiter);
  1484. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1485. struct btrfs_qgroup *qg;
  1486. struct btrfs_qgroup_list *glist;
  1487. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1488. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1489. qg->reserved + (s64)qg->rfer + num_bytes >
  1490. qg->max_rfer) {
  1491. ret = -EDQUOT;
  1492. goto out;
  1493. }
  1494. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1495. qg->reserved + (s64)qg->excl + num_bytes >
  1496. qg->max_excl) {
  1497. ret = -EDQUOT;
  1498. goto out;
  1499. }
  1500. list_for_each_entry(glist, &qg->groups, next_group) {
  1501. ret = ulist_add(fs_info->qgroup_ulist,
  1502. glist->group->qgroupid,
  1503. (uintptr_t)glist->group, GFP_ATOMIC);
  1504. if (ret < 0)
  1505. goto out;
  1506. }
  1507. }
  1508. ret = 0;
  1509. /*
  1510. * no limits exceeded, now record the reservation into all qgroups
  1511. */
  1512. ULIST_ITER_INIT(&uiter);
  1513. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1514. struct btrfs_qgroup *qg;
  1515. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1516. qg->reserved += num_bytes;
  1517. }
  1518. out:
  1519. spin_unlock(&fs_info->qgroup_lock);
  1520. return ret;
  1521. }
  1522. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1523. {
  1524. struct btrfs_root *quota_root;
  1525. struct btrfs_qgroup *qgroup;
  1526. struct btrfs_fs_info *fs_info = root->fs_info;
  1527. struct ulist_node *unode;
  1528. struct ulist_iterator uiter;
  1529. u64 ref_root = root->root_key.objectid;
  1530. int ret = 0;
  1531. if (!is_fstree(ref_root))
  1532. return;
  1533. if (num_bytes == 0)
  1534. return;
  1535. spin_lock(&fs_info->qgroup_lock);
  1536. quota_root = fs_info->quota_root;
  1537. if (!quota_root)
  1538. goto out;
  1539. qgroup = find_qgroup_rb(fs_info, ref_root);
  1540. if (!qgroup)
  1541. goto out;
  1542. ulist_reinit(fs_info->qgroup_ulist);
  1543. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1544. (uintptr_t)qgroup, GFP_ATOMIC);
  1545. if (ret < 0)
  1546. goto out;
  1547. ULIST_ITER_INIT(&uiter);
  1548. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1549. struct btrfs_qgroup *qg;
  1550. struct btrfs_qgroup_list *glist;
  1551. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1552. qg->reserved -= num_bytes;
  1553. list_for_each_entry(glist, &qg->groups, next_group) {
  1554. ret = ulist_add(fs_info->qgroup_ulist,
  1555. glist->group->qgroupid,
  1556. (uintptr_t)glist->group, GFP_ATOMIC);
  1557. if (ret < 0)
  1558. goto out;
  1559. }
  1560. }
  1561. out:
  1562. spin_unlock(&fs_info->qgroup_lock);
  1563. }
  1564. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1565. {
  1566. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1567. return;
  1568. btrfs_err(trans->root->fs_info,
  1569. "qgroups not uptodate in trans handle %p: list is%s empty, "
  1570. "seq is %#x.%x",
  1571. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1572. (u32)(trans->delayed_ref_elem.seq >> 32),
  1573. (u32)trans->delayed_ref_elem.seq);
  1574. BUG();
  1575. }
  1576. /*
  1577. * returns < 0 on error, 0 when more leafs are to be scanned.
  1578. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  1579. */
  1580. static int
  1581. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1582. struct btrfs_trans_handle *trans, struct ulist *tmp,
  1583. struct extent_buffer *scratch_leaf)
  1584. {
  1585. struct btrfs_key found;
  1586. struct ulist *roots = NULL;
  1587. struct ulist_node *unode;
  1588. struct ulist_iterator uiter;
  1589. struct seq_list tree_mod_seq_elem = {};
  1590. u64 seq;
  1591. int slot;
  1592. int ret;
  1593. path->leave_spinning = 1;
  1594. mutex_lock(&fs_info->qgroup_rescan_lock);
  1595. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1596. &fs_info->qgroup_rescan_progress,
  1597. path, 1, 0);
  1598. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  1599. fs_info->qgroup_rescan_progress.objectid,
  1600. fs_info->qgroup_rescan_progress.type,
  1601. fs_info->qgroup_rescan_progress.offset, ret);
  1602. if (ret) {
  1603. /*
  1604. * The rescan is about to end, we will not be scanning any
  1605. * further blocks. We cannot unset the RESCAN flag here, because
  1606. * we want to commit the transaction if everything went well.
  1607. * To make the live accounting work in this phase, we set our
  1608. * scan progress pointer such that every real extent objectid
  1609. * will be smaller.
  1610. */
  1611. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1612. btrfs_release_path(path);
  1613. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1614. return ret;
  1615. }
  1616. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1617. btrfs_header_nritems(path->nodes[0]) - 1);
  1618. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1619. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1620. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  1621. slot = path->slots[0];
  1622. btrfs_release_path(path);
  1623. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1624. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1625. u64 num_bytes;
  1626. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1627. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  1628. found.type != BTRFS_METADATA_ITEM_KEY)
  1629. continue;
  1630. if (found.type == BTRFS_METADATA_ITEM_KEY)
  1631. num_bytes = fs_info->extent_root->leafsize;
  1632. else
  1633. num_bytes = found.offset;
  1634. ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
  1635. tree_mod_seq_elem.seq, &roots);
  1636. if (ret < 0)
  1637. goto out;
  1638. spin_lock(&fs_info->qgroup_lock);
  1639. seq = fs_info->qgroup_seq;
  1640. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1641. ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
  1642. if (ret) {
  1643. spin_unlock(&fs_info->qgroup_lock);
  1644. ulist_free(roots);
  1645. goto out;
  1646. }
  1647. /*
  1648. * step2 of btrfs_qgroup_account_ref works from a single root,
  1649. * we're doing all at once here.
  1650. */
  1651. ulist_reinit(tmp);
  1652. ULIST_ITER_INIT(&uiter);
  1653. while ((unode = ulist_next(roots, &uiter))) {
  1654. struct btrfs_qgroup *qg;
  1655. qg = find_qgroup_rb(fs_info, unode->val);
  1656. if (!qg)
  1657. continue;
  1658. ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
  1659. GFP_ATOMIC);
  1660. if (ret < 0) {
  1661. spin_unlock(&fs_info->qgroup_lock);
  1662. ulist_free(roots);
  1663. goto out;
  1664. }
  1665. }
  1666. /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
  1667. ULIST_ITER_INIT(&uiter);
  1668. while ((unode = ulist_next(tmp, &uiter))) {
  1669. struct btrfs_qgroup *qg;
  1670. struct btrfs_qgroup_list *glist;
  1671. qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
  1672. qg->rfer += num_bytes;
  1673. qg->rfer_cmpr += num_bytes;
  1674. WARN_ON(qg->tag >= seq);
  1675. if (qg->refcnt - seq == roots->nnodes) {
  1676. qg->excl += num_bytes;
  1677. qg->excl_cmpr += num_bytes;
  1678. }
  1679. qgroup_dirty(fs_info, qg);
  1680. list_for_each_entry(glist, &qg->groups, next_group) {
  1681. ret = ulist_add(tmp, glist->group->qgroupid,
  1682. (uintptr_t)glist->group,
  1683. GFP_ATOMIC);
  1684. if (ret < 0) {
  1685. spin_unlock(&fs_info->qgroup_lock);
  1686. ulist_free(roots);
  1687. goto out;
  1688. }
  1689. }
  1690. }
  1691. spin_unlock(&fs_info->qgroup_lock);
  1692. ulist_free(roots);
  1693. ret = 0;
  1694. }
  1695. out:
  1696. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1697. return ret;
  1698. }
  1699. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  1700. {
  1701. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  1702. qgroup_rescan_work);
  1703. struct btrfs_path *path;
  1704. struct btrfs_trans_handle *trans = NULL;
  1705. struct ulist *tmp = NULL;
  1706. struct extent_buffer *scratch_leaf = NULL;
  1707. int err = -ENOMEM;
  1708. path = btrfs_alloc_path();
  1709. if (!path)
  1710. goto out;
  1711. tmp = ulist_alloc(GFP_NOFS);
  1712. if (!tmp)
  1713. goto out;
  1714. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  1715. if (!scratch_leaf)
  1716. goto out;
  1717. err = 0;
  1718. while (!err) {
  1719. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  1720. if (IS_ERR(trans)) {
  1721. err = PTR_ERR(trans);
  1722. break;
  1723. }
  1724. if (!fs_info->quota_enabled) {
  1725. err = -EINTR;
  1726. } else {
  1727. err = qgroup_rescan_leaf(fs_info, path, trans,
  1728. tmp, scratch_leaf);
  1729. }
  1730. if (err > 0)
  1731. btrfs_commit_transaction(trans, fs_info->fs_root);
  1732. else
  1733. btrfs_end_transaction(trans, fs_info->fs_root);
  1734. }
  1735. out:
  1736. kfree(scratch_leaf);
  1737. ulist_free(tmp);
  1738. btrfs_free_path(path);
  1739. mutex_lock(&fs_info->qgroup_rescan_lock);
  1740. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1741. if (err == 2 &&
  1742. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  1743. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1744. } else if (err < 0) {
  1745. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1746. }
  1747. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1748. if (err >= 0) {
  1749. btrfs_info(fs_info, "qgroup scan completed%s",
  1750. err == 2 ? " (inconsistency flag cleared)" : "");
  1751. } else {
  1752. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  1753. }
  1754. complete_all(&fs_info->qgroup_rescan_completion);
  1755. }
  1756. /*
  1757. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  1758. * memory required for the rescan context.
  1759. */
  1760. static int
  1761. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  1762. int init_flags)
  1763. {
  1764. int ret = 0;
  1765. if (!init_flags &&
  1766. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  1767. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  1768. ret = -EINVAL;
  1769. goto err;
  1770. }
  1771. mutex_lock(&fs_info->qgroup_rescan_lock);
  1772. spin_lock(&fs_info->qgroup_lock);
  1773. if (init_flags) {
  1774. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  1775. ret = -EINPROGRESS;
  1776. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  1777. ret = -EINVAL;
  1778. if (ret) {
  1779. spin_unlock(&fs_info->qgroup_lock);
  1780. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1781. goto err;
  1782. }
  1783. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1784. }
  1785. memset(&fs_info->qgroup_rescan_progress, 0,
  1786. sizeof(fs_info->qgroup_rescan_progress));
  1787. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  1788. spin_unlock(&fs_info->qgroup_lock);
  1789. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1790. init_completion(&fs_info->qgroup_rescan_completion);
  1791. memset(&fs_info->qgroup_rescan_work, 0,
  1792. sizeof(fs_info->qgroup_rescan_work));
  1793. fs_info->qgroup_rescan_work.func = btrfs_qgroup_rescan_worker;
  1794. if (ret) {
  1795. err:
  1796. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  1797. return ret;
  1798. }
  1799. return 0;
  1800. }
  1801. static void
  1802. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  1803. {
  1804. struct rb_node *n;
  1805. struct btrfs_qgroup *qgroup;
  1806. spin_lock(&fs_info->qgroup_lock);
  1807. /* clear all current qgroup tracking information */
  1808. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  1809. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  1810. qgroup->rfer = 0;
  1811. qgroup->rfer_cmpr = 0;
  1812. qgroup->excl = 0;
  1813. qgroup->excl_cmpr = 0;
  1814. }
  1815. spin_unlock(&fs_info->qgroup_lock);
  1816. }
  1817. int
  1818. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  1819. {
  1820. int ret = 0;
  1821. struct btrfs_trans_handle *trans;
  1822. ret = qgroup_rescan_init(fs_info, 0, 1);
  1823. if (ret)
  1824. return ret;
  1825. /*
  1826. * We have set the rescan_progress to 0, which means no more
  1827. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  1828. * However, btrfs_qgroup_account_ref may be right after its call
  1829. * to btrfs_find_all_roots, in which case it would still do the
  1830. * accounting.
  1831. * To solve this, we're committing the transaction, which will
  1832. * ensure we run all delayed refs and only after that, we are
  1833. * going to clear all tracking information for a clean start.
  1834. */
  1835. trans = btrfs_join_transaction(fs_info->fs_root);
  1836. if (IS_ERR(trans)) {
  1837. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1838. return PTR_ERR(trans);
  1839. }
  1840. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  1841. if (ret) {
  1842. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1843. return ret;
  1844. }
  1845. qgroup_rescan_zero_tracking(fs_info);
  1846. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1847. &fs_info->qgroup_rescan_work);
  1848. return 0;
  1849. }
  1850. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  1851. {
  1852. int running;
  1853. int ret = 0;
  1854. mutex_lock(&fs_info->qgroup_rescan_lock);
  1855. spin_lock(&fs_info->qgroup_lock);
  1856. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1857. spin_unlock(&fs_info->qgroup_lock);
  1858. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1859. if (running)
  1860. ret = wait_for_completion_interruptible(
  1861. &fs_info->qgroup_rescan_completion);
  1862. return ret;
  1863. }
  1864. /*
  1865. * this is only called from open_ctree where we're still single threaded, thus
  1866. * locking is omitted here.
  1867. */
  1868. void
  1869. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  1870. {
  1871. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  1872. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1873. &fs_info->qgroup_rescan_work);
  1874. }