read.c 15.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * linux/fs/nfs/read.c
 *
 * Block I/O for NFS
 *
 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
 * modified for async RPC by okir@monad.swb.de
 */

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_page.h>
A
Andy Adamson 已提交
21
#include <linux/module.h>
L
Linus Torvalds 已提交
22

23
#include "nfs4_fs.h"
24
#include "internal.h"
C
Chuck Lever 已提交
25
#include "iostat.h"
26
#include "fscache.h"
27
#include "pnfs.h"
C
Chuck Lever 已提交
28

L
Linus Torvalds 已提交
29 30
#define NFSDBG_FACILITY		NFSDBG_PAGECACHE

31
static const struct nfs_pageio_ops nfs_pageio_read_ops;
32
static const struct rpc_call_ops nfs_read_common_ops;
33
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
34
static const struct nfs_rw_ops nfs_rw_read_ops;
L
Linus Torvalds 已提交
35

36
static struct kmem_cache *nfs_rdata_cachep;
L
Linus Torvalds 已提交
37

38
static struct nfs_rw_header *nfs_readhdr_alloc(void)
39
{
40
	return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
41 42
}

43
static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
44
{
45
	kmem_cache_free(nfs_rdata_cachep, rhdr);
46 47
}

L
Linus Torvalds 已提交
48 49 50
static
int nfs_return_empty_page(struct page *page)
{
51
	zero_user(page, 0, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
52 53 54 55 56
	SetPageUptodate(page);
	unlock_page(page);
	return 0;
}

57
void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
58
			      struct inode *inode, bool force_mds,
59
			      const struct nfs_pgio_completion_ops *compl_ops)
60
{
61 62 63 64 65 66 67
	struct nfs_server *server = NFS_SERVER(inode);
	const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;

#ifdef CONFIG_NFS_V4_1
	if (server->pnfs_curr_ld && !force_mds)
		pg_ops = server->pnfs_curr_ld->pg_read_ops;
#endif
68 69
	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
			server->rsize, 0);
70
}
B
Bryan Schumaker 已提交
71
EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
72

73 74 75 76 77
void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
{
	pgio->pg_ops = &nfs_pageio_read_ops;
	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
}
78
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
79

80 81
int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
		       struct page *page)
L
Linus Torvalds 已提交
82 83 84
{
	struct nfs_page	*new;
	unsigned int len;
F
Fred Isaman 已提交
85
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
86

87
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95
	if (len == 0)
		return nfs_return_empty_page(page);
	new = nfs_create_request(ctx, inode, page, 0, len);
	if (IS_ERR(new)) {
		unlock_page(page);
		return PTR_ERR(new);
	}
	if (len < PAGE_CACHE_SIZE)
96
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
97

98 99
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
100
	nfs_pageio_add_request(&pgio, new);
101
	nfs_pageio_complete(&pgio);
102
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
L
Linus Torvalds 已提交
103 104 105 106 107
	return 0;
}

static void nfs_readpage_release(struct nfs_page *req)
{
108
	struct inode *d_inode = req->wb_context->dentry->d_inode;
109 110 111 112

	if (PageUptodate(req->wb_page))
		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);

L
Linus Torvalds 已提交
113 114
	unlock_page(req->wb_page);

115
	dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
116
			req->wb_context->dentry->d_inode->i_sb->s_id,
117
			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
L
Linus Torvalds 已提交
118 119
			req->wb_bytes,
			(long long)req_offset(req));
120
	nfs_release_request(req);
L
Linus Torvalds 已提交
121 122
}

