read.c 17.5 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 "pnfs.h"
L
Linus Torvalds 已提交
24

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

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

32
static const struct nfs_pageio_ops nfs_pageio_read_ops;
T
Trond Myklebust 已提交
33 34
static const struct rpc_call_ops nfs_read_partial_ops;
static const struct rpc_call_ops nfs_read_full_ops;
L
Linus Torvalds 已提交
35

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

38
struct nfs_read_header *nfs_readhdr_alloc(unsigned int pagecount)
39
{
40
	struct nfs_read_header *p;
41

42
	p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
43
	if (p) {
44 45 46 47 48 49
		struct nfs_pgio_header *hdr = &p->header;
		struct nfs_read_data *data = &p->rpc_data;

		INIT_LIST_HEAD(&hdr->pages);
		INIT_LIST_HEAD(&data->list);
		data->header = hdr;
F
Fred Isaman 已提交
50 51 52
		if (!nfs_pgarray_set(&data->pages, pagecount)) {
			kmem_cache_free(nfs_rdata_cachep, p);
			p = NULL;
53 54 55 56 57
		}
	}
	return p;
}

58
void nfs_readhdr_free(struct nfs_pgio_header *hdr)
59
{
60 61 62
	struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);

	kmem_cache_free(nfs_rdata_cachep, rhdr);
63 64
}

65
void nfs_readdata_release(struct nfs_read_data *rdata)
L
Linus Torvalds 已提交
66
{
67
	put_nfs_open_context(rdata->args.context);
F
Fred Isaman 已提交
68 69
	if (rdata->pages.pagevec != rdata->pages.page_array)
		kfree(rdata->pages.pagevec);
70
	nfs_readhdr_free(rdata->header);
L
Linus Torvalds 已提交
71 72 73 74 75
}

static
int nfs_return_empty_page(struct page *page)
{
76
	zero_user(page, 0, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
77 78 79 80 81
	SetPageUptodate(page);
	unlock_page(page);
	return 0;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
{
	unsigned int remainder = data->args.count - data->res.count;
	unsigned int base = data->args.pgbase + data->res.count;
	unsigned int pglen;
	struct page **pages;

	if (data->res.eof == 0 || remainder == 0)
		return;
	/*
	 * Note: "remainder" can never be negative, since we check for
	 * 	this in the XDR code.
	 */
	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
	base &= ~PAGE_CACHE_MASK;
	pglen = PAGE_CACHE_SIZE - base;
98 99
	for (;;) {
		if (remainder <= pglen) {
100
			zero_user(*pages, base, remainder);
101 102
			break;
		}
103
		zero_user(*pages, base, pglen);
104 105 106 107 108
		pages++;
		remainder -= pglen;
		pglen = PAGE_CACHE_SIZE;
		base = 0;
	}
109 110
}

111
void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
112 113 114 115 116 117
		struct inode *inode)
{
	nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
			NFS_SERVER(inode)->rsize, 0);
}

118 119 120 121 122
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;
}
123
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
124

125 126 127 128 129 130 131
static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
		struct inode *inode)
{
	if (!pnfs_pageio_init_read(pgio, inode))
		nfs_pageio_init_read_mds(pgio, inode);
}

132 133
int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
		       struct page *page)
L
Linus Torvalds 已提交
134 135 136
{
	struct nfs_page	*new;
	unsigned int len;
F
Fred Isaman 已提交
137
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
138

139
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147
	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)
148
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
149

150
	nfs_pageio_init_read(&pgio, inode);
151
	nfs_pageio_add_request(&pgio, new);
152
	nfs_pageio_complete(&pgio);
L
Linus Torvalds 已提交
153 154 155 156 157
	return 0;
}

