xref: /openbmc/linux/fs/nfs/direct.c (revision 6cccbb6f52dceec5f4faed8846ac05ae830640e6)
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>
496296556fSPeng Tao #include <linux/module.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <linux/nfs_fs.h>
521da177e4SLinus Torvalds #include <linux/nfs_page.h>
531da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <asm/uaccess.h>
5660063497SArun Sharma #include <linux/atomic.h>
571da177e4SLinus Torvalds 
588d5658c9STrond Myklebust #include "internal.h"
5991d5b470SChuck Lever #include "iostat.h"
601763da12SFred Isaman #include "pnfs.h"
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_VFS
631da177e4SLinus Torvalds 
64e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * This represents a set of asynchronous requests that we're waiting on
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds struct nfs_direct_req {
701da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7115ce4a0cSChuck Lever 
7215ce4a0cSChuck Lever 	/* I/O parameters */
73a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
74f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7599514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
7688467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
7715ce4a0cSChuck Lever 
7815ce4a0cSChuck Lever 	/* completion state */
79607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
8015ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
8115ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
8235754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
831da177e4SLinus Torvalds 				error;		/* any reported error */
84d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
85fad61490STrond Myklebust 
86fad61490STrond Myklebust 	/* commit state */
871763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
881763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
891763da12SFred Isaman 	struct work_struct	work;
90fad61490STrond Myklebust 	int			flags;
91fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
92fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
93fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
971763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
991763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
100607f31e8STrond Myklebust 
101607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
102607f31e8STrond Myklebust {
103607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
104607f31e8STrond Myklebust }
105607f31e8STrond Myklebust 
106607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
107607f31e8STrond Myklebust {
108607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
109607f31e8STrond Myklebust }
110607f31e8STrond Myklebust 
1115002c586SWeston Andros Adamson /*
1125002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
1135002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1145002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
115*6cccbb6fSWeston Andros Adamson  * @commit_idx - commit bucket index for the DS
1165002c586SWeston Andros Adamson  *
1175002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
1185002c586SWeston Andros Adamson  */
1195002c586SWeston Andros Adamson static struct nfs_writeverf *
1205002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
1215002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
122*6cccbb6fSWeston Andros Adamson 		       int commit_idx)
1235002c586SWeston Andros Adamson {
1245002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
1255002c586SWeston Andros Adamson 
1265002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
1275002c586SWeston Andros Adamson 	if (ds_clp) {
1285002c586SWeston Andros Adamson 		/* pNFS is in use, use the DS verf */
129*6cccbb6fSWeston Andros Adamson 		if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
130*6cccbb6fSWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
1315002c586SWeston Andros Adamson 		else
1325002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
1335002c586SWeston Andros Adamson 	}
1345002c586SWeston Andros Adamson #endif
1355002c586SWeston Andros Adamson 	return verfp;
1365002c586SWeston Andros Adamson }
1375002c586SWeston Andros Adamson 
1385002c586SWeston Andros Adamson 
1395002c586SWeston Andros Adamson /*
1405002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
1415002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1425002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
1435002c586SWeston Andros Adamson  *
1445002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
1455002c586SWeston Andros Adamson  */
1465002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
1475002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
1485002c586SWeston Andros Adamson {
1495002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1505002c586SWeston Andros Adamson 
151*6cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
1525002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
1535002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
1545002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
1555002c586SWeston Andros Adamson }
1565002c586SWeston Andros Adamson 
1575002c586SWeston Andros Adamson /*
1585002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
1595002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1605002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
1615002c586SWeston Andros Adamson  *
1625002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
1635002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
1645002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
1655002c586SWeston Andros Adamson  */
1665002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
1675002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
1685002c586SWeston Andros Adamson {
1695002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1705002c586SWeston Andros Adamson 
171*6cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
1725002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
1735002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
1745002c586SWeston Andros Adamson 		return 0;
1755002c586SWeston Andros Adamson 	}
1765002c586SWeston Andros Adamson 	return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
1775002c586SWeston Andros Adamson }
1785002c586SWeston Andros Adamson 
1795002c586SWeston Andros Adamson /*
1805002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
1815002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1825002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
1835002c586SWeston Andros Adamson  *
1845002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
1855002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
1865002c586SWeston Andros Adamson  */
1875002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
1885002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
1895002c586SWeston Andros Adamson {
1905002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1915002c586SWeston Andros Adamson 
1925002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
1935002c586SWeston Andros Adamson 					 data->ds_commit_index);
1945002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
1955002c586SWeston Andros Adamson 	return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf));
1965002c586SWeston Andros Adamson }
1975002c586SWeston Andros Adamson 
1981da177e4SLinus Torvalds /**
199b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
200b8a32e2bSChuck Lever  * @rw: direction (read or write)
201b8a32e2bSChuck Lever  * @iocb: target I/O control block
202b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
203b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
204b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
205b8a32e2bSChuck Lever  *
206b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
207a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
208a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
209a564b8f0SMel Gorman  * so this method is only ever called for swap.
2101da177e4SLinus Torvalds  */
211d8d3d94bSAl Viro ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
212b8a32e2bSChuck Lever {
213a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP
2146de1472fSAl Viro 	dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n",
215d8d3d94bSAl Viro 			iocb->ki_filp, (long long) pos, iter->nr_segs);
216b8a32e2bSChuck Lever 
217b8a32e2bSChuck Lever 	return -EINVAL;
218a564b8f0SMel Gorman #else
219a564b8f0SMel Gorman 	VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
220a564b8f0SMel Gorman 
221e19a8a0aSMartin K. Petersen 	if (rw == READ)
222e19a8a0aSMartin K. Petersen 		return nfs_file_direct_read(iocb, iter, pos);
223e19a8a0aSMartin K. Petersen 	return nfs_file_direct_write(iocb, iter, pos);
224a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */
225b8a32e2bSChuck Lever }
226b8a32e2bSChuck Lever 
227749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2289c93ab7dSChuck Lever {
229749e146eSChuck Lever 	unsigned int i;
230607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
231607f31e8STrond Myklebust 		page_cache_release(pages[i]);
2326b45d858STrond Myklebust }
2336b45d858STrond Myklebust 
2341763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
2351763da12SFred Isaman 			      struct nfs_direct_req *dreq)
2361763da12SFred Isaman {
2371763da12SFred Isaman 	cinfo->lock = &dreq->lock;
2381763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
2391763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
2401763da12SFred Isaman 	cinfo->dreq = dreq;
2411763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
2421763da12SFred Isaman }
2431763da12SFred Isaman 
24493619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
2471da177e4SLinus Torvalds 
248292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
2491da177e4SLinus Torvalds 	if (!dreq)
2501da177e4SLinus Torvalds 		return NULL;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	kref_init(&dreq->kref);
253607f31e8STrond Myklebust 	kref_get(&dreq->kref);
254d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
2551763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
2565002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
2571763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
25815ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
25993619e59SChuck Lever 
26093619e59SChuck Lever 	return dreq;
26193619e59SChuck Lever }
26293619e59SChuck Lever 
263b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
266a8881f5aSTrond Myklebust 
2678c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
268f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
269f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
270a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
271a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
2721da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
275b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
276b4946ffbSTrond Myklebust {
277b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
278b4946ffbSTrond Myklebust }
279b4946ffbSTrond Myklebust 
2806296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
2816296556fSPeng Tao {
2826296556fSPeng Tao 	return dreq->bytes_left;
2836296556fSPeng Tao }
2846296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
2856296556fSPeng Tao 
286d4cc948bSChuck Lever /*
287bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
288bc0fb201SChuck Lever  */
289bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
290bc0fb201SChuck Lever {
29115ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
292bc0fb201SChuck Lever 
293bc0fb201SChuck Lever 	/* Async requests don't wait here */
294bc0fb201SChuck Lever 	if (dreq->iocb)
295bc0fb201SChuck Lever 		goto out;
296bc0fb201SChuck Lever 
297150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
298bc0fb201SChuck Lever 
299bc0fb201SChuck Lever 	if (!result)
30015ce4a0cSChuck Lever 		result = dreq->error;
301bc0fb201SChuck Lever 	if (!result)
30215ce4a0cSChuck Lever 		result = dreq->count;
303bc0fb201SChuck Lever 
304bc0fb201SChuck Lever out:
305bc0fb201SChuck Lever 	return (ssize_t) result;
306bc0fb201SChuck Lever }
307bc0fb201SChuck Lever 
308bc0fb201SChuck Lever /*
309607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
310607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
31163ab46abSChuck Lever  */
3129811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
31363ab46abSChuck Lever {
3149811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3159811cd57SChristoph Hellwig 
3162a009ec9SChristoph Hellwig 	if (dreq->iocb && write) {
3179811cd57SChristoph Hellwig 		loff_t pos = dreq->iocb->ki_pos + dreq->count;
3189811cd57SChristoph Hellwig 
3199811cd57SChristoph Hellwig 		spin_lock(&inode->i_lock);
3209811cd57SChristoph Hellwig 		if (i_size_read(inode) < pos)
3219811cd57SChristoph Hellwig 			i_size_write(inode, pos);
3229811cd57SChristoph Hellwig 		spin_unlock(&inode->i_lock);
3239811cd57SChristoph Hellwig 	}
3249811cd57SChristoph Hellwig 
3251f90ee27SChristoph Hellwig 	if (write)
3262a009ec9SChristoph Hellwig 		nfs_zap_mapping(inode, inode->i_mapping);
3271f90ee27SChristoph Hellwig 
3282a009ec9SChristoph Hellwig 	inode_dio_done(inode);
3292a009ec9SChristoph Hellwig 
3302a009ec9SChristoph Hellwig 	if (dreq->iocb) {
3312a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
3322a009ec9SChristoph Hellwig 		if (!res)
3332a009ec9SChristoph Hellwig 			res = (long) dreq->count;
33463ab46abSChuck Lever 		aio_complete(dreq->iocb, res, 0);
335d72b7a6bSTrond Myklebust 	}
3362a009ec9SChristoph Hellwig 
337d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
33863ab46abSChuck Lever 
339b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
34063ab46abSChuck Lever }
34163ab46abSChuck Lever 
3421385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
3431da177e4SLinus Torvalds {
3441e8968c5SNiels de Vos 	dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
345584aa810SFred Isaman 		req->wb_context->dentry->d_inode->i_sb->s_id,
3461e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
347584aa810SFred Isaman 		req->wb_bytes,
348584aa810SFred Isaman 		(long long)req_offset(req));
349584aa810SFred Isaman 	nfs_release_request(req);
350fdd1e74cSTrond Myklebust }
351fdd1e74cSTrond Myklebust 
352584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
353fdd1e74cSTrond Myklebust {
354584aa810SFred Isaman 	unsigned long bytes = 0;
355584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
356fdd1e74cSTrond Myklebust 
357584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
358584aa810SFred Isaman 		goto out_put;
3591da177e4SLinus Torvalds 
36015ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
361584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
362584aa810SFred Isaman 		dreq->error = hdr->error;
363584aa810SFred Isaman 	else
364584aa810SFred Isaman 		dreq->count += hdr->good_bytes;
36515ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
3661da177e4SLinus Torvalds 
367584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
368584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
369584aa810SFred Isaman 		struct page *page = req->wb_page;
370584aa810SFred Isaman 
371be7e9858SJeff Layton 		if (!PageCompound(page) && bytes < hdr->good_bytes)
3724bd8b010STrond Myklebust 			set_page_dirty(page);
373584aa810SFred Isaman 		bytes += req->wb_bytes;
374584aa810SFred Isaman 		nfs_list_remove_request(req);
375584aa810SFred Isaman 		nfs_direct_readpage_release(req);
376584aa810SFred Isaman 	}
377584aa810SFred Isaman out_put:
378607f31e8STrond Myklebust 	if (put_dreq(dreq))
3799811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
380584aa810SFred Isaman 	hdr->release(hdr);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3833e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
384cd841605SFred Isaman {
385584aa810SFred Isaman 	struct nfs_page *req;
386cd841605SFred Isaman 
387584aa810SFred Isaman 	while (!list_empty(head)) {
388584aa810SFred Isaman 		req = nfs_list_entry(head->next);
389584aa810SFred Isaman 		nfs_list_remove_request(req);
390584aa810SFred Isaman 		nfs_release_request(req);
391cd841605SFred Isaman 	}
392584aa810SFred Isaman }
393584aa810SFred Isaman 
394584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
395584aa810SFred Isaman {
396584aa810SFred Isaman 	get_dreq(hdr->dreq);
397584aa810SFred Isaman }
398584aa810SFred Isaman 
399584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4003e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
401584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
402584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
403584aa810SFred Isaman };
404cd841605SFred Isaman 
405d4cc948bSChuck Lever /*
406607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
407607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
408607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
409607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
410607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4111da177e4SLinus Torvalds  */
41291f79c43SAl Viro 
41391f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
41491f79c43SAl Viro 					      struct iov_iter *iter,
41591f79c43SAl Viro 					      loff_t pos)
4161da177e4SLinus Torvalds {
41791f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
41891f79c43SAl Viro 	struct inode *inode = dreq->inode;
41991f79c43SAl Viro 	ssize_t result = -EINVAL;
42091f79c43SAl Viro 	size_t requested_bytes = 0;
42191f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
42282b145c5SChuck Lever 
42316b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
42491f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
42591f79c43SAl Viro 	get_dreq(dreq);
42691f79c43SAl Viro 	desc.pg_dreq = dreq;
42791f79c43SAl Viro 	atomic_inc(&inode->i_dio_count);
42891f79c43SAl Viro 
42991f79c43SAl Viro 	while (iov_iter_count(iter)) {
43091f79c43SAl Viro 		struct page **pagevec;
4315dd602f2SChuck Lever 		size_t bytes;
43291f79c43SAl Viro 		size_t pgbase;
43391f79c43SAl Viro 		unsigned npages, i;
4341da177e4SLinus Torvalds 
43591f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
43691f79c43SAl Viro 						  rsize, &pgbase);
437584aa810SFred Isaman 		if (result < 0)
438749e146eSChuck Lever 			break;
439a564b8f0SMel Gorman 
44091f79c43SAl Viro 		bytes = result;
44191f79c43SAl Viro 		iov_iter_advance(iter, bytes);
44291f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
443584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
444584aa810SFred Isaman 			struct nfs_page *req;
445bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
446584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
4472bfc6e56SWeston Andros Adamson 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
448584aa810SFred Isaman 						 pgbase, req_len);
449584aa810SFred Isaman 			if (IS_ERR(req)) {
450584aa810SFred Isaman 				result = PTR_ERR(req);
451dbae4c73STrond Myklebust 				break;
452584aa810SFred Isaman 			}
453584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
454584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
45591f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
45691f79c43SAl Viro 				result = desc.pg_error;
457584aa810SFred Isaman 				nfs_release_request(req);
458584aa810SFred Isaman 				break;
459584aa810SFred Isaman 			}
460584aa810SFred Isaman 			pgbase = 0;
461584aa810SFred Isaman 			bytes -= req_len;
46291f79c43SAl Viro 			requested_bytes += req_len;
463584aa810SFred Isaman 			pos += req_len;
46435754bc0SPeng Tao 			dreq->bytes_left -= req_len;
465584aa810SFred Isaman 		}
4666d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
46791f79c43SAl Viro 		kvfree(pagevec);
46819f73787SChuck Lever 		if (result < 0)
46919f73787SChuck Lever 			break;
47019f73787SChuck Lever 	}
47119f73787SChuck Lever 
472584aa810SFred Isaman 	nfs_pageio_complete(&desc);
473584aa810SFred Isaman 
474839f7ad6SChuck Lever 	/*
475839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
476839f7ad6SChuck Lever 	 * generic layer handle the completion.
477839f7ad6SChuck Lever 	 */
478839f7ad6SChuck Lever 	if (requested_bytes == 0) {
4791f90ee27SChristoph Hellwig 		inode_dio_done(inode);
480839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
481839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
482839f7ad6SChuck Lever 	}
483839f7ad6SChuck Lever 
48419f73787SChuck Lever 	if (put_dreq(dreq))
4859811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
48619f73787SChuck Lever 	return 0;
48719f73787SChuck Lever }
48819f73787SChuck Lever 
48914a3ec79SChristoph Hellwig /**
49014a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
49114a3ec79SChristoph Hellwig  * @iocb: target I/O control block
492619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
49314a3ec79SChristoph Hellwig  * @pos: byte offset in file where reading starts
49414a3ec79SChristoph Hellwig  *
49514a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
49614a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
49714a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
49814a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
49914a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
50014a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
50114a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
50214a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
50314a3ec79SChristoph Hellwig  *
50414a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
50514a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
50614a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
50714a3ec79SChristoph Hellwig  * cache.
50814a3ec79SChristoph Hellwig  */
509619d30b4SAl Viro ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
510e19a8a0aSMartin K. Petersen 				loff_t pos)
5111da177e4SLinus Torvalds {
51214a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
51314a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
51414a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5151da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
516b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
51714a3ec79SChristoph Hellwig 	ssize_t result = -EINVAL;
518a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
51914a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
52014a3ec79SChristoph Hellwig 
52114a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
52214a3ec79SChristoph Hellwig 		file, count, (long long) pos);
52314a3ec79SChristoph Hellwig 
52414a3ec79SChristoph Hellwig 	result = 0;
52514a3ec79SChristoph Hellwig 	if (!count)
52614a3ec79SChristoph Hellwig 		goto out;
52714a3ec79SChristoph Hellwig 
528d0b9875dSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
52914a3ec79SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
53014a3ec79SChristoph Hellwig 	if (result)
531d0b9875dSChristoph Hellwig 		goto out_unlock;
53214a3ec79SChristoph Hellwig 
53314a3ec79SChristoph Hellwig 	task_io_account_read(count);
53414a3ec79SChristoph Hellwig 
53514a3ec79SChristoph Hellwig 	result = -ENOMEM;
536607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
537f11ac8dbSTrond Myklebust 	if (dreq == NULL)
538d0b9875dSChristoph Hellwig 		goto out_unlock;
5391da177e4SLinus Torvalds 
54091d5b470SChuck Lever 	dreq->inode = inode;
541619d30b4SAl Viro 	dreq->bytes_left = count;
542cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
543b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
544b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
545b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
546f11ac8dbSTrond Myklebust 		goto out_release;
547b3c54de6STrond Myklebust 	}
548b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
549487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
550487b8372SChuck Lever 		dreq->iocb = iocb;
5511da177e4SLinus Torvalds 
552619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
55391f79c43SAl Viro 	result = nfs_direct_read_schedule_iovec(dreq, iter, pos);
554d0b9875dSChristoph Hellwig 
555d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
556d0b9875dSChristoph Hellwig 
55714a3ec79SChristoph Hellwig 	if (!result) {
558bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
55914a3ec79SChristoph Hellwig 		if (result > 0)
56014a3ec79SChristoph Hellwig 			iocb->ki_pos = pos + result;
56114a3ec79SChristoph Hellwig 	}
562d0b9875dSChristoph Hellwig 
563d0b9875dSChristoph Hellwig 	nfs_direct_req_release(dreq);
564d0b9875dSChristoph Hellwig 	return result;
565d0b9875dSChristoph Hellwig 
566f11ac8dbSTrond Myklebust out_release:
567b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
568d0b9875dSChristoph Hellwig out_unlock:
569d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
570f11ac8dbSTrond Myklebust out:
5711da177e4SLinus Torvalds 	return result;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574085d1e33STom Haynes static void
575085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
576085d1e33STom Haynes 				  struct list_head *list,
577085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
578085d1e33STom Haynes {
579085d1e33STom Haynes 	spin_lock(cinfo->lock);
580085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
581085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
582085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
583085d1e33STom Haynes #endif
584085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
585085d1e33STom Haynes 	spin_unlock(cinfo->lock);
586085d1e33STom Haynes }
587085d1e33STom Haynes 
588fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
5891da177e4SLinus Torvalds {
5901763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
5911763da12SFred Isaman 	struct nfs_page *req, *tmp;
5921763da12SFred Isaman 	LIST_HEAD(reqs);
5931763da12SFred Isaman 	struct nfs_commit_info cinfo;
5941763da12SFred Isaman 	LIST_HEAD(failed);
5951763da12SFred Isaman 
5961763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
597085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
5981da177e4SLinus Torvalds 
599fad61490STrond Myklebust 	dreq->count = 0;
600607f31e8STrond Myklebust 	get_dreq(dreq);
6011da177e4SLinus Torvalds 
602a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6031763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6041763da12SFred Isaman 	desc.pg_dreq = dreq;
605607f31e8STrond Myklebust 
6061763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
6071763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
6084035c248STrond Myklebust 			nfs_list_remove_request(req);
6091763da12SFred Isaman 			nfs_list_add_request(req, &failed);
6101763da12SFred Isaman 			spin_lock(cinfo.lock);
6111763da12SFred Isaman 			dreq->flags = 0;
6121763da12SFred Isaman 			dreq->error = -EIO;
6131763da12SFred Isaman 			spin_unlock(cinfo.lock);
6141763da12SFred Isaman 		}
6155a695da2STrond Myklebust 		nfs_release_request(req);
6161763da12SFred Isaman 	}
6171763da12SFred Isaman 	nfs_pageio_complete(&desc);
618607f31e8STrond Myklebust 
6194035c248STrond Myklebust 	while (!list_empty(&failed)) {
6204035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
6214035c248STrond Myklebust 		nfs_list_remove_request(req);
6221d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
6234035c248STrond Myklebust 	}
624607f31e8STrond Myklebust 
625607f31e8STrond Myklebust 	if (put_dreq(dreq))
6261763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
627fad61490STrond Myklebust }
6281da177e4SLinus Torvalds 
6291763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
630fad61490STrond Myklebust {
6310b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
6321763da12SFred Isaman 	struct nfs_commit_info cinfo;
6331763da12SFred Isaman 	struct nfs_page *req;
634c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
635c9d8f89dSTrond Myklebust 
6361763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
637c9d8f89dSTrond Myklebust 	if (status < 0) {
63860fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
639c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
640fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
6415002c586SWeston Andros Adamson 	} else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
642c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
643fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
644fad61490STrond Myklebust 	}
645fad61490STrond Myklebust 
646c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
6471763da12SFred Isaman 	while (!list_empty(&data->pages)) {
6481763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
6491763da12SFred Isaman 		nfs_list_remove_request(req);
6501763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
6511763da12SFred Isaman 			/* Note the rewrite will go through mds */
6521763da12SFred Isaman 			nfs_mark_request_commit(req, NULL, &cinfo);
653906369e4SFred Isaman 		} else
654906369e4SFred Isaman 			nfs_release_request(req);
6551d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
656fad61490STrond Myklebust 	}
657fad61490STrond Myklebust 
6581763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
6591763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
6601763da12SFred Isaman }
6611763da12SFred Isaman 
6621763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
6631763da12SFred Isaman {
6641763da12SFred Isaman 	/* There is no lock to clear */
6651763da12SFred Isaman }
6661763da12SFred Isaman 
6671763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
6681763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
6691763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
670fad61490STrond Myklebust };
671fad61490STrond Myklebust 
672fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
673fad61490STrond Myklebust {
6741763da12SFred Isaman 	int res;
6751763da12SFred Isaman 	struct nfs_commit_info cinfo;
6761763da12SFred Isaman 	LIST_HEAD(mds_list);
677fad61490STrond Myklebust 
6781763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
6791763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
6801763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
6811763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
6821763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
6861da177e4SLinus Torvalds {
6871763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
688fad61490STrond Myklebust 	int flags = dreq->flags;
6891da177e4SLinus Torvalds 
690fad61490STrond Myklebust 	dreq->flags = 0;
691fad61490STrond Myklebust 	switch (flags) {
692fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
693fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
6941da177e4SLinus Torvalds 			break;
695fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
696fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
6971da177e4SLinus Torvalds 			break;
6981da177e4SLinus Torvalds 		default:
6999811cd57SChristoph Hellwig 			nfs_direct_complete(dreq, true);
7001da177e4SLinus Torvalds 	}
701fad61490STrond Myklebust }
702fad61490STrond Myklebust 
7031763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
704fad61490STrond Myklebust {
7051763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
706fad61490STrond Myklebust }
7071763da12SFred Isaman 
7081763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7091763da12SFred Isaman {
7101763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7111763da12SFred Isaman 	struct nfs_commit_info cinfo;
712c65e6254SWeston Andros Adamson 	bool request_commit = false;
7131763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7141763da12SFred Isaman 
7151763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
7161763da12SFred Isaman 		goto out_put;
7171763da12SFred Isaman 
7181763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7191763da12SFred Isaman 
7201763da12SFred Isaman 	spin_lock(&dreq->lock);
7211763da12SFred Isaman 
7221763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7231763da12SFred Isaman 		dreq->flags = 0;
7241763da12SFred Isaman 		dreq->error = hdr->error;
7251763da12SFred Isaman 	}
726c65e6254SWeston Andros Adamson 	if (dreq->error == 0) {
7271763da12SFred Isaman 		dreq->count += hdr->good_bytes;
728c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
7291763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
730c65e6254SWeston Andros Adamson 				request_commit = true;
7311763da12SFred Isaman 			else if (dreq->flags == 0) {
7325002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
733c65e6254SWeston Andros Adamson 				request_commit = true;
7341763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7351763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
736c65e6254SWeston Andros Adamson 				request_commit = true;
737c65e6254SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
7385002c586SWeston Andros Adamson 					dreq->flags =
7395002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
7401763da12SFred Isaman 			}
7411763da12SFred Isaman 		}
7421763da12SFred Isaman 	}
7431763da12SFred Isaman 	spin_unlock(&dreq->lock);
7441763da12SFred Isaman 
7451763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
7462bfc6e56SWeston Andros Adamson 
7471763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
7481763da12SFred Isaman 		nfs_list_remove_request(req);
749c65e6254SWeston Andros Adamson 		if (request_commit) {
75004277086STrond Myklebust 			kref_get(&req->wb_kref);
7511763da12SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
7521763da12SFred Isaman 		}
7531d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7541763da12SFred Isaman 	}
7551763da12SFred Isaman 
7561763da12SFred Isaman out_put:
7571763da12SFred Isaman 	if (put_dreq(dreq))
7581763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
7591763da12SFred Isaman 	hdr->release(hdr);
7601763da12SFred Isaman }
7611763da12SFred Isaman 
7623e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
7633e9e0ca3STrond Myklebust {
7643e9e0ca3STrond Myklebust 	struct nfs_page *req;
7653e9e0ca3STrond Myklebust 
7663e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
7673e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
7683e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
7691d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
7703e9e0ca3STrond Myklebust 	}
7713e9e0ca3STrond Myklebust }
7723e9e0ca3STrond Myklebust 
7731763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
7743e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
7751763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
7761763da12SFred Isaman 	.completion = nfs_direct_write_completion,
7771763da12SFred Isaman };
7781763da12SFred Isaman 
77991f79c43SAl Viro 
78091f79c43SAl Viro /*
78191f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
78291f79c43SAl Viro  *     errors after the first one are ignored.
78391f79c43SAl Viro  */
78491f79c43SAl Viro /*
78591f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
78691f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
78791f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
78891f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
78991f79c43SAl Viro  * no requests have been sent, just return an error.
79091f79c43SAl Viro  */
79119f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
792619d30b4SAl Viro 					       struct iov_iter *iter,
79391f79c43SAl Viro 					       loff_t pos)
79419f73787SChuck Lever {
7951763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
7961d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
79719f73787SChuck Lever 	ssize_t result = 0;
79819f73787SChuck Lever 	size_t requested_bytes = 0;
79991f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
80019f73787SChuck Lever 
801a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
8021763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8031763da12SFred Isaman 	desc.pg_dreq = dreq;
80419f73787SChuck Lever 	get_dreq(dreq);
8051d59d61fSTrond Myklebust 	atomic_inc(&inode->i_dio_count);
80619f73787SChuck Lever 
80791f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
80891f79c43SAl Viro 	while (iov_iter_count(iter)) {
80991f79c43SAl Viro 		struct page **pagevec;
81091f79c43SAl Viro 		size_t bytes;
81191f79c43SAl Viro 		size_t pgbase;
81291f79c43SAl Viro 		unsigned npages, i;
81391f79c43SAl Viro 
81491f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
81591f79c43SAl Viro 						  wsize, &pgbase);
81619f73787SChuck Lever 		if (result < 0)
81719f73787SChuck Lever 			break;
81891f79c43SAl Viro 
81991f79c43SAl Viro 		bytes = result;
82091f79c43SAl Viro 		iov_iter_advance(iter, bytes);
82191f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
82291f79c43SAl Viro 		for (i = 0; i < npages; i++) {
82391f79c43SAl Viro 			struct nfs_page *req;
82491f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
82591f79c43SAl Viro 
82616b90578SLinus Torvalds 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
82791f79c43SAl Viro 						 pgbase, req_len);
82891f79c43SAl Viro 			if (IS_ERR(req)) {
82991f79c43SAl Viro 				result = PTR_ERR(req);
83019f73787SChuck Lever 				break;
83191f79c43SAl Viro 			}
83291f79c43SAl Viro 			nfs_lock_request(req);
83391f79c43SAl Viro 			req->wb_index = pos >> PAGE_SHIFT;
83491f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
83591f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
83691f79c43SAl Viro 				result = desc.pg_error;
83791f79c43SAl Viro 				nfs_unlock_and_release_request(req);
83891f79c43SAl Viro 				break;
83991f79c43SAl Viro 			}
84091f79c43SAl Viro 			pgbase = 0;
84191f79c43SAl Viro 			bytes -= req_len;
84291f79c43SAl Viro 			requested_bytes += req_len;
84391f79c43SAl Viro 			pos += req_len;
84491f79c43SAl Viro 			dreq->bytes_left -= req_len;
84591f79c43SAl Viro 		}
84691f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
84791f79c43SAl Viro 		kvfree(pagevec);
84891f79c43SAl Viro 		if (result < 0)
84991f79c43SAl Viro 			break;
85019f73787SChuck Lever 	}
8511763da12SFred Isaman 	nfs_pageio_complete(&desc);
85219f73787SChuck Lever 
853839f7ad6SChuck Lever 	/*
854839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
855839f7ad6SChuck Lever 	 * generic layer handle the completion.
856839f7ad6SChuck Lever 	 */
857839f7ad6SChuck Lever 	if (requested_bytes == 0) {
8581d59d61fSTrond Myklebust 		inode_dio_done(inode);
859839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
860839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
861839f7ad6SChuck Lever 	}
862839f7ad6SChuck Lever 
86319f73787SChuck Lever 	if (put_dreq(dreq))
86419f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
86519f73787SChuck Lever 	return 0;
86619f73787SChuck Lever }
86719f73787SChuck Lever 
8681da177e4SLinus Torvalds /**
8691da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
8701da177e4SLinus Torvalds  * @iocb: target I/O control block
871619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
87288467055SChuck Lever  * @pos: byte offset in file where writing starts
8731da177e4SLinus Torvalds  *
8741da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
8751da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
8761da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
8771da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
8781da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
8791da177e4SLinus Torvalds  * parameter checking and report problems.
8801da177e4SLinus Torvalds  *
8811da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
8821da177e4SLinus Torvalds  *
8831da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
8841da177e4SLinus Torvalds  * readers of this file.
8851da177e4SLinus Torvalds  *
8861da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
8871da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
8881da177e4SLinus Torvalds  */
889619d30b4SAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
890e19a8a0aSMartin K. Petersen 				loff_t pos)
8911da177e4SLinus Torvalds {
89222cd1bf1SChristoph Hellwig 	ssize_t result = -EINVAL;
8931da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
8941da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
89522cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
89622cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
89722cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
898a9ab5e84SChristoph Hellwig 	loff_t end;
899a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
900a9ab5e84SChristoph Hellwig 	end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
901a9ab5e84SChristoph Hellwig 
902c216fd70SChuck Lever 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
903c216fd70SChuck Lever 
9046de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
9056de1472fSAl Viro 		file, count, (long long) pos);
906027445c3SBadari Pulavarty 
90722cd1bf1SChristoph Hellwig 	result = generic_write_checks(file, &pos, &count, 0);
90822cd1bf1SChristoph Hellwig 	if (result)
9091da177e4SLinus Torvalds 		goto out;
910ce1a8e67SChuck Lever 
91122cd1bf1SChristoph Hellwig 	result = -EINVAL;
912ce1a8e67SChuck Lever 	if ((ssize_t) count < 0)
9131da177e4SLinus Torvalds 		goto out;
91422cd1bf1SChristoph Hellwig 	result = 0;
9151da177e4SLinus Torvalds 	if (!count)
9161da177e4SLinus Torvalds 		goto out;
917ce1a8e67SChuck Lever 
918a9ab5e84SChristoph Hellwig 	mutex_lock(&inode->i_mutex);
919a9ab5e84SChristoph Hellwig 
92022cd1bf1SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
92122cd1bf1SChristoph Hellwig 	if (result)
922a9ab5e84SChristoph Hellwig 		goto out_unlock;
923a9ab5e84SChristoph Hellwig 
924a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
925a9ab5e84SChristoph Hellwig 		result = invalidate_inode_pages2_range(mapping,
926a9ab5e84SChristoph Hellwig 					pos >> PAGE_CACHE_SHIFT, end);
927a9ab5e84SChristoph Hellwig 		if (result)
928a9ab5e84SChristoph Hellwig 			goto out_unlock;
929a9ab5e84SChristoph Hellwig 	}
9301da177e4SLinus Torvalds 
9317ec10f26SKonstantin Khlebnikov 	task_io_account_write(count);
9327ec10f26SKonstantin Khlebnikov 
93322cd1bf1SChristoph Hellwig 	result = -ENOMEM;
93422cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
93522cd1bf1SChristoph Hellwig 	if (!dreq)
936a9ab5e84SChristoph Hellwig 		goto out_unlock;
93722cd1bf1SChristoph Hellwig 
93822cd1bf1SChristoph Hellwig 	dreq->inode = inode;
93922cd1bf1SChristoph Hellwig 	dreq->bytes_left = count;
94022cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
94122cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
94222cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
94322cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
94422cd1bf1SChristoph Hellwig 		goto out_release;
94522cd1bf1SChristoph Hellwig 	}
94622cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
94722cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
94822cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
94922cd1bf1SChristoph Hellwig 
95091f79c43SAl Viro 	result = nfs_direct_write_schedule_iovec(dreq, iter, pos);
951a9ab5e84SChristoph Hellwig 
952a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
953a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
954a9ab5e84SChristoph Hellwig 					      pos >> PAGE_CACHE_SHIFT, end);
955a9ab5e84SChristoph Hellwig 	}
956a9ab5e84SChristoph Hellwig 
957a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
958a9ab5e84SChristoph Hellwig 
95922cd1bf1SChristoph Hellwig 	if (!result) {
96022cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
96122cd1bf1SChristoph Hellwig 		if (result > 0) {
9621763da12SFred Isaman 			struct inode *inode = mapping->host;
9639eafa8ccSChuck Lever 
96422cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
9651763da12SFred Isaman 			spin_lock(&inode->i_lock);
9661763da12SFred Isaman 			if (i_size_read(inode) < iocb->ki_pos)
9671763da12SFred Isaman 				i_size_write(inode, iocb->ki_pos);
9681763da12SFred Isaman 			spin_unlock(&inode->i_lock);
9691763da12SFred Isaman 		}
97022cd1bf1SChristoph Hellwig 	}
971a9ab5e84SChristoph Hellwig 	nfs_direct_req_release(dreq);
972a9ab5e84SChristoph Hellwig 	return result;
973a9ab5e84SChristoph Hellwig 
97422cd1bf1SChristoph Hellwig out_release:
97522cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
976a9ab5e84SChristoph Hellwig out_unlock:
977a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
9781da177e4SLinus Torvalds out:
97922cd1bf1SChristoph Hellwig 	return result;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
98288467055SChuck Lever /**
98388467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
98488467055SChuck Lever  *
98588467055SChuck Lever  */
986f7b422b1SDavid Howells int __init nfs_init_directcache(void)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
9891da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
990fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
991fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
99220c2df83SPaul Mundt 						NULL);
9931da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
9941da177e4SLinus Torvalds 		return -ENOMEM;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	return 0;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
99988467055SChuck Lever /**
1000f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
100188467055SChuck Lever  *
100288467055SChuck Lever  */
1003266bee88SDavid Brownell void nfs_destroy_directcache(void)
10041da177e4SLinus Torvalds {
10051a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10061da177e4SLinus Torvalds }
1007