[Devel] [PATCH RH8 1/3] ploop: Move fget() code to separate function

Kirill Tkhai ktkhai at virtuozzo.com
Wed May 5 14:51:54 MSK 2021


Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-ploop-bat.c    |   12 +-----------
 drivers/md/dm-ploop-target.c |   30 ++++++++++++++++++++++++++----
 drivers/md/dm-ploop.h        |    2 +-
 3 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index c17f874fc25d..ba912001dca9 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -476,21 +476,13 @@ static int ploop_check_delta_length(struct ploop *ploop, struct file *file,
  * @fd refers to a new delta, which is placed right before top_delta.
  * So, userspace has to populate deltas stack from oldest to newest.
  */
-int ploop_add_delta(struct ploop *ploop, u32 level, int fd, bool is_raw)
+int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_raw)
 {
 	struct ploop_delta *deltas = ploop->deltas;
 	struct ploop_pvd_header *hdr = NULL;
-	struct file *file;
 	u32 size_in_clus;
 	int ret;
 
-	file = fget(fd);
-	if (!file)
-		return -ENOENT;
-	ret = -EBADF;
-	if (!(file->f_mode & FMODE_READ))
-		goto out;
-
 	ret = ploop_check_delta_length(ploop, file, &size_in_clus);
 	if (ret)
 		goto out;
@@ -515,7 +507,5 @@ int ploop_add_delta(struct ploop *ploop, u32 level, int fd, bool is_raw)
 	ret = 0;
 out:
 	vfree(hdr);
-	if (ret)
-		fput(file);
 	return ret;
 }
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 39c39a77b05f..155327242fc5 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -116,13 +116,28 @@ static int ploop_check_origin_dev(struct dm_target *ti, struct ploop *ploop)
 	return 0;
 }
 
+static struct file * get_delta_file(int fd)
+{
+	struct file *file;
+
+	file = fget(fd);
+	if (!file)
+		return ERR_PTR(-ENOENT);
+	if (!(file->f_mode & FMODE_READ)) {
+		fput(file);
+		return ERR_PTR(-EBADF);
+	}
+
+	return file;
+}
+
 static int ploop_add_deltas_stack(struct ploop *ploop, char **argv, int argc)
 {
 	struct ploop_delta *deltas;
 	int i, delta_fd, ret = 0;
+	struct file *file;
 	const char *arg;
 	bool is_raw;
-	u32 level;
 
 	if (!argc)
 		goto out;
@@ -150,10 +165,17 @@ static int ploop_add_deltas_stack(struct ploop *ploop, char **argv, int argc)
 		if (kstrtos32(arg, 10, &delta_fd) < 0)
 			goto out;
 
-		level = i;
-		ret = ploop_add_delta(ploop, level, delta_fd, is_raw);
-		if (ret < 0)
+		file = get_delta_file(delta_fd);
+		if (IS_ERR(file)) {
+			ret = PTR_ERR(file);
 			goto out;
+		}
+
+		ret = ploop_add_delta(ploop, i, file, is_raw);
+		if (ret < 0) {
+			fput(file);
+			goto out;
+		}
 	}
 
 	ret = 0;
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index e21a0ab83378..d0c613b743b8 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -488,7 +488,7 @@ extern void free_md_pages_tree(struct rb_root *root);
 extern bool try_update_bat_entry(struct ploop *ploop, unsigned int cluster,
 				 u8 level, unsigned int dst_cluster);
 
-extern int ploop_add_delta(struct ploop *ploop, u32 level, int fd, bool is_raw);
+extern int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_raw);
 extern void defer_bios(struct ploop *ploop, struct bio *bio, struct bio_list *bio_list);
 extern void do_ploop_work(struct work_struct *ws);
 extern void process_deferred_cmd(struct ploop *ploop,




More information about the Devel mailing list