Skip to contentSkip to conversation

ext2 + -osync: not as easy as it seems

PATCH In review Pavel Machek 12 participants 73 messages Jan 13, 2009 – Feb 23, 2009

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

Hi!

I tried to use ext2+ -osync, and was surprised that my data do not hit the (SATA) disk. Fortunately I soon realized that I need hdparm -W0 /dev/sda.

Could the ext2 be teached to use barriers? -W0 hurts performance more than neccessary...

Plus it has a nasty behaviour where it reverts to -W1 if disk connection is momentarily lost. (If you unplug/replug SATA disk, linux will happily rediscover and use it, but -W0 was already forgotten at that point, right?)
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#1 lore raw
Quoted 4 lines

Plus it has a nasty behaviour where it reverts to -W1 if disk connection is momentarily lost. (If you unplug/replug SATA disk, linux will happily rediscover and use it, but -W0 was already forgotten at that point, right?)

If you momentarily lose power your disk state isn't defined anyway. If you do that with the SATA code it should treat it the same as a scsi disk swap so it'll get a new device and the old fs will go down.

maintainer
#2 lore raw

Adding a barrier shouldn't be that hard; just a matter adding a call to blkdev_issue_flush() to ext2_sync_file() before it returns.

BTW, I think there's a stale documentation bug in in block/blk-barrier.c, around line 305:

* Description:

  *    Issue a flush for the block device in question. Caller can supply
  *    room for storing the error offset in case of a flush error, if they
- *    wish to.  Caller must run wait_for_completion() on its own.
+ *    wish to.

*/
int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) {

Jens, is that right?

- Ted

#3 lore raw
Quoted 11 lines · Theodore Tso

Adding a barrier shouldn't be that hard; just a matter adding a call to blkdev_issue_flush() to ext2_sync_file() before it returns.

BTW, I think there's a stale documentation bug in in block/blk-barrier.c, around line 305:

* Description:

  *    Issue a flush for the block device in question. Caller can supply
  *    room for storing the error offset in case of a flush error, if they
- *    wish to.  Caller must run wait_for_completion() on its own.
+ *    wish to.
Quoted 5 lines

*/
int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) {

Jens, is that right?

Yep, that is indeed stale!

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw

PATCH 1/1 block: Fix documentation for blkdev_issue_flush()

---
 block/blk-barrier.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block/blk-barrier.c +1 -1
index 8eba4e4..f7dae57 100644
@@ -302,7 +302,7 @@ static void bio_end_empty_barrier(struct bio *bio, int err)
  * Description:
  *    Issue a flush for the block device in question. Caller can supply
  *    room for storing the error offset in case of a flush error, if they
- *    wish to.  Caller must run wait_for_completion() on its own.
+ *    wish to.
  */
 int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
 {
Signature · 1 line
1.6.0.4.8.g36f27.dirty
#5 lore raw

Re: [PATCH] block: Fix documentation for blkdev_issue_flush()

Quoted 10 lines · Theodore Ts'o
---
 block/blk-barrier.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block/blk-barrier.c +0 -0
index 8eba4e4..f7dae57 100644

block/blk-barrier.c static void bio_end_empty_barrier(struct bio *bio, int err) · line 308

Quoted 8 lines
block/blk-barrier.c +1 -1
@@ -302,7 +302,7 @@ static void bio_end_empty_barrier(struct bio *bio, int err)
  * Description:
  *    Issue a flush for the block device in question. Caller can supply
  *    room for storing the error offset in case of a flush error, if they
- *    wish to.  Caller must run wait_for_completion() on its own.
+ *    wish to.
  */
 int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
 {

Thanks Ted, applied.

Signature · 1 line
Jens Axboe
reviewer
#6 lore raw
In reply to Theodore Tso
Quoted 2 lines · Theodore Tso

Adding a barrier shouldn't be that hard; just a matter adding a call to blkdev_issue_flush() to ext2_sync_file() before it returns.

Yes. Something like the patch below?

But it's not the whole story. Strictly speaking we should also call
blkdev_issue_flush() whenever we write things because of O_SYNC or O_DIRSYNC flags. My patch does also that (it's based on the previous ext2 patch I've sent a while before).

Honza

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

---
Quoted 1 line

From 0f5a1865a9f1538886699c5d13d3527343c88c11 Mon Sep 17 00:00:00 2001

From: Jan Kara <[email protected]>
Date: Tue, 13 Jan 2009 15:09:18 +0100
Subject: [PATCH] ext2: Add blk_issue_flush() to syncing paths

To be really safe that the data hit the platter, we should also flush drive's writeback caches on fsync and for O_SYNC files or O_DIRSYNC inodes.

---
 fs/ext2/dir.c   |    5 ++++-
 fs/ext2/fsync.c |    7 +++++--
 fs/ext2/inode.c |    7 +++++++
 fs/ext2/xattr.c |    6 +++++-
 4 files changed, 21 insertions(+), 4 deletions(-)
fs/ext2/dir.c +4 -1
index 7fba549..9ab9347 100644
@@ -25,6 +25,7 @@
 #include <linux/buffer_head.h>
 #include <linux/pagemap.h>
 #include <linux/swap.h>
+#include <linux/blkdev.h>		/* for blkdev_issue_flush() */
 
 typedef struct ext2_dir_entry_2 ext2_dirent;
 
@@ -96,8 +97,10 @@ static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
 	}
 	unlock_page(page);
 
-	if (IS_DIRSYNC(dir))
+	if (IS_DIRSYNC(dir)) {
 		err = ext2_sync_inode(dir);
+		blkdev_issue_flush(dir->i_sb->s_bdev, NULL);
+	}
 
 	return err;
 }
fs/ext2/fsync.c +5 -2
index fc66c93..9cd1838 100644
@@ -24,6 +24,7 @@
 
 #include "ext2.h"
 #include <linux/buffer_head.h>		/* for sync_mapping_buffers() */
+#include <linux/blkdev.h>		/* for blkdev_issue_flush() */
 
 
 /*
@@ -39,12 +40,14 @@ int ext2_sync_file(struct file *file, struct dentry *dentry, int datasync)
 
 	ret = sync_mapping_buffers(inode->i_mapping);
 	if (!(inode->i_state & I_DIRTY))
-		return ret;
+		goto out;
 	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		return ret;
+		goto out;
 
 	err = ext2_sync_inode(inode);
 	if (ret == 0)
 		ret = err;
+out:
+	blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 	return ret;
 }
fs/ext2/inode.c +7 -0
index 23fff2f..49b479e 100644
@@ -33,6 +33,7 @@
 #include <linux/mpage.h>
 #include <linux/fiemap.h>
 #include <linux/namei.h>
+#include <linux/blkdev.h>	/* for blkdev_issue_flush() */
 #include "ext2.h"
 #include "acl.h"
 #include "xip.h"
@@ -68,9 +69,14 @@ void ext2_delete_inode (struct inode * inode)
 	mark_inode_dirty(inode);
 	ext2_update_inode(inode, inode_needs_sync(inode));
 
+	/* Make sure inode deletion really gets to disk. Disk write caches
+	 * are flushed either in ext2_truncate() or we do it explicitly */
 	inode->i_size = 0;
 	if (inode->i_blocks)
 		ext2_truncate (inode);
+	else if (inode_needs_sync(inode))
+		blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+
 	ext2_free_inode (inode);
 
 	return;
@@ -1104,6 +1110,7 @@ do_indirects:
 	if (inode_needs_sync(inode)) {
 		sync_mapping_buffers(inode->i_mapping);
 		ext2_sync_inode (inode);
+		blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 	} else {
 		mark_inode_dirty(inode);
 	}
fs/ext2/xattr.c +5 -1
index 987a526..d480216 100644
@@ -60,6 +60,7 @@
 #include <linux/mbcache.h>
 #include <linux/quotaops.h>
 #include <linux/rwsem.h>
+#include <linux/blkdev.h>		/* for blkdev_issue_flush() */
 #include "ext2.h"
 #include "xattr.h"
 #include "acl.h"
@@ -702,6 +703,7 @@ ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
 				DQUOT_FREE_BLOCK(inode, 1);
 			goto cleanup;
 		}
+		blkdev_issue_flush(sb->s_bdev, NULL);
 	} else
 		mark_inode_dirty(inode);
 
@@ -792,8 +794,10 @@ ext2_xattr_delete_inode(struct inode *inode)
 			le32_to_cpu(HDR(bh)->h_refcount));
 		unlock_buffer(bh);
 		mark_buffer_dirty(bh);
-		if (IS_SYNC(inode))
+		if (IS_SYNC(inode)) {
 			sync_dirty_buffer(bh);
+			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		}
 		DQUOT_FREE_BLOCK(inode, 1);
 	}
 	EXT2_I(inode)->i_file_acl = 0;
Signature · 1 line
1.6.0.2
maintainer Acked-by
#7 lore raw
Quoted 5 lines · Jan Kara

But it's not the whole story. Strictly speaking we should also call
blkdev_issue_flush() whenever we write things because of O_SYNC or O_DIRSYNC flags. My patch does also that (it's based on the previous ext2 patch I've sent a while before).

Looks good to me.

- Ted

#8 lore raw
In reply to Jan Kara

(CCing Eric Sandeen)

Quoted 9 lines · Jan Kara
Quoted 2 lines · Theodore Tso

Adding a barrier shouldn't be that hard; just a matter adding a call to blkdev_issue_flush() to ext2_sync_file() before it returns.

Yes. Something like the patch below?

But it's not the whole story. Strictly speaking we should also call
blkdev_issue_flush() whenever we write things because of O_SYNC or O_DIRSYNC flags. My patch does also that (it's based on the previous ext2 patch I've sent a while before).

Commit d755fb384250d6bd7fd18a0930e71965acc8e72e added a call to blkdev_issue_flush to ext4_sync_file, and looking at its ext3 counterpart it seems it might be needed there too.

I may be missing something, but is it possible to ensure the inode hits the platter without the patch below?

--

From: Fernando Luis Vazquez Cao <[email protected]>
Subject: ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync, we should call blkdev_issue_flush if barriers are supported.

This is a straight port of a similar patch written by Eric Sandeen for ext4 (commit d755fb384250d6bd7fd18a0930e71965acc8e72e).

---

fs/ext3/fsync.c +5 -0
diff -urNp linux-2.6.29-rc1-orig/fs/ext3/fsync.c linux-2.6.29-rc1/fs/ext3/fsync.c
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,7 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -85,6 +87,9 @@ int ext3_sync_file(struct file * file, s
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+
+		if (journal && (journal->j_flags & JFS_BARRIER))
+			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 	}
 out:
 	return ret;
reviewer
#9 lore raw
Quoted 13 lines · Fernando Luis Vázquez Cao

(CCing Eric Sandeen)

Quoted 9 lines · Jan Kara
Quoted 2 lines · Theodore Tso

Adding a barrier shouldn't be that hard; just a matter adding a call to blkdev_issue_flush() to ext2_sync_file() before it returns.

Yes. Something like the patch below?

But it's not the whole story. Strictly speaking we should also call
blkdev_issue_flush() whenever we write things because of O_SYNC or O_DIRSYNC flags. My patch does also that (it's based on the previous ext2 patch I've sent a while before).

Quoted 6 lines

