All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: linux-cachefs@redhat.com
Cc: dhowells@redhat.com, Trond Myklebust <trondmy@hammerspace.com>,
	Anna Schumaker <anna.schumaker@netapp.com>,
	Steve French <sfrench@samba.org>,
	Dominique Martinet <asmadeus@codewreck.org>,
	Jeff Layton <jlayton@kernel.com>,
	Matthew Wilcox <willy@infradead.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Omar Sandoval <osandov@osandov.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org,
	linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org,
	v9fs-developer@lists.sourceforge.net,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 21/53] fscache: Provide a means to begin an operation
Date: Fri, 22 Oct 2021 20:03:57 +0100	[thread overview]
Message-ID: <163492943791.1038219.9645087798121151922.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <163492911924.1038219.13107463173777870713.stgit@warthog.procyon.org.uk>

Provide a function to begin a read operation:

	int fscache_begin_read_operation(
		struct netfs_cache_resources *cres,
		struct fscache_cookie *cookie)

This may be called by the network filesystem on behalf of netfslib, but may
also be called to use the I/O access functions directly.  It attaches the
resources required by the cache to cres struct from the supplied cookie.

This holds access to the cache behind the cookie for the duration of the
operation and forces cache withdrawal and cookie invalidation to perform
some sort of synchronisation on the operation.

Note that this does not guarantee that the cache state is fully set up and
able to perform I/O immediately; looking up and creation may be left
proceeding in the background.  The operations intended to be called by the
network filesystem, such as reading and writing, are expected to wait for
the cookie to move to the correct state.

This will, however, potentially sleep, waiting for a certain minimum state
to be set or for operations such as invalidate to advance far enough that
I/O can resume.

A function is also provided for the cache to call to wait for the cache
object to get to a state where it can be used for certain things:

	bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
					enum fscache_want_stage stage);

This looks at the cache resources provided by the begin function and waits
for them to get to an appropriate stage.  There's a choice of wanting just
some parameters (FSCACHE_WANT_PARAM) or the ability to do I/O
(FSCACHE_WANT_READ or FSCACHE_WANT_WRITE).

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

 fs/fscache/Makefile            |    1 
 fs/fscache/io.c                |  152 ++++++++++++++++++++++++++++++++++++++++
 include/linux/fscache-cache.h  |   23 ++++++
 include/linux/fscache.h        |   51 +++++++++++++
 include/trace/events/fscache.h |    6 ++
 5 files changed, 233 insertions(+)
 create mode 100644 fs/fscache/io.c

diff --git a/fs/fscache/Makefile b/fs/fscache/Makefile
index bcc79615f93a..afb090ea16c4 100644
--- a/fs/fscache/Makefile
+++ b/fs/fscache/Makefile
@@ -6,6 +6,7 @@
 fscache-y := \
 	cache.o \
 	cookie.o \
+	io.o \
 	main.o \
 	volume.o
 
