upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / 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  *
37  */
38
39 #include <linux/config.h>
40 #include <linux/errno.h>
41 #include <linux/sched.h>
42 #include <linux/kernel.h>
43 #include <linux/smp_lock.h>
44 #include <linux/file.h>
45 #include <linux/pagemap.h>
46
47 #include <linux/nfs_fs.h>
48 #include <linux/nfs_page.h>
49 #include <linux/sunrpc/clnt.h>
50
51 #include <asm/system.h>
52 #include <asm/uaccess.h>
53
54 #define NFSDBG_FACILITY         NFSDBG_VFS
55 #define VERF_SIZE               (2 * sizeof(__u32))
56 #define MAX_DIRECTIO_SIZE       (4096UL << PAGE_SHIFT)
57
58
59 /**
60  * nfs_get_user_pages - find and set up pages underlying user's buffer
61  * rw: direction (read or write)
62  * user_addr: starting address of this segment of user's buffer
63  * count: size of this segment
64  * @pages: returned array of page struct pointers underlying user's buffer
65  */
66 static inline int
67 nfs_get_user_pages(int rw, unsigned long user_addr, size_t size,
68                 struct page ***pages)
69 {
70         int result = -ENOMEM;
71         unsigned long page_count;
72         size_t array_size;
73
74         /* set an arbitrary limit to prevent arithmetic overflow */
75         if (size > MAX_DIRECTIO_SIZE) {
76                 *pages = NULL;
77                 return -EFBIG;
78         }
79
80         page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
81         page_count -= user_addr >> PAGE_SHIFT;
82
83         array_size = (page_count * sizeof(struct page *));
84         *pages = kmalloc(array_size, GFP_KERNEL);
85         if (*pages) {
86                 down_read(&current->mm->mmap_sem);
87                 result = get_user_pages(current, current->mm, user_addr,
88                                         page_count, (rw == READ), 0,
89                                         *pages, NULL);
90                 up_read(&current->mm->mmap_sem);
91         }
92         return result;
93 }
94
95 /**
96  * nfs_free_user_pages - tear down page struct array
97  * @pages: array of page struct pointers underlying target buffer
98  */
99 static void
100 nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
101 {
102         int i;
103         for (i = 0; i < npages; i++) {
104                 if (do_dirty)
105                         set_page_dirty_lock(pages[i]);
106                 page_cache_release(pages[i]);
107         }
108         kfree(pages);
109 }
110
111 /**
112  * nfs_direct_read_seg - Read in one iov segment.  Generate separate
113  *                        read RPCs for each "rsize" bytes.
114  * @inode: target inode
115  * @ctx: target file open context
116  * user_addr: starting address of this segment of user's buffer
117  * count: size of this segment
118  * file_offset: offset in file to begin the operation
119  * @pages: array of addresses of page structs defining user's buffer
120  * nr_pages: size of pages array
121  */
122 static int
123 nfs_direct_read_seg(struct inode *inode, struct nfs_open_context *ctx,
124                 unsigned long user_addr, size_t count, loff_t file_offset,
125                 struct page **pages, int nr_pages)
126 {
127         const unsigned int rsize = NFS_SERVER(inode)->rsize;
128         int tot_bytes = 0;
129         int curpage = 0;
130         struct nfs_read_data    rdata = {
131                 .inode          = inode,
132                 .cred           = ctx->cred,
133                 .args           = {
134                         .fh             = NFS_FH(inode),
135                         .context        = ctx,
136                 },
137                 .res            = {
138                         .fattr          = &rdata.fattr,
139                 },
140         };
141
142         rdata.args.pgbase = user_addr & ~PAGE_MASK;
143         rdata.args.offset = file_offset;
144         do {
145                 int result;
146
147                 rdata.args.count = count;
148                 if (rdata.args.count > rsize)
149                         rdata.args.count = rsize;
150                 rdata.args.pages = &pages[curpage];
151
152                 dprintk("NFS: direct read: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
153                         rdata.args.count, (long long) rdata.args.offset,
154                         user_addr + tot_bytes, rdata.args.pgbase, curpage);
155
156                 lock_kernel();
157                 result = NFS_PROTO(inode)->read(&rdata);
158                 unlock_kernel();
159
160                 if (result <= 0) {
161                         if (tot_bytes > 0)
162                                 break;
163                         if (result == -EISDIR)
164                                 result = -EINVAL;
165                         return result;
166                 }
167
168                 tot_bytes += result;
169                 if (rdata.res.eof)
170                         break;
171
172                 rdata.args.offset += result;
173                 rdata.args.pgbase += result;
174                 curpage += rdata.args.pgbase >> PAGE_SHIFT;
175                 rdata.args.pgbase &= ~PAGE_MASK;
176                 count -= result;
177         } while (count != 0);
178
179         /* XXX: should we zero the rest of the user's buffer if we
180          *      hit eof? */
181
182         return tot_bytes;
183 }
184
185 /**
186  * nfs_direct_read - For each iov segment, map the user's buffer
187  *                   then generate read RPCs.
188  * @inode: target inode
189  * @ctx: target file open context
190  * @iov: array of vectors that define I/O buffer
191  * file_offset: offset in file to begin the operation
192  * nr_segs: size of iovec array
193  *
194  * generic_file_direct_IO has already pushed out any non-direct
195  * writes so that this read will see them when we read from the
196  * server.
197  */
198 static ssize_t
199 nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx,
200                 const struct iovec *iov, loff_t file_offset,
201                 unsigned long nr_segs)
202 {
203         ssize_t tot_bytes = 0;
204         unsigned long seg = 0;
205
206         while ((seg < nr_segs) && (tot_bytes >= 0)) {
207                 ssize_t result;
208                 int page_count;
209                 struct page **pages;
210                 const struct iovec *vec = &iov[seg++];
211                 unsigned long user_addr = (unsigned long) vec->iov_base;
212                 size_t size = vec->iov_len;
213
214                 page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
215                 if (page_count < 0) {
216                         nfs_free_user_pages(pages, 0, 0);
217                         if (tot_bytes > 0)
218                                 break;
219                         return page_count;
220                 }
221
222                 result = nfs_direct_read_seg(inode, ctx, user_addr, size,
223                                 file_offset, pages, page_count);
224
225                 nfs_free_user_pages(pages, page_count, 1);
226
227                 if (result <= 0) {
228                         if (tot_bytes > 0)
229                                 break;
230                         return result;
231                 }
232                 tot_bytes += result;
233                 file_offset += result;
234                 if (result < size)
235                         break;
236         }
237
238         return tot_bytes;
239 }
240
241 /**
242  * nfs_direct_write_seg - Write out one iov segment.  Generate separate
243  *                        write RPCs for each "wsize" bytes, then commit.
244  * @inode: target inode
245  * @ctx: target file open context
246  * user_addr: starting address of this segment of user's buffer
247  * count: size of this segment
248  * file_offset: offset in file to begin the operation
249  * @pages: array of addresses of page structs defining user's buffer
250  * nr_pages: size of pages array
251  */
252 static int
253 nfs_direct_write_seg(struct inode *inode, struct nfs_open_context *ctx,
254                 unsigned long user_addr, size_t count, loff_t file_offset,
255                 struct page **pages, int nr_pages)
256 {
257         const unsigned int wsize = NFS_SERVER(inode)->wsize;
258         size_t request;
259         int curpage, need_commit, result, tot_bytes;
260         struct nfs_writeverf first_verf;
261         struct nfs_write_data   wdata = {
262                 .inode          = inode,
263                 .cred           = ctx->cred,
264                 .args           = {
265                         .fh             = NFS_FH(inode),
266                         .context        = ctx,
267                 },
268                 .res            = {
269                         .fattr          = &wdata.fattr,
270                         .verf           = &wdata.verf,
271                 },
272         };
273
274         wdata.args.stable = NFS_UNSTABLE;
275         if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
276                 wdata.args.stable = NFS_FILE_SYNC;
277
278         nfs_begin_data_update(inode);
279 retry:
280         need_commit = 0;
281         tot_bytes = 0;
282         curpage = 0;
283         request = count;
284         wdata.args.pgbase = user_addr & ~PAGE_MASK;
285         wdata.args.offset = file_offset;
286         do {
287                 wdata.args.count = request;
288                 if (wdata.args.count > wsize)
289                         wdata.args.count = wsize;
290                 wdata.args.pages = &pages[curpage];
291
292                 dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
293                         wdata.args.count, (long long) wdata.args.offset,
294                         user_addr + tot_bytes, wdata.args.pgbase, curpage);
295
296                 lock_kernel();
297                 result = NFS_PROTO(inode)->write(&wdata);
298                 unlock_kernel();
299
300                 if (result <= 0) {
301                         if (tot_bytes > 0)
302                                 break;
303                         goto out;
304                 }
305
306                 if (tot_bytes == 0)
307                         memcpy(&first_verf.verifier, &wdata.verf.verifier,
308                                                                 VERF_SIZE);
309                 if (wdata.verf.committed != NFS_FILE_SYNC) {
310                         need_commit = 1;
311                         if (memcmp(&first_verf.verifier,
312                                         &wdata.verf.verifier, VERF_SIZE))
313                                 goto sync_retry;
314                 }
315
316                 tot_bytes += result;
317                 wdata.args.offset += result;
318                 wdata.args.pgbase += result;
319                 curpage += wdata.args.pgbase >> PAGE_SHIFT;
320                 wdata.args.pgbase &= ~PAGE_MASK;
321                 request -= result;
322         } while (request != 0);
323
324         /*
325          * Commit data written so far, even in the event of an error
326          */
327         if (need_commit) {
328                 wdata.args.count = tot_bytes;
329                 wdata.args.offset = file_offset;
330
331                 lock_kernel();
332                 result = NFS_PROTO(inode)->commit(&wdata);
333                 unlock_kernel();
334
335                 if (result < 0 || memcmp(&first_verf.verifier,
336                                                 &wdata.verf.verifier,
337                                                 VERF_SIZE) != 0)
338                         goto sync_retry;
339         }
340         result = tot_bytes;
341
342 out:
343         nfs_end_data_update_defer(inode);
344
345         return result;
346
347 sync_retry:
348         wdata.args.stable = NFS_FILE_SYNC;
349         goto retry;
350 }
351
352 /**
353  * nfs_direct_write - For each iov segment, map the user's buffer
354  *                    then generate write and commit RPCs.
355  * @inode: target inode
356  * @ctx: target file open context
357  * @iov: array of vectors that define I/O buffer
358  * file_offset: offset in file to begin the operation
359  * nr_segs: size of iovec array
360  *
361  * Upon return, generic_file_direct_IO invalidates any cached pages
362  * that non-direct readers might access, so they will pick up these
363  * writes immediately.
364  */
365 static int nfs_direct_write(struct inode *inode, struct nfs_open_context *ctx,
366                 const struct iovec *iov, loff_t file_offset,
367                 unsigned long nr_segs)
368 {
369         ssize_t tot_bytes = 0;
370         unsigned long seg = 0;
371
372         while ((seg < nr_segs) && (tot_bytes >= 0)) {
373                 ssize_t result;
374                 int page_count;
375                 struct page **pages;
376                 const struct iovec *vec = &iov[seg++];
377                 unsigned long user_addr = (unsigned long) vec->iov_base;
378                 size_t size = vec->iov_len;
379
380                 page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
381                 if (page_count < 0) {
382                         nfs_free_user_pages(pages, 0, 0);
383                         if (tot_bytes > 0)
384                                 break;
385                         return page_count;
386                 }
387
388                 result = nfs_direct_write_seg(inode, ctx, user_addr, size,
389                                 file_offset, pages, page_count);
390                 nfs_free_user_pages(pages, page_count, 0);
391
392                 if (result <= 0) {
393                         if (tot_bytes > 0)
394                                 break;
395                         return result;
396                 }
397                 tot_bytes += result;
398                 file_offset += result;
399                 if (result < size)
400                         break;
401         }
402         return tot_bytes;
403 }
404
405 /**
406  * nfs_direct_IO - NFS address space operation for direct I/O
407  * rw: direction (read or write)
408  * @iocb: target I/O control block
409  * @iov: array of vectors that define I/O buffer
410  * file_offset: offset in file to begin the operation
411  * nr_segs: size of iovec array
412  *
413  */
414 ssize_t
415 nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
416                 loff_t file_offset, unsigned long nr_segs)
417 {
418         ssize_t result = -EINVAL;
419         struct file *file = iocb->ki_filp;
420         struct nfs_open_context *ctx;
421         struct dentry *dentry = file->f_dentry;
422         struct inode *inode = dentry->d_inode;
423
424         /*
425          * No support for async yet
426          */
427         if (!is_sync_kiocb(iocb))
428                 return result;
429
430         ctx = (struct nfs_open_context *)file->private_data;
431         switch (rw) {
432         case READ:
433                 dprintk("NFS: direct_IO(read) (%s) off/no(%Lu/%lu)\n",
434                                 dentry->d_name.name, file_offset, nr_segs);
435
436                 result = nfs_direct_read(inode, ctx, iov,
437                                                 file_offset, nr_segs);
438                 break;
439         case WRITE:
440                 dprintk("NFS: direct_IO(write) (%s) off/no(%Lu/%lu)\n",
441                                 dentry->d_name.name, file_offset, nr_segs);
442
443                 result = nfs_direct_write(inode, ctx, iov,
444                                                 file_offset, nr_segs);
445                 break;
446         default:
447                 break;
448         }
449         return result;
450 }
451
452 /**
453  * nfs_file_direct_read - file direct read operation for NFS files
454  * @iocb: target I/O control block
455  * @buf: user's buffer into which to read data
456  * count: number of bytes to read
457  * pos: byte offset in file where reading starts
458  *
459  * We use this function for direct reads instead of calling
460  * generic_file_aio_read() in order to avoid gfar's check to see if
461  * the request starts before the end of the file.  For that check
462  * to work, we must generate a GETATTR before each direct read, and
463  * even then there is a window between the GETATTR and the subsequent
464  * READ where the file size could change.  So our preference is simply
465  * to do all reads the application wants, and the server will take
466  * care of managing the end of file boundary.
467  * 
468  * This function also eliminates unnecessarily updating the file's
469  * atime locally, as the NFS server sets the file's atime, and this
470  * client must read the updated atime from the server back into its
471  * cache.
472  */
473 ssize_t
474 nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
475 {
476         ssize_t retval = -EINVAL;
477         loff_t *ppos = &iocb->ki_pos;
478         struct file *file = iocb->ki_filp;
479         struct nfs_open_context *ctx =
480                         (struct nfs_open_context *) file->private_data;
481         struct dentry *dentry = file->f_dentry;
482         struct address_space *mapping = file->f_mapping;
483         struct inode *inode = mapping->host;
484         struct iovec iov = {
485                 .iov_base = buf,
486                 .iov_len = count,
487         };
488
489         dprintk("nfs: direct read(%s/%s, %lu@%lu)\n",
490                 dentry->d_parent->d_name.name, dentry->d_name.name,
491                 (unsigned long) count, (unsigned long) pos);
492
493         if (!is_sync_kiocb(iocb))
494                 goto out;
495         if (count < 0)
496                 goto out;
497         retval = -EFAULT;
498         if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
499                 goto out;
500         retval = 0;
501         if (!count)
502                 goto out;
503
504         if (mapping->nrpages) {
505                 retval = filemap_fdatawrite(mapping);
506                 if (retval == 0)
507                         retval = filemap_fdatawait(mapping);
508                 if (retval)
509                         goto out;
510         }
511
512         retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
513         if (retval > 0)
514                 *ppos = pos + retval;
515
516 out:
517         return retval;
518 }
519
520 /**
521  * nfs_file_direct_write - file direct write operation for NFS files
522  * @iocb: target I/O control block
523  * @buf: user's buffer from which to write data
524  * count: number of bytes to write
525  * pos: byte offset in file where writing starts
526  *
527  * We use this function for direct writes instead of calling
528  * generic_file_aio_write() in order to avoid taking the inode
529  * semaphore and updating the i_size.  The NFS server will set
530  * the new i_size and this client must read the updated size
531  * back into its cache.  We let the server do generic write
532  * parameter checking and report problems.
533  *
534  * We also avoid an unnecessary invocation of generic_osync_inode(),
535  * as it is fairly meaningless to sync the metadata of an NFS file.
536  *
537  * We eliminate local atime updates, see direct read above.
538  *
539  * We avoid unnecessary page cache invalidations for normal cached
540  * readers of this file.
541  *
542  * Note that O_APPEND is not supported for NFS direct writes, as there
543  * is no atomic O_APPEND write facility in the NFS protocol.
544  */
545 ssize_t
546 nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
547 {
548         ssize_t retval = -EINVAL;
549         loff_t *ppos = &iocb->ki_pos;
550         unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
551         struct file *file = iocb->ki_filp;
552         struct nfs_open_context *ctx =
553                         (struct nfs_open_context *) file->private_data;
554         struct dentry *dentry = file->f_dentry;
555         struct address_space *mapping = file->f_mapping;
556         struct inode *inode = mapping->host;
557         struct iovec iov = {
558                 .iov_base = (char __user *)buf,
559                 .iov_len = count,
560         };
561
562         dfprintk(VFS, "nfs: direct write(%s/%s(%ld), %lu@%lu)\n",
563                 dentry->d_parent->d_name.name, dentry->d_name.name,
564                 inode->i_ino, (unsigned long) count, (unsigned long) pos);
565
566         if (!is_sync_kiocb(iocb))
567                 goto out;
568         if (count < 0)
569                 goto out;
570         if (pos < 0)
571                 goto out;
572         retval = -EFAULT;
573         if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
574                 goto out;
575         if (file->f_error) {
576                 retval = file->f_error;
577                 file->f_error = 0;
578                 goto out;
579         }
580         retval = -EFBIG;
581         if (limit != RLIM_INFINITY) {
582                 if (pos >= limit) {
583                         send_sig(SIGXFSZ, current, 0);
584                         goto out;
585                 }
586                 if (count > limit - (unsigned long) pos)
587                         count = limit - (unsigned long) pos;
588         }
589         retval = 0;
590         if (!count)
591                 goto out;
592
593         if (mapping->nrpages) {
594                 retval = filemap_fdatawrite(mapping);
595                 if (retval == 0)
596                         retval = filemap_fdatawait(mapping);
597                 if (retval)
598                         goto out;
599         }
600
601         retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
602         if (mapping->nrpages)
603                 invalidate_inode_pages2(mapping);
604         if (retval > 0)
605                 *ppos = pos + retval;
606
607 out:
608         return retval;
609 }