Commit d755fb384250d6bd7fd18a0930e71965acc8e72e added a call to blkdev_issue_flush to ext4_sync_file, and looking at its ext3 counterpart it seems it might be needed there too.

I may be missing something, but is it possible to ensure the inode hits the platter without the patch below?

Yes, I noticed that yesterday as well. But then I was puzzled why ext4
would need the flush where it has it... sync_inode() has started and committed a transaction which issued a barrier when the commit was done. The only reason I could imagine is that barrier (although it is usually translated to flushing writeback caches) actually means just an ordering requirement and hence does not necessarily mean that the caches are properly flushed. Is that so Eric?
BTW: We should also issue the flush in the fdatasync() case, shouldn't
we?

Honza

Quoted 43 lines

From: Fernando Luis Vazquez Cao <[email protected]>
Subject: ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync, we should call blkdev_issue_flush if barriers are supported.

This is a straight port of a similar patch written by Eric Sandeen for ext4 (commit d755fb384250d6bd7fd18a0930e71965acc8e72e).

---

diff -urNp linux-2.6.29-rc1-orig/fs/ext3/fsync.c linux-2.6.29-rc1/fs/ext3/fsync.c

fs/ext3/fsync.c +5 -0
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,7 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -85,6 +87,9 @@ int ext3_sync_file(struct file * file, s
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+
+		if (journal && (journal->j_flags & JFS_BARRIER))
+			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 	}
 out:
 	return ret;
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#10 lore raw
Quoted 3 lines · Jan Kara

Yes, I noticed that yesterday as well. But then I was puzzled why ext4
would need the flush where it has it... sync_inode() has started and committed a transaction which issued a barrier when the commit was done.

You're right; I'm not convinced we need the flush in ext4 (or ext3) at all. We write the data blocks, *then* we call ext4_write_inode(), which will force a commit. Now, if we apply that patch which optimizes out commits if there are no dirty blocks, then we'll be trouble, because we won't know for sure whether or not ext4_write_inode() will have forced a journal commit.

If we optimize out the journal commit when there are no blocks attached to the transaction, we could change the patch to only force a flush if inode->i_state did not have I_DIRTY before the call to sync_inode(). Does that sound sane?

Quoted 4 lines

The only reason I could imagine is that barrier (although it is usually translated to flushing writeback caches) actually means just an ordering requirement and hence does not necessarily mean that the caches are properly flushed. Is that so Eric?

I'm not sure what you mean; if the barrier operation isn't flushing all of the caches all the way out to the iron oxide, it's not going to be working properly no matter where it is being called, whether it's in ext4_sync_file() or in jbd2's journal_submit_commit_record().

- Ted

reviewer
#11 lore raw
Quoted 12 lines · Theodore Tso
Quoted 3 lines · Jan Kara

Yes, I noticed that yesterday as well. But then I was puzzled why ext4
would need the flush where it has it... sync_inode() has started and committed a transaction which issued a barrier when the commit was done.

You're right; I'm not convinced we need the flush in ext4 (or ext3) at all. We write the data blocks, *then* we call ext4_write_inode(), which will force a commit. Now, if we apply that patch which optimizes out commits if there are no dirty blocks, then we'll be trouble, because we won't know for sure whether or not ext4_write_inode() will have forced a journal commit.

Quoted 4 lines

If we optimize out the journal commit when there are no blocks attached to the transaction, we could change the patch to only force a flush if inode->i_state did not have I_DIRTY before the call to sync_inode(). Does that sound sane?

Yes. And also add a flush in case of fdatasync().

Quoted 9 lines
Quoted 4 lines

The only reason I could imagine is that barrier (although it is usually translated to flushing writeback caches) actually means just an ordering requirement and hence does not necessarily mean that the caches are properly flushed. Is that so Eric?

I'm not sure what you mean; if the barrier operation isn't flushing all of the caches all the way out to the iron oxide, it's not going to be working properly no matter where it is being called, whether it's in ext4_sync_file() or in jbd2's journal_submit_commit_record().

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

Honza

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

I'm not sure what you mean; if the barrier operation isn't flushing all of the caches all the way out to the iron oxide, it's not going to be working properly no matter where it is being called, whether it's in ext4_sync_file() or in jbd2's journal_submit_commit_record().

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

It also guarentees that when you get a completion for that barrier write, it's on safe storage. Think of it as a flush-write-flush operation, in the presence of write back caching.

Signature · 1 line
Jens Axboe
maintainer
#13 lore raw
Quoted 5 lines · Jens Axboe

It also guarentees that when you get a completion for that barrier write, it's on safe storage. Think of it as a flush-write-flush operation, in the presence of write back caching.

Is that true even if the barrier isn't attached to a write operation, i.e., when using

      blkdev_issue_flush(sb, NULL);

?

- Ted

#14 lore raw
Quoted 7 lines · Theodore Tso
Quoted 5 lines · Jens Axboe

It also guarentees that when you get a completion for that barrier write, it's on safe storage. Think of it as a flush-write-flush operation, in the presence of write back caching.

Quoted 6 lines

Is that true even if the barrier isn't attached to a write operation, i.e., when using

      blkdev_issue_flush(sb, NULL);

?

No, since there's no specific write to protect in that case. The above will flush existing writes to platter, but has no bearing on writes submitted later (other than the obvious effect that they will be on disk later then previously submitted writes). So for blkdev_issue_flush(), when that returns with good status, then the writes are safe. In the absence of the barrier flag on a write, completion of that write may just mean that the disk drive is aware of it, not that it is in any way safe.

The hardware may even treat the flush as a noop and do absolutely nothing, which is perfectly valid if it guarantees us that it will never lose a submitted write.

Signature · 1 line
Jens Axboe
#15 lore raw
In reply to Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 9 lines · Jan Kara
Quoted 4 lines

I'm not sure what you mean; if the barrier operation isn't flushing all of the caches all the way out to the iron oxide, it's not going to be working properly no matter where it is being called, whether it's in ext4_sync_file() or in jbd2's journal_submit_commit_record().

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

Quoted 3 lines

It also guarentees that when you get a completion for that barrier write, it's on safe storage. Think of it as a flush-write-flush operation, in the presence of write back caching.

(sorry for chiming in so late)

Jens, isn't this just the way it's implemented today? At some point couldn't a barrier bio simply be a reordering barrier that the storage can use when destaging the write cache, rather than the heavy-handed flush-write-flush we have today?

I guess it's a question of the intended semantics of a barrier bio, vs. today's implementation based on current hardware functionality...

-Eric

#16 lore raw
Quoted 18 lines · Eric Sandeen
Quoted 11 lines · Jens Axboe
Quoted 9 lines · Jan Kara
Quoted 4 lines

I'm not sure what you mean; if the barrier operation isn't flushing all of the caches all the way out to the iron oxide, it's not going to be working properly no matter where it is being called, whether it's in ext4_sync_file() or in jbd2's journal_submit_commit_record().

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

Quoted 3 lines

It also guarentees that when you get a completion for that barrier write, it's on safe storage. Think of it as a flush-write-flush operation, in the presence of write back caching.

(sorry for chiming in so late)

Quoted 7 lines

Jens, isn't this just the way it's implemented today? At some point couldn't a barrier bio simply be a reordering barrier that the storage can use when destaging the write cache, rather than the heavy-handed flush-write-flush we have today?

I guess it's a question of the intended semantics of a barrier bio, vs. today's implementation based on current hardware functionality...

Right, the current implementation is largely an artefact of what we can do with todays hardware. It would be quite reasonable to decouple the flush from the reordering, if we were able to support that on the hardware side.

Signature · 1 line
Jens Axboe
maintainer
#17 lore raw
In reply to Jan Kara
Quoted 7 lines · Jan Kara
Quoted 5 lines · Theodore Tso

If we optimize out the journal commit when there are no blocks attached to the transaction, we could change the patch to only force a flush if inode->i_state did not have I_DIRTY before the call to sync_inode(). Does that sound sane?

Yes. And also add a flush in case of fdatasync().

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Quoted 5 lines

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

Ah, yes, that's what you're getting at. True, but for better or for worse, we have no other interface other than blkdev_issue_flush(). This will guarantee that the data has made it to the disk controller, but it won't necessarily guarantee that it will have made it onto the disk platter, as I understand things; but I don't think we have any other interfaces available to us at this point.

- Ted

reviewer
#18 lore raw
Quoted 11 lines · Theodore Tso
Quoted 7 lines · Jan Kara
Quoted 5 lines · Theodore Tso

If we optimize out the journal commit when there are no blocks attached to the transaction, we could change the patch to only force a flush if inode->i_state did not have I_DIRTY before the call to sync_inode(). Does that sound sane?

Yes. And also add a flush in case of fdatasync().

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Maybe ext4-patch-queue changes that area but in Linus's tree I see:

  if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
         goto out;

So if we just overwrite some data, we send them to disk via fdatawrite()
and then we quickly bail out from ext4_sync_file() without doing blkdev_issue_flush().

Quoted 6 lines
Quoted 5 lines

Well, I thought that a barrier, as an abstraction, only guarantees that
any IO which happened before the barrier hits the iron before any IO which has been submitted after a barrier. This is actually enough for a journalling to work correctly but it's not enough for fsync() guarantees. But I might be wrong...

Quoted 6 lines

Ah, yes, that's what you're getting at. True, but for better or for worse, we have no other interface other than blkdev_issue_flush(). This will guarantee that the data has made it to the disk controller, but it won't necessarily guarantee that it will have made it onto the disk platter, as I understand things; but I don't think we have any other interfaces available to us at this point.

As Jens wrote, it seems barrier guarantees more than I thought so we are
correct.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#19 lore raw
Quoted 11 lines · Jan Kara
Quoted 2 lines

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Maybe ext4-patch-queue changes that area but in Linus's tree I see:

  if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
         goto out;

So if we just overwrite some data, we send them to disk via fdatawrite()
and then we quickly bail out from ext4_sync_file() without doing blkdev_issue_flush().

So you're thinking about fdatawrite() being called by some code path other than ext4_sync_file() before we call fsync()? Yeah, that could happen.... I think that will only happen if the file is opened O_SYNC, but that raises another issue, which is that we're not forcing a flush for writes when the file is opened O_SYNC.

- Ted

#20 lore raw
Quoted 13 lines · Theodore Tso
Quoted 11 lines · Jan Kara
Quoted 2 lines

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Maybe ext4-patch-queue changes that area but in Linus's tree I see:

  if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
         goto out;

So if we just overwrite some data, we send them to disk via fdatawrite()
and then we quickly bail out from ext4_sync_file() without doing blkdev_issue_flush().

Quoted 5 lines

So you're thinking about fdatawrite() being called by some code path other than ext4_sync_file() before we call fsync()? Yeah, that could happen.... I think that will only happen if the file is opened O_SYNC, but that raises another issue, which is that we're not forcing a flush for writes when the file is opened O_SYNC.

Hi Jan, Ted

Is something like the patch below what you had in mind?

--

From: Fernando Luis Vazquez Cao <[email protected]>
Subject: ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync or fdatasync, we should call blkdev_issue_flush if barriers are supported.

---

fs/ext4/fsync.c +19 -5
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -79,22 +80,35 @@ int ext4_sync_file(struct file *file, st
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		/*
+		 * When there are no blocks attached to the journal transaction
+		 * some optimizations are possible, but if there were dirty
+		 * pages sync_inode() should have ensured that all data gets
+		 * actually written to disk. Thus, we can skip
+		 * blkdev_issue_flush() below.
+		 */
+		if (!(i_state & I_DIRTY_PAGES))
+			goto flush_blkdev;
 	}
