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" |
| 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 | |
Trond Myklebust | 143f412 | 2006-03-13 21:20:46 -0800 | [diff] [blame] | 62 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static kmem_cache_t *nfs_direct_cachep; |
| 64 | |
| 65 | /* |
| 66 | * This represents a set of asynchronous requests that we're waiting on |
| 67 | */ |
| 68 | struct nfs_direct_req { |
| 69 | struct kref kref; /* release manager */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 70 | |
| 71 | /* I/O parameters */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 72 | struct list_head list; /* nfs_read/write_data structs */ |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 73 | struct file * filp; /* file descriptor */ |
| 74 | struct kiocb * iocb; /* controlling i/o request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | wait_queue_head_t wait; /* wait for i/o completion */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 76 | struct inode * inode; /* target file of i/o */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct page ** pages; /* pages in our buffer */ |
| 78 | unsigned int npages; /* count of pages */ |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 79 | |
| 80 | /* completion state */ |
| 81 | spinlock_t lock; /* protect completion state */ |
| 82 | int outstanding; /* i/os we're waiting for */ |
| 83 | ssize_t count, /* bytes actually processed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | error; /* any reported error */ |
| 85 | }; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /** |
Chuck Lever | b8a32e2 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 88 | * nfs_direct_IO - NFS address space operation for direct I/O |
| 89 | * @rw: direction (read or write) |
| 90 | * @iocb: target I/O control block |
| 91 | * @iov: array of vectors that define I/O buffer |
| 92 | * @pos: offset in file to begin the operation |
| 93 | * @nr_segs: size of iovec array |
| 94 | * |
| 95 | * The presence of this routine in the address space ops vector means |
| 96 | * the NFS client supports direct I/O. However, we shunt off direct |
| 97 | * read and write requests before the VFS gets them, so this method |
| 98 | * should never be called. |
| 99 | */ |
| 100 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) |
| 101 | { |
| 102 | struct dentry *dentry = iocb->ki_filp->f_dentry; |
| 103 | |
| 104 | dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", |
| 105 | dentry->d_name.name, (long long) pos, nr_segs); |
| 106 | |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 110 | 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] | 111 | { |
| 112 | int result = -ENOMEM; |
| 113 | unsigned long page_count; |
| 114 | size_t array_size; |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 117 | page_count -= user_addr >> PAGE_SHIFT; |
| 118 | |
| 119 | array_size = (page_count * sizeof(struct page *)); |
| 120 | *pages = kmalloc(array_size, GFP_KERNEL); |
| 121 | if (*pages) { |
| 122 | down_read(¤t->mm->mmap_sem); |
| 123 | result = get_user_pages(current, current->mm, user_addr, |
| 124 | page_count, (rw == READ), 0, |
| 125 | *pages, NULL); |
| 126 | up_read(¤t->mm->mmap_sem); |
Trond Myklebust | 143f412 | 2006-03-13 21:20:46 -0800 | [diff] [blame] | 127 | /* |
| 128 | * If we got fewer pages than expected from get_user_pages(), |
| 129 | * the user buffer runs off the end of a mapping; return EFAULT. |
| 130 | */ |
| 131 | if (result >= 0 && result < page_count) { |
| 132 | nfs_free_user_pages(*pages, result, 0); |
| 133 | *pages = NULL; |
| 134 | result = -EFAULT; |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | return result; |
| 138 | } |
| 139 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 140 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | for (i = 0; i < npages; i++) { |
Trond Myklebust | 566dd60 | 2006-01-03 09:55:35 +0100 | [diff] [blame] | 144 | struct page *page = pages[i]; |
| 145 | if (do_dirty && !PageCompound(page)) |
| 146 | set_page_dirty_lock(page); |
| 147 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | kfree(pages); |
| 150 | } |
| 151 | |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 152 | static inline struct nfs_direct_req *nfs_direct_req_alloc(void) |
| 153 | { |
| 154 | struct nfs_direct_req *dreq; |
| 155 | |
| 156 | dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); |
| 157 | if (!dreq) |
| 158 | return NULL; |
| 159 | |
| 160 | kref_init(&dreq->kref); |
| 161 | init_waitqueue_head(&dreq->wait); |
| 162 | INIT_LIST_HEAD(&dreq->list); |
| 163 | dreq->iocb = NULL; |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 164 | spin_lock_init(&dreq->lock); |
| 165 | dreq->outstanding = 0; |
| 166 | dreq->count = 0; |
| 167 | dreq->error = 0; |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 168 | |
| 169 | return dreq; |
| 170 | } |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | static void nfs_direct_req_release(struct kref *kref) |
| 173 | { |
| 174 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); |
| 175 | kmem_cache_free(nfs_direct_cachep, dreq); |
| 176 | } |
| 177 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 178 | /* |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 179 | * Collects and returns the final error value/byte-count. |
| 180 | */ |
| 181 | static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) |
| 182 | { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 183 | ssize_t result = -EIOCBQUEUED; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 184 | |
| 185 | /* Async requests don't wait here */ |
| 186 | if (dreq->iocb) |
| 187 | goto out; |
| 188 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 189 | result = wait_event_interruptible(dreq->wait, (dreq->outstanding == 0)); |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 190 | |
| 191 | if (!result) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 192 | result = dreq->error; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 193 | if (!result) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 194 | result = dreq->count; |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 195 | |
| 196 | out: |
| 197 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 198 | return (ssize_t) result; |
| 199 | } |
| 200 | |
| 201 | /* |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 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_wait (for instance, if someone hits ^C on a slow server). |
| 205 | * |
| 206 | * In addition, synchronous I/O uses a stack-allocated iocb. Thus we |
| 207 | * can't trust the iocb is still valid here if this is a synchronous |
| 208 | * request. If the waiter is woken prematurely, the iocb is long gone. |
| 209 | */ |
| 210 | static void nfs_direct_complete(struct nfs_direct_req *dreq) |
| 211 | { |
| 212 | nfs_free_user_pages(dreq->pages, dreq->npages, 1); |
| 213 | |
| 214 | if (dreq->iocb) { |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 215 | long res = (long) dreq->error; |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 216 | if (!res) |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 217 | res = (long) dreq->count; |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 218 | aio_complete(dreq->iocb, res, 0); |
| 219 | } else |
| 220 | wake_up(&dreq->wait); |
| 221 | |
Chuck Lever | a37ec01 | 2006-03-20 13:44:35 -0500 | [diff] [blame^] | 222 | iput(dreq->inode); |
Chuck Lever | 63ab46a | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 223 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 224 | } |
| 225 | |
| 226 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | * Note we also set the number of requests we have in the dreq when we are |
| 228 | * done. This prevents races with I/O completion so we will always wait |
| 229 | * until all requests have been dispatched and completed. |
| 230 | */ |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 231 | 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] | 232 | { |
| 233 | struct list_head *list; |
| 234 | struct nfs_direct_req *dreq; |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 235 | unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Chuck Lever | 93619e5 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 237 | dreq = nfs_direct_req_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (!dreq) |
| 239 | return NULL; |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | list = &dreq->list; |
| 242 | for(;;) { |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 243 | struct nfs_read_data *data = nfs_readdata_alloc(rpages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | if (unlikely(!data)) { |
| 246 | while (!list_empty(list)) { |
| 247 | data = list_entry(list->next, |
| 248 | struct nfs_read_data, pages); |
| 249 | list_del(&data->pages); |
| 250 | nfs_readdata_free(data); |
| 251 | } |
| 252 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | INIT_LIST_HEAD(&data->pages); |
| 257 | list_add(&data->pages, list); |
| 258 | |
| 259 | data->req = (struct nfs_page *) dreq; |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 260 | dreq->outstanding++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (nbytes <= rsize) |
| 262 | break; |
| 263 | nbytes -= rsize; |
| 264 | } |
| 265 | kref_get(&dreq->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | return dreq; |
| 267 | } |
| 268 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 269 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 271 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 273 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 274 | if (nfs_readpage_result(task, data) != 0) |
| 275 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 277 | spin_lock(&dreq->lock); |
| 278 | |
| 279 | if (likely(task->tk_status >= 0)) |
| 280 | dreq->count += data->res.count; |
| 281 | else |
| 282 | dreq->error = task->tk_status; |
| 283 | |
| 284 | if (--dreq->outstanding) { |
| 285 | spin_unlock(&dreq->lock); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | spin_unlock(&dreq->lock); |
| 290 | nfs_direct_complete(dreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 293 | static const struct rpc_call_ops nfs_read_direct_ops = { |
| 294 | .rpc_call_done = nfs_direct_read_result, |
| 295 | .rpc_release = nfs_readdata_release, |
| 296 | }; |
| 297 | |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 298 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * For each nfs_read_data struct that was allocated on the list, dispatch |
| 300 | * an NFS READ operation |
| 301 | */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 302 | static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 304 | struct file *file = dreq->filp; |
| 305 | struct inode *inode = file->f_mapping->host; |
| 306 | struct nfs_open_context *ctx = (struct nfs_open_context *) |
| 307 | file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | struct list_head *list = &dreq->list; |
| 309 | struct page **pages = dreq->pages; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 310 | size_t rsize = NFS_SERVER(inode)->rsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | unsigned int curpage, pgbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | curpage = 0; |
| 314 | pgbase = user_addr & ~PAGE_MASK; |
| 315 | do { |
| 316 | struct nfs_read_data *data; |
Chuck Lever | 5dd602f | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 317 | size_t bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | bytes = rsize; |
| 320 | if (count < rsize) |
| 321 | bytes = count; |
| 322 | |
| 323 | data = list_entry(list->next, struct nfs_read_data, pages); |
| 324 | list_del_init(&data->pages); |
| 325 | |
| 326 | data->inode = inode; |
| 327 | data->cred = ctx->cred; |
| 328 | data->args.fh = NFS_FH(inode); |
| 329 | data->args.context = ctx; |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 330 | data->args.offset = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | data->args.pgbase = pgbase; |
| 332 | data->args.pages = &pages[curpage]; |
| 333 | data->args.count = bytes; |
| 334 | data->res.fattr = &data->fattr; |
| 335 | data->res.eof = 0; |
| 336 | data->res.count = bytes; |
| 337 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 338 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
| 339 | &nfs_read_direct_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | NFS_PROTO(inode)->read_setup(data); |
| 341 | |
| 342 | data->task.tk_cookie = (unsigned long) inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | lock_kernel(); |
| 345 | rpc_execute(&data->task); |
| 346 | unlock_kernel(); |
| 347 | |
| 348 | dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 349 | data->task.tk_pid, |
| 350 | inode->i_sb->s_id, |
| 351 | (long long)NFS_FILEID(inode), |
| 352 | bytes, |
| 353 | (unsigned long long)data->args.offset); |
| 354 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 355 | pos += bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | pgbase += bytes; |
| 357 | curpage += pgbase >> PAGE_SHIFT; |
| 358 | pgbase &= ~PAGE_MASK; |
| 359 | |
| 360 | count -= bytes; |
| 361 | } while (count != 0); |
| 362 | } |
| 363 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | ssize_t result; |
| 367 | sigset_t oldset; |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 368 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
| 370 | struct nfs_direct_req *dreq; |
| 371 | |
| 372 | dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); |
| 373 | if (!dreq) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | dreq->pages = pages; |
| 377 | dreq->npages = nr_pages; |
Chuck Lever | a37ec01 | 2006-03-20 13:44:35 -0500 | [diff] [blame^] | 378 | igrab(inode); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 379 | dreq->inode = inode; |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 380 | dreq->filp = iocb->ki_filp; |
Chuck Lever | 487b837 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 381 | if (!is_sync_kiocb(iocb)) |
| 382 | dreq->iocb = iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 384 | nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | rpc_clnt_sigmask(clnt, &oldset); |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 386 | nfs_direct_read_schedule(dreq, user_addr, count, pos); |
Chuck Lever | bc0fb20 | 2006-03-20 13:44:31 -0500 | [diff] [blame] | 387 | result = nfs_direct_wait(dreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | rpc_clnt_sigunmask(clnt, &oldset); |
| 389 | |
| 390 | return result; |
| 391 | } |
| 392 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 393 | static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize) |
| 394 | { |
| 395 | struct list_head *list; |
| 396 | struct nfs_direct_req *dreq; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 397 | unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 398 | |
| 399 | dreq = nfs_direct_req_alloc(); |
| 400 | if (!dreq) |
| 401 | return NULL; |
| 402 | |
| 403 | list = &dreq->list; |
| 404 | for(;;) { |
| 405 | struct nfs_write_data *data = nfs_writedata_alloc(wpages); |
| 406 | |
| 407 | if (unlikely(!data)) { |
| 408 | while (!list_empty(list)) { |
| 409 | data = list_entry(list->next, |
| 410 | struct nfs_write_data, pages); |
| 411 | list_del(&data->pages); |
| 412 | nfs_writedata_free(data); |
| 413 | } |
| 414 | kref_put(&dreq->kref, nfs_direct_req_release); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | INIT_LIST_HEAD(&data->pages); |
| 419 | list_add(&data->pages, list); |
| 420 | |
| 421 | data->req = (struct nfs_page *) dreq; |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 422 | dreq->outstanding++; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 423 | if (nbytes <= wsize) |
| 424 | break; |
| 425 | nbytes -= wsize; |
| 426 | } |
| 427 | kref_get(&dreq->kref); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 428 | return dreq; |
| 429 | } |
| 430 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 431 | /* |
| 432 | * NB: Return the value of the first error return code. Subsequent |
| 433 | * errors after the first one are ignored. |
| 434 | */ |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 435 | static void nfs_direct_write_result(struct rpc_task *task, void *calldata) |
| 436 | { |
| 437 | struct nfs_write_data *data = calldata; |
| 438 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
| 439 | int status = task->tk_status; |
| 440 | |
| 441 | if (nfs_writeback_done(task, data) != 0) |
| 442 | return; |
| 443 | /* If the server fell back to an UNSTABLE write, it's an error. */ |
| 444 | if (unlikely(data->res.verf->committed != NFS_FILE_SYNC)) |
| 445 | status = -EIO; |
| 446 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 447 | spin_lock(&dreq->lock); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 448 | |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 449 | if (likely(status >= 0)) |
| 450 | dreq->count += data->res.count; |
| 451 | else |
| 452 | dreq->error = status; |
| 453 | |
| 454 | if (--dreq->outstanding) { |
| 455 | spin_unlock(&dreq->lock); |
| 456 | return; |
Chuck Lever | 9eafa8c | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 457 | } |
Chuck Lever | 15ce4a0 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 458 | |
| 459 | spin_unlock(&dreq->lock); |
| 460 | |
| 461 | nfs_end_data_update(data->inode); |
| 462 | nfs_direct_complete(dreq); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static const struct rpc_call_ops nfs_write_direct_ops = { |
| 466 | .rpc_call_done = nfs_direct_write_result, |
| 467 | .rpc_release = nfs_writedata_release, |
| 468 | }; |
| 469 | |
| 470 | /* |
| 471 | * For each nfs_write_data struct that was allocated on the list, dispatch |
| 472 | * an NFS WRITE operation |
| 473 | * |
| 474 | * XXX: For now, support only FILE_SYNC writes. Later we may add |
| 475 | * support for UNSTABLE + COMMIT. |
| 476 | */ |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 477 | static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos) |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 478 | { |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 479 | struct file *file = dreq->filp; |
| 480 | struct inode *inode = file->f_mapping->host; |
| 481 | struct nfs_open_context *ctx = (struct nfs_open_context *) |
| 482 | file->private_data; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 483 | struct list_head *list = &dreq->list; |
| 484 | struct page **pages = dreq->pages; |
| 485 | size_t wsize = NFS_SERVER(inode)->wsize; |
| 486 | unsigned int curpage, pgbase; |
| 487 | |
| 488 | curpage = 0; |
| 489 | pgbase = user_addr & ~PAGE_MASK; |
| 490 | do { |
| 491 | struct nfs_write_data *data; |
| 492 | size_t bytes; |
| 493 | |
| 494 | bytes = wsize; |
| 495 | if (count < wsize) |
| 496 | bytes = count; |
| 497 | |
| 498 | data = list_entry(list->next, struct nfs_write_data, pages); |
| 499 | list_del_init(&data->pages); |
| 500 | |
| 501 | data->inode = inode; |
| 502 | data->cred = ctx->cred; |
| 503 | data->args.fh = NFS_FH(inode); |
| 504 | data->args.context = ctx; |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 505 | data->args.offset = pos; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 506 | data->args.pgbase = pgbase; |
| 507 | data->args.pages = &pages[curpage]; |
| 508 | data->args.count = bytes; |
| 509 | data->res.fattr = &data->fattr; |
| 510 | data->res.count = bytes; |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 511 | data->res.verf = &data->verf; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 512 | |
| 513 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
| 514 | &nfs_write_direct_ops, data); |
| 515 | NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE); |
| 516 | |
| 517 | data->task.tk_priority = RPC_PRIORITY_NORMAL; |
| 518 | data->task.tk_cookie = (unsigned long) inode; |
| 519 | |
| 520 | lock_kernel(); |
| 521 | rpc_execute(&data->task); |
| 522 | unlock_kernel(); |
| 523 | |
| 524 | dfprintk(VFS, "NFS: %4d initiated direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 525 | data->task.tk_pid, |
| 526 | inode->i_sb->s_id, |
| 527 | (long long)NFS_FILEID(inode), |
| 528 | bytes, |
| 529 | (unsigned long long)data->args.offset); |
| 530 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 531 | pos += bytes; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 532 | pgbase += bytes; |
| 533 | curpage += pgbase >> PAGE_SHIFT; |
| 534 | pgbase &= ~PAGE_MASK; |
| 535 | |
| 536 | count -= bytes; |
| 537 | } while (count != 0); |
| 538 | } |
| 539 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 540 | 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] | 541 | { |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 542 | ssize_t result; |
| 543 | sigset_t oldset; |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 544 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 545 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
| 546 | struct nfs_direct_req *dreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 548 | dreq = nfs_direct_write_alloc(count, NFS_SERVER(inode)->wsize); |
| 549 | if (!dreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | return -ENOMEM; |
| 551 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 552 | dreq->pages = pages; |
| 553 | dreq->npages = nr_pages; |
Chuck Lever | a37ec01 | 2006-03-20 13:44:35 -0500 | [diff] [blame^] | 554 | igrab(inode); |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 555 | dreq->inode = inode; |
| 556 | dreq->filp = iocb->ki_filp; |
| 557 | if (!is_sync_kiocb(iocb)) |
| 558 | dreq->iocb = iocb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 560 | nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | nfs_begin_data_update(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 564 | rpc_clnt_sigmask(clnt, &oldset); |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 565 | nfs_direct_write_schedule(dreq, user_addr, count, pos); |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 566 | result = nfs_direct_wait(dreq); |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 567 | rpc_clnt_sigunmask(clnt, &oldset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 569 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | * nfs_file_direct_read - file direct read operation for NFS files |
| 574 | * @iocb: target I/O control block |
| 575 | * @buf: user's buffer into which to read data |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 576 | * @count: number of bytes to read |
| 577 | * @pos: byte offset in file where reading starts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | * |
| 579 | * We use this function for direct reads instead of calling |
| 580 | * generic_file_aio_read() in order to avoid gfar's check to see if |
| 581 | * the request starts before the end of the file. For that check |
| 582 | * to work, we must generate a GETATTR before each direct read, and |
| 583 | * even then there is a window between the GETATTR and the subsequent |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 584 | * READ where the file size could change. Our preference is simply |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | * to do all reads the application wants, and the server will take |
| 586 | * care of managing the end of file boundary. |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 587 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | * This function also eliminates unnecessarily updating the file's |
| 589 | * atime locally, as the NFS server sets the file's atime, and this |
| 590 | * client must read the updated atime from the server back into its |
| 591 | * cache. |
| 592 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 593 | 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] | 594 | { |
| 595 | ssize_t retval = -EINVAL; |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 596 | int page_count; |
| 597 | struct page **pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | struct address_space *mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 601 | dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 602 | file->f_dentry->d_parent->d_name.name, |
| 603 | file->f_dentry->d_name.name, |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 604 | (unsigned long) count, (long long) pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | if (count < 0) |
| 607 | goto out; |
| 608 | retval = -EFAULT; |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 609 | if (!access_ok(VERIFY_WRITE, buf, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | goto out; |
| 611 | retval = 0; |
| 612 | if (!count) |
| 613 | goto out; |
| 614 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 615 | retval = nfs_sync_mapping(mapping); |
| 616 | if (retval) |
| 617 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 619 | page_count = nfs_get_user_pages(READ, (unsigned long) buf, |
| 620 | count, &pages); |
| 621 | if (page_count < 0) { |
| 622 | nfs_free_user_pages(pages, 0, 0); |
| 623 | retval = page_count; |
| 624 | goto out; |
| 625 | } |
| 626 | |
Chuck Lever | 99514f8 | 2006-03-20 13:44:30 -0500 | [diff] [blame] | 627 | retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos, |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 628 | pages, page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (retval > 0) |
Chuck Lever | 0cdd80d | 2006-03-20 13:44:29 -0500 | [diff] [blame] | 630 | iocb->ki_pos = pos + retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | out: |
| 633 | return retval; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * nfs_file_direct_write - file direct write operation for NFS files |
| 638 | * @iocb: target I/O control block |
| 639 | * @buf: user's buffer from which to write data |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 640 | * @count: number of bytes to write |
| 641 | * @pos: byte offset in file where writing starts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | * |
| 643 | * We use this function for direct writes instead of calling |
| 644 | * generic_file_aio_write() in order to avoid taking the inode |
| 645 | * semaphore and updating the i_size. The NFS server will set |
| 646 | * the new i_size and this client must read the updated size |
| 647 | * back into its cache. We let the server do generic write |
| 648 | * parameter checking and report problems. |
| 649 | * |
| 650 | * We also avoid an unnecessary invocation of generic_osync_inode(), |
| 651 | * as it is fairly meaningless to sync the metadata of an NFS file. |
| 652 | * |
| 653 | * We eliminate local atime updates, see direct read above. |
| 654 | * |
| 655 | * We avoid unnecessary page cache invalidations for normal cached |
| 656 | * readers of this file. |
| 657 | * |
| 658 | * Note that O_APPEND is not supported for NFS direct writes, as there |
| 659 | * is no atomic O_APPEND write facility in the NFS protocol. |
| 660 | */ |
Chuck Lever | d4cc948 | 2006-03-20 13:44:28 -0500 | [diff] [blame] | 661 | 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] | 662 | { |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 663 | ssize_t retval; |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 664 | int page_count; |
| 665 | struct page **pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | struct address_space *mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 669 | dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 670 | file->f_dentry->d_parent->d_name.name, |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 671 | file->f_dentry->d_name.name, |
| 672 | (unsigned long) count, (long long) pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 674 | retval = generic_write_checks(file, &pos, &count, 0); |
| 675 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 677 | |
| 678 | retval = -EINVAL; |
| 679 | if ((ssize_t) count < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | retval = 0; |
| 682 | if (!count) |
| 683 | goto out; |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 684 | |
| 685 | retval = -EFAULT; |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 686 | if (!access_ok(VERIFY_READ, buf, count)) |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 687 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Trond Myklebust | 29884df | 2005-12-13 16:13:54 -0500 | [diff] [blame] | 689 | retval = nfs_sync_mapping(mapping); |
| 690 | if (retval) |
| 691 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 693 | page_count = nfs_get_user_pages(WRITE, (unsigned long) buf, |
| 694 | count, &pages); |
| 695 | if (page_count < 0) { |
| 696 | nfs_free_user_pages(pages, 0, 0); |
| 697 | retval = page_count; |
| 698 | goto out; |
| 699 | } |
| 700 | |
Chuck Lever | c89f2ee | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 701 | retval = nfs_direct_write(iocb, (unsigned long) buf, count, |
Chuck Lever | 47989d7 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 702 | pos, pages, page_count); |
Chuck Lever | 9eafa8c | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * XXX: nfs_end_data_update() already ensures this file's |
| 706 | * cached data is subsequently invalidated. Do we really |
| 707 | * need to call invalidate_inode_pages2() again here? |
| 708 | * |
| 709 | * For aio writes, this invalidation will almost certainly |
| 710 | * occur before the writes complete. Kind of racey. |
| 711 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if (mapping->nrpages) |
| 713 | invalidate_inode_pages2(mapping); |
Chuck Lever | 9eafa8c | 2006-03-20 13:44:33 -0500 | [diff] [blame] | 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (retval > 0) |
Chuck Lever | ce1a8e6 | 2005-11-30 18:08:17 -0500 | [diff] [blame] | 716 | iocb->ki_pos = pos + retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | out: |
| 719 | return retval; |
| 720 | } |
| 721 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 722 | /** |
| 723 | * nfs_init_directcache - create a slab cache for nfs_direct_req structures |
| 724 | * |
| 725 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | int nfs_init_directcache(void) |
| 727 | { |
| 728 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", |
| 729 | sizeof(struct nfs_direct_req), |
| 730 | 0, SLAB_RECLAIM_ACCOUNT, |
| 731 | NULL, NULL); |
| 732 | if (nfs_direct_cachep == NULL) |
| 733 | return -ENOMEM; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Chuck Lever | 8846705 | 2006-03-20 13:44:34 -0500 | [diff] [blame] | 738 | /** |
| 739 | * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures |
| 740 | * |
| 741 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | void nfs_destroy_directcache(void) |
| 743 | { |
| 744 | if (kmem_cache_destroy(nfs_direct_cachep)) |
| 745 | printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); |
| 746 | } |