Skip to contentSkip to conversation

batch-write.patch

PATCH In review Hans Reiser 8 participants 20 messages Jun 29, 2006 – Jul 10, 2006

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 batch-write.patch

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

diff -puN mm/filemap.c~batched-write mm/filemap.c

diff -puN include/linux/fs.h~batched-write include/linux/fs.h

include/linux/fs.h renamed from include/linux/fs.h~batched-write +35 -0
diff -puN include/linux/fs.h~batched-write include/linux/fs.h
@@ -346,6 +346,39 @@ enum positive_aop_returns {
 struct page;
 struct address_space;
 struct writeback_control;
+typedef struct write_descriptor write_descriptor_t;
+
+/*
+ * This is "write_actor" function type, used by write() to copy data from user
+ * space. There are two functions of this type: write_actor and
+ * write_iovec_actor. First is used when user data are in one segment, second
+ * is used in case of vectored write.
+ */
+typedef size_t (*write_actor_t)(struct page *, unsigned long, size_t,
+				const write_descriptor_t *);
+
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @cur_iov: current i/o vector
+ * @iov_off: offset within current i/o vector
+ * @buf: user data
+ * @actor: function to copy user data with 
+ *
+ * This structure is to pass to batch_write address space operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	const struct iovec *cur_iov;
+	size_t iov_off;
+	char __user *buf;
+	write_actor_t actor;
+};
+
+#include <linux/pagevec.h>
 
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
@@ -367,6 +400,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *, const write_descriptor_t *,
+			    struct pagevec *, struct page **, size_t *);
 	/* Unfortunately this kludge is needed for FIBMAP. Don't use it */
 	sector_t (*bmap)(struct address_space *, sector_t);
 	void (*invalidatepage) (struct page *, unsigned long);
mm/filemap.c renamed from mm/filemap.c~batched-write +184 -85

313 lines not on this page · raw message

_

#1 lore raw
Quoted 3 lines · Hans Reiser

(patch was attached)

Not quoted because patch isn't inlined, but you're testing for the presence of the batch_write pointer repeatedly in the loop. How about declare a batch_write ptr on the stack and then do your test once, outside of your do { } while (count) loop, and then set it to the generic method (before entering the loop) if the generic method isn't available?

Thanks,
Chase

#2 lore raw
Quoted 7 lines · Chase Venters
Quoted 3 lines · Hans Reiser

(patch was attached)

Not quoted because patch isn't inlined, but you're testing for the presence of the batch_write pointer repeatedly in the loop. How about declare a batch_write ptr on the stack and then do your test once, outside of your do { } while (count) loop, and then set it to the generic method (before entering the loop) if the generic method isn't available?

                                        ^^^^^^^^^

s/available/overridden/;

Quoted 2 lines

Thanks,
Chase

#3 lore raw
In reply to Chase Venters

Chase, we incorporated your suggestion, and I want to thank you for it.

Hans

Quoted 17 lines · Chase Venters
Quoted 3 lines · Hans Reiser

(patch was attached)

Not quoted because patch isn't inlined, but you're testing for the presence of the batch_write pointer repeatedly in the loop. How about declare a batch_write ptr on the stack and then do your test once, outside of your do { } while (count) loop, and then set it to the generic method (before entering the loop) if the generic method isn't available?

Thanks,
Chase

#4 lore raw
In reply to Hans Reiser
Quoted 7 lines · On Thu, 29 Jun 2006 12:17:36 -0700 Hans Reiser <[email protected]> wrote:

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

Please remember to fill in the authorship and signed-off-by info.

The approach seems sane.

