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

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

D
David Howells 已提交
27 28 29
static int afs_readpages(struct file *filp, struct address_space *mapping,
			 struct list_head *pages, unsigned nr_pages);

D
David Howells 已提交
30 31 32 33
const struct file_operations afs_file_operations = {
	.open		= afs_open,
	.release	= afs_release,
	.llseek		= generic_file_llseek,
34
	.read		= new_sync_read,
35
	.write		= do_sync_write,
36
	.read_iter	= generic_file_read_iter,
37
	.aio_write	= afs_file_write,
D
David Howells 已提交
38
	.mmap		= generic_file_readonly_mmap,
39
	.splice_read	= generic_file_splice_read,
40
	.fsync		= afs_fsync,
D
David Howells 已提交
41 42
	.lock		= afs_lock,
	.flock		= afs_flock,
D
David Howells 已提交
43 44
};

45
const struct inode_operations afs_file_inode_operations = {
D
David Howells 已提交
46
	.getattr	= afs_getattr,
47
	.setattr	= afs_setattr,
D
David Howells 已提交
48
	.permission	= afs_permission,
L
Linus Torvalds 已提交
49 50
};

51
const struct address_space_operations afs_fs_aops = {
D
David Howells 已提交
52
	.readpage	= afs_readpage,
D
David Howells 已提交
53
	.readpages	= afs_readpages,
54 55
	.set_page_dirty	= afs_set_page_dirty,
	.launder_page	= afs_launder_page,
D
David Howells 已提交
56 57
	.releasepage	= afs_releasepage,
	.invalidatepage	= afs_invalidatepage,
N
Nick Piggin 已提交
58 59
	.write_begin	= afs_write_begin,
	.write_end	= afs_write_end,
60 61
	.writepage	= afs_writepage,
	.writepages	= afs_writepages,
L
Linus Torvalds 已提交
62 63
};

D
David Howells 已提交
64 65 66 67 68 69 70
/*
 * 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;
71
	int ret;
D
David Howells 已提交
72

D
David Howells 已提交
73
	_enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
D
David Howells 已提交
74 75 76 77 78 79 80

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

81 82 83 84 85 86
	ret = afs_validate(vnode, key);
	if (ret < 0) {
		_leave(" = %d [val]", ret);
		return ret;
	}

D
David Howells 已提交
87 88 89 90 91 92 93 94 95 96 97 98
	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 已提交
99
	_enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
D
David Howells 已提交
100 101 102 103 104 105

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

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

D
David Howells 已提交
116 117 118
	/* if the read completes with an error, we just unlock the page and let
	 * the VM reissue the readpage */
	if (!error)
L
Linus Torvalds 已提交
119 120
		SetPageUptodate(page);
	unlock_page(page);
D
David Howells 已提交
121
}
122
#endif
L
Linus Torvalds 已提交
123 124

/*
125
 * read page from file, directory or symlink, given a key to use
L
Linus Torvalds 已提交
126
 */
