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