xref: /openbmc/linux/fs/nfs/direct.c (revision 51a7bc6caec94bab256b272bffd24d00ea81c698)
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 
1296b45d858STrond Myklebust static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1306b45d858STrond Myklebust {
1316b45d858STrond Myklebust 	int i;
1326b45d858STrond Myklebust 	for (i = 0; i < npages; i++) {
1336b45d858STrond Myklebust 		struct page *page = pages[i];
1346b45d858STrond Myklebust 		if (do_dirty && !PageCompound(page))
1356b45d858STrond Myklebust 			set_page_dirty_lock(page);
1366b45d858STrond Myklebust 		page_cache_release(page);
1376b45d858STrond Myklebust 	}
1386b45d858STrond Myklebust 	kfree(pages);
1396b45d858STrond Myklebust }
1406b45d858STrond Myklebust 
141d4cc948bSChuck Lever static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	int result = -ENOMEM;
1441da177e4SLinus Torvalds 	unsigned long page_count;
1451da177e4SLinus Torvalds 	size_t array_size;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1481da177e4SLinus Torvalds 	page_count -= user_addr >> PAGE_SHIFT;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	array_size = (page_count * sizeof(struct page *));
1511da177e4SLinus Torvalds 	*pages = kmalloc(array_size, GFP_KERNEL);
1521da177e4SLinus Torvalds 	if (*pages) {
1531da177e4SLinus Torvalds 		down_read(&current->mm->mmap_sem);
1541da177e4SLinus Torvalds 		result = get_user_pages(current, current->mm, user_addr,
1551da177e4SLinus Torvalds 					page_count, (rw == READ), 0,
1561da177e4SLinus Torvalds 					*pages, NULL);
1571da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
1586b45d858STrond Myklebust 		if (result != page_count) {
159143f412eSTrond Myklebust 			/*
1606b45d858STrond Myklebust 			 * If we got fewer pages than expected from
1616b45d858STrond Myklebust 			 * get_user_pages(), the user buffer runs off the
1626b45d858STrond Myklebust 			 * end of a mapping; return EFAULT.
163143f412eSTrond Myklebust 			 */
1646b45d858STrond Myklebust 			if (result >= 0) {
165143f412eSTrond Myklebust 				nfs_free_user_pages(*pages, result, 0);
166143f412eSTrond Myklebust 				result = -EFAULT;
1676b45d858STrond Myklebust 			} else
1686b45d858STrond Myklebust 				kfree(*pages);
1696b45d858STrond Myklebust 			*pages = NULL;
170143f412eSTrond Myklebust 		}
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 	return result;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
17593619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
1801da177e4SLinus Torvalds 	if (!dreq)
1811da177e4SLinus Torvalds 		return NULL;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	kref_init(&dreq->kref);
184d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1851da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dreq->list);
186fad61490STrond Myklebust 	INIT_LIST_HEAD(&dreq->rewrite_list);
18793619e59SChuck Lever 	dreq->iocb = NULL;
188a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
18915ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
190b1c5921cSChuck Lever 	atomic_set(&dreq->io_count, 0);
19115ce4a0cSChuck Lever 	dreq->count = 0;
19215ce4a0cSChuck Lever 	dreq->error = 0;
193fad61490STrond Myklebust 	dreq->flags = 0;
19493619e59SChuck Lever 
19593619e59SChuck Lever 	return dreq;
19693619e59SChuck Lever }
19793619e59SChuck Lever 
1981da177e4SLinus Torvalds static void nfs_direct_req_release(struct kref *kref)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
201a8881f5aSTrond Myklebust 
202a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
203a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2041da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207d4cc948bSChuck Lever /*
208bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
209bc0fb201SChuck Lever  */
210bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
211bc0fb201SChuck Lever {
21215ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
213bc0fb201SChuck Lever 
214bc0fb201SChuck Lever 	/* Async requests don't wait here */
215bc0fb201SChuck Lever 	if (dreq->iocb)
216bc0fb201SChuck Lever 		goto out;
217bc0fb201SChuck Lever 
218d72b7a6bSTrond Myklebust 	result = wait_for_completion_interruptible(&dreq->completion);
219bc0fb201SChuck Lever 
220bc0fb201SChuck Lever 	if (!result)
22115ce4a0cSChuck Lever 		result = dreq->error;
222bc0fb201SChuck Lever 	if (!result)
22315ce4a0cSChuck Lever 		result = dreq->count;
224bc0fb201SChuck Lever 
225bc0fb201SChuck Lever out:
226bc0fb201SChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
227bc0fb201SChuck Lever 	return (ssize_t) result;
228bc0fb201SChuck Lever }
229bc0fb201SChuck Lever 
230bc0fb201SChuck Lever /*
23163ab46abSChuck Lever  * We must hold a reference to all the pages in this direct read request
23263ab46abSChuck Lever  * until the RPCs complete.  This could be long *after* we are woken up in
23363ab46abSChuck Lever  * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
23463ab46abSChuck Lever  *
23563ab46abSChuck Lever  * In addition, synchronous I/O uses a stack-allocated iocb.  Thus we
23663ab46abSChuck Lever  * can't trust the iocb is still valid here if this is a synchronous
23763ab46abSChuck Lever  * request.  If the waiter is woken prematurely, the iocb is long gone.
23863ab46abSChuck Lever  */
23963ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
24063ab46abSChuck Lever {
24163ab46abSChuck Lever 	nfs_free_user_pages(dreq->pages, dreq->npages, 1);
24263ab46abSChuck Lever 
24363ab46abSChuck Lever 	if (dreq->iocb) {
24415ce4a0cSChuck Lever 		long res = (long) dreq->error;
24563ab46abSChuck Lever 		if (!res)
24615ce4a0cSChuck Lever 			res = (long) dreq->count;
24763ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
248d72b7a6bSTrond Myklebust 	}
249d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
25063ab46abSChuck Lever 
25163ab46abSChuck Lever 	kref_put(&dreq->kref, nfs_direct_req_release);
25263ab46abSChuck Lever }
25363ab46abSChuck Lever 
25463ab46abSChuck Lever /*
2551da177e4SLinus Torvalds  * Note we also set the number of requests we have in the dreq when we are
2561da177e4SLinus Torvalds  * done.  This prevents races with I/O completion so we will always wait
2571da177e4SLinus Torvalds  * until all requests have been dispatched and completed.
2581da177e4SLinus Torvalds  */
2595dd602f2SChuck Lever static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	struct list_head *list;
2621da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2631da177e4SLinus Torvalds 	unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2641da177e4SLinus Torvalds 
26593619e59SChuck Lever 	dreq = nfs_direct_req_alloc();
2661da177e4SLinus Torvalds 	if (!dreq)
2671da177e4SLinus Torvalds 		return NULL;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	list = &dreq->list;
2701da177e4SLinus Torvalds 	for(;;) {
27140859d7eSChuck Lever 		struct nfs_read_data *data = nfs_readdata_alloc(rpages);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 		if (unlikely(!data)) {
2741da177e4SLinus Torvalds 			while (!list_empty(list)) {
2751da177e4SLinus Torvalds 				data = list_entry(list->next,
2761da177e4SLinus Torvalds 						  struct nfs_read_data, pages);
2771da177e4SLinus Torvalds 				list_del(&data->pages);
2781da177e4SLinus Torvalds 				nfs_readdata_free(data);
2791da177e4SLinus Torvalds 			}
2801da177e4SLinus Torvalds 			kref_put(&dreq->kref, nfs_direct_req_release);
2811da177e4SLinus Torvalds 			return NULL;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2851da177e4SLinus Torvalds 		list_add(&data->pages, list);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 		data->req = (struct nfs_page *) dreq;
288b1c5921cSChuck Lever 		get_dreq(dreq);
2891da177e4SLinus Torvalds 		if (nbytes <= rsize)
2901da177e4SLinus Torvalds 			break;
2911da177e4SLinus Torvalds 		nbytes -= rsize;
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 	kref_get(&dreq->kref);
2941da177e4SLinus Torvalds 	return dreq;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
297ec06c096STrond Myklebust static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
2981da177e4SLinus Torvalds {
299ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
3001da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
3011da177e4SLinus Torvalds 
302ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
303ec06c096STrond Myklebust 		return;
3041da177e4SLinus Torvalds 
30515ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
30615ce4a0cSChuck Lever 
30715ce4a0cSChuck Lever 	if (likely(task->tk_status >= 0))
30815ce4a0cSChuck Lever 		dreq->count += data->res.count;
3091da177e4SLinus Torvalds 	else
31015ce4a0cSChuck Lever 		dreq->error = task->tk_status;
3111da177e4SLinus Torvalds 
31215ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3131da177e4SLinus Torvalds 
314b1c5921cSChuck Lever 	if (put_dreq(dreq))
31563ab46abSChuck Lever 		nfs_direct_complete(dreq);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_direct_ops = {
319ec06c096STrond Myklebust 	.rpc_call_done = nfs_direct_read_result,
320ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
321ec06c096STrond Myklebust };
322ec06c096STrond Myklebust 
323d4cc948bSChuck Lever /*
3241da177e4SLinus Torvalds  * For each nfs_read_data struct that was allocated on the list, dispatch
3251da177e4SLinus Torvalds  * an NFS READ operation
3261da177e4SLinus Torvalds  */
327*51a7bc6cSChuck Lever static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
3281da177e4SLinus Torvalds {
329a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
330a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
3311da177e4SLinus Torvalds 	struct list_head *list = &dreq->list;
3321da177e4SLinus Torvalds 	struct page **pages = dreq->pages;
3335dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
3341da177e4SLinus Torvalds 	unsigned int curpage, pgbase;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	curpage = 0;
337*51a7bc6cSChuck Lever 	pgbase = user_addr & ~PAGE_MASK;
3381da177e4SLinus Torvalds 	do {
3391da177e4SLinus Torvalds 		struct nfs_read_data *data;
3405dd602f2SChuck Lever 		size_t bytes;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 		bytes = rsize;
3431da177e4SLinus Torvalds 		if (count < rsize)
3441da177e4SLinus Torvalds 			bytes = count;
3451da177e4SLinus Torvalds 
3465db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
3471da177e4SLinus Torvalds 		data = list_entry(list->next, struct nfs_read_data, pages);
3481da177e4SLinus Torvalds 		list_del_init(&data->pages);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		data->inode = inode;
3511da177e4SLinus Torvalds 		data->cred = ctx->cred;
3521da177e4SLinus Torvalds 		data->args.fh = NFS_FH(inode);
3531da177e4SLinus Torvalds 		data->args.context = ctx;
35488467055SChuck Lever 		data->args.offset = pos;
3551da177e4SLinus Torvalds 		data->args.pgbase = pgbase;
3561da177e4SLinus Torvalds 		data->args.pages = &pages[curpage];
3571da177e4SLinus Torvalds 		data->args.count = bytes;
3581da177e4SLinus Torvalds 		data->res.fattr = &data->fattr;
3591da177e4SLinus Torvalds 		data->res.eof = 0;
3601da177e4SLinus Torvalds 		data->res.count = bytes;
3611da177e4SLinus Torvalds 
362ec06c096STrond Myklebust 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
363ec06c096STrond Myklebust 				&nfs_read_direct_ops, data);
3641da177e4SLinus Torvalds 		NFS_PROTO(inode)->read_setup(data);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 		data->task.tk_cookie = (unsigned long) inode;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		lock_kernel();
3691da177e4SLinus Torvalds 		rpc_execute(&data->task);
3701da177e4SLinus Torvalds 		unlock_kernel();
3711da177e4SLinus Torvalds 
372606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
3731da177e4SLinus Torvalds 				data->task.tk_pid,
3741da177e4SLinus Torvalds 				inode->i_sb->s_id,
3751da177e4SLinus Torvalds 				(long long)NFS_FILEID(inode),
3761da177e4SLinus Torvalds 				bytes,
3771da177e4SLinus Torvalds 				(unsigned long long)data->args.offset);
3781da177e4SLinus Torvalds 
37988467055SChuck Lever 		pos += bytes;
3801da177e4SLinus Torvalds 		pgbase += bytes;
3811da177e4SLinus Torvalds 		curpage += pgbase >> PAGE_SHIFT;
3821da177e4SLinus Torvalds 		pgbase &= ~PAGE_MASK;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		count -= bytes;
3851da177e4SLinus Torvalds 	} while (count != 0);
3865db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
38988467055SChuck 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)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	ssize_t result;
3921da177e4SLinus Torvalds 	sigset_t oldset;
39399514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
3941da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
3951da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
3981da177e4SLinus Torvalds 	if (!dreq)
3991da177e4SLinus Torvalds 		return -ENOMEM;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	dreq->pages = pages;
4021da177e4SLinus Torvalds 	dreq->npages = nr_pages;
40391d5b470SChuck Lever 	dreq->inode = inode;
404a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
405487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
406487b8372SChuck Lever 		dreq->iocb = iocb;
4071da177e4SLinus Torvalds 
40891d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
4091da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
410*51a7bc6cSChuck Lever 	nfs_direct_read_schedule(dreq, user_addr, count, pos);
411bc0fb201SChuck Lever 	result = nfs_direct_wait(dreq);
4121da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	return result;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
417fad61490STrond Myklebust static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
4181da177e4SLinus Torvalds {
419fad61490STrond Myklebust 	list_splice_init(&dreq->rewrite_list, &dreq->list);
420fad61490STrond Myklebust 	while (!list_empty(&dreq->list)) {
421fad61490STrond Myklebust 		struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
422fad61490STrond Myklebust 		list_del(&data->pages);
423fad61490STrond Myklebust 		nfs_writedata_release(data);
424fad61490STrond Myklebust 	}
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
428fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4291da177e4SLinus Torvalds {
430fedb595cSChuck Lever 	struct inode *inode = dreq->inode;
431fedb595cSChuck Lever 	struct list_head *p;
432fedb595cSChuck Lever 	struct nfs_write_data *data;
4331da177e4SLinus Torvalds 
434fad61490STrond Myklebust 	dreq->count = 0;
435fedb595cSChuck Lever 	get_dreq(dreq);
4361da177e4SLinus Torvalds 
437fedb595cSChuck Lever 	list_for_each(p, &dreq->rewrite_list) {
438fedb595cSChuck Lever 		data = list_entry(p, struct nfs_write_data, pages);
439fedb595cSChuck Lever 
440fedb595cSChuck Lever 		get_dreq(dreq);
441fedb595cSChuck Lever 
442fedb595cSChuck Lever 		/*
443fedb595cSChuck Lever 		 * Reset data->res.
444fedb595cSChuck Lever 		 */
445fedb595cSChuck Lever 		nfs_fattr_init(&data->fattr);
446fedb595cSChuck Lever 		data->res.count = data->args.count;
447fedb595cSChuck Lever 		memset(&data->verf, 0, sizeof(data->verf));
448fedb595cSChuck Lever 
449fedb595cSChuck Lever 		/*
450fedb595cSChuck Lever 		 * Reuse data->task; data->args should not have changed
451fedb595cSChuck Lever 		 * since the original request was sent.
452fedb595cSChuck Lever 		 */
453fedb595cSChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
454fedb595cSChuck Lever 				&nfs_write_direct_ops, data);
455fedb595cSChuck Lever 		NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
456fedb595cSChuck Lever 
457fedb595cSChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
458fedb595cSChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
459fedb595cSChuck Lever 
460fedb595cSChuck Lever 		/*
461fedb595cSChuck Lever 		 * We're called via an RPC callback, so BKL is already held.
462fedb595cSChuck Lever 		 */
463fedb595cSChuck Lever 		rpc_execute(&data->task);
464fedb595cSChuck Lever 
465fedb595cSChuck Lever 		dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
466fedb595cSChuck Lever 				data->task.tk_pid,
467fedb595cSChuck Lever 				inode->i_sb->s_id,
468fedb595cSChuck Lever 				(long long)NFS_FILEID(inode),
469fedb595cSChuck Lever 				data->args.count,
470fedb595cSChuck Lever 				(unsigned long long)data->args.offset);
471fedb595cSChuck Lever 	}
472fedb595cSChuck Lever 
473fedb595cSChuck Lever 	if (put_dreq(dreq))
474fedb595cSChuck Lever 		nfs_direct_write_complete(dreq, inode);
475fad61490STrond Myklebust }
4761da177e4SLinus Torvalds 
477fad61490STrond Myklebust static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
478fad61490STrond Myklebust {
479fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
480fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
4811da177e4SLinus Torvalds 
482fad61490STrond Myklebust 	/* Call the NFS version-specific code */
483fad61490STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
484fad61490STrond Myklebust 		return;
485fad61490STrond Myklebust 	if (unlikely(task->tk_status < 0)) {
486fad61490STrond Myklebust 		dreq->error = task->tk_status;
487fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
488fad61490STrond Myklebust 	}
489fad61490STrond Myklebust 	if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
490fad61490STrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
491fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
492fad61490STrond Myklebust 	}
493fad61490STrond Myklebust 
494fad61490STrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
495fad61490STrond Myklebust 	nfs_direct_write_complete(dreq, data->inode);
496fad61490STrond Myklebust }
497fad61490STrond Myklebust 
498fad61490STrond Myklebust static const struct rpc_call_ops nfs_commit_direct_ops = {
499fad61490STrond Myklebust 	.rpc_call_done = nfs_direct_commit_result,
500fad61490STrond Myklebust 	.rpc_release = nfs_commit_release,
501fad61490STrond Myklebust };
502fad61490STrond Myklebust 
503fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
504fad61490STrond Myklebust {
505fad61490STrond Myklebust 	struct nfs_write_data *data = dreq->commit_data;
506fad61490STrond Myklebust 
507fad61490STrond Myklebust 	data->inode = dreq->inode;
508a8881f5aSTrond Myklebust 	data->cred = dreq->ctx->cred;
509fad61490STrond Myklebust 
510fad61490STrond Myklebust 	data->args.fh = NFS_FH(data->inode);
511*51a7bc6cSChuck Lever 	data->args.offset = 0;
512*51a7bc6cSChuck Lever 	data->args.count = 0;
513fad61490STrond Myklebust 	data->res.count = 0;
514fad61490STrond Myklebust 	data->res.fattr = &data->fattr;
515fad61490STrond Myklebust 	data->res.verf = &data->verf;
516fad61490STrond Myklebust 
517fad61490STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
518fad61490STrond Myklebust 				&nfs_commit_direct_ops, data);
519fad61490STrond Myklebust 	NFS_PROTO(data->inode)->commit_setup(data, 0);
520fad61490STrond Myklebust 
521fad61490STrond Myklebust 	data->task.tk_priority = RPC_PRIORITY_NORMAL;
522fad61490STrond Myklebust 	data->task.tk_cookie = (unsigned long)data->inode;
523fad61490STrond Myklebust 	/* Note: task.tk_ops->rpc_release will free dreq->commit_data */
524fad61490STrond Myklebust 	dreq->commit_data = NULL;
525fad61490STrond Myklebust 
526e99170ffSTrond Myklebust 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	lock_kernel();
529fad61490STrond Myklebust 	rpc_execute(&data->task);
5301da177e4SLinus Torvalds 	unlock_kernel();
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
5341da177e4SLinus Torvalds {
535fad61490STrond Myklebust 	int flags = dreq->flags;
5361da177e4SLinus Torvalds 
537fad61490STrond Myklebust 	dreq->flags = 0;
538fad61490STrond Myklebust 	switch (flags) {
539fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
540fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5411da177e4SLinus Torvalds 			break;
542fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
543fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5441da177e4SLinus Torvalds 			break;
5451da177e4SLinus Torvalds 		default:
546fad61490STrond Myklebust 			nfs_end_data_update(inode);
547fad61490STrond Myklebust 			if (dreq->commit_data != NULL)
548fad61490STrond Myklebust 				nfs_commit_free(dreq->commit_data);
549fad61490STrond Myklebust 			nfs_direct_free_writedata(dreq);
550fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5511da177e4SLinus Torvalds 	}
552fad61490STrond Myklebust }
553fad61490STrond Myklebust 
554fad61490STrond Myklebust static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
555fad61490STrond Myklebust {
556fad61490STrond Myklebust 	dreq->commit_data = nfs_commit_alloc(0);
557fad61490STrond Myklebust 	if (dreq->commit_data != NULL)
558fad61490STrond Myklebust 		dreq->commit_data->req = (struct nfs_page *) dreq;
559fad61490STrond Myklebust }
560fad61490STrond Myklebust #else
561fad61490STrond Myklebust static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
562fad61490STrond Myklebust {
563fad61490STrond Myklebust 	dreq->commit_data = NULL;
564fad61490STrond Myklebust }
565fad61490STrond Myklebust 
566fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
567fad61490STrond Myklebust {
568fad61490STrond Myklebust 	nfs_end_data_update(inode);
569fad61490STrond Myklebust 	nfs_direct_free_writedata(dreq);
570fad61490STrond Myklebust 	nfs_direct_complete(dreq);
571fad61490STrond Myklebust }
572fad61490STrond Myklebust #endif
573fad61490STrond Myklebust 
574462d5b32SChuck Lever static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
5751da177e4SLinus Torvalds {
576462d5b32SChuck Lever 	struct list_head *list;
577462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
578462d5b32SChuck Lever 	unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
5791da177e4SLinus Torvalds 
580462d5b32SChuck Lever 	dreq = nfs_direct_req_alloc();
581462d5b32SChuck Lever 	if (!dreq)
582462d5b32SChuck Lever 		return NULL;
5831da177e4SLinus Torvalds 
584462d5b32SChuck Lever 	list = &dreq->list;
585462d5b32SChuck Lever 	for(;;) {
586462d5b32SChuck Lever 		struct nfs_write_data *data = nfs_writedata_alloc(wpages);
5871da177e4SLinus Torvalds 
588462d5b32SChuck Lever 		if (unlikely(!data)) {
589462d5b32SChuck Lever 			while (!list_empty(list)) {
590462d5b32SChuck Lever 				data = list_entry(list->next,
591462d5b32SChuck Lever 						  struct nfs_write_data, pages);
592462d5b32SChuck Lever 				list_del(&data->pages);
593462d5b32SChuck Lever 				nfs_writedata_free(data);
594462d5b32SChuck Lever 			}
595462d5b32SChuck Lever 			kref_put(&dreq->kref, nfs_direct_req_release);
596462d5b32SChuck Lever 			return NULL;
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 
599462d5b32SChuck Lever 		INIT_LIST_HEAD(&data->pages);
600462d5b32SChuck Lever 		list_add(&data->pages, list);
6011da177e4SLinus Torvalds 
602462d5b32SChuck Lever 		data->req = (struct nfs_page *) dreq;
603b1c5921cSChuck Lever 		get_dreq(dreq);
604462d5b32SChuck Lever 		if (nbytes <= wsize)
6051da177e4SLinus Torvalds 			break;
606462d5b32SChuck Lever 		nbytes -= wsize;
607462d5b32SChuck Lever 	}
608fad61490STrond Myklebust 
609fad61490STrond Myklebust 	nfs_alloc_commit_data(dreq);
610fad61490STrond Myklebust 
611462d5b32SChuck Lever 	kref_get(&dreq->kref);
612462d5b32SChuck Lever 	return dreq;
613462d5b32SChuck Lever }
6141da177e4SLinus Torvalds 
615462d5b32SChuck Lever static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
616462d5b32SChuck Lever {
617462d5b32SChuck Lever 	struct nfs_write_data *data = calldata;
618462d5b32SChuck Lever 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
619462d5b32SChuck Lever 	int status = task->tk_status;
620462d5b32SChuck Lever 
621462d5b32SChuck Lever 	if (nfs_writeback_done(task, data) != 0)
622462d5b32SChuck Lever 		return;
623462d5b32SChuck Lever 
62415ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
625462d5b32SChuck Lever 
62615ce4a0cSChuck Lever 	if (likely(status >= 0))
62715ce4a0cSChuck Lever 		dreq->count += data->res.count;
62815ce4a0cSChuck Lever 	else
629fad61490STrond Myklebust 		dreq->error = task->tk_status;
63015ce4a0cSChuck Lever 
631fad61490STrond Myklebust 	if (data->res.verf->committed != NFS_FILE_SYNC) {
632fad61490STrond Myklebust 		switch (dreq->flags) {
633fad61490STrond Myklebust 			case 0:
634fad61490STrond Myklebust 				memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
635fad61490STrond Myklebust 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
636fad61490STrond Myklebust 				break;
637fad61490STrond Myklebust 			case NFS_ODIRECT_DO_COMMIT:
638fad61490STrond Myklebust 				if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
639fad61490STrond Myklebust 					dprintk("NFS: %5u write verify failed\n", task->tk_pid);
640fad61490STrond Myklebust 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
641fad61490STrond Myklebust 				}
642fad61490STrond Myklebust 		}
643fad61490STrond Myklebust 	}
644fad61490STrond Myklebust 
645fad61490STrond Myklebust 	spin_unlock(&dreq->lock);
646fad61490STrond Myklebust }
647fad61490STrond Myklebust 
648fad61490STrond Myklebust /*
649fad61490STrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
650fad61490STrond Myklebust  *     errors after the first one are ignored.
651fad61490STrond Myklebust  */
652fad61490STrond Myklebust static void nfs_direct_write_release(void *calldata)
653fad61490STrond Myklebust {
654fad61490STrond Myklebust 	struct nfs_write_data *data = calldata;
655fad61490STrond Myklebust 	struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
656fad61490STrond Myklebust 
657b1c5921cSChuck Lever 	if (put_dreq(dreq))
658fad61490STrond Myklebust 		nfs_direct_write_complete(dreq, data->inode);
659462d5b32SChuck Lever }
660462d5b32SChuck Lever 
661462d5b32SChuck Lever static const struct rpc_call_ops nfs_write_direct_ops = {
662462d5b32SChuck Lever 	.rpc_call_done = nfs_direct_write_result,
663fad61490STrond Myklebust 	.rpc_release = nfs_direct_write_release,
664462d5b32SChuck Lever };
665462d5b32SChuck Lever 
666462d5b32SChuck Lever /*
667462d5b32SChuck Lever  * For each nfs_write_data struct that was allocated on the list, dispatch
668462d5b32SChuck Lever  * an NFS WRITE operation
669462d5b32SChuck Lever  */
670*51a7bc6cSChuck Lever static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
671462d5b32SChuck Lever {
672a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
673a8881f5aSTrond Myklebust 	struct inode *inode = ctx->dentry->d_inode;
674462d5b32SChuck Lever 	struct list_head *list = &dreq->list;
675462d5b32SChuck Lever 	struct page **pages = dreq->pages;
676462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
677462d5b32SChuck Lever 	unsigned int curpage, pgbase;
678462d5b32SChuck Lever 
679462d5b32SChuck Lever 	curpage = 0;
680*51a7bc6cSChuck Lever 	pgbase = user_addr & ~PAGE_MASK;
681462d5b32SChuck Lever 	do {
682462d5b32SChuck Lever 		struct nfs_write_data *data;
683462d5b32SChuck Lever 		size_t bytes;
684462d5b32SChuck Lever 
685462d5b32SChuck Lever 		bytes = wsize;
686462d5b32SChuck Lever 		if (count < wsize)
687462d5b32SChuck Lever 			bytes = count;
688462d5b32SChuck Lever 
6895db3a7b2STrond Myklebust 		BUG_ON(list_empty(list));
690462d5b32SChuck Lever 		data = list_entry(list->next, struct nfs_write_data, pages);
691fad61490STrond Myklebust 		list_move_tail(&data->pages, &dreq->rewrite_list);
692462d5b32SChuck Lever 
693462d5b32SChuck Lever 		data->inode = inode;
694462d5b32SChuck Lever 		data->cred = ctx->cred;
695462d5b32SChuck Lever 		data->args.fh = NFS_FH(inode);
696462d5b32SChuck Lever 		data->args.context = ctx;
69788467055SChuck Lever 		data->args.offset = pos;
698462d5b32SChuck Lever 		data->args.pgbase = pgbase;
699462d5b32SChuck Lever 		data->args.pages = &pages[curpage];
700462d5b32SChuck Lever 		data->args.count = bytes;
701462d5b32SChuck Lever 		data->res.fattr = &data->fattr;
702462d5b32SChuck Lever 		data->res.count = bytes;
70347989d74SChuck Lever 		data->res.verf = &data->verf;
704462d5b32SChuck Lever 
705462d5b32SChuck Lever 		rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
706462d5b32SChuck Lever 				&nfs_write_direct_ops, data);
707fad61490STrond Myklebust 		NFS_PROTO(inode)->write_setup(data, sync);
708462d5b32SChuck Lever 
709462d5b32SChuck Lever 		data->task.tk_priority = RPC_PRIORITY_NORMAL;
710462d5b32SChuck Lever 		data->task.tk_cookie = (unsigned long) inode;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 		lock_kernel();
713462d5b32SChuck Lever 		rpc_execute(&data->task);
7141da177e4SLinus Torvalds 		unlock_kernel();
7151da177e4SLinus Torvalds 
716606bbba0SChuck Lever 		dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
717462d5b32SChuck Lever 				data->task.tk_pid,
718462d5b32SChuck Lever 				inode->i_sb->s_id,
719462d5b32SChuck Lever 				(long long)NFS_FILEID(inode),
720462d5b32SChuck Lever 				bytes,
721462d5b32SChuck Lever 				(unsigned long long)data->args.offset);
722462d5b32SChuck Lever 
72388467055SChuck Lever 		pos += bytes;
724462d5b32SChuck Lever 		pgbase += bytes;
725462d5b32SChuck Lever 		curpage += pgbase >> PAGE_SHIFT;
726462d5b32SChuck Lever 		pgbase &= ~PAGE_MASK;
727462d5b32SChuck Lever 
728462d5b32SChuck Lever 		count -= bytes;
729462d5b32SChuck Lever 	} while (count != 0);
7305db3a7b2STrond Myklebust 	BUG_ON(!list_empty(list));
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
73388467055SChuck 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)
734462d5b32SChuck Lever {
735462d5b32SChuck Lever 	ssize_t result;
736462d5b32SChuck Lever 	sigset_t oldset;
737c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
738462d5b32SChuck Lever 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
739462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
740fad61490STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize;
741fad61490STrond Myklebust 	int sync = 0;
742462d5b32SChuck Lever 
743fad61490STrond Myklebust 	dreq = nfs_direct_write_alloc(count, wsize);
744462d5b32SChuck Lever 	if (!dreq)
745462d5b32SChuck Lever 		return -ENOMEM;
746fad61490STrond Myklebust 	if (dreq->commit_data == NULL || count < wsize)
747fad61490STrond Myklebust 		sync = FLUSH_STABLE;
748462d5b32SChuck Lever 
749462d5b32SChuck Lever 	dreq->pages = pages;
750462d5b32SChuck Lever 	dreq->npages = nr_pages;
751c89f2ee5SChuck Lever 	dreq->inode = inode;
752a8881f5aSTrond Myklebust 	dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
753c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
754c89f2ee5SChuck Lever 		dreq->iocb = iocb;
755462d5b32SChuck Lever 
75647989d74SChuck Lever 	nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
75747989d74SChuck Lever 
758462d5b32SChuck Lever 	nfs_begin_data_update(inode);
759462d5b32SChuck Lever 
760462d5b32SChuck Lever 	rpc_clnt_sigmask(clnt, &oldset);
761*51a7bc6cSChuck Lever 	nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
762c89f2ee5SChuck Lever 	result = nfs_direct_wait(dreq);
763462d5b32SChuck Lever 	rpc_clnt_sigunmask(clnt, &oldset);
764462d5b32SChuck Lever 
7651da177e4SLinus Torvalds 	return result;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds /**
7691da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
7701da177e4SLinus Torvalds  * @iocb: target I/O control block
7711da177e4SLinus Torvalds  * @buf: user's buffer into which to read data
77288467055SChuck Lever  * @count: number of bytes to read
77388467055SChuck Lever  * @pos: byte offset in file where reading starts
7741da177e4SLinus Torvalds  *
7751da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
7761da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
7771da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
7781da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
7791da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
78088467055SChuck Lever  * READ where the file size could change.  Our preference is simply
7811da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
7821da177e4SLinus Torvalds  * care of managing the end of file boundary.
7831da177e4SLinus Torvalds  *
7841da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
7851da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
7861da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
7871da177e4SLinus Torvalds  * cache.
7881da177e4SLinus Torvalds  */
789d4cc948bSChuck Lever ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
7920cdd80d0SChuck Lever 	int page_count;
7930cdd80d0SChuck Lever 	struct page **pages;
7941da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
7951da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
7961da177e4SLinus Torvalds 
797ce1a8e67SChuck Lever 	dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
7980bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7990bbacc40SChuck Lever 		file->f_dentry->d_name.name,
800ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	if (count < 0)
8031da177e4SLinus Torvalds 		goto out;
8041da177e4SLinus Torvalds 	retval = -EFAULT;
8050cdd80d0SChuck Lever 	if (!access_ok(VERIFY_WRITE, buf, count))
8061da177e4SLinus Torvalds 		goto out;
8071da177e4SLinus Torvalds 	retval = 0;
8081da177e4SLinus Torvalds 	if (!count)
8091da177e4SLinus Torvalds 		goto out;
8101da177e4SLinus Torvalds 
81129884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8121da177e4SLinus Torvalds 	if (retval)
8131da177e4SLinus Torvalds 		goto out;
8141da177e4SLinus Torvalds 
8156b45d858STrond Myklebust 	retval = nfs_get_user_pages(READ, (unsigned long) buf,
8160cdd80d0SChuck Lever 						count, &pages);
8176b45d858STrond Myklebust 	if (retval < 0)
8180cdd80d0SChuck Lever 		goto out;
8196b45d858STrond Myklebust 	page_count = retval;
8200cdd80d0SChuck Lever 
82199514f8fSChuck Lever 	retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
8220cdd80d0SChuck Lever 						pages, page_count);
8231da177e4SLinus Torvalds 	if (retval > 0)
8240cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds out:
8271da177e4SLinus Torvalds 	return retval;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds /**
8311da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8321da177e4SLinus Torvalds  * @iocb: target I/O control block
8331da177e4SLinus Torvalds  * @buf: user's buffer from which to write data
83488467055SChuck Lever  * @count: number of bytes to write
83588467055SChuck Lever  * @pos: byte offset in file where writing starts
8361da177e4SLinus Torvalds  *
8371da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8381da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8391da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8401da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8411da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8421da177e4SLinus Torvalds  * parameter checking and report problems.
8431da177e4SLinus Torvalds  *
8441da177e4SLinus Torvalds  * We also avoid an unnecessary invocation of generic_osync_inode(),
8451da177e4SLinus Torvalds  * as it is fairly meaningless to sync the metadata of an NFS file.
8461da177e4SLinus Torvalds  *
8471da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8481da177e4SLinus Torvalds  *
8491da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8501da177e4SLinus Torvalds  * readers of this file.
8511da177e4SLinus Torvalds  *
8521da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8531da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8541da177e4SLinus Torvalds  */
855d4cc948bSChuck Lever ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
8561da177e4SLinus Torvalds {
857ce1a8e67SChuck Lever 	ssize_t retval;
85847989d74SChuck Lever 	int page_count;
85947989d74SChuck Lever 	struct page **pages;
8601da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8611da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
8621da177e4SLinus Torvalds 
863ce1a8e67SChuck Lever 	dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
8640bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
865ce1a8e67SChuck Lever 		file->f_dentry->d_name.name,
866ce1a8e67SChuck Lever 		(unsigned long) count, (long long) pos);
8671da177e4SLinus Torvalds 
868ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
869ce1a8e67SChuck Lever 	if (retval)
8701da177e4SLinus Torvalds 		goto out;
871ce1a8e67SChuck Lever 
872ce1a8e67SChuck Lever 	retval = -EINVAL;
873ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
8741da177e4SLinus Torvalds 		goto out;
8751da177e4SLinus Torvalds 	retval = 0;
8761da177e4SLinus Torvalds 	if (!count)
8771da177e4SLinus Torvalds 		goto out;
878ce1a8e67SChuck Lever 
879ce1a8e67SChuck Lever 	retval = -EFAULT;
88047989d74SChuck Lever 	if (!access_ok(VERIFY_READ, buf, count))
881ce1a8e67SChuck Lever 		goto out;
8821da177e4SLinus Torvalds 
88329884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8841da177e4SLinus Torvalds 	if (retval)
8851da177e4SLinus Torvalds 		goto out;
8861da177e4SLinus Torvalds 
8876b45d858STrond Myklebust 	retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
88847989d74SChuck Lever 						count, &pages);
8896b45d858STrond Myklebust 	if (retval < 0)
89047989d74SChuck Lever 		goto out;
8916b45d858STrond Myklebust 	page_count = retval;
89247989d74SChuck Lever 
893c89f2ee5SChuck Lever 	retval = nfs_direct_write(iocb, (unsigned long) buf, count,
89447989d74SChuck Lever 					pos, pages, page_count);
8959eafa8ccSChuck Lever 
8969eafa8ccSChuck Lever 	/*
8979eafa8ccSChuck Lever 	 * XXX: nfs_end_data_update() already ensures this file's
8989eafa8ccSChuck Lever 	 *      cached data is subsequently invalidated.  Do we really
8999eafa8ccSChuck Lever 	 *      need to call invalidate_inode_pages2() again here?
9009eafa8ccSChuck Lever 	 *
9019eafa8ccSChuck Lever 	 *      For aio writes, this invalidation will almost certainly
9029eafa8ccSChuck Lever 	 *      occur before the writes complete.  Kind of racey.
9039eafa8ccSChuck Lever 	 */
9041da177e4SLinus Torvalds 	if (mapping->nrpages)
9051da177e4SLinus Torvalds 		invalidate_inode_pages2(mapping);
9069eafa8ccSChuck Lever 
9071da177e4SLinus Torvalds 	if (retval > 0)
908ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds out:
9111da177e4SLinus Torvalds 	return retval;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
91488467055SChuck Lever /**
91588467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
91688467055SChuck Lever  *
91788467055SChuck Lever  */
918f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9211da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
922fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
923fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
9241da177e4SLinus Torvalds 						NULL, NULL);
9251da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9261da177e4SLinus Torvalds 		return -ENOMEM;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	return 0;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
93188467055SChuck Lever /**
932f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
93388467055SChuck Lever  *
93488467055SChuck Lever  */
935f7b422b1SDavid Howells void __exit nfs_destroy_directcache(void)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_direct_cachep))
9381da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
9391da177e4SLinus Torvalds }
940