nfs4filelayout.c 37.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  Module for the pnfs nfs4 file layout driver.
 *  Defines all I/O and Policy interface operations, plus code
 *  to register itself with the pNFS client.
 *
 *  Copyright (c) 2002
 *  The Regents of the University of Michigan
 *  All Rights Reserved
 *
 *  Dean Hildebrand <dhildebz@umich.edu>
 *
 *  Permission is granted to use, copy, create derivative works, and
 *  redistribute this software and such derivative works for any purpose,
 *  so long as the name of the University of Michigan is not used in
 *  any advertising or publicity pertaining to the use or distribution
 *  of this software without specific, written prior authorization. If
 *  the above copyright notice or any other identification of the
 *  University of Michigan is included in any copy of any portion of
 *  this software, then the disclaimer below must also be included.
 *
 *  This software is provided as is, without representation or warranty
 *  of any kind either express or implied, including without limitation
 *  the implied warranties of merchantability, fitness for a particular
 *  purpose, or noninfringement.  The Regents of the University of
 *  Michigan shall not be liable for any damages, including special,
 *  indirect, incidental, or consequential damages, with respect to any
 *  claim arising out of or in connection with the use of the software,
 *  even if it has been or is hereafter advised of the possibility of
 *  such damages.
 */

#include <linux/nfs_fs.h>
33
#include <linux/nfs_page.h>
34
#include <linux/module.h>
35

36 37
#include <linux/sunrpc/metrics.h>

38
#include "nfs4session.h"
39
#include "internal.h"
A
Andy Adamson 已提交
40
#include "delegation.h"
41
#include "nfs4filelayout.h"
42
#include "nfs4trace.h"
43 44 45 46 47 48 49

#define NFSDBG_FACILITY         NFSDBG_PNFS_LD

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
MODULE_DESCRIPTION("The NFSv4 file layout driver");

50 51
#define FILELAYOUT_POLL_RETRY_MAX     (15*HZ)

F
Fred Isaman 已提交
52 53 54 55 56
static loff_t
filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
			    loff_t offset)
{
	u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
57 58
	u64 stripe_no;
	u32 rem;
F
Fred Isaman 已提交
59 60

	offset -= flseg->pattern_offset;
61 62
	stripe_no = div_u64(offset, stripe_width);
	div_u64_rem(offset, flseg->stripe_unit, &rem);
F
Fred Isaman 已提交
63

64
	return stripe_no * flseg->stripe_unit + rem;
F
Fred Isaman 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

/* This function is used by the layout driver to calculate the
 * offset of the file on the dserver based on whether the
 * layout type is STRIPE_DENSE or STRIPE_SPARSE
 */
static loff_t
filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
{
	struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);

	switch (flseg->stripe_type) {
	case STRIPE_SPARSE:
		return offset;

	case STRIPE_DENSE:
		return filelayout_get_dense_offset(flseg, offset);
	}

	BUG();
}

87
static void filelayout_reset_write(struct nfs_pgio_data *data)
88 89 90 91 92 93
{
	struct nfs_pgio_header *hdr = data->header;
	struct rpc_task *task = &data->task;

	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
		dprintk("%s Reset task %5u for i/o through MDS "
94
			"(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
95
			data->task.tk_pid,
B
Bryan Schumaker 已提交
96
			hdr->inode->i_sb->s_id,
97
			(unsigned long long)NFS_FILEID(hdr->inode),
98 99 100 101 102
			data->args.count,
			(unsigned long long)data->args.offset);

		task->tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
							&hdr->pages,
103 104
							hdr->completion_ops,
							hdr->dreq);
105 106 107
	}
}

108
static void filelayout_reset_read(struct nfs_pgio_data *data)
109 110 111 112 113 114
{
	struct nfs_pgio_header *hdr = data->header;
	struct rpc_task *task = &data->task;

	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
		dprintk("%s Reset task %5u for i/o through MDS "
115
			"(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
116
			data->task.tk_pid,
B
Bryan Schumaker 已提交
117
			hdr->inode->i_sb->s_id,
118
			(unsigned long long)NFS_FILEID(hdr->inode),
119 120 121 122 123
			data->args.count,
			(unsigned long long)data->args.offset);

		task->tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
							&hdr->pages,
124 125
							hdr->completion_ops,
							hdr->dreq);
126 127 128
	}
}

