bcache: reduce redundant code in bch_cached_dev_run()

In bch_cached_dev_run(), free(env[1])|free(env[2])|free(buf)
show up three times. This patch introduce out tag in
which free(env[1])|free(env[2])|free(buf) are only called
one time. If we need to call free() when errors occur,
we can set error code to ret, and then goto out tag directly.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: Coly Li <colyli@suse.de>
Link: https://lore.kernel.org/r/20210411134316.80274-2-colyli@suse.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Zhiqiang Liu 2021-04-11 21:43:10 +08:00 коммит произвёл Jens Axboe
Родитель ff91763835
Коммит 13e1db65d2
1 изменённых файлов: 12 добавлений и 13 удалений

Просмотреть файл

@ -1052,6 +1052,7 @@ static int cached_dev_status_update(void *arg)
int bch_cached_dev_run(struct cached_dev *dc) int bch_cached_dev_run(struct cached_dev *dc)
{ {
int ret = 0;
struct bcache_device *d = &dc->disk; struct bcache_device *d = &dc->disk;
char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL);
char *env[] = { char *env[] = {
@ -1064,19 +1065,15 @@ int bch_cached_dev_run(struct cached_dev *dc)
if (dc->io_disable) { if (dc->io_disable) {
pr_err("I/O disabled on cached dev %s\n", pr_err("I/O disabled on cached dev %s\n",
dc->backing_dev_name); dc->backing_dev_name);
kfree(env[1]); ret = -EIO;
kfree(env[2]); goto out;
kfree(buf);
return -EIO;
} }
if (atomic_xchg(&dc->running, 1)) { if (atomic_xchg(&dc->running, 1)) {
kfree(env[1]);
kfree(env[2]);
kfree(buf);
pr_info("cached dev %s is running already\n", pr_info("cached dev %s is running already\n",
dc->backing_dev_name); dc->backing_dev_name);
return -EBUSY; ret = -EBUSY;
goto out;
} }
if (!d->c && if (!d->c &&
@ -1097,15 +1094,13 @@ int bch_cached_dev_run(struct cached_dev *dc)
* only class / kset properties are persistent * only class / kset properties are persistent
*/ */
kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
kfree(env[1]);
kfree(env[2]);
kfree(buf);
if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
sysfs_create_link(&disk_to_dev(d->disk)->kobj, sysfs_create_link(&disk_to_dev(d->disk)->kobj,
&d->kobj, "bcache")) { &d->kobj, "bcache")) {
pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n"); pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n");
return -ENOMEM; ret = -ENOMEM;
goto out;
} }
dc->status_update_thread = kthread_run(cached_dev_status_update, dc->status_update_thread = kthread_run(cached_dev_status_update,
@ -1114,7 +1109,11 @@ int bch_cached_dev_run(struct cached_dev *dc)
pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n"); pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n");
} }
return 0; out:
kfree(env[1]);
kfree(env[2]);
kfree(buf);
return ret;
} }
/* /*