123
/* Note io was page aligned */
124
static void nfs_read_completion(struct nfs_pgio_header *hdr)
125 126 127 128 129
{
	unsigned long bytes = 0;

	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
		goto out;
130 131 132 133 134 135 136 137 138 139 140
	while (!list_empty(&hdr->pages)) {
		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
		struct page *page = req->wb_page;

		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
			if (bytes > hdr->good_bytes)
				zero_user(page, 0, PAGE_SIZE);
			else if (hdr->good_bytes - bytes < PAGE_SIZE)
				zero_user_segment(page,
					hdr->good_bytes & ~PAGE_MASK,
					PAGE_SIZE);
141
		}
142 143
		bytes += req->wb_bytes;
		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
144
			if (bytes <= hdr->good_bytes)
145 146 147 148 149
				SetPageUptodate(page);
		} else
			SetPageUptodate(page);
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
150 151 152 153 154
	}
out:
	hdr->release(hdr);
}

155
int nfs_initiate_read(struct rpc_clnt *clnt,
156
		      struct nfs_pgio_data *data,
157
		      const struct rpc_call_ops *call_ops, int flags)
L
Linus Torvalds 已提交
158
{
159
	struct inode *inode = data->header->inode;
160
	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
161
	struct rpc_task *task;
162 163 164
	struct rpc_message msg = {
		.rpc_argp = &data->args,
		.rpc_resp = &data->res,
165
		.rpc_cred = data->header->cred,
166
	};
167
	struct rpc_task_setup task_setup_data = {
168
		.task = &data->task,
A
Andy Adamson 已提交
169
		.rpc_client = clnt,
170
		.rpc_message = &msg,
171 172
		.callback_ops = call_ops,
		.callback_data = data,
173
		.workqueue = nfsiod_workqueue,
174
		.flags = RPC_TASK_ASYNC | swap_flags | flags,
175
	};
L
Linus Torvalds 已提交
176

A
Andy Adamson 已提交
177 178 179
	/* Set up the initial task struct. */
	NFS_PROTO(inode)->read_setup(data, &msg);

180
	dprintk("NFS: %5u initiated read call (req %s/%llu, %u bytes @ "
A
Andy Adamson 已提交
181 182 183
			"offset %llu)\n",
			data->task.tk_pid,
			inode->i_sb->s_id,
184
			(unsigned long long)NFS_FILEID(inode),
A
Andy Adamson 已提交
185 186 187 188 189 190 191 192 193
			data->args.count,
			(unsigned long long)data->args.offset);

	task = rpc_run_task(&task_setup_data);
	if (IS_ERR(task))
		return PTR_ERR(task);
	rpc_put_task(task);
	return 0;
}
A
Andy Adamson 已提交
194
EXPORT_SYMBOL_GPL(nfs_initiate_read);
A
Andy Adamson 已提交
195 196 197 198

/*
 * Set up the NFS read request struct
 */
199
static void nfs_read_rpcsetup(struct nfs_pgio_data *data,
200
		unsigned int count, unsigned int offset)
A
Andy Adamson 已提交
201
{
202
	struct nfs_page *req = data->header->req;
L
Linus Torvalds 已提交
203

204
	data->args.fh     = NFS_FH(data->header->inode);
L
Linus Torvalds 已提交
205 206
	data->args.offset = req_offset(req) + offset;
	data->args.pgbase = req->wb_pgbase + offset;
F
Fred Isaman 已提交
207
	data->args.pages  = data->pages.pagevec;
L
Linus Torvalds 已提交
208
	data->args.count  = count;
209
	data->args.context = get_nfs_open_context(req->wb_context);
210
	data->args.lock_context = req->wb_lock_context;
L
Linus Torvalds 已提交
211 212 213 214

	data->res.fattr   = &data->fattr;
	data->res.count   = count;
	data->res.eof     = 0;
215
	nfs_fattr_init(&data->fattr);
216
}
L
Linus Torvalds 已提交
217

218
static int nfs_do_read(struct nfs_pgio_data *data,
219
		const struct rpc_call_ops *call_ops)
220
{
221
	struct inode *inode = data->header->inode;
222

223
	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
L
Linus Torvalds 已提交
224 225
}

