write.c 21.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6
/* handling of writes to regular files and writing back to the server
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 */
7

A
Alexey Dobriyan 已提交
8
#include <linux/backing-dev.h>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/pagevec.h>
#include "internal.h"

/*
 * mark a page as having been made dirty and thus needing writeback
 */
int afs_set_page_dirty(struct page *page)
{
	_enter("");
	return __set_page_dirty_nobuffers(page);
}

/*
 * partly or wholly fill a page that's under preparation for writing
 */
static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
29
			 loff_t pos, unsigned int len, struct page *page)
30
{
31
	struct afs_read *req;
32 33
	size_t p;
	void *data;
34 35
	int ret;

36
	_enter(",,%llu", (unsigned long long)pos);
37

38 39 40 41 42 43 44 45 46
	if (pos >= vnode->vfs_inode.i_size) {
		p = pos & ~PAGE_MASK;
		ASSERTCMP(p + len, <=, PAGE_SIZE);
		data = kmap(page);
		memset(data + p, 0, len);
		kunmap(page);
		return 0;
	}

47
	req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
48 49 50
	if (!req)
		return -ENOMEM;

D
David Howells 已提交
51
	refcount_set(&req->usage, 1);
52
	req->pos = pos;
53
	req->len = len;
54
	req->nr_pages = 1;
D
David Howells 已提交
55
	req->pages = req->array;
56
	req->pages[0] = page;
57
	get_page(page);
58

59
	ret = afs_fetch_data(vnode, key, req);
60
	afs_put_read(req);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	if (ret < 0) {
		if (ret == -ENOENT) {
			_debug("got NOENT from server"
			       " - marking file deleted and stale");
			set_bit(AFS_VNODE_DELETED, &vnode->flags);
			ret = -ESTALE;
		}
	}

	_leave(" = %d", ret);
	return ret;
}

/*
 * prepare to perform part of a write to a page
 */
N
Nick Piggin 已提交
77 78 79
int afs_write_begin(struct file *file, struct address_space *mapping,
		    loff_t pos, unsigned len, unsigned flags,
		    struct page **pagep, void **fsdata)
80
{
A
Al Viro 已提交
81
	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
N
Nick Piggin 已提交
82
	struct page *page;
83
	struct key *key = afs_file_key(file);
84 85 86
	unsigned long priv;
	unsigned f, from = pos & (PAGE_SIZE - 1);
	unsigned t, to = from + len;
87
	pgoff_t index = pos >> PAGE_SHIFT;
88 89
	int ret;

90
	_enter("{%llx:%llu},{%lx},%u,%u",
N
Nick Piggin 已提交
91
	       vnode->fid.vid, vnode->fid.vnode, index, from, to);
92

93 94 95 96
	/* We want to store information about how much of a page is altered in
	 * page->private.
	 */
	BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
97

98
	page = grab_cache_page_write_begin(mapping, index, flags);
99
	if (!page)
N
Nick Piggin 已提交
100 101
		return -ENOMEM;

102
	if (!PageUptodate(page) && len != PAGE_SIZE) {
103
		ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
104
		if (ret < 0) {
105 106
			unlock_page(page);
			put_page(page);
107 108 109
			_leave(" = %d [prep]", ret);
			return ret;
		}
N
Nick Piggin 已提交
110
		SetPageUptodate(page);
111 112
	}

113 114 115
	/* page won't leak in error case: it eventually gets cleaned off LRU */
	*pagep = page;

116
try_again:
117 118 119 120 121 122 123 124 125
	/* See if this page is already partially written in a way that we can
	 * merge the new write with.
	 */
	t = f = 0;
	if (PagePrivate(page)) {
		priv = page_private(page);
		f = priv & AFS_PRIV_MAX;
		t = priv >> AFS_PRIV_SHIFT;
		ASSERTCMP(f, <=, t);
126 127
	}

128
	if (f != t) {
129 130 131 132 133
		if (PageWriteback(page)) {
			trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
					     page->index, priv);
			goto flush_conflicting_write;
		}
134 135 136 137 138 139
		/* If the file is being filled locally, allow inter-write
		 * spaces to be merged into writes.  If it's not, only write
		 * back what the user gives us.
		 */
		if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
		    (to < f || from > t))
