qgroup.c 85 KB

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