static void nfs_readpage_release(struct nfs_page *req)
{
158
	struct inode *d_inode = req->wb_context->dentry->d_inode;
159 160 161 162

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

L
Linus Torvalds 已提交
163 164 165
	unlock_page(req->wb_page);

	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
166 167
			req->wb_context->dentry->d_inode->i_sb->s_id,
			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
L
Linus Torvalds 已提交
168 169
			req->wb_bytes,
			(long long)req_offset(req));
170
	nfs_release_request(req);
L
Linus Torvalds 已提交
171 172
}

173 174
int nfs_initiate_read(struct rpc_clnt *clnt,
		      struct nfs_read_data *data,
A
Andy Adamson 已提交
175
		      const struct rpc_call_ops *call_ops)
L
Linus Torvalds 已提交
176
{
177
	struct inode *inode = data->header->inode;
178
	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
179
	struct rpc_task *task;
180 181 182
	struct rpc_message msg = {
		.rpc_argp = &data->args,
		.rpc_resp = &data->res,
183
		.rpc_cred = data->header->cred,
184
	};
185
	struct rpc_task_setup task_setup_data = {
186
		.task = &data->task,
A
Andy Adamson 已提交
187
		.rpc_client = clnt,
188
		.rpc_message = &msg,
189 190
		.callback_ops = call_ops,
		.callback_data = data,
191
		.workqueue = nfsiod_workqueue,
192 193
		.flags = RPC_TASK_ASYNC | swap_flags,
	};
L
Linus Torvalds 已提交
194

A
Andy Adamson 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	/* Set up the initial task struct. */
	NFS_PROTO(inode)->read_setup(data, &msg);

	dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
			"offset %llu)\n",
			data->task.tk_pid,
			inode->i_sb->s_id,
			(long long)NFS_FILEID(inode),
			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 已提交
212
EXPORT_SYMBOL_GPL(nfs_initiate_read);
A
Andy Adamson 已提交
213 214 215 216

/*
 * Set up the NFS read request struct
 */
217 218
static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
		unsigned int count, unsigned int offset)
A
Andy Adamson 已提交
219
{
220
	struct inode *inode = data->header->inode;
A
Andy Adamson 已提交
221

222 223 224
	data->header->req	  = req;
	data->header->inode	  = inode;
	data->header->cred	  = req->wb_context->cred;
L
Linus Torvalds 已提交
225 226 227 228

	data->args.fh     = NFS_FH(inode);
	data->args.offset = req_offset(req) + offset;
	data->args.pgbase = req->wb_pgbase + offset;
F
Fred Isaman 已提交
229
	data->args.pages  = data->pages.pagevec;
L
Linus Torvalds 已提交
230
	data->args.count  = count;
231
	data->args.context = get_nfs_open_context(req->wb_context);
232
	data->args.lock_context = req->wb_lock_context;
L
Linus Torvalds 已提交
233 234 235 236

	data->res.fattr   = &data->fattr;
	data->res.count   = count;
	data->res.eof     = 0;
237
	nfs_fattr_init(&data->fattr);
238
}
L
Linus Torvalds 已提交
239

240
static int nfs_do_read(struct nfs_read_data *data,
241
		const struct rpc_call_ops *call_ops)
242
{
243
	struct inode *inode = data->header->inode;
244

245
	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops);
L
Linus Torvalds 已提交
246 247
}

248 249
static int
nfs_do_multiple_reads(struct list_head *head,
250
		const struct rpc_call_ops *call_ops)
251 252 253 254 255 256 257 258 259 260
{
	struct nfs_read_data *data;
	int ret = 0;

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

		data = list_entry(head->next, struct nfs_read_data, list);
		list_del_init(&data->list);

261
		ret2 = nfs_do_read(data, call_ops);
262 263 264 265 266 267
		if (ret == 0)
			ret = ret2;
	}
	return ret;
}

L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static void
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);
	}
}

/*
 * 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.
 */
