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> |
| 47 | |
| 48 | #include <linux/nfs_fs.h> |
| 49 | #include <linux/nfs_page.h> |
| 50 | #include <linux/sunrpc/clnt.h> |
| 51 | |
| 52 | #include <asm/system.h> |
| 53 | #include <asm/uaccess.h> |
| 54 | #include <asm/atomic.h> |
| 55 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 56 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 57 | #include "iostat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define NFSDBG_FACILITY NFSDBG_VFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 61 | static struct kmem_cache *nfs_direct_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * This represents a set of asynchronous requests that we're waiting on |
| 65 | */ |
| 66 | struct nfs_direct_req { |
| 67 | struct kref kref; /* release manager */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 68 | |
| 69 | /* I/O parameters */ |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 70 | struct nfs_open_context *ctx; /* file open context info */ |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 71 | struct kiocb * iocb; /* controlling i/o request */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 72 | struct inode * inode; /* target file of i/o */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 73 | |
| 74 | /* completion state */ |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 75 | atomic_t io_count; /* i/os we're waiting for */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 76 | spinlock_t lock; /* protect completion state */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 77 | ssize_t count, /* bytes actually processed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | error; /* any reported error */ |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 79 | struct completion completion; /* wait for i/o completion */ |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 80 | |
| 81 | /* commit state */ |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 82 | struct list_head rewrite_list; /* saved nfs_write_data structs */ |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 83 | struct nfs_write_data * commit_data; /* special write_data for commits */ |
| 84 | int flags; |
| 85 | #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ |
| 86 | #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ |
| 87 | struct nfs_writeverf verf; /* unstable write verifier */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 90 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 91 | static const struct rpc_call_ops nfs_write_direct_ops; |
| 92 | |
| 93 | static inline void get_dreq(struct nfs_direct_req *dreq) |
| 94 | { |
| 95 | atomic_inc(&dreq->io_count); |
| 96 | } |
| 97 | |
| 98 | static inline int put_dreq(struct nfs_direct_req *dreq) |
| 99 | { |
| 100 | return atomic_dec_and_test(&dreq->io_count); |
| 101 | } |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /** |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 104 | * nfs_direct_IO - NFS address space operation for direct I/O |
| 105 | * @rw: direction (read or write) |
| 106 | * @iocb: target I/O control block |
| 107 | * @iov: array of vectors that define I/O buffer |
| 108 | * @pos: offset in file to begin the operation |
| 109 | * @nr_segs: size of iovec array |
| 110 | * |
| 111 | * The presence of this routine in the address space ops vector means |
| 112 | * the NFS client supports direct I/O. However, we shunt off direct |
| 113 | * read and write requests before the VFS gets them, so this method |
| 114 | * should never be called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 116 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) |
| 117 | { |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 118 | dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 119 | iocb->ki_filp->f_path.dentry->d_name.name, |
Trond Myklebust | e99170f | 2006-04-18 13:21:42 -0400 | [diff] [blame] | 120 | (long long) pos, nr_segs); |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 121 | |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
Trond Myklebust | d4a8f36 | 2007-05-22 10:22:27 -0400 | [diff] [blame] | 125 | static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count) |
Trond Myklebust | 6b45d85 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 126 | { |
Trond Myklebust | d4a8f36 | 2007-05-22 10:22:27 -0400 | [diff] [blame] | 127 | unsigned int npages; |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 128 | unsigned int i; |
Trond Myklebust | d4a8f36 | 2007-05-22 10:22:27 -0400 | [diff] [blame] | 129 | |
| 130 | if (count == 0) |
| 131 | return; |
| 132 | pages += (pgbase >> PAGE_SHIFT); |
| 133 | npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Trond Myklebust | 6b45d85 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 134 | for (i = 0; i < npages; i++) { |
| 135 | struct page *page = pages[i]; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 136 | if (!PageCompound(page)) |
Trond Myklebust | d4a8f36 | 2007-05-22 10:22:27 -0400 | [diff] [blame] | 137 | set_page_dirty(page); |
Trond Myklebust | 6b45d85 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 138 | } |
Chuck Lever | 9c93ab7 | 2006-06-20 12:56:31 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 141 | static void nfs_direct_release_pages(struct page **pages, unsigned int npages) |
Chuck Lever | 9c93ab7 | 2006-06-20 12:56:31 -0400 | [diff] [blame] | 142 | { |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 143 | unsigned int i; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 144 | for (i = 0; i < npages; i++) |
| 145 | page_cache_release(pages[i]); |
Trond Myklebust | 6b45d85 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 148 | static inline struct nfs_direct_req *nfs_direct_req_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | struct nfs_direct_req *dreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 152 | dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (!dreq) |
| 154 | return NULL; |
| 155 | |
| 156 | kref_init(&dreq->kref); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 157 | kref_get(&dreq->kref); |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 158 | init_completion(&dreq->completion); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 159 | INIT_LIST_HEAD(&dreq->rewrite_list); |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 160 | dreq->iocb = NULL; |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 161 | dreq->ctx = NULL; |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 162 | spin_lock_init(&dreq->lock); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 163 | atomic_set(&dreq->io_count, 0); |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 164 | dreq->count = 0; |
| 165 | dreq->error = 0; |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 166 | dreq->flags = 0; |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 167 | |
| 168 | return dreq; |
| 169 | } |
| 170 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 171 | static void nfs_direct_req_free(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | 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] | 174 | |
| 175 | if (dreq->ctx != NULL) |
| 176 | put_nfs_open_context(dreq->ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | kmem_cache_free(nfs_direct_cachep, dreq); |
| 178 | } |
| 179 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 180 | static void nfs_direct_req_release(struct nfs_direct_req *dreq) |
| 181 | { |
| 182 | kref_put(&dreq->kref, nfs_direct_req_free); |
| 183 | } |
| 184 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 185 | /* |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 186 | * Collects and returns the final error value/byte-count. |
| 187 | */ |
| 188 | static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) |
| 189 | { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 190 | ssize_t result = -EIOCBQUEUED; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 191 | |
| 192 | /* Async requests don't wait here */ |
| 193 | if (dreq->iocb) |
| 194 | goto out; |
| 195 | |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 196 | result = wait_for_completion_killable(&dreq->completion); |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 197 | |
| 198 | if (!result) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 199 | result = dreq->error; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 200 | if (!result) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 201 | result = dreq->count; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 202 | |
| 203 | out: |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 204 | return (ssize_t) result; |
| 205 | } |
| 206 | |
| 207 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 208 | * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust |
| 209 | * the iocb is still valid here if this is a synchronous request. |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 210 | */ |
| 211 | static void nfs_direct_complete(struct nfs_direct_req *dreq) |
| 212 | { |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 213 | if (dreq->iocb) { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 214 | long res = (long) dreq->error; |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 215 | if (!res) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 216 | res = (long) dreq->count; |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 217 | aio_complete(dreq->iocb, res, 0); |
Trond Myklebust | d72b7a6 | 2006-03-20 13:44:43 -0500 | [diff] [blame] | 218 | } |
| 219 | complete_all(&dreq->completion); |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 220 | |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 221 | nfs_direct_req_release(dreq); |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 225 | * We must hold a reference to all the pages in this direct read request |
| 226 | * until the RPCs complete. This could be long *after* we are woken up in |
| 227 | * nfs_direct_wait (for instance, if someone hits ^C on a slow server). |
Chuck Lever | 06cf6f2 | 2006-06-20 12:56:49 -0400 | [diff] [blame] | 228 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 229 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 231 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame^] | 233 | nfs_readpage_result(task, data); |
| 234 | } |
| 235 | |
| 236 | static void nfs_direct_read_release(void *calldata) |
| 237 | { |
| 238 | |
| 239 | struct nfs_read_data *data = calldata; |
| 240 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 241 | int status = data->task.tk_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 243 | spin_lock(&dreq->lock); |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame^] | 244 | if (unlikely(status < 0)) { |
| 245 | dreq->error = status; |
Trond Myklebust | d4a8f36 | 2007-05-22 10:22:27 -0400 | [diff] [blame] | 246 | spin_unlock(&dreq->lock); |
| 247 | } else { |
| 248 | dreq->count += data->res.count; |
| 249 | spin_unlock(&dreq->lock); |
| 250 | nfs_direct_dirty_pages(data->pagevec, |
| 251 | data->args.pgbase, |
| 252 | data->res.count); |
| 253 | } |
| 254 | nfs_direct_release_pages(data->pagevec, data->npages); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 255 | |
| 256 | if (put_dreq(dreq)) |
| 257 | nfs_direct_complete(dreq); |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame^] | 258 | nfs_readdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 261 | static const struct rpc_call_ops nfs_read_direct_ops = { |
| 262 | .rpc_call_done = nfs_direct_read_result, |
Trond Myklebust | fdd1e74 | 2008-04-15 16:33:58 -0400 | [diff] [blame^] | 263 | .rpc_release = nfs_direct_read_release, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 264 | }; |
| 265 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 266 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 267 | * For each rsize'd chunk of the user's buffer, dispatch an NFS READ |
| 268 | * operation. If nfs_readdata_alloc() or get_user_pages() fails, |
| 269 | * bail and stop sending more reads. Read length accounting is |
| 270 | * handled automatically by nfs_direct_read_result(). Otherwise, if |
| 271 | * no requests have been sent, just return an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | */ |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 273 | static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq, |
| 274 | const struct iovec *iov, |
| 275 | loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 277 | struct nfs_open_context *ctx = dreq->ctx; |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 278 | struct inode *inode = ctx->path.dentry->d_inode; |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 279 | unsigned long user_addr = (unsigned long)iov->iov_base; |
| 280 | size_t count = iov->iov_len; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 281 | size_t rsize = NFS_SERVER(inode)->rsize; |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 282 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 283 | struct rpc_message msg = { |
| 284 | .rpc_cred = ctx->cred, |
| 285 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 286 | struct rpc_task_setup task_setup_data = { |
| 287 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 288 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 289 | .callback_ops = &nfs_read_direct_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 290 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 291 | .flags = RPC_TASK_ASYNC, |
| 292 | }; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 293 | unsigned int pgbase; |
| 294 | int result; |
| 295 | ssize_t started = 0; |
Chuck Lever | 82b145c | 2006-06-20 12:57:03 -0400 | [diff] [blame] | 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | do { |
Chuck Lever | 82b145c | 2006-06-20 12:57:03 -0400 | [diff] [blame] | 298 | struct nfs_read_data *data; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 299 | size_t bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 301 | pgbase = user_addr & ~PAGE_MASK; |
| 302 | bytes = min(rsize,count); |
| 303 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 304 | result = -ENOMEM; |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 305 | data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes)); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 306 | if (unlikely(!data)) |
| 307 | break; |
| 308 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 309 | down_read(¤t->mm->mmap_sem); |
| 310 | result = get_user_pages(current, current->mm, user_addr, |
| 311 | data->npages, 1, 0, data->pagevec, NULL); |
| 312 | up_read(¤t->mm->mmap_sem); |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 313 | if (result < 0) { |
| 314 | nfs_readdata_release(data); |
| 315 | break; |
| 316 | } |
| 317 | if ((unsigned)result < data->npages) { |
Trond Myklebust | d9df8d6 | 2007-05-22 10:22:20 -0400 | [diff] [blame] | 318 | bytes = result * PAGE_SIZE; |
| 319 | if (bytes <= pgbase) { |
| 320 | nfs_direct_release_pages(data->pagevec, result); |
| 321 | nfs_readdata_release(data); |
| 322 | break; |
| 323 | } |
| 324 | bytes -= pgbase; |
| 325 | data->npages = result; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 326 | } |
Chuck Lever | 06cf6f2 | 2006-06-20 12:56:49 -0400 | [diff] [blame] | 327 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 328 | get_dreq(dreq); |
| 329 | |
| 330 | data->req = (struct nfs_page *) dreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | data->inode = inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 332 | data->cred = msg.rpc_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | data->args.fh = NFS_FH(inode); |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 334 | data->args.context = get_nfs_open_context(ctx); |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 335 | data->args.offset = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | data->args.pgbase = pgbase; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 337 | data->args.pages = data->pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | data->args.count = bytes; |
| 339 | data->res.fattr = &data->fattr; |
| 340 | data->res.eof = 0; |
| 341 | data->res.count = bytes; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 342 | msg.rpc_argp = &data->args; |
| 343 | msg.rpc_resp = &data->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 345 | task_setup_data.task = &data->task; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 346 | task_setup_data.callback_data = data; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 347 | NFS_PROTO(inode)->read_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 349 | task = rpc_run_task(&task_setup_data); |
| 350 | if (!IS_ERR(task)) |
| 351 | rpc_put_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 353 | dprintk("NFS: %5u initiated direct read call " |
| 354 | "(req %s/%Ld, %zu bytes @ offset %Lu)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | data->task.tk_pid, |
| 356 | inode->i_sb->s_id, |
| 357 | (long long)NFS_FILEID(inode), |
| 358 | bytes, |
| 359 | (unsigned long long)data->args.offset); |
| 360 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 361 | started += bytes; |
| 362 | user_addr += bytes; |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 363 | pos += bytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 364 | /* FIXME: Remove this unnecessary math from final patch */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | pgbase += bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | pgbase &= ~PAGE_MASK; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 367 | BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | count -= bytes; |
| 370 | } while (count != 0); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 371 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 372 | if (started) |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 373 | return started; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 374 | return result < 0 ? (ssize_t) result : -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 377 | static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, |
| 378 | const struct iovec *iov, |
| 379 | unsigned long nr_segs, |
| 380 | loff_t pos) |
| 381 | { |
| 382 | ssize_t result = -EINVAL; |
| 383 | size_t requested_bytes = 0; |
| 384 | unsigned long seg; |
| 385 | |
| 386 | get_dreq(dreq); |
| 387 | |
| 388 | for (seg = 0; seg < nr_segs; seg++) { |
| 389 | const struct iovec *vec = &iov[seg]; |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 390 | result = nfs_direct_read_schedule_segment(dreq, vec, pos); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 391 | if (result < 0) |
| 392 | break; |
| 393 | requested_bytes += result; |
| 394 | if ((size_t)result < vec->iov_len) |
| 395 | break; |
| 396 | pos += vec->iov_len; |
| 397 | } |
| 398 | |
| 399 | if (put_dreq(dreq)) |
| 400 | nfs_direct_complete(dreq); |
| 401 | |
| 402 | if (requested_bytes != 0) |
| 403 | return 0; |
| 404 | |
| 405 | if (result < 0) |
| 406 | return result; |
| 407 | return -EIO; |
| 408 | } |
| 409 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 410 | static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov, |
| 411 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 413 | ssize_t result = 0; |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 414 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | struct nfs_direct_req *dreq; |
| 416 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 417 | dreq = nfs_direct_req_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | if (!dreq) |
| 419 | return -ENOMEM; |
| 420 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 421 | dreq->inode = inode; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 422 | dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); |
Chuck Lever | 487b837 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 423 | if (!is_sync_kiocb(iocb)) |
| 424 | dreq->iocb = iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 426 | result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 427 | if (!result) |
| 428 | result = nfs_direct_wait(dreq); |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 429 | nfs_direct_req_release(dreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | return result; |
| 432 | } |
| 433 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 434 | static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 436 | while (!list_empty(&dreq->rewrite_list)) { |
| 437 | struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 438 | list_del(&data->pages); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 439 | nfs_direct_release_pages(data->pagevec, data->npages); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 440 | nfs_writedata_release(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 444 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 445 | static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 447 | struct inode *inode = dreq->inode; |
| 448 | struct list_head *p; |
| 449 | struct nfs_write_data *data; |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 450 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 451 | struct rpc_message msg = { |
| 452 | .rpc_cred = dreq->ctx->cred, |
| 453 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 454 | struct rpc_task_setup task_setup_data = { |
| 455 | .rpc_client = NFS_CLIENT(inode), |
| 456 | .callback_ops = &nfs_write_direct_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 457 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 458 | .flags = RPC_TASK_ASYNC, |
| 459 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 461 | dreq->count = 0; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 462 | get_dreq(dreq); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 463 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 464 | list_for_each(p, &dreq->rewrite_list) { |
| 465 | data = list_entry(p, struct nfs_write_data, pages); |
| 466 | |
| 467 | get_dreq(dreq); |
| 468 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 469 | /* Use stable writes */ |
| 470 | data->args.stable = NFS_FILE_SYNC; |
| 471 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 472 | /* |
| 473 | * Reset data->res. |
| 474 | */ |
| 475 | nfs_fattr_init(&data->fattr); |
| 476 | data->res.count = data->args.count; |
| 477 | memset(&data->verf, 0, sizeof(data->verf)); |
| 478 | |
| 479 | /* |
| 480 | * Reuse data->task; data->args should not have changed |
| 481 | * since the original request was sent. |
| 482 | */ |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 483 | task_setup_data.task = &data->task; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 484 | task_setup_data.callback_data = data; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 485 | msg.rpc_argp = &data->args; |
| 486 | msg.rpc_resp = &data->res; |
| 487 | NFS_PROTO(inode)->write_setup(data, &msg); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 488 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 489 | /* |
| 490 | * We're called via an RPC callback, so BKL is already held. |
| 491 | */ |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 492 | task = rpc_run_task(&task_setup_data); |
| 493 | if (!IS_ERR(task)) |
| 494 | rpc_put_task(task); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 495 | |
| 496 | dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 497 | data->task.tk_pid, |
| 498 | inode->i_sb->s_id, |
| 499 | (long long)NFS_FILEID(inode), |
| 500 | data->args.count, |
| 501 | (unsigned long long)data->args.offset); |
| 502 | } |
| 503 | |
| 504 | if (put_dreq(dreq)) |
| 505 | nfs_direct_write_complete(dreq, inode); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) |
| 509 | { |
| 510 | struct nfs_write_data *data = calldata; |
| 511 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 512 | |
| 513 | /* Call the NFS version-specific code */ |
| 514 | if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) |
| 515 | return; |
| 516 | if (unlikely(task->tk_status < 0)) { |
Trond Myklebust | 60fa3f7 | 2007-04-14 19:11:52 -0400 | [diff] [blame] | 517 | dprintk("NFS: %5u commit failed with error %d.\n", |
| 518 | task->tk_pid, task->tk_status); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 519 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
Trond Myklebust | 60fa3f7 | 2007-04-14 19:11:52 -0400 | [diff] [blame] | 520 | } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 521 | dprintk("NFS: %5u commit verify failed\n", task->tk_pid); |
| 522 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
| 523 | } |
| 524 | |
| 525 | dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); |
| 526 | nfs_direct_write_complete(dreq, data->inode); |
| 527 | } |
| 528 | |
| 529 | static const struct rpc_call_ops nfs_commit_direct_ops = { |
| 530 | .rpc_call_done = nfs_direct_commit_result, |
| 531 | .rpc_release = nfs_commit_release, |
| 532 | }; |
| 533 | |
| 534 | static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) |
| 535 | { |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 536 | struct nfs_write_data *data = dreq->commit_data; |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 537 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 538 | struct rpc_message msg = { |
| 539 | .rpc_argp = &data->args, |
| 540 | .rpc_resp = &data->res, |
| 541 | .rpc_cred = dreq->ctx->cred, |
| 542 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 543 | struct rpc_task_setup task_setup_data = { |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 544 | .task = &data->task, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 545 | .rpc_client = NFS_CLIENT(dreq->inode), |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 546 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 547 | .callback_ops = &nfs_commit_direct_ops, |
| 548 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 549 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 550 | .flags = RPC_TASK_ASYNC, |
| 551 | }; |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 552 | |
| 553 | data->inode = dreq->inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 554 | data->cred = msg.rpc_cred; |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 555 | |
| 556 | data->args.fh = NFS_FH(data->inode); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 557 | data->args.offset = 0; |
| 558 | data->args.count = 0; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 559 | data->args.context = get_nfs_open_context(dreq->ctx); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 560 | data->res.count = 0; |
| 561 | data->res.fattr = &data->fattr; |
| 562 | data->res.verf = &data->verf; |
| 563 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 564 | NFS_PROTO(data->inode)->commit_setup(data, &msg); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 565 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 566 | /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ |
| 567 | dreq->commit_data = NULL; |
| 568 | |
Trond Myklebust | e99170f | 2006-04-18 13:21:42 -0400 | [diff] [blame] | 569 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 570 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 571 | task = rpc_run_task(&task_setup_data); |
| 572 | if (!IS_ERR(task)) |
| 573 | rpc_put_task(task); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) |
| 577 | { |
| 578 | int flags = dreq->flags; |
| 579 | |
| 580 | dreq->flags = 0; |
| 581 | switch (flags) { |
| 582 | case NFS_ODIRECT_DO_COMMIT: |
| 583 | nfs_direct_commit_schedule(dreq); |
| 584 | break; |
| 585 | case NFS_ODIRECT_RESCHED_WRITES: |
| 586 | nfs_direct_write_reschedule(dreq); |
| 587 | break; |
| 588 | default: |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 589 | if (dreq->commit_data != NULL) |
| 590 | nfs_commit_free(dreq->commit_data); |
| 591 | nfs_direct_free_writedata(dreq); |
Trond Myklebust | cd9ae2b | 2006-10-19 23:28:40 -0700 | [diff] [blame] | 592 | nfs_zap_mapping(inode, inode->i_mapping); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 593 | nfs_direct_complete(dreq); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) |
| 598 | { |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 599 | dreq->commit_data = nfs_commit_alloc(); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 600 | if (dreq->commit_data != NULL) |
| 601 | dreq->commit_data->req = (struct nfs_page *) dreq; |
| 602 | } |
| 603 | #else |
| 604 | static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) |
| 605 | { |
| 606 | dreq->commit_data = NULL; |
| 607 | } |
| 608 | |
| 609 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) |
| 610 | { |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 611 | nfs_direct_free_writedata(dreq); |
Trond Myklebust | cd9ae2b | 2006-10-19 23:28:40 -0700 | [diff] [blame] | 612 | nfs_zap_mapping(inode, inode->i_mapping); |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 613 | nfs_direct_complete(dreq); |
| 614 | } |
| 615 | #endif |
| 616 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 617 | static void nfs_direct_write_result(struct rpc_task *task, void *calldata) |
| 618 | { |
| 619 | struct nfs_write_data *data = calldata; |
| 620 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 621 | int status = task->tk_status; |
| 622 | |
| 623 | if (nfs_writeback_done(task, data) != 0) |
| 624 | return; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 625 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 626 | spin_lock(&dreq->lock); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 627 | |
Trond Myklebust | 60fa3f7 | 2007-04-14 19:11:52 -0400 | [diff] [blame] | 628 | if (unlikely(status < 0)) { |
Neil Brown | 432409e | 2007-10-23 17:09:13 -0400 | [diff] [blame] | 629 | /* An error has occurred, so we should not commit */ |
Trond Myklebust | 60fa3f7 | 2007-04-14 19:11:52 -0400 | [diff] [blame] | 630 | dreq->flags = 0; |
| 631 | dreq->error = status; |
Trond Myklebust | eda3cef | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 632 | } |
Neil Brown | 432409e | 2007-10-23 17:09:13 -0400 | [diff] [blame] | 633 | if (unlikely(dreq->error != 0)) |
| 634 | goto out_unlock; |
Trond Myklebust | eda3cef | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 635 | |
| 636 | dreq->count += data->res.count; |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 637 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 638 | if (data->res.verf->committed != NFS_FILE_SYNC) { |
| 639 | switch (dreq->flags) { |
| 640 | case 0: |
| 641 | memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); |
| 642 | dreq->flags = NFS_ODIRECT_DO_COMMIT; |
| 643 | break; |
| 644 | case NFS_ODIRECT_DO_COMMIT: |
| 645 | if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { |
| 646 | dprintk("NFS: %5u write verify failed\n", task->tk_pid); |
| 647 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
| 648 | } |
| 649 | } |
| 650 | } |
Trond Myklebust | eda3cef | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 651 | out_unlock: |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 652 | spin_unlock(&dreq->lock); |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * NB: Return the value of the first error return code. Subsequent |
| 657 | * errors after the first one are ignored. |
| 658 | */ |
| 659 | static void nfs_direct_write_release(void *calldata) |
| 660 | { |
| 661 | struct nfs_write_data *data = calldata; |
| 662 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 663 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 664 | if (put_dreq(dreq)) |
| 665 | nfs_direct_write_complete(dreq, data->inode); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static const struct rpc_call_ops nfs_write_direct_ops = { |
| 669 | .rpc_call_done = nfs_direct_write_result, |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 670 | .rpc_release = nfs_direct_write_release, |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | /* |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 674 | * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE |
| 675 | * operation. If nfs_writedata_alloc() or get_user_pages() fails, |
| 676 | * bail and stop sending more writes. Write length accounting is |
| 677 | * handled automatically by nfs_direct_write_result(). Otherwise, if |
| 678 | * no requests have been sent, just return an error. |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 679 | */ |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 680 | static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq, |
| 681 | const struct iovec *iov, |
| 682 | loff_t pos, int sync) |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 683 | { |
Trond Myklebust | a8881f5 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 684 | struct nfs_open_context *ctx = dreq->ctx; |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 685 | struct inode *inode = ctx->path.dentry->d_inode; |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 686 | unsigned long user_addr = (unsigned long)iov->iov_base; |
| 687 | size_t count = iov->iov_len; |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 688 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 689 | struct rpc_message msg = { |
| 690 | .rpc_cred = ctx->cred, |
| 691 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 692 | struct rpc_task_setup task_setup_data = { |
| 693 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 694 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 695 | .callback_ops = &nfs_write_direct_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 696 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 697 | .flags = RPC_TASK_ASYNC, |
| 698 | }; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 699 | size_t wsize = NFS_SERVER(inode)->wsize; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 700 | unsigned int pgbase; |
| 701 | int result; |
| 702 | ssize_t started = 0; |
Chuck Lever | 82b145c | 2006-06-20 12:57:03 -0400 | [diff] [blame] | 703 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 704 | do { |
Chuck Lever | 82b145c | 2006-06-20 12:57:03 -0400 | [diff] [blame] | 705 | struct nfs_write_data *data; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 706 | size_t bytes; |
| 707 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 708 | pgbase = user_addr & ~PAGE_MASK; |
| 709 | bytes = min(wsize,count); |
| 710 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 711 | result = -ENOMEM; |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 712 | data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes)); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 713 | if (unlikely(!data)) |
| 714 | break; |
| 715 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 716 | down_read(¤t->mm->mmap_sem); |
| 717 | result = get_user_pages(current, current->mm, user_addr, |
| 718 | data->npages, 0, 0, data->pagevec, NULL); |
| 719 | up_read(¤t->mm->mmap_sem); |
Chuck Lever | 749e146 | 2007-05-19 17:22:46 -0400 | [diff] [blame] | 720 | if (result < 0) { |
| 721 | nfs_writedata_release(data); |
| 722 | break; |
| 723 | } |
| 724 | if ((unsigned)result < data->npages) { |
Trond Myklebust | d9df8d6 | 2007-05-22 10:22:20 -0400 | [diff] [blame] | 725 | bytes = result * PAGE_SIZE; |
| 726 | if (bytes <= pgbase) { |
| 727 | nfs_direct_release_pages(data->pagevec, result); |
| 728 | nfs_writedata_release(data); |
| 729 | break; |
| 730 | } |
| 731 | bytes -= pgbase; |
| 732 | data->npages = result; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | get_dreq(dreq); |
| 736 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 737 | list_move_tail(&data->pages, &dreq->rewrite_list); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 738 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 739 | data->req = (struct nfs_page *) dreq; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 740 | data->inode = inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 741 | data->cred = msg.rpc_cred; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 742 | data->args.fh = NFS_FH(inode); |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 743 | data->args.context = get_nfs_open_context(ctx); |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 744 | data->args.offset = pos; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 745 | data->args.pgbase = pgbase; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 746 | data->args.pages = data->pagevec; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 747 | data->args.count = bytes; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 748 | data->args.stable = sync; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 749 | data->res.fattr = &data->fattr; |
| 750 | data->res.count = bytes; |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 751 | data->res.verf = &data->verf; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 752 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 753 | task_setup_data.task = &data->task; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 754 | task_setup_data.callback_data = data; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 755 | msg.rpc_argp = &data->args; |
| 756 | msg.rpc_resp = &data->res; |
| 757 | NFS_PROTO(inode)->write_setup(data, &msg); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 758 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 759 | task = rpc_run_task(&task_setup_data); |
| 760 | if (!IS_ERR(task)) |
| 761 | rpc_put_task(task); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 762 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 763 | dprintk("NFS: %5u initiated direct write call " |
| 764 | "(req %s/%Ld, %zu bytes @ offset %Lu)\n", |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 765 | data->task.tk_pid, |
| 766 | inode->i_sb->s_id, |
| 767 | (long long)NFS_FILEID(inode), |
| 768 | bytes, |
| 769 | (unsigned long long)data->args.offset); |
| 770 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 771 | started += bytes; |
| 772 | user_addr += bytes; |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 773 | pos += bytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 774 | |
| 775 | /* FIXME: Remove this useless math from the final patch */ |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 776 | pgbase += bytes; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 777 | pgbase &= ~PAGE_MASK; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 778 | BUG_ON(pgbase != (user_addr & ~PAGE_MASK)); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 779 | |
| 780 | count -= bytes; |
| 781 | } while (count != 0); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 782 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 783 | if (started) |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 784 | return started; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 785 | return result < 0 ? (ssize_t) result : -EFAULT; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 786 | } |
| 787 | |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 788 | static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, |
| 789 | const struct iovec *iov, |
| 790 | unsigned long nr_segs, |
| 791 | loff_t pos, int sync) |
| 792 | { |
| 793 | ssize_t result = 0; |
| 794 | size_t requested_bytes = 0; |
| 795 | unsigned long seg; |
| 796 | |
| 797 | get_dreq(dreq); |
| 798 | |
| 799 | for (seg = 0; seg < nr_segs; seg++) { |
| 800 | const struct iovec *vec = &iov[seg]; |
Chuck Lever | 02fe494 | 2007-11-12 12:17:03 -0500 | [diff] [blame] | 801 | result = nfs_direct_write_schedule_segment(dreq, vec, |
| 802 | pos, sync); |
Chuck Lever | 19f7378 | 2007-11-12 12:16:47 -0500 | [diff] [blame] | 803 | if (result < 0) |
| 804 | break; |
| 805 | requested_bytes += result; |
| 806 | if ((size_t)result < vec->iov_len) |
| 807 | break; |
| 808 | pos += vec->iov_len; |
| 809 | } |
| 810 | |
| 811 | if (put_dreq(dreq)) |
| 812 | nfs_direct_write_complete(dreq, dreq->inode); |
| 813 | |
| 814 | if (requested_bytes != 0) |
| 815 | return 0; |
| 816 | |
| 817 | if (result < 0) |
| 818 | return result; |
| 819 | return -EIO; |
| 820 | } |
| 821 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 822 | static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov, |
| 823 | unsigned long nr_segs, loff_t pos, |
| 824 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 826 | ssize_t result = 0; |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 827 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 828 | struct nfs_direct_req *dreq; |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 829 | size_t wsize = NFS_SERVER(inode)->wsize; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 830 | int sync = NFS_UNSTABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 832 | dreq = nfs_direct_req_alloc(); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 833 | if (!dreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return -ENOMEM; |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 835 | nfs_alloc_commit_data(dreq); |
| 836 | |
Trond Myklebust | fad6149 | 2006-03-20 13:44:36 -0500 | [diff] [blame] | 837 | if (dreq->commit_data == NULL || count < wsize) |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 838 | sync = NFS_FILE_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 840 | dreq->inode = inode; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 841 | dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 842 | if (!is_sync_kiocb(iocb)) |
| 843 | dreq->iocb = iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 845 | result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync); |
Trond Myklebust | 607f31e | 2006-06-28 16:52:45 -0400 | [diff] [blame] | 846 | if (!result) |
| 847 | result = nfs_direct_wait(dreq); |
Trond Myklebust | b4946ff | 2007-05-30 12:58:00 -0400 | [diff] [blame] | 848 | nfs_direct_req_release(dreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return result; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * nfs_file_direct_read - file direct read operation for NFS files |
| 855 | * @iocb: target I/O control block |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 856 | * @iov: vector of user buffers into which to read data |
| 857 | * @nr_segs: size of iov vector |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 858 | * @pos: byte offset in file where reading starts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | * |
| 860 | * We use this function for direct reads instead of calling |
| 861 | * generic_file_aio_read() in order to avoid gfar's check to see if |
| 862 | * the request starts before the end of the file. For that check |
| 863 | * to work, we must generate a GETATTR before each direct read, and |
| 864 | * even then there is a window between the GETATTR and the subsequent |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 865 | * READ where the file size could change. Our preference is simply |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | * to do all reads the application wants, and the server will take |
| 867 | * care of managing the end of file boundary. |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 868 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | * This function also eliminates unnecessarily updating the file's |
| 870 | * atime locally, as the NFS server sets the file's atime, and this |
| 871 | * client must read the updated atime from the server back into its |
| 872 | * cache. |
| 873 | */ |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 874 | ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov, |
| 875 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
| 877 | ssize_t retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | struct address_space *mapping = file->f_mapping; |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 880 | size_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 882 | count = iov_length(iov, nr_segs); |
| 883 | nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); |
| 884 | |
| 885 | dprintk("nfs: direct read(%s/%s, %zd@%Ld)\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 886 | file->f_path.dentry->d_parent->d_name.name, |
| 887 | file->f_path.dentry->d_name.name, |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 888 | count, (long long) pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | retval = 0; |
| 891 | if (!count) |
| 892 | goto out; |
| 893 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 894 | retval = nfs_sync_mapping(mapping); |
| 895 | if (retval) |
| 896 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 898 | retval = nfs_direct_read(iocb, iov, nr_segs, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | if (retval > 0) |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 900 | iocb->ki_pos = pos + retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | out: |
| 903 | return retval; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * nfs_file_direct_write - file direct write operation for NFS files |
| 908 | * @iocb: target I/O control block |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 909 | * @iov: vector of user buffers from which to write data |
| 910 | * @nr_segs: size of iov vector |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 911 | * @pos: byte offset in file where writing starts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | * |
| 913 | * We use this function for direct writes instead of calling |
| 914 | * generic_file_aio_write() in order to avoid taking the inode |
| 915 | * semaphore and updating the i_size. The NFS server will set |
| 916 | * the new i_size and this client must read the updated size |
| 917 | * back into its cache. We let the server do generic write |
| 918 | * parameter checking and report problems. |
| 919 | * |
| 920 | * We also avoid an unnecessary invocation of generic_osync_inode(), |
| 921 | * as it is fairly meaningless to sync the metadata of an NFS file. |
| 922 | * |
| 923 | * We eliminate local atime updates, see direct read above. |
| 924 | * |
| 925 | * We avoid unnecessary page cache invalidations for normal cached |
| 926 | * readers of this file. |
| 927 | * |
| 928 | * Note that O_APPEND is not supported for NFS direct writes, as there |
| 929 | * is no atomic O_APPEND write facility in the NFS protocol. |
| 930 | */ |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 931 | ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov, |
| 932 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | { |
Chuck Lever | 070ea60 | 2007-05-19 17:22:52 -0400 | [diff] [blame] | 934 | ssize_t retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | struct address_space *mapping = file->f_mapping; |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 937 | size_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 939 | count = iov_length(iov, nr_segs); |
| 940 | nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); |
| 941 | |
| 942 | dfprintk(VFS, "nfs: direct write(%s/%s, %zd@%Ld)\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 943 | file->f_path.dentry->d_parent->d_name.name, |
| 944 | file->f_path.dentry->d_name.name, |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 945 | count, (long long) pos); |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 946 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 947 | retval = generic_write_checks(file, &pos, &count, 0); |
| 948 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 950 | |
| 951 | retval = -EINVAL; |
| 952 | if ((ssize_t) count < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | retval = 0; |
| 955 | if (!count) |
| 956 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 957 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 958 | retval = nfs_sync_mapping(mapping); |
| 959 | if (retval) |
| 960 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Chuck Lever | c216fd7 | 2007-11-12 12:16:52 -0500 | [diff] [blame] | 962 | retval = nfs_direct_write(iocb, iov, nr_segs, pos, count); |
Chuck Lever | 9eafa8c | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (retval > 0) |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 965 | iocb->ki_pos = pos + retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
| 967 | out: |
| 968 | return retval; |
| 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 | } |