]>
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; |
061ae2ed | 32 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops; |
4a0de55c | 33 | static const struct nfs_rw_ops nfs_rw_read_ops; |
1da177e4 | 34 | |
e18b890b | 35 | static struct kmem_cache *nfs_rdata_cachep; |
1da177e4 | 36 | |
4a0de55c | 37 | static struct nfs_rw_header *nfs_readhdr_alloc(void) |
3feb2d49 | 38 | { |
4a0de55c | 39 | return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL); |
4db6e0b7 FI |
40 | } |
41 | ||
4a0de55c | 42 | static void nfs_readhdr_free(struct nfs_rw_header *rhdr) |
3feb2d49 | 43 | { |
cd841605 | 44 | kmem_cache_free(nfs_rdata_cachep, rhdr); |
3feb2d49 TM |
45 | } |
46 | ||
1da177e4 LT |
47 | static |
48 | int nfs_return_empty_page(struct page *page) | |
49 | { | |
eebd2aa3 | 50 | zero_user(page, 0, PAGE_CACHE_SIZE); |
1da177e4 LT |
51 | SetPageUptodate(page); |
52 | unlock_page(page); | |
53 | return 0; | |
54 | } | |
55 | ||
1abb5088 | 56 | void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, |
fab5fc25 | 57 | struct inode *inode, bool force_mds, |
061ae2ed | 58 | const struct nfs_pgio_completion_ops *compl_ops) |
1751c363 | 59 | { |
fab5fc25 CH |
60 | struct nfs_server *server = NFS_SERVER(inode); |
61 | const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops; | |
62 | ||
63 | #ifdef CONFIG_NFS_V4_1 | |
64 | if (server->pnfs_curr_ld && !force_mds) | |
65 | pg_ops = server->pnfs_curr_ld->pg_read_ops; | |
66 | #endif | |
4a0de55c AS |
67 | nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops, |
68 | server->rsize, 0); | |
1751c363 | 69 | } |
ddda8e0a | 70 | EXPORT_SYMBOL_GPL(nfs_pageio_init_read); |
1751c363 | 71 | |
493292dd TM |
72 | void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) |
73 | { | |
74 | pgio->pg_ops = &nfs_pageio_read_ops; | |
75 | pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; | |
76 | } | |
1f945357 | 77 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); |
493292dd | 78 | |
f42b293d DH |
79 | int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
80 | struct page *page) | |
1da177e4 | 81 | { |
1da177e4 LT |
82 | struct nfs_page *new; |
83 | unsigned int len; | |
c76069bd | 84 | struct nfs_pageio_descriptor pgio; |
1da177e4 | 85 | |
49a70f27 | 86 | len = nfs_page_length(page); |
1da177e4 LT |
87 | if (len == 0) |
88 | return nfs_return_empty_page(page); | |
89 | new = nfs_create_request(ctx, inode, page, 0, len); | |
90 | if (IS_ERR(new)) { | |
91 | unlock_page(page); | |
92 | return PTR_ERR(new); | |
93 | } | |
94 | if (len < PAGE_CACHE_SIZE) | |
eebd2aa3 | 95 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
1da177e4 | 96 | |
fab5fc25 CH |
97 | nfs_pageio_init_read(&pgio, inode, false, |
98 | &nfs_async_read_completion_ops); | |
d8007d4d | 99 | nfs_pageio_add_request(&pgio, new); |
1751c363 | 100 | nfs_pageio_complete(&pgio); |
2701d086 | 101 | NFS_I(inode)->read_io += pgio.pg_bytes_written; |
1da177e4 LT |
102 | return 0; |
103 | } | |
104 | ||
105 | static void nfs_readpage_release(struct nfs_page *req) | |
106 | { | |
3d4ff43d | 107 | struct inode *d_inode = req->wb_context->dentry->d_inode; |
7f8e05f6 DH |
108 | |
109 | if (PageUptodate(req->wb_page)) | |
110 | nfs_readpage_to_fscache(d_inode, req->wb_page, 0); | |
111 | ||
1da177e4 LT |
112 | unlock_page(req->wb_page); |
113 | ||
1e8968c5 | 114 | dprintk("NFS: read done (%s/%Lu %d@%Ld)\n", |
3d4ff43d | 115 | req->wb_context->dentry->d_inode->i_sb->s_id, |
1e8968c5 | 116 | (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), |
1da177e4 LT |
117 | req->wb_bytes, |
118 | (long long)req_offset(req)); | |
10d2c46f | 119 | nfs_release_request(req); |
1da177e4 LT |
120 | } |
121 | ||
4db6e0b7 | 122 | /* Note io was page aligned */ |
061ae2ed | 123 | static void nfs_read_completion(struct nfs_pgio_header *hdr) |
4db6e0b7 FI |
124 | { |
125 | unsigned long bytes = 0; | |
126 | ||
127 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) | |
128 | goto out; | |
4bd8b010 TM |
129 | while (!list_empty(&hdr->pages)) { |
130 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
131 | struct page *page = req->wb_page; | |
132 | ||
133 | if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) { | |
134 | if (bytes > hdr->good_bytes) | |
135 | zero_user(page, 0, PAGE_SIZE); | |
136 | else if (hdr->good_bytes - bytes < PAGE_SIZE) | |
137 | zero_user_segment(page, | |
138 | hdr->good_bytes & ~PAGE_MASK, | |
139 | PAGE_SIZE); | |
4db6e0b7 | 140 | } |
4bd8b010 TM |
141 | bytes += req->wb_bytes; |
142 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { | |
4db6e0b7 | 143 | if (bytes <= hdr->good_bytes) |
4bd8b010 TM |
144 | SetPageUptodate(page); |
145 | } else | |
146 | SetPageUptodate(page); | |
147 | nfs_list_remove_request(req); | |
148 | nfs_readpage_release(req); | |
4db6e0b7 FI |
149 | } |
150 | out: | |
151 | hdr->release(hdr); | |
152 | } | |
153 | ||
1ed26f33 AS |
154 | static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg, |
155 | struct rpc_task_setup *task_setup_data, int how) | |
1da177e4 | 156 | { |
cd841605 | 157 | struct inode *inode = data->header->inode; |
84115e1c | 158 | int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; |
1da177e4 | 159 | |
1ed26f33 AS |
160 | task_setup_data->flags |= swap_flags; |
161 | NFS_PROTO(inode)->read_setup(data, msg); | |
64419a9b AA |
162 | } |
163 | ||
9c7e1b3d | 164 | static int nfs_do_read(struct nfs_pgio_data *data, |
493292dd | 165 | const struct rpc_call_ops *call_ops) |
6e4efd56 | 166 | { |
cd841605 | 167 | struct inode *inode = data->header->inode; |
6e4efd56 | 168 | |
1ed26f33 | 169 | return nfs_initiate_pgio(NFS_CLIENT(inode), data, call_ops, 0, 0); |
1da177e4 LT |
170 | } |
171 | ||
275acaaf TM |
172 | static int |
173 | nfs_do_multiple_reads(struct list_head *head, | |
493292dd | 174 | const struct rpc_call_ops *call_ops) |
275acaaf | 175 | { |
9c7e1b3d | 176 | struct nfs_pgio_data *data; |
275acaaf TM |
177 | int ret = 0; |
178 | ||
179 | while (!list_empty(head)) { | |
180 | int ret2; | |
181 | ||
9c7e1b3d | 182 | data = list_first_entry(head, struct nfs_pgio_data, list); |
275acaaf TM |
183 | list_del_init(&data->list); |
184 | ||
493292dd | 185 | ret2 = nfs_do_read(data, call_ops); |
275acaaf TM |
186 | if (ret == 0) |
187 | ret = ret2; | |
188 | } | |
189 | return ret; | |
190 | } | |
191 | ||
061ae2ed | 192 | static void |
1da177e4 LT |
193 | nfs_async_read_error(struct list_head *head) |
194 | { | |
195 | struct nfs_page *req; | |
196 | ||
197 | while (!list_empty(head)) { | |
198 | req = nfs_list_entry(head->next); | |
199 | nfs_list_remove_request(req); | |
1da177e4 LT |
200 | nfs_readpage_release(req); |
201 | } | |
202 | } | |
203 | ||
061ae2ed FI |
204 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = { |
205 | .error_cleanup = nfs_async_read_error, | |
206 | .completion = nfs_read_completion, | |
207 | }; | |
208 | ||
493292dd | 209 | static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) |
1751c363 | 210 | { |
c0752cdf | 211 | struct nfs_rw_header *rhdr; |
4db6e0b7 | 212 | struct nfs_pgio_header *hdr; |
275acaaf TM |
213 | int ret; |
214 | ||
4a0de55c | 215 | rhdr = nfs_rw_header_alloc(desc->pg_rw_ops); |
4db6e0b7 | 216 | if (!rhdr) { |
061ae2ed | 217 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
4db6e0b7 FI |
218 | return -ENOMEM; |
219 | } | |
220 | hdr = &rhdr->header; | |
4a0de55c | 221 | nfs_pgheader_init(desc, hdr, nfs_rw_header_free); |
4db6e0b7 | 222 | atomic_inc(&hdr->refcnt); |
ef2c488c | 223 | ret = nfs_generic_pgio(desc, hdr); |
50828d7e | 224 | if (ret == 0) |
4db6e0b7 FI |
225 | ret = nfs_do_multiple_reads(&hdr->rpc_list, |
226 | desc->pg_rpc_callops); | |
4db6e0b7 | 227 | if (atomic_dec_and_test(&hdr->refcnt)) |
061ae2ed | 228 | hdr->completion_ops->completion(hdr); |
275acaaf | 229 | return ret; |
1751c363 | 230 | } |
1751c363 TM |
231 | |
232 | static const struct nfs_pageio_ops nfs_pageio_read_ops = { | |
233 | .pg_test = nfs_generic_pg_test, | |
234 | .pg_doio = nfs_generic_pg_readpages, | |
235 | }; | |
236 | ||
0b671301 TM |
237 | /* |
238 | * This is the callback from RPC telling us whether a reply was | |
239 | * received or some error occurred (timeout or socket shutdown). | |
240 | */ | |
0eecb214 AS |
241 | static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data, |
242 | struct inode *inode) | |
0b671301 | 243 | { |
0eecb214 | 244 | int status = NFS_PROTO(inode)->read_done(task, data); |
0b671301 TM |
245 | if (status != 0) |
246 | return status; | |
247 | ||
cd841605 | 248 | nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count); |
0b671301 TM |
249 | |
250 | if (task->tk_status == -ESTALE) { | |
cd841605 FI |
251 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
252 | nfs_mark_for_revalidate(inode); | |
0b671301 | 253 | } |
0b671301 TM |
254 | return 0; |
255 | } | |
256 | ||
9c7e1b3d | 257 | static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data) |
0b671301 | 258 | { |
3c6b899c | 259 | struct nfs_pgio_args *argp = &data->args; |
9137bdf3 | 260 | struct nfs_pgio_res *resp = &data->res; |
0b671301 | 261 | |
0b671301 | 262 | /* This is a short read! */ |
cd841605 | 263 | nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD); |
0b671301 | 264 | /* Has the server at least made some progress? */ |
4db6e0b7 FI |
265 | if (resp->count == 0) { |
266 | nfs_set_pgio_error(data->header, -EIO, argp->offset); | |
d61e612a | 267 | return; |
4db6e0b7 | 268 | } |
0b671301 | 269 | /* Yes, so retry the read at the end of the data */ |
cbdabc7f | 270 | data->mds_offset += resp->count; |
0b671301 TM |
271 | argp->offset += resp->count; |
272 | argp->pgbase += resp->count; | |
273 | argp->count -= resp->count; | |
d00c5d43 | 274 | rpc_restart_call_prepare(task); |
0b671301 TM |
275 | } |
276 | ||
0eecb214 | 277 | static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data) |
1da177e4 | 278 | { |
4db6e0b7 FI |
279 | struct nfs_pgio_header *hdr = data->header; |
280 | ||
0eecb214 | 281 | if (data->res.eof) { |
4db6e0b7 FI |
282 | loff_t bound; |
283 | ||
284 | bound = data->args.offset + data->res.count; | |
285 | spin_lock(&hdr->lock); | |
286 | if (bound < hdr->io_start + hdr->good_bytes) { | |
287 | set_bit(NFS_IOHDR_EOF, &hdr->flags); | |
288 | clear_bit(NFS_IOHDR_ERROR, &hdr->flags); | |
289 | hdr->good_bytes = bound - hdr->io_start; | |
290 | } | |
291 | spin_unlock(&hdr->lock); | |
292 | } else if (data->res.count != data->args.count) | |
293 | nfs_readpage_retry(task, data); | |
fdd1e74c TM |
294 | } |
295 | ||
1da177e4 LT |
296 | /* |
297 | * Read a page over NFS. | |
298 | * We read the page synchronously in the following case: | |
299 | * - The error flag is set for this page. This happens only when a | |
300 | * previous async read operation failed. | |
301 | */ | |
302 | int nfs_readpage(struct file *file, struct page *page) | |
303 | { | |
304 | struct nfs_open_context *ctx; | |
d56b4ddf | 305 | struct inode *inode = page_file_mapping(page)->host; |
1da177e4 LT |
306 | int error; |
307 | ||
308 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", | |
d56b4ddf | 309 | page, PAGE_CACHE_SIZE, page_file_index(page)); |
91d5b470 CL |
310 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
311 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); | |
312 | ||
1da177e4 LT |
313 | /* |
314 | * Try to flush any pending writes to the file.. | |
315 | * | |
316 | * NOTE! Because we own the page lock, there cannot | |
317 | * be any new pending writes generated at this point | |
318 | * for this page (other pages can be written to). | |
319 | */ | |
320 | error = nfs_wb_page(inode, page); | |
321 | if (error) | |
de05a0cc TM |
322 | goto out_unlock; |
323 | if (PageUptodate(page)) | |
324 | goto out_unlock; | |
1da177e4 | 325 | |
5f004cf2 TM |
326 | error = -ESTALE; |
327 | if (NFS_STALE(inode)) | |
de05a0cc | 328 | goto out_unlock; |
5f004cf2 | 329 | |
1da177e4 | 330 | if (file == NULL) { |
cf1308ff | 331 | error = -EBADF; |
d530838b | 332 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
1da177e4 | 333 | if (ctx == NULL) |
de05a0cc | 334 | goto out_unlock; |
1da177e4 | 335 | } else |
cd3758e3 | 336 | ctx = get_nfs_open_context(nfs_file_open_context(file)); |
1da177e4 | 337 | |
9a9fc1c0 DH |
338 | if (!IS_SYNC(inode)) { |
339 | error = nfs_readpage_from_fscache(ctx, inode, page); | |
340 | if (error == 0) | |
341 | goto out; | |
342 | } | |
343 | ||
8e0969f0 TM |
344 | error = nfs_readpage_async(ctx, inode, page); |
345 | ||
9a9fc1c0 | 346 | out: |
1da177e4 LT |
347 | put_nfs_open_context(ctx); |
348 | return error; | |
de05a0cc | 349 | out_unlock: |
1da177e4 LT |
350 | unlock_page(page); |
351 | return error; | |
352 | } | |
353 | ||
354 | struct nfs_readdesc { | |
8b09bee3 | 355 | struct nfs_pageio_descriptor *pgio; |
1da177e4 LT |
356 | struct nfs_open_context *ctx; |
357 | }; | |
358 | ||
359 | static int | |
360 | readpage_async_filler(void *data, struct page *page) | |
361 | { | |
362 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; | |
d56b4ddf | 363 | struct inode *inode = page_file_mapping(page)->host; |
1da177e4 LT |
364 | struct nfs_page *new; |
365 | unsigned int len; | |
de05a0cc TM |
366 | int error; |
367 | ||
49a70f27 | 368 | len = nfs_page_length(page); |
1da177e4 LT |
369 | if (len == 0) |
370 | return nfs_return_empty_page(page); | |
de05a0cc | 371 | |
1da177e4 | 372 | new = nfs_create_request(desc->ctx, inode, page, 0, len); |
de05a0cc TM |
373 | if (IS_ERR(new)) |
374 | goto out_error; | |
375 | ||
1da177e4 | 376 | if (len < PAGE_CACHE_SIZE) |
eebd2aa3 | 377 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
f8512ad0 FI |
378 | if (!nfs_pageio_add_request(desc->pgio, new)) { |
379 | error = desc->pgio->pg_error; | |
380 | goto out_unlock; | |
381 | } | |
1da177e4 | 382 | return 0; |
de05a0cc TM |
383 | out_error: |
384 | error = PTR_ERR(new); | |
de05a0cc TM |
385 | out_unlock: |
386 | unlock_page(page); | |
387 | return error; | |
1da177e4 LT |
388 | } |
389 | ||
390 | int nfs_readpages(struct file *filp, struct address_space *mapping, | |
391 | struct list_head *pages, unsigned nr_pages) | |
392 | { | |
8b09bee3 | 393 | struct nfs_pageio_descriptor pgio; |
1da177e4 | 394 | struct nfs_readdesc desc = { |
8b09bee3 | 395 | .pgio = &pgio, |
1da177e4 LT |
396 | }; |
397 | struct inode *inode = mapping->host; | |
8b09bee3 | 398 | unsigned long npages; |
5f004cf2 | 399 | int ret = -ESTALE; |
1da177e4 | 400 | |
1e8968c5 | 401 | dprintk("NFS: nfs_readpages (%s/%Lu %d)\n", |
1da177e4 | 402 | inode->i_sb->s_id, |
1e8968c5 | 403 | (unsigned long long)NFS_FILEID(inode), |
1da177e4 | 404 | nr_pages); |
91d5b470 | 405 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
1da177e4 | 406 | |
5f004cf2 TM |
407 | if (NFS_STALE(inode)) |
408 | goto out; | |
409 | ||
1da177e4 | 410 | if (filp == NULL) { |
d530838b | 411 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
1da177e4 LT |
412 | if (desc.ctx == NULL) |
413 | return -EBADF; | |
414 | } else | |
cd3758e3 | 415 | desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); |
9a9fc1c0 DH |
416 | |
417 | /* attempt to read as many of the pages as possible from the cache | |
418 | * - this returns -ENOBUFS immediately if the cookie is negative | |
419 | */ | |
420 | ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, | |
421 | pages, &nr_pages); | |
422 | if (ret == 0) | |
423 | goto read_complete; /* all pages were read */ | |
424 | ||
fab5fc25 CH |
425 | nfs_pageio_init_read(&pgio, inode, false, |
426 | &nfs_async_read_completion_ops); | |
8b09bee3 | 427 | |
1da177e4 | 428 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
8b09bee3 TM |
429 | |
430 | nfs_pageio_complete(&pgio); | |
2701d086 | 431 | NFS_I(inode)->read_io += pgio.pg_bytes_written; |
8b09bee3 TM |
432 | npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
433 | nfs_add_stats(inode, NFSIOS_READPAGES, npages); | |
9a9fc1c0 | 434 | read_complete: |
1da177e4 | 435 | put_nfs_open_context(desc.ctx); |
5f004cf2 | 436 | out: |
1da177e4 LT |
437 | return ret; |
438 | } | |
439 | ||
f7b422b1 | 440 | int __init nfs_init_readpagecache(void) |
1da177e4 LT |
441 | { |
442 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", | |
c0752cdf | 443 | sizeof(struct nfs_rw_header), |
1da177e4 | 444 | 0, SLAB_HWCACHE_ALIGN, |
20c2df83 | 445 | NULL); |
1da177e4 LT |
446 | if (nfs_rdata_cachep == NULL) |
447 | return -ENOMEM; | |
448 | ||
1da177e4 LT |
449 | return 0; |
450 | } | |
451 | ||
266bee88 | 452 | void nfs_destroy_readpagecache(void) |
1da177e4 | 453 | { |
1a1d92c1 | 454 | kmem_cache_destroy(nfs_rdata_cachep); |
1da177e4 | 455 | } |
4a0de55c AS |
456 | |
457 | static const struct nfs_rw_ops nfs_rw_read_ops = { | |
a4cdda59 | 458 | .rw_mode = FMODE_READ, |
4a0de55c AS |
459 | .rw_alloc_header = nfs_readhdr_alloc, |
460 | .rw_free_header = nfs_readhdr_free, | |
0eecb214 AS |
461 | .rw_done = nfs_readpage_done, |
462 | .rw_result = nfs_readpage_result, | |
1ed26f33 | 463 | .rw_initiate = nfs_initiate_read, |
4a0de55c | 464 | }; |