[PATCH] invalidate_complete_page() race fix
[deliverable/linux.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
88467055 10 * (multiple copies of the same instance running on separate hosts)
1da177e4 11 * implement their own cache coherency protocol that subsumes file
88467055
CL
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
1da177e4
LT
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
88467055 37 * 04 May 2005 support O_DIRECT with aio --cel
1da177e4
LT
38 *
39 */
40
1da177e4
LT
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 59#define NFSDBG_FACILITY NFSDBG_VFS
1da177e4
LT
60
61static kmem_cache_t *nfs_direct_cachep;
62
63/*
64 * This represents a set of asynchronous requests that we're waiting on
65 */
66struct nfs_direct_req {
67 struct kref kref; /* release manager */
15ce4a0c
CL
68
69 /* I/O parameters */
a8881f5a 70 struct nfs_open_context *ctx; /* file open context info */
99514f8f 71 struct kiocb * iocb; /* controlling i/o request */
88467055 72 struct inode * inode; /* target file of i/o */
15ce4a0c
CL
73
74 /* completion state */
607f31e8 75 atomic_t io_count; /* i/os we're waiting for */
15ce4a0c 76 spinlock_t lock; /* protect completion state */
15ce4a0c 77 ssize_t count, /* bytes actually processed */
1da177e4 78 error; /* any reported error */
d72b7a6b 79 struct completion completion; /* wait for i/o completion */
fad61490
TM
80
81 /* commit state */
607f31e8 82 struct list_head rewrite_list; /* saved nfs_write_data structs */
fad61490
TM
83 struct nfs_write_data * commit_data; /* special write_data for commits */
84 int flags;
85#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
86#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
87 struct nfs_writeverf verf; /* unstable write verifier */
1da177e4
LT
88};
89
fad61490 90static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
607f31e8
TM
91static const struct rpc_call_ops nfs_write_direct_ops;
92
93static inline void get_dreq(struct nfs_direct_req *dreq)
94{
95 atomic_inc(&dreq->io_count);
96}
97
98static inline int put_dreq(struct nfs_direct_req *dreq)
99{
100 return atomic_dec_and_test(&dreq->io_count);
101}
102
103/*
104 * "size" is never larger than rsize or wsize.
105 */
106static inline int nfs_direct_count_pages(unsigned long user_addr, size_t size)
107{
108 int page_count;
109
110 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
111 page_count -= user_addr >> PAGE_SHIFT;
112 BUG_ON(page_count < 0);
113
114 return page_count;
115}
116
117static inline unsigned int nfs_max_pages(unsigned int size)
118{
119 return (size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
120}
82b145c5 121
1da177e4 122/**
b8a32e2b
CL
123 * nfs_direct_IO - NFS address space operation for direct I/O
124 * @rw: direction (read or write)
125 * @iocb: target I/O control block
126 * @iov: array of vectors that define I/O buffer
127 * @pos: offset in file to begin the operation
128 * @nr_segs: size of iovec array
129 *
130 * The presence of this routine in the address space ops vector means
131 * the NFS client supports direct I/O. However, we shunt off direct
132 * read and write requests before the VFS gets them, so this method
133 * should never be called.
1da177e4 134 */
b8a32e2b
CL
135ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
136{
b8a32e2b 137 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
e99170ff
TM
138 iocb->ki_filp->f_dentry->d_name.name,
139 (long long) pos, nr_segs);
b8a32e2b
CL
140
141 return -EINVAL;
142}
143
607f31e8 144static void nfs_direct_dirty_pages(struct page **pages, int npages)
6b45d858
TM
145{
146 int i;
147 for (i = 0; i < npages; i++) {
148 struct page *page = pages[i];
607f31e8 149 if (!PageCompound(page))
6b45d858 150 set_page_dirty_lock(page);
6b45d858 151 }
9c93ab7d
CL
152}
153
607f31e8 154static void nfs_direct_release_pages(struct page **pages, int npages)
9c93ab7d 155{
607f31e8
TM
156 int i;
157 for (i = 0; i < npages; i++)
158 page_cache_release(pages[i]);
6b45d858
TM
159}
160
93619e59 161static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
1da177e4 162{
93619e59
CL
163 struct nfs_direct_req *dreq;
164
165 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
166 if (!dreq)
167 return NULL;
168
169 kref_init(&dreq->kref);
607f31e8 170 kref_get(&dreq->kref);
d72b7a6b 171 init_completion(&dreq->completion);
fad61490 172 INIT_LIST_HEAD(&dreq->rewrite_list);
93619e59 173 dreq->iocb = NULL;
a8881f5a 174 dreq->ctx = NULL;
15ce4a0c 175 spin_lock_init(&dreq->lock);
607f31e8 176 atomic_set(&dreq->io_count, 0);
15ce4a0c
CL
177 dreq->count = 0;
178 dreq->error = 0;
fad61490 179 dreq->flags = 0;
93619e59
CL
180
181 return dreq;
1da177e4
LT
182}
183
1da177e4
LT
184static void nfs_direct_req_release(struct kref *kref)
185{
186 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
a8881f5a
TM
187
188 if (dreq->ctx != NULL)
189 put_nfs_open_context(dreq->ctx);
1da177e4
LT
190 kmem_cache_free(nfs_direct_cachep, dreq);
191}
192
bc0fb201
CL
193/*
194 * Collects and returns the final error value/byte-count.
195 */
196static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
197{
15ce4a0c 198 ssize_t result = -EIOCBQUEUED;
bc0fb201
CL
199
200 /* Async requests don't wait here */
201 if (dreq->iocb)
202 goto out;
203
d72b7a6b 204 result = wait_for_completion_interruptible(&dreq->completion);
bc0fb201
CL
205
206 if (!result)
15ce4a0c 207 result = dreq->error;
bc0fb201 208 if (!result)
15ce4a0c 209 result = dreq->count;
bc0fb201
CL
210
211out:
212 kref_put(&dreq->kref, nfs_direct_req_release);
213 return (ssize_t) result;
214}
215
63ab46ab 216/*
607f31e8
TM
217 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
218 * the iocb is still valid here if this is a synchronous request.
63ab46ab
CL
219 */
220static void nfs_direct_complete(struct nfs_direct_req *dreq)
221{
63ab46ab 222 if (dreq->iocb) {
15ce4a0c 223 long res = (long) dreq->error;
63ab46ab 224 if (!res)
15ce4a0c 225 res = (long) dreq->count;
63ab46ab 226 aio_complete(dreq->iocb, res, 0);
d72b7a6b
TM
227 }
228 complete_all(&dreq->completion);
63ab46ab
CL
229
230 kref_put(&dreq->kref, nfs_direct_req_release);
231}
232
06cf6f2e 233/*
607f31e8
TM
234 * We must hold a reference to all the pages in this direct read request
235 * until the RPCs complete. This could be long *after* we are woken up in
236 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
06cf6f2e 237 */
ec06c096 238static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
1da177e4 239{
ec06c096 240 struct nfs_read_data *data = calldata;
1da177e4
LT
241 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
242
ec06c096
TM
243 if (nfs_readpage_result(task, data) != 0)
244 return;
15ce4a0c 245
607f31e8
TM
246 nfs_direct_dirty_pages(data->pagevec, data->npages);
247 nfs_direct_release_pages(data->pagevec, data->npages);
248
15ce4a0c
CL
249 spin_lock(&dreq->lock);
250
ec06c096 251 if (likely(task->tk_status >= 0))
15ce4a0c 252 dreq->count += data->res.count;
1da177e4 253 else
15ce4a0c 254 dreq->error = task->tk_status;
1da177e4 255
ccf01ef7 256 spin_unlock(&dreq->lock);
607f31e8
TM
257
258 if (put_dreq(dreq))
259 nfs_direct_complete(dreq);
1da177e4
LT
260}
261
ec06c096
TM
262static const struct rpc_call_ops nfs_read_direct_ops = {
263 .rpc_call_done = nfs_direct_read_result,
264 .rpc_release = nfs_readdata_release,
265};
266
d4cc948b 267/*
607f31e8
TM
268 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
269 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
270 * bail and stop sending more reads. Read length accounting is
271 * handled automatically by nfs_direct_read_result(). Otherwise, if
272 * no requests have been sent, just return an error.
1da177e4 273 */
607f31e8 274static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
1da177e4 275{
a8881f5a
TM
276 struct nfs_open_context *ctx = dreq->ctx;
277 struct inode *inode = ctx->dentry->d_inode;
5dd602f2 278 size_t rsize = NFS_SERVER(inode)->rsize;
607f31e8
TM
279 unsigned int rpages = nfs_max_pages(rsize);
280 unsigned int pgbase;
281 int result;
282 ssize_t started = 0;
283
284 get_dreq(dreq);
1da177e4 285
607f31e8 286 pgbase = user_addr & ~PAGE_MASK;
1da177e4 287 do {
82b145c5 288 struct nfs_read_data *data;
5dd602f2 289 size_t bytes;
1da177e4 290
607f31e8
TM
291 result = -ENOMEM;
292 data = nfs_readdata_alloc(rpages);
293 if (unlikely(!data))
294 break;
295
1da177e4
LT
296 bytes = rsize;
297 if (count < rsize)
298 bytes = count;
299
607f31e8
TM
300 data->npages = nfs_direct_count_pages(user_addr, bytes);
301 down_read(&current->mm->mmap_sem);
302 result = get_user_pages(current, current->mm, user_addr,
303 data->npages, 1, 0, data->pagevec, NULL);
304 up_read(&current->mm->mmap_sem);
305 if (unlikely(result < data->npages)) {
306 if (result > 0)
307 nfs_direct_release_pages(data->pagevec, result);
308 nfs_readdata_release(data);
309 break;
310 }
311
312 get_dreq(dreq);
82b145c5 313
607f31e8 314 data->req = (struct nfs_page *) dreq;
1da177e4
LT
315 data->inode = inode;
316 data->cred = ctx->cred;
317 data->args.fh = NFS_FH(inode);
318 data->args.context = ctx;
88467055 319 data->args.offset = pos;
1da177e4 320 data->args.pgbase = pgbase;
607f31e8 321 data->args.pages = data->pagevec;
1da177e4
LT
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
606bbba0 337 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
1da177e4
LT
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
607f31e8
TM
344 started += bytes;
345 user_addr += bytes;
88467055 346 pos += bytes;
1da177e4 347 pgbase += bytes;
1da177e4
LT
348 pgbase &= ~PAGE_MASK;
349
350 count -= bytes;
351 } while (count != 0);
607f31e8
TM
352
353 if (put_dreq(dreq))
354 nfs_direct_complete(dreq);
355
356 if (started)
357 return 0;
358 return result < 0 ? (ssize_t) result : -EFAULT;
1da177e4
LT
359}
360
607f31e8 361static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
1da177e4 362{
607f31e8 363 ssize_t result = 0;
1da177e4 364 sigset_t oldset;
99514f8f 365 struct inode *inode = iocb->ki_filp->f_mapping->host;
1da177e4
LT
366 struct rpc_clnt *clnt = NFS_CLIENT(inode);
367 struct nfs_direct_req *dreq;
368
607f31e8 369 dreq = nfs_direct_req_alloc();
1da177e4
LT
370 if (!dreq)
371 return -ENOMEM;
372
91d5b470 373 dreq->inode = inode;
a8881f5a 374 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
487b8372
CL
375 if (!is_sync_kiocb(iocb))
376 dreq->iocb = iocb;
1da177e4 377
91d5b470 378 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
1da177e4 379 rpc_clnt_sigmask(clnt, &oldset);
607f31e8
TM
380 result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
381 if (!result)
382 result = nfs_direct_wait(dreq);
1da177e4
LT
383 rpc_clnt_sigunmask(clnt, &oldset);
384
385 return result;
386}
387
fad61490 388static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
1da177e4 389{
607f31e8
TM
390 while (!list_empty(&dreq->rewrite_list)) {
391 struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
fad61490 392 list_del(&data->pages);
607f31e8 393 nfs_direct_release_pages(data->pagevec, data->npages);
fad61490
TM
394 nfs_writedata_release(data);
395 }
396}
1da177e4 397
fad61490
TM
398#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
399static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
400{
607f31e8
TM
401 struct inode *inode = dreq->inode;
402 struct list_head *p;
403 struct nfs_write_data *data;
1da177e4 404
fad61490 405 dreq->count = 0;
607f31e8
TM
406 get_dreq(dreq);
407
408 list_for_each(p, &dreq->rewrite_list) {
409 data = list_entry(p, struct nfs_write_data, pages);
410
411 get_dreq(dreq);
412
413 /*
414 * Reset data->res.
415 */
416 nfs_fattr_init(&data->fattr);
417 data->res.count = data->args.count;
418 memset(&data->verf, 0, sizeof(data->verf));
419
420 /*
421 * Reuse data->task; data->args should not have changed
422 * since the original request was sent.
423 */
424 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
425 &nfs_write_direct_ops, data);
426 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
427
428 data->task.tk_priority = RPC_PRIORITY_NORMAL;
429 data->task.tk_cookie = (unsigned long) inode;
430
431 /*
432 * We're called via an RPC callback, so BKL is already held.
433 */
434 rpc_execute(&data->task);
435
436 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
437 data->task.tk_pid,
438 inode->i_sb->s_id,
439 (long long)NFS_FILEID(inode),
440 data->args.count,
441 (unsigned long long)data->args.offset);
442 }
fedb595c 443
607f31e8
TM
444 if (put_dreq(dreq))
445 nfs_direct_write_complete(dreq, inode);
fad61490
TM
446}
447
448static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
449{
450 struct nfs_write_data *data = calldata;
451 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
452
453 /* Call the NFS version-specific code */
454 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
455 return;
456 if (unlikely(task->tk_status < 0)) {
457 dreq->error = task->tk_status;
458 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
459 }
460 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
461 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
462 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
1da177e4
LT
463 }
464
fad61490
TM
465 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
466 nfs_direct_write_complete(dreq, data->inode);
1da177e4
LT
467}
468
fad61490
TM
469static const struct rpc_call_ops nfs_commit_direct_ops = {
470 .rpc_call_done = nfs_direct_commit_result,
471 .rpc_release = nfs_commit_release,
472};
473
474static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
1da177e4 475{
fad61490 476 struct nfs_write_data *data = dreq->commit_data;
1da177e4 477
fad61490 478 data->inode = dreq->inode;
a8881f5a 479 data->cred = dreq->ctx->cred;
1da177e4 480
fad61490 481 data->args.fh = NFS_FH(data->inode);
607f31e8
TM
482 data->args.offset = 0;
483 data->args.count = 0;
fad61490
TM
484 data->res.count = 0;
485 data->res.fattr = &data->fattr;
486 data->res.verf = &data->verf;
1da177e4 487
fad61490
TM
488 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
489 &nfs_commit_direct_ops, data);
490 NFS_PROTO(data->inode)->commit_setup(data, 0);
1da177e4 491
fad61490
TM
492 data->task.tk_priority = RPC_PRIORITY_NORMAL;
493 data->task.tk_cookie = (unsigned long)data->inode;
494 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
495 dreq->commit_data = NULL;
1da177e4 496
e99170ff 497 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1da177e4 498
fad61490
TM
499 lock_kernel();
500 rpc_execute(&data->task);
501 unlock_kernel();
502}
1da177e4 503
fad61490
TM
504static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
505{
506 int flags = dreq->flags;
1da177e4 507
fad61490
TM
508 dreq->flags = 0;
509 switch (flags) {
510 case NFS_ODIRECT_DO_COMMIT:
511 nfs_direct_commit_schedule(dreq);
1da177e4 512 break;
fad61490
TM
513 case NFS_ODIRECT_RESCHED_WRITES:
514 nfs_direct_write_reschedule(dreq);
515 break;
516 default:
517 nfs_end_data_update(inode);
518 if (dreq->commit_data != NULL)
519 nfs_commit_free(dreq->commit_data);
520 nfs_direct_free_writedata(dreq);
521 nfs_direct_complete(dreq);
522 }
523}
1da177e4 524
fad61490
TM
525static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
526{
527 dreq->commit_data = nfs_commit_alloc(0);
528 if (dreq->commit_data != NULL)
529 dreq->commit_data->req = (struct nfs_page *) dreq;
530}
531#else
532static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
533{
534 dreq->commit_data = NULL;
535}
1da177e4 536
fad61490
TM
537static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
538{
539 nfs_end_data_update(inode);
540 nfs_direct_free_writedata(dreq);
541 nfs_direct_complete(dreq);
542}
543#endif
1da177e4 544
462d5b32 545static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
1da177e4 546{
462d5b32
CL
547 struct nfs_write_data *data = calldata;
548 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
549 int status = task->tk_status;
550
551 if (nfs_writeback_done(task, data) != 0)
552 return;
462d5b32 553
15ce4a0c 554 spin_lock(&dreq->lock);
1da177e4 555
462d5b32 556 if (likely(status >= 0))
15ce4a0c 557 dreq->count += data->res.count;
462d5b32 558 else
fad61490 559 dreq->error = task->tk_status;
1da177e4 560
fad61490
TM
561 if (data->res.verf->committed != NFS_FILE_SYNC) {
562 switch (dreq->flags) {
563 case 0:
564 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
565 dreq->flags = NFS_ODIRECT_DO_COMMIT;
1da177e4 566 break;
fad61490
TM
567 case NFS_ODIRECT_DO_COMMIT:
568 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
569 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
570 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
571 }
1da177e4 572 }
1da177e4 573 }
fad61490
TM
574
575 spin_unlock(&dreq->lock);
1da177e4
LT
576}
577
fad61490
TM
578/*
579 * NB: Return the value of the first error return code. Subsequent
580 * errors after the first one are ignored.
1da177e4 581 */
fad61490 582static void nfs_direct_write_release(void *calldata)
1da177e4 583{
fad61490
TM
584 struct nfs_write_data *data = calldata;
585 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
1da177e4 586
607f31e8
TM
587 if (put_dreq(dreq))
588 nfs_direct_write_complete(dreq, data->inode);
462d5b32
CL
589}
590
591static const struct rpc_call_ops nfs_write_direct_ops = {
592 .rpc_call_done = nfs_direct_write_result,
fad61490 593 .rpc_release = nfs_direct_write_release,
462d5b32
CL
594};
595
596/*
607f31e8
TM
597 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
598 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
599 * bail and stop sending more writes. Write length accounting is
600 * handled automatically by nfs_direct_write_result(). Otherwise, if
601 * no requests have been sent, just return an error.
462d5b32 602 */
607f31e8 603static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
462d5b32 604{
a8881f5a
TM
605 struct nfs_open_context *ctx = dreq->ctx;
606 struct inode *inode = ctx->dentry->d_inode;
462d5b32 607 size_t wsize = NFS_SERVER(inode)->wsize;
607f31e8
TM
608 unsigned int wpages = nfs_max_pages(wsize);
609 unsigned int pgbase;
610 int result;
611 ssize_t started = 0;
82b145c5 612
607f31e8
TM
613 get_dreq(dreq);
614
615 pgbase = user_addr & ~PAGE_MASK;
1da177e4 616 do {
82b145c5 617 struct nfs_write_data *data;
462d5b32
CL
618 size_t bytes;
619
607f31e8
TM
620 result = -ENOMEM;
621 data = nfs_writedata_alloc(wpages);
622 if (unlikely(!data))
623 break;
624
462d5b32
CL
625 bytes = wsize;
626 if (count < wsize)
627 bytes = count;
628
607f31e8
TM
629 data->npages = nfs_direct_count_pages(user_addr, bytes);
630 down_read(&current->mm->mmap_sem);
631 result = get_user_pages(current, current->mm, user_addr,
632 data->npages, 0, 0, data->pagevec, NULL);
633 up_read(&current->mm->mmap_sem);
634 if (unlikely(result < data->npages)) {
635 if (result > 0)
636 nfs_direct_release_pages(data->pagevec, result);
637 nfs_writedata_release(data);
638 break;
639 }
640
641 get_dreq(dreq);
642
fad61490 643 list_move_tail(&data->pages, &dreq->rewrite_list);
462d5b32 644
607f31e8 645 data->req = (struct nfs_page *) dreq;
462d5b32
CL
646 data->inode = inode;
647 data->cred = ctx->cred;
648 data->args.fh = NFS_FH(inode);
649 data->args.context = ctx;
88467055 650 data->args.offset = pos;
462d5b32 651 data->args.pgbase = pgbase;
607f31e8 652 data->args.pages = data->pagevec;
462d5b32
CL
653 data->args.count = bytes;
654 data->res.fattr = &data->fattr;
655 data->res.count = bytes;
47989d74 656 data->res.verf = &data->verf;
462d5b32
CL
657
658 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
659 &nfs_write_direct_ops, data);
fad61490 660 NFS_PROTO(inode)->write_setup(data, sync);
1da177e4 661
462d5b32
CL
662 data->task.tk_priority = RPC_PRIORITY_NORMAL;
663 data->task.tk_cookie = (unsigned long) inode;
1da177e4
LT
664
665 lock_kernel();
462d5b32 666 rpc_execute(&data->task);
1da177e4
LT
667 unlock_kernel();
668
606bbba0 669 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
462d5b32
CL
670 data->task.tk_pid,
671 inode->i_sb->s_id,
672 (long long)NFS_FILEID(inode),
673 bytes,
674 (unsigned long long)data->args.offset);
1da177e4 675
607f31e8
TM
676 started += bytes;
677 user_addr += bytes;
88467055 678 pos += bytes;
462d5b32 679 pgbase += bytes;
462d5b32 680 pgbase &= ~PAGE_MASK;
1da177e4 681
462d5b32
CL
682 count -= bytes;
683 } while (count != 0);
607f31e8
TM
684
685 if (put_dreq(dreq))
686 nfs_direct_write_complete(dreq, inode);
687
688 if (started)
689 return 0;
690 return result < 0 ? (ssize_t) result : -EFAULT;
462d5b32 691}
1da177e4 692
607f31e8 693static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
462d5b32 694{
607f31e8 695 ssize_t result = 0;
462d5b32 696 sigset_t oldset;
c89f2ee5 697 struct inode *inode = iocb->ki_filp->f_mapping->host;
462d5b32
CL
698 struct rpc_clnt *clnt = NFS_CLIENT(inode);
699 struct nfs_direct_req *dreq;
fad61490
TM
700 size_t wsize = NFS_SERVER(inode)->wsize;
701 int sync = 0;
1da177e4 702
607f31e8 703 dreq = nfs_direct_req_alloc();
462d5b32
CL
704 if (!dreq)
705 return -ENOMEM;
607f31e8
TM
706 nfs_alloc_commit_data(dreq);
707
fad61490
TM
708 if (dreq->commit_data == NULL || count < wsize)
709 sync = FLUSH_STABLE;
1da177e4 710
c89f2ee5 711 dreq->inode = inode;
a8881f5a 712 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
c89f2ee5
CL
713 if (!is_sync_kiocb(iocb))
714 dreq->iocb = iocb;
1da177e4 715
47989d74
CL
716 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
717
462d5b32 718 nfs_begin_data_update(inode);
1da177e4 719
462d5b32 720 rpc_clnt_sigmask(clnt, &oldset);
607f31e8
TM
721 result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
722 if (!result)
723 result = nfs_direct_wait(dreq);
462d5b32 724 rpc_clnt_sigunmask(clnt, &oldset);
1da177e4 725
1da177e4
LT
726 return result;
727}
728
729/**
730 * nfs_file_direct_read - file direct read operation for NFS files
731 * @iocb: target I/O control block
732 * @buf: user's buffer into which to read data
88467055
CL
733 * @count: number of bytes to read
734 * @pos: byte offset in file where reading starts
1da177e4
LT
735 *
736 * We use this function for direct reads instead of calling
737 * generic_file_aio_read() in order to avoid gfar's check to see if
738 * the request starts before the end of the file. For that check
739 * to work, we must generate a GETATTR before each direct read, and
740 * even then there is a window between the GETATTR and the subsequent
88467055 741 * READ where the file size could change. Our preference is simply
1da177e4
LT
742 * to do all reads the application wants, and the server will take
743 * care of managing the end of file boundary.
88467055 744 *
1da177e4
LT
745 * This function also eliminates unnecessarily updating the file's
746 * atime locally, as the NFS server sets the file's atime, and this
747 * client must read the updated atime from the server back into its
748 * cache.
749 */
d4cc948b 750ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
1da177e4
LT
751{
752 ssize_t retval = -EINVAL;
1da177e4 753 struct file *file = iocb->ki_filp;
1da177e4 754 struct address_space *mapping = file->f_mapping;
1da177e4 755
ce1a8e67 756 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
0bbacc40
CL
757 file->f_dentry->d_parent->d_name.name,
758 file->f_dentry->d_name.name,
ce1a8e67 759 (unsigned long) count, (long long) pos);
1da177e4 760
1da177e4
LT
761 if (count < 0)
762 goto out;
763 retval = -EFAULT;
0cdd80d0 764 if (!access_ok(VERIFY_WRITE, buf, count))
1da177e4
LT
765 goto out;
766 retval = 0;
767 if (!count)
768 goto out;
769
29884df0
TM
770 retval = nfs_sync_mapping(mapping);
771 if (retval)
772 goto out;
1da177e4 773
607f31e8 774 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
1da177e4 775 if (retval > 0)
0cdd80d0 776 iocb->ki_pos = pos + retval;
1da177e4
LT
777
778out:
779 return retval;
780}
781
782/**
783 * nfs_file_direct_write - file direct write operation for NFS files
784 * @iocb: target I/O control block
785 * @buf: user's buffer from which to write data
88467055
CL
786 * @count: number of bytes to write
787 * @pos: byte offset in file where writing starts
1da177e4
LT
788 *
789 * We use this function for direct writes instead of calling
790 * generic_file_aio_write() in order to avoid taking the inode
791 * semaphore and updating the i_size. The NFS server will set
792 * the new i_size and this client must read the updated size
793 * back into its cache. We let the server do generic write
794 * parameter checking and report problems.
795 *
796 * We also avoid an unnecessary invocation of generic_osync_inode(),
797 * as it is fairly meaningless to sync the metadata of an NFS file.
798 *
799 * We eliminate local atime updates, see direct read above.
800 *
801 * We avoid unnecessary page cache invalidations for normal cached
802 * readers of this file.
803 *
804 * Note that O_APPEND is not supported for NFS direct writes, as there
805 * is no atomic O_APPEND write facility in the NFS protocol.
806 */
d4cc948b 807ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
1da177e4 808{
ce1a8e67 809 ssize_t retval;
1da177e4 810 struct file *file = iocb->ki_filp;
1da177e4 811 struct address_space *mapping = file->f_mapping;
1da177e4 812
ce1a8e67 813 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
0bbacc40 814 file->f_dentry->d_parent->d_name.name,
ce1a8e67
CL
815 file->f_dentry->d_name.name,
816 (unsigned long) count, (long long) pos);
1da177e4 817
ce1a8e67
CL
818 retval = generic_write_checks(file, &pos, &count, 0);
819 if (retval)
1da177e4 820 goto out;
ce1a8e67
CL
821
822 retval = -EINVAL;
823 if ((ssize_t) count < 0)
1da177e4 824 goto out;
1da177e4
LT
825 retval = 0;
826 if (!count)
827 goto out;
ce1a8e67
CL
828
829 retval = -EFAULT;
47989d74 830 if (!access_ok(VERIFY_READ, buf, count))
ce1a8e67 831 goto out;
1da177e4 832
29884df0
TM
833 retval = nfs_sync_mapping(mapping);
834 if (retval)
835 goto out;
1da177e4 836
607f31e8 837 retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
9eafa8cc
CL
838
839 /*
840 * XXX: nfs_end_data_update() already ensures this file's
841 * cached data is subsequently invalidated. Do we really
842 * need to call invalidate_inode_pages2() again here?
843 *
844 * For aio writes, this invalidation will almost certainly
845 * occur before the writes complete. Kind of racey.
846 */
1da177e4
LT
847 if (mapping->nrpages)
848 invalidate_inode_pages2(mapping);
9eafa8cc 849
1da177e4 850 if (retval > 0)
ce1a8e67 851 iocb->ki_pos = pos + retval;
1da177e4
LT
852
853out:
854 return retval;
855}
856
88467055
CL
857/**
858 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
859 *
860 */
f7b422b1 861int __init nfs_init_directcache(void)
1da177e4
LT
862{
863 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
864 sizeof(struct nfs_direct_req),
fffb60f9
PJ
865 0, (SLAB_RECLAIM_ACCOUNT|
866 SLAB_MEM_SPREAD),
1da177e4
LT
867 NULL, NULL);
868 if (nfs_direct_cachep == NULL)
869 return -ENOMEM;
870
871 return 0;
872}
873
88467055 874/**
f7b422b1 875 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
88467055
CL
876 *
877 */
266bee88 878void nfs_destroy_directcache(void)
1da177e4
LT
879{
880 if (kmem_cache_destroy(nfs_direct_cachep))
881 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
882}
This page took 0.201858 seconds and 5 git commands to generate.