write.c 22.5 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
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/pagevec.h>
14 15
#include <linux/netfs.h>
#include <linux/fscache.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#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);
}

/*
 * prepare to perform part of a write to a page
 */
N
Nick Piggin 已提交
30 31
int afs_write_begin(struct file *file, struct address_space *mapping,
		    loff_t pos, unsigned len, unsigned flags,
32
		    struct page **_page, void **fsdata)
33
{
A
Al Viro 已提交
34
	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
N
Nick Piggin 已提交
35
	struct page *page;
36
	unsigned long priv;
D
David Howells 已提交
37 38 39
	unsigned f, from;
	unsigned t, to;
	pgoff_t index;
40 41
	int ret;

D
David Howells 已提交
42 43
	_enter("{%llx:%llu},%llx,%x",
	       vnode->fid.vid, vnode->fid.vnode, pos, len);
44

45 46 47 48 49 50 51 52
	/* Prefetch area to be written into the cache if we're caching this
	 * file.  We need to do this before we get a lock on the page in case
	 * there's more than one writer competing for the same cache block.
	 */
	ret = netfs_write_begin(file, mapping, pos, len, flags, &page, fsdata,
				&afs_req_ops, NULL);
	if (ret < 0)
		return ret;
53

D
David Howells 已提交
54 55 56 57
	index = page->index;
	from = pos - index * PAGE_SIZE;
	to = from + len;

58
try_again:
59 60 61 62 63
	/* See if this page is already partially written in a way that we can
	 * merge the new write with.
	 */
	if (PagePrivate(page)) {
		priv = page_private(page);
64 65
		f = afs_page_dirty_from(page, priv);
		t = afs_page_dirty_to(page, priv);
66
		ASSERTCMP(f, <=, t);
67

68
		if (PageWriteback(page)) {
69
			trace_afs_page_dirty(vnode, tracepoint_string("alrdy"), page);
70 71
			goto flush_conflicting_write;
		}
72 73 74 75 76 77
		/* 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))
78
			goto flush_conflicting_write;
79 80
	}

81
	*_page = page;
82
	_leave(" = 0");
83 84
	return 0;

85 86 87 88
	/* The previous write and this write aren't adjacent or overlapping, so
	 * flush the page out.
	 */
flush_conflicting_write:
89
	_debug("flush conflict");
90
	ret = write_one_page(page);
91 92
	if (ret < 0)
		goto error;
93

94
	ret = lock_page_killable(page);
95 96
	if (ret < 0)
		goto error;
97
	goto try_again;
98 99 100 101 102

error:
	put_page(page);
	_leave(" = %d", ret);
	return ret;
103 104 105 106 107
}

/*
 * finalise part of a write to a page
 */
N
Nick Piggin 已提交
108 109 110
int afs_write_end(struct file *file, struct address_space *mapping,
		  loff_t pos, unsigned len, unsigned copied,
		  struct page *page, void *fsdata)
111
{
A
Al Viro 已提交
112
	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
113
	unsigned long priv;
D
David Howells 已提交
114
	unsigned int f, from = pos & (thp_size(page) - 1);
115
	unsigned int t, to = from + copied;
116 117
	loff_t i_size, maybe_i_size;

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

121 122 123
	if (copied == 0)
		goto out;

N
Nick Piggin 已提交
124
	maybe_i_size = pos + copied;
125 126 127

	i_size = i_size_read(&vnode->vfs_inode);
	if (maybe_i_size > i_size) {
128
		write_seqlock(&vnode->cb_lock);
129 130 131
		i_size = i_size_read(&vnode->vfs_inode);
		if (maybe_i_size > i_size)
			i_size_write(&vnode->vfs_inode, maybe_i_size);
132
		write_sequnlock(&vnode->cb_lock);
133 134
	}

135
	ASSERT(PageUptodate(page));
136

137 138
	if (PagePrivate(page)) {
		priv = page_private(page);
139 140
		f = afs_page_dirty_from(page, priv);
		t = afs_page_dirty_to(page, priv);
141 142 143 144
		if (from < f)
			f = from;
		if (to > t)
			t = to;
145
		priv = afs_page_dirty(page, f, t);
146
		set_page_private(page, priv);
147
		trace_afs_page_dirty(vnode, tracepoint_string("dirty+"), page);
148
	} else {
149
		priv = afs_page_dirty(page, from, to);
150
		attach_page_private(page, (void *)priv);
151
		trace_afs_page_dirty(vnode, tracepoint_string("dirty"), page);
152 153
	}

D
David Howells 已提交
154 155
	if (set_page_dirty(page))
		_debug("dirtied %lx", page->index);
156 157

out:
N
Nick Piggin 已提交
158
	unlock_page(page);
159
	put_page(page);
160
	return copied;
161 162 163 164 165
}

/*
 * kill all the pages in the given range
 */
166
static void afs_kill_pages(struct address_space *mapping,
D
David Howells 已提交
167
			   loff_t start, loff_t len)
168
{
169
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
170
	struct pagevec pv;
D
David Howells 已提交
171
	unsigned int loop, psize;
172

D
David Howells 已提交
173 174
	_enter("{%llx:%llu},%llx @%llx",
	       vnode->fid.vid, vnode->fid.vnode, len, start);
175

176
	pagevec_init(&pv);
177 178

	do {
D
David Howells 已提交
179
		_debug("kill %llx @%llx", len, start);
180

D
David Howells 已提交
181 182 183 184
		pv.nr = find_get_pages_contig(mapping, start / PAGE_SIZE,
					      PAGEVEC_SIZE, pv.pages);
		if (pv.nr == 0)
			break;
185

D
David Howells 已提交
186
		for (loop = 0; loop < pv.nr; loop++) {
D
David Howells 已提交
187
			struct page *page = pv.pages[loop];
D
David Howells 已提交
188 189 190 191 192 193 194

			if (page->index * PAGE_SIZE >= start + len)
				break;

			psize = thp_size(page);
			start += psize;
			len -= psize;
D
David Howells 已提交
195
			ClearPageUptodate(page);
196 197 198
			end_page_writeback(page);
			lock_page(page);
			generic_error_remove_page(mapping, page);
199
			unlock_page(page);
200 201 202
		}

		__pagevec_release(&pv);
D
David Howells 已提交
203
	} while (len > 0);
204 205 206 207 208

	_leave("");
}

/*
209 210 211 212
 * Redirty all the pages in a given range.
 */
static void afs_redirty_pages(struct writeback_control *wbc,
			      struct address_space *mapping,
D
David Howells 已提交
213
			      loff_t start, loff_t len)
214 215 216
{
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	struct pagevec pv;
D
David Howells 已提交
217
	unsigned int loop, psize;
218

D
David Howells 已提交
219 220
	_enter("{%llx:%llu},%llx @%llx",
	       vnode->fid.vid, vnode->fid.vnode, len, start);
221

222
	pagevec_init(&pv);
223 224

	do {
D
David Howells 已提交
225
		_debug("redirty %llx @%llx", len, start);
226

D
David Howells 已提交
227 228 229 230
		pv.nr = find_get_pages_contig(mapping, start / PAGE_SIZE,
					      PAGEVEC_SIZE, pv.pages);
		if (pv.nr == 0)
			break;
231

D
David Howells 已提交
232
		for (loop = 0; loop < pv.nr; loop++) {
233 234
			struct page *page = pv.pages[loop];

D
David Howells 已提交
235 236 237 238 239 240
			if (page->index * PAGE_SIZE >= start + len)
				break;

			psize = thp_size(page);
			start += psize;
			len -= psize;
241 242 243 244 245
			redirty_page_for_writepage(wbc, page);
			end_page_writeback(page);
		}

		__pagevec_release(&pv);
D
David Howells 已提交
246
	} while (len > 0);
247 248 249 250

	_leave("");
}

251 252 253
/*
 * completion of write to server
 */
D
David Howells 已提交
254
static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
255
{
D
David Howells 已提交
256 257
	struct address_space *mapping = vnode->vfs_inode.i_mapping;
	struct page *page;
D
David Howells 已提交
258
	pgoff_t end;
D
David Howells 已提交
259

D
David Howells 已提交
260
	XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
261

D
David Howells 已提交
262 263
	_enter("{%llx:%llu},{%x @%llx}",
	       vnode->fid.vid, vnode->fid.vnode, len, start);
264

D
David Howells 已提交
265
	rcu_read_lock();
266

D
David Howells 已提交
267 268 269 270 271 272
	end = (start + len - 1) / PAGE_SIZE;
	xas_for_each(&xas, page, end) {
		if (!PageWriteback(page)) {
			kdebug("bad %x @%llx page %lx %lx", len, start, page->index, end);
			ASSERT(PageWriteback(page));
		}
273

D
David Howells 已提交
274
		trace_afs_page_dirty(vnode, tracepoint_string("clear"), page);
D
David Howells 已提交
275
		detach_page_private(page);
D
David Howells 已提交
276 277
		page_endio(page, true, 0);
	}
278

D
David Howells 已提交
279
	rcu_read_unlock();
280 281 282 283 284

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

285
/*
286 287 288
 * 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.
289
 */
290 291
static int afs_get_writeback_key(struct afs_vnode *vnode,
				 struct afs_wb_key **_wbk)
292
{
293 294 295
	struct afs_wb_key *wbk = NULL;
	struct list_head *p;
	int ret = -ENOKEY, ret2;
296

297
	spin_lock(&vnode->wb_lock);
298 299 300 301
	if (*_wbk)
		p = (*_wbk)->vnode_link.next;
	else
		p = vnode->wb_keys.next;
302 303 304 305 306

	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);
307 308 309 310 311 312 313
		if (ret2 == 0) {
			refcount_inc(&wbk->usage);
			_debug("USE WB KEY %u", key_serial(wbk->key));
			break;
		}

		wbk = NULL;
314 315 316 317 318 319
		if (ret == -ENOKEY)
			ret = ret2;
		p = p->next;
	}

	spin_unlock(&vnode->wb_lock);
320 321 322 323 324
	if (*_wbk)
		afs_put_wb_key(*_wbk);
	*_wbk = wbk;
	return 0;
}
325

