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 "nfs4_fs.h"
24
#include "internal.h"
C
Chuck Lever 已提交
25
#include "iostat.h"
26
#include "fscache.h"
C
Chuck Lever 已提交
27

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

30
static const struct nfs_pageio_ops nfs_pageio_read_ops;
31
static const struct rpc_call_ops nfs_read_common_ops;
32
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
L
Linus Torvalds 已提交
33

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

T
Trond Myklebust 已提交
36
struct nfs_read_header *nfs_readhdr_alloc(void)
37
{
38
	struct nfs_read_header *rhdr;
39

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

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

53 54
static struct nfs_read_data *nfs_readdata_alloc(struct nfs_pgio_header *hdr,
						unsigned int pagecount)
55 56 57 58 59 60 61 62 63 64 65 66
{
	struct nfs_read_data *data, *prealloc;

	prealloc = &container_of(hdr, struct nfs_read_header, header)->rpc_data;
	if (prealloc->header == NULL)
		data = prealloc;
	else
		data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (!data)
		goto out;

	if (nfs_pgarray_set(&data->pages, pagecount)) {
67
		data->header = hdr;
68 69 70 71 72
		atomic_inc(&hdr->refcnt);
	} else {
		if (data != prealloc)
			kfree(data);
		data = NULL;
73
	}
74 75
out:
	return data;
76 77
}

78
void nfs_readhdr_free(struct nfs_pgio_header *hdr)
79
{
80 81 82
	struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);

	kmem_cache_free(nfs_rdata_cachep, rhdr);
83
}
B
Bryan Schumaker 已提交
84
EXPORT_SYMBOL_GPL(nfs_readhdr_free);
85

86
void nfs_readdata_release(struct nfs_read_data *rdata)
L
Linus Torvalds 已提交
87
{
88 89 90
	struct nfs_pgio_header *hdr = rdata->header;
	struct nfs_read_header *read_header = container_of(hdr, struct nfs_read_header, header);

91
	put_nfs_open_context(rdata->args.context);
F
Fred Isaman 已提交
92 93
	if (rdata->pages.pagevec != rdata->pages.page_array)
		kfree(rdata->pages.pagevec);
94
	if (rdata == &read_header->rpc_data) {
95
		rdata->header = NULL;
96 97
		rdata = NULL;
	}
98
	if (atomic_dec_and_test(&hdr->refcnt))
99
		hdr->completion_ops->completion(hdr);
100 101 102 103
	/* Note: we only free the rpc_task after callbacks are done.
	 * See the comment in rpc_free_task() for why
	 */
	kfree(rdata);
L
Linus Torvalds 已提交
104
}
B
Bryan Schumaker 已提交
105
EXPORT_SYMBOL_GPL(nfs_readdata_release);
L
Linus Torvalds 已提交
106 107 108 109

static
int nfs_return_empty_page(struct page *page)
{
110
	zero_user(page, 0, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
111 112 113 114 115
	SetPageUptodate(page);
	unlock_page(page);
	return 0;
}

116
void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
117 118
			      struct inode *inode,
			      const struct nfs_pgio_completion_ops *compl_ops)
119
{
120
	nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, compl_ops,
121 122
			NFS_SERVER(inode)->rsize, 0);
}
B
Bryan Schumaker 已提交
123
EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
124

125 126 127 128 129
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;
}
130
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
131

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_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
151
	nfs_pageio_add_request(&pgio, new);
152
	nfs_pageio_complete(&pgio);
153
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
L
Linus Torvalds 已提交
154 155 156 157 158
	return 0;
}

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

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

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

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

174
/* Note io was page aligned */
175
static void nfs_read_completion(struct nfs_pgio_header *hdr)
176 177 178 179 180
{
	unsigned long bytes = 0;

	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
		goto out;
181 182 183 184 185 186 187 188 189 190 191
	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);
192
		}
193 194
		bytes += req->wb_bytes;
		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
195
			if (bytes <= hdr->good_bytes)