+
+	goto out;
+
+flush_blkdev:
+	if (journal && (journal->j_flags & JBD2_BARRIER))
+		blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 out:
 	return ret;
 }
reviewer
#21 lore raw
Quoted 66 lines · Fernando Luis Vázquez Cao
Quoted 13 lines · Theodore Tso
Quoted 11 lines · Jan Kara
Quoted 2 lines

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Maybe ext4-patch-queue changes that area but in Linus's tree I see:

  if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
         goto out;

So if we just overwrite some data, we send them to disk via fdatawrite()
and then we quickly bail out from ext4_sync_file() without doing blkdev_issue_flush().

Quoted 5 lines

So you're thinking about fdatawrite() being called by some code path other than ext4_sync_file() before we call fsync()? Yeah, that could happen.... I think that will only happen if the file is opened O_SYNC, but that raises another issue, which is that we're not forcing a flush for writes when the file is opened O_SYNC.

Hi Jan, Ted

Is something like the patch below what you had in mind?

--

From: Fernando Luis Vazquez Cao <[email protected]>
Subject: ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync or fdatasync, we should call blkdev_issue_flush if barriers are supported.

---

fs/ext4/fsync.c +4 -4
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -79,22 +80,35 @@ int ext4_sync_file(struct file *file, st
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 104

Quoted 10 lines
fs/ext4/fsync.c +9 -1
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		/*
+		 * When there are no blocks attached to the journal transaction
+		 * some optimizations are possible, but if there were dirty
+		 * pages sync_inode() should have ensured that all data gets
+		 * actually written to disk. Thus, we can skip
+		 * blkdev_issue_flush() below.
+		 */
+		if (!(i_state & I_DIRTY_PAGES))
+			goto flush_blkdev;

Uh. Here I don't get it. When we did sync_inode(), blkdev_issue_flush()
is needed only if the journal does not do barriers. So I'd expect here:

	if (!(journal->j_flags & JBD2_BARRIER))
		goto flush_blkdev;
	goto out;

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 107

Quoted 3 lines
fs/ext4/fsync.c +2 -0
 	}
+
+	goto out;

But here we might need to issue a flush if there are some data written. So
I'd have here:

	if (!(i_state & I_DIRTY_PAGES))
		goto out;

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 114

Quoted 7 lines
fs/ext4/fsync.c +4 -0
+
+flush_blkdev:
+	if (journal && (journal->j_flags & JBD2_BARRIER))
+		blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 out:
 	return ret;
 }

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#22 lore raw
Quoted 79 lines · Jan Kara
Quoted 66 lines · Fernando Luis Vázquez Cao
Quoted 13 lines · Theodore Tso
Quoted 11 lines · Jan Kara
Quoted 2 lines

Um, we have that already; the sync_inode() followed by blkdev_issue_flush() is the path taken by fdatasync(), I do believe.

Maybe ext4-patch-queue changes that area but in Linus's tree I see:

  if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
         goto out;

So if we just overwrite some data, we send them to disk via fdatawrite()
and then we quickly bail out from ext4_sync_file() without doing blkdev_issue_flush().

Quoted 5 lines

So you're thinking about fdatawrite() being called by some code path other than ext4_sync_file() before we call fsync()? Yeah, that could happen.... I think that will only happen if the file is opened O_SYNC, but that raises another issue, which is that we're not forcing a flush for writes when the file is opened O_SYNC.

Hi Jan, Ted

Is something like the patch below what you had in mind?

--

From: Fernando Luis Vazquez Cao <[email protected]>
Subject: ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync or fdatasync, we should call blkdev_issue_flush if barriers are supported.

---

fs/ext4/fsync.c +4 -4
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -79,22 +80,35 @@ int ext4_sync_file(struct file *file, st
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 104

Quoted 10 lines
fs/ext4/fsync.c +9 -1
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		/*
+		 * When there are no blocks attached to the journal transaction
+		 * some optimizations are possible, but if there were dirty
+		 * pages sync_inode() should have ensured that all data gets
+		 * actually written to disk. Thus, we can skip
+		 * blkdev_issue_flush() below.
+		 */
+		if (!(i_state & I_DIRTY_PAGES))
+			goto flush_blkdev;

Uh. Here I don't get it. When we did sync_inode(), blkdev_issue_flush()
is needed only if the journal does not do barriers. So I'd expect here:

Quoted 3 lines
	if (!(journal->j_flags & JBD2_BARRIER))
		goto flush_blkdev;
	goto out;

Ups, you are right. I somehow managed to mangle the logic that I intended to put here and under flush_blkdev. By the way, I think that the same check may be needed for the data==journal case too.

Thank you for the feedback, Jan.

I'll be replying to this email with new patches for ext2/ext3.

- Fernando

#23 lore raw

ext3: call blkdev_issue_flush on fsync

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext3/fsync.c +16 -3
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 	}
+
+flush_blkdev:
+	if (!(i_state & I_DIRTY_PAGES))
+		goto out;
+no_journal_barrier:
+	blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 out:
 	return ret;
 }
reviewer
#24 lore raw

Re: ext3: call blkdev_issue_flush on fsync

Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 36 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 10 lines
fs/ext3/fsync.c +3 -1
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.
And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 104

Quoted 10 lines
fs/ext3/fsync.c +6 -0
 	}
+
+flush_blkdev:
+	if (!(i_state & I_DIRTY_PAGES))
+		goto out;
+no_journal_barrier:
+	blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 out:
 	return ret;
 }

Honza

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

Re: ext3: call blkdev_issue_flush on fsync

Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

I'll try to check whether that is always so just in case.

Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1". What are your thoughts on this?

#26 lore raw

Re: ext3: call blkdev_issue_flush on fsync

Quoted 59 lines · Fernando Luis Vázquez Cao
Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

Quoted 8 lines

I'll try to check whether that is always so just in case.

Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1".

The last phrase should read " do not force a flush when "barrier=0" ".

Sorry for the noise.

- Fernando

reviewer
#27 lore raw

Re: ext3: call blkdev_issue_flush on fsync

Quoted 62 lines · Fernando Luis Vázquez Cao
Quoted 61 lines · Fernando Luis Vázquez Cao
Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

I'll try to check whether that is always so just in case.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1".

The last phrase should read " do not force a flush when "barrier=0" ".

I was hesitating about this a bit. But I don't think so. The reason is
that POSIX (or any other reasonable specification) mandates that fsync() should return only after the data is safely on storage. So if we don't flush blockdevice caches, we effectively violate POSIX and we should never do that. With barriers the matter is a bit different - that is just a filesystem specific thing, no standard guarantees anything.

Honza

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

Re: ext3: call blkdev_issue_flush on fsync

Quoted 70 lines · Jan Kara
Quoted 69 lines · Fernando Luis Vázquez Cao
Quoted 67 lines · Fernando Luis Vázquez Cao
Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

I'll try to check whether that is always so just in case.

Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1".

Quoted 7 lines
Quoted 1 line

The last phrase should read " do not force a flush when "barrier=0" ".

I was hesitating about this a bit. But I don't think so. The reason is
that POSIX (or any other reasonable specification) mandates that fsync() should return only after the data is safely on storage. So if we don't flush blockdevice caches, we effectively violate POSIX and we should never do that. With barriers the matter is a bit different - that is just a filesystem specific thing, no standard guarantees anything.

Hi Jan,

Sorry it's taken me so long to get back to you.

Thinking a lit bit more about this issue, it occurred to me that adding a new mount option à la existing "barrier" is likely to be preferable. As an example where such an option could make sense, let's consider a system with battery-backup cache devices. Since the battery-backup guarantees the data still not committed to the platter will not vanish in the event of a power down, it should be possible to obtain a performance gain by optimizing out the device flush on every fsync()/fdatasync() call.

If there is consensus on the propriety of this approach, I will send updated patches.

Regards,

Fernando

reviewer
#29 lore raw

Re: ext3: call blkdev_issue_flush on fsync

Quoted 83 lines · Fernando Luis Vázquez Cao
Quoted 70 lines · Jan Kara
Quoted 69 lines · Fernando Luis Vázquez Cao
Quoted 67 lines · Fernando Luis Vázquez Cao
Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

I'll try to check whether that is always so just in case.

Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1".

Quoted 7 lines
Quoted 1 line

The last phrase should read " do not force a flush when "barrier=0" ".

I was hesitating about this a bit. But I don't think so. The reason is
that POSIX (or any other reasonable specification) mandates that fsync() should return only after the data is safely on storage. So if we don't flush blockdevice caches, we effectively violate POSIX and we should never do that. With barriers the matter is a bit different - that is just a filesystem specific thing, no standard guarantees anything.

Hi Jan,

Sorry it's taken me so long to get back to you.

Quoted 8 lines

Thinking a lit bit more about this issue, it occurred to me that adding a new mount option à la existing "barrier" is likely to be preferable. As an example where such an option could make sense, let's consider a system with battery-backup cache devices. Since the battery-backup guarantees the data still not committed to the platter will not vanish in the event of a power down, it should be possible to obtain a performance gain by optimizing out the device flush on every fsync()/fdatasync() call.

