bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen MODULE_CONTEXT(obj, quota_mailbox_list_module)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen struct mailbox_transaction_context *expunge_trans;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen MODULE_CONTEXT_INIT(&mail_user_module_register);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(quota_storage_module,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(quota_mail_module, &mail_module_register);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(quota_mailbox_list_module,
38ce5769db11e7f52562610ee6e6fc4f0ea7888fMartti Rannanjärvistatic void quota_set_storage_error(struct quota_transaction_context *qt,
38ce5769db11e7f52562610ee6e6fc4f0ea7888fMartti Rannanjärvi const char *errstr = quota_alloc_result_errstr(res, qt);
0060557e0171750089c3cee3878ddbf67ac7c4e8Martti Rannanjärvi struct mail_storage *storage = box->storage;
7f4fa37676bac8efcf4e2ac706172b1bad779a8aMartti Rannanjärvi mail_storage_set_error(storage, MAIL_ERROR_LIMIT, errstr);
38ce5769db11e7f52562610ee6e6fc4f0ea7888fMartti Rannanjärvi case QUOTA_ALLOC_RESULT_OVER_QUOTA_LIMIT:
38ce5769db11e7f52562610ee6e6fc4f0ea7888fMartti Rannanjärvi mail_storage_set_error(storage, MAIL_ERROR_NOQUOTA, errstr);
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi case QUOTA_ALLOC_RESULT_BACKGROUND_CALC:
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(box, "quota: %s", internal_err);
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainenstatic void quota_mail_expunge(struct mail *_mail)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen struct mail_private *mail = (struct mail_private *)_mail;
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(_mail->box);
9390fe0c1b5b7495363f9194ff701470e242dd0eAki Tuomi struct quota_user *quser = QUOTA_USER_CONTEXT_REQUIRE(_mail->box->storage->user);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen union mail_module_context *qmail = QUOTA_MAIL_CONTEXT(mail);
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(_mail->transaction);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* We need to handle the situation where multiple transactions expunged
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen the mail at the same time. In here we'll just save the message's
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen physical size and do the quota freeing later when the message was
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen known to be expunged. */
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen array_append(&qbox->expunge_uids, &_mail->uid, 1);
aaebcf0da12df7216be69961204fa64ec24c54b9Timo Sirainen if ((_mail->transaction->flags & MAILBOX_TRANSACTION_FLAG_SYNC) != 0) {
aaebcf0da12df7216be69961204fa64ec24c54b9Timo Sirainen /* we're running dsync. if this brings the quota below
aaebcf0da12df7216be69961204fa64ec24c54b9Timo Sirainen a negative quota warning, don't execute it, because
aaebcf0da12df7216be69961204fa64ec24c54b9Timo Sirainen it probably was already executed by the replica. */
9357960a30d4936de5e261bf522d83f7d3f49986Timo Sirainenquota_get_status(struct mailbox *box, enum mailbox_status_items items,
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(box);
b62139aae0a0099d0ede35b164b2870479f9b027Martti Rannanjärvi enum quota_alloc_result qret = quota_test_alloc(qt, 0, &error);
0060557e0171750089c3cee3878ddbf67ac7c4e8Martti Rannanjärvi quota_set_storage_error(qt, box, qret, error);
9357960a30d4936de5e261bf522d83f7d3f49986Timo Sirainen if ((items & ~STATUS_CHECK_OVER_QUOTA) == 0) {
9357960a30d4936de5e261bf522d83f7d3f49986Timo Sirainen /* don't bother calling parent, it may unnecessarily
9357960a30d4936de5e261bf522d83f7d3f49986Timo Sirainen try to open the mailbox */
9357960a30d4936de5e261bf522d83f7d3f49986Timo Sirainen if (qbox->module_ctx.super.get_status(box, items, status_r) < 0)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenquota_mailbox_transaction_begin(struct mailbox *box,
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(box);
c6ce2e251ac75fa650c7fbfa52150eae69386293Martti Rannanjärvi t = qbox->module_ctx.super.transaction_begin(box, flags, reason);
aaebcf0da12df7216be69961204fa64ec24c54b9Timo Sirainen qt->sync_transaction = (flags & MAILBOX_TRANSACTION_FLAG_SYNC) != 0;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen MODULE_CONTEXT_SET(t, quota_storage_module, qt);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenquota_mailbox_transaction_commit(struct mailbox_transaction_context *ctx,
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen struct mail_transaction_commit_changes *changes_r)
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(ctx->box);
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(ctx);
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen if (qbox->module_ctx.super.transaction_commit(ctx, changes_r) < 0) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenquota_mailbox_transaction_rollback(struct mailbox_transaction_context *ctx)
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(ctx->box);
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(ctx);
26550dc10a8c2057d09894c4e3f9dc12b124e90cTimo Sirainen qbox->module_ctx.super.transaction_rollback(ctx);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen struct quota_mailbox *qbox = QUOTA_CONTEXT(_mail->box);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen struct mail_private *mail = (struct mail_private *)_mail;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen qmail = p_new(mail->pool, union mail_module_context, 1);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen MODULE_CONTEXT_SET_SELF(mail, quota_mail_module, qmail);
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainenquota_move_requires_check(struct mailbox *dest_box, struct mailbox *src_box)
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen struct mail_namespace *src_ns = src_box->list->ns;
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen struct mail_namespace *dest_ns = dest_box->list->ns;
9390fe0c1b5b7495363f9194ff701470e242dd0eAki Tuomi struct quota_user *quser = QUOTA_USER_CONTEXT_REQUIRE(src_ns->user);
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen have_src_quota = quota_root_is_namespace_visible(*rootp, src_ns);
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen have_dest_quota = quota_root_is_namespace_visible(*rootp, dest_ns);
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen /* Both/neither have this quota */
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen /* Destination mailbox has a quota that doesn't exist
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen in source. We'll need to check if it's being
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen /* Source mailbox has a quota root that doesn't exist
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen in destination. We're not increasing the source
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen quota, so ignore it. */
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainenstatic int quota_check(struct mail_save_context *ctx, struct mailbox *src_box)
406ea4e54dc603e381749b5c29176a4e756e9a99Timo Sirainen struct mailbox_transaction_context *t = ctx->transaction;
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(t);
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen !quota_move_requires_check(ctx->transaction->box, src_box)) {
406ea4e54dc603e381749b5c29176a4e756e9a99Timo Sirainen /* the mail is being moved. the quota won't increase (after
406ea4e54dc603e381749b5c29176a4e756e9a99Timo Sirainen the following expunge), so allow this even if user is
406ea4e54dc603e381749b5c29176a4e756e9a99Timo Sirainen currently over quota */
b62139aae0a0099d0ede35b164b2870479f9b027Martti Rannanjärvi ret = quota_try_alloc(qt, ctx->dest_mail, &error);
b62139aae0a0099d0ede35b164b2870479f9b027Martti Rannanjärvi /* Log the error, but allow saving anyway. */
d81702ddd148abde32cbe2a45f6239e1baee6907Timo Sirainen i_error("quota: Failed to check if user is under quota: %s - saving mail anyway", error);
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi case QUOTA_ALLOC_RESULT_BACKGROUND_CALC:
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi /* Could not determine if there is enough space due to ongoing
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi background quota calculation, allow saving anyway. */
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_warning("quota: Failed to check if user is under quota: %s - saving mail anyway", error);
0060557e0171750089c3cee3878ddbf67ac7c4e8Martti Rannanjärvi quota_set_storage_error(qt, t->box, ret, error);
bd4d0a1a7c0626452b8d82f37e3ec07267ac9896Timo Sirainenquota_copy(struct mail_save_context *ctx, struct mail *mail)
bd4d0a1a7c0626452b8d82f37e3ec07267ac9896Timo Sirainen struct mailbox_transaction_context *t = ctx->transaction;
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(t);
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(t->box);
880f7a015f7dc2df2a06cec91dcecbbbe7e8dce3Timo Sirainen /* we always want to know the mail size */
880f7a015f7dc2df2a06cec91dcecbbbe7e8dce3Timo Sirainen mail_add_temp_wanted_fields(ctx->dest_mail, MAIL_FETCH_PHYSICAL_SIZE, NULL);
9cc53ac30c33f403964cc3f4f51456301dc044bcAki Tuomi /* get quota before copying any mails. this avoids dovecot-vsize.lock
5b5da56e477dd55fbac23be9c96d8baa00482069Timo Sirainen deadlocks with backends that lock mails for expunging/copying. */
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi if (quota_transaction_set_limits(qt, &error_res, &error) < 0) {
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi if (error_res == QUOTA_GET_RESULT_BACKGROUND_CALC)
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_warning("quota: %s - copying mail anyway", error);
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_error("quota: %s - copying mail anyway", error);
bd4d0a1a7c0626452b8d82f37e3ec07267ac9896Timo Sirainen if (qbox->module_ctx.super.copy(ctx, mail) < 0)
807b06485623fe389f9414ba9f5ed08df01a4b57Timo Sirainen /* copying used saving internally, we already checked the
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainenquota_save_begin(struct mail_save_context *ctx, struct istream *input)
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen struct mailbox_transaction_context *t = ctx->transaction;
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_transaction_context *qt = QUOTA_CONTEXT_REQUIRE(t);
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(t->box);
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi if (!ctx->moving && i_stream_get_size(input, TRUE, &size) > 0 &&
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen /* Input size is known, check for quota immediately. This
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen check isn't perfect, especially because input stream's
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen linefeeds may contain CR+LFs while physical message would
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen only contain LFs. With mbox some headers might be skipped
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen I think these don't really matter though compared to the
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen benefit of giving "out of quota" error before sending the
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen full mail. */
b62139aae0a0099d0ede35b164b2870479f9b027Martti Rannanjärvi enum quota_alloc_result qret = quota_test_alloc(qt, size, &error);
38ce5769db11e7f52562610ee6e6fc4f0ea7888fMartti Rannanjärvi /* Great, there is space. */
b62139aae0a0099d0ede35b164b2870479f9b027Martti Rannanjärvi /* Log the error, but allow saving anyway. */
d81702ddd148abde32cbe2a45f6239e1baee6907Timo Sirainen i_error("quota: Failed to check if user is under quota: %s - saving mail anyway", error);
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi case QUOTA_ALLOC_RESULT_BACKGROUND_CALC:
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi /* Could not determine if there is enough space due to
b14f93ea4f7e9b78e5adbabcc78bd2f63d2bcd36Martti Rannanjärvi * ongoing background quota calculation, allow saving
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_warning("quota: Failed to check if user is under quota: %s - saving mail anyway", error);
0060557e0171750089c3cee3878ddbf67ac7c4e8Martti Rannanjärvi quota_set_storage_error(qt, t->box, qret, error);
880f7a015f7dc2df2a06cec91dcecbbbe7e8dce3Timo Sirainen /* we always want to know the mail size */
880f7a015f7dc2df2a06cec91dcecbbbe7e8dce3Timo Sirainen mail_add_temp_wanted_fields(ctx->dest_mail, MAIL_FETCH_PHYSICAL_SIZE, NULL);
9cc53ac30c33f403964cc3f4f51456301dc044bcAki Tuomi /* get quota before copying any mails. this avoids dovecot-vsize.lock
5b5da56e477dd55fbac23be9c96d8baa00482069Timo Sirainen deadlocks with backends that lock mails for expunging/copying. */
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi if (quota_transaction_set_limits(qt, &error_res, &error) < 0) {
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi if (error_res == QUOTA_GET_RESULT_BACKGROUND_CALC)
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_warning("quota: %s - saving mail anyway", error);
39914d983f6de82ba5613a9848e0935782013da1Martti Rannanjärvi i_error("quota: %s - saving mail anyway", error);
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen return qbox->module_ctx.super.save_begin(ctx, input);
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainenstatic int quota_save_finish(struct mail_save_context *ctx)
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(ctx->transaction->box);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen if (qbox->module_ctx.super.save_finish(ctx) < 0)
2ec4ab6f5a1172e86afc72c0f29f470d6fd2bd9aTimo Sirainen src_box = ctx->copy_src_mail == NULL ? NULL : ctx->copy_src_mail->box;
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainenstatic void quota_mailbox_sync_cleanup(struct quota_mailbox *qbox)
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainen if (qbox->expunge_qt != NULL && qbox->expunge_qt->tmp_mail != NULL) {
93eb96e09c0fd00a77b3fba185b043cc2c011444Timo Sirainen (void)mailbox_transaction_commit(&qbox->expunge_trans);
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainenstatic void quota_mailbox_sync_commit(struct quota_mailbox *qbox)
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainen (void)quota_transaction_commit(&qbox->expunge_qt);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenstatic void quota_mailbox_sync_notify(struct mailbox *box, uint32_t uid,
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(box);
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen struct index_mailbox_context *ibox = INDEX_STORAGE_CONTEXT(box);
9390fe0c1b5b7495363f9194ff701470e242dd0eAki Tuomi struct quota_user *quser = QUOTA_USER_CONTEXT_REQUIRE(box->storage->user);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen if (qbox->module_ctx.super.sync_notify != NULL)
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen qbox->module_ctx.super.sync_notify(box, uid, sync_type);
f8a63c14a4d2eed05019e5d8ed48fc3d8c976ed4Timo Sirainen if (sync_type != MAILBOX_SYNC_TYPE_EXPUNGE || qbox->recalculate ||
f8a63c14a4d2eed05019e5d8ed48fc3d8c976ed4Timo Sirainen (box->flags & MAILBOX_FLAG_DELETE_UNSAFE) != 0) {
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainen /* free the transaction before view syncing begins,
3091db1d26fed038841a3a81dd5ff507d0b375ceTimo Sirainen otherwise it'll crash. */
e2e64c109827f782e9e20b50a15c7489479bcadaTimo Sirainen qbox->expunge_qt = quota_transaction_begin(box);
f9362223346d10a5866b376ed227b77b26ea4292Timo Sirainen /* even though backend doesn't care about size/count changes,
f9362223346d10a5866b376ed227b77b26ea4292Timo Sirainen make sure count_used changes so quota_warnings are
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* we're in the middle of syncing the mailbox, so it's a bad idea to
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen try and get the message sizes at this point. Rely on sizes that
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen we saved earlier, or recalculate the whole quota if we don't know
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen uids = array_get(&qbox->expunge_uids, &count);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* we already know the size */
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen /* FIXME: it's not ideal that we do the vsize update here, but
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen this is the easiest place for it for now.. maybe the mail
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen size checking code could be moved to lib-storage */
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen if (ibox->vsize_update != NULL && quser->quota->set->vsizes)
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen index_mailbox_vsize_hdr_expunge(ibox->vsize_update, uid, *sizep);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* try to look up the size. this works only if it's cached. */
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen /* FIXME: ugly kludge to open the transaction for sync_view.
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen box->view may not have all the new messages that
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen sync_notify() notifies about, and those messages would
6ae329de09afb7214c906d762320847e05469d53Timo Sirainen cause a quota recalculation. */
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi qbox->expunge_trans = mailbox_transaction_begin(box, 0, "quota");
93f1642397e46497894e6695749e5c52fda61774Timo Sirainen if (!mail_set_uid(qbox->expunge_qt->tmp_mail, uid))
93f1642397e46497894e6695749e5c52fda61774Timo Sirainen if (mail_get_physical_size(qbox->expunge_qt->tmp_mail, &size) == 0) {
93f1642397e46497894e6695749e5c52fda61774Timo Sirainen } else if (mail_get_virtual_size(qbox->expunge_qt->tmp_mail, &size) == 0) {
9963bef626fd9ea227fb606e8b1694cdb1ab39aaTimo Sirainen index_mailbox_vsize_hdr_expunge(ibox->vsize_update, uid, size);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen /* there's no way to get the size. recalculate the quota. */
39dea5f2e78f6bfc3adc0655176f596ee211938fTimo Sirainen quota_recalculate(qbox->expunge_qt, QUOTA_RECALCULATE_MISSING_FREES);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainenstatic int quota_mailbox_sync_deinit(struct mailbox_sync_context *ctx,
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(ctx->box);
2584e86cc2d8c31ba30a4109cf4ba09d1e37e28aTimo Sirainen ret = qbox->module_ctx.super.sync_deinit(ctx, status_r);
c1563f46bd9ae4ad20f37f003220443a6224fb9fTimo Sirainen /* update quota only after syncing is finished. the quota commit may
c1563f46bd9ae4ad20f37f003220443a6224fb9fTimo Sirainen recalculate the quota and cause all mailboxes to be synced,
c1563f46bd9ae4ad20f37f003220443a6224fb9fTimo Sirainen including the one we're already syncing. */
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainenstatic void quota_roots_flush(struct quota *quota)
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen for (i = 0; i < count; i++) {
1e9c53531a31e3ad0168872e513046a3222a1afdTimo Sirainenstatic void quota_mailbox_close(struct mailbox *box)
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(box);
9390fe0c1b5b7495363f9194ff701470e242dd0eAki Tuomi struct quota_user *quser = QUOTA_USER_CONTEXT_REQUIRE(box->storage->user);
1e9c53531a31e3ad0168872e513046a3222a1afdTimo Sirainen /* sync_notify() may be called outside sync_begin()..sync_deinit().
1e9c53531a31e3ad0168872e513046a3222a1afdTimo Sirainen make sure we apply changes at close time at latest. */
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen /* make sure quota backend flushes all data. this could also be done
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen somewhat later, but user.deinit() is too late, since the flushing
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen can trigger quota recalculation which isn't safe to do anymore
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen at user.deinit() when most of the loaded plugins have already been
048e40f9364fa68482bc276dd4a5d595a3d742e9Timo Sirainen deinitialized. */
e03d986a74128f5ba30fcfda9f6e36578f5d8decTimo Sirainenstatic void quota_mailbox_free(struct mailbox *box)
fe6356ecbb1c64802f121a37d2e4c6199fe67074Aki Tuomi struct quota_mailbox *qbox = QUOTA_CONTEXT_REQUIRE(box);
ea9d9d99948cff5f9b881f79b28fa3b80da0f2a7Timo Sirainenvoid quota_mailbox_allocated(struct mailbox *box)
40c24f8fe31037b61f2ac594a6dfcd123080b4b5Timo Sirainen if ((box->storage->class_flags & MAIL_STORAGE_CLASS_FLAG_NOQUOTA) != 0)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen qbox = p_new(box->pool, struct quota_mailbox, 1);
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen v->transaction_begin = quota_mailbox_transaction_begin;
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen v->transaction_commit = quota_mailbox_transaction_commit;
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen v->transaction_rollback = quota_mailbox_transaction_rollback;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen MODULE_CONTEXT_SET(box, quota_storage_module, qbox);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic void quota_mailbox_list_deinit(struct mailbox_list *list)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct quota_mailbox_list *qlist = QUOTA_LIST_CONTEXT(list);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstruct quota *quota_get_mail_user_quota(struct mail_user *user)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen struct quota_user *quser = QUOTA_USER_CONTEXT(user);
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenstatic void quota_user_deinit(struct mail_user *user)
9390fe0c1b5b7495363f9194ff701470e242dd0eAki Tuomi struct quota_user *quser = QUOTA_USER_CONTEXT_REQUIRE(user);
bb34c6538784b564c05eefccc74af2bd7c9505f5Timo Sirainen struct quota_settings *quota_set = quser->quota->set;
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid quota_mail_user_created(struct mail_user *user)
4316355ca8b7698516272520a972291378698140Timo Sirainen if ((ret = quota_user_read_settings(user, &set, &error)) > 0) {
4316355ca8b7698516272520a972291378698140Timo Sirainen if (quota_init(set, user, "a, &error) < 0) {
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen quser = p_new(user->pool, struct quota_user, 1);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen MODULE_CONTEXT_SET(user, quota_user_module, quser);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk i_debug("quota: No quota setting - plugin disabled");
2d8b23805db6f06b8b38174fb6e135386694f429Timo Sirainenstatic struct quota_root *
2d8b23805db6f06b8b38174fb6e135386694f429Timo Sirainenquota_find_root_for_ns(struct quota *quota, struct mail_namespace *ns)
2d8b23805db6f06b8b38174fb6e135386694f429Timo Sirainen for (i = 0; i < count; i++) {
d7c2ae49f036ef256f63bd2d437c53da122c665eTimo Sirainenvoid quota_mailbox_list_created(struct mailbox_list *list)
805872b9f885c1cc4bddf0b6ff71ca61f6cdd7bfTimo Sirainen /* see if we have a quota explicitly defined for this namespace */
d7c2ae49f036ef256f63bd2d437c53da122c665eTimo Sirainen quota = quota_get_mail_user_quota(list->ns->user);
d7c2ae49f036ef256f63bd2d437c53da122c665eTimo Sirainen root = quota_find_root_for_ns(quota, list->ns);
1f5670bce3e5ec0c1ddf8d95c57ebb428f1339f6Timo Sirainen /* explicit quota root */
40c24f8fe31037b61f2ac594a6dfcd123080b4b5Timo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_NOQUOTA) != 0)
805872b9f885c1cc4bddf0b6ff71ca61f6cdd7bfTimo Sirainen /* public namespace - add quota only if namespace is
805872b9f885c1cc4bddf0b6ff71ca61f6cdd7bfTimo Sirainen explicitly defined for it */
1f5670bce3e5ec0c1ddf8d95c57ebb428f1339f6Timo Sirainen /* for shared namespaces add only if the owner has quota
f7fcdca120f1b591f885408d08fc90cfd725bf94Timo Sirainen qlist = p_new(list->pool, struct quota_mailbox_list, 1);
f7fcdca120f1b591f885408d08fc90cfd725bf94Timo Sirainen MODULE_CONTEXT_SET(list, quota_mailbox_list_module, qlist);
1f5670bce3e5ec0c1ddf8d95c57ebb428f1339f6Timo Sirainen quota = quota_get_mail_user_quota(quota_user);
eafecb7ad0fbbde0da8be7e8ae735459b7bb688bTimo Sirainenstatic void quota_root_set_namespace(struct quota_root *root,
9a30df5ac82378e2706715eaf3ee6653d8b46161Timo Sirainen /* silence errors for autocreated (shared) users */
9a30df5ac82378e2706715eaf3ee6653d8b46161Timo Sirainen bool silent_errors = namespaces->user->autocreated;
eafecb7ad0fbbde0da8be7e8ae735459b7bb688bTimo Sirainen if (root->ns_prefix != NULL && root->ns == NULL) {
eafecb7ad0fbbde0da8be7e8ae735459b7bb688bTimo Sirainen root->ns = mail_namespace_find_prefix(namespaces,
9a30df5ac82378e2706715eaf3ee6653d8b46161Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_UNUSABLE) != 0 &&
eafecb7ad0fbbde0da8be7e8ae735459b7bb688bTimo Sirainen i_error("quota: Unknown namespace: %s", name);
2d8b23805db6f06b8b38174fb6e135386694f429Timo Sirainenvoid quota_mail_namespaces_created(struct mail_namespace *namespaces)
2d8b23805db6f06b8b38174fb6e135386694f429Timo Sirainen quota = quota_get_mail_user_quota(namespaces->user);
eafecb7ad0fbbde0da8be7e8ae735459b7bb688bTimo Sirainen for (i = 0; i < count; i++)