[Ocfs2-tools-devel] [PATCH 06/13] libocfs2: Allow a global nocache flag on io_channels.

Joel Becker joel.becker at oracle.com
Tue May 26 16:03:26 PDT 2009


We've added _nocache() versions of the I/O functions so that smart
callers can specify when certain I/Os should not pollute the I/O cache.
However, not all code is smart.  Rather than teach
ocfs2_file_read/write() to pass another nocache argument, let's give I/O
channels the knowledge to skip caching.

The io_set_nocache() function will set or clear a nocache flag on the
channel.  While set, the channel will use the _nocache() functions for
I/O (assuming a cache is there).  This preserves the qualities of the
cache - it's always up to date - but will not pollute it with new
blocks.  When finished, the caller can io_set_nocache(channel, false)
and return to using the cache.

Signed-off-by: Joel Becker <joel.becker at oracle.com>
---
 include/ocfs2/ocfs2.h |    8 ++++++++
 libocfs2/unix_io.c    |   19 ++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/include/ocfs2/ocfs2.h b/include/ocfs2/ocfs2.h
index 3f2ca54..e3c62d2 100644
--- a/include/ocfs2/ocfs2.h
+++ b/include/ocfs2/ocfs2.h
@@ -40,6 +40,7 @@
 #include <time.h>
 #include <string.h>
 #include <stddef.h>
+#include <stdbool.h>
 
 #include <limits.h>
 
@@ -229,6 +230,12 @@ int io_get_fd(io_channel *channel);
  * in a good state.
  *
  * Use ocfs2_read_blocks() if your application might handle o2image file.
+ *
+ * If a channel is set to 'nocache' via io_set_nocache(), it will use
+ * the _nocache() functions even if called via the regular functions.
+ * This allows control of naive code that we don't want to have to carry
+ * nocache parameters around.  Smarter code can ignore this function and
+ * use the _nocache() functions directly.
  */
 errcode_t io_read_block(io_channel *channel, int64_t blkno, int count,
 			char *data);
@@ -239,6 +246,7 @@ errcode_t io_write_block(io_channel *channel, int64_t blkno, int count,
 errcode_t io_write_block_nocache(io_channel *channel, int64_t blkno, int count,
 			 const char *data);
 errcode_t io_init_cache(io_channel *channel, size_t nr_blocks);
+void io_set_nocache(io_channel *channel, bool nocache);
 errcode_t io_init_cache_size(io_channel *channel, size_t bytes);
 void io_destroy_cache(io_channel *channel);
 
diff --git a/libocfs2/unix_io.c b/libocfs2/unix_io.c
index 50fa480..bbc8587 100644
--- a/libocfs2/unix_io.c
+++ b/libocfs2/unix_io.c
@@ -42,7 +42,6 @@
 #include <sys/utsname.h>
 #endif
 #include <inttypes.h>
-#include <stdbool.h>
 
 #include "ocfs2/kernel-rbtree.h"
 
@@ -89,6 +88,7 @@ struct _io_channel {
 	int io_flags;
 	int io_error;
 	int io_fd;
+	bool io_nocache;
 	struct io_cache *io_cache;
 };
 
@@ -570,6 +570,7 @@ errcode_t io_open(const char *name, int flags, io_channel **channel)
 	strcpy(chan->io_name, name);
 	chan->io_blksize = OCFS2_MIN_BLOCKSIZE;
 	chan->io_flags = (flags & OCFS2_FLAG_RW) ? O_RDWR : O_RDONLY;
+	chan->io_nocache = false;
 	if (!(flags & OCFS2_FLAG_BUFFERED))
 		chan->io_flags |= O_DIRECT;
 	chan->io_error = 0;
@@ -687,12 +688,24 @@ int io_get_fd(io_channel *channel)
 	return channel->io_fd;
 }
 
+/*
+ * If a channel is set to 'nocache', it will use the _nocache() functions
+ * even if called via the regular functions.  This allows control of
+ * naive code that we don't want to have to carry nocache parameters
+ * around.  Smarter code can ignore this function and use the _nocache()
+ * functions directly.
+ */
+void io_set_nocache(io_channel *channel, bool nocache)
+{
+	channel->io_nocache = nocache;
+}
+
 errcode_t io_read_block(io_channel *channel, int64_t blkno, int count,
 			char *data)
 {
 	if (channel->io_cache)
 		return io_cache_read_block(channel, blkno, count, data,
-					   false);
+					   channel->io_nocache);
 	else
 		return unix_io_read_block(channel, blkno, count, data);
 }
@@ -712,7 +725,7 @@ errcode_t io_write_block(io_channel *channel, int64_t blkno, int count,
 {
 	if (channel->io_cache)
 		return io_cache_write_block(channel, blkno, count, data,
-					    false);
+					    channel->io_nocache);
 	else
 		return unix_io_write_block(channel, blkno, count, data);
 }
-- 
1.6.3




More information about the Ocfs2-tools-devel mailing list