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