326 327 328
static void afs_store_data_success(struct afs_operation *op)
{
	struct afs_vnode *vnode = op->file[0].vnode;
329

D
David Howells 已提交
330
	op->ctime = op->file[0].scb.status.mtime_client;
331 332
	afs_vnode_commit_status(op, &op->file[0]);
	if (op->error == 0) {
333
		if (!op->store.laundering)
D
David Howells 已提交
334
			afs_pages_written_back(vnode, op->store.pos, op->store.size);
335
		afs_stat_v(vnode, n_stores);
D
David Howells 已提交
336
		atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
337 338
	}
}
339

340 341 342 343 344
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,
};
345

346 347 348
/*
 * write to a file
 */
D
David Howells 已提交
349
static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
D
David Howells 已提交
350
			  bool laundering)
351 352 353
{
	struct afs_operation *op;
	struct afs_wb_key *wbk = NULL;
D
David Howells 已提交
354 355
	loff_t size = iov_iter_count(iter), i_size;
	int ret = -ENOKEY;
356

D
David Howells 已提交
357
	_enter("%s{%llx:%llu.%u},%llx,%llx",
358 359 360 361
	       vnode->volume->name,
	       vnode->fid.vid,
	       vnode->fid.vnode,
	       vnode->fid.unique,
D
David Howells 已提交
362
	       size, pos);
363

364 365 366 367
	ret = afs_get_writeback_key(vnode, &wbk);
	if (ret) {
		_leave(" = %d [no keys]", ret);
		return ret;
368 369
	}

370 371 372 373 374 375
	op = afs_alloc_operation(wbk->key, vnode->volume);
	if (IS_ERR(op)) {
		afs_put_wb_key(wbk);
		return -ENOMEM;
	}

D
David Howells 已提交
376 377
	i_size = i_size_read(&vnode->vfs_inode);

378 379
	afs_op_set_vnode(op, 0, vnode);
	op->file[0].dv_delta = 1;
380
	op->file[0].modification = true;
D
David Howells 已提交
381 382 383 384
	op->store.write_iter = iter;
	op->store.pos = pos;
	op->store.size = size;
	op->store.i_size = max(pos + size, i_size);
385
	op->store.laundering = laundering;
386
	op->mtime = vnode->vfs_inode.i_mtime;
387
	op->flags |= AFS_OPERATION_UNINTR;
388 389 390 391 392 393 394
	op->ops = &afs_store_data_operation;

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

	switch (op->error) {
395 396 397 398 399 400 401
	case -EACCES:
	case -EPERM:
	case -ENOKEY:
	case -EKEYEXPIRED:
	case -EKEYREJECTED:
	case -EKEYREVOKED:
		_debug("next");
402 403 404 405 406 407 408 409

		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;
410 411 412
	}

	afs_put_wb_key(wbk);
413 414
	_leave(" = %d", op->error);
	return afs_put_operation(op);
415 416
}

