qgroup.c 78 KB

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