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