xref: /openbmc/linux/fs/nfs/direct.c (revision dbae4c73f08b8a7980cc912954ade3d4c1fb6147)
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>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include <linux/nfs_fs.h>
491da177e4SLinus Torvalds #include <linux/nfs_page.h>
501da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <asm/system.h>
531da177e4SLinus Torvalds #include <asm/uaccess.h>
541da177e4SLinus Torvalds #include <asm/atomic.h>
551da177e4SLinus Torvalds 
568d5658c9STrond Myklebust #include "internal.h"
5791d5b470SChuck Lever #include "iostat.h"
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
601da177e4SLinus Torvalds 
61e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /*
641da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds struct nfs_direct_req {
671da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
6815ce4a0cSChuck Lever 
6915ce4a0cSChuck Lever 	/* I/O parameters */
70a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
7199514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7288467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7315ce4a0cSChuck Lever 
7415ce4a0cSChuck Lever 	/* completion state */
75607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7615ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
7715ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
781da177e4SLinus Torvalds 				error;		/* any reported error */
79d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
80fad61490STrond Myklebust 
81fad61490STrond Myklebust 	/* commit state */
82607f31e8STrond Myklebust 	struct list_head	rewrite_list;	/* saved nfs_write_data structs */
83fad61490STrond Myklebust 	struct nfs_write_data *	commit_data;	/* special write_data for commits */
84fad61490STrond Myklebust 	int			flags;
85fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
86fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
87fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
90fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
91607f31e8STrond Myklebust static const struct rpc_call_ops nfs_write_direct_ops;
92607f31e8STrond Myklebust 
93607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
94607f31e8STrond Myklebust {
95607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
96607f31e8STrond Myklebust }
97607f31e8STrond Myklebust 
98607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
99607f31e8STrond Myklebust {
100607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
101607f31e8STrond Myklebust }
102607f31e8STrond Myklebust 
1031da177e4SLinus Torvalds /**
104b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
105b8a32e2bSChuck Lever  * @rw: direction (read or write)
106b8a32e2bSChuck Lever  * @iocb: target I/O control block
107b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
108b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
109b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
110b8a32e2bSChuck Lever  *
111b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
112b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
113b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
114b8a32e2bSChuck Lever  * should never be called.
1151da177e4SLinus Torvalds  */
116b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
117b8a32e2bSChuck Lever {
118b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
11901cce933SJosef "Jeff" Sipek 			iocb->ki_filp->f_path.dentry->d_name.name,
120e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
121b8a32e2bSChuck Lever 
122b8a32e2bSChuck Lever 	return -EINVAL;
123b8a32e2bSChuck Lever }
124b8a32e2bSChuck Lever 
125d4a8f367STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
1266b45d858STrond Myklebust {
127d4a8f367STrond Myklebust 	unsigned int npages;
128749e146eSChuck Lever 	unsigned int i;
129d4a8f367STrond Myklebust 
130d4a8f367STrond Myklebust 	if (count == 0)
131d4a8f367STrond Myklebust 		return;
132d4a8f367STrond Myklebust 	pages += (pgbase >> PAGE_SHIFT);
133d4a8f367STrond Myklebust 	npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1346b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1356b45d858STrond Myklebust 		struct page *page = pages[i];
136607f31e8STrond Myklebust 		if (!PageCompound(page))
137d4a8f367STrond Myklebust 			set_page_dirty(page);
1386b45d858STrond Myklebust 	}
1399c93ab7dSChuck Lever }
1409c93ab7dSChuck Lever 
141749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1429c93ab7dSChuck Lever {
143749e146eSChuck Lever 	unsigned int i;
144607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
145607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1466b45d858STrond Myklebust }
1476b45d858STrond Myklebust 
14893619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1511da177e4SLinus Torvalds 
152e94b1766SChristoph Lameter 	dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
1531da177e4SLinus Torvalds 	if (!dreq)
1541da177e4SLinus Torvalds 		return NULL;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	kref_init(&dreq->kref);
157607f31e8STrond Myklebust 	kref_get(&dreq->kref);
158d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
159fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
16093619e59SChuck Lever 	dreq->iocb = NULL;
161a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
16215ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
163607f31e8STrond Myklebust 	atomic_set(&dreq->io_count, 0);
16415ce4a0cSChuck Lever 	dreq->count = 0;
16515ce4a0cSChuck Lever 	dreq->error = 0;
166fad61490STrond Myklebust 	dreq->flags = 0;
16793619e59SChuck Lever 
16893619e59SChuck Lever 	return dreq;
16993619e59SChuck Lever }
17093619e59SChuck Lever 
171b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
174a8881f5aSTrond Myklebust 
175a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
176a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1771da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
181b4946ffbSTrond Myklebust {
182b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
183b4946ffbSTrond Myklebust }
184b4946ffbSTrond Myklebust 
185d4cc948bSChuck Lever /*
186bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
187bc0fb201SChuck Lever  */
188bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
189bc0fb201SChuck Lever {
19015ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
191bc0fb201SChuck Lever 
192bc0fb201SChuck Lever 	/* Async requests don't wait here */
193bc0fb201SChuck Lever 	if (dreq->iocb)
194bc0fb201SChuck Lever 		goto out;
195bc0fb201SChuck Lever 
196150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
197bc0fb201SChuck Lever 
198bc0fb201SChuck Lever 	if (!result)
19915ce4a0cSChuck Lever 		result = dreq->error;
200bc0fb201SChuck Lever 	if (!result)
20115ce4a0cSChuck Lever 		result = dreq->count;
202bc0fb201SChuck Lever 
203bc0fb201SChuck Lever out:
204bc0fb201SChuck Lever 	return (ssize_t) result;
205bc0fb201SChuck Lever }
206bc0fb201SChuck Lever 
207bc0fb201SChuck Lever /*
208607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
209607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
21063ab46abSChuck Lever  */
21163ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
21263ab46abSChuck Lever {
21363ab46abSChuck Lever 	if (dreq->iocb) {
21415ce4a0cSChuck Lever 		long res = (long) dreq->error;
21563ab46abSChuck Lever 		if (!res)
21615ce4a0cSChuck Lever 			res = (long) dreq->count;
21763ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
218d72b7a6bSTrond Myklebust 	}
219d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
22063ab46abSChuck Lever 
221b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
22263ab46abSChuck Lever }
22363ab46abSChuck Lever 
22463ab46abSChuck Lever /*
225607f31e8STrond Myklebust  * We must hold a reference to all the pages in this direct read request
226607f31e8STrond Myklebust  * until the RPCs complete.  This could be long *after* we are woken up in
227607f31e8STrond Myklebust  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
22806cf6f2eSChuck Lever  */
229ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2301da177e4SLinus Torvalds {
231ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2321da177e4SLinus Torvalds 
233fdd1e74cSTrond Myklebust 	nfs_readpage_result(task, data);
234fdd1e74cSTrond Myklebust }
235fdd1e74cSTrond Myklebust 
236fdd1e74cSTrond Myklebust static void nfs_direct_read_release(void *calldata)
237fdd1e74cSTrond Myklebust {
238fdd1e74cSTrond Myklebust 
239fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
240fdd1e74cSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
241fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
2421da177e4SLinus Torvalds 
24315ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
244fdd1e74cSTrond Myklebust 	if (unlikely(status < 0)) {
245fdd1e74cSTrond Myklebust 		dreq->error = status;
24615ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
247d4a8f367STrond Myklebust 	} else {
248d4a8f367STrond Myklebust 		dreq->count += data->res.count;
249d4a8f367STrond Myklebust 		spin_unlock(&dreq->lock);
250d4a8f367STrond Myklebust 		nfs_direct_dirty_pages(data->pagevec,
251d4a8f367STrond Myklebust 				data->args.pgbase,
252d4a8f367STrond Myklebust 				data->res.count);
253d4a8f367STrond Myklebust 	}
254d4a8f367STrond Myklebust 	nfs_direct_release_pages(data->pagevec, data->npages);
2551da177e4SLinus Torvalds 
256607f31e8STrond Myklebust 	if (put_dreq(dreq))
25763ab46abSChuck Lever 		nfs_direct_complete(dreq);
258fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
261ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
262ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
263fdd1e74cSTrond Myklebust 	.rpc_release = nfs_direct_read_release,
264ec06c096STrond Myklebust };
265ec06c096STrond Myklebust 
266d4cc948bSChuck Lever /*
267607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
268607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
269607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
270607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
271607f31e8STrond Myklebust  * no requests have been sent, just return an error.
2721da177e4SLinus Torvalds  */
27302fe4946SChuck Lever static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq,
27402fe4946SChuck Lever 						const struct iovec *iov,
27502fe4946SChuck Lever 						loff_t pos)
2761da177e4SLinus Torvalds {
277a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
27888be9f99STrond Myklebust 	struct inode *inode = ctx->path.dentry->d_inode;
27902fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
28002fe4946SChuck Lever 	size_t count = iov->iov_len;
2815dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
28207737691STrond Myklebust 	struct rpc_task *task;
283bdc7f021STrond Myklebust 	struct rpc_message msg = {
284bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
285bdc7f021STrond Myklebust 	};
28684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
28784115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
288bdc7f021STrond Myklebust 		.rpc_message = &msg,
28984115e1cSTrond Myklebust 		.callback_ops = &nfs_read_direct_ops,
290101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
29184115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
29284115e1cSTrond Myklebust 	};
293607f31e8STrond Myklebust 	unsigned int pgbase;
294607f31e8STrond Myklebust 	int result;
295607f31e8STrond Myklebust 	ssize_t started = 0;
29682b145c5SChuck Lever 
2971da177e4SLinus Torvalds 	do {
29882b145c5SChuck Lever 		struct nfs_read_data *data;
2995dd602f2SChuck Lever 		size_t bytes;
3001da177e4SLinus Torvalds 
301e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
302e9f7bee1STrond Myklebust 		bytes = min(rsize,count);
303e9f7bee1STrond Myklebust 
304607f31e8STrond Myklebust 		result = -ENOMEM;
3058d5658c9STrond Myklebust 		data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes));
306607f31e8STrond Myklebust 		if (unlikely(!data))
307607f31e8STrond Myklebust 			break;
308607f31e8STrond Myklebust 
309607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
310607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
311607f31e8STrond Myklebust 					data->npages, 1, 0, data->pagevec, NULL);
312607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
313749e146eSChuck Lever 		if (result < 0) {
314749e146eSChuck Lever 			nfs_readdata_release(data);
315749e146eSChuck Lever 			break;
316749e146eSChuck Lever 		}
317749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
318d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
319d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
320607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
321607f31e8STrond Myklebust 				nfs_readdata_release(data);
322607f31e8STrond Myklebust 				break;
323607f31e8STrond Myklebust 			}
324d9df8d6bSTrond Myklebust 			bytes -= pgbase;
325d9df8d6bSTrond Myklebust 			data->npages = result;
326d9df8d6bSTrond Myklebust 		}
32706cf6f2eSChuck Lever 
328607f31e8STrond Myklebust 		get_dreq(dreq);
329607f31e8STrond Myklebust 
330607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
3311da177e4SLinus Torvalds 		data->inode = inode;
332bdc7f021STrond Myklebust 		data->cred = msg.rpc_cred;
3331da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
334383ba719STrond Myklebust 		data->args.context = get_nfs_open_context(ctx);
33588467055SChuck Lever 		data->args.offset = pos;
3361da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
337607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
3381da177e4SLinus Torvalds 		data->args.count = bytes;
3391da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3401da177e4SLinus Torvalds 		data->res.eof = 0;
3411da177e4SLinus Torvalds 		data->res.count = bytes;
342bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
343bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
3441da177e4SLinus Torvalds 
34507737691STrond Myklebust 		task_setup_data.task = &data->task;
34684115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
347bdc7f021STrond Myklebust 		NFS_PROTO(inode)->read_setup(data, &msg);
3481da177e4SLinus Torvalds 
34907737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
350*dbae4c73STrond Myklebust 		if (IS_ERR(task))
351*dbae4c73STrond Myklebust 			break;
35207737691STrond Myklebust 		rpc_put_task(task);
3531da177e4SLinus Torvalds 
354a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct read call "
355a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
3561da177e4SLinus Torvalds 				data->task.tk_pid,
3571da177e4SLinus Torvalds 				inode->i_sb->s_id,
3581da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3591da177e4SLinus Torvalds 				bytes,
3601da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3611da177e4SLinus Torvalds 
362607f31e8STrond Myklebust 		started += bytes;
363607f31e8STrond Myklebust 		user_addr += bytes;
36488467055SChuck Lever 		pos += bytes;
365e9f7bee1STrond Myklebust 		/* FIXME: Remove this unnecessary math from final patch */
3661da177e4SLinus Torvalds 		pgbase += bytes;
3671da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
368e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		count -= bytes;
3711da177e4SLinus Torvalds 	} while (count != 0);
372607f31e8STrond Myklebust 
373607f31e8STrond Myklebust 	if (started)
374c216fd70SChuck Lever 		return started;
375607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
37606cf6f2eSChuck Lever }
37706cf6f2eSChuck Lever 
37819f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
37919f73787SChuck Lever 					      const struct iovec *iov,
38019f73787SChuck Lever 					      unsigned long nr_segs,
38119f73787SChuck Lever 					      loff_t pos)
38219f73787SChuck Lever {
38319f73787SChuck Lever 	ssize_t result = -EINVAL;
38419f73787SChuck Lever 	size_t requested_bytes = 0;
38519f73787SChuck Lever 	unsigned long seg;
38619f73787SChuck Lever 
38719f73787SChuck Lever 	get_dreq(dreq);
38819f73787SChuck Lever 
38919f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
39019f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
39102fe4946SChuck Lever 		result = nfs_direct_read_schedule_segment(dreq, vec, pos);
39219f73787SChuck Lever 		if (result < 0)
39319f73787SChuck Lever 			break;
39419f73787SChuck Lever 		requested_bytes += result;
39519f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
39619f73787SChuck Lever 			break;
39719f73787SChuck Lever 		pos += vec->iov_len;
39819f73787SChuck Lever 	}
39919f73787SChuck Lever 
40019f73787SChuck Lever 	if (put_dreq(dreq))
40119f73787SChuck Lever 		nfs_direct_complete(dreq);
40219f73787SChuck Lever 
40319f73787SChuck Lever 	if (requested_bytes != 0)
40419f73787SChuck Lever 		return 0;
40519f73787SChuck Lever 
40619f73787SChuck Lever 	if (result < 0)
40719f73787SChuck Lever 		return result;
40819f73787SChuck Lever 	return -EIO;
40919f73787SChuck Lever }
41019f73787SChuck Lever 
411c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
412c216fd70SChuck Lever 			       unsigned long nr_segs, loff_t pos)
4131da177e4SLinus Torvalds {
414607f31e8STrond Myklebust 	ssize_t result = 0;
41599514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4161da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4171da177e4SLinus Torvalds 
418607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
4191da177e4SLinus Torvalds 	if (!dreq)
4201da177e4SLinus Torvalds 		return -ENOMEM;
4211da177e4SLinus Torvalds 
42291d5b470SChuck Lever 	dreq->inode = inode;
423cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
424487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
425487b8372SChuck Lever 		dreq->iocb = iocb;
4261da177e4SLinus Torvalds 
427c216fd70SChuck Lever 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
428607f31e8STrond Myklebust 	if (!result)
429bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
430b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	return result;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4361da177e4SLinus Torvalds {
437607f31e8STrond Myklebust 	while (!list_empty(&dreq->rewrite_list)) {
438607f31e8STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
439fad61490STrond Myklebust 		list_del(&data->pages);
440607f31e8STrond Myklebust 		nfs_direct_release_pages(data->pagevec, data->npages);
441fad61490STrond Myklebust 		nfs_writedata_release(data);
442fad61490STrond Myklebust 	}
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
445fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
446fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4471da177e4SLinus Torvalds {
448607f31e8STrond Myklebust 	struct inode *inode = dreq->inode;
449607f31e8STrond Myklebust 	struct list_head *p;
450607f31e8STrond Myklebust 	struct nfs_write_data *data;
45107737691STrond Myklebust 	struct rpc_task *task;
452bdc7f021STrond Myklebust 	struct rpc_message msg = {
453bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
454bdc7f021STrond Myklebust 	};
45584115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
45684115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
45784115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
458101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
45984115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
46084115e1cSTrond Myklebust 	};
4611da177e4SLinus Torvalds 
462fad61490STrond Myklebust 	dreq->count = 0;
463607f31e8STrond Myklebust 	get_dreq(dreq);
4641da177e4SLinus Torvalds 
465607f31e8STrond Myklebust 	list_for_each(p, &dreq->rewrite_list) {
466607f31e8STrond Myklebust 		data = list_entry(p, struct nfs_write_data, pages);
467607f31e8STrond Myklebust 
468607f31e8STrond Myklebust 		get_dreq(dreq);
469607f31e8STrond Myklebust 
470bdc7f021STrond Myklebust 		/* Use stable writes */
471bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
472bdc7f021STrond Myklebust 
473607f31e8STrond Myklebust 		/*
474607f31e8STrond Myklebust 		 * Reset data->res.
475607f31e8STrond Myklebust 		 */
476607f31e8STrond Myklebust 		nfs_fattr_init(&data->fattr);
477607f31e8STrond Myklebust 		data->res.count = data->args.count;
478607f31e8STrond Myklebust 		memset(&data->verf, 0, sizeof(data->verf));
479607f31e8STrond Myklebust 
480607f31e8STrond Myklebust 		/*
481607f31e8STrond Myklebust 		 * Reuse data->task; data->args should not have changed
482607f31e8STrond Myklebust 		 * since the original request was sent.
483607f31e8STrond Myklebust 		 */
48407737691STrond Myklebust 		task_setup_data.task = &data->task;
48584115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
486bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
487bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
488bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
489607f31e8STrond Myklebust 
490607f31e8STrond Myklebust 		/*
491607f31e8STrond Myklebust 		 * We're called via an RPC callback, so BKL is already held.
492607f31e8STrond Myklebust 		 */
49307737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
49407737691STrond Myklebust 		if (!IS_ERR(task))
49507737691STrond Myklebust 			rpc_put_task(task);
496607f31e8STrond Myklebust 
497607f31e8STrond Myklebust 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
498607f31e8STrond Myklebust 				data->task.tk_pid,
499607f31e8STrond Myklebust 				inode->i_sb->s_id,
500607f31e8STrond Myklebust 				(long long)NFS_FILEID(inode),
501607f31e8STrond Myklebust 				data->args.count,
502607f31e8STrond Myklebust 				(unsigned long long)data->args.offset);
503607f31e8STrond Myklebust 	}
504607f31e8STrond Myklebust 
505607f31e8STrond Myklebust 	if (put_dreq(dreq))
506607f31e8STrond Myklebust 		nfs_direct_write_complete(dreq, inode);
507fad61490STrond Myklebust }
5081da177e4SLinus Torvalds 
509fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
510fad61490STrond Myklebust {
511fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
5121da177e4SLinus Torvalds 
513fad61490STrond Myklebust 	/* Call the NFS version-specific code */
514c9d8f89dSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
515c9d8f89dSTrond Myklebust }
516c9d8f89dSTrond Myklebust 
517c9d8f89dSTrond Myklebust static void nfs_direct_commit_release(void *calldata)
518c9d8f89dSTrond Myklebust {
519c9d8f89dSTrond Myklebust 	struct nfs_write_data *data = calldata;
520c9d8f89dSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
521c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
522c9d8f89dSTrond Myklebust 
523c9d8f89dSTrond Myklebust 	if (status < 0) {
52460fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
525c9d8f89dSTrond Myklebust 				data->task.tk_pid, status);
526fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
52760fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
528c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
529fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
530fad61490STrond Myklebust 	}
531fad61490STrond Myklebust 
532c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
533fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
534c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
535fad61490STrond Myklebust }
536fad61490STrond Myklebust 
537fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
538fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
539c9d8f89dSTrond Myklebust 	.rpc_release = nfs_direct_commit_release,
540fad61490STrond Myklebust };
541fad61490STrond Myklebust 
542fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
543fad61490STrond Myklebust {
544fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
54507737691STrond Myklebust 	struct rpc_task *task;
546bdc7f021STrond Myklebust 	struct rpc_message msg = {
547bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
548bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
549bdc7f021STrond Myklebust 		.rpc_cred = dreq->ctx->cred,
550bdc7f021STrond Myklebust 	};
55184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
55207737691STrond Myklebust 		.task = &data->task,
55384115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(dreq->inode),
554bdc7f021STrond Myklebust 		.rpc_message = &msg,
55584115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_direct_ops,
55684115e1cSTrond Myklebust 		.callback_data = data,
557101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
55884115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
55984115e1cSTrond Myklebust 	};
560fad61490STrond Myklebust 
561fad61490STrond Myklebust 	data->inode = dreq->inode;
562bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
563fad61490STrond Myklebust 
564fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
565607f31e8STrond Myklebust 	data->args.offset = 0;
566607f31e8STrond Myklebust 	data->args.count = 0;
567383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(dreq->ctx);
568fad61490STrond Myklebust 	data->res.count = 0;
569fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
570fad61490STrond Myklebust 	data->res.verf = &data->verf;
571fad61490STrond Myklebust 
572bdc7f021STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
573fad61490STrond Myklebust 
574fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
575fad61490STrond Myklebust 	dreq->commit_data = NULL;
576fad61490STrond Myklebust 
577e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
5781da177e4SLinus Torvalds 
57907737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
58007737691STrond Myklebust 	if (!IS_ERR(task))
58107737691STrond Myklebust 		rpc_put_task(task);
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
584fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
5851da177e4SLinus Torvalds {
586fad61490STrond Myklebust 	int flags = dreq->flags;
5871da177e4SLinus Torvalds 
588fad61490STrond Myklebust 	dreq->flags = 0;
589fad61490STrond Myklebust 	switch (flags) {
590fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
591fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5921da177e4SLinus Torvalds 			break;
593fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
594fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5951da177e4SLinus Torvalds 			break;
5961da177e4SLinus Torvalds 		default:
597fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
598fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
599fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
600cd9ae2b6STrond Myklebust 			nfs_zap_mapping(inode, inode->i_mapping);
601fad61490STrond Myklebust 			nfs_direct_complete(dreq);
6021da177e4SLinus Torvalds 	}
603fad61490STrond Myklebust }
604fad61490STrond Myklebust 
605fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
606fad61490STrond Myklebust {
607c9d8f89dSTrond Myklebust 	dreq->commit_data = nfs_commitdata_alloc();
608fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
609fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
610fad61490STrond Myklebust }
611fad61490STrond Myklebust #else
612fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
613fad61490STrond Myklebust {
614fad61490STrond Myklebust 	dreq->commit_data = NULL;
615fad61490STrond Myklebust }
616fad61490STrond Myklebust 
617fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
618fad61490STrond Myklebust {
619fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
620cd9ae2b6STrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
621fad61490STrond Myklebust 	nfs_direct_complete(dreq);
622fad61490STrond Myklebust }
623fad61490STrond Myklebust #endif
624fad61490STrond Myklebust 
625462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
626462d5b32SChuck Lever {
627462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
628462d5b32SChuck Lever 
629462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
630462d5b32SChuck Lever 		return;
631c9d8f89dSTrond Myklebust }
632c9d8f89dSTrond Myklebust 
633c9d8f89dSTrond Myklebust /*
634c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
635c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
636c9d8f89dSTrond Myklebust  */
637c9d8f89dSTrond Myklebust static void nfs_direct_write_release(void *calldata)
638c9d8f89dSTrond Myklebust {
639c9d8f89dSTrond Myklebust 	struct nfs_write_data *data = calldata;
640c9d8f89dSTrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
641c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
642462d5b32SChuck Lever 
64315ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
644462d5b32SChuck Lever 
64560fa3f76STrond Myklebust 	if (unlikely(status < 0)) {
646432409eeSNeil Brown 		/* An error has occurred, so we should not commit */
64760fa3f76STrond Myklebust 		dreq->flags = 0;
64860fa3f76STrond Myklebust 		dreq->error = status;
649eda3cef8STrond Myklebust 	}
650432409eeSNeil Brown 	if (unlikely(dreq->error != 0))
651432409eeSNeil Brown 		goto out_unlock;
652eda3cef8STrond Myklebust 
65315ce4a0cSChuck Lever 	dreq->count += data->res.count;
65415ce4a0cSChuck Lever 
655fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
656fad61490STrond Myklebust 		switch (dreq->flags) {
657fad61490STrond Myklebust 			case 0:
658fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
659fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
660fad61490STrond Myklebust 				break;
661fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
662fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
663c9d8f89dSTrond Myklebust 					dprintk("NFS: %5u write verify failed\n", data->task.tk_pid);
664fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
665fad61490STrond Myklebust 				}
666fad61490STrond Myklebust 		}
667fad61490STrond Myklebust 	}
668eda3cef8STrond Myklebust out_unlock:
669fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
670fad61490STrond Myklebust 
671607f31e8STrond Myklebust 	if (put_dreq(dreq))
672fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
673462d5b32SChuck Lever }
674462d5b32SChuck Lever 
675462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
676462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
677fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
678462d5b32SChuck Lever };
679462d5b32SChuck Lever 
680462d5b32SChuck Lever /*
681607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
682607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
683607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
684607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
685607f31e8STrond Myklebust  * no requests have been sent, just return an error.
686462d5b32SChuck Lever  */
68702fe4946SChuck Lever static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq,
68802fe4946SChuck Lever 						 const struct iovec *iov,
68902fe4946SChuck Lever 						 loff_t pos, int sync)
690462d5b32SChuck Lever {
691a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
69288be9f99STrond Myklebust 	struct inode *inode = ctx->path.dentry->d_inode;
69302fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
69402fe4946SChuck Lever 	size_t count = iov->iov_len;
69507737691STrond Myklebust 	struct rpc_task *task;
696bdc7f021STrond Myklebust 	struct rpc_message msg = {
697bdc7f021STrond Myklebust 		.rpc_cred = ctx->cred,
698bdc7f021STrond Myklebust 	};
69984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
70084115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
701bdc7f021STrond Myklebust 		.rpc_message = &msg,
70284115e1cSTrond Myklebust 		.callback_ops = &nfs_write_direct_ops,
703101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
70484115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
70584115e1cSTrond Myklebust 	};
706462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
707607f31e8STrond Myklebust 	unsigned int pgbase;
708607f31e8STrond Myklebust 	int result;
709607f31e8STrond Myklebust 	ssize_t started = 0;
71082b145c5SChuck Lever 
711462d5b32SChuck Lever 	do {
71282b145c5SChuck Lever 		struct nfs_write_data *data;
713462d5b32SChuck Lever 		size_t bytes;
714462d5b32SChuck Lever 
715e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
716e9f7bee1STrond Myklebust 		bytes = min(wsize,count);
717e9f7bee1STrond Myklebust 
718607f31e8STrond Myklebust 		result = -ENOMEM;
7198d5658c9STrond Myklebust 		data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes));
720607f31e8STrond Myklebust 		if (unlikely(!data))
721607f31e8STrond Myklebust 			break;
722607f31e8STrond Myklebust 
723607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
724607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
725607f31e8STrond Myklebust 					data->npages, 0, 0, data->pagevec, NULL);
726607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
727749e146eSChuck Lever 		if (result < 0) {
728749e146eSChuck Lever 			nfs_writedata_release(data);
729749e146eSChuck Lever 			break;
730749e146eSChuck Lever 		}
731749e146eSChuck Lever 		if ((unsigned)result < data->npages) {
732d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
733d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
734607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
735607f31e8STrond Myklebust 				nfs_writedata_release(data);
736607f31e8STrond Myklebust 				break;
737607f31e8STrond Myklebust 			}
738d9df8d6bSTrond Myklebust 			bytes -= pgbase;
739d9df8d6bSTrond Myklebust 			data->npages = result;
740d9df8d6bSTrond Myklebust 		}
741607f31e8STrond Myklebust 
742607f31e8STrond Myklebust 		get_dreq(dreq);
743607f31e8STrond Myklebust 
744fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
745462d5b32SChuck Lever 
746607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
747462d5b32SChuck Lever 		data->inode = inode;
748bdc7f021STrond Myklebust 		data->cred = msg.rpc_cred;
749462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
750383ba719STrond Myklebust 		data->args.context = get_nfs_open_context(ctx);
75188467055SChuck Lever 		data->args.offset = pos;
752462d5b32SChuck Lever 		data->args.pgbase = pgbase;
753607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
754462d5b32SChuck Lever 		data->args.count = bytes;
755bdc7f021STrond Myklebust 		data->args.stable = sync;
756462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
757462d5b32SChuck Lever 		data->res.count = bytes;
75847989d74SChuck Lever 		data->res.verf = &data->verf;
759462d5b32SChuck Lever 
76007737691STrond Myklebust 		task_setup_data.task = &data->task;
76184115e1cSTrond Myklebust 		task_setup_data.callback_data = data;
762bdc7f021STrond Myklebust 		msg.rpc_argp = &data->args;
763bdc7f021STrond Myklebust 		msg.rpc_resp = &data->res;
764bdc7f021STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, &msg);
765462d5b32SChuck Lever 
76607737691STrond Myklebust 		task = rpc_run_task(&task_setup_data);
767*dbae4c73STrond Myklebust 		if (IS_ERR(task))
768*dbae4c73STrond Myklebust 			break;
76907737691STrond Myklebust 		rpc_put_task(task);
7701da177e4SLinus Torvalds 
771a3f565b1SChuck Lever 		dprintk("NFS: %5u initiated direct write call "
772a3f565b1SChuck Lever 			"(req %s/%Ld, %zu bytes @ offset %Lu)\n",
773462d5b32SChuck Lever 				data->task.tk_pid,
774462d5b32SChuck Lever 				inode->i_sb->s_id,
775462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
776462d5b32SChuck Lever 				bytes,
777462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
778462d5b32SChuck Lever 
779607f31e8STrond Myklebust 		started += bytes;
780607f31e8STrond Myklebust 		user_addr += bytes;
78188467055SChuck Lever 		pos += bytes;
782e9f7bee1STrond Myklebust 
783e9f7bee1STrond Myklebust 		/* FIXME: Remove this useless math from the final patch */
784462d5b32SChuck Lever 		pgbase += bytes;
785462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
786e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
787462d5b32SChuck Lever 
788462d5b32SChuck Lever 		count -= bytes;
789462d5b32SChuck Lever 	} while (count != 0);
790607f31e8STrond Myklebust 
791607f31e8STrond Myklebust 	if (started)
792c216fd70SChuck Lever 		return started;
793607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
79406cf6f2eSChuck Lever }
79506cf6f2eSChuck Lever 
79619f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
79719f73787SChuck Lever 					       const struct iovec *iov,
79819f73787SChuck Lever 					       unsigned long nr_segs,
79919f73787SChuck Lever 					       loff_t pos, int sync)
80019f73787SChuck Lever {
80119f73787SChuck Lever 	ssize_t result = 0;
80219f73787SChuck Lever 	size_t requested_bytes = 0;
80319f73787SChuck Lever 	unsigned long seg;
80419f73787SChuck Lever 
80519f73787SChuck Lever 	get_dreq(dreq);
80619f73787SChuck Lever 
80719f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
80819f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
80902fe4946SChuck Lever 		result = nfs_direct_write_schedule_segment(dreq, vec,
81019f73787SChuck Lever 							   pos, sync);
81119f73787SChuck Lever 		if (result < 0)
81219f73787SChuck Lever 			break;
81319f73787SChuck Lever 		requested_bytes += result;
81419f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
81519f73787SChuck Lever 			break;
81619f73787SChuck Lever 		pos += vec->iov_len;
81719f73787SChuck Lever 	}
81819f73787SChuck Lever 
81919f73787SChuck Lever 	if (put_dreq(dreq))
82019f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
82119f73787SChuck Lever 
82219f73787SChuck Lever 	if (requested_bytes != 0)
82319f73787SChuck Lever 		return 0;
82419f73787SChuck Lever 
82519f73787SChuck Lever 	if (result < 0)
82619f73787SChuck Lever 		return result;
82719f73787SChuck Lever 	return -EIO;
82819f73787SChuck Lever }
82919f73787SChuck Lever 
830c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
831c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
832c216fd70SChuck Lever 				size_t count)
833462d5b32SChuck Lever {
834607f31e8STrond Myklebust 	ssize_t result = 0;
835c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
836462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
837fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
838bdc7f021STrond Myklebust 	int sync = NFS_UNSTABLE;
839462d5b32SChuck Lever 
840607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
841462d5b32SChuck Lever 	if (!dreq)
842462d5b32SChuck Lever 		return -ENOMEM;
843607f31e8STrond Myklebust 	nfs_alloc_commit_data(dreq);
844607f31e8STrond Myklebust 
845fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
846bdc7f021STrond Myklebust 		sync = NFS_FILE_SYNC;
847462d5b32SChuck Lever 
848c89f2ee5SChuck Lever 	dreq->inode = inode;
849cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
850c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
851c89f2ee5SChuck Lever 		dreq->iocb = iocb;
852462d5b32SChuck Lever 
853c216fd70SChuck Lever 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync);
854607f31e8STrond Myklebust 	if (!result)
855c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
856b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
857462d5b32SChuck Lever 
8581da177e4SLinus Torvalds 	return result;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /**
8621da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8631da177e4SLinus Torvalds  * @iocb: target I/O control block
864027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
865027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
86688467055SChuck Lever  * @pos: byte offset in file where reading starts
8671da177e4SLinus Torvalds  *
8681da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
8691da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
8701da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
8711da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
8721da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
87388467055SChuck Lever  * READ where the file size could change.  Our preference is simply
8741da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
8751da177e4SLinus Torvalds  * care of managing the end of file boundary.
8761da177e4SLinus Torvalds  *
8771da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
8781da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
8791da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8801da177e4SLinus Torvalds  * cache.
8811da177e4SLinus Torvalds  */
882027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
883027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
8841da177e4SLinus Torvalds {
8851da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
8861da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8871da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
888c216fd70SChuck Lever 	size_t count;
8891da177e4SLinus Torvalds 
890c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
891c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
892c216fd70SChuck Lever 
893c216fd70SChuck Lever 	dprintk("nfs: direct read(%s/%s, %zd@%Ld)\n",
89401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
89501cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
896c216fd70SChuck Lever 		count, (long long) pos);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	retval = 0;
8991da177e4SLinus Torvalds 	if (!count)
9001da177e4SLinus Torvalds 		goto out;
9011da177e4SLinus Torvalds 
90229884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9031da177e4SLinus Torvalds 	if (retval)
9041da177e4SLinus Torvalds 		goto out;
9051da177e4SLinus Torvalds 
906c216fd70SChuck Lever 	retval = nfs_direct_read(iocb, iov, nr_segs, pos);
9071da177e4SLinus Torvalds 	if (retval > 0)
9080cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds out:
9111da177e4SLinus Torvalds 	return retval;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds /**
9151da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9161da177e4SLinus Torvalds  * @iocb: target I/O control block
917027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
918027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
91988467055SChuck Lever  * @pos: byte offset in file where writing starts
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9221da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9231da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9241da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9251da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9261da177e4SLinus Torvalds  * parameter checking and report problems.
9271da177e4SLinus Torvalds  *
9281da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
9291da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
9301da177e4SLinus Torvalds  *
9311da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9321da177e4SLinus Torvalds  *
9331da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9341da177e4SLinus Torvalds  * readers of this file.
9351da177e4SLinus Torvalds  *
9361da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9371da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9381da177e4SLinus Torvalds  */
939027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
940027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9411da177e4SLinus Torvalds {
942070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9431da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9441da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
945c216fd70SChuck Lever 	size_t count;
9461da177e4SLinus Torvalds 
947c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
948c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
949c216fd70SChuck Lever 
950c216fd70SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %zd@%Ld)\n",
95101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
95201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
953c216fd70SChuck Lever 		count, (long long) pos);
954027445c3SBadari Pulavarty 
955ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
956ce1a8e67SChuck Lever 	if (retval)
9571da177e4SLinus Torvalds 		goto out;
958ce1a8e67SChuck Lever 
959ce1a8e67SChuck Lever 	retval = -EINVAL;
960ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9611da177e4SLinus Torvalds 		goto out;
9621da177e4SLinus Torvalds 	retval = 0;
9631da177e4SLinus Torvalds 	if (!count)
9641da177e4SLinus Torvalds 		goto out;
965ce1a8e67SChuck Lever 
96629884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9671da177e4SLinus Torvalds 	if (retval)
9681da177e4SLinus Torvalds 		goto out;
9691da177e4SLinus Torvalds 
970c216fd70SChuck Lever 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
9719eafa8ccSChuck Lever 
9721da177e4SLinus Torvalds 	if (retval > 0)
973ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds out:
9761da177e4SLinus Torvalds 	return retval;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
97988467055SChuck Lever /**
98088467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
98188467055SChuck Lever  *
98288467055SChuck Lever  */
983f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9861da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
987fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
988fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
98920c2df83SPaul Mundt 						NULL);
9901da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9911da177e4SLinus Torvalds 		return -ENOMEM;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	return 0;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
99688467055SChuck Lever /**
997f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
99888467055SChuck Lever  *
99988467055SChuck Lever  */
1000266bee88SDavid Brownell void nfs_destroy_directcache(void)
10011da177e4SLinus Torvalds {
10021a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10031da177e4SLinus Torvalds }
1004