read.c 16.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 "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;
L
Linus Torvalds 已提交
34

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

37
struct nfs_rw_header *nfs_readhdr_alloc(void)
38
{
39
	struct nfs_rw_header *rhdr;
40

41 42 43
	rhdr = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
	if (rhdr) {
		struct nfs_pgio_header *hdr = &rhdr->header;
44 45

		INIT_LIST_HEAD(&hdr->pages);
46 47 48 49 50 51
		INIT_LIST_HEAD(&hdr->rpc_list);
		spin_lock_init(&hdr->lock);
		atomic_set(&hdr->refcnt, 0);
	}
	return rhdr;
}
B
Bryan Schumaker 已提交
52
EXPORT_SYMBOL_GPL(nfs_readhdr_alloc);
53

54
void nfs_readhdr_free(struct nfs_pgio_header *hdr)
55
{
56
	struct nfs_rw_header *rhdr = container_of(hdr, struct nfs_rw_header, header);
57 58

	kmem_cache_free(nfs_rdata_cachep, rhdr);
59
}
B
Bryan Schumaker 已提交
60
EXPORT_SYMBOL_GPL(nfs_readhdr_free);
61

L
Linus Torvalds 已提交
62 63 64
static
int nfs_return_empty_page(struct page *page)
{
65
	zero_user(page, 0, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
66 67 68 69 70
	SetPageUptodate(page);
	unlock_page(page);
	return 0;
}

71
void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
72
			      struct inode *inode, bool force_mds,
73
			      const struct nfs_pgio_completion_ops *compl_ops)
74
{
75 76 77 78 79 80 81 82
	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
	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, server->rsize, 0);
83
}
B
Bryan Schumaker 已提交
84
EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
85

86 87 88 89 90
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;
}
91
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
92

93 94
int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
		       struct page *page)
L
Linus Torvalds 已提交
95 96 97
{
	struct nfs_page	*new;
	unsigned int len;
F
Fred Isaman 已提交
98
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
99

100
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108
	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)
109
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
110

111 112
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
113
	nfs_pageio_add_request(&pgio, new);
114
	nfs_pageio_complete(&pgio);
115
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
L
Linus Torvalds 已提交
116 117 118 119 120
	return 0;
}

static void nfs_readpage_release(struct nfs_page *req)
{
121
	struct inode *d_inode = req->wb_context->dentry->d_inode;
122 123 124 125

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

L
Linus Torvalds 已提交
126 127
	unlock_page(req->wb_page);

128
	dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
129
			req->wb_context->dentry->d_inode->i_sb->s_id,
130
			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
L
Linus Torvalds 已提交
131 132
			req->wb_bytes,
			(long long)req_offset(req));
133
	nfs_release_request(req);
L
Linus Torvalds 已提交
134 135
}

136
/* Note io was page aligned */
137
static void nfs_read_completion(struct nfs_pgio_header *hdr)
138 139 140 141 142
{
	unsigned long bytes = 0;

	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
		goto out;
143 144 145 146 147 148 149 150 151 152 153
	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);
154
		}
155 156
		bytes += req->wb_bytes;
		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
157
			if (bytes <= hdr->good_bytes)
158 159 160 161 162
				SetPageUptodate(page);
		} else
			SetPageUptodate(page);
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
163 164 165 166 167
	}
out:
	hdr->release(hdr);
}

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

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

193
	dprintk("NFS: %5u initiated read call (req %s/%llu, %u bytes @ "
A
Andy Adamson 已提交
194 195 196
			"offset %llu)\n",
			data->task.tk_pid,
			inode->i_sb->s_id,
197
			(unsigned long long)NFS_FILEID(inode),
A
Andy Adamson 已提交
198 199 200 201 202 203 204 205 206
			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 已提交
207
EXPORT_SYMBOL_GPL(nfs_initiate_read);
A
Andy Adamson 已提交
208 209 210 211

/*
 * Set up the NFS read request struct
 */
212
static void nfs_read_rpcsetup(struct nfs_pgio_data *data,
213
		unsigned int count, unsigned int offset)
A
Andy Adamson 已提交
214
{
215
	struct nfs_page *req = data->header->req;
L
Linus Torvalds 已提交
216

217
	data->args.fh     = NFS_FH(data->header->inode);
L
Linus Torvalds 已提交
218 219
	data->args.offset = req_offset(req) + offset;
	data->args.pgbase = req->wb_pgbase + offset;
F
Fred Isaman 已提交
220
	data->args.pages  = data->pages.pagevec;
L
Linus Torvalds 已提交
221
	data->args.count  = count;
222
	data->args.context = get_nfs_open_context(req->wb_context);
223
	data->args.lock_context = req->wb_lock_context;
L
Linus Torvalds 已提交
224 225 226 227

	data->res.fattr   = &data->fattr;
	data->res.count   = count;
	data->res.eof     = 0;
228
	nfs_fattr_init(&data->fattr);
229
}
L
Linus Torvalds 已提交
230

231
static int nfs_do_read(struct nfs_pgio_data *data,
232
		const struct rpc_call_ops *call_ops)
233
{
234
	struct inode *inode = data->header->inode;
235

236
	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
L
Linus Torvalds 已提交
237 238
}

239 240
static int
nfs_do_multiple_reads(struct list_head *head,
241
		const struct rpc_call_ops *call_ops)
242
{
243
	struct nfs_pgio_data *data;
244 245 246 247 248
	int ret = 0;

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

249
		data = list_first_entry(head, struct nfs_pgio_data, list);
250 251
		list_del_init(&data->list);

252
		ret2 = nfs_do_read(data, call_ops);
253 254 255 256 257 258
		if (ret == 0)
			ret = ret2;
	}
	return ret;
}

