read.c 11.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"
25
#include "iostat.h"
26
#include "fscache.h"
27
#include "pnfs.h"
28

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

31
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
32
static const struct nfs_rw_ops nfs_rw_read_ops;
L
Linus Torvalds 已提交
33

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

36
static struct nfs_pgio_header *nfs_readhdr_alloc(void)
37
{
38 39 40 41 42
	struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);

	if (p)
		p->rw_mode = FMODE_READ;
	return p;
43 44
}

45
static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
46
{
47
	kmem_cache_free(nfs_rdata_cachep, rhdr);
48 49
}

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

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

#ifdef CONFIG_NFS_V4_1
	if (server->pnfs_curr_ld && !force_mds)
		pg_ops = server->pnfs_curr_ld->pg_read_ops;
#endif
70
	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
71
			server->rsize, 0);
72
}
73
EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
74

75 76
void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
{
77 78
	struct nfs_pgio_mirror *mirror;

79 80 81
	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
		pgio->pg_ops->pg_cleanup(pgio);

82
	pgio->pg_ops = &nfs_pgio_rw_ops;
83 84 85 86 87 88

	/* read path should never have more than one mirror */
	WARN_ON_ONCE(pgio->pg_mirror_count != 1);

	mirror = &pgio->pg_mirrors[0];
	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
89
}
90
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static void nfs_readpage_release(struct nfs_page *req)
{
	struct inode *inode = d_inode(req->wb_context->dentry);

	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
		(long long)req_offset(req));

	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
		if (PageUptodate(req->wb_page))
			nfs_readpage_to_fscache(inode, req->wb_page, 0);

		unlock_page(req->wb_page);
	}
	nfs_release_request(req);
}

109 110
int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
		       struct page *page)
L
Linus Torvalds 已提交
111 112 113
{
	struct nfs_page	*new;
	unsigned int len;
114
	struct nfs_pageio_descriptor pgio;
115
	struct nfs_pgio_mirror *pgm;
L
Linus Torvalds 已提交
116

117
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
118 119
	if (len == 0)
		return nfs_return_empty_page(page);
120
	new = nfs_create_request(ctx, page, NULL, 0, len);
L
Linus Torvalds 已提交
121 122 123 124
	if (IS_ERR(new)) {
		unlock_page(page);
		return PTR_ERR(new);
	}
125 126
	if (len < PAGE_SIZE)
		zero_user_segment(page, len, PAGE_SIZE);
L
Linus Torvalds 已提交
127

128 129
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
130 131 132 133
	if (!nfs_pageio_add_request(&pgio, new)) {
		nfs_list_remove_request(new);
		nfs_readpage_release(new);
	}
134
	nfs_pageio_complete(&pgio);
135 136 137 138 139 140 141

	/* It doesn't make sense to do mirrored reads! */
	WARN_ON_ONCE(pgio.pg_mirror_count != 1);

	pgm = &pgio.pg_mirrors[0];
	NFS_I(inode)->read_io += pgm->pg_bytes_written;

142
	return pgio.pg_error < 0 ? pgio.pg_error : 0;
L
Linus Torvalds 已提交
143 144
}

145 146 147 148 149 150
static void nfs_page_group_set_uptodate(struct nfs_page *req)
{
	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
		SetPageUptodate(req->wb_page);
}

151
static void nfs_read_completion(struct nfs_pgio_header *hdr)
152 153 154 155 156
{
	unsigned long bytes = 0;

	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
		goto out;
157 158 159
	while (!list_empty(&hdr->pages)) {
		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
		struct page *page = req->wb_page;
160 161
		unsigned long start = req->wb_pgbase;
		unsigned long end = req->wb_pgbase + req->wb_bytes;
162 163

		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
			/* note: regions of the page not covered by a
			 * request are zeroed in nfs_readpage_async /
			 * readpage_async_filler */
			if (bytes > hdr->good_bytes) {
				/* nothing in this request was good, so zero
				 * the full extent of the request */
				zero_user_segment(page, start, end);

			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
				/* part of this request has good bytes, but
				 * not all. zero the bad bytes */
				start += hdr->good_bytes - bytes;
				WARN_ON(start < req->wb_pgbase);
				zero_user_segment(page, start, end);
			}
179
		}
180 181
		bytes += req->wb_bytes;
		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
182
			if (bytes <= hdr->good_bytes)
183
				nfs_page_group_set_uptodate(req);
184
		} else
185
			nfs_page_group_set_uptodate(req);
186 187
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
188 189 190 191 192
	}
out:
	hdr->release(hdr);
}

193 194
static void nfs_initiate_read(struct nfs_pgio_header *hdr,
			      struct rpc_message *msg,
195
			      const struct nfs_rpc_ops *rpc_ops,
196
			      struct rpc_task_setup *task_setup_data, int how)
L
Linus Torvalds 已提交
197
{
198
	struct inode *inode = hdr->inode;
199
	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
L
Linus Torvalds 已提交
200

201
	task_setup_data->flags |= swap_flags;
202
	rpc_ops->read_setup(hdr, msg);
A
Andy Adamson 已提交
203 204
}

205
static void
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216
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);
	}
}

217 218 219 220 221
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
	.error_cleanup = nfs_async_read_error,
	.completion = nfs_read_completion,
};

222 223 224 225
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
226 227
static int nfs_readpage_done(struct rpc_task *task,
			     struct nfs_pgio_header *hdr,
228
			     struct inode *inode)
229
{
230
	int status = NFS_PROTO(inode)->read_done(task, hdr);
231 232 233
	if (status != 0)
		return status;

234
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
235 236

	if (task->tk_status == -ESTALE) {
237 238
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
239 240 241 242
	}
	return 0;
}

