Skip to contentSkip to conversation

Block reservation on page fault time for ext3

PATCH In review Jan Kara 3 participants 12 messages May 2, 2011 – May 11, 2011 VFS

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

cover Block reservation on page fault time for ext3

Hi,

ext3 has a problem that mmap writes end up allocating blocks only in
writepage() callback. This then effectively invalidates any quota checking because writepage() is called from flusher thread thus with root priviledges. So any user is able to arbitrarily exceed quota limit using mmap write.

The following four patches try to address this problem. The patches implement page_mkwrite() callback which allocates all necessary metadata and reserves space for data block (this is the main difference from the patches I was sending last autumn which did not allocate metadata). Then during writepage() (or write()) time the reservation gets converted into real block allocation.

With this implementation I don't see any performance difference in heavy BerkleyDB load from the ext3 without these patches. Simple allocation in page_mkwrite() ends up being about 3x slower than this reservation scheme because of fragmentation.

I've tested the patch on both x86_64 (1K and 4K blocksize) and ppc with 64k pages (1K and 4K blocksize) to catch possible bugs. I've also run tests in ENOSPC conditions and conditions when quota is getting exceeded. All these tests run fine with this version of patches (actually, I've triggered two genuine ext3 bugs during this testing which I'm going to merge separately).

So I'd like to merge these patches but before I do that I'd like another pair of eyes to have a look at these changes... So comments are welcome.

Maybe one more addition: As we spoke at LSF, we plan to remove ext3 driver from kernel. But it's still going to take significant amount of time (more than an year) so I'd like to have this serious issue fixed in ext3.

Honza

author reviewer
#1 lore raw

PATCH 1/4 vfs: Unmap underlying metadata of new data buffers only when buffer is mapped

When we do delayed allocation of some buffer, we want to signal to VFS that the buffer is new (set buffer_new) so that it properly zeros out everything. But we don't have the buffer mapped yet so we cannot really unmap underlying metadata in this state. Make VFS avoid doing unmapping of metadata when the buffer is not yet mapped.

---
 fs/buffer.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
fs/buffer.c +7 -5
index a08bb8e..9e6d67a 100644
@@ -1673,8 +1673,9 @@ static int __block_write_full_page(struct inode *inode, struct page *page,
 			if (buffer_new(bh)) {
 				/* blockdev mappings never come here */
 				clear_buffer_new(bh);
-				unmap_underlying_metadata(bh->b_bdev,
-							bh->b_blocknr);
+				if (buffer_mapped(bh))
+					unmap_underlying_metadata(bh->b_bdev,
+						bh->b_blocknr);
 			}
 		}
 		bh = bh->b_this_page;
