xref: /openbmc/linux/fs/afs/write.c (revision 50b5551d)
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>
17a27bb332SKent Overstreet #include <linux/aio.h>
1831143d5dSDavid Howells #include "internal.h"
1931143d5dSDavid Howells 
2031143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb,
2131143d5dSDavid Howells 					   struct page *page);
2231143d5dSDavid Howells 
2331143d5dSDavid Howells /*
2431143d5dSDavid Howells  * mark a page as having been made dirty and thus needing writeback
2531143d5dSDavid Howells  */
2631143d5dSDavid Howells int afs_set_page_dirty(struct page *page)
2731143d5dSDavid Howells {
2831143d5dSDavid Howells 	_enter("");
2931143d5dSDavid Howells 	return __set_page_dirty_nobuffers(page);
3031143d5dSDavid Howells }
3131143d5dSDavid Howells 
3231143d5dSDavid Howells /*
3331143d5dSDavid Howells  * unlink a writeback record because its usage has reached zero
3431143d5dSDavid Howells  * - must be called with the wb->vnode->writeback_lock held
3531143d5dSDavid Howells  */
3631143d5dSDavid Howells static void afs_unlink_writeback(struct afs_writeback *wb)
3731143d5dSDavid Howells {
3831143d5dSDavid Howells 	struct afs_writeback *front;
3931143d5dSDavid Howells 	struct afs_vnode *vnode = wb->vnode;
4031143d5dSDavid Howells 
4131143d5dSDavid Howells 	list_del_init(&wb->link);
4231143d5dSDavid Howells 	if (!list_empty(&vnode->writebacks)) {
4331143d5dSDavid Howells 		/* if an fsync rises to the front of the queue then wake it
4431143d5dSDavid Howells 		 * up */
4531143d5dSDavid Howells 		front = list_entry(vnode->writebacks.next,
4631143d5dSDavid Howells 				   struct afs_writeback, link);
4731143d5dSDavid Howells 		if (front->state == AFS_WBACK_SYNCING) {
4831143d5dSDavid Howells 			_debug("wake up sync");
4931143d5dSDavid Howells 			front->state = AFS_WBACK_COMPLETE;
5031143d5dSDavid Howells 			wake_up(&front->waitq);
5131143d5dSDavid Howells 		}
5231143d5dSDavid Howells 	}
5331143d5dSDavid Howells }
5431143d5dSDavid Howells 
5531143d5dSDavid Howells /*
5631143d5dSDavid Howells  * free a writeback record
5731143d5dSDavid Howells  */
5831143d5dSDavid Howells static void afs_free_writeback(struct afs_writeback *wb)
5931143d5dSDavid Howells {
6031143d5dSDavid Howells 	_enter("");
6131143d5dSDavid Howells 	key_put(wb->key);
6231143d5dSDavid Howells 	kfree(wb);
6331143d5dSDavid Howells }
6431143d5dSDavid Howells 
6531143d5dSDavid Howells /*
6631143d5dSDavid Howells  * dispose of a reference to a writeback record
6731143d5dSDavid Howells  */
6831143d5dSDavid Howells void afs_put_writeback(struct afs_writeback *wb)
6931143d5dSDavid Howells {
7031143d5dSDavid Howells 	struct afs_vnode *vnode = wb->vnode;
7131143d5dSDavid Howells 
7231143d5dSDavid Howells 	_enter("{%d}", wb->usage);
7331143d5dSDavid Howells 
7431143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
7531143d5dSDavid Howells 	if (--wb->usage == 0)
7631143d5dSDavid Howells 		afs_unlink_writeback(wb);
7731143d5dSDavid Howells 	else
7831143d5dSDavid Howells 		wb = NULL;
7931143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
8031143d5dSDavid Howells 	if (wb)
8131143d5dSDavid Howells 		afs_free_writeback(wb);
8231143d5dSDavid Howells }
8331143d5dSDavid Howells 
8431143d5dSDavid Howells /*
8531143d5dSDavid Howells  * partly or wholly fill a page that's under preparation for writing
8631143d5dSDavid Howells  */
8731143d5dSDavid Howells static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
885e7f2337SAnton Blanchard 			 loff_t pos, struct page *page)
8931143d5dSDavid Howells {
9015b4650eSNick Piggin 	loff_t i_size;
9131143d5dSDavid Howells 	int ret;
925e7f2337SAnton Blanchard 	int len;
9331143d5dSDavid Howells 
945e7f2337SAnton Blanchard 	_enter(",,%llu", (unsigned long long)pos);
9531143d5dSDavid Howells 
9615b4650eSNick Piggin 	i_size = i_size_read(&vnode->vfs_inode);
975e7f2337SAnton Blanchard 	if (pos + PAGE_CACHE_SIZE > i_size)
985e7f2337SAnton Blanchard 		len = i_size - pos;
9915b4650eSNick Piggin 	else
1005e7f2337SAnton Blanchard 		len = PAGE_CACHE_SIZE;
10115b4650eSNick Piggin 
1025e7f2337SAnton Blanchard 	ret = afs_vnode_fetch_data(vnode, key, pos, len, page);
10331143d5dSDavid Howells 	if (ret < 0) {
10431143d5dSDavid Howells 		if (ret == -ENOENT) {
10531143d5dSDavid Howells 			_debug("got NOENT from server"
10631143d5dSDavid Howells 			       " - marking file deleted and stale");
10731143d5dSDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
10831143d5dSDavid Howells 			ret = -ESTALE;
10931143d5dSDavid Howells 		}
11031143d5dSDavid Howells 	}
11131143d5dSDavid Howells 
11231143d5dSDavid Howells 	_leave(" = %d", ret);
11331143d5dSDavid Howells 	return ret;
11431143d5dSDavid Howells }
11531143d5dSDavid Howells 
11631143d5dSDavid Howells /*
11731143d5dSDavid Howells  * prepare to perform part of a write to a page
11831143d5dSDavid Howells  */
11915b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping,
12015b4650eSNick Piggin 		    loff_t pos, unsigned len, unsigned flags,
12115b4650eSNick Piggin 		    struct page **pagep, void **fsdata)
12231143d5dSDavid Howells {
12331143d5dSDavid Howells 	struct afs_writeback *candidate, *wb;
124496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
12515b4650eSNick Piggin 	struct page *page;
12631143d5dSDavid Howells 	struct key *key = file->private_data;
12715b4650eSNick Piggin 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
12815b4650eSNick Piggin 	unsigned to = from + len;
12915b4650eSNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
13031143d5dSDavid Howells 	int ret;
13131143d5dSDavid Howells 
13231143d5dSDavid Howells 	_enter("{%x:%u},{%lx},%u,%u",
13315b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, index, from, to);
13431143d5dSDavid Howells 
13531143d5dSDavid Howells 	candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
13631143d5dSDavid Howells 	if (!candidate)
13731143d5dSDavid Howells 		return -ENOMEM;
13831143d5dSDavid Howells 	candidate->vnode = vnode;
13915b4650eSNick Piggin 	candidate->first = candidate->last = index;
14015b4650eSNick Piggin 	candidate->offset_first = from;
14131143d5dSDavid Howells 	candidate->to_last = to;
142f129ccc9SAnton Blanchard 	INIT_LIST_HEAD(&candidate->link);
14331143d5dSDavid Howells 	candidate->usage = 1;
14431143d5dSDavid Howells 	candidate->state = AFS_WBACK_PENDING;
14531143d5dSDavid Howells 	init_waitqueue_head(&candidate->waitq);
14631143d5dSDavid Howells 
14754566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
14815b4650eSNick Piggin 	if (!page) {
14915b4650eSNick Piggin 		kfree(candidate);
15015b4650eSNick Piggin 		return -ENOMEM;
15115b4650eSNick Piggin 	}
15215b4650eSNick Piggin 	*pagep = page;
15315b4650eSNick Piggin 	/* page won't leak in error case: it eventually gets cleaned off LRU */
15415b4650eSNick Piggin 
1555e7f2337SAnton Blanchard 	if (!PageUptodate(page) && len != PAGE_CACHE_SIZE) {
1565e7f2337SAnton Blanchard 		ret = afs_fill_page(vnode, key, index << PAGE_CACHE_SHIFT, page);
15731143d5dSDavid Howells 		if (ret < 0) {
15831143d5dSDavid Howells 			kfree(candidate);
15931143d5dSDavid Howells 			_leave(" = %d [prep]", ret);
16031143d5dSDavid Howells 			return ret;
16131143d5dSDavid Howells 		}
16215b4650eSNick Piggin 		SetPageUptodate(page);
16331143d5dSDavid Howells 	}
16431143d5dSDavid Howells 
16531143d5dSDavid Howells try_again:
16631143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
16731143d5dSDavid Howells 
16831143d5dSDavid Howells 	/* see if this page is already pending a writeback under a suitable key
16931143d5dSDavid Howells 	 * - if so we can just join onto that one */
17031143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
17131143d5dSDavid Howells 	if (wb) {
17231143d5dSDavid Howells 		if (wb->key == key && wb->state == AFS_WBACK_PENDING)
17331143d5dSDavid Howells 			goto subsume_in_current_wb;
17431143d5dSDavid Howells 		goto flush_conflicting_wb;
17531143d5dSDavid Howells 	}
17631143d5dSDavid Howells 
17731143d5dSDavid Howells 	if (index > 0) {
17831143d5dSDavid Howells 		/* see if we can find an already pending writeback that we can
17931143d5dSDavid Howells 		 * append this page to */
18031143d5dSDavid Howells 		list_for_each_entry(wb, &vnode->writebacks, link) {
18131143d5dSDavid Howells 			if (wb->last == index - 1 && wb->key == key &&
18231143d5dSDavid Howells 			    wb->state == AFS_WBACK_PENDING)
18331143d5dSDavid Howells 				goto append_to_previous_wb;
18431143d5dSDavid Howells 		}
18531143d5dSDavid Howells 	}
18631143d5dSDavid Howells 
18731143d5dSDavid Howells 	list_add_tail(&candidate->link, &vnode->writebacks);
18831143d5dSDavid Howells 	candidate->key = key_get(key);
18931143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
19031143d5dSDavid Howells 	SetPagePrivate(page);
19131143d5dSDavid Howells 	set_page_private(page, (unsigned long) candidate);
19231143d5dSDavid Howells 	_leave(" = 0 [new]");
19331143d5dSDavid Howells 	return 0;
19431143d5dSDavid Howells 
19531143d5dSDavid Howells subsume_in_current_wb:
19631143d5dSDavid Howells 	_debug("subsume");
19731143d5dSDavid Howells 	ASSERTRANGE(wb->first, <=, index, <=, wb->last);
19815b4650eSNick Piggin 	if (index == wb->first && from < wb->offset_first)
19915b4650eSNick Piggin 		wb->offset_first = from;
20031143d5dSDavid Howells 	if (index == wb->last && to > wb->to_last)
20131143d5dSDavid Howells 		wb->to_last = to;
20231143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
20331143d5dSDavid Howells 	kfree(candidate);
20431143d5dSDavid Howells 	_leave(" = 0 [sub]");
20531143d5dSDavid Howells 	return 0;
20631143d5dSDavid Howells 
20731143d5dSDavid Howells append_to_previous_wb:
20831143d5dSDavid Howells 	_debug("append into %lx-%lx", wb->first, wb->last);
20931143d5dSDavid Howells 	wb->usage++;
21031143d5dSDavid Howells 	wb->last++;
21131143d5dSDavid Howells 	wb->to_last = to;
21231143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
21331143d5dSDavid Howells 	SetPagePrivate(page);
21431143d5dSDavid Howells 	set_page_private(page, (unsigned long) wb);
21531143d5dSDavid Howells 	kfree(candidate);
21631143d5dSDavid Howells 	_leave(" = 0 [app]");
21731143d5dSDavid Howells 	return 0;
21831143d5dSDavid Howells 
21931143d5dSDavid Howells 	/* the page is currently bound to another context, so if it's dirty we
22031143d5dSDavid Howells 	 * need to flush it before we can use the new context */
22131143d5dSDavid Howells flush_conflicting_wb:
22231143d5dSDavid Howells 	_debug("flush conflict");
22331143d5dSDavid Howells 	if (wb->state == AFS_WBACK_PENDING)
22431143d5dSDavid Howells 		wb->state = AFS_WBACK_CONFLICTING;
22531143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
22631143d5dSDavid Howells 	if (PageDirty(page)) {
22731143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
22831143d5dSDavid Howells 		if (ret < 0) {
22931143d5dSDavid Howells 			afs_put_writeback(candidate);
23031143d5dSDavid Howells 			_leave(" = %d", ret);
23131143d5dSDavid Howells 			return ret;
23231143d5dSDavid Howells 		}
23331143d5dSDavid Howells 	}
23431143d5dSDavid Howells 
23531143d5dSDavid Howells 	/* the page holds a ref on the writeback record */
23631143d5dSDavid Howells 	afs_put_writeback(wb);
23731143d5dSDavid Howells 	set_page_private(page, 0);
23831143d5dSDavid Howells 	ClearPagePrivate(page);
23931143d5dSDavid Howells 	goto try_again;
24031143d5dSDavid Howells }
24131143d5dSDavid Howells 
24231143d5dSDavid Howells /*
24331143d5dSDavid Howells  * finalise part of a write to a page
24431143d5dSDavid Howells  */
24515b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping,
24615b4650eSNick Piggin 		  loff_t pos, unsigned len, unsigned copied,
24715b4650eSNick Piggin 		  struct page *page, void *fsdata)
24831143d5dSDavid Howells {
249496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
25031143d5dSDavid Howells 	loff_t i_size, maybe_i_size;
25131143d5dSDavid Howells 
25215b4650eSNick Piggin 	_enter("{%x:%u},{%lx}",
25315b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, page->index);
25431143d5dSDavid Howells 
25515b4650eSNick Piggin 	maybe_i_size = pos + copied;
25631143d5dSDavid Howells 
25731143d5dSDavid Howells 	i_size = i_size_read(&vnode->vfs_inode);
25831143d5dSDavid Howells 	if (maybe_i_size > i_size) {
25931143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
26031143d5dSDavid Howells 		i_size = i_size_read(&vnode->vfs_inode);
26131143d5dSDavid Howells 		if (maybe_i_size > i_size)
26231143d5dSDavid Howells 			i_size_write(&vnode->vfs_inode, maybe_i_size);
26331143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
26431143d5dSDavid Howells 	}
26531143d5dSDavid Howells 
26631143d5dSDavid Howells 	set_page_dirty(page);
26731143d5dSDavid Howells 	if (PageDirty(page))
26831143d5dSDavid Howells 		_debug("dirtied");
26915b4650eSNick Piggin 	unlock_page(page);
27015b4650eSNick Piggin 	page_cache_release(page);
27131143d5dSDavid Howells 
27215b4650eSNick Piggin 	return copied;
27331143d5dSDavid Howells }
27431143d5dSDavid Howells 
27531143d5dSDavid Howells /*
27631143d5dSDavid Howells  * kill all the pages in the given range
27731143d5dSDavid Howells  */
27831143d5dSDavid Howells static void afs_kill_pages(struct afs_vnode *vnode, bool error,
27931143d5dSDavid Howells 			   pgoff_t first, pgoff_t last)
28031143d5dSDavid Howells {
28131143d5dSDavid Howells 	struct pagevec pv;
28231143d5dSDavid Howells 	unsigned count, loop;
28331143d5dSDavid Howells 
28431143d5dSDavid Howells 	_enter("{%x:%u},%lx-%lx",
28531143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
28631143d5dSDavid Howells 
28731143d5dSDavid Howells 	pagevec_init(&pv, 0);
28831143d5dSDavid Howells 
28931143d5dSDavid Howells 	do {
29031143d5dSDavid Howells 		_debug("kill %lx-%lx", first, last);
29131143d5dSDavid Howells 
29231143d5dSDavid Howells 		count = last - first + 1;
29331143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
29431143d5dSDavid Howells 			count = PAGEVEC_SIZE;
29531143d5dSDavid Howells 		pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
29631143d5dSDavid Howells 					      first, count, pv.pages);
29731143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
29831143d5dSDavid Howells 
29931143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
30031143d5dSDavid Howells 			ClearPageUptodate(pv.pages[loop]);
30131143d5dSDavid Howells 			if (error)
30231143d5dSDavid Howells 				SetPageError(pv.pages[loop]);
30331143d5dSDavid Howells 			end_page_writeback(pv.pages[loop]);
30431143d5dSDavid Howells 		}
30531143d5dSDavid Howells 
30631143d5dSDavid Howells 		__pagevec_release(&pv);
30731143d5dSDavid Howells 	} while (first < last);
30831143d5dSDavid Howells 
30931143d5dSDavid Howells 	_leave("");
31031143d5dSDavid Howells }
31131143d5dSDavid Howells 
31231143d5dSDavid Howells /*
31331143d5dSDavid Howells  * synchronously write back the locked page and any subsequent non-locked dirty
31431143d5dSDavid Howells  * pages also covered by the same writeback record
31531143d5dSDavid Howells  */
31631143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb,
31731143d5dSDavid Howells 					   struct page *primary_page)
31831143d5dSDavid Howells {
31931143d5dSDavid Howells 	struct page *pages[8], *page;
32031143d5dSDavid Howells 	unsigned long count;
32131143d5dSDavid Howells 	unsigned n, offset, to;
32231143d5dSDavid Howells 	pgoff_t start, first, last;
32331143d5dSDavid Howells 	int loop, ret;
32431143d5dSDavid Howells 
32531143d5dSDavid Howells 	_enter(",%lx", primary_page->index);
32631143d5dSDavid Howells 
32731143d5dSDavid Howells 	count = 1;
32831143d5dSDavid Howells 	if (!clear_page_dirty_for_io(primary_page))
32931143d5dSDavid Howells 		BUG();
33031143d5dSDavid Howells 	if (test_set_page_writeback(primary_page))
33131143d5dSDavid Howells 		BUG();
33231143d5dSDavid Howells 
33331143d5dSDavid Howells 	/* find all consecutive lockable dirty pages, stopping when we find a
33431143d5dSDavid Howells 	 * page that is not immediately lockable, is not dirty or is missing,
33531143d5dSDavid Howells 	 * or we reach the end of the range */
33631143d5dSDavid Howells 	start = primary_page->index;
33731143d5dSDavid Howells 	if (start >= wb->last)
33831143d5dSDavid Howells 		goto no_more;
33931143d5dSDavid Howells 	start++;
34031143d5dSDavid Howells 	do {
34131143d5dSDavid Howells 		_debug("more %lx [%lx]", start, count);
34231143d5dSDavid Howells 		n = wb->last - start + 1;
34331143d5dSDavid Howells 		if (n > ARRAY_SIZE(pages))
34431143d5dSDavid Howells 			n = ARRAY_SIZE(pages);
34531143d5dSDavid Howells 		n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
34631143d5dSDavid Howells 					  start, n, pages);
34731143d5dSDavid Howells 		_debug("fgpc %u", n);
34831143d5dSDavid Howells 		if (n == 0)
34931143d5dSDavid Howells 			goto no_more;
35031143d5dSDavid Howells 		if (pages[0]->index != start) {
3519d577b6aSDavid Howells 			do {
3529d577b6aSDavid Howells 				put_page(pages[--n]);
3539d577b6aSDavid Howells 			} while (n > 0);
35431143d5dSDavid Howells 			goto no_more;
35531143d5dSDavid Howells 		}
35631143d5dSDavid Howells 
35731143d5dSDavid Howells 		for (loop = 0; loop < n; loop++) {
35831143d5dSDavid Howells 			page = pages[loop];
35931143d5dSDavid Howells 			if (page->index > wb->last)
36031143d5dSDavid Howells 				break;
361529ae9aaSNick Piggin 			if (!trylock_page(page))
36231143d5dSDavid Howells 				break;
36331143d5dSDavid Howells 			if (!PageDirty(page) ||
36431143d5dSDavid Howells 			    page_private(page) != (unsigned long) wb) {
36531143d5dSDavid Howells 				unlock_page(page);
36631143d5dSDavid Howells 				break;
36731143d5dSDavid Howells 			}
36831143d5dSDavid Howells 			if (!clear_page_dirty_for_io(page))
36931143d5dSDavid Howells 				BUG();
37031143d5dSDavid Howells 			if (test_set_page_writeback(page))
37131143d5dSDavid Howells 				BUG();
37231143d5dSDavid Howells 			unlock_page(page);
37331143d5dSDavid Howells 			put_page(page);
37431143d5dSDavid Howells 		}
37531143d5dSDavid Howells 		count += loop;
37631143d5dSDavid Howells 		if (loop < n) {
37731143d5dSDavid Howells 			for (; loop < n; loop++)
37831143d5dSDavid Howells 				put_page(pages[loop]);
37931143d5dSDavid Howells 			goto no_more;
38031143d5dSDavid Howells 		}
38131143d5dSDavid Howells 
38231143d5dSDavid Howells 		start += loop;
38331143d5dSDavid Howells 	} while (start <= wb->last && count < 65536);
38431143d5dSDavid Howells 
38531143d5dSDavid Howells no_more:
38631143d5dSDavid Howells 	/* we now have a contiguous set of dirty pages, each with writeback set
38731143d5dSDavid Howells 	 * and the dirty mark cleared; the first page is locked and must remain
38831143d5dSDavid Howells 	 * so, all the rest are unlocked */
38931143d5dSDavid Howells 	first = primary_page->index;
39031143d5dSDavid Howells 	last = first + count - 1;
39131143d5dSDavid Howells 
39231143d5dSDavid Howells 	offset = (first == wb->first) ? wb->offset_first : 0;
39331143d5dSDavid Howells 	to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
39431143d5dSDavid Howells 
39531143d5dSDavid Howells 	_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
39631143d5dSDavid Howells 
39731143d5dSDavid Howells 	ret = afs_vnode_store_data(wb, first, last, offset, to);
39831143d5dSDavid Howells 	if (ret < 0) {
39931143d5dSDavid Howells 		switch (ret) {
40031143d5dSDavid Howells 		case -EDQUOT:
40131143d5dSDavid Howells 		case -ENOSPC:
40231143d5dSDavid Howells 			set_bit(AS_ENOSPC,
40331143d5dSDavid Howells 				&wb->vnode->vfs_inode.i_mapping->flags);
40431143d5dSDavid Howells 			break;
40531143d5dSDavid Howells 		case -EROFS:
40631143d5dSDavid Howells 		case -EIO:
40731143d5dSDavid Howells 		case -EREMOTEIO:
40831143d5dSDavid Howells 		case -EFBIG:
40931143d5dSDavid Howells 		case -ENOENT:
41031143d5dSDavid Howells 		case -ENOMEDIUM:
41131143d5dSDavid Howells 		case -ENXIO:
41231143d5dSDavid Howells 			afs_kill_pages(wb->vnode, true, first, last);
41331143d5dSDavid Howells 			set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
41431143d5dSDavid Howells 			break;
41531143d5dSDavid Howells 		case -EACCES:
41631143d5dSDavid Howells 		case -EPERM:
41731143d5dSDavid Howells 		case -ENOKEY:
41831143d5dSDavid Howells 		case -EKEYEXPIRED:
41931143d5dSDavid Howells 		case -EKEYREJECTED:
42031143d5dSDavid Howells 		case -EKEYREVOKED:
42131143d5dSDavid Howells 			afs_kill_pages(wb->vnode, false, first, last);
42231143d5dSDavid Howells 			break;
42331143d5dSDavid Howells 		default:
42431143d5dSDavid Howells 			break;
42531143d5dSDavid Howells 		}
42631143d5dSDavid Howells 	} else {
42731143d5dSDavid Howells 		ret = count;
42831143d5dSDavid Howells 	}
42931143d5dSDavid Howells 
43031143d5dSDavid Howells 	_leave(" = %d", ret);
43131143d5dSDavid Howells 	return ret;
43231143d5dSDavid Howells }
43331143d5dSDavid Howells 
43431143d5dSDavid Howells /*
43531143d5dSDavid Howells  * write a page back to the server
43631143d5dSDavid Howells  * - the caller locked the page for us
43731143d5dSDavid Howells  */
43831143d5dSDavid Howells int afs_writepage(struct page *page, struct writeback_control *wbc)
43931143d5dSDavid Howells {
44031143d5dSDavid Howells 	struct afs_writeback *wb;
44131143d5dSDavid Howells 	int ret;
44231143d5dSDavid Howells 
44331143d5dSDavid Howells 	_enter("{%lx},", page->index);
44431143d5dSDavid Howells 
44531143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
44631143d5dSDavid Howells 	ASSERT(wb != NULL);
44731143d5dSDavid Howells 
44831143d5dSDavid Howells 	ret = afs_write_back_from_locked_page(wb, page);
44931143d5dSDavid Howells 	unlock_page(page);
45031143d5dSDavid Howells 	if (ret < 0) {
45131143d5dSDavid Howells 		_leave(" = %d", ret);
45231143d5dSDavid Howells 		return 0;
45331143d5dSDavid Howells 	}
45431143d5dSDavid Howells 
45531143d5dSDavid Howells 	wbc->nr_to_write -= ret;
45631143d5dSDavid Howells 
45731143d5dSDavid Howells 	_leave(" = 0");
45831143d5dSDavid Howells 	return 0;
45931143d5dSDavid Howells }
46031143d5dSDavid Howells 
46131143d5dSDavid Howells /*
46231143d5dSDavid Howells  * write a region of pages back to the server
46331143d5dSDavid Howells  */
464c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
46531143d5dSDavid Howells 				 struct writeback_control *wbc,
46631143d5dSDavid Howells 				 pgoff_t index, pgoff_t end, pgoff_t *_next)
46731143d5dSDavid Howells {
46831143d5dSDavid Howells 	struct afs_writeback *wb;
46931143d5dSDavid Howells 	struct page *page;
47031143d5dSDavid Howells 	int ret, n;
47131143d5dSDavid Howells 
47231143d5dSDavid Howells 	_enter(",,%lx,%lx,", index, end);
47331143d5dSDavid Howells 
47431143d5dSDavid Howells 	do {
47531143d5dSDavid Howells 		n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
47631143d5dSDavid Howells 				       1, &page);
47731143d5dSDavid Howells 		if (!n)
47831143d5dSDavid Howells 			break;
47931143d5dSDavid Howells 
48031143d5dSDavid Howells 		_debug("wback %lx", page->index);
48131143d5dSDavid Howells 
48231143d5dSDavid Howells 		if (page->index > end) {
48331143d5dSDavid Howells 			*_next = index;
48431143d5dSDavid Howells 			page_cache_release(page);
48531143d5dSDavid Howells 			_leave(" = 0 [%lx]", *_next);
48631143d5dSDavid Howells 			return 0;
48731143d5dSDavid Howells 		}
48831143d5dSDavid Howells 
48931143d5dSDavid Howells 		/* at this point we hold neither mapping->tree_lock nor lock on
49031143d5dSDavid Howells 		 * the page itself: the page may be truncated or invalidated
49131143d5dSDavid Howells 		 * (changing page->mapping to NULL), or even swizzled back from
49231143d5dSDavid Howells 		 * swapper_space to tmpfs file mapping
49331143d5dSDavid Howells 		 */
49431143d5dSDavid Howells 		lock_page(page);
49531143d5dSDavid Howells 
49631143d5dSDavid Howells 		if (page->mapping != mapping) {
49731143d5dSDavid Howells 			unlock_page(page);
49831143d5dSDavid Howells 			page_cache_release(page);
49931143d5dSDavid Howells 			continue;
50031143d5dSDavid Howells 		}
50131143d5dSDavid Howells 
50231143d5dSDavid Howells 		if (wbc->sync_mode != WB_SYNC_NONE)
50331143d5dSDavid Howells 			wait_on_page_writeback(page);
50431143d5dSDavid Howells 
50531143d5dSDavid Howells 		if (PageWriteback(page) || !PageDirty(page)) {
50631143d5dSDavid Howells 			unlock_page(page);
50731143d5dSDavid Howells 			continue;
50831143d5dSDavid Howells 		}
50931143d5dSDavid Howells 
51031143d5dSDavid Howells 		wb = (struct afs_writeback *) page_private(page);
51131143d5dSDavid Howells 		ASSERT(wb != NULL);
51231143d5dSDavid Howells 
51331143d5dSDavid Howells 		spin_lock(&wb->vnode->writeback_lock);
51431143d5dSDavid Howells 		wb->state = AFS_WBACK_WRITING;
51531143d5dSDavid Howells 		spin_unlock(&wb->vnode->writeback_lock);
51631143d5dSDavid Howells 
51731143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
51831143d5dSDavid Howells 		unlock_page(page);
51931143d5dSDavid Howells 		page_cache_release(page);
52031143d5dSDavid Howells 		if (ret < 0) {
52131143d5dSDavid Howells 			_leave(" = %d", ret);
52231143d5dSDavid Howells 			return ret;
52331143d5dSDavid Howells 		}
52431143d5dSDavid Howells 
52531143d5dSDavid Howells 		wbc->nr_to_write -= ret;
52631143d5dSDavid Howells 
52731143d5dSDavid Howells 		cond_resched();
52831143d5dSDavid Howells 	} while (index < end && wbc->nr_to_write > 0);
52931143d5dSDavid Howells 
53031143d5dSDavid Howells 	*_next = index;
53131143d5dSDavid Howells 	_leave(" = 0 [%lx]", *_next);
53231143d5dSDavid Howells 	return 0;
53331143d5dSDavid Howells }
53431143d5dSDavid Howells 
53531143d5dSDavid Howells /*
53631143d5dSDavid Howells  * write some of the pending data back to the server
53731143d5dSDavid Howells  */
53831143d5dSDavid Howells int afs_writepages(struct address_space *mapping,
53931143d5dSDavid Howells 		   struct writeback_control *wbc)
54031143d5dSDavid Howells {
54131143d5dSDavid Howells 	pgoff_t start, end, next;
54231143d5dSDavid Howells 	int ret;
54331143d5dSDavid Howells 
54431143d5dSDavid Howells 	_enter("");
54531143d5dSDavid Howells 
54631143d5dSDavid Howells 	if (wbc->range_cyclic) {
54731143d5dSDavid Howells 		start = mapping->writeback_index;
54831143d5dSDavid Howells 		end = -1;
54931143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
5501b430beeSWu Fengguang 		if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
55131143d5dSDavid Howells 			ret = afs_writepages_region(mapping, wbc, 0, start,
55231143d5dSDavid Howells 						    &next);
55331143d5dSDavid Howells 		mapping->writeback_index = next;
55431143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
55531143d5dSDavid Howells 		end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
55631143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, end, &next);
55731143d5dSDavid Howells 		if (wbc->nr_to_write > 0)
55831143d5dSDavid Howells 			mapping->writeback_index = next;
55931143d5dSDavid Howells 	} else {
56031143d5dSDavid Howells 		start = wbc->range_start >> PAGE_CACHE_SHIFT;
56131143d5dSDavid Howells 		end = wbc->range_end >> PAGE_CACHE_SHIFT;
56231143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
56331143d5dSDavid Howells 	}
56431143d5dSDavid Howells 
56531143d5dSDavid Howells 	_leave(" = %d", ret);
56631143d5dSDavid Howells 	return ret;
56731143d5dSDavid Howells }
56831143d5dSDavid Howells 
56931143d5dSDavid Howells /*
57031143d5dSDavid Howells  * completion of write to server
57131143d5dSDavid Howells  */
57231143d5dSDavid Howells void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
57331143d5dSDavid Howells {
57431143d5dSDavid Howells 	struct afs_writeback *wb = call->wb;
57531143d5dSDavid Howells 	struct pagevec pv;
57631143d5dSDavid Howells 	unsigned count, loop;
57731143d5dSDavid Howells 	pgoff_t first = call->first, last = call->last;
57831143d5dSDavid Howells 	bool free_wb;
57931143d5dSDavid Howells 
58031143d5dSDavid Howells 	_enter("{%x:%u},{%lx-%lx}",
58131143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
58231143d5dSDavid Howells 
58331143d5dSDavid Howells 	ASSERT(wb != NULL);
58431143d5dSDavid Howells 
58531143d5dSDavid Howells 	pagevec_init(&pv, 0);
58631143d5dSDavid Howells 
58731143d5dSDavid Howells 	do {
5885bbf5d39SDavid Howells 		_debug("done %lx-%lx", first, last);
58931143d5dSDavid Howells 
59031143d5dSDavid Howells 		count = last - first + 1;
59131143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
59231143d5dSDavid Howells 			count = PAGEVEC_SIZE;
59331143d5dSDavid Howells 		pv.nr = find_get_pages_contig(call->mapping, first, count,
59431143d5dSDavid Howells 					      pv.pages);
59531143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
59631143d5dSDavid Howells 
59731143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
59831143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
59931143d5dSDavid Howells 			struct page *page = pv.pages[loop];
60031143d5dSDavid Howells 			end_page_writeback(page);
60131143d5dSDavid Howells 			if (page_private(page) == (unsigned long) wb) {
60231143d5dSDavid Howells 				set_page_private(page, 0);
60331143d5dSDavid Howells 				ClearPagePrivate(page);
60431143d5dSDavid Howells 				wb->usage--;
60531143d5dSDavid Howells 			}
60631143d5dSDavid Howells 		}
60731143d5dSDavid Howells 		free_wb = false;
60831143d5dSDavid Howells 		if (wb->usage == 0) {
60931143d5dSDavid Howells 			afs_unlink_writeback(wb);
61031143d5dSDavid Howells 			free_wb = true;
61131143d5dSDavid Howells 		}
61231143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
61331143d5dSDavid Howells 		first += count;
61431143d5dSDavid Howells 		if (free_wb) {
61531143d5dSDavid Howells 			afs_free_writeback(wb);
61631143d5dSDavid Howells 			wb = NULL;
61731143d5dSDavid Howells 		}
61831143d5dSDavid Howells 
61931143d5dSDavid Howells 		__pagevec_release(&pv);
6205bbf5d39SDavid Howells 	} while (first <= last);
62131143d5dSDavid Howells 
62231143d5dSDavid Howells 	_leave("");
62331143d5dSDavid Howells }
62431143d5dSDavid Howells 
62531143d5dSDavid Howells /*
62631143d5dSDavid Howells  * write to an AFS file
62731143d5dSDavid Howells  */
62850b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
62931143d5dSDavid Howells {
630496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
63131143d5dSDavid Howells 	ssize_t result;
63250b5551dSAl Viro 	size_t count = iov_iter_count(from);
63331143d5dSDavid Howells 
63450b5551dSAl Viro 	_enter("{%x.%u},{%zu},",
63550b5551dSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, count);
63631143d5dSDavid Howells 
63731143d5dSDavid Howells 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
63831143d5dSDavid Howells 		printk(KERN_INFO
63931143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
64031143d5dSDavid Howells 		return -EBUSY;
64131143d5dSDavid Howells 	}
64231143d5dSDavid Howells 
64331143d5dSDavid Howells 	if (!count)
64431143d5dSDavid Howells 		return 0;
64531143d5dSDavid Howells 
64650b5551dSAl Viro 	result = generic_file_write_iter(iocb, from);
64731143d5dSDavid Howells 	if (IS_ERR_VALUE(result)) {
64831143d5dSDavid Howells 		_leave(" = %zd", result);
64931143d5dSDavid Howells 		return result;
65031143d5dSDavid Howells 	}
65131143d5dSDavid Howells 
65231143d5dSDavid Howells 	_leave(" = %zd", result);
65331143d5dSDavid Howells 	return result;
65431143d5dSDavid Howells }
65531143d5dSDavid Howells 
65631143d5dSDavid Howells /*
65731143d5dSDavid Howells  * flush the vnode to the fileserver
65831143d5dSDavid Howells  */
65931143d5dSDavid Howells int afs_writeback_all(struct afs_vnode *vnode)
66031143d5dSDavid Howells {
66131143d5dSDavid Howells 	struct address_space *mapping = vnode->vfs_inode.i_mapping;
66231143d5dSDavid Howells 	struct writeback_control wbc = {
66331143d5dSDavid Howells 		.sync_mode	= WB_SYNC_ALL,
66431143d5dSDavid Howells 		.nr_to_write	= LONG_MAX,
66531143d5dSDavid Howells 		.range_cyclic	= 1,
66631143d5dSDavid Howells 	};
66731143d5dSDavid Howells 	int ret;
66831143d5dSDavid Howells 
66931143d5dSDavid Howells 	_enter("");
67031143d5dSDavid Howells 
67131143d5dSDavid Howells 	ret = mapping->a_ops->writepages(mapping, &wbc);
67231143d5dSDavid Howells 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
67331143d5dSDavid Howells 
67431143d5dSDavid Howells 	_leave(" = %d", ret);
67531143d5dSDavid Howells 	return ret;
67631143d5dSDavid Howells }
67731143d5dSDavid Howells 
67831143d5dSDavid Howells /*
67931143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
68031143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
68131143d5dSDavid Howells  *   whether any write errors occurred for this process.
68231143d5dSDavid Howells  */
68302c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
68431143d5dSDavid Howells {
6857ea80859SChristoph Hellwig 	struct dentry *dentry = file->f_path.dentry;
68602c24a82SJosef Bacik 	struct inode *inode = file->f_mapping->host;
68731143d5dSDavid Howells 	struct afs_writeback *wb, *xwb;
68831143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
68931143d5dSDavid Howells 	int ret;
69031143d5dSDavid Howells 
69131143d5dSDavid Howells 	_enter("{%x:%u},{n=%s},%d",
69231143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
69331143d5dSDavid Howells 	       datasync);
69431143d5dSDavid Howells 
69502c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
69602c24a82SJosef Bacik 	if (ret)
69702c24a82SJosef Bacik 		return ret;
69802c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
69902c24a82SJosef Bacik 
70031143d5dSDavid Howells 	/* use a writeback record as a marker in the queue - when this reaches
70131143d5dSDavid Howells 	 * the front of the queue, all the outstanding writes are either
70231143d5dSDavid Howells 	 * completed or rejected */
70331143d5dSDavid Howells 	wb = kzalloc(sizeof(*wb), GFP_KERNEL);
70402c24a82SJosef Bacik 	if (!wb) {
70502c24a82SJosef Bacik 		ret = -ENOMEM;
70602c24a82SJosef Bacik 		goto out;
70702c24a82SJosef Bacik 	}
70831143d5dSDavid Howells 	wb->vnode = vnode;
70931143d5dSDavid Howells 	wb->first = 0;
71031143d5dSDavid Howells 	wb->last = -1;
71131143d5dSDavid Howells 	wb->offset_first = 0;
71231143d5dSDavid Howells 	wb->to_last = PAGE_SIZE;
71331143d5dSDavid Howells 	wb->usage = 1;
71431143d5dSDavid Howells 	wb->state = AFS_WBACK_SYNCING;
71531143d5dSDavid Howells 	init_waitqueue_head(&wb->waitq);
71631143d5dSDavid Howells 
71731143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
71831143d5dSDavid Howells 	list_for_each_entry(xwb, &vnode->writebacks, link) {
71931143d5dSDavid Howells 		if (xwb->state == AFS_WBACK_PENDING)
72031143d5dSDavid Howells 			xwb->state = AFS_WBACK_CONFLICTING;
72131143d5dSDavid Howells 	}
72231143d5dSDavid Howells 	list_add_tail(&wb->link, &vnode->writebacks);
72331143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
72431143d5dSDavid Howells 
72531143d5dSDavid Howells 	/* push all the outstanding writebacks to the server */
72631143d5dSDavid Howells 	ret = afs_writeback_all(vnode);
72731143d5dSDavid Howells 	if (ret < 0) {
72831143d5dSDavid Howells 		afs_put_writeback(wb);
72931143d5dSDavid Howells 		_leave(" = %d [wb]", ret);
73002c24a82SJosef Bacik 		goto out;
73131143d5dSDavid Howells 	}
73231143d5dSDavid Howells 
73331143d5dSDavid Howells 	/* wait for the preceding writes to actually complete */
73431143d5dSDavid Howells 	ret = wait_event_interruptible(wb->waitq,
73531143d5dSDavid Howells 				       wb->state == AFS_WBACK_COMPLETE ||
73631143d5dSDavid Howells 				       vnode->writebacks.next == &wb->link);
73731143d5dSDavid Howells 	afs_put_writeback(wb);
73831143d5dSDavid Howells 	_leave(" = %d", ret);
73902c24a82SJosef Bacik out:
74002c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
74131143d5dSDavid Howells 	return ret;
74231143d5dSDavid Howells }
7439b3f26c9SDavid Howells 
7449b3f26c9SDavid Howells /*
7459b3f26c9SDavid Howells  * notification that a previously read-only page is about to become writable
7469b3f26c9SDavid Howells  * - if it returns an error, the caller will deliver a bus error signal
7479b3f26c9SDavid Howells  */
7489b3f26c9SDavid Howells int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
7499b3f26c9SDavid Howells {
7509b3f26c9SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
7519b3f26c9SDavid Howells 
7529b3f26c9SDavid Howells 	_enter("{{%x:%u}},{%lx}",
7539b3f26c9SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, page->index);
7549b3f26c9SDavid Howells 
7559b3f26c9SDavid Howells 	/* wait for the page to be written to the cache before we allow it to
7569b3f26c9SDavid Howells 	 * be modified */
7579b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE
7589b3f26c9SDavid Howells 	fscache_wait_on_page_write(vnode->cache, page);
7599b3f26c9SDavid Howells #endif
7609b3f26c9SDavid Howells 
7619b3f26c9SDavid Howells 	_leave(" = 0");
7629b3f26c9SDavid Howells 	return 0;
7639b3f26c9SDavid Howells }
764