243 244
static void nfs_readpage_retry(struct rpc_task *task,
			       struct nfs_pgio_header *hdr)
245
{
246 247
	struct nfs_pgio_args *argp = &hdr->args;
	struct nfs_pgio_res  *resp = &hdr->res;
248 249

	/* This is a short read! */
250
	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
251
	/* Has the server at least made some progress? */
252
	if (resp->count == 0) {
253
		nfs_set_pgio_error(hdr, -EIO, argp->offset);
254
		return;
255
	}
256 257 258 259 260 261 262

	/* For non rpc-based layout drivers, retry-through-MDS */
	if (!task->tk_ops) {
		hdr->pnfs_error = -EAGAIN;
		return;
	}

263 264
	/* Yes, so retry the read at the end of the hdr */
	hdr->mds_offset += resp->count;
265 266 267
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
268
	rpc_restart_call_prepare(task);
269 270
}

271 272
static void nfs_readpage_result(struct rpc_task *task,
				struct nfs_pgio_header *hdr)
L
Linus Torvalds 已提交
273
{
274
	if (hdr->res.eof) {
275 276
		loff_t bound;

277
		bound = hdr->args.offset + hdr->res.count;
278 279 280 281 282 283 284
		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);
285
	} else if (hdr->res.count < hdr->args.count)
286
		nfs_readpage_retry(task, hdr);
287 288
}

L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297
/*
 * 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;
298
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
299 300 301
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
H
Huang Ying 已提交
302
		page, PAGE_SIZE, page_index(page));
303
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
304
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
305

L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314
	/*
	 * 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)
315 316 317
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
318

319 320
	error = -ESTALE;
	if (NFS_STALE(inode))
321
		goto out_unlock;
322

L
Linus Torvalds 已提交
323
	if (file == NULL) {
324
		error = -EBADF;
325
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
326
		if (ctx == NULL)
327
			goto out_unlock;
L
Linus Torvalds 已提交
328
	} else
329
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
330

331 332 333 334 335 336
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

337 338
	error = nfs_readpage_async(ctx, inode, page);

339
out:
L
Linus Torvalds 已提交
340 341
	put_nfs_open_context(ctx);
	return error;
342
out_unlock:
L
Linus Torvalds 已提交
343 344 345 346 347
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
348
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
	struct nfs_page *new;
	unsigned int len;
358 359
	int error;

360
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
361 362
	if (len == 0)
		return nfs_return_empty_page(page);
363

364
	new = nfs_create_request(desc->ctx, page, NULL, 0, len);
365 366 367
	if (IS_ERR(new))
		goto out_error;

368 369
	if (len < PAGE_SIZE)
		zero_user_segment(page, len, PAGE_SIZE);
370
	if (!nfs_pageio_add_request(desc->pgio, new)) {
371 372
		nfs_list_remove_request(new);
		nfs_readpage_release(new);
373
		error = desc->pgio->pg_error;
374
		goto out;
375
	}
L
Linus Torvalds 已提交
376
	return 0;
377 378 379
out_error:
	error = PTR_ERR(new);
	unlock_page(page);
380
out:
381
	return error;
L
Linus Torvalds 已提交
382 383 384 385 386
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
387
	struct nfs_pageio_descriptor pgio;
388
	struct nfs_pgio_mirror *pgm;
L
Linus Torvalds 已提交
389
	struct nfs_readdesc desc = {
390
		.pgio = &pgio,
L
Linus Torvalds 已提交
391 392
	};
	struct inode *inode = mapping->host;
393
	unsigned long npages;
394
	int ret = -ESTALE;
L
Linus Torvalds 已提交
395

396
	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
L
Linus Torvalds 已提交
397
			inode->i_sb->s_id,
398
			(unsigned long long)NFS_FILEID(inode),
L
Linus Torvalds 已提交
399
			nr_pages);
400
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
401

402 403 404
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
405
	if (filp == NULL) {
406
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
407 408 409
		if (desc.ctx == NULL)
			return -EBADF;
	} else
410
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
411 412 413 414 415 416 417 418 419

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

420 421
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
422

L
Linus Torvalds 已提交
423
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
424
	nfs_pageio_complete(&pgio);
425 426 427 428 429 430

	/* It doesn't make sense to do mirrored reads! */
	WARN_ON_ONCE(pgio.pg_mirror_count != 1);

	pgm = &pgio.pg_mirrors[0];
	NFS_I(inode)->read_io += pgm->pg_bytes_written;
431 432
	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >>
		 PAGE_SHIFT;
433
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
434
read_complete:
L
Linus Torvalds 已提交
435
	put_nfs_open_context(desc.ctx);
436
out:
L
Linus Torvalds 已提交
437 438 439
	return ret;
}

D
David Howells 已提交
440
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
441 442
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
443
					     sizeof(struct nfs_pgio_header),
L
Linus Torvalds 已提交
444
					     0, SLAB_HWCACHE_ALIGN,
445
					     NULL);
L
Linus Torvalds 已提交
446 447 448 449 450 451
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

452
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
453
{
454
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
455
}
456 457 458 459

static const struct nfs_rw_ops nfs_rw_read_ops = {
	.rw_alloc_header	= nfs_readhdr_alloc,
	.rw_free_header		= nfs_readhdr_free,
460 461
	.rw_done		= nfs_readpage_done,
	.rw_result		= nfs_readpage_result,
462
	.rw_initiate		= nfs_initiate_read,
463
};
新手
引导
客服 返回
顶部