1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Module for pnfs flexfile layout driver.
4  *
5  * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6  *
7  * Tao Peng <bergwolf@primarydata.com>
8  */
9 
10 #include <linux/nfs_fs.h>
11 #include <linux/nfs_mount.h>
12 #include <linux/nfs_page.h>
13 #include <linux/module.h>
14 #include <linux/sched/mm.h>
15 
16 #include <linux/sunrpc/metrics.h>
17 
18 #include "flexfilelayout.h"
19 #include "../nfs4session.h"
20 #include "../nfs4idmap.h"
21 #include "../internal.h"
22 #include "../delegation.h"
23 #include "../nfs4trace.h"
24 #include "../iostat.h"
25 #include "../nfs.h"
26 #include "../nfs42.h"
27 
28 #define NFSDBG_FACILITY         NFSDBG_PNFS_LD
29 
30 #define FF_LAYOUT_POLL_RETRY_MAX     (15*HZ)
31 #define FF_LAYOUTRETURN_MAXERR 20
32 
33 static unsigned short io_maxretrans;
34 
35 static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
36 		struct nfs_pgio_header *hdr);
37 static int ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr *lo,
38 			       struct nfs42_layoutstat_devinfo *devinfo,
39 			       int dev_limit);
40 static void ff_layout_encode_ff_layoutupdate(struct xdr_stream *xdr,
41 			      const struct nfs42_layoutstat_devinfo *devinfo,
42 			      struct nfs4_ff_layout_mirror *mirror);
43 
44 static struct pnfs_layout_hdr *
45 ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
46 {
47 	struct nfs4_flexfile_layout *ffl;
48 
49 	ffl = kzalloc(sizeof(*ffl), gfp_flags);
50 	if (ffl) {
51 		INIT_LIST_HEAD(&ffl->error_list);
52 		INIT_LIST_HEAD(&ffl->mirrors);
53 		ffl->last_report_time = ktime_get();
54 		return &ffl->generic_hdr;
55 	} else
56 		return NULL;
57 }
58 
59 static void
60 ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
61 {
62 	struct nfs4_ff_layout_ds_err *err, *n;
63 
64 	list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
65 				 list) {
66 		list_del(&err->list);
67 		kfree(err);
68 	}
69 	kfree(FF_LAYOUT_FROM_HDR(lo));
70 }
71 
72 static int decode_pnfs_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
73 {
74 	__be32 *p;
75 
76 	p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
77 	if (unlikely(p == NULL))
78 		return -ENOBUFS;
79 	stateid->type = NFS4_PNFS_DS_STATEID_TYPE;
80 	memcpy(stateid->data, p, NFS4_STATEID_SIZE);
81 	dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
82 		p[0], p[1], p[2], p[3]);
83 	return 0;
84 }
85 
86 static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
87 {
88 	__be32 *p;
89 
90 	p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
91 	if (unlikely(!p))
92 		return -ENOBUFS;
93 	memcpy(devid, p, NFS4_DEVICEID4_SIZE);
94 	nfs4_print_deviceid(devid);
95 	return 0;
96 }
97 
98 static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
99 {
100 	__be32 *p;
101 
102 	p = xdr_inline_decode(xdr, 4);
103 	if (unlikely(!p))
104 		return -ENOBUFS;
105 	fh->size = be32_to_cpup(p++);
106 	if (fh->size > sizeof(struct nfs_fh)) {
107 		printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
108 		       fh->size);
109 		return -EOVERFLOW;
110 	}
111 	/* fh.data */
112 	p = xdr_inline_decode(xdr, fh->size);
113 	if (unlikely(!p))
114 		return -ENOBUFS;
115 	memcpy(&fh->data, p, fh->size);
116 	dprintk("%s: fh len %d\n", __func__, fh->size);
117 
118 	return 0;
119 }
120 
121 /*
122  * Currently only stringified uids and gids are accepted.
123  * I.e., kerberos is not supported to the DSes, so no pricipals.
124  *
125  * That means that one common function will suffice, but when
126  * principals are added, this should be split to accomodate
127  * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
128  */
129 static int
130 decode_name(struct xdr_stream *xdr, u32 *id)
131 {
132 	__be32 *p;
133 	int len;
134 
135 	/* opaque_length(4)*/
136 	p = xdr_inline_decode(xdr, 4);
137 	if (unlikely(!p))
138 		return -ENOBUFS;
139 	len = be32_to_cpup(p++);
140 	if (len < 0)
141 		return -EINVAL;
142 
143 	dprintk("%s: len %u\n", __func__, len);
144 
145 	/* opaque body */
146 	p = xdr_inline_decode(xdr, len);
147 	if (unlikely(!p))
148 		return -ENOBUFS;
149 
150 	if (!nfs_map_string_to_numeric((char *)p, len, id))
151 		return -EINVAL;
152 
153 	return 0;
154 }
155 
156 static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1,
157 		const struct nfs4_ff_layout_mirror *m2)
158 {
159 	int i, j;
160 
161 	if (m1->fh_versions_cnt != m2->fh_versions_cnt)
162 		return false;
163 	for (i = 0; i < m1->fh_versions_cnt; i++) {
164 		bool found_fh = false;
165 		for (j = 0; j < m2->fh_versions_cnt; j++) {
166 			if (nfs_compare_fh(&m1->fh_versions[i],
167 					&m2->fh_versions[j]) == 0) {
168 				found_fh = true;
169 				break;
170 			}
171 		}
172 		if (!found_fh)
173 			return false;
174 	}
175 	return true;
176 }
177 
178 static struct nfs4_ff_layout_mirror *
179 ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
180 		struct nfs4_ff_layout_mirror *mirror)
181 {
182 	struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
183 	struct nfs4_ff_layout_mirror *pos;
184 	struct inode *inode = lo->plh_inode;
185 
186 	spin_lock(&inode->i_lock);
187 	list_for_each_entry(pos, &ff_layout->mirrors, mirrors) {
188 		if (memcmp(&mirror->devid, &pos->devid, sizeof(pos->devid)) != 0)
189 			continue;
190 		if (!ff_mirror_match_fh(mirror, pos))
191 			continue;
192 		if (refcount_inc_not_zero(&pos->ref)) {
193 			spin_unlock(&inode->i_lock);
194 			return pos;
195 		}
196 	}
197 	list_add(&mirror->mirrors, &ff_layout->mirrors);
198 	mirror->layout = lo;
199 	spin_unlock(&inode->i_lock);
200 	return mirror;
201 }
202 
203 static void
204 ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
205 {
206 	struct inode *inode;
207 	if (mirror->layout == NULL)
208 		return;
209 	inode = mirror->layout->plh_inode;
210 	spin_lock(&inode->i_lock);
211 	list_del(&mirror->mirrors);
212 	spin_unlock(&inode->i_lock);
213 	mirror->layout = NULL;
214 }
215 
216 static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
217 {
218 	struct nfs4_ff_layout_mirror *mirror;
219 
220 	mirror = kzalloc(sizeof(*mirror), gfp_flags);
221 	if (mirror != NULL) {
222 		spin_lock_init(&mirror->lock);
223 		refcount_set(&mirror->ref, 1);
224 		INIT_LIST_HEAD(&mirror->mirrors);
225 	}
226 	return mirror;
227 }
228 
229 static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
230 {
231 	const struct cred	*cred;
232 
233 	ff_layout_remove_mirror(mirror);
234 	kfree(mirror->fh_versions);
235 	cred = rcu_access_pointer(mirror->ro_cred);
236 	put_cred(cred);
237 	cred = rcu_access_pointer(mirror->rw_cred);
238 	put_cred(cred);
239 	nfs4_ff_layout_put_deviceid(mirror->mirror_ds);
240 	kfree(mirror);
241 }
242 
243 static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
244 {
245 	if (mirror != NULL && refcount_dec_and_test(&mirror->ref))
246 		ff_layout_free_mirror(mirror);
247 }
248 
249 static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
250 {
251 	int i;
252 
253 	if (fls->mirror_array) {
254 		for (i = 0; i < fls->mirror_array_cnt; i++) {
255 			/* normally mirror_ds is freed in
256 			 * .free_deviceid_node but we still do it here
257 			 * for .alloc_lseg error path */
258 			ff_layout_put_mirror(fls->mirror_array[i]);
259 		}
260 		kfree(fls->mirror_array);
261 		fls->mirror_array = NULL;
262 	}
263 }
264 
265 static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
266 {
267 	int ret = 0;
268 
269 	dprintk("--> %s\n", __func__);
270 
271 	/* FIXME: remove this check when layout segment support is added */
272 	if (lgr->range.offset != 0 ||
273 	    lgr->range.length != NFS4_MAX_UINT64) {
274 		dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
275 			__func__);
276 		ret = -EINVAL;
277 	}
278 
279 	dprintk("--> %s returns %d\n", __func__, ret);
280 	return ret;
281 }
282 
283 static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
284 {
285 	if (fls) {
286 		ff_layout_free_mirror_array(fls);
287 		kfree(fls);
288 	}
289 }
290 
291 static bool
292 ff_lseg_range_is_after(const struct pnfs_layout_range *l1,
293 		const struct pnfs_layout_range *l2)
294 {
295 	u64 end1, end2;
296 
297 	if (l1->iomode != l2->iomode)
298 		return l1->iomode != IOMODE_READ;
299 	end1 = pnfs_calc_offset_end(l1->offset, l1->length);
300 	end2 = pnfs_calc_offset_end(l2->offset, l2->length);
301 	if (end1 < l2->offset)
302 		return false;
303 	if (end2 < l1->offset)
304 		return true;
305 	return l2->offset <= l1->offset;
306 }
307 
308 static bool
309 ff_lseg_merge(struct pnfs_layout_segment *new,
310 		struct pnfs_layout_segment *old)
311 {
312 	u64 new_end, old_end;
313 
314 	if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
315 		return false;
316 	if (new->pls_range.iomode != old->pls_range.iomode)
317 		return false;
318 	old_end = pnfs_calc_offset_end(old->pls_range.offset,
319 			old->pls_range.length);
320 	if (old_end < new->pls_range.offset)
321 		return false;
322 	new_end = pnfs_calc_offset_end(new->pls_range.offset,
323 			new->pls_range.length);
324 	if (new_end < old->pls_range.offset)
325 		return false;
326 
327 	/* Mergeable: copy info from 'old' to 'new' */
328 	if (new_end < old_end)
329 		new_end = old_end;
330 	if (new->pls_range.offset < old->pls_range.offset)
331 		new->pls_range.offset = old->pls_range.offset;
332 	new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
333 			new_end);
334 	if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
335 		set_bit(NFS_LSEG_ROC, &new->pls_flags);
336 	return true;
337 }
338 
339 static void
340 ff_layout_add_lseg(struct pnfs_layout_hdr *lo,
341 		struct pnfs_layout_segment *lseg,
342 		struct list_head *free_me)
343 {
344 	pnfs_generic_layout_insert_lseg(lo, lseg,
345 			ff_lseg_range_is_after,
346 			ff_lseg_merge,
347 			free_me);
348 }
349 
350 static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
351 {
352 	int i, j;
353 
354 	for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
355 		for (j = i + 1; j < fls->mirror_array_cnt; j++)
356 			if (fls->mirror_array[i]->efficiency <
357 			    fls->mirror_array[j]->efficiency)
358 				swap(fls->mirror_array[i],
359 				     fls->mirror_array[j]);
360 	}
361 }
362 
363 static struct pnfs_layout_segment *
364 ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
365 		     struct nfs4_layoutget_res *lgr,
366 		     gfp_t gfp_flags)
367 {
368 	struct pnfs_layout_segment *ret;
369 	struct nfs4_ff_layout_segment *fls = NULL;
370 	struct xdr_stream stream;
371 	struct xdr_buf buf;
372 	struct page *scratch;
373 	u64 stripe_unit;
374 	u32 mirror_array_cnt;
375 	__be32 *p;
376 	int i, rc;
377 
378 	dprintk("--> %s\n", __func__);
379 	scratch = alloc_page(gfp_flags);
380 	if (!scratch)
381 		return ERR_PTR(-ENOMEM);
382 
383 	xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
384 			      lgr->layoutp->len);
385 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
386 
387 	/* stripe unit and mirror_array_cnt */
388 	rc = -EIO;
389 	p = xdr_inline_decode(&stream, 8 + 4);
390 	if (!p)
391 		goto out_err_free;
392 
393 	p = xdr_decode_hyper(p, &stripe_unit);
394 	mirror_array_cnt = be32_to_cpup(p++);
395 	dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
396 		stripe_unit, mirror_array_cnt);
397 
398 	if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
399 	    mirror_array_cnt == 0)
400 		goto out_err_free;
401 
402 	rc = -ENOMEM;
403 	fls = kzalloc(sizeof(*fls), gfp_flags);
404 	if (!fls)
405 		goto out_err_free;
406 
407 	fls->mirror_array_cnt = mirror_array_cnt;
408 	fls->stripe_unit = stripe_unit;
409 	fls->mirror_array = kcalloc(fls->mirror_array_cnt,
410 				    sizeof(fls->mirror_array[0]), gfp_flags);
411 	if (fls->mirror_array == NULL)
412 		goto out_err_free;
413 
414 	for (i = 0; i < fls->mirror_array_cnt; i++) {
415 		struct nfs4_ff_layout_mirror *mirror;
416 		struct cred *kcred;
417 		const struct cred __rcu *cred;
418 		kuid_t uid;
419 		kgid_t gid;
420 		u32 ds_count, fh_count, id;
421 		int j;
422 
423 		rc = -EIO;
424 		p = xdr_inline_decode(&stream, 4);
425 		if (!p)
426 			goto out_err_free;
427 		ds_count = be32_to_cpup(p);
428 
429 		/* FIXME: allow for striping? */
430 		if (ds_count != 1)
431 			goto out_err_free;
432 
433 		fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
434 		if (fls->mirror_array[i] == NULL) {
435 			rc = -ENOMEM;
436 			goto out_err_free;
437 		}
438 
439 		fls->mirror_array[i]->ds_count = ds_count;
440 
441 		/* deviceid */
442 		rc = decode_deviceid(&stream, &fls->mirror_array[i]->devid);
443 		if (rc)
444 			goto out_err_free;
445 
446 		/* efficiency */
447 		rc = -EIO;
448 		p = xdr_inline_decode(&stream, 4);
449 		if (!p)
450 			goto out_err_free;
451 		fls->mirror_array[i]->efficiency = be32_to_cpup(p);
452 
453 		/* stateid */
454 		rc = decode_pnfs_stateid(&stream, &fls->mirror_array[i]->stateid);
455 		if (rc)
456 			goto out_err_free;
457 
458 		/* fh */
459 		rc = -EIO;
460 		p = xdr_inline_decode(&stream, 4);
461 		if (!p)
462 			goto out_err_free;
463 		fh_count = be32_to_cpup(p);
464 
465 		fls->mirror_array[i]->fh_versions =
466 			kcalloc(fh_count, sizeof(struct nfs_fh),
467 				gfp_flags);
468 		if (fls->mirror_array[i]->fh_versions == NULL) {
469 			rc = -ENOMEM;
470 			goto out_err_free;
471 		}
472 
473 		for (j = 0; j < fh_count; j++) {
474 			rc = decode_nfs_fh(&stream,
475 					   &fls->mirror_array[i]->fh_versions[j]);
476 			if (rc)
477 				goto out_err_free;
478 		}
479 
480 		fls->mirror_array[i]->fh_versions_cnt = fh_count;
481 
482 		/* user */
483 		rc = decode_name(&stream, &id);
484 		if (rc)
485 			goto out_err_free;
486 
487 		uid = make_kuid(&init_user_ns, id);
488 
489 		/* group */
490 		rc = decode_name(&stream, &id);
491 		if (rc)
492 			goto out_err_free;
493 
494 		gid = make_kgid(&init_user_ns, id);
495 
496 		if (gfp_flags & __GFP_FS)
497 			kcred = prepare_kernel_cred(NULL);
498 		else {
499 			unsigned int nofs_flags = memalloc_nofs_save();
500 			kcred = prepare_kernel_cred(NULL);
501 			memalloc_nofs_restore(nofs_flags);
502 		}
503 		rc = -ENOMEM;
504 		if (!kcred)
505 			goto out_err_free;
506 		kcred->fsuid = uid;
507 		kcred->fsgid = gid;
508 		cred = RCU_INITIALIZER(kcred);
509 
510 		if (lgr->range.iomode == IOMODE_READ)
511 			rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
512 		else
513 			rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
514 
515 		mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
516 		if (mirror != fls->mirror_array[i]) {
517 			/* swap cred ptrs so free_mirror will clean up old */
518 			if (lgr->range.iomode == IOMODE_READ) {
519 				cred = xchg(&mirror->ro_cred, cred);
520 				rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
521 			} else {
522 				cred = xchg(&mirror->rw_cred, cred);
523 				rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
524 			}
525 			ff_layout_free_mirror(fls->mirror_array[i]);
526 			fls->mirror_array[i] = mirror;
527 		}
528 
529 		dprintk("%s: iomode %s uid %u gid %u\n", __func__,
530 			lgr->range.iomode == IOMODE_READ ? "READ" : "RW",
531 			from_kuid(&init_user_ns, uid),
532 			from_kgid(&init_user_ns, gid));
533 	}
534 
535 	p = xdr_inline_decode(&stream, 4);
536 	if (!p)
537 		goto out_sort_mirrors;
538 	fls->flags = be32_to_cpup(p);
539 
540 	p = xdr_inline_decode(&stream, 4);
541 	if (!p)
542 		goto out_sort_mirrors;
543 	for (i=0; i < fls->mirror_array_cnt; i++)
544 		fls->mirror_array[i]->report_interval = be32_to_cpup(p);
545 
546 out_sort_mirrors:
547 	ff_layout_sort_mirrors(fls);
548 	rc = ff_layout_check_layout(lgr);
549 	if (rc)
550 		goto out_err_free;
551 	ret = &fls->generic_hdr;
552 	dprintk("<-- %s (success)\n", __func__);
553 out_free_page:
554 	__free_page(scratch);
555 	return ret;
556 out_err_free:
557 	_ff_layout_free_lseg(fls);
558 	ret = ERR_PTR(rc);
559 	dprintk("<-- %s (%d)\n", __func__, rc);
560 	goto out_free_page;
561 }
562 
563 static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
564 {
565 	struct pnfs_layout_segment *lseg;
566 
567 	list_for_each_entry(lseg, &layout->plh_segs, pls_list)
568 		if (lseg->pls_range.iomode == IOMODE_RW)
569 			return true;
570 
571 	return false;
572 }
573 
574 static void
575 ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
576 {
577 	struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
578 
579 	dprintk("--> %s\n", __func__);
580 
581 	if (lseg->pls_range.iomode == IOMODE_RW) {
582 		struct nfs4_flexfile_layout *ffl;
583 		struct inode *inode;
584 
585 		ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
586 		inode = ffl->generic_hdr.plh_inode;
587 		spin_lock(&inode->i_lock);
588 		if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
589 			ffl->commit_info.nbuckets = 0;
590 			kfree(ffl->commit_info.buckets);
591 			ffl->commit_info.buckets = NULL;
592 		}
593 		spin_unlock(&inode->i_lock);
594 	}
595 	_ff_layout_free_lseg(fls);
596 }
597 
598 /* Return 1 until we have multiple lsegs support */
599 static int
600 ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
601 {
602 	return 1;
603 }
604 
605 static void
606 nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
607 {
608 	/* first IO request? */
609 	if (atomic_inc_return(&timer->n_ops) == 1) {
610 		timer->start_time = now;
611 	}
612 }
613 
614 static ktime_t
615 nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
616 {
617 	ktime_t start;
618 
619 	if (atomic_dec_return(&timer->n_ops) < 0)
620 		WARN_ON_ONCE(1);
621 
622 	start = timer->start_time;
623 	timer->start_time = now;
624 	return ktime_sub(now, start);
625 }
626 
627 static bool
628 nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
629 			    struct nfs4_ff_layoutstat *layoutstat,
630 			    ktime_t now)
631 {
632 	s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
633 	struct nfs4_flexfile_layout *ffl = FF_LAYOUT_FROM_HDR(mirror->layout);
634 
635 	nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
636 	if (!mirror->start_time)
637 		mirror->start_time = now;
638 	if (mirror->report_interval != 0)
639 		report_interval = (s64)mirror->report_interval * 1000LL;
640 	else if (layoutstats_timer != 0)
641 		report_interval = (s64)layoutstats_timer * 1000LL;
642 	if (ktime_to_ms(ktime_sub(now, ffl->last_report_time)) >=
643 			report_interval) {
644 		ffl->last_report_time = now;
645 		return true;
646 	}
647 
648 	return false;
649 }
650 
651 static void
652 nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
653 		__u64 requested)
654 {
655 	struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
656 
657 	iostat->ops_requested++;
658 	iostat->bytes_requested += requested;
659 }
660 
661 static void
662 nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
663 		__u64 requested,
664 		__u64 completed,
665 		ktime_t time_completed,
666 		ktime_t time_started)
667 {
668 	struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
669 	ktime_t completion_time = ktime_sub(time_completed, time_started);
670 	ktime_t timer;
671 
672 	iostat->ops_completed++;
673 	iostat->bytes_completed += completed;
674 	iostat->bytes_not_delivered += requested - completed;
675 
676 	timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
677 	iostat->total_busy_time =
678 			ktime_add(iostat->total_busy_time, timer);
679 	iostat->aggregate_completion_time =
680 			ktime_add(iostat->aggregate_completion_time,
681 					completion_time);
682 }
683 
684 static void
685 nfs4_ff_layout_stat_io_start_read(struct inode *inode,
686 		struct nfs4_ff_layout_mirror *mirror,
687 		__u64 requested, ktime_t now)
688 {
689 	bool report;
690 
691 	spin_lock(&mirror->lock);
692 	report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
693 	nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
694 	set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags);
695 	spin_unlock(&mirror->lock);
696 
697 	if (report)
698 		pnfs_report_layoutstat(inode, GFP_KERNEL);
699 }
700 
701 static void
702 nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
703 		struct nfs4_ff_layout_mirror *mirror,
704 		__u64 requested,
705 		__u64 completed)
706 {
707 	spin_lock(&mirror->lock);
708 	nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
709 			requested, completed,
710 			ktime_get(), task->tk_start);
711 	set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags);
712 	spin_unlock(&mirror->lock);
713 }
714 
715 static void
716 nfs4_ff_layout_stat_io_start_write(struct inode *inode,
717 		struct nfs4_ff_layout_mirror *mirror,
718 		__u64 requested, ktime_t now)
719 {
720 	bool report;
721 
722 	spin_lock(&mirror->lock);
723 	report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
724 	nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
725 	set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags);
726 	spin_unlock(&mirror->lock);
727 
728 	if (report)
729 		pnfs_report_layoutstat(inode, GFP_NOIO);
730 }
731 
732 static void
733 nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
734 		struct nfs4_ff_layout_mirror *mirror,
735 		__u64 requested,
736 		__u64 completed,
737 		enum nfs3_stable_how committed)
738 {
739 	if (committed == NFS_UNSTABLE)
740 		requested = completed = 0;
741 
742 	spin_lock(&mirror->lock);
743 	nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
744 			requested, completed, ktime_get(), task->tk_start);
745 	set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags);
746 	spin_unlock(&mirror->lock);
747 }
748 
749 static int
750 ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
751 			    struct nfs_commit_info *cinfo,
752 			    gfp_t gfp_flags)
753 {
754 	struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
755 	struct pnfs_commit_bucket *buckets;
756 	int size;
757 
758 	if (cinfo->ds->nbuckets != 0) {
759 		/* This assumes there is only one RW lseg per file.
760 		 * To support multiple lseg per file, we need to
761 		 * change struct pnfs_commit_bucket to allow dynamic
762 		 * increasing nbuckets.
763 		 */
764 		return 0;
765 	}
766 
767 	size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
768 
769 	buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
770 			  gfp_flags);
771 	if (!buckets)
772 		return -ENOMEM;
773 	else {
774 		int i;
775 
776 		spin_lock(&cinfo->inode->i_lock);
777 		if (cinfo->ds->nbuckets != 0)
778 			kfree(buckets);
779 		else {
780 			cinfo->ds->buckets = buckets;
781 			cinfo->ds->nbuckets = size;
782 			for (i = 0; i < size; i++) {
783 				INIT_LIST_HEAD(&buckets[i].written);
784 				INIT_LIST_HEAD(&buckets[i].committing);
785 				/* mark direct verifier as unset */
786 				buckets[i].direct_verf.committed =
787 					NFS_INVALID_STABLE_HOW;
788 			}
789 		}
790 		spin_unlock(&cinfo->inode->i_lock);
791 		return 0;
792 	}
793 }
794 
795 static void
796 ff_layout_mark_ds_unreachable(struct pnfs_layout_segment *lseg, int idx)
797 {
798 	struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
799 
800 	if (devid)
801 		nfs4_mark_deviceid_unavailable(devid);
802 }
803 
804 static void
805 ff_layout_mark_ds_reachable(struct pnfs_layout_segment *lseg, int idx)
806 {
807 	struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
808 
809 	if (devid)
810 		nfs4_mark_deviceid_available(devid);
811 }
812 
813 static struct nfs4_pnfs_ds *
814 ff_layout_choose_ds_for_read(struct pnfs_layout_segment *lseg,
815 			     int start_idx, int *best_idx,
816 			     bool check_device)
817 {
818 	struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
819 	struct nfs4_ff_layout_mirror *mirror;
820 	struct nfs4_pnfs_ds *ds;
821 	bool fail_return = false;
822 	int idx;
823 
824 	/* mirrors are initially sorted by efficiency */
825 	for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
826 		if (idx+1 == fls->mirror_array_cnt)
827 			fail_return = !check_device;
828 
829 		mirror = FF_LAYOUT_COMP(lseg, idx);
830 		ds = nfs4_ff_layout_prepare_ds(lseg, mirror, fail_return);
831 		if (!ds)
832 			continue;
833 
834 		if (check_device &&
835 		    nfs4_test_deviceid_unavailable(&mirror->mirror_ds->id_node))
836 			continue;
837 
838 		*best_idx = idx;
839 		return ds;
840 	}
841 
842 	return NULL;
843 }
844 
845 static struct nfs4_pnfs_ds *
846 ff_layout_choose_any_ds_for_read(struct pnfs_layout_segment *lseg,
847 				 int start_idx, int *best_idx)
848 {
849 	return ff_layout_choose_ds_for_read(lseg, start_idx, best_idx, false);
850 }
851 
852 static struct nfs4_pnfs_ds *
853 ff_layout_choose_valid_ds_for_read(struct pnfs_layout_segment *lseg,
854 				   int start_idx, int *best_idx)
855 {
856 	return ff_layout_choose_ds_for_read(lseg, start_idx, best_idx, true);
857 }
858 
859 static struct nfs4_pnfs_ds *
860 ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
861 				  int start_idx, int *best_idx)
862 {
863 	struct nfs4_pnfs_ds *ds;
864 
865 	ds = ff_layout_choose_valid_ds_for_read(lseg, start_idx, best_idx);
866 	if (ds)
867 		return ds;
868 	return ff_layout_choose_any_ds_for_read(lseg, start_idx, best_idx);
869 }
870 
871 static void
872 ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio,
873 		      struct nfs_page *req,
874 		      bool strict_iomode)
875 {
876 	pnfs_put_lseg(pgio->pg_lseg);
877 	pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
878 					   nfs_req_openctx(req),
879 					   0,
880 					   NFS4_MAX_UINT64,
881 					   IOMODE_READ,
882 					   strict_iomode,
883 					   GFP_KERNEL);
884 	if (IS_ERR(pgio->pg_lseg)) {
885 		pgio->pg_error = PTR_ERR(pgio->pg_lseg);
886 		pgio->pg_lseg = NULL;
887 	}
888 }
889 
890 static void
891 ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
892 			struct nfs_page *req)
893 {
894 	struct nfs_pgio_mirror *pgm;
895 	struct nfs4_ff_layout_mirror *mirror;
896 	struct nfs4_pnfs_ds *ds;
897 	int ds_idx;
898 
899 retry:
900 	pnfs_generic_pg_check_layout(pgio);
901 	/* Use full layout for now */
902 	if (!pgio->pg_lseg) {
903 		ff_layout_pg_get_read(pgio, req, false);
904 		if (!pgio->pg_lseg)
905 			goto out_nolseg;
906 	}
907 	if (ff_layout_avoid_read_on_rw(pgio->pg_lseg)) {
908 		ff_layout_pg_get_read(pgio, req, true);
909 		if (!pgio->pg_lseg)
910 			goto out_nolseg;
911 	}
912 
913 	ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
914 	if (!ds) {
915 		if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg))
916 			goto out_mds;
917 		pnfs_put_lseg(pgio->pg_lseg);
918 		pgio->pg_lseg = NULL;
919 		/* Sleep for 1 second before retrying */
920 		ssleep(1);
921 		goto retry;
922 	}
923 
924 	mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
925 
926 	pgio->pg_mirror_idx = ds_idx;
927 
928 	/* read always uses only one mirror - idx 0 for pgio layer */
929 	pgm = &pgio->pg_mirrors[0];
930 	pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
931 
932 	if (NFS_SERVER(pgio->pg_inode)->flags &
933 			(NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR))
934 		pgio->pg_maxretrans = io_maxretrans;
935 	return;
936 out_nolseg:
937 	if (pgio->pg_error < 0)
938 		return;
939 out_mds:
940 	trace_pnfs_mds_fallback_pg_init_read(pgio->pg_inode,
941 			0, NFS4_MAX_UINT64, IOMODE_READ,
942 			NFS_I(pgio->pg_inode)->layout,
943 			pgio->pg_lseg);
944 	pnfs_put_lseg(pgio->pg_lseg);
945 	pgio->pg_lseg = NULL;
946 	pgio->pg_maxretrans = 0;
947 	nfs_pageio_reset_read_mds(pgio);
948 }
949 
950 static void
951 ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
952 			struct nfs_page *req)
953 {
954 	struct nfs4_ff_layout_mirror *mirror;
955 	struct nfs_pgio_mirror *pgm;
956 	struct nfs_commit_info cinfo;
957 	struct nfs4_pnfs_ds *ds;
958 	int i;
959 	int status;
960 
961 retry:
962 	pnfs_generic_pg_check_layout(pgio);
963 	if (!pgio->pg_lseg) {
964 		pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
965 						   nfs_req_openctx(req),
966 						   0,
967 						   NFS4_MAX_UINT64,
968 						   IOMODE_RW,
969 						   false,
970 						   GFP_NOFS);
971 		if (IS_ERR(pgio->pg_lseg)) {
972 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
973 			pgio->pg_lseg = NULL;
974 			return;
975 		}
976 	}
977 	/* If no lseg, fall back to write through mds */
978 	if (pgio->pg_lseg == NULL)
979 		goto out_mds;
980 
981 	nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
982 	status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
983 	if (status < 0)
984 		goto out_mds;
985 
986 	/* Use a direct mapping of ds_idx to pgio mirror_idx */
987 	if (WARN_ON_ONCE(pgio->pg_mirror_count !=
988 	    FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
989 		goto out_mds;
990 
991 	for (i = 0; i < pgio->pg_mirror_count; i++) {
992 		mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
993 		ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, mirror, true);
994 		if (!ds) {
995 			if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg))
996 				goto out_mds;
997 			pnfs_put_lseg(pgio->pg_lseg);
998 			pgio->pg_lseg = NULL;
999 			/* Sleep for 1 second before retrying */
1000 			ssleep(1);
1001 			goto retry;
1002 		}
1003 		pgm = &pgio->pg_mirrors[i];
1004 		pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
1005 	}
1006 
1007 	if (NFS_SERVER(pgio->pg_inode)->flags &
1008 			(NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR))
1009 		pgio->pg_maxretrans = io_maxretrans;
1010 	return;
1011 
1012 out_mds:
1013 	trace_pnfs_mds_fallback_pg_init_write(pgio->pg_inode,
1014 			0, NFS4_MAX_UINT64, IOMODE_RW,
1015 			NFS_I(pgio->pg_inode)->layout,
1016 			pgio->pg_lseg);
1017 	pnfs_put_lseg(pgio->pg_lseg);
1018 	pgio->pg_lseg = NULL;
1019 	pgio->pg_maxretrans = 0;
1020 	nfs_pageio_reset_write_mds(pgio);
1021 }
1022 
1023 static unsigned int
1024 ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
1025 				    struct nfs_page *req)
1026 {
1027 	if (!pgio->pg_lseg) {
1028 		pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1029 						   nfs_req_openctx(req),
1030 						   0,
1031 						   NFS4_MAX_UINT64,
1032 						   IOMODE_RW,
1033 						   false,
1034 						   GFP_NOFS);
1035 		if (IS_ERR(pgio->pg_lseg)) {
1036 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
1037 			pgio->pg_lseg = NULL;
1038 			goto out;
1039 		}
1040 	}
1041 	if (pgio->pg_lseg)
1042 		return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
1043 
1044 	trace_pnfs_mds_fallback_pg_get_mirror_count(pgio->pg_inode,
1045 			0, NFS4_MAX_UINT64, IOMODE_RW,
1046 			NFS_I(pgio->pg_inode)->layout,
1047 			pgio->pg_lseg);
1048 	/* no lseg means that pnfs is not in use, so no mirroring here */
1049 	nfs_pageio_reset_write_mds(pgio);
1050 out:
1051 	return 1;
1052 }
1053 
1054 static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
1055 	.pg_init = ff_layout_pg_init_read,
1056 	.pg_test = pnfs_generic_pg_test,
1057 	.pg_doio = pnfs_generic_pg_readpages,
1058 	.pg_cleanup = pnfs_generic_pg_cleanup,
1059 };
1060 
1061 static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
1062 	.pg_init = ff_layout_pg_init_write,
1063 	.pg_test = pnfs_generic_pg_test,
1064 	.pg_doio = pnfs_generic_pg_writepages,
1065 	.pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
1066 	.pg_cleanup = pnfs_generic_pg_cleanup,
1067 };
1068 
1069 static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
1070 {
1071 	struct rpc_task *task = &hdr->task;
1072 
1073 	pnfs_layoutcommit_inode(hdr->inode, false);
1074 
1075 	if (retry_pnfs) {
1076 		dprintk("%s Reset task %5u for i/o through pNFS "
1077 			"(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1078 			hdr->task.tk_pid,
1079 			hdr->inode->i_sb->s_id,
1080 			(unsigned long long)NFS_FILEID(hdr->inode),
1081 			hdr->args.count,
1082 			(unsigned long long)hdr->args.offset);
1083 
1084 		hdr->completion_ops->reschedule_io(hdr);
1085 		return;
1086 	}
1087 
1088 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1089 		dprintk("%s Reset task %5u for i/o through MDS "
1090 			"(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1091 			hdr->task.tk_pid,
1092 			hdr->inode->i_sb->s_id,
1093 			(unsigned long long)NFS_FILEID(hdr->inode),
1094 			hdr->args.count,
1095 			(unsigned long long)hdr->args.offset);
1096 
1097 		trace_pnfs_mds_fallback_write_done(hdr->inode,
1098 				hdr->args.offset, hdr->args.count,
1099 				IOMODE_RW, NFS_I(hdr->inode)->layout,
1100 				hdr->lseg);
1101 		task->tk_status = pnfs_write_done_resend_to_mds(hdr);
1102 	}
1103 }
1104 
1105 static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
1106 {
1107 	struct rpc_task *task = &hdr->task;
1108 
1109 	pnfs_layoutcommit_inode(hdr->inode, false);
1110 
1111 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1112 		dprintk("%s Reset task %5u for i/o through MDS "
1113 			"(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1114 			hdr->task.tk_pid,
1115 			hdr->inode->i_sb->s_id,
1116 			(unsigned long long)NFS_FILEID(hdr->inode),
1117 			hdr->args.count,
1118 			(unsigned long long)hdr->args.offset);
1119 
1120 		trace_pnfs_mds_fallback_read_done(hdr->inode,
1121 				hdr->args.offset, hdr->args.count,
1122 				IOMODE_READ, NFS_I(hdr->inode)->layout,
1123 				hdr->lseg);
1124 		task->tk_status = pnfs_read_done_resend_to_mds(hdr);
1125 	}
1126 }
1127 
1128 static int ff_layout_async_handle_error_v4(struct rpc_task *task,
1129 					   struct nfs4_state *state,
1130 					   struct nfs_client *clp,
1131 					   struct pnfs_layout_segment *lseg,
1132 					   int idx)
1133 {
1134 	struct pnfs_layout_hdr *lo = lseg->pls_layout;
1135 	struct inode *inode = lo->plh_inode;
1136 	struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1137 	struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1138 
1139 	switch (task->tk_status) {
1140 	case -NFS4ERR_BADSESSION:
1141 	case -NFS4ERR_BADSLOT:
1142 	case -NFS4ERR_BAD_HIGH_SLOT:
1143 	case -NFS4ERR_DEADSESSION:
1144 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1145 	case -NFS4ERR_SEQ_FALSE_RETRY:
1146 	case -NFS4ERR_SEQ_MISORDERED:
1147 		dprintk("%s ERROR %d, Reset session. Exchangeid "
1148 			"flags 0x%x\n", __func__, task->tk_status,
1149 			clp->cl_exchange_flags);
1150 		nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1151 		break;
1152 	case -NFS4ERR_DELAY:
1153 	case -NFS4ERR_GRACE:
1154 		rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1155 		break;
1156 	case -NFS4ERR_RETRY_UNCACHED_REP:
1157 		break;
1158 	/* Invalidate Layout errors */
1159 	case -NFS4ERR_PNFS_NO_LAYOUT:
1160 	case -ESTALE:           /* mapped NFS4ERR_STALE */
1161 	case -EBADHANDLE:       /* mapped NFS4ERR_BADHANDLE */
1162 	case -EISDIR:           /* mapped NFS4ERR_ISDIR */
1163 	case -NFS4ERR_FHEXPIRED:
1164 	case -NFS4ERR_WRONG_TYPE:
1165 		dprintk("%s Invalid layout error %d\n", __func__,
1166 			task->tk_status);
1167 		/*
1168 		 * Destroy layout so new i/o will get a new layout.
1169 		 * Layout will not be destroyed until all current lseg
1170 		 * references are put. Mark layout as invalid to resend failed
1171 		 * i/o and all i/o waiting on the slot table to the MDS until
1172 		 * layout is destroyed and a new valid layout is obtained.
1173 		 */
1174 		pnfs_destroy_layout(NFS_I(inode));
1175 		rpc_wake_up(&tbl->slot_tbl_waitq);
1176 		goto reset;
1177 	/* RPC connection errors */
1178 	case -ECONNREFUSED:
1179 	case -EHOSTDOWN:
1180 	case -EHOSTUNREACH:
1181 	case -ENETUNREACH:
1182 	case -EIO:
1183 	case -ETIMEDOUT:
1184 	case -EPIPE:
1185 		dprintk("%s DS connection error %d\n", __func__,
1186 			task->tk_status);
1187 		nfs4_delete_deviceid(devid->ld, devid->nfs_client,
1188 				&devid->deviceid);
1189 		rpc_wake_up(&tbl->slot_tbl_waitq);
1190 		/* fall through */
1191 	default:
1192 		if (ff_layout_avoid_mds_available_ds(lseg))
1193 			return -NFS4ERR_RESET_TO_PNFS;
1194 reset:
1195 		dprintk("%s Retry through MDS. Error %d\n", __func__,
1196 			task->tk_status);
1197 		return -NFS4ERR_RESET_TO_MDS;
1198 	}
1199 	task->tk_status = 0;
1200 	return -EAGAIN;
1201 }
1202 
1203 /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1204 static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1205 					   struct pnfs_layout_segment *lseg,
1206 					   int idx)
1207 {
1208 	struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1209 
1210 	switch (task->tk_status) {
1211 	/* File access problems. Don't mark the device as unavailable */
1212 	case -EACCES:
1213 	case -ESTALE:
1214 	case -EISDIR:
1215 	case -EBADHANDLE:
1216 	case -ELOOP:
1217 	case -ENOSPC:
1218 		break;
1219 	case -EJUKEBOX:
1220 		nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1221 		goto out_retry;
1222 	default:
1223 		dprintk("%s DS connection error %d\n", __func__,
1224 			task->tk_status);
1225 		nfs4_delete_deviceid(devid->ld, devid->nfs_client,
1226 				&devid->deviceid);
1227 	}
1228 	/* FIXME: Need to prevent infinite looping here. */
1229 	return -NFS4ERR_RESET_TO_PNFS;
1230 out_retry:
1231 	task->tk_status = 0;
1232 	rpc_restart_call_prepare(task);
1233 	rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1234 	return -EAGAIN;
1235 }
1236 
1237 static int ff_layout_async_handle_error(struct rpc_task *task,
1238 					struct nfs4_state *state,
1239 					struct nfs_client *clp,
1240 					struct pnfs_layout_segment *lseg,
1241 					int idx)
1242 {
1243 	int vers = clp->cl_nfs_mod->rpc_vers->number;
1244 
1245 	if (task->tk_status >= 0) {
1246 		ff_layout_mark_ds_reachable(lseg, idx);
1247 		return 0;
1248 	}
1249 
1250 	/* Handle the case of an invalid layout segment */
1251 	if (!pnfs_is_valid_lseg(lseg))
1252 		return -NFS4ERR_RESET_TO_PNFS;
1253 
1254 	switch (vers) {
1255 	case 3:
1256 		return ff_layout_async_handle_error_v3(task, lseg, idx);
1257 	case 4:
1258 		return ff_layout_async_handle_error_v4(task, state, clp,
1259 						       lseg, idx);
1260 	default:
1261 		/* should never happen */
1262 		WARN_ON_ONCE(1);
1263 		return 0;
1264 	}
1265 }
1266 
1267 static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1268 					int idx, u64 offset, u64 length,
1269 					u32 *op_status, int opnum, int error)
1270 {
1271 	struct nfs4_ff_layout_mirror *mirror;
1272 	u32 status = *op_status;
1273 	int err;
1274 
1275 	if (status == 0) {
1276 		switch (error) {
1277 		case -ETIMEDOUT:
1278 		case -EPFNOSUPPORT:
1279 		case -EPROTONOSUPPORT:
1280 		case -EOPNOTSUPP:
1281 		case -ECONNREFUSED:
1282 		case -ECONNRESET:
1283 		case -EHOSTDOWN:
1284 		case -EHOSTUNREACH:
1285 		case -ENETUNREACH:
1286 		case -EADDRINUSE:
1287 		case -ENOBUFS:
1288 		case -EPIPE:
1289 		case -EPERM:
1290 			*op_status = status = NFS4ERR_NXIO;
1291 			break;
1292 		case -EACCES:
1293 			*op_status = status = NFS4ERR_ACCESS;
1294 			break;
1295 		default:
1296 			return;
1297 		}
1298 	}
1299 
1300 	switch (status) {
1301 	case NFS4ERR_DELAY:
1302 	case NFS4ERR_GRACE:
1303 		return;
1304 	default:
1305 		break;
1306 	}
1307 
1308 	mirror = FF_LAYOUT_COMP(lseg, idx);
1309 	err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1310 				       mirror, offset, length, status, opnum,
1311 				       GFP_NOIO);
1312 	if (status == NFS4ERR_NXIO)
1313 		ff_layout_mark_ds_unreachable(lseg, idx);
1314 	pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
1315 	dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1316 }
1317 
1318 /* NFS_PROTO call done callback routines */
1319 static int ff_layout_read_done_cb(struct rpc_task *task,
1320 				struct nfs_pgio_header *hdr)
1321 {
1322 	int new_idx = hdr->pgio_mirror_idx;
1323 	int err;
1324 
1325 	if (task->tk_status < 0) {
1326 		ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1327 					    hdr->args.offset, hdr->args.count,
1328 					    &hdr->res.op_status, OP_READ,
1329 					    task->tk_status);
1330 		trace_ff_layout_read_error(hdr);
1331 	}
1332 
1333 	err = ff_layout_async_handle_error(task, hdr->args.context->state,
1334 					   hdr->ds_clp, hdr->lseg,
1335 					   hdr->pgio_mirror_idx);
1336 
1337 	trace_nfs4_pnfs_read(hdr, err);
1338 	clear_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags);
1339 	clear_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags);
1340 	switch (err) {
1341 	case -NFS4ERR_RESET_TO_PNFS:
1342 		if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1343 					hdr->pgio_mirror_idx + 1,
1344 					&new_idx))
1345 			goto out_layouterror;
1346 		set_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags);
1347 		return task->tk_status;
1348 	case -NFS4ERR_RESET_TO_MDS:
1349 		set_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags);
1350 		return task->tk_status;
1351 	case -EAGAIN:
1352 		goto out_eagain;
1353 	}
1354 
1355 	return 0;
1356 out_layouterror:
1357 	ff_layout_read_record_layoutstats_done(task, hdr);
1358 	ff_layout_send_layouterror(hdr->lseg);
1359 	hdr->pgio_mirror_idx = new_idx;
1360 out_eagain:
1361 	rpc_restart_call_prepare(task);
1362 	return -EAGAIN;
1363 }
1364 
1365 static bool
1366 ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1367 {
1368 	return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1369 }
1370 
1371 /*
1372  * We reference the rpc_cred of the first WRITE that triggers the need for
1373  * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1374  * rfc5661 is not clear about which credential should be used.
1375  *
1376  * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1377  * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1378  * we always send layoutcommit after DS writes.
1379  */
1380 static void
1381 ff_layout_set_layoutcommit(struct inode *inode,
1382 		struct pnfs_layout_segment *lseg,
1383 		loff_t end_offset)
1384 {
1385 	if (!ff_layout_need_layoutcommit(lseg))
1386 		return;
1387 
1388 	pnfs_set_layoutcommit(inode, lseg, end_offset);
1389 	dprintk("%s inode %lu pls_end_pos %llu\n", __func__, inode->i_ino,
1390 		(unsigned long long) NFS_I(inode)->layout->plh_lwb);
1391 }
1392 
1393 static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1394 		struct nfs_pgio_header *hdr)
1395 {
1396 	if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1397 		return;
1398 	nfs4_ff_layout_stat_io_start_read(hdr->inode,
1399 			FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1400 			hdr->args.count,
1401 			task->tk_start);
1402 }
1403 
1404 static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1405 		struct nfs_pgio_header *hdr)
1406 {
1407 	if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1408 		return;
1409 	nfs4_ff_layout_stat_io_end_read(task,
1410 			FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1411 			hdr->args.count,
1412 			hdr->res.count);
1413 	set_bit(NFS_LSEG_LAYOUTRETURN, &hdr->lseg->pls_flags);
1414 }
1415 
1416 static int ff_layout_read_prepare_common(struct rpc_task *task,
1417 					 struct nfs_pgio_header *hdr)
1418 {
1419 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1420 		rpc_exit(task, -EIO);
1421 		return -EIO;
1422 	}
1423 
1424 	ff_layout_read_record_layoutstats_start(task, hdr);
1425 	return 0;
1426 }
1427 
1428 /*
1429  * Call ops for the async read/write cases
1430  * In the case of dense layouts, the offset needs to be reset to its
1431  * original value.
1432  */
1433 static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1434 {
1435 	struct nfs_pgio_header *hdr = data;
1436 
1437 	if (ff_layout_read_prepare_common(task, hdr))
1438 		return;
1439 
1440 	rpc_call_start(task);
1441 }
1442 
1443 static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1444 {
1445 	struct nfs_pgio_header *hdr = data;
1446 
1447 	if (nfs4_setup_sequence(hdr->ds_clp,
1448 				&hdr->args.seq_args,
1449 				&hdr->res.seq_res,
1450 				task))
1451 		return;
1452 
1453 	ff_layout_read_prepare_common(task, hdr);
1454 }
1455 
1456 static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1457 {
1458 	struct nfs_pgio_header *hdr = data;
1459 
1460 	dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1461 
1462 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1463 	    task->tk_status == 0) {
1464 		nfs4_sequence_done(task, &hdr->res.seq_res);
1465 		return;
1466 	}
1467 
1468 	/* Note this may cause RPC to be resent */
1469 	hdr->mds_ops->rpc_call_done(task, hdr);
1470 }
1471 
1472 static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1473 {
1474 	struct nfs_pgio_header *hdr = data;
1475 
1476 	ff_layout_read_record_layoutstats_done(task, hdr);
1477 	rpc_count_iostats_metrics(task,
1478 	    &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1479 }
1480 
1481 static void ff_layout_read_release(void *data)
1482 {
1483 	struct nfs_pgio_header *hdr = data;
1484 
1485 	ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
1486 	if (test_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags)) {
1487 		ff_layout_send_layouterror(hdr->lseg);
1488 		pnfs_read_resend_pnfs(hdr);
1489 	} else if (test_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags))
1490 		ff_layout_reset_read(hdr);
1491 	pnfs_generic_rw_release(data);
1492 }
1493 
1494 
1495 static int ff_layout_write_done_cb(struct rpc_task *task,
1496 				struct nfs_pgio_header *hdr)
1497 {
1498 	loff_t end_offs = 0;
1499 	int err;
1500 
1501 	if (task->tk_status < 0) {
1502 		ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1503 					    hdr->args.offset, hdr->args.count,
1504 					    &hdr->res.op_status, OP_WRITE,
1505 					    task->tk_status);
1506 		trace_ff_layout_write_error(hdr);
1507 	}
1508 
1509 	err = ff_layout_async_handle_error(task, hdr->args.context->state,
1510 					   hdr->ds_clp, hdr->lseg,
1511 					   hdr->pgio_mirror_idx);
1512 
1513 	trace_nfs4_pnfs_write(hdr, err);
1514 	clear_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags);
1515 	clear_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags);
1516 	switch (err) {
1517 	case -NFS4ERR_RESET_TO_PNFS:
1518 		set_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags);
1519 		return task->tk_status;
1520 	case -NFS4ERR_RESET_TO_MDS:
1521 		set_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags);
1522 		return task->tk_status;
1523 	case -EAGAIN:
1524 		return -EAGAIN;
1525 	}
1526 
1527 	if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1528 	    hdr->res.verf->committed == NFS_DATA_SYNC)
1529 		end_offs = hdr->mds_offset + (loff_t)hdr->res.count;
1530 
1531 	/* Note: if the write is unstable, don't set end_offs until commit */
1532 	ff_layout_set_layoutcommit(hdr->inode, hdr->lseg, end_offs);
1533 
1534 	/* zero out fattr since we don't care DS attr at all */
1535 	hdr->fattr.valid = 0;
1536 	if (task->tk_status >= 0)
1537 		nfs_writeback_update_inode(hdr);
1538 
1539 	return 0;
1540 }
1541 
1542 static int ff_layout_commit_done_cb(struct rpc_task *task,
1543 				     struct nfs_commit_data *data)
1544 {
1545 	int err;
1546 
1547 	if (task->tk_status < 0) {
1548 		ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1549 					    data->args.offset, data->args.count,
1550 					    &data->res.op_status, OP_COMMIT,
1551 					    task->tk_status);
1552 		trace_ff_layout_commit_error(data);
1553 	}
1554 
1555 	err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1556 					   data->lseg, data->ds_commit_index);
1557 
1558 	trace_nfs4_pnfs_commit_ds(data, err);
1559 	switch (err) {
1560 	case -NFS4ERR_RESET_TO_PNFS:
1561 		pnfs_generic_prepare_to_resend_writes(data);
1562 		return -EAGAIN;
1563 	case -NFS4ERR_RESET_TO_MDS:
1564 		pnfs_generic_prepare_to_resend_writes(data);
1565 		return -EAGAIN;
1566 	case -EAGAIN:
1567 		rpc_restart_call_prepare(task);
1568 		return -EAGAIN;
1569 	}
1570 
1571 	ff_layout_set_layoutcommit(data->inode, data->lseg, data->lwb);
1572 
1573 	return 0;
1574 }
1575 
1576 static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1577 		struct nfs_pgio_header *hdr)
1578 {
1579 	if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1580 		return;
1581 	nfs4_ff_layout_stat_io_start_write(hdr->inode,
1582 			FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1583 			hdr->args.count,
1584 			task->tk_start);
1585 }
1586 
1587 static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1588 		struct nfs_pgio_header *hdr)
1589 {
1590 	if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1591 		return;
1592 	nfs4_ff_layout_stat_io_end_write(task,
1593 			FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1594 			hdr->args.count, hdr->res.count,
1595 			hdr->res.verf->committed);
1596 	set_bit(NFS_LSEG_LAYOUTRETURN, &hdr->lseg->pls_flags);
1597 }
1598 
1599 static int ff_layout_write_prepare_common(struct rpc_task *task,
1600 					  struct nfs_pgio_header *hdr)
1601 {
1602 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1603 		rpc_exit(task, -EIO);
1604 		return -EIO;
1605 	}
1606 
1607 	ff_layout_write_record_layoutstats_start(task, hdr);
1608 	return 0;
1609 }
1610 
1611 static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1612 {
1613 	struct nfs_pgio_header *hdr = data;
1614 
1615 	if (ff_layout_write_prepare_common(task, hdr))
1616 		return;
1617 
1618 	rpc_call_start(task);
1619 }
1620 
1621 static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1622 {
1623 	struct nfs_pgio_header *hdr = data;
1624 
1625 	if (nfs4_setup_sequence(hdr->ds_clp,
1626 				&hdr->args.seq_args,
1627 				&hdr->res.seq_res,
1628 				task))
1629 		return;
1630 
1631 	ff_layout_write_prepare_common(task, hdr);
1632 }
1633 
1634 static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1635 {
1636 	struct nfs_pgio_header *hdr = data;
1637 
1638 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1639 	    task->tk_status == 0) {
1640 		nfs4_sequence_done(task, &hdr->res.seq_res);
1641 		return;
1642 	}
1643 
1644 	/* Note this may cause RPC to be resent */
1645 	hdr->mds_ops->rpc_call_done(task, hdr);
1646 }
1647 
1648 static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1649 {
1650 	struct nfs_pgio_header *hdr = data;
1651 
1652 	ff_layout_write_record_layoutstats_done(task, hdr);
1653 	rpc_count_iostats_metrics(task,
1654 	    &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1655 }
1656 
1657 static void ff_layout_write_release(void *data)
1658 {
1659 	struct nfs_pgio_header *hdr = data;
1660 
1661 	ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
1662 	if (test_bit(NFS_IOHDR_RESEND_PNFS, &hdr->flags)) {
1663 		ff_layout_send_layouterror(hdr->lseg);
1664 		ff_layout_reset_write(hdr, true);
1665 	} else if (test_bit(NFS_IOHDR_RESEND_MDS, &hdr->flags))
1666 		ff_layout_reset_write(hdr, false);
1667 	pnfs_generic_rw_release(data);
1668 }
1669 
1670 static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1671 		struct nfs_commit_data *cdata)
1672 {
1673 	if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1674 		return;
1675 	nfs4_ff_layout_stat_io_start_write(cdata->inode,
1676 			FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1677 			0, task->tk_start);
1678 }
1679 
1680 static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1681 		struct nfs_commit_data *cdata)
1682 {
1683 	struct nfs_page *req;
1684 	__u64 count = 0;
1685 
1686 	if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1687 		return;
1688 
1689 	if (task->tk_status == 0) {
1690 		list_for_each_entry(req, &cdata->pages, wb_list)
1691 			count += req->wb_bytes;
1692 	}
1693 	nfs4_ff_layout_stat_io_end_write(task,
1694 			FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1695 			count, count, NFS_FILE_SYNC);
1696 	set_bit(NFS_LSEG_LAYOUTRETURN, &cdata->lseg->pls_flags);
1697 }
1698 
1699 static void ff_layout_commit_prepare_common(struct rpc_task *task,
1700 		struct nfs_commit_data *cdata)
1701 {
1702 	ff_layout_commit_record_layoutstats_start(task, cdata);
1703 }
1704 
1705 static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1706 {
1707 	ff_layout_commit_prepare_common(task, data);
1708 	rpc_call_start(task);
1709 }
1710 
1711 static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1712 {
1713 	struct nfs_commit_data *wdata = data;
1714 
1715 	if (nfs4_setup_sequence(wdata->ds_clp,
1716 				&wdata->args.seq_args,
1717 				&wdata->res.seq_res,
1718 				task))
1719 		return;
1720 	ff_layout_commit_prepare_common(task, data);
1721 }
1722 
1723 static void ff_layout_commit_done(struct rpc_task *task, void *data)
1724 {
1725 	pnfs_generic_write_commit_done(task, data);
1726 }
1727 
1728 static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1729 {
1730 	struct nfs_commit_data *cdata = data;
1731 
1732 	ff_layout_commit_record_layoutstats_done(task, cdata);
1733 	rpc_count_iostats_metrics(task,
1734 	    &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1735 }
1736 
1737 static void ff_layout_commit_release(void *data)
1738 {
1739 	struct nfs_commit_data *cdata = data;
1740 
1741 	ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
1742 	pnfs_generic_commit_release(data);
1743 }
1744 
1745 static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1746 	.rpc_call_prepare = ff_layout_read_prepare_v3,
1747 	.rpc_call_done = ff_layout_read_call_done,
1748 	.rpc_count_stats = ff_layout_read_count_stats,
1749 	.rpc_release = ff_layout_read_release,
1750 };
1751 
1752 static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1753 	.rpc_call_prepare = ff_layout_read_prepare_v4,
1754 	.rpc_call_done = ff_layout_read_call_done,
1755 	.rpc_count_stats = ff_layout_read_count_stats,
1756 	.rpc_release = ff_layout_read_release,
1757 };
1758 
1759 static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1760 	.rpc_call_prepare = ff_layout_write_prepare_v3,
1761 	.rpc_call_done = ff_layout_write_call_done,
1762 	.rpc_count_stats = ff_layout_write_count_stats,
1763 	.rpc_release = ff_layout_write_release,
1764 };
1765 
1766 static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1767 	.rpc_call_prepare = ff_layout_write_prepare_v4,
1768 	.rpc_call_done = ff_layout_write_call_done,
1769 	.rpc_count_stats = ff_layout_write_count_stats,
1770 	.rpc_release = ff_layout_write_release,
1771 };
1772 
1773 static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1774 	.rpc_call_prepare = ff_layout_commit_prepare_v3,
1775 	.rpc_call_done = ff_layout_commit_done,
1776 	.rpc_count_stats = ff_layout_commit_count_stats,
1777 	.rpc_release = ff_layout_commit_release,
1778 };
1779 
1780 static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1781 	.rpc_call_prepare = ff_layout_commit_prepare_v4,
1782 	.rpc_call_done = ff_layout_commit_done,
1783 	.rpc_count_stats = ff_layout_commit_count_stats,
1784 	.rpc_release = ff_layout_commit_release,
1785 };
1786 
1787 static enum pnfs_try_status
1788 ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1789 {
1790 	struct pnfs_layout_segment *lseg = hdr->lseg;
1791 	struct nfs4_pnfs_ds *ds;
1792 	struct rpc_clnt *ds_clnt;
1793 	struct nfs4_ff_layout_mirror *mirror;
1794 	const struct cred *ds_cred;
1795 	loff_t offset = hdr->args.offset;
1796 	u32 idx = hdr->pgio_mirror_idx;
1797 	int vers;
1798 	struct nfs_fh *fh;
1799 
1800 	dprintk("--> %s ino %lu pgbase %u req %zu@%llu\n",
1801 		__func__, hdr->inode->i_ino,
1802 		hdr->args.pgbase, (size_t)hdr->args.count, offset);
1803 
1804 	mirror = FF_LAYOUT_COMP(lseg, idx);
1805 	ds = nfs4_ff_layout_prepare_ds(lseg, mirror, false);
1806 	if (!ds)
1807 		goto out_failed;
1808 
1809 	ds_clnt = nfs4_ff_find_or_create_ds_client(mirror, ds->ds_clp,
1810 						   hdr->inode);
1811 	if (IS_ERR(ds_clnt))
1812 		goto out_failed;
1813 
1814 	ds_cred = ff_layout_get_ds_cred(mirror, &lseg->pls_range, hdr->cred);
1815 	if (!ds_cred)
1816 		goto out_failed;
1817 
1818 	vers = nfs4_ff_layout_ds_version(mirror);
1819 
1820 	dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1821 		ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count), vers);
1822 
1823 	hdr->pgio_done_cb = ff_layout_read_done_cb;
1824 	refcount_inc(&ds->ds_clp->cl_count);
1825 	hdr->ds_clp = ds->ds_clp;
1826 	fh = nfs4_ff_layout_select_ds_fh(mirror);
1827 	if (fh)
1828 		hdr->args.fh = fh;
1829 
1830 	nfs4_ff_layout_select_ds_stateid(mirror, &hdr->args.stateid);
1831 
1832 	/*
1833 	 * Note that if we ever decide to split across DSes,
1834 	 * then we may need to handle dense-like offsets.
1835 	 */
1836 	hdr->args.offset = offset;
1837 	hdr->mds_offset = offset;
1838 
1839 	/* Perform an asynchronous read to ds */
1840 	nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1841 			  vers == 3 ? &ff_layout_read_call_ops_v3 :
1842 				      &ff_layout_read_call_ops_v4,
1843 			  0, RPC_TASK_SOFTCONN);
1844 	put_cred(ds_cred);
1845 	return PNFS_ATTEMPTED;
1846 
1847 out_failed:
1848 	if (ff_layout_avoid_mds_available_ds(lseg))
1849 		return PNFS_TRY_AGAIN;
1850 	trace_pnfs_mds_fallback_read_pagelist(hdr->inode,
1851 			hdr->args.offset, hdr->args.count,
1852 			IOMODE_READ, NFS_I(hdr->inode)->layout, lseg);
1853 	return PNFS_NOT_ATTEMPTED;
1854 }
1855 
1856 /* Perform async writes. */
1857 static enum pnfs_try_status
1858 ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1859 {
1860 	struct pnfs_layout_segment *lseg = hdr->lseg;
1861 	struct nfs4_pnfs_ds *ds;
1862 	struct rpc_clnt *ds_clnt;
1863 	struct nfs4_ff_layout_mirror *mirror;
1864 	const struct cred *ds_cred;
1865 	loff_t offset = hdr->args.offset;
1866 	int vers;
1867 	struct nfs_fh *fh;
1868 	int idx = hdr->pgio_mirror_idx;
1869 
1870 	mirror = FF_LAYOUT_COMP(lseg, idx);
1871 	ds = nfs4_ff_layout_prepare_ds(lseg, mirror, true);
1872 	if (!ds)
1873 		goto out_failed;
1874 
1875 	ds_clnt = nfs4_ff_find_or_create_ds_client(mirror, ds->ds_clp,
1876 						   hdr->inode);
1877 	if (IS_ERR(ds_clnt))
1878 		goto out_failed;
1879 
1880 	ds_cred = ff_layout_get_ds_cred(mirror, &lseg->pls_range, hdr->cred);
1881 	if (!ds_cred)
1882 		goto out_failed;
1883 
1884 	vers = nfs4_ff_layout_ds_version(mirror);
1885 
1886 	dprintk("%s ino %lu sync %d req %zu@%llu DS: %s cl_count %d vers %d\n",
1887 		__func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1888 		offset, ds->ds_remotestr, refcount_read(&ds->ds_clp->cl_count),
1889 		vers);
1890 
1891 	hdr->pgio_done_cb = ff_layout_write_done_cb;
1892 	refcount_inc(&ds->ds_clp->cl_count);
1893 	hdr->ds_clp = ds->ds_clp;
1894 	hdr->ds_commit_idx = idx;
1895 	fh = nfs4_ff_layout_select_ds_fh(mirror);
1896 	if (fh)
1897 		hdr->args.fh = fh;
1898 
1899 	nfs4_ff_layout_select_ds_stateid(mirror, &hdr->args.stateid);
1900 
1901 	/*
1902 	 * Note that if we ever decide to split across DSes,
1903 	 * then we may need to handle dense-like offsets.
1904 	 */
1905 	hdr->args.offset = offset;
1906 
1907 	/* Perform an asynchronous write */
1908 	nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1909 			  vers == 3 ? &ff_layout_write_call_ops_v3 :
1910 				      &ff_layout_write_call_ops_v4,
1911 			  sync, RPC_TASK_SOFTCONN);
1912 	put_cred(ds_cred);
1913 	return PNFS_ATTEMPTED;
1914 
1915 out_failed:
1916 	if (ff_layout_avoid_mds_available_ds(lseg))
1917 		return PNFS_TRY_AGAIN;
1918 	trace_pnfs_mds_fallback_write_pagelist(hdr->inode,
1919 			hdr->args.offset, hdr->args.count,
1920 			IOMODE_RW, NFS_I(hdr->inode)->layout, lseg);
1921 	return PNFS_NOT_ATTEMPTED;
1922 }
1923 
1924 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1925 {
1926 	return i;
1927 }
1928 
1929 static struct nfs_fh *
1930 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1931 {
1932 	struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1933 
1934 	/* FIXME: Assume that there is only one NFS version available
1935 	 * for the DS.
1936 	 */
1937 	return &flseg->mirror_array[i]->fh_versions[0];
1938 }
1939 
1940 static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1941 {
1942 	struct pnfs_layout_segment *lseg = data->lseg;
1943 	struct nfs4_pnfs_ds *ds;
1944 	struct rpc_clnt *ds_clnt;
1945 	struct nfs4_ff_layout_mirror *mirror;
1946 	const struct cred *ds_cred;
1947 	u32 idx;
1948 	int vers, ret;
1949 	struct nfs_fh *fh;
1950 
1951 	if (!lseg || !(pnfs_is_valid_lseg(lseg) ||
1952 	    test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)))
1953 		goto out_err;
1954 
1955 	idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1956 	mirror = FF_LAYOUT_COMP(lseg, idx);
1957 	ds = nfs4_ff_layout_prepare_ds(lseg, mirror, true);
1958 	if (!ds)
1959 		goto out_err;
1960 
1961 	ds_clnt = nfs4_ff_find_or_create_ds_client(mirror, ds->ds_clp,
1962 						   data->inode);
1963 	if (IS_ERR(ds_clnt))
1964 		goto out_err;
1965 
1966 	ds_cred = ff_layout_get_ds_cred(mirror, &lseg->pls_range, data->cred);
1967 	if (!ds_cred)
1968 		goto out_err;
1969 
1970 	vers = nfs4_ff_layout_ds_version(mirror);
1971 
1972 	dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1973 		data->inode->i_ino, how, refcount_read(&ds->ds_clp->cl_count),
1974 		vers);
1975 	data->commit_done_cb = ff_layout_commit_done_cb;
1976 	data->cred = ds_cred;
1977 	refcount_inc(&ds->ds_clp->cl_count);
1978 	data->ds_clp = ds->ds_clp;
1979 	fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1980 	if (fh)
1981 		data->args.fh = fh;
1982 
1983 	ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
1984 				   vers == 3 ? &ff_layout_commit_call_ops_v3 :
1985 					       &ff_layout_commit_call_ops_v4,
1986 				   how, RPC_TASK_SOFTCONN);
1987 	put_cred(ds_cred);
1988 	return ret;
1989 out_err:
1990 	pnfs_generic_prepare_to_resend_writes(data);
1991 	pnfs_generic_commit_release(data);
1992 	return -EAGAIN;
1993 }
1994 
1995 static int
1996 ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1997 			   int how, struct nfs_commit_info *cinfo)
1998 {
1999 	return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
2000 					    ff_layout_initiate_commit);
2001 }
2002 
2003 static struct pnfs_ds_commit_info *
2004 ff_layout_get_ds_info(struct inode *inode)
2005 {
2006 	struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
2007 
2008 	if (layout == NULL)
2009 		return NULL;
2010 
2011 	return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
2012 }
2013 
2014 static void
2015 ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
2016 {
2017 	nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
2018 						  id_node));
2019 }
2020 
2021 static int ff_layout_encode_ioerr(struct xdr_stream *xdr,
2022 				  const struct nfs4_layoutreturn_args *args,
2023 				  const struct nfs4_flexfile_layoutreturn_args *ff_args)
2024 {
2025 	__be32 *start;
2026 
2027 	start = xdr_reserve_space(xdr, 4);
2028 	if (unlikely(!start))
2029 		return -E2BIG;
2030 
2031 	*start = cpu_to_be32(ff_args->num_errors);
2032 	/* This assume we always return _ALL_ layouts */
2033 	return ff_layout_encode_ds_ioerr(xdr, &ff_args->errors);
2034 }
2035 
2036 static void
2037 encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len)
2038 {
2039 	WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0);
2040 }
2041 
2042 static void
2043 ff_layout_encode_ff_iostat_head(struct xdr_stream *xdr,
2044 			    const nfs4_stateid *stateid,
2045 			    const struct nfs42_layoutstat_devinfo *devinfo)
2046 {
2047 	__be32 *p;
2048 
2049 	p = xdr_reserve_space(xdr, 8 + 8);
2050 	p = xdr_encode_hyper(p, devinfo->offset);
2051 	p = xdr_encode_hyper(p, devinfo->length);
2052 	encode_opaque_fixed(xdr, stateid->data, NFS4_STATEID_SIZE);
2053 	p = xdr_reserve_space(xdr, 4*8);
2054 	p = xdr_encode_hyper(p, devinfo->read_count);
2055 	p = xdr_encode_hyper(p, devinfo->read_bytes);
2056 	p = xdr_encode_hyper(p, devinfo->write_count);
2057 	p = xdr_encode_hyper(p, devinfo->write_bytes);
2058 	encode_opaque_fixed(xdr, devinfo->dev_id.data, NFS4_DEVICEID4_SIZE);
2059 }
2060 
2061 static void
2062 ff_layout_encode_ff_iostat(struct xdr_stream *xdr,
2063 			    const nfs4_stateid *stateid,
2064 			    const struct nfs42_layoutstat_devinfo *devinfo)
2065 {
2066 	ff_layout_encode_ff_iostat_head(xdr, stateid, devinfo);
2067 	ff_layout_encode_ff_layoutupdate(xdr, devinfo,
2068 			devinfo->ld_private.data);
2069 }
2070 
2071 /* report nothing for now */
2072 static void ff_layout_encode_iostats_array(struct xdr_stream *xdr,
2073 		const struct nfs4_layoutreturn_args *args,
2074 		struct nfs4_flexfile_layoutreturn_args *ff_args)
2075 {
2076 	__be32 *p;
2077 	int i;
2078 
2079 	p = xdr_reserve_space(xdr, 4);
2080 	*p = cpu_to_be32(ff_args->num_dev);
2081 	for (i = 0; i < ff_args->num_dev; i++)
2082 		ff_layout_encode_ff_iostat(xdr,
2083 				&args->layout->plh_stateid,
2084 				&ff_args->devinfo[i]);
2085 }
2086 
2087 static void
2088 ff_layout_free_iostats_array(struct nfs42_layoutstat_devinfo *devinfo,
2089 		unsigned int num_entries)
2090 {
2091 	unsigned int i;
2092 
2093 	for (i = 0; i < num_entries; i++) {
2094 		if (!devinfo[i].ld_private.ops)
2095 			continue;
2096 		if (!devinfo[i].ld_private.ops->free)
2097 			continue;
2098 		devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
2099 	}
2100 }
2101 
2102 static struct nfs4_deviceid_node *
2103 ff_layout_alloc_deviceid_node(struct nfs_server *server,
2104 			      struct pnfs_device *pdev, gfp_t gfp_flags)
2105 {
2106 	struct nfs4_ff_layout_ds *dsaddr;
2107 
2108 	dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
2109 	if (!dsaddr)
2110 		return NULL;
2111 	return &dsaddr->id_node;
2112 }
2113 
2114 static void
2115 ff_layout_encode_layoutreturn(struct xdr_stream *xdr,
2116 		const void *voidargs,
2117 		const struct nfs4_xdr_opaque_data *ff_opaque)
2118 {
2119 	const struct nfs4_layoutreturn_args *args = voidargs;
2120 	struct nfs4_flexfile_layoutreturn_args *ff_args = ff_opaque->data;
2121 	struct xdr_buf tmp_buf = {
2122 		.head = {
2123 			[0] = {
2124 				.iov_base = page_address(ff_args->pages[0]),
2125 			},
2126 		},
2127 		.buflen = PAGE_SIZE,
2128 	};
2129 	struct xdr_stream tmp_xdr;
2130 	__be32 *start;
2131 
2132 	dprintk("%s: Begin\n", __func__);
2133 
2134 	xdr_init_encode(&tmp_xdr, &tmp_buf, NULL, NULL);
2135 
2136 	ff_layout_encode_ioerr(&tmp_xdr, args, ff_args);
2137 	ff_layout_encode_iostats_array(&tmp_xdr, args, ff_args);
2138 
2139 	start = xdr_reserve_space(xdr, 4);
2140 	*start = cpu_to_be32(tmp_buf.len);
2141 	xdr_write_pages(xdr, ff_args->pages, 0, tmp_buf.len);
2142 
2143 	dprintk("%s: Return\n", __func__);
2144 }
2145 
2146 static void
2147 ff_layout_free_layoutreturn(struct nfs4_xdr_opaque_data *args)
2148 {
2149 	struct nfs4_flexfile_layoutreturn_args *ff_args;
2150 
2151 	if (!args->data)
2152 		return;
2153 	ff_args = args->data;
2154 	args->data = NULL;
2155 
2156 	ff_layout_free_ds_ioerr(&ff_args->errors);
2157 	ff_layout_free_iostats_array(ff_args->devinfo, ff_args->num_dev);
2158 
2159 	put_page(ff_args->pages[0]);
2160 	kfree(ff_args);
2161 }
2162 
2163 static const struct nfs4_xdr_opaque_ops layoutreturn_ops = {
2164 	.encode = ff_layout_encode_layoutreturn,
2165 	.free = ff_layout_free_layoutreturn,
2166 };
2167 
2168 static int
2169 ff_layout_prepare_layoutreturn(struct nfs4_layoutreturn_args *args)
2170 {
2171 	struct nfs4_flexfile_layoutreturn_args *ff_args;
2172 	struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(args->layout);
2173 
2174 	ff_args = kmalloc(sizeof(*ff_args), GFP_KERNEL);
2175 	if (!ff_args)
2176 		goto out_nomem;
2177 	ff_args->pages[0] = alloc_page(GFP_KERNEL);
2178 	if (!ff_args->pages[0])
2179 		goto out_nomem_free;
2180 
2181 	INIT_LIST_HEAD(&ff_args->errors);
2182 	ff_args->num_errors = ff_layout_fetch_ds_ioerr(args->layout,
2183 			&args->range, &ff_args->errors,
2184 			FF_LAYOUTRETURN_MAXERR);
2185 
2186 	spin_lock(&args->inode->i_lock);
2187 	ff_args->num_dev = ff_layout_mirror_prepare_stats(&ff_layout->generic_hdr,
2188 			&ff_args->devinfo[0], ARRAY_SIZE(ff_args->devinfo));
2189 	spin_unlock(&args->inode->i_lock);
2190 
2191 	args->ld_private->ops = &layoutreturn_ops;
2192 	args->ld_private->data = ff_args;
2193 	return 0;
2194 out_nomem_free:
2195 	kfree(ff_args);
2196 out_nomem:
2197 	return -ENOMEM;
2198 }
2199 
2200 #ifdef CONFIG_NFS_V4_2
2201 void
2202 ff_layout_send_layouterror(struct pnfs_layout_segment *lseg)
2203 {
2204 	struct pnfs_layout_hdr *lo = lseg->pls_layout;
2205 	struct nfs42_layout_error *errors;
2206 	LIST_HEAD(head);
2207 
2208 	if (!nfs_server_capable(lo->plh_inode, NFS_CAP_LAYOUTERROR))
2209 		return;
2210 	ff_layout_fetch_ds_ioerr(lo, &lseg->pls_range, &head, -1);
2211 	if (list_empty(&head))
2212 		return;
2213 
2214 	errors = kmalloc_array(NFS42_LAYOUTERROR_MAX,
2215 			sizeof(*errors), GFP_NOFS);
2216 	if (errors != NULL) {
2217 		const struct nfs4_ff_layout_ds_err *pos;
2218 		size_t n = 0;
2219 
2220 		list_for_each_entry(pos, &head, list) {
2221 			errors[n].offset = pos->offset;
2222 			errors[n].length = pos->length;
2223 			nfs4_stateid_copy(&errors[n].stateid, &pos->stateid);
2224 			errors[n].errors[0].dev_id = pos->deviceid;
2225 			errors[n].errors[0].status = pos->status;
2226 			errors[n].errors[0].opnum = pos->opnum;
2227 			n++;
2228 			if (!list_is_last(&pos->list, &head) &&
2229 			    n < NFS42_LAYOUTERROR_MAX)
2230 				continue;
2231 			if (nfs42_proc_layouterror(lseg, errors, n) < 0)
2232 				break;
2233 			n = 0;
2234 		}
2235 		kfree(errors);
2236 	}
2237 	ff_layout_free_ds_ioerr(&head);
2238 }
2239 #else
2240 void
2241 ff_layout_send_layouterror(struct pnfs_layout_segment *lseg)
2242 {
2243 }
2244 #endif
2245 
2246 static int
2247 ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
2248 {
2249 	const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
2250 
2251 	return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
2252 }
2253 
2254 static size_t
2255 ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
2256 			  const int buflen)
2257 {
2258 	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
2259 	const struct in6_addr *addr = &sin6->sin6_addr;
2260 
2261 	/*
2262 	 * RFC 4291, Section 2.2.2
2263 	 *
2264 	 * Shorthanded ANY address
2265 	 */
2266 	if (ipv6_addr_any(addr))
2267 		return snprintf(buf, buflen, "::");
2268 
2269 	/*
2270 	 * RFC 4291, Section 2.2.2
2271 	 *
2272 	 * Shorthanded loopback address
2273 	 */
2274 	if (ipv6_addr_loopback(addr))
2275 		return snprintf(buf, buflen, "::1");
2276 
2277 	/*
2278 	 * RFC 4291, Section 2.2.3
2279 	 *
2280 	 * Special presentation address format for mapped v4
2281 	 * addresses.
2282 	 */
2283 	if (ipv6_addr_v4mapped(addr))
2284 		return snprintf(buf, buflen, "::ffff:%pI4",
2285 					&addr->s6_addr32[3]);
2286 
2287 	/*
2288 	 * RFC 4291, Section 2.2.1
2289 	 */
2290 	return snprintf(buf, buflen, "%pI6c", addr);
2291 }
2292 
2293 /* Derived from rpc_sockaddr2uaddr */
2294 static void
2295 ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
2296 {
2297 	struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
2298 	char portbuf[RPCBIND_MAXUADDRPLEN];
2299 	char addrbuf[RPCBIND_MAXUADDRLEN];
2300 	char *netid;
2301 	unsigned short port;
2302 	int len, netid_len;
2303 	__be32 *p;
2304 
2305 	switch (sap->sa_family) {
2306 	case AF_INET:
2307 		if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2308 			return;
2309 		port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2310 		netid = "tcp";
2311 		netid_len = 3;
2312 		break;
2313 	case AF_INET6:
2314 		if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2315 			return;
2316 		port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2317 		netid = "tcp6";
2318 		netid_len = 4;
2319 		break;
2320 	default:
2321 		/* we only support tcp and tcp6 */
2322 		WARN_ON_ONCE(1);
2323 		return;
2324 	}
2325 
2326 	snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2327 	len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2328 
2329 	p = xdr_reserve_space(xdr, 4 + netid_len);
2330 	xdr_encode_opaque(p, netid, netid_len);
2331 
2332 	p = xdr_reserve_space(xdr, 4 + len);
2333 	xdr_encode_opaque(p, addrbuf, len);
2334 }
2335 
2336 static void
2337 ff_layout_encode_nfstime(struct xdr_stream *xdr,
2338 			 ktime_t t)
2339 {
2340 	struct timespec64 ts;
2341 	__be32 *p;
2342 
2343 	p = xdr_reserve_space(xdr, 12);
2344 	ts = ktime_to_timespec64(t);
2345 	p = xdr_encode_hyper(p, ts.tv_sec);
2346 	*p++ = cpu_to_be32(ts.tv_nsec);
2347 }
2348 
2349 static void
2350 ff_layout_encode_io_latency(struct xdr_stream *xdr,
2351 			    struct nfs4_ff_io_stat *stat)
2352 {
2353 	__be32 *p;
2354 
2355 	p = xdr_reserve_space(xdr, 5 * 8);
2356 	p = xdr_encode_hyper(p, stat->ops_requested);
2357 	p = xdr_encode_hyper(p, stat->bytes_requested);
2358 	p = xdr_encode_hyper(p, stat->ops_completed);
2359 	p = xdr_encode_hyper(p, stat->bytes_completed);
2360 	p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2361 	ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2362 	ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2363 }
2364 
2365 static void
2366 ff_layout_encode_ff_layoutupdate(struct xdr_stream *xdr,
2367 			      const struct nfs42_layoutstat_devinfo *devinfo,
2368 			      struct nfs4_ff_layout_mirror *mirror)
2369 {
2370 	struct nfs4_pnfs_ds_addr *da;
2371 	struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2372 	struct nfs_fh *fh = &mirror->fh_versions[0];
2373 	__be32 *p;
2374 
2375 	da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2376 	dprintk("%s: DS %s: encoding address %s\n",
2377 		__func__, ds->ds_remotestr, da->da_remotestr);
2378 	/* netaddr4 */
2379 	ff_layout_encode_netaddr(xdr, da);
2380 	/* nfs_fh4 */
2381 	p = xdr_reserve_space(xdr, 4 + fh->size);
2382 	xdr_encode_opaque(p, fh->data, fh->size);
2383 	/* ff_io_latency4 read */
2384 	spin_lock(&mirror->lock);
2385 	ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2386 	/* ff_io_latency4 write */
2387 	ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2388 	spin_unlock(&mirror->lock);
2389 	/* nfstime4 */
2390 	ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2391 	/* bool */
2392 	p = xdr_reserve_space(xdr, 4);
2393 	*p = cpu_to_be32(false);
2394 }
2395 
2396 static void
2397 ff_layout_encode_layoutstats(struct xdr_stream *xdr, const void *args,
2398 			     const struct nfs4_xdr_opaque_data *opaque)
2399 {
2400 	struct nfs42_layoutstat_devinfo *devinfo = container_of(opaque,
2401 			struct nfs42_layoutstat_devinfo, ld_private);
2402 	__be32 *start;
2403 
2404 	/* layoutupdate length */
2405 	start = xdr_reserve_space(xdr, 4);
2406 	ff_layout_encode_ff_layoutupdate(xdr, devinfo, opaque->data);
2407 
2408 	*start = cpu_to_be32((xdr->p - start - 1) * 4);
2409 }
2410 
2411 static void
2412 ff_layout_free_layoutstats(struct nfs4_xdr_opaque_data *opaque)
2413 {
2414 	struct nfs4_ff_layout_mirror *mirror = opaque->data;
2415 
2416 	ff_layout_put_mirror(mirror);
2417 }
2418 
2419 static const struct nfs4_xdr_opaque_ops layoutstat_ops = {
2420 	.encode = ff_layout_encode_layoutstats,
2421 	.free	= ff_layout_free_layoutstats,
2422 };
2423 
2424 static int
2425 ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr *lo,
2426 			       struct nfs42_layoutstat_devinfo *devinfo,
2427 			       int dev_limit)
2428 {
2429 	struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
2430 	struct nfs4_ff_layout_mirror *mirror;
2431 	struct nfs4_deviceid_node *dev;
2432 	int i = 0;
2433 
2434 	list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2435 		if (i >= dev_limit)
2436 			break;
2437 		if (IS_ERR_OR_NULL(mirror->mirror_ds))
2438 			continue;
2439 		if (!test_and_clear_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags))
2440 			continue;
2441 		/* mirror refcount put in cleanup_layoutstats */
2442 		if (!refcount_inc_not_zero(&mirror->ref))
2443 			continue;
2444 		dev = &mirror->mirror_ds->id_node;
2445 		memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
2446 		devinfo->offset = 0;
2447 		devinfo->length = NFS4_MAX_UINT64;
2448 		spin_lock(&mirror->lock);
2449 		devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
2450 		devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
2451 		devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
2452 		devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2453 		spin_unlock(&mirror->lock);
2454 		devinfo->layout_type = LAYOUT_FLEX_FILES;
2455 		devinfo->ld_private.ops = &layoutstat_ops;
2456 		devinfo->ld_private.data = mirror;
2457 
2458 		devinfo++;
2459 		i++;
2460 	}
2461 	return i;
2462 }
2463 
2464 static int
2465 ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2466 {
2467 	struct nfs4_flexfile_layout *ff_layout;
2468 	const int dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2469 
2470 	/* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2471 	args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
2472 	if (!args->devinfo)
2473 		return -ENOMEM;
2474 
2475 	spin_lock(&args->inode->i_lock);
2476 	ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2477 	args->num_dev = ff_layout_mirror_prepare_stats(&ff_layout->generic_hdr,
2478 			&args->devinfo[0], dev_count);
2479 	spin_unlock(&args->inode->i_lock);
2480 	if (!args->num_dev) {
2481 		kfree(args->devinfo);
2482 		args->devinfo = NULL;
2483 		return -ENOENT;
2484 	}
2485 
2486 	return 0;
2487 }
2488 
2489 static int
2490 ff_layout_set_layoutdriver(struct nfs_server *server,
2491 		const struct nfs_fh *dummy)
2492 {
2493 #if IS_ENABLED(CONFIG_NFS_V4_2)
2494 	server->caps |= NFS_CAP_LAYOUTSTATS;
2495 #endif
2496 	return 0;
2497 }
2498 
2499 static struct pnfs_layoutdriver_type flexfilelayout_type = {
2500 	.id			= LAYOUT_FLEX_FILES,
2501 	.name			= "LAYOUT_FLEX_FILES",
2502 	.owner			= THIS_MODULE,
2503 	.flags			= PNFS_LAYOUTGET_ON_OPEN,
2504 	.max_layoutget_response	= 4096, /* 1 page or so... */
2505 	.set_layoutdriver	= ff_layout_set_layoutdriver,
2506 	.alloc_layout_hdr	= ff_layout_alloc_layout_hdr,
2507 	.free_layout_hdr	= ff_layout_free_layout_hdr,
2508 	.alloc_lseg		= ff_layout_alloc_lseg,
2509 	.free_lseg		= ff_layout_free_lseg,
2510 	.add_lseg		= ff_layout_add_lseg,
2511 	.pg_read_ops		= &ff_layout_pg_read_ops,
2512 	.pg_write_ops		= &ff_layout_pg_write_ops,
2513 	.get_ds_info		= ff_layout_get_ds_info,
2514 	.free_deviceid_node	= ff_layout_free_deviceid_node,
2515 	.mark_request_commit	= pnfs_layout_mark_request_commit,
2516 	.clear_request_commit	= pnfs_generic_clear_request_commit,
2517 	.scan_commit_lists	= pnfs_generic_scan_commit_lists,
2518 	.recover_commit_reqs	= pnfs_generic_recover_commit_reqs,
2519 	.commit_pagelist	= ff_layout_commit_pagelist,
2520 	.read_pagelist		= ff_layout_read_pagelist,
2521 	.write_pagelist		= ff_layout_write_pagelist,
2522 	.alloc_deviceid_node    = ff_layout_alloc_deviceid_node,
2523 	.prepare_layoutreturn   = ff_layout_prepare_layoutreturn,
2524 	.sync			= pnfs_nfs_generic_sync,
2525 	.prepare_layoutstats	= ff_layout_prepare_layoutstats,
2526 };
2527 
2528 static int __init nfs4flexfilelayout_init(void)
2529 {
2530 	printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2531 	       __func__);
2532 	return pnfs_register_layoutdriver(&flexfilelayout_type);
2533 }
2534 
2535 static void __exit nfs4flexfilelayout_exit(void)
2536 {
2537 	printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2538 	       __func__);
2539 	pnfs_unregister_layoutdriver(&flexfilelayout_type);
2540 }
2541 
2542 MODULE_ALIAS("nfs-layouttype4-4");
2543 
2544 MODULE_LICENSE("GPL");
2545 MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2546 
2547 module_init(nfs4flexfilelayout_init);
2548 module_exit(nfs4flexfilelayout_exit);
2549 
2550 module_param(io_maxretrans, ushort, 0644);
2551 MODULE_PARM_DESC(io_maxretrans, "The  number of times the NFSv4.1 client "
2552 			"retries an I/O request before returning an error. ");
2553