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 unsigned long user_addr; /* location of user's buffer */
77 size_t user_count; /* total bytes to move */
78 loff_t pos; /* starting offset in file */
79 struct page ** pages; /* pages in our buffer */
80 unsigned int npages; /* count of pages */
82 /* completion state */
83 atomic_t io_count; /* i/os we're waiting for */
84 spinlock_t lock; /* protect completion state */
85 ssize_t count, /* bytes actually processed */
86 error; /* any reported error */
87 struct completion completion; /* wait for i/o completion */
90 struct nfs_write_data * commit_data; /* special write_data for commits */
92 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
93 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
94 struct nfs_writeverf verf; /* unstable write verifier */
97 static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync);
98 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
100 static inline void get_dreq(struct nfs_direct_req *dreq)
102 atomic_inc(&dreq->io_count);
105 static inline int put_dreq(struct nfs_direct_req *dreq)
107 return atomic_dec_and_test(&dreq->io_count);
111 * nfs_direct_IO - NFS address space operation for direct I/O
112 * @rw: direction (read or write)
113 * @iocb: target I/O control block
114 * @iov: array of vectors that define I/O buffer
115 * @pos: offset in file to begin the operation
116 * @nr_segs: size of iovec array
118 * The presence of this routine in the address space ops vector means
119 * the NFS client supports direct I/O. However, we shunt off direct
120 * read and write requests before the VFS gets them, so this method
121 * should never be called.
123 ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
125 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
126 iocb->ki_filp->f_dentry->d_name.name,
127 (long long) pos, nr_segs);
132 static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
135 for (i = 0; i < npages; i++) {
136 struct page *page = pages[i];
137 if (do_dirty && !PageCompound(page))
138 set_page_dirty_lock(page);
139 page_cache_release(page);
144 static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
146 int result = -ENOMEM;
147 unsigned long page_count;
150 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
151 page_count -= user_addr >> PAGE_SHIFT;
153 array_size = (page_count * sizeof(struct page *));
154 *pages = kmalloc(array_size, GFP_KERNEL);
156 down_read(¤t->mm->mmap_sem);
157 result = get_user_pages(current, current->mm, user_addr,
158 page_count, (rw == READ), 0,
160 up_read(¤t->mm->mmap_sem);
161 if (result != page_count) {
163 * If we got fewer pages than expected from
164 * get_user_pages(), the user buffer runs off the
165 * end of a mapping; return EFAULT.
168 nfs_free_user_pages(*pages, result, 0);
178 static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
180 struct nfs_direct_req *dreq;
182 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
186 kref_init(&dreq->kref);
187 init_completion(&dreq->completion);
188 INIT_LIST_HEAD(&dreq->list);
189 INIT_LIST_HEAD(&dreq->rewrite_list);
192 spin_lock_init(&dreq->lock);
193 atomic_set(&dreq->io_count, 0);
201 static void nfs_direct_req_release(struct kref *kref)
203 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
205 if (dreq->ctx != NULL)
206 put_nfs_open_context(dreq->ctx);
207 kmem_cache_free(nfs_direct_cachep, dreq);
211 * Collects and returns the final error value/byte-count.
213 static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
215 ssize_t result = -EIOCBQUEUED;
217 /* Async requests don't wait here */
221 result = wait_for_completion_interruptible(&dreq->completion);
224 result = dreq->error;
226 result = dreq->count;
229 kref_put(&dreq->kref, nfs_direct_req_release);
230 return (ssize_t) result;
234 * We must hold a reference to all the pages in this direct read request
235 * until the RPCs complete. This could be long *after* we are woken up in
236 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
238 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
239 * can't trust the iocb is still valid here if this is a synchronous
240 * request. If the waiter is woken prematurely, the iocb is long gone.
242 static void nfs_direct_complete(struct nfs_direct_req *dreq)
244 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
247 long res = (long) dreq->error;
249 res = (long) dreq->count;
250 aio_complete(dreq->iocb, res, 0);
252 complete_all(&dreq->completion);
254 kref_put(&dreq->kref, nfs_direct_req_release);
258 * Note we also set the number of requests we have in the dreq when we are
259 * done. This prevents races with I/O completion so we will always wait
260 * until all requests have been dispatched and completed.
262 static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
264 struct list_head *list;
265 struct nfs_direct_req *dreq;
266 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
268 dreq = nfs_direct_req_alloc();
274 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
276 if (unlikely(!data)) {
277 while (!list_empty(list)) {
278 data = list_entry(list->next,
279 struct nfs_read_data, pages);
280 list_del(&data->pages);
281 nfs_readdata_free(data);
283 kref_put(&dreq->kref, nfs_direct_req_release);
287 INIT_LIST_HEAD(&data->pages);
288 list_add(&data->pages, list);
290 data->req = (struct nfs_page *) dreq;
296 kref_get(&dreq->kref);
300 static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
302 struct nfs_read_data *data = calldata;
303 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
305 if (nfs_readpage_result(task, data) != 0)
308 spin_lock(&dreq->lock);
310 if (likely(task->tk_status >= 0))
311 dreq->count += data->res.count;
313 dreq->error = task->tk_status;
315 spin_unlock(&dreq->lock);
318 nfs_direct_complete(dreq);
321 static const struct rpc_call_ops nfs_read_direct_ops = {
322 .rpc_call_done = nfs_direct_read_result,
323 .rpc_release = nfs_readdata_release,
327 * For each nfs_read_data struct that was allocated on the list, dispatch
328 * an NFS READ operation
330 static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
332 struct nfs_open_context *ctx = dreq->ctx;
333 struct inode *inode = ctx->dentry->d_inode;
334 struct list_head *list = &dreq->list;
335 struct page **pages = dreq->pages;
336 size_t count = dreq->user_count;
337 loff_t pos = dreq->pos;
338 size_t rsize = NFS_SERVER(inode)->rsize;
339 unsigned int curpage, pgbase;
342 pgbase = dreq->user_addr & ~PAGE_MASK;
344 struct nfs_read_data *data;
351 BUG_ON(list_empty(list));
352 data = list_entry(list->next, struct nfs_read_data, pages);
353 list_del_init(&data->pages);
356 data->cred = ctx->cred;
357 data->args.fh = NFS_FH(inode);
358 data->args.context = ctx;
359 data->args.offset = pos;
360 data->args.pgbase = pgbase;
361 data->args.pages = &pages[curpage];
362 data->args.count = bytes;
363 data->res.fattr = &data->fattr;
365 data->res.count = bytes;
367 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
368 &nfs_read_direct_ops, data);
369 NFS_PROTO(inode)->read_setup(data);
371 data->task.tk_cookie = (unsigned long) inode;
374 rpc_execute(&data->task);
377 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
380 (long long)NFS_FILEID(inode),
382 (unsigned long long)data->args.offset);
386 curpage += pgbase >> PAGE_SHIFT;
387 pgbase &= ~PAGE_MASK;
390 } while (count != 0);
391 BUG_ON(!list_empty(list));
394 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)
398 struct inode *inode = iocb->ki_filp->f_mapping->host;
399 struct rpc_clnt *clnt = NFS_CLIENT(inode);
400 struct nfs_direct_req *dreq;
402 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
406 dreq->user_addr = user_addr;
407 dreq->user_count = count;
410 dreq->npages = nr_pages;
412 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
413 if (!is_sync_kiocb(iocb))
416 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
417 rpc_clnt_sigmask(clnt, &oldset);
418 nfs_direct_read_schedule(dreq);
419 result = nfs_direct_wait(dreq);
420 rpc_clnt_sigunmask(clnt, &oldset);
425 static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
427 list_splice_init(&dreq->rewrite_list, &dreq->list);
428 while (!list_empty(&dreq->list)) {
429 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
430 list_del(&data->pages);
431 nfs_writedata_release(data);
435 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
436 static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
438 struct list_head *pos;
440 list_splice_init(&dreq->rewrite_list, &dreq->list);
441 list_for_each(pos, &dreq->list)
445 nfs_direct_write_schedule(dreq, FLUSH_STABLE);
448 static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
450 struct nfs_write_data *data = calldata;
451 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
453 /* Call the NFS version-specific code */
454 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
456 if (unlikely(task->tk_status < 0)) {
457 dreq->error = task->tk_status;
458 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
460 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
461 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
462 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
465 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
466 nfs_direct_write_complete(dreq, data->inode);
469 static const struct rpc_call_ops nfs_commit_direct_ops = {
470 .rpc_call_done = nfs_direct_commit_result,
471 .rpc_release = nfs_commit_release,
474 static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
476 struct nfs_write_data *data = dreq->commit_data;
478 data->inode = dreq->inode;
479 data->cred = dreq->ctx->cred;
481 data->args.fh = NFS_FH(data->inode);
482 data->args.offset = dreq->pos;
483 data->args.count = dreq->user_count;
485 data->res.fattr = &data->fattr;
486 data->res.verf = &data->verf;
488 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
489 &nfs_commit_direct_ops, data);
490 NFS_PROTO(data->inode)->commit_setup(data, 0);
492 data->task.tk_priority = RPC_PRIORITY_NORMAL;
493 data->task.tk_cookie = (unsigned long)data->inode;
494 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
495 dreq->commit_data = NULL;
497 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
500 rpc_execute(&data->task);
504 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
506 int flags = dreq->flags;
510 case NFS_ODIRECT_DO_COMMIT:
511 nfs_direct_commit_schedule(dreq);
513 case NFS_ODIRECT_RESCHED_WRITES:
514 nfs_direct_write_reschedule(dreq);
517 nfs_end_data_update(inode);
518 if (dreq->commit_data != NULL)
519 nfs_commit_free(dreq->commit_data);
520 nfs_direct_free_writedata(dreq);
521 nfs_direct_complete(dreq);
525 static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
527 dreq->commit_data = nfs_commit_alloc(0);
528 if (dreq->commit_data != NULL)
529 dreq->commit_data->req = (struct nfs_page *) dreq;
532 static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
534 dreq->commit_data = NULL;
537 static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
539 nfs_end_data_update(inode);
540 nfs_direct_free_writedata(dreq);
541 nfs_direct_complete(dreq);
545 static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
547 struct list_head *list;
548 struct nfs_direct_req *dreq;
549 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
551 dreq = nfs_direct_req_alloc();
557 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
559 if (unlikely(!data)) {
560 while (!list_empty(list)) {
561 data = list_entry(list->next,
562 struct nfs_write_data, pages);
563 list_del(&data->pages);
564 nfs_writedata_free(data);
566 kref_put(&dreq->kref, nfs_direct_req_release);
570 INIT_LIST_HEAD(&data->pages);
571 list_add(&data->pages, list);
573 data->req = (struct nfs_page *) dreq;
580 nfs_alloc_commit_data(dreq);
582 kref_get(&dreq->kref);
586 static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
588 struct nfs_write_data *data = calldata;
589 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
590 int status = task->tk_status;
592 if (nfs_writeback_done(task, data) != 0)
595 spin_lock(&dreq->lock);
597 if (likely(status >= 0))
598 dreq->count += data->res.count;
600 dreq->error = task->tk_status;
602 if (data->res.verf->committed != NFS_FILE_SYNC) {
603 switch (dreq->flags) {
605 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
606 dreq->flags = NFS_ODIRECT_DO_COMMIT;
608 case NFS_ODIRECT_DO_COMMIT:
609 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
610 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
611 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
615 /* In case we have to resend */
616 data->args.stable = NFS_FILE_SYNC;
618 spin_unlock(&dreq->lock);
622 * NB: Return the value of the first error return code. Subsequent
623 * errors after the first one are ignored.
625 static void nfs_direct_write_release(void *calldata)
627 struct nfs_write_data *data = calldata;
628 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
631 nfs_direct_write_complete(dreq, data->inode);
634 static const struct rpc_call_ops nfs_write_direct_ops = {
635 .rpc_call_done = nfs_direct_write_result,
636 .rpc_release = nfs_direct_write_release,
640 * For each nfs_write_data struct that was allocated on the list, dispatch
641 * an NFS WRITE operation
643 static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
645 struct nfs_open_context *ctx = dreq->ctx;
646 struct inode *inode = ctx->dentry->d_inode;
647 struct list_head *list = &dreq->list;
648 struct page **pages = dreq->pages;
649 size_t count = dreq->user_count;
650 loff_t pos = dreq->pos;
651 size_t wsize = NFS_SERVER(inode)->wsize;
652 unsigned int curpage, pgbase;
655 pgbase = dreq->user_addr & ~PAGE_MASK;
657 struct nfs_write_data *data;
664 BUG_ON(list_empty(list));
665 data = list_entry(list->next, struct nfs_write_data, pages);
666 list_move_tail(&data->pages, &dreq->rewrite_list);
669 data->cred = ctx->cred;
670 data->args.fh = NFS_FH(inode);
671 data->args.context = ctx;
672 data->args.offset = pos;
673 data->args.pgbase = pgbase;
674 data->args.pages = &pages[curpage];
675 data->args.count = bytes;
676 data->res.fattr = &data->fattr;
677 data->res.count = bytes;
678 data->res.verf = &data->verf;
680 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
681 &nfs_write_direct_ops, data);
682 NFS_PROTO(inode)->write_setup(data, sync);
684 data->task.tk_priority = RPC_PRIORITY_NORMAL;
685 data->task.tk_cookie = (unsigned long) inode;
688 rpc_execute(&data->task);
691 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
694 (long long)NFS_FILEID(inode),
696 (unsigned long long)data->args.offset);
700 curpage += pgbase >> PAGE_SHIFT;
701 pgbase &= ~PAGE_MASK;
704 } while (count != 0);
705 BUG_ON(!list_empty(list));
708 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)
712 struct inode *inode = iocb->ki_filp->f_mapping->host;
713 struct rpc_clnt *clnt = NFS_CLIENT(inode);
714 struct nfs_direct_req *dreq;
715 size_t wsize = NFS_SERVER(inode)->wsize;
718 dreq = nfs_direct_write_alloc(count, wsize);
721 if (dreq->commit_data == NULL || count < wsize)
724 dreq->user_addr = user_addr;
725 dreq->user_count = count;
728 dreq->npages = nr_pages;
730 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
731 if (!is_sync_kiocb(iocb))
734 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
736 nfs_begin_data_update(inode);
738 rpc_clnt_sigmask(clnt, &oldset);
739 nfs_direct_write_schedule(dreq, sync);
740 result = nfs_direct_wait(dreq);
741 rpc_clnt_sigunmask(clnt, &oldset);
747 * nfs_file_direct_read - file direct read operation for NFS files
748 * @iocb: target I/O control block
749 * @buf: user's buffer into which to read data
750 * @count: number of bytes to read
751 * @pos: byte offset in file where reading starts
753 * We use this function for direct reads instead of calling
754 * generic_file_aio_read() in order to avoid gfar's check to see if
755 * the request starts before the end of the file. For that check
756 * to work, we must generate a GETATTR before each direct read, and
757 * even then there is a window between the GETATTR and the subsequent
758 * READ where the file size could change. Our preference is simply
759 * to do all reads the application wants, and the server will take
760 * care of managing the end of file boundary.
762 * This function also eliminates unnecessarily updating the file's
763 * atime locally, as the NFS server sets the file's atime, and this
764 * client must read the updated atime from the server back into its
767 ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
769 ssize_t retval = -EINVAL;
772 struct file *file = iocb->ki_filp;
773 struct address_space *mapping = file->f_mapping;
775 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
776 file->f_dentry->d_parent->d_name.name,
777 file->f_dentry->d_name.name,
778 (unsigned long) count, (long long) pos);
783 if (!access_ok(VERIFY_WRITE, buf, count))
789 retval = nfs_sync_mapping(mapping);
793 retval = nfs_get_user_pages(READ, (unsigned long) buf,
799 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
802 iocb->ki_pos = pos + retval;
809 * nfs_file_direct_write - file direct write operation for NFS files
810 * @iocb: target I/O control block
811 * @buf: user's buffer from which to write data
812 * @count: number of bytes to write
813 * @pos: byte offset in file where writing starts
815 * We use this function for direct writes instead of calling
816 * generic_file_aio_write() in order to avoid taking the inode
817 * semaphore and updating the i_size. The NFS server will set
818 * the new i_size and this client must read the updated size
819 * back into its cache. We let the server do generic write
820 * parameter checking and report problems.
822 * We also avoid an unnecessary invocation of generic_osync_inode(),
823 * as it is fairly meaningless to sync the metadata of an NFS file.
825 * We eliminate local atime updates, see direct read above.
827 * We avoid unnecessary page cache invalidations for normal cached
828 * readers of this file.
830 * Note that O_APPEND is not supported for NFS direct writes, as there
831 * is no atomic O_APPEND write facility in the NFS protocol.
833 ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
838 struct file *file = iocb->ki_filp;
839 struct address_space *mapping = file->f_mapping;
841 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
842 file->f_dentry->d_parent->d_name.name,
843 file->f_dentry->d_name.name,
844 (unsigned long) count, (long long) pos);
846 retval = generic_write_checks(file, &pos, &count, 0);
851 if ((ssize_t) count < 0)
858 if (!access_ok(VERIFY_READ, buf, count))
861 retval = nfs_sync_mapping(mapping);
865 retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
871 retval = nfs_direct_write(iocb, (unsigned long) buf, count,
872 pos, pages, page_count);
875 * XXX: nfs_end_data_update() already ensures this file's
876 * cached data is subsequently invalidated. Do we really
877 * need to call invalidate_inode_pages2() again here?
879 * For aio writes, this invalidation will almost certainly
880 * occur before the writes complete. Kind of racey.
882 if (mapping->nrpages)
883 invalidate_inode_pages2(mapping);
886 iocb->ki_pos = pos + retval;
893 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
896 int __init nfs_init_directcache(void)
898 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
899 sizeof(struct nfs_direct_req),
900 0, (SLAB_RECLAIM_ACCOUNT|
903 if (nfs_direct_cachep == NULL)
910 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
913 void __exit nfs_destroy_directcache(void)
915 if (kmem_cache_destroy(nfs_direct_cachep))
916 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");