140 141 142 143 144 145 146 147
			goto flush_conflicting_write;
		if (from < f)
			f = from;
		if (to > t)
			t = to;
	} else {
		f = from;
		t = to;
148 149
	}

150 151
	priv = (unsigned long)t << AFS_PRIV_SHIFT;
	priv |= f;
D
David Howells 已提交
152 153
	trace_afs_page_dirty(vnode, tracepoint_string("begin"),
			     page->index, priv);
154
	SetPagePrivate(page);
155 156
	set_page_private(page, priv);
	_leave(" = 0");
157 158
	return 0;

159 160 161 162
	/* The previous write and this write aren't adjacent or overlapping, so
	 * flush the page out.
	 */
flush_conflicting_write:
163
	_debug("flush conflict");
164 165 166 167
	ret = write_one_page(page);
	if (ret < 0) {
		_leave(" = %d", ret);
		return ret;
168 169
	}

170 171 172 173 174
	ret = lock_page_killable(page);
	if (ret < 0) {
		_leave(" = %d", ret);
		return ret;
	}
175 176 177 178 179 180
	goto try_again;
}

/*
 * finalise part of a write to a page
 */
N
Nick Piggin 已提交
181 182 183
int afs_write_end(struct file *file, struct address_space *mapping,
		  loff_t pos, unsigned len, unsigned copied,
		  struct page *page, void *fsdata)
184
{
A
Al Viro 已提交
185
	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
186
	struct key *key = afs_file_key(file);
187
	loff_t i_size, maybe_i_size;
188
	int ret;
189

190
	_enter("{%llx:%llu},{%lx}",
N
Nick Piggin 已提交
191
	       vnode->fid.vid, vnode->fid.vnode, page->index);
192

N
Nick Piggin 已提交
193
	maybe_i_size = pos + copied;
194 195 196

	i_size = i_size_read(&vnode->vfs_inode);
	if (maybe_i_size > i_size) {
197
		write_seqlock(&vnode->cb_lock);
198 199 200
		i_size = i_size_read(&vnode->vfs_inode);
		if (maybe_i_size > i_size)
			i_size_write(&vnode->vfs_inode, maybe_i_size);
201
		write_sequnlock(&vnode->cb_lock);
202 203
	}

204 205 206 207 208 209 210 211 212
	if (!PageUptodate(page)) {
		if (copied < len) {
			/* Try and load any missing data from the server.  The
			 * unmarshalling routine will take care of clearing any
			 * bits that are beyond the EOF.
			 */
			ret = afs_fill_page(vnode, key, pos + copied,
					    len - copied, page);
			if (ret < 0)
213
				goto out;
214 215 216 217
		}
		SetPageUptodate(page);
	}

218 219 220
	set_page_dirty(page);
	if (PageDirty(page))
		_debug("dirtied");
221 222 223
	ret = copied;

out:
N
Nick Piggin 已提交
224
	unlock_page(page);
225
	put_page(page);
226
	return ret;
227 228 229 230 231
}

/*
 * kill all the pages in the given range
 */
232
static void afs_kill_pages(struct address_space *mapping,
233 234
			   pgoff_t first, pgoff_t last)
{
235
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
236 237 238
	struct pagevec pv;
	unsigned count, loop;

239
	_enter("{%llx:%llu},%lx-%lx",
240 241
	       vnode->fid.vid, vnode->fid.vnode, first, last);

242
	pagevec_init(&pv);
243 244 245 246 247 248 249

	do {
		_debug("kill %lx-%lx", first, last);

		count = last - first + 1;
		if (count > PAGEVEC_SIZE)
			count = PAGEVEC_SIZE;
250
		pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
251 252 253
		ASSERTCMP(pv.nr, ==, count);

		for (loop = 0; loop < count; loop++) {
D
David Howells 已提交
254 255
			struct page *page = pv.pages[loop];
			ClearPageUptodate(page);
256 257
			SetPageError(page);
			end_page_writeback(page);
D
David Howells 已提交
258 259
			if (page->index >= first)
				first = page->index + 1;
260 261
			lock_page(page);
			generic_error_remove_page(mapping, page);
262
			unlock_page(page);
263 264 265
		}

		__pagevec_release(&pv);
266
	} while (first <= last);
267 268 269 270 271

	_leave("");
}