@@ -1862,8 +1863,9 @@ int __block_write_begin(struct page *page, loff_t pos, unsigned len,
 			if (err)
 				break;
 			if (buffer_new(bh)) {
-				unmap_underlying_metadata(bh->b_bdev,
-							bh->b_blocknr);
+				if (buffer_mapped(bh))
+					unmap_underlying_metadata(bh->b_bdev,
+						bh->b_blocknr);
 				if (PageUptodate(page)) {
 					clear_buffer_new(bh);
 					set_buffer_uptodate(bh);
@@ -2491,7 +2493,7 @@ int nobh_write_begin(struct address_space *mapping,
 			goto failed;
 		if (!buffer_mapped(bh))
 			is_mapped_to_disk = 0;
-		if (buffer_new(bh))
+		if (buffer_new(bh) && buffer_mapped(bh))
 			unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
 		if (PageUptodate(page)) {
 			set_buffer_uptodate(bh);
Signature · 1 line
1.7.1
author reviewer
#2 lore raw

PATCH 3/4 ext3: Implement per-cpu counters for delayed allocation

In reply to Jan Kara

Implement free blocks and reserved blocks counters for delayed allocation. These counters are reliable in the sence that when they return success, the subsequent conversion from reserved to allocated blocks always succeeds (see comments in the code for details). This is useful for ext3 filesystem to implement delayed allocation in particular for allocation in page_mkwrite.

---
 fs/ext3/delalloc_counter.c |  109 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ext3/delalloc_counter.h |   73 +++++++++++++++++++++++++++++
 2 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 fs/ext3/delalloc_counter.c
 create mode 100644 fs/ext3/delalloc_counter.h
fs/ext3/delalloc_counter.c new file +109 -0
new file mode 100644
index 0000000..b584961
@@ -0,0 +1,109 @@
+/*
+ *  Per-cpu counters for delayed allocation
+ */
+#include <linux/percpu_counter.h>
+#include <linux/module.h>
+#include <linux/log2.h>
+#include "delalloc_counter.h"
+
+static long dac_error(struct delalloc_counter *c)
+{
+#ifdef CONFIG_SMP
+	return c->batch * nr_cpu_ids;
+#else
+	return 0;
+#endif
+}
+
+/*
+ * Reserve blocks for delayed allocation
+ *
+ * This code is subtle because we want to avoid synchronization of processes
+ * doing allocation in the common case when there's plenty of space in the
+ * filesystem.
+ *
+ * The code maintains the following property: Among all the calls to
+ * dac_reserve() that return 0 there exists a simple sequential ordering of
+ * these calls such that the check (free - reserved >= limit) in each call
+ * succeeds. This guarantees that we never reserve blocks we don't have.
+ *
+ * The proof of the above invariant: The function can return 0 either when the
+ * first if succeeds or when both ifs fail. To the first type of callers we
+ * assign the time of read of c->reserved in the first if, to the second type
+ * of callers we assign the time of read of c->reserved in the second if. We
+ * order callers by their assigned time and claim that this is the ordering
+ * required by the invariant. Suppose that a check (free - reserved >= limit)
+ * fails for caller C in the proposed ordering. We distinguish two cases:
+ * 1) function called by C returned zero because the first if succeeded - in
+ *  this case reads of counters in the first if must have seen effects of
+ *  __percpu_counter_add of all the callers before C (even their condition
+ *  evaluation happened before our). The errors accumulated in cpu-local
+ *  variables are clearly < dac_error(c) and thus the condition should fail.
+ *  Contradiction.
+ * 2) function called by C returned zero because the second if failed - again
+ *  the read of the counters must have seen effects of __percpu_counter_add of
+ *  all the callers before C and thus the condition should have succeeded.
+ *  Contradiction.
+ */
+int dac_reserve(struct delalloc_counter *c, s32 amount, s64 limit)
+{
+	s64 free, reserved;
+	int ret = 0;
+
+	__percpu_counter_add(&c->reserved, amount, c->batch);
+	/*
+	 * This barrier makes sure that when effects of the following read of
+	 * c->reserved are observable by another CPU also effects of the
+	 * previous store to c->reserved are seen.
+	 */
+	smp_mb();
+	if (percpu_counter_read(&c->free) - percpu_counter_read(&c->reserved)
+	    - 2 * dac_error(c) >= limit)
+		return ret;
+	/*
+	 * Near the limit - sum the counter to avoid returning ENOSPC too
+	 * early. Note that we can still "unnecessarily" return ENOSPC when
+	 * there are several racing writers. Spinlock in this section would
+	 * solve it but let's ignore it for now.
+	 */
+	free = percpu_counter_sum_positive(&c->free);
+	reserved = percpu_counter_sum_positive(&c->reserved);
+	if (free - reserved < limit) {
+		__percpu_counter_add(&c->reserved, -amount, c->batch);
+		ret = -ENOSPC;
+	}
+	return ret;
+}
+EXPORT_SYMBOL(dac_reserve);
+
+/* Account reserved blocks as allocated */
+void dac_alloc_reserved(struct delalloc_counter *c, s32 amount)
+{
+	__percpu_counter_add(&c->free, -amount, c->batch);
+	/*
+	 * Make sure update of free counter is seen before update of
+	 * reserved counter.
+	 */
+	smp_wmb();
+	__percpu_counter_add(&c->reserved, -amount, c->batch);
+}
+EXPORT_SYMBOL(dac_alloc_reserved);
+
+int dac_init(struct delalloc_counter *c, s64 amount)
+{
+	int err;
+
+	c->batch = 8*(1+ilog2(nr_cpu_ids));
+	err = percpu_counter_init(&c->free, amount);
+	if (!err)
+		err = percpu_counter_init(&c->reserved, 0);
+	return err;
+}
+EXPORT_SYMBOL(dac_init);
+
+void dac_destroy(struct delalloc_counter *c)
+{
+	percpu_counter_destroy(&c->free);
+	percpu_counter_destroy(&c->reserved);
+}
+EXPORT_SYMBOL(dac_destroy);
fs/ext3/delalloc_counter.h new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
1.7.1
author reviewer
#4 lore raw

PATCH 4/4 ext3: Implement delayed allocation on page_mkwrite time

In reply to Jan Kara

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

---
 fs/ext3/Makefile     |    3 +-
 fs/ext3/balloc.c     |  119 +++++++++++------
 fs/ext3/ext3.h       |   11 ++-
 fs/ext3/ext3_fs_i.h  |    4 +
 fs/ext3/ext3_fs_sb.h |    3 +-
 fs/ext3/file.c       |   19 +++-
 fs/ext3/ialloc.c     |    2 +-
 fs/ext3/inode.c      |  357 +++++++++++++++++++++++++++++++++++++++++++-------
 fs/ext3/resize.c     |    2 +-
 fs/ext3/super.c      |   22 +++-
 10 files changed, 442 insertions(+), 100 deletions(-)
fs/ext3/Makefile +2 -1
index e77766a..ed5cead 100644
@@ -5,7 +5,8 @@
 obj-$(CONFIG_EXT3_FS) += ext3.o
 
 ext3-y	:= balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
-	   ioctl.o namei.o super.o symlink.o hash.o resize.o ext3_jbd.o
+	   ioctl.o namei.o super.o symlink.o hash.o resize.o ext3_jbd.o \
+	   delalloc_counter.o
 
 ext3-$(CONFIG_EXT3_FS_XATTR)	 += xattr.o xattr_user.o xattr_trusted.o
 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
fs/ext3/balloc.c +79 -40

265 lines not on this page · raw message

fs/ext3/ext3.h +9 -2
index 35c10e7..fe008eb 100644
@@ -846,12 +846,14 @@ ext3_group_first_block_no(struct super_block *sb, unsigned long group_no)
 # define NORET_AND     noreturn,
 
 /* balloc.c */
+extern ext3_fsblk_t ext3_free_blocks_limit(struct super_block *sb);
 extern int ext3_bg_has_super(struct super_block *sb, int group);
 extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
 extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
 			ext3_fsblk_t goal, int *errp);
 extern ext3_fsblk_t ext3_new_blocks (handle_t *handle, struct inode *inode,
-			ext3_fsblk_t goal, unsigned long *count, int *errp);
+			ext3_fsblk_t goal, unsigned long *count,
+			bool reserved, int *errp);
 extern void ext3_free_blocks (handle_t *handle, struct inode *inode,
 			ext3_fsblk_t block, unsigned long count);
 extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
@@ -899,9 +901,13 @@ int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
 		struct buffer_head *bh, ext3_fsblk_t blocknr);
 struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
 struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
+#define EXT3_GET_BLOCKS_CREATE		0x01
+#define EXT3_GET_BLOCKS_CREATE_DA	0x02
+#define EXT3_GET_BLOCKS_CREATE_RESERVED	0x04	/* Internal flag of
+						ext3_get_blocks_handle() */
 int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
 	sector_t iblock, unsigned long maxblocks, struct buffer_head *bh_result,
-	int create);
+	int flags);
 
 extern struct inode *ext3_iget(struct super_block *, unsigned long);
 extern int  ext3_write_inode (struct inode *, struct writeback_control *);