Yes, I came to this conclusion as well when we were discussing how to fix
other filesystems which fail to issue flush in fsync
(https://kerneltrap.org/mailarchive/linux-ext4/2009/1/22/4788004/thread).
I think it makes sence there's a general mount option recognized at VFS level which would set superblock flag whether flush on fsync is needed or not. Then you could use this flag in your patch.
So if you have time, please write a (separate) patch introducing this
generic option. Thanks.

Honza

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

Re: ext3: call blkdev_issue_flush on fsync

Quoted 92 lines · Jan Kara
Quoted 83 lines · Fernando Luis Vázquez Cao
Quoted 70 lines · Jan Kara
Quoted 69 lines · Fernando Luis Vázquez Cao
Quoted 67 lines · Fernando Luis Vázquez Cao
Quoted 43 lines · Jan Kara
Quoted 7 lines · Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

Only two minor nits:

Quoted 33 lines
fs/ext3/fsync.c +7 -2
@@ -27,6 +27,7 @@
 #include <linux/sched.h>
 #include <linux/writeback.h>
 #include <linux/jbd.h>
+#include <linux/blkdev.h>
 #include <linux/ext3_fs.h>
 #include <linux/ext3_jbd.h>
 
@@ -45,6 +46,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +72,33 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
Quoted 14 lines

fs/ext3/fsync.c int ext3_sync_file(struct file * file, s · line 94

Quoted 13 lines
fs/ext3/fsync.c +3 -1
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;

I cannot imagine "journal" will be NULL here.

I'll try to check whether that is always so just in case.

Quoted 2 lines

And we can also optimize here a bit and do "goto out" because here
we know the barrier has been issued.

Yep, I was considering the same optimization. By the way, I was wondering if we should honor ext3 and ext4's "barrier" mount option for sys_fsync()/sys_fdatasync() and do not force a flush when "barrier=1".

Quoted 7 lines
Quoted 1 line

The last phrase should read " do not force a flush when "barrier=0" ".

I was hesitating about this a bit. But I don't think so. The reason is
that POSIX (or any other reasonable specification) mandates that fsync() should return only after the data is safely on storage. So if we don't flush blockdevice caches, we effectively violate POSIX and we should never do that. With barriers the matter is a bit different - that is just a filesystem specific thing, no standard guarantees anything.

Hi Jan,

Sorry it's taken me so long to get back to you.

Quoted 8 lines

Thinking a lit bit more about this issue, it occurred to me that adding a new mount option à la existing "barrier" is likely to be preferable. As an example where such an option could make sense, let's consider a system with battery-backup cache devices. Since the battery-backup guarantees the data still not committed to the platter will not vanish in the event of a power down, it should be possible to obtain a performance gain by optimizing out the device flush on every fsync()/fdatasync() call.

Quoted 8 lines

Yes, I came to this conclusion as well when we were discussing how to fix
other filesystems which fail to issue flush in fsync
(https://kerneltrap.org/mailarchive/linux-ext4/2009/1/22/4788004/thread).
I think it makes sence there's a general mount option recognized at VFS level which would set superblock flag whether flush on fsync is needed or not. Then you could use this flag in your patch.
So if you have time, please write a (separate) patch introducing this
generic option. Thanks.

Sorry it's taken me so long to get back at you, but I got distracted with some high priority stuff. I finally got around writing the patches for both the kernel and util-linux which I am sending as a reply to this email. Can you tell me if this is what you had in mind?

Regards,

Fernando

#31 lore raw

vfs: Improve readability off mount flag definitins by using offsets

Currently mount flags are defined using both decimal values and offsets which is kind of confusing. Define all flags using offsets for the sake of readability.

---

include/linux/fs.h +15 -15
diff -urNp linux-2.6.29-rc3-orig/include/linux/fs.h linux-2.6.29-rc3/include/linux/fs.h
@@ -111,22 +111,22 @@ struct inodes_stat_t {
 /*
  * These are the fs-independent mount-flags: up to 32 flags are supported
  */
-#define MS_RDONLY	 1	/* Mount read-only */
-#define MS_NOSUID	 2	/* Ignore suid and sgid bits */
-#define MS_NODEV	 4	/* Disallow access to device special files */
-#define MS_NOEXEC	 8	/* Disallow program execution */
-#define MS_SYNCHRONOUS	16	/* Writes are synced at once */
-#define MS_REMOUNT	32	/* Alter flags of a mounted FS */
-#define MS_MANDLOCK	64	/* Allow mandatory locks on an FS */
-#define MS_DIRSYNC	128	/* Directory modifications are synchronous */
-#define MS_NOATIME	1024	/* Do not update access times. */
-#define MS_NODIRATIME	2048	/* Do not update directory access times */
-#define MS_BIND		4096
-#define MS_MOVE		8192
-#define MS_REC		16384
-#define MS_VERBOSE	32768	/* War is peace. Verbosity is silence.
+#define MS_RDONLY	(1<<0)	/* Mount read-only */
+#define MS_NOSUID	(1<<1)	/* Ignore suid and sgid bits */
+#define MS_NODEV	(1<<2)	/* Disallow access to device special files */
+#define MS_NOEXEC	(1<<3)	/* Disallow program execution */
+#define MS_SYNCHRONOUS	(1<<4)	/* Writes are synced at once */
+#define MS_REMOUNT	(1<<5)	/* Alter flags of a mounted FS */
+#define MS_MANDLOCK	(1<<6)	/* Allow mandatory locks on an FS */
+#define MS_DIRSYNC	(1<<7)	/* Directory modifications are synchronous */
+#define MS_NOATIME	(1<<10)	/* Do not update access times. */
+#define MS_NODIRATIME	(1<<11)	/* Do not update directory access times */
+#define MS_BIND		(1<<12)
+#define MS_MOVE		(1<<13)
+#define MS_REC		(1<<14)
+#define MS_VERBOSE	(1<<15)	/* War is peace. Verbosity is silence.
 				   MS_VERBOSE is deprecated. */
-#define MS_SILENT	32768
+#define MS_SILENT	(1<<15)
 #define MS_POSIXACL	(1<<16)	/* VFS does not apply the umask */
 #define MS_UNBINDABLE	(1<<17)	/* change to unbindable */
 #define MS_PRIVATE	(1<<18)	/* change to private */
#32 lore raw

vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

fs/namespace.c +2 -2
diff -urNp linux-2.6.29-rc3-orig/fs/namespace.c linux-2.6.29-rc3/fs/namespace.c
@@ -1933,8 +1933,8 @@ long do_mount(char *dev_name, char *dir_
 	if (flags & MS_RDONLY)
 		mnt_flags |= MNT_READONLY;
 
-	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
-		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
+	flags &= ~(MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME |
+		   MS_NODIRATIME | MS_RELATIME | MS_ACTIVE | MS_KERNMOUNT);
 
 	/* ... and get the mountpoint */
 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
include/linux/fs.h +2 -0
diff -urNp linux-2.6.29-rc3-orig/include/linux/fs.h linux-2.6.29-rc3/include/linux/fs.h
@@ -135,6 +135,8 @@ struct inodes_stat_t {
 #define MS_RELATIME	(1<<21)	/* Update atime relative to mtime/ctime. */
 #define MS_KERNMOUNT	(1<<22) /* this is a kern_mount call */
 #define MS_I_VERSION	(1<<23) /* Update inode I_version field */
+#define MS_FLUSHONFSYNC (1<<24)	/* Force block device flush on
+				   fsync()/fdatasync() */
 #define MS_ACTIVE	(1<<30)
 #define MS_NOUSER	(1<<31)
 
#33 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

Somewhere in the middle, if an administrator knows they have a UPS they trust and hardware that stays connected to it, they could tune the bdev to ignore these flush requests.

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

Thoughts?

Thanks,
-Eric

Quoted 28 lines

diff -urNp linux-2.6.29-rc3-orig/fs/namespace.c linux-2.6.29-rc3/fs/namespace.c

fs/namespace.c +2 -2
@@ -1933,8 +1933,8 @@ long do_mount(char *dev_name, char *dir_
 	if (flags & MS_RDONLY)
 		mnt_flags |= MNT_READONLY;
 
-	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
-		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
+	flags &= ~(MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME |
+		   MS_NODIRATIME | MS_RELATIME | MS_ACTIVE | MS_KERNMOUNT);
 
 	/* ... and get the mountpoint */
 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);

diff -urNp linux-2.6.29-rc3-orig/include/linux/fs.h linux-2.6.29-rc3/include/linux/fs.h

include/linux/fs.h +2 -0
@@ -135,6 +135,8 @@ struct inodes_stat_t {
 #define MS_RELATIME	(1<<21)	/* Update atime relative to mtime/ctime. */
 #define MS_KERNMOUNT	(1<<22) /* this is a kern_mount call */
 #define MS_I_VERSION	(1<<23) /* Update inode I_version field */
+#define MS_FLUSHONFSYNC (1<<24)	/* Force block device flush on
+				   fsync()/fdatasync() */
 #define MS_ACTIVE	(1<<30)
 #define MS_NOUSER	(1<<31)
 
#34 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 9 lines · Eric Sandeen
Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

Quoted 8 lines

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

That would certainly be my preference -- turn this ON by default, and them if a layer NEEDS to ignore it, it can.

Jeff

#35 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 5 lines · Jeff Garzik
Quoted 2 lines

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

That would certainly be my preference -- turn this ON by default, and them if a layer NEEDS to ignore it, it can.

Yeah, and we should integrate this with the barriers settings.

I think the right setup is:

- each gendisk has a variable to indicate if we have a write-back cache, which is filled from scsi inquiry data (or whatever the equivalent in the storage protocol is), but we allow an override from userspace if the admin knows better (if he really does or wants to play fast and lose is the admin's business)
- filesystems do the right things by using barriers and cache flushes if they see the underlying device needs it.

#36 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Jeff Garzik
Quoted 2 lines

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

That would certainly be my preference -- turn this ON by default, and them if a layer NEEDS to ignore it, it can.

Yeah, and we should integrate this with the barriers settings.

I think the right setup is:

Quoted 7 lines

- each gendisk has a variable to indicate if we have a write-back cache, which is filled from scsi inquiry data (or whatever the equivalent in the storage protocol is), but we allow an override from userspace if the admin knows better (if he really does or wants to play fast and lose is the admin's business)
- filesystems do the right things by using barriers and cache flushes if they see the underlying device needs it.

That makes sense, but the contentious issue seems to be whether the override from userspace you mention should take the form of mount option or per-block device sysfs tunable instead. Making this override (flushonfsync in my patches) be a mount option would be consistent with what filesystems such as ext3/4 and xfs do when it comes to barriers, but, if there is consensus, I would not mind turning it into a per-device tunable instead.

You mentioned "we should integrate this with the barrier settings". Do you imply we should make it a per-device tunable too? Should we keep the barrier-related mount options some filesystems provide?

- Fernando

maintainer
#37 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 4 lines · Fernando Luis Vázquez Cao

You mentioned "we should integrate this with the barrier settings". Do you imply we should make it a per-device tunable too? Should we keep the barrier-related mount options some filesystems provide?

Making barriers to be a per-device tunable makes sense. The only reason why we kept it as a mount option in ext4 is for benchmarking purposes, and in ext3, because the filesystem predated the barrier code, and there was a desire to be able to benchmark with and without the old behavior --- and because akpm is still worried about the performance hit of the barrier code, so he's been resistant about change the default for ext3.

- Ted

#38 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 14 lines · Theodore Tso
Quoted 4 lines · Fernando Luis Vázquez Cao

You mentioned "we should integrate this with the barrier settings". Do you imply we should make it a per-device tunable too? Should we keep the barrier-related mount options some filesystems provide?

Making barriers to be a per-device tunable makes sense. The only reason why we kept it as a mount option in ext4 is for benchmarking purposes, and in ext3, because the filesystem predated the barrier code, and there was a desire to be able to benchmark with and without the old behavior --- and because akpm is still worried about the performance hit of the barrier code, so he's been resistant about change the default for ext3.

Quoted 1 line

- Ted

I agree that making barriers a per-device tunable probably does make the most sense. And talking w/ Dave, he points out that if it were this way, the fs could just issue barrier IOs at every appropriate place, and the bdev would just ignore them if they got tuned off, instead of the current fs world of trying a barrier, always watching out for -EOPNOTSUPP coming back up, then disabling barriers, etc.

-Eric

#39 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Theodore Tso
Quoted 6 lines · Theodore Tso
Quoted 4 lines · Fernando Luis Vázquez Cao

You mentioned "we should integrate this with the barrier settings". Do you imply we should make it a per-device tunable too? Should we keep the barrier-related mount options some filesystems provide?

Quoted 7 lines

Making barriers to be a per-device tunable makes sense. The only reason why we kept it as a mount option in ext4 is for benchmarking purposes, and in ext3, because the filesystem predated the barrier code, and there was a desire to be able to benchmark with and without the old behavior --- and because akpm is still worried about the performance hit of the barrier code, so he's been resistant about change the default for ext3.

Ok, I'll turn both barriers and flushonfsync into a sysfs-exported per-device knob and see how it turns out.

By the way, should we also add/keep a mount option for "benchmarking purposes"?. I guess that once we get the per-device tunable we probable do not need it anymore.

Regards,

Fernando

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

reviewer
#40 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Eric Sandeen
Quoted 11 lines · Eric Sandeen
Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

Hum, that's an interesting idea. Yes, probably it makes even more sence
than a global mount option.

Quoted 6 lines

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

Quoted 7 lines

Somewhere in the middle, if an administrator knows they have a UPS they trust and hardware that stays connected to it, they could tune the bdev to ignore these flush requests.

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

Honza

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

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 1 line · Jan Kara

On Thu 12-02-09 11:13:37, Eric Sandeen wrote:

...

Quoted 9 lines
Quoted 3 lines

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

Honza

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

But do we really want to go out of our way to enable people to short-circuit data integrity paths and then file bugs when their files go missing? :)

(I guess the blockdev tunable is similarly dangerous, but it more clearly meets the explicit need (writecache-safe devices))

-Eric

#42 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 15 lines · Eric Sandeen
Quoted 1 line · Jan Kara

On Thu 12-02-09 11:13:37, Eric Sandeen wrote:

...

Quoted 9 lines
Quoted 3 lines

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

Honza

Quoted 8 lines

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

But do we really want to go out of our way to enable people to short-circuit data integrity paths and then file bugs when their files go missing? :)

Well, it is just a matter of using safe defaults. IMHO, a scenario where the administrator wants to optimize writes to a certain partition and _explicitly_ clears MS_FLUSHONFSYNC on that superblock is not completely unreasonable.

Quoted 2 lines

(I guess the blockdev tunable is similarly dangerous, but it more clearly meets the explicit need (writecache-safe devices))

If distributions use sane defaults and we document the mount option or bdev tunable properly I guess it might make sense to allow system administrators to shoot themselves in the foot.

(By the way, in this patch-set a patch for mount(8) is included.)

- Fernando

#43 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 19 lines · Fernando Luis Vázquez Cao
Quoted 18 lines · Eric Sandeen
Quoted 1 line · Jan Kara

On Thu 12-02-09 11:13:37, Eric Sandeen wrote:

...

Quoted 9 lines
Quoted 3 lines

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

Honza

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

Quoted 8 lines
Quoted 3 lines

But do we really want to go out of our way to enable people to short-circuit data integrity paths and then file bugs when their files go missing? :)

Well, it is just a matter of using safe defaults. IMHO, a scenario where the administrator wants to optimize writes to a certain partition and _explicitly_ clears MS_FLUSHONFSYNC on that superblock is not completely unreasonable.

One case is "this device is safe with a write cache and flush is not necessary for data consistency" - that's the per-bdev setting.

The other case is "I don't really care and I just want to go faster" - that's the per-mount setting.

I'd be much more likely to support the first case, as it's needed for maximum performance w/o sacrificing correctness, when properly used.

The latter case is really only for cutting corners and giving people more rope than they need to hang themselves.

Quoted 8 lines
Quoted 2 lines

(I guess the blockdev tunable is similarly dangerous, but it more clearly meets the explicit need (writecache-safe devices))

If distributions use sane defaults and we document the mount option or bdev tunable properly I guess it might make sense to allow system administrators to shoot themselves in the foot.

(By the way, in this patch-set a patch for mount(8) is included.)

... one of the advantages of making it a bdev tunable is that you don't have to mess with mount(8) :)

-Eric

maintainer
#44 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Eric Sandeen
Quoted 10 lines · Eric Sandeen
Quoted 5 lines

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

Actually, Bart Samwel at FOSDEM talked to me and asked for something similar --- what we came up which meant his request while still being standards-compliant was a per-process personality flag which had three options:

     *)  Always honor fsync() calls (the default)
     *)  Never honor fsync() calls
     *)  Only honor fsync() calls if a global "honor fsync" flag

(which would be manipulated by the laptop mode scripts) is set.

The flag would be reset to the default across a setuid exec, but would otherwise be inherited across fork()'s. It might be possible to set/get the flag via a /proc interface.

The basic idea is that laptop systems where the system administrator wants longer battery life (and trusts the battery not to suddenly give out) more than they care about fsync() guarantees can set up a pam library which sets the flag for at login time so that all of the user's processes can be set up not to honor fsync() calls; however, all of the system daemons would still function normally.

- Ted

#45 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 27 lines · Theodore Tso
Quoted 10 lines · Eric Sandeen
Quoted 5 lines

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

Actually, Bart Samwel at FOSDEM talked to me and asked for something similar --- what we came up which meant his request while still being standards-compliant was a per-process personality flag which had three options:

     *)  Always honor fsync() calls (the default)
     *)  Never honor fsync() calls
     *)  Only honor fsync() calls if a global "honor fsync" flag