/*
272 273 274 275 276 277 278 279 280 281
 * Redirty all the pages in a given range.
 */
static void afs_redirty_pages(struct writeback_control *wbc,
			      struct address_space *mapping,
			      pgoff_t first, pgoff_t last)
{
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	struct pagevec pv;
	unsigned count, loop;

282
	_enter("{%llx:%llu},%lx-%lx",
283 284
	       vnode->fid.vid, vnode->fid.vnode, first, last);

285
	pagevec_init(&pv);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

	do {
		_debug("redirty %lx-%lx", first, last);

		count = last - first + 1;
		if (count > PAGEVEC_SIZE)
			count = PAGEVEC_SIZE;
		pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
		ASSERTCMP(pv.nr, ==, count);

		for (loop = 0; loop < count; loop++) {
			struct page *page = pv.pages[loop];

			redirty_page_for_writepage(wbc, page);
			end_page_writeback(page);
			if (page->index >= first)
				first = page->index + 1;
		}

		__pagevec_release(&pv);
	} while (first <= last);
307 308 309 310

	_leave("");
}

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
/*
 * completion of write to server
 */
static void afs_pages_written_back(struct afs_vnode *vnode,
				   pgoff_t first, pgoff_t last)
{
	struct pagevec pv;
	unsigned long priv;
	unsigned count, loop;

	_enter("{%llx:%llu},{%lx-%lx}",
	       vnode->fid.vid, vnode->fid.vnode, first, last);

	pagevec_init(&pv);

	do {
		_debug("done %lx-%lx", first, last);

		count = last - first + 1;
		if (count > PAGEVEC_SIZE)
			count = PAGEVEC_SIZE;
		pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
					      first, count, pv.pages);
		ASSERTCMP(pv.nr, ==, count);

		for (loop = 0; loop < count; loop++) {
			priv = page_private(pv.pages[loop]);
			trace_afs_page_dirty(vnode, tracepoint_string("clear"),
					     pv.pages[loop]->index, priv);
			set_page_private(pv.pages[loop], 0);
			end_page_writeback(pv.pages[loop]);
		}
		first += count;
		__pagevec_release(&pv);
	} while (first <= last);

	afs_prune_wb_keys(vnode);
	_leave("");
}

351
/*
352 353 354
 * Find a key to use for the writeback.  We cached the keys used to author the
 * writes on the vnode.  *_wbk will contain the last writeback key used or NULL
 * and we need to start from there if it's set.
355
 */
356 357
static int afs_get_writeback_key(struct afs_vnode *vnode,
				 struct afs_wb_key **_wbk)
358
{
359 360 361
	struct afs_wb_key *wbk = NULL;
	struct list_head *p;
	int ret = -ENOKEY, ret2;
362

363
	spin_lock(&vnode->wb_lock);
364 365 366 367
	if (*_wbk)
		p = (*_wbk)->vnode_link.next;
	else
		p = vnode->wb_keys.next;
368 369 370 371 372

	while (p != &vnode->wb_keys) {
		wbk = list_entry(p, struct afs_wb_key, vnode_link);
		_debug("wbk %u", key_serial(wbk->key));
		ret2 = key_validate(wbk->key);
373 374 375 376 377 378 379
		if (ret2 == 0) {
			refcount_inc(&wbk->usage);
			_debug("USE WB KEY %u", key_serial(wbk->key));
			break;
		}

		wbk = NULL;
380 381 382 383 384 385
		if (ret == -ENOKEY)
			ret = ret2;
		p = p->next;
	}

	spin_unlock(&vnode->wb_lock);
386 387 388 389 390
	if (*_wbk)
		afs_put_wb_key(*_wbk);
	*_wbk = wbk;
	return 0;
}
391

