quota.c revision 0e5819a061034f1636b124c03a89f67d37c852b1
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
cb2b5a7d6e7e651191bf9ee1eda94a6e207288b0Timo Sirainen "Quota exceeded (mailbox for user is full)"
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenunsigned int quota_module_id = 0;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenextern struct quota_backend quota_backend_dict;
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainenextern struct quota_backend quota_backend_dirsize;
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainenextern struct quota_backend quota_backend_maildir;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic const struct quota_backend *quota_backends[] = {
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic int quota_default_test_alloc(struct quota_transaction_context *ctx,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_settings *quota_settings_init(void)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen pool = pool_alloconly_create("quota settings", 256);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set = p_new(pool, struct quota_settings, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->test_alloc = quota_default_test_alloc;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->quota_exceeded_msg = getenv("QUOTA_EXCEEDED_MESSAGE");
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_set->quota_exceeded_msg = DEFAULT_QUOTA_EXCEEDED_MSG;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_settings_deinit(struct quota_settings **_quota_set)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_settings *quota_set = *_quota_set;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic const struct quota_backend *quota_backend_find(const char *name)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen unsigned int i;
fd1f0e9ef52b3e157cfd1a01c464c2ac7458ab17Timo Sirainen for (i = 0; i < N_ELEMENTS(quota_backends); i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (strcmp(quota_backends[i]->name, name) == 0)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_settings_init(struct quota_settings *quota_set, const char *root_def)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* <backend>[:<quota root name>[:<backend args>]] */
d9b8c65d0a0ffc709ba7d23c449dbf2f46b10674Timo Sirainen i_error("Unknown quota backend: %s", backend_name);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set = p_new(quota_set->pool, struct quota_root_settings, 1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* save root's name */
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name = p_strdup(quota_set->pool, args);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->args = p_strdup(quota_set->pool, args);
e3077468777f5d324224365e34d7bbc449168e52Timo Sirainen i_info("Quota root: name=%s backend=%s args=%s",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_set->name, backend_name, args == NULL ? "" : args);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_array_init(&root_set->rules, quota_set->pool, 4);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen p_array_init(&root_set->warning_rules, quota_set->pool, 4);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_append("a_set->root_sets, &root_set, 1);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstatic struct quota_root *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_init(struct quota_root_settings *root_set, struct quota *quota)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen const char *const *tmp;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root->pool = pool_alloconly_create("quota root", 512);
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen root->bytes_limit = root_set->default_rule.bytes_limit;
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen root->count_limit = root_set->default_rule.count_limit;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen array_create(&root->quota_module_contexts, root->pool,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen sizeof(void *), 10);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (root->backend.v.init(root, root_set->args) < 0)
00589bcc8452de52d8a4f753be402487eb83ca41Timo Sirainen i_fatal("Quota root %s init() failed", root_set->name);
65e14cef911d5d3fac8993c8a76911a587f05cd7Timo Sirainen "Unknown parameter: %s",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstatic void quota_root_deinit(struct quota_root *root)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota *quota_init(struct quota_settings *quota_set,
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen root_sets = array_get("a_set->root_sets, &count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen unsigned int i, count;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen for (i = 0; i < count; i++)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_root_rule_find(struct quota_root_settings *root_set, const char *name)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, count;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rules = array_get_modifiable(&root_set->rules, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_rule_parse_percentage(struct quota_root_settings *root_set,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen "Invalid rule percentage: %lld", (long long)percentage);
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen *error_r = "Default rule can't be a percentage";
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainenquota_rule_recalculate_relative_rules(struct quota_rule *rule,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->bytes_limit = default_rule->bytes_limit *
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen rule->count_limit = default_rule->count_limit *
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_root_recalculate_relative_rules(struct quota_root_settings *root_set)
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen unsigned int i, count;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rules = array_get_modifiable(&root_set->rules, &count);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen quota_rule_recalculate_relative_rules(&rules[i],
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warning_rules = array_get_modifiable(&root_set->warning_rules, &count);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen quota_rule_recalculate_relative_rules(&warning_rules[i].rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenquota_rule_parse_limits(struct quota_root_settings *root_set,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const char **args;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else if (strncmp(*args, "bytes=", 6) == 0) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else if (strncmp(*args, "messages=", 9) == 0) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen switch (i_toupper(*p)) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen /* default */
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_rule_parse_percentage(root_set, rule, limit,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen *error_r = p_strdup_printf(root_set->set->pool,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenint quota_root_add_rule(struct quota_root_settings *root_set,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const char *p, *mailbox_name;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* <mailbox name>:<quota limits> */
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen rule = quota_root_rule_find(root_set, mailbox_name);
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen if (strcmp(mailbox_name, RULE_NAME_DEFAULT_NONFORCE) == 0)
7ca2a9f1cca63cbc2ebffc185c7e5a2b32bc2780Timo Sirainen else if (strcmp(mailbox_name, RULE_NAME_DEFAULT_FORCE) == 0) {
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen i_info("Quota rule: root=%s mailbox=%s ignored",
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (!root_set->backend->v.parse_rule(root_set, rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen bool allow_negative = rule != &root_set->default_rule;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen if (quota_rule_parse_limits(root_set, rule, p,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_root_recalculate_relative_rules(root_set);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen "bytes=%lld (%u%%) messages=%lld (%u%%)", root_set->name,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (long long)rule->bytes_limit, rule->bytes_percent,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (long long)rule->count_limit, rule->count_percent);
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainenstatic bool quota_root_get_rule_limits(struct quota_root *root,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* if default rule limits are 0, this rule applies only to specific
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen rule = quota_root_rule_find(root->set, mailbox_name);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *bytes_limit_r = bytes_limit <= 0 ? 0 : bytes_limit;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *count_limit_r = count_limit <= 0 ? 0 : count_limit;
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainenstatic void quota_maildir_storage_set(struct mail_storage *storage)
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen /* FIXME: a bit ugly location for this code. */
ccd44abfe14f51cc1f6d8c0ec1aa6dc31242e2d3Timo Sirainen /* For newly generated filenames add ,S=size. */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_add_user_storage(struct quota *quota, struct mail_storage *storage)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, j, count;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen /* first check if there already exists a storage with the exact same
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path. we don't want to count them twice. */
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path = mail_storage_get_mailbox_path(storage, "", &is_file);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen storages = array_get("a->storages, &count);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen for (i = 0; i < count; i++) {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen path2 = mail_storage_get_mailbox_path(storages[i], "",
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (path2 != NULL && strcmp(path, path2) == 0) {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen /* duplicate */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* @UNSAFE: get different backends into one array */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen backends = t_new(struct quota_backend *, count + 1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen if (backends[j]->name == roots[i]->backend.name)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_remove_user_storage(struct mail_storage *storage)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen quota_get_mail_user_quota(storage->ns->owner);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen /* no quota for this storage */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen storages = array_get("a->storages, &count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenint quota_root_add_warning_rule(struct quota_root_settings *root_set,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen const char *p;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen ret = quota_rule_parse_limits(root_set, &rule,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warning = array_append_space(&root_set->warning_rules);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen quota_root_recalculate_relative_rules(root_set);
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen "messages=%llu (%u%%) command=%s",
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (unsigned long long)warning->rule.bytes_limit,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (unsigned long long)warning->rule.count_limit,
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen warning->rule.count_percent, warning->command);
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct mail_user *user = box->storage->ns->owner;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen iter->quota = quota_get_mail_user_quota(user);
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainenquota_root_is_visible(struct quota_root *root, struct mailbox *box,
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen /* we don't want to include this root in quota enforcing */
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen /* a single quota root: don't bother checking further */
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen return root->backend.v.match_box == NULL ? TRUE :
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstruct quota_root *quota_root_iter_next(struct quota_root_iter *iter)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct quota_root *const *roots, *root = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int count;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&iter->quota->roots, &count);
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[iter->i], iter->box, FALSE))
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenvoid quota_root_iter_deinit(struct quota_root_iter **_iter)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_root *quota_root_lookup(struct mail_user *user, const char *name)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen for (i = 0; i < count; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenconst char *quota_root_get_name(struct quota_root *root)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenconst char *const *quota_root_get_resources(struct quota_root *root)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_get_resource(struct quota_root *root, const char *mailbox_name,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const char *name, uint64_t *value_r, uint64_t *limit_r)
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_KILOBYTES) == 0) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen /* Get the value first. This call may also update quota limits if
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen they're defined externally. */
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen ret = root->backend.v.get_resource(root, name, value_r);
b1678954f83e1059b981e2def52a70054fa71399Timo Sirainen (void)quota_root_get_rule_limits(root, mailbox_name,
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainenint quota_set_resource(struct quota_root *root ATTR_UNUSED,
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen uint64_t value ATTR_UNUSED, const char **error_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* the quota information comes from userdb (or even config file),
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen so there's really no way to support this until some major changes
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota_transaction_context *quota_transaction_begin(struct mailbox *box)
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct mail_user *user = box->storage->ns->owner;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ctx = i_new(struct quota_transaction_context, 1);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenstatic int quota_transaction_set_limits(struct quota_transaction_context *ctx)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen unsigned int i, count;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen /* find the lowest quota limits from all roots and use them */
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen roots = array_get(&ctx->quota->roots, &count);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen for (i = 0; i < count; i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ret = quota_get_resource(roots[i], mailbox_name,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen } else if (ret < 0) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen ret = quota_get_resource(roots[i], mailbox_name,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen } else if (ret < 0) {
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainenstatic void quota_warning_execute(struct quota_root *root, const char *cmd)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_error("system(%s) died with signal %d", cmd, WTERMSIG(ret));
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen cmd, WIFEXITED(ret) ? WEXITSTATUS(ret) : ret);
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainenstatic void quota_warnings_execute(struct quota_transaction_context *ctx,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen unsigned int i, count;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen uint64_t bytes_current, bytes_before, bytes_limit;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen uint64_t count_current, count_before, count_limit;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen warnings = array_get_modifiable(&root->set->warning_rules, &count);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_STORAGE_BYTES,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (quota_get_resource(root, "", QUOTA_NAME_MESSAGES,
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen bytes_before = bytes_current - ctx->bytes_used;
20dd6f6726db692904794d7daafe5b6e7238b720Timo Sirainen count_before = count_current - ctx->count_used;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen for (i = 0; i < count; i++) {
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen if ((bytes_before < (uint64_t)warnings[i].rule.bytes_limit &&
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen bytes_current >= (uint64_t)warnings[i].rule.bytes_limit) ||
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen (count_before < (uint64_t)warnings[i].rule.count_limit &&
3851ad9fcb25635f02b46d44586742ef1081876bTimo Sirainen count_current >= (uint64_t)warnings[i].rule.count_limit)) {
b40d1ef77181fda648e2ba22188c91a124f5bee6Timo Sirainen quota_warning_execute(root, warnings[i].command);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenint quota_transaction_commit(struct quota_transaction_context **_ctx)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_transaction_context *ctx = *_ctx;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen unsigned int i, count;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen else if (ctx->bytes_used != 0 || ctx->count_used != 0 ||
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen roots = array_get(&ctx->quota->roots, &count);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen for (i = 0; i < count; i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen /* mailbox not included in quota */
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (roots[i]->backend.v.update(roots[i], ctx) < 0)
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen /* execute quota warnings after all updates. this makes it
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen work correctly regardless of whether backend.get_resource()
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen returns updated values before backend.update() or not */
d8548e14925a06e44c4f1983d7b118b0180939e0Timo Sirainen for (i = 0; i < count; i++)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenvoid quota_transaction_rollback(struct quota_transaction_context **_ctx)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct quota_transaction_context *ctx = *_ctx;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_try_alloc(struct quota_transaction_context *ctx,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen ret = quota_test_alloc(ctx, size, too_large_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint quota_test_alloc(struct quota_transaction_context *ctx,
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen return ctx->quota->set->test_alloc(ctx, size, too_large_r);
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic int quota_default_test_alloc(struct quota_transaction_context *ctx,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int i, count;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (ctx->count_left != 0 && ctx->bytes_left >= ctx->bytes_used + size)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen roots = array_get(&ctx->quota->roots, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
6d6bbe8787354bbb69d0c03187adfe0f497d70b8Timo Sirainen if (!quota_root_is_visible(roots[i], ctx->box, TRUE))
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* if size is bigger than any limit, then
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen it is bigger than the lowest limit */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_alloc(struct quota_transaction_context *ctx, struct mail *mail)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid quota_free(struct quota_transaction_context *ctx, struct mail *mail)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenvoid quota_free_bytes(struct quota_transaction_context *ctx,