293
static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
L
Linus Torvalds 已提交
294
{
F
Fred Isaman 已提交
295
	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
L
Linus Torvalds 已提交
296
	struct page *page = req->wb_page;
297
	struct nfs_read_header *rhdr;
L
Linus Torvalds 已提交
298
	struct nfs_read_data *data;
299
	size_t rsize = desc->pg_bsize, nbytes;
300
	unsigned int offset;
L
Linus Torvalds 已提交
301
	int requests = 0;
302
	int ret = 0;
L
Linus Torvalds 已提交
303 304 305

	nfs_list_remove_request(req);

306
	offset = 0;
F
Fred Isaman 已提交
307
	nbytes = desc->pg_count;
308 309 310
	do {
		size_t len = min(nbytes,rsize);

311 312
		rhdr = nfs_readhdr_alloc(1);
		if (!rhdr)
L
Linus Torvalds 已提交
313
			goto out_bad;
314
		data = &rhdr->rpc_data;
F
Fred Isaman 已提交
315
		data->pages.pagevec[0] = page;
316 317
		nfs_read_rpcsetup(req, data, len, offset);
		list_add(&data->list, res);
L
Linus Torvalds 已提交
318
		requests++;
319
		nbytes -= len;
320
		offset += len;
321
	} while(nbytes != 0);
L
Linus Torvalds 已提交
322
	atomic_set(&req->wb_complete, requests);
323
	desc->pg_rpc_callops = &nfs_read_partial_ops;
324
	return ret;
L
Linus Torvalds 已提交
325
out_bad:
326 327
	while (!list_empty(res)) {
		data = list_entry(res->next, struct nfs_read_data, list);
328
		list_del(&data->list);
329
		nfs_readdata_release(data);
L
Linus Torvalds 已提交
330 331 332 333 334
	}
	nfs_readpage_release(req);
	return -ENOMEM;
}

335
static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
L
Linus Torvalds 已提交
336 337 338
{
	struct nfs_page		*req;
	struct page		**pages;
339
	struct nfs_read_header	*rhdr;
L
Linus Torvalds 已提交
340
	struct nfs_read_data	*data;
F
Fred Isaman 已提交
341
	struct list_head *head = &desc->pg_list;
342
	int ret = 0;
L
Linus Torvalds 已提交
343

344 345 346
	rhdr = nfs_readhdr_alloc(nfs_page_array_len(desc->pg_base,
						    desc->pg_count));
	if (!rhdr) {
347
		nfs_async_read_error(head);
348
		ret = -ENOMEM;
349 350
		goto out;
	}
L
Linus Torvalds 已提交
351

352
	data = &rhdr->rpc_data;
F
Fred Isaman 已提交
353
	pages = data->pages.pagevec;
L
Linus Torvalds 已提交
354 355 356
	while (!list_empty(head)) {
		req = nfs_list_entry(head->next);
		nfs_list_remove_request(req);
357
		nfs_list_add_request(req, &rhdr->header.pages);
L
Linus Torvalds 已提交
358 359
		*pages++ = req->wb_page;
	}
360
	req = nfs_list_entry(rhdr->header.pages.next);
L
Linus Torvalds 已提交
361

362
	nfs_read_rpcsetup(req, data, desc->pg_count, 0);
363
	list_add(&data->list, res);
364
	desc->pg_rpc_callops = &nfs_read_full_ops;
365
out:
366
	return ret;
L
Linus Torvalds 已提交
367 368
}

369 370 371 372 373 374 375 376
int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
{
	if (desc->pg_bsize < PAGE_CACHE_SIZE)
		return nfs_pagein_multi(desc, head);
	return nfs_pagein_one(desc, head);
}

static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
377
{
378 379 380
	LIST_HEAD(head);
	int ret;

381
	ret = nfs_generic_pagein(desc, &head);
382
	if (ret == 0)
383
		ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
384
	return ret;
385 386 387 388 389 390 391
}

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

392 393 394 395 396 397
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
{
398
	struct inode *inode = data->header->inode;
399 400
	int status;

401
	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
402 403
			task->tk_status);

