read.c 10.4 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 182
static const struct nfs_pageio_ops nfs_pageio_read_ops = {
	.pg_test = nfs_generic_pg_test,
183
	.pg_doio = nfs_generic_pg_pgios,
184 185
};

186 187 188 189
/*
 * This is the callback from RPC telling us whether a reply was
 * received or some error occurred (timeout or socket shutdown).
 */
190 191
static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
			     struct inode *inode)
192
{
193
	int status = NFS_PROTO(inode)->read_done(task, data);
194 195 196
	if (status != 0)
		return status;

197
	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
198 199

	if (task->tk_status == -ESTALE) {
200 201
		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
		nfs_mark_for_revalidate(inode);
202 203 204 205
	}
	return 0;
}

206
static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
207
{
208
	struct nfs_pgio_args *argp = &data->args;
209
	struct nfs_pgio_res  *resp = &data->res;
210 211

	/* This is a short read! */
212
	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
213
	/* Has the server at least made some progress? */
214 215
	if (resp->count == 0) {
		nfs_set_pgio_error(data->header, -EIO, argp->offset);
216
		return;
217
	}
218
	/* Yes, so retry the read at the end of the data */
219
	data->mds_offset += resp->count;
220 221 222
	argp->offset += resp->count;
	argp->pgbase += resp->count;
	argp->count -= resp->count;
223
	rpc_restart_call_prepare(task);
224 225
}

226
static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
L
Linus Torvalds 已提交
227
{
228 229
	struct nfs_pgio_header *hdr = data->header;

230
	if (data->res.eof) {
231 232 233 234 235 236 237 238 239 240 241 242
		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);
243 244
}

L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253
/*
 * 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;
254
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
255 256 257
	int		error;

	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
258
		page, PAGE_CACHE_SIZE, page_file_index(page));
C
Chuck Lever 已提交
259 260 261
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
	nfs_add_stats(inode, NFSIOS_READPAGES, 1);

L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270
	/*
	 * 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)
271 272 273
		goto out_unlock;
	if (PageUptodate(page))
		goto out_unlock;
L
Linus Torvalds 已提交
274

275 276
	error = -ESTALE;
	if (NFS_STALE(inode))
277
		goto out_unlock;
278

L
Linus Torvalds 已提交
279
	if (file == NULL) {
280
		error = -EBADF;
281
		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
282
		if (ctx == NULL)
283
			goto out_unlock;
L
Linus Torvalds 已提交
284
	} else
285
		ctx = get_nfs_open_context(nfs_file_open_context(file));
L
Linus Torvalds 已提交
286

287 288 289 290 291 292
	if (!IS_SYNC(inode)) {
		error = nfs_readpage_from_fscache(ctx, inode, page);
		if (error == 0)
			goto out;
	}

293 294
	error = nfs_readpage_async(ctx, inode, page);

295
out:
L
Linus Torvalds 已提交
296 297
	put_nfs_open_context(ctx);
	return error;
298
out_unlock:
L
Linus Torvalds 已提交
299 300 301 302 303
	unlock_page(page);
	return error;
}

struct nfs_readdesc {
304
	struct nfs_pageio_descriptor *pgio;
L
Linus Torvalds 已提交
305 306 307 308 309 310 311
	struct nfs_open_context *ctx;
};

static int
readpage_async_filler(void *data, struct page *page)
{
	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
312
	struct inode *inode = page_file_mapping(page)->host;
L
Linus Torvalds 已提交
313 314
	struct nfs_page *new;
	unsigned int len;
315 316
	int error;

317
	len = nfs_page_length(page);
L
Linus Torvalds 已提交
318 319
	if (len == 0)
		return nfs_return_empty_page(page);
320

L
Linus Torvalds 已提交
321
	new = nfs_create_request(desc->ctx, inode, page, 0, len);
322 323 324
	if (IS_ERR(new))
		goto out_error;

L
Linus Torvalds 已提交
325
	if (len < PAGE_CACHE_SIZE)
326
		zero_user_segment(page, len, PAGE_CACHE_SIZE);
327 328 329 330
	if (!nfs_pageio_add_request(desc->pgio, new)) {
		error = desc->pgio->pg_error;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
331
	return 0;
332 333 334 335 336
out_error:
	error = PTR_ERR(new);
out_unlock:
	unlock_page(page);
	return error;
L
Linus Torvalds 已提交
337 338 339 340 341
}

int nfs_readpages(struct file *filp, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
342
	struct nfs_pageio_descriptor pgio;
L
Linus Torvalds 已提交
343
	struct nfs_readdesc desc = {
344
		.pgio = &pgio,
L
Linus Torvalds 已提交
345 346
	};
	struct inode *inode = mapping->host;
347
	unsigned long npages;
348
	int ret = -ESTALE;
L
Linus Torvalds 已提交
349

350
	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
L
Linus Torvalds 已提交
351
			inode->i_sb->s_id,
352
			(unsigned long long)NFS_FILEID(inode),
L
Linus Torvalds 已提交
353
			nr_pages);
C
Chuck Lever 已提交
354
	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
L
Linus Torvalds 已提交
355

356 357 358
	if (NFS_STALE(inode))
		goto out;

L
Linus Torvalds 已提交
359
	if (filp == NULL) {
360
		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
L
Linus Torvalds 已提交
361 362 363
		if (desc.ctx == NULL)
			return -EBADF;
	} else
364
		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
365 366 367 368 369 370 371 372 373

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

374 375
	nfs_pageio_init_read(&pgio, inode, false,
			     &nfs_async_read_completion_ops);
376

L
Linus Torvalds 已提交
377
	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
378 379

	nfs_pageio_complete(&pgio);
380
	NFS_I(inode)->read_io += pgio.pg_bytes_written;
381 382
	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
383
read_complete:
L
Linus Torvalds 已提交
384
	put_nfs_open_context(desc.ctx);
385
out:
L
Linus Torvalds 已提交
386 387 388
	return ret;
}

D
David Howells 已提交
389
int __init nfs_init_readpagecache(void)
L
Linus Torvalds 已提交
390 391
{
	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
392
					     sizeof(struct nfs_rw_header),
L
Linus Torvalds 已提交
393
					     0, SLAB_HWCACHE_ALIGN,
394
					     NULL);
L
Linus Torvalds 已提交
395 396 397 398 399 400
	if (nfs_rdata_cachep == NULL)
		return -ENOMEM;

	return 0;
}

401
void nfs_destroy_readpagecache(void)
L
Linus Torvalds 已提交
402
{
403
	kmem_cache_destroy(nfs_rdata_cachep);
L
Linus Torvalds 已提交
404
}
405 406

static const struct nfs_rw_ops nfs_rw_read_ops = {
407
	.rw_mode		= FMODE_READ,
408 409
	.rw_alloc_header	= nfs_readhdr_alloc,
	.rw_free_header		= nfs_readhdr_free,
410 411
	.rw_done		= nfs_readpage_done,
	.rw_result		= nfs_readpage_result,
412
	.rw_initiate		= nfs_initiate_read,
413
};