(which would be manipulated by the laptop mode scripts) is set.

The flag would be reset to the default across a setuid exec, but would otherwise be inherited across fork()'s. It might be possible to set/get the flag via a /proc interface.

Quoted 6 lines

The basic idea is that laptop systems where the system administrator wants longer battery life (and trusts the battery not to suddenly give out) more than they care about fsync() guarantees can set up a pam library which sets the flag for at login time so that all of the user's processes can be set up not to honor fsync() calls; however, all of the system daemons would still function normally.

Sounds like posix violation to
me... '/sys/fsync_does_not_really_sync'?

Perhaps it is better done at glibc level? Environment variables already mostly have semantics you want.....

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#46 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 33 lines · Pavel Machek
Quoted 25 lines · Theodore Tso
Quoted 9 lines · Eric Sandeen
Quoted 5 lines

Yes, but OTOH we should give sysadmin a possibility to enable / disable
it on just some partitions. I don't see a reasonable use for that but people tend to do strange things ;) and here isn't probably a strong reason to not allow them.

But nobody has asked for that, have they? So why offer it up a this point?

They could use LD_PRELOAD to make fsync a no-op if they really don't care for it, I guess... though that's not easily per-fs either.

Actually, Bart Samwel at FOSDEM talked to me and asked for something similar --- what we came up which meant his request while still being standards-compliant was a per-process personality flag which had three options:

     *)  Always honor fsync() calls (the default)
     *)  Never honor fsync() calls
     *)  Only honor fsync() calls if a global "honor fsync" flag

(which would be manipulated by the laptop mode scripts) is set.

The flag would be reset to the default across a setuid exec, but would otherwise be inherited across fork()'s. It might be possible to set/get the flag via a /proc interface.

Quoted 6 lines

The basic idea is that laptop systems where the system administrator wants longer battery life (and trusts the battery not to suddenly give out) more than they care about fsync() guarantees can set up a pam library which sets the flag for at login time so that all of the user's processes can be set up not to honor fsync() calls; however, all of the system daemons would still function normally.

Quoted 7 lines

Sounds like posix violation to
me... '/sys/fsync_does_not_really_sync'?

Perhaps it is better done at glibc level? Environment variables already mostly have semantics you want.....

Pavel

One other thing that may be worth bringing up (just to muddy the waters more) is OSX's handling of this stuff.

Quoted 15 lines

From the fsync(2) manpage:

Note that while fsync() will flush all data from the host to the drive (i.e. the "permanent storage device"), the drive itself may not physically write the data to the platters for quite some time and it may be written in an out-of-order sequence.

Specifically, if the drive loses power or the OS crashes, the appli- cation may find that only some or none of their data was written. The disk drive may also re-order the data so that later writes may be present, while earlier writes are not.

This is not a theoretical edge case. This scenario is easily repro- duced with real world workloads and drive power failures.

Quoted 7 lines

For applications that require tighter guarantees about the integrity of their data, Mac OS X provides the F_FULLFSYNC fcntl. The F_FULLF- SYNC fcntl asks the drive to flush all buffered data to permanent storage. Applications, such as databases, that require a strict ordering of writes should use F_FULLFSYNC to ensure that their data is written in the order they expect. Please see fcntl(2) for more detail.

and from fcntl(2)

Quoted 8 lines

F_FULLFSYNC Does the same thing as fsync(2) then asks the drive to
flush all buffered data to the permanent storage device (arg is ignored). This is currently imple- mented on HFS, MS-DOS (FAT), and Universal Disk Format (UDF) file systems. The operation may take quite a while to complete. Certain FireWire drives have also been known to ignore the request to flush their buffered data.

-Eric

maintainer
#47 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Pavel Machek
Quoted 3 lines · Pavel Machek

Sounds like posix violation to
me... '/sys/fsync_does_not_really_sync'?

It's as much a Posix violation as noatime. :-)

Quoted 2 lines

Perhaps it is better done at glibc level? Environment variables already mostly have semantics you want.....

Environment variables own't allow us to switch fsync's on and off while the process is running, so that the system can run "safe" while it is on AC mains, but "low power" when on battery.

- Ted

#48 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 4 lines · Theodore Tso
Quoted 2 lines · Pavel Machek

Sounds like posix violation to
me... '/sys/fsync_does_not_really_sync'?

Quoted 8 lines

It's as much a Posix violation as noatime. :-)

Quoted 2 lines

Perhaps it is better done at glibc level? Environment variables already mostly have semantics you want.....

Environment variables own't allow us to switch fsync's on and off while the process is running, so that the system can run "safe" while it is on AC mains, but "low power" when on battery.

Overall, for each filesystem, we really should _default_ to a safe mode where fsync(2), fdatasync(2), sync_file_range(2) and transaction commits are guaranteed to be committed to stable storage -- i.e. SYNC CACHE / FLUSH CACHE, a forced unit access (FUA) bit, or guarantee that the underlying storage has disabled write-back caching.

Correctness should come before performance. Linux has not had credibility here, in the ATA+ext[23] space at least, and it is embarrassing.

Modern SATA and SCSI disks can do tagged command queueing with the FUA bit, which does a lot to mitigate the performance loss seen with a less capable "FLUSH CACHE + barrier" setup.

Jeff

#49 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 1 line · Jeff Garzik

Correctness should come before performance. Linux has not had credibility here, in the ATA+ext[23] space at least, and it is embarrassing.

To be more clear / precise, this means actually performing the guarantees we claim to the user. For example,

fsync(2) on ext2 should trigger a storage device writeback cache flush [or equivalent guarantee via FUA].

fsync(2) or journal commit on ext3 should trigger a flush [or equivalent guarantee via FUA].

Though, certainly, the user should be able to disable this strict behavior and trade correctness for performance.

Jeff

maintainer
#50 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 4 lines · Jeff Garzik

To be more clear / precise, this means actually performing the guarantees we claim to the user. For example,

Quoted 8 lines

fsync(2) on ext2 should trigger a storage device writeback cache flush [or equivalent guarantee via FUA].

fsync(2) or journal commit on ext3 should trigger a flush [or equivalent guarantee via FUA].

Though, certainly, the user should be able to disable this strict behavior and trade correctness for performance.

No argument here. The *default* should be that we respect fsync(). However, the user should be able to configure with a reasonable amount of granularity tradeoffs between safety and performance and/or battery usage.

- Ted

#51 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Eric Sandeen
Quoted 18 lines · Eric Sandeen
Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

Quoted 7 lines

Somewhere in the middle, if an administrator knows they have a UPS they trust and hardware that stays connected to it, they could tune the bdev to ignore these flush requests.

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

The main reason I decided to go for the mount option approach is to be consistent with what we do when it comes to write barriers. Treating one as a mount option and the other as a (possibly) sysfs tunable property seems a bit confusing to me.

Do you suggest using sysfs tunables instead?

- Fernando