@@ -919,6 +925,7 @@ extern void ext3_get_inode_flags(struct ext3_inode_info *);
 extern void ext3_set_aops(struct inode *inode);
 extern int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 		       u64 start, u64 len);
+extern int ext3_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf);
 
 /* ioctl.c */
 extern long ext3_ioctl(struct file *, unsigned int, unsigned long);
fs/ext3/ext3_fs_i.h +4 -0
index b0a3d8a..e87de74 100644
@@ -64,6 +64,7 @@ struct ext3_block_alloc_info {
 #define rsv_start rsv_window._rsv_start
 #define rsv_end rsv_window._rsv_end
 
+
 /*
  * third extended file system inode data in memory
  */
@@ -125,6 +126,9 @@ struct ext3_inode_info {
 
 	/* on-disk additional length */
 	__u16 i_extra_isize;
+#ifdef CONFIG_QUOTA
+	qsize_t i_reserved_quota;
+#endif
 
 	/*
 	 * truncate_mutex is for serialising ext3_truncate() against
fs/ext3/ext3_fs_sb.h +2 -1
index efa1c2f..bc9dc4c 100644
@@ -21,6 +21,7 @@
 #include <linux/wait.h>
 #include <linux/blockgroup_lock.h>
 #include <linux/percpu_counter.h>
+#include "delalloc_counter.h"
 #endif
 #include <linux/rbtree.h>
 
@@ -58,7 +59,7 @@ struct ext3_sb_info {
 	u32 s_hash_seed[4];
 	int s_def_hash_version;
 	int s_hash_unsigned;	/* 3 if hash should be signed, 0 if not */
-	struct percpu_counter s_freeblocks_counter;
+	struct delalloc_counter s_alloc_counter;
 	struct percpu_counter s_freeinodes_counter;
 	struct percpu_counter s_dirs_counter;
 	struct blockgroup_lock *s_blockgroup_lock;
fs/ext3/file.c +18 -1
index 22ad54b..843e4d0 100644
@@ -52,6 +52,23 @@ static int ext3_release_file (struct inode * inode, struct file * filp)
 	return 0;
 }
 
+static const struct vm_operations_struct ext3_file_vm_ops = {
+	.fault		= filemap_fault,
+	.page_mkwrite	= ext3_page_mkwrite,
+};
+
+static int ext3_file_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct address_space *mapping = file->f_mapping;
+
+	if (!mapping->a_ops->readpage)
+		return -ENOEXEC;
+	file_accessed(file);
+	vma->vm_ops = &ext3_file_vm_ops;
+	vma->vm_flags |= VM_CAN_NONLINEAR;
+	return 0;
+}
+
 const struct file_operations ext3_file_operations = {
 	.llseek		= generic_file_llseek,
 	.read		= do_sync_read,
@@ -62,7 +79,7 @@ const struct file_operations ext3_file_operations = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl	= ext3_compat_ioctl,
 #endif
-	.mmap		= generic_file_mmap,
+	.mmap		= ext3_file_mmap,
 	.open		= dquot_file_open,
 	.release	= ext3_release_file,
 	.fsync		= ext3_sync_file,
fs/ext3/ialloc.c +1 -1
index 3f56851..b7b982c 100644
@@ -257,7 +257,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent)
 
 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
 	avefreei = freei / ngroups;
