Commit 7f3283ab authored by David Howells's avatar David Howells

fscache: Implement cookie registration

Add functions to the fscache API to allow data file cookies to be acquired
and relinquished by the network filesystem.  It is intended that the
filesystem will create such cookies per-inode under a volume.

To request a cookie, the filesystem should call:

	struct fscache_cookie *
	fscache_acquire_cookie(struct fscache_volume *volume,
			       u8 advice,
			       const void *index_key,
			       size_t index_key_len,
			       const void *aux_data,
			       size_t aux_data_len,
			       loff_t object_size)


The filesystem must first have created a volume cookie, which is passed in
here.  If it passes in NULL then the function will just return a NULL
cookie.

A binary key should be passed in index_key and is of size index_key_len.
This is saved in the cookie and is used to locate the associated data in
the cache.

A coherency data buffer of size aux_data_len will be allocated and
initialised from the buffer pointed to by aux_data.  This is used to
validate cache objects when they're opened and is stored on disk with them
when they're committed.  The data is stored in the cookie and will be
updateable by various functions in later patches.

The object_size must also be given.  This is also used to perform a
coherency check and to size the backing storage appropriately.

This function disallows a cookie from being acquired twice in parallel,
though it will cause the second user to wait if the first is busy
relinquishing its cookie.


When a network filesystem has finished with a cookie, it should call:

	void
	fscache_relinquish_cookie(struct fscache_volume *volume,
				  bool retire)

If retire is true, any backing data will be discarded immediately.

Changes
=======
ver #3:
 - fscache_hash()'s size parameter is now in bytes.  Use __le32 as the unit
   to round up to.
 - When comparing cookies, simply see if the attributes are the same rather
   than subtracting them to produce a strcmp-style return[1].
 - Add a check to see if the cookie is still hashed at the point of
   freeing.

