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 */
77 /* completion state */
78 atomic_t io_count; /* i/os we're waiting for */
79 spinlock_t lock; /* protect completion state */
80 ssize_t count, /* bytes actually processed */
81 error; /* any reported error */
82 struct completion completion; /* wait for i/o completion */
85 struct nfs_write_data * commit_data; /* special write_data for commits */
87 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
88 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
89 struct nfs_writeverf verf; /* unstable write verifier */
92 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
93 static const struct rpc_call_ops nfs_write_direct_ops;
95 static inline void get_dreq(struct nfs_direct_req *dreq)
97 atomic_inc(&dreq->io_count);
100 static inline int put_dreq(struct nfs_direct_req *dreq)
102 return atomic_dec_and_test(&dreq->io_count);
106 * "size" is never larger than rsize or wsize.
108 static inline int nfs_direct_count_pages(unsigned long user_addr, size_t size)
112 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
113 page_count -= user_addr >> PAGE_SHIFT;
114 BUG_ON(page_count < 0);
120 * nfs_direct_IO - NFS address space operation for direct I/O
121 * @rw: direction (read or write)
122 * @iocb: target I/O control block
123 * @iov: array of vectors that define I/O buffer
124 * @pos: offset in file to begin the operation
125 * @nr_segs: size of iovec array
127 * The presence of this routine in the address space ops vector means
128 * the NFS client supports direct I/O. However, we shunt off direct
129 * read and write requests before the VFS gets them, so this method
130 * should never be called.
132 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
134 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
135 iocb->ki_filp->f_dentry->d_name.name,
136 (long long) pos, nr_segs);
141 static void nfs_direct_dirty_pages(struct page **pages, int npages)
144 for (i = 0; i < npages; i++) {
145 struct page *page = pages[i];
146 if (!PageCompound(page))
147 set_page_dirty_lock(page);
151 static void nfs_direct_release_pages(struct page **pages, int npages)
154 for (i = 0; i < npages; i++)
155 page_cache_release(pages[i]);
158 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
160 struct nfs_direct_req *dreq;
162 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
166 kref_init(&dreq->kref);
167 init_completion(&dreq->completion);
168 INIT_LIST_HEAD(&dreq->list);
169 INIT_LIST_HEAD(&dreq->rewrite_list);
172 spin_lock_init(&dreq->lock);
173 atomic_set(&dreq->io_count, 0);
181 static void nfs_direct_req_release(struct kref *kref)
183 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
185 if (dreq->ctx != NULL)
186 put_nfs_open_context(dreq->ctx);
187 kmem_cache_free(nfs_direct_cachep, dreq);
191 * Collects and returns the final error value/byte-count.
193 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
195 ssize_t result = -EIOCBQUEUED;
197 /* Async requests don't wait here */
201 result = wait_for_completion_interruptible(&dreq->completion);
204 result = dreq->error;
206 result = dreq->count;
209 kref_put(&dreq->kref, nfs_direct_req_release);
210 return (ssize_t) result;
214 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
215 * the iocb is still valid here if this is a synchronous request.
217 static void nfs_direct_complete(struct nfs_direct_req *dreq)
220 long res = (long) dreq->error;
222 res = (long) dreq->count;
223 aio_complete(dreq->iocb, res, 0);
225 complete_all(&dreq->completion);
227 kref_put(&dreq->kref, nfs_direct_req_release);
231 * Note we also set the number of requests we have in the dreq when we are
232 * done. This prevents races with I/O completion so we will always wait
233 * until all requests have been dispatched and completed.
235 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
237 struct list_head *list;
238 struct nfs_direct_req *dreq;
239 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
241 dreq = nfs_direct_req_alloc();
247 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
249 if (unlikely(!data)) {
250 while (!list_empty(list)) {
251 data = list_entry(list->next,
252 struct nfs_read_data, pages);
253 list_del(&data->pages);
254 nfs_readdata_free(data);
256 kref_put(&dreq->kref, nfs_direct_req_release);
260 INIT_LIST_HEAD(&data->pages);
261 list_add(&data->pages, list);
263 data->req = (struct nfs_page *) dreq;
269 kref_get(&dreq->kref);
274 * We must hold a reference to all the pages in this direct read request
275 * until the RPCs complete. This could be long *after* we are woken up in
276 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
278 static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
280 struct nfs_read_data *data = calldata;
281 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
283 if (nfs_readpage_result(task, data) != 0)
286 nfs_direct_dirty_pages(data->pagevec, data->npages);
287 nfs_direct_release_pages(data->pagevec, data->npages);
289 spin_lock(&dreq->lock);
291 if (likely(task->tk_status >= 0))
292 dreq->count += data->res.count;
294 dreq->error = task->tk_status;
296 spin_unlock(&dreq->lock);
299 nfs_direct_complete(dreq);
302 static const struct rpc_call_ops nfs_read_direct_ops = {
303 .rpc_call_done = nfs_direct_read_result,
304 .rpc_release = nfs_readdata_release,
308 * For each nfs_read_data struct that was allocated on the list, dispatch
309 * an NFS READ operation. If get_user_pages() fails, we stop sending reads.
310 * Read length accounting is handled by nfs_direct_read_result().
311 * Otherwise, if no requests have been sent, just return an error.
313 static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
315 struct nfs_open_context *ctx = dreq->ctx;
316 struct inode *inode = ctx->dentry->d_inode;
317 struct list_head *list = &dreq->list;
318 size_t rsize = NFS_SERVER(inode)->rsize;
322 struct nfs_read_data *data;
324 pgbase = user_addr & ~PAGE_MASK;
332 BUG_ON(list_empty(list));
333 data = list_entry(list->next, struct nfs_read_data, pages);
334 list_del_init(&data->pages);
336 data->npages = nfs_direct_count_pages(user_addr, bytes);
337 down_read(¤t->mm->mmap_sem);
338 result = get_user_pages(current, current->mm, user_addr,
339 data->npages, 1, 0, data->pagevec, NULL);
340 up_read(¤t->mm->mmap_sem);
341 if (unlikely(result < data->npages))
345 data->cred = ctx->cred;
346 data->args.fh = NFS_FH(inode);
347 data->args.context = ctx;
348 data->args.offset = pos;
349 data->args.pgbase = pgbase;
350 data->args.pages = data->pagevec;
351 data->args.count = bytes;
352 data->res.fattr = &data->fattr;
354 data->res.count = bytes;
356 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
357 &nfs_read_direct_ops, data);
358 NFS_PROTO(inode)->read_setup(data);
360 data->task.tk_cookie = (unsigned long) inode;
363 rpc_execute(&data->task);
366 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
369 (long long)NFS_FILEID(inode),
371 (unsigned long long)data->args.offset);
377 pgbase &= ~PAGE_MASK;
380 } while (count != 0);
381 BUG_ON(!list_empty(list));
386 nfs_direct_release_pages(data->pagevec, result);
388 list_add(&data->pages, list);
389 while (!list_empty(list)) {
390 data = list_entry(list->next, struct nfs_read_data, pages);
391 list_del(&data->pages);
392 nfs_readdata_free(data);
394 nfs_direct_complete(dreq);
399 return result < 0 ? (ssize_t) result : -EFAULT;
402 static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
406 struct inode *inode = iocb->ki_filp->f_mapping->host;
407 struct rpc_clnt *clnt = NFS_CLIENT(inode);
408 struct nfs_direct_req *dreq;
410 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
415 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
416 if (!is_sync_kiocb(iocb))
419 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
420 rpc_clnt_sigmask(clnt, &oldset);
421 result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
423 result = nfs_direct_wait(dreq);
424 rpc_clnt_sigunmask(clnt, &oldset);
429 static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
431 list_splice_init(&dreq->rewrite_list, &dreq->list);
432 while (!list_empty(&dreq->list)) {
433 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
434 list_del(&data->pages);
435 nfs_direct_release_pages(data->pagevec, data->npages);
436 nfs_writedata_release(data);
440 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
441 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
443 struct inode *inode = dreq->inode;
445 struct nfs_write_data *data;
450 list_for_each(p, &dreq->rewrite_list) {
451 data = list_entry(p, struct nfs_write_data, pages);
458 nfs_fattr_init(&data->fattr);
459 data->res.count = data->args.count;
460 memset(&data->verf, 0, sizeof(data->verf));
463 * Reuse data->task; data->args should not have changed
464 * since the original request was sent.
466 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
467 &nfs_write_direct_ops, data);
468 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
470 data->task.tk_priority = RPC_PRIORITY_NORMAL;
471 data->task.tk_cookie = (unsigned long) inode;
474 * We're called via an RPC callback, so BKL is already held.
476 rpc_execute(&data->task);
478 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
481 (long long)NFS_FILEID(inode),
483 (unsigned long long)data->args.offset);
487 nfs_direct_write_complete(dreq, inode);
490 static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
492 struct nfs_write_data *data = calldata;
493 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
495 /* Call the NFS version-specific code */
496 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
498 if (unlikely(task->tk_status < 0)) {
499 dreq->error = task->tk_status;
500 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
502 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
503 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
504 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
507 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
508 nfs_direct_write_complete(dreq, data->inode);
511 static const struct rpc_call_ops nfs_commit_direct_ops = {
512 .rpc_call_done = nfs_direct_commit_result,
513 .rpc_release = nfs_commit_release,
516 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
518 struct nfs_write_data *data = dreq->commit_data;
520 data->inode = dreq->inode;
521 data->cred = dreq->ctx->cred;
523 data->args.fh = NFS_FH(data->inode);
524 data->args.offset = 0;
525 data->args.count = 0;
527 data->res.fattr = &data->fattr;
528 data->res.verf = &data->verf;
530 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
531 &nfs_commit_direct_ops, data);
532 NFS_PROTO(data->inode)->commit_setup(data, 0);
534 data->task.tk_priority = RPC_PRIORITY_NORMAL;
535 data->task.tk_cookie = (unsigned long)data->inode;
536 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
537 dreq->commit_data = NULL;
539 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
542 rpc_execute(&data->task);
546 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
548 int flags = dreq->flags;
552 case NFS_ODIRECT_DO_COMMIT:
553 nfs_direct_commit_schedule(dreq);
555 case NFS_ODIRECT_RESCHED_WRITES:
556 nfs_direct_write_reschedule(dreq);
559 nfs_end_data_update(inode);
560 if (dreq->commit_data != NULL)
561 nfs_commit_free(dreq->commit_data);
562 nfs_direct_free_writedata(dreq);
563 nfs_direct_complete(dreq);
567 static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
569 dreq->commit_data = nfs_commit_alloc(0);
570 if (dreq->commit_data != NULL)
571 dreq->commit_data->req = (struct nfs_page *) dreq;
574 static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
576 dreq->commit_data = NULL;
579 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
581 nfs_end_data_update(inode);
582 nfs_direct_free_writedata(dreq);
583 nfs_direct_complete(dreq);
587 static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
589 struct list_head *list;
590 struct nfs_direct_req *dreq;
591 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
593 dreq = nfs_direct_req_alloc();
599 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
601 if (unlikely(!data)) {
602 while (!list_empty(list)) {
603 data = list_entry(list->next,
604 struct nfs_write_data, pages);
605 list_del(&data->pages);
606 nfs_writedata_free(data);
608 kref_put(&dreq->kref, nfs_direct_req_release);
612 INIT_LIST_HEAD(&data->pages);
613 list_add(&data->pages, list);
615 data->req = (struct nfs_page *) dreq;
622 nfs_alloc_commit_data(dreq);
624 kref_get(&dreq->kref);
628 static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
630 struct nfs_write_data *data = calldata;
631 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
632 int status = task->tk_status;
634 if (nfs_writeback_done(task, data) != 0)
637 spin_lock(&dreq->lock);
639 if (likely(status >= 0))
640 dreq->count += data->res.count;
642 dreq->error = task->tk_status;
644 if (data->res.verf->committed != NFS_FILE_SYNC) {
645 switch (dreq->flags) {
647 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
648 dreq->flags = NFS_ODIRECT_DO_COMMIT;
650 case NFS_ODIRECT_DO_COMMIT:
651 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
652 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
653 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
658 spin_unlock(&dreq->lock);
662 * NB: Return the value of the first error return code. Subsequent
663 * errors after the first one are ignored.
665 static void nfs_direct_write_release(void *calldata)
667 struct nfs_write_data *data = calldata;
668 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
671 nfs_direct_write_complete(dreq, data->inode);
674 static const struct rpc_call_ops nfs_write_direct_ops = {
675 .rpc_call_done = nfs_direct_write_result,
676 .rpc_release = nfs_direct_write_release,
680 * For each nfs_write_data struct that was allocated on the list, dispatch
681 * an NFS WRITE operation. If get_user_pages() fails, we stop sending writes.
682 * Write length accounting is handled by nfs_direct_write_result().
683 * Otherwise, if no requests have been sent, just return an error.
685 static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
687 struct nfs_open_context *ctx = dreq->ctx;
688 struct inode *inode = ctx->dentry->d_inode;
689 struct list_head *list = &dreq->list;
690 size_t wsize = NFS_SERVER(inode)->wsize;
694 struct nfs_write_data *data;
696 pgbase = user_addr & ~PAGE_MASK;
704 BUG_ON(list_empty(list));
705 data = list_entry(list->next, struct nfs_write_data, pages);
707 data->npages = nfs_direct_count_pages(user_addr, bytes);
708 down_read(¤t->mm->mmap_sem);
709 result = get_user_pages(current, current->mm, user_addr,
710 data->npages, 0, 0, data->pagevec, NULL);
711 up_read(¤t->mm->mmap_sem);
712 if (unlikely(result < data->npages))
715 list_move_tail(&data->pages, &dreq->rewrite_list);
718 data->cred = ctx->cred;
719 data->args.fh = NFS_FH(inode);
720 data->args.context = ctx;
721 data->args.offset = pos;
722 data->args.pgbase = pgbase;
723 data->args.pages = data->pagevec;
724 data->args.count = bytes;
725 data->res.fattr = &data->fattr;
726 data->res.count = bytes;
727 data->res.verf = &data->verf;
729 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
730 &nfs_write_direct_ops, data);
731 NFS_PROTO(inode)->write_setup(data, sync);
733 data->task.tk_priority = RPC_PRIORITY_NORMAL;
734 data->task.tk_cookie = (unsigned long) inode;
737 rpc_execute(&data->task);
740 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
743 (long long)NFS_FILEID(inode),
745 (unsigned long long)data->args.offset);
751 pgbase &= ~PAGE_MASK;
754 } while (count != 0);
755 BUG_ON(!list_empty(list));
760 nfs_direct_release_pages(data->pagevec, result);
762 list_add(&data->pages, list);
763 while (!list_empty(list)) {
764 data = list_entry(list->next, struct nfs_write_data, pages);
765 list_del(&data->pages);
766 nfs_writedata_free(data);
768 nfs_direct_write_complete(dreq, inode);
773 return result < 0 ? (ssize_t) result : -EFAULT;
776 static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
780 struct inode *inode = iocb->ki_filp->f_mapping->host;
781 struct rpc_clnt *clnt = NFS_CLIENT(inode);
782 struct nfs_direct_req *dreq;
783 size_t wsize = NFS_SERVER(inode)->wsize;
786 dreq = nfs_direct_write_alloc(count, wsize);
789 if (dreq->commit_data == NULL || count < wsize)
793 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
794 if (!is_sync_kiocb(iocb))
797 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
799 nfs_begin_data_update(inode);
801 rpc_clnt_sigmask(clnt, &oldset);
802 result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
804 result = nfs_direct_wait(dreq);
805 rpc_clnt_sigunmask(clnt, &oldset);
811 * nfs_file_direct_read - file direct read operation for NFS files
812 * @iocb: target I/O control block
813 * @buf: user's buffer into which to read data
814 * @count: number of bytes to read
815 * @pos: byte offset in file where reading starts
817 * We use this function for direct reads instead of calling
818 * generic_file_aio_read() in order to avoid gfar's check to see if
819 * the request starts before the end of the file. For that check
820 * to work, we must generate a GETATTR before each direct read, and
821 * even then there is a window between the GETATTR and the subsequent
822 * READ where the file size could change. Our preference is simply
823 * to do all reads the application wants, and the server will take
824 * care of managing the end of file boundary.
826 * This function also eliminates unnecessarily updating the file's
827 * atime locally, as the NFS server sets the file's atime, and this
828 * client must read the updated atime from the server back into its
831 ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
833 ssize_t retval = -EINVAL;
834 struct file *file = iocb->ki_filp;
835 struct address_space *mapping = file->f_mapping;
837 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
838 file->f_dentry->d_parent->d_name.name,
839 file->f_dentry->d_name.name,
840 (unsigned long) count, (long long) pos);
845 if (!access_ok(VERIFY_WRITE, buf, count))
851 retval = nfs_sync_mapping(mapping);
855 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
857 iocb->ki_pos = pos + retval;
864 * nfs_file_direct_write - file direct write operation for NFS files
865 * @iocb: target I/O control block
866 * @buf: user's buffer from which to write data
867 * @count: number of bytes to write
868 * @pos: byte offset in file where writing starts
870 * We use this function for direct writes instead of calling
871 * generic_file_aio_write() in order to avoid taking the inode
872 * semaphore and updating the i_size. The NFS server will set
873 * the new i_size and this client must read the updated size
874 * back into its cache. We let the server do generic write
875 * parameter checking and report problems.
877 * We also avoid an unnecessary invocation of generic_osync_inode(),
878 * as it is fairly meaningless to sync the metadata of an NFS file.
880 * We eliminate local atime updates, see direct read above.
882 * We avoid unnecessary page cache invalidations for normal cached
883 * readers of this file.
885 * Note that O_APPEND is not supported for NFS direct writes, as there
886 * is no atomic O_APPEND write facility in the NFS protocol.
888 ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
891 struct file *file = iocb->ki_filp;
892 struct address_space *mapping = file->f_mapping;
894 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
895 file->f_dentry->d_parent->d_name.name,
896 file->f_dentry->d_name.name,
897 (unsigned long) count, (long long) pos);
899 retval = generic_write_checks(file, &pos, &count, 0);
904 if ((ssize_t) count < 0)
911 if (!access_ok(VERIFY_READ, buf, count))
914 retval = nfs_sync_mapping(mapping);
918 retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
921 * XXX: nfs_end_data_update() already ensures this file's
922 * cached data is subsequently invalidated. Do we really
923 * need to call invalidate_inode_pages2() again here?
925 * For aio writes, this invalidation will almost certainly
926 * occur before the writes complete. Kind of racey.
928 if (mapping->nrpages)
929 invalidate_inode_pages2(mapping);
932 iocb->ki_pos = pos + retval;
939 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
942 int __init nfs_init_directcache(void)
944 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
945 sizeof(struct nfs_direct_req),
946 0, (SLAB_RECLAIM_ACCOUNT|
949 if (nfs_direct_cachep == NULL)
956 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
959 void __exit nfs_destroy_directcache(void)
961 if (kmem_cache_destroy(nfs_direct_cachep))
962 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");