-	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
+	freeb = dac_get_avail(&sbi->s_alloc_counter);
 	avefreeb = freeb / ngroups;
 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
 
fs/ext3/inode.c +309 -48

601 lines not on this page · raw message

fs/ext3/resize.c +1 -1
index eb10e6f..302d896 100644
@@ -958,7 +958,7 @@ int ext3_group_add(struct super_block *sb, struct ext3_new_group_data *input)
 	le32_add_cpu(&es->s_r_blocks_count, input->reserved_blocks);
 
 	/* Update the free space counts */
-	percpu_counter_add(&sbi->s_freeblocks_counter,
+	percpu_counter_add(&sbi->s_alloc_counter.free,
 			   input->free_blocks_count);
 	percpu_counter_add(&sbi->s_freeinodes_counter,
 			   EXT3_INODES_PER_GROUP(sb));
fs/ext3/super.c +17 -5
index 5be257b..df08c68 100644
@@ -444,7 +444,7 @@ static void ext3_put_super (struct super_block * sb)
 	for (i = 0; i < sbi->s_gdb_count; i++)
 		brelse(sbi->s_group_desc[i]);
 	kfree(sbi->s_group_desc);
-	percpu_counter_destroy(&sbi->s_freeblocks_counter);
+	dac_destroy(&sbi->s_alloc_counter);
 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
 	percpu_counter_destroy(&sbi->s_dirs_counter);
 	brelse(sbi->s_sbh);
@@ -493,6 +493,10 @@ static struct inode *ext3_alloc_inode(struct super_block *sb)
 	ei->vfs_inode.i_version = 1;
 	atomic_set(&ei->i_datasync_tid, 0);
 	atomic_set(&ei->i_sync_tid, 0);
+#ifdef CONFIG_QUOTA
+	ei->i_reserved_quota = 0;
+#endif
+
 	return &ei->vfs_inode;
 }
 