129 130 131 132 133 134 135
static void filelayout_fenceme(struct inode *inode, struct pnfs_layout_hdr *lo)
{
	if (!test_and_clear_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
		return;
	pnfs_return_layout(inode);
}

136 137 138
static int filelayout_async_handle_error(struct rpc_task *task,
					 struct nfs4_state *state,
					 struct nfs_client *clp,
139
					 struct pnfs_layout_segment *lseg)
140
{
141 142
	struct pnfs_layout_hdr *lo = lseg->pls_layout;
	struct inode *inode = lo->plh_inode;
143 144
	struct nfs_server *mds_server = NFS_SERVER(inode);
	struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
A
Andy Adamson 已提交
145
	struct nfs_client *mds_client = mds_server->nfs_client;
146
	struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
A
Andy Adamson 已提交
147

148 149 150 151
	if (task->tk_status >= 0)
		return 0;

	switch (task->tk_status) {
A
Andy Adamson 已提交
152 153 154 155
	/* MDS state errors */
	case -NFS4ERR_DELEG_REVOKED:
	case -NFS4ERR_ADMIN_REVOKED:
	case -NFS4ERR_BAD_STATEID:
156 157
		if (state == NULL)
			break;
158
		nfs_remove_bad_delegation(state->inode);
A
Andy Adamson 已提交
159
	case -NFS4ERR_OPENMODE:
160 161
		if (state == NULL)
			break;
162 163
		if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
			goto out_bad_stateid;
A
Andy Adamson 已提交
164 165
		goto wait_on_recovery;
	case -NFS4ERR_EXPIRED:
166 167 168 169
		if (state != NULL) {
			if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
				goto out_bad_stateid;
		}
A
Andy Adamson 已提交
170 171 172
		nfs4_schedule_lease_recovery(mds_client);
		goto wait_on_recovery;
	/* DS session errors */
173 174 175 176 177 178 179 180 181 182
	case -NFS4ERR_BADSESSION:
	case -NFS4ERR_BADSLOT:
	case -NFS4ERR_BAD_HIGH_SLOT:
	case -NFS4ERR_DEADSESSION:
	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
	case -NFS4ERR_SEQ_FALSE_RETRY:
	case -NFS4ERR_SEQ_MISORDERED:
		dprintk("%s ERROR %d, Reset session. Exchangeid "
			"flags 0x%x\n", __func__, task->tk_status,
			clp->cl_exchange_flags);
183
		nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
184 185 186 187 188
		break;
	case -NFS4ERR_DELAY:
	case -NFS4ERR_GRACE:
		rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
		break;
189 190
	case -NFS4ERR_RETRY_UNCACHED_REP:
		break;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	/* Invalidate Layout errors */
	case -NFS4ERR_PNFS_NO_LAYOUT:
	case -ESTALE:           /* mapped NFS4ERR_STALE */
	case -EBADHANDLE:       /* mapped NFS4ERR_BADHANDLE */
	case -EISDIR:           /* mapped NFS4ERR_ISDIR */
	case -NFS4ERR_FHEXPIRED:
	case -NFS4ERR_WRONG_TYPE:
		dprintk("%s Invalid layout error %d\n", __func__,
			task->tk_status);
		/*
		 * Destroy layout so new i/o will get a new layout.
		 * Layout will not be destroyed until all current lseg
		 * references are put. Mark layout as invalid to resend failed
		 * i/o and all i/o waiting on the slot table to the MDS until
		 * layout is destroyed and a new valid layout is obtained.
		 */
207
		pnfs_destroy_layout(NFS_I(inode));
208 209
		rpc_wake_up(&tbl->slot_tbl_waitq);
		goto reset;
210 211 212 213 214 215 216 217 218 219
	/* RPC connection errors */
	case -ECONNREFUSED:
	case -EHOSTDOWN:
	case -EHOSTUNREACH:
	case -ENETUNREACH:
	case -EIO:
	case -ETIMEDOUT:
	case -EPIPE:
		dprintk("%s DS connection error %d\n", __func__,
			task->tk_status);
220
		nfs4_mark_deviceid_unavailable(devid);
221
		set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
222
		rpc_wake_up(&tbl->slot_tbl_waitq);
223
		/* fall through */
224
	default:
225
reset:
226
		dprintk("%s Retry through MDS. Error %d\n", __func__,
227
			task->tk_status);
228
		return -NFS4ERR_RESET_TO_MDS;
229
	}
A
Andy Adamson 已提交
230
out:
231 232
	task->tk_status = 0;
	return -EAGAIN;
233 234 235
out_bad_stateid:
	task->tk_status = -EIO;
	return 0;
A
Andy Adamson 已提交
236 237 238 239 240
wait_on_recovery:
	rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
	if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
		rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
	goto out;
241 242 243 244 245
}

/* NFS_PROTO call done callback routines */

static int filelayout_read_done_cb(struct rpc_task *task,
246
				struct nfs_pgio_data *data)
247
{
248 249
	struct nfs_pgio_header *hdr = data->header;
	int err;
250

251
	trace_nfs4_pnfs_read(data, task->tk_status);
252 253
	err = filelayout_async_handle_error(task, data->args.context->state,
					    data->ds_clp, hdr->lseg);
254

255 256 257 258 259
	switch (err) {
	case -NFS4ERR_RESET_TO_MDS:
		filelayout_reset_read(data);
		return task->tk_status;
	case -EAGAIN:
260
		rpc_restart_call_prepare(task);
261 262 263 264 265 266
		return -EAGAIN;
	}

	return 0;
}

A
Andy Adamson 已提交
267 268 269 270 271 272
/*
 * We reference the rpc_cred of the first WRITE that triggers the need for
 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
 * rfc5661 is not clear about which credential should be used.
 */
static void
273
filelayout_set_layoutcommit(struct nfs_pgio_data *wdata)
A
Andy Adamson 已提交
274
{
275 276 277
	struct nfs_pgio_header *hdr = wdata->header;

	if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
A
Andy Adamson 已提交
278 279 280 281
	    wdata->res.verf->committed == NFS_FILE_SYNC)
		return;

	pnfs_set_layoutcommit(wdata);
282 283
	dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
		(unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
A
Andy Adamson 已提交
284 285
}

286 287 288 289 290 291 292 293 294 295 296 297
bool
filelayout_test_devid_unavailable(struct nfs4_deviceid_node *node)
{
	return filelayout_test_devid_invalid(node) ||
		nfs4_test_deviceid_unavailable(node);
}

static bool
filelayout_reset_to_mds(struct pnfs_layout_segment *lseg)
{
	struct nfs4_deviceid_node *node = FILELAYOUT_DEVID_NODE(lseg);

298
	return filelayout_test_devid_unavailable(node);
299 300
}

A
Andy Adamson 已提交
301 302 303 304 305 306 307
/*
 * Call ops for the async read/write cases
 * In the case of dense layouts, the offset needs to be reset to its
 * original value.
 */
static void filelayout_read_prepare(struct rpc_task *task, void *data)
{
308
	struct nfs_pgio_data *rdata = data;
A
Andy Adamson 已提交
309

310 311 312 313
	if (unlikely(test_bit(NFS_CONTEXT_BAD, &rdata->args.context->flags))) {
		rpc_exit(task, -EIO);
		return;
	}
314
	if (filelayout_reset_to_mds(rdata->header->lseg)) {
315 316 317 318 319
		dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
		filelayout_reset_read(rdata);
		rpc_exit(task, 0);
		return;
	}
320
	rdata->pgio_done_cb = filelayout_read_done_cb;
321

322
	if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
323 324
			&rdata->args.seq_args,
			&rdata->res.seq_res,
325 326
			task))
		return;
327 328 329
	if (nfs4_set_rw_stateid(&rdata->args.stateid, rdata->args.context,
			rdata->args.lock_context, FMODE_READ) == -EIO)
		rpc_exit(task, -EIO); /* lost lock, terminate I/O */
A
Andy Adamson 已提交
330 331 332 333
}

static void filelayout_read_call_done(struct rpc_task *task, void *data)
{
334
	struct nfs_pgio_data *rdata = data;
A
Andy Adamson 已提交
335 336 337

	dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);

338
	if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags) &&
339
	    task->tk_status == 0) {
T
Trond Myklebust 已提交
340
		nfs41_sequence_done(task, &rdata->res.seq_res);
341
		return;
342
	}
