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