linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Overstreet <kmo@daterainc.com>
To: axboe@kernel.dk, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org
Cc: hch@infradead.org, clm@fb.com, viro@zeniv.linux.org.uk,
	zab@redhat.com, shaggy@kernel.org,
	Kent Overstreet <kmo@daterainc.com>
Subject: [PATCH 09/11] block: iov_count_pages()
Date: Tue,  3 Dec 2013 14:00:15 -0800	[thread overview]
Message-ID: <1386108017-27964-10-git-send-email-kmo@daterainc.com> (raw)
In-Reply-To: <1386108017-27964-1-git-send-email-kmo@daterainc.com>

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
---
 fs/bio.c            | 42 ++++++------------------------------------
 include/linux/uio.h |  2 ++
 lib/iovec.c         | 30 ++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/fs/bio.c b/fs/bio.c
index a7e5789..a660e3f 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -1095,23 +1095,9 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
 	unsigned int len;
 	unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
 
-	for (i = 0; i < iter->nr_segs; i++) {
-		unsigned long uaddr;
-		unsigned long end;
-		unsigned long start;
-
-		uaddr = (unsigned long) iter->iov[i].iov_base;
-		end = (uaddr + iter->iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
-		start = uaddr >> PAGE_SHIFT;
-
-		/*
-		 * Overflow, abort
-		 */
-		if (end < start)
-			return ERR_PTR(-EINVAL);
-
-		nr_pages += end - start;
-	}
+	nr_pages = iov_count_pages(iter, 0);
+	if (nr_pages < 0)
+		return ERR_PTR(nr_pages);
 
 	if (offset)
 		nr_pages++;
@@ -1241,25 +1227,9 @@ static struct bio *__bio_map_user_iov(struct request_queue *q,
 	struct iov_iter i;
 	struct iovec iov;
 
-	iov_for_each(iov, i, *iter) {
-		unsigned long uaddr = (unsigned long) iov.iov_base;
-		unsigned long len = iov.iov_len;
-		unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
-		unsigned long start = uaddr >> PAGE_SHIFT;
-
-		/*
-		 * Overflow, abort
-		 */
-		if (end < start)
-			return ERR_PTR(-EINVAL);
-
-		nr_pages += end - start;
-		/*
-		 * buffer must be aligned to at least hardsector size for now
-		 */
-		if (uaddr & queue_dma_alignment(q))
-			return ERR_PTR(-EINVAL);
-	}
+	nr_pages = iov_count_pages(iter, queue_dma_alignment(q));
+	if (nr_pages < 0)
+		return ERR_PTR(nr_pages);
 
 	if (!nr_pages)
 		return ERR_PTR(-EINVAL);
diff --git a/include/linux/uio.h b/include/linux/uio.h
index d653fee..d74c3451 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -60,6 +60,8 @@ static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
 
 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to);
 
+int iov_count_pages(const struct iov_iter *iter, unsigned align);
+
 size_t iov_iter_copy_from_user_atomic(struct page *page,
 		struct iov_iter *i, unsigned long offset, size_t bytes);
 size_t iov_iter_copy_from_user(struct page *page,
diff --git a/lib/iovec.c b/lib/iovec.c
index 454baa8..fa2c050 100644
--- a/lib/iovec.c
+++ b/lib/iovec.c
@@ -1,5 +1,7 @@
 #include <linux/uaccess.h>
 #include <linux/export.h>
+#include <linux/fs.h>
+#include <linux/mm.h>
 #include <linux/uio.h>
 
 /*
@@ -51,3 +53,31 @@ int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
 	return 0;
 }
 EXPORT_SYMBOL(memcpy_toiovec);
+
+int iov_count_pages(const struct iov_iter *iter, unsigned align)
+{
+	struct iov_iter i = *iter;
+	int nr_pages = 0;
+
+	while (iov_iter_count(&i)) {
+		unsigned long uaddr = (unsigned long) i.iov->iov_base +
+			i.iov_offset;
+		unsigned long len = i.iov->iov_len - i.iov_offset;
+
+		if ((uaddr & align) || (len & align))
+			return -EINVAL;
+
+		/*
+		 * Overflow, abort
+		 */
+		if (uaddr + len < uaddr)
+			return -EINVAL;
+
+		nr_pages += DIV_ROUND_UP(len + offset_in_page(uaddr),
+					 PAGE_SIZE);
+		iov_iter_advance(&i, len);
+	}
+
+	return nr_pages;
+}
+EXPORT_SYMBOL(iov_count_pages);
-- 
1.8.4.4


  parent reply	other threads:[~2013-12-03 22:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-03 22:00 [PATCH] DIO rewrite Kent Overstreet
2013-12-03 22:00 ` [PATCH 01/11] block: Make generic_make_request handle arbitrary sized bios Kent Overstreet
2013-12-03 22:00 ` [PATCH 02/11] block: Gut bio_add_page() Kent Overstreet
2013-12-03 22:00 ` [PATCH 03/11] btrfs: generic_make_request() handles arbitrary size bios now Kent Overstreet
2013-12-03 22:00 ` [PATCH 04/11] btrfs: convert to bio_for_each_segment() Kent Overstreet
2013-12-03 22:00 ` [PATCH 05/11] iov_iter: Move iov_iter to uio.h Kent Overstreet
2013-12-06 11:09   ` Christoph Hellwig
2013-12-03 22:00 ` [PATCH 06/11] iov_iter: Kill iov_iter_single_seg_count() Kent Overstreet
2013-12-06 11:11   ` Christoph Hellwig
2013-12-03 22:00 ` [PATCH 07/11] iov_iter: Kill written arg to iov_iter_init() Kent Overstreet
2013-12-06 11:14   ` Christoph Hellwig
2013-12-03 22:00 ` [PATCH 08/11] block: convert to iov_iter Kent Overstreet
2013-12-06 19:50   ` Christoph Hellwig
2013-12-03 22:00 ` Kent Overstreet [this message]
2013-12-06 19:53   ` [PATCH 09/11] block: iov_count_pages() Christoph Hellwig
2013-12-03 22:00 ` [PATCH 10/11] block: Add bio_get_user_pages() Kent Overstreet
2013-12-06 19:57   ` Christoph Hellwig
2013-12-03 22:00 ` [PATCH 11/11] direct-io: Rewrite based on immutable biovecs Kent Overstreet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1386108017-27964-10-git-send-email-kmo@daterainc.com \
    --to=kmo@daterainc.com \
    --cc=axboe@kernel.dk \
    --cc=clm@fb.com \
    --cc=hch@infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shaggy@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zab@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).