qgroup.c 67 KB

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