2 * linux/fs/nfs/direct.c
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
6 * High-performance uncached I/O for the Linux NFS client
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.
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
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.
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
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
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>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/sunrpc/clnt.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <asm/atomic.h>
59 #define NFSDBG_FACILITY NFSDBG_VFS
60 #define MAX_DIRECTIO_SIZE (4096UL << PAGE_SHIFT)
62 static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty);
63 static kmem_cache_t *nfs_direct_cachep;
66 * This represents a set of asynchronous requests that we're waiting on
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 */
72 struct inode * inode; /* target file of I/O */
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 */
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
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.
94 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
96 struct dentry *dentry = iocb->ki_filp->f_dentry;
98 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
99 dentry->d_name.name, (long long) pos, nr_segs);
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
112 nfs_get_user_pages(int rw, unsigned long user_addr, size_t size,
113 struct page ***pages)
115 int result = -ENOMEM;
116 unsigned long page_count;
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) {
126 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
127 page_count -= user_addr >> PAGE_SHIFT;
129 array_size = (page_count * sizeof(struct page *));
130 *pages = kmalloc(array_size, GFP_KERNEL);
132 down_read(¤t->mm->mmap_sem);
133 result = get_user_pages(current, current->mm, user_addr,
134 page_count, (rw == READ), 0,
136 up_read(¤t->mm->mmap_sem);
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.
141 if (result >= 0 && result < page_count) {
142 nfs_free_user_pages(*pages, result, 0);
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
157 nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
160 for (i = 0; i < npages; i++) {
161 struct page *page = pages[i];
162 if (do_dirty && !PageCompound(page))
163 set_page_dirty_lock(page);
164 page_cache_release(page);
170 * nfs_direct_req_release - release nfs_direct_req structure for direct read
171 * @kref: kref object embedded in an nfs_direct_req structure
174 static void nfs_direct_req_release(struct kref *kref)
176 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
177 kmem_cache_free(nfs_direct_cachep, dreq);
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
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.
189 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize)
191 struct list_head *list;
192 struct nfs_direct_req *dreq;
193 unsigned int reads = 0;
194 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
196 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
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);
208 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
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);
217 kref_put(&dreq->kref, nfs_direct_req_release);
221 INIT_LIST_HEAD(&data->pages);
222 list_add(&data->pages, list);
224 data->req = (struct nfs_page *) dreq;
230 kref_get(&dreq->kref);
231 atomic_set(&dreq->complete, reads);
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
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).
244 static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
246 struct nfs_read_data *data = calldata;
247 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
249 if (nfs_readpage_result(task, data) != 0)
251 if (likely(task->tk_status >= 0))
252 atomic_add(data->res.count, &dreq->count);
254 atomic_set(&dreq->error, task->tk_status);
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);
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,
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
277 * For each nfs_read_data struct that was allocated on the list, dispatch
278 * an NFS READ operation
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)
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;
290 pgbase = user_addr & ~PAGE_MASK;
292 struct nfs_read_data *data;
299 data = list_entry(list->next, struct nfs_read_data, pages);
300 list_del_init(&data->pages);
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;
312 data->res.count = bytes;
314 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
315 &nfs_read_direct_ops, data);
316 NFS_PROTO(inode)->read_setup(data);
318 data->task.tk_cookie = (unsigned long) inode;
321 rpc_execute(&data->task);
324 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
327 (long long)NFS_FILEID(inode),
329 (unsigned long long)data->args.offset);
331 file_offset += bytes;
333 curpage += pgbase >> PAGE_SHIFT;
334 pgbase &= ~PAGE_MASK;
337 } while (count != 0);
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
345 * Collects and returns the final error value/byte-count.
347 static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
352 result = wait_event_interruptible(dreq->wait,
353 (atomic_read(&dreq->complete) == 0));
355 wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
359 result = atomic_read(&dreq->error);
361 result = atomic_read(&dreq->count);
363 kref_put(&dreq->kref, nfs_direct_req_release);
364 return (ssize_t) result;
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
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)
386 struct rpc_clnt *clnt = NFS_CLIENT(inode);
387 struct nfs_direct_req *dreq;
389 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
394 dreq->npages = nr_pages;
397 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
398 rpc_clnt_sigmask(clnt, &oldset);
399 nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
401 result = nfs_direct_read_wait(dreq, clnt->cl_intr);
402 rpc_clnt_sigunmask(clnt, &oldset);
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
416 * We've already pushed out any non-direct writes so that this read
417 * will see them when we read from the server.
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)
424 ssize_t tot_bytes = 0;
425 unsigned long seg = 0;
427 while ((seg < nr_segs) && (tot_bytes >= 0)) {
431 const struct iovec *vec = &iov[seg++];
432 unsigned long user_addr = (unsigned long) vec->iov_base;
433 size_t size = vec->iov_len;
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);
443 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
444 file_offset, pages, page_count);
452 file_offset += result;
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
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,
476 const unsigned int wsize = NFS_SERVER(inode)->wsize;
478 int curpage, need_commit;
479 ssize_t result, tot_bytes;
480 struct nfs_writeverf first_verf;
481 struct nfs_write_data *wdata;
483 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
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;
497 nfs_begin_data_update(inode);
503 wdata->args.pgbase = user_addr & ~PAGE_MASK;
504 wdata->args.offset = file_offset;
506 wdata->args.count = request;
507 if (wdata->args.count > wsize)
508 wdata->args.count = wsize;
509 wdata->args.pages = &pages[curpage];
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);
516 result = NFS_PROTO(inode)->write(wdata);
526 memcpy(&first_verf.verifier, &wdata->verf.verifier,
527 sizeof(first_verf.verifier));
528 if (wdata->verf.committed != NFS_FILE_SYNC) {
530 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
531 sizeof(first_verf.verifier)))
537 /* in case of a short write: stop now, let the app recover */
538 if (result < wdata->args.count)
541 wdata->args.offset += result;
542 wdata->args.pgbase += result;
543 curpage += wdata->args.pgbase >> PAGE_SHIFT;
544 wdata->args.pgbase &= ~PAGE_MASK;
546 } while (request != 0);
549 * Commit data written so far, even in the event of an error
552 wdata->args.count = tot_bytes;
553 wdata->args.offset = file_offset;
556 result = NFS_PROTO(inode)->commit(wdata);
559 if (result < 0 || memcmp(&first_verf.verifier,
560 &wdata->verf.verifier,
561 sizeof(first_verf.verifier)) != 0)
567 nfs_end_data_update(inode);
568 nfs_writedata_free(wdata);
572 wdata->args.stable = NFS_FILE_SYNC;
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
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.
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)
593 ssize_t tot_bytes = 0;
594 unsigned long seg = 0;
596 while ((seg < nr_segs) && (tot_bytes >= 0)) {
600 const struct iovec *vec = &iov[seg++];
601 unsigned long user_addr = (unsigned long) vec->iov_base;
602 size_t size = vec->iov_len;
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);
612 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
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);
622 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
624 file_offset += result;
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
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.
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
653 nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
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;
660 struct address_space *mapping = file->f_mapping;
661 struct inode *inode = mapping->host;
667 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
668 file->f_dentry->d_parent->d_name.name,
669 file->f_dentry->d_name.name,
670 (unsigned long) count, (long long) pos);
672 if (!is_sync_kiocb(iocb))
677 if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
683 retval = nfs_sync_mapping(mapping);
687 retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
689 *ppos = pos + retval;
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
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.
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.
712 * We eliminate local atime updates, see direct read above.
714 * We avoid unnecessary page cache invalidations for normal cached
715 * readers of this file.
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.
721 nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
724 struct file *file = iocb->ki_filp;
725 struct nfs_open_context *ctx =
726 (struct nfs_open_context *) file->private_data;
727 struct address_space *mapping = file->f_mapping;
728 struct inode *inode = mapping->host;
730 .iov_base = (char __user *)buf,
733 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
734 file->f_dentry->d_parent->d_name.name,
735 file->f_dentry->d_name.name,
736 (unsigned long) count, (long long) pos);
739 if (!is_sync_kiocb(iocb))
742 retval = generic_write_checks(file, &pos, &count, 0);
747 if ((ssize_t) count < 0)
755 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
758 retval = nfs_sync_mapping(mapping);
762 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
763 if (mapping->nrpages)
764 invalidate_inode_pages2(mapping);
766 iocb->ki_pos = pos + retval;
772 int nfs_init_directcache(void)
774 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
775 sizeof(struct nfs_direct_req),
776 0, SLAB_RECLAIM_ACCOUNT,
778 if (nfs_direct_cachep == NULL)
784 void nfs_destroy_directcache(void)
786 if (kmem_cache_destroy(nfs_direct_cachep))
787 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");