Skip to content

Commit fe2140e

Browse files
committed
cachefiles: Implement volume support
Implement support for creating the directory layout for a volume on disk and setting up and withdrawing volume caching. Each volume has a directory named for the volume key under the root of the cache (prefixed with an 'I' to indicate to cachefilesd that it's an index) and then creates a bunch of hash bucket subdirectories under that (named as '@' plus a hex number) in which cookie files will be created. Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeff Layton <[email protected]> cc: [email protected] Link: https://lore.kernel.org/r/163819635314.215744.13081522301564537723.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/163906936397.143852.17788457778396467161.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/163967143860.1823006.7185205806080225038.stgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/164021545212.640689.5064821392307582927.stgit@warthog.procyon.org.uk/ # v4
1 parent d1065b0 commit fe2140e

File tree

6 files changed

+171
-2
lines changed

6 files changed

+171
-2
lines changed

fs/cachefiles/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ cachefiles-y := \
99
interface.o \
1010
main.o \
1111
namei.o \
12-
security.o
12+
security.o \
13+
volume.o
1314

1415
cachefiles-$(CONFIG_CACHEFILES_ERROR_INJECTION) += error_inject.o
1516

fs/cachefiles/cache.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,32 @@ int cachefiles_has_space(struct cachefiles_cache *cache,
262262
return ret;
263263
}
264264

265+
/*
266+
* Withdraw volumes.
267+
*/
268+
static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
269+
{
270+
_enter("");
271+
272+
for (;;) {
273+
struct cachefiles_volume *volume = NULL;
274+
275+
spin_lock(&cache->object_list_lock);
276+
if (!list_empty(&cache->volumes)) {
277+
volume = list_first_entry(&cache->volumes,
278+
struct cachefiles_volume, cache_link);
279+
list_del_init(&volume->cache_link);
280+
}
281+
spin_unlock(&cache->object_list_lock);
282+
if (!volume)
283+
break;
284+
285+
cachefiles_withdraw_volume(volume);
286+
}
287+
288+
_leave("");
289+
}
290+
265291
/*
266292
* Sync a cache to backing disk.
267293
*/
@@ -303,7 +329,7 @@ void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
303329
// PLACEHOLDER: Withdraw objects
304330
fscache_wait_for_objects(fscache);
305331

306-
// PLACEHOLDER: Withdraw volume
332+
cachefiles_withdraw_volumes(cache);
307333
cachefiles_sync_cache(cache);
308334
cache->cache = NULL;
309335
fscache_relinquish_cache(fscache);

fs/cachefiles/daemon.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static int cachefiles_daemon_open(struct inode *inode, struct file *file)
105105

106106
mutex_init(&cache->daemon_mutex);
107107
init_waitqueue_head(&cache->daemon_pollwq);
108+
INIT_LIST_HEAD(&cache->volumes);
109+
spin_lock_init(&cache->object_list_lock);
108110

109111
/* set default caching limits
110112
* - limit at 1% free space and/or free files

fs/cachefiles/interface.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
const struct fscache_cache_ops cachefiles_cache_ops = {
1717
.name = "cachefiles",
18+
.acquire_volume = cachefiles_acquire_volume,
19+
.free_volume = cachefiles_free_volume,
1820
};

fs/cachefiles/internal.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
struct cachefiles_cache;
2020
struct cachefiles_object;
2121

22+
/*
23+
* Cached volume representation.
24+
*/
25+
struct cachefiles_volume {
26+
struct cachefiles_cache *cache;
27+
struct list_head cache_link; /* Link in cache->volumes */
28+
struct fscache_volume *vcookie; /* The netfs's representation */
29+
struct dentry *dentry; /* The volume dentry */
30+
struct dentry *fanout[256]; /* Fanout subdirs */
31+
};
32+
2233
/*
2334
* Data file records.
2435
*/
@@ -35,6 +46,8 @@ struct cachefiles_cache {
3546
struct dentry *store; /* Directory into which live objects go */
3647
struct dentry *graveyard; /* directory into which dead objects go */
3748
struct file *cachefilesd; /* manager daemon handle */
49+
struct list_head volumes; /* List of volume objects */
50+
spinlock_t object_list_lock; /* Lock for volumes and object_list */
3851
const struct cred *cache_cred; /* security override for accessing cache */
3952
struct mutex daemon_mutex; /* command serialisation mutex */
4053
wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
@@ -163,6 +176,13 @@ static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
163176
revert_creds(saved_cred);
164177
}
165178

179+
/*
180+
* volume.c
181+
*/
182+
void cachefiles_acquire_volume(struct fscache_volume *volume);
183+
void cachefiles_free_volume(struct fscache_volume *volume);
184+
void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
185+
166186
/*
167187
* Error handling
168188
*/

fs/cachefiles/volume.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/* Volume handling.
3+
*
4+
* Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5+
* Written by David Howells ([email protected])
6+
*/
7+
8+
#include <linux/fs.h>
9+
#include <linux/slab.h>
10+
#include "internal.h"
11+
#include <trace/events/fscache.h>
12+
13+
/*
14+
* Allocate and set up a volume representation. We make sure all the fanout
15+
* directories are created and pinned.
16+
*/
17+
void cachefiles_acquire_volume(struct fscache_volume *vcookie)
18+
{
19+
struct cachefiles_volume *volume;
20+
struct cachefiles_cache *cache = vcookie->cache->cache_priv;
21+
const struct cred *saved_cred;
22+
struct dentry *vdentry, *fan;
23+
size_t len;
24+
char *name;
25+
int n_accesses, i;
26+
27+
_enter("");
28+
29+
volume = kzalloc(sizeof(struct cachefiles_volume), GFP_KERNEL);
30+
if (!volume)
31+
return;
32+
volume->vcookie = vcookie;
33+
volume->cache = cache;
34+
INIT_LIST_HEAD(&volume->cache_link);
35+
36+
cachefiles_begin_secure(cache, &saved_cred);
37+
38+
len = vcookie->key[0];
39+
name = kmalloc(len + 3, GFP_NOFS);
40+
if (!name)
41+
goto error_vol;
42+
name[0] = 'I';
43+
memcpy(name + 1, vcookie->key + 1, len);
44+
name[len + 1] = 0;
45+
46+
vdentry = cachefiles_get_directory(cache, cache->store, name, NULL);
47+
if (IS_ERR(vdentry))
48+
goto error_name;
49+
volume->dentry = vdentry;
50+
51+
for (i = 0; i < 256; i++) {
52+
sprintf(name, "@%02x", i);
53+
fan = cachefiles_get_directory(cache, vdentry, name, NULL);
54+
if (IS_ERR(fan))
55+
goto error_fan;
56+
volume->fanout[i] = fan;
57+
}
58+
59+
cachefiles_end_secure(cache, saved_cred);
60+
61+
vcookie->cache_priv = volume;
62+
n_accesses = atomic_inc_return(&vcookie->n_accesses); /* Stop wakeups on dec-to-0 */
63+
trace_fscache_access_volume(vcookie->debug_id, 0,
64+
refcount_read(&vcookie->ref),
65+
n_accesses, fscache_access_cache_pin);
66+
67+
spin_lock(&cache->object_list_lock);
68+
list_add(&volume->cache_link, &volume->cache->volumes);
69+
spin_unlock(&cache->object_list_lock);
70+
71+
kfree(name);
72+
return;
73+
74+
error_fan:
75+
for (i = 0; i < 256; i++)
76+
cachefiles_put_directory(volume->fanout[i]);
77+
cachefiles_put_directory(volume->dentry);
78+
error_name:
79+
kfree(name);
80+
error_vol:
81+
kfree(volume);
82+
cachefiles_end_secure(cache, saved_cred);
83+
}
84+
85+
/*
86+
* Release a volume representation.
87+
*/
88+
static void __cachefiles_free_volume(struct cachefiles_volume *volume)
89+
{
90+
int i;
91+
92+
_enter("");
93+
94+
volume->vcookie->cache_priv = NULL;
95+
96+
for (i = 0; i < 256; i++)
97+
cachefiles_put_directory(volume->fanout[i]);
98+
cachefiles_put_directory(volume->dentry);
99+
kfree(volume);
100+
}
101+
102+
void cachefiles_free_volume(struct fscache_volume *vcookie)
103+
{
104+
struct cachefiles_volume *volume = vcookie->cache_priv;
105+
106+
if (volume) {
107+
spin_lock(&volume->cache->object_list_lock);
108+
list_del_init(&volume->cache_link);
109+
spin_unlock(&volume->cache->object_list_lock);
110+
__cachefiles_free_volume(volume);
111+
}
112+
}
113+
114+
void cachefiles_withdraw_volume(struct cachefiles_volume *volume)
115+
{
116+
fscache_withdraw_volume(volume->vcookie);
117+
__cachefiles_free_volume(volume);
118+
}

0 commit comments

Comments
 (0)