blob: 4be8673ee18dbb464234772358f530ef0ced8989 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/nfs_fs.h>
51#include <linux/nfs_page.h>
52#include <linux/sunrpc/clnt.h>
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070055#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Trond Myklebust8d5658c2007-04-10 09:26:35 -040057#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050058#include "iostat.h"
Fred Isaman1763da12012-04-20 14:47:57 -040059#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Christoph Lametere18b8902006-12-06 20:33:20 -080063static struct kmem_cache *nfs_direct_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * This represents a set of asynchronous requests that we're waiting on
67 */
68struct nfs_direct_req {
69 struct kref kref; /* release manager */
Chuck Lever15ce4a02006-03-20 13:44:34 -050070
71 /* I/O parameters */
Trond Myklebusta8881f52006-03-20 13:44:36 -050072 struct nfs_open_context *ctx; /* file open context info */
Trond Myklebustf11ac8d2010-06-25 16:35:53 -040073 struct nfs_lock_context *l_ctx; /* Lock context info */
Chuck Lever99514f82006-03-20 13:44:30 -050074 struct kiocb * iocb; /* controlling i/o request */
Chuck Lever88467052006-03-20 13:44:34 -050075 struct inode * inode; /* target file of i/o */
Chuck Lever15ce4a02006-03-20 13:44:34 -050076
77 /* completion state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040078 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050079 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050080 ssize_t count, /* bytes actually processed */
Peng Tao35754bc2012-09-25 14:55:57 +080081 bytes_left, /* bytes left to be sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050083 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050084
85 /* commit state */
Fred Isaman1763da12012-04-20 14:47:57 -040086 struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
87 struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
88 struct work_struct work;
Trond Myklebustfad61492006-03-20 13:44:36 -050089 int flags;
90#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
91#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
92 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
Fred Isaman1763da12012-04-20 14:47:57 -040095static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
96static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
Trond Myklebustfad61492006-03-20 13:44:36 -050097static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Fred Isaman1763da12012-04-20 14:47:57 -040098static void nfs_direct_write_schedule_work(struct work_struct *work);
Trond Myklebust607f31e2006-06-28 16:52:45 -040099
100static inline void get_dreq(struct nfs_direct_req *dreq)
101{
102 atomic_inc(&dreq->io_count);
103}
104
105static inline int put_dreq(struct nfs_direct_req *dreq)
106{
107 return atomic_dec_and_test(&dreq->io_count);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500111 * nfs_direct_IO - NFS address space operation for direct I/O
112 * @rw: direction (read or write)
113 * @iocb: target I/O control block
114 * @iov: array of vectors that define I/O buffer
115 * @pos: offset in file to begin the operation
116 * @nr_segs: size of iovec array
117 *
118 * The presence of this routine in the address space ops vector means
Mel Gormana564b8f2012-07-31 16:45:12 -0700119 * the NFS client supports direct I/O. However, for most direct IO, we
120 * shunt off direct read and write requests before the VFS gets them,
121 * so this method is only ever called for swap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500123ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
124{
Mel Gormana564b8f2012-07-31 16:45:12 -0700125#ifndef CONFIG_NFS_SWAP
Chuck Leverb8a32e22006-03-20 13:44:28 -0500126 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800127 iocb->ki_filp->f_path.dentry->d_name.name,
Trond Myklebuste99170f2006-04-18 13:21:42 -0400128 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500129
130 return -EINVAL;
Mel Gormana564b8f2012-07-31 16:45:12 -0700131#else
132 VM_BUG_ON(iocb->ki_left != PAGE_SIZE);
133 VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
134
135 if (rw == READ || rw == KERNEL_READ)
136 return nfs_file_direct_read(iocb, iov, nr_segs, pos,
137 rw == READ ? true : false);
138 return nfs_file_direct_write(iocb, iov, nr_segs, pos,
139 rw == WRITE ? true : false);
140#endif /* CONFIG_NFS_SWAP */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500141}
142
Chuck Lever749e146e2007-05-19 17:22:46 -0400143static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
Chuck Lever9c93ab72006-06-20 12:56:31 -0400144{
Chuck Lever749e146e2007-05-19 17:22:46 -0400145 unsigned int i;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400146 for (i = 0; i < npages; i++)
147 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500148}
149
Fred Isaman1763da12012-04-20 14:47:57 -0400150void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
151 struct nfs_direct_req *dreq)
152{
153 cinfo->lock = &dreq->lock;
154 cinfo->mds = &dreq->mds_cinfo;
155 cinfo->ds = &dreq->ds_cinfo;
156 cinfo->dreq = dreq;
157 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
158}
159
Chuck Lever93619e52006-03-20 13:44:31 -0500160static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Trond Myklebust292f3ee2012-04-30 18:31:49 -0400164 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!dreq)
166 return NULL;
167
168 kref_init(&dreq->kref);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400169 kref_get(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500170 init_completion(&dreq->completion);
Fred Isaman1763da12012-04-20 14:47:57 -0400171 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
172 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500173 spin_lock_init(&dreq->lock);
Chuck Lever93619e52006-03-20 13:44:31 -0500174
175 return dreq;
176}
177
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400178static void nfs_direct_req_free(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500181
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400182 if (dreq->l_ctx != NULL)
183 nfs_put_lock_context(dreq->l_ctx);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500184 if (dreq->ctx != NULL)
185 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kmem_cache_free(nfs_direct_cachep, dreq);
187}
188
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400189static void nfs_direct_req_release(struct nfs_direct_req *dreq)
190{
191 kref_put(&dreq->kref, nfs_direct_req_free);
192}
193
Chuck Leverd4cc9482006-03-20 13:44:28 -0500194/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500195 * Collects and returns the final error value/byte-count.
196 */
197static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
198{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500199 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500200
201 /* Async requests don't wait here */
202 if (dreq->iocb)
203 goto out;
204
Matthew Wilcox150030b2007-12-06 16:24:39 -0500205 result = wait_for_completion_killable(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500206
207 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500208 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500209 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500210 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500211
212out:
Chuck Leverbc0fb202006-03-20 13:44:31 -0500213 return (ssize_t) result;
214}
215
216/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400217 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
218 * the iocb is still valid here if this is a synchronous request.
Chuck Lever63ab46a2006-03-20 13:44:31 -0500219 */
220static void nfs_direct_complete(struct nfs_direct_req *dreq)
221{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500222 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500223 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500224 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500225 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500226 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500227 }
228 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500229
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400230 nfs_direct_req_release(dreq);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500231}
232
Trond Myklebust1385b812012-05-04 13:54:24 -0400233static void nfs_direct_readpage_release(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Fred Isaman584aa812012-04-20 14:47:51 -0400235 dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
236 req->wb_context->dentry->d_inode->i_sb->s_id,
237 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
238 req->wb_bytes,
239 (long long)req_offset(req));
240 nfs_release_request(req);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400241}
242
Fred Isaman584aa812012-04-20 14:47:51 -0400243static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400244{
Fred Isaman584aa812012-04-20 14:47:51 -0400245 unsigned long bytes = 0;
246 struct nfs_direct_req *dreq = hdr->dreq;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400247
Fred Isaman584aa812012-04-20 14:47:51 -0400248 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
249 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Chuck Lever15ce4a02006-03-20 13:44:34 -0500251 spin_lock(&dreq->lock);
Fred Isaman584aa812012-04-20 14:47:51 -0400252 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
253 dreq->error = hdr->error;
254 else
255 dreq->count += hdr->good_bytes;
256 spin_unlock(&dreq->lock);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400257
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400258 while (!list_empty(&hdr->pages)) {
259 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
260 struct page *page = req->wb_page;
Fred Isaman584aa812012-04-20 14:47:51 -0400261
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400262 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
263 if (bytes > hdr->good_bytes)
264 zero_user(page, 0, PAGE_SIZE);
265 else if (hdr->good_bytes - bytes < PAGE_SIZE)
266 zero_user_segment(page,
267 hdr->good_bytes & ~PAGE_MASK,
268 PAGE_SIZE);
269 }
270 if (!PageCompound(page)) {
271 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
272 if (bytes < hdr->good_bytes)
273 set_page_dirty(page);
274 } else
Fred Isaman584aa812012-04-20 14:47:51 -0400275 set_page_dirty(page);
Fred Isaman584aa812012-04-20 14:47:51 -0400276 }
Trond Myklebust4bd8b012012-05-01 12:49:58 -0400277 bytes += req->wb_bytes;
278 nfs_list_remove_request(req);
279 nfs_direct_readpage_release(req);
Fred Isaman584aa812012-04-20 14:47:51 -0400280 }
281out_put:
Trond Myklebust607f31e2006-06-28 16:52:45 -0400282 if (put_dreq(dreq))
283 nfs_direct_complete(dreq);
Fred Isaman584aa812012-04-20 14:47:51 -0400284 hdr->release(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400287static void nfs_read_sync_pgio_error(struct list_head *head)
Fred Isamancd841602012-04-20 14:47:44 -0400288{
Fred Isaman584aa812012-04-20 14:47:51 -0400289 struct nfs_page *req;
Fred Isamancd841602012-04-20 14:47:44 -0400290
Fred Isaman584aa812012-04-20 14:47:51 -0400291 while (!list_empty(head)) {
292 req = nfs_list_entry(head->next);
293 nfs_list_remove_request(req);
294 nfs_release_request(req);
295 }
Fred Isamancd841602012-04-20 14:47:44 -0400296}
297
Fred Isaman584aa812012-04-20 14:47:51 -0400298static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
299{
300 get_dreq(hdr->dreq);
301}
302
303static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400304 .error_cleanup = nfs_read_sync_pgio_error,
Fred Isaman584aa812012-04-20 14:47:51 -0400305 .init_hdr = nfs_direct_pgio_init,
306 .completion = nfs_direct_read_completion,
307};
308
Chuck Leverd4cc9482006-03-20 13:44:28 -0500309/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400310 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
311 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
312 * bail and stop sending more reads. Read length accounting is
313 * handled automatically by nfs_direct_read_result(). Otherwise, if
314 * no requests have been sent, just return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Fred Isaman584aa812012-04-20 14:47:51 -0400316static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
Chuck Lever02fe4942007-11-12 12:17:03 -0500317 const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700318 loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Fred Isaman584aa812012-04-20 14:47:51 -0400320 struct nfs_direct_req *dreq = desc->pg_dreq;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500321 struct nfs_open_context *ctx = dreq->ctx;
Al Viro3d4ff432011-06-22 18:40:12 -0400322 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500323 unsigned long user_addr = (unsigned long)iov->iov_base;
324 size_t count = iov->iov_len;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500325 size_t rsize = NFS_SERVER(inode)->rsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400326 unsigned int pgbase;
327 int result;
328 ssize_t started = 0;
Fred Isaman584aa812012-04-20 14:47:51 -0400329 struct page **pagevec = NULL;
330 unsigned int npages;
Chuck Lever82b145c2006-06-20 12:57:03 -0400331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 do {
Chuck Lever5dd602f2006-03-20 13:44:29 -0500333 size_t bytes;
Fred Isaman584aa812012-04-20 14:47:51 -0400334 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700336 pgbase = user_addr & ~PAGE_MASK;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400337 bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700338
Trond Myklebust607f31e2006-06-28 16:52:45 -0400339 result = -ENOMEM;
Fred Isaman584aa812012-04-20 14:47:51 -0400340 npages = nfs_page_array_len(pgbase, bytes);
341 if (!pagevec)
342 pagevec = kmalloc(npages * sizeof(struct page *),
343 GFP_KERNEL);
344 if (!pagevec)
Trond Myklebust607f31e2006-06-28 16:52:45 -0400345 break;
Mel Gormana564b8f2012-07-31 16:45:12 -0700346 if (uio) {
347 down_read(&current->mm->mmap_sem);
348 result = get_user_pages(current, current->mm, user_addr,
Fred Isaman584aa812012-04-20 14:47:51 -0400349 npages, 1, 0, pagevec, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -0700350 up_read(&current->mm->mmap_sem);
351 if (result < 0)
352 break;
353 } else {
354 WARN_ON(npages != 1);
355 result = get_kernel_page(user_addr, 1, pagevec);
356 if (WARN_ON(result != 1))
357 break;
358 }
359
Fred Isaman584aa812012-04-20 14:47:51 -0400360 if ((unsigned)result < npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400361 bytes = result * PAGE_SIZE;
362 if (bytes <= pgbase) {
Fred Isaman584aa812012-04-20 14:47:51 -0400363 nfs_direct_release_pages(pagevec, result);
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400364 break;
365 }
366 bytes -= pgbase;
Fred Isaman584aa812012-04-20 14:47:51 -0400367 npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400368 }
Chuck Lever06cf6f22006-06-20 12:56:49 -0400369
Fred Isaman584aa812012-04-20 14:47:51 -0400370 for (i = 0; i < npages; i++) {
371 struct nfs_page *req;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400372 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
Fred Isaman584aa812012-04-20 14:47:51 -0400373 /* XXX do we need to do the eof zeroing found in async_filler? */
374 req = nfs_create_request(dreq->ctx, dreq->inode,
375 pagevec[i],
376 pgbase, req_len);
377 if (IS_ERR(req)) {
Fred Isaman584aa812012-04-20 14:47:51 -0400378 result = PTR_ERR(req);
379 break;
380 }
381 req->wb_index = pos >> PAGE_SHIFT;
382 req->wb_offset = pos & ~PAGE_MASK;
383 if (!nfs_pageio_add_request(desc, req)) {
384 result = desc->pg_error;
385 nfs_release_request(req);
Fred Isaman584aa812012-04-20 14:47:51 -0400386 break;
387 }
388 pgbase = 0;
389 bytes -= req_len;
390 started += req_len;
391 user_addr += req_len;
392 pos += req_len;
393 count -= req_len;
Peng Tao35754bc2012-09-25 14:55:57 +0800394 dreq->bytes_left -= req_len;
Fred Isaman584aa812012-04-20 14:47:51 -0400395 }
Trond Myklebust6d747432012-04-30 13:27:31 -0400396 /* The nfs_page now hold references to these pages */
397 nfs_direct_release_pages(pagevec, npages);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400398 } while (count != 0 && result >= 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400399
Fred Isaman584aa812012-04-20 14:47:51 -0400400 kfree(pagevec);
401
Trond Myklebust607f31e2006-06-28 16:52:45 -0400402 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500403 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400404 return result < 0 ? (ssize_t) result : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Chuck Lever19f73782007-11-12 12:16:47 -0500407static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
408 const struct iovec *iov,
409 unsigned long nr_segs,
Mel Gormana564b8f2012-07-31 16:45:12 -0700410 loff_t pos, bool uio)
Chuck Lever19f73782007-11-12 12:16:47 -0500411{
Fred Isaman584aa812012-04-20 14:47:51 -0400412 struct nfs_pageio_descriptor desc;
Chuck Lever19f73782007-11-12 12:16:47 -0500413 ssize_t result = -EINVAL;
414 size_t requested_bytes = 0;
415 unsigned long seg;
416
Fred Isaman59948db2012-07-18 14:20:49 -0400417 NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
Fred Isaman584aa812012-04-20 14:47:51 -0400418 &nfs_direct_read_completion_ops);
Chuck Lever19f73782007-11-12 12:16:47 -0500419 get_dreq(dreq);
Fred Isaman584aa812012-04-20 14:47:51 -0400420 desc.pg_dreq = dreq;
Chuck Lever19f73782007-11-12 12:16:47 -0500421
422 for (seg = 0; seg < nr_segs; seg++) {
423 const struct iovec *vec = &iov[seg];
Mel Gormana564b8f2012-07-31 16:45:12 -0700424 result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
Chuck Lever19f73782007-11-12 12:16:47 -0500425 if (result < 0)
426 break;
427 requested_bytes += result;
428 if ((size_t)result < vec->iov_len)
429 break;
430 pos += vec->iov_len;
431 }
432
Fred Isaman584aa812012-04-20 14:47:51 -0400433 nfs_pageio_complete(&desc);
434
Chuck Lever839f7ad2011-01-21 15:54:57 +0000435 /*
436 * If no bytes were started, return the error, and let the
437 * generic layer handle the completion.
438 */
439 if (requested_bytes == 0) {
440 nfs_direct_req_release(dreq);
441 return result < 0 ? result : -EIO;
442 }
443
Chuck Lever19f73782007-11-12 12:16:47 -0500444 if (put_dreq(dreq))
445 nfs_direct_complete(dreq);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000446 return 0;
Chuck Lever19f73782007-11-12 12:16:47 -0500447}
448
Chuck Leverc216fd72007-11-12 12:16:52 -0500449static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700450 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400452 ssize_t result = -ENOMEM;
Chuck Lever99514f82006-03-20 13:44:30 -0500453 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct nfs_direct_req *dreq;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400455 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Trond Myklebust607f31e2006-06-28 16:52:45 -0400457 dreq = nfs_direct_req_alloc();
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400458 if (dreq == NULL)
459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Chuck Lever91d5b472006-03-20 13:44:14 -0500461 dreq->inode = inode;
Peng Tao35754bc2012-09-25 14:55:57 +0800462 dreq->bytes_left = iov_length(iov, nr_segs);
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400463 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400464 l_ctx = nfs_get_lock_context(dreq->ctx);
465 if (IS_ERR(l_ctx)) {
466 result = PTR_ERR(l_ctx);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400467 goto out_release;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400468 }
469 dreq->l_ctx = l_ctx;
Chuck Lever487b8372006-03-20 13:44:30 -0500470 if (!is_sync_kiocb(iocb))
471 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Peng Tao7acdb022012-08-24 00:27:48 +0800473 NFS_I(inode)->read_io += iov_length(iov, nr_segs);
Mel Gormana564b8f2012-07-31 16:45:12 -0700474 result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400475 if (!result)
476 result = nfs_direct_wait(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400477out_release:
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400478 nfs_direct_req_release(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400479out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return result;
481}
482
Trond Myklebust1d59d612012-05-31 12:22:33 -0400483static void nfs_inode_dio_write_done(struct inode *inode)
484{
485 nfs_zap_mapping(inode, inode->i_mapping);
486 inode_dio_done(inode);
487}
488
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400489#if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
Trond Myklebustfad61492006-03-20 13:44:36 -0500490static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Fred Isaman1763da12012-04-20 14:47:57 -0400492 struct nfs_pageio_descriptor desc;
493 struct nfs_page *req, *tmp;
494 LIST_HEAD(reqs);
495 struct nfs_commit_info cinfo;
496 LIST_HEAD(failed);
497
498 nfs_init_cinfo_from_dreq(&cinfo, dreq);
499 pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
500 spin_lock(cinfo.lock);
501 nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
502 spin_unlock(cinfo.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Trond Myklebustfad61492006-03-20 13:44:36 -0500504 dreq->count = 0;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400505 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500506
Fred Isamanc95908e2012-07-18 14:20:50 -0400507 NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
Fred Isaman1763da12012-04-20 14:47:57 -0400508 &nfs_direct_write_completion_ops);
509 desc.pg_dreq = dreq;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400510
Fred Isaman1763da12012-04-20 14:47:57 -0400511 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
512 if (!nfs_pageio_add_request(&desc, req)) {
Trond Myklebust4035c242012-07-08 10:24:10 -0400513 nfs_list_remove_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400514 nfs_list_add_request(req, &failed);
515 spin_lock(cinfo.lock);
516 dreq->flags = 0;
517 dreq->error = -EIO;
518 spin_unlock(cinfo.lock);
519 }
Trond Myklebust5a695da2012-06-19 13:39:14 -0400520 nfs_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400521 }
522 nfs_pageio_complete(&desc);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400523
Trond Myklebust4035c242012-07-08 10:24:10 -0400524 while (!list_empty(&failed)) {
525 req = nfs_list_entry(failed.next);
526 nfs_list_remove_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400527 nfs_unlock_and_release_request(req);
Trond Myklebust4035c242012-07-08 10:24:10 -0400528 }
Trond Myklebust607f31e2006-06-28 16:52:45 -0400529
530 if (put_dreq(dreq))
Fred Isaman1763da12012-04-20 14:47:57 -0400531 nfs_direct_write_complete(dreq, dreq->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500532}
533
Fred Isaman1763da12012-04-20 14:47:57 -0400534static void nfs_direct_commit_complete(struct nfs_commit_data *data)
Trond Myklebustfad61492006-03-20 13:44:36 -0500535{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400536 struct nfs_direct_req *dreq = data->dreq;
Fred Isaman1763da12012-04-20 14:47:57 -0400537 struct nfs_commit_info cinfo;
538 struct nfs_page *req;
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400539 int status = data->task.tk_status;
540
Fred Isaman1763da12012-04-20 14:47:57 -0400541 nfs_init_cinfo_from_dreq(&cinfo, dreq);
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400542 if (status < 0) {
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400543 dprintk("NFS: %5u commit failed with error %d.\n",
Fred Isaman1763da12012-04-20 14:47:57 -0400544 data->task.tk_pid, status);
Trond Myklebustfad61492006-03-20 13:44:36 -0500545 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400546 } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400547 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500548 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
549 }
550
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400551 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
Fred Isaman1763da12012-04-20 14:47:57 -0400552 while (!list_empty(&data->pages)) {
553 req = nfs_list_entry(data->pages.next);
554 nfs_list_remove_request(req);
555 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
556 /* Note the rewrite will go through mds */
557 nfs_mark_request_commit(req, NULL, &cinfo);
Fred Isaman906369e2012-06-08 16:48:33 -0400558 } else
559 nfs_release_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400560 nfs_unlock_and_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400561 }
562
563 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
564 nfs_direct_write_complete(dreq, data->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500565}
566
Fred Isaman1763da12012-04-20 14:47:57 -0400567static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
568{
569 /* There is no lock to clear */
570}
571
572static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
573 .completion = nfs_direct_commit_complete,
574 .error_cleanup = nfs_direct_error_cleanup,
Trond Myklebustfad61492006-03-20 13:44:36 -0500575};
576
577static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
578{
Fred Isaman1763da12012-04-20 14:47:57 -0400579 int res;
580 struct nfs_commit_info cinfo;
581 LIST_HEAD(mds_list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500582
Fred Isaman1763da12012-04-20 14:47:57 -0400583 nfs_init_cinfo_from_dreq(&cinfo, dreq);
584 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
585 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
586 if (res < 0) /* res == -ENOMEM */
587 nfs_direct_write_reschedule(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500588}
589
Fred Isaman1763da12012-04-20 14:47:57 -0400590static void nfs_direct_write_schedule_work(struct work_struct *work)
Trond Myklebustfad61492006-03-20 13:44:36 -0500591{
Fred Isaman1763da12012-04-20 14:47:57 -0400592 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
Trond Myklebustfad61492006-03-20 13:44:36 -0500593 int flags = dreq->flags;
594
595 dreq->flags = 0;
596 switch (flags) {
597 case NFS_ODIRECT_DO_COMMIT:
598 nfs_direct_commit_schedule(dreq);
599 break;
600 case NFS_ODIRECT_RESCHED_WRITES:
601 nfs_direct_write_reschedule(dreq);
602 break;
603 default:
Trond Myklebust1d59d612012-05-31 12:22:33 -0400604 nfs_inode_dio_write_done(dreq->inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500605 nfs_direct_complete(dreq);
606 }
607}
608
Fred Isaman1763da12012-04-20 14:47:57 -0400609static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
Trond Myklebustfad61492006-03-20 13:44:36 -0500610{
Fred Isaman1763da12012-04-20 14:47:57 -0400611 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
Trond Myklebustfad61492006-03-20 13:44:36 -0500612}
Fred Isaman1763da12012-04-20 14:47:57 -0400613
Trond Myklebustfad61492006-03-20 13:44:36 -0500614#else
Bryan Schumaker24fc9212012-04-30 13:27:11 -0400615static void nfs_direct_write_schedule_work(struct work_struct *work)
616{
617}
Trond Myklebustfad61492006-03-20 13:44:36 -0500618
619static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
620{
Trond Myklebust1d59d612012-05-31 12:22:33 -0400621 nfs_inode_dio_write_done(inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500622 nfs_direct_complete(dreq);
623}
624#endif
625
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400626/*
627 * NB: Return the value of the first error return code. Subsequent
628 * errors after the first one are ignored.
629 */
Chuck Lever462d5b32006-03-20 13:44:32 -0500630/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400631 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
632 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
633 * bail and stop sending more writes. Write length accounting is
634 * handled automatically by nfs_direct_write_result(). Otherwise, if
635 * no requests have been sent, just return an error.
Chuck Lever462d5b32006-03-20 13:44:32 -0500636 */
Fred Isaman1763da12012-04-20 14:47:57 -0400637static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
Chuck Lever02fe4942007-11-12 12:17:03 -0500638 const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700639 loff_t pos, bool uio)
Chuck Lever462d5b32006-03-20 13:44:32 -0500640{
Fred Isaman1763da12012-04-20 14:47:57 -0400641 struct nfs_direct_req *dreq = desc->pg_dreq;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500642 struct nfs_open_context *ctx = dreq->ctx;
Al Viro3d4ff432011-06-22 18:40:12 -0400643 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500644 unsigned long user_addr = (unsigned long)iov->iov_base;
645 size_t count = iov->iov_len;
Chuck Lever462d5b32006-03-20 13:44:32 -0500646 size_t wsize = NFS_SERVER(inode)->wsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400647 unsigned int pgbase;
648 int result;
649 ssize_t started = 0;
Fred Isaman1763da12012-04-20 14:47:57 -0400650 struct page **pagevec = NULL;
651 unsigned int npages;
Chuck Lever82b145c2006-06-20 12:57:03 -0400652
Chuck Lever462d5b32006-03-20 13:44:32 -0500653 do {
Chuck Lever462d5b32006-03-20 13:44:32 -0500654 size_t bytes;
Fred Isaman1763da12012-04-20 14:47:57 -0400655 int i;
Chuck Lever462d5b32006-03-20 13:44:32 -0500656
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700657 pgbase = user_addr & ~PAGE_MASK;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400658 bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700659
Trond Myklebust607f31e2006-06-28 16:52:45 -0400660 result = -ENOMEM;
Fred Isaman1763da12012-04-20 14:47:57 -0400661 npages = nfs_page_array_len(pgbase, bytes);
662 if (!pagevec)
663 pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
664 if (!pagevec)
Trond Myklebust607f31e2006-06-28 16:52:45 -0400665 break;
666
Mel Gormana564b8f2012-07-31 16:45:12 -0700667 if (uio) {
668 down_read(&current->mm->mmap_sem);
669 result = get_user_pages(current, current->mm, user_addr,
670 npages, 0, 0, pagevec, NULL);
671 up_read(&current->mm->mmap_sem);
672 if (result < 0)
673 break;
674 } else {
675 WARN_ON(npages != 1);
676 result = get_kernel_page(user_addr, 0, pagevec);
677 if (WARN_ON(result != 1))
678 break;
679 }
Fred Isaman1763da12012-04-20 14:47:57 -0400680
681 if ((unsigned)result < npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400682 bytes = result * PAGE_SIZE;
683 if (bytes <= pgbase) {
Fred Isaman1763da12012-04-20 14:47:57 -0400684 nfs_direct_release_pages(pagevec, result);
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400685 break;
686 }
687 bytes -= pgbase;
Fred Isaman1763da12012-04-20 14:47:57 -0400688 npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400689 }
690
Fred Isaman1763da12012-04-20 14:47:57 -0400691 for (i = 0; i < npages; i++) {
692 struct nfs_page *req;
Trond Myklebustbf5fc402012-05-04 13:47:16 -0400693 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400694
Fred Isaman1763da12012-04-20 14:47:57 -0400695 req = nfs_create_request(dreq->ctx, dreq->inode,
696 pagevec[i],
697 pgbase, req_len);
698 if (IS_ERR(req)) {
Fred Isaman1763da12012-04-20 14:47:57 -0400699 result = PTR_ERR(req);
700 break;
701 }
702 nfs_lock_request(req);
703 req->wb_index = pos >> PAGE_SHIFT;
704 req->wb_offset = pos & ~PAGE_MASK;
705 if (!nfs_pageio_add_request(desc, req)) {
706 result = desc->pg_error;
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400707 nfs_unlock_and_release_request(req);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400708 break;
Fred Isaman1763da12012-04-20 14:47:57 -0400709 }
710 pgbase = 0;
711 bytes -= req_len;
712 started += req_len;
713 user_addr += req_len;
714 pos += req_len;
715 count -= req_len;
Peng Tao35754bc2012-09-25 14:55:57 +0800716 dreq->bytes_left -= req_len;
Fred Isaman1763da12012-04-20 14:47:57 -0400717 }
Trond Myklebust6d747432012-04-30 13:27:31 -0400718 /* The nfs_page now hold references to these pages */
719 nfs_direct_release_pages(pagevec, npages);
Trond Myklebust71e8cc02012-04-30 13:22:54 -0400720 } while (count != 0 && result >= 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400721
Fred Isaman1763da12012-04-20 14:47:57 -0400722 kfree(pagevec);
723
Trond Myklebust607f31e2006-06-28 16:52:45 -0400724 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500725 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400726 return result < 0 ? (ssize_t) result : -EFAULT;
Chuck Lever462d5b32006-03-20 13:44:32 -0500727}
728
Fred Isaman1763da12012-04-20 14:47:57 -0400729static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
730{
731 struct nfs_direct_req *dreq = hdr->dreq;
732 struct nfs_commit_info cinfo;
733 int bit = -1;
734 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
735
736 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
737 goto out_put;
738
739 nfs_init_cinfo_from_dreq(&cinfo, dreq);
740
741 spin_lock(&dreq->lock);
742
743 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
744 dreq->flags = 0;
745 dreq->error = hdr->error;
746 }
747 if (dreq->error != 0)
748 bit = NFS_IOHDR_ERROR;
749 else {
750 dreq->count += hdr->good_bytes;
751 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
752 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
753 bit = NFS_IOHDR_NEED_RESCHED;
754 } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
755 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
756 bit = NFS_IOHDR_NEED_RESCHED;
757 else if (dreq->flags == 0) {
Trond Myklebust9bce0082012-06-05 18:32:03 -0400758 memcpy(&dreq->verf, hdr->verf,
Fred Isaman1763da12012-04-20 14:47:57 -0400759 sizeof(dreq->verf));
760 bit = NFS_IOHDR_NEED_COMMIT;
761 dreq->flags = NFS_ODIRECT_DO_COMMIT;
762 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
Trond Myklebust9bce0082012-06-05 18:32:03 -0400763 if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
Fred Isaman1763da12012-04-20 14:47:57 -0400764 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
765 bit = NFS_IOHDR_NEED_RESCHED;
766 } else
767 bit = NFS_IOHDR_NEED_COMMIT;
768 }
769 }
770 }
771 spin_unlock(&dreq->lock);
772
773 while (!list_empty(&hdr->pages)) {
774 req = nfs_list_entry(hdr->pages.next);
775 nfs_list_remove_request(req);
776 switch (bit) {
777 case NFS_IOHDR_NEED_RESCHED:
778 case NFS_IOHDR_NEED_COMMIT:
Trond Myklebust04277082012-05-09 13:54:53 -0400779 kref_get(&req->wb_kref);
Fred Isaman1763da12012-04-20 14:47:57 -0400780 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
Fred Isaman1763da12012-04-20 14:47:57 -0400781 }
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400782 nfs_unlock_and_release_request(req);
Fred Isaman1763da12012-04-20 14:47:57 -0400783 }
784
785out_put:
786 if (put_dreq(dreq))
787 nfs_direct_write_complete(dreq, hdr->inode);
788 hdr->release(hdr);
789}
790
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400791static void nfs_write_sync_pgio_error(struct list_head *head)
792{
793 struct nfs_page *req;
794
795 while (!list_empty(head)) {
796 req = nfs_list_entry(head->next);
797 nfs_list_remove_request(req);
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400798 nfs_unlock_and_release_request(req);
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400799 }
800}
801
Fred Isaman1763da12012-04-20 14:47:57 -0400802static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
Trond Myklebust3e9e0ca32012-04-30 13:40:06 -0400803 .error_cleanup = nfs_write_sync_pgio_error,
Fred Isaman1763da12012-04-20 14:47:57 -0400804 .init_hdr = nfs_direct_pgio_init,
805 .completion = nfs_direct_write_completion,
806};
807
Chuck Lever19f73782007-11-12 12:16:47 -0500808static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
809 const struct iovec *iov,
810 unsigned long nr_segs,
Mel Gormana564b8f2012-07-31 16:45:12 -0700811 loff_t pos, bool uio)
Chuck Lever19f73782007-11-12 12:16:47 -0500812{
Fred Isaman1763da12012-04-20 14:47:57 -0400813 struct nfs_pageio_descriptor desc;
Trond Myklebust1d59d612012-05-31 12:22:33 -0400814 struct inode *inode = dreq->inode;
Chuck Lever19f73782007-11-12 12:16:47 -0500815 ssize_t result = 0;
816 size_t requested_bytes = 0;
817 unsigned long seg;
818
Fred Isamanc95908e2012-07-18 14:20:50 -0400819 NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
Fred Isaman1763da12012-04-20 14:47:57 -0400820 &nfs_direct_write_completion_ops);
821 desc.pg_dreq = dreq;
Chuck Lever19f73782007-11-12 12:16:47 -0500822 get_dreq(dreq);
Trond Myklebust1d59d612012-05-31 12:22:33 -0400823 atomic_inc(&inode->i_dio_count);
Chuck Lever19f73782007-11-12 12:16:47 -0500824
Peng Tao7acdb022012-08-24 00:27:48 +0800825 NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
Chuck Lever19f73782007-11-12 12:16:47 -0500826 for (seg = 0; seg < nr_segs; seg++) {
827 const struct iovec *vec = &iov[seg];
Mel Gormana564b8f2012-07-31 16:45:12 -0700828 result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
Chuck Lever19f73782007-11-12 12:16:47 -0500829 if (result < 0)
830 break;
831 requested_bytes += result;
832 if ((size_t)result < vec->iov_len)
833 break;
834 pos += vec->iov_len;
835 }
Fred Isaman1763da12012-04-20 14:47:57 -0400836 nfs_pageio_complete(&desc);
Chuck Lever19f73782007-11-12 12:16:47 -0500837
Chuck Lever839f7ad2011-01-21 15:54:57 +0000838 /*
839 * If no bytes were started, return the error, and let the
840 * generic layer handle the completion.
841 */
842 if (requested_bytes == 0) {
Trond Myklebust1d59d612012-05-31 12:22:33 -0400843 inode_dio_done(inode);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000844 nfs_direct_req_release(dreq);
845 return result < 0 ? result : -EIO;
846 }
847
Chuck Lever19f73782007-11-12 12:16:47 -0500848 if (put_dreq(dreq))
849 nfs_direct_write_complete(dreq, dreq->inode);
Chuck Lever839f7ad2011-01-21 15:54:57 +0000850 return 0;
Chuck Lever19f73782007-11-12 12:16:47 -0500851}
852
Chuck Leverc216fd72007-11-12 12:16:52 -0500853static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
854 unsigned long nr_segs, loff_t pos,
Mel Gormana564b8f2012-07-31 16:45:12 -0700855 size_t count, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400857 ssize_t result = -ENOMEM;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500858 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500859 struct nfs_direct_req *dreq;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400860 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Trond Myklebust607f31e2006-06-28 16:52:45 -0400862 dreq = nfs_direct_req_alloc();
Chuck Lever462d5b32006-03-20 13:44:32 -0500863 if (!dreq)
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400864 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500866 dreq->inode = inode;
Peng Tao35754bc2012-09-25 14:55:57 +0800867 dreq->bytes_left = count;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400868 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400869 l_ctx = nfs_get_lock_context(dreq->ctx);
870 if (IS_ERR(l_ctx)) {
871 result = PTR_ERR(l_ctx);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400872 goto out_release;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400873 }
874 dreq->l_ctx = l_ctx;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500875 if (!is_sync_kiocb(iocb))
876 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Mel Gormana564b8f2012-07-31 16:45:12 -0700878 result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400879 if (!result)
880 result = nfs_direct_wait(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400881out_release:
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400882 nfs_direct_req_release(dreq);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400883out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return result;
885}
886
887/**
888 * nfs_file_direct_read - file direct read operation for NFS files
889 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700890 * @iov: vector of user buffers into which to read data
891 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500892 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 *
894 * We use this function for direct reads instead of calling
895 * generic_file_aio_read() in order to avoid gfar's check to see if
896 * the request starts before the end of the file. For that check
897 * to work, we must generate a GETATTR before each direct read, and
898 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500899 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * to do all reads the application wants, and the server will take
901 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500902 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * This function also eliminates unnecessarily updating the file's
904 * atime locally, as the NFS server sets the file's atime, and this
905 * client must read the updated atime from the server back into its
906 * cache.
907 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700908ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700909 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500914 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Chuck Leverc216fd72007-11-12 12:16:52 -0500916 count = iov_length(iov, nr_segs);
917 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
918
Chuck Lever6da24bc2008-06-11 17:55:58 -0400919 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800920 file->f_path.dentry->d_parent->d_name.name,
921 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500922 count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 retval = 0;
925 if (!count)
926 goto out;
927
Trond Myklebust29884df2005-12-13 16:13:54 -0500928 retval = nfs_sync_mapping(mapping);
929 if (retval)
930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Konstantin Khlebnikov7ec10f22011-02-22 00:28:34 +0300932 task_io_account_read(count);
933
Mel Gormana564b8f2012-07-31 16:45:12 -0700934 retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500936 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938out:
939 return retval;
940}
941
942/**
943 * nfs_file_direct_write - file direct write operation for NFS files
944 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700945 * @iov: vector of user buffers from which to write data
946 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500947 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
949 * We use this function for direct writes instead of calling
950 * generic_file_aio_write() in order to avoid taking the inode
951 * semaphore and updating the i_size. The NFS server will set
952 * the new i_size and this client must read the updated size
953 * back into its cache. We let the server do generic write
954 * parameter checking and report problems.
955 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * We eliminate local atime updates, see direct read above.
957 *
958 * We avoid unnecessary page cache invalidations for normal cached
959 * readers of this file.
960 *
961 * Note that O_APPEND is not supported for NFS direct writes, as there
962 * is no atomic O_APPEND write facility in the NFS protocol.
963 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700964ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
Mel Gormana564b8f2012-07-31 16:45:12 -0700965 unsigned long nr_segs, loff_t pos, bool uio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Chuck Lever070ea602007-05-19 17:22:52 -0400967 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500970 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Chuck Leverc216fd72007-11-12 12:16:52 -0500972 count = iov_length(iov, nr_segs);
973 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
974
Chuck Lever6da24bc2008-06-11 17:55:58 -0400975 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800976 file->f_path.dentry->d_parent->d_name.name,
977 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500978 count, (long long) pos);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700979
Chuck Leverce1a8e62005-11-30 18:08:17 -0500980 retval = generic_write_checks(file, &pos, &count, 0);
981 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500983
984 retval = -EINVAL;
985 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 retval = 0;
988 if (!count)
989 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500990
Trond Myklebust29884df2005-12-13 16:13:54 -0500991 retval = nfs_sync_mapping(mapping);
992 if (retval)
993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Konstantin Khlebnikov7ec10f22011-02-22 00:28:34 +0300995 task_io_account_write(count);
996
Mel Gormana564b8f2012-07-31 16:45:12 -0700997 retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
Fred Isaman1763da12012-04-20 14:47:57 -0400998 if (retval > 0) {
999 struct inode *inode = mapping->host;
Chuck Lever9eafa8c2006-03-20 13:44:33 -05001000
Chuck Leverce1a8e62005-11-30 18:08:17 -05001001 iocb->ki_pos = pos + retval;
Fred Isaman1763da12012-04-20 14:47:57 -04001002 spin_lock(&inode->i_lock);
1003 if (i_size_read(inode) < iocb->ki_pos)
1004 i_size_write(inode, iocb->ki_pos);
1005 spin_unlock(&inode->i_lock);
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007out:
1008 return retval;
1009}
1010
Chuck Lever88467052006-03-20 13:44:34 -05001011/**
1012 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1013 *
1014 */
David Howellsf7b422b2006-06-09 09:34:33 -04001015int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
1018 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001019 0, (SLAB_RECLAIM_ACCOUNT|
1020 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001021 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (nfs_direct_cachep == NULL)
1023 return -ENOMEM;
1024
1025 return 0;
1026}
1027
Chuck Lever88467052006-03-20 13:44:34 -05001028/**
David Howellsf7b422b2006-06-09 09:34:33 -04001029 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -05001030 *
1031 */
David Brownell266bee82006-06-27 12:59:15 -07001032void nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001034 kmem_cache_destroy(nfs_direct_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}