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