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