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