linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: 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/43] FS-Cache: Add cache management [ver #46]
Date: Thu, 02 Apr 2009 00:04:28 +0100	[thread overview]
Message-ID: <20090401230428.28177.86392.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20090401230321.28177.12010.stgit@warthog.procyon.org.uk>

Implement the entry points by which a cache backend may initialise, add,
declare an error upon and withdraw a cache.

Further, an object is created in sysfs under which each cache added will get
an object created:

	/sys/fs/fscache/<cachetag>/

All of this is described in Documentation/filesystems/caching/backend-api.txt
added by a previous patch.

Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Steve Dickson <steved@redhat.com>
Acked-by: Trond Myklebust <Trond.Myklebust@netapp.com>
---

 fs/fscache/cache.c |  249 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fscache/main.c  |    7 +
 2 files changed, 256 insertions(+), 0 deletions(-)


diff --git a/fs/fscache/cache.c b/fs/fscache/cache.c
index 1a28df3..355172f 100644
--- a/fs/fscache/cache.c
+++ b/fs/fscache/cache.c
@@ -16,6 +16,8 @@
 
 LIST_HEAD(fscache_cache_list);
 DECLARE_RWSEM(fscache_addremove_sem);
+DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq);
+EXPORT_SYMBOL(fscache_cache_cleared_wq);
 
 static LIST_HEAD(fscache_cache_tag_list);
 
@@ -164,3 +166,250 @@ no_preference:
 	_leave(" = %p [first]", cache);
 	return cache;
 }
