file.c 6.3 KB
Newer Older
1
/* AFS filesystem file handling
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include "internal.h"

static int afs_file_readpage(struct file *file, struct page *page);
21
static void afs_file_invalidatepage(struct page *page, unsigned long offset);
A
Al Viro 已提交
22
static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
L
Linus Torvalds 已提交
23

D
David Howells 已提交
24 25 26 27 28 29 30 31 32 33
const struct file_operations afs_file_operations = {
	.open		= afs_open,
	.release	= afs_release,
	.llseek		= generic_file_llseek,
	.read		= do_sync_read,
	.aio_read	= generic_file_aio_read,
	.mmap		= generic_file_readonly_mmap,
	.sendfile	= generic_file_sendfile,
};

34
const struct inode_operations afs_file_inode_operations = {
L
Linus Torvalds 已提交
35
	.getattr	= afs_inode_getattr,
D
David Howells 已提交
36
	.permission	= afs_permission,
L
Linus Torvalds 已提交
37 38
};

39
const struct address_space_operations afs_fs_aops = {
L
Linus Torvalds 已提交
40 41 42 43 44 45
	.readpage	= afs_file_readpage,
	.set_page_dirty	= __set_page_dirty_nobuffers,
	.releasepage	= afs_file_releasepage,
	.invalidatepage	= afs_file_invalidatepage,
};

D
David Howells 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*
 * open an AFS file or directory and attach a key to it
 */
int afs_open(struct inode *inode, struct file *file)
{
	struct afs_vnode *vnode = AFS_FS_I(inode);
	struct key *key;

	_enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);

	key = afs_request_key(vnode->volume->cell);
	if (IS_ERR(key)) {
		_leave(" = %ld [key]", PTR_ERR(key));
		return PTR_ERR(key);
	}

	file->private_data = key;
	_leave(" = 0");
	return 0;
}

/*
 * release an AFS file or directory and discard its key
 */
int afs_release(struct inode *inode, struct file *file)
{
	struct afs_vnode *vnode = AFS_FS_I(inode);

	_enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);

	key_put(file->private_data);
	_leave(" = 0");
	return 0;
}

L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
 * deal with notification that a page was read from the cache
 */
#ifdef AFS_CACHING_SUPPORT
static void afs_file_readpage_read_complete(void *cookie_data,
					    struct page *page,
					    void *data,
					    int error)
{
	_enter("%p,%p,%p,%d", cookie_data, page, data, error);

	if (error)
		SetPageError(page);
	else
		SetPageUptodate(page);
	unlock_page(page);

D
David Howells 已提交
98
}
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
#endif

/*
 * deal with notification that a page was written to the cache
 */
#ifdef AFS_CACHING_SUPPORT
static void afs_file_readpage_write_complete(void *cookie_data,
					     struct page *page,
					     void *data,
					     int error)
{
	_enter("%p,%p,%p,%d", cookie_data, page, data, error);

	unlock_page(page);
D
David Howells 已提交
113
}
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122
#endif

/*
 * AFS read page from file (or symlink)
 */
