blob: 8f5d2dfd5a8a3681bb840bbf5ca610c368022fb2 [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 Myklebust143f412e2006-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/**
Chuck Leverb8a32e22006-03-20 13:44:28 -050082 * nfs_direct_IO - NFS address space operation for direct I/O
83 * @rw: direction (read or write)
84 * @iocb: target I/O control block
85 * @iov: array of vectors that define I/O buffer
86 * @pos: offset in file to begin the operation
87 * @nr_segs: size of iovec array
88 *
89 * The presence of this routine in the address space ops vector means
90 * the NFS client supports direct I/O. However, we shunt off direct
91 * read and write requests before the VFS gets them, so this method
92 * should never be called.
93 */
94ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
95{
96 struct dentry *dentry = iocb->ki_filp->f_dentry;
97
98 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
99 dentry->d_name.name, (long long) pos, nr_segs);
100
101 return -EINVAL;
102}
103
Chuck Leverd4cc9482006-03-20 13:44:28 -0500104static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int result = -ENOMEM;
107 unsigned long page_count;
108 size_t array_size;
109
110 /* set an arbitrary limit to prevent type overflow */
111 /* XXX: this can probably be as large as INT_MAX */
112 if (size > MAX_DIRECTIO_SIZE) {
113 *pages = NULL;
114 return -EFBIG;
115 }
116
117 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
118 page_count -= user_addr >> PAGE_SHIFT;
119
120 array_size = (page_count * sizeof(struct page *));
121 *pages = kmalloc(array_size, GFP_KERNEL);
122 if (*pages) {
123 down_read(&current->mm->mmap_sem);
124 result = get_user_pages(current, current->mm, user_addr,
125 page_count, (rw == READ), 0,
126 *pages, NULL);
127 up_read(&current->mm->mmap_sem);
Trond Myklebust143f412e2006-03-13 21:20:46 -0800128 /*
129 * If we got fewer pages than expected from get_user_pages(),
130 * the user buffer runs off the end of a mapping; return EFAULT.
131 */
132 if (result >= 0 && result < page_count) {
133 nfs_free_user_pages(*pages, result, 0);
134 *pages = NULL;
135 result = -EFAULT;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 return result;
139}
140
Chuck Leverd4cc9482006-03-20 13:44:28 -0500141static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 int i;
144 for (i = 0; i < npages; i++) {
Trond Myklebust566dd602006-01-03 09:55:35 +0100145 struct page *page = pages[i];
146 if (do_dirty && !PageCompound(page))
147 set_page_dirty_lock(page);
148 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 kfree(pages);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void nfs_direct_req_release(struct kref *kref)
154{
155 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
156 kmem_cache_free(nfs_direct_cachep, dreq);
157}
158
Chuck Leverd4cc9482006-03-20 13:44:28 -0500159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Note we also set the number of requests we have in the dreq when we are
161 * done. This prevents races with I/O completion so we will always wait
162 * until all requests have been dispatched and completed.
163 */
Chuck Lever5dd602f2006-03-20 13:44:29 -0500164static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct list_head *list;
167 struct nfs_direct_req *dreq;
168 unsigned int reads = 0;
Chuck Lever40859d72005-11-30 18:09:02 -0500169 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
172 if (!dreq)
173 return NULL;
174
175 kref_init(&dreq->kref);
176 init_waitqueue_head(&dreq->wait);
177 INIT_LIST_HEAD(&dreq->list);
178 atomic_set(&dreq->count, 0);
179 atomic_set(&dreq->error, 0);
180
181 list = &dreq->list;
182 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500183 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (unlikely(!data)) {
186 while (!list_empty(list)) {
187 data = list_entry(list->next,
188 struct nfs_read_data, pages);
189 list_del(&data->pages);
190 nfs_readdata_free(data);
191 }
192 kref_put(&dreq->kref, nfs_direct_req_release);
193 return NULL;
194 }
195
196 INIT_LIST_HEAD(&data->pages);
197 list_add(&data->pages, list);
198
199 data->req = (struct nfs_page *) dreq;
200 reads++;
201 if (nbytes <= rsize)
202 break;
203 nbytes -= rsize;
204 }
205 kref_get(&dreq->kref);
206 atomic_set(&dreq->complete, reads);
207 return dreq;
208}
209
Chuck Leverd4cc9482006-03-20 13:44:28 -0500210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * We must hold a reference to all the pages in this direct read request
212 * until the RPCs complete. This could be long *after* we are woken up in
213 * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server).
214 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500215static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Trond Myklebustec06c092006-03-20 13:44:27 -0500217 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
219
Trond Myklebustec06c092006-03-20 13:44:27 -0500220 if (nfs_readpage_result(task, data) != 0)
221 return;
222 if (likely(task->tk_status >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 atomic_add(data->res.count, &dreq->count);
224 else
Trond Myklebustec06c092006-03-20 13:44:27 -0500225 atomic_set(&dreq->error, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (unlikely(atomic_dec_and_test(&dreq->complete))) {
228 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
229 wake_up(&dreq->wait);
230 kref_put(&dreq->kref, nfs_direct_req_release);
231 }
232}
233
Trond Myklebustec06c092006-03-20 13:44:27 -0500234static const struct rpc_call_ops nfs_read_direct_ops = {
235 .rpc_call_done = nfs_direct_read_result,
236 .rpc_release = nfs_readdata_release,
237};
238
Chuck Leverd4cc9482006-03-20 13:44:28 -0500239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * For each nfs_read_data struct that was allocated on the list, dispatch
241 * an NFS READ operation
242 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500243static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct list_head *list = &dreq->list;
246 struct page **pages = dreq->pages;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500247 size_t rsize = NFS_SERVER(inode)->rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsigned int curpage, pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 curpage = 0;
251 pgbase = user_addr & ~PAGE_MASK;
252 do {
253 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500254 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 bytes = rsize;
257 if (count < rsize)
258 bytes = count;
259
260 data = list_entry(list->next, struct nfs_read_data, pages);
261 list_del_init(&data->pages);
262
263 data->inode = inode;
264 data->cred = ctx->cred;
265 data->args.fh = NFS_FH(inode);
266 data->args.context = ctx;
267 data->args.offset = file_offset;
268 data->args.pgbase = pgbase;
269 data->args.pages = &pages[curpage];
270 data->args.count = bytes;
271 data->res.fattr = &data->fattr;
272 data->res.eof = 0;
273 data->res.count = bytes;
274
Trond Myklebustec06c092006-03-20 13:44:27 -0500275 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
276 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 NFS_PROTO(inode)->read_setup(data);
278
279 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 lock_kernel();
282 rpc_execute(&data->task);
283 unlock_kernel();
284
285 dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
286 data->task.tk_pid,
287 inode->i_sb->s_id,
288 (long long)NFS_FILEID(inode),
289 bytes,
290 (unsigned long long)data->args.offset);
291
292 file_offset += bytes;
293 pgbase += bytes;
294 curpage += pgbase >> PAGE_SHIFT;
295 pgbase &= ~PAGE_MASK;
296
297 count -= bytes;
298 } while (count != 0);
299}
300
Chuck Leverd4cc9482006-03-20 13:44:28 -0500301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Collects and returns the final error value/byte-count.
303 */
304static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr)
305{
306 int result = 0;
307
308 if (intr) {
309 result = wait_event_interruptible(dreq->wait,
310 (atomic_read(&dreq->complete) == 0));
311 } else {
312 wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0));
313 }
314
315 if (!result)
316 result = atomic_read(&dreq->error);
317 if (!result)
318 result = atomic_read(&dreq->count);
319
320 kref_put(&dreq->kref, nfs_direct_req_release);
321 return (ssize_t) result;
322}
323
Chuck Leverd4cc9482006-03-20 13:44:28 -0500324static ssize_t nfs_direct_read_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 ssize_t result;
327 sigset_t oldset;
328 struct rpc_clnt *clnt = NFS_CLIENT(inode);
329 struct nfs_direct_req *dreq;
330
331 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
332 if (!dreq)
333 return -ENOMEM;
334
335 dreq->pages = pages;
336 dreq->npages = nr_pages;
Chuck Lever91d5b472006-03-20 13:44:14 -0500337 dreq->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Chuck Lever91d5b472006-03-20 13:44:14 -0500339 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 rpc_clnt_sigmask(clnt, &oldset);
341 nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count,
342 file_offset);
343 result = nfs_direct_read_wait(dreq, clnt->cl_intr);
344 rpc_clnt_sigunmask(clnt, &oldset);
345
346 return result;
347}
348
Chuck Leverd4cc9482006-03-20 13:44:28 -0500349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * We've already pushed out any non-direct writes so that this read
351 * will see them when we read from the server.
352 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500353static ssize_t nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 ssize_t tot_bytes = 0;
356 unsigned long seg = 0;
357
358 while ((seg < nr_segs) && (tot_bytes >= 0)) {
359 ssize_t result;
360 int page_count;
361 struct page **pages;
362 const struct iovec *vec = &iov[seg++];
363 unsigned long user_addr = (unsigned long) vec->iov_base;
364 size_t size = vec->iov_len;
365
366 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
367 if (page_count < 0) {
368 nfs_free_user_pages(pages, 0, 0);
369 if (tot_bytes > 0)
370 break;
371 return page_count;
372 }
373
374 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
375 file_offset, pages, page_count);
376
377 if (result <= 0) {
378 if (tot_bytes > 0)
379 break;
380 return result;
381 }
382 tot_bytes += result;
383 file_offset += result;
384 if (result < size)
385 break;
386 }
387
388 return tot_bytes;
389}
390
Chuck Leverd4cc9482006-03-20 13:44:28 -0500391static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 const unsigned int wsize = NFS_SERVER(inode)->wsize;
394 size_t request;
395 int curpage, need_commit;
396 ssize_t result, tot_bytes;
397 struct nfs_writeverf first_verf;
398 struct nfs_write_data *wdata;
399
Chuck Lever40859d72005-11-30 18:09:02 -0500400 wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!wdata)
402 return -ENOMEM;
403
404 wdata->inode = inode;
405 wdata->cred = ctx->cred;
406 wdata->args.fh = NFS_FH(inode);
407 wdata->args.context = ctx;
408 wdata->args.stable = NFS_UNSTABLE;
409 if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
410 wdata->args.stable = NFS_FILE_SYNC;
411 wdata->res.fattr = &wdata->fattr;
412 wdata->res.verf = &wdata->verf;
413
414 nfs_begin_data_update(inode);
415retry:
416 need_commit = 0;
417 tot_bytes = 0;
418 curpage = 0;
419 request = count;
420 wdata->args.pgbase = user_addr & ~PAGE_MASK;
421 wdata->args.offset = file_offset;
422 do {
423 wdata->args.count = request;
424 if (wdata->args.count > wsize)
425 wdata->args.count = wsize;
426 wdata->args.pages = &pages[curpage];
427
428 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
429 wdata->args.count, (long long) wdata->args.offset,
430 user_addr + tot_bytes, wdata->args.pgbase, curpage);
431
432 lock_kernel();
433 result = NFS_PROTO(inode)->write(wdata);
434 unlock_kernel();
435
436 if (result <= 0) {
437 if (tot_bytes > 0)
438 break;
439 goto out;
440 }
441
442 if (tot_bytes == 0)
443 memcpy(&first_verf.verifier, &wdata->verf.verifier,
444 sizeof(first_verf.verifier));
445 if (wdata->verf.committed != NFS_FILE_SYNC) {
446 need_commit = 1;
447 if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
Dirk Mueller19352452006-02-01 12:19:47 -0500448 sizeof(first_verf.verifier)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto sync_retry;
450 }
451
452 tot_bytes += result;
453
454 /* in case of a short write: stop now, let the app recover */
455 if (result < wdata->args.count)
456 break;
457
458 wdata->args.offset += result;
459 wdata->args.pgbase += result;
460 curpage += wdata->args.pgbase >> PAGE_SHIFT;
461 wdata->args.pgbase &= ~PAGE_MASK;
462 request -= result;
463 } while (request != 0);
464
465 /*
466 * Commit data written so far, even in the event of an error
467 */
468 if (need_commit) {
469 wdata->args.count = tot_bytes;
470 wdata->args.offset = file_offset;
471
472 lock_kernel();
473 result = NFS_PROTO(inode)->commit(wdata);
474 unlock_kernel();
475
476 if (result < 0 || memcmp(&first_verf.verifier,
477 &wdata->verf.verifier,
478 sizeof(first_verf.verifier)) != 0)
479 goto sync_retry;
480 }
481 result = tot_bytes;
482
483out:
Trond Myklebust951a1432005-06-22 17:16:30 +0000484 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 nfs_writedata_free(wdata);
486 return result;
487
488sync_retry:
489 wdata->args.stable = NFS_FILE_SYNC;
490 goto retry;
491}
492
Chuck Leverd4cc9482006-03-20 13:44:28 -0500493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * Upon return, generic_file_direct_IO invalidates any cached pages
495 * that non-direct readers might access, so they will pick up these
496 * writes immediately.
497 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500498static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 ssize_t tot_bytes = 0;
501 unsigned long seg = 0;
502
503 while ((seg < nr_segs) && (tot_bytes >= 0)) {
504 ssize_t result;
505 int page_count;
506 struct page **pages;
507 const struct iovec *vec = &iov[seg++];
508 unsigned long user_addr = (unsigned long) vec->iov_base;
509 size_t size = vec->iov_len;
510
511 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
512 if (page_count < 0) {
513 nfs_free_user_pages(pages, 0, 0);
514 if (tot_bytes > 0)
515 break;
516 return page_count;
517 }
518
Chuck Lever91d5b472006-03-20 13:44:14 -0500519 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
521 file_offset, pages, page_count);
522 nfs_free_user_pages(pages, page_count, 0);
523
524 if (result <= 0) {
525 if (tot_bytes > 0)
526 break;
527 return result;
528 }
Chuck Lever91d5b472006-03-20 13:44:14 -0500529 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 tot_bytes += result;
531 file_offset += result;
532 if (result < size)
533 break;
534 }
535 return tot_bytes;
536}
537
538/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * nfs_file_direct_read - file direct read operation for NFS files
540 * @iocb: target I/O control block
541 * @buf: user's buffer into which to read data
542 * count: number of bytes to read
543 * pos: byte offset in file where reading starts
544 *
545 * We use this function for direct reads instead of calling
546 * generic_file_aio_read() in order to avoid gfar's check to see if
547 * the request starts before the end of the file. For that check
548 * to work, we must generate a GETATTR before each direct read, and
549 * even then there is a window between the GETATTR and the subsequent
550 * READ where the file size could change. So our preference is simply
551 * to do all reads the application wants, and the server will take
552 * care of managing the end of file boundary.
553 *
554 * This function also eliminates unnecessarily updating the file's
555 * atime locally, as the NFS server sets the file's atime, and this
556 * client must read the updated atime from the server back into its
557 * cache.
558 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500559ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 ssize_t retval = -EINVAL;
562 loff_t *ppos = &iocb->ki_pos;
563 struct file *file = iocb->ki_filp;
564 struct nfs_open_context *ctx =
565 (struct nfs_open_context *) file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct address_space *mapping = file->f_mapping;
567 struct inode *inode = mapping->host;
568 struct iovec iov = {
569 .iov_base = buf,
570 .iov_len = count,
571 };
572
Chuck Leverce1a8e62005-11-30 18:08:17 -0500573 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500574 file->f_dentry->d_parent->d_name.name,
575 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500576 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (!is_sync_kiocb(iocb))
579 goto out;
580 if (count < 0)
581 goto out;
582 retval = -EFAULT;
583 if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
584 goto out;
585 retval = 0;
586 if (!count)
587 goto out;
588
Trond Myklebust29884df2005-12-13 16:13:54 -0500589 retval = nfs_sync_mapping(mapping);
590 if (retval)
591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
594 if (retval > 0)
595 *ppos = pos + retval;
596
597out:
598 return retval;
599}
600
601/**
602 * nfs_file_direct_write - file direct write operation for NFS files
603 * @iocb: target I/O control block
604 * @buf: user's buffer from which to write data
605 * count: number of bytes to write
606 * pos: byte offset in file where writing starts
607 *
608 * We use this function for direct writes instead of calling
609 * generic_file_aio_write() in order to avoid taking the inode
610 * semaphore and updating the i_size. The NFS server will set
611 * the new i_size and this client must read the updated size
612 * back into its cache. We let the server do generic write
613 * parameter checking and report problems.
614 *
615 * We also avoid an unnecessary invocation of generic_osync_inode(),
616 * as it is fairly meaningless to sync the metadata of an NFS file.
617 *
618 * We eliminate local atime updates, see direct read above.
619 *
620 * We avoid unnecessary page cache invalidations for normal cached
621 * readers of this file.
622 *
623 * Note that O_APPEND is not supported for NFS direct writes, as there
624 * is no atomic O_APPEND write facility in the NFS protocol.
625 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500626ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500628 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct file *file = iocb->ki_filp;
630 struct nfs_open_context *ctx =
631 (struct nfs_open_context *) file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct address_space *mapping = file->f_mapping;
633 struct inode *inode = mapping->host;
634 struct iovec iov = {
635 .iov_base = (char __user *)buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 };
637
Chuck Leverce1a8e62005-11-30 18:08:17 -0500638 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500639 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500640 file->f_dentry->d_name.name,
641 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Chuck Leverce1a8e62005-11-30 18:08:17 -0500643 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!is_sync_kiocb(iocb))
645 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500646
647 retval = generic_write_checks(file, &pos, &count, 0);
648 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500650
651 retval = -EINVAL;
652 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 retval = 0;
655 if (!count)
656 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500657 iov.iov_len = count,
658
659 retval = -EFAULT;
660 if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Trond Myklebust29884df2005-12-13 16:13:54 -0500663 retval = nfs_sync_mapping(mapping);
664 if (retval)
665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
668 if (mapping->nrpages)
669 invalidate_inode_pages2(mapping);
670 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500671 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673out:
674 return retval;
675}
676
677int nfs_init_directcache(void)
678{
679 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
680 sizeof(struct nfs_direct_req),
681 0, SLAB_RECLAIM_ACCOUNT,
682 NULL, NULL);
683 if (nfs_direct_cachep == NULL)
684 return -ENOMEM;
685
686 return 0;
687}
688
689void nfs_destroy_directcache(void)
690{
691 if (kmem_cache_destroy(nfs_direct_cachep))
692 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
693}