343

A
Andy Adamson 已提交
344
	/* Note this may cause RPC to be resent */
345
	rdata->header->mds_ops->rpc_call_done(task, data);
A
Andy Adamson 已提交
346 347
}

348 349
static void filelayout_read_count_stats(struct rpc_task *task, void *data)
{
350
	struct nfs_pgio_data *rdata = data;
351

352
	rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
353 354
}

A
Andy Adamson 已提交
355 356
static void filelayout_read_release(void *data)
{
357
	struct nfs_pgio_data *rdata = data;
358
	struct pnfs_layout_hdr *lo = rdata->header->lseg->pls_layout;
A
Andy Adamson 已提交
359

360
	filelayout_fenceme(lo->plh_inode, lo);
361
	nfs_put_client(rdata->ds_clp);
362
	rdata->header->mds_ops->rpc_release(data);
A
Andy Adamson 已提交
363 364
}

365
static int filelayout_write_done_cb(struct rpc_task *task,
366
				struct nfs_pgio_data *data)
367
{
368 369 370
	struct nfs_pgio_header *hdr = data->header;
	int err;

371
	trace_nfs4_pnfs_write(data, task->tk_status);
372 373 374 375 376 377 378 379
	err = filelayout_async_handle_error(task, data->args.context->state,
					    data->ds_clp, hdr->lseg);

	switch (err) {
	case -NFS4ERR_RESET_TO_MDS:
		filelayout_reset_write(data);
		return task->tk_status;
	case -EAGAIN:
380
		rpc_restart_call_prepare(task);
381 382 383
		return -EAGAIN;
	}

A
Andy Adamson 已提交
384
	filelayout_set_layoutcommit(data);
385 386 387
	return 0;
}

388
/* Fake up some data that will cause nfs_commit_release to retry the writes. */
389
static void prepare_to_resend_writes(struct nfs_commit_data *data)
390 391 392 393
{
	struct nfs_page *first = nfs_list_entry(data->pages.next);

	data->task.tk_status = 0;
394 395 396
	memcpy(&data->verf.verifier, &first->wb_verf,
	       sizeof(data->verf.verifier));
	data->verf.verifier.data[0]++; /* ensure verifier mismatch */
397 398 399
}

static int filelayout_commit_done_cb(struct rpc_task *task,
400
				     struct nfs_commit_data *data)
401
{
402 403
	int err;

404
	trace_nfs4_pnfs_commit_ds(data, task->tk_status);
405 406 407 408 409 410 411 412 413
	err = filelayout_async_handle_error(task, NULL, data->ds_clp,
					    data->lseg);

	switch (err) {
	case -NFS4ERR_RESET_TO_MDS:
		prepare_to_resend_writes(data);
		return -EAGAIN;
	case -EAGAIN:
		rpc_restart_call_prepare(task);
414 415 416 417 418 419
		return -EAGAIN;
	}

	return 0;
}

420 421
static void filelayout_write_prepare(struct rpc_task *task, void *data)
{
422
	struct nfs_pgio_data *wdata = data;
423

424 425 426 427
	if (unlikely(test_bit(NFS_CONTEXT_BAD, &wdata->args.context->flags))) {
		rpc_exit(task, -EIO);
		return;
	}
428
	if (filelayout_reset_to_mds(wdata->header->lseg)) {
429 430 431 432 433
		dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
		filelayout_reset_write(wdata);
		rpc_exit(task, 0);
		return;
	}
434
	if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
435 436
			&wdata->args.seq_args,
			&wdata->res.seq_res,
437 438
			task))
		return;
439 440 441
	if (nfs4_set_rw_stateid(&wdata->args.stateid, wdata->args.context,
			wdata->args.lock_context, FMODE_WRITE) == -EIO)
		rpc_exit(task, -EIO); /* lost lock, terminate I/O */
442 443 444 445
}

static void filelayout_write_call_done(struct rpc_task *task, void *data)
{
446
	struct nfs_pgio_data *wdata = data;
447

448
	if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags) &&
449
	    task->tk_status == 0) {
T
Trond Myklebust 已提交
450
		nfs41_sequence_done(task, &wdata->res.seq_res);
451
		return;
452
	}
453

454
	/* Note this may cause RPC to be resent */
455
	wdata->header->mds_ops->rpc_call_done(task, data);
456 457
}

458 459
static void filelayout_write_count_stats(struct rpc_task *task, void *data)
{
460
	struct nfs_pgio_data *wdata = data;
461

462
	rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
463 464
}

465 466
static void filelayout_write_release(void *data)
{
467
	struct nfs_pgio_data *wdata = data;
468
	struct pnfs_layout_hdr *lo = wdata->header->lseg->pls_layout;
469

470
	filelayout_fenceme(lo->plh_inode, lo);
471
	nfs_put_client(wdata->ds_clp);
472
	wdata->header->mds_ops->rpc_release(data);
473 474
}

475
static void filelayout_commit_prepare(struct rpc_task *task, void *data)
476
{
477
	struct nfs_commit_data *wdata = data;
478

479 480 481 482
	nfs41_setup_sequence(wdata->ds_clp->cl_session,
			&wdata->args.seq_args,
			&wdata->res.seq_res,
			task);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
}

static void filelayout_write_commit_done(struct rpc_task *task, void *data)
{
	struct nfs_commit_data *wdata = data;

	/* Note this may cause RPC to be resent */
	wdata->mds_ops->rpc_call_done(task, data);
}

static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
{
	struct nfs_commit_data *cdata = data;

	rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
}

static void filelayout_commit_release(void *calldata)
{
	struct nfs_commit_data *data = calldata;

504
	data->completion_ops->completion(data);
505
	pnfs_put_lseg(data->lseg);
506
	nfs_put_client(data->ds_clp);
507
	nfs_commitdata_release(data);
508 509
}

510
static const struct rpc_call_ops filelayout_read_call_ops = {
A
Andy Adamson 已提交
511 512
	.rpc_call_prepare = filelayout_read_prepare,
	.rpc_call_done = filelayout_read_call_done,
513
	.rpc_count_stats = filelayout_read_count_stats,
A
Andy Adamson 已提交
514 515 516
	.rpc_release = filelayout_read_release,
};