@@ -760,8 +764,17 @@ static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
 			       size_t len, loff_t off);
 static ssize_t ext3_quota_write(struct super_block *sb, int type,
 				const char *data, size_t len, loff_t off);
+#ifdef CONFIG_QUOTA
+qsize_t *ext3_get_reserved_space(struct inode *inode)
+{
+	return &EXT3_I(inode)->i_reserved_quota;
+}
+#endif
 
 static const struct dquot_operations ext3_quota_operations = {
+#ifdef CONFIG_QUOTA
+	.get_reserved_space = ext3_get_reserved_space,
+#endif
 	.write_dquot	= ext3_write_dquot,
 	.acquire_dquot	= ext3_acquire_dquot,
 	.release_dquot	= ext3_release_dquot,
@@ -1972,8 +1985,7 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
 				"mounting ext3 over ext2?");
 		goto failed_mount2;
 	}
-	err = percpu_counter_init(&sbi->s_freeblocks_counter,
-			ext3_count_free_blocks(sb));
+	err = dac_init(&sbi->s_alloc_counter, ext3_count_free_blocks(sb));
 	if (!err) {
 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
 				ext3_count_free_inodes(sb));
@@ -2062,7 +2074,7 @@ cantfind_ext3:
 	goto failed_mount;
 
 failed_mount3:
-	percpu_counter_destroy(&sbi->s_freeblocks_counter);
+	dac_destroy(&sbi->s_alloc_counter);
 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
 	percpu_counter_destroy(&sbi->s_dirs_counter);
 	journal_destroy(sbi->s_journal);
@@ -2759,7 +2771,7 @@ static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
 	buf->f_type = EXT3_SUPER_MAGIC;
 	buf->f_bsize = sb->s_blocksize;
 	buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
-	buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
+	buf->f_bfree = dac_get_avail_sum(&sbi->s_alloc_counter);
 	buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
 	if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
 		buf->f_bavail = 0;
Signature · 1 line
1.7.1
#3 lore raw

Re: [PATCH 3/4] ext3: Implement per-cpu counters for delayed allocation

In reply to Jan Kara
Quoted 26 lines · On Mon, 2 May 2011 22:56:55 +0200 Jan Kara <[email protected]> wrote:

Implement free blocks and reserved blocks counters for delayed allocation. These counters are reliable in the sence that when they return success, the subsequent conversion from reserved to allocated blocks always succeeds (see comments in the code for details). This is useful for ext3 filesystem to implement delayed allocation in particular for allocation in page_mkwrite.

---
 fs/ext3/delalloc_counter.c |  109 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ext3/delalloc_counter.h |   73 +++++++++++++++++++++++++++++
 2 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 fs/ext3/delalloc_counter.c
 create mode 100644 fs/ext3/delalloc_counter.h
fs/ext3/delalloc_counter.c new file +6 -0
new file mode 100644
index 0000000..b584961
@@ -0,0 +1,109 @@
+/*
+ *  Per-cpu counters for delayed allocation
+ */
+#include <linux/percpu_counter.h>
+#include <linux/module.h>
+#include <linux/log2.h>

fs/ext3/delalloc_counter.c line 16

Quoted 10 lines
fs/ext3/delalloc_counter.c new file +10 -0
+#include "delalloc_counter.h"
+
+static long dac_error(struct delalloc_counter *c)
+{
+#ifdef CONFIG_SMP
+	return c->batch * nr_cpu_ids;
+#else
+	return 0;
+#endif
+}

This function needs a comment please.

The use of nr_cpu_ids was a surprise. Why not num_online_cpus() or num_possible_cpus()? Please change the code so that readers can understand the reasoning here.