226 227
static int
nfs_do_multiple_reads(struct list_head *head,
228
		const struct rpc_call_ops *call_ops)
229
{
230
	struct nfs_pgio_data *data;
231 232 233 234 235
	int ret = 0;

	while (!list_empty(head)) {
		int ret2;

236
		data = list_first_entry(head, struct nfs_pgio_data, list);
237 238
		list_del_init(&data->list);

239
		ret2 = nfs_do_read(data, call_ops);
240 241 242 243 244 245
		if (ret == 0)
			ret = ret2;
	}
	return ret;
}

246
static void
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257
nfs_async_read_error(struct list_head *head)
{
	struct nfs_page	*req;

	while (!list_empty(head)) {
		req = nfs_list_entry(head->next);
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
	}
}

258 259 260 261 262
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
	.error_cleanup = nfs_async_read_error,
	.completion = nfs_read_completion,
};

263 264 265 266 267
static void nfs_pagein_error(struct nfs_pageio_descriptor *desc,
		struct nfs_pgio_header *hdr)
{
	set_bit(NFS_IOHDR_REDO, &hdr->flags);
	while (!list_empty(&hdr->rpc_list)) {
268 269
		struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list,
				struct nfs_pgio_data, list);
270
		list_del(&data->list);
271
		nfs_pgio_data_release(data);
272 273 274 275
	}
	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
}

L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288
/*
 * Generate multiple requests to fill a single page.
 *
 * We optimize to reduce the number of read operations on the wire.  If we
 * detect that we're reading a page, or an area of a page, that is past the
 * end of file, we do not generate NFS read operations but just clear the
 * parts of the page that would have come back zero from the server anyway.
 *
 * We rely on the cached value of i_size to make this determination; another
 * client can fill pages on the server past our cached end-of-file, but we
 * won't see the new data until our attribute cache is updated.  This is more
 * or less conventional NFS client behavior.
 */
289 290
static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
			    struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
291
{
292
	struct nfs_page *req = hdr->req;
L
Linus Torvalds 已提交
293
	struct page *page = req->wb_page;
294
	struct nfs_pgio_data *data;
295
	size_t rsize = desc->pg_bsize, nbytes;
296
	unsigned int offset;
L
Linus Torvalds 已提交
297

298
	offset = 0;
F
Fred Isaman 已提交
299
	nbytes = desc->pg_count;
300 301 302
	do {
		size_t len = min(nbytes,rsize);

303
		data = nfs_pgio_data_alloc(hdr, 1);
304 305 306 307
		if (!data) {
			nfs_pagein_error(desc, hdr);
			return -ENOMEM;
		}
F
Fred Isaman 已提交
308
		data->pages.pagevec[0] = page;
309 310
		nfs_read_rpcsetup(data, len, offset);
		list_add(&data->list, &hdr->rpc_list);
311
		nbytes -= len;
312
		offset += len;
T
Trond Myklebust 已提交
313
	} while (nbytes != 0);
314 315 316

	nfs_list_remove_request(req);
	nfs_list_add_request(req, &hdr->pages);
317
	desc->pg_rpc_callops = &nfs_read_common_ops;
T
Trond Myklebust 已提交
318
	return 0;
L
Linus Torvalds 已提交
319 320
}

321 322
static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
			  struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
323 324 325
{
	struct nfs_page		*req;
	struct page		**pages;
326
	struct nfs_pgio_data	*data;
F
Fred Isaman 已提交
327
	struct list_head *head = &desc->pg_list;
L
Linus Torvalds 已提交
328

329
	data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
330 331
							  desc->pg_count));
	if (!data) {
332
		nfs_pagein_error(desc, hdr);
T
Trond Myklebust 已提交
333
		return -ENOMEM;
334
	}
L
Linus Torvalds 已提交
335

F
Fred Isaman 已提交
336
	pages = data->pages.pagevec;
