From 5146908366083abd2578f636c12acf8823b8db89 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Wed, 21 Dec 2011 13:17:54 +0000 Subject: [PATCH] Add common initialization code for struct device Avoid duplicate code and add _dev_init() where all common member values are initialized. --- WHATS_NEW | 1 + lib/device/dev-cache.c | 39 ++++++++++++++++----------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index 62ca287c5..1bb373e1b 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.89 - ================================== + Add function to inialize common structure device members. Always zalloc device structure during initialization. Fix missing thread list manipulation protection in dmeventd. Do not derefence lv pointer in _percent_run() function before NULL check. diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index 922dff05d..d236bd643 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -54,6 +54,18 @@ static struct { static int _insert(const char *path, int rec, int check_with_udev_db); +/* Setup non-zero members of passed zeroed 'struct device' */ +static void _dev_init(struct device *dev, int max_error_count) +{ + dev->block_size = -1; + dev->fd = -1; + dev->read_ahead = -1; + dev->max_error_count = max_error_count; + + dm_list_init(&dev->aliases); + dm_list_init(&dev->open_list); +} + struct device *dev_create_file(const char *filename, struct device *dev, struct str_list *alias, int use_malloc) { @@ -76,7 +88,6 @@ struct device *dev_create_file(const char *filename, struct device *dev, dm_free(alias); return NULL; } - dev->flags = DEV_ALLOCED; } else { if (!(dev = _zalloc(sizeof(*dev)))) { log_error("struct device allocation failed"); @@ -97,19 +108,9 @@ struct device *dev_create_file(const char *filename, struct device *dev, return NULL; } - dev->flags |= DEV_REGULAR; - dm_list_init(&dev->aliases); + _dev_init(dev, NO_DEV_ERROR_COUNT_LIMIT); + dev->flags = DEV_REGULAR | ((use_malloc) ? DEV_ALLOCED : 0); dm_list_add(&dev->aliases, &alias->list); - dev->end = UINT64_C(0); - dev->dev = 0; - dev->fd = -1; - dev->open_count = 0; - dev->error_count = 0; - dev->max_error_count = NO_DEV_ERROR_COUNT_LIMIT; - dev->block_size = -1; - dev->read_ahead = -1; - memset(dev->pvid, 0, sizeof(dev->pvid)); - dm_list_init(&dev->open_list); return dev; } @@ -122,17 +123,9 @@ static struct device *_dev_create(dev_t d) log_error("struct device allocation failed"); return NULL; } - dev->flags = 0; - dm_list_init(&dev->aliases); + + _dev_init(dev, dev_disable_after_error_count()); dev->dev = d; - dev->fd = -1; - dev->open_count = 0; - dev->max_error_count = dev_disable_after_error_count(); - dev->block_size = -1; - dev->read_ahead = -1; - dev->end = UINT64_C(0); - memset(dev->pvid, 0, sizeof(dev->pvid)); - dm_list_init(&dev->open_list); return dev; }