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);
104 static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
106 int result = -ENOMEM;
107 unsigned long page_count;
110 /* set an arbitrary limit to prevent type overflow */
111 /* XXX: this can probably be as large as INT_MAX */
112 if (size > MAX_DIRECTIO_SIZE) {
117 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
118 page_count -= user_addr >> PAGE_SHIFT;
120 array_size = (page_count * sizeof(struct page *));
121 *pages = kmalloc(array_size, GFP_KERNEL);
123 down_read(¤t->mm->mmap_sem);
124 result = get_user_pages(current, current->mm, user_addr,
125 page_count, (rw == READ), 0,
127 up_read(¤t->mm->mmap_sem);
129 * If we got fewer pages than expected from get_user_pages(),
130 * the user buffer runs off the end of a mapping; return EFAULT.
132 if (result >= 0 && result < page_count) {
133 nfs_free_user_pages(*pages, result, 0);
141 static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
144 for (i = 0; i < npages; i++) {
145 struct page *page = pages[i];
146 if (do_dirty && !PageCompound(page))
147 set_page_dirty_lock(page);
148 page_cache_release(page);
153 static void nfs_direct_req_release(struct kref *kref)
155 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
156 kmem_cache_free(nfs_direct_cachep, dreq);
160 * Note we also set the number of requests we have in the dreq when we are
161 * done. This prevents races with I/O completion so we will always wait
162 * until all requests have been dispatched and completed.
164 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize)
166 struct list_head *list;
167 struct nfs_direct_req *dreq;
168 unsigned int reads = 0;
169 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
171 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
175 kref_init(&dreq->kref);
176 init_waitqueue_head(&dreq->wait);
177 INIT_LIST_HEAD(&dreq->list);
178 atomic_set(&dreq->count, 0);
179 atomic_set(&dreq->error, 0);
183 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
185 if (unlikely(!data)) {
186 while (!list_empty(list)) {
187 data = list_entry(list->next,
188 struct nfs_read_data, pages);
189 list_del(&data->pages);
190 nfs_readdata_free(data);
192 kref_put(&dreq->kref, nfs_direct_req_release);
196 INIT_LIST_HEAD(&data->pages);
197 list_add(&data->pages, list);
199 data->req = (struct nfs_page *) dreq;
205 kref_get(&dreq->kref);
206 atomic_set(&dreq->complete, reads);
211 * We must hold a reference to all the pages in this direct read request
212 * until the RPCs complete. This could be long *after* we are woken up in
213 * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server).
215 static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
217 struct nfs_read_data *data = calldata;
218 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
220 if (nfs_readpage_result(task, data) != 0)
222 if (likely(task->tk_status >= 0))
223 atomic_add(data->res.count, &dreq->count);
225 atomic_set(&dreq->error, task->tk_status);
227 if (unlikely(atomic_dec_and_test(&dreq->complete))) {
228 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
229 wake_up(&dreq->wait);
230 kref_put(&dreq->kref, nfs_direct_req_release);
234 static const struct rpc_call_ops nfs_read_direct_ops = {
235 .rpc_call_done = nfs_direct_read_result,
236 .rpc_release = nfs_readdata_release,
240 * For each nfs_read_data struct that was allocated on the list, dispatch
241 * an NFS READ operation
243 static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset)
245 struct list_head *list = &dreq->list;
246 struct page **pages = dreq->pages;
247 unsigned int curpage, pgbase;
248 unsigned int rsize = NFS_SERVER(inode)->rsize;
251 pgbase = user_addr & ~PAGE_MASK;
253 struct nfs_read_data *data;
260 data = list_entry(list->next, struct nfs_read_data, pages);
261 list_del_init(&data->pages);
264 data->cred = ctx->cred;
265 data->args.fh = NFS_FH(inode);
266 data->args.context = ctx;
267 data->args.offset = file_offset;
268 data->args.pgbase = pgbase;
269 data->args.pages = &pages[curpage];
270 data->args.count = bytes;
271 data->res.fattr = &data->fattr;
273 data->res.count = bytes;
275 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
276 &nfs_read_direct_ops, data);
277 NFS_PROTO(inode)->read_setup(data);
279 data->task.tk_cookie = (unsigned long) inode;
282 rpc_execute(&data->task);
285 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
288 (long long)NFS_FILEID(inode),
290 (unsigned long long)data->args.offset);
292 file_offset += bytes;
294 curpage += pgbase >> PAGE_SHIFT;
295 pgbase &= ~PAGE_MASK;
298 } while (count != 0);
302 * Collects and returns the final error value/byte-count.
304 static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
309 result = wait_event_interruptible(dreq->wait,
310 (atomic_read(&dreq->complete) == 0));
312 wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
316 result = atomic_read(&dreq->error);
318 result = atomic_read(&dreq->count);
320 kref_put(&dreq->kref, nfs_direct_req_release);
321 return (ssize_t) result;
324 static ssize_t nfs_direct_read_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, unsigned int nr_pages)
328 struct rpc_clnt *clnt = NFS_CLIENT(inode);
329 struct nfs_direct_req *dreq;
331 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
336 dreq->npages = nr_pages;
339 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
340 rpc_clnt_sigmask(clnt, &oldset);
341 nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
343 result = nfs_direct_read_wait(dreq, clnt->cl_intr);
344 rpc_clnt_sigunmask(clnt, &oldset);
350 * We've already pushed out any non-direct writes so that this read
351 * will see them when we read from the server.
353 static ssize_t nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs)
355 ssize_t tot_bytes = 0;
356 unsigned long seg = 0;
358 while ((seg < nr_segs) && (tot_bytes >= 0)) {
362 const struct iovec *vec = &iov[seg++];
363 unsigned long user_addr = (unsigned long) vec->iov_base;
364 size_t size = vec->iov_len;
366 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
367 if (page_count < 0) {
368 nfs_free_user_pages(pages, 0, 0);
374 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
375 file_offset, pages, page_count);
383 file_offset += result;
391 static ssize_t nfs_direct_write_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, int nr_pages)
393 const unsigned int wsize = NFS_SERVER(inode)->wsize;
395 int curpage, need_commit;
396 ssize_t result, tot_bytes;
397 struct nfs_writeverf first_verf;
398 struct nfs_write_data *wdata;
400 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
404 wdata->inode = inode;
405 wdata->cred = ctx->cred;
406 wdata->args.fh = NFS_FH(inode);
407 wdata->args.context = ctx;
408 wdata->args.stable = NFS_UNSTABLE;
409 if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
410 wdata->args.stable = NFS_FILE_SYNC;
411 wdata->res.fattr = &wdata->fattr;
412 wdata->res.verf = &wdata->verf;
414 nfs_begin_data_update(inode);
420 wdata->args.pgbase = user_addr & ~PAGE_MASK;
421 wdata->args.offset = file_offset;
423 wdata->args.count = request;
424 if (wdata->args.count > wsize)
425 wdata->args.count = wsize;
426 wdata->args.pages = &pages[curpage];
428 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
429 wdata->args.count, (long long) wdata->args.offset,
430 user_addr + tot_bytes, wdata->args.pgbase, curpage);
433 result = NFS_PROTO(inode)->write(wdata);
443 memcpy(&first_verf.verifier, &wdata->verf.verifier,
444 sizeof(first_verf.verifier));
445 if (wdata->verf.committed != NFS_FILE_SYNC) {
447 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
448 sizeof(first_verf.verifier)))
454 /* in case of a short write: stop now, let the app recover */
455 if (result < wdata->args.count)
458 wdata->args.offset += result;
459 wdata->args.pgbase += result;
460 curpage += wdata->args.pgbase >> PAGE_SHIFT;
461 wdata->args.pgbase &= ~PAGE_MASK;
463 } while (request != 0);
466 * Commit data written so far, even in the event of an error
469 wdata->args.count = tot_bytes;
470 wdata->args.offset = file_offset;
473 result = NFS_PROTO(inode)->commit(wdata);
476 if (result < 0 || memcmp(&first_verf.verifier,
477 &wdata->verf.verifier,
478 sizeof(first_verf.verifier)) != 0)
484 nfs_end_data_update(inode);
485 nfs_writedata_free(wdata);
489 wdata->args.stable = NFS_FILE_SYNC;
494 * Upon return, generic_file_direct_IO invalidates any cached pages
495 * that non-direct readers might access, so they will pick up these
496 * writes immediately.
498 static ssize_t nfs_direct_write(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs)
500 ssize_t tot_bytes = 0;
501 unsigned long seg = 0;
503 while ((seg < nr_segs) && (tot_bytes >= 0)) {
507 const struct iovec *vec = &iov[seg++];
508 unsigned long user_addr = (unsigned long) vec->iov_base;
509 size_t size = vec->iov_len;
511 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
512 if (page_count < 0) {
513 nfs_free_user_pages(pages, 0, 0);
519 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
520 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
521 file_offset, pages, page_count);
522 nfs_free_user_pages(pages, page_count, 0);
529 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
531 file_offset += result;
539 * nfs_file_direct_read - file direct read operation for NFS files
540 * @iocb: target I/O control block
541 * @buf: user's buffer into which to read data
542 * count: number of bytes to read
543 * pos: byte offset in file where reading starts
545 * We use this function for direct reads instead of calling
546 * generic_file_aio_read() in order to avoid gfar's check to see if
547 * the request starts before the end of the file. For that check
548 * to work, we must generate a GETATTR before each direct read, and
549 * even then there is a window between the GETATTR and the subsequent
550 * READ where the file size could change. So our preference is simply
551 * to do all reads the application wants, and the server will take
552 * care of managing the end of file boundary.
554 * This function also eliminates unnecessarily updating the file's
555 * atime locally, as the NFS server sets the file's atime, and this
556 * client must read the updated atime from the server back into its
559 ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
561 ssize_t retval = -EINVAL;
562 loff_t *ppos = &iocb->ki_pos;
563 struct file *file = iocb->ki_filp;
564 struct nfs_open_context *ctx =
565 (struct nfs_open_context *) file->private_data;
566 struct address_space *mapping = file->f_mapping;
567 struct inode *inode = mapping->host;
573 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
574 file->f_dentry->d_parent->d_name.name,
575 file->f_dentry->d_name.name,
576 (unsigned long) count, (long long) pos);
578 if (!is_sync_kiocb(iocb))
583 if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
589 retval = nfs_sync_mapping(mapping);
593 retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
595 *ppos = pos + retval;
602 * nfs_file_direct_write - file direct write operation for NFS files
603 * @iocb: target I/O control block
604 * @buf: user's buffer from which to write data
605 * count: number of bytes to write
606 * pos: byte offset in file where writing starts
608 * We use this function for direct writes instead of calling
609 * generic_file_aio_write() in order to avoid taking the inode
610 * semaphore and updating the i_size. The NFS server will set
611 * the new i_size and this client must read the updated size
612 * back into its cache. We let the server do generic write
613 * parameter checking and report problems.
615 * We also avoid an unnecessary invocation of generic_osync_inode(),
616 * as it is fairly meaningless to sync the metadata of an NFS file.
618 * We eliminate local atime updates, see direct read above.
620 * We avoid unnecessary page cache invalidations for normal cached
621 * readers of this file.
623 * Note that O_APPEND is not supported for NFS direct writes, as there
624 * is no atomic O_APPEND write facility in the NFS protocol.
626 ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
629 struct file *file = iocb->ki_filp;
630 struct nfs_open_context *ctx =
631 (struct nfs_open_context *) file->private_data;
632 struct address_space *mapping = file->f_mapping;
633 struct inode *inode = mapping->host;
635 .iov_base = (char __user *)buf,
638 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
639 file->f_dentry->d_parent->d_name.name,
640 file->f_dentry->d_name.name,
641 (unsigned long) count, (long long) pos);
644 if (!is_sync_kiocb(iocb))
647 retval = generic_write_checks(file, &pos, &count, 0);
652 if ((ssize_t) count < 0)
660 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
663 retval = nfs_sync_mapping(mapping);
667 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
668 if (mapping->nrpages)
669 invalidate_inode_pages2(mapping);
671 iocb->ki_pos = pos + retval;
677 int nfs_init_directcache(void)
679 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
680 sizeof(struct nfs_direct_req),
681 0, SLAB_RECLAIM_ACCOUNT,
683 if (nfs_direct_cachep == NULL)
689 void nfs_destroy_directcache(void)
691 if (kmem_cache_destroy(nfs_direct_cachep))
692 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");