qgroup.c 86 KB

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