xref: /linux/fs/afs/write.c (revision 680e6ffa15103ab610c0fc1241d2f98c801b13e2)
1 /* handling of writes to regular files and writing back to the server
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/backing-dev.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 #include <linux/pagemap.h>
16 #include <linux/writeback.h>
17 #include <linux/pagevec.h>
18 #include "internal.h"
19 
20 /*
21  * mark a page as having been made dirty and thus needing writeback
22  */
23 int afs_set_page_dirty(struct page *page)
24 {
25 	_enter("");
26 	return __set_page_dirty_nobuffers(page);
27 }
28 
29 /*
30  * partly or wholly fill a page that's under preparation for writing
31  */
32 static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
33 			 loff_t pos, unsigned int len, struct page *page)
34 {
35 	struct afs_read *req;
36 	size_t p;
37 	void *data;
38 	int ret;
39 
40 	_enter(",,%llu", (unsigned long long)pos);
41 
42 	if (pos >= vnode->vfs_inode.i_size) {
43 		p = pos & ~PAGE_MASK;
44 		ASSERTCMP(p + len, <=, PAGE_SIZE);
45 		data = kmap(page);
46 		memset(data + p, 0, len);
47 		kunmap(page);
48 		return 0;
49 	}
50 
51 	req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
52 		      GFP_KERNEL);
53 	if (!req)
54 		return -ENOMEM;
55 
56 	refcount_set(&req->usage, 1);
57 	req->pos = pos;
58 	req->len = len;
59 	req->nr_pages = 1;
60 	req->pages = req->array;
61 	req->pages[0] = page;
62 	get_page(page);
63 
64 	ret = afs_fetch_data(vnode, key, req);
65 	afs_put_read(req);
66 	if (ret < 0) {
67 		if (ret == -ENOENT) {
68 			_debug("got NOENT from server"
69 			       " - marking file deleted and stale");
70 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
71 			ret = -ESTALE;
72 		}
73 	}
74 
75 	_leave(" = %d", ret);
76 	return ret;
77 }
78 
79 /*
80  * prepare to perform part of a write to a page
81  */
82 int afs_write_begin(struct file *file, struct address_space *mapping,
83 		    loff_t pos, unsigned len, unsigned flags,
84 		    struct page **pagep, void **fsdata)
85 {
86 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
87 	struct page *page;
88 	struct key *key = afs_file_key(file);
89 	unsigned long priv;
90 	unsigned f, from = pos & (PAGE_SIZE - 1);
91 	unsigned t, to = from + len;
92 	pgoff_t index = pos >> PAGE_SHIFT;
93 	int ret;
94 
95 	_enter("{%llx:%llu},{%lx},%u,%u",
96 	       vnode->fid.vid, vnode->fid.vnode, index, from, to);
97 
98 	/* We want to store information about how much of a page is altered in
99 	 * page->private.
100 	 */
101 	BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
102 
103 	page = grab_cache_page_write_begin(mapping, index, flags);
104 	if (!page)
105 		return -ENOMEM;
106 
107 	if (!PageUptodate(page) && len != PAGE_SIZE) {
108 		ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
109 		if (ret < 0) {
110 			unlock_page(page);
111 			put_page(page);
112 			_leave(" = %d [prep]", ret);
113 			return ret;
114 		}
115 		SetPageUptodate(page);
116 	}
117 
118 	/* page won't leak in error case: it eventually gets cleaned off LRU */
119 	*pagep = page;
120 
121 try_again:
122 	/* See if this page is already partially written in a way that we can
123 	 * merge the new write with.
124 	 */
125 	t = f = 0;
126 	if (PagePrivate(page)) {
127 		priv = page_private(page);
128 		f = priv & AFS_PRIV_MAX;
129 		t = priv >> AFS_PRIV_SHIFT;
130 		ASSERTCMP(f, <=, t);
131 	}
132 
133 	if (f != t) {
134 		if (PageWriteback(page)) {
135 			trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
136 					     page->index, priv);
137 			goto flush_conflicting_write;
138 		}
139 		/* If the file is being filled locally, allow inter-write
140 		 * spaces to be merged into writes.  If it's not, only write
141 		 * back what the user gives us.
142 		 */
143 		if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
144 		    (to < f || from > t))
145 			goto flush_conflicting_write;
146 		if (from < f)
147 			f = from;
148 		if (to > t)
149 			t = to;
150 	} else {
151 		f = from;
152 		t = to;
153 	}
154 
155 	priv = (unsigned long)t << AFS_PRIV_SHIFT;
156 	priv |= f;
157 	trace_afs_page_dirty(vnode, tracepoint_string("begin"),
158 			     page->index, priv);
159 	SetPagePrivate(page);
160 	set_page_private(page, priv);
161 	_leave(" = 0");
162 	return 0;
163 
164 	/* The previous write and this write aren't adjacent or overlapping, so
165 	 * flush the page out.
166 	 */
167 flush_conflicting_write:
168 	_debug("flush conflict");
169 	ret = write_one_page(page);
170 	if (ret < 0) {
171 		_leave(" = %d", ret);
172 		return ret;
173 	}
174 
175 	ret = lock_page_killable(page);
176 	if (ret < 0) {
177 		_leave(" = %d", ret);
178 		return ret;
179 	}
180 	goto try_again;
181 }
182 
183 /*
184  * finalise part of a write to a page
185  */
186 int afs_write_end(struct file *file, struct address_space *mapping,
187 		  loff_t pos, unsigned len, unsigned copied,
188 		  struct page *page, void *fsdata)
189 {
190 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
191 	struct key *key = afs_file_key(file);
192 	loff_t i_size, maybe_i_size;
193 	int ret;
194 
195 	_enter("{%llx:%llu},{%lx}",
196 	       vnode->fid.vid, vnode->fid.vnode, page->index);
197 
198 	maybe_i_size = pos + copied;
199 
200 	i_size = i_size_read(&vnode->vfs_inode);
201 	if (maybe_i_size > i_size) {
202 		spin_lock(&vnode->wb_lock);
203 		i_size = i_size_read(&vnode->vfs_inode);
204 		if (maybe_i_size > i_size)
205 			i_size_write(&vnode->vfs_inode, maybe_i_size);
206 		spin_unlock(&vnode->wb_lock);
207 	}
208 
209 	if (!PageUptodate(page)) {
210 		if (copied < len) {
211 			/* Try and load any missing data from the server.  The
212 			 * unmarshalling routine will take care of clearing any
213 			 * bits that are beyond the EOF.
214 			 */
215 			ret = afs_fill_page(vnode, key, pos + copied,
216 					    len - copied, page);
217 			if (ret < 0)
218 				goto out;
219 		}
220 		SetPageUptodate(page);
221 	}
222 
223 	set_page_dirty(page);
224 	if (PageDirty(page))
225 		_debug("dirtied");
226 	ret = copied;
227 
228 out:
229 	unlock_page(page);
230 	put_page(page);
231 	return ret;
232 }
233 
234 /*
235  * kill all the pages in the given range
236  */
237 static void afs_kill_pages(struct address_space *mapping,
238 			   pgoff_t first, pgoff_t last)
239 {
240 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
241 	struct pagevec pv;
242 	unsigned count, loop;
243 
244 	_enter("{%llx:%llu},%lx-%lx",
245 	       vnode->fid.vid, vnode->fid.vnode, first, last);
246 
247 	pagevec_init(&pv);
248 
249 	do {
250 		_debug("kill %lx-%lx", first, last);
251 
252 		count = last - first + 1;
253 		if (count > PAGEVEC_SIZE)
254 			count = PAGEVEC_SIZE;
255 		pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
256 		ASSERTCMP(pv.nr, ==, count);
257 
258 		for (loop = 0; loop < count; loop++) {
259 			struct page *page = pv.pages[loop];
260 			ClearPageUptodate(page);
261 			SetPageError(page);
262 			end_page_writeback(page);
263 			if (page->index >= first)
264 				first = page->index + 1;
265 			lock_page(page);
266 			generic_error_remove_page(mapping, page);
267 		}
268 
269 		__pagevec_release(&pv);
270 	} while (first <= last);
271 
272 	_leave("");
273 }
274 
275 /*
276  * Redirty all the pages in a given range.
277  */
278 static void afs_redirty_pages(struct writeback_control *wbc,
279 			      struct address_space *mapping,
280 			      pgoff_t first, pgoff_t last)
281 {
282 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
283 	struct pagevec pv;
284 	unsigned count, loop;
285 
286 	_enter("{%llx:%llu},%lx-%lx",
287 	       vnode->fid.vid, vnode->fid.vnode, first, last);
288 
289 	pagevec_init(&pv);
290 
291 	do {
292 		_debug("redirty %lx-%lx", first, last);
293 
294 		count = last - first + 1;
295 		if (count > PAGEVEC_SIZE)
296 			count = PAGEVEC_SIZE;
297 		pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
298 		ASSERTCMP(pv.nr, ==, count);
299 
300 		for (loop = 0; loop < count; loop++) {
301 			struct page *page = pv.pages[loop];
302 
303 			redirty_page_for_writepage(wbc, page);
304 			end_page_writeback(page);
305 			if (page->index >= first)
306 				first = page->index + 1;
307 		}
308 
309 		__pagevec_release(&pv);
310 	} while (first <= last);
311 
312 	_leave("");
313 }
314 
315 /*
316  * write to a file
317  */
318 static int afs_store_data(struct address_space *mapping,
319 			  pgoff_t first, pgoff_t last,
320 			  unsigned offset, unsigned to)
321 {
322 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
323 	struct afs_fs_cursor fc;
324 	struct afs_wb_key *wbk = NULL;
325 	struct list_head *p;
326 	int ret = -ENOKEY, ret2;
327 
328 	_enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x",
329 	       vnode->volume->name,
330 	       vnode->fid.vid,
331 	       vnode->fid.vnode,
332 	       vnode->fid.unique,
333 	       first, last, offset, to);
334 
335 	spin_lock(&vnode->wb_lock);
336 	p = vnode->wb_keys.next;
337 
338 	/* Iterate through the list looking for a valid key to use. */
339 try_next_key:
340 	while (p != &vnode->wb_keys) {
341 		wbk = list_entry(p, struct afs_wb_key, vnode_link);
342 		_debug("wbk %u", key_serial(wbk->key));
343 		ret2 = key_validate(wbk->key);
344 		if (ret2 == 0)
345 			goto found_key;
346 		if (ret == -ENOKEY)
347 			ret = ret2;
348 		p = p->next;
349 	}
350 
351 	spin_unlock(&vnode->wb_lock);
352 	afs_put_wb_key(wbk);
353 	_leave(" = %d [no keys]", ret);
354 	return ret;
355 
356 found_key:
357 	refcount_inc(&wbk->usage);
358 	spin_unlock(&vnode->wb_lock);
359 
360 	_debug("USE WB KEY %u", key_serial(wbk->key));
361 
362 	ret = -ERESTARTSYS;
363 	if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) {
364 		while (afs_select_fileserver(&fc)) {
365 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
366 			afs_fs_store_data(&fc, mapping, first, last, offset, to);
367 		}
368 
369 		afs_check_for_remote_deletion(&fc, fc.vnode);
370 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
371 		ret = afs_end_vnode_operation(&fc);
372 	}
373 
374 	switch (ret) {
375 	case 0:
376 		afs_stat_v(vnode, n_stores);
377 		atomic_long_add((last * PAGE_SIZE + to) -
378 				(first * PAGE_SIZE + offset),
379 				&afs_v2net(vnode)->n_store_bytes);
380 		break;
381 	case -EACCES:
382 	case -EPERM:
383 	case -ENOKEY:
384 	case -EKEYEXPIRED:
385 	case -EKEYREJECTED:
386 	case -EKEYREVOKED:
387 		_debug("next");
388 		spin_lock(&vnode->wb_lock);
389 		p = wbk->vnode_link.next;
390 		afs_put_wb_key(wbk);
391 		goto try_next_key;
392 	}
393 
394 	afs_put_wb_key(wbk);
395 	_leave(" = %d", ret);
396 	return ret;
397 }
398 
399 /*
400  * Synchronously write back the locked page and any subsequent non-locked dirty
401  * pages.
402  */
403 static int afs_write_back_from_locked_page(struct address_space *mapping,
404 					   struct writeback_control *wbc,
405 					   struct page *primary_page,
406 					   pgoff_t final_page)
407 {
408 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
409 	struct page *pages[8], *page;
410 	unsigned long count, priv;
411 	unsigned n, offset, to, f, t;
412 	pgoff_t start, first, last;
413 	int loop, ret;
414 
415 	_enter(",%lx", primary_page->index);
416 
417 	count = 1;
418 	if (test_set_page_writeback(primary_page))
419 		BUG();
420 
421 	/* Find all consecutive lockable dirty pages that have contiguous
422 	 * written regions, stopping when we find a page that is not
423 	 * immediately lockable, is not dirty or is missing, or we reach the
424 	 * end of the range.
425 	 */
426 	start = primary_page->index;
427 	priv = page_private(primary_page);
428 	offset = priv & AFS_PRIV_MAX;
429 	to = priv >> AFS_PRIV_SHIFT;
430 	trace_afs_page_dirty(vnode, tracepoint_string("store"),
431 			     primary_page->index, priv);
432 
433 	WARN_ON(offset == to);
434 	if (offset == to)
435 		trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
436 				     primary_page->index, priv);
437 
438 	if (start >= final_page ||
439 	    (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)))
440 		goto no_more;
441 
442 	start++;
443 	do {
444 		_debug("more %lx [%lx]", start, count);
445 		n = final_page - start + 1;
446 		if (n > ARRAY_SIZE(pages))
447 			n = ARRAY_SIZE(pages);
448 		n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
449 		_debug("fgpc %u", n);
450 		if (n == 0)
451 			goto no_more;
452 		if (pages[0]->index != start) {
453 			do {
454 				put_page(pages[--n]);
455 			} while (n > 0);
456 			goto no_more;
457 		}
458 
459 		for (loop = 0; loop < n; loop++) {
460 			page = pages[loop];
461 			if (to != PAGE_SIZE &&
462 			    !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))
463 				break;
464 			if (page->index > final_page)
465 				break;
466 			if (!trylock_page(page))
467 				break;
468 			if (!PageDirty(page) || PageWriteback(page)) {
469 				unlock_page(page);
470 				break;
471 			}
472 
473 			priv = page_private(page);
474 			f = priv & AFS_PRIV_MAX;
475 			t = priv >> AFS_PRIV_SHIFT;
476 			if (f != 0 &&
477 			    !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) {
478 				unlock_page(page);
479 				break;
480 			}
481 			to = t;
482 
483 			trace_afs_page_dirty(vnode, tracepoint_string("store+"),
484 					     page->index, priv);
485 
486 			if (!clear_page_dirty_for_io(page))
487 				BUG();
488 			if (test_set_page_writeback(page))
489 				BUG();
490 			unlock_page(page);
491 			put_page(page);
492 		}
493 		count += loop;
494 		if (loop < n) {
495 			for (; loop < n; loop++)
496 				put_page(pages[loop]);
497 			goto no_more;
498 		}
499 
500 		start += loop;
501 	} while (start <= final_page && count < 65536);
502 
503 no_more:
504 	/* We now have a contiguous set of dirty pages, each with writeback
505 	 * set; the first page is still locked at this point, but all the rest
506 	 * have been unlocked.
507 	 */
508 	unlock_page(primary_page);
509 
510 	first = primary_page->index;
511 	last = first + count - 1;
512 
513 	_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
514 
515 	ret = afs_store_data(mapping, first, last, offset, to);
516 	switch (ret) {
517 	case 0:
518 		ret = count;
519 		break;
520 
521 	default:
522 		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
523 		/* Fall through */
524 	case -EACCES:
525 	case -EPERM:
526 	case -ENOKEY:
527 	case -EKEYEXPIRED:
528 	case -EKEYREJECTED:
529 	case -EKEYREVOKED:
530 		afs_redirty_pages(wbc, mapping, first, last);
531 		mapping_set_error(mapping, ret);
532 		break;
533 
534 	case -EDQUOT:
535 	case -ENOSPC:
536 		afs_redirty_pages(wbc, mapping, first, last);
537 		mapping_set_error(mapping, -ENOSPC);
538 		break;
539 
540 	case -EROFS:
541 	case -EIO:
542 	case -EREMOTEIO:
543 	case -EFBIG:
544 	case -ENOENT:
545 	case -ENOMEDIUM:
546 	case -ENXIO:
547 		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
548 		afs_kill_pages(mapping, first, last);
549 		mapping_set_error(mapping, ret);
550 		break;
551 	}
552 
553 	_leave(" = %d", ret);
554 	return ret;
555 }
556 
557 /*
558  * write a page back to the server
559  * - the caller locked the page for us
560  */
561 int afs_writepage(struct page *page, struct writeback_control *wbc)
562 {
563 	int ret;
564 
565 	_enter("{%lx},", page->index);
566 
567 	ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
568 					      wbc->range_end >> PAGE_SHIFT);
569 	if (ret < 0) {
570 		_leave(" = %d", ret);
571 		return 0;
572 	}
573 
574 	wbc->nr_to_write -= ret;
575 
576 	_leave(" = 0");
577 	return 0;
578 }
579 
580 /*
581  * write a region of pages back to the server
582  */
583 static int afs_writepages_region(struct address_space *mapping,
584 				 struct writeback_control *wbc,
585 				 pgoff_t index, pgoff_t end, pgoff_t *_next)
586 {
587 	struct page *page;
588 	int ret, n;
589 
590 	_enter(",,%lx,%lx,", index, end);
591 
592 	do {
593 		n = find_get_pages_range_tag(mapping, &index, end,
594 					PAGECACHE_TAG_DIRTY, 1, &page);
595 		if (!n)
596 			break;
597 
598 		_debug("wback %lx", page->index);
599 
600 		/*
601 		 * at this point we hold neither the i_pages lock nor the
602 		 * page lock: the page may be truncated or invalidated
603 		 * (changing page->mapping to NULL), or even swizzled
604 		 * back from swapper_space to tmpfs file mapping
605 		 */
606 		ret = lock_page_killable(page);
607 		if (ret < 0) {
608 			put_page(page);
609 			_leave(" = %d", ret);
610 			return ret;
611 		}
612 
613 		if (page->mapping != mapping || !PageDirty(page)) {
614 			unlock_page(page);
615 			put_page(page);
616 			continue;
617 		}
618 
619 		if (PageWriteback(page)) {
620 			unlock_page(page);
621 			if (wbc->sync_mode != WB_SYNC_NONE)
622 				wait_on_page_writeback(page);
623 			put_page(page);
624 			continue;
625 		}
626 
627 		if (!clear_page_dirty_for_io(page))
628 			BUG();
629 		ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
630 		put_page(page);
631 		if (ret < 0) {
632 			_leave(" = %d", ret);
633 			return ret;
634 		}
635 
636 		wbc->nr_to_write -= ret;
637 
638 		cond_resched();
639 	} while (index < end && wbc->nr_to_write > 0);
640 
641 	*_next = index;
642 	_leave(" = 0 [%lx]", *_next);
643 	return 0;
644 }
645 
646 /*
647  * write some of the pending data back to the server
648  */
649 int afs_writepages(struct address_space *mapping,
650 		   struct writeback_control *wbc)
651 {
652 	pgoff_t start, end, next;
653 	int ret;
654 
655 	_enter("");
656 
657 	if (wbc->range_cyclic) {
658 		start = mapping->writeback_index;
659 		end = -1;
660 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
661 		if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
662 			ret = afs_writepages_region(mapping, wbc, 0, start,
663 						    &next);
664 		mapping->writeback_index = next;
665 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
666 		end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
667 		ret = afs_writepages_region(mapping, wbc, 0, end, &next);
668 		if (wbc->nr_to_write > 0)
669 			mapping->writeback_index = next;
670 	} else {
671 		start = wbc->range_start >> PAGE_SHIFT;
672 		end = wbc->range_end >> PAGE_SHIFT;
673 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
674 	}
675 
676 	_leave(" = %d", ret);
677 	return ret;
678 }
679 
680 /*
681  * completion of write to server
682  */
683 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
684 {
685 	struct pagevec pv;
686 	unsigned long priv;
687 	unsigned count, loop;
688 	pgoff_t first = call->first, last = call->last;
689 
690 	_enter("{%llx:%llu},{%lx-%lx}",
691 	       vnode->fid.vid, vnode->fid.vnode, first, last);
692 
693 	pagevec_init(&pv);
694 
695 	do {
696 		_debug("done %lx-%lx", first, last);
697 
698 		count = last - first + 1;
699 		if (count > PAGEVEC_SIZE)
700 			count = PAGEVEC_SIZE;
701 		pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
702 					      first, count, pv.pages);
703 		ASSERTCMP(pv.nr, ==, count);
704 
705 		for (loop = 0; loop < count; loop++) {
706 			priv = page_private(pv.pages[loop]);
707 			trace_afs_page_dirty(vnode, tracepoint_string("clear"),
708 					     pv.pages[loop]->index, priv);
709 			set_page_private(pv.pages[loop], 0);
710 			end_page_writeback(pv.pages[loop]);
711 		}
712 		first += count;
713 		__pagevec_release(&pv);
714 	} while (first <= last);
715 
716 	afs_prune_wb_keys(vnode);
717 	_leave("");
718 }
719 
720 /*
721  * write to an AFS file
722  */
723 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
724 {
725 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
726 	ssize_t result;
727 	size_t count = iov_iter_count(from);
728 
729 	_enter("{%llx:%llu},{%zu},",
730 	       vnode->fid.vid, vnode->fid.vnode, count);
731 
732 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
733 		printk(KERN_INFO
734 		       "AFS: Attempt to write to active swap file!\n");
735 		return -EBUSY;
736 	}
737 
738 	if (!count)
739 		return 0;
740 
741 	result = generic_file_write_iter(iocb, from);
742 
743 	_leave(" = %zd", result);
744 	return result;
745 }
746 
747 /*
748  * flush any dirty pages for this process, and check for write errors.
749  * - the return status from this call provides a reliable indication of
750  *   whether any write errors occurred for this process.
751  */
752 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
753 {
754 	struct inode *inode = file_inode(file);
755 	struct afs_vnode *vnode = AFS_FS_I(inode);
756 
757 	_enter("{%llx:%llu},{n=%pD},%d",
758 	       vnode->fid.vid, vnode->fid.vnode, file,
759 	       datasync);
760 
761 	return file_write_and_wait_range(file, start, end);
762 }
763 
764 /*
765  * notification that a previously read-only page is about to become writable
766  * - if it returns an error, the caller will deliver a bus error signal
767  */
768 vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
769 {
770 	struct file *file = vmf->vma->vm_file;
771 	struct inode *inode = file_inode(file);
772 	struct afs_vnode *vnode = AFS_FS_I(inode);
773 	unsigned long priv;
774 
775 	_enter("{{%llx:%llu}},{%lx}",
776 	       vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
777 
778 	sb_start_pagefault(inode->i_sb);
779 
780 	/* Wait for the page to be written to the cache before we allow it to
781 	 * be modified.  We then assume the entire page will need writing back.
782 	 */
783 #ifdef CONFIG_AFS_FSCACHE
784 	fscache_wait_on_page_write(vnode->cache, vmf->page);
785 #endif
786 
787 	if (PageWriteback(vmf->page) &&
788 	    wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
789 		return VM_FAULT_RETRY;
790 
791 	if (lock_page_killable(vmf->page) < 0)
792 		return VM_FAULT_RETRY;
793 
794 	/* We mustn't change page->private until writeback is complete as that
795 	 * details the portion of the page we need to write back and we might
796 	 * need to redirty the page if there's a problem.
797 	 */
798 	wait_on_page_writeback(vmf->page);
799 
800 	priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
801 	priv |= 0; /* From */
802 	trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
803 			     vmf->page->index, priv);
804 	SetPagePrivate(vmf->page);
805 	set_page_private(vmf->page, priv);
806 
807 	sb_end_pagefault(inode->i_sb);
808 	return VM_FAULT_LOCKED;
809 }
810 
811 /*
812  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
813  */
814 void afs_prune_wb_keys(struct afs_vnode *vnode)
815 {
816 	LIST_HEAD(graveyard);
817 	struct afs_wb_key *wbk, *tmp;
818 
819 	/* Discard unused keys */
820 	spin_lock(&vnode->wb_lock);
821 
822 	if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
823 	    !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
824 		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
825 			if (refcount_read(&wbk->usage) == 1)
826 				list_move(&wbk->vnode_link, &graveyard);
827 		}
828 	}
829 
830 	spin_unlock(&vnode->wb_lock);
831 
832 	while (!list_empty(&graveyard)) {
833 		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
834 		list_del(&wbk->vnode_link);
835 		afs_put_wb_key(wbk);
836 	}
837 }
838 
839 /*
840  * Clean up a page during invalidation.
841  */
842 int afs_launder_page(struct page *page)
843 {
844 	struct address_space *mapping = page->mapping;
845 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
846 	unsigned long priv;
847 	unsigned int f, t;
848 	int ret = 0;
849 
850 	_enter("{%lx}", page->index);
851 
852 	priv = page_private(page);
853 	if (clear_page_dirty_for_io(page)) {
854 		f = 0;
855 		t = PAGE_SIZE;
856 		if (PagePrivate(page)) {
857 			f = priv & AFS_PRIV_MAX;
858 			t = priv >> AFS_PRIV_SHIFT;
859 		}
860 
861 		trace_afs_page_dirty(vnode, tracepoint_string("launder"),
862 				     page->index, priv);
863 		ret = afs_store_data(mapping, page->index, page->index, t, f);
864 	}
865 
866 	trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
867 			     page->index, priv);
868 	set_page_private(page, 0);
869 	ClearPagePrivate(page);
870 
871 #ifdef CONFIG_AFS_FSCACHE
872 	if (PageFsCache(page)) {
873 		fscache_wait_on_page_write(vnode->cache, page);
874 		fscache_uncache_page(vnode->cache, page);
875 	}
876 #endif
877 	return ret;
878 }
879