[Devel] [PATCH rh7 3/4] ploop: get rid of dio_fsync()

Maxim Patlasov mpatlasov at virtuozzo.com
Wed May 18 18:23:37 PDT 2016


For ext4 dio_fsync() is actually equivalent to direct call to fsync fop:

1) file->f_op cannot be NULL;
2) file->f_op->fsync is always equal to ext4_sync_file;
3) ext4_sync_file() does filemap_write_and_wait() internally,
   no need to call it explicitly.

The patch also fixes a potential problem: if fsync() fails, it's better
to pass error code up to the stack of callers.

Signed-off-by: Maxim Patlasov <mpatlasov at virtuozzo.com>
---
 drivers/block/ploop/io_direct.c |   52 ++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 28 deletions(-)

diff --git a/drivers/block/ploop/io_direct.c b/drivers/block/ploop/io_direct.c
index a37f296..1ff848c 100644
--- a/drivers/block/ploop/io_direct.c
+++ b/drivers/block/ploop/io_direct.c
@@ -844,21 +844,6 @@ static int dio_fsync_thread(void * data)
 	return 0;
 }
 
-static int dio_fsync(struct file * file)
-{
-	int err, ret;
-	struct address_space *mapping = file->f_mapping;
-
-	ret = filemap_write_and_wait(mapping);
-	err = 0;
-	if (file->f_op && file->f_op->fsync) {
-		err = file->f_op->fsync(file, 0, LLONG_MAX, 0);
-		if (!ret)
-			ret = err;
-	}
-	return ret;
-}
-
 /* Invalidate page cache. It is called with inode mutex taken
  * and mapping mapping must be synced. If some dirty pages remained,
  * it will fail.
@@ -949,20 +934,17 @@ static void dio_destroy(struct ploop_io * io)
 static int dio_sync(struct ploop_io * io)
 {
 	struct file * file = io->files.file;
+	int err = 0;
 
 	if (file)
-		dio_fsync(file);
-	return 0;
+		err = file->f_op->fsync(file, 0, LLONG_MAX, 0);
+
+	return err;
 }
 
 static int dio_stop(struct ploop_io * io)
 {
-	struct file * file = io->files.file;
-
-	if (file) {
-		dio_fsync(file);
-	}
-	return 0;
+	return io->ops->sync(io);
 }
 
 static int dio_open(struct ploop_io * io)
@@ -979,7 +961,9 @@ static int dio_open(struct ploop_io * io)
 	io->files.inode = io->files.mapping->host;
 	io->files.bdev = io->files.inode->i_sb->s_bdev;
 
-	dio_fsync(file);
+	err = io->ops->sync(io);
+	if (err)
+		return err;
 
 	mutex_lock(&io->files.inode->i_mutex);
 	em_tree = ploop_dio_open(io, (delta->flags & PLOOP_FMT_RDONLY));
@@ -1646,7 +1630,11 @@ static int dio_prepare_snapshot(struct ploop_io * io, struct ploop_snapdata *sd)
 		return -EINVAL;
 	}
 
-	dio_fsync(file);
+	err = io->ops->sync(io);
+	if (err) {
+		fput(file);
+		return err;
+	}
 
 	mutex_lock(&io->files.inode->i_mutex);
 	err = dio_invalidate_cache(io->files.mapping, io->files.bdev);
@@ -1713,7 +1701,11 @@ static int dio_prepare_merge(struct ploop_io * io, struct ploop_snapdata *sd)
 		return -EINVAL;
 	}
 
-	dio_fsync(file);
+	err = io->ops->sync(io);
+	if (err) {
+		fput(file);
+		return err;
+	}
 
 	mutex_lock(&io->files.inode->i_mutex);
 
@@ -1772,8 +1764,12 @@ static int dio_truncate(struct ploop_io * io, struct file * file,
 	atomic_long_sub(*io->size_ptr - new_size, &ploop_io_images_size);
 	*io->size_ptr = new_size;
 
-	if (!err)
-		err = dio_fsync(file);
+	if (!err) {
+		if (io->files.file == file)
+			err = io->ops->sync(io);
+		else
+			err = file->f_op->fsync(file, 0, LLONG_MAX, 0);
+	}
 
 	return err;
 }



More information about the Devel mailing list