]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/quota.c
Update bcachefs sources to bca25b802d fixup! bcachefs: Fix bch2_check_discard_freespa...
[bcachefs-tools-debian] / libbcachefs / quota.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "btree_update.h"
4 #include "errcode.h"
5 #include "error.h"
6 #include "inode.h"
7 #include "quota.h"
8 #include "subvolume.h"
9 #include "super-io.h"
10
11 static const char * const bch2_quota_types[] = {
12         "user",
13         "group",
14         "project",
15 };
16
17 static const char * const bch2_quota_counters[] = {
18         "space",
19         "inodes",
20 };
21
22 static int bch2_sb_quota_validate(struct bch_sb *sb, struct bch_sb_field *f,
23                                   struct printbuf *err)
24 {
25         struct bch_sb_field_quota *q = field_to_type(f, quota);
26
27         if (vstruct_bytes(&q->field) < sizeof(*q)) {
28                 prt_printf(err, "wrong size (got %zu should be %zu)",
29                        vstruct_bytes(&q->field), sizeof(*q));
30                 return -BCH_ERR_invalid_sb_quota;
31         }
32
33         return 0;
34 }
35
36 static void bch2_sb_quota_to_text(struct printbuf *out, struct bch_sb *sb,
37                                   struct bch_sb_field *f)
38 {
39         struct bch_sb_field_quota *q = field_to_type(f, quota);
40         unsigned qtyp, counter;
41
42         for (qtyp = 0; qtyp < ARRAY_SIZE(q->q); qtyp++) {
43                 prt_printf(out, "%s: flags %llx",
44                        bch2_quota_types[qtyp],
45                        le64_to_cpu(q->q[qtyp].flags));
46
47                 for (counter = 0; counter < Q_COUNTERS; counter++)
48                         prt_printf(out, " %s timelimit %u warnlimit %u",
49                                bch2_quota_counters[counter],
50                                le32_to_cpu(q->q[qtyp].c[counter].timelimit),
51                                le32_to_cpu(q->q[qtyp].c[counter].warnlimit));
52
53                 prt_newline(out);
54         }
55 }
56
57 const struct bch_sb_field_ops bch_sb_field_ops_quota = {
58         .validate       = bch2_sb_quota_validate,
59         .to_text        = bch2_sb_quota_to_text,
60 };
61
62 int bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k,
63                        unsigned flags, struct printbuf *err)
64 {
65         if (k.k->p.inode >= QTYP_NR) {
66                 prt_printf(err, "invalid quota type (%llu >= %u)",
67                        k.k->p.inode, QTYP_NR);
68                 return -BCH_ERR_invalid_bkey;
69         }
70
71         return 0;
72 }
73
74 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
75                         struct bkey_s_c k)
76 {
77         struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
78         unsigned i;
79
80         for (i = 0; i < Q_COUNTERS; i++)
81                 prt_printf(out, "%s hardlimit %llu softlimit %llu",
82                        bch2_quota_counters[i],
83                        le64_to_cpu(dq.v->c[i].hardlimit),
84                        le64_to_cpu(dq.v->c[i].softlimit));
85 }
86
87 #ifdef CONFIG_BCACHEFS_QUOTA
88
89 #include <linux/cred.h>
90 #include <linux/fs.h>
91 #include <linux/quota.h>
92
93 static void qc_info_to_text(struct printbuf *out, struct qc_info *i)
94 {
95         printbuf_tabstops_reset(out);
96         printbuf_tabstop_push(out, 20);
97
98         prt_str(out, "i_fieldmask");
99         prt_tab(out);
100         prt_printf(out, "%x", i->i_fieldmask);
101         prt_newline(out);
102
103         prt_str(out, "i_flags");
104         prt_tab(out);
105         prt_printf(out, "%u", i->i_flags);
106         prt_newline(out);
107
108         prt_str(out, "i_spc_timelimit");
109         prt_tab(out);
110         prt_printf(out, "%u", i->i_spc_timelimit);
111         prt_newline(out);
112
113         prt_str(out, "i_ino_timelimit");
114         prt_tab(out);
115         prt_printf(out, "%u", i->i_ino_timelimit);
116         prt_newline(out);
117
118         prt_str(out, "i_rt_spc_timelimit");
119         prt_tab(out);
120         prt_printf(out, "%u", i->i_rt_spc_timelimit);
121         prt_newline(out);
122
123         prt_str(out, "i_spc_warnlimit");
124         prt_tab(out);
125         prt_printf(out, "%u", i->i_spc_warnlimit);
126         prt_newline(out);
127
128         prt_str(out, "i_ino_warnlimit");
129         prt_tab(out);
130         prt_printf(out, "%u", i->i_ino_warnlimit);
131         prt_newline(out);
132
133         prt_str(out, "i_rt_spc_warnlimit");
134         prt_tab(out);
135         prt_printf(out, "%u", i->i_rt_spc_warnlimit);
136         prt_newline(out);
137 }
138
139 static void qc_dqblk_to_text(struct printbuf *out, struct qc_dqblk *q)
140 {
141         printbuf_tabstops_reset(out);
142         printbuf_tabstop_push(out, 20);
143
144         prt_str(out, "d_fieldmask");
145         prt_tab(out);
146         prt_printf(out, "%x", q->d_fieldmask);
147         prt_newline(out);
148
149         prt_str(out, "d_spc_hardlimit");
150         prt_tab(out);
151         prt_printf(out, "%llu", q->d_spc_hardlimit);
152         prt_newline(out);
153
154         prt_str(out, "d_spc_softlimit");
155         prt_tab(out);
156         prt_printf(out, "%llu", q->d_spc_softlimit);
157         prt_newline(out);
158
159         prt_str(out, "d_ino_hardlimit");
160         prt_tab(out);
161         prt_printf(out, "%llu", q->d_ino_hardlimit);
162         prt_newline(out);
163
164         prt_str(out, "d_ino_softlimit");
165         prt_tab(out);
166         prt_printf(out, "%llu", q->d_ino_softlimit);
167         prt_newline(out);
168
169         prt_str(out, "d_space");
170         prt_tab(out);
171         prt_printf(out, "%llu", q->d_space);
172         prt_newline(out);
173
174         prt_str(out, "d_ino_count");
175         prt_tab(out);
176         prt_printf(out, "%llu", q->d_ino_count);
177         prt_newline(out);
178
179         prt_str(out, "d_ino_timer");
180         prt_tab(out);
181         prt_printf(out, "%llu", q->d_ino_timer);
182         prt_newline(out);
183
184         prt_str(out, "d_spc_timer");
185         prt_tab(out);
186         prt_printf(out, "%llu", q->d_spc_timer);
187         prt_newline(out);
188
189         prt_str(out, "d_ino_warns");
190         prt_tab(out);
191         prt_printf(out, "%i", q->d_ino_warns);
192         prt_newline(out);
193
194         prt_str(out, "d_spc_warns");
195         prt_tab(out);
196         prt_printf(out, "%i", q->d_spc_warns);
197         prt_newline(out);
198 }
199
200 static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
201 {
202         qtypes >>= i;
203         return qtypes ? i + __ffs(qtypes) : QTYP_NR;
204 }
205
206 #define for_each_set_qtype(_c, _i, _q, _qtypes)                         \
207         for (_i = 0;                                                    \
208              (_i = __next_qtype(_i, _qtypes),                           \
209               _q = &(_c)->quotas[_i],                                   \
210               _i < QTYP_NR);                                            \
211              _i++)
212
213 static bool ignore_hardlimit(struct bch_memquota_type *q)
214 {
215         if (capable(CAP_SYS_RESOURCE))
216                 return true;
217 #if 0
218         struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
219
220         return capable(CAP_SYS_RESOURCE) &&
221                (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
222                 !(info->dqi_flags & DQF_ROOT_SQUASH));
223 #endif
224         return false;
225 }
226
227 enum quota_msg {
228         SOFTWARN,       /* Softlimit reached */
229         SOFTLONGWARN,   /* Grace time expired */
230         HARDWARN,       /* Hardlimit reached */
231
232         HARDBELOW,      /* Usage got below inode hardlimit */
233         SOFTBELOW,      /* Usage got below inode softlimit */
234 };
235
236 static int quota_nl[][Q_COUNTERS] = {
237         [HARDWARN][Q_SPC]       = QUOTA_NL_BHARDWARN,
238         [SOFTLONGWARN][Q_SPC]   = QUOTA_NL_BSOFTLONGWARN,
239         [SOFTWARN][Q_SPC]       = QUOTA_NL_BSOFTWARN,
240         [HARDBELOW][Q_SPC]      = QUOTA_NL_BHARDBELOW,
241         [SOFTBELOW][Q_SPC]      = QUOTA_NL_BSOFTBELOW,
242
243         [HARDWARN][Q_INO]       = QUOTA_NL_IHARDWARN,
244         [SOFTLONGWARN][Q_INO]   = QUOTA_NL_ISOFTLONGWARN,
245         [SOFTWARN][Q_INO]       = QUOTA_NL_ISOFTWARN,
246         [HARDBELOW][Q_INO]      = QUOTA_NL_IHARDBELOW,
247         [SOFTBELOW][Q_INO]      = QUOTA_NL_ISOFTBELOW,
248 };
249
250 struct quota_msgs {
251         u8              nr;
252         struct {
253                 u8      qtype;
254                 u8      msg;
255         }               m[QTYP_NR * Q_COUNTERS];
256 };
257
258 static void prepare_msg(unsigned qtype,
259                         enum quota_counters counter,
260                         struct quota_msgs *msgs,
261                         enum quota_msg msg_type)
262 {
263         BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
264
265         msgs->m[msgs->nr].qtype = qtype;
266         msgs->m[msgs->nr].msg   = quota_nl[msg_type][counter];
267         msgs->nr++;
268 }
269
270 static void prepare_warning(struct memquota_counter *qc,
271                             unsigned qtype,
272                             enum quota_counters counter,
273                             struct quota_msgs *msgs,
274                             enum quota_msg msg_type)
275 {
276         if (qc->warning_issued & (1 << msg_type))
277                 return;
278
279         prepare_msg(qtype, counter, msgs, msg_type);
280 }
281
282 static void flush_warnings(struct bch_qid qid,
283                            struct super_block *sb,
284                            struct quota_msgs *msgs)
285 {
286         unsigned i;
287
288         for (i = 0; i < msgs->nr; i++)
289                 quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
290                                    sb->s_dev, msgs->m[i].msg);
291 }
292
293 static int bch2_quota_check_limit(struct bch_fs *c,
294                                   unsigned qtype,
295                                   struct bch_memquota *mq,
296                                   struct quota_msgs *msgs,
297                                   enum quota_counters counter,
298                                   s64 v,
299                                   enum quota_acct_mode mode)
300 {
301         struct bch_memquota_type *q = &c->quotas[qtype];
302         struct memquota_counter *qc = &mq->c[counter];
303         u64 n = qc->v + v;
304
305         BUG_ON((s64) n < 0);
306
307         if (mode == KEY_TYPE_QUOTA_NOCHECK)
308                 return 0;
309
310         if (v <= 0) {
311                 if (n < qc->hardlimit &&
312                     (qc->warning_issued & (1 << HARDWARN))) {
313                         qc->warning_issued &= ~(1 << HARDWARN);
314                         prepare_msg(qtype, counter, msgs, HARDBELOW);
315                 }
316
317                 if (n < qc->softlimit &&
318                     (qc->warning_issued & (1 << SOFTWARN))) {
319                         qc->warning_issued &= ~(1 << SOFTWARN);
320                         prepare_msg(qtype, counter, msgs, SOFTBELOW);
321                 }
322
323                 qc->warning_issued = 0;
324                 return 0;
325         }
326
327         if (qc->hardlimit &&
328             qc->hardlimit < n &&
329             !ignore_hardlimit(q)) {
330                 prepare_warning(qc, qtype, counter, msgs, HARDWARN);
331                 return -EDQUOT;
332         }
333
334         if (qc->softlimit &&
335             qc->softlimit < n) {
336                 if (qc->timer == 0) {
337                         qc->timer = ktime_get_real_seconds() + q->limits[counter].timelimit;
338                         prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
339                 } else if (ktime_get_real_seconds() >= qc->timer &&
340                            !ignore_hardlimit(q)) {
341                         prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
342                         return -EDQUOT;
343                 }
344         }
345
346         return 0;
347 }
348
349 int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
350                     enum quota_counters counter, s64 v,
351                     enum quota_acct_mode mode)
352 {
353         unsigned qtypes = enabled_qtypes(c);
354         struct bch_memquota_type *q;
355         struct bch_memquota *mq[QTYP_NR];
356         struct quota_msgs msgs;
357         unsigned i;
358         int ret = 0;
359
360         memset(&msgs, 0, sizeof(msgs));
361
362         for_each_set_qtype(c, i, q, qtypes) {
363                 mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_KERNEL);
364                 if (!mq[i])
365                         return -ENOMEM;
366         }
367
368         for_each_set_qtype(c, i, q, qtypes)
369                 mutex_lock_nested(&q->lock, i);
370
371         for_each_set_qtype(c, i, q, qtypes) {
372                 ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
373                 if (ret)
374                         goto err;
375         }
376
377         for_each_set_qtype(c, i, q, qtypes)
378                 mq[i]->c[counter].v += v;
379 err:
380         for_each_set_qtype(c, i, q, qtypes)
381                 mutex_unlock(&q->lock);
382
383         flush_warnings(qid, c->vfs_sb, &msgs);
384
385         return ret;
386 }
387
388 static void __bch2_quota_transfer(struct bch_memquota *src_q,
389                                   struct bch_memquota *dst_q,
390                                   enum quota_counters counter, s64 v)
391 {
392         BUG_ON(v > src_q->c[counter].v);
393         BUG_ON(v + dst_q->c[counter].v < v);
394
395         src_q->c[counter].v -= v;
396         dst_q->c[counter].v += v;
397 }
398
399 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
400                         struct bch_qid dst,
401                         struct bch_qid src, u64 space,
402                         enum quota_acct_mode mode)
403 {
404         struct bch_memquota_type *q;
405         struct bch_memquota *src_q[3], *dst_q[3];
406         struct quota_msgs msgs;
407         unsigned i;
408         int ret = 0;
409
410         qtypes &= enabled_qtypes(c);
411
412         memset(&msgs, 0, sizeof(msgs));
413
414         for_each_set_qtype(c, i, q, qtypes) {
415                 src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_KERNEL);
416                 dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_KERNEL);
417                 if (!src_q[i] || !dst_q[i])
418                         return -ENOMEM;
419         }
420
421         for_each_set_qtype(c, i, q, qtypes)
422                 mutex_lock_nested(&q->lock, i);
423
424         for_each_set_qtype(c, i, q, qtypes) {
425                 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
426                                              dst_q[i]->c[Q_SPC].v + space,
427                                              mode);
428                 if (ret)
429                         goto err;
430
431                 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
432                                              dst_q[i]->c[Q_INO].v + 1,
433                                              mode);
434                 if (ret)
435                         goto err;
436         }
437
438         for_each_set_qtype(c, i, q, qtypes) {
439                 __bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
440                 __bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
441         }
442
443 err:
444         for_each_set_qtype(c, i, q, qtypes)
445                 mutex_unlock(&q->lock);
446
447         flush_warnings(dst, c->vfs_sb, &msgs);
448
449         return ret;
450 }
451
452 static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k,
453                             struct qc_dqblk *qdq)
454 {
455         struct bkey_s_c_quota dq;
456         struct bch_memquota_type *q;
457         struct bch_memquota *mq;
458         unsigned i;
459
460         BUG_ON(k.k->p.inode >= QTYP_NR);
461
462         if (!((1U << k.k->p.inode) & enabled_qtypes(c)))
463                 return 0;
464
465         switch (k.k->type) {
466         case KEY_TYPE_quota:
467                 dq = bkey_s_c_to_quota(k);
468                 q = &c->quotas[k.k->p.inode];
469
470                 mutex_lock(&q->lock);
471                 mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
472                 if (!mq) {
473                         mutex_unlock(&q->lock);
474                         return -ENOMEM;
475                 }
476
477                 for (i = 0; i < Q_COUNTERS; i++) {
478                         mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
479                         mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
480                 }
481
482                 if (qdq && qdq->d_fieldmask & QC_SPC_TIMER)
483                         mq->c[Q_SPC].timer      = cpu_to_le64(qdq->d_spc_timer);
484                 if (qdq && qdq->d_fieldmask & QC_SPC_WARNS)
485                         mq->c[Q_SPC].warns      = cpu_to_le64(qdq->d_spc_warns);
486                 if (qdq && qdq->d_fieldmask & QC_INO_TIMER)
487                         mq->c[Q_INO].timer      = cpu_to_le64(qdq->d_ino_timer);
488                 if (qdq && qdq->d_fieldmask & QC_INO_WARNS)
489                         mq->c[Q_INO].warns      = cpu_to_le64(qdq->d_ino_warns);
490
491                 mutex_unlock(&q->lock);
492         }
493
494         return 0;
495 }
496
497 void bch2_fs_quota_exit(struct bch_fs *c)
498 {
499         unsigned i;
500
501         for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
502                 genradix_free(&c->quotas[i].table);
503 }
504
505 void bch2_fs_quota_init(struct bch_fs *c)
506 {
507         unsigned i;
508
509         for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
510                 mutex_init(&c->quotas[i].lock);
511 }
512
513 static struct bch_sb_field_quota *bch2_sb_get_or_create_quota(struct bch_sb_handle *sb)
514 {
515         struct bch_sb_field_quota *sb_quota = bch2_sb_get_quota(sb->sb);
516
517         if (sb_quota)
518                 return sb_quota;
519
520         sb_quota = bch2_sb_resize_quota(sb, sizeof(*sb_quota) / sizeof(u64));
521         if (sb_quota) {
522                 unsigned qtype, qc;
523
524                 for (qtype = 0; qtype < QTYP_NR; qtype++)
525                         for (qc = 0; qc < Q_COUNTERS; qc++)
526                                 sb_quota->q[qtype].c[qc].timelimit =
527                                         cpu_to_le32(7 * 24 * 60 * 60);
528         }
529
530         return sb_quota;
531 }
532
533 static void bch2_sb_quota_read(struct bch_fs *c)
534 {
535         struct bch_sb_field_quota *sb_quota;
536         unsigned i, j;
537
538         sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
539         if (!sb_quota)
540                 return;
541
542         for (i = 0; i < QTYP_NR; i++) {
543                 struct bch_memquota_type *q = &c->quotas[i];
544
545                 for (j = 0; j < Q_COUNTERS; j++) {
546                         q->limits[j].timelimit =
547                                 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
548                         q->limits[j].warnlimit =
549                                 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
550                 }
551         }
552 }
553
554 static int bch2_fs_quota_read_inode(struct btree_trans *trans,
555                                     struct btree_iter *iter,
556                                     struct bkey_s_c k)
557 {
558         struct bch_fs *c = trans->c;
559         struct bch_inode_unpacked u;
560         struct bch_snapshot_tree s_t;
561         int ret;
562
563         ret = bch2_snapshot_tree_lookup(trans,
564                         snapshot_t(c, k.k->p.snapshot)->tree, &s_t);
565         bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
566                         "%s: snapshot tree %u not found", __func__,
567                         snapshot_t(c, k.k->p.snapshot)->tree);
568         if (ret)
569                 return ret;
570
571         if (!s_t.master_subvol)
572                 goto advance;
573
574         ret = bch2_inode_find_by_inum_trans(trans,
575                                 (subvol_inum) {
576                                         le32_to_cpu(s_t.master_subvol),
577                                         k.k->p.offset,
578                                 }, &u);
579         /*
580          * Inode might be deleted in this snapshot - the easiest way to handle
581          * that is to just skip it here:
582          */
583         if (bch2_err_matches(ret, ENOENT))
584                 goto advance;
585
586         if (ret)
587                 return ret;
588
589         bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
590                         KEY_TYPE_QUOTA_NOCHECK);
591         bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
592                         KEY_TYPE_QUOTA_NOCHECK);
593 advance:
594         bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
595         return 0;
596 }
597
598 int bch2_fs_quota_read(struct bch_fs *c)
599 {
600         struct bch_sb_field_quota *sb_quota;
601         struct btree_trans trans;
602         struct btree_iter iter;
603         struct bkey_s_c k;
604         int ret;
605
606         mutex_lock(&c->sb_lock);
607         sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
608         if (!sb_quota) {
609                 mutex_unlock(&c->sb_lock);
610                 return -BCH_ERR_ENOSPC_sb_quota;
611         }
612
613         bch2_sb_quota_read(c);
614         mutex_unlock(&c->sb_lock);
615
616         bch2_trans_init(&trans, c, 0, 0);
617
618         ret = for_each_btree_key2(&trans, iter, BTREE_ID_quotas,
619                         POS_MIN, BTREE_ITER_PREFETCH, k,
620                 __bch2_quota_set(c, k, NULL)) ?:
621               for_each_btree_key2(&trans, iter, BTREE_ID_inodes,
622                         POS_MIN, BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
623                 bch2_fs_quota_read_inode(&trans, &iter, k));
624         if (ret)
625                 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
626
627         bch2_trans_exit(&trans);
628         return ret;
629 }
630
631 /* Enable/disable/delete quotas for an entire filesystem: */
632
633 static int bch2_quota_enable(struct super_block *sb, unsigned uflags)
634 {
635         struct bch_fs *c = sb->s_fs_info;
636         struct bch_sb_field_quota *sb_quota;
637         int ret = 0;
638
639         if (sb->s_flags & SB_RDONLY)
640                 return -EROFS;
641
642         /* Accounting must be enabled at mount time: */
643         if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
644                 return -EINVAL;
645
646         /* Can't enable enforcement without accounting: */
647         if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
648                 return -EINVAL;
649
650         if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
651                 return -EINVAL;
652
653         if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
654                 return -EINVAL;
655
656         mutex_lock(&c->sb_lock);
657         sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
658         if (!sb_quota) {
659                 ret = -BCH_ERR_ENOSPC_sb_quota;
660                 goto unlock;
661         }
662
663         if (uflags & FS_QUOTA_UDQ_ENFD)
664                 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
665
666         if (uflags & FS_QUOTA_GDQ_ENFD)
667                 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
668
669         if (uflags & FS_QUOTA_PDQ_ENFD)
670                 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
671
672         bch2_write_super(c);
673 unlock:
674         mutex_unlock(&c->sb_lock);
675
676         return bch2_err_class(ret);
677 }
678
679 static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
680 {
681         struct bch_fs *c = sb->s_fs_info;
682
683         if (sb->s_flags & SB_RDONLY)
684                 return -EROFS;
685
686         mutex_lock(&c->sb_lock);
687         if (uflags & FS_QUOTA_UDQ_ENFD)
688                 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
689
690         if (uflags & FS_QUOTA_GDQ_ENFD)
691                 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
692
693         if (uflags & FS_QUOTA_PDQ_ENFD)
694                 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
695
696         bch2_write_super(c);
697         mutex_unlock(&c->sb_lock);
698
699         return 0;
700 }
701
702 static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
703 {
704         struct bch_fs *c = sb->s_fs_info;
705         int ret;
706
707         if (sb->s_flags & SB_RDONLY)
708                 return -EROFS;
709
710         if (uflags & FS_USER_QUOTA) {
711                 if (c->opts.usrquota)
712                         return -EINVAL;
713
714                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
715                                               POS(QTYP_USR, 0),
716                                               POS(QTYP_USR, U64_MAX),
717                                               0, NULL);
718                 if (ret)
719                         return ret;
720         }
721
722         if (uflags & FS_GROUP_QUOTA) {
723                 if (c->opts.grpquota)
724                         return -EINVAL;
725
726                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
727                                               POS(QTYP_GRP, 0),
728                                               POS(QTYP_GRP, U64_MAX),
729                                               0, NULL);
730                 if (ret)
731                         return ret;
732         }
733
734         if (uflags & FS_PROJ_QUOTA) {
735                 if (c->opts.prjquota)
736                         return -EINVAL;
737
738                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
739                                               POS(QTYP_PRJ, 0),
740                                               POS(QTYP_PRJ, U64_MAX),
741                                               0, NULL);
742                 if (ret)
743                         return ret;
744         }
745
746         return 0;
747 }
748
749 /*
750  * Return quota status information, such as enforcements, quota file inode
751  * numbers etc.
752  */
753 static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
754 {
755         struct bch_fs *c = sb->s_fs_info;
756         unsigned qtypes = enabled_qtypes(c);
757         unsigned i;
758
759         memset(state, 0, sizeof(*state));
760
761         for (i = 0; i < QTYP_NR; i++) {
762                 state->s_state[i].flags |= QCI_SYSFILE;
763
764                 if (!(qtypes & (1 << i)))
765                         continue;
766
767                 state->s_state[i].flags |= QCI_ACCT_ENABLED;
768
769                 state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
770                 state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
771
772                 state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
773                 state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
774         }
775
776         return 0;
777 }
778
779 /*
780  * Adjust quota timers & warnings
781  */
782 static int bch2_quota_set_info(struct super_block *sb, int type,
783                                struct qc_info *info)
784 {
785         struct bch_fs *c = sb->s_fs_info;
786         struct bch_sb_field_quota *sb_quota;
787         struct bch_memquota_type *q;
788         int ret = 0;
789
790         if (0) {
791                 struct printbuf buf = PRINTBUF;
792
793                 qc_info_to_text(&buf, info);
794                 pr_info("setting:\n%s", buf.buf);
795                 printbuf_exit(&buf);
796         }
797
798         if (sb->s_flags & SB_RDONLY)
799                 return -EROFS;
800
801         if (type >= QTYP_NR)
802                 return -EINVAL;
803
804         if (!((1 << type) & enabled_qtypes(c)))
805                 return -ESRCH;
806
807         if (info->i_fieldmask &
808             ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
809                 return -EINVAL;
810
811         q = &c->quotas[type];
812
813         mutex_lock(&c->sb_lock);
814         sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
815         if (!sb_quota) {
816                 ret = -BCH_ERR_ENOSPC_sb_quota;
817                 goto unlock;
818         }
819
820         if (info->i_fieldmask & QC_SPC_TIMER)
821                 sb_quota->q[type].c[Q_SPC].timelimit =
822                         cpu_to_le32(info->i_spc_timelimit);
823
824         if (info->i_fieldmask & QC_SPC_WARNS)
825                 sb_quota->q[type].c[Q_SPC].warnlimit =
826                         cpu_to_le32(info->i_spc_warnlimit);
827
828         if (info->i_fieldmask & QC_INO_TIMER)
829                 sb_quota->q[type].c[Q_INO].timelimit =
830                         cpu_to_le32(info->i_ino_timelimit);
831
832         if (info->i_fieldmask & QC_INO_WARNS)
833                 sb_quota->q[type].c[Q_INO].warnlimit =
834                         cpu_to_le32(info->i_ino_warnlimit);
835
836         bch2_sb_quota_read(c);
837
838         bch2_write_super(c);
839 unlock:
840         mutex_unlock(&c->sb_lock);
841
842         return bch2_err_class(ret);
843 }
844
845 /* Get/set individual quotas: */
846
847 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
848 {
849         dst->d_space            = src->c[Q_SPC].v << 9;
850         dst->d_spc_hardlimit    = src->c[Q_SPC].hardlimit << 9;
851         dst->d_spc_softlimit    = src->c[Q_SPC].softlimit << 9;
852         dst->d_spc_timer        = src->c[Q_SPC].timer;
853         dst->d_spc_warns        = src->c[Q_SPC].warns;
854
855         dst->d_ino_count        = src->c[Q_INO].v;
856         dst->d_ino_hardlimit    = src->c[Q_INO].hardlimit;
857         dst->d_ino_softlimit    = src->c[Q_INO].softlimit;
858         dst->d_ino_timer        = src->c[Q_INO].timer;
859         dst->d_ino_warns        = src->c[Q_INO].warns;
860 }
861
862 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
863                           struct qc_dqblk *qdq)
864 {
865         struct bch_fs *c                = sb->s_fs_info;
866         struct bch_memquota_type *q     = &c->quotas[kqid.type];
867         qid_t qid                       = from_kqid(&init_user_ns, kqid);
868         struct bch_memquota *mq;
869
870         memset(qdq, 0, sizeof(*qdq));
871
872         mutex_lock(&q->lock);
873         mq = genradix_ptr(&q->table, qid);
874         if (mq)
875                 __bch2_quota_get(qdq, mq);
876         mutex_unlock(&q->lock);
877
878         return 0;
879 }
880
881 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
882                                struct qc_dqblk *qdq)
883 {
884         struct bch_fs *c                = sb->s_fs_info;
885         struct bch_memquota_type *q     = &c->quotas[kqid->type];
886         qid_t qid                       = from_kqid(&init_user_ns, *kqid);
887         struct genradix_iter iter;
888         struct bch_memquota *mq;
889         int ret = 0;
890
891         mutex_lock(&q->lock);
892
893         genradix_for_each_from(&q->table, iter, mq, qid)
894                 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
895                         __bch2_quota_get(qdq, mq);
896                         *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
897                         goto found;
898                 }
899
900         ret = -ENOENT;
901 found:
902         mutex_unlock(&q->lock);
903         return bch2_err_class(ret);
904 }
905
906 static int bch2_set_quota_trans(struct btree_trans *trans,
907                                 struct bkey_i_quota *new_quota,
908                                 struct qc_dqblk *qdq)
909 {
910         struct btree_iter iter;
911         struct bkey_s_c k;
912         int ret;
913
914         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
915                                BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
916         ret = bkey_err(k);
917         if (unlikely(ret))
918                 return ret;
919
920         if (k.k->type == KEY_TYPE_quota)
921                 new_quota->v = *bkey_s_c_to_quota(k).v;
922
923         if (qdq->d_fieldmask & QC_SPC_SOFT)
924                 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
925         if (qdq->d_fieldmask & QC_SPC_HARD)
926                 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
927
928         if (qdq->d_fieldmask & QC_INO_SOFT)
929                 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
930         if (qdq->d_fieldmask & QC_INO_HARD)
931                 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
932
933         ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
934         bch2_trans_iter_exit(trans, &iter);
935         return ret;
936 }
937
938 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
939                           struct qc_dqblk *qdq)
940 {
941         struct bch_fs *c = sb->s_fs_info;
942         struct bkey_i_quota new_quota;
943         int ret;
944
945         if (0) {
946                 struct printbuf buf = PRINTBUF;
947
948                 qc_dqblk_to_text(&buf, qdq);
949                 pr_info("setting:\n%s", buf.buf);
950                 printbuf_exit(&buf);
951         }
952
953         if (sb->s_flags & SB_RDONLY)
954                 return -EROFS;
955
956         bkey_quota_init(&new_quota.k_i);
957         new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
958
959         ret = bch2_trans_do(c, NULL, NULL, 0,
960                             bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
961                 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
962
963         return bch2_err_class(ret);
964 }
965
966 const struct quotactl_ops bch2_quotactl_operations = {
967         .quota_enable           = bch2_quota_enable,
968         .quota_disable          = bch2_quota_disable,
969         .rm_xquota              = bch2_quota_remove,
970
971         .get_state              = bch2_quota_get_state,
972         .set_info               = bch2_quota_set_info,
973
974         .get_dqblk              = bch2_get_quota,
975         .get_nextdqblk          = bch2_get_next_quota,
976         .set_dqblk              = bch2_set_quota,
977 };
978
979 #endif /* CONFIG_BCACHEFS_QUOTA */