From: Timo Sirainen Date: Sun, 3 Dec 2017 17:26:11 +0000 (+0200) Subject: lib-fs: Add fs_file/iter_init_with_event() X-Git-Tag: 2.3.0.rc1~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc59aba4be5d2f57908e84f0f7477c467a72d567;p=thirdparty%2Fdovecot%2Fcore.git lib-fs: Add fs_file/iter_init_with_event() Use the event for logging critical errors --- diff --git a/src/lib-fs/fs-api-private.h b/src/lib-fs/fs-api-private.h index a968c583e7..ac813d7116 100644 --- a/src/lib-fs/fs-api-private.h +++ b/src/lib-fs/fs-api-private.h @@ -91,6 +91,7 @@ struct fs { unsigned int files_open_count; struct fs_file *files; struct fs_iter *iters; + struct event *event; struct fs_stats stats; @@ -104,6 +105,7 @@ struct fs_file { struct fs_file *parent; /* for wrapper filesystems */ struct fs *fs; struct ostream *output; + struct event *event; char *path; enum fs_open_flags flags; @@ -141,6 +143,7 @@ struct fs_iter { struct fs_iter *prev, *next; struct fs *fs; + struct event *event; enum fs_iter_flags flags; struct timeval start_time; diff --git a/src/lib-fs/fs-api.c b/src/lib-fs/fs-api.c index b50b8ef2ff..38fd28374e 100644 --- a/src/lib-fs/fs-api.c +++ b/src/lib-fs/fs-api.c @@ -128,6 +128,16 @@ static void fs_class_try_load_plugin(const char *driver) lib_atexit(fs_class_deinit_modules); } +static struct event *fs_create_event(struct fs *fs, struct event *parent) +{ + struct event *event; + + event = event_create(parent); + event_set_append_log_prefix(event, + t_strdup_printf("fs-%s: ", fs->name)); + return event; +} + int fs_init(const char *driver, const char *args, const struct fs_settings *set, struct fs **fs_r, const char **error_r) @@ -148,6 +158,7 @@ int fs_init(const char *driver, const char *args, } if (fs_alloc(fs_class, args, set, fs_r, error_r) < 0) return -1; + (*fs_r)->event = fs_create_event(*fs_r, set->event); temp_file_prefix = set->temp_file_prefix != NULL ? set->temp_file_prefix : ".temp.dovecot"; @@ -203,6 +214,7 @@ void fs_unref(struct fs **_fs) } i_assert(fs->files == NULL); + event_unref(&fs->event); i_free(fs->username); i_free(fs->session_id); i_free(fs->temp_path_prefix); @@ -235,6 +247,12 @@ const char *fs_get_root_driver(struct fs *fs) } struct fs_file *fs_file_init(struct fs *fs, const char *path, int mode_flags) +{ + return fs_file_init_with_event(fs, fs->event, path, mode_flags); +} + +struct fs_file *fs_file_init_with_event(struct fs *fs, struct event *event, + const char *path, int mode_flags) { struct fs_file *file; @@ -246,9 +264,11 @@ struct fs_file *fs_file_init(struct fs *fs, const char *path, int mode_flags) file = fs->v.file_alloc(); file->fs = fs; file->flags = mode_flags & ~FS_OPEN_MODE_MASK; + file->event = fs_create_event(fs, event); fs->v.file_init(file, path, mode_flags & FS_OPEN_MODE_MASK, mode_flags & ~FS_OPEN_MODE_MASK); } T_END; + fs->files_open_count++; DLLIST_PREPEND(&fs->files, file); @@ -259,6 +279,7 @@ struct fs_file *fs_file_init(struct fs *fs, const char *path, int mode_flags) void fs_file_deinit(struct fs_file **_file) { struct fs_file *file = *_file; + struct event *event = file->event; pool_t metadata_pool = file->metadata_pool; i_assert(file->fs->files_open_count > 0); @@ -273,6 +294,7 @@ void fs_file_deinit(struct fs_file **_file) file->fs->v.file_deinit(file); } T_END; + event_unref(&event); if (metadata_pool != NULL) pool_unref(&metadata_pool); } @@ -473,6 +495,11 @@ struct fs *fs_file_fs(struct fs_file *file) return file->fs; } +struct event *fs_file_event(struct fs_file *file) +{ + return file->event; +} + static void ATTR_FORMAT(2, 0) fs_set_verror(struct fs *fs, const char *fmt, va_list args) { @@ -1062,6 +1089,13 @@ int fs_delete(struct fs_file *file) struct fs_iter * fs_iter_init(struct fs *fs, const char *path, enum fs_iter_flags flags) +{ + return fs_iter_init_with_event(fs, fs->event, path, flags); +} + +struct fs_iter * +fs_iter_init_with_event(struct fs *fs, struct event *event, + const char *path, enum fs_iter_flags flags) { struct fs_iter *iter; struct timeval now = ioloop_timeval; @@ -1081,6 +1115,7 @@ fs_iter_init(struct fs *fs, const char *path, enum fs_iter_flags flags) iter = fs->v.iter_alloc(); iter->fs = fs; iter->flags = flags; + iter->event = fs_create_event(fs, event); fs->v.iter_init(iter, path, flags); } T_END; iter->start_time = now; @@ -1091,6 +1126,7 @@ fs_iter_init(struct fs *fs, const char *path, enum fs_iter_flags flags) int fs_iter_deinit(struct fs_iter **_iter) { struct fs_iter *iter = *_iter; + struct event *event = iter->event; int ret; *_iter = NULL; @@ -1103,6 +1139,7 @@ int fs_iter_deinit(struct fs_iter **_iter) } else T_BEGIN { ret = iter->fs->v.iter_deinit(iter); } T_END; + event_unref(&event); return ret; } @@ -1161,7 +1198,7 @@ void fs_set_critical(struct fs *fs, const char *fmt, ...) va_start(args, fmt); fs_set_verror(fs, fmt, args); - i_error("fs-%s: %s", fs->name, fs_last_error(fs)); + e_error(fs->event, "%s", fs_last_error(fs)); va_end(args); } @@ -1204,12 +1241,14 @@ uint64_t fs_stats_get_write_usecs(const struct fs_stats *stats) struct fs_file * fs_file_init_parent(struct fs_file *parent, const char *path, int mode_flags) { - return fs_file_init(parent->fs->parent, path, mode_flags); + return fs_file_init_with_event(parent->fs->parent, parent->event, + path, mode_flags); } struct fs_iter * fs_iter_init_parent(struct fs_iter *parent, const char *path, enum fs_iter_flags flags) { - return fs_iter_init(parent->fs->parent, path, flags); + return fs_iter_init_with_event(parent->fs->parent, parent->event, + path, flags); } diff --git a/src/lib-fs/fs-api.h b/src/lib-fs/fs-api.h index 6811961802..6cd84ca694 100644 --- a/src/lib-fs/fs-api.h +++ b/src/lib-fs/fs-api.h @@ -145,6 +145,10 @@ struct fs_settings { them. */ struct dns_client *dns_client; + /* Parent event to use, unless overridden by + fs_file_init_with_event() */ + struct event *event; + /* Enable debugging */ bool debug; /* Enable timing statistics */ @@ -221,6 +225,8 @@ const char *fs_get_driver(struct fs *fs); const char *fs_get_root_driver(struct fs *fs); struct fs_file *fs_file_init(struct fs *fs, const char *path, int mode_flags); +struct fs_file *fs_file_init_with_event(struct fs *fs, struct event *event, + const char *path, int mode_flags); void fs_file_deinit(struct fs_file **file); /* If the file has an input streams open, close them. */ @@ -246,6 +252,8 @@ int fs_lookup_metadata(struct fs_file *file, const char *key, const char *fs_file_path(struct fs_file *file); /* Returns the file's fs. */ struct fs *fs_file_fs(struct fs_file *file); +/* Returns the file's event. */ +struct event *fs_file_event(struct fs_file *file); /* Return the error message for the last failed operation. */ const char *fs_last_error(struct fs *fs); @@ -340,6 +348,9 @@ void fs_unlock(struct fs_lock **lock); nonexistent directory. */ struct fs_iter * fs_iter_init(struct fs *fs, const char *path, enum fs_iter_flags flags); +struct fs_iter * +fs_iter_init_with_event(struct fs *fs, struct event *event, + const char *path, enum fs_iter_flags flags); /* Returns 0 if ok, -1 if iteration failed. */ int fs_iter_deinit(struct fs_iter **iter); /* Returns the next filename. */ diff --git a/src/lib-fs/fs-sis-common.c b/src/lib-fs/fs-sis-common.c index 3fe3bd290e..06d1bcdf71 100644 --- a/src/lib-fs/fs-sis-common.c +++ b/src/lib-fs/fs-sis-common.c @@ -41,8 +41,9 @@ void fs_sis_try_unlink_hash_file(struct fs *sis_fs, struct fs_file *super_file) /* this may be the last link. if hashes/ file is the same, delete it. */ hash_path = t_strdup_printf("%s/"HASH_DIR_NAME"/%s", dir, hash); - hash_file = fs_file_init(super_file->fs, hash_path, - FS_OPEN_MODE_READONLY); + hash_file = fs_file_init_with_event(super_file->fs, + super_file->event, hash_path, + FS_OPEN_MODE_READONLY); if (fs_stat(hash_file, &st2) == 0 && st1.st_ino == st2.st_ino && CMP_DEV_T(st1.st_dev, st2.st_dev)) {