xref: /openbmc/linux/fs/nfs/direct.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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/errno.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/file.h>
451da177e4SLinus Torvalds #include <linux/pagemap.h>
461da177e4SLinus Torvalds #include <linux/kref.h>
47*5a0e3ad6STejun Heo #include <linux/slab.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <linux/nfs_fs.h>
501da177e4SLinus Torvalds #include <linux/nfs_page.h>
511da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <asm/system.h>
541da177e4SLinus Torvalds #include <asm/uaccess.h>
551da177e4SLinus Torvalds #include <asm/atomic.h>
561da177e4SLinus Torvalds 
578d5658c9STrond Myklebust #include "internal.h"
5891d5b470SChuck Lever #include "iostat.h"
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
611da177e4SLinus Torvalds 
62e18b890bSChristoph Lameter static struct kmem_cache *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 */
71a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
7299514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7388467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7415ce4a0cSChuck Lever 
7515ce4a0cSChuck Lever 	/* completion state */
76607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7715ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
7815ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
791da177e4SLinus Torvalds 				error;		/* any reported error */
80d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
81fad61490STrond Myklebust 
82fad61490STrond Myklebust 	/* commit state */
83607f31e8STrond Myklebust 	struct list_head	rewrite_list;	/* saved nfs_write_data structs */
84fad61490STrond Myklebust 	struct nfs_write_data *	commit_data;	/* special write_data for commits */
85fad61490STrond Myklebust 	int			flags;
86fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
87fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
88fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
891da177e4SLinus Torvalds };
901da177e4SLinus Torvalds 
91fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
92607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops;
93607f31e8STrond Myklebust 
94607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
95607f31e8STrond Myklebust {
96607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
97607f31e8STrond Myklebust }
98607f31e8STrond Myklebust 
99607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
100607f31e8STrond Myklebust {
101607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
102607f31e8STrond Myklebust }
103607f31e8STrond Myklebust 
1041da177e4SLinus Torvalds /**
105b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
106b8a32e2bSChuck Lever  * @rw: direction (read or write)
107b8a32e2bSChuck Lever  * @iocb: target I/O control block
108b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
109b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
110b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
111b8a32e2bSChuck Lever  *
112b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
113b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
114b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
115b8a32e2bSChuck Lever  * should never be called.
1161da177e4SLinus Torvalds  */
117b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
118b8a32e2bSChuck Lever {
119b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
12001cce933SJosef "Jeff" Sipek 			iocb->ki_filp->f_path.dentry->d_name.name,
121e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
122b8a32e2bSChuck Lever 
123b8a32e2bSChuck Lever 	return -EINVAL;
124b8a32e2bSChuck Lever }
125b8a32e2bSChuck Lever 
126d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
1276b45d858STrond Myklebust {
128d4a8f367STrond Myklebust 	unsigned int npages;
129749e146eSChuck Lever 	unsigned int i;
130d4a8f367STrond Myklebust 
131d4a8f367STrond Myklebust 	if (count == 0)
132d4a8f367STrond Myklebust 		return;
133d4a8f367STrond Myklebust 	pages += (pgbase >> PAGE_SHIFT);
134d4a8f367STrond Myklebust 	npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1356b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1366b45d858STrond Myklebust 		struct page *page = pages[i];
137607f31e8STrond Myklebust 		if (!PageCompound(page))
138d4a8f367STrond Myklebust 			set_page_dirty(page);
1396b45d858STrond Myklebust 	}
1409c93ab7dSChuck Lever }
1419c93ab7dSChuck Lever 
142749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1439c93ab7dSChuck Lever {
144749e146eSChuck Lever 	unsigned int i;
145607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
146607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1476b45d858STrond Myklebust }
1486b45d858STrond Myklebust 
14993619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1521da177e4SLinus Torvalds 
153e94b1766SChristoph Lameter 	dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
1541da177e4SLinus Torvalds 	if (!dreq)
1551da177e4SLinus Torvalds 		return NULL;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	kref_init(&dreq->kref);
158607f31e8STrond Myklebust 	kref_get(&dreq->kref);
159d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
160fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
16193619e59SChuck Lever 	dreq->iocb = NULL;
162a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
16315ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
164607f31e8STrond Myklebust 	atomic_set(&dreq->io_count, 0);
16515ce4a0cSChuck Lever 	dreq->count = 0;
16615ce4a0cSChuck Lever 	dreq->error = 0;
167fad61490STrond Myklebust 	dreq->flags = 0;
16893619e59SChuck Lever 
16993619e59SChuck Lever 	return dreq;
17093619e59SChuck Lever }
17193619e59SChuck Lever 
172b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
175a8881f5aSTrond Myklebust 
176a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
177a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1781da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
181b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
182b4946ffbSTrond Myklebust {
183b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
184b4946ffbSTrond Myklebust }
185b4946ffbSTrond Myklebust 
186d4cc948bSChuck Lever /*
187bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
188bc0fb201SChuck Lever  */
189bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
190bc0fb201SChuck Lever {
19115ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
192bc0fb201SChuck Lever 
193bc0fb201SChuck Lever 	/* Async requests don't wait here */
194bc0fb201SChuck Lever 	if (dreq->iocb)
195bc0fb201SChuck Lever 		goto out;
196bc0fb201SChuck Lever 
197150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
198bc0fb201SChuck Lever 
199bc0fb201SChuck Lever 	if (!result)
20015ce4a0cSChuck Lever 		result = dreq->error;
201bc0fb201SChuck Lever 	if (!result)
20215ce4a0cSChuck Lever 		result = dreq->count;
203bc0fb201SChuck Lever 
204bc0fb201SChuck Lever out:
205bc0fb201SChuck Lever 	return (ssize_t) result;
206bc0fb201SChuck Lever }
207bc0fb201SChuck Lever 
208bc0fb201SChuck Lever /*
209607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
210607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
21163ab46abSChuck Lever  */
21263ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
21363ab46abSChuck Lever {
21463ab46abSChuck Lever 	if (dreq->iocb) {
21515ce4a0cSChuck Lever 		long res = (long) dreq->error;
21663ab46abSChuck Lever 		if (!res)
21715ce4a0cSChuck Lever 			res = (long) dreq->count;
21863ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
219d72b7a6bSTrond Myklebust 	}
220d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
22163ab46abSChuck Lever 
222b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
22363ab46abSChuck Lever }
22463ab46abSChuck Lever 
22563ab46abSChuck Lever /*
226607f31e8STrond Myklebust  * We must hold a reference to all the pages in this direct read request
227607f31e8STrond Myklebust  * until the RPCs complete.  This could be long *after* we are woken up in
228607f31e8STrond Myklebust  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
22906cf6f2eSChuck Lever  */
230ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2311da177e4SLinus Torvalds {
232ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2331da177e4SLinus Torvalds 
234fdd1e74cSTrond Myklebust 	nfs_readpage_result(task, data);
235fdd1e74cSTrond Myklebust }
236fdd1e74cSTrond Myklebust 
237fdd1e74cSTrond Myklebust static void nfs_direct_read_release(void *calldata)
238fdd1e74cSTrond Myklebust {
239fdd1e74cSTrond Myklebust 
240fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
241fdd1e74cSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
242fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
2431da177e4SLinus Torvalds 
24415ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
245fdd1e74cSTrond Myklebust 	if (unlikely(status < 0)) {
246fdd1e74cSTrond Myklebust 		dreq->error = status;
24715ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
248d4a8f367STrond Myklebust 	} else {
249d4a8f367STrond Myklebust 		dreq->count += data->res.count;
250d4a8f367STrond Myklebust 		spin_unlock(&dreq->lock);
251d4a8f367STrond Myklebust 		nfs_direct_dirty_pages(data->pagevec,
252d4a8f367STrond Myklebust 				data->args.pgbase,
253d4a8f367STrond Myklebust 				data->res.count);
254d4a8f367STrond Myklebust 	}
255d4a8f367STrond Myklebust 	nfs_direct_release_pages(data->pagevec, data->npages);
2561da177e4SLinus Torvalds 
257607f31e8STrond Myklebust 	if (put_dreq(dreq))
25863ab46abSChuck Lever 		nfs_direct_complete(dreq);
2591ae88b2eSTrond Myklebust 	nfs_readdata_free(data);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
263f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
264f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
265f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
266ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
267fdd1e74cSTrond Myklebust 	.rpc_release = nfs_direct_read_release,
268ec06c096STrond Myklebust };
269ec06c096STrond Myklebust 
270d4cc948bSChuck Lever /*
271607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
272607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
273607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
274607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
275607f31e8STrond Myklebust  * no requests have been sent, just return an error.
2761da177e4SLinus Torvalds  */
27702fe4946SChuck Lever static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq,
27802fe4946SChuck Lever 						const struct iovec *iov,
27902fe4946SChuck Lever 						loff_t pos)
2801da177e4SLinus Torvalds {
281a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
28288be9f99STrond Myklebust 	struct inode *inode = ctx->path.dentry->d_inode;
28302fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
28402fe4946SChuck Lever 	size_t count = iov->iov_len;
2855dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
28607737691STrond Myklebust 	struct rpc_task *task;
287bdc7f021STrond Myklebust 	struct rpc_message msg = {
288bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
289bdc7f021STrond Myklebust 	};
29084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
29184115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
292bdc7f021STrond Myklebust 		.rpc_message = &msg,
29384115e1cSTrond Myklebust 		.callback_ops = &nfs_read_direct_ops,
294101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
29584115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
29684115e1cSTrond Myklebust 	};
297607f31e8STrond Myklebust 	unsigned int pgbase;
298607f31e8STrond Myklebust 	int result;
299607f31e8STrond Myklebust 	ssize_t started = 0;
30082b145c5SChuck Lever 
3011da177e4SLinus Torvalds 	do {
30282b145c5SChuck Lever 		struct nfs_read_data *data;
3035dd602f2SChuck Lever 		size_t bytes;
3041da177e4SLinus Torvalds 
305e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
306e9f7bee1STrond Myklebust 		bytes = min(rsize,count);
307e9f7bee1STrond Myklebust 
308607f31e8STrond Myklebust 		result = -ENOMEM;
3098d5658c9STrond Myklebust 		data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes));
310607f31e8STrond Myklebust 		if (unlikely(!data))
311607f31e8STrond Myklebust 			break;
312607f31e8STrond Myklebust 
313607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
314607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
315607f31e8STrond Myklebust 					data->npages, 1, 0, data->pagevec, NULL);
316607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
317749e146eSChuck Lever 		if (result < 0) {
3181ae88b2eSTrond Myklebust 			nfs_readdata_free(data);
319749e146eSChuck Lever 			break;
320749e146eSChuck Lever 		}
321749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
322d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
323d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
324607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
3251ae88b2eSTrond Myklebust 				nfs_readdata_free(data);
326607f31e8STrond Myklebust 				break;
327607f31e8STrond Myklebust 			}
328d9df8d6bSTrond Myklebust 			bytes -= pgbase;
329d9df8d6bSTrond Myklebust 			data->npages = result;
330d9df8d6bSTrond Myklebust 		}
33106cf6f2eSChuck Lever 
332607f31e8STrond Myklebust 		get_dreq(dreq);
333607f31e8STrond Myklebust 
334607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
3351da177e4SLinus Torvalds 		data->inode = inode;
336bdc7f021STrond Myklebust 		data->cred = msg.rpc_cred;
3371da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3381ae88b2eSTrond Myklebust 		data->args.context = ctx;
33988467055SChuck Lever 		data->args.offset = pos;
3401da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
341607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
3421da177e4SLinus Torvalds 		data->args.count = bytes;
3431da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3441da177e4SLinus Torvalds 		data->res.eof = 0;
3451da177e4SLinus Torvalds 		data->res.count = bytes;
34665d26953SChuck Lever 		nfs_fattr_init(&data->fattr);
347bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
348bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
3491da177e4SLinus Torvalds 
35007737691STrond Myklebust 		task_setup_data.task = &data->task;
35184115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
352bdc7f021STrond Myklebust 		NFS_PROTO(inode)->read_setup(data, &msg);
3531da177e4SLinus Torvalds 
35407737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
355dbae4c73STrond Myklebust 		if (IS_ERR(task))
356dbae4c73STrond Myklebust 			break;
35707737691STrond Myklebust 		rpc_put_task(task);
3581da177e4SLinus Torvalds 
359a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct read call "
360a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
3611da177e4SLinus Torvalds 				data->task.tk_pid,
3621da177e4SLinus Torvalds 				inode->i_sb->s_id,
3631da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3641da177e4SLinus Torvalds 				bytes,
3651da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3661da177e4SLinus Torvalds 
367607f31e8STrond Myklebust 		started += bytes;
368607f31e8STrond Myklebust 		user_addr += bytes;
36988467055SChuck Lever 		pos += bytes;
370e9f7bee1STrond Myklebust 		/* FIXME: Remove this unnecessary math from final patch */
3711da177e4SLinus Torvalds 		pgbase += bytes;
3721da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
373e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		count -= bytes;
3761da177e4SLinus Torvalds 	} while (count != 0);
377607f31e8STrond Myklebust 
378607f31e8STrond Myklebust 	if (started)
379c216fd70SChuck Lever 		return started;
380607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
38106cf6f2eSChuck Lever }
38206cf6f2eSChuck Lever 
38319f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
38419f73787SChuck Lever 					      const struct iovec *iov,
38519f73787SChuck Lever 					      unsigned long nr_segs,
38619f73787SChuck Lever 					      loff_t pos)
38719f73787SChuck Lever {
38819f73787SChuck Lever 	ssize_t result = -EINVAL;
38919f73787SChuck Lever 	size_t requested_bytes = 0;
39019f73787SChuck Lever 	unsigned long seg;
39119f73787SChuck Lever 
39219f73787SChuck Lever 	get_dreq(dreq);
39319f73787SChuck Lever 
39419f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
39519f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
39602fe4946SChuck Lever 		result = nfs_direct_read_schedule_segment(dreq, vec, pos);
39719f73787SChuck Lever 		if (result < 0)
39819f73787SChuck Lever 			break;
39919f73787SChuck Lever 		requested_bytes += result;
40019f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
40119f73787SChuck Lever 			break;
40219f73787SChuck Lever 		pos += vec->iov_len;
40319f73787SChuck Lever 	}
40419f73787SChuck Lever 
40519f73787SChuck Lever 	if (put_dreq(dreq))
40619f73787SChuck Lever 		nfs_direct_complete(dreq);
40719f73787SChuck Lever 
40819f73787SChuck Lever 	if (requested_bytes != 0)
40919f73787SChuck Lever 		return 0;
41019f73787SChuck Lever 
41119f73787SChuck Lever 	if (result < 0)
41219f73787SChuck Lever 		return result;
41319f73787SChuck Lever 	return -EIO;
41419f73787SChuck Lever }
41519f73787SChuck Lever 
416c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
417c216fd70SChuck Lever 			       unsigned long nr_segs, loff_t pos)
4181da177e4SLinus Torvalds {
419607f31e8STrond Myklebust 	ssize_t result = 0;
42099514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4211da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4221da177e4SLinus Torvalds 
423607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
4241da177e4SLinus Torvalds 	if (!dreq)
4251da177e4SLinus Torvalds 		return -ENOMEM;
4261da177e4SLinus Torvalds 
42791d5b470SChuck Lever 	dreq->inode = inode;
428cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
429487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
430487b8372SChuck Lever 		dreq->iocb = iocb;
4311da177e4SLinus Torvalds 
432c216fd70SChuck Lever 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
433607f31e8STrond Myklebust 	if (!result)
434bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
435b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	return result;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4411da177e4SLinus Torvalds {
442607f31e8STrond Myklebust 	while (!list_empty(&dreq->rewrite_list)) {
443607f31e8STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
444fad61490STrond Myklebust 		list_del(&data->pages);
445607f31e8STrond Myklebust 		nfs_direct_release_pages(data->pagevec, data->npages);
4461ae88b2eSTrond Myklebust 		nfs_writedata_free(data);
447fad61490STrond Myklebust 	}
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
450fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
451fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4521da177e4SLinus Torvalds {
453607f31e8STrond Myklebust 	struct inode *inode = dreq->inode;
454607f31e8STrond Myklebust 	struct list_head *p;
455607f31e8STrond Myklebust 	struct nfs_write_data *data;
45607737691STrond Myklebust 	struct rpc_task *task;
457bdc7f021STrond Myklebust 	struct rpc_message msg = {
458bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
459bdc7f021STrond Myklebust 	};
46084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
46184115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
462a8b40bc7STerry Loftin 		.rpc_message = &msg,
46384115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
464101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
46584115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
46684115e1cSTrond Myklebust 	};
4671da177e4SLinus Torvalds 
468fad61490STrond Myklebust 	dreq->count = 0;
469607f31e8STrond Myklebust 	get_dreq(dreq);
4701da177e4SLinus Torvalds 
471607f31e8STrond Myklebust 	list_for_each(p, &dreq->rewrite_list) {
472607f31e8STrond Myklebust 		data = list_entry(p, struct nfs_write_data, pages);
473607f31e8STrond Myklebust 
474607f31e8STrond Myklebust 		get_dreq(dreq);
475607f31e8STrond Myklebust 
476bdc7f021STrond Myklebust 		/* Use stable writes */
477bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
478bdc7f021STrond Myklebust 
479607f31e8STrond Myklebust 		/*
480607f31e8STrond Myklebust 		 * Reset data->res.
481607f31e8STrond Myklebust 		 */
482607f31e8STrond Myklebust 		nfs_fattr_init(&data->fattr);
483607f31e8STrond Myklebust 		data->res.count = data->args.count;
484607f31e8STrond Myklebust 		memset(&data->verf, 0, sizeof(data->verf));
485607f31e8STrond Myklebust 
486607f31e8STrond Myklebust 		/*
487607f31e8STrond Myklebust 		 * Reuse data->task; data->args should not have changed
488607f31e8STrond Myklebust 		 * since the original request was sent.
489607f31e8STrond Myklebust 		 */
49007737691STrond Myklebust 		task_setup_data.task = &data->task;
49184115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
492bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
493bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
494bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
495607f31e8STrond Myklebust 
496607f31e8STrond Myklebust 		/*
497607f31e8STrond Myklebust 		 * We're called via an RPC callback, so BKL is already held.
498607f31e8STrond Myklebust 		 */
49907737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
50007737691STrond Myklebust 		if (!IS_ERR(task))
50107737691STrond Myklebust 			rpc_put_task(task);
502607f31e8STrond Myklebust 
503607f31e8STrond Myklebust 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
504607f31e8STrond Myklebust 				data->task.tk_pid,
505607f31e8STrond Myklebust 				inode->i_sb->s_id,
506607f31e8STrond Myklebust 				(long long)NFS_FILEID(inode),
507607f31e8STrond Myklebust 				data->args.count,
508607f31e8STrond Myklebust 				(unsigned long long)data->args.offset);
509607f31e8STrond Myklebust 	}
510607f31e8STrond Myklebust 
511607f31e8STrond Myklebust 	if (put_dreq(dreq))
512607f31e8STrond Myklebust 		nfs_direct_write_complete(dreq, inode);
513fad61490STrond Myklebust }
5141da177e4SLinus Torvalds 
515fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
516fad61490STrond Myklebust {
517fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
5181da177e4SLinus Torvalds 
519fad61490STrond Myklebust 	/* Call the NFS version-specific code */
520c9d8f89dSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
521c9d8f89dSTrond Myklebust }
522c9d8f89dSTrond Myklebust 
523c9d8f89dSTrond Myklebust static void nfs_direct_commit_release(void *calldata)
524c9d8f89dSTrond Myklebust {
525c9d8f89dSTrond Myklebust 	struct nfs_write_data *data = calldata;
526c9d8f89dSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
527c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
528c9d8f89dSTrond Myklebust 
529c9d8f89dSTrond Myklebust 	if (status < 0) {
53060fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
531c9d8f89dSTrond Myklebust 				data->task.tk_pid, status);
532fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
53360fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
534c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
535fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
536fad61490STrond Myklebust 	}
537fad61490STrond Myklebust 
538c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
539fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
5401ae88b2eSTrond Myklebust 	nfs_commit_free(data);
541fad61490STrond Myklebust }
542fad61490STrond Myklebust 
543fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
54421d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
54521d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
54621d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
547fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
548c9d8f89dSTrond Myklebust 	.rpc_release = nfs_direct_commit_release,
549fad61490STrond Myklebust };
550fad61490STrond Myklebust 
551fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
552fad61490STrond Myklebust {
553fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
55407737691STrond Myklebust 	struct rpc_task *task;
555bdc7f021STrond Myklebust 	struct rpc_message msg = {
556bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
557bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
558bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
559bdc7f021STrond Myklebust 	};
56084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
56107737691STrond Myklebust 		.task = &data->task,
56284115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(dreq->inode),
563bdc7f021STrond Myklebust 		.rpc_message = &msg,
56484115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_direct_ops,
56584115e1cSTrond Myklebust 		.callback_data = data,
566101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
56784115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
56884115e1cSTrond Myklebust 	};
569fad61490STrond Myklebust 
570fad61490STrond Myklebust 	data->inode = dreq->inode;
571bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
572fad61490STrond Myklebust 
573fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
574607f31e8STrond Myklebust 	data->args.offset = 0;
575607f31e8STrond Myklebust 	data->args.count = 0;
5761ae88b2eSTrond Myklebust 	data->args.context = dreq->ctx;
577fad61490STrond Myklebust 	data->res.count = 0;
578fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
579fad61490STrond Myklebust 	data->res.verf = &data->verf;
58065d26953SChuck Lever 	nfs_fattr_init(&data->fattr);
581fad61490STrond Myklebust 
582bdc7f021STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
583fad61490STrond Myklebust 
584fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
585fad61490STrond Myklebust 	dreq->commit_data = NULL;
586fad61490STrond Myklebust 
587e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
5881da177e4SLinus Torvalds 
58907737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
59007737691STrond Myklebust 	if (!IS_ERR(task))
59107737691STrond Myklebust 		rpc_put_task(task);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
5951da177e4SLinus Torvalds {
596fad61490STrond Myklebust 	int flags = dreq->flags;
5971da177e4SLinus Torvalds 
598fad61490STrond Myklebust 	dreq->flags = 0;
599fad61490STrond Myklebust 	switch (flags) {
600fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
601fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6021da177e4SLinus Torvalds 			break;
603fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
604fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6051da177e4SLinus Torvalds 			break;
6061da177e4SLinus Torvalds 		default:
607fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
608fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
609fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
610cd9ae2b6STrond Myklebust 			nfs_zap_mapping(inode, inode->i_mapping);
611fad61490STrond Myklebust 			nfs_direct_complete(dreq);
6121da177e4SLinus Torvalds 	}
613fad61490STrond Myklebust }
614fad61490STrond Myklebust 
615fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
616fad61490STrond Myklebust {
617c9d8f89dSTrond Myklebust 	dreq->commit_data = nfs_commitdata_alloc();
618fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
619fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
620fad61490STrond Myklebust }
621fad61490STrond Myklebust #else
622fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
623fad61490STrond Myklebust {
624fad61490STrond Myklebust 	dreq->commit_data = NULL;
625fad61490STrond Myklebust }
626fad61490STrond Myklebust 
627fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
628fad61490STrond Myklebust {
629fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
630cd9ae2b6STrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
631fad61490STrond Myklebust 	nfs_direct_complete(dreq);
632fad61490STrond Myklebust }
633fad61490STrond Myklebust #endif
634fad61490STrond Myklebust 
635462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
636462d5b32SChuck Lever {
637462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
638462d5b32SChuck Lever 
639462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
640462d5b32SChuck Lever 		return;
641c9d8f89dSTrond Myklebust }
642c9d8f89dSTrond Myklebust 
643c9d8f89dSTrond Myklebust /*
644c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
645c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
646c9d8f89dSTrond Myklebust  */
647c9d8f89dSTrond Myklebust static void nfs_direct_write_release(void *calldata)
648c9d8f89dSTrond Myklebust {
649c9d8f89dSTrond Myklebust 	struct nfs_write_data *data = calldata;
650c9d8f89dSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
651c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
652462d5b32SChuck Lever 
65315ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
654462d5b32SChuck Lever 
65560fa3f76STrond Myklebust 	if (unlikely(status < 0)) {
656432409eeSNeil Brown 		/* An error has occurred, so we should not commit */
65760fa3f76STrond Myklebust 		dreq->flags = 0;
65860fa3f76STrond Myklebust 		dreq->error = status;
659eda3cef8STrond Myklebust 	}
660432409eeSNeil Brown 	if (unlikely(dreq->error != 0))
661432409eeSNeil Brown 		goto out_unlock;
662eda3cef8STrond Myklebust 
66315ce4a0cSChuck Lever 	dreq->count += data->res.count;
66415ce4a0cSChuck Lever 
665fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
666fad61490STrond Myklebust 		switch (dreq->flags) {
667fad61490STrond Myklebust 			case 0:
668fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
669fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
670fad61490STrond Myklebust 				break;
671fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
672fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
673c9d8f89dSTrond Myklebust 					dprintk("NFS: %5u write verify failed\n", data->task.tk_pid);
674fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
675fad61490STrond Myklebust 				}
676fad61490STrond Myklebust 		}
677fad61490STrond Myklebust 	}
678eda3cef8STrond Myklebust out_unlock:
679fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
680fad61490STrond Myklebust 
681607f31e8STrond Myklebust 	if (put_dreq(dreq))
682fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
683462d5b32SChuck Lever }
684462d5b32SChuck Lever 
685462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
686def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
687def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
688def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
689462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
690fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
691462d5b32SChuck Lever };
692462d5b32SChuck Lever 
693462d5b32SChuck Lever /*
694607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
695607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
696607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
697607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
698607f31e8STrond Myklebust  * no requests have been sent, just return an error.
699462d5b32SChuck Lever  */
70002fe4946SChuck Lever static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq,
70102fe4946SChuck Lever 						 const struct iovec *iov,
70202fe4946SChuck Lever 						 loff_t pos, int sync)
703462d5b32SChuck Lever {
704a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
70588be9f99STrond Myklebust 	struct inode *inode = ctx->path.dentry->d_inode;
70602fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
70702fe4946SChuck Lever 	size_t count = iov->iov_len;
70807737691STrond Myklebust 	struct rpc_task *task;
709bdc7f021STrond Myklebust 	struct rpc_message msg = {
710bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
711bdc7f021STrond Myklebust 	};
71284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
71384115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
714bdc7f021STrond Myklebust 		.rpc_message = &msg,
71584115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
716101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
71784115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
71884115e1cSTrond Myklebust 	};
719462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
720607f31e8STrond Myklebust 	unsigned int pgbase;
721607f31e8STrond Myklebust 	int result;
722607f31e8STrond Myklebust 	ssize_t started = 0;
72382b145c5SChuck Lever 
724462d5b32SChuck Lever 	do {
72582b145c5SChuck Lever 		struct nfs_write_data *data;
726462d5b32SChuck Lever 		size_t bytes;
727462d5b32SChuck Lever 
728e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
729e9f7bee1STrond Myklebust 		bytes = min(wsize,count);
730e9f7bee1STrond Myklebust 
731607f31e8STrond Myklebust 		result = -ENOMEM;
7328d5658c9STrond Myklebust 		data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes));
733607f31e8STrond Myklebust 		if (unlikely(!data))
734607f31e8STrond Myklebust 			break;
735607f31e8STrond Myklebust 
736607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
737607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
738607f31e8STrond Myklebust 					data->npages, 0, 0, data->pagevec, NULL);
739607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
740749e146eSChuck Lever 		if (result < 0) {
7411ae88b2eSTrond Myklebust 			nfs_writedata_free(data);
742749e146eSChuck Lever 			break;
743749e146eSChuck Lever 		}
744749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
745d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
746d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
747607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
7481ae88b2eSTrond Myklebust 				nfs_writedata_free(data);
749607f31e8STrond Myklebust 				break;
750607f31e8STrond Myklebust 			}
751d9df8d6bSTrond Myklebust 			bytes -= pgbase;
752d9df8d6bSTrond Myklebust 			data->npages = result;
753d9df8d6bSTrond Myklebust 		}
754607f31e8STrond Myklebust 
755607f31e8STrond Myklebust 		get_dreq(dreq);
756607f31e8STrond Myklebust 
757fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
758462d5b32SChuck Lever 
759607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
760462d5b32SChuck Lever 		data->inode = inode;
761bdc7f021STrond Myklebust 		data->cred = msg.rpc_cred;
762462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
7631ae88b2eSTrond Myklebust 		data->args.context = ctx;
76488467055SChuck Lever 		data->args.offset = pos;
765462d5b32SChuck Lever 		data->args.pgbase = pgbase;
766607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
767462d5b32SChuck Lever 		data->args.count = bytes;
768bdc7f021STrond Myklebust 		data->args.stable = sync;
769462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
770462d5b32SChuck Lever 		data->res.count = bytes;
77147989d74SChuck Lever 		data->res.verf = &data->verf;
77265d26953SChuck Lever 		nfs_fattr_init(&data->fattr);
773462d5b32SChuck Lever 
77407737691STrond Myklebust 		task_setup_data.task = &data->task;
77584115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
776bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
777bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
778bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
779462d5b32SChuck Lever 
78007737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
781dbae4c73STrond Myklebust 		if (IS_ERR(task))
782dbae4c73STrond Myklebust 			break;
78307737691STrond Myklebust 		rpc_put_task(task);
7841da177e4SLinus Torvalds 
785a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct write call "
786a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
787462d5b32SChuck Lever 				data->task.tk_pid,
788462d5b32SChuck Lever 				inode->i_sb->s_id,
789462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
790462d5b32SChuck Lever 				bytes,
791462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
792462d5b32SChuck Lever 
793607f31e8STrond Myklebust 		started += bytes;
794607f31e8STrond Myklebust 		user_addr += bytes;
79588467055SChuck Lever 		pos += bytes;
796e9f7bee1STrond Myklebust 
797e9f7bee1STrond Myklebust 		/* FIXME: Remove this useless math from the final patch */
798462d5b32SChuck Lever 		pgbase += bytes;
799462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
800e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
801462d5b32SChuck Lever 
802462d5b32SChuck Lever 		count -= bytes;
803462d5b32SChuck Lever 	} while (count != 0);
804607f31e8STrond Myklebust 
805607f31e8STrond Myklebust 	if (started)
806c216fd70SChuck Lever 		return started;
807607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
80806cf6f2eSChuck Lever }
80906cf6f2eSChuck Lever 
81019f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
81119f73787SChuck Lever 					       const struct iovec *iov,
81219f73787SChuck Lever 					       unsigned long nr_segs,
81319f73787SChuck Lever 					       loff_t pos, int sync)
81419f73787SChuck Lever {
81519f73787SChuck Lever 	ssize_t result = 0;
81619f73787SChuck Lever 	size_t requested_bytes = 0;
81719f73787SChuck Lever 	unsigned long seg;
81819f73787SChuck Lever 
81919f73787SChuck Lever 	get_dreq(dreq);
82019f73787SChuck Lever 
82119f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
82219f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
82302fe4946SChuck Lever 		result = nfs_direct_write_schedule_segment(dreq, vec,
82419f73787SChuck Lever 							   pos, sync);
82519f73787SChuck Lever 		if (result < 0)
82619f73787SChuck Lever 			break;
82719f73787SChuck Lever 		requested_bytes += result;
82819f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
82919f73787SChuck Lever 			break;
83019f73787SChuck Lever 		pos += vec->iov_len;
83119f73787SChuck Lever 	}
83219f73787SChuck Lever 
83319f73787SChuck Lever 	if (put_dreq(dreq))
83419f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
83519f73787SChuck Lever 
83619f73787SChuck Lever 	if (requested_bytes != 0)
83719f73787SChuck Lever 		return 0;
83819f73787SChuck Lever 
83919f73787SChuck Lever 	if (result < 0)
84019f73787SChuck Lever 		return result;
84119f73787SChuck Lever 	return -EIO;
84219f73787SChuck Lever }
84319f73787SChuck Lever 
844c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
845c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
846c216fd70SChuck Lever 				size_t count)
847462d5b32SChuck Lever {
848607f31e8STrond Myklebust 	ssize_t result = 0;
849c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
850462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
851fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
852bdc7f021STrond Myklebust 	int sync = NFS_UNSTABLE;
853462d5b32SChuck Lever 
854607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
855462d5b32SChuck Lever 	if (!dreq)
856462d5b32SChuck Lever 		return -ENOMEM;
857607f31e8STrond Myklebust 	nfs_alloc_commit_data(dreq);
858607f31e8STrond Myklebust 
859fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
860bdc7f021STrond Myklebust 		sync = NFS_FILE_SYNC;
861462d5b32SChuck Lever 
862c89f2ee5SChuck Lever 	dreq->inode = inode;
863cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
864c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
865c89f2ee5SChuck Lever 		dreq->iocb = iocb;
866462d5b32SChuck Lever 
867c216fd70SChuck Lever 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync);
868607f31e8STrond Myklebust 	if (!result)
869c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
870b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
871462d5b32SChuck Lever 
8721da177e4SLinus Torvalds 	return result;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /**
8761da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8771da177e4SLinus Torvalds  * @iocb: target I/O control block
878027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
879027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
88088467055SChuck Lever  * @pos: byte offset in file where reading starts
8811da177e4SLinus Torvalds  *
8821da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
8831da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
8841da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
8851da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
8861da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
88788467055SChuck Lever  * READ where the file size could change.  Our preference is simply
8881da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
8891da177e4SLinus Torvalds  * care of managing the end of file boundary.
8901da177e4SLinus Torvalds  *
8911da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
8921da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
8931da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8941da177e4SLinus Torvalds  * cache.
8951da177e4SLinus Torvalds  */
896027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
897027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
9001da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9011da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
902c216fd70SChuck Lever 	size_t count;
9031da177e4SLinus Torvalds 
904c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
905c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
906c216fd70SChuck Lever 
9076da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
90801cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
90901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
910c216fd70SChuck Lever 		count, (long long) pos);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	retval = 0;
9131da177e4SLinus Torvalds 	if (!count)
9141da177e4SLinus Torvalds 		goto out;
9151da177e4SLinus Torvalds 
91629884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9171da177e4SLinus Torvalds 	if (retval)
9181da177e4SLinus Torvalds 		goto out;
9191da177e4SLinus Torvalds 
920c216fd70SChuck Lever 	retval = nfs_direct_read(iocb, iov, nr_segs, pos);
9211da177e4SLinus Torvalds 	if (retval > 0)
9220cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds out:
9251da177e4SLinus Torvalds 	return retval;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds /**
9291da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9301da177e4SLinus Torvalds  * @iocb: target I/O control block
931027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
932027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
93388467055SChuck Lever  * @pos: byte offset in file where writing starts
9341da177e4SLinus Torvalds  *
9351da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9361da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9371da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9381da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9391da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9401da177e4SLinus Torvalds  * parameter checking and report problems.
9411da177e4SLinus Torvalds  *
9421da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9431da177e4SLinus Torvalds  *
9441da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9451da177e4SLinus Torvalds  * readers of this file.
9461da177e4SLinus Torvalds  *
9471da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9481da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9491da177e4SLinus Torvalds  */
950027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
951027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9521da177e4SLinus Torvalds {
953070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9541da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9551da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
956c216fd70SChuck Lever 	size_t count;
9571da177e4SLinus Torvalds 
958c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
959c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
960c216fd70SChuck Lever 
9616da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
96201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
96301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
964c216fd70SChuck Lever 		count, (long long) pos);
965027445c3SBadari Pulavarty 
966ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
967ce1a8e67SChuck Lever 	if (retval)
9681da177e4SLinus Torvalds 		goto out;
969ce1a8e67SChuck Lever 
970ce1a8e67SChuck Lever 	retval = -EINVAL;
971ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9721da177e4SLinus Torvalds 		goto out;
9731da177e4SLinus Torvalds 	retval = 0;
9741da177e4SLinus Torvalds 	if (!count)
9751da177e4SLinus Torvalds 		goto out;
976ce1a8e67SChuck Lever 
97729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9781da177e4SLinus Torvalds 	if (retval)
9791da177e4SLinus Torvalds 		goto out;
9801da177e4SLinus Torvalds 
981c216fd70SChuck Lever 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
9829eafa8ccSChuck Lever 
9831da177e4SLinus Torvalds 	if (retval > 0)
984ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds out:
9871da177e4SLinus Torvalds 	return retval;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99088467055SChuck Lever /**
99188467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
99288467055SChuck Lever  *
99388467055SChuck Lever  */
994f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9971da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
998fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
999fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
100020c2df83SPaul Mundt 						NULL);
10011da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10021da177e4SLinus Torvalds 		return -ENOMEM;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	return 0;
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
100788467055SChuck Lever /**
1008f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
100988467055SChuck Lever  *
101088467055SChuck Lever  */
1011266bee88SDavid Brownell void nfs_destroy_directcache(void)
10121da177e4SLinus Torvalds {
10131a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10141da177e4SLinus Torvalds }
1015