blob: cae26cbd59ee89cd5d73e719ef201b26ef9cabc3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/direct.c
3 *
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
5 *
6 * High-performance uncached I/O for the Linux NFS client
7 *
8 * There are important applications whose performance or correctness
9 * depends on uncached access to file data. Database clusters
Chuck Lever88467052006-03-20 13:44:34 -050010 * (multiple copies of the same instance running on separate hosts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * implement their own cache coherency protocol that subsumes file
Chuck Lever88467052006-03-20 13:44:34 -050012 * system cache protocols. Applications that process datasets
13 * considerably larger than the client's memory do not always benefit
14 * from a local cache. A streaming video server, for instance, has no
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * need to cache the contents of a file.
16 *
17 * When an application requests uncached I/O, all read and write requests
18 * are made directly to the server; data stored or fetched via these
19 * requests is not cached in the Linux page cache. The client does not
20 * correct unaligned requests from applications. All requested bytes are
21 * held on permanent storage before a direct write system call returns to
22 * an application.
23 *
24 * Solaris implements an uncached I/O facility called directio() that
25 * is used for backups and sequential I/O to very large files. Solaris
26 * also supports uncaching whole NFS partitions with "-o forcedirectio,"
27 * an undocumented mount option.
28 *
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
31 *
32 * 18 Dec 2001 Initial implementation for 2.4 --cel
33 * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
34 * 08 Jun 2003 Port to 2.5 APIs --cel
35 * 31 Mar 2004 Handle direct I/O without VFS support --cel
36 * 15 Sep 2004 Parallel async reads --cel
Chuck Lever88467052006-03-20 13:44:34 -050037 * 04 May 2005 support O_DIRECT with aio --cel
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/sched.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/file.h>
45#include <linux/pagemap.h>
46#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Konstantin Khlebnikov7ec10f22011-02-22 00:28:34 +030048#include <linux/task_io_accounting_ops.h>
Peng Tao62965562012-09-25 14:55:57 +080049#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <linux/nfs_fs.h>
52#include <linux/nfs_page.h>
53#include <linux/sunrpc/clnt.h>
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070056#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust8d5658c2007-04-10 09:26:35 -040058#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050059#include "iostat.h"
Fred Isaman1763da12012-04-20 14:47:57 -040060#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Christoph Lametere18b8902006-12-06 20:33:20 -080064static struct kmem_cache *nfs_direct_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * This represents a set of asynchronous requests that we're waiting on
68 */
69struct nfs_direct_req {
70 struct kref kref; /* release manager */
Chuck Lever15ce4a02006-03-20 13:44:34 -050071
72 /* I/O parameters */
Trond Myklebusta8881f52006-03-20 13:44:36 -050073 struct nfs_open_context *ctx; /* file open context info */
Trond Myklebustf11ac8d2010-06-25 16:35:53 -040074 struct nfs_lock_context *l_ctx; /* Lock context info */
Chuck Lever99514f82006-03-20 13:44:30 -050075 struct kiocb * iocb; /* controlling i/o request */
Chuck Lever88467052006-03-20 13:44:34 -050076 struct inode * inode; /* target file of i/o */
Chuck Lever15ce4a02006-03-20 13:44:34 -050077
78 /* completion state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040079 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050080 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050081 ssize_t count, /* bytes actually processed */
Peng Tao35754bc2012-09-25 14:55:57 +080082 bytes_left, /* bytes left to be sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050084 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050085
86 /* commit state */
Fred Isaman1763da12012-04-20 14:47:57 -040087 struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
88 struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
89 struct work_struct work;
Trond Myklebustfad61492006-03-20 13:44:36 -050090 int flags;
91#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
92#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
93 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
Fred Isaman1763da12012-04-20 14:47:57 -040096static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
97static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
Trond Myklebustfad61492006-03-20 13:44:36 -050098static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Fred Isaman1763da12012-04-20 14:47:57 -040099static void nfs_direct_write_schedule_work(struct work_struct *work);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400100
101static inline void get_dreq(struct nfs_direct_req *dreq)
102{
103 atomic_inc(&dreq->io_count);
104}
105
106static inline int put_dreq(struct nfs_direct_req *dreq)
107{
108 return atomic_dec_and_test(&dreq->io_count);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500112 * nfs_direct_IO - NFS address space operation for direct I/O
113 * @rw: direction (read or write)
114 * @iocb: target I/O control block
115 * @iov: array of vectors that define I/O buffer
116 * @pos: offset in file to begin the operation
117 * @nr_segs: size of iovec array
118 *
119 * The presence of this routine in the address space ops vector means
Mel Gormana564b8f2012-07-31 16:45:12 -0700120 * the NFS client supports direct I/O. However, for most direct IO, we
121 * shunt off direct read and write requests before the VFS gets them,
122 * so this method is only ever called for swap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500124ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
125{
Mel Gormana564b8f2012-07-31 16:45:12 -0700126#ifndef CONFIG_NFS_SWAP
Chuck Leverb8a32e22006-03-20 13:44:28 -0500127 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800128 iocb->ki_filp->f_path.dentry->d_name.name,
Trond Myklebuste99170f2006-04-18 13:21:42 -0400129 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500130
131 return -EINVAL;
Mel Gormana564b8f2012-07-31 16:45:12 -0700132#else
133 VM_BUG_ON(iocb->ki_left != PAGE_SIZE);
134 VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
135
136 if (rw == READ || rw == KERNEL_READ)
137 return nfs_file_direct_read(iocb, iov, nr_segs, pos,
138 rw == READ ? true : false);
139 return nfs_file_direct_write(iocb, iov, nr_segs, pos,
140 rw == WRITE ? true : false);
141#endif /* CONFIG_NFS_SWAP */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500142}
143
Chuck Lever749e1462007-05-19 17:22:46 -0400144static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
Chuck Lever9c93ab72006-06-20 12:56:31 -0400145{
Chuck Lever749e1462007-05-19 17:22:46 -0400146 unsigned int i;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400147 for (i = 0; i < npages; i++)
148 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500149}
150
Fred Isaman1763da12012-04-20 14:47:57 -0400151void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
152 struct nfs_direct_req *dreq)
153{
154 cinfo->lock = &dreq->lock;
155 cinfo->mds = &dreq->mds_cinfo;
156 cinfo->ds = &dreq->ds_cinfo;
157 cinfo->dreq = dreq;
158 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
159}
160
Chuck Lever93619e52006-03-20 13:44:31 -0500161static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Trond Myklebust292f3ee2012-04-30 18:31:49 -0400165 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (!dreq)
167 return NULL;
168
169 kref_init(&dreq->kref);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400170 kref_get(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500171 init_completion(&dreq->completion);
Fred Isaman1763da12012-04-20 14:47:57 -0400172 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
173 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500174 spin_lock_init(&dreq->lock);
Chuck Lever93619e52006-03-20 13:44:31 -0500175
176 return dreq;
177}
178
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400179static void nfs_direct_req_free(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500182
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400183 if (dreq->l_ctx != NULL)
184 nfs_put_lock_context(dreq->l_ctx);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500185 if (dreq->ctx != NULL)
186 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kmem_cache_free(nfs_direct_cachep, dreq);
188}
189
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400190static void nfs_direct_req_release(struct nfs_direct_req *dreq)
191{
192 kref_put(&dreq->kref, nfs_direct_req_free);
193}
194
Peng Tao62965562012-09-25 14:55:57 +0800195ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
196{
197 return dreq->bytes_left;
198}
199EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
200
Chuck Leverd4cc9482006-03-20 13:44:28 -0500201/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500202 * Collects and returns the final error value/byte-count.
203 */
204static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
205{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500206 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500207
208 /* Async requests don't wait here */
209 if (dreq->iocb)
210 goto out;
211
Matthew Wilcox150030b2007-12-06 16:24:39 -0500212 result = wait_for_completion_killable(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500213
214 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500215 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500216 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500217 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500218
219out:
Chuck Leverbc0fb202006-03-20 13:44:31 -0500220 return (ssize_t) result;
221}
222
223/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400224 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
225 * the iocb is still valid here if this is a synchronous request.
Chuck Lever63ab46a2006-03-20 13:44:31 -0500226 */
227static void nfs_direct_complete(struct nfs_direct_req *dreq)
228{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500229 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500230 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500231 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500232 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500233 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500234 }
235 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500236
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400237 nfs_direct_req_release(dreq);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500238}
239
Trond Myklebust1385b812012-05-04 13:54:24 -0400240static void nfs_direct_readpage_release(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Fred Isaman584aa812012-04-20 14:47:51 -0400242 dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
243 req->wb_context->dentry->d_inode->i_sb->s_id,
244 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
245 req->wb_bytes,
246 (long long)req_offset(req));
247 nfs_release_request(req);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400248}
249
Fred Isaman584aa812012-04-20 14:47:51 -0400250static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400251{
Fred Isaman584aa812012-04-20 14:47:51 -0400252 unsigned long bytes = 0;
253 struct nfs_direct_req *dreq = hdr->dreq;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400254
Fred Isaman584aa812012-04-20 14:47:51 -0400255 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
256 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Chuck Lever15ce4a02006-03-20 13:44:34 -0500258 spin_lock(&dreq->lock);
Fred Isaman584aa812012-04-20 14:47:51 -0400259 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
260 dreq->error = hdr->error;
261 else
262 dreq->count += hdr->good_bytes;
263 spin_unlock(&dreq->lock);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400264
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400265 while (!list_empty(&hdr->pages)) {
266 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
267 struct page *page = req->wb_page;
Fred Isaman584aa812012-04-20 14:47:51 -0400268
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400269 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
270 if (bytes > hdr->good_bytes)
271 zero_user(page, 0, PAGE_SIZE);
272 else if (hdr->good_bytes - bytes < PAGE_SIZE)
273 zero_user_segment(page,
274 hdr->good_bytes & ~PAGE_MASK,
275 PAGE_SIZE);
276 }
277 if (!PageCompound(page)) {
278 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
279 if (bytes < hdr->good_bytes)
280 set_page_dirty(page);
281 } else
Fred Isaman584aa812012-04-20 14:47:51 -0400282 set_page_dirty(page);
Fred Isaman584aa812012-04-20 14:47:51 -0400283 }
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400284 bytes += req->wb_bytes;
285 nfs_list_remove_request(req);
286 nfs_direct_readpage_release(req);
Fred Isaman584aa812012-04-20 14:47:51 -0400287 }
288out_put:
Trond Myklebust607f31e2006-06-28 16:52:45 -0400289 if (put_dreq(dreq))
290 nfs_direct_complete(dreq);
Fred Isaman584aa812012-04-20 14:47:51 -0400291 hdr->release(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400294static void nfs_read_sync_pgio_error(struct list_head *head)
Fred Isamancd841602012-04-20 14:47:44 -0400295{
Fred Isaman584aa812012-04-20 14:47:51 -0400296 struct nfs_page *req;
Fred Isamancd841602012-04-20 14:47:44 -0400297
Fred Isaman584aa812012-04-20 14:47:51 -0400298 while (!list_empty(head)) {
299 req = nfs_list_entry(head->next);
300 nfs_list_remove_request(req);
301 nfs_release_request(req);
302 }
Fred Isamancd841602012-04-20 14:47:44 -0400303}
304
Fred Isaman584aa812012-04-20 14:47:51 -0400305static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
306{
307 get_dreq(hdr->dreq);
308}
309
310static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400311 .error_cleanup = nfs_read_sync_pgio_error,
Fred Isaman584aa812012-04-20 14:47:51 -0400312 .init_hdr = nfs_direct_pgio_init,
313 .completion = nfs_direct_read_completion,
314};
315
Chuck Leverd4cc9482006-03-20 13:44:28 -0500316/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400317 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
318 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
319 * bail and stop sending more reads. Read length accounting is
320 * handled automatically by nfs_direct_read_result(). Otherwise, if
321 * no requests have been sent, just return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Fred Isaman584aa812012-04-20 14:47:51 -0400323static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
Chuck Lever02fe4942007-11-12 12:17:03 -0500324 const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700325 loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Fred Isaman584aa812012-04-20 14:47:51 -0400327 struct nfs_direct_req *dreq = desc->pg_dreq;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500328 struct nfs_open_context *ctx = dreq->ctx;
Al Viro3d4ff432011-06-22 18:40:12 -0400329 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500330 unsigned long user_addr = (unsigned long)iov->iov_base;
331 size_t count = iov->iov_len;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500332 size_t rsize = NFS_SERVER(inode)->rsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400333 unsigned int pgbase;
334 int result;
335 ssize_t started = 0;
Fred Isaman584aa812012-04-20 14:47:51 -0400336 struct page **pagevec = NULL;
337 unsigned int npages;
Chuck Lever82b145c2006-06-20 12:57:03 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 do {
Chuck Lever5dd602f2006-03-20 13:44:29 -0500340 size_t bytes;
Fred Isaman584aa812012-04-20 14:47:51 -0400341 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700343 pgbase = user_addr & ~PAGE_MASK;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400344 bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700345
Trond Myklebust607f31e2006-06-28 16:52:45 -0400346 result = -ENOMEM;
Fred Isaman584aa812012-04-20 14:47:51 -0400347 npages = nfs_page_array_len(pgbase, bytes);
348 if (!pagevec)
349 pagevec = kmalloc(npages * sizeof(struct page *),
350 GFP_KERNEL);
351 if (!pagevec)
Trond Myklebust607f31e2006-06-28 16:52:45 -0400352 break;
Mel Gormana564b8f2012-07-31 16:45:12 -0700353 if (uio) {
354 down_read(&current->mm->mmap_sem);
355 result = get_user_pages(current, current->mm, user_addr,
Fred Isaman584aa812012-04-20 14:47:51 -0400356 npages, 1, 0, pagevec, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -0700357 up_read(&current->mm->mmap_sem);
358 if (result < 0)
359 break;
360 } else {
361 WARN_ON(npages != 1);
362 result = get_kernel_page(user_addr, 1, pagevec);
363 if (WARN_ON(result != 1))
364 break;
365 }
366
Fred Isaman584aa812012-04-20 14:47:51 -0400367 if ((unsigned)result < npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400368 bytes = result * PAGE_SIZE;
369 if (bytes <= pgbase) {
Fred Isaman584aa812012-04-20 14:47:51 -0400370 nfs_direct_release_pages(pagevec, result);
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400371 break;
372 }
373 bytes -= pgbase;
Fred Isaman584aa812012-04-20 14:47:51 -0400374 npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400375 }
Chuck Lever06cf6f22006-06-20 12:56:49 -0400376
Fred Isaman584aa812012-04-20 14:47:51 -0400377 for (i = 0; i < npages; i++) {
378 struct nfs_page *req;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400379 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
Fred Isaman584aa812012-04-20 14:47:51 -0400380 /* XXX do we need to do the eof zeroing found in async_filler? */
381 req = nfs_create_request(dreq->ctx, dreq->inode,
382 pagevec[i],
383 pgbase, req_len);
384 if (IS_ERR(req)) {
Fred Isaman584aa812012-04-20 14:47:51 -0400385 result = PTR_ERR(req);
386 break;
387 }
388 req->wb_index = pos >> PAGE_SHIFT;
389 req->wb_offset = pos & ~PAGE_MASK;
390 if (!nfs_pageio_add_request(desc, req)) {
391 result = desc->pg_error;
392 nfs_release_request(req);
Fred Isaman584aa812012-04-20 14:47:51 -0400393 break;
394 }
395 pgbase = 0;
396 bytes -= req_len;
397 started += req_len;
398 user_addr += req_len;
399 pos += req_len;
400 count -= req_len;
Peng Tao35754bc2012-09-25 14:55:57 +0800401 dreq->bytes_left -= req_len;
Fred Isaman584aa812012-04-20 14:47:51 -0400402 }
Trond Myklebust6d747432012-04-30 13:27:31 -0400403 /* The nfs_page now hold references to these pages */
404 nfs_direct_release_pages(pagevec, npages);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400405 } while (count != 0 && result >= 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400406
Fred Isaman584aa812012-04-20 14:47:51 -0400407 kfree(pagevec);
408
Trond Myklebust607f31e2006-06-28 16:52:45 -0400409 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500410 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400411 return result < 0 ? (ssize_t) result : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Chuck Lever19f73782007-11-12 12:16:47 -0500414static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
415 const struct iovec *iov,
416 unsigned long nr_segs,
Mel Gormana564b8f2012-07-31 16:45:12 -0700417 loff_t pos, bool uio)
Chuck Lever19f73782007-11-12 12:16:47 -0500418{
Fred Isaman584aa812012-04-20 14:47:51 -0400419 struct nfs_pageio_descriptor desc;
Chuck Lever19f73782007-11-12 12:16:47 -0500420 ssize_t result = -EINVAL;
421 size_t requested_bytes = 0;
422 unsigned long seg;
423
Fred Isaman59948db2012-07-18 14:20:49 -0400424 NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
Fred Isaman584aa812012-04-20 14:47:51 -0400425 &nfs_direct_read_completion_ops);
Chuck Lever19f73782007-11-12 12:16:47 -0500426 get_dreq(dreq);
Fred Isaman584aa812012-04-20 14:47:51 -0400427 desc.pg_dreq = dreq;
Chuck Lever19f73782007-11-12 12:16:47 -0500428
429 for (seg = 0; seg < nr_segs; seg++) {
430 const struct iovec *vec = &iov[seg];
Mel Gormana564b8f2012-07-31 16:45:12 -0700431 result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
Chuck Lever19f73782007-11-12 12:16:47 -0500432 if (result < 0)
433 break;
434 requested_bytes += result;
435 if ((size_t)result < vec->iov_len)
436 break;
437 pos += vec->iov_len;
438 }
439
Fred Isaman584aa812012-04-20 14:47:51 -0400440 nfs_pageio_complete(&desc);
441
Chuck Lever839f7ad2011-01-21 15:54:57 +0000442 /*
443 * If no bytes were started, return the error, and let the
444 * generic layer handle the completion.
445 */
446 if (requested_bytes == 0) {
447 nfs_direct_req_release(dreq);
448 return result < 0 ? result : -EIO;
449 }
450
Chuck Lever19f73782007-11-12 12:16:47 -0500451 if (put_dreq(dreq))
452 nfs_direct_complete(dreq);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000453 return 0;
Chuck Lever19f73782007-11-12 12:16:47 -0500454}
455
Chuck Leverc216fd72007-11-12 12:16:52 -0500456static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700457 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400459 ssize_t result = -ENOMEM;
Chuck Lever99514f82006-03-20 13:44:30 -0500460 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct nfs_direct_req *dreq;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400462 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Trond Myklebust607f31e2006-06-28 16:52:45 -0400464 dreq = nfs_direct_req_alloc();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400465 if (dreq == NULL)
466 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Chuck Lever91d5b472006-03-20 13:44:14 -0500468 dreq->inode = inode;
Peng Tao35754bc2012-09-25 14:55:57 +0800469 dreq->bytes_left = iov_length(iov, nr_segs);
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400470 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400471 l_ctx = nfs_get_lock_context(dreq->ctx);
472 if (IS_ERR(l_ctx)) {
473 result = PTR_ERR(l_ctx);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400474 goto out_release;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400475 }
476 dreq->l_ctx = l_ctx;
Chuck Lever487b8372006-03-20 13:44:30 -0500477 if (!is_sync_kiocb(iocb))
478 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Peng Tao7acdb022012-08-24 00:27:48 +0800480 NFS_I(inode)->read_io += iov_length(iov, nr_segs);
Mel Gormana564b8f2012-07-31 16:45:12 -0700481 result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400482 if (!result)
483 result = nfs_direct_wait(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400484out_release:
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400485 nfs_direct_req_release(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400486out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return result;
488}
489
Trond Myklebust1d59d612012-05-31 12:22:33 -0400490static void nfs_inode_dio_write_done(struct inode *inode)
491{
492 nfs_zap_mapping(inode, inode->i_mapping);
493 inode_dio_done(inode);
494}
495
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400496#if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
Trond Myklebustfad61492006-03-20 13:44:36 -0500497static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Fred Isaman1763da12012-04-20 14:47:57 -0400499 struct nfs_pageio_descriptor desc;
500 struct nfs_page *req, *tmp;
501 LIST_HEAD(reqs);
502 struct nfs_commit_info cinfo;
503 LIST_HEAD(failed);
504
505 nfs_init_cinfo_from_dreq(&cinfo, dreq);
506 pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
507 spin_lock(cinfo.lock);
508 nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
509 spin_unlock(cinfo.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Trond Myklebustfad61492006-03-20 13:44:36 -0500511 dreq->count = 0;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400512 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500513
Fred Isamanc95908e2012-07-18 14:20:50 -0400514 NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
Fred Isaman1763da12012-04-20 14:47:57 -0400515 &nfs_direct_write_completion_ops);
516 desc.pg_dreq = dreq;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400517
Fred Isaman1763da12012-04-20 14:47:57 -0400518 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
519 if (!nfs_pageio_add_request(&desc, req)) {
Trond Myklebust4035c242012-07-08 10:24:10 -0400520 nfs_list_remove_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400521 nfs_list_add_request(req, &failed);
522 spin_lock(cinfo.lock);
523 dreq->flags = 0;
524 dreq->error = -EIO;
525 spin_unlock(cinfo.lock);
526 }
Trond Myklebust5a695da2012-06-19 13:39:14 -0400527 nfs_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400528 }
529 nfs_pageio_complete(&desc);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400530
Trond Myklebust4035c242012-07-08 10:24:10 -0400531 while (!list_empty(&failed)) {
532 req = nfs_list_entry(failed.next);
533 nfs_list_remove_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400534 nfs_unlock_and_release_request(req);
Trond Myklebust4035c242012-07-08 10:24:10 -0400535 }
Trond Myklebust607f31e2006-06-28 16:52:45 -0400536
537 if (put_dreq(dreq))
Fred Isaman1763da12012-04-20 14:47:57 -0400538 nfs_direct_write_complete(dreq, dreq->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500539}
540
Fred Isaman1763da12012-04-20 14:47:57 -0400541static void nfs_direct_commit_complete(struct nfs_commit_data *data)
Trond Myklebustfad61492006-03-20 13:44:36 -0500542{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400543 struct nfs_direct_req *dreq = data->dreq;
Fred Isaman1763da12012-04-20 14:47:57 -0400544 struct nfs_commit_info cinfo;
545 struct nfs_page *req;
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400546 int status = data->task.tk_status;
547
Fred Isaman1763da12012-04-20 14:47:57 -0400548 nfs_init_cinfo_from_dreq(&cinfo, dreq);
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400549 if (status < 0) {
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400550 dprintk("NFS: %5u commit failed with error %d.\n",
Fred Isaman1763da12012-04-20 14:47:57 -0400551 data->task.tk_pid, status);
Trond Myklebustfad61492006-03-20 13:44:36 -0500552 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400553 } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400554 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500555 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
556 }
557
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400558 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
Fred Isaman1763da12012-04-20 14:47:57 -0400559 while (!list_empty(&data->pages)) {
560 req = nfs_list_entry(data->pages.next);
561 nfs_list_remove_request(req);
562 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
563 /* Note the rewrite will go through mds */
564 nfs_mark_request_commit(req, NULL, &cinfo);
Fred Isaman906369e2012-06-08 16:48:33 -0400565 } else
566 nfs_release_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400567 nfs_unlock_and_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400568 }
569
570 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
571 nfs_direct_write_complete(dreq, data->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500572}
573
Fred Isaman1763da12012-04-20 14:47:57 -0400574static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
575{
576 /* There is no lock to clear */
577}
578
579static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
580 .completion = nfs_direct_commit_complete,
581 .error_cleanup = nfs_direct_error_cleanup,
Trond Myklebustfad61492006-03-20 13:44:36 -0500582};
583
584static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
585{
Fred Isaman1763da12012-04-20 14:47:57 -0400586 int res;
587 struct nfs_commit_info cinfo;
588 LIST_HEAD(mds_list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500589
Fred Isaman1763da12012-04-20 14:47:57 -0400590 nfs_init_cinfo_from_dreq(&cinfo, dreq);
591 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
592 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
593 if (res < 0) /* res == -ENOMEM */
594 nfs_direct_write_reschedule(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500595}
596
Fred Isaman1763da12012-04-20 14:47:57 -0400597static void nfs_direct_write_schedule_work(struct work_struct *work)
Trond Myklebustfad61492006-03-20 13:44:36 -0500598{
Fred Isaman1763da12012-04-20 14:47:57 -0400599 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
Trond Myklebustfad61492006-03-20 13:44:36 -0500600 int flags = dreq->flags;
601
602 dreq->flags = 0;
603 switch (flags) {
604 case NFS_ODIRECT_DO_COMMIT:
605 nfs_direct_commit_schedule(dreq);
606 break;
607 case NFS_ODIRECT_RESCHED_WRITES:
608 nfs_direct_write_reschedule(dreq);
609 break;
610 default:
Trond Myklebust1d59d612012-05-31 12:22:33 -0400611 nfs_inode_dio_write_done(dreq->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500612 nfs_direct_complete(dreq);
613 }
614}
615
Fred Isaman1763da12012-04-20 14:47:57 -0400616static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
Trond Myklebustfad61492006-03-20 13:44:36 -0500617{
Fred Isaman1763da12012-04-20 14:47:57 -0400618 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
Trond Myklebustfad61492006-03-20 13:44:36 -0500619}
Fred Isaman1763da12012-04-20 14:47:57 -0400620
Trond Myklebustfad61492006-03-20 13:44:36 -0500621#else
Bryan Schumaker24fc9212012-04-30 13:27:11 -0400622static void nfs_direct_write_schedule_work(struct work_struct *work)
623{
624}
Trond Myklebustfad61492006-03-20 13:44:36 -0500625
626static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
627{
Trond Myklebust1d59d612012-05-31 12:22:33 -0400628 nfs_inode_dio_write_done(inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500629 nfs_direct_complete(dreq);
630}
631#endif
632
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400633/*
634 * NB: Return the value of the first error return code. Subsequent
635 * errors after the first one are ignored.
636 */
Chuck Lever462d5b32006-03-20 13:44:32 -0500637/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400638 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
639 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
640 * bail and stop sending more writes. Write length accounting is
641 * handled automatically by nfs_direct_write_result(). Otherwise, if
642 * no requests have been sent, just return an error.
Chuck Lever462d5b32006-03-20 13:44:32 -0500643 */
Fred Isaman1763da12012-04-20 14:47:57 -0400644static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
Chuck Lever02fe4942007-11-12 12:17:03 -0500645 const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700646 loff_t pos, bool uio)
Chuck Lever462d5b32006-03-20 13:44:32 -0500647{
Fred Isaman1763da12012-04-20 14:47:57 -0400648 struct nfs_direct_req *dreq = desc->pg_dreq;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500649 struct nfs_open_context *ctx = dreq->ctx;
Al Viro3d4ff432011-06-22 18:40:12 -0400650 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500651 unsigned long user_addr = (unsigned long)iov->iov_base;
652 size_t count = iov->iov_len;
Chuck Lever462d5b32006-03-20 13:44:32 -0500653 size_t wsize = NFS_SERVER(inode)->wsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400654 unsigned int pgbase;
655 int result;
656 ssize_t started = 0;
Fred Isaman1763da12012-04-20 14:47:57 -0400657 struct page **pagevec = NULL;
658 unsigned int npages;
Chuck Lever82b145c2006-06-20 12:57:03 -0400659
Chuck Lever462d5b32006-03-20 13:44:32 -0500660 do {
Chuck Lever462d5b32006-03-20 13:44:32 -0500661 size_t bytes;
Fred Isaman1763da12012-04-20 14:47:57 -0400662 int i;
Chuck Lever462d5b32006-03-20 13:44:32 -0500663
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700664 pgbase = user_addr & ~PAGE_MASK;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400665 bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700666
Trond Myklebust607f31e2006-06-28 16:52:45 -0400667 result = -ENOMEM;
Fred Isaman1763da12012-04-20 14:47:57 -0400668 npages = nfs_page_array_len(pgbase, bytes);
669 if (!pagevec)
670 pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
671 if (!pagevec)
Trond Myklebust607f31e2006-06-28 16:52:45 -0400672 break;
673
Mel Gormana564b8f2012-07-31 16:45:12 -0700674 if (uio) {
675 down_read(&current->mm->mmap_sem);
676 result = get_user_pages(current, current->mm, user_addr,
677 npages, 0, 0, pagevec, NULL);
678 up_read(&current->mm->mmap_sem);
679 if (result < 0)
680 break;
681 } else {
682 WARN_ON(npages != 1);
683 result = get_kernel_page(user_addr, 0, pagevec);
684 if (WARN_ON(result != 1))
685 break;
686 }
Fred Isaman1763da12012-04-20 14:47:57 -0400687
688 if ((unsigned)result < npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400689 bytes = result * PAGE_SIZE;
690 if (bytes <= pgbase) {
Fred Isaman1763da12012-04-20 14:47:57 -0400691 nfs_direct_release_pages(pagevec, result);
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400692 break;
693 }
694 bytes -= pgbase;
Fred Isaman1763da12012-04-20 14:47:57 -0400695 npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400696 }
697
Fred Isaman1763da12012-04-20 14:47:57 -0400698 for (i = 0; i < npages; i++) {
699 struct nfs_page *req;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400700 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400701
Fred Isaman1763da12012-04-20 14:47:57 -0400702 req = nfs_create_request(dreq->ctx, dreq->inode,
703 pagevec[i],
704 pgbase, req_len);
705 if (IS_ERR(req)) {
Fred Isaman1763da12012-04-20 14:47:57 -0400706 result = PTR_ERR(req);
707 break;
708 }
709 nfs_lock_request(req);
710 req->wb_index = pos >> PAGE_SHIFT;
711 req->wb_offset = pos & ~PAGE_MASK;
712 if (!nfs_pageio_add_request(desc, req)) {
713 result = desc->pg_error;
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400714 nfs_unlock_and_release_request(req);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400715 break;
Fred Isaman1763da12012-04-20 14:47:57 -0400716 }
717 pgbase = 0;
718 bytes -= req_len;
719 started += req_len;
720 user_addr += req_len;
721 pos += req_len;
722 count -= req_len;
Peng Tao35754bc2012-09-25 14:55:57 +0800723 dreq->bytes_left -= req_len;
Fred Isaman1763da12012-04-20 14:47:57 -0400724 }
Trond Myklebust6d747432012-04-30 13:27:31 -0400725 /* The nfs_page now hold references to these pages */
726 nfs_direct_release_pages(pagevec, npages);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400727 } while (count != 0 && result >= 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400728
Fred Isaman1763da12012-04-20 14:47:57 -0400729 kfree(pagevec);
730
Trond Myklebust607f31e2006-06-28 16:52:45 -0400731 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500732 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400733 return result < 0 ? (ssize_t) result : -EFAULT;
Chuck Lever462d5b32006-03-20 13:44:32 -0500734}
735
Fred Isaman1763da12012-04-20 14:47:57 -0400736static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
737{
738 struct nfs_direct_req *dreq = hdr->dreq;
739 struct nfs_commit_info cinfo;
740 int bit = -1;
741 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
742
743 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
744 goto out_put;
745
746 nfs_init_cinfo_from_dreq(&cinfo, dreq);
747
748 spin_lock(&dreq->lock);
749
750 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
751 dreq->flags = 0;
752 dreq->error = hdr->error;
753 }
754 if (dreq->error != 0)
755 bit = NFS_IOHDR_ERROR;
756 else {
757 dreq->count += hdr->good_bytes;
758 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
759 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
760 bit = NFS_IOHDR_NEED_RESCHED;
761 } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
762 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
763 bit = NFS_IOHDR_NEED_RESCHED;
764 else if (dreq->flags == 0) {
Trond Myklebust9bce0082012-06-05 18:32:03 -0400765 memcpy(&dreq->verf, hdr->verf,
Fred Isaman1763da12012-04-20 14:47:57 -0400766 sizeof(dreq->verf));
767 bit = NFS_IOHDR_NEED_COMMIT;
768 dreq->flags = NFS_ODIRECT_DO_COMMIT;
769 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
Trond Myklebust9bce0082012-06-05 18:32:03 -0400770 if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
Fred Isaman1763da12012-04-20 14:47:57 -0400771 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
772 bit = NFS_IOHDR_NEED_RESCHED;
773 } else
774 bit = NFS_IOHDR_NEED_COMMIT;
775 }
776 }
777 }
778 spin_unlock(&dreq->lock);
779
780 while (!list_empty(&hdr->pages)) {
781 req = nfs_list_entry(hdr->pages.next);
782 nfs_list_remove_request(req);
783 switch (bit) {
784 case NFS_IOHDR_NEED_RESCHED:
785 case NFS_IOHDR_NEED_COMMIT:
Trond Myklebust04277082012-05-09 13:54:53 -0400786 kref_get(&req->wb_kref);
Fred Isaman1763da12012-04-20 14:47:57 -0400787 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
Fred Isaman1763da12012-04-20 14:47:57 -0400788 }
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400789 nfs_unlock_and_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400790 }
791
792out_put:
793 if (put_dreq(dreq))
794 nfs_direct_write_complete(dreq, hdr->inode);
795 hdr->release(hdr);
796}
797
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400798static void nfs_write_sync_pgio_error(struct list_head *head)
799{
800 struct nfs_page *req;
801
802 while (!list_empty(head)) {
803 req = nfs_list_entry(head->next);
804 nfs_list_remove_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400805 nfs_unlock_and_release_request(req);
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400806 }
807}
808
Fred Isaman1763da12012-04-20 14:47:57 -0400809static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400810 .error_cleanup = nfs_write_sync_pgio_error,
Fred Isaman1763da12012-04-20 14:47:57 -0400811 .init_hdr = nfs_direct_pgio_init,
812 .completion = nfs_direct_write_completion,
813};
814
Chuck Lever19f73782007-11-12 12:16:47 -0500815static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
816 const struct iovec *iov,
817 unsigned long nr_segs,
Mel Gormana564b8f2012-07-31 16:45:12 -0700818 loff_t pos, bool uio)
Chuck Lever19f73782007-11-12 12:16:47 -0500819{
Fred Isaman1763da12012-04-20 14:47:57 -0400820 struct nfs_pageio_descriptor desc;
Trond Myklebust1d59d612012-05-31 12:22:33 -0400821 struct inode *inode = dreq->inode;
Chuck Lever19f73782007-11-12 12:16:47 -0500822 ssize_t result = 0;
823 size_t requested_bytes = 0;
824 unsigned long seg;
825
Fred Isamanc95908e2012-07-18 14:20:50 -0400826 NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
Fred Isaman1763da12012-04-20 14:47:57 -0400827 &nfs_direct_write_completion_ops);
828 desc.pg_dreq = dreq;
Chuck Lever19f73782007-11-12 12:16:47 -0500829 get_dreq(dreq);
Trond Myklebust1d59d612012-05-31 12:22:33 -0400830 atomic_inc(&inode->i_dio_count);
Chuck Lever19f73782007-11-12 12:16:47 -0500831
Peng Tao7acdb022012-08-24 00:27:48 +0800832 NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
Chuck Lever19f73782007-11-12 12:16:47 -0500833 for (seg = 0; seg < nr_segs; seg++) {
834 const struct iovec *vec = &iov[seg];
Mel Gormana564b8f2012-07-31 16:45:12 -0700835 result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
Chuck Lever19f73782007-11-12 12:16:47 -0500836 if (result < 0)
837 break;
838 requested_bytes += result;
839 if ((size_t)result < vec->iov_len)
840 break;
841 pos += vec->iov_len;
842 }
Fred Isaman1763da12012-04-20 14:47:57 -0400843 nfs_pageio_complete(&desc);
Chuck Lever19f73782007-11-12 12:16:47 -0500844
Chuck Lever839f7ad2011-01-21 15:54:57 +0000845 /*
846 * If no bytes were started, return the error, and let the
847 * generic layer handle the completion.
848 */
849 if (requested_bytes == 0) {
Trond Myklebust1d59d612012-05-31 12:22:33 -0400850 inode_dio_done(inode);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000851 nfs_direct_req_release(dreq);
852 return result < 0 ? result : -EIO;
853 }
854
Chuck Lever19f73782007-11-12 12:16:47 -0500855 if (put_dreq(dreq))
856 nfs_direct_write_complete(dreq, dreq->inode);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000857 return 0;
Chuck Lever19f73782007-11-12 12:16:47 -0500858}
859
Chuck Leverc216fd72007-11-12 12:16:52 -0500860static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
861 unsigned long nr_segs, loff_t pos,
Mel Gormana564b8f2012-07-31 16:45:12 -0700862 size_t count, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400864 ssize_t result = -ENOMEM;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500865 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500866 struct nfs_direct_req *dreq;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400867 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Trond Myklebust607f31e2006-06-28 16:52:45 -0400869 dreq = nfs_direct_req_alloc();
Chuck Lever462d5b32006-03-20 13:44:32 -0500870 if (!dreq)
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500873 dreq->inode = inode;
Peng Tao35754bc2012-09-25 14:55:57 +0800874 dreq->bytes_left = count;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400875 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400876 l_ctx = nfs_get_lock_context(dreq->ctx);
877 if (IS_ERR(l_ctx)) {
878 result = PTR_ERR(l_ctx);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400879 goto out_release;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400880 }
881 dreq->l_ctx = l_ctx;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500882 if (!is_sync_kiocb(iocb))
883 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Mel Gormana564b8f2012-07-31 16:45:12 -0700885 result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400886 if (!result)
887 result = nfs_direct_wait(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400888out_release:
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400889 nfs_direct_req_release(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400890out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return result;
892}
893
894/**
895 * nfs_file_direct_read - file direct read operation for NFS files
896 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700897 * @iov: vector of user buffers into which to read data
898 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500899 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *
901 * We use this function for direct reads instead of calling
902 * generic_file_aio_read() in order to avoid gfar's check to see if
903 * the request starts before the end of the file. For that check
904 * to work, we must generate a GETATTR before each direct read, and
905 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500906 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * to do all reads the application wants, and the server will take
908 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * This function also eliminates unnecessarily updating the file's
911 * atime locally, as the NFS server sets the file's atime, and this
912 * client must read the updated atime from the server back into its
913 * cache.
914 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700915ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700916 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500921 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Chuck Leverc216fd72007-11-12 12:16:52 -0500923 count = iov_length(iov, nr_segs);
924 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
925
Chuck Lever6da24bc2008-06-11 17:55:58 -0400926 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800927 file->f_path.dentry->d_parent->d_name.name,
928 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500929 count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 retval = 0;
932 if (!count)
933 goto out;
934
Trond Myklebust29884df2005-12-13 16:13:54 -0500935 retval = nfs_sync_mapping(mapping);
936 if (retval)
937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Konstantin Khlebnikov7ec10f22011-02-22 00:28:34 +0300939 task_io_account_read(count);
940
Mel Gormana564b8f2012-07-31 16:45:12 -0700941 retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500943 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945out:
946 return retval;
947}
948
949/**
950 * nfs_file_direct_write - file direct write operation for NFS files
951 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700952 * @iov: vector of user buffers from which to write data
953 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500954 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
956 * We use this function for direct writes instead of calling
957 * generic_file_aio_write() in order to avoid taking the inode
958 * semaphore and updating the i_size. The NFS server will set
959 * the new i_size and this client must read the updated size
960 * back into its cache. We let the server do generic write
961 * parameter checking and report problems.
962 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * We eliminate local atime updates, see direct read above.
964 *
965 * We avoid unnecessary page cache invalidations for normal cached
966 * readers of this file.
967 *
968 * Note that O_APPEND is not supported for NFS direct writes, as there
969 * is no atomic O_APPEND write facility in the NFS protocol.
970 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700971ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700972 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Chuck Lever070ea602007-05-19 17:22:52 -0400974 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500977 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Chuck Leverc216fd72007-11-12 12:16:52 -0500979 count = iov_length(iov, nr_segs);
980 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
981
Chuck Lever6da24bc2008-06-11 17:55:58 -0400982 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800983 file->f_path.dentry->d_parent->d_name.name,
984 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500985 count, (long long) pos);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700986
Chuck Leverce1a8e62005-11-30 18:08:17 -0500987 retval = generic_write_checks(file, &pos, &count, 0);
988 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500990
991 retval = -EINVAL;
992 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 retval = 0;
995 if (!count)
996 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500997
Trond Myklebust29884df2005-12-13 16:13:54 -0500998 retval = nfs_sync_mapping(mapping);
999 if (retval)
1000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Konstantin Khlebnikov7ec10f22011-02-22 00:28:34 +03001002 task_io_account_write(count);
1003
Mel Gormana564b8f2012-07-31 16:45:12 -07001004 retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
Fred Isaman1763da12012-04-20 14:47:57 -04001005 if (retval > 0) {
1006 struct inode *inode = mapping->host;
Chuck Lever9eafa8c2006-03-20 13:44:33 -05001007
Chuck Leverce1a8e62005-11-30 18:08:17 -05001008 iocb->ki_pos = pos + retval;
Fred Isaman1763da12012-04-20 14:47:57 -04001009 spin_lock(&inode->i_lock);
1010 if (i_size_read(inode) < iocb->ki_pos)
1011 i_size_write(inode, iocb->ki_pos);
1012 spin_unlock(&inode->i_lock);
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014out:
1015 return retval;
1016}
1017
Chuck Lever88467052006-03-20 13:44:34 -05001018/**
1019 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1020 *
1021 */
David Howellsf7b422b2006-06-09 09:34:33 -04001022int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
1025 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001026 0, (SLAB_RECLAIM_ACCOUNT|
1027 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001028 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (nfs_direct_cachep == NULL)
1030 return -ENOMEM;
1031
1032 return 0;
1033}
1034
Chuck Lever88467052006-03-20 13:44:34 -05001035/**
David Howellsf7b422b2006-06-09 09:34:33 -04001036 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -05001037 *
1038 */
David Brownell266bee82006-06-27 12:59:15 -07001039void nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001041 kmem_cache_destroy(nfs_direct_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}