1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
/* Only for reporting filesystem quota */
#include "lib.h"
#include "array.h"
#include "str.h"
#include "hostpid.h"
#include "mountpoint.h"
#include "quota-private.h"
#include "quota-fs.h"
#ifdef HAVE_FS_QUOTA
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef HAVE_LINUX_DQBLK_XFS_H
# include <linux/dqblk_xfs.h>
# define HAVE_XFS_QUOTA
#elif defined (HAVE_XFS_XQM_H)
# include <xfs/xqm.h> /* CentOS 4.x at least uses this */
# define HAVE_XFS_QUOTA
#endif
#ifdef HAVE_RQUOTA
# include "rquota.h"
# define RQUOTA_GETQUOTA_TIMEOUT_SECS 10
#endif
#ifndef DEV_BSIZE
# ifdef DQBSIZE
# define DEV_BSIZE DQBSIZE /* AIX */
# else
# define DEV_BSIZE 512
# endif
#endif
#ifdef HAVE_STRUCT_DQBLK_CURSPACE
# define dqb_curblocks dqb_curspace
#endif
/* Very old sys/quota.h doesn't define _LINUX_QUOTA_VERSION at all, which means
it supports only v1 quota. However, new sys/quota.h (glibc 2.25) removes
support for v1 entirely and again it doesn't define it. I guess we can just
assume v2 now, and if someone still wants v1 support they can add
-D_LINUX_QUOTA_VERSION=1 to CFLAGS. */
#ifndef _LINUX_QUOTA_VERSION
# define _LINUX_QUOTA_VERSION 2
#endif
#define mount_type_is_nfs(mount) \
(strcmp((mount)->type, "nfs") == 0 || \
strcmp((mount)->type, "nfs4") == 0)
struct fs_quota_mountpoint {
int refcount;
char *mount_path;
char *device_path;
char *type;
unsigned int block_size;
#ifdef FS_QUOTA_SOLARIS
int fd;
char *path;
#endif
};
struct fs_quota_root {
struct quota_root root;
char *storage_mount_path;
uid_t uid;
gid_t gid;
struct fs_quota_mountpoint *mount;
bool inode_per_mail:1;
bool user_disabled:1;
bool group_disabled:1;
#ifdef FS_QUOTA_NETBSD
struct quotahandle *qh;
#endif
};
extern struct quota_backend quota_backend_fs;
static struct quota_root *fs_quota_alloc(void)
{
struct fs_quota_root *root;
root = i_new(struct fs_quota_root, 1);
root->uid = geteuid();
root->gid = getegid();
return &root->root;
}
static void handle_user_param(struct quota_root *_root, const char *param_value ATTR_UNUSED)
{
((struct fs_quota_root *)_root)->group_disabled = TRUE;
}
static void handle_group_param(struct quota_root *_root, const char *param_value ATTR_UNUSED)
{
((struct fs_quota_root *)_root)->user_disabled = TRUE;
}
static void handle_inode_param(struct quota_root *_root, const char *param_value ATTR_UNUSED)
{
((struct fs_quota_root *)_root)->inode_per_mail = TRUE;
}
static void handle_mount_param(struct quota_root *_root, const char *param_value)
{
struct fs_quota_root *root = (struct fs_quota_root *)_root;
i_free(root->storage_mount_path);
root->storage_mount_path = i_strdup(param_value);
}
static int fs_quota_init(struct quota_root *_root, const char *args,
const char **error_r)
{
const struct quota_param_parser fs_params[] = {
{.param_name = "user", .param_handler = handle_user_param},
{.param_name = "group", .param_handler = handle_group_param},
{.param_name = "mount=", .param_handler = handle_mount_param},
{.param_name = "inode_per_mail", .param_handler = handle_inode_param},
quota_param_hidden, quota_param_noenforcing, quota_param_ns,
{.param_name = NULL}
};
event_set_append_log_prefix(_root->backend.event, "quota-fs: ");
if (quota_parse_parameters(_root, &args, error_r, fs_params, TRUE) < 0)
return -1;
_root->auto_updating = TRUE;
return 0;
}
static void fs_quota_mountpoint_free(struct fs_quota_mountpoint *mount)
{
if (--mount->refcount > 0)
return;
#ifdef FS_QUOTA_SOLARIS
i_close_fd_path(&mount->fd, mount->path);
i_free(mount->path);
#endif
i_free(mount->device_path);
i_free(mount->mount_path);
i_free(mount->type);
i_free(mount);
}
static void fs_quota_deinit(struct quota_root *_root)
{
struct fs_quota_root *root = (struct fs_quota_root *)_root;
if (root->mount != NULL)
fs_quota_mountpoint_free(root->mount);
i_free(root->storage_mount_path);
i_free(root);
}
static struct fs_quota_mountpoint *fs_quota_mountpoint_get(const char *dir)
{
struct fs_quota_mountpoint *mount;
struct mountpoint point;
int ret;
ret = mountpoint_get(dir, default_pool, &point);
if (ret <= 0)
return NULL;
mount = i_new(struct fs_quota_mountpoint, 1);
mount->refcount = 1;
mount->device_path = point.device_path;
mount->mount_path = point.mount_path;
mount->type = point.type;
mount->block_size = point.block_size;
#ifdef FS_QUOTA_SOLARIS
mount->fd = -1;
#endif
if (mount_type_is_nfs(mount)) {
if (strchr(mount->device_path, ':') == NULL) {
e_error(quota_backend_fs.event,
"%s is not a valid NFS device path",
mount->device_path);
fs_quota_mountpoint_free(mount);
return NULL;
}
}
return mount;
}
#define QUOTA_ROOT_MATCH(root, mount) \
((root)->root.backend.name == quota_backend_fs.name && \
((root)->storage_mount_path == NULL || \
strcmp((root)->storage_mount_path, (mount)->mount_path) == 0))
static struct fs_quota_root *
fs_quota_root_find_mountpoint(struct quota *quota,
const struct fs_quota_mountpoint *mount)
{
struct quota_root *const *roots;
struct fs_quota_root *empty = NULL;
unsigned int i, count;
roots = array_get("a->roots, &count);
for (i = 0; i < count; i++) {
struct fs_quota_root *root = (struct fs_quota_root *)roots[i];
if (QUOTA_ROOT_MATCH(root, mount)) {
if (root->mount == NULL)
empty = root;
else if (strcmp(root->mount->mount_path,
mount->mount_path) == 0)
return root;
}
}
return empty;
}
static void
fs_quota_mount_init(struct fs_quota_root *root,
struct fs_quota_mountpoint *mount, const char *dir)
{
struct quota_root *const *roots;
unsigned int i, count;
#ifdef FS_QUOTA_SOLARIS
#ifdef HAVE_RQUOTA
if (mount_type_is_nfs(mount)) {
/* using rquota for this mount */
} else
#endif
if (mount->path == NULL) {
mount->path = i_strconcat(mount->mount_path, "/quotas", NULL);
mount->fd = open(mount->path, O_RDONLY);
if (mount->fd == -1 && errno != ENOENT)
e_error(root->root.backend.event,
"open(%s) failed: %m", mount->path);
}
#endif
root->mount = mount;
e_debug(root->root.backend.event, "fs quota add mailbox dir = %s", dir);
e_debug(root->root.backend.event, "fs quota block device = %s", mount->device_path);
e_debug(root->root.backend.event, "fs quota mount point = %s", mount->mount_path);
e_debug(root->root.backend.event, "fs quota mount type = %s", mount->type);
/* if there are more unused quota roots, copy this mount to them */
roots = array_get(&root->root.quota->roots, &count);
for (i = 0; i < count; i++) {
root = (struct fs_quota_root *)roots[i];
if (QUOTA_ROOT_MATCH(root, mount) && root->mount == NULL) {
mount->refcount++;
root->mount = mount;
}
}
}
static void fs_quota_add_missing_mounts(struct quota *quota)
{
struct fs_quota_mountpoint *mount;
struct quota_root *const *roots;
unsigned int i, count;
roots = array_get("a->roots, &count);
for (i = 0; i < count; i++) {
struct fs_quota_root *root = (struct fs_quota_root *)roots[i];
if (root->root.backend.name != quota_backend_fs.name ||
root->storage_mount_path == NULL || root->mount != NULL)
continue;
mount = fs_quota_mountpoint_get(root->storage_mount_path);
if (mount != NULL) {
fs_quota_mount_init(root, mount,
root->storage_mount_path);
}
}
}
static void fs_quota_namespace_added(struct quota *quota,
struct mail_namespace *ns)
{
struct fs_quota_mountpoint *mount;
struct fs_quota_root *root;
const char *dir;
if (!mailbox_list_get_root_path(ns->list, MAILBOX_LIST_PATH_TYPE_MAILBOX,
&dir))
mount = NULL;
else
mount = fs_quota_mountpoint_get(dir);
if (mount != NULL) {
root = fs_quota_root_find_mountpoint(quota, mount);
if (root != NULL && root->mount == NULL)
fs_quota_mount_init(root, mount, dir);
else
fs_quota_mountpoint_free(mount);
}
/* we would actually want to do this only once after all quota roots
are created, but there's no way to do this right now */
fs_quota_add_missing_mounts(quota);
}
static const char *const *
fs_quota_root_get_resources(struct quota_root *_root)
{
struct fs_quota_root *root = (struct fs_quota_root *)_root;
static const char *resources_kb[] = {
QUOTA_NAME_STORAGE_KILOBYTES,
NULL
};
static const char *resources_kb_messages[] = {
QUOTA_NAME_STORAGE_KILOBYTES,
QUOTA_NAME_MESSAGES,
NULL
};
return root->inode_per_mail ? resources_kb_messages : resources_kb;
}
#if defined(FS_QUOTA_LINUX) || defined(FS_QUOTA_BSDAIX) || \
defined(FS_QUOTA_NETBSD) || defined(HAVE_RQUOTA)
static void fs_quota_root_disable(struct fs_quota_root *root, bool group)
{
if (group)
root->group_disabled = TRUE;
else
root->user_disabled = TRUE;
}
#endif
#ifdef HAVE_RQUOTA
static void
rquota_get_result(const rquota *rq,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r)
{
/* use soft limits if they exist, fallback to hard limits */
/* convert the results from blocks to bytes */
*bytes_value_r = (uint64_t)rq->rq_curblocks *
(uint64_t)rq->rq_bsize;
if (rq->rq_bsoftlimit != 0) {
*bytes_limit_r = (uint64_t)rq->rq_bsoftlimit *
(uint64_t)rq->rq_bsize;
} else {
*bytes_limit_r = (uint64_t)rq->rq_bhardlimit *
(uint64_t)rq->rq_bsize;
}
*count_value_r = rq->rq_curfiles;
if (rq->rq_fsoftlimit != 0)
*count_limit_r = rq->rq_fsoftlimit;
else
*count_limit_r = rq->rq_fhardlimit;
}
static int
do_rquota_user(struct fs_quota_root *root,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct getquota_rslt result;
struct getquota_args args;
struct timeval timeout;
enum clnt_stat call_status;
CLIENT *cl;
struct fs_quota_mountpoint *mount = root->mount;
const char *host;
char *path;
path = strchr(mount->device_path, ':');
i_assert(path != NULL);
host = t_strdup_until(mount->device_path, path);
path++;
/* For NFSv4, we send the filesystem path without initial /. Server
prepends proper NFS pseudoroot automatically and uses this for
detection of NFSv4 mounts. */
if (strcmp(root->mount->type, "nfs4") == 0) {
while (*path == '/')
path++;
}
e_debug(root->root.backend.event, "host=%s, path=%s, uid=%s",
host, path, dec2str(root->uid));
/* clnt_create() polls for a while to establish a connection */
cl = clnt_create(host, RQUOTAPROG, RQUOTAVERS, "udp");
if (cl == NULL) {
*error_r = t_strdup_printf(
"could not contact RPC service on %s", host);
return -1;
}
/* Establish some RPC credentials */
auth_destroy(cl->cl_auth);
cl->cl_auth = authunix_create_default();
/* make the rquota call on the remote host */
args.gqa_pathp = path;
args.gqa_uid = root->uid;
timeout.tv_sec = RQUOTA_GETQUOTA_TIMEOUT_SECS;
timeout.tv_usec = 0;
call_status = clnt_call(cl, RQUOTAPROC_GETQUOTA,
(xdrproc_t)xdr_getquota_args, (char *)&args,
(xdrproc_t)xdr_getquota_rslt, (char *)&result,
timeout);
/* the result has been deserialized, let the client go */
auth_destroy(cl->cl_auth);
clnt_destroy(cl);
if (call_status != RPC_SUCCESS) {
const char *rpc_error_msg = clnt_sperrno(call_status);
*error_r = t_strdup_printf(
"remote rquota call failed: %s",
rpc_error_msg);
return -1;
}
switch (result.status) {
case Q_OK: {
rquota_get_result(&result.getquota_rslt_u.gqr_rquota,
bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r);
e_debug(root->root.backend.event, "uid=%s, bytes=%"PRIu64"/%"PRIu64" "
"files=%"PRIu64"/%"PRIu64,
dec2str(root->uid),
*bytes_value_r, *bytes_limit_r,
*count_value_r, *count_limit_r);
return 1;
}
case Q_NOQUOTA:
e_debug(root->root.backend.event, "uid=%s, limit=unlimited",
dec2str(root->uid));
fs_quota_root_disable(root, FALSE);
return 0;
case Q_EPERM:
*error_r = "permission denied to rquota service";
return -1;
default:
*error_r = t_strdup_printf(
"unrecognized status code (%d) from rquota service",
result.status);
return -1;
}
}
static int
do_rquota_group(struct fs_quota_root *root ATTR_UNUSED,
uint64_t *bytes_value_r ATTR_UNUSED,
uint64_t *bytes_limit_r ATTR_UNUSED,
uint64_t *count_value_r ATTR_UNUSED,
uint64_t *count_limit_r ATTR_UNUSED,
const char **error_r)
{
#if defined(EXT_RQUOTAVERS) && defined(GRPQUOTA)
struct getquota_rslt result;
ext_getquota_args args;
struct timeval timeout;
enum clnt_stat call_status;
CLIENT *cl;
struct fs_quota_mountpoint *mount = root->mount;
const char *host;
char *path;
path = strchr(mount->device_path, ':');
i_assert(path != NULL);
host = t_strdup_until(mount->device_path, path);
path++;
e_debug(root->root.backend.event, "host=%s, path=%s, gid=%s",
host, path, dec2str(root->gid));
/* clnt_create() polls for a while to establish a connection */
cl = clnt_create(host, RQUOTAPROG, EXT_RQUOTAVERS, "udp");
if (cl == NULL) {
*error_r = t_strdup_printf(
"could not contact RPC service on %s (group)", host);
return -1;
}
/* Establish some RPC credentials */
auth_destroy(cl->cl_auth);
cl->cl_auth = authunix_create_default();
/* make the rquota call on the remote host */
args.gqa_pathp = path;
args.gqa_id = root->gid;
args.gqa_type = GRPQUOTA;
timeout.tv_sec = RQUOTA_GETQUOTA_TIMEOUT_SECS;
timeout.tv_usec = 0;
call_status = clnt_call(cl, RQUOTAPROC_GETQUOTA,
(xdrproc_t)xdr_ext_getquota_args, (char *)&args,
(xdrproc_t)xdr_getquota_rslt, (char *)&result,
timeout);
/* the result has been deserialized, let the client go */
auth_destroy(cl->cl_auth);
clnt_destroy(cl);
if (call_status != RPC_SUCCESS) {
const char *rpc_error_msg = clnt_sperrno(call_status);
*error_r = t_strdup_printf(
"remote ext rquota call failed: %s", rpc_error_msg);
return -1;
}
switch (result.status) {
case Q_OK: {
rquota_get_result(&result.getquota_rslt_u.gqr_rquota,
bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r);
e_debug(root->root.backend.event, "gid=%s, bytes=%"PRIu64"/%"PRIu64" "
"files=%"PRIu64"/%"PRIu64,
dec2str(root->gid),
*bytes_value_r, *bytes_limit_r,
*count_value_r, *count_limit_r);
return 1;
}
case Q_NOQUOTA:
e_debug(root->root.backend.event, "gid=%s, limit=unlimited",
dec2str(root->gid));
fs_quota_root_disable(root, TRUE);
return 0;
case Q_EPERM:
*error_r = "permission denied to ext rquota service";
return -1;
default:
*error_r = t_strdup_printf(
"unrecognized status code (%d) from ext rquota service",
result.status);
return -1;
}
#else
*error_r = "rquota not compiled with group support";
return -1;
#endif
}
#endif
#ifdef FS_QUOTA_LINUX
static int
fs_quota_get_linux(struct fs_quota_root *root, bool group,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct dqblk dqblk;
int type, id;
type = group ? GRPQUOTA : USRQUOTA;
id = group ? root->gid : root->uid;
#ifdef HAVE_XFS_QUOTA
if (strcmp(root->mount->type, "xfs") == 0) {
struct fs_disk_quota xdqblk;
if (quotactl(QCMD(Q_XGETQUOTA, type),
root->mount->device_path,
id, (caddr_t)&xdqblk) < 0) {
if (errno == ESRCH) {
fs_quota_root_disable(root, group);
return 0;
}
*error_r = t_strdup_printf(
"errno=%d, quotactl(Q_XGETQUOTA, %s) failed: %m",
errno, root->mount->device_path);
return -1;
}
/* values always returned in 512 byte blocks */
*bytes_value_r = xdqblk.d_bcount * 512ULL;
*bytes_limit_r = xdqblk.d_blk_softlimit * 512ULL;
if (*bytes_limit_r == 0) {
*bytes_limit_r = xdqblk.d_blk_hardlimit * 512ULL;
}
*count_value_r = xdqblk.d_icount;
*count_limit_r = xdqblk.d_ino_softlimit;
if (*count_limit_r == 0) {
*count_limit_r = xdqblk.d_ino_hardlimit;
}
} else
#endif
{
/* ext2, ext3 */
if (quotactl(QCMD(Q_GETQUOTA, type),
root->mount->device_path,
id, (caddr_t)&dqblk) < 0) {
if (errno == ESRCH) {
fs_quota_root_disable(root, group);
return 0;
}
*error_r = t_strdup_printf(
"quotactl(Q_GETQUOTA, %s) failed: %m",
root->mount->device_path);
if (errno == EINVAL) {
*error_r = t_strdup_printf("%s, "
"Dovecot was compiled with Linux quota "
"v%d support, try changing it "
"(CPPFLAGS=-D_LINUX_QUOTA_VERSION=%d configure)",
*error_r,
_LINUX_QUOTA_VERSION,
_LINUX_QUOTA_VERSION == 1 ? 2 : 1);
}
return -1;
}
#if _LINUX_QUOTA_VERSION == 1
*bytes_value_r = dqblk.dqb_curblocks * 1024ULL;
#else
*bytes_value_r = dqblk.dqb_curblocks;
#endif
*bytes_limit_r = dqblk.dqb_bsoftlimit * 1024ULL;
if (*bytes_limit_r == 0) {
*bytes_limit_r = dqblk.dqb_bhardlimit * 1024ULL;
}
*count_value_r = dqblk.dqb_curinodes;
*count_limit_r = dqblk.dqb_isoftlimit;
if (*count_limit_r == 0) {
*count_limit_r = dqblk.dqb_ihardlimit;
}
}
return 1;
}
#endif
#ifdef FS_QUOTA_BSDAIX
static int
fs_quota_get_bsdaix(struct fs_quota_root *root, bool group,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct dqblk dqblk;
int type, id;
type = group ? GRPQUOTA : USRQUOTA;
id = group ? root->gid : root->uid;
if (quotactl(root->mount->mount_path, QCMD(Q_GETQUOTA, type),
id, (void *)&dqblk) < 0) {
if (errno == ESRCH) {
fs_quota_root_disable(root, group);
return 0;
}
*error_r = t_strdup_printf(
"quotactl(Q_GETQUOTA, %s) failed: %m",
root->mount->mount_path);
return -1;
}
*bytes_value_r = (uint64_t)dqblk.dqb_curblocks * DEV_BSIZE;
*bytes_limit_r = (uint64_t)dqblk.dqb_bsoftlimit * DEV_BSIZE;
if (*bytes_limit_r == 0) {
*bytes_limit_r = (uint64_t)dqblk.dqb_bhardlimit * DEV_BSIZE;
}
*count_value_r = dqblk.dqb_curinodes;
*count_limit_r = dqblk.dqb_isoftlimit;
if (*count_limit_r == 0) {
*count_limit_r = dqblk.dqb_ihardlimit;
}
return 1;
}
#endif
#ifdef FS_QUOTA_NETBSD
static int
fs_quota_get_netbsd(struct fs_quota_root *root, bool group,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct quotakey qk;
struct quotaval qv;
struct quotahandle *qh;
int ret;
if ((qh = quota_open(root->mount->mount_path)) == NULL) {
*error_r = t_strdup_printf("cannot open quota for %s: %m",
root->mount->mount_path);
fs_quota_root_disable(root, group);
return 0;
}
qk.qk_idtype = group ? QUOTA_IDTYPE_GROUP : QUOTA_IDTYPE_USER;
qk.qk_id = group ? root->gid : root->uid;
for (int i = 0; i < 2; i++) {
qk.qk_objtype = i == 0 ? QUOTA_OBJTYPE_BLOCKS : QUOTA_OBJTYPE_FILES;
if (quota_get(qh, &qk, &qv) != 0) {
if (errno == ESRCH) {
fs_quota_root_disable(root, group);
return 0;
}
*error_r = t_strdup_printf(
"quotactl(Q_GETQUOTA, %s) failed: %m",
root->mount->mount_path);
ret = -1;
break;
}
if (i == 0) {
*bytes_value_r = qv.qv_usage * DEV_BSIZE;
*bytes_limit_r = qv.qv_softlimit * DEV_BSIZE;
} else {
*count_value_r = qv.qv_usage;
*count_limit_r = qv.qv_softlimit;
}
ret = 1;
}
quota_close(qh);
return ret;
}
#endif
#ifdef FS_QUOTA_HPUX
static int
fs_quota_get_hpux(struct fs_quota_root *root,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct dqblk dqblk;
if (quotactl(Q_GETQUOTA, root->mount->device_path,
root->uid, &dqblk) < 0) {
if (errno == ESRCH) {
root->user_disabled = TRUE;
return 0;
}
*error_r = t_strdup_printf(
"quotactl(Q_GETQUOTA, %s) failed: %m",
root->mount->device_path);
return -1;
}
*bytes_value_r = (uint64_t)dqblk.dqb_curblocks *
root->mount->block_size;
*bytes_limit_r = (uint64_t)dqblk.dqb_bsoftlimit *
root->mount->block_size;
if (*bytes_limit_r == 0) {
*bytes_limit_r = (uint64_t)dqblk.dqb_bhardlimit *
root->mount->block_size;
}
*count_value_r = dqblk.dqb_curfiles;
*count_limit_r = dqblk.dqb_fsoftlimit;
if (*count_limit_r == 0) {
*count_limit_r = dqblk.dqb_fhardlimit;
}
return 1;
}
#endif
#ifdef FS_QUOTA_SOLARIS
static int
fs_quota_get_solaris(struct fs_quota_root *root,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
struct dqblk dqblk;
struct quotctl ctl;
if (root->mount->fd == -1)
return 0;
ctl.op = Q_GETQUOTA;
ctl.uid = root->uid;
ctl.addr = (caddr_t)&dqblk;
if (ioctl(root->mount->fd, Q_QUOTACTL, &ctl) < 0) {
*error_r = t_strdup_printf(
"ioctl(%s, Q_QUOTACTL) failed: %m",
root->mount->path);
return -1;
}
*bytes_value_r = (uint64_t)dqblk.dqb_curblocks * DEV_BSIZE;
*bytes_limit_r = (uint64_t)dqblk.dqb_bsoftlimit * DEV_BSIZE;
if (*bytes_limit_r == 0) {
*bytes_limit_r = (uint64_t)dqblk.dqb_bhardlimit * DEV_BSIZE;
}
*count_value_r = dqblk.dqb_curfiles;
*count_limit_r = dqblk.dqb_fsoftlimit;
if (*count_limit_r == 0) {
*count_limit_r = dqblk.dqb_fhardlimit;
}
return 1;
}
#endif
static int
fs_quota_get_resources(struct fs_quota_root *root, bool group,
uint64_t *bytes_value_r, uint64_t *bytes_limit_r,
uint64_t *count_value_r, uint64_t *count_limit_r,
const char **error_r)
{
if (group) {
if (root->group_disabled)
return 0;
} else {
if (root->user_disabled)
return 0;
}
#ifdef FS_QUOTA_LINUX
return fs_quota_get_linux(root, group, bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r, error_r);
#elif defined (FS_QUOTA_NETBSD)
return fs_quota_get_netbsd(root, group, bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r, error_r);
#elif defined (FS_QUOTA_BSDAIX)
return fs_quota_get_bsdaix(root, group, bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r, error_r);
#else
if (group) {
/* not supported */
return 0;
}
#ifdef FS_QUOTA_HPUX
return fs_quota_get_hpux(root, bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r, error_r);
#else
return fs_quota_get_solaris(root, bytes_value_r, bytes_limit_r,
count_value_r, count_limit_r, error_r);
#endif
#endif
}
static bool fs_quota_match_box(struct quota_root *_root, struct mailbox *box)
{
struct fs_quota_root *root = (struct fs_quota_root *)_root;
struct stat mst, rst;
const char *mailbox_path;
bool match;
if (root->storage_mount_path == NULL)
return TRUE;
if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_MAILBOX,
&mailbox_path) <= 0)
return FALSE;
if (stat(mailbox_path, &mst) < 0) {
if (errno != ENOENT)
e_error(_root->backend.event,
"stat(%s) failed: %m", mailbox_path);
return FALSE;
}
if (stat(root->storage_mount_path, &rst) < 0) {
e_debug(_root->backend.event, "stat(%s) failed: %m",
root->storage_mount_path);
return FALSE;
}
match = CMP_DEV_T(mst.st_dev, rst.st_dev);
e_debug(_root->backend.event, "box=%s mount=%s match=%s", mailbox_path,
root->storage_mount_path, match ? "yes" : "no");
return match;
}
static enum quota_get_result
fs_quota_get_resource(struct quota_root *_root, const char *name,
uint64_t *value_r, const char **error_r)
{
struct fs_quota_root *root = (struct fs_quota_root *)_root;
uint64_t bytes_value, count_value;
uint64_t bytes_limit = 0, count_limit = 0;
int ret;
*value_r = 0;
if (root->mount == NULL) {
if (root->storage_mount_path != NULL)
*error_r = t_strdup_printf(
"Mount point unknown for path %s",
root->storage_mount_path);
else
*error_r = "Mount point unknown";
return QUOTA_GET_RESULT_INTERNAL_ERROR;
}
if (strcasecmp(name, QUOTA_NAME_STORAGE_BYTES) != 0 &&
strcasecmp(name, QUOTA_NAME_MESSAGES) != 0) {
*error_r = QUOTA_UNKNOWN_RESOURCE_ERROR_STRING;
return QUOTA_GET_RESULT_UNKNOWN_RESOURCE;
}
#ifdef HAVE_RQUOTA
if (mount_type_is_nfs(root->mount)) {
ret = root->user_disabled ? 0 :
do_rquota_user(root, &bytes_value, &bytes_limit,
&count_value, &count_limit, error_r);
if (ret == 0 && !root->group_disabled)
ret = do_rquota_group(root, &bytes_value,
&bytes_limit, &count_value,
&count_limit, error_r);
} else
#endif
{
ret = fs_quota_get_resources(root, FALSE, &bytes_value,
&bytes_limit, &count_value,
&count_limit, error_r);
if (ret == 0) {
/* fallback to group quota */
ret = fs_quota_get_resources(root, TRUE, &bytes_value,
&bytes_limit, &count_value,
&count_limit, error_r);
}
}
if (ret < 0)
return QUOTA_GET_RESULT_INTERNAL_ERROR;
else if (ret == 0)
return QUOTA_GET_RESULT_LIMITED;
if (strcasecmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
*value_r = bytes_value;
else
*value_r = count_value;
if (_root->bytes_limit != (int64_t)bytes_limit ||
_root->count_limit != (int64_t)count_limit) {
/* update limit */
_root->bytes_limit = bytes_limit;
_root->count_limit = count_limit;
/* limits have changed, so we'll need to recalculate
relative quota rules */
quota_root_recalculate_relative_rules(_root->set, bytes_limit, count_limit);
}
return QUOTA_GET_RESULT_LIMITED;
}
static int
fs_quota_update(struct quota_root *root ATTR_UNUSED,
struct quota_transaction_context *ctx ATTR_UNUSED,
const char **error_r ATTR_UNUSED)
{
return 0;
}
struct quota_backend quota_backend_fs = {
.name = "fs",
.v = {
.alloc = fs_quota_alloc,
.init = fs_quota_init,
.deinit = fs_quota_deinit,
.namespace_added = fs_quota_namespace_added,
.get_resources = fs_quota_root_get_resources,
.get_resource = fs_quota_get_resource,
.update = fs_quota_update,
.match_box = fs_quota_match_box,
}
};
#endif
|