xref: /openbmc/linux/fs/nfs/direct.c (revision 5db3a7b2cabe8f0957683f798c4f8fa8605f9ebb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * There are important applications whose performance or correctness
91da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
101da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
111da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
121da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
131da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
141da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
151da177e4SLinus Torvalds  * need to cache the contents of a file.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
181da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
191da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
201da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
211da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
221da177e4SLinus Torvalds  * an application.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
251da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
261da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
271da177e4SLinus Torvalds  * an undocumented mount option.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
301da177e4SLinus Torvalds  * help from Andrew Morton.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
331da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
341da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
351da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
361da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3788467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/config.h>
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
441da177e4SLinus Torvalds #include <linux/kernel.h>
451da177e4SLinus Torvalds #include <linux/smp_lock.h>
461da177e4SLinus Torvalds #include <linux/file.h>
471da177e4SLinus Torvalds #include <linux/pagemap.h>
481da177e4SLinus Torvalds #include <linux/kref.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <linux/nfs_fs.h>
511da177e4SLinus Torvalds #include <linux/nfs_page.h>
521da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <asm/system.h>
551da177e4SLinus Torvalds #include <asm/uaccess.h>
561da177e4SLinus Torvalds #include <asm/atomic.h>
571da177e4SLinus Torvalds 
5891d5b470SChuck Lever #include "iostat.h"
5991d5b470SChuck Lever 
601da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
611da177e4SLinus Torvalds 
62143f412eSTrond Myklebust static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty);
631da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds struct nfs_direct_req {
691da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7015ce4a0cSChuck Lever 
7115ce4a0cSChuck Lever 	/* I/O parameters */
72fad61490STrond Myklebust 	struct list_head	list,		/* nfs_read/write_data structs */
73fad61490STrond Myklebust 				rewrite_list;	/* saved nfs_write_data structs */
74a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
7599514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
761da177e4SLinus Torvalds 	wait_queue_head_t	wait;		/* wait for i/o completion */
7788467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
78fad61490STrond Myklebust 	unsigned long		user_addr;	/* location of user's buffer */
79fad61490STrond Myklebust 	size_t			user_count;	/* total bytes to move */
80fad61490STrond Myklebust 	loff_t			pos;		/* starting offset in file */
811da177e4SLinus Torvalds 	struct page **		pages;		/* pages in our buffer */
821da177e4SLinus Torvalds 	unsigned int		npages;		/* count of pages */
8315ce4a0cSChuck Lever 
8415ce4a0cSChuck Lever 	/* completion state */
8515ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8615ce4a0cSChuck Lever 	int			outstanding;	/* i/os we're waiting for */
8715ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
881da177e4SLinus Torvalds 				error;		/* any reported error */
89fad61490STrond Myklebust 
90fad61490STrond Myklebust 	/* commit state */
91fad61490STrond Myklebust 	struct nfs_write_data *	commit_data;	/* special write_data for commits */
92fad61490STrond Myklebust 	int			flags;
93fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
94fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
95fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
98fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync);
99fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
100fad61490STrond Myklebust 
1011da177e4SLinus Torvalds /**
102b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
103b8a32e2bSChuck Lever  * @rw: direction (read or write)
104b8a32e2bSChuck Lever  * @iocb: target I/O control block
105b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
106b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
107b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
108b8a32e2bSChuck Lever  *
109b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
110b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
111b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
112b8a32e2bSChuck Lever  * should never be called.
113b8a32e2bSChuck Lever  */
114b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
115b8a32e2bSChuck Lever {
116b8a32e2bSChuck Lever 	struct dentry *dentry = iocb->ki_filp->f_dentry;
117b8a32e2bSChuck Lever 
118b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
119b8a32e2bSChuck Lever 			dentry->d_name.name, (long long) pos, nr_segs);
120b8a32e2bSChuck Lever 
121b8a32e2bSChuck Lever 	return -EINVAL;
122b8a32e2bSChuck Lever }
123b8a32e2bSChuck Lever 
124d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	int result = -ENOMEM;
1271da177e4SLinus Torvalds 	unsigned long page_count;
1281da177e4SLinus Torvalds 	size_t array_size;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1311da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1341da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1351da177e4SLinus Torvalds 	if (*pages) {
1361da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1371da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1381da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1391da177e4SLinus Torvalds 					*pages, NULL);
1401da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
141143f412eSTrond Myklebust 		/*
142143f412eSTrond Myklebust 		 * If we got fewer pages than expected from get_user_pages(),
143143f412eSTrond Myklebust 		 * the user buffer runs off the end of a mapping; return EFAULT.
144143f412eSTrond Myklebust 		 */
145143f412eSTrond Myklebust 		if (result >= 0 && result < page_count) {
146143f412eSTrond Myklebust 			nfs_free_user_pages(*pages, result, 0);
147143f412eSTrond Myklebust 			*pages = NULL;
148143f412eSTrond Myklebust 			result = -EFAULT;
149143f412eSTrond Myklebust 		}
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	return result;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
154d4cc948bSChuck Lever static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	int i;
1571da177e4SLinus Torvalds 	for (i = 0; i < npages; i++) {
158566dd606STrond Myklebust 		struct page *page = pages[i];
159566dd606STrond Myklebust 		if (do_dirty && !PageCompound(page))
160566dd606STrond Myklebust 			set_page_dirty_lock(page);
161566dd606STrond Myklebust 		page_cache_release(page);
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 	kfree(pages);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
16693619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
16793619e59SChuck Lever {
16893619e59SChuck Lever 	struct nfs_direct_req *dreq;
16993619e59SChuck Lever 
17093619e59SChuck Lever 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
17193619e59SChuck Lever 	if (!dreq)
17293619e59SChuck Lever 		return NULL;
17393619e59SChuck Lever 
17493619e59SChuck Lever 	kref_init(&dreq->kref);
17593619e59SChuck Lever 	init_waitqueue_head(&dreq->wait);
17693619e59SChuck Lever 	INIT_LIST_HEAD(&dreq->list);
177fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
17893619e59SChuck Lever 	dreq->iocb = NULL;
179a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
18015ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
18115ce4a0cSChuck Lever 	dreq->outstanding = 0;
18215ce4a0cSChuck Lever 	dreq->count = 0;
18315ce4a0cSChuck Lever 	dreq->error = 0;
184fad61490STrond Myklebust 	dreq->flags = 0;
18593619e59SChuck Lever 
18693619e59SChuck Lever 	return dreq;
18793619e59SChuck Lever }
18893619e59SChuck Lever 
1891da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
192a8881f5aSTrond Myklebust 
193a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
194a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1951da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
198d4cc948bSChuck Lever /*
199bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
200bc0fb201SChuck Lever  */
201bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
202bc0fb201SChuck Lever {
20315ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
204bc0fb201SChuck Lever 
205bc0fb201SChuck Lever 	/* Async requests don't wait here */
206bc0fb201SChuck Lever 	if (dreq->iocb)
207bc0fb201SChuck Lever 		goto out;
208bc0fb201SChuck Lever 
20915ce4a0cSChuck Lever 	result = wait_event_interruptible(dreq->wait, (dreq->outstanding == 0));
210bc0fb201SChuck Lever 
211bc0fb201SChuck Lever 	if (!result)
21215ce4a0cSChuck Lever 		result = dreq->error;
213bc0fb201SChuck Lever 	if (!result)
21415ce4a0cSChuck Lever 		result = dreq->count;
215bc0fb201SChuck Lever 
216bc0fb201SChuck Lever out:
217bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
218bc0fb201SChuck Lever 	return (ssize_t) result;
219bc0fb201SChuck Lever }
220bc0fb201SChuck Lever 
221bc0fb201SChuck Lever /*
22263ab46abSChuck Lever  * We must hold a reference to all the pages in this direct read request
22363ab46abSChuck Lever  * until the RPCs complete.  This could be long *after* we are woken up in
22463ab46abSChuck Lever  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
22563ab46abSChuck Lever  *
22663ab46abSChuck Lever  * In addition, synchronous I/O uses a stack-allocated iocb.  Thus we
22763ab46abSChuck Lever  * can't trust the iocb is still valid here if this is a synchronous
22863ab46abSChuck Lever  * request.  If the waiter is woken prematurely, the iocb is long gone.
22963ab46abSChuck Lever  */
23063ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
23163ab46abSChuck Lever {
23263ab46abSChuck Lever 	nfs_free_user_pages(dreq->pages, dreq->npages, 1);
23363ab46abSChuck Lever 
23463ab46abSChuck Lever 	if (dreq->iocb) {
23515ce4a0cSChuck Lever 		long res = (long) dreq->error;
23663ab46abSChuck Lever 		if (!res)
23715ce4a0cSChuck Lever 			res = (long) dreq->count;
23863ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
23963ab46abSChuck Lever 	} else
24063ab46abSChuck Lever 		wake_up(&dreq->wait);
24163ab46abSChuck Lever 
24263ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
24363ab46abSChuck Lever }
24463ab46abSChuck Lever 
24563ab46abSChuck Lever /*
2461da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
2471da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
2481da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
2491da177e4SLinus Torvalds  */
2505dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct list_head *list;
2531da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
25440859d7eSChuck Lever 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2551da177e4SLinus Torvalds 
25693619e59SChuck Lever 	dreq = nfs_direct_req_alloc();
2571da177e4SLinus Torvalds 	if (!dreq)
2581da177e4SLinus Torvalds 		return NULL;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	list = &dreq->list;
2611da177e4SLinus Torvalds 	for(;;) {
26240859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		if (unlikely(!data)) {
2651da177e4SLinus Torvalds 			while (!list_empty(list)) {
2661da177e4SLinus Torvalds 				data = list_entry(list->next,
2671da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
2681da177e4SLinus Torvalds 				list_del(&data->pages);
2691da177e4SLinus Torvalds 				nfs_readdata_free(data);
2701da177e4SLinus Torvalds 			}
2711da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
2721da177e4SLinus Torvalds 			return NULL;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2761da177e4SLinus Torvalds 		list_add(&data->pages, list);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
27915ce4a0cSChuck Lever 		dreq->outstanding++;
2801da177e4SLinus Torvalds 		if (nbytes <= rsize)
2811da177e4SLinus Torvalds 			break;
2821da177e4SLinus Torvalds 		nbytes -= rsize;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 	kref_get(&dreq->kref);
2851da177e4SLinus Torvalds 	return dreq;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
288ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2891da177e4SLinus Torvalds {
290ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
2911da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
2921da177e4SLinus Torvalds 
293ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
294ec06c096STrond Myklebust 		return;
2951da177e4SLinus Torvalds 
29615ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
29715ce4a0cSChuck Lever 
29815ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
29915ce4a0cSChuck Lever 		dreq->count += data->res.count;
30015ce4a0cSChuck Lever 	else
30115ce4a0cSChuck Lever 		dreq->error = task->tk_status;
30215ce4a0cSChuck Lever 
30315ce4a0cSChuck Lever 	if (--dreq->outstanding) {
30415ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
30515ce4a0cSChuck Lever 		return;
30615ce4a0cSChuck Lever 	}
30715ce4a0cSChuck Lever 
30815ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
30963ab46abSChuck Lever 	nfs_direct_complete(dreq);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
312ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
313ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
314ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
315ec06c096STrond Myklebust };
316ec06c096STrond Myklebust 
317d4cc948bSChuck Lever /*
3181da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
3191da177e4SLinus Torvalds  * an NFS READ operation
3201da177e4SLinus Torvalds  */
321fad61490STrond Myklebust static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
3221da177e4SLinus Torvalds {
323a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
324a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
3251da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
3261da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
327fad61490STrond Myklebust 	size_t count = dreq->user_count;
328fad61490STrond Myklebust 	loff_t pos = dreq->pos;
3295dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
3301da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	curpage = 0;
333fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
3341da177e4SLinus Torvalds 	do {
3351da177e4SLinus Torvalds 		struct nfs_read_data *data;
3365dd602f2SChuck Lever 		size_t bytes;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		bytes = rsize;
3391da177e4SLinus Torvalds 		if (count < rsize)
3401da177e4SLinus Torvalds 			bytes = count;
3411da177e4SLinus Torvalds 
342*5db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
3431da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
3441da177e4SLinus Torvalds 		list_del_init(&data->pages);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 		data->inode = inode;
3471da177e4SLinus Torvalds 		data->cred = ctx->cred;
3481da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3491da177e4SLinus Torvalds 		data->args.context = ctx;
35088467055SChuck Lever 		data->args.offset = pos;
3511da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
3521da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
3531da177e4SLinus Torvalds 		data->args.count = bytes;
3541da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3551da177e4SLinus Torvalds 		data->res.eof = 0;
3561da177e4SLinus Torvalds 		data->res.count = bytes;
3571da177e4SLinus Torvalds 
358ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
359ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3601da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 		lock_kernel();
3651da177e4SLinus Torvalds 		rpc_execute(&data->task);
3661da177e4SLinus Torvalds 		unlock_kernel();
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
3691da177e4SLinus Torvalds 				data->task.tk_pid,
3701da177e4SLinus Torvalds 				inode->i_sb->s_id,
3711da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3721da177e4SLinus Torvalds 				bytes,
3731da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3741da177e4SLinus Torvalds 
37588467055SChuck Lever 		pos += bytes;
3761da177e4SLinus Torvalds 		pgbase += bytes;
3771da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
3781da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		count -= bytes;
3811da177e4SLinus Torvalds 	} while (count != 0);
382*5db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
38588467055SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	ssize_t result;
3881da177e4SLinus Torvalds 	sigset_t oldset;
38999514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3901da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
3911da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
3941da177e4SLinus Torvalds 	if (!dreq)
3951da177e4SLinus Torvalds 		return -ENOMEM;
3961da177e4SLinus Torvalds 
397fad61490STrond Myklebust 	dreq->user_addr = user_addr;
398fad61490STrond Myklebust 	dreq->user_count = count;
399fad61490STrond Myklebust 	dreq->pos = pos;
4001da177e4SLinus Torvalds 	dreq->pages = pages;
4011da177e4SLinus Torvalds 	dreq->npages = nr_pages;
40291d5b470SChuck Lever 	dreq->inode = inode;
403a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
404487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
405487b8372SChuck Lever 		dreq->iocb = iocb;
4061da177e4SLinus Torvalds 
40791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
4081da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
409fad61490STrond Myklebust 	nfs_direct_read_schedule(dreq);
410bc0fb201SChuck Lever 	result = nfs_direct_wait(dreq);
4111da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	return result;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
417fad61490STrond Myklebust {
418fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
419fad61490STrond Myklebust 	while (!list_empty(&dreq->list)) {
420fad61490STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
421fad61490STrond Myklebust 		list_del(&data->pages);
422fad61490STrond Myklebust 		nfs_writedata_release(data);
423fad61490STrond Myklebust 	}
424fad61490STrond Myklebust }
425fad61490STrond Myklebust 
426fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
427fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
428fad61490STrond Myklebust {
429fad61490STrond Myklebust 	struct list_head *pos;
430fad61490STrond Myklebust 
431fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
432fad61490STrond Myklebust 	list_for_each(pos, &dreq->list)
433fad61490STrond Myklebust 		dreq->outstanding++;
434fad61490STrond Myklebust 	dreq->count = 0;
435fad61490STrond Myklebust 
436fad61490STrond Myklebust 	nfs_direct_write_schedule(dreq, FLUSH_STABLE);
437fad61490STrond Myklebust }
438fad61490STrond Myklebust 
439fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
440fad61490STrond Myklebust {
441fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
442fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
443fad61490STrond Myklebust 
444fad61490STrond Myklebust 	/* Call the NFS version-specific code */
445fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
446fad61490STrond Myklebust 		return;
447fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
448fad61490STrond Myklebust 		dreq->error = task->tk_status;
449fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
450fad61490STrond Myklebust 	}
451fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
452fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
453fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
454fad61490STrond Myklebust 	}
455fad61490STrond Myklebust 
456fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
457fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
458fad61490STrond Myklebust }
459fad61490STrond Myklebust 
460fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
461fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
462fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
463fad61490STrond Myklebust };
464fad61490STrond Myklebust 
465fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
466fad61490STrond Myklebust {
467fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
468fad61490STrond Myklebust 	struct rpc_task *task = &data->task;
469fad61490STrond Myklebust 
470fad61490STrond Myklebust 	data->inode = dreq->inode;
471a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
472fad61490STrond Myklebust 
473fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
474fad61490STrond Myklebust 	data->args.offset = dreq->pos;
475fad61490STrond Myklebust 	data->args.count = dreq->user_count;
476fad61490STrond Myklebust 	data->res.count = 0;
477fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
478fad61490STrond Myklebust 	data->res.verf = &data->verf;
479fad61490STrond Myklebust 
480fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
481fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
482fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
483fad61490STrond Myklebust 
484fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
485fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
486fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
487fad61490STrond Myklebust 	dreq->commit_data = NULL;
488fad61490STrond Myklebust 
489fad61490STrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", task->tk_pid);
490fad61490STrond Myklebust 
491fad61490STrond Myklebust 	lock_kernel();
492fad61490STrond Myklebust 	rpc_execute(&data->task);
493fad61490STrond Myklebust 	unlock_kernel();
494fad61490STrond Myklebust }
495fad61490STrond Myklebust 
496fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
497fad61490STrond Myklebust {
498fad61490STrond Myklebust 	int flags = dreq->flags;
499fad61490STrond Myklebust 
500fad61490STrond Myklebust 	dreq->flags = 0;
501fad61490STrond Myklebust 	switch (flags) {
502fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
503fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
504fad61490STrond Myklebust 			break;
505fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
506fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
507fad61490STrond Myklebust 			break;
508fad61490STrond Myklebust 		default:
509fad61490STrond Myklebust 			nfs_end_data_update(inode);
510fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
511fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
512fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
513fad61490STrond Myklebust 			nfs_direct_complete(dreq);
514fad61490STrond Myklebust 	}
515fad61490STrond Myklebust }
516fad61490STrond Myklebust 
517fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
518fad61490STrond Myklebust {
519fad61490STrond Myklebust 	dreq->commit_data = nfs_commit_alloc(0);
520fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
521fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
522fad61490STrond Myklebust }
523fad61490STrond Myklebust #else
524fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
525fad61490STrond Myklebust {
526fad61490STrond Myklebust 	dreq->commit_data = NULL;
527fad61490STrond Myklebust }
528fad61490STrond Myklebust 
529fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
530fad61490STrond Myklebust {
531fad61490STrond Myklebust 	nfs_end_data_update(inode);
532fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
533fad61490STrond Myklebust 	nfs_direct_complete(dreq);
534fad61490STrond Myklebust }
535fad61490STrond Myklebust #endif
536fad61490STrond Myklebust 
537462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
5381da177e4SLinus Torvalds {
539462d5b32SChuck Lever 	struct list_head *list;
540462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
541462d5b32SChuck Lever 	unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5421da177e4SLinus Torvalds 
543462d5b32SChuck Lever 	dreq = nfs_direct_req_alloc();
544462d5b32SChuck Lever 	if (!dreq)
545462d5b32SChuck Lever 		return NULL;
5461da177e4SLinus Torvalds 
547462d5b32SChuck Lever 	list = &dreq->list;
548462d5b32SChuck Lever 	for(;;) {
549462d5b32SChuck Lever 		struct nfs_write_data *data = nfs_writedata_alloc(wpages);
5501da177e4SLinus Torvalds 
551462d5b32SChuck Lever 		if (unlikely(!data)) {
552462d5b32SChuck Lever 			while (!list_empty(list)) {
553462d5b32SChuck Lever 				data = list_entry(list->next,
554462d5b32SChuck Lever 						  struct nfs_write_data, pages);
555462d5b32SChuck Lever 				list_del(&data->pages);
556462d5b32SChuck Lever 				nfs_writedata_free(data);
557462d5b32SChuck Lever 			}
558462d5b32SChuck Lever 			kref_put(&dreq->kref, nfs_direct_req_release);
559462d5b32SChuck Lever 			return NULL;
5601da177e4SLinus Torvalds 		}
5611da177e4SLinus Torvalds 
562462d5b32SChuck Lever 		INIT_LIST_HEAD(&data->pages);
563462d5b32SChuck Lever 		list_add(&data->pages, list);
5641da177e4SLinus Torvalds 
565462d5b32SChuck Lever 		data->req = (struct nfs_page *) dreq;
56615ce4a0cSChuck Lever 		dreq->outstanding++;
567462d5b32SChuck Lever 		if (nbytes <= wsize)
5681da177e4SLinus Torvalds 			break;
569462d5b32SChuck Lever 		nbytes -= wsize;
570462d5b32SChuck Lever 	}
571fad61490STrond Myklebust 
572fad61490STrond Myklebust 	nfs_alloc_commit_data(dreq);
573fad61490STrond Myklebust 
574462d5b32SChuck Lever 	kref_get(&dreq->kref);
575462d5b32SChuck Lever 	return dreq;
576462d5b32SChuck Lever }
5771da177e4SLinus Torvalds 
578462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
579462d5b32SChuck Lever {
580462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
581462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
582462d5b32SChuck Lever 	int status = task->tk_status;
583462d5b32SChuck Lever 
584462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
585462d5b32SChuck Lever 		return;
586462d5b32SChuck Lever 
58715ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
588462d5b32SChuck Lever 
58915ce4a0cSChuck Lever 	if (likely(status >= 0))
59015ce4a0cSChuck Lever 		dreq->count += data->res.count;
59115ce4a0cSChuck Lever 	else
592fad61490STrond Myklebust 		dreq->error = task->tk_status;
59315ce4a0cSChuck Lever 
594fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
595fad61490STrond Myklebust 		switch (dreq->flags) {
596fad61490STrond Myklebust 			case 0:
597fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
598fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
599fad61490STrond Myklebust 				break;
600fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
601fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
602fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
603fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
604fad61490STrond Myklebust 				}
605fad61490STrond Myklebust 		}
606fad61490STrond Myklebust 	}
607fad61490STrond Myklebust 	/* In case we have to resend */
608fad61490STrond Myklebust 	data->args.stable = NFS_FILE_SYNC;
609fad61490STrond Myklebust 
610fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
611fad61490STrond Myklebust }
612fad61490STrond Myklebust 
613fad61490STrond Myklebust /*
614fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
615fad61490STrond Myklebust  *     errors after the first one are ignored.
616fad61490STrond Myklebust  */
617fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
618fad61490STrond Myklebust {
619fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
620fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
621fad61490STrond Myklebust 
622fad61490STrond Myklebust 	spin_lock(&dreq->lock);
62315ce4a0cSChuck Lever 	if (--dreq->outstanding) {
62415ce4a0cSChuck Lever 		spin_unlock(&dreq->lock);
62515ce4a0cSChuck Lever 		return;
62615ce4a0cSChuck Lever 	}
62715ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
62815ce4a0cSChuck Lever 
629fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
630462d5b32SChuck Lever }
631462d5b32SChuck Lever 
632462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
633462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
634fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
635462d5b32SChuck Lever };
636462d5b32SChuck Lever 
637462d5b32SChuck Lever /*
638462d5b32SChuck Lever  * For each nfs_write_data struct that was allocated on the list, dispatch
639462d5b32SChuck Lever  * an NFS WRITE operation
640462d5b32SChuck Lever  */
641fad61490STrond Myklebust static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
642462d5b32SChuck Lever {
643a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
644a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
645462d5b32SChuck Lever 	struct list_head *list = &dreq->list;
646462d5b32SChuck Lever 	struct page **pages = dreq->pages;
647fad61490STrond Myklebust 	size_t count = dreq->user_count;
648fad61490STrond Myklebust 	loff_t pos = dreq->pos;
649462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
650462d5b32SChuck Lever 	unsigned int curpage, pgbase;
651462d5b32SChuck Lever 
652462d5b32SChuck Lever 	curpage = 0;
653fad61490STrond Myklebust 	pgbase = dreq->user_addr & ~PAGE_MASK;
654462d5b32SChuck Lever 	do {
655462d5b32SChuck Lever 		struct nfs_write_data *data;
656462d5b32SChuck Lever 		size_t bytes;
657462d5b32SChuck Lever 
658462d5b32SChuck Lever 		bytes = wsize;
659462d5b32SChuck Lever 		if (count < wsize)
660462d5b32SChuck Lever 			bytes = count;
661462d5b32SChuck Lever 
662*5db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
663462d5b32SChuck Lever 		data = list_entry(list->next, struct nfs_write_data, pages);
664fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
665462d5b32SChuck Lever 
666462d5b32SChuck Lever 		data->inode = inode;
667462d5b32SChuck Lever 		data->cred = ctx->cred;
668462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
669462d5b32SChuck Lever 		data->args.context = ctx;
67088467055SChuck Lever 		data->args.offset = pos;
671462d5b32SChuck Lever 		data->args.pgbase = pgbase;
672462d5b32SChuck Lever 		data->args.pages = &pages[curpage];
673462d5b32SChuck Lever 		data->args.count = bytes;
674462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
675462d5b32SChuck Lever 		data->res.count = bytes;
67647989d74SChuck Lever 		data->res.verf = &data->verf;
677462d5b32SChuck Lever 
678462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
679462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
680fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
681462d5b32SChuck Lever 
682462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
683462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		lock_kernel();
686462d5b32SChuck Lever 		rpc_execute(&data->task);
6871da177e4SLinus Torvalds 		unlock_kernel();
6881da177e4SLinus Torvalds 
689462d5b32SChuck Lever 		dfprintk(VFS, "NFS: %4d initiated direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
690462d5b32SChuck Lever 				data->task.tk_pid,
691462d5b32SChuck Lever 				inode->i_sb->s_id,
692462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
693462d5b32SChuck Lever 				bytes,
694462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
695462d5b32SChuck Lever 
69688467055SChuck Lever 		pos += bytes;
697462d5b32SChuck Lever 		pgbase += bytes;
698462d5b32SChuck Lever 		curpage += pgbase >> PAGE_SHIFT;
699462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
700462d5b32SChuck Lever 
701462d5b32SChuck Lever 		count -= bytes;
702462d5b32SChuck Lever 	} while (count != 0);
703*5db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
70688467055SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages)
707462d5b32SChuck Lever {
708462d5b32SChuck Lever 	ssize_t result;
709462d5b32SChuck Lever 	sigset_t oldset;
710c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
711462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
712462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
713fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
714fad61490STrond Myklebust 	int sync = 0;
715462d5b32SChuck Lever 
716fad61490STrond Myklebust 	dreq = nfs_direct_write_alloc(count, wsize);
717462d5b32SChuck Lever 	if (!dreq)
718462d5b32SChuck Lever 		return -ENOMEM;
719fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
720fad61490STrond Myklebust 		sync = FLUSH_STABLE;
721462d5b32SChuck Lever 
722fad61490STrond Myklebust 	dreq->user_addr = user_addr;
723fad61490STrond Myklebust 	dreq->user_count = count;
724fad61490STrond Myklebust 	dreq->pos = pos;
725462d5b32SChuck Lever 	dreq->pages = pages;
726462d5b32SChuck Lever 	dreq->npages = nr_pages;
727c89f2ee5SChuck Lever 	dreq->inode = inode;
728a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
729c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
730c89f2ee5SChuck Lever 		dreq->iocb = iocb;
731462d5b32SChuck Lever 
73247989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
73347989d74SChuck Lever 
734462d5b32SChuck Lever 	nfs_begin_data_update(inode);
735462d5b32SChuck Lever 
736462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
737fad61490STrond Myklebust 	nfs_direct_write_schedule(dreq, sync);
738c89f2ee5SChuck Lever 	result = nfs_direct_wait(dreq);
739462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
740462d5b32SChuck Lever 
741462d5b32SChuck Lever 	return result;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /**
7451da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7461da177e4SLinus Torvalds  * @iocb: target I/O control block
7471da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
74888467055SChuck Lever  * @count: number of bytes to read
74988467055SChuck Lever  * @pos: byte offset in file where reading starts
7501da177e4SLinus Torvalds  *
7511da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7521da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7531da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7541da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7551da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
75688467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7571da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7581da177e4SLinus Torvalds  * care of managing the end of file boundary.
7591da177e4SLinus Torvalds  *
7601da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7611da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7621da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
7631da177e4SLinus Torvalds  * cache.
7641da177e4SLinus Torvalds  */
765d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
7680cdd80d0SChuck Lever 	int page_count;
7690cdd80d0SChuck Lever 	struct page **pages;
7701da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7711da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
7721da177e4SLinus Torvalds 
773ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
7740bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7750bbacc40SChuck Lever 		file->f_dentry->d_name.name,
776ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (count < 0)
7791da177e4SLinus Torvalds 		goto out;
7801da177e4SLinus Torvalds 	retval = -EFAULT;
7810cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
7821da177e4SLinus Torvalds 		goto out;
7831da177e4SLinus Torvalds 	retval = 0;
7841da177e4SLinus Torvalds 	if (!count)
7851da177e4SLinus Torvalds 		goto out;
7861da177e4SLinus Torvalds 
78729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
7881da177e4SLinus Torvalds 	if (retval)
7891da177e4SLinus Torvalds 		goto out;
7901da177e4SLinus Torvalds 
7910cdd80d0SChuck Lever 	page_count = nfs_get_user_pages(READ, (unsigned long) buf,
7920cdd80d0SChuck Lever 						count, &pages);
7930cdd80d0SChuck Lever 	if (page_count < 0) {
7940cdd80d0SChuck Lever 		nfs_free_user_pages(pages, 0, 0);
7950cdd80d0SChuck Lever 		retval = page_count;
7960cdd80d0SChuck Lever 		goto out;
7970cdd80d0SChuck Lever 	}
7980cdd80d0SChuck Lever 
79999514f8fSChuck Lever 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
8000cdd80d0SChuck Lever 						pages, page_count);
8011da177e4SLinus Torvalds 	if (retval > 0)
8020cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds out:
8051da177e4SLinus Torvalds 	return retval;
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds /**
8091da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8101da177e4SLinus Torvalds  * @iocb: target I/O control block
8111da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
81288467055SChuck Lever  * @count: number of bytes to write
81388467055SChuck Lever  * @pos: byte offset in file where writing starts
8141da177e4SLinus Torvalds  *
8151da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8161da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8171da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8181da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8191da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8201da177e4SLinus Torvalds  * parameter checking and report problems.
8211da177e4SLinus Torvalds  *
8221da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
8231da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
8241da177e4SLinus Torvalds  *
8251da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8261da177e4SLinus Torvalds  *
8271da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8281da177e4SLinus Torvalds  * readers of this file.
8291da177e4SLinus Torvalds  *
8301da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8311da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8321da177e4SLinus Torvalds  */
833d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
8341da177e4SLinus Torvalds {
835ce1a8e67SChuck Lever 	ssize_t retval;
83647989d74SChuck Lever 	int page_count;
83747989d74SChuck Lever 	struct page **pages;
8381da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8391da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8401da177e4SLinus Torvalds 
841ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8420bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
843ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
844ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8451da177e4SLinus Torvalds 
846ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
847ce1a8e67SChuck Lever 	if (retval)
8481da177e4SLinus Torvalds 		goto out;
849ce1a8e67SChuck Lever 
850ce1a8e67SChuck Lever 	retval = -EINVAL;
851ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8521da177e4SLinus Torvalds 		goto out;
8531da177e4SLinus Torvalds 	retval = 0;
8541da177e4SLinus Torvalds 	if (!count)
8551da177e4SLinus Torvalds 		goto out;
856ce1a8e67SChuck Lever 
857ce1a8e67SChuck Lever 	retval = -EFAULT;
85847989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
859ce1a8e67SChuck Lever 		goto out;
8601da177e4SLinus Torvalds 
86129884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8621da177e4SLinus Torvalds 	if (retval)
8631da177e4SLinus Torvalds 		goto out;
8641da177e4SLinus Torvalds 
86547989d74SChuck Lever 	page_count = nfs_get_user_pages(WRITE, (unsigned long) buf,
86647989d74SChuck Lever 						count, &pages);
86747989d74SChuck Lever 	if (page_count < 0) {
86847989d74SChuck Lever 		nfs_free_user_pages(pages, 0, 0);
86947989d74SChuck Lever 		retval = page_count;
87047989d74SChuck Lever 		goto out;
87147989d74SChuck Lever 	}
87247989d74SChuck Lever 
873c89f2ee5SChuck Lever 	retval = nfs_direct_write(iocb, (unsigned long) buf, count,
87447989d74SChuck Lever 					pos, pages, page_count);
8759eafa8ccSChuck Lever 
8769eafa8ccSChuck Lever 	/*
8779eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
8789eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
8799eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
8809eafa8ccSChuck Lever 	 *
8819eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
8829eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
8839eafa8ccSChuck Lever 	 */
8841da177e4SLinus Torvalds 	if (mapping->nrpages)
8851da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
8869eafa8ccSChuck Lever 
8871da177e4SLinus Torvalds 	if (retval > 0)
888ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds out:
8911da177e4SLinus Torvalds 	return retval;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
89488467055SChuck Lever /**
89588467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
89688467055SChuck Lever  *
89788467055SChuck Lever  */
8981da177e4SLinus Torvalds int nfs_init_directcache(void)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9011da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
9021da177e4SLinus Torvalds 						0, SLAB_RECLAIM_ACCOUNT,
9031da177e4SLinus Torvalds 						NULL, NULL);
9041da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9051da177e4SLinus Torvalds 		return -ENOMEM;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	return 0;
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
91088467055SChuck Lever /**
91188467055SChuck Lever  * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures
91288467055SChuck Lever  *
91388467055SChuck Lever  */
9141da177e4SLinus Torvalds void nfs_destroy_directcache(void)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
9171da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
9181da177e4SLinus Torvalds }
919