xref: /openbmc/linux/fs/nfs/direct.c (revision d72b7a6b26b9009b7a05117fe2e04b3a73ae4a5c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * There are important applications whose performance or correctness
91da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
101da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
111da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
121da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
131da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
141da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
151da177e4SLinus Torvalds  * need to cache the contents of a file.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
181da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
191da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
201da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
211da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
221da177e4SLinus Torvalds  * an application.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
251da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
261da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
271da177e4SLinus Torvalds  * an undocumented mount option.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
301da177e4SLinus Torvalds  * help from Andrew Morton.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
331da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
341da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
351da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
361da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3788467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/config.h>
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/smp_lock.h>
461da177e4SLinus Torvalds #include <linux/file.h>
471da177e4SLinus Torvalds #include <linux/pagemap.h>
481da177e4SLinus Torvalds #include <linux/kref.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <linux/nfs_fs.h>
511da177e4SLinus Torvalds #include <linux/nfs_page.h>
521da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <asm/system.h>
551da177e4SLinus Torvalds #include <asm/uaccess.h>
561da177e4SLinus Torvalds #include <asm/atomic.h>
571da177e4SLinus Torvalds 
5891d5b470SChuck Lever #include "iostat.h"
5991d5b470SChuck Lever 
601da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds struct nfs_direct_req {
681da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
6915ce4a0cSChuck Lever 
7015ce4a0cSChuck Lever 	/* I/O parameters */
71fad61490STrond Myklebust 	struct list_head	list,		/* nfs_read/write_data structs */
72fad61490STrond Myklebust 				rewrite_list;	/* saved nfs_write_data structs */
73a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
7499514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7588467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
76fad61490STrond Myklebust 	unsigned long		user_addr;	/* location of user's buffer */
77fad61490STrond Myklebust 	size_t			user_count;	/* total bytes to move */
78fad61490STrond Myklebust 	loff_t			pos;		/* starting offset in file */
791da177e4SLinus Torvalds 	struct page **		pages;		/* pages in our buffer */
801da177e4SLinus Torvalds 	unsigned int		npages;		/* count of pages */
8115ce4a0cSChuck Lever 
8215ce4a0cSChuck Lever 	/* completion state */
8315ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8415ce4a0cSChuck Lever 	int			outstanding;	/* i/os we're waiting for */
8515ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
861da177e4SLinus Torvalds 				error;		/* any reported error */
87*d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
88fad61490STrond Myklebust 
89fad61490STrond Myklebust 	/* commit state */
90fad61490STrond Myklebust 	struct nfs_write_data *	commit_data;	/* special write_data for commits */
91fad61490STrond Myklebust 	int			flags;
92fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
93fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
94fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
97fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync);
98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
99fad61490STrond Myklebust 
1001da177e4SLinus Torvalds /**
101b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
102b8a32e2bSChuck Lever  * @rw: direction (read or write)
103b8a32e2bSChuck Lever  * @iocb: target I/O control block
104b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
105b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
106b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
107b8a32e2bSChuck Lever  *
108b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
109b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
110b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
111b8a32e2bSChuck Lever  * should never be called.
112b8a32e2bSChuck Lever  */
113b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
114b8a32e2bSChuck Lever {
115b8a32e2bSChuck Lever 	struct dentry *dentry = iocb->ki_filp->f_dentry;
116b8a32e2bSChuck Lever 
117b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
118b8a32e2bSChuck Lever 			dentry->d_name.name, (long long) pos, nr_segs);
119b8a32e2bSChuck Lever 
120b8a32e2bSChuck Lever 	return -EINVAL;
121b8a32e2bSChuck Lever }
122b8a32e2bSChuck Lever 
1236b45d858STrond Myklebust static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1246b45d858STrond Myklebust {
1256b45d858STrond Myklebust 	int i;
1266b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1276b45d858STrond Myklebust 		struct page *page = pages[i];
1286b45d858STrond Myklebust 		if (do_dirty && !PageCompound(page))
1296b45d858STrond Myklebust 			set_page_dirty_lock(page);
1306b45d858STrond Myklebust 		page_cache_release(page);
1316b45d858STrond Myklebust 	}
1326b45d858STrond Myklebust 	kfree(pages);
1336b45d858STrond Myklebust }
1346b45d858STrond Myklebust 
135d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	int result = -ENOMEM;
1381da177e4SLinus Torvalds 	unsigned long page_count;
1391da177e4SLinus Torvalds 	size_t array_size;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1421da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1451da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1461da177e4SLinus Torvalds 	if (*pages) {
1471da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1481da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1491da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1501da177e4SLinus Torvalds 					*pages, NULL);
1511da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
1526b45d858STrond Myklebust 		if (result != page_count) {
153143f412eSTrond Myklebust 			/*
1546b45d858STrond Myklebust 			 * If we got fewer pages than expected from
1556b45d858STrond Myklebust 			 * get_user_pages(), the user buffer runs off the
1566b45d858STrond Myklebust 			 * end of a mapping; return EFAULT.
157143f412eSTrond Myklebust 			 */
1586b45d858STrond Myklebust 			if (result >= 0) {
159143f412eSTrond Myklebust 				nfs_free_user_pages(*pages, result, 0);
160143f412eSTrond Myklebust 				result = -EFAULT;
1616b45d858STrond Myklebust 			} else
1626b45d858STrond Myklebust 				kfree(*pages);
1636b45d858STrond Myklebust 			*pages = NULL;
164143f412eSTrond Myklebust 		}
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 	return result;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
16993619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
17093619e59SChuck Lever {
17193619e59SChuck Lever 	struct nfs_direct_req *dreq;
17293619e59SChuck Lever 
17393619e59SChuck Lever 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
17493619e59SChuck Lever 	if (!dreq)
17593619e59SChuck Lever 		return NULL;
17693619e59SChuck Lever 
17793619e59SChuck Lever 	kref_init(&dreq->kref);
178*d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
17993619e59SChuck Lever 	INIT_LIST_HEAD(&dreq->list);
180fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
18193619e59SChuck Lever 	dreq->iocb = NULL;
182a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
18315ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
18415ce4a0cSChuck Lever 	dreq->outstanding = 0;
18515ce4a0cSChuck Lever 	dreq->count = 0;
18615ce4a0cSChuck Lever 	dreq->error = 0;
187fad61490STrond Myklebust 	dreq->flags = 0;
18893619e59SChuck Lever 
18993619e59SChuck Lever 	return dreq;
19093619e59SChuck Lever }
19193619e59SChuck Lever 
1921da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
195a8881f5aSTrond Myklebust 
196a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
197a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1981da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
201d4cc948bSChuck Lever /*
202bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
203bc0fb201SChuck Lever  */
204bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
205bc0fb201SChuck Lever {
20615ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
207bc0fb201SChuck Lever 
208bc0fb201SChuck Lever 	/* Async requests don't wait here */
209bc0fb201SChuck Lever 	if (dreq->iocb)
210bc0fb201SChuck Lever 		goto out;
211bc0fb201SChuck Lever 
212*d72b7a6bSTrond Myklebust 	result = wait_for_completion_interruptible(&dreq->completion);
213bc0fb201SChuck Lever 
214bc0fb201SChuck Lever 	if (!result)
21515ce4a0cSChuck Lever 		result = dreq->error;
216bc0fb201SChuck Lever 	if (!result)
21715ce4a0cSChuck Lever 		result = dreq->count;
218bc0fb201SChuck Lever 
219bc0fb201SChuck Lever out:
220bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
221bc0fb201SChuck Lever 	return (ssize_t) result;
222bc0fb201SChuck Lever }
223bc0fb201SChuck Lever 
224bc0fb201SChuck Lever /*
22563ab46abSChuck Lever  * We must hold a reference to all the pages in this direct read request
22663ab46abSChuck Lever  * until the RPCs complete.  This could be long *after* we are woken up in
22763ab46abSChuck Lever  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
22863ab46abSChuck Lever  *
22963ab46abSChuck Lever  * In addition, synchronous I/O uses a stack-allocated iocb.  Thus we
23063ab46abSChuck Lever  * can't trust the iocb is still valid here if this is a synchronous
23163ab46abSChuck Lever  * request.  If the waiter is woken prematurely, the iocb is long gone.
23263ab46abSChuck Lever  */
23363ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
23463ab46abSChuck Lever {
23563ab46abSChuck Lever 	nfs_free_user_pages(dreq->pages, dreq->npages, 1);
23663ab46abSChuck Lever 
23763ab46abSChuck Lever 	if (dreq->iocb) {
23815ce4a0cSChuck Lever 		long res = (long) dreq->error;
23963ab46abSChuck Lever 		if (!res)
24015ce4a0cSChuck Lever 			res = (long) dreq->count;
24163ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
242*d72b7a6bSTrond Myklebust 	}
243*d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
24463ab46abSChuck Lever 
24563ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
24663ab46abSChuck Lever }
24763ab46abSChuck Lever 
24863ab46abSChuck Lever /*
2491da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
2501da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
2511da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
2521da177e4SLinus Torvalds  */
2535dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct list_head *list;
2561da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
25740859d7eSChuck Lever 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2581da177e4SLinus Torvalds 
25993619e59SChuck Lever 	dreq = nfs_direct_req_alloc();
2601da177e4SLinus Torvalds 	if (!dreq)
2611da177e4SLinus Torvalds 		return NULL;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	list = &dreq->list;
2641da177e4SLinus Torvalds 	for(;;) {
26540859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		if (unlikely(!data)) {
2681da177e4SLinus Torvalds 			while (!list_empty(list)) {
2691da177e4SLinus Torvalds 				data = list_entry(list->next,
2701da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
2711da177e4SLinus Torvalds 				list_del(&data->pages);
2721da177e4SLinus Torvalds 				nfs_readdata_free(data);
2731da177e4SLinus Torvalds 			}
2741da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
2751da177e4SLinus Torvalds 			return NULL;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2791da177e4SLinus Torvalds 		list_add(&data->pages, list);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
28215ce4a0cSChuck Lever 		dreq->outstanding++;
2831da177e4SLinus Torvalds 		if (nbytes <= rsize)
2841da177e4SLinus Torvalds 			break;
2851da177e4SLinus Torvalds 		nbytes -= rsize;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	kref_get(&dreq->kref);
2881da177e4SLinus Torvalds 	return dreq;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
291ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2921da177e4SLinus Torvalds {
293ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2941da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
2951da177e4SLinus Torvalds 
296ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
297ec06c096STrond Myklebust 		return;
2981da177e4SLinus Torvalds 
29915ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
30015ce4a0cSChuck Lever 
30115ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
30215ce4a0cSChuck Lever 		dreq->count += data->res.count;
30315ce4a0cSChuck Lever 	else
30415ce4a0cSChuck Lever 		dreq->error = task->tk_status;
30515ce4a0cSChuck Lever 
30615ce4a0cSChuck Lever 	if (--dreq->outstanding) {
30715ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
30815ce4a0cSChuck Lever 		return;
30915ce4a0cSChuck Lever 	}
31015ce4a0cSChuck Lever 
31115ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
31263ab46abSChuck Lever 	nfs_direct_complete(dreq);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
315ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
316ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
317ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
318ec06c096STrond Myklebust };
319ec06c096STrond Myklebust 
320d4cc948bSChuck Lever /*
3211da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
3221da177e4SLinus Torvalds  * an NFS READ operation
3231da177e4SLinus Torvalds  */
324fad61490STrond Myklebust static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
3251da177e4SLinus Torvalds {
326a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
327a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
3281da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
3291da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
330fad61490STrond Myklebust 	size_t count = dreq->user_count;
331fad61490STrond Myklebust 	loff_t pos = dreq->pos;
3325dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
3331da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	curpage = 0;
336fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
3371da177e4SLinus Torvalds 	do {
3381da177e4SLinus Torvalds 		struct nfs_read_data *data;
3395dd602f2SChuck Lever 		size_t bytes;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		bytes = rsize;
3421da177e4SLinus Torvalds 		if (count < rsize)
3431da177e4SLinus Torvalds 			bytes = count;
3441da177e4SLinus Torvalds 
3455db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
3461da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
3471da177e4SLinus Torvalds 		list_del_init(&data->pages);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		data->inode = inode;
3501da177e4SLinus Torvalds 		data->cred = ctx->cred;
3511da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3521da177e4SLinus Torvalds 		data->args.context = ctx;
35388467055SChuck Lever 		data->args.offset = pos;
3541da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
3551da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
3561da177e4SLinus Torvalds 		data->args.count = bytes;
3571da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3581da177e4SLinus Torvalds 		data->res.eof = 0;
3591da177e4SLinus Torvalds 		data->res.count = bytes;
3601da177e4SLinus Torvalds 
361ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
362ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3631da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		lock_kernel();
3681da177e4SLinus Torvalds 		rpc_execute(&data->task);
3691da177e4SLinus Torvalds 		unlock_kernel();
3701da177e4SLinus Torvalds 
371606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
3721da177e4SLinus Torvalds 				data->task.tk_pid,
3731da177e4SLinus Torvalds 				inode->i_sb->s_id,
3741da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3751da177e4SLinus Torvalds 				bytes,
3761da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3771da177e4SLinus Torvalds 
37888467055SChuck Lever 		pos += bytes;
3791da177e4SLinus Torvalds 		pgbase += bytes;
3801da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
3811da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		count -= bytes;
3841da177e4SLinus Torvalds 	} while (count != 0);
3855db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
38888467055SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	ssize_t result;
3911da177e4SLinus Torvalds 	sigset_t oldset;
39299514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3931da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
3941da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
3971da177e4SLinus Torvalds 	if (!dreq)
3981da177e4SLinus Torvalds 		return -ENOMEM;
3991da177e4SLinus Torvalds 
400fad61490STrond Myklebust 	dreq->user_addr = user_addr;
401fad61490STrond Myklebust 	dreq->user_count = count;
402fad61490STrond Myklebust 	dreq->pos = pos;
4031da177e4SLinus Torvalds 	dreq->pages = pages;
4041da177e4SLinus Torvalds 	dreq->npages = nr_pages;
40591d5b470SChuck Lever 	dreq->inode = inode;
406a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
407487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
408487b8372SChuck Lever 		dreq->iocb = iocb;
4091da177e4SLinus Torvalds 
41091d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
4111da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
412fad61490STrond Myklebust 	nfs_direct_read_schedule(dreq);
413bc0fb201SChuck Lever 	result = nfs_direct_wait(dreq);
4141da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	return result;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
419fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
420fad61490STrond Myklebust {
421fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
422fad61490STrond Myklebust 	while (!list_empty(&dreq->list)) {
423fad61490STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
424fad61490STrond Myklebust 		list_del(&data->pages);
425fad61490STrond Myklebust 		nfs_writedata_release(data);
426fad61490STrond Myklebust 	}
427fad61490STrond Myklebust }
428fad61490STrond Myklebust 
429fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
430fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
431fad61490STrond Myklebust {
432fad61490STrond Myklebust 	struct list_head *pos;
433fad61490STrond Myklebust 
434fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
435fad61490STrond Myklebust 	list_for_each(pos, &dreq->list)
436fad61490STrond Myklebust 		dreq->outstanding++;
437fad61490STrond Myklebust 	dreq->count = 0;
438fad61490STrond Myklebust 
439fad61490STrond Myklebust 	nfs_direct_write_schedule(dreq, FLUSH_STABLE);
440fad61490STrond Myklebust }
441fad61490STrond Myklebust 
442fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
443fad61490STrond Myklebust {
444fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
445fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
446fad61490STrond Myklebust 
447fad61490STrond Myklebust 	/* Call the NFS version-specific code */
448fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
449fad61490STrond Myklebust 		return;
450fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
451fad61490STrond Myklebust 		dreq->error = task->tk_status;
452fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
453fad61490STrond Myklebust 	}
454fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
455fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
456fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
457fad61490STrond Myklebust 	}
458fad61490STrond Myklebust 
459fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
460fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
461fad61490STrond Myklebust }
462fad61490STrond Myklebust 
463fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
464fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
465fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
466fad61490STrond Myklebust };
467fad61490STrond Myklebust 
468fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
469fad61490STrond Myklebust {
470fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
471fad61490STrond Myklebust 	struct rpc_task *task = &data->task;
472fad61490STrond Myklebust 
473fad61490STrond Myklebust 	data->inode = dreq->inode;
474a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
475fad61490STrond Myklebust 
476fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
477fad61490STrond Myklebust 	data->args.offset = dreq->pos;
478fad61490STrond Myklebust 	data->args.count = dreq->user_count;
479fad61490STrond Myklebust 	data->res.count = 0;
480fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
481fad61490STrond Myklebust 	data->res.verf = &data->verf;
482fad61490STrond Myklebust 
483fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
484fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
485fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
486fad61490STrond Myklebust 
487fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
488fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
489fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
490fad61490STrond Myklebust 	dreq->commit_data = NULL;
491fad61490STrond Myklebust 
492fad61490STrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", task->tk_pid);
493fad61490STrond Myklebust 
494fad61490STrond Myklebust 	lock_kernel();
495fad61490STrond Myklebust 	rpc_execute(&data->task);
496fad61490STrond Myklebust 	unlock_kernel();
497fad61490STrond Myklebust }
498fad61490STrond Myklebust 
499fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
500fad61490STrond Myklebust {
501fad61490STrond Myklebust 	int flags = dreq->flags;
502fad61490STrond Myklebust 
503fad61490STrond Myklebust 	dreq->flags = 0;
504fad61490STrond Myklebust 	switch (flags) {
505fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
506fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
507fad61490STrond Myklebust 			break;
508fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
509fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
510fad61490STrond Myklebust 			break;
511fad61490STrond Myklebust 		default:
512fad61490STrond Myklebust 			nfs_end_data_update(inode);
513fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
514fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
515fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
516fad61490STrond Myklebust 			nfs_direct_complete(dreq);
517fad61490STrond Myklebust 	}
518fad61490STrond Myklebust }
519fad61490STrond Myklebust 
520fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
521fad61490STrond Myklebust {
522fad61490STrond Myklebust 	dreq->commit_data = nfs_commit_alloc(0);
523fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
524fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
525fad61490STrond Myklebust }
526fad61490STrond Myklebust #else
527fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
528fad61490STrond Myklebust {
529fad61490STrond Myklebust 	dreq->commit_data = NULL;
530fad61490STrond Myklebust }
531fad61490STrond Myklebust 
532fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
533fad61490STrond Myklebust {
534fad61490STrond Myklebust 	nfs_end_data_update(inode);
535fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
536fad61490STrond Myklebust 	nfs_direct_complete(dreq);
537fad61490STrond Myklebust }
538fad61490STrond Myklebust #endif
539fad61490STrond Myklebust 
540462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
5411da177e4SLinus Torvalds {
542462d5b32SChuck Lever 	struct list_head *list;
543462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
544462d5b32SChuck Lever 	unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5451da177e4SLinus Torvalds 
546462d5b32SChuck Lever 	dreq = nfs_direct_req_alloc();
547462d5b32SChuck Lever 	if (!dreq)
548462d5b32SChuck Lever 		return NULL;
5491da177e4SLinus Torvalds 
550462d5b32SChuck Lever 	list = &dreq->list;
551462d5b32SChuck Lever 	for(;;) {
552462d5b32SChuck Lever 		struct nfs_write_data *data = nfs_writedata_alloc(wpages);
5531da177e4SLinus Torvalds 
554462d5b32SChuck Lever 		if (unlikely(!data)) {
555462d5b32SChuck Lever 			while (!list_empty(list)) {
556462d5b32SChuck Lever 				data = list_entry(list->next,
557462d5b32SChuck Lever 						  struct nfs_write_data, pages);
558462d5b32SChuck Lever 				list_del(&data->pages);
559462d5b32SChuck Lever 				nfs_writedata_free(data);
560462d5b32SChuck Lever 			}
561462d5b32SChuck Lever 			kref_put(&dreq->kref, nfs_direct_req_release);
562462d5b32SChuck Lever 			return NULL;
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 
565462d5b32SChuck Lever 		INIT_LIST_HEAD(&data->pages);
566462d5b32SChuck Lever 		list_add(&data->pages, list);
5671da177e4SLinus Torvalds 
568462d5b32SChuck Lever 		data->req = (struct nfs_page *) dreq;
56915ce4a0cSChuck Lever 		dreq->outstanding++;
570462d5b32SChuck Lever 		if (nbytes <= wsize)
5711da177e4SLinus Torvalds 			break;
572462d5b32SChuck Lever 		nbytes -= wsize;
573462d5b32SChuck Lever 	}
574fad61490STrond Myklebust 
575fad61490STrond Myklebust 	nfs_alloc_commit_data(dreq);
576fad61490STrond Myklebust 
577462d5b32SChuck Lever 	kref_get(&dreq->kref);
578462d5b32SChuck Lever 	return dreq;
579462d5b32SChuck Lever }
5801da177e4SLinus Torvalds 
581462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
582462d5b32SChuck Lever {
583462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
584462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
585462d5b32SChuck Lever 	int status = task->tk_status;
586462d5b32SChuck Lever 
587462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
588462d5b32SChuck Lever 		return;
589462d5b32SChuck Lever 
59015ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
591462d5b32SChuck Lever 
59215ce4a0cSChuck Lever 	if (likely(status >= 0))
59315ce4a0cSChuck Lever 		dreq->count += data->res.count;
59415ce4a0cSChuck Lever 	else
595fad61490STrond Myklebust 		dreq->error = task->tk_status;
59615ce4a0cSChuck Lever 
597fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
598fad61490STrond Myklebust 		switch (dreq->flags) {
599fad61490STrond Myklebust 			case 0:
600fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
601fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
602fad61490STrond Myklebust 				break;
603fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
604fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
605fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
606fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
607fad61490STrond Myklebust 				}
608fad61490STrond Myklebust 		}
609fad61490STrond Myklebust 	}
610fad61490STrond Myklebust 	/* In case we have to resend */
611fad61490STrond Myklebust 	data->args.stable = NFS_FILE_SYNC;
612fad61490STrond Myklebust 
613fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
614fad61490STrond Myklebust }
615fad61490STrond Myklebust 
616fad61490STrond Myklebust /*
617fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
618fad61490STrond Myklebust  *     errors after the first one are ignored.
619fad61490STrond Myklebust  */
620fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
621fad61490STrond Myklebust {
622fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
623fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
624fad61490STrond Myklebust 
625fad61490STrond Myklebust 	spin_lock(&dreq->lock);
62615ce4a0cSChuck Lever 	if (--dreq->outstanding) {
62715ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
62815ce4a0cSChuck Lever 		return;
62915ce4a0cSChuck Lever 	}
63015ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
63115ce4a0cSChuck Lever 
632fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
633462d5b32SChuck Lever }
634462d5b32SChuck Lever 
635462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
636462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
637fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
638462d5b32SChuck Lever };
639462d5b32SChuck Lever 
640462d5b32SChuck Lever /*
641462d5b32SChuck Lever  * For each nfs_write_data struct that was allocated on the list, dispatch
642462d5b32SChuck Lever  * an NFS WRITE operation
643462d5b32SChuck Lever  */
644fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
645462d5b32SChuck Lever {
646a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
647a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
648462d5b32SChuck Lever 	struct list_head *list = &dreq->list;
649462d5b32SChuck Lever 	struct page **pages = dreq->pages;
650fad61490STrond Myklebust 	size_t count = dreq->user_count;
651fad61490STrond Myklebust 	loff_t pos = dreq->pos;
652462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
653462d5b32SChuck Lever 	unsigned int curpage, pgbase;
654462d5b32SChuck Lever 
655462d5b32SChuck Lever 	curpage = 0;
656fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
657462d5b32SChuck Lever 	do {
658462d5b32SChuck Lever 		struct nfs_write_data *data;
659462d5b32SChuck Lever 		size_t bytes;
660462d5b32SChuck Lever 
661462d5b32SChuck Lever 		bytes = wsize;
662462d5b32SChuck Lever 		if (count < wsize)
663462d5b32SChuck Lever 			bytes = count;
664462d5b32SChuck Lever 
6655db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
666462d5b32SChuck Lever 		data = list_entry(list->next, struct nfs_write_data, pages);
667fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
668462d5b32SChuck Lever 
669462d5b32SChuck Lever 		data->inode = inode;
670462d5b32SChuck Lever 		data->cred = ctx->cred;
671462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
672462d5b32SChuck Lever 		data->args.context = ctx;
67388467055SChuck Lever 		data->args.offset = pos;
674462d5b32SChuck Lever 		data->args.pgbase = pgbase;
675462d5b32SChuck Lever 		data->args.pages = &pages[curpage];
676462d5b32SChuck Lever 		data->args.count = bytes;
677462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
678462d5b32SChuck Lever 		data->res.count = bytes;
67947989d74SChuck Lever 		data->res.verf = &data->verf;
680462d5b32SChuck Lever 
681462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
682462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
683fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
684462d5b32SChuck Lever 
685462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
686462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 		lock_kernel();
689462d5b32SChuck Lever 		rpc_execute(&data->task);
6901da177e4SLinus Torvalds 		unlock_kernel();
6911da177e4SLinus Torvalds 
692606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
693462d5b32SChuck Lever 				data->task.tk_pid,
694462d5b32SChuck Lever 				inode->i_sb->s_id,
695462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
696462d5b32SChuck Lever 				bytes,
697462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
698462d5b32SChuck Lever 
69988467055SChuck Lever 		pos += bytes;
700462d5b32SChuck Lever 		pgbase += bytes;
701462d5b32SChuck Lever 		curpage += pgbase >> PAGE_SHIFT;
702462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
703462d5b32SChuck Lever 
704462d5b32SChuck Lever 		count -= bytes;
705462d5b32SChuck Lever 	} while (count != 0);
7065db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
70988467055SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages)
710462d5b32SChuck Lever {
711462d5b32SChuck Lever 	ssize_t result;
712462d5b32SChuck Lever 	sigset_t oldset;
713c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
714462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
715462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
716fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
717fad61490STrond Myklebust 	int sync = 0;
718462d5b32SChuck Lever 
719fad61490STrond Myklebust 	dreq = nfs_direct_write_alloc(count, wsize);
720462d5b32SChuck Lever 	if (!dreq)
721462d5b32SChuck Lever 		return -ENOMEM;
722fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
723fad61490STrond Myklebust 		sync = FLUSH_STABLE;
724462d5b32SChuck Lever 
725fad61490STrond Myklebust 	dreq->user_addr = user_addr;
726fad61490STrond Myklebust 	dreq->user_count = count;
727fad61490STrond Myklebust 	dreq->pos = pos;
728462d5b32SChuck Lever 	dreq->pages = pages;
729462d5b32SChuck Lever 	dreq->npages = nr_pages;
730c89f2ee5SChuck Lever 	dreq->inode = inode;
731a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
732c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
733c89f2ee5SChuck Lever 		dreq->iocb = iocb;
734462d5b32SChuck Lever 
73547989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
73647989d74SChuck Lever 
737462d5b32SChuck Lever 	nfs_begin_data_update(inode);
738462d5b32SChuck Lever 
739462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
740fad61490STrond Myklebust 	nfs_direct_write_schedule(dreq, sync);
741c89f2ee5SChuck Lever 	result = nfs_direct_wait(dreq);
742462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
743462d5b32SChuck Lever 
744462d5b32SChuck Lever 	return result;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /**
7481da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7491da177e4SLinus Torvalds  * @iocb: target I/O control block
7501da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
75188467055SChuck Lever  * @count: number of bytes to read
75288467055SChuck Lever  * @pos: byte offset in file where reading starts
7531da177e4SLinus Torvalds  *
7541da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7551da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7561da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7571da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7581da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
75988467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7601da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7611da177e4SLinus Torvalds  * care of managing the end of file boundary.
7621da177e4SLinus Torvalds  *
7631da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7641da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7651da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
7661da177e4SLinus Torvalds  * cache.
7671da177e4SLinus Torvalds  */
768d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
7710cdd80d0SChuck Lever 	int page_count;
7720cdd80d0SChuck Lever 	struct page **pages;
7731da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7741da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
7751da177e4SLinus Torvalds 
776ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
7770bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7780bbacc40SChuck Lever 		file->f_dentry->d_name.name,
779ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (count < 0)
7821da177e4SLinus Torvalds 		goto out;
7831da177e4SLinus Torvalds 	retval = -EFAULT;
7840cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
7851da177e4SLinus Torvalds 		goto out;
7861da177e4SLinus Torvalds 	retval = 0;
7871da177e4SLinus Torvalds 	if (!count)
7881da177e4SLinus Torvalds 		goto out;
7891da177e4SLinus Torvalds 
79029884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
7911da177e4SLinus Torvalds 	if (retval)
7921da177e4SLinus Torvalds 		goto out;
7931da177e4SLinus Torvalds 
7946b45d858STrond Myklebust 	retval = nfs_get_user_pages(READ, (unsigned long) buf,
7950cdd80d0SChuck Lever 						count, &pages);
7966b45d858STrond Myklebust 	if (retval < 0)
7970cdd80d0SChuck Lever 		goto out;
7986b45d858STrond Myklebust 	page_count = retval;
7990cdd80d0SChuck Lever 
80099514f8fSChuck Lever 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
8010cdd80d0SChuck Lever 						pages, page_count);
8021da177e4SLinus Torvalds 	if (retval > 0)
8030cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds out:
8061da177e4SLinus Torvalds 	return retval;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds /**
8101da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8111da177e4SLinus Torvalds  * @iocb: target I/O control block
8121da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
81388467055SChuck Lever  * @count: number of bytes to write
81488467055SChuck Lever  * @pos: byte offset in file where writing starts
8151da177e4SLinus Torvalds  *
8161da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8171da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8181da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8191da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8201da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8211da177e4SLinus Torvalds  * parameter checking and report problems.
8221da177e4SLinus Torvalds  *
8231da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
8241da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
8251da177e4SLinus Torvalds  *
8261da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8271da177e4SLinus Torvalds  *
8281da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8291da177e4SLinus Torvalds  * readers of this file.
8301da177e4SLinus Torvalds  *
8311da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8321da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8331da177e4SLinus Torvalds  */
834d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
8351da177e4SLinus Torvalds {
836ce1a8e67SChuck Lever 	ssize_t retval;
83747989d74SChuck Lever 	int page_count;
83847989d74SChuck Lever 	struct page **pages;
8391da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8401da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8411da177e4SLinus Torvalds 
842ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8430bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
844ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
845ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8461da177e4SLinus Torvalds 
847ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
848ce1a8e67SChuck Lever 	if (retval)
8491da177e4SLinus Torvalds 		goto out;
850ce1a8e67SChuck Lever 
851ce1a8e67SChuck Lever 	retval = -EINVAL;
852ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8531da177e4SLinus Torvalds 		goto out;
8541da177e4SLinus Torvalds 	retval = 0;
8551da177e4SLinus Torvalds 	if (!count)
8561da177e4SLinus Torvalds 		goto out;
857ce1a8e67SChuck Lever 
858ce1a8e67SChuck Lever 	retval = -EFAULT;
85947989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
860ce1a8e67SChuck Lever 		goto out;
8611da177e4SLinus Torvalds 
86229884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8631da177e4SLinus Torvalds 	if (retval)
8641da177e4SLinus Torvalds 		goto out;
8651da177e4SLinus Torvalds 
8666b45d858STrond Myklebust 	retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
86747989d74SChuck Lever 						count, &pages);
8686b45d858STrond Myklebust 	if (retval < 0)
86947989d74SChuck Lever 		goto out;
8706b45d858STrond Myklebust 	page_count = retval;
87147989d74SChuck Lever 
872c89f2ee5SChuck Lever 	retval = nfs_direct_write(iocb, (unsigned long) buf, count,
87347989d74SChuck Lever 					pos, pages, page_count);
8749eafa8ccSChuck Lever 
8759eafa8ccSChuck Lever 	/*
8769eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
8779eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
8789eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
8799eafa8ccSChuck Lever 	 *
8809eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
8819eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
8829eafa8ccSChuck Lever 	 */
8831da177e4SLinus Torvalds 	if (mapping->nrpages)
8841da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
8859eafa8ccSChuck Lever 
8861da177e4SLinus Torvalds 	if (retval > 0)
887ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds out:
8901da177e4SLinus Torvalds 	return retval;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
89388467055SChuck Lever /**
89488467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
89588467055SChuck Lever  *
89688467055SChuck Lever  */
8971da177e4SLinus Torvalds int nfs_init_directcache(void)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9001da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
9011da177e4SLinus Torvalds 						0, SLAB_RECLAIM_ACCOUNT,
9021da177e4SLinus Torvalds 						NULL, NULL);
9031da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9041da177e4SLinus Torvalds 		return -ENOMEM;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	return 0;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
90988467055SChuck Lever /**
91088467055SChuck Lever  * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures
91188467055SChuck Lever  *
91288467055SChuck Lever  */
9131da177e4SLinus Torvalds void nfs_destroy_directcache(void)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
9161da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
9171da177e4SLinus Torvalds }
918