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