bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
c16bf6335c393c00465903f7a46ce2b7ceb6726eTimo Sirainenextern struct quota_backend quota_backend_count;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenquota_count_mailbox(struct quota_root *root, struct mail_namespace *ns,
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi const char *vname, uint64_t *bytes, uint64_t *count,
cc5e3d3391d7e54b643ab4949e8df465f37234bbTimo Sirainen rule = quota_root_rule_find(root->set, vname);
2b682d8d3661800f16aceaa45fa4de9b6b140a59Timo Sirainen /* mailbox not included in quota */
4145cbac82bfc0c8bfeceeca0ef841700117930cTimo Sirainen box = mailbox_alloc(ns->list, vname, MAILBOX_FLAG_READONLY);
9e99739b14df2035477ee58668b91a800dfee260Timo Sirainen if ((box->storage->class_flags & MAIL_STORAGE_CLASS_FLAG_NOQUOTA) != 0) {
9e99739b14df2035477ee58668b91a800dfee260Timo Sirainen /* quota doesn't exist for this mailbox/storage */
9e99739b14df2035477ee58668b91a800dfee260Timo Sirainen } else if (mailbox_get_metadata(box, root->quota->set->vsizes ?
c16bf6335c393c00465903f7a46ce2b7ceb6726eTimo Sirainen mailbox_get_status(box, STATUS_MESSAGES, &status) < 0) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi errstr = mailbox_get_last_internal_error(box, &error);
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Couldn't get size of mailbox %s: %s",
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi *error_result_r = QUOTA_GET_RESULT_INTERNAL_ERROR;
a758be690d736655ed7c1e3dfb46d3aaf4dd5db2Timo Sirainen /* started on background. don't log an error. */
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi "Ongoing quota calculation blocked getting size of %s: %s",
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi *error_result_r = QUOTA_GET_RESULT_BACKGROUND_CALC;
c16bf6335c393c00465903f7a46ce2b7ceb6726eTimo Sirainen /* non-temporary error, e.g. ACLs denied access. */
93f1642397e46497894e6695749e5c52fda61774Timo Sirainen metadata.virtual_size : metadata.physical_size;
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainenquota_mailbox_iter_begin(struct quota_root *root)
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärviquota_mailbox_iter_deinit(struct quota_mailbox_iter **_iter,
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen if (mailbox_list_iter_deinit(&iter->iter) < 0) {
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Listing namespace '%s' failed: %s",
d4847b921058734e0668bc7690465c91523d9ec0Martti Rannanjärvi mailbox_list_get_last_internal_error(iter->ns->list, NULL));
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi *iter->error != '\0' && *error2 != '\0' ? " and " : "";
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainenstatic const struct mailbox_info *
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainenquota_mailbox_iter_next(struct quota_mailbox_iter *iter)
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen namespaces = array_get(&iter->root->quota->namespaces, &count);
c80cf58a347d1b6777e12b3bdd38cc52f6a45f73Timo Sirainen } while (!quota_root_is_namespace_visible(iter->root, iter->ns));
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen iter->iter = mailbox_list_iter_init(iter->ns->list, "*",
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen while ((info = mailbox_list_iter_next(iter->iter)) != NULL) {
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen if (mailbox_list_iter_deinit(&iter->iter) < 0) {
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Listing namespace '%s' failed: %s",
d4847b921058734e0668bc7690465c91523d9ec0Martti Rannanjärvi mailbox_list_get_last_internal_error(iter->ns->list, NULL));
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen strncasecmp(iter->ns->prefix, "INBOX", 5) != 0)) {
87b71ffbf111ce67a8392c8d4ef0055f208cc88aTimo Sirainen /* if the namespace prefix itself exists, count it also */
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen iter->info.vname = t_strndup(iter->ns->prefix,
88b06199c3b87b14c41a636219a19156a5df2485Timo Sirainen /* try the next namespace */
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärviint quota_count(struct quota_root *root, uint64_t *bytes_r, uint64_t *count_r,
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi enum quota_get_result *error_result_r, const char **error_r)
617092b0f17b37625e354d45108ad62baa65a858Timo Sirainen while ((info = quota_mailbox_iter_next(iter)) != NULL) {
92c671c1667ea1bea90030a2b69298bf8c541623Timo Sirainen if (quota_count_mailbox(root, info->ns, info->vname,
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi if (quota_mailbox_iter_deinit(&iter, &error2) < 0) {
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi *error_result_r = QUOTA_GET_RESULT_INTERNAL_ERROR;
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi *error1 != '\0' && *error2 != '\0' ? " and " : "";
de338ef27767268c2018f54f0b5ddeff7fca3870Martti Rannanjärvi *error_r = t_strconcat(error1, separator, error2, NULL);
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärviquota_count_cached(struct count_quota_root *root,
ad3bf867168121a1255720487457d8bffc3f5e48Timo Sirainen if (root->cache_timeval.tv_usec == ioloop_timeval.tv_usec &&
ad3bf867168121a1255720487457d8bffc3f5e48Timo Sirainen root->cache_timeval.tv_sec == ioloop_timeval.tv_sec &&
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi ret = quota_count(&root->root, bytes_r, count_r, &error_res, error_r);
4f52de745cf26ee9dcbde7ca4500d442116226cfMartti Rannanjärvi } else if (ret > 0) {
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainenstatic struct quota_root *count_quota_alloc(void)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainenstatic int count_quota_init(struct quota_root *root, const char *args,
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen const char **error_r)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen *error_r = "quota count backend requires quota_vsizes=yes";
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen return quota_root_default_init(root, args, error_r);
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainenstatic void count_quota_deinit(struct quota_root *_root)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainenstatic const char *const *
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainencount_quota_root_get_resources(struct quota_root *root ATTR_UNUSED)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen QUOTA_NAME_STORAGE_KILOBYTES, QUOTA_NAME_MESSAGES, NULL
ad3bf867168121a1255720487457d8bffc3f5e48Timo Sirainencount_quota_get_resource(struct quota_root *_root,
ad3bf867168121a1255720487457d8bffc3f5e48Timo Sirainen struct count_quota_root *root = (struct count_quota_root *)_root;
12d7f667b636405ae54dcc4cb3031f4ba52aff04Martti Rannanjärvi ret = quota_count_cached(root, &bytes, &count, error_r);
12d7f667b636405ae54dcc4cb3031f4ba52aff04Martti Rannanjärvi if (ret <= QUOTA_GET_RESULT_INTERNAL_ERROR)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen if (strcmp(name, QUOTA_NAME_STORAGE_BYTES) == 0)
c4db1218645ed8ec8b5ae67c05bc5d7a80c1b8aeTimo Sirainen else if (strcmp(name, QUOTA_NAME_MESSAGES) == 0)
1379bb74c2855aaf3415fdfe965164a44ac3c001Martti Rannanjärvi *error_r = QUOTA_UNKNOWN_RESOURCE_ERROR_STRING;
1379bb74c2855aaf3415fdfe965164a44ac3c001Martti Rannanjärvi return QUOTA_GET_RESULT_UNKNOWN_RESOURCE;
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvistatic int quota_count_recalculate_box(struct mailbox *box,
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi errstr = mailbox_get_last_internal_error(box, &error);
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen /* non-temporary error, e.g. ACLs denied access. */
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Couldn't open mailbox %s: %s", box->vname, errstr);
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen /* reset the vsize header first */
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen trans = mail_index_transaction_begin(box->view,
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen mail_index_update_header_ext(trans, box->vsize_hdr_ext_id,
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi if (mail_index_transaction_commit(&trans) < 0) {
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Couldn't commit mail index transaction for %s: %s",
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi mail_index_get_error_message(box->view->index));
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen /* getting the vsize now forces its recalculation */
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen if (mailbox_get_metadata(box, MAILBOX_METADATA_VIRTUAL_SIZE,
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Couldn't get mailbox %s vsize: %s", box->vname,
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen /* call sync to write the change to mailbox list index */
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FAST) < 0) {
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "Couldn't sync mailbox %s: %s", box->vname,
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvistatic int quota_count_recalculate(struct quota_root *root,
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen while ((info = quota_mailbox_iter_next(iter)) != NULL) {
ddd7c3b36893263c7de0df139a266daf75bcca60Timo Sirainen box = mailbox_alloc(info->ns->list, info->vname, 0);
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi if (quota_count_recalculate_box(box, &error1) < 0)
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi if (quota_mailbox_iter_deinit(&iter, &error2) < 0)
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi *error1 != '\0' && *error2 != '\0' ? " and " : "";
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi "quota-count: recalculate failed: %s%s%s",
464db6d9d6d9126ac87143fc7a466120ff5b70f4Timo Sirainen struct count_quota_root *croot = (struct count_quota_root *)root;
39dea5f2e78f6bfc3adc0655176f596ee211938fTimo Sirainen if (ctx->recalculate == QUOTA_RECALCULATE_FORCED) {
d2cbbecf76de3f4eb945895fab5760ed0a28281cMartti Rannanjärvi if (quota_count_recalculate(root, error_r) < 0)
5997118fa7aee2535edac28092261ca085a958aeMartti Rannanjärvi .get_resources = count_quota_root_get_resources,