linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: trond.myklebust@fys.uio.no, viro@ZenIV.linux.org.uk
Cc: dhowells@redhat.com, nfsv4@linux-nfs.org,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: [PATCH 13/45] FS-Cache: Add cache tag handling [ver #41]
Date: Thu, 20 Nov 2008 14:42:46 +0000	[thread overview]
Message-ID: <20081120144246.10667.23266.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20081120144139.10667.75519.stgit@warthog.procyon.org.uk>

Implement two features of FS-Cache:

 (1) The ability to request and release cache tags - names by which a cache may
     be known to a netfs, and thus selected for use.

 (2) An internal function by which a cache is selected by consulting the netfs,
     if the netfs wishes to be consulted.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 fs/fscache/Makefile       |    1 
 fs/fscache/fsc-cache.c    |  166 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fscache/fsc-internal.h |   20 +++++
 include/linux/fscache.h   |    9 ++
 4 files changed, 195 insertions(+), 1 deletions(-)
 create mode 100644 fs/fscache/fsc-cache.c


diff --git a/fs/fscache/Makefile b/fs/fscache/Makefile
index 8fb7bcc..85d1dd4 100644
--- a/fs/fscache/Makefile
+++ b/fs/fscache/Makefile
@@ -3,6 +3,7 @@
 #
 
 fscache-y := \
+	fsc-cache.o \
 	fsc-fsdef.o \
 	fsc-main.o \
 	fsc-proc.o \
diff --git a/fs/fscache/fsc-cache.c b/fs/fscache/fsc-cache.c
new file mode 100644
index 0000000..4b28536
--- /dev/null
+++ b/fs/fscache/fsc-cache.c
@@ -0,0 +1,166 @@
+/* FS-Cache cache handling
+ *
+ * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#define FSCACHE_DEBUG_LEVEL CACHE
+#include <linux/module.h>
+#include <linux/slab.h>
+#include "fsc-internal.h"
+
+LIST_HEAD(fscache_cache_list);
+DECLARE_RWSEM(fscache_addremove_sem);
+
+static LIST_HEAD(fscache_cache_tag_list);
+
+/*
+ * look up a cache tag
+ */
+struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
+{
+	struct fscache_cache_tag *tag, *xtag;
+
+	/* firstly check for the existence of the tag under read lock */
+	down_read(&fscache_addremove_sem);
+
+	list_for_each_entry(tag, &fscache_cache_tag_list, link) {
+		if (strcmp(tag->name, name) == 0) {
+			atomic_inc(&tag->usage);
+			up_read(&fscache_addremove_sem);
+			return tag;
+		}
+	}
+
+	up_read(&fscache_addremove_sem);
+
+	/* the tag does not exist - create a candidate */
+	xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
+	if (!xtag)
+		/* return a dummy tag if out of memory */
+		return ERR_PTR(-ENOMEM);
+
+	atomic_set(&xtag->usage, 1);
+	strcpy(xtag->name, name);
+
+	/* write lock, search again and add if still not present */
+	down_write(&fscache_addremove_sem);
+
+	list_for_each_entry(tag, &fscache_cache_tag_list, link) {
+		if (strcmp(tag->name, name) == 0) {
+			atomic_inc(&tag->usage);
+			up_write(&fscache_addremove_sem);
+			kfree(xtag);
+			return tag;
+		}
+	}
+
+	list_add_tail(&xtag->link, &fscache_cache_tag_list);
+	up_write(&fscache_addremove_sem);
+	return xtag;
+}
+
+/*
+ * release a reference to a cache tag
+ */
+void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
+{
+	if (tag != ERR_PTR(-ENOMEM)) {
+		down_write(&fscache_addremove_sem);
+
+		if (atomic_dec_and_test(&tag->usage))
+			list_del_init(&tag->link);
+		else
+			tag = NULL;
+
+		up_write(&fscache_addremove_sem);
+
+		kfree(tag);
+	}
+}
+
+/*
+ * select a cache in which to store an object
+ * - the cache addremove semaphore must be at least read-locked by the caller
+ * - the object will never be an index
+ */
+struct fscache_cache *fscache_select_cache_for_object(
+	struct fscache_cookie *cookie)
+{
+	struct fscache_cache_tag *tag;
+	struct fscache_object *object;
+	struct fscache_cache *cache;
+
+	_enter("");
+
+	if (list_empty(&fscache_cache_list)) {
+		_leave(" = NULL [no cache]");
+		return NULL;
+	}
+
+	/* we check the parent to determine the cache to use */
+	spin_lock(&cookie->lock);
+
+	/* the first in the parent's backing list should be the preferred
+	 * cache */
+	if (!hlist_empty(&cookie->backing_objects)) {
+		object = hlist_entry(cookie->backing_objects.first,
+				     struct fscache_object, cookie_link);
+
+		cache = object->cache;
+		if (object->state >= FSCACHE_OBJECT_DYING ||
+		    test_bit(FSCACHE_IOERROR, &cache->flags))
+			cache = NULL;
+
+		spin_unlock(&cookie->lock);
+		_leave(" = %p [parent]", cache);
+		return cache;
+	}
+
+	/* the parent is unbacked */
+	if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
+		/* cookie not an index and is unbacked */
+		spin_unlock(&cookie->lock);
+		_leave(" = NULL [cookie ub,ni]");
+		return NULL;
+	}
+
+	spin_unlock(&cookie->lock);
+
+	if (!cookie->def->select_cache)
+		goto no_preference;
+
+	/* ask the netfs for its preference */
+	tag = cookie->def->select_cache(cookie->parent->netfs_data,
+					cookie->netfs_data);
+	if (!tag)
+		goto no_preference;
+
+	if (tag == ERR_PTR(-ENOMEM)) {
+		_leave(" = NULL [nomem tag]");
+		return NULL;
+	}
+
+	if (!tag->cache) {
+		_leave(" = NULL [unbacked tag]");
+		return NULL;
+	}
+
+	if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
+		return NULL;
+
+	_leave(" = %p [specific]", tag->cache);
+	return tag->cache;
+
+no_preference:
+	/* netfs has no preference - just select first cache */
+	cache = list_entry(fscache_cache_list.next,
+			   struct fscache_cache, link);
+	_leave(" = %p [first]", cache);
+	return cache;
+}
diff --git a/fs/fscache/fsc-internal.h b/fs/fscache/fsc-internal.h
index 5ff611a..ffd2c88 100644
--- a/fs/fscache/fsc-internal.h
+++ b/fs/fscache/fsc-internal.h
@@ -28,6 +28,15 @@
 #define FSCACHE_MAX_THREADS	32
 
 /*
+ * fsc-cache.c
+ */
+extern struct list_head fscache_cache_list;
+extern struct rw_semaphore fscache_addremove_sem;
+
+extern struct fscache_cache *fscache_select_cache_for_object(
+	struct fscache_cookie *);
+
+/*
  * fsc-fsdef.c
  */
 extern struct fscache_cookie fscache_fsdef_index;