196 197 198 199 200
				SetPageUptodate(page);
		} else
			SetPageUptodate(page);
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
201 202 203 204 205
	}
out:
	hdr->release(hdr);
}

206 207
int nfs_initiate_read(struct rpc_clnt *clnt,
		      struct nfs_read_data *data,
208
		      const struct rpc_call_ops *call_ops, int flags)
L
Linus Torvalds 已提交
209
{
210
	struct inode *inode = data->header->inode;
211
	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
212
	struct rpc_task *task;
213 214 215
	struct rpc_message msg = {
		.rpc_argp = &data->args,
		.rpc_resp = &data->res,
216
		.rpc_cred = data->header->cred,
217
	};
218
	struct rpc_task_setup task_setup_data = {
219
		.task = &data->task,
A
Andy Adamson 已提交
220
		.rpc_client = clnt,
221
		.rpc_message = &msg,
222 223
		.callback_ops = call_ops,
		.callback_data = data,
224
		.workqueue = nfsiod_workqueue,
225
		.flags = RPC_TASK_ASYNC | swap_flags | flags,
226
	};
L
Linus Torvalds 已提交
227

A
Andy Adamson 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	/* 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 已提交
245
EXPORT_SYMBOL_GPL(nfs_initiate_read);
A
Andy Adamson 已提交
246 247 248 249

/*
 * Set up the NFS read request struct
 */
250
static void nfs_read_rpcsetup(struct nfs_read_data *data,
251
		unsigned int count, unsigned int offset)
A
Andy Adamson 已提交
252
{
253
	struct nfs_page *req = data->header->req;
L
Linus Torvalds 已提交
254

255
	data->args.fh     = NFS_FH(data->header->inode);
L
Linus Torvalds 已提交
256 257
	data->args.offset = req_offset(req) + offset;
	data->args.pgbase = req->wb_pgbase + offset;
F
Fred Isaman 已提交
258
	data->args.pages  = data->pages.pagevec;
L
Linus Torvalds 已提交
259
	data->args.count  = count;
260
	data->args.context = get_nfs_open_context(req->wb_context);
261
	data->args.lock_context = req->wb_lock_context;
L
Linus Torvalds 已提交
262 263 264 265

	data->res.fattr   = &data->fattr;
	data->res.count   = count;
	data->res.eof     = 0;
266
	nfs_fattr_init(&data->fattr);
267
}
L
Linus Torvalds 已提交
268

269
static int nfs_do_read(struct nfs_read_data *data,
270
		const struct rpc_call_ops *call_ops)
271
{
272
	struct inode *inode = data->header->inode;
273

274
	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
L
Linus Torvalds 已提交
275 276
}

277 278
static int
nfs_do_multiple_reads(struct list_head *head,
279
		const struct rpc_call_ops *call_ops)
280 281 282 283 284 285 286
{
	struct nfs_read_data *data;
	int ret = 0;

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

287
		data = list_first_entry(head, struct nfs_read_data, list);
288 289
		list_del_init(&data->list);

290
		ret2 = nfs_do_read(data, call_ops);
291 292 293 294 295 296
		if (ret == 0)
			ret = ret2;
	}
	return ret;
}

297
static void
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306 307 308
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);
	}
}

309 310 311 312 313
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
	.error_cleanup = nfs_async_read_error,
	.completion = nfs_read_completion,
};

314 315 316 317 318 319 320 321 322 323 324 325 326
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)) {
		struct nfs_read_data *data = list_first_entry(&hdr->rpc_list,
				struct nfs_read_data, list);
		list_del(&data->list);
		nfs_readdata_release(data);
	}
	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
}

L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339
/*
 * 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.
 */
340 341
static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
			    struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
