qgroup.c 74 KB

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