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