517
static const struct rpc_call_ops filelayout_write_call_ops = {
518 519
	.rpc_call_prepare = filelayout_write_prepare,
	.rpc_call_done = filelayout_write_call_done,
520
	.rpc_count_stats = filelayout_write_count_stats,
521 522 523
	.rpc_release = filelayout_write_release,
};

524
static const struct rpc_call_ops filelayout_commit_call_ops = {
525 526 527
	.rpc_call_prepare = filelayout_commit_prepare,
	.rpc_call_done = filelayout_write_commit_done,
	.rpc_count_stats = filelayout_commit_count_stats,
528 529 530
	.rpc_release = filelayout_commit_release,
};

A
Andy Adamson 已提交
531
static enum pnfs_try_status
532
filelayout_read_pagelist(struct nfs_pgio_data *data)
A
Andy Adamson 已提交
533
{
534 535
	struct nfs_pgio_header *hdr = data->header;
	struct pnfs_layout_segment *lseg = hdr->lseg;
A
Andy Adamson 已提交
536
	struct nfs4_pnfs_ds *ds;
537
	struct rpc_clnt *ds_clnt;
A
Andy Adamson 已提交
538 539 540 541 542
	loff_t offset = data->args.offset;
	u32 j, idx;
	struct nfs_fh *fh;

	dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
543
		__func__, hdr->inode->i_ino,
A
Andy Adamson 已提交
544 545 546 547 548 549
		data->args.pgbase, (size_t)data->args.count, offset);

	/* Retrieve the correct rpc_client for the byte range */
	j = nfs4_fl_calc_j_index(lseg, offset);
	idx = nfs4_fl_calc_ds_index(lseg, j);
	ds = nfs4_fl_prepare_ds(lseg, idx);
550
	if (!ds)
A
Andy Adamson 已提交
551
		return PNFS_NOT_ATTEMPTED;
552 553 554 555 556

	ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
	if (IS_ERR(ds_clnt))
		return PNFS_NOT_ATTEMPTED;

557 558
	dprintk("%s USE DS: %s cl_count %d\n", __func__,
		ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
A
Andy Adamson 已提交
559 560

	/* No multipath support. Use first DS */
561
	atomic_inc(&ds->ds_clp->cl_count);
A
Andy Adamson 已提交
562 563 564 565 566 567 568 569 570
	data->ds_clp = ds->ds_clp;
	fh = nfs4_fl_select_ds_fh(lseg, j);
	if (fh)
		data->args.fh = fh;

	data->args.offset = filelayout_get_dserver_offset(lseg, offset);
	data->mds_offset = offset;

	/* Perform an asynchronous read to ds */
571
	nfs_initiate_read(ds_clnt, data,
572
				  &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
A
Andy Adamson 已提交
573 574 575
	return PNFS_ATTEMPTED;
}

576
/* Perform async writes. */
577
static enum pnfs_try_status
578
filelayout_write_pagelist(struct nfs_pgio_data *data, int sync)
579
{
580 581
	struct nfs_pgio_header *hdr = data->header;
	struct pnfs_layout_segment *lseg = hdr->lseg;
582
	struct nfs4_pnfs_ds *ds;
583
	struct rpc_clnt *ds_clnt;
584 585 586 587 588 589 590 591
	loff_t offset = data->args.offset;
	u32 j, idx;
	struct nfs_fh *fh;

	/* Retrieve the correct rpc_client for the byte range */
	j = nfs4_fl_calc_j_index(lseg, offset);
	idx = nfs4_fl_calc_ds_index(lseg, j);
	ds = nfs4_fl_prepare_ds(lseg, idx);
592
	if (!ds)
593
		return PNFS_NOT_ATTEMPTED;
594 595 596 597 598

	ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
	if (IS_ERR(ds_clnt))
		return PNFS_NOT_ATTEMPTED;

599 600 601
	dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
		__func__, hdr->inode->i_ino, sync, (size_t) data->args.count,
		offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
602

603
	data->pgio_done_cb = filelayout_write_done_cb;
604
	atomic_inc(&ds->ds_clp->cl_count);
605 606 607 608 609 610 611 612 613 614 615
	data->ds_clp = ds->ds_clp;
	fh = nfs4_fl_select_ds_fh(lseg, j);
	if (fh)
		data->args.fh = fh;
	/*
	 * Get the file offset on the dserver. Set the write offset to
	 * this offset and save the original offset.
	 */
	data->args.offset = filelayout_get_dserver_offset(lseg, offset);

	/* Perform an asynchronous write */
616
	nfs_initiate_write(ds_clnt, data,
617 618
				    &filelayout_write_call_ops, sync,
				    RPC_TASK_SOFTCONN);
619
	return PNFS_ATTEMPTED;
620 621
}

622 623 624 625 626 627 628 629 630 631 632 633
/*
 * filelayout_check_layout()
 *
 * Make sure layout segment parameters are sane WRT the device.
 * At this point no generic layer initialization of the lseg has occurred,
 * and nothing has been added to the layout_hdr cache.
 *
 */
static int
filelayout_check_layout(struct pnfs_layout_hdr *lo,
			struct nfs4_filelayout_segment *fl,
			struct nfs4_layoutget_res *lgr,
634 635
			struct nfs4_deviceid *id,
			gfp_t gfp_flags)
636
{
637
	struct nfs4_deviceid_node *d;
638 639
	struct nfs4_file_layout_dsaddr *dsaddr;
	int status = -EINVAL;
640
	struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
641 642 643

	dprintk("--> %s\n", __func__);

644 645 646 647 648 649 650 651
	/* FIXME: remove this check when layout segment support is added */
	if (lgr->range.offset != 0 ||
	    lgr->range.length != NFS4_MAX_UINT64) {
		dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
			__func__);
		goto out;
	}

652
	if (fl->pattern_offset > lgr->range.offset) {
653
		dprintk("%s pattern_offset %lld too large\n",
654 655 656 657
				__func__, fl->pattern_offset);
		goto out;
	}

658 659
	if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
		dprintk("%s Invalid stripe unit (%u)\n",
660 661 662 663 664
			__func__, fl->stripe_unit);
		goto out;
	}

	/* find and reference the deviceid */
665 666
	d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
				   NFS_SERVER(lo->plh_inode)->nfs_client, id);
667
	if (d == NULL) {
668 669
		dsaddr = filelayout_get_device_info(lo->plh_inode, id,
				lo->plh_lc_cred, gfp_flags);
670 671
		if (dsaddr == NULL)
			goto out;
672 673
	} else
		dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
