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