qgroup.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  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. printk(KERN_ERR
  260. "btrfs: old qgroup version, quota disabled\n");
  261. goto out;
  262. }
  263. if (btrfs_qgroup_status_generation(l, ptr) !=
  264. fs_info->generation) {
  265. flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  266. printk(KERN_ERR
  267. "btrfs: qgroup generation mismatch, "
  268. "marked as inconsistent\n");
  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. printk(KERN_ERR "btrfs: inconsitent qgroup config\n");
  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. printk(KERN_WARNING
  347. "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
  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, path->slots[0],
  552. struct btrfs_qgroup_limit_item);
  553. btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
  554. btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
  555. btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
  556. btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
  557. btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
  558. btrfs_mark_buffer_dirty(l);
  559. out:
  560. btrfs_free_path(path);
  561. return ret;
  562. }
  563. static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
  564. struct btrfs_root *root,
  565. struct btrfs_qgroup *qgroup)
  566. {
  567. struct btrfs_path *path;
  568. struct btrfs_key key;
  569. struct extent_buffer *l;
  570. struct btrfs_qgroup_info_item *qgroup_info;
  571. int ret;
  572. int slot;
  573. key.objectid = 0;
  574. key.type = BTRFS_QGROUP_INFO_KEY;
  575. key.offset = qgroup->qgroupid;
  576. path = btrfs_alloc_path();
  577. if (!path)
  578. return -ENOMEM;
  579. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  580. if (ret > 0)
  581. ret = -ENOENT;
  582. if (ret)
  583. goto out;
  584. l = path->nodes[0];
  585. slot = path->slots[0];
  586. qgroup_info = btrfs_item_ptr(l, path->slots[0],
  587. struct btrfs_qgroup_info_item);
  588. btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
  589. btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
  590. btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
  591. btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
  592. btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
  593. btrfs_mark_buffer_dirty(l);
  594. out:
  595. btrfs_free_path(path);
  596. return ret;
  597. }
  598. static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
  599. struct btrfs_fs_info *fs_info,
  600. struct btrfs_root *root)
  601. {
  602. struct btrfs_path *path;
  603. struct btrfs_key key;
  604. struct extent_buffer *l;
  605. struct btrfs_qgroup_status_item *ptr;
  606. int ret;
  607. int slot;
  608. key.objectid = 0;
  609. key.type = BTRFS_QGROUP_STATUS_KEY;
  610. key.offset = 0;
  611. path = btrfs_alloc_path();
  612. if (!path)
  613. return -ENOMEM;
  614. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  615. if (ret > 0)
  616. ret = -ENOENT;
  617. if (ret)
  618. goto out;
  619. l = path->nodes[0];
  620. slot = path->slots[0];
  621. ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
  622. btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
  623. btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
  624. btrfs_set_qgroup_status_rescan(l, ptr,
  625. fs_info->qgroup_rescan_progress.objectid);
  626. btrfs_mark_buffer_dirty(l);
  627. out:
  628. btrfs_free_path(path);
  629. return ret;
  630. }
  631. /*
  632. * called with qgroup_lock held
  633. */
  634. static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
  635. struct btrfs_root *root)
  636. {
  637. struct btrfs_path *path;
  638. struct btrfs_key key;
  639. struct extent_buffer *leaf = NULL;
  640. int ret;
  641. int nr = 0;
  642. path = btrfs_alloc_path();
  643. if (!path)
  644. return -ENOMEM;
  645. path->leave_spinning = 1;
  646. key.objectid = 0;
  647. key.offset = 0;
  648. key.type = 0;
  649. while (1) {
  650. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  651. if (ret < 0)
  652. goto out;
  653. leaf = path->nodes[0];
  654. nr = btrfs_header_nritems(leaf);
  655. if (!nr)
  656. break;
  657. /*
  658. * delete the leaf one by one
  659. * since the whole tree is going
  660. * to be deleted.
  661. */
  662. path->slots[0] = 0;
  663. ret = btrfs_del_items(trans, root, path, 0, nr);
  664. if (ret)
  665. goto out;
  666. btrfs_release_path(path);
  667. }
  668. ret = 0;
  669. out:
  670. root->fs_info->pending_quota_state = 0;
  671. btrfs_free_path(path);
  672. return ret;
  673. }
  674. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  675. struct btrfs_fs_info *fs_info)
  676. {
  677. struct btrfs_root *quota_root;
  678. struct btrfs_root *tree_root = fs_info->tree_root;
  679. struct btrfs_path *path = NULL;
  680. struct btrfs_qgroup_status_item *ptr;
  681. struct extent_buffer *leaf;
  682. struct btrfs_key key;
  683. struct btrfs_key found_key;
  684. struct btrfs_qgroup *qgroup = NULL;
  685. int ret = 0;
  686. int slot;
  687. mutex_lock(&fs_info->qgroup_ioctl_lock);
  688. if (fs_info->quota_root) {
  689. fs_info->pending_quota_state = 1;
  690. goto out;
  691. }
  692. fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
  693. if (!fs_info->qgroup_ulist) {
  694. ret = -ENOMEM;
  695. goto out;
  696. }
  697. /*
  698. * initially create the quota tree
  699. */
  700. quota_root = btrfs_create_tree(trans, fs_info,
  701. BTRFS_QUOTA_TREE_OBJECTID);
  702. if (IS_ERR(quota_root)) {
  703. ret = PTR_ERR(quota_root);
  704. goto out;
  705. }
  706. path = btrfs_alloc_path();
  707. if (!path) {
  708. ret = -ENOMEM;
  709. goto out_free_root;
  710. }
  711. key.objectid = 0;
  712. key.type = BTRFS_QGROUP_STATUS_KEY;
  713. key.offset = 0;
  714. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  715. sizeof(*ptr));
  716. if (ret)
  717. goto out_free_path;
  718. leaf = path->nodes[0];
  719. ptr = btrfs_item_ptr(leaf, path->slots[0],
  720. struct btrfs_qgroup_status_item);
  721. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  722. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  723. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  724. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  725. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  726. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  727. btrfs_mark_buffer_dirty(leaf);
  728. key.objectid = 0;
  729. key.type = BTRFS_ROOT_REF_KEY;
  730. key.offset = 0;
  731. btrfs_release_path(path);
  732. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  733. if (ret > 0)
  734. goto out_add_root;
  735. if (ret < 0)
  736. goto out_free_path;
  737. while (1) {
  738. slot = path->slots[0];
  739. leaf = path->nodes[0];
  740. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  741. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  742. ret = add_qgroup_item(trans, quota_root,
  743. found_key.offset);
  744. if (ret)
  745. goto out_free_path;
  746. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  747. if (IS_ERR(qgroup)) {
  748. ret = PTR_ERR(qgroup);
  749. goto out_free_path;
  750. }
  751. }
  752. ret = btrfs_next_item(tree_root, path);
  753. if (ret < 0)
  754. goto out_free_path;
  755. if (ret)
  756. break;
  757. }
  758. out_add_root:
  759. btrfs_release_path(path);
  760. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  761. if (ret)
  762. goto out_free_path;
  763. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  764. if (IS_ERR(qgroup)) {
  765. ret = PTR_ERR(qgroup);
  766. goto out_free_path;
  767. }
  768. spin_lock(&fs_info->qgroup_lock);
  769. fs_info->quota_root = quota_root;
  770. fs_info->pending_quota_state = 1;
  771. spin_unlock(&fs_info->qgroup_lock);
  772. out_free_path:
  773. btrfs_free_path(path);
  774. out_free_root:
  775. if (ret) {
  776. free_extent_buffer(quota_root->node);
  777. free_extent_buffer(quota_root->commit_root);
  778. kfree(quota_root);
  779. }
  780. out:
  781. if (ret) {
  782. ulist_free(fs_info->qgroup_ulist);
  783. fs_info->qgroup_ulist = NULL;
  784. }
  785. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  786. return ret;
  787. }
  788. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  789. struct btrfs_fs_info *fs_info)
  790. {
  791. struct btrfs_root *tree_root = fs_info->tree_root;
  792. struct btrfs_root *quota_root;
  793. int ret = 0;
  794. mutex_lock(&fs_info->qgroup_ioctl_lock);
  795. if (!fs_info->quota_root)
  796. goto out;
  797. spin_lock(&fs_info->qgroup_lock);
  798. fs_info->quota_enabled = 0;
  799. fs_info->pending_quota_state = 0;
  800. quota_root = fs_info->quota_root;
  801. fs_info->quota_root = NULL;
  802. spin_unlock(&fs_info->qgroup_lock);
  803. btrfs_free_qgroup_config(fs_info);
  804. ret = btrfs_clean_quota_tree(trans, quota_root);
  805. if (ret)
  806. goto out;
  807. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  808. if (ret)
  809. goto out;
  810. list_del(&quota_root->dirty_list);
  811. btrfs_tree_lock(quota_root->node);
  812. clean_tree_block(trans, tree_root, quota_root->node);
  813. btrfs_tree_unlock(quota_root->node);
  814. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  815. free_extent_buffer(quota_root->node);
  816. free_extent_buffer(quota_root->commit_root);
  817. kfree(quota_root);
  818. out:
  819. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  820. return ret;
  821. }
  822. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  823. struct btrfs_qgroup *qgroup)
  824. {
  825. if (list_empty(&qgroup->dirty))
  826. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  827. }
  828. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  829. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  830. {
  831. struct btrfs_root *quota_root;
  832. struct btrfs_qgroup *parent;
  833. struct btrfs_qgroup *member;
  834. struct btrfs_qgroup_list *list;
  835. int ret = 0;
  836. mutex_lock(&fs_info->qgroup_ioctl_lock);
  837. quota_root = fs_info->quota_root;
  838. if (!quota_root) {
  839. ret = -EINVAL;
  840. goto out;
  841. }
  842. member = find_qgroup_rb(fs_info, src);
  843. parent = find_qgroup_rb(fs_info, dst);
  844. if (!member || !parent) {
  845. ret = -EINVAL;
  846. goto out;
  847. }
  848. /* check if such qgroup relation exist firstly */
  849. list_for_each_entry(list, &member->groups, next_group) {
  850. if (list->group == parent) {
  851. ret = -EEXIST;
  852. goto out;
  853. }
  854. }
  855. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  856. if (ret)
  857. goto out;
  858. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  859. if (ret) {
  860. del_qgroup_relation_item(trans, quota_root, src, dst);
  861. goto out;
  862. }
  863. spin_lock(&fs_info->qgroup_lock);
  864. ret = add_relation_rb(quota_root->fs_info, src, dst);
  865. spin_unlock(&fs_info->qgroup_lock);
  866. out:
  867. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  868. return ret;
  869. }
  870. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  871. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  872. {
  873. struct btrfs_root *quota_root;
  874. struct btrfs_qgroup *parent;
  875. struct btrfs_qgroup *member;
  876. struct btrfs_qgroup_list *list;
  877. int ret = 0;
  878. int err;
  879. mutex_lock(&fs_info->qgroup_ioctl_lock);
  880. quota_root = fs_info->quota_root;
  881. if (!quota_root) {
  882. ret = -EINVAL;
  883. goto out;
  884. }
  885. member = find_qgroup_rb(fs_info, src);
  886. parent = find_qgroup_rb(fs_info, dst);
  887. if (!member || !parent) {
  888. ret = -EINVAL;
  889. goto out;
  890. }
  891. /* check if such qgroup relation exist firstly */
  892. list_for_each_entry(list, &member->groups, next_group) {
  893. if (list->group == parent)
  894. goto exist;
  895. }
  896. ret = -ENOENT;
  897. goto out;
  898. exist:
  899. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  900. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  901. if (err && !ret)
  902. ret = err;
  903. spin_lock(&fs_info->qgroup_lock);
  904. del_relation_rb(fs_info, src, dst);
  905. spin_unlock(&fs_info->qgroup_lock);
  906. out:
  907. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  908. return ret;
  909. }
  910. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  911. struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
  912. {
  913. struct btrfs_root *quota_root;
  914. struct btrfs_qgroup *qgroup;
  915. int ret = 0;
  916. mutex_lock(&fs_info->qgroup_ioctl_lock);
  917. quota_root = fs_info->quota_root;
  918. if (!quota_root) {
  919. ret = -EINVAL;
  920. goto out;
  921. }
  922. qgroup = find_qgroup_rb(fs_info, qgroupid);
  923. if (qgroup) {
  924. ret = -EEXIST;
  925. goto out;
  926. }
  927. ret = add_qgroup_item(trans, quota_root, qgroupid);
  928. if (ret)
  929. goto out;
  930. spin_lock(&fs_info->qgroup_lock);
  931. qgroup = add_qgroup_rb(fs_info, qgroupid);
  932. spin_unlock(&fs_info->qgroup_lock);
  933. if (IS_ERR(qgroup))
  934. ret = PTR_ERR(qgroup);
  935. out:
  936. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  937. return ret;
  938. }
  939. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  940. struct btrfs_fs_info *fs_info, u64 qgroupid)
  941. {
  942. struct btrfs_root *quota_root;
  943. struct btrfs_qgroup *qgroup;
  944. int ret = 0;
  945. mutex_lock(&fs_info->qgroup_ioctl_lock);
  946. quota_root = fs_info->quota_root;
  947. if (!quota_root) {
  948. ret = -EINVAL;
  949. goto out;
  950. }
  951. qgroup = find_qgroup_rb(fs_info, qgroupid);
  952. if (!qgroup) {
  953. ret = -ENOENT;
  954. goto out;
  955. } else {
  956. /* check if there are no relations to this qgroup */
  957. if (!list_empty(&qgroup->groups) ||
  958. !list_empty(&qgroup->members)) {
  959. ret = -EBUSY;
  960. goto out;
  961. }
  962. }
  963. ret = del_qgroup_item(trans, quota_root, qgroupid);
  964. spin_lock(&fs_info->qgroup_lock);
  965. del_qgroup_rb(quota_root->fs_info, qgroupid);
  966. spin_unlock(&fs_info->qgroup_lock);
  967. out:
  968. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  969. return ret;
  970. }
  971. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  972. struct btrfs_fs_info *fs_info, u64 qgroupid,
  973. struct btrfs_qgroup_limit *limit)
  974. {
  975. struct btrfs_root *quota_root;
  976. struct btrfs_qgroup *qgroup;
  977. int ret = 0;
  978. mutex_lock(&fs_info->qgroup_ioctl_lock);
  979. quota_root = fs_info->quota_root;
  980. if (!quota_root) {
  981. ret = -EINVAL;
  982. goto out;
  983. }
  984. qgroup = find_qgroup_rb(fs_info, qgroupid);
  985. if (!qgroup) {
  986. ret = -ENOENT;
  987. goto out;
  988. }
  989. ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
  990. limit->flags, limit->max_rfer,
  991. limit->max_excl, limit->rsv_rfer,
  992. limit->rsv_excl);
  993. if (ret) {
  994. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  995. printk(KERN_INFO "unable to update quota limit for %llu\n",
  996. qgroupid);
  997. }
  998. spin_lock(&fs_info->qgroup_lock);
  999. qgroup->lim_flags = limit->flags;
  1000. qgroup->max_rfer = limit->max_rfer;
  1001. qgroup->max_excl = limit->max_excl;
  1002. qgroup->rsv_rfer = limit->rsv_rfer;
  1003. qgroup->rsv_excl = limit->rsv_excl;
  1004. spin_unlock(&fs_info->qgroup_lock);
  1005. out:
  1006. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1007. return ret;
  1008. }
  1009. /*
  1010. * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
  1011. * the modification into a list that's later used by btrfs_end_transaction to
  1012. * pass the recorded modifications on to btrfs_qgroup_account_ref.
  1013. */
  1014. int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
  1015. struct btrfs_delayed_ref_node *node,
  1016. struct btrfs_delayed_extent_op *extent_op)
  1017. {
  1018. struct qgroup_update *u;
  1019. BUG_ON(!trans->delayed_ref_elem.seq);
  1020. u = kmalloc(sizeof(*u), GFP_NOFS);
  1021. if (!u)
  1022. return -ENOMEM;
  1023. u->node = node;
  1024. u->extent_op = extent_op;
  1025. list_add_tail(&u->list, &trans->qgroup_ref_list);
  1026. return 0;
  1027. }
  1028. static int qgroup_account_ref_step1(struct btrfs_fs_info *fs_info,
  1029. struct ulist *roots, struct ulist *tmp,
  1030. u64 seq)
  1031. {
  1032. struct ulist_node *unode;
  1033. struct ulist_iterator uiter;
  1034. struct ulist_node *tmp_unode;
  1035. struct ulist_iterator tmp_uiter;
  1036. struct btrfs_qgroup *qg;
  1037. int ret;
  1038. ULIST_ITER_INIT(&uiter);
  1039. while ((unode = ulist_next(roots, &uiter))) {
  1040. qg = find_qgroup_rb(fs_info, unode->val);
  1041. if (!qg)
  1042. continue;
  1043. ulist_reinit(tmp);
  1044. /* XXX id not needed */
  1045. ret = ulist_add(tmp, qg->qgroupid,
  1046. (u64)(uintptr_t)qg, GFP_ATOMIC);
  1047. if (ret < 0)
  1048. return ret;
  1049. ULIST_ITER_INIT(&tmp_uiter);
  1050. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1051. struct btrfs_qgroup_list *glist;
  1052. qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
  1053. if (qg->refcnt < seq)
  1054. qg->refcnt = seq + 1;
  1055. else
  1056. ++qg->refcnt;
  1057. list_for_each_entry(glist, &qg->groups, next_group) {
  1058. ret = ulist_add(tmp, glist->group->qgroupid,
  1059. (u64)(uintptr_t)glist->group,
  1060. GFP_ATOMIC);
  1061. if (ret < 0)
  1062. return ret;
  1063. }
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. static int qgroup_account_ref_step2(struct btrfs_fs_info *fs_info,
  1069. struct ulist *roots, struct ulist *tmp,
  1070. u64 seq, int sgn, u64 num_bytes,
  1071. struct btrfs_qgroup *qgroup)
  1072. {
  1073. struct ulist_node *unode;
  1074. struct ulist_iterator uiter;
  1075. struct btrfs_qgroup *qg;
  1076. struct btrfs_qgroup_list *glist;
  1077. int ret;
  1078. ulist_reinit(tmp);
  1079. ret = ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
  1080. if (ret < 0)
  1081. return ret;
  1082. ULIST_ITER_INIT(&uiter);
  1083. while ((unode = ulist_next(tmp, &uiter))) {
  1084. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1085. if (qg->refcnt < seq) {
  1086. /* not visited by step 1 */
  1087. qg->rfer += sgn * num_bytes;
  1088. qg->rfer_cmpr += sgn * num_bytes;
  1089. if (roots->nnodes == 0) {
  1090. qg->excl += sgn * num_bytes;
  1091. qg->excl_cmpr += sgn * num_bytes;
  1092. }
  1093. qgroup_dirty(fs_info, qg);
  1094. }
  1095. WARN_ON(qg->tag >= seq);
  1096. qg->tag = seq;
  1097. list_for_each_entry(glist, &qg->groups, next_group) {
  1098. ret = ulist_add(tmp, glist->group->qgroupid,
  1099. (uintptr_t)glist->group, GFP_ATOMIC);
  1100. if (ret < 0)
  1101. return ret;
  1102. }
  1103. }
  1104. return 0;
  1105. }
  1106. static int qgroup_account_ref_step3(struct btrfs_fs_info *fs_info,
  1107. struct ulist *roots, struct ulist *tmp,
  1108. u64 seq, int sgn, u64 num_bytes)
  1109. {
  1110. struct ulist_node *unode;
  1111. struct ulist_iterator uiter;
  1112. struct btrfs_qgroup *qg;
  1113. struct ulist_node *tmp_unode;
  1114. struct ulist_iterator tmp_uiter;
  1115. int ret;
  1116. ULIST_ITER_INIT(&uiter);
  1117. while ((unode = ulist_next(roots, &uiter))) {
  1118. qg = find_qgroup_rb(fs_info, unode->val);
  1119. if (!qg)
  1120. continue;
  1121. ulist_reinit(tmp);
  1122. ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
  1123. if (ret < 0)
  1124. return ret;
  1125. ULIST_ITER_INIT(&tmp_uiter);
  1126. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1127. struct btrfs_qgroup_list *glist;
  1128. qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
  1129. if (qg->tag == seq)
  1130. continue;
  1131. if (qg->refcnt - seq == roots->nnodes) {
  1132. qg->excl -= sgn * num_bytes;
  1133. qg->excl_cmpr -= sgn * num_bytes;
  1134. qgroup_dirty(fs_info, qg);
  1135. }
  1136. list_for_each_entry(glist, &qg->groups, next_group) {
  1137. ret = ulist_add(tmp, glist->group->qgroupid,
  1138. (uintptr_t)glist->group,
  1139. GFP_ATOMIC);
  1140. if (ret < 0)
  1141. return ret;
  1142. }
  1143. }
  1144. }
  1145. return 0;
  1146. }
  1147. /*
  1148. * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
  1149. * from the fs. First, all roots referencing the extent are searched, and
  1150. * then the space is accounted accordingly to the different roots. The
  1151. * accounting algorithm works in 3 steps documented inline.
  1152. */
  1153. int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
  1154. struct btrfs_fs_info *fs_info,
  1155. struct btrfs_delayed_ref_node *node,
  1156. struct btrfs_delayed_extent_op *extent_op)
  1157. {
  1158. struct btrfs_key ins;
  1159. struct btrfs_root *quota_root;
  1160. u64 ref_root;
  1161. struct btrfs_qgroup *qgroup;
  1162. struct ulist *roots = NULL;
  1163. u64 seq;
  1164. int ret = 0;
  1165. int sgn;
  1166. if (!fs_info->quota_enabled)
  1167. return 0;
  1168. BUG_ON(!fs_info->quota_root);
  1169. ins.objectid = node->bytenr;
  1170. ins.offset = node->num_bytes;
  1171. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1172. if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
  1173. node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  1174. struct btrfs_delayed_tree_ref *ref;
  1175. ref = btrfs_delayed_node_to_tree_ref(node);
  1176. ref_root = ref->root;
  1177. } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  1178. node->type == BTRFS_SHARED_DATA_REF_KEY) {
  1179. struct btrfs_delayed_data_ref *ref;
  1180. ref = btrfs_delayed_node_to_data_ref(node);
  1181. ref_root = ref->root;
  1182. } else {
  1183. BUG();
  1184. }
  1185. if (!is_fstree(ref_root)) {
  1186. /*
  1187. * non-fs-trees are not being accounted
  1188. */
  1189. return 0;
  1190. }
  1191. switch (node->action) {
  1192. case BTRFS_ADD_DELAYED_REF:
  1193. case BTRFS_ADD_DELAYED_EXTENT:
  1194. sgn = 1;
  1195. seq = btrfs_tree_mod_seq_prev(node->seq);
  1196. break;
  1197. case BTRFS_DROP_DELAYED_REF:
  1198. sgn = -1;
  1199. seq = node->seq;
  1200. break;
  1201. case BTRFS_UPDATE_DELAYED_HEAD:
  1202. return 0;
  1203. default:
  1204. BUG();
  1205. }
  1206. mutex_lock(&fs_info->qgroup_rescan_lock);
  1207. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1208. if (fs_info->qgroup_rescan_progress.objectid <= node->bytenr) {
  1209. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1210. return 0;
  1211. }
  1212. }
  1213. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1214. /*
  1215. * the delayed ref sequence number we pass depends on the direction of
  1216. * the operation. for add operations, we pass
  1217. * tree_mod_log_prev_seq(node->seq) to skip
  1218. * the delayed ref's current sequence number, because we need the state
  1219. * of the tree before the add operation. for delete operations, we pass
  1220. * (node->seq) to include the delayed ref's current sequence number,
  1221. * because we need the state of the tree after the delete operation.
  1222. */
  1223. ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, seq, &roots);
  1224. if (ret < 0)
  1225. return ret;
  1226. spin_lock(&fs_info->qgroup_lock);
  1227. quota_root = fs_info->quota_root;
  1228. if (!quota_root)
  1229. goto unlock;
  1230. qgroup = find_qgroup_rb(fs_info, ref_root);
  1231. if (!qgroup)
  1232. goto unlock;
  1233. /*
  1234. * step 1: for each old ref, visit all nodes once and inc refcnt
  1235. */
  1236. ulist_reinit(fs_info->qgroup_ulist);
  1237. seq = fs_info->qgroup_seq;
  1238. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1239. ret = qgroup_account_ref_step1(fs_info, roots, fs_info->qgroup_ulist,
  1240. seq);
  1241. if (ret)
  1242. goto unlock;
  1243. /*
  1244. * step 2: walk from the new root
  1245. */
  1246. ret = qgroup_account_ref_step2(fs_info, roots, fs_info->qgroup_ulist,
  1247. seq, sgn, node->num_bytes, qgroup);
  1248. if (ret)
  1249. goto unlock;
  1250. /*
  1251. * step 3: walk again from old refs
  1252. */
  1253. ret = qgroup_account_ref_step3(fs_info, roots, fs_info->qgroup_ulist,
  1254. seq, sgn, node->num_bytes);
  1255. if (ret)
  1256. goto unlock;
  1257. unlock:
  1258. spin_unlock(&fs_info->qgroup_lock);
  1259. ulist_free(roots);
  1260. return ret;
  1261. }
  1262. /*
  1263. * called from commit_transaction. Writes all changed qgroups to disk.
  1264. */
  1265. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1266. struct btrfs_fs_info *fs_info)
  1267. {
  1268. struct btrfs_root *quota_root = fs_info->quota_root;
  1269. int ret = 0;
  1270. int start_rescan_worker = 0;
  1271. if (!quota_root)
  1272. goto out;
  1273. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1274. start_rescan_worker = 1;
  1275. fs_info->quota_enabled = fs_info->pending_quota_state;
  1276. spin_lock(&fs_info->qgroup_lock);
  1277. while (!list_empty(&fs_info->dirty_qgroups)) {
  1278. struct btrfs_qgroup *qgroup;
  1279. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1280. struct btrfs_qgroup, dirty);
  1281. list_del_init(&qgroup->dirty);
  1282. spin_unlock(&fs_info->qgroup_lock);
  1283. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1284. if (ret)
  1285. fs_info->qgroup_flags |=
  1286. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1287. spin_lock(&fs_info->qgroup_lock);
  1288. }
  1289. if (fs_info->quota_enabled)
  1290. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1291. else
  1292. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1293. spin_unlock(&fs_info->qgroup_lock);
  1294. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1295. if (ret)
  1296. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1297. if (!ret && start_rescan_worker) {
  1298. ret = qgroup_rescan_init(fs_info, 0, 1);
  1299. if (!ret) {
  1300. qgroup_rescan_zero_tracking(fs_info);
  1301. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1302. &fs_info->qgroup_rescan_work);
  1303. }
  1304. ret = 0;
  1305. }
  1306. out:
  1307. return ret;
  1308. }
  1309. /*
  1310. * copy the acounting information between qgroups. This is necessary when a
  1311. * snapshot or a subvolume is created
  1312. */
  1313. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1314. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1315. struct btrfs_qgroup_inherit *inherit)
  1316. {
  1317. int ret = 0;
  1318. int i;
  1319. u64 *i_qgroups;
  1320. struct btrfs_root *quota_root = fs_info->quota_root;
  1321. struct btrfs_qgroup *srcgroup;
  1322. struct btrfs_qgroup *dstgroup;
  1323. u32 level_size = 0;
  1324. u64 nums;
  1325. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1326. if (!fs_info->quota_enabled)
  1327. goto out;
  1328. if (!quota_root) {
  1329. ret = -EINVAL;
  1330. goto out;
  1331. }
  1332. if (inherit) {
  1333. i_qgroups = (u64 *)(inherit + 1);
  1334. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1335. 2 * inherit->num_excl_copies;
  1336. for (i = 0; i < nums; ++i) {
  1337. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1338. if (!srcgroup) {
  1339. ret = -EINVAL;
  1340. goto out;
  1341. }
  1342. ++i_qgroups;
  1343. }
  1344. }
  1345. /*
  1346. * create a tracking group for the subvol itself
  1347. */
  1348. ret = add_qgroup_item(trans, quota_root, objectid);
  1349. if (ret)
  1350. goto out;
  1351. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1352. ret = update_qgroup_limit_item(trans, quota_root, objectid,
  1353. inherit->lim.flags,
  1354. inherit->lim.max_rfer,
  1355. inherit->lim.max_excl,
  1356. inherit->lim.rsv_rfer,
  1357. inherit->lim.rsv_excl);
  1358. if (ret)
  1359. goto out;
  1360. }
  1361. if (srcid) {
  1362. struct btrfs_root *srcroot;
  1363. struct btrfs_key srckey;
  1364. int srcroot_level;
  1365. srckey.objectid = srcid;
  1366. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1367. srckey.offset = (u64)-1;
  1368. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1369. if (IS_ERR(srcroot)) {
  1370. ret = PTR_ERR(srcroot);
  1371. goto out;
  1372. }
  1373. rcu_read_lock();
  1374. srcroot_level = btrfs_header_level(srcroot->node);
  1375. level_size = btrfs_level_size(srcroot, srcroot_level);
  1376. rcu_read_unlock();
  1377. }
  1378. /*
  1379. * add qgroup to all inherited groups
  1380. */
  1381. if (inherit) {
  1382. i_qgroups = (u64 *)(inherit + 1);
  1383. for (i = 0; i < inherit->num_qgroups; ++i) {
  1384. ret = add_qgroup_relation_item(trans, quota_root,
  1385. objectid, *i_qgroups);
  1386. if (ret)
  1387. goto out;
  1388. ret = add_qgroup_relation_item(trans, quota_root,
  1389. *i_qgroups, objectid);
  1390. if (ret)
  1391. goto out;
  1392. ++i_qgroups;
  1393. }
  1394. }
  1395. spin_lock(&fs_info->qgroup_lock);
  1396. dstgroup = add_qgroup_rb(fs_info, objectid);
  1397. if (IS_ERR(dstgroup)) {
  1398. ret = PTR_ERR(dstgroup);
  1399. goto unlock;
  1400. }
  1401. if (srcid) {
  1402. srcgroup = find_qgroup_rb(fs_info, srcid);
  1403. if (!srcgroup)
  1404. goto unlock;
  1405. dstgroup->rfer = srcgroup->rfer - level_size;
  1406. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
  1407. srcgroup->excl = level_size;
  1408. srcgroup->excl_cmpr = level_size;
  1409. qgroup_dirty(fs_info, dstgroup);
  1410. qgroup_dirty(fs_info, srcgroup);
  1411. }
  1412. if (!inherit)
  1413. goto unlock;
  1414. i_qgroups = (u64 *)(inherit + 1);
  1415. for (i = 0; i < inherit->num_qgroups; ++i) {
  1416. ret = add_relation_rb(quota_root->fs_info, objectid,
  1417. *i_qgroups);
  1418. if (ret)
  1419. goto unlock;
  1420. ++i_qgroups;
  1421. }
  1422. for (i = 0; i < inherit->num_ref_copies; ++i) {
  1423. struct btrfs_qgroup *src;
  1424. struct btrfs_qgroup *dst;
  1425. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1426. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1427. if (!src || !dst) {
  1428. ret = -EINVAL;
  1429. goto unlock;
  1430. }
  1431. dst->rfer = src->rfer - level_size;
  1432. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1433. i_qgroups += 2;
  1434. }
  1435. for (i = 0; i < inherit->num_excl_copies; ++i) {
  1436. struct btrfs_qgroup *src;
  1437. struct btrfs_qgroup *dst;
  1438. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1439. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1440. if (!src || !dst) {
  1441. ret = -EINVAL;
  1442. goto unlock;
  1443. }
  1444. dst->excl = src->excl + level_size;
  1445. dst->excl_cmpr = src->excl_cmpr + level_size;
  1446. i_qgroups += 2;
  1447. }
  1448. unlock:
  1449. spin_unlock(&fs_info->qgroup_lock);
  1450. out:
  1451. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1452. return ret;
  1453. }
  1454. /*
  1455. * reserve some space for a qgroup and all its parents. The reservation takes
  1456. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  1457. * accounting. If not enough space is available, EDQUOT is returned.
  1458. * We assume that the requested space is new for all qgroups.
  1459. */
  1460. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  1461. {
  1462. struct btrfs_root *quota_root;
  1463. struct btrfs_qgroup *qgroup;
  1464. struct btrfs_fs_info *fs_info = root->fs_info;
  1465. u64 ref_root = root->root_key.objectid;
  1466. int ret = 0;
  1467. struct ulist_node *unode;
  1468. struct ulist_iterator uiter;
  1469. if (!is_fstree(ref_root))
  1470. return 0;
  1471. if (num_bytes == 0)
  1472. return 0;
  1473. spin_lock(&fs_info->qgroup_lock);
  1474. quota_root = fs_info->quota_root;
  1475. if (!quota_root)
  1476. goto out;
  1477. qgroup = find_qgroup_rb(fs_info, ref_root);
  1478. if (!qgroup)
  1479. goto out;
  1480. /*
  1481. * in a first step, we check all affected qgroups if any limits would
  1482. * be exceeded
  1483. */
  1484. ulist_reinit(fs_info->qgroup_ulist);
  1485. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1486. (uintptr_t)qgroup, GFP_ATOMIC);
  1487. if (ret < 0)
  1488. goto out;
  1489. ULIST_ITER_INIT(&uiter);
  1490. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1491. struct btrfs_qgroup *qg;
  1492. struct btrfs_qgroup_list *glist;
  1493. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1494. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  1495. qg->reserved + (s64)qg->rfer + num_bytes >
  1496. qg->max_rfer) {
  1497. ret = -EDQUOT;
  1498. goto out;
  1499. }
  1500. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  1501. qg->reserved + (s64)qg->excl + num_bytes >
  1502. qg->max_excl) {
  1503. ret = -EDQUOT;
  1504. goto out;
  1505. }
  1506. list_for_each_entry(glist, &qg->groups, next_group) {
  1507. ret = ulist_add(fs_info->qgroup_ulist,
  1508. glist->group->qgroupid,
  1509. (uintptr_t)glist->group, GFP_ATOMIC);
  1510. if (ret < 0)
  1511. goto out;
  1512. }
  1513. }
  1514. ret = 0;
  1515. /*
  1516. * no limits exceeded, now record the reservation into all qgroups
  1517. */
  1518. ULIST_ITER_INIT(&uiter);
  1519. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1520. struct btrfs_qgroup *qg;
  1521. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1522. qg->reserved += num_bytes;
  1523. }
  1524. out:
  1525. spin_unlock(&fs_info->qgroup_lock);
  1526. return ret;
  1527. }
  1528. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  1529. {
  1530. struct btrfs_root *quota_root;
  1531. struct btrfs_qgroup *qgroup;
  1532. struct btrfs_fs_info *fs_info = root->fs_info;
  1533. struct ulist_node *unode;
  1534. struct ulist_iterator uiter;
  1535. u64 ref_root = root->root_key.objectid;
  1536. int ret = 0;
  1537. if (!is_fstree(ref_root))
  1538. return;
  1539. if (num_bytes == 0)
  1540. return;
  1541. spin_lock(&fs_info->qgroup_lock);
  1542. quota_root = fs_info->quota_root;
  1543. if (!quota_root)
  1544. goto out;
  1545. qgroup = find_qgroup_rb(fs_info, ref_root);
  1546. if (!qgroup)
  1547. goto out;
  1548. ulist_reinit(fs_info->qgroup_ulist);
  1549. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  1550. (uintptr_t)qgroup, GFP_ATOMIC);
  1551. if (ret < 0)
  1552. goto out;
  1553. ULIST_ITER_INIT(&uiter);
  1554. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  1555. struct btrfs_qgroup *qg;
  1556. struct btrfs_qgroup_list *glist;
  1557. qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
  1558. qg->reserved -= num_bytes;
  1559. list_for_each_entry(glist, &qg->groups, next_group) {
  1560. ret = ulist_add(fs_info->qgroup_ulist,
  1561. glist->group->qgroupid,
  1562. (uintptr_t)glist->group, GFP_ATOMIC);
  1563. if (ret < 0)
  1564. goto out;
  1565. }
  1566. }
  1567. out:
  1568. spin_unlock(&fs_info->qgroup_lock);
  1569. }
  1570. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  1571. {
  1572. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  1573. return;
  1574. pr_err("btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x\n",
  1575. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  1576. (u32)(trans->delayed_ref_elem.seq >> 32),
  1577. (u32)trans->delayed_ref_elem.seq);
  1578. BUG();
  1579. }
  1580. /*
  1581. * returns < 0 on error, 0 when more leafs are to be scanned.
  1582. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  1583. */
  1584. static int
  1585. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1586. struct btrfs_trans_handle *trans, struct ulist *tmp,
  1587. struct extent_buffer *scratch_leaf)
  1588. {
  1589. struct btrfs_key found;
  1590. struct ulist *roots = NULL;
  1591. struct ulist_node *unode;
  1592. struct ulist_iterator uiter;
  1593. struct seq_list tree_mod_seq_elem = {};
  1594. u64 seq;
  1595. int slot;
  1596. int ret;
  1597. path->leave_spinning = 1;
  1598. mutex_lock(&fs_info->qgroup_rescan_lock);
  1599. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  1600. &fs_info->qgroup_rescan_progress,
  1601. path, 1, 0);
  1602. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  1603. fs_info->qgroup_rescan_progress.objectid,
  1604. fs_info->qgroup_rescan_progress.type,
  1605. fs_info->qgroup_rescan_progress.offset, ret);
  1606. if (ret) {
  1607. /*
  1608. * The rescan is about to end, we will not be scanning any
  1609. * further blocks. We cannot unset the RESCAN flag here, because
  1610. * we want to commit the transaction if everything went well.
  1611. * To make the live accounting work in this phase, we set our
  1612. * scan progress pointer such that every real extent objectid
  1613. * will be smaller.
  1614. */
  1615. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  1616. btrfs_release_path(path);
  1617. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1618. return ret;
  1619. }
  1620. btrfs_item_key_to_cpu(path->nodes[0], &found,
  1621. btrfs_header_nritems(path->nodes[0]) - 1);
  1622. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  1623. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1624. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  1625. slot = path->slots[0];
  1626. btrfs_release_path(path);
  1627. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1628. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  1629. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  1630. if (found.type != BTRFS_EXTENT_ITEM_KEY)
  1631. continue;
  1632. ret = btrfs_find_all_roots(trans, fs_info, found.objectid,
  1633. tree_mod_seq_elem.seq, &roots);
  1634. if (ret < 0)
  1635. goto out;
  1636. spin_lock(&fs_info->qgroup_lock);
  1637. seq = fs_info->qgroup_seq;
  1638. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  1639. ret = qgroup_account_ref_step1(fs_info, roots, tmp, seq);
  1640. if (ret) {
  1641. spin_unlock(&fs_info->qgroup_lock);
  1642. ulist_free(roots);
  1643. goto out;
  1644. }
  1645. /*
  1646. * step2 of btrfs_qgroup_account_ref works from a single root,
  1647. * we're doing all at once here.
  1648. */
  1649. ulist_reinit(tmp);
  1650. ULIST_ITER_INIT(&uiter);
  1651. while ((unode = ulist_next(roots, &uiter))) {
  1652. struct btrfs_qgroup *qg;
  1653. qg = find_qgroup_rb(fs_info, unode->val);
  1654. if (!qg)
  1655. continue;
  1656. ret = ulist_add(tmp, qg->qgroupid, (uintptr_t)qg,
  1657. GFP_ATOMIC);
  1658. if (ret < 0) {
  1659. spin_unlock(&fs_info->qgroup_lock);
  1660. ulist_free(roots);
  1661. goto out;
  1662. }
  1663. }
  1664. /* this loop is similar to step 2 of btrfs_qgroup_account_ref */
  1665. ULIST_ITER_INIT(&uiter);
  1666. while ((unode = ulist_next(tmp, &uiter))) {
  1667. struct btrfs_qgroup *qg;
  1668. struct btrfs_qgroup_list *glist;
  1669. qg = (struct btrfs_qgroup *)(uintptr_t) unode->aux;
  1670. qg->rfer += found.offset;
  1671. qg->rfer_cmpr += found.offset;
  1672. WARN_ON(qg->tag >= seq);
  1673. if (qg->refcnt - seq == roots->nnodes) {
  1674. qg->excl += found.offset;
  1675. qg->excl_cmpr += found.offset;
  1676. }
  1677. qgroup_dirty(fs_info, qg);
  1678. list_for_each_entry(glist, &qg->groups, next_group) {
  1679. ret = ulist_add(tmp, glist->group->qgroupid,
  1680. (uintptr_t)glist->group,
  1681. GFP_ATOMIC);
  1682. if (ret < 0) {
  1683. spin_unlock(&fs_info->qgroup_lock);
  1684. ulist_free(roots);
  1685. goto out;
  1686. }
  1687. }
  1688. }
  1689. spin_unlock(&fs_info->qgroup_lock);
  1690. ulist_free(roots);
  1691. ret = 0;
  1692. }
  1693. out:
  1694. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  1695. return ret;
  1696. }
  1697. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  1698. {
  1699. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  1700. qgroup_rescan_work);
  1701. struct btrfs_path *path;
  1702. struct btrfs_trans_handle *trans = NULL;
  1703. struct ulist *tmp = NULL;
  1704. struct extent_buffer *scratch_leaf = NULL;
  1705. int err = -ENOMEM;
  1706. path = btrfs_alloc_path();
  1707. if (!path)
  1708. goto out;
  1709. tmp = ulist_alloc(GFP_NOFS);
  1710. if (!tmp)
  1711. goto out;
  1712. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  1713. if (!scratch_leaf)
  1714. goto out;
  1715. err = 0;
  1716. while (!err) {
  1717. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  1718. if (IS_ERR(trans)) {
  1719. err = PTR_ERR(trans);
  1720. break;
  1721. }
  1722. if (!fs_info->quota_enabled) {
  1723. err = -EINTR;
  1724. } else {
  1725. err = qgroup_rescan_leaf(fs_info, path, trans,
  1726. tmp, scratch_leaf);
  1727. }
  1728. if (err > 0)
  1729. btrfs_commit_transaction(trans, fs_info->fs_root);
  1730. else
  1731. btrfs_end_transaction(trans, fs_info->fs_root);
  1732. }
  1733. out:
  1734. kfree(scratch_leaf);
  1735. ulist_free(tmp);
  1736. btrfs_free_path(path);
  1737. mutex_lock(&fs_info->qgroup_rescan_lock);
  1738. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1739. if (err == 2 &&
  1740. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  1741. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1742. } else if (err < 0) {
  1743. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1744. }
  1745. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1746. if (err >= 0) {
  1747. pr_info("btrfs: qgroup scan completed%s\n",
  1748. err == 2 ? " (inconsistency flag cleared)" : "");
  1749. } else {
  1750. pr_err("btrfs: qgroup scan failed with %d\n", err);
  1751. }
  1752. complete_all(&fs_info->qgroup_rescan_completion);
  1753. }
  1754. /*
  1755. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  1756. * memory required for the rescan context.
  1757. */
  1758. static int
  1759. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  1760. int init_flags)
  1761. {
  1762. int ret = 0;
  1763. if (!init_flags &&
  1764. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  1765. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  1766. ret = -EINVAL;
  1767. goto err;
  1768. }
  1769. mutex_lock(&fs_info->qgroup_rescan_lock);
  1770. spin_lock(&fs_info->qgroup_lock);
  1771. if (init_flags) {
  1772. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  1773. ret = -EINPROGRESS;
  1774. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  1775. ret = -EINVAL;
  1776. if (ret) {
  1777. spin_unlock(&fs_info->qgroup_lock);
  1778. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1779. goto err;
  1780. }
  1781. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1782. }
  1783. memset(&fs_info->qgroup_rescan_progress, 0,
  1784. sizeof(fs_info->qgroup_rescan_progress));
  1785. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  1786. spin_unlock(&fs_info->qgroup_lock);
  1787. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1788. init_completion(&fs_info->qgroup_rescan_completion);
  1789. memset(&fs_info->qgroup_rescan_work, 0,
  1790. sizeof(fs_info->qgroup_rescan_work));
  1791. fs_info->qgroup_rescan_work.func = btrfs_qgroup_rescan_worker;
  1792. if (ret) {
  1793. err:
  1794. pr_info("btrfs: qgroup_rescan_init failed with %d\n", ret);
  1795. return ret;
  1796. }
  1797. return 0;
  1798. }
  1799. static void
  1800. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  1801. {
  1802. struct rb_node *n;
  1803. struct btrfs_qgroup *qgroup;
  1804. spin_lock(&fs_info->qgroup_lock);
  1805. /* clear all current qgroup tracking information */
  1806. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  1807. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  1808. qgroup->rfer = 0;
  1809. qgroup->rfer_cmpr = 0;
  1810. qgroup->excl = 0;
  1811. qgroup->excl_cmpr = 0;
  1812. }
  1813. spin_unlock(&fs_info->qgroup_lock);
  1814. }
  1815. int
  1816. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  1817. {
  1818. int ret = 0;
  1819. struct btrfs_trans_handle *trans;
  1820. ret = qgroup_rescan_init(fs_info, 0, 1);
  1821. if (ret)
  1822. return ret;
  1823. /*
  1824. * We have set the rescan_progress to 0, which means no more
  1825. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  1826. * However, btrfs_qgroup_account_ref may be right after its call
  1827. * to btrfs_find_all_roots, in which case it would still do the
  1828. * accounting.
  1829. * To solve this, we're committing the transaction, which will
  1830. * ensure we run all delayed refs and only after that, we are
  1831. * going to clear all tracking information for a clean start.
  1832. */
  1833. trans = btrfs_join_transaction(fs_info->fs_root);
  1834. if (IS_ERR(trans)) {
  1835. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1836. return PTR_ERR(trans);
  1837. }
  1838. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  1839. if (ret) {
  1840. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1841. return ret;
  1842. }
  1843. qgroup_rescan_zero_tracking(fs_info);
  1844. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1845. &fs_info->qgroup_rescan_work);
  1846. return 0;
  1847. }
  1848. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  1849. {
  1850. int running;
  1851. int ret = 0;
  1852. mutex_lock(&fs_info->qgroup_rescan_lock);
  1853. spin_lock(&fs_info->qgroup_lock);
  1854. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  1855. spin_unlock(&fs_info->qgroup_lock);
  1856. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1857. if (running)
  1858. ret = wait_for_completion_interruptible(
  1859. &fs_info->qgroup_rescan_completion);
  1860. return ret;
  1861. }
  1862. /*
  1863. * this is only called from open_ctree where we're still single threaded, thus
  1864. * locking is omitted here.
  1865. */
  1866. void
  1867. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  1868. {
  1869. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  1870. btrfs_queue_worker(&fs_info->qgroup_rescan_workers,
  1871. &fs_info->qgroup_rescan_work);
  1872. }