file.c 12.2 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>
19
#include <linux/task_io_accounting_ops.h>
L
Linus Torvalds 已提交
20 21
#include "internal.h"

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

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

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

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

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

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

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

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

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

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

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

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/*
 * Dispose of a ref to a read record.
 */
void afs_put_read(struct afs_read *req)
{
	int i;

	if (atomic_dec_and_test(&req->usage)) {
		for (i = 0; i < req->nr_pages; i++)
			if (req->pages[i])
				put_page(req->pages[i]);
		kfree(req);
	}
}

120
#ifdef CONFIG_AFS_FSCACHE
L
Linus Torvalds 已提交
121 122 123
/*
 * deal with notification that a page was read from the cache
 */
D
David Howells 已提交
124 125 126
static void afs_file_readpage_read_complete(struct page *page,
					    void *data,
					    int error)
L
Linus Torvalds 已提交
127
{
D
David Howells 已提交
128
	_enter("%p,%p,%d", page, data, error);
L
Linus Torvalds 已提交
129

D
David Howells 已提交
130 131 132
	/* if the read completes with an error, we just unlock the page and let
	 * the VM reissue the readpage */
	if (!error)
L
Linus Torvalds 已提交
133 134
		SetPageUptodate(page);
	unlock_page(page);
D
David Howells 已提交
135
}
136
#endif
L
Linus Torvalds 已提交
137 138

/*
139
 * read page from file, directory or symlink, given a key to use
L
Linus Torvalds 已提交
140
 */
