xref: /openbmc/linux/fs/nfs/direct.c (revision 85128b2be6738fe36c224995c5ee80fe80fbb1b5)
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 
557c0f6ba6SLinus Torvalds #include <linux/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  */
690a00b77bSWeston Andros Adamson struct nfs_direct_mirror {
700a00b77bSWeston Andros Adamson 	ssize_t count;
710a00b77bSWeston Andros Adamson };
720a00b77bSWeston Andros Adamson 
731da177e4SLinus Torvalds struct nfs_direct_req {
741da177e4SLinus Torvalds 	struct kref		kref;		/* release manager */
7515ce4a0cSChuck Lever 
7615ce4a0cSChuck Lever 	/* I/O parameters */
77a8881f5aSTrond Myklebust 	struct nfs_open_context	*ctx;		/* file open context info */
78f11ac8dbSTrond Myklebust 	struct nfs_lock_context *l_ctx;		/* Lock context info */
7999514f8fSChuck Lever 	struct kiocb *		iocb;		/* controlling i/o request */
8088467055SChuck Lever 	struct inode *		inode;		/* target file of i/o */
8115ce4a0cSChuck Lever 
8215ce4a0cSChuck Lever 	/* completion state */
83607f31e8STrond Myklebust 	atomic_t		io_count;	/* i/os we're waiting for */
8415ce4a0cSChuck Lever 	spinlock_t		lock;		/* protect completion state */
850a00b77bSWeston Andros Adamson 
860a00b77bSWeston Andros Adamson 	struct nfs_direct_mirror mirrors[NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX];
870a00b77bSWeston Andros Adamson 	int			mirror_count;
880a00b77bSWeston Andros Adamson 
8915ce4a0cSChuck Lever 	ssize_t			count,		/* bytes actually processed */
90ed3743a6SWeston Andros Adamson 				max_count,	/* max expected count */
9135754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
925fadeb47SPeng Tao 				io_start,	/* start of IO */
931da177e4SLinus Torvalds 				error;		/* any reported error */
94d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
95fad61490STrond Myklebust 
96fad61490STrond Myklebust 	/* commit state */
971763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
981763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
991763da12SFred Isaman 	struct work_struct	work;
100fad61490STrond Myklebust 	int			flags;
101fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
102fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
103fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
1071763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
1084d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
1091763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
110607f31e8STrond Myklebust 
111607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
112607f31e8STrond Myklebust {
113607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
114607f31e8STrond Myklebust }
115607f31e8STrond Myklebust 
116607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
117607f31e8STrond Myklebust {
118607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
119607f31e8STrond Myklebust }
120607f31e8STrond Myklebust 
1210a00b77bSWeston Andros Adamson static void
1220a00b77bSWeston Andros Adamson nfs_direct_good_bytes(struct nfs_direct_req *dreq, struct nfs_pgio_header *hdr)
1230a00b77bSWeston Andros Adamson {
1240a00b77bSWeston Andros Adamson 	int i;
1250a00b77bSWeston Andros Adamson 	ssize_t count;
1260a00b77bSWeston Andros Adamson 
127ed3743a6SWeston Andros Adamson 	WARN_ON_ONCE(dreq->count >= dreq->max_count);
128ed3743a6SWeston Andros Adamson 
1291ccbad9fSPeng Tao 	if (dreq->mirror_count == 1) {
1301ccbad9fSPeng Tao 		dreq->mirrors[hdr->pgio_mirror_idx].count += hdr->good_bytes;
1311ccbad9fSPeng Tao 		dreq->count += hdr->good_bytes;
1321ccbad9fSPeng Tao 	} else {
1331ccbad9fSPeng Tao 		/* mirrored writes */
1345fadeb47SPeng Tao 		count = dreq->mirrors[hdr->pgio_mirror_idx].count;
1355fadeb47SPeng Tao 		if (count + dreq->io_start < hdr->io_start + hdr->good_bytes) {
1365fadeb47SPeng Tao 			count = hdr->io_start + hdr->good_bytes - dreq->io_start;
1375fadeb47SPeng Tao 			dreq->mirrors[hdr->pgio_mirror_idx].count = count;
1385fadeb47SPeng Tao 		}
1390a00b77bSWeston Andros Adamson 		/* update the dreq->count by finding the minimum agreed count from all
1400a00b77bSWeston Andros Adamson 		 * mirrors */
1410a00b77bSWeston Andros Adamson 		count = dreq->mirrors[0].count;
1420a00b77bSWeston Andros Adamson 
1430a00b77bSWeston Andros Adamson 		for (i = 1; i < dreq->mirror_count; i++)
1440a00b77bSWeston Andros Adamson 			count = min(count, dreq->mirrors[i].count);
1450a00b77bSWeston Andros Adamson 
1460a00b77bSWeston Andros Adamson 		dreq->count = count;
1470a00b77bSWeston Andros Adamson 	}
1481ccbad9fSPeng Tao }
1490a00b77bSWeston Andros Adamson 
1505002c586SWeston Andros Adamson /*
1515002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
1525002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1535002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
1546cccbb6fSWeston Andros Adamson  * @commit_idx - commit bucket index for the DS
1555002c586SWeston Andros Adamson  *
1565002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
1575002c586SWeston Andros Adamson  */
1585002c586SWeston Andros Adamson static struct nfs_writeverf *
1595002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
1605002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
1616cccbb6fSWeston Andros Adamson 		       int commit_idx)
1625002c586SWeston Andros Adamson {
1635002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
1645002c586SWeston Andros Adamson 
1655002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
166834e465bSKinglong Mee 	/*
167834e465bSKinglong Mee 	 * pNFS is in use, use the DS verf except commit_through_mds is set
168834e465bSKinglong Mee 	 * for layout segment where nbuckets is zero.
169834e465bSKinglong Mee 	 */
170834e465bSKinglong Mee 	if (ds_clp && dreq->ds_cinfo.nbuckets > 0) {
1716cccbb6fSWeston Andros Adamson 		if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
1726cccbb6fSWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
1735002c586SWeston Andros Adamson 		else
1745002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
1755002c586SWeston Andros Adamson 	}
1765002c586SWeston Andros Adamson #endif
1775002c586SWeston Andros Adamson 	return verfp;
1785002c586SWeston Andros Adamson }
1795002c586SWeston Andros Adamson 
1805002c586SWeston Andros Adamson 
1815002c586SWeston Andros Adamson /*
1825002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
1835002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1845002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
1855002c586SWeston Andros Adamson  *
1865002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
1875002c586SWeston Andros Adamson  */
1885002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
1895002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
1905002c586SWeston Andros Adamson {
1915002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1925002c586SWeston Andros Adamson 
1936cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
1945002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
1955002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
1965002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
1975002c586SWeston Andros Adamson }
1985002c586SWeston Andros Adamson 
1998fc3c386STrond Myklebust static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1,
2008fc3c386STrond Myklebust 		const struct nfs_writeverf *v2)
2018fc3c386STrond Myklebust {
2028fc3c386STrond Myklebust 	return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier);
2038fc3c386STrond Myklebust }
2048fc3c386STrond Myklebust 
2055002c586SWeston Andros Adamson /*
2065002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
2075002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2085002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
2095002c586SWeston Andros Adamson  *
2105002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
2115002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
2125002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
2135002c586SWeston Andros Adamson  */
2145002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
2155002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
2165002c586SWeston Andros Adamson {
2175002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2185002c586SWeston Andros Adamson 
2196cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
2205002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
2215002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
2225002c586SWeston Andros Adamson 		return 0;
2235002c586SWeston Andros Adamson 	}
2248fc3c386STrond Myklebust 	return nfs_direct_cmp_verf(verfp, &hdr->verf);
2255002c586SWeston Andros Adamson }
2265002c586SWeston Andros Adamson 
2275002c586SWeston Andros Adamson /*
2285002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
2295002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2305002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
2315002c586SWeston Andros Adamson  *
2325002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
2335002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
2345002c586SWeston Andros Adamson  */
2355002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
2365002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
2375002c586SWeston Andros Adamson {
2385002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2395002c586SWeston Andros Adamson 
2405002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
2415002c586SWeston Andros Adamson 					 data->ds_commit_index);
24280c76fe3SWeston Andros Adamson 
24380c76fe3SWeston Andros Adamson 	/* verifier not set so always fail */
24480c76fe3SWeston Andros Adamson 	if (verfp->committed < 0)
24580c76fe3SWeston Andros Adamson 		return 1;
24680c76fe3SWeston Andros Adamson 
2478fc3c386STrond Myklebust 	return nfs_direct_cmp_verf(verfp, &data->verf);
2485002c586SWeston Andros Adamson }
2495002c586SWeston Andros Adamson 
2501da177e4SLinus Torvalds /**
251b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
252b8a32e2bSChuck Lever  * @iocb: target I/O control block
25390090ae6SAl Viro  * @iter: I/O buffer
254b8a32e2bSChuck Lever  *
255b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
256a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
257a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
258a564b8f0SMel Gorman  * so this method is only ever called for swap.
2591da177e4SLinus Torvalds  */
260c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
261b8a32e2bSChuck Lever {
262ee8a1a8bSPeng Tao 	struct inode *inode = iocb->ki_filp->f_mapping->host;
263ee8a1a8bSPeng Tao 
264ee8a1a8bSPeng Tao 	/* we only support swap file calling nfs_direct_IO */
265ee8a1a8bSPeng Tao 	if (!IS_SWAPFILE(inode))
266ee8a1a8bSPeng Tao 		return 0;
267ee8a1a8bSPeng Tao 
26866ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
269a564b8f0SMel Gorman 
2706f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
271c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, iter);
27265a4a1caSAl Viro 	return nfs_file_direct_write(iocb, iter);
273b8a32e2bSChuck Lever }
274b8a32e2bSChuck Lever 
275749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2769c93ab7dSChuck Lever {
277749e146eSChuck Lever 	unsigned int i;
278607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
27909cbfeafSKirill A. Shutemov 		put_page(pages[i]);
2806b45d858STrond Myklebust }
2816b45d858STrond Myklebust 
2821763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
2831763da12SFred Isaman 			      struct nfs_direct_req *dreq)
2841763da12SFred Isaman {
285fe238e60SDave Wysochanski 	cinfo->inode = dreq->inode;
2861763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
2871763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
2881763da12SFred Isaman 	cinfo->dreq = dreq;
2891763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
2901763da12SFred Isaman }
2911763da12SFred Isaman 
2920a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq,
2930a00b77bSWeston Andros Adamson 					     struct nfs_pageio_descriptor *pgio,
2940a00b77bSWeston Andros Adamson 					     struct nfs_page *req)
2950a00b77bSWeston Andros Adamson {
2960a00b77bSWeston Andros Adamson 	int mirror_count = 1;
2970a00b77bSWeston Andros Adamson 
2980a00b77bSWeston Andros Adamson 	if (pgio->pg_ops->pg_get_mirror_count)
2990a00b77bSWeston Andros Adamson 		mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
3000a00b77bSWeston Andros Adamson 
3010a00b77bSWeston Andros Adamson 	dreq->mirror_count = mirror_count;
3020a00b77bSWeston Andros Adamson }
3030a00b77bSWeston Andros Adamson 
30493619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3071da177e4SLinus Torvalds 
308292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
3091da177e4SLinus Torvalds 	if (!dreq)
3101da177e4SLinus Torvalds 		return NULL;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	kref_init(&dreq->kref);
313607f31e8STrond Myklebust 	kref_get(&dreq->kref);
314d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
3151763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
3165002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
3171763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
3180a00b77bSWeston Andros Adamson 	dreq->mirror_count = 1;
31915ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
32093619e59SChuck Lever 
32193619e59SChuck Lever 	return dreq;
32293619e59SChuck Lever }
32393619e59SChuck Lever 
324b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
327a8881f5aSTrond Myklebust 
3288c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
329f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
330f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
331a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
332a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
3331da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
337b4946ffbSTrond Myklebust {
338b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
339b4946ffbSTrond Myklebust }
340b4946ffbSTrond Myklebust 
3416296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
3426296556fSPeng Tao {
3436296556fSPeng Tao 	return dreq->bytes_left;
3446296556fSPeng Tao }
3456296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
3466296556fSPeng Tao 
347d4cc948bSChuck Lever /*
348bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
349bc0fb201SChuck Lever  */
350bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
351bc0fb201SChuck Lever {
35215ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
353bc0fb201SChuck Lever 
354bc0fb201SChuck Lever 	/* Async requests don't wait here */
355bc0fb201SChuck Lever 	if (dreq->iocb)
356bc0fb201SChuck Lever 		goto out;
357bc0fb201SChuck Lever 
358150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
359bc0fb201SChuck Lever 
360d2a7de0bSTrond Myklebust 	if (!result) {
361d2a7de0bSTrond Myklebust 		result = dreq->count;
362d2a7de0bSTrond Myklebust 		WARN_ON_ONCE(dreq->count < 0);
363d2a7de0bSTrond Myklebust 	}
364bc0fb201SChuck Lever 	if (!result)
36515ce4a0cSChuck Lever 		result = dreq->error;
366bc0fb201SChuck Lever 
367bc0fb201SChuck Lever out:
368bc0fb201SChuck Lever 	return (ssize_t) result;
369bc0fb201SChuck Lever }
370bc0fb201SChuck Lever 
371bc0fb201SChuck Lever /*
372607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
373607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
37463ab46abSChuck Lever  */
375f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq)
37663ab46abSChuck Lever {
3779811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3789811cd57SChristoph Hellwig 
379fe0f07d0SJens Axboe 	inode_dio_end(inode);
3802a009ec9SChristoph Hellwig 
3812a009ec9SChristoph Hellwig 	if (dreq->iocb) {
3822a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
383d2a7de0bSTrond Myklebust 		if (dreq->count != 0) {
3842a009ec9SChristoph Hellwig 			res = (long) dreq->count;
385d2a7de0bSTrond Myklebust 			WARN_ON_ONCE(dreq->count < 0);
386d2a7de0bSTrond Myklebust 		}
38704b2fa9fSChristoph Hellwig 		dreq->iocb->ki_complete(dreq->iocb, res, 0);
388d72b7a6bSTrond Myklebust 	}
3892a009ec9SChristoph Hellwig 
390024de8f1SDaniel Wagner 	complete(&dreq->completion);
39163ab46abSChuck Lever 
392b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
39363ab46abSChuck Lever }
39463ab46abSChuck Lever 
3951385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
3961da177e4SLinus Torvalds {
3971e8968c5SNiels de Vos 	dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
398fc64005cSAl Viro 		req->wb_context->dentry->d_sb->s_id,
3992b0143b5SDavid Howells 		(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
400584aa810SFred Isaman 		req->wb_bytes,
401584aa810SFred Isaman 		(long long)req_offset(req));
402584aa810SFred Isaman 	nfs_release_request(req);
403fdd1e74cSTrond Myklebust }
404fdd1e74cSTrond Myklebust 
405584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
406fdd1e74cSTrond Myklebust {
407584aa810SFred Isaman 	unsigned long bytes = 0;
408584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
409fdd1e74cSTrond Myklebust 
410584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
411584aa810SFred Isaman 		goto out_put;
4121da177e4SLinus Torvalds 
41315ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
414584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
415584aa810SFred Isaman 		dreq->error = hdr->error;
416584aa810SFred Isaman 	else
4170a00b77bSWeston Andros Adamson 		nfs_direct_good_bytes(dreq, hdr);
4180a00b77bSWeston Andros Adamson 
41915ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
4201da177e4SLinus Torvalds 
421584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
422584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
423584aa810SFred Isaman 		struct page *page = req->wb_page;
424584aa810SFred Isaman 
425be7e9858SJeff Layton 		if (!PageCompound(page) && bytes < hdr->good_bytes)
4264bd8b010STrond Myklebust 			set_page_dirty(page);
427584aa810SFred Isaman 		bytes += req->wb_bytes;
428584aa810SFred Isaman 		nfs_list_remove_request(req);
429584aa810SFred Isaman 		nfs_direct_readpage_release(req);
430584aa810SFred Isaman 	}
431584aa810SFred Isaman out_put:
432607f31e8STrond Myklebust 	if (put_dreq(dreq))
433f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
434584aa810SFred Isaman 	hdr->release(hdr);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4373e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
438cd841605SFred Isaman {
439584aa810SFred Isaman 	struct nfs_page *req;
440cd841605SFred Isaman 
441584aa810SFred Isaman 	while (!list_empty(head)) {
442584aa810SFred Isaman 		req = nfs_list_entry(head->next);
443584aa810SFred Isaman 		nfs_list_remove_request(req);
444584aa810SFred Isaman 		nfs_release_request(req);
445cd841605SFred Isaman 	}
446584aa810SFred Isaman }
447584aa810SFred Isaman 
448584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
449584aa810SFred Isaman {
450584aa810SFred Isaman 	get_dreq(hdr->dreq);
451584aa810SFred Isaman }
452584aa810SFred Isaman 
453584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4543e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
455584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
456584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
457584aa810SFred Isaman };
458cd841605SFred Isaman 
459d4cc948bSChuck Lever /*
460607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
461607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
462607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
463607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
464607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4651da177e4SLinus Torvalds  */
46691f79c43SAl Viro 
46791f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
46891f79c43SAl Viro 					      struct iov_iter *iter,
46991f79c43SAl Viro 					      loff_t pos)
4701da177e4SLinus Torvalds {
47191f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
47291f79c43SAl Viro 	struct inode *inode = dreq->inode;
47391f79c43SAl Viro 	ssize_t result = -EINVAL;
47491f79c43SAl Viro 	size_t requested_bytes = 0;
47591f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
47682b145c5SChuck Lever 
47716b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
47891f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
47991f79c43SAl Viro 	get_dreq(dreq);
48091f79c43SAl Viro 	desc.pg_dreq = dreq;
481fe0f07d0SJens Axboe 	inode_dio_begin(inode);
48291f79c43SAl Viro 
48391f79c43SAl Viro 	while (iov_iter_count(iter)) {
48491f79c43SAl Viro 		struct page **pagevec;
4855dd602f2SChuck Lever 		size_t bytes;
48691f79c43SAl Viro 		size_t pgbase;
48791f79c43SAl Viro 		unsigned npages, i;
4881da177e4SLinus Torvalds 
48991f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
49091f79c43SAl Viro 						  rsize, &pgbase);
491584aa810SFred Isaman 		if (result < 0)
492749e146eSChuck Lever 			break;
493a564b8f0SMel Gorman 
49491f79c43SAl Viro 		bytes = result;
49591f79c43SAl Viro 		iov_iter_advance(iter, bytes);
49691f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
497584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
498584aa810SFred Isaman 			struct nfs_page *req;
499bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
500584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
5012bfc6e56SWeston Andros Adamson 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
502584aa810SFred Isaman 						 pgbase, req_len);
503584aa810SFred Isaman 			if (IS_ERR(req)) {
504584aa810SFred Isaman 				result = PTR_ERR(req);
505dbae4c73STrond Myklebust 				break;
506584aa810SFred Isaman 			}
507584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
508584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
50991f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
51091f79c43SAl Viro 				result = desc.pg_error;
511584aa810SFred Isaman 				nfs_release_request(req);
512584aa810SFred Isaman 				break;
513584aa810SFred Isaman 			}
514584aa810SFred Isaman 			pgbase = 0;
515584aa810SFred Isaman 			bytes -= req_len;
51691f79c43SAl Viro 			requested_bytes += req_len;
517584aa810SFred Isaman 			pos += req_len;
51835754bc0SPeng Tao 			dreq->bytes_left -= req_len;
519584aa810SFred Isaman 		}
5206d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
52191f79c43SAl Viro 		kvfree(pagevec);
52219f73787SChuck Lever 		if (result < 0)
52319f73787SChuck Lever 			break;
52419f73787SChuck Lever 	}
52519f73787SChuck Lever 
526584aa810SFred Isaman 	nfs_pageio_complete(&desc);
527584aa810SFred Isaman 
528839f7ad6SChuck Lever 	/*
529839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
530839f7ad6SChuck Lever 	 * generic layer handle the completion.
531839f7ad6SChuck Lever 	 */
532839f7ad6SChuck Lever 	if (requested_bytes == 0) {
533fe0f07d0SJens Axboe 		inode_dio_end(inode);
534839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
535839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
536839f7ad6SChuck Lever 	}
537839f7ad6SChuck Lever 
53819f73787SChuck Lever 	if (put_dreq(dreq))
539f7b5c340STrond Myklebust 		nfs_direct_complete(dreq);
540*85128b2bSAl Viro 	return requested_bytes;
54119f73787SChuck Lever }
54219f73787SChuck Lever 
54314a3ec79SChristoph Hellwig /**
54414a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
54514a3ec79SChristoph Hellwig  * @iocb: target I/O control block
546619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
54714a3ec79SChristoph Hellwig  *
54814a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
54914a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
55014a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
55114a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
55214a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
55314a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
55414a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
55514a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
55614a3ec79SChristoph Hellwig  *
55714a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
55814a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
55914a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
56014a3ec79SChristoph Hellwig  * cache.
56114a3ec79SChristoph Hellwig  */
562c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
5631da177e4SLinus Torvalds {
56414a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
56514a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
56614a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5671da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
568b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
569*85128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
570a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
57114a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
57214a3ec79SChristoph Hellwig 
57314a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
574c8b8e32dSChristoph Hellwig 		file, count, (long long) iocb->ki_pos);
57514a3ec79SChristoph Hellwig 
57614a3ec79SChristoph Hellwig 	result = 0;
57714a3ec79SChristoph Hellwig 	if (!count)
57814a3ec79SChristoph Hellwig 		goto out;
57914a3ec79SChristoph Hellwig 
58014a3ec79SChristoph Hellwig 	task_io_account_read(count);
58114a3ec79SChristoph Hellwig 
58214a3ec79SChristoph Hellwig 	result = -ENOMEM;
583607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
584f11ac8dbSTrond Myklebust 	if (dreq == NULL)
585a5864c99STrond Myklebust 		goto out;
5861da177e4SLinus Torvalds 
58791d5b470SChuck Lever 	dreq->inode = inode;
588ed3743a6SWeston Andros Adamson 	dreq->bytes_left = dreq->max_count = count;
589c8b8e32dSChristoph Hellwig 	dreq->io_start = iocb->ki_pos;
590cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
591b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
592b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
593b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
594f11ac8dbSTrond Myklebust 		goto out_release;
595b3c54de6STrond Myklebust 	}
596b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
597487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
598487b8372SChuck Lever 		dreq->iocb = iocb;
5991da177e4SLinus Torvalds 
600a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
601a5864c99STrond Myklebust 
602619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
603*85128b2bSAl Viro 	requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
604d0b9875dSChristoph Hellwig 
605a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
606d0b9875dSChristoph Hellwig 
607*85128b2bSAl Viro 	if (requested > 0) {
608bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
609*85128b2bSAl Viro 		if (result > 0) {
610*85128b2bSAl Viro 			requested -= result;
611c8b8e32dSChristoph Hellwig 			iocb->ki_pos += result;
61214a3ec79SChristoph Hellwig 		}
613*85128b2bSAl Viro 		iov_iter_revert(iter, requested);
614*85128b2bSAl Viro 	} else {
615*85128b2bSAl Viro 		result = requested;
616*85128b2bSAl Viro 	}
617d0b9875dSChristoph Hellwig 
618f11ac8dbSTrond Myklebust out_release:
619b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
620f11ac8dbSTrond Myklebust out:
6211da177e4SLinus Torvalds 	return result;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
624085d1e33STom Haynes static void
625085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
626085d1e33STom Haynes 				  struct list_head *list,
627085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
628085d1e33STom Haynes {
629fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
630085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
631085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
632085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
633085d1e33STom Haynes #endif
634085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
635fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
636085d1e33STom Haynes }
637085d1e33STom Haynes 
638fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
6391da177e4SLinus Torvalds {
6401763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
6411763da12SFred Isaman 	struct nfs_page *req, *tmp;
6421763da12SFred Isaman 	LIST_HEAD(reqs);
6431763da12SFred Isaman 	struct nfs_commit_info cinfo;
6441763da12SFred Isaman 	LIST_HEAD(failed);
6450a00b77bSWeston Andros Adamson 	int i;
6461763da12SFred Isaman 
6471763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
648085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
6491da177e4SLinus Torvalds 
650fad61490STrond Myklebust 	dreq->count = 0;
651a5314a74STrond Myklebust 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;
652a5314a74STrond Myklebust 	nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
6530a00b77bSWeston Andros Adamson 	for (i = 0; i < dreq->mirror_count; i++)
6540a00b77bSWeston Andros Adamson 		dreq->mirrors[i].count = 0;
655607f31e8STrond Myklebust 	get_dreq(dreq);
6561da177e4SLinus Torvalds 
657a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6581763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6591763da12SFred Isaman 	desc.pg_dreq = dreq;
660607f31e8STrond Myklebust 
6610a00b77bSWeston Andros Adamson 	req = nfs_list_entry(reqs.next);
6620a00b77bSWeston Andros Adamson 	nfs_direct_setup_mirroring(dreq, &desc, req);
663d600ad1fSPeng Tao 	if (desc.pg_error < 0) {
664d600ad1fSPeng Tao 		list_splice_init(&reqs, &failed);
665d600ad1fSPeng Tao 		goto out_failed;
666d600ad1fSPeng Tao 	}
6670a00b77bSWeston Andros Adamson 
6681763da12SFred Isaman 	list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
6691763da12SFred Isaman 		if (!nfs_pageio_add_request(&desc, req)) {
6704035c248STrond Myklebust 			nfs_list_remove_request(req);
6711763da12SFred Isaman 			nfs_list_add_request(req, &failed);
672fe238e60SDave Wysochanski 			spin_lock(&cinfo.inode->i_lock);
6731763da12SFred Isaman 			dreq->flags = 0;
674d600ad1fSPeng Tao 			if (desc.pg_error < 0)
675d600ad1fSPeng Tao 				dreq->error = desc.pg_error;
676d600ad1fSPeng Tao 			else
6771763da12SFred Isaman 				dreq->error = -EIO;
678fe238e60SDave Wysochanski 			spin_unlock(&cinfo.inode->i_lock);
6791763da12SFred Isaman 		}
6805a695da2STrond Myklebust 		nfs_release_request(req);
6811763da12SFred Isaman 	}
6821763da12SFred Isaman 	nfs_pageio_complete(&desc);
683607f31e8STrond Myklebust 
684d600ad1fSPeng Tao out_failed:
6854035c248STrond Myklebust 	while (!list_empty(&failed)) {
6864035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
6874035c248STrond Myklebust 		nfs_list_remove_request(req);
6881d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
6894035c248STrond Myklebust 	}
690607f31e8STrond Myklebust 
691607f31e8STrond Myklebust 	if (put_dreq(dreq))
6924d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
693fad61490STrond Myklebust }
6941da177e4SLinus Torvalds 
6951763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
696fad61490STrond Myklebust {
6970b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
6981763da12SFred Isaman 	struct nfs_commit_info cinfo;
6991763da12SFred Isaman 	struct nfs_page *req;
700c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
701c9d8f89dSTrond Myklebust 
7021763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
703c9d8f89dSTrond Myklebust 	if (status < 0) {
70460fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
705c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
706fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7075002c586SWeston Andros Adamson 	} else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
708c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
709fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
710fad61490STrond Myklebust 	}
711fad61490STrond Myklebust 
712c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
7131763da12SFred Isaman 	while (!list_empty(&data->pages)) {
7141763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
7151763da12SFred Isaman 		nfs_list_remove_request(req);
7161763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
7171763da12SFred Isaman 			/* Note the rewrite will go through mds */
718b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
719906369e4SFred Isaman 		} else
720906369e4SFred Isaman 			nfs_release_request(req);
7211d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
722fad61490STrond Myklebust 	}
723fad61490STrond Myklebust 
7241763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
7254d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
7261763da12SFred Isaman }
7271763da12SFred Isaman 
728b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
729b20135d0STrond Myklebust 		struct nfs_page *req)
7301763da12SFred Isaman {
731b20135d0STrond Myklebust 	struct nfs_direct_req *dreq = cinfo->dreq;
732b20135d0STrond Myklebust 
733b20135d0STrond Myklebust 	spin_lock(&dreq->lock);
734b20135d0STrond Myklebust 	dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
735b20135d0STrond Myklebust 	spin_unlock(&dreq->lock);
736b20135d0STrond Myklebust 	nfs_mark_request_commit(req, NULL, cinfo, 0);
7371763da12SFred Isaman }
7381763da12SFred Isaman 
7391763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
7401763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
741b20135d0STrond Myklebust 	.resched_write = nfs_direct_resched_write,
742fad61490STrond Myklebust };
743fad61490STrond Myklebust 
744fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
745fad61490STrond Myklebust {
7461763da12SFred Isaman 	int res;
7471763da12SFred Isaman 	struct nfs_commit_info cinfo;
7481763da12SFred Isaman 	LIST_HEAD(mds_list);
749fad61490STrond Myklebust 
7501763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7511763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
7521763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
7531763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
7541763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
7581da177e4SLinus Torvalds {
7591763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
760fad61490STrond Myklebust 	int flags = dreq->flags;
7611da177e4SLinus Torvalds 
762fad61490STrond Myklebust 	dreq->flags = 0;
763fad61490STrond Myklebust 	switch (flags) {
764fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
765fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
7661da177e4SLinus Torvalds 			break;
767fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
768fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
7691da177e4SLinus Torvalds 			break;
7701da177e4SLinus Torvalds 		default:
771f7b5c340STrond Myklebust 			nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
772f7b5c340STrond Myklebust 			nfs_direct_complete(dreq);
7731da177e4SLinus Torvalds 	}
774fad61490STrond Myklebust }
775fad61490STrond Myklebust 
7764d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
777fad61490STrond Myklebust {
7781763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
779fad61490STrond Myklebust }
7801763da12SFred Isaman 
7811763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7821763da12SFred Isaman {
7831763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7841763da12SFred Isaman 	struct nfs_commit_info cinfo;
785c65e6254SWeston Andros Adamson 	bool request_commit = false;
7861763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7871763da12SFred Isaman 
7881763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
7891763da12SFred Isaman 		goto out_put;
7901763da12SFred Isaman 
7911763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7921763da12SFred Isaman 
7931763da12SFred Isaman 	spin_lock(&dreq->lock);
7941763da12SFred Isaman 
7951763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7961763da12SFred Isaman 		dreq->flags = 0;
7971763da12SFred Isaman 		dreq->error = hdr->error;
7981763da12SFred Isaman 	}
799c65e6254SWeston Andros Adamson 	if (dreq->error == 0) {
8000a00b77bSWeston Andros Adamson 		nfs_direct_good_bytes(dreq, hdr);
801c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
8021763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
803c65e6254SWeston Andros Adamson 				request_commit = true;
8041763da12SFred Isaman 			else if (dreq->flags == 0) {
8055002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
806c65e6254SWeston Andros Adamson 				request_commit = true;
8071763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
8081763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
809c65e6254SWeston Andros Adamson 				request_commit = true;
810c65e6254SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
8115002c586SWeston Andros Adamson 					dreq->flags =
8125002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
8131763da12SFred Isaman 			}
8141763da12SFred Isaman 		}
8151763da12SFred Isaman 	}
8161763da12SFred Isaman 	spin_unlock(&dreq->lock);
8171763da12SFred Isaman 
8181763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
8192bfc6e56SWeston Andros Adamson 
8201763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
8211763da12SFred Isaman 		nfs_list_remove_request(req);
822c65e6254SWeston Andros Adamson 		if (request_commit) {
82304277086STrond Myklebust 			kref_get(&req->wb_kref);
824b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
825b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
8261763da12SFred Isaman 		}
8271d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8281763da12SFred Isaman 	}
8291763da12SFred Isaman 
8301763da12SFred Isaman out_put:
8311763da12SFred Isaman 	if (put_dreq(dreq))
8324d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
8331763da12SFred Isaman 	hdr->release(hdr);
8341763da12SFred Isaman }
8351763da12SFred Isaman 
8363e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
8373e9e0ca3STrond Myklebust {
8383e9e0ca3STrond Myklebust 	struct nfs_page *req;
8393e9e0ca3STrond Myklebust 
8403e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
8413e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
8423e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
8431d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8443e9e0ca3STrond Myklebust 	}
8453e9e0ca3STrond Myklebust }
8463e9e0ca3STrond Myklebust 
847dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
848dc602dd7STrond Myklebust {
849dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
850dc602dd7STrond Myklebust 
851dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
852dc602dd7STrond Myklebust 	if (dreq->error == 0) {
853dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
854dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
855dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
856dc602dd7STrond Myklebust 		hdr->good_bytes = hdr->args.count;
857dc602dd7STrond Myklebust 	}
858dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
859dc602dd7STrond Myklebust }
860dc602dd7STrond Myklebust 
8611763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
8623e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
8631763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
8641763da12SFred Isaman 	.completion = nfs_direct_write_completion,
865dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
8661763da12SFred Isaman };
8671763da12SFred Isaman 
86891f79c43SAl Viro 
86991f79c43SAl Viro /*
87091f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
87191f79c43SAl Viro  *     errors after the first one are ignored.
87291f79c43SAl Viro  */
87391f79c43SAl Viro /*
87491f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
87591f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
87691f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
87791f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
87891f79c43SAl Viro  * no requests have been sent, just return an error.
87991f79c43SAl Viro  */
88019f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
881619d30b4SAl Viro 					       struct iov_iter *iter,
88291f79c43SAl Viro 					       loff_t pos)
88319f73787SChuck Lever {
8841763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8851d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
88619f73787SChuck Lever 	ssize_t result = 0;
88719f73787SChuck Lever 	size_t requested_bytes = 0;
88891f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
88919f73787SChuck Lever 
890a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
8911763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8921763da12SFred Isaman 	desc.pg_dreq = dreq;
89319f73787SChuck Lever 	get_dreq(dreq);
894fe0f07d0SJens Axboe 	inode_dio_begin(inode);
89519f73787SChuck Lever 
89691f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
89791f79c43SAl Viro 	while (iov_iter_count(iter)) {
89891f79c43SAl Viro 		struct page **pagevec;
89991f79c43SAl Viro 		size_t bytes;
90091f79c43SAl Viro 		size_t pgbase;
90191f79c43SAl Viro 		unsigned npages, i;
90291f79c43SAl Viro 
90391f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
90491f79c43SAl Viro 						  wsize, &pgbase);
90519f73787SChuck Lever 		if (result < 0)
90619f73787SChuck Lever 			break;
90791f79c43SAl Viro 
90891f79c43SAl Viro 		bytes = result;
90991f79c43SAl Viro 		iov_iter_advance(iter, bytes);
91091f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
91191f79c43SAl Viro 		for (i = 0; i < npages; i++) {
91291f79c43SAl Viro 			struct nfs_page *req;
91391f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
91491f79c43SAl Viro 
91516b90578SLinus Torvalds 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
91691f79c43SAl Viro 						 pgbase, req_len);
91791f79c43SAl Viro 			if (IS_ERR(req)) {
91891f79c43SAl Viro 				result = PTR_ERR(req);
91919f73787SChuck Lever 				break;
92091f79c43SAl Viro 			}
9210a00b77bSWeston Andros Adamson 
9220a00b77bSWeston Andros Adamson 			nfs_direct_setup_mirroring(dreq, &desc, req);
923d600ad1fSPeng Tao 			if (desc.pg_error < 0) {
924d600ad1fSPeng Tao 				nfs_free_request(req);
925d600ad1fSPeng Tao 				result = desc.pg_error;
926d600ad1fSPeng Tao 				break;
927d600ad1fSPeng Tao 			}
9280a00b77bSWeston Andros Adamson 
92991f79c43SAl Viro 			nfs_lock_request(req);
93091f79c43SAl Viro 			req->wb_index = pos >> PAGE_SHIFT;
93191f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
93291f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
93391f79c43SAl Viro 				result = desc.pg_error;
93491f79c43SAl Viro 				nfs_unlock_and_release_request(req);
93591f79c43SAl Viro 				break;
93691f79c43SAl Viro 			}
93791f79c43SAl Viro 			pgbase = 0;
93891f79c43SAl Viro 			bytes -= req_len;
93991f79c43SAl Viro 			requested_bytes += req_len;
94091f79c43SAl Viro 			pos += req_len;
94191f79c43SAl Viro 			dreq->bytes_left -= req_len;
94291f79c43SAl Viro 		}
94391f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
94491f79c43SAl Viro 		kvfree(pagevec);
94591f79c43SAl Viro 		if (result < 0)
94691f79c43SAl Viro 			break;
94719f73787SChuck Lever 	}
9481763da12SFred Isaman 	nfs_pageio_complete(&desc);
94919f73787SChuck Lever 
950839f7ad6SChuck Lever 	/*
951839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
952839f7ad6SChuck Lever 	 * generic layer handle the completion.
953839f7ad6SChuck Lever 	 */
954839f7ad6SChuck Lever 	if (requested_bytes == 0) {
955fe0f07d0SJens Axboe 		inode_dio_end(inode);
956839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
957839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
958839f7ad6SChuck Lever 	}
959839f7ad6SChuck Lever 
96019f73787SChuck Lever 	if (put_dreq(dreq))
9614d3b55d3SAnna Schumaker 		nfs_direct_write_complete(dreq);
962*85128b2bSAl Viro 	return requested_bytes;
96319f73787SChuck Lever }
96419f73787SChuck Lever 
9651da177e4SLinus Torvalds /**
9661da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9671da177e4SLinus Torvalds  * @iocb: target I/O control block
968619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
9691da177e4SLinus Torvalds  *
9701da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9711da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9721da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9731da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9741da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9751da177e4SLinus Torvalds  * parameter checking and report problems.
9761da177e4SLinus Torvalds  *
9771da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9781da177e4SLinus Torvalds  *
9791da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9801da177e4SLinus Torvalds  * readers of this file.
9811da177e4SLinus Torvalds  *
9821da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9831da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9841da177e4SLinus Torvalds  */
98565a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
9861da177e4SLinus Torvalds {
987*85128b2bSAl Viro 	ssize_t result = -EINVAL, requested;
98889698b24STrond Myklebust 	size_t count;
9891da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9901da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
99122cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
99222cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
99322cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
99465a4a1caSAl Viro 	loff_t pos, end;
995c216fd70SChuck Lever 
9966de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
9973309dd04SAl Viro 		file, iov_iter_count(iter), (long long) iocb->ki_pos);
998027445c3SBadari Pulavarty 
99989698b24STrond Myklebust 	result = generic_write_checks(iocb, iter);
100089698b24STrond Myklebust 	if (result <= 0)
100189698b24STrond Myklebust 		return result;
100289698b24STrond Myklebust 	count = result;
100389698b24STrond Myklebust 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
10043309dd04SAl Viro 
10053309dd04SAl Viro 	pos = iocb->ki_pos;
100609cbfeafSKirill A. Shutemov 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
1007ce1a8e67SChuck Lever 
100889698b24STrond Myklebust 	task_io_account_write(count);
10097ec10f26SKonstantin Khlebnikov 
101022cd1bf1SChristoph Hellwig 	result = -ENOMEM;
101122cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
101222cd1bf1SChristoph Hellwig 	if (!dreq)
1013a5864c99STrond Myklebust 		goto out;
101422cd1bf1SChristoph Hellwig 
101522cd1bf1SChristoph Hellwig 	dreq->inode = inode;
101689698b24STrond Myklebust 	dreq->bytes_left = dreq->max_count = count;
10175fadeb47SPeng Tao 	dreq->io_start = pos;
101822cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
101922cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
102022cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
102122cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
102222cd1bf1SChristoph Hellwig 		goto out_release;
102322cd1bf1SChristoph Hellwig 	}
102422cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
102522cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
102622cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
102722cd1bf1SChristoph Hellwig 
1028a5864c99STrond Myklebust 	nfs_start_io_direct(inode);
1029a5864c99STrond Myklebust 
1030*85128b2bSAl Viro 	requested = nfs_direct_write_schedule_iovec(dreq, iter, pos);
1031a9ab5e84SChristoph Hellwig 
1032a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1033a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
103409cbfeafSKirill A. Shutemov 					      pos >> PAGE_SHIFT, end);
1035a9ab5e84SChristoph Hellwig 	}
1036a9ab5e84SChristoph Hellwig 
1037a5864c99STrond Myklebust 	nfs_end_io_direct(inode);
1038a9ab5e84SChristoph Hellwig 
1039*85128b2bSAl Viro 	if (requested > 0) {
104022cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
104122cd1bf1SChristoph Hellwig 		if (result > 0) {
1042*85128b2bSAl Viro 			requested -= result;
104322cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
1044e2592217SChristoph Hellwig 			/* XXX: should check the generic_write_sync retval */
1045e2592217SChristoph Hellwig 			generic_write_sync(iocb, result);
10461763da12SFred Isaman 		}
1047*85128b2bSAl Viro 		iov_iter_revert(iter, requested);
1048*85128b2bSAl Viro 	} else {
1049*85128b2bSAl Viro 		result = requested;
105022cd1bf1SChristoph Hellwig 	}
105122cd1bf1SChristoph Hellwig out_release:
105222cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
1053a5864c99STrond Myklebust out:
105422cd1bf1SChristoph Hellwig 	return result;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
105788467055SChuck Lever /**
105888467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
105988467055SChuck Lever  *
106088467055SChuck Lever  */
1061f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10641da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1065fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1066fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
106720c2df83SPaul Mundt 						NULL);
10681da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10691da177e4SLinus Torvalds 		return -ENOMEM;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	return 0;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
107488467055SChuck Lever /**
1075f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
107688467055SChuck Lever  *
107788467055SChuck Lever  */
1078266bee88SDavid Brownell void nfs_destroy_directcache(void)
10791da177e4SLinus Torvalds {
10801a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10811da177e4SLinus Torvalds }
1082