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 |
| 10 | * (multiple copies of the same instance running on separate hosts) |
| 11 | * implement their own cache coherency protocol that subsumes file |
| 12 | * system cache protocols. Applications that process datasets |
| 13 | * considerably larger than the client's memory do not always benefit |
| 14 | * from a local cache. A streaming video server, for instance, has no |
| 15 | * need to cache the contents of a file. |
| 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 |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | #include <linux/config.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/sched.h> |
| 43 | #include <linux/kernel.h> |
| 44 | #include <linux/smp_lock.h> |
| 45 | #include <linux/file.h> |
| 46 | #include <linux/pagemap.h> |
| 47 | #include <linux/kref.h> |
| 48 | |
| 49 | #include <linux/nfs_fs.h> |
| 50 | #include <linux/nfs_page.h> |
| 51 | #include <linux/sunrpc/clnt.h> |
| 52 | |
| 53 | #include <asm/system.h> |
| 54 | #include <asm/uaccess.h> |
| 55 | #include <asm/atomic.h> |
| 56 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 57 | #include "iostat.h" |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define NFSDBG_FACILITY NFSDBG_VFS |
| 60 | #define MAX_DIRECTIO_SIZE (4096UL << PAGE_SHIFT) |
| 61 | |
Trond Myklebust | 143f412e | 2006-03-13 21:20:46 -0800 | [diff] [blame] | 62 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static kmem_cache_t *nfs_direct_cachep; |
| 64 | |
| 65 | /* |
| 66 | * This represents a set of asynchronous requests that we're waiting on |
| 67 | */ |
| 68 | struct nfs_direct_req { |
| 69 | struct kref kref; /* release manager */ |
| 70 | struct list_head list; /* nfs_read_data structs */ |
| 71 | wait_queue_head_t wait; /* wait for i/o completion */ |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 72 | struct inode * inode; /* target file of I/O */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct page ** pages; /* pages in our buffer */ |
| 74 | unsigned int npages; /* count of pages */ |
| 75 | atomic_t complete, /* i/os we're waiting for */ |
| 76 | count, /* bytes actually processed */ |
| 77 | error; /* any reported error */ |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | /** |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 82 | * nfs_direct_IO - NFS address space operation for direct I/O |
| 83 | * @rw: direction (read or write) |
| 84 | * @iocb: target I/O control block |
| 85 | * @iov: array of vectors that define I/O buffer |
| 86 | * @pos: offset in file to begin the operation |
| 87 | * @nr_segs: size of iovec array |
| 88 | * |
| 89 | * The presence of this routine in the address space ops vector means |
| 90 | * the NFS client supports direct I/O. However, we shunt off direct |
| 91 | * read and write requests before the VFS gets them, so this method |
| 92 | * should never be called. |
| 93 | */ |
| 94 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) |
| 95 | { |
| 96 | struct dentry *dentry = iocb->ki_filp->f_dentry; |
| 97 | |
| 98 | dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", |
| 99 | dentry->d_name.name, (long long) pos, nr_segs); |
| 100 | |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 104 | static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | int result = -ENOMEM; |
| 107 | unsigned long page_count; |
| 108 | size_t array_size; |
| 109 | |
| 110 | /* set an arbitrary limit to prevent type overflow */ |
| 111 | /* XXX: this can probably be as large as INT_MAX */ |
| 112 | if (size > MAX_DIRECTIO_SIZE) { |
| 113 | *pages = NULL; |
| 114 | return -EFBIG; |
| 115 | } |
| 116 | |
| 117 | page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 118 | page_count -= user_addr >> PAGE_SHIFT; |
| 119 | |
| 120 | array_size = (page_count * sizeof(struct page *)); |
| 121 | *pages = kmalloc(array_size, GFP_KERNEL); |
| 122 | if (*pages) { |
| 123 | down_read(¤t->mm->mmap_sem); |
| 124 | result = get_user_pages(current, current->mm, user_addr, |
| 125 | page_count, (rw == READ), 0, |
| 126 | *pages, NULL); |
| 127 | up_read(¤t->mm->mmap_sem); |
Trond Myklebust | 143f412e | 2006-03-13 21:20:46 -0800 | [diff] [blame] | 128 | /* |
| 129 | * If we got fewer pages than expected from get_user_pages(), |
| 130 | * the user buffer runs off the end of a mapping; return EFAULT. |
| 131 | */ |
| 132 | if (result >= 0 && result < page_count) { |
| 133 | nfs_free_user_pages(*pages, result, 0); |
| 134 | *pages = NULL; |
| 135 | result = -EFAULT; |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 141 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | int i; |
| 144 | for (i = 0; i < npages; i++) { |
Trond Myklebust | 566dd60 | 2006-01-03 09:55:35 +0100 | [diff] [blame] | 145 | struct page *page = pages[i]; |
| 146 | if (do_dirty && !PageCompound(page)) |
| 147 | set_page_dirty_lock(page); |
| 148 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | kfree(pages); |
| 151 | } |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | static void nfs_direct_req_release(struct kref *kref) |
| 154 | { |
| 155 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); |
| 156 | kmem_cache_free(nfs_direct_cachep, dreq); |
| 157 | } |
| 158 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 159 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | * Note we also set the number of requests we have in the dreq when we are |
| 161 | * done. This prevents races with I/O completion so we will always wait |
| 162 | * until all requests have been dispatched and completed. |
| 163 | */ |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame^] | 164 | 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] | 165 | { |
| 166 | struct list_head *list; |
| 167 | struct nfs_direct_req *dreq; |
| 168 | unsigned int reads = 0; |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 169 | unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); |
| 172 | if (!dreq) |
| 173 | return NULL; |
| 174 | |
| 175 | kref_init(&dreq->kref); |
| 176 | init_waitqueue_head(&dreq->wait); |
| 177 | INIT_LIST_HEAD(&dreq->list); |
| 178 | atomic_set(&dreq->count, 0); |
| 179 | atomic_set(&dreq->error, 0); |
| 180 | |
| 181 | list = &dreq->list; |
| 182 | for(;;) { |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 183 | struct nfs_read_data *data = nfs_readdata_alloc(rpages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | if (unlikely(!data)) { |
| 186 | while (!list_empty(list)) { |
| 187 | data = list_entry(list->next, |
| 188 | struct nfs_read_data, pages); |
| 189 | list_del(&data->pages); |
| 190 | nfs_readdata_free(data); |
| 191 | } |
| 192 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | INIT_LIST_HEAD(&data->pages); |
| 197 | list_add(&data->pages, list); |
| 198 | |
| 199 | data->req = (struct nfs_page *) dreq; |
| 200 | reads++; |
| 201 | if (nbytes <= rsize) |
| 202 | break; |
| 203 | nbytes -= rsize; |
| 204 | } |
| 205 | kref_get(&dreq->kref); |
| 206 | atomic_set(&dreq->complete, reads); |
| 207 | return dreq; |
| 208 | } |
| 209 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | * We must hold a reference to all the pages in this direct read request |
| 212 | * until the RPCs complete. This could be long *after* we are woken up in |
| 213 | * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server). |
| 214 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 215 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 217 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 219 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 220 | if (nfs_readpage_result(task, data) != 0) |
| 221 | return; |
| 222 | if (likely(task->tk_status >= 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | atomic_add(data->res.count, &dreq->count); |
| 224 | else |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 225 | atomic_set(&dreq->error, task->tk_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | if (unlikely(atomic_dec_and_test(&dreq->complete))) { |
| 228 | nfs_free_user_pages(dreq->pages, dreq->npages, 1); |
| 229 | wake_up(&dreq->wait); |
| 230 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 231 | } |
| 232 | } |
| 233 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 234 | static const struct rpc_call_ops nfs_read_direct_ops = { |
| 235 | .rpc_call_done = nfs_direct_read_result, |
| 236 | .rpc_release = nfs_readdata_release, |
| 237 | }; |
| 238 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 239 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * For each nfs_read_data struct that was allocated on the list, dispatch |
| 241 | * an NFS READ operation |
| 242 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 243 | static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
| 245 | struct list_head *list = &dreq->list; |
| 246 | struct page **pages = dreq->pages; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame^] | 247 | size_t rsize = NFS_SERVER(inode)->rsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | unsigned int curpage, pgbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | curpage = 0; |
| 251 | pgbase = user_addr & ~PAGE_MASK; |
| 252 | do { |
| 253 | struct nfs_read_data *data; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame^] | 254 | size_t bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | bytes = rsize; |
| 257 | if (count < rsize) |
| 258 | bytes = count; |
| 259 | |
| 260 | data = list_entry(list->next, struct nfs_read_data, pages); |
| 261 | list_del_init(&data->pages); |
| 262 | |
| 263 | data->inode = inode; |
| 264 | data->cred = ctx->cred; |
| 265 | data->args.fh = NFS_FH(inode); |
| 266 | data->args.context = ctx; |
| 267 | data->args.offset = file_offset; |
| 268 | data->args.pgbase = pgbase; |
| 269 | data->args.pages = &pages[curpage]; |
| 270 | data->args.count = bytes; |
| 271 | data->res.fattr = &data->fattr; |
| 272 | data->res.eof = 0; |
| 273 | data->res.count = bytes; |
| 274 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 275 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
| 276 | &nfs_read_direct_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | NFS_PROTO(inode)->read_setup(data); |
| 278 | |
| 279 | data->task.tk_cookie = (unsigned long) inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | lock_kernel(); |
| 282 | rpc_execute(&data->task); |
| 283 | unlock_kernel(); |
| 284 | |
| 285 | dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 286 | data->task.tk_pid, |
| 287 | inode->i_sb->s_id, |
| 288 | (long long)NFS_FILEID(inode), |
| 289 | bytes, |
| 290 | (unsigned long long)data->args.offset); |
| 291 | |
| 292 | file_offset += bytes; |
| 293 | pgbase += bytes; |
| 294 | curpage += pgbase >> PAGE_SHIFT; |
| 295 | pgbase &= ~PAGE_MASK; |
| 296 | |
| 297 | count -= bytes; |
| 298 | } while (count != 0); |
| 299 | } |
| 300 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 301 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * Collects and returns the final error value/byte-count. |
| 303 | */ |
| 304 | static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr) |
| 305 | { |
| 306 | int result = 0; |
| 307 | |
| 308 | if (intr) { |
| 309 | result = wait_event_interruptible(dreq->wait, |
| 310 | (atomic_read(&dreq->complete) == 0)); |
| 311 | } else { |
| 312 | wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0)); |
| 313 | } |
| 314 | |
| 315 | if (!result) |
| 316 | result = atomic_read(&dreq->error); |
| 317 | if (!result) |
| 318 | result = atomic_read(&dreq->count); |
| 319 | |
| 320 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 321 | return (ssize_t) result; |
| 322 | } |
| 323 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 324 | static ssize_t nfs_direct_read_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, unsigned int nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | ssize_t result; |
| 327 | sigset_t oldset; |
| 328 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
| 329 | struct nfs_direct_req *dreq; |
| 330 | |
| 331 | dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); |
| 332 | if (!dreq) |
| 333 | return -ENOMEM; |
| 334 | |
| 335 | dreq->pages = pages; |
| 336 | dreq->npages = nr_pages; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 337 | dreq->inode = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 339 | nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | rpc_clnt_sigmask(clnt, &oldset); |
| 341 | nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count, |
| 342 | file_offset); |
| 343 | result = nfs_direct_read_wait(dreq, clnt->cl_intr); |
| 344 | rpc_clnt_sigunmask(clnt, &oldset); |
| 345 | |
| 346 | return result; |
| 347 | } |
| 348 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 349 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | * We've already pushed out any non-direct writes so that this read |
| 351 | * will see them when we read from the server. |
| 352 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 353 | static ssize_t nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | ssize_t tot_bytes = 0; |
| 356 | unsigned long seg = 0; |
| 357 | |
| 358 | while ((seg < nr_segs) && (tot_bytes >= 0)) { |
| 359 | ssize_t result; |
| 360 | int page_count; |
| 361 | struct page **pages; |
| 362 | const struct iovec *vec = &iov[seg++]; |
| 363 | unsigned long user_addr = (unsigned long) vec->iov_base; |
| 364 | size_t size = vec->iov_len; |
| 365 | |
| 366 | page_count = nfs_get_user_pages(READ, user_addr, size, &pages); |
| 367 | if (page_count < 0) { |
| 368 | nfs_free_user_pages(pages, 0, 0); |
| 369 | if (tot_bytes > 0) |
| 370 | break; |
| 371 | return page_count; |
| 372 | } |
| 373 | |
| 374 | result = nfs_direct_read_seg(inode, ctx, user_addr, size, |
| 375 | file_offset, pages, page_count); |
| 376 | |
| 377 | if (result <= 0) { |
| 378 | if (tot_bytes > 0) |
| 379 | break; |
| 380 | return result; |
| 381 | } |
| 382 | tot_bytes += result; |
| 383 | file_offset += result; |
| 384 | if (result < size) |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | return tot_bytes; |
| 389 | } |
| 390 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 391 | static ssize_t nfs_direct_write_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, int nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
| 393 | const unsigned int wsize = NFS_SERVER(inode)->wsize; |
| 394 | size_t request; |
| 395 | int curpage, need_commit; |
| 396 | ssize_t result, tot_bytes; |
| 397 | struct nfs_writeverf first_verf; |
| 398 | struct nfs_write_data *wdata; |
| 399 | |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 400 | wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (!wdata) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | wdata->inode = inode; |
| 405 | wdata->cred = ctx->cred; |
| 406 | wdata->args.fh = NFS_FH(inode); |
| 407 | wdata->args.context = ctx; |
| 408 | wdata->args.stable = NFS_UNSTABLE; |
| 409 | if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize) |
| 410 | wdata->args.stable = NFS_FILE_SYNC; |
| 411 | wdata->res.fattr = &wdata->fattr; |
| 412 | wdata->res.verf = &wdata->verf; |
| 413 | |
| 414 | nfs_begin_data_update(inode); |
| 415 | retry: |
| 416 | need_commit = 0; |
| 417 | tot_bytes = 0; |
| 418 | curpage = 0; |
| 419 | request = count; |
| 420 | wdata->args.pgbase = user_addr & ~PAGE_MASK; |
| 421 | wdata->args.offset = file_offset; |
| 422 | do { |
| 423 | wdata->args.count = request; |
| 424 | if (wdata->args.count > wsize) |
| 425 | wdata->args.count = wsize; |
| 426 | wdata->args.pages = &pages[curpage]; |
| 427 | |
| 428 | dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n", |
| 429 | wdata->args.count, (long long) wdata->args.offset, |
| 430 | user_addr + tot_bytes, wdata->args.pgbase, curpage); |
| 431 | |
| 432 | lock_kernel(); |
| 433 | result = NFS_PROTO(inode)->write(wdata); |
| 434 | unlock_kernel(); |
| 435 | |
| 436 | if (result <= 0) { |
| 437 | if (tot_bytes > 0) |
| 438 | break; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | if (tot_bytes == 0) |
| 443 | memcpy(&first_verf.verifier, &wdata->verf.verifier, |
| 444 | sizeof(first_verf.verifier)); |
| 445 | if (wdata->verf.committed != NFS_FILE_SYNC) { |
| 446 | need_commit = 1; |
| 447 | if (memcmp(&first_verf.verifier, &wdata->verf.verifier, |
Dirk Mueller | 1935245 | 2006-02-01 12:19:47 -0500 | [diff] [blame] | 448 | sizeof(first_verf.verifier))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | goto sync_retry; |
| 450 | } |
| 451 | |
| 452 | tot_bytes += result; |
| 453 | |
| 454 | /* in case of a short write: stop now, let the app recover */ |
| 455 | if (result < wdata->args.count) |
| 456 | break; |
| 457 | |
| 458 | wdata->args.offset += result; |
| 459 | wdata->args.pgbase += result; |
| 460 | curpage += wdata->args.pgbase >> PAGE_SHIFT; |
| 461 | wdata->args.pgbase &= ~PAGE_MASK; |
| 462 | request -= result; |
| 463 | } while (request != 0); |
| 464 | |
| 465 | /* |
| 466 | * Commit data written so far, even in the event of an error |
| 467 | */ |
| 468 | if (need_commit) { |
| 469 | wdata->args.count = tot_bytes; |
| 470 | wdata->args.offset = file_offset; |
| 471 | |
| 472 | lock_kernel(); |
| 473 | result = NFS_PROTO(inode)->commit(wdata); |
| 474 | unlock_kernel(); |
| 475 | |
| 476 | if (result < 0 || memcmp(&first_verf.verifier, |
| 477 | &wdata->verf.verifier, |
| 478 | sizeof(first_verf.verifier)) != 0) |
| 479 | goto sync_retry; |
| 480 | } |
| 481 | result = tot_bytes; |
| 482 | |
| 483 | out: |
Trond Myklebust | 951a143 | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 484 | nfs_end_data_update(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | nfs_writedata_free(wdata); |
| 486 | return result; |
| 487 | |
| 488 | sync_retry: |
| 489 | wdata->args.stable = NFS_FILE_SYNC; |
| 490 | goto retry; |
| 491 | } |
| 492 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 493 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | * Upon return, generic_file_direct_IO invalidates any cached pages |
| 495 | * that non-direct readers might access, so they will pick up these |
| 496 | * writes immediately. |
| 497 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 498 | static ssize_t nfs_direct_write(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | ssize_t tot_bytes = 0; |
| 501 | unsigned long seg = 0; |
| 502 | |
| 503 | while ((seg < nr_segs) && (tot_bytes >= 0)) { |
| 504 | ssize_t result; |
| 505 | int page_count; |
| 506 | struct page **pages; |
| 507 | const struct iovec *vec = &iov[seg++]; |
| 508 | unsigned long user_addr = (unsigned long) vec->iov_base; |
| 509 | size_t size = vec->iov_len; |
| 510 | |
| 511 | page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages); |
| 512 | if (page_count < 0) { |
| 513 | nfs_free_user_pages(pages, 0, 0); |
| 514 | if (tot_bytes > 0) |
| 515 | break; |
| 516 | return page_count; |
| 517 | } |
| 518 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 519 | nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | result = nfs_direct_write_seg(inode, ctx, user_addr, size, |
| 521 | file_offset, pages, page_count); |
| 522 | nfs_free_user_pages(pages, page_count, 0); |
| 523 | |
| 524 | if (result <= 0) { |
| 525 | if (tot_bytes > 0) |
| 526 | break; |
| 527 | return result; |
| 528 | } |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 529 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | tot_bytes += result; |
| 531 | file_offset += result; |
| 532 | if (result < size) |
| 533 | break; |
| 534 | } |
| 535 | return tot_bytes; |
| 536 | } |
| 537 | |
| 538 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | * nfs_file_direct_read - file direct read operation for NFS files |
| 540 | * @iocb: target I/O control block |
| 541 | * @buf: user's buffer into which to read data |
| 542 | * count: number of bytes to read |
| 543 | * pos: byte offset in file where reading starts |
| 544 | * |
| 545 | * We use this function for direct reads instead of calling |
| 546 | * generic_file_aio_read() in order to avoid gfar's check to see if |
| 547 | * the request starts before the end of the file. For that check |
| 548 | * to work, we must generate a GETATTR before each direct read, and |
| 549 | * even then there is a window between the GETATTR and the subsequent |
| 550 | * READ where the file size could change. So our preference is simply |
| 551 | * to do all reads the application wants, and the server will take |
| 552 | * care of managing the end of file boundary. |
| 553 | * |
| 554 | * This function also eliminates unnecessarily updating the file's |
| 555 | * atime locally, as the NFS server sets the file's atime, and this |
| 556 | * client must read the updated atime from the server back into its |
| 557 | * cache. |
| 558 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 559 | 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] | 560 | { |
| 561 | ssize_t retval = -EINVAL; |
| 562 | loff_t *ppos = &iocb->ki_pos; |
| 563 | struct file *file = iocb->ki_filp; |
| 564 | struct nfs_open_context *ctx = |
| 565 | (struct nfs_open_context *) file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | struct address_space *mapping = file->f_mapping; |
| 567 | struct inode *inode = mapping->host; |
| 568 | struct iovec iov = { |
| 569 | .iov_base = buf, |
| 570 | .iov_len = count, |
| 571 | }; |
| 572 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 573 | dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 574 | file->f_dentry->d_parent->d_name.name, |
| 575 | file->f_dentry->d_name.name, |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 576 | (unsigned long) count, (long long) pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | if (!is_sync_kiocb(iocb)) |
| 579 | goto out; |
| 580 | if (count < 0) |
| 581 | goto out; |
| 582 | retval = -EFAULT; |
| 583 | if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len)) |
| 584 | goto out; |
| 585 | retval = 0; |
| 586 | if (!count) |
| 587 | goto out; |
| 588 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 589 | retval = nfs_sync_mapping(mapping); |
| 590 | if (retval) |
| 591 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | retval = nfs_direct_read(inode, ctx, &iov, pos, 1); |
| 594 | if (retval > 0) |
| 595 | *ppos = pos + retval; |
| 596 | |
| 597 | out: |
| 598 | return retval; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * nfs_file_direct_write - file direct write operation for NFS files |
| 603 | * @iocb: target I/O control block |
| 604 | * @buf: user's buffer from which to write data |
| 605 | * count: number of bytes to write |
| 606 | * pos: byte offset in file where writing starts |
| 607 | * |
| 608 | * We use this function for direct writes instead of calling |
| 609 | * generic_file_aio_write() in order to avoid taking the inode |
| 610 | * semaphore and updating the i_size. The NFS server will set |
| 611 | * the new i_size and this client must read the updated size |
| 612 | * back into its cache. We let the server do generic write |
| 613 | * parameter checking and report problems. |
| 614 | * |
| 615 | * We also avoid an unnecessary invocation of generic_osync_inode(), |
| 616 | * as it is fairly meaningless to sync the metadata of an NFS file. |
| 617 | * |
| 618 | * We eliminate local atime updates, see direct read above. |
| 619 | * |
| 620 | * We avoid unnecessary page cache invalidations for normal cached |
| 621 | * readers of this file. |
| 622 | * |
| 623 | * Note that O_APPEND is not supported for NFS direct writes, as there |
| 624 | * is no atomic O_APPEND write facility in the NFS protocol. |
| 625 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 626 | 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] | 627 | { |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 628 | ssize_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | struct file *file = iocb->ki_filp; |
| 630 | struct nfs_open_context *ctx = |
| 631 | (struct nfs_open_context *) file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | struct address_space *mapping = file->f_mapping; |
| 633 | struct inode *inode = mapping->host; |
| 634 | struct iovec iov = { |
| 635 | .iov_base = (char __user *)buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 638 | dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 639 | file->f_dentry->d_parent->d_name.name, |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 640 | file->f_dentry->d_name.name, |
| 641 | (unsigned long) count, (long long) pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 643 | retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (!is_sync_kiocb(iocb)) |
| 645 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 646 | |
| 647 | retval = generic_write_checks(file, &pos, &count, 0); |
| 648 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 650 | |
| 651 | retval = -EINVAL; |
| 652 | if ((ssize_t) count < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | retval = 0; |
| 655 | if (!count) |
| 656 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 657 | iov.iov_len = count, |
| 658 | |
| 659 | retval = -EFAULT; |
| 660 | if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len)) |
| 661 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 663 | retval = nfs_sync_mapping(mapping); |
| 664 | if (retval) |
| 665 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | retval = nfs_direct_write(inode, ctx, &iov, pos, 1); |
| 668 | if (mapping->nrpages) |
| 669 | invalidate_inode_pages2(mapping); |
| 670 | if (retval > 0) |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 671 | iocb->ki_pos = pos + retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | out: |
| 674 | return retval; |
| 675 | } |
| 676 | |
| 677 | int nfs_init_directcache(void) |
| 678 | { |
| 679 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", |
| 680 | sizeof(struct nfs_direct_req), |
| 681 | 0, SLAB_RECLAIM_ACCOUNT, |
| 682 | NULL, NULL); |
| 683 | if (nfs_direct_cachep == NULL) |
| 684 | return -ENOMEM; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | void nfs_destroy_directcache(void) |
| 690 | { |
| 691 | if (kmem_cache_destroy(nfs_direct_cachep)) |
| 692 | printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); |
| 693 | } |