cache.c 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * V9FS cache definitions.
 *
 *  Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/jiffies.h>
#include <linux/file.h>
25
#include <linux/slab.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <linux/stat.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <net/9p/9p.h>

#include "v9fs.h"
#include "cache.h"

#define CACHETAG_LEN  11

struct fscache_netfs v9fs_cache_netfs = {
	.name 		= "9p",
	.version 	= 0,
};

/**
 * v9fs_random_cachetag - Generate a random tag to be associated
 *			  with a new cache session.
 *
 * The value of jiffies is used for a fairly randomly cache tag.
 */

static
int v9fs_random_cachetag(struct v9fs_session_info *v9ses)
{
	v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL);
	if (!v9ses->cachetag)
		return -ENOMEM;

	return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies);
}

const struct fscache_cookie_def v9fs_cache_session_index_def = {
A
Aneesh Kumar K.V 已提交
59 60
	.name		= "9P.session",
	.type		= FSCACHE_COOKIE_TYPE_INDEX,
61 62 63 64 65
};

void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
{
	/* If no cache session tag was specified, we generate a random one. */
66 67 68
	if (!v9ses->cachetag) {
		if (v9fs_random_cachetag(v9ses) < 0) {
			v9ses->fscache = NULL;
69 70
			kfree(v9ses->cachetag);
			v9ses->cachetag = NULL;
71 72 73
			return;
		}
	}
74 75 76

	v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
						&v9fs_cache_session_index_def,
77 78 79
						v9ses->cachetag,
						strlen(v9ses->cachetag),
						NULL, 0,
80
						v9ses, 0, true);
81 82
	p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
		 v9ses, v9ses->fscache);
83 84 85 86
}

void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
{
87 88
	p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
		 v9ses, v9ses->fscache);
89
	fscache_relinquish_cookie(v9ses->fscache, NULL, false);
90 91 92 93 94 95
	v9ses->fscache = NULL;
}

static enum
fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
					    const void *buffer,
96 97
					    uint16_t buflen,
					    loff_t object_size)
98
{
A
Aneesh Kumar K.V 已提交
99
	const struct v9fs_inode *v9inode = cookie_netfs_data;
100

101
	if (buflen != sizeof(v9inode->qid.version))
102 103
		return FSCACHE_CHECKAUX_OBSOLETE;

104 105
	if (memcmp(buffer, &v9inode->qid.version,
		   sizeof(v9inode->qid.version)))
106 107 108 109 110 111 112 113 114 115 116 117 118
		return FSCACHE_CHECKAUX_OBSOLETE;

	return FSCACHE_CHECKAUX_OKAY;
}

const struct fscache_cookie_def v9fs_cache_inode_index_def = {
	.name		= "9p.inode",
	.type		= FSCACHE_COOKIE_TYPE_DATAFILE,
	.check_aux	= v9fs_cache_inode_check_aux,
};

void v9fs_cache_inode_get_cookie(struct inode *inode)
{
A
Aneesh Kumar K.V 已提交
119
	struct v9fs_inode *v9inode;
120 121 122 123 124
	struct v9fs_session_info *v9ses;

	if (!S_ISREG(inode->i_mode))
		return;

A
Aneesh Kumar K.V 已提交
125 126
	v9inode = V9FS_I(inode);
	if (v9inode->fscache)
127 128 129
		return;

	v9ses = v9fs_inode2v9ses(inode);
A
Aneesh Kumar K.V 已提交
130
	v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
131
						  &v9fs_cache_inode_index_def,
132 133 134 135
						  &v9inode->qid.path,
						  sizeof(v9inode->qid.path),
						  &v9inode->qid.version,
						  sizeof(v9inode->qid.version),
136 137 138
						  v9inode,
						  i_size_read(&v9inode->vfs_inode),
						  true);
139

140 141
	p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
		 inode, v9inode->fscache);
142 143 144 145
}

void v9fs_cache_inode_put_cookie(struct inode *inode)
{
A
Aneesh Kumar K.V 已提交
146
	struct v9fs_inode *v9inode = V9FS_I(inode);
147

A
Aneesh Kumar K.V 已提交
148
	if (!v9inode->fscache)
149
		return;
150 151
	p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
		 inode, v9inode->fscache);
152

153 154
	fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version,
				  false);
A
Aneesh Kumar K.V 已提交
155
	v9inode->fscache = NULL;
156 157 158 159
}

void v9fs_cache_inode_flush_cookie(struct inode *inode)
{
A
Aneesh Kumar K.V 已提交
160
	struct v9fs_inode *v9inode = V9FS_I(inode);
161

A
Aneesh Kumar K.V 已提交
162
	if (!v9inode->fscache)
163
		return;
164 165
	p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
		 inode, v9inode->fscache);
166

167
	fscache_relinquish_cookie(v9inode->fscache, NULL, true);
A
Aneesh Kumar K.V 已提交
168
	v9inode->fscache = NULL;
169 170 171 172
}

void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
{
A
Aneesh Kumar K.V 已提交
173
	struct v9fs_inode *v9inode = V9FS_I(inode);
174

A
Aneesh Kumar K.V 已提交
175
	if (!v9inode->fscache)
176 177
		return;

178
	mutex_lock(&v9inode->fscache_lock);
179

180 181 182 183 184
	if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
		v9fs_cache_inode_flush_cookie(inode);
	else
		v9fs_cache_inode_get_cookie(inode);

185
	mutex_unlock(&v9inode->fscache_lock);
186 187 188 189
}

