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