Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/nfs/direct.c |
| 4 | * |
| 5 | * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> |
| 6 | * |
| 7 | * High-performance uncached I/O for the Linux NFS client |
| 8 | * |
| 9 | * There are important applications whose performance or correctness |
| 10 | * depends on uncached access to file data. Database clusters |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 11 | * (multiple copies of the same instance running on separate hosts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * implement their own cache coherency protocol that subsumes file |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 13 | * system cache protocols. Applications that process datasets |
| 14 | * considerably larger than the client's memory do not always benefit |
| 15 | * from a local cache. A streaming video server, for instance, has no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * need to cache the contents of a file. |
| 17 | * |
| 18 | * When an application requests uncached I/O, all read and write requests |
| 19 | * are made directly to the server; data stored or fetched via these |
| 20 | * requests is not cached in the Linux page cache. The client does not |
| 21 | * correct unaligned requests from applications. All requested bytes are |
| 22 | * held on permanent storage before a direct write system call returns to |
| 23 | * an application. |
| 24 | * |
| 25 | * Solaris implements an uncached I/O facility called directio() that |
| 26 | * is used for backups and sequential I/O to very large files. Solaris |
| 27 | * also supports uncaching whole NFS partitions with "-o forcedirectio," |
| 28 | * an undocumented mount option. |
| 29 | * |
| 30 | * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with |
| 31 | * help from Andrew Morton. |
| 32 | * |
| 33 | * 18 Dec 2001 Initial implementation for 2.4 --cel |
| 34 | * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy |
| 35 | * 08 Jun 2003 Port to 2.5 APIs --cel |
| 36 | * 31 Mar 2004 Handle direct I/O without VFS support --cel |
| 37 | * 15 Sep 2004 Parallel async reads --cel |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 38 | * 04 May 2005 support O_DIRECT with aio --cel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * |
| 40 | */ |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/errno.h> |
| 43 | #include <linux/sched.h> |
| 44 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/file.h> |
| 46 | #include <linux/pagemap.h> |
| 47 | #include <linux/kref.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 48 | #include <linux/slab.h> |
Konstantin Khlebnikov | 7ec10f2 | 2011-02-22 00:28:34 +0300 | [diff] [blame] | 49 | #include <linux/task_io_accounting_ops.h> |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 50 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/nfs_fs.h> |
| 53 | #include <linux/nfs_page.h> |
| 54 | #include <linux/sunrpc/clnt.h> |
| 55 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 56 | #include <linux/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 57 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 59 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 60 | #include "iostat.h" |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 61 | #include "pnfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define NFSDBG_FACILITY NFSDBG_VFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 65 | static struct kmem_cache *nfs_direct_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct nfs_direct_req { |
| 68 | struct kref kref; /* release manager */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 69 | |
| 70 | /* I/O parameters */ |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 71 | struct nfs_open_context *ctx; /* file open context info */ |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 72 | struct nfs_lock_context *l_ctx; /* Lock context info */ |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 73 | struct kiocb * iocb; /* controlling i/o request */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 74 | struct inode * inode; /* target file of i/o */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 75 | |
| 76 | /* completion state */ |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 77 | atomic_t io_count; /* i/os we're waiting for */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 78 | spinlock_t lock; /* protect completion state */ |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 79 | |
Trond Myklebust | d9ee655 | 2018-03-06 12:47:08 -0500 | [diff] [blame] | 80 | loff_t io_start; /* Start offset for I/O */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 81 | ssize_t count, /* bytes actually processed */ |
Weston Andros Adamson | ed3743a | 2016-04-01 11:42:29 -0400 | [diff] [blame] | 82 | max_count, /* max expected count */ |
Peng Tao | 35754bc | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 83 | bytes_left, /* bytes left to be sent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | error; /* any reported error */ |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 85 | struct completion completion; /* wait for i/o completion */ |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 86 | |
| 87 | /* commit state */ |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 88 | struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ |
| 89 | struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ |
| 90 | struct work_struct work; |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 91 | int flags; |
Dave Kleikamp | ad3cba2 | 2018-11-27 19:31:30 +0000 | [diff] [blame] | 92 | /* for write */ |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 93 | #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ |
| 94 | #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ |
Dave Kleikamp | ad3cba2 | 2018-11-27 19:31:30 +0000 | [diff] [blame] | 95 | /* for read */ |
| 96 | #define NFS_ODIRECT_SHOULD_DIRTY (3) /* dirty user-space page after read */ |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 97 | #define NFS_ODIRECT_DONE INT_MAX /* write verification failed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 100 | static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; |
| 101 | static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 102 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 103 | static void nfs_direct_write_schedule_work(struct work_struct *work); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 104 | |
| 105 | static inline void get_dreq(struct nfs_direct_req *dreq) |
| 106 | { |
| 107 | atomic_inc(&dreq->io_count); |
| 108 | } |
| 109 | |
| 110 | static inline int put_dreq(struct nfs_direct_req *dreq) |
| 111 | { |
| 112 | return atomic_dec_and_test(&dreq->io_count); |
| 113 | } |
| 114 | |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 115 | static void |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 116 | nfs_direct_handle_truncated(struct nfs_direct_req *dreq, |
| 117 | const struct nfs_pgio_header *hdr, |
| 118 | ssize_t dreq_len) |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 119 | { |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 120 | if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) || |
| 121 | test_bit(NFS_IOHDR_EOF, &hdr->flags))) |
| 122 | return; |
| 123 | if (dreq->max_count >= dreq_len) { |
| 124 | dreq->max_count = dreq_len; |
| 125 | if (dreq->count > dreq_len) |
| 126 | dreq->count = dreq_len; |
Weston Andros Adamson | ed3743a | 2016-04-01 11:42:29 -0400 | [diff] [blame] | 127 | |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 128 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) |
| 129 | dreq->error = hdr->error; |
| 130 | else /* Clear outstanding error if this is EOF */ |
| 131 | dreq->error = 0; |
Peng Tao | 5fadeb4 | 2015-01-19 12:41:16 +0800 | [diff] [blame] | 132 | } |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void |
| 136 | nfs_direct_count_bytes(struct nfs_direct_req *dreq, |
| 137 | const struct nfs_pgio_header *hdr) |
| 138 | { |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 139 | loff_t hdr_end = hdr->io_start + hdr->good_bytes; |
| 140 | ssize_t dreq_len = 0; |
| 141 | |
| 142 | if (hdr_end > dreq->io_start) |
| 143 | dreq_len = hdr_end - dreq->io_start; |
| 144 | |
| 145 | nfs_direct_handle_truncated(dreq, hdr, dreq_len); |
| 146 | |
| 147 | if (dreq_len > dreq->max_count) |
| 148 | dreq_len = dreq->max_count; |
| 149 | |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 150 | if (dreq->count < dreq_len) |
| 151 | dreq->count = dreq_len; |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /** |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 155 | * nfs_direct_IO - NFS address space operation for direct I/O |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 156 | * @iocb: target I/O control block |
Al Viro | 90090ae | 2016-05-29 19:05:03 -0400 | [diff] [blame] | 157 | * @iter: I/O buffer |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 158 | * |
| 159 | * The presence of this routine in the address space ops vector means |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 160 | * the NFS client supports direct I/O. However, for most direct IO, we |
| 161 | * shunt off direct read and write requests before the VFS gets them, |
| 162 | * so this method is only ever called for swap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | */ |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 164 | ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 165 | { |
Peng Tao | ee8a1a8 | 2015-01-20 07:44:29 +0800 | [diff] [blame] | 166 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 167 | |
| 168 | /* we only support swap file calling nfs_direct_IO */ |
| 169 | if (!IS_SWAPFILE(inode)) |
| 170 | return 0; |
| 171 | |
Christoph Hellwig | 66ee59a | 2015-02-11 19:56:46 +0100 | [diff] [blame] | 172 | VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE); |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 173 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 174 | if (iov_iter_rw(iter) == READ) |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 175 | return nfs_file_direct_read(iocb, iter); |
Al Viro | 65a4a1c | 2015-04-09 14:11:08 -0400 | [diff] [blame] | 176 | return nfs_file_direct_write(iocb, iter); |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 179 | static void nfs_direct_release_pages(struct page **pages, unsigned int npages) |
Chuck Lever | 9c93ab7 | 2006-06-20 12:56:31 -0400 | [diff] [blame] | 180 | { |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 181 | unsigned int i; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 182 | for (i = 0; i < npages; i++) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 183 | put_page(pages[i]); |
Trond Myklebust | 6b45d85 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 186 | void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, |
| 187 | struct nfs_direct_req *dreq) |
| 188 | { |
Dave Wysochanski | fe238e6 | 2016-04-01 13:45:09 -0400 | [diff] [blame] | 189 | cinfo->inode = dreq->inode; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 190 | cinfo->mds = &dreq->mds_cinfo; |
| 191 | cinfo->ds = &dreq->ds_cinfo; |
| 192 | cinfo->dreq = dreq; |
| 193 | cinfo->completion_ops = &nfs_direct_commit_completion_ops; |
| 194 | } |
| 195 | |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 196 | static inline struct nfs_direct_req *nfs_direct_req_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct nfs_direct_req *dreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Trond Myklebust | 292f3ee | 2012-04-30 18:31:49 -0400 | [diff] [blame] | 200 | dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (!dreq) |
| 202 | return NULL; |
| 203 | |
| 204 | kref_init(&dreq->kref); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 205 | kref_get(&dreq->kref); |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 206 | init_completion(&dreq->completion); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 207 | INIT_LIST_HEAD(&dreq->mds_cinfo.list); |
Trond Myklebust | c21e716 | 2020-03-19 13:36:36 -0400 | [diff] [blame] | 208 | pnfs_init_ds_commit_info(&dreq->ds_cinfo); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 209 | INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 210 | spin_lock_init(&dreq->lock); |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 211 | |
| 212 | return dreq; |
| 213 | } |
| 214 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 215 | static void nfs_direct_req_free(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 218 | |
Trond Myklebust | 18f4129 | 2020-03-20 17:08:02 -0400 | [diff] [blame] | 219 | pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 220 | if (dreq->l_ctx != NULL) |
| 221 | nfs_put_lock_context(dreq->l_ctx); |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 222 | if (dreq->ctx != NULL) |
| 223 | put_nfs_open_context(dreq->ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | kmem_cache_free(nfs_direct_cachep, dreq); |
| 225 | } |
| 226 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 227 | static void nfs_direct_req_release(struct nfs_direct_req *dreq) |
| 228 | { |
| 229 | kref_put(&dreq->kref, nfs_direct_req_free); |
| 230 | } |
| 231 | |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 232 | ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) |
| 233 | { |
| 234 | return dreq->bytes_left; |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); |
| 237 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 238 | /* |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 239 | * Collects and returns the final error value/byte-count. |
| 240 | */ |
| 241 | static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) |
| 242 | { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 243 | ssize_t result = -EIOCBQUEUED; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 244 | |
| 245 | /* Async requests don't wait here */ |
| 246 | if (dreq->iocb) |
| 247 | goto out; |
| 248 | |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 249 | result = wait_for_completion_killable(&dreq->completion); |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 250 | |
Trond Myklebust | d2a7de0 | 2016-06-17 16:48:22 -0400 | [diff] [blame] | 251 | if (!result) { |
| 252 | result = dreq->count; |
| 253 | WARN_ON_ONCE(dreq->count < 0); |
| 254 | } |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 255 | if (!result) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 256 | result = dreq->error; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 257 | |
| 258 | out: |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 259 | return (ssize_t) result; |
| 260 | } |
| 261 | |
| 262 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 263 | * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust |
| 264 | * the iocb is still valid here if this is a synchronous request. |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 265 | */ |
Trond Myklebust | f7b5c34 | 2016-06-23 09:29:47 -0400 | [diff] [blame] | 266 | static void nfs_direct_complete(struct nfs_direct_req *dreq) |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 267 | { |
Christoph Hellwig | 9811cd5 | 2013-11-14 08:50:28 -0800 | [diff] [blame] | 268 | struct inode *inode = dreq->inode; |
| 269 | |
Olga Kornievskaia | 65caafd | 2020-07-15 13:04:15 -0400 | [diff] [blame] | 270 | inode_dio_end(inode); |
| 271 | |
Christoph Hellwig | 2a009ec | 2013-11-14 08:50:29 -0800 | [diff] [blame] | 272 | if (dreq->iocb) { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 273 | long res = (long) dreq->error; |
Trond Myklebust | d2a7de0 | 2016-06-17 16:48:22 -0400 | [diff] [blame] | 274 | if (dreq->count != 0) { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 275 | res = (long) dreq->count; |
Trond Myklebust | d2a7de0 | 2016-06-17 16:48:22 -0400 | [diff] [blame] | 276 | WARN_ON_ONCE(dreq->count < 0); |
| 277 | } |
Christoph Hellwig | 04b2fa9 | 2015-02-02 14:49:06 +0100 | [diff] [blame] | 278 | dreq->iocb->ki_complete(dreq->iocb, res, 0); |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 279 | } |
Christoph Hellwig | 2a009ec | 2013-11-14 08:50:29 -0800 | [diff] [blame] | 280 | |
Daniel Wagner | 024de8f | 2016-09-22 13:54:28 +0200 | [diff] [blame] | 281 | complete(&dreq->completion); |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 282 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 283 | nfs_direct_req_release(dreq); |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 286 | static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 287 | { |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 288 | unsigned long bytes = 0; |
| 289 | struct nfs_direct_req *dreq = hdr->dreq; |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame] | 290 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 291 | spin_lock(&dreq->lock); |
Trond Myklebust | eb2c50d | 2019-08-12 18:04:36 -0400 | [diff] [blame] | 292 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 293 | spin_unlock(&dreq->lock); |
| 294 | goto out_put; |
| 295 | } |
| 296 | |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 297 | nfs_direct_count_bytes(dreq, hdr); |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 298 | spin_unlock(&dreq->lock); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 299 | |
Trond Myklebust | 4bd8b01 | 2012-05-01 12:49:58 -0400 | [diff] [blame] | 300 | while (!list_empty(&hdr->pages)) { |
| 301 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); |
| 302 | struct page *page = req->wb_page; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 303 | |
Dave Kleikamp | ad3cba2 | 2018-11-27 19:31:30 +0000 | [diff] [blame] | 304 | if (!PageCompound(page) && bytes < hdr->good_bytes && |
| 305 | (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY)) |
Jeff Layton | be7e985 | 2012-12-12 12:36:31 -0500 | [diff] [blame] | 306 | set_page_dirty(page); |
Trond Myklebust | 4bd8b01 | 2012-05-01 12:49:58 -0400 | [diff] [blame] | 307 | bytes += req->wb_bytes; |
| 308 | nfs_list_remove_request(req); |
Anna Schumaker | beeb533 | 2017-04-07 14:15:07 -0400 | [diff] [blame] | 309 | nfs_release_request(req); |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 310 | } |
| 311 | out_put: |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 312 | if (put_dreq(dreq)) |
Trond Myklebust | f7b5c34 | 2016-06-23 09:29:47 -0400 | [diff] [blame] | 313 | nfs_direct_complete(dreq); |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 314 | hdr->release(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Trond Myklebust | df3accb | 2019-02-13 10:39:39 -0500 | [diff] [blame] | 317 | static void nfs_read_sync_pgio_error(struct list_head *head, int error) |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 318 | { |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 319 | struct nfs_page *req; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 320 | |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 321 | while (!list_empty(head)) { |
| 322 | req = nfs_list_entry(head->next); |
| 323 | nfs_list_remove_request(req); |
| 324 | nfs_release_request(req); |
| 325 | } |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 328 | static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) |
| 329 | { |
| 330 | get_dreq(hdr->dreq); |
| 331 | } |
| 332 | |
| 333 | static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { |
Trond Myklebust | 3e9e0ca3 | 2012-04-30 13:40:06 -0400 | [diff] [blame] | 334 | .error_cleanup = nfs_read_sync_pgio_error, |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 335 | .init_hdr = nfs_direct_pgio_init, |
| 336 | .completion = nfs_direct_read_completion, |
| 337 | }; |
| 338 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 339 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 340 | * For each rsize'd chunk of the user's buffer, dispatch an NFS READ |
| 341 | * operation. If nfs_readdata_alloc() or get_user_pages() fails, |
| 342 | * bail and stop sending more reads. Read length accounting is |
| 343 | * handled automatically by nfs_direct_read_result(). Otherwise, if |
| 344 | * no requests have been sent, just return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | */ |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 346 | |
| 347 | static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, |
| 348 | struct iov_iter *iter, |
| 349 | loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 351 | struct nfs_pageio_descriptor desc; |
| 352 | struct inode *inode = dreq->inode; |
| 353 | ssize_t result = -EINVAL; |
| 354 | size_t requested_bytes = 0; |
| 355 | size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); |
Chuck Lever | 82b145c | 2006-06-20 12:57:03 -0400 | [diff] [blame] | 356 | |
Linus Torvalds | 16b9057 | 2014-06-12 10:30:18 -0700 | [diff] [blame] | 357 | nfs_pageio_init_read(&desc, dreq->inode, false, |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 358 | &nfs_direct_read_completion_ops); |
| 359 | get_dreq(dreq); |
| 360 | desc.pg_dreq = dreq; |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 361 | inode_dio_begin(inode); |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 362 | |
| 363 | while (iov_iter_count(iter)) { |
| 364 | struct page **pagevec; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 365 | size_t bytes; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 366 | size_t pgbase; |
| 367 | unsigned npages, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 369 | result = iov_iter_get_pages_alloc(iter, &pagevec, |
| 370 | rsize, &pgbase); |
| 371 | if (result < 0) |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 372 | break; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 373 | |
| 374 | bytes = result; |
| 375 | iov_iter_advance(iter, bytes); |
| 376 | npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 377 | for (i = 0; i < npages; i++) { |
| 378 | struct nfs_page *req; |
Trond Myklebust | bf5fc40 | 2012-05-04 13:47:16 -0400 | [diff] [blame] | 379 | unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 380 | /* XXX do we need to do the eof zeroing found in async_filler? */ |
Trond Myklebust | 28b1d3f | 2019-04-07 13:59:07 -0400 | [diff] [blame] | 381 | req = nfs_create_request(dreq->ctx, pagevec[i], |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 382 | pgbase, req_len); |
| 383 | if (IS_ERR(req)) { |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 384 | result = PTR_ERR(req); |
| 385 | break; |
| 386 | } |
| 387 | req->wb_index = pos >> PAGE_SHIFT; |
| 388 | req->wb_offset = pos & ~PAGE_MASK; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 389 | if (!nfs_pageio_add_request(&desc, req)) { |
| 390 | result = desc.pg_error; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 391 | nfs_release_request(req); |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | pgbase = 0; |
| 395 | bytes -= req_len; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 396 | requested_bytes += req_len; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 397 | pos += req_len; |
Peng Tao | 35754bc | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 398 | dreq->bytes_left -= req_len; |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 399 | } |
Trond Myklebust | 6d74743 | 2012-04-30 13:27:31 -0400 | [diff] [blame] | 400 | nfs_direct_release_pages(pagevec, npages); |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 401 | kvfree(pagevec); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 402 | if (result < 0) |
| 403 | break; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Fred Isaman | 584aa81 | 2012-04-20 14:47:51 -0400 | [diff] [blame] | 406 | nfs_pageio_complete(&desc); |
| 407 | |
Chuck Lever | 839f7ad | 2011-01-21 15:54:57 +0000 | [diff] [blame] | 408 | /* |
| 409 | * If no bytes were started, return the error, and let the |
| 410 | * generic layer handle the completion. |
| 411 | */ |
| 412 | if (requested_bytes == 0) { |
Olga Kornievskaia | d03727b | 2020-06-24 13:54:08 -0400 | [diff] [blame] | 413 | inode_dio_end(inode); |
Olga Kornievskaia | 65caafd | 2020-07-15 13:04:15 -0400 | [diff] [blame] | 414 | nfs_direct_req_release(dreq); |
Chuck Lever | 839f7ad | 2011-01-21 15:54:57 +0000 | [diff] [blame] | 415 | return result < 0 ? result : -EIO; |
| 416 | } |
| 417 | |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 418 | if (put_dreq(dreq)) |
Trond Myklebust | f7b5c34 | 2016-06-23 09:29:47 -0400 | [diff] [blame] | 419 | nfs_direct_complete(dreq); |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 420 | return requested_bytes; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 423 | /** |
| 424 | * nfs_file_direct_read - file direct read operation for NFS files |
| 425 | * @iocb: target I/O control block |
Al Viro | 619d30b | 2014-03-04 21:53:33 -0500 | [diff] [blame] | 426 | * @iter: vector of user buffers into which to read data |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 427 | * |
| 428 | * We use this function for direct reads instead of calling |
| 429 | * generic_file_aio_read() in order to avoid gfar's check to see if |
| 430 | * the request starts before the end of the file. For that check |
| 431 | * to work, we must generate a GETATTR before each direct read, and |
| 432 | * even then there is a window between the GETATTR and the subsequent |
| 433 | * READ where the file size could change. Our preference is simply |
| 434 | * to do all reads the application wants, and the server will take |
| 435 | * care of managing the end of file boundary. |
| 436 | * |
| 437 | * This function also eliminates unnecessarily updating the file's |
| 438 | * atime locally, as the NFS server sets the file's atime, and this |
| 439 | * client must read the updated atime from the server back into its |
| 440 | * cache. |
| 441 | */ |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 442 | ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 444 | struct file *file = iocb->ki_filp; |
| 445 | struct address_space *mapping = file->f_mapping; |
| 446 | struct inode *inode = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct nfs_direct_req *dreq; |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 448 | struct nfs_lock_context *l_ctx; |
Colin Ian King | 86b9366 | 2020-05-27 13:56:11 +0100 | [diff] [blame] | 449 | ssize_t result, requested; |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 450 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 451 | nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); |
| 452 | |
| 453 | dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 454 | file, count, (long long) iocb->ki_pos); |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 455 | |
| 456 | result = 0; |
| 457 | if (!count) |
| 458 | goto out; |
| 459 | |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 460 | task_io_account_read(count); |
| 461 | |
| 462 | result = -ENOMEM; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 463 | dreq = nfs_direct_req_alloc(); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 464 | if (dreq == NULL) |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 465 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 467 | dreq->inode = inode; |
Weston Andros Adamson | ed3743a | 2016-04-01 11:42:29 -0400 | [diff] [blame] | 468 | dreq->bytes_left = dreq->max_count = count; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 469 | dreq->io_start = iocb->ki_pos; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 470 | dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 471 | l_ctx = nfs_get_lock_context(dreq->ctx); |
| 472 | if (IS_ERR(l_ctx)) { |
| 473 | result = PTR_ERR(l_ctx); |
Misono Tomohiro | 8605cf0 | 2019-08-28 17:01:22 +0900 | [diff] [blame] | 474 | nfs_direct_req_release(dreq); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 475 | goto out_release; |
Trond Myklebust | b3c54de | 2012-08-13 17:15:50 -0400 | [diff] [blame] | 476 | } |
| 477 | dreq->l_ctx = l_ctx; |
Chuck Lever | 487b837 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 478 | if (!is_sync_kiocb(iocb)) |
| 479 | dreq->iocb = iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Dave Kleikamp | ad3cba2 | 2018-11-27 19:31:30 +0000 | [diff] [blame] | 481 | if (iter_is_iovec(iter)) |
| 482 | dreq->flags = NFS_ODIRECT_SHOULD_DIRTY; |
| 483 | |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 484 | nfs_start_io_direct(inode); |
| 485 | |
Al Viro | 619d30b | 2014-03-04 21:53:33 -0500 | [diff] [blame] | 486 | NFS_I(inode)->read_io += count; |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 487 | requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos); |
Christoph Hellwig | d0b9875 | 2013-11-14 08:50:33 -0800 | [diff] [blame] | 488 | |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 489 | nfs_end_io_direct(inode); |
Christoph Hellwig | d0b9875 | 2013-11-14 08:50:33 -0800 | [diff] [blame] | 490 | |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 491 | if (requested > 0) { |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 492 | result = nfs_direct_wait(dreq); |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 493 | if (result > 0) { |
| 494 | requested -= result; |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 495 | iocb->ki_pos += result; |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 496 | } |
| 497 | iov_iter_revert(iter, requested); |
| 498 | } else { |
| 499 | result = requested; |
Christoph Hellwig | 14a3ec79 | 2013-11-14 08:50:31 -0800 | [diff] [blame] | 500 | } |
Christoph Hellwig | d0b9875 | 2013-11-14 08:50:33 -0800 | [diff] [blame] | 501 | |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 502 | out_release: |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 503 | nfs_direct_req_release(dreq); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 504 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return result; |
| 506 | } |
| 507 | |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 508 | static void |
Trond Myklebust | ed5d588 | 2020-03-30 20:57:49 -0400 | [diff] [blame] | 509 | nfs_direct_join_group(struct list_head *list, struct inode *inode) |
| 510 | { |
| 511 | struct nfs_page *req, *next; |
| 512 | |
| 513 | list_for_each_entry(req, list, wb_list) { |
| 514 | if (req->wb_head != req || req->wb_this_page == req) |
| 515 | continue; |
| 516 | for (next = req->wb_this_page; |
| 517 | next != req->wb_head; |
| 518 | next = next->wb_this_page) { |
| 519 | nfs_list_remove_request(next); |
| 520 | nfs_release_request(next); |
| 521 | } |
| 522 | nfs_join_page_group(req, inode); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static void |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 527 | nfs_direct_write_scan_commit_list(struct inode *inode, |
| 528 | struct list_head *list, |
| 529 | struct nfs_commit_info *cinfo) |
| 530 | { |
Trond Myklebust | e824f99 | 2017-08-01 11:53:49 -0400 | [diff] [blame] | 531 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | 9c455a8 | 2020-03-21 11:13:05 -0400 | [diff] [blame] | 532 | pnfs_recover_commit_reqs(list, cinfo); |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 533 | nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); |
Trond Myklebust | e824f99 | 2017-08-01 11:53:49 -0400 | [diff] [blame] | 534 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 537 | static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 539 | struct nfs_pageio_descriptor desc; |
| 540 | struct nfs_page *req, *tmp; |
| 541 | LIST_HEAD(reqs); |
| 542 | struct nfs_commit_info cinfo; |
| 543 | LIST_HEAD(failed); |
| 544 | |
| 545 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 546 | nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Trond Myklebust | ed5d588 | 2020-03-30 20:57:49 -0400 | [diff] [blame] | 548 | nfs_direct_join_group(&reqs, dreq->inode); |
| 549 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 550 | dreq->count = 0; |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 551 | dreq->max_count = 0; |
| 552 | list_for_each_entry(req, &reqs, wb_list) |
| 553 | dreq->max_count += req->wb_bytes; |
Trond Myklebust | a5314a7 | 2016-06-01 21:42:32 -0400 | [diff] [blame] | 554 | nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 555 | get_dreq(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 556 | |
Christoph Hellwig | a20c93e | 2014-04-16 15:07:21 +0200 | [diff] [blame] | 557 | nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 558 | &nfs_direct_write_completion_ops); |
| 559 | desc.pg_dreq = dreq; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 560 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 561 | list_for_each_entry_safe(req, tmp, &reqs, wb_list) { |
Trond Myklebust | 33344e0 | 2019-04-07 13:59:08 -0400 | [diff] [blame] | 562 | /* Bump the transmission count */ |
| 563 | req->wb_nio++; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 564 | if (!nfs_pageio_add_request(&desc, req)) { |
Trond Myklebust | 078b5fd | 2019-02-18 11:35:54 -0500 | [diff] [blame] | 565 | nfs_list_move_request(req, &failed); |
Dave Wysochanski | fe238e6 | 2016-04-01 13:45:09 -0400 | [diff] [blame] | 566 | spin_lock(&cinfo.inode->i_lock); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 567 | dreq->flags = 0; |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 568 | if (desc.pg_error < 0) |
| 569 | dreq->error = desc.pg_error; |
| 570 | else |
| 571 | dreq->error = -EIO; |
Dave Wysochanski | fe238e6 | 2016-04-01 13:45:09 -0400 | [diff] [blame] | 572 | spin_unlock(&cinfo.inode->i_lock); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 573 | } |
Trond Myklebust | 5a695da | 2012-06-19 13:39:14 -0400 | [diff] [blame] | 574 | nfs_release_request(req); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 575 | } |
| 576 | nfs_pageio_complete(&desc); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 577 | |
Trond Myklebust | 4035c24 | 2012-07-08 10:24:10 -0400 | [diff] [blame] | 578 | while (!list_empty(&failed)) { |
| 579 | req = nfs_list_entry(failed.next); |
| 580 | nfs_list_remove_request(req); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 581 | nfs_unlock_and_release_request(req); |
Trond Myklebust | 4035c24 | 2012-07-08 10:24:10 -0400 | [diff] [blame] | 582 | } |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 583 | |
| 584 | if (put_dreq(dreq)) |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 585 | nfs_direct_write_complete(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 588 | static void nfs_direct_commit_complete(struct nfs_commit_data *data) |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 589 | { |
Trond Myklebust | 1f28476 | 2020-03-21 09:27:46 -0400 | [diff] [blame] | 590 | const struct nfs_writeverf *verf = data->res.verf; |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 591 | struct nfs_direct_req *dreq = data->dreq; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 592 | struct nfs_commit_info cinfo; |
| 593 | struct nfs_page *req; |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 594 | int status = data->task.tk_status; |
| 595 | |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 596 | if (status < 0) { |
| 597 | /* Errors in commit are fatal */ |
| 598 | dreq->error = status; |
| 599 | dreq->max_count = 0; |
| 600 | dreq->count = 0; |
| 601 | dreq->flags = NFS_ODIRECT_DONE; |
| 602 | } else if (dreq->flags == NFS_ODIRECT_DONE) |
| 603 | status = dreq->error; |
| 604 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 605 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 606 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 607 | while (!list_empty(&data->pages)) { |
| 608 | req = nfs_list_entry(data->pages.next); |
| 609 | nfs_list_remove_request(req); |
Trond Myklebust | 1f28476 | 2020-03-21 09:27:46 -0400 | [diff] [blame] | 610 | if (status >= 0 && !nfs_write_match_verf(verf, req)) { |
| 611 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
Trond Myklebust | 33344e0 | 2019-04-07 13:59:08 -0400 | [diff] [blame] | 612 | /* |
| 613 | * Despite the reboot, the write was successful, |
| 614 | * so reset wb_nio. |
| 615 | */ |
| 616 | req->wb_nio = 0; |
Weston Andros Adamson | b57ff13 | 2014-09-05 18:20:21 -0400 | [diff] [blame] | 617 | nfs_mark_request_commit(req, NULL, &cinfo, 0); |
Trond Myklebust | 1f28476 | 2020-03-21 09:27:46 -0400 | [diff] [blame] | 618 | } else /* Error or match */ |
Fred Isaman | 906369e | 2012-06-08 16:48:33 -0400 | [diff] [blame] | 619 | nfs_release_request(req); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 620 | nfs_unlock_and_release_request(req); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 624 | nfs_direct_write_complete(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Trond Myklebust | b20135d | 2015-12-31 09:28:06 -0500 | [diff] [blame] | 627 | static void nfs_direct_resched_write(struct nfs_commit_info *cinfo, |
| 628 | struct nfs_page *req) |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 629 | { |
Trond Myklebust | b20135d | 2015-12-31 09:28:06 -0500 | [diff] [blame] | 630 | struct nfs_direct_req *dreq = cinfo->dreq; |
| 631 | |
| 632 | spin_lock(&dreq->lock); |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 633 | if (dreq->flags != NFS_ODIRECT_DONE) |
| 634 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
Trond Myklebust | b20135d | 2015-12-31 09:28:06 -0500 | [diff] [blame] | 635 | spin_unlock(&dreq->lock); |
| 636 | nfs_mark_request_commit(req, NULL, cinfo, 0); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { |
| 640 | .completion = nfs_direct_commit_complete, |
Trond Myklebust | b20135d | 2015-12-31 09:28:06 -0500 | [diff] [blame] | 641 | .resched_write = nfs_direct_resched_write, |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) |
| 645 | { |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 646 | int res; |
| 647 | struct nfs_commit_info cinfo; |
| 648 | LIST_HEAD(mds_list); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 649 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 650 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
| 651 | nfs_scan_commit(dreq->inode, &mds_list, &cinfo); |
| 652 | res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); |
| 653 | if (res < 0) /* res == -ENOMEM */ |
| 654 | nfs_direct_write_reschedule(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 655 | } |
| 656 | |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 657 | static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq) |
| 658 | { |
| 659 | struct nfs_commit_info cinfo; |
| 660 | struct nfs_page *req; |
| 661 | LIST_HEAD(reqs); |
| 662 | |
| 663 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
| 664 | nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); |
| 665 | |
| 666 | while (!list_empty(&reqs)) { |
| 667 | req = nfs_list_entry(reqs.next); |
| 668 | nfs_list_remove_request(req); |
Trond Myklebust | f02cec9 | 2020-03-30 20:13:48 -0400 | [diff] [blame] | 669 | nfs_release_request(req); |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 670 | nfs_unlock_and_release_request(req); |
| 671 | } |
| 672 | } |
| 673 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 674 | static void nfs_direct_write_schedule_work(struct work_struct *work) |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 675 | { |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 676 | struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 677 | int flags = dreq->flags; |
| 678 | |
| 679 | dreq->flags = 0; |
| 680 | switch (flags) { |
| 681 | case NFS_ODIRECT_DO_COMMIT: |
| 682 | nfs_direct_commit_schedule(dreq); |
| 683 | break; |
| 684 | case NFS_ODIRECT_RESCHED_WRITES: |
| 685 | nfs_direct_write_reschedule(dreq); |
| 686 | break; |
| 687 | default: |
Trond Myklebust | fb5f7f2 | 2020-03-21 09:36:13 -0400 | [diff] [blame] | 688 | nfs_direct_write_clear_reqs(dreq); |
Trond Myklebust | f7b5c34 | 2016-06-23 09:29:47 -0400 | [diff] [blame] | 689 | nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); |
| 690 | nfs_direct_complete(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 694 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq) |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 695 | { |
NeilBrown | 46483c2 | 2018-08-08 09:20:02 +1000 | [diff] [blame] | 696 | queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 697 | } |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 698 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 699 | static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) |
| 700 | { |
| 701 | struct nfs_direct_req *dreq = hdr->dreq; |
| 702 | struct nfs_commit_info cinfo; |
Weston Andros Adamson | c65e625 | 2014-06-09 11:48:36 -0400 | [diff] [blame] | 703 | bool request_commit = false; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 704 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); |
| 705 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 706 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
| 707 | |
| 708 | spin_lock(&dreq->lock); |
Trond Myklebust | eb2c50d | 2019-08-12 18:04:36 -0400 | [diff] [blame] | 709 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 710 | spin_unlock(&dreq->lock); |
| 711 | goto out_put; |
| 712 | } |
| 713 | |
Trond Myklebust | 031d73e | 2019-09-30 14:02:56 -0400 | [diff] [blame] | 714 | nfs_direct_count_bytes(dreq, hdr); |
Trond Myklebust | 1f28476 | 2020-03-21 09:27:46 -0400 | [diff] [blame] | 715 | if (hdr->good_bytes != 0 && nfs_write_need_commit(hdr)) { |
| 716 | switch (dreq->flags) { |
| 717 | case 0: |
| 718 | dreq->flags = NFS_ODIRECT_DO_COMMIT; |
| 719 | request_commit = true; |
| 720 | break; |
| 721 | case NFS_ODIRECT_RESCHED_WRITES: |
| 722 | case NFS_ODIRECT_DO_COMMIT: |
| 723 | request_commit = true; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | spin_unlock(&dreq->lock); |
| 727 | |
| 728 | while (!list_empty(&hdr->pages)) { |
Weston Andros Adamson | 2bfc6e5 | 2014-05-15 11:56:45 -0400 | [diff] [blame] | 729 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 730 | req = nfs_list_entry(hdr->pages.next); |
| 731 | nfs_list_remove_request(req); |
Weston Andros Adamson | c65e625 | 2014-06-09 11:48:36 -0400 | [diff] [blame] | 732 | if (request_commit) { |
Trond Myklebust | 0427708 | 2012-05-09 13:54:53 -0400 | [diff] [blame] | 733 | kref_get(&req->wb_kref); |
Chuck Lever | ba838a7 | 2020-05-29 14:14:40 -0400 | [diff] [blame] | 734 | memcpy(&req->wb_verf, &hdr->verf.verifier, |
| 735 | sizeof(req->wb_verf)); |
Weston Andros Adamson | b57ff13 | 2014-09-05 18:20:21 -0400 | [diff] [blame] | 736 | nfs_mark_request_commit(req, hdr->lseg, &cinfo, |
| 737 | hdr->ds_commit_idx); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 738 | } |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 739 | nfs_unlock_and_release_request(req); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | out_put: |
| 743 | if (put_dreq(dreq)) |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 744 | nfs_direct_write_complete(dreq); |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 745 | hdr->release(hdr); |
| 746 | } |
| 747 | |
Trond Myklebust | df3accb | 2019-02-13 10:39:39 -0500 | [diff] [blame] | 748 | static void nfs_write_sync_pgio_error(struct list_head *head, int error) |
Trond Myklebust | 3e9e0ca3 | 2012-04-30 13:40:06 -0400 | [diff] [blame] | 749 | { |
| 750 | struct nfs_page *req; |
| 751 | |
| 752 | while (!list_empty(head)) { |
| 753 | req = nfs_list_entry(head->next); |
| 754 | nfs_list_remove_request(req); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 755 | nfs_unlock_and_release_request(req); |
Trond Myklebust | 3e9e0ca3 | 2012-04-30 13:40:06 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Trond Myklebust | dc602dd | 2015-12-31 11:44:06 -0500 | [diff] [blame] | 759 | static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr) |
| 760 | { |
| 761 | struct nfs_direct_req *dreq = hdr->dreq; |
| 762 | |
| 763 | spin_lock(&dreq->lock); |
| 764 | if (dreq->error == 0) { |
| 765 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
| 766 | /* fake unstable write to let common nfs resend pages */ |
| 767 | hdr->verf.committed = NFS_UNSTABLE; |
Trond Myklebust | 4daaeba | 2020-01-06 15:25:14 -0500 | [diff] [blame] | 768 | hdr->good_bytes = hdr->args.offset + hdr->args.count - |
| 769 | hdr->io_start; |
Trond Myklebust | dc602dd | 2015-12-31 11:44:06 -0500 | [diff] [blame] | 770 | } |
| 771 | spin_unlock(&dreq->lock); |
| 772 | } |
| 773 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 774 | static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { |
Trond Myklebust | 3e9e0ca3 | 2012-04-30 13:40:06 -0400 | [diff] [blame] | 775 | .error_cleanup = nfs_write_sync_pgio_error, |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 776 | .init_hdr = nfs_direct_pgio_init, |
| 777 | .completion = nfs_direct_write_completion, |
Trond Myklebust | dc602dd | 2015-12-31 11:44:06 -0500 | [diff] [blame] | 778 | .reschedule_io = nfs_direct_write_reschedule_io, |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 779 | }; |
| 780 | |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * NB: Return the value of the first error return code. Subsequent |
| 784 | * errors after the first one are ignored. |
| 785 | */ |
| 786 | /* |
| 787 | * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE |
| 788 | * operation. If nfs_writedata_alloc() or get_user_pages() fails, |
| 789 | * bail and stop sending more writes. Write length accounting is |
| 790 | * handled automatically by nfs_direct_write_result(). Otherwise, if |
| 791 | * no requests have been sent, just return an error. |
| 792 | */ |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 793 | static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, |
Al Viro | 619d30b | 2014-03-04 21:53:33 -0500 | [diff] [blame] | 794 | struct iov_iter *iter, |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 795 | loff_t pos) |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 796 | { |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 797 | struct nfs_pageio_descriptor desc; |
Trond Myklebust | 1d59d61 | 2012-05-31 12:22:33 -0400 | [diff] [blame] | 798 | struct inode *inode = dreq->inode; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 799 | ssize_t result = 0; |
| 800 | size_t requested_bytes = 0; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 801 | size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 802 | |
Christoph Hellwig | a20c93e | 2014-04-16 15:07:21 +0200 | [diff] [blame] | 803 | nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 804 | &nfs_direct_write_completion_ops); |
| 805 | desc.pg_dreq = dreq; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 806 | get_dreq(dreq); |
Jens Axboe | fe0f07d | 2015-04-15 17:05:48 -0600 | [diff] [blame] | 807 | inode_dio_begin(inode); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 808 | |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 809 | NFS_I(inode)->write_io += iov_iter_count(iter); |
| 810 | while (iov_iter_count(iter)) { |
| 811 | struct page **pagevec; |
| 812 | size_t bytes; |
| 813 | size_t pgbase; |
| 814 | unsigned npages, i; |
| 815 | |
| 816 | result = iov_iter_get_pages_alloc(iter, &pagevec, |
| 817 | wsize, &pgbase); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 818 | if (result < 0) |
| 819 | break; |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 820 | |
| 821 | bytes = result; |
| 822 | iov_iter_advance(iter, bytes); |
| 823 | npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; |
| 824 | for (i = 0; i < npages; i++) { |
| 825 | struct nfs_page *req; |
| 826 | unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); |
| 827 | |
Trond Myklebust | 28b1d3f | 2019-04-07 13:59:07 -0400 | [diff] [blame] | 828 | req = nfs_create_request(dreq->ctx, pagevec[i], |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 829 | pgbase, req_len); |
| 830 | if (IS_ERR(req)) { |
| 831 | result = PTR_ERR(req); |
| 832 | break; |
| 833 | } |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 834 | |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 835 | if (desc.pg_error < 0) { |
| 836 | nfs_free_request(req); |
| 837 | result = desc.pg_error; |
| 838 | break; |
| 839 | } |
Weston Andros Adamson | 0a00b77 | 2014-09-19 12:48:33 -0400 | [diff] [blame] | 840 | |
Al Viro | 91f79c4 | 2014-03-21 04:58:33 -0400 | [diff] [blame] | 841 | nfs_lock_request(req); |
| 842 | req->wb_index = pos >> PAGE_SHIFT; |
| 843 | req->wb_offset = pos & ~PAGE_MASK; |
| 844 | if (!nfs_pageio_add_request(&desc, req)) { |
| 845 | result = desc.pg_error; |
| 846 | nfs_unlock_and_release_request(req); |
| 847 | break; |
| 848 | } |
| 849 | pgbase = 0; |
| 850 | bytes -= req_len; |
| 851 | requested_bytes += req_len; |
| 852 | pos += req_len; |
| 853 | dreq->bytes_left -= req_len; |
| 854 | } |
| 855 | nfs_direct_release_pages(pagevec, npages); |
| 856 | kvfree(pagevec); |
| 857 | if (result < 0) |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 858 | break; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 859 | } |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 860 | nfs_pageio_complete(&desc); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 861 | |
Chuck Lever | 839f7ad | 2011-01-21 15:54:57 +0000 | [diff] [blame] | 862 | /* |
| 863 | * If no bytes were started, return the error, and let the |
| 864 | * generic layer handle the completion. |
| 865 | */ |
| 866 | if (requested_bytes == 0) { |
Olga Kornievskaia | d03727b | 2020-06-24 13:54:08 -0400 | [diff] [blame] | 867 | inode_dio_end(inode); |
Olga Kornievskaia | 65caafd | 2020-07-15 13:04:15 -0400 | [diff] [blame] | 868 | nfs_direct_req_release(dreq); |
Chuck Lever | 839f7ad | 2011-01-21 15:54:57 +0000 | [diff] [blame] | 869 | return result < 0 ? result : -EIO; |
| 870 | } |
| 871 | |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 872 | if (put_dreq(dreq)) |
Anna Schumaker | 4d3b55d | 2016-11-23 13:49:38 -0500 | [diff] [blame] | 873 | nfs_direct_write_complete(dreq); |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 874 | return requested_bytes; |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | * nfs_file_direct_write - file direct write operation for NFS files |
| 879 | * @iocb: target I/O control block |
Al Viro | 619d30b | 2014-03-04 21:53:33 -0500 | [diff] [blame] | 880 | * @iter: vector of user buffers from which to write data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | * |
| 882 | * We use this function for direct writes instead of calling |
| 883 | * generic_file_aio_write() in order to avoid taking the inode |
| 884 | * semaphore and updating the i_size. The NFS server will set |
| 885 | * the new i_size and this client must read the updated size |
| 886 | * back into its cache. We let the server do generic write |
| 887 | * parameter checking and report problems. |
| 888 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | * We eliminate local atime updates, see direct read above. |
| 890 | * |
| 891 | * We avoid unnecessary page cache invalidations for normal cached |
| 892 | * readers of this file. |
| 893 | * |
| 894 | * Note that O_APPEND is not supported for NFS direct writes, as there |
| 895 | * is no atomic O_APPEND write facility in the NFS protocol. |
| 896 | */ |
Al Viro | 65a4a1c | 2015-04-09 14:11:08 -0400 | [diff] [blame] | 897 | ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
Colin Ian King | 9a74a2b | 2020-07-24 11:44:41 +0100 | [diff] [blame] | 899 | ssize_t result, requested; |
Trond Myklebust | 89698b2 | 2016-06-23 10:35:48 -0400 | [diff] [blame] | 900 | size_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | struct address_space *mapping = file->f_mapping; |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 903 | struct inode *inode = mapping->host; |
| 904 | struct nfs_direct_req *dreq; |
| 905 | struct nfs_lock_context *l_ctx; |
Al Viro | 65a4a1c | 2015-04-09 14:11:08 -0400 | [diff] [blame] | 906 | loff_t pos, end; |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 907 | |
Al Viro | 6de1472 | 2013-09-16 10:53:17 -0400 | [diff] [blame] | 908 | dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 909 | file, iov_iter_count(iter), (long long) iocb->ki_pos); |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 910 | |
Trond Myklebust | 89698b2 | 2016-06-23 10:35:48 -0400 | [diff] [blame] | 911 | result = generic_write_checks(iocb, iter); |
| 912 | if (result <= 0) |
| 913 | return result; |
| 914 | count = result; |
| 915 | nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 916 | |
| 917 | pos = iocb->ki_pos; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 918 | end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 919 | |
Trond Myklebust | 89698b2 | 2016-06-23 10:35:48 -0400 | [diff] [blame] | 920 | task_io_account_write(count); |
Konstantin Khlebnikov | 7ec10f2 | 2011-02-22 00:28:34 +0300 | [diff] [blame] | 921 | |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 922 | result = -ENOMEM; |
| 923 | dreq = nfs_direct_req_alloc(); |
| 924 | if (!dreq) |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 925 | goto out; |
Chuck Lever | 9eafa8c | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 926 | |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 927 | dreq->inode = inode; |
Trond Myklebust | 89698b2 | 2016-06-23 10:35:48 -0400 | [diff] [blame] | 928 | dreq->bytes_left = dreq->max_count = count; |
Peng Tao | 5fadeb4 | 2015-01-19 12:41:16 +0800 | [diff] [blame] | 929 | dreq->io_start = pos; |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 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); |
Misono Tomohiro | 8605cf0 | 2019-08-28 17:01:22 +0900 | [diff] [blame] | 934 | nfs_direct_req_release(dreq); |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 935 | goto out_release; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 936 | } |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 937 | dreq->l_ctx = l_ctx; |
| 938 | if (!is_sync_kiocb(iocb)) |
| 939 | dreq->iocb = iocb; |
Trond Myklebust | 9c455a8 | 2020-03-21 11:13:05 -0400 | [diff] [blame] | 940 | pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode); |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 941 | |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 942 | nfs_start_io_direct(inode); |
| 943 | |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 944 | requested = nfs_direct_write_schedule_iovec(dreq, iter, pos); |
Christoph Hellwig | a9ab5e8 | 2013-11-14 08:50:34 -0800 | [diff] [blame] | 945 | |
| 946 | if (mapping->nrpages) { |
| 947 | invalidate_inode_pages2_range(mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 948 | pos >> PAGE_SHIFT, end); |
Christoph Hellwig | a9ab5e8 | 2013-11-14 08:50:34 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 951 | nfs_end_io_direct(inode); |
Christoph Hellwig | a9ab5e8 | 2013-11-14 08:50:34 -0800 | [diff] [blame] | 952 | |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 953 | if (requested > 0) { |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 954 | result = nfs_direct_wait(dreq); |
| 955 | if (result > 0) { |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 956 | requested -= result; |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 957 | iocb->ki_pos = pos + result; |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 958 | /* XXX: should check the generic_write_sync retval */ |
| 959 | generic_write_sync(iocb, result); |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 960 | } |
Al Viro | 85128b2 | 2017-04-13 09:31:51 -0400 | [diff] [blame] | 961 | iov_iter_revert(iter, requested); |
| 962 | } else { |
| 963 | result = requested; |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 964 | } |
| 965 | out_release: |
| 966 | nfs_direct_req_release(dreq); |
Trond Myklebust | a5864c9 | 2016-06-03 17:07:19 -0400 | [diff] [blame] | 967 | out: |
Christoph Hellwig | 22cd1bf | 2013-11-14 08:50:32 -0800 | [diff] [blame] | 968 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 971 | /** |
| 972 | * nfs_init_directcache - create a slab cache for nfs_direct_req structures |
| 973 | * |
| 974 | */ |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 975 | int __init nfs_init_directcache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
| 977 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", |
| 978 | sizeof(struct nfs_direct_req), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 979 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 980 | SLAB_MEM_SPREAD), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 981 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if (nfs_direct_cachep == NULL) |
| 983 | return -ENOMEM; |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 988 | /** |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 989 | * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 990 | * |
| 991 | */ |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 992 | void nfs_destroy_directcache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 994 | kmem_cache_destroy(nfs_direct_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |