qgroup.c 68 KB

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