#52 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 22 lines · Fernando Luis Vázquez Cao
Quoted 21 lines · Eric Sandeen
Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

Somewhere in the middle, if an administrator knows they have a UPS they trust and hardware that stays connected to it, they could tune the bdev to ignore these flush requests.

Quoted 8 lines
Quoted 3 lines

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

The main reason I decided to go for the mount option approach is to be consistent with what we do when it comes to write barriers. Treating one as a mount option and the other as a (possibly) sysfs tunable property seems a bit confusing to me.

well... technically, I think barriers really *should* mean "don't reorder these writes, I need them this way for consistency" - and that is really specific to the fs implementation, isn't it? (we just happen to implement them as cache flushes) and so that is a per-fs setting, I think.

Maybe there is no good argument for ignoring barriers on one fs, and implementing them on another, other than playing fast & loose & dangerous.... hrm.

Quoted 1 line

Do you suggest using sysfs tunables instead?

For a per-bdev flush setting, yes...

I guess I'll have to try to convince myself one way or another whether barrier mount options are consistent with this view. :)

I guess sometimes you do have workloads where you simply want speed, and on a crash you start over. In this case you don't care about barriers (ordering constraints - if you don't care about fs integrity if fsck or re-mkfs is ok) or flushing (caches - if you don't care about data integrity, you regenerate your results). That could vary from fs to fs....

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

-Eric

Quoted 2 lines

- Fernando

#53 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 42 lines · Eric Sandeen
Quoted 22 lines · Fernando Luis Vázquez Cao
Quoted 21 lines · Eric Sandeen
Quoted 5 lines · Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

Again, apologies for chiming in late.

But wouldn't it be better to make this a block device property rather than a new filesystem mount option?

That way the filesystem can always do "the right thing" and call the blkdev flush on fsync.

The block device *could* choose to ignore this in hardware if it knows it's built with a nonvolatile write cache or if it has no write cache.

Somewhere in the middle, if an administrator knows they have a UPS they trust and hardware that stays connected to it, they could tune the bdev to ignore these flush requests.

Quoted 8 lines
Quoted 3 lines

Also that way if you have 8 partitions on a battery-backed blockdev, you can tune it once, instead of needing to mount all 8 filesystems with the new option.

The main reason I decided to go for the mount option approach is to be consistent with what we do when it comes to write barriers. Treating one as a mount option and the other as a (possibly) sysfs tunable property seems a bit confusing to me.

well... technically, I think barriers really *should* mean "don't reorder these writes, I need them this way for consistency" - and that is really specific to the fs implementation, isn't it? (we just happen to implement them as cache flushes) and so that is a per-fs setting, I think.

Maybe there is no good argument for ignoring barriers on one fs, and implementing them on another, other than playing fast & loose & dangerous.... hrm.

Quoted 6 lines
Quoted 1 line

Do you suggest using sysfs tunables instead?

For a per-bdev flush setting, yes...

I guess I'll have to try to convince myself one way or another whether barrier mount options are consistent with this view. :)

I went through the same process :), and finally concluded that in both cases it all comes down a trade-off between between integrity (be it filesystem integrity or data integrity) and speed. Since the desired behavior could vary from filesystem to filesystem and for the sake of consistency with barriers the mount option approach seemed to make more sense.

Quoted 8 lines