392 393 394
static void afs_store_data_success(struct afs_operation *op)
{
	struct afs_vnode *vnode = op->file[0].vnode;
395

D
David Howells 已提交
396
	op->ctime = op->file[0].scb.status.mtime_client;
397 398 399 400 401 402 403 404 405
	afs_vnode_commit_status(op, &op->file[0]);
	if (op->error == 0) {
		afs_pages_written_back(vnode, op->store.first, op->store.last);
		afs_stat_v(vnode, n_stores);
		atomic_long_add((op->store.last * PAGE_SIZE + op->store.last_to) -
				(op->store.first * PAGE_SIZE + op->store.first_offset),
				&afs_v2net(vnode)->n_store_bytes);
	}
}
406

407 408 409 410 411
static const struct afs_operation_ops afs_store_data_operation = {
	.issue_afs_rpc	= afs_fs_store_data,
	.issue_yfs_rpc	= yfs_fs_store_data,
	.success	= afs_store_data_success,
};
412

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * write to a file
 */
static int afs_store_data(struct address_space *mapping,
			  pgoff_t first, pgoff_t last,
			  unsigned offset, unsigned to)
{
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	struct afs_operation *op;
	struct afs_wb_key *wbk = NULL;
	int ret;

	_enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x",
	       vnode->volume->name,
	       vnode->fid.vid,
	       vnode->fid.vnode,
	       vnode->fid.unique,
	       first, last, offset, to);
431

432 433 434 435
	ret = afs_get_writeback_key(vnode, &wbk);
	if (ret) {
		_leave(" = %d [no keys]", ret);
		return ret;
436 437
	}

438 439 440 441 442 443 444 445 446 447 448 449 450
	op = afs_alloc_operation(wbk->key, vnode->volume);
	if (IS_ERR(op)) {
		afs_put_wb_key(wbk);
		return -ENOMEM;
	}

	afs_op_set_vnode(op, 0, vnode);
	op->file[0].dv_delta = 1;
	op->store.mapping = mapping;
	op->store.first = first;
	op->store.last = last;
	op->store.first_offset = offset;
	op->store.last_to = to;
451
	op->mtime = vnode->vfs_inode.i_mtime;
452 453 454 455 456 457 458
	op->ops = &afs_store_data_operation;

try_next_key:
	afs_begin_vnode_operation(op);
	afs_wait_for_operation(op);

	switch (op->error) {
459 460 461 462 463 464 465
	case -EACCES:
	case -EPERM:
	case -ENOKEY:
	case -EKEYEXPIRED:
	case -EKEYREJECTED:
	case -EKEYREVOKED:
		_debug("next");
466 467 468 469 470 471 472 473

		ret = afs_get_writeback_key(vnode, &wbk);
		if (ret == 0) {
			key_put(op->key);
			op->key = key_get(wbk->key);
			goto try_next_key;
		}
		break;
474 475 476
	}

	afs_put_wb_key(wbk);
477 478
	_leave(" = %d", op->error);
	return afs_put_operation(op);
479 480
}

481
/*
482 483
 * Synchronously write back the locked page and any subsequent non-locked dirty
 * pages.
484
 */
485 486 487 488
static int afs_write_back_from_locked_page(struct address_space *mapping,
					   struct writeback_control *wbc,
					   struct page *primary_page,
					   pgoff_t final_page)
489
{
D
David Howells 已提交
490
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
491
	struct page *pages[8], *page;
492 493
	unsigned long count, priv;
	unsigned n, offset, to, f, t;
494 495 496 497 498 499 500 501 502
	pgoff_t start, first, last;
	int loop, ret;

	_enter(",%lx", primary_page->index);

	count = 1;
	if (test_set_page_writeback(primary_page))
		BUG();

503 504 505 506 507
	/* Find all consecutive lockable dirty pages that have contiguous
	 * written regions, stopping when we find a page that is not
	 * immediately lockable, is not dirty or is missing, or we reach the
	 * end of the range.
	 */
508
	start = primary_page->index;
509 510 511
	priv = page_private(primary_page);
	offset = priv & AFS_PRIV_MAX;
	to = priv >> AFS_PRIV_SHIFT;
D
David Howells 已提交
512 513
	trace_afs_page_dirty(vnode, tracepoint_string("store"),
			     primary_page->index, priv);
514 515

	WARN_ON(offset == to);
D
David Howells 已提交
516 517 518
	if (offset == to)
		trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
				     primary_page->index, priv);
