xref: /openbmc/linux/fs/nfs/direct.c (revision 9c93ab7dff5eb22027ab15010557bb73f9b44c99)
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"
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static kmem_cache_t *nfs_direct_cachep;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds struct nfs_direct_req {
681da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
6915ce4a0cSChuck Lever 
7015ce4a0cSChuck Lever 	/* I/O parameters */
71fad61490STrond Myklebust 	struct list_head	list,		/* nfs_read/write_data structs */
72fad61490STrond Myklebust 				rewrite_list;	/* saved nfs_write_data structs */
73a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
7499514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7588467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
761da177e4SLinus Torvalds 	struct page **		pages;		/* pages in our buffer */
771da177e4SLinus Torvalds 	unsigned int		npages;		/* count of pages */
7815ce4a0cSChuck Lever 
7915ce4a0cSChuck Lever 	/* completion state */
80b1c5921cSChuck Lever 	atomic_t		io_count;	/* i/os we're waiting for */
8115ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8215ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
831da177e4SLinus Torvalds 				error;		/* any reported error */
84d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
85fad61490STrond Myklebust 
86fad61490STrond Myklebust 	/* commit state */
87fad61490STrond Myklebust 	struct nfs_write_data *	commit_data;	/* special write_data for commits */
88fad61490STrond Myklebust 	int			flags;
89fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
90fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
91fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
921da177e4SLinus Torvalds };
931da177e4SLinus Torvalds 
94fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
95fedb595cSChuck Lever static const struct rpc_call_ops nfs_write_direct_ops;
961da177e4SLinus Torvalds 
97b1c5921cSChuck Lever static inline void get_dreq(struct nfs_direct_req *dreq)
98b1c5921cSChuck Lever {
99b1c5921cSChuck Lever 	atomic_inc(&dreq->io_count);
100b1c5921cSChuck Lever }
101b1c5921cSChuck Lever 
102b1c5921cSChuck Lever static inline int put_dreq(struct nfs_direct_req *dreq)
103b1c5921cSChuck Lever {
104b1c5921cSChuck Lever 	return atomic_dec_and_test(&dreq->io_count);
105b1c5921cSChuck Lever }
106b1c5921cSChuck Lever 
1071da177e4SLinus Torvalds /**
108b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
109b8a32e2bSChuck Lever  * @rw: direction (read or write)
110b8a32e2bSChuck Lever  * @iocb: target I/O control block
111b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
112b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
113b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
114b8a32e2bSChuck Lever  *
115b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
116b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
117b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
118b8a32e2bSChuck Lever  * should never be called.
1191da177e4SLinus Torvalds  */
120b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
121b8a32e2bSChuck Lever {
122b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
123e99170ffSTrond Myklebust 			iocb->ki_filp->f_dentry->d_name.name,
124e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
125b8a32e2bSChuck Lever 
126b8a32e2bSChuck Lever 	return -EINVAL;
127b8a32e2bSChuck Lever }
128b8a32e2bSChuck Lever 
129*9c93ab7dSChuck Lever static void nfs_direct_dirty_pages(struct page **pages, int npages)
1306b45d858STrond Myklebust {
1316b45d858STrond Myklebust 	int i;
1326b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1336b45d858STrond Myklebust 		struct page *page = pages[i];
134*9c93ab7dSChuck Lever 		if (!PageCompound(page))
1356b45d858STrond Myklebust 			set_page_dirty_lock(page);
1366b45d858STrond Myklebust 	}
137*9c93ab7dSChuck Lever }
138*9c93ab7dSChuck Lever 
139*9c93ab7dSChuck Lever static void nfs_direct_release_pages(struct page **pages, int npages)
140*9c93ab7dSChuck Lever {
141*9c93ab7dSChuck Lever 	int i;
142*9c93ab7dSChuck Lever 	for (i = 0; i < npages; i++)
143*9c93ab7dSChuck Lever 		page_cache_release(pages[i]);
1446b45d858STrond Myklebust }
1456b45d858STrond Myklebust 
146d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	int result = -ENOMEM;
1491da177e4SLinus Torvalds 	unsigned long page_count;
1501da177e4SLinus Torvalds 	size_t array_size;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1531da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1561da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1571da177e4SLinus Torvalds 	if (*pages) {
1581da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1591da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1601da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1611da177e4SLinus Torvalds 					*pages, NULL);
1621da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
1636b45d858STrond Myklebust 		if (result != page_count) {
164143f412eSTrond Myklebust 			/*
1656b45d858STrond Myklebust 			 * If we got fewer pages than expected from
1666b45d858STrond Myklebust 			 * get_user_pages(), the user buffer runs off the
1676b45d858STrond Myklebust 			 * end of a mapping; return EFAULT.
168143f412eSTrond Myklebust 			 */
1696b45d858STrond Myklebust 			if (result >= 0) {
170*9c93ab7dSChuck Lever 				nfs_direct_release_pages(*pages, result);
171143f412eSTrond Myklebust 				result = -EFAULT;
1726b45d858STrond Myklebust 			} else
1736b45d858STrond Myklebust 				kfree(*pages);
1746b45d858STrond Myklebust 			*pages = NULL;
175143f412eSTrond Myklebust 		}
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 	return result;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
18093619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
1851da177e4SLinus Torvalds 	if (!dreq)
1861da177e4SLinus Torvalds 		return NULL;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	kref_init(&dreq->kref);
189d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dreq->list);
191fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
19293619e59SChuck Lever 	dreq->iocb = NULL;
193a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
19415ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
195b1c5921cSChuck Lever 	atomic_set(&dreq->io_count, 0);
19615ce4a0cSChuck Lever 	dreq->count = 0;
19715ce4a0cSChuck Lever 	dreq->error = 0;
198fad61490STrond Myklebust 	dreq->flags = 0;
19993619e59SChuck Lever 
20093619e59SChuck Lever 	return dreq;
20193619e59SChuck Lever }
20293619e59SChuck Lever 
2031da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
206a8881f5aSTrond Myklebust 
207a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
208a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2091da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212d4cc948bSChuck Lever /*
213bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
214bc0fb201SChuck Lever  */
215bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
216bc0fb201SChuck Lever {
21715ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
218bc0fb201SChuck Lever 
219bc0fb201SChuck Lever 	/* Async requests don't wait here */
220bc0fb201SChuck Lever 	if (dreq->iocb)
221bc0fb201SChuck Lever 		goto out;
222bc0fb201SChuck Lever 
223d72b7a6bSTrond Myklebust 	result = wait_for_completion_interruptible(&dreq->completion);
224bc0fb201SChuck Lever 
225bc0fb201SChuck Lever 	if (!result)
22615ce4a0cSChuck Lever 		result = dreq->error;
227bc0fb201SChuck Lever 	if (!result)
22815ce4a0cSChuck Lever 		result = dreq->count;
229bc0fb201SChuck Lever 
230bc0fb201SChuck Lever out:
231bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
232bc0fb201SChuck Lever 	return (ssize_t) result;
233bc0fb201SChuck Lever }
234bc0fb201SChuck Lever 
235bc0fb201SChuck Lever /*
23663ab46abSChuck Lever  * We must hold a reference to all the pages in this direct read request
23763ab46abSChuck Lever  * until the RPCs complete.  This could be long *after* we are woken up in
23863ab46abSChuck Lever  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
23963ab46abSChuck Lever  *
24063ab46abSChuck Lever  * In addition, synchronous I/O uses a stack-allocated iocb.  Thus we
24163ab46abSChuck Lever  * can't trust the iocb is still valid here if this is a synchronous
24263ab46abSChuck Lever  * request.  If the waiter is woken prematurely, the iocb is long gone.
24363ab46abSChuck Lever  */
24463ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
24563ab46abSChuck Lever {
24663ab46abSChuck Lever 	if (dreq->iocb) {
24715ce4a0cSChuck Lever 		long res = (long) dreq->error;
24863ab46abSChuck Lever 		if (!res)
24915ce4a0cSChuck Lever 			res = (long) dreq->count;
25063ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
251d72b7a6bSTrond Myklebust 	}
252d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
25363ab46abSChuck Lever 
25463ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
25563ab46abSChuck Lever }
25663ab46abSChuck Lever 
25763ab46abSChuck Lever /*
2581da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
2591da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
2601da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
2611da177e4SLinus Torvalds  */
2625dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	struct list_head *list;
2651da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2661da177e4SLinus Torvalds 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2671da177e4SLinus Torvalds 
26893619e59SChuck Lever 	dreq = nfs_direct_req_alloc();
2691da177e4SLinus Torvalds 	if (!dreq)
2701da177e4SLinus Torvalds 		return NULL;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	list = &dreq->list;
2731da177e4SLinus Torvalds 	for(;;) {
27440859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		if (unlikely(!data)) {
2771da177e4SLinus Torvalds 			while (!list_empty(list)) {
2781da177e4SLinus Torvalds 				data = list_entry(list->next,
2791da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
2801da177e4SLinus Torvalds 				list_del(&data->pages);
2811da177e4SLinus Torvalds 				nfs_readdata_free(data);
2821da177e4SLinus Torvalds 			}
2831da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
2841da177e4SLinus Torvalds 			return NULL;
2851da177e4SLinus Torvalds 		}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2881da177e4SLinus Torvalds 		list_add(&data->pages, list);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
291b1c5921cSChuck Lever 		get_dreq(dreq);
2921da177e4SLinus Torvalds 		if (nbytes <= rsize)
2931da177e4SLinus Torvalds 			break;
2941da177e4SLinus Torvalds 		nbytes -= rsize;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	kref_get(&dreq->kref);
2971da177e4SLinus Torvalds 	return dreq;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
300ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
3011da177e4SLinus Torvalds {
302ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
3031da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
3041da177e4SLinus Torvalds 
305ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
306ec06c096STrond Myklebust 		return;
3071da177e4SLinus Torvalds 
30815ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
30915ce4a0cSChuck Lever 
31015ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
31115ce4a0cSChuck Lever 		dreq->count += data->res.count;
3121da177e4SLinus Torvalds 	else
31315ce4a0cSChuck Lever 		dreq->error = task->tk_status;
3141da177e4SLinus Torvalds 
31515ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3161da177e4SLinus Torvalds 
317*9c93ab7dSChuck Lever 	if (put_dreq(dreq)) {
318*9c93ab7dSChuck Lever 		nfs_direct_dirty_pages(dreq->pages, dreq->npages);
319*9c93ab7dSChuck Lever 		nfs_direct_release_pages(dreq->pages, dreq->npages);
32063ab46abSChuck Lever 		nfs_direct_complete(dreq);
3211da177e4SLinus Torvalds 	}
322*9c93ab7dSChuck Lever }
3231da177e4SLinus Torvalds 
324ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
325ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
326ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
327ec06c096STrond Myklebust };
328ec06c096STrond Myklebust 
329d4cc948bSChuck Lever /*
3301da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
3311da177e4SLinus Torvalds  * an NFS READ operation
3321da177e4SLinus Torvalds  */
33351a7bc6cSChuck Lever static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
3341da177e4SLinus Torvalds {
335a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
336a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
3371da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
3381da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
3395dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
3401da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	curpage = 0;
34351a7bc6cSChuck Lever 	pgbase = user_addr & ~PAGE_MASK;
3441da177e4SLinus Torvalds 	do {
3451da177e4SLinus Torvalds 		struct nfs_read_data *data;
3465dd602f2SChuck Lever 		size_t bytes;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		bytes = rsize;
3491da177e4SLinus Torvalds 		if (count < rsize)
3501da177e4SLinus Torvalds 			bytes = count;
3511da177e4SLinus Torvalds 
3525db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
3531da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
3541da177e4SLinus Torvalds 		list_del_init(&data->pages);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		data->inode = inode;
3571da177e4SLinus Torvalds 		data->cred = ctx->cred;
3581da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3591da177e4SLinus Torvalds 		data->args.context = ctx;
36088467055SChuck Lever 		data->args.offset = pos;
3611da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
3621da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
3631da177e4SLinus Torvalds 		data->args.count = bytes;
3641da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3651da177e4SLinus Torvalds 		data->res.eof = 0;
3661da177e4SLinus Torvalds 		data->res.count = bytes;
3671da177e4SLinus Torvalds 
368ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
369ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3701da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		lock_kernel();
3751da177e4SLinus Torvalds 		rpc_execute(&data->task);
3761da177e4SLinus Torvalds 		unlock_kernel();
3771da177e4SLinus Torvalds 
378606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
3791da177e4SLinus Torvalds 				data->task.tk_pid,
3801da177e4SLinus Torvalds 				inode->i_sb->s_id,
3811da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3821da177e4SLinus Torvalds 				bytes,
3831da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3841da177e4SLinus Torvalds 
38588467055SChuck Lever 		pos += bytes;
3861da177e4SLinus Torvalds 		pgbase += bytes;
3871da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
3881da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 		count -= bytes;
3911da177e4SLinus Torvalds 	} while (count != 0);
3925db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
39588467055SChuck 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)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	ssize_t result;
3981da177e4SLinus Torvalds 	sigset_t oldset;
39999514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4001da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4011da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
4041da177e4SLinus Torvalds 	if (!dreq)
4051da177e4SLinus Torvalds 		return -ENOMEM;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	dreq->pages = pages;
4081da177e4SLinus Torvalds 	dreq->npages = nr_pages;
40991d5b470SChuck Lever 	dreq->inode = inode;
410a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
411487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
412487b8372SChuck Lever 		dreq->iocb = iocb;
4131da177e4SLinus Torvalds 
41491d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
4151da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
41651a7bc6cSChuck Lever 	nfs_direct_read_schedule(dreq, user_addr, count, pos);
417bc0fb201SChuck Lever 	result = nfs_direct_wait(dreq);
4181da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	return result;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
423fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4241da177e4SLinus Torvalds {
425fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
426fad61490STrond Myklebust 	while (!list_empty(&dreq->list)) {
427fad61490STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
428fad61490STrond Myklebust 		list_del(&data->pages);
429fad61490STrond Myklebust 		nfs_writedata_release(data);
430fad61490STrond Myklebust 	}
431*9c93ab7dSChuck Lever 	nfs_direct_release_pages(dreq->pages, dreq->npages);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
434fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
435fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4361da177e4SLinus Torvalds {
437fedb595cSChuck Lever 	struct inode *inode = dreq->inode;
438fedb595cSChuck Lever 	struct list_head *p;
439fedb595cSChuck Lever 	struct nfs_write_data *data;
4401da177e4SLinus Torvalds 
441fad61490STrond Myklebust 	dreq->count = 0;
442fedb595cSChuck Lever 	get_dreq(dreq);
4431da177e4SLinus Torvalds 
444fedb595cSChuck Lever 	list_for_each(p, &dreq->rewrite_list) {
445fedb595cSChuck Lever 		data = list_entry(p, struct nfs_write_data, pages);
446fedb595cSChuck Lever 
447fedb595cSChuck Lever 		get_dreq(dreq);
448fedb595cSChuck Lever 
449fedb595cSChuck Lever 		/*
450fedb595cSChuck Lever 		 * Reset data->res.
451fedb595cSChuck Lever 		 */
452fedb595cSChuck Lever 		nfs_fattr_init(&data->fattr);
453fedb595cSChuck Lever 		data->res.count = data->args.count;
454fedb595cSChuck Lever 		memset(&data->verf, 0, sizeof(data->verf));
455fedb595cSChuck Lever 
456fedb595cSChuck Lever 		/*
457fedb595cSChuck Lever 		 * Reuse data->task; data->args should not have changed
458fedb595cSChuck Lever 		 * since the original request was sent.
459fedb595cSChuck Lever 		 */
460fedb595cSChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
461fedb595cSChuck Lever 				&nfs_write_direct_ops, data);
462fedb595cSChuck Lever 		NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
463fedb595cSChuck Lever 
464fedb595cSChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
465fedb595cSChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
466fedb595cSChuck Lever 
467fedb595cSChuck Lever 		/*
468fedb595cSChuck Lever 		 * We're called via an RPC callback, so BKL is already held.
469fedb595cSChuck Lever 		 */
470fedb595cSChuck Lever 		rpc_execute(&data->task);
471fedb595cSChuck Lever 
472fedb595cSChuck Lever 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
473fedb595cSChuck Lever 				data->task.tk_pid,
474fedb595cSChuck Lever 				inode->i_sb->s_id,
475fedb595cSChuck Lever 				(long long)NFS_FILEID(inode),
476fedb595cSChuck Lever 				data->args.count,
477fedb595cSChuck Lever 				(unsigned long long)data->args.offset);
478fedb595cSChuck Lever 	}
479fedb595cSChuck Lever 
480fedb595cSChuck Lever 	if (put_dreq(dreq))
481fedb595cSChuck Lever 		nfs_direct_write_complete(dreq, inode);
482fad61490STrond Myklebust }
4831da177e4SLinus Torvalds 
484fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
485fad61490STrond Myklebust {
486fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
487fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
4881da177e4SLinus Torvalds 
489fad61490STrond Myklebust 	/* Call the NFS version-specific code */
490fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
491fad61490STrond Myklebust 		return;
492fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
493fad61490STrond Myklebust 		dreq->error = task->tk_status;
494fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
495fad61490STrond Myklebust 	}
496fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
497fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
498fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
499fad61490STrond Myklebust 	}
500fad61490STrond Myklebust 
501fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
502fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
503fad61490STrond Myklebust }
504fad61490STrond Myklebust 
505fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
506fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
507fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
508fad61490STrond Myklebust };
509fad61490STrond Myklebust 
510fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
511fad61490STrond Myklebust {
512fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
513fad61490STrond Myklebust 
514fad61490STrond Myklebust 	data->inode = dreq->inode;
515a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
516fad61490STrond Myklebust 
517fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
51851a7bc6cSChuck Lever 	data->args.offset = 0;
51951a7bc6cSChuck Lever 	data->args.count = 0;
520fad61490STrond Myklebust 	data->res.count = 0;
521fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
522fad61490STrond Myklebust 	data->res.verf = &data->verf;
523fad61490STrond Myklebust 
524fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
525fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
526fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
527fad61490STrond Myklebust 
528fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
529fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
530fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
531fad61490STrond Myklebust 	dreq->commit_data = NULL;
532fad61490STrond Myklebust 
533e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	lock_kernel();
536fad61490STrond Myklebust 	rpc_execute(&data->task);
5371da177e4SLinus Torvalds 	unlock_kernel();
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
540fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
5411da177e4SLinus Torvalds {
542fad61490STrond Myklebust 	int flags = dreq->flags;
5431da177e4SLinus Torvalds 
544fad61490STrond Myklebust 	dreq->flags = 0;
545fad61490STrond Myklebust 	switch (flags) {
546fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
547fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5481da177e4SLinus Torvalds 			break;
549fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
550fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5511da177e4SLinus Torvalds 			break;
5521da177e4SLinus Torvalds 		default:
553fad61490STrond Myklebust 			nfs_end_data_update(inode);
554fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
555fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
556fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
557fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5581da177e4SLinus Torvalds 	}
559fad61490STrond Myklebust }
560fad61490STrond Myklebust 
561fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
562fad61490STrond Myklebust {
563fad61490STrond Myklebust 	dreq->commit_data = nfs_commit_alloc(0);
564fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
565fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
566fad61490STrond Myklebust }
567fad61490STrond Myklebust #else
568fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
569fad61490STrond Myklebust {
570fad61490STrond Myklebust 	dreq->commit_data = NULL;
571fad61490STrond Myklebust }
572fad61490STrond Myklebust 
573fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
574fad61490STrond Myklebust {
575fad61490STrond Myklebust 	nfs_end_data_update(inode);
576fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
577fad61490STrond Myklebust 	nfs_direct_complete(dreq);
578fad61490STrond Myklebust }
579fad61490STrond Myklebust #endif
580fad61490STrond Myklebust 
581462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
5821da177e4SLinus Torvalds {
583462d5b32SChuck Lever 	struct list_head *list;
584462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
585462d5b32SChuck Lever 	unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5861da177e4SLinus Torvalds 
587462d5b32SChuck Lever 	dreq = nfs_direct_req_alloc();
588462d5b32SChuck Lever 	if (!dreq)
589462d5b32SChuck Lever 		return NULL;
5901da177e4SLinus Torvalds 
591462d5b32SChuck Lever 	list = &dreq->list;
592462d5b32SChuck Lever 	for(;;) {
593462d5b32SChuck Lever 		struct nfs_write_data *data = nfs_writedata_alloc(wpages);
5941da177e4SLinus Torvalds 
595462d5b32SChuck Lever 		if (unlikely(!data)) {
596462d5b32SChuck Lever 			while (!list_empty(list)) {
597462d5b32SChuck Lever 				data = list_entry(list->next,
598462d5b32SChuck Lever 						  struct nfs_write_data, pages);
599462d5b32SChuck Lever 				list_del(&data->pages);
600462d5b32SChuck Lever 				nfs_writedata_free(data);
601462d5b32SChuck Lever 			}
602462d5b32SChuck Lever 			kref_put(&dreq->kref, nfs_direct_req_release);
603462d5b32SChuck Lever 			return NULL;
6041da177e4SLinus Torvalds 		}
6051da177e4SLinus Torvalds 
606462d5b32SChuck Lever 		INIT_LIST_HEAD(&data->pages);
607462d5b32SChuck Lever 		list_add(&data->pages, list);
6081da177e4SLinus Torvalds 
609462d5b32SChuck Lever 		data->req = (struct nfs_page *) dreq;
610b1c5921cSChuck Lever 		get_dreq(dreq);
611462d5b32SChuck Lever 		if (nbytes <= wsize)
6121da177e4SLinus Torvalds 			break;
613462d5b32SChuck Lever 		nbytes -= wsize;
614462d5b32SChuck Lever 	}
615fad61490STrond Myklebust 
616fad61490STrond Myklebust 	nfs_alloc_commit_data(dreq);
617fad61490STrond Myklebust 
618462d5b32SChuck Lever 	kref_get(&dreq->kref);
619462d5b32SChuck Lever 	return dreq;
620462d5b32SChuck Lever }
6211da177e4SLinus Torvalds 
622462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
623462d5b32SChuck Lever {
624462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
625462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
626462d5b32SChuck Lever 	int status = task->tk_status;
627462d5b32SChuck Lever 
628462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
629462d5b32SChuck Lever 		return;
630462d5b32SChuck Lever 
63115ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
632462d5b32SChuck Lever 
63315ce4a0cSChuck Lever 	if (likely(status >= 0))
63415ce4a0cSChuck Lever 		dreq->count += data->res.count;
63515ce4a0cSChuck Lever 	else
636fad61490STrond Myklebust 		dreq->error = task->tk_status;
63715ce4a0cSChuck Lever 
638fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
639fad61490STrond Myklebust 		switch (dreq->flags) {
640fad61490STrond Myklebust 			case 0:
641fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
642fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
643fad61490STrond Myklebust 				break;
644fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
645fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
646fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
647fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
648fad61490STrond Myklebust 				}
649fad61490STrond Myklebust 		}
650fad61490STrond Myklebust 	}
651fad61490STrond Myklebust 
652fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
653fad61490STrond Myklebust }
654fad61490STrond Myklebust 
655fad61490STrond Myklebust /*
656fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
657fad61490STrond Myklebust  *     errors after the first one are ignored.
658fad61490STrond Myklebust  */
659fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
660fad61490STrond Myklebust {
661fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
662fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
663fad61490STrond Myklebust 
664b1c5921cSChuck Lever 	if (put_dreq(dreq))
665fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
666462d5b32SChuck Lever }
667462d5b32SChuck Lever 
668462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
669462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
670fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
671462d5b32SChuck Lever };
672462d5b32SChuck Lever 
673462d5b32SChuck Lever /*
674462d5b32SChuck Lever  * For each nfs_write_data struct that was allocated on the list, dispatch
675462d5b32SChuck Lever  * an NFS WRITE operation
676462d5b32SChuck Lever  */
67751a7bc6cSChuck Lever static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
678462d5b32SChuck Lever {
679a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
680a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
681462d5b32SChuck Lever 	struct list_head *list = &dreq->list;
682462d5b32SChuck Lever 	struct page **pages = dreq->pages;
683462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
684462d5b32SChuck Lever 	unsigned int curpage, pgbase;
685462d5b32SChuck Lever 
686462d5b32SChuck Lever 	curpage = 0;
68751a7bc6cSChuck Lever 	pgbase = user_addr & ~PAGE_MASK;
688462d5b32SChuck Lever 	do {
689462d5b32SChuck Lever 		struct nfs_write_data *data;
690462d5b32SChuck Lever 		size_t bytes;
691462d5b32SChuck Lever 
692462d5b32SChuck Lever 		bytes = wsize;
693462d5b32SChuck Lever 		if (count < wsize)
694462d5b32SChuck Lever 			bytes = count;
695462d5b32SChuck Lever 
6965db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
697462d5b32SChuck Lever 		data = list_entry(list->next, struct nfs_write_data, pages);
698fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
699462d5b32SChuck Lever 
700462d5b32SChuck Lever 		data->inode = inode;
701462d5b32SChuck Lever 		data->cred = ctx->cred;
702462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
703462d5b32SChuck Lever 		data->args.context = ctx;
70488467055SChuck Lever 		data->args.offset = pos;
705462d5b32SChuck Lever 		data->args.pgbase = pgbase;
706462d5b32SChuck Lever 		data->args.pages = &pages[curpage];
707462d5b32SChuck Lever 		data->args.count = bytes;
708462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
709462d5b32SChuck Lever 		data->res.count = bytes;
71047989d74SChuck Lever 		data->res.verf = &data->verf;
711462d5b32SChuck Lever 
712462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
713462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
714fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
715462d5b32SChuck Lever 
716462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
717462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 		lock_kernel();
720462d5b32SChuck Lever 		rpc_execute(&data->task);
7211da177e4SLinus Torvalds 		unlock_kernel();
7221da177e4SLinus Torvalds 
723606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
724462d5b32SChuck Lever 				data->task.tk_pid,
725462d5b32SChuck Lever 				inode->i_sb->s_id,
726462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
727462d5b32SChuck Lever 				bytes,
728462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
729462d5b32SChuck Lever 
73088467055SChuck Lever 		pos += bytes;
731462d5b32SChuck Lever 		pgbase += bytes;
732462d5b32SChuck Lever 		curpage += pgbase >> PAGE_SHIFT;
733462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
734462d5b32SChuck Lever 
735462d5b32SChuck Lever 		count -= bytes;
736462d5b32SChuck Lever 	} while (count != 0);
7375db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
74088467055SChuck 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)
741462d5b32SChuck Lever {
742462d5b32SChuck Lever 	ssize_t result;
743462d5b32SChuck Lever 	sigset_t oldset;
744c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
745462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
746462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
747fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
748fad61490STrond Myklebust 	int sync = 0;
749462d5b32SChuck Lever 
750fad61490STrond Myklebust 	dreq = nfs_direct_write_alloc(count, wsize);
751462d5b32SChuck Lever 	if (!dreq)
752462d5b32SChuck Lever 		return -ENOMEM;
753fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
754fad61490STrond Myklebust 		sync = FLUSH_STABLE;
755462d5b32SChuck Lever 
756462d5b32SChuck Lever 	dreq->pages = pages;
757462d5b32SChuck Lever 	dreq->npages = nr_pages;
758c89f2ee5SChuck Lever 	dreq->inode = inode;
759a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
760c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
761c89f2ee5SChuck Lever 		dreq->iocb = iocb;
762462d5b32SChuck Lever 
76347989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
76447989d74SChuck Lever 
765462d5b32SChuck Lever 	nfs_begin_data_update(inode);
766462d5b32SChuck Lever 
767462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
76851a7bc6cSChuck Lever 	nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
769c89f2ee5SChuck Lever 	result = nfs_direct_wait(dreq);
770462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
771462d5b32SChuck Lever 
7721da177e4SLinus Torvalds 	return result;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /**
7761da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7771da177e4SLinus Torvalds  * @iocb: target I/O control block
7781da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
77988467055SChuck Lever  * @count: number of bytes to read
78088467055SChuck Lever  * @pos: byte offset in file where reading starts
7811da177e4SLinus Torvalds  *
7821da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7831da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7841da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7851da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7861da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
78788467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7881da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7891da177e4SLinus Torvalds  * care of managing the end of file boundary.
7901da177e4SLinus Torvalds  *
7911da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7921da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7931da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
7941da177e4SLinus Torvalds  * cache.
7951da177e4SLinus Torvalds  */
796d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
7990cdd80d0SChuck Lever 	int page_count;
8000cdd80d0SChuck Lever 	struct page **pages;
8011da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8021da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8031da177e4SLinus Torvalds 
804ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
8050bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
8060bbacc40SChuck Lever 		file->f_dentry->d_name.name,
807ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	if (count < 0)
8101da177e4SLinus Torvalds 		goto out;
8111da177e4SLinus Torvalds 	retval = -EFAULT;
8120cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
8131da177e4SLinus Torvalds 		goto out;
8141da177e4SLinus Torvalds 	retval = 0;
8151da177e4SLinus Torvalds 	if (!count)
8161da177e4SLinus Torvalds 		goto out;
8171da177e4SLinus Torvalds 
81829884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8191da177e4SLinus Torvalds 	if (retval)
8201da177e4SLinus Torvalds 		goto out;
8211da177e4SLinus Torvalds 
8226b45d858STrond Myklebust 	retval = nfs_get_user_pages(READ, (unsigned long) buf,
8230cdd80d0SChuck Lever 						count, &pages);
8246b45d858STrond Myklebust 	if (retval < 0)
8250cdd80d0SChuck Lever 		goto out;
8266b45d858STrond Myklebust 	page_count = retval;
8270cdd80d0SChuck Lever 
82899514f8fSChuck Lever 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
8290cdd80d0SChuck Lever 						pages, page_count);
8301da177e4SLinus Torvalds 	if (retval > 0)
8310cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds out:
8341da177e4SLinus Torvalds 	return retval;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds /**
8381da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8391da177e4SLinus Torvalds  * @iocb: target I/O control block
8401da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
84188467055SChuck Lever  * @count: number of bytes to write
84288467055SChuck Lever  * @pos: byte offset in file where writing starts
8431da177e4SLinus Torvalds  *
8441da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8451da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8461da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8471da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8481da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8491da177e4SLinus Torvalds  * parameter checking and report problems.
8501da177e4SLinus Torvalds  *
8511da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
8521da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
8531da177e4SLinus Torvalds  *
8541da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8551da177e4SLinus Torvalds  *
8561da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8571da177e4SLinus Torvalds  * readers of this file.
8581da177e4SLinus Torvalds  *
8591da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8601da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8611da177e4SLinus Torvalds  */
862d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
8631da177e4SLinus Torvalds {
864ce1a8e67SChuck Lever 	ssize_t retval;
86547989d74SChuck Lever 	int page_count;
86647989d74SChuck Lever 	struct page **pages;
8671da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8681da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8691da177e4SLinus Torvalds 
870ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8710bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
872ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
873ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8741da177e4SLinus Torvalds 
875ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
876ce1a8e67SChuck Lever 	if (retval)
8771da177e4SLinus Torvalds 		goto out;
878ce1a8e67SChuck Lever 
879ce1a8e67SChuck Lever 	retval = -EINVAL;
880ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8811da177e4SLinus Torvalds 		goto out;
8821da177e4SLinus Torvalds 	retval = 0;
8831da177e4SLinus Torvalds 	if (!count)
8841da177e4SLinus Torvalds 		goto out;
885ce1a8e67SChuck Lever 
886ce1a8e67SChuck Lever 	retval = -EFAULT;
88747989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
888ce1a8e67SChuck Lever 		goto out;
8891da177e4SLinus Torvalds 
89029884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8911da177e4SLinus Torvalds 	if (retval)
8921da177e4SLinus Torvalds 		goto out;
8931da177e4SLinus Torvalds 
8946b45d858STrond Myklebust 	retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
89547989d74SChuck Lever 						count, &pages);
8966b45d858STrond Myklebust 	if (retval < 0)
89747989d74SChuck Lever 		goto out;
8986b45d858STrond Myklebust 	page_count = retval;
89947989d74SChuck Lever 
900c89f2ee5SChuck Lever 	retval = nfs_direct_write(iocb, (unsigned long) buf, count,
90147989d74SChuck Lever 					pos, pages, page_count);
9029eafa8ccSChuck Lever 
9039eafa8ccSChuck Lever 	/*
9049eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
9059eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
9069eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
9079eafa8ccSChuck Lever 	 *
9089eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
9099eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
9109eafa8ccSChuck Lever 	 */
9111da177e4SLinus Torvalds 	if (mapping->nrpages)
9121da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
9139eafa8ccSChuck Lever 
9141da177e4SLinus Torvalds 	if (retval > 0)
915ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds out:
9181da177e4SLinus Torvalds 	return retval;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
92188467055SChuck Lever /**
92288467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
92388467055SChuck Lever  *
92488467055SChuck Lever  */
925f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9281da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
929fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
930fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
9311da177e4SLinus Torvalds 						NULL, NULL);
9321da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9331da177e4SLinus Torvalds 		return -ENOMEM;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	return 0;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
93888467055SChuck Lever /**
939f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
94088467055SChuck Lever  *
94188467055SChuck Lever  */
942f7b422b1SDavid Howells void __exit nfs_destroy_directcache(void)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
9451da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
9461da177e4SLinus Torvalds }
947