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>
48 #include <linux/task_io_accounting_ops.h>
49 #include <linux/module.h>
51 #include <linux/nfs_fs.h>
52 #include <linux/nfs_page.h>
53 #include <linux/sunrpc/clnt.h>
55 #include <asm/uaccess.h>
56 #include <linux/atomic.h>
62 #define NFSDBG_FACILITY NFSDBG_VFS
64 static struct kmem_cache
*nfs_direct_cachep
;
67 * This represents a set of asynchronous requests that we're waiting on
69 struct nfs_direct_req
{
70 struct kref kref
; /* release manager */
73 struct nfs_open_context
*ctx
; /* file open context info */
74 struct nfs_lock_context
*l_ctx
; /* Lock context info */
75 struct kiocb
* iocb
; /* controlling i/o request */
76 struct inode
* inode
; /* target file of i/o */
78 /* completion state */
79 atomic_t io_count
; /* i/os we're waiting for */
80 spinlock_t lock
; /* protect completion state */
81 ssize_t count
, /* bytes actually processed */
82 bytes_left
, /* bytes left to be sent */
83 error
; /* any reported error */
84 struct completion completion
; /* wait for i/o completion */
87 struct nfs_mds_commit_info mds_cinfo
; /* Storage for cinfo */
88 struct pnfs_ds_commit_info ds_cinfo
; /* Storage for cinfo */
89 struct work_struct work
;
91 #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
92 #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
93 struct nfs_writeverf verf
; /* unstable write verifier */
96 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops
;
97 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops
;
98 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
);
99 static void nfs_direct_write_schedule_work(struct work_struct
*work
);
101 static inline void get_dreq(struct nfs_direct_req
*dreq
)
103 atomic_inc(&dreq
->io_count
);
106 static inline int put_dreq(struct nfs_direct_req
*dreq
)
108 return atomic_dec_and_test(&dreq
->io_count
);
112 * nfs_direct_IO - NFS address space operation for direct I/O
113 * @rw: direction (read or write)
114 * @iocb: target I/O control block
115 * @iov: array of vectors that define I/O buffer
116 * @pos: offset in file to begin the operation
117 * @nr_segs: size of iovec array
119 * The presence of this routine in the address space ops vector means
120 * the NFS client supports direct I/O. However, for most direct IO, we
121 * shunt off direct read and write requests before the VFS gets them,
122 * so this method is only ever called for swap.
124 ssize_t
nfs_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
, loff_t pos
, unsigned long nr_segs
)
126 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
128 /* we only support swap file calling nfs_direct_IO */
129 if (!IS_SWAPFILE(inode
))
132 #ifndef CONFIG_NFS_SWAP
133 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
134 iocb
->ki_filp
->f_path
.dentry
->d_name
.name
,
135 (long long) pos
, nr_segs
);
139 VM_BUG_ON(iocb
->ki_nbytes
!= PAGE_SIZE
);
141 if (rw
== READ
|| rw
== KERNEL_READ
)
142 return nfs_file_direct_read(iocb
, iov
, nr_segs
, pos
,
143 rw
== READ
? true : false);
144 return nfs_file_direct_write(iocb
, iov
, nr_segs
, pos
,
145 rw
== WRITE
? true : false);
146 #endif /* CONFIG_NFS_SWAP */
149 static void nfs_direct_release_pages(struct page
**pages
, unsigned int npages
)
152 for (i
= 0; i
< npages
; i
++)
153 page_cache_release(pages
[i
]);
156 void nfs_init_cinfo_from_dreq(struct nfs_commit_info
*cinfo
,
157 struct nfs_direct_req
*dreq
)
159 cinfo
->lock
= &dreq
->lock
;
160 cinfo
->mds
= &dreq
->mds_cinfo
;
161 cinfo
->ds
= &dreq
->ds_cinfo
;
163 cinfo
->completion_ops
= &nfs_direct_commit_completion_ops
;
166 static inline struct nfs_direct_req
*nfs_direct_req_alloc(void)
168 struct nfs_direct_req
*dreq
;
170 dreq
= kmem_cache_zalloc(nfs_direct_cachep
, GFP_KERNEL
);
174 kref_init(&dreq
->kref
);
175 kref_get(&dreq
->kref
);
176 init_completion(&dreq
->completion
);
177 INIT_LIST_HEAD(&dreq
->mds_cinfo
.list
);
178 INIT_WORK(&dreq
->work
, nfs_direct_write_schedule_work
);
179 spin_lock_init(&dreq
->lock
);
184 static void nfs_direct_req_free(struct kref
*kref
)
186 struct nfs_direct_req
*dreq
= container_of(kref
, struct nfs_direct_req
, kref
);
188 nfs_free_pnfs_ds_cinfo(&dreq
->ds_cinfo
);
189 if (dreq
->l_ctx
!= NULL
)
190 nfs_put_lock_context(dreq
->l_ctx
);
191 if (dreq
->ctx
!= NULL
)
192 put_nfs_open_context(dreq
->ctx
);
193 kmem_cache_free(nfs_direct_cachep
, dreq
);
196 static void nfs_direct_req_release(struct nfs_direct_req
*dreq
)
198 kref_put(&dreq
->kref
, nfs_direct_req_free
);
201 ssize_t
nfs_dreq_bytes_left(struct nfs_direct_req
*dreq
)
203 return dreq
->bytes_left
;
205 EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left
);
208 * Collects and returns the final error value/byte-count.
210 static ssize_t
nfs_direct_wait(struct nfs_direct_req
*dreq
)
212 ssize_t result
= -EIOCBQUEUED
;
214 /* Async requests don't wait here */
218 result
= wait_for_completion_killable(&dreq
->completion
);
221 result
= dreq
->error
;
223 result
= dreq
->count
;
226 return (ssize_t
) result
;
230 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
231 * the iocb is still valid here if this is a synchronous request.
233 static void nfs_direct_complete(struct nfs_direct_req
*dreq
, bool write
)
235 struct inode
*inode
= dreq
->inode
;
237 if (dreq
->iocb
&& write
) {
238 loff_t pos
= dreq
->iocb
->ki_pos
+ dreq
->count
;
240 spin_lock(&inode
->i_lock
);
241 if (i_size_read(inode
) < pos
)
242 i_size_write(inode
, pos
);
243 spin_unlock(&inode
->i_lock
);
247 nfs_zap_mapping(inode
, inode
->i_mapping
);
249 inode_dio_done(inode
);
252 long res
= (long) dreq
->error
;
254 res
= (long) dreq
->count
;
255 aio_complete(dreq
->iocb
, res
, 0);
258 complete_all(&dreq
->completion
);
260 nfs_direct_req_release(dreq
);
263 static void nfs_direct_readpage_release(struct nfs_page
*req
)
265 dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
266 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
267 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
269 (long long)req_offset(req
));
270 nfs_release_request(req
);
273 static void nfs_direct_read_completion(struct nfs_pgio_header
*hdr
)
275 unsigned long bytes
= 0;
276 struct nfs_direct_req
*dreq
= hdr
->dreq
;
278 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
281 spin_lock(&dreq
->lock
);
282 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
) && (hdr
->good_bytes
== 0))
283 dreq
->error
= hdr
->error
;
285 dreq
->count
+= hdr
->good_bytes
;
286 spin_unlock(&dreq
->lock
);
288 while (!list_empty(&hdr
->pages
)) {
289 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
290 struct page
*page
= req
->wb_page
;
292 if (!PageCompound(page
) && bytes
< hdr
->good_bytes
)
293 set_page_dirty(page
);
294 bytes
+= req
->wb_bytes
;
295 nfs_list_remove_request(req
);
296 nfs_direct_readpage_release(req
);
300 nfs_direct_complete(dreq
, false);
304 static void nfs_read_sync_pgio_error(struct list_head
*head
)
306 struct nfs_page
*req
;
308 while (!list_empty(head
)) {
309 req
= nfs_list_entry(head
->next
);
310 nfs_list_remove_request(req
);
311 nfs_release_request(req
);
315 static void nfs_direct_pgio_init(struct nfs_pgio_header
*hdr
)
320 static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops
= {
321 .error_cleanup
= nfs_read_sync_pgio_error
,
322 .init_hdr
= nfs_direct_pgio_init
,
323 .completion
= nfs_direct_read_completion
,
327 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
328 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
329 * bail and stop sending more reads. Read length accounting is
330 * handled automatically by nfs_direct_read_result(). Otherwise, if
331 * no requests have been sent, just return an error.
333 static ssize_t
nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor
*desc
,
334 const struct iovec
*iov
,
335 loff_t pos
, bool uio
)
337 struct nfs_direct_req
*dreq
= desc
->pg_dreq
;
338 struct nfs_open_context
*ctx
= dreq
->ctx
;
339 struct inode
*inode
= ctx
->dentry
->d_inode
;
340 unsigned long user_addr
= (unsigned long)iov
->iov_base
;
341 size_t count
= iov
->iov_len
;
342 size_t rsize
= NFS_SERVER(inode
)->rsize
;
346 struct page
**pagevec
= NULL
;
353 pgbase
= user_addr
& ~PAGE_MASK
;
354 bytes
= min(max_t(size_t, rsize
, PAGE_SIZE
), count
);
357 npages
= nfs_page_array_len(pgbase
, bytes
);
359 pagevec
= kmalloc(npages
* sizeof(struct page
*),
364 down_read(¤t
->mm
->mmap_sem
);
365 result
= get_user_pages(current
, current
->mm
, user_addr
,
366 npages
, 1, 0, pagevec
, NULL
);
367 up_read(¤t
->mm
->mmap_sem
);
371 WARN_ON(npages
!= 1);
372 result
= get_kernel_page(user_addr
, 1, pagevec
);
373 if (WARN_ON(result
!= 1))
377 if ((unsigned)result
< npages
) {
378 bytes
= result
* PAGE_SIZE
;
379 if (bytes
<= pgbase
) {
380 nfs_direct_release_pages(pagevec
, result
);
387 for (i
= 0; i
< npages
; i
++) {
388 struct nfs_page
*req
;
389 unsigned int req_len
= min_t(size_t, bytes
, PAGE_SIZE
- pgbase
);
390 /* XXX do we need to do the eof zeroing found in async_filler? */
391 req
= nfs_create_request(dreq
->ctx
, dreq
->inode
,
395 result
= PTR_ERR(req
);
398 req
->wb_index
= pos
>> PAGE_SHIFT
;
399 req
->wb_offset
= pos
& ~PAGE_MASK
;
400 if (!nfs_pageio_add_request(desc
, req
)) {
401 result
= desc
->pg_error
;
402 nfs_release_request(req
);
408 user_addr
+= req_len
;
411 dreq
->bytes_left
-= req_len
;
413 /* The nfs_page now hold references to these pages */
414 nfs_direct_release_pages(pagevec
, npages
);
415 } while (count
!= 0 && result
>= 0);
421 return result
< 0 ? (ssize_t
) result
: -EFAULT
;
424 static ssize_t
nfs_direct_read_schedule_iovec(struct nfs_direct_req
*dreq
,
425 const struct iovec
*iov
,
426 unsigned long nr_segs
,
427 loff_t pos
, bool uio
)
429 struct nfs_pageio_descriptor desc
;
430 struct inode
*inode
= dreq
->inode
;
431 ssize_t result
= -EINVAL
;
432 size_t requested_bytes
= 0;
435 NFS_PROTO(dreq
->inode
)->read_pageio_init(&desc
, dreq
->inode
,
436 &nfs_direct_read_completion_ops
);
439 atomic_inc(&inode
->i_dio_count
);
441 for (seg
= 0; seg
< nr_segs
; seg
++) {
442 const struct iovec
*vec
= &iov
[seg
];
443 result
= nfs_direct_read_schedule_segment(&desc
, vec
, pos
, uio
);
446 requested_bytes
+= result
;
447 if ((size_t)result
< vec
->iov_len
)
452 nfs_pageio_complete(&desc
);
455 * If no bytes were started, return the error, and let the
456 * generic layer handle the completion.
458 if (requested_bytes
== 0) {
459 inode_dio_done(inode
);
460 nfs_direct_req_release(dreq
);
461 return result
< 0 ? result
: -EIO
;
465 nfs_direct_complete(dreq
, false);
469 static ssize_t
nfs_direct_read(struct kiocb
*iocb
, const struct iovec
*iov
,
470 unsigned long nr_segs
, loff_t pos
, bool uio
)
472 ssize_t result
= -ENOMEM
;
473 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
474 struct nfs_direct_req
*dreq
;
475 struct nfs_lock_context
*l_ctx
;
477 dreq
= nfs_direct_req_alloc();
482 dreq
->bytes_left
= iov_length(iov
, nr_segs
);
483 dreq
->ctx
= get_nfs_open_context(nfs_file_open_context(iocb
->ki_filp
));
484 l_ctx
= nfs_get_lock_context(dreq
->ctx
);
486 result
= PTR_ERR(l_ctx
);
490 if (!is_sync_kiocb(iocb
))
493 NFS_I(inode
)->read_io
+= iov_length(iov
, nr_segs
);
494 result
= nfs_direct_read_schedule_iovec(dreq
, iov
, nr_segs
, pos
, uio
);
496 result
= nfs_direct_wait(dreq
);
498 nfs_direct_req_release(dreq
);
503 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
504 static void nfs_direct_write_reschedule(struct nfs_direct_req
*dreq
)
506 struct nfs_pageio_descriptor desc
;
507 struct nfs_page
*req
, *tmp
;
509 struct nfs_commit_info cinfo
;
512 nfs_init_cinfo_from_dreq(&cinfo
, dreq
);
513 pnfs_recover_commit_reqs(dreq
->inode
, &reqs
, &cinfo
);
514 spin_lock(cinfo
.lock
);
515 nfs_scan_commit_list(&cinfo
.mds
->list
, &reqs
, &cinfo
, 0);
516 spin_unlock(cinfo
.lock
);
521 NFS_PROTO(dreq
->inode
)->write_pageio_init(&desc
, dreq
->inode
, FLUSH_STABLE
,
522 &nfs_direct_write_completion_ops
);
525 list_for_each_entry_safe(req
, tmp
, &reqs
, wb_list
) {
526 if (!nfs_pageio_add_request(&desc
, req
)) {
527 nfs_list_remove_request(req
);
528 nfs_list_add_request(req
, &failed
);
529 spin_lock(cinfo
.lock
);
532 spin_unlock(cinfo
.lock
);
534 nfs_release_request(req
);
536 nfs_pageio_complete(&desc
);
538 while (!list_empty(&failed
)) {
539 req
= nfs_list_entry(failed
.next
);
540 nfs_list_remove_request(req
);
541 nfs_unlock_and_release_request(req
);
545 nfs_direct_write_complete(dreq
, dreq
->inode
);
548 static void nfs_direct_commit_complete(struct nfs_commit_data
*data
)
550 struct nfs_direct_req
*dreq
= data
->dreq
;
551 struct nfs_commit_info cinfo
;
552 struct nfs_page
*req
;
553 int status
= data
->task
.tk_status
;
555 nfs_init_cinfo_from_dreq(&cinfo
, dreq
);
557 dprintk("NFS: %5u commit failed with error %d.\n",
558 data
->task
.tk_pid
, status
);
559 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
560 } else if (memcmp(&dreq
->verf
, &data
->verf
, sizeof(data
->verf
))) {
561 dprintk("NFS: %5u commit verify failed\n", data
->task
.tk_pid
);
562 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
565 dprintk("NFS: %5u commit returned %d\n", data
->task
.tk_pid
, status
);
566 while (!list_empty(&data
->pages
)) {
567 req
= nfs_list_entry(data
->pages
.next
);
568 nfs_list_remove_request(req
);
569 if (dreq
->flags
== NFS_ODIRECT_RESCHED_WRITES
) {
570 /* Note the rewrite will go through mds */
571 nfs_mark_request_commit(req
, NULL
, &cinfo
);
573 nfs_release_request(req
);
574 nfs_unlock_and_release_request(req
);
577 if (atomic_dec_and_test(&cinfo
.mds
->rpcs_out
))
578 nfs_direct_write_complete(dreq
, data
->inode
);
581 static void nfs_direct_error_cleanup(struct nfs_inode
*nfsi
)
583 /* There is no lock to clear */
586 static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops
= {
587 .completion
= nfs_direct_commit_complete
,
588 .error_cleanup
= nfs_direct_error_cleanup
,
591 static void nfs_direct_commit_schedule(struct nfs_direct_req
*dreq
)
594 struct nfs_commit_info cinfo
;
597 nfs_init_cinfo_from_dreq(&cinfo
, dreq
);
598 nfs_scan_commit(dreq
->inode
, &mds_list
, &cinfo
);
599 res
= nfs_generic_commit_list(dreq
->inode
, &mds_list
, 0, &cinfo
);
600 if (res
< 0) /* res == -ENOMEM */
601 nfs_direct_write_reschedule(dreq
);
604 static void nfs_direct_write_schedule_work(struct work_struct
*work
)
606 struct nfs_direct_req
*dreq
= container_of(work
, struct nfs_direct_req
, work
);
607 int flags
= dreq
->flags
;
611 case NFS_ODIRECT_DO_COMMIT
:
612 nfs_direct_commit_schedule(dreq
);
614 case NFS_ODIRECT_RESCHED_WRITES
:
615 nfs_direct_write_reschedule(dreq
);
618 nfs_direct_complete(dreq
, true);
622 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
624 schedule_work(&dreq
->work
); /* Calls nfs_direct_write_schedule_work */
628 static void nfs_direct_write_schedule_work(struct work_struct
*work
)
632 static void nfs_direct_write_complete(struct nfs_direct_req
*dreq
, struct inode
*inode
)
634 nfs_direct_complete(dreq
, true);
639 * NB: Return the value of the first error return code. Subsequent
640 * errors after the first one are ignored.
643 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
644 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
645 * bail and stop sending more writes. Write length accounting is
646 * handled automatically by nfs_direct_write_result(). Otherwise, if
647 * no requests have been sent, just return an error.
649 static ssize_t
nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor
*desc
,
650 const struct iovec
*iov
,
651 loff_t pos
, bool uio
)
653 struct nfs_direct_req
*dreq
= desc
->pg_dreq
;
654 struct nfs_open_context
*ctx
= dreq
->ctx
;
655 struct inode
*inode
= ctx
->dentry
->d_inode
;
656 unsigned long user_addr
= (unsigned long)iov
->iov_base
;
657 size_t count
= iov
->iov_len
;
658 size_t wsize
= NFS_SERVER(inode
)->wsize
;
662 struct page
**pagevec
= NULL
;
669 pgbase
= user_addr
& ~PAGE_MASK
;
670 bytes
= min(max_t(size_t, wsize
, PAGE_SIZE
), count
);
673 npages
= nfs_page_array_len(pgbase
, bytes
);
675 pagevec
= kmalloc(npages
* sizeof(struct page
*), GFP_KERNEL
);
680 down_read(¤t
->mm
->mmap_sem
);
681 result
= get_user_pages(current
, current
->mm
, user_addr
,
682 npages
, 0, 0, pagevec
, NULL
);
683 up_read(¤t
->mm
->mmap_sem
);
687 WARN_ON(npages
!= 1);
688 result
= get_kernel_page(user_addr
, 0, pagevec
);
689 if (WARN_ON(result
!= 1))
693 if ((unsigned)result
< npages
) {
694 bytes
= result
* PAGE_SIZE
;
695 if (bytes
<= pgbase
) {
696 nfs_direct_release_pages(pagevec
, result
);
703 for (i
= 0; i
< npages
; i
++) {
704 struct nfs_page
*req
;
705 unsigned int req_len
= min_t(size_t, bytes
, PAGE_SIZE
- pgbase
);
707 req
= nfs_create_request(dreq
->ctx
, dreq
->inode
,
711 result
= PTR_ERR(req
);
714 nfs_lock_request(req
);
715 req
->wb_index
= pos
>> PAGE_SHIFT
;
716 req
->wb_offset
= pos
& ~PAGE_MASK
;
717 if (!nfs_pageio_add_request(desc
, req
)) {
718 result
= desc
->pg_error
;
719 nfs_unlock_and_release_request(req
);
725 user_addr
+= req_len
;
728 dreq
->bytes_left
-= req_len
;
730 /* The nfs_page now hold references to these pages */
731 nfs_direct_release_pages(pagevec
, npages
);
732 } while (count
!= 0 && result
>= 0);
738 return result
< 0 ? (ssize_t
) result
: -EFAULT
;
741 static void nfs_direct_write_completion(struct nfs_pgio_header
*hdr
)
743 struct nfs_direct_req
*dreq
= hdr
->dreq
;
744 struct nfs_commit_info cinfo
;
746 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
748 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
751 nfs_init_cinfo_from_dreq(&cinfo
, dreq
);
753 spin_lock(&dreq
->lock
);
755 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
)) {
757 dreq
->error
= hdr
->error
;
759 if (dreq
->error
!= 0)
760 bit
= NFS_IOHDR_ERROR
;
762 dreq
->count
+= hdr
->good_bytes
;
763 if (test_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
)) {
764 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
765 bit
= NFS_IOHDR_NEED_RESCHED
;
766 } else if (test_bit(NFS_IOHDR_NEED_COMMIT
, &hdr
->flags
)) {
767 if (dreq
->flags
== NFS_ODIRECT_RESCHED_WRITES
)
768 bit
= NFS_IOHDR_NEED_RESCHED
;
769 else if (dreq
->flags
== 0) {
770 memcpy(&dreq
->verf
, hdr
->verf
,
772 bit
= NFS_IOHDR_NEED_COMMIT
;
773 dreq
->flags
= NFS_ODIRECT_DO_COMMIT
;
774 } else if (dreq
->flags
== NFS_ODIRECT_DO_COMMIT
) {
775 if (memcmp(&dreq
->verf
, hdr
->verf
, sizeof(dreq
->verf
))) {
776 dreq
->flags
= NFS_ODIRECT_RESCHED_WRITES
;
777 bit
= NFS_IOHDR_NEED_RESCHED
;
779 bit
= NFS_IOHDR_NEED_COMMIT
;
783 spin_unlock(&dreq
->lock
);
785 while (!list_empty(&hdr
->pages
)) {
786 req
= nfs_list_entry(hdr
->pages
.next
);
787 nfs_list_remove_request(req
);
789 case NFS_IOHDR_NEED_RESCHED
:
790 case NFS_IOHDR_NEED_COMMIT
:
791 kref_get(&req
->wb_kref
);
792 nfs_mark_request_commit(req
, hdr
->lseg
, &cinfo
);
794 nfs_unlock_and_release_request(req
);
799 nfs_direct_write_complete(dreq
, hdr
->inode
);
803 static void nfs_write_sync_pgio_error(struct list_head
*head
)
805 struct nfs_page
*req
;
807 while (!list_empty(head
)) {
808 req
= nfs_list_entry(head
->next
);
809 nfs_list_remove_request(req
);
810 nfs_unlock_and_release_request(req
);
814 static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops
= {
815 .error_cleanup
= nfs_write_sync_pgio_error
,
816 .init_hdr
= nfs_direct_pgio_init
,
817 .completion
= nfs_direct_write_completion
,
820 static ssize_t
nfs_direct_write_schedule_iovec(struct nfs_direct_req
*dreq
,
821 const struct iovec
*iov
,
822 unsigned long nr_segs
,
823 loff_t pos
, bool uio
)
825 struct nfs_pageio_descriptor desc
;
826 struct inode
*inode
= dreq
->inode
;
828 size_t requested_bytes
= 0;
831 NFS_PROTO(inode
)->write_pageio_init(&desc
, inode
, FLUSH_COND_STABLE
,
832 &nfs_direct_write_completion_ops
);
835 atomic_inc(&inode
->i_dio_count
);
837 NFS_I(dreq
->inode
)->write_io
+= iov_length(iov
, nr_segs
);
838 for (seg
= 0; seg
< nr_segs
; seg
++) {
839 const struct iovec
*vec
= &iov
[seg
];
840 result
= nfs_direct_write_schedule_segment(&desc
, vec
, pos
, uio
);
843 requested_bytes
+= result
;
844 if ((size_t)result
< vec
->iov_len
)
848 nfs_pageio_complete(&desc
);
851 * If no bytes were started, return the error, and let the
852 * generic layer handle the completion.
854 if (requested_bytes
== 0) {
855 inode_dio_done(inode
);
856 nfs_direct_req_release(dreq
);
857 return result
< 0 ? result
: -EIO
;
861 nfs_direct_write_complete(dreq
, dreq
->inode
);
865 static ssize_t
nfs_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
866 unsigned long nr_segs
, loff_t pos
,
867 size_t count
, bool uio
)
869 ssize_t result
= -ENOMEM
;
870 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
871 struct nfs_direct_req
*dreq
;
872 struct nfs_lock_context
*l_ctx
;
874 dreq
= nfs_direct_req_alloc();
879 dreq
->bytes_left
= count
;
880 dreq
->ctx
= get_nfs_open_context(nfs_file_open_context(iocb
->ki_filp
));
881 l_ctx
= nfs_get_lock_context(dreq
->ctx
);
883 result
= PTR_ERR(l_ctx
);
887 if (!is_sync_kiocb(iocb
))
890 result
= nfs_direct_write_schedule_iovec(dreq
, iov
, nr_segs
, pos
, uio
);
892 result
= nfs_direct_wait(dreq
);
894 nfs_direct_req_release(dreq
);
900 * nfs_file_direct_read - file direct read operation for NFS files
901 * @iocb: target I/O control block
902 * @iov: vector of user buffers into which to read data
903 * @nr_segs: size of iov vector
904 * @pos: byte offset in file where reading starts
906 * We use this function for direct reads instead of calling
907 * generic_file_aio_read() in order to avoid gfar's check to see if
908 * the request starts before the end of the file. For that check
909 * to work, we must generate a GETATTR before each direct read, and
910 * even then there is a window between the GETATTR and the subsequent
911 * READ where the file size could change. Our preference is simply
912 * to do all reads the application wants, and the server will take
913 * care of managing the end of file boundary.
915 * This function also eliminates unnecessarily updating the file's
916 * atime locally, as the NFS server sets the file's atime, and this
917 * client must read the updated atime from the server back into its
920 ssize_t
nfs_file_direct_read(struct kiocb
*iocb
, const struct iovec
*iov
,
921 unsigned long nr_segs
, loff_t pos
, bool uio
)
923 ssize_t retval
= -EINVAL
;
924 struct file
*file
= iocb
->ki_filp
;
925 struct address_space
*mapping
= file
->f_mapping
;
928 count
= iov_length(iov
, nr_segs
);
929 nfs_add_stats(mapping
->host
, NFSIOS_DIRECTREADBYTES
, count
);
931 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
932 file
->f_path
.dentry
->d_parent
->d_name
.name
,
933 file
->f_path
.dentry
->d_name
.name
,
934 count
, (long long) pos
);
940 retval
= nfs_sync_mapping(mapping
);
944 task_io_account_read(count
);
946 retval
= nfs_direct_read(iocb
, iov
, nr_segs
, pos
, uio
);
948 iocb
->ki_pos
= pos
+ retval
;
955 * nfs_file_direct_write - file direct write operation for NFS files
956 * @iocb: target I/O control block
957 * @iov: vector of user buffers from which to write data
958 * @nr_segs: size of iov vector
959 * @pos: byte offset in file where writing starts
961 * We use this function for direct writes instead of calling
962 * generic_file_aio_write() in order to avoid taking the inode
963 * semaphore and updating the i_size. The NFS server will set
964 * the new i_size and this client must read the updated size
965 * back into its cache. We let the server do generic write
966 * parameter checking and report problems.
968 * We eliminate local atime updates, see direct read above.
970 * We avoid unnecessary page cache invalidations for normal cached
971 * readers of this file.
973 * Note that O_APPEND is not supported for NFS direct writes, as there
974 * is no atomic O_APPEND write facility in the NFS protocol.
976 ssize_t
nfs_file_direct_write(struct kiocb
*iocb
, const struct iovec
*iov
,
977 unsigned long nr_segs
, loff_t pos
, bool uio
)
979 ssize_t retval
= -EINVAL
;
980 struct file
*file
= iocb
->ki_filp
;
981 struct address_space
*mapping
= file
->f_mapping
;
984 count
= iov_length(iov
, nr_segs
);
985 nfs_add_stats(mapping
->host
, NFSIOS_DIRECTWRITTENBYTES
, count
);
987 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
988 file
->f_path
.dentry
->d_parent
->d_name
.name
,
989 file
->f_path
.dentry
->d_name
.name
,
990 count
, (long long) pos
);
992 retval
= generic_write_checks(file
, &pos
, &count
, 0);
997 if ((ssize_t
) count
< 0)
1003 retval
= nfs_sync_mapping(mapping
);
1007 task_io_account_write(count
);
1009 retval
= nfs_direct_write(iocb
, iov
, nr_segs
, pos
, count
, uio
);
1011 struct inode
*inode
= mapping
->host
;
1013 iocb
->ki_pos
= pos
+ retval
;
1014 spin_lock(&inode
->i_lock
);
1015 if (i_size_read(inode
) < iocb
->ki_pos
)
1016 i_size_write(inode
, iocb
->ki_pos
);
1017 spin_unlock(&inode
->i_lock
);
1024 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
1027 int __init
nfs_init_directcache(void)
1029 nfs_direct_cachep
= kmem_cache_create("nfs_direct_cache",
1030 sizeof(struct nfs_direct_req
),
1031 0, (SLAB_RECLAIM_ACCOUNT
|
1034 if (nfs_direct_cachep
== NULL
)
1041 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
1044 void nfs_destroy_directcache(void)
1046 kmem_cache_destroy(nfs_direct_cachep
);