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