674 675
	/* Found deviceid is unavailable */
	if (filelayout_test_devid_unavailable(&dsaddr->id_node))
676 677
			goto out_put;

678 679
	fl->dsaddr = dsaddr;

680 681
	if (fl->first_stripe_index >= dsaddr->stripe_count) {
		dprintk("%s Bad first_stripe_index %u\n",
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
				__func__, fl->first_stripe_index);
		goto out_put;
	}

	if ((fl->stripe_type == STRIPE_SPARSE &&
	    fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
	    (fl->stripe_type == STRIPE_DENSE &&
	    fl->num_fh != dsaddr->stripe_count)) {
		dprintk("%s num_fh %u not valid for given packing\n",
			__func__, fl->num_fh);
		goto out_put;
	}

	if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
		dprintk("%s Stripe unit (%u) not aligned with rsize %u "
			"wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
			nfss->wsize);
	}

	status = 0;
out:
	dprintk("--> %s returns %d\n", __func__, status);
	return status;
out_put:
706
	nfs4_fl_put_deviceid(dsaddr);
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	goto out;
}

static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
{
	int i;

	for (i = 0; i < fl->num_fh; i++) {
		if (!fl->fh_array[i])
			break;
		kfree(fl->fh_array[i]);
	}
	kfree(fl->fh_array);
	fl->fh_array = NULL;
}

static void
_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
{
	filelayout_free_fh_array(fl);
	kfree(fl);
}

static int
filelayout_decode_layout(struct pnfs_layout_hdr *flo,
			 struct nfs4_filelayout_segment *fl,
			 struct nfs4_layoutget_res *lgr,
734 735
			 struct nfs4_deviceid *id,
			 gfp_t gfp_flags)
736
{
737
	struct xdr_stream stream;
738
	struct xdr_buf buf;
739 740
	struct page *scratch;
	__be32 *p;
741 742 743 744 745
	uint32_t nfl_util;
	int i;

	dprintk("%s: set_layout_map Begin\n", __func__);

746
	scratch = alloc_page(gfp_flags);
747 748 749
	if (!scratch)
		return -ENOMEM;

750
	xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
751 752 753 754 755 756 757 758
	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);

	/* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
	 * num_fh (4) */
	p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
	if (unlikely(!p))
		goto out_err;

759 760
	memcpy(id, p, sizeof(*id));
	p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
761
	nfs4_print_deviceid(id);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

	nfl_util = be32_to_cpup(p++);
	if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
		fl->commit_through_mds = 1;
	if (nfl_util & NFL4_UFLG_DENSE)
		fl->stripe_type = STRIPE_DENSE;
	else
		fl->stripe_type = STRIPE_SPARSE;
	fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;

	fl->first_stripe_index = be32_to_cpup(p++);
	p = xdr_decode_hyper(p, &fl->pattern_offset);
	fl->num_fh = be32_to_cpup(p++);

	dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
		__func__, nfl_util, fl->num_fh, fl->first_stripe_index,
		fl->pattern_offset);

780 781
	/* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
	 * Futher checking is done in filelayout_check_layout */
782
	if (fl->num_fh >
783
	    max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
784 785
		goto out_err;

786
	if (fl->num_fh > 0) {
787
		fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
788 789 790 791
				       gfp_flags);
		if (!fl->fh_array)
			goto out_err;
	}
792 793 794

	for (i = 0; i < fl->num_fh; i++) {
		/* Do we want to use a mempool here? */
795
		fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
796 797 798 799 800 801
		if (!fl->fh_array[i])
			goto out_err_free;

		p = xdr_inline_decode(&stream, 4);
		if (unlikely(!p))
			goto out_err_free;
802 803
		fl->fh_array[i]->size = be32_to_cpup(p++);
		if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
804
			printk(KERN_ERR "NFS: Too big fh %d received %d\n",
805
			       i, fl->fh_array[i]->size);
806
			goto out_err_free;
807
		}
808 809 810 811

		p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
		if (unlikely(!p))
			goto out_err_free;
812 813 814 815 816
		memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
		dprintk("DEBUG: %s: fh len %d\n", __func__,
			fl->fh_array[i]->size);
	}

817
	__free_page(scratch);
818
	return 0;
819 820 821 822 823 824

out_err_free:
	filelayout_free_fh_array(fl);
out_err:
	__free_page(scratch);
	return -EIO;
825 826
}

827 828 829 830 831 832 833
static void
filelayout_free_lseg(struct pnfs_layout_segment *lseg)
{
	struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);

	dprintk("--> %s\n", __func__);
	nfs4_fl_put_deviceid(fl->dsaddr);
834 835 836 837 838 839 840 841 842
	/* This assumes a single RW lseg */
	if (lseg->pls_range.iomode == IOMODE_RW) {
		struct nfs4_filelayout *flo;

		flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
		flo->commit_info.nbuckets = 0;
		kfree(flo->commit_info.buckets);
		flo->commit_info.buckets = NULL;
	}
843 844 845
	_filelayout_free_lseg(fl);
}

846 847
static int
filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
F
Fred Isaman 已提交
848
			     struct nfs_commit_info *cinfo,
849 850 851
			     gfp_t gfp_flags)
{
	struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
F
Fred Isaman 已提交
852
	struct pnfs_commit_bucket *buckets;
853 854 855 856
	int size;

	if (fl->commit_through_mds)
		return 0;
F
Fred Isaman 已提交
857
	if (cinfo->ds->nbuckets != 0) {
858 859 860 861 862 863 864 865 866 867 868 869
		/* This assumes there is only one IOMODE_RW lseg.  What
		 * we really want to do is have a layout_hdr level
		 * dictionary of <multipath_list4, fh> keys, each
		 * associated with a struct list_head, populated by calls
		 * to filelayout_write_pagelist().
		 * */
		return 0;
	}

	size = (fl->stripe_type == STRIPE_SPARSE) ?
		fl->dsaddr->ds_num : fl->dsaddr->stripe_count;

F
Fred Isaman 已提交
870
	buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
871 872 873 874 875 876
			  gfp_flags);
	if (!buckets)
		return -ENOMEM;
	else {
		int i;

F
Fred Isaman 已提交
877 878
		spin_lock(cinfo->lock);
		if (cinfo->ds->nbuckets != 0)
879 880
			kfree(buckets);
		else {
F
Fred Isaman 已提交
881 882
			cinfo->ds->buckets = buckets;
			cinfo->ds->nbuckets = size;
883 884 885 886 887
			for (i = 0; i < size; i++) {
				INIT_LIST_HEAD(&buckets[i].written);
				INIT_LIST_HEAD(&buckets[i].committing);
			}
		}
F
Fred Isaman 已提交
888
		spin_unlock(cinfo->lock);
889 890 891 892
		return 0;
	}
}