I guess sometimes you do have workloads where you simply want speed, and on a crash you start over. In this case you don't care about barriers (ordering constraints - if you don't care about fs integrity if fsck or re-mkfs is ok) or flushing (caches - if you don't care about data integrity, you regenerate your results). That could vary from fs to fs....

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

If we can have distros make "barrier=1,flushonfsync" be the default setting and document these two mount options properly by explicitly indicating the dangers of deviating from these defaults, I think we'll be heading in the right direction.

By the way, apart from the issue of whether flushonsync should be a mount option or sysfs tunable is there any other issue with the patches? Jan, have I addressed all your concerns?

Regards,

Fernando

#54 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Eric Sandeen
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

Quite frankly, the VFS should do stuff that is slow and safe and filesystems can choose to ignore the VFS (via filesystem specific mount options) if they want to be fast and potentially unsafe.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#55 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 8 lines · Dave Chinner
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

That is precisely the reason why we should use sensible defaults, which in this case means enabling barriers and flushing disk caches on fsync()/fdatasync() by default.

Adding either a new mount option (as you yourself suggest below) or a sysfs tunable is desirable for those cases when we really do not need to flush the disk write cache to guarantee integrity (battery-backed block devices come to mind), or we want to be fast at the cost of potentially losing some data.

Quoted 4 lines

Quite frankly, the VFS should do stuff that is slow and safe and filesystems can choose to ignore the VFS (via filesystem specific mount options) if they want to be fast and potentially unsafe.

To avoid unnecessary flushes and allow for filesystem-specific optimizations I was considering the following approach:

1- Add flushonfsync mount option (as an aside, I am of the opinion that it should be set by default).
2- Modify file_fsync() so that it checks whether FLUSHONFSYNC is set and flushes the underlying device accordingly. With this we would cover all filesystems that use the vfs-provided file_fsync() as their fsync method (commonly used filesystems such as fat fall in this group). 3- Advanced filesystems (ext3/4, XFS, btrfs, etc) which provide their own fsync implementations are allowed to perform filesystem-specific optimizations there to minimize the number of flushes and maximize throughput.

In this patch set I implemented (1) and (3) for ext3/4 to have some code to comment on.

Does this approach make sense? Thoughts?

- Fernando

#56 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 14 lines · Fernando Luis Vazquez Cao
Quoted 8 lines · Dave Chinner
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

That is precisely the reason why we should use sensible defaults, which in this case means enabling barriers and flushing disk caches on fsync()/fdatasync() by default.

Quoted 5 lines

Adding either a new mount option (as you yourself suggest below) or a sysfs tunable is desirable for those cases when we really do not need to flush the disk write cache to guarantee integrity (battery-backed block devices come to mind), or we want to be fast at the cost of potentially losing some data.

Mount options are the wrong place for this. if you want to change the behaviour of the block device, then it should be at that level.

Quoted 10 lines
Quoted 4 lines

Quite frankly, the VFS should do stuff that is slow and safe and filesystems can choose to ignore the VFS (via filesystem specific mount options) if they want to be fast and potentially unsafe.

To avoid unnecessary flushes and allow for filesystem-specific optimizations I was considering the following approach:

1- Add flushonfsync mount option (as an aside, I am of the opinion that it should be set by default).

No mount option - too confusing for someone to work out what combination of barriers and flushing for things to work correctly. Just make filesystems issue the necessary flush calls or barrier IOs and allow the block devices to ignore flushes.

Quoted 4 lines

2- Modify file_fsync() so that it checks whether FLUSHONFSYNC is set and flushes the underlying device accordingly. With this we would cover all filesystems that use the vfs-provided file_fsync() as their fsync method (commonly used filesystems such as fat fall in this group).

Just make it flush the block device.

Quoted 4 lines

3- Advanced filesystems (ext3/4, XFS, btrfs, etc) which provide their own fsync implementations are allowed to perform filesystem-specific optimizations there to minimize the number of flushes and maximize throughput.

Um, you are describing what we already have in place. Almost every filesystem provides it's own ->fsync method, not just the "advanced" ones. It is those methods that need to be fixed to issue flushes, not just file_fsync().

Quoted 2 lines

In this patch set I implemented (1) and (3) for ext3/4 to have some code to comment on.

I don't think we want (1) at all, and I thought that if ext3/4 are using barriers then the barrier I/O issued by the journal does the flush already. Hence (3) is redundant, right?

FWIW, block device flushes are implemented by barrier IOs, so if the underlying block device doesn't support barriers then you can't flush the cache, either...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#57 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 15 lines · Dave Chinner
Quoted 14 lines · Fernando Luis Vazquez Cao
Quoted 8 lines · Dave Chinner
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

That is precisely the reason why we should use sensible defaults, which in this case means enabling barriers and flushing disk caches on fsync()/fdatasync() by default.

Quoted 8 lines
Quoted 5 lines

Adding either a new mount option (as you yourself suggest below) or a sysfs tunable is desirable for those cases when we really do not need to flush the disk write cache to guarantee integrity (battery-backed block devices come to mind), or we want to be fast at the cost of potentially losing some data.

Mount options are the wrong place for this. if you want to change the behaviour of the block device, then it should be at that level.

To be more precise, what we are trying to change is the behavior of fsync()/fdatasync(), which users might want to change on a per-partition basis. I guess this is the reason the barrier switch was made a mount option, and I just wanted to be consistent.

My fear is that making one of them a mount option (barriers) and the other a sysfs-tunable block device property (device flushes on fsync()) might end up creating more confusion than using a mount option for both.

Anyway, I do not have strong feelings on this issue and if there is consensus I would be willing to change the patches so that flushonfsync becomes a per block device tunable instead.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Quite frankly, the VFS should do stuff that is slow and safe and filesystems can choose to ignore the VFS (via filesystem specific mount options) if they want to be fast and potentially unsafe.

Quoted 8 lines
Quoted 5 lines

To avoid unnecessary flushes and allow for filesystem-specific optimizations I was considering the following approach:

1- Add flushonfsync mount option (as an aside, I am of the opinion that it should be set by default).

No mount option - too confusing for someone to work out what combination of barriers and flushing for things to work correctly.

As I suggested in a previous email, it is just a matter of using a safe combination by default so that users do not need to figure out anything.

Quoted 1 line

Just make filesystems issue the necessary flush calls or barrier IOs

"ext3: call blkdev_issue_flush on fsync" and "ext4: call blkdev_issue_flush on fsync" in this patch set implement just that for ext3/4.

Quoted 1 line

and allow the block devices to ignore flushes.

Wouldn't it make more sense to avoid sending bios down the block layer which we can know in advance are going to be ignored by the block device?

Quoted 6 lines
Quoted 4 lines

2- Modify file_fsync() so that it checks whether FLUSHONFSYNC is set and flushes the underlying device accordingly. With this we would cover all filesystems that use the vfs-provided file_fsync() as their fsync method (commonly used filesystems such as fat fall in this group).

Just make it flush the block device.

I wrote a patch that does exactly that but, in addition, it checks whether FLUSHONFSYNC is set to avoid sending unnecessary flushes down the block layer (this patch is not included in this patch-set, but I will add it in the next iteration).

As I mentioned above, if everyone thinks this small optimization not elegant or an undesirable layering violation I will remove it.

Quoted 8 lines
Quoted 4 lines

3- Advanced filesystems (ext3/4, XFS, btrfs, etc) which provide their own fsync implementations are allowed to perform filesystem-specific optimizations there to minimize the number of flushes and maximize throughput.

Um, you are describing what we already have in place. Almost every filesystem provides it's own ->fsync method, not just the "advanced" ones.

Yes, I know. There are some remarkable exceptions such as fat, though.

Quoted 2 lines

It is those methods that need to be fixed to issue flushes, not just
file_fsync().

Exactly, and this patch-set is my first attempt at that. For the first submission I limited myself to fixing only ext3/4 so that I can get some early feedback on my approach before moving forward.

Quoted 6 lines
Quoted 2 lines

In this patch set I implemented (1) and (3) for ext3/4 to have some code to comment on.

I don't think we want (1) at all, and I thought that if ext3/4 are using barriers then the barrier I/O issued by the journal does the flush already. Hence (3) is redundant, right?

No, it is no redundant because a barrier is not issued in all cases. The aforementioned two patches fix ext3/4 by emitting a device flush only when necessary (i.e. when a barrier would not be emitted).

My impression is that we all agree in the basic approach, the only point of contention being whether filesystems/VFS should be allowed to optimize out disk flushes when the user told the kernel to do so (be it via a sysfs tunable or mount option).

Cheers,

Fernando

#58 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 11 lines · Fernando Luis Vázquez Cao
Quoted 6 lines
Quoted 4 lines

2- Modify file_fsync() so that it checks whether FLUSHONFSYNC is set and flushes the underlying device accordingly. With this we would cover all filesystems that use the vfs-provided file_fsync() as their fsync method (commonly used filesystems such as fat fall in this group).

Just make it flush the block device.

I wrote a patch that does exactly that but, in addition, it checks whether FLUSHONFSYNC is set to avoid sending unnecessary flushes down the block layer (this patch is not included in this patch-set, but I will add it in the next iteration).

I probably should have mentioned in my proposal all filesystems would just call the helper function block_flush_device() to flush the underlying block device, unconditionally.

This helper function looks like this:

+/* Issue flush of write caches on the block device */ +int block_flush_device(struct super_block *sb) +{
+ int ret = 0;
+
+ if (!(sb->s_flags & MS_FLUSHONFSYNC))
+ return ret;
+
+ ret = blkdev_issue_flush(sb->s_bdev, NULL);
+
+ if (ret == -EOPNOTSUPP)
+ return 0;
+
+ return ret;
+}

As you can see the check for flushonfsync is done here, so changing my patches along the lines you suggest would be a trivial two lines patch.

- Fernando

Quoted 2 lines

As I mentioned above, if everyone thinks this small optimization not elegant or an undesirable layering violation I will remove it.

#59 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

In reply to Fernando Luis Vázquez Cao
Quoted 22 lines · Fernando Luis Vázquez Cao
Quoted 21 lines · Dave Chinner
Quoted 19 lines · Fernando Luis Vazquez Cao
Quoted 8 lines · Dave Chinner
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

That is precisely the reason why we should use sensible defaults, which in this case means enabling barriers and flushing disk caches on fsync()/fdatasync() by default.

Adding either a new mount option (as you yourself suggest below) or a sysfs tunable is desirable for those cases when we really do not need to flush the disk write cache to guarantee integrity (battery-backed block devices come to mind), or we want to be fast at the cost of potentially losing some data.

Quoted 7 lines
Quoted 2 lines

Mount options are the wrong place for this. if you want to change the behaviour of the block device, then it should be at that level.

To be more precise, what we are trying to change is the behavior of fsync()/fdatasync(), which users might want to change on a per-partition basis. I guess this is the reason the barrier switch was made a mount option, and I just wanted to be consistent.

This has no place in the kernel. Use LD_PRELOAD to make fsync() a no-op.

Quoted 5 lines
Quoted 2 lines

No mount option - too confusing for someone to work out what combination of barriers and flushing for things to work correctly.

As I suggested in a previous email, it is just a matter of using a safe combination by default so that users do not need to figure out anything.

Too many users think that they need to specify everything rather than rely on defaults...

Quoted 11 lines
Quoted 1 line

Just make filesystems issue the necessary flush calls or barrier IOs

"ext3: call blkdev_issue_flush on fsync" and "ext4: call blkdev_issue_flush on fsync" in this patch set implement just that for ext3/4.

Quoted 1 line

and allow the block devices to ignore flushes.

Wouldn't it make more sense to avoid sending bios down the block layer which we can know in advance are going to be ignored by the block device?

As soon as the block layer reports EOPNOTSUPPORTED to a barrier IO, the filesystem will switch them off and not issue them anymore.

Quoted 7 lines
Quoted 3 lines

I don't think we want (1) at all, and I thought that if ext3/4 are using barriers then the barrier I/O issued by the journal does the flush already. Hence (3) is redundant, right?

No, it is no redundant because a barrier is not issued in all cases. The aforementioned two patches fix ext3/4 by emitting a device flush only when necessary (i.e. when a barrier would not be emitted).

Then that is a filesystem fix, not something that requires VFS modifications or new mount options....

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#60 lore raw

Re: vfs: Add MS_FLUSHONFSYNC mount flag

Quoted 26 lines · Dave Chinner
Quoted 25 lines · Fernando Luis Vázquez Cao
Quoted 23 lines · Dave Chinner
Quoted 14 lines · Fernando Luis Vazquez Cao
Quoted 8 lines · Dave Chinner
Quoted 2 lines · Eric Sandeen

I'm just a little leery of the "dangerous" mount option proliferation, I guess.

You're not the only one, Eric. It's bad enough having to explain to users what barriers do once they have lost data after a power loss, let alone confusing them further by adding more mount options they will get wrong by accident....

That is precisely the reason why we should use sensible defaults, which in this case means enabling barriers and flushing disk caches on fsync()/fdatasync() by default.

Quoted 5 lines

Adding either a new mount option (as you yourself suggest below) or a sysfs tunable is desirable for those cases when we really do not need to flush the disk write cache to guarantee integrity (battery-backed block devices come to mind), or we want to be fast at the cost of potentially losing some data.

Mount options are the wrong place for this. if you want to change the behaviour of the block device, then it should be at that level.

Quoted 7 lines
Quoted 4 lines

To be more precise, what we are trying to change is the behavior of fsync()/fdatasync(), which users might want to change on a per-partition basis. I guess this is the reason the barrier switch was made a mount option, and I just wanted to be consistent.

This has no place in the kernel. Use LD_PRELOAD to make fsync() a no-op.

The purpose of flushonfsync is not making fsync() a no-op and goes beyond what we can currently achieve with LD_PRELOAD. For example, if we send the data to disk but avoid flushing the block device's write cache we can potentially improve I/O performance at the cost of compromising data and filesystem integrity. This is a risk that those who play fast and loose may want assume. By the way, sadly enough this is the way many of the filesystems in Linus' tree behave. I just wanted to change this situation by making all filesystems issue write-cache flushes by default.

Some people suggested to leave a knob for those who wanted to revert to the old behavior and I myself thought that it could make sense in some cases so decided to add the tunable flushonsync.

If there is consensus flushonfsync should be a per-device tunable I am more than willing to make it so. My goal is to fix all filesystem so that they emit barriers and disk flushes when they should. flushonfsync is just a nicety I added for those who, for whatever reason, still want the old behavior.

For the next iteration of this patchset I will take out the contentious bits and leave only the filesystem/VFS fixes so that we can move forward while we discuss the propriety of adding a per-device or a per-filesystem tunable such as flushonfsync to change the default (and safe) behavior.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

No mount option - too confusing for someone to work out what combination of barriers and flushing for things to work correctly.

As I suggested in a previous email, it is just a matter of using a safe combination by default so that users do not need to figure out anything.

Too many users think that they need to specify everything rather than rely on defaults...

Well that is their business. From my experience most admins in the field do not stray from their enterprise-distro provided defaults.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Just make filesystems issue the necessary flush calls or barrier IOs

"ext3: call blkdev_issue_flush on fsync" and "ext4: call blkdev_issue_flush on fsync" in this patch set implement just that for ext3/4.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

and allow the block devices to ignore flushes.

Wouldn't it make more sense to avoid sending bios down the block layer which we can know in advance are going to be ignored by the block device?

As soon as the block layer reports EOPNOTSUPPORTED to a barrier IO, the filesystem will switch them off and not issue them anymore.

Yes, that certainly makes sense. But the point in discussion is whether users should be allowed to switch them off (it arguably makes sense in some scenarios). I am afraid that some users will not be happy if we do not leave the door open for them to revert to the old behavior.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

I don't think we want (1) at all, and I thought that if ext3/4 are using barriers then the barrier I/O issued by the journal does the flush already. Hence (3) is redundant, right?

No, it is no redundant because a barrier is not issued in all cases. The aforementioned two patches fix ext3/4 by emitting a device flush only when necessary (i.e. when a barrier would not be emitted).

Then that is a filesystem fix, not something that requires VFS modifications or new mount options....

Yup, as mentioned above flushonfsync is just a nicety I added to the second iteration of this patchset and is independent from the filesystem fixes.

Regards,

Fernando

#61 lore raw

util-linux: Add new mount options flushonfsync and noflushonfsync to mount

In reply to Fernando Luis Vázquez Cao

This mount flag will be used to determine whether the block device's write cache should be flush or not on fsync()/fdatasync().

---

mount.c +7 -0
diff -urp mount-orig/mount.c mount/mount.c
@@ -185,6 +185,13 @@ static const struct opt_map opt_map[] = 
   { "norelatime", 0, 1, MS_RELATIME }, /* Update access time without regard
 					  to mtime/ctime */
 #endif
+#ifdef MS_FLUSHONFSYNC
+  { "flushonfsync", 0, 0, MS_FLUSHONFSYNC },	/* Force block device flush on
+						   fsync()/fdatasync() */
+  { "noflushonfsync", 0, 1, MS_FLUSHONFSYNC },	/* Do not force block device
+						   flush on
+						   fsync()/fdatasync() */
+#endif
   { "nofail",	0, 0, MS_COMMENT},	/* Do not fail if ENOENT on dev */
   { NULL,	0, 0, 0		}
 };
mount_constants.h +4 -0
diff -urp mount-orig/mount_constants.h mount/mount_constants.h
@@ -56,6 +56,10 @@
 #ifndef MS_SHARED
 #define MS_SHARED	(1<<20)	/* 1048576 Shared*/
 #endif
+#ifndef MS_FLUSHONFSYNC
+#define MS_FLUSHONFSYNC	(1<<24) /* Force block device flush on
+				   fsync()/fdatasync() */
+#endif
 /*
  * Magic mount flag number. Had to be or-ed to the flag values.
  */
#62 lore raw

util-linux: Add explanation for new mount options flushonfsync and noflushonfsync to mount(8) man page

In reply to Fernando Luis Vázquez Cao

---

mount.8 +12 -0
diff -urp mount-orig/mount.8 mount/mount.8
@@ -649,6 +649,18 @@ current modify or change time. (Similar 
 mutt or other applications that need to know if a file has been read
 since the last time it was modified.)
 .TP