Quoted 20 lines
fs/ext3/delalloc_counter.c new file +20 -0
+/*
+ * Reserve blocks for delayed allocation
+ *
+ * This code is subtle because we want to avoid synchronization of processes
+ * doing allocation in the common case when there's plenty of space in the
+ * filesystem.
+ *
+ * The code maintains the following property: Among all the calls to
+ * dac_reserve() that return 0 there exists a simple sequential ordering of
+ * these calls such that the check (free - reserved >= limit) in each call
+ * succeeds. This guarantees that we never reserve blocks we don't have.
+ *
+ * The proof of the above invariant: The function can return 0 either when the
+ * first if succeeds or when both ifs fail. To the first type of callers we
+ * assign the time of read of c->reserved in the first if, to the second type
+ * of callers we assign the time of read of c->reserved in the second if. We
+ * order callers by their assigned time and claim that this is the ordering
+ * required by the invariant. Suppose that a check (free - reserved >= limit)
+ * fails for caller C in the proposed ordering. We distinguish two cases:
+ * 1) function called by C returned zero because the first if succeeded - in

fs/ext3/delalloc_counter.c line 47

Quoted 10 lines
fs/ext3/delalloc_counter.c new file +10 -0
+ *  this case reads of counters in the first if must have seen effects of
+ *  __percpu_counter_add of all the callers before C (even their condition
+ *  evaluation happened before our). The errors accumulated in cpu-local
+ *  variables are clearly < dac_error(c) and thus the condition should fail.
+ *  Contradiction.
+ * 2) function called by C returned zero because the second if failed - again
+ *  the read of the counters must have seen effects of __percpu_counter_add of
+ *  all the callers before C and thus the condition should have succeeded.
+ *  Contradiction.
+ */

Geeze. I'll believe you :)

fs/ext3/delalloc_counter.c line 109

Quoted 4 lines
fs/ext3/delalloc_counter.c new file +4 -0
+EXPORT_SYMBOL(dac_reserve);
+EXPORT_SYMBOL(dac_alloc_reserved);
+EXPORT_SYMBOL(dac_init);
+EXPORT_SYMBOL(dac_destroy);

I'm not sure that these are needed?

#5 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

In reply to Jan Kara
Quoted 7 lines · On Mon, 2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Yes, instantiating the metadata and accounting the data is a good approach. The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch! Are we really sure that we want to make changes this extensive to our antiquated old fs? Or do we just say "yeah, it's broken with quotas - use ext4"?

author reviewer
#6 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Mon, 2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Quoted 7 lines

Yes, instantiating the metadata and accounting the data is a good approach. The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch! Are we really sure that we want to make changes this extensive to our antiquated old fs? Or do we just say "yeah, it's broken with quotas - use ext4"?

The patch isn't trivial, I agree (although it's mostly straightforward).
Regarding telling users to switch to ext4 - it seems a bit harsh to me to ask people to switch to ext4 as a response to a (possibly security) issue they uncover. Because for most admins switching to ext4 will require some non-trivial testing I presume. Of course, the counterweight is the possibility of new bugs introduced to the code by my patch. But after some considerations I've decided it's worth it and and fixed the bug...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

Quoted 25 lines · On Tue, 3 May 2011 00:20:20 +0200 Jan Kara <[email protected]> wrote:
Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Mon, 2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Quoted 7 lines

Yes, instantiating the metadata and accounting the data is a good approach. The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch! Are we really sure that we want to make changes this extensive to our antiquated old fs? Or do we just say "yeah, it's broken with quotas - use ext4"?

Quoted 6 lines

The patch isn't trivial, I agree (although it's mostly straightforward).
Regarding telling users to switch to ext4 - it seems a bit harsh to me to ask people to switch to ext4 as a response to a (possibly security) issue they uncover. Because for most admins switching to ext4 will require some non-trivial testing I presume. Of course, the counterweight is the possibility of new bugs introduced to the code by my patch.

Yes.

Quoted 2 lines

But after some
considerations I've decided it's worth it and and fixed the bug...

Well. How did you come to that decision? Are real users hurting from this problem? What's the real-world case for fixing it?

#11 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

In reply to Jan Kara
Quoted 25 lines · Jan Kara
Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Mon,  2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Quoted 7 lines

Yes, instantiating the metadata and accounting the data is a good approach.  The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch!  Are we really sure that we want to make changes this extensive to our antiquated old fs?  Or do we just say "yeah, it's broken with quotas - use ext4"?

