summaryrefslogtreecommitdiffstats
path: root/src/plugins/quota-clone/quota-clone-plugin.c
blob: 5f5efa41ad2c777808b204b5244e6d85e5165285 (plain)
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
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "module-context.h"
#include "ioloop.h"
#include "dict.h"
#include "mail-storage-private.h"
#include "quota.h"
#include "quota-clone-plugin.h"

/* If mailbox is kept open for this many milliseconds after quota update,
   flush quota-clone. */
#define QUOTA_CLONE_FLUSH_DELAY_MSECS (10*1000)

#define DICT_QUOTA_CLONE_PATH DICT_PATH_PRIVATE"quota/"
#define DICT_QUOTA_CLONE_BYTES_PATH DICT_QUOTA_CLONE_PATH"storage"
#define DICT_QUOTA_CLONE_COUNT_PATH DICT_QUOTA_CLONE_PATH"messages"

#define QUOTA_CLONE_USER_CONTEXT_REQUIRE(obj) \
	MODULE_CONTEXT_REQUIRE(obj, quota_clone_user_module)
#define QUOTA_CLONE_USER_CONTEXT(obj) \
	MODULE_CONTEXT(obj, quota_clone_user_module)
#define QUOTA_CLONE_CONTEXT(obj) \
	MODULE_CONTEXT_REQUIRE(obj, quota_clone_storage_module)

static MODULE_CONTEXT_DEFINE_INIT(quota_clone_user_module,
				  &mail_user_module_register);
static MODULE_CONTEXT_DEFINE_INIT(quota_clone_storage_module,
				  &mail_storage_module_register);

struct quota_clone_user {
	union mail_user_module_context module_ctx;
	struct dict *dict;
	struct timeout *to_quota_flush;
	bool quota_changed;
	bool quota_flushing;
};

static void
quota_clone_dict_commit(const struct dict_commit_result *result,
			struct quota_clone_user *quser)
{
	switch (result->ret) {
	case DICT_COMMIT_RET_OK:
	case DICT_COMMIT_RET_NOTFOUND:
		if (!quser->quota_changed)
			timeout_remove(&quser->to_quota_flush);
		break;
	case DICT_COMMIT_RET_FAILED:
		quser->quota_changed = TRUE;
		i_error("quota_clone_dict: Failed to write value: %s",
			result->error);
		break;
	case DICT_COMMIT_RET_WRITE_UNCERTAIN:
		quser->quota_changed = TRUE;
		i_error("quota_clone_dict: Write was unconfirmed (timeout or disconnect): %s",
			result->error);
		break;
	}

	quser->quota_flushing = FALSE;
}

