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