diff --git a/fs/fscache/io.c b/fs/fscache/io.c
new file mode 100644
index 000000000000..9c747069b923
--- /dev/null
+++ b/fs/fscache/io.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Cache data I/O routines
+ *
+ * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ */
+#define FSCACHE_DEBUG_LEVEL OPERATION
+#define FSCACHE_USE_NEW_IO_API
+#define FSCACHE_USE_FALLBACK_IO_API
+#include <linux/fscache-cache.h>
+#include <linux/uio.h>
+#include <linux/bvec.h>
+#include <linux/slab.h>
+#include <linux/uio.h>
+#include "internal.h"
+
+/**
+ * fscache_wait_for_operation - Wait for an object become accessible
+ * @cres: The cache resources for the operation being performed
+ * @want_stage: The minimum stage the object must be at
+ *
+ * See if the target cache object is at the specified minimum stage of
+ * accessibility yet, and if not, wait for it.
+ */
+bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
+				enum fscache_want_stage want_stage)
+{
+	struct fscache_cookie *cookie = fscache_cres_cookie(cres);
+	enum fscache_cookie_stage stage;
+
+again:
+	if (!fscache_cache_is_live(cookie->volume->cache)) {
+		_leave(" [broken]");
+		return false;
+	}
+
+	stage = READ_ONCE(cookie->stage);
+	_enter("c=%08x{%u},%x", cookie->debug_id, stage, want_stage);
+
+	switch (stage) {
+	case FSCACHE_COOKIE_STAGE_CREATING:
+	case FSCACHE_COOKIE_STAGE_INVALIDATING:
+		if (want_stage == FSCACHE_WANT_PARAMS)
+			goto ready; /* There can be no content */
+		fallthrough;
+	case FSCACHE_COOKIE_STAGE_LOOKING_UP:
+	case FSCACHE_COOKIE_STAGE_COMMITTING:
+		wait_var_event(&cookie->stage, READ_ONCE(cookie->stage) != stage);
+		goto again;
+
+	case FSCACHE_COOKIE_STAGE_ACTIVE:
+		goto ready;
+	case FSCACHE_COOKIE_STAGE_DROPPED:
+	case FSCACHE_COOKIE_STAGE_RELINQUISHING:
+	default:
+		_leave(" [not live]");
+		return false;
+	}
+
+ready:
+	if (!cres->cache_priv2)
+		return cookie->volume->cache->ops->begin_operation(cres, want_stage);
+	return true;
+}
+EXPORT_SYMBOL(fscache_wait_for_operation);
+
+/*
+ * Begin an I/O operation on the cache, waiting till we reach the right state.
+ *
+ * Attaches the resources required to the operation resources record.
+ */
+static int fscache_begin_operation(struct netfs_cache_resources *cres,
+				   struct fscache_cookie *cookie,
+				   enum fscache_want_stage want_stage,
+				   enum fscache_access_trace why)
+{
+	enum fscache_cookie_stage stage;
+	long timeo;
+	bool once_only = false;
+
+	cres->ops		= NULL;
+	cres->cache_priv	= cookie;
+	cres->cache_priv2	= NULL;
+	cres->debug_id		= cookie->debug_id;
+	cres->inval_counter	= cookie->inval_counter;
+
+	if (!fscache_begin_cookie_access(cookie, why))
+		return -ENOBUFS;
+
+again:
+	spin_lock(&cookie->lock);
+
+	stage = cookie->stage;
+	_enter("c=%08x{%u},%x", cookie->debug_id, stage, want_stage);
+
+	switch (stage) {
+	case FSCACHE_COOKIE_STAGE_LOOKING_UP:
+	case FSCACHE_COOKIE_STAGE_COMMITTING:
+		goto wait_and_validate;
+	case FSCACHE_COOKIE_STAGE_INVALIDATING:
+	case FSCACHE_COOKIE_STAGE_CREATING:
+		if (want_stage == FSCACHE_WANT_PARAMS)
+			goto ready; /* There can be no content */
+		goto wait_and_validate;
+	case FSCACHE_COOKIE_STAGE_ACTIVE:
+		goto ready;
+	case FSCACHE_COOKIE_STAGE_DROPPED:
+	case FSCACHE_COOKIE_STAGE_RELINQUISHING:
+		WARN(1, "Can't use cookie in stage %u\n", cookie->stage);
+		goto not_live;
+	default:
+		goto not_live;
+	}
+
+ready:
+	spin_unlock(&cookie->lock);
+	if (!cookie->volume->cache->ops->begin_operation(cres, want_stage))
+		goto failed;
+	return 0;
+
+wait_and_validate:
+	spin_unlock(&cookie->lock);
+	trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
+			     atomic_read(&cookie->n_accesses),
+			     fscache_access_io_wait);
+	timeo = wait_var_event_timeout(&cookie->stage,
+				       READ_ONCE(cookie->stage) != stage, 20 * HZ);
+	if (timeo <= 1 && !once_only) {
+		pr_warn("%s: cookie stage change wait timed out: cookie->stage=%u stage=%u",
+			__func__, READ_ONCE(cookie->stage), stage);
+		fscache_print_cookie(cookie, 'O');
+		once_only = true;
+	}
+	goto again;
+
+not_live:
+	spin_unlock(&cookie->lock);
+failed:
+	cres->cache_priv = NULL;
+	cres->ops = NULL;
+	fscache_end_cookie_access(cookie, fscache_access_io_not_live);
+	_leave(" = -ENOBUFS");
+	return -ENOBUFS;
+}
+
+int __fscache_begin_read_operation(struct netfs_cache_resources *cres,
+				   struct fscache_cookie *cookie)
+{
+	return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
+				       fscache_access_io_read);
+}
+EXPORT_SYMBOL(__fscache_begin_read_operation);
diff --git a/include/linux/fscache-cache.h b/include/linux/fscache-cache.h
index 46d77a1840cd..2d57b85cfde3 100644
--- a/include/linux/fscache-cache.h
+++ b/include/linux/fscache-cache.h
@@ -69,6 +69,10 @@ struct fscache_cache_ops {
 	/* Invalidate an object */
 	bool (*invalidate_cookie)(struct fscache_cookie *cookie);
 
+	/* Begin an operation for the netfs lib */
+	bool (*begin_operation)(struct netfs_cache_resources *cres,
+				enum fscache_want_stage want_stage);
+
 	/* Prepare to write to a live cache object */
 	void (*prepare_to_write)(struct fscache_cookie *cookie);
 };