893 894
static struct pnfs_layout_segment *
filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
895 896
		      struct nfs4_layoutget_res *lgr,
		      gfp_t gfp_flags)
897 898 899 900 901 902
{
	struct nfs4_filelayout_segment *fl;
	int rc;
	struct nfs4_deviceid id;

	dprintk("--> %s\n", __func__);
903
	fl = kzalloc(sizeof(*fl), gfp_flags);
904 905 906
	if (!fl)
		return NULL;

907 908
	rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
	if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
909 910 911 912 913 914
		_filelayout_free_lseg(fl);
		return NULL;
	}
	return &fl->generic_hdr;
}

915 916 917
/*
 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
 *
918 919
 * return true  : coalesce page
 * return false : don't coalesce page
920
 */
921
static bool
922 923 924 925 926 927
filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
		   struct nfs_page *req)
{
	u64 p_stripe, r_stripe;
	u32 stripe_unit;

928 929 930
	if (!pnfs_generic_pg_test(pgio, prev, req) ||
	    !nfs_generic_pg_test(pgio, prev, req))
		return false;
931

932 933
	p_stripe = (u64)req_offset(prev);
	r_stripe = (u64)req_offset(req);
934 935 936 937 938 939 940 941
	stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;

	do_div(p_stripe, stripe_unit);
	do_div(r_stripe, stripe_unit);

	return (p_stripe == r_stripe);
}

942
static void
943 944 945
filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
			struct nfs_page *req)
{
946
	WARN_ON_ONCE(pgio->pg_lseg != NULL);
947

948 949 950 951 952 953 954 955 956 957
	if (req->wb_offset != req->wb_pgbase) {
		/*
		 * Handling unaligned pages is difficult, because have to
		 * somehow split a req in two in certain cases in the
		 * pg.test code.  Avoid this by just not using pnfs
		 * in this case.
		 */
		nfs_pageio_reset_read_mds(pgio);
		return;
	}
958 959 960 961 962 963 964 965
	pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
					   req->wb_context,
					   0,
					   NFS4_MAX_UINT64,
					   IOMODE_READ,
					   GFP_KERNEL);
	/* If no lseg, fall back to read through mds */
	if (pgio->pg_lseg == NULL)
966
		nfs_pageio_reset_read_mds(pgio);
967 968
}

969
static void
970 971 972
filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
			 struct nfs_page *req)
{
F
Fred Isaman 已提交
973
	struct nfs_commit_info cinfo;
974 975
	int status;

976
	WARN_ON_ONCE(pgio->pg_lseg != NULL);
977

978 979
	if (req->wb_offset != req->wb_pgbase)
		goto out_mds;
980 981 982 983 984 985 986 987
	pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
					   req->wb_context,
					   0,
					   NFS4_MAX_UINT64,
					   IOMODE_RW,
					   GFP_NOFS);
	/* If no lseg, fall back to write through mds */
	if (pgio->pg_lseg == NULL)
988
		goto out_mds;
F
Fred Isaman 已提交
989 990
	nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
	status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
991
	if (status < 0) {
992
		pnfs_put_lseg(pgio->pg_lseg);
993 994 995 996 997 998
		pgio->pg_lseg = NULL;
		goto out_mds;
	}
	return;
out_mds:
	nfs_pageio_reset_write_mds(pgio);
999 1000
}

1001
static const struct nfs_pageio_ops filelayout_pg_read_ops = {
1002
	.pg_init = filelayout_pg_init_read,
1003
	.pg_test = filelayout_pg_test,
1004
	.pg_doio = pnfs_generic_pg_readpages,
1005 1006 1007
};

static const struct nfs_pageio_ops filelayout_pg_write_ops = {
1008
	.pg_init = filelayout_pg_init_write,
1009
	.pg_test = filelayout_pg_test,
1010
	.pg_doio = pnfs_generic_pg_writepages,
1011 1012
};

1013 1014 1015 1016 1017 1018 1019 1020
static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
{
	if (fl->stripe_type == STRIPE_SPARSE)
		return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
	else
		return j;
}

F
Fred Isaman 已提交
1021 1022 1023
/* The generic layer is about to remove the req from the commit list.
 * If this will make the bucket empty, it will need to put the lseg reference.
 */
1024
static void
F
Fred Isaman 已提交
1025 1026
filelayout_clear_request_commit(struct nfs_page *req,
				struct nfs_commit_info *cinfo)
F
Fred Isaman 已提交
1027
{
1028 1029
	struct pnfs_layout_segment *freeme = NULL;

F
Fred Isaman 已提交
1030
	spin_lock(cinfo->lock);
1031 1032
	if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
		goto out;
F
Fred Isaman 已提交
1033
	cinfo->ds->nwritten--;
F
Fred Isaman 已提交
1034
	if (list_is_singular(&req->wb_list)) {
F
Fred Isaman 已提交
1035
		struct pnfs_commit_bucket *bucket;
F
Fred Isaman 已提交
1036

1037
		bucket = list_first_entry(&req->wb_list,
F
Fred Isaman 已提交
1038
					  struct pnfs_commit_bucket,
1039 1040 1041
					  written);
		freeme = bucket->wlseg;
		bucket->wlseg = NULL;
F
Fred Isaman 已提交
1042
	}
1043
out:
F
Fred Isaman 已提交
1044 1045
	nfs_request_remove_commit_list(req, cinfo);
	spin_unlock(cinfo->lock);
1046
	pnfs_put_lseg(freeme);
F
Fred Isaman 已提交
1047 1048 1049 1050
}

static struct list_head *
filelayout_choose_commit_list(struct nfs_page *req,
F
Fred Isaman 已提交
1051 1052
			      struct pnfs_layout_segment *lseg,
			      struct nfs_commit_info *cinfo)
