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
37 * 04 May 2005 support O_DIRECT with aio --cel
41 #include <linux/config.h>
42 #include <linux/errno.h>
43 #include <linux/sched.h>
44 #include <linux/kernel.h>
45 #include <linux/smp_lock.h>
46 #include <linux/file.h>
47 #include <linux/pagemap.h>
48 #include <linux/kref.h>
50 #include <linux/nfs_fs.h>
51 #include <linux/nfs_page.h>
52 #include <linux/sunrpc/clnt.h>
54 #include <asm/system.h>
55 #include <asm/uaccess.h>
56 #include <asm/atomic.h>
60 #define NFSDBG_FACILITY NFSDBG_VFS
62 static kmem_cache_t *nfs_direct_cachep;
65 * This represents a set of asynchronous requests that we're waiting on
67 struct nfs_direct_req {
68 struct kref kref; /* release manager */
71 struct list_head list, /* nfs_read/write_data structs */
72 rewrite_list; /* saved nfs_write_data structs */
73 struct nfs_open_context *ctx; /* file open context info */
74 struct kiocb * iocb; /* controlling i/o request */
75 struct inode * inode; /* target file of i/o */
76 struct page ** pages; /* pages in our buffer */
77 unsigned int npages; /* count of pages */
79 /* completion state */
80 atomic_t io_count; /* i/os we're waiting for */
81 spinlock_t lock; /* protect completion state */
82 ssize_t count, /* bytes actually processed */
83 error; /* any reported error */
84 struct completion completion; /* wait for i/o completion */
87 struct nfs_write_data * commit_data; /* special write_data for commits */
89 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
90 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
91 struct nfs_writeverf verf; /* unstable write verifier */
94 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
95 static const struct rpc_call_ops nfs_write_direct_ops;
97 static inline void get_dreq(struct nfs_direct_req *dreq)
99 atomic_inc(&dreq->io_count);
102 static inline int put_dreq(struct nfs_direct_req *dreq)
104 return atomic_dec_and_test(&dreq->io_count);
108 * nfs_direct_IO - NFS address space operation for direct I/O
109 * @rw: direction (read or write)
110 * @iocb: target I/O control block
111 * @iov: array of vectors that define I/O buffer
112 * @pos: offset in file to begin the operation
113 * @nr_segs: size of iovec array
115 * The presence of this routine in the address space ops vector means
116 * the NFS client supports direct I/O. However, we shunt off direct
117 * read and write requests before the VFS gets them, so this method
118 * should never be called.
120 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
122 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
123 iocb->ki_filp->f_dentry->d_name.name,
124 (long long) pos, nr_segs);
129 static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
132 for (i = 0; i < npages; i++) {
133 struct page *page = pages[i];
134 if (do_dirty && !PageCompound(page))
135 set_page_dirty_lock(page);
136 page_cache_release(page);
141 static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
143 int result = -ENOMEM;
144 unsigned long page_count;
147 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
148 page_count -= user_addr >> PAGE_SHIFT;
150 array_size = (page_count * sizeof(struct page *));
151 *pages = kmalloc(array_size, GFP_KERNEL);
153 down_read(¤t->mm->mmap_sem);
154 result = get_user_pages(current, current->mm, user_addr,
155 page_count, (rw == READ), 0,
157 up_read(¤t->mm->mmap_sem);
158 if (result != page_count) {
160 * If we got fewer pages than expected from
161 * get_user_pages(), the user buffer runs off the
162 * end of a mapping; return EFAULT.
165 nfs_free_user_pages(*pages, result, 0);
175 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
177 struct nfs_direct_req *dreq;
179 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
183 kref_init(&dreq->kref);
184 init_completion(&dreq->completion);
185 INIT_LIST_HEAD(&dreq->list);
186 INIT_LIST_HEAD(&dreq->rewrite_list);
189 spin_lock_init(&dreq->lock);
190 atomic_set(&dreq->io_count, 0);
198 static void nfs_direct_req_release(struct kref *kref)
200 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
202 if (dreq->ctx != NULL)
203 put_nfs_open_context(dreq->ctx);
204 kmem_cache_free(nfs_direct_cachep, dreq);
208 * Collects and returns the final error value/byte-count.
210 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
212 ssize_t result = -EIOCBQUEUED;
214 /* Async requests don't wait here */
218 result = wait_for_completion_interruptible(&dreq->completion);
221 result = dreq->error;
223 result = dreq->count;
226 kref_put(&dreq->kref, nfs_direct_req_release);
227 return (ssize_t) result;
231 * We must hold a reference to all the pages in this direct read request
232 * until the RPCs complete. This could be long *after* we are woken up in
233 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
235 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
236 * can't trust the iocb is still valid here if this is a synchronous
237 * request. If the waiter is woken prematurely, the iocb is long gone.
239 static void nfs_direct_complete(struct nfs_direct_req *dreq)
241 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
244 long res = (long) dreq->error;
246 res = (long) dreq->count;
247 aio_complete(dreq->iocb, res, 0);
249 complete_all(&dreq->completion);
251 kref_put(&dreq->kref, nfs_direct_req_release);
255 * Note we also set the number of requests we have in the dreq when we are
256 * done. This prevents races with I/O completion so we will always wait
257 * until all requests have been dispatched and completed.
259 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
261 struct list_head *list;
262 struct nfs_direct_req *dreq;
263 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
265 dreq = nfs_direct_req_alloc();
271 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
273 if (unlikely(!data)) {
274 while (!list_empty(list)) {
275 data = list_entry(list->next,
276 struct nfs_read_data, pages);
277 list_del(&data->pages);
278 nfs_readdata_free(data);
280 kref_put(&dreq->kref, nfs_direct_req_release);
284 INIT_LIST_HEAD(&data->pages);
285 list_add(&data->pages, list);
287 data->req = (struct nfs_page *) dreq;
293 kref_get(&dreq->kref);
297 static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
299 struct nfs_read_data *data = calldata;
300 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
302 if (nfs_readpage_result(task, data) != 0)
305 spin_lock(&dreq->lock);
307 if (likely(task->tk_status >= 0))
308 dreq->count += data->res.count;
310 dreq->error = task->tk_status;
312 spin_unlock(&dreq->lock);
315 nfs_direct_complete(dreq);
318 static const struct rpc_call_ops nfs_read_direct_ops = {
319 .rpc_call_done = nfs_direct_read_result,
320 .rpc_release = nfs_readdata_release,
324 * For each nfs_read_data struct that was allocated on the list, dispatch
325 * an NFS READ operation
327 static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
329 struct nfs_open_context *ctx = dreq->ctx;
330 struct inode *inode = ctx->dentry->d_inode;
331 struct list_head *list = &dreq->list;
332 struct page **pages = dreq->pages;
333 size_t rsize = NFS_SERVER(inode)->rsize;
334 unsigned int curpage, pgbase;
337 pgbase = user_addr & ~PAGE_MASK;
339 struct nfs_read_data *data;
346 BUG_ON(list_empty(list));
347 data = list_entry(list->next, struct nfs_read_data, pages);
348 list_del_init(&data->pages);
351 data->cred = ctx->cred;
352 data->args.fh = NFS_FH(inode);
353 data->args.context = ctx;
354 data->args.offset = pos;
355 data->args.pgbase = pgbase;
356 data->args.pages = &pages[curpage];
357 data->args.count = bytes;
358 data->res.fattr = &data->fattr;
360 data->res.count = bytes;
362 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
363 &nfs_read_direct_ops, data);
364 NFS_PROTO(inode)->read_setup(data);
366 data->task.tk_cookie = (unsigned long) inode;
369 rpc_execute(&data->task);
372 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
375 (long long)NFS_FILEID(inode),
377 (unsigned long long)data->args.offset);
381 curpage += pgbase >> PAGE_SHIFT;
382 pgbase &= ~PAGE_MASK;
385 } while (count != 0);
386 BUG_ON(!list_empty(list));
389 static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages)
393 struct inode *inode = iocb->ki_filp->f_mapping->host;
394 struct rpc_clnt *clnt = NFS_CLIENT(inode);
395 struct nfs_direct_req *dreq;
397 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
402 dreq->npages = nr_pages;
404 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
405 if (!is_sync_kiocb(iocb))
408 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
409 rpc_clnt_sigmask(clnt, &oldset);
410 nfs_direct_read_schedule(dreq, user_addr, count, pos);
411 result = nfs_direct_wait(dreq);
412 rpc_clnt_sigunmask(clnt, &oldset);
417 static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
419 list_splice_init(&dreq->rewrite_list, &dreq->list);
420 while (!list_empty(&dreq->list)) {
421 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
422 list_del(&data->pages);
423 nfs_writedata_release(data);
427 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
428 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
430 struct inode *inode = dreq->inode;
432 struct nfs_write_data *data;
437 list_for_each(p, &dreq->rewrite_list) {
438 data = list_entry(p, struct nfs_write_data, pages);
445 nfs_fattr_init(&data->fattr);
446 data->res.count = data->args.count;
447 memset(&data->verf, 0, sizeof(data->verf));
450 * Reuse data->task; data->args should not have changed
451 * since the original request was sent.
453 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
454 &nfs_write_direct_ops, data);
455 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
457 data->task.tk_priority = RPC_PRIORITY_NORMAL;
458 data->task.tk_cookie = (unsigned long) inode;
461 * We're called via an RPC callback, so BKL is already held.
463 rpc_execute(&data->task);
465 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
468 (long long)NFS_FILEID(inode),
470 (unsigned long long)data->args.offset);
474 nfs_direct_write_complete(dreq, inode);
477 static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
479 struct nfs_write_data *data = calldata;
480 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
482 /* Call the NFS version-specific code */
483 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
485 if (unlikely(task->tk_status < 0)) {
486 dreq->error = task->tk_status;
487 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
489 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
490 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
491 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
494 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
495 nfs_direct_write_complete(dreq, data->inode);
498 static const struct rpc_call_ops nfs_commit_direct_ops = {
499 .rpc_call_done = nfs_direct_commit_result,
500 .rpc_release = nfs_commit_release,
503 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
505 struct nfs_write_data *data = dreq->commit_data;
507 data->inode = dreq->inode;
508 data->cred = dreq->ctx->cred;
510 data->args.fh = NFS_FH(data->inode);
511 data->args.offset = 0;
512 data->args.count = 0;
514 data->res.fattr = &data->fattr;
515 data->res.verf = &data->verf;
517 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
518 &nfs_commit_direct_ops, data);
519 NFS_PROTO(data->inode)->commit_setup(data, 0);
521 data->task.tk_priority = RPC_PRIORITY_NORMAL;
522 data->task.tk_cookie = (unsigned long)data->inode;
523 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
524 dreq->commit_data = NULL;
526 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
529 rpc_execute(&data->task);
533 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
535 int flags = dreq->flags;
539 case NFS_ODIRECT_DO_COMMIT:
540 nfs_direct_commit_schedule(dreq);
542 case NFS_ODIRECT_RESCHED_WRITES:
543 nfs_direct_write_reschedule(dreq);
546 nfs_end_data_update(inode);
547 if (dreq->commit_data != NULL)
548 nfs_commit_free(dreq->commit_data);
549 nfs_direct_free_writedata(dreq);
550 nfs_direct_complete(dreq);
554 static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
556 dreq->commit_data = nfs_commit_alloc(0);
557 if (dreq->commit_data != NULL)
558 dreq->commit_data->req = (struct nfs_page *) dreq;
561 static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
563 dreq->commit_data = NULL;
566 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
568 nfs_end_data_update(inode);
569 nfs_direct_free_writedata(dreq);
570 nfs_direct_complete(dreq);
574 static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
576 struct list_head *list;
577 struct nfs_direct_req *dreq;
578 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
580 dreq = nfs_direct_req_alloc();
586 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
588 if (unlikely(!data)) {
589 while (!list_empty(list)) {
590 data = list_entry(list->next,
591 struct nfs_write_data, pages);
592 list_del(&data->pages);
593 nfs_writedata_free(data);
595 kref_put(&dreq->kref, nfs_direct_req_release);
599 INIT_LIST_HEAD(&data->pages);
600 list_add(&data->pages, list);
602 data->req = (struct nfs_page *) dreq;
609 nfs_alloc_commit_data(dreq);
611 kref_get(&dreq->kref);
615 static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
617 struct nfs_write_data *data = calldata;
618 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
619 int status = task->tk_status;
621 if (nfs_writeback_done(task, data) != 0)
624 spin_lock(&dreq->lock);
626 if (likely(status >= 0))
627 dreq->count += data->res.count;
629 dreq->error = task->tk_status;
631 if (data->res.verf->committed != NFS_FILE_SYNC) {
632 switch (dreq->flags) {
634 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
635 dreq->flags = NFS_ODIRECT_DO_COMMIT;
637 case NFS_ODIRECT_DO_COMMIT:
638 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
639 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
640 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
645 spin_unlock(&dreq->lock);
649 * NB: Return the value of the first error return code. Subsequent
650 * errors after the first one are ignored.
652 static void nfs_direct_write_release(void *calldata)
654 struct nfs_write_data *data = calldata;
655 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
658 nfs_direct_write_complete(dreq, data->inode);
661 static const struct rpc_call_ops nfs_write_direct_ops = {
662 .rpc_call_done = nfs_direct_write_result,
663 .rpc_release = nfs_direct_write_release,
667 * For each nfs_write_data struct that was allocated on the list, dispatch
668 * an NFS WRITE operation
670 static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
672 struct nfs_open_context *ctx = dreq->ctx;
673 struct inode *inode = ctx->dentry->d_inode;
674 struct list_head *list = &dreq->list;
675 struct page **pages = dreq->pages;
676 size_t wsize = NFS_SERVER(inode)->wsize;
677 unsigned int curpage, pgbase;
680 pgbase = user_addr & ~PAGE_MASK;
682 struct nfs_write_data *data;
689 BUG_ON(list_empty(list));
690 data = list_entry(list->next, struct nfs_write_data, pages);
691 list_move_tail(&data->pages, &dreq->rewrite_list);
694 data->cred = ctx->cred;
695 data->args.fh = NFS_FH(inode);
696 data->args.context = ctx;
697 data->args.offset = pos;
698 data->args.pgbase = pgbase;
699 data->args.pages = &pages[curpage];
700 data->args.count = bytes;
701 data->res.fattr = &data->fattr;
702 data->res.count = bytes;
703 data->res.verf = &data->verf;
705 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
706 &nfs_write_direct_ops, data);
707 NFS_PROTO(inode)->write_setup(data, sync);
709 data->task.tk_priority = RPC_PRIORITY_NORMAL;
710 data->task.tk_cookie = (unsigned long) inode;
713 rpc_execute(&data->task);
716 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
719 (long long)NFS_FILEID(inode),
721 (unsigned long long)data->args.offset);
725 curpage += pgbase >> PAGE_SHIFT;
726 pgbase &= ~PAGE_MASK;
729 } while (count != 0);
730 BUG_ON(!list_empty(list));
733 static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages)
737 struct inode *inode = iocb->ki_filp->f_mapping->host;
738 struct rpc_clnt *clnt = NFS_CLIENT(inode);
739 struct nfs_direct_req *dreq;
740 size_t wsize = NFS_SERVER(inode)->wsize;
743 dreq = nfs_direct_write_alloc(count, wsize);
746 if (dreq->commit_data == NULL || count < wsize)
750 dreq->npages = nr_pages;
752 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
753 if (!is_sync_kiocb(iocb))
756 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
758 nfs_begin_data_update(inode);
760 rpc_clnt_sigmask(clnt, &oldset);
761 nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
762 result = nfs_direct_wait(dreq);
763 rpc_clnt_sigunmask(clnt, &oldset);
769 * nfs_file_direct_read - file direct read operation for NFS files
770 * @iocb: target I/O control block
771 * @buf: user's buffer into which to read data
772 * @count: number of bytes to read
773 * @pos: byte offset in file where reading starts
775 * We use this function for direct reads instead of calling
776 * generic_file_aio_read() in order to avoid gfar's check to see if
777 * the request starts before the end of the file. For that check
778 * to work, we must generate a GETATTR before each direct read, and
779 * even then there is a window between the GETATTR and the subsequent
780 * READ where the file size could change. Our preference is simply
781 * to do all reads the application wants, and the server will take
782 * care of managing the end of file boundary.
784 * This function also eliminates unnecessarily updating the file's
785 * atime locally, as the NFS server sets the file's atime, and this
786 * client must read the updated atime from the server back into its
789 ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
791 ssize_t retval = -EINVAL;
794 struct file *file = iocb->ki_filp;
795 struct address_space *mapping = file->f_mapping;
797 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
798 file->f_dentry->d_parent->d_name.name,
799 file->f_dentry->d_name.name,
800 (unsigned long) count, (long long) pos);
805 if (!access_ok(VERIFY_WRITE, buf, count))
811 retval = nfs_sync_mapping(mapping);
815 retval = nfs_get_user_pages(READ, (unsigned long) buf,
821 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
824 iocb->ki_pos = pos + retval;
831 * nfs_file_direct_write - file direct write operation for NFS files
832 * @iocb: target I/O control block
833 * @buf: user's buffer from which to write data
834 * @count: number of bytes to write
835 * @pos: byte offset in file where writing starts
837 * We use this function for direct writes instead of calling
838 * generic_file_aio_write() in order to avoid taking the inode
839 * semaphore and updating the i_size. The NFS server will set
840 * the new i_size and this client must read the updated size
841 * back into its cache. We let the server do generic write
842 * parameter checking and report problems.
844 * We also avoid an unnecessary invocation of generic_osync_inode(),
845 * as it is fairly meaningless to sync the metadata of an NFS file.
847 * We eliminate local atime updates, see direct read above.
849 * We avoid unnecessary page cache invalidations for normal cached
850 * readers of this file.
852 * Note that O_APPEND is not supported for NFS direct writes, as there
853 * is no atomic O_APPEND write facility in the NFS protocol.
855 ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
860 struct file *file = iocb->ki_filp;
861 struct address_space *mapping = file->f_mapping;
863 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
864 file->f_dentry->d_parent->d_name.name,
865 file->f_dentry->d_name.name,
866 (unsigned long) count, (long long) pos);
868 retval = generic_write_checks(file, &pos, &count, 0);
873 if ((ssize_t) count < 0)
880 if (!access_ok(VERIFY_READ, buf, count))
883 retval = nfs_sync_mapping(mapping);
887 retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
893 retval = nfs_direct_write(iocb, (unsigned long) buf, count,
894 pos, pages, page_count);
897 * XXX: nfs_end_data_update() already ensures this file's
898 * cached data is subsequently invalidated. Do we really
899 * need to call invalidate_inode_pages2() again here?
901 * For aio writes, this invalidation will almost certainly
902 * occur before the writes complete. Kind of racey.
904 if (mapping->nrpages)
905 invalidate_inode_pages2(mapping);
908 iocb->ki_pos = pos + retval;
915 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
918 int __init nfs_init_directcache(void)
920 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
921 sizeof(struct nfs_direct_req),
922 0, (SLAB_RECLAIM_ACCOUNT|
925 if (nfs_direct_cachep == NULL)
932 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
935 void __exit nfs_destroy_directcache(void)
937 if (kmem_cache_destroy(nfs_direct_cachep))
938 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");