void v9fs_cache_inode_reset_cookie(struct inode *inode)
{
A
Aneesh Kumar K.V 已提交
190
	struct v9fs_inode *v9inode = V9FS_I(inode);
191 192 193
	struct v9fs_session_info *v9ses;
	struct fscache_cookie *old;

A
Aneesh Kumar K.V 已提交
194
	if (!v9inode->fscache)
195 196
		return;

A
Aneesh Kumar K.V 已提交
197
	old = v9inode->fscache;
198

199
	mutex_lock(&v9inode->fscache_lock);
200
	fscache_relinquish_cookie(v9inode->fscache, NULL, true);
201 202

	v9ses = v9fs_inode2v9ses(inode);
A
Aneesh Kumar K.V 已提交
203
	v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
204
						  &v9fs_cache_inode_index_def,
205 206 207 208
						  &v9inode->qid.path,
						  sizeof(v9inode->qid.path),
						  &v9inode->qid.version,
						  sizeof(v9inode->qid.version),
209 210 211
						  v9inode,
						  i_size_read(&v9inode->vfs_inode),
						  true);
212 213
	p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
		 inode, old, v9inode->fscache);
214

215
	mutex_unlock(&v9inode->fscache_lock);
216 217 218 219 220
}

int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
{
	struct inode *inode = page->mapping->host;
A
Aneesh Kumar K.V 已提交
221
	struct v9fs_inode *v9inode = V9FS_I(inode);
222

A
Aneesh Kumar K.V 已提交
223
	BUG_ON(!v9inode->fscache);
224

A
Aneesh Kumar K.V 已提交
225
	return fscache_maybe_release_page(v9inode->fscache, page, gfp);
226 227 228 229 230
}

void __v9fs_fscache_invalidate_page(struct page *page)
{
	struct inode *inode = page->mapping->host;
A
Aneesh Kumar K.V 已提交
231
	struct v9fs_inode *v9inode = V9FS_I(inode);
232

A
Aneesh Kumar K.V 已提交
233
	BUG_ON(!v9inode->fscache);
234 235

	if (PageFsCache(page)) {
A
Aneesh Kumar K.V 已提交
236
		fscache_wait_on_page_write(v9inode->fscache, page);
237
		BUG_ON(!PageLocked(page));
A
Aneesh Kumar K.V 已提交
238
		fscache_uncache_page(v9inode->fscache, page);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	}
}

static void v9fs_vfs_readpage_complete(struct page *page, void *data,
				       int error)
{
	if (!error)
		SetPageUptodate(page);

	unlock_page(page);
}

/**
 * __v9fs_readpage_from_fscache - read a page from cache
 *
 * Returns 0 if the pages are in cache and a BIO is submitted,
 * 1 if the pages are not in cache and -error otherwise.
 */

int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
{
	int ret;
A
Aneesh Kumar K.V 已提交
261
	const struct v9fs_inode *v9inode = V9FS_I(inode);
262

263
	p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
A
Aneesh Kumar K.V 已提交
264
	if (!v9inode->fscache)
265 266
		return -ENOBUFS;

A
Aneesh Kumar K.V 已提交
267
	ret = fscache_read_or_alloc_page(v9inode->fscache,
268 269 270 271 272 273 274
					 page,
					 v9fs_vfs_readpage_complete,
					 NULL,
					 GFP_KERNEL);
	switch (ret) {
	case -ENOBUFS:
	case -ENODATA:
275
		p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
276 277
		return 1;
	case 0:
278
		p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
279 280
		return ret;
	default:
281
		p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		return ret;
	}
}

/**
 * __v9fs_readpages_from_fscache - read multiple pages from cache
 *
 * Returns 0 if the pages are in cache and a BIO is submitted,
 * 1 if the pages are not in cache and -error otherwise.
 */

int __v9fs_readpages_from_fscache(struct inode *inode,
				  struct address_space *mapping,
				  struct list_head *pages,
				  unsigned *nr_pages)
{
	int ret;
A
Aneesh Kumar K.V 已提交
299
	const struct v9fs_inode *v9inode = V9FS_I(inode);
300

301
	p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
A
Aneesh Kumar K.V 已提交
302
	if (!v9inode->fscache)
303 304
		return -ENOBUFS;

A
Aneesh Kumar K.V 已提交
305
	ret = fscache_read_or_alloc_pages(v9inode->fscache,
306 307 308 309 310 311 312
					  mapping, pages, nr_pages,
					  v9fs_vfs_readpage_complete,
					  NULL,
					  mapping_gfp_mask(mapping));
	switch (ret) {
	case -ENOBUFS:
	case -ENODATA:
313
		p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
314 315 316 317
		return 1;
	case 0:
		BUG_ON(!list_empty(pages));
		BUG_ON(*nr_pages != 0);
318
		p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
319 320
		return ret;
	default:
321
		p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
322 323 324 325 326 327 328 329 330 331 332 333
		return ret;
	}
}

/**
 * __v9fs_readpage_to_fscache - write a page to the cache
 *
 */

void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
{
	int ret;
A
Aneesh Kumar K.V 已提交
334
	const struct v9fs_inode *v9inode = V9FS_I(inode);
335

336
	p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
337 338
	ret = fscache_write_page(v9inode->fscache, page,
				 i_size_read(&v9inode->vfs_inode), GFP_KERNEL);
339
	p9_debug(P9_DEBUG_FSC, "ret =  %d\n", ret);
340 341 342
	if (ret != 0)
		v9fs_uncache_page(inode, page);
}
343 344 345 346 347 348

/*
 * wait for a page to complete writing to the cache
 */
void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
{
A
Aneesh Kumar K.V 已提交
349
	const struct v9fs_inode *v9inode = V9FS_I(inode);
350
	p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
351
	if (PageFsCache(page))
A
Aneesh Kumar K.V 已提交
352
		fscache_wait_on_page_write(v9inode->fscache, page);
353
}