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