new primitive: iov_iter_for_each_range()
authorAl Viro <viro@zeniv.linux.org.uk>
Sat, 18 Feb 2017 06:44:03 +0000 (01:44 -0500)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 12 Oct 2017 02:36:54 +0000 (22:36 -0400)
For kvec and bvec: feeds segments to given callback as long as it
returns 0.  For iovec and pipe: fails.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
include/linux/uio.h
lib/iov_iter.c

index 5885daeae72145aad91946a9caa68153cd576ed5..e67e12adb1362da1e8ef729b914f6c5e05979efa 100644 (file)
@@ -244,4 +244,8 @@ int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
 int import_single_range(int type, void __user *buf, size_t len,
                 struct iovec *iov, struct iov_iter *i);
 
+int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
+                           int (*f)(struct kvec *vec, void *context),
+                           void *context);
+
 #endif
index 1c1c06ddc20a8a961d7d9f73e43d65b695636d5d..970212670b6a11bf8e033bd7d0cc0fff34041dc8 100644 (file)
@@ -1446,3 +1446,25 @@ int import_single_range(int rw, void __user *buf, size_t len,
        return 0;
 }
 EXPORT_SYMBOL(import_single_range);
+
+int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
+                           int (*f)(struct kvec *vec, void *context),
+                           void *context)
+{
+       struct kvec w;
+       int err = -EINVAL;
+       if (!bytes)
+               return 0;
+
+       iterate_all_kinds(i, bytes, v, -EINVAL, ({
+               w.iov_base = kmap(v.bv_page) + v.bv_offset;
+               w.iov_len = v.bv_len;
+               err = f(&w, context);
+               kunmap(v.bv_page);
+               err;}), ({
+               w = v;
+               err = f(&w, context);})
+       )
+       return err;
+}
+EXPORT_SYMBOL(iov_iter_for_each_range);