pnfs: pass ds_commit_idx through the commit path
[linux/fpc-iii.git] / fs / nfs / direct.c
blob1ee41d74c31c761985349a4c89699502db8aa27a
1 /*
2 * linux/fs/nfs/direct.c
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
6 * High-performance uncached I/O for the Linux NFS client
8 * There are important applications whose performance or correctness
9 * depends on uncached access to file data. Database clusters
10 * (multiple copies of the same instance running on separate hosts)
11 * implement their own cache coherency protocol that subsumes file
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
15 * need to cache the contents of a file.
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.
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.
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
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
37 * 04 May 2005 support O_DIRECT with aio --cel
41 #include <linux/errno.h>
42 #include <linux/sched.h>
43 #include <linux/kernel.h>
44 #include <linux/file.h>
45 #include <linux/pagemap.h>
46 #include <linux/kref.h>
47 #include <linux/slab.h>
48 #include <linux/task_io_accounting_ops.h>
49 #include <linux/module.h>
51 #include <linux/nfs_fs.h>
52 #include <linux/nfs_page.h>
53 #include <linux/sunrpc/clnt.h>
55 #include <asm/uaccess.h>
56 #include <linux/atomic.h>
58 #include "internal.h"
59 #include "iostat.h"
60 #include "pnfs.h"
62 #define NFSDBG_FACILITY NFSDBG_VFS
64 static struct kmem_cache *nfs_direct_cachep;
67 * This represents a set of asynchronous requests that we're waiting on
69 struct nfs_direct_req {
70 struct kref kref; /* release manager */
72 /* I/O parameters */
73 struct nfs_open_context *ctx; /* file open context info */
74 struct nfs_lock_context *l_ctx; /* Lock context info */
75 struct kiocb * iocb; /* controlling i/o request */
76 struct inode * inode; /* target file of i/o */
78 /* completion state */
79 atomic_t io_count; /* i/os we're waiting for */
80 spinlock_t lock; /* protect completion state */
81 ssize_t count, /* bytes actually processed */
82 bytes_left, /* bytes left to be sent */
83 error; /* any reported error */
84 struct completion completion; /* wait for i/o completion */
86 /* commit state */
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;
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 */
96 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
97 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
98 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
99 static void nfs_direct_write_schedule_work(struct work_struct *work);
101 static inline void get_dreq(struct nfs_direct_req *dreq)
103 atomic_inc(&dreq->io_count);
106 static inline int put_dreq(struct nfs_direct_req *dreq)
108 return atomic_dec_and_test(&dreq->io_count);
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 * @commit_idx - commit bucket index for the DS
117 * returns the correct verifier to use given the role of the server
119 static struct nfs_writeverf *
120 nfs_direct_select_verf(struct nfs_direct_req *dreq,
121 struct nfs_client *ds_clp,
122 int commit_idx)
124 struct nfs_writeverf *verfp = &dreq->verf;
126 #ifdef CONFIG_NFS_V4_1
127 if (ds_clp) {
128 /* pNFS is in use, use the DS verf */
129 if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
130 verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
131 else
132 WARN_ON_ONCE(1);
134 #endif
135 return verfp;
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
144 * Set the server's (MDS or DS) "seen" verifier
146 static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
147 struct nfs_pgio_header *hdr)
149 struct nfs_writeverf *verfp;
151 verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
152 WARN_ON_ONCE(verfp->committed >= 0);
153 memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
154 WARN_ON_ONCE(verfp->committed < 0);
158 * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
159 * @dreq - direct request possibly spanning multiple servers
160 * @hdr - pageio header to validate against previously seen verf
162 * set the server's "seen" verf if not initialized.
163 * returns result of comparison between @hdr->verf and the "seen"
164 * verf of the server used by @hdr (DS or MDS)
166 static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
167 struct nfs_pgio_header *hdr)
169 struct nfs_writeverf *verfp;
171 verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
172 if (verfp->committed < 0) {
173 nfs_direct_set_hdr_verf(dreq, hdr);
174 return 0;
176 return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
180 * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
181 * @dreq - direct request possibly spanning multiple servers
182 * @data - commit data to validate against previously seen verf
184 * returns result of comparison between @data->verf and the verf of
185 * the server used by @data (DS or MDS)
187 static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
188 struct nfs_commit_data *data)
190 struct nfs_writeverf *verfp;
192 verfp = nfs_direct_select_verf(dreq, data->ds_clp,
193 data->ds_commit_index);
194 WARN_ON_ONCE(verfp->committed < 0);
195 return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf));
199 * nfs_direct_IO - NFS address space operation for direct I/O
200 * @rw: direction (read or write)
201 * @iocb: target I/O control block
202 * @iov: array of vectors that define I/O buffer
203 * @pos: offset in file to begin the operation
204 * @nr_segs: size of iovec array
206 * The presence of this routine in the address space ops vector means
207 * the NFS client supports direct I/O. However, for most direct IO, we
208 * shunt off direct read and write requests before the VFS gets them,
209 * so this method is only ever called for swap.
211 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
213 #ifndef CONFIG_NFS_SWAP
214 dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n",
215 iocb->ki_filp, (long long) pos, iter->nr_segs);
217 return -EINVAL;
218 #else
219 VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
221 if (rw == READ)
222 return nfs_file_direct_read(iocb, iter, pos);
223 return nfs_file_direct_write(iocb, iter, pos);
224 #endif /* CONFIG_NFS_SWAP */
227 static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
229 unsigned int i;
230 for (i = 0; i < npages; i++)
231 page_cache_release(pages[i]);
234 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
235 struct nfs_direct_req *dreq)
237 cinfo->lock = &dreq->lock;
238 cinfo->mds = &dreq->mds_cinfo;
239 cinfo->ds = &dreq->ds_cinfo;
240 cinfo->dreq = dreq;
241 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
244 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
246 struct nfs_direct_req *dreq;
248 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
249 if (!dreq)
250 return NULL;
252 kref_init(&dreq->kref);
253 kref_get(&dreq->kref);
254 init_completion(&dreq->completion);
255 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
256 dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */
257 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
258 spin_lock_init(&dreq->lock);
260 return dreq;
263 static void nfs_direct_req_free(struct kref *kref)
265 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
267 nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
268 if (dreq->l_ctx != NULL)
269 nfs_put_lock_context(dreq->l_ctx);
270 if (dreq->ctx != NULL)
271 put_nfs_open_context(dreq->ctx);
272 kmem_cache_free(nfs_direct_cachep, dreq);
275 static void nfs_direct_req_release(struct nfs_direct_req *dreq)
277 kref_put(&dreq->kref, nfs_direct_req_free);
280 ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
282 return dreq->bytes_left;
284 EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
287 * Collects and returns the final error value/byte-count.
289 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
291 ssize_t result = -EIOCBQUEUED;
293 /* Async requests don't wait here */
294 if (dreq->iocb)
295 goto out;
297 result = wait_for_completion_killable(&dreq->completion);
299 if (!result)
300 result = dreq->error;
301 if (!result)
302 result = dreq->count;
304 out:
305 return (ssize_t) result;
309 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
310 * the iocb is still valid here if this is a synchronous request.
312 static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write)
314 struct inode *inode = dreq->inode;
316 if (dreq->iocb && write) {
317 loff_t pos = dreq->iocb->ki_pos + dreq->count;
319 spin_lock(&inode->i_lock);
320 if (i_size_read(inode) < pos)
321 i_size_write(inode, pos);
322 spin_unlock(&inode->i_lock);
325 if (write)
326 nfs_zap_mapping(inode, inode->i_mapping);
328 inode_dio_done(inode);
330 if (dreq->iocb) {
331 long res = (long) dreq->error;
332 if (!res)
333 res = (long) dreq->count;
334 aio_complete(dreq->iocb, res, 0);
337 complete_all(&dreq->completion);
339 nfs_direct_req_release(dreq);
342 static void nfs_direct_readpage_release(struct nfs_page *req)
344 dprintk("NFS: direct read done (%s/%llu %d@%lld)\n",
345 req->wb_context->dentry->d_inode->i_sb->s_id,
346 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
347 req->wb_bytes,
348 (long long)req_offset(req));
349 nfs_release_request(req);
352 static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
354 unsigned long bytes = 0;
355 struct nfs_direct_req *dreq = hdr->dreq;
357 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
358 goto out_put;
360 spin_lock(&dreq->lock);
361 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
362 dreq->error = hdr->error;
363 else
364 dreq->count += hdr->good_bytes;
365 spin_unlock(&dreq->lock);
367 while (!list_empty(&hdr->pages)) {
368 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
369 struct page *page = req->wb_page;
371 if (!PageCompound(page) && bytes < hdr->good_bytes)
372 set_page_dirty(page);
373 bytes += req->wb_bytes;
374 nfs_list_remove_request(req);
375 nfs_direct_readpage_release(req);
377 out_put:
378 if (put_dreq(dreq))
379 nfs_direct_complete(dreq, false);
380 hdr->release(hdr);
383 static void nfs_read_sync_pgio_error(struct list_head *head)
385 struct nfs_page *req;
387 while (!list_empty(head)) {
388 req = nfs_list_entry(head->next);
389 nfs_list_remove_request(req);
390 nfs_release_request(req);
394 static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
396 get_dreq(hdr->dreq);
399 static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
400 .error_cleanup = nfs_read_sync_pgio_error,
401 .init_hdr = nfs_direct_pgio_init,
402 .completion = nfs_direct_read_completion,
406 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
407 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
408 * bail and stop sending more reads. Read length accounting is
409 * handled automatically by nfs_direct_read_result(). Otherwise, if
410 * no requests have been sent, just return an error.
413 static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
414 struct iov_iter *iter,
415 loff_t pos)
417 struct nfs_pageio_descriptor desc;
418 struct inode *inode = dreq->inode;
419 ssize_t result = -EINVAL;
420 size_t requested_bytes = 0;
421 size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
423 nfs_pageio_init_read(&desc, dreq->inode, false,
424 &nfs_direct_read_completion_ops);
425 get_dreq(dreq);
426 desc.pg_dreq = dreq;
427 atomic_inc(&inode->i_dio_count);
429 while (iov_iter_count(iter)) {
430 struct page **pagevec;
431 size_t bytes;
432 size_t pgbase;
433 unsigned npages, i;
435 result = iov_iter_get_pages_alloc(iter, &pagevec,
436 rsize, &pgbase);
437 if (result < 0)
438 break;
440 bytes = result;
441 iov_iter_advance(iter, bytes);
442 npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
443 for (i = 0; i < npages; i++) {
444 struct nfs_page *req;
445 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
446 /* XXX do we need to do the eof zeroing found in async_filler? */
447 req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
448 pgbase, req_len);
449 if (IS_ERR(req)) {
450 result = PTR_ERR(req);
451 break;
453 req->wb_index = pos >> PAGE_SHIFT;
454 req->wb_offset = pos & ~PAGE_MASK;
455 if (!nfs_pageio_add_request(&desc, req)) {
456 result = desc.pg_error;
457 nfs_release_request(req);
458 break;
460 pgbase = 0;
461 bytes -= req_len;
462 requested_bytes += req_len;
463 pos += req_len;
464 dreq->bytes_left -= req_len;
466 nfs_direct_release_pages(pagevec, npages);
467 kvfree(pagevec);
468 if (result < 0)
469 break;
472 nfs_pageio_complete(&desc);
475 * If no bytes were started, return the error, and let the
476 * generic layer handle the completion.
478 if (requested_bytes == 0) {
479 inode_dio_done(inode);
480 nfs_direct_req_release(dreq);
481 return result < 0 ? result : -EIO;
484 if (put_dreq(dreq))
485 nfs_direct_complete(dreq, false);
486 return 0;
490 * nfs_file_direct_read - file direct read operation for NFS files
491 * @iocb: target I/O control block
492 * @iter: vector of user buffers into which to read data
493 * @pos: byte offset in file where reading starts
495 * We use this function for direct reads instead of calling
496 * generic_file_aio_read() in order to avoid gfar's check to see if
497 * the request starts before the end of the file. For that check
498 * to work, we must generate a GETATTR before each direct read, and
499 * even then there is a window between the GETATTR and the subsequent
500 * READ where the file size could change. Our preference is simply
501 * to do all reads the application wants, and the server will take
502 * care of managing the end of file boundary.
504 * This function also eliminates unnecessarily updating the file's
505 * atime locally, as the NFS server sets the file's atime, and this
506 * client must read the updated atime from the server back into its
507 * cache.
509 ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
510 loff_t pos)
512 struct file *file = iocb->ki_filp;
513 struct address_space *mapping = file->f_mapping;
514 struct inode *inode = mapping->host;
515 struct nfs_direct_req *dreq;
516 struct nfs_lock_context *l_ctx;
517 ssize_t result = -EINVAL;
518 size_t count = iov_iter_count(iter);
519 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
521 dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
522 file, count, (long long) pos);
524 result = 0;
525 if (!count)
526 goto out;
528 mutex_lock(&inode->i_mutex);
529 result = nfs_sync_mapping(mapping);
530 if (result)
531 goto out_unlock;
533 task_io_account_read(count);
535 result = -ENOMEM;
536 dreq = nfs_direct_req_alloc();
537 if (dreq == NULL)
538 goto out_unlock;
540 dreq->inode = inode;
541 dreq->bytes_left = count;
542 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
543 l_ctx = nfs_get_lock_context(dreq->ctx);
544 if (IS_ERR(l_ctx)) {
545 result = PTR_ERR(l_ctx);
546 goto out_release;
548 dreq->l_ctx = l_ctx;
549 if (!is_sync_kiocb(iocb))
550 dreq->iocb = iocb;
552 NFS_I(inode)->read_io += count;
553 result = nfs_direct_read_schedule_iovec(dreq, iter, pos);
555 mutex_unlock(&inode->i_mutex);
557 if (!result) {
558 result = nfs_direct_wait(dreq);
559 if (result > 0)
560 iocb->ki_pos = pos + result;
563 nfs_direct_req_release(dreq);
564 return result;
566 out_release:
567 nfs_direct_req_release(dreq);
568 out_unlock:
569 mutex_unlock(&inode->i_mutex);
570 out:
571 return result;
574 static void
575 nfs_direct_write_scan_commit_list(struct inode *inode,
576 struct list_head *list,
577 struct nfs_commit_info *cinfo)
579 spin_lock(cinfo->lock);
580 #ifdef CONFIG_NFS_V4_1
581 if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
582 NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
583 #endif
584 nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
585 spin_unlock(cinfo->lock);
588 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
590 struct nfs_pageio_descriptor desc;
591 struct nfs_page *req, *tmp;
592 LIST_HEAD(reqs);
593 struct nfs_commit_info cinfo;
594 LIST_HEAD(failed);
596 nfs_init_cinfo_from_dreq(&cinfo, dreq);
597 nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
599 dreq->count = 0;
600 get_dreq(dreq);
602 nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
603 &nfs_direct_write_completion_ops);
604 desc.pg_dreq = dreq;
606 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
607 if (!nfs_pageio_add_request(&desc, req)) {
608 nfs_list_remove_request(req);
609 nfs_list_add_request(req, &failed);
610 spin_lock(cinfo.lock);
611 dreq->flags = 0;
612 dreq->error = -EIO;
613 spin_unlock(cinfo.lock);
615 nfs_release_request(req);
617 nfs_pageio_complete(&desc);
619 while (!list_empty(&failed)) {
620 req = nfs_list_entry(failed.next);
621 nfs_list_remove_request(req);
622 nfs_unlock_and_release_request(req);
625 if (put_dreq(dreq))
626 nfs_direct_write_complete(dreq, dreq->inode);
629 static void nfs_direct_commit_complete(struct nfs_commit_data *data)
631 struct nfs_direct_req *dreq = data->dreq;
632 struct nfs_commit_info cinfo;
633 struct nfs_page *req;
634 int status = data->task.tk_status;
636 nfs_init_cinfo_from_dreq(&cinfo, dreq);
637 if (status < 0) {
638 dprintk("NFS: %5u commit failed with error %d.\n",
639 data->task.tk_pid, status);
640 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
641 } else if (nfs_direct_cmp_commit_data_verf(dreq, data)) {
642 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
643 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
646 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
647 while (!list_empty(&data->pages)) {
648 req = nfs_list_entry(data->pages.next);
649 nfs_list_remove_request(req);
650 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
651 /* Note the rewrite will go through mds */
652 nfs_mark_request_commit(req, NULL, &cinfo, 0);
653 } else
654 nfs_release_request(req);
655 nfs_unlock_and_release_request(req);
658 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
659 nfs_direct_write_complete(dreq, data->inode);
662 static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
664 /* There is no lock to clear */
667 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
668 .completion = nfs_direct_commit_complete,
669 .error_cleanup = nfs_direct_error_cleanup,
672 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
674 int res;
675 struct nfs_commit_info cinfo;
676 LIST_HEAD(mds_list);
678 nfs_init_cinfo_from_dreq(&cinfo, dreq);
679 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
680 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
681 if (res < 0) /* res == -ENOMEM */
682 nfs_direct_write_reschedule(dreq);
685 static void nfs_direct_write_schedule_work(struct work_struct *work)
687 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
688 int flags = dreq->flags;
690 dreq->flags = 0;
691 switch (flags) {
692 case NFS_ODIRECT_DO_COMMIT:
693 nfs_direct_commit_schedule(dreq);
694 break;
695 case NFS_ODIRECT_RESCHED_WRITES:
696 nfs_direct_write_reschedule(dreq);
697 break;
698 default:
699 nfs_direct_complete(dreq, true);
703 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
705 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
708 static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
710 struct nfs_direct_req *dreq = hdr->dreq;
711 struct nfs_commit_info cinfo;
712 bool request_commit = false;
713 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
715 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
716 goto out_put;
718 nfs_init_cinfo_from_dreq(&cinfo, dreq);
720 spin_lock(&dreq->lock);
722 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
723 dreq->flags = 0;
724 dreq->error = hdr->error;
726 if (dreq->error == 0) {
727 dreq->count += hdr->good_bytes;
728 if (nfs_write_need_commit(hdr)) {
729 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
730 request_commit = true;
731 else if (dreq->flags == 0) {
732 nfs_direct_set_hdr_verf(dreq, hdr);
733 request_commit = true;
734 dreq->flags = NFS_ODIRECT_DO_COMMIT;
735 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
736 request_commit = true;
737 if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
738 dreq->flags =
739 NFS_ODIRECT_RESCHED_WRITES;
743 spin_unlock(&dreq->lock);
745 while (!list_empty(&hdr->pages)) {
747 req = nfs_list_entry(hdr->pages.next);
748 nfs_list_remove_request(req);
749 if (request_commit) {
750 kref_get(&req->wb_kref);
751 nfs_mark_request_commit(req, hdr->lseg, &cinfo,
752 hdr->ds_commit_idx);
754 nfs_unlock_and_release_request(req);
757 out_put:
758 if (put_dreq(dreq))
759 nfs_direct_write_complete(dreq, hdr->inode);
760 hdr->release(hdr);
763 static void nfs_write_sync_pgio_error(struct list_head *head)
765 struct nfs_page *req;
767 while (!list_empty(head)) {
768 req = nfs_list_entry(head->next);
769 nfs_list_remove_request(req);
770 nfs_unlock_and_release_request(req);
774 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
775 .error_cleanup = nfs_write_sync_pgio_error,
776 .init_hdr = nfs_direct_pgio_init,
777 .completion = nfs_direct_write_completion,
782 * NB: Return the value of the first error return code. Subsequent
783 * errors after the first one are ignored.
786 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
787 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
788 * bail and stop sending more writes. Write length accounting is
789 * handled automatically by nfs_direct_write_result(). Otherwise, if
790 * no requests have been sent, just return an error.
792 static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
793 struct iov_iter *iter,
794 loff_t pos)
796 struct nfs_pageio_descriptor desc;
797 struct inode *inode = dreq->inode;
798 ssize_t result = 0;
799 size_t requested_bytes = 0;
800 size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
802 nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
803 &nfs_direct_write_completion_ops);
804 desc.pg_dreq = dreq;
805 get_dreq(dreq);
806 atomic_inc(&inode->i_dio_count);
808 NFS_I(inode)->write_io += iov_iter_count(iter);
809 while (iov_iter_count(iter)) {
810 struct page **pagevec;
811 size_t bytes;
812 size_t pgbase;
813 unsigned npages, i;
815 result = iov_iter_get_pages_alloc(iter, &pagevec,
816 wsize, &pgbase);
817 if (result < 0)
818 break;
820 bytes = result;
821 iov_iter_advance(iter, bytes);
822 npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
823 for (i = 0; i < npages; i++) {
824 struct nfs_page *req;
825 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
827 req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
828 pgbase, req_len);
829 if (IS_ERR(req)) {
830 result = PTR_ERR(req);
831 break;
833 nfs_lock_request(req);
834 req->wb_index = pos >> PAGE_SHIFT;
835 req->wb_offset = pos & ~PAGE_MASK;
836 if (!nfs_pageio_add_request(&desc, req)) {
837 result = desc.pg_error;
838 nfs_unlock_and_release_request(req);
839 break;
841 pgbase = 0;
842 bytes -= req_len;
843 requested_bytes += req_len;
844 pos += req_len;
845 dreq->bytes_left -= req_len;
847 nfs_direct_release_pages(pagevec, npages);
848 kvfree(pagevec);
849 if (result < 0)
850 break;
852 nfs_pageio_complete(&desc);
855 * If no bytes were started, return the error, and let the
856 * generic layer handle the completion.
858 if (requested_bytes == 0) {
859 inode_dio_done(inode);
860 nfs_direct_req_release(dreq);
861 return result < 0 ? result : -EIO;
864 if (put_dreq(dreq))
865 nfs_direct_write_complete(dreq, dreq->inode);
866 return 0;
870 * nfs_file_direct_write - file direct write operation for NFS files
871 * @iocb: target I/O control block
872 * @iter: vector of user buffers from which to write data
873 * @pos: byte offset in file where writing starts
875 * We use this function for direct writes instead of calling
876 * generic_file_aio_write() in order to avoid taking the inode
877 * semaphore and updating the i_size. The NFS server will set
878 * the new i_size and this client must read the updated size
879 * back into its cache. We let the server do generic write
880 * parameter checking and report problems.
882 * We eliminate local atime updates, see direct read above.
884 * We avoid unnecessary page cache invalidations for normal cached
885 * readers of this file.
887 * Note that O_APPEND is not supported for NFS direct writes, as there
888 * is no atomic O_APPEND write facility in the NFS protocol.
890 ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
891 loff_t pos)
893 ssize_t result = -EINVAL;
894 struct file *file = iocb->ki_filp;
895 struct address_space *mapping = file->f_mapping;
896 struct inode *inode = mapping->host;
897 struct nfs_direct_req *dreq;
898 struct nfs_lock_context *l_ctx;
899 loff_t end;
900 size_t count = iov_iter_count(iter);
901 end = (pos + count - 1) >> PAGE_CACHE_SHIFT;
903 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
905 dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
906 file, count, (long long) pos);
908 result = generic_write_checks(file, &pos, &count, 0);
909 if (result)
910 goto out;
912 result = -EINVAL;
913 if ((ssize_t) count < 0)
914 goto out;
915 result = 0;
916 if (!count)
917 goto out;
919 mutex_lock(&inode->i_mutex);
921 result = nfs_sync_mapping(mapping);
922 if (result)
923 goto out_unlock;
925 if (mapping->nrpages) {
926 result = invalidate_inode_pages2_range(mapping,
927 pos >> PAGE_CACHE_SHIFT, end);
928 if (result)
929 goto out_unlock;
932 task_io_account_write(count);
934 result = -ENOMEM;
935 dreq = nfs_direct_req_alloc();
936 if (!dreq)
937 goto out_unlock;
939 dreq->inode = inode;
940 dreq->bytes_left = count;
941 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
942 l_ctx = nfs_get_lock_context(dreq->ctx);
943 if (IS_ERR(l_ctx)) {
944 result = PTR_ERR(l_ctx);
945 goto out_release;
947 dreq->l_ctx = l_ctx;
948 if (!is_sync_kiocb(iocb))
949 dreq->iocb = iocb;
951 result = nfs_direct_write_schedule_iovec(dreq, iter, pos);
953 if (mapping->nrpages) {
954 invalidate_inode_pages2_range(mapping,
955 pos >> PAGE_CACHE_SHIFT, end);
958 mutex_unlock(&inode->i_mutex);
960 if (!result) {
961 result = nfs_direct_wait(dreq);
962 if (result > 0) {
963 struct inode *inode = mapping->host;
965 iocb->ki_pos = pos + result;
966 spin_lock(&inode->i_lock);
967 if (i_size_read(inode) < iocb->ki_pos)
968 i_size_write(inode, iocb->ki_pos);
969 spin_unlock(&inode->i_lock);
972 nfs_direct_req_release(dreq);
973 return result;
975 out_release:
976 nfs_direct_req_release(dreq);
977 out_unlock:
978 mutex_unlock(&inode->i_mutex);
979 out:
980 return result;
984 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
987 int __init nfs_init_directcache(void)
989 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
990 sizeof(struct nfs_direct_req),
991 0, (SLAB_RECLAIM_ACCOUNT|
992 SLAB_MEM_SPREAD),
993 NULL);
994 if (nfs_direct_cachep == NULL)
995 return -ENOMEM;
997 return 0;
1001 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1004 void nfs_destroy_directcache(void)
1006 kmem_cache_destroy(nfs_direct_cachep);