static int afs_file_readpage(struct file *file, struct page *page)
{
	struct afs_vnode *vnode;
	struct inode *inode;
D
David Howells 已提交
123
	struct key *key;
124 125
	size_t len;
	off_t offset;
L
Linus Torvalds 已提交
126 127 128 129
	int ret;

	inode = page->mapping->host;

D
David Howells 已提交
130 131 132 133 134
	ASSERT(file != NULL);
	key = file->private_data;
	ASSERT(key != NULL);

	_enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
L
Linus Torvalds 已提交
135 136 137

	vnode = AFS_FS_I(inode);

M
Matt Mackall 已提交
138
	BUG_ON(!PageLocked(page));
L
Linus Torvalds 已提交
139 140

	ret = -ESTALE;
141
	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		goto error;

#ifdef AFS_CACHING_SUPPORT
	/* is it cached? */
	ret = cachefs_read_or_alloc_page(vnode->cache,
					 page,
					 afs_file_readpage_read_complete,
					 NULL,
					 GFP_KERNEL);
#else
	ret = -ENOBUFS;
#endif

	switch (ret) {
		/* read BIO submitted and wb-journal entry found */
	case 1:
		BUG(); // TODO - handle wb-journal match

		/* read BIO submitted (page in cache) */
	case 0:
		break;

		/* no page available in cache */
	case -ENOBUFS:
	case -ENODATA:
	default:
168 169
		offset = page->index << PAGE_CACHE_SHIFT;
		len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
L
Linus Torvalds 已提交
170 171 172

		/* read the contents of the file from the server into the
		 * page */
D
David Howells 已提交
173
		ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
L
Linus Torvalds 已提交
174
		if (ret < 0) {
175
			if (ret == -ENOENT) {
L
Linus Torvalds 已提交
176 177
				_debug("got NOENT from server"
				       " - marking file deleted and stale");
178
				set_bit(AFS_VNODE_DELETED, &vnode->flags);
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
				ret = -ESTALE;
			}
#ifdef AFS_CACHING_SUPPORT
			cachefs_uncache_page(vnode->cache, page);
#endif
			goto error;
		}

		SetPageUptodate(page);

#ifdef AFS_CACHING_SUPPORT
		if (cachefs_write_page(vnode->cache,
				       page,
				       afs_file_readpage_write_complete,
				       NULL,
				       GFP_KERNEL) != 0
		    ) {
			cachefs_uncache_page(vnode->cache, page);
			unlock_page(page);
		}
#else
		unlock_page(page);
#endif
	}

	_leave(" = 0");
	return 0;

207
error:
L
Linus Torvalds 已提交
208 209 210 211
	SetPageError(page);
	unlock_page(page);
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
212
}
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

/*
 * get a page cookie for the specified page
 */
#ifdef AFS_CACHING_SUPPORT
int afs_cache_get_page_cookie(struct page *page,
			      struct cachefs_page **_page_cookie)
{
	int ret;

	_enter("");
	ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);

	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
228
}
L
Linus Torvalds 已提交
229 230 231 232 233
#endif

/*
 * invalidate part or all of a page
 */
234
static void afs_file_invalidatepage(struct page *page, unsigned long offset)
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
	int ret = 1;

	_enter("{%lu},%lu", page->index, offset);

	BUG_ON(!PageLocked(page));

	if (PagePrivate(page)) {
#ifdef AFS_CACHING_SUPPORT
		struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
		cachefs_uncache_page(vnode->cache,page);
#endif

		/* We release buffers only if the entire page is being
		 * invalidated.
		 * The get_block cached value has been unconditionally
		 * invalidated, so real IO is not possible anymore.
		 */
		if (offset == 0) {
			BUG_ON(!PageLocked(page));

			ret = 0;
			if (!PageWriteback(page))
				ret = page->mapping->a_ops->releasepage(page,
									0);
260
			/* possibly should BUG_ON(!ret); - neilb */
L
Linus Torvalds 已提交
261 262 263 264
		}
	}

	_leave(" = %d", ret);
D
David Howells 已提交
265
}
L
Linus Torvalds 已提交
266 267 268 269

/*
 * release a page and cleanup its private data
 */
A
Al Viro 已提交
270
static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281
{
	struct cachefs_page *pageio;

	_enter("{%lu},%x", page->index, gfp_flags);

	if (PagePrivate(page)) {
#ifdef AFS_CACHING_SUPPORT
		struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
		cachefs_uncache_page(vnode->cache, page);
#endif

H
Hugh Dickins 已提交
282 283
		pageio = (struct cachefs_page *) page_private(page);
		set_page_private(page, 0);
L
Linus Torvalds 已提交
284 285
		ClearPagePrivate(page);

J
Jesper Juhl 已提交
286
		kfree(pageio);
L
Linus Torvalds 已提交
287 288 289 290
	}

	_leave(" = 0");
	return 0;
D
David Howells 已提交
291
}