xref: /openbmc/linux/fs/nfs/direct.c (revision 1763da1234cba663b849476d451bdccac5147859)
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>
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/uaccess.h>
5560063497SArun Sharma #include <linux/atomic.h>
561da177e4SLinus Torvalds 
578d5658c9STrond Myklebust #include "internal.h"
5891d5b470SChuck Lever #include "iostat.h"
59*1763da12SFred Isaman #include "pnfs.h"
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
621da177e4SLinus Torvalds 
63e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds struct nfs_direct_req {
691da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7015ce4a0cSChuck Lever 
7115ce4a0cSChuck Lever 	/* I/O parameters */
72a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
73f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7499514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7588467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7615ce4a0cSChuck Lever 
7715ce4a0cSChuck Lever 	/* completion state */
78607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
7915ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8015ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
811da177e4SLinus Torvalds 				error;		/* any reported error */
82d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
83fad61490STrond Myklebust 
84fad61490STrond Myklebust 	/* commit state */
85*1763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
86*1763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
87*1763da12SFred Isaman 	struct work_struct	work;
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 
94*1763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
95*1763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
96fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
97*1763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
98607f31e8STrond Myklebust 
99607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
100607f31e8STrond Myklebust {
101607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
102607f31e8STrond Myklebust }
103607f31e8STrond Myklebust 
104607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
105607f31e8STrond Myklebust {
106607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
107607f31e8STrond Myklebust }
108607f31e8STrond Myklebust 
1091da177e4SLinus Torvalds /**
110b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
111b8a32e2bSChuck Lever  * @rw: direction (read or write)
112b8a32e2bSChuck Lever  * @iocb: target I/O control block
113b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
114b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
115b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
116b8a32e2bSChuck Lever  *
117b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
118b8a32e2bSChuck Lever  * the NFS client supports direct I/O.  However, we shunt off direct
119b8a32e2bSChuck Lever  * read and write requests before the VFS gets them, so this method
120b8a32e2bSChuck Lever  * should never be called.
1211da177e4SLinus Torvalds  */
122b8a32e2bSChuck Lever ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
123b8a32e2bSChuck Lever {
124b8a32e2bSChuck Lever 	dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
12501cce933SJosef "Jeff" Sipek 			iocb->ki_filp->f_path.dentry->d_name.name,
126e99170ffSTrond Myklebust 			(long long) pos, nr_segs);
127b8a32e2bSChuck Lever 
128b8a32e2bSChuck Lever 	return -EINVAL;
129b8a32e2bSChuck Lever }
130b8a32e2bSChuck Lever 
131749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
1329c93ab7dSChuck Lever {
133749e146eSChuck Lever 	unsigned int i;
134607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
135607f31e8STrond Myklebust 		page_cache_release(pages[i]);
1366b45d858STrond Myklebust }
1376b45d858STrond Myklebust 
138*1763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
139*1763da12SFred Isaman 			      struct nfs_direct_req *dreq)
140*1763da12SFred Isaman {
141*1763da12SFred Isaman 	cinfo->lock = &dreq->lock;
142*1763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
143*1763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
144*1763da12SFred Isaman 	cinfo->dreq = dreq;
145*1763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
146*1763da12SFred Isaman }
147*1763da12SFred Isaman 
14893619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
1511da177e4SLinus Torvalds 
152e94b1766SChristoph Lameter 	dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
1531da177e4SLinus Torvalds 	if (!dreq)
1541da177e4SLinus Torvalds 		return NULL;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	kref_init(&dreq->kref);
157607f31e8STrond Myklebust 	kref_get(&dreq->kref);
158d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
159*1763da12SFred Isaman 	dreq->mds_cinfo.ncommit = 0;
160*1763da12SFred Isaman 	atomic_set(&dreq->mds_cinfo.rpcs_out, 0);
161*1763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
162*1763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
163*1763da12SFred Isaman 	memset(&dreq->ds_cinfo, 0, sizeof(dreq->ds_cinfo));
16493619e59SChuck Lever 	dreq->iocb = NULL;
165a8881f5aSTrond Myklebust 	dreq->ctx = NULL;
166f11ac8dbSTrond Myklebust 	dreq->l_ctx = NULL;
16715ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
168607f31e8STrond Myklebust 	atomic_set(&dreq->io_count, 0);
16915ce4a0cSChuck Lever 	dreq->count = 0;
17015ce4a0cSChuck Lever 	dreq->error = 0;
171fad61490STrond Myklebust 	dreq->flags = 0;
17293619e59SChuck Lever 
17393619e59SChuck Lever 	return dreq;
17493619e59SChuck Lever }
17593619e59SChuck Lever 
176b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
179a8881f5aSTrond Myklebust 
180f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
181f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
182a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
183a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1841da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
187b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
188b4946ffbSTrond Myklebust {
189b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
190b4946ffbSTrond Myklebust }
191b4946ffbSTrond Myklebust 
192d4cc948bSChuck Lever /*
193bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
194bc0fb201SChuck Lever  */
195bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
196bc0fb201SChuck Lever {
19715ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
198bc0fb201SChuck Lever 
199bc0fb201SChuck Lever 	/* Async requests don't wait here */
200bc0fb201SChuck Lever 	if (dreq->iocb)
201bc0fb201SChuck Lever 		goto out;
202bc0fb201SChuck Lever 
203150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
204bc0fb201SChuck Lever 
205bc0fb201SChuck Lever 	if (!result)
20615ce4a0cSChuck Lever 		result = dreq->error;
207bc0fb201SChuck Lever 	if (!result)
20815ce4a0cSChuck Lever 		result = dreq->count;
209bc0fb201SChuck Lever 
210bc0fb201SChuck Lever out:
211bc0fb201SChuck Lever 	return (ssize_t) result;
212bc0fb201SChuck Lever }
213bc0fb201SChuck Lever 
214bc0fb201SChuck Lever /*
215607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
216607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
21763ab46abSChuck Lever  */
21863ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
21963ab46abSChuck Lever {
22063ab46abSChuck Lever 	if (dreq->iocb) {
22115ce4a0cSChuck Lever 		long res = (long) dreq->error;
22263ab46abSChuck Lever 		if (!res)
22315ce4a0cSChuck Lever 			res = (long) dreq->count;
22463ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
225d72b7a6bSTrond Myklebust 	}
226d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
22763ab46abSChuck Lever 
228b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
22963ab46abSChuck Lever }
23063ab46abSChuck Lever 
231584aa810SFred Isaman void nfs_direct_readpage_release(struct nfs_page *req)
2321da177e4SLinus Torvalds {
233584aa810SFred Isaman 	dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
234584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
235584aa810SFred Isaman 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
236584aa810SFred Isaman 		req->wb_bytes,
237584aa810SFred Isaman 		(long long)req_offset(req));
238584aa810SFred Isaman 	nfs_release_request(req);
239fdd1e74cSTrond Myklebust }
240fdd1e74cSTrond Myklebust 
241584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
242fdd1e74cSTrond Myklebust {
243584aa810SFred Isaman 	unsigned long bytes = 0;
244584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
245fdd1e74cSTrond Myklebust 
246584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
247584aa810SFred Isaman 		goto out_put;
2481da177e4SLinus Torvalds 
24915ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
250584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
251584aa810SFred Isaman 		dreq->error = hdr->error;
252584aa810SFred Isaman 	else
253584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
25415ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2551da177e4SLinus Torvalds 
256584aa810SFred Isaman 	if (!test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
257584aa810SFred Isaman 		while (!list_empty(&hdr->pages)) {
258584aa810SFred Isaman 			struct nfs_page *req = nfs_list_entry(hdr->pages.next);
259584aa810SFred Isaman 			struct page *page = req->wb_page;
260584aa810SFred Isaman 
261584aa810SFred Isaman 			if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
262584aa810SFred Isaman 				if (bytes > hdr->good_bytes)
263584aa810SFred Isaman 					zero_user(page, 0, PAGE_SIZE);
264584aa810SFred Isaman 				else if (hdr->good_bytes - bytes < PAGE_SIZE)
265584aa810SFred Isaman 					zero_user_segment(page,
266584aa810SFred Isaman 						hdr->good_bytes & ~PAGE_MASK,
267584aa810SFred Isaman 						PAGE_SIZE);
268584aa810SFred Isaman 			}
269584aa810SFred Isaman 			bytes += req->wb_bytes;
270584aa810SFred Isaman 			nfs_list_remove_request(req);
271584aa810SFred Isaman 			nfs_direct_readpage_release(req);
272584aa810SFred Isaman 			if (!PageCompound(page))
273584aa810SFred Isaman 				set_page_dirty(page);
274584aa810SFred Isaman 			page_cache_release(page);
275584aa810SFred Isaman 		}
276584aa810SFred Isaman 	} else {
277584aa810SFred Isaman 		while (!list_empty(&hdr->pages)) {
278584aa810SFred Isaman 			struct nfs_page *req = nfs_list_entry(hdr->pages.next);
279584aa810SFred Isaman 
280584aa810SFred Isaman 			if (bytes < hdr->good_bytes)
281584aa810SFred Isaman 				if (!PageCompound(req->wb_page))
282584aa810SFred Isaman 					set_page_dirty(req->wb_page);
283584aa810SFred Isaman 			bytes += req->wb_bytes;
284584aa810SFred Isaman 			page_cache_release(req->wb_page);
285584aa810SFred Isaman 			nfs_list_remove_request(req);
286584aa810SFred Isaman 			nfs_direct_readpage_release(req);
287584aa810SFred Isaman 		}
288584aa810SFred Isaman 	}
289584aa810SFred Isaman out_put:
290607f31e8STrond Myklebust 	if (put_dreq(dreq))
29163ab46abSChuck Lever 		nfs_direct_complete(dreq);
292584aa810SFred Isaman 	hdr->release(hdr);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
295584aa810SFred Isaman static void nfs_sync_pgio_error(struct list_head *head)
296cd841605SFred Isaman {
297584aa810SFred Isaman 	struct nfs_page *req;
298cd841605SFred Isaman 
299584aa810SFred Isaman 	while (!list_empty(head)) {
300584aa810SFred Isaman 		req = nfs_list_entry(head->next);
301584aa810SFred Isaman 		nfs_list_remove_request(req);
302584aa810SFred Isaman 		nfs_release_request(req);
303cd841605SFred Isaman 	}
304584aa810SFred Isaman }
305584aa810SFred Isaman 
306584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
307584aa810SFred Isaman {
308584aa810SFred Isaman 	get_dreq(hdr->dreq);
309584aa810SFred Isaman }
310584aa810SFred Isaman 
311584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
312584aa810SFred Isaman 	.error_cleanup = nfs_sync_pgio_error,
313584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
314584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
315584aa810SFred Isaman };
316cd841605SFred Isaman 
317d4cc948bSChuck Lever /*
318607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
319607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
320607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
321607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
322607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3231da177e4SLinus Torvalds  */
324584aa810SFred Isaman static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
32502fe4946SChuck Lever 						const struct iovec *iov,
32602fe4946SChuck Lever 						loff_t pos)
3271da177e4SLinus Torvalds {
328584aa810SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
329a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
3303d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
33102fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
33202fe4946SChuck Lever 	size_t count = iov->iov_len;
3335dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
334607f31e8STrond Myklebust 	unsigned int pgbase;
335607f31e8STrond Myklebust 	int result;
336607f31e8STrond Myklebust 	ssize_t started = 0;
337584aa810SFred Isaman 	struct page **pagevec = NULL;
338584aa810SFred Isaman 	unsigned int npages;
33982b145c5SChuck Lever 
3401da177e4SLinus Torvalds 	do {
3415dd602f2SChuck Lever 		size_t bytes;
342584aa810SFred Isaman 		int i;
3431da177e4SLinus Torvalds 
344e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
345584aa810SFred Isaman 		bytes = min(max(rsize, PAGE_SIZE), count);
346e9f7bee1STrond Myklebust 
347607f31e8STrond Myklebust 		result = -ENOMEM;
348584aa810SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
349584aa810SFred Isaman 		if (!pagevec)
350584aa810SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *),
351584aa810SFred Isaman 					  GFP_KERNEL);
352584aa810SFred Isaman 		if (!pagevec)
353607f31e8STrond Myklebust 			break;
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;
360584aa810SFred Isaman 		if ((unsigned)result < npages) {
361d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
362d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
363584aa810SFred Isaman 				nfs_direct_release_pages(pagevec, result);
364607f31e8STrond Myklebust 				break;
365607f31e8STrond Myklebust 			}
366d9df8d6bSTrond Myklebust 			bytes -= pgbase;
367584aa810SFred Isaman 			npages = result;
368d9df8d6bSTrond Myklebust 		}
36906cf6f2eSChuck Lever 
370584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
371584aa810SFred Isaman 			struct nfs_page *req;
372584aa810SFred Isaman 			unsigned int req_len = min(bytes, PAGE_SIZE - pgbase);
373584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
374584aa810SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
375584aa810SFred Isaman 						 pagevec[i],
376584aa810SFred Isaman 						 pgbase, req_len);
377584aa810SFred Isaman 			if (IS_ERR(req)) {
378584aa810SFred Isaman 				nfs_direct_release_pages(pagevec + i,
379584aa810SFred Isaman 							 npages - i);
380584aa810SFred Isaman 				result = PTR_ERR(req);
381dbae4c73STrond Myklebust 				break;
382584aa810SFred Isaman 			}
383584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
384584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
385584aa810SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
386584aa810SFred Isaman 				result = desc->pg_error;
387584aa810SFred Isaman 				nfs_release_request(req);
388584aa810SFred Isaman 				nfs_direct_release_pages(pagevec + i,
389584aa810SFred Isaman 							 npages - i);
390584aa810SFred Isaman 				break;
391584aa810SFred Isaman 			}
392584aa810SFred Isaman 			pgbase = 0;
393584aa810SFred Isaman 			bytes -= req_len;
394584aa810SFred Isaman 			started += req_len;
395584aa810SFred Isaman 			user_addr += req_len;
396584aa810SFred Isaman 			pos += req_len;
397584aa810SFred Isaman 			count -= req_len;
398584aa810SFred Isaman 		}
3991da177e4SLinus Torvalds 	} while (count != 0);
400607f31e8STrond Myklebust 
401584aa810SFred Isaman 	kfree(pagevec);
402584aa810SFred Isaman 
403607f31e8STrond Myklebust 	if (started)
404c216fd70SChuck Lever 		return started;
405607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
40606cf6f2eSChuck Lever }
40706cf6f2eSChuck Lever 
40819f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
40919f73787SChuck Lever 					      const struct iovec *iov,
41019f73787SChuck Lever 					      unsigned long nr_segs,
41119f73787SChuck Lever 					      loff_t pos)
41219f73787SChuck Lever {
413584aa810SFred Isaman 	struct nfs_pageio_descriptor desc;
41419f73787SChuck Lever 	ssize_t result = -EINVAL;
41519f73787SChuck Lever 	size_t requested_bytes = 0;
41619f73787SChuck Lever 	unsigned long seg;
41719f73787SChuck Lever 
418584aa810SFred Isaman 	nfs_pageio_init_read(&desc, dreq->inode,
419584aa810SFred Isaman 			     &nfs_direct_read_completion_ops);
42019f73787SChuck Lever 	get_dreq(dreq);
421584aa810SFred Isaman 	desc.pg_dreq = dreq;
42219f73787SChuck Lever 
42319f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
42419f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
425584aa810SFred Isaman 		result = nfs_direct_read_schedule_segment(&desc, vec, pos);
42619f73787SChuck Lever 		if (result < 0)
42719f73787SChuck Lever 			break;
42819f73787SChuck Lever 		requested_bytes += result;
42919f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
43019f73787SChuck Lever 			break;
43119f73787SChuck Lever 		pos += vec->iov_len;
43219f73787SChuck Lever 	}
43319f73787SChuck Lever 
434584aa810SFred Isaman 	nfs_pageio_complete(&desc);
435584aa810SFred Isaman 
436839f7ad6SChuck Lever 	/*
437839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
438839f7ad6SChuck Lever 	 * generic layer handle the completion.
439839f7ad6SChuck Lever 	 */
440839f7ad6SChuck Lever 	if (requested_bytes == 0) {
441839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
442839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
443839f7ad6SChuck Lever 	}
444839f7ad6SChuck Lever 
44519f73787SChuck Lever 	if (put_dreq(dreq))
44619f73787SChuck Lever 		nfs_direct_complete(dreq);
44719f73787SChuck Lever 	return 0;
44819f73787SChuck Lever }
44919f73787SChuck Lever 
450c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
451c216fd70SChuck Lever 			       unsigned long nr_segs, loff_t pos)
4521da177e4SLinus Torvalds {
453f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
45499514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4551da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4561da177e4SLinus Torvalds 
457607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
458f11ac8dbSTrond Myklebust 	if (dreq == NULL)
459f11ac8dbSTrond Myklebust 		goto out;
4601da177e4SLinus Torvalds 
46191d5b470SChuck Lever 	dreq->inode = inode;
462cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
463f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
464f11ac8dbSTrond Myklebust 	if (dreq->l_ctx == NULL)
465f11ac8dbSTrond Myklebust 		goto out_release;
466487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
467487b8372SChuck Lever 		dreq->iocb = iocb;
4681da177e4SLinus Torvalds 
469c216fd70SChuck Lever 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
470607f31e8STrond Myklebust 	if (!result)
471bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
472f11ac8dbSTrond Myklebust out_release:
473b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
474f11ac8dbSTrond Myklebust out:
4751da177e4SLinus Torvalds 	return result;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
479fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4801da177e4SLinus Torvalds {
481*1763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
482*1763da12SFred Isaman 	struct nfs_page *req, *tmp;
483*1763da12SFred Isaman 	LIST_HEAD(reqs);
484*1763da12SFred Isaman 	struct nfs_commit_info cinfo;
485*1763da12SFred Isaman 	LIST_HEAD(failed);
486*1763da12SFred Isaman 
487*1763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
488*1763da12SFred Isaman 	pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
489*1763da12SFred Isaman 	spin_lock(cinfo.lock);
490*1763da12SFred Isaman 	nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
491*1763da12SFred Isaman 	spin_unlock(cinfo.lock);
4921da177e4SLinus Torvalds 
493fad61490STrond Myklebust 	dreq->count = 0;
494607f31e8STrond Myklebust 	get_dreq(dreq);
4951da177e4SLinus Torvalds 
496*1763da12SFred Isaman 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE,
497*1763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
498*1763da12SFred Isaman 	desc.pg_dreq = dreq;
499607f31e8STrond Myklebust 
500*1763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
501*1763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
502*1763da12SFred Isaman 			nfs_list_add_request(req, &failed);
503*1763da12SFred Isaman 			spin_lock(cinfo.lock);
504*1763da12SFred Isaman 			dreq->flags = 0;
505*1763da12SFred Isaman 			dreq->error = -EIO;
506*1763da12SFred Isaman 			spin_unlock(cinfo.lock);
507*1763da12SFred Isaman 		}
508*1763da12SFred Isaman 	}
509*1763da12SFred Isaman 	nfs_pageio_complete(&desc);
510607f31e8STrond Myklebust 
511*1763da12SFred Isaman 	while (!list_empty(&failed)) {
512*1763da12SFred Isaman 		page_cache_release(req->wb_page);
513*1763da12SFred Isaman 		nfs_release_request(req);
514*1763da12SFred Isaman 		nfs_unlock_request(req);
515607f31e8STrond Myklebust 	}
516607f31e8STrond Myklebust 
517607f31e8STrond Myklebust 	if (put_dreq(dreq))
518*1763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
519fad61490STrond Myklebust }
5201da177e4SLinus Torvalds 
521*1763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
522fad61490STrond Myklebust {
5230b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
524*1763da12SFred Isaman 	struct nfs_commit_info cinfo;
525*1763da12SFred Isaman 	struct nfs_page *req;
526c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
527c9d8f89dSTrond Myklebust 
528*1763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
529c9d8f89dSTrond Myklebust 	if (status < 0) {
53060fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
531c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
532fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
53360fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
534c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
535fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
536fad61490STrond Myklebust 	}
537fad61490STrond Myklebust 
538c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
539*1763da12SFred Isaman 	while (!list_empty(&data->pages)) {
540*1763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
541*1763da12SFred Isaman 		nfs_list_remove_request(req);
542*1763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
543*1763da12SFred Isaman 			/* Note the rewrite will go through mds */
544*1763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
545*1763da12SFred Isaman 		} else {
546*1763da12SFred Isaman 			page_cache_release(req->wb_page);
547*1763da12SFred Isaman 			nfs_release_request(req);
548*1763da12SFred Isaman 		}
549*1763da12SFred Isaman 		nfs_unlock_request(req);
550fad61490STrond Myklebust 	}
551fad61490STrond Myklebust 
552*1763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
553*1763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
554*1763da12SFred Isaman }
555*1763da12SFred Isaman 
556*1763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
557*1763da12SFred Isaman {
558*1763da12SFred Isaman 	/* There is no lock to clear */
559*1763da12SFred Isaman }
560*1763da12SFred Isaman 
561*1763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
562*1763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
563*1763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
564fad61490STrond Myklebust };
565fad61490STrond Myklebust 
566fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
567fad61490STrond Myklebust {
568*1763da12SFred Isaman 	int res;
569*1763da12SFred Isaman 	struct nfs_commit_info cinfo;
570*1763da12SFred Isaman 	LIST_HEAD(mds_list);
571fad61490STrond Myklebust 
572*1763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
573*1763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
574*1763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
575*1763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
576*1763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
579*1763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
5801da177e4SLinus Torvalds {
581*1763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
582fad61490STrond Myklebust 	int flags = dreq->flags;
5831da177e4SLinus Torvalds 
584fad61490STrond Myklebust 	dreq->flags = 0;
585fad61490STrond Myklebust 	switch (flags) {
586fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
587fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5881da177e4SLinus Torvalds 			break;
589fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
590fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5911da177e4SLinus Torvalds 			break;
5921da177e4SLinus Torvalds 		default:
593*1763da12SFred Isaman 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
594fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5951da177e4SLinus Torvalds 	}
596fad61490STrond Myklebust }
597fad61490STrond Myklebust 
598*1763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
599fad61490STrond Myklebust {
600*1763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
601fad61490STrond Myklebust }
602*1763da12SFred Isaman 
603fad61490STrond Myklebust #else
604fad61490STrond Myklebust 
605fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
606fad61490STrond Myklebust {
607cd9ae2b6STrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
608fad61490STrond Myklebust 	nfs_direct_complete(dreq);
609fad61490STrond Myklebust }
610fad61490STrond Myklebust #endif
611fad61490STrond Myklebust 
612c9d8f89dSTrond Myklebust /*
613c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
614c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
615c9d8f89dSTrond Myklebust  */
616462d5b32SChuck Lever /*
617607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
618607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
619607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
620607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
621607f31e8STrond Myklebust  * no requests have been sent, just return an error.
622462d5b32SChuck Lever  */
623*1763da12SFred Isaman static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
62402fe4946SChuck Lever 						 const struct iovec *iov,
625*1763da12SFred Isaman 						 loff_t pos)
626462d5b32SChuck Lever {
627*1763da12SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
628a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
6293d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
63002fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
63102fe4946SChuck Lever 	size_t count = iov->iov_len;
632462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
633607f31e8STrond Myklebust 	unsigned int pgbase;
634607f31e8STrond Myklebust 	int result;
635607f31e8STrond Myklebust 	ssize_t started = 0;
636*1763da12SFred Isaman 	struct page **pagevec = NULL;
637*1763da12SFred Isaman 	unsigned int npages;
63882b145c5SChuck Lever 
639462d5b32SChuck Lever 	do {
640462d5b32SChuck Lever 		size_t bytes;
641*1763da12SFred Isaman 		int i;
642462d5b32SChuck Lever 
643e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
644*1763da12SFred Isaman 		bytes = min(max(wsize, PAGE_SIZE), count);
645e9f7bee1STrond Myklebust 
646607f31e8STrond Myklebust 		result = -ENOMEM;
647*1763da12SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
648*1763da12SFred Isaman 		if (!pagevec)
649*1763da12SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
650*1763da12SFred Isaman 		if (!pagevec)
651607f31e8STrond Myklebust 			break;
652607f31e8STrond Myklebust 
653607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
654607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
655*1763da12SFred Isaman 					npages, 0, 0, pagevec, NULL);
656607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
657*1763da12SFred Isaman 		if (result < 0)
658749e146eSChuck Lever 			break;
659*1763da12SFred Isaman 
660*1763da12SFred Isaman 		if ((unsigned)result < npages) {
661d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
662d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
663*1763da12SFred Isaman 				nfs_direct_release_pages(pagevec, result);
664607f31e8STrond Myklebust 				break;
665607f31e8STrond Myklebust 			}
666d9df8d6bSTrond Myklebust 			bytes -= pgbase;
667*1763da12SFred Isaman 			npages = result;
668d9df8d6bSTrond Myklebust 		}
669607f31e8STrond Myklebust 
670*1763da12SFred Isaman 		for (i = 0; i < npages; i++) {
671*1763da12SFred Isaman 			struct nfs_page *req;
672*1763da12SFred Isaman 			unsigned int req_len = min(bytes, PAGE_SIZE - pgbase);
673607f31e8STrond Myklebust 
674*1763da12SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
675*1763da12SFred Isaman 						 pagevec[i],
676*1763da12SFred Isaman 						 pgbase, req_len);
677*1763da12SFred Isaman 			if (IS_ERR(req)) {
678*1763da12SFred Isaman 				nfs_direct_release_pages(pagevec + i,
679*1763da12SFred Isaman 							 npages - i);
680*1763da12SFred Isaman 				result = PTR_ERR(req);
681dbae4c73STrond Myklebust 				break;
682*1763da12SFred Isaman 			}
683*1763da12SFred Isaman 			nfs_lock_request(req);
684*1763da12SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
685*1763da12SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
686*1763da12SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
687*1763da12SFred Isaman 				result = desc->pg_error;
688*1763da12SFred Isaman 				nfs_unlock_request(req);
689*1763da12SFred Isaman 				nfs_release_request(req);
690*1763da12SFred Isaman 				nfs_direct_release_pages(pagevec + i,
691*1763da12SFred Isaman 							 npages - i);
692*1763da12SFred Isaman 			}
693*1763da12SFred Isaman 			pgbase = 0;
694*1763da12SFred Isaman 			bytes -= req_len;
695*1763da12SFred Isaman 			started += req_len;
696*1763da12SFred Isaman 			user_addr += req_len;
697*1763da12SFred Isaman 			pos += req_len;
698*1763da12SFred Isaman 			count -= req_len;
699*1763da12SFred Isaman 		}
700462d5b32SChuck Lever 	} while (count != 0);
701607f31e8STrond Myklebust 
702*1763da12SFred Isaman 	kfree(pagevec);
703*1763da12SFred Isaman 
704607f31e8STrond Myklebust 	if (started)
705c216fd70SChuck Lever 		return started;
706607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
70706cf6f2eSChuck Lever }
70806cf6f2eSChuck Lever 
709*1763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
710*1763da12SFred Isaman {
711*1763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
712*1763da12SFred Isaman 	struct nfs_commit_info cinfo;
713*1763da12SFred Isaman 	int bit = -1;
714*1763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
715*1763da12SFred Isaman 
716*1763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
717*1763da12SFred Isaman 		goto out_put;
718*1763da12SFred Isaman 
719*1763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
720*1763da12SFred Isaman 
721*1763da12SFred Isaman 	spin_lock(&dreq->lock);
722*1763da12SFred Isaman 
723*1763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
724*1763da12SFred Isaman 		dreq->flags = 0;
725*1763da12SFred Isaman 		dreq->error = hdr->error;
726*1763da12SFred Isaman 	}
727*1763da12SFred Isaman 	if (dreq->error != 0)
728*1763da12SFred Isaman 		bit = NFS_IOHDR_ERROR;
729*1763da12SFred Isaman 	else {
730*1763da12SFred Isaman 		dreq->count += hdr->good_bytes;
731*1763da12SFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
732*1763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
733*1763da12SFred Isaman 			bit = NFS_IOHDR_NEED_RESCHED;
734*1763da12SFred Isaman 		} else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
735*1763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
736*1763da12SFred Isaman 				bit = NFS_IOHDR_NEED_RESCHED;
737*1763da12SFred Isaman 			else if (dreq->flags == 0) {
738*1763da12SFred Isaman 				memcpy(&dreq->verf, &req->wb_verf,
739*1763da12SFred Isaman 				       sizeof(dreq->verf));
740*1763da12SFred Isaman 				bit = NFS_IOHDR_NEED_COMMIT;
741*1763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
742*1763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
743*1763da12SFred Isaman 				if (memcmp(&dreq->verf, &req->wb_verf, sizeof(dreq->verf))) {
744*1763da12SFred Isaman 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
745*1763da12SFred Isaman 					bit = NFS_IOHDR_NEED_RESCHED;
746*1763da12SFred Isaman 				} else
747*1763da12SFred Isaman 					bit = NFS_IOHDR_NEED_COMMIT;
748*1763da12SFred Isaman 			}
749*1763da12SFred Isaman 		}
750*1763da12SFred Isaman 	}
751*1763da12SFred Isaman 	spin_unlock(&dreq->lock);
752*1763da12SFred Isaman 
753*1763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
754*1763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
755*1763da12SFred Isaman 		nfs_list_remove_request(req);
756*1763da12SFred Isaman 		switch (bit) {
757*1763da12SFred Isaman 		case NFS_IOHDR_NEED_RESCHED:
758*1763da12SFred Isaman 		case NFS_IOHDR_NEED_COMMIT:
759*1763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
760*1763da12SFred Isaman 			break;
761*1763da12SFred Isaman 		default:
762*1763da12SFred Isaman 			page_cache_release(req->wb_page);
763*1763da12SFred Isaman 			nfs_release_request(req);
764*1763da12SFred Isaman 		}
765*1763da12SFred Isaman 		nfs_unlock_request(req);
766*1763da12SFred Isaman 	}
767*1763da12SFred Isaman 
768*1763da12SFred Isaman out_put:
769*1763da12SFred Isaman 	if (put_dreq(dreq))
770*1763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
771*1763da12SFred Isaman 	hdr->release(hdr);
772*1763da12SFred Isaman }
773*1763da12SFred Isaman 
774*1763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
775*1763da12SFred Isaman 	.error_cleanup = nfs_sync_pgio_error,
776*1763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
777*1763da12SFred Isaman 	.completion = nfs_direct_write_completion,
778*1763da12SFred Isaman };
779*1763da12SFred Isaman 
78019f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
78119f73787SChuck Lever 					       const struct iovec *iov,
78219f73787SChuck Lever 					       unsigned long nr_segs,
783*1763da12SFred Isaman 					       loff_t pos)
78419f73787SChuck Lever {
785*1763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
78619f73787SChuck Lever 	ssize_t result = 0;
78719f73787SChuck Lever 	size_t requested_bytes = 0;
78819f73787SChuck Lever 	unsigned long seg;
78919f73787SChuck Lever 
790*1763da12SFred Isaman 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_COND_STABLE,
791*1763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
792*1763da12SFred Isaman 	desc.pg_dreq = dreq;
79319f73787SChuck Lever 	get_dreq(dreq);
79419f73787SChuck Lever 
79519f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
79619f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
797*1763da12SFred Isaman 		result = nfs_direct_write_schedule_segment(&desc, vec, pos);
79819f73787SChuck Lever 		if (result < 0)
79919f73787SChuck Lever 			break;
80019f73787SChuck Lever 		requested_bytes += result;
80119f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
80219f73787SChuck Lever 			break;
80319f73787SChuck Lever 		pos += vec->iov_len;
80419f73787SChuck Lever 	}
805*1763da12SFred Isaman 	nfs_pageio_complete(&desc);
80619f73787SChuck Lever 
807839f7ad6SChuck Lever 	/*
808839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
809839f7ad6SChuck Lever 	 * generic layer handle the completion.
810839f7ad6SChuck Lever 	 */
811839f7ad6SChuck Lever 	if (requested_bytes == 0) {
812839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
813839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
814839f7ad6SChuck Lever 	}
815839f7ad6SChuck Lever 
81619f73787SChuck Lever 	if (put_dreq(dreq))
81719f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
81819f73787SChuck Lever 	return 0;
81919f73787SChuck Lever }
82019f73787SChuck Lever 
821c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
822c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
823c216fd70SChuck Lever 				size_t count)
824462d5b32SChuck Lever {
825f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
826c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
827462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
828462d5b32SChuck Lever 
829607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
830462d5b32SChuck Lever 	if (!dreq)
831f11ac8dbSTrond Myklebust 		goto out;
832462d5b32SChuck Lever 
833c89f2ee5SChuck Lever 	dreq->inode = inode;
834cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
835f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
836568a810dSSteve Dickson 	if (dreq->l_ctx == NULL)
837f11ac8dbSTrond Myklebust 		goto out_release;
838c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
839c89f2ee5SChuck Lever 		dreq->iocb = iocb;
840462d5b32SChuck Lever 
841*1763da12SFred Isaman 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos);
842607f31e8STrond Myklebust 	if (!result)
843c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
844f11ac8dbSTrond Myklebust out_release:
845b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
846f11ac8dbSTrond Myklebust out:
8471da177e4SLinus Torvalds 	return result;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds /**
8511da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8521da177e4SLinus Torvalds  * @iocb: target I/O control block
853027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
854027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
85588467055SChuck Lever  * @pos: byte offset in file where reading starts
8561da177e4SLinus Torvalds  *
8571da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
8581da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
8591da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
8601da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
8611da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
86288467055SChuck Lever  * READ where the file size could change.  Our preference is simply
8631da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
8641da177e4SLinus Torvalds  * care of managing the end of file boundary.
8651da177e4SLinus Torvalds  *
8661da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
8671da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
8681da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8691da177e4SLinus Torvalds  * cache.
8701da177e4SLinus Torvalds  */
871027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
872027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
8751da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8761da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
877c216fd70SChuck Lever 	size_t count;
8781da177e4SLinus Torvalds 
879c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
880c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
881c216fd70SChuck Lever 
8826da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
88301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
88401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
885c216fd70SChuck Lever 		count, (long long) pos);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	retval = 0;
8881da177e4SLinus Torvalds 	if (!count)
8891da177e4SLinus Torvalds 		goto out;
8901da177e4SLinus Torvalds 
89129884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8921da177e4SLinus Torvalds 	if (retval)
8931da177e4SLinus Torvalds 		goto out;
8941da177e4SLinus Torvalds 
8957ec10f26SKonstantin Khlebnikov 	task_io_account_read(count);
8967ec10f26SKonstantin Khlebnikov 
897c216fd70SChuck Lever 	retval = nfs_direct_read(iocb, iov, nr_segs, pos);
8981da177e4SLinus Torvalds 	if (retval > 0)
8990cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds out:
9021da177e4SLinus Torvalds 	return retval;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds /**
9061da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9071da177e4SLinus Torvalds  * @iocb: target I/O control block
908027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
909027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
91088467055SChuck Lever  * @pos: byte offset in file where writing starts
9111da177e4SLinus Torvalds  *
9121da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9131da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9141da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9151da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9161da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9171da177e4SLinus Torvalds  * parameter checking and report problems.
9181da177e4SLinus Torvalds  *
9191da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9221da177e4SLinus Torvalds  * readers of this file.
9231da177e4SLinus Torvalds  *
9241da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9251da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9261da177e4SLinus Torvalds  */
927027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
928027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9291da177e4SLinus Torvalds {
930070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9311da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9321da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
933c216fd70SChuck Lever 	size_t count;
9341da177e4SLinus Torvalds 
935c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
936c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
937c216fd70SChuck Lever 
9386da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
93901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
94001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
941c216fd70SChuck Lever 		count, (long long) pos);
942027445c3SBadari Pulavarty 
943ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
944ce1a8e67SChuck Lever 	if (retval)
9451da177e4SLinus Torvalds 		goto out;
946ce1a8e67SChuck Lever 
947ce1a8e67SChuck Lever 	retval = -EINVAL;
948ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9491da177e4SLinus Torvalds 		goto out;
9501da177e4SLinus Torvalds 	retval = 0;
9511da177e4SLinus Torvalds 	if (!count)
9521da177e4SLinus Torvalds 		goto out;
953ce1a8e67SChuck Lever 
95429884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9551da177e4SLinus Torvalds 	if (retval)
9561da177e4SLinus Torvalds 		goto out;
9571da177e4SLinus Torvalds 
9587ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
9597ec10f26SKonstantin Khlebnikov 
960c216fd70SChuck Lever 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
961*1763da12SFred Isaman 	if (retval > 0) {
962*1763da12SFred Isaman 		struct inode *inode = mapping->host;
9639eafa8ccSChuck Lever 
964ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
965*1763da12SFred Isaman 		spin_lock(&inode->i_lock);
966*1763da12SFred Isaman 		if (i_size_read(inode) < iocb->ki_pos)
967*1763da12SFred Isaman 			i_size_write(inode, iocb->ki_pos);
968*1763da12SFred Isaman 		spin_unlock(&inode->i_lock);
969*1763da12SFred Isaman 	}
9701da177e4SLinus Torvalds out:
9711da177e4SLinus Torvalds 	return retval;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
97488467055SChuck Lever /**
97588467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
97688467055SChuck Lever  *
97788467055SChuck Lever  */
978f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9811da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
982fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
983fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
98420c2df83SPaul Mundt 						NULL);
9851da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9861da177e4SLinus Torvalds 		return -ENOMEM;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	return 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
99188467055SChuck Lever /**
992f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
99388467055SChuck Lever  *
99488467055SChuck Lever  */
995266bee88SDavid Brownell void nfs_destroy_directcache(void)
9961da177e4SLinus Torvalds {
9971a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
9981da177e4SLinus Torvalds }
999