xref: /openbmc/linux/fs/nfs/direct.c (revision 6296556f0b31eaff29f2a3aee2c17b7eae895b98)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/direct.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * High-performance uncached I/O for the Linux NFS client
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * There are important applications whose performance or correctness
91da177e4SLinus Torvalds  * depends on uncached access to file data.  Database clusters
101da177e4SLinus Torvalds  * (multiple copies of the same instance running on separate hosts)
111da177e4SLinus Torvalds  * implement their own cache coherency protocol that subsumes file
121da177e4SLinus Torvalds  * system cache protocols.  Applications that process datasets
131da177e4SLinus Torvalds  * considerably larger than the client's memory do not always benefit
141da177e4SLinus Torvalds  * from a local cache.  A streaming video server, for instance, has no
151da177e4SLinus Torvalds  * need to cache the contents of a file.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * When an application requests uncached I/O, all read and write requests
181da177e4SLinus Torvalds  * are made directly to the server; data stored or fetched via these
191da177e4SLinus Torvalds  * requests is not cached in the Linux page cache.  The client does not
201da177e4SLinus Torvalds  * correct unaligned requests from applications.  All requested bytes are
211da177e4SLinus Torvalds  * held on permanent storage before a direct write system call returns to
221da177e4SLinus Torvalds  * an application.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Solaris implements an uncached I/O facility called directio() that
251da177e4SLinus Torvalds  * is used for backups and sequential I/O to very large files.  Solaris
261da177e4SLinus Torvalds  * also supports uncaching whole NFS partitions with "-o forcedirectio,"
271da177e4SLinus Torvalds  * an undocumented mount option.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
301da177e4SLinus Torvalds  * help from Andrew Morton.
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * 18 Dec 2001	Initial implementation for 2.4  --cel
331da177e4SLinus Torvalds  * 08 Jul 2002	Version for 2.4.19, with bug fixes --trondmy
341da177e4SLinus Torvalds  * 08 Jun 2003	Port to 2.5 APIs  --cel
351da177e4SLinus Torvalds  * 31 Mar 2004	Handle direct I/O without VFS support  --cel
361da177e4SLinus Torvalds  * 15 Sep 2004	Parallel async reads  --cel
3788467055SChuck Lever  * 04 May 2005	support O_DIRECT with aio  --cel
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/file.h>
451da177e4SLinus Torvalds #include <linux/pagemap.h>
461da177e4SLinus Torvalds #include <linux/kref.h>
475a0e3ad6STejun Heo #include <linux/slab.h>
487ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h>
49*6296556fSPeng Tao #include <linux/module.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <linux/nfs_fs.h>
521da177e4SLinus Torvalds #include <linux/nfs_page.h>
531da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <asm/uaccess.h>
5660063497SArun Sharma #include <linux/atomic.h>
571da177e4SLinus Torvalds 
588d5658c9STrond Myklebust #include "internal.h"
5991d5b470SChuck Lever #include "iostat.h"
601763da12SFred Isaman #include "pnfs.h"
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
631da177e4SLinus Torvalds 
64e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds struct nfs_direct_req {
701da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7115ce4a0cSChuck Lever 
7215ce4a0cSChuck Lever 	/* I/O parameters */
73a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
74f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7599514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7688467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7715ce4a0cSChuck Lever 
7815ce4a0cSChuck Lever 	/* completion state */
79607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
8015ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
8235754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
831da177e4SLinus Torvalds 				error;		/* any reported error */
84d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
85fad61490STrond Myklebust 
86fad61490STrond Myklebust 	/* commit state */
871763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
881763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
891763da12SFred Isaman 	struct work_struct	work;
90fad61490STrond Myklebust 	int			flags;
91fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
92fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
93fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
971763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
991763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
100607f31e8STrond Myklebust 
101607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
102607f31e8STrond Myklebust {
103607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
104607f31e8STrond Myklebust }
105607f31e8STrond Myklebust 
106607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
107607f31e8STrond Myklebust {
108607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
109607f31e8STrond Myklebust }
110607f31e8STrond Myklebust 
1111da177e4SLinus Torvalds /**
112b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
113b8a32e2bSChuck Lever  * @rw: direction (read or write)
114b8a32e2bSChuck Lever  * @iocb: target I/O control block
115b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
116b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
117b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
118b8a32e2bSChuck Lever  *
119b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
120a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
121a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
122a564b8f0SMel Gorman  * so this method is only ever called for swap.
1231da177e4SLinus Torvalds  */
124b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
125b8a32e2bSChuck Lever {
126a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP
127b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
12801cce933SJosef "Jeff" Sipek 			iocb->ki_filp->f_path.dentry->d_name.name,
129e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
130b8a32e2bSChuck Lever 
131b8a32e2bSChuck Lever 	return -EINVAL;
132a564b8f0SMel Gorman #else
133a564b8f0SMel Gorman 	VM_BUG_ON(iocb->ki_left != PAGE_SIZE);
134a564b8f0SMel Gorman 	VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
135a564b8f0SMel Gorman 
136a564b8f0SMel Gorman 	if (rw == READ || rw == KERNEL_READ)
137a564b8f0SMel Gorman 		return nfs_file_direct_read(iocb, iov, nr_segs, pos,
138a564b8f0SMel Gorman 				rw == READ ? true : false);
139a564b8f0SMel Gorman 	return nfs_file_direct_write(iocb, iov, nr_segs, pos,
140a564b8f0SMel Gorman 				rw == WRITE ? true : false);
141a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */
142b8a32e2bSChuck Lever }
143b8a32e2bSChuck Lever 
144749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1459c93ab7dSChuck Lever {
146749e146eSChuck Lever 	unsigned int i;
147607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
148607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1496b45d858STrond Myklebust }
1506b45d858STrond Myklebust 
1511763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
1521763da12SFred Isaman 			      struct nfs_direct_req *dreq)
1531763da12SFred Isaman {
1541763da12SFred Isaman 	cinfo->lock = &dreq->lock;
1551763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
1561763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
1571763da12SFred Isaman 	cinfo->dreq = dreq;
1581763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
1591763da12SFred Isaman }
1601763da12SFred Isaman 
16193619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1641da177e4SLinus Torvalds 
165292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
1661da177e4SLinus Torvalds 	if (!dreq)
1671da177e4SLinus Torvalds 		return NULL;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	kref_init(&dreq->kref);
170607f31e8STrond Myklebust 	kref_get(&dreq->kref);
171d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
1721763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
1731763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
17415ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
17593619e59SChuck Lever 
17693619e59SChuck Lever 	return dreq;
17793619e59SChuck Lever }
17893619e59SChuck Lever 
179b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
182a8881f5aSTrond Myklebust 
183f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
184f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
185a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
186a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1871da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
190b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
191b4946ffbSTrond Myklebust {
192b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
193b4946ffbSTrond Myklebust }
194b4946ffbSTrond Myklebust 
195*6296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
196*6296556fSPeng Tao {
197*6296556fSPeng Tao 	return dreq->bytes_left;
198*6296556fSPeng Tao }
199*6296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
200*6296556fSPeng Tao 
201d4cc948bSChuck Lever /*
202bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
203bc0fb201SChuck Lever  */
204bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
205bc0fb201SChuck Lever {
20615ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
207bc0fb201SChuck Lever 
208bc0fb201SChuck Lever 	/* Async requests don't wait here */
209bc0fb201SChuck Lever 	if (dreq->iocb)
210bc0fb201SChuck Lever 		goto out;
211bc0fb201SChuck Lever 
212150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
213bc0fb201SChuck Lever 
214bc0fb201SChuck Lever 	if (!result)
21515ce4a0cSChuck Lever 		result = dreq->error;
216bc0fb201SChuck Lever 	if (!result)
21715ce4a0cSChuck Lever 		result = dreq->count;
218bc0fb201SChuck Lever 
219bc0fb201SChuck Lever out:
220bc0fb201SChuck Lever 	return (ssize_t) result;
221bc0fb201SChuck Lever }
222bc0fb201SChuck Lever 
223bc0fb201SChuck Lever /*
224607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
225607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
22663ab46abSChuck Lever  */
22763ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
22863ab46abSChuck Lever {
22963ab46abSChuck Lever 	if (dreq->iocb) {
23015ce4a0cSChuck Lever 		long res = (long) dreq->error;
23163ab46abSChuck Lever 		if (!res)
23215ce4a0cSChuck Lever 			res = (long) dreq->count;
23363ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
234d72b7a6bSTrond Myklebust 	}
235d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
23663ab46abSChuck Lever 
237b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
23863ab46abSChuck Lever }
23963ab46abSChuck Lever 
2401385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
2411da177e4SLinus Torvalds {
242584aa810SFred Isaman 	dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
243584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
244584aa810SFred Isaman 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
245584aa810SFred Isaman 		req->wb_bytes,
246584aa810SFred Isaman 		(long long)req_offset(req));
247584aa810SFred Isaman 	nfs_release_request(req);
248fdd1e74cSTrond Myklebust }
249fdd1e74cSTrond Myklebust 
250584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
251fdd1e74cSTrond Myklebust {
252584aa810SFred Isaman 	unsigned long bytes = 0;
253584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
254fdd1e74cSTrond Myklebust 
255584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
256584aa810SFred Isaman 		goto out_put;
2571da177e4SLinus Torvalds 
25815ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
259584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
260584aa810SFred Isaman 		dreq->error = hdr->error;
261584aa810SFred Isaman 	else
262584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
26315ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2641da177e4SLinus Torvalds 
265584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
266584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
267584aa810SFred Isaman 		struct page *page = req->wb_page;
268584aa810SFred Isaman 
269584aa810SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
270584aa810SFred Isaman 			if (bytes > hdr->good_bytes)
271584aa810SFred Isaman 				zero_user(page, 0, PAGE_SIZE);
272584aa810SFred Isaman 			else if (hdr->good_bytes - bytes < PAGE_SIZE)
273584aa810SFred Isaman 				zero_user_segment(page,
274584aa810SFred Isaman 					hdr->good_bytes & ~PAGE_MASK,
275584aa810SFred Isaman 					PAGE_SIZE);
276584aa810SFred Isaman 		}
2774bd8b010STrond Myklebust 		if (!PageCompound(page)) {
2784bd8b010STrond Myklebust 			if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
279584aa810SFred Isaman 				if (bytes < hdr->good_bytes)
2804bd8b010STrond Myklebust 					set_page_dirty(page);
2814bd8b010STrond Myklebust 			} else
2824bd8b010STrond Myklebust 				set_page_dirty(page);
2834bd8b010STrond Myklebust 		}
284584aa810SFred Isaman 		bytes += req->wb_bytes;
285584aa810SFred Isaman 		nfs_list_remove_request(req);
286584aa810SFred Isaman 		nfs_direct_readpage_release(req);
287584aa810SFred Isaman 	}
288584aa810SFred Isaman out_put:
289607f31e8STrond Myklebust 	if (put_dreq(dreq))
29063ab46abSChuck Lever 		nfs_direct_complete(dreq);
291584aa810SFred Isaman 	hdr->release(hdr);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2943e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
295cd841605SFred Isaman {
296584aa810SFred Isaman 	struct nfs_page *req;
297cd841605SFred Isaman 
298584aa810SFred Isaman 	while (!list_empty(head)) {
299584aa810SFred Isaman 		req = nfs_list_entry(head->next);
300584aa810SFred Isaman 		nfs_list_remove_request(req);
301584aa810SFred Isaman 		nfs_release_request(req);
302cd841605SFred Isaman 	}
303584aa810SFred Isaman }
304584aa810SFred Isaman 
305584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
306584aa810SFred Isaman {
307584aa810SFred Isaman 	get_dreq(hdr->dreq);
308584aa810SFred Isaman }
309584aa810SFred Isaman 
310584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
3113e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
312584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
313584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
314584aa810SFred Isaman };
315cd841605SFred Isaman 
316d4cc948bSChuck Lever /*
317607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
318607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
319607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
320607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
321607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3221da177e4SLinus Torvalds  */
323584aa810SFred Isaman static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
32402fe4946SChuck Lever 						const struct iovec *iov,
325a564b8f0SMel Gorman 						loff_t pos, bool uio)
3261da177e4SLinus Torvalds {
327584aa810SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
328a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
3293d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
33002fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
33102fe4946SChuck Lever 	size_t count = iov->iov_len;
3325dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
333607f31e8STrond Myklebust 	unsigned int pgbase;
334607f31e8STrond Myklebust 	int result;
335607f31e8STrond Myklebust 	ssize_t started = 0;
336584aa810SFred Isaman 	struct page **pagevec = NULL;
337584aa810SFred Isaman 	unsigned int npages;
33882b145c5SChuck Lever 
3391da177e4SLinus Torvalds 	do {
3405dd602f2SChuck Lever 		size_t bytes;
341584aa810SFred Isaman 		int i;
3421da177e4SLinus Torvalds 
343e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
344bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
345e9f7bee1STrond Myklebust 
346607f31e8STrond Myklebust 		result = -ENOMEM;
347584aa810SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
348584aa810SFred Isaman 		if (!pagevec)
349584aa810SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *),
350584aa810SFred Isaman 					  GFP_KERNEL);
351584aa810SFred Isaman 		if (!pagevec)
352607f31e8STrond Myklebust 			break;
353a564b8f0SMel Gorman 		if (uio) {
354607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
355607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
356584aa810SFred Isaman 					npages, 1, 0, pagevec, NULL);
357607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
358584aa810SFred Isaman 			if (result < 0)
359749e146eSChuck Lever 				break;
360a564b8f0SMel Gorman 		} else {
361a564b8f0SMel Gorman 			WARN_ON(npages != 1);
362a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 1, pagevec);
363a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
364a564b8f0SMel Gorman 				break;
365a564b8f0SMel Gorman 		}
366a564b8f0SMel Gorman 
367584aa810SFred Isaman 		if ((unsigned)result < npages) {
368d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
369d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
370584aa810SFred Isaman 				nfs_direct_release_pages(pagevec, result);
371607f31e8STrond Myklebust 				break;
372607f31e8STrond Myklebust 			}
373d9df8d6bSTrond Myklebust 			bytes -= pgbase;
374584aa810SFred Isaman 			npages = result;
375d9df8d6bSTrond Myklebust 		}
37606cf6f2eSChuck Lever 
377584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
378584aa810SFred Isaman 			struct nfs_page *req;
379bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
380584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
381584aa810SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
382584aa810SFred Isaman 						 pagevec[i],
383584aa810SFred Isaman 						 pgbase, req_len);
384584aa810SFred Isaman 			if (IS_ERR(req)) {
385584aa810SFred Isaman 				result = PTR_ERR(req);
386dbae4c73STrond Myklebust 				break;
387584aa810SFred Isaman 			}
388584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
389584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
390584aa810SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
391584aa810SFred Isaman 				result = desc->pg_error;
392584aa810SFred Isaman 				nfs_release_request(req);
393584aa810SFred Isaman 				break;
394584aa810SFred Isaman 			}
395584aa810SFred Isaman 			pgbase = 0;
396584aa810SFred Isaman 			bytes -= req_len;
397584aa810SFred Isaman 			started += req_len;
398584aa810SFred Isaman 			user_addr += req_len;
399584aa810SFred Isaman 			pos += req_len;
400584aa810SFred Isaman 			count -= req_len;
40135754bc0SPeng Tao 			dreq->bytes_left -= req_len;
402584aa810SFred Isaman 		}
4036d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
4046d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
40571e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
406607f31e8STrond Myklebust 
407584aa810SFred Isaman 	kfree(pagevec);
408584aa810SFred Isaman 
409607f31e8STrond Myklebust 	if (started)
410c216fd70SChuck Lever 		return started;
411607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
41206cf6f2eSChuck Lever }
41306cf6f2eSChuck Lever 
41419f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
41519f73787SChuck Lever 					      const struct iovec *iov,
41619f73787SChuck Lever 					      unsigned long nr_segs,
417a564b8f0SMel Gorman 					      loff_t pos, bool uio)
41819f73787SChuck Lever {
419584aa810SFred Isaman 	struct nfs_pageio_descriptor desc;
42019f73787SChuck Lever 	ssize_t result = -EINVAL;
42119f73787SChuck Lever 	size_t requested_bytes = 0;
42219f73787SChuck Lever 	unsigned long seg;
42319f73787SChuck Lever 
42459948db3SFred Isaman 	NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
425584aa810SFred Isaman 			     &nfs_direct_read_completion_ops);
42619f73787SChuck Lever 	get_dreq(dreq);
427584aa810SFred Isaman 	desc.pg_dreq = dreq;
42819f73787SChuck Lever 
42919f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
43019f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
431a564b8f0SMel Gorman 		result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
43219f73787SChuck Lever 		if (result < 0)
43319f73787SChuck Lever 			break;
43419f73787SChuck Lever 		requested_bytes += result;
43519f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
43619f73787SChuck Lever 			break;
43719f73787SChuck Lever 		pos += vec->iov_len;
43819f73787SChuck Lever 	}
43919f73787SChuck Lever 
440584aa810SFred Isaman 	nfs_pageio_complete(&desc);
441584aa810SFred Isaman 
442839f7ad6SChuck Lever 	/*
443839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
444839f7ad6SChuck Lever 	 * generic layer handle the completion.
445839f7ad6SChuck Lever 	 */
446839f7ad6SChuck Lever 	if (requested_bytes == 0) {
447839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
448839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
449839f7ad6SChuck Lever 	}
450839f7ad6SChuck Lever 
45119f73787SChuck Lever 	if (put_dreq(dreq))
45219f73787SChuck Lever 		nfs_direct_complete(dreq);
45319f73787SChuck Lever 	return 0;
45419f73787SChuck Lever }
45519f73787SChuck Lever 
456c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
457a564b8f0SMel Gorman 			       unsigned long nr_segs, loff_t pos, bool uio)
4581da177e4SLinus Torvalds {
459f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
46099514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4611da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
462b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
4631da177e4SLinus Torvalds 
464607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
465f11ac8dbSTrond Myklebust 	if (dreq == NULL)
466f11ac8dbSTrond Myklebust 		goto out;
4671da177e4SLinus Torvalds 
46891d5b470SChuck Lever 	dreq->inode = inode;
46935754bc0SPeng Tao 	dreq->bytes_left = iov_length(iov, nr_segs);
470cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
471b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
472b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
473b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
474f11ac8dbSTrond Myklebust 		goto out_release;
475b3c54de6STrond Myklebust 	}
476b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
477487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
478487b8372SChuck Lever 		dreq->iocb = iocb;
4791da177e4SLinus Torvalds 
4807acdb026SPeng Tao 	NFS_I(inode)->read_io += iov_length(iov, nr_segs);
481a564b8f0SMel Gorman 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
482607f31e8STrond Myklebust 	if (!result)
483bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
484f11ac8dbSTrond Myklebust out_release:
485b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
486f11ac8dbSTrond Myklebust out:
4871da177e4SLinus Torvalds 	return result;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901d59d61fSTrond Myklebust static void nfs_inode_dio_write_done(struct inode *inode)
4911d59d61fSTrond Myklebust {
4921d59d61fSTrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
4931d59d61fSTrond Myklebust 	inode_dio_done(inode);
4941d59d61fSTrond Myklebust }
4951d59d61fSTrond Myklebust 
49689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
497fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4981da177e4SLinus Torvalds {
4991763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
5001763da12SFred Isaman 	struct nfs_page *req, *tmp;
5011763da12SFred Isaman 	LIST_HEAD(reqs);
5021763da12SFred Isaman 	struct nfs_commit_info cinfo;
5031763da12SFred Isaman 	LIST_HEAD(failed);
5041763da12SFred Isaman 
5051763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
5061763da12SFred Isaman 	pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
5071763da12SFred Isaman 	spin_lock(cinfo.lock);
5081763da12SFred Isaman 	nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
5091763da12SFred Isaman 	spin_unlock(cinfo.lock);
5101da177e4SLinus Torvalds 
511fad61490STrond Myklebust 	dreq->count = 0;
512607f31e8STrond Myklebust 	get_dreq(dreq);
5131da177e4SLinus Torvalds 
514c95908e4SFred Isaman 	NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
5151763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
5161763da12SFred Isaman 	desc.pg_dreq = dreq;
517607f31e8STrond Myklebust 
5181763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
5191763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
5204035c248STrond Myklebust 			nfs_list_remove_request(req);
5211763da12SFred Isaman 			nfs_list_add_request(req, &failed);
5221763da12SFred Isaman 			spin_lock(cinfo.lock);
5231763da12SFred Isaman 			dreq->flags = 0;
5241763da12SFred Isaman 			dreq->error = -EIO;
5251763da12SFred Isaman 			spin_unlock(cinfo.lock);
5261763da12SFred Isaman 		}
5275a695da2STrond Myklebust 		nfs_release_request(req);
5281763da12SFred Isaman 	}
5291763da12SFred Isaman 	nfs_pageio_complete(&desc);
530607f31e8STrond Myklebust 
5314035c248STrond Myklebust 	while (!list_empty(&failed)) {
5324035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
5334035c248STrond Myklebust 		nfs_list_remove_request(req);
5341d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
5354035c248STrond Myklebust 	}
536607f31e8STrond Myklebust 
537607f31e8STrond Myklebust 	if (put_dreq(dreq))
5381763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
539fad61490STrond Myklebust }
5401da177e4SLinus Torvalds 
5411763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
542fad61490STrond Myklebust {
5430b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
5441763da12SFred Isaman 	struct nfs_commit_info cinfo;
5451763da12SFred Isaman 	struct nfs_page *req;
546c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
547c9d8f89dSTrond Myklebust 
5481763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
549c9d8f89dSTrond Myklebust 	if (status < 0) {
55060fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
551c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
552fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
55360fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
554c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
555fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
556fad61490STrond Myklebust 	}
557fad61490STrond Myklebust 
558c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
5591763da12SFred Isaman 	while (!list_empty(&data->pages)) {
5601763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
5611763da12SFred Isaman 		nfs_list_remove_request(req);
5621763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
5631763da12SFred Isaman 			/* Note the rewrite will go through mds */
5641763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
565906369e4SFred Isaman 		} else
566906369e4SFred Isaman 			nfs_release_request(req);
5671d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
568fad61490STrond Myklebust 	}
569fad61490STrond Myklebust 
5701763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
5711763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
5721763da12SFred Isaman }
5731763da12SFred Isaman 
5741763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
5751763da12SFred Isaman {
5761763da12SFred Isaman 	/* There is no lock to clear */
5771763da12SFred Isaman }
5781763da12SFred Isaman 
5791763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
5801763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
5811763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
582fad61490STrond Myklebust };
583fad61490STrond Myklebust 
584fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
585fad61490STrond Myklebust {
5861763da12SFred Isaman 	int res;
5871763da12SFred Isaman 	struct nfs_commit_info cinfo;
5881763da12SFred Isaman 	LIST_HEAD(mds_list);
589fad61490STrond Myklebust 
5901763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
5911763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
5921763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
5931763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
5941763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
5981da177e4SLinus Torvalds {
5991763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
600fad61490STrond Myklebust 	int flags = dreq->flags;
6011da177e4SLinus Torvalds 
602fad61490STrond Myklebust 	dreq->flags = 0;
603fad61490STrond Myklebust 	switch (flags) {
604fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
605fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6061da177e4SLinus Torvalds 			break;
607fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
608fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6091da177e4SLinus Torvalds 			break;
6101da177e4SLinus Torvalds 		default:
6111d59d61fSTrond Myklebust 			nfs_inode_dio_write_done(dreq->inode);
612fad61490STrond Myklebust 			nfs_direct_complete(dreq);
6131da177e4SLinus Torvalds 	}
614fad61490STrond Myklebust }
615fad61490STrond Myklebust 
6161763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
617fad61490STrond Myklebust {
6181763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
619fad61490STrond Myklebust }
6201763da12SFred Isaman 
621fad61490STrond Myklebust #else
62224fc9211SBryan Schumaker static void nfs_direct_write_schedule_work(struct work_struct *work)
62324fc9211SBryan Schumaker {
62424fc9211SBryan Schumaker }
625fad61490STrond Myklebust 
626fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
627fad61490STrond Myklebust {
6281d59d61fSTrond Myklebust 	nfs_inode_dio_write_done(inode);
629fad61490STrond Myklebust 	nfs_direct_complete(dreq);
630fad61490STrond Myklebust }
631fad61490STrond Myklebust #endif
632fad61490STrond Myklebust 
633c9d8f89dSTrond Myklebust /*
634c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
635c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
636c9d8f89dSTrond Myklebust  */
637462d5b32SChuck Lever /*
638607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
639607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
640607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
641607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
642607f31e8STrond Myklebust  * no requests have been sent, just return an error.
643462d5b32SChuck Lever  */
6441763da12SFred Isaman static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
64502fe4946SChuck Lever 						 const struct iovec *iov,
646a564b8f0SMel Gorman 						 loff_t pos, bool uio)
647462d5b32SChuck Lever {
6481763da12SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
649a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
6503d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
65102fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
65202fe4946SChuck Lever 	size_t count = iov->iov_len;
653462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
654607f31e8STrond Myklebust 	unsigned int pgbase;
655607f31e8STrond Myklebust 	int result;
656607f31e8STrond Myklebust 	ssize_t started = 0;
6571763da12SFred Isaman 	struct page **pagevec = NULL;
6581763da12SFred Isaman 	unsigned int npages;
65982b145c5SChuck Lever 
660462d5b32SChuck Lever 	do {
661462d5b32SChuck Lever 		size_t bytes;
6621763da12SFred Isaman 		int i;
663462d5b32SChuck Lever 
664e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
665bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
666e9f7bee1STrond Myklebust 
667607f31e8STrond Myklebust 		result = -ENOMEM;
6681763da12SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
6691763da12SFred Isaman 		if (!pagevec)
6701763da12SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
6711763da12SFred Isaman 		if (!pagevec)
672607f31e8STrond Myklebust 			break;
673607f31e8STrond Myklebust 
674a564b8f0SMel Gorman 		if (uio) {
675607f31e8STrond Myklebust 			down_read(&current->mm->mmap_sem);
676607f31e8STrond Myklebust 			result = get_user_pages(current, current->mm, user_addr,
6771763da12SFred Isaman 						npages, 0, 0, pagevec, NULL);
678607f31e8STrond Myklebust 			up_read(&current->mm->mmap_sem);
6791763da12SFred Isaman 			if (result < 0)
680749e146eSChuck Lever 				break;
681a564b8f0SMel Gorman 		} else {
682a564b8f0SMel Gorman 			WARN_ON(npages != 1);
683a564b8f0SMel Gorman 			result = get_kernel_page(user_addr, 0, pagevec);
684a564b8f0SMel Gorman 			if (WARN_ON(result != 1))
685a564b8f0SMel Gorman 				break;
686a564b8f0SMel Gorman 		}
6871763da12SFred Isaman 
6881763da12SFred Isaman 		if ((unsigned)result < npages) {
689d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
690d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
6911763da12SFred Isaman 				nfs_direct_release_pages(pagevec, result);
692607f31e8STrond Myklebust 				break;
693607f31e8STrond Myklebust 			}
694d9df8d6bSTrond Myklebust 			bytes -= pgbase;
6951763da12SFred Isaman 			npages = result;
696d9df8d6bSTrond Myklebust 		}
697607f31e8STrond Myklebust 
6981763da12SFred Isaman 		for (i = 0; i < npages; i++) {
6991763da12SFred Isaman 			struct nfs_page *req;
700bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
701607f31e8STrond Myklebust 
7021763da12SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
7031763da12SFred Isaman 						 pagevec[i],
7041763da12SFred Isaman 						 pgbase, req_len);
7051763da12SFred Isaman 			if (IS_ERR(req)) {
7061763da12SFred Isaman 				result = PTR_ERR(req);
707dbae4c73STrond Myklebust 				break;
7081763da12SFred Isaman 			}
7091763da12SFred Isaman 			nfs_lock_request(req);
7101763da12SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
7111763da12SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
7121763da12SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
7131763da12SFred Isaman 				result = desc->pg_error;
7141d1afcbcSTrond Myklebust 				nfs_unlock_and_release_request(req);
71571e8cc00STrond Myklebust 				break;
7161763da12SFred Isaman 			}
7171763da12SFred Isaman 			pgbase = 0;
7181763da12SFred Isaman 			bytes -= req_len;
7191763da12SFred Isaman 			started += req_len;
7201763da12SFred Isaman 			user_addr += req_len;
7211763da12SFred Isaman 			pos += req_len;
7221763da12SFred Isaman 			count -= req_len;
72335754bc0SPeng Tao 			dreq->bytes_left -= req_len;
7241763da12SFred Isaman 		}
7256d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
7266d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
72771e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
728607f31e8STrond Myklebust 
7291763da12SFred Isaman 	kfree(pagevec);
7301763da12SFred Isaman 
731607f31e8STrond Myklebust 	if (started)
732c216fd70SChuck Lever 		return started;
733607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
73406cf6f2eSChuck Lever }
73506cf6f2eSChuck Lever 
7361763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7371763da12SFred Isaman {
7381763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7391763da12SFred Isaman 	struct nfs_commit_info cinfo;
7401763da12SFred Isaman 	int bit = -1;
7411763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7421763da12SFred Isaman 
7431763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
7441763da12SFred Isaman 		goto out_put;
7451763da12SFred Isaman 
7461763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7471763da12SFred Isaman 
7481763da12SFred Isaman 	spin_lock(&dreq->lock);
7491763da12SFred Isaman 
7501763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7511763da12SFred Isaman 		dreq->flags = 0;
7521763da12SFred Isaman 		dreq->error = hdr->error;
7531763da12SFred Isaman 	}
7541763da12SFred Isaman 	if (dreq->error != 0)
7551763da12SFred Isaman 		bit = NFS_IOHDR_ERROR;
7561763da12SFred Isaman 	else {
7571763da12SFred Isaman 		dreq->count += hdr->good_bytes;
7581763da12SFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
7591763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7601763da12SFred Isaman 			bit = NFS_IOHDR_NEED_RESCHED;
7611763da12SFred Isaman 		} else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
7621763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
7631763da12SFred Isaman 				bit = NFS_IOHDR_NEED_RESCHED;
7641763da12SFred Isaman 			else if (dreq->flags == 0) {
7659bce008bSTrond Myklebust 				memcpy(&dreq->verf, hdr->verf,
7661763da12SFred Isaman 				       sizeof(dreq->verf));
7671763da12SFred Isaman 				bit = NFS_IOHDR_NEED_COMMIT;
7681763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7691763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
7709bce008bSTrond Myklebust 				if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
7711763da12SFred Isaman 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7721763da12SFred Isaman 					bit = NFS_IOHDR_NEED_RESCHED;
7731763da12SFred Isaman 				} else
7741763da12SFred Isaman 					bit = NFS_IOHDR_NEED_COMMIT;
7751763da12SFred Isaman 			}
7761763da12SFred Isaman 		}
7771763da12SFred Isaman 	}
7781763da12SFred Isaman 	spin_unlock(&dreq->lock);
7791763da12SFred Isaman 
7801763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7811763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7821763da12SFred Isaman 		nfs_list_remove_request(req);
7831763da12SFred Isaman 		switch (bit) {
7841763da12SFred Isaman 		case NFS_IOHDR_NEED_RESCHED:
7851763da12SFred Isaman 		case NFS_IOHDR_NEED_COMMIT:
78604277086STrond Myklebust 			kref_get(&req->wb_kref);
7871763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
7881763da12SFred Isaman 		}
7891d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7901763da12SFred Isaman 	}
7911763da12SFred Isaman 
7921763da12SFred Isaman out_put:
7931763da12SFred Isaman 	if (put_dreq(dreq))
7941763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
7951763da12SFred Isaman 	hdr->release(hdr);
7961763da12SFred Isaman }
7971763da12SFred Isaman 
7983e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
7993e9e0ca3STrond Myklebust {
8003e9e0ca3STrond Myklebust 	struct nfs_page *req;
8013e9e0ca3STrond Myklebust 
8023e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
8033e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
8043e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
8051d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8063e9e0ca3STrond Myklebust 	}
8073e9e0ca3STrond Myklebust }
8083e9e0ca3STrond Myklebust 
8091763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
8103e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
8111763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
8121763da12SFred Isaman 	.completion = nfs_direct_write_completion,
8131763da12SFred Isaman };
8141763da12SFred Isaman 
81519f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
81619f73787SChuck Lever 					       const struct iovec *iov,
81719f73787SChuck Lever 					       unsigned long nr_segs,
818a564b8f0SMel Gorman 					       loff_t pos, bool uio)
81919f73787SChuck Lever {
8201763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8211d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
82219f73787SChuck Lever 	ssize_t result = 0;
82319f73787SChuck Lever 	size_t requested_bytes = 0;
82419f73787SChuck Lever 	unsigned long seg;
82519f73787SChuck Lever 
826c95908e4SFred Isaman 	NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
8271763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8281763da12SFred Isaman 	desc.pg_dreq = dreq;
82919f73787SChuck Lever 	get_dreq(dreq);
8301d59d61fSTrond Myklebust 	atomic_inc(&inode->i_dio_count);
83119f73787SChuck Lever 
8327acdb026SPeng Tao 	NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
83319f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
83419f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
835a564b8f0SMel Gorman 		result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
83619f73787SChuck Lever 		if (result < 0)
83719f73787SChuck Lever 			break;
83819f73787SChuck Lever 		requested_bytes += result;
83919f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
84019f73787SChuck Lever 			break;
84119f73787SChuck Lever 		pos += vec->iov_len;
84219f73787SChuck Lever 	}
8431763da12SFred Isaman 	nfs_pageio_complete(&desc);
84419f73787SChuck Lever 
845839f7ad6SChuck Lever 	/*
846839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
847839f7ad6SChuck Lever 	 * generic layer handle the completion.
848839f7ad6SChuck Lever 	 */
849839f7ad6SChuck Lever 	if (requested_bytes == 0) {
8501d59d61fSTrond Myklebust 		inode_dio_done(inode);
851839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
852839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
853839f7ad6SChuck Lever 	}
854839f7ad6SChuck Lever 
85519f73787SChuck Lever 	if (put_dreq(dreq))
85619f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
85719f73787SChuck Lever 	return 0;
85819f73787SChuck Lever }
85919f73787SChuck Lever 
860c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
861c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
862a564b8f0SMel Gorman 				size_t count, bool uio)
863462d5b32SChuck Lever {
864f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
865c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
866462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
867b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
868462d5b32SChuck Lever 
869607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
870462d5b32SChuck Lever 	if (!dreq)
871f11ac8dbSTrond Myklebust 		goto out;
872462d5b32SChuck Lever 
873c89f2ee5SChuck Lever 	dreq->inode = inode;
87435754bc0SPeng Tao 	dreq->bytes_left = count;
875cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
876b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
877b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
878b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
879f11ac8dbSTrond Myklebust 		goto out_release;
880b3c54de6STrond Myklebust 	}
881b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
882c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
883c89f2ee5SChuck Lever 		dreq->iocb = iocb;
884462d5b32SChuck Lever 
885a564b8f0SMel Gorman 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
886607f31e8STrond Myklebust 	if (!result)
887c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
888f11ac8dbSTrond Myklebust out_release:
889b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
890f11ac8dbSTrond Myklebust out:
8911da177e4SLinus Torvalds 	return result;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds /**
8951da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8961da177e4SLinus Torvalds  * @iocb: target I/O control block
897027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
898027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
89988467055SChuck Lever  * @pos: byte offset in file where reading starts
9001da177e4SLinus Torvalds  *
9011da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
9021da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
9031da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
9041da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
9051da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
90688467055SChuck Lever  * READ where the file size could change.  Our preference is simply
9071da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
9081da177e4SLinus Torvalds  * care of managing the end of file boundary.
9091da177e4SLinus Torvalds  *
9101da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
9111da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
9121da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
9131da177e4SLinus Torvalds  * cache.
9141da177e4SLinus Torvalds  */
915027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
916a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
9191da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9201da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
921c216fd70SChuck Lever 	size_t count;
9221da177e4SLinus Torvalds 
923c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
924c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
925c216fd70SChuck Lever 
9266da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
92701cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
92801cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
929c216fd70SChuck Lever 		count, (long long) pos);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	retval = 0;
9321da177e4SLinus Torvalds 	if (!count)
9331da177e4SLinus Torvalds 		goto out;
9341da177e4SLinus Torvalds 
93529884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9361da177e4SLinus Torvalds 	if (retval)
9371da177e4SLinus Torvalds 		goto out;
9381da177e4SLinus Torvalds 
9397ec10f26SKonstantin Khlebnikov 	task_io_account_read(count);
9407ec10f26SKonstantin Khlebnikov 
941a564b8f0SMel Gorman 	retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
9421da177e4SLinus Torvalds 	if (retval > 0)
9430cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds out:
9461da177e4SLinus Torvalds 	return retval;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds /**
9501da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9511da177e4SLinus Torvalds  * @iocb: target I/O control block
952027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
953027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
95488467055SChuck Lever  * @pos: byte offset in file where writing starts
9551da177e4SLinus Torvalds  *
9561da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9571da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9581da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9591da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9601da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9611da177e4SLinus Torvalds  * parameter checking and report problems.
9621da177e4SLinus Torvalds  *
9631da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9641da177e4SLinus Torvalds  *
9651da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9661da177e4SLinus Torvalds  * readers of this file.
9671da177e4SLinus Torvalds  *
9681da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9691da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9701da177e4SLinus Torvalds  */
971027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
972a564b8f0SMel Gorman 				unsigned long nr_segs, loff_t pos, bool uio)
9731da177e4SLinus Torvalds {
974070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9751da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9761da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
977c216fd70SChuck Lever 	size_t count;
9781da177e4SLinus Torvalds 
979c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
980c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
981c216fd70SChuck Lever 
9826da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
98301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
98401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
985c216fd70SChuck Lever 		count, (long long) pos);
986027445c3SBadari Pulavarty 
987ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
988ce1a8e67SChuck Lever 	if (retval)
9891da177e4SLinus Torvalds 		goto out;
990ce1a8e67SChuck Lever 
991ce1a8e67SChuck Lever 	retval = -EINVAL;
992ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9931da177e4SLinus Torvalds 		goto out;
9941da177e4SLinus Torvalds 	retval = 0;
9951da177e4SLinus Torvalds 	if (!count)
9961da177e4SLinus Torvalds 		goto out;
997ce1a8e67SChuck Lever 
99829884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9991da177e4SLinus Torvalds 	if (retval)
10001da177e4SLinus Torvalds 		goto out;
10011da177e4SLinus Torvalds 
10027ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
10037ec10f26SKonstantin Khlebnikov 
1004a564b8f0SMel Gorman 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
10051763da12SFred Isaman 	if (retval > 0) {
10061763da12SFred Isaman 		struct inode *inode = mapping->host;
10079eafa8ccSChuck Lever 
1008ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
10091763da12SFred Isaman 		spin_lock(&inode->i_lock);
10101763da12SFred Isaman 		if (i_size_read(inode) < iocb->ki_pos)
10111763da12SFred Isaman 			i_size_write(inode, iocb->ki_pos);
10121763da12SFred Isaman 		spin_unlock(&inode->i_lock);
10131763da12SFred Isaman 	}
10141da177e4SLinus Torvalds out:
10151da177e4SLinus Torvalds 	return retval;
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
101888467055SChuck Lever /**
101988467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
102088467055SChuck Lever  *
102188467055SChuck Lever  */
1022f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10251da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1026fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1027fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
102820c2df83SPaul Mundt 						NULL);
10291da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10301da177e4SLinus Torvalds 		return -ENOMEM;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	return 0;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
103588467055SChuck Lever /**
1036f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
103788467055SChuck Lever  *
103888467055SChuck Lever  */
1039266bee88SDavid Brownell void nfs_destroy_directcache(void)
10401da177e4SLinus Torvalds {
10411a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10421da177e4SLinus Torvalds }
1043