xref: /openbmc/linux/fs/afs/write.c (revision 29c8bbbd)
131143d5dSDavid Howells /* handling of writes to regular files and writing back to the server
231143d5dSDavid Howells  *
331143d5dSDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
431143d5dSDavid Howells  * Written by David Howells (dhowells@redhat.com)
531143d5dSDavid Howells  *
631143d5dSDavid Howells  * This program is free software; you can redistribute it and/or
731143d5dSDavid Howells  * modify it under the terms of the GNU General Public License
831143d5dSDavid Howells  * as published by the Free Software Foundation; either version
931143d5dSDavid Howells  * 2 of the License, or (at your option) any later version.
1031143d5dSDavid Howells  */
114af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
1231143d5dSDavid Howells #include <linux/slab.h>
1331143d5dSDavid Howells #include <linux/fs.h>
1431143d5dSDavid Howells #include <linux/pagemap.h>
1531143d5dSDavid Howells #include <linux/writeback.h>
1631143d5dSDavid Howells #include <linux/pagevec.h>
1731143d5dSDavid Howells #include "internal.h"
1831143d5dSDavid Howells 
1931143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb,
2031143d5dSDavid Howells 					   struct page *page);
2131143d5dSDavid Howells 
2231143d5dSDavid Howells /*
2331143d5dSDavid Howells  * mark a page as having been made dirty and thus needing writeback
2431143d5dSDavid Howells  */
2531143d5dSDavid Howells int afs_set_page_dirty(struct page *page)
2631143d5dSDavid Howells {
2731143d5dSDavid Howells 	_enter("");
2831143d5dSDavid Howells 	return __set_page_dirty_nobuffers(page);
2931143d5dSDavid Howells }
3031143d5dSDavid Howells 
3131143d5dSDavid Howells /*
3231143d5dSDavid Howells  * unlink a writeback record because its usage has reached zero
3331143d5dSDavid Howells  * - must be called with the wb->vnode->writeback_lock held
3431143d5dSDavid Howells  */
3531143d5dSDavid Howells static void afs_unlink_writeback(struct afs_writeback *wb)
3631143d5dSDavid Howells {
3731143d5dSDavid Howells 	struct afs_writeback *front;
3831143d5dSDavid Howells 	struct afs_vnode *vnode = wb->vnode;
3931143d5dSDavid Howells 
4031143d5dSDavid Howells 	list_del_init(&wb->link);
4131143d5dSDavid Howells 	if (!list_empty(&vnode->writebacks)) {
4231143d5dSDavid Howells 		/* if an fsync rises to the front of the queue then wake it
4331143d5dSDavid Howells 		 * up */
4431143d5dSDavid Howells 		front = list_entry(vnode->writebacks.next,
4531143d5dSDavid Howells 				   struct afs_writeback, link);
4631143d5dSDavid Howells 		if (front->state == AFS_WBACK_SYNCING) {
4731143d5dSDavid Howells 			_debug("wake up sync");
4831143d5dSDavid Howells 			front->state = AFS_WBACK_COMPLETE;
4931143d5dSDavid Howells 			wake_up(&front->waitq);
5031143d5dSDavid Howells 		}
5131143d5dSDavid Howells 	}
5231143d5dSDavid Howells }
5331143d5dSDavid Howells 
5431143d5dSDavid Howells /*
5531143d5dSDavid Howells  * free a writeback record
5631143d5dSDavid Howells  */
5731143d5dSDavid Howells static void afs_free_writeback(struct afs_writeback *wb)
5831143d5dSDavid Howells {
5931143d5dSDavid Howells 	_enter("");
6031143d5dSDavid Howells 	key_put(wb->key);
6131143d5dSDavid Howells 	kfree(wb);
6231143d5dSDavid Howells }
6331143d5dSDavid Howells 
6431143d5dSDavid Howells /*
6531143d5dSDavid Howells  * dispose of a reference to a writeback record
6631143d5dSDavid Howells  */
6731143d5dSDavid Howells void afs_put_writeback(struct afs_writeback *wb)
6831143d5dSDavid Howells {
6931143d5dSDavid Howells 	struct afs_vnode *vnode = wb->vnode;
7031143d5dSDavid Howells 
7131143d5dSDavid Howells 	_enter("{%d}", wb->usage);
7231143d5dSDavid Howells 
7331143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
7431143d5dSDavid Howells 	if (--wb->usage == 0)
7531143d5dSDavid Howells 		afs_unlink_writeback(wb);
7631143d5dSDavid Howells 	else
7731143d5dSDavid Howells 		wb = NULL;
7831143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
7931143d5dSDavid Howells 	if (wb)
8031143d5dSDavid Howells 		afs_free_writeback(wb);
8131143d5dSDavid Howells }
8231143d5dSDavid Howells 
8331143d5dSDavid Howells /*
8431143d5dSDavid Howells  * partly or wholly fill a page that's under preparation for writing
8531143d5dSDavid Howells  */
8631143d5dSDavid Howells static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
875e7f2337SAnton Blanchard 			 loff_t pos, struct page *page)
8831143d5dSDavid Howells {
89196ee9cdSDavid Howells 	struct afs_read *req;
9015b4650eSNick Piggin 	loff_t i_size;
9131143d5dSDavid Howells 	int ret;
9231143d5dSDavid Howells 
935e7f2337SAnton Blanchard 	_enter(",,%llu", (unsigned long long)pos);
9431143d5dSDavid Howells 
95196ee9cdSDavid Howells 	req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
96196ee9cdSDavid Howells 		      GFP_KERNEL);
97196ee9cdSDavid Howells 	if (!req)
98196ee9cdSDavid Howells 		return -ENOMEM;
99196ee9cdSDavid Howells 
100196ee9cdSDavid Howells 	atomic_set(&req->usage, 1);
101196ee9cdSDavid Howells 	req->pos = pos;
102196ee9cdSDavid Howells 	req->nr_pages = 1;
103196ee9cdSDavid Howells 	req->pages[0] = page;
104196ee9cdSDavid Howells 
10515b4650eSNick Piggin 	i_size = i_size_read(&vnode->vfs_inode);
10609cbfeafSKirill A. Shutemov 	if (pos + PAGE_SIZE > i_size)
107196ee9cdSDavid Howells 		req->len = i_size - pos;
10815b4650eSNick Piggin 	else
109196ee9cdSDavid Howells 		req->len = PAGE_SIZE;
11015b4650eSNick Piggin 
111196ee9cdSDavid Howells 	ret = afs_vnode_fetch_data(vnode, key, req);
112196ee9cdSDavid Howells 	afs_put_read(req);
11331143d5dSDavid Howells 	if (ret < 0) {
11431143d5dSDavid Howells 		if (ret == -ENOENT) {
11531143d5dSDavid Howells 			_debug("got NOENT from server"
11631143d5dSDavid Howells 			       " - marking file deleted and stale");
11731143d5dSDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
11831143d5dSDavid Howells 			ret = -ESTALE;
11931143d5dSDavid Howells 		}
12031143d5dSDavid Howells 	}
12131143d5dSDavid Howells 
12231143d5dSDavid Howells 	_leave(" = %d", ret);
12331143d5dSDavid Howells 	return ret;
12431143d5dSDavid Howells }
12531143d5dSDavid Howells 
12631143d5dSDavid Howells /*
12731143d5dSDavid Howells  * prepare to perform part of a write to a page
12831143d5dSDavid Howells  */
12915b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping,
13015b4650eSNick Piggin 		    loff_t pos, unsigned len, unsigned flags,
13115b4650eSNick Piggin 		    struct page **pagep, void **fsdata)
13231143d5dSDavid Howells {
13331143d5dSDavid Howells 	struct afs_writeback *candidate, *wb;
134496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
13515b4650eSNick Piggin 	struct page *page;
13631143d5dSDavid Howells 	struct key *key = file->private_data;
13709cbfeafSKirill A. Shutemov 	unsigned from = pos & (PAGE_SIZE - 1);
13815b4650eSNick Piggin 	unsigned to = from + len;
13909cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
14031143d5dSDavid Howells 	int ret;
14131143d5dSDavid Howells 
14231143d5dSDavid Howells 	_enter("{%x:%u},{%lx},%u,%u",
14315b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, index, from, to);
14431143d5dSDavid Howells 
14531143d5dSDavid Howells 	candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
14631143d5dSDavid Howells 	if (!candidate)
14731143d5dSDavid Howells 		return -ENOMEM;
14831143d5dSDavid Howells 	candidate->vnode = vnode;
14915b4650eSNick Piggin 	candidate->first = candidate->last = index;
15015b4650eSNick Piggin 	candidate->offset_first = from;
15131143d5dSDavid Howells 	candidate->to_last = to;
152f129ccc9SAnton Blanchard 	INIT_LIST_HEAD(&candidate->link);
15331143d5dSDavid Howells 	candidate->usage = 1;
15431143d5dSDavid Howells 	candidate->state = AFS_WBACK_PENDING;
15531143d5dSDavid Howells 	init_waitqueue_head(&candidate->waitq);
15631143d5dSDavid Howells 
15754566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
15815b4650eSNick Piggin 	if (!page) {
15915b4650eSNick Piggin 		kfree(candidate);
16015b4650eSNick Piggin 		return -ENOMEM;
16115b4650eSNick Piggin 	}
16215b4650eSNick Piggin 	*pagep = page;
16315b4650eSNick Piggin 	/* page won't leak in error case: it eventually gets cleaned off LRU */
16415b4650eSNick Piggin 
16509cbfeafSKirill A. Shutemov 	if (!PageUptodate(page) && len != PAGE_SIZE) {
16609cbfeafSKirill A. Shutemov 		ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page);
16731143d5dSDavid Howells 		if (ret < 0) {
16831143d5dSDavid Howells 			kfree(candidate);
16931143d5dSDavid Howells 			_leave(" = %d [prep]", ret);
17031143d5dSDavid Howells 			return ret;
17131143d5dSDavid Howells 		}
17215b4650eSNick Piggin 		SetPageUptodate(page);
17331143d5dSDavid Howells 	}
17431143d5dSDavid Howells 
17531143d5dSDavid Howells try_again:
17631143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
17731143d5dSDavid Howells 
17831143d5dSDavid Howells 	/* see if this page is already pending a writeback under a suitable key
17931143d5dSDavid Howells 	 * - if so we can just join onto that one */
18031143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
18131143d5dSDavid Howells 	if (wb) {
18231143d5dSDavid Howells 		if (wb->key == key && wb->state == AFS_WBACK_PENDING)
18331143d5dSDavid Howells 			goto subsume_in_current_wb;
18431143d5dSDavid Howells 		goto flush_conflicting_wb;
18531143d5dSDavid Howells 	}
18631143d5dSDavid Howells 
18731143d5dSDavid Howells 	if (index > 0) {
18831143d5dSDavid Howells 		/* see if we can find an already pending writeback that we can
18931143d5dSDavid Howells 		 * append this page to */
19031143d5dSDavid Howells 		list_for_each_entry(wb, &vnode->writebacks, link) {
19131143d5dSDavid Howells 			if (wb->last == index - 1 && wb->key == key &&
19231143d5dSDavid Howells 			    wb->state == AFS_WBACK_PENDING)
19331143d5dSDavid Howells 				goto append_to_previous_wb;
19431143d5dSDavid Howells 		}
19531143d5dSDavid Howells 	}
19631143d5dSDavid Howells 
19731143d5dSDavid Howells 	list_add_tail(&candidate->link, &vnode->writebacks);
19831143d5dSDavid Howells 	candidate->key = key_get(key);
19931143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
20031143d5dSDavid Howells 	SetPagePrivate(page);
20131143d5dSDavid Howells 	set_page_private(page, (unsigned long) candidate);
20231143d5dSDavid Howells 	_leave(" = 0 [new]");
20331143d5dSDavid Howells 	return 0;
20431143d5dSDavid Howells 
20531143d5dSDavid Howells subsume_in_current_wb:
20631143d5dSDavid Howells 	_debug("subsume");
20731143d5dSDavid Howells 	ASSERTRANGE(wb->first, <=, index, <=, wb->last);
20815b4650eSNick Piggin 	if (index == wb->first && from < wb->offset_first)
20915b4650eSNick Piggin 		wb->offset_first = from;
21031143d5dSDavid Howells 	if (index == wb->last && to > wb->to_last)
21131143d5dSDavid Howells 		wb->to_last = to;
21231143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
21331143d5dSDavid Howells 	kfree(candidate);
21431143d5dSDavid Howells 	_leave(" = 0 [sub]");
21531143d5dSDavid Howells 	return 0;
21631143d5dSDavid Howells 
21731143d5dSDavid Howells append_to_previous_wb:
21831143d5dSDavid Howells 	_debug("append into %lx-%lx", wb->first, wb->last);
21931143d5dSDavid Howells 	wb->usage++;
22031143d5dSDavid Howells 	wb->last++;
22131143d5dSDavid Howells 	wb->to_last = to;
22231143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
22331143d5dSDavid Howells 	SetPagePrivate(page);
22431143d5dSDavid Howells 	set_page_private(page, (unsigned long) wb);
22531143d5dSDavid Howells 	kfree(candidate);
22631143d5dSDavid Howells 	_leave(" = 0 [app]");
22731143d5dSDavid Howells 	return 0;
22831143d5dSDavid Howells 
22931143d5dSDavid Howells 	/* the page is currently bound to another context, so if it's dirty we
23031143d5dSDavid Howells 	 * need to flush it before we can use the new context */
23131143d5dSDavid Howells flush_conflicting_wb:
23231143d5dSDavid Howells 	_debug("flush conflict");
23331143d5dSDavid Howells 	if (wb->state == AFS_WBACK_PENDING)
23431143d5dSDavid Howells 		wb->state = AFS_WBACK_CONFLICTING;
23531143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
23631143d5dSDavid Howells 	if (PageDirty(page)) {
23731143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
23831143d5dSDavid Howells 		if (ret < 0) {
23931143d5dSDavid Howells 			afs_put_writeback(candidate);
24031143d5dSDavid Howells 			_leave(" = %d", ret);
24131143d5dSDavid Howells 			return ret;
24231143d5dSDavid Howells 		}
24331143d5dSDavid Howells 	}
24431143d5dSDavid Howells 
24531143d5dSDavid Howells 	/* the page holds a ref on the writeback record */
24631143d5dSDavid Howells 	afs_put_writeback(wb);
24731143d5dSDavid Howells 	set_page_private(page, 0);
24831143d5dSDavid Howells 	ClearPagePrivate(page);
24931143d5dSDavid Howells 	goto try_again;
25031143d5dSDavid Howells }
25131143d5dSDavid Howells 
25231143d5dSDavid Howells /*
25331143d5dSDavid Howells  * finalise part of a write to a page
25431143d5dSDavid Howells  */
25515b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping,
25615b4650eSNick Piggin 		  loff_t pos, unsigned len, unsigned copied,
25715b4650eSNick Piggin 		  struct page *page, void *fsdata)
25831143d5dSDavid Howells {
259496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
26031143d5dSDavid Howells 	loff_t i_size, maybe_i_size;
26131143d5dSDavid Howells 
26215b4650eSNick Piggin 	_enter("{%x:%u},{%lx}",
26315b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, page->index);
26431143d5dSDavid Howells 
26515b4650eSNick Piggin 	maybe_i_size = pos + copied;
26631143d5dSDavid Howells 
26731143d5dSDavid Howells 	i_size = i_size_read(&vnode->vfs_inode);
26831143d5dSDavid Howells 	if (maybe_i_size > i_size) {
26931143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
27031143d5dSDavid Howells 		i_size = i_size_read(&vnode->vfs_inode);
27131143d5dSDavid Howells 		if (maybe_i_size > i_size)
27231143d5dSDavid Howells 			i_size_write(&vnode->vfs_inode, maybe_i_size);
27331143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
27431143d5dSDavid Howells 	}
27531143d5dSDavid Howells 
27631143d5dSDavid Howells 	set_page_dirty(page);
27731143d5dSDavid Howells 	if (PageDirty(page))
27831143d5dSDavid Howells 		_debug("dirtied");
27915b4650eSNick Piggin 	unlock_page(page);
28009cbfeafSKirill A. Shutemov 	put_page(page);
28131143d5dSDavid Howells 
28215b4650eSNick Piggin 	return copied;
28331143d5dSDavid Howells }
28431143d5dSDavid Howells 
28531143d5dSDavid Howells /*
28631143d5dSDavid Howells  * kill all the pages in the given range
28731143d5dSDavid Howells  */
28831143d5dSDavid Howells static void afs_kill_pages(struct afs_vnode *vnode, bool error,
28931143d5dSDavid Howells 			   pgoff_t first, pgoff_t last)
29031143d5dSDavid Howells {
29131143d5dSDavid Howells 	struct pagevec pv;
29231143d5dSDavid Howells 	unsigned count, loop;
29331143d5dSDavid Howells 
29431143d5dSDavid Howells 	_enter("{%x:%u},%lx-%lx",
29531143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
29631143d5dSDavid Howells 
29731143d5dSDavid Howells 	pagevec_init(&pv, 0);
29831143d5dSDavid Howells 
29931143d5dSDavid Howells 	do {
30031143d5dSDavid Howells 		_debug("kill %lx-%lx", first, last);
30131143d5dSDavid Howells 
30231143d5dSDavid Howells 		count = last - first + 1;
30331143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
30431143d5dSDavid Howells 			count = PAGEVEC_SIZE;
30531143d5dSDavid Howells 		pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
30631143d5dSDavid Howells 					      first, count, pv.pages);
30731143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
30831143d5dSDavid Howells 
30931143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
31031143d5dSDavid Howells 			ClearPageUptodate(pv.pages[loop]);
31131143d5dSDavid Howells 			if (error)
31231143d5dSDavid Howells 				SetPageError(pv.pages[loop]);
31331143d5dSDavid Howells 			end_page_writeback(pv.pages[loop]);
31431143d5dSDavid Howells 		}
31531143d5dSDavid Howells 
31631143d5dSDavid Howells 		__pagevec_release(&pv);
31731143d5dSDavid Howells 	} while (first < last);
31831143d5dSDavid Howells 
31931143d5dSDavid Howells 	_leave("");
32031143d5dSDavid Howells }
32131143d5dSDavid Howells 
32231143d5dSDavid Howells /*
32331143d5dSDavid Howells  * synchronously write back the locked page and any subsequent non-locked dirty
32431143d5dSDavid Howells  * pages also covered by the same writeback record
32531143d5dSDavid Howells  */
32631143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb,
32731143d5dSDavid Howells 					   struct page *primary_page)
32831143d5dSDavid Howells {
32931143d5dSDavid Howells 	struct page *pages[8], *page;
33031143d5dSDavid Howells 	unsigned long count;
33131143d5dSDavid Howells 	unsigned n, offset, to;
33231143d5dSDavid Howells 	pgoff_t start, first, last;
33331143d5dSDavid Howells 	int loop, ret;
33431143d5dSDavid Howells 
33531143d5dSDavid Howells 	_enter(",%lx", primary_page->index);
33631143d5dSDavid Howells 
33731143d5dSDavid Howells 	count = 1;
33831143d5dSDavid Howells 	if (!clear_page_dirty_for_io(primary_page))
33931143d5dSDavid Howells 		BUG();
34031143d5dSDavid Howells 	if (test_set_page_writeback(primary_page))
34131143d5dSDavid Howells 		BUG();
34231143d5dSDavid Howells 
34331143d5dSDavid Howells 	/* find all consecutive lockable dirty pages, stopping when we find a
34431143d5dSDavid Howells 	 * page that is not immediately lockable, is not dirty or is missing,
34531143d5dSDavid Howells 	 * or we reach the end of the range */
34631143d5dSDavid Howells 	start = primary_page->index;
34731143d5dSDavid Howells 	if (start >= wb->last)
34831143d5dSDavid Howells 		goto no_more;
34931143d5dSDavid Howells 	start++;
35031143d5dSDavid Howells 	do {
35131143d5dSDavid Howells 		_debug("more %lx [%lx]", start, count);
35231143d5dSDavid Howells 		n = wb->last - start + 1;
35331143d5dSDavid Howells 		if (n > ARRAY_SIZE(pages))
35431143d5dSDavid Howells 			n = ARRAY_SIZE(pages);
35531143d5dSDavid Howells 		n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
35631143d5dSDavid Howells 					  start, n, pages);
35731143d5dSDavid Howells 		_debug("fgpc %u", n);
35831143d5dSDavid Howells 		if (n == 0)
35931143d5dSDavid Howells 			goto no_more;
36031143d5dSDavid Howells 		if (pages[0]->index != start) {
3619d577b6aSDavid Howells 			do {
3629d577b6aSDavid Howells 				put_page(pages[--n]);
3639d577b6aSDavid Howells 			} while (n > 0);
36431143d5dSDavid Howells 			goto no_more;
36531143d5dSDavid Howells 		}
36631143d5dSDavid Howells 
36731143d5dSDavid Howells 		for (loop = 0; loop < n; loop++) {
36831143d5dSDavid Howells 			page = pages[loop];
36931143d5dSDavid Howells 			if (page->index > wb->last)
37031143d5dSDavid Howells 				break;
371529ae9aaSNick Piggin 			if (!trylock_page(page))
37231143d5dSDavid Howells 				break;
37331143d5dSDavid Howells 			if (!PageDirty(page) ||
37431143d5dSDavid Howells 			    page_private(page) != (unsigned long) wb) {
37531143d5dSDavid Howells 				unlock_page(page);
37631143d5dSDavid Howells 				break;
37731143d5dSDavid Howells 			}
37831143d5dSDavid Howells 			if (!clear_page_dirty_for_io(page))
37931143d5dSDavid Howells 				BUG();
38031143d5dSDavid Howells 			if (test_set_page_writeback(page))
38131143d5dSDavid Howells 				BUG();
38231143d5dSDavid Howells 			unlock_page(page);
38331143d5dSDavid Howells 			put_page(page);
38431143d5dSDavid Howells 		}
38531143d5dSDavid Howells 		count += loop;
38631143d5dSDavid Howells 		if (loop < n) {
38731143d5dSDavid Howells 			for (; loop < n; loop++)
38831143d5dSDavid Howells 				put_page(pages[loop]);
38931143d5dSDavid Howells 			goto no_more;
39031143d5dSDavid Howells 		}
39131143d5dSDavid Howells 
39231143d5dSDavid Howells 		start += loop;
39331143d5dSDavid Howells 	} while (start <= wb->last && count < 65536);
39431143d5dSDavid Howells 
39531143d5dSDavid Howells no_more:
39631143d5dSDavid Howells 	/* we now have a contiguous set of dirty pages, each with writeback set
39731143d5dSDavid Howells 	 * and the dirty mark cleared; the first page is locked and must remain
39831143d5dSDavid Howells 	 * so, all the rest are unlocked */
39931143d5dSDavid Howells 	first = primary_page->index;
40031143d5dSDavid Howells 	last = first + count - 1;
40131143d5dSDavid Howells 
40231143d5dSDavid Howells 	offset = (first == wb->first) ? wb->offset_first : 0;
40331143d5dSDavid Howells 	to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
40431143d5dSDavid Howells 
40531143d5dSDavid Howells 	_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
40631143d5dSDavid Howells 
40731143d5dSDavid Howells 	ret = afs_vnode_store_data(wb, first, last, offset, to);
40831143d5dSDavid Howells 	if (ret < 0) {
40931143d5dSDavid Howells 		switch (ret) {
41031143d5dSDavid Howells 		case -EDQUOT:
41131143d5dSDavid Howells 		case -ENOSPC:
4125114a97aSMichal Hocko 			mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC);
41331143d5dSDavid Howells 			break;
41431143d5dSDavid Howells 		case -EROFS:
41531143d5dSDavid Howells 		case -EIO:
41631143d5dSDavid Howells 		case -EREMOTEIO:
41731143d5dSDavid Howells 		case -EFBIG:
41831143d5dSDavid Howells 		case -ENOENT:
41931143d5dSDavid Howells 		case -ENOMEDIUM:
42031143d5dSDavid Howells 		case -ENXIO:
42131143d5dSDavid Howells 			afs_kill_pages(wb->vnode, true, first, last);
4225114a97aSMichal Hocko 			mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO);
42331143d5dSDavid Howells 			break;
42431143d5dSDavid Howells 		case -EACCES:
42531143d5dSDavid Howells 		case -EPERM:
42631143d5dSDavid Howells 		case -ENOKEY:
42731143d5dSDavid Howells 		case -EKEYEXPIRED:
42831143d5dSDavid Howells 		case -EKEYREJECTED:
42931143d5dSDavid Howells 		case -EKEYREVOKED:
43031143d5dSDavid Howells 			afs_kill_pages(wb->vnode, false, first, last);
43131143d5dSDavid Howells 			break;
43231143d5dSDavid Howells 		default:
43331143d5dSDavid Howells 			break;
43431143d5dSDavid Howells 		}
43531143d5dSDavid Howells 	} else {
43631143d5dSDavid Howells 		ret = count;
43731143d5dSDavid Howells 	}
43831143d5dSDavid Howells 
43931143d5dSDavid Howells 	_leave(" = %d", ret);
44031143d5dSDavid Howells 	return ret;
44131143d5dSDavid Howells }
44231143d5dSDavid Howells 
44331143d5dSDavid Howells /*
44431143d5dSDavid Howells  * write a page back to the server
44531143d5dSDavid Howells  * - the caller locked the page for us
44631143d5dSDavid Howells  */
44731143d5dSDavid Howells int afs_writepage(struct page *page, struct writeback_control *wbc)
44831143d5dSDavid Howells {
44931143d5dSDavid Howells 	struct afs_writeback *wb;
45031143d5dSDavid Howells 	int ret;
45131143d5dSDavid Howells 
45231143d5dSDavid Howells 	_enter("{%lx},", page->index);
45331143d5dSDavid Howells 
45431143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
45531143d5dSDavid Howells 	ASSERT(wb != NULL);
45631143d5dSDavid Howells 
45731143d5dSDavid Howells 	ret = afs_write_back_from_locked_page(wb, page);
45831143d5dSDavid Howells 	unlock_page(page);
45931143d5dSDavid Howells 	if (ret < 0) {
46031143d5dSDavid Howells 		_leave(" = %d", ret);
46131143d5dSDavid Howells 		return 0;
46231143d5dSDavid Howells 	}
46331143d5dSDavid Howells 
46431143d5dSDavid Howells 	wbc->nr_to_write -= ret;
46531143d5dSDavid Howells 
46631143d5dSDavid Howells 	_leave(" = 0");
46731143d5dSDavid Howells 	return 0;
46831143d5dSDavid Howells }
46931143d5dSDavid Howells 
47031143d5dSDavid Howells /*
47131143d5dSDavid Howells  * write a region of pages back to the server
47231143d5dSDavid Howells  */
473c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
47431143d5dSDavid Howells 				 struct writeback_control *wbc,
47531143d5dSDavid Howells 				 pgoff_t index, pgoff_t end, pgoff_t *_next)
47631143d5dSDavid Howells {
47731143d5dSDavid Howells 	struct afs_writeback *wb;
47831143d5dSDavid Howells 	struct page *page;
47931143d5dSDavid Howells 	int ret, n;
48031143d5dSDavid Howells 
48131143d5dSDavid Howells 	_enter(",,%lx,%lx,", index, end);
48231143d5dSDavid Howells 
48331143d5dSDavid Howells 	do {
48431143d5dSDavid Howells 		n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
48531143d5dSDavid Howells 				       1, &page);
48631143d5dSDavid Howells 		if (!n)
48731143d5dSDavid Howells 			break;
48831143d5dSDavid Howells 
48931143d5dSDavid Howells 		_debug("wback %lx", page->index);
49031143d5dSDavid Howells 
49131143d5dSDavid Howells 		if (page->index > end) {
49231143d5dSDavid Howells 			*_next = index;
49309cbfeafSKirill A. Shutemov 			put_page(page);
49431143d5dSDavid Howells 			_leave(" = 0 [%lx]", *_next);
49531143d5dSDavid Howells 			return 0;
49631143d5dSDavid Howells 		}
49731143d5dSDavid Howells 
49831143d5dSDavid Howells 		/* at this point we hold neither mapping->tree_lock nor lock on
49931143d5dSDavid Howells 		 * the page itself: the page may be truncated or invalidated
50031143d5dSDavid Howells 		 * (changing page->mapping to NULL), or even swizzled back from
50131143d5dSDavid Howells 		 * swapper_space to tmpfs file mapping
50231143d5dSDavid Howells 		 */
50331143d5dSDavid Howells 		lock_page(page);
50431143d5dSDavid Howells 
50531143d5dSDavid Howells 		if (page->mapping != mapping) {
50631143d5dSDavid Howells 			unlock_page(page);
50709cbfeafSKirill A. Shutemov 			put_page(page);
50831143d5dSDavid Howells 			continue;
50931143d5dSDavid Howells 		}
51031143d5dSDavid Howells 
51131143d5dSDavid Howells 		if (wbc->sync_mode != WB_SYNC_NONE)
51231143d5dSDavid Howells 			wait_on_page_writeback(page);
51331143d5dSDavid Howells 
51431143d5dSDavid Howells 		if (PageWriteback(page) || !PageDirty(page)) {
51531143d5dSDavid Howells 			unlock_page(page);
51629c8bbbdSDavid Howells 			put_page(page);
51731143d5dSDavid Howells 			continue;
51831143d5dSDavid Howells 		}
51931143d5dSDavid Howells 
52031143d5dSDavid Howells 		wb = (struct afs_writeback *) page_private(page);
52131143d5dSDavid Howells 		ASSERT(wb != NULL);
52231143d5dSDavid Howells 
52331143d5dSDavid Howells 		spin_lock(&wb->vnode->writeback_lock);
52431143d5dSDavid Howells 		wb->state = AFS_WBACK_WRITING;
52531143d5dSDavid Howells 		spin_unlock(&wb->vnode->writeback_lock);
52631143d5dSDavid Howells 
52731143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
52831143d5dSDavid Howells 		unlock_page(page);
52909cbfeafSKirill A. Shutemov 		put_page(page);
53031143d5dSDavid Howells 		if (ret < 0) {
53131143d5dSDavid Howells 			_leave(" = %d", ret);
53231143d5dSDavid Howells 			return ret;
53331143d5dSDavid Howells 		}
53431143d5dSDavid Howells 
53531143d5dSDavid Howells 		wbc->nr_to_write -= ret;
53631143d5dSDavid Howells 
53731143d5dSDavid Howells 		cond_resched();
53831143d5dSDavid Howells 	} while (index < end && wbc->nr_to_write > 0);
53931143d5dSDavid Howells 
54031143d5dSDavid Howells 	*_next = index;
54131143d5dSDavid Howells 	_leave(" = 0 [%lx]", *_next);
54231143d5dSDavid Howells 	return 0;
54331143d5dSDavid Howells }
54431143d5dSDavid Howells 
54531143d5dSDavid Howells /*
54631143d5dSDavid Howells  * write some of the pending data back to the server
54731143d5dSDavid Howells  */
54831143d5dSDavid Howells int afs_writepages(struct address_space *mapping,
54931143d5dSDavid Howells 		   struct writeback_control *wbc)
55031143d5dSDavid Howells {
55131143d5dSDavid Howells 	pgoff_t start, end, next;
55231143d5dSDavid Howells 	int ret;
55331143d5dSDavid Howells 
55431143d5dSDavid Howells 	_enter("");
55531143d5dSDavid Howells 
55631143d5dSDavid Howells 	if (wbc->range_cyclic) {
55731143d5dSDavid Howells 		start = mapping->writeback_index;
55831143d5dSDavid Howells 		end = -1;
55931143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
5601b430beeSWu Fengguang 		if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
56131143d5dSDavid Howells 			ret = afs_writepages_region(mapping, wbc, 0, start,
56231143d5dSDavid Howells 						    &next);
56331143d5dSDavid Howells 		mapping->writeback_index = next;
56431143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
56509cbfeafSKirill A. Shutemov 		end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
56631143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, end, &next);
56731143d5dSDavid Howells 		if (wbc->nr_to_write > 0)
56831143d5dSDavid Howells 			mapping->writeback_index = next;
56931143d5dSDavid Howells 	} else {
57009cbfeafSKirill A. Shutemov 		start = wbc->range_start >> PAGE_SHIFT;
57109cbfeafSKirill A. Shutemov 		end = wbc->range_end >> PAGE_SHIFT;
57231143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
57331143d5dSDavid Howells 	}
57431143d5dSDavid Howells 
57531143d5dSDavid Howells 	_leave(" = %d", ret);
57631143d5dSDavid Howells 	return ret;
57731143d5dSDavid Howells }
57831143d5dSDavid Howells 
57931143d5dSDavid Howells /*
58031143d5dSDavid Howells  * completion of write to server
58131143d5dSDavid Howells  */
58231143d5dSDavid Howells void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
58331143d5dSDavid Howells {
58431143d5dSDavid Howells 	struct afs_writeback *wb = call->wb;
58531143d5dSDavid Howells 	struct pagevec pv;
58631143d5dSDavid Howells 	unsigned count, loop;
58731143d5dSDavid Howells 	pgoff_t first = call->first, last = call->last;
58831143d5dSDavid Howells 	bool free_wb;
58931143d5dSDavid Howells 
59031143d5dSDavid Howells 	_enter("{%x:%u},{%lx-%lx}",
59131143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
59231143d5dSDavid Howells 
59331143d5dSDavid Howells 	ASSERT(wb != NULL);
59431143d5dSDavid Howells 
59531143d5dSDavid Howells 	pagevec_init(&pv, 0);
59631143d5dSDavid Howells 
59731143d5dSDavid Howells 	do {
5985bbf5d39SDavid Howells 		_debug("done %lx-%lx", first, last);
59931143d5dSDavid Howells 
60031143d5dSDavid Howells 		count = last - first + 1;
60131143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
60231143d5dSDavid Howells 			count = PAGEVEC_SIZE;
60331143d5dSDavid Howells 		pv.nr = find_get_pages_contig(call->mapping, first, count,
60431143d5dSDavid Howells 					      pv.pages);
60531143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
60631143d5dSDavid Howells 
60731143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
60831143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
60931143d5dSDavid Howells 			struct page *page = pv.pages[loop];
61031143d5dSDavid Howells 			end_page_writeback(page);
61131143d5dSDavid Howells 			if (page_private(page) == (unsigned long) wb) {
61231143d5dSDavid Howells 				set_page_private(page, 0);
61331143d5dSDavid Howells 				ClearPagePrivate(page);
61431143d5dSDavid Howells 				wb->usage--;
61531143d5dSDavid Howells 			}
61631143d5dSDavid Howells 		}
61731143d5dSDavid Howells 		free_wb = false;
61831143d5dSDavid Howells 		if (wb->usage == 0) {
61931143d5dSDavid Howells 			afs_unlink_writeback(wb);
62031143d5dSDavid Howells 			free_wb = true;
62131143d5dSDavid Howells 		}
62231143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
62331143d5dSDavid Howells 		first += count;
62431143d5dSDavid Howells 		if (free_wb) {
62531143d5dSDavid Howells 			afs_free_writeback(wb);
62631143d5dSDavid Howells 			wb = NULL;
62731143d5dSDavid Howells 		}
62831143d5dSDavid Howells 
62931143d5dSDavid Howells 		__pagevec_release(&pv);
6305bbf5d39SDavid Howells 	} while (first <= last);
63131143d5dSDavid Howells 
63231143d5dSDavid Howells 	_leave("");
63331143d5dSDavid Howells }
63431143d5dSDavid Howells 
63531143d5dSDavid Howells /*
63631143d5dSDavid Howells  * write to an AFS file
63731143d5dSDavid Howells  */
63850b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
63931143d5dSDavid Howells {
640496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
64131143d5dSDavid Howells 	ssize_t result;
64250b5551dSAl Viro 	size_t count = iov_iter_count(from);
64331143d5dSDavid Howells 
64450b5551dSAl Viro 	_enter("{%x.%u},{%zu},",
64550b5551dSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, count);
64631143d5dSDavid Howells 
64731143d5dSDavid Howells 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
64831143d5dSDavid Howells 		printk(KERN_INFO
64931143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
65031143d5dSDavid Howells 		return -EBUSY;
65131143d5dSDavid Howells 	}
65231143d5dSDavid Howells 
65331143d5dSDavid Howells 	if (!count)
65431143d5dSDavid Howells 		return 0;
65531143d5dSDavid Howells 
65650b5551dSAl Viro 	result = generic_file_write_iter(iocb, from);
65731143d5dSDavid Howells 
65831143d5dSDavid Howells 	_leave(" = %zd", result);
65931143d5dSDavid Howells 	return result;
66031143d5dSDavid Howells }
66131143d5dSDavid Howells 
66231143d5dSDavid Howells /*
66331143d5dSDavid Howells  * flush the vnode to the fileserver
66431143d5dSDavid Howells  */
66531143d5dSDavid Howells int afs_writeback_all(struct afs_vnode *vnode)
66631143d5dSDavid Howells {
66731143d5dSDavid Howells 	struct address_space *mapping = vnode->vfs_inode.i_mapping;
66831143d5dSDavid Howells 	struct writeback_control wbc = {
66931143d5dSDavid Howells 		.sync_mode	= WB_SYNC_ALL,
67031143d5dSDavid Howells 		.nr_to_write	= LONG_MAX,
67131143d5dSDavid Howells 		.range_cyclic	= 1,
67231143d5dSDavid Howells 	};
67331143d5dSDavid Howells 	int ret;
67431143d5dSDavid Howells 
67531143d5dSDavid Howells 	_enter("");
67631143d5dSDavid Howells 
67731143d5dSDavid Howells 	ret = mapping->a_ops->writepages(mapping, &wbc);
67831143d5dSDavid Howells 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
67931143d5dSDavid Howells 
68031143d5dSDavid Howells 	_leave(" = %d", ret);
68131143d5dSDavid Howells 	return ret;
68231143d5dSDavid Howells }
68331143d5dSDavid Howells 
68431143d5dSDavid Howells /*
68531143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
68631143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
68731143d5dSDavid Howells  *   whether any write errors occurred for this process.
68831143d5dSDavid Howells  */
68902c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
69031143d5dSDavid Howells {
6913c981bfcSAl Viro 	struct inode *inode = file_inode(file);
69231143d5dSDavid Howells 	struct afs_writeback *wb, *xwb;
6933c981bfcSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(inode);
69431143d5dSDavid Howells 	int ret;
69531143d5dSDavid Howells 
6963c981bfcSAl Viro 	_enter("{%x:%u},{n=%pD},%d",
6973c981bfcSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, file,
69831143d5dSDavid Howells 	       datasync);
69931143d5dSDavid Howells 
70002c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
70102c24a82SJosef Bacik 	if (ret)
70202c24a82SJosef Bacik 		return ret;
7035955102cSAl Viro 	inode_lock(inode);
70402c24a82SJosef Bacik 
70531143d5dSDavid Howells 	/* use a writeback record as a marker in the queue - when this reaches
70631143d5dSDavid Howells 	 * the front of the queue, all the outstanding writes are either
70731143d5dSDavid Howells 	 * completed or rejected */
70831143d5dSDavid Howells 	wb = kzalloc(sizeof(*wb), GFP_KERNEL);
70902c24a82SJosef Bacik 	if (!wb) {
71002c24a82SJosef Bacik 		ret = -ENOMEM;
71102c24a82SJosef Bacik 		goto out;
71202c24a82SJosef Bacik 	}
71331143d5dSDavid Howells 	wb->vnode = vnode;
71431143d5dSDavid Howells 	wb->first = 0;
71531143d5dSDavid Howells 	wb->last = -1;
71631143d5dSDavid Howells 	wb->offset_first = 0;
71731143d5dSDavid Howells 	wb->to_last = PAGE_SIZE;
71831143d5dSDavid Howells 	wb->usage = 1;
71931143d5dSDavid Howells 	wb->state = AFS_WBACK_SYNCING;
72031143d5dSDavid Howells 	init_waitqueue_head(&wb->waitq);
72131143d5dSDavid Howells 
72231143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
72331143d5dSDavid Howells 	list_for_each_entry(xwb, &vnode->writebacks, link) {
72431143d5dSDavid Howells 		if (xwb->state == AFS_WBACK_PENDING)
72531143d5dSDavid Howells 			xwb->state = AFS_WBACK_CONFLICTING;
72631143d5dSDavid Howells 	}
72731143d5dSDavid Howells 	list_add_tail(&wb->link, &vnode->writebacks);
72831143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
72931143d5dSDavid Howells 
73031143d5dSDavid Howells 	/* push all the outstanding writebacks to the server */
73131143d5dSDavid Howells 	ret = afs_writeback_all(vnode);
73231143d5dSDavid Howells 	if (ret < 0) {
73331143d5dSDavid Howells 		afs_put_writeback(wb);
73431143d5dSDavid Howells 		_leave(" = %d [wb]", ret);
73502c24a82SJosef Bacik 		goto out;
73631143d5dSDavid Howells 	}
73731143d5dSDavid Howells 
73831143d5dSDavid Howells 	/* wait for the preceding writes to actually complete */
73931143d5dSDavid Howells 	ret = wait_event_interruptible(wb->waitq,
74031143d5dSDavid Howells 				       wb->state == AFS_WBACK_COMPLETE ||
74131143d5dSDavid Howells 				       vnode->writebacks.next == &wb->link);
74231143d5dSDavid Howells 	afs_put_writeback(wb);
74331143d5dSDavid Howells 	_leave(" = %d", ret);
74402c24a82SJosef Bacik out:
7455955102cSAl Viro 	inode_unlock(inode);
74631143d5dSDavid Howells 	return ret;
74731143d5dSDavid Howells }
7489b3f26c9SDavid Howells 
7499b3f26c9SDavid Howells /*
7509b3f26c9SDavid Howells  * notification that a previously read-only page is about to become writable
7519b3f26c9SDavid Howells  * - if it returns an error, the caller will deliver a bus error signal
7529b3f26c9SDavid Howells  */
7539b3f26c9SDavid Howells int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
7549b3f26c9SDavid Howells {
7559b3f26c9SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
7569b3f26c9SDavid Howells 
7579b3f26c9SDavid Howells 	_enter("{{%x:%u}},{%lx}",
7589b3f26c9SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, page->index);
7599b3f26c9SDavid Howells 
7609b3f26c9SDavid Howells 	/* wait for the page to be written to the cache before we allow it to
7619b3f26c9SDavid Howells 	 * be modified */
7629b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE
7639b3f26c9SDavid Howells 	fscache_wait_on_page_write(vnode->cache, page);
7649b3f26c9SDavid Howells #endif
7659b3f26c9SDavid Howells 
7669b3f26c9SDavid Howells 	_leave(" = 0");
7679b3f26c9SDavid Howells 	return 0;
7689b3f26c9SDavid Howells }
769