xref: /openbmc/linux/fs/nfs/direct.c (revision eda3cef8dd2b83875affe82595db9d0c278879b2)
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/smp_lock.h>
451da177e4SLinus Torvalds #include <linux/file.h>
461da177e4SLinus Torvalds #include <linux/pagemap.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <linux/nfs_fs.h>
501da177e4SLinus Torvalds #include <linux/nfs_page.h>
511da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <asm/system.h>
541da177e4SLinus Torvalds #include <asm/uaccess.h>
551da177e4SLinus Torvalds #include <asm/atomic.h>
561da177e4SLinus Torvalds 
5791d5b470SChuck Lever #include "iostat.h"
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static kmem_cache_t *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",
119e99170ffSTrond Myklebust 			iocb->ki_filp->f_dentry->d_name.name,
120e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
121b8a32e2bSChuck Lever 
122b8a32e2bSChuck Lever 	return -EINVAL;
123b8a32e2bSChuck Lever }
124b8a32e2bSChuck Lever 
125607f31e8STrond Myklebust static void nfs_direct_dirty_pages(struct page **pages, int npages)
1266b45d858STrond Myklebust {
1276b45d858STrond Myklebust 	int i;
1286b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1296b45d858STrond Myklebust 		struct page *page = pages[i];
130607f31e8STrond Myklebust 		if (!PageCompound(page))
1316b45d858STrond Myklebust 			set_page_dirty_lock(page);
1326b45d858STrond Myklebust 	}
1339c93ab7dSChuck Lever }
1349c93ab7dSChuck Lever 
135607f31e8STrond Myklebust static void nfs_direct_release_pages(struct page **pages, int npages)
1369c93ab7dSChuck Lever {
137607f31e8STrond Myklebust 	int i;
138607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
139607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1406b45d858STrond Myklebust }
1416b45d858STrond Myklebust 
14293619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
1471da177e4SLinus Torvalds 	if (!dreq)
1481da177e4SLinus Torvalds 		return NULL;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	kref_init(&dreq->kref);
151607f31e8STrond Myklebust 	kref_get(&dreq->kref);
152d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
153fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
15493619e59SChuck Lever 	dreq->iocb = NULL;
155a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
15615ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
157607f31e8STrond Myklebust 	atomic_set(&dreq->io_count, 0);
15815ce4a0cSChuck Lever 	dreq->count = 0;
15915ce4a0cSChuck Lever 	dreq->error = 0;
160fad61490STrond Myklebust 	dreq->flags = 0;
16193619e59SChuck Lever 
16293619e59SChuck Lever 	return dreq;
16393619e59SChuck Lever }
16493619e59SChuck Lever 
1651da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
168a8881f5aSTrond Myklebust 
169a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
170a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1711da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
174d4cc948bSChuck Lever /*
175bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
176bc0fb201SChuck Lever  */
177bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
178bc0fb201SChuck Lever {
17915ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
180bc0fb201SChuck Lever 
181bc0fb201SChuck Lever 	/* Async requests don't wait here */
182bc0fb201SChuck Lever 	if (dreq->iocb)
183bc0fb201SChuck Lever 		goto out;
184bc0fb201SChuck Lever 
185d72b7a6bSTrond Myklebust 	result = wait_for_completion_interruptible(&dreq->completion);
186bc0fb201SChuck Lever 
187bc0fb201SChuck Lever 	if (!result)
18815ce4a0cSChuck Lever 		result = dreq->error;
189bc0fb201SChuck Lever 	if (!result)
19015ce4a0cSChuck Lever 		result = dreq->count;
191bc0fb201SChuck Lever 
192bc0fb201SChuck Lever out:
193bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
194bc0fb201SChuck Lever 	return (ssize_t) result;
195bc0fb201SChuck Lever }
196bc0fb201SChuck Lever 
197bc0fb201SChuck Lever /*
198607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
199607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
20063ab46abSChuck Lever  */
20163ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
20263ab46abSChuck Lever {
20363ab46abSChuck Lever 	if (dreq->iocb) {
20415ce4a0cSChuck Lever 		long res = (long) dreq->error;
20563ab46abSChuck Lever 		if (!res)
20615ce4a0cSChuck Lever 			res = (long) dreq->count;
20763ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
208d72b7a6bSTrond Myklebust 	}
209d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
21063ab46abSChuck Lever 
21163ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
21263ab46abSChuck Lever }
21363ab46abSChuck Lever 
21463ab46abSChuck Lever /*
215607f31e8STrond Myklebust  * We must hold a reference to all the pages in this direct read request
216607f31e8STrond Myklebust  * until the RPCs complete.  This could be long *after* we are woken up in
217607f31e8STrond Myklebust  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
21806cf6f2eSChuck Lever  */
219ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2201da177e4SLinus Torvalds {
221ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2221da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
2231da177e4SLinus Torvalds 
224ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
225ec06c096STrond Myklebust 		return;
2261da177e4SLinus Torvalds 
227607f31e8STrond Myklebust 	nfs_direct_dirty_pages(data->pagevec, data->npages);
228607f31e8STrond Myklebust 	nfs_direct_release_pages(data->pagevec, data->npages);
229607f31e8STrond Myklebust 
23015ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
23115ce4a0cSChuck Lever 
23215ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
23315ce4a0cSChuck Lever 		dreq->count += data->res.count;
2341da177e4SLinus Torvalds 	else
23515ce4a0cSChuck Lever 		dreq->error = task->tk_status;
2361da177e4SLinus Torvalds 
23715ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2381da177e4SLinus Torvalds 
239607f31e8STrond Myklebust 	if (put_dreq(dreq))
24063ab46abSChuck Lever 		nfs_direct_complete(dreq);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
243ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
244ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
245ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
246ec06c096STrond Myklebust };
247ec06c096STrond Myklebust 
248d4cc948bSChuck Lever /*
249607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
250607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
251607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
252607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
253607f31e8STrond Myklebust  * no requests have been sent, just return an error.
2541da177e4SLinus Torvalds  */
255607f31e8STrond Myklebust static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
2561da177e4SLinus Torvalds {
257a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
258a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
2595dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
260607f31e8STrond Myklebust 	unsigned int pgbase;
261607f31e8STrond Myklebust 	int result;
262607f31e8STrond Myklebust 	ssize_t started = 0;
26382b145c5SChuck Lever 
264607f31e8STrond Myklebust 	get_dreq(dreq);
265607f31e8STrond Myklebust 
2661da177e4SLinus Torvalds 	do {
26782b145c5SChuck Lever 		struct nfs_read_data *data;
2685dd602f2SChuck Lever 		size_t bytes;
2691da177e4SLinus Torvalds 
270e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
271e9f7bee1STrond Myklebust 		bytes = min(rsize,count);
272e9f7bee1STrond Myklebust 
273607f31e8STrond Myklebust 		result = -ENOMEM;
274e9f7bee1STrond Myklebust 		data = nfs_readdata_alloc(pgbase + bytes);
275607f31e8STrond Myklebust 		if (unlikely(!data))
276607f31e8STrond Myklebust 			break;
277607f31e8STrond Myklebust 
278607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
279607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
280607f31e8STrond Myklebust 					data->npages, 1, 0, data->pagevec, NULL);
281607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
282607f31e8STrond Myklebust 		if (unlikely(result < data->npages)) {
283607f31e8STrond Myklebust 			if (result > 0)
284607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
285607f31e8STrond Myklebust 			nfs_readdata_release(data);
286607f31e8STrond Myklebust 			break;
287607f31e8STrond Myklebust 		}
28806cf6f2eSChuck Lever 
289607f31e8STrond Myklebust 		get_dreq(dreq);
290607f31e8STrond Myklebust 
291607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
2921da177e4SLinus Torvalds 		data->inode = inode;
2931da177e4SLinus Torvalds 		data->cred = ctx->cred;
2941da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
2951da177e4SLinus Torvalds 		data->args.context = ctx;
29688467055SChuck Lever 		data->args.offset = pos;
2971da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
298607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
2991da177e4SLinus Torvalds 		data->args.count = bytes;
3001da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3011da177e4SLinus Torvalds 		data->res.eof = 0;
3021da177e4SLinus Torvalds 		data->res.count = bytes;
3031da177e4SLinus Torvalds 
304ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
305ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3061da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 		lock_kernel();
3111da177e4SLinus Torvalds 		rpc_execute(&data->task);
3121da177e4SLinus Torvalds 		unlock_kernel();
3131da177e4SLinus Torvalds 
314606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
3151da177e4SLinus Torvalds 				data->task.tk_pid,
3161da177e4SLinus Torvalds 				inode->i_sb->s_id,
3171da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3181da177e4SLinus Torvalds 				bytes,
3191da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3201da177e4SLinus Torvalds 
321607f31e8STrond Myklebust 		started += bytes;
322607f31e8STrond Myklebust 		user_addr += bytes;
32388467055SChuck Lever 		pos += bytes;
324e9f7bee1STrond Myklebust 		/* FIXME: Remove this unnecessary math from final patch */
3251da177e4SLinus Torvalds 		pgbase += bytes;
3261da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
327e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 		count -= bytes;
3301da177e4SLinus Torvalds 	} while (count != 0);
331607f31e8STrond Myklebust 
332607f31e8STrond Myklebust 	if (put_dreq(dreq))
333607f31e8STrond Myklebust 		nfs_direct_complete(dreq);
334607f31e8STrond Myklebust 
335607f31e8STrond Myklebust 	if (started)
336607f31e8STrond Myklebust 		return 0;
337607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
33806cf6f2eSChuck Lever }
33906cf6f2eSChuck Lever 
340607f31e8STrond Myklebust static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
3411da177e4SLinus Torvalds {
342607f31e8STrond Myklebust 	ssize_t result = 0;
3431da177e4SLinus Torvalds 	sigset_t oldset;
34499514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3451da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
3461da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3471da177e4SLinus Torvalds 
348607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
3491da177e4SLinus Torvalds 	if (!dreq)
3501da177e4SLinus Torvalds 		return -ENOMEM;
3511da177e4SLinus Torvalds 
35291d5b470SChuck Lever 	dreq->inode = inode;
353a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
354487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
355487b8372SChuck Lever 		dreq->iocb = iocb;
3561da177e4SLinus Torvalds 
35791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
3581da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
359607f31e8STrond Myklebust 	result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
360607f31e8STrond Myklebust 	if (!result)
361bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
3621da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	return result;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
3681da177e4SLinus Torvalds {
369607f31e8STrond Myklebust 	while (!list_empty(&dreq->rewrite_list)) {
370607f31e8STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
371fad61490STrond Myklebust 		list_del(&data->pages);
372607f31e8STrond Myklebust 		nfs_direct_release_pages(data->pagevec, data->npages);
373fad61490STrond Myklebust 		nfs_writedata_release(data);
374fad61490STrond Myklebust 	}
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
377fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
378fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
3791da177e4SLinus Torvalds {
380607f31e8STrond Myklebust 	struct inode *inode = dreq->inode;
381607f31e8STrond Myklebust 	struct list_head *p;
382607f31e8STrond Myklebust 	struct nfs_write_data *data;
3831da177e4SLinus Torvalds 
384fad61490STrond Myklebust 	dreq->count = 0;
385607f31e8STrond Myklebust 	get_dreq(dreq);
3861da177e4SLinus Torvalds 
387607f31e8STrond Myklebust 	list_for_each(p, &dreq->rewrite_list) {
388607f31e8STrond Myklebust 		data = list_entry(p, struct nfs_write_data, pages);
389607f31e8STrond Myklebust 
390607f31e8STrond Myklebust 		get_dreq(dreq);
391607f31e8STrond Myklebust 
392607f31e8STrond Myklebust 		/*
393607f31e8STrond Myklebust 		 * Reset data->res.
394607f31e8STrond Myklebust 		 */
395607f31e8STrond Myklebust 		nfs_fattr_init(&data->fattr);
396607f31e8STrond Myklebust 		data->res.count = data->args.count;
397607f31e8STrond Myklebust 		memset(&data->verf, 0, sizeof(data->verf));
398607f31e8STrond Myklebust 
399607f31e8STrond Myklebust 		/*
400607f31e8STrond Myklebust 		 * Reuse data->task; data->args should not have changed
401607f31e8STrond Myklebust 		 * since the original request was sent.
402607f31e8STrond Myklebust 		 */
403607f31e8STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
404607f31e8STrond Myklebust 				&nfs_write_direct_ops, data);
405607f31e8STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
406607f31e8STrond Myklebust 
407607f31e8STrond Myklebust 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
408607f31e8STrond Myklebust 		data->task.tk_cookie = (unsigned long) inode;
409607f31e8STrond Myklebust 
410607f31e8STrond Myklebust 		/*
411607f31e8STrond Myklebust 		 * We're called via an RPC callback, so BKL is already held.
412607f31e8STrond Myklebust 		 */
413607f31e8STrond Myklebust 		rpc_execute(&data->task);
414607f31e8STrond Myklebust 
415607f31e8STrond Myklebust 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
416607f31e8STrond Myklebust 				data->task.tk_pid,
417607f31e8STrond Myklebust 				inode->i_sb->s_id,
418607f31e8STrond Myklebust 				(long long)NFS_FILEID(inode),
419607f31e8STrond Myklebust 				data->args.count,
420607f31e8STrond Myklebust 				(unsigned long long)data->args.offset);
421607f31e8STrond Myklebust 	}
422607f31e8STrond Myklebust 
423607f31e8STrond Myklebust 	if (put_dreq(dreq))
424607f31e8STrond Myklebust 		nfs_direct_write_complete(dreq, inode);
425fad61490STrond Myklebust }
4261da177e4SLinus Torvalds 
427fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
428fad61490STrond Myklebust {
429fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
430fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
4311da177e4SLinus Torvalds 
432fad61490STrond Myklebust 	/* Call the NFS version-specific code */
433fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
434fad61490STrond Myklebust 		return;
435fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
436fad61490STrond Myklebust 		dreq->error = task->tk_status;
437fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
438fad61490STrond Myklebust 	}
439fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
440fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
441fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
442fad61490STrond Myklebust 	}
443fad61490STrond Myklebust 
444fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
445fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
446fad61490STrond Myklebust }
447fad61490STrond Myklebust 
448fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
449fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
450fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
451fad61490STrond Myklebust };
452fad61490STrond Myklebust 
453fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
454fad61490STrond Myklebust {
455fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
456fad61490STrond Myklebust 
457fad61490STrond Myklebust 	data->inode = dreq->inode;
458a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
459fad61490STrond Myklebust 
460fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
461607f31e8STrond Myklebust 	data->args.offset = 0;
462607f31e8STrond Myklebust 	data->args.count = 0;
463fad61490STrond Myklebust 	data->res.count = 0;
464fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
465fad61490STrond Myklebust 	data->res.verf = &data->verf;
466fad61490STrond Myklebust 
467fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
468fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
469fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
470fad61490STrond Myklebust 
471fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
472fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
473fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
474fad61490STrond Myklebust 	dreq->commit_data = NULL;
475fad61490STrond Myklebust 
476e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	lock_kernel();
479fad61490STrond Myklebust 	rpc_execute(&data->task);
4801da177e4SLinus Torvalds 	unlock_kernel();
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
483fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
4841da177e4SLinus Torvalds {
485fad61490STrond Myklebust 	int flags = dreq->flags;
4861da177e4SLinus Torvalds 
487fad61490STrond Myklebust 	dreq->flags = 0;
488fad61490STrond Myklebust 	switch (flags) {
489fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
490fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
4911da177e4SLinus Torvalds 			break;
492fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
493fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
4941da177e4SLinus Torvalds 			break;
4951da177e4SLinus Torvalds 		default:
496fad61490STrond Myklebust 			nfs_end_data_update(inode);
497fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
498fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
499fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
500fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5011da177e4SLinus Torvalds 	}
502fad61490STrond Myklebust }
503fad61490STrond Myklebust 
504fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
505fad61490STrond Myklebust {
506e9f7bee1STrond Myklebust 	dreq->commit_data = nfs_commit_alloc();
507fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
508fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
509fad61490STrond Myklebust }
510fad61490STrond Myklebust #else
511fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
512fad61490STrond Myklebust {
513fad61490STrond Myklebust 	dreq->commit_data = NULL;
514fad61490STrond Myklebust }
515fad61490STrond Myklebust 
516fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
517fad61490STrond Myklebust {
518fad61490STrond Myklebust 	nfs_end_data_update(inode);
519fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
520fad61490STrond Myklebust 	nfs_direct_complete(dreq);
521fad61490STrond Myklebust }
522fad61490STrond Myklebust #endif
523fad61490STrond Myklebust 
524462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
525462d5b32SChuck Lever {
526462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
527462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
528462d5b32SChuck Lever 	int status = task->tk_status;
529462d5b32SChuck Lever 
530462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
531462d5b32SChuck Lever 		return;
532462d5b32SChuck Lever 
53315ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
534462d5b32SChuck Lever 
535*eda3cef8STrond Myklebust 	if (unlikely(status < 0)) {
536*eda3cef8STrond Myklebust 		dreq->error = status;
537*eda3cef8STrond Myklebust 		goto out_unlock;
538*eda3cef8STrond Myklebust 	}
539*eda3cef8STrond Myklebust 
54015ce4a0cSChuck Lever 	dreq->count += data->res.count;
54115ce4a0cSChuck Lever 
542fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
543fad61490STrond Myklebust 		switch (dreq->flags) {
544fad61490STrond Myklebust 			case 0:
545fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
546fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
547fad61490STrond Myklebust 				break;
548fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
549fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
550fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
551fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
552fad61490STrond Myklebust 				}
553fad61490STrond Myklebust 		}
554fad61490STrond Myklebust 	}
555*eda3cef8STrond Myklebust out_unlock:
556fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
557fad61490STrond Myklebust }
558fad61490STrond Myklebust 
559fad61490STrond Myklebust /*
560fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
561fad61490STrond Myklebust  *     errors after the first one are ignored.
562fad61490STrond Myklebust  */
563fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
564fad61490STrond Myklebust {
565fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
566fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
567fad61490STrond Myklebust 
568607f31e8STrond Myklebust 	if (put_dreq(dreq))
569fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
570462d5b32SChuck Lever }
571462d5b32SChuck Lever 
572462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
573462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
574fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
575462d5b32SChuck Lever };
576462d5b32SChuck Lever 
577462d5b32SChuck Lever /*
578607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
579607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
580607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
581607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
582607f31e8STrond Myklebust  * no requests have been sent, just return an error.
583462d5b32SChuck Lever  */
584607f31e8STrond Myklebust static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
585462d5b32SChuck Lever {
586a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
587a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
588462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
589607f31e8STrond Myklebust 	unsigned int pgbase;
590607f31e8STrond Myklebust 	int result;
591607f31e8STrond Myklebust 	ssize_t started = 0;
59282b145c5SChuck Lever 
593607f31e8STrond Myklebust 	get_dreq(dreq);
594607f31e8STrond Myklebust 
595462d5b32SChuck Lever 	do {
59682b145c5SChuck Lever 		struct nfs_write_data *data;
597462d5b32SChuck Lever 		size_t bytes;
598462d5b32SChuck Lever 
599e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
600e9f7bee1STrond Myklebust 		bytes = min(wsize,count);
601e9f7bee1STrond Myklebust 
602607f31e8STrond Myklebust 		result = -ENOMEM;
603e9f7bee1STrond Myklebust 		data = nfs_writedata_alloc(pgbase + bytes);
604607f31e8STrond Myklebust 		if (unlikely(!data))
605607f31e8STrond Myklebust 			break;
606607f31e8STrond Myklebust 
607607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
608607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
609607f31e8STrond Myklebust 					data->npages, 0, 0, data->pagevec, NULL);
610607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
611607f31e8STrond Myklebust 		if (unlikely(result < data->npages)) {
612607f31e8STrond Myklebust 			if (result > 0)
613607f31e8STrond Myklebust 				nfs_direct_release_pages(data->pagevec, result);
614607f31e8STrond Myklebust 			nfs_writedata_release(data);
615607f31e8STrond Myklebust 			break;
616607f31e8STrond Myklebust 		}
617607f31e8STrond Myklebust 
618607f31e8STrond Myklebust 		get_dreq(dreq);
619607f31e8STrond Myklebust 
620fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
621462d5b32SChuck Lever 
622607f31e8STrond Myklebust 		data->req = (struct nfs_page *) dreq;
623462d5b32SChuck Lever 		data->inode = inode;
624462d5b32SChuck Lever 		data->cred = ctx->cred;
625462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
626462d5b32SChuck Lever 		data->args.context = ctx;
62788467055SChuck Lever 		data->args.offset = pos;
628462d5b32SChuck Lever 		data->args.pgbase = pgbase;
629607f31e8STrond Myklebust 		data->args.pages = data->pagevec;
630462d5b32SChuck Lever 		data->args.count = bytes;
631462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
632462d5b32SChuck Lever 		data->res.count = bytes;
63347989d74SChuck Lever 		data->res.verf = &data->verf;
634462d5b32SChuck Lever 
635462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
636462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
637fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
638462d5b32SChuck Lever 
639462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
640462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 		lock_kernel();
643462d5b32SChuck Lever 		rpc_execute(&data->task);
6441da177e4SLinus Torvalds 		unlock_kernel();
6451da177e4SLinus Torvalds 
646606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
647462d5b32SChuck Lever 				data->task.tk_pid,
648462d5b32SChuck Lever 				inode->i_sb->s_id,
649462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
650462d5b32SChuck Lever 				bytes,
651462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
652462d5b32SChuck Lever 
653607f31e8STrond Myklebust 		started += bytes;
654607f31e8STrond Myklebust 		user_addr += bytes;
65588467055SChuck Lever 		pos += bytes;
656e9f7bee1STrond Myklebust 
657e9f7bee1STrond Myklebust 		/* FIXME: Remove this useless math from the final patch */
658462d5b32SChuck Lever 		pgbase += bytes;
659462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
660e9f7bee1STrond Myklebust 		BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
661462d5b32SChuck Lever 
662462d5b32SChuck Lever 		count -= bytes;
663462d5b32SChuck Lever 	} while (count != 0);
664607f31e8STrond Myklebust 
665607f31e8STrond Myklebust 	if (put_dreq(dreq))
666607f31e8STrond Myklebust 		nfs_direct_write_complete(dreq, inode);
667607f31e8STrond Myklebust 
668607f31e8STrond Myklebust 	if (started)
669607f31e8STrond Myklebust 		return 0;
670607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
67106cf6f2eSChuck Lever }
67206cf6f2eSChuck Lever 
673607f31e8STrond Myklebust static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
674462d5b32SChuck Lever {
675607f31e8STrond Myklebust 	ssize_t result = 0;
676462d5b32SChuck Lever 	sigset_t oldset;
677c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
678462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
679462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
680fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
681fad61490STrond Myklebust 	int sync = 0;
682462d5b32SChuck Lever 
683607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
684462d5b32SChuck Lever 	if (!dreq)
685462d5b32SChuck Lever 		return -ENOMEM;
686607f31e8STrond Myklebust 	nfs_alloc_commit_data(dreq);
687607f31e8STrond Myklebust 
688fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
689fad61490STrond Myklebust 		sync = FLUSH_STABLE;
690462d5b32SChuck Lever 
691c89f2ee5SChuck Lever 	dreq->inode = inode;
692a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
693c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
694c89f2ee5SChuck Lever 		dreq->iocb = iocb;
695462d5b32SChuck Lever 
69647989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
69747989d74SChuck Lever 
698462d5b32SChuck Lever 	nfs_begin_data_update(inode);
699462d5b32SChuck Lever 
700462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
701607f31e8STrond Myklebust 	result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
702607f31e8STrond Myklebust 	if (!result)
703c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
704462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
705462d5b32SChuck Lever 
7061da177e4SLinus Torvalds 	return result;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /**
7101da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7111da177e4SLinus Torvalds  * @iocb: target I/O control block
712027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
713027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
71488467055SChuck Lever  * @pos: byte offset in file where reading starts
7151da177e4SLinus Torvalds  *
7161da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7171da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7181da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7191da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7201da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
72188467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7221da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7231da177e4SLinus Torvalds  * care of managing the end of file boundary.
7241da177e4SLinus Torvalds  *
7251da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7261da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7271da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
7281da177e4SLinus Torvalds  * cache.
7291da177e4SLinus Torvalds  */
730027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
731027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
7341da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7351da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
736027445c3SBadari Pulavarty 	/* XXX: temporary */
737027445c3SBadari Pulavarty 	const char __user *buf = iov[0].iov_base;
738027445c3SBadari Pulavarty 	size_t count = iov[0].iov_len;
7391da177e4SLinus Torvalds 
740ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
7410bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7420bbacc40SChuck Lever 		file->f_dentry->d_name.name,
743ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
7441da177e4SLinus Torvalds 
745027445c3SBadari Pulavarty 	if (nr_segs != 1)
746027445c3SBadari Pulavarty 		return -EINVAL;
747027445c3SBadari Pulavarty 
7481da177e4SLinus Torvalds 	if (count < 0)
7491da177e4SLinus Torvalds 		goto out;
7501da177e4SLinus Torvalds 	retval = -EFAULT;
7510cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
7521da177e4SLinus Torvalds 		goto out;
7531da177e4SLinus Torvalds 	retval = 0;
7541da177e4SLinus Torvalds 	if (!count)
7551da177e4SLinus Torvalds 		goto out;
7561da177e4SLinus Torvalds 
75729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
7581da177e4SLinus Torvalds 	if (retval)
7591da177e4SLinus Torvalds 		goto out;
7601da177e4SLinus Torvalds 
761607f31e8STrond Myklebust 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
7621da177e4SLinus Torvalds 	if (retval > 0)
7630cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds out:
7661da177e4SLinus Torvalds 	return retval;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds /**
7701da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
7711da177e4SLinus Torvalds  * @iocb: target I/O control block
772027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
773027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
77488467055SChuck Lever  * @pos: byte offset in file where writing starts
7751da177e4SLinus Torvalds  *
7761da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
7771da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
7781da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
7791da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
7801da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
7811da177e4SLinus Torvalds  * parameter checking and report problems.
7821da177e4SLinus Torvalds  *
7831da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
7841da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
7851da177e4SLinus Torvalds  *
7861da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
7871da177e4SLinus Torvalds  *
7881da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
7891da177e4SLinus Torvalds  * readers of this file.
7901da177e4SLinus Torvalds  *
7911da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
7921da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
7931da177e4SLinus Torvalds  */
794027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
795027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
7961da177e4SLinus Torvalds {
797ce1a8e67SChuck Lever 	ssize_t retval;
7981da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7991da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
800027445c3SBadari Pulavarty 	/* XXX: temporary */
801027445c3SBadari Pulavarty 	const char __user *buf = iov[0].iov_base;
802027445c3SBadari Pulavarty 	size_t count = iov[0].iov_len;
8031da177e4SLinus Torvalds 
804ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8050bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
806ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
807ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8081da177e4SLinus Torvalds 
809027445c3SBadari Pulavarty 	if (nr_segs != 1)
810027445c3SBadari Pulavarty 		return -EINVAL;
811027445c3SBadari Pulavarty 
812ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
813ce1a8e67SChuck Lever 	if (retval)
8141da177e4SLinus Torvalds 		goto out;
815ce1a8e67SChuck Lever 
816ce1a8e67SChuck Lever 	retval = -EINVAL;
817ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8181da177e4SLinus Torvalds 		goto out;
8191da177e4SLinus Torvalds 	retval = 0;
8201da177e4SLinus Torvalds 	if (!count)
8211da177e4SLinus Torvalds 		goto out;
822ce1a8e67SChuck Lever 
823ce1a8e67SChuck Lever 	retval = -EFAULT;
82447989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
825ce1a8e67SChuck Lever 		goto out;
8261da177e4SLinus Torvalds 
82729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8281da177e4SLinus Torvalds 	if (retval)
8291da177e4SLinus Torvalds 		goto out;
8301da177e4SLinus Torvalds 
831607f31e8STrond Myklebust 	retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
8329eafa8ccSChuck Lever 
8339eafa8ccSChuck Lever 	/*
8349eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
8359eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
8369eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
8379eafa8ccSChuck Lever 	 *
8389eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
8399eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
8409eafa8ccSChuck Lever 	 */
8411da177e4SLinus Torvalds 	if (mapping->nrpages)
8421da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
8439eafa8ccSChuck Lever 
8441da177e4SLinus Torvalds 	if (retval > 0)
845ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds out:
8481da177e4SLinus Torvalds 	return retval;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
85188467055SChuck Lever /**
85288467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
85388467055SChuck Lever  *
85488467055SChuck Lever  */
855f7b422b1SDavid Howells int __init nfs_init_directcache(void)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
8581da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
859fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
860fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
8611da177e4SLinus Torvalds 						NULL, NULL);
8621da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
8631da177e4SLinus Torvalds 		return -ENOMEM;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	return 0;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
86888467055SChuck Lever /**
869f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
87088467055SChuck Lever  *
87188467055SChuck Lever  */
872266bee88SDavid Brownell void nfs_destroy_directcache(void)
8731da177e4SLinus Torvalds {
8741a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
8751da177e4SLinus Torvalds }
876