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