127
int afs_page_filler(void *data, struct page *page)
L
Linus Torvalds 已提交
128
{
129 130 131
	struct inode *inode = page->mapping->host;
	struct afs_vnode *vnode = AFS_FS_I(inode);
	struct key *key = data;
132 133
	size_t len;
	off_t offset;
L
Linus Torvalds 已提交
134 135
	int ret;

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

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
		goto error;

	/* is it cached? */
D
David Howells 已提交
145 146
#ifdef CONFIG_AFS_FSCACHE
	ret = fscache_read_or_alloc_page(vnode->cache,
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158
					 page,
					 afs_file_readpage_read_complete,
					 NULL,
					 GFP_KERNEL);
#else
	ret = -ENOBUFS;
#endif
	switch (ret) {
		/* read BIO submitted (page in cache) */
	case 0:
		break;

D
David Howells 已提交
159
		/* page not yet cached */
L
Linus Torvalds 已提交
160
	case -ENODATA:
D
David Howells 已提交
161 162 163 164 165 166
		_debug("cache said ENODATA");
		goto go_on;

		/* page will not be cached */
	case -ENOBUFS:
		_debug("cache said ENOBUFS");
L
Linus Torvalds 已提交
167
	default:
D
David Howells 已提交
168
	go_on:
169 170
		offset = page->index << PAGE_CACHE_SHIFT;
		len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
L
Linus Torvalds 已提交
171 172 173

		/* read the contents of the file from the server into the
		 * page */
D
David Howells 已提交
174
		ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
L
Linus Torvalds 已提交
175
		if (ret < 0) {
176
			if (ret == -ENOENT) {
L
Linus Torvalds 已提交
177 178
				_debug("got NOENT from server"
				       " - marking file deleted and stale");
179
				set_bit(AFS_VNODE_DELETED, &vnode->flags);
L
Linus Torvalds 已提交
180 181
				ret = -ESTALE;
			}
D
David Howells 已提交
182 183 184

#ifdef CONFIG_AFS_FSCACHE
			fscache_uncache_page(vnode->cache, page);
L
Linus Torvalds 已提交
185
#endif
D
David Howells 已提交
186
			BUG_ON(PageFsCache(page));
L
Linus Torvalds 已提交
187 188 189 190 191
			goto error;
		}

		SetPageUptodate(page);

D
David Howells 已提交
192 193 194 195 196 197
		/* send the page to the cache */
#ifdef CONFIG_AFS_FSCACHE
		if (PageFsCache(page) &&
		    fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
			fscache_uncache_page(vnode->cache, page);
			BUG_ON(PageFsCache(page));
L
Linus Torvalds 已提交
198 199
		}
#endif
D
David Howells 已提交
200
		unlock_page(page);
L
Linus Torvalds 已提交
201 202 203 204 205
	}

	_leave(" = 0");
	return 0;

206
error:
L
Linus Torvalds 已提交
207 208 209 210
	SetPageError(page);
	unlock_page(page);
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
211
}
L
Linus Torvalds 已提交
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/*
 * read page from file, directory or symlink, given a file to nominate the key
 * to be used
 */
static int afs_readpage(struct file *file, struct page *page)
{
	struct key *key;
	int ret;

	if (file) {
		key = file->private_data;
		ASSERT(key != NULL);
		ret = afs_page_filler(key, page);
	} else {
		struct inode *inode = page->mapping->host;
		key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
		if (IS_ERR(key)) {
			ret = PTR_ERR(key);
		} else {
			ret = afs_page_filler(key, page);
			key_put(key);
		}
	}
	return ret;
}

L
Linus Torvalds 已提交
239
/*
D
David Howells 已提交
240
 * read a set of pages
L
Linus Torvalds 已提交
241
 */
D
David Howells 已提交
242 243
static int afs_readpages(struct file *file, struct address_space *mapping,
			 struct list_head *pages, unsigned nr_pages)
L
Linus Torvalds 已提交
244
{
245
	struct key *key = file->private_data;
D
David Howells 已提交
246 247
	struct afs_vnode *vnode;
	int ret = 0;
L
Linus Torvalds 已提交
248

249 250 251 252
	_enter("{%d},{%lu},,%d",
	       key_serial(key), mapping->host->i_ino, nr_pages);

	ASSERT(key != NULL);
L
Linus Torvalds 已提交
253

D
David Howells 已提交
254
	vnode = AFS_FS_I(mapping->host);
255
	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
D
David Howells 已提交
256 257 258
		_leave(" = -ESTALE");
		return -ESTALE;
	}
L
Linus Torvalds 已提交
259

D
David Howells 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	/* attempt to read as many of the pages as possible */
#ifdef CONFIG_AFS_FSCACHE
	ret = fscache_read_or_alloc_pages(vnode->cache,
					  mapping,
					  pages,
					  &nr_pages,
					  afs_file_readpage_read_complete,
					  NULL,
					  mapping_gfp_mask(mapping));
