]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/direct.c
NFS: create common routine for allocating nfs_direct_req
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / direct.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/direct.c
3 *
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
5 *
6 * High-performance uncached I/O for the Linux NFS client
7 *
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.
16 *
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
22 * an application.
23 *
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.
28 *
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
31 *
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 *
38 */
39
40#include <linux/config.h>
41#include <linux/errno.h>
42#include <linux/sched.h>
43#include <linux/kernel.h>
44#include <linux/smp_lock.h>
45#include <linux/file.h>
46#include <linux/pagemap.h>
47#include <linux/kref.h>
48
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
51#include <linux/sunrpc/clnt.h>
52
53#include <asm/system.h>
54#include <asm/uaccess.h>
55#include <asm/atomic.h>
56
91d5b470
CL
57#include "iostat.h"
58
1da177e4
LT
59#define NFSDBG_FACILITY NFSDBG_VFS
60#define MAX_DIRECTIO_SIZE (4096UL << PAGE_SHIFT)
61
143f412e 62static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty);
1da177e4
LT
63static kmem_cache_t *nfs_direct_cachep;
64
65/*
66 * This represents a set of asynchronous requests that we're waiting on
67 */
68struct nfs_direct_req {
69 struct kref kref; /* release manager */
70 struct list_head list; /* nfs_read_data structs */
99514f8f
CL
71 struct file * filp; /* file descriptor */
72 struct kiocb * iocb; /* controlling i/o request */
1da177e4 73 wait_queue_head_t wait; /* wait for i/o completion */
91d5b470 74 struct inode * inode; /* target file of I/O */
1da177e4
LT
75 struct page ** pages; /* pages in our buffer */
76 unsigned int npages; /* count of pages */
77 atomic_t complete, /* i/os we're waiting for */
78 count, /* bytes actually processed */
79 error; /* any reported error */
80};
81
82
b8a32e2b
CL
83/**
84 * nfs_direct_IO - NFS address space operation for direct I/O
85 * @rw: direction (read or write)
86 * @iocb: target I/O control block
87 * @iov: array of vectors that define I/O buffer
88 * @pos: offset in file to begin the operation
89 * @nr_segs: size of iovec array
90 *
91 * The presence of this routine in the address space ops vector means
92 * the NFS client supports direct I/O. However, we shunt off direct
93 * read and write requests before the VFS gets them, so this method
94 * should never be called.
95 */
96ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
97{
98 struct dentry *dentry = iocb->ki_filp->f_dentry;
99
100 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
101 dentry->d_name.name, (long long) pos, nr_segs);
102
103 return -EINVAL;
104}
105
d4cc948b 106static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
1da177e4
LT
107{
108 int result = -ENOMEM;
109 unsigned long page_count;
110 size_t array_size;
111
112 /* set an arbitrary limit to prevent type overflow */
113 /* XXX: this can probably be as large as INT_MAX */
114 if (size > MAX_DIRECTIO_SIZE) {
115 *pages = NULL;
116 return -EFBIG;
117 }
118
119 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
120 page_count -= user_addr >> PAGE_SHIFT;
121
122 array_size = (page_count * sizeof(struct page *));
123 *pages = kmalloc(array_size, GFP_KERNEL);
124 if (*pages) {
125 down_read(&current->mm->mmap_sem);
126 result = get_user_pages(current, current->mm, user_addr,
127 page_count, (rw == READ), 0,
128 *pages, NULL);
129 up_read(&current->mm->mmap_sem);
143f412e
TM
130 /*
131 * If we got fewer pages than expected from get_user_pages(),
132 * the user buffer runs off the end of a mapping; return EFAULT.
133 */
134 if (result >= 0 && result < page_count) {
135 nfs_free_user_pages(*pages, result, 0);
136 *pages = NULL;
137 result = -EFAULT;
138 }
1da177e4
LT
139 }
140 return result;
141}
142
d4cc948b 143static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
1da177e4
LT
144{
145 int i;
146 for (i = 0; i < npages; i++) {
566dd606
TM
147 struct page *page = pages[i];
148 if (do_dirty && !PageCompound(page))
149 set_page_dirty_lock(page);
150 page_cache_release(page);
1da177e4
LT
151 }
152 kfree(pages);
153}
154
93619e59
CL
155static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
156{
157 struct nfs_direct_req *dreq;
158
159 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
160 if (!dreq)
161 return NULL;
162
163 kref_init(&dreq->kref);
164 init_waitqueue_head(&dreq->wait);
165 INIT_LIST_HEAD(&dreq->list);
166 dreq->iocb = NULL;
167 atomic_set(&dreq->count, 0);
168 atomic_set(&dreq->error, 0);
169
170 return dreq;
171}
172
1da177e4
LT
173static void nfs_direct_req_release(struct kref *kref)
174{
175 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
176 kmem_cache_free(nfs_direct_cachep, dreq);
177}
178
bc0fb201
CL
179/*
180 * Collects and returns the final error value/byte-count.
181 */
182static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
183{
184 int result = -EIOCBQUEUED;
185
186 /* Async requests don't wait here */
187 if (dreq->iocb)
188 goto out;
189
190 result = wait_event_interruptible(dreq->wait,
191 (atomic_read(&dreq->complete) == 0));
192
193 if (!result)
194 result = atomic_read(&dreq->error);
195 if (!result)
196 result = atomic_read(&dreq->count);
197
198out:
199 kref_put(&dreq->kref, nfs_direct_req_release);
200 return (ssize_t) result;
201}
202
d4cc948b 203/*
1da177e4
LT
204 * Note we also set the number of requests we have in the dreq when we are
205 * done. This prevents races with I/O completion so we will always wait
206 * until all requests have been dispatched and completed.
207 */
5dd602f2 208static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
1da177e4
LT
209{
210 struct list_head *list;
211 struct nfs_direct_req *dreq;
212 unsigned int reads = 0;
40859d7e 213 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1da177e4 214
93619e59 215 dreq = nfs_direct_req_alloc();
1da177e4
LT
216 if (!dreq)
217 return NULL;
218
1da177e4
LT
219 list = &dreq->list;
220 for(;;) {
40859d7e 221 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
1da177e4
LT
222
223 if (unlikely(!data)) {
224 while (!list_empty(list)) {
225 data = list_entry(list->next,
226 struct nfs_read_data, pages);
227 list_del(&data->pages);
228 nfs_readdata_free(data);
229 }
230 kref_put(&dreq->kref, nfs_direct_req_release);
231 return NULL;
232 }
233
234 INIT_LIST_HEAD(&data->pages);
235 list_add(&data->pages, list);
236
237 data->req = (struct nfs_page *) dreq;
238 reads++;
239 if (nbytes <= rsize)
240 break;
241 nbytes -= rsize;
242 }
243 kref_get(&dreq->kref);
244 atomic_set(&dreq->complete, reads);
245 return dreq;
246}
247
d4cc948b 248/*
1da177e4
LT
249 * We must hold a reference to all the pages in this direct read request
250 * until the RPCs complete. This could be long *after* we are woken up in
bc0fb201 251 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
487b8372
CL
252 *
253 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
254 * can't trust the iocb is still valid here if this is a synchronous
255 * request. If the waiter is woken prematurely, the iocb is long gone.
1da177e4 256 */
ec06c096 257static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
1da177e4 258{
ec06c096 259 struct nfs_read_data *data = calldata;
1da177e4
LT
260 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
261
ec06c096
TM
262 if (nfs_readpage_result(task, data) != 0)
263 return;
264 if (likely(task->tk_status >= 0))
1da177e4
LT
265 atomic_add(data->res.count, &dreq->count);
266 else
ec06c096 267 atomic_set(&dreq->error, task->tk_status);
1da177e4
LT
268
269 if (unlikely(atomic_dec_and_test(&dreq->complete))) {
270 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
487b8372
CL
271 if (dreq->iocb) {
272 long res = atomic_read(&dreq->error);
273 if (!res)
274 res = atomic_read(&dreq->count);
275 aio_complete(dreq->iocb, res, 0);
276 } else
277 wake_up(&dreq->wait);
1da177e4
LT
278 kref_put(&dreq->kref, nfs_direct_req_release);
279 }
280}
281
ec06c096
TM
282static const struct rpc_call_ops nfs_read_direct_ops = {
283 .rpc_call_done = nfs_direct_read_result,
284 .rpc_release = nfs_readdata_release,
285};
286
d4cc948b 287/*
1da177e4
LT
288 * For each nfs_read_data struct that was allocated on the list, dispatch
289 * an NFS READ operation
290 */
99514f8f 291static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t file_offset)
1da177e4 292{
99514f8f
CL
293 struct file *file = dreq->filp;
294 struct inode *inode = file->f_mapping->host;
295 struct nfs_open_context *ctx = (struct nfs_open_context *)
296 file->private_data;
1da177e4
LT
297 struct list_head *list = &dreq->list;
298 struct page **pages = dreq->pages;
5dd602f2 299 size_t rsize = NFS_SERVER(inode)->rsize;
1da177e4 300 unsigned int curpage, pgbase;
1da177e4
LT
301
302 curpage = 0;
303 pgbase = user_addr & ~PAGE_MASK;
304 do {
305 struct nfs_read_data *data;
5dd602f2 306 size_t bytes;
1da177e4
LT
307
308 bytes = rsize;
309 if (count < rsize)
310 bytes = count;
311
312 data = list_entry(list->next, struct nfs_read_data, pages);
313 list_del_init(&data->pages);
314
315 data->inode = inode;
316 data->cred = ctx->cred;
317 data->args.fh = NFS_FH(inode);
318 data->args.context = ctx;
319 data->args.offset = file_offset;
320 data->args.pgbase = pgbase;
321 data->args.pages = &pages[curpage];
322 data->args.count = bytes;
323 data->res.fattr = &data->fattr;
324 data->res.eof = 0;
325 data->res.count = bytes;
326
ec06c096
TM
327 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
328 &nfs_read_direct_ops, data);
1da177e4
LT
329 NFS_PROTO(inode)->read_setup(data);
330
331 data->task.tk_cookie = (unsigned long) inode;
1da177e4
LT
332
333 lock_kernel();
334 rpc_execute(&data->task);
335 unlock_kernel();
336
337 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
338 data->task.tk_pid,
339 inode->i_sb->s_id,
340 (long long)NFS_FILEID(inode),
341 bytes,
342 (unsigned long long)data->args.offset);
343
344 file_offset += bytes;
345 pgbase += bytes;
346 curpage += pgbase >> PAGE_SHIFT;
347 pgbase &= ~PAGE_MASK;
348
349 count -= bytes;
350 } while (count != 0);
351}
352
99514f8f 353static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, unsigned int nr_pages)
1da177e4
LT
354{
355 ssize_t result;
356 sigset_t oldset;
99514f8f 357 struct inode *inode = iocb->ki_filp->f_mapping->host;
1da177e4
LT
358 struct rpc_clnt *clnt = NFS_CLIENT(inode);
359 struct nfs_direct_req *dreq;
360
361 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
362 if (!dreq)
363 return -ENOMEM;
364
365 dreq->pages = pages;
366 dreq->npages = nr_pages;
91d5b470 367 dreq->inode = inode;
99514f8f 368 dreq->filp = iocb->ki_filp;
487b8372
CL
369 if (!is_sync_kiocb(iocb))
370 dreq->iocb = iocb;
1da177e4 371
91d5b470 372 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
1da177e4 373 rpc_clnt_sigmask(clnt, &oldset);
99514f8f 374 nfs_direct_read_schedule(dreq, user_addr, count, file_offset);
bc0fb201 375 result = nfs_direct_wait(dreq);
1da177e4
LT
376 rpc_clnt_sigunmask(clnt, &oldset);
377
378 return result;
379}
380
d4cc948b 381static ssize_t nfs_direct_write_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, int nr_pages)
1da177e4
LT
382{
383 const unsigned int wsize = NFS_SERVER(inode)->wsize;
384 size_t request;
385 int curpage, need_commit;
386 ssize_t result, tot_bytes;
387 struct nfs_writeverf first_verf;
388 struct nfs_write_data *wdata;
389
40859d7e 390 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
1da177e4
LT
391 if (!wdata)
392 return -ENOMEM;
393
394 wdata->inode = inode;
395 wdata->cred = ctx->cred;
396 wdata->args.fh = NFS_FH(inode);
397 wdata->args.context = ctx;
398 wdata->args.stable = NFS_UNSTABLE;
399 if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
400 wdata->args.stable = NFS_FILE_SYNC;
401 wdata->res.fattr = &wdata->fattr;
402 wdata->res.verf = &wdata->verf;
403
404 nfs_begin_data_update(inode);
405retry:
406 need_commit = 0;
407 tot_bytes = 0;
408 curpage = 0;
409 request = count;
410 wdata->args.pgbase = user_addr & ~PAGE_MASK;
411 wdata->args.offset = file_offset;
412 do {
413 wdata->args.count = request;
414 if (wdata->args.count > wsize)
415 wdata->args.count = wsize;
416 wdata->args.pages = &pages[curpage];
417
418 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
419 wdata->args.count, (long long) wdata->args.offset,
420 user_addr + tot_bytes, wdata->args.pgbase, curpage);
421
422 lock_kernel();
423 result = NFS_PROTO(inode)->write(wdata);
424 unlock_kernel();
425
426 if (result <= 0) {
427 if (tot_bytes > 0)
428 break;
429 goto out;
430 }
431
432 if (tot_bytes == 0)
433 memcpy(&first_verf.verifier, &wdata->verf.verifier,
434 sizeof(first_verf.verifier));
435 if (wdata->verf.committed != NFS_FILE_SYNC) {
436 need_commit = 1;
437 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
19352456 438 sizeof(first_verf.verifier)))
1da177e4
LT
439 goto sync_retry;
440 }
441
442 tot_bytes += result;
443
444 /* in case of a short write: stop now, let the app recover */
445 if (result < wdata->args.count)
446 break;
447
448 wdata->args.offset += result;
449 wdata->args.pgbase += result;
450 curpage += wdata->args.pgbase >> PAGE_SHIFT;
451 wdata->args.pgbase &= ~PAGE_MASK;
452 request -= result;
453 } while (request != 0);
454
455 /*
456 * Commit data written so far, even in the event of an error
457 */
458 if (need_commit) {
459 wdata->args.count = tot_bytes;
460 wdata->args.offset = file_offset;
461
462 lock_kernel();
463 result = NFS_PROTO(inode)->commit(wdata);
464 unlock_kernel();
465
466 if (result < 0 || memcmp(&first_verf.verifier,
467 &wdata->verf.verifier,
468 sizeof(first_verf.verifier)) != 0)
469 goto sync_retry;
470 }
471 result = tot_bytes;
472
473out:
951a143b 474 nfs_end_data_update(inode);
1da177e4
LT
475 nfs_writedata_free(wdata);
476 return result;
477
478sync_retry:
479 wdata->args.stable = NFS_FILE_SYNC;
480 goto retry;
481}
482
d4cc948b 483/*
1da177e4
LT
484 * Upon return, generic_file_direct_IO invalidates any cached pages
485 * that non-direct readers might access, so they will pick up these
486 * writes immediately.
487 */
d4cc948b 488static ssize_t nfs_direct_write(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs)
1da177e4
LT
489{
490 ssize_t tot_bytes = 0;
491 unsigned long seg = 0;
492
493 while ((seg < nr_segs) && (tot_bytes >= 0)) {
494 ssize_t result;
495 int page_count;
496 struct page **pages;
497 const struct iovec *vec = &iov[seg++];
498 unsigned long user_addr = (unsigned long) vec->iov_base;
499 size_t size = vec->iov_len;
500
501 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
502 if (page_count < 0) {
503 nfs_free_user_pages(pages, 0, 0);
504 if (tot_bytes > 0)
505 break;
506 return page_count;
507 }
508
91d5b470 509 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
1da177e4
LT
510 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
511 file_offset, pages, page_count);
512 nfs_free_user_pages(pages, page_count, 0);
513
514 if (result <= 0) {
515 if (tot_bytes > 0)
516 break;
517 return result;
518 }
91d5b470 519 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
1da177e4
LT
520 tot_bytes += result;
521 file_offset += result;
522 if (result < size)
523 break;
524 }
525 return tot_bytes;
526}
527
1da177e4
LT
528/**
529 * nfs_file_direct_read - file direct read operation for NFS files
530 * @iocb: target I/O control block
531 * @buf: user's buffer into which to read data
532 * count: number of bytes to read
533 * pos: byte offset in file where reading starts
534 *
535 * We use this function for direct reads instead of calling
536 * generic_file_aio_read() in order to avoid gfar's check to see if
537 * the request starts before the end of the file. For that check
538 * to work, we must generate a GETATTR before each direct read, and
539 * even then there is a window between the GETATTR and the subsequent
540 * READ where the file size could change. So our preference is simply
541 * to do all reads the application wants, and the server will take
542 * care of managing the end of file boundary.
543 *
544 * This function also eliminates unnecessarily updating the file's
545 * atime locally, as the NFS server sets the file's atime, and this
546 * client must read the updated atime from the server back into its
547 * cache.
548 */
d4cc948b 549ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1da177e4
LT
550{
551 ssize_t retval = -EINVAL;
0cdd80d0
CL
552 int page_count;
553 struct page **pages;
1da177e4 554 struct file *file = iocb->ki_filp;
1da177e4 555 struct address_space *mapping = file->f_mapping;
1da177e4 556
ce1a8e67 557 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
0bbacc40
CL
558 file->f_dentry->d_parent->d_name.name,
559 file->f_dentry->d_name.name,
ce1a8e67 560 (unsigned long) count, (long long) pos);
1da177e4 561
1da177e4
LT
562 if (count < 0)
563 goto out;
564 retval = -EFAULT;
0cdd80d0 565 if (!access_ok(VERIFY_WRITE, buf, count))
1da177e4
LT
566 goto out;
567 retval = 0;
568 if (!count)
569 goto out;
570
29884df0
TM
571 retval = nfs_sync_mapping(mapping);
572 if (retval)
573 goto out;
1da177e4 574
0cdd80d0
CL
575 page_count = nfs_get_user_pages(READ, (unsigned long) buf,
576 count, &pages);
577 if (page_count < 0) {
578 nfs_free_user_pages(pages, 0, 0);
579 retval = page_count;
580 goto out;
581 }
582
99514f8f 583 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
0cdd80d0 584 pages, page_count);
1da177e4 585 if (retval > 0)
0cdd80d0 586 iocb->ki_pos = pos + retval;
1da177e4
LT
587
588out:
589 return retval;
590}
591
592/**
593 * nfs_file_direct_write - file direct write operation for NFS files
594 * @iocb: target I/O control block
595 * @buf: user's buffer from which to write data
596 * count: number of bytes to write
597 * pos: byte offset in file where writing starts
598 *
599 * We use this function for direct writes instead of calling
600 * generic_file_aio_write() in order to avoid taking the inode
601 * semaphore and updating the i_size. The NFS server will set
602 * the new i_size and this client must read the updated size
603 * back into its cache. We let the server do generic write
604 * parameter checking and report problems.
605 *
606 * We also avoid an unnecessary invocation of generic_osync_inode(),
607 * as it is fairly meaningless to sync the metadata of an NFS file.
608 *
609 * We eliminate local atime updates, see direct read above.
610 *
611 * We avoid unnecessary page cache invalidations for normal cached
612 * readers of this file.
613 *
614 * Note that O_APPEND is not supported for NFS direct writes, as there
615 * is no atomic O_APPEND write facility in the NFS protocol.
616 */
d4cc948b 617ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
1da177e4 618{
ce1a8e67 619 ssize_t retval;
1da177e4
LT
620 struct file *file = iocb->ki_filp;
621 struct nfs_open_context *ctx =
622 (struct nfs_open_context *) file->private_data;
1da177e4
LT
623 struct address_space *mapping = file->f_mapping;
624 struct inode *inode = mapping->host;
625 struct iovec iov = {
626 .iov_base = (char __user *)buf,
1da177e4
LT
627 };
628
ce1a8e67 629 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
0bbacc40 630 file->f_dentry->d_parent->d_name.name,
ce1a8e67
CL
631 file->f_dentry->d_name.name,
632 (unsigned long) count, (long long) pos);
1da177e4 633
ce1a8e67 634 retval = -EINVAL;
1da177e4
LT
635 if (!is_sync_kiocb(iocb))
636 goto out;
ce1a8e67
CL
637
638 retval = generic_write_checks(file, &pos, &count, 0);
639 if (retval)
1da177e4 640 goto out;
ce1a8e67
CL
641
642 retval = -EINVAL;
643 if ((ssize_t) count < 0)
1da177e4 644 goto out;
1da177e4
LT
645 retval = 0;
646 if (!count)
647 goto out;
ce1a8e67
CL
648 iov.iov_len = count,
649
650 retval = -EFAULT;
651 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
652 goto out;
1da177e4 653
29884df0
TM
654 retval = nfs_sync_mapping(mapping);
655 if (retval)
656 goto out;
1da177e4
LT
657
658 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
659 if (mapping->nrpages)
660 invalidate_inode_pages2(mapping);
661 if (retval > 0)
ce1a8e67 662 iocb->ki_pos = pos + retval;
1da177e4
LT
663
664out:
665 return retval;
666}
667
668int nfs_init_directcache(void)
669{
670 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
671 sizeof(struct nfs_direct_req),
672 0, SLAB_RECLAIM_ACCOUNT,
673 NULL, NULL);
674 if (nfs_direct_cachep == NULL)
675 return -ENOMEM;
676
677 return 0;
678}
679
680void nfs_destroy_directcache(void)
681{
682 if (kmem_cache_destroy(nfs_direct_cachep))
683 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
684}