blob: fc07ce4885da13de7e45262280b913e0b3ca9b0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Chuck Lever91d5b472006-03-20 13:44:14 -050057#include "iostat.h"
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFSDBG_FACILITY NFSDBG_VFS
60#define MAX_DIRECTIO_SIZE (4096UL << PAGE_SHIFT)
61
Trond Myklebust143f4122006-03-13 21:20:46 -080062static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static 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 */
71 wait_queue_head_t wait; /* wait for i/o completion */
Chuck Lever91d5b472006-03-20 13:44:14 -050072 struct inode * inode; /* target file of I/O */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct page ** pages; /* pages in our buffer */
74 unsigned int npages; /* count of pages */
75 atomic_t complete, /* i/os we're waiting for */
76 count, /* bytes actually processed */
77 error; /* any reported error */
78};
79
80
81/**
82 * nfs_get_user_pages - find and set up pages underlying user's buffer
83 * rw: direction (read or write)
84 * user_addr: starting address of this segment of user's buffer
85 * count: size of this segment
86 * @pages: returned array of page struct pointers underlying user's buffer
87 */
88static inline int
89nfs_get_user_pages(int rw, unsigned long user_addr, size_t size,
90 struct page ***pages)
91{
92 int result = -ENOMEM;
93 unsigned long page_count;
94 size_t array_size;
95
96 /* set an arbitrary limit to prevent type overflow */
97 /* XXX: this can probably be as large as INT_MAX */
98 if (size > MAX_DIRECTIO_SIZE) {
99 *pages = NULL;
100 return -EFBIG;
101 }
102
103 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
104 page_count -= user_addr >> PAGE_SHIFT;
105
106 array_size = (page_count * sizeof(struct page *));
107 *pages = kmalloc(array_size, GFP_KERNEL);
108 if (*pages) {
109 down_read(&current->mm->mmap_sem);
110 result = get_user_pages(current, current->mm, user_addr,
111 page_count, (rw == READ), 0,
112 *pages, NULL);
113 up_read(&current->mm->mmap_sem);
Trond Myklebust143f4122006-03-13 21:20:46 -0800114 /*
115 * If we got fewer pages than expected from get_user_pages(),
116 * the user buffer runs off the end of a mapping; return EFAULT.
117 */
118 if (result >= 0 && result < page_count) {
119 nfs_free_user_pages(*pages, result, 0);
120 *pages = NULL;
121 result = -EFAULT;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124 return result;
125}
126
127/**
128 * nfs_free_user_pages - tear down page struct array
129 * @pages: array of page struct pointers underlying target buffer
130 * @npages: number of pages in the array
131 * @do_dirty: dirty the pages as we release them
132 */
133static void
134nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
135{
136 int i;
137 for (i = 0; i < npages; i++) {
Trond Myklebust566dd602006-01-03 09:55:35 +0100138 struct page *page = pages[i];
139 if (do_dirty && !PageCompound(page))
140 set_page_dirty_lock(page);
141 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 kfree(pages);
144}
145
146/**
147 * nfs_direct_req_release - release nfs_direct_req structure for direct read
148 * @kref: kref object embedded in an nfs_direct_req structure
149 *
150 */
151static void nfs_direct_req_release(struct kref *kref)
152{
153 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
154 kmem_cache_free(nfs_direct_cachep, dreq);
155}
156
157/**
158 * nfs_direct_read_alloc - allocate nfs_read_data structures for direct read
159 * @count: count of bytes for the read request
160 * @rsize: local rsize setting
161 *
162 * Note we also set the number of requests we have in the dreq when we are
163 * done. This prevents races with I/O completion so we will always wait
164 * until all requests have been dispatched and completed.
165 */
166static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize)
167{
168 struct list_head *list;
169 struct nfs_direct_req *dreq;
170 unsigned int reads = 0;
Chuck Lever40859d72005-11-30 18:09:02 -0500171 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
174 if (!dreq)
175 return NULL;
176
177 kref_init(&dreq->kref);
178 init_waitqueue_head(&dreq->wait);
179 INIT_LIST_HEAD(&dreq->list);
180 atomic_set(&dreq->count, 0);
181 atomic_set(&dreq->error, 0);
182
183 list = &dreq->list;
184 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500185 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (unlikely(!data)) {
188 while (!list_empty(list)) {
189 data = list_entry(list->next,
190 struct nfs_read_data, pages);
191 list_del(&data->pages);
192 nfs_readdata_free(data);
193 }
194 kref_put(&dreq->kref, nfs_direct_req_release);
195 return NULL;
196 }
197
198 INIT_LIST_HEAD(&data->pages);
199 list_add(&data->pages, list);
200
201 data->req = (struct nfs_page *) dreq;
202 reads++;
203 if (nbytes <= rsize)
204 break;
205 nbytes -= rsize;
206 }
207 kref_get(&dreq->kref);
208 atomic_set(&dreq->complete, reads);
209 return dreq;
210}
211
212/**
213 * nfs_direct_read_result - handle a read reply for a direct read request
214 * @data: address of NFS READ operation control block
215 * @status: status of this NFS READ operation
216 *
217 * We must hold a reference to all the pages in this direct read request
218 * until the RPCs complete. This could be long *after* we are woken up in
219 * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server).
220 */
221static void nfs_direct_read_result(struct nfs_read_data *data, int status)
222{
223 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
224
225 if (likely(status >= 0))
226 atomic_add(data->res.count, &dreq->count);
227 else
228 atomic_set(&dreq->error, status);
229
230 if (unlikely(atomic_dec_and_test(&dreq->complete))) {
231 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
232 wake_up(&dreq->wait);
233 kref_put(&dreq->kref, nfs_direct_req_release);
234 }
235}
236
237/**
238 * nfs_direct_read_schedule - dispatch NFS READ operations for a direct read
239 * @dreq: address of nfs_direct_req struct for this request
240 * @inode: target inode
241 * @ctx: target file open context
242 * @user_addr: starting address of this segment of user's buffer
243 * @count: size of this segment
244 * @file_offset: offset in file to begin the operation
245 *
246 * For each nfs_read_data struct that was allocated on the list, dispatch
247 * an NFS READ operation
248 */
249static void nfs_direct_read_schedule(struct nfs_direct_req *dreq,
250 struct inode *inode, struct nfs_open_context *ctx,
251 unsigned long user_addr, size_t count, loff_t file_offset)
252{
253 struct list_head *list = &dreq->list;
254 struct page **pages = dreq->pages;
255 unsigned int curpage, pgbase;
256 unsigned int rsize = NFS_SERVER(inode)->rsize;
257
258 curpage = 0;
259 pgbase = user_addr & ~PAGE_MASK;
260 do {
261 struct nfs_read_data *data;
262 unsigned int bytes;
263
264 bytes = rsize;
265 if (count < rsize)
266 bytes = count;
267
268 data = list_entry(list->next, struct nfs_read_data, pages);
269 list_del_init(&data->pages);
270
271 data->inode = inode;
272 data->cred = ctx->cred;
273 data->args.fh = NFS_FH(inode);
274 data->args.context = ctx;
275 data->args.offset = file_offset;
276 data->args.pgbase = pgbase;
277 data->args.pages = &pages[curpage];
278 data->args.count = bytes;
279 data->res.fattr = &data->fattr;
280 data->res.eof = 0;
281 data->res.count = bytes;
282
283 NFS_PROTO(inode)->read_setup(data);
284
285 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 data->complete = nfs_direct_read_result;
287
288 lock_kernel();
289 rpc_execute(&data->task);
290 unlock_kernel();
291
292 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
293 data->task.tk_pid,
294 inode->i_sb->s_id,
295 (long long)NFS_FILEID(inode),
296 bytes,
297 (unsigned long long)data->args.offset);
298
299 file_offset += bytes;
300 pgbase += bytes;
301 curpage += pgbase >> PAGE_SHIFT;
302 pgbase &= ~PAGE_MASK;
303
304 count -= bytes;
305 } while (count != 0);
306}
307
308/**
309 * nfs_direct_read_wait - wait for I/O completion for direct reads
310 * @dreq: request on which we are to wait
311 * @intr: whether or not this wait can be interrupted
312 *
313 * Collects and returns the final error value/byte-count.
314 */
315static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
316{
317 int result = 0;
318
319 if (intr) {
320 result = wait_event_interruptible(dreq->wait,
321 (atomic_read(&dreq->complete) == 0));
322 } else {
323 wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
324 }
325
326 if (!result)
327 result = atomic_read(&dreq->error);
328 if (!result)
329 result = atomic_read(&dreq->count);
330
331 kref_put(&dreq->kref, nfs_direct_req_release);
332 return (ssize_t) result;
333}
334
335/**
336 * nfs_direct_read_seg - Read in one iov segment. Generate separate
337 * read RPCs for each "rsize" bytes.
338 * @inode: target inode
339 * @ctx: target file open context
340 * @user_addr: starting address of this segment of user's buffer
341 * @count: size of this segment
342 * @file_offset: offset in file to begin the operation
343 * @pages: array of addresses of page structs defining user's buffer
344 * @nr_pages: number of pages in the array
345 *
346 */
347static ssize_t nfs_direct_read_seg(struct inode *inode,
348 struct nfs_open_context *ctx, unsigned long user_addr,
349 size_t count, loff_t file_offset, struct page **pages,
350 unsigned int nr_pages)
351{
352 ssize_t result;
353 sigset_t oldset;
354 struct rpc_clnt *clnt = NFS_CLIENT(inode);
355 struct nfs_direct_req *dreq;
356
357 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
358 if (!dreq)
359 return -ENOMEM;
360
361 dreq->pages = pages;
362 dreq->npages = nr_pages;
Chuck Lever91d5b472006-03-20 13:44:14 -0500363 dreq->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Chuck Lever91d5b472006-03-20 13:44:14 -0500365 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 rpc_clnt_sigmask(clnt, &oldset);
367 nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
368 file_offset);
369 result = nfs_direct_read_wait(dreq, clnt->cl_intr);
370 rpc_clnt_sigunmask(clnt, &oldset);
371
372 return result;
373}
374
375/**
376 * nfs_direct_read - For each iov segment, map the user's buffer
377 * then generate read RPCs.
378 * @inode: target inode
379 * @ctx: target file open context
380 * @iov: array of vectors that define I/O buffer
381 * file_offset: offset in file to begin the operation
382 * nr_segs: size of iovec array
383 *
384 * We've already pushed out any non-direct writes so that this read
385 * will see them when we read from the server.
386 */
387static ssize_t
388nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx,
389 const struct iovec *iov, loff_t file_offset,
390 unsigned long nr_segs)
391{
392 ssize_t tot_bytes = 0;
393 unsigned long seg = 0;
394
395 while ((seg < nr_segs) && (tot_bytes >= 0)) {
396 ssize_t result;
397 int page_count;
398 struct page **pages;
399 const struct iovec *vec = &iov[seg++];
400 unsigned long user_addr = (unsigned long) vec->iov_base;
401 size_t size = vec->iov_len;
402
403 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
404 if (page_count < 0) {
405 nfs_free_user_pages(pages, 0, 0);
406 if (tot_bytes > 0)
407 break;
408 return page_count;
409 }
410
411 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
412 file_offset, pages, page_count);
413
414 if (result <= 0) {
415 if (tot_bytes > 0)
416 break;
417 return result;
418 }
419 tot_bytes += result;
420 file_offset += result;
421 if (result < size)
422 break;
423 }
424
425 return tot_bytes;
426}
427
428/**
429 * nfs_direct_write_seg - Write out one iov segment. Generate separate
430 * write RPCs for each "wsize" bytes, then commit.
431 * @inode: target inode
432 * @ctx: target file open context
433 * user_addr: starting address of this segment of user's buffer
434 * count: size of this segment
435 * file_offset: offset in file to begin the operation
436 * @pages: array of addresses of page structs defining user's buffer
437 * nr_pages: size of pages array
438 */
439static ssize_t nfs_direct_write_seg(struct inode *inode,
440 struct nfs_open_context *ctx, unsigned long user_addr,
441 size_t count, loff_t file_offset, struct page **pages,
442 int nr_pages)
443{
444 const unsigned int wsize = NFS_SERVER(inode)->wsize;
445 size_t request;
446 int curpage, need_commit;
447 ssize_t result, tot_bytes;
448 struct nfs_writeverf first_verf;
449 struct nfs_write_data *wdata;
450
Chuck Lever40859d72005-11-30 18:09:02 -0500451 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!wdata)
453 return -ENOMEM;
454
455 wdata->inode = inode;
456 wdata->cred = ctx->cred;
457 wdata->args.fh = NFS_FH(inode);
458 wdata->args.context = ctx;
459 wdata->args.stable = NFS_UNSTABLE;
460 if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
461 wdata->args.stable = NFS_FILE_SYNC;
462 wdata->res.fattr = &wdata->fattr;
463 wdata->res.verf = &wdata->verf;
464
465 nfs_begin_data_update(inode);
466retry:
467 need_commit = 0;
468 tot_bytes = 0;
469 curpage = 0;
470 request = count;
471 wdata->args.pgbase = user_addr & ~PAGE_MASK;
472 wdata->args.offset = file_offset;
473 do {
474 wdata->args.count = request;
475 if (wdata->args.count > wsize)
476 wdata->args.count = wsize;
477 wdata->args.pages = &pages[curpage];
478
479 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
480 wdata->args.count, (long long) wdata->args.offset,
481 user_addr + tot_bytes, wdata->args.pgbase, curpage);
482
483 lock_kernel();
484 result = NFS_PROTO(inode)->write(wdata);
485 unlock_kernel();
486
487 if (result <= 0) {
488 if (tot_bytes > 0)
489 break;
490 goto out;
491 }
492
493 if (tot_bytes == 0)
494 memcpy(&first_verf.verifier, &wdata->verf.verifier,
495 sizeof(first_verf.verifier));
496 if (wdata->verf.committed != NFS_FILE_SYNC) {
497 need_commit = 1;
498 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
Dirk Mueller19352452006-02-01 12:19:47 -0500499 sizeof(first_verf.verifier)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto sync_retry;
501 }
502
503 tot_bytes += result;
504
505 /* in case of a short write: stop now, let the app recover */
506 if (result < wdata->args.count)
507 break;
508
509 wdata->args.offset += result;
510 wdata->args.pgbase += result;
511 curpage += wdata->args.pgbase >> PAGE_SHIFT;
512 wdata->args.pgbase &= ~PAGE_MASK;
513 request -= result;
514 } while (request != 0);
515
516 /*
517 * Commit data written so far, even in the event of an error
518 */
519 if (need_commit) {
520 wdata->args.count = tot_bytes;
521 wdata->args.offset = file_offset;
522
523 lock_kernel();
524 result = NFS_PROTO(inode)->commit(wdata);
525 unlock_kernel();
526
527 if (result < 0 || memcmp(&first_verf.verifier,
528 &wdata->verf.verifier,
529 sizeof(first_verf.verifier)) != 0)
530 goto sync_retry;
531 }
532 result = tot_bytes;
533
534out:
Trond Myklebust951a1432005-06-22 17:16:30 +0000535 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 nfs_writedata_free(wdata);
537 return result;
538
539sync_retry:
540 wdata->args.stable = NFS_FILE_SYNC;
541 goto retry;
542}
543
544/**
545 * nfs_direct_write - For each iov segment, map the user's buffer
546 * then generate write and commit RPCs.
547 * @inode: target inode
548 * @ctx: target file open context
549 * @iov: array of vectors that define I/O buffer
550 * file_offset: offset in file to begin the operation
551 * nr_segs: size of iovec array
552 *
553 * Upon return, generic_file_direct_IO invalidates any cached pages
554 * that non-direct readers might access, so they will pick up these
555 * writes immediately.
556 */
557static ssize_t nfs_direct_write(struct inode *inode,
558 struct nfs_open_context *ctx, const struct iovec *iov,
559 loff_t file_offset, unsigned long nr_segs)
560{
561 ssize_t tot_bytes = 0;
562 unsigned long seg = 0;
563
564 while ((seg < nr_segs) && (tot_bytes >= 0)) {
565 ssize_t result;
566 int page_count;
567 struct page **pages;
568 const struct iovec *vec = &iov[seg++];
569 unsigned long user_addr = (unsigned long) vec->iov_base;
570 size_t size = vec->iov_len;
571
572 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
573 if (page_count < 0) {
574 nfs_free_user_pages(pages, 0, 0);
575 if (tot_bytes > 0)
576 break;
577 return page_count;
578 }
579
Chuck Lever91d5b472006-03-20 13:44:14 -0500580 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
582 file_offset, pages, page_count);
583 nfs_free_user_pages(pages, page_count, 0);
584
585 if (result <= 0) {
586 if (tot_bytes > 0)
587 break;
588 return result;
589 }
Chuck Lever91d5b472006-03-20 13:44:14 -0500590 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 tot_bytes += result;
592 file_offset += result;
593 if (result < size)
594 break;
595 }
596 return tot_bytes;
597}
598
599/**
600 * nfs_direct_IO - NFS address space operation for direct I/O
601 * rw: direction (read or write)
602 * @iocb: target I/O control block
603 * @iov: array of vectors that define I/O buffer
604 * file_offset: offset in file to begin the operation
605 * nr_segs: size of iovec array
606 *
607 */
608ssize_t
609nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
610 loff_t file_offset, unsigned long nr_segs)
611{
612 ssize_t result = -EINVAL;
613 struct file *file = iocb->ki_filp;
614 struct nfs_open_context *ctx;
615 struct dentry *dentry = file->f_dentry;
616 struct inode *inode = dentry->d_inode;
617
618 /*
619 * No support for async yet
620 */
621 if (!is_sync_kiocb(iocb))
622 return result;
623
624 ctx = (struct nfs_open_context *)file->private_data;
625 switch (rw) {
626 case READ:
627 dprintk("NFS: direct_IO(read) (%s) off/no(%Lu/%lu)\n",
628 dentry->d_name.name, file_offset, nr_segs);
629
630 result = nfs_direct_read(inode, ctx, iov,
631 file_offset, nr_segs);
632 break;
633 case WRITE:
634 dprintk("NFS: direct_IO(write) (%s) off/no(%Lu/%lu)\n",
635 dentry->d_name.name, file_offset, nr_segs);
636
637 result = nfs_direct_write(inode, ctx, iov,
638 file_offset, nr_segs);
639 break;
640 default:
641 break;
642 }
643 return result;
644}
645
646/**
647 * nfs_file_direct_read - file direct read operation for NFS files
648 * @iocb: target I/O control block
649 * @buf: user's buffer into which to read data
650 * count: number of bytes to read
651 * pos: byte offset in file where reading starts
652 *
653 * We use this function for direct reads instead of calling
654 * generic_file_aio_read() in order to avoid gfar's check to see if
655 * the request starts before the end of the file. For that check
656 * to work, we must generate a GETATTR before each direct read, and
657 * even then there is a window between the GETATTR and the subsequent
658 * READ where the file size could change. So our preference is simply
659 * to do all reads the application wants, and the server will take
660 * care of managing the end of file boundary.
661 *
662 * This function also eliminates unnecessarily updating the file's
663 * atime locally, as the NFS server sets the file's atime, and this
664 * client must read the updated atime from the server back into its
665 * cache.
666 */
667ssize_t
668nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
669{
670 ssize_t retval = -EINVAL;
671 loff_t *ppos = &iocb->ki_pos;
672 struct file *file = iocb->ki_filp;
673 struct nfs_open_context *ctx =
674 (struct nfs_open_context *) file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct address_space *mapping = file->f_mapping;
676 struct inode *inode = mapping->host;
677 struct iovec iov = {
678 .iov_base = buf,
679 .iov_len = count,
680 };
681
Chuck Leverce1a8e62005-11-30 18:08:17 -0500682 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500683 file->f_dentry->d_parent->d_name.name,
684 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500685 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (!is_sync_kiocb(iocb))
688 goto out;
689 if (count < 0)
690 goto out;
691 retval = -EFAULT;
692 if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
693 goto out;
694 retval = 0;
695 if (!count)
696 goto out;
697
Trond Myklebust29884df2005-12-13 16:13:54 -0500698 retval = nfs_sync_mapping(mapping);
699 if (retval)
700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
703 if (retval > 0)
704 *ppos = pos + retval;
705
706out:
707 return retval;
708}
709
710/**
711 * nfs_file_direct_write - file direct write operation for NFS files
712 * @iocb: target I/O control block
713 * @buf: user's buffer from which to write data
714 * count: number of bytes to write
715 * pos: byte offset in file where writing starts
716 *
717 * We use this function for direct writes instead of calling
718 * generic_file_aio_write() in order to avoid taking the inode
719 * semaphore and updating the i_size. The NFS server will set
720 * the new i_size and this client must read the updated size
721 * back into its cache. We let the server do generic write
722 * parameter checking and report problems.
723 *
724 * We also avoid an unnecessary invocation of generic_osync_inode(),
725 * as it is fairly meaningless to sync the metadata of an NFS file.
726 *
727 * We eliminate local atime updates, see direct read above.
728 *
729 * We avoid unnecessary page cache invalidations for normal cached
730 * readers of this file.
731 *
732 * Note that O_APPEND is not supported for NFS direct writes, as there
733 * is no atomic O_APPEND write facility in the NFS protocol.
734 */
735ssize_t
736nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
737{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500738 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct file *file = iocb->ki_filp;
740 struct nfs_open_context *ctx =
741 (struct nfs_open_context *) file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct address_space *mapping = file->f_mapping;
743 struct inode *inode = mapping->host;
744 struct iovec iov = {
745 .iov_base = (char __user *)buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 };
747
Chuck Leverce1a8e62005-11-30 18:08:17 -0500748 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500749 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500750 file->f_dentry->d_name.name,
751 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Chuck Leverce1a8e62005-11-30 18:08:17 -0500753 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!is_sync_kiocb(iocb))
755 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500756
757 retval = generic_write_checks(file, &pos, &count, 0);
758 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500760
761 retval = -EINVAL;
762 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 retval = 0;
765 if (!count)
766 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500767 iov.iov_len = count,
768
769 retval = -EFAULT;
770 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Trond Myklebust29884df2005-12-13 16:13:54 -0500773 retval = nfs_sync_mapping(mapping);
774 if (retval)
775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
778 if (mapping->nrpages)
779 invalidate_inode_pages2(mapping);
780 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500781 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783out:
784 return retval;
785}
786
787int nfs_init_directcache(void)
788{
789 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
790 sizeof(struct nfs_direct_req),
791 0, SLAB_RECLAIM_ACCOUNT,
792 NULL, NULL);
793 if (nfs_direct_cachep == NULL)
794 return -ENOMEM;
795
796 return 0;
797}
798
799void nfs_destroy_directcache(void)
800{
801 if (kmem_cache_destroy(nfs_direct_cachep))
802 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
803}