Quoted 7 lines

 The patch isn't trivial, I agree (although it's mostly straightforward). Regarding telling users to switch to ext4 - it seems a bit harsh to me to ask people to switch to ext4 as a response to a (possibly security) issue they uncover. Because for most admins switching to ext4 will require some non-trivial testing I presume. Of course, the counterweight is the possibility of new bugs introduced to the code by my patch. But after some considerations I've decided it's worth it and and fixed the bug...

Jan,

Maybe you can work out a simpler(=safer) patch, which uses much larger reservation margins.

For example, by reserving on mmap call, the difference between i_size and i_blocks
and storing that reservation in in-memory inode, protected by i_mutex. If an in-memory inode RESERVATION flag is set, you can update i_reserved_blocks when updating i_blocks (alloc and free) and i_size (truncate). global in-memory reservation counters can be updated at the same time.

This approach will avoid the need to introduce delayed allocation to ext3, since you got the reservation on mmap time, you can rest assure that no dirty data will be dropped on the floor and no quota limits will be exceeded.

The trade off is that an application that want to mmap a very large sparse file to write just some blocks will not be able to do that on a near full file system.
Do you know of any such real application?

Amir.
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author reviewer
#8 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

In reply to Andrew Morton
Quoted 34 lines · Andrew Morton
Quoted 25 lines · On Tue, 3 May 2011 00:20:20 +0200 Jan Kara <[email protected]> wrote:
Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Mon, 2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Quoted 7 lines

Yes, instantiating the metadata and accounting the data is a good approach. The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch! Are we really sure that we want to make changes this extensive to our antiquated old fs? Or do we just say "yeah, it's broken with quotas - use ext4"?

Quoted 6 lines

The patch isn't trivial, I agree (although it's mostly straightforward).
Regarding telling users to switch to ext4 - it seems a bit harsh to me to ask people to switch to ext4 as a response to a (possibly security) issue they uncover. Because for most admins switching to ext4 will require some non-trivial testing I presume. Of course, the counterweight is the possibility of new bugs introduced to the code by my patch.

Quoted 6 lines

Yes.

Quoted 2 lines

But after some
considerations I've decided it's worth it and and fixed the bug...

Well. How did you come to that decision?

So my thoughts were: If a company runs a hosting or similar service and
some load either inadvertedly or even maliciously triggers this bug, your systems can be DOSed. That's bad and you need to fix that ASAP. From my experience with our SLE customers, they are willing to listen to advices such as fs choice when they plan a system deployment. After that they vehemently refuse any major change (and fs driver change is a major one). So I'm quite certain they'd rather accept this largish ext3 change. Finally, admittedly, I didn't think the patch will end up so large.

Looking into the patch, I could split off some cleanups and code reorganizations which are 20-30% of the patch but it probably does not make sense to split it more. What I think is a plus of the patch is that there are only two code paths that really change - ext3_get_blocks() has two new cases how it is called (to allocate only indirect blocks and to allocate already reserved data block) and trucate path which has to do more work to check whether indirect block can be removed.

Quoted 1 line

Are real users hurting from this problem?

I've got a report of this from NEC
(http://ns3.spinics.net/lists/linux-ext4/msg20239.html) and OpenVZ people were also concerned
(http://ns3.spinics.net/lists/linux-ext4/msg20288.html). I think there was one more report of this problem but I can't find it now. So yes, there are users who care.

Quoted 1 line

What's the real-world case for fixing it?

Sorry, I don't understand the question (or how it is different from the
previous one).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#9 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

Quoted 66 lines · Jan Kara
Quoted 34 lines · Andrew Morton
Quoted 25 lines · On Tue, 3 May 2011 00:20:20 +0200 Jan Kara <[email protected]> wrote:
Quoted 17 lines · Andrew Morton
Quoted 7 lines · On Mon, 2 May 2011 22:56:56 +0200 Jan Kara <[email protected]> wrote:

So far, ext3 was allocating necessary blocks for mmapped writes when writepage() was called. There are several issues with this. The worst being that user is allowed to arbitrarily exceed disk quotas because writepage() is called from flusher thread context (which is root) and thus quota limits are ignored. Another bad consequence is that data is just lost if we find there's no space on the filesystem during ->writepage() time.

Quoted 6 lines

We solve these issues by implementing block reservation in page_mkwrite() callback. We don't want to really allocate blocks on page_mkwrite() time because for random writes via mmap (as seen for example with applications using BerkeleyDB) it results in much more fragmented files and thus much worse performance. So we allocate indirect blocks and reserve space for data block in page_mkwrite() and do the allocation of data block from writepage().

Quoted 7 lines

Yes, instantiating the metadata and accounting the data is a good approach. The file layout will be a bit suboptimal, but surely that will be a minor thing.

But boy, it's a complicated patch! Are we really sure that we want to make changes this extensive to our antiquated old fs? Or do we just say "yeah, it's broken with quotas - use ext4"?

Quoted 6 lines

The patch isn't trivial, I agree (although it's mostly straightforward).
Regarding telling users to switch to ext4 - it seems a bit harsh to me to ask people to switch to ext4 as a response to a (possibly security) issue they uncover. Because for most admins switching to ext4 will require some non-trivial testing I presume. Of course, the counterweight is the possibility of new bugs introduced to the code by my patch.

