cache.c 5.3 KB
Newer Older
1 2
/* AFS caching stuff
 *
D
David Howells 已提交
3
 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 5 6 7 8 9 10 11
 * 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.
 */

D
David Howells 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <linux/sched.h>
#include "internal.h"

static uint16_t afs_cell_cache_get_key(const void *cookie_netfs_data,
				       void *buffer, uint16_t buflen);
static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data,
					 void *buffer, uint16_t buflen);

static uint16_t afs_vnode_cache_get_key(const void *cookie_netfs_data,
					void *buffer, uint16_t buflen);
static void afs_vnode_cache_get_attr(const void *cookie_netfs_data,
				     uint64_t *size);
static uint16_t afs_vnode_cache_get_aux(const void *cookie_netfs_data,
					void *buffer, uint16_t buflen);
static enum fscache_checkaux afs_vnode_cache_check_aux(void *cookie_netfs_data,
						       const void *buffer,
						       uint16_t buflen);

struct fscache_netfs afs_cache_netfs = {
	.name			= "afs",
32
	.version		= 1,
D
David Howells 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
};

struct fscache_cookie_def afs_cell_cache_index_def = {
	.name		= "AFS.cell",
	.type		= FSCACHE_COOKIE_TYPE_INDEX,
	.get_key	= afs_cell_cache_get_key,
};

struct fscache_cookie_def afs_volume_cache_index_def = {
	.name		= "AFS.volume",
	.type		= FSCACHE_COOKIE_TYPE_INDEX,
	.get_key	= afs_volume_cache_get_key,
};

struct fscache_cookie_def afs_vnode_cache_index_def = {
	.name			= "AFS.vnode",
	.type			= FSCACHE_COOKIE_TYPE_DATAFILE,
	.get_key		= afs_vnode_cache_get_key,
	.get_attr		= afs_vnode_cache_get_attr,
	.get_aux		= afs_vnode_cache_get_aux,
	.check_aux		= afs_vnode_cache_check_aux,
54 55 56
};

/*
D
David Howells 已提交
57
 * set the key for the index entry
58
 */
D
David Howells 已提交
59 60
static uint16_t afs_cell_cache_get_key(const void *cookie_netfs_data,
				       void *buffer, uint16_t bufmax)
61
{
D
David Howells 已提交
62 63
	const struct afs_cell *cell = cookie_netfs_data;
	uint16_t klen;
64

D
David Howells 已提交
65
	_enter("%p,%p,%u", cell, buffer, bufmax);
66

D
David Howells 已提交
67 68 69
	klen = strlen(cell->name);
	if (klen > bufmax)
		return 0;
70

D
David Howells 已提交
71 72
	memcpy(buffer, cell->name, klen);
	return klen;
73 74
}

D
David Howells 已提交
75
/*****************************************************************************/
76
/*
D
David Howells 已提交
77
 * set the key for the volume index entry
78
 */
D
David Howells 已提交
79
static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data,
80
					 void *buffer, uint16_t bufmax)
81
{
D
David Howells 已提交
82
	const struct afs_volume *volume = cookie_netfs_data;
83 84 85
	struct {
		u64 volid;
	} __packed key;
D
David Howells 已提交
86 87 88

	_enter("{%u},%p,%u", volume->type, buffer, bufmax);

89
	if (bufmax < sizeof(key))
D
David Howells 已提交
90
		return 0;
91

92 93 94
	key.volid = volume->vid;
	memcpy(buffer, &key, sizeof(key));
	return sizeof(key);
95 96
}

D
David Howells 已提交
97
/*****************************************************************************/
98
/*
D
David Howells 已提交
99
 * set the key for the index entry
100
 */
D
David Howells 已提交
101 102
static uint16_t afs_vnode_cache_get_key(const void *cookie_netfs_data,
					void *buffer, uint16_t bufmax)
