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