Skip to contentSkip to conversation

FileJournal: stop using sync_file_range

PATCH In review Christoph Hellwig 3 participants 5 messages Nov 3, 2011 – Nov 5, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 FileJournal: stop using sync_file_range

Using sync_file_range means that neither any required metadata gets commited, nor the disk cache gets flushed. Stop using it for the journal, and add a comment on why a fsync_range system call would be helpful here.

Btw, why does the code use O_SYNC (and not even O_DSYNC!) if using direct I/O, but fdatasync/fsync for buffered I/O? Avoiding cache flushes and metadata updates for every writes is just as important for direct I/O as it is for buffered I/O.

src/os/FileJournal.cc +17 -14
Index: ceph/src/os/FileJournal.cc
===================================================================
@@ -842,23 +842,26 @@ void FileJournal::do_write(bufferlist& b
 
   if (!directio) {
     dout(20) << "do_write fsync" << dendl;
+
+    /*
+     * We'd really love to have a fsync_range or fdatasync_range and do a:
+     *
+     *  if (split) {
+     *    ::fsync_range(fd, header.max_size - split, split)l
+     *    ::fsync_range(fd, get_top(), bl.length() - split);
+     *  else
+     *    ::fsync_range(fd, write_pos, bl.length())
+     *
+     * NetBSD and AIX apparently have it, and adding it to Linux wouldn't be
+     * too hard given all the underlying infrastructure already exist.
+     *
+     * NOTE: using sync_file_range here would not be safe as it does not
+     * flush disk caches or commits any sort of metadata.
+     */
 #if defined(DARWIN) || defined(__FreeBSD__)
     ::fsync(fd);
 #else
-# ifdef HAVE_SYNC_FILE_RANGE
-    if (is_bdev) {
-      if (split) {
-	::sync_file_range(fd, header.max_size - split, split, SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE);
-	::sync_file_range(fd, get_top(), bl.length() - split, SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE);
-	::sync_file_range(fd, header.max_size - split, split, SYNC_FILE_RANGE_WAIT_AFTER);
-	::sync_file_range(fd, get_top(), bl.length() - split, SYNC_FILE_RANGE_WAIT_AFTER);
-      } else {
-	::sync_file_range(fd, write_pos, bl.length(),
-			  SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER);
-      }
-    } else
-# endif
-      ::fdatasync(fd);
+    ::fdatasync(fd);
 #endif
   }
 
#1 lore raw
Quoted 3 lines · Christoph Hellwig

Using sync_file_range means that neither any required metadata gets commited, nor the disk cache gets flushed.  Stop using it for the journal, and add a comment on why a fsync_range system call would be helpful here.

One clarification here that came up in a conversation: the code quoted was explicitly inside if (is_bdev), that is, it is only used when the journal was on a raw block device. That should make a lot of the metadata & block allocation irrelevant.

We're still very interested in hearing what are the real world barrier / write cache flush guarantees from all the syscalls; the documentation doesn't guarantee much, even for fsync.. from fsync(2):

If the underlying hard disk has write caching enabled, then the
data may not really be on
permanent storage when fsync() / fdatasync() return.
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 6 lines · Tommi Virtanen

On Thu, Nov 3, 2011 at 14:45, Christoph Hellwig <[email protected]> wrote:
One clarification here that came up in a conversation: the code quoted was explicitly inside if (is_bdev), that is, it is only used when the journal was on a raw block device. That should make a lot of the metadata & block allocation irrelevant.

Quoted 7 lines

We're still very interested in hearing what are the real world barrier / write cache flush guarantees from all the syscalls; the documentation doesn't guarantee much, even for fsync.. from fsync(2):

If the underlying hard disk has write caching enabled, then the
data may not really be on
permanent storage when fsync() / fdatasync() return.

It should make it to disk, but various filesystems have QOI issues and never flush a disk cache. This is different from sync_file_range, which is implemented entirely inside the VM and has no chance at all to flush the cache (or filesystem metadata).

Note that a block device might also have metadata for block allocations to flush - a prominent example is the device mapper thin provisioning target just merged in Linus' tree that relies on flushes to commit its metadata. Another example is any kind of sparse VM image file when seen from the guest. In both cases the metadata obviously hides behind the concept of cache flushes and isn't directly visible.

#3 lore raw
In reply to Christoph Hellwig
Quoted 3 lines · Christoph Hellwig

Using sync_file_range means that neither any required metadata gets commited, nor the disk cache gets flushed. Stop using it for the journal, and add a comment on why a fsync_range system call would be helpful here.

Sigh... doesn't that mean that sync_file_range is almost completely useless?

(Okay, not completely useless; we use it in FileStore.cc to push data to disk quickly so that the eventual sync(2) is faster.)

Quoted 4 lines

Btw, why does the code use O_SYNC (and not even O_DSYNC!) if using direct I/O, but fdatasync/fsync for buffered I/O? Avoiding cache flushes and metadata updates for every writes is just as important for direct I/O as it is for buffered I/O.

Good call.. looks like I added it in 2010-02 but I couldn't tell you why. :/

Thanks!
sage

Quoted 54 lines

Index: ceph/src/os/FileJournal.cc
===================================================================

src/os/FileJournal.cc +17 -15
@@ -842,23 +842,26 @@ void FileJournal::do_write(bufferlist& b
 
   if (!directio) {
     dout(20) << "do_write fsync" << dendl;
+
+    /*
+     * We'd really love to have a fsync_range or fdatasync_range and do a:
+     *
+     *  if (split) {
+     *    ::fsync_range(fd, header.max_size - split, split)l
+     *    ::fsync_range(fd, get_top(), bl.length() - split);
+     *  else
+     *    ::fsync_range(fd, write_pos, bl.length())
+     *
+     * NetBSD and AIX apparently have it, and adding it to Linux wouldn't be
+     * too hard given all the underlying infrastructure already exist.
+     *
+     * NOTE: using sync_file_range here would not be safe as it does not
+     * flush disk caches or commits any sort of metadata.
+     */
 #if defined(DARWIN) || defined(__FreeBSD__)
     ::fsync(fd);
 #else
-# ifdef HAVE_SYNC_FILE_RANGE
-    if (is_bdev) {
-      if (split) {
-	::sync_file_range(fd, header.max_size - split, split, SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE);
-	::sync_file_range(fd, get_top(), bl.length() - split, SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE);
-	::sync_file_range(fd, header.max_size - split, split, SYNC_FILE_RANGE_WAIT_AFTER);
-	::sync_file_range(fd, get_top(), bl.length() - split, SYNC_FILE_RANGE_WAIT_AFTER);
-      } else {
-	::sync_file_range(fd, write_pos, bl.length(),
-			  SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER);
-      }
-    } else
-# endif
-      ::fdatasync(fd);
+    ::fdatasync(fd);
 #endif
   }
 
--

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

#4 lore raw
Quoted 10 lines · Sage Weil
Quoted 3 lines · Christoph Hellwig

Using sync_file_range means that neither any required metadata gets commited, nor the disk cache gets flushed. Stop using it for the journal, and add a comment on why a fsync_range system call would be helpful here.

Sigh... doesn't that mean that sync_file_range is almost completely useless?

(Okay, not completely useless; we use it in FileStore.cc to push data to disk quickly so that the eventual sync(2) is faster.)

Yeah, you found the rare use case for it.

Quoted 6 lines
Quoted 4 lines

Btw, why does the code use O_SYNC (and not even O_DSYNC!) if using direct I/O, but fdatasync/fsync for buffered I/O? Avoiding cache flushes and metadata updates for every writes is just as important for direct I/O as it is for buffered I/O.

Good call.. looks like I added it in 2010-02 but I couldn't tell you why.

I'm going to look into that once I have a real test setup. There's a few other I/O related bits I want to investigate, too.