+
+/**
+ * fscache_init_cache - Initialise a cache record
+ * @cache: The cache record to be initialised
+ * @ops: The cache operations to be installed in that record
+ * @idfmt: Format string to define identifier
+ * @...: sprintf-style arguments
+ *
+ * Initialise a record of a cache and fill in the name.
+ *
+ * See Documentation/filesystems/caching/backend-api.txt for a complete
+ * description.
+ */
+void fscache_init_cache(struct fscache_cache *cache,
+			const struct fscache_cache_ops *ops,
+			const char *idfmt,
+			...)
+{
+	va_list va;
+
+	memset(cache, 0, sizeof(*cache));
+
+	cache->ops = ops;
+
+	va_start(va, idfmt);
+	vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va);
+	va_end(va);
+
+	INIT_WORK(&cache->op_gc, NULL);
+	INIT_LIST_HEAD(&cache->link);
+	INIT_LIST_HEAD(&cache->object_list);
+	INIT_LIST_HEAD(&cache->op_gc_list);
+	spin_lock_init(&cache->object_list_lock);
+	spin_lock_init(&cache->op_gc_list_lock);
+}
+EXPORT_SYMBOL(fscache_init_cache);
+
+/**
+ * fscache_add_cache - Declare a cache as being open for business
+ * @cache: The record describing the cache
+ * @ifsdef: The record of the cache object describing the top-level index
+ * @tagname: The tag describing this cache
+ *
+ * Add a cache to the system, making it available for netfs's to use.
+ *
+ * See Documentation/filesystems/caching/backend-api.txt for a complete
+ * description.
+ */
+int fscache_add_cache(struct fscache_cache *cache,
+		      struct fscache_object *ifsdef,
+		      const char *tagname)
+{
+	struct fscache_cache_tag *tag;
+
+	BUG_ON(!cache->ops);
+	BUG_ON(!ifsdef);
+
+	cache->flags = 0;
+	ifsdef->event_mask = ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
+	ifsdef->state = FSCACHE_OBJECT_ACTIVE;
+
+	if (!tagname)
+		tagname = cache->identifier;
+
+	BUG_ON(!tagname[0]);
+
+	_enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
+
+	/* we use the cache tag to uniquely identify caches */
+	tag = __fscache_lookup_cache_tag(tagname);
+	if (IS_ERR(tag))
+		goto nomem;
+
+	if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
+		goto tag_in_use;
+
+	cache->kobj = kobject_create_and_add(tagname, fscache_root);
+	if (!cache->kobj)
+		goto error;
+
+	ifsdef->cookie = &fscache_fsdef_index;
+	ifsdef->cache = cache;
+	cache->fsdef = ifsdef;
+
+	down_write(&fscache_addremove_sem);
+
+	tag->cache = cache;
+	cache->tag = tag;
+
+	/* add the cache to the list */
+	list_add(&cache->link, &fscache_cache_list);
+
+	/* add the cache's netfs definition index object to the cache's
+	 * list */
+	spin_lock(&cache->object_list_lock);
+	list_add_tail(&ifsdef->cache_link, &cache->object_list);
+	spin_unlock(&cache->object_list_lock);
+
+	/* add the cache's netfs definition index object to the top level index
+	 * cookie as a known backing object */
+	spin_lock(&fscache_fsdef_index.lock);
+
+	hlist_add_head(&ifsdef->cookie_link,
+		       &fscache_fsdef_index.backing_objects);
+
+	atomic_inc(&fscache_fsdef_index.usage);
+
+	/* done */
+	spin_unlock(&fscache_fsdef_index.lock);
+	up_write(&fscache_addremove_sem);
+
+	printk(KERN_NOTICE "FS-Cache: Cache \"%s\" added (type %s)\n",
+	       cache->tag->name, cache->ops->name);
+	kobject_uevent(cache->kobj, KOBJ_ADD);
+
+	_leave(" = 0 [%s]", cache->identifier);
+	return 0;
+
+tag_in_use:
+	printk(KERN_ERR "FS-Cache: Cache tag '%s' already in use\n", tagname);
+	__fscache_release_cache_tag(tag);
+	_leave(" = -EXIST");
+	return -EEXIST;
+
+error:
+	__fscache_release_cache_tag(tag);
+	_leave(" = -EINVAL");
+	return -EINVAL;
+
+nomem:
+	_leave(" = -ENOMEM");
+	return -ENOMEM;
+}
+EXPORT_SYMBOL(fscache_add_cache);
+
+/**
+ * fscache_io_error - Note a cache I/O error
+ * @cache: The record describing the cache
+ *
+ * Note that an I/O error occurred in a cache and that it should no longer be
+ * used for anything.  This also reports the error into the kernel log.
+ *
+ * See Documentation/filesystems/caching/backend-api.txt for a complete
+ * description.
+ */
+void fscache_io_error(struct fscache_cache *cache)
+{
+	set_bit(FSCACHE_IOERROR, &cache->flags);
+
+	printk(KERN_ERR "FS-Cache: Cache %s stopped due to I/O error\n",
+	       cache->ops->name);
+}
+EXPORT_SYMBOL(fscache_io_error);
+
+/*
+ * request withdrawal of all the objects in a cache
+ * - all the objects being withdrawn are moved onto the supplied list
+ */
+static void fscache_withdraw_all_objects(struct fscache_cache *cache,
+					 struct list_head *dying_objects)
+{
+	struct fscache_object *object;
+
+	spin_lock(&cache->object_list_lock);
+
+	while (!list_empty(&cache->object_list)) {
+		object = list_entry(cache->object_list.next,
+				    struct fscache_object, cache_link);
+		list_move_tail(&object->cache_link, dying_objects);
+
+		_debug("withdraw %p", object->cookie);
+
+		spin_lock(&object->lock);
+		spin_unlock(&cache->object_list_lock);
+		fscache_raise_event(object, FSCACHE_OBJECT_EV_WITHDRAW);
+		spin_unlock(&object->lock);
+
+		cond_resched();
+		spin_lock(&cache->object_list_lock);
+	}
+
+	spin_unlock(&cache->object_list_lock);
+}
+
+/**
+ * fscache_withdraw_cache - Withdraw a cache from the active service
+ * @cache: The record describing the cache
+ *
+ * Withdraw a cache from service, unbinding all its cache objects from the
+ * netfs cookies they're currently representing.
+ *
+ * See Documentation/filesystems/caching/backend-api.txt for a complete
+ * description.
+ */
+void fscache_withdraw_cache(struct fscache_cache *cache)
+{
+	LIST_HEAD(dying_objects);
+
+	_enter("");
+
+	printk(KERN_NOTICE "FS-Cache: Withdrawing cache \"%s\"\n",
+	       cache->tag->name);
+
+	/* make the cache unavailable for cookie acquisition */
+	if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
+		BUG();
+
+	down_write(&fscache_addremove_sem);
+	list_del_init(&cache->link);
+	cache->tag->cache = NULL;
+	up_write(&fscache_addremove_sem);
+
+	/* make sure all pages pinned by operations on behalf of the netfs are
+	 * written to disk */
+	cache->ops->sync_cache(cache);
+
+	/* dissociate all the netfs pages backed by this cache from the block
+	 * mappings in the cache */
+	cache->ops->dissociate_pages(cache);
+
+	/* we now have to destroy all the active objects pertaining to this
+	 * cache - which we do by passing them off to thread pool to be
+	 * disposed of */
+	_debug("destroy");
+
+	fscache_withdraw_all_objects(cache, &dying_objects);
+
+	/* wait for all extant objects to finish their outstanding operations
+	 * and go away */
+	_debug("wait for finish");
+	wait_event(fscache_cache_cleared_wq,
+		   atomic_read(&cache->object_count) == 0);
+	_debug("wait for clearance");
+	wait_event(fscache_cache_cleared_wq,
+		   list_empty(&cache->object_list));
+	_debug("cleared");
+	ASSERT(list_empty(&dying_objects));
+
+	kobject_put(cache->kobj);
+
+	clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
+	fscache_release_cache_tag(cache->tag);
+	cache->tag = NULL;
+
+	_leave("");
+}
+EXPORT_SYMBOL(fscache_withdraw_cache);
diff --git a/fs/fscache/main.c b/fs/fscache/main.c
index 7c734b7..c2f3e63 100644
--- a/fs/fscache/main.c
+++ b/fs/fscache/main.c
@@ -56,9 +56,15 @@ static int __init fscache_init(void)
 	if (ret < 0)
 		goto error_proc;
 
