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