static bool quota_clone_flush_real(struct mail_user *user)
{
	struct quota_clone_user *quser =
		QUOTA_CLONE_USER_CONTEXT_REQUIRE(user);
	struct dict_transaction_context *trans;
	struct quota_root_iter *iter;
	struct quota_root *root;
	uint64_t bytes_value, count_value, limit;
	const char *error;
	enum quota_get_result bytes_res, count_res;

	/* we'll clone the first quota root */
	iter = quota_root_iter_init_user(user);
	root = quota_root_iter_next(iter);
	quota_root_iter_deinit(&iter);
	if (root == NULL) {
		/* no quota roots defined - ignore */
		quser->quota_changed = FALSE;
		return TRUE;
	}

	/* get new values first */
	bytes_res = quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
				       &bytes_value, &limit, &error);
	if (bytes_res == QUOTA_GET_RESULT_INTERNAL_ERROR) {
		i_error("quota_clone_plugin: "
			"Failed to get quota resource "QUOTA_NAME_STORAGE_BYTES": %s",
			error);
		return TRUE;
	}
	count_res = quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
				       &count_value, &limit, &error);
	if (count_res == QUOTA_GET_RESULT_INTERNAL_ERROR) {
		i_error("quota_clone_plugin: "
			"Failed to get quota resource "QUOTA_NAME_MESSAGES": %s",
			error);
		return TRUE;
	}
	if (bytes_res == QUOTA_GET_RESULT_UNKNOWN_RESOURCE &&
	    count_res == QUOTA_GET_RESULT_UNKNOWN_RESOURCE) {
		/* quota resources don't exist - no point in updating it */
		return TRUE;
	}
	if (bytes_res == QUOTA_GET_RESULT_BACKGROUND_CALC &&
	    count_res == QUOTA_GET_RESULT_BACKGROUND_CALC) {
		/* Blocked by an ongoing quota calculation - try again later */
		quser->quota_flushing = FALSE;
		return FALSE;
	}

	/* Then update the resources that exist. The resources' existence can't
	   change unless the quota backend is changed, so we don't worry about
	   the special case of lookup changing from
	   RESULT_LIMITED/RESULT_UNLIMITED to RESULT_UNKNOWN_RESOURCE, which
	   leaves the old value unchanged. */
	const struct dict_op_settings *set = mail_user_get_dict_op_settings(user);
	trans = dict_transaction_begin(quser->dict, set);
	if (bytes_res == QUOTA_GET_RESULT_LIMITED ||
	    bytes_res == QUOTA_GET_RESULT_UNLIMITED) {
		dict_set(trans, DICT_QUOTA_CLONE_BYTES_PATH,
			 t_strdup_printf("%"PRIu64, bytes_value));
	}
	if (count_res == QUOTA_GET_RESULT_LIMITED ||
	    count_res == QUOTA_GET_RESULT_UNLIMITED) {
		dict_set(trans, DICT_QUOTA_CLONE_COUNT_PATH,
			 t_strdup_printf("%"PRIu64, count_value));
	}
	quser->quota_changed = FALSE;
	dict_transaction_commit_async(&trans, quota_clone_dict_commit, quser);
	return FALSE;
}

static void quota_clone_flush(struct mail_user *user)
{
	struct quota_clone_user *quser =
		QUOTA_CLONE_USER_CONTEXT_REQUIRE(user);

	if (quser->quota_changed) {
		i_assert(quser->to_quota_flush != NULL);
		if (quser->quota_flushing) {
			/* async quota commit is running in background. timeout is still
			   active, so another update will be done later. */
		} else {
			quser->quota_flushing = TRUE;
			/* Returns TRUE if flushing action is complete. */
			if (quota_clone_flush_real(user)) {
				quser->quota_flushing = FALSE;
				timeout_remove(&quser->to_quota_flush);
			}
		}
	} else {
		timeout_remove(&quser->to_quota_flush);
	}
}

static struct mail_user *quota_mailbox_get_user(struct mailbox *box)
{
	struct mail_namespace *ns = mailbox_list_get_namespace(box->list);
	return ns->owner != NULL ? ns->owner : ns->user;
}

static void quota_clone_changed(struct mailbox *box)
{
	struct mail_user *user = quota_mailbox_get_user(box);
	struct quota_clone_user *quser =
		QUOTA_CLONE_USER_CONTEXT_REQUIRE(user);

	quser->quota_changed = TRUE;
	if (quser->to_quota_flush == NULL) {
		quser->to_quota_flush = timeout_add(QUOTA_CLONE_FLUSH_DELAY_MSECS,
						    quota_clone_flush, user);
	}
}

static int quota_clone_save_finish(struct mail_save_context *ctx)
{
	union mailbox_module_context *qbox =
		QUOTA_CLONE_CONTEXT(ctx->transaction->box);

	quota_clone_changed(ctx->transaction->box);
	return qbox->super.save_finish(ctx);
}

static int
quota_clone_copy(struct mail_save_context *ctx, struct mail *mail)
{
	union mailbox_module_context *qbox =
		QUOTA_CLONE_CONTEXT(ctx->transaction->box);

	quota_clone_changed(ctx->transaction->box);
	return qbox->super.copy(ctx, mail);
}

static void
quota_clone_mailbox_sync_notify(struct mailbox *box, uint32_t uid,
				enum mailbox_sync_type sync_type)
{
	union mailbox_module_context *qbox = QUOTA_CLONE_CONTEXT(box);