+	fscache_root = kobject_create_and_add("fscache", kernel_kobj);
+	if (!fscache_root)
+		goto error_kobj;
+
 	printk(KERN_NOTICE "FS-Cache: Loaded\n");
 	return 0;
 
+error_kobj:
+	fscache_proc_cleanup();
 error_proc:
 	slow_work_unregister_user();
 error_slow_work:
@@ -74,6 +80,7 @@ static void __exit fscache_exit(void)
 {
 	_enter("");
 
+	kobject_put(fscache_root);
 	fscache_proc_cleanup();
 	slow_work_unregister_user();
 	printk(KERN_NOTICE "FS-Cache: Unloaded\n");


  parent reply	other threads:[~2009-04-01 23:04 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-01 23:03 [PATCH 00/43] Permit filesystem local caching [ver #46] David Howells
2009-04-01 23:03 ` [PATCH 01/43] Create a dynamically sized pool of threads for doing very slow work items " David Howells
2009-04-01 23:03 ` [PATCH 02/43] Make slow-work thread pool actually dynamic " David Howells
2009-04-01 23:03 ` [PATCH 03/43] Make the slow work pool configurable " David Howells
2009-04-01 23:03 ` [PATCH 04/43] Document the slow work thread pool " David Howells
2009-04-01 23:03 ` [PATCH 05/43] FS-Cache: Release page->private after failed readahead " David Howells
2009-04-02 13:51   ` Nick Piggin
2009-04-02 14:21   ` David Howells
2009-04-02 17:17   ` Rik van Riel
2009-04-01 23:03 ` [PATCH 06/43] FS-Cache: Recruit a couple of page flags for cache management " David Howells
2009-04-02 13:55   ` Nick Piggin
2009-04-02 14:36   ` David Howells
2009-04-02 15:25     ` Nick Piggin
2009-04-02 15:51     ` David Howells
2009-04-02 16:15       ` Nick Piggin
2009-04-02 16:53         ` Nick Piggin
2009-04-02 17:09         ` David Howells
2009-04-02 17:31           ` Nick Piggin
2009-04-02 17:40           ` David Howells
2009-04-02 18:14             ` Nick Piggin
2009-04-02 19:19             ` Trond Myklebust
2009-04-03  1:11             ` David Howells
2009-04-02 16:48       ` What I mean by a 'netfs' when dealing with FS-Cache David Howells
2009-04-02 17:06       ` David Howells
2009-04-02 18:23   ` [PATCH 06/43] FS-Cache: Recruit a couple of page flags for cache management [ver #46] Rik van Riel
2009-04-01 23:03 ` [PATCH 07/43] FS-Cache: Add the FS-Cache netfs API and documentation " David Howells
2009-04-01 23:04 ` [PATCH 08/43] FS-Cache: Add the FS-Cache cache backend " David Howells
2009-04-01 23:04 ` [PATCH 09/43] FS-Cache: Add main configuration option, module entry points and debugging " David Howells
2009-04-01 23:04 ` [PATCH 10/43] FS-Cache: Add use of /proc and presentation of statistics " David Howells
2009-04-01 23:04 ` [PATCH 11/43] FS-Cache: Root index definition " David Howells
2009-04-01 23:04 ` [PATCH 12/43] FS-Cache: Add cache tag handling " David Howells
2009-04-01 23:04 ` David Howells [this message]
2009-04-01 23:04 ` [PATCH 14/43] FS-Cache: Provide a slab for cookie allocation " David Howells
2009-04-01 23:04 ` [PATCH 15/43] FS-Cache: Add netfs registration " David Howells
2009-04-01 23:04 ` [PATCH 16/43] FS-Cache: Bit waiting helpers " David Howells
2009-04-01 23:04 ` [PATCH 17/43] FS-Cache: Object management state machine " David Howells
2009-04-01 23:04 ` [PATCH 18/43] FS-Cache: Implement the cookie management part of the netfs API " David Howells
2009-04-01 23:04 ` [PATCH 19/43] FS-Cache: Add and document asynchronous operation handling " David Howells
2009-04-01 23:05 ` [PATCH 20/43] FS-Cache: Implement data I/O part of netfs API " David Howells
2009-04-01 23:05 ` [PATCH 21/43] CacheFiles: Be consistent about the use of mapping vs file->f_mapping in Ext3 " David Howells
2009-04-02 18:52   ` Rik van Riel
2009-04-01 23:05 ` [PATCH 22/43] CacheFiles: Add a hook to write a single page of data to an inode " David Howells
2009-04-02 14:00   ` Nick Piggin
2009-04-02 14:55   ` David Howells
2009-04-02 15:32     ` Nick Piggin
2009-04-02 16:37       ` Christoph Hellwig
2009-04-02 16:47         ` Nick Piggin
2009-04-02 16:55           ` Christoph Hellwig
2009-04-02 17:07             ` Nick Piggin
2009-04-02 17:26               ` Jan Kara
2009-04-02 17:22           ` David Howells
2009-04-02 18:18           ` David Howells
2009-04-02 22:44           ` David Howells
2009-04-03 13:41           ` David Howells
2009-04-02 16:02     ` David Howells
2009-04-02 15:32   ` Peter Staubach
2009-04-02 16:03   ` David Howells
2009-04-02 18:48   ` Rik van Riel
2009-04-01 23:05 ` [PATCH 23/43] CacheFiles: Permit the page lock state to be monitored " David Howells
2009-04-02 15:37   ` Nick Piggin
2009-04-02 16:14   ` David Howells
2009-04-02 16:35     ` Nick Piggin
2009-04-02 17:05     ` David Howells
2009-04-02 18:03       ` Nick Piggin
2009-04-02 18:51   ` Rik van Riel
2009-04-01 23:05 ` [PATCH 24/43] CacheFiles: Export things for CacheFiles " David Howells
2009-04-02 18:53   ` Rik van Riel
2009-04-01 23:05 ` [PATCH 25/43] CacheFiles: A cache that backs onto a mounted filesystem " David Howells
2009-04-01 23:05 ` [PATCH 26/43] FS-Cache: Make kAFS use FS-Cache " David Howells
2009-04-01 23:05 ` [PATCH 27/43] NFS: Add comment banners to some NFS functions " David Howells
2009-04-01 23:05 ` [PATCH 28/43] NFS: Add FS-Cache option bit and debug bit " David Howells
2009-04-01 23:05 ` [PATCH 29/43] NFS: Permit local filesystem caching to be enabled for NFS " David Howells
2009-04-01 23:05 ` [PATCH 30/43] NFS: Register NFS for caching and retrieve the top-level index " David Howells
2009-04-01 23:06 ` [PATCH 31/43] NFS: Define and create server-level objects " David Howells
2009-04-01 23:06 ` [PATCH 32/43] NFS: Define and create superblock-level " David Howells
2009-04-01 23:06 ` [PATCH 33/43] NFS: Define and create inode-level cache " David Howells
2009-04-01 23:06 ` [PATCH 34/43] NFS: Use local disk inode cache " David Howells
2009-04-01 23:06 ` [PATCH 35/43] NFS: Invalidate FsCache page flags when cache removed " David Howells
2009-04-01 23:06 ` [PATCH 36/43] NFS: Add some new I/O counters for FS-Cache doing things for NFS " David Howells
2009-04-01 23:06 ` [PATCH 37/43] NFS: FS-Cache page management " David Howells
2009-04-01 23:06 ` [PATCH 38/43] NFS: Add read context retention for FS-Cache to call back with " David Howells
2009-04-01 23:06 ` [PATCH 39/43] NFS: nfs_readpage_async() needs to be accessible as a fallback for local caching " David Howells
2009-04-01 23:06 ` [PATCH 40/43] NFS: Read pages from FS-Cache into an NFS inode " David Howells
2009-04-01 23:06 ` [PATCH 41/43] NFS: Store pages from an NFS inode into a local cache " David Howells
2009-04-01 23:06 ` [PATCH 42/43] NFS: Display local caching state " David Howells
2009-04-01 23:07 ` [PATCH 43/43] NFS: Add mount options to enable local caching on NFS " 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=20090401230428.28177.86392.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=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).