342
{
343
	struct nfs_page *req = hdr->req;
L
Linus Torvalds 已提交
344 345
	struct page *page = req->wb_page;
	struct nfs_read_data *data;
346
	size_t rsize = desc->pg_bsize, nbytes;
347
	unsigned int offset;
L
Linus Torvalds 已提交
348

349
	offset = 0;
F
Fred Isaman 已提交
350
	nbytes = desc->pg_count;
351 352 353
	do {
		size_t len = min(nbytes,rsize);

354
		data = nfs_readdata_alloc(hdr, 1);
355 356 357 358
		if (!data) {
			nfs_pagein_error(desc, hdr);
			return -ENOMEM;
		}
F
Fred Isaman 已提交
359
		data->pages.pagevec[0] = page;
360 361
		nfs_read_rpcsetup(data, len, offset);
		list_add(&data->list, &hdr->rpc_list);
362
		nbytes -= len;
363
		offset += len;
T
Trond Myklebust 已提交
364
	} while (nbytes != 0);
365 366 367

	nfs_list_remove_request(req);
	nfs_list_add_request(req, &hdr->pages);
368
	desc->pg_rpc_callops = &nfs_read_common_ops;
T
Trond Myklebust 已提交
369
	return 0;
L
Linus Torvalds 已提交
370 371
}

372 373
static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
			  struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
374 375 376
{
	struct nfs_page		*req;
	struct page		**pages;
377
	struct nfs_read_data    *data;
F
Fred Isaman 已提交
378
	struct list_head *head = &desc->pg_list;
L
Linus Torvalds 已提交
379

380 381 382
	data = nfs_readdata_alloc(hdr, nfs_page_array_len(desc->pg_base,
							  desc->pg_count));
	if (!data) {
383
		nfs_pagein_error(desc, hdr);
T
Trond Myklebust 已提交
384
		return -ENOMEM;
385
	}
L
Linus Torvalds 已提交
386

F
Fred Isaman 已提交
387
	pages = data->pages.pagevec;
L
Linus Torvalds 已提交
388 389 390
	while (!list_empty(head)) {
		req = nfs_list_entry(head->next);
		nfs_list_remove_request(req);
391
		nfs_list_add_request(req, &hdr->pages);
L
Linus Torvalds 已提交
392 393 394
		*pages++ = req->wb_page;
	}

395 396 397
	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 已提交
398
	return 0;
L
Linus Torvalds 已提交
399 400
}

401 402
int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
		       struct nfs_pgio_header *hdr)
403 404
{
	if (desc->pg_bsize < PAGE_CACHE_SIZE)
405 406
		return nfs_pagein_multi(desc, hdr);
	return nfs_pagein_one(desc, hdr);
407
}
B
Bryan Schumaker 已提交
408
EXPORT_SYMBOL_GPL(nfs_generic_pagein);
409 410

static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
411
{
412 413
	struct nfs_read_header *rhdr;
	struct nfs_pgio_header *hdr;
414 415
	int ret;

416 417
	rhdr = nfs_readhdr_alloc();
	if (!rhdr) {
418
		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
419 420 421 422 423 424
		return -ENOMEM;
	}
	hdr = &rhdr->header;
	nfs_pgheader_init(desc, hdr, nfs_readhdr_free);
	atomic_inc(&hdr->refcnt);
	ret = nfs_generic_pagein(desc, hdr);
425
	if (ret == 0)
426 427 428
		ret = nfs_do_multiple_reads(&hdr->rpc_list,
					    desc->pg_rpc_callops);
	if (atomic_dec_and_test(&hdr->refcnt))
429
		hdr->completion_ops->completion(hdr);
430
	return ret;
431 432 433 434 435 436 437
}

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

438 439 440 441 442 443
/*
 * 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)
{
444
	struct inode *inode = data->header->inode;
445 446
	int status;

447
	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
448 449
			task->tk_status);

450
	status = NFS_PROTO(inode)->read_done(task, data);
451 452 453
	if (status != 0)
		return status;

454
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
455 456

	if (task->tk_status == -ESTALE) {
457 458
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
459 460 461 462
	}
	return 0;
}

463
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
464 465 466 467 468
{
	struct nfs_readargs *argp = &data->args;
	struct nfs_readres *resp = &data->res;

	/* This is a short read! */
469
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
470
	/* Has the server at least made some progress? */