141
int afs_page_filler(void *data, struct page *page)
L
Linus Torvalds 已提交
142
{
143 144
	struct inode *inode = page->mapping->host;
	struct afs_vnode *vnode = AFS_FS_I(inode);
145
	struct afs_read *req;
146
	struct key *key = data;
L
Linus Torvalds 已提交
147 148
	int ret;

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

M
Matt Mackall 已提交
151
	BUG_ON(!PageLocked(page));
L
Linus Torvalds 已提交
152 153

	ret = -ESTALE;
154
	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
L
Linus Torvalds 已提交
155 156 157
		goto error;

	/* is it cached? */
D
David Howells 已提交
158 159
#ifdef CONFIG_AFS_FSCACHE
	ret = fscache_read_or_alloc_page(vnode->cache,
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171
					 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 已提交
172
		/* page not yet cached */
L
Linus Torvalds 已提交
173
	case -ENODATA:
D
David Howells 已提交
174 175 176 177 178 179
		_debug("cache said ENODATA");
		goto go_on;

		/* page will not be cached */
	case -ENOBUFS:
		_debug("cache said ENOBUFS");
L
Linus Torvalds 已提交
180
	default:
D
David Howells 已提交
181
	go_on:
182 183 184 185 186
		req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
			      GFP_KERNEL);
		if (!req)
			goto enomem;

187 188 189 190
		/* We request a full page.  If the page is a partial one at the
		 * end of the file, the server will return a short read and the
		 * unmarshalling code will clear the unfilled space.
		 */
191 192
		atomic_set(&req->usage, 1);
		req->pos = (loff_t)page->index << PAGE_SHIFT;
193
		req->len = PAGE_SIZE;
194 195 196
		req->nr_pages = 1;
		req->pages[0] = page;
		get_page(page);
L
Linus Torvalds 已提交
197 198 199

		/* read the contents of the file from the server into the
		 * page */
200 201
		ret = afs_vnode_fetch_data(vnode, key, req);
		afs_put_read(req);
L
Linus Torvalds 已提交
202
		if (ret < 0) {
203
			if (ret == -ENOENT) {
L
Linus Torvalds 已提交
204 205
				_debug("got NOENT from server"
				       " - marking file deleted and stale");
206
				set_bit(AFS_VNODE_DELETED, &vnode->flags);
L
Linus Torvalds 已提交
207 208
				ret = -ESTALE;
			}
D
David Howells 已提交
209 210 211

#ifdef CONFIG_AFS_FSCACHE
			fscache_uncache_page(vnode->cache, page);
L
Linus Torvalds 已提交
212
#endif
D
David Howells 已提交
213
			BUG_ON(PageFsCache(page));
L
Linus Torvalds 已提交
214 215 216 217 218
			goto error;
		}

		SetPageUptodate(page);

D
David Howells 已提交
219 220 221 222 223 224
		/* 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 已提交
225 226
		}
#endif
D
David Howells 已提交
227
		unlock_page(page);
L
Linus Torvalds 已提交
228 229 230 231 232
	}

	_leave(" = 0");
	return 0;

233 234
enomem:
	ret = -ENOMEM;
235
error:
L
Linus Torvalds 已提交
236 237 238 239
	SetPageError(page);
	unlock_page(page);
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
240
}
L
Linus Torvalds 已提交
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 * 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;
}

268 269 270 271 272
/*
 * Make pages available as they're filled.
 */
static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
{
273
#ifdef CONFIG_AFS_FSCACHE
274
	struct afs_vnode *vnode = call->reply;
275
#endif
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	struct page *page = req->pages[req->index];

	req->pages[req->index] = NULL;
	SetPageUptodate(page);

	/* 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));
	}
#endif
	unlock_page(page);
	put_page(page);
}

/*
 * Read a contiguous set of pages.
 */
static int afs_readpages_one(struct file *file, struct address_space *mapping,
			     struct list_head *pages)
{
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	struct afs_read *req;
	struct list_head *p;
	struct page *first, *page;
	struct key *key = file->private_data;
	pgoff_t index;
	int ret, n, i;

	/* Count the number of contiguous pages at the front of the list.  Note
	 * that the list goes prev-wards rather than next-wards.
	 */
	first = list_entry(pages->prev, struct page, lru);
	index = first->index + 1;
	n = 1;
	for (p = first->lru.prev; p != pages; p = p->prev) {
		page = list_entry(p, struct page, lru);
		if (page->index != index)
			break;
		index++;
		n++;
	}

	req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
		      GFP_NOFS);
	if (!req)
		return -ENOMEM;

	atomic_set(&req->usage, 1);
	req->page_done = afs_readpages_page_done;
	req->pos = first->index;
	req->pos <<= PAGE_SHIFT;

	/* Transfer the pages to the request.  We add them in until one fails
	 * to add to the LRU and then we stop (as that'll make a hole in the
	 * contiguous run.
	 *
	 * Note that it's possible for the file size to change whilst we're
	 * doing this, but we rely on the server returning less than we asked
	 * for if the file shrank.  We also rely on this to deal with a partial
	 * page at the end of the file.
	 */
	do {
		page = list_entry(pages->prev, struct page, lru);
		list_del(&page->lru);
		index = page->index;
		if (add_to_page_cache_lru(page, mapping, index,
					  readahead_gfp_mask(mapping))) {
#ifdef CONFIG_AFS_FSCACHE
			fscache_uncache_page(vnode->cache, page);
#endif
			put_page(page);
			break;
		}

		req->pages[req->nr_pages++] = page;
		req->len += PAGE_SIZE;
	} while (req->nr_pages < n);

	if (req->nr_pages == 0) {
		kfree(req);
		return 0;
	}

	ret = afs_vnode_fetch_data(vnode, key, req);
	if (ret < 0)
		goto error;

	task_io_account_read(PAGE_SIZE * req->nr_pages);
	afs_put_read(req);
	return 0;

error:
	if (ret == -ENOENT) {
		_debug("got NOENT from server"
		       " - marking file deleted and stale");
		set_bit(AFS_VNODE_DELETED, &vnode->flags);
		ret = -ESTALE;
	}

	for (i = 0; i < req->nr_pages; i++) {
		page = req->pages[i];
		if (page) {
#ifdef CONFIG_AFS_FSCACHE
			fscache_uncache_page(vnode->cache, page);
#endif
			SetPageError(page);
			unlock_page(page);
		}
	}

	afs_put_read(req);
	return ret;
}

L
Linus Torvalds 已提交
393
/*
D
David Howells 已提交
394
 * read a set of pages
L
Linus Torvalds 已提交
395
 */
D
David Howells 已提交
396 397
static int afs_readpages(struct file *file, struct address_space *mapping,
			 struct list_head *pages, unsigned nr_pages)
L
Linus Torvalds 已提交
398
{
399
	struct key *key = file->private_data;
D
David Howells 已提交
400 401
	struct afs_vnode *vnode;
	int ret = 0;
L
Linus Torvalds 已提交
402

403 404 405 406
	_enter("{%d},{%lu},,%d",
	       key_serial(key), mapping->host->i_ino, nr_pages);

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

D
David Howells 已提交
408
	vnode = AFS_FS_I(mapping->host);
409
	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
D
David Howells 已提交
410 411 412
		_leave(" = -ESTALE");
		return -ESTALE;
	}
L
Linus Torvalds 已提交
413

D
David Howells 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	/* 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 已提交
444 445
	}

446 447 448 449 450
	while (!list_empty(pages)) {
		ret = afs_readpages_one(file, mapping, pages);
		if (ret < 0)
			break;
	}
D
David Howells 已提交
451 452 453

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

456 457 458 459 460 461 462 463 464 465
/*
 * write back a dirty page
 */
static int afs_launder_page(struct page *page)
{
	_enter("{%lu}", page->index);

	return 0;
}

L
Linus Torvalds 已提交
466
/*
D
David Howells 已提交
467 468 469 470
 * invalidate part or all of a page
 * - release a page and clean up its private data if offset is 0 (indicating
 *   the entire page)
 */
471 472
static void afs_invalidatepage(struct page *page, unsigned int offset,
			       unsigned int length)
D
David Howells 已提交
473 474 475
{
	struct afs_writeback *wb = (struct afs_writeback *) page_private(page);

476
	_enter("{%lu},%u,%u", page->index, offset, length);
D
David Howells 已提交
477 478 479 480

	BUG_ON(!PageLocked(page));

	/* we clean up only if the entire page is being invalidated */
481
	if (offset == 0 && length == PAGE_SIZE) {
D
David Howells 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
#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 已提交
507
 */
D
David Howells 已提交
508
static int afs_releasepage(struct page *page, gfp_t gfp_flags)
L
Linus Torvalds 已提交
509
{
D
David Howells 已提交
510
	struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
D
David Howells 已提交
511
	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
L
Linus Torvalds 已提交
512

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

D
David Howells 已提交
517 518 519
	/* deny if page is being written to the cache and the caller hasn't
	 * elected to wait */
#ifdef CONFIG_AFS_FSCACHE
520 521 522
	if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
		_leave(" = F [cache busy]");
		return 0;
D
David Howells 已提交
523 524 525
	}
#endif

L
Linus Torvalds 已提交
526
	if (PagePrivate(page)) {
D
David Howells 已提交
527 528 529 530
		if (wb) {
			set_page_private(page, 0);
			afs_put_writeback(wb);
		}
L
Linus Torvalds 已提交
531 532 533
		ClearPagePrivate(page);
	}

D
David Howells 已提交
534 535 536
	/* indicate that the page can be released */
	_leave(" = T");
	return 1;
D
David Howells 已提交
537
}