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