Revert "NFS: nfs4_do_open should add negative results to the dcache."
[deliverable/linux.git] / fs / nfs / direct.c
CommitLineData
1da177e4
LT
1/*
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
88467055 10 * (multiple copies of the same instance running on separate hosts)
1da177e4 11 * implement their own cache coherency protocol that subsumes file
88467055
CL
12 * 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
1da177e4
LT
15 * 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
88467055 37 * 04 May 2005 support O_DIRECT with aio --cel
1da177e4
LT
38 *
39 */
40
1da177e4
LT
41#include <linux/errno.h>
42#include <linux/sched.h>
43#include <linux/kernel.h>
1da177e4
LT
44#include <linux/file.h>
45#include <linux/pagemap.h>
46#include <linux/kref.h>
5a0e3ad6 47#include <linux/slab.h>
7ec10f26 48#include <linux/task_io_accounting_ops.h>
6296556f 49#include <linux/module.h>
1da177e4
LT
50
51#include <linux/nfs_fs.h>
52#include <linux/nfs_page.h>
53#include <linux/sunrpc/clnt.h>
54
1da177e4 55#include <asm/uaccess.h>
60063497 56#include <linux/atomic.h>
1da177e4 57
8d5658c9 58#include "internal.h"
91d5b470 59#include "iostat.h"
1763da12 60#include "pnfs.h"
91d5b470 61
1da177e4 62#define NFSDBG_FACILITY NFSDBG_VFS
1da177e4 63
e18b890b 64static struct kmem_cache *nfs_direct_cachep;
1da177e4
LT
65
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 */
15ce4a0c
CL
71
72 /* I/O parameters */
a8881f5a 73 struct nfs_open_context *ctx; /* file open context info */
f11ac8db 74 struct nfs_lock_context *l_ctx; /* Lock context info */
99514f8f 75 struct kiocb * iocb; /* controlling i/o request */
88467055 76 struct inode * inode; /* target file of i/o */
15ce4a0c
CL
77
78 /* completion state */
607f31e8 79 atomic_t io_count; /* i/os we're waiting for */
15ce4a0c 80 spinlock_t lock; /* protect completion state */
15ce4a0c 81 ssize_t count, /* bytes actually processed */
35754bc0 82 bytes_left, /* bytes left to be sent */
1da177e4 83 error; /* any reported error */
d72b7a6b 84 struct completion completion; /* wait for i/o completion */
fad61490
TM
85
86 /* commit state */
1763da12
FI
87 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;
fad61490
TM
90 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 */
1da177e4
LT
94};
95
1763da12
FI
96static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
97static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
fad61490 98static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
1763da12 99static void nfs_direct_write_schedule_work(struct work_struct *work);
607f31e8
TM
100
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
5002c586
WAA
111/*
112 * nfs_direct_select_verf - select the right verifier
113 * @dreq - direct request possibly spanning multiple servers
114 * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
115 * @ds_idx - index of data server in data server list, only valid if ds_clp set
116 *
117 * returns the correct verifier to use given the role of the server
118 */
119static struct nfs_writeverf *
120nfs_direct_select_verf(struct nfs_direct_req *dreq,
121 struct nfs_client *ds_clp,
122 int ds_idx)
123{
124 struct nfs_writeverf *verfp = &dreq->verf;
125
126#ifdef CONFIG_NFS_V4_1
127 if (ds_clp) {
128 /* pNFS is in use, use the DS verf */
129 if (ds_idx >= 0 && ds_idx < dreq->ds_cinfo.nbuckets)
130 verfp = &dreq->ds_cinfo.buckets[ds_idx].direct_verf;
131 else
132 WARN_ON_ONCE(1);
133 }
134#endif
135 return verfp;
136}
137
138
139/*
140 * nfs_direct_set_hdr_verf - set the write/commit verifier
141 * @dreq - direct request possibly spanning multiple servers
142 * @hdr - pageio header to validate against previously seen verfs
143 *
144 * Set the server's (MDS or DS) "seen" verifier
145 */
146static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
147 struct nfs_pgio_header *hdr)
148{
149 struct nfs_writeverf *verfp;
150
d45f60c6
WAA
151 verfp = nfs_direct_select_verf(dreq, hdr->ds_clp,
152 hdr->ds_idx);
5002c586
WAA
153 WARN_ON_ONCE(verfp->committed >= 0);
154 memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
155 WARN_ON_ONCE(verfp->committed < 0);
156}
157
158/*
159 * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
160 * @dreq - direct request possibly spanning multiple servers
161 * @hdr - pageio header to validate against previously seen verf
162 *
163 * set the server's "seen" verf if not initialized.
164 * returns result of comparison between @hdr->verf and the "seen"
165 * verf of the server used by @hdr (DS or MDS)
166 */
167static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
168 struct nfs_pgio_header *hdr)
169{
170 struct nfs_writeverf *verfp;
171
d45f60c6
WAA
172 verfp = nfs_direct_select_verf(dreq, hdr->ds_clp,
173 hdr->ds_idx);
5002c586
WAA
174 if (verfp->committed < 0) {
175 nfs_direct_set_hdr_verf(dreq, hdr);
176 return 0;
177 }
178 return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
179}
180
5002c586
WAA
181/*
182 * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
183 * @dreq - direct request possibly spanning multiple servers
184 * @data - commit data to validate against previously seen verf
185 *
186 * returns result of comparison between @data->verf and the verf of
187 * the server used by @data (DS or MDS)
188 */
189static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
190 struct nfs_commit_data *data)
191{
192 struct nfs_writeverf *verfp;
193
194 verfp = nfs_direct_select_verf(dreq, data->ds_clp,
195 data->ds_commit_index);
196 WARN_ON_ONCE(verfp->committed < 0);
197 return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf));
198}
5002c586 199
1da177e4 200/**
b8a32e2b
CL
201 * nfs_direct_IO - NFS address space operation for direct I/O
202 * @rw: direction (read or write)
203 * @iocb: target I/O control block
204 * @iov: array of vectors that define I/O buffer
205 * @pos: offset in file to begin the operation
206 * @nr_segs: size of iovec array
207 *
208 * The presence of this routine in the address space ops vector means
a564b8f0
MG
209 * the NFS client supports direct I/O. However, for most direct IO, we
210 * shunt off direct read and write requests before the VFS gets them,
211 * so this method is only ever called for swap.
1da177e4 212 */
d8d3d94b 213ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
b8a32e2b 214{
a564b8f0 215#ifndef CONFIG_NFS_SWAP
6de1472f 216 dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n",
d8d3d94b 217 iocb->ki_filp, (long long) pos, iter->nr_segs);
b8a32e2b
CL
218
219 return -EINVAL;
a564b8f0 220#else
a564b8f0
MG
221 VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
222
e19a8a0a
MP
223 if (rw == READ)
224 return nfs_file_direct_read(iocb, iter, pos);
225 return nfs_file_direct_write(iocb, iter, pos);
a564b8f0 226#endif /* CONFIG_NFS_SWAP */
b8a32e2b
CL
227}
228
749e146e 229static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
9c93ab7d 230{
749e146e 231 unsigned int i;
607f31e8
TM
232 for (i = 0; i < npages; i++)
233 page_cache_release(pages[i]);
6b45d858
TM
234}
235
1763da12
FI
236void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
237 struct nfs_direct_req *dreq)
238{
239 cinfo->lock = &dreq->lock;
240 cinfo->mds = &dreq->mds_cinfo;
241 cinfo->ds = &dreq->ds_cinfo;
242 cinfo->dreq = dreq;
243 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
244}
245
93619e59 246static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1da177e4 247{
93619e59
CL
248 struct nfs_direct_req *dreq;
249
292f3eee 250 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
93619e59
CL
251 if (!dreq)
252 return NULL;
253
254 kref_init(&dreq->kref);
607f31e8 255 kref_get(&dreq->kref);
d72b7a6b 256 init_completion(&dreq->completion);
1763da12 257 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
5002c586 258 dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */
1763da12 259 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
15ce4a0c 260 spin_lock_init(&dreq->lock);
93619e59
CL
261
262 return dreq;
1da177e4
LT
263}
264
b4946ffb 265static void nfs_direct_req_free(struct kref *kref)
1da177e4
LT
266{
267 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
a8881f5a 268
f11ac8db
TM
269 if (dreq->l_ctx != NULL)
270 nfs_put_lock_context(dreq->l_ctx);
a8881f5a
TM
271 if (dreq->ctx != NULL)
272 put_nfs_open_context(dreq->ctx);
1da177e4
LT
273 kmem_cache_free(nfs_direct_cachep, dreq);
274}
275
b4946ffb
TM
276static void nfs_direct_req_release(struct nfs_direct_req *dreq)
277{
278 kref_put(&dreq->kref, nfs_direct_req_free);
279}
280
6296556f
PT
281ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
282{
283 return dreq->bytes_left;
284}
285EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
286
bc0fb201
CL
287/*
288 * Collects and returns the final error value/byte-count.
289 */
290static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
291{
15ce4a0c 292 ssize_t result = -EIOCBQUEUED;
bc0fb201
CL
293
294 /* Async requests don't wait here */
295 if (dreq->iocb)
296 goto out;
297
150030b7 298 result = wait_for_completion_killable(&dreq->completion);
bc0fb201
CL
299
300 if (!result)
15ce4a0c 301 result = dreq->error;
bc0fb201 302 if (!result)
15ce4a0c 303 result = dreq->count;
bc0fb201
CL
304
305out:
bc0fb201
CL
306 return (ssize_t) result;
307}
308
63ab46ab 309/*
607f31e8
TM
310 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
311 * the iocb is still valid here if this is a synchronous request.
63ab46ab 312 */
9811cd57 313static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
63ab46ab 314{
9811cd57
CH
315 struct inode *inode = dreq->inode;
316
2a009ec9 317 if (dreq->iocb && write) {
9811cd57 318 loff_t pos = dreq->iocb->ki_pos + dreq->count;
2a009ec9
CH
319
320 spin_lock(&inode->i_lock);
321 if (i_size_read(inode) < pos)
322 i_size_write(inode, pos);
323 spin_unlock(&inode->i_lock);
324 }
325
1f90ee27 326 if (write)
2a009ec9 327 nfs_zap_mapping(inode, inode->i_mapping);
1f90ee27
CH
328
329 inode_dio_done(inode);
2a009ec9
CH
330
331 if (dreq->iocb) {
15ce4a0c 332 long res = (long) dreq->error;
63ab46ab 333 if (!res)
15ce4a0c 334 res = (long) dreq->count;
63ab46ab 335 aio_complete(dreq->iocb, res, 0);
d72b7a6b 336 }
2a009ec9 337
d72b7a6b 338 complete_all(&dreq->completion);
63ab46ab 339
b4946ffb 340 nfs_direct_req_release(dreq);
63ab46ab
CL
341}
342
1385b811 343static void nfs_direct_readpage_release(struct nfs_page *req)
1da177e4 344{
1e8968c5 345 dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
584aa810 346 req->wb_context->dentry->d_inode->i_sb->s_id,
1e8968c5 347 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
584aa810
FI
348 req->wb_bytes,
349 (long long)req_offset(req));
350 nfs_release_request(req);
fdd1e74c
TM
351}
352
584aa810 353static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
fdd1e74c 354{
584aa810
FI
355 unsigned long bytes = 0;
356 struct nfs_direct_req *dreq = hdr->dreq;
fdd1e74c 357
584aa810
FI
358 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
359 goto out_put;
15ce4a0c
CL
360
361 spin_lock(&dreq->lock);
584aa810
FI
362 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
363 dreq->error = hdr->error;
364 else
365 dreq->count += hdr->good_bytes;
366 spin_unlock(&dreq->lock);
367
4bd8b010
TM
368 while (!list_empty(&hdr->pages)) {
369 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
370 struct page *page = req->wb_page;
371
be7e9858
JL
372 if (!PageCompound(page) && bytes < hdr->good_bytes)
373 set_page_dirty(page);
4bd8b010
TM
374 bytes += req->wb_bytes;
375 nfs_list_remove_request(req);
376 nfs_direct_readpage_release(req);
d4a8f367 377 }
584aa810 378out_put:
607f31e8 379 if (put_dreq(dreq))
9811cd57 380 nfs_direct_complete(dreq, false);
584aa810 381 hdr->release(hdr);
1da177e4
LT
382}
383
3e9e0ca3 384static void nfs_read_sync_pgio_error(struct list_head *head)
cd841605 385{
584aa810 386 struct nfs_page *req;
cd841605 387
584aa810
FI
388 while (!list_empty(head)) {
389 req = nfs_list_entry(head->next);
390 nfs_list_remove_request(req);
391 nfs_release_request(req);
392 }
cd841605
FI
393}
394
584aa810
FI
395static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
396{
397 get_dreq(hdr->dreq);
398}
399
400static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
3e9e0ca3 401 .error_cleanup = nfs_read_sync_pgio_error,
584aa810
FI
402 .init_hdr = nfs_direct_pgio_init,
403 .completion = nfs_direct_read_completion,
404};
405
d4cc948b 406/*
607f31e8
TM
407 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
408 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
409 * bail and stop sending more reads. Read length accounting is
410 * handled automatically by nfs_direct_read_result(). Otherwise, if
411 * no requests have been sent, just return an error.
1da177e4 412 */
1da177e4 413
91f79c43
AV
414static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
415 struct iov_iter *iter,
416 loff_t pos)
417{
418 struct nfs_pageio_descriptor desc;
419 struct inode *inode = dreq->inode;
420 ssize_t result = -EINVAL;
421 size_t requested_bytes = 0;
422 size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
e9f7bee1 423
16b90578 424 nfs_pageio_init_read(&desc, dreq->inode, false,
91f79c43
AV
425 &nfs_direct_read_completion_ops);
426 get_dreq(dreq);
427 desc.pg_dreq = dreq;
428 atomic_inc(&inode->i_dio_count);
a564b8f0 429
91f79c43
AV
430 while (iov_iter_count(iter)) {
431 struct page **pagevec;
432 size_t bytes;
433 size_t pgbase;
434 unsigned npages, i;
607f31e8 435
91f79c43
AV
436 result = iov_iter_get_pages_alloc(iter, &pagevec,
437 rsize, &pgbase);
438 if (result < 0)
439 break;
440
441 bytes = result;
442 iov_iter_advance(iter, bytes);
443 npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
584aa810
FI
444 for (i = 0; i < npages; i++) {
445 struct nfs_page *req;
bf5fc402 446 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
584aa810 447 /* XXX do we need to do the eof zeroing found in async_filler? */
2bfc6e56 448 req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
584aa810
FI
449 pgbase, req_len);
450 if (IS_ERR(req)) {
584aa810
FI
451 result = PTR_ERR(req);
452 break;
453 }
454 req->wb_index = pos >> PAGE_SHIFT;
455 req->wb_offset = pos & ~PAGE_MASK;
91f79c43
AV
456 if (!nfs_pageio_add_request(&desc, req)) {
457 result = desc.pg_error;
584aa810 458 nfs_release_request(req);
584aa810
FI
459 break;
460 }
461 pgbase = 0;
462 bytes -= req_len;
91f79c43 463 requested_bytes += req_len;
584aa810 464 pos += req_len;
35754bc0 465 dreq->bytes_left -= req_len;
584aa810 466 }
6d74743b 467 nfs_direct_release_pages(pagevec, npages);
91f79c43 468 kvfree(pagevec);
19f73787
CL
469 if (result < 0)
470 break;
19f73787
CL
471 }
472
584aa810
FI
473 nfs_pageio_complete(&desc);
474
839f7ad6
CL
475 /*
476 * If no bytes were started, return the error, and let the
477 * generic layer handle the completion.
478 */
479 if (requested_bytes == 0) {
1f90ee27 480 inode_dio_done(inode);
839f7ad6
CL
481 nfs_direct_req_release(dreq);
482 return result < 0 ? result : -EIO;
483 }
484
19f73787 485 if (put_dreq(dreq))
9811cd57 486 nfs_direct_complete(dreq, false);
839f7ad6 487 return 0;
19f73787
CL
488}
489
14a3ec79
CH
490/**
491 * nfs_file_direct_read - file direct read operation for NFS files
492 * @iocb: target I/O control block
619d30b4 493 * @iter: vector of user buffers into which to read data
14a3ec79
CH
494 * @pos: byte offset in file where reading starts
495 *
496 * We use this function for direct reads instead of calling
497 * generic_file_aio_read() in order to avoid gfar's check to see if
498 * the request starts before the end of the file. For that check
499 * to work, we must generate a GETATTR before each direct read, and
500 * even then there is a window between the GETATTR and the subsequent
501 * READ where the file size could change. Our preference is simply
502 * to do all reads the application wants, and the server will take
503 * care of managing the end of file boundary.
504 *
505 * This function also eliminates unnecessarily updating the file's
506 * atime locally, as the NFS server sets the file's atime, and this
507 * client must read the updated atime from the server back into its
508 * cache.
509 */
619d30b4 510ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
e19a8a0a 511 loff_t pos)
1da177e4 512{
14a3ec79
CH
513 struct file *file = iocb->ki_filp;
514 struct address_space *mapping = file->f_mapping;
515 struct inode *inode = mapping->host;
1da177e4 516 struct nfs_direct_req *dreq;
b3c54de6 517 struct nfs_lock_context *l_ctx;
14a3ec79 518 ssize_t result = -EINVAL;
a6cbcd4a 519 size_t count = iov_iter_count(iter);
14a3ec79
CH
520 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
521
522 dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
523 file, count, (long long) pos);
524
525 result = 0;
526 if (!count)
527 goto out;
528
d0b9875d 529 mutex_lock(&inode->i_mutex);
14a3ec79
CH
530 result = nfs_sync_mapping(mapping);
531 if (result)
d0b9875d 532 goto out_unlock;
1da177e4 533
14a3ec79
CH
534 task_io_account_read(count);
535
536 result = -ENOMEM;
607f31e8 537 dreq = nfs_direct_req_alloc();
f11ac8db 538 if (dreq == NULL)
d0b9875d 539 goto out_unlock;
1da177e4 540
91d5b470 541 dreq->inode = inode;
619d30b4 542 dreq->bytes_left = count;
cd3758e3 543 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
b3c54de6
TM
544 l_ctx = nfs_get_lock_context(dreq->ctx);
545 if (IS_ERR(l_ctx)) {
546 result = PTR_ERR(l_ctx);
f11ac8db 547 goto out_release;
b3c54de6
TM
548 }
549 dreq->l_ctx = l_ctx;
487b8372
CL
550 if (!is_sync_kiocb(iocb))
551 dreq->iocb = iocb;
1da177e4 552
619d30b4 553 NFS_I(inode)->read_io += count;
91f79c43 554 result = nfs_direct_read_schedule_iovec(dreq, iter, pos);
d0b9875d
CH
555
556 mutex_unlock(&inode->i_mutex);
557
14a3ec79 558 if (!result) {
607f31e8 559 result = nfs_direct_wait(dreq);
14a3ec79
CH
560 if (result > 0)
561 iocb->ki_pos = pos + result;
562 }
d0b9875d
CH
563
564 nfs_direct_req_release(dreq);
565 return result;
566
f11ac8db 567out_release:
b4946ffb 568 nfs_direct_req_release(dreq);
d0b9875d
CH
569out_unlock:
570 mutex_unlock(&inode->i_mutex);
f11ac8db 571out:
1da177e4
LT
572 return result;
573}
574
fad61490
TM
575static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
576{
1763da12
FI
577 struct nfs_pageio_descriptor desc;
578 struct nfs_page *req, *tmp;
579 LIST_HEAD(reqs);
580 struct nfs_commit_info cinfo;
581 LIST_HEAD(failed);
582
583 nfs_init_cinfo_from_dreq(&cinfo, dreq);
584 pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
585 spin_lock(cinfo.lock);
586 nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
587 spin_unlock(cinfo.lock);
1da177e4 588
fad61490 589 dreq->count = 0;
607f31e8
TM
590 get_dreq(dreq);
591
a20c93e3 592 nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
1763da12
FI
593 &nfs_direct_write_completion_ops);
594 desc.pg_dreq = dreq;
fedb595c 595
1763da12
FI
596 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
597 if (!nfs_pageio_add_request(&desc, req)) {
4035c248 598 nfs_list_remove_request(req);
1763da12
FI
599 nfs_list_add_request(req, &failed);
600 spin_lock(cinfo.lock);
601 dreq->flags = 0;
602 dreq->error = -EIO;
603 spin_unlock(cinfo.lock);
604 }
5a695da2 605 nfs_release_request(req);
1763da12
FI
606 }
607 nfs_pageio_complete(&desc);
fad61490 608
4035c248
TM
609 while (!list_empty(&failed)) {
610 req = nfs_list_entry(failed.next);
611 nfs_list_remove_request(req);
1d1afcbc 612 nfs_unlock_and_release_request(req);
4035c248 613 }
fad61490 614
1763da12
FI
615 if (put_dreq(dreq))
616 nfs_direct_write_complete(dreq, dreq->inode);
c9d8f89d
TM
617}
618
1763da12 619static void nfs_direct_commit_complete(struct nfs_commit_data *data)
c9d8f89d 620{
0b7c0153 621 struct nfs_direct_req *dreq = data->dreq;
1763da12
FI
622 struct nfs_commit_info cinfo;
623 struct nfs_page *req;
c9d8f89d
TM
624 int status = data->task.tk_status;
625
1763da12 626 nfs_init_cinfo_from_dreq(&cinfo, dreq);
c9d8f89d 627 if (status < 0) {
60fa3f76 628 dprintk("NFS: %5u commit failed with error %d.\n",
1763da12 629 data->task.tk_pid, status);
fad61490 630 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
5002c586 631 } else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
c9d8f89d 632 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
fad61490 633 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
1da177e4
LT
634 }
635
c9d8f89d 636 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
1763da12
FI
637 while (!list_empty(&data->pages)) {
638 req = nfs_list_entry(data->pages.next);
639 nfs_list_remove_request(req);
640 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
641 /* Note the rewrite will go through mds */
642 nfs_mark_request_commit(req, NULL, &cinfo);
906369e4
FI
643 } else
644 nfs_release_request(req);
1d1afcbc 645 nfs_unlock_and_release_request(req);
1763da12
FI
646 }
647
648 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
649 nfs_direct_write_complete(dreq, data->inode);
1da177e4
LT
650}
651
1763da12
FI
652static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
653{
654 /* There is no lock to clear */
655}
656
657static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
658 .completion = nfs_direct_commit_complete,
659 .error_cleanup = nfs_direct_error_cleanup,
fad61490
TM
660};
661
662static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
1da177e4 663{
1763da12
FI
664 int res;
665 struct nfs_commit_info cinfo;
666 LIST_HEAD(mds_list);
667
668 nfs_init_cinfo_from_dreq(&cinfo, dreq);
669 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
670 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
671 if (res < 0) /* res == -ENOMEM */
672 nfs_direct_write_reschedule(dreq);
fad61490 673}
1da177e4 674
1763da12 675static void nfs_direct_write_schedule_work(struct work_struct *work)
fad61490 676{
1763da12 677 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
fad61490 678 int flags = dreq->flags;
1da177e4 679
fad61490
TM
680 dreq->flags = 0;
681 switch (flags) {
682 case NFS_ODIRECT_DO_COMMIT:
683 nfs_direct_commit_schedule(dreq);
1da177e4 684 break;
fad61490
TM
685 case NFS_ODIRECT_RESCHED_WRITES:
686 nfs_direct_write_reschedule(dreq);
687 break;
688 default:
9811cd57 689 nfs_direct_complete(dreq, true);
fad61490
TM
690 }
691}
1da177e4 692
1763da12 693static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
fad61490 694{
1763da12 695 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
fad61490 696}
1763da12 697
1763da12
FI
698static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
699{
700 struct nfs_direct_req *dreq = hdr->dreq;
701 struct nfs_commit_info cinfo;
c65e6254 702 bool request_commit = false;
1763da12
FI
703 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
704
705 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
706 goto out_put;
707
708 nfs_init_cinfo_from_dreq(&cinfo, dreq);
709
710 spin_lock(&dreq->lock);
711
712 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
713 dreq->flags = 0;
714 dreq->error = hdr->error;
715 }
c65e6254 716 if (dreq->error == 0) {
1763da12 717 dreq->count += hdr->good_bytes;
c65e6254 718 if (nfs_write_need_commit(hdr)) {
1763da12 719 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
c65e6254 720 request_commit = true;
1763da12 721 else if (dreq->flags == 0) {
5002c586 722 nfs_direct_set_hdr_verf(dreq, hdr);
c65e6254 723 request_commit = true;
1763da12
FI
724 dreq->flags = NFS_ODIRECT_DO_COMMIT;
725 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
c65e6254
WAA
726 request_commit = true;
727 if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
5002c586
WAA
728 dreq->flags =
729 NFS_ODIRECT_RESCHED_WRITES;
1763da12
FI
730 }
731 }
732 }
733 spin_unlock(&dreq->lock);
734
735 while (!list_empty(&hdr->pages)) {
2bfc6e56 736
1763da12
FI
737 req = nfs_list_entry(hdr->pages.next);
738 nfs_list_remove_request(req);
c65e6254 739 if (request_commit) {
04277086 740 kref_get(&req->wb_kref);
1763da12 741 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
1763da12 742 }
1d1afcbc 743 nfs_unlock_and_release_request(req);
1763da12
FI
744 }
745
746out_put:
747 if (put_dreq(dreq))
748 nfs_direct_write_complete(dreq, hdr->inode);
749 hdr->release(hdr);
750}
751
3e9e0ca3
TM
752static void nfs_write_sync_pgio_error(struct list_head *head)
753{
754 struct nfs_page *req;
755
756 while (!list_empty(head)) {
757 req = nfs_list_entry(head->next);
758 nfs_list_remove_request(req);
1d1afcbc 759 nfs_unlock_and_release_request(req);
3e9e0ca3
TM
760 }
761}
762
1763da12 763static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
3e9e0ca3 764 .error_cleanup = nfs_write_sync_pgio_error,
1763da12
FI
765 .init_hdr = nfs_direct_pgio_init,
766 .completion = nfs_direct_write_completion,
767};
768
91f79c43
AV
769
770/*
771 * NB: Return the value of the first error return code. Subsequent
772 * errors after the first one are ignored.
773 */
774/*
775 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
776 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
777 * bail and stop sending more writes. Write length accounting is
778 * handled automatically by nfs_direct_write_result(). Otherwise, if
779 * no requests have been sent, just return an error.
780 */
19f73787 781static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
619d30b4 782 struct iov_iter *iter,
91f79c43 783 loff_t pos)
19f73787 784{
1763da12 785 struct nfs_pageio_descriptor desc;
1d59d61f 786 struct inode *inode = dreq->inode;
19f73787
CL
787 ssize_t result = 0;
788 size_t requested_bytes = 0;
91f79c43 789 size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
19f73787 790
a20c93e3 791 nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
1763da12
FI
792 &nfs_direct_write_completion_ops);
793 desc.pg_dreq = dreq;
19f73787 794 get_dreq(dreq);
1d59d61f 795 atomic_inc(&inode->i_dio_count);
19f73787 796
91f79c43
AV
797 NFS_I(inode)->write_io += iov_iter_count(iter);
798 while (iov_iter_count(iter)) {
799 struct page **pagevec;
800 size_t bytes;
801 size_t pgbase;
802 unsigned npages, i;
803
804 result = iov_iter_get_pages_alloc(iter, &pagevec,
805 wsize, &pgbase);
19f73787
CL
806 if (result < 0)
807 break;
91f79c43
AV
808
809 bytes = result;
810 iov_iter_advance(iter, bytes);
811 npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
812 for (i = 0; i < npages; i++) {
813 struct nfs_page *req;
814 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
815
16b90578 816 req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
91f79c43
AV
817 pgbase, req_len);
818 if (IS_ERR(req)) {
819 result = PTR_ERR(req);
820 break;
821 }
822 nfs_lock_request(req);
823 req->wb_index = pos >> PAGE_SHIFT;
824 req->wb_offset = pos & ~PAGE_MASK;
825 if (!nfs_pageio_add_request(&desc, req)) {
826 result = desc.pg_error;
827 nfs_unlock_and_release_request(req);
828 break;
829 }
830 pgbase = 0;
831 bytes -= req_len;
832 requested_bytes += req_len;
833 pos += req_len;
834 dreq->bytes_left -= req_len;
835 }
836 nfs_direct_release_pages(pagevec, npages);
837 kvfree(pagevec);
838 if (result < 0)
19f73787 839 break;
19f73787 840 }
1763da12 841 nfs_pageio_complete(&desc);
19f73787 842
839f7ad6
CL
843 /*
844 * If no bytes were started, return the error, and let the
845 * generic layer handle the completion.
846 */
847 if (requested_bytes == 0) {
1d59d61f 848 inode_dio_done(inode);
839f7ad6
CL
849 nfs_direct_req_release(dreq);
850 return result < 0 ? result : -EIO;
851 }
852
19f73787
CL
853 if (put_dreq(dreq))
854 nfs_direct_write_complete(dreq, dreq->inode);
839f7ad6 855 return 0;
19f73787
CL
856}
857
1da177e4
LT
858/**
859 * nfs_file_direct_write - file direct write operation for NFS files
860 * @iocb: target I/O control block
619d30b4 861 * @iter: vector of user buffers from which to write data
88467055 862 * @pos: byte offset in file where writing starts
1da177e4
LT
863 *
864 * We use this function for direct writes instead of calling
865 * generic_file_aio_write() in order to avoid taking the inode
866 * semaphore and updating the i_size. The NFS server will set
867 * the new i_size and this client must read the updated size
868 * back into its cache. We let the server do generic write
869 * parameter checking and report problems.
870 *
1da177e4
LT
871 * We eliminate local atime updates, see direct read above.
872 *
873 * We avoid unnecessary page cache invalidations for normal cached
874 * readers of this file.
875 *
876 * Note that O_APPEND is not supported for NFS direct writes, as there
877 * is no atomic O_APPEND write facility in the NFS protocol.
878 */
619d30b4 879ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
e19a8a0a 880 loff_t pos)
1da177e4 881{
22cd1bf1 882 ssize_t result = -EINVAL;
1da177e4 883 struct file *file = iocb->ki_filp;
1da177e4 884 struct address_space *mapping = file->f_mapping;
22cd1bf1
CH
885 struct inode *inode = mapping->host;
886 struct nfs_direct_req *dreq;
887 struct nfs_lock_context *l_ctx;
a9ab5e84 888 loff_t end;
a6cbcd4a 889 size_t count = iov_iter_count(iter);
a9ab5e84
CH
890 end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
891
c216fd70
CL
892 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
893
6de1472f
AV
894 dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
895 file, count, (long long) pos);
027445c3 896
22cd1bf1
CH
897 result = generic_write_checks(file, &pos, &count, 0);
898 if (result)
1da177e4 899 goto out;
ce1a8e67 900
22cd1bf1 901 result = -EINVAL;
ce1a8e67 902 if ((ssize_t) count < 0)
1da177e4 903 goto out;
22cd1bf1 904 result = 0;
1da177e4
LT
905 if (!count)
906 goto out;
ce1a8e67 907
a9ab5e84
CH
908 mutex_lock(&inode->i_mutex);
909
22cd1bf1
CH
910 result = nfs_sync_mapping(mapping);
911 if (result)
a9ab5e84
CH
912 goto out_unlock;
913
914 if (mapping->nrpages) {
915 result = invalidate_inode_pages2_range(mapping,
916 pos >> PAGE_CACHE_SHIFT, end);
917 if (result)
918 goto out_unlock;
919 }
1da177e4 920
7ec10f26
KK
921 task_io_account_write(count);
922
22cd1bf1
CH
923 result = -ENOMEM;
924 dreq = nfs_direct_req_alloc();
925 if (!dreq)
a9ab5e84 926 goto out_unlock;
9eafa8cc 927
22cd1bf1
CH
928 dreq->inode = inode;
929 dreq->bytes_left = count;
930 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
931 l_ctx = nfs_get_lock_context(dreq->ctx);
932 if (IS_ERR(l_ctx)) {
933 result = PTR_ERR(l_ctx);
934 goto out_release;
935 }
936 dreq->l_ctx = l_ctx;
937 if (!is_sync_kiocb(iocb))
938 dreq->iocb = iocb;
939
91f79c43 940 result = nfs_direct_write_schedule_iovec(dreq, iter, pos);
a9ab5e84
CH
941
942 if (mapping->nrpages) {
943 invalidate_inode_pages2_range(mapping,
944 pos >> PAGE_CACHE_SHIFT, end);
945 }
946
947 mutex_unlock(&inode->i_mutex);
948
22cd1bf1
CH
949 if (!result) {
950 result = nfs_direct_wait(dreq);
951 if (result > 0) {
952 struct inode *inode = mapping->host;
953
954 iocb->ki_pos = pos + result;
955 spin_lock(&inode->i_lock);
956 if (i_size_read(inode) < iocb->ki_pos)
957 i_size_write(inode, iocb->ki_pos);
958 spin_unlock(&inode->i_lock);
959 }
1763da12 960 }
a9ab5e84
CH
961 nfs_direct_req_release(dreq);
962 return result;
963
22cd1bf1
CH
964out_release:
965 nfs_direct_req_release(dreq);
a9ab5e84
CH
966out_unlock:
967 mutex_unlock(&inode->i_mutex);
1da177e4 968out:
22cd1bf1 969 return result;
1da177e4
LT
970}
971
88467055
CL
972/**
973 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
974 *
975 */
f7b422b1 976int __init nfs_init_directcache(void)
1da177e4
LT
977{
978 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
979 sizeof(struct nfs_direct_req),
fffb60f9
PJ
980 0, (SLAB_RECLAIM_ACCOUNT|
981 SLAB_MEM_SPREAD),
20c2df83 982 NULL);
1da177e4
LT
983 if (nfs_direct_cachep == NULL)
984 return -ENOMEM;
985
986 return 0;
987}
988
88467055 989/**
f7b422b1 990 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
88467055
CL
991 *
992 */
266bee88 993void nfs_destroy_directcache(void)
1da177e4 994{
1a1d92c1 995 kmem_cache_destroy(nfs_direct_cachep);
1da177e4 996}
This page took 0.747676 seconds and 5 git commands to generate.