qgroup.c 74 KB

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