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