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
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>
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 */
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 */
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;
91 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
92 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
93 #define NFS_ODIRECT_MARK_DIRTY (4) /* mark read pages dirty */
94 struct nfs_writeverf verf; /* unstable write verifier */
97 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
98 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
99 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
100 static void nfs_direct_write_schedule_work(struct work_struct *work);
102 static inline void get_dreq(struct nfs_direct_req *dreq)
104 atomic_inc(&dreq->io_count);
107 static inline int put_dreq(struct nfs_direct_req *dreq)
109 return atomic_dec_and_test(&dreq->io_count);
113 * nfs_direct_IO - NFS address space operation for direct I/O
114 * @rw: direction (read or write)
115 * @iocb: target I/O control block
116 * @iter: array of vectors that define I/O buffer
117 * @pos: offset in file to begin the operation
118 * @nr_segs: size of iovec array
120 * The presence of this routine in the address space ops vector means
121 * the NFS client supports direct I/O. However, we shunt off direct
122 * read and write requests before the VFS gets them, so this method
123 * should never be called.
125 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
128 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
129 iocb->ki_filp->f_path.dentry->d_name.name,
130 (long long) pos, iter->nr_segs);
135 static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
138 for (i = 0; i < npages; i++)
139 page_cache_release(pages[i]);
142 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
143 struct nfs_direct_req *dreq)
145 cinfo->lock = &dreq->lock;
146 cinfo->mds = &dreq->mds_cinfo;
147 cinfo->ds = &dreq->ds_cinfo;
149 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
152 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
154 struct nfs_direct_req *dreq;
156 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
160 kref_init(&dreq->kref);
161 kref_get(&dreq->kref);
162 init_completion(&dreq->completion);
163 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
164 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
165 spin_lock_init(&dreq->lock);
170 static void nfs_direct_req_free(struct kref *kref)
172 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
174 if (dreq->l_ctx != NULL)
175 nfs_put_lock_context(dreq->l_ctx);
176 if (dreq->ctx != NULL)
177 put_nfs_open_context(dreq->ctx);
178 kmem_cache_free(nfs_direct_cachep, dreq);
181 static void nfs_direct_req_release(struct nfs_direct_req *dreq)
183 kref_put(&dreq->kref, nfs_direct_req_free);
186 ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
188 return dreq->bytes_left;
190 EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
193 * Collects and returns the final error value/byte-count.
195 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
197 ssize_t result = -EIOCBQUEUED;
199 /* Async requests don't wait here */
203 result = wait_for_completion_killable(&dreq->completion);
206 result = dreq->error;
208 result = dreq->count;
211 return (ssize_t) result;
215 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
216 * the iocb is still valid here if this is a synchronous request.
218 static void nfs_direct_complete(struct nfs_direct_req *dreq)
221 long res = (long) dreq->error;
223 res = (long) dreq->count;
224 aio_complete(dreq->iocb, res, 0);
226 complete_all(&dreq->completion);
228 nfs_direct_req_release(dreq);
231 static void nfs_direct_readpage_release(struct nfs_page *req)
233 dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
234 req->wb_context->dentry->d_inode->i_sb->s_id,
235 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
237 (long long)req_offset(req));
238 nfs_release_request(req);
241 static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
243 unsigned long bytes = 0;
244 struct nfs_direct_req *dreq = hdr->dreq;
246 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
249 spin_lock(&dreq->lock);
250 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
251 dreq->error = hdr->error;
253 dreq->count += hdr->good_bytes;
254 spin_unlock(&dreq->lock);
256 while (!list_empty(&hdr->pages)) {
257 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
258 struct page *page = req->wb_page;
260 if ((dreq->flags & NFS_ODIRECT_MARK_DIRTY) &&
261 !PageCompound(page) && bytes < hdr->good_bytes)
262 set_page_dirty(page);
263 bytes += req->wb_bytes;
264 nfs_list_remove_request(req);
265 nfs_direct_readpage_release(req);
269 nfs_direct_complete(dreq);
273 static void nfs_read_sync_pgio_error(struct list_head *head)
275 struct nfs_page *req;
277 while (!list_empty(head)) {
278 req = nfs_list_entry(head->next);
279 nfs_list_remove_request(req);
280 nfs_release_request(req);
284 static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
289 static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
290 .error_cleanup = nfs_read_sync_pgio_error,
291 .init_hdr = nfs_direct_pgio_init,
292 .completion = nfs_direct_read_completion,
296 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
297 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
298 * bail and stop sending more reads. Read length accounting is
299 * handled automatically by nfs_direct_read_result(). Otherwise, if
300 * no requests have been sent, just return an error.
302 static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
303 const struct iovec *iov,
306 struct nfs_direct_req *dreq = desc->pg_dreq;
307 struct nfs_open_context *ctx = dreq->ctx;
308 struct inode *inode = ctx->dentry->d_inode;
309 unsigned long user_addr = (unsigned long)iov->iov_base;
310 size_t count = iov->iov_len;
311 size_t rsize = NFS_SERVER(inode)->rsize;
315 struct page **pagevec = NULL;
322 pgbase = user_addr & ~PAGE_MASK;
323 bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
326 npages = nfs_page_array_len(pgbase, bytes);
328 pagevec = kmalloc(npages * sizeof(struct page *),
332 down_read(¤t->mm->mmap_sem);
333 result = get_user_pages(current, current->mm, user_addr,
334 npages, 1, 0, pagevec, NULL);
335 up_read(¤t->mm->mmap_sem);
338 if ((unsigned)result < npages) {
339 bytes = result * PAGE_SIZE;
340 if (bytes <= pgbase) {
341 nfs_direct_release_pages(pagevec, result);
348 for (i = 0; i < npages; i++) {
349 struct nfs_page *req;
350 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
351 /* XXX do we need to do the eof zeroing found in async_filler? */
352 req = nfs_create_request(dreq->ctx, dreq->inode,
356 result = PTR_ERR(req);
359 req->wb_index = pos >> PAGE_SHIFT;
360 req->wb_offset = pos & ~PAGE_MASK;
361 if (!nfs_pageio_add_request(desc, req)) {
362 result = desc->pg_error;
363 nfs_release_request(req);
369 user_addr += req_len;
372 dreq->bytes_left -= req_len;
374 /* The nfs_page now hold references to these pages */
375 nfs_direct_release_pages(pagevec, npages);
376 } while (count != 0 && result >= 0);
382 return result < 0 ? (ssize_t) result : -EFAULT;
385 static ssize_t nfs_direct_do_schedule_read_iovec(
386 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
387 unsigned long nr_segs, loff_t pos)
389 ssize_t result = -EINVAL;
390 size_t requested_bytes = 0;
393 for (seg = 0; seg < nr_segs; seg++) {
394 const struct iovec *vec = &iov[seg];
395 result = nfs_direct_read_schedule_segment(desc, vec, pos);
398 requested_bytes += result;
399 if ((size_t)result < vec->iov_len)
404 return requested_bytes;
406 return result < 0 ? result : -EIO;
410 static ssize_t nfs_direct_do_schedule_read_bvec(
411 struct nfs_pageio_descriptor *desc,
412 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
414 struct nfs_direct_req *dreq = desc->pg_dreq;
415 struct nfs_open_context *ctx = dreq->ctx;
416 struct inode *inode = ctx->dentry->d_inode;
417 ssize_t result = -EINVAL;
418 size_t requested_bytes = 0;
420 struct nfs_page *req;
421 unsigned int req_len;
423 for (seg = 0; seg < nr_segs; seg++) {
425 req_len = bvec[seg].bv_len;
426 req = nfs_create_request(ctx, inode,
428 bvec[seg].bv_offset, req_len);
430 result = PTR_ERR(req);
433 req->wb_index = pos >> PAGE_SHIFT;
434 req->wb_offset = pos & ~PAGE_MASK;
435 if (!nfs_pageio_add_request(desc, req)) {
436 result = desc->pg_error;
437 nfs_release_request(req);
440 requested_bytes += req_len;
445 return requested_bytes;
447 return result < 0 ? result : -EIO;
449 #endif /* CONFIG_BLOCK */
451 static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq,
452 struct iov_iter *iter, loff_t pos)
454 struct nfs_pageio_descriptor desc;
457 NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
458 &nfs_direct_read_completion_ops);
462 if (iov_iter_has_iovec(iter)) {
463 result = nfs_direct_do_schedule_read_iovec(&desc,
464 iov_iter_iovec(iter), iter->nr_segs, pos);
466 } else if (iov_iter_has_bvec(iter)) {
467 result = nfs_direct_do_schedule_read_bvec(&desc,
468 iov_iter_bvec(iter), iter->nr_segs, pos);
473 nfs_pageio_complete(&desc);
476 * If no bytes were started, return the error, and let the
477 * generic layer handle the completion.
480 nfs_direct_req_release(dreq);
485 nfs_direct_complete(dreq);
489 static ssize_t nfs_direct_read(struct kiocb *iocb, struct iov_iter *iter,
492 ssize_t result = -ENOMEM;
493 struct inode *inode = iocb->ki_filp->f_mapping->host;
494 struct nfs_direct_req *dreq;
495 struct nfs_lock_context *l_ctx;
497 dreq = nfs_direct_req_alloc();
502 dreq->bytes_left = iov_iter_count(iter);
503 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
504 l_ctx = nfs_get_lock_context(dreq->ctx);
506 result = PTR_ERR(l_ctx);
510 if (!is_sync_kiocb(iocb))
513 NFS_I(inode)->read_io += iov_iter_count(iter);
514 result = nfs_direct_read_schedule(dreq, iter, pos);
516 result = nfs_direct_wait(dreq);
518 nfs_direct_req_release(dreq);
523 static void nfs_inode_dio_write_done(struct inode *inode)
525 nfs_zap_mapping(inode, inode->i_mapping);
526 inode_dio_done(inode);
529 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
530 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
532 struct nfs_pageio_descriptor desc;
533 struct nfs_page *req, *tmp;
535 struct nfs_commit_info cinfo;
538 nfs_init_cinfo_from_dreq(&cinfo, dreq);
539 pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
540 spin_lock(cinfo.lock);
541 nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
542 spin_unlock(cinfo.lock);
547 NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
548 &nfs_direct_write_completion_ops);
551 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
552 if (!nfs_pageio_add_request(&desc, req)) {
553 nfs_list_remove_request(req);
554 nfs_list_add_request(req, &failed);
555 spin_lock(cinfo.lock);
558 spin_unlock(cinfo.lock);
560 nfs_release_request(req);
562 nfs_pageio_complete(&desc);
564 while (!list_empty(&failed)) {
565 req = nfs_list_entry(failed.next);
566 nfs_list_remove_request(req);
567 nfs_unlock_and_release_request(req);
571 nfs_direct_write_complete(dreq, dreq->inode);
574 static void nfs_direct_commit_complete(struct nfs_commit_data *data)
576 struct nfs_direct_req *dreq = data->dreq;
577 struct nfs_commit_info cinfo;
578 struct nfs_page *req;
579 int status = data->task.tk_status;
581 nfs_init_cinfo_from_dreq(&cinfo, dreq);
583 dprintk("NFS: %5u commit failed with error %d.\n",
584 data->task.tk_pid, status);
585 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
586 } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
587 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
588 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
591 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
592 while (!list_empty(&data->pages)) {
593 req = nfs_list_entry(data->pages.next);
594 nfs_list_remove_request(req);
595 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
596 /* Note the rewrite will go through mds */
597 nfs_mark_request_commit(req, NULL, &cinfo);
599 nfs_release_request(req);
600 nfs_unlock_and_release_request(req);
603 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
604 nfs_direct_write_complete(dreq, data->inode);
607 static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
609 /* There is no lock to clear */
612 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
613 .completion = nfs_direct_commit_complete,
614 .error_cleanup = nfs_direct_error_cleanup,
617 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
620 struct nfs_commit_info cinfo;
623 nfs_init_cinfo_from_dreq(&cinfo, dreq);
624 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
625 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
626 if (res < 0) /* res == -ENOMEM */
627 nfs_direct_write_reschedule(dreq);
630 static void nfs_direct_write_schedule_work(struct work_struct *work)
632 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
633 int flags = dreq->flags;
637 case NFS_ODIRECT_DO_COMMIT:
638 nfs_direct_commit_schedule(dreq);
640 case NFS_ODIRECT_RESCHED_WRITES:
641 nfs_direct_write_reschedule(dreq);
644 nfs_inode_dio_write_done(dreq->inode);
645 nfs_direct_complete(dreq);
649 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
651 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
655 static void nfs_direct_write_schedule_work(struct work_struct *work)
659 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
661 nfs_inode_dio_write_done(inode);
662 nfs_direct_complete(dreq);
667 * NB: Return the value of the first error return code. Subsequent
668 * errors after the first one are ignored.
671 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
672 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
673 * bail and stop sending more writes. Write length accounting is
674 * handled automatically by nfs_direct_write_result(). Otherwise, if
675 * no requests have been sent, just return an error.
677 static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
678 const struct iovec *iov,
681 struct nfs_direct_req *dreq = desc->pg_dreq;
682 struct nfs_open_context *ctx = dreq->ctx;
683 struct inode *inode = ctx->dentry->d_inode;
684 unsigned long user_addr = (unsigned long)iov->iov_base;
685 size_t count = iov->iov_len;
686 size_t wsize = NFS_SERVER(inode)->wsize;
690 struct page **pagevec = NULL;
697 pgbase = user_addr & ~PAGE_MASK;
698 bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
701 npages = nfs_page_array_len(pgbase, bytes);
703 pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
707 down_read(¤t->mm->mmap_sem);
708 result = get_user_pages(current, current->mm, user_addr,
709 npages, 0, 0, pagevec, NULL);
710 up_read(¤t->mm->mmap_sem);
714 if ((unsigned)result < npages) {
715 bytes = result * PAGE_SIZE;
716 if (bytes <= pgbase) {
717 nfs_direct_release_pages(pagevec, result);
724 for (i = 0; i < npages; i++) {
725 struct nfs_page *req;
726 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
728 req = nfs_create_request(dreq->ctx, dreq->inode,
732 result = PTR_ERR(req);
735 nfs_lock_request(req);
736 req->wb_index = pos >> PAGE_SHIFT;
737 req->wb_offset = pos & ~PAGE_MASK;
738 if (!nfs_pageio_add_request(desc, req)) {
739 result = desc->pg_error;
740 nfs_unlock_and_release_request(req);
746 user_addr += req_len;
749 dreq->bytes_left -= req_len;
751 /* The nfs_page now hold references to these pages */
752 nfs_direct_release_pages(pagevec, npages);
753 } while (count != 0 && result >= 0);
759 return result < 0 ? (ssize_t) result : -EFAULT;
762 static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
764 struct nfs_direct_req *dreq = hdr->dreq;
765 struct nfs_commit_info cinfo;
767 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
769 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
772 nfs_init_cinfo_from_dreq(&cinfo, dreq);
774 spin_lock(&dreq->lock);
776 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
778 dreq->error = hdr->error;
780 if (dreq->error != 0)
781 bit = NFS_IOHDR_ERROR;
783 dreq->count += hdr->good_bytes;
784 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
785 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
786 bit = NFS_IOHDR_NEED_RESCHED;
787 } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
788 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
789 bit = NFS_IOHDR_NEED_RESCHED;
790 else if (dreq->flags == 0) {
791 memcpy(&dreq->verf, hdr->verf,
793 bit = NFS_IOHDR_NEED_COMMIT;
794 dreq->flags = NFS_ODIRECT_DO_COMMIT;
795 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
796 if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
797 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
798 bit = NFS_IOHDR_NEED_RESCHED;
800 bit = NFS_IOHDR_NEED_COMMIT;
804 spin_unlock(&dreq->lock);
806 while (!list_empty(&hdr->pages)) {
807 req = nfs_list_entry(hdr->pages.next);
808 nfs_list_remove_request(req);
810 case NFS_IOHDR_NEED_RESCHED:
811 case NFS_IOHDR_NEED_COMMIT:
812 kref_get(&req->wb_kref);
813 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
815 nfs_unlock_and_release_request(req);
820 nfs_direct_write_complete(dreq, hdr->inode);
824 static void nfs_write_sync_pgio_error(struct list_head *head)
826 struct nfs_page *req;
828 while (!list_empty(head)) {
829 req = nfs_list_entry(head->next);
830 nfs_list_remove_request(req);
831 nfs_unlock_and_release_request(req);
835 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
836 .error_cleanup = nfs_write_sync_pgio_error,
837 .init_hdr = nfs_direct_pgio_init,
838 .completion = nfs_direct_write_completion,
841 static ssize_t nfs_direct_do_schedule_write_iovec(
842 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
843 unsigned long nr_segs, loff_t pos)
845 ssize_t result = -EINVAL;
846 size_t requested_bytes = 0;
849 for (seg = 0; seg < nr_segs; seg++) {
850 const struct iovec *vec = &iov[seg];
851 result = nfs_direct_write_schedule_segment(desc, vec,
855 requested_bytes += result;
856 if ((size_t)result < vec->iov_len)
862 return requested_bytes;
864 return result < 0 ? result : -EIO;
868 static ssize_t nfs_direct_do_schedule_write_bvec(
869 struct nfs_pageio_descriptor *desc,
870 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
872 struct nfs_direct_req *dreq = desc->pg_dreq;
873 struct nfs_open_context *ctx = dreq->ctx;
874 struct inode *inode = dreq->inode;
876 size_t requested_bytes = 0;
878 struct nfs_page *req;
879 unsigned int req_len;
881 for (seg = 0; seg < nr_segs; seg++) {
882 req_len = bvec[seg].bv_len;
884 req = nfs_create_request(ctx, inode, bvec[seg].bv_page,
885 bvec[seg].bv_offset, req_len);
887 result = PTR_ERR(req);
890 nfs_lock_request(req);
891 req->wb_index = pos >> PAGE_SHIFT;
892 req->wb_offset = pos & ~PAGE_MASK;
893 if (!nfs_pageio_add_request(desc, req)) {
894 result = desc->pg_error;
895 nfs_unlock_and_release_request(req);
898 requested_bytes += req_len;
903 return requested_bytes;
905 return result < 0 ? result : -EIO;
907 #endif /* CONFIG_BLOCK */
909 static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq,
910 struct iov_iter *iter, loff_t pos)
912 struct nfs_pageio_descriptor desc;
913 struct inode *inode = dreq->inode;
916 NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
917 &nfs_direct_write_completion_ops);
920 atomic_inc(&inode->i_dio_count);
922 NFS_I(dreq->inode)->write_io += iov_iter_count(iter);
924 if (iov_iter_has_iovec(iter)) {
925 result = nfs_direct_do_schedule_write_iovec(&desc,
926 iov_iter_iovec(iter), iter->nr_segs, pos);
928 } else if (iov_iter_has_bvec(iter)) {
929 result = nfs_direct_do_schedule_write_bvec(&desc,
930 iov_iter_bvec(iter), iter->nr_segs, pos);
935 nfs_pageio_complete(&desc);
938 * If no bytes were started, return the error, and let the
939 * generic layer handle the completion.
942 inode_dio_done(inode);
943 nfs_direct_req_release(dreq);
948 nfs_direct_write_complete(dreq, dreq->inode);
952 static ssize_t nfs_direct_write(struct kiocb *iocb, struct iov_iter *iter,
955 ssize_t result = -ENOMEM;
956 struct inode *inode = iocb->ki_filp->f_mapping->host;
957 struct nfs_direct_req *dreq;
958 struct nfs_lock_context *l_ctx;
960 dreq = nfs_direct_req_alloc();
965 dreq->bytes_left = iov_iter_count(iter);
966 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
967 l_ctx = nfs_get_lock_context(dreq->ctx);
969 result = PTR_ERR(l_ctx);
973 if (!is_sync_kiocb(iocb))
976 result = nfs_direct_write_schedule(dreq, iter, pos);
978 result = nfs_direct_wait(dreq);
980 nfs_direct_req_release(dreq);
986 * nfs_file_direct_read - file direct read operation for NFS files
987 * @iocb: target I/O control block
988 * @iter: vector of buffers into which to read data
989 * @pos: byte offset in file where reading starts
991 * We use this function for direct reads instead of calling
992 * generic_file_read_iter() in order to avoid gfar's check to see if
993 * the request starts before the end of the file. For that check
994 * to work, we must generate a GETATTR before each direct read, and
995 * even then there is a window between the GETATTR and the subsequent
996 * READ where the file size could change. Our preference is simply
997 * to do all reads the application wants, and the server will take
998 * care of managing the end of file boundary.
1000 * This function also eliminates unnecessarily updating the file's
1001 * atime locally, as the NFS server sets the file's atime, and this
1002 * client must read the updated atime from the server back into its
1005 ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
1008 ssize_t retval = -EINVAL;
1009 struct file *file = iocb->ki_filp;
1010 struct address_space *mapping = file->f_mapping;
1013 count = iov_iter_count(iter);
1014 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
1016 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
1017 file->f_path.dentry->d_parent->d_name.name,
1018 file->f_path.dentry->d_name.name,
1019 count, (long long) pos);
1025 retval = nfs_sync_mapping(mapping);
1029 task_io_account_read(count);
1031 retval = nfs_direct_read(iocb, iter, pos);
1033 iocb->ki_pos = pos + retval;
1040 * nfs_file_direct_write - file direct write operation for NFS files
1041 * @iocb: target I/O control block
1042 * @iter: vector of buffers from which to write data
1043 * @pos: byte offset in file where writing starts
1045 * We use this function for direct writes instead of calling
1046 * generic_file_write_iter() in order to avoid taking the inode
1047 * semaphore and updating the i_size. The NFS server will set
1048 * the new i_size and this client must read the updated size
1049 * back into its cache. We let the server do generic write
1050 * parameter checking and report problems.
1052 * We eliminate local atime updates, see direct read above.
1054 * We avoid unnecessary page cache invalidations for normal cached
1055 * readers of this file.
1057 * Note that O_APPEND is not supported for NFS direct writes, as there
1058 * is no atomic O_APPEND write facility in the NFS protocol.
1060 ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
1063 ssize_t retval = -EINVAL;
1064 struct file *file = iocb->ki_filp;
1065 struct address_space *mapping = file->f_mapping;
1068 count = iov_iter_count(iter);
1069 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
1071 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
1072 file->f_path.dentry->d_parent->d_name.name,
1073 file->f_path.dentry->d_name.name,
1074 count, (long long) pos);
1076 retval = generic_write_checks(file, &pos, &count, 0);
1081 if ((ssize_t) count < 0)
1087 retval = nfs_sync_mapping(mapping);
1091 task_io_account_write(count);
1093 retval = nfs_direct_write(iocb, iter, pos);
1095 struct inode *inode = mapping->host;
1097 iocb->ki_pos = pos + retval;
1098 spin_lock(&inode->i_lock);
1099 if (i_size_read(inode) < iocb->ki_pos)
1100 i_size_write(inode, iocb->ki_pos);
1101 spin_unlock(&inode->i_lock);
1108 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1111 int __init nfs_init_directcache(void)
1113 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
1114 sizeof(struct nfs_direct_req),
1115 0, (SLAB_RECLAIM_ACCOUNT|
1118 if (nfs_direct_cachep == NULL)
1125 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1128 void nfs_destroy_directcache(void)
1130 kmem_cache_destroy(nfs_direct_cachep);