Quoted 6 lines

Yes.

Quoted 2 lines

But after some
considerations I've decided it's worth it and and fixed the bug...

Well. How did you come to that decision?

So my thoughts were: If a company runs a hosting or similar service and
some load either inadvertedly or even maliciously triggers this bug, your systems can be DOSed. That's bad and you need to fix that ASAP. From my experience with our SLE customers, they are willing to listen to advices such as fs choice when they plan a system deployment. After that they vehemently refuse any major change (and fs driver change is a major one). So I'm quite certain they'd rather accept this largish ext3 change. Finally, admittedly, I didn't think the patch will end up so large.

Looking into the patch, I could split off some cleanups and code reorganizations which are 20-30% of the patch but it probably does not make sense to split it more. What I think is a plus of the patch is that there are only two code paths that really change - ext3_get_blocks() has two new cases how it is called (to allocate only indirect blocks and to allocate already reserved data block) and trucate path which has to do more work to check whether indirect block can be removed.

Quoted 1 line

Are real users hurting from this problem?

I've got a report of this from NEC
(http://ns3.spinics.net/lists/linux-ext4/msg20239.html) and OpenVZ people were also concerned
(http://ns3.spinics.net/lists/linux-ext4/msg20288.html). I think there was one more report of this problem but I can't find it now. So yes, there are users who care.

Quoted 3 lines
Quoted 1 line

What's the real-world case for fixing it?

Sorry, I don't understand the question (or how it is different from the
previous one).

Andrew, does this change your opinion in any way?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw

Re: [PATCH 4/4] ext3: Implement delayed allocation on page_mkwrite time

Quoted 28 lines · On Wed, 11 May 2011 17:38:41 +0200 Jan Kara <[email protected]> wrote:
Quoted 28 lines
Quoted 3 lines
Quoted 1 line

considerations I've decided it's worth it and and fixed the bug...

Well. How did you come to that decision?

So my thoughts were: If a company runs a hosting or similar service and
some load either inadvertedly or even maliciously triggers this bug, your systems can be DOSed. That's bad and you need to fix that ASAP. From my experience with our SLE customers, they are willing to listen to advices such as fs choice when they plan a system deployment. After that they vehemently refuse any major change (and fs driver change is a major one). So I'm quite certain they'd rather accept this largish ext3 change. Finally, admittedly, I didn't think the patch will end up so large.

Looking into the patch, I could split off some cleanups and code reorganizations which are 20-30% of the patch but it probably does not make sense to split it more. What I think is a plus of the patch is that there are only two code paths that really change - ext3_get_blocks() has two new cases how it is called (to allocate only indirect blocks and to allocate already reserved data block) and trucate path which has to do more work to check whether indirect block can be removed.

Quoted 1 line

Are real users hurting from this problem?

I've got a report of this from NEC
(http://ns3.spinics.net/lists/linux-ext4/msg20239.html) and OpenVZ people were also concerned
(http://ns3.spinics.net/lists/linux-ext4/msg20288.html). I think there was one more report of this problem but I can't find it now. So yes, there are users who care.

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

What's the real-world case for fixing it?

Sorry, I don't understand the question (or how it is different from the
previous one).

Andrew, does this change your opinion in any way?

I didn't actually have an opinion - I was checking, as usual, that the proposed change passes the cost-vs-benefit test.

Yes, if real users are hitting the problem in real life then that strengthens the case for fixing it.