417
/*
418 419 420 421 422
 * Extend the region to be written back to include subsequent contiguously
 * dirty pages if possible, but don't sleep while doing so.
 *
 * If this page holds new content, then we can include filler zeros in the
 * writeback.
423
 */
424 425 426
static void afs_extend_writeback(struct address_space *mapping,
				 struct afs_vnode *vnode,
				 long *_count,
D
David Howells 已提交
427 428 429 430
				 loff_t start,
				 loff_t max_len,
				 bool new_content,
				 unsigned int *_len)
431
{
D
David Howells 已提交
432 433 434 435 436 437 438 439 440 441 442 443
	struct pagevec pvec;
	struct page *page;
	unsigned long priv;
	unsigned int psize, filler = 0;
	unsigned int f, t;
	loff_t len = *_len;
	pgoff_t index = (start + len) / PAGE_SIZE;
	bool stop = true;
	unsigned int i;

	XA_STATE(xas, &mapping->i_pages, index);
	pagevec_init(&pvec);
444

445
	do {
D
David Howells 已提交
446 447 448 449 450
		/* Firstly, we gather up a batch of contiguous dirty pages
		 * under the RCU read lock - but we can't clear the dirty flags
		 * there if any of those pages are mapped.
		 */
		rcu_read_lock();
451

D
David Howells 已提交
452 453 454 455 456 457 458
		xas_for_each(&xas, page, ULONG_MAX) {
			stop = true;
			if (xas_retry(&xas, page))
				continue;
			if (xa_is_value(page))
				break;
			if (page->index != index)
459
				break;
D
David Howells 已提交
460 461 462 463 464 465 466 467

			if (!page_cache_get_speculative(page)) {
				xas_reset(&xas);
				continue;
			}

			/* Has the page moved or been split? */
			if (unlikely(page != xas_reload(&xas)))
468
				break;
D
David Howells 已提交
469

N
Nick Piggin 已提交
470
			if (!trylock_page(page))
471
				break;
472
			if (!PageDirty(page) || PageWriteback(page)) {
473 474 475
				unlock_page(page);
				break;
			}
476

D
David Howells 已提交
477
			psize = thp_size(page);
478
			priv = page_private(page);
479 480
			f = afs_page_dirty_from(page, priv);
			t = afs_page_dirty_to(page, priv);
481
			if (f != 0 && !new_content) {
482 483 484
				unlock_page(page);
				break;
			}
485

D
David Howells 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
			len += filler + t;
			filler = psize - t;
			if (len >= max_len || *_count <= 0)
				stop = true;
			else if (t == psize || new_content)
				stop = false;

			index += thp_nr_pages(page);
			if (!pagevec_add(&pvec, page))
				break;
			if (stop)
				break;
		}

		if (!stop)
			xas_pause(&xas);
		rcu_read_unlock();

		/* Now, if we obtained any pages, we can shift them to being
		 * writable and mark them for caching.
		 */
		if (!pagevec_count(&pvec))
			break;

		for (i = 0; i < pagevec_count(&pvec); i++) {
			page = pvec.pages[i];
512
			trace_afs_page_dirty(vnode, tracepoint_string("store+"), page);
D
David Howells 已提交
513

514 515 516 517
			if (!clear_page_dirty_for_io(page))
				BUG();
			if (test_set_page_writeback(page))
				BUG();
D
David Howells 已提交
518 519

			*_count -= thp_nr_pages(page);
520 521 522
			unlock_page(page);
		}

D
David Howells 已提交
523 524 525
		pagevec_release(&pvec);
		cond_resched();
	} while (!stop);
526

D
David Howells 已提交
527
	*_len = len;
528 529 530 531 532 533
}