L
Linus Torvalds 已提交
337 338 339
	while (!list_empty(head)) {
		req = nfs_list_entry(head->next);
		nfs_list_remove_request(req);
340
		nfs_list_add_request(req, &hdr->pages);
L
Linus Torvalds 已提交
341 342 343
		*pages++ = req->wb_page;
	}

344 345 346
	nfs_read_rpcsetup(data, desc->pg_count, 0);
	list_add(&data->list, &hdr->rpc_list);
	desc->pg_rpc_callops = &nfs_read_common_ops;
T
Trond Myklebust 已提交
347
	return 0;
L
Linus Torvalds 已提交
348 349
}

350 351
int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
		       struct nfs_pgio_header *hdr)
352 353
{
	if (desc->pg_bsize < PAGE_CACHE_SIZE)
354 355
		return nfs_pagein_multi(desc, hdr);
	return nfs_pagein_one(desc, hdr);
356
}
B
Bryan Schumaker 已提交
357
EXPORT_SYMBOL_GPL(nfs_generic_pagein);
358 359

static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
360
{
361
	struct nfs_rw_header *rhdr;
362
	struct nfs_pgio_header *hdr;
363 364
	int ret;

365
	rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
366
	if (!rhdr) {
367
		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
368 369 370
		return -ENOMEM;
	}
	hdr = &rhdr->header;
371
	nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
372 373
	atomic_inc(&hdr->refcnt);
	ret = nfs_generic_pagein(desc, hdr);
374
	if (ret == 0)
375 376 377
		ret = nfs_do_multiple_reads(&hdr->rpc_list,
					    desc->pg_rpc_callops);
	if (atomic_dec_and_test(&hdr->refcnt))
378
		hdr->completion_ops->completion(hdr);
379
	return ret;
380 381 382 383 384 385 386
}

static const struct nfs_pageio_ops nfs_pageio_read_ops = {
	.pg_test = nfs_generic_pg_test,
	.pg_doio = nfs_generic_pg_readpages,
};

387 388 389 390
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
391 392
static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
			     struct inode *inode)
393
{
394
	int status = NFS_PROTO(inode)->read_done(task, data);
395 396 397
	if (status != 0)
		return status;

398
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
399 400

	if (task->tk_status == -ESTALE) {
401 402
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
403 404 405 406
	}
	return 0;
}

407
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
408
{
409
	struct nfs_pgio_args *argp = &data->args;
410
	struct nfs_pgio_res  *resp = &data->res;
411 412

	/* This is a short read! */
413
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
414
	/* Has the server at least made some progress? */
415 416
	if (resp->count == 0) {
		nfs_set_pgio_error(data->header, -EIO, argp->offset);
417
		return;
418
	}
419
	/* Yes, so retry the read at the end of the data */
420
	data->mds_offset += resp->count;
421 422 423
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
424
	rpc_restart_call_prepare(task);
425 426
}

427
static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
L
Linus Torvalds 已提交
428
{
429 430
	struct nfs_pgio_header *hdr = data->header;

431
	if (data->res.eof) {
432 433 434 435 436 437 438 439 440 441 442 443
		loff_t bound;

		bound = data->args.offset + data->res.count;
		spin_lock(&hdr->lock);
		if (bound < hdr->io_start + hdr->good_bytes) {
			set_bit(NFS_IOHDR_EOF, &hdr->flags);
			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
			hdr->good_bytes = bound - hdr->io_start;
		}
		spin_unlock(&hdr->lock);
	} else if (data->res.count != data->args.count)
		nfs_readpage_retry(task, data);
444 445
}

446
static const struct rpc_call_ops nfs_read_common_ops = {
447
	.rpc_call_prepare = nfs_pgio_prepare,
448
	.rpc_call_done = nfs_pgio_result,
449
	.rpc_release = nfs_pgio_release,
T
Trond Myklebust 已提交
450 451
};