404
	status = NFS_PROTO(inode)->read_done(task, data);
405 406 407
	if (status != 0)
		return status;

408
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
409 410

	if (task->tk_status == -ESTALE) {
411 412
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
413 414 415 416
	}
	return 0;
}

417
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
418 419 420 421 422
{
	struct nfs_readargs *argp = &data->args;
	struct nfs_readres *resp = &data->res;

	if (resp->eof || resp->count == argp->count)
423
		return;
424 425

	/* This is a short read! */
426
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
427 428
	/* Has the server at least made some progress? */
	if (resp->count == 0)
429
		return;
430 431

	/* Yes, so retry the read at the end of the data */
432
	data->mds_offset += resp->count;
433 434 435
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
436
	rpc_restart_call_prepare(task);
437 438
}

L
Linus Torvalds 已提交
439 440 441
/*
 * Handle a read reply that fills part of a page.
 */
T
Trond Myklebust 已提交
442
static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
443
{
T
Trond Myklebust 已提交
444
	struct nfs_read_data *data = calldata;
L
Linus Torvalds 已提交
445
 
T
Trond Myklebust 已提交
446 447
	if (nfs_readpage_result(task, data) != 0)
		return;
448 449
	if (task->tk_status < 0)
		return;
450

451 452 453 454 455 456 457
	nfs_readpage_truncate_uninitialised_page(data);
	nfs_readpage_retry(task, data);
}

static void nfs_readpage_release_partial(void *calldata)
{
	struct nfs_read_data *data = calldata;
458
	struct nfs_page *req = data->header->req;
459 460 461 462
	struct page *page = req->wb_page;
	int status = data->task.tk_status;

	if (status < 0)
463
		set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
464

L
Linus Torvalds 已提交
465
	if (atomic_dec_and_test(&req->wb_complete)) {
466
		if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
L
Linus Torvalds 已提交
467 468 469
			SetPageUptodate(page);
		nfs_readpage_release(req);
	}
470
	nfs_readdata_release(data);
L
Linus Torvalds 已提交
471 472
}

473 474 475
void nfs_read_prepare(struct rpc_task *task, void *calldata)
{
	struct nfs_read_data *data = calldata;
476
	NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
477 478
}

T
Trond Myklebust 已提交
479
static const struct rpc_call_ops nfs_read_partial_ops = {
480
	.rpc_call_prepare = nfs_read_prepare,
T
Trond Myklebust 已提交
481
	.rpc_call_done = nfs_readpage_result_partial,
482
	.rpc_release = nfs_readpage_release_partial,
T
Trond Myklebust 已提交
483 484
};

485 486 487 488 489 490
static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
{
	unsigned int count = data->res.count;
	unsigned int base = data->args.pgbase;
	struct page **pages;

491 492
	if (data->res.eof)
		count = data->args.count;
493 494 495 496 497 498 499
	if (unlikely(count == 0))
		return;
	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
	base &= ~PAGE_CACHE_MASK;
	count += base;
	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
		SetPageUptodate(*pages);
500 501 502 503
	if (count == 0)
		return;
	/* Was this a short read? */
	if (data->res.eof || data->res.count == data->args.count)
504 505 506
		SetPageUptodate(*pages);
}

L
Linus Torvalds 已提交
507 508 509 510
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
T
Trond Myklebust 已提交
511
static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
512
{
T
Trond Myklebust 已提交
513
	struct nfs_read_data *data = calldata;
L
Linus Torvalds 已提交
514

515 516
	if (nfs_readpage_result(task, data) != 0)
		return;
517 518
	if (task->tk_status < 0)
		return;
519
	/*
520
	 * Note: nfs_readpage_retry may change the values of
521
	 * data->args. In the multi-page case, we therefore need
522 523
	 * to ensure that we call nfs_readpage_set_pages_uptodate()
	 * first.
524
	 */
525 526 527 528 529 530 531 532
	nfs_readpage_truncate_uninitialised_page(data);
	nfs_readpage_set_pages_uptodate(data);
	nfs_readpage_retry(task, data);
}