include/linux/fs.h enum positive_aop_returns { · line 349

Quoted 11 lines

diff -puN include/linux/fs.h~batched-write include/linux/fs.h

include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
@@ -346,6 +346,39 @@ enum positive_aop_returns {
 struct page;
 struct address_space;
 struct writeback_control;
+typedef struct write_descriptor write_descriptor_t;

Let's just use `struct write descriptor' throughout please.

include/linux/fs.h enum positive_aop_returns { · line 358

Quoted 9 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +9 -0
+
+/*
+ * This is "write_actor" function type, used by write() to copy data from user
+ * space. There are two functions of this type: write_actor and
+ * write_iovec_actor. First is used when user data are in one segment, second
+ * is used in case of vectored write.
+ */
+typedef size_t (*write_actor_t)(struct page *, unsigned long, size_t,
+				const write_descriptor_t *);

But yes, we do use typedefs for these things - they're such a mouthful.

Can we please fill in the variable names in declarations such as this? You look at it and wonder what that unsigned long' and size_t' actually _do_. It's so much clearer if the names are filled in.

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @cur_iov: current i/o vector
+ * @iov_off: offset within current i/o vector
+ * @buf: user data
+ * @actor: function to copy user data with 
+ *
+ * This structure is to pass to batch_write address space operation all

include/linux/fs.h enum positive_aop_returns { · line 379

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	const struct iovec *cur_iov;
+	size_t iov_off;
+	char __user *buf;
+	write_actor_t actor;
+};

Boy, it's complex, isn't it? Not your fault though.

include/linux/fs.h enum positive_aop_returns { · line 381

Quoted 1 line
include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
+#include <linux/pagevec.h>

A simple declaration:

struct pagevec;

would suffice here. Please place it right at the top-of-file, with the others.

include/linux/fs.h struct address_space_operations { · line 404

Quoted 8 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
@@ -367,6 +400,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *, const write_descriptor_t *,
+			    struct pagevec *, struct page **, size_t *);

It's nice to fill in the names here too, IMO. Yes, there are past sins there.

Should this be an address_space_operation or a file_operation?

include/linux/fs.h struct address_space_operations { · line 406

Quoted 2 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
+/**
+ * generic_batch_write - generic implementation of batched write

gneric_file_batch_write(), perhaps? I guess that depends upon the answer to the previous question.

Quoted 5 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +5 -0
+ * @file: the file to write to
+ * @desc: set of write arguments
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ * @written: returned number of bytes successfully written

include/linux/fs.h struct address_space_operations {

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+ *
+ * This implementation of batch_write method writes not more than one page of
+ * file. It faults in user space, allocates page and calls prepare_write and
+ * commit_write address space operations. User data are copied by an actor
+ * which is set by caller depending on whether write or writev is on the way.
+ */
+static long generic_batch_write(struct file *file,
+ 				const write_descriptor_t *desc,
+ 				struct pagevec *lru_pvec,
+ 				struct page **cached_page, size_t *written)

I wonder if we really need that cached_page thing.

Did you consider putting more of these arguments into the (now non-const) `struct write_descriptor'? It'd be sligtly more efficient and would save stack on some of our stack-hungriest codepaths.

Quoted 56 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +25 -20
+{
+ 	const struct address_space_operations *a_ops = file->f_mapping->a_ops;
+	struct page *page;
+	unsigned long index;
+	size_t bytes;
+	unsigned long offset;
+	long status;
+
+	/* offset within page write is to start at */
+	offset = (desc->pos & (PAGE_CACHE_SIZE - 1));
 
-	do {
-		unsigned long index;
-		unsigned long offset;
-		size_t copied;
-
-		offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
-		index = pos >> PAGE_CACHE_SHIFT;
-		bytes = PAGE_CACHE_SIZE - offset;
+	/* index of page we are to write to */
+	index = desc->pos >> PAGE_CACHE_SHIFT;
 
-		/* Limit the size of the copy to the caller's write size */
-		bytes = min(bytes, count);
+	/* number of bytes which can be written to the page */
+	bytes = PAGE_CACHE_SIZE - offset;
 
-		/*
-		 * Limit the size of the copy to that of the current segment,
-		 * because fault_in_pages_readable() doesn't know how to walk
-		 * segments.
-		 */
-		bytes = min(bytes, cur_iov->iov_len - iov_base);
+	/* Limit the size of the copy to the caller's write size */
+	bytes = min(bytes, desc->count);
 
+	/*
+	 * Limit the size of the copy to that of the current segment,
+	 * because fault_in_pages_readable() doesn't know how to walk
+	 * segments.
+	 */
+	bytes = min(bytes, desc->cur_iov->iov_len - desc->iov_off);
+
+	while (1) {
 		/*
 		 * Bring in the user page that we will copy from _first_.
 		 * Otherwise there's a nasty deadlock on copying from the
 		 * same page as we're writing to, without it being marked
 		 * up-to-date.
 		 */
-		fault_in_pages_readable(buf, bytes);
+		fault_in_pages_readable(desc->buf, bytes);
 
-		page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
-		if (!page) {
-			status = -ENOMEM;

include/linux/fs.h

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +6 -3
-			break;
-		}
+		page = __grab_cache_page(file->f_mapping, index, cached_page,
+					 lru_pvec);
+		if (!page)
+			return -ENOMEM;
 
-		status = a_ops->prepare_write(file, page, offset, offset+bytes);
+		status = a_ops->prepare_write(file, page, offset,
+					      offset+bytes);

The code you're patching here changed this morning, and it'll change again (a little bit) in the next few days.

And Neil has suggested some simplifications to filemap_set_next_iovec(), although I don't think anyone's working on that at present (everyone's justifiably afraid to touch this code).

Quoted 41 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +41 -0
+
+ssize_t
+generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
+			    unsigned long nr_segs, loff_t pos, loff_t *ppos,
+			    size_t count, ssize_t written)
+{
+	struct file *file = iocb->ki_filp;
+	struct address_space * mapping = file->f_mapping;
+	const struct address_space_operations *a_ops = mapping->a_ops;
+	struct inode 	*inode = mapping->host;
+	long		status;
+	struct page	*cached_page = NULL;
+	struct pagevec	lru_pvec;
+	write_descriptor_t desc;
+	size_t copied = 0;
+
+	pagevec_init(&lru_pvec, 0);
+
+	/*
+	 * initialize write descriptor fields: position to write to
+	 * and number of bytes to write
+	 */
+	desc.pos = pos;
+	desc.count = count;
+
+	/*
+	 * handle partial DIO write.  Adjust cur_iov if needed.
+	 */
+	if (likely(nr_segs == 1)) {
+		desc.cur_iov = iov;
+		desc.iov_off = written;
+		desc.actor = write_actor;
+	} else {
+		filemap_set_next_iovec(&desc.cur_iov, &desc.iov_off, written);
+		desc.actor = write_iovec_actor;
+	}
+	/* pointer to user buffer */
+	desc.buf = desc.cur_iov->iov_base + desc.iov_off;
+
+	do {
+		/*

include/linux/fs.h

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+		 * When calling the filesystem for writes, there is processing
+		 * that must be done:
+		 * 1) per word
+		 * 2) per page
+		 * 3) per call to the FS
+		 * If the FS is called per page, then it turns out that 3)
+		 * costs more than 1) and 2) for sophisticated filesystems.  To
+		 * allow the FS to choose to pay the cost of 3) only once we
+		 * call batch_write, if the FS supports it.
+		 */

I think it's better that the more philosophical comments like this go in the comment block at the start of the function.

include/linux/fs.h

Quoted 7 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +7 -0
+		if (a_ops->batch_write)
+			status = a_ops->batch_write(file, &desc, &lru_pvec,
+						    &cached_page, &copied);
+		else
+			status = generic_batch_write(file, &desc, &lru_pvec,
+						     &cached_page, &copied);
+		if (likely(copied > 0)) {

This is missing this morning's handle-zero-length-segment bugfix.

Quoted 29 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +20 -2
+			written += copied;
+			desc.count -= copied;
+			if (desc.count) {
+				/*
+				 * not everything is written yet. Adjust write
+				 * descriptor for next iteration
+				 */
+				desc.pos += copied;
+				if (unlikely(nr_segs > 1))
+					filemap_set_next_iovec(&desc.cur_iov,
+							       &desc.iov_off,
+							       copied);
+				else
+					desc.iov_off += copied;
+				desc.buf = desc.cur_iov->iov_base +
+					desc.iov_off;
+			}
+		}
 		if (status < 0)
 			break;
 		balance_dirty_pages_ratelimited(mapping);
 		cond_resched();
-	} while (count);
-	*ppos = pos;
+	} while (desc.count);	
+	*ppos = pos + written;
 
 	if (cached_page)
 		page_cache_release(cached_page);
#5 lore raw

Hello

Andrew, thanks a lot for your improvements.
Here is new version of the patch.

Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Thu, 29 Jun 2006 12:17:36 -0700 Hans Reiser <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

Please remember to fill in the authorship and signed-off-by info.

done.

Quoted 5 lines

The approach seems sane.

Quoted 3 lines
Quoted 11 lines

include/linux/fs.h enum positive_aop_returns { · line 349

Quoted 8 lines

diff -puN include/linux/fs.h~batched-write include/linux/fs.h

include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
@@ -346,6 +346,39 @@ enum positive_aop_returns {
 struct page;
 struct address_space;
 struct writeback_control;
+typedef struct write_descriptor write_descriptor_t;

Let's just use `struct write descriptor' throughout please.

done.

Quoted 12 lines

include/linux/fs.h enum positive_aop_returns { · line 358

Quoted 9 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +9 -0
+
+/*
+ * This is "write_actor" function type, used by write() to copy data from user
+ * space. There are two functions of this type: write_actor and
+ * write_iovec_actor. First is used when user data are in one segment, second
+ * is used in case of vectored write.
+ */
+typedef size_t (*write_actor_t)(struct page *, unsigned long, size_t,
+				const write_descriptor_t *);

But yes, we do use typedefs for these things - they're such a mouthful.

As long as now we never call batch_write to write from more than segment - we do no need write_actor. filemap_copy_from_user is only used to copy data.

Quoted 18 lines

Can we please fill in the variable names in declarations such as this? You look at it and wonder what that unsigned long' and size_t' actually _do_. It's so much clearer if the names are filled in.

Quoted 14 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +14 -0
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @cur_iov: current i/o vector
+ * @iov_off: offset within current i/o vector
+ * @buf: user data
+ * @actor: function to copy user data with 
+ *
+ * This structure is to pass to batch_write address space operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
Quoted 9 lines

include/linux/fs.h enum positive_aop_returns { · line 378

Quoted 6 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +6 -0
+	size_t count;
+	const struct iovec *cur_iov;
+	size_t iov_off;
+	char __user *buf;
+	write_actor_t actor;
+};

Boy, it's complex, isn't it? Not your fault though.

struct write_descriptor changed. It has 5 fields now.

Quoted 8 lines

include/linux/fs.h enum positive_aop_returns { · line 379

Quoted 1 line
include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
+#include <linux/pagevec.h>

A simple declaration:

struct pagevec;

would suffice here. Please place it right at the top-of-file, with the others.

done.

Quoted 12 lines

include/linux/fs.h struct address_space_operations { · line 404

Quoted 8 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
@@ -367,6 +400,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *, const write_descriptor_t *,
+			    struct pagevec *, struct page **, size_t *);

It's nice to fill in the names here too, IMO. Yes, there are past sins there.

done.

Quoted 2 lines

Should this be an address_space_operation or a file_operation?

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

Quoted 6 lines

include/linux/fs.h struct address_space_operations { · line 406

Quoted 2 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
+/**
+ * generic_batch_write - generic implementation of batched write

gneric_file_batch_write(), perhaps? I guess that depends upon the answer to the previous question.

renamed.

Quoted 16 lines
Quoted 5 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +5 -0
+ * @file: the file to write to
+ * @desc: set of write arguments
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ * @written: returned number of bytes successfully written

include/linux/fs.h struct address_space_operations {

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+ *
+ * This implementation of batch_write method writes not more than one page of
+ * file. It faults in user space, allocates page and calls prepare_write and
+ * commit_write address space operations. User data are copied by an actor
+ * which is set by caller depending on whether write or writev is on the way.
+ */
+static long generic_batch_write(struct file *file,
+ 				const write_descriptor_t *desc,
+ 				struct pagevec *lru_pvec,
+ 				struct page **cached_page, size_t *written)
Quoted 6 lines

I wonder if we really need that cached_page thing.

Did you consider putting more of these arguments into the (now non-const) `struct write_descriptor'? It'd be sligtly more efficient and would save stack on some of our stack-hungriest codepaths.

lru_pvec and cached_page are included to struct write_descriptor.

Quoted 63 lines
Quoted 63 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +29 -22
+{
+ 	const struct address_space_operations *a_ops = file->f_mapping->a_ops;
+	struct page *page;
+	unsigned long index;
+	size_t bytes;
+	unsigned long offset;
+	long status;
+
+	/* offset within page write is to start at */
+	offset = (desc->pos & (PAGE_CACHE_SIZE - 1));
 
-	do {
-		unsigned long index;
-		unsigned long offset;
-		size_t copied;
-
-		offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
-		index = pos >> PAGE_CACHE_SHIFT;
-		bytes = PAGE_CACHE_SIZE - offset;
+	/* index of page we are to write to */
+	index = desc->pos >> PAGE_CACHE_SHIFT;
 
-		/* Limit the size of the copy to the caller's write size */
-		bytes = min(bytes, count);
+	/* number of bytes which can be written to the page */
+	bytes = PAGE_CACHE_SIZE - offset;
 
-		/*
-		 * Limit the size of the copy to that of the current segment,
-		 * because fault_in_pages_readable() doesn't know how to walk
-		 * segments.
-		 */
-		bytes = min(bytes, cur_iov->iov_len - iov_base);
+	/* Limit the size of the copy to the caller's write size */
+	bytes = min(bytes, desc->count);
 
+	/*
+	 * Limit the size of the copy to that of the current segment,
+	 * because fault_in_pages_readable() doesn't know how to walk
+	 * segments.
+	 */
+	bytes = min(bytes, desc->cur_iov->iov_len - desc->iov_off);
+
+	while (1) {
 		/*
 		 * Bring in the user page that we will copy from _first_.
 		 * Otherwise there's a nasty deadlock on copying from the
 		 * same page as we're writing to, without it being marked
 		 * up-to-date.
 		 */
-		fault_in_pages_readable(buf, bytes);
+		fault_in_pages_readable(desc->buf, bytes);
 
-		page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
-		if (!page) {
-			status = -ENOMEM;
-			break;
-		}
+		page = __grab_cache_page(file->f_mapping, index, cached_page,
+					 lru_pvec);
+		if (!page)
+			return -ENOMEM;
Quoted 7 lines

include/linux/fs.h

Quoted 3 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -1
-		status = a_ops->prepare_write(file, page, offset, offset+bytes);
+		status = a_ops->prepare_write(file, page, offset,
+					      offset+bytes);

The code you're patching here changed this morning, and it'll change again (a little bit) in the next few days.

New patch is against 2.6.17-mm5.

Quoted 49 lines

And Neil has suggested some simplifications to filemap_set_next_iovec(), although I don't think anyone's working on that at present (everyone's justifiably afraid to touch this code).

Quoted 46 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +46 -0
+
+ssize_t
+generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
+			    unsigned long nr_segs, loff_t pos, loff_t *ppos,
+			    size_t count, ssize_t written)
+{
+	struct file *file = iocb->ki_filp;
+	struct address_space * mapping = file->f_mapping;
+	const struct address_space_operations *a_ops = mapping->a_ops;
+	struct inode 	*inode = mapping->host;
+	long		status;
+	struct page	*cached_page = NULL;
+	struct pagevec	lru_pvec;
+	write_descriptor_t desc;
+	size_t copied = 0;
+
+	pagevec_init(&lru_pvec, 0);
+
+	/*
+	 * initialize write descriptor fields: position to write to
+	 * and number of bytes to write
+	 */
+	desc.pos = pos;
+	desc.count = count;
+
+	/*
+	 * handle partial DIO write.  Adjust cur_iov if needed.
+	 */
+	if (likely(nr_segs == 1)) {
+		desc.cur_iov = iov;
+		desc.iov_off = written;
+		desc.actor = write_actor;
+	} else {
+		filemap_set_next_iovec(&desc.cur_iov, &desc.iov_off, written);
+		desc.actor = write_iovec_actor;
+	}
+	/* pointer to user buffer */
+	desc.buf = desc.cur_iov->iov_base + desc.iov_off;
+
+	do {
+		/*
+		 * When calling the filesystem for writes, there is processing
+		 * that must be done:
+		 * 1) per word
+		 * 2) per page
+		 * 3) per call to the FS
Quoted 9 lines

include/linux/fs.h

Quoted 5 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +5 -0
+		 * If the FS is called per page, then it turns out that 3)
+		 * costs more than 1) and 2) for sophisticated filesystems.  To
+		 * allow the FS to choose to pay the cost of 3) only once we
+		 * call batch_write, if the FS supports it.
+		 */

I think it's better that the more philosophical comments like this go in the comment block at the start of the function.

This comment is moved at the start of this function.

Quoted 10 lines

include/linux/fs.h

Quoted 7 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +7 -0
+		if (a_ops->batch_write)
+			status = a_ops->batch_write(file, &desc, &lru_pvec,
+						    &cached_page, &copied);
+		else
+			status = generic_batch_write(file, &desc, &lru_pvec,
+						     &cached_page, &copied);
+		if (likely(copied > 0)) {

This is missing this morning's handle-zero-length-segment bugfix.

I hope new patch deals properly with zero length segments. I writev-ed a vector which had 3 zero length segments: first, last and 3rd.

Quoted 27 lines
Quoted 27 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +20 -2
+			written += copied;
+			desc.count -= copied;
+			if (desc.count) {
+				/*
+				 * not everything is written yet. Adjust write
+				 * descriptor for next iteration
+				 */
+				desc.pos += copied;
+				if (unlikely(nr_segs > 1))
+					filemap_set_next_iovec(&desc.cur_iov,
+							       &desc.iov_off,
+							       copied);
+				else
+					desc.iov_off += copied;
+				desc.buf = desc.cur_iov->iov_base +
+					desc.iov_off;
+			}
+		}
 		if (status < 0)
 			break;
 		balance_dirty_pages_ratelimited(mapping);
 		cond_resched();
-	} while (count);
-	*ppos = pos;
+	} while (desc.count);	
+	*ppos = pos + written;
Quoted 4 lines

include/linux/fs.h

Quoted 2 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +0 -0
 	if (cached_page)
 		page_cache_release(cached_page);

From: Vladimir Saveliev <[email protected]>

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

include/linux/fs.h renamed from include/linux/fs.h~batched-write +22 -0
diff -puN include/linux/fs.h~batched-write include/linux/fs.h
@@ -246,6 +246,7 @@ struct poll_table_struct;
 struct kstatfs;
 struct vm_area_struct;
 struct vfsmount;
+struct pagevec;
 
 extern void __init inode_init(unsigned long);
 extern void __init inode_init_early(void);
@@ -347,6 +348,25 @@ struct page;
 struct address_space;
 struct writeback_control;
 
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @buf: pointer to data to be written
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ *
+ * This structure is to pass to batch_write address space operation
+ * all information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	char __user *buf;
+	struct page *cached_page;
+	struct pagevec *lru_pvec;
+};
+
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
 	int (*readpage)(struct file *, struct page *);
@@ -367,6 +387,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	/* Unfortunately this kludge is needed for FIBMAP. Don't use it */
 	sector_t (*bmap)(struct address_space *, sector_t);
 	void (*invalidatepage) (struct page *, unsigned long);
mm/filemap.c renamed from mm/filemap.c~batched-write +161 -103

300 lines not on this page · raw message

_

From: Vladimir Saveliev <[email protected]>

This patch adds a method batch_write to struct file_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

include/linux/fs.h renamed from include/linux/fs.h~batched-write +22 -0
diff -puN include/linux/fs.h~batched-write include/linux/fs.h
@@ -246,6 +246,7 @@ struct poll_table_struct;
 struct kstatfs;
 struct vm_area_struct;
 struct vfsmount;
+struct pagevec;
 
 extern void __init inode_init(unsigned long);
 extern void __init inode_init_early(void);
@@ -1096,6 +1097,25 @@ typedef int (*read_actor_t)(read_descrip
 #define HAVE_COMPAT_IOCTL 1
 #define HAVE_UNLOCKED_IOCTL 1
 
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @buf: pointer to data to be written
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ *
+ * This structure is to pass to batch_write file operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	char __user *buf;
+	struct page *cached_page;
+	struct pagevec *lru_pvec;
+};
+
 /*
  * NOTE:
  * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
@@ -1123,6 +1143,8 @@ struct file_operations {
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
 	ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
mm/filemap.c renamed from mm/filemap.c~batched-write +163 -104

302 lines not on this page · raw message

Documentation/filesystems/vfs.txt renamed from Documentation/filesystems/vfs.txt~batched-write +4 -0
diff -puN Documentation/filesystems/vfs.txt~batched-write Documentation/filesystems/vfs.txt
@@ -717,6 +717,8 @@ struct file_operations {
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
 	ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
@@ -784,6 +786,8 @@ otherwise noted.
 
   writev: called by the writev(2) system call
 
+  batch_write: optional, if implemented called by writev(2) and write(2)
+
   sendfile: called by the sendfile(2) system call
 
   get_unmapped_area: called by the mmap(2) system call

_

#6 lore raw

Hello

Sorry, previous mail contained 2 patches incorrectly inlined. This time everything should be correct.

Quoted 299 lines · Vladimir V. Saveliev

Hello

Andrew, thanks a lot for your improvements.
Here is new version of the patch.

Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Thu, 29 Jun 2006 12:17:36 -0700 Hans Reiser <[email protected]> wrote:
Quoted 8 lines
Quoted 5 lines

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

Please remember to fill in the authorship and signed-off-by info.

done.

Quoted 5 lines

The approach seems sane.

Quoted 3 lines
Quoted 11 lines

include/linux/fs.h enum positive_aop_returns { · line 349

Quoted 8 lines

diff -puN include/linux/fs.h~batched-write include/linux/fs.h

include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
@@ -346,6 +346,39 @@ enum positive_aop_returns {
 struct page;
 struct address_space;
 struct writeback_control;
+typedef struct write_descriptor write_descriptor_t;

Let's just use `struct write descriptor' throughout please.

done.

Quoted 12 lines

include/linux/fs.h enum positive_aop_returns { · line 358

Quoted 9 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +9 -0
+
+/*
+ * This is "write_actor" function type, used by write() to copy data from user
+ * space. There are two functions of this type: write_actor and
+ * write_iovec_actor. First is used when user data are in one segment, second
+ * is used in case of vectored write.
+ */
+typedef size_t (*write_actor_t)(struct page *, unsigned long, size_t,
+				const write_descriptor_t *);

But yes, we do use typedefs for these things - they're such a mouthful.

As long as now we never call batch_write to write from more than segment - we do no need write_actor. filemap_copy_from_user is only used to copy data.

Quoted 18 lines

Can we please fill in the variable names in declarations such as this? You look at it and wonder what that unsigned long' and size_t' actually _do_. It's so much clearer if the names are filled in.

Quoted 14 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +14 -0
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @cur_iov: current i/o vector
+ * @iov_off: offset within current i/o vector
+ * @buf: user data
+ * @actor: function to copy user data with 
+ *
+ * This structure is to pass to batch_write address space operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
Quoted 9 lines

include/linux/fs.h enum positive_aop_returns { · line 378

Quoted 6 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +6 -0
+	size_t count;
+	const struct iovec *cur_iov;
+	size_t iov_off;
+	char __user *buf;
+	write_actor_t actor;
+};

Boy, it's complex, isn't it? Not your fault though.

struct write_descriptor changed. It has 5 fields now.

Quoted 8 lines

include/linux/fs.h enum positive_aop_returns { · line 379

Quoted 1 line
include/linux/fs.h renamed from include/linux/fs.h~batched-write +1 -0
+#include <linux/pagevec.h>

A simple declaration:

struct pagevec;

would suffice here. Please place it right at the top-of-file, with the others.

done.

Quoted 12 lines

include/linux/fs.h struct address_space_operations { · line 404

Quoted 8 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
@@ -367,6 +400,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *, const write_descriptor_t *,
+			    struct pagevec *, struct page **, size_t *);

It's nice to fill in the names here too, IMO. Yes, there are past sins there.

done.

Quoted 2 lines

Should this be an address_space_operation or a file_operation?

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

Quoted 6 lines

include/linux/fs.h struct address_space_operations { · line 406

Quoted 2 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -0
+/**
+ * generic_batch_write - generic implementation of batched write

gneric_file_batch_write(), perhaps? I guess that depends upon the answer to the previous question.

renamed.

Quoted 16 lines
Quoted 5 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +5 -0
+ * @file: the file to write to
+ * @desc: set of write arguments
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ * @written: returned number of bytes successfully written

include/linux/fs.h struct address_space_operations {

Quoted 10 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +10 -0
+ *
+ * This implementation of batch_write method writes not more than one page of
+ * file. It faults in user space, allocates page and calls prepare_write and
+ * commit_write address space operations. User data are copied by an actor
+ * which is set by caller depending on whether write or writev is on the way.
+ */
+static long generic_batch_write(struct file *file,
+ 				const write_descriptor_t *desc,
+ 				struct pagevec *lru_pvec,
+ 				struct page **cached_page, size_t *written)
Quoted 6 lines

I wonder if we really need that cached_page thing.

Did you consider putting more of these arguments into the (now non-const) `struct write_descriptor'? It'd be sligtly more efficient and would save stack on some of our stack-hungriest codepaths.

lru_pvec and cached_page are included to struct write_descriptor.

Quoted 63 lines
Quoted 63 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +29 -22
+{
+ 	const struct address_space_operations *a_ops = file->f_mapping->a_ops;
+	struct page *page;
+	unsigned long index;
+	size_t bytes;
+	unsigned long offset;
+	long status;
+
+	/* offset within page write is to start at */
+	offset = (desc->pos & (PAGE_CACHE_SIZE - 1));
 
-	do {
-		unsigned long index;
-		unsigned long offset;
-		size_t copied;
-
-		offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
-		index = pos >> PAGE_CACHE_SHIFT;
-		bytes = PAGE_CACHE_SIZE - offset;
+	/* index of page we are to write to */
+	index = desc->pos >> PAGE_CACHE_SHIFT;
 
-		/* Limit the size of the copy to the caller's write size */
-		bytes = min(bytes, count);
+	/* number of bytes which can be written to the page */
+	bytes = PAGE_CACHE_SIZE - offset;
 
-		/*
-		 * Limit the size of the copy to that of the current segment,
-		 * because fault_in_pages_readable() doesn't know how to walk
-		 * segments.
-		 */
-		bytes = min(bytes, cur_iov->iov_len - iov_base);
+	/* Limit the size of the copy to the caller's write size */
+	bytes = min(bytes, desc->count);
 
+	/*
+	 * Limit the size of the copy to that of the current segment,
+	 * because fault_in_pages_readable() doesn't know how to walk
+	 * segments.
+	 */
+	bytes = min(bytes, desc->cur_iov->iov_len - desc->iov_off);
+
+	while (1) {
 		/*
 		 * Bring in the user page that we will copy from _first_.
 		 * Otherwise there's a nasty deadlock on copying from the
 		 * same page as we're writing to, without it being marked
 		 * up-to-date.
 		 */
-		fault_in_pages_readable(buf, bytes);
+		fault_in_pages_readable(desc->buf, bytes);
 
-		page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
-		if (!page) {
-			status = -ENOMEM;
-			break;
-		}
+		page = __grab_cache_page(file->f_mapping, index, cached_page,
+					 lru_pvec);
+		if (!page)
+			return -ENOMEM;
Quoted 7 lines

include/linux/fs.h

Quoted 3 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +2 -1
-		status = a_ops->prepare_write(file, page, offset, offset+bytes);
+		status = a_ops->prepare_write(file, page, offset,
+					      offset+bytes);

The code you're patching here changed this morning, and it'll change again (a little bit) in the next few days.

New patch is against 2.6.17-mm5.

Quoted 49 lines

And Neil has suggested some simplifications to filemap_set_next_iovec(), although I don't think anyone's working on that at present (everyone's justifiably afraid to touch this code).

Quoted 46 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +46 -0
+
+ssize_t
+generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
+			    unsigned long nr_segs, loff_t pos, loff_t *ppos,
+			    size_t count, ssize_t written)
+{
+	struct file *file = iocb->ki_filp;
+	struct address_space * mapping = file->f_mapping;
+	const struct address_space_operations *a_ops = mapping->a_ops;
+	struct inode 	*inode = mapping->host;
+	long		status;
+	struct page	*cached_page = NULL;
+	struct pagevec	lru_pvec;
+	write_descriptor_t desc;
+	size_t copied = 0;
+
+	pagevec_init(&lru_pvec, 0);
+
+	/*
+	 * initialize write descriptor fields: position to write to
+	 * and number of bytes to write
+	 */
+	desc.pos = pos;
+	desc.count = count;
+
+	/*
+	 * handle partial DIO write.  Adjust cur_iov if needed.
+	 */
+	if (likely(nr_segs == 1)) {
+		desc.cur_iov = iov;
+		desc.iov_off = written;
+		desc.actor = write_actor;
+	} else {
+		filemap_set_next_iovec(&desc.cur_iov, &desc.iov_off, written);
+		desc.actor = write_iovec_actor;
+	}
+	/* pointer to user buffer */
+	desc.buf = desc.cur_iov->iov_base + desc.iov_off;
+
+	do {
+		/*
+		 * When calling the filesystem for writes, there is processing
+		 * that must be done:
+		 * 1) per word
+		 * 2) per page
+		 * 3) per call to the FS
Quoted 9 lines

include/linux/fs.h

Quoted 5 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +5 -0
+		 * If the FS is called per page, then it turns out that 3)
+		 * costs more than 1) and 2) for sophisticated filesystems.  To
+		 * allow the FS to choose to pay the cost of 3) only once we
+		 * call batch_write, if the FS supports it.
+		 */

I think it's better that the more philosophical comments like this go in the comment block at the start of the function.

This comment is moved at the start of this function.

Quoted 10 lines

include/linux/fs.h

Quoted 7 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +7 -0
+		if (a_ops->batch_write)
+			status = a_ops->batch_write(file, &desc, &lru_pvec,
+						    &cached_page, &copied);
+		else
+			status = generic_batch_write(file, &desc, &lru_pvec,
+						     &cached_page, &copied);
+		if (likely(copied > 0)) {

This is missing this morning's handle-zero-length-segment bugfix.

Quoted 33 lines

I hope new patch deals properly with zero length segments. I writev-ed a vector which had 3 zero length segments: first, last and 3rd.

Quoted 30 lines
Quoted 29 lines
include/linux/fs.h renamed from include/linux/fs.h~batched-write +20 -2
+			written += copied;
+			desc.count -= copied;
+			if (desc.count) {
+				/*
+				 * not everything is written yet. Adjust write
+				 * descriptor for next iteration
+				 */
+				desc.pos += copied;
+				if (unlikely(nr_segs > 1))
+					filemap_set_next_iovec(&desc.cur_iov,
+							       &desc.iov_off,
+							       copied);
+				else
+					desc.iov_off += copied;
+				desc.buf = desc.cur_iov->iov_base +
+					desc.iov_off;
+			}
+		}
 		if (status < 0)
 			break;
 		balance_dirty_pages_ratelimited(mapping);
 		cond_resched();
-	} while (count);
-	*ppos = pos;
+	} while (desc.count);	
+	*ppos = pos + written;
 
 	if (cached_page)
 		page_cache_release(cached_page);

From: Vladimir Saveliev <[email protected]>

This patch adds a method batch_write to struct file_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

include/linux/fs.h renamed from include/linux/fs.h~batched-write +22 -0
diff -puN include/linux/fs.h~batched-write include/linux/fs.h
@@ -246,6 +246,7 @@ struct poll_table_struct;
 struct kstatfs;
 struct vm_area_struct;
 struct vfsmount;
+struct pagevec;
 
 extern void __init inode_init(unsigned long);
 extern void __init inode_init_early(void);
@@ -1096,6 +1097,25 @@ typedef int (*read_actor_t)(read_descrip
 #define HAVE_COMPAT_IOCTL 1
 #define HAVE_UNLOCKED_IOCTL 1
 
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @buf: pointer to data to be written
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ *
+ * This structure is to pass to batch_write file operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	char __user *buf;
+	struct page *cached_page;
+	struct pagevec *lru_pvec;
+};
+
 /*
  * NOTE:
  * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
@@ -1123,6 +1143,8 @@ struct file_operations {
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
 	ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
mm/filemap.c renamed from mm/filemap.c~batched-write +163 -104

302 lines not on this page · raw message

Documentation/filesystems/vfs.txt renamed from Documentation/filesystems/vfs.txt~batched-write +4 -0
diff -puN Documentation/filesystems/vfs.txt~batched-write Documentation/filesystems/vfs.txt
@@ -717,6 +717,8 @@ struct file_operations {
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
 	ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
@@ -784,6 +786,8 @@ otherwise noted.
 
   writev: called by the writev(2) system call
 
+  batch_write: optional, if implemented called by writev(2) and write(2)
+
   sendfile: called by the sendfile(2) system call
 
   get_unmapped_area: called by the mmap(2) system call

_

#7 lore raw

Documentation/filesystems/vfs.txt otherwise noted. · line 790

Quoted 6 lines · Vladimir V. Saveliev
Documentation/filesystems/vfs.txt renamed from Documentation/filesystems/vfs.txt~batched-write +2 -0
@@ -784,6 +786,8 @@ otherwise noted.
 
   writev: called by the writev(2) system call
 
+  batch_write: optional, if implemented called by writev(2) and write(2)
+

It'd be nice if you added some explanation here why a filesystem developer would want to implement it.

Pekka

#8 lore raw
Quoted 14 lines · Pekka Enberg

Documentation/filesystems/vfs.txt otherwise noted. · line 791

Quoted 7 lines · Vladimir V. Saveliev
Documentation/filesystems/vfs.txt renamed from Documentation/filesystems/vfs.txt~batched-write +2 -0
@@ -784,6 +786,8 @@ otherwise noted.
 
   writev: called by the writev(2) system call
 
+  batch_write: optional, if implemented called by writev(2) and
 write(2)
+

It'd be nice if you added some explanation here why a filesystem developer would want to implement it.

Quoted 3 lines

Pekka

Vladimir, it sounds like he found another place you might insert this comment:

+/*
+ * When calling the filesystem for writes, there is processing
+ * that must be done:
+ * 1) per word
+ * 2) per page
+ * 3) per call to the FS
+ * If the FS is called per page, then it turns out that 3) costs more
+ * than 1) and 2) for sophisticated filesystems. To allow the FS to
+ * choose to pay the cost of 3) only once we call batch_write, if the
+ * FS supports it.
+ */

Thanks Pekka!

#9 lore raw
In reply to Vladimir V. Saveliev
Quoted 6 lines · Vladimir V. Saveliev
Quoted 3 lines

Should this be an address_space_operation or a file_operation?

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

It definitly should not be a file_operation! It works at the address_space not the much higher file level. Maybe all three should become callbacks for the generic write routines, but that's left for the future.

#10 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Vladimir V. Saveliev
Quoted 4 lines

Should this be an address_space_operation or a file_operation?

Quoted 12 lines
Quoted 4 lines

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

It definitly should not be a file_operation! It works at the address_space not the much higher file level. Maybe all three should become callbacks for the generic write routines, but that's left for the future.

I don't have a commitment to one way or the other, probably because there are some things that are unclear in my mind. Could you help me with them? Can you define what is the address space vs. the file level please? It is odd to be asking such a basic question, but these things are genuinely unclear to me. If the use of something varies according to the file, is it a file method? What things vary according to address space and not according to file? Should things that vary according to address space be address space ops and things that vary according to file be file ops? If that logic seems valid, should a lot more be changed?

Oh, and Andrew, while such things are discussed, could you just pick one way or the other and let the patch go in?

Hans

#11 lore raw
Quoted 31 lines · On Tue, 04 Jul 2006 10:44:13 -0700 Hans Reiser <[email protected]> wrote:
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Vladimir V. Saveliev
Quoted 4 lines

Should this be an address_space_operation or a file_operation?

Quoted 12 lines
Quoted 4 lines

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

It definitly should not be a file_operation! It works at the address_space not the much higher file level. Maybe all three should become callbacks for the generic write routines, but that's left for the future.

I don't have a commitment to one way or the other, probably because there are some things that are unclear in my mind. Could you help me with them? Can you define what is the address space vs. the file level please? It is odd to be asking such a basic question, but these things are genuinely unclear to me. If the use of something varies according to the file, is it a file method? What things vary according to address space and not according to file? Should things that vary according to address space be address space ops and things that vary according to file be file ops? If that logic seems valid, should a lot more be changed?

Quoted 3 lines

Oh, and Andrew, while such things are discussed, could you just pick one way or the other and let the patch go in?

I wasn't sure, which was I asked rather than suggested..

Looking closer, yes I agree with Christoph, sorry. It's called at the same level as ->prepare_write/commit_write so if there's any logic to this, it's logical that batched_write be an a_op too.

#12 lore raw
Quoted 6 lines · Andrew Morton

It's called at the same
level as ->prepare_write/commit_write so if there's any logic to this, it's logical that batched_write be an a_op too.

I agree that prepare_write/commit_write should be the same as batch_write, but maybe they all should be file ops?

vs will send a patch which makes batch_write an a_op tomorrow.....

Hans

#13 lore raw
In reply to Andrew Morton

Andrew, I just wanted to thank you for all the detailed comments you made. As I have said in the past, any time your schedule might allow you for reading and criticizing reiser4 is a gift we will be grateful for. I have the greatest respect for the code you write.

Hans

#14 lore raw
In reply to Andrew Morton

Hello

Quoted 38 lines · Andrew Morton
Quoted 31 lines · On Tue, 04 Jul 2006 10:44:13 -0700 Hans Reiser <[email protected]> wrote:
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Vladimir V. Saveliev
Quoted 4 lines

Should this be an address_space_operation or a file_operation?

Quoted 12 lines
Quoted 4 lines

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

It definitly should not be a file_operation! It works at the address_space not the much higher file level. Maybe all three should become callbacks for the generic write routines, but that's left for the future.

I don't have a commitment to one way or the other, probably because there are some things that are unclear in my mind. Could you help me with them? Can you define what is the address space vs. the file level please? It is odd to be asking such a basic question, but these things are genuinely unclear to me. If the use of something varies according to the file, is it a file method? What things vary according to address space and not according to file? Should things that vary according to address space be address space ops and things that vary according to file be file ops? If that logic seems valid, should a lot more be changed?

Quoted 3 lines

Oh, and Andrew, while such things are discussed, could you just pick one way or the other and let the patch go in?

Quoted 6 lines

I wasn't sure, which was I asked rather than suggested..

Looking closer, yes I agree with Christoph, sorry. It's called at the same level as ->prepare_write/commit_write so if there's any logic to this, it's logical that batched_write be an a_op too.

ok, the attached is the final version of the patch. Please, take a look and make comments.

Quoted 1 line

From: Vladimir Saveliev <[email protected]>

This patch adds a method batch_write to struct address_space_operations.
A filesystem may want to implement this operation to improve write performance. Generic implementation for the method is made by cut-n-paste off generic_file_buffered_write:
it writes one page using prepare_write and commit_write address space operations.

include/linux/fs.h renamed from include/linux/fs.h~batched-write +22 -0
diff -puN include/linux/fs.h~batched-write include/linux/fs.h
@@ -246,6 +246,7 @@ struct poll_table_struct;
 struct kstatfs;
 struct vm_area_struct;
 struct vfsmount;
+struct pagevec;
 
 extern void __init inode_init(unsigned long);
 extern void __init inode_init_early(void);
@@ -347,6 +348,25 @@ struct page;
 struct address_space;
 struct writeback_control;
 
+/**
+ * struct write_descriptor - set of write arguments
+ * @pos: offset from the start of the file to write to
+ * @count: number of bytes to write
+ * @buf: pointer to data to be written
+ * @lru_pvec: multipage container to batch adding pages to LRU list
+ * @cached_page: allocated but not used on previous call
+ *
+ * This structure is to pass to batch_write file operation all
+ * information which is needed to continue write.
+ */
+struct write_descriptor {
+	loff_t pos;
+	size_t count;
+	char __user *buf;
+	struct page *cached_page;
+	struct pagevec *lru_pvec;
+};
+
 struct address_space_operations {
 	int (*writepage)(struct page *page, struct writeback_control *wbc);
 	int (*readpage)(struct file *, struct page *);
@@ -367,6 +387,8 @@ struct address_space_operations {
 	 */
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	/* Unfortunately this kludge is needed for FIBMAP. Don't use it */
 	sector_t (*bmap)(struct address_space *, sector_t);
 	void (*invalidatepage) (struct page *, unsigned long);
mm/filemap.c renamed from mm/filemap.c~batched-write +147 -104

283 lines not on this page · raw message

Documentation/filesystems/vfs.txt renamed from Documentation/filesystems/vfs.txt~batched-write +13 -0
diff -puN Documentation/filesystems/vfs.txt~batched-write Documentation/filesystems/vfs.txt
@@ -534,6 +534,8 @@ struct address_space_operations {
 			struct list_head *pages, unsigned nr_pages);
 	int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
 	int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
+	long (*batch_write)(struct file *file, struct write_descriptor *desc,
+			    size_t *written);
 	sector_t (*bmap)(struct address_space *, sector_t);
 	int (*invalidatepage) (struct page *, unsigned long);
 	int (*releasepage) (struct page *, int);
@@ -624,6 +626,17 @@ struct address_space_operations {
         operations.  It should avoid returning an error if possible -
         errors should have been handled by prepare_write.
 
+  batch_write: optional
+        When calling the filesystem for writes, there is processing
+        that must be done:
+        1) per word
+        2) per page
+        3) per call to the FS
+        If the FS is called per page, then it turns out that 3) costs more
+        than 1) and 2) for sophisticated filesystems.  To allow the FS to
+        choose to pay the cost of 3) only once we call batch_write, if the
+        FS supports it.
+
   bmap: called by the VFS to map a logical block offset within object to
   	physical block number. This method is used by the FIBMAP
   	ioctl and for working with swap-files.  To be able to swap to

_

#15 lore raw
Quoted 3 lines · Vladimir V. Saveliev

This patch adds a method batch_write to struct address_space_operations. A filesystem may want to implement this operation to improve write performance.

I failed to make a record of which other filesystems will want to use this. Do you recall?

#16 lore raw

Reiser4, FUSE, maybe XFS, and GFS2. Andreas, your support had conditions, so I will not characterize for you whether you said Lustre or you would like it. Andreas, could you look at the patch and see what you think?

I think with some confidence that this interface will acquire more users than those over time. It really is the right natural interface for most filesystems that try to carefully optimize write performance.

Quoted 8 lines · Andrew Morton

I failed to make a record of which other filesystems will want to use this. Do you recall?

#17 lore raw
Quoted 4 lines · Hans Reiser

Reiser4, FUSE, maybe XFS, and GFS2. Andreas, your support had conditions, so I will not characterize for you whether you said Lustre or you would like it. Andreas, could you look at the patch and see what you think?

Lustre definitely wants to be able to aggregate IOs into larger chunks wherever possible. Having interfaces that allow this instead of going through page-at-a-time interfaces is definitely a win. As it stands now we have to avoid the VFS or otherwise have back-channel information in order to ensure that large IOs remain large.

Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.

#18 lore raw
In reply to Vladimir V. Saveliev
Quoted 2 lines · Vladimir V. Saveliev

ok, the attached is the final version of the patch. Please, take a look and make comments.

Perfect, this is exactly how I hoped it will be done in the end. Very nice massaging of the generic code without runtime impact on older filesystems or code duplication. Thanks a lot Vladimir!

Now before we can put this into mainline we'd need some in-tree filesystems to make use of it. I've cc'ed the usual suspects for this..

p.s. you mail setup seems a little odd. any chance you could just inline the patch normally instead of using mime?

#19 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Vladimir V. Saveliev
Quoted 1 line

Should this be an address_space_operation or a file_operation?

I was seeking to be minimal in my changes to the philosophy of the code. So, it was an address_space operation. Now it is a file operation.

It definitly should not be a file_operation!

It works at the
address_space

generic_batch_write works with the page cache, another batch_write implementation may not.

Except the cached_page and pagevec which are generic_batch_write context, the batch_write switch leaves file stuff _before_ the switch and using of the page cache _after_ the switch. It gives much flexibility to a file system to choose between simple page cache buffered write, batch version of page cache write or even not a page-oriented write method like reiser4 write method for packed (tails only) files. address space op which does not use the page cache looks better as a file op.

Quoted 5 lines

not the much higher file level.

Maybe all three should
become callbacks for the generic write routines, but that's left for the future.

Thanks,
Alex.