qgroup.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  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(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. if (WARN_ON(!record->old_roots))
  1262. ret = btrfs_find_all_roots(NULL, fs_info,
  1263. record->bytenr, 0, &record->old_roots);
  1264. if (ret < 0)
  1265. break;
  1266. if (qgroup_to_skip)
  1267. ulist_del(record->old_roots, qgroup_to_skip, 0);
  1268. node = rb_next(node);
  1269. }
  1270. return ret;
  1271. }
  1272. int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
  1273. struct btrfs_delayed_ref_root *delayed_refs,
  1274. struct btrfs_qgroup_extent_record *record)
  1275. {
  1276. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1277. struct rb_node *parent_node = NULL;
  1278. struct btrfs_qgroup_extent_record *entry;
  1279. u64 bytenr = record->bytenr;
  1280. assert_spin_locked(&delayed_refs->lock);
  1281. trace_btrfs_qgroup_trace_extent(fs_info, record);
  1282. while (*p) {
  1283. parent_node = *p;
  1284. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1285. node);
  1286. if (bytenr < entry->bytenr)
  1287. p = &(*p)->rb_left;
  1288. else if (bytenr > entry->bytenr)
  1289. p = &(*p)->rb_right;
  1290. else
  1291. return 1;
  1292. }
  1293. rb_link_node(&record->node, parent_node, p);
  1294. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1295. return 0;
  1296. }
  1297. int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
  1298. struct btrfs_qgroup_extent_record *qrecord)
  1299. {
  1300. struct ulist *old_root;
  1301. u64 bytenr = qrecord->bytenr;
  1302. int ret;
  1303. ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
  1304. if (ret < 0)
  1305. return ret;
  1306. /*
  1307. * Here we don't need to get the lock of
  1308. * trans->transaction->delayed_refs, since inserted qrecord won't
  1309. * be deleted, only qrecord->node may be modified (new qrecord insert)
  1310. *
  1311. * So modifying qrecord->old_roots is safe here
  1312. */
  1313. qrecord->old_roots = old_root;
  1314. return 0;
  1315. }
  1316. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
  1317. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  1318. gfp_t gfp_flag)
  1319. {
  1320. struct btrfs_qgroup_extent_record *record;
  1321. struct btrfs_delayed_ref_root *delayed_refs;
  1322. int ret;
  1323. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
  1324. || bytenr == 0 || num_bytes == 0)
  1325. return 0;
  1326. if (WARN_ON(trans == NULL))
  1327. return -EINVAL;
  1328. record = kmalloc(sizeof(*record), gfp_flag);
  1329. if (!record)
  1330. return -ENOMEM;
  1331. delayed_refs = &trans->transaction->delayed_refs;
  1332. record->bytenr = bytenr;
  1333. record->num_bytes = num_bytes;
  1334. record->old_roots = NULL;
  1335. spin_lock(&delayed_refs->lock);
  1336. ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
  1337. spin_unlock(&delayed_refs->lock);
  1338. if (ret > 0) {
  1339. kfree(record);
  1340. return 0;
  1341. }
  1342. return btrfs_qgroup_trace_extent_post(fs_info, record);
  1343. }
  1344. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  1345. struct btrfs_fs_info *fs_info,
  1346. struct extent_buffer *eb)
  1347. {
  1348. int nr = btrfs_header_nritems(eb);
  1349. int i, extent_type, ret;
  1350. struct btrfs_key key;
  1351. struct btrfs_file_extent_item *fi;
  1352. u64 bytenr, num_bytes;
  1353. /* We can be called directly from walk_up_proc() */
  1354. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1355. return 0;
  1356. for (i = 0; i < nr; i++) {
  1357. btrfs_item_key_to_cpu(eb, &key, i);
  1358. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1359. continue;
  1360. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  1361. /* filter out non qgroup-accountable extents */
  1362. extent_type = btrfs_file_extent_type(eb, fi);
  1363. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1364. continue;
  1365. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  1366. if (!bytenr)
  1367. continue;
  1368. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  1369. ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
  1370. num_bytes, GFP_NOFS);
  1371. if (ret)
  1372. return ret;
  1373. }
  1374. return 0;
  1375. }
  1376. /*
  1377. * Walk up the tree from the bottom, freeing leaves and any interior
  1378. * nodes which have had all slots visited. If a node (leaf or
  1379. * interior) is freed, the node above it will have it's slot
  1380. * incremented. The root node will never be freed.
  1381. *
  1382. * At the end of this function, we should have a path which has all
  1383. * slots incremented to the next position for a search. If we need to
  1384. * read a new node it will be NULL and the node above it will have the
  1385. * correct slot selected for a later read.
  1386. *
  1387. * If we increment the root nodes slot counter past the number of
  1388. * elements, 1 is returned to signal completion of the search.
  1389. */
  1390. static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
  1391. {
  1392. int level = 0;
  1393. int nr, slot;
  1394. struct extent_buffer *eb;
  1395. if (root_level == 0)
  1396. return 1;
  1397. while (level <= root_level) {
  1398. eb = path->nodes[level];
  1399. nr = btrfs_header_nritems(eb);
  1400. path->slots[level]++;
  1401. slot = path->slots[level];
  1402. if (slot >= nr || level == 0) {
  1403. /*
  1404. * Don't free the root - we will detect this
  1405. * condition after our loop and return a
  1406. * positive value for caller to stop walking the tree.
  1407. */
  1408. if (level != root_level) {
  1409. btrfs_tree_unlock_rw(eb, path->locks[level]);
  1410. path->locks[level] = 0;
  1411. free_extent_buffer(eb);
  1412. path->nodes[level] = NULL;
  1413. path->slots[level] = 0;
  1414. }
  1415. } else {
  1416. /*
  1417. * We have a valid slot to walk back down
  1418. * from. Stop here so caller can process these
  1419. * new nodes.
  1420. */
  1421. break;
  1422. }
  1423. level++;
  1424. }
  1425. eb = path->nodes[root_level];
  1426. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  1427. return 1;
  1428. return 0;
  1429. }
  1430. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  1431. struct btrfs_root *root,
  1432. struct extent_buffer *root_eb,
  1433. u64 root_gen, int root_level)
  1434. {
  1435. struct btrfs_fs_info *fs_info = root->fs_info;
  1436. int ret = 0;
  1437. int level;
  1438. struct extent_buffer *eb = root_eb;
  1439. struct btrfs_path *path = NULL;
  1440. BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
  1441. BUG_ON(root_eb == NULL);
  1442. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1443. return 0;
  1444. if (!extent_buffer_uptodate(root_eb)) {
  1445. ret = btrfs_read_buffer(root_eb, root_gen);
  1446. if (ret)
  1447. goto out;
  1448. }
  1449. if (root_level == 0) {
  1450. ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
  1451. goto out;
  1452. }
  1453. path = btrfs_alloc_path();
  1454. if (!path)
  1455. return -ENOMEM;
  1456. /*
  1457. * Walk down the tree. Missing extent blocks are filled in as
  1458. * we go. Metadata is accounted every time we read a new
  1459. * extent block.
  1460. *
  1461. * When we reach a leaf, we account for file extent items in it,
  1462. * walk back up the tree (adjusting slot pointers as we go)
  1463. * and restart the search process.
  1464. */
  1465. extent_buffer_get(root_eb); /* For path */
  1466. path->nodes[root_level] = root_eb;
  1467. path->slots[root_level] = 0;
  1468. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  1469. walk_down:
  1470. level = root_level;
  1471. while (level >= 0) {
  1472. if (path->nodes[level] == NULL) {
  1473. int parent_slot;
  1474. u64 child_gen;
  1475. u64 child_bytenr;
  1476. /*
  1477. * We need to get child blockptr/gen from parent before
  1478. * we can read it.
  1479. */
  1480. eb = path->nodes[level + 1];
  1481. parent_slot = path->slots[level + 1];
  1482. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  1483. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  1484. eb = read_tree_block(fs_info, child_bytenr, child_gen);
  1485. if (IS_ERR(eb)) {
  1486. ret = PTR_ERR(eb);
  1487. goto out;
  1488. } else if (!extent_buffer_uptodate(eb)) {
  1489. free_extent_buffer(eb);
  1490. ret = -EIO;
  1491. goto out;
  1492. }
  1493. path->nodes[level] = eb;
  1494. path->slots[level] = 0;
  1495. btrfs_tree_read_lock(eb);
  1496. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1497. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  1498. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  1499. child_bytenr,
  1500. fs_info->nodesize,
  1501. GFP_NOFS);
  1502. if (ret)
  1503. goto out;
  1504. }
  1505. if (level == 0) {
  1506. ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
  1507. path->nodes[level]);
  1508. if (ret)
  1509. goto out;
  1510. /* Nonzero return here means we completed our search */
  1511. ret = adjust_slots_upwards(path, root_level);
  1512. if (ret)
  1513. break;
  1514. /* Restart search with new slots */
  1515. goto walk_down;
  1516. }
  1517. level--;
  1518. }
  1519. ret = 0;
  1520. out:
  1521. btrfs_free_path(path);
  1522. return ret;
  1523. }
  1524. #define UPDATE_NEW 0
  1525. #define UPDATE_OLD 1
  1526. /*
  1527. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1528. */
  1529. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1530. struct ulist *roots, struct ulist *tmp,
  1531. struct ulist *qgroups, u64 seq, int update_old)
  1532. {
  1533. struct ulist_node *unode;
  1534. struct ulist_iterator uiter;
  1535. struct ulist_node *tmp_unode;
  1536. struct ulist_iterator tmp_uiter;
  1537. struct btrfs_qgroup *qg;
  1538. int ret = 0;
  1539. if (!roots)
  1540. return 0;
  1541. ULIST_ITER_INIT(&uiter);
  1542. while ((unode = ulist_next(roots, &uiter))) {
  1543. qg = find_qgroup_rb(fs_info, unode->val);
  1544. if (!qg)
  1545. continue;
  1546. ulist_reinit(tmp);
  1547. ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
  1548. GFP_ATOMIC);
  1549. if (ret < 0)
  1550. return ret;
  1551. ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
  1552. if (ret < 0)
  1553. return ret;
  1554. ULIST_ITER_INIT(&tmp_uiter);
  1555. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1556. struct btrfs_qgroup_list *glist;
  1557. qg = unode_aux_to_qgroup(tmp_unode);
  1558. if (update_old)
  1559. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1560. else
  1561. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1562. list_for_each_entry(glist, &qg->groups, next_group) {
  1563. ret = ulist_add(qgroups, glist->group->qgroupid,
  1564. qgroup_to_aux(glist->group),
  1565. GFP_ATOMIC);
  1566. if (ret < 0)
  1567. return ret;
  1568. ret = ulist_add(tmp, glist->group->qgroupid,
  1569. qgroup_to_aux(glist->group),
  1570. GFP_ATOMIC);
  1571. if (ret < 0)
  1572. return ret;
  1573. }
  1574. }
  1575. }
  1576. return 0;
  1577. }
  1578. /*
  1579. * Update qgroup rfer/excl counters.
  1580. * Rfer update is easy, codes can explain themselves.
  1581. *
  1582. * Excl update is tricky, the update is split into 2 part.
  1583. * Part 1: Possible exclusive <-> sharing detect:
  1584. * | A | !A |
  1585. * -------------------------------------
  1586. * B | * | - |
  1587. * -------------------------------------
  1588. * !B | + | ** |
  1589. * -------------------------------------
  1590. *
  1591. * Conditions:
  1592. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1593. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1594. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1595. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  1596. *
  1597. * Results:
  1598. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1599. * *: Definitely not changed. **: Possible unchanged.
  1600. *
  1601. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1602. *
  1603. * To make the logic clear, we first use condition A and B to split
  1604. * combination into 4 results.
  1605. *
  1606. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1607. * only on variant maybe 0.
  1608. *
  1609. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1610. * again(2x2).
  1611. * But this time we don't need to consider other things, the codes and logic
  1612. * is easy to understand now.
  1613. */
  1614. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1615. struct ulist *qgroups,
  1616. u64 nr_old_roots,
  1617. u64 nr_new_roots,
  1618. u64 num_bytes, u64 seq)
  1619. {
  1620. struct ulist_node *unode;
  1621. struct ulist_iterator uiter;
  1622. struct btrfs_qgroup *qg;
  1623. u64 cur_new_count, cur_old_count;
  1624. ULIST_ITER_INIT(&uiter);
  1625. while ((unode = ulist_next(qgroups, &uiter))) {
  1626. bool dirty = false;
  1627. qg = unode_aux_to_qgroup(unode);
  1628. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1629. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1630. trace_qgroup_update_counters(fs_info, qg->qgroupid,
  1631. cur_old_count, cur_new_count);
  1632. /* Rfer update part */
  1633. if (cur_old_count == 0 && cur_new_count > 0) {
  1634. qg->rfer += num_bytes;
  1635. qg->rfer_cmpr += num_bytes;
  1636. dirty = true;
  1637. }
  1638. if (cur_old_count > 0 && cur_new_count == 0) {
  1639. qg->rfer -= num_bytes;
  1640. qg->rfer_cmpr -= num_bytes;
  1641. dirty = true;
  1642. }
  1643. /* Excl update part */
  1644. /* Exclusive/none -> shared case */
  1645. if (cur_old_count == nr_old_roots &&
  1646. cur_new_count < nr_new_roots) {
  1647. /* Exclusive -> shared */
  1648. if (cur_old_count != 0) {
  1649. qg->excl -= num_bytes;
  1650. qg->excl_cmpr -= num_bytes;
  1651. dirty = true;
  1652. }
  1653. }
  1654. /* Shared -> exclusive/none case */
  1655. if (cur_old_count < nr_old_roots &&
  1656. cur_new_count == nr_new_roots) {
  1657. /* Shared->exclusive */
  1658. if (cur_new_count != 0) {
  1659. qg->excl += num_bytes;
  1660. qg->excl_cmpr += num_bytes;
  1661. dirty = true;
  1662. }
  1663. }
  1664. /* Exclusive/none -> exclusive/none case */
  1665. if (cur_old_count == nr_old_roots &&
  1666. cur_new_count == nr_new_roots) {
  1667. if (cur_old_count == 0) {
  1668. /* None -> exclusive/none */
  1669. if (cur_new_count != 0) {
  1670. /* None -> exclusive */
  1671. qg->excl += num_bytes;
  1672. qg->excl_cmpr += num_bytes;
  1673. dirty = true;
  1674. }
  1675. /* None -> none, nothing changed */
  1676. } else {
  1677. /* Exclusive -> exclusive/none */
  1678. if (cur_new_count == 0) {
  1679. /* Exclusive -> none */
  1680. qg->excl -= num_bytes;
  1681. qg->excl_cmpr -= num_bytes;
  1682. dirty = true;
  1683. }
  1684. /* Exclusive -> exclusive, nothing changed */
  1685. }
  1686. }
  1687. if (dirty)
  1688. qgroup_dirty(fs_info, qg);
  1689. }
  1690. return 0;
  1691. }
  1692. int
  1693. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1694. struct btrfs_fs_info *fs_info,
  1695. u64 bytenr, u64 num_bytes,
  1696. struct ulist *old_roots, struct ulist *new_roots)
  1697. {
  1698. struct ulist *qgroups = NULL;
  1699. struct ulist *tmp = NULL;
  1700. u64 seq;
  1701. u64 nr_new_roots = 0;
  1702. u64 nr_old_roots = 0;
  1703. int ret = 0;
  1704. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1705. return 0;
  1706. if (new_roots)
  1707. nr_new_roots = new_roots->nnodes;
  1708. if (old_roots)
  1709. nr_old_roots = old_roots->nnodes;
  1710. BUG_ON(!fs_info->quota_root);
  1711. trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
  1712. nr_old_roots, nr_new_roots);
  1713. qgroups = ulist_alloc(GFP_NOFS);
  1714. if (!qgroups) {
  1715. ret = -ENOMEM;
  1716. goto out_free;
  1717. }
  1718. tmp = ulist_alloc(GFP_NOFS);
  1719. if (!tmp) {
  1720. ret = -ENOMEM;
  1721. goto out_free;
  1722. }
  1723. mutex_lock(&fs_info->qgroup_rescan_lock);
  1724. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1725. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1726. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1727. ret = 0;
  1728. goto out_free;
  1729. }
  1730. }
  1731. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1732. spin_lock(&fs_info->qgroup_lock);
  1733. seq = fs_info->qgroup_seq;
  1734. /* Update old refcnts using old_roots */
  1735. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1736. UPDATE_OLD);
  1737. if (ret < 0)
  1738. goto out;
  1739. /* Update new refcnts using new_roots */
  1740. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1741. UPDATE_NEW);
  1742. if (ret < 0)
  1743. goto out;
  1744. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1745. num_bytes, seq);
  1746. /*
  1747. * Bump qgroup_seq to avoid seq overlap
  1748. */
  1749. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1750. out:
  1751. spin_unlock(&fs_info->qgroup_lock);
  1752. out_free:
  1753. ulist_free(tmp);
  1754. ulist_free(qgroups);
  1755. ulist_free(old_roots);
  1756. ulist_free(new_roots);
  1757. return ret;
  1758. }
  1759. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1760. struct btrfs_fs_info *fs_info)
  1761. {
  1762. struct btrfs_qgroup_extent_record *record;
  1763. struct btrfs_delayed_ref_root *delayed_refs;
  1764. struct ulist *new_roots = NULL;
  1765. struct rb_node *node;
  1766. u64 qgroup_to_skip;
  1767. int ret = 0;
  1768. delayed_refs = &trans->transaction->delayed_refs;
  1769. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1770. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1771. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1772. node);
  1773. trace_btrfs_qgroup_account_extents(fs_info, record);
  1774. if (!ret) {
  1775. /*
  1776. * Use (u64)-1 as time_seq to do special search, which
  1777. * doesn't lock tree or delayed_refs and search current
  1778. * root. It's safe inside commit_transaction().
  1779. */
  1780. ret = btrfs_find_all_roots(trans, fs_info,
  1781. record->bytenr, (u64)-1, &new_roots);
  1782. if (ret < 0)
  1783. goto cleanup;
  1784. if (qgroup_to_skip)
  1785. ulist_del(new_roots, qgroup_to_skip, 0);
  1786. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1787. record->bytenr, record->num_bytes,
  1788. record->old_roots, new_roots);
  1789. record->old_roots = NULL;
  1790. new_roots = NULL;
  1791. }
  1792. cleanup:
  1793. ulist_free(record->old_roots);
  1794. ulist_free(new_roots);
  1795. new_roots = NULL;
  1796. rb_erase(node, &delayed_refs->dirty_extent_root);
  1797. kfree(record);
  1798. }
  1799. return ret;
  1800. }
  1801. /*
  1802. * called from commit_transaction. Writes all changed qgroups to disk.
  1803. */
  1804. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1805. struct btrfs_fs_info *fs_info)
  1806. {
  1807. struct btrfs_root *quota_root = fs_info->quota_root;
  1808. int ret = 0;
  1809. int start_rescan_worker = 0;
  1810. if (!quota_root)
  1811. goto out;
  1812. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
  1813. test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1814. start_rescan_worker = 1;
  1815. if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1816. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1817. if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
  1818. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1819. spin_lock(&fs_info->qgroup_lock);
  1820. while (!list_empty(&fs_info->dirty_qgroups)) {
  1821. struct btrfs_qgroup *qgroup;
  1822. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1823. struct btrfs_qgroup, dirty);
  1824. list_del_init(&qgroup->dirty);
  1825. spin_unlock(&fs_info->qgroup_lock);
  1826. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1827. if (ret)
  1828. fs_info->qgroup_flags |=
  1829. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1830. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1831. if (ret)
  1832. fs_info->qgroup_flags |=
  1833. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1834. spin_lock(&fs_info->qgroup_lock);
  1835. }
  1836. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1837. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1838. else
  1839. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1840. spin_unlock(&fs_info->qgroup_lock);
  1841. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1842. if (ret)
  1843. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1844. if (!ret && start_rescan_worker) {
  1845. ret = qgroup_rescan_init(fs_info, 0, 1);
  1846. if (!ret) {
  1847. qgroup_rescan_zero_tracking(fs_info);
  1848. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1849. &fs_info->qgroup_rescan_work);
  1850. }
  1851. ret = 0;
  1852. }
  1853. out:
  1854. return ret;
  1855. }
  1856. /*
  1857. * Copy the accounting information between qgroups. This is necessary
  1858. * when a snapshot or a subvolume is created. Throwing an error will
  1859. * cause a transaction abort so we take extra care here to only error
  1860. * when a readonly fs is a reasonable outcome.
  1861. */
  1862. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1863. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1864. struct btrfs_qgroup_inherit *inherit)
  1865. {
  1866. int ret = 0;
  1867. int i;
  1868. u64 *i_qgroups;
  1869. struct btrfs_root *quota_root = fs_info->quota_root;
  1870. struct btrfs_qgroup *srcgroup;
  1871. struct btrfs_qgroup *dstgroup;
  1872. u32 level_size = 0;
  1873. u64 nums;
  1874. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1875. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1876. goto out;
  1877. if (!quota_root) {
  1878. ret = -EINVAL;
  1879. goto out;
  1880. }
  1881. if (inherit) {
  1882. i_qgroups = (u64 *)(inherit + 1);
  1883. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1884. 2 * inherit->num_excl_copies;
  1885. for (i = 0; i < nums; ++i) {
  1886. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1887. /*
  1888. * Zero out invalid groups so we can ignore
  1889. * them later.
  1890. */
  1891. if (!srcgroup ||
  1892. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  1893. *i_qgroups = 0ULL;
  1894. ++i_qgroups;
  1895. }
  1896. }
  1897. /*
  1898. * create a tracking group for the subvol itself
  1899. */
  1900. ret = add_qgroup_item(trans, quota_root, objectid);
  1901. if (ret)
  1902. goto out;
  1903. if (srcid) {
  1904. struct btrfs_root *srcroot;
  1905. struct btrfs_key srckey;
  1906. srckey.objectid = srcid;
  1907. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1908. srckey.offset = (u64)-1;
  1909. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1910. if (IS_ERR(srcroot)) {
  1911. ret = PTR_ERR(srcroot);
  1912. goto out;
  1913. }
  1914. level_size = fs_info->nodesize;
  1915. }
  1916. /*
  1917. * add qgroup to all inherited groups
  1918. */
  1919. if (inherit) {
  1920. i_qgroups = (u64 *)(inherit + 1);
  1921. for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
  1922. if (*i_qgroups == 0)
  1923. continue;
  1924. ret = add_qgroup_relation_item(trans, quota_root,
  1925. objectid, *i_qgroups);
  1926. if (ret && ret != -EEXIST)
  1927. goto out;
  1928. ret = add_qgroup_relation_item(trans, quota_root,
  1929. *i_qgroups, objectid);
  1930. if (ret && ret != -EEXIST)
  1931. goto out;
  1932. }
  1933. ret = 0;
  1934. }
  1935. spin_lock(&fs_info->qgroup_lock);
  1936. dstgroup = add_qgroup_rb(fs_info, objectid);
  1937. if (IS_ERR(dstgroup)) {
  1938. ret = PTR_ERR(dstgroup);
  1939. goto unlock;
  1940. }
  1941. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1942. dstgroup->lim_flags = inherit->lim.flags;
  1943. dstgroup->max_rfer = inherit->lim.max_rfer;
  1944. dstgroup->max_excl = inherit->lim.max_excl;
  1945. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1946. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1947. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1948. if (ret) {
  1949. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1950. btrfs_info(fs_info,
  1951. "unable to update quota limit for %llu",
  1952. dstgroup->qgroupid);
  1953. goto unlock;
  1954. }
  1955. }
  1956. if (srcid) {
  1957. srcgroup = find_qgroup_rb(fs_info, srcid);
  1958. if (!srcgroup)
  1959. goto unlock;
  1960. /*
  1961. * We call inherit after we clone the root in order to make sure
  1962. * our counts don't go crazy, so at this point the only
  1963. * difference between the two roots should be the root node.
  1964. */
  1965. dstgroup->rfer = srcgroup->rfer;
  1966. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1967. dstgroup->excl = level_size;
  1968. dstgroup->excl_cmpr = level_size;
  1969. srcgroup->excl = level_size;
  1970. srcgroup->excl_cmpr = level_size;
  1971. /* inherit the limit info */
  1972. dstgroup->lim_flags = srcgroup->lim_flags;
  1973. dstgroup->max_rfer = srcgroup->max_rfer;
  1974. dstgroup->max_excl = srcgroup->max_excl;
  1975. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1976. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1977. qgroup_dirty(fs_info, dstgroup);
  1978. qgroup_dirty(fs_info, srcgroup);
  1979. }
  1980. if (!inherit)
  1981. goto unlock;
  1982. i_qgroups = (u64 *)(inherit + 1);
  1983. for (i = 0; i < inherit->num_qgroups; ++i) {
  1984. if (*i_qgroups) {
  1985. ret = add_relation_rb(fs_info, objectid, *i_qgroups);
  1986. if (ret)
  1987. goto unlock;
  1988. }
  1989. ++i_qgroups;
  1990. }
  1991. for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
  1992. struct btrfs_qgroup *src;
  1993. struct btrfs_qgroup *dst;
  1994. if (!i_qgroups[0] || !i_qgroups[1])
  1995. continue;
  1996. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1997. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1998. if (!src || !dst) {
  1999. ret = -EINVAL;
  2000. goto unlock;
  2001. }
  2002. dst->rfer = src->rfer - level_size;
  2003. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  2004. }
  2005. for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
  2006. struct btrfs_qgroup *src;
  2007. struct btrfs_qgroup *dst;
  2008. if (!i_qgroups[0] || !i_qgroups[1])
  2009. continue;
  2010. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2011. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2012. if (!src || !dst) {
  2013. ret = -EINVAL;
  2014. goto unlock;
  2015. }
  2016. dst->excl = src->excl + level_size;
  2017. dst->excl_cmpr = src->excl_cmpr + level_size;
  2018. }
  2019. unlock:
  2020. spin_unlock(&fs_info->qgroup_lock);
  2021. out:
  2022. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2023. return ret;
  2024. }
  2025. static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
  2026. {
  2027. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2028. qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
  2029. return false;
  2030. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2031. qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
  2032. return false;
  2033. return true;
  2034. }
  2035. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
  2036. {
  2037. struct btrfs_root *quota_root;
  2038. struct btrfs_qgroup *qgroup;
  2039. struct btrfs_fs_info *fs_info = root->fs_info;
  2040. u64 ref_root = root->root_key.objectid;
  2041. int ret = 0;
  2042. struct ulist_node *unode;
  2043. struct ulist_iterator uiter;
  2044. if (!is_fstree(ref_root))
  2045. return 0;
  2046. if (num_bytes == 0)
  2047. return 0;
  2048. spin_lock(&fs_info->qgroup_lock);
  2049. quota_root = fs_info->quota_root;
  2050. if (!quota_root)
  2051. goto out;
  2052. qgroup = find_qgroup_rb(fs_info, ref_root);
  2053. if (!qgroup)
  2054. goto out;
  2055. /*
  2056. * in a first step, we check all affected qgroups if any limits would
  2057. * be exceeded
  2058. */
  2059. ulist_reinit(fs_info->qgroup_ulist);
  2060. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2061. (uintptr_t)qgroup, GFP_ATOMIC);
  2062. if (ret < 0)
  2063. goto out;
  2064. ULIST_ITER_INIT(&uiter);
  2065. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2066. struct btrfs_qgroup *qg;
  2067. struct btrfs_qgroup_list *glist;
  2068. qg = unode_aux_to_qgroup(unode);
  2069. if (enforce && !qgroup_check_limits(qg, num_bytes)) {
  2070. ret = -EDQUOT;
  2071. goto out;
  2072. }
  2073. list_for_each_entry(glist, &qg->groups, next_group) {
  2074. ret = ulist_add(fs_info->qgroup_ulist,
  2075. glist->group->qgroupid,
  2076. (uintptr_t)glist->group, GFP_ATOMIC);
  2077. if (ret < 0)
  2078. goto out;
  2079. }
  2080. }
  2081. ret = 0;
  2082. /*
  2083. * no limits exceeded, now record the reservation into all qgroups
  2084. */
  2085. ULIST_ITER_INIT(&uiter);
  2086. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2087. struct btrfs_qgroup *qg;
  2088. qg = unode_aux_to_qgroup(unode);
  2089. qg->reserved += num_bytes;
  2090. }
  2091. out:
  2092. spin_unlock(&fs_info->qgroup_lock);
  2093. return ret;
  2094. }
  2095. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  2096. u64 ref_root, u64 num_bytes)
  2097. {
  2098. struct btrfs_root *quota_root;
  2099. struct btrfs_qgroup *qgroup;
  2100. struct ulist_node *unode;
  2101. struct ulist_iterator uiter;
  2102. int ret = 0;
  2103. if (!is_fstree(ref_root))
  2104. return;
  2105. if (num_bytes == 0)
  2106. return;
  2107. spin_lock(&fs_info->qgroup_lock);
  2108. quota_root = fs_info->quota_root;
  2109. if (!quota_root)
  2110. goto out;
  2111. qgroup = find_qgroup_rb(fs_info, ref_root);
  2112. if (!qgroup)
  2113. goto out;
  2114. ulist_reinit(fs_info->qgroup_ulist);
  2115. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2116. (uintptr_t)qgroup, GFP_ATOMIC);
  2117. if (ret < 0)
  2118. goto out;
  2119. ULIST_ITER_INIT(&uiter);
  2120. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2121. struct btrfs_qgroup *qg;
  2122. struct btrfs_qgroup_list *glist;
  2123. qg = unode_aux_to_qgroup(unode);
  2124. if (WARN_ON(qg->reserved < num_bytes))
  2125. report_reserved_underflow(fs_info, qg, num_bytes);
  2126. else
  2127. qg->reserved -= num_bytes;
  2128. list_for_each_entry(glist, &qg->groups, next_group) {
  2129. ret = ulist_add(fs_info->qgroup_ulist,
  2130. glist->group->qgroupid,
  2131. (uintptr_t)glist->group, GFP_ATOMIC);
  2132. if (ret < 0)
  2133. goto out;
  2134. }
  2135. }
  2136. out:
  2137. spin_unlock(&fs_info->qgroup_lock);
  2138. }
  2139. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  2140. {
  2141. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  2142. return;
  2143. btrfs_err(trans->fs_info,
  2144. "qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x",
  2145. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2146. (u32)(trans->delayed_ref_elem.seq >> 32),
  2147. (u32)trans->delayed_ref_elem.seq);
  2148. BUG();
  2149. }
  2150. /*
  2151. * returns < 0 on error, 0 when more leafs are to be scanned.
  2152. * returns 1 when done.
  2153. */
  2154. static int
  2155. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2156. struct btrfs_trans_handle *trans)
  2157. {
  2158. struct btrfs_key found;
  2159. struct extent_buffer *scratch_leaf = NULL;
  2160. struct ulist *roots = NULL;
  2161. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  2162. u64 num_bytes;
  2163. int slot;
  2164. int ret;
  2165. mutex_lock(&fs_info->qgroup_rescan_lock);
  2166. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2167. &fs_info->qgroup_rescan_progress,
  2168. path, 1, 0);
  2169. btrfs_debug(fs_info,
  2170. "current progress key (%llu %u %llu), search_slot ret %d",
  2171. fs_info->qgroup_rescan_progress.objectid,
  2172. fs_info->qgroup_rescan_progress.type,
  2173. fs_info->qgroup_rescan_progress.offset, ret);
  2174. if (ret) {
  2175. /*
  2176. * The rescan is about to end, we will not be scanning any
  2177. * further blocks. We cannot unset the RESCAN flag here, because
  2178. * we want to commit the transaction if everything went well.
  2179. * To make the live accounting work in this phase, we set our
  2180. * scan progress pointer such that every real extent objectid
  2181. * will be smaller.
  2182. */
  2183. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2184. btrfs_release_path(path);
  2185. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2186. return ret;
  2187. }
  2188. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2189. btrfs_header_nritems(path->nodes[0]) - 1);
  2190. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2191. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2192. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  2193. if (!scratch_leaf) {
  2194. ret = -ENOMEM;
  2195. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2196. goto out;
  2197. }
  2198. extent_buffer_get(scratch_leaf);
  2199. btrfs_tree_read_lock(scratch_leaf);
  2200. btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
  2201. slot = path->slots[0];
  2202. btrfs_release_path(path);
  2203. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2204. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2205. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2206. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2207. found.type != BTRFS_METADATA_ITEM_KEY)
  2208. continue;
  2209. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2210. num_bytes = fs_info->nodesize;
  2211. else
  2212. num_bytes = found.offset;
  2213. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2214. &roots);
  2215. if (ret < 0)
  2216. goto out;
  2217. /* For rescan, just pass old_roots as NULL */
  2218. ret = btrfs_qgroup_account_extent(trans, fs_info,
  2219. found.objectid, num_bytes, NULL, roots);
  2220. if (ret < 0)
  2221. goto out;
  2222. }
  2223. out:
  2224. if (scratch_leaf) {
  2225. btrfs_tree_read_unlock_blocking(scratch_leaf);
  2226. free_extent_buffer(scratch_leaf);
  2227. }
  2228. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2229. return ret;
  2230. }
  2231. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2232. {
  2233. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2234. qgroup_rescan_work);
  2235. struct btrfs_path *path;
  2236. struct btrfs_trans_handle *trans = NULL;
  2237. int err = -ENOMEM;
  2238. int ret = 0;
  2239. path = btrfs_alloc_path();
  2240. if (!path)
  2241. goto out;
  2242. err = 0;
  2243. while (!err && !btrfs_fs_closing(fs_info)) {
  2244. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2245. if (IS_ERR(trans)) {
  2246. err = PTR_ERR(trans);
  2247. break;
  2248. }
  2249. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  2250. err = -EINTR;
  2251. } else {
  2252. err = qgroup_rescan_leaf(fs_info, path, trans);
  2253. }
  2254. if (err > 0)
  2255. btrfs_commit_transaction(trans);
  2256. else
  2257. btrfs_end_transaction(trans);
  2258. }
  2259. out:
  2260. btrfs_free_path(path);
  2261. mutex_lock(&fs_info->qgroup_rescan_lock);
  2262. if (!btrfs_fs_closing(fs_info))
  2263. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2264. if (err > 0 &&
  2265. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2266. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2267. } else if (err < 0) {
  2268. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2269. }
  2270. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2271. /*
  2272. * only update status, since the previous part has already updated the
  2273. * qgroup info.
  2274. */
  2275. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2276. if (IS_ERR(trans)) {
  2277. err = PTR_ERR(trans);
  2278. btrfs_err(fs_info,
  2279. "fail to start transaction for status update: %d\n",
  2280. err);
  2281. goto done;
  2282. }
  2283. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2284. if (ret < 0) {
  2285. err = ret;
  2286. btrfs_err(fs_info, "fail to update qgroup status: %d", err);
  2287. }
  2288. btrfs_end_transaction(trans);
  2289. if (btrfs_fs_closing(fs_info)) {
  2290. btrfs_info(fs_info, "qgroup scan paused");
  2291. } else if (err >= 0) {
  2292. btrfs_info(fs_info, "qgroup scan completed%s",
  2293. err > 0 ? " (inconsistency flag cleared)" : "");
  2294. } else {
  2295. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2296. }
  2297. done:
  2298. mutex_lock(&fs_info->qgroup_rescan_lock);
  2299. fs_info->qgroup_rescan_running = false;
  2300. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2301. complete_all(&fs_info->qgroup_rescan_completion);
  2302. }
  2303. /*
  2304. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2305. * memory required for the rescan context.
  2306. */
  2307. static int
  2308. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2309. int init_flags)
  2310. {
  2311. int ret = 0;
  2312. if (!init_flags &&
  2313. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2314. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2315. ret = -EINVAL;
  2316. goto err;
  2317. }
  2318. mutex_lock(&fs_info->qgroup_rescan_lock);
  2319. spin_lock(&fs_info->qgroup_lock);
  2320. if (init_flags) {
  2321. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2322. ret = -EINPROGRESS;
  2323. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2324. ret = -EINVAL;
  2325. if (ret) {
  2326. spin_unlock(&fs_info->qgroup_lock);
  2327. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2328. goto err;
  2329. }
  2330. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2331. }
  2332. memset(&fs_info->qgroup_rescan_progress, 0,
  2333. sizeof(fs_info->qgroup_rescan_progress));
  2334. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2335. init_completion(&fs_info->qgroup_rescan_completion);
  2336. fs_info->qgroup_rescan_running = true;
  2337. spin_unlock(&fs_info->qgroup_lock);
  2338. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2339. memset(&fs_info->qgroup_rescan_work, 0,
  2340. sizeof(fs_info->qgroup_rescan_work));
  2341. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2342. btrfs_qgroup_rescan_helper,
  2343. btrfs_qgroup_rescan_worker, NULL, NULL);
  2344. if (ret) {
  2345. err:
  2346. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2347. return ret;
  2348. }
  2349. return 0;
  2350. }
  2351. static void
  2352. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2353. {
  2354. struct rb_node *n;
  2355. struct btrfs_qgroup *qgroup;
  2356. spin_lock(&fs_info->qgroup_lock);
  2357. /* clear all current qgroup tracking information */
  2358. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2359. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2360. qgroup->rfer = 0;
  2361. qgroup->rfer_cmpr = 0;
  2362. qgroup->excl = 0;
  2363. qgroup->excl_cmpr = 0;
  2364. }
  2365. spin_unlock(&fs_info->qgroup_lock);
  2366. }
  2367. int
  2368. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2369. {
  2370. int ret = 0;
  2371. struct btrfs_trans_handle *trans;
  2372. ret = qgroup_rescan_init(fs_info, 0, 1);
  2373. if (ret)
  2374. return ret;
  2375. /*
  2376. * We have set the rescan_progress to 0, which means no more
  2377. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2378. * However, btrfs_qgroup_account_ref may be right after its call
  2379. * to btrfs_find_all_roots, in which case it would still do the
  2380. * accounting.
  2381. * To solve this, we're committing the transaction, which will
  2382. * ensure we run all delayed refs and only after that, we are
  2383. * going to clear all tracking information for a clean start.
  2384. */
  2385. trans = btrfs_join_transaction(fs_info->fs_root);
  2386. if (IS_ERR(trans)) {
  2387. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2388. return PTR_ERR(trans);
  2389. }
  2390. ret = btrfs_commit_transaction(trans);
  2391. if (ret) {
  2392. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2393. return ret;
  2394. }
  2395. qgroup_rescan_zero_tracking(fs_info);
  2396. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2397. &fs_info->qgroup_rescan_work);
  2398. return 0;
  2399. }
  2400. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  2401. bool interruptible)
  2402. {
  2403. int running;
  2404. int ret = 0;
  2405. mutex_lock(&fs_info->qgroup_rescan_lock);
  2406. spin_lock(&fs_info->qgroup_lock);
  2407. running = fs_info->qgroup_rescan_running;
  2408. spin_unlock(&fs_info->qgroup_lock);
  2409. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2410. if (!running)
  2411. return 0;
  2412. if (interruptible)
  2413. ret = wait_for_completion_interruptible(
  2414. &fs_info->qgroup_rescan_completion);
  2415. else
  2416. wait_for_completion(&fs_info->qgroup_rescan_completion);
  2417. return ret;
  2418. }
  2419. /*
  2420. * this is only called from open_ctree where we're still single threaded, thus
  2421. * locking is omitted here.
  2422. */
  2423. void
  2424. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2425. {
  2426. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2427. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2428. &fs_info->qgroup_rescan_work);
  2429. }
  2430. /*
  2431. * Reserve qgroup space for range [start, start + len).
  2432. *
  2433. * This function will either reserve space from related qgroups or doing
  2434. * nothing if the range is already reserved.
  2435. *
  2436. * Return 0 for successful reserve
  2437. * Return <0 for error (including -EQUOT)
  2438. *
  2439. * NOTE: this function may sleep for memory allocation.
  2440. */
  2441. int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
  2442. {
  2443. struct btrfs_root *root = BTRFS_I(inode)->root;
  2444. struct extent_changeset changeset;
  2445. struct ulist_node *unode;
  2446. struct ulist_iterator uiter;
  2447. int ret;
  2448. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2449. !is_fstree(root->objectid) || len == 0)
  2450. return 0;
  2451. changeset.bytes_changed = 0;
  2452. ulist_init(&changeset.range_changed);
  2453. ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2454. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2455. trace_btrfs_qgroup_reserve_data(inode, start, len,
  2456. changeset.bytes_changed,
  2457. QGROUP_RESERVE);
  2458. if (ret < 0)
  2459. goto cleanup;
  2460. ret = qgroup_reserve(root, changeset.bytes_changed, true);
  2461. if (ret < 0)
  2462. goto cleanup;
  2463. ulist_release(&changeset.range_changed);
  2464. return ret;
  2465. cleanup:
  2466. /* cleanup already reserved ranges */
  2467. ULIST_ITER_INIT(&uiter);
  2468. while ((unode = ulist_next(&changeset.range_changed, &uiter)))
  2469. clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
  2470. unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
  2471. GFP_NOFS);
  2472. ulist_release(&changeset.range_changed);
  2473. return ret;
  2474. }
  2475. static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
  2476. int free)
  2477. {
  2478. struct extent_changeset changeset;
  2479. int trace_op = QGROUP_RELEASE;
  2480. int ret;
  2481. changeset.bytes_changed = 0;
  2482. ulist_init(&changeset.range_changed);
  2483. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2484. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2485. if (ret < 0)
  2486. goto out;
  2487. if (free) {
  2488. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2489. BTRFS_I(inode)->root->objectid,
  2490. changeset.bytes_changed);
  2491. trace_op = QGROUP_FREE;
  2492. }
  2493. trace_btrfs_qgroup_release_data(inode, start, len,
  2494. changeset.bytes_changed, trace_op);
  2495. out:
  2496. ulist_release(&changeset.range_changed);
  2497. return ret;
  2498. }
  2499. /*
  2500. * Free a reserved space range from io_tree and related qgroups
  2501. *
  2502. * Should be called when a range of pages get invalidated before reaching disk.
  2503. * Or for error cleanup case.
  2504. *
  2505. * For data written to disk, use btrfs_qgroup_release_data().
  2506. *
  2507. * NOTE: This function may sleep for memory allocation.
  2508. */
  2509. int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
  2510. {
  2511. return __btrfs_qgroup_release_data(inode, start, len, 1);
  2512. }
  2513. /*
  2514. * Release a reserved space range from io_tree only.
  2515. *
  2516. * Should be called when a range of pages get written to disk and corresponding
  2517. * FILE_EXTENT is inserted into corresponding root.
  2518. *
  2519. * Since new qgroup accounting framework will only update qgroup numbers at
  2520. * commit_transaction() time, its reserved space shouldn't be freed from
  2521. * related qgroups.
  2522. *
  2523. * But we should release the range from io_tree, to allow further write to be
  2524. * COWed.
  2525. *
  2526. * NOTE: This function may sleep for memory allocation.
  2527. */
  2528. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
  2529. {
  2530. return __btrfs_qgroup_release_data(inode, start, len, 0);
  2531. }
  2532. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  2533. bool enforce)
  2534. {
  2535. struct btrfs_fs_info *fs_info = root->fs_info;
  2536. int ret;
  2537. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2538. !is_fstree(root->objectid) || num_bytes == 0)
  2539. return 0;
  2540. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2541. ret = qgroup_reserve(root, num_bytes, enforce);
  2542. if (ret < 0)
  2543. return ret;
  2544. atomic_add(num_bytes, &root->qgroup_meta_rsv);
  2545. return ret;
  2546. }
  2547. void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
  2548. {
  2549. struct btrfs_fs_info *fs_info = root->fs_info;
  2550. int reserved;
  2551. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2552. !is_fstree(root->objectid))
  2553. return;
  2554. reserved = atomic_xchg(&root->qgroup_meta_rsv, 0);
  2555. if (reserved == 0)
  2556. return;
  2557. btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
  2558. }
  2559. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
  2560. {
  2561. struct btrfs_fs_info *fs_info = root->fs_info;
  2562. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2563. !is_fstree(root->objectid))
  2564. return;
  2565. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2566. WARN_ON(atomic_read(&root->qgroup_meta_rsv) < num_bytes);
  2567. atomic_sub(num_bytes, &root->qgroup_meta_rsv);
  2568. btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
  2569. }
  2570. /*
  2571. * Check qgroup reserved space leaking, normally at destroy inode
  2572. * time
  2573. */
  2574. void btrfs_qgroup_check_reserved_leak(struct inode *inode)
  2575. {
  2576. struct extent_changeset changeset;
  2577. struct ulist_node *unode;
  2578. struct ulist_iterator iter;
  2579. int ret;
  2580. changeset.bytes_changed = 0;
  2581. ulist_init(&changeset.range_changed);
  2582. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  2583. EXTENT_QGROUP_RESERVED, &changeset);
  2584. WARN_ON(ret < 0);
  2585. if (WARN_ON(changeset.bytes_changed)) {
  2586. ULIST_ITER_INIT(&iter);
  2587. while ((unode = ulist_next(&changeset.range_changed, &iter))) {
  2588. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2589. "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
  2590. inode->i_ino, unode->val, unode->aux);
  2591. }
  2592. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2593. BTRFS_I(inode)->root->objectid,
  2594. changeset.bytes_changed);
  2595. }
  2596. ulist_release(&changeset.range_changed);
  2597. }