file.c 6.4 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
 * 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>
18
#include <linux/writeback.h>
L
Linus Torvalds 已提交
19 20
#include "internal.h"

D
David Howells 已提交
21 22 23
static int afs_readpage(struct file *file, struct page *page);
static void afs_invalidatepage(struct page *page, unsigned long offset);
static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24
static int afs_launder_page(struct page *page);
L
Linus Torvalds 已提交
25

D
David Howells 已提交
26 27 28 29 30
const struct file_operations afs_file_operations = {
	.open		= afs_open,
	.release	= afs_release,
	.llseek		= generic_file_llseek,
	.read		= do_sync_read,
31
	.write		= do_sync_write,
D
David Howells 已提交
32
	.aio_read	= generic_file_aio_read,
33
	.aio_write	= afs_file_write,
D
David Howells 已提交
34 35
	.mmap		= generic_file_readonly_mmap,
	.sendfile	= generic_file_sendfile,
36
	.fsync		= afs_fsync,
D
David Howells 已提交
37 38
};

39
const struct inode_operations afs_file_inode_operations = {
D
David Howells 已提交
40
	.getattr	= afs_getattr,
41
	.setattr	= afs_setattr,
D
David Howells 已提交
42
	.permission	= afs_permission,
L
Linus Torvalds 已提交
43 44
};

45
const struct address_space_operations afs_fs_aops = {
D
David Howells 已提交
46
	.readpage	= afs_readpage,
47 48
	.set_page_dirty	= afs_set_page_dirty,
	.launder_page	= afs_launder_page,
D
David Howells 已提交
49 50
	.releasepage	= afs_releasepage,
	.invalidatepage	= afs_invalidatepage,
51 52 53 54
	.prepare_write	= afs_prepare_write,
	.commit_write	= afs_commit_write,
	.writepage	= afs_writepage,
	.writepages	= afs_writepages,
L
Linus Torvalds 已提交
55 56
};

D
David Howells 已提交
57 58 59 60 61 62 63
/*
 * 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;
64
	int ret;
D
David Howells 已提交
65

D
David Howells 已提交
66
	_enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
D
David Howells 已提交
67 68 69 70 71 72 73

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

74 75 76 77 78 79
	ret = afs_validate(vnode, key);
	if (ret < 0) {
		_leave(" = %d [val]", ret);
		return ret;
	}

D
David Howells 已提交
80 81 82 83 84 85 86 87 88 89 90 91
	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);

D
David Howells 已提交
92
	_enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
D
David Howells 已提交
93 94 95 96 97 98

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

L
Linus Torvalds 已提交
99 100 101 102
/*
 * deal with notification that a page was read from the cache
 */
#ifdef AFS_CACHING_SUPPORT
D
David Howells 已提交
103 104 105 106
static void afs_readpage_read_complete(void *cookie_data,
				       struct page *page,
				       void *data,
				       int error)
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115
{
	_enter("%p,%p,%p,%d", cookie_data, page, data, error);

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

D
David Howells 已提交
116
}
L
Linus Torvalds 已提交
117 118 119 120 121 122
#endif

/*
 * deal with notification that a page was written to the cache
 */
#ifdef AFS_CACHING_SUPPORT
D
David Howells 已提交
123 124 125 126
static void afs_readpage_write_complete(void *cookie_data,
					struct page *page,
					void *data,
					int error)
L
Linus Torvalds 已提交
127 128 129 130
{
	_enter("%p,%p,%p,%d", cookie_data, page, data, error);

	unlock_page(page);
D
David Howells 已提交
131
}
L
Linus Torvalds 已提交
132 133 134
#endif

/*
D
David Howells 已提交
135
 * AFS read page from file, directory or symlink
L
Linus Torvalds 已提交
136
 */
D
David Howells 已提交
137
static int afs_readpage(struct file *file, struct page *page)
L
Linus Torvalds 已提交
138 139 140
{
	struct afs_vnode *vnode;
	struct inode *inode;
D
David Howells 已提交
141
	struct key *key;
142 143
	size_t len;
	off_t offset;
L
Linus Torvalds 已提交
144 145 146 147
	int ret;

	inode = page->mapping->host;

D
David Howells 已提交
148 149 150 151 152
	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 已提交
153 154 155

	vnode = AFS_FS_I(inode);

M
Matt Mackall 已提交
156
	BUG_ON(!PageLocked(page));
L
Linus Torvalds 已提交
157 158

	ret = -ESTALE;
159
	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		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:
186 187
		offset = page->index << PAGE_CACHE_SHIFT;
		len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
L
Linus Torvalds 已提交
188 189 190

		/* read the contents of the file from the server into the
		 * page */
D
David Howells 已提交
191
		ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
L
Linus Torvalds 已提交
192
		if (ret < 0) {
193
			if (ret == -ENOENT) {
L
Linus Torvalds 已提交
194 195
				_debug("got NOENT from server"
				       " - marking file deleted and stale");
196
				set_bit(AFS_VNODE_DELETED, &vnode->flags);
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
				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;

225
error:
L
Linus Torvalds 已提交
226 227 228 229
	SetPageError(page);
	unlock_page(page);
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
230
}
L
Linus Torvalds 已提交
231 232 233 234

/*
 * invalidate part or all of a page
 */
D
David Howells 已提交
235
static void afs_invalidatepage(struct page *page, unsigned long offset)
L
Linus Torvalds 已提交
236 237 238
{
	int ret = 1;

239
	_enter("{%lu},%lu", page->index, offset);
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

	BUG_ON(!PageLocked(page));

	if (PagePrivate(page)) {
		/* 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);
256
			/* possibly should BUG_ON(!ret); - neilb */
L
Linus Torvalds 已提交
257 258 259 260
		}
	}

	_leave(" = %d", ret);
D
David Howells 已提交
261
}
L
Linus Torvalds 已提交
262

263 264 265 266 267 268 269 270 271 272
/*
 * write back a dirty page
 */
static int afs_launder_page(struct page *page)
{
	_enter("{%lu}", page->index);

	return 0;
}

L
Linus Torvalds 已提交
273 274 275
/*
 * release a page and cleanup its private data
 */
D
David Howells 已提交
276
static int afs_releasepage(struct page *page, gfp_t gfp_flags)
L
Linus Torvalds 已提交
277
{
D
David Howells 已提交
278
	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
279
	struct afs_writeback *wb;
L
Linus Torvalds 已提交
280

D
David Howells 已提交
281 282 283
	_enter("{{%x:%u}[%lu],%lx},%x",
	       vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
	       gfp_flags);
L
Linus Torvalds 已提交
284 285

	if (PagePrivate(page)) {
286 287
		wb = (struct afs_writeback *) page_private(page);
		ASSERT(wb != NULL);
H
Hugh Dickins 已提交
288
		set_page_private(page, 0);
L
Linus Torvalds 已提交
289
		ClearPagePrivate(page);
290
		afs_put_writeback(wb);
L
Linus Torvalds 已提交
291 292 293 294
	}

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