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