Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/read.c |
| 3 | * |
| 4 | * Block I/O for NFS |
| 5 | * |
| 6 | * Partial copy of Linus' read cache modifications to fs/nfs/file.c |
| 7 | * modified for async RPC by okir@monad.swb.de |
| 8 | * |
| 9 | * We do an ugly hack here in order to return proper error codes to the |
| 10 | * user program when a read request failed: since generic_file_read |
| 11 | * only checks the return value of inode->i_op->readpage() which is always 0 |
| 12 | * for async RPC, we set the error bit of the page to 1 when an error occurs, |
| 13 | * and make nfs_readpage transmit requests synchronously when encountering this. |
| 14 | * This is only a small problem, though, since we now retry all operations |
| 15 | * within the RPC code when root squashing is suspected. |
| 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/time.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/fcntl.h> |
| 22 | #include <linux/stat.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/sunrpc/clnt.h> |
| 27 | #include <linux/nfs_fs.h> |
| 28 | #include <linux/nfs_page.h> |
| 29 | #include <linux/smp_lock.h> |
| 30 | |
| 31 | #include <asm/system.h> |
| 32 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 33 | #include "iostat.h" |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 36 | |
| 37 | static int nfs_pagein_one(struct list_head *, struct inode *); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 38 | static const struct rpc_call_ops nfs_read_partial_ops; |
| 39 | static const struct rpc_call_ops nfs_read_full_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | static kmem_cache_t *nfs_rdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 42 | static mempool_t *nfs_rdata_mempool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define MIN_POOL_READ (32) |
| 45 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 46 | struct nfs_read_data *nfs_readdata_alloc(size_t len) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 47 | { |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 48 | unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 49 | struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS); |
| 50 | |
| 51 | if (p) { |
| 52 | memset(p, 0, sizeof(*p)); |
| 53 | INIT_LIST_HEAD(&p->pages); |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 54 | p->npages = pagecount; |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 55 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 56 | p->pagevec = p->page_array; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 57 | else { |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 58 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); |
| 59 | if (!p->pagevec) { |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 60 | mempool_free(p, nfs_rdata_mempool); |
| 61 | p = NULL; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return p; |
| 66 | } |
| 67 | |
Adrian Bunk | e4e2051 | 2006-08-03 15:07:47 -0400 | [diff] [blame] | 68 | static void nfs_readdata_free(struct nfs_read_data *p) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 69 | { |
| 70 | if (p && (p->pagevec != &p->page_array[0])) |
| 71 | kfree(p->pagevec); |
| 72 | mempool_free(p, nfs_rdata_mempool); |
| 73 | } |
| 74 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 75 | void nfs_readdata_release(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | nfs_readdata_free(data); |
| 78 | } |
| 79 | |
| 80 | static |
| 81 | unsigned int nfs_page_length(struct inode *inode, struct page *page) |
| 82 | { |
| 83 | loff_t i_size = i_size_read(inode); |
| 84 | unsigned long idx; |
| 85 | |
| 86 | if (i_size <= 0) |
| 87 | return 0; |
| 88 | idx = (i_size - 1) >> PAGE_CACHE_SHIFT; |
| 89 | if (page->index > idx) |
| 90 | return 0; |
| 91 | if (page->index != idx) |
| 92 | return PAGE_CACHE_SIZE; |
| 93 | return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1)); |
| 94 | } |
| 95 | |
| 96 | static |
| 97 | int nfs_return_empty_page(struct page *page) |
| 98 | { |
| 99 | memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE); |
| 100 | SetPageUptodate(page); |
| 101 | unlock_page(page); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 105 | static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data) |
| 106 | { |
| 107 | unsigned int remainder = data->args.count - data->res.count; |
| 108 | unsigned int base = data->args.pgbase + data->res.count; |
| 109 | unsigned int pglen; |
| 110 | struct page **pages; |
| 111 | |
| 112 | if (data->res.eof == 0 || remainder == 0) |
| 113 | return; |
| 114 | /* |
| 115 | * Note: "remainder" can never be negative, since we check for |
| 116 | * this in the XDR code. |
| 117 | */ |
| 118 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 119 | base &= ~PAGE_CACHE_MASK; |
| 120 | pglen = PAGE_CACHE_SIZE - base; |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 121 | for (;;) { |
| 122 | if (remainder <= pglen) { |
| 123 | memclear_highpage_flush(*pages, base, remainder); |
| 124 | break; |
| 125 | } |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 126 | memclear_highpage_flush(*pages, base, pglen); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 127 | pages++; |
| 128 | remainder -= pglen; |
| 129 | pglen = PAGE_CACHE_SIZE; |
| 130 | base = 0; |
| 131 | } |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Read a page synchronously. |
| 136 | */ |
| 137 | static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode, |
| 138 | struct page *page) |
| 139 | { |
| 140 | unsigned int rsize = NFS_SERVER(inode)->rsize; |
| 141 | unsigned int count = PAGE_CACHE_SIZE; |
| 142 | int result; |
| 143 | struct nfs_read_data *rdata; |
| 144 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 145 | rdata = nfs_readdata_alloc(count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!rdata) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | memset(rdata, 0, sizeof(*rdata)); |
| 150 | rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); |
| 151 | rdata->cred = ctx->cred; |
| 152 | rdata->inode = inode; |
| 153 | INIT_LIST_HEAD(&rdata->pages); |
| 154 | rdata->args.fh = NFS_FH(inode); |
| 155 | rdata->args.context = ctx; |
| 156 | rdata->args.pages = &page; |
| 157 | rdata->args.pgbase = 0UL; |
| 158 | rdata->args.count = rsize; |
| 159 | rdata->res.fattr = &rdata->fattr; |
| 160 | |
| 161 | dprintk("NFS: nfs_readpage_sync(%p)\n", page); |
| 162 | |
| 163 | /* |
| 164 | * This works now because the socket layer never tries to DMA |
| 165 | * into this buffer directly. |
| 166 | */ |
| 167 | do { |
| 168 | if (count < rsize) |
| 169 | rdata->args.count = count; |
| 170 | rdata->res.count = rdata->args.count; |
| 171 | rdata->args.offset = page_offset(page) + rdata->args.pgbase; |
| 172 | |
| 173 | dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 174 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | inode->i_sb->s_id, |
| 176 | (long long)NFS_FILEID(inode), |
| 177 | (unsigned long long)rdata->args.pgbase, |
| 178 | rdata->args.count); |
| 179 | |
| 180 | lock_kernel(); |
| 181 | result = NFS_PROTO(inode)->read(rdata); |
| 182 | unlock_kernel(); |
| 183 | |
| 184 | /* |
| 185 | * Even if we had a partial success we can't mark the page |
| 186 | * cache valid. |
| 187 | */ |
| 188 | if (result < 0) { |
| 189 | if (result == -EISDIR) |
| 190 | result = -EINVAL; |
| 191 | goto io_error; |
| 192 | } |
| 193 | count -= result; |
| 194 | rdata->args.pgbase += result; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 195 | nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result); |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* Note: result == 0 should only happen if we're caching |
| 198 | * a write that extends the file and punches a hole. |
| 199 | */ |
| 200 | if (rdata->res.eof != 0 || result == 0) |
| 201 | break; |
| 202 | } while (count); |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 203 | spin_lock(&inode->i_lock); |
Chuck Lever | 5529680 | 2005-08-18 11:24:09 -0700 | [diff] [blame] | 204 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 205 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Trond Myklebust | 7a52411 | 2006-09-15 16:03:45 -0400 | [diff] [blame] | 207 | if (rdata->res.eof || rdata->res.count == rdata->args.count) { |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 208 | SetPageUptodate(page); |
Trond Myklebust | 7a52411 | 2006-09-15 16:03:45 -0400 | [diff] [blame] | 209 | if (rdata->res.eof && count != 0) |
| 210 | memclear_highpage_flush(page, rdata->args.pgbase, count); |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | result = 0; |
| 213 | |
| 214 | io_error: |
| 215 | unlock_page(page); |
| 216 | nfs_readdata_free(rdata); |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
| 221 | struct page *page) |
| 222 | { |
| 223 | LIST_HEAD(one_request); |
| 224 | struct nfs_page *new; |
| 225 | unsigned int len; |
| 226 | |
| 227 | len = nfs_page_length(inode, page); |
| 228 | if (len == 0) |
| 229 | return nfs_return_empty_page(page); |
| 230 | new = nfs_create_request(ctx, inode, page, 0, len); |
| 231 | if (IS_ERR(new)) { |
| 232 | unlock_page(page); |
| 233 | return PTR_ERR(new); |
| 234 | } |
| 235 | if (len < PAGE_CACHE_SIZE) |
| 236 | memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | nfs_list_add_request(new, &one_request); |
| 239 | nfs_pagein_one(&one_request, inode); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static void nfs_readpage_release(struct nfs_page *req) |
| 244 | { |
| 245 | unlock_page(req->wb_page); |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", |
| 248 | req->wb_context->dentry->d_inode->i_sb->s_id, |
| 249 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), |
| 250 | req->wb_bytes, |
| 251 | (long long)req_offset(req)); |
Nick Wilson | 10d2c46 | 2005-09-22 21:44:28 -0700 | [diff] [blame] | 252 | nfs_clear_request(req); |
| 253 | nfs_release_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Set up the NFS read request struct |
| 258 | */ |
| 259 | static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 260 | const struct rpc_call_ops *call_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | unsigned int count, unsigned int offset) |
| 262 | { |
| 263 | struct inode *inode; |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 264 | int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | data->req = req; |
| 267 | data->inode = inode = req->wb_context->dentry->d_inode; |
| 268 | data->cred = req->wb_context->cred; |
| 269 | |
| 270 | data->args.fh = NFS_FH(inode); |
| 271 | data->args.offset = req_offset(req) + offset; |
| 272 | data->args.pgbase = req->wb_pgbase + offset; |
| 273 | data->args.pages = data->pagevec; |
| 274 | data->args.count = count; |
| 275 | data->args.context = req->wb_context; |
| 276 | |
| 277 | data->res.fattr = &data->fattr; |
| 278 | data->res.count = count; |
| 279 | data->res.eof = 0; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 280 | nfs_fattr_init(&data->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 282 | /* Set up the initial task struct. */ |
| 283 | flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); |
| 284 | rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | NFS_PROTO(inode)->read_setup(data); |
| 286 | |
| 287 | data->task.tk_cookie = (unsigned long)inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n", |
| 290 | data->task.tk_pid, |
| 291 | inode->i_sb->s_id, |
| 292 | (long long)NFS_FILEID(inode), |
| 293 | count, |
| 294 | (unsigned long long)data->args.offset); |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | nfs_async_read_error(struct list_head *head) |
| 299 | { |
| 300 | struct nfs_page *req; |
| 301 | |
| 302 | while (!list_empty(head)) { |
| 303 | req = nfs_list_entry(head->next); |
| 304 | nfs_list_remove_request(req); |
| 305 | SetPageError(req->wb_page); |
| 306 | nfs_readpage_release(req); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Start an async read operation |
| 312 | */ |
| 313 | static void nfs_execute_read(struct nfs_read_data *data) |
| 314 | { |
| 315 | struct rpc_clnt *clnt = NFS_CLIENT(data->inode); |
| 316 | sigset_t oldset; |
| 317 | |
| 318 | rpc_clnt_sigmask(clnt, &oldset); |
| 319 | lock_kernel(); |
| 320 | rpc_execute(&data->task); |
| 321 | unlock_kernel(); |
| 322 | rpc_clnt_sigunmask(clnt, &oldset); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Generate multiple requests to fill a single page. |
| 327 | * |
| 328 | * We optimize to reduce the number of read operations on the wire. If we |
| 329 | * detect that we're reading a page, or an area of a page, that is past the |
| 330 | * end of file, we do not generate NFS read operations but just clear the |
| 331 | * parts of the page that would have come back zero from the server anyway. |
| 332 | * |
| 333 | * We rely on the cached value of i_size to make this determination; another |
| 334 | * client can fill pages on the server past our cached end-of-file, but we |
| 335 | * won't see the new data until our attribute cache is updated. This is more |
| 336 | * or less conventional NFS client behavior. |
| 337 | */ |
| 338 | static int nfs_pagein_multi(struct list_head *head, struct inode *inode) |
| 339 | { |
| 340 | struct nfs_page *req = nfs_list_entry(head->next); |
| 341 | struct page *page = req->wb_page; |
| 342 | struct nfs_read_data *data; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 343 | size_t rsize = NFS_SERVER(inode)->rsize, nbytes; |
| 344 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | int requests = 0; |
| 346 | LIST_HEAD(list); |
| 347 | |
| 348 | nfs_list_remove_request(req); |
| 349 | |
| 350 | nbytes = req->wb_bytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 351 | do { |
| 352 | size_t len = min(nbytes,rsize); |
| 353 | |
| 354 | data = nfs_readdata_alloc(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (!data) |
| 356 | goto out_bad; |
| 357 | INIT_LIST_HEAD(&data->pages); |
| 358 | list_add(&data->pages, &list); |
| 359 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 360 | nbytes -= len; |
| 361 | } while(nbytes != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | atomic_set(&req->wb_complete, requests); |
| 363 | |
| 364 | ClearPageError(page); |
| 365 | offset = 0; |
| 366 | nbytes = req->wb_bytes; |
| 367 | do { |
| 368 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 369 | list_del_init(&data->pages); |
| 370 | |
| 371 | data->pagevec[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | if (nbytes > rsize) { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 374 | nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, |
| 375 | rsize, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | offset += rsize; |
| 377 | nbytes -= rsize; |
| 378 | } else { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 379 | nfs_read_rpcsetup(req, data, &nfs_read_partial_ops, |
| 380 | nbytes, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | nbytes = 0; |
| 382 | } |
| 383 | nfs_execute_read(data); |
| 384 | } while (nbytes != 0); |
| 385 | |
| 386 | return 0; |
| 387 | |
| 388 | out_bad: |
| 389 | while (!list_empty(&list)) { |
| 390 | data = list_entry(list.next, struct nfs_read_data, pages); |
| 391 | list_del(&data->pages); |
| 392 | nfs_readdata_free(data); |
| 393 | } |
| 394 | SetPageError(page); |
| 395 | nfs_readpage_release(req); |
| 396 | return -ENOMEM; |
| 397 | } |
| 398 | |
| 399 | static int nfs_pagein_one(struct list_head *head, struct inode *inode) |
| 400 | { |
| 401 | struct nfs_page *req; |
| 402 | struct page **pages; |
| 403 | struct nfs_read_data *data; |
| 404 | unsigned int count; |
| 405 | |
| 406 | if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) |
| 407 | return nfs_pagein_multi(head, inode); |
| 408 | |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 409 | data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!data) |
| 411 | goto out_bad; |
| 412 | |
| 413 | INIT_LIST_HEAD(&data->pages); |
| 414 | pages = data->pagevec; |
| 415 | count = 0; |
| 416 | while (!list_empty(head)) { |
| 417 | req = nfs_list_entry(head->next); |
| 418 | nfs_list_remove_request(req); |
| 419 | nfs_list_add_request(req, &data->pages); |
| 420 | ClearPageError(req->wb_page); |
| 421 | *pages++ = req->wb_page; |
| 422 | count += req->wb_bytes; |
| 423 | } |
| 424 | req = nfs_list_entry(data->pages.next); |
| 425 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 426 | nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | nfs_execute_read(data); |
| 429 | return 0; |
| 430 | out_bad: |
| 431 | nfs_async_read_error(head); |
| 432 | return -ENOMEM; |
| 433 | } |
| 434 | |
| 435 | static int |
| 436 | nfs_pagein_list(struct list_head *head, int rpages) |
| 437 | { |
| 438 | LIST_HEAD(one_request); |
| 439 | struct nfs_page *req; |
| 440 | int error = 0; |
| 441 | unsigned int pages = 0; |
| 442 | |
| 443 | while (!list_empty(head)) { |
| 444 | pages += nfs_coalesce_requests(head, &one_request, rpages); |
| 445 | req = nfs_list_entry(one_request.next); |
| 446 | error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode); |
| 447 | if (error < 0) |
| 448 | break; |
| 449 | } |
| 450 | if (error >= 0) |
| 451 | return pages; |
| 452 | |
| 453 | nfs_async_read_error(head); |
| 454 | return error; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Handle a read reply that fills part of a page. |
| 459 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 460 | static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 462 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | struct nfs_page *req = data->req; |
| 464 | struct page *page = req->wb_page; |
| 465 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 466 | if (likely(task->tk_status >= 0)) |
| 467 | nfs_readpage_truncate_uninitialised_page(data); |
| 468 | else |
| 469 | SetPageError(page); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 470 | if (nfs_readpage_result(task, data) != 0) |
| 471 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (atomic_dec_and_test(&req->wb_complete)) { |
| 473 | if (!PageError(page)) |
| 474 | SetPageUptodate(page); |
| 475 | nfs_readpage_release(req); |
| 476 | } |
| 477 | } |
| 478 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 479 | static const struct rpc_call_ops nfs_read_partial_ops = { |
| 480 | .rpc_call_done = nfs_readpage_result_partial, |
| 481 | .rpc_release = nfs_readdata_release, |
| 482 | }; |
| 483 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 484 | static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data) |
| 485 | { |
| 486 | unsigned int count = data->res.count; |
| 487 | unsigned int base = data->args.pgbase; |
| 488 | struct page **pages; |
| 489 | |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 490 | if (data->res.eof) |
| 491 | count = data->args.count; |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 492 | if (unlikely(count == 0)) |
| 493 | return; |
| 494 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 495 | base &= ~PAGE_CACHE_MASK; |
| 496 | count += base; |
| 497 | for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) |
| 498 | SetPageUptodate(*pages); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 499 | if (count != 0) |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 500 | SetPageUptodate(*pages); |
| 501 | } |
| 502 | |
| 503 | static void nfs_readpage_set_pages_error(struct nfs_read_data *data) |
| 504 | { |
| 505 | unsigned int count = data->args.count; |
| 506 | unsigned int base = data->args.pgbase; |
| 507 | struct page **pages; |
| 508 | |
| 509 | pages = &data->args.pages[base >> PAGE_CACHE_SHIFT]; |
| 510 | base &= ~PAGE_CACHE_MASK; |
| 511 | count += base; |
| 512 | for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++) |
| 513 | SetPageError(*pages); |
Trond Myklebust | 79558f3 | 2006-08-22 13:44:32 -0400 | [diff] [blame] | 514 | if (count != 0) |
| 515 | SetPageError(*pages); |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* |
| 519 | * This is the callback from RPC telling us whether a reply was |
| 520 | * received or some error occurred (timeout or socket shutdown). |
| 521 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 522 | static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 524 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 526 | /* |
| 527 | * Note: nfs_readpage_result may change the values of |
| 528 | * data->args. In the multi-page case, we therefore need |
| 529 | * to ensure that we call the next nfs_readpage_set_page_uptodate() |
| 530 | * first in the multi-page case. |
| 531 | */ |
| 532 | if (likely(task->tk_status >= 0)) { |
| 533 | nfs_readpage_truncate_uninitialised_page(data); |
| 534 | nfs_readpage_set_pages_uptodate(data); |
| 535 | } else |
| 536 | nfs_readpage_set_pages_error(data); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 537 | if (nfs_readpage_result(task, data) != 0) |
| 538 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | while (!list_empty(&data->pages)) { |
| 540 | struct nfs_page *req = nfs_list_entry(data->pages.next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Trond Myklebust | 1de3fc1 | 2006-05-25 01:40:44 -0400 | [diff] [blame] | 542 | nfs_list_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | nfs_readpage_release(req); |
| 544 | } |
| 545 | } |
| 546 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 547 | static const struct rpc_call_ops nfs_read_full_ops = { |
| 548 | .rpc_call_done = nfs_readpage_result_full, |
| 549 | .rpc_release = nfs_readdata_release, |
| 550 | }; |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* |
| 553 | * This is the callback from RPC telling us whether a reply was |
| 554 | * received or some error occurred (timeout or socket shutdown). |
| 555 | */ |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 556 | int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | struct nfs_readargs *argp = &data->args; |
| 559 | struct nfs_readres *resp = &data->res; |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 560 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | dprintk("NFS: %4d nfs_readpage_result, (status %d)\n", |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 563 | task->tk_pid, task->tk_status); |
| 564 | |
| 565 | status = NFS_PROTO(data->inode)->read_done(task, data); |
| 566 | if (status != 0) |
| 567 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 569 | nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count); |
| 570 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 571 | if (task->tk_status < 0) { |
| 572 | if (task->tk_status == -ESTALE) { |
| 573 | set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode)); |
| 574 | nfs_mark_for_revalidate(data->inode); |
| 575 | } |
| 576 | } else if (resp->count < argp->count && !resp->eof) { |
| 577 | /* This is a short read! */ |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 578 | nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* Has the server at least made some progress? */ |
| 580 | if (resp->count != 0) { |
| 581 | /* Yes, so retry the read at the end of the data */ |
| 582 | argp->offset += resp->count; |
| 583 | argp->pgbase += resp->count; |
| 584 | argp->count -= resp->count; |
| 585 | rpc_restart_call(task); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 586 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | task->tk_status = -EIO; |
| 589 | } |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 590 | spin_lock(&data->inode->i_lock); |
Chuck Lever | 5529680 | 2005-08-18 11:24:09 -0700 | [diff] [blame] | 591 | NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME; |
Chuck Lever | dc59250 | 2005-08-18 11:24:12 -0700 | [diff] [blame] | 592 | spin_unlock(&data->inode->i_lock); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 593 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Read a page over NFS. |
| 598 | * We read the page synchronously in the following case: |
| 599 | * - The error flag is set for this page. This happens only when a |
| 600 | * previous async read operation failed. |
| 601 | */ |
| 602 | int nfs_readpage(struct file *file, struct page *page) |
| 603 | { |
| 604 | struct nfs_open_context *ctx; |
| 605 | struct inode *inode = page->mapping->host; |
| 606 | int error; |
| 607 | |
| 608 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", |
| 609 | page, PAGE_CACHE_SIZE, page->index); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 610 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
| 611 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Try to flush any pending writes to the file.. |
| 615 | * |
| 616 | * NOTE! Because we own the page lock, there cannot |
| 617 | * be any new pending writes generated at this point |
| 618 | * for this page (other pages can be written to). |
| 619 | */ |
| 620 | error = nfs_wb_page(inode, page); |
| 621 | if (error) |
| 622 | goto out_error; |
| 623 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 624 | error = -ESTALE; |
| 625 | if (NFS_STALE(inode)) |
| 626 | goto out_error; |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (file == NULL) { |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 629 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | if (ctx == NULL) |
| 631 | return -EBADF; |
| 632 | } else |
| 633 | ctx = get_nfs_open_context((struct nfs_open_context *) |
| 634 | file->private_data); |
| 635 | if (!IS_SYNC(inode)) { |
| 636 | error = nfs_readpage_async(ctx, inode, page); |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | error = nfs_readpage_sync(ctx, inode, page); |
| 641 | if (error < 0 && IS_SWAPFILE(inode)) |
| 642 | printk("Aiee.. nfs swap-in of page failed!\n"); |
| 643 | out: |
| 644 | put_nfs_open_context(ctx); |
| 645 | return error; |
| 646 | |
| 647 | out_error: |
| 648 | unlock_page(page); |
| 649 | return error; |
| 650 | } |
| 651 | |
| 652 | struct nfs_readdesc { |
| 653 | struct list_head *head; |
| 654 | struct nfs_open_context *ctx; |
| 655 | }; |
| 656 | |
| 657 | static int |
| 658 | readpage_async_filler(void *data, struct page *page) |
| 659 | { |
| 660 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; |
| 661 | struct inode *inode = page->mapping->host; |
| 662 | struct nfs_page *new; |
| 663 | unsigned int len; |
| 664 | |
| 665 | nfs_wb_page(inode, page); |
| 666 | len = nfs_page_length(inode, page); |
| 667 | if (len == 0) |
| 668 | return nfs_return_empty_page(page); |
| 669 | new = nfs_create_request(desc->ctx, inode, page, 0, len); |
| 670 | if (IS_ERR(new)) { |
| 671 | SetPageError(page); |
| 672 | unlock_page(page); |
| 673 | return PTR_ERR(new); |
| 674 | } |
| 675 | if (len < PAGE_CACHE_SIZE) |
| 676 | memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | nfs_list_add_request(new, desc->head); |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | int nfs_readpages(struct file *filp, struct address_space *mapping, |
| 682 | struct list_head *pages, unsigned nr_pages) |
| 683 | { |
| 684 | LIST_HEAD(head); |
| 685 | struct nfs_readdesc desc = { |
| 686 | .head = &head, |
| 687 | }; |
| 688 | struct inode *inode = mapping->host; |
| 689 | struct nfs_server *server = NFS_SERVER(inode); |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 690 | int ret = -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", |
| 693 | inode->i_sb->s_id, |
| 694 | (long long)NFS_FILEID(inode), |
| 695 | nr_pages); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 696 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 698 | if (NFS_STALE(inode)) |
| 699 | goto out; |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (filp == NULL) { |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 702 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | if (desc.ctx == NULL) |
| 704 | return -EBADF; |
| 705 | } else |
| 706 | desc.ctx = get_nfs_open_context((struct nfs_open_context *) |
| 707 | filp->private_data); |
| 708 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
| 709 | if (!list_empty(&head)) { |
| 710 | int err = nfs_pagein_list(&head, server->rpages); |
| 711 | if (!ret) |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 712 | nfs_add_stats(inode, NFSIOS_READPAGES, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | ret = err; |
| 714 | } |
| 715 | put_nfs_open_context(desc.ctx); |
Trond Myklebust | 5f004cf | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 716 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return ret; |
| 718 | } |
| 719 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 720 | int __init nfs_init_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", |
| 723 | sizeof(struct nfs_read_data), |
| 724 | 0, SLAB_HWCACHE_ALIGN, |
| 725 | NULL, NULL); |
| 726 | if (nfs_rdata_cachep == NULL) |
| 727 | return -ENOMEM; |
| 728 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 729 | nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ, |
| 730 | nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (nfs_rdata_mempool == NULL) |
| 732 | return -ENOMEM; |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 737 | void nfs_destroy_readpagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
| 739 | mempool_destroy(nfs_rdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 740 | kmem_cache_destroy(nfs_rdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |