[RHEL7,COMMIT] fs: Resurrect generic_segment_checks()

Submitted by Vasily Averin on Dec. 30, 2020, 10:27 a.m.

Details

Message ID 202012301027.0BUARU1S029531@vz7build.vvs.sw.ru
State New
Series "fs: Resurrect generic_segment_checks()"
Headers show

Commit Message

Vasily Averin Dec. 30, 2020, 10:27 a.m.
The commit is pushed to "branch-rh7-3.10.0-1160.11.1.vz7.172.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-1160.11.1.vz7.172.7
------>
commit eeb28e46662c0936389316fa6b8e70fb44b72ba9
Author: Konstantin Khorenko <khorenko@virtuozzo.com>
Date:   Wed Dec 30 13:27:30 2020 +0300

    fs: Resurrect generic_segment_checks()
    
    This is a partial revert of a9ca9d715754 ("kill generic_segment_checks()").
    
    The patch resurrects the function, but does not restore any call for it.
    
    Why this functiona is needed then?
    For zfs.
    
    VZ kernel does not have file_operations::read_iter()/write_iter()
    compatible with mainstream version thus zfs external module
    tries live without iters and assumes generic_segment_checks() exists.
    
    So keep the generic_segment_checks() specially for zfs.
    
    https://bugs.openvz.org/browse/OVZ-7243
    Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 include/linux/fs.h |  2 ++
 mm/filemap.c       | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

Patch hide | download patch | download mbox

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 8398ec2..aee8adf 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3255,6 +3255,8 @@  extern ssize_t generic_file_buffered_write_iter(struct kiocb *, struct iov_iter
 		loff_t, loff_t *, ssize_t);
 extern ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos);
 extern ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos);
+extern int generic_segment_checks(const struct iovec *iov,
+		unsigned long *nr_segs, size_t *count, int access_flags);
 
 /* fs/block_dev.c */
 extern ssize_t blkdev_aio_read(struct kiocb *, const struct iovec *,
diff --git a/mm/filemap.c b/mm/filemap.c
index 950d92b..585c57e 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1981,6 +1981,45 @@  success:
 	return size;
 }
 
+/*
+ * Performs necessary checks before doing a write
+ * @iov:	io vector request
+ * @nr_segs:	number of segments in the iovec
+ * @count:	number of bytes to write
+ * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
+ *
+ * Adjust number of segments and amount of bytes to write (nr_segs should be
+ * properly initialized first). Returns appropriate error code that caller
+ * should return or zero in case that write should be allowed.
+ */
+int generic_segment_checks(const struct iovec *iov,
+			unsigned long *nr_segs, size_t *count, int access_flags)
+{
+	unsigned long   seg;
+	size_t cnt = 0;
+	for (seg = 0; seg < *nr_segs; seg++) {
+		const struct iovec *iv = &iov[seg];
+
+		/*
+		 * If any segment has a negative length, or the cumulative
+		 * length ever wraps negative then return -EINVAL.
+		 */
+		cnt += iv->iov_len;
+		if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
+			return -EINVAL;
+		if (access_ok(access_flags, iv->iov_base, iv->iov_len))
+			continue;
+		if (seg == 0)
+			return -EFAULT;
+		*nr_segs = seg;
+		cnt -= iv->iov_len;	/* This segment is no good */
+		break;
+	}
+	*count = cnt;
+	return 0;
+}
+EXPORT_SYMBOL(generic_segment_checks);
+
 static int file_read_iter_actor(read_descriptor_t *desc, struct page *page,
 				unsigned long offset, unsigned long size)
 {