Skip to contentSkip to conversation

Ext3 data=guarded

RFC PATCH In review Chris Mason 3 participants 11 messages Sep 8, 2009 – Sep 24, 2009 VFS

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Ext3 data=guarded

Hello everyone,

Here is a respin of the ext3 data=guarded code. This adds a new mount option (mount -o data=guarded) to prevent garbage in files after a crash.

The main difference from data=ordered is that data=guarded only updates the on disk i_size after all of the data blocks are on disk. This allows us to avoid flushing all the data pages down to disk with every commit.

More details are in the patch emails. It has survived some long stress tests, but I do still need to hammer on O_DIRECT and hole filling.

Jan Kara has already picked up patch 1/2, but I'm including it here in case anyone wants to test the code.

One important part of this patch series is that it tries to have minimal impact on the other data modes of ext3. So, data=ordered and data=writeback should stay the same in performance and reliability with this code.

-chris

#1 lore raw

PATCH 1/2 Ext3: Fix race in ext3_mark_inode_dirty

ext3_mark_inode_dirty has no internal locking to make sure that only one CPU is updating the buffer head at a time. Generally this works out ok because everyone that changes the inode then calls ext3_mark_inode_dirty themselves. Even though it races, eventually someone updates the buffer heads and things move on.

But there is still a risk of the wrong values getting in, and the data=guarded code seems to hit the race very often.

Since everyone that changes the inode also logs it, it should be possible to fix this with some memory barriers. I'll leave that as an exercise to the reader and lock the buffer head instead.

---
 fs/ext3/inode.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
fs/ext3/inode.c +10 -0
index b49908a..a272365 100644
@@ -2892,6 +2892,10 @@ static int ext3_do_update_inode(handle_t *handle,
 	struct buffer_head *bh = iloc->bh;
 	int err = 0, rc, block;
 
+again:
+	/* we can't allow multiple procs in here at once, its a bit racey */
+	lock_buffer(bh);
+
 	/* For fields not not tracking in the in-memory inode,
 	 * initialise them to zero for new inodes. */
 	if (ei->i_state & EXT3_STATE_NEW)
@@ -2951,16 +2955,20 @@ static int ext3_do_update_inode(handle_t *handle,
 			       /* If this is the first large file
 				* created, add a flag to the superblock.
 				*/
+				unlock_buffer(bh);
 				err = ext3_journal_get_write_access(handle,
 						EXT3_SB(sb)->s_sbh);
 				if (err)
 					goto out_brelse;
+
 				ext3_update_dynamic_rev(sb);
 				EXT3_SET_RO_COMPAT_FEATURE(sb,
 					EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
 				handle->h_sync = 1;
 				err = ext3_journal_dirty_metadata(handle,
 						EXT3_SB(sb)->s_sbh);
+				/* get our lock and start over */
+				goto again;
 			}
 		}
 	}
@@ -2983,6 +2991,7 @@ static int ext3_do_update_inode(handle_t *handle,
 		raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
 
 	BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
+	unlock_buffer(bh);
 	rc = ext3_journal_dirty_metadata(handle, bh);
 	if (!err)
 		err = rc;
@@ -2991,6 +3000,7 @@ static int ext3_do_update_inode(handle_t *handle,
 out_brelse:
 	brelse (bh);
 	ext3_std_error(inode->i_sb, err);
+
 	return err;
 }
 
Signature · 1 line
1.6.4.1
#2 lore raw

PATCH 2/2 Ext3: data=guarded mode

In reply to Chris Mason

ext3 data=ordered mode makes sure that data blocks are on disk before the metadata that references them, which avoids files full of garbage or previously deleted data after a crash. It does this by adding every dirty buffer onto a list of things that must be written before a commit.

This makes every fsync write out all the dirty data on the entire FS, which has high latencies and is generally much more expensive than it needs to be.

Another way to avoid exposing stale data after a crash is to wait until after the data buffers are written before updating the on-disk record of the file's size. If we crash before the data IO is done, i_size doesn't yet include the new blocks and no stale data is exposed.

This patch adds the delayed i_size update to ext3, along with a new mount option (data=guarded) to enable it. The basic mechanism works like this:

* Add a list to the in-memory ext3 inode for tracking data=guarded buffer heads that are waiting to be sent to disk.

* Add an ext3 guarded write_end call to add buffer heads for newly allocated blocks into the list. If we have a newly allocated block that is filling a hole inside i_size, this is done as an old style data=ordered write instead.

* Add an ext3 guarded writepage call that uses a special buffer head end_io handler for buffers that are marked as guarded. Again, if we find newly allocated blocks filling holes, they are sent through data=ordered instead of data=guarded.

* When a guarded IO finishes, kick a per-FS workqueue to do the on disk i_size updates. The workqueue function must be very careful. We only update the on disk i_size if all of the IO between the old on disk i_size and the new on disk i_size is complete. The on disk i_size is incrementally updated to the largest safe value every time an IO completes.

* When we start tracking guarded buffers on a given inode, we put the inode into ext3's orphan list. This way if we do crash, the file will be truncated back down to the on disk i_size and we'll free any blocks that were not completely written. The inode is removed from the orphan list only after all the guarded buffers are done.

---
 fs/ext3/Makefile           |    3 +-
 fs/ext3/fsync.c            |   12 +
 fs/ext3/inode.c            |  626 +++++++++++++++++++++++++++++++++++++++++++-
 fs/ext3/namei.c            |   21 +-
 fs/ext3/ordered-data.c     |  234 +++++++++++++++++
 fs/ext3/super.c            |   48 +++-
 fs/jbd/transaction.c       |    1 +
 include/linux/ext3_fs.h    |   33 +++-
 include/linux/ext3_fs_i.h  |   45 ++++
 include/linux/ext3_fs_sb.h |    6 +
 include/linux/ext3_jbd.h   |   11 +
 include/linux/jbd.h        |   10 +
 12 files changed, 1024 insertions(+), 26 deletions(-)
 create mode 100644 fs/ext3/ordered-data.c
fs/ext3/Makefile +2 -1
index e77766a..f3a9dc1 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 \
+	   ordered-data.o
 
 ext3-$(CONFIG_EXT3_FS_XATTR)	 += xattr.o xattr_user.o xattr_trusted.o
 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
fs/ext3/fsync.c +12 -0
index d336341..a50abb4 100644
@@ -59,6 +59,11 @@ int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 	 *  sync_inode() will write the inode if it is dirty.  Then the caller's
 	 *  filemap_fdatawait() will wait on the pages.
 	 *
+	 * data=guarded:
+	 * The caller's filemap_fdatawrite will start the IO, and we
+	 * use filemap_fdatawait here to make sure all the disk i_size updates
+	 * are done before we commit the inode.
+	 *
 	 * data=journal:
 	 *  filemap_fdatawrite won't do anything (the buffers are clean).
 	 *  ext3_force_commit will write the file data into the journal and
@@ -84,6 +89,13 @@ int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
+		/*
+		 * the new disk i_size must be logged before we commit,
+		 * so we wait here for pending writeback
+		 */
+		if (ext3_should_guard_data(inode))
+			filemap_write_and_wait(inode->i_mapping);
+
 		ret = sync_inode(inode, &wbc);
 	}
 out:
fs/ext3/inode.c +614 -12

791 lines not on this page · raw message

fs/ext3/namei.c +14 -7
index 6ff7b97..711549a 100644
@@ -1973,11 +1973,21 @@ out_unlock:
 	return err;
 }
 
+int ext3_orphan_del(handle_t *handle, struct inode *inode)
+{
+	int ret;
+
+	lock_super(inode->i_sb);
+	ret = ext3_orphan_del_locked(handle, inode);
+	unlock_super(inode->i_sb);
+	return ret;
+}
+
 /*
  * ext3_orphan_del() removes an unlinked or truncated inode from the list
  * of such inodes stored on disk, because it is finally being cleaned up.
  */
-int ext3_orphan_del(handle_t *handle, struct inode *inode)
+int ext3_orphan_del_locked(handle_t *handle, struct inode *inode)
 {
 	struct list_head *prev;
 	struct ext3_inode_info *ei = EXT3_I(inode);
@@ -1986,11 +1996,8 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 	struct ext3_iloc iloc;
 	int err = 0;
 
-	lock_super(inode->i_sb);
-	if (list_empty(&ei->i_orphan)) {
-		unlock_super(inode->i_sb);
+	if (list_empty(&ei->i_orphan))
 		return 0;
-	}
 
 	ino_next = NEXT_ORPHAN(inode);
 	prev = ei->i_orphan.prev;
@@ -2040,7 +2047,6 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 out_err:
 	ext3_std_error(inode->i_sb, err);
 out:
-	unlock_super(inode->i_sb);
 	return err;
 
 out_brelse:
@@ -2410,7 +2416,8 @@ static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
 		ext3_mark_inode_dirty(handle, new_inode);
 		if (!new_inode->i_nlink)
 			ext3_orphan_add(handle, new_inode);
-		if (ext3_should_writeback_data(new_inode))
+		if (ext3_should_writeback_data(new_inode) ||
+		    ext3_should_guard_data(new_inode))
 			flush_file = 1;
 	}
 	retval = 0;