259
static void
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270
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);
	}
}

271 272 273 274 275
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
	.error_cleanup = nfs_async_read_error,
	.completion = nfs_read_completion,
};

276 277 278 279 280
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)) {
281 282
		struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list,
				struct nfs_pgio_data, list);
283
		list_del(&data->list);
284
		nfs_pgio_data_release(data);
285 286 287 288
	}
	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
}

L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301
/*
 * 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.
 */
302 303
static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
			    struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
304
{
305
	struct nfs_page *req = hdr->req;
L
Linus Torvalds 已提交
306
	struct page *page = req->wb_page;
307
	struct nfs_pgio_data *data;
308
	size_t rsize = desc->pg_bsize, nbytes;
309
	unsigned int offset;
L
Linus Torvalds 已提交
310

311
	offset = 0;
F
Fred Isaman 已提交
312
	nbytes = desc->pg_count;
313 314 315
	do {
		size_t len = min(nbytes,rsize);

316
		data = nfs_pgio_data_alloc(hdr, 1);
317 318 319 320
		if (!data) {
			nfs_pagein_error(desc, hdr);
			return -ENOMEM;
		}
F
Fred Isaman 已提交
321
		data->pages.pagevec[0] = page;
322 323
		nfs_read_rpcsetup(data, len, offset);
		list_add(&data->list, &hdr->rpc_list);
324
		nbytes -= len;
325
		offset += len;
T
Trond Myklebust 已提交
326
	} while (nbytes != 0);
327 328 329

	nfs_list_remove_request(req);
	nfs_list_add_request(req, &hdr->pages);
330
	desc->pg_rpc_callops = &nfs_read_common_ops;
T
Trond Myklebust 已提交
331
	return 0;
L
Linus Torvalds 已提交
332 333
}

334 335
static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
			  struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
336 337 338
{
	struct nfs_page		*req;
	struct page		**pages;
339
	struct nfs_pgio_data	*data;
F
Fred Isaman 已提交
340
	struct list_head *head = &desc->pg_list;
L
Linus Torvalds 已提交
341

342
	data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
343 344
							  desc->pg_count));
	if (!data) {
345
		nfs_pagein_error(desc, hdr);
T
Trond Myklebust 已提交
346
		return -ENOMEM;
347
	}
L
Linus Torvalds 已提交
348

F
Fred Isaman 已提交
349
	pages = data->pages.pagevec;
L
Linus Torvalds 已提交
350 351 352
	while (!list_empty(head)) {
		req = nfs_list_entry(head->next);
		nfs_list_remove_request(req);
353
		nfs_list_add_request(req, &hdr->pages);
L
Linus Torvalds 已提交
354 355 356
		*pages++ = req->wb_page;
	}

357 358 359
	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 已提交
360
	return 0;
L
Linus Torvalds 已提交
361 362
}

363 364
int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
		       struct nfs_pgio_header *hdr)
365 366
{
	if (desc->pg_bsize < PAGE_CACHE_SIZE)
367 368
		return nfs_pagein_multi(desc, hdr);
	return nfs_pagein_one(desc, hdr);
369
}
B
Bryan Schumaker 已提交
370
EXPORT_SYMBOL_GPL(nfs_generic_pagein);
371 372

static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
373
{
374
	struct nfs_rw_header *rhdr;
375
	struct nfs_pgio_header *hdr;
376 377
	int ret;

378 379
	rhdr = nfs_readhdr_alloc();
	if (!rhdr) {
380
		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
381 382 383 384 385 386
		return -ENOMEM;
	}
	hdr = &rhdr->header;
	nfs_pgheader_init(desc, hdr, nfs_readhdr_free);
	atomic_inc(&hdr->refcnt);
	ret = nfs_generic_pagein(desc, hdr);
387
	if (ret == 0)
388 389 390
		ret = nfs_do_multiple_reads(&hdr->rpc_list,
					    desc->pg_rpc_callops);
	if (atomic_dec_and_test(&hdr->refcnt))
391
		hdr->completion_ops->completion(hdr);
392
	return ret;
393 394 395 396 397 398 399
}

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

400 401 402 403
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
404
int nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
405
{
406
	struct inode *inode = data->header->inode;
407 408
	int status;

409
	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
410 411
			task->tk_status);

