xref: /openbmc/linux/fs/nfs/direct.c (revision cd841605f7a721878d8a2d1362484723d8abf569)
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>
475a0e3ad6STejun Heo #include <linux/slab.h>
487ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.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/uaccess.h>
5560063497SArun Sharma #include <linux/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 */
72f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7399514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7488467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7515ce4a0cSChuck Lever 
7615ce4a0cSChuck Lever 	/* completion state */
77607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7815ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
7915ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
801da177e4SLinus Torvalds 				error;		/* any reported error */
81d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
82fad61490STrond Myklebust 
83fad61490STrond Myklebust 	/* commit state */
84607f31e8STrond Myklebust 	struct list_head	rewrite_list;	/* saved nfs_write_data structs */
850b7c0153SFred Isaman 	struct nfs_commit_data *commit_data;	/* special write_data for commits */
86fad61490STrond Myklebust 	int			flags;
87fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
88fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
89fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
901da177e4SLinus Torvalds };
911da177e4SLinus Torvalds 
92fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
93607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops;
94607f31e8STrond Myklebust 
95607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
96607f31e8STrond Myklebust {
97607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
98607f31e8STrond Myklebust }
99607f31e8STrond Myklebust 
100607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
101607f31e8STrond Myklebust {
102607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
103607f31e8STrond Myklebust }
104607f31e8STrond Myklebust 
1051da177e4SLinus Torvalds /**
106b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
107b8a32e2bSChuck Lever  * @rw: direction (read or write)
108b8a32e2bSChuck Lever  * @iocb: target I/O control block
109b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
110b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
111b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
112b8a32e2bSChuck Lever  *
113b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
114b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
115b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
116b8a32e2bSChuck Lever  * should never be called.
1171da177e4SLinus Torvalds  */
118b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
119b8a32e2bSChuck Lever {
120b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
12101cce933SJosef "Jeff" Sipek 			iocb->ki_filp->f_path.dentry->d_name.name,
122e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
123b8a32e2bSChuck Lever 
124b8a32e2bSChuck Lever 	return -EINVAL;
125b8a32e2bSChuck Lever }
126b8a32e2bSChuck Lever 
127d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
1286b45d858STrond Myklebust {
129d4a8f367STrond Myklebust 	unsigned int npages;
130749e146eSChuck Lever 	unsigned int i;
131d4a8f367STrond Myklebust 
132d4a8f367STrond Myklebust 	if (count == 0)
133d4a8f367STrond Myklebust 		return;
134d4a8f367STrond Myklebust 	pages += (pgbase >> PAGE_SHIFT);
135d4a8f367STrond Myklebust 	npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1366b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1376b45d858STrond Myklebust 		struct page *page = pages[i];
138607f31e8STrond Myklebust 		if (!PageCompound(page))
139d4a8f367STrond Myklebust 			set_page_dirty(page);
1406b45d858STrond Myklebust 	}
1419c93ab7dSChuck Lever }
1429c93ab7dSChuck Lever 
143749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1449c93ab7dSChuck Lever {
145749e146eSChuck Lever 	unsigned int i;
146607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
147607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1486b45d858STrond Myklebust }
1496b45d858STrond Myklebust 
15093619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1531da177e4SLinus Torvalds 
154e94b1766SChristoph Lameter 	dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
1551da177e4SLinus Torvalds 	if (!dreq)
1561da177e4SLinus Torvalds 		return NULL;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	kref_init(&dreq->kref);
159607f31e8STrond Myklebust 	kref_get(&dreq->kref);
160d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
161fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
16293619e59SChuck Lever 	dreq->iocb = NULL;
163a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
164f11ac8dbSTrond Myklebust 	dreq->l_ctx = NULL;
16515ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
166607f31e8STrond Myklebust 	atomic_set(&dreq->io_count, 0);
16715ce4a0cSChuck Lever 	dreq->count = 0;
16815ce4a0cSChuck Lever 	dreq->error = 0;
169fad61490STrond Myklebust 	dreq->flags = 0;
17093619e59SChuck Lever 
17193619e59SChuck Lever 	return dreq;
17293619e59SChuck Lever }
17393619e59SChuck Lever 
174b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
177a8881f5aSTrond Myklebust 
178f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
179f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
180a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
181a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1821da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
186b4946ffbSTrond Myklebust {
187b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
188b4946ffbSTrond Myklebust }
189b4946ffbSTrond Myklebust 
190d4cc948bSChuck Lever /*
191bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
192bc0fb201SChuck Lever  */
193bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
194bc0fb201SChuck Lever {
19515ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
196bc0fb201SChuck Lever 
197bc0fb201SChuck Lever 	/* Async requests don't wait here */
198bc0fb201SChuck Lever 	if (dreq->iocb)
199bc0fb201SChuck Lever 		goto out;
200bc0fb201SChuck Lever 
201150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
202bc0fb201SChuck Lever 
203bc0fb201SChuck Lever 	if (!result)
20415ce4a0cSChuck Lever 		result = dreq->error;
205bc0fb201SChuck Lever 	if (!result)
20615ce4a0cSChuck Lever 		result = dreq->count;
207bc0fb201SChuck Lever 
208bc0fb201SChuck Lever out:
209bc0fb201SChuck Lever 	return (ssize_t) result;
210bc0fb201SChuck Lever }
211bc0fb201SChuck Lever 
212bc0fb201SChuck Lever /*
213607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
214607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
21563ab46abSChuck Lever  */
21663ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
21763ab46abSChuck Lever {
21863ab46abSChuck Lever 	if (dreq->iocb) {
21915ce4a0cSChuck Lever 		long res = (long) dreq->error;
22063ab46abSChuck Lever 		if (!res)
22115ce4a0cSChuck Lever 			res = (long) dreq->count;
22263ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
223d72b7a6bSTrond Myklebust 	}
224d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
22563ab46abSChuck Lever 
226b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
22763ab46abSChuck Lever }
22863ab46abSChuck Lever 
22963ab46abSChuck Lever /*
230607f31e8STrond Myklebust  * We must hold a reference to all the pages in this direct read request
231607f31e8STrond Myklebust  * until the RPCs complete.  This could be long *after* we are woken up in
232607f31e8STrond Myklebust  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
23306cf6f2eSChuck Lever  */
234ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2351da177e4SLinus Torvalds {
236ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2371da177e4SLinus Torvalds 
238fdd1e74cSTrond Myklebust 	nfs_readpage_result(task, data);
239fdd1e74cSTrond Myklebust }
240fdd1e74cSTrond Myklebust 
241fdd1e74cSTrond Myklebust static void nfs_direct_read_release(void *calldata)
242fdd1e74cSTrond Myklebust {
243fdd1e74cSTrond Myklebust 
244fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
245*cd841605SFred Isaman 	struct nfs_direct_req *dreq = (struct nfs_direct_req *)data->header->req;
246fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
2471da177e4SLinus Torvalds 
24815ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
249fdd1e74cSTrond Myklebust 	if (unlikely(status < 0)) {
250fdd1e74cSTrond Myklebust 		dreq->error = status;
25115ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
252d4a8f367STrond Myklebust 	} else {
253d4a8f367STrond Myklebust 		dreq->count += data->res.count;
254d4a8f367STrond Myklebust 		spin_unlock(&dreq->lock);
255d4a8f367STrond Myklebust 		nfs_direct_dirty_pages(data->pagevec,
256d4a8f367STrond Myklebust 				data->args.pgbase,
257d4a8f367STrond Myklebust 				data->res.count);
258d4a8f367STrond Myklebust 	}
259d4a8f367STrond Myklebust 	nfs_direct_release_pages(data->pagevec, data->npages);
2601da177e4SLinus Torvalds 
261607f31e8STrond Myklebust 	if (put_dreq(dreq))
26263ab46abSChuck Lever 		nfs_direct_complete(dreq);
263a20c6becSFred Isaman 	nfs_readdata_release(data);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
266ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
267f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
268ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
269fdd1e74cSTrond Myklebust 	.rpc_release = nfs_direct_read_release,
270ec06c096STrond Myklebust };
271ec06c096STrond Myklebust 
272*cd841605SFred Isaman static void nfs_direct_readhdr_release(struct nfs_read_header *rhdr)
273*cd841605SFred Isaman {
274*cd841605SFred Isaman 	struct nfs_read_data *data = &rhdr->rpc_data;
275*cd841605SFred Isaman 
276*cd841605SFred Isaman 	if (data->pagevec != data->page_array)
277*cd841605SFred Isaman 		kfree(data->pagevec);
278*cd841605SFred Isaman 	nfs_readhdr_free(&rhdr->header);
279*cd841605SFred Isaman }
280*cd841605SFred Isaman 
281d4cc948bSChuck Lever /*
282607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
283607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
284607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
285607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
286607f31e8STrond Myklebust  * no requests have been sent, just return an error.
2871da177e4SLinus Torvalds  */
28802fe4946SChuck Lever static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq,
28902fe4946SChuck Lever 						const struct iovec *iov,
29002fe4946SChuck Lever 						loff_t pos)
2911da177e4SLinus Torvalds {
292a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
2933d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
29402fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
29502fe4946SChuck Lever 	size_t count = iov->iov_len;
2965dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
29707737691STrond Myklebust 	struct rpc_task *task;
298bdc7f021STrond Myklebust 	struct rpc_message msg = {
299bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
300bdc7f021STrond Myklebust 	};
30184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
30284115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
303bdc7f021STrond Myklebust 		.rpc_message = &msg,
30484115e1cSTrond Myklebust 		.callback_ops = &nfs_read_direct_ops,
305101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
30684115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
30784115e1cSTrond Myklebust 	};
308607f31e8STrond Myklebust 	unsigned int pgbase;
309607f31e8STrond Myklebust 	int result;
310607f31e8STrond Myklebust 	ssize_t started = 0;
31182b145c5SChuck Lever 
3121da177e4SLinus Torvalds 	do {
313*cd841605SFred Isaman 		struct nfs_read_header *rhdr;
31482b145c5SChuck Lever 		struct nfs_read_data *data;
3155dd602f2SChuck Lever 		size_t bytes;
3161da177e4SLinus Torvalds 
317e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
318e9f7bee1STrond Myklebust 		bytes = min(rsize,count);
319e9f7bee1STrond Myklebust 
320607f31e8STrond Myklebust 		result = -ENOMEM;
321*cd841605SFred Isaman 		rhdr = nfs_readhdr_alloc(nfs_page_array_len(pgbase, bytes));
322*cd841605SFred Isaman 		if (unlikely(!rhdr))
323607f31e8STrond Myklebust 			break;
324*cd841605SFred Isaman 		data = &rhdr->rpc_data;
325607f31e8STrond Myklebust 
326607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
327607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
328607f31e8STrond Myklebust 					data->npages, 1, 0, data->pagevec, NULL);
329607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
330749e146eSChuck Lever 		if (result < 0) {
331*cd841605SFred Isaman 			nfs_direct_readhdr_release(rhdr);
332749e146eSChuck Lever 			break;
333749e146eSChuck Lever 		}
334749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
335d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
336d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
337607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
338*cd841605SFred Isaman 				nfs_direct_readhdr_release(rhdr);
339607f31e8STrond Myklebust 				break;
340607f31e8STrond Myklebust 			}
341d9df8d6bSTrond Myklebust 			bytes -= pgbase;
342d9df8d6bSTrond Myklebust 			data->npages = result;
343d9df8d6bSTrond Myklebust 		}
34406cf6f2eSChuck Lever 
345607f31e8STrond Myklebust 		get_dreq(dreq);
346607f31e8STrond Myklebust 
347*cd841605SFred Isaman 		rhdr->header.req = (struct nfs_page *) dreq;
348*cd841605SFred Isaman 		rhdr->header.inode = inode;
349*cd841605SFred Isaman 		rhdr->header.cred = msg.rpc_cred;
3501da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
351a20c6becSFred Isaman 		data->args.context = get_nfs_open_context(ctx);
352f11ac8dbSTrond Myklebust 		data->args.lock_context = dreq->l_ctx;
35388467055SChuck Lever 		data->args.offset = pos;
3541da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
355607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
3561da177e4SLinus Torvalds 		data->args.count = bytes;
3571da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3581da177e4SLinus Torvalds 		data->res.eof = 0;
3591da177e4SLinus Torvalds 		data->res.count = bytes;
36065d26953SChuck Lever 		nfs_fattr_init(&data->fattr);
361bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
362bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
3631da177e4SLinus Torvalds 
36407737691STrond Myklebust 		task_setup_data.task = &data->task;
36584115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
366bdc7f021STrond Myklebust 		NFS_PROTO(inode)->read_setup(data, &msg);
3671da177e4SLinus Torvalds 
36807737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
369dbae4c73STrond Myklebust 		if (IS_ERR(task))
370dbae4c73STrond Myklebust 			break;
3711da177e4SLinus Torvalds 
372a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct read call "
373a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
37431f6852aSFred Isaman 				task->tk_pid,
3751da177e4SLinus Torvalds 				inode->i_sb->s_id,
3761da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3771da177e4SLinus Torvalds 				bytes,
3781da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
37931f6852aSFred Isaman 		rpc_put_task(task);
3801da177e4SLinus Torvalds 
381607f31e8STrond Myklebust 		started += bytes;
382607f31e8STrond Myklebust 		user_addr += bytes;
38388467055SChuck Lever 		pos += bytes;
384e9f7bee1STrond Myklebust 		/* FIXME: Remove this unnecessary math from final patch */
3851da177e4SLinus Torvalds 		pgbase += bytes;
3861da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
387e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		count -= bytes;
3901da177e4SLinus Torvalds 	} while (count != 0);
391607f31e8STrond Myklebust 
392607f31e8STrond Myklebust 	if (started)
393c216fd70SChuck Lever 		return started;
394607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
39506cf6f2eSChuck Lever }
39606cf6f2eSChuck Lever 
39719f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
39819f73787SChuck Lever 					      const struct iovec *iov,
39919f73787SChuck Lever 					      unsigned long nr_segs,
40019f73787SChuck Lever 					      loff_t pos)
40119f73787SChuck Lever {
40219f73787SChuck Lever 	ssize_t result = -EINVAL;
40319f73787SChuck Lever 	size_t requested_bytes = 0;
40419f73787SChuck Lever 	unsigned long seg;
40519f73787SChuck Lever 
40619f73787SChuck Lever 	get_dreq(dreq);
40719f73787SChuck Lever 
40819f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
40919f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
41002fe4946SChuck Lever 		result = nfs_direct_read_schedule_segment(dreq, vec, pos);
41119f73787SChuck Lever 		if (result < 0)
41219f73787SChuck Lever 			break;
41319f73787SChuck Lever 		requested_bytes += result;
41419f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
41519f73787SChuck Lever 			break;
41619f73787SChuck Lever 		pos += vec->iov_len;
41719f73787SChuck Lever 	}
41819f73787SChuck Lever 
419839f7ad6SChuck Lever 	/*
420839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
421839f7ad6SChuck Lever 	 * generic layer handle the completion.
422839f7ad6SChuck Lever 	 */
423839f7ad6SChuck Lever 	if (requested_bytes == 0) {
424839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
425839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
426839f7ad6SChuck Lever 	}
427839f7ad6SChuck Lever 
42819f73787SChuck Lever 	if (put_dreq(dreq))
42919f73787SChuck Lever 		nfs_direct_complete(dreq);
43019f73787SChuck Lever 	return 0;
43119f73787SChuck Lever }
43219f73787SChuck Lever 
433c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
434c216fd70SChuck Lever 			       unsigned long nr_segs, loff_t pos)
4351da177e4SLinus Torvalds {
436f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
43799514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4381da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4391da177e4SLinus Torvalds 
440607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
441f11ac8dbSTrond Myklebust 	if (dreq == NULL)
442f11ac8dbSTrond Myklebust 		goto out;
4431da177e4SLinus Torvalds 
44491d5b470SChuck Lever 	dreq->inode = inode;
445cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
446f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
447f11ac8dbSTrond Myklebust 	if (dreq->l_ctx == NULL)
448f11ac8dbSTrond Myklebust 		goto out_release;
449487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
450487b8372SChuck Lever 		dreq->iocb = iocb;
4511da177e4SLinus Torvalds 
452c216fd70SChuck Lever 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
453607f31e8STrond Myklebust 	if (!result)
454bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
455f11ac8dbSTrond Myklebust out_release:
456b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
457f11ac8dbSTrond Myklebust out:
4581da177e4SLinus Torvalds 	return result;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
461*cd841605SFred Isaman static void nfs_direct_writehdr_release(struct nfs_write_header *whdr)
462*cd841605SFred Isaman {
463*cd841605SFred Isaman 	struct nfs_write_data *data = &whdr->rpc_data;
464*cd841605SFred Isaman 
465*cd841605SFred Isaman 	if (data->pagevec != data->page_array)
466*cd841605SFred Isaman 		kfree(data->pagevec);
467*cd841605SFred Isaman 	nfs_writehdr_free(&whdr->header);
468*cd841605SFred Isaman }
469*cd841605SFred Isaman 
470fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4711da177e4SLinus Torvalds {
472607f31e8STrond Myklebust 	while (!list_empty(&dreq->rewrite_list)) {
473*cd841605SFred Isaman 		struct nfs_pgio_header *hdr = list_entry(dreq->rewrite_list.next, struct nfs_pgio_header, pages);
474*cd841605SFred Isaman 		struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
475*cd841605SFred Isaman 		list_del(&hdr->pages);
476*cd841605SFred Isaman 		nfs_direct_release_pages(whdr->rpc_data.pagevec, whdr->rpc_data.npages);
477*cd841605SFred Isaman 		nfs_direct_writehdr_release(whdr);
478fad61490STrond Myklebust 	}
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
481fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
482fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4831da177e4SLinus Torvalds {
484607f31e8STrond Myklebust 	struct inode *inode = dreq->inode;
485607f31e8STrond Myklebust 	struct list_head *p;
486607f31e8STrond Myklebust 	struct nfs_write_data *data;
487*cd841605SFred Isaman 	struct nfs_pgio_header *hdr;
48807737691STrond Myklebust 	struct rpc_task *task;
489bdc7f021STrond Myklebust 	struct rpc_message msg = {
490bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
491bdc7f021STrond Myklebust 	};
49284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
49384115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
494a8b40bc7STerry Loftin 		.rpc_message = &msg,
49584115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
496101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
49784115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
49884115e1cSTrond Myklebust 	};
4991da177e4SLinus Torvalds 
500fad61490STrond Myklebust 	dreq->count = 0;
501607f31e8STrond Myklebust 	get_dreq(dreq);
5021da177e4SLinus Torvalds 
503607f31e8STrond Myklebust 	list_for_each(p, &dreq->rewrite_list) {
504*cd841605SFred Isaman 		hdr = list_entry(p, struct nfs_pgio_header, pages);
505*cd841605SFred Isaman 		data = &(container_of(hdr, struct nfs_write_header, header))->rpc_data;
506607f31e8STrond Myklebust 
507607f31e8STrond Myklebust 		get_dreq(dreq);
508607f31e8STrond Myklebust 
509bdc7f021STrond Myklebust 		/* Use stable writes */
510bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
511bdc7f021STrond Myklebust 
512607f31e8STrond Myklebust 		/*
513607f31e8STrond Myklebust 		 * Reset data->res.
514607f31e8STrond Myklebust 		 */
515607f31e8STrond Myklebust 		nfs_fattr_init(&data->fattr);
516607f31e8STrond Myklebust 		data->res.count = data->args.count;
517607f31e8STrond Myklebust 		memset(&data->verf, 0, sizeof(data->verf));
518607f31e8STrond Myklebust 
519607f31e8STrond Myklebust 		/*
520607f31e8STrond Myklebust 		 * Reuse data->task; data->args should not have changed
521607f31e8STrond Myklebust 		 * since the original request was sent.
522607f31e8STrond Myklebust 		 */
52307737691STrond Myklebust 		task_setup_data.task = &data->task;
52484115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
525bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
526bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
527bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
528607f31e8STrond Myklebust 
529607f31e8STrond Myklebust 		/*
530607f31e8STrond Myklebust 		 * We're called via an RPC callback, so BKL is already held.
531607f31e8STrond Myklebust 		 */
53207737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
53307737691STrond Myklebust 		if (!IS_ERR(task))
53407737691STrond Myklebust 			rpc_put_task(task);
535607f31e8STrond Myklebust 
536607f31e8STrond Myklebust 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
537607f31e8STrond Myklebust 				data->task.tk_pid,
538607f31e8STrond Myklebust 				inode->i_sb->s_id,
539607f31e8STrond Myklebust 				(long long)NFS_FILEID(inode),
540607f31e8STrond Myklebust 				data->args.count,
541607f31e8STrond Myklebust 				(unsigned long long)data->args.offset);
542607f31e8STrond Myklebust 	}
543607f31e8STrond Myklebust 
544607f31e8STrond Myklebust 	if (put_dreq(dreq))
545607f31e8STrond Myklebust 		nfs_direct_write_complete(dreq, inode);
546fad61490STrond Myklebust }
5471da177e4SLinus Torvalds 
548fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
549fad61490STrond Myklebust {
5500b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
5511da177e4SLinus Torvalds 
552fad61490STrond Myklebust 	/* Call the NFS version-specific code */
553c9d8f89dSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
554c9d8f89dSTrond Myklebust }
555c9d8f89dSTrond Myklebust 
556c9d8f89dSTrond Myklebust static void nfs_direct_commit_release(void *calldata)
557c9d8f89dSTrond Myklebust {
5580b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
5590b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
560c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
561c9d8f89dSTrond Myklebust 
562c9d8f89dSTrond Myklebust 	if (status < 0) {
56360fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
564c9d8f89dSTrond Myklebust 				data->task.tk_pid, status);
565fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
56660fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
567c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
568fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
569fad61490STrond Myklebust 	}
570fad61490STrond Myklebust 
571c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
572fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
5731ae88b2eSTrond Myklebust 	nfs_commit_free(data);
574fad61490STrond Myklebust }
575fad61490STrond Myklebust 
576fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
5770b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
578fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
579c9d8f89dSTrond Myklebust 	.rpc_release = nfs_direct_commit_release,
580fad61490STrond Myklebust };
581fad61490STrond Myklebust 
582fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
583fad61490STrond Myklebust {
5840b7c0153SFred Isaman 	struct nfs_commit_data *data = dreq->commit_data;
58507737691STrond Myklebust 	struct rpc_task *task;
586bdc7f021STrond Myklebust 	struct rpc_message msg = {
587bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
588bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
589bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
590bdc7f021STrond Myklebust 	};
59184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
59207737691STrond Myklebust 		.task = &data->task,
59384115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(dreq->inode),
594bdc7f021STrond Myklebust 		.rpc_message = &msg,
59584115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_direct_ops,
59684115e1cSTrond Myklebust 		.callback_data = data,
597101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
59884115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
59984115e1cSTrond Myklebust 	};
600fad61490STrond Myklebust 
601fad61490STrond Myklebust 	data->inode = dreq->inode;
602bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
603fad61490STrond Myklebust 
604fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
605607f31e8STrond Myklebust 	data->args.offset = 0;
606607f31e8STrond Myklebust 	data->args.count = 0;
607fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
608fad61490STrond Myklebust 	data->res.verf = &data->verf;
60965d26953SChuck Lever 	nfs_fattr_init(&data->fattr);
610fad61490STrond Myklebust 
611bdc7f021STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
612fad61490STrond Myklebust 
613fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
614fad61490STrond Myklebust 	dreq->commit_data = NULL;
615fad61490STrond Myklebust 
616e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
6171da177e4SLinus Torvalds 
61807737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
61907737691STrond Myklebust 	if (!IS_ERR(task))
62007737691STrond Myklebust 		rpc_put_task(task);
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
6241da177e4SLinus Torvalds {
625fad61490STrond Myklebust 	int flags = dreq->flags;
6261da177e4SLinus Torvalds 
627fad61490STrond Myklebust 	dreq->flags = 0;
628fad61490STrond Myklebust 	switch (flags) {
629fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
630fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6311da177e4SLinus Torvalds 			break;
632fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
633fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6341da177e4SLinus Torvalds 			break;
6351da177e4SLinus Torvalds 		default:
636fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
637fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
638fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
639cd9ae2b6STrond Myklebust 			nfs_zap_mapping(inode, inode->i_mapping);
640fad61490STrond Myklebust 			nfs_direct_complete(dreq);
6411da177e4SLinus Torvalds 	}
642fad61490STrond Myklebust }
643fad61490STrond Myklebust 
644fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
645fad61490STrond Myklebust {
646c9d8f89dSTrond Myklebust 	dreq->commit_data = nfs_commitdata_alloc();
647fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
6480b7c0153SFred Isaman 		dreq->commit_data->dreq = dreq;
649fad61490STrond Myklebust }
650fad61490STrond Myklebust #else
651fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
652fad61490STrond Myklebust {
653fad61490STrond Myklebust 	dreq->commit_data = NULL;
654fad61490STrond Myklebust }
655fad61490STrond Myklebust 
656fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
657fad61490STrond Myklebust {
658fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
659cd9ae2b6STrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
660fad61490STrond Myklebust 	nfs_direct_complete(dreq);
661fad61490STrond Myklebust }
662fad61490STrond Myklebust #endif
663fad61490STrond Myklebust 
664462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
665462d5b32SChuck Lever {
666462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
667462d5b32SChuck Lever 
66883762c56SFred Isaman 	nfs_writeback_done(task, data);
669c9d8f89dSTrond Myklebust }
670c9d8f89dSTrond Myklebust 
671c9d8f89dSTrond Myklebust /*
672c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
673c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
674c9d8f89dSTrond Myklebust  */
675c9d8f89dSTrond Myklebust static void nfs_direct_write_release(void *calldata)
676c9d8f89dSTrond Myklebust {
677c9d8f89dSTrond Myklebust 	struct nfs_write_data *data = calldata;
678*cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
679*cd841605SFred Isaman 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) hdr->req;
680c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
681462d5b32SChuck Lever 
68215ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
683462d5b32SChuck Lever 
68460fa3f76STrond Myklebust 	if (unlikely(status < 0)) {
685432409eeSNeil Brown 		/* An error has occurred, so we should not commit */
68660fa3f76STrond Myklebust 		dreq->flags = 0;
68760fa3f76STrond Myklebust 		dreq->error = status;
688eda3cef8STrond Myklebust 	}
689432409eeSNeil Brown 	if (unlikely(dreq->error != 0))
690432409eeSNeil Brown 		goto out_unlock;
691eda3cef8STrond Myklebust 
69215ce4a0cSChuck Lever 	dreq->count += data->res.count;
69315ce4a0cSChuck Lever 
694fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
695fad61490STrond Myklebust 		switch (dreq->flags) {
696fad61490STrond Myklebust 			case 0:
697fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
698fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
699fad61490STrond Myklebust 				break;
700fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
701fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
702c9d8f89dSTrond Myklebust 					dprintk("NFS: %5u write verify failed\n", data->task.tk_pid);
703fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
704fad61490STrond Myklebust 				}
705fad61490STrond Myklebust 		}
706fad61490STrond Myklebust 	}
707eda3cef8STrond Myklebust out_unlock:
708fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
709fad61490STrond Myklebust 
710607f31e8STrond Myklebust 	if (put_dreq(dreq))
711*cd841605SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
712462d5b32SChuck Lever }
713462d5b32SChuck Lever 
714462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
715def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
716462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
717fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
718462d5b32SChuck Lever };
719462d5b32SChuck Lever 
720462d5b32SChuck Lever /*
721607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
722607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
723607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
724607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
725607f31e8STrond Myklebust  * no requests have been sent, just return an error.
726462d5b32SChuck Lever  */
72702fe4946SChuck Lever static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq,
72802fe4946SChuck Lever 						 const struct iovec *iov,
72902fe4946SChuck Lever 						 loff_t pos, int sync)
730462d5b32SChuck Lever {
731a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
7323d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
73302fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
73402fe4946SChuck Lever 	size_t count = iov->iov_len;
73507737691STrond Myklebust 	struct rpc_task *task;
736bdc7f021STrond Myklebust 	struct rpc_message msg = {
737bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
738bdc7f021STrond Myklebust 	};
73984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
74084115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
741bdc7f021STrond Myklebust 		.rpc_message = &msg,
74284115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
743101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
74484115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
74584115e1cSTrond Myklebust 	};
746462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
747607f31e8STrond Myklebust 	unsigned int pgbase;
748607f31e8STrond Myklebust 	int result;
749607f31e8STrond Myklebust 	ssize_t started = 0;
75082b145c5SChuck Lever 
751462d5b32SChuck Lever 	do {
752*cd841605SFred Isaman 		struct nfs_write_header *whdr;
75382b145c5SChuck Lever 		struct nfs_write_data *data;
754462d5b32SChuck Lever 		size_t bytes;
755462d5b32SChuck Lever 
756e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
757e9f7bee1STrond Myklebust 		bytes = min(wsize,count);
758e9f7bee1STrond Myklebust 
759607f31e8STrond Myklebust 		result = -ENOMEM;
760*cd841605SFred Isaman 		whdr = nfs_writehdr_alloc(nfs_page_array_len(pgbase, bytes));
761*cd841605SFred Isaman 		if (unlikely(!whdr))
762607f31e8STrond Myklebust 			break;
763607f31e8STrond Myklebust 
764*cd841605SFred Isaman 		data = &whdr->rpc_data;
765*cd841605SFred Isaman 
766607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
767607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
768607f31e8STrond Myklebust 					data->npages, 0, 0, data->pagevec, NULL);
769607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
770749e146eSChuck Lever 		if (result < 0) {
771*cd841605SFred Isaman 			nfs_direct_writehdr_release(whdr);
772749e146eSChuck Lever 			break;
773749e146eSChuck Lever 		}
774749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
775d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
776d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
777607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
778*cd841605SFred Isaman 				nfs_direct_writehdr_release(whdr);
779607f31e8STrond Myklebust 				break;
780607f31e8STrond Myklebust 			}
781d9df8d6bSTrond Myklebust 			bytes -= pgbase;
782d9df8d6bSTrond Myklebust 			data->npages = result;
783d9df8d6bSTrond Myklebust 		}
784607f31e8STrond Myklebust 
785607f31e8STrond Myklebust 		get_dreq(dreq);
786607f31e8STrond Myklebust 
787*cd841605SFred Isaman 		list_move_tail(&whdr->header.pages, &dreq->rewrite_list);
788462d5b32SChuck Lever 
789*cd841605SFred Isaman 		whdr->header.req = (struct nfs_page *) dreq;
790*cd841605SFred Isaman 		whdr->header.inode = inode;
791*cd841605SFred Isaman 		whdr->header.cred = msg.rpc_cred;
792462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
7931ae88b2eSTrond Myklebust 		data->args.context = ctx;
794f11ac8dbSTrond Myklebust 		data->args.lock_context = dreq->l_ctx;
79588467055SChuck Lever 		data->args.offset = pos;
796462d5b32SChuck Lever 		data->args.pgbase = pgbase;
797607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
798462d5b32SChuck Lever 		data->args.count = bytes;
799bdc7f021STrond Myklebust 		data->args.stable = sync;
800462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
801462d5b32SChuck Lever 		data->res.count = bytes;
80247989d74SChuck Lever 		data->res.verf = &data->verf;
80365d26953SChuck Lever 		nfs_fattr_init(&data->fattr);
804462d5b32SChuck Lever 
80507737691STrond Myklebust 		task_setup_data.task = &data->task;
80684115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
807bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
808bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
809bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
810462d5b32SChuck Lever 
81107737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
812dbae4c73STrond Myklebust 		if (IS_ERR(task))
813dbae4c73STrond Myklebust 			break;
8141da177e4SLinus Torvalds 
815a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct write call "
816a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
81731f6852aSFred Isaman 				task->tk_pid,
818462d5b32SChuck Lever 				inode->i_sb->s_id,
819462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
820462d5b32SChuck Lever 				bytes,
821462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
82231f6852aSFred Isaman 		rpc_put_task(task);
823462d5b32SChuck Lever 
824607f31e8STrond Myklebust 		started += bytes;
825607f31e8STrond Myklebust 		user_addr += bytes;
82688467055SChuck Lever 		pos += bytes;
827e9f7bee1STrond Myklebust 
828e9f7bee1STrond Myklebust 		/* FIXME: Remove this useless math from the final patch */
829462d5b32SChuck Lever 		pgbase += bytes;
830462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
831e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
832462d5b32SChuck Lever 
833462d5b32SChuck Lever 		count -= bytes;
834462d5b32SChuck Lever 	} while (count != 0);
835607f31e8STrond Myklebust 
836607f31e8STrond Myklebust 	if (started)
837c216fd70SChuck Lever 		return started;
838607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
83906cf6f2eSChuck Lever }
84006cf6f2eSChuck Lever 
84119f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
84219f73787SChuck Lever 					       const struct iovec *iov,
84319f73787SChuck Lever 					       unsigned long nr_segs,
84419f73787SChuck Lever 					       loff_t pos, int sync)
84519f73787SChuck Lever {
84619f73787SChuck Lever 	ssize_t result = 0;
84719f73787SChuck Lever 	size_t requested_bytes = 0;
84819f73787SChuck Lever 	unsigned long seg;
84919f73787SChuck Lever 
85019f73787SChuck Lever 	get_dreq(dreq);
85119f73787SChuck Lever 
85219f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
85319f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
85402fe4946SChuck Lever 		result = nfs_direct_write_schedule_segment(dreq, vec,
85519f73787SChuck Lever 							   pos, sync);
85619f73787SChuck Lever 		if (result < 0)
85719f73787SChuck Lever 			break;
85819f73787SChuck Lever 		requested_bytes += result;
85919f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
86019f73787SChuck Lever 			break;
86119f73787SChuck Lever 		pos += vec->iov_len;
86219f73787SChuck Lever 	}
86319f73787SChuck Lever 
864839f7ad6SChuck Lever 	/*
865839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
866839f7ad6SChuck Lever 	 * generic layer handle the completion.
867839f7ad6SChuck Lever 	 */
868839f7ad6SChuck Lever 	if (requested_bytes == 0) {
869839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
870839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
871839f7ad6SChuck Lever 	}
872839f7ad6SChuck Lever 
87319f73787SChuck Lever 	if (put_dreq(dreq))
87419f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
87519f73787SChuck Lever 	return 0;
87619f73787SChuck Lever }
87719f73787SChuck Lever 
878c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
879c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
880c216fd70SChuck Lever 				size_t count)
881462d5b32SChuck Lever {
882f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
883c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
884462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
885fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
886bdc7f021STrond Myklebust 	int sync = NFS_UNSTABLE;
887462d5b32SChuck Lever 
888607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
889462d5b32SChuck Lever 	if (!dreq)
890f11ac8dbSTrond Myklebust 		goto out;
891607f31e8STrond Myklebust 	nfs_alloc_commit_data(dreq);
892607f31e8STrond Myklebust 
893b47d19deSArun Bharadwaj 	if (dreq->commit_data == NULL || count <= wsize)
894bdc7f021STrond Myklebust 		sync = NFS_FILE_SYNC;
895462d5b32SChuck Lever 
896c89f2ee5SChuck Lever 	dreq->inode = inode;
897cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
898f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
899568a810dSSteve Dickson 	if (dreq->l_ctx == NULL)
900f11ac8dbSTrond Myklebust 		goto out_release;
901c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
902c89f2ee5SChuck Lever 		dreq->iocb = iocb;
903462d5b32SChuck Lever 
904c216fd70SChuck Lever 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync);
905607f31e8STrond Myklebust 	if (!result)
906c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
907f11ac8dbSTrond Myklebust out_release:
908b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
909f11ac8dbSTrond Myklebust out:
9101da177e4SLinus Torvalds 	return result;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /**
9141da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
9151da177e4SLinus Torvalds  * @iocb: target I/O control block
916027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
917027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
91888467055SChuck Lever  * @pos: byte offset in file where reading starts
9191da177e4SLinus Torvalds  *
9201da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
9211da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
9221da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
9231da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
9241da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
92588467055SChuck Lever  * READ where the file size could change.  Our preference is simply
9261da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
9271da177e4SLinus Torvalds  * care of managing the end of file boundary.
9281da177e4SLinus Torvalds  *
9291da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
9301da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
9311da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
9321da177e4SLinus Torvalds  * cache.
9331da177e4SLinus Torvalds  */
934027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
935027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
9381da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9391da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
940c216fd70SChuck Lever 	size_t count;
9411da177e4SLinus Torvalds 
942c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
943c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
944c216fd70SChuck Lever 
9456da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
94601cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
94701cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
948c216fd70SChuck Lever 		count, (long long) pos);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	retval = 0;
9511da177e4SLinus Torvalds 	if (!count)
9521da177e4SLinus Torvalds 		goto out;
9531da177e4SLinus Torvalds 
95429884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9551da177e4SLinus Torvalds 	if (retval)
9561da177e4SLinus Torvalds 		goto out;
9571da177e4SLinus Torvalds 
9587ec10f26SKonstantin Khlebnikov 	task_io_account_read(count);
9597ec10f26SKonstantin Khlebnikov 
960c216fd70SChuck Lever 	retval = nfs_direct_read(iocb, iov, nr_segs, pos);
9611da177e4SLinus Torvalds 	if (retval > 0)
9620cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds out:
9651da177e4SLinus Torvalds 	return retval;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds /**
9691da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9701da177e4SLinus Torvalds  * @iocb: target I/O control block
971027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
972027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
97388467055SChuck Lever  * @pos: byte offset in file where writing starts
9741da177e4SLinus Torvalds  *
9751da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9761da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9771da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9781da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9791da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9801da177e4SLinus Torvalds  * parameter checking and report problems.
9811da177e4SLinus Torvalds  *
9821da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9831da177e4SLinus Torvalds  *
9841da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9851da177e4SLinus Torvalds  * readers of this file.
9861da177e4SLinus Torvalds  *
9871da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9881da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9891da177e4SLinus Torvalds  */
990027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
991027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9921da177e4SLinus Torvalds {
993070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9941da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9951da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
996c216fd70SChuck Lever 	size_t count;
9971da177e4SLinus Torvalds 
998c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
999c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
1000c216fd70SChuck Lever 
10016da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
100201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
100301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
1004c216fd70SChuck Lever 		count, (long long) pos);
1005027445c3SBadari Pulavarty 
1006ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
1007ce1a8e67SChuck Lever 	if (retval)
10081da177e4SLinus Torvalds 		goto out;
1009ce1a8e67SChuck Lever 
1010ce1a8e67SChuck Lever 	retval = -EINVAL;
1011ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
10121da177e4SLinus Torvalds 		goto out;
10131da177e4SLinus Torvalds 	retval = 0;
10141da177e4SLinus Torvalds 	if (!count)
10151da177e4SLinus Torvalds 		goto out;
1016ce1a8e67SChuck Lever 
101729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
10181da177e4SLinus Torvalds 	if (retval)
10191da177e4SLinus Torvalds 		goto out;
10201da177e4SLinus Torvalds 
10217ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
10227ec10f26SKonstantin Khlebnikov 
1023c216fd70SChuck Lever 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
10249eafa8ccSChuck Lever 
10251da177e4SLinus Torvalds 	if (retval > 0)
1026ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds out:
10291da177e4SLinus Torvalds 	return retval;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
103288467055SChuck Lever /**
103388467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
103488467055SChuck Lever  *
103588467055SChuck Lever  */
1036f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10391da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1040fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1041fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
104220c2df83SPaul Mundt 						NULL);
10431da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10441da177e4SLinus Torvalds 		return -ENOMEM;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	return 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
104988467055SChuck Lever /**
1050f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
105188467055SChuck Lever  *
105288467055SChuck Lever  */
1053266bee88SDavid Brownell void nfs_destroy_directcache(void)
10541da177e4SLinus Torvalds {
10551a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10561da177e4SLinus Torvalds }
1057