fs/ext3/ordered-data.c new file +234 -0

234 lines not on this page · raw message

fs/ext3/super.c +43 -5

136 lines not on this page · raw message

fs/jbd/transaction.c +1 -0
index c03ac11..f6b7195 100644
@@ -1967,6 +1967,7 @@ zap_buffer_unlocked:
 	clear_buffer_mapped(bh);
 	clear_buffer_req(bh);
 	clear_buffer_new(bh);
+	clear_buffer_datanew(bh);
 	bh->b_bdev = NULL;
 	return may_free;
 }
include/linux/ext3_fs.h +32 -1
index 7499b36..8583819 100644
@@ -18,6 +18,7 @@
 
 #include <linux/types.h>
 #include <linux/magic.h>
+#include <linux/workqueue.h>
 
 /*
  * The second extended filesystem constants/structures
@@ -398,7 +399,6 @@ struct ext3_inode {
 #define EXT3_MOUNT_MINIX_DF		0x00080	/* Mimics the Minix statfs */
 #define EXT3_MOUNT_NOLOAD		0x00100	/* Don't use existing journal*/
 #define EXT3_MOUNT_ABORT		0x00200	/* Fatal error detected */
-#define EXT3_MOUNT_DATA_FLAGS		0x00C00	/* Mode for data writes: */
 #define EXT3_MOUNT_JOURNAL_DATA		0x00400	/* Write data to journal */
 #define EXT3_MOUNT_ORDERED_DATA		0x00800	/* Flush data before commit */
 #define EXT3_MOUNT_WRITEBACK_DATA	0x00C00	/* No data ordering */
@@ -414,6 +414,12 @@ struct ext3_inode {
 #define EXT3_MOUNT_GRPQUOTA		0x200000 /* "old" group quota */
 #define EXT3_MOUNT_DATA_ERR_ABORT	0x400000 /* Abort on file data write
 						  * error in ordered mode */
+#define EXT3_MOUNT_GUARDED_DATA		0x800000 /* guard new writes with
+						    i_size */
+#define EXT3_MOUNT_DATA_FLAGS		(EXT3_MOUNT_JOURNAL_DATA | \
+					 EXT3_MOUNT_ORDERED_DATA | \
+					 EXT3_MOUNT_WRITEBACK_DATA | \
+					 EXT3_MOUNT_GUARDED_DATA)
 
 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
 #ifndef _LINUX_EXT2_FS_H
@@ -892,6 +898,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);
+void ext3_run_guarded_work(struct work_struct *work);
 
 /* ioctl.c */
 extern long ext3_ioctl(struct file *, unsigned int, unsigned long);
@@ -900,6 +907,7 @@ extern long ext3_compat_ioctl(struct file *, unsigned int, unsigned long);
 /* namei.c */
 extern int ext3_orphan_add(handle_t *, struct inode *);
 extern int ext3_orphan_del(handle_t *, struct inode *);
+extern int ext3_orphan_del_locked(handle_t *, struct inode *);
 extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
 				__u32 start_minor_hash, __u32 *next_hash);
 
@@ -945,7 +953,30 @@ extern const struct inode_operations ext3_special_inode_operations;
 extern const struct inode_operations ext3_symlink_inode_operations;
 extern const struct inode_operations ext3_fast_symlink_inode_operations;
 
+/* ordered-data.c */
+int ext3_add_ordered_extent(struct inode *inode, u64 file_offset,
+			    struct buffer_head *bh);
+int ext3_put_ordered_extent(struct ext3_ordered_extent *entry);
+int ext3_remove_ordered_extent(struct inode *inode,
+				struct ext3_ordered_extent *entry);
+int ext3_ordered_update_i_size(struct inode *inode);
+void ext3_ordered_inode_init(struct ext3_inode_info *ei);
+void ext3_truncate_ordered_extents(struct inode *inode, u64 offset);
+
+static inline void ext3_ordered_lock(struct inode *inode)
+{
+	spin_lock(&EXT3_I(inode)->ordered_buffers.lock);
+}
 
+static inline void ext3_ordered_unlock(struct inode *inode)
+{
+	spin_unlock(&EXT3_I(inode)->ordered_buffers.lock);
+}
+
+static inline void ext3_get_ordered_extent(struct ext3_ordered_extent *entry)
+{
+	atomic_inc(&entry->refs);
+}
 #endif	/* __KERNEL__ */
 
 #endif	/* _LINUX_EXT3_FS_H */
include/linux/ext3_fs_i.h +45 -0

57 lines not on this page · raw message

include/linux/ext3_fs_sb.h +6 -0
index f07f34d..5dbdbeb 100644
@@ -21,6 +21,7 @@
 #include <linux/wait.h>
 #include <linux/blockgroup_lock.h>
 #include <linux/percpu_counter.h>
+#include <linux/workqueue.h>
 #endif
 #include <linux/rbtree.h>
 
@@ -82,6 +83,11 @@ struct ext3_sb_info {
 	char *s_qf_names[MAXQUOTAS];		/* Names of quota files with journalled quota */
 	int s_jquota_fmt;			/* Format of quota to use */
 #endif
+
+	struct workqueue_struct *guarded_wq;
+	struct work_struct guarded_work;
+	struct list_head guarded_buffers;
+	spinlock_t guarded_lock;
 };
 
 static inline spinlock_t *
include/linux/ext3_jbd.h +11 -0
index cf82d51..45cb4aa 100644
@@ -212,6 +212,17 @@ static inline int ext3_should_order_data(struct inode *inode)
 	return 0;
 }
 
