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