qgroup.c 51 KB

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