+static inline int ext3_should_guard_data(struct inode *inode)
+{
+	if (!S_ISREG(inode->i_mode))
+		return 0;
+	if (EXT3_I(inode)->i_flags & EXT3_JOURNAL_DATA_FL)
+		return 0;
+	if (test_opt(inode->i_sb, GUARDED_DATA) == EXT3_MOUNT_GUARDED_DATA)
+		return 1;
+	return 0;
+}
+
 static inline int ext3_should_writeback_data(struct inode *inode)
 {
 	if (!S_ISREG(inode->i_mode))
include/linux/jbd.h +10 -0
index c2049a0..bbb7990 100644
@@ -291,6 +291,13 @@ enum jbd_state_bits {
 	BH_State,		/* Pins most journal_head state */
 	BH_JournalHead,		/* Pins bh->b_private and jh->b_bh */
 	BH_Unshadow,		/* Dummy bit, for BJ_Shadow wakeup filtering */
+	BH_DataGuarded,		/* ext3 data=guarded mode buffer
+				 * these have something other than a
+				 * journal_head at b_private */
+	BH_DataNew,		/* BH_new gets cleared too early for
+				 * data=guarded to use it.  So,
+				 * this gets set instead.
+				 */
 };
 
 BUFFER_FNS(JBD, jbd)
@@ -302,6 +309,9 @@ TAS_BUFFER_FNS(Revoked, revoked)
 BUFFER_FNS(RevokeValid, revokevalid)
 TAS_BUFFER_FNS(RevokeValid, revokevalid)
 BUFFER_FNS(Freed, freed)
+BUFFER_FNS(DataGuarded, dataguarded)
+BUFFER_FNS(DataNew, datanew)
+TAS_BUFFER_FNS(DataNew, datanew)
 
 static inline struct buffer_head *jh2bh(struct journal_head *jh)
 {
Signature · 1 line
1.6.4.1
reviewer
#3 lore raw

Re: [PATCH 2/2] Ext3: data=guarded mode

Hi Chris,

thanks for the patch and sorry for replying a bit late...

Quoted 85 lines · Chris Mason

ext3 data=ordered mode makes sure that data blocks are on disk before the metadata that references them, which avoids files full of garbage or previously deleted data after a crash. It does this by adding every dirty buffer onto a list of things that must be written before a commit.

This makes every fsync write out all the dirty data on the entire FS, which has high latencies and is generally much more expensive than it needs to be.

Another way to avoid exposing stale data after a crash is to wait until after the data buffers are written before updating the on-disk record of the file's size. If we crash before the data IO is done, i_size doesn't yet include the new blocks and no stale data is exposed.

This patch adds the delayed i_size update to ext3, along with a new mount option (data=guarded) to enable it. The basic mechanism works like this:

* Add a list to the in-memory ext3 inode for tracking data=guarded buffer heads that are waiting to be sent to disk.

* Add an ext3 guarded write_end call to add buffer heads for newly allocated blocks into the list. If we have a newly allocated block that is filling a hole inside i_size, this is done as an old style data=ordered write instead.

* Add an ext3 guarded writepage call that uses a special buffer head end_io handler for buffers that are marked as guarded. Again, if we find newly allocated blocks filling holes, they are sent through data=ordered instead of data=guarded.

* When a guarded IO finishes, kick a per-FS workqueue to do the on disk i_size updates. The workqueue function must be very careful. We only update the on disk i_size if all of the IO between the old on disk i_size and the new on disk i_size is complete. The on disk i_size is incrementally updated to the largest safe value every time an IO completes.

* When we start tracking guarded buffers on a given inode, we put the inode into ext3's orphan list. This way if we do crash, the file will be truncated back down to the on disk i_size and we'll free any blocks that were not completely written. The inode is removed from the orphan list only after all the guarded buffers are done.

---

fs/ext3/inode.c +33 -0
index a272365..248ac79 100644
@@ -179,6 +180,110 @@ static int ext3_journal_test_restart(handle_t *handle, struct inode *inode)
 }
 
 /*
+ * after a data=guarded IO is done, we need to update the
+ * disk i_size to reflect the data we've written.  If there are
+ * no more ordered data extents left in the list, we need to
+ * get rid of the orphan entry making sure the file's
+ * block pointers match the i_size after a crash
+ *
+ * When we aren't in data=guarded mode, this just does an ext3_orphan_del.
+ *
+ * It returns the result of ext3_orphan_del.
+ *
+ * handle may be null if we are just cleaning up the orphan list in
+ * memory.
+ *
+ * pass must_log == 1 when the inode must be logged in order to get
+ * an i_size update on disk
+ */
+static int orphan_del(handle_t *handle, struct inode *inode, int must_log)
+{
+	int ret = 0;
+	struct list_head *ordered_list;
+
+	ordered_list = &EXT3_I(inode)->ordered_buffers.ordered_list;
+
+	/* fast out when data=guarded isn't on */
+	if (!ext3_should_guard_data(inode)) {
+		WARN_ON(must_log);
+		return ext3_orphan_del(handle, inode);
+	}
+
+	ext3_ordered_lock(inode);
+	if (inode->i_nlink && list_empty(ordered_list)) {
+		ext3_ordered_unlock(inode);
+

fs/ext3/inode.c static int ext3_journal_test_restart(handle_t *handle, struct inode *inode) · line 225

Quoted 10 lines
fs/ext3/inode.c +10 -0
+		lock_super(inode->i_sb);
+
+		/*
+		 * now that we have the lock make sure we are allowed to
+		 * get rid of the orphan.  This way we make sure our
+		 * test isn't happening concurrently with someone else
+		 * adding an orphan.  Memory barrier for the ordered list.
+		 */
+		smp_mb();
+		if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...
2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

We could fix it by removing the list_empty() check but that means taking
superblock lock on each journal_dirty_data_guarded_fn() call which isn't nice either.
Maybe we could postpone removing inode from orphan list until we can do
proper locking. But that means that we would have to do it from a different thread after ending page writeback. Or we could just set i_disksize from end_io, pin the inode in memory by dirtying it and queue work doing proper ext3_mark_inode_dirty() and ext3_orphan_del(). This looks like the cleanest way way but it would need a way to call __mark_inode_dirty() without calling ->dirty_inode() and a support in ext3_write_inode().

Quoted 26 lines
fs/ext3/inode.c +22 -0
+			unlock_super(inode->i_sb);
+			if (must_log)
+				ext3_mark_inode_dirty(handle, inode);
+			goto out;
+		}
+
+		/*
+		 * if we aren't actually on the orphan list, the orphan
+		 * del won't log our inode.  Log it now to make sure
+		 */
+		ext3_mark_inode_dirty(handle, inode);
+
+		ret = ext3_orphan_del_locked(handle, inode);
+
+		unlock_super(inode->i_sb);
+	} else if (handle && must_log) {
@@ -767,6 +879,24 @@ err_out:
 }
 
 /*
+ * This protects the disk i_size with the  spinlock for the ordered
+ * extent tree.  It returns 1 when the inode needs to be logged
+ * because the i_disksize has been updated.
+ */
+static int maybe_update_disk_isize(struct inode *inode, loff_t new_size)
+{

fs/ext3/inode.c err_out: · line 897

Quoted 10 lines
fs/ext3/inode.c +10 -0
+	int ret = 0;
+
+	ext3_ordered_lock(inode);
+	if (EXT3_I(inode)->i_disksize < new_size) {
+		EXT3_I(inode)->i_disksize = new_size;
+		ret = 1;
+	}
+	ext3_ordered_unlock(inode);
+	return ret;
+}

Why is this function here? It is called only from update_file_sizes()
which is called only from ext3_ordered_write_end() and ext3_writeback_write_end(). So ordered_lock shouldn't be needed for them?

Quoted 21 lines
fs/ext3/inode.c +17 -0
@@ -1294,6 +1595,73 @@ static int ext3_ordered_write_end(struct file *file,
 	return ret ? ret : copied;
 }
 
+static int ext3_guarded_write_end(struct file *file,
+				struct address_space *mapping,
+				loff_t pos, unsigned len, unsigned copied,
+				struct page *page, void *fsdata)
+{
+	handle_t *handle = ext3_journal_current_handle();
+	struct inode *inode = file->f_mapping->host;
+	unsigned from, to;
+	int ret = 0, ret2;
+
+	copied = block_write_end(file, mapping, pos, len, copied,
+				 page, fsdata);
+
+	from = pos & (PAGE_CACHE_SIZE - 1);
+	to = from + copied;
+	ret = walk_page_buffers(handle, page_buffers(page),
+		from, to, NULL, journal_dirty_data_guarded_fn);

fs/ext3/inode.c static int ext3_ordered_write_end(struct file *file, · line 1624

Quoted 10 lines
fs/ext3/inode.c +10 -0
+
+	/*
+	 * we only update the in-memory i_size.  The disk i_size is done
+	 * by the end io handlers
+	 */
+	if (ret == 0 && pos + copied > inode->i_size) {
+		int must_log;
+
+		/* updated i_size, but we may have raced with a
+		 * data=guarded end_io handler.

I don't understand the above sentence.

Quoted 5 lines
fs/ext3/inode.c +5 -0
+int ext3_put_ordered_extent(struct ext3_ordered_extent *entry)
+{
+	if (atomic_dec_and_test(&entry->refs)) {
+		WARN_ON(entry->bh);
+		WARN_ON(entry->end_io_bh);

fs/ext3/inode.c static int ext3_ordered_write_end(struct file *file, · line 1639

Quoted 10 lines
fs/ext3/inode.c +10 -0
+		kfree(entry);
+	}
+	return 0;
+}
+
+/*
+ * remove an ordered extent from the list.  This removes the
+ * reference held by the list on 'entry' and the
+ * reference on the buffer head held by the entry.
+ */

Maybe add a note that this expects buffers->lock (ext3_ordered_lock) to be
locked.

Quoted 2 lines
fs/ext3/inode.c +2 -0
+int ext3_remove_ordered_extent(struct inode *inode,
+				struct ext3_ordered_extent *entry)

...

Quoted 4 lines
fs/ext3/inode.c +4 -0
+/*
+ * during a truncate or delete, we need to get rid of pending
+ * ordered extents so there isn't a war over who updates disk i_size first.
+ * This does that, without waiting for any of the IO to actually finish.

fs/ext3/inode.c

Quoted 10 lines
fs/ext3/inode.c +10 -0
+ *
+ * When the IO does finish, it will find the ordered extent removed from the
+ * list and all will work properly.
+ */
+void ext3_truncate_ordered_extents(struct inode *inode, u64 offset)
+{
+	struct ext3_ordered_buffers *buffers = &EXT3_I(inode)->ordered_buffers;
+	struct ext3_ordered_extent *test;
+
+	spin_lock(&buffers->lock);

It would be easier to read if you used ext3_ordered_lock() here as
everywhere else...

Quoted 24 lines
fs/ext3/inode.c +24 -0
+	while (!list_empty(&buffers->ordered_list)) {
+
+		test = list_entry(buffers->ordered_list.prev,
+				  struct ext3_ordered_extent, ordered_list);
+
+		if (test->start < offset)
+			break;
+		/*
+		 * once this is called, the end_io handler won't run,
+		 * and we won't update disk_i_size to include this buffer.
+		 *
+		 * That's ok for truncates because the truncate code is
+		 * writing a new i_size.
+		 *
+		 * This ignores any IO in flight, which is ok
+		 * because the guarded_buffers list has a reference
+		 * on the ordered extent
+		 */
+		ext3_remove_ordered_extent(inode, test);
+	}
+	spin_unlock(&buffers->lock);
+	return;
+
+}

...

include/linux/ext3_fs_i.h struct ext3_inode_info { · line 186

Quoted 12 lines
include/linux/ext3_fs_i.h +2 -0
index ca1bfe9..a6cf26d 100644
@@ -137,6 +180,8 @@ struct ext3_inode_info {
 	 * by other means, so we have truncate_mutex.
 	 */
 	struct mutex truncate_mutex;
+
+	struct ext3_ordered_buffers ordered_buffers;
 	struct inode vfs_inode;
 };

Hmm, how hard would it be to hide especially this behind
CONFIG_EXT3_GUARDED_DATA so that we can avoid increasing inode size for users which are not interested in the new guarded mode?

Honza

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

Re: [PATCH 2/2] Ext3: data=guarded mode

Quoted 4 lines · Jan Kara

Hi Chris,

thanks for the patch and sorry for replying a bit late...

Thanks for reading through things!

Quoted 25 lines
Quoted 25 lines
fs/ext3/inode.c +25 -0
+static int orphan_del(handle_t *handle, struct inode *inode, int must_log)
+{
+	int ret = 0;
+	struct list_head *ordered_list;
+
+	ordered_list = &EXT3_I(inode)->ordered_buffers.ordered_list;
+
+	/* fast out when data=guarded isn't on */
+	if (!ext3_should_guard_data(inode)) {
+		WARN_ON(must_log);
+		return ext3_orphan_del(handle, inode);
+	}
+
+	ext3_ordered_lock(inode);
+	if (inode->i_nlink && list_empty(ordered_list)) {
+		ext3_ordered_unlock(inode);
+
+		lock_super(inode->i_sb);
+
+		/*
+		 * now that we have the lock make sure we are allowed to
+		 * get rid of the orphan.  This way we make sure our
+		 * test isn't happening concurrently with someone else
+		 * adding an orphan.  Memory barrier for the ordered list.
+		 */
Quoted 8 lines

fs/ext3/inode.c static int ext3_journal_test_restart(handle_t *handle, struct inode *inode) · line 225

Quoted 2 lines
fs/ext3/inode.c +2 -0
+		smp_mb();
+		if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...

The only risky place for this is the work thread doing the ordered writes. Truncate gets around it by waiting for the ordered completions. I'll add the wait to the error handlers as well.

Quoted 15 lines

2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't
Quoted 2 lines

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

This used to have a check after the orphan_del to re-add the orphan if we raced with the end_io handlers. I removed it because I thought it was over-paranoid, but I see that you're right. So, I'll put that one back in.

Quoted 13 lines
Quoted 13 lines

/*
+ * This protects the disk i_size with the spinlock for the ordered
+ * extent tree. It returns 1 when the inode needs to be logged
+ * because the i_disksize has been updated.
+ */

fs/ext3/inode.c +7 -0
+static int maybe_update_disk_isize(struct inode *inode, loff_t new_size)
+{
+	int ret = 0;
+
+	ext3_ordered_lock(inode);
+	if (EXT3_I(inode)->i_disksize < new_size) {
+		EXT3_I(inode)->i_disksize = new_size;
Quoted 8 lines

fs/ext3/inode.c err_out: · line 897

Quoted 5 lines
fs/ext3/inode.c +5 -0
+		ret = 1;
+	}
+	ext3_ordered_unlock(inode);
+	return ret;
+}

Why is this function here? It is called only from update_file_sizes()
which is called only from ext3_ordered_write_end() and ext3_writeback_write_end(). So ordered_lock shouldn't be needed for them?

Well, its a leftover from the original patch. I'll get rid of it.

Quoted 25 lines
Quoted 25 lines
fs/ext3/inode.c +20 -0
@@ -1294,6 +1595,73 @@ static int ext3_ordered_write_end(struct file *file,
 	return ret ? ret : copied;
 }
 
+static int ext3_guarded_write_end(struct file *file,
+				struct address_space *mapping,
+				loff_t pos, unsigned len, unsigned copied,
+				struct page *page, void *fsdata)
+{
+	handle_t *handle = ext3_journal_current_handle();
+	struct inode *inode = file->f_mapping->host;
+	unsigned from, to;
+	int ret = 0, ret2;
+
+	copied = block_write_end(file, mapping, pos, len, copied,
+				 page, fsdata);
+
+	from = pos & (PAGE_CACHE_SIZE - 1);
+	to = from + copied;
+	ret = walk_page_buffers(handle, page_buffers(page),
+		from, to, NULL, journal_dirty_data_guarded_fn);
+
+	/*
+	 * we only update the in-memory i_size.  The disk i_size is done
Quoted 8 lines

fs/ext3/inode.c static int ext3_ordered_write_end(struct file *file, · line 1624

Quoted 7 lines
fs/ext3/inode.c +7 -0
+	 * by the end io handlers
+	 */
+	if (ret == 0 && pos + copied > inode->i_size) {
+		int must_log;
+
+		/* updated i_size, but we may have raced with a
+		 * data=guarded end_io handler.

I don't understand the above sentence.

After an IO finishes, we update the on disk i_size to either the on disk i_size or the start of the first item on the data=guarded list. The write_end code is the place we update i_size, so it must also check the guarded list to see if it is supposed to update the disk i_size as well.

This is especially important for this case:

Create a 3KB file
sync
Append 1 byte on the end

The append won't be a data=guarded write because the block is already on disk. So when we update i_size we have to make sure to also update i_disksize. There are variations on this that involve racing with the end_io worker threads too.

[ ok ]

Quoted 7 lines
Quoted 7 lines
include/linux/ext3_fs_i.h +0 -0
index ca1bfe9..a6cf26d 100644
@@ -137,6 +180,8 @@ struct ext3_inode_info {
 	 * by other means, so we have truncate_mutex.
 	 */
Quoted 8 lines

include/linux/ext3_fs_i.h struct ext3_inode_info { · line 186

Quoted 5 lines
include/linux/ext3_fs_i.h +2 -0
 	struct mutex truncate_mutex;
+
+	struct ext3_ordered_buffers ordered_buffers;
 	struct inode vfs_inode;
 };

Hmm, how hard would it be to hide especially this behind
CONFIG_EXT3_GUARDED_DATA so that we can avoid increasing inode size for users which are not interested in the new guarded mode?

I'm not too picky, but it would litter the code with #ifdefs around the guarded functions. I'd rather not.

-chris

reviewer
#5 lore raw

Re: [PATCH 2/2] Ext3: data=guarded mode

Quoted 35 lines · Chris Mason
Quoted 25 lines · Jan Kara
Quoted 25 lines
fs/ext3/inode.c +25 -0
+static int orphan_del(handle_t *handle, struct inode *inode, int must_log)
+{
+	int ret = 0;
+	struct list_head *ordered_list;
+
+	ordered_list = &EXT3_I(inode)->ordered_buffers.ordered_list;
+
+	/* fast out when data=guarded isn't on */
+	if (!ext3_should_guard_data(inode)) {
+		WARN_ON(must_log);
+		return ext3_orphan_del(handle, inode);
+	}
+
+	ext3_ordered_lock(inode);
+	if (inode->i_nlink && list_empty(ordered_list)) {
+		ext3_ordered_unlock(inode);
+
+		lock_super(inode->i_sb);
+
+		/*
+		 * now that we have the lock make sure we are allowed to
+		 * get rid of the orphan.  This way we make sure our
+		 * test isn't happening concurrently with someone else
+		 * adding an orphan.  Memory barrier for the ordered list.
+		 */
Quoted 8 lines

fs/ext3/inode.c static int ext3_journal_test_restart(handle_t *handle, struct inode *inode) · line 225

Quoted 2 lines
fs/ext3/inode.c +2 -0
+		smp_mb();
+		if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...

Quoted 3 lines

The only risky place for this is the work thread doing the ordered writes. Truncate gets around it by waiting for the ordered completions. I'll add the wait to the error handlers as well.

You probably mean guarded writes. I agree.

Quoted 18 lines
Quoted 15 lines

2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't
Quoted 2 lines

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

Quoted 4 lines

This used to have a check after the orphan_del to re-add the orphan if we raced with the end_io handlers. I removed it because I thought it was over-paranoid, but I see that you're right. So, I'll put that one back in.

Hmm, that will probably work but it's ugly :(. The ugliness is localized
in the guarded mode code so probably we can bear it for a while but I'll certainly try to look into what we can do to get rid of it :).

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
include/linux/ext3_fs_i.h +0 -0
index ca1bfe9..a6cf26d 100644
@@ -137,6 +180,8 @@ struct ext3_inode_info {
 	 * by other means, so we have truncate_mutex.
 	 */
Quoted 8 lines

include/linux/ext3_fs_i.h struct ext3_inode_info { · line 186

Quoted 5 lines
include/linux/ext3_fs_i.h +2 -0
 	struct mutex truncate_mutex;
+
+	struct ext3_ordered_buffers ordered_buffers;
 	struct inode vfs_inode;
 };

Hmm, how hard would it be to hide especially this behind
CONFIG_EXT3_GUARDED_DATA so that we can avoid increasing inode size for users which are not interested in the new guarded mode?

Quoted 2 lines

I'm not too picky, but it would litter the code with #ifdefs around the guarded functions. I'd rather not.

Looking into the code, it needn't be too bad if we define a a few
functions as empty in !guarded case. I'll have a look at it for the next version of your patch.

Honza

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

Re: [PATCH 2/2] Ext3: data=guarded mode

Quoted 1 line · Jan Kara

On Tue 15-09-09 14:39:06, Chris Mason wrote:

[ ... ]

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...

The only risky place for this is the work thread doing the ordered writes. Truncate gets around it by waiting for the ordered completions. I'll add the wait to the error handlers as well.

You probably mean guarded writes. I agree.

Sorry, guarded in ext3 is ordered in btrfs and I'm easily confused.

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines

2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

Quoted 7 lines
Quoted 4 lines

This used to have a check after the orphan_del to re-add the orphan if we raced with the end_io handlers. I removed it because I thought it was over-paranoid, but I see that you're right. So, I'll put that one back in.

Hmm, that will probably work but it's ugly :(. The ugliness is localized
in the guarded mode code so probably we can bear it for a while but I'll certainly try to look into what we can do to get rid of it :).

;)

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

include/linux/ext3_fs_i.h struct ext3_inode_info { · line 186

Quoted 13 lines
include/linux/ext3_fs_i.h +2 -0
index ca1bfe9..a6cf26d 100644
@@ -137,6 +180,8 @@ struct ext3_inode_info {
 	 * by other means, so we have truncate_mutex.
 	 */
 	struct mutex truncate_mutex;
+
+	struct ext3_ordered_buffers ordered_buffers;
 	struct inode vfs_inode;
 };

Hmm, how hard would it be to hide especially this behind
CONFIG_EXT3_GUARDED_DATA so that we can avoid increasing inode size for users which are not interested in the new guarded mode?

Quoted 5 lines
Quoted 2 lines

I'm not too picky, but it would litter the code with #ifdefs around the guarded functions. I'd rather not.

Looking into the code, it needn't be too bad if we define a a few
functions as empty in !guarded case. I'll have a look at it for the next version of your patch.

Fair enough, I plan on hammering out the next version today or tomorrow.

-chris

#7 lore raw

Re: [PATCH 2/2] Ext3: data=guarded mode

In reply to Jan Kara
Quoted 21 lines · Jan Kara

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...
2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't
Quoted 6 lines

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

We could fix it by removing the list_empty() check but that means taking
superblock lock on each journal_dirty_data_guarded_fn() call which isn't nice either.

I've just started testing this incremental, but wanted to send it along so you could poke holes in the idea. Instead of trying to loop around a lack of locking, I've just added the ordered lock into the orphan processing.

With this patch we have the ordered lock held when we're deciding if we're allowed to remove an orphan off the list. It will catch new ordered extents being added to the list, and after new extents are added we have the ordered lock held while we are testing the orphan list.

I think it should solve all the problems, aside from the part where ext3_orphan_del_locked drops the ordered lock halfway through the call.

The second major problem you brought up was with truncate. write_begin and write_end both add an orphan and ext3_truncate to make sure the metadata matches i_size when things go wrong.

This incremental changes the guarded code to flag a given inode as pinned on the ordered list. It won't be removed from the ordered list until it is unpinned, and truncate is the only place that unpins it. This allows us to make sure the inode stays on the orphan list the entire time truncate is working, regardless of how many transactions take place.

I was planning on just flushing the guarded list, but that doesn't work with the page lock held, and write_end needs the page lock held until after we are added to the orphan list.

This is incremental over the last patch and barely tested. Its really only meant for Jan ;)

-chris

fs/ext3/inode.c +36 -29
index 248ac79..a9d3cf6 100644
@@ -209,6 +209,9 @@ static int orphan_del(handle_t *handle, struct inode *inode, int must_log)
 		return ext3_orphan_del(handle, inode);
 	}
 
+	if (ext3_ordered_orphan_pinned(inode))
+		return 0;
+
 	ext3_ordered_lock(inode);
 	if (inode->i_nlink && list_empty(ordered_list)) {
 		ext3_ordered_unlock(inode);
@@ -219,23 +222,27 @@ static int orphan_del(handle_t *handle, struct inode *inode, int must_log)
 		 * now that we have the lock make sure we are allowed to
 		 * get rid of the orphan.  This way we make sure our
 		 * test isn't happening concurrently with someone else
-		 * adding an orphan.  Memory barrier for the ordered list.
+		 * adding an orphan.
 		 */
-		smp_mb();
-		if (inode->i_nlink == 0 || !list_empty(ordered_list)) {
+		ext3_ordered_lock(inode);
+		if (inode->i_nlink == 0 || !list_empty(ordered_list) ||
+		    ext3_ordered_orphan_pinned(inode)) {
+			ext3_ordered_unlock(inode);
 			unlock_super(inode->i_sb);
 			if (must_log)
 				ext3_mark_inode_dirty(handle, inode);
 			goto out;
 		}
 
+		/* this drops the ordered lock for us */
+		ret = ext3_orphan_del_locked(handle, inode);
+
 		/*
 		 * if we aren't actually on the orphan list, the orphan
 		 * del won't log our inode.  Log it now to make sure
 		 */
 		ext3_mark_inode_dirty(handle, inode);
 
-		ret = ext3_orphan_del_locked(handle, inode);
 
 		unlock_super(inode->i_sb);
 	} else if (handle && must_log) {
@@ -879,24 +886,6 @@ err_out:
 }
 
 /*
- * This protects the disk i_size with the  spinlock for the ordered
- * extent tree.  It returns 1 when the inode needs to be logged
- * because the i_disksize has been updated.
- */
-static int maybe_update_disk_isize(struct inode *inode, loff_t new_size)
-{
-	int ret = 0;
-
-	ext3_ordered_lock(inode);
-	if (EXT3_I(inode)->i_disksize < new_size) {
-		EXT3_I(inode)->i_disksize = new_size;
-		ret = 1;
-	}
-	ext3_ordered_unlock(inode);
-	return ret;
-}
-
-/*
  * Allocation strategy is simple: if we have to allocate something, we will
  * have to go the whole way to leaf. So let's do it before attaching anything
  * to tree, set linkage between the newborn blocks, write them if sync is
@@ -1394,11 +1383,15 @@ write_begin_failed:
 		 * finishes. Do this only if ext3_can_truncate() agrees so
 		 * that orphan processing code is happy.
 		 */
-		if (pos + len > inode->i_size && ext3_can_truncate(inode))
+		if (pos + len > inode->i_size && ext3_can_truncate(inode)) {
+			ext3_ordered_pin_orphan(inode);
 			ext3_orphan_add(handle, inode);
+		}
 		ext3_journal_stop(handle);
 		unlock_page(page);
 		page_cache_release(page);
+
+		/* truncate unpins the orphan */
 		if (pos + len > inode->i_size)
 			ext3_truncate(inode);
 	}
@@ -1489,11 +1482,17 @@ static int journal_dirty_data_guarded_fn(handle_t *handle,
 	 * list is the work queue to process completed guarded
 	 * buffers.  That will find the ordered_extent we added
 	 * above and leave us on the orphan list.
+	 *
+	 * We have the ordered lock while testing the orphan list for empty
 	 */
+	ext3_ordered_lock(inode);
 	if (ret == 0 && buffer_dataguarded(bh) &&
 	    list_empty(&EXT3_I(inode)->i_orphan) &&
 	    !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
-			ret = ext3_orphan_add(handle, inode);
+		ext3_ordered_unlock(inode);
+		ret = ext3_orphan_add(handle, inode);
+	} else {
+		ext3_ordered_unlock(inode);
 	}
 out:
 	return ret;
@@ -1548,8 +1547,10 @@ static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
 	/* What matters to us is i_disksize. We don't write i_size anywhere */
 	if (pos + copied > inode->i_size)
 		i_size_write(inode, pos + copied);
-	if (maybe_update_disk_isize(inode, pos + copied))
+	if (pos + copied > EXT3_I(inode)->i_disksize) {
+		EXT3_I(inode)->i_disksize = pos + copied;
 		mark_inode_dirty(inode);
+	}
 }
 
 /*
@@ -1620,9 +1621,7 @@ static int ext3_guarded_write_end(struct file *file,
 	if (ret == 0 && pos + copied > inode->i_size) {
 		int must_log;
 
-		/* updated i_size, but we may have raced with a
-		 * data=guarded end_io handler.
-		 *
+		/*
 		 * All the guarded IO could have ended while i_size was still
 		 * small, and if we're just adding bytes into an existing block
 		 * in the file, we may not be adding a new guarded IO with this
@@ -2962,7 +2961,11 @@ void ext3_truncate(struct inode *inode)
 	 *
 	 * Implication: the file must always be in a sane, consistent
 	 * truncatable state while each transaction commits.
+	 *
+	 * We don't want any of the data=guarded end io handlers to take
+	 * this orphan off the list, so we flag it to make sure it stays
 	 */
+	ext3_ordered_pin_orphan(inode);
 	if (ext3_orphan_add(handle, inode))
 		goto out_stop;
 
@@ -3085,6 +3088,8 @@ out_stop:
 	 * ext3_delete_inode(), and we allow that function to clean up the
 	 * orphan info for us.
 	 */
+	ext3_ordered_unpin_orphan(inode);
+
 	if (inode->i_nlink)
 		orphan_del(handle, inode, 0);
 
@@ -3095,8 +3100,10 @@ out_notrans:
 	 * Delete the inode from orphan list so that it doesn't stay there
 	 * forever and trigger assertion on umount.
 	 */
-	if (inode->i_nlink)
+	if (inode->i_nlink) {
+		ext3_ordered_unpin_orphan(inode);
 		orphan_del(NULL, inode, 0);
+	}
 }
 
 static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
fs/ext3/namei.c +15 -1
index 711549a..7a3fc1f 100644
@@ -1978,6 +1978,9 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 	int ret;
 
 	lock_super(inode->i_sb);
+	ext3_ordered_lock(inode);
+
+	/* drops the ordered lock for us */
 	ret = ext3_orphan_del_locked(handle, inode);
 	unlock_super(inode->i_sb);
 	return ret;
@@ -1986,6 +1989,9 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 /*
  * ext3_orphan_del() removes an unlinked or truncated inode from the list
  * of such inodes stored on disk, because it is finally being cleaned up.
+ *
+ * This expects the ordered lock to be held by the caller and will drop
+ * it right after removing our inode from the orphan list
  */
 int ext3_orphan_del_locked(handle_t *handle, struct inode *inode)
 {
@@ -1996,8 +2002,10 @@ int ext3_orphan_del_locked(handle_t *handle, struct inode *inode)
 	struct ext3_iloc iloc;
 	int err = 0;
 
-	if (list_empty(&ei->i_orphan))
+	if (list_empty(&ei->i_orphan)) {
+		ext3_ordered_unlock(inode);
 		return 0;
+	}
 
 	ino_next = NEXT_ORPHAN(inode);
 	prev = ei->i_orphan.prev;
@@ -2007,6 +2015,12 @@ int ext3_orphan_del_locked(handle_t *handle, struct inode *inode)
 
 	list_del_init(&ei->i_orphan);
 
+	/*
+	 * now that we're off the orphan list, the ordered extent code will
+	 * add it back if a new extent is added.  Drop the ordered lock here
+	 */
+	ext3_ordered_unlock(inode);
+
 	/* If we're on an error path, we may not have a valid
 	 * transaction handle with which to update the orphan list on
 	 * disk, but we still need to remove the inode from the linked
fs/ext3/ordered-data.c +26 -3
index 9f12474..cc8f01d 100644
@@ -24,6 +24,9 @@
 #include <linux/buffer_head.h>
 #include <linux/ext3_jbd.h>
 
+/* bits for the flag field of the ext3_ordered_buffers struct */
+#define ORDERED_FLAG_PIN_ORPHAN 0
+
 /*
  * simple helper to make sure a new entry we're adding is
  * at a larger offset in the file than the last entry in the list
@@ -108,6 +111,8 @@ int ext3_put_ordered_extent(struct ext3_ordered_extent *entry)
  * remove an ordered extent from the list.  This removes the
  * reference held by the list on 'entry' and the
  * reference on the buffer head held by the entry.
+ *
+ * The ordered lock must be held when calling this.
  */
 int ext3_remove_ordered_extent(struct inode *inode,
 				struct ext3_ordered_extent *entry)
@@ -200,7 +205,7 @@ void ext3_truncate_ordered_extents(struct inode *inode, u64 offset)
 	struct ext3_ordered_buffers *buffers = &EXT3_I(inode)->ordered_buffers;
 	struct ext3_ordered_extent *test;
 
-	spin_lock(&buffers->lock);
+	ext3_ordered_lock(inode);
 	while (!list_empty(&buffers->ordered_list)) {
 
 		test = list_entry(buffers->ordered_list.prev,
@@ -221,14 +226,32 @@ void ext3_truncate_ordered_extents(struct inode *inode, u64 offset)
 		 */
 		ext3_remove_ordered_extent(inode, test);
 	}
-	spin_unlock(&buffers->lock);
+	ext3_ordered_unlock(inode);
 	return;
 
 }
 
+void ext3_ordered_pin_orphan(struct inode *inode)
+{
+	set_bit(ORDERED_FLAG_PIN_ORPHAN,
+		&EXT3_I(inode)->ordered_buffers.flags);
+}
+
+void ext3_ordered_unpin_orphan(struct inode *inode)
+{
+	clear_bit(ORDERED_FLAG_PIN_ORPHAN,
+		&EXT3_I(inode)->ordered_buffers.flags);
+}
+
+int ext3_ordered_orphan_pinned(struct inode *inode)
+{
+	return test_bit(ORDERED_FLAG_PIN_ORPHAN,
+		&EXT3_I(inode)->ordered_buffers.flags);
+}
+
 void ext3_ordered_inode_init(struct ext3_inode_info *ei)
 {
 	INIT_LIST_HEAD(&ei->ordered_buffers.ordered_list);
 	spin_lock_init(&ei->ordered_buffers.lock);
+	ei->ordered_buffers.flags = 0;
 }
-
include/linux/ext3_fs.h +4 -0
index 8583819..f55e7e9 100644
@@ -963,6 +963,10 @@ int ext3_ordered_update_i_size(struct inode *inode);
 void ext3_ordered_inode_init(struct ext3_inode_info *ei);
 void ext3_truncate_ordered_extents(struct inode *inode, u64 offset);
 
+void ext3_ordered_pin_orphan(struct inode *inode);
+void ext3_ordered_unpin_orphan(struct inode *inode);
+int ext3_ordered_orphan_pinned(struct inode *inode);
+
 static inline void ext3_ordered_lock(struct inode *inode)
 {
 	spin_lock(&EXT3_I(inode)->ordered_buffers.lock);
include/linux/ext3_fs_i.h +5 -0
index a6cf26d..1e00648 100644
@@ -76,6 +76,11 @@ struct ext3_ordered_buffers {
 	/* protects the list and disk i_size */
 	spinlock_t lock;
 
+	/*
+	 * only one flag right now, keep the orphan entry regardless of the
+	 * ordered data state
+	 */
+	unsigned long flags;
 	struct list_head ordered_list;
 };
 
#8 lore raw

Re: [PATCH 2/2] Ext3: data=guarded mode

Quoted 53 lines · Chris Mason
Quoted 21 lines · Jan Kara

The code here still looks suspicious.
1) Inodes can be on orphan list with i_nlink > 0 when a write failed for some reason and we have to truncate blocks instantiated beyond i_size. Those places (similarly as truncate) expect that while they hold i_mutex they are safe doing what they want with the orphan list. This code would happily remove the inode from orphan list...
2) Cannot it happen that:
CPU1
orphan_del()

  if (inode->i_nlink && list_empty(ordered_list)) {
	ext3_ordered_unlock(inode);
	lock_super(inode->i_sb);
	smp_mb();
	if (inode->i_nlink == 0 || !list_empty(ordered_list)) {

     CPU2
journal_dirty_data_guarded_fn()
  ret = ext3_add_ordered_extent(inode, offset, bh);
  if (ret == 0 && buffer_dataguarded(bh) &&
      list_empty(&EXT3_I(inode)->i_orphan) &&
      !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) - list isn't
Quoted 6 lines

empty yet so we don't add inode to orphan list, but on CPU1, we go ahead and remove inode from the orphan list...

We could fix it by removing the list_empty() check but that means taking
superblock lock on each journal_dirty_data_guarded_fn() call which isn't nice either.

I've just started testing this incremental, but wanted to send it along so you could poke holes in the idea. Instead of trying to loop around a lack of locking, I've just added the ordered lock into the orphan processing.

With this patch we have the ordered lock held when we're deciding if we're allowed to remove an orphan off the list. It will catch new ordered extents being added to the list, and after new extents are added we have the ordered lock held while we are testing the orphan list.

I think it should solve all the problems, aside from the part where ext3_orphan_del_locked drops the ordered lock halfway through the call.

The second major problem you brought up was with truncate. write_begin and write_end both add an orphan and ext3_truncate to make sure the metadata matches i_size when things go wrong.

This incremental changes the guarded code to flag a given inode as pinned on the ordered list. It won't be removed from the ordered list until it is unpinned, and truncate is the only place that unpins it. This allows us to make sure the inode stays on the orphan list the entire time truncate is working, regardless of how many transactions take place.

Quoted 3 lines

I was planning on just flushing the guarded list, but that doesn't work with the page lock held, and write_end needs the page lock held until after we are added to the orphan list.

Ok, here is a new patch with the incremental folded in. No other changes and it has held up pretty well:

From: Chris Mason <[email protected]>
Subject: [PATCH] Ext3: data=guarded mode

ext3 data=ordered mode makes sure that data blocks are on disk before the metadata that references them, which avoids files full of garbage or previously deleted data after a crash. It does this by adding every dirty buffer onto a list of things that must be written before a commit.

This makes every fsync write out all the dirty data on the entire FS, which has high latencies and is generally much more expensive than it needs to be.

Another way to avoid exposing stale data after a crash is to wait until after the data buffers are written before updating the on-disk record of the file's size. If we crash before the data IO is done, i_size doesn't yet include the new blocks and no stale data is exposed.

This patch adds the delayed i_size update to ext3, along with a new mount option (data=guarded) to enable it. The basic mechanism works like this:

* Add a list to the in-memory ext3 inode for tracking data=guarded buffer heads that are waiting to be sent to disk.

* Add an ext3 guarded write_end call to add buffer heads for newly allocated blocks into the list. If we have a newly allocated block that is filling a hole inside i_size, this is done as an old style data=ordered write instead.

* Add an ext3 guarded writepage call that uses a special buffer head end_io handler for buffers that are marked as guarded. Again, if we find newly allocated blocks filling holes, they are sent through data=ordered instead of data=guarded.

* When a guarded IO finishes, kick a per-FS workqueue to do the on disk i_size updates. The workqueue function must be very careful. We only update the on disk i_size if all of the IO between the old on disk i_size and the new on disk i_size is complete. The on disk i_size is incrementally updated to the largest safe value every time an IO completes.

* When we start tracking guarded buffers on a given inode, we put the inode into ext3's orphan list. This way if we do crash, the file will be truncated back down to the on disk i_size and we'll free any blocks that were not completely written. The inode is removed from the orphan list only after all the guarded buffers are done.

---
 fs/ext3/Makefile           |    3 +-
 fs/ext3/fsync.c            |   12 +
 fs/ext3/inode.c            |  631 +++++++++++++++++++++++++++++++++++++++++++-
 fs/ext3/namei.c            |   31 ++-
 fs/ext3/ordered-data.c     |  257 ++++++++++++++++++
 fs/ext3/super.c            |   48 +++-
 fs/jbd/transaction.c       |    1 +
 include/linux/ext3_fs.h    |   37 +++-
 include/linux/ext3_fs_i.h  |   50 ++++
 include/linux/ext3_fs_sb.h |    6 +
 include/linux/ext3_jbd.h   |   11 +
 include/linux/jbd.h        |   10 +
 12 files changed, 1074 insertions(+), 23 deletions(-)
 create mode 100644 fs/ext3/ordered-data.c
fs/ext3/Makefile +2 -1
index e77766a..f3a9dc1 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 \
+	   ordered-data.o
 
 ext3-$(CONFIG_EXT3_FS_XATTR)	 += xattr.o xattr_user.o xattr_trusted.o
 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
fs/ext3/fsync.c +12 -0
index d336341..a50abb4 100644
@@ -59,6 +59,11 @@ int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 	 *  sync_inode() will write the inode if it is dirty.  Then the caller's
 	 *  filemap_fdatawait() will wait on the pages.
 	 *
+	 * data=guarded:
+	 * The caller's filemap_fdatawrite will start the IO, and we
+	 * use filemap_fdatawait here to make sure all the disk i_size updates
+	 * are done before we commit the inode.
+	 *
 	 * data=journal:
 	 *  filemap_fdatawrite won't do anything (the buffers are clean).
 	 *  ext3_force_commit will write the file data into the journal and
@@ -84,6 +89,13 @@ int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
+		/*
+		 * the new disk i_size must be logged before we commit,
+		 * so we wait here for pending writeback
+		 */
+		if (ext3_should_guard_data(inode))
+			filemap_write_and_wait(inode->i_mapping);
+
 		ret = sync_inode(inode, &wbc);
 	}
 out:
fs/ext3/inode.c +620 -11

801 lines not on this page · raw message

fs/ext3/namei.c +26 -5
index 6ff7b97..7a3fc1f 100644
@@ -1973,11 +1973,27 @@ out_unlock:
 	return err;
 }
 
+int ext3_orphan_del(handle_t *handle, struct inode *inode)
+{
+	int ret;
+
+	lock_super(inode->i_sb);
+	ext3_ordered_lock(inode);
+
+	/* drops the ordered lock for us */
+	ret = ext3_orphan_del_locked(handle, inode);
+	unlock_super(inode->i_sb);
+	return ret;
+}
+
 /*
  * ext3_orphan_del() removes an unlinked or truncated inode from the list
  * of such inodes stored on disk, because it is finally being cleaned up.
+ *
+ * This expects the ordered lock to be held by the caller and will drop
+ * it right after removing our inode from the orphan list
  */
-int ext3_orphan_del(handle_t *handle, struct inode *inode)
+int ext3_orphan_del_locked(handle_t *handle, struct inode *inode)
 {
 	struct list_head *prev;
 	struct ext3_inode_info *ei = EXT3_I(inode);
@@ -1986,9 +2002,8 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 	struct ext3_iloc iloc;
 	int err = 0;
 
-	lock_super(inode->i_sb);
 	if (list_empty(&ei->i_orphan)) {
-		unlock_super(inode->i_sb);
+		ext3_ordered_unlock(inode);
 		return 0;
 	}
 
@@ -2000,6 +2015,12 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 
 	list_del_init(&ei->i_orphan);
 
+	/*
+	 * now that we're off the orphan list, the ordered extent code will
+	 * add it back if a new extent is added.  Drop the ordered lock here
+	 */
+	ext3_ordered_unlock(inode);
+
 	/* If we're on an error path, we may not have a valid
 	 * transaction handle with which to update the orphan list on
 	 * disk, but we still need to remove the inode from the linked
@@ -2040,7 +2061,6 @@ int ext3_orphan_del(handle_t *handle, struct inode *inode)
 out_err:
 	ext3_std_error(inode->i_sb, err);
 out:
-	unlock_super(inode->i_sb);
 	return err;
 
 out_brelse:
@@ -2410,7 +2430,8 @@ static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
 		ext3_mark_inode_dirty(handle, new_inode);
 		if (!new_inode->i_nlink)
 			ext3_orphan_add(handle, new_inode);
-		if (ext3_should_writeback_data(new_inode))
+		if (ext3_should_writeback_data(new_inode) ||
+		    ext3_should_guard_data(new_inode))
 			flush_file = 1;
 	}
 	retval = 0;
fs/ext3/ordered-data.c new file +257 -0

257 lines not on this page · raw message

fs/ext3/super.c +43 -5

136 lines not on this page · raw message

fs/jbd/transaction.c +1 -0
index c03ac11..f6b7195 100644
@@ -1967,6 +1967,7 @@ zap_buffer_unlocked:
 	clear_buffer_mapped(bh);
 	clear_buffer_req(bh);
 	clear_buffer_new(bh);
+	clear_buffer_datanew(bh);
 	bh->b_bdev = NULL;
 	return may_free;
 }
include/linux/ext3_fs.h +36 -1
index 7499b36..f55e7e9 100644
@@ -18,6 +18,7 @@
 
 #include <linux/types.h>
 #include <linux/magic.h>
+#include <linux/workqueue.h>
 
 /*
  * The second extended filesystem constants/structures
@@ -398,7 +399,6 @@ struct ext3_inode {
 #define EXT3_MOUNT_MINIX_DF		0x00080	/* Mimics the Minix statfs */
 #define EXT3_MOUNT_NOLOAD		0x00100	/* Don't use existing journal*/
 #define EXT3_MOUNT_ABORT		0x00200	/* Fatal error detected */
-#define EXT3_MOUNT_DATA_FLAGS		0x00C00	/* Mode for data writes: */
 #define EXT3_MOUNT_JOURNAL_DATA		0x00400	/* Write data to journal */
 #define EXT3_MOUNT_ORDERED_DATA		0x00800	/* Flush data before commit */
 #define EXT3_MOUNT_WRITEBACK_DATA	0x00C00	/* No data ordering */
@@ -414,6 +414,12 @@ struct ext3_inode {
 #define EXT3_MOUNT_GRPQUOTA		0x200000 /* "old" group quota */
 #define EXT3_MOUNT_DATA_ERR_ABORT	0x400000 /* Abort on file data write
 						  * error in ordered mode */
+#define EXT3_MOUNT_GUARDED_DATA		0x800000 /* guard new writes with
+						    i_size */
+#define EXT3_MOUNT_DATA_FLAGS		(EXT3_MOUNT_JOURNAL_DATA | \
+					 EXT3_MOUNT_ORDERED_DATA | \
+					 EXT3_MOUNT_WRITEBACK_DATA | \
+					 EXT3_MOUNT_GUARDED_DATA)
 
 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
 #ifndef _LINUX_EXT2_FS_H
@@ -892,6 +898,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);
+void ext3_run_guarded_work(struct work_struct *work);
 
 /* ioctl.c */
 extern long ext3_ioctl(struct file *, unsigned int, unsigned long);
@@ -900,6 +907,7 @@ extern long ext3_compat_ioctl(struct file *, unsigned int, unsigned long);
 /* namei.c */
 extern int ext3_orphan_add(handle_t *, struct inode *);
 extern int ext3_orphan_del(handle_t *, struct inode *);
+extern int ext3_orphan_del_locked(handle_t *, struct inode *);
 extern int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
 				__u32 start_minor_hash, __u32 *next_hash);
 
@@ -945,7 +953,34 @@ extern const struct inode_operations ext3_special_inode_operations;
 extern const struct inode_operations ext3_symlink_inode_operations;
 extern const struct inode_operations ext3_fast_symlink_inode_operations;
 
+/* ordered-data.c */
+int ext3_add_ordered_extent(struct inode *inode, u64 file_offset,
+			    struct buffer_head *bh);
+int ext3_put_ordered_extent(struct ext3_ordered_extent *entry);
+int ext3_remove_ordered_extent(struct inode *inode,
+				struct ext3_ordered_extent *entry);
+int ext3_ordered_update_i_size(struct inode *inode);
+void ext3_ordered_inode_init(struct ext3_inode_info *ei);
+void ext3_truncate_ordered_extents(struct inode *inode, u64 offset);
+
+void ext3_ordered_pin_orphan(struct inode *inode);
+void ext3_ordered_unpin_orphan(struct inode *inode);
+int ext3_ordered_orphan_pinned(struct inode *inode);
+
+static inline void ext3_ordered_lock(struct inode *inode)
+{
+	spin_lock(&EXT3_I(inode)->ordered_buffers.lock);
+}
 
+static inline void ext3_ordered_unlock(struct inode *inode)
+{
+	spin_unlock(&EXT3_I(inode)->ordered_buffers.lock);
+}
+
+static inline void ext3_get_ordered_extent(struct ext3_ordered_extent *entry)
+{
+	atomic_inc(&entry->refs);
+}
 #endif	/* __KERNEL__ */
 
 #endif	/* _LINUX_EXT3_FS_H */
include/linux/ext3_fs_i.h +50 -0

62 lines not on this page · raw message

include/linux/ext3_fs_sb.h +6 -0
index f07f34d..5dbdbeb 100644
@@ -21,6 +21,7 @@
 #include <linux/wait.h>
 #include <linux/blockgroup_lock.h>
 #include <linux/percpu_counter.h>
+#include <linux/workqueue.h>
 #endif
 #include <linux/rbtree.h>
 
@@ -82,6 +83,11 @@ struct ext3_sb_info {
 	char *s_qf_names[MAXQUOTAS];		/* Names of quota files with journalled quota */
 	int s_jquota_fmt;			/* Format of quota to use */
 #endif
+
+	struct workqueue_struct *guarded_wq;
+	struct work_struct guarded_work;
+	struct list_head guarded_buffers;
+	spinlock_t guarded_lock;
 };
 
 static inline spinlock_t *
include/linux/ext3_jbd.h +11 -0
index cf82d51..45cb4aa 100644
@@ -212,6 +212,17 @@ static inline int ext3_should_order_data(struct inode *inode)
 	return 0;
 }
 
+static inline int ext3_should_guard_data(struct inode *inode)
+{
+	if (!S_ISREG(inode->i_mode))
+		return 0;
+	if (EXT3_I(inode)->i_flags & EXT3_JOURNAL_DATA_FL)
+		return 0;
+	if (test_opt(inode->i_sb, GUARDED_DATA) == EXT3_MOUNT_GUARDED_DATA)
+		return 1;
+	return 0;
+}
+
 static inline int ext3_should_writeback_data(struct inode *inode)
 {
 	if (!S_ISREG(inode->i_mode))
include/linux/jbd.h +10 -0
index c2049a0..bbb7990 100644
@@ -291,6 +291,13 @@ enum jbd_state_bits {
 	BH_State,		/* Pins most journal_head state */
 	BH_JournalHead,		/* Pins bh->b_private and jh->b_bh */
 	BH_Unshadow,		/* Dummy bit, for BJ_Shadow wakeup filtering */
+	BH_DataGuarded,		/* ext3 data=guarded mode buffer
+				 * these have something other than a
+				 * journal_head at b_private */
+	BH_DataNew,		/* BH_new gets cleared too early for
+				 * data=guarded to use it.  So,
+				 * this gets set instead.
+				 */
 };
 
 BUFFER_FNS(JBD, jbd)
@@ -302,6 +309,9 @@ TAS_BUFFER_FNS(Revoked, revoked)
 BUFFER_FNS(RevokeValid, revokevalid)
 TAS_BUFFER_FNS(RevokeValid, revokevalid)
 BUFFER_FNS(Freed, freed)
+BUFFER_FNS(DataGuarded, dataguarded)
+BUFFER_FNS(DataNew, datanew)
+TAS_BUFFER_FNS(DataNew, datanew)
 
 static inline struct buffer_head *jh2bh(struct journal_head *jh)
 {
Signature · 1 line
1.6.4.1
#9 lore raw
In reply to Chris Mason
Quoted 3 lines · Chris Mason

The main difference from data=ordered is that data=guarded only updates the on disk i_size after all of the data blocks are on disk. This allows us to avoid flushing all the data pages down to disk with every commit.

I'm a bit confused, because I thought that was already guaranteed by ext3 data=ordered, due to the following mail:

Date: Thu, 16 Mar 2006 13:09:04 -0500
Subject: Re: ext3_ordered_writepage() questions
From: Theodore Ts'o <[email protected]>

Quoted 10 lines
Quoted 5 lines

Yup. Ordered-mode JBD commit needs to write and wait upon all dirty file-data buffers prior to journalling the metadata. If we didn't run journal_dirty_data_fn() against those buffers then they'd still be under I/O after commit had completed.

In non-block allocation case, what metadata are we journaling in writepage() ?
block allocation happend in prepare_write() and commit_write() journaled the transaction. All the meta data updates should be done there. What JBD commit are you refering to here ?

Basically, this boils down to what is our definition of ordered-mode?

If the goal is to make sure we avoid the security exposure of allocating a block and then crashing before we write the data block, potentially exposing previously written data that might be belong to another user, then what Badari is suggesting would avoid this particular problem.

However, if what we are doing is overwriting our own data with more an updated, more recent version of the data block, do we guarantee that any ordering semantics apply? For example, what if we write a data block, and then follow it up with some kind of metadata update (say we touch atime, or add an extended attribute). Do we guarantee that if the metadata update is committed, that the data block will have made it to disk as well? Today that is the way things work, but is that guarantee part of the contract of ordered-mode?

- Ted

-- Jamie

#10 lore raw
Quoted 7 lines · Jamie Lokier
Quoted 3 lines · Chris Mason

The main difference from data=ordered is that data=guarded only updates the on disk i_size after all of the data blocks are on disk. This allows us to avoid flushing all the data pages down to disk with every commit.

I'm a bit confused, because I thought that was already guaranteed by ext3 data=ordered, due to the following mail:

Well, in data=ordered mode, we update the on disk i_size immediately. This means that when the current transaction commits, the on disk i_size reflects everything that has been written from file_write.

In order to avoid exposing stale data in data=ordered, we must force all the dirty data down to disk before the transaction commits.

In data=guarded mode, we update the on disk i_size after all the data IO is complete. This may happen in a later transaction than the original file write, but it allows us to avoid exposing stale data because the i_size on disk is never bumped up until the data isn't stale anymore.

In data=guarded mode, the orphan list is used to make sure that all of the metadata related to bytes that exist past the on disk i_size is properly dealt with if we crash before the on disk i_size is updated.

data=guarded makes no ordering promises about overwriting existing blocks inside of i_size.

-chris