/*
 * Synchronously write back the locked page and any subsequent non-locked dirty
 * pages.
 */
D
David Howells 已提交
534 535 536 537
static ssize_t afs_write_back_from_locked_page(struct address_space *mapping,
					       struct writeback_control *wbc,
					       struct page *page,
					       loff_t start, loff_t end)
538 539 540
{
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
	struct iov_iter iter;
D
David Howells 已提交
541 542 543
	unsigned long priv;
	unsigned int offset, to, len, max_len;
	loff_t i_size = i_size_read(&vnode->vfs_inode);
544
	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
D
David Howells 已提交
545
	long count = wbc->nr_to_write;
546 547
	int ret;

D
David Howells 已提交
548
	_enter(",%lx,%llx-%llx", page->index, start, end);
549

D
David Howells 已提交
550
	if (test_set_page_writeback(page))
551 552
		BUG();

D
David Howells 已提交
553 554
	count -= thp_nr_pages(page);

555 556 557 558 559
	/* 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.
	 */
D
David Howells 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	priv = page_private(page);
	offset = afs_page_dirty_from(page, priv);
	to = afs_page_dirty_to(page, priv);
	trace_afs_page_dirty(vnode, tracepoint_string("store"), page);

	len = to - offset;
	start += offset;
	if (start < i_size) {
		/* Trim the write to the EOF; the extra data is ignored.  Also
		 * put an upper limit on the size of a single storedata op.
		 */
		max_len = 65536 * 4096;
		max_len = min_t(unsigned long long, max_len, end - start + 1);
		max_len = min_t(unsigned long long, max_len, i_size - start);

		if (len < max_len &&
		    (to == thp_size(page) || new_content))
			afs_extend_writeback(mapping, vnode, &count,
					     start, max_len, new_content, &len);
		len = min_t(loff_t, len, max_len);
	}
581

582 583 584 585
	/* 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.
	 */
D
David Howells 已提交
586
	unlock_page(page);
587

D
David Howells 已提交
588 589
	if (start < i_size) {
		_debug("write back %x @%llx [%llx]", len, start, i_size);
D
David Howells 已提交
590

D
David Howells 已提交
591 592
		iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
		ret = afs_store_data(vnode, &iter, start, false);
D
David Howells 已提交
593
	} else {
D
David Howells 已提交
594 595
		_debug("write discard %x @%llx [%llx]", len, start, i_size);

D
David Howells 已提交
596
		/* The dirty region was entirely beyond the EOF. */
D
David Howells 已提交
597
		afs_pages_written_back(vnode, start, len);
D
David Howells 已提交
598 599
		ret = 0;
	}
600

601 602
	switch (ret) {
	case 0:
D
David Howells 已提交
603 604
		wbc->nr_to_write = count;
		ret = len;
605 606 607 608
		break;

	default:
		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
609
		fallthrough;
610 611 612 613 614 615
	case -EACCES:
	case -EPERM:
	case -ENOKEY:
	case -EKEYEXPIRED:
	case -EKEYREJECTED:
	case -EKEYREVOKED:
D
David Howells 已提交
616
		afs_redirty_pages(wbc, mapping, start, len);
617 618 619 620 621
		mapping_set_error(mapping, ret);
		break;

	case -EDQUOT:
	case -ENOSPC:
D
David Howells 已提交
622
		afs_redirty_pages(wbc, mapping, start, len);
623 624 625 626 627 628 629 630 631 632
		mapping_set_error(mapping, -ENOSPC);
		break;

	case -EROFS:
	case -EIO:
	case -EREMOTEIO:
	case -EFBIG:
	case -ENOENT:
	case -ENOMEDIUM:
	case -ENXIO:
633
		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
D
David Howells 已提交
634
		afs_kill_pages(mapping, start, len);
635 636
		mapping_set_error(mapping, ret);
		break;
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)
{
D
David Howells 已提交
649 650
	ssize_t ret;
	loff_t start;
651 652 653

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

D
David Howells 已提交
654
	start = page->index * PAGE_SIZE;
655
	ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
D
David Howells 已提交
656
					      start, LLONG_MAX - start);
657
	if (ret < 0) {
D
David Howells 已提交
658 659
		_leave(" = %zd", ret);
		return ret;
660 661 662 663 664 665 666 667 668
	}

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

/*
 * write a region of pages back to the server
 */
A
Adrian Bunk 已提交
669 670
static int afs_writepages_region(struct address_space *mapping,
				 struct writeback_control *wbc,
D
David Howells 已提交
671
				 loff_t start, loff_t end, loff_t *_next)
672 673
{
	struct page *page;
D
David Howells 已提交
674 675
	ssize_t ret;
	int n;
676

D
David Howells 已提交
677
	_enter("%llx,%llx,", start, end);
678 679

	do {
D
David Howells 已提交
680 681 682 683
		pgoff_t index = start / PAGE_SIZE;

		n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE,
					     PAGECACHE_TAG_DIRTY, 1, &page);
684 685 686
		if (!n)
			break;

D
David Howells 已提交
687 688
		start = (loff_t)page->index * PAGE_SIZE; /* May regress with THPs */

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

D
David Howells 已提交
691
		/* At this point we hold neither the i_pages lock nor the
M
Matthew Wilcox 已提交
692 693 694
		 * 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
695
		 */
D
David Howells 已提交
696 697 698 699 700 701 702 703 704 705 706
		if (wbc->sync_mode != WB_SYNC_NONE) {
			ret = lock_page_killable(page);
			if (ret < 0) {
				put_page(page);
				return ret;
			}
		} else {
			if (!trylock_page(page)) {
				put_page(page);
				return 0;
			}
707
		}
708

709
		if (page->mapping != mapping || !PageDirty(page)) {
D
David Howells 已提交
710
			start += thp_size(page);
711
			unlock_page(page);
712
			put_page(page);
713 714 715
			continue;
		}

716
		if (PageWriteback(page)) {
717
			unlock_page(page);
718 719
			if (wbc->sync_mode != WB_SYNC_NONE)
				wait_on_page_writeback(page);
D
David Howells 已提交
720
			put_page(page);
721 722 723
			continue;
		}

724 725
		if (!clear_page_dirty_for_io(page))
			BUG();
D
David Howells 已提交
726
		ret = afs_write_back_from_locked_page(mapping, wbc, page, start, end);
727
		put_page(page);
728
		if (ret < 0) {
D
David Howells 已提交
729
			_leave(" = %zd", ret);
730 731 732
			return ret;
		}

733
		start += ret;
734 735

		cond_resched();
D
David Howells 已提交
736
	} while (wbc->nr_to_write > 0);
737

D
David Howells 已提交
738 739
	*_next = start;
	_leave(" = 0 [%llx]", *_next);
740 741 742 743 744 745 746 747 748
	return 0;
}