#else
	ret = -ENOBUFS;
#endif

	switch (ret) {
		/* all pages are being read from the cache */
	case 0:
		BUG_ON(!list_empty(pages));
		BUG_ON(nr_pages != 0);
		_leave(" = 0 [reading all]");
		return 0;

		/* there were pages that couldn't be read from the cache */
	case -ENODATA:
	case -ENOBUFS:
		break;

		/* other error */
	default:
		_leave(" = %d", ret);
		return ret;
L
Linus Torvalds 已提交
290 291
	}

D
David Howells 已提交
292
	/* load the missing pages from the network */
293
	ret = read_cache_pages(mapping, pages, afs_page_filler, key);
D
David Howells 已提交
294 295 296

	_leave(" = %d [netting]", ret);
	return ret;
D
David Howells 已提交
297
}
L
Linus Torvalds 已提交
298

299 300 301 302 303 304 305 306 307 308
/*
 * write back a dirty page
 */
static int afs_launder_page(struct page *page)
{
	_enter("{%lu}", page->index);

	return 0;
}

L
Linus Torvalds 已提交
309
/*
D
David Howells 已提交
310 311 312 313
 * invalidate part or all of a page
 * - release a page and clean up its private data if offset is 0 (indicating
 *   the entire page)
 */
314 315
static void afs_invalidatepage(struct page *page, unsigned int offset,
			       unsigned int length)
D
David Howells 已提交
316 317 318
{
	struct afs_writeback *wb = (struct afs_writeback *) page_private(page);

319
	_enter("{%lu},%u,%u", page->index, offset, length);
D
David Howells 已提交
320 321 322 323

	BUG_ON(!PageLocked(page));

	/* we clean up only if the entire page is being invalidated */
324
	if (offset == 0 && length == PAGE_CACHE_SIZE) {
D
David Howells 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#ifdef CONFIG_AFS_FSCACHE
		if (PageFsCache(page)) {
			struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
			fscache_wait_on_page_write(vnode->cache, page);
			fscache_uncache_page(vnode->cache, page);
		}
#endif

		if (PagePrivate(page)) {
			if (wb && !PageWriteback(page)) {
				set_page_private(page, 0);
				afs_put_writeback(wb);
			}

			if (!page_private(page))
				ClearPagePrivate(page);
		}
	}

	_leave("");
}

/*
 * release a page and clean up its private state if it's not busy
 * - return true if the page can now be released, false if not
L
Linus Torvalds 已提交
350
 */
D
David Howells 已提交
351
static int afs_releasepage(struct page *page, gfp_t gfp_flags)
L
Linus Torvalds 已提交
352
{
D
David Howells 已提交
353
	struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
D
David Howells 已提交
354
	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
L
Linus Torvalds 已提交
355

D
David Howells 已提交
356 357 358
	_enter("{{%x:%u}[%lu],%lx},%x",
	       vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
	       gfp_flags);
L
Linus Torvalds 已提交
359

D
David Howells 已提交
360 361 362
	/* deny if page is being written to the cache and the caller hasn't
	 * elected to wait */
#ifdef CONFIG_AFS_FSCACHE
363 364 365
	if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
		_leave(" = F [cache busy]");
		return 0;
D
David Howells 已提交
366 367 368
	}
#endif

L
Linus Torvalds 已提交
369
	if (PagePrivate(page)) {
D
David Howells 已提交
370 371 372 373
		if (wb) {
			set_page_private(page, 0);
			afs_put_writeback(wb);
		}
L
Linus Torvalds 已提交
374 375 376
		ClearPagePrivate(page);
	}

D
David Howells 已提交
377 378 379
	/* indicate that the page can be released */
	_leave(" = T");
	return 1;
D
David Howells 已提交
380
}