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 (%pD) off/no(%Ld/%lu) EINVAL\n",
129 iocb->ki_filp, (long long) pos, iter->nr_segs);
134 static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
137 for (i = 0; i < npages; i++)
138 page_cache_release(pages[i]);
141 void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
142 struct nfs_direct_req *dreq)
144 cinfo->lock = &dreq->lock;
145 cinfo->mds = &dreq->mds_cinfo;
146 cinfo->ds = &dreq->ds_cinfo;
148 cinfo->completion_ops = &nfs_direct_commit_completion_ops;
151 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
153 struct nfs_direct_req *dreq;
155 dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
159 kref_init(&dreq->kref);
160 kref_get(&dreq->kref);
161 init_completion(&dreq->completion);
162 INIT_LIST_HEAD(&dreq->mds_cinfo.list);
163 INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
164 spin_lock_init(&dreq->lock);
169 static void nfs_direct_req_free(struct kref *kref)
171 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
173 if (dreq->l_ctx != NULL)
174 nfs_put_lock_context(dreq->l_ctx);
175 if (dreq->ctx != NULL)
176 put_nfs_open_context(dreq->ctx);
177 kmem_cache_free(nfs_direct_cachep, dreq);
180 static void nfs_direct_req_release(struct nfs_direct_req *dreq)
182 kref_put(&dreq->kref, nfs_direct_req_free);
185 ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
187 return dreq->bytes_left;
189 EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
192 * Collects and returns the final error value/byte-count.
194 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
196 ssize_t result = -EIOCBQUEUED;
198 /* Async requests don't wait here */
202 result = wait_for_completion_killable(&dreq->completion);
205 result = dreq->error;
207 result = dreq->count;
210 return (ssize_t) result;
214 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
215 * the iocb is still valid here if this is a synchronous request.
217 static void nfs_direct_complete(struct nfs_direct_req *dreq)
220 long res = (long) dreq->error;
222 res = (long) dreq->count;
223 aio_complete(dreq->iocb, res, 0);
225 complete_all(&dreq->completion);
227 nfs_direct_req_release(dreq);
230 static void nfs_direct_readpage_release(struct nfs_page *req)
232 dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
233 req->wb_context->dentry->d_inode->i_sb->s_id,
234 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
236 (long long)req_offset(req));
237 nfs_release_request(req);
240 static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
242 unsigned long bytes = 0;
243 struct nfs_direct_req *dreq = hdr->dreq;
245 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
248 spin_lock(&dreq->lock);
249 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
250 dreq->error = hdr->error;
252 dreq->count += hdr->good_bytes;
253 spin_unlock(&dreq->lock);
255 while (!list_empty(&hdr->pages)) {
256 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
257 struct page *page = req->wb_page;
259 if ((dreq->flags & NFS_ODIRECT_MARK_DIRTY) &&
260 !PageCompound(page) && bytes < hdr->good_bytes)
261 set_page_dirty(page);
262 bytes += req->wb_bytes;
263 nfs_list_remove_request(req);
264 nfs_direct_readpage_release(req);
268 nfs_direct_complete(dreq);
272 static void nfs_read_sync_pgio_error(struct list_head *head)
274 struct nfs_page *req;
276 while (!list_empty(head)) {
277 req = nfs_list_entry(head->next);
278 nfs_list_remove_request(req);
279 nfs_release_request(req);
283 static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
288 static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
289 .error_cleanup = nfs_read_sync_pgio_error,
290 .init_hdr = nfs_direct_pgio_init,
291 .completion = nfs_direct_read_completion,
295 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
296 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
297 * bail and stop sending more reads. Read length accounting is
298 * handled automatically by nfs_direct_read_result(). Otherwise, if
299 * no requests have been sent, just return an error.
301 static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
302 const struct iovec *iov,
305 struct nfs_direct_req *dreq = desc->pg_dreq;
306 struct nfs_open_context *ctx = dreq->ctx;
307 struct inode *inode = ctx->dentry->d_inode;
308 unsigned long user_addr = (unsigned long)iov->iov_base;
309 size_t count = iov->iov_len;
310 size_t rsize = NFS_SERVER(inode)->rsize;
314 struct page **pagevec = NULL;
321 pgbase = user_addr & ~PAGE_MASK;
322 bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
325 npages = nfs_page_array_len(pgbase, bytes);
327 pagevec = kmalloc(npages * sizeof(struct page *),
331 down_read(¤t->mm->mmap_sem);
332 result = get_user_pages(current, current->mm, user_addr,
333 npages, 1, 0, pagevec, NULL);
334 up_read(¤t->mm->mmap_sem);
337 if ((unsigned)result < npages) {
338 bytes = result * PAGE_SIZE;
339 if (bytes <= pgbase) {
340 nfs_direct_release_pages(pagevec, result);
347 for (i = 0; i < npages; i++) {
348 struct nfs_page *req;
349 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
350 /* XXX do we need to do the eof zeroing found in async_filler? */
351 req = nfs_create_request(dreq->ctx, dreq->inode,
355 result = PTR_ERR(req);
358 req->wb_index = pos >> PAGE_SHIFT;
359 req->wb_offset = pos & ~PAGE_MASK;
360 if (!nfs_pageio_add_request(desc, req)) {
361 result = desc->pg_error;
362 nfs_release_request(req);
368 user_addr += req_len;
371 dreq->bytes_left -= req_len;
373 /* The nfs_page now hold references to these pages */
374 nfs_direct_release_pages(pagevec, npages);
375 } while (count != 0 && result >= 0);
381 return result < 0 ? (ssize_t) result : -EFAULT;
384 static ssize_t nfs_direct_do_schedule_read_iovec(
385 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
386 unsigned long nr_segs, loff_t pos)
388 ssize_t result = -EINVAL;
389 size_t requested_bytes = 0;
392 for (seg = 0; seg < nr_segs; seg++) {
393 const struct iovec *vec = &iov[seg];
394 result = nfs_direct_read_schedule_segment(desc, vec, pos);
397 requested_bytes += result;
398 if ((size_t)result < vec->iov_len)
403 return requested_bytes;
405 return result < 0 ? result : -EIO;
409 static ssize_t nfs_direct_do_schedule_read_bvec(
410 struct nfs_pageio_descriptor *desc,
411 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
413 struct nfs_direct_req *dreq = desc->pg_dreq;
414 struct nfs_open_context *ctx = dreq->ctx;
415 struct inode *inode = ctx->dentry->d_inode;
416 ssize_t result = -EINVAL;
417 size_t requested_bytes = 0;
419 struct nfs_page *req;
420 unsigned int req_len;
422 for (seg = 0; seg < nr_segs; seg++) {
424 req_len = bvec[seg].bv_len;
425 req = nfs_create_request(ctx, inode,
427 bvec[seg].bv_offset, req_len);
429 result = PTR_ERR(req);
432 req->wb_index = pos >> PAGE_SHIFT;
433 req->wb_offset = pos & ~PAGE_MASK;
434 if (!nfs_pageio_add_request(desc, req)) {
435 result = desc->pg_error;
436 nfs_release_request(req);
439 requested_bytes += req_len;
444 return requested_bytes;
446 return result < 0 ? result : -EIO;
448 #endif /* CONFIG_BLOCK */
450 static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq,
451 struct iov_iter *iter, loff_t pos)
453 struct nfs_pageio_descriptor desc;
456 NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
457 &nfs_direct_read_completion_ops);
461 if (iov_iter_has_iovec(iter)) {
462 result = nfs_direct_do_schedule_read_iovec(&desc,
463 iov_iter_iovec(iter), iter->nr_segs, pos);
465 } else if (iov_iter_has_bvec(iter)) {
466 result = nfs_direct_do_schedule_read_bvec(&desc,
467 iov_iter_bvec(iter), iter->nr_segs, pos);
472 nfs_pageio_complete(&desc);
475 * If no bytes were started, return the error, and let the
476 * generic layer handle the completion.
479 nfs_direct_req_release(dreq);
484 nfs_direct_complete(dreq);
488 static ssize_t nfs_direct_read(struct kiocb *iocb, struct iov_iter *iter,
491 ssize_t result = -ENOMEM;
492 struct inode *inode = iocb->ki_filp->f_mapping->host;
493 struct nfs_direct_req *dreq;
494 struct nfs_lock_context *l_ctx;
496 dreq = nfs_direct_req_alloc();
501 dreq->bytes_left = iov_iter_count(iter);
502 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
503 l_ctx = nfs_get_lock_context(dreq->ctx);
505 result = PTR_ERR(l_ctx);
509 if (!is_sync_kiocb(iocb))
512 NFS_I(inode)->read_io += iov_iter_count(iter);
513 result = nfs_direct_read_schedule(dreq, iter, pos);
515 result = nfs_direct_wait(dreq);
517 nfs_direct_req_release(dreq);
522 static void nfs_inode_dio_write_done(struct inode *inode)
524 nfs_zap_mapping(inode, inode->i_mapping);
525 inode_dio_done(inode);
528 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
529 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
531 struct nfs_pageio_descriptor desc;
532 struct nfs_page *req, *tmp;
534 struct nfs_commit_info cinfo;
537 nfs_init_cinfo_from_dreq(&cinfo, dreq);
538 pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
539 spin_lock(cinfo.lock);
540 nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
541 spin_unlock(cinfo.lock);
546 NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
547 &nfs_direct_write_completion_ops);
550 list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
551 if (!nfs_pageio_add_request(&desc, req)) {
552 nfs_list_remove_request(req);
553 nfs_list_add_request(req, &failed);
554 spin_lock(cinfo.lock);
557 spin_unlock(cinfo.lock);
559 nfs_release_request(req);
561 nfs_pageio_complete(&desc);
563 while (!list_empty(&failed)) {
564 req = nfs_list_entry(failed.next);
565 nfs_list_remove_request(req);
566 nfs_unlock_and_release_request(req);
570 nfs_direct_write_complete(dreq, dreq->inode);
573 static void nfs_direct_commit_complete(struct nfs_commit_data *data)
575 struct nfs_direct_req *dreq = data->dreq;
576 struct nfs_commit_info cinfo;
577 struct nfs_page *req;
578 int status = data->task.tk_status;
580 nfs_init_cinfo_from_dreq(&cinfo, dreq);
582 dprintk("NFS: %5u commit failed with error %d.\n",
583 data->task.tk_pid, status);
584 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
585 } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
586 dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
587 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
590 dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
591 while (!list_empty(&data->pages)) {
592 req = nfs_list_entry(data->pages.next);
593 nfs_list_remove_request(req);
594 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
595 /* Note the rewrite will go through mds */
596 nfs_mark_request_commit(req, NULL, &cinfo);
598 nfs_release_request(req);
599 nfs_unlock_and_release_request(req);
602 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
603 nfs_direct_write_complete(dreq, data->inode);
606 static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
608 /* There is no lock to clear */
611 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
612 .completion = nfs_direct_commit_complete,
613 .error_cleanup = nfs_direct_error_cleanup,
616 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
619 struct nfs_commit_info cinfo;
622 nfs_init_cinfo_from_dreq(&cinfo, dreq);
623 nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
624 res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
625 if (res < 0) /* res == -ENOMEM */
626 nfs_direct_write_reschedule(dreq);
629 static void nfs_direct_write_schedule_work(struct work_struct *work)
631 struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
632 int flags = dreq->flags;
636 case NFS_ODIRECT_DO_COMMIT:
637 nfs_direct_commit_schedule(dreq);
639 case NFS_ODIRECT_RESCHED_WRITES:
640 nfs_direct_write_reschedule(dreq);
643 nfs_inode_dio_write_done(dreq->inode);
644 nfs_direct_complete(dreq);
648 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
650 schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
654 static void nfs_direct_write_schedule_work(struct work_struct *work)
658 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
660 nfs_inode_dio_write_done(inode);
661 nfs_direct_complete(dreq);
666 * NB: Return the value of the first error return code. Subsequent
667 * errors after the first one are ignored.
670 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
671 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
672 * bail and stop sending more writes. Write length accounting is
673 * handled automatically by nfs_direct_write_result(). Otherwise, if
674 * no requests have been sent, just return an error.
676 static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
677 const struct iovec *iov,
680 struct nfs_direct_req *dreq = desc->pg_dreq;
681 struct nfs_open_context *ctx = dreq->ctx;
682 struct inode *inode = ctx->dentry->d_inode;
683 unsigned long user_addr = (unsigned long)iov->iov_base;
684 size_t count = iov->iov_len;
685 size_t wsize = NFS_SERVER(inode)->wsize;
689 struct page **pagevec = NULL;
696 pgbase = user_addr & ~PAGE_MASK;
697 bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
700 npages = nfs_page_array_len(pgbase, bytes);
702 pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
706 down_read(¤t->mm->mmap_sem);
707 result = get_user_pages(current, current->mm, user_addr,
708 npages, 0, 0, pagevec, NULL);
709 up_read(¤t->mm->mmap_sem);
713 if ((unsigned)result < npages) {
714 bytes = result * PAGE_SIZE;
715 if (bytes <= pgbase) {
716 nfs_direct_release_pages(pagevec, result);
723 for (i = 0; i < npages; i++) {
724 struct nfs_page *req;
725 unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
727 req = nfs_create_request(dreq->ctx, dreq->inode,
731 result = PTR_ERR(req);
734 nfs_lock_request(req);
735 req->wb_index = pos >> PAGE_SHIFT;
736 req->wb_offset = pos & ~PAGE_MASK;
737 if (!nfs_pageio_add_request(desc, req)) {
738 result = desc->pg_error;
739 nfs_unlock_and_release_request(req);
745 user_addr += req_len;
748 dreq->bytes_left -= req_len;
750 /* The nfs_page now hold references to these pages */
751 nfs_direct_release_pages(pagevec, npages);
752 } while (count != 0 && result >= 0);
758 return result < 0 ? (ssize_t) result : -EFAULT;
761 static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
763 struct nfs_direct_req *dreq = hdr->dreq;
764 struct nfs_commit_info cinfo;
766 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
768 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
771 nfs_init_cinfo_from_dreq(&cinfo, dreq);
773 spin_lock(&dreq->lock);
775 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
777 dreq->error = hdr->error;
779 if (dreq->error != 0)
780 bit = NFS_IOHDR_ERROR;
782 dreq->count += hdr->good_bytes;
783 if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
784 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
785 bit = NFS_IOHDR_NEED_RESCHED;
786 } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
787 if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
788 bit = NFS_IOHDR_NEED_RESCHED;
789 else if (dreq->flags == 0) {
790 memcpy(&dreq->verf, hdr->verf,
792 bit = NFS_IOHDR_NEED_COMMIT;
793 dreq->flags = NFS_ODIRECT_DO_COMMIT;
794 } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
795 if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
796 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
797 bit = NFS_IOHDR_NEED_RESCHED;
799 bit = NFS_IOHDR_NEED_COMMIT;
803 spin_unlock(&dreq->lock);
805 while (!list_empty(&hdr->pages)) {
806 req = nfs_list_entry(hdr->pages.next);
807 nfs_list_remove_request(req);
809 case NFS_IOHDR_NEED_RESCHED:
810 case NFS_IOHDR_NEED_COMMIT:
811 kref_get(&req->wb_kref);
812 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
814 nfs_unlock_and_release_request(req);
819 nfs_direct_write_complete(dreq, hdr->inode);
823 static void nfs_write_sync_pgio_error(struct list_head *head)
825 struct nfs_page *req;
827 while (!list_empty(head)) {
828 req = nfs_list_entry(head->next);
829 nfs_list_remove_request(req);
830 nfs_unlock_and_release_request(req);
834 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
835 .error_cleanup = nfs_write_sync_pgio_error,
836 .init_hdr = nfs_direct_pgio_init,
837 .completion = nfs_direct_write_completion,
840 static ssize_t nfs_direct_do_schedule_write_iovec(
841 struct nfs_pageio_descriptor *desc, const struct iovec *iov,
842 unsigned long nr_segs, loff_t pos)
844 ssize_t result = -EINVAL;
845 size_t requested_bytes = 0;
848 for (seg = 0; seg < nr_segs; seg++) {
849 const struct iovec *vec = &iov[seg];
850 result = nfs_direct_write_schedule_segment(desc, vec,
854 requested_bytes += result;
855 if ((size_t)result < vec->iov_len)
861 return requested_bytes;
863 return result < 0 ? result : -EIO;
867 static ssize_t nfs_direct_do_schedule_write_bvec(
868 struct nfs_pageio_descriptor *desc,
869 struct bio_vec *bvec, unsigned long nr_segs, loff_t pos)
871 struct nfs_direct_req *dreq = desc->pg_dreq;
872 struct nfs_open_context *ctx = dreq->ctx;
873 struct inode *inode = dreq->inode;
875 size_t requested_bytes = 0;
877 struct nfs_page *req;
878 unsigned int req_len;
880 for (seg = 0; seg < nr_segs; seg++) {
881 req_len = bvec[seg].bv_len;
883 req = nfs_create_request(ctx, inode, bvec[seg].bv_page,
884 bvec[seg].bv_offset, req_len);
886 result = PTR_ERR(req);
889 nfs_lock_request(req);
890 req->wb_index = pos >> PAGE_SHIFT;
891 req->wb_offset = pos & ~PAGE_MASK;
892 if (!nfs_pageio_add_request(desc, req)) {
893 result = desc->pg_error;
894 nfs_unlock_and_release_request(req);
897 requested_bytes += req_len;
902 return requested_bytes;
904 return result < 0 ? result : -EIO;
906 #endif /* CONFIG_BLOCK */
908 static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq,
909 struct iov_iter *iter, loff_t pos)
911 struct nfs_pageio_descriptor desc;
912 struct inode *inode = dreq->inode;
915 NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
916 &nfs_direct_write_completion_ops);
919 atomic_inc(&inode->i_dio_count);
921 NFS_I(dreq->inode)->write_io += iov_iter_count(iter);
923 if (iov_iter_has_iovec(iter)) {
924 result = nfs_direct_do_schedule_write_iovec(&desc,
925 iov_iter_iovec(iter), iter->nr_segs, pos);
927 } else if (iov_iter_has_bvec(iter)) {
928 result = nfs_direct_do_schedule_write_bvec(&desc,
929 iov_iter_bvec(iter), iter->nr_segs, pos);
934 nfs_pageio_complete(&desc);
937 * If no bytes were started, return the error, and let the
938 * generic layer handle the completion.
941 inode_dio_done(inode);
942 nfs_direct_req_release(dreq);
947 nfs_direct_write_complete(dreq, dreq->inode);
951 static ssize_t nfs_direct_write(struct kiocb *iocb, struct iov_iter *iter,
954 ssize_t result = -ENOMEM;
955 struct inode *inode = iocb->ki_filp->f_mapping->host;
956 struct nfs_direct_req *dreq;
957 struct nfs_lock_context *l_ctx;
959 dreq = nfs_direct_req_alloc();
964 dreq->bytes_left = iov_iter_count(iter);
965 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
966 l_ctx = nfs_get_lock_context(dreq->ctx);
968 result = PTR_ERR(l_ctx);
972 if (!is_sync_kiocb(iocb))
975 result = nfs_direct_write_schedule(dreq, iter, pos);
977 result = nfs_direct_wait(dreq);
979 nfs_direct_req_release(dreq);
985 * nfs_file_direct_read - file direct read operation for NFS files
986 * @iocb: target I/O control block
987 * @iter: vector of buffers into which to read data
988 * @pos: byte offset in file where reading starts
990 * We use this function for direct reads instead of calling
991 * generic_file_read_iter() in order to avoid gfar's check to see if
992 * the request starts before the end of the file. For that check
993 * to work, we must generate a GETATTR before each direct read, and
994 * even then there is a window between the GETATTR and the subsequent
995 * READ where the file size could change. Our preference is simply
996 * to do all reads the application wants, and the server will take
997 * care of managing the end of file boundary.
999 * This function also eliminates unnecessarily updating the file's
1000 * atime locally, as the NFS server sets the file's atime, and this
1001 * client must read the updated atime from the server back into its
1004 ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
1007 ssize_t retval = -EINVAL;
1008 struct file *file = iocb->ki_filp;
1009 struct address_space *mapping = file->f_mapping;
1012 count = iov_iter_count(iter);
1013 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
1015 dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
1016 file, count, (long long) pos);
1022 retval = nfs_sync_mapping(mapping);
1026 task_io_account_read(count);
1028 retval = nfs_direct_read(iocb, iter, pos);
1030 iocb->ki_pos = pos + retval;
1037 * nfs_file_direct_write - file direct write operation for NFS files
1038 * @iocb: target I/O control block
1039 * @iter: vector of buffers from which to write data
1040 * @pos: byte offset in file where writing starts
1042 * We use this function for direct writes instead of calling
1043 * generic_file_write_iter() in order to avoid taking the inode
1044 * semaphore and updating the i_size. The NFS server will set
1045 * the new i_size and this client must read the updated size
1046 * back into its cache. We let the server do generic write
1047 * parameter checking and report problems.
1049 * We eliminate local atime updates, see direct read above.
1051 * We avoid unnecessary page cache invalidations for normal cached
1052 * readers of this file.
1054 * Note that O_APPEND is not supported for NFS direct writes, as there
1055 * is no atomic O_APPEND write facility in the NFS protocol.
1057 ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter,
1060 ssize_t retval = -EINVAL;
1061 struct file *file = iocb->ki_filp;
1062 struct address_space *mapping = file->f_mapping;
1065 count = iov_iter_count(iter);
1066 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
1068 dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
1069 file, count, (long long) pos);
1071 retval = generic_write_checks(file, &pos, &count, 0);
1076 if ((ssize_t) count < 0)
1082 retval = nfs_sync_mapping(mapping);
1086 task_io_account_write(count);
1088 retval = nfs_direct_write(iocb, iter, pos);
1090 struct inode *inode = mapping->host;
1092 iocb->ki_pos = pos + retval;
1093 spin_lock(&inode->i_lock);
1094 if (i_size_read(inode) < iocb->ki_pos)
1095 i_size_write(inode, iocb->ki_pos);
1096 spin_unlock(&inode->i_lock);
1103 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1106 int __init nfs_init_directcache(void)
1108 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
1109 sizeof(struct nfs_direct_req),
1110 0, (SLAB_RECLAIM_ACCOUNT|
1113 if (nfs_direct_cachep == NULL)
1120 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1123 void nfs_destroy_directcache(void)
1125 kmem_cache_destroy(nfs_direct_cachep);