]>
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 | |
1da177e4 LT |
8 | */ |
9 | ||
1da177e4 LT |
10 | #include <linux/time.h> |
11 | #include <linux/kernel.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/fcntl.h> | |
14 | #include <linux/stat.h> | |
15 | #include <linux/mm.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/pagemap.h> | |
18 | #include <linux/sunrpc/clnt.h> | |
19 | #include <linux/nfs_fs.h> | |
20 | #include <linux/nfs_page.h> | |
64419a9b | 21 | #include <linux/module.h> |
1da177e4 | 22 | |
f11c88af | 23 | #include "nfs4_fs.h" |
49a70f27 | 24 | #include "internal.h" |
91d5b470 | 25 | #include "iostat.h" |
9a9fc1c0 | 26 | #include "fscache.h" |
91d5b470 | 27 | |
1da177e4 LT |
28 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
29 | ||
1751c363 | 30 | static const struct nfs_pageio_ops nfs_pageio_read_ops; |
4db6e0b7 | 31 | static const struct rpc_call_ops nfs_read_common_ops; |
061ae2ed | 32 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops; |
1da177e4 | 33 | |
e18b890b | 34 | static struct kmem_cache *nfs_rdata_cachep; |
1da177e4 | 35 | |
1385b811 | 36 | struct nfs_read_header *nfs_readhdr_alloc(void) |
3feb2d49 | 37 | { |
4db6e0b7 | 38 | struct nfs_read_header *rhdr; |
3feb2d49 | 39 | |
4db6e0b7 FI |
40 | rhdr = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL); |
41 | if (rhdr) { | |
42 | struct nfs_pgio_header *hdr = &rhdr->header; | |
cd841605 FI |
43 | |
44 | INIT_LIST_HEAD(&hdr->pages); | |
4db6e0b7 FI |
45 | INIT_LIST_HEAD(&hdr->rpc_list); |
46 | spin_lock_init(&hdr->lock); | |
47 | atomic_set(&hdr->refcnt, 0); | |
48 | } | |
49 | return rhdr; | |
50 | } | |
51 | ||
584aa810 FI |
52 | static struct nfs_read_data *nfs_readdata_alloc(struct nfs_pgio_header *hdr, |
53 | unsigned int pagecount) | |
4db6e0b7 FI |
54 | { |
55 | struct nfs_read_data *data, *prealloc; | |
56 | ||
57 | prealloc = &container_of(hdr, struct nfs_read_header, header)->rpc_data; | |
58 | if (prealloc->header == NULL) | |
59 | data = prealloc; | |
60 | else | |
61 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
62 | if (!data) | |
63 | goto out; | |
64 | ||
65 | if (nfs_pgarray_set(&data->pages, pagecount)) { | |
cd841605 | 66 | data->header = hdr; |
4db6e0b7 FI |
67 | atomic_inc(&hdr->refcnt); |
68 | } else { | |
69 | if (data != prealloc) | |
70 | kfree(data); | |
71 | data = NULL; | |
3feb2d49 | 72 | } |
4db6e0b7 FI |
73 | out: |
74 | return data; | |
3feb2d49 TM |
75 | } |
76 | ||
cd841605 | 77 | void nfs_readhdr_free(struct nfs_pgio_header *hdr) |
3feb2d49 | 78 | { |
cd841605 FI |
79 | struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header); |
80 | ||
81 | kmem_cache_free(nfs_rdata_cachep, rhdr); | |
3feb2d49 TM |
82 | } |
83 | ||
493292dd | 84 | void nfs_readdata_release(struct nfs_read_data *rdata) |
1da177e4 | 85 | { |
4db6e0b7 FI |
86 | struct nfs_pgio_header *hdr = rdata->header; |
87 | struct nfs_read_header *read_header = container_of(hdr, struct nfs_read_header, header); | |
88 | ||
383ba719 | 89 | put_nfs_open_context(rdata->args.context); |
30dd374f FI |
90 | if (rdata->pages.pagevec != rdata->pages.page_array) |
91 | kfree(rdata->pages.pagevec); | |
4db6e0b7 FI |
92 | if (rdata != &read_header->rpc_data) |
93 | kfree(rdata); | |
94 | else | |
95 | rdata->header = NULL; | |
96 | if (atomic_dec_and_test(&hdr->refcnt)) | |
061ae2ed | 97 | hdr->completion_ops->completion(hdr); |
1da177e4 LT |
98 | } |
99 | ||
1da177e4 LT |
100 | static |
101 | int nfs_return_empty_page(struct page *page) | |
102 | { | |
eebd2aa3 | 103 | zero_user(page, 0, PAGE_CACHE_SIZE); |
1da177e4 LT |
104 | SetPageUptodate(page); |
105 | unlock_page(page); | |
106 | return 0; | |
107 | } | |
108 | ||
1abb5088 | 109 | void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, |
061ae2ed FI |
110 | struct inode *inode, |
111 | const struct nfs_pgio_completion_ops *compl_ops) | |
1751c363 | 112 | { |
061ae2ed | 113 | nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, compl_ops, |
1751c363 TM |
114 | NFS_SERVER(inode)->rsize, 0); |
115 | } | |
116 | ||
493292dd TM |
117 | void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) |
118 | { | |
119 | pgio->pg_ops = &nfs_pageio_read_ops; | |
120 | pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; | |
121 | } | |
1f945357 | 122 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); |
493292dd | 123 | |
f42b293d DH |
124 | int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
125 | struct page *page) | |
1da177e4 | 126 | { |
1da177e4 LT |
127 | struct nfs_page *new; |
128 | unsigned int len; | |
c76069bd | 129 | struct nfs_pageio_descriptor pgio; |
1da177e4 | 130 | |
49a70f27 | 131 | len = nfs_page_length(page); |
1da177e4 LT |
132 | if (len == 0) |
133 | return nfs_return_empty_page(page); | |
134 | new = nfs_create_request(ctx, inode, page, 0, len); | |
135 | if (IS_ERR(new)) { | |
136 | unlock_page(page); | |
137 | return PTR_ERR(new); | |
138 | } | |
139 | if (len < PAGE_CACHE_SIZE) | |
eebd2aa3 | 140 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
1da177e4 | 141 | |
1abb5088 | 142 | NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops); |
d8007d4d | 143 | nfs_pageio_add_request(&pgio, new); |
1751c363 | 144 | nfs_pageio_complete(&pgio); |
2701d086 | 145 | NFS_I(inode)->read_io += pgio.pg_bytes_written; |
1da177e4 LT |
146 | return 0; |
147 | } | |
148 | ||
149 | static void nfs_readpage_release(struct nfs_page *req) | |
150 | { | |
3d4ff43d | 151 | struct inode *d_inode = req->wb_context->dentry->d_inode; |
7f8e05f6 DH |
152 | |
153 | if (PageUptodate(req->wb_page)) | |
154 | nfs_readpage_to_fscache(d_inode, req->wb_page, 0); | |
155 | ||
1da177e4 LT |
156 | unlock_page(req->wb_page); |
157 | ||
1da177e4 | 158 | dprintk("NFS: read done (%s/%Ld %d@%Ld)\n", |
3d4ff43d AV |
159 | req->wb_context->dentry->d_inode->i_sb->s_id, |
160 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | |
1da177e4 LT |
161 | req->wb_bytes, |
162 | (long long)req_offset(req)); | |
10d2c46f | 163 | nfs_release_request(req); |
1da177e4 LT |
164 | } |
165 | ||
4db6e0b7 | 166 | /* Note io was page aligned */ |
061ae2ed | 167 | static void nfs_read_completion(struct nfs_pgio_header *hdr) |
4db6e0b7 FI |
168 | { |
169 | unsigned long bytes = 0; | |
170 | ||
171 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) | |
172 | goto out; | |
4bd8b010 TM |
173 | while (!list_empty(&hdr->pages)) { |
174 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
175 | struct page *page = req->wb_page; | |
176 | ||
177 | if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) { | |
178 | if (bytes > hdr->good_bytes) | |
179 | zero_user(page, 0, PAGE_SIZE); | |
180 | else if (hdr->good_bytes - bytes < PAGE_SIZE) | |
181 | zero_user_segment(page, | |
182 | hdr->good_bytes & ~PAGE_MASK, | |
183 | PAGE_SIZE); | |
4db6e0b7 | 184 | } |
4bd8b010 TM |
185 | bytes += req->wb_bytes; |
186 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { | |
4db6e0b7 | 187 | if (bytes <= hdr->good_bytes) |
4bd8b010 TM |
188 | SetPageUptodate(page); |
189 | } else | |
190 | SetPageUptodate(page); | |
191 | nfs_list_remove_request(req); | |
192 | nfs_readpage_release(req); | |
4db6e0b7 FI |
193 | } |
194 | out: | |
195 | hdr->release(hdr); | |
196 | } | |
197 | ||
c5996c4e FI |
198 | int nfs_initiate_read(struct rpc_clnt *clnt, |
199 | struct nfs_read_data *data, | |
9f0ec176 | 200 | const struct rpc_call_ops *call_ops, int flags) |
1da177e4 | 201 | { |
cd841605 | 202 | struct inode *inode = data->header->inode; |
84115e1c | 203 | int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; |
07737691 | 204 | struct rpc_task *task; |
bdc7f021 TM |
205 | struct rpc_message msg = { |
206 | .rpc_argp = &data->args, | |
207 | .rpc_resp = &data->res, | |
cd841605 | 208 | .rpc_cred = data->header->cred, |
bdc7f021 | 209 | }; |
84115e1c | 210 | struct rpc_task_setup task_setup_data = { |
07737691 | 211 | .task = &data->task, |
64419a9b | 212 | .rpc_client = clnt, |
bdc7f021 | 213 | .rpc_message = &msg, |
84115e1c TM |
214 | .callback_ops = call_ops, |
215 | .callback_data = data, | |
101070ca | 216 | .workqueue = nfsiod_workqueue, |
9f0ec176 | 217 | .flags = RPC_TASK_ASYNC | swap_flags | flags, |
84115e1c | 218 | }; |
1da177e4 | 219 | |
64419a9b AA |
220 | /* Set up the initial task struct. */ |
221 | NFS_PROTO(inode)->read_setup(data, &msg); | |
222 | ||
223 | dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ " | |
224 | "offset %llu)\n", | |
225 | data->task.tk_pid, | |
226 | inode->i_sb->s_id, | |
227 | (long long)NFS_FILEID(inode), | |
228 | data->args.count, | |
229 | (unsigned long long)data->args.offset); | |
230 | ||
231 | task = rpc_run_task(&task_setup_data); | |
232 | if (IS_ERR(task)) | |
233 | return PTR_ERR(task); | |
234 | rpc_put_task(task); | |
235 | return 0; | |
236 | } | |
dc70d7b3 | 237 | EXPORT_SYMBOL_GPL(nfs_initiate_read); |
64419a9b AA |
238 | |
239 | /* | |
240 | * Set up the NFS read request struct | |
241 | */ | |
4db6e0b7 | 242 | static void nfs_read_rpcsetup(struct nfs_read_data *data, |
6e4efd56 | 243 | unsigned int count, unsigned int offset) |
64419a9b | 244 | { |
4db6e0b7 | 245 | struct nfs_page *req = data->header->req; |
1da177e4 | 246 | |
4db6e0b7 | 247 | data->args.fh = NFS_FH(data->header->inode); |
1da177e4 LT |
248 | data->args.offset = req_offset(req) + offset; |
249 | data->args.pgbase = req->wb_pgbase + offset; | |
30dd374f | 250 | data->args.pages = data->pages.pagevec; |
1da177e4 | 251 | data->args.count = count; |
383ba719 | 252 | data->args.context = get_nfs_open_context(req->wb_context); |
f11ac8db | 253 | data->args.lock_context = req->wb_lock_context; |
1da177e4 LT |
254 | |
255 | data->res.fattr = &data->fattr; | |
256 | data->res.count = count; | |
257 | data->res.eof = 0; | |
0e574af1 | 258 | nfs_fattr_init(&data->fattr); |
6e4efd56 | 259 | } |
1da177e4 | 260 | |
6e4efd56 | 261 | static int nfs_do_read(struct nfs_read_data *data, |
493292dd | 262 | const struct rpc_call_ops *call_ops) |
6e4efd56 | 263 | { |
cd841605 | 264 | struct inode *inode = data->header->inode; |
6e4efd56 | 265 | |
9f0ec176 | 266 | return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0); |
1da177e4 LT |
267 | } |
268 | ||
275acaaf TM |
269 | static int |
270 | nfs_do_multiple_reads(struct list_head *head, | |
493292dd | 271 | const struct rpc_call_ops *call_ops) |
275acaaf TM |
272 | { |
273 | struct nfs_read_data *data; | |
274 | int ret = 0; | |
275 | ||
276 | while (!list_empty(head)) { | |
277 | int ret2; | |
278 | ||
4db6e0b7 | 279 | data = list_first_entry(head, struct nfs_read_data, list); |
275acaaf TM |
280 | list_del_init(&data->list); |
281 | ||
493292dd | 282 | ret2 = nfs_do_read(data, call_ops); |
275acaaf TM |
283 | if (ret == 0) |
284 | ret = ret2; | |
285 | } | |
286 | return ret; | |
287 | } | |
288 | ||
061ae2ed | 289 | static void |
1da177e4 LT |
290 | nfs_async_read_error(struct list_head *head) |
291 | { | |
292 | struct nfs_page *req; | |
293 | ||
294 | while (!list_empty(head)) { | |
295 | req = nfs_list_entry(head->next); | |
296 | nfs_list_remove_request(req); | |
1da177e4 LT |
297 | nfs_readpage_release(req); |
298 | } | |
299 | } | |
300 | ||
061ae2ed FI |
301 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = { |
302 | .error_cleanup = nfs_async_read_error, | |
303 | .completion = nfs_read_completion, | |
304 | }; | |
305 | ||
25b11dcd TM |
306 | static void nfs_pagein_error(struct nfs_pageio_descriptor *desc, |
307 | struct nfs_pgio_header *hdr) | |
308 | { | |
309 | set_bit(NFS_IOHDR_REDO, &hdr->flags); | |
310 | while (!list_empty(&hdr->rpc_list)) { | |
311 | struct nfs_read_data *data = list_first_entry(&hdr->rpc_list, | |
312 | struct nfs_read_data, list); | |
313 | list_del(&data->list); | |
314 | nfs_readdata_release(data); | |
315 | } | |
316 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); | |
317 | } | |
318 | ||
1da177e4 LT |
319 | /* |
320 | * Generate multiple requests to fill a single page. | |
321 | * | |
322 | * We optimize to reduce the number of read operations on the wire. If we | |
323 | * detect that we're reading a page, or an area of a page, that is past the | |
324 | * end of file, we do not generate NFS read operations but just clear the | |
325 | * parts of the page that would have come back zero from the server anyway. | |
326 | * | |
327 | * We rely on the cached value of i_size to make this determination; another | |
328 | * client can fill pages on the server past our cached end-of-file, but we | |
329 | * won't see the new data until our attribute cache is updated. This is more | |
330 | * or less conventional NFS client behavior. | |
331 | */ | |
4db6e0b7 FI |
332 | static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, |
333 | struct nfs_pgio_header *hdr) | |
1da177e4 | 334 | { |
4db6e0b7 | 335 | struct nfs_page *req = hdr->req; |
1da177e4 LT |
336 | struct page *page = req->wb_page; |
337 | struct nfs_read_data *data; | |
d097971d | 338 | size_t rsize = desc->pg_bsize, nbytes; |
e9f7bee1 | 339 | unsigned int offset; |
1da177e4 | 340 | |
275acaaf | 341 | offset = 0; |
c76069bd | 342 | nbytes = desc->pg_count; |
e9f7bee1 TM |
343 | do { |
344 | size_t len = min(nbytes,rsize); | |
345 | ||
4db6e0b7 | 346 | data = nfs_readdata_alloc(hdr, 1); |
25b11dcd TM |
347 | if (!data) { |
348 | nfs_pagein_error(desc, hdr); | |
349 | return -ENOMEM; | |
350 | } | |
30dd374f | 351 | data->pages.pagevec[0] = page; |
4db6e0b7 FI |
352 | nfs_read_rpcsetup(data, len, offset); |
353 | list_add(&data->list, &hdr->rpc_list); | |
e9f7bee1 | 354 | nbytes -= len; |
275acaaf | 355 | offset += len; |
9146ab50 | 356 | } while (nbytes != 0); |
25b11dcd TM |
357 | |
358 | nfs_list_remove_request(req); | |
359 | nfs_list_add_request(req, &hdr->pages); | |
4db6e0b7 | 360 | desc->pg_rpc_callops = &nfs_read_common_ops; |
9146ab50 | 361 | return 0; |
1da177e4 LT |
362 | } |
363 | ||
4db6e0b7 FI |
364 | static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, |
365 | struct nfs_pgio_header *hdr) | |
1da177e4 LT |
366 | { |
367 | struct nfs_page *req; | |
368 | struct page **pages; | |
4db6e0b7 | 369 | struct nfs_read_data *data; |
c76069bd | 370 | struct list_head *head = &desc->pg_list; |
1da177e4 | 371 | |
4db6e0b7 FI |
372 | data = nfs_readdata_alloc(hdr, nfs_page_array_len(desc->pg_base, |
373 | desc->pg_count)); | |
374 | if (!data) { | |
25b11dcd | 375 | nfs_pagein_error(desc, hdr); |
9146ab50 | 376 | return -ENOMEM; |
bae724ef | 377 | } |
1da177e4 | 378 | |
30dd374f | 379 | pages = data->pages.pagevec; |
1da177e4 LT |
380 | while (!list_empty(head)) { |
381 | req = nfs_list_entry(head->next); | |
382 | nfs_list_remove_request(req); | |
4db6e0b7 | 383 | nfs_list_add_request(req, &hdr->pages); |
1da177e4 | 384 | *pages++ = req->wb_page; |
1da177e4 | 385 | } |
1da177e4 | 386 | |
4db6e0b7 FI |
387 | nfs_read_rpcsetup(data, desc->pg_count, 0); |
388 | list_add(&data->list, &hdr->rpc_list); | |
389 | desc->pg_rpc_callops = &nfs_read_common_ops; | |
9146ab50 | 390 | return 0; |
1da177e4 LT |
391 | } |
392 | ||
4db6e0b7 FI |
393 | int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, |
394 | struct nfs_pgio_header *hdr) | |
493292dd TM |
395 | { |
396 | if (desc->pg_bsize < PAGE_CACHE_SIZE) | |
4db6e0b7 FI |
397 | return nfs_pagein_multi(desc, hdr); |
398 | return nfs_pagein_one(desc, hdr); | |
493292dd TM |
399 | } |
400 | ||
401 | static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) | |
1751c363 | 402 | { |
4db6e0b7 FI |
403 | struct nfs_read_header *rhdr; |
404 | struct nfs_pgio_header *hdr; | |
275acaaf TM |
405 | int ret; |
406 | ||
4db6e0b7 FI |
407 | rhdr = nfs_readhdr_alloc(); |
408 | if (!rhdr) { | |
061ae2ed | 409 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
4db6e0b7 FI |
410 | return -ENOMEM; |
411 | } | |
412 | hdr = &rhdr->header; | |
413 | nfs_pgheader_init(desc, hdr, nfs_readhdr_free); | |
414 | atomic_inc(&hdr->refcnt); | |
415 | ret = nfs_generic_pagein(desc, hdr); | |
50828d7e | 416 | if (ret == 0) |
4db6e0b7 FI |
417 | ret = nfs_do_multiple_reads(&hdr->rpc_list, |
418 | desc->pg_rpc_callops); | |
4db6e0b7 | 419 | if (atomic_dec_and_test(&hdr->refcnt)) |
061ae2ed | 420 | hdr->completion_ops->completion(hdr); |
275acaaf | 421 | return ret; |
1751c363 | 422 | } |
1751c363 TM |
423 | |
424 | static const struct nfs_pageio_ops nfs_pageio_read_ops = { | |
425 | .pg_test = nfs_generic_pg_test, | |
426 | .pg_doio = nfs_generic_pg_readpages, | |
427 | }; | |
428 | ||
0b671301 TM |
429 | /* |
430 | * This is the callback from RPC telling us whether a reply was | |
431 | * received or some error occurred (timeout or socket shutdown). | |
432 | */ | |
433 | int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data) | |
434 | { | |
cd841605 | 435 | struct inode *inode = data->header->inode; |
0b671301 TM |
436 | int status; |
437 | ||
3110ff80 | 438 | dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid, |
0b671301 TM |
439 | task->tk_status); |
440 | ||
cd841605 | 441 | status = NFS_PROTO(inode)->read_done(task, data); |
0b671301 TM |
442 | if (status != 0) |
443 | return status; | |
444 | ||
cd841605 | 445 | nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count); |
0b671301 TM |
446 | |
447 | if (task->tk_status == -ESTALE) { | |
cd841605 FI |
448 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
449 | nfs_mark_for_revalidate(inode); | |
0b671301 | 450 | } |
0b671301 TM |
451 | return 0; |
452 | } | |
453 | ||
fdd1e74c | 454 | static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data) |
0b671301 TM |
455 | { |
456 | struct nfs_readargs *argp = &data->args; | |
457 | struct nfs_readres *resp = &data->res; | |
458 | ||
0b671301 | 459 | /* This is a short read! */ |
cd841605 | 460 | nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD); |
0b671301 | 461 | /* Has the server at least made some progress? */ |
4db6e0b7 FI |
462 | if (resp->count == 0) { |
463 | nfs_set_pgio_error(data->header, -EIO, argp->offset); | |
d61e612a | 464 | return; |
4db6e0b7 | 465 | } |
0b671301 | 466 | /* Yes, so retry the read at the end of the data */ |
cbdabc7f | 467 | data->mds_offset += resp->count; |
0b671301 TM |
468 | argp->offset += resp->count; |
469 | argp->pgbase += resp->count; | |
470 | argp->count -= resp->count; | |
d00c5d43 | 471 | rpc_restart_call_prepare(task); |
0b671301 TM |
472 | } |
473 | ||
4db6e0b7 | 474 | static void nfs_readpage_result_common(struct rpc_task *task, void *calldata) |
1da177e4 | 475 | { |
ec06c096 | 476 | struct nfs_read_data *data = calldata; |
4db6e0b7 FI |
477 | struct nfs_pgio_header *hdr = data->header; |
478 | ||
479 | /* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */ | |
ec06c096 TM |
480 | if (nfs_readpage_result(task, data) != 0) |
481 | return; | |
fdd1e74c | 482 | if (task->tk_status < 0) |
4db6e0b7 FI |
483 | nfs_set_pgio_error(hdr, task->tk_status, data->args.offset); |
484 | else if (data->res.eof) { | |
485 | loff_t bound; | |
486 | ||
487 | bound = data->args.offset + data->res.count; | |
488 | spin_lock(&hdr->lock); | |
489 | if (bound < hdr->io_start + hdr->good_bytes) { | |
490 | set_bit(NFS_IOHDR_EOF, &hdr->flags); | |
491 | clear_bit(NFS_IOHDR_ERROR, &hdr->flags); | |
492 | hdr->good_bytes = bound - hdr->io_start; | |
493 | } | |
494 | spin_unlock(&hdr->lock); | |
495 | } else if (data->res.count != data->args.count) | |
496 | nfs_readpage_retry(task, data); | |
fdd1e74c TM |
497 | } |
498 | ||
4db6e0b7 | 499 | static void nfs_readpage_release_common(void *calldata) |
fdd1e74c | 500 | { |
4db6e0b7 | 501 | nfs_readdata_release(calldata); |
1da177e4 LT |
502 | } |
503 | ||
f11c88af AA |
504 | void nfs_read_prepare(struct rpc_task *task, void *calldata) |
505 | { | |
506 | struct nfs_read_data *data = calldata; | |
cd841605 | 507 | NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data); |
f11c88af | 508 | } |
f11c88af | 509 | |
4db6e0b7 | 510 | static const struct rpc_call_ops nfs_read_common_ops = { |
f11c88af | 511 | .rpc_call_prepare = nfs_read_prepare, |
4db6e0b7 FI |
512 | .rpc_call_done = nfs_readpage_result_common, |
513 | .rpc_release = nfs_readpage_release_common, | |
ec06c096 TM |
514 | }; |
515 | ||
1da177e4 LT |
516 | /* |
517 | * Read a page over NFS. | |
518 | * We read the page synchronously in the following case: | |
519 | * - The error flag is set for this page. This happens only when a | |
520 | * previous async read operation failed. | |
521 | */ | |
522 | int nfs_readpage(struct file *file, struct page *page) | |
523 | { | |
524 | struct nfs_open_context *ctx; | |
525 | struct inode *inode = page->mapping->host; | |
526 | int error; | |
527 | ||
528 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", | |
529 | page, PAGE_CACHE_SIZE, page->index); | |
91d5b470 CL |
530 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
531 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); | |
532 | ||
1da177e4 LT |
533 | /* |
534 | * Try to flush any pending writes to the file.. | |
535 | * | |
536 | * NOTE! Because we own the page lock, there cannot | |
537 | * be any new pending writes generated at this point | |
538 | * for this page (other pages can be written to). | |
539 | */ | |
540 | error = nfs_wb_page(inode, page); | |
541 | if (error) | |
de05a0cc TM |
542 | goto out_unlock; |
543 | if (PageUptodate(page)) | |
544 | goto out_unlock; | |
1da177e4 | 545 | |
5f004cf2 TM |
546 | error = -ESTALE; |
547 | if (NFS_STALE(inode)) | |
de05a0cc | 548 | goto out_unlock; |
5f004cf2 | 549 | |
1da177e4 | 550 | if (file == NULL) { |
cf1308ff | 551 | error = -EBADF; |
d530838b | 552 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
1da177e4 | 553 | if (ctx == NULL) |
de05a0cc | 554 | goto out_unlock; |
1da177e4 | 555 | } else |
cd3758e3 | 556 | ctx = get_nfs_open_context(nfs_file_open_context(file)); |
1da177e4 | 557 | |
9a9fc1c0 DH |
558 | if (!IS_SYNC(inode)) { |
559 | error = nfs_readpage_from_fscache(ctx, inode, page); | |
560 | if (error == 0) | |
561 | goto out; | |
562 | } | |
563 | ||
8e0969f0 TM |
564 | error = nfs_readpage_async(ctx, inode, page); |
565 | ||
9a9fc1c0 | 566 | out: |
1da177e4 LT |
567 | put_nfs_open_context(ctx); |
568 | return error; | |
de05a0cc | 569 | out_unlock: |
1da177e4 LT |
570 | unlock_page(page); |
571 | return error; | |
572 | } | |
573 | ||
574 | struct nfs_readdesc { | |
8b09bee3 | 575 | struct nfs_pageio_descriptor *pgio; |
1da177e4 LT |
576 | struct nfs_open_context *ctx; |
577 | }; | |
578 | ||
579 | static int | |
580 | readpage_async_filler(void *data, struct page *page) | |
581 | { | |
582 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; | |
583 | struct inode *inode = page->mapping->host; | |
584 | struct nfs_page *new; | |
585 | unsigned int len; | |
de05a0cc TM |
586 | int error; |
587 | ||
49a70f27 | 588 | len = nfs_page_length(page); |
1da177e4 LT |
589 | if (len == 0) |
590 | return nfs_return_empty_page(page); | |
de05a0cc | 591 | |
1da177e4 | 592 | new = nfs_create_request(desc->ctx, inode, page, 0, len); |
de05a0cc TM |
593 | if (IS_ERR(new)) |
594 | goto out_error; | |
595 | ||
1da177e4 | 596 | if (len < PAGE_CACHE_SIZE) |
eebd2aa3 | 597 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
f8512ad0 FI |
598 | if (!nfs_pageio_add_request(desc->pgio, new)) { |
599 | error = desc->pgio->pg_error; | |
600 | goto out_unlock; | |
601 | } | |
1da177e4 | 602 | return 0; |
de05a0cc TM |
603 | out_error: |
604 | error = PTR_ERR(new); | |
de05a0cc TM |
605 | out_unlock: |
606 | unlock_page(page); | |
607 | return error; | |
1da177e4 LT |
608 | } |
609 | ||
610 | int nfs_readpages(struct file *filp, struct address_space *mapping, | |
611 | struct list_head *pages, unsigned nr_pages) | |
612 | { | |
8b09bee3 | 613 | struct nfs_pageio_descriptor pgio; |
1da177e4 | 614 | struct nfs_readdesc desc = { |
8b09bee3 | 615 | .pgio = &pgio, |
1da177e4 LT |
616 | }; |
617 | struct inode *inode = mapping->host; | |
8b09bee3 | 618 | unsigned long npages; |
5f004cf2 | 619 | int ret = -ESTALE; |
1da177e4 LT |
620 | |
621 | dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", | |
622 | inode->i_sb->s_id, | |
623 | (long long)NFS_FILEID(inode), | |
624 | nr_pages); | |
91d5b470 | 625 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
1da177e4 | 626 | |
5f004cf2 TM |
627 | if (NFS_STALE(inode)) |
628 | goto out; | |
629 | ||
1da177e4 | 630 | if (filp == NULL) { |
d530838b | 631 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
1da177e4 LT |
632 | if (desc.ctx == NULL) |
633 | return -EBADF; | |
634 | } else | |
cd3758e3 | 635 | desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); |
9a9fc1c0 DH |
636 | |
637 | /* attempt to read as many of the pages as possible from the cache | |
638 | * - this returns -ENOBUFS immediately if the cookie is negative | |
639 | */ | |
640 | ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, | |
641 | pages, &nr_pages); | |
642 | if (ret == 0) | |
643 | goto read_complete; /* all pages were read */ | |
644 | ||
1abb5088 | 645 | NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops); |
8b09bee3 | 646 | |
1da177e4 | 647 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
8b09bee3 TM |
648 | |
649 | nfs_pageio_complete(&pgio); | |
2701d086 | 650 | NFS_I(inode)->read_io += pgio.pg_bytes_written; |
8b09bee3 TM |
651 | npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
652 | nfs_add_stats(inode, NFSIOS_READPAGES, npages); | |
9a9fc1c0 | 653 | read_complete: |
1da177e4 | 654 | put_nfs_open_context(desc.ctx); |
5f004cf2 | 655 | out: |
1da177e4 LT |
656 | return ret; |
657 | } | |
658 | ||
f7b422b1 | 659 | int __init nfs_init_readpagecache(void) |
1da177e4 LT |
660 | { |
661 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", | |
cd841605 | 662 | sizeof(struct nfs_read_header), |
1da177e4 | 663 | 0, SLAB_HWCACHE_ALIGN, |
20c2df83 | 664 | NULL); |
1da177e4 LT |
665 | if (nfs_rdata_cachep == NULL) |
666 | return -ENOMEM; | |
667 | ||
1da177e4 LT |
668 | return 0; |
669 | } | |
670 | ||
266bee88 | 671 | void nfs_destroy_readpagecache(void) |
1da177e4 | 672 | { |
1a1d92c1 | 673 | kmem_cache_destroy(nfs_rdata_cachep); |
1da177e4 | 674 | } |