412
	status = NFS_PROTO(inode)->read_done(task, data);
413 414 415
	if (status != 0)
		return status;

416
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
417 418

	if (task->tk_status == -ESTALE) {
419 420
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
421 422 423 424
	}
	return 0;
}

425
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
426
{
427
	struct nfs_pgio_args *argp = &data->args;
428
	struct nfs_pgio_res  *resp = &data->res;
429 430

	/* This is a short read! */
431
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
432
	/* Has the server at least made some progress? */
433 434
	if (resp->count == 0) {
		nfs_set_pgio_error(data->header, -EIO, argp->offset);
435
		return;
436
	}
437
	/* Yes, so retry the read at the end of the data */
438
	data->mds_offset += resp->count;
439 440 441
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
442
	rpc_restart_call_prepare(task);
443 444
}

445
static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
446
{
447
	struct nfs_pgio_data *data = calldata;
448 449 450
	struct nfs_pgio_header *hdr = data->header;

	/* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
T
Trond Myklebust 已提交
451 452
	if (nfs_readpage_result(task, data) != 0)
		return;
453
	if (task->tk_status < 0)
454 455 456 457 458 459 460 461 462 463 464 465 466 467
		nfs_set_pgio_error(hdr, task->tk_status, data->args.offset);
	else if (data->res.eof) {
		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);
468 469
}

470
static void nfs_readpage_release_common(void *calldata)
471
{
472
	nfs_pgio_data_release(calldata);
L
Linus Torvalds 已提交
473 474
}

475 476
void nfs_read_prepare(struct rpc_task *task, void *calldata)
{
477
	struct nfs_pgio_data *data = calldata;
478 479 480 481
	int err;
	err = NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
	if (err)
		rpc_exit(task, err);
482 483
}

484
static const struct rpc_call_ops nfs_read_common_ops = {
485
	.rpc_call_prepare = nfs_read_prepare,
486 487
	.rpc_call_done = nfs_readpage_result_common,
	.rpc_release = nfs_readpage_release_common,
T
Trond Myklebust 已提交
488 489
};

L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498
/*
 * 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;
499
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
500 501 502
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
503
		page, PAGE_CACHE_SIZE, page_file_index(page));
C
Chuck Lever 已提交
504 505 506
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515
	/*
	 * 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)
516 517 518
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
519

520 521
	error = -ESTALE;
	if (NFS_STALE(inode))
522
		goto out_unlock;
523

L
Linus Torvalds 已提交
524
	if (file == NULL) {
525
		error = -EBADF;
526
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
527
		if (ctx == NULL)
528
			goto out_unlock;
L
Linus Torvalds 已提交
529
	} else
530
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
531

532 533 534 535 536 537
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

538 539
	error = nfs_readpage_async(ctx, inode, page);

540
out:
L
Linus Torvalds 已提交
541 542
	put_nfs_open_context(ctx);
	return error;
543
out_unlock:
L
Linus Torvalds 已提交
544 545 546 547 548
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
549
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
550 551 552 553 554 555 556
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
557
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
558 559
	struct nfs_page *new;
	unsigned int len;
560 561
	int error;

562
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
563 564
	if (len == 0)
		return nfs_return_empty_page(page);
565

L
Linus Torvalds 已提交
566
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
567 568 569
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
570
	if (len < PAGE_CACHE_SIZE)
571
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
572 573 574 575
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
576
	return 0;
577 578 579 580 581
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
582 583 584 585 586
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
587
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
588
	struct nfs_readdesc desc = {
589
		.pgio = &pgio,
L
Linus Torvalds 已提交
590 591
	};
	struct inode *inode = mapping->host;
592
	unsigned long npages;
593
	int ret = -ESTALE;
L
Linus Torvalds 已提交
594

595
	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
L
Linus Torvalds 已提交
596
			inode->i_sb->s_id,
597
			(unsigned long long)NFS_FILEID(inode),
L
Linus Torvalds 已提交
598
			nr_pages);
C
Chuck Lever 已提交
599
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
600

601 602 603
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
604
	if (filp == NULL) {
605
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
606 607 608
		if (desc.ctx == NULL)
			return -EBADF;
	} else
609
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
610 611 612 613 614 615 616 617 618

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

619 620
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
621

L
Linus Torvalds 已提交
622
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
623 624

	nfs_pageio_complete(&pgio);
625
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
626 627
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
628
read_complete:
L
Linus Torvalds 已提交
629
	put_nfs_open_context(desc.ctx);
630
out:
L
Linus Torvalds 已提交
631 632 633
	return ret;
}

D
David Howells 已提交
634
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
635 636
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
637
					     sizeof(struct nfs_rw_header),
L
Linus Torvalds 已提交
638
					     0, SLAB_HWCACHE_ALIGN,
639
					     NULL);
L
Linus Torvalds 已提交
640 641 642 643 644 645
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

646
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
647
{
648
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
649
}