From f4ab67232100dfc7d3f4f69e9d22e9e992060423 Mon Sep 17 00:00:00 2001 From: Markus Valentin Date: Tue, 7 Jul 2020 14:39:22 +0200 Subject: [PATCH] quota: Introduce per quota-backend events --- src/plugins/quota/quota-count.c | 2 ++ src/plugins/quota/quota-dict.c | 15 +++++------ src/plugins/quota/quota-dirsize.c | 1 + src/plugins/quota/quota-fs.c | 41 ++++++++++++++++--------------- src/plugins/quota/quota-imapc.c | 13 +++++----- src/plugins/quota/quota-maildir.c | 27 ++++++++++---------- src/plugins/quota/quota-private.h | 1 + src/plugins/quota/quota.c | 6 +++++ 8 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/plugins/quota/quota-count.c b/src/plugins/quota/quota-count.c index 87bbdc7315..cb869fc639 100644 --- a/src/plugins/quota/quota-count.c +++ b/src/plugins/quota/quota-count.c @@ -242,6 +242,8 @@ static int count_quota_init(struct quota_root *root, const char *args, *error_r = "quota count backend requires quota_vsizes=yes"; return -1; } + event_set_append_log_prefix(root->backend.event, "quota-count: "); + root->auto_updating = TRUE; return quota_root_default_init(root, args, error_r); } diff --git a/src/plugins/quota/quota-dict.c b/src/plugins/quota/quota-dict.c index 690dc76881..37549b5f74 100644 --- a/src/plugins/quota/quota-dict.c +++ b/src/plugins/quota/quota-dict.c @@ -42,6 +42,8 @@ static int dict_quota_init(struct quota_root *_root, const char *args, struct dict_settings set; const char *username, *p, *error; + event_set_append_log_prefix(_root->backend.event, "quota-dict: "); + const struct quota_param_parser dict_params[] = { {.param_name = "no-unset", .param_handler = handle_nounset_param}, quota_param_hidden, quota_param_ignoreunlimited, quota_param_noenforcing, quota_param_ns, @@ -63,8 +65,7 @@ static int dict_quota_init(struct quota_root *_root, const char *args, if (*username == '\0') username = _root->quota->user->username; - e_debug(_root->quota->event, - "dict quota: user=%s, uri=%s, noenforcing=%d", + e_debug(_root->backend.event, "user=%s, uri=%s, noenforcing=%d", username, args, _root->no_enforcing ? 1 : 0); /* FIXME: we should use 64bit integer as datatype instead but before @@ -129,7 +130,7 @@ dict_quota_count(struct dict_quota_root *root, dict_set(dt, DICT_QUOTA_CURRENT_BYTES_PATH, dec2str(bytes)); dict_set(dt, DICT_QUOTA_CURRENT_COUNT_PATH, dec2str(count)); - e_debug(root->root.quota->event, "dict quota: Quota recalculated: " + e_debug(root->root.backend.event, "Quota recalculated: " "count=%"PRIu64" bytes=%"PRIu64, count, bytes); dict_transaction_commit_async(&dt, NULL, NULL); @@ -186,8 +187,8 @@ static void dict_quota_recalc_timeout(struct dict_quota_root *root) timeout_remove(&root->to_update); if (dict_quota_count(root, TRUE, &value, &error) <= QUOTA_GET_RESULT_INTERNAL_ERROR) - e_error(root->root.quota->event, - "quota-dict: Recalculation failed: %s", error); + e_error(root->root.backend.event, + "Recalculation failed: %s", error); } static void dict_quota_update_callback(const struct dict_commit_result *result, @@ -200,8 +201,8 @@ static void dict_quota_update_callback(const struct dict_commit_result *result, if (root->to_update == NULL) root->to_update = timeout_add_short(0, dict_quota_recalc_timeout, root); } else if (result->ret < 0) { - e_error(root->root.quota->event, - "dict quota: Quota update failed: %s " + e_error(root->root.backend.event, + "Quota update failed: %s " "- Quota is now desynced", result->error); } } diff --git a/src/plugins/quota/quota-dirsize.c b/src/plugins/quota/quota-dirsize.c index 3d81c2a86c..a8305d8b58 100644 --- a/src/plugins/quota/quota-dirsize.c +++ b/src/plugins/quota/quota-dirsize.c @@ -29,6 +29,7 @@ static int dirsize_quota_init(struct quota_root *root, const char *args, const char **error_r) { root->auto_updating = TRUE; + event_set_append_log_prefix(root->backend.event, "quota-dirsize: "); return quota_root_default_init(root, args, error_r); } diff --git a/src/plugins/quota/quota-fs.c b/src/plugins/quota/quota-fs.c index e519b4fbaf..57620c12dd 100644 --- a/src/plugins/quota/quota-fs.c +++ b/src/plugins/quota/quota-fs.c @@ -131,6 +131,8 @@ static int fs_quota_init(struct quota_root *_root, const char *args, {.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; @@ -163,8 +165,7 @@ static void fs_quota_deinit(struct quota_root *_root) i_free(root); } -static struct fs_quota_mountpoint * -fs_quota_mountpoint_get(struct quota *quota, const char *dir) +static struct fs_quota_mountpoint *fs_quota_mountpoint_get(const char *dir) { struct fs_quota_mountpoint *mount; struct mountpoint point; @@ -186,8 +187,8 @@ fs_quota_mountpoint_get(struct quota *quota, const char *dir) if (mount_type_is_nfs(mount)) { if (strchr(mount->device_path, ':') == NULL) { - e_error(quota->event, - "quota-fs: %s is not a valid NFS device path", + e_error(quota_backend_fs.event, + "%s is not a valid NFS device path", mount->device_path); fs_quota_mountpoint_free(mount); return NULL; @@ -240,16 +241,16 @@ fs_quota_mount_init(struct fs_quota_root *root, 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.quota->event, + e_error(root->root.backend.event, "open(%s) failed: %m", mount->path); } #endif root->mount = mount; - e_debug(root->root.quota->event, "fs quota add mailbox dir = %s", dir); - e_debug(root->root.quota->event, "fs quota block device = %s", mount->device_path); - e_debug(root->root.quota->event, "fs quota mount point = %s", mount->mount_path); - e_debug(root->root.quota->event, "fs quota mount type = %s", mount->type); + 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); @@ -276,7 +277,7 @@ static void fs_quota_add_missing_mounts(struct quota *quota) root->storage_mount_path == NULL || root->mount != NULL) continue; - mount = fs_quota_mountpoint_get(quota, root->storage_mount_path); + mount = fs_quota_mountpoint_get(root->storage_mount_path); if (mount != NULL) { fs_quota_mount_init(root, mount, root->storage_mount_path); @@ -295,7 +296,7 @@ static void fs_quota_namespace_added(struct quota *quota, &dir)) mount = NULL; else - mount = fs_quota_mountpoint_get(quota, dir); + mount = fs_quota_mountpoint_get(dir); if (mount != NULL) { root = fs_quota_root_find_mountpoint(quota, mount); if (root != NULL && root->mount == NULL) @@ -392,7 +393,7 @@ do_rquota_user(struct fs_quota_root *root, path++; } - e_debug(root->root.quota->event, "quota-fs: host=%s, path=%s, uid=%s", + 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 */ @@ -436,7 +437,7 @@ do_rquota_user(struct fs_quota_root *root, 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.quota->event, "quota-fs: uid=%s, bytes=%"PRIu64"/%"PRIu64" " + e_debug(root->root.backend.event, "uid=%s, bytes=%"PRIu64"/%"PRIu64" " "files=%"PRIu64"/%"PRIu64, dec2str(root->uid), *bytes_value_r, *bytes_limit_r, @@ -444,7 +445,7 @@ do_rquota_user(struct fs_quota_root *root, return 1; } case Q_NOQUOTA: - e_debug(root->root.quota->event, "quota-fs: uid=%s, limit=unlimited", + e_debug(root->root.backend.event, "uid=%s, limit=unlimited", dec2str(root->uid)); fs_quota_root_disable(root, FALSE); return 0; @@ -483,7 +484,7 @@ do_rquota_group(struct fs_quota_root *root ATTR_UNUSED, host = t_strdup_until(mount->device_path, path); path++; - e_debug(root->root.quota->event, "quota-fs: host=%s, path=%s, gid=%s", + 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 */ @@ -527,7 +528,7 @@ do_rquota_group(struct fs_quota_root *root ATTR_UNUSED, 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.quota->event, "quota-fs: gid=%s, bytes=%"PRIu64"/%"PRIu64" " + e_debug(root->root.backend.event, "gid=%s, bytes=%"PRIu64"/%"PRIu64" " "files=%"PRIu64"/%"PRIu64, dec2str(root->gid), *bytes_value_r, *bytes_limit_r, @@ -535,7 +536,7 @@ do_rquota_group(struct fs_quota_root *root ATTR_UNUSED, return 1; } case Q_NOQUOTA: - e_debug(root->root.quota->event, "quota-fs: gid=%s, limit=unlimited", + e_debug(root->root.backend.event, "gid=%s, limit=unlimited", dec2str(root->gid)); fs_quota_root_disable(root, TRUE); return 0; @@ -855,17 +856,17 @@ static bool fs_quota_match_box(struct quota_root *_root, struct mailbox *box) return FALSE; if (stat(mailbox_path, &mst) < 0) { if (errno != ENOENT) - e_error(_root->quota->event, + e_error(_root->backend.event, "stat(%s) failed: %m", mailbox_path); return FALSE; } if (stat(root->storage_mount_path, &rst) < 0) { - e_debug(_root->quota->event, "stat(%s) failed: %m", + 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->quota->event, "box=%s mount=%s match=%s", mailbox_path, + e_debug(_root->backend.event, "box=%s mount=%s match=%s", mailbox_path, root->storage_mount_path, match ? "yes" : "no"); return match; } diff --git a/src/plugins/quota/quota-imapc.c b/src/plugins/quota/quota-imapc.c index 870e1b6044..f453eae442 100644 --- a/src/plugins/quota/quota-imapc.c +++ b/src/plugins/quota/quota-imapc.c @@ -68,6 +68,7 @@ static int imapc_quota_init(struct quota_root *_root, const char *args, }; _root->auto_updating = TRUE; + event_set_append_log_prefix(root->root.backend.event, "quota-imapc: "); if (quota_parse_parameters(_root, &args, error_r, imapc_params, TRUE) < 0) return -1; @@ -222,8 +223,8 @@ static bool imapc_quota_client_init(struct imapc_quota_root *root) /* non-imapc namespace, skip */ if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_NOQUOTA) == 0) { - e_warning(root->root.quota->event, - "quota: Namespace '%s' is not imapc, " + e_warning(root->root.backend.event, + "Namespace '%s' is not imapc, " "skipping for imapc quota", root->imapc_ns->prefix); } @@ -254,8 +255,8 @@ imapc_quota_refresh_update(struct quota *quota, const struct imapc_quota_refresh_root *refresh_root; if (array_count(&refresh->roots) == 0) { - e_error(quota->event, - "quota: imapc didn't return any QUOTA results"); + e_error(quota_backend_imapc.event, + "imapc didn't return any QUOTA results"); return; } /* use the first quota root for everything */ @@ -388,8 +389,8 @@ static int imapc_quota_refresh(struct imapc_quota_root *root, } if ((capa & IMAPC_CAPABILITY_QUOTA) == 0) { /* no QUOTA capability - disable quota */ - e_warning(root->root.quota->event, - "quota: Remote IMAP server doesn't support QUOTA - disabling"); + e_warning(root->root.backend.event, + "Remote IMAP server doesn't support QUOTA - disabling"); root->client = NULL; return 0; } diff --git a/src/plugins/quota/quota-maildir.c b/src/plugins/quota/quota-maildir.c index b3391e5a80..fbc2385687 100644 --- a/src/plugins/quota/quota-maildir.c +++ b/src/plugins/quota/quota-maildir.c @@ -188,7 +188,7 @@ maildir_list_next(struct maildir_list_context *ctx, time_t *mtime_r) /* ignore if the directory got lost, stale or if it was actually a file and not a directory */ if (errno != ENOENT && errno != ESTALE && errno != ENOTDIR) { - e_error(ctx->root->root.quota->event, + e_error(ctx->root->root.backend.event, "stat(%s) failed: %m", str_c(ctx->path)); ctx->state = 0; } @@ -279,7 +279,7 @@ static int maildirsize_write(struct maildir_quota_root *root, const char *path) perm.file_create_gid, perm.file_create_gid_origin) < 0 && errno != EEXIST) { - e_error(root->root.quota->event, + e_error(root->root.backend.event, "mkdir_parents(%s) failed: %m", dir); return -1; } @@ -289,7 +289,7 @@ static int maildirsize_write(struct maildir_quota_root *root, const char *path) perm.file_create_gid_origin); } if (fd == -1) { - e_error(root->root.quota->event, + e_error(root->root.backend.event, "safe_mkstemp(%s) failed: %m", path); return -1; } @@ -307,7 +307,7 @@ static int maildirsize_write(struct maildir_quota_root *root, const char *path) str_printfa(str, "\n%"PRIu64" %"PRIu64"\n", root->total_bytes, root->total_count); if (write_full(fd, str_data(str), str_len(str)) < 0) { - e_error(_root->quota->event, + e_error(root->root.backend.event, "write_full(%s) failed: %m", str_c(temp_path)); i_close_fd(&fd); i_unlink(str_c(temp_path)); @@ -316,7 +316,7 @@ static int maildirsize_write(struct maildir_quota_root *root, const char *path) i_close_fd(&fd); if (rename(str_c(temp_path), path) < 0) { - e_error(_root->quota->event, + e_error(root->root.backend.event, "rename(%s, %s) failed: %m", str_c(temp_path), path); i_unlink_if_exists(str_c(temp_path)); return -1; @@ -363,7 +363,7 @@ static void maildirsize_rebuild_later(struct maildir_quota_root *root) if (unlink(root->maildirsize_path) < 0 && errno != ENOENT && errno != ESTALE) - e_error(root->root.quota->event, + e_error(root->root.backend.event, "unlink(%s) failed: %m", root->maildirsize_path); } @@ -663,7 +663,7 @@ static bool maildirquota_limits_init(struct maildir_quota_root *root) /* non-maildir namespace, skip */ if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_NOQUOTA) == 0) { - e_warning(root->root.quota->event, + e_warning(root->root.backend.event, "Namespace '%s' is not Maildir, " "skipping for Maildir++ quota", root->maildirsize_ns->prefix); @@ -750,7 +750,7 @@ static int maildirsize_update(struct maildir_quota_root *root, if (errno == ESTALE) { /* deleted/replaced already, ignore */ } else { - e_error(root->root.quota->event, + e_error(root->root.backend.event, "write_full(%s) failed: %m", root->maildirsize_path); } @@ -759,7 +759,7 @@ static int maildirsize_update(struct maildir_quota_root *root, if (close(root->fd) < 0) { ret = -1; if (errno != ESTALE) - e_error(root->root.quota->event, + e_error(root->root.backend.event, "close(%s) failed: %m", root->maildirsize_path); } root->fd = -1; @@ -779,6 +779,7 @@ static struct quota_root *maildir_quota_alloc(void) static int maildir_quota_init(struct quota_root *_root, const char *args, const char **error_r) { + event_set_append_log_prefix(_root->backend.event, "quota-maildir: "); return quota_root_default_init(_root, args, error_r); } @@ -908,7 +909,7 @@ maildir_quota_update(struct quota_root *_root, it doesn't exist. */ if (maildirquota_refresh(root, &recalculated, &error) < 0) { *error_r = t_strdup_printf( - "quota-maildir: Could not update storage usage data: %s", + "Could not update storage usage data: %s", error); return -1; } @@ -918,13 +919,11 @@ maildir_quota_update(struct quota_root *_root, we wanted to do. */ } else if (root->fd == -1) { if (maildirsize_recalculate(root, &error) < 0) - e_error(_root->quota->event, - "quota-maildir: %s", error); + e_error(root->root.backend.event, "%s", error); } else if (ctx->recalculate != QUOTA_RECALCULATE_DONT) { i_close_fd(&root->fd); if (maildirsize_recalculate(root, &error) < 0) - e_error(_root->quota->event, - "quota-maildir: %s", error); + e_error(root->root.backend.event, "%s", error); } else if (maildirsize_update(root, ctx->count_used, ctx->bytes_used) < 0) { i_close_fd(&root->fd); maildirsize_rebuild_later(root); diff --git a/src/plugins/quota/quota-private.h b/src/plugins/quota/quota-private.h index 665795c74f..ac091d3789 100644 --- a/src/plugins/quota/quota-private.h +++ b/src/plugins/quota/quota-private.h @@ -86,6 +86,7 @@ struct quota_backend_vfuncs { struct quota_backend { /* quota backends equal if backend1.name == backend2.name */ const char *name; + struct event *event; struct quota_backend_vfuncs v; }; diff --git a/src/plugins/quota/quota.c b/src/plugins/quota/quota.c index 81cb3334a9..f7a46ddc6d 100644 --- a/src/plugins/quota/quota.c +++ b/src/plugins/quota/quota.c @@ -376,6 +376,7 @@ static void quota_root_deinit(struct quota_root *root) if (root->limit_set_dict != NULL) dict_deinit(&root->limit_set_dict); + event_unref(&root->backend.event); root->backend.v.deinit(root); pool_unref(&pool); } @@ -411,9 +412,14 @@ quota_root_init(struct quota_root_settings *root_set, struct quota *quota, sizeof(void *), 10); if (root->backend.v.init != NULL) { + root->backend.event = event_create(quota->event); + event_set_forced_debug(root->backend.event, root->quota->set->debug); + if (root->backend.v.init(root, root_set->args, error_r) < 0) { *error_r = t_strdup_printf("%s quota init failed: %s", root->backend.name, *error_r); + + event_unref(&root->backend.event); return -1; } } else { -- 2.47.3