519

520 521
	if (start >= final_page ||
	    (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)))
522
		goto no_more;
523

524 525 526
	start++;
	do {
		_debug("more %lx [%lx]", start, count);
527
		n = final_page - start + 1;
528 529
		if (n > ARRAY_SIZE(pages))
			n = ARRAY_SIZE(pages);
530
		n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
531 532 533 534
		_debug("fgpc %u", n);
		if (n == 0)
			goto no_more;
		if (pages[0]->index != start) {
535 536 537
			do {
				put_page(pages[--n]);
			} while (n > 0);
538 539 540 541 542
			goto no_more;
		}

		for (loop = 0; loop < n; loop++) {
			page = pages[loop];
543 544 545
			if (to != PAGE_SIZE &&
			    !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))
				break;
546
			if (page->index > final_page)
547
				break;
N
Nick Piggin 已提交
548
			if (!trylock_page(page))
549
				break;
550
			if (!PageDirty(page) || PageWriteback(page)) {
551 552 553
				unlock_page(page);
				break;
			}
554 555 556 557

			priv = page_private(page);
			f = priv & AFS_PRIV_MAX;
			t = priv >> AFS_PRIV_SHIFT;
558 559
			if (f != 0 &&
			    !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) {
560 561 562
				unlock_page(page);
				break;
			}
563 564
			to = t;

D
David Howells 已提交
565 566 567
			trace_afs_page_dirty(vnode, tracepoint_string("store+"),
					     page->index, priv);

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
			if (!clear_page_dirty_for_io(page))
				BUG();
			if (test_set_page_writeback(page))
				BUG();
			unlock_page(page);
			put_page(page);
		}
		count += loop;
		if (loop < n) {
			for (; loop < n; loop++)
				put_page(pages[loop]);
			goto no_more;
		}

		start += loop;
583
	} while (start <= final_page && count < 65536);
584 585

no_more:
586 587 588 589 590 591
	/* We now have a contiguous set of dirty pages, each with writeback
	 * set; the first page is still locked at this point, but all the rest
	 * have been unlocked.
	 */
	unlock_page(primary_page);

592 593 594 595 596
	first = primary_page->index;
	last = first + count - 1;

	_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);

597 598 599
	ret = afs_store_data(mapping, first, last, offset, to);
	switch (ret) {
	case 0:
600
		ret = count;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		break;

	default:
		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
		/* Fall through */
	case -EACCES:
	case -EPERM:
	case -ENOKEY:
	case -EKEYEXPIRED:
	case -EKEYREJECTED:
	case -EKEYREVOKED:
		afs_redirty_pages(wbc, mapping, first, last);
		mapping_set_error(mapping, ret);
		break;

	case -EDQUOT:
	case -ENOSPC:
		afs_redirty_pages(wbc, mapping, first, last);
		mapping_set_error(mapping, -ENOSPC);
		break;

	case -EROFS:
	case -EIO:
	case -EREMOTEIO:
	case -EFBIG:
	case -ENOENT:
	case -ENOMEDIUM:
	case -ENXIO:
629
		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
630 631 632
		afs_kill_pages(mapping, first, last);
		mapping_set_error(mapping, ret);
		break;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	}

	_leave(" = %d", ret);
	return ret;
}

/*
 * write a page back to the server
 * - the caller locked the page for us
 */
int afs_writepage(struct page *page, struct writeback_control *wbc)
{
	int ret;

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

649 650
	ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
					      wbc->range_end >> PAGE_SHIFT);
651 652 653 654 655 656 657 658 659 660 661 662 663 664
	if (ret < 0) {
		_leave(" = %d", ret);
		return 0;
	}

	wbc->nr_to_write -= ret;

	_leave(" = 0");
	return 0;
}

