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_get_user_pages - find and set up pages underlying user's buffer
83 * rw: direction (read or write)
84 * user_addr: starting address of this segment of user's buffer
85 * count: size of this segment
86 * @pages: returned array of page struct pointers underlying user's buffer
89 nfs_get_user_pages(int rw, unsigned long user_addr, size_t size,
93 unsigned long page_count;
96 /* set an arbitrary limit to prevent type overflow */
97 /* XXX: this can probably be as large as INT_MAX */
98 if (size > MAX_DIRECTIO_SIZE) {
103 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
104 page_count -= user_addr >> PAGE_SHIFT;
106 array_size = (page_count * sizeof(struct page *));
107 *pages = kmalloc(array_size, GFP_KERNEL);
109 down_read(¤t->mm->mmap_sem);
110 result = get_user_pages(current, current->mm, user_addr,
111 page_count, (rw == READ), 0,
113 up_read(¤t->mm->mmap_sem);
115 * If we got fewer pages than expected from get_user_pages(),
116 * the user buffer runs off the end of a mapping; return EFAULT.
118 if (result >= 0 && result < page_count) {
119 nfs_free_user_pages(*pages, result, 0);
128 * nfs_free_user_pages - tear down page struct array
129 * @pages: array of page struct pointers underlying target buffer
130 * @npages: number of pages in the array
131 * @do_dirty: dirty the pages as we release them
134 nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
137 for (i = 0; i < npages; i++) {
138 struct page *page = pages[i];
139 if (do_dirty && !PageCompound(page))
140 set_page_dirty_lock(page);
141 page_cache_release(page);
147 * nfs_direct_req_release - release nfs_direct_req structure for direct read
148 * @kref: kref object embedded in an nfs_direct_req structure
151 static void nfs_direct_req_release(struct kref *kref)
153 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
154 kmem_cache_free(nfs_direct_cachep, dreq);
158 * nfs_direct_read_alloc - allocate nfs_read_data structures for direct read
159 * @count: count of bytes for the read request
160 * @rsize: local rsize setting
162 * Note we also set the number of requests we have in the dreq when we are
163 * done. This prevents races with I/O completion so we will always wait
164 * until all requests have been dispatched and completed.
166 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize)
168 struct list_head *list;
169 struct nfs_direct_req *dreq;
170 unsigned int reads = 0;
171 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
173 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
177 kref_init(&dreq->kref);
178 init_waitqueue_head(&dreq->wait);
179 INIT_LIST_HEAD(&dreq->list);
180 atomic_set(&dreq->count, 0);
181 atomic_set(&dreq->error, 0);
185 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
187 if (unlikely(!data)) {
188 while (!list_empty(list)) {
189 data = list_entry(list->next,
190 struct nfs_read_data, pages);
191 list_del(&data->pages);
192 nfs_readdata_free(data);
194 kref_put(&dreq->kref, nfs_direct_req_release);
198 INIT_LIST_HEAD(&data->pages);
199 list_add(&data->pages, list);
201 data->req = (struct nfs_page *) dreq;
207 kref_get(&dreq->kref);
208 atomic_set(&dreq->complete, reads);
213 * nfs_direct_read_result - handle a read reply for a direct read request
214 * @data: address of NFS READ operation control block
215 * @status: status of this NFS READ operation
217 * We must hold a reference to all the pages in this direct read request
218 * until the RPCs complete. This could be long *after* we are woken up in
219 * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server).
221 static void nfs_direct_read_result(struct nfs_read_data *data, int status)
223 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
225 if (likely(status >= 0))
226 atomic_add(data->res.count, &dreq->count);
228 atomic_set(&dreq->error, status);
230 if (unlikely(atomic_dec_and_test(&dreq->complete))) {
231 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
232 wake_up(&dreq->wait);
233 kref_put(&dreq->kref, nfs_direct_req_release);
238 * nfs_direct_read_schedule - dispatch NFS READ operations for a direct read
239 * @dreq: address of nfs_direct_req struct for this request
240 * @inode: target inode
241 * @ctx: target file open context
242 * @user_addr: starting address of this segment of user's buffer
243 * @count: size of this segment
244 * @file_offset: offset in file to begin the operation
246 * For each nfs_read_data struct that was allocated on the list, dispatch
247 * an NFS READ operation
249 static void nfs_direct_read_schedule(struct nfs_direct_req *dreq,
250 struct inode *inode, struct nfs_open_context *ctx,
251 unsigned long user_addr, size_t count, loff_t file_offset)
253 struct list_head *list = &dreq->list;
254 struct page **pages = dreq->pages;
255 unsigned int curpage, pgbase;
256 unsigned int rsize = NFS_SERVER(inode)->rsize;
259 pgbase = user_addr & ~PAGE_MASK;
261 struct nfs_read_data *data;
268 data = list_entry(list->next, struct nfs_read_data, pages);
269 list_del_init(&data->pages);
272 data->cred = ctx->cred;
273 data->args.fh = NFS_FH(inode);
274 data->args.context = ctx;
275 data->args.offset = file_offset;
276 data->args.pgbase = pgbase;
277 data->args.pages = &pages[curpage];
278 data->args.count = bytes;
279 data->res.fattr = &data->fattr;
281 data->res.count = bytes;
283 NFS_PROTO(inode)->read_setup(data);
285 data->task.tk_cookie = (unsigned long) inode;
286 data->complete = nfs_direct_read_result;
289 rpc_execute(&data->task);
292 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
295 (long long)NFS_FILEID(inode),
297 (unsigned long long)data->args.offset);
299 file_offset += bytes;
301 curpage += pgbase >> PAGE_SHIFT;
302 pgbase &= ~PAGE_MASK;
305 } while (count != 0);
309 * nfs_direct_read_wait - wait for I/O completion for direct reads
310 * @dreq: request on which we are to wait
311 * @intr: whether or not this wait can be interrupted
313 * Collects and returns the final error value/byte-count.
315 static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
320 result = wait_event_interruptible(dreq->wait,
321 (atomic_read(&dreq->complete) == 0));
323 wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
327 result = atomic_read(&dreq->error);
329 result = atomic_read(&dreq->count);
331 kref_put(&dreq->kref, nfs_direct_req_release);
332 return (ssize_t) result;
336 * nfs_direct_read_seg - Read in one iov segment. Generate separate
337 * read RPCs for each "rsize" bytes.
338 * @inode: target inode
339 * @ctx: target file open context
340 * @user_addr: starting address of this segment of user's buffer
341 * @count: size of this segment
342 * @file_offset: offset in file to begin the operation
343 * @pages: array of addresses of page structs defining user's buffer
344 * @nr_pages: number of pages in the array
347 static ssize_t nfs_direct_read_seg(struct inode *inode,
348 struct nfs_open_context *ctx, unsigned long user_addr,
349 size_t count, loff_t file_offset, struct page **pages,
350 unsigned int nr_pages)
354 struct rpc_clnt *clnt = NFS_CLIENT(inode);
355 struct nfs_direct_req *dreq;
357 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
362 dreq->npages = nr_pages;
365 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
366 rpc_clnt_sigmask(clnt, &oldset);
367 nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
369 result = nfs_direct_read_wait(dreq, clnt->cl_intr);
370 rpc_clnt_sigunmask(clnt, &oldset);
376 * nfs_direct_read - For each iov segment, map the user's buffer
377 * then generate read RPCs.
378 * @inode: target inode
379 * @ctx: target file open context
380 * @iov: array of vectors that define I/O buffer
381 * file_offset: offset in file to begin the operation
382 * nr_segs: size of iovec array
384 * We've already pushed out any non-direct writes so that this read
385 * will see them when we read from the server.
388 nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx,
389 const struct iovec *iov, loff_t file_offset,
390 unsigned long nr_segs)
392 ssize_t tot_bytes = 0;
393 unsigned long seg = 0;
395 while ((seg < nr_segs) && (tot_bytes >= 0)) {
399 const struct iovec *vec = &iov[seg++];
400 unsigned long user_addr = (unsigned long) vec->iov_base;
401 size_t size = vec->iov_len;
403 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
404 if (page_count < 0) {
405 nfs_free_user_pages(pages, 0, 0);
411 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
412 file_offset, pages, page_count);
420 file_offset += result;
429 * nfs_direct_write_seg - Write out one iov segment. Generate separate
430 * write RPCs for each "wsize" bytes, then commit.
431 * @inode: target inode
432 * @ctx: target file open context
433 * user_addr: starting address of this segment of user's buffer
434 * count: size of this segment
435 * file_offset: offset in file to begin the operation
436 * @pages: array of addresses of page structs defining user's buffer
437 * nr_pages: size of pages array
439 static ssize_t nfs_direct_write_seg(struct inode *inode,
440 struct nfs_open_context *ctx, unsigned long user_addr,
441 size_t count, loff_t file_offset, struct page **pages,
444 const unsigned int wsize = NFS_SERVER(inode)->wsize;
446 int curpage, need_commit;
447 ssize_t result, tot_bytes;
448 struct nfs_writeverf first_verf;
449 struct nfs_write_data *wdata;
451 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
455 wdata->inode = inode;
456 wdata->cred = ctx->cred;
457 wdata->args.fh = NFS_FH(inode);
458 wdata->args.context = ctx;
459 wdata->args.stable = NFS_UNSTABLE;
460 if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
461 wdata->args.stable = NFS_FILE_SYNC;
462 wdata->res.fattr = &wdata->fattr;
463 wdata->res.verf = &wdata->verf;
465 nfs_begin_data_update(inode);
471 wdata->args.pgbase = user_addr & ~PAGE_MASK;
472 wdata->args.offset = file_offset;
474 wdata->args.count = request;
475 if (wdata->args.count > wsize)
476 wdata->args.count = wsize;
477 wdata->args.pages = &pages[curpage];
479 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
480 wdata->args.count, (long long) wdata->args.offset,
481 user_addr + tot_bytes, wdata->args.pgbase, curpage);
484 result = NFS_PROTO(inode)->write(wdata);
494 memcpy(&first_verf.verifier, &wdata->verf.verifier,
495 sizeof(first_verf.verifier));
496 if (wdata->verf.committed != NFS_FILE_SYNC) {
498 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
499 sizeof(first_verf.verifier)))
505 /* in case of a short write: stop now, let the app recover */
506 if (result < wdata->args.count)
509 wdata->args.offset += result;
510 wdata->args.pgbase += result;
511 curpage += wdata->args.pgbase >> PAGE_SHIFT;
512 wdata->args.pgbase &= ~PAGE_MASK;
514 } while (request != 0);
517 * Commit data written so far, even in the event of an error
520 wdata->args.count = tot_bytes;
521 wdata->args.offset = file_offset;
524 result = NFS_PROTO(inode)->commit(wdata);
527 if (result < 0 || memcmp(&first_verf.verifier,
528 &wdata->verf.verifier,
529 sizeof(first_verf.verifier)) != 0)
535 nfs_end_data_update(inode);
536 nfs_writedata_free(wdata);
540 wdata->args.stable = NFS_FILE_SYNC;
545 * nfs_direct_write - For each iov segment, map the user's buffer
546 * then generate write and commit RPCs.
547 * @inode: target inode
548 * @ctx: target file open context
549 * @iov: array of vectors that define I/O buffer
550 * file_offset: offset in file to begin the operation
551 * nr_segs: size of iovec array
553 * Upon return, generic_file_direct_IO invalidates any cached pages
554 * that non-direct readers might access, so they will pick up these
555 * writes immediately.
557 static ssize_t nfs_direct_write(struct inode *inode,
558 struct nfs_open_context *ctx, const struct iovec *iov,
559 loff_t file_offset, unsigned long nr_segs)
561 ssize_t tot_bytes = 0;
562 unsigned long seg = 0;
564 while ((seg < nr_segs) && (tot_bytes >= 0)) {
568 const struct iovec *vec = &iov[seg++];
569 unsigned long user_addr = (unsigned long) vec->iov_base;
570 size_t size = vec->iov_len;
572 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
573 if (page_count < 0) {
574 nfs_free_user_pages(pages, 0, 0);
580 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
581 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
582 file_offset, pages, page_count);
583 nfs_free_user_pages(pages, page_count, 0);
590 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
592 file_offset += result;
600 * nfs_direct_IO - NFS address space operation for direct I/O
601 * rw: direction (read or write)
602 * @iocb: target I/O control block
603 * @iov: array of vectors that define I/O buffer
604 * file_offset: offset in file to begin the operation
605 * nr_segs: size of iovec array
609 nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
610 loff_t file_offset, unsigned long nr_segs)
612 ssize_t result = -EINVAL;
613 struct file *file = iocb->ki_filp;
614 struct nfs_open_context *ctx;
615 struct dentry *dentry = file->f_dentry;
616 struct inode *inode = dentry->d_inode;
619 * No support for async yet
621 if (!is_sync_kiocb(iocb))
624 ctx = (struct nfs_open_context *)file->private_data;
627 dprintk("NFS: direct_IO(read) (%s) off/no(%Lu/%lu)\n",
628 dentry->d_name.name, file_offset, nr_segs);
630 result = nfs_direct_read(inode, ctx, iov,
631 file_offset, nr_segs);
634 dprintk("NFS: direct_IO(write) (%s) off/no(%Lu/%lu)\n",
635 dentry->d_name.name, file_offset, nr_segs);
637 result = nfs_direct_write(inode, ctx, iov,
638 file_offset, nr_segs);
647 * nfs_file_direct_read - file direct read operation for NFS files
648 * @iocb: target I/O control block
649 * @buf: user's buffer into which to read data
650 * count: number of bytes to read
651 * pos: byte offset in file where reading starts
653 * We use this function for direct reads instead of calling
654 * generic_file_aio_read() in order to avoid gfar's check to see if
655 * the request starts before the end of the file. For that check
656 * to work, we must generate a GETATTR before each direct read, and
657 * even then there is a window between the GETATTR and the subsequent
658 * READ where the file size could change. So our preference is simply
659 * to do all reads the application wants, and the server will take
660 * care of managing the end of file boundary.
662 * This function also eliminates unnecessarily updating the file's
663 * atime locally, as the NFS server sets the file's atime, and this
664 * client must read the updated atime from the server back into its
668 nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
670 ssize_t retval = -EINVAL;
671 loff_t *ppos = &iocb->ki_pos;
672 struct file *file = iocb->ki_filp;
673 struct nfs_open_context *ctx =
674 (struct nfs_open_context *) file->private_data;
675 struct address_space *mapping = file->f_mapping;
676 struct inode *inode = mapping->host;
682 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
683 file->f_dentry->d_parent->d_name.name,
684 file->f_dentry->d_name.name,
685 (unsigned long) count, (long long) pos);
687 if (!is_sync_kiocb(iocb))
692 if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
698 retval = nfs_sync_mapping(mapping);
702 retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
704 *ppos = pos + retval;
711 * nfs_file_direct_write - file direct write operation for NFS files
712 * @iocb: target I/O control block
713 * @buf: user's buffer from which to write data
714 * count: number of bytes to write
715 * pos: byte offset in file where writing starts
717 * We use this function for direct writes instead of calling
718 * generic_file_aio_write() in order to avoid taking the inode
719 * semaphore and updating the i_size. The NFS server will set
720 * the new i_size and this client must read the updated size
721 * back into its cache. We let the server do generic write
722 * parameter checking and report problems.
724 * We also avoid an unnecessary invocation of generic_osync_inode(),
725 * as it is fairly meaningless to sync the metadata of an NFS file.
727 * We eliminate local atime updates, see direct read above.
729 * We avoid unnecessary page cache invalidations for normal cached
730 * readers of this file.
732 * Note that O_APPEND is not supported for NFS direct writes, as there
733 * is no atomic O_APPEND write facility in the NFS protocol.
736 nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
739 struct file *file = iocb->ki_filp;
740 struct nfs_open_context *ctx =
741 (struct nfs_open_context *) file->private_data;
742 struct address_space *mapping = file->f_mapping;
743 struct inode *inode = mapping->host;
745 .iov_base = (char __user *)buf,
748 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
749 file->f_dentry->d_parent->d_name.name,
750 file->f_dentry->d_name.name,
751 (unsigned long) count, (long long) pos);
754 if (!is_sync_kiocb(iocb))
757 retval = generic_write_checks(file, &pos, &count, 0);
762 if ((ssize_t) count < 0)
770 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
773 retval = nfs_sync_mapping(mapping);
777 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
778 if (mapping->nrpages)
779 invalidate_inode_pages2(mapping);
781 iocb->ki_pos = pos + retval;
787 int nfs_init_directcache(void)
789 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
790 sizeof(struct nfs_direct_req),
791 0, SLAB_RECLAIM_ACCOUNT,
793 if (nfs_direct_cachep == NULL)
799 void nfs_destroy_directcache(void)
801 if (kmem_cache_destroy(nfs_direct_cachep))
802 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");