@@ -123,6 +127,8 @@ extern void fscache_end_cookie_access(struct fscache_cookie *cookie,
 				      enum fscache_access_trace why);
 extern void fscache_set_cookie_stage(struct fscache_cookie *cookie,
 				     enum fscache_cookie_stage stage);
+extern bool fscache_wait_for_operation(struct netfs_cache_resources *cred,
+				       enum fscache_want_stage stage);
 
 /*
  * Find the key on a cookie.
@@ -149,6 +155,23 @@ static inline void fscache_cookie_lookup_negative(struct fscache_cookie *cookie)
 	fscache_set_cookie_stage(cookie, FSCACHE_COOKIE_STAGE_CREATING);
 }
 
+static inline struct fscache_cookie *fscache_cres_cookie(struct netfs_cache_resources *cres)
+{
+	return cres->cache_priv;
+}
+
+/**
+ * fscache_end_operation - End an fscache I/O operation.
+ * @cres: The resources to dispose of.
+ */
+static inline
+void fscache_end_operation(struct netfs_cache_resources *cres)
+{
+	const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
+	if (ops)
+		ops->end_operation(cres);
+}
+
 extern struct workqueue_struct *fscache_wq;
 
 #endif /* _LINUX_FSCACHE_CACHE_H */
diff --git a/include/linux/fscache.h b/include/linux/fscache.h
index 0b01eaaef44c..ba55717f81ca 100644
--- a/include/linux/fscache.h
+++ b/include/linux/fscache.h
@@ -47,6 +47,12 @@ struct fscache_cookie;
 
 #define FSCACHE_INVAL_DIO_WRITE		0x01 /* Invalidate due to DIO write */
 
+enum fscache_want_stage {
+	FSCACHE_WANT_PARAMS,
+	FSCACHE_WANT_WRITE,
+	FSCACHE_WANT_READ,
+};
+
 /*
  * Data object state.
  */
@@ -162,6 +168,9 @@ extern void __fscache_use_cookie(struct fscache_cookie *, bool);
 extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
 extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
 extern void __fscache_invalidate(struct fscache_cookie *, const void *, loff_t, unsigned int);
+#ifdef FSCACHE_USE_NEW_IO_API
+extern int __fscache_begin_read_operation(struct netfs_cache_resources *, struct fscache_cookie *);
+#endif
 
 /**
  * fscache_acquire_volume - Register a volume as desiring caching services
@@ -358,4 +367,46 @@ void fscache_invalidate(struct fscache_cookie *cookie,
 		__fscache_invalidate(cookie, aux_data, size, flags);
 }
 
+/**
+ * fscache_operation_valid - Return true if operations resources are usable
+ * @cres: The resources to check.
+ *
+ * Returns a pointer to the operations table if usable or NULL if not.
+ */
+static inline
+const struct netfs_cache_ops *fscache_operation_valid(const struct netfs_cache_resources *cres)
+{
+	return fscache_resources_valid(cres) ? cres->ops : NULL;
+}
+
+#ifdef FSCACHE_USE_NEW_IO_API
+
+/**
+ * fscache_begin_read_operation - Begin a read operation for the netfs lib
+ * @cres: The cache resources for the read being performed
+ * @cookie: The cookie representing the cache object
+ *
+ * Begin a read operation on behalf of the netfs helper library.  @cres
+ * indicates the cache resources to which the operation state should be
+ * attached; @cookie indicates the cache object that will be accessed.
+ *
+ * This is intended to be called from the ->begin_cache_operation() netfs lib
+ * operation as implemented by the network filesystem.
+ *
+ * Returns:
+ * * 0		- Success
+ * * -ENOBUFS	- No caching available
+ * * Other error code from the cache, such as -ENOMEM.
+ */
+static inline
+int fscache_begin_read_operation(struct netfs_cache_resources *cres,
+				 struct fscache_cookie *cookie)
+{
+	if (fscache_cookie_enabled(cookie))
+		return __fscache_begin_read_operation(cres, cookie);
+	return -ENOBUFS;
+}
+
+#endif /* FSCACHE_USE_NEW_IO_API */
+
 #endif /* _LINUX_FSCACHE_H */