L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460
/*
 * Read a page over NFS.
 * We read the page synchronously in the following case:
 *  -	The error flag is set for this page. This happens only when a
 *	previous async read operation failed.
 */
int nfs_readpage(struct file *file, struct page *page)
{
	struct nfs_open_context *ctx;
461
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
462 463 464
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
465
		page, PAGE_CACHE_SIZE, page_file_index(page));
C
Chuck Lever 已提交
466 467 468
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477
	/*
	 * Try to flush any pending writes to the file..
	 *
	 * NOTE! Because we own the page lock, there cannot
	 * be any new pending writes generated at this point
	 * for this page (other pages can be written to).
	 */
	error = nfs_wb_page(inode, page);
	if (error)
478 479 480
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
481

482 483
	error = -ESTALE;
	if (NFS_STALE(inode))
484
		goto out_unlock;
485

L
Linus Torvalds 已提交
486
	if (file == NULL) {
487
		error = -EBADF;
488
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
489
		if (ctx == NULL)
490
			goto out_unlock;
L
Linus Torvalds 已提交
491
	} else
492
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
493

494 495 496 497 498 499
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

500 501
	error = nfs_readpage_async(ctx, inode, page);

502
out:
L
Linus Torvalds 已提交
503 504
	put_nfs_open_context(ctx);
	return error;
505
out_unlock:
L
Linus Torvalds 已提交
506 507 508 509 510
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
511
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
512 513 514 515 516 517 518
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
519
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
520 521
	struct nfs_page *new;
	unsigned int len;
522 523
	int error;

524
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
525 526
	if (len == 0)
		return nfs_return_empty_page(page);
527

L
Linus Torvalds 已提交
528
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
529 530 531
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
532
	if (len < PAGE_CACHE_SIZE)
533
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
534 535 536 537
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
538
	return 0;
539 540 541 542 543
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
544 545 546 547 548
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
549
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
550
	struct nfs_readdesc desc = {
551
		.pgio = &pgio,
L
Linus Torvalds 已提交
552 553
	};
	struct inode *inode = mapping->host;
554
	unsigned long npages;
555
	int ret = -ESTALE;
L
Linus Torvalds 已提交
556

557
	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
L
Linus Torvalds 已提交
558
			inode->i_sb->s_id,
559
			(unsigned long long)NFS_FILEID(inode),
L
Linus Torvalds 已提交
560
			nr_pages);
C
Chuck Lever 已提交
561
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
562

563 564 565
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
566
	if (filp == NULL) {
567
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
568 569 570
		if (desc.ctx == NULL)
			return -EBADF;
	} else
571
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
572 573 574 575 576 577 578 579 580

	/* attempt to read as many of the pages as possible from the cache
	 * - this returns -ENOBUFS immediately if the cookie is negative
	 */
	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
					 pages, &nr_pages);
	if (ret == 0)
		goto read_complete; /* all pages were read */

581 582
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
583

L
Linus Torvalds 已提交
584
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
585 586

	nfs_pageio_complete(&pgio);
587
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
588 589
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
590
read_complete:
L
Linus Torvalds 已提交
591
	put_nfs_open_context(desc.ctx);
592
out:
L
Linus Torvalds 已提交
593 594 595
	return ret;
}

D
David Howells 已提交
596
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
597 598
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
599
					     sizeof(struct nfs_rw_header),
L
Linus Torvalds 已提交
600
					     0, SLAB_HWCACHE_ALIGN,
601
					     NULL);
L
Linus Torvalds 已提交
602 603 604 605 606 607
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

608
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
609
{
610
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
611
}
612 613

static const struct nfs_rw_ops nfs_rw_read_ops = {
614
	.rw_mode		= FMODE_READ,
615 616
	.rw_alloc_header	= nfs_readhdr_alloc,
	.rw_free_header		= nfs_readhdr_free,
617 618
	.rw_done		= nfs_readpage_done,
	.rw_result		= nfs_readpage_result,
619
};