/*
 * write some of the pending data back to the server
 */
int afs_writepages(struct address_space *mapping,
		   struct writeback_control *wbc)
{
749
	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
D
David Howells 已提交
750
	loff_t start, next;
751 752 753 754
	int ret;

	_enter("");

755 756 757 758 759 760 761 762 763
	/* We have to be careful as we can end up racing with setattr()
	 * truncating the pagecache since the caller doesn't take a lock here
	 * to prevent it.
	 */
	if (wbc->sync_mode == WB_SYNC_ALL)
		down_read(&vnode->validate_lock);
	else if (!down_read_trylock(&vnode->validate_lock))
		return 0;

764
	if (wbc->range_cyclic) {
D
David Howells 已提交
765 766
		start = mapping->writeback_index * PAGE_SIZE;
		ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
767
		if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
768 769
			ret = afs_writepages_region(mapping, wbc, 0, start,
						    &next);
D
David Howells 已提交
770
		mapping->writeback_index = next / PAGE_SIZE;
771
	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
D
David Howells 已提交
772
		ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
773 774 775
		if (wbc->nr_to_write > 0)
			mapping->writeback_index = next;
	} else {
D
David Howells 已提交
776 777
		ret = afs_writepages_region(mapping, wbc,
					    wbc->range_start, wbc->range_end, &next);
778 779
	}

780
	up_read(&vnode->validate_lock);
781 782 783 784 785 786 787
	_leave(" = %d", ret);
	return ret;
}