471 472
	if (resp->count == 0) {
		nfs_set_pgio_error(data->header, -EIO, argp->offset);
473
		return;
474
	}
475
	/* Yes, so retry the read at the end of the data */
476
	data->mds_offset += resp->count;
477 478 479
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
480
	rpc_restart_call_prepare(task);
481 482
}

483
static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
484
{
T
Trond Myklebust 已提交
485
	struct nfs_read_data *data = calldata;
486 487 488
	struct nfs_pgio_header *hdr = data->header;

	/* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
T
Trond Myklebust 已提交
489 490
	if (nfs_readpage_result(task, data) != 0)
		return;
491
	if (task->tk_status < 0)
492 493 494 495 496 497 498 499 500 501 502 503 504 505
		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);
506 507
}

508
static void nfs_readpage_release_common(void *calldata)
509
{
510
	nfs_readdata_release(calldata);
L
Linus Torvalds 已提交
511 512
}

513 514 515
void nfs_read_prepare(struct rpc_task *task, void *calldata)
{
	struct nfs_read_data *data = calldata;
516
	NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
517 518
	if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
		rpc_exit(task, -EIO);
519 520
}

521
static const struct rpc_call_ops nfs_read_common_ops = {
522
	.rpc_call_prepare = nfs_read_prepare,
523 524
	.rpc_call_done = nfs_readpage_result_common,
	.rpc_release = nfs_readpage_release_common,
T
Trond Myklebust 已提交
525 526
};

L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534 535
/*
 * 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;
536
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
537 538 539
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
540
		page, PAGE_CACHE_SIZE, page_file_index(page));
C
Chuck Lever 已提交
541 542 543
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552
	/*
	 * 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)
553 554 555
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
556

557 558
	error = -ESTALE;
	if (NFS_STALE(inode))
559
		goto out_unlock;
560

L
Linus Torvalds 已提交
561
	if (file == NULL) {
562
		error = -EBADF;
563
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
564
		if (ctx == NULL)
565
			goto out_unlock;
L
Linus Torvalds 已提交
566
	} else
567
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
568

569 570 571 572 573 574
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

575 576
	error = nfs_readpage_async(ctx, inode, page);

577
out:
L
Linus Torvalds 已提交
578 579
	put_nfs_open_context(ctx);
	return error;
580
out_unlock:
L
Linus Torvalds 已提交
581 582 583 584 585
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
586
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
587 588 589 590 591 592 593
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
594
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
595 596
	struct nfs_page *new;
	unsigned int len;
597 598
	int error;

599
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
600 601
	if (len == 0)
		return nfs_return_empty_page(page);
602

L
Linus Torvalds 已提交
603
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
604 605 606
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
607
	if (len < PAGE_CACHE_SIZE)
608
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
609 610 611 612
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
613
	return 0;
614 615 616 617 618
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
619 620 621 622 623
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
624
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
625
	struct nfs_readdesc desc = {
626
		.pgio = &pgio,
L
Linus Torvalds 已提交
627 628
	};
	struct inode *inode = mapping->host;
629
	unsigned long npages;
630
	int ret = -ESTALE;
L
Linus Torvalds 已提交
631 632 633 634 635

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

638 639 640
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
641
	if (filp == NULL) {
642
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
643 644 645
		if (desc.ctx == NULL)
			return -EBADF;
	} else
646
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
647 648 649 650 651 652 653 654 655

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

656
	NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
657

L
Linus Torvalds 已提交
658
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
659 660

	nfs_pageio_complete(&pgio);
661
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
662 663
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
664
read_complete:
L
Linus Torvalds 已提交
665
	put_nfs_open_context(desc.ctx);
666
out:
L
Linus Torvalds 已提交
667 668 669
	return ret;
}

D
David Howells 已提交
670
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
671 672
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
673
					     sizeof(struct nfs_read_header),
L
Linus Torvalds 已提交
674
					     0, SLAB_HWCACHE_ALIGN,
675
					     NULL);
L
Linus Torvalds 已提交
676 677 678 679 680 681
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

682
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
683
{
684
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
685
}