/*
 * write a region of pages back to the server
 */
A
Adrian Bunk 已提交
665 666 667
static int afs_writepages_region(struct address_space *mapping,
				 struct writeback_control *wbc,
				 pgoff_t index, pgoff_t end, pgoff_t *_next)
668 669 670 671 672 673 674
{
	struct page *page;
	int ret, n;

	_enter(",,%lx,%lx,", index, end);

	do {
J
Jan Kara 已提交
675 676
		n = find_get_pages_range_tag(mapping, &index, end,
					PAGECACHE_TAG_DIRTY, 1, &page);
677 678 679 680 681
		if (!n)
			break;

		_debug("wback %lx", page->index);

M
Matthew Wilcox 已提交
682 683 684 685 686
		/*
		 * at this point we hold neither the i_pages lock nor the
		 * page lock: the page may be truncated or invalidated
		 * (changing page->mapping to NULL), or even swizzled
		 * back from swapper_space to tmpfs file mapping
687
		 */
688 689 690 691 692 693
		ret = lock_page_killable(page);
		if (ret < 0) {
			put_page(page);
			_leave(" = %d", ret);
			return ret;
		}
694

695
		if (page->mapping != mapping || !PageDirty(page)) {
696
			unlock_page(page);
697
			put_page(page);
698 699 700
			continue;
		}

701
		if (PageWriteback(page)) {
702
			unlock_page(page);
703 704
			if (wbc->sync_mode != WB_SYNC_NONE)
				wait_on_page_writeback(page);
D
David Howells 已提交
705
			put_page(page);
706 707 708
			continue;
		}

709 710
		if (!clear_page_dirty_for_io(page))
			BUG();
711
		ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
712
		put_page(page);
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		if (ret < 0) {
			_leave(" = %d", ret);
			return ret;
		}

		wbc->nr_to_write -= ret;

		cond_resched();
	} while (index < end && wbc->nr_to_write > 0);

	*_next = index;
	_leave(" = 0 [%lx]", *_next);
	return 0;
}

/*
 * write some of the pending data back to the server
 */
int afs_writepages(struct address_space *mapping,
		   struct writeback_control *wbc)
{
	pgoff_t start, end, next;
	int ret;

	_enter("");

	if (wbc->range_cyclic) {
		start = mapping->writeback_index;
		end = -1;
		ret = afs_writepages_region(mapping, wbc, start, end, &next);
743
		if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
744 745 746 747
			ret = afs_writepages_region(mapping, wbc, 0, start,
						    &next);
		mapping->writeback_index = next;
	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
748
		end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
749 750 751 752
		ret = afs_writepages_region(mapping, wbc, 0, end, &next);
		if (wbc->nr_to_write > 0)
			mapping->writeback_index = next;
	} else {
753 754
		start = wbc->range_start >> PAGE_SHIFT;
		end = wbc->range_end >> PAGE_SHIFT;
755 756 757 758 759 760 761 762 763 764
		ret = afs_writepages_region(mapping, wbc, start, end, &next);
	}

	_leave(" = %d", ret);
	return ret;
}

/*
 * write to an AFS file
 */
A
Al Viro 已提交
765
ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
766
{
A
Al Viro 已提交
767
	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
768
	ssize_t result;
A
Al Viro 已提交
769
	size_t count = iov_iter_count(from);
770

771
	_enter("{%llx:%llu},{%zu},",
A
Al Viro 已提交
772
	       vnode->fid.vid, vnode->fid.vnode, count);
773 774 775 776 777 778 779 780 781 782

	if (IS_SWAPFILE(&vnode->vfs_inode)) {
		printk(KERN_INFO
		       "AFS: Attempt to write to active swap file!\n");
		return -EBUSY;
	}

	if (!count)
		return 0;

A
Al Viro 已提交
783
	result = generic_file_write_iter(iocb, from);
784 785 786 787 788 789 790 791 792 793

	_leave(" = %zd", result);
	return result;
}

/*
 * flush any dirty pages for this process, and check for write errors.
 * - the return status from this call provides a reliable indication of
 *   whether any write errors occurred for this process.
 */
