qgroup.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  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. spin_unlock(&fs_info->qgroup_lock);
  825. btrfs_free_qgroup_config(fs_info);
  826. ret = btrfs_clean_quota_tree(trans, quota_root);
  827. if (ret)
  828. goto out;
  829. ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
  830. if (ret)
  831. goto out;
  832. list_del(&quota_root->dirty_list);
  833. btrfs_tree_lock(quota_root->node);
  834. clean_tree_block(trans, tree_root->fs_info, quota_root->node);
  835. btrfs_tree_unlock(quota_root->node);
  836. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  837. free_extent_buffer(quota_root->node);
  838. free_extent_buffer(quota_root->commit_root);
  839. kfree(quota_root);
  840. out:
  841. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  842. return ret;
  843. }
  844. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  845. struct btrfs_qgroup *qgroup)
  846. {
  847. if (list_empty(&qgroup->dirty))
  848. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  849. }
  850. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  851. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  852. {
  853. struct btrfs_root *quota_root;
  854. struct btrfs_qgroup *parent;
  855. struct btrfs_qgroup *member;
  856. struct btrfs_qgroup_list *list;
  857. int ret = 0;
  858. mutex_lock(&fs_info->qgroup_ioctl_lock);
  859. quota_root = fs_info->quota_root;
  860. if (!quota_root) {
  861. ret = -EINVAL;
  862. goto out;
  863. }
  864. member = find_qgroup_rb(fs_info, src);
  865. parent = find_qgroup_rb(fs_info, dst);
  866. if (!member || !parent) {
  867. ret = -EINVAL;
  868. goto out;
  869. }
  870. /* check if such qgroup relation exist firstly */
  871. list_for_each_entry(list, &member->groups, next_group) {
  872. if (list->group == parent) {
  873. ret = -EEXIST;
  874. goto out;
  875. }
  876. }
  877. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  878. if (ret)
  879. goto out;
  880. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  881. if (ret) {
  882. del_qgroup_relation_item(trans, quota_root, src, dst);
  883. goto out;
  884. }
  885. spin_lock(&fs_info->qgroup_lock);
  886. ret = add_relation_rb(quota_root->fs_info, src, dst);
  887. spin_unlock(&fs_info->qgroup_lock);
  888. out:
  889. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  890. return ret;
  891. }
  892. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  893. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  894. {
  895. struct btrfs_root *quota_root;
  896. struct btrfs_qgroup *parent;
  897. struct btrfs_qgroup *member;
  898. struct btrfs_qgroup_list *list;
  899. int ret = 0;
  900. int err;
  901. mutex_lock(&fs_info->qgroup_ioctl_lock);
  902. quota_root = fs_info->quota_root;
  903. if (!quota_root) {
  904. ret = -EINVAL;
  905. goto out;
  906. }
  907. member = find_qgroup_rb(fs_info, src);
  908. parent = find_qgroup_rb(fs_info, dst);
  909. if (!member || !parent) {
  910. ret = -EINVAL;
  911. goto out;
  912. }
  913. /* check if such qgroup relation exist firstly */
  914. list_for_each_entry(list, &member->groups, next_group) {
  915. if (list->group == parent)
  916. goto exist;
  917. }
  918. ret = -ENOENT;
  919. goto out;
  920. exist:
  921. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  922. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  923. if (err && !ret)
  924. ret = err;
  925. spin_lock(&fs_info->qgroup_lock);
  926. del_relation_rb(fs_info, src, dst);
  927. spin_unlock(&fs_info->qgroup_lock);
  928. out:
  929. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  930. return ret;
  931. }
  932. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  933. struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
  934. {
  935. struct btrfs_root *quota_root;
  936. struct btrfs_qgroup *qgroup;
  937. int ret = 0;
  938. mutex_lock(&fs_info->qgroup_ioctl_lock);
  939. quota_root = fs_info->quota_root;
  940. if (!quota_root) {
  941. ret = -EINVAL;
  942. goto out;
  943. }
  944. qgroup = find_qgroup_rb(fs_info, qgroupid);
  945. if (qgroup) {
  946. ret = -EEXIST;
  947. goto out;
  948. }
  949. ret = add_qgroup_item(trans, quota_root, qgroupid);
  950. if (ret)
  951. goto out;
  952. spin_lock(&fs_info->qgroup_lock);
  953. qgroup = add_qgroup_rb(fs_info, qgroupid);
  954. spin_unlock(&fs_info->qgroup_lock);
  955. if (IS_ERR(qgroup))
  956. ret = PTR_ERR(qgroup);
  957. out:
  958. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  959. return ret;
  960. }
  961. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  962. struct btrfs_fs_info *fs_info, u64 qgroupid)
  963. {
  964. struct btrfs_root *quota_root;
  965. struct btrfs_qgroup *qgroup;
  966. int ret = 0;
  967. mutex_lock(&fs_info->qgroup_ioctl_lock);
  968. quota_root = fs_info->quota_root;
  969. if (!quota_root) {
  970. ret = -EINVAL;
  971. goto out;
  972. }
  973. qgroup = find_qgroup_rb(fs_info, qgroupid);
  974. if (!qgroup) {
  975. ret = -ENOENT;
  976. goto out;
  977. } else {
  978. /* check if there are no relations to this qgroup */
  979. if (!list_empty(&qgroup->groups) ||
  980. !list_empty(&qgroup->members)) {
  981. ret = -EBUSY;
  982. goto out;
  983. }
  984. }
  985. ret = del_qgroup_item(trans, quota_root, qgroupid);
  986. spin_lock(&fs_info->qgroup_lock);
  987. del_qgroup_rb(quota_root->fs_info, qgroupid);
  988. spin_unlock(&fs_info->qgroup_lock);
  989. out:
  990. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  991. return ret;
  992. }
  993. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  994. struct btrfs_fs_info *fs_info, u64 qgroupid,
  995. struct btrfs_qgroup_limit *limit)
  996. {
  997. struct btrfs_root *quota_root;
  998. struct btrfs_qgroup *qgroup;
  999. int ret = 0;
  1000. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1001. quota_root = fs_info->quota_root;
  1002. if (!quota_root) {
  1003. ret = -EINVAL;
  1004. goto out;
  1005. }
  1006. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1007. if (!qgroup) {
  1008. ret = -ENOENT;
  1009. goto out;
  1010. }
  1011. spin_lock(&fs_info->qgroup_lock);
  1012. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
  1013. qgroup->max_rfer = limit->max_rfer;
  1014. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
  1015. qgroup->max_excl = limit->max_excl;
  1016. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER)
  1017. qgroup->rsv_rfer = limit->rsv_rfer;
  1018. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL)
  1019. qgroup->rsv_excl = limit->rsv_excl;
  1020. qgroup->lim_flags |= limit->flags;
  1021. spin_unlock(&fs_info->qgroup_lock);
  1022. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1023. if (ret) {
  1024. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1025. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1026. qgroupid);
  1027. }
  1028. out:
  1029. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1030. return ret;
  1031. }
  1032. static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
  1033. struct btrfs_qgroup_operation *oper2)
  1034. {
  1035. /*
  1036. * Ignore seq and type here, we're looking for any operation
  1037. * at all related to this extent on that root.
  1038. */
  1039. if (oper1->bytenr < oper2->bytenr)
  1040. return -1;
  1041. if (oper1->bytenr > oper2->bytenr)
  1042. return 1;
  1043. if (oper1->ref_root < oper2->ref_root)
  1044. return -1;
  1045. if (oper1->ref_root > oper2->ref_root)
  1046. return 1;
  1047. return 0;
  1048. }
  1049. static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
  1050. struct btrfs_qgroup_operation *oper)
  1051. {
  1052. struct rb_node *n;
  1053. struct btrfs_qgroup_operation *cur;
  1054. int cmp;
  1055. spin_lock(&fs_info->qgroup_op_lock);
  1056. n = fs_info->qgroup_op_tree.rb_node;
  1057. while (n) {
  1058. cur = rb_entry(n, struct btrfs_qgroup_operation, n);
  1059. cmp = comp_oper_exist(cur, oper);
  1060. if (cmp < 0) {
  1061. n = n->rb_right;
  1062. } else if (cmp) {
  1063. n = n->rb_left;
  1064. } else {
  1065. spin_unlock(&fs_info->qgroup_op_lock);
  1066. return -EEXIST;
  1067. }
  1068. }
  1069. spin_unlock(&fs_info->qgroup_op_lock);
  1070. return 0;
  1071. }
  1072. static int comp_oper(struct btrfs_qgroup_operation *oper1,
  1073. struct btrfs_qgroup_operation *oper2)
  1074. {
  1075. if (oper1->bytenr < oper2->bytenr)
  1076. return -1;
  1077. if (oper1->bytenr > oper2->bytenr)
  1078. return 1;
  1079. if (oper1->ref_root < oper2->ref_root)
  1080. return -1;
  1081. if (oper1->ref_root > oper2->ref_root)
  1082. return 1;
  1083. if (oper1->seq < oper2->seq)
  1084. return -1;
  1085. if (oper1->seq > oper2->seq)
  1086. return 1;
  1087. if (oper1->type < oper2->type)
  1088. return -1;
  1089. if (oper1->type > oper2->type)
  1090. return 1;
  1091. return 0;
  1092. }
  1093. static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
  1094. struct btrfs_qgroup_operation *oper)
  1095. {
  1096. struct rb_node **p;
  1097. struct rb_node *parent = NULL;
  1098. struct btrfs_qgroup_operation *cur;
  1099. int cmp;
  1100. spin_lock(&fs_info->qgroup_op_lock);
  1101. p = &fs_info->qgroup_op_tree.rb_node;
  1102. while (*p) {
  1103. parent = *p;
  1104. cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
  1105. cmp = comp_oper(cur, oper);
  1106. if (cmp < 0) {
  1107. p = &(*p)->rb_right;
  1108. } else if (cmp) {
  1109. p = &(*p)->rb_left;
  1110. } else {
  1111. spin_unlock(&fs_info->qgroup_op_lock);
  1112. return -EEXIST;
  1113. }
  1114. }
  1115. rb_link_node(&oper->n, parent, p);
  1116. rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
  1117. spin_unlock(&fs_info->qgroup_op_lock);
  1118. return 0;
  1119. }
  1120. /*
  1121. * Record a quota operation for processing later on.
  1122. * @trans: the transaction we are adding the delayed op to.
  1123. * @fs_info: the fs_info for this fs.
  1124. * @ref_root: the root of the reference we are acting on,
  1125. * @bytenr: the bytenr we are acting on.
  1126. * @num_bytes: the number of bytes in the reference.
  1127. * @type: the type of operation this is.
  1128. * @mod_seq: do we need to get a sequence number for looking up roots.
  1129. *
  1130. * We just add it to our trans qgroup_ref_list and carry on and process these
  1131. * operations in order at some later point. If the reference root isn't a fs
  1132. * root then we don't bother with doing anything.
  1133. *
  1134. * MUST BE HOLDING THE REF LOCK.
  1135. */
  1136. int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
  1137. struct btrfs_fs_info *fs_info, u64 ref_root,
  1138. u64 bytenr, u64 num_bytes,
  1139. enum btrfs_qgroup_operation_type type, int mod_seq)
  1140. {
  1141. struct btrfs_qgroup_operation *oper;
  1142. int ret;
  1143. if (!is_fstree(ref_root) || !fs_info->quota_enabled)
  1144. return 0;
  1145. oper = kmalloc(sizeof(*oper), GFP_NOFS);
  1146. if (!oper)
  1147. return -ENOMEM;
  1148. oper->ref_root = ref_root;
  1149. oper->bytenr = bytenr;
  1150. oper->num_bytes = num_bytes;
  1151. oper->type = type;
  1152. oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
  1153. INIT_LIST_HEAD(&oper->elem.list);
  1154. oper->elem.seq = 0;
  1155. trace_btrfs_qgroup_record_ref(oper);
  1156. if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
  1157. /*
  1158. * If any operation for this bytenr/ref_root combo
  1159. * exists, then we know it's not exclusively owned and
  1160. * shouldn't be queued up.
  1161. *
  1162. * This also catches the case where we have a cloned
  1163. * extent that gets queued up multiple times during
  1164. * drop snapshot.
  1165. */
  1166. if (qgroup_oper_exists(fs_info, oper)) {
  1167. kfree(oper);
  1168. return 0;
  1169. }
  1170. }
  1171. ret = insert_qgroup_oper(fs_info, oper);
  1172. if (ret) {
  1173. /* Shouldn't happen so have an assert for developers */
  1174. ASSERT(0);
  1175. kfree(oper);
  1176. return ret;
  1177. }
  1178. list_add_tail(&oper->list, &trans->qgroup_ref_list);
  1179. if (mod_seq)
  1180. btrfs_get_tree_mod_seq(fs_info, &oper->elem);
  1181. return 0;
  1182. }
  1183. /*
  1184. * The easy accounting, if we are adding/removing the only ref for an extent
  1185. * then this qgroup and all of the parent qgroups get their refrence and
  1186. * exclusive counts adjusted.
  1187. */
  1188. static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  1189. struct btrfs_qgroup_operation *oper)
  1190. {
  1191. struct btrfs_qgroup *qgroup;
  1192. struct ulist *tmp;
  1193. struct btrfs_qgroup_list *glist;
  1194. struct ulist_node *unode;
  1195. struct ulist_iterator uiter;
  1196. int sign = 0;
  1197. int ret = 0;
  1198. tmp = ulist_alloc(GFP_NOFS);
  1199. if (!tmp)
  1200. return -ENOMEM;
  1201. spin_lock(&fs_info->qgroup_lock);
  1202. if (!fs_info->quota_root)
  1203. goto out;
  1204. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1205. if (!qgroup)
  1206. goto out;
  1207. switch (oper->type) {
  1208. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1209. sign = 1;
  1210. break;
  1211. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1212. sign = -1;
  1213. break;
  1214. default:
  1215. ASSERT(0);
  1216. }
  1217. qgroup->rfer += sign * oper->num_bytes;
  1218. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1219. WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
  1220. qgroup->excl += sign * oper->num_bytes;
  1221. qgroup->excl_cmpr += sign * oper->num_bytes;
  1222. qgroup_dirty(fs_info, qgroup);
  1223. /* Get all of the parent groups that contain this qgroup */
  1224. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1225. ret = ulist_add(tmp, glist->group->qgroupid,
  1226. ptr_to_u64(glist->group), GFP_ATOMIC);
  1227. if (ret < 0)
  1228. goto out;
  1229. }
  1230. /* Iterate all of the parents and adjust their reference counts */
  1231. ULIST_ITER_INIT(&uiter);
  1232. while ((unode = ulist_next(tmp, &uiter))) {
  1233. qgroup = u64_to_ptr(unode->aux);
  1234. qgroup->rfer += sign * oper->num_bytes;
  1235. qgroup->rfer_cmpr += sign * oper->num_bytes;
  1236. WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
  1237. qgroup->excl += sign * oper->num_bytes;
  1238. qgroup->excl_cmpr += sign * oper->num_bytes;
  1239. qgroup_dirty(fs_info, qgroup);
  1240. /* Add any parents of the parents */
  1241. list_for_each_entry(glist, &qgroup->groups, next_group) {
  1242. ret = ulist_add(tmp, glist->group->qgroupid,
  1243. ptr_to_u64(glist->group), GFP_ATOMIC);
  1244. if (ret < 0)
  1245. goto out;
  1246. }
  1247. }
  1248. ret = 0;
  1249. out:
  1250. spin_unlock(&fs_info->qgroup_lock);
  1251. ulist_free(tmp);
  1252. return ret;
  1253. }
  1254. /*
  1255. * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
  1256. * properly.
  1257. */
  1258. static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
  1259. u64 root_to_skip, struct ulist *tmp,
  1260. struct ulist *roots, struct ulist *qgroups,
  1261. u64 seq, int *old_roots, int rescan)
  1262. {
  1263. struct ulist_node *unode;
  1264. struct ulist_iterator uiter;
  1265. struct ulist_node *tmp_unode;
  1266. struct ulist_iterator tmp_uiter;
  1267. struct btrfs_qgroup *qg;
  1268. int ret;
  1269. ULIST_ITER_INIT(&uiter);
  1270. while ((unode = ulist_next(roots, &uiter))) {
  1271. /* We don't count our current root here */
  1272. if (unode->val == root_to_skip)
  1273. continue;
  1274. qg = find_qgroup_rb(fs_info, unode->val);
  1275. if (!qg)
  1276. continue;
  1277. /*
  1278. * We could have a pending removal of this same ref so we may
  1279. * not have actually found our ref root when doing
  1280. * btrfs_find_all_roots, so we need to keep track of how many
  1281. * old roots we find in case we removed ours and added a
  1282. * different one at the same time. I don't think this could
  1283. * happen in practice but that sort of thinking leads to pain
  1284. * and suffering and to the dark side.
  1285. */
  1286. (*old_roots)++;
  1287. ulist_reinit(tmp);
  1288. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1289. GFP_ATOMIC);
  1290. if (ret < 0)
  1291. return ret;
  1292. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
  1293. if (ret < 0)
  1294. return ret;
  1295. ULIST_ITER_INIT(&tmp_uiter);
  1296. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1297. struct btrfs_qgroup_list *glist;
  1298. qg = u64_to_ptr(tmp_unode->aux);
  1299. /*
  1300. * We use this sequence number to keep from having to
  1301. * run the whole list and 0 out the refcnt every time.
  1302. * We basically use sequnce as the known 0 count and
  1303. * then add 1 everytime we see a qgroup. This is how we
  1304. * get how many of the roots actually point up to the
  1305. * upper level qgroups in order to determine exclusive
  1306. * counts.
  1307. *
  1308. * For rescan we want to set old_refcnt to seq so our
  1309. * exclusive calculations end up correct.
  1310. */
  1311. if (rescan)
  1312. qg->old_refcnt = seq;
  1313. else if (qg->old_refcnt < seq)
  1314. qg->old_refcnt = seq + 1;
  1315. else
  1316. qg->old_refcnt++;
  1317. if (qg->new_refcnt < seq)
  1318. qg->new_refcnt = seq + 1;
  1319. else
  1320. qg->new_refcnt++;
  1321. list_for_each_entry(glist, &qg->groups, next_group) {
  1322. ret = ulist_add(qgroups, glist->group->qgroupid,
  1323. ptr_to_u64(glist->group),
  1324. GFP_ATOMIC);
  1325. if (ret < 0)
  1326. return ret;
  1327. ret = ulist_add(tmp, glist->group->qgroupid,
  1328. ptr_to_u64(glist->group),
  1329. GFP_ATOMIC);
  1330. if (ret < 0)
  1331. return ret;
  1332. }
  1333. }
  1334. }
  1335. return 0;
  1336. }
  1337. /*
  1338. * We need to walk forward in our operation tree and account for any roots that
  1339. * were deleted after we made this operation.
  1340. */
  1341. static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
  1342. struct btrfs_qgroup_operation *oper,
  1343. struct ulist *tmp,
  1344. struct ulist *qgroups, u64 seq,
  1345. int *old_roots)
  1346. {
  1347. struct ulist_node *unode;
  1348. struct ulist_iterator uiter;
  1349. struct btrfs_qgroup *qg;
  1350. struct btrfs_qgroup_operation *tmp_oper;
  1351. struct rb_node *n;
  1352. int ret;
  1353. ulist_reinit(tmp);
  1354. /*
  1355. * We only walk forward in the tree since we're only interested in
  1356. * removals that happened _after_ our operation.
  1357. */
  1358. spin_lock(&fs_info->qgroup_op_lock);
  1359. n = rb_next(&oper->n);
  1360. spin_unlock(&fs_info->qgroup_op_lock);
  1361. if (!n)
  1362. return 0;
  1363. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1364. while (tmp_oper->bytenr == oper->bytenr) {
  1365. /*
  1366. * If it's not a removal we don't care, additions work out
  1367. * properly with our refcnt tracking.
  1368. */
  1369. if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
  1370. tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
  1371. goto next;
  1372. qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
  1373. if (!qg)
  1374. goto next;
  1375. ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
  1376. GFP_ATOMIC);
  1377. if (ret) {
  1378. if (ret < 0)
  1379. return ret;
  1380. /*
  1381. * We only want to increase old_roots if this qgroup is
  1382. * not already in the list of qgroups. If it is already
  1383. * there then that means it must have been re-added or
  1384. * the delete will be discarded because we had an
  1385. * existing ref that we haven't looked up yet. In this
  1386. * case we don't want to increase old_roots. So if ret
  1387. * == 1 then we know that this is the first time we've
  1388. * seen this qgroup and we can bump the old_roots.
  1389. */
  1390. (*old_roots)++;
  1391. ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
  1392. GFP_ATOMIC);
  1393. if (ret < 0)
  1394. return ret;
  1395. }
  1396. next:
  1397. spin_lock(&fs_info->qgroup_op_lock);
  1398. n = rb_next(&tmp_oper->n);
  1399. spin_unlock(&fs_info->qgroup_op_lock);
  1400. if (!n)
  1401. break;
  1402. tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
  1403. }
  1404. /* Ok now process the qgroups we found */
  1405. ULIST_ITER_INIT(&uiter);
  1406. while ((unode = ulist_next(tmp, &uiter))) {
  1407. struct btrfs_qgroup_list *glist;
  1408. qg = u64_to_ptr(unode->aux);
  1409. if (qg->old_refcnt < seq)
  1410. qg->old_refcnt = seq + 1;
  1411. else
  1412. qg->old_refcnt++;
  1413. if (qg->new_refcnt < seq)
  1414. qg->new_refcnt = seq + 1;
  1415. else
  1416. qg->new_refcnt++;
  1417. list_for_each_entry(glist, &qg->groups, next_group) {
  1418. ret = ulist_add(qgroups, glist->group->qgroupid,
  1419. ptr_to_u64(glist->group), GFP_ATOMIC);
  1420. if (ret < 0)
  1421. return ret;
  1422. ret = ulist_add(tmp, glist->group->qgroupid,
  1423. ptr_to_u64(glist->group), GFP_ATOMIC);
  1424. if (ret < 0)
  1425. return ret;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. /* Add refcnt for the newly added reference. */
  1431. static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
  1432. struct btrfs_qgroup_operation *oper,
  1433. struct btrfs_qgroup *qgroup,
  1434. struct ulist *tmp, struct ulist *qgroups,
  1435. u64 seq)
  1436. {
  1437. struct ulist_node *unode;
  1438. struct ulist_iterator uiter;
  1439. struct btrfs_qgroup *qg;
  1440. int ret;
  1441. ulist_reinit(tmp);
  1442. ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
  1443. GFP_ATOMIC);
  1444. if (ret < 0)
  1445. return ret;
  1446. ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
  1447. GFP_ATOMIC);
  1448. if (ret < 0)
  1449. return ret;
  1450. ULIST_ITER_INIT(&uiter);
  1451. while ((unode = ulist_next(tmp, &uiter))) {
  1452. struct btrfs_qgroup_list *glist;
  1453. qg = u64_to_ptr(unode->aux);
  1454. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1455. if (qg->new_refcnt < seq)
  1456. qg->new_refcnt = seq + 1;
  1457. else
  1458. qg->new_refcnt++;
  1459. } else {
  1460. if (qg->old_refcnt < seq)
  1461. qg->old_refcnt = seq + 1;
  1462. else
  1463. qg->old_refcnt++;
  1464. }
  1465. list_for_each_entry(glist, &qg->groups, next_group) {
  1466. ret = ulist_add(tmp, glist->group->qgroupid,
  1467. ptr_to_u64(glist->group), GFP_ATOMIC);
  1468. if (ret < 0)
  1469. return ret;
  1470. ret = ulist_add(qgroups, glist->group->qgroupid,
  1471. ptr_to_u64(glist->group), GFP_ATOMIC);
  1472. if (ret < 0)
  1473. return ret;
  1474. }
  1475. }
  1476. return 0;
  1477. }
  1478. /*
  1479. * This adjusts the counters for all referenced qgroups if need be.
  1480. */
  1481. static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
  1482. u64 root_to_skip, u64 num_bytes,
  1483. struct ulist *qgroups, u64 seq,
  1484. int old_roots, int new_roots, int rescan)
  1485. {
  1486. struct ulist_node *unode;
  1487. struct ulist_iterator uiter;
  1488. struct btrfs_qgroup *qg;
  1489. u64 cur_new_count, cur_old_count;
  1490. ULIST_ITER_INIT(&uiter);
  1491. while ((unode = ulist_next(qgroups, &uiter))) {
  1492. bool dirty = false;
  1493. qg = u64_to_ptr(unode->aux);
  1494. /*
  1495. * Wasn't referenced before but is now, add to the reference
  1496. * counters.
  1497. */
  1498. if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
  1499. qg->rfer += num_bytes;
  1500. qg->rfer_cmpr += num_bytes;
  1501. dirty = true;
  1502. }
  1503. /*
  1504. * Was referenced before but isn't now, subtract from the
  1505. * reference counters.
  1506. */
  1507. if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
  1508. qg->rfer -= num_bytes;
  1509. qg->rfer_cmpr -= num_bytes;
  1510. dirty = true;
  1511. }
  1512. if (qg->old_refcnt < seq)
  1513. cur_old_count = 0;
  1514. else
  1515. cur_old_count = qg->old_refcnt - seq;
  1516. if (qg->new_refcnt < seq)
  1517. cur_new_count = 0;
  1518. else
  1519. cur_new_count = qg->new_refcnt - seq;
  1520. /*
  1521. * If our refcount was the same as the roots previously but our
  1522. * new count isn't the same as the number of roots now then we
  1523. * went from having a exclusive reference on this range to not.
  1524. */
  1525. if (old_roots && cur_old_count == old_roots &&
  1526. (cur_new_count != new_roots || new_roots == 0)) {
  1527. WARN_ON(cur_new_count != new_roots && new_roots == 0);
  1528. qg->excl -= num_bytes;
  1529. qg->excl_cmpr -= num_bytes;
  1530. dirty = true;
  1531. }
  1532. /*
  1533. * If we didn't reference all the roots before but now we do we
  1534. * have an exclusive reference to this range.
  1535. */
  1536. if ((!old_roots || (old_roots && cur_old_count != old_roots))
  1537. && cur_new_count == new_roots) {
  1538. qg->excl += num_bytes;
  1539. qg->excl_cmpr += num_bytes;
  1540. dirty = true;
  1541. }
  1542. if (dirty)
  1543. qgroup_dirty(fs_info, qg);
  1544. }
  1545. return 0;
  1546. }
  1547. /*
  1548. * If we removed a data extent and there were other references for that bytenr
  1549. * then we need to lookup all referenced roots to make sure we still don't
  1550. * reference this bytenr. If we do then we can just discard this operation.
  1551. */
  1552. static int check_existing_refs(struct btrfs_trans_handle *trans,
  1553. struct btrfs_fs_info *fs_info,
  1554. struct btrfs_qgroup_operation *oper)
  1555. {
  1556. struct ulist *roots = NULL;
  1557. struct ulist_node *unode;
  1558. struct ulist_iterator uiter;
  1559. int ret = 0;
  1560. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1561. oper->elem.seq, &roots);
  1562. if (ret < 0)
  1563. return ret;
  1564. ret = 0;
  1565. ULIST_ITER_INIT(&uiter);
  1566. while ((unode = ulist_next(roots, &uiter))) {
  1567. if (unode->val == oper->ref_root) {
  1568. ret = 1;
  1569. break;
  1570. }
  1571. }
  1572. ulist_free(roots);
  1573. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1574. return ret;
  1575. }
  1576. /*
  1577. * If we share a reference across multiple roots then we may need to adjust
  1578. * various qgroups referenced and exclusive counters. The basic premise is this
  1579. *
  1580. * 1) We have seq to represent a 0 count. Instead of looping through all of the
  1581. * qgroups and resetting their refcount to 0 we just constantly bump this
  1582. * sequence number to act as the base reference count. This means that if
  1583. * anybody is equal to or below this sequence they were never referenced. We
  1584. * jack this sequence up by the number of roots we found each time in order to
  1585. * make sure we don't have any overlap.
  1586. *
  1587. * 2) We first search all the roots that reference the area _except_ the root
  1588. * we're acting on currently. This makes up the old_refcnt of all the qgroups
  1589. * before.
  1590. *
  1591. * 3) We walk all of the qgroups referenced by the root we are currently acting
  1592. * on, and will either adjust old_refcnt in the case of a removal or the
  1593. * new_refcnt in the case of an addition.
  1594. *
  1595. * 4) Finally we walk all the qgroups that are referenced by this range
  1596. * including the root we are acting on currently. We will adjust the counters
  1597. * based on the number of roots we had and will have after this operation.
  1598. *
  1599. * Take this example as an illustration
  1600. *
  1601. * [qgroup 1/0]
  1602. * / | \
  1603. * [qg 0/0] [qg 0/1] [qg 0/2]
  1604. * \ | /
  1605. * [ extent ]
  1606. *
  1607. * Say we are adding a reference that is covered by qg 0/0. The first step
  1608. * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
  1609. * old_roots being 2. Because it is adding new_roots will be 1. We then go
  1610. * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
  1611. * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
  1612. * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
  1613. * reference and thus must add the size to the referenced bytes. Everything
  1614. * else is the same so nothing else changes.
  1615. */
  1616. static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
  1617. struct btrfs_fs_info *fs_info,
  1618. struct btrfs_qgroup_operation *oper)
  1619. {
  1620. struct ulist *roots = NULL;
  1621. struct ulist *qgroups, *tmp;
  1622. struct btrfs_qgroup *qgroup;
  1623. struct seq_list elem = SEQ_LIST_INIT(elem);
  1624. u64 seq;
  1625. int old_roots = 0;
  1626. int new_roots = 0;
  1627. int ret = 0;
  1628. if (oper->elem.seq) {
  1629. ret = check_existing_refs(trans, fs_info, oper);
  1630. if (ret < 0)
  1631. return ret;
  1632. if (ret)
  1633. return 0;
  1634. }
  1635. qgroups = ulist_alloc(GFP_NOFS);
  1636. if (!qgroups)
  1637. return -ENOMEM;
  1638. tmp = ulist_alloc(GFP_NOFS);
  1639. if (!tmp) {
  1640. ulist_free(qgroups);
  1641. return -ENOMEM;
  1642. }
  1643. btrfs_get_tree_mod_seq(fs_info, &elem);
  1644. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
  1645. &roots);
  1646. btrfs_put_tree_mod_seq(fs_info, &elem);
  1647. if (ret < 0) {
  1648. ulist_free(qgroups);
  1649. ulist_free(tmp);
  1650. return ret;
  1651. }
  1652. spin_lock(&fs_info->qgroup_lock);
  1653. qgroup = find_qgroup_rb(fs_info, oper->ref_root);
  1654. if (!qgroup)
  1655. goto out;
  1656. seq = fs_info->qgroup_seq;
  1657. /*
  1658. * So roots is the list of all the roots currently pointing at the
  1659. * bytenr, including the ref we are adding if we are adding, or not if
  1660. * we are removing a ref. So we pass in the ref_root to skip that root
  1661. * in our calculations. We set old_refnct and new_refcnt cause who the
  1662. * hell knows what everything looked like before, and it doesn't matter
  1663. * except...
  1664. */
  1665. ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
  1666. seq, &old_roots, 0);
  1667. if (ret < 0)
  1668. goto out;
  1669. /*
  1670. * Now adjust the refcounts of the qgroups that care about this
  1671. * reference, either the old_count in the case of removal or new_count
  1672. * in the case of an addition.
  1673. */
  1674. ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
  1675. seq);
  1676. if (ret < 0)
  1677. goto out;
  1678. /*
  1679. * ...in the case of removals. If we had a removal before we got around
  1680. * to processing this operation then we need to find that guy and count
  1681. * his references as if they really existed so we don't end up screwing
  1682. * up the exclusive counts. Then whenever we go to process the delete
  1683. * everything will be grand and we can account for whatever exclusive
  1684. * changes need to be made there. We also have to pass in old_roots so
  1685. * we have an accurate count of the roots as it pertains to this
  1686. * operations view of the world.
  1687. */
  1688. ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
  1689. &old_roots);
  1690. if (ret < 0)
  1691. goto out;
  1692. /*
  1693. * We are adding our root, need to adjust up the number of roots,
  1694. * otherwise old_roots is the number of roots we want.
  1695. */
  1696. if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
  1697. new_roots = old_roots + 1;
  1698. } else {
  1699. new_roots = old_roots;
  1700. old_roots++;
  1701. }
  1702. fs_info->qgroup_seq += old_roots + 1;
  1703. /*
  1704. * And now the magic happens, bless Arne for having a pretty elegant
  1705. * solution for this.
  1706. */
  1707. qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
  1708. qgroups, seq, old_roots, new_roots, 0);
  1709. out:
  1710. spin_unlock(&fs_info->qgroup_lock);
  1711. ulist_free(qgroups);
  1712. ulist_free(roots);
  1713. ulist_free(tmp);
  1714. return ret;
  1715. }
  1716. /*
  1717. * Process a reference to a shared subtree. This type of operation is
  1718. * queued during snapshot removal when we encounter extents which are
  1719. * shared between more than one root.
  1720. */
  1721. static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
  1722. struct btrfs_fs_info *fs_info,
  1723. struct btrfs_qgroup_operation *oper)
  1724. {
  1725. struct ulist *roots = NULL;
  1726. struct ulist_node *unode;
  1727. struct ulist_iterator uiter;
  1728. struct btrfs_qgroup_list *glist;
  1729. struct ulist *parents;
  1730. int ret = 0;
  1731. int err;
  1732. struct btrfs_qgroup *qg;
  1733. u64 root_obj = 0;
  1734. struct seq_list elem = SEQ_LIST_INIT(elem);
  1735. parents = ulist_alloc(GFP_NOFS);
  1736. if (!parents)
  1737. return -ENOMEM;
  1738. btrfs_get_tree_mod_seq(fs_info, &elem);
  1739. ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
  1740. elem.seq, &roots);
  1741. btrfs_put_tree_mod_seq(fs_info, &elem);
  1742. if (ret < 0)
  1743. goto out;
  1744. if (roots->nnodes != 1)
  1745. goto out;
  1746. ULIST_ITER_INIT(&uiter);
  1747. unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
  1748. /*
  1749. * If we find our ref root then that means all refs
  1750. * this extent has to the root have not yet been
  1751. * deleted. In that case, we do nothing and let the
  1752. * last ref for this bytenr drive our update.
  1753. *
  1754. * This can happen for example if an extent is
  1755. * referenced multiple times in a snapshot (clone,
  1756. * etc). If we are in the middle of snapshot removal,
  1757. * queued updates for such an extent will find the
  1758. * root if we have not yet finished removing the
  1759. * snapshot.
  1760. */
  1761. if (unode->val == oper->ref_root)
  1762. goto out;
  1763. root_obj = unode->val;
  1764. BUG_ON(!root_obj);
  1765. spin_lock(&fs_info->qgroup_lock);
  1766. qg = find_qgroup_rb(fs_info, root_obj);
  1767. if (!qg)
  1768. goto out_unlock;
  1769. qg->excl += oper->num_bytes;
  1770. qg->excl_cmpr += oper->num_bytes;
  1771. qgroup_dirty(fs_info, qg);
  1772. /*
  1773. * Adjust counts for parent groups. First we find all
  1774. * parents, then in the 2nd loop we do the adjustment
  1775. * while adding parents of the parents to our ulist.
  1776. */
  1777. list_for_each_entry(glist, &qg->groups, next_group) {
  1778. err = ulist_add(parents, glist->group->qgroupid,
  1779. ptr_to_u64(glist->group), GFP_ATOMIC);
  1780. if (err < 0) {
  1781. ret = err;
  1782. goto out_unlock;
  1783. }
  1784. }
  1785. ULIST_ITER_INIT(&uiter);
  1786. while ((unode = ulist_next(parents, &uiter))) {
  1787. qg = u64_to_ptr(unode->aux);
  1788. qg->excl += oper->num_bytes;
  1789. qg->excl_cmpr += oper->num_bytes;
  1790. qgroup_dirty(fs_info, qg);
  1791. /* Add any parents of the parents */
  1792. list_for_each_entry(glist, &qg->groups, next_group) {
  1793. err = ulist_add(parents, glist->group->qgroupid,
  1794. ptr_to_u64(glist->group), GFP_ATOMIC);
  1795. if (err < 0) {
  1796. ret = err;
  1797. goto out_unlock;
  1798. }
  1799. }
  1800. }
  1801. out_unlock:
  1802. spin_unlock(&fs_info->qgroup_lock);
  1803. out:
  1804. ulist_free(roots);
  1805. ulist_free(parents);
  1806. return ret;
  1807. }
  1808. /*
  1809. * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
  1810. * from the fs. First, all roots referencing the extent are searched, and
  1811. * then the space is accounted accordingly to the different roots. The
  1812. * accounting algorithm works in 3 steps documented inline.
  1813. */
  1814. static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
  1815. struct btrfs_fs_info *fs_info,
  1816. struct btrfs_qgroup_operation *oper)
  1817. {
  1818. int ret = 0;
  1819. if (!fs_info->quota_enabled)
  1820. return 0;
  1821. BUG_ON(!fs_info->quota_root);
  1822. mutex_lock(&fs_info->qgroup_rescan_lock);
  1823. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1824. if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
  1825. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1826. return 0;
  1827. }
  1828. }
  1829. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1830. ASSERT(is_fstree(oper->ref_root));
  1831. trace_btrfs_qgroup_account(oper);
  1832. switch (oper->type) {
  1833. case BTRFS_QGROUP_OPER_ADD_EXCL:
  1834. case BTRFS_QGROUP_OPER_SUB_EXCL:
  1835. ret = qgroup_excl_accounting(fs_info, oper);
  1836. break;
  1837. case BTRFS_QGROUP_OPER_ADD_SHARED:
  1838. case BTRFS_QGROUP_OPER_SUB_SHARED:
  1839. ret = qgroup_shared_accounting(trans, fs_info, oper);
  1840. break;
  1841. case BTRFS_QGROUP_OPER_SUB_SUBTREE:
  1842. ret = qgroup_subtree_accounting(trans, fs_info, oper);
  1843. break;
  1844. default:
  1845. ASSERT(0);
  1846. }
  1847. return ret;
  1848. }
  1849. /*
  1850. * Needs to be called everytime we run delayed refs, even if there is an error
  1851. * in order to cleanup outstanding operations.
  1852. */
  1853. int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
  1854. struct btrfs_fs_info *fs_info)
  1855. {
  1856. struct btrfs_qgroup_operation *oper;
  1857. int ret = 0;
  1858. while (!list_empty(&trans->qgroup_ref_list)) {
  1859. oper = list_first_entry(&trans->qgroup_ref_list,
  1860. struct btrfs_qgroup_operation, list);
  1861. list_del_init(&oper->list);
  1862. if (!ret || !trans->aborted)
  1863. ret = btrfs_qgroup_account(trans, fs_info, oper);
  1864. spin_lock(&fs_info->qgroup_op_lock);
  1865. rb_erase(&oper->n, &fs_info->qgroup_op_tree);
  1866. spin_unlock(&fs_info->qgroup_op_lock);
  1867. btrfs_put_tree_mod_seq(fs_info, &oper->elem);
  1868. kfree(oper);
  1869. }
  1870. return ret;
  1871. }
  1872. /*
  1873. * called from commit_transaction. Writes all changed qgroups to disk.
  1874. */
  1875. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1876. struct btrfs_fs_info *fs_info)
  1877. {
  1878. struct btrfs_root *quota_root = fs_info->quota_root;
  1879. int ret = 0;
  1880. int start_rescan_worker = 0;
  1881. if (!quota_root)
  1882. goto out;
  1883. if (!fs_info->quota_enabled && fs_info->pending_quota_state)
  1884. start_rescan_worker = 1;
  1885. fs_info->quota_enabled = fs_info->pending_quota_state;
  1886. spin_lock(&fs_info->qgroup_lock);
  1887. while (!list_empty(&fs_info->dirty_qgroups)) {
  1888. struct btrfs_qgroup *qgroup;
  1889. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1890. struct btrfs_qgroup, dirty);
  1891. list_del_init(&qgroup->dirty);
  1892. spin_unlock(&fs_info->qgroup_lock);
  1893. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1894. if (ret)
  1895. fs_info->qgroup_flags |=
  1896. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1897. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1898. if (ret)
  1899. fs_info->qgroup_flags |=
  1900. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1901. spin_lock(&fs_info->qgroup_lock);
  1902. }
  1903. if (fs_info->quota_enabled)
  1904. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1905. else
  1906. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1907. spin_unlock(&fs_info->qgroup_lock);
  1908. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1909. if (ret)
  1910. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1911. if (!ret && start_rescan_worker) {
  1912. ret = qgroup_rescan_init(fs_info, 0, 1);
  1913. if (!ret) {
  1914. qgroup_rescan_zero_tracking(fs_info);
  1915. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1916. &fs_info->qgroup_rescan_work);
  1917. }
  1918. ret = 0;
  1919. }
  1920. out:
  1921. return ret;
  1922. }
  1923. /*
  1924. * copy the acounting information between qgroups. This is necessary when a
  1925. * snapshot or a subvolume is created
  1926. */
  1927. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1928. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1929. struct btrfs_qgroup_inherit *inherit)
  1930. {
  1931. int ret = 0;
  1932. int i;
  1933. u64 *i_qgroups;
  1934. struct btrfs_root *quota_root = fs_info->quota_root;
  1935. struct btrfs_qgroup *srcgroup;
  1936. struct btrfs_qgroup *dstgroup;
  1937. u32 level_size = 0;
  1938. u64 nums;
  1939. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1940. if (!fs_info->quota_enabled)
  1941. goto out;
  1942. if (!quota_root) {
  1943. ret = -EINVAL;
  1944. goto out;
  1945. }
  1946. if (inherit) {
  1947. i_qgroups = (u64 *)(inherit + 1);
  1948. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1949. 2 * inherit->num_excl_copies;
  1950. for (i = 0; i < nums; ++i) {
  1951. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1952. if (!srcgroup) {
  1953. ret = -EINVAL;
  1954. goto out;
  1955. }
  1956. ++i_qgroups;
  1957. }
  1958. }
  1959. /*
  1960. * create a tracking group for the subvol itself
  1961. */
  1962. ret = add_qgroup_item(trans, quota_root, objectid);
  1963. if (ret)
  1964. goto out;
  1965. if (srcid) {
  1966. struct btrfs_root *srcroot;
  1967. struct btrfs_key srckey;
  1968. srckey.objectid = srcid;
  1969. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1970. srckey.offset = (u64)-1;
  1971. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1972. if (IS_ERR(srcroot)) {
  1973. ret = PTR_ERR(srcroot);
  1974. goto out;
  1975. }
  1976. rcu_read_lock();
  1977. level_size = srcroot->nodesize;
  1978. rcu_read_unlock();
  1979. }
  1980. /*
  1981. * add qgroup to all inherited groups
  1982. */
  1983. if (inherit) {
  1984. i_qgroups = (u64 *)(inherit + 1);
  1985. for (i = 0; i < inherit->num_qgroups; ++i) {
  1986. ret = add_qgroup_relation_item(trans, quota_root,
  1987. objectid, *i_qgroups);
  1988. if (ret)
  1989. goto out;
  1990. ret = add_qgroup_relation_item(trans, quota_root,
  1991. *i_qgroups, objectid);
  1992. if (ret)
  1993. goto out;
  1994. ++i_qgroups;
  1995. }
  1996. }
  1997. spin_lock(&fs_info->qgroup_lock);
  1998. dstgroup = add_qgroup_rb(fs_info, objectid);
  1999. if (IS_ERR(dstgroup)) {
  2000. ret = PTR_ERR(dstgroup);
  2001. goto unlock;
  2002. }
  2003. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  2004. dstgroup->lim_flags = inherit->lim.flags;
  2005. dstgroup->max_rfer = inherit->lim.max_rfer;
  2006. dstgroup->max_excl = inherit->lim.max_excl;
  2007. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  2008. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  2009. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  2010. if (ret) {
  2011. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2012. btrfs_info(fs_info, "unable to update quota limit for %llu",
  2013. dstgroup->qgroupid);
  2014. goto unlock;
  2015. }
  2016. }
  2017. if (srcid) {
  2018. srcgroup = find_qgroup_rb(fs_info, srcid);
  2019. if (!srcgroup)
  2020. goto unlock;
  2021. /*
  2022. * We call inherit after we clone the root in order to make sure
  2023. * our counts don't go crazy, so at this point the only
  2024. * difference between the two roots should be the root node.
  2025. */
  2026. dstgroup->rfer = srcgroup->rfer;
  2027. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  2028. dstgroup->excl = level_size;
  2029. dstgroup->excl_cmpr = level_size;
  2030. srcgroup->excl = level_size;
  2031. srcgroup->excl_cmpr = level_size;
  2032. /* inherit the limit info */
  2033. dstgroup->lim_flags = srcgroup->lim_flags;
  2034. dstgroup->max_rfer = srcgroup->max_rfer;
  2035. dstgroup->max_excl = srcgroup->max_excl;
  2036. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  2037. dstgroup->rsv_excl = srcgroup->rsv_excl;
  2038. qgroup_dirty(fs_info, dstgroup);
  2039. qgroup_dirty(fs_info, srcgroup);
  2040. }
  2041. if (!inherit)
  2042. goto unlock;
  2043. i_qgroups = (u64 *)(inherit + 1);
  2044. for (i = 0; i < inherit->num_qgroups; ++i) {
  2045. ret = add_relation_rb(quota_root->fs_info, objectid,
  2046. *i_qgroups);
  2047. if (ret)
  2048. goto unlock;
  2049. ++i_qgroups;
  2050. }
  2051. for (i = 0; i < inherit->num_ref_copies; ++i) {
  2052. struct btrfs_qgroup *src;
  2053. struct btrfs_qgroup *dst;
  2054. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2055. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2056. if (!src || !dst) {
  2057. ret = -EINVAL;
  2058. goto unlock;
  2059. }
  2060. dst->rfer = src->rfer - level_size;
  2061. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  2062. i_qgroups += 2;
  2063. }
  2064. for (i = 0; i < inherit->num_excl_copies; ++i) {
  2065. struct btrfs_qgroup *src;
  2066. struct btrfs_qgroup *dst;
  2067. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  2068. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  2069. if (!src || !dst) {
  2070. ret = -EINVAL;
  2071. goto unlock;
  2072. }
  2073. dst->excl = src->excl + level_size;
  2074. dst->excl_cmpr = src->excl_cmpr + level_size;
  2075. i_qgroups += 2;
  2076. }
  2077. unlock:
  2078. spin_unlock(&fs_info->qgroup_lock);
  2079. out:
  2080. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2081. return ret;
  2082. }
  2083. /*
  2084. * reserve some space for a qgroup and all its parents. The reservation takes
  2085. * place with start_transaction or dealloc_reserve, similar to ENOSPC
  2086. * accounting. If not enough space is available, EDQUOT is returned.
  2087. * We assume that the requested space is new for all qgroups.
  2088. */
  2089. int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
  2090. {
  2091. struct btrfs_root *quota_root;
  2092. struct btrfs_qgroup *qgroup;
  2093. struct btrfs_fs_info *fs_info = root->fs_info;
  2094. u64 ref_root = root->root_key.objectid;
  2095. int ret = 0;
  2096. struct ulist_node *unode;
  2097. struct ulist_iterator uiter;
  2098. if (!is_fstree(ref_root))
  2099. return 0;
  2100. if (num_bytes == 0)
  2101. return 0;
  2102. spin_lock(&fs_info->qgroup_lock);
  2103. quota_root = fs_info->quota_root;
  2104. if (!quota_root)
  2105. goto out;
  2106. qgroup = find_qgroup_rb(fs_info, ref_root);
  2107. if (!qgroup)
  2108. goto out;
  2109. /*
  2110. * in a first step, we check all affected qgroups if any limits would
  2111. * be exceeded
  2112. */
  2113. ulist_reinit(fs_info->qgroup_ulist);
  2114. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2115. (uintptr_t)qgroup, GFP_ATOMIC);
  2116. if (ret < 0)
  2117. goto out;
  2118. ULIST_ITER_INIT(&uiter);
  2119. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2120. struct btrfs_qgroup *qg;
  2121. struct btrfs_qgroup_list *glist;
  2122. qg = u64_to_ptr(unode->aux);
  2123. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2124. qg->reserved + (s64)qg->rfer + num_bytes >
  2125. qg->max_rfer) {
  2126. ret = -EDQUOT;
  2127. goto out;
  2128. }
  2129. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2130. qg->reserved + (s64)qg->excl + num_bytes >
  2131. qg->max_excl) {
  2132. ret = -EDQUOT;
  2133. goto out;
  2134. }
  2135. list_for_each_entry(glist, &qg->groups, next_group) {
  2136. ret = ulist_add(fs_info->qgroup_ulist,
  2137. glist->group->qgroupid,
  2138. (uintptr_t)glist->group, GFP_ATOMIC);
  2139. if (ret < 0)
  2140. goto out;
  2141. }
  2142. }
  2143. ret = 0;
  2144. /*
  2145. * no limits exceeded, now record the reservation into all qgroups
  2146. */
  2147. ULIST_ITER_INIT(&uiter);
  2148. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2149. struct btrfs_qgroup *qg;
  2150. qg = u64_to_ptr(unode->aux);
  2151. qg->reserved += num_bytes;
  2152. }
  2153. out:
  2154. spin_unlock(&fs_info->qgroup_lock);
  2155. return ret;
  2156. }
  2157. void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
  2158. {
  2159. struct btrfs_root *quota_root;
  2160. struct btrfs_qgroup *qgroup;
  2161. struct btrfs_fs_info *fs_info = root->fs_info;
  2162. struct ulist_node *unode;
  2163. struct ulist_iterator uiter;
  2164. u64 ref_root = root->root_key.objectid;
  2165. int ret = 0;
  2166. if (!is_fstree(ref_root))
  2167. return;
  2168. if (num_bytes == 0)
  2169. return;
  2170. spin_lock(&fs_info->qgroup_lock);
  2171. quota_root = fs_info->quota_root;
  2172. if (!quota_root)
  2173. goto out;
  2174. qgroup = find_qgroup_rb(fs_info, ref_root);
  2175. if (!qgroup)
  2176. goto out;
  2177. ulist_reinit(fs_info->qgroup_ulist);
  2178. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2179. (uintptr_t)qgroup, GFP_ATOMIC);
  2180. if (ret < 0)
  2181. goto out;
  2182. ULIST_ITER_INIT(&uiter);
  2183. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2184. struct btrfs_qgroup *qg;
  2185. struct btrfs_qgroup_list *glist;
  2186. qg = u64_to_ptr(unode->aux);
  2187. qg->reserved -= num_bytes;
  2188. list_for_each_entry(glist, &qg->groups, next_group) {
  2189. ret = ulist_add(fs_info->qgroup_ulist,
  2190. glist->group->qgroupid,
  2191. (uintptr_t)glist->group, GFP_ATOMIC);
  2192. if (ret < 0)
  2193. goto out;
  2194. }
  2195. }
  2196. out:
  2197. spin_unlock(&fs_info->qgroup_lock);
  2198. }
  2199. void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
  2200. {
  2201. if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
  2202. return;
  2203. btrfs_err(trans->root->fs_info,
  2204. "qgroups not uptodate in trans handle %p: list is%s empty, "
  2205. "seq is %#x.%x",
  2206. trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
  2207. (u32)(trans->delayed_ref_elem.seq >> 32),
  2208. (u32)trans->delayed_ref_elem.seq);
  2209. BUG();
  2210. }
  2211. /*
  2212. * returns < 0 on error, 0 when more leafs are to be scanned.
  2213. * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
  2214. */
  2215. static int
  2216. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2217. struct btrfs_trans_handle *trans, struct ulist *qgroups,
  2218. struct ulist *tmp, struct extent_buffer *scratch_leaf)
  2219. {
  2220. struct btrfs_key found;
  2221. struct ulist *roots = NULL;
  2222. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  2223. u64 num_bytes;
  2224. u64 seq;
  2225. int new_roots;
  2226. int slot;
  2227. int ret;
  2228. path->leave_spinning = 1;
  2229. mutex_lock(&fs_info->qgroup_rescan_lock);
  2230. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2231. &fs_info->qgroup_rescan_progress,
  2232. path, 1, 0);
  2233. pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
  2234. fs_info->qgroup_rescan_progress.objectid,
  2235. fs_info->qgroup_rescan_progress.type,
  2236. fs_info->qgroup_rescan_progress.offset, ret);
  2237. if (ret) {
  2238. /*
  2239. * The rescan is about to end, we will not be scanning any
  2240. * further blocks. We cannot unset the RESCAN flag here, because
  2241. * we want to commit the transaction if everything went well.
  2242. * To make the live accounting work in this phase, we set our
  2243. * scan progress pointer such that every real extent objectid
  2244. * will be smaller.
  2245. */
  2246. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2247. btrfs_release_path(path);
  2248. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2249. return ret;
  2250. }
  2251. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2252. btrfs_header_nritems(path->nodes[0]) - 1);
  2253. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2254. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2255. memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
  2256. slot = path->slots[0];
  2257. btrfs_release_path(path);
  2258. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2259. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2260. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2261. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2262. found.type != BTRFS_METADATA_ITEM_KEY)
  2263. continue;
  2264. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2265. num_bytes = fs_info->extent_root->nodesize;
  2266. else
  2267. num_bytes = found.offset;
  2268. ulist_reinit(qgroups);
  2269. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2270. &roots);
  2271. if (ret < 0)
  2272. goto out;
  2273. spin_lock(&fs_info->qgroup_lock);
  2274. seq = fs_info->qgroup_seq;
  2275. fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
  2276. new_roots = 0;
  2277. ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
  2278. seq, &new_roots, 1);
  2279. if (ret < 0) {
  2280. spin_unlock(&fs_info->qgroup_lock);
  2281. ulist_free(roots);
  2282. goto out;
  2283. }
  2284. ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
  2285. seq, 0, new_roots, 1);
  2286. if (ret < 0) {
  2287. spin_unlock(&fs_info->qgroup_lock);
  2288. ulist_free(roots);
  2289. goto out;
  2290. }
  2291. spin_unlock(&fs_info->qgroup_lock);
  2292. ulist_free(roots);
  2293. }
  2294. out:
  2295. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2296. return ret;
  2297. }
  2298. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2299. {
  2300. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2301. qgroup_rescan_work);
  2302. struct btrfs_path *path;
  2303. struct btrfs_trans_handle *trans = NULL;
  2304. struct ulist *tmp = NULL, *qgroups = NULL;
  2305. struct extent_buffer *scratch_leaf = NULL;
  2306. int err = -ENOMEM;
  2307. path = btrfs_alloc_path();
  2308. if (!path)
  2309. goto out;
  2310. qgroups = ulist_alloc(GFP_NOFS);
  2311. if (!qgroups)
  2312. goto out;
  2313. tmp = ulist_alloc(GFP_NOFS);
  2314. if (!tmp)
  2315. goto out;
  2316. scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
  2317. if (!scratch_leaf)
  2318. goto out;
  2319. err = 0;
  2320. while (!err) {
  2321. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2322. if (IS_ERR(trans)) {
  2323. err = PTR_ERR(trans);
  2324. break;
  2325. }
  2326. if (!fs_info->quota_enabled) {
  2327. err = -EINTR;
  2328. } else {
  2329. err = qgroup_rescan_leaf(fs_info, path, trans,
  2330. qgroups, tmp, scratch_leaf);
  2331. }
  2332. if (err > 0)
  2333. btrfs_commit_transaction(trans, fs_info->fs_root);
  2334. else
  2335. btrfs_end_transaction(trans, fs_info->fs_root);
  2336. }
  2337. out:
  2338. kfree(scratch_leaf);
  2339. ulist_free(qgroups);
  2340. ulist_free(tmp);
  2341. btrfs_free_path(path);
  2342. mutex_lock(&fs_info->qgroup_rescan_lock);
  2343. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2344. if (err == 2 &&
  2345. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2346. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2347. } else if (err < 0) {
  2348. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2349. }
  2350. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2351. if (err >= 0) {
  2352. btrfs_info(fs_info, "qgroup scan completed%s",
  2353. err == 2 ? " (inconsistency flag cleared)" : "");
  2354. } else {
  2355. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2356. }
  2357. complete_all(&fs_info->qgroup_rescan_completion);
  2358. }
  2359. /*
  2360. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2361. * memory required for the rescan context.
  2362. */
  2363. static int
  2364. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2365. int init_flags)
  2366. {
  2367. int ret = 0;
  2368. if (!init_flags &&
  2369. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2370. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2371. ret = -EINVAL;
  2372. goto err;
  2373. }
  2374. mutex_lock(&fs_info->qgroup_rescan_lock);
  2375. spin_lock(&fs_info->qgroup_lock);
  2376. if (init_flags) {
  2377. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2378. ret = -EINPROGRESS;
  2379. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2380. ret = -EINVAL;
  2381. if (ret) {
  2382. spin_unlock(&fs_info->qgroup_lock);
  2383. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2384. goto err;
  2385. }
  2386. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2387. }
  2388. memset(&fs_info->qgroup_rescan_progress, 0,
  2389. sizeof(fs_info->qgroup_rescan_progress));
  2390. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2391. spin_unlock(&fs_info->qgroup_lock);
  2392. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2393. init_completion(&fs_info->qgroup_rescan_completion);
  2394. memset(&fs_info->qgroup_rescan_work, 0,
  2395. sizeof(fs_info->qgroup_rescan_work));
  2396. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2397. btrfs_qgroup_rescan_helper,
  2398. btrfs_qgroup_rescan_worker, NULL, NULL);
  2399. if (ret) {
  2400. err:
  2401. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2402. return ret;
  2403. }
  2404. return 0;
  2405. }
  2406. static void
  2407. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2408. {
  2409. struct rb_node *n;
  2410. struct btrfs_qgroup *qgroup;
  2411. spin_lock(&fs_info->qgroup_lock);
  2412. /* clear all current qgroup tracking information */
  2413. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2414. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2415. qgroup->rfer = 0;
  2416. qgroup->rfer_cmpr = 0;
  2417. qgroup->excl = 0;
  2418. qgroup->excl_cmpr = 0;
  2419. }
  2420. spin_unlock(&fs_info->qgroup_lock);
  2421. }
  2422. int
  2423. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2424. {
  2425. int ret = 0;
  2426. struct btrfs_trans_handle *trans;
  2427. ret = qgroup_rescan_init(fs_info, 0, 1);
  2428. if (ret)
  2429. return ret;
  2430. /*
  2431. * We have set the rescan_progress to 0, which means no more
  2432. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2433. * However, btrfs_qgroup_account_ref may be right after its call
  2434. * to btrfs_find_all_roots, in which case it would still do the
  2435. * accounting.
  2436. * To solve this, we're committing the transaction, which will
  2437. * ensure we run all delayed refs and only after that, we are
  2438. * going to clear all tracking information for a clean start.
  2439. */
  2440. trans = btrfs_join_transaction(fs_info->fs_root);
  2441. if (IS_ERR(trans)) {
  2442. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2443. return PTR_ERR(trans);
  2444. }
  2445. ret = btrfs_commit_transaction(trans, fs_info->fs_root);
  2446. if (ret) {
  2447. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2448. return ret;
  2449. }
  2450. qgroup_rescan_zero_tracking(fs_info);
  2451. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2452. &fs_info->qgroup_rescan_work);
  2453. return 0;
  2454. }
  2455. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
  2456. {
  2457. int running;
  2458. int ret = 0;
  2459. mutex_lock(&fs_info->qgroup_rescan_lock);
  2460. spin_lock(&fs_info->qgroup_lock);
  2461. running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2462. spin_unlock(&fs_info->qgroup_lock);
  2463. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2464. if (running)
  2465. ret = wait_for_completion_interruptible(
  2466. &fs_info->qgroup_rescan_completion);
  2467. return ret;
  2468. }
  2469. /*
  2470. * this is only called from open_ctree where we're still single threaded, thus
  2471. * locking is omitted here.
  2472. */
  2473. void
  2474. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2475. {
  2476. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2477. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2478. &fs_info->qgroup_rescan_work);
  2479. }