/*
 * write to an AFS file
 */
A
Al Viro 已提交
788
ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
789
{
A
Al Viro 已提交
790
	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
791
	ssize_t result;
A
Al Viro 已提交
792
	size_t count = iov_iter_count(from);
793

794
	_enter("{%llx:%llu},{%zu},",
A
Al Viro 已提交
795
	       vnode->fid.vid, vnode->fid.vnode, count);
796 797 798 799 800 801 802 803 804 805

	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 已提交
806
	result = generic_file_write_iter(iocb, from);
807 808 809 810 811 812 813 814 815 816

	_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.
 */
817
int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
818
{
819 820
	struct inode *inode = file_inode(file);
	struct afs_vnode *vnode = AFS_FS_I(inode);
821

822
	_enter("{%llx:%llu},{n=%pD},%d",
823
	       vnode->fid.vid, vnode->fid.vnode, file,
824 825
	       datasync);

826
	return file_write_and_wait_range(file, start, end);
827
}
D
David Howells 已提交
828 829 830 831 832

/*
 * 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
 */
833
vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
D
David Howells 已提交
834
{
D
David Howells 已提交
835
	struct page *page = thp_head(vmf->page);
836 837 838 839
	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 已提交
840

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

843
	sb_start_pagefault(inode->i_sb);
D
David Howells 已提交
844

845 846 847
	/* 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.
	 */
848
#ifdef CONFIG_AFS_FSCACHE
D
David Howells 已提交
849
	if (PageFsCache(page) &&
850
	    wait_on_page_fscache_killable(page) < 0)
851 852
		return VM_FAULT_RETRY;
#endif
D
David Howells 已提交
853

D
David Howells 已提交
854
	if (wait_on_page_writeback_killable(page))
855 856
		return VM_FAULT_RETRY;

D
David Howells 已提交
857
	if (lock_page_killable(page) < 0)
858 859 860 861 862 863
		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.
	 */
864 865 866 867
	if (wait_on_page_writeback_killable(page) < 0) {
		unlock_page(page);
		return VM_FAULT_RETRY;
	}
868

D
David Howells 已提交
869
	priv = afs_page_dirty(page, 0, thp_size(page));
870
	priv = afs_page_dirty_mmapped(priv);
D
David Howells 已提交
871 872 873 874 875 876 877
	if (PagePrivate(page)) {
		set_page_private(page, priv);
		trace_afs_page_dirty(vnode, tracepoint_string("mkwrite+"), page);
	} else {
		attach_page_private(page, (void *)priv);
		trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"), page);
	}
878
	file_update_time(file);
879 880 881

	sb_end_pagefault(inode->i_sb);
	return VM_FAULT_LOCKED;
D
David Howells 已提交
882
}
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

/*
 * 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);
D
David Howells 已提交
919 920
	struct iov_iter iter;
	struct bio_vec bv[1];
921 922 923 924 925 926 927 928 929
	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;
D
David Howells 已提交
930
		t = thp_size(page);
931
		if (PagePrivate(page)) {
932 933
			f = afs_page_dirty_from(page, priv);
			t = afs_page_dirty_to(page, priv);
934 935
		}

D
David Howells 已提交
936 937 938 939 940
		bv[0].bv_page = page;
		bv[0].bv_offset = f;
		bv[0].bv_len = t - f;
		iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len);

941
		trace_afs_page_dirty(vnode, tracepoint_string("launder"), page);
D
David Howells 已提交
942 943
		ret = afs_store_data(vnode, &iter, (loff_t)page->index * PAGE_SIZE,
				     true);
944 945
	}

946
	trace_afs_page_dirty(vnode, tracepoint_string("laundered"), page);
D
David Howells 已提交
947
	detach_page_private(page);
948
	wait_on_page_fscache(page);
949
	return ret;
D
David Howells 已提交
950
}