xref: /openbmc/linux/fs/nfs/direct.c (revision 1d59d61f606547f0712aa6971f91f71154071c99)
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"
591763da12SFred 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 */
851763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
861763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
871763da12SFred 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 
941763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
951763da12SFred 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);
971763da12SFred 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 
1381763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
1391763da12SFred Isaman 			      struct nfs_direct_req *dreq)
1401763da12SFred Isaman {
1411763da12SFred Isaman 	cinfo->lock = &dreq->lock;
1421763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
1431763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
1441763da12SFred Isaman 	cinfo->dreq = dreq;
1451763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
1461763da12SFred Isaman }
1471763da12SFred 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 
152292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(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);
1591763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
1601763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
16115ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
16293619e59SChuck Lever 
16393619e59SChuck Lever 	return dreq;
16493619e59SChuck Lever }
16593619e59SChuck Lever 
166b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
169a8881f5aSTrond Myklebust 
170f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
171f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
172a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
173a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
1741da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
178b4946ffbSTrond Myklebust {
179b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
180b4946ffbSTrond Myklebust }
181b4946ffbSTrond Myklebust 
182d4cc948bSChuck Lever /*
183bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
184bc0fb201SChuck Lever  */
185bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
186bc0fb201SChuck Lever {
18715ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
188bc0fb201SChuck Lever 
189bc0fb201SChuck Lever 	/* Async requests don't wait here */
190bc0fb201SChuck Lever 	if (dreq->iocb)
191bc0fb201SChuck Lever 		goto out;
192bc0fb201SChuck Lever 
193150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
194bc0fb201SChuck Lever 
195bc0fb201SChuck Lever 	if (!result)
19615ce4a0cSChuck Lever 		result = dreq->error;
197bc0fb201SChuck Lever 	if (!result)
19815ce4a0cSChuck Lever 		result = dreq->count;
199bc0fb201SChuck Lever 
200bc0fb201SChuck Lever out:
201bc0fb201SChuck Lever 	return (ssize_t) result;
202bc0fb201SChuck Lever }
203bc0fb201SChuck Lever 
204bc0fb201SChuck Lever /*
205607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
206607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
20763ab46abSChuck Lever  */
20863ab46abSChuck Lever static void nfs_direct_complete(struct nfs_direct_req *dreq)
20963ab46abSChuck Lever {
21063ab46abSChuck Lever 	if (dreq->iocb) {
21115ce4a0cSChuck Lever 		long res = (long) dreq->error;
21263ab46abSChuck Lever 		if (!res)
21315ce4a0cSChuck Lever 			res = (long) dreq->count;
21463ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
215d72b7a6bSTrond Myklebust 	}
216d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
21763ab46abSChuck Lever 
218b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
21963ab46abSChuck Lever }
22063ab46abSChuck Lever 
2211385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
2221da177e4SLinus Torvalds {
223584aa810SFred Isaman 	dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
224584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
225584aa810SFred Isaman 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
226584aa810SFred Isaman 		req->wb_bytes,
227584aa810SFred Isaman 		(long long)req_offset(req));
228584aa810SFred Isaman 	nfs_release_request(req);
229fdd1e74cSTrond Myklebust }
230fdd1e74cSTrond Myklebust 
231584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
232fdd1e74cSTrond Myklebust {
233584aa810SFred Isaman 	unsigned long bytes = 0;
234584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
235fdd1e74cSTrond Myklebust 
236584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
237584aa810SFred Isaman 		goto out_put;
2381da177e4SLinus Torvalds 
23915ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
240584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
241584aa810SFred Isaman 		dreq->error = hdr->error;
242584aa810SFred Isaman 	else
243584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
24415ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
2451da177e4SLinus Torvalds 
246584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
247584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
248584aa810SFred Isaman 		struct page *page = req->wb_page;
249584aa810SFred Isaman 
250584aa810SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
251584aa810SFred Isaman 			if (bytes > hdr->good_bytes)
252584aa810SFred Isaman 				zero_user(page, 0, PAGE_SIZE);
253584aa810SFred Isaman 			else if (hdr->good_bytes - bytes < PAGE_SIZE)
254584aa810SFred Isaman 				zero_user_segment(page,
255584aa810SFred Isaman 					hdr->good_bytes & ~PAGE_MASK,
256584aa810SFred Isaman 					PAGE_SIZE);
257584aa810SFred Isaman 		}
2584bd8b010STrond Myklebust 		if (!PageCompound(page)) {
2594bd8b010STrond Myklebust 			if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
260584aa810SFred Isaman 				if (bytes < hdr->good_bytes)
2614bd8b010STrond Myklebust 					set_page_dirty(page);
2624bd8b010STrond Myklebust 			} else
2634bd8b010STrond Myklebust 				set_page_dirty(page);
2644bd8b010STrond Myklebust 		}
265584aa810SFred Isaman 		bytes += req->wb_bytes;
266584aa810SFred Isaman 		nfs_list_remove_request(req);
267584aa810SFred Isaman 		nfs_direct_readpage_release(req);
268584aa810SFred Isaman 	}
269584aa810SFred Isaman out_put:
270607f31e8STrond Myklebust 	if (put_dreq(dreq))
27163ab46abSChuck Lever 		nfs_direct_complete(dreq);
272584aa810SFred Isaman 	hdr->release(hdr);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2753e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
276cd841605SFred Isaman {
277584aa810SFred Isaman 	struct nfs_page *req;
278cd841605SFred Isaman 
279584aa810SFred Isaman 	while (!list_empty(head)) {
280584aa810SFred Isaman 		req = nfs_list_entry(head->next);
281584aa810SFred Isaman 		nfs_list_remove_request(req);
282584aa810SFred Isaman 		nfs_release_request(req);
283cd841605SFred Isaman 	}
284584aa810SFred Isaman }
285584aa810SFred Isaman 
286584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
287584aa810SFred Isaman {
288584aa810SFred Isaman 	get_dreq(hdr->dreq);
289584aa810SFred Isaman }
290584aa810SFred Isaman 
291584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
2923e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
293584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
294584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
295584aa810SFred Isaman };
296cd841605SFred Isaman 
297d4cc948bSChuck Lever /*
298607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
299607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
300607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
301607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
302607f31e8STrond Myklebust  * no requests have been sent, just return an error.
3031da177e4SLinus Torvalds  */
304584aa810SFred Isaman static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
30502fe4946SChuck Lever 						const struct iovec *iov,
30602fe4946SChuck Lever 						loff_t pos)
3071da177e4SLinus Torvalds {
308584aa810SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
309a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
3103d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
31102fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
31202fe4946SChuck Lever 	size_t count = iov->iov_len;
3135dd602f2SChuck Lever 	size_t rsize = NFS_SERVER(inode)->rsize;
314607f31e8STrond Myklebust 	unsigned int pgbase;
315607f31e8STrond Myklebust 	int result;
316607f31e8STrond Myklebust 	ssize_t started = 0;
317584aa810SFred Isaman 	struct page **pagevec = NULL;
318584aa810SFred Isaman 	unsigned int npages;
31982b145c5SChuck Lever 
3201da177e4SLinus Torvalds 	do {
3215dd602f2SChuck Lever 		size_t bytes;
322584aa810SFred Isaman 		int i;
3231da177e4SLinus Torvalds 
324e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
325bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
326e9f7bee1STrond Myklebust 
327607f31e8STrond Myklebust 		result = -ENOMEM;
328584aa810SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
329584aa810SFred Isaman 		if (!pagevec)
330584aa810SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *),
331584aa810SFred Isaman 					  GFP_KERNEL);
332584aa810SFred Isaman 		if (!pagevec)
333607f31e8STrond Myklebust 			break;
334607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
335607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
336584aa810SFred Isaman 					npages, 1, 0, pagevec, NULL);
337607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
338584aa810SFred Isaman 		if (result < 0)
339749e146eSChuck Lever 			break;
340584aa810SFred Isaman 		if ((unsigned)result < npages) {
341d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
342d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
343584aa810SFred Isaman 				nfs_direct_release_pages(pagevec, result);
344607f31e8STrond Myklebust 				break;
345607f31e8STrond Myklebust 			}
346d9df8d6bSTrond Myklebust 			bytes -= pgbase;
347584aa810SFred Isaman 			npages = result;
348d9df8d6bSTrond Myklebust 		}
34906cf6f2eSChuck Lever 
350584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
351584aa810SFred Isaman 			struct nfs_page *req;
352bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
353584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
354584aa810SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
355584aa810SFred Isaman 						 pagevec[i],
356584aa810SFred Isaman 						 pgbase, req_len);
357584aa810SFred Isaman 			if (IS_ERR(req)) {
358584aa810SFred Isaman 				result = PTR_ERR(req);
359dbae4c73STrond Myklebust 				break;
360584aa810SFred Isaman 			}
361584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
362584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
363584aa810SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
364584aa810SFred Isaman 				result = desc->pg_error;
365584aa810SFred Isaman 				nfs_release_request(req);
366584aa810SFred Isaman 				break;
367584aa810SFred Isaman 			}
368584aa810SFred Isaman 			pgbase = 0;
369584aa810SFred Isaman 			bytes -= req_len;
370584aa810SFred Isaman 			started += req_len;
371584aa810SFred Isaman 			user_addr += req_len;
372584aa810SFred Isaman 			pos += req_len;
373584aa810SFred Isaman 			count -= req_len;
374584aa810SFred Isaman 		}
3756d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
3766d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
37771e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
378607f31e8STrond Myklebust 
379584aa810SFred Isaman 	kfree(pagevec);
380584aa810SFred Isaman 
381607f31e8STrond Myklebust 	if (started)
382c216fd70SChuck Lever 		return started;
383607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
38406cf6f2eSChuck Lever }
38506cf6f2eSChuck Lever 
38619f73787SChuck Lever static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
38719f73787SChuck Lever 					      const struct iovec *iov,
38819f73787SChuck Lever 					      unsigned long nr_segs,
38919f73787SChuck Lever 					      loff_t pos)
39019f73787SChuck Lever {
391584aa810SFred Isaman 	struct nfs_pageio_descriptor desc;
39219f73787SChuck Lever 	ssize_t result = -EINVAL;
39319f73787SChuck Lever 	size_t requested_bytes = 0;
39419f73787SChuck Lever 	unsigned long seg;
39519f73787SChuck Lever 
396584aa810SFred Isaman 	nfs_pageio_init_read(&desc, dreq->inode,
397584aa810SFred Isaman 			     &nfs_direct_read_completion_ops);
39819f73787SChuck Lever 	get_dreq(dreq);
399584aa810SFred Isaman 	desc.pg_dreq = dreq;
40019f73787SChuck Lever 
40119f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
40219f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
403584aa810SFred Isaman 		result = nfs_direct_read_schedule_segment(&desc, vec, pos);
40419f73787SChuck Lever 		if (result < 0)
40519f73787SChuck Lever 			break;
40619f73787SChuck Lever 		requested_bytes += result;
40719f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
40819f73787SChuck Lever 			break;
40919f73787SChuck Lever 		pos += vec->iov_len;
41019f73787SChuck Lever 	}
41119f73787SChuck Lever 
412584aa810SFred Isaman 	nfs_pageio_complete(&desc);
413584aa810SFred Isaman 
414839f7ad6SChuck Lever 	/*
415839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
416839f7ad6SChuck Lever 	 * generic layer handle the completion.
417839f7ad6SChuck Lever 	 */
418839f7ad6SChuck Lever 	if (requested_bytes == 0) {
419839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
420839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
421839f7ad6SChuck Lever 	}
422839f7ad6SChuck Lever 
42319f73787SChuck Lever 	if (put_dreq(dreq))
42419f73787SChuck Lever 		nfs_direct_complete(dreq);
42519f73787SChuck Lever 	return 0;
42619f73787SChuck Lever }
42719f73787SChuck Lever 
428c216fd70SChuck Lever static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
429c216fd70SChuck Lever 			       unsigned long nr_segs, loff_t pos)
4301da177e4SLinus Torvalds {
431f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
43299514f8fSChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
4331da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
4341da177e4SLinus Torvalds 
435607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
436f11ac8dbSTrond Myklebust 	if (dreq == NULL)
437f11ac8dbSTrond Myklebust 		goto out;
4381da177e4SLinus Torvalds 
43991d5b470SChuck Lever 	dreq->inode = inode;
440cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
441f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
442f11ac8dbSTrond Myklebust 	if (dreq->l_ctx == NULL)
443f11ac8dbSTrond Myklebust 		goto out_release;
444487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
445487b8372SChuck Lever 		dreq->iocb = iocb;
4461da177e4SLinus Torvalds 
447c216fd70SChuck Lever 	result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
448607f31e8STrond Myklebust 	if (!result)
449bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
4502701d086SAndy Adamson 	NFS_I(inode)->read_io += result;
451f11ac8dbSTrond Myklebust out_release:
452b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
453f11ac8dbSTrond Myklebust out:
4541da177e4SLinus Torvalds 	return result;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
457*1d59d61fSTrond Myklebust static void nfs_inode_dio_write_done(struct inode *inode)
458*1d59d61fSTrond Myklebust {
459*1d59d61fSTrond Myklebust 	nfs_zap_mapping(inode, inode->i_mapping);
460*1d59d61fSTrond Myklebust 	inode_dio_done(inode);
461*1d59d61fSTrond Myklebust }
462*1d59d61fSTrond Myklebust 
463fad61490STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
464fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
4651da177e4SLinus Torvalds {
4661763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
4671763da12SFred Isaman 	struct nfs_page *req, *tmp;
4681763da12SFred Isaman 	LIST_HEAD(reqs);
4691763da12SFred Isaman 	struct nfs_commit_info cinfo;
4701763da12SFred Isaman 	LIST_HEAD(failed);
4711763da12SFred Isaman 
4721763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
4731763da12SFred Isaman 	pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
4741763da12SFred Isaman 	spin_lock(cinfo.lock);
4751763da12SFred Isaman 	nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
4761763da12SFred Isaman 	spin_unlock(cinfo.lock);
4771da177e4SLinus Torvalds 
478fad61490STrond Myklebust 	dreq->count = 0;
479607f31e8STrond Myklebust 	get_dreq(dreq);
4801da177e4SLinus Torvalds 
4811763da12SFred Isaman 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE,
4821763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
4831763da12SFred Isaman 	desc.pg_dreq = dreq;
484607f31e8STrond Myklebust 
4851763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
4861763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
4871763da12SFred Isaman 			nfs_list_add_request(req, &failed);
4881763da12SFred Isaman 			spin_lock(cinfo.lock);
4891763da12SFred Isaman 			dreq->flags = 0;
4901763da12SFred Isaman 			dreq->error = -EIO;
4911763da12SFred Isaman 			spin_unlock(cinfo.lock);
4921763da12SFred Isaman 		}
4931763da12SFred Isaman 	}
4941763da12SFred Isaman 	nfs_pageio_complete(&desc);
495607f31e8STrond Myklebust 
49604277086STrond Myklebust 	while (!list_empty(&failed))
4971d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
498607f31e8STrond Myklebust 
499607f31e8STrond Myklebust 	if (put_dreq(dreq))
5001763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
501fad61490STrond Myklebust }
5021da177e4SLinus Torvalds 
5031763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
504fad61490STrond Myklebust {
5050b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
5061763da12SFred Isaman 	struct nfs_commit_info cinfo;
5071763da12SFred Isaman 	struct nfs_page *req;
508c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
509c9d8f89dSTrond Myklebust 
5101763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
511c9d8f89dSTrond Myklebust 	if (status < 0) {
51260fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
513c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
514fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
51560fa3f76STrond Myklebust 	} else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
516c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
517fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
518fad61490STrond Myklebust 	}
519fad61490STrond Myklebust 
520c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
5211763da12SFred Isaman 	while (!list_empty(&data->pages)) {
5221763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
5231763da12SFred Isaman 		nfs_list_remove_request(req);
5241763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
5251763da12SFred Isaman 			/* Note the rewrite will go through mds */
52604277086STrond Myklebust 			kref_get(&req->wb_kref);
5271763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
52804277086STrond Myklebust 		}
5291d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
530fad61490STrond Myklebust 	}
531fad61490STrond Myklebust 
5321763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
5331763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
5341763da12SFred Isaman }
5351763da12SFred Isaman 
5361763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
5371763da12SFred Isaman {
5381763da12SFred Isaman 	/* There is no lock to clear */
5391763da12SFred Isaman }
5401763da12SFred Isaman 
5411763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
5421763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
5431763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
544fad61490STrond Myklebust };
545fad61490STrond Myklebust 
546fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
547fad61490STrond Myklebust {
5481763da12SFred Isaman 	int res;
5491763da12SFred Isaman 	struct nfs_commit_info cinfo;
5501763da12SFred Isaman 	LIST_HEAD(mds_list);
551fad61490STrond Myklebust 
5521763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
5531763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
5541763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
5551763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
5561763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
5601da177e4SLinus Torvalds {
5611763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
562fad61490STrond Myklebust 	int flags = dreq->flags;
5631da177e4SLinus Torvalds 
564fad61490STrond Myklebust 	dreq->flags = 0;
565fad61490STrond Myklebust 	switch (flags) {
566fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
567fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
5681da177e4SLinus Torvalds 			break;
569fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
570fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
5711da177e4SLinus Torvalds 			break;
5721da177e4SLinus Torvalds 		default:
573*1d59d61fSTrond Myklebust 			nfs_inode_dio_write_done(dreq->inode);
574fad61490STrond Myklebust 			nfs_direct_complete(dreq);
5751da177e4SLinus Torvalds 	}
576fad61490STrond Myklebust }
577fad61490STrond Myklebust 
5781763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
579fad61490STrond Myklebust {
5801763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
581fad61490STrond Myklebust }
5821763da12SFred Isaman 
583fad61490STrond Myklebust #else
58424fc9211SBryan Schumaker static void nfs_direct_write_schedule_work(struct work_struct *work)
58524fc9211SBryan Schumaker {
58624fc9211SBryan Schumaker }
587fad61490STrond Myklebust 
588fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
589fad61490STrond Myklebust {
590*1d59d61fSTrond Myklebust 	nfs_inode_dio_write_done(inode);
591fad61490STrond Myklebust 	nfs_direct_complete(dreq);
592fad61490STrond Myklebust }
593fad61490STrond Myklebust #endif
594fad61490STrond Myklebust 
595c9d8f89dSTrond Myklebust /*
596c9d8f89dSTrond Myklebust  * NB: Return the value of the first error return code.  Subsequent
597c9d8f89dSTrond Myklebust  *     errors after the first one are ignored.
598c9d8f89dSTrond Myklebust  */
599462d5b32SChuck Lever /*
600607f31e8STrond Myklebust  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
601607f31e8STrond Myklebust  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
602607f31e8STrond Myklebust  * bail and stop sending more writes.  Write length accounting is
603607f31e8STrond Myklebust  * handled automatically by nfs_direct_write_result().  Otherwise, if
604607f31e8STrond Myklebust  * no requests have been sent, just return an error.
605462d5b32SChuck Lever  */
6061763da12SFred Isaman static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
60702fe4946SChuck Lever 						 const struct iovec *iov,
6081763da12SFred Isaman 						 loff_t pos)
609462d5b32SChuck Lever {
6101763da12SFred Isaman 	struct nfs_direct_req *dreq = desc->pg_dreq;
611a8881f5aSTrond Myklebust 	struct nfs_open_context *ctx = dreq->ctx;
6123d4ff43dSAl Viro 	struct inode *inode = ctx->dentry->d_inode;
61302fe4946SChuck Lever 	unsigned long user_addr = (unsigned long)iov->iov_base;
61402fe4946SChuck Lever 	size_t count = iov->iov_len;
615462d5b32SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
616607f31e8STrond Myklebust 	unsigned int pgbase;
617607f31e8STrond Myklebust 	int result;
618607f31e8STrond Myklebust 	ssize_t started = 0;
6191763da12SFred Isaman 	struct page **pagevec = NULL;
6201763da12SFred Isaman 	unsigned int npages;
62182b145c5SChuck Lever 
622462d5b32SChuck Lever 	do {
623462d5b32SChuck Lever 		size_t bytes;
6241763da12SFred Isaman 		int i;
625462d5b32SChuck Lever 
626e9f7bee1STrond Myklebust 		pgbase = user_addr & ~PAGE_MASK;
627bf5fc402STrond Myklebust 		bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
628e9f7bee1STrond Myklebust 
629607f31e8STrond Myklebust 		result = -ENOMEM;
6301763da12SFred Isaman 		npages = nfs_page_array_len(pgbase, bytes);
6311763da12SFred Isaman 		if (!pagevec)
6321763da12SFred Isaman 			pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
6331763da12SFred Isaman 		if (!pagevec)
634607f31e8STrond Myklebust 			break;
635607f31e8STrond Myklebust 
636607f31e8STrond Myklebust 		down_read(&current->mm->mmap_sem);
637607f31e8STrond Myklebust 		result = get_user_pages(current, current->mm, user_addr,
6381763da12SFred Isaman 					npages, 0, 0, pagevec, NULL);
639607f31e8STrond Myklebust 		up_read(&current->mm->mmap_sem);
6401763da12SFred Isaman 		if (result < 0)
641749e146eSChuck Lever 			break;
6421763da12SFred Isaman 
6431763da12SFred Isaman 		if ((unsigned)result < npages) {
644d9df8d6bSTrond Myklebust 			bytes = result * PAGE_SIZE;
645d9df8d6bSTrond Myklebust 			if (bytes <= pgbase) {
6461763da12SFred Isaman 				nfs_direct_release_pages(pagevec, result);
647607f31e8STrond Myklebust 				break;
648607f31e8STrond Myklebust 			}
649d9df8d6bSTrond Myklebust 			bytes -= pgbase;
6501763da12SFred Isaman 			npages = result;
651d9df8d6bSTrond Myklebust 		}
652607f31e8STrond Myklebust 
6531763da12SFred Isaman 		for (i = 0; i < npages; i++) {
6541763da12SFred Isaman 			struct nfs_page *req;
655bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
656607f31e8STrond Myklebust 
6571763da12SFred Isaman 			req = nfs_create_request(dreq->ctx, dreq->inode,
6581763da12SFred Isaman 						 pagevec[i],
6591763da12SFred Isaman 						 pgbase, req_len);
6601763da12SFred Isaman 			if (IS_ERR(req)) {
6611763da12SFred Isaman 				result = PTR_ERR(req);
662dbae4c73STrond Myklebust 				break;
6631763da12SFred Isaman 			}
6641763da12SFred Isaman 			nfs_lock_request(req);
6651763da12SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
6661763da12SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
6671763da12SFred Isaman 			if (!nfs_pageio_add_request(desc, req)) {
6681763da12SFred Isaman 				result = desc->pg_error;
6691d1afcbcSTrond Myklebust 				nfs_unlock_and_release_request(req);
67071e8cc00STrond Myklebust 				break;
6711763da12SFred Isaman 			}
6721763da12SFred Isaman 			pgbase = 0;
6731763da12SFred Isaman 			bytes -= req_len;
6741763da12SFred Isaman 			started += req_len;
6751763da12SFred Isaman 			user_addr += req_len;
6761763da12SFred Isaman 			pos += req_len;
6771763da12SFred Isaman 			count -= req_len;
6781763da12SFred Isaman 		}
6796d74743bSTrond Myklebust 		/* The nfs_page now hold references to these pages */
6806d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
68171e8cc00STrond Myklebust 	} while (count != 0 && result >= 0);
682607f31e8STrond Myklebust 
6831763da12SFred Isaman 	kfree(pagevec);
6841763da12SFred Isaman 
685607f31e8STrond Myklebust 	if (started)
686c216fd70SChuck Lever 		return started;
687607f31e8STrond Myklebust 	return result < 0 ? (ssize_t) result : -EFAULT;
68806cf6f2eSChuck Lever }
68906cf6f2eSChuck Lever 
6901763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
6911763da12SFred Isaman {
6921763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
6931763da12SFred Isaman 	struct nfs_commit_info cinfo;
6941763da12SFred Isaman 	int bit = -1;
6951763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6961763da12SFred Isaman 
6971763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6981763da12SFred Isaman 		goto out_put;
6991763da12SFred Isaman 
7001763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7011763da12SFred Isaman 
7021763da12SFred Isaman 	spin_lock(&dreq->lock);
7031763da12SFred Isaman 
7041763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7051763da12SFred Isaman 		dreq->flags = 0;
7061763da12SFred Isaman 		dreq->error = hdr->error;
7071763da12SFred Isaman 	}
7081763da12SFred Isaman 	if (dreq->error != 0)
7091763da12SFred Isaman 		bit = NFS_IOHDR_ERROR;
7101763da12SFred Isaman 	else {
7111763da12SFred Isaman 		dreq->count += hdr->good_bytes;
7121763da12SFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
7131763da12SFred Isaman 			dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7141763da12SFred Isaman 			bit = NFS_IOHDR_NEED_RESCHED;
7151763da12SFred Isaman 		} else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
7161763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
7171763da12SFred Isaman 				bit = NFS_IOHDR_NEED_RESCHED;
7181763da12SFred Isaman 			else if (dreq->flags == 0) {
7191763da12SFred Isaman 				memcpy(&dreq->verf, &req->wb_verf,
7201763da12SFred Isaman 				       sizeof(dreq->verf));
7211763da12SFred Isaman 				bit = NFS_IOHDR_NEED_COMMIT;
7221763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7231763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
7241763da12SFred Isaman 				if (memcmp(&dreq->verf, &req->wb_verf, sizeof(dreq->verf))) {
7251763da12SFred Isaman 					dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7261763da12SFred Isaman 					bit = NFS_IOHDR_NEED_RESCHED;
7271763da12SFred Isaman 				} else
7281763da12SFred Isaman 					bit = NFS_IOHDR_NEED_COMMIT;
7291763da12SFred Isaman 			}
7301763da12SFred Isaman 		}
7311763da12SFred Isaman 	}
7321763da12SFred Isaman 	spin_unlock(&dreq->lock);
7331763da12SFred Isaman 
7341763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7351763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7361763da12SFred Isaman 		nfs_list_remove_request(req);
7371763da12SFred Isaman 		switch (bit) {
7381763da12SFred Isaman 		case NFS_IOHDR_NEED_RESCHED:
7391763da12SFred Isaman 		case NFS_IOHDR_NEED_COMMIT:
74004277086STrond Myklebust 			kref_get(&req->wb_kref);
7411763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
7421763da12SFred Isaman 		}
7431d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7441763da12SFred Isaman 	}
7451763da12SFred Isaman 
7461763da12SFred Isaman out_put:
7471763da12SFred Isaman 	if (put_dreq(dreq))
7481763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
7491763da12SFred Isaman 	hdr->release(hdr);
7501763da12SFred Isaman }
7511763da12SFred Isaman 
7523e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
7533e9e0ca3STrond Myklebust {
7543e9e0ca3STrond Myklebust 	struct nfs_page *req;
7553e9e0ca3STrond Myklebust 
7563e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
7573e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
7583e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
7591d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7603e9e0ca3STrond Myklebust 	}
7613e9e0ca3STrond Myklebust }
7623e9e0ca3STrond Myklebust 
7631763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
7643e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
7651763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
7661763da12SFred Isaman 	.completion = nfs_direct_write_completion,
7671763da12SFred Isaman };
7681763da12SFred Isaman 
76919f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
77019f73787SChuck Lever 					       const struct iovec *iov,
77119f73787SChuck Lever 					       unsigned long nr_segs,
7721763da12SFred Isaman 					       loff_t pos)
77319f73787SChuck Lever {
7741763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
775*1d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
77619f73787SChuck Lever 	ssize_t result = 0;
77719f73787SChuck Lever 	size_t requested_bytes = 0;
77819f73787SChuck Lever 	unsigned long seg;
77919f73787SChuck Lever 
780*1d59d61fSTrond Myklebust 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE,
7811763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
7821763da12SFred Isaman 	desc.pg_dreq = dreq;
78319f73787SChuck Lever 	get_dreq(dreq);
784*1d59d61fSTrond Myklebust 	atomic_inc(&inode->i_dio_count);
78519f73787SChuck Lever 
78619f73787SChuck Lever 	for (seg = 0; seg < nr_segs; seg++) {
78719f73787SChuck Lever 		const struct iovec *vec = &iov[seg];
7881763da12SFred Isaman 		result = nfs_direct_write_schedule_segment(&desc, vec, pos);
78919f73787SChuck Lever 		if (result < 0)
79019f73787SChuck Lever 			break;
79119f73787SChuck Lever 		requested_bytes += result;
79219f73787SChuck Lever 		if ((size_t)result < vec->iov_len)
79319f73787SChuck Lever 			break;
79419f73787SChuck Lever 		pos += vec->iov_len;
79519f73787SChuck Lever 	}
7961763da12SFred Isaman 	nfs_pageio_complete(&desc);
7972701d086SAndy Adamson 	NFS_I(dreq->inode)->write_io += desc.pg_bytes_written;
79819f73787SChuck Lever 
799839f7ad6SChuck Lever 	/*
800839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
801839f7ad6SChuck Lever 	 * generic layer handle the completion.
802839f7ad6SChuck Lever 	 */
803839f7ad6SChuck Lever 	if (requested_bytes == 0) {
804*1d59d61fSTrond Myklebust 		inode_dio_done(inode);
805839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
806839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
807839f7ad6SChuck Lever 	}
808839f7ad6SChuck Lever 
80919f73787SChuck Lever 	if (put_dreq(dreq))
81019f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
81119f73787SChuck Lever 	return 0;
81219f73787SChuck Lever }
81319f73787SChuck Lever 
814c216fd70SChuck Lever static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
815c216fd70SChuck Lever 				unsigned long nr_segs, loff_t pos,
816c216fd70SChuck Lever 				size_t count)
817462d5b32SChuck Lever {
818f11ac8dbSTrond Myklebust 	ssize_t result = -ENOMEM;
819c89f2ee5SChuck Lever 	struct inode *inode = iocb->ki_filp->f_mapping->host;
820462d5b32SChuck Lever 	struct nfs_direct_req *dreq;
821462d5b32SChuck Lever 
822607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
823462d5b32SChuck Lever 	if (!dreq)
824f11ac8dbSTrond Myklebust 		goto out;
825462d5b32SChuck Lever 
826c89f2ee5SChuck Lever 	dreq->inode = inode;
827cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
828f11ac8dbSTrond Myklebust 	dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
829568a810dSSteve Dickson 	if (dreq->l_ctx == NULL)
830f11ac8dbSTrond Myklebust 		goto out_release;
831c89f2ee5SChuck Lever 	if (!is_sync_kiocb(iocb))
832c89f2ee5SChuck Lever 		dreq->iocb = iocb;
833462d5b32SChuck Lever 
8341763da12SFred Isaman 	result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos);
835607f31e8STrond Myklebust 	if (!result)
836c89f2ee5SChuck Lever 		result = nfs_direct_wait(dreq);
837f11ac8dbSTrond Myklebust out_release:
838b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
839f11ac8dbSTrond Myklebust out:
8401da177e4SLinus Torvalds 	return result;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /**
8441da177e4SLinus Torvalds  * nfs_file_direct_read - file direct read operation for NFS files
8451da177e4SLinus Torvalds  * @iocb: target I/O control block
846027445c3SBadari Pulavarty  * @iov: vector of user buffers into which to read data
847027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
84888467055SChuck Lever  * @pos: byte offset in file where reading starts
8491da177e4SLinus Torvalds  *
8501da177e4SLinus Torvalds  * We use this function for direct reads instead of calling
8511da177e4SLinus Torvalds  * generic_file_aio_read() in order to avoid gfar's check to see if
8521da177e4SLinus Torvalds  * the request starts before the end of the file.  For that check
8531da177e4SLinus Torvalds  * to work, we must generate a GETATTR before each direct read, and
8541da177e4SLinus Torvalds  * even then there is a window between the GETATTR and the subsequent
85588467055SChuck Lever  * READ where the file size could change.  Our preference is simply
8561da177e4SLinus Torvalds  * to do all reads the application wants, and the server will take
8571da177e4SLinus Torvalds  * care of managing the end of file boundary.
8581da177e4SLinus Torvalds  *
8591da177e4SLinus Torvalds  * This function also eliminates unnecessarily updating the file's
8601da177e4SLinus Torvalds  * atime locally, as the NFS server sets the file's atime, and this
8611da177e4SLinus Torvalds  * client must read the updated atime from the server back into its
8621da177e4SLinus Torvalds  * cache.
8631da177e4SLinus Torvalds  */
864027445c3SBadari Pulavarty ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
865027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	ssize_t retval = -EINVAL;
8681da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8691da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
870c216fd70SChuck Lever 	size_t count;
8711da177e4SLinus Torvalds 
872c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
873c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
874c216fd70SChuck Lever 
8756da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
87601cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
87701cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
878c216fd70SChuck Lever 		count, (long long) pos);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	retval = 0;
8811da177e4SLinus Torvalds 	if (!count)
8821da177e4SLinus Torvalds 		goto out;
8831da177e4SLinus Torvalds 
88429884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
8851da177e4SLinus Torvalds 	if (retval)
8861da177e4SLinus Torvalds 		goto out;
8871da177e4SLinus Torvalds 
8887ec10f26SKonstantin Khlebnikov 	task_io_account_read(count);
8897ec10f26SKonstantin Khlebnikov 
890c216fd70SChuck Lever 	retval = nfs_direct_read(iocb, iov, nr_segs, pos);
8911da177e4SLinus Torvalds 	if (retval > 0)
8920cdd80d0SChuck Lever 		iocb->ki_pos = pos + retval;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds out:
8951da177e4SLinus Torvalds 	return retval;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds /**
8991da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9001da177e4SLinus Torvalds  * @iocb: target I/O control block
901027445c3SBadari Pulavarty  * @iov: vector of user buffers from which to write data
902027445c3SBadari Pulavarty  * @nr_segs: size of iov vector
90388467055SChuck Lever  * @pos: byte offset in file where writing starts
9041da177e4SLinus Torvalds  *
9051da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9061da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9071da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9081da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9091da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9101da177e4SLinus Torvalds  * parameter checking and report problems.
9111da177e4SLinus Torvalds  *
9121da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9131da177e4SLinus Torvalds  *
9141da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9151da177e4SLinus Torvalds  * readers of this file.
9161da177e4SLinus Torvalds  *
9171da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9181da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9191da177e4SLinus Torvalds  */
920027445c3SBadari Pulavarty ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
921027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
9221da177e4SLinus Torvalds {
923070ea602SChuck Lever 	ssize_t retval = -EINVAL;
9241da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9251da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
926c216fd70SChuck Lever 	size_t count;
9271da177e4SLinus Torvalds 
928c216fd70SChuck Lever 	count = iov_length(iov, nr_segs);
929c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
930c216fd70SChuck Lever 
9316da24bc9SChuck Lever 	dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
93201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
93301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name,
934c216fd70SChuck Lever 		count, (long long) pos);
935027445c3SBadari Pulavarty 
936ce1a8e67SChuck Lever 	retval = generic_write_checks(file, &pos, &count, 0);
937ce1a8e67SChuck Lever 	if (retval)
9381da177e4SLinus Torvalds 		goto out;
939ce1a8e67SChuck Lever 
940ce1a8e67SChuck Lever 	retval = -EINVAL;
941ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9421da177e4SLinus Torvalds 		goto out;
9431da177e4SLinus Torvalds 	retval = 0;
9441da177e4SLinus Torvalds 	if (!count)
9451da177e4SLinus Torvalds 		goto out;
946ce1a8e67SChuck Lever 
94729884df0STrond Myklebust 	retval = nfs_sync_mapping(mapping);
9481da177e4SLinus Torvalds 	if (retval)
9491da177e4SLinus Torvalds 		goto out;
9501da177e4SLinus Torvalds 
9517ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
9527ec10f26SKonstantin Khlebnikov 
953c216fd70SChuck Lever 	retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
9541763da12SFred Isaman 	if (retval > 0) {
9551763da12SFred Isaman 		struct inode *inode = mapping->host;
9569eafa8ccSChuck Lever 
957ce1a8e67SChuck Lever 		iocb->ki_pos = pos + retval;
9581763da12SFred Isaman 		spin_lock(&inode->i_lock);
9591763da12SFred Isaman 		if (i_size_read(inode) < iocb->ki_pos)
9601763da12SFred Isaman 			i_size_write(inode, iocb->ki_pos);
9611763da12SFred Isaman 		spin_unlock(&inode->i_lock);
9621763da12SFred Isaman 	}
9631da177e4SLinus Torvalds out:
9641da177e4SLinus Torvalds 	return retval;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
96788467055SChuck Lever /**
96888467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
96988467055SChuck Lever  *
97088467055SChuck Lever  */
971f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9721da177e4SLinus Torvalds {
9731da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9741da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
975fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
976fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
97720c2df83SPaul Mundt 						NULL);
9781da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9791da177e4SLinus Torvalds 		return -ENOMEM;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	return 0;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
98488467055SChuck Lever /**
985f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
98688467055SChuck Lever  *
98788467055SChuck Lever  */
988266bee88SDavid Brownell void nfs_destroy_directcache(void)
9891da177e4SLinus Torvalds {
9901a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
9911da177e4SLinus Torvalds }
992