@@ -158,6 +167,17 @@ extern void fscache_proc_cleanup(void);
 #define fscache_proc_cleanup()	do {} while (0)
 #endif
 
+/*
+ * raise an event on an object
+ * - if the event is not masked for that object, then the object is
+ *   queued for attention by the thread pool.
+ */
+static inline void fscache_raise_event(struct fscache_object *object,
+				       unsigned event)
+{
+	BUG();  // TODO
+}
+
 /*****************************************************************************/
 /*
  * debug tracing
diff --git a/include/linux/fscache.h b/include/linux/fscache.h
index ea0fd0f..0b66726 100644
--- a/include/linux/fscache.h
+++ b/include/linux/fscache.h
@@ -187,6 +187,8 @@ struct fscache_netfs {
  * - these are undefined symbols when FS-Cache is not configured and the
  *   optimiser takes care of not using them
  */
+extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
+extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
 
 /**
  * fscache_register_netfs - Register a filesystem as desiring caching services
@@ -232,7 +234,10 @@ void fscache_unregister_netfs(struct fscache_netfs *netfs)
 static inline
 struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
 {
-	return NULL;
+	if (fscache_available())
+		return __fscache_lookup_cache_tag(name);
+	else
+		return NULL;
 }
 
 /**
@@ -247,6 +252,8 @@ struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
 static inline
 void fscache_release_cache_tag(struct fscache_cache_tag *tag)
 {
+	if (fscache_available())
+		__fscache_release_cache_tag(tag);
 }
 
 /**


  parent reply	other threads:[~2008-11-20 14:42 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-20 14:41 [PATCH 00/45] Permit filesystem local caching [ver #41] David Howells
2008-11-20 14:41 ` [PATCH 01/45] Create a dynamically sized pool of threads for doing very slow work items " David Howells
2008-11-21  8:09   ` Andrew Morton
2008-11-21 10:24   ` David Howells
2008-11-21 18:17     ` Andrew Morton
2008-11-22  0:38     ` David Howells
2008-12-19  4:14   ` Serge E. Hallyn
2008-12-19  4:19   ` Serge E. Hallyn
2008-12-19  7:15   ` Andrew Morton
2008-12-19 11:42   ` David Howells
2008-12-19 11:44   ` David Howells
2008-12-19 12:12   ` David Howells
2008-12-19 16:52     ` Serge E. Hallyn
2008-12-19 12:54   ` David Howells
2008-11-20 14:41 ` [PATCH 02/45] Make slow-work thread pool actually dynamic " David Howells
2008-12-19 17:58   ` Serge E. Hallyn
2008-11-20 14:41 ` [PATCH 03/45] Make the slow work pool configurable " David Howells
2008-12-19 18:33   ` Serge E. Hallyn
2008-11-20 14:42 ` [PATCH 04/45] Document the slow work thread pool " David Howells
2008-11-20 14:42 ` [PATCH 05/45] FS-Cache: Release page->private after failed readahead " David Howells
2008-11-21  8:12   ` Andrew Morton
2008-11-21 10:27   ` David Howells
2008-11-20 14:42 ` [PATCH 06/45] FS-Cache: Recruit a couple of page flags for cache management " David Howells
2008-11-21  8:17   ` Andrew Morton
2008-11-21 10:31   ` David Howells
2008-11-20 14:42 ` [PATCH 07/45] FS-Cache: Provide an add_wait_queue_tail() function " David Howells
2008-11-21  8:17   ` Andrew Morton
2008-11-21 13:32   ` David Howells
2008-11-20 14:42 ` [PATCH 08/45] FS-Cache: Add the FS-Cache netfs API and documentation " David Howells
2008-11-20 14:42 ` [PATCH 09/45] FS-Cache: Add the FS-Cache cache backend " David Howells
2008-11-20 14:42 ` [PATCH 10/45] FS-Cache: Add main configuration option, module entry points and debugging " David Howells
2008-11-20 14:42 ` [PATCH 11/45] FS-Cache: Add use of /proc and presentation of statistics " David Howells
2008-11-21  0:15   ` Alexey Dobriyan
2008-11-21  2:17   ` David Howells
2008-11-21  2:34     ` Alexey Dobriyan
2008-11-21 15:32   ` David Howells
2008-11-20 14:42 ` [PATCH 12/45] FS-Cache: Root index definition " David Howells
2008-11-20 14:42 ` David Howells [this message]
2008-11-20 14:42 ` [PATCH 14/45] FS-Cache: Add cache management " David Howells
2008-11-20 14:42 ` [PATCH 15/45] FS-Cache: Provide a slab for cookie allocation " David Howells
2008-11-20 14:43 ` [PATCH 16/45] FS-Cache: Add netfs registration " David Howells
2008-11-20 14:43 ` [PATCH 17/45] FS-Cache: Bit waiting helpers " David Howells
2008-11-20 14:43 ` [PATCH 18/45] FS-Cache: Object management state machine " David Howells
2008-11-20 14:43 ` [PATCH 19/45] FS-Cache: Implement the cookie management part of the netfs API " David Howells
2008-11-20 14:43 ` [PATCH 20/45] FS-Cache: Add and document asynchronous operation handling " David Howells
2008-11-20 14:43 ` [PATCH 21/45] FS-Cache: Implement data I/O part of netfs API " David Howells
2008-11-20 14:43 ` [PATCH 22/45] CacheFiles: Add missing copy_page export for ia64 " David Howells
2008-11-20 14:43 ` [PATCH 23/45] CacheFiles: Be consistent about the use of mapping vs file->f_mapping in Ext3 " David Howells
2008-11-22 17:38   ` Andreas Dilger
2008-11-26 14:40   ` David Howells
2008-11-20 14:43 ` [PATCH 24/45] CacheFiles: Add a hook to write a single page of data to an inode " David Howells
2008-11-21  8:23   ` Andrew Morton
2008-11-21 12:43   ` David Howells
2008-11-21 13:00     ` Jamie Lokier
2008-11-21 17:15       ` Valdis.Kletnieks
2008-11-21 17:36     ` Randy Dunlap
2008-11-21 18:31     ` Andrew Morton
2008-11-22  0:48     ` David Howells
2008-11-20 14:43 ` [PATCH 25/45] CacheFiles: Permit the page lock state to be monitored " David Howells
2008-11-20 14:43 ` [PATCH 26/45] CacheFiles: Export things for CacheFiles " David Howells
2008-11-20 14:43 ` [PATCH 27/45] CacheFiles: A cache that backs onto a mounted filesystem " David Howells
2008-11-20 14:44 ` [PATCH 28/45] FS-Cache: Make kAFS use FS-Cache " David Howells
2008-11-20 14:44 ` [PATCH 29/45] NFS: Add comment banners to some NFS functions " David Howells
2008-11-20 14:44 ` [PATCH 30/45] NFS: Add FS-Cache option bit and debug bit " David Howells
2008-11-20 14:44 ` [PATCH 31/45] NFS: Permit local filesystem caching to be enabled for NFS " David Howells
2008-11-20 14:44 ` [PATCH 32/45] NFS: Register NFS for caching and retrieve the top-level index " David Howells
2008-11-20 14:44 ` [PATCH 33/45] NFS: Define and create server-level objects " David Howells
2008-11-20 14:44 ` [PATCH 34/45] NFS: Define and create superblock-level " David Howells
2008-11-20 14:44 ` [PATCH 35/45] NFS: Define and create inode-level cache " David Howells
2008-11-20 14:44 ` [PATCH 36/45] NFS: Use local disk inode cache " David Howells
2008-11-20 14:44 ` [PATCH 37/45] NFS: Invalidate FsCache page flags when cache removed " David Howells
2008-11-20 14:44 ` [PATCH 38/45] NFS: Add some new I/O counters for FS-Cache doing things for NFS " David Howells
2008-11-20 14:45 ` [PATCH 39/45] NFS: FS-Cache page management " David Howells
2008-11-20 14:45 ` [PATCH 40/45] NFS: Add read context retention for FS-Cache to call back with " David Howells
2008-11-20 14:45 ` [PATCH 41/45] NFS: nfs_readpage_async() needs to be accessible as a fallback for local caching " David Howells
2008-11-20 14:45 ` [PATCH 42/45] NFS: Read pages from FS-Cache into an NFS inode " David Howells
2008-11-20 14:45 ` [PATCH 43/45] NFS: Store pages from an NFS inode into a local cache " David Howells
2008-11-20 14:45 ` [PATCH 44/45] NFS: Display local caching state " David Howells
2008-11-20 14:45 ` [PATCH 45/45] NFS: Add mount options to enable local caching on NFS " David Howells
2008-11-21  8:28 ` [PATCH 00/45] Permit filesystem local caching " Andrew Morton
2008-11-22  1:11 ` David Howells
2008-11-25  0:09 ` David Howells
2008-11-25 13:39 ` FS-Cache Benchmarks David Howells

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20081120144246.10667.23266.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nfsv4@linux-nfs.org \
    --cc=trond.myklebust@fys.uio.no \
    --cc=viro@ZenIV.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).