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 void nfs_free_user_pages(struct page
**pages
, int npages
, int do_dirty
);
63 static kmem_cache_t
*nfs_direct_cachep
;
66 * This represents a set of asynchronous requests that we're waiting on
68 struct nfs_direct_req
{
69 struct kref kref
; /* release manager */
72 struct list_head list
, /* nfs_read/write_data structs */
73 rewrite_list
; /* saved nfs_write_data structs */
74 struct nfs_open_context
*ctx
; /* file open context info */
75 struct kiocb
* iocb
; /* controlling i/o request */
76 wait_queue_head_t wait
; /* wait for i/o completion */
77 struct inode
* inode
; /* target file of i/o */
78 unsigned long user_addr
; /* location of user's buffer */
79 size_t user_count
; /* total bytes to move */
80 loff_t pos
; /* starting offset in file */
81 struct page
** pages
; /* pages in our buffer */
82 unsigned int npages
; /* count of pages */
84 /* completion state */
85 spinlock_t lock
; /* protect completion state */
86 int outstanding
; /* i/os we're waiting for */
87 ssize_t count
, /* bytes actually processed */
88 error
; /* any reported error */
91 struct nfs_write_data
* commit_data
; /* special write_data for commits */
93 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
94 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
95 struct nfs_writeverf verf
; /* unstable write verifier */
98 static void nfs_direct_write_schedule(struct nfs_direct_req
*dreq
, int sync
);
99 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
);
102 * nfs_direct_IO - NFS address space operation for direct I/O
103 * @rw: direction (read or write)
104 * @iocb: target I/O control block
105 * @iov: array of vectors that define I/O buffer
106 * @pos: offset in file to begin the operation
107 * @nr_segs: size of iovec array
109 * The presence of this routine in the address space ops vector means
110 * the NFS client supports direct I/O. However, we shunt off direct
111 * read and write requests before the VFS gets them, so this method
112 * should never be called.
114 ssize_t
nfs_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
, loff_t pos
, unsigned long nr_segs
)
116 struct dentry
*dentry
= iocb
->ki_filp
->f_dentry
;
118 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
119 dentry
->d_name
.name
, (long long) pos
, nr_segs
);
124 static inline int nfs_get_user_pages(int rw
, unsigned long user_addr
, size_t size
, struct page
***pages
)
126 int result
= -ENOMEM
;
127 unsigned long page_count
;
130 page_count
= (user_addr
+ size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
131 page_count
-= user_addr
>> PAGE_SHIFT
;
133 array_size
= (page_count
* sizeof(struct page
*));
134 *pages
= kmalloc(array_size
, GFP_KERNEL
);
136 down_read(¤t
->mm
->mmap_sem
);
137 result
= get_user_pages(current
, current
->mm
, user_addr
,
138 page_count
, (rw
== READ
), 0,
140 up_read(¤t
->mm
->mmap_sem
);
142 * If we got fewer pages than expected from get_user_pages(),
143 * the user buffer runs off the end of a mapping; return EFAULT.
145 if (result
>= 0 && result
< page_count
) {
146 nfs_free_user_pages(*pages
, result
, 0);
154 static void nfs_free_user_pages(struct page
**pages
, int npages
, int do_dirty
)
157 for (i
= 0; i
< npages
; i
++) {
158 struct page
*page
= pages
[i
];
159 if (do_dirty
&& !PageCompound(page
))
160 set_page_dirty_lock(page
);
161 page_cache_release(page
);
166 static inline struct nfs_direct_req
*nfs_direct_req_alloc(void)
168 struct nfs_direct_req
*dreq
;
170 dreq
= kmem_cache_alloc(nfs_direct_cachep
, SLAB_KERNEL
);
174 kref_init(&dreq
->kref
);
175 init_waitqueue_head(&dreq
->wait
);
176 INIT_LIST_HEAD(&dreq
->list
);
177 INIT_LIST_HEAD(&dreq
->rewrite_list
);
180 spin_lock_init(&dreq
->lock
);
181 dreq
->outstanding
= 0;
189 static void nfs_direct_req_release(struct kref
*kref
)
191 struct nfs_direct_req
*dreq
= container_of(kref
, struct nfs_direct_req
, kref
);
193 if (dreq
->ctx
!= NULL
)
194 put_nfs_open_context(dreq
->ctx
);
195 kmem_cache_free(nfs_direct_cachep
, dreq
);
199 * Collects and returns the final error value/byte-count.
201 static ssize_t
nfs_direct_wait(struct nfs_direct_req
*dreq
)
203 ssize_t result
= -EIOCBQUEUED
;
205 /* Async requests don't wait here */
209 result
= wait_event_interruptible(dreq
->wait
, (dreq
->outstanding
== 0));
212 result
= dreq
->error
;
214 result
= dreq
->count
;
217 kref_put(&dreq
->kref
, nfs_direct_req_release
);
218 return (ssize_t
) result
;
222 * We must hold a reference to all the pages in this direct read request
223 * until the RPCs complete. This could be long *after* we are woken up in
224 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
226 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
227 * can't trust the iocb is still valid here if this is a synchronous
228 * request. If the waiter is woken prematurely, the iocb is long gone.
230 static void nfs_direct_complete(struct nfs_direct_req
*dreq
)
232 nfs_free_user_pages(dreq
->pages
, dreq
->npages
, 1);
235 long res
= (long) dreq
->error
;
237 res
= (long) dreq
->count
;
238 aio_complete(dreq
->iocb
, res
, 0);
240 wake_up(&dreq
->wait
);
242 kref_put(&dreq
->kref
, nfs_direct_req_release
);
246 * Note we also set the number of requests we have in the dreq when we are
247 * done. This prevents races with I/O completion so we will always wait
248 * until all requests have been dispatched and completed.
250 static struct nfs_direct_req
*nfs_direct_read_alloc(size_t nbytes
, size_t rsize
)
252 struct list_head
*list
;
253 struct nfs_direct_req
*dreq
;
254 unsigned int rpages
= (rsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
256 dreq
= nfs_direct_req_alloc();
262 struct nfs_read_data
*data
= nfs_readdata_alloc(rpages
);
264 if (unlikely(!data
)) {
265 while (!list_empty(list
)) {
266 data
= list_entry(list
->next
,
267 struct nfs_read_data
, pages
);
268 list_del(&data
->pages
);
269 nfs_readdata_free(data
);
271 kref_put(&dreq
->kref
, nfs_direct_req_release
);
275 INIT_LIST_HEAD(&data
->pages
);
276 list_add(&data
->pages
, list
);
278 data
->req
= (struct nfs_page
*) dreq
;
284 kref_get(&dreq
->kref
);
288 static void nfs_direct_read_result(struct rpc_task
*task
, void *calldata
)
290 struct nfs_read_data
*data
= calldata
;
291 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
293 if (nfs_readpage_result(task
, data
) != 0)
296 spin_lock(&dreq
->lock
);
298 if (likely(task
->tk_status
>= 0))
299 dreq
->count
+= data
->res
.count
;
301 dreq
->error
= task
->tk_status
;
303 if (--dreq
->outstanding
) {
304 spin_unlock(&dreq
->lock
);
308 spin_unlock(&dreq
->lock
);
309 nfs_direct_complete(dreq
);
312 static const struct rpc_call_ops nfs_read_direct_ops
= {
313 .rpc_call_done
= nfs_direct_read_result
,
314 .rpc_release
= nfs_readdata_release
,
318 * For each nfs_read_data struct that was allocated on the list, dispatch
319 * an NFS READ operation
321 static void nfs_direct_read_schedule(struct nfs_direct_req
*dreq
)
323 struct nfs_open_context
*ctx
= dreq
->ctx
;
324 struct inode
*inode
= ctx
->dentry
->d_inode
;
325 struct list_head
*list
= &dreq
->list
;
326 struct page
**pages
= dreq
->pages
;
327 size_t count
= dreq
->user_count
;
328 loff_t pos
= dreq
->pos
;
329 size_t rsize
= NFS_SERVER(inode
)->rsize
;
330 unsigned int curpage
, pgbase
;
333 pgbase
= dreq
->user_addr
& ~PAGE_MASK
;
335 struct nfs_read_data
*data
;
342 BUG_ON(list_empty(list
));
343 data
= list_entry(list
->next
, struct nfs_read_data
, pages
);
344 list_del_init(&data
->pages
);
347 data
->cred
= ctx
->cred
;
348 data
->args
.fh
= NFS_FH(inode
);
349 data
->args
.context
= ctx
;
350 data
->args
.offset
= pos
;
351 data
->args
.pgbase
= pgbase
;
352 data
->args
.pages
= &pages
[curpage
];
353 data
->args
.count
= bytes
;
354 data
->res
.fattr
= &data
->fattr
;
356 data
->res
.count
= bytes
;
358 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), RPC_TASK_ASYNC
,
359 &nfs_read_direct_ops
, data
);
360 NFS_PROTO(inode
)->read_setup(data
);
362 data
->task
.tk_cookie
= (unsigned long) inode
;
365 rpc_execute(&data
->task
);
368 dfprintk(VFS
, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
371 (long long)NFS_FILEID(inode
),
373 (unsigned long long)data
->args
.offset
);
377 curpage
+= pgbase
>> PAGE_SHIFT
;
378 pgbase
&= ~PAGE_MASK
;
381 } while (count
!= 0);
382 BUG_ON(!list_empty(list
));
385 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
)
389 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
390 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
391 struct nfs_direct_req
*dreq
;
393 dreq
= nfs_direct_read_alloc(count
, NFS_SERVER(inode
)->rsize
);
397 dreq
->user_addr
= user_addr
;
398 dreq
->user_count
= count
;
401 dreq
->npages
= nr_pages
;
403 dreq
->ctx
= get_nfs_open_context((struct nfs_open_context
*)iocb
->ki_filp
->private_data
);
404 if (!is_sync_kiocb(iocb
))
407 nfs_add_stats(inode
, NFSIOS_DIRECTREADBYTES
, count
);
408 rpc_clnt_sigmask(clnt
, &oldset
);
409 nfs_direct_read_schedule(dreq
);
410 result
= nfs_direct_wait(dreq
);
411 rpc_clnt_sigunmask(clnt
, &oldset
);
416 static void nfs_direct_free_writedata(struct nfs_direct_req
*dreq
)
418 list_splice_init(&dreq
->rewrite_list
, &dreq
->list
);
419 while (!list_empty(&dreq
->list
)) {
420 struct nfs_write_data
*data
= list_entry(dreq
->list
.next
, struct nfs_write_data
, pages
);
421 list_del(&data
->pages
);
422 nfs_writedata_release(data
);
426 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
427 static void nfs_direct_write_reschedule(struct nfs_direct_req
*dreq
)
429 struct list_head
*pos
;
431 list_splice_init(&dreq
->rewrite_list
, &dreq
->list
);
432 list_for_each(pos
, &dreq
->list
)
436 nfs_direct_write_schedule(dreq
, FLUSH_STABLE
);
439 static void nfs_direct_commit_result(struct rpc_task
*task
, void *calldata
)
441 struct nfs_write_data
*data
= calldata
;
442 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
444 /* Call the NFS version-specific code */
445 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
447 if (unlikely(task
->tk_status
< 0)) {
448 dreq
->error
= task
->tk_status
;
449 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
451 if (memcmp(&dreq
->verf
, &data
->verf
, sizeof(data
->verf
))) {
452 dprintk("NFS: %5u commit verify failed\n", task
->tk_pid
);
453 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
456 dprintk("NFS: %5u commit returned %d\n", task
->tk_pid
, task
->tk_status
);
457 nfs_direct_write_complete(dreq
, data
->inode
);
460 static const struct rpc_call_ops nfs_commit_direct_ops
= {
461 .rpc_call_done
= nfs_direct_commit_result
,
462 .rpc_release
= nfs_commit_release
,
465 static void nfs_direct_commit_schedule(struct nfs_direct_req
*dreq
)
467 struct nfs_write_data
*data
= dreq
->commit_data
;
468 struct rpc_task
*task
= &data
->task
;
470 data
->inode
= dreq
->inode
;
471 data
->cred
= dreq
->ctx
->cred
;
473 data
->args
.fh
= NFS_FH(data
->inode
);
474 data
->args
.offset
= dreq
->pos
;
475 data
->args
.count
= dreq
->user_count
;
477 data
->res
.fattr
= &data
->fattr
;
478 data
->res
.verf
= &data
->verf
;
480 rpc_init_task(&data
->task
, NFS_CLIENT(dreq
->inode
), RPC_TASK_ASYNC
,
481 &nfs_commit_direct_ops
, data
);
482 NFS_PROTO(data
->inode
)->commit_setup(data
, 0);
484 data
->task
.tk_priority
= RPC_PRIORITY_NORMAL
;
485 data
->task
.tk_cookie
= (unsigned long)data
->inode
;
486 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
487 dreq
->commit_data
= NULL
;
489 dprintk("NFS: %5u initiated commit call\n", task
->tk_pid
);
492 rpc_execute(&data
->task
);
496 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
498 int flags
= dreq
->flags
;
502 case NFS_ODIRECT_DO_COMMIT
:
503 nfs_direct_commit_schedule(dreq
);
505 case NFS_ODIRECT_RESCHED_WRITES
:
506 nfs_direct_write_reschedule(dreq
);
509 nfs_end_data_update(inode
);
510 if (dreq
->commit_data
!= NULL
)
511 nfs_commit_free(dreq
->commit_data
);
512 nfs_direct_free_writedata(dreq
);
513 nfs_direct_complete(dreq
);
517 static void nfs_alloc_commit_data(struct nfs_direct_req
*dreq
)
519 dreq
->commit_data
= nfs_commit_alloc(0);
520 if (dreq
->commit_data
!= NULL
)
521 dreq
->commit_data
->req
= (struct nfs_page
*) dreq
;
524 static inline void nfs_alloc_commit_data(struct nfs_direct_req
*dreq
)
526 dreq
->commit_data
= NULL
;
529 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
531 nfs_end_data_update(inode
);
532 nfs_direct_free_writedata(dreq
);
533 nfs_direct_complete(dreq
);
537 static struct nfs_direct_req
*nfs_direct_write_alloc(size_t nbytes
, size_t wsize
)
539 struct list_head
*list
;
540 struct nfs_direct_req
*dreq
;
541 unsigned int wpages
= (wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
543 dreq
= nfs_direct_req_alloc();
549 struct nfs_write_data
*data
= nfs_writedata_alloc(wpages
);
551 if (unlikely(!data
)) {
552 while (!list_empty(list
)) {
553 data
= list_entry(list
->next
,
554 struct nfs_write_data
, pages
);
555 list_del(&data
->pages
);
556 nfs_writedata_free(data
);
558 kref_put(&dreq
->kref
, nfs_direct_req_release
);
562 INIT_LIST_HEAD(&data
->pages
);
563 list_add(&data
->pages
, list
);
565 data
->req
= (struct nfs_page
*) dreq
;
572 nfs_alloc_commit_data(dreq
);
574 kref_get(&dreq
->kref
);
578 static void nfs_direct_write_result(struct rpc_task
*task
, void *calldata
)
580 struct nfs_write_data
*data
= calldata
;
581 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
582 int status
= task
->tk_status
;
584 if (nfs_writeback_done(task
, data
) != 0)
587 spin_lock(&dreq
->lock
);
589 if (likely(status
>= 0))
590 dreq
->count
+= data
->res
.count
;
592 dreq
->error
= task
->tk_status
;
594 if (data
->res
.verf
->committed
!= NFS_FILE_SYNC
) {
595 switch (dreq
->flags
) {
597 memcpy(&dreq
->verf
, &data
->verf
, sizeof(dreq
->verf
));
598 dreq
->flags
= NFS_ODIRECT_DO_COMMIT
;
600 case NFS_ODIRECT_DO_COMMIT
:
601 if (memcmp(&dreq
->verf
, &data
->verf
, sizeof(dreq
->verf
))) {
602 dprintk("NFS: %5u write verify failed\n", task
->tk_pid
);
603 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
607 /* In case we have to resend */
608 data
->args
.stable
= NFS_FILE_SYNC
;
610 spin_unlock(&dreq
->lock
);
614 * NB: Return the value of the first error return code. Subsequent
615 * errors after the first one are ignored.
617 static void nfs_direct_write_release(void *calldata
)
619 struct nfs_write_data
*data
= calldata
;
620 struct nfs_direct_req
*dreq
= (struct nfs_direct_req
*) data
->req
;
622 spin_lock(&dreq
->lock
);
623 if (--dreq
->outstanding
) {
624 spin_unlock(&dreq
->lock
);
627 spin_unlock(&dreq
->lock
);
629 nfs_direct_write_complete(dreq
, data
->inode
);
632 static const struct rpc_call_ops nfs_write_direct_ops
= {
633 .rpc_call_done
= nfs_direct_write_result
,
634 .rpc_release
= nfs_direct_write_release
,
638 * For each nfs_write_data struct that was allocated on the list, dispatch
639 * an NFS WRITE operation
641 static void nfs_direct_write_schedule(struct nfs_direct_req
*dreq
, int sync
)
643 struct nfs_open_context
*ctx
= dreq
->ctx
;
644 struct inode
*inode
= ctx
->dentry
->d_inode
;
645 struct list_head
*list
= &dreq
->list
;
646 struct page
**pages
= dreq
->pages
;
647 size_t count
= dreq
->user_count
;
648 loff_t pos
= dreq
->pos
;
649 size_t wsize
= NFS_SERVER(inode
)->wsize
;
650 unsigned int curpage
, pgbase
;
653 pgbase
= dreq
->user_addr
& ~PAGE_MASK
;
655 struct nfs_write_data
*data
;
662 BUG_ON(list_empty(list
));
663 data
= list_entry(list
->next
, struct nfs_write_data
, pages
);
664 list_move_tail(&data
->pages
, &dreq
->rewrite_list
);
667 data
->cred
= ctx
->cred
;
668 data
->args
.fh
= NFS_FH(inode
);
669 data
->args
.context
= ctx
;
670 data
->args
.offset
= pos
;
671 data
->args
.pgbase
= pgbase
;
672 data
->args
.pages
= &pages
[curpage
];
673 data
->args
.count
= bytes
;
674 data
->res
.fattr
= &data
->fattr
;
675 data
->res
.count
= bytes
;
676 data
->res
.verf
= &data
->verf
;
678 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), RPC_TASK_ASYNC
,
679 &nfs_write_direct_ops
, data
);
680 NFS_PROTO(inode
)->write_setup(data
, sync
);
682 data
->task
.tk_priority
= RPC_PRIORITY_NORMAL
;
683 data
->task
.tk_cookie
= (unsigned long) inode
;
686 rpc_execute(&data
->task
);
689 dfprintk(VFS
, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
692 (long long)NFS_FILEID(inode
),
694 (unsigned long long)data
->args
.offset
);
698 curpage
+= pgbase
>> PAGE_SHIFT
;
699 pgbase
&= ~PAGE_MASK
;
702 } while (count
!= 0);
703 BUG_ON(!list_empty(list
));
706 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
)
710 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
711 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
712 struct nfs_direct_req
*dreq
;
713 size_t wsize
= NFS_SERVER(inode
)->wsize
;
716 dreq
= nfs_direct_write_alloc(count
, wsize
);
719 if (dreq
->commit_data
== NULL
|| count
< wsize
)
722 dreq
->user_addr
= user_addr
;
723 dreq
->user_count
= count
;
726 dreq
->npages
= nr_pages
;
728 dreq
->ctx
= get_nfs_open_context((struct nfs_open_context
*)iocb
->ki_filp
->private_data
);
729 if (!is_sync_kiocb(iocb
))
732 nfs_add_stats(inode
, NFSIOS_DIRECTWRITTENBYTES
, count
);
734 nfs_begin_data_update(inode
);
736 rpc_clnt_sigmask(clnt
, &oldset
);
737 nfs_direct_write_schedule(dreq
, sync
);
738 result
= nfs_direct_wait(dreq
);
739 rpc_clnt_sigunmask(clnt
, &oldset
);
745 * nfs_file_direct_read - file direct read operation for NFS files
746 * @iocb: target I/O control block
747 * @buf: user's buffer into which to read data
748 * @count: number of bytes to read
749 * @pos: byte offset in file where reading starts
751 * We use this function for direct reads instead of calling
752 * generic_file_aio_read() in order to avoid gfar's check to see if
753 * the request starts before the end of the file. For that check
754 * to work, we must generate a GETATTR before each direct read, and
755 * even then there is a window between the GETATTR and the subsequent
756 * READ where the file size could change. Our preference is simply
757 * to do all reads the application wants, and the server will take
758 * care of managing the end of file boundary.
760 * This function also eliminates unnecessarily updating the file's
761 * atime locally, as the NFS server sets the file's atime, and this
762 * client must read the updated atime from the server back into its
765 ssize_t
nfs_file_direct_read(struct kiocb
*iocb
, char __user
*buf
, size_t count
, loff_t pos
)
767 ssize_t retval
= -EINVAL
;
770 struct file
*file
= iocb
->ki_filp
;
771 struct address_space
*mapping
= file
->f_mapping
;
773 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
774 file
->f_dentry
->d_parent
->d_name
.name
,
775 file
->f_dentry
->d_name
.name
,
776 (unsigned long) count
, (long long) pos
);
781 if (!access_ok(VERIFY_WRITE
, buf
, count
))
787 retval
= nfs_sync_mapping(mapping
);
791 page_count
= nfs_get_user_pages(READ
, (unsigned long) buf
,
793 if (page_count
< 0) {
794 nfs_free_user_pages(pages
, 0, 0);
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 page_count
= nfs_get_user_pages(WRITE
, (unsigned long) buf
,
867 if (page_count
< 0) {
868 nfs_free_user_pages(pages
, 0, 0);
873 retval
= nfs_direct_write(iocb
, (unsigned long) buf
, count
,
874 pos
, pages
, page_count
);
877 * XXX: nfs_end_data_update() already ensures this file's
878 * cached data is subsequently invalidated. Do we really
879 * need to call invalidate_inode_pages2() again here?
881 * For aio writes, this invalidation will almost certainly
882 * occur before the writes complete. Kind of racey.
884 if (mapping
->nrpages
)
885 invalidate_inode_pages2(mapping
);
888 iocb
->ki_pos
= pos
+ retval
;
895 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
898 int nfs_init_directcache(void)
900 nfs_direct_cachep
= kmem_cache_create("nfs_direct_cache",
901 sizeof(struct nfs_direct_req
),
902 0, SLAB_RECLAIM_ACCOUNT
,
904 if (nfs_direct_cachep
== NULL
)
911 * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures
914 void nfs_destroy_directcache(void)
916 if (kmem_cache_destroy(nfs_direct_cachep
))
917 printk(KERN_INFO
"nfs_direct_cache: not all structures were freed\n");