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/errno.h>
42 #include <linux/sched.h>
43 #include <linux/kernel.h>
44 #include <linux/file.h>
45 #include <linux/pagemap.h>
46 #include <linux/kref.h>
47 #include <linux/slab.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>
60 #define NFSDBG_FACILITY NFSDBG_VFS
62 static struct kmem_cache
*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 nfs_open_context
*ctx
; /* file open context info */
72 struct kiocb
* iocb
; /* controlling i/o request */
73 struct inode
* inode
; /* target file of i/o */
75 /* completion state */
76 atomic_t io_count
; /* i/os we're waiting for */
77 spinlock_t lock
; /* protect completion state */
78 ssize_t count
, /* bytes actually processed */
79 error
; /* any reported error */
80 struct completion completion
; /* wait for i/o completion */
83 struct list_head rewrite_list
; /* saved nfs_write_data structs */
84 struct nfs_write_data
* commit_data
; /* special write_data for commits */
86 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
87 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
88 struct nfs_writeverf verf
; /* unstable write verifier */
91 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
);
92 static const struct rpc_call_ops nfs_write_direct_ops
;
94 static inline void get_dreq(struct nfs_direct_req
*dreq
)
96 atomic_inc(&dreq
->io_count
);
99 static inline int put_dreq(struct nfs_direct_req
*dreq
)
101 return atomic_dec_and_test(&dreq
->io_count
);
105 * nfs_direct_IO - NFS address space operation for direct I/O
106 * @rw: direction (read or write)
107 * @iocb: target I/O control block
108 * @iov: array of vectors that define I/O buffer
109 * @pos: offset in file to begin the operation
110 * @nr_segs: size of iovec array
112 * The presence of this routine in the address space ops vector means
113 * the NFS client supports direct I/O. However, we shunt off direct
114 * read and write requests before the VFS gets them, so this method
115 * should never be called.
117 ssize_t
nfs_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
, loff_t pos
, unsigned long nr_segs
)
119 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
120 iocb
->ki_filp
->f_path
.dentry
->d_name
.name
,
121 (long long) pos
, nr_segs
);
126 static void nfs_direct_dirty_pages(struct page
**pages
, unsigned int pgbase
, size_t count
)
133 pages
+= (pgbase
>> PAGE_SHIFT
);
134 npages
= (count
+ (pgbase
& ~PAGE_MASK
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
135 for (i
= 0; i
< npages
; i
++) {
136 struct page
*page
= pages
[i
];
137 if (!PageCompound(page
))
138 set_page_dirty(page
);
142 static void nfs_direct_release_pages(struct page
**pages
, unsigned int npages
)
145 for (i
= 0; i
< npages
; i
++)
146 page_cache_release(pages
[i
]);
149 static inline struct nfs_direct_req
*nfs_direct_req_alloc(void)
151 struct nfs_direct_req
*dreq
;
153 dreq
= kmem_cache_alloc(nfs_direct_cachep
, GFP_KERNEL
);
157 kref_init(&dreq
->kref
);
158 kref_get(&dreq
->kref
);
159 init_completion(&dreq
->completion
);
160 INIT_LIST_HEAD(&dreq
->rewrite_list
);
163 spin_lock_init(&dreq
->lock
);
164 atomic_set(&dreq
->io_count
, 0);
172 static void nfs_direct_req_free(struct kref
*kref
)
174 struct nfs_direct_req
*dreq
= container_of(kref
, struct nfs_direct_req
, kref
);
176 if (dreq
->ctx
!= NULL
)
177 put_nfs_open_context(dreq
->ctx
);
178 kmem_cache_free(nfs_direct_cachep
, dreq
);
181 static void nfs_direct_req_release(struct nfs_direct_req
*dreq
)
183 kref_put(&dreq
->kref
, nfs_direct_req_free
);
187 * Collects and returns the final error value/byte-count.
189 static ssize_t
nfs_direct_wait(struct nfs_direct_req
*dreq
)
191 ssize_t result
= -EIOCBQUEUED
;
193 /* Async requests don't wait here */
197 result
= wait_for_completion_killable(&dreq
->completion
);
200 result
= dreq
->error
;
202 result
= dreq
->count
;
205 return (ssize_t
) result
;
209 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
210 * the iocb is still valid here if this is a synchronous request.
212 static void nfs_direct_complete(struct nfs_direct_req
*dreq
)
215 long res
= (long) dreq
->error
;
217 res
= (long) dreq
->count
;
218 aio_complete(dreq
->iocb
, res
, 0);
220 complete_all(&dreq
->completion
);
222 nfs_direct_req_release(dreq
);
226 * We must hold a reference to all the pages in this direct read request
227 * until the RPCs complete. This could be long *after* we are woken up in
228 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
230 static void nfs_direct_read_result(struct rpc_task
*task
, void *calldata
)
232 struct nfs_read_data
*data
= calldata
;
234 nfs_readpage_result(task
, data
);
237 static void nfs_direct_read_release(void *calldata
)
240 struct nfs_read_data
*data
= calldata
;
241 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
242 int status
= data
->task
.tk_status
;
244 spin_lock(&dreq
->lock
);
245 if (unlikely(status
< 0)) {
246 dreq
->error
= status
;
247 spin_unlock(&dreq
->lock
);
249 dreq
->count
+= data
->res
.count
;
250 spin_unlock(&dreq
->lock
);
251 nfs_direct_dirty_pages(data
->pagevec
,
255 nfs_direct_release_pages(data
->pagevec
, data
->npages
);
258 nfs_direct_complete(dreq
);
259 nfs_readdata_free(data
);
262 static const struct rpc_call_ops nfs_read_direct_ops
= {
263 #if defined(CONFIG_NFS_V4_1)
264 .rpc_call_prepare
= nfs_read_prepare
,
265 #endif /* CONFIG_NFS_V4_1 */
266 .rpc_call_done
= nfs_direct_read_result
,
267 .rpc_release
= nfs_direct_read_release
,
271 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
272 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
273 * bail and stop sending more reads. Read length accounting is
274 * handled automatically by nfs_direct_read_result(). Otherwise, if
275 * no requests have been sent, just return an error.
277 static ssize_t
nfs_direct_read_schedule_segment(struct nfs_direct_req
*dreq
,
278 const struct iovec
*iov
,
281 struct nfs_open_context
*ctx
= dreq
->ctx
;
282 struct inode
*inode
= ctx
->path
.dentry
->d_inode
;
283 unsigned long user_addr
= (unsigned long)iov
->iov_base
;
284 size_t count
= iov
->iov_len
;
285 size_t rsize
= NFS_SERVER(inode
)->rsize
;
286 struct rpc_task
*task
;
287 struct rpc_message msg
= {
288 .rpc_cred
= ctx
->cred
,
290 struct rpc_task_setup task_setup_data
= {
291 .rpc_client
= NFS_CLIENT(inode
),
293 .callback_ops
= &nfs_read_direct_ops
,
294 .workqueue
= nfsiod_workqueue
,
295 .flags
= RPC_TASK_ASYNC
,
302 struct nfs_read_data
*data
;
305 pgbase
= user_addr
& ~PAGE_MASK
;
306 bytes
= min(rsize
,count
);
309 data
= nfs_readdata_alloc(nfs_page_array_len(pgbase
, bytes
));
313 down_read(¤t
->mm
->mmap_sem
);
314 result
= get_user_pages(current
, current
->mm
, user_addr
,
315 data
->npages
, 1, 0, data
->pagevec
, NULL
);
316 up_read(¤t
->mm
->mmap_sem
);
318 nfs_readdata_free(data
);
321 if ((unsigned)result
< data
->npages
) {
322 bytes
= result
* PAGE_SIZE
;
323 if (bytes
<= pgbase
) {
324 nfs_direct_release_pages(data
->pagevec
, result
);
325 nfs_readdata_free(data
);
329 data
->npages
= result
;
334 data
->req
= (struct nfs_page
*) dreq
;
336 data
->cred
= msg
.rpc_cred
;
337 data
->args
.fh
= NFS_FH(inode
);
338 data
->args
.context
= ctx
;
339 data
->args
.offset
= pos
;
340 data
->args
.pgbase
= pgbase
;
341 data
->args
.pages
= data
->pagevec
;
342 data
->args
.count
= bytes
;
343 data
->res
.fattr
= &data
->fattr
;
345 data
->res
.count
= bytes
;
346 nfs_fattr_init(&data
->fattr
);
347 msg
.rpc_argp
= &data
->args
;
348 msg
.rpc_resp
= &data
->res
;
350 task_setup_data
.task
= &data
->task
;
351 task_setup_data
.callback_data
= data
;
352 NFS_PROTO(inode
)->read_setup(data
, &msg
);
354 task
= rpc_run_task(&task_setup_data
);
359 dprintk("NFS: %5u initiated direct read call "
360 "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
363 (long long)NFS_FILEID(inode
),
365 (unsigned long long)data
->args
.offset
);
370 /* FIXME: Remove this unnecessary math from final patch */
372 pgbase
&= ~PAGE_MASK
;
373 BUG_ON(pgbase
!= (user_addr
& ~PAGE_MASK
));
376 } while (count
!= 0);
380 return result
< 0 ? (ssize_t
) result
: -EFAULT
;
383 static ssize_t
nfs_direct_read_schedule_iovec(struct nfs_direct_req
*dreq
,
384 const struct iovec
*iov
,
385 unsigned long nr_segs
,
388 ssize_t result
= -EINVAL
;
389 size_t requested_bytes
= 0;
394 for (seg
= 0; seg
< nr_segs
; seg
++) {
395 const struct iovec
*vec
= &iov
[seg
];
396 result
= nfs_direct_read_schedule_segment(dreq
, vec
, pos
);
399 requested_bytes
+= result
;
400 if ((size_t)result
< vec
->iov_len
)
406 nfs_direct_complete(dreq
);
408 if (requested_bytes
!= 0)
416 static ssize_t
nfs_direct_read(struct kiocb
*iocb
, const struct iovec
*iov
,
417 unsigned long nr_segs
, loff_t pos
)
420 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
421 struct nfs_direct_req
*dreq
;
423 dreq
= nfs_direct_req_alloc();
428 dreq
->ctx
= get_nfs_open_context(nfs_file_open_context(iocb
->ki_filp
));
429 if (!is_sync_kiocb(iocb
))
432 result
= nfs_direct_read_schedule_iovec(dreq
, iov
, nr_segs
, pos
);
434 result
= nfs_direct_wait(dreq
);
435 nfs_direct_req_release(dreq
);
440 static void nfs_direct_free_writedata(struct nfs_direct_req
*dreq
)
442 while (!list_empty(&dreq
->rewrite_list
)) {
443 struct nfs_write_data
*data
= list_entry(dreq
->rewrite_list
.next
, struct nfs_write_data
, pages
);
444 list_del(&data
->pages
);
445 nfs_direct_release_pages(data
->pagevec
, data
->npages
);
446 nfs_writedata_free(data
);
450 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
451 static void nfs_direct_write_reschedule(struct nfs_direct_req
*dreq
)
453 struct inode
*inode
= dreq
->inode
;
455 struct nfs_write_data
*data
;
456 struct rpc_task
*task
;
457 struct rpc_message msg
= {
458 .rpc_cred
= dreq
->ctx
->cred
,
460 struct rpc_task_setup task_setup_data
= {
461 .rpc_client
= NFS_CLIENT(inode
),
463 .callback_ops
= &nfs_write_direct_ops
,
464 .workqueue
= nfsiod_workqueue
,
465 .flags
= RPC_TASK_ASYNC
,
471 list_for_each(p
, &dreq
->rewrite_list
) {
472 data
= list_entry(p
, struct nfs_write_data
, pages
);
476 /* Use stable writes */
477 data
->args
.stable
= NFS_FILE_SYNC
;
482 nfs_fattr_init(&data
->fattr
);
483 data
->res
.count
= data
->args
.count
;
484 memset(&data
->verf
, 0, sizeof(data
->verf
));
487 * Reuse data->task; data->args should not have changed
488 * since the original request was sent.
490 task_setup_data
.task
= &data
->task
;
491 task_setup_data
.callback_data
= data
;
492 msg
.rpc_argp
= &data
->args
;
493 msg
.rpc_resp
= &data
->res
;
494 NFS_PROTO(inode
)->write_setup(data
, &msg
);
497 * We're called via an RPC callback, so BKL is already held.
499 task
= rpc_run_task(&task_setup_data
);
503 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
506 (long long)NFS_FILEID(inode
),
508 (unsigned long long)data
->args
.offset
);
512 nfs_direct_write_complete(dreq
, inode
);
515 static void nfs_direct_commit_result(struct rpc_task
*task
, void *calldata
)
517 struct nfs_write_data
*data
= calldata
;
519 /* Call the NFS version-specific code */
520 NFS_PROTO(data
->inode
)->commit_done(task
, data
);
523 static void nfs_direct_commit_release(void *calldata
)
525 struct nfs_write_data
*data
= calldata
;
526 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
527 int status
= data
->task
.tk_status
;
530 dprintk("NFS: %5u commit failed with error %d.\n",
531 data
->task
.tk_pid
, status
);
532 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
533 } else if (memcmp(&dreq
->verf
, &data
->verf
, sizeof(data
->verf
))) {
534 dprintk("NFS: %5u commit verify failed\n", data
->task
.tk_pid
);
535 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
538 dprintk("NFS: %5u commit returned %d\n", data
->task
.tk_pid
, status
);
539 nfs_direct_write_complete(dreq
, data
->inode
);
540 nfs_commit_free(data
);
543 static const struct rpc_call_ops nfs_commit_direct_ops
= {
544 #if defined(CONFIG_NFS_V4_1)
545 .rpc_call_prepare
= nfs_write_prepare
,
546 #endif /* CONFIG_NFS_V4_1 */
547 .rpc_call_done
= nfs_direct_commit_result
,
548 .rpc_release
= nfs_direct_commit_release
,
551 static void nfs_direct_commit_schedule(struct nfs_direct_req
*dreq
)
553 struct nfs_write_data
*data
= dreq
->commit_data
;
554 struct rpc_task
*task
;
555 struct rpc_message msg
= {
556 .rpc_argp
= &data
->args
,
557 .rpc_resp
= &data
->res
,
558 .rpc_cred
= dreq
->ctx
->cred
,
560 struct rpc_task_setup task_setup_data
= {
562 .rpc_client
= NFS_CLIENT(dreq
->inode
),
564 .callback_ops
= &nfs_commit_direct_ops
,
565 .callback_data
= data
,
566 .workqueue
= nfsiod_workqueue
,
567 .flags
= RPC_TASK_ASYNC
,
570 data
->inode
= dreq
->inode
;
571 data
->cred
= msg
.rpc_cred
;
573 data
->args
.fh
= NFS_FH(data
->inode
);
574 data
->args
.offset
= 0;
575 data
->args
.count
= 0;
576 data
->args
.context
= dreq
->ctx
;
578 data
->res
.fattr
= &data
->fattr
;
579 data
->res
.verf
= &data
->verf
;
580 nfs_fattr_init(&data
->fattr
);
582 NFS_PROTO(data
->inode
)->commit_setup(data
, &msg
);
584 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
585 dreq
->commit_data
= NULL
;
587 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
589 task
= rpc_run_task(&task_setup_data
);
594 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
596 int flags
= dreq
->flags
;
600 case NFS_ODIRECT_DO_COMMIT
:
601 nfs_direct_commit_schedule(dreq
);
603 case NFS_ODIRECT_RESCHED_WRITES
:
604 nfs_direct_write_reschedule(dreq
);
607 if (dreq
->commit_data
!= NULL
)
608 nfs_commit_free(dreq
->commit_data
);
609 nfs_direct_free_writedata(dreq
);
610 nfs_zap_mapping(inode
, inode
->i_mapping
);
611 nfs_direct_complete(dreq
);
615 static void nfs_alloc_commit_data(struct nfs_direct_req
*dreq
)
617 dreq
->commit_data
= nfs_commitdata_alloc();
618 if (dreq
->commit_data
!= NULL
)
619 dreq
->commit_data
->req
= (struct nfs_page
*) dreq
;
622 static inline void nfs_alloc_commit_data(struct nfs_direct_req
*dreq
)
624 dreq
->commit_data
= NULL
;
627 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
629 nfs_direct_free_writedata(dreq
);
630 nfs_zap_mapping(inode
, inode
->i_mapping
);
631 nfs_direct_complete(dreq
);
635 static void nfs_direct_write_result(struct rpc_task
*task
, void *calldata
)
637 struct nfs_write_data
*data
= calldata
;
639 if (nfs_writeback_done(task
, data
) != 0)
644 * NB: Return the value of the first error return code. Subsequent
645 * errors after the first one are ignored.
647 static void nfs_direct_write_release(void *calldata
)
649 struct nfs_write_data
*data
= calldata
;
650 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
651 int status
= data
->task
.tk_status
;
653 spin_lock(&dreq
->lock
);
655 if (unlikely(status
< 0)) {
656 /* An error has occurred, so we should not commit */
658 dreq
->error
= status
;
660 if (unlikely(dreq
->error
!= 0))
663 dreq
->count
+= data
->res
.count
;
665 if (data
->res
.verf
->committed
!= NFS_FILE_SYNC
) {
666 switch (dreq
->flags
) {
668 memcpy(&dreq
->verf
, &data
->verf
, sizeof(dreq
->verf
));
669 dreq
->flags
= NFS_ODIRECT_DO_COMMIT
;
671 case NFS_ODIRECT_DO_COMMIT
:
672 if (memcmp(&dreq
->verf
, &data
->verf
, sizeof(dreq
->verf
))) {
673 dprintk("NFS: %5u write verify failed\n", data
->task
.tk_pid
);
674 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
679 spin_unlock(&dreq
->lock
);
682 nfs_direct_write_complete(dreq
, data
->inode
);
685 static const struct rpc_call_ops nfs_write_direct_ops
= {
686 #if defined(CONFIG_NFS_V4_1)
687 .rpc_call_prepare
= nfs_write_prepare
,
688 #endif /* CONFIG_NFS_V4_1 */
689 .rpc_call_done
= nfs_direct_write_result
,
690 .rpc_release
= nfs_direct_write_release
,
694 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
695 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
696 * bail and stop sending more writes. Write length accounting is
697 * handled automatically by nfs_direct_write_result(). Otherwise, if
698 * no requests have been sent, just return an error.
700 static ssize_t
nfs_direct_write_schedule_segment(struct nfs_direct_req
*dreq
,
701 const struct iovec
*iov
,
702 loff_t pos
, int sync
)
704 struct nfs_open_context
*ctx
= dreq
->ctx
;
705 struct inode
*inode
= ctx
->path
.dentry
->d_inode
;
706 unsigned long user_addr
= (unsigned long)iov
->iov_base
;
707 size_t count
= iov
->iov_len
;
708 struct rpc_task
*task
;
709 struct rpc_message msg
= {
710 .rpc_cred
= ctx
->cred
,
712 struct rpc_task_setup task_setup_data
= {
713 .rpc_client
= NFS_CLIENT(inode
),
715 .callback_ops
= &nfs_write_direct_ops
,
716 .workqueue
= nfsiod_workqueue
,
717 .flags
= RPC_TASK_ASYNC
,
719 size_t wsize
= NFS_SERVER(inode
)->wsize
;
725 struct nfs_write_data
*data
;
728 pgbase
= user_addr
& ~PAGE_MASK
;
729 bytes
= min(wsize
,count
);
732 data
= nfs_writedata_alloc(nfs_page_array_len(pgbase
, bytes
));
736 down_read(¤t
->mm
->mmap_sem
);
737 result
= get_user_pages(current
, current
->mm
, user_addr
,
738 data
->npages
, 0, 0, data
->pagevec
, NULL
);
739 up_read(¤t
->mm
->mmap_sem
);
741 nfs_writedata_free(data
);
744 if ((unsigned)result
< data
->npages
) {
745 bytes
= result
* PAGE_SIZE
;
746 if (bytes
<= pgbase
) {
747 nfs_direct_release_pages(data
->pagevec
, result
);
748 nfs_writedata_free(data
);
752 data
->npages
= result
;
757 list_move_tail(&data
->pages
, &dreq
->rewrite_list
);
759 data
->req
= (struct nfs_page
*) dreq
;
761 data
->cred
= msg
.rpc_cred
;
762 data
->args
.fh
= NFS_FH(inode
);
763 data
->args
.context
= ctx
;
764 data
->args
.offset
= pos
;
765 data
->args
.pgbase
= pgbase
;
766 data
->args
.pages
= data
->pagevec
;
767 data
->args
.count
= bytes
;
768 data
->args
.stable
= sync
;
769 data
->res
.fattr
= &data
->fattr
;
770 data
->res
.count
= bytes
;
771 data
->res
.verf
= &data
->verf
;
772 nfs_fattr_init(&data
->fattr
);
774 task_setup_data
.task
= &data
->task
;
775 task_setup_data
.callback_data
= data
;
776 msg
.rpc_argp
= &data
->args
;
777 msg
.rpc_resp
= &data
->res
;
778 NFS_PROTO(inode
)->write_setup(data
, &msg
);
780 task
= rpc_run_task(&task_setup_data
);
785 dprintk("NFS: %5u initiated direct write call "
786 "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
789 (long long)NFS_FILEID(inode
),
791 (unsigned long long)data
->args
.offset
);
797 /* FIXME: Remove this useless math from the final patch */
799 pgbase
&= ~PAGE_MASK
;
800 BUG_ON(pgbase
!= (user_addr
& ~PAGE_MASK
));
803 } while (count
!= 0);
807 return result
< 0 ? (ssize_t
) result
: -EFAULT
;
810 static ssize_t
nfs_direct_write_schedule_iovec(struct nfs_direct_req
*dreq
,
811 const struct iovec
*iov
,
812 unsigned long nr_segs
,
813 loff_t pos
, int sync
)
816 size_t requested_bytes
= 0;
821 for (seg
= 0; seg
< nr_segs
; seg
++) {
822 const struct iovec
*vec
= &iov
[seg
];
823 result
= nfs_direct_write_schedule_segment(dreq
, vec
,
827 requested_bytes
+= result
;
828 if ((size_t)result
< vec
->iov_len
)
834 nfs_direct_write_complete(dreq
, dreq
->inode
);
836 if (requested_bytes
!= 0)
844 static ssize_t
nfs_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
845 unsigned long nr_segs
, loff_t pos
,
849 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
850 struct nfs_direct_req
*dreq
;
851 size_t wsize
= NFS_SERVER(inode
)->wsize
;
852 int sync
= NFS_UNSTABLE
;
854 dreq
= nfs_direct_req_alloc();
857 nfs_alloc_commit_data(dreq
);
859 if (dreq
->commit_data
== NULL
|| count
< wsize
)
860 sync
= NFS_FILE_SYNC
;
863 dreq
->ctx
= get_nfs_open_context(nfs_file_open_context(iocb
->ki_filp
));
864 if (!is_sync_kiocb(iocb
))
867 result
= nfs_direct_write_schedule_iovec(dreq
, iov
, nr_segs
, pos
, sync
);
869 result
= nfs_direct_wait(dreq
);
870 nfs_direct_req_release(dreq
);
876 * nfs_file_direct_read - file direct read operation for NFS files
877 * @iocb: target I/O control block
878 * @iov: vector of user buffers into which to read data
879 * @nr_segs: size of iov vector
880 * @pos: byte offset in file where reading starts
882 * We use this function for direct reads instead of calling
883 * generic_file_aio_read() in order to avoid gfar's check to see if
884 * the request starts before the end of the file. For that check
885 * to work, we must generate a GETATTR before each direct read, and
886 * even then there is a window between the GETATTR and the subsequent
887 * READ where the file size could change. Our preference is simply
888 * to do all reads the application wants, and the server will take
889 * care of managing the end of file boundary.
891 * This function also eliminates unnecessarily updating the file's
892 * atime locally, as the NFS server sets the file's atime, and this
893 * client must read the updated atime from the server back into its
896 ssize_t
nfs_file_direct_read(struct kiocb
*iocb
, const struct iovec
*iov
,
897 unsigned long nr_segs
, loff_t pos
)
899 ssize_t retval
= -EINVAL
;
900 struct file
*file
= iocb
->ki_filp
;
901 struct address_space
*mapping
= file
->f_mapping
;
904 count
= iov_length(iov
, nr_segs
);
905 nfs_add_stats(mapping
->host
, NFSIOS_DIRECTREADBYTES
, count
);
907 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
908 file
->f_path
.dentry
->d_parent
->d_name
.name
,
909 file
->f_path
.dentry
->d_name
.name
,
910 count
, (long long) pos
);
916 retval
= nfs_sync_mapping(mapping
);
920 retval
= nfs_direct_read(iocb
, iov
, nr_segs
, pos
);
922 iocb
->ki_pos
= pos
+ retval
;
929 * nfs_file_direct_write - file direct write operation for NFS files
930 * @iocb: target I/O control block
931 * @iov: vector of user buffers from which to write data
932 * @nr_segs: size of iov vector
933 * @pos: byte offset in file where writing starts
935 * We use this function for direct writes instead of calling
936 * generic_file_aio_write() in order to avoid taking the inode
937 * semaphore and updating the i_size. The NFS server will set
938 * the new i_size and this client must read the updated size
939 * back into its cache. We let the server do generic write
940 * parameter checking and report problems.
942 * We eliminate local atime updates, see direct read above.
944 * We avoid unnecessary page cache invalidations for normal cached
945 * readers of this file.
947 * Note that O_APPEND is not supported for NFS direct writes, as there
948 * is no atomic O_APPEND write facility in the NFS protocol.
950 ssize_t
nfs_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
951 unsigned long nr_segs
, loff_t pos
)
953 ssize_t retval
= -EINVAL
;
954 struct file
*file
= iocb
->ki_filp
;
955 struct address_space
*mapping
= file
->f_mapping
;
958 count
= iov_length(iov
, nr_segs
);
959 nfs_add_stats(mapping
->host
, NFSIOS_DIRECTWRITTENBYTES
, count
);
961 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
962 file
->f_path
.dentry
->d_parent
->d_name
.name
,
963 file
->f_path
.dentry
->d_name
.name
,
964 count
, (long long) pos
);
966 retval
= generic_write_checks(file
, &pos
, &count
, 0);
971 if ((ssize_t
) count
< 0)
977 retval
= nfs_sync_mapping(mapping
);
981 retval
= nfs_direct_write(iocb
, iov
, nr_segs
, pos
, count
);
984 iocb
->ki_pos
= pos
+ retval
;
991 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
994 int __init
nfs_init_directcache(void)
996 nfs_direct_cachep
= kmem_cache_create("nfs_direct_cache",
997 sizeof(struct nfs_direct_req
),
998 0, (SLAB_RECLAIM_ACCOUNT
|
1001 if (nfs_direct_cachep
== NULL
)
1008 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1011 void nfs_destroy_directcache(void)
1013 kmem_cache_destroy(nfs_direct_cachep
);