794
int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
795
{
796 797
	struct inode *inode = file_inode(file);
	struct afs_vnode *vnode = AFS_FS_I(inode);
798

799
	_enter("{%llx:%llu},{n=%pD},%d",
800
	       vnode->fid.vid, vnode->fid.vnode, file,
801 802
	       datasync);

803
	return file_write_and_wait_range(file, start, end);
804
}
D
David Howells 已提交
805 806 807 808 809

/*
 * notification that a previously read-only page is about to become writable
 * - if it returns an error, the caller will deliver a bus error signal
 */
810
vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
D
David Howells 已提交
811
{
812 813 814 815
	struct file *file = vmf->vma->vm_file;
	struct inode *inode = file_inode(file);
	struct afs_vnode *vnode = AFS_FS_I(inode);
	unsigned long priv;
D
David Howells 已提交
816

817
	_enter("{{%llx:%llu}},{%lx}",
818
	       vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
D
David Howells 已提交
819

820
	sb_start_pagefault(inode->i_sb);
D
David Howells 已提交
821

822 823 824
	/* Wait for the page to be written to the cache before we allow it to
	 * be modified.  We then assume the entire page will need writing back.
	 */
D
David Howells 已提交
825
#ifdef CONFIG_AFS_FSCACHE
826
	fscache_wait_on_page_write(vnode->cache, vmf->page);
D
David Howells 已提交
827 828
#endif

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	if (PageWriteback(vmf->page) &&
	    wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
		return VM_FAULT_RETRY;

	if (lock_page_killable(vmf->page) < 0)
		return VM_FAULT_RETRY;

	/* We mustn't change page->private until writeback is complete as that
	 * details the portion of the page we need to write back and we might
	 * need to redirty the page if there's a problem.
	 */
	wait_on_page_writeback(vmf->page);

	priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
	priv |= 0; /* From */
D
David Howells 已提交
844 845
	trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
			     vmf->page->index, priv);
846 847
	SetPagePrivate(vmf->page);
	set_page_private(vmf->page, priv);
848
	file_update_time(file);
849 850 851

	sb_end_pagefault(inode->i_sb);
	return VM_FAULT_LOCKED;
D
David Howells 已提交
852
}
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

/*
 * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
 */
void afs_prune_wb_keys(struct afs_vnode *vnode)
{
	LIST_HEAD(graveyard);
	struct afs_wb_key *wbk, *tmp;

	/* Discard unused keys */
	spin_lock(&vnode->wb_lock);

	if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
	    !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
			if (refcount_read(&wbk->usage) == 1)
				list_move(&wbk->vnode_link, &graveyard);
		}
	}

	spin_unlock(&vnode->wb_lock);

	while (!list_empty(&graveyard)) {
		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
		list_del(&wbk->vnode_link);
		afs_put_wb_key(wbk);
	}
}

/*
 * Clean up a page during invalidation.
 */
int afs_launder_page(struct page *page)
{
	struct address_space *mapping = page->mapping;
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	unsigned long priv;
	unsigned int f, t;
	int ret = 0;

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

	priv = page_private(page);
	if (clear_page_dirty_for_io(page)) {
		f = 0;
		t = PAGE_SIZE;
		if (PagePrivate(page)) {
			f = priv & AFS_PRIV_MAX;
			t = priv >> AFS_PRIV_SHIFT;
		}

D
David Howells 已提交
904 905
		trace_afs_page_dirty(vnode, tracepoint_string("launder"),
				     page->index, priv);
906 907 908
		ret = afs_store_data(mapping, page->index, page->index, t, f);
	}

D
David Howells 已提交
909 910
	trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
			     page->index, priv);
911 912 913 914 915 916 917 918 919 920
	set_page_private(page, 0);
	ClearPagePrivate(page);

#ifdef CONFIG_AFS_FSCACHE
	if (PageFsCache(page)) {
		fscache_wait_on_page_write(vnode->cache, page);
		fscache_uncache_page(vnode->cache, page);
	}
#endif
	return ret;
D
David Howells 已提交
921
}