xref: /openbmc/linux/fs/nfs/direct.c (revision dc602dd706cb64036132a7903ead1c67d9a7bcb9)
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  */
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 */
9035754bc0SPeng Tao 				bytes_left,	/* bytes left to be sent */
915fadeb47SPeng Tao 				io_start,	/* start of IO */
921da177e4SLinus Torvalds 				error;		/* any reported error */
93d72b7a6bSTrond Myklebust 	struct completion	completion;	/* wait for i/o completion */
94fad61490STrond Myklebust 
95fad61490STrond Myklebust 	/* commit state */
961763da12SFred Isaman 	struct nfs_mds_commit_info mds_cinfo;	/* Storage for cinfo */
971763da12SFred Isaman 	struct pnfs_ds_commit_info ds_cinfo;	/* Storage for cinfo */
981763da12SFred Isaman 	struct work_struct	work;
99fad61490STrond Myklebust 	int			flags;
100fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT		(1)	/* an unstable reply was received */
101fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES	(2)	/* write verification failed */
102fad61490STrond Myklebust 	struct nfs_writeverf	verf;		/* unstable write verifier */
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
1061763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
107fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
1081763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work);
109607f31e8STrond Myklebust 
110607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq)
111607f31e8STrond Myklebust {
112607f31e8STrond Myklebust 	atomic_inc(&dreq->io_count);
113607f31e8STrond Myklebust }
114607f31e8STrond Myklebust 
115607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq)
116607f31e8STrond Myklebust {
117607f31e8STrond Myklebust 	return atomic_dec_and_test(&dreq->io_count);
118607f31e8STrond Myklebust }
119607f31e8STrond Myklebust 
1200a00b77bSWeston Andros Adamson static void
1210a00b77bSWeston Andros Adamson nfs_direct_good_bytes(struct nfs_direct_req *dreq, struct nfs_pgio_header *hdr)
1220a00b77bSWeston Andros Adamson {
1230a00b77bSWeston Andros Adamson 	int i;
1240a00b77bSWeston Andros Adamson 	ssize_t count;
1250a00b77bSWeston Andros Adamson 
1261ccbad9fSPeng Tao 	if (dreq->mirror_count == 1) {
1271ccbad9fSPeng Tao 		dreq->mirrors[hdr->pgio_mirror_idx].count += hdr->good_bytes;
1281ccbad9fSPeng Tao 		dreq->count += hdr->good_bytes;
1291ccbad9fSPeng Tao 	} else {
1301ccbad9fSPeng Tao 		/* mirrored writes */
1315fadeb47SPeng Tao 		count = dreq->mirrors[hdr->pgio_mirror_idx].count;
1325fadeb47SPeng Tao 		if (count + dreq->io_start < hdr->io_start + hdr->good_bytes) {
1335fadeb47SPeng Tao 			count = hdr->io_start + hdr->good_bytes - dreq->io_start;
1345fadeb47SPeng Tao 			dreq->mirrors[hdr->pgio_mirror_idx].count = count;
1355fadeb47SPeng Tao 		}
1360a00b77bSWeston Andros Adamson 		/* update the dreq->count by finding the minimum agreed count from all
1370a00b77bSWeston Andros Adamson 		 * mirrors */
1380a00b77bSWeston Andros Adamson 		count = dreq->mirrors[0].count;
1390a00b77bSWeston Andros Adamson 
1400a00b77bSWeston Andros Adamson 		for (i = 1; i < dreq->mirror_count; i++)
1410a00b77bSWeston Andros Adamson 			count = min(count, dreq->mirrors[i].count);
1420a00b77bSWeston Andros Adamson 
1430a00b77bSWeston Andros Adamson 		dreq->count = count;
1440a00b77bSWeston Andros Adamson 	}
1451ccbad9fSPeng Tao }
1460a00b77bSWeston Andros Adamson 
1475002c586SWeston Andros Adamson /*
1485002c586SWeston Andros Adamson  * nfs_direct_select_verf - select the right verifier
1495002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1505002c586SWeston Andros Adamson  * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
1516cccbb6fSWeston Andros Adamson  * @commit_idx - commit bucket index for the DS
1525002c586SWeston Andros Adamson  *
1535002c586SWeston Andros Adamson  * returns the correct verifier to use given the role of the server
1545002c586SWeston Andros Adamson  */
1555002c586SWeston Andros Adamson static struct nfs_writeverf *
1565002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq,
1575002c586SWeston Andros Adamson 		       struct nfs_client *ds_clp,
1586cccbb6fSWeston Andros Adamson 		       int commit_idx)
1595002c586SWeston Andros Adamson {
1605002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp = &dreq->verf;
1615002c586SWeston Andros Adamson 
1625002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1
163834e465bSKinglong Mee 	/*
164834e465bSKinglong Mee 	 * pNFS is in use, use the DS verf except commit_through_mds is set
165834e465bSKinglong Mee 	 * for layout segment where nbuckets is zero.
166834e465bSKinglong Mee 	 */
167834e465bSKinglong Mee 	if (ds_clp && dreq->ds_cinfo.nbuckets > 0) {
1686cccbb6fSWeston Andros Adamson 		if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
1696cccbb6fSWeston Andros Adamson 			verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
1705002c586SWeston Andros Adamson 		else
1715002c586SWeston Andros Adamson 			WARN_ON_ONCE(1);
1725002c586SWeston Andros Adamson 	}
1735002c586SWeston Andros Adamson #endif
1745002c586SWeston Andros Adamson 	return verfp;
1755002c586SWeston Andros Adamson }
1765002c586SWeston Andros Adamson 
1775002c586SWeston Andros Adamson 
1785002c586SWeston Andros Adamson /*
1795002c586SWeston Andros Adamson  * nfs_direct_set_hdr_verf - set the write/commit verifier
1805002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1815002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verfs
1825002c586SWeston Andros Adamson  *
1835002c586SWeston Andros Adamson  * Set the server's (MDS or DS) "seen" verifier
1845002c586SWeston Andros Adamson  */
1855002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
1865002c586SWeston Andros Adamson 				    struct nfs_pgio_header *hdr)
1875002c586SWeston Andros Adamson {
1885002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
1895002c586SWeston Andros Adamson 
1906cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
1915002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed >= 0);
1925002c586SWeston Andros Adamson 	memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
1935002c586SWeston Andros Adamson 	WARN_ON_ONCE(verfp->committed < 0);
1945002c586SWeston Andros Adamson }
1955002c586SWeston Andros Adamson 
1965002c586SWeston Andros Adamson /*
1975002c586SWeston Andros Adamson  * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
1985002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
1995002c586SWeston Andros Adamson  * @hdr - pageio header to validate against previously seen verf
2005002c586SWeston Andros Adamson  *
2015002c586SWeston Andros Adamson  * set the server's "seen" verf if not initialized.
2025002c586SWeston Andros Adamson  * returns result of comparison between @hdr->verf and the "seen"
2035002c586SWeston Andros Adamson  * verf of the server used by @hdr (DS or MDS)
2045002c586SWeston Andros Adamson  */
2055002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
2065002c586SWeston Andros Adamson 					  struct nfs_pgio_header *hdr)
2075002c586SWeston Andros Adamson {
2085002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2095002c586SWeston Andros Adamson 
2106cccbb6fSWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
2115002c586SWeston Andros Adamson 	if (verfp->committed < 0) {
2125002c586SWeston Andros Adamson 		nfs_direct_set_hdr_verf(dreq, hdr);
2135002c586SWeston Andros Adamson 		return 0;
2145002c586SWeston Andros Adamson 	}
2155002c586SWeston Andros Adamson 	return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
2165002c586SWeston Andros Adamson }
2175002c586SWeston Andros Adamson 
2185002c586SWeston Andros Adamson /*
2195002c586SWeston Andros Adamson  * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
2205002c586SWeston Andros Adamson  * @dreq - direct request possibly spanning multiple servers
2215002c586SWeston Andros Adamson  * @data - commit data to validate against previously seen verf
2225002c586SWeston Andros Adamson  *
2235002c586SWeston Andros Adamson  * returns result of comparison between @data->verf and the verf of
2245002c586SWeston Andros Adamson  * the server used by @data (DS or MDS)
2255002c586SWeston Andros Adamson  */
2265002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
2275002c586SWeston Andros Adamson 					   struct nfs_commit_data *data)
2285002c586SWeston Andros Adamson {
2295002c586SWeston Andros Adamson 	struct nfs_writeverf *verfp;
2305002c586SWeston Andros Adamson 
2315002c586SWeston Andros Adamson 	verfp = nfs_direct_select_verf(dreq, data->ds_clp,
2325002c586SWeston Andros Adamson 					 data->ds_commit_index);
23380c76fe3SWeston Andros Adamson 
23480c76fe3SWeston Andros Adamson 	/* verifier not set so always fail */
23580c76fe3SWeston Andros Adamson 	if (verfp->committed < 0)
23680c76fe3SWeston Andros Adamson 		return 1;
23780c76fe3SWeston Andros Adamson 
2385002c586SWeston Andros Adamson 	return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf));
2395002c586SWeston Andros Adamson }
2405002c586SWeston Andros Adamson 
2411da177e4SLinus Torvalds /**
242b8a32e2bSChuck Lever  * nfs_direct_IO - NFS address space operation for direct I/O
243b8a32e2bSChuck Lever  * @iocb: target I/O control block
244b8a32e2bSChuck Lever  * @iov: array of vectors that define I/O buffer
245b8a32e2bSChuck Lever  * @pos: offset in file to begin the operation
246b8a32e2bSChuck Lever  * @nr_segs: size of iovec array
247b8a32e2bSChuck Lever  *
248b8a32e2bSChuck Lever  * The presence of this routine in the address space ops vector means
249a564b8f0SMel Gorman  * the NFS client supports direct I/O. However, for most direct IO, we
250a564b8f0SMel Gorman  * shunt off direct read and write requests before the VFS gets them,
251a564b8f0SMel Gorman  * so this method is only ever called for swap.
2521da177e4SLinus Torvalds  */
25322c6186eSOmar Sandoval ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
254b8a32e2bSChuck Lever {
255ee8a1a8bSPeng Tao 	struct inode *inode = iocb->ki_filp->f_mapping->host;
256ee8a1a8bSPeng Tao 
257ee8a1a8bSPeng Tao 	/* we only support swap file calling nfs_direct_IO */
258ee8a1a8bSPeng Tao 	if (!IS_SWAPFILE(inode))
259ee8a1a8bSPeng Tao 		return 0;
260ee8a1a8bSPeng Tao 
26166ee59afSChristoph Hellwig 	VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
262a564b8f0SMel Gorman 
2636f673763SOmar Sandoval 	if (iov_iter_rw(iter) == READ)
264e19a8a0aSMartin K. Petersen 		return nfs_file_direct_read(iocb, iter, pos);
26565a4a1caSAl Viro 	return nfs_file_direct_write(iocb, iter);
266b8a32e2bSChuck Lever }
267b8a32e2bSChuck Lever 
268749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
2699c93ab7dSChuck Lever {
270749e146eSChuck Lever 	unsigned int i;
271607f31e8STrond Myklebust 	for (i = 0; i < npages; i++)
272607f31e8STrond Myklebust 		page_cache_release(pages[i]);
2736b45d858STrond Myklebust }
2746b45d858STrond Myklebust 
2751763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
2761763da12SFred Isaman 			      struct nfs_direct_req *dreq)
2771763da12SFred Isaman {
278f4086a3dSTrond Myklebust 	cinfo->lock = &dreq->inode->i_lock;
2791763da12SFred Isaman 	cinfo->mds = &dreq->mds_cinfo;
2801763da12SFred Isaman 	cinfo->ds = &dreq->ds_cinfo;
2811763da12SFred Isaman 	cinfo->dreq = dreq;
2821763da12SFred Isaman 	cinfo->completion_ops = &nfs_direct_commit_completion_ops;
2831763da12SFred Isaman }
2841763da12SFred Isaman 
2850a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq,
2860a00b77bSWeston Andros Adamson 					     struct nfs_pageio_descriptor *pgio,
2870a00b77bSWeston Andros Adamson 					     struct nfs_page *req)
2880a00b77bSWeston Andros Adamson {
2890a00b77bSWeston Andros Adamson 	int mirror_count = 1;
2900a00b77bSWeston Andros Adamson 
2910a00b77bSWeston Andros Adamson 	if (pgio->pg_ops->pg_get_mirror_count)
2920a00b77bSWeston Andros Adamson 		mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
2930a00b77bSWeston Andros Adamson 
2940a00b77bSWeston Andros Adamson 	dreq->mirror_count = mirror_count;
2950a00b77bSWeston Andros Adamson }
2960a00b77bSWeston Andros Adamson 
29793619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
3001da177e4SLinus Torvalds 
301292f3eeeSTrond Myklebust 	dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
3021da177e4SLinus Torvalds 	if (!dreq)
3031da177e4SLinus Torvalds 		return NULL;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	kref_init(&dreq->kref);
306607f31e8STrond Myklebust 	kref_get(&dreq->kref);
307d72b7a6bSTrond Myklebust 	init_completion(&dreq->completion);
3081763da12SFred Isaman 	INIT_LIST_HEAD(&dreq->mds_cinfo.list);
3095002c586SWeston Andros Adamson 	dreq->verf.committed = NFS_INVALID_STABLE_HOW;	/* not set yet */
3101763da12SFred Isaman 	INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
3110a00b77bSWeston Andros Adamson 	dreq->mirror_count = 1;
31215ce4a0cSChuck Lever 	spin_lock_init(&dreq->lock);
31393619e59SChuck Lever 
31493619e59SChuck Lever 	return dreq;
31593619e59SChuck Lever }
31693619e59SChuck Lever 
317b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
320a8881f5aSTrond Myklebust 
3218c393f9aSPeng Tao 	nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
322f11ac8dbSTrond Myklebust 	if (dreq->l_ctx != NULL)
323f11ac8dbSTrond Myklebust 		nfs_put_lock_context(dreq->l_ctx);
324a8881f5aSTrond Myklebust 	if (dreq->ctx != NULL)
325a8881f5aSTrond Myklebust 		put_nfs_open_context(dreq->ctx);
3261da177e4SLinus Torvalds 	kmem_cache_free(nfs_direct_cachep, dreq);
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq)
330b4946ffbSTrond Myklebust {
331b4946ffbSTrond Myklebust 	kref_put(&dreq->kref, nfs_direct_req_free);
332b4946ffbSTrond Myklebust }
333b4946ffbSTrond Myklebust 
3346296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
3356296556fSPeng Tao {
3366296556fSPeng Tao 	return dreq->bytes_left;
3376296556fSPeng Tao }
3386296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
3396296556fSPeng Tao 
340d4cc948bSChuck Lever /*
341bc0fb201SChuck Lever  * Collects and returns the final error value/byte-count.
342bc0fb201SChuck Lever  */
343bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
344bc0fb201SChuck Lever {
34515ce4a0cSChuck Lever 	ssize_t result = -EIOCBQUEUED;
346bc0fb201SChuck Lever 
347bc0fb201SChuck Lever 	/* Async requests don't wait here */
348bc0fb201SChuck Lever 	if (dreq->iocb)
349bc0fb201SChuck Lever 		goto out;
350bc0fb201SChuck Lever 
351150030b7SMatthew Wilcox 	result = wait_for_completion_killable(&dreq->completion);
352bc0fb201SChuck Lever 
353bc0fb201SChuck Lever 	if (!result)
35415ce4a0cSChuck Lever 		result = dreq->error;
355bc0fb201SChuck Lever 	if (!result)
35615ce4a0cSChuck Lever 		result = dreq->count;
357bc0fb201SChuck Lever 
358bc0fb201SChuck Lever out:
359bc0fb201SChuck Lever 	return (ssize_t) result;
360bc0fb201SChuck Lever }
361bc0fb201SChuck Lever 
362bc0fb201SChuck Lever /*
363607f31e8STrond Myklebust  * Synchronous I/O uses a stack-allocated iocb.  Thus we can't trust
364607f31e8STrond Myklebust  * the iocb is still valid here if this is a synchronous request.
36563ab46abSChuck Lever  */
3669811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
36763ab46abSChuck Lever {
3689811cd57SChristoph Hellwig 	struct inode *inode = dreq->inode;
3699811cd57SChristoph Hellwig 
3702a009ec9SChristoph Hellwig 	if (dreq->iocb && write) {
3719811cd57SChristoph Hellwig 		loff_t pos = dreq->iocb->ki_pos + dreq->count;
3729811cd57SChristoph Hellwig 
3739811cd57SChristoph Hellwig 		spin_lock(&inode->i_lock);
3749811cd57SChristoph Hellwig 		if (i_size_read(inode) < pos)
3759811cd57SChristoph Hellwig 			i_size_write(inode, pos);
3769811cd57SChristoph Hellwig 		spin_unlock(&inode->i_lock);
3779811cd57SChristoph Hellwig 	}
3789811cd57SChristoph Hellwig 
3791f90ee27SChristoph Hellwig 	if (write)
3802a009ec9SChristoph Hellwig 		nfs_zap_mapping(inode, inode->i_mapping);
3811f90ee27SChristoph Hellwig 
382fe0f07d0SJens Axboe 	inode_dio_end(inode);
3832a009ec9SChristoph Hellwig 
3842a009ec9SChristoph Hellwig 	if (dreq->iocb) {
3852a009ec9SChristoph Hellwig 		long res = (long) dreq->error;
3862a009ec9SChristoph Hellwig 		if (!res)
3872a009ec9SChristoph Hellwig 			res = (long) dreq->count;
38804b2fa9fSChristoph Hellwig 		dreq->iocb->ki_complete(dreq->iocb, res, 0);
389d72b7a6bSTrond Myklebust 	}
3902a009ec9SChristoph Hellwig 
391d72b7a6bSTrond Myklebust 	complete_all(&dreq->completion);
39263ab46abSChuck Lever 
393b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
39463ab46abSChuck Lever }
39563ab46abSChuck Lever 
3961385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req)
3971da177e4SLinus Torvalds {
3981e8968c5SNiels de Vos 	dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
3992b0143b5SDavid Howells 		d_inode(req->wb_context->dentry)->i_sb->s_id,
4002b0143b5SDavid Howells 		(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
401584aa810SFred Isaman 		req->wb_bytes,
402584aa810SFred Isaman 		(long long)req_offset(req));
403584aa810SFred Isaman 	nfs_release_request(req);
404fdd1e74cSTrond Myklebust }
405fdd1e74cSTrond Myklebust 
406584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
407fdd1e74cSTrond Myklebust {
408584aa810SFred Isaman 	unsigned long bytes = 0;
409584aa810SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
410fdd1e74cSTrond Myklebust 
411584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
412584aa810SFred Isaman 		goto out_put;
4131da177e4SLinus Torvalds 
41415ce4a0cSChuck Lever 	spin_lock(&dreq->lock);
415584aa810SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
416584aa810SFred Isaman 		dreq->error = hdr->error;
417584aa810SFred Isaman 	else
4180a00b77bSWeston Andros Adamson 		nfs_direct_good_bytes(dreq, hdr);
4190a00b77bSWeston Andros Adamson 
42015ce4a0cSChuck Lever 	spin_unlock(&dreq->lock);
4211da177e4SLinus Torvalds 
422584aa810SFred Isaman 	while (!list_empty(&hdr->pages)) {
423584aa810SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
424584aa810SFred Isaman 		struct page *page = req->wb_page;
425584aa810SFred Isaman 
426be7e9858SJeff Layton 		if (!PageCompound(page) && bytes < hdr->good_bytes)
4274bd8b010STrond Myklebust 			set_page_dirty(page);
428584aa810SFred Isaman 		bytes += req->wb_bytes;
429584aa810SFred Isaman 		nfs_list_remove_request(req);
430584aa810SFred Isaman 		nfs_direct_readpage_release(req);
431584aa810SFred Isaman 	}
432584aa810SFred Isaman out_put:
433607f31e8STrond Myklebust 	if (put_dreq(dreq))
4349811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
435584aa810SFred Isaman 	hdr->release(hdr);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4383e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head)
439cd841605SFred Isaman {
440584aa810SFred Isaman 	struct nfs_page *req;
441cd841605SFred Isaman 
442584aa810SFred Isaman 	while (!list_empty(head)) {
443584aa810SFred Isaman 		req = nfs_list_entry(head->next);
444584aa810SFred Isaman 		nfs_list_remove_request(req);
445584aa810SFred Isaman 		nfs_release_request(req);
446cd841605SFred Isaman 	}
447584aa810SFred Isaman }
448584aa810SFred Isaman 
449584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
450584aa810SFred Isaman {
451584aa810SFred Isaman 	get_dreq(hdr->dreq);
452584aa810SFred Isaman }
453584aa810SFred Isaman 
454584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
4553e9e0ca3STrond Myklebust 	.error_cleanup = nfs_read_sync_pgio_error,
456584aa810SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
457584aa810SFred Isaman 	.completion = nfs_direct_read_completion,
458584aa810SFred Isaman };
459cd841605SFred Isaman 
460d4cc948bSChuck Lever /*
461607f31e8STrond Myklebust  * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
462607f31e8STrond Myklebust  * operation.  If nfs_readdata_alloc() or get_user_pages() fails,
463607f31e8STrond Myklebust  * bail and stop sending more reads.  Read length accounting is
464607f31e8STrond Myklebust  * handled automatically by nfs_direct_read_result().  Otherwise, if
465607f31e8STrond Myklebust  * no requests have been sent, just return an error.
4661da177e4SLinus Torvalds  */
46791f79c43SAl Viro 
46891f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
46991f79c43SAl Viro 					      struct iov_iter *iter,
47091f79c43SAl Viro 					      loff_t pos)
4711da177e4SLinus Torvalds {
47291f79c43SAl Viro 	struct nfs_pageio_descriptor desc;
47391f79c43SAl Viro 	struct inode *inode = dreq->inode;
47491f79c43SAl Viro 	ssize_t result = -EINVAL;
47591f79c43SAl Viro 	size_t requested_bytes = 0;
47691f79c43SAl Viro 	size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
47782b145c5SChuck Lever 
47816b90578SLinus Torvalds 	nfs_pageio_init_read(&desc, dreq->inode, false,
47991f79c43SAl Viro 			     &nfs_direct_read_completion_ops);
48091f79c43SAl Viro 	get_dreq(dreq);
48191f79c43SAl Viro 	desc.pg_dreq = dreq;
482fe0f07d0SJens Axboe 	inode_dio_begin(inode);
48391f79c43SAl Viro 
48491f79c43SAl Viro 	while (iov_iter_count(iter)) {
48591f79c43SAl Viro 		struct page **pagevec;
4865dd602f2SChuck Lever 		size_t bytes;
48791f79c43SAl Viro 		size_t pgbase;
48891f79c43SAl Viro 		unsigned npages, i;
4891da177e4SLinus Torvalds 
49091f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
49191f79c43SAl Viro 						  rsize, &pgbase);
492584aa810SFred Isaman 		if (result < 0)
493749e146eSChuck Lever 			break;
494a564b8f0SMel Gorman 
49591f79c43SAl Viro 		bytes = result;
49691f79c43SAl Viro 		iov_iter_advance(iter, bytes);
49791f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
498584aa810SFred Isaman 		for (i = 0; i < npages; i++) {
499584aa810SFred Isaman 			struct nfs_page *req;
500bf5fc402STrond Myklebust 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
501584aa810SFred Isaman 			/* XXX do we need to do the eof zeroing found in async_filler? */
5022bfc6e56SWeston Andros Adamson 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
503584aa810SFred Isaman 						 pgbase, req_len);
504584aa810SFred Isaman 			if (IS_ERR(req)) {
505584aa810SFred Isaman 				result = PTR_ERR(req);
506dbae4c73STrond Myklebust 				break;
507584aa810SFred Isaman 			}
508584aa810SFred Isaman 			req->wb_index = pos >> PAGE_SHIFT;
509584aa810SFred Isaman 			req->wb_offset = pos & ~PAGE_MASK;
51091f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
51191f79c43SAl Viro 				result = desc.pg_error;
512584aa810SFred Isaman 				nfs_release_request(req);
513584aa810SFred Isaman 				break;
514584aa810SFred Isaman 			}
515584aa810SFred Isaman 			pgbase = 0;
516584aa810SFred Isaman 			bytes -= req_len;
51791f79c43SAl Viro 			requested_bytes += req_len;
518584aa810SFred Isaman 			pos += req_len;
51935754bc0SPeng Tao 			dreq->bytes_left -= req_len;
520584aa810SFred Isaman 		}
5216d74743bSTrond Myklebust 		nfs_direct_release_pages(pagevec, npages);
52291f79c43SAl Viro 		kvfree(pagevec);
52319f73787SChuck Lever 		if (result < 0)
52419f73787SChuck Lever 			break;
52519f73787SChuck Lever 	}
52619f73787SChuck Lever 
527584aa810SFred Isaman 	nfs_pageio_complete(&desc);
528584aa810SFred Isaman 
529839f7ad6SChuck Lever 	/*
530839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
531839f7ad6SChuck Lever 	 * generic layer handle the completion.
532839f7ad6SChuck Lever 	 */
533839f7ad6SChuck Lever 	if (requested_bytes == 0) {
534fe0f07d0SJens Axboe 		inode_dio_end(inode);
535839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
536839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
537839f7ad6SChuck Lever 	}
538839f7ad6SChuck Lever 
53919f73787SChuck Lever 	if (put_dreq(dreq))
5409811cd57SChristoph Hellwig 		nfs_direct_complete(dreq, false);
54119f73787SChuck Lever 	return 0;
54219f73787SChuck Lever }
54319f73787SChuck Lever 
54414a3ec79SChristoph Hellwig /**
54514a3ec79SChristoph Hellwig  * nfs_file_direct_read - file direct read operation for NFS files
54614a3ec79SChristoph Hellwig  * @iocb: target I/O control block
547619d30b4SAl Viro  * @iter: vector of user buffers into which to read data
54814a3ec79SChristoph Hellwig  * @pos: byte offset in file where reading starts
54914a3ec79SChristoph Hellwig  *
55014a3ec79SChristoph Hellwig  * We use this function for direct reads instead of calling
55114a3ec79SChristoph Hellwig  * generic_file_aio_read() in order to avoid gfar's check to see if
55214a3ec79SChristoph Hellwig  * the request starts before the end of the file.  For that check
55314a3ec79SChristoph Hellwig  * to work, we must generate a GETATTR before each direct read, and
55414a3ec79SChristoph Hellwig  * even then there is a window between the GETATTR and the subsequent
55514a3ec79SChristoph Hellwig  * READ where the file size could change.  Our preference is simply
55614a3ec79SChristoph Hellwig  * to do all reads the application wants, and the server will take
55714a3ec79SChristoph Hellwig  * care of managing the end of file boundary.
55814a3ec79SChristoph Hellwig  *
55914a3ec79SChristoph Hellwig  * This function also eliminates unnecessarily updating the file's
56014a3ec79SChristoph Hellwig  * atime locally, as the NFS server sets the file's atime, and this
56114a3ec79SChristoph Hellwig  * client must read the updated atime from the server back into its
56214a3ec79SChristoph Hellwig  * cache.
56314a3ec79SChristoph Hellwig  */
564619d30b4SAl Viro ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
565e19a8a0aSMartin K. Petersen 				loff_t pos)
5661da177e4SLinus Torvalds {
56714a3ec79SChristoph Hellwig 	struct file *file = iocb->ki_filp;
56814a3ec79SChristoph Hellwig 	struct address_space *mapping = file->f_mapping;
56914a3ec79SChristoph Hellwig 	struct inode *inode = mapping->host;
5701da177e4SLinus Torvalds 	struct nfs_direct_req *dreq;
571b3c54de6STrond Myklebust 	struct nfs_lock_context *l_ctx;
57214a3ec79SChristoph Hellwig 	ssize_t result = -EINVAL;
573a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
57414a3ec79SChristoph Hellwig 	nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
57514a3ec79SChristoph Hellwig 
57614a3ec79SChristoph Hellwig 	dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
57714a3ec79SChristoph Hellwig 		file, count, (long long) pos);
57814a3ec79SChristoph Hellwig 
57914a3ec79SChristoph Hellwig 	result = 0;
58014a3ec79SChristoph Hellwig 	if (!count)
58114a3ec79SChristoph Hellwig 		goto out;
58214a3ec79SChristoph Hellwig 
583d0b9875dSChristoph Hellwig 	mutex_lock(&inode->i_mutex);
58414a3ec79SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
58514a3ec79SChristoph Hellwig 	if (result)
586d0b9875dSChristoph Hellwig 		goto out_unlock;
58714a3ec79SChristoph Hellwig 
58814a3ec79SChristoph Hellwig 	task_io_account_read(count);
58914a3ec79SChristoph Hellwig 
59014a3ec79SChristoph Hellwig 	result = -ENOMEM;
591607f31e8STrond Myklebust 	dreq = nfs_direct_req_alloc();
592f11ac8dbSTrond Myklebust 	if (dreq == NULL)
593d0b9875dSChristoph Hellwig 		goto out_unlock;
5941da177e4SLinus Torvalds 
59591d5b470SChuck Lever 	dreq->inode = inode;
596619d30b4SAl Viro 	dreq->bytes_left = count;
5975fadeb47SPeng Tao 	dreq->io_start = pos;
598cd3758e3STrond Myklebust 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
599b3c54de6STrond Myklebust 	l_ctx = nfs_get_lock_context(dreq->ctx);
600b3c54de6STrond Myklebust 	if (IS_ERR(l_ctx)) {
601b3c54de6STrond Myklebust 		result = PTR_ERR(l_ctx);
602f11ac8dbSTrond Myklebust 		goto out_release;
603b3c54de6STrond Myklebust 	}
604b3c54de6STrond Myklebust 	dreq->l_ctx = l_ctx;
605487b8372SChuck Lever 	if (!is_sync_kiocb(iocb))
606487b8372SChuck Lever 		dreq->iocb = iocb;
6071da177e4SLinus Torvalds 
608619d30b4SAl Viro 	NFS_I(inode)->read_io += count;
60991f79c43SAl Viro 	result = nfs_direct_read_schedule_iovec(dreq, iter, pos);
610d0b9875dSChristoph Hellwig 
611d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
612d0b9875dSChristoph Hellwig 
61314a3ec79SChristoph Hellwig 	if (!result) {
614bc0fb201SChuck Lever 		result = nfs_direct_wait(dreq);
61514a3ec79SChristoph Hellwig 		if (result > 0)
61614a3ec79SChristoph Hellwig 			iocb->ki_pos = pos + result;
61714a3ec79SChristoph Hellwig 	}
618d0b9875dSChristoph Hellwig 
619d0b9875dSChristoph Hellwig 	nfs_direct_req_release(dreq);
620d0b9875dSChristoph Hellwig 	return result;
621d0b9875dSChristoph Hellwig 
622f11ac8dbSTrond Myklebust out_release:
623b4946ffbSTrond Myklebust 	nfs_direct_req_release(dreq);
624d0b9875dSChristoph Hellwig out_unlock:
625d0b9875dSChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
626f11ac8dbSTrond Myklebust out:
6271da177e4SLinus Torvalds 	return result;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
630085d1e33STom Haynes static void
631085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode,
632085d1e33STom Haynes 				  struct list_head *list,
633085d1e33STom Haynes 				  struct nfs_commit_info *cinfo)
634085d1e33STom Haynes {
635085d1e33STom Haynes 	spin_lock(cinfo->lock);
636085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1
637085d1e33STom Haynes 	if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
638085d1e33STom Haynes 		NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
639085d1e33STom Haynes #endif
640085d1e33STom Haynes 	nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
641085d1e33STom Haynes 	spin_unlock(cinfo->lock);
642085d1e33STom Haynes }
643085d1e33STom Haynes 
644fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
6451da177e4SLinus Torvalds {
6461763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
6471763da12SFred Isaman 	struct nfs_page *req, *tmp;
6481763da12SFred Isaman 	LIST_HEAD(reqs);
6491763da12SFred Isaman 	struct nfs_commit_info cinfo;
6501763da12SFred Isaman 	LIST_HEAD(failed);
6510a00b77bSWeston Andros Adamson 	int i;
6521763da12SFred Isaman 
6531763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
654085d1e33STom Haynes 	nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
6551da177e4SLinus Torvalds 
656fad61490STrond Myklebust 	dreq->count = 0;
6570a00b77bSWeston Andros Adamson 	for (i = 0; i < dreq->mirror_count; i++)
6580a00b77bSWeston Andros Adamson 		dreq->mirrors[i].count = 0;
659607f31e8STrond Myklebust 	get_dreq(dreq);
6601da177e4SLinus Torvalds 
661a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
6621763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
6631763da12SFred Isaman 	desc.pg_dreq = dreq;
664607f31e8STrond Myklebust 
6650a00b77bSWeston Andros Adamson 	req = nfs_list_entry(reqs.next);
6660a00b77bSWeston Andros Adamson 	nfs_direct_setup_mirroring(dreq, &desc, req);
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);
6721763da12SFred Isaman 			spin_lock(cinfo.lock);
6731763da12SFred Isaman 			dreq->flags = 0;
6741763da12SFred Isaman 			dreq->error = -EIO;
6751763da12SFred Isaman 			spin_unlock(cinfo.lock);
6761763da12SFred Isaman 		}
6775a695da2STrond Myklebust 		nfs_release_request(req);
6781763da12SFred Isaman 	}
6791763da12SFred Isaman 	nfs_pageio_complete(&desc);
680607f31e8STrond Myklebust 
6814035c248STrond Myklebust 	while (!list_empty(&failed)) {
6824035c248STrond Myklebust 		req = nfs_list_entry(failed.next);
6834035c248STrond Myklebust 		nfs_list_remove_request(req);
6841d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
6854035c248STrond Myklebust 	}
686607f31e8STrond Myklebust 
687607f31e8STrond Myklebust 	if (put_dreq(dreq))
6881763da12SFred Isaman 		nfs_direct_write_complete(dreq, dreq->inode);
689fad61490STrond Myklebust }
6901da177e4SLinus Torvalds 
6911763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data)
692fad61490STrond Myklebust {
6930b7c0153SFred Isaman 	struct nfs_direct_req *dreq = data->dreq;
6941763da12SFred Isaman 	struct nfs_commit_info cinfo;
6951763da12SFred Isaman 	struct nfs_page *req;
696c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
697c9d8f89dSTrond Myklebust 
6981763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
699c9d8f89dSTrond Myklebust 	if (status < 0) {
70060fa3f76STrond Myklebust 		dprintk("NFS: %5u commit failed with error %d.\n",
701c9d8f89dSTrond Myklebust 			data->task.tk_pid, status);
702fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
7035002c586SWeston Andros Adamson 	} else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
704c9d8f89dSTrond Myklebust 		dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
705fad61490STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
706fad61490STrond Myklebust 	}
707fad61490STrond Myklebust 
708c9d8f89dSTrond Myklebust 	dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
7091763da12SFred Isaman 	while (!list_empty(&data->pages)) {
7101763da12SFred Isaman 		req = nfs_list_entry(data->pages.next);
7111763da12SFred Isaman 		nfs_list_remove_request(req);
7121763da12SFred Isaman 		if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
7131763da12SFred Isaman 			/* Note the rewrite will go through mds */
714b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, NULL, &cinfo, 0);
715906369e4SFred Isaman 		} else
716906369e4SFred Isaman 			nfs_release_request(req);
7171d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
718fad61490STrond Myklebust 	}
719fad61490STrond Myklebust 
7201763da12SFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
7211763da12SFred Isaman 		nfs_direct_write_complete(dreq, data->inode);
7221763da12SFred Isaman }
7231763da12SFred Isaman 
7241763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
7251763da12SFred Isaman {
7261763da12SFred Isaman 	/* There is no lock to clear */
7271763da12SFred Isaman }
7281763da12SFred Isaman 
7291763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
7301763da12SFred Isaman 	.completion = nfs_direct_commit_complete,
7311763da12SFred Isaman 	.error_cleanup = nfs_direct_error_cleanup,
732fad61490STrond Myklebust };
733fad61490STrond Myklebust 
734fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
735fad61490STrond Myklebust {
7361763da12SFred Isaman 	int res;
7371763da12SFred Isaman 	struct nfs_commit_info cinfo;
7381763da12SFred Isaman 	LIST_HEAD(mds_list);
739fad61490STrond Myklebust 
7401763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7411763da12SFred Isaman 	nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
7421763da12SFred Isaman 	res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
7431763da12SFred Isaman 	if (res < 0) /* res == -ENOMEM */
7441763da12SFred Isaman 		nfs_direct_write_reschedule(dreq);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work)
7481da177e4SLinus Torvalds {
7491763da12SFred Isaman 	struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
750fad61490STrond Myklebust 	int flags = dreq->flags;
7511da177e4SLinus Torvalds 
752fad61490STrond Myklebust 	dreq->flags = 0;
753fad61490STrond Myklebust 	switch (flags) {
754fad61490STrond Myklebust 		case NFS_ODIRECT_DO_COMMIT:
755fad61490STrond Myklebust 			nfs_direct_commit_schedule(dreq);
7561da177e4SLinus Torvalds 			break;
757fad61490STrond Myklebust 		case NFS_ODIRECT_RESCHED_WRITES:
758fad61490STrond Myklebust 			nfs_direct_write_reschedule(dreq);
7591da177e4SLinus Torvalds 			break;
7601da177e4SLinus Torvalds 		default:
7619811cd57SChristoph Hellwig 			nfs_direct_complete(dreq, true);
7621da177e4SLinus Torvalds 	}
763fad61490STrond Myklebust }
764fad61490STrond Myklebust 
7651763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
766fad61490STrond Myklebust {
7671763da12SFred Isaman 	schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
768fad61490STrond Myklebust }
7691763da12SFred Isaman 
7701763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
7711763da12SFred Isaman {
7721763da12SFred Isaman 	struct nfs_direct_req *dreq = hdr->dreq;
7731763da12SFred Isaman 	struct nfs_commit_info cinfo;
774c65e6254SWeston Andros Adamson 	bool request_commit = false;
7751763da12SFred Isaman 	struct nfs_page *req = nfs_list_entry(hdr->pages.next);
7761763da12SFred Isaman 
7771763da12SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
7781763da12SFred Isaman 		goto out_put;
7791763da12SFred Isaman 
7801763da12SFred Isaman 	nfs_init_cinfo_from_dreq(&cinfo, dreq);
7811763da12SFred Isaman 
7821763da12SFred Isaman 	spin_lock(&dreq->lock);
7831763da12SFred Isaman 
7841763da12SFred Isaman 	if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
7851763da12SFred Isaman 		dreq->flags = 0;
7861763da12SFred Isaman 		dreq->error = hdr->error;
7871763da12SFred Isaman 	}
788c65e6254SWeston Andros Adamson 	if (dreq->error == 0) {
7890a00b77bSWeston Andros Adamson 		nfs_direct_good_bytes(dreq, hdr);
790c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
7911763da12SFred Isaman 			if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
792c65e6254SWeston Andros Adamson 				request_commit = true;
7931763da12SFred Isaman 			else if (dreq->flags == 0) {
7945002c586SWeston Andros Adamson 				nfs_direct_set_hdr_verf(dreq, hdr);
795c65e6254SWeston Andros Adamson 				request_commit = true;
7961763da12SFred Isaman 				dreq->flags = NFS_ODIRECT_DO_COMMIT;
7971763da12SFred Isaman 			} else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
798c65e6254SWeston Andros Adamson 				request_commit = true;
799c65e6254SWeston Andros Adamson 				if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
8005002c586SWeston Andros Adamson 					dreq->flags =
8015002c586SWeston Andros Adamson 						NFS_ODIRECT_RESCHED_WRITES;
8021763da12SFred Isaman 			}
8031763da12SFred Isaman 		}
8041763da12SFred Isaman 	}
8051763da12SFred Isaman 	spin_unlock(&dreq->lock);
8061763da12SFred Isaman 
8071763da12SFred Isaman 	while (!list_empty(&hdr->pages)) {
8082bfc6e56SWeston Andros Adamson 
8091763da12SFred Isaman 		req = nfs_list_entry(hdr->pages.next);
8101763da12SFred Isaman 		nfs_list_remove_request(req);
811c65e6254SWeston Andros Adamson 		if (request_commit) {
81204277086STrond Myklebust 			kref_get(&req->wb_kref);
813b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
814b57ff130SWeston Andros Adamson 				hdr->ds_commit_idx);
8151763da12SFred Isaman 		}
8161d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8171763da12SFred Isaman 	}
8181763da12SFred Isaman 
8191763da12SFred Isaman out_put:
8201763da12SFred Isaman 	if (put_dreq(dreq))
8211763da12SFred Isaman 		nfs_direct_write_complete(dreq, hdr->inode);
8221763da12SFred Isaman 	hdr->release(hdr);
8231763da12SFred Isaman }
8241763da12SFred Isaman 
8253e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head)
8263e9e0ca3STrond Myklebust {
8273e9e0ca3STrond Myklebust 	struct nfs_page *req;
8283e9e0ca3STrond Myklebust 
8293e9e0ca3STrond Myklebust 	while (!list_empty(head)) {
8303e9e0ca3STrond Myklebust 		req = nfs_list_entry(head->next);
8313e9e0ca3STrond Myklebust 		nfs_list_remove_request(req);
8321d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
8333e9e0ca3STrond Myklebust 	}
8343e9e0ca3STrond Myklebust }
8353e9e0ca3STrond Myklebust 
836*dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
837*dc602dd7STrond Myklebust {
838*dc602dd7STrond Myklebust 	struct nfs_direct_req *dreq = hdr->dreq;
839*dc602dd7STrond Myklebust 
840*dc602dd7STrond Myklebust 	spin_lock(&dreq->lock);
841*dc602dd7STrond Myklebust 	if (dreq->error == 0) {
842*dc602dd7STrond Myklebust 		dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
843*dc602dd7STrond Myklebust 		/* fake unstable write to let common nfs resend pages */
844*dc602dd7STrond Myklebust 		hdr->verf.committed = NFS_UNSTABLE;
845*dc602dd7STrond Myklebust 		hdr->good_bytes = hdr->args.count;
846*dc602dd7STrond Myklebust 	}
847*dc602dd7STrond Myklebust 	spin_unlock(&dreq->lock);
848*dc602dd7STrond Myklebust }
849*dc602dd7STrond Myklebust 
8501763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
8513e9e0ca3STrond Myklebust 	.error_cleanup = nfs_write_sync_pgio_error,
8521763da12SFred Isaman 	.init_hdr = nfs_direct_pgio_init,
8531763da12SFred Isaman 	.completion = nfs_direct_write_completion,
854*dc602dd7STrond Myklebust 	.reschedule_io = nfs_direct_write_reschedule_io,
8551763da12SFred Isaman };
8561763da12SFred Isaman 
85791f79c43SAl Viro 
85891f79c43SAl Viro /*
85991f79c43SAl Viro  * NB: Return the value of the first error return code.  Subsequent
86091f79c43SAl Viro  *     errors after the first one are ignored.
86191f79c43SAl Viro  */
86291f79c43SAl Viro /*
86391f79c43SAl Viro  * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
86491f79c43SAl Viro  * operation.  If nfs_writedata_alloc() or get_user_pages() fails,
86591f79c43SAl Viro  * bail and stop sending more writes.  Write length accounting is
86691f79c43SAl Viro  * handled automatically by nfs_direct_write_result().  Otherwise, if
86791f79c43SAl Viro  * no requests have been sent, just return an error.
86891f79c43SAl Viro  */
86919f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
870619d30b4SAl Viro 					       struct iov_iter *iter,
87191f79c43SAl Viro 					       loff_t pos)
87219f73787SChuck Lever {
8731763da12SFred Isaman 	struct nfs_pageio_descriptor desc;
8741d59d61fSTrond Myklebust 	struct inode *inode = dreq->inode;
87519f73787SChuck Lever 	ssize_t result = 0;
87619f73787SChuck Lever 	size_t requested_bytes = 0;
87791f79c43SAl Viro 	size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
87819f73787SChuck Lever 
879a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
8801763da12SFred Isaman 			      &nfs_direct_write_completion_ops);
8811763da12SFred Isaman 	desc.pg_dreq = dreq;
88219f73787SChuck Lever 	get_dreq(dreq);
883fe0f07d0SJens Axboe 	inode_dio_begin(inode);
88419f73787SChuck Lever 
88591f79c43SAl Viro 	NFS_I(inode)->write_io += iov_iter_count(iter);
88691f79c43SAl Viro 	while (iov_iter_count(iter)) {
88791f79c43SAl Viro 		struct page **pagevec;
88891f79c43SAl Viro 		size_t bytes;
88991f79c43SAl Viro 		size_t pgbase;
89091f79c43SAl Viro 		unsigned npages, i;
89191f79c43SAl Viro 
89291f79c43SAl Viro 		result = iov_iter_get_pages_alloc(iter, &pagevec,
89391f79c43SAl Viro 						  wsize, &pgbase);
89419f73787SChuck Lever 		if (result < 0)
89519f73787SChuck Lever 			break;
89691f79c43SAl Viro 
89791f79c43SAl Viro 		bytes = result;
89891f79c43SAl Viro 		iov_iter_advance(iter, bytes);
89991f79c43SAl Viro 		npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
90091f79c43SAl Viro 		for (i = 0; i < npages; i++) {
90191f79c43SAl Viro 			struct nfs_page *req;
90291f79c43SAl Viro 			unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
90391f79c43SAl Viro 
90416b90578SLinus Torvalds 			req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
90591f79c43SAl Viro 						 pgbase, req_len);
90691f79c43SAl Viro 			if (IS_ERR(req)) {
90791f79c43SAl Viro 				result = PTR_ERR(req);
90819f73787SChuck Lever 				break;
90991f79c43SAl Viro 			}
9100a00b77bSWeston Andros Adamson 
9110a00b77bSWeston Andros Adamson 			nfs_direct_setup_mirroring(dreq, &desc, req);
9120a00b77bSWeston Andros Adamson 
91391f79c43SAl Viro 			nfs_lock_request(req);
91491f79c43SAl Viro 			req->wb_index = pos >> PAGE_SHIFT;
91591f79c43SAl Viro 			req->wb_offset = pos & ~PAGE_MASK;
91691f79c43SAl Viro 			if (!nfs_pageio_add_request(&desc, req)) {
91791f79c43SAl Viro 				result = desc.pg_error;
91891f79c43SAl Viro 				nfs_unlock_and_release_request(req);
91991f79c43SAl Viro 				break;
92091f79c43SAl Viro 			}
92191f79c43SAl Viro 			pgbase = 0;
92291f79c43SAl Viro 			bytes -= req_len;
92391f79c43SAl Viro 			requested_bytes += req_len;
92491f79c43SAl Viro 			pos += req_len;
92591f79c43SAl Viro 			dreq->bytes_left -= req_len;
92691f79c43SAl Viro 		}
92791f79c43SAl Viro 		nfs_direct_release_pages(pagevec, npages);
92891f79c43SAl Viro 		kvfree(pagevec);
92991f79c43SAl Viro 		if (result < 0)
93091f79c43SAl Viro 			break;
93119f73787SChuck Lever 	}
9321763da12SFred Isaman 	nfs_pageio_complete(&desc);
93319f73787SChuck Lever 
934839f7ad6SChuck Lever 	/*
935839f7ad6SChuck Lever 	 * If no bytes were started, return the error, and let the
936839f7ad6SChuck Lever 	 * generic layer handle the completion.
937839f7ad6SChuck Lever 	 */
938839f7ad6SChuck Lever 	if (requested_bytes == 0) {
939fe0f07d0SJens Axboe 		inode_dio_end(inode);
940839f7ad6SChuck Lever 		nfs_direct_req_release(dreq);
941839f7ad6SChuck Lever 		return result < 0 ? result : -EIO;
942839f7ad6SChuck Lever 	}
943839f7ad6SChuck Lever 
94419f73787SChuck Lever 	if (put_dreq(dreq))
94519f73787SChuck Lever 		nfs_direct_write_complete(dreq, dreq->inode);
94619f73787SChuck Lever 	return 0;
94719f73787SChuck Lever }
94819f73787SChuck Lever 
9491da177e4SLinus Torvalds /**
9501da177e4SLinus Torvalds  * nfs_file_direct_write - file direct write operation for NFS files
9511da177e4SLinus Torvalds  * @iocb: target I/O control block
952619d30b4SAl Viro  * @iter: vector of user buffers from which to write data
95388467055SChuck Lever  * @pos: byte offset in file where writing starts
9541da177e4SLinus Torvalds  *
9551da177e4SLinus Torvalds  * We use this function for direct writes instead of calling
9561da177e4SLinus Torvalds  * generic_file_aio_write() in order to avoid taking the inode
9571da177e4SLinus Torvalds  * semaphore and updating the i_size.  The NFS server will set
9581da177e4SLinus Torvalds  * the new i_size and this client must read the updated size
9591da177e4SLinus Torvalds  * back into its cache.  We let the server do generic write
9601da177e4SLinus Torvalds  * parameter checking and report problems.
9611da177e4SLinus Torvalds  *
9621da177e4SLinus Torvalds  * We eliminate local atime updates, see direct read above.
9631da177e4SLinus Torvalds  *
9641da177e4SLinus Torvalds  * We avoid unnecessary page cache invalidations for normal cached
9651da177e4SLinus Torvalds  * readers of this file.
9661da177e4SLinus Torvalds  *
9671da177e4SLinus Torvalds  * Note that O_APPEND is not supported for NFS direct writes, as there
9681da177e4SLinus Torvalds  * is no atomic O_APPEND write facility in the NFS protocol.
9691da177e4SLinus Torvalds  */
97065a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
9711da177e4SLinus Torvalds {
97222cd1bf1SChristoph Hellwig 	ssize_t result = -EINVAL;
9731da177e4SLinus Torvalds 	struct file *file = iocb->ki_filp;
9741da177e4SLinus Torvalds 	struct address_space *mapping = file->f_mapping;
97522cd1bf1SChristoph Hellwig 	struct inode *inode = mapping->host;
97622cd1bf1SChristoph Hellwig 	struct nfs_direct_req *dreq;
97722cd1bf1SChristoph Hellwig 	struct nfs_lock_context *l_ctx;
97865a4a1caSAl Viro 	loff_t pos, end;
979c216fd70SChuck Lever 
9806de1472fSAl Viro 	dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
9813309dd04SAl Viro 		file, iov_iter_count(iter), (long long) iocb->ki_pos);
982027445c3SBadari Pulavarty 
9833309dd04SAl Viro 	nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES,
9843309dd04SAl Viro 		      iov_iter_count(iter));
9853309dd04SAl Viro 
9863309dd04SAl Viro 	pos = iocb->ki_pos;
9873309dd04SAl Viro 	end = (pos + iov_iter_count(iter) - 1) >> PAGE_CACHE_SHIFT;
988ce1a8e67SChuck Lever 
989a9ab5e84SChristoph Hellwig 	mutex_lock(&inode->i_mutex);
990a9ab5e84SChristoph Hellwig 
99122cd1bf1SChristoph Hellwig 	result = nfs_sync_mapping(mapping);
99222cd1bf1SChristoph Hellwig 	if (result)
993a9ab5e84SChristoph Hellwig 		goto out_unlock;
994a9ab5e84SChristoph Hellwig 
995a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
996a9ab5e84SChristoph Hellwig 		result = invalidate_inode_pages2_range(mapping,
997a9ab5e84SChristoph Hellwig 					pos >> PAGE_CACHE_SHIFT, end);
998a9ab5e84SChristoph Hellwig 		if (result)
999a9ab5e84SChristoph Hellwig 			goto out_unlock;
1000a9ab5e84SChristoph Hellwig 	}
10011da177e4SLinus Torvalds 
10023309dd04SAl Viro 	task_io_account_write(iov_iter_count(iter));
10037ec10f26SKonstantin Khlebnikov 
100422cd1bf1SChristoph Hellwig 	result = -ENOMEM;
100522cd1bf1SChristoph Hellwig 	dreq = nfs_direct_req_alloc();
100622cd1bf1SChristoph Hellwig 	if (!dreq)
1007a9ab5e84SChristoph Hellwig 		goto out_unlock;
100822cd1bf1SChristoph Hellwig 
100922cd1bf1SChristoph Hellwig 	dreq->inode = inode;
10103309dd04SAl Viro 	dreq->bytes_left = iov_iter_count(iter);
10115fadeb47SPeng Tao 	dreq->io_start = pos;
101222cd1bf1SChristoph Hellwig 	dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
101322cd1bf1SChristoph Hellwig 	l_ctx = nfs_get_lock_context(dreq->ctx);
101422cd1bf1SChristoph Hellwig 	if (IS_ERR(l_ctx)) {
101522cd1bf1SChristoph Hellwig 		result = PTR_ERR(l_ctx);
101622cd1bf1SChristoph Hellwig 		goto out_release;
101722cd1bf1SChristoph Hellwig 	}
101822cd1bf1SChristoph Hellwig 	dreq->l_ctx = l_ctx;
101922cd1bf1SChristoph Hellwig 	if (!is_sync_kiocb(iocb))
102022cd1bf1SChristoph Hellwig 		dreq->iocb = iocb;
102122cd1bf1SChristoph Hellwig 
102291f79c43SAl Viro 	result = nfs_direct_write_schedule_iovec(dreq, iter, pos);
1023a9ab5e84SChristoph Hellwig 
1024a9ab5e84SChristoph Hellwig 	if (mapping->nrpages) {
1025a9ab5e84SChristoph Hellwig 		invalidate_inode_pages2_range(mapping,
1026a9ab5e84SChristoph Hellwig 					      pos >> PAGE_CACHE_SHIFT, end);
1027a9ab5e84SChristoph Hellwig 	}
1028a9ab5e84SChristoph Hellwig 
1029a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
1030a9ab5e84SChristoph Hellwig 
103122cd1bf1SChristoph Hellwig 	if (!result) {
103222cd1bf1SChristoph Hellwig 		result = nfs_direct_wait(dreq);
103322cd1bf1SChristoph Hellwig 		if (result > 0) {
10341763da12SFred Isaman 			struct inode *inode = mapping->host;
10359eafa8ccSChuck Lever 
103622cd1bf1SChristoph Hellwig 			iocb->ki_pos = pos + result;
10371763da12SFred Isaman 			spin_lock(&inode->i_lock);
10381763da12SFred Isaman 			if (i_size_read(inode) < iocb->ki_pos)
10391763da12SFred Isaman 				i_size_write(inode, iocb->ki_pos);
10401763da12SFred Isaman 			spin_unlock(&inode->i_lock);
1041a0815d55STrond Myklebust 			generic_write_sync(file, pos, result);
10421763da12SFred Isaman 		}
104322cd1bf1SChristoph Hellwig 	}
1044a9ab5e84SChristoph Hellwig 	nfs_direct_req_release(dreq);
1045a9ab5e84SChristoph Hellwig 	return result;
1046a9ab5e84SChristoph Hellwig 
104722cd1bf1SChristoph Hellwig out_release:
104822cd1bf1SChristoph Hellwig 	nfs_direct_req_release(dreq);
1049a9ab5e84SChristoph Hellwig out_unlock:
1050a9ab5e84SChristoph Hellwig 	mutex_unlock(&inode->i_mutex);
105122cd1bf1SChristoph Hellwig 	return result;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
105488467055SChuck Lever /**
105588467055SChuck Lever  * nfs_init_directcache - create a slab cache for nfs_direct_req structures
105688467055SChuck Lever  *
105788467055SChuck Lever  */
1058f7b422b1SDavid Howells int __init nfs_init_directcache(void)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
10611da177e4SLinus Torvalds 						sizeof(struct nfs_direct_req),
1062fffb60f9SPaul Jackson 						0, (SLAB_RECLAIM_ACCOUNT|
1063fffb60f9SPaul Jackson 							SLAB_MEM_SPREAD),
106420c2df83SPaul Mundt 						NULL);
10651da177e4SLinus Torvalds 	if (nfs_direct_cachep == NULL)
10661da177e4SLinus Torvalds 		return -ENOMEM;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	return 0;
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
107188467055SChuck Lever /**
1072f7b422b1SDavid Howells  * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
107388467055SChuck Lever  *
107488467055SChuck Lever  */
1075266bee88SDavid Brownell void nfs_destroy_directcache(void)
10761da177e4SLinus Torvalds {
10771a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_direct_cachep);
10781da177e4SLinus Torvalds }
1079