1053 1054 1055 1056
{
	struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
	u32 i, j;
	struct list_head *list;
F
Fred Isaman 已提交
1057
	struct pnfs_commit_bucket *buckets;
1058

F
Fred Isaman 已提交
1059
	if (fl->commit_through_mds)
F
Fred Isaman 已提交
1060
		return &cinfo->mds->list;
F
Fred Isaman 已提交
1061

1062 1063 1064 1065 1066 1067
	/* Note that we are calling nfs4_fl_calc_j_index on each page
	 * that ends up being committed to a data server.  An attractive
	 * alternative is to add a field to nfs_write_data and nfs_page
	 * to store the value calculated in filelayout_write_pagelist
	 * and just use that here.
	 */
1068
	j = nfs4_fl_calc_j_index(lseg, req_offset(req));
1069
	i = select_bucket_index(fl, j);
F
Fred Isaman 已提交
1070
	buckets = cinfo->ds->buckets;
1071
	list = &buckets[i].written;
1072
	if (list_empty(list)) {
F
Fred Isaman 已提交
1073 1074 1075 1076
		/* Non-empty buckets hold a reference on the lseg.  That ref
		 * is normally transferred to the COMMIT call and released
		 * there.  It could also be released if the last req is pulled
		 * off due to a rewrite, in which case it will be done in
1077
		 * filelayout_clear_request_commit
F
Fred Isaman 已提交
1078
		 */
1079
		buckets[i].wlseg = pnfs_get_lseg(lseg);
1080
	}
1081
	set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
F
Fred Isaman 已提交
1082
	cinfo->ds->nwritten++;
1083 1084 1085
	return list;
}

1086 1087
static void
filelayout_mark_request_commit(struct nfs_page *req,
F
Fred Isaman 已提交
1088 1089
			       struct pnfs_layout_segment *lseg,
			       struct nfs_commit_info *cinfo)
1090 1091 1092
{
	struct list_head *list;

F
Fred Isaman 已提交
1093 1094
	list = filelayout_choose_commit_list(req, lseg, cinfo);
	nfs_request_add_commit_list(req, list, cinfo);
1095 1096
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
{
	struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);

	if (flseg->stripe_type == STRIPE_SPARSE)
		return i;
	else
		return nfs4_fl_calc_ds_index(lseg, i);
}

static struct nfs_fh *
select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
{
	struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);

	if (flseg->stripe_type == STRIPE_SPARSE) {
		if (flseg->num_fh == 1)
			i = 0;
		else if (flseg->num_fh == 0)
			/* Use the MDS OPEN fh set in nfs_read_rpcsetup */
			return NULL;
	}
	return flseg->fh_array[i];
}

1122
static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
1123 1124 1125
{
	struct pnfs_layout_segment *lseg = data->lseg;
	struct nfs4_pnfs_ds *ds;
1126
	struct rpc_clnt *ds_clnt;
1127 1128 1129 1130 1131
	u32 idx;
	struct nfs_fh *fh;

	idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
	ds = nfs4_fl_prepare_ds(lseg, idx);
1132 1133 1134 1135 1136 1137 1138
	if (!ds)
		goto out_err;

	ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
	if (IS_ERR(ds_clnt))
		goto out_err;

1139 1140
	dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
		data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
1141
	data->commit_done_cb = filelayout_commit_done_cb;
1142
	atomic_inc(&ds->ds_clp->cl_count);
1143 1144 1145 1146
	data->ds_clp = ds->ds_clp;
	fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
	if (fh)
		data->args.fh = fh;
1147
	return nfs_initiate_commit(ds_clnt, data,
1148 1149
				   &filelayout_commit_call_ops, how,
				   RPC_TASK_SOFTCONN);
1150 1151 1152 1153
out_err:
	prepare_to_resend_writes(data);
	filelayout_commit_release(data);
	return -EAGAIN;
1154 1155
}

1156
static int
1157 1158
transfer_commit_list(struct list_head *src, struct list_head *dst,
		     struct nfs_commit_info *cinfo, int max)
1159 1160 1161 1162 1163 1164 1165
{
	struct nfs_page *req, *tmp;
	int ret = 0;

	list_for_each_entry_safe(req, tmp, src, wb_list) {
		if (!nfs_lock_request(req))
			continue;
1166
		kref_get(&req->wb_kref);
F
Fred Isaman 已提交
1167
		if (cond_resched_lock(cinfo->lock))
1168
			list_safe_reset_next(req, tmp, wb_list);
F
Fred Isaman 已提交
1169
		nfs_request_remove_commit_list(req, cinfo);
1170 1171 1172
		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
		nfs_list_add_request(req, dst);
		ret++;
1173
		if ((ret == max) && !cinfo->dreq)
1174 1175
			break;
	}
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	return ret;
}

static int
filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
			       struct nfs_commit_info *cinfo,
			       int max)
{
	struct list_head *src = &bucket->written;
	struct list_head *dst = &bucket->committing;
	int ret;

	ret = transfer_commit_list(src, dst, cinfo, max);
1189
	if (ret) {
F
Fred Isaman 已提交
1190 1191
		cinfo->ds->nwritten -= ret;
		cinfo->ds->ncommitting += ret;
1192 1193 1194 1195
		bucket->clseg = bucket->wlseg;
		if (list_empty(src))
			bucket->wlseg = NULL;
		else
1196
			pnfs_get_lseg(bucket->clseg);
1197
	}
1198 1199 1200
	return ret;
}

F
Fred Isaman 已提交
1201
/* Move reqs from written to committing lists, returning count of number moved.
F
Fred Isaman 已提交
1202
 * Note called with cinfo->lock held.
F
Fred Isaman 已提交
1203
 */
F
Fred Isaman 已提交
1204 1205
static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
					int max)
F
Fred Isaman 已提交
1206 1207 1208
{
	int i, rv = 0, cnt;

F
Fred Isaman 已提交
1209 1210 1211
	for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
		cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
						     cinfo, max);
F
Fred Isaman 已提交
1212 1213 1214 1215 1216 1217
		max -= cnt;
		rv += cnt;
	}
	return rv;
}

1218 1219 1220 1221 1222 1223 1224
/* Pull everything off the committing lists and dump into @dst */
static void filelayout_recover_commit_reqs(struct list_head *dst,
					   struct nfs_commit_info *cinfo)
{
	struct pnfs_commit_bucket *b;
	int i;

1225
	spin_lock(cinfo->lock);
1226 1227
	for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
		if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1228
			spin_unlock(cinfo->lock);
1229
			pnfs_put_lseg(b->wlseg);
1230
			b->wlseg = NULL;
1231
			spin_lock(cinfo->lock);
1232 1233 1234
		}
	}
	cinfo->ds->nwritten = 0;