diff --git a/include/trace/events/fscache.h b/include/trace/events/fscache.h
index b705ff36bddc..4ccaf4490b1b 100644
--- a/include/trace/events/fscache.h
+++ b/include/trace/events/fscache.h
@@ -75,6 +75,9 @@ enum fscache_access_trace {
 	fscache_access_cache_unpin,
 	fscache_access_invalidate_cookie,
 	fscache_access_invalidate_cookie_end,
+	fscache_access_io_not_live,
+	fscache_access_io_read,
+	fscache_access_io_wait,
 	fscache_access_lookup_cookie,
 	fscache_access_lookup_cookie_end,
 	fscache_access_relinquish_volume,
@@ -140,6 +143,9 @@ enum fscache_access_trace {
 	EM(fscache_access_cache_unpin,		"UNPIN cache  ")	\
 	EM(fscache_access_invalidate_cookie,	"BEGIN inval  ")	\
 	EM(fscache_access_invalidate_cookie_end,"END   inval  ")	\
+	EM(fscache_access_io_not_live,		"END   io_notl")	\
+	EM(fscache_access_io_read,		"BEGIN io_read")	\
+	EM(fscache_access_io_wait,		"WAIT  io    ")		\
 	EM(fscache_access_lookup_cookie,	"BEGIN lookup ")	\
 	EM(fscache_access_lookup_cookie_end,	"END   lookup ")	\
 	EM(fscache_access_relinquish_volume,	"BEGIN rlq_vol")	\



  parent reply	other threads:[~2021-10-22 19:04 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-22 18:58 [PATCH v2 00/53] fscache: Rewrite index API and management system David Howells
2021-10-22 18:58 ` [PATCH v2 01/53] fscache_old: Move the old fscache driver to one side David Howells
2021-10-22 18:59 ` [PATCH v2 02/53] fscache_old: Rename CONFIG_FSCACHE* to CONFIG_FSCACHE_OLD* David Howells
2021-10-22 18:59 ` [PATCH v2 03/53] cachefiles_old: Move the old cachefiles driver to one side David Howells
2021-10-22 18:59 ` [PATCH v2 04/53] cachefiles_old: Rename CONFIG_CACHEFILES* to CONFIG_CACHEFILES_OLD* David Howells
2021-10-22 18:59 ` [PATCH v2 05/53] netfs: Display the netfs inode number in the netfs_read tracepoint David Howells
2021-10-22 19:00 ` [PATCH v2 06/53] netfs: Pass a flag to ->prepare_write() to say if there's no alloc'd space David Howells
2021-10-22 19:00 ` [PATCH v2 07/53] fscache: Introduce new driver David Howells
2021-10-22 19:01 ` [PATCH v2 08/53] fscache: Implement a hash function David Howells
2021-10-22 19:01 ` [PATCH v2 09/53] fscache: Implement cache registration David Howells
2021-10-22 19:01 ` [PATCH v2 10/53] fscache: Implement volume registration David Howells
2021-10-22 19:01 ` [PATCH v2 11/53] fscache: Implement cookie registration David Howells
2021-10-22 19:02 ` [PATCH v2 12/53] fscache: Implement cache-level access helpers David Howells
2021-10-22 19:02 ` [PATCH v2 13/53] fscache: Implement volume-level " David Howells
2021-10-22 19:02 ` [PATCH v2 14/53] fscache: Implement cookie-level " David Howells
2021-10-22 19:02 ` [PATCH v2 15/53] fscache: Implement functions add/remove a cache David Howells
2021-10-22 19:02 ` [PATCH v2 16/53] fscache: Provide and use cache methods to lookup/create/free a volume David Howells
2021-10-22 19:03 ` [PATCH v2 17/53] fscache: Add a function for a cache backend to note an I/O error David Howells
2021-10-22 19:03 ` [PATCH v2 18/53] fscache: Implement simple cookie state machine David Howells
2021-10-22 19:03 ` [PATCH v2 19/53] fscache: Implement cookie user counting and resource pinning David Howells
2021-10-22 19:03 ` [PATCH v2 20/53] fscache: Implement cookie invalidation David Howells
2021-10-22 19:03 ` David Howells [this message]
2021-10-22 19:04 ` [PATCH v2 22/53] fscache: Provide read/write stat counters for the cache David Howells
2021-10-22 19:04 ` [PATCH v2 23/53] fscache: Provide a function to let the netfs update its coherency data David Howells
2021-10-22 19:04 ` [PATCH v2 24/53] fscache: Implement I/O interface David Howells
2021-10-22 19:04 ` [PATCH v2 25/53] fscache: Provide fallback I/O functions David Howells
2021-10-22 19:05 ` [PATCH v2 26/53] vfs, fscache: Implement pinning of cache usage for writeback David Howells
2021-10-22 19:05 ` [PATCH v2 27/53] fscache: Provide a function to note the release of a page David Howells
2021-10-22 19:05 ` [PATCH v2 28/53] fscache: Provide a function to resize a cookie David Howells
2021-10-22 19:05 ` [PATCH v2 29/53] cachefiles: Introduce new driver David Howells
2021-10-22 19:06 ` [PATCH v2 30/53] cachefiles: Add some error injection support David Howells
2021-10-22 19:06 ` [PATCH v2 31/53] cachefiles: Define structs David Howells
2021-10-22 19:06 ` [PATCH v2 32/53] cachefiles: Add a couple of tracepoints for logging errors David Howells
2021-10-22 19:06 ` [PATCH v2 33/53] cachefiles: Add I/O error reporting macros David Howells
2021-10-22 19:06 ` [PATCH v2 34/53] cachefiles: Provide a function to check how much space there is David Howells
2021-10-22 19:07 ` [PATCH v2 35/53] cachefiles: Implement a function to get/create a directory in the cache David Howells
2021-10-22 19:07 ` [PATCH v2 36/53] cachefiles: Implement daemon UAPI and cache registration David Howells
2021-10-22 19:07 ` [PATCH v2 37/53] cachefiles: Implement volume support David Howells
2021-10-22 19:07 ` [PATCH v2 38/53] cachefiles: Implement data storage object handling David Howells
2021-10-22 19:07 ` [PATCH v2 39/53] cachefiles: Implement begin and end I/O David Howells
2021-10-22 19:08 ` [PATCH v2 40/53] cachefiles: Implement the I/O routines David Howells
2021-10-22 19:08 ` [PATCH v2 41/53] afs: Handle len being extending over page end in write_begin/write_end David Howells
2021-10-22 19:08 ` [PATCH v2 42/53] afs: Fix afs_write_end() to handle len > page size David Howells
2021-10-22 19:08 ` [PATCH v2 43/53] afs: Make afs_write_begin() return the THP subpage David Howells
2021-10-22 19:08 ` [PATCH v2 44/53] afs: Convert afs to use the new fscache API David Howells
2021-10-22 19:09 ` [PATCH v2 45/53] afs: Copy local writes to the cache when writing to the server David Howells
2021-10-22 19:09 ` [PATCH v2 46/53] afs: Skip truncation on the server of data we haven't written yet David Howells
2021-10-22 19:09 ` [PATCH v2 47/53] afs: Add synchronous O_DIRECT support David Howells
2021-10-22 19:09 ` [PATCH v2 48/53] 9p: Use fscache indexing rewrite and reenable caching David Howells
2021-10-22 19:10 ` [PATCH v2 49/53] 9p: Copy local writes to the cache when writing to the server David Howells
2021-10-22 19:10 ` [PATCH v2 50/53] nfs: Convert to new fscache volume/cookie API David Howells
2021-10-22 19:10 ` [PATCH v2 51/53] cifs: Support fscache indexing rewrite (untested) David Howells
2021-10-22 19:10 ` [PATCH v2 52/53] fscache, cachefiles: Display stats of no-space events David Howells
2021-10-22 19:11 ` [PATCH v2 53/53] fscache, cachefiles: Display stat of culling events David Howells
2021-10-22 19:21 ` [PATCH v2 00/53] fscache: Rewrite index API and management system Linus Torvalds
2021-10-22 19:40 ` David Howells
2021-10-22 19:58   ` Linus Torvalds
2021-10-22 20:24     ` Linus Torvalds

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=163492943791.1038219.9645087798121151922.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=anna.schumaker@netapp.com \
    --cc=asmadeus@codewreck.org \
    --cc=ceph-devel@vger.kernel.org \
    --cc=jlayton@kernel.com \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-cachefs@redhat.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=osandov@osandov.com \
    --cc=sfrench@samba.org \
    --cc=torvalds@linux-foundation.org \
    --cc=trondmy@hammerspace.com \
    --cc=v9fs-developer@lists.sourceforge.net \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.