static void nfs_readpage_release_full(void *calldata)
{
	struct nfs_read_data *data = calldata;
533
	struct nfs_pgio_header *hdr = data->header;
534

535 536
	while (!list_empty(&hdr->pages)) {
		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
L
Linus Torvalds 已提交
537

538
		nfs_list_remove_request(req);
539
		nfs_readpage_release(req);
L
Linus Torvalds 已提交
540
	}
541
	nfs_readdata_release(calldata);
L
Linus Torvalds 已提交
542 543
}

T
Trond Myklebust 已提交
544
static const struct rpc_call_ops nfs_read_full_ops = {
545
	.rpc_call_prepare = nfs_read_prepare,
T
Trond Myklebust 已提交
546
	.rpc_call_done = nfs_readpage_result_full,
547
	.rpc_release = nfs_readpage_release_full,
T
Trond Myklebust 已提交
548 549
};

L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563
/*
 * 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;
	struct inode *inode = page->mapping->host;
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
		page, PAGE_CACHE_SIZE, page->index);
C
Chuck Lever 已提交
564 565 566
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574 575
	/*
	 * 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)
576 577 578
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
579

580 581
	error = -ESTALE;
	if (NFS_STALE(inode))
582
		goto out_unlock;
583

L
Linus Torvalds 已提交
584
	if (file == NULL) {
585
		error = -EBADF;
586
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
587
		if (ctx == NULL)
588
			goto out_unlock;
L
Linus Torvalds 已提交
589
	} else
590
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
591

592 593 594 595 596 597
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

598 599
	error = nfs_readpage_async(ctx, inode, page);

600
out:
L
Linus Torvalds 已提交
601 602
	put_nfs_open_context(ctx);
	return error;
603
out_unlock:
L
Linus Torvalds 已提交
604 605 606 607 608
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
609
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618 619
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
	struct inode *inode = page->mapping->host;
	struct nfs_page *new;
	unsigned int len;
620 621
	int error;

622
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
623 624
	if (len == 0)
		return nfs_return_empty_page(page);
625

L
Linus Torvalds 已提交
626
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
627 628 629
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
630
	if (len < PAGE_CACHE_SIZE)
631
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
632 633 634 635
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
636
	return 0;
637 638 639 640 641
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
642 643 644 645 646
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
647
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
648
	struct nfs_readdesc desc = {
649
		.pgio = &pgio,
L
Linus Torvalds 已提交
650 651
	};
	struct inode *inode = mapping->host;
652
	unsigned long npages;
653
	int ret = -ESTALE;
L
Linus Torvalds 已提交
654 655 656 657 658

	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
			inode->i_sb->s_id,
			(long long)NFS_FILEID(inode),
			nr_pages);
C
Chuck Lever 已提交
659
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
660

661 662 663
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
664
	if (filp == NULL) {
665
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
666 667 668
		if (desc.ctx == NULL)
			return -EBADF;
	} else
669
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
670 671 672 673 674 675 676 677 678

	/* 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 */

679
	nfs_pageio_init_read(&pgio, inode);
680

L
Linus Torvalds 已提交
681
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
682 683 684 685

	nfs_pageio_complete(&pgio);
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
686
read_complete:
L
Linus Torvalds 已提交
687
	put_nfs_open_context(desc.ctx);
688
out:
L
Linus Torvalds 已提交
689 690 691
	return ret;
}

D
David Howells 已提交
692
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
693 694
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
695
					     sizeof(struct nfs_read_header),
L
Linus Torvalds 已提交
696
					     0, SLAB_HWCACHE_ALIGN,
697
					     NULL);
L
Linus Torvalds 已提交
698 699 700 701 702 703
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

704
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
705
{
706
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
707
}