read.c 11.0 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 nfs_pgio_completion_ops nfs_async_read_completion_ops;
33
static const struct nfs_rw_ops nfs_rw_read_ops;
L
Linus Torvalds 已提交
34

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

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

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

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

56
void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
57
			      struct inode *inode, bool force_mds,
58
			      const struct nfs_pgio_completion_ops *compl_ops)
59
{
60 61 62 63 64 65 66
	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
67 68
	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
			server->rsize, 0);
69
}
B
Bryan Schumaker 已提交
70
EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
71

72 73 74 75 76
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;
}
77
EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
78

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

86
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94
	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)
95
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
L
Linus Torvalds 已提交
96

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

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

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

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

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

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

	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
		goto out;
129 130 131 132 133 134 135 136 137 138 139
	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);
140
		}
141 142
		bytes += req->wb_bytes;
		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
143
			if (bytes <= hdr->good_bytes)
144 145 146 147 148
				SetPageUptodate(page);
		} else
			SetPageUptodate(page);
		nfs_list_remove_request(req);
		nfs_readpage_release(req);
149 150 151 152 153
	}
out:
	hdr->release(hdr);
}

154 155
static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
			      struct rpc_task_setup *task_setup_data, int how)
L
Linus Torvalds 已提交
156
{
157
	struct inode *inode = data->header->inode;
158
	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
L
Linus Torvalds 已提交
159

160 161
	task_setup_data->flags |= swap_flags;
	NFS_PROTO(inode)->read_setup(data, msg);
A
Andy Adamson 已提交
162 163
}

164
static void
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175
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);
	}
}

176 177 178 179 180
static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
	.error_cleanup = nfs_async_read_error,
	.completion = nfs_read_completion,
};

181
static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
182
{
183
	struct nfs_rw_header *rhdr;
184
	struct nfs_pgio_header *hdr;
185 186
	int ret;

187
	rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
188
	if (!rhdr) {
189
		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
190 191 192
		return -ENOMEM;
	}
	hdr = &rhdr->header;
193
	nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
194
	atomic_inc(&hdr->refcnt);
195
	ret = nfs_generic_pgio(desc, hdr);
196
	if (ret == 0)
197 198
		ret = nfs_do_multiple_pgios(&hdr->rpc_list,
					    desc->pg_rpc_callops, 0);
199
	if (atomic_dec_and_test(&hdr->refcnt))
200
		hdr->completion_ops->completion(hdr);
201
	return ret;
202 203 204 205 206 207 208
}

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

209 210 211 212
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
213 214
static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
			     struct inode *inode)
215
{
216
	int status = NFS_PROTO(inode)->read_done(task, data);
217 218 219
	if (status != 0)
		return status;

220
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
221 222

	if (task->tk_status == -ESTALE) {
223 224
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
225 226 227 228
	}
	return 0;
}

229
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
230
{
231
	struct nfs_pgio_args *argp = &data->args;
232
	struct nfs_pgio_res  *resp = &data->res;
233 234

	/* This is a short read! */
235
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
236
	/* Has the server at least made some progress? */
237 238
	if (resp->count == 0) {
		nfs_set_pgio_error(data->header, -EIO, argp->offset);
239
		return;
240
	}
241
	/* Yes, so retry the read at the end of the data */
242
	data->mds_offset += resp->count;
243 244 245
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
246
	rpc_restart_call_prepare(task);
247 248
}

249
static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
L
Linus Torvalds 已提交
250
{
251 252
	struct nfs_pgio_header *hdr = data->header;

253
	if (data->res.eof) {
254 255 256 257 258 259 260 261 262 263 264 265
		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);
266 267
}

L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276
/*
 * 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;
277
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
278 279 280
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
281
		page, PAGE_CACHE_SIZE, page_file_index(page));
C
Chuck Lever 已提交
282 283 284
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293
	/*
	 * 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)
294 295 296
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
297

298 299
	error = -ESTALE;
	if (NFS_STALE(inode))
300
		goto out_unlock;
301

L
Linus Torvalds 已提交
302
	if (file == NULL) {
303
		error = -EBADF;
304
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
305
		if (ctx == NULL)
306
			goto out_unlock;
L
Linus Torvalds 已提交
307
	} else
308
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
309

310 311 312 313 314 315
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

316 317
	error = nfs_readpage_async(ctx, inode, page);

318
out:
L
Linus Torvalds 已提交
319 320
	put_nfs_open_context(ctx);
	return error;
321
out_unlock:
L
Linus Torvalds 已提交
322 323 324 325 326
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
327
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
328 329 330 331 332 333 334
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
335
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
336 337
	struct nfs_page *new;
	unsigned int len;
338 339
	int error;

340
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
341 342
	if (len == 0)
		return nfs_return_empty_page(page);
343

L
Linus Torvalds 已提交
344
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
345 346 347
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
348
	if (len < PAGE_CACHE_SIZE)
349
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
350 351 352 353
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
354
	return 0;
355 356 357 358 359
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
360 361 362 363 364
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
365
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
366
	struct nfs_readdesc desc = {
367
		.pgio = &pgio,
L
Linus Torvalds 已提交
368 369
	};
	struct inode *inode = mapping->host;
370
	unsigned long npages;
371
	int ret = -ESTALE;
L
Linus Torvalds 已提交
372

373
	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
L
Linus Torvalds 已提交
374
			inode->i_sb->s_id,
375
			(unsigned long long)NFS_FILEID(inode),
L
Linus Torvalds 已提交
376
			nr_pages);
C
Chuck Lever 已提交
377
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
378

379 380 381
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
382
	if (filp == NULL) {
383
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
384 385 386
		if (desc.ctx == NULL)
			return -EBADF;
	} else
387
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
388 389 390 391 392 393 394 395 396

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

397 398
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
399

L
Linus Torvalds 已提交
400
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
401 402

	nfs_pageio_complete(&pgio);
403
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
404 405
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
406
read_complete:
L
Linus Torvalds 已提交
407
	put_nfs_open_context(desc.ctx);
408
out:
L
Linus Torvalds 已提交
409 410 411
	return ret;
}

D
David Howells 已提交
412
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
413 414
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
415
					     sizeof(struct nfs_rw_header),
L
Linus Torvalds 已提交
416
					     0, SLAB_HWCACHE_ALIGN,
417
					     NULL);
L
Linus Torvalds 已提交
418 419 420 421 422 423
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

424
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
425
{
426
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
427
}
428 429

static const struct nfs_rw_ops nfs_rw_read_ops = {
430
	.rw_mode		= FMODE_READ,
431 432
	.rw_alloc_header	= nfs_readhdr_alloc,
	.rw_free_header		= nfs_readhdr_free,
433 434
	.rw_done		= nfs_readpage_done,
	.rw_result		= nfs_readpage_result,
435
	.rw_initiate		= nfs_initiate_read,
436
};