	if (qbox->super.sync_notify != NULL)
		qbox->super.sync_notify(box, uid, sync_type);

	if (sync_type == MAILBOX_SYNC_TYPE_EXPUNGE)
		quota_clone_changed(box);
}

static void quota_clone_mailbox_allocated(struct mailbox *box)
{
	struct quota_clone_user *quser =
		QUOTA_CLONE_USER_CONTEXT(box->storage->user);
	struct mailbox_vfuncs *v = box->vlast;
	union mailbox_module_context *qbox;

	if (quser == NULL)
		return;

	qbox = p_new(box->pool, union mailbox_module_context, 1);
	qbox->super = *v;
	box->vlast = &qbox->super;

	v->save_finish = quota_clone_save_finish;
	v->copy = quota_clone_copy;
	v->sync_notify = quota_clone_mailbox_sync_notify;
	MODULE_CONTEXT_SET_SELF(box, quota_clone_storage_module, qbox);
}

static void quota_clone_mail_user_deinit_pre(struct mail_user *user)
{
	struct quota_clone_user *quser = QUOTA_CLONE_USER_CONTEXT_REQUIRE(user);

	dict_wait(quser->dict);
	/* Check once more if quota needs to be updated. This needs to be done
	   in deinit_pre(), because at deinit() the quota is already
	   deinitialized. */
	if (quser->to_quota_flush != NULL) {
		i_assert(!quser->quota_flushing);
		quota_clone_flush(user);
		dict_wait(quser->dict);
		/* If dict update fails or background calculation is running,
		   the timeout is still set. Just forget about it. */
		timeout_remove(&quser->to_quota_flush);
	}
	quser->module_ctx.super.deinit_pre(user);
}

static void quota_clone_mail_user_deinit(struct mail_user *user)
{
	struct quota_clone_user *quser = QUOTA_CLONE_USER_CONTEXT_REQUIRE(user);

	/* wait once more, just in case something changed quota during
	   deinit_pre() */
	dict_wait(quser->dict);
	i_assert(quser->to_quota_flush == NULL);
	dict_deinit(&quser->dict);
	quser->module_ctx.super.deinit(user);
}

static void quota_clone_mail_user_created(struct mail_user *user)
{
	struct quota_clone_user *quser;
	struct mail_user_vfuncs *v = user->vlast;
	struct dict_settings dict_set;
	struct dict *dict;
	const char *uri, *error;

	uri = mail_user_plugin_getenv(user, "quota_clone_dict");
	if (uri == NULL || uri[0] == '\0') {
		e_debug(user->event, "The quota_clone_dict setting is missing from configuration");
		return;
	}

	i_zero(&dict_set);
	dict_set.base_dir = user->set->base_dir;
	dict_set.event_parent = user->event;
	if (dict_init(uri, &dict_set, &dict, &error) < 0) {
		i_error("quota_clone_dict: Failed to initialize '%s': %s",
			uri, error);
		return;
	}

	quser = p_new(user->pool, struct quota_clone_user, 1);
	quser->module_ctx.super = *v;
	user->vlast = &quser->module_ctx.super;
	v->deinit_pre = quota_clone_mail_user_deinit_pre;
	v->deinit = quota_clone_mail_user_deinit;
	quser->dict = dict;
	MODULE_CONTEXT_SET(user, quota_clone_user_module, quser);
}

static struct mail_storage_hooks quota_clone_mail_storage_hooks = {
	.mailbox_allocated = quota_clone_mailbox_allocated,
	.mail_user_created = quota_clone_mail_user_created
};

void quota_clone_plugin_init(struct module *module ATTR_UNUSED)
{
	mail_storage_hooks_add(module, &quota_clone_mail_storage_hooks);
}

void quota_clone_plugin_deinit(void)
{
	mail_storage_hooks_remove(&quota_clone_mail_storage_hooks);
}

const char *quota_clone_plugin_dependencies[] = { "quota", NULL };