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