+.B flushonfsync
+Force block device flush on fsync()/fdatasync(). When using storage equipped
+with write caches that can be explicitly flushed by the kernel, this guarantees
+that the data/metadata actually made it to the physical media after invoking
+the respective file data/metadata synchronization system call.
+.TP
+.B noflushonfsync
+Do not force a flush of the backing device's write cache when a file
+data/metadata synchronization system call is executed. Unless the device is
+equipped with a battery-backup write cache, this behavior can cause data loss
+and/or file system corruption in the event of a power failure.
+.TP
 .B noauto
 Can only be mounted explicitly (i.e., the
 .B \-a
#63 lore raw

block: Add block_flush_device()

In reply to Fernando Luis Vázquez Cao

This patch adds a helper function that should be used by filesystems that need to flush the underlying block device on fsync()/fdatasync().

---

fs/buffer.c +18 -0
diff -urNp linux-2.6.29-rc3-orig/fs/buffer.c linux-2.6.29-rc3/fs/buffer.c
@@ -165,6 +165,24 @@ void end_buffer_write_sync(struct buffer
 	put_bh(bh);
 }
 
+/* Issue flush of write caches on the block device */
+int block_flush_device(struct super_block *sb)
+{
+	int ret = 0;
+
+	if (!(sb->s_flags & MS_FLUSHONFSYNC))
+		return ret;
+
+	ret = blkdev_issue_flush(sb->s_bdev, NULL);
+
+	if (ret == -EOPNOTSUPP)
+		return 0;
+
+	return ret;
+}
+EXPORT_SYMBOL(block_flush_device);
+
+
 /*
  * Write out and wait upon all the dirty data associated with a block
  * device via its mapping.  Does not take the superblock lock.
include/linux/buffer_head.h +1 -0
diff -urNp linux-2.6.29-rc3-orig/include/linux/buffer_head.h linux-2.6.29-rc3/include/linux/buffer_head.h
@@ -238,6 +238,7 @@ int nobh_write_end(struct file *, struct
 int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
 int nobh_writepage(struct page *page, get_block_t *get_block,
                         struct writeback_control *wbc);
+int block_flush_device(struct super_block *sb);
 
 void buffer_init(void);
 
#64 lore raw

ext3: call blkdev_issue_flush on fsync

In reply to Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext3/fsync.c +16 -3
diff -urNp linux-2.6.29-rc4-orig/fs/ext3/fsync.c linux-2.6.29-rc4/fs/ext3/fsync.c
@@ -45,6 +45,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +71,34 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			goto no_journal_barrier;
+		goto out;
 	}
+
+flush_blkdev:
+	if (!(i_state & I_DIRTY_PAGES))
+		goto out;
+no_journal_barrier:
+	block_flush_device(inode->i_sb);
 out:
 	return ret;
 }
#65 lore raw

ext4: call blkdev_issue_flush on fsync

In reply to Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext4/fsync.c +15 -5
diff -urNp linux-2.6.29-rc4-orig/fs/ext4/fsync.c linux-2.6.29-rc4/fs/ext4/fsync.c
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -76,25 +77,34 @@ int ext4_sync_file(struct file *file, st
 	 */
 	if (ext4_should_journal_data(inode)) {
 		ret = ext4_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		if (journal && !(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
+		goto out;
 	}
+
+flush_blkdev:
+	if (!(i_state & I_DIRTY_PAGES))
+		goto out;
+no_journal_barrier:
+	block_flush_device(inode->i_sb);
 out:
 	return ret;
 }
maintainer
#66 lore raw

Re: ext4: call blkdev_issue_flush on fsync

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 83

Quoted 8 lines · Fernando Luis Vázquez Cao
fs/ext4/fsync.c +2 -0
@@ -76,25 +77,34 @@ int ext4_sync_file(struct file *file, st
 	 */
 	if (ext4_should_journal_data(inode)) {
 		ret = ext4_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}

All of these goto statements makes it one gigantic pile of spaghetti. The code will be much more understable if you do:

		if (!(journal->j_flags & JBD2_BARRIER))
			block_flush_device(inode->i_sb);
		return ret;	

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 87

Quoted 6 lines
fs/ext4/fsync.c +2 -2
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 

Maybe instead:

	if (datasync && !(i_state & I_DIRTY_DATASYNC)) {
		if (i_state & I_DIRTY_PAGES)
			block_flush_device(inode->i_sb);
		return ret;
	}

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 100

Quoted 5 lines
fs/ext4/fsync.c +3 -2
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		if (journal && !(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
+		goto out;

Maybe instead:

		if (journal && !(journal->j_flags & JBD2_BARRIER))
			block_flush_device(inode->i_sb);
		return ret;

}

I'm not a fanatic about eliminating all goto's, but "goto out" which could be replaced by "return ret;" is just silly.

- Ted

#67 lore raw

Re: ext4: call blkdev_issue_flush on fsync

Quoted 39 lines · Theodore Tso

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 83

Quoted 8 lines · Fernando Luis Vázquez Cao
fs/ext4/fsync.c +2 -0
@@ -76,25 +77,34 @@ int ext4_sync_file(struct file *file, st
 	 */
 	if (ext4_should_journal_data(inode)) {
 		ret = ext4_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}

All of these goto statements makes it one gigantic pile of spaghetti. The code will be much more understable if you do:

		if (!(journal->j_flags & JBD2_BARRIER))
			block_flush_device(inode->i_sb);
		return ret;	

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 87

Quoted 6 lines
fs/ext4/fsync.c +2 -2
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 

Maybe instead:

	if (datasync && !(i_state & I_DIRTY_DATASYNC)) {
		if (i_state & I_DIRTY_PAGES)
			block_flush_device(inode->i_sb);
		return ret;
	}

fs/ext4/fsync.c int ext4_sync_file(struct file *file, st · line 100

Quoted 5 lines
fs/ext4/fsync.c +3 -2
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		if (journal && !(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
+		goto out;

Maybe instead:

Quoted 7 lines
		if (journal && !(journal->j_flags & JBD2_BARRIER))
			block_flush_device(inode->i_sb);
		return ret;

}

I'm not a fanatic about eliminating all goto's, but "goto out" which could be replaced by "return ret;" is just silly.

Yes, you are right. I just wanted to follow the current style of the code, but I got carried away and took things a bit too far.

I'll be replying to this email with a new iteration of the patch-set that leaves out the potentially conflictive bits and should be more readable.

Regards,

Fernando

#68 lore raw

PATCH 1/3 block: Add block_flush_device()

This patch adds a helper function that should be used by filesystems that need to flush the underlying block device on fsync()/fdatasync().

---

fs/buffer.c +11 -0
diff -urNp linux-2.6.29-rc4-orig/fs/buffer.c linux-2.6.29-rc4/fs/buffer.c
@@ -165,6 +165,17 @@ void end_buffer_write_sync(struct buffer
 	put_bh(bh);
 }
 
+/* Issue flush of write caches on the block device */
+int block_flush_device(struct super_block *sb)
+{
+	int ret = 0;
+
+	ret = blkdev_issue_flush(sb->s_bdev, NULL);
+
+	return (ret == -EOPNOTSUPP) ? 0 : ret;
+}
+EXPORT_SYMBOL(block_flush_device);
+
 /*
  * Write out and wait upon all the dirty data associated with a block
  * device via its mapping.  Does not take the superblock lock.
include/linux/buffer_head.h +1 -0
diff -urNp linux-2.6.29-rc4-orig/include/linux/buffer_head.h linux-2.6.29-rc4/include/linux/buffer_head.h
@@ -238,6 +238,7 @@ int nobh_write_end(struct file *, struct
 int nobh_truncate_page(struct address_space *, loff_t, get_block_t *);
 int nobh_writepage(struct page *page, get_block_t *get_block,
                         struct writeback_control *wbc);
+int block_flush_device(struct super_block *sb);
 
 void buffer_init(void);
 
#69 lore raw

PATCH 2/3 ext3: call block_flush_device() on fsync

In reply to Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext3/fsync.c +14 -5
diff -urNp linux-2.6.29-rc5-orig/fs/ext3/fsync.c linux-2.6.29-rc5/fs/ext3/fsync.c
@@ -45,6 +45,8 @@
 int ext3_sync_file(struct file * file, struct dentry *dentry, int datasync)
 {
 	struct inode *inode = dentry->d_inode;
+	journal_t *journal = EXT3_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext3_journal_current_handle() == NULL);
@@ -69,23 +71,30 @@ int ext3_sync_file(struct file * file, s
 	 */
 	if (ext3_should_journal_data(inode)) {
 		ret = ext3_force_commit(inode->i_sb);
-		goto out;
+		if (!(journal->j_flags & JFS_BARRIER))
+			block_flush_device(inode->i_sb);
+		return ret;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC)) {
+		if (i_state & I_DIRTY_PAGES)
+			block_flush_device(inode->i_sb);
+		return ret;
+	}
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
+		if (journal && !(journal->j_flags & JFS_BARRIER))
+			block_flush_device(inode->i_sb);
 	}
-out:
+
 	return ret;
 }
#70 lore raw

PATCH 3/3 ext4: call block_flush_device() on fsync

In reply to Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext4/fsync.c +13 -7
diff -urNp linux-2.6.29-rc5-orig/fs/ext4/fsync.c linux-2.6.29-rc5/fs/ext4/fsync.c
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -76,25 +77,30 @@ int ext4_sync_file(struct file *file, st
 	 */
 	if (ext4_should_journal_data(inode)) {
 		ret = ext4_force_commit(inode->i_sb);
-		goto out;
+		if (!(journal->j_flags & JBD2_BARRIER))
+			block_flush_device(inode->i_sb);
+		return ret;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC)) {
+		if (i_state & I_DIRTY_PAGES)
+			block_flush_device(inode->i_sb);
+		return ret;
+	}
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		if (journal && !(journal->j_flags & JBD2_BARRIER))
+			block_flush_device(inode->i_sb);
 	}
-out:
+
 	return ret;
 }
#71 lore raw

ext4: call blkdev_issue_flush on fsync

In reply to Fernando Luis Vázquez Cao

To ensure that bits are truly on-disk after an fsync or fdatasync, we should force a disk flush explicitly when there is dirty data/metadata and the journal didn't emit a write barrier (either because metadata is not being synched or barriers are disabled).

---

fs/ext4/fsync.c +14 -5
@@ -48,6 +48,7 @@ int ext4_sync_file(struct file *file, st
 {
 	struct inode *inode = dentry->d_inode;
 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+	unsigned long i_state = inode->i_state;
 	int ret = 0;
 
 	J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -76,25 +77,33 @@ int ext4_sync_file(struct file *file, st
 	 */
 	if (ext4_should_journal_data(inode)) {
 		ret = ext4_force_commit(inode->i_sb);
+		if (!(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
 		goto out;
 	}
 
-	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
-		goto out;
+	if (datasync && !(i_state & I_DIRTY_DATASYNC))
+		goto flush_blkdev;
 
 	/*
 	 * The VFS has written the file data.  If the inode is unaltered
 	 * then we need not start a commit.
 	 */
-	if (inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
+	if (i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC)) {
 		struct writeback_control wbc = {
 			.sync_mode = WB_SYNC_ALL,
 			.nr_to_write = 0, /* sys_fsync did this */
 		};
 		ret = sync_inode(inode, &wbc);
-		if (journal && (journal->j_flags & JBD2_BARRIER))
-			blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
+		if (journal && !(journal->j_flags & JBD2_BARRIER))
+			goto no_journal_barrier;
 	}
+
+flush_blkdev:
+	if (!(i_state & I_DIRTY_PAGES))
+		goto out;
+no_journal_barrier:
+	blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
 out:
 	return ret;
 }
#72 lore raw
In reply to Alan Cox
Quoted 8 lines · Alan Cox
Quoted 4 lines

Plus it has a nasty behaviour where it reverts to -W1 if disk connection is momentarily lost. (If you unplug/replug SATA disk, linux will happily rediscover and use it, but -W0 was already forgotten at that point, right?)

If you momentarily lose power your disk state isn't defined anyway. If you do that with the SATA code it should treat it the same as a scsi disk swap so it'll get a new device and the old fs will go down.

No, that is not what happened :-(. If I replug disk within 10 seconds, it just behaves as if nothing happened, and continues operating on the disk. AMD machine... but I don't think that matters.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html