xref: /openbmc/linux/fs/afs/write.c (revision 15b4650e)
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,
8715b4650eSNick Piggin 			 loff_t pos, unsigned len, struct page *page)
8831143d5dSDavid Howells {
8915b4650eSNick Piggin 	loff_t i_size;
9015b4650eSNick Piggin 	unsigned eof;
9131143d5dSDavid Howells 	int ret;
9231143d5dSDavid Howells 
9315b4650eSNick Piggin 	_enter(",,%llu,%u", (unsigned long long)pos, len);
9431143d5dSDavid Howells 
9515b4650eSNick Piggin 	ASSERTCMP(len, <=, PAGE_CACHE_SIZE);
9631143d5dSDavid Howells 
9715b4650eSNick Piggin 	i_size = i_size_read(&vnode->vfs_inode);
9815b4650eSNick Piggin 	if (pos + len > i_size)
9915b4650eSNick Piggin 		eof = i_size;
10015b4650eSNick Piggin 	else
10115b4650eSNick Piggin 		eof = PAGE_CACHE_SIZE;
10215b4650eSNick Piggin 
10315b4650eSNick Piggin 	ret = afs_vnode_fetch_data(vnode, key, 0, eof, page);
10431143d5dSDavid Howells 	if (ret < 0) {
10531143d5dSDavid Howells 		if (ret == -ENOENT) {
10631143d5dSDavid Howells 			_debug("got NOENT from server"
10731143d5dSDavid Howells 			       " - marking file deleted and stale");
10831143d5dSDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
10931143d5dSDavid Howells 			ret = -ESTALE;
11031143d5dSDavid Howells 		}
11131143d5dSDavid Howells 	}
11231143d5dSDavid Howells 
11331143d5dSDavid Howells 	_leave(" = %d", ret);
11431143d5dSDavid Howells 	return ret;
11531143d5dSDavid Howells }
11631143d5dSDavid Howells 
11731143d5dSDavid Howells /*
11831143d5dSDavid Howells  * prepare to perform part of a write to a page
11931143d5dSDavid Howells  */
12015b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping,
12115b4650eSNick Piggin 		    loff_t pos, unsigned len, unsigned flags,
12215b4650eSNick Piggin 		    struct page **pagep, void **fsdata)
12331143d5dSDavid Howells {
12431143d5dSDavid Howells 	struct afs_writeback *candidate, *wb;
12531143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
12615b4650eSNick Piggin 	struct page *page;
12731143d5dSDavid Howells 	struct key *key = file->private_data;
12815b4650eSNick Piggin 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
12915b4650eSNick Piggin 	unsigned to = from + len;
13015b4650eSNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
13131143d5dSDavid Howells 	int ret;
13231143d5dSDavid Howells 
13331143d5dSDavid Howells 	_enter("{%x:%u},{%lx},%u,%u",
13415b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, index, from, to);
13531143d5dSDavid Howells 
13631143d5dSDavid Howells 	candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
13731143d5dSDavid Howells 	if (!candidate)
13831143d5dSDavid Howells 		return -ENOMEM;
13931143d5dSDavid Howells 	candidate->vnode = vnode;
14015b4650eSNick Piggin 	candidate->first = candidate->last = index;
14115b4650eSNick Piggin 	candidate->offset_first = from;
14231143d5dSDavid Howells 	candidate->to_last = to;
14331143d5dSDavid Howells 	candidate->usage = 1;
14431143d5dSDavid Howells 	candidate->state = AFS_WBACK_PENDING;
14531143d5dSDavid Howells 	init_waitqueue_head(&candidate->waitq);
14631143d5dSDavid Howells 
14715b4650eSNick Piggin 	page = __grab_cache_page(mapping, index);
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 
15531143d5dSDavid Howells 	if (!PageUptodate(page)) {
15631143d5dSDavid Howells 		_debug("not up to date");
15715b4650eSNick Piggin 		ret = afs_fill_page(vnode, key, pos, len, page);
15831143d5dSDavid Howells 		if (ret < 0) {
15931143d5dSDavid Howells 			kfree(candidate);
16031143d5dSDavid Howells 			_leave(" = %d [prep]", ret);
16131143d5dSDavid Howells 			return ret;
16231143d5dSDavid Howells 		}
16315b4650eSNick Piggin 		SetPageUptodate(page);
16431143d5dSDavid Howells 	}
16531143d5dSDavid Howells 
16631143d5dSDavid Howells try_again:
16731143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
16831143d5dSDavid Howells 
16931143d5dSDavid Howells 	/* see if this page is already pending a writeback under a suitable key
17031143d5dSDavid Howells 	 * - if so we can just join onto that one */
17131143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
17231143d5dSDavid Howells 	if (wb) {
17331143d5dSDavid Howells 		if (wb->key == key && wb->state == AFS_WBACK_PENDING)
17431143d5dSDavid Howells 			goto subsume_in_current_wb;
17531143d5dSDavid Howells 		goto flush_conflicting_wb;
17631143d5dSDavid Howells 	}
17731143d5dSDavid Howells 
17831143d5dSDavid Howells 	if (index > 0) {
17931143d5dSDavid Howells 		/* see if we can find an already pending writeback that we can
18031143d5dSDavid Howells 		 * append this page to */
18131143d5dSDavid Howells 		list_for_each_entry(wb, &vnode->writebacks, link) {
18231143d5dSDavid Howells 			if (wb->last == index - 1 && wb->key == key &&
18331143d5dSDavid Howells 			    wb->state == AFS_WBACK_PENDING)
18431143d5dSDavid Howells 				goto append_to_previous_wb;
18531143d5dSDavid Howells 		}
18631143d5dSDavid Howells 	}
18731143d5dSDavid Howells 
18831143d5dSDavid Howells 	list_add_tail(&candidate->link, &vnode->writebacks);
18931143d5dSDavid Howells 	candidate->key = key_get(key);
19031143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
19131143d5dSDavid Howells 	SetPagePrivate(page);
19231143d5dSDavid Howells 	set_page_private(page, (unsigned long) candidate);
19331143d5dSDavid Howells 	_leave(" = 0 [new]");
19431143d5dSDavid Howells 	return 0;
19531143d5dSDavid Howells 
19631143d5dSDavid Howells subsume_in_current_wb:
19731143d5dSDavid Howells 	_debug("subsume");
19831143d5dSDavid Howells 	ASSERTRANGE(wb->first, <=, index, <=, wb->last);
19915b4650eSNick Piggin 	if (index == wb->first && from < wb->offset_first)
20015b4650eSNick Piggin 		wb->offset_first = from;
20131143d5dSDavid Howells 	if (index == wb->last && to > wb->to_last)
20231143d5dSDavid Howells 		wb->to_last = to;
20331143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
20431143d5dSDavid Howells 	kfree(candidate);
20531143d5dSDavid Howells 	_leave(" = 0 [sub]");
20631143d5dSDavid Howells 	return 0;
20731143d5dSDavid Howells 
20831143d5dSDavid Howells append_to_previous_wb:
20931143d5dSDavid Howells 	_debug("append into %lx-%lx", wb->first, wb->last);
21031143d5dSDavid Howells 	wb->usage++;
21131143d5dSDavid Howells 	wb->last++;
21231143d5dSDavid Howells 	wb->to_last = to;
21331143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
21431143d5dSDavid Howells 	SetPagePrivate(page);
21531143d5dSDavid Howells 	set_page_private(page, (unsigned long) wb);
21631143d5dSDavid Howells 	kfree(candidate);
21731143d5dSDavid Howells 	_leave(" = 0 [app]");
21831143d5dSDavid Howells 	return 0;
21931143d5dSDavid Howells 
22031143d5dSDavid Howells 	/* the page is currently bound to another context, so if it's dirty we
22131143d5dSDavid Howells 	 * need to flush it before we can use the new context */
22231143d5dSDavid Howells flush_conflicting_wb:
22331143d5dSDavid Howells 	_debug("flush conflict");
22431143d5dSDavid Howells 	if (wb->state == AFS_WBACK_PENDING)
22531143d5dSDavid Howells 		wb->state = AFS_WBACK_CONFLICTING;
22631143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
22731143d5dSDavid Howells 	if (PageDirty(page)) {
22831143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
22931143d5dSDavid Howells 		if (ret < 0) {
23031143d5dSDavid Howells 			afs_put_writeback(candidate);
23131143d5dSDavid Howells 			_leave(" = %d", ret);
23231143d5dSDavid Howells 			return ret;
23331143d5dSDavid Howells 		}
23431143d5dSDavid Howells 	}
23531143d5dSDavid Howells 
23631143d5dSDavid Howells 	/* the page holds a ref on the writeback record */
23731143d5dSDavid Howells 	afs_put_writeback(wb);
23831143d5dSDavid Howells 	set_page_private(page, 0);
23931143d5dSDavid Howells 	ClearPagePrivate(page);
24031143d5dSDavid Howells 	goto try_again;
24131143d5dSDavid Howells }
24231143d5dSDavid Howells 
24331143d5dSDavid Howells /*
24431143d5dSDavid Howells  * finalise part of a write to a page
24531143d5dSDavid Howells  */
24615b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping,
24715b4650eSNick Piggin 		  loff_t pos, unsigned len, unsigned copied,
24815b4650eSNick Piggin 		  struct page *page, void *fsdata)
24931143d5dSDavid Howells {
25031143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
25131143d5dSDavid Howells 	loff_t i_size, maybe_i_size;
25231143d5dSDavid Howells 
25315b4650eSNick Piggin 	_enter("{%x:%u},{%lx}",
25415b4650eSNick Piggin 	       vnode->fid.vid, vnode->fid.vnode, page->index);
25531143d5dSDavid Howells 
25615b4650eSNick Piggin 	maybe_i_size = pos + copied;
25731143d5dSDavid Howells 
25831143d5dSDavid Howells 	i_size = i_size_read(&vnode->vfs_inode);
25931143d5dSDavid Howells 	if (maybe_i_size > i_size) {
26031143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
26131143d5dSDavid Howells 		i_size = i_size_read(&vnode->vfs_inode);
26231143d5dSDavid Howells 		if (maybe_i_size > i_size)
26331143d5dSDavid Howells 			i_size_write(&vnode->vfs_inode, maybe_i_size);
26431143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
26531143d5dSDavid Howells 	}
26631143d5dSDavid Howells 
26731143d5dSDavid Howells 	set_page_dirty(page);
26831143d5dSDavid Howells 	if (PageDirty(page))
26931143d5dSDavid Howells 		_debug("dirtied");
27015b4650eSNick Piggin 	unlock_page(page);
27115b4650eSNick Piggin 	page_cache_release(page);
27231143d5dSDavid Howells 
27315b4650eSNick Piggin 	return copied;
27431143d5dSDavid Howells }
27531143d5dSDavid Howells 
27631143d5dSDavid Howells /*
27731143d5dSDavid Howells  * kill all the pages in the given range
27831143d5dSDavid Howells  */
27931143d5dSDavid Howells static void afs_kill_pages(struct afs_vnode *vnode, bool error,
28031143d5dSDavid Howells 			   pgoff_t first, pgoff_t last)
28131143d5dSDavid Howells {
28231143d5dSDavid Howells 	struct pagevec pv;
28331143d5dSDavid Howells 	unsigned count, loop;
28431143d5dSDavid Howells 
28531143d5dSDavid Howells 	_enter("{%x:%u},%lx-%lx",
28631143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
28731143d5dSDavid Howells 
28831143d5dSDavid Howells 	pagevec_init(&pv, 0);
28931143d5dSDavid Howells 
29031143d5dSDavid Howells 	do {
29131143d5dSDavid Howells 		_debug("kill %lx-%lx", first, last);
29231143d5dSDavid Howells 
29331143d5dSDavid Howells 		count = last - first + 1;
29431143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
29531143d5dSDavid Howells 			count = PAGEVEC_SIZE;
29631143d5dSDavid Howells 		pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
29731143d5dSDavid Howells 					      first, count, pv.pages);
29831143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
29931143d5dSDavid Howells 
30031143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
30131143d5dSDavid Howells 			ClearPageUptodate(pv.pages[loop]);
30231143d5dSDavid Howells 			if (error)
30331143d5dSDavid Howells 				SetPageError(pv.pages[loop]);
30431143d5dSDavid Howells 			end_page_writeback(pv.pages[loop]);
30531143d5dSDavid Howells 		}
30631143d5dSDavid Howells 
30731143d5dSDavid Howells 		__pagevec_release(&pv);
30831143d5dSDavid Howells 	} while (first < last);
30931143d5dSDavid Howells 
31031143d5dSDavid Howells 	_leave("");
31131143d5dSDavid Howells }
31231143d5dSDavid Howells 
31331143d5dSDavid Howells /*
31431143d5dSDavid Howells  * synchronously write back the locked page and any subsequent non-locked dirty
31531143d5dSDavid Howells  * pages also covered by the same writeback record
31631143d5dSDavid Howells  */
31731143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb,
31831143d5dSDavid Howells 					   struct page *primary_page)
31931143d5dSDavid Howells {
32031143d5dSDavid Howells 	struct page *pages[8], *page;
32131143d5dSDavid Howells 	unsigned long count;
32231143d5dSDavid Howells 	unsigned n, offset, to;
32331143d5dSDavid Howells 	pgoff_t start, first, last;
32431143d5dSDavid Howells 	int loop, ret;
32531143d5dSDavid Howells 
32631143d5dSDavid Howells 	_enter(",%lx", primary_page->index);
32731143d5dSDavid Howells 
32831143d5dSDavid Howells 	count = 1;
32931143d5dSDavid Howells 	if (!clear_page_dirty_for_io(primary_page))
33031143d5dSDavid Howells 		BUG();
33131143d5dSDavid Howells 	if (test_set_page_writeback(primary_page))
33231143d5dSDavid Howells 		BUG();
33331143d5dSDavid Howells 
33431143d5dSDavid Howells 	/* find all consecutive lockable dirty pages, stopping when we find a
33531143d5dSDavid Howells 	 * page that is not immediately lockable, is not dirty or is missing,
33631143d5dSDavid Howells 	 * or we reach the end of the range */
33731143d5dSDavid Howells 	start = primary_page->index;
33831143d5dSDavid Howells 	if (start >= wb->last)
33931143d5dSDavid Howells 		goto no_more;
34031143d5dSDavid Howells 	start++;
34131143d5dSDavid Howells 	do {
34231143d5dSDavid Howells 		_debug("more %lx [%lx]", start, count);
34331143d5dSDavid Howells 		n = wb->last - start + 1;
34431143d5dSDavid Howells 		if (n > ARRAY_SIZE(pages))
34531143d5dSDavid Howells 			n = ARRAY_SIZE(pages);
34631143d5dSDavid Howells 		n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
34731143d5dSDavid Howells 					  start, n, pages);
34831143d5dSDavid Howells 		_debug("fgpc %u", n);
34931143d5dSDavid Howells 		if (n == 0)
35031143d5dSDavid Howells 			goto no_more;
35131143d5dSDavid Howells 		if (pages[0]->index != start) {
3529d577b6aSDavid Howells 			do {
3539d577b6aSDavid Howells 				put_page(pages[--n]);
3549d577b6aSDavid Howells 			} while (n > 0);
35531143d5dSDavid Howells 			goto no_more;
35631143d5dSDavid Howells 		}
35731143d5dSDavid Howells 
35831143d5dSDavid Howells 		for (loop = 0; loop < n; loop++) {
35931143d5dSDavid Howells 			page = pages[loop];
36031143d5dSDavid Howells 			if (page->index > wb->last)
36131143d5dSDavid Howells 				break;
362529ae9aaSNick Piggin 			if (!trylock_page(page))
36331143d5dSDavid Howells 				break;
36431143d5dSDavid Howells 			if (!PageDirty(page) ||
36531143d5dSDavid Howells 			    page_private(page) != (unsigned long) wb) {
36631143d5dSDavid Howells 				unlock_page(page);
36731143d5dSDavid Howells 				break;
36831143d5dSDavid Howells 			}
36931143d5dSDavid Howells 			if (!clear_page_dirty_for_io(page))
37031143d5dSDavid Howells 				BUG();
37131143d5dSDavid Howells 			if (test_set_page_writeback(page))
37231143d5dSDavid Howells 				BUG();
37331143d5dSDavid Howells 			unlock_page(page);
37431143d5dSDavid Howells 			put_page(page);
37531143d5dSDavid Howells 		}
37631143d5dSDavid Howells 		count += loop;
37731143d5dSDavid Howells 		if (loop < n) {
37831143d5dSDavid Howells 			for (; loop < n; loop++)
37931143d5dSDavid Howells 				put_page(pages[loop]);
38031143d5dSDavid Howells 			goto no_more;
38131143d5dSDavid Howells 		}
38231143d5dSDavid Howells 
38331143d5dSDavid Howells 		start += loop;
38431143d5dSDavid Howells 	} while (start <= wb->last && count < 65536);
38531143d5dSDavid Howells 
38631143d5dSDavid Howells no_more:
38731143d5dSDavid Howells 	/* we now have a contiguous set of dirty pages, each with writeback set
38831143d5dSDavid Howells 	 * and the dirty mark cleared; the first page is locked and must remain
38931143d5dSDavid Howells 	 * so, all the rest are unlocked */
39031143d5dSDavid Howells 	first = primary_page->index;
39131143d5dSDavid Howells 	last = first + count - 1;
39231143d5dSDavid Howells 
39331143d5dSDavid Howells 	offset = (first == wb->first) ? wb->offset_first : 0;
39431143d5dSDavid Howells 	to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
39531143d5dSDavid Howells 
39631143d5dSDavid Howells 	_debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
39731143d5dSDavid Howells 
39831143d5dSDavid Howells 	ret = afs_vnode_store_data(wb, first, last, offset, to);
39931143d5dSDavid Howells 	if (ret < 0) {
40031143d5dSDavid Howells 		switch (ret) {
40131143d5dSDavid Howells 		case -EDQUOT:
40231143d5dSDavid Howells 		case -ENOSPC:
40331143d5dSDavid Howells 			set_bit(AS_ENOSPC,
40431143d5dSDavid Howells 				&wb->vnode->vfs_inode.i_mapping->flags);
40531143d5dSDavid Howells 			break;
40631143d5dSDavid Howells 		case -EROFS:
40731143d5dSDavid Howells 		case -EIO:
40831143d5dSDavid Howells 		case -EREMOTEIO:
40931143d5dSDavid Howells 		case -EFBIG:
41031143d5dSDavid Howells 		case -ENOENT:
41131143d5dSDavid Howells 		case -ENOMEDIUM:
41231143d5dSDavid Howells 		case -ENXIO:
41331143d5dSDavid Howells 			afs_kill_pages(wb->vnode, true, first, last);
41431143d5dSDavid Howells 			set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
41531143d5dSDavid Howells 			break;
41631143d5dSDavid Howells 		case -EACCES:
41731143d5dSDavid Howells 		case -EPERM:
41831143d5dSDavid Howells 		case -ENOKEY:
41931143d5dSDavid Howells 		case -EKEYEXPIRED:
42031143d5dSDavid Howells 		case -EKEYREJECTED:
42131143d5dSDavid Howells 		case -EKEYREVOKED:
42231143d5dSDavid Howells 			afs_kill_pages(wb->vnode, false, first, last);
42331143d5dSDavid Howells 			break;
42431143d5dSDavid Howells 		default:
42531143d5dSDavid Howells 			break;
42631143d5dSDavid Howells 		}
42731143d5dSDavid Howells 	} else {
42831143d5dSDavid Howells 		ret = count;
42931143d5dSDavid Howells 	}
43031143d5dSDavid Howells 
43131143d5dSDavid Howells 	_leave(" = %d", ret);
43231143d5dSDavid Howells 	return ret;
43331143d5dSDavid Howells }
43431143d5dSDavid Howells 
43531143d5dSDavid Howells /*
43631143d5dSDavid Howells  * write a page back to the server
43731143d5dSDavid Howells  * - the caller locked the page for us
43831143d5dSDavid Howells  */
43931143d5dSDavid Howells int afs_writepage(struct page *page, struct writeback_control *wbc)
44031143d5dSDavid Howells {
44131143d5dSDavid Howells 	struct backing_dev_info *bdi = page->mapping->backing_dev_info;
44231143d5dSDavid Howells 	struct afs_writeback *wb;
44331143d5dSDavid Howells 	int ret;
44431143d5dSDavid Howells 
44531143d5dSDavid Howells 	_enter("{%lx},", page->index);
44631143d5dSDavid Howells 
44731143d5dSDavid Howells 	wb = (struct afs_writeback *) page_private(page);
44831143d5dSDavid Howells 	ASSERT(wb != NULL);
44931143d5dSDavid Howells 
45031143d5dSDavid Howells 	ret = afs_write_back_from_locked_page(wb, page);
45131143d5dSDavid Howells 	unlock_page(page);
45231143d5dSDavid Howells 	if (ret < 0) {
45331143d5dSDavid Howells 		_leave(" = %d", ret);
45431143d5dSDavid Howells 		return 0;
45531143d5dSDavid Howells 	}
45631143d5dSDavid Howells 
45731143d5dSDavid Howells 	wbc->nr_to_write -= ret;
45831143d5dSDavid Howells 	if (wbc->nonblocking && bdi_write_congested(bdi))
45931143d5dSDavid Howells 		wbc->encountered_congestion = 1;
46031143d5dSDavid Howells 
46131143d5dSDavid Howells 	_leave(" = 0");
46231143d5dSDavid Howells 	return 0;
46331143d5dSDavid Howells }
46431143d5dSDavid Howells 
46531143d5dSDavid Howells /*
46631143d5dSDavid Howells  * write a region of pages back to the server
46731143d5dSDavid Howells  */
468c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
46931143d5dSDavid Howells 				 struct writeback_control *wbc,
47031143d5dSDavid Howells 				 pgoff_t index, pgoff_t end, pgoff_t *_next)
47131143d5dSDavid Howells {
47231143d5dSDavid Howells 	struct backing_dev_info *bdi = mapping->backing_dev_info;
47331143d5dSDavid Howells 	struct afs_writeback *wb;
47431143d5dSDavid Howells 	struct page *page;
47531143d5dSDavid Howells 	int ret, n;
47631143d5dSDavid Howells 
47731143d5dSDavid Howells 	_enter(",,%lx,%lx,", index, end);
47831143d5dSDavid Howells 
47931143d5dSDavid Howells 	do {
48031143d5dSDavid Howells 		n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
48131143d5dSDavid Howells 				       1, &page);
48231143d5dSDavid Howells 		if (!n)
48331143d5dSDavid Howells 			break;
48431143d5dSDavid Howells 
48531143d5dSDavid Howells 		_debug("wback %lx", page->index);
48631143d5dSDavid Howells 
48731143d5dSDavid Howells 		if (page->index > end) {
48831143d5dSDavid Howells 			*_next = index;
48931143d5dSDavid Howells 			page_cache_release(page);
49031143d5dSDavid Howells 			_leave(" = 0 [%lx]", *_next);
49131143d5dSDavid Howells 			return 0;
49231143d5dSDavid Howells 		}
49331143d5dSDavid Howells 
49431143d5dSDavid Howells 		/* at this point we hold neither mapping->tree_lock nor lock on
49531143d5dSDavid Howells 		 * the page itself: the page may be truncated or invalidated
49631143d5dSDavid Howells 		 * (changing page->mapping to NULL), or even swizzled back from
49731143d5dSDavid Howells 		 * swapper_space to tmpfs file mapping
49831143d5dSDavid Howells 		 */
49931143d5dSDavid Howells 		lock_page(page);
50031143d5dSDavid Howells 
50131143d5dSDavid Howells 		if (page->mapping != mapping) {
50231143d5dSDavid Howells 			unlock_page(page);
50331143d5dSDavid Howells 			page_cache_release(page);
50431143d5dSDavid Howells 			continue;
50531143d5dSDavid Howells 		}
50631143d5dSDavid Howells 
50731143d5dSDavid Howells 		if (wbc->sync_mode != WB_SYNC_NONE)
50831143d5dSDavid Howells 			wait_on_page_writeback(page);
50931143d5dSDavid Howells 
51031143d5dSDavid Howells 		if (PageWriteback(page) || !PageDirty(page)) {
51131143d5dSDavid Howells 			unlock_page(page);
51231143d5dSDavid Howells 			continue;
51331143d5dSDavid Howells 		}
51431143d5dSDavid Howells 
51531143d5dSDavid Howells 		wb = (struct afs_writeback *) page_private(page);
51631143d5dSDavid Howells 		ASSERT(wb != NULL);
51731143d5dSDavid Howells 
51831143d5dSDavid Howells 		spin_lock(&wb->vnode->writeback_lock);
51931143d5dSDavid Howells 		wb->state = AFS_WBACK_WRITING;
52031143d5dSDavid Howells 		spin_unlock(&wb->vnode->writeback_lock);
52131143d5dSDavid Howells 
52231143d5dSDavid Howells 		ret = afs_write_back_from_locked_page(wb, page);
52331143d5dSDavid Howells 		unlock_page(page);
52431143d5dSDavid Howells 		page_cache_release(page);
52531143d5dSDavid Howells 		if (ret < 0) {
52631143d5dSDavid Howells 			_leave(" = %d", ret);
52731143d5dSDavid Howells 			return ret;
52831143d5dSDavid Howells 		}
52931143d5dSDavid Howells 
53031143d5dSDavid Howells 		wbc->nr_to_write -= ret;
53131143d5dSDavid Howells 
53231143d5dSDavid Howells 		if (wbc->nonblocking && bdi_write_congested(bdi)) {
53331143d5dSDavid Howells 			wbc->encountered_congestion = 1;
53431143d5dSDavid Howells 			break;
53531143d5dSDavid Howells 		}
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 	struct backing_dev_info *bdi = mapping->backing_dev_info;
55231143d5dSDavid Howells 	pgoff_t start, end, next;
55331143d5dSDavid Howells 	int ret;
55431143d5dSDavid Howells 
55531143d5dSDavid Howells 	_enter("");
55631143d5dSDavid Howells 
55731143d5dSDavid Howells 	if (wbc->nonblocking && bdi_write_congested(bdi)) {
55831143d5dSDavid Howells 		wbc->encountered_congestion = 1;
55931143d5dSDavid Howells 		_leave(" = 0 [congest]");
56031143d5dSDavid Howells 		return 0;
56131143d5dSDavid Howells 	}
56231143d5dSDavid Howells 
56331143d5dSDavid Howells 	if (wbc->range_cyclic) {
56431143d5dSDavid Howells 		start = mapping->writeback_index;
56531143d5dSDavid Howells 		end = -1;
56631143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
56731143d5dSDavid Howells 		if (start > 0 && wbc->nr_to_write > 0 && ret == 0 &&
56831143d5dSDavid Howells 		    !(wbc->nonblocking && wbc->encountered_congestion))
56931143d5dSDavid Howells 			ret = afs_writepages_region(mapping, wbc, 0, start,
57031143d5dSDavid Howells 						    &next);
57131143d5dSDavid Howells 		mapping->writeback_index = next;
57231143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
57331143d5dSDavid Howells 		end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
57431143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, end, &next);
57531143d5dSDavid Howells 		if (wbc->nr_to_write > 0)
57631143d5dSDavid Howells 			mapping->writeback_index = next;
57731143d5dSDavid Howells 	} else {
57831143d5dSDavid Howells 		start = wbc->range_start >> PAGE_CACHE_SHIFT;
57931143d5dSDavid Howells 		end = wbc->range_end >> PAGE_CACHE_SHIFT;
58031143d5dSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, end, &next);
58131143d5dSDavid Howells 	}
58231143d5dSDavid Howells 
58331143d5dSDavid Howells 	_leave(" = %d", ret);
58431143d5dSDavid Howells 	return ret;
58531143d5dSDavid Howells }
58631143d5dSDavid Howells 
58731143d5dSDavid Howells /*
58831143d5dSDavid Howells  * write an inode back
58931143d5dSDavid Howells  */
59031143d5dSDavid Howells int afs_write_inode(struct inode *inode, int sync)
59131143d5dSDavid Howells {
59231143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
59331143d5dSDavid Howells 	int ret;
59431143d5dSDavid Howells 
59531143d5dSDavid Howells 	_enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
59631143d5dSDavid Howells 
59731143d5dSDavid Howells 	ret = 0;
59831143d5dSDavid Howells 	if (sync) {
59931143d5dSDavid Howells 		ret = filemap_fdatawait(inode->i_mapping);
60031143d5dSDavid Howells 		if (ret < 0)
60131143d5dSDavid Howells 			__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
60231143d5dSDavid Howells 	}
60331143d5dSDavid Howells 
60431143d5dSDavid Howells 	_leave(" = %d", ret);
60531143d5dSDavid Howells 	return ret;
60631143d5dSDavid Howells }
60731143d5dSDavid Howells 
60831143d5dSDavid Howells /*
60931143d5dSDavid Howells  * completion of write to server
61031143d5dSDavid Howells  */
61131143d5dSDavid Howells void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
61231143d5dSDavid Howells {
61331143d5dSDavid Howells 	struct afs_writeback *wb = call->wb;
61431143d5dSDavid Howells 	struct pagevec pv;
61531143d5dSDavid Howells 	unsigned count, loop;
61631143d5dSDavid Howells 	pgoff_t first = call->first, last = call->last;
61731143d5dSDavid Howells 	bool free_wb;
61831143d5dSDavid Howells 
61931143d5dSDavid Howells 	_enter("{%x:%u},{%lx-%lx}",
62031143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, first, last);
62131143d5dSDavid Howells 
62231143d5dSDavid Howells 	ASSERT(wb != NULL);
62331143d5dSDavid Howells 
62431143d5dSDavid Howells 	pagevec_init(&pv, 0);
62531143d5dSDavid Howells 
62631143d5dSDavid Howells 	do {
6275bbf5d39SDavid Howells 		_debug("done %lx-%lx", first, last);
62831143d5dSDavid Howells 
62931143d5dSDavid Howells 		count = last - first + 1;
63031143d5dSDavid Howells 		if (count > PAGEVEC_SIZE)
63131143d5dSDavid Howells 			count = PAGEVEC_SIZE;
63231143d5dSDavid Howells 		pv.nr = find_get_pages_contig(call->mapping, first, count,
63331143d5dSDavid Howells 					      pv.pages);
63431143d5dSDavid Howells 		ASSERTCMP(pv.nr, ==, count);
63531143d5dSDavid Howells 
63631143d5dSDavid Howells 		spin_lock(&vnode->writeback_lock);
63731143d5dSDavid Howells 		for (loop = 0; loop < count; loop++) {
63831143d5dSDavid Howells 			struct page *page = pv.pages[loop];
63931143d5dSDavid Howells 			end_page_writeback(page);
64031143d5dSDavid Howells 			if (page_private(page) == (unsigned long) wb) {
64131143d5dSDavid Howells 				set_page_private(page, 0);
64231143d5dSDavid Howells 				ClearPagePrivate(page);
64331143d5dSDavid Howells 				wb->usage--;
64431143d5dSDavid Howells 			}
64531143d5dSDavid Howells 		}
64631143d5dSDavid Howells 		free_wb = false;
64731143d5dSDavid Howells 		if (wb->usage == 0) {
64831143d5dSDavid Howells 			afs_unlink_writeback(wb);
64931143d5dSDavid Howells 			free_wb = true;
65031143d5dSDavid Howells 		}
65131143d5dSDavid Howells 		spin_unlock(&vnode->writeback_lock);
65231143d5dSDavid Howells 		first += count;
65331143d5dSDavid Howells 		if (free_wb) {
65431143d5dSDavid Howells 			afs_free_writeback(wb);
65531143d5dSDavid Howells 			wb = NULL;
65631143d5dSDavid Howells 		}
65731143d5dSDavid Howells 
65831143d5dSDavid Howells 		__pagevec_release(&pv);
6595bbf5d39SDavid Howells 	} while (first <= last);
66031143d5dSDavid Howells 
66131143d5dSDavid Howells 	_leave("");
66231143d5dSDavid Howells }
66331143d5dSDavid Howells 
66431143d5dSDavid Howells /*
66531143d5dSDavid Howells  * write to an AFS file
66631143d5dSDavid Howells  */
66731143d5dSDavid Howells ssize_t afs_file_write(struct kiocb *iocb, const struct iovec *iov,
66831143d5dSDavid Howells 		       unsigned long nr_segs, loff_t pos)
66931143d5dSDavid Howells {
67031143d5dSDavid Howells 	struct dentry *dentry = iocb->ki_filp->f_path.dentry;
67131143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
67231143d5dSDavid Howells 	ssize_t result;
67331143d5dSDavid Howells 	size_t count = iov_length(iov, nr_segs);
67431143d5dSDavid Howells 	int ret;
67531143d5dSDavid Howells 
67631143d5dSDavid Howells 	_enter("{%x.%u},{%zu},%lu,",
67731143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, count, nr_segs);
67831143d5dSDavid Howells 
67931143d5dSDavid Howells 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
68031143d5dSDavid Howells 		printk(KERN_INFO
68131143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
68231143d5dSDavid Howells 		return -EBUSY;
68331143d5dSDavid Howells 	}
68431143d5dSDavid Howells 
68531143d5dSDavid Howells 	if (!count)
68631143d5dSDavid Howells 		return 0;
68731143d5dSDavid Howells 
68831143d5dSDavid Howells 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
68931143d5dSDavid Howells 	if (IS_ERR_VALUE(result)) {
69031143d5dSDavid Howells 		_leave(" = %zd", result);
69131143d5dSDavid Howells 		return result;
69231143d5dSDavid Howells 	}
69331143d5dSDavid Howells 
69431143d5dSDavid Howells 	/* return error values for O_SYNC and IS_SYNC() */
69531143d5dSDavid Howells 	if (IS_SYNC(&vnode->vfs_inode) || iocb->ki_filp->f_flags & O_SYNC) {
69631143d5dSDavid Howells 		ret = afs_fsync(iocb->ki_filp, dentry, 1);
69731143d5dSDavid Howells 		if (ret < 0)
69831143d5dSDavid Howells 			result = ret;
69931143d5dSDavid Howells 	}
70031143d5dSDavid Howells 
70131143d5dSDavid Howells 	_leave(" = %zd", result);
70231143d5dSDavid Howells 	return result;
70331143d5dSDavid Howells }
70431143d5dSDavid Howells 
70531143d5dSDavid Howells /*
70631143d5dSDavid Howells  * flush the vnode to the fileserver
70731143d5dSDavid Howells  */
70831143d5dSDavid Howells int afs_writeback_all(struct afs_vnode *vnode)
70931143d5dSDavid Howells {
71031143d5dSDavid Howells 	struct address_space *mapping = vnode->vfs_inode.i_mapping;
71131143d5dSDavid Howells 	struct writeback_control wbc = {
71231143d5dSDavid Howells 		.bdi		= mapping->backing_dev_info,
71331143d5dSDavid Howells 		.sync_mode	= WB_SYNC_ALL,
71431143d5dSDavid Howells 		.nr_to_write	= LONG_MAX,
71531143d5dSDavid Howells 		.for_writepages = 1,
71631143d5dSDavid Howells 		.range_cyclic	= 1,
71731143d5dSDavid Howells 	};
71831143d5dSDavid Howells 	int ret;
71931143d5dSDavid Howells 
72031143d5dSDavid Howells 	_enter("");
72131143d5dSDavid Howells 
72231143d5dSDavid Howells 	ret = mapping->a_ops->writepages(mapping, &wbc);
72331143d5dSDavid Howells 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
72431143d5dSDavid Howells 
72531143d5dSDavid Howells 	_leave(" = %d", ret);
72631143d5dSDavid Howells 	return ret;
72731143d5dSDavid Howells }
72831143d5dSDavid Howells 
72931143d5dSDavid Howells /*
73031143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
73131143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
73231143d5dSDavid Howells  *   whether any write errors occurred for this process.
73331143d5dSDavid Howells  */
73431143d5dSDavid Howells int afs_fsync(struct file *file, struct dentry *dentry, int datasync)
73531143d5dSDavid Howells {
73631143d5dSDavid Howells 	struct afs_writeback *wb, *xwb;
73731143d5dSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
73831143d5dSDavid Howells 	int ret;
73931143d5dSDavid Howells 
74031143d5dSDavid Howells 	_enter("{%x:%u},{n=%s},%d",
74131143d5dSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
74231143d5dSDavid Howells 	       datasync);
74331143d5dSDavid Howells 
74431143d5dSDavid Howells 	/* use a writeback record as a marker in the queue - when this reaches
74531143d5dSDavid Howells 	 * the front of the queue, all the outstanding writes are either
74631143d5dSDavid Howells 	 * completed or rejected */
74731143d5dSDavid Howells 	wb = kzalloc(sizeof(*wb), GFP_KERNEL);
74831143d5dSDavid Howells 	if (!wb)
74931143d5dSDavid Howells 		return -ENOMEM;
75031143d5dSDavid Howells 	wb->vnode = vnode;
75131143d5dSDavid Howells 	wb->first = 0;
75231143d5dSDavid Howells 	wb->last = -1;
75331143d5dSDavid Howells 	wb->offset_first = 0;
75431143d5dSDavid Howells 	wb->to_last = PAGE_SIZE;
75531143d5dSDavid Howells 	wb->usage = 1;
75631143d5dSDavid Howells 	wb->state = AFS_WBACK_SYNCING;
75731143d5dSDavid Howells 	init_waitqueue_head(&wb->waitq);
75831143d5dSDavid Howells 
75931143d5dSDavid Howells 	spin_lock(&vnode->writeback_lock);
76031143d5dSDavid Howells 	list_for_each_entry(xwb, &vnode->writebacks, link) {
76131143d5dSDavid Howells 		if (xwb->state == AFS_WBACK_PENDING)
76231143d5dSDavid Howells 			xwb->state = AFS_WBACK_CONFLICTING;
76331143d5dSDavid Howells 	}
76431143d5dSDavid Howells 	list_add_tail(&wb->link, &vnode->writebacks);
76531143d5dSDavid Howells 	spin_unlock(&vnode->writeback_lock);
76631143d5dSDavid Howells 
76731143d5dSDavid Howells 	/* push all the outstanding writebacks to the server */
76831143d5dSDavid Howells 	ret = afs_writeback_all(vnode);
76931143d5dSDavid Howells 	if (ret < 0) {
77031143d5dSDavid Howells 		afs_put_writeback(wb);
77131143d5dSDavid Howells 		_leave(" = %d [wb]", ret);
77231143d5dSDavid Howells 		return ret;
77331143d5dSDavid Howells 	}
77431143d5dSDavid Howells 
77531143d5dSDavid Howells 	/* wait for the preceding writes to actually complete */
77631143d5dSDavid Howells 	ret = wait_event_interruptible(wb->waitq,
77731143d5dSDavid Howells 				       wb->state == AFS_WBACK_COMPLETE ||
77831143d5dSDavid Howells 				       vnode->writebacks.next == &wb->link);
77931143d5dSDavid Howells 	afs_put_writeback(wb);
78031143d5dSDavid Howells 	_leave(" = %d", ret);
78131143d5dSDavid Howells 	return ret;
78231143d5dSDavid Howells }
783