annotate src/plugins/quota/quota-dict.c @ 9558:cf3fe573a560 HEAD

quota: If dict quota update fails, log an error.
author Timo Sirainen <tss@iki.fi>
date Tue, 13 Apr 2010 12:49:48 +0300
parents 00cd9aacd03c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9532
00cd9aacd03c Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents: 9380
diff changeset
1 /* Copyright (c) 2005-2010 Dovecot authors, see the included COPYING file */
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "str.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "dict.h"
8082
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 8030
diff changeset
6 #include "mail-user.h"
8958
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
7 #include "mail-namespace.h"
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "quota-private.h"
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include <stdlib.h>
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
4329
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
12 #define DICT_QUOTA_CURRENT_PATH DICT_PATH_PRIVATE"quota/"
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
13 #define DICT_QUOTA_CURRENT_BYTES_PATH DICT_QUOTA_CURRENT_PATH"storage"
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
14 #define DICT_QUOTA_CURRENT_COUNT_PATH DICT_QUOTA_CURRENT_PATH"messages"
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
16 struct dict_quota_root {
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 struct quota_root root;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 struct dict *dict;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 };
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
21 extern struct quota_backend quota_backend_dict;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
23 static struct quota_root *dict_quota_alloc(void)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 {
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
25 struct dict_quota_root *root;
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
26
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
27 root = i_new(struct dict_quota_root, 1);
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
28 return &root->root;
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
29 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
31 static int dict_quota_init(struct quota_root *_root, const char *args)
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
32 {
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
33 struct dict_quota_root *root = (struct dict_quota_root *)_root;
9174
eed86bcc33aa dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents: 8958
diff changeset
34 const char *username, *p, *base_dir;
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
35
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
36 p = args == NULL ? NULL : strchr(args, ':');
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
37 if (p == NULL) {
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
38 i_error("dict quota: URI missing from parameters");
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
39 return -1;
4329
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
40 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
42 username = t_strdup_until(args, p);
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
43 args = p+1;
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
44
8958
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
45 do {
7971
7a6e72d2c599 quota: All backends now take noenforcing parameter.
Timo Sirainen <tss@iki.fi>
parents: 7470
diff changeset
46 /* FIXME: pretty ugly in here. the parameters should have
7a6e72d2c599 quota: All backends now take noenforcing parameter.
Timo Sirainen <tss@iki.fi>
parents: 7470
diff changeset
47 been designed to be extensible. do it in a future version */
8958
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
48 if (strncmp(args, "noenforcing:", 12) == 0) {
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
49 _root->no_enforcing = TRUE;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
50 args += 12;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
51 continue;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
52 }
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
53 if (strncmp(args, "ns=", 3) == 0) {
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
54 p = strchr(args, ':');
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
55 if (p == NULL)
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
56 break;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
57
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
58 _root->ns_prefix = p_strdup_until(_root->pool,
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
59 args + 3, p);
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
60 args = p + 1;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
61 continue;
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
62 }
16c286aee307 Implemented support for per-namespace quotas. Can be used with public namespaces.
Timo Sirainen <tss@iki.fi>
parents: 8661
diff changeset
63 } while (0);
7971
7a6e72d2c599 quota: All backends now take noenforcing parameter.
Timo Sirainen <tss@iki.fi>
parents: 7470
diff changeset
64
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
65 if (*username == '\0')
8082
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 8030
diff changeset
66 username = _root->quota->user->username;
4329
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
67
8082
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 8030
diff changeset
68 if (_root->quota->set->debug) {
8655
46f95c498708 quota: Fixed debug logging.
Timo Sirainen <tss@iki.fi>
parents: 8590
diff changeset
69 i_info("dict quota: user=%s, uri=%s, noenforcing=%d",
7971
7a6e72d2c599 quota: All backends now take noenforcing parameter.
Timo Sirainen <tss@iki.fi>
parents: 7470
diff changeset
70 username, args, _root->no_enforcing);
7a6e72d2c599 quota: All backends now take noenforcing parameter.
Timo Sirainen <tss@iki.fi>
parents: 7470
diff changeset
71 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72
4516
aa2f73a4df26 Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4515
diff changeset
73 /* FIXME: we should use 64bit integer as datatype instead but before
aa2f73a4df26 Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4515
diff changeset
74 it can actually be used don't bother */
9174
eed86bcc33aa dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents: 8958
diff changeset
75 base_dir = getenv("BASE_DIR");
eed86bcc33aa dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents: 8958
diff changeset
76 if (base_dir == NULL)
eed86bcc33aa dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents: 8958
diff changeset
77 base_dir = PKG_RUNDIR;
eed86bcc33aa dict proxy: Use base_dir as the default dict-server location.
Timo Sirainen <tss@iki.fi>
parents: 8958
diff changeset
78 root->dict = dict_init(args, DICT_DATA_TYPE_STRING, username, base_dir);
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
79 return root->dict != NULL ? 0 : -1;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
82 static void dict_quota_deinit(struct quota_root *_root)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 {
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
84 struct dict_quota_root *root = (struct dict_quota_root *)_root;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
86 if (root->dict != NULL) {
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
87 (void)dict_wait(root->dict);
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
88 dict_deinit(&root->dict);
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
89 }
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
90 i_free(root);
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 static const char *const *
6411
6a64e64fa3a3 Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents: 5704
diff changeset
94 dict_quota_root_get_resources(struct quota_root *root ATTR_UNUSED)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 {
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
96 static const char *resources[] = {
4844
a0ba02928787 Bytes and kilobytes were somewhat mixed up everywhere. Also fixes a
Timo Sirainen <tss@iki.fi>
parents: 4516
diff changeset
97 QUOTA_NAME_STORAGE_KILOBYTES, QUOTA_NAME_MESSAGES, NULL
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
98 };
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 return resources;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 static int
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
104 dict_quota_count(struct dict_quota_root *root,
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
105 bool want_bytes, uint64_t *value_r)
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
106 {
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
107 struct dict_transaction_context *dt;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
108 uint64_t bytes, count;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
109
7470
73368107aeda Added "ignore" option to quota rules which causes the mailbox to be ignored
Timo Sirainen <tss@iki.fi>
parents: 7294
diff changeset
110 if (quota_count(&root->root, &bytes, &count) < 0)
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
111 return -1;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
112
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
113 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
114 dt = dict_transaction_begin(root->dict);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
115 dict_set(dt, DICT_QUOTA_CURRENT_BYTES_PATH, dec2str(bytes));
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
116 dict_set(dt, DICT_QUOTA_CURRENT_COUNT_PATH, dec2str(count));
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
117 } T_END;
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
118
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
119 dict_transaction_commit_async(&dt, NULL, NULL);
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
120 *value_r = want_bytes ? bytes : count;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
121 return 1;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
122 }
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
123
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
124 static int
8082
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 8030
diff changeset
125 dict_quota_get_resource(struct quota_root *_root,
db66611fd195 Added struct mail_user and fixed the code to support multiple users per process.
Timo Sirainen <tss@iki.fi>
parents: 8030
diff changeset
126 const char *name, uint64_t *value_r)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 {
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
128 struct dict_quota_root *root = (struct dict_quota_root *)_root;
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
129 bool want_bytes;
3787
40600601502e Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents: 3767
diff changeset
130 int ret;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
132 if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
133 want_bytes = TRUE;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
134 else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
135 want_bytes = FALSE;
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
136 else
4329
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
137 return 0;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
139 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
140 const char *value;
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
141
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
142 ret = dict_lookup(root->dict, unsafe_data_stack_pool,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
143 want_bytes ? DICT_QUOTA_CURRENT_BYTES_PATH :
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
144 DICT_QUOTA_CURRENT_COUNT_PATH, &value);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
145 if (ret < 0)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
146 *value_r = 0;
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
147 else {
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
148 long long tmp;
5127
97193e15d688 Don't break if current quota value is negative.
Timo Sirainen <tss@iki.fi>
parents: 4870
diff changeset
149
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
150 /* recalculate quota if it's negative or if it
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
151 wasn't found */
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
152 tmp = ret == 0 ? -1 : strtoll(value, NULL, 10);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
153 if (tmp >= 0)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
154 *value_r = tmp;
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
155 else {
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
156 ret = dict_quota_count(root, want_bytes,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
157 value_r);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
158 }
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
159 }
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
160 } T_END;
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
161 return ret;
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
164 static void dict_quota_update_callback(int ret, void *context)
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
165 {
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
166 struct dict_quota_root *root = context;
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
167 uint64_t value;
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
168
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
169 if (ret == 0) {
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
170 /* row doesn't exist, need to recalculate it */
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
171 (void)dict_quota_count(root, TRUE, &value);
9558
cf3fe573a560 quota: If dict quota update fails, log an error.
Timo Sirainen <tss@iki.fi>
parents: 9532
diff changeset
172 } else if (ret < 0) {
cf3fe573a560 quota: If dict quota update fails, log an error.
Timo Sirainen <tss@iki.fi>
parents: 9532
diff changeset
173 i_error("dict quota: Quota update failed, it's now desynced");
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
174 }
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
175 }
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
176
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 static int
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
178 dict_quota_update(struct quota_root *_root,
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
179 struct quota_transaction_context *ctx)
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 {
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
181 struct dict_quota_root *root = (struct dict_quota_root *) _root;
4329
72361670eaab Get the quota limits from plugin parameters instead of from the dictionary
Timo Sirainen <tss@iki.fi>
parents: 4086
diff changeset
182 struct dict_transaction_context *dt;
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
183 uint64_t value;
5566
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
184
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
185 if (ctx->recalculate) {
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
186 if (dict_quota_count(root, TRUE, &value) < 0)
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
187 return -1;
5566
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
188 } else {
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
189 dt = dict_transaction_begin(root->dict);
5566
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
190 if (ctx->bytes_used != 0) {
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
191 dict_atomic_inc(dt, DICT_QUOTA_CURRENT_BYTES_PATH,
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
192 ctx->bytes_used);
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
193 }
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
194 if (ctx->count_used != 0) {
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
195 dict_atomic_inc(dt, DICT_QUOTA_CURRENT_COUNT_PATH,
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
196 ctx->count_used);
88307a648e0e Use sync_notify() callback to count expunges. This makes the calculation
Timo Sirainen <tss@iki.fi>
parents: 5492
diff changeset
197 }
9362
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
198 dict_transaction_commit_async(&dt, dict_quota_update_callback,
ea522175c549 dict quota: When updating quota, recalculate it when necessary.
Timo Sirainen <tss@iki.fi>
parents: 9174
diff changeset
199 root);
4870
e92b3eaab490 dict quota: If dictionary doesn't yet contain the quota, calculate it by
Timo Sirainen <tss@iki.fi>
parents: 4844
diff changeset
200 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 return 0;
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 }
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
204 struct quota_backend quota_backend_dict = {
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 "dict",
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
207 {
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
208 dict_quota_alloc,
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
209 dict_quota_init,
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
210 dict_quota_deinit,
4509
e5e79558ac2d Added support for multiple quota roots, rules and storages. The configuration
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4392
diff changeset
211 NULL,
5704
4bf8438b497f Allow backend-specific quota rules with backend=<string>.
Timo Sirainen <tss@iki.fi>
parents: 5566
diff changeset
212 NULL,
9380
fe063e0d7109 maildir quota: Limits weren't read early enough from maildirsize file.
Timo Sirainen <tss@iki.fi>
parents: 9362
diff changeset
213 NULL,
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
214 dict_quota_root_get_resources,
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
215 dict_quota_get_resource,
8029
796307638449 FS quota: Handle quota only for the quota roots which apply to the mailbox
Timo Sirainen <tss@iki.fi>
parents: 7971
diff changeset
216 dict_quota_update,
796307638449 FS quota: Handle quota only for the quota roots which apply to the mailbox
Timo Sirainen <tss@iki.fi>
parents: 7971
diff changeset
217 NULL
4084
51306837698b Quota API redesign. Still not perfect, but better than before. Quota backends no longer need to be told where the mails exist. fs backend doesn't work correctly with multiple partitions (if you've multiple namespaces).
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4044
diff changeset
218 }
3738
732b62dc1976 Added beginnings of plugin infrastructure. TODO: These could be optionally
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 };