[Devel] [PATCH rh7] fs: Resurrect generic_segment_checks()

Konstantin Khorenko khorenko at virtuozzo.com
Tue Dec 29 16:40:13 MSK 2020


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 at virtuozzo.com>
---
 include/linux/fs.h |  2 ++
 mm/filemap.c       | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 8398ec202a42..aee8adfc1252 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 950d92b6059b..585c57ef1e0a 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1981,6 +1981,45 @@ int file_read_actor(read_descriptor_t *desc, struct page *page,
 	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)
 {
-- 
2.24.3



More information about the Devel mailing list