qgroup.c 75 KB

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