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