]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/quota.c
Update bcachefs sources to b964c6cba8 bcachefs: Change lockrestart_do() to always...
[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_put(&trans, iter);
376
377         return bch2_trans_exit(&trans) ?: ret;
378 }
379
380 void bch2_fs_quota_exit(struct bch_fs *c)
381 {
382         unsigned i;
383
384         for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
385                 genradix_free(&c->quotas[i].table);
386 }
387
388 void bch2_fs_quota_init(struct bch_fs *c)
389 {
390         unsigned i;
391
392         for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
393                 mutex_init(&c->quotas[i].lock);
394 }
395
396 static void bch2_sb_quota_read(struct bch_fs *c)
397 {
398         struct bch_sb_field_quota *sb_quota;
399         unsigned i, j;
400
401         sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
402         if (!sb_quota)
403                 return;
404
405         for (i = 0; i < QTYP_NR; i++) {
406                 struct bch_memquota_type *q = &c->quotas[i];
407
408                 for (j = 0; j < Q_COUNTERS; j++) {
409                         q->limits[j].timelimit =
410                                 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
411                         q->limits[j].warnlimit =
412                                 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
413                 }
414         }
415 }
416
417 int bch2_fs_quota_read(struct bch_fs *c)
418 {
419         unsigned i, qtypes = enabled_qtypes(c);
420         struct bch_memquota_type *q;
421         struct btree_trans trans;
422         struct btree_iter *iter;
423         struct bch_inode_unpacked u;
424         struct bkey_s_c k;
425         int ret;
426
427         mutex_lock(&c->sb_lock);
428         bch2_sb_quota_read(c);
429         mutex_unlock(&c->sb_lock);
430
431         for_each_set_qtype(c, i, q, qtypes) {
432                 ret = bch2_quota_init_type(c, i);
433                 if (ret)
434                         return ret;
435         }
436
437         bch2_trans_init(&trans, c, 0, 0);
438
439         for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
440                            BTREE_ITER_PREFETCH, k, ret) {
441                 switch (k.k->type) {
442                 case KEY_TYPE_inode:
443                         ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
444                         if (ret)
445                                 return ret;
446
447                         bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
448                                         KEY_TYPE_QUOTA_NOCHECK);
449                         bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
450                                         KEY_TYPE_QUOTA_NOCHECK);
451                 }
452         }
453         bch2_trans_iter_put(&trans, iter);
454
455         return bch2_trans_exit(&trans) ?: ret;
456 }
457
458 /* Enable/disable/delete quotas for an entire filesystem: */
459
460 static int bch2_quota_enable(struct super_block *sb, unsigned uflags)
461 {
462         struct bch_fs *c = sb->s_fs_info;
463
464         if (sb->s_flags & SB_RDONLY)
465                 return -EROFS;
466
467         /* Accounting must be enabled at mount time: */
468         if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
469                 return -EINVAL;
470
471         /* Can't enable enforcement without accounting: */
472         if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
473                 return -EINVAL;
474
475         if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
476                 return -EINVAL;
477
478         if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
479                 return -EINVAL;
480
481         mutex_lock(&c->sb_lock);
482         if (uflags & FS_QUOTA_UDQ_ENFD)
483                 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
484
485         if (uflags & FS_QUOTA_GDQ_ENFD)
486                 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
487
488         if (uflags & FS_QUOTA_PDQ_ENFD)
489                 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
490
491         bch2_write_super(c);
492         mutex_unlock(&c->sb_lock);
493
494         return 0;
495 }
496
497 static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
498 {
499         struct bch_fs *c = sb->s_fs_info;
500
501         if (sb->s_flags & SB_RDONLY)
502                 return -EROFS;
503
504         mutex_lock(&c->sb_lock);
505         if (uflags & FS_QUOTA_UDQ_ENFD)
506                 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
507
508         if (uflags & FS_QUOTA_GDQ_ENFD)
509                 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
510
511         if (uflags & FS_QUOTA_PDQ_ENFD)
512                 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
513
514         bch2_write_super(c);
515         mutex_unlock(&c->sb_lock);
516
517         return 0;
518 }
519
520 static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
521 {
522         struct bch_fs *c = sb->s_fs_info;
523         int ret;
524
525         if (sb->s_flags & SB_RDONLY)
526                 return -EROFS;
527
528         if (uflags & FS_USER_QUOTA) {
529                 if (c->opts.usrquota)
530                         return -EINVAL;
531
532                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
533                                               POS(QTYP_USR, 0),
534                                               POS(QTYP_USR + 1, 0),
535                                               NULL);
536                 if (ret)
537                         return ret;
538         }
539
540         if (uflags & FS_GROUP_QUOTA) {
541                 if (c->opts.grpquota)
542                         return -EINVAL;
543
544                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
545                                               POS(QTYP_GRP, 0),
546                                               POS(QTYP_GRP + 1, 0),
547                                               NULL);
548                 if (ret)
549                         return ret;
550         }
551
552         if (uflags & FS_PROJ_QUOTA) {
553                 if (c->opts.prjquota)
554                         return -EINVAL;
555
556                 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
557                                               POS(QTYP_PRJ, 0),
558                                               POS(QTYP_PRJ + 1, 0),
559                                               NULL);
560                 if (ret)
561                         return ret;
562         }
563
564         return 0;
565 }
566
567 /*
568  * Return quota status information, such as enforcements, quota file inode
569  * numbers etc.
570  */
571 static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
572 {
573         struct bch_fs *c = sb->s_fs_info;
574         unsigned qtypes = enabled_qtypes(c);
575         unsigned i;
576
577         memset(state, 0, sizeof(*state));
578
579         for (i = 0; i < QTYP_NR; i++) {
580                 state->s_state[i].flags |= QCI_SYSFILE;
581
582                 if (!(qtypes & (1 << i)))
583                         continue;
584
585                 state->s_state[i].flags |= QCI_ACCT_ENABLED;
586
587                 state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
588                 state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
589
590                 state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
591                 state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
592         }
593
594         return 0;
595 }
596
597 /*
598  * Adjust quota timers & warnings
599  */
600 static int bch2_quota_set_info(struct super_block *sb, int type,
601                                struct qc_info *info)
602 {
603         struct bch_fs *c = sb->s_fs_info;
604         struct bch_sb_field_quota *sb_quota;
605         struct bch_memquota_type *q;
606
607         if (sb->s_flags & SB_RDONLY)
608                 return -EROFS;
609
610         if (type >= QTYP_NR)
611                 return -EINVAL;
612
613         if (!((1 << type) & enabled_qtypes(c)))
614                 return -ESRCH;
615
616         if (info->i_fieldmask &
617             ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
618                 return -EINVAL;
619
620         q = &c->quotas[type];
621
622         mutex_lock(&c->sb_lock);
623         sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
624         if (!sb_quota) {
625                 sb_quota = bch2_sb_resize_quota(&c->disk_sb,
626                                         sizeof(*sb_quota) / sizeof(u64));
627                 if (!sb_quota)
628                         return -ENOSPC;
629         }
630
631         if (info->i_fieldmask & QC_SPC_TIMER)
632                 sb_quota->q[type].c[Q_SPC].timelimit =
633                         cpu_to_le32(info->i_spc_timelimit);
634
635         if (info->i_fieldmask & QC_SPC_WARNS)
636                 sb_quota->q[type].c[Q_SPC].warnlimit =
637                         cpu_to_le32(info->i_spc_warnlimit);
638
639         if (info->i_fieldmask & QC_INO_TIMER)
640                 sb_quota->q[type].c[Q_INO].timelimit =
641                         cpu_to_le32(info->i_ino_timelimit);
642
643         if (info->i_fieldmask & QC_INO_WARNS)
644                 sb_quota->q[type].c[Q_INO].warnlimit =
645                         cpu_to_le32(info->i_ino_warnlimit);
646
647         bch2_sb_quota_read(c);
648
649         bch2_write_super(c);
650         mutex_unlock(&c->sb_lock);
651
652         return 0;
653 }
654
655 /* Get/set individual quotas: */
656
657 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
658 {
659         dst->d_space            = src->c[Q_SPC].v << 9;
660         dst->d_spc_hardlimit    = src->c[Q_SPC].hardlimit << 9;
661         dst->d_spc_softlimit    = src->c[Q_SPC].softlimit << 9;
662         dst->d_spc_timer        = src->c[Q_SPC].timer;
663         dst->d_spc_warns        = src->c[Q_SPC].warns;
664
665         dst->d_ino_count        = src->c[Q_INO].v;
666         dst->d_ino_hardlimit    = src->c[Q_INO].hardlimit;
667         dst->d_ino_softlimit    = src->c[Q_INO].softlimit;
668         dst->d_ino_timer        = src->c[Q_INO].timer;
669         dst->d_ino_warns        = src->c[Q_INO].warns;
670 }
671
672 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
673                           struct qc_dqblk *qdq)
674 {
675         struct bch_fs *c                = sb->s_fs_info;
676         struct bch_memquota_type *q     = &c->quotas[kqid.type];
677         qid_t qid                       = from_kqid(&init_user_ns, kqid);
678         struct bch_memquota *mq;
679
680         memset(qdq, 0, sizeof(*qdq));
681
682         mutex_lock(&q->lock);
683         mq = genradix_ptr(&q->table, qid);
684         if (mq)
685                 __bch2_quota_get(qdq, mq);
686         mutex_unlock(&q->lock);
687
688         return 0;
689 }
690
691 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
692                                struct qc_dqblk *qdq)
693 {
694         struct bch_fs *c                = sb->s_fs_info;
695         struct bch_memquota_type *q     = &c->quotas[kqid->type];
696         qid_t qid                       = from_kqid(&init_user_ns, *kqid);
697         struct genradix_iter iter;
698         struct bch_memquota *mq;
699         int ret = 0;
700
701         mutex_lock(&q->lock);
702
703         genradix_for_each_from(&q->table, iter, mq, qid)
704                 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
705                         __bch2_quota_get(qdq, mq);
706                         *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
707                         goto found;
708                 }
709
710         ret = -ENOENT;
711 found:
712         mutex_unlock(&q->lock);
713         return ret;
714 }
715
716 static int bch2_set_quota_trans(struct btree_trans *trans,
717                                 struct bkey_i_quota *new_quota,
718                                 struct qc_dqblk *qdq)
719 {
720         struct btree_iter *iter;
721         struct bkey_s_c k;
722         int ret;
723
724         iter = bch2_trans_get_iter(trans, BTREE_ID_quotas, new_quota->k.p,
725                                    BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
726         k = bch2_btree_iter_peek_slot(iter);
727
728         ret = bkey_err(k);
729         if (unlikely(ret))
730                 return ret;
731
732         if (k.k->type == KEY_TYPE_quota)
733                 new_quota->v = *bkey_s_c_to_quota(k).v;
734
735         if (qdq->d_fieldmask & QC_SPC_SOFT)
736                 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
737         if (qdq->d_fieldmask & QC_SPC_HARD)
738                 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
739
740         if (qdq->d_fieldmask & QC_INO_SOFT)
741                 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
742         if (qdq->d_fieldmask & QC_INO_HARD)
743                 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
744
745         ret = bch2_trans_update(trans, iter, &new_quota->k_i, 0);
746         bch2_trans_iter_put(trans, iter);
747         return ret;
748 }
749
750 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
751                           struct qc_dqblk *qdq)
752 {
753         struct bch_fs *c = sb->s_fs_info;
754         struct bkey_i_quota new_quota;
755         int ret;
756
757         if (sb->s_flags & SB_RDONLY)
758                 return -EROFS;
759
760         bkey_quota_init(&new_quota.k_i);
761         new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
762
763         ret = bch2_trans_do(c, NULL, NULL, 0,
764                             bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
765                 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i));
766
767         return ret;
768 }
769
770 const struct quotactl_ops bch2_quotactl_operations = {
771         .quota_enable           = bch2_quota_enable,
772         .quota_disable          = bch2_quota_disable,
773         .rm_xquota              = bch2_quota_remove,
774
775         .get_state              = bch2_quota_get_state,
776         .set_info               = bch2_quota_set_info,
777
778         .get_dqblk              = bch2_get_quota,
779         .get_nextdqblk          = bch2_get_next_quota,
780         .set_dqblk              = bch2_set_quota,
781 };
782
783 #endif /* CONFIG_BCACHEFS_QUOTA */