xref: /openbmc/linux/fs/nfs/direct.c (revision fedb595c66e1fbd5acafe0d43b7e95c13c936d61)
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"
591da177e4SLinus Torvalds 
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 */
83b1c5921cSChuck Lever 	atomic_t		io_count;	/* i/os we're waiting for */
8415ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8515ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
861da177e4SLinus Torvalds 				error;		/* any reported error */
87d72b7a6bSTrond 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_complete(struct nfs_direct_req *dreq, struct inode *inode);
98*fedb595cSChuck Lever static const struct rpc_call_ops nfs_write_direct_ops;
991da177e4SLinus Torvalds 
100b1c5921cSChuck Lever static inline void get_dreq(struct nfs_direct_req *dreq)
101b1c5921cSChuck Lever {
102b1c5921cSChuck Lever 	atomic_inc(&dreq->io_count);
103b1c5921cSChuck Lever }
104b1c5921cSChuck Lever 
105b1c5921cSChuck Lever static inline int put_dreq(struct nfs_direct_req *dreq)
106b1c5921cSChuck Lever {
107b1c5921cSChuck Lever 	return atomic_dec_and_test(&dreq->io_count);
108b1c5921cSChuck Lever }
109b1c5921cSChuck Lever 
1101da177e4SLinus Torvalds /**
111b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
112b8a32e2bSChuck Lever  * @rw: direction (read or write)
113b8a32e2bSChuck Lever  * @iocb: target I/O control block
114b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
115b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
116b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
117b8a32e2bSChuck Lever  *
118b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
119b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
120b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
121b8a32e2bSChuck Lever  * should never be called.
1221da177e4SLinus Torvalds  */
123b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
124b8a32e2bSChuck Lever {
125b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
126e99170ffSTrond Myklebust 			iocb->ki_filp->f_dentry->d_name.name,
127e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
128b8a32e2bSChuck Lever 
129b8a32e2bSChuck Lever 	return -EINVAL;
130b8a32e2bSChuck Lever }
131b8a32e2bSChuck Lever 
1326b45d858STrond Myklebust static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1336b45d858STrond Myklebust {
1346b45d858STrond Myklebust 	int i;
1356b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1366b45d858STrond Myklebust 		struct page *page = pages[i];
1376b45d858STrond Myklebust 		if (do_dirty && !PageCompound(page))
1386b45d858STrond Myklebust 			set_page_dirty_lock(page);
1396b45d858STrond Myklebust 		page_cache_release(page);
1406b45d858STrond Myklebust 	}
1416b45d858STrond Myklebust 	kfree(pages);
1426b45d858STrond Myklebust }
1436b45d858STrond Myklebust 
144d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	int result = -ENOMEM;
1471da177e4SLinus Torvalds 	unsigned long page_count;
1481da177e4SLinus Torvalds 	size_t array_size;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1511da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1541da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1551da177e4SLinus Torvalds 	if (*pages) {
1561da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1571da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1581da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1591da177e4SLinus Torvalds 					*pages, NULL);
1601da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
1616b45d858STrond Myklebust 		if (result != page_count) {
162143f412eSTrond Myklebust 			/*
1636b45d858STrond Myklebust 			 * If we got fewer pages than expected from
1646b45d858STrond Myklebust 			 * get_user_pages(), the user buffer runs off the
1656b45d858STrond Myklebust 			 * end of a mapping; return EFAULT.
166143f412eSTrond Myklebust 			 */
1676b45d858STrond Myklebust 			if (result >= 0) {
168143f412eSTrond Myklebust 				nfs_free_user_pages(*pages, result, 0);
169143f412eSTrond Myklebust 				result = -EFAULT;
1706b45d858STrond Myklebust 			} else
1716b45d858STrond Myklebust 				kfree(*pages);
1726b45d858STrond Myklebust 			*pages = NULL;
173143f412eSTrond Myklebust 		}
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 	return result;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
17893619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
1831da177e4SLinus Torvalds 	if (!dreq)
1841da177e4SLinus Torvalds 		return NULL;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	kref_init(&dreq->kref);
187d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1881da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dreq->list);
189fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
19093619e59SChuck Lever 	dreq->iocb = NULL;
191a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
19215ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
193b1c5921cSChuck Lever 	atomic_set(&dreq->io_count, 0);
19415ce4a0cSChuck Lever 	dreq->count = 0;
19515ce4a0cSChuck Lever 	dreq->error = 0;
196fad61490STrond Myklebust 	dreq->flags = 0;
19793619e59SChuck Lever 
19893619e59SChuck Lever 	return dreq;
19993619e59SChuck Lever }
20093619e59SChuck Lever 
2011da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
204a8881f5aSTrond Myklebust 
205a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
206a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2071da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
210d4cc948bSChuck Lever /*
211bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
212bc0fb201SChuck Lever  */
213bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
214bc0fb201SChuck Lever {
21515ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
216bc0fb201SChuck Lever 
217bc0fb201SChuck Lever 	/* Async requests don't wait here */
218bc0fb201SChuck Lever 	if (dreq->iocb)
219bc0fb201SChuck Lever 		goto out;
220bc0fb201SChuck Lever 
221d72b7a6bSTrond Myklebust 	result = wait_for_completion_interruptible(&dreq->completion);
222bc0fb201SChuck Lever 
223bc0fb201SChuck Lever 	if (!result)
22415ce4a0cSChuck Lever 		result = dreq->error;
225bc0fb201SChuck Lever 	if (!result)
22615ce4a0cSChuck Lever 		result = dreq->count;
227bc0fb201SChuck Lever 
228bc0fb201SChuck Lever out:
229bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
230bc0fb201SChuck Lever 	return (ssize_t) result;
231bc0fb201SChuck Lever }
232bc0fb201SChuck Lever 
233bc0fb201SChuck Lever /*
23463ab46abSChuck Lever  * We must hold a reference to all the pages in this direct read request
23563ab46abSChuck Lever  * until the RPCs complete.  This could be long *after* we are woken up in
23663ab46abSChuck Lever  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
23763ab46abSChuck Lever  *
23863ab46abSChuck Lever  * In addition, synchronous I/O uses a stack-allocated iocb.  Thus we
23963ab46abSChuck Lever  * can't trust the iocb is still valid here if this is a synchronous
24063ab46abSChuck Lever  * request.  If the waiter is woken prematurely, the iocb is long gone.
24163ab46abSChuck Lever  */
24263ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
24363ab46abSChuck Lever {
24463ab46abSChuck Lever 	nfs_free_user_pages(dreq->pages, dreq->npages, 1);
24563ab46abSChuck Lever 
24663ab46abSChuck Lever 	if (dreq->iocb) {
24715ce4a0cSChuck Lever 		long res = (long) dreq->error;
24863ab46abSChuck Lever 		if (!res)
24915ce4a0cSChuck Lever 			res = (long) dreq->count;
25063ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
251d72b7a6bSTrond Myklebust 	}
252d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
25363ab46abSChuck Lever 
25463ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
25563ab46abSChuck Lever }
25663ab46abSChuck Lever 
25763ab46abSChuck Lever /*
2581da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
2591da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
2601da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
2611da177e4SLinus Torvalds  */
2625dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	struct list_head *list;
2651da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2661da177e4SLinus Torvalds 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2671da177e4SLinus Torvalds 
26893619e59SChuck Lever 	dreq = nfs_direct_req_alloc();
2691da177e4SLinus Torvalds 	if (!dreq)
2701da177e4SLinus Torvalds 		return NULL;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	list = &dreq->list;
2731da177e4SLinus Torvalds 	for(;;) {
27440859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		if (unlikely(!data)) {
2771da177e4SLinus Torvalds 			while (!list_empty(list)) {
2781da177e4SLinus Torvalds 				data = list_entry(list->next,
2791da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
2801da177e4SLinus Torvalds 				list_del(&data->pages);
2811da177e4SLinus Torvalds 				nfs_readdata_free(data);
2821da177e4SLinus Torvalds 			}
2831da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
2841da177e4SLinus Torvalds 			return NULL;
2851da177e4SLinus Torvalds 		}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2881da177e4SLinus Torvalds 		list_add(&data->pages, list);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
291b1c5921cSChuck Lever 		get_dreq(dreq);
2921da177e4SLinus Torvalds 		if (nbytes <= rsize)
2931da177e4SLinus Torvalds 			break;
2941da177e4SLinus Torvalds 		nbytes -= rsize;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	kref_get(&dreq->kref);
2971da177e4SLinus Torvalds 	return dreq;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
300ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
3011da177e4SLinus Torvalds {
302ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
3031da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
3041da177e4SLinus Torvalds 
305ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
306ec06c096STrond Myklebust 		return;
3071da177e4SLinus Torvalds 
30815ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
30915ce4a0cSChuck Lever 
31015ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
31115ce4a0cSChuck Lever 		dreq->count += data->res.count;
3121da177e4SLinus Torvalds 	else
31315ce4a0cSChuck Lever 		dreq->error = task->tk_status;
3141da177e4SLinus Torvalds 
31515ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3161da177e4SLinus Torvalds 
317b1c5921cSChuck Lever 	if (put_dreq(dreq))
31863ab46abSChuck Lever 		nfs_direct_complete(dreq);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
321ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
322ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
323ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
324ec06c096STrond Myklebust };
325ec06c096STrond Myklebust 
326d4cc948bSChuck Lever /*
3271da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
3281da177e4SLinus Torvalds  * an NFS READ operation
3291da177e4SLinus Torvalds  */
330fad61490STrond Myklebust static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
3311da177e4SLinus Torvalds {
332a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
333a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
3341da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
3351da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
336fad61490STrond Myklebust 	size_t count = dreq->user_count;
337fad61490STrond Myklebust 	loff_t pos = dreq->pos;
3385dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
3391da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	curpage = 0;
342fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
3431da177e4SLinus Torvalds 	do {
3441da177e4SLinus Torvalds 		struct nfs_read_data *data;
3455dd602f2SChuck Lever 		size_t bytes;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 		bytes = rsize;
3481da177e4SLinus Torvalds 		if (count < rsize)
3491da177e4SLinus Torvalds 			bytes = count;
3501da177e4SLinus Torvalds 
3515db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
3521da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
3531da177e4SLinus Torvalds 		list_del_init(&data->pages);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 		data->inode = inode;
3561da177e4SLinus Torvalds 		data->cred = ctx->cred;
3571da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3581da177e4SLinus Torvalds 		data->args.context = ctx;
35988467055SChuck Lever 		data->args.offset = pos;
3601da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
3611da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
3621da177e4SLinus Torvalds 		data->args.count = bytes;
3631da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3641da177e4SLinus Torvalds 		data->res.eof = 0;
3651da177e4SLinus Torvalds 		data->res.count = bytes;
3661da177e4SLinus Torvalds 
367ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
368ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3691da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		lock_kernel();
3741da177e4SLinus Torvalds 		rpc_execute(&data->task);
3751da177e4SLinus Torvalds 		unlock_kernel();
3761da177e4SLinus Torvalds 
377606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
3781da177e4SLinus Torvalds 				data->task.tk_pid,
3791da177e4SLinus Torvalds 				inode->i_sb->s_id,
3801da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3811da177e4SLinus Torvalds 				bytes,
3821da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3831da177e4SLinus Torvalds 
38488467055SChuck Lever 		pos += bytes;
3851da177e4SLinus Torvalds 		pgbase += bytes;
3861da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
3871da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		count -= bytes;
3901da177e4SLinus Torvalds 	} while (count != 0);
3915db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
39488467055SChuck 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)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	ssize_t result;
3971da177e4SLinus Torvalds 	sigset_t oldset;
39899514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3991da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4001da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
4031da177e4SLinus Torvalds 	if (!dreq)
4041da177e4SLinus Torvalds 		return -ENOMEM;
4051da177e4SLinus Torvalds 
406fad61490STrond Myklebust 	dreq->user_addr = user_addr;
407fad61490STrond Myklebust 	dreq->user_count = count;
408fad61490STrond Myklebust 	dreq->pos = pos;
4091da177e4SLinus Torvalds 	dreq->pages = pages;
4101da177e4SLinus Torvalds 	dreq->npages = nr_pages;
41191d5b470SChuck Lever 	dreq->inode = inode;
412a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
413487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
414487b8372SChuck Lever 		dreq->iocb = iocb;
4151da177e4SLinus Torvalds 
41691d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
4171da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
418fad61490STrond Myklebust 	nfs_direct_read_schedule(dreq);
419bc0fb201SChuck Lever 	result = nfs_direct_wait(dreq);
4201da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	return result;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
425fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4261da177e4SLinus Torvalds {
427fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
428fad61490STrond Myklebust 	while (!list_empty(&dreq->list)) {
429fad61490STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
430fad61490STrond Myklebust 		list_del(&data->pages);
431fad61490STrond Myklebust 		nfs_writedata_release(data);
432fad61490STrond Myklebust 	}
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
436fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4371da177e4SLinus Torvalds {
438*fedb595cSChuck Lever 	struct inode *inode = dreq->inode;
439*fedb595cSChuck Lever 	struct list_head *p;
440*fedb595cSChuck Lever 	struct nfs_write_data *data;
4411da177e4SLinus Torvalds 
442fad61490STrond Myklebust 	dreq->count = 0;
443*fedb595cSChuck Lever 	get_dreq(dreq);
4441da177e4SLinus Torvalds 
445*fedb595cSChuck Lever 	list_for_each(p, &dreq->rewrite_list) {
446*fedb595cSChuck Lever 		data = list_entry(p, struct nfs_write_data, pages);
447*fedb595cSChuck Lever 
448*fedb595cSChuck Lever 		get_dreq(dreq);
449*fedb595cSChuck Lever 
450*fedb595cSChuck Lever 		/*
451*fedb595cSChuck Lever 		 * Reset data->res.
452*fedb595cSChuck Lever 		 */
453*fedb595cSChuck Lever 		nfs_fattr_init(&data->fattr);
454*fedb595cSChuck Lever 		data->res.count = data->args.count;
455*fedb595cSChuck Lever 		memset(&data->verf, 0, sizeof(data->verf));
456*fedb595cSChuck Lever 
457*fedb595cSChuck Lever 		/*
458*fedb595cSChuck Lever 		 * Reuse data->task; data->args should not have changed
459*fedb595cSChuck Lever 		 * since the original request was sent.
460*fedb595cSChuck Lever 		 */
461*fedb595cSChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
462*fedb595cSChuck Lever 				&nfs_write_direct_ops, data);
463*fedb595cSChuck Lever 		NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
464*fedb595cSChuck Lever 
465*fedb595cSChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
466*fedb595cSChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
467*fedb595cSChuck Lever 
468*fedb595cSChuck Lever 		/*
469*fedb595cSChuck Lever 		 * We're called via an RPC callback, so BKL is already held.
470*fedb595cSChuck Lever 		 */
471*fedb595cSChuck Lever 		rpc_execute(&data->task);
472*fedb595cSChuck Lever 
473*fedb595cSChuck Lever 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
474*fedb595cSChuck Lever 				data->task.tk_pid,
475*fedb595cSChuck Lever 				inode->i_sb->s_id,
476*fedb595cSChuck Lever 				(long long)NFS_FILEID(inode),
477*fedb595cSChuck Lever 				data->args.count,
478*fedb595cSChuck Lever 				(unsigned long long)data->args.offset);
479*fedb595cSChuck Lever 	}
480*fedb595cSChuck Lever 
481*fedb595cSChuck Lever 	if (put_dreq(dreq))
482*fedb595cSChuck Lever 		nfs_direct_write_complete(dreq, inode);
483fad61490STrond Myklebust }
4841da177e4SLinus Torvalds 
485fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
486fad61490STrond Myklebust {
487fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
488fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
4891da177e4SLinus Torvalds 
490fad61490STrond Myklebust 	/* Call the NFS version-specific code */
491fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
492fad61490STrond Myklebust 		return;
493fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
494fad61490STrond Myklebust 		dreq->error = task->tk_status;
495fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
496fad61490STrond Myklebust 	}
497fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
498fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
499fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
500fad61490STrond Myklebust 	}
501fad61490STrond Myklebust 
502fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
503fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
504fad61490STrond Myklebust }
505fad61490STrond Myklebust 
506fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
507fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
508fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
509fad61490STrond Myklebust };
510fad61490STrond Myklebust 
511fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
512fad61490STrond Myklebust {
513fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
514fad61490STrond Myklebust 
515fad61490STrond Myklebust 	data->inode = dreq->inode;
516a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
517fad61490STrond Myklebust 
518fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
519fad61490STrond Myklebust 	data->args.offset = dreq->pos;
520fad61490STrond Myklebust 	data->args.count = dreq->user_count;
521fad61490STrond Myklebust 	data->res.count = 0;
522fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
523fad61490STrond Myklebust 	data->res.verf = &data->verf;
524fad61490STrond Myklebust 
525fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
526fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
527fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
528fad61490STrond Myklebust 
529fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
530fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
531fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
532fad61490STrond Myklebust 	dreq->commit_data = NULL;
533fad61490STrond Myklebust 
534e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	lock_kernel();
537fad61490STrond Myklebust 	rpc_execute(&data->task);
5381da177e4SLinus Torvalds 	unlock_kernel();
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
541fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
5421da177e4SLinus Torvalds {
543fad61490STrond Myklebust 	int flags = dreq->flags;
5441da177e4SLinus Torvalds 
545fad61490STrond Myklebust 	dreq->flags = 0;
546fad61490STrond Myklebust 	switch (flags) {
547fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
548fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5491da177e4SLinus Torvalds 			break;
550fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
551fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5521da177e4SLinus Torvalds 			break;
5531da177e4SLinus Torvalds 		default:
554fad61490STrond Myklebust 			nfs_end_data_update(inode);
555fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
556fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
557fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
558fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5591da177e4SLinus Torvalds 	}
560fad61490STrond Myklebust }
561fad61490STrond Myklebust 
562fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
563fad61490STrond Myklebust {
564fad61490STrond Myklebust 	dreq->commit_data = nfs_commit_alloc(0);
565fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
566fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
567fad61490STrond Myklebust }
568fad61490STrond Myklebust #else
569fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
570fad61490STrond Myklebust {
571fad61490STrond Myklebust 	dreq->commit_data = NULL;
572fad61490STrond Myklebust }
573fad61490STrond Myklebust 
574fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
575fad61490STrond Myklebust {
576fad61490STrond Myklebust 	nfs_end_data_update(inode);
577fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
578fad61490STrond Myklebust 	nfs_direct_complete(dreq);
579fad61490STrond Myklebust }
580fad61490STrond Myklebust #endif
581fad61490STrond Myklebust 
582462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
5831da177e4SLinus Torvalds {
584462d5b32SChuck Lever 	struct list_head *list;
585462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
586462d5b32SChuck Lever 	unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5871da177e4SLinus Torvalds 
588462d5b32SChuck Lever 	dreq = nfs_direct_req_alloc();
589462d5b32SChuck Lever 	if (!dreq)
590462d5b32SChuck Lever 		return NULL;
5911da177e4SLinus Torvalds 
592462d5b32SChuck Lever 	list = &dreq->list;
593462d5b32SChuck Lever 	for(;;) {
594462d5b32SChuck Lever 		struct nfs_write_data *data = nfs_writedata_alloc(wpages);
5951da177e4SLinus Torvalds 
596462d5b32SChuck Lever 		if (unlikely(!data)) {
597462d5b32SChuck Lever 			while (!list_empty(list)) {
598462d5b32SChuck Lever 				data = list_entry(list->next,
599462d5b32SChuck Lever 						  struct nfs_write_data, pages);
600462d5b32SChuck Lever 				list_del(&data->pages);
601462d5b32SChuck Lever 				nfs_writedata_free(data);
602462d5b32SChuck Lever 			}
603462d5b32SChuck Lever 			kref_put(&dreq->kref, nfs_direct_req_release);
604462d5b32SChuck Lever 			return NULL;
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 
607462d5b32SChuck Lever 		INIT_LIST_HEAD(&data->pages);
608462d5b32SChuck Lever 		list_add(&data->pages, list);
6091da177e4SLinus Torvalds 
610462d5b32SChuck Lever 		data->req = (struct nfs_page *) dreq;
611b1c5921cSChuck Lever 		get_dreq(dreq);
612462d5b32SChuck Lever 		if (nbytes <= wsize)
6131da177e4SLinus Torvalds 			break;
614462d5b32SChuck Lever 		nbytes -= wsize;
615462d5b32SChuck Lever 	}
616fad61490STrond Myklebust 
617fad61490STrond Myklebust 	nfs_alloc_commit_data(dreq);
618fad61490STrond Myklebust 
619462d5b32SChuck Lever 	kref_get(&dreq->kref);
620462d5b32SChuck Lever 	return dreq;
621462d5b32SChuck Lever }
6221da177e4SLinus Torvalds 
623462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
624462d5b32SChuck Lever {
625462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
626462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
627462d5b32SChuck Lever 	int status = task->tk_status;
628462d5b32SChuck Lever 
629462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
630462d5b32SChuck Lever 		return;
631462d5b32SChuck Lever 
63215ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
633462d5b32SChuck Lever 
63415ce4a0cSChuck Lever 	if (likely(status >= 0))
63515ce4a0cSChuck Lever 		dreq->count += data->res.count;
63615ce4a0cSChuck Lever 	else
637fad61490STrond Myklebust 		dreq->error = task->tk_status;
63815ce4a0cSChuck Lever 
639fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
640fad61490STrond Myklebust 		switch (dreq->flags) {
641fad61490STrond Myklebust 			case 0:
642fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
643fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
644fad61490STrond Myklebust 				break;
645fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
646fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
647fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
648fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
649fad61490STrond Myklebust 				}
650fad61490STrond Myklebust 		}
651fad61490STrond Myklebust 	}
652fad61490STrond Myklebust 
653fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
654fad61490STrond Myklebust }
655fad61490STrond Myklebust 
656fad61490STrond Myklebust /*
657fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
658fad61490STrond Myklebust  *     errors after the first one are ignored.
659fad61490STrond Myklebust  */
660fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
661fad61490STrond Myklebust {
662fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
663fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
664fad61490STrond Myklebust 
665b1c5921cSChuck Lever 	if (put_dreq(dreq))
666fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
667462d5b32SChuck Lever }
668462d5b32SChuck Lever 
669462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
670462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
671fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
672462d5b32SChuck Lever };
673462d5b32SChuck Lever 
674462d5b32SChuck Lever /*
675462d5b32SChuck Lever  * For each nfs_write_data struct that was allocated on the list, dispatch
676462d5b32SChuck Lever  * an NFS WRITE operation
677462d5b32SChuck Lever  */
678fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
679462d5b32SChuck Lever {
680a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
681a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
682462d5b32SChuck Lever 	struct list_head *list = &dreq->list;
683462d5b32SChuck Lever 	struct page **pages = dreq->pages;
684fad61490STrond Myklebust 	size_t count = dreq->user_count;
685fad61490STrond Myklebust 	loff_t pos = dreq->pos;
686462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
687462d5b32SChuck Lever 	unsigned int curpage, pgbase;
688462d5b32SChuck Lever 
689462d5b32SChuck Lever 	curpage = 0;
690fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
691462d5b32SChuck Lever 	do {
692462d5b32SChuck Lever 		struct nfs_write_data *data;
693462d5b32SChuck Lever 		size_t bytes;
694462d5b32SChuck Lever 
695462d5b32SChuck Lever 		bytes = wsize;
696462d5b32SChuck Lever 		if (count < wsize)
697462d5b32SChuck Lever 			bytes = count;
698462d5b32SChuck Lever 
6995db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
700462d5b32SChuck Lever 		data = list_entry(list->next, struct nfs_write_data, pages);
701fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
702462d5b32SChuck Lever 
703462d5b32SChuck Lever 		data->inode = inode;
704462d5b32SChuck Lever 		data->cred = ctx->cred;
705462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
706462d5b32SChuck Lever 		data->args.context = ctx;
70788467055SChuck Lever 		data->args.offset = pos;
708462d5b32SChuck Lever 		data->args.pgbase = pgbase;
709462d5b32SChuck Lever 		data->args.pages = &pages[curpage];
710462d5b32SChuck Lever 		data->args.count = bytes;
711462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
712462d5b32SChuck Lever 		data->res.count = bytes;
71347989d74SChuck Lever 		data->res.verf = &data->verf;
714462d5b32SChuck Lever 
715462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
716462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
717fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
718462d5b32SChuck Lever 
719462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
720462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		lock_kernel();
723462d5b32SChuck Lever 		rpc_execute(&data->task);
7241da177e4SLinus Torvalds 		unlock_kernel();
7251da177e4SLinus Torvalds 
726606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
727462d5b32SChuck Lever 				data->task.tk_pid,
728462d5b32SChuck Lever 				inode->i_sb->s_id,
729462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
730462d5b32SChuck Lever 				bytes,
731462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
732462d5b32SChuck Lever 
73388467055SChuck Lever 		pos += bytes;
734462d5b32SChuck Lever 		pgbase += bytes;
735462d5b32SChuck Lever 		curpage += pgbase >> PAGE_SHIFT;
736462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
737462d5b32SChuck Lever 
738462d5b32SChuck Lever 		count -= bytes;
739462d5b32SChuck Lever 	} while (count != 0);
7405db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
74388467055SChuck 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)
744462d5b32SChuck Lever {
745462d5b32SChuck Lever 	ssize_t result;
746462d5b32SChuck Lever 	sigset_t oldset;
747c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
748462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
749462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
750fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
751fad61490STrond Myklebust 	int sync = 0;
752462d5b32SChuck Lever 
753fad61490STrond Myklebust 	dreq = nfs_direct_write_alloc(count, wsize);
754462d5b32SChuck Lever 	if (!dreq)
755462d5b32SChuck Lever 		return -ENOMEM;
756fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
757fad61490STrond Myklebust 		sync = FLUSH_STABLE;
758462d5b32SChuck Lever 
759fad61490STrond Myklebust 	dreq->user_addr = user_addr;
760fad61490STrond Myklebust 	dreq->user_count = count;
761fad61490STrond Myklebust 	dreq->pos = pos;
762462d5b32SChuck Lever 	dreq->pages = pages;
763462d5b32SChuck Lever 	dreq->npages = nr_pages;
764c89f2ee5SChuck Lever 	dreq->inode = inode;
765a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
766c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
767c89f2ee5SChuck Lever 		dreq->iocb = iocb;
768462d5b32SChuck Lever 
76947989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
77047989d74SChuck Lever 
771462d5b32SChuck Lever 	nfs_begin_data_update(inode);
772462d5b32SChuck Lever 
773462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
774fad61490STrond Myklebust 	nfs_direct_write_schedule(dreq, sync);
775c89f2ee5SChuck Lever 	result = nfs_direct_wait(dreq);
776462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
777462d5b32SChuck Lever 
7781da177e4SLinus Torvalds 	return result;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds /**
7821da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7831da177e4SLinus Torvalds  * @iocb: target I/O control block
7841da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
78588467055SChuck Lever  * @count: number of bytes to read
78688467055SChuck Lever  * @pos: byte offset in file where reading starts
7871da177e4SLinus Torvalds  *
7881da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7891da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7901da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7911da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7921da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
79388467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7941da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7951da177e4SLinus Torvalds  * care of managing the end of file boundary.
7961da177e4SLinus Torvalds  *
7971da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7981da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7991da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8001da177e4SLinus Torvalds  * cache.
8011da177e4SLinus Torvalds  */
802d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
8050cdd80d0SChuck Lever 	int page_count;
8060cdd80d0SChuck Lever 	struct page **pages;
8071da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8081da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8091da177e4SLinus Torvalds 
810ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
8110bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
8120bbacc40SChuck Lever 		file->f_dentry->d_name.name,
813ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (count < 0)
8161da177e4SLinus Torvalds 		goto out;
8171da177e4SLinus Torvalds 	retval = -EFAULT;
8180cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
8191da177e4SLinus Torvalds 		goto out;
8201da177e4SLinus Torvalds 	retval = 0;
8211da177e4SLinus Torvalds 	if (!count)
8221da177e4SLinus Torvalds 		goto out;
8231da177e4SLinus Torvalds 
82429884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8251da177e4SLinus Torvalds 	if (retval)
8261da177e4SLinus Torvalds 		goto out;
8271da177e4SLinus Torvalds 
8286b45d858STrond Myklebust 	retval = nfs_get_user_pages(READ, (unsigned long) buf,
8290cdd80d0SChuck Lever 						count, &pages);
8306b45d858STrond Myklebust 	if (retval < 0)
8310cdd80d0SChuck Lever 		goto out;
8326b45d858STrond Myklebust 	page_count = retval;
8330cdd80d0SChuck Lever 
83499514f8fSChuck Lever 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
8350cdd80d0SChuck Lever 						pages, page_count);
8361da177e4SLinus Torvalds 	if (retval > 0)
8370cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds out:
8401da177e4SLinus Torvalds 	return retval;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /**
8441da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8451da177e4SLinus Torvalds  * @iocb: target I/O control block
8461da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
84788467055SChuck Lever  * @count: number of bytes to write
84888467055SChuck Lever  * @pos: byte offset in file where writing starts
8491da177e4SLinus Torvalds  *
8501da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8511da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8521da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8531da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8541da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8551da177e4SLinus Torvalds  * parameter checking and report problems.
8561da177e4SLinus Torvalds  *
8571da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
8581da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
8591da177e4SLinus Torvalds  *
8601da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8611da177e4SLinus Torvalds  *
8621da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8631da177e4SLinus Torvalds  * readers of this file.
8641da177e4SLinus Torvalds  *
8651da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8661da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8671da177e4SLinus Torvalds  */
868d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
8691da177e4SLinus Torvalds {
870ce1a8e67SChuck Lever 	ssize_t retval;
87147989d74SChuck Lever 	int page_count;
87247989d74SChuck Lever 	struct page **pages;
8731da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8741da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8751da177e4SLinus Torvalds 
876ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8770bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
878ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
879ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8801da177e4SLinus Torvalds 
881ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
882ce1a8e67SChuck Lever 	if (retval)
8831da177e4SLinus Torvalds 		goto out;
884ce1a8e67SChuck Lever 
885ce1a8e67SChuck Lever 	retval = -EINVAL;
886ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8871da177e4SLinus Torvalds 		goto out;
8881da177e4SLinus Torvalds 	retval = 0;
8891da177e4SLinus Torvalds 	if (!count)
8901da177e4SLinus Torvalds 		goto out;
891ce1a8e67SChuck Lever 
892ce1a8e67SChuck Lever 	retval = -EFAULT;
89347989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
894ce1a8e67SChuck Lever 		goto out;
8951da177e4SLinus Torvalds 
89629884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8971da177e4SLinus Torvalds 	if (retval)
8981da177e4SLinus Torvalds 		goto out;
8991da177e4SLinus Torvalds 
9006b45d858STrond Myklebust 	retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
90147989d74SChuck Lever 						count, &pages);
9026b45d858STrond Myklebust 	if (retval < 0)
90347989d74SChuck Lever 		goto out;
9046b45d858STrond Myklebust 	page_count = retval;
90547989d74SChuck Lever 
906c89f2ee5SChuck Lever 	retval = nfs_direct_write(iocb, (unsigned long) buf, count,
90747989d74SChuck Lever 					pos, pages, page_count);
9089eafa8ccSChuck Lever 
9099eafa8ccSChuck Lever 	/*
9109eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
9119eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
9129eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
9139eafa8ccSChuck Lever 	 *
9149eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
9159eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
9169eafa8ccSChuck Lever 	 */
9171da177e4SLinus Torvalds 	if (mapping->nrpages)
9181da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
9199eafa8ccSChuck Lever 
9201da177e4SLinus Torvalds 	if (retval > 0)
921ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds out:
9241da177e4SLinus Torvalds 	return retval;
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
92788467055SChuck Lever /**
92888467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
92988467055SChuck Lever  *
93088467055SChuck Lever  */
931f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9341da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
935fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
936fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
9371da177e4SLinus Torvalds 						NULL, NULL);
9381da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9391da177e4SLinus Torvalds 		return -ENOMEM;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	return 0;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
94488467055SChuck Lever /**
945f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
94688467055SChuck Lever  *
94788467055SChuck Lever  */
948f7b422b1SDavid Howells void __exit nfs_destroy_directcache(void)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
9511da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
9521da177e4SLinus Torvalds }
953