103
{
D
David Howells 已提交
104
	const struct afs_vnode *vnode = cookie_netfs_data;
105 106 107
	struct {
		u32 vnode_id[3];
	} __packed key;
108

D
David Howells 已提交
109 110 111
	_enter("{%x,%x,%llx},%p,%u",
	       vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version,
	       buffer, bufmax);
112

113 114 115 116 117
	/* Allow for a 96-bit key */
	memset(&key, 0, sizeof(key));
	key.vnode_id[0] = vnode->fid.vnode;
	key.vnode_id[1] = 0;
	key.vnode_id[2] = 0;
D
David Howells 已提交
118

119 120
	if (sizeof(key) > bufmax)
		return 0;
121

122 123
	memcpy(buffer, &key, sizeof(key));
	return sizeof(key);
124 125 126
}

/*
D
David Howells 已提交
127
 * provide updated file attributes
128
 */
D
David Howells 已提交
129 130
static void afs_vnode_cache_get_attr(const void *cookie_netfs_data,
				     uint64_t *size)
131
{
D
David Howells 已提交
132
	const struct afs_vnode *vnode = cookie_netfs_data;
133

D
David Howells 已提交
134 135 136
	_enter("{%x,%x,%llx},",
	       vnode->fid.vnode, vnode->fid.unique,
	       vnode->status.data_version);
137

D
David Howells 已提交
138
	*size = vnode->status.size;
139 140
}

141 142 143 144 145
struct afs_vnode_cache_aux {
	u64 data_version;
	u32 fid_unique;
} __packed;

146
/*
L
Lucas De Marchi 已提交
147
 * provide new auxiliary cache data
D
David Howells 已提交
148 149 150 151 152
 */
static uint16_t afs_vnode_cache_get_aux(const void *cookie_netfs_data,
					void *buffer, uint16_t bufmax)
{
	const struct afs_vnode *vnode = cookie_netfs_data;
153
	struct afs_vnode_cache_aux aux;
D
David Howells 已提交
154 155 156 157 158

	_enter("{%x,%x,%Lx},%p,%u",
	       vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version,
	       buffer, bufmax);

159 160 161
	memset(&aux, 0, sizeof(aux));
	aux.data_version = vnode->status.data_version;
	aux.fid_unique = vnode->fid.unique;
D
David Howells 已提交
162

163 164
	if (bufmax < sizeof(aux))
		return 0;
D
David Howells 已提交
165

166 167
	memcpy(buffer, &aux, sizeof(aux));
	return sizeof(aux);
D
David Howells 已提交
168 169 170
}

/*
L
Lucas De Marchi 已提交
171
 * check that the auxiliary data indicates that the entry is still valid
172
 */
D
David Howells 已提交
173 174 175
static enum fscache_checkaux afs_vnode_cache_check_aux(void *cookie_netfs_data,
						       const void *buffer,
						       uint16_t buflen)
176
{
D
David Howells 已提交
177
	struct afs_vnode *vnode = cookie_netfs_data;
178
	struct afs_vnode_cache_aux aux;
D
David Howells 已提交
179 180 181 182 183

	_enter("{%x,%x,%llx},%p,%u",
	       vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version,
	       buffer, buflen);

184 185
	memcpy(&aux, buffer, sizeof(aux));

D
David Howells 已提交
186
	/* check the size of the data is what we're expecting */
187 188
	if (buflen != sizeof(aux)) {
		_leave(" = OBSOLETE [len %hx != %zx]", buflen, sizeof(aux));
D
David Howells 已提交
189
		return FSCACHE_CHECKAUX_OBSOLETE;
190 191
	}

192
	if (vnode->fid.unique != aux.fid_unique) {
D
David Howells 已提交
193
		_leave(" = OBSOLETE [uniq %x != %x]",
194
		       aux.fid_unique, vnode->fid.unique);
D
David Howells 已提交
195 196 197
		return FSCACHE_CHECKAUX_OBSOLETE;
	}

198
	if (vnode->status.data_version != aux.data_version) {
D
David Howells 已提交
199
		_leave(" = OBSOLETE [vers %llx != %llx]",
200
		       aux.data_version, vnode->status.data_version);
D
David Howells 已提交
201
		return FSCACHE_CHECKAUX_OBSOLETE;
202 203 204
	}

	_leave(" = SUCCESS");
D
David Howells 已提交
205
	return FSCACHE_CHECKAUX_OKAY;
206
}