qgroup.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  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. btrfs_free_path(path);
  685. return ret;
  686. }
  687. int btrfs_quota_enable(struct btrfs_trans_handle *trans,
  688. struct btrfs_fs_info *fs_info)
  689. {
  690. struct btrfs_root *quota_root;
  691. struct btrfs_root *tree_root = fs_info->tree_root;
  692. struct btrfs_path *path = NULL;
  693. struct btrfs_qgroup_status_item *ptr;
  694. struct extent_buffer *leaf;
  695. struct btrfs_key key;
  696. struct btrfs_key found_key;
  697. struct btrfs_qgroup *qgroup = NULL;
  698. int ret = 0;
  699. int slot;
  700. mutex_lock(&fs_info->qgroup_ioctl_lock);
  701. if (fs_info->quota_root) {
  702. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  703. goto out;
  704. }
  705. fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
  706. if (!fs_info->qgroup_ulist) {
  707. ret = -ENOMEM;
  708. goto out;
  709. }
  710. /*
  711. * initially create the quota tree
  712. */
  713. quota_root = btrfs_create_tree(trans, fs_info,
  714. BTRFS_QUOTA_TREE_OBJECTID);
  715. if (IS_ERR(quota_root)) {
  716. ret = PTR_ERR(quota_root);
  717. goto out;
  718. }
  719. path = btrfs_alloc_path();
  720. if (!path) {
  721. ret = -ENOMEM;
  722. goto out_free_root;
  723. }
  724. key.objectid = 0;
  725. key.type = BTRFS_QGROUP_STATUS_KEY;
  726. key.offset = 0;
  727. ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
  728. sizeof(*ptr));
  729. if (ret)
  730. goto out_free_path;
  731. leaf = path->nodes[0];
  732. ptr = btrfs_item_ptr(leaf, path->slots[0],
  733. struct btrfs_qgroup_status_item);
  734. btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
  735. btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
  736. fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
  737. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  738. btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
  739. btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
  740. btrfs_mark_buffer_dirty(leaf);
  741. key.objectid = 0;
  742. key.type = BTRFS_ROOT_REF_KEY;
  743. key.offset = 0;
  744. btrfs_release_path(path);
  745. ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
  746. if (ret > 0)
  747. goto out_add_root;
  748. if (ret < 0)
  749. goto out_free_path;
  750. while (1) {
  751. slot = path->slots[0];
  752. leaf = path->nodes[0];
  753. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  754. if (found_key.type == BTRFS_ROOT_REF_KEY) {
  755. ret = add_qgroup_item(trans, quota_root,
  756. found_key.offset);
  757. if (ret)
  758. goto out_free_path;
  759. qgroup = add_qgroup_rb(fs_info, found_key.offset);
  760. if (IS_ERR(qgroup)) {
  761. ret = PTR_ERR(qgroup);
  762. goto out_free_path;
  763. }
  764. }
  765. ret = btrfs_next_item(tree_root, path);
  766. if (ret < 0)
  767. goto out_free_path;
  768. if (ret)
  769. break;
  770. }
  771. out_add_root:
  772. btrfs_release_path(path);
  773. ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
  774. if (ret)
  775. goto out_free_path;
  776. qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
  777. if (IS_ERR(qgroup)) {
  778. ret = PTR_ERR(qgroup);
  779. goto out_free_path;
  780. }
  781. spin_lock(&fs_info->qgroup_lock);
  782. fs_info->quota_root = quota_root;
  783. set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
  784. spin_unlock(&fs_info->qgroup_lock);
  785. out_free_path:
  786. btrfs_free_path(path);
  787. out_free_root:
  788. if (ret) {
  789. free_extent_buffer(quota_root->node);
  790. free_extent_buffer(quota_root->commit_root);
  791. kfree(quota_root);
  792. }
  793. out:
  794. if (ret) {
  795. ulist_free(fs_info->qgroup_ulist);
  796. fs_info->qgroup_ulist = NULL;
  797. }
  798. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  799. return ret;
  800. }
  801. int btrfs_quota_disable(struct btrfs_trans_handle *trans,
  802. struct btrfs_fs_info *fs_info)
  803. {
  804. struct btrfs_root *quota_root;
  805. int ret = 0;
  806. mutex_lock(&fs_info->qgroup_ioctl_lock);
  807. if (!fs_info->quota_root)
  808. goto out;
  809. clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  810. btrfs_qgroup_wait_for_completion(fs_info, false);
  811. spin_lock(&fs_info->qgroup_lock);
  812. quota_root = fs_info->quota_root;
  813. fs_info->quota_root = NULL;
  814. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  815. spin_unlock(&fs_info->qgroup_lock);
  816. btrfs_free_qgroup_config(fs_info);
  817. ret = btrfs_clean_quota_tree(trans, quota_root);
  818. if (ret)
  819. goto out;
  820. ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
  821. if (ret)
  822. goto out;
  823. list_del(&quota_root->dirty_list);
  824. btrfs_tree_lock(quota_root->node);
  825. clean_tree_block(fs_info, quota_root->node);
  826. btrfs_tree_unlock(quota_root->node);
  827. btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
  828. free_extent_buffer(quota_root->node);
  829. free_extent_buffer(quota_root->commit_root);
  830. kfree(quota_root);
  831. out:
  832. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  833. return ret;
  834. }
  835. static void qgroup_dirty(struct btrfs_fs_info *fs_info,
  836. struct btrfs_qgroup *qgroup)
  837. {
  838. if (list_empty(&qgroup->dirty))
  839. list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
  840. }
  841. static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
  842. struct btrfs_qgroup *qgroup,
  843. u64 num_bytes)
  844. {
  845. #ifdef CONFIG_BTRFS_DEBUG
  846. WARN_ON(qgroup->reserved < num_bytes);
  847. btrfs_debug(fs_info,
  848. "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
  849. qgroup->qgroupid, qgroup->reserved, num_bytes);
  850. #endif
  851. qgroup->reserved = 0;
  852. }
  853. /*
  854. * The easy accounting, if we are adding/removing the only ref for an extent
  855. * then this qgroup and all of the parent qgroups get their reference and
  856. * exclusive counts adjusted.
  857. *
  858. * Caller should hold fs_info->qgroup_lock.
  859. */
  860. static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
  861. struct ulist *tmp, u64 ref_root,
  862. u64 num_bytes, int sign)
  863. {
  864. struct btrfs_qgroup *qgroup;
  865. struct btrfs_qgroup_list *glist;
  866. struct ulist_node *unode;
  867. struct ulist_iterator uiter;
  868. int ret = 0;
  869. qgroup = find_qgroup_rb(fs_info, ref_root);
  870. if (!qgroup)
  871. goto out;
  872. qgroup->rfer += sign * num_bytes;
  873. qgroup->rfer_cmpr += sign * num_bytes;
  874. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  875. qgroup->excl += sign * num_bytes;
  876. qgroup->excl_cmpr += sign * num_bytes;
  877. if (sign > 0) {
  878. trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
  879. if (qgroup->reserved < num_bytes)
  880. report_reserved_underflow(fs_info, qgroup, num_bytes);
  881. else
  882. qgroup->reserved -= num_bytes;
  883. }
  884. qgroup_dirty(fs_info, qgroup);
  885. /* Get all of the parent groups that contain this qgroup */
  886. list_for_each_entry(glist, &qgroup->groups, next_group) {
  887. ret = ulist_add(tmp, glist->group->qgroupid,
  888. qgroup_to_aux(glist->group), GFP_ATOMIC);
  889. if (ret < 0)
  890. goto out;
  891. }
  892. /* Iterate all of the parents and adjust their reference counts */
  893. ULIST_ITER_INIT(&uiter);
  894. while ((unode = ulist_next(tmp, &uiter))) {
  895. qgroup = unode_aux_to_qgroup(unode);
  896. qgroup->rfer += sign * num_bytes;
  897. qgroup->rfer_cmpr += sign * num_bytes;
  898. WARN_ON(sign < 0 && qgroup->excl < num_bytes);
  899. qgroup->excl += sign * num_bytes;
  900. if (sign > 0) {
  901. trace_qgroup_update_reserve(fs_info, qgroup,
  902. -(s64)num_bytes);
  903. if (qgroup->reserved < num_bytes)
  904. report_reserved_underflow(fs_info, qgroup,
  905. num_bytes);
  906. else
  907. qgroup->reserved -= num_bytes;
  908. }
  909. qgroup->excl_cmpr += sign * num_bytes;
  910. qgroup_dirty(fs_info, qgroup);
  911. /* Add any parents of the parents */
  912. list_for_each_entry(glist, &qgroup->groups, next_group) {
  913. ret = ulist_add(tmp, glist->group->qgroupid,
  914. qgroup_to_aux(glist->group), GFP_ATOMIC);
  915. if (ret < 0)
  916. goto out;
  917. }
  918. }
  919. ret = 0;
  920. out:
  921. return ret;
  922. }
  923. /*
  924. * Quick path for updating qgroup with only excl refs.
  925. *
  926. * In that case, just update all parent will be enough.
  927. * Or we needs to do a full rescan.
  928. * Caller should also hold fs_info->qgroup_lock.
  929. *
  930. * Return 0 for quick update, return >0 for need to full rescan
  931. * and mark INCONSISTENT flag.
  932. * Return < 0 for other error.
  933. */
  934. static int quick_update_accounting(struct btrfs_fs_info *fs_info,
  935. struct ulist *tmp, u64 src, u64 dst,
  936. int sign)
  937. {
  938. struct btrfs_qgroup *qgroup;
  939. int ret = 1;
  940. int err = 0;
  941. qgroup = find_qgroup_rb(fs_info, src);
  942. if (!qgroup)
  943. goto out;
  944. if (qgroup->excl == qgroup->rfer) {
  945. ret = 0;
  946. err = __qgroup_excl_accounting(fs_info, tmp, dst,
  947. qgroup->excl, sign);
  948. if (err < 0) {
  949. ret = err;
  950. goto out;
  951. }
  952. }
  953. out:
  954. if (ret)
  955. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  956. return ret;
  957. }
  958. int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
  959. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  960. {
  961. struct btrfs_root *quota_root;
  962. struct btrfs_qgroup *parent;
  963. struct btrfs_qgroup *member;
  964. struct btrfs_qgroup_list *list;
  965. struct ulist *tmp;
  966. int ret = 0;
  967. /* Check the level of src and dst first */
  968. if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
  969. return -EINVAL;
  970. tmp = ulist_alloc(GFP_KERNEL);
  971. if (!tmp)
  972. return -ENOMEM;
  973. mutex_lock(&fs_info->qgroup_ioctl_lock);
  974. quota_root = fs_info->quota_root;
  975. if (!quota_root) {
  976. ret = -EINVAL;
  977. goto out;
  978. }
  979. member = find_qgroup_rb(fs_info, src);
  980. parent = find_qgroup_rb(fs_info, dst);
  981. if (!member || !parent) {
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. /* check if such qgroup relation exist firstly */
  986. list_for_each_entry(list, &member->groups, next_group) {
  987. if (list->group == parent) {
  988. ret = -EEXIST;
  989. goto out;
  990. }
  991. }
  992. ret = add_qgroup_relation_item(trans, quota_root, src, dst);
  993. if (ret)
  994. goto out;
  995. ret = add_qgroup_relation_item(trans, quota_root, dst, src);
  996. if (ret) {
  997. del_qgroup_relation_item(trans, quota_root, src, dst);
  998. goto out;
  999. }
  1000. spin_lock(&fs_info->qgroup_lock);
  1001. ret = add_relation_rb(fs_info, src, dst);
  1002. if (ret < 0) {
  1003. spin_unlock(&fs_info->qgroup_lock);
  1004. goto out;
  1005. }
  1006. ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
  1007. spin_unlock(&fs_info->qgroup_lock);
  1008. out:
  1009. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1010. ulist_free(tmp);
  1011. return ret;
  1012. }
  1013. static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
  1014. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1015. {
  1016. struct btrfs_root *quota_root;
  1017. struct btrfs_qgroup *parent;
  1018. struct btrfs_qgroup *member;
  1019. struct btrfs_qgroup_list *list;
  1020. struct ulist *tmp;
  1021. int ret = 0;
  1022. int err;
  1023. tmp = ulist_alloc(GFP_KERNEL);
  1024. if (!tmp)
  1025. return -ENOMEM;
  1026. quota_root = fs_info->quota_root;
  1027. if (!quota_root) {
  1028. ret = -EINVAL;
  1029. goto out;
  1030. }
  1031. member = find_qgroup_rb(fs_info, src);
  1032. parent = find_qgroup_rb(fs_info, dst);
  1033. if (!member || !parent) {
  1034. ret = -EINVAL;
  1035. goto out;
  1036. }
  1037. /* check if such qgroup relation exist firstly */
  1038. list_for_each_entry(list, &member->groups, next_group) {
  1039. if (list->group == parent)
  1040. goto exist;
  1041. }
  1042. ret = -ENOENT;
  1043. goto out;
  1044. exist:
  1045. ret = del_qgroup_relation_item(trans, quota_root, src, dst);
  1046. err = del_qgroup_relation_item(trans, quota_root, dst, src);
  1047. if (err && !ret)
  1048. ret = err;
  1049. spin_lock(&fs_info->qgroup_lock);
  1050. del_relation_rb(fs_info, src, dst);
  1051. ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
  1052. spin_unlock(&fs_info->qgroup_lock);
  1053. out:
  1054. ulist_free(tmp);
  1055. return ret;
  1056. }
  1057. int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
  1058. struct btrfs_fs_info *fs_info, u64 src, u64 dst)
  1059. {
  1060. int ret = 0;
  1061. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1062. ret = __del_qgroup_relation(trans, fs_info, src, dst);
  1063. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1064. return ret;
  1065. }
  1066. int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
  1067. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1068. {
  1069. struct btrfs_root *quota_root;
  1070. struct btrfs_qgroup *qgroup;
  1071. int ret = 0;
  1072. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1073. quota_root = fs_info->quota_root;
  1074. if (!quota_root) {
  1075. ret = -EINVAL;
  1076. goto out;
  1077. }
  1078. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1079. if (qgroup) {
  1080. ret = -EEXIST;
  1081. goto out;
  1082. }
  1083. ret = add_qgroup_item(trans, quota_root, qgroupid);
  1084. if (ret)
  1085. goto out;
  1086. spin_lock(&fs_info->qgroup_lock);
  1087. qgroup = add_qgroup_rb(fs_info, qgroupid);
  1088. spin_unlock(&fs_info->qgroup_lock);
  1089. if (IS_ERR(qgroup))
  1090. ret = PTR_ERR(qgroup);
  1091. out:
  1092. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1093. return ret;
  1094. }
  1095. int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
  1096. struct btrfs_fs_info *fs_info, u64 qgroupid)
  1097. {
  1098. struct btrfs_root *quota_root;
  1099. struct btrfs_qgroup *qgroup;
  1100. struct btrfs_qgroup_list *list;
  1101. int ret = 0;
  1102. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1103. quota_root = fs_info->quota_root;
  1104. if (!quota_root) {
  1105. ret = -EINVAL;
  1106. goto out;
  1107. }
  1108. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1109. if (!qgroup) {
  1110. ret = -ENOENT;
  1111. goto out;
  1112. } else {
  1113. /* check if there are no children of this qgroup */
  1114. if (!list_empty(&qgroup->members)) {
  1115. ret = -EBUSY;
  1116. goto out;
  1117. }
  1118. }
  1119. ret = del_qgroup_item(trans, quota_root, qgroupid);
  1120. if (ret && ret != -ENOENT)
  1121. goto out;
  1122. while (!list_empty(&qgroup->groups)) {
  1123. list = list_first_entry(&qgroup->groups,
  1124. struct btrfs_qgroup_list, next_group);
  1125. ret = __del_qgroup_relation(trans, fs_info,
  1126. qgroupid,
  1127. list->group->qgroupid);
  1128. if (ret)
  1129. goto out;
  1130. }
  1131. spin_lock(&fs_info->qgroup_lock);
  1132. del_qgroup_rb(fs_info, qgroupid);
  1133. spin_unlock(&fs_info->qgroup_lock);
  1134. out:
  1135. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1136. return ret;
  1137. }
  1138. int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
  1139. struct btrfs_fs_info *fs_info, u64 qgroupid,
  1140. struct btrfs_qgroup_limit *limit)
  1141. {
  1142. struct btrfs_root *quota_root;
  1143. struct btrfs_qgroup *qgroup;
  1144. int ret = 0;
  1145. /* Sometimes we would want to clear the limit on this qgroup.
  1146. * To meet this requirement, we treat the -1 as a special value
  1147. * which tell kernel to clear the limit on this qgroup.
  1148. */
  1149. const u64 CLEAR_VALUE = -1;
  1150. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1151. quota_root = fs_info->quota_root;
  1152. if (!quota_root) {
  1153. ret = -EINVAL;
  1154. goto out;
  1155. }
  1156. qgroup = find_qgroup_rb(fs_info, qgroupid);
  1157. if (!qgroup) {
  1158. ret = -ENOENT;
  1159. goto out;
  1160. }
  1161. spin_lock(&fs_info->qgroup_lock);
  1162. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
  1163. if (limit->max_rfer == CLEAR_VALUE) {
  1164. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1165. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
  1166. qgroup->max_rfer = 0;
  1167. } else {
  1168. qgroup->max_rfer = limit->max_rfer;
  1169. }
  1170. }
  1171. if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
  1172. if (limit->max_excl == CLEAR_VALUE) {
  1173. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1174. limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
  1175. qgroup->max_excl = 0;
  1176. } else {
  1177. qgroup->max_excl = limit->max_excl;
  1178. }
  1179. }
  1180. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
  1181. if (limit->rsv_rfer == CLEAR_VALUE) {
  1182. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1183. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
  1184. qgroup->rsv_rfer = 0;
  1185. } else {
  1186. qgroup->rsv_rfer = limit->rsv_rfer;
  1187. }
  1188. }
  1189. if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
  1190. if (limit->rsv_excl == CLEAR_VALUE) {
  1191. qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1192. limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
  1193. qgroup->rsv_excl = 0;
  1194. } else {
  1195. qgroup->rsv_excl = limit->rsv_excl;
  1196. }
  1197. }
  1198. qgroup->lim_flags |= limit->flags;
  1199. spin_unlock(&fs_info->qgroup_lock);
  1200. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1201. if (ret) {
  1202. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1203. btrfs_info(fs_info, "unable to update quota limit for %llu",
  1204. qgroupid);
  1205. }
  1206. out:
  1207. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  1208. return ret;
  1209. }
  1210. int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
  1211. struct btrfs_delayed_ref_root *delayed_refs,
  1212. struct btrfs_qgroup_extent_record *record)
  1213. {
  1214. struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
  1215. struct rb_node *parent_node = NULL;
  1216. struct btrfs_qgroup_extent_record *entry;
  1217. u64 bytenr = record->bytenr;
  1218. assert_spin_locked(&delayed_refs->lock);
  1219. trace_btrfs_qgroup_trace_extent(fs_info, record);
  1220. while (*p) {
  1221. parent_node = *p;
  1222. entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
  1223. node);
  1224. if (bytenr < entry->bytenr)
  1225. p = &(*p)->rb_left;
  1226. else if (bytenr > entry->bytenr)
  1227. p = &(*p)->rb_right;
  1228. else
  1229. return 1;
  1230. }
  1231. rb_link_node(&record->node, parent_node, p);
  1232. rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
  1233. return 0;
  1234. }
  1235. int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
  1236. struct btrfs_qgroup_extent_record *qrecord)
  1237. {
  1238. struct ulist *old_root;
  1239. u64 bytenr = qrecord->bytenr;
  1240. int ret;
  1241. ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
  1242. if (ret < 0)
  1243. return ret;
  1244. /*
  1245. * Here we don't need to get the lock of
  1246. * trans->transaction->delayed_refs, since inserted qrecord won't
  1247. * be deleted, only qrecord->node may be modified (new qrecord insert)
  1248. *
  1249. * So modifying qrecord->old_roots is safe here
  1250. */
  1251. qrecord->old_roots = old_root;
  1252. return 0;
  1253. }
  1254. int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
  1255. struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
  1256. gfp_t gfp_flag)
  1257. {
  1258. struct btrfs_qgroup_extent_record *record;
  1259. struct btrfs_delayed_ref_root *delayed_refs;
  1260. int ret;
  1261. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
  1262. || bytenr == 0 || num_bytes == 0)
  1263. return 0;
  1264. if (WARN_ON(trans == NULL))
  1265. return -EINVAL;
  1266. record = kmalloc(sizeof(*record), gfp_flag);
  1267. if (!record)
  1268. return -ENOMEM;
  1269. delayed_refs = &trans->transaction->delayed_refs;
  1270. record->bytenr = bytenr;
  1271. record->num_bytes = num_bytes;
  1272. record->old_roots = NULL;
  1273. spin_lock(&delayed_refs->lock);
  1274. ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
  1275. spin_unlock(&delayed_refs->lock);
  1276. if (ret > 0) {
  1277. kfree(record);
  1278. return 0;
  1279. }
  1280. return btrfs_qgroup_trace_extent_post(fs_info, record);
  1281. }
  1282. int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
  1283. struct btrfs_fs_info *fs_info,
  1284. struct extent_buffer *eb)
  1285. {
  1286. int nr = btrfs_header_nritems(eb);
  1287. int i, extent_type, ret;
  1288. struct btrfs_key key;
  1289. struct btrfs_file_extent_item *fi;
  1290. u64 bytenr, num_bytes;
  1291. /* We can be called directly from walk_up_proc() */
  1292. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1293. return 0;
  1294. for (i = 0; i < nr; i++) {
  1295. btrfs_item_key_to_cpu(eb, &key, i);
  1296. if (key.type != BTRFS_EXTENT_DATA_KEY)
  1297. continue;
  1298. fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
  1299. /* filter out non qgroup-accountable extents */
  1300. extent_type = btrfs_file_extent_type(eb, fi);
  1301. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1302. continue;
  1303. bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
  1304. if (!bytenr)
  1305. continue;
  1306. num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
  1307. ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
  1308. num_bytes, GFP_NOFS);
  1309. if (ret)
  1310. return ret;
  1311. }
  1312. cond_resched();
  1313. return 0;
  1314. }
  1315. /*
  1316. * Walk up the tree from the bottom, freeing leaves and any interior
  1317. * nodes which have had all slots visited. If a node (leaf or
  1318. * interior) is freed, the node above it will have it's slot
  1319. * incremented. The root node will never be freed.
  1320. *
  1321. * At the end of this function, we should have a path which has all
  1322. * slots incremented to the next position for a search. If we need to
  1323. * read a new node it will be NULL and the node above it will have the
  1324. * correct slot selected for a later read.
  1325. *
  1326. * If we increment the root nodes slot counter past the number of
  1327. * elements, 1 is returned to signal completion of the search.
  1328. */
  1329. static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
  1330. {
  1331. int level = 0;
  1332. int nr, slot;
  1333. struct extent_buffer *eb;
  1334. if (root_level == 0)
  1335. return 1;
  1336. while (level <= root_level) {
  1337. eb = path->nodes[level];
  1338. nr = btrfs_header_nritems(eb);
  1339. path->slots[level]++;
  1340. slot = path->slots[level];
  1341. if (slot >= nr || level == 0) {
  1342. /*
  1343. * Don't free the root - we will detect this
  1344. * condition after our loop and return a
  1345. * positive value for caller to stop walking the tree.
  1346. */
  1347. if (level != root_level) {
  1348. btrfs_tree_unlock_rw(eb, path->locks[level]);
  1349. path->locks[level] = 0;
  1350. free_extent_buffer(eb);
  1351. path->nodes[level] = NULL;
  1352. path->slots[level] = 0;
  1353. }
  1354. } else {
  1355. /*
  1356. * We have a valid slot to walk back down
  1357. * from. Stop here so caller can process these
  1358. * new nodes.
  1359. */
  1360. break;
  1361. }
  1362. level++;
  1363. }
  1364. eb = path->nodes[root_level];
  1365. if (path->slots[root_level] >= btrfs_header_nritems(eb))
  1366. return 1;
  1367. return 0;
  1368. }
  1369. int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
  1370. struct btrfs_root *root,
  1371. struct extent_buffer *root_eb,
  1372. u64 root_gen, int root_level)
  1373. {
  1374. struct btrfs_fs_info *fs_info = root->fs_info;
  1375. int ret = 0;
  1376. int level;
  1377. struct extent_buffer *eb = root_eb;
  1378. struct btrfs_path *path = NULL;
  1379. BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
  1380. BUG_ON(root_eb == NULL);
  1381. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1382. return 0;
  1383. if (!extent_buffer_uptodate(root_eb)) {
  1384. ret = btrfs_read_buffer(root_eb, root_gen);
  1385. if (ret)
  1386. goto out;
  1387. }
  1388. if (root_level == 0) {
  1389. ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
  1390. goto out;
  1391. }
  1392. path = btrfs_alloc_path();
  1393. if (!path)
  1394. return -ENOMEM;
  1395. /*
  1396. * Walk down the tree. Missing extent blocks are filled in as
  1397. * we go. Metadata is accounted every time we read a new
  1398. * extent block.
  1399. *
  1400. * When we reach a leaf, we account for file extent items in it,
  1401. * walk back up the tree (adjusting slot pointers as we go)
  1402. * and restart the search process.
  1403. */
  1404. extent_buffer_get(root_eb); /* For path */
  1405. path->nodes[root_level] = root_eb;
  1406. path->slots[root_level] = 0;
  1407. path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
  1408. walk_down:
  1409. level = root_level;
  1410. while (level >= 0) {
  1411. if (path->nodes[level] == NULL) {
  1412. int parent_slot;
  1413. u64 child_gen;
  1414. u64 child_bytenr;
  1415. /*
  1416. * We need to get child blockptr/gen from parent before
  1417. * we can read it.
  1418. */
  1419. eb = path->nodes[level + 1];
  1420. parent_slot = path->slots[level + 1];
  1421. child_bytenr = btrfs_node_blockptr(eb, parent_slot);
  1422. child_gen = btrfs_node_ptr_generation(eb, parent_slot);
  1423. eb = read_tree_block(fs_info, child_bytenr, child_gen);
  1424. if (IS_ERR(eb)) {
  1425. ret = PTR_ERR(eb);
  1426. goto out;
  1427. } else if (!extent_buffer_uptodate(eb)) {
  1428. free_extent_buffer(eb);
  1429. ret = -EIO;
  1430. goto out;
  1431. }
  1432. path->nodes[level] = eb;
  1433. path->slots[level] = 0;
  1434. btrfs_tree_read_lock(eb);
  1435. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1436. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  1437. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  1438. child_bytenr,
  1439. fs_info->nodesize,
  1440. GFP_NOFS);
  1441. if (ret)
  1442. goto out;
  1443. }
  1444. if (level == 0) {
  1445. ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
  1446. path->nodes[level]);
  1447. if (ret)
  1448. goto out;
  1449. /* Nonzero return here means we completed our search */
  1450. ret = adjust_slots_upwards(path, root_level);
  1451. if (ret)
  1452. break;
  1453. /* Restart search with new slots */
  1454. goto walk_down;
  1455. }
  1456. level--;
  1457. }
  1458. ret = 0;
  1459. out:
  1460. btrfs_free_path(path);
  1461. return ret;
  1462. }
  1463. #define UPDATE_NEW 0
  1464. #define UPDATE_OLD 1
  1465. /*
  1466. * Walk all of the roots that points to the bytenr and adjust their refcnts.
  1467. */
  1468. static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
  1469. struct ulist *roots, struct ulist *tmp,
  1470. struct ulist *qgroups, u64 seq, int update_old)
  1471. {
  1472. struct ulist_node *unode;
  1473. struct ulist_iterator uiter;
  1474. struct ulist_node *tmp_unode;
  1475. struct ulist_iterator tmp_uiter;
  1476. struct btrfs_qgroup *qg;
  1477. int ret = 0;
  1478. if (!roots)
  1479. return 0;
  1480. ULIST_ITER_INIT(&uiter);
  1481. while ((unode = ulist_next(roots, &uiter))) {
  1482. qg = find_qgroup_rb(fs_info, unode->val);
  1483. if (!qg)
  1484. continue;
  1485. ulist_reinit(tmp);
  1486. ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
  1487. GFP_ATOMIC);
  1488. if (ret < 0)
  1489. return ret;
  1490. ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
  1491. if (ret < 0)
  1492. return ret;
  1493. ULIST_ITER_INIT(&tmp_uiter);
  1494. while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
  1495. struct btrfs_qgroup_list *glist;
  1496. qg = unode_aux_to_qgroup(tmp_unode);
  1497. if (update_old)
  1498. btrfs_qgroup_update_old_refcnt(qg, seq, 1);
  1499. else
  1500. btrfs_qgroup_update_new_refcnt(qg, seq, 1);
  1501. list_for_each_entry(glist, &qg->groups, next_group) {
  1502. ret = ulist_add(qgroups, glist->group->qgroupid,
  1503. qgroup_to_aux(glist->group),
  1504. GFP_ATOMIC);
  1505. if (ret < 0)
  1506. return ret;
  1507. ret = ulist_add(tmp, glist->group->qgroupid,
  1508. qgroup_to_aux(glist->group),
  1509. GFP_ATOMIC);
  1510. if (ret < 0)
  1511. return ret;
  1512. }
  1513. }
  1514. }
  1515. return 0;
  1516. }
  1517. /*
  1518. * Update qgroup rfer/excl counters.
  1519. * Rfer update is easy, codes can explain themselves.
  1520. *
  1521. * Excl update is tricky, the update is split into 2 part.
  1522. * Part 1: Possible exclusive <-> sharing detect:
  1523. * | A | !A |
  1524. * -------------------------------------
  1525. * B | * | - |
  1526. * -------------------------------------
  1527. * !B | + | ** |
  1528. * -------------------------------------
  1529. *
  1530. * Conditions:
  1531. * A: cur_old_roots < nr_old_roots (not exclusive before)
  1532. * !A: cur_old_roots == nr_old_roots (possible exclusive before)
  1533. * B: cur_new_roots < nr_new_roots (not exclusive now)
  1534. * !B: cur_new_roots == nr_new_roots (possible exclusive now)
  1535. *
  1536. * Results:
  1537. * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
  1538. * *: Definitely not changed. **: Possible unchanged.
  1539. *
  1540. * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
  1541. *
  1542. * To make the logic clear, we first use condition A and B to split
  1543. * combination into 4 results.
  1544. *
  1545. * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
  1546. * only on variant maybe 0.
  1547. *
  1548. * Lastly, check result **, since there are 2 variants maybe 0, split them
  1549. * again(2x2).
  1550. * But this time we don't need to consider other things, the codes and logic
  1551. * is easy to understand now.
  1552. */
  1553. static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
  1554. struct ulist *qgroups,
  1555. u64 nr_old_roots,
  1556. u64 nr_new_roots,
  1557. u64 num_bytes, u64 seq)
  1558. {
  1559. struct ulist_node *unode;
  1560. struct ulist_iterator uiter;
  1561. struct btrfs_qgroup *qg;
  1562. u64 cur_new_count, cur_old_count;
  1563. ULIST_ITER_INIT(&uiter);
  1564. while ((unode = ulist_next(qgroups, &uiter))) {
  1565. bool dirty = false;
  1566. qg = unode_aux_to_qgroup(unode);
  1567. cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
  1568. cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
  1569. trace_qgroup_update_counters(fs_info, qg->qgroupid,
  1570. cur_old_count, cur_new_count);
  1571. /* Rfer update part */
  1572. if (cur_old_count == 0 && cur_new_count > 0) {
  1573. qg->rfer += num_bytes;
  1574. qg->rfer_cmpr += num_bytes;
  1575. dirty = true;
  1576. }
  1577. if (cur_old_count > 0 && cur_new_count == 0) {
  1578. qg->rfer -= num_bytes;
  1579. qg->rfer_cmpr -= num_bytes;
  1580. dirty = true;
  1581. }
  1582. /* Excl update part */
  1583. /* Exclusive/none -> shared case */
  1584. if (cur_old_count == nr_old_roots &&
  1585. cur_new_count < nr_new_roots) {
  1586. /* Exclusive -> shared */
  1587. if (cur_old_count != 0) {
  1588. qg->excl -= num_bytes;
  1589. qg->excl_cmpr -= num_bytes;
  1590. dirty = true;
  1591. }
  1592. }
  1593. /* Shared -> exclusive/none case */
  1594. if (cur_old_count < nr_old_roots &&
  1595. cur_new_count == nr_new_roots) {
  1596. /* Shared->exclusive */
  1597. if (cur_new_count != 0) {
  1598. qg->excl += num_bytes;
  1599. qg->excl_cmpr += num_bytes;
  1600. dirty = true;
  1601. }
  1602. }
  1603. /* Exclusive/none -> exclusive/none case */
  1604. if (cur_old_count == nr_old_roots &&
  1605. cur_new_count == nr_new_roots) {
  1606. if (cur_old_count == 0) {
  1607. /* None -> exclusive/none */
  1608. if (cur_new_count != 0) {
  1609. /* None -> exclusive */
  1610. qg->excl += num_bytes;
  1611. qg->excl_cmpr += num_bytes;
  1612. dirty = true;
  1613. }
  1614. /* None -> none, nothing changed */
  1615. } else {
  1616. /* Exclusive -> exclusive/none */
  1617. if (cur_new_count == 0) {
  1618. /* Exclusive -> none */
  1619. qg->excl -= num_bytes;
  1620. qg->excl_cmpr -= num_bytes;
  1621. dirty = true;
  1622. }
  1623. /* Exclusive -> exclusive, nothing changed */
  1624. }
  1625. }
  1626. if (dirty)
  1627. qgroup_dirty(fs_info, qg);
  1628. }
  1629. return 0;
  1630. }
  1631. /*
  1632. * Check if the @roots potentially is a list of fs tree roots
  1633. *
  1634. * Return 0 for definitely not a fs/subvol tree roots ulist
  1635. * Return 1 for possible fs/subvol tree roots in the list (considering an empty
  1636. * one as well)
  1637. */
  1638. static int maybe_fs_roots(struct ulist *roots)
  1639. {
  1640. struct ulist_node *unode;
  1641. struct ulist_iterator uiter;
  1642. /* Empty one, still possible for fs roots */
  1643. if (!roots || roots->nnodes == 0)
  1644. return 1;
  1645. ULIST_ITER_INIT(&uiter);
  1646. unode = ulist_next(roots, &uiter);
  1647. if (!unode)
  1648. return 1;
  1649. /*
  1650. * If it contains fs tree roots, then it must belong to fs/subvol
  1651. * trees.
  1652. * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
  1653. */
  1654. return is_fstree(unode->val);
  1655. }
  1656. int
  1657. btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
  1658. struct btrfs_fs_info *fs_info,
  1659. u64 bytenr, u64 num_bytes,
  1660. struct ulist *old_roots, struct ulist *new_roots)
  1661. {
  1662. struct ulist *qgroups = NULL;
  1663. struct ulist *tmp = NULL;
  1664. u64 seq;
  1665. u64 nr_new_roots = 0;
  1666. u64 nr_old_roots = 0;
  1667. int ret = 0;
  1668. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1669. return 0;
  1670. if (new_roots) {
  1671. if (!maybe_fs_roots(new_roots))
  1672. goto out_free;
  1673. nr_new_roots = new_roots->nnodes;
  1674. }
  1675. if (old_roots) {
  1676. if (!maybe_fs_roots(old_roots))
  1677. goto out_free;
  1678. nr_old_roots = old_roots->nnodes;
  1679. }
  1680. /* Quick exit, either not fs tree roots, or won't affect any qgroup */
  1681. if (nr_old_roots == 0 && nr_new_roots == 0)
  1682. goto out_free;
  1683. BUG_ON(!fs_info->quota_root);
  1684. trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
  1685. nr_old_roots, nr_new_roots);
  1686. qgroups = ulist_alloc(GFP_NOFS);
  1687. if (!qgroups) {
  1688. ret = -ENOMEM;
  1689. goto out_free;
  1690. }
  1691. tmp = ulist_alloc(GFP_NOFS);
  1692. if (!tmp) {
  1693. ret = -ENOMEM;
  1694. goto out_free;
  1695. }
  1696. mutex_lock(&fs_info->qgroup_rescan_lock);
  1697. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  1698. if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
  1699. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1700. ret = 0;
  1701. goto out_free;
  1702. }
  1703. }
  1704. mutex_unlock(&fs_info->qgroup_rescan_lock);
  1705. spin_lock(&fs_info->qgroup_lock);
  1706. seq = fs_info->qgroup_seq;
  1707. /* Update old refcnts using old_roots */
  1708. ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
  1709. UPDATE_OLD);
  1710. if (ret < 0)
  1711. goto out;
  1712. /* Update new refcnts using new_roots */
  1713. ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
  1714. UPDATE_NEW);
  1715. if (ret < 0)
  1716. goto out;
  1717. qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
  1718. num_bytes, seq);
  1719. /*
  1720. * Bump qgroup_seq to avoid seq overlap
  1721. */
  1722. fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
  1723. out:
  1724. spin_unlock(&fs_info->qgroup_lock);
  1725. out_free:
  1726. ulist_free(tmp);
  1727. ulist_free(qgroups);
  1728. ulist_free(old_roots);
  1729. ulist_free(new_roots);
  1730. return ret;
  1731. }
  1732. int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
  1733. struct btrfs_fs_info *fs_info)
  1734. {
  1735. struct btrfs_qgroup_extent_record *record;
  1736. struct btrfs_delayed_ref_root *delayed_refs;
  1737. struct ulist *new_roots = NULL;
  1738. struct rb_node *node;
  1739. u64 qgroup_to_skip;
  1740. int ret = 0;
  1741. delayed_refs = &trans->transaction->delayed_refs;
  1742. qgroup_to_skip = delayed_refs->qgroup_to_skip;
  1743. while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
  1744. record = rb_entry(node, struct btrfs_qgroup_extent_record,
  1745. node);
  1746. trace_btrfs_qgroup_account_extents(fs_info, record);
  1747. if (!ret) {
  1748. /*
  1749. * Old roots should be searched when inserting qgroup
  1750. * extent record
  1751. */
  1752. if (WARN_ON(!record->old_roots)) {
  1753. /* Search commit root to find old_roots */
  1754. ret = btrfs_find_all_roots(NULL, fs_info,
  1755. record->bytenr, 0,
  1756. &record->old_roots, false);
  1757. if (ret < 0)
  1758. goto cleanup;
  1759. }
  1760. /*
  1761. * Use SEQ_LAST as time_seq to do special search, which
  1762. * doesn't lock tree or delayed_refs and search current
  1763. * root. It's safe inside commit_transaction().
  1764. */
  1765. ret = btrfs_find_all_roots(trans, fs_info,
  1766. record->bytenr, SEQ_LAST, &new_roots, false);
  1767. if (ret < 0)
  1768. goto cleanup;
  1769. if (qgroup_to_skip) {
  1770. ulist_del(new_roots, qgroup_to_skip, 0);
  1771. ulist_del(record->old_roots, qgroup_to_skip,
  1772. 0);
  1773. }
  1774. ret = btrfs_qgroup_account_extent(trans, fs_info,
  1775. record->bytenr, record->num_bytes,
  1776. record->old_roots, new_roots);
  1777. record->old_roots = NULL;
  1778. new_roots = NULL;
  1779. }
  1780. cleanup:
  1781. ulist_free(record->old_roots);
  1782. ulist_free(new_roots);
  1783. new_roots = NULL;
  1784. rb_erase(node, &delayed_refs->dirty_extent_root);
  1785. kfree(record);
  1786. }
  1787. return ret;
  1788. }
  1789. /*
  1790. * called from commit_transaction. Writes all changed qgroups to disk.
  1791. */
  1792. int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
  1793. struct btrfs_fs_info *fs_info)
  1794. {
  1795. struct btrfs_root *quota_root = fs_info->quota_root;
  1796. int ret = 0;
  1797. int start_rescan_worker = 0;
  1798. if (!quota_root)
  1799. goto out;
  1800. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
  1801. test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1802. start_rescan_worker = 1;
  1803. if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
  1804. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  1805. spin_lock(&fs_info->qgroup_lock);
  1806. while (!list_empty(&fs_info->dirty_qgroups)) {
  1807. struct btrfs_qgroup *qgroup;
  1808. qgroup = list_first_entry(&fs_info->dirty_qgroups,
  1809. struct btrfs_qgroup, dirty);
  1810. list_del_init(&qgroup->dirty);
  1811. spin_unlock(&fs_info->qgroup_lock);
  1812. ret = update_qgroup_info_item(trans, quota_root, qgroup);
  1813. if (ret)
  1814. fs_info->qgroup_flags |=
  1815. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1816. ret = update_qgroup_limit_item(trans, quota_root, qgroup);
  1817. if (ret)
  1818. fs_info->qgroup_flags |=
  1819. BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1820. spin_lock(&fs_info->qgroup_lock);
  1821. }
  1822. if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1823. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
  1824. else
  1825. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
  1826. spin_unlock(&fs_info->qgroup_lock);
  1827. ret = update_qgroup_status_item(trans, fs_info, quota_root);
  1828. if (ret)
  1829. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1830. if (!ret && start_rescan_worker) {
  1831. ret = qgroup_rescan_init(fs_info, 0, 1);
  1832. if (!ret) {
  1833. qgroup_rescan_zero_tracking(fs_info);
  1834. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  1835. &fs_info->qgroup_rescan_work);
  1836. }
  1837. ret = 0;
  1838. }
  1839. out:
  1840. return ret;
  1841. }
  1842. /*
  1843. * Copy the accounting information between qgroups. This is necessary
  1844. * when a snapshot or a subvolume is created. Throwing an error will
  1845. * cause a transaction abort so we take extra care here to only error
  1846. * when a readonly fs is a reasonable outcome.
  1847. */
  1848. int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
  1849. struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
  1850. struct btrfs_qgroup_inherit *inherit)
  1851. {
  1852. int ret = 0;
  1853. int i;
  1854. u64 *i_qgroups;
  1855. struct btrfs_root *quota_root = fs_info->quota_root;
  1856. struct btrfs_qgroup *srcgroup;
  1857. struct btrfs_qgroup *dstgroup;
  1858. u32 level_size = 0;
  1859. u64 nums;
  1860. mutex_lock(&fs_info->qgroup_ioctl_lock);
  1861. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1862. goto out;
  1863. if (!quota_root) {
  1864. ret = -EINVAL;
  1865. goto out;
  1866. }
  1867. if (inherit) {
  1868. i_qgroups = (u64 *)(inherit + 1);
  1869. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1870. 2 * inherit->num_excl_copies;
  1871. for (i = 0; i < nums; ++i) {
  1872. srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
  1873. /*
  1874. * Zero out invalid groups so we can ignore
  1875. * them later.
  1876. */
  1877. if (!srcgroup ||
  1878. ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
  1879. *i_qgroups = 0ULL;
  1880. ++i_qgroups;
  1881. }
  1882. }
  1883. /*
  1884. * create a tracking group for the subvol itself
  1885. */
  1886. ret = add_qgroup_item(trans, quota_root, objectid);
  1887. if (ret)
  1888. goto out;
  1889. if (srcid) {
  1890. struct btrfs_root *srcroot;
  1891. struct btrfs_key srckey;
  1892. srckey.objectid = srcid;
  1893. srckey.type = BTRFS_ROOT_ITEM_KEY;
  1894. srckey.offset = (u64)-1;
  1895. srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
  1896. if (IS_ERR(srcroot)) {
  1897. ret = PTR_ERR(srcroot);
  1898. goto out;
  1899. }
  1900. level_size = fs_info->nodesize;
  1901. }
  1902. /*
  1903. * add qgroup to all inherited groups
  1904. */
  1905. if (inherit) {
  1906. i_qgroups = (u64 *)(inherit + 1);
  1907. for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
  1908. if (*i_qgroups == 0)
  1909. continue;
  1910. ret = add_qgroup_relation_item(trans, quota_root,
  1911. objectid, *i_qgroups);
  1912. if (ret && ret != -EEXIST)
  1913. goto out;
  1914. ret = add_qgroup_relation_item(trans, quota_root,
  1915. *i_qgroups, objectid);
  1916. if (ret && ret != -EEXIST)
  1917. goto out;
  1918. }
  1919. ret = 0;
  1920. }
  1921. spin_lock(&fs_info->qgroup_lock);
  1922. dstgroup = add_qgroup_rb(fs_info, objectid);
  1923. if (IS_ERR(dstgroup)) {
  1924. ret = PTR_ERR(dstgroup);
  1925. goto unlock;
  1926. }
  1927. if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
  1928. dstgroup->lim_flags = inherit->lim.flags;
  1929. dstgroup->max_rfer = inherit->lim.max_rfer;
  1930. dstgroup->max_excl = inherit->lim.max_excl;
  1931. dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
  1932. dstgroup->rsv_excl = inherit->lim.rsv_excl;
  1933. ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
  1934. if (ret) {
  1935. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  1936. btrfs_info(fs_info,
  1937. "unable to update quota limit for %llu",
  1938. dstgroup->qgroupid);
  1939. goto unlock;
  1940. }
  1941. }
  1942. if (srcid) {
  1943. srcgroup = find_qgroup_rb(fs_info, srcid);
  1944. if (!srcgroup)
  1945. goto unlock;
  1946. /*
  1947. * We call inherit after we clone the root in order to make sure
  1948. * our counts don't go crazy, so at this point the only
  1949. * difference between the two roots should be the root node.
  1950. */
  1951. dstgroup->rfer = srcgroup->rfer;
  1952. dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
  1953. dstgroup->excl = level_size;
  1954. dstgroup->excl_cmpr = level_size;
  1955. srcgroup->excl = level_size;
  1956. srcgroup->excl_cmpr = level_size;
  1957. /* inherit the limit info */
  1958. dstgroup->lim_flags = srcgroup->lim_flags;
  1959. dstgroup->max_rfer = srcgroup->max_rfer;
  1960. dstgroup->max_excl = srcgroup->max_excl;
  1961. dstgroup->rsv_rfer = srcgroup->rsv_rfer;
  1962. dstgroup->rsv_excl = srcgroup->rsv_excl;
  1963. qgroup_dirty(fs_info, dstgroup);
  1964. qgroup_dirty(fs_info, srcgroup);
  1965. }
  1966. if (!inherit)
  1967. goto unlock;
  1968. i_qgroups = (u64 *)(inherit + 1);
  1969. for (i = 0; i < inherit->num_qgroups; ++i) {
  1970. if (*i_qgroups) {
  1971. ret = add_relation_rb(fs_info, objectid, *i_qgroups);
  1972. if (ret)
  1973. goto unlock;
  1974. }
  1975. ++i_qgroups;
  1976. }
  1977. for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
  1978. struct btrfs_qgroup *src;
  1979. struct btrfs_qgroup *dst;
  1980. if (!i_qgroups[0] || !i_qgroups[1])
  1981. continue;
  1982. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1983. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1984. if (!src || !dst) {
  1985. ret = -EINVAL;
  1986. goto unlock;
  1987. }
  1988. dst->rfer = src->rfer - level_size;
  1989. dst->rfer_cmpr = src->rfer_cmpr - level_size;
  1990. }
  1991. for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
  1992. struct btrfs_qgroup *src;
  1993. struct btrfs_qgroup *dst;
  1994. if (!i_qgroups[0] || !i_qgroups[1])
  1995. continue;
  1996. src = find_qgroup_rb(fs_info, i_qgroups[0]);
  1997. dst = find_qgroup_rb(fs_info, i_qgroups[1]);
  1998. if (!src || !dst) {
  1999. ret = -EINVAL;
  2000. goto unlock;
  2001. }
  2002. dst->excl = src->excl + level_size;
  2003. dst->excl_cmpr = src->excl_cmpr + level_size;
  2004. }
  2005. unlock:
  2006. spin_unlock(&fs_info->qgroup_lock);
  2007. out:
  2008. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  2009. return ret;
  2010. }
  2011. static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
  2012. {
  2013. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
  2014. qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
  2015. return false;
  2016. if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
  2017. qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
  2018. return false;
  2019. return true;
  2020. }
  2021. static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
  2022. {
  2023. struct btrfs_root *quota_root;
  2024. struct btrfs_qgroup *qgroup;
  2025. struct btrfs_fs_info *fs_info = root->fs_info;
  2026. u64 ref_root = root->root_key.objectid;
  2027. int ret = 0;
  2028. int retried = 0;
  2029. struct ulist_node *unode;
  2030. struct ulist_iterator uiter;
  2031. if (!is_fstree(ref_root))
  2032. return 0;
  2033. if (num_bytes == 0)
  2034. return 0;
  2035. if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
  2036. capable(CAP_SYS_RESOURCE))
  2037. enforce = false;
  2038. retry:
  2039. spin_lock(&fs_info->qgroup_lock);
  2040. quota_root = fs_info->quota_root;
  2041. if (!quota_root)
  2042. goto out;
  2043. qgroup = find_qgroup_rb(fs_info, ref_root);
  2044. if (!qgroup)
  2045. goto out;
  2046. /*
  2047. * in a first step, we check all affected qgroups if any limits would
  2048. * be exceeded
  2049. */
  2050. ulist_reinit(fs_info->qgroup_ulist);
  2051. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2052. (uintptr_t)qgroup, GFP_ATOMIC);
  2053. if (ret < 0)
  2054. goto out;
  2055. ULIST_ITER_INIT(&uiter);
  2056. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2057. struct btrfs_qgroup *qg;
  2058. struct btrfs_qgroup_list *glist;
  2059. qg = unode_aux_to_qgroup(unode);
  2060. if (enforce && !qgroup_check_limits(qg, num_bytes)) {
  2061. /*
  2062. * Commit the tree and retry, since we may have
  2063. * deletions which would free up space.
  2064. */
  2065. if (!retried && qg->reserved > 0) {
  2066. struct btrfs_trans_handle *trans;
  2067. spin_unlock(&fs_info->qgroup_lock);
  2068. ret = btrfs_start_delalloc_inodes(root, 0);
  2069. if (ret)
  2070. return ret;
  2071. btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
  2072. trans = btrfs_join_transaction(root);
  2073. if (IS_ERR(trans))
  2074. return PTR_ERR(trans);
  2075. ret = btrfs_commit_transaction(trans);
  2076. if (ret)
  2077. return ret;
  2078. retried++;
  2079. goto retry;
  2080. }
  2081. ret = -EDQUOT;
  2082. goto out;
  2083. }
  2084. list_for_each_entry(glist, &qg->groups, next_group) {
  2085. ret = ulist_add(fs_info->qgroup_ulist,
  2086. glist->group->qgroupid,
  2087. (uintptr_t)glist->group, GFP_ATOMIC);
  2088. if (ret < 0)
  2089. goto out;
  2090. }
  2091. }
  2092. ret = 0;
  2093. /*
  2094. * no limits exceeded, now record the reservation into all qgroups
  2095. */
  2096. ULIST_ITER_INIT(&uiter);
  2097. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2098. struct btrfs_qgroup *qg;
  2099. qg = unode_aux_to_qgroup(unode);
  2100. trace_qgroup_update_reserve(fs_info, qg, num_bytes);
  2101. qg->reserved += num_bytes;
  2102. }
  2103. out:
  2104. spin_unlock(&fs_info->qgroup_lock);
  2105. return ret;
  2106. }
  2107. void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
  2108. u64 ref_root, u64 num_bytes)
  2109. {
  2110. struct btrfs_root *quota_root;
  2111. struct btrfs_qgroup *qgroup;
  2112. struct ulist_node *unode;
  2113. struct ulist_iterator uiter;
  2114. int ret = 0;
  2115. if (!is_fstree(ref_root))
  2116. return;
  2117. if (num_bytes == 0)
  2118. return;
  2119. spin_lock(&fs_info->qgroup_lock);
  2120. quota_root = fs_info->quota_root;
  2121. if (!quota_root)
  2122. goto out;
  2123. qgroup = find_qgroup_rb(fs_info, ref_root);
  2124. if (!qgroup)
  2125. goto out;
  2126. ulist_reinit(fs_info->qgroup_ulist);
  2127. ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
  2128. (uintptr_t)qgroup, GFP_ATOMIC);
  2129. if (ret < 0)
  2130. goto out;
  2131. ULIST_ITER_INIT(&uiter);
  2132. while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
  2133. struct btrfs_qgroup *qg;
  2134. struct btrfs_qgroup_list *glist;
  2135. qg = unode_aux_to_qgroup(unode);
  2136. trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
  2137. if (qg->reserved < num_bytes)
  2138. report_reserved_underflow(fs_info, qg, num_bytes);
  2139. else
  2140. qg->reserved -= num_bytes;
  2141. list_for_each_entry(glist, &qg->groups, next_group) {
  2142. ret = ulist_add(fs_info->qgroup_ulist,
  2143. glist->group->qgroupid,
  2144. (uintptr_t)glist->group, GFP_ATOMIC);
  2145. if (ret < 0)
  2146. goto out;
  2147. }
  2148. }
  2149. out:
  2150. spin_unlock(&fs_info->qgroup_lock);
  2151. }
  2152. /*
  2153. * returns < 0 on error, 0 when more leafs are to be scanned.
  2154. * returns 1 when done.
  2155. */
  2156. static int
  2157. qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  2158. struct btrfs_trans_handle *trans)
  2159. {
  2160. struct btrfs_key found;
  2161. struct extent_buffer *scratch_leaf = NULL;
  2162. struct ulist *roots = NULL;
  2163. struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
  2164. u64 num_bytes;
  2165. int slot;
  2166. int ret;
  2167. mutex_lock(&fs_info->qgroup_rescan_lock);
  2168. ret = btrfs_search_slot_for_read(fs_info->extent_root,
  2169. &fs_info->qgroup_rescan_progress,
  2170. path, 1, 0);
  2171. btrfs_debug(fs_info,
  2172. "current progress key (%llu %u %llu), search_slot ret %d",
  2173. fs_info->qgroup_rescan_progress.objectid,
  2174. fs_info->qgroup_rescan_progress.type,
  2175. fs_info->qgroup_rescan_progress.offset, ret);
  2176. if (ret) {
  2177. /*
  2178. * The rescan is about to end, we will not be scanning any
  2179. * further blocks. We cannot unset the RESCAN flag here, because
  2180. * we want to commit the transaction if everything went well.
  2181. * To make the live accounting work in this phase, we set our
  2182. * scan progress pointer such that every real extent objectid
  2183. * will be smaller.
  2184. */
  2185. fs_info->qgroup_rescan_progress.objectid = (u64)-1;
  2186. btrfs_release_path(path);
  2187. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2188. return ret;
  2189. }
  2190. btrfs_item_key_to_cpu(path->nodes[0], &found,
  2191. btrfs_header_nritems(path->nodes[0]) - 1);
  2192. fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
  2193. btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2194. scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
  2195. if (!scratch_leaf) {
  2196. ret = -ENOMEM;
  2197. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2198. goto out;
  2199. }
  2200. extent_buffer_get(scratch_leaf);
  2201. btrfs_tree_read_lock(scratch_leaf);
  2202. btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
  2203. slot = path->slots[0];
  2204. btrfs_release_path(path);
  2205. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2206. for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
  2207. btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
  2208. if (found.type != BTRFS_EXTENT_ITEM_KEY &&
  2209. found.type != BTRFS_METADATA_ITEM_KEY)
  2210. continue;
  2211. if (found.type == BTRFS_METADATA_ITEM_KEY)
  2212. num_bytes = fs_info->nodesize;
  2213. else
  2214. num_bytes = found.offset;
  2215. ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
  2216. &roots, false);
  2217. if (ret < 0)
  2218. goto out;
  2219. /* For rescan, just pass old_roots as NULL */
  2220. ret = btrfs_qgroup_account_extent(trans, fs_info,
  2221. found.objectid, num_bytes, NULL, roots);
  2222. if (ret < 0)
  2223. goto out;
  2224. }
  2225. out:
  2226. if (scratch_leaf) {
  2227. btrfs_tree_read_unlock_blocking(scratch_leaf);
  2228. free_extent_buffer(scratch_leaf);
  2229. }
  2230. btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
  2231. return ret;
  2232. }
  2233. static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
  2234. {
  2235. struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
  2236. qgroup_rescan_work);
  2237. struct btrfs_path *path;
  2238. struct btrfs_trans_handle *trans = NULL;
  2239. int err = -ENOMEM;
  2240. int ret = 0;
  2241. path = btrfs_alloc_path();
  2242. if (!path)
  2243. goto out;
  2244. err = 0;
  2245. while (!err && !btrfs_fs_closing(fs_info)) {
  2246. trans = btrfs_start_transaction(fs_info->fs_root, 0);
  2247. if (IS_ERR(trans)) {
  2248. err = PTR_ERR(trans);
  2249. break;
  2250. }
  2251. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
  2252. err = -EINTR;
  2253. } else {
  2254. err = qgroup_rescan_leaf(fs_info, path, trans);
  2255. }
  2256. if (err > 0)
  2257. btrfs_commit_transaction(trans);
  2258. else
  2259. btrfs_end_transaction(trans);
  2260. }
  2261. out:
  2262. btrfs_free_path(path);
  2263. mutex_lock(&fs_info->qgroup_rescan_lock);
  2264. if (!btrfs_fs_closing(fs_info))
  2265. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2266. if (err > 0 &&
  2267. fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
  2268. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2269. } else if (err < 0) {
  2270. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
  2271. }
  2272. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2273. /*
  2274. * only update status, since the previous part has already updated the
  2275. * qgroup info.
  2276. */
  2277. trans = btrfs_start_transaction(fs_info->quota_root, 1);
  2278. if (IS_ERR(trans)) {
  2279. err = PTR_ERR(trans);
  2280. btrfs_err(fs_info,
  2281. "fail to start transaction for status update: %d",
  2282. err);
  2283. goto done;
  2284. }
  2285. ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
  2286. if (ret < 0) {
  2287. err = ret;
  2288. btrfs_err(fs_info, "fail to update qgroup status: %d", err);
  2289. }
  2290. btrfs_end_transaction(trans);
  2291. if (btrfs_fs_closing(fs_info)) {
  2292. btrfs_info(fs_info, "qgroup scan paused");
  2293. } else if (err >= 0) {
  2294. btrfs_info(fs_info, "qgroup scan completed%s",
  2295. err > 0 ? " (inconsistency flag cleared)" : "");
  2296. } else {
  2297. btrfs_err(fs_info, "qgroup scan failed with %d", err);
  2298. }
  2299. done:
  2300. mutex_lock(&fs_info->qgroup_rescan_lock);
  2301. fs_info->qgroup_rescan_running = false;
  2302. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2303. complete_all(&fs_info->qgroup_rescan_completion);
  2304. }
  2305. /*
  2306. * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
  2307. * memory required for the rescan context.
  2308. */
  2309. static int
  2310. qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
  2311. int init_flags)
  2312. {
  2313. int ret = 0;
  2314. if (!init_flags &&
  2315. (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
  2316. !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
  2317. ret = -EINVAL;
  2318. goto err;
  2319. }
  2320. mutex_lock(&fs_info->qgroup_rescan_lock);
  2321. spin_lock(&fs_info->qgroup_lock);
  2322. if (init_flags) {
  2323. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2324. ret = -EINPROGRESS;
  2325. else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
  2326. ret = -EINVAL;
  2327. if (ret) {
  2328. spin_unlock(&fs_info->qgroup_lock);
  2329. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2330. goto err;
  2331. }
  2332. fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2333. }
  2334. memset(&fs_info->qgroup_rescan_progress, 0,
  2335. sizeof(fs_info->qgroup_rescan_progress));
  2336. fs_info->qgroup_rescan_progress.objectid = progress_objectid;
  2337. init_completion(&fs_info->qgroup_rescan_completion);
  2338. fs_info->qgroup_rescan_running = true;
  2339. spin_unlock(&fs_info->qgroup_lock);
  2340. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2341. memset(&fs_info->qgroup_rescan_work, 0,
  2342. sizeof(fs_info->qgroup_rescan_work));
  2343. btrfs_init_work(&fs_info->qgroup_rescan_work,
  2344. btrfs_qgroup_rescan_helper,
  2345. btrfs_qgroup_rescan_worker, NULL, NULL);
  2346. if (ret) {
  2347. err:
  2348. btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
  2349. return ret;
  2350. }
  2351. return 0;
  2352. }
  2353. static void
  2354. qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
  2355. {
  2356. struct rb_node *n;
  2357. struct btrfs_qgroup *qgroup;
  2358. spin_lock(&fs_info->qgroup_lock);
  2359. /* clear all current qgroup tracking information */
  2360. for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
  2361. qgroup = rb_entry(n, struct btrfs_qgroup, node);
  2362. qgroup->rfer = 0;
  2363. qgroup->rfer_cmpr = 0;
  2364. qgroup->excl = 0;
  2365. qgroup->excl_cmpr = 0;
  2366. }
  2367. spin_unlock(&fs_info->qgroup_lock);
  2368. }
  2369. int
  2370. btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
  2371. {
  2372. int ret = 0;
  2373. struct btrfs_trans_handle *trans;
  2374. ret = qgroup_rescan_init(fs_info, 0, 1);
  2375. if (ret)
  2376. return ret;
  2377. /*
  2378. * We have set the rescan_progress to 0, which means no more
  2379. * delayed refs will be accounted by btrfs_qgroup_account_ref.
  2380. * However, btrfs_qgroup_account_ref may be right after its call
  2381. * to btrfs_find_all_roots, in which case it would still do the
  2382. * accounting.
  2383. * To solve this, we're committing the transaction, which will
  2384. * ensure we run all delayed refs and only after that, we are
  2385. * going to clear all tracking information for a clean start.
  2386. */
  2387. trans = btrfs_join_transaction(fs_info->fs_root);
  2388. if (IS_ERR(trans)) {
  2389. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2390. return PTR_ERR(trans);
  2391. }
  2392. ret = btrfs_commit_transaction(trans);
  2393. if (ret) {
  2394. fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
  2395. return ret;
  2396. }
  2397. qgroup_rescan_zero_tracking(fs_info);
  2398. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2399. &fs_info->qgroup_rescan_work);
  2400. return 0;
  2401. }
  2402. int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
  2403. bool interruptible)
  2404. {
  2405. int running;
  2406. int ret = 0;
  2407. mutex_lock(&fs_info->qgroup_rescan_lock);
  2408. spin_lock(&fs_info->qgroup_lock);
  2409. running = fs_info->qgroup_rescan_running;
  2410. spin_unlock(&fs_info->qgroup_lock);
  2411. mutex_unlock(&fs_info->qgroup_rescan_lock);
  2412. if (!running)
  2413. return 0;
  2414. if (interruptible)
  2415. ret = wait_for_completion_interruptible(
  2416. &fs_info->qgroup_rescan_completion);
  2417. else
  2418. wait_for_completion(&fs_info->qgroup_rescan_completion);
  2419. return ret;
  2420. }
  2421. /*
  2422. * this is only called from open_ctree where we're still single threaded, thus
  2423. * locking is omitted here.
  2424. */
  2425. void
  2426. btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
  2427. {
  2428. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
  2429. btrfs_queue_work(fs_info->qgroup_rescan_workers,
  2430. &fs_info->qgroup_rescan_work);
  2431. }
  2432. /*
  2433. * Reserve qgroup space for range [start, start + len).
  2434. *
  2435. * This function will either reserve space from related qgroups or doing
  2436. * nothing if the range is already reserved.
  2437. *
  2438. * Return 0 for successful reserve
  2439. * Return <0 for error (including -EQUOT)
  2440. *
  2441. * NOTE: this function may sleep for memory allocation.
  2442. * if btrfs_qgroup_reserve_data() is called multiple times with
  2443. * same @reserved, caller must ensure when error happens it's OK
  2444. * to free *ALL* reserved space.
  2445. */
  2446. int btrfs_qgroup_reserve_data(struct inode *inode,
  2447. struct extent_changeset **reserved_ret, u64 start,
  2448. u64 len)
  2449. {
  2450. struct btrfs_root *root = BTRFS_I(inode)->root;
  2451. struct ulist_node *unode;
  2452. struct ulist_iterator uiter;
  2453. struct extent_changeset *reserved;
  2454. u64 orig_reserved;
  2455. u64 to_reserve;
  2456. int ret;
  2457. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
  2458. !is_fstree(root->objectid) || len == 0)
  2459. return 0;
  2460. /* @reserved parameter is mandatory for qgroup */
  2461. if (WARN_ON(!reserved_ret))
  2462. return -EINVAL;
  2463. if (!*reserved_ret) {
  2464. *reserved_ret = extent_changeset_alloc();
  2465. if (!*reserved_ret)
  2466. return -ENOMEM;
  2467. }
  2468. reserved = *reserved_ret;
  2469. /* Record already reserved space */
  2470. orig_reserved = reserved->bytes_changed;
  2471. ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2472. start + len -1, EXTENT_QGROUP_RESERVED, reserved);
  2473. /* Newly reserved space */
  2474. to_reserve = reserved->bytes_changed - orig_reserved;
  2475. trace_btrfs_qgroup_reserve_data(inode, start, len,
  2476. to_reserve, QGROUP_RESERVE);
  2477. if (ret < 0)
  2478. goto cleanup;
  2479. ret = qgroup_reserve(root, to_reserve, true);
  2480. if (ret < 0)
  2481. goto cleanup;
  2482. return ret;
  2483. cleanup:
  2484. /* cleanup *ALL* already reserved ranges */
  2485. ULIST_ITER_INIT(&uiter);
  2486. while ((unode = ulist_next(&reserved->range_changed, &uiter)))
  2487. clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
  2488. unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
  2489. GFP_NOFS);
  2490. extent_changeset_release(reserved);
  2491. return ret;
  2492. }
  2493. /* Free ranges specified by @reserved, normally in error path */
  2494. static int qgroup_free_reserved_data(struct inode *inode,
  2495. struct extent_changeset *reserved, u64 start, u64 len)
  2496. {
  2497. struct btrfs_root *root = BTRFS_I(inode)->root;
  2498. struct ulist_node *unode;
  2499. struct ulist_iterator uiter;
  2500. struct extent_changeset changeset;
  2501. int freed = 0;
  2502. int ret;
  2503. extent_changeset_init(&changeset);
  2504. len = round_up(start + len, root->fs_info->sectorsize);
  2505. start = round_down(start, root->fs_info->sectorsize);
  2506. ULIST_ITER_INIT(&uiter);
  2507. while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
  2508. u64 range_start = unode->val;
  2509. /* unode->aux is the inclusive end */
  2510. u64 range_len = unode->aux - range_start + 1;
  2511. u64 free_start;
  2512. u64 free_len;
  2513. extent_changeset_release(&changeset);
  2514. /* Only free range in range [start, start + len) */
  2515. if (range_start >= start + len ||
  2516. range_start + range_len <= start)
  2517. continue;
  2518. free_start = max(range_start, start);
  2519. free_len = min(start + len, range_start + range_len) -
  2520. free_start;
  2521. /*
  2522. * TODO: To also modify reserved->ranges_reserved to reflect
  2523. * the modification.
  2524. *
  2525. * However as long as we free qgroup reserved according to
  2526. * EXTENT_QGROUP_RESERVED, we won't double free.
  2527. * So not need to rush.
  2528. */
  2529. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  2530. free_start, free_start + free_len - 1,
  2531. EXTENT_QGROUP_RESERVED, &changeset);
  2532. if (ret < 0)
  2533. goto out;
  2534. freed += changeset.bytes_changed;
  2535. }
  2536. btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed);
  2537. ret = freed;
  2538. out:
  2539. extent_changeset_release(&changeset);
  2540. return ret;
  2541. }
  2542. static int __btrfs_qgroup_release_data(struct inode *inode,
  2543. struct extent_changeset *reserved, u64 start, u64 len,
  2544. int free)
  2545. {
  2546. struct extent_changeset changeset;
  2547. int trace_op = QGROUP_RELEASE;
  2548. int ret;
  2549. /* In release case, we shouldn't have @reserved */
  2550. WARN_ON(!free && reserved);
  2551. if (free && reserved)
  2552. return qgroup_free_reserved_data(inode, reserved, start, len);
  2553. extent_changeset_init(&changeset);
  2554. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
  2555. start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
  2556. if (ret < 0)
  2557. goto out;
  2558. if (free)
  2559. trace_op = QGROUP_FREE;
  2560. trace_btrfs_qgroup_release_data(inode, start, len,
  2561. changeset.bytes_changed, trace_op);
  2562. if (free)
  2563. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2564. BTRFS_I(inode)->root->objectid,
  2565. changeset.bytes_changed);
  2566. ret = changeset.bytes_changed;
  2567. out:
  2568. extent_changeset_release(&changeset);
  2569. return ret;
  2570. }
  2571. /*
  2572. * Free a reserved space range from io_tree and related qgroups
  2573. *
  2574. * Should be called when a range of pages get invalidated before reaching disk.
  2575. * Or for error cleanup case.
  2576. * if @reserved is given, only reserved range in [@start, @start + @len) will
  2577. * be freed.
  2578. *
  2579. * For data written to disk, use btrfs_qgroup_release_data().
  2580. *
  2581. * NOTE: This function may sleep for memory allocation.
  2582. */
  2583. int btrfs_qgroup_free_data(struct inode *inode,
  2584. struct extent_changeset *reserved, u64 start, u64 len)
  2585. {
  2586. return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
  2587. }
  2588. /*
  2589. * Release a reserved space range from io_tree only.
  2590. *
  2591. * Should be called when a range of pages get written to disk and corresponding
  2592. * FILE_EXTENT is inserted into corresponding root.
  2593. *
  2594. * Since new qgroup accounting framework will only update qgroup numbers at
  2595. * commit_transaction() time, its reserved space shouldn't be freed from
  2596. * related qgroups.
  2597. *
  2598. * But we should release the range from io_tree, to allow further write to be
  2599. * COWed.
  2600. *
  2601. * NOTE: This function may sleep for memory allocation.
  2602. */
  2603. int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
  2604. {
  2605. return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
  2606. }
  2607. int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
  2608. bool enforce)
  2609. {
  2610. struct btrfs_fs_info *fs_info = root->fs_info;
  2611. int ret;
  2612. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2613. !is_fstree(root->objectid) || num_bytes == 0)
  2614. return 0;
  2615. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2616. trace_qgroup_meta_reserve(root, (s64)num_bytes);
  2617. ret = qgroup_reserve(root, num_bytes, enforce);
  2618. if (ret < 0)
  2619. return ret;
  2620. atomic64_add(num_bytes, &root->qgroup_meta_rsv);
  2621. return ret;
  2622. }
  2623. void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
  2624. {
  2625. struct btrfs_fs_info *fs_info = root->fs_info;
  2626. u64 reserved;
  2627. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2628. !is_fstree(root->objectid))
  2629. return;
  2630. reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
  2631. if (reserved == 0)
  2632. return;
  2633. trace_qgroup_meta_reserve(root, -(s64)reserved);
  2634. btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
  2635. }
  2636. void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
  2637. {
  2638. struct btrfs_fs_info *fs_info = root->fs_info;
  2639. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
  2640. !is_fstree(root->objectid))
  2641. return;
  2642. BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
  2643. WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
  2644. atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
  2645. trace_qgroup_meta_reserve(root, -(s64)num_bytes);
  2646. btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
  2647. }
  2648. /*
  2649. * Check qgroup reserved space leaking, normally at destroy inode
  2650. * time
  2651. */
  2652. void btrfs_qgroup_check_reserved_leak(struct inode *inode)
  2653. {
  2654. struct extent_changeset changeset;
  2655. struct ulist_node *unode;
  2656. struct ulist_iterator iter;
  2657. int ret;
  2658. extent_changeset_init(&changeset);
  2659. ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
  2660. EXTENT_QGROUP_RESERVED, &changeset);
  2661. WARN_ON(ret < 0);
  2662. if (WARN_ON(changeset.bytes_changed)) {
  2663. ULIST_ITER_INIT(&iter);
  2664. while ((unode = ulist_next(&changeset.range_changed, &iter))) {
  2665. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2666. "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
  2667. inode->i_ino, unode->val, unode->aux);
  2668. }
  2669. btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
  2670. BTRFS_I(inode)->root->objectid,
  2671. changeset.bytes_changed);
  2672. }
  2673. extent_changeset_release(&changeset);
  2674. }