1235
	spin_unlock(cinfo->lock);
1236 1237
}

1238
static unsigned int
F
Fred Isaman 已提交
1239
alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
1240
{
F
Fred Isaman 已提交
1241 1242
	struct pnfs_ds_commit_info *fl_cinfo;
	struct pnfs_commit_bucket *bucket;
1243
	struct nfs_commit_data *data;
1244
	int i, j;
1245
	unsigned int nreq = 0;
1246

F
Fred Isaman 已提交
1247
	fl_cinfo = cinfo->ds;
1248 1249 1250
	bucket = fl_cinfo->buckets;
	for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
		if (list_empty(&bucket->committing))
1251 1252 1253
			continue;
		data = nfs_commitdata_alloc();
		if (!data)
1254
			break;
1255
		data->ds_commit_index = i;
1256 1257
		data->lseg = bucket->clseg;
		bucket->clseg = NULL;
1258
		list_add(&data->pages, list);
1259
		nreq++;
1260 1261
	}

1262
	/* Clean up on error */
1263 1264
	for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
		if (list_empty(&bucket->committing))
1265
			continue;
F
Fred Isaman 已提交
1266
		nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
1267
		pnfs_put_lseg(bucket->clseg);
1268
		bucket->clseg = NULL;
1269 1270
	}
	/* Caller will clean up entries put on list */
1271
	return nreq;
1272 1273 1274 1275 1276
}

/* This follows nfs_commit_list pretty closely */
static int
filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
F
Fred Isaman 已提交
1277
			   int how, struct nfs_commit_info *cinfo)
1278
{
1279
	struct nfs_commit_data *data, *tmp;
1280
	LIST_HEAD(list);
1281
	unsigned int nreq = 0;
1282 1283 1284

	if (!list_empty(mds_pages)) {
		data = nfs_commitdata_alloc();
1285 1286 1287 1288 1289
		if (data != NULL) {
			data->lseg = NULL;
			list_add(&data->pages, &list);
			nreq++;
		} else
F
Fred Isaman 已提交
1290
			nfs_retry_commit(mds_pages, NULL, cinfo);
1291 1292
	}

F
Fred Isaman 已提交
1293
	nreq += alloc_ds_commits(cinfo, &list);
1294 1295

	if (nreq == 0) {
1296
		cinfo->completion_ops->error_cleanup(NFS_I(inode));
1297 1298 1299
		goto out;
	}

F
Fred Isaman 已提交
1300
	atomic_add(nreq, &cinfo->mds->rpcs_out);
1301 1302 1303 1304

	list_for_each_entry_safe(data, tmp, &list, pages) {
		list_del_init(&data->pages);
		if (!data->lseg) {
1305
			nfs_init_commit(data, mds_pages, NULL, cinfo);
1306
			nfs_initiate_commit(NFS_CLIENT(inode), data,
1307
					    data->mds_ops, how, 0);
1308
		} else {
F
Fred Isaman 已提交
1309
			struct pnfs_commit_bucket *buckets;
1310

F
Fred Isaman 已提交
1311
			buckets = cinfo->ds->buckets;
1312
			nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
1313 1314 1315
			filelayout_initiate_commit(data, how);
		}
	}
1316
out:
F
Fred Isaman 已提交
1317
	cinfo->ds->ncommitting = 0;
1318
	return PNFS_ATTEMPTED;
1319 1320
}

1321 1322 1323 1324 1325 1326
static void
filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
{
	nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
static struct pnfs_layout_hdr *
filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
{
	struct nfs4_filelayout *flo;

	flo = kzalloc(sizeof(*flo), gfp_flags);
	return &flo->generic_hdr;
}

static void
filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
{
	kfree(FILELAYOUT_FROM_HDR(lo));
}

F
Fred Isaman 已提交
1342 1343 1344
static struct pnfs_ds_commit_info *
filelayout_get_ds_info(struct inode *inode)
{
1345 1346 1347 1348 1349 1350
	struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;

	if (layout == NULL)
		return NULL;
	else
		return &FILELAYOUT_FROM_HDR(layout)->commit_info;
F
Fred Isaman 已提交
1351 1352
}

1353
static struct pnfs_layoutdriver_type filelayout_type = {
1354 1355 1356
	.id			= LAYOUT_NFSV4_1_FILES,
	.name			= "LAYOUT_NFSV4_1_FILES",
	.owner			= THIS_MODULE,
1357 1358
	.alloc_layout_hdr	= filelayout_alloc_layout_hdr,
	.free_layout_hdr	= filelayout_free_layout_hdr,
1359 1360
	.alloc_lseg		= filelayout_alloc_lseg,
	.free_lseg		= filelayout_free_lseg,
1361 1362
	.pg_read_ops		= &filelayout_pg_read_ops,
	.pg_write_ops		= &filelayout_pg_write_ops,
F
Fred Isaman 已提交
1363
	.get_ds_info		= &filelayout_get_ds_info,
1364 1365
	.mark_request_commit	= filelayout_mark_request_commit,
	.clear_request_commit	= filelayout_clear_request_commit,
F
Fred Isaman 已提交
1366
	.scan_commit_lists	= filelayout_scan_commit_lists,
1367
	.recover_commit_reqs	= filelayout_recover_commit_reqs,
1368
	.commit_pagelist	= filelayout_commit_pagelist,
A
Andy Adamson 已提交
1369
	.read_pagelist		= filelayout_read_pagelist,
1370
	.write_pagelist		= filelayout_write_pagelist,
1371
	.free_deviceid_node	= filelayout_free_deveiceid_node,
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
};

static int __init nfs4filelayout_init(void)
{
	printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
	       __func__);
	return pnfs_register_layoutdriver(&filelayout_type);
}

static void __exit nfs4filelayout_exit(void)
{
	printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
	       __func__);
	pnfs_unregister_layoutdriver(&filelayout_type);
}

1388 1389
MODULE_ALIAS("nfs-layouttype4-1");

1390 1391
module_init(nfs4filelayout_init);
module_exit(nfs4filelayout_exit);