ver #2:
 - Don't hold n_accesses elevated whilst cache is bound to a cookie, but
   rather add a flag that prevents the state machine from being queued when
   n_accesses reaches 0.
 - Remove the unused cookie pointer field from the fscache_acquire
   tracepoint.
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Reviewed-by: default avatarJeff Layton <jlayton@kernel.org>
cc: linux-cachefs@redhat.com
Link: https://lore.kernel.org/r/CAHk-=whtkzB446+hX0zdLsdcUJsJ=8_-0S1mE_R+YurThfUbLA@mail.gmail.com/ [1]
Link: https://lore.kernel.org/r/163819590658.215744.14934902514281054323.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906891983.143852.6219772337558577395.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967088507.1823006.12659006350221417165.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021498432.640689.12743483856927722772.stgit@warthog.procyon.org.uk/ # v4
parent 62ab6335
......@@ -5,6 +5,7 @@
fscache-y := \
cache.o \
cookie.o \
main.o \
volume.o
......
This diff is collapsed.
......@@ -50,6 +50,20 @@ static inline bool fscache_set_cache_state_maybe(struct fscache_cache *cache,
return try_cmpxchg_release(&cache->state, &old_state, new_state);
}
/*
* cookie.c
*/
extern struct kmem_cache *fscache_cookie_jar;
extern const struct seq_operations fscache_cookies_seq_ops;
extern void fscache_print_cookie(struct fscache_cookie *cookie, char prefix);
static inline void fscache_see_cookie(struct fscache_cookie *cookie,
enum fscache_cookie_trace where)
{
trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
where);
}
/*
* main.c
*/
......@@ -75,6 +89,15 @@ extern void fscache_proc_cleanup(void);
extern atomic_t fscache_n_volumes;
extern atomic_t fscache_n_volumes_collision;
extern atomic_t fscache_n_volumes_nomem;
extern atomic_t fscache_n_cookies;
extern atomic_t fscache_n_acquires;
extern atomic_t fscache_n_acquires_ok;
extern atomic_t fscache_n_acquires_oom;
extern atomic_t fscache_n_relinquishes;
extern atomic_t fscache_n_relinquishes_retire;
extern atomic_t fscache_n_relinquishes_dropped;
static inline void fscache_stat(atomic_t *stat)
{
......
......@@ -79,9 +79,20 @@ static int __init fscache_init(void)
if (ret < 0)
goto error_proc;
fscache_cookie_jar = kmem_cache_create("fscache_cookie_jar",
sizeof(struct fscache_cookie),
0, 0, NULL);
if (!fscache_cookie_jar) {
pr_notice("Failed to allocate a cookie jar\n");
ret = -ENOMEM;
goto error_cookie_jar;
}
pr_notice("Loaded\n");
return 0;
error_cookie_jar:
fscache_proc_cleanup();
error_proc:
destroy_workqueue(fscache_wq);
error_wq:
......@@ -97,6 +108,7 @@ static void __exit fscache_exit(void)
{
_enter("");
kmem_cache_destroy(fscache_cookie_jar);
fscache_proc_cleanup();
destroy_workqueue(fscache_wq);
pr_notice("Unloaded\n");
......
......@@ -27,6 +27,10 @@ int __init fscache_proc_init(void)
&fscache_volumes_seq_ops))
goto error;
if (!proc_create_seq("fs/fscache/cookies", S_IFREG | 0444, NULL,
&fscache_cookies_seq_ops))
goto error;
#ifdef CONFIG_FSCACHE_STATS
if (!proc_create_single("fs/fscache/stats", S_IFREG | 0444, NULL,
fscache_stats_show))
......
......@@ -16,6 +16,18 @@
atomic_t fscache_n_volumes;
atomic_t fscache_n_volumes_collision;
atomic_t fscache_n_volumes_nomem;
atomic_t fscache_n_cookies;
atomic_t fscache_n_acquires;
atomic_t fscache_n_acquires_ok;
atomic_t fscache_n_acquires_oom;
atomic_t fscache_n_updates;
EXPORT_SYMBOL(fscache_n_updates);
atomic_t fscache_n_relinquishes;
atomic_t fscache_n_relinquishes_retire;
atomic_t fscache_n_relinquishes_dropped;
/*
* display the general statistics
......@@ -23,12 +35,26 @@ atomic_t fscache_n_volumes_nomem;
int fscache_stats_show(struct seq_file *m, void *v)
{
seq_puts(m, "FS-Cache statistics\n");
seq_printf(m, "Cookies: v=%d vcol=%u voom=%u\n",
seq_printf(m, "Cookies: n=%d v=%d vcol=%u voom=%u\n",
atomic_read(&fscache_n_cookies),
atomic_read(&fscache_n_volumes),
atomic_read(&fscache_n_volumes_collision),
atomic_read(&fscache_n_volumes_nomem)
);
seq_printf(m, "Acquire: n=%u ok=%u oom=%u\n",
atomic_read(&fscache_n_acquires),
atomic_read(&fscache_n_acquires_ok),
atomic_read(&fscache_n_acquires_oom));
seq_printf(m, "Updates: n=%u\n",
atomic_read(&fscache_n_updates));
seq_printf(m, "Relinqs: n=%u rtr=%u drop=%u\n",
atomic_read(&fscache_n_relinquishes),
atomic_read(&fscache_n_relinquishes_retire),
atomic_read(&fscache_n_relinquishes_dropped));
netfs_stats_show(m);
return 0;
}
......@@ -17,6 +17,7 @@
#include <linux/fscache.h>
enum fscache_cache_trace;
enum fscache_cookie_trace;
enum fscache_access_trace;
enum fscache_cache_state {
......@@ -52,4 +53,25 @@ extern struct rw_semaphore fscache_addremove_sem;
extern struct fscache_cache *fscache_acquire_cache(const char *name);
extern void fscache_relinquish_cache(struct fscache_cache *cache);
extern struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
enum fscache_cookie_trace where);
extern void fscache_put_cookie(struct fscache_cookie *cookie,
enum fscache_cookie_trace where);
extern void fscache_set_cookie_state(struct fscache_cookie *cookie,
enum fscache_cookie_state state);
/**
* fscache_get_key - Get a pointer to the cookie key
* @cookie: The cookie to query
*
* Return a pointer to the where a cookie's key is stored.
*/
static inline void *fscache_get_key(struct fscache_cookie *cookie)
{
if (cookie->key_len <= sizeof(cookie->inline_key))
return cookie->inline_key;
else
return cookie->key;
}
#endif /* _LINUX_FSCACHE_CACHE_H */
......@@ -31,6 +31,27 @@
#define fscache_cookie_enabled(cookie) (0)
#endif
struct fscache_cookie;
#define FSCACHE_ADV_SINGLE_CHUNK 0x01 /* The object is a single chunk of data */
#define FSCACHE_ADV_WRITE_CACHE 0x00 /* Do cache if written to locally */
#define FSCACHE_ADV_WRITE_NOCACHE 0x02 /* Don't cache if written to locally */
/*
* Data object state.
*/
enum fscache_cookie_state {
FSCACHE_COOKIE_STATE_QUIESCENT, /* The cookie is uncached */
FSCACHE_COOKIE_STATE_LOOKING_UP, /* The cache object is being looked up */
FSCACHE_COOKIE_STATE_CREATING, /* The cache object is being created */
FSCACHE_COOKIE_STATE_ACTIVE, /* The cache is active, readable and writable */
FSCACHE_COOKIE_STATE_FAILED, /* The cache failed, withdraw to clear */
FSCACHE_COOKIE_STATE_WITHDRAWING, /* The cookie is being withdrawn */
FSCACHE_COOKIE_STATE_RELINQUISHING, /* The cookie is being relinquished */
FSCACHE_COOKIE_STATE_DROPPED, /* The cookie has been dropped */
#define FSCACHE_COOKIE_STATE__NR (FSCACHE_COOKIE_STATE_DROPPED + 1)
} __attribute__((mode(byte)));
/*
* Volume representation cookie.
*/
......@@ -55,6 +76,60 @@ struct fscache_volume {
#define FSCACHE_VOLUME_CREATING 4 /* Volume is being created on disk */
};
/*
* Data file representation cookie.
* - a file will only appear in one cache
* - a request to cache a file may or may not be honoured, subject to
* constraints such as disk space
* - indices are created on disk just-in-time
*/
struct fscache_cookie {
refcount_t ref;
atomic_t n_active; /* number of active users of cookie */
atomic_t n_accesses; /* Number of cache accesses in progress */
unsigned int debug_id;
unsigned int inval_counter; /* Number of invalidations made */
spinlock_t lock;
struct fscache_volume *volume; /* Parent volume of this file. */
void *cache_priv; /* Cache-side representation */
struct hlist_bl_node hash_link; /* Link in hash table */
struct list_head proc_link; /* Link in proc list */
struct list_head commit_link; /* Link in commit queue */
struct work_struct work; /* Commit/relinq/withdraw work */
loff_t object_size; /* Size of the netfs object */
unsigned long unused_at; /* Time at which unused (jiffies) */
unsigned long flags;
#define FSCACHE_COOKIE_RELINQUISHED 0 /* T if cookie has been relinquished */
#define FSCACHE_COOKIE_RETIRED 1 /* T if this cookie has retired on relinq */
#define FSCACHE_COOKIE_IS_CACHING 2 /* T if this cookie is cached */
#define FSCACHE_COOKIE_NO_DATA_TO_READ 3 /* T if this cookie has nothing to read */
#define FSCACHE_COOKIE_NEEDS_UPDATE 4 /* T if attrs have been updated */
#define FSCACHE_COOKIE_HAS_BEEN_CACHED 5 /* T if cookie needs withdraw-on-relinq */
#define FSCACHE_COOKIE_DISABLED 6 /* T if cookie has been disabled */
#define FSCACHE_COOKIE_LOCAL_WRITE 7 /* T if cookie has been modified locally */
#define FSCACHE_COOKIE_NO_ACCESS_WAKE 8 /* T if no wake when n_accesses goes 0 */
#define FSCACHE_COOKIE_DO_RELINQUISH 9 /* T if this cookie needs relinquishment */
#define FSCACHE_COOKIE_DO_WITHDRAW 10 /* T if this cookie needs withdrawing */
#define FSCACHE_COOKIE_DO_LRU_DISCARD 11 /* T if this cookie needs LRU discard */
#define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */
#define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */
#define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */
enum fscache_cookie_state state;
u8 advice; /* FSCACHE_ADV_* */
u8 key_len; /* Length of index key */
u8 aux_len; /* Length of auxiliary data */
u32 key_hash; /* Hash of volume, key, len */
union {
void *key; /* Index key */
u8 inline_key[16]; /* - If the key is short enough */
};
union {
void *aux; /* Auxiliary data */
u8 inline_aux[8]; /* - If the aux data is short enough */
};
};
/*
* slow-path functions for when there is actually caching available, and the
* netfs does actually have a valid token
......@@ -66,6 +141,14 @@ extern struct fscache_volume *__fscache_acquire_volume(const char *, const char
const void *, size_t);
extern void __fscache_relinquish_volume(struct fscache_volume *, const void *, bool);
extern struct fscache_cookie *__fscache_acquire_cookie(
struct fscache_volume *,
u8,
const void *, size_t,
const void *, size_t,
loff_t);
extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
/**
* fscache_acquire_volume - Register a volume as desiring caching services
* @volume_key: An identification string for the volume
......@@ -113,4 +196,55 @@ void fscache_relinquish_volume(struct fscache_volume *volume,
__fscache_relinquish_volume(volume, coherency_data, invalidate);
}
/**
* fscache_acquire_cookie - Acquire a cookie to represent a cache object
* @volume: The volume in which to locate/create this cookie
* @advice: Advice flags (FSCACHE_COOKIE_ADV_*)
* @index_key: The index key for this cookie
* @index_key_len: Size of the index key
* @aux_data: The auxiliary data for the cookie (may be NULL)
* @aux_data_len: Size of the auxiliary data buffer
* @object_size: The initial size of object
*
* Acquire a cookie to represent a data file within the given cache volume.
*
* See Documentation/filesystems/caching/netfs-api.rst for a complete
* description.
*/
static inline
struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume,
u8 advice,
const void *index_key,
size_t index_key_len,
const void *aux_data,
size_t aux_data_len,
loff_t object_size)
{
if (!fscache_volume_valid(volume))
return NULL;
return __fscache_acquire_cookie(volume, advice,
index_key, index_key_len,
aux_data, aux_data_len,
object_size);
}
/**
* fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
* it
* @cookie: The cookie being returned
* @retire: True if the cache object the cookie represents is to be discarded
*
* This function returns a cookie to the cache, forcibly discarding the
* associated cache object if retire is set to true.
*
* See Documentation/filesystems/caching/netfs-api.rst for a complete
* description.
*/
static inline
void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
{
if (fscache_cookie_valid(cookie))
__fscache_relinquish_cookie(cookie, retire);
}
#endif /* _LINUX_FSCACHE_H */
......@@ -45,6 +45,23 @@ enum fscache_volume_trace {
fscache_volume_see_hash_wake,
};
enum fscache_cookie_trace {
fscache_cookie_collision,
fscache_cookie_discard,
fscache_cookie_get_end_access,
fscache_cookie_get_hash_collision,
fscache_cookie_new_acquire,
fscache_cookie_put_hash_collision,
fscache_cookie_put_over_queued,
fscache_cookie_put_relinquish,
fscache_cookie_put_withdrawn,
fscache_cookie_put_work,
fscache_cookie_see_active,
fscache_cookie_see_relinquish,
fscache_cookie_see_withdraw,
fscache_cookie_see_work,
};
#endif
/*
......@@ -74,6 +91,22 @@ enum fscache_volume_trace {
EM(fscache_volume_see_create_work, "SEE creat") \
E_(fscache_volume_see_hash_wake, "SEE hwake")
#define fscache_cookie_traces \
EM(fscache_cookie_collision, "*COLLIDE*") \
EM(fscache_cookie_discard, "DISCARD ") \
EM(fscache_cookie_get_hash_collision, "GET hcoll") \
EM(fscache_cookie_get_end_access, "GQ endac") \
EM(fscache_cookie_new_acquire, "NEW acq ") \
EM(fscache_cookie_put_hash_collision, "PUT hcoll") \
EM(fscache_cookie_put_over_queued, "PQ overq") \
EM(fscache_cookie_put_relinquish, "PUT relnq") \
EM(fscache_cookie_put_withdrawn, "PUT wthdn") \
EM(fscache_cookie_put_work, "PQ work ") \
EM(fscache_cookie_see_active, "- activ") \
EM(fscache_cookie_see_relinquish, "- x-rlq") \
EM(fscache_cookie_see_withdraw, "- x-wth") \
E_(fscache_cookie_see_work, "- work ")
/*
* Export enum symbols via userspace.
*/
......@@ -84,6 +117,7 @@ enum fscache_volume_trace {
fscache_cache_traces;
fscache_volume_traces;
fscache_cookie_traces;
/*
* Now redefine the EM() and E_() macros to map the enums to the strings that
......@@ -145,6 +179,83 @@ TRACE_EVENT(fscache_volume,
__entry->usage)
);
TRACE_EVENT(fscache_cookie,
TP_PROTO(unsigned int cookie_debug_id,
int ref,
enum fscache_cookie_trace where),
TP_ARGS(cookie_debug_id, ref, where),
TP_STRUCT__entry(
__field(unsigned int, cookie )
__field(int, ref )
__field(enum fscache_cookie_trace, where )
),
TP_fast_assign(
__entry->cookie = cookie_debug_id;
__entry->ref = ref;
__entry->where = where;
),
TP_printk("c=%08x %s r=%d",
__entry->cookie,
__print_symbolic(__entry->where, fscache_cookie_traces),
__entry->ref)
);
TRACE_EVENT(fscache_acquire,
TP_PROTO(struct fscache_cookie *cookie),
TP_ARGS(cookie),
TP_STRUCT__entry(
__field(unsigned int, cookie )
__field(unsigned int, volume )
__field(int, v_ref )
__field(int, v_n_cookies )
),
TP_fast_assign(
__entry->cookie = cookie->debug_id;
__entry->volume = cookie->volume->debug_id;
__entry->v_ref = refcount_read(&cookie->volume->ref);
__entry->v_n_cookies = atomic_read(&cookie->volume->n_cookies);
),
TP_printk("c=%08x V=%08x vr=%d vc=%d",
__entry->cookie,
__entry->volume, __entry->v_ref, __entry->v_n_cookies)
);
TRACE_EVENT(fscache_relinquish,
TP_PROTO(struct fscache_cookie *cookie, bool retire),
TP_ARGS(cookie, retire),
TP_STRUCT__entry(
__field(unsigned int, cookie )
__field(unsigned int, volume )
__field(int, ref )
__field(int, n_active )
__field(u8, flags )
__field(bool, retire )
),
TP_fast_assign(
__entry->cookie = cookie->debug_id;
__entry->volume = cookie->volume->debug_id;
__entry->ref = refcount_read(&cookie->ref);
__entry->n_active = atomic_read(&cookie->n_active);
__entry->flags = cookie->flags;
__entry->retire = retire;
),
TP_printk("c=%08x V=%08x r=%d U=%d f=%02x rt=%u",
__entry->cookie, __entry->volume, __entry->ref,
__entry->n_active, __entry->flags, __entry->retire)
);
#endif /* _TRACE_FSCACHE_H */
/* This part must be outside protection */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment