2 * Module for pnfs flexfile layout driver.
4 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6 * Tao Peng <bergwolf@primarydata.com>
9 #include <linux/nfs_fs.h>
10 #include <linux/nfs_page.h>
11 #include <linux/module.h>
13 #include <linux/sunrpc/metrics.h>
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"
25 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
27 #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ)
28 #define FF_LAYOUTRETURN_MAXERR 20
31 static struct group_info
*ff_zero_group
;
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
,
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
);
42 static struct pnfs_layout_hdr
*
43 ff_layout_alloc_layout_hdr(struct inode
*inode
, gfp_t gfp_flags
)
45 struct nfs4_flexfile_layout
*ffl
;
47 ffl
= kzalloc(sizeof(*ffl
), gfp_flags
);
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
;
58 ff_layout_free_layout_hdr(struct pnfs_layout_hdr
*lo
)
60 struct nfs4_ff_layout_ds_err
*err
, *n
;
62 list_for_each_entry_safe(err
, n
, &FF_LAYOUT_FROM_HDR(lo
)->error_list
,
67 kfree(FF_LAYOUT_FROM_HDR(lo
));
70 static int decode_pnfs_stateid(struct xdr_stream
*xdr
, nfs4_stateid
*stateid
)
74 p
= xdr_inline_decode(xdr
, NFS4_STATEID_SIZE
);
75 if (unlikely(p
== NULL
))
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]);
84 static int decode_deviceid(struct xdr_stream
*xdr
, struct nfs4_deviceid
*devid
)
88 p
= xdr_inline_decode(xdr
, NFS4_DEVICEID4_SIZE
);
91 memcpy(devid
, p
, NFS4_DEVICEID4_SIZE
);
92 nfs4_print_deviceid(devid
);
96 static int decode_nfs_fh(struct xdr_stream
*xdr
, struct nfs_fh
*fh
)
100 p
= xdr_inline_decode(xdr
, 4);
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",
110 p
= xdr_inline_decode(xdr
, fh
->size
);
113 memcpy(&fh
->data
, p
, fh
->size
);
114 dprintk("%s: fh len %d\n", __func__
, fh
->size
);
120 * Currently only stringified uids and gids are accepted.
121 * I.e., kerberos is not supported to the DSes, so no pricipals.
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().
128 decode_name(struct xdr_stream
*xdr
, u32
*id
)
133 /* opaque_length(4)*/
134 p
= xdr_inline_decode(xdr
, 4);
137 len
= be32_to_cpup(p
++);
141 dprintk("%s: len %u\n", __func__
, len
);
144 p
= xdr_inline_decode(xdr
, len
);
148 if (!nfs_map_string_to_numeric((char *)p
, len
, id
))
154 static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror
*m1
,
155 const struct nfs4_ff_layout_mirror
*m2
)
159 if (m1
->fh_versions_cnt
!= m2
->fh_versions_cnt
)
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) {
176 static struct nfs4_ff_layout_mirror
*
177 ff_layout_add_mirror(struct pnfs_layout_hdr
*lo
,
178 struct nfs4_ff_layout_mirror
*mirror
)
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
;
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)
188 if (!ff_mirror_match_fh(mirror
, pos
))
190 if (refcount_inc_not_zero(&pos
->ref
)) {
191 spin_unlock(&inode
->i_lock
);
195 list_add(&mirror
->mirrors
, &ff_layout
->mirrors
);
197 spin_unlock(&inode
->i_lock
);
202 ff_layout_remove_mirror(struct nfs4_ff_layout_mirror
*mirror
)
205 if (mirror
->layout
== NULL
)
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
;
214 static struct nfs4_ff_layout_mirror
*ff_layout_alloc_mirror(gfp_t gfp_flags
)
216 struct nfs4_ff_layout_mirror
*mirror
;
218 mirror
= kzalloc(sizeof(*mirror
), gfp_flags
);
219 if (mirror
!= NULL
) {
220 spin_lock_init(&mirror
->lock
);
221 refcount_set(&mirror
->ref
, 1);
222 INIT_LIST_HEAD(&mirror
->mirrors
);
227 static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror
*mirror
)
229 struct rpc_cred
*cred
;
231 ff_layout_remove_mirror(mirror
);
232 kfree(mirror
->fh_versions
);
233 cred
= rcu_access_pointer(mirror
->ro_cred
);
236 cred
= rcu_access_pointer(mirror
->rw_cred
);
239 nfs4_ff_layout_put_deviceid(mirror
->mirror_ds
);
243 static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror
*mirror
)
245 if (mirror
!= NULL
&& refcount_dec_and_test(&mirror
->ref
))
246 ff_layout_free_mirror(mirror
);
249 static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment
*fls
)
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
]);
260 kfree(fls
->mirror_array
);
261 fls
->mirror_array
= NULL
;
265 static int ff_layout_check_layout(struct nfs4_layoutget_res
*lgr
)
269 dprintk("--> %s\n", __func__
);
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",
279 dprintk("--> %s returns %d\n", __func__
, ret
);
283 static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment
*fls
)
286 ff_layout_free_mirror_array(fls
);
292 ff_lseg_range_is_after(const struct pnfs_layout_range
*l1
,
293 const struct pnfs_layout_range
*l2
)
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
)
303 if (end2
< l1
->offset
)
305 return l2
->offset
<= l1
->offset
;
309 ff_lseg_merge(struct pnfs_layout_segment
*new,
310 struct pnfs_layout_segment
*old
)
312 u64 new_end
, old_end
;
314 if (test_bit(NFS_LSEG_LAYOUTRETURN
, &old
->pls_flags
))
316 if (new->pls_range
.iomode
!= old
->pls_range
.iomode
)
318 old_end
= pnfs_calc_offset_end(old
->pls_range
.offset
,
319 old
->pls_range
.length
);
320 if (old_end
< new->pls_range
.offset
)
322 new_end
= pnfs_calc_offset_end(new->pls_range
.offset
,
323 new->pls_range
.length
);
324 if (new_end
< old
->pls_range
.offset
)
327 /* Mergeable: copy info from 'old' to 'new' */
328 if (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
,
334 if (test_bit(NFS_LSEG_ROC
, &old
->pls_flags
))
335 set_bit(NFS_LSEG_ROC
, &new->pls_flags
);
340 ff_layout_add_lseg(struct pnfs_layout_hdr
*lo
,
341 struct pnfs_layout_segment
*lseg
,
342 struct list_head
*free_me
)
344 pnfs_generic_layout_insert_lseg(lo
, lseg
,
345 ff_lseg_range_is_after
,
350 static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment
*fls
)
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
]);
363 static struct pnfs_layout_segment
*
364 ff_layout_alloc_lseg(struct pnfs_layout_hdr
*lh
,
365 struct nfs4_layoutget_res
*lgr
,
368 struct pnfs_layout_segment
*ret
;
369 struct nfs4_ff_layout_segment
*fls
= NULL
;
370 struct xdr_stream stream
;
372 struct page
*scratch
;
374 u32 mirror_array_cnt
;
378 dprintk("--> %s\n", __func__
);
379 scratch
= alloc_page(gfp_flags
);
381 return ERR_PTR(-ENOMEM
);
383 xdr_init_decode_pages(&stream
, &buf
, lgr
->layoutp
->pages
,
385 xdr_set_scratch_buffer(&stream
, page_address(scratch
), PAGE_SIZE
);
387 /* stripe unit and mirror_array_cnt */
389 p
= xdr_inline_decode(&stream
, 8 + 4);
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
);
398 if (mirror_array_cnt
> NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT
||
399 mirror_array_cnt
== 0)
403 fls
= kzalloc(sizeof(*fls
), gfp_flags
);
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
)
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
;
422 p
= xdr_inline_decode(&stream
, 4);
425 ds_count
= be32_to_cpup(p
);
427 /* FIXME: allow for striping? */
431 fls
->mirror_array
[i
] = ff_layout_alloc_mirror(gfp_flags
);
432 if (fls
->mirror_array
[i
] == NULL
) {
437 fls
->mirror_array
[i
]->ds_count
= ds_count
;
440 rc
= decode_deviceid(&stream
, &fls
->mirror_array
[i
]->devid
);
446 p
= xdr_inline_decode(&stream
, 4);
449 fls
->mirror_array
[i
]->efficiency
= be32_to_cpup(p
);
452 rc
= decode_pnfs_stateid(&stream
, &fls
->mirror_array
[i
]->stateid
);
458 p
= xdr_inline_decode(&stream
, 4);
461 fh_count
= be32_to_cpup(p
);
463 fls
->mirror_array
[i
]->fh_versions
=
464 kzalloc(fh_count
* sizeof(struct nfs_fh
),
466 if (fls
->mirror_array
[i
]->fh_versions
== NULL
) {
471 for (j
= 0; j
< fh_count
; j
++) {
472 rc
= decode_nfs_fh(&stream
,
473 &fls
->mirror_array
[i
]->fh_versions
[j
]);
478 fls
->mirror_array
[i
]->fh_versions_cnt
= fh_count
;
481 rc
= decode_name(&stream
, &id
);
485 acred
.uid
= make_kuid(&init_user_ns
, id
);
488 rc
= decode_name(&stream
, &id
);
492 acred
.gid
= make_kgid(&init_user_ns
, id
);
494 /* find the cred for it */
495 rcu_assign_pointer(cred
, rpc_lookup_generic_cred(&acred
, 0, gfp_flags
));
501 if (lgr
->range
.iomode
== IOMODE_READ
)
502 rcu_assign_pointer(fls
->mirror_array
[i
]->ro_cred
, cred
);
504 rcu_assign_pointer(fls
->mirror_array
[i
]->rw_cred
, cred
);
506 mirror
= ff_layout_add_mirror(lh
, fls
->mirror_array
[i
]);
507 if (mirror
!= fls
->mirror_array
[i
]) {
508 /* swap cred ptrs so free_mirror will clean up old */
509 if (lgr
->range
.iomode
== IOMODE_READ
) {
510 cred
= xchg(&mirror
->ro_cred
, cred
);
511 rcu_assign_pointer(fls
->mirror_array
[i
]->ro_cred
, cred
);
513 cred
= xchg(&mirror
->rw_cred
, cred
);
514 rcu_assign_pointer(fls
->mirror_array
[i
]->rw_cred
, cred
);
516 ff_layout_free_mirror(fls
->mirror_array
[i
]);
517 fls
->mirror_array
[i
] = mirror
;
520 dprintk("%s: iomode %s uid %u gid %u\n", __func__
,
521 lgr
->range
.iomode
== IOMODE_READ
? "READ" : "RW",
522 from_kuid(&init_user_ns
, acred
.uid
),
523 from_kgid(&init_user_ns
, acred
.gid
));
526 p
= xdr_inline_decode(&stream
, 4);
528 goto out_sort_mirrors
;
529 fls
->flags
= be32_to_cpup(p
);
531 p
= xdr_inline_decode(&stream
, 4);
533 goto out_sort_mirrors
;
534 for (i
=0; i
< fls
->mirror_array_cnt
; i
++)
535 fls
->mirror_array
[i
]->report_interval
= be32_to_cpup(p
);
538 ff_layout_sort_mirrors(fls
);
539 rc
= ff_layout_check_layout(lgr
);
542 ret
= &fls
->generic_hdr
;
543 dprintk("<-- %s (success)\n", __func__
);
545 __free_page(scratch
);
548 _ff_layout_free_lseg(fls
);
550 dprintk("<-- %s (%d)\n", __func__
, rc
);
554 static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr
*layout
)
556 struct pnfs_layout_segment
*lseg
;
558 list_for_each_entry(lseg
, &layout
->plh_segs
, pls_list
)
559 if (lseg
->pls_range
.iomode
== IOMODE_RW
)
566 ff_layout_free_lseg(struct pnfs_layout_segment
*lseg
)
568 struct nfs4_ff_layout_segment
*fls
= FF_LAYOUT_LSEG(lseg
);
570 dprintk("--> %s\n", __func__
);
572 if (lseg
->pls_range
.iomode
== IOMODE_RW
) {
573 struct nfs4_flexfile_layout
*ffl
;
576 ffl
= FF_LAYOUT_FROM_HDR(lseg
->pls_layout
);
577 inode
= ffl
->generic_hdr
.plh_inode
;
578 spin_lock(&inode
->i_lock
);
579 if (!ff_layout_has_rw_segments(lseg
->pls_layout
)) {
580 ffl
->commit_info
.nbuckets
= 0;
581 kfree(ffl
->commit_info
.buckets
);
582 ffl
->commit_info
.buckets
= NULL
;
584 spin_unlock(&inode
->i_lock
);
586 _ff_layout_free_lseg(fls
);
589 /* Return 1 until we have multiple lsegs support */
591 ff_layout_get_lseg_count(struct nfs4_ff_layout_segment
*fls
)
597 nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer
*timer
, ktime_t now
)
599 /* first IO request? */
600 if (atomic_inc_return(&timer
->n_ops
) == 1) {
601 timer
->start_time
= now
;
606 nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer
*timer
, ktime_t now
)
610 if (atomic_dec_return(&timer
->n_ops
) < 0)
613 start
= timer
->start_time
;
614 timer
->start_time
= now
;
615 return ktime_sub(now
, start
);
619 nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror
*mirror
,
620 struct nfs4_ff_layoutstat
*layoutstat
,
623 s64 report_interval
= FF_LAYOUTSTATS_REPORT_INTERVAL
;
624 struct nfs4_flexfile_layout
*ffl
= FF_LAYOUT_FROM_HDR(mirror
->layout
);
626 nfs4_ff_start_busy_timer(&layoutstat
->busy_timer
, now
);
627 if (!mirror
->start_time
)
628 mirror
->start_time
= now
;
629 if (mirror
->report_interval
!= 0)
630 report_interval
= (s64
)mirror
->report_interval
* 1000LL;
631 else if (layoutstats_timer
!= 0)
632 report_interval
= (s64
)layoutstats_timer
* 1000LL;
633 if (ktime_to_ms(ktime_sub(now
, ffl
->last_report_time
)) >=
635 ffl
->last_report_time
= now
;
643 nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat
*layoutstat
,
646 struct nfs4_ff_io_stat
*iostat
= &layoutstat
->io_stat
;
648 iostat
->ops_requested
++;
649 iostat
->bytes_requested
+= requested
;
653 nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat
*layoutstat
,
656 ktime_t time_completed
,
657 ktime_t time_started
)
659 struct nfs4_ff_io_stat
*iostat
= &layoutstat
->io_stat
;
660 ktime_t completion_time
= ktime_sub(time_completed
, time_started
);
663 iostat
->ops_completed
++;
664 iostat
->bytes_completed
+= completed
;
665 iostat
->bytes_not_delivered
+= requested
- completed
;
667 timer
= nfs4_ff_end_busy_timer(&layoutstat
->busy_timer
, time_completed
);
668 iostat
->total_busy_time
=
669 ktime_add(iostat
->total_busy_time
, timer
);
670 iostat
->aggregate_completion_time
=
671 ktime_add(iostat
->aggregate_completion_time
,
676 nfs4_ff_layout_stat_io_start_read(struct inode
*inode
,
677 struct nfs4_ff_layout_mirror
*mirror
,
678 __u64 requested
, ktime_t now
)
682 spin_lock(&mirror
->lock
);
683 report
= nfs4_ff_layoutstat_start_io(mirror
, &mirror
->read_stat
, now
);
684 nfs4_ff_layout_stat_io_update_requested(&mirror
->read_stat
, requested
);
685 set_bit(NFS4_FF_MIRROR_STAT_AVAIL
, &mirror
->flags
);
686 spin_unlock(&mirror
->lock
);
689 pnfs_report_layoutstat(inode
, GFP_KERNEL
);
693 nfs4_ff_layout_stat_io_end_read(struct rpc_task
*task
,
694 struct nfs4_ff_layout_mirror
*mirror
,
698 spin_lock(&mirror
->lock
);
699 nfs4_ff_layout_stat_io_update_completed(&mirror
->read_stat
,
700 requested
, completed
,
701 ktime_get(), task
->tk_start
);
702 set_bit(NFS4_FF_MIRROR_STAT_AVAIL
, &mirror
->flags
);
703 spin_unlock(&mirror
->lock
);
707 nfs4_ff_layout_stat_io_start_write(struct inode
*inode
,
708 struct nfs4_ff_layout_mirror
*mirror
,
709 __u64 requested
, ktime_t now
)
713 spin_lock(&mirror
->lock
);
714 report
= nfs4_ff_layoutstat_start_io(mirror
, &mirror
->write_stat
, now
);
715 nfs4_ff_layout_stat_io_update_requested(&mirror
->write_stat
, requested
);
716 set_bit(NFS4_FF_MIRROR_STAT_AVAIL
, &mirror
->flags
);
717 spin_unlock(&mirror
->lock
);
720 pnfs_report_layoutstat(inode
, GFP_NOIO
);
724 nfs4_ff_layout_stat_io_end_write(struct rpc_task
*task
,
725 struct nfs4_ff_layout_mirror
*mirror
,
728 enum nfs3_stable_how committed
)
730 if (committed
== NFS_UNSTABLE
)
731 requested
= completed
= 0;
733 spin_lock(&mirror
->lock
);
734 nfs4_ff_layout_stat_io_update_completed(&mirror
->write_stat
,
735 requested
, completed
, ktime_get(), task
->tk_start
);
736 set_bit(NFS4_FF_MIRROR_STAT_AVAIL
, &mirror
->flags
);
737 spin_unlock(&mirror
->lock
);
741 ff_layout_alloc_commit_info(struct pnfs_layout_segment
*lseg
,
742 struct nfs_commit_info
*cinfo
,
745 struct nfs4_ff_layout_segment
*fls
= FF_LAYOUT_LSEG(lseg
);
746 struct pnfs_commit_bucket
*buckets
;
749 if (cinfo
->ds
->nbuckets
!= 0) {
750 /* This assumes there is only one RW lseg per file.
751 * To support multiple lseg per file, we need to
752 * change struct pnfs_commit_bucket to allow dynamic
753 * increasing nbuckets.
758 size
= ff_layout_get_lseg_count(fls
) * FF_LAYOUT_MIRROR_COUNT(lseg
);
760 buckets
= kcalloc(size
, sizeof(struct pnfs_commit_bucket
),
767 spin_lock(&cinfo
->inode
->i_lock
);
768 if (cinfo
->ds
->nbuckets
!= 0)
771 cinfo
->ds
->buckets
= buckets
;
772 cinfo
->ds
->nbuckets
= size
;
773 for (i
= 0; i
< size
; i
++) {
774 INIT_LIST_HEAD(&buckets
[i
].written
);
775 INIT_LIST_HEAD(&buckets
[i
].committing
);
776 /* mark direct verifier as unset */
777 buckets
[i
].direct_verf
.committed
=
778 NFS_INVALID_STABLE_HOW
;
781 spin_unlock(&cinfo
->inode
->i_lock
);
786 static struct nfs4_pnfs_ds
*
787 ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment
*lseg
,
791 struct nfs4_ff_layout_segment
*fls
= FF_LAYOUT_LSEG(lseg
);
792 struct nfs4_pnfs_ds
*ds
;
793 bool fail_return
= false;
796 /* mirrors are sorted by efficiency */
797 for (idx
= start_idx
; idx
< fls
->mirror_array_cnt
; idx
++) {
798 if (idx
+1 == fls
->mirror_array_cnt
)
800 ds
= nfs4_ff_layout_prepare_ds(lseg
, idx
, fail_return
);
811 ff_layout_pg_get_read(struct nfs_pageio_descriptor
*pgio
,
812 struct nfs_page
*req
,
816 pnfs_put_lseg(pgio
->pg_lseg
);
817 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
824 if (IS_ERR(pgio
->pg_lseg
)) {
825 pgio
->pg_error
= PTR_ERR(pgio
->pg_lseg
);
826 pgio
->pg_lseg
= NULL
;
829 /* If we don't have checking, do get a IOMODE_RW
830 * segment, and the server wants to avoid READs
833 if (pgio
->pg_lseg
&& !strict_iomode
&&
834 ff_layout_avoid_read_on_rw(pgio
->pg_lseg
)) {
835 strict_iomode
= true;
841 ff_layout_pg_init_read(struct nfs_pageio_descriptor
*pgio
,
842 struct nfs_page
*req
)
844 struct nfs_pgio_mirror
*pgm
;
845 struct nfs4_ff_layout_mirror
*mirror
;
846 struct nfs4_pnfs_ds
*ds
;
850 pnfs_generic_pg_check_layout(pgio
);
851 /* Use full layout for now */
853 ff_layout_pg_get_read(pgio
, req
, false);
854 else if (ff_layout_avoid_read_on_rw(pgio
->pg_lseg
))
855 ff_layout_pg_get_read(pgio
, req
, true);
857 /* If no lseg, fall back to read through mds */
858 if (pgio
->pg_lseg
== NULL
)
861 ds
= ff_layout_choose_best_ds_for_read(pgio
->pg_lseg
, 0, &ds_idx
);
863 if (!ff_layout_no_fallback_to_mds(pgio
->pg_lseg
))
865 pnfs_put_lseg(pgio
->pg_lseg
);
866 pgio
->pg_lseg
= NULL
;
867 /* Sleep for 1 second before retrying */
872 mirror
= FF_LAYOUT_COMP(pgio
->pg_lseg
, ds_idx
);
874 pgio
->pg_mirror_idx
= ds_idx
;
876 /* read always uses only one mirror - idx 0 for pgio layer */
877 pgm
= &pgio
->pg_mirrors
[0];
878 pgm
->pg_bsize
= mirror
->mirror_ds
->ds_versions
[0].rsize
;
882 pnfs_put_lseg(pgio
->pg_lseg
);
883 pgio
->pg_lseg
= NULL
;
884 nfs_pageio_reset_read_mds(pgio
);
888 ff_layout_pg_init_write(struct nfs_pageio_descriptor
*pgio
,
889 struct nfs_page
*req
)
891 struct nfs4_ff_layout_mirror
*mirror
;
892 struct nfs_pgio_mirror
*pgm
;
893 struct nfs_commit_info cinfo
;
894 struct nfs4_pnfs_ds
*ds
;
899 pnfs_generic_pg_check_layout(pgio
);
900 if (!pgio
->pg_lseg
) {
901 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
908 if (IS_ERR(pgio
->pg_lseg
)) {
909 pgio
->pg_error
= PTR_ERR(pgio
->pg_lseg
);
910 pgio
->pg_lseg
= NULL
;
914 /* If no lseg, fall back to write through mds */
915 if (pgio
->pg_lseg
== NULL
)
918 nfs_init_cinfo(&cinfo
, pgio
->pg_inode
, pgio
->pg_dreq
);
919 status
= ff_layout_alloc_commit_info(pgio
->pg_lseg
, &cinfo
, GFP_NOFS
);
923 /* Use a direct mapping of ds_idx to pgio mirror_idx */
924 if (WARN_ON_ONCE(pgio
->pg_mirror_count
!=
925 FF_LAYOUT_MIRROR_COUNT(pgio
->pg_lseg
)))
928 for (i
= 0; i
< pgio
->pg_mirror_count
; i
++) {
929 ds
= nfs4_ff_layout_prepare_ds(pgio
->pg_lseg
, i
, true);
931 if (!ff_layout_no_fallback_to_mds(pgio
->pg_lseg
))
933 pnfs_put_lseg(pgio
->pg_lseg
);
934 pgio
->pg_lseg
= NULL
;
935 /* Sleep for 1 second before retrying */
939 pgm
= &pgio
->pg_mirrors
[i
];
940 mirror
= FF_LAYOUT_COMP(pgio
->pg_lseg
, i
);
941 pgm
->pg_bsize
= mirror
->mirror_ds
->ds_versions
[0].wsize
;
947 pnfs_put_lseg(pgio
->pg_lseg
);
948 pgio
->pg_lseg
= NULL
;
949 nfs_pageio_reset_write_mds(pgio
);
953 ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor
*pgio
,
954 struct nfs_page
*req
)
956 if (!pgio
->pg_lseg
) {
957 pgio
->pg_lseg
= pnfs_update_layout(pgio
->pg_inode
,
964 if (IS_ERR(pgio
->pg_lseg
)) {
965 pgio
->pg_error
= PTR_ERR(pgio
->pg_lseg
);
966 pgio
->pg_lseg
= NULL
;
971 return FF_LAYOUT_MIRROR_COUNT(pgio
->pg_lseg
);
973 /* no lseg means that pnfs is not in use, so no mirroring here */
974 nfs_pageio_reset_write_mds(pgio
);
979 static const struct nfs_pageio_ops ff_layout_pg_read_ops
= {
980 .pg_init
= ff_layout_pg_init_read
,
981 .pg_test
= pnfs_generic_pg_test
,
982 .pg_doio
= pnfs_generic_pg_readpages
,
983 .pg_cleanup
= pnfs_generic_pg_cleanup
,
986 static const struct nfs_pageio_ops ff_layout_pg_write_ops
= {
987 .pg_init
= ff_layout_pg_init_write
,
988 .pg_test
= pnfs_generic_pg_test
,
989 .pg_doio
= pnfs_generic_pg_writepages
,
990 .pg_get_mirror_count
= ff_layout_pg_get_mirror_count_write
,
991 .pg_cleanup
= pnfs_generic_pg_cleanup
,
994 static void ff_layout_reset_write(struct nfs_pgio_header
*hdr
, bool retry_pnfs
)
996 struct rpc_task
*task
= &hdr
->task
;
998 pnfs_layoutcommit_inode(hdr
->inode
, false);
1001 dprintk("%s Reset task %5u for i/o through pNFS "
1002 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__
,
1004 hdr
->inode
->i_sb
->s_id
,
1005 (unsigned long long)NFS_FILEID(hdr
->inode
),
1007 (unsigned long long)hdr
->args
.offset
);
1009 hdr
->completion_ops
->reschedule_io(hdr
);
1013 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
)) {
1014 dprintk("%s Reset task %5u for i/o through MDS "
1015 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__
,
1017 hdr
->inode
->i_sb
->s_id
,
1018 (unsigned long long)NFS_FILEID(hdr
->inode
),
1020 (unsigned long long)hdr
->args
.offset
);
1022 task
->tk_status
= pnfs_write_done_resend_to_mds(hdr
);
1026 static void ff_layout_reset_read(struct nfs_pgio_header
*hdr
)
1028 struct rpc_task
*task
= &hdr
->task
;
1030 pnfs_layoutcommit_inode(hdr
->inode
, false);
1032 if (!test_and_set_bit(NFS_IOHDR_REDO
, &hdr
->flags
)) {
1033 dprintk("%s Reset task %5u for i/o through MDS "
1034 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__
,
1036 hdr
->inode
->i_sb
->s_id
,
1037 (unsigned long long)NFS_FILEID(hdr
->inode
),
1039 (unsigned long long)hdr
->args
.offset
);
1041 task
->tk_status
= pnfs_read_done_resend_to_mds(hdr
);
1045 static int ff_layout_async_handle_error_v4(struct rpc_task
*task
,
1046 struct nfs4_state
*state
,
1047 struct nfs_client
*clp
,
1048 struct pnfs_layout_segment
*lseg
,
1051 struct pnfs_layout_hdr
*lo
= lseg
->pls_layout
;
1052 struct inode
*inode
= lo
->plh_inode
;
1053 struct nfs4_deviceid_node
*devid
= FF_LAYOUT_DEVID_NODE(lseg
, idx
);
1054 struct nfs4_slot_table
*tbl
= &clp
->cl_session
->fc_slot_table
;
1056 switch (task
->tk_status
) {
1057 case -NFS4ERR_BADSESSION
:
1058 case -NFS4ERR_BADSLOT
:
1059 case -NFS4ERR_BAD_HIGH_SLOT
:
1060 case -NFS4ERR_DEADSESSION
:
1061 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1062 case -NFS4ERR_SEQ_FALSE_RETRY
:
1063 case -NFS4ERR_SEQ_MISORDERED
:
1064 dprintk("%s ERROR %d, Reset session. Exchangeid "
1065 "flags 0x%x\n", __func__
, task
->tk_status
,
1066 clp
->cl_exchange_flags
);
1067 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
1069 case -NFS4ERR_DELAY
:
1070 case -NFS4ERR_GRACE
:
1071 rpc_delay(task
, FF_LAYOUT_POLL_RETRY_MAX
);
1073 case -NFS4ERR_RETRY_UNCACHED_REP
:
1075 /* Invalidate Layout errors */
1076 case -NFS4ERR_PNFS_NO_LAYOUT
:
1077 case -ESTALE
: /* mapped NFS4ERR_STALE */
1078 case -EBADHANDLE
: /* mapped NFS4ERR_BADHANDLE */
1079 case -EISDIR
: /* mapped NFS4ERR_ISDIR */
1080 case -NFS4ERR_FHEXPIRED
:
1081 case -NFS4ERR_WRONG_TYPE
:
1082 dprintk("%s Invalid layout error %d\n", __func__
,
1085 * Destroy layout so new i/o will get a new layout.
1086 * Layout will not be destroyed until all current lseg
1087 * references are put. Mark layout as invalid to resend failed
1088 * i/o and all i/o waiting on the slot table to the MDS until
1089 * layout is destroyed and a new valid layout is obtained.
1091 pnfs_destroy_layout(NFS_I(inode
));
1092 rpc_wake_up(&tbl
->slot_tbl_waitq
);
1094 /* RPC connection errors */
1102 dprintk("%s DS connection error %d\n", __func__
,
1104 nfs4_delete_deviceid(devid
->ld
, devid
->nfs_client
,
1106 rpc_wake_up(&tbl
->slot_tbl_waitq
);
1109 if (ff_layout_avoid_mds_available_ds(lseg
))
1110 return -NFS4ERR_RESET_TO_PNFS
;
1112 dprintk("%s Retry through MDS. Error %d\n", __func__
,
1114 return -NFS4ERR_RESET_TO_MDS
;
1116 task
->tk_status
= 0;
1120 /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1121 static int ff_layout_async_handle_error_v3(struct rpc_task
*task
,
1122 struct pnfs_layout_segment
*lseg
,
1125 struct nfs4_deviceid_node
*devid
= FF_LAYOUT_DEVID_NODE(lseg
, idx
);
1127 switch (task
->tk_status
) {
1128 /* File access problems. Don't mark the device as unavailable */
1137 nfs_inc_stats(lseg
->pls_layout
->plh_inode
, NFSIOS_DELAY
);
1140 dprintk("%s DS connection error %d\n", __func__
,
1142 nfs4_delete_deviceid(devid
->ld
, devid
->nfs_client
,
1145 /* FIXME: Need to prevent infinite looping here. */
1146 return -NFS4ERR_RESET_TO_PNFS
;
1148 task
->tk_status
= 0;
1149 rpc_restart_call_prepare(task
);
1150 rpc_delay(task
, NFS_JUKEBOX_RETRY_TIME
);
1154 static int ff_layout_async_handle_error(struct rpc_task
*task
,
1155 struct nfs4_state
*state
,
1156 struct nfs_client
*clp
,
1157 struct pnfs_layout_segment
*lseg
,
1160 int vers
= clp
->cl_nfs_mod
->rpc_vers
->number
;
1162 if (task
->tk_status
>= 0)
1165 /* Handle the case of an invalid layout segment */
1166 if (!pnfs_is_valid_lseg(lseg
))
1167 return -NFS4ERR_RESET_TO_PNFS
;
1171 return ff_layout_async_handle_error_v3(task
, lseg
, idx
);
1173 return ff_layout_async_handle_error_v4(task
, state
, clp
,
1176 /* should never happen */
1182 static void ff_layout_io_track_ds_error(struct pnfs_layout_segment
*lseg
,
1183 int idx
, u64 offset
, u64 length
,
1184 u32 status
, int opnum
, int error
)
1186 struct nfs4_ff_layout_mirror
*mirror
;
1193 case -EPROTONOSUPPORT
:
1204 status
= NFS4ERR_NXIO
;
1207 status
= NFS4ERR_ACCESS
;
1222 mirror
= FF_LAYOUT_COMP(lseg
, idx
);
1223 err
= ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg
->pls_layout
),
1224 mirror
, offset
, length
, status
, opnum
,
1226 pnfs_error_mark_layout_for_return(lseg
->pls_layout
->plh_inode
, lseg
);
1227 dprintk("%s: err %d op %d status %u\n", __func__
, err
, opnum
, status
);
1230 /* NFS_PROTO call done callback routines */
1231 static int ff_layout_read_done_cb(struct rpc_task
*task
,
1232 struct nfs_pgio_header
*hdr
)
1236 trace_nfs4_pnfs_read(hdr
, task
->tk_status
);
1237 if (task
->tk_status
< 0)
1238 ff_layout_io_track_ds_error(hdr
->lseg
, hdr
->pgio_mirror_idx
,
1239 hdr
->args
.offset
, hdr
->args
.count
,
1240 hdr
->res
.op_status
, OP_READ
,
1242 err
= ff_layout_async_handle_error(task
, hdr
->args
.context
->state
,
1243 hdr
->ds_clp
, hdr
->lseg
,
1244 hdr
->pgio_mirror_idx
);
1247 case -NFS4ERR_RESET_TO_PNFS
:
1248 if (ff_layout_choose_best_ds_for_read(hdr
->lseg
,
1249 hdr
->pgio_mirror_idx
+ 1,
1250 &hdr
->pgio_mirror_idx
))
1252 ff_layout_read_record_layoutstats_done(task
, hdr
);
1253 pnfs_read_resend_pnfs(hdr
);
1254 return task
->tk_status
;
1255 case -NFS4ERR_RESET_TO_MDS
:
1256 ff_layout_reset_read(hdr
);
1257 return task
->tk_status
;
1264 rpc_restart_call_prepare(task
);
1269 ff_layout_need_layoutcommit(struct pnfs_layout_segment
*lseg
)
1271 return !(FF_LAYOUT_LSEG(lseg
)->flags
& FF_FLAGS_NO_LAYOUTCOMMIT
);
1275 * We reference the rpc_cred of the first WRITE that triggers the need for
1276 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1277 * rfc5661 is not clear about which credential should be used.
1279 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1280 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1281 * we always send layoutcommit after DS writes.
1284 ff_layout_set_layoutcommit(struct inode
*inode
,
1285 struct pnfs_layout_segment
*lseg
,
1288 if (!ff_layout_need_layoutcommit(lseg
))
1291 pnfs_set_layoutcommit(inode
, lseg
, end_offset
);
1292 dprintk("%s inode %lu pls_end_pos %llu\n", __func__
, inode
->i_ino
,
1293 (unsigned long long) NFS_I(inode
)->layout
->plh_lwb
);
1297 ff_layout_device_unavailable(struct pnfs_layout_segment
*lseg
, int idx
)
1299 /* No mirroring for now */
1300 struct nfs4_deviceid_node
*node
= FF_LAYOUT_DEVID_NODE(lseg
, idx
);
1302 return ff_layout_test_devid_unavailable(node
);
1305 static void ff_layout_read_record_layoutstats_start(struct rpc_task
*task
,
1306 struct nfs_pgio_header
*hdr
)
1308 if (test_and_set_bit(NFS_IOHDR_STAT
, &hdr
->flags
))
1310 nfs4_ff_layout_stat_io_start_read(hdr
->inode
,
1311 FF_LAYOUT_COMP(hdr
->lseg
, hdr
->pgio_mirror_idx
),
1316 static void ff_layout_read_record_layoutstats_done(struct rpc_task
*task
,
1317 struct nfs_pgio_header
*hdr
)
1319 if (!test_and_clear_bit(NFS_IOHDR_STAT
, &hdr
->flags
))
1321 nfs4_ff_layout_stat_io_end_read(task
,
1322 FF_LAYOUT_COMP(hdr
->lseg
, hdr
->pgio_mirror_idx
),
1327 static int ff_layout_read_prepare_common(struct rpc_task
*task
,
1328 struct nfs_pgio_header
*hdr
)
1330 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
))) {
1331 rpc_exit(task
, -EIO
);
1334 if (ff_layout_device_unavailable(hdr
->lseg
, hdr
->pgio_mirror_idx
)) {
1335 rpc_exit(task
, -EHOSTDOWN
);
1339 ff_layout_read_record_layoutstats_start(task
, hdr
);
1344 * Call ops for the async read/write cases
1345 * In the case of dense layouts, the offset needs to be reset to its
1348 static void ff_layout_read_prepare_v3(struct rpc_task
*task
, void *data
)
1350 struct nfs_pgio_header
*hdr
= data
;
1352 if (ff_layout_read_prepare_common(task
, hdr
))
1355 rpc_call_start(task
);
1358 static void ff_layout_read_prepare_v4(struct rpc_task
*task
, void *data
)
1360 struct nfs_pgio_header
*hdr
= data
;
1362 if (nfs4_setup_sequence(hdr
->ds_clp
,
1363 &hdr
->args
.seq_args
,
1368 if (ff_layout_read_prepare_common(task
, hdr
))
1371 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
1372 hdr
->args
.lock_context
, FMODE_READ
) == -EIO
)
1373 rpc_exit(task
, -EIO
); /* lost lock, terminate I/O */
1376 static void ff_layout_read_call_done(struct rpc_task
*task
, void *data
)
1378 struct nfs_pgio_header
*hdr
= data
;
1380 dprintk("--> %s task->tk_status %d\n", __func__
, task
->tk_status
);
1382 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
) &&
1383 task
->tk_status
== 0) {
1384 nfs4_sequence_done(task
, &hdr
->res
.seq_res
);
1388 /* Note this may cause RPC to be resent */
1389 hdr
->mds_ops
->rpc_call_done(task
, hdr
);
1392 static void ff_layout_read_count_stats(struct rpc_task
*task
, void *data
)
1394 struct nfs_pgio_header
*hdr
= data
;
1396 ff_layout_read_record_layoutstats_done(task
, hdr
);
1397 rpc_count_iostats_metrics(task
,
1398 &NFS_CLIENT(hdr
->inode
)->cl_metrics
[NFSPROC4_CLNT_READ
]);
1401 static void ff_layout_read_release(void *data
)
1403 struct nfs_pgio_header
*hdr
= data
;
1405 ff_layout_read_record_layoutstats_done(&hdr
->task
, hdr
);
1406 pnfs_generic_rw_release(data
);
1410 static int ff_layout_write_done_cb(struct rpc_task
*task
,
1411 struct nfs_pgio_header
*hdr
)
1413 loff_t end_offs
= 0;
1416 trace_nfs4_pnfs_write(hdr
, task
->tk_status
);
1417 if (task
->tk_status
< 0)
1418 ff_layout_io_track_ds_error(hdr
->lseg
, hdr
->pgio_mirror_idx
,
1419 hdr
->args
.offset
, hdr
->args
.count
,
1420 hdr
->res
.op_status
, OP_WRITE
,
1422 err
= ff_layout_async_handle_error(task
, hdr
->args
.context
->state
,
1423 hdr
->ds_clp
, hdr
->lseg
,
1424 hdr
->pgio_mirror_idx
);
1427 case -NFS4ERR_RESET_TO_PNFS
:
1428 ff_layout_reset_write(hdr
, true);
1429 return task
->tk_status
;
1430 case -NFS4ERR_RESET_TO_MDS
:
1431 ff_layout_reset_write(hdr
, false);
1432 return task
->tk_status
;
1437 if (hdr
->res
.verf
->committed
== NFS_FILE_SYNC
||
1438 hdr
->res
.verf
->committed
== NFS_DATA_SYNC
)
1439 end_offs
= hdr
->mds_offset
+ (loff_t
)hdr
->res
.count
;
1441 /* Note: if the write is unstable, don't set end_offs until commit */
1442 ff_layout_set_layoutcommit(hdr
->inode
, hdr
->lseg
, end_offs
);
1444 /* zero out fattr since we don't care DS attr at all */
1445 hdr
->fattr
.valid
= 0;
1446 if (task
->tk_status
>= 0)
1447 nfs_writeback_update_inode(hdr
);
1452 static int ff_layout_commit_done_cb(struct rpc_task
*task
,
1453 struct nfs_commit_data
*data
)
1457 trace_nfs4_pnfs_commit_ds(data
, task
->tk_status
);
1458 if (task
->tk_status
< 0)
1459 ff_layout_io_track_ds_error(data
->lseg
, data
->ds_commit_index
,
1460 data
->args
.offset
, data
->args
.count
,
1461 data
->res
.op_status
, OP_COMMIT
,
1463 err
= ff_layout_async_handle_error(task
, NULL
, data
->ds_clp
,
1464 data
->lseg
, data
->ds_commit_index
);
1467 case -NFS4ERR_RESET_TO_PNFS
:
1468 pnfs_generic_prepare_to_resend_writes(data
);
1470 case -NFS4ERR_RESET_TO_MDS
:
1471 pnfs_generic_prepare_to_resend_writes(data
);
1474 rpc_restart_call_prepare(task
);
1478 ff_layout_set_layoutcommit(data
->inode
, data
->lseg
, data
->lwb
);
1483 static void ff_layout_write_record_layoutstats_start(struct rpc_task
*task
,
1484 struct nfs_pgio_header
*hdr
)
1486 if (test_and_set_bit(NFS_IOHDR_STAT
, &hdr
->flags
))
1488 nfs4_ff_layout_stat_io_start_write(hdr
->inode
,
1489 FF_LAYOUT_COMP(hdr
->lseg
, hdr
->pgio_mirror_idx
),
1494 static void ff_layout_write_record_layoutstats_done(struct rpc_task
*task
,
1495 struct nfs_pgio_header
*hdr
)
1497 if (!test_and_clear_bit(NFS_IOHDR_STAT
, &hdr
->flags
))
1499 nfs4_ff_layout_stat_io_end_write(task
,
1500 FF_LAYOUT_COMP(hdr
->lseg
, hdr
->pgio_mirror_idx
),
1501 hdr
->args
.count
, hdr
->res
.count
,
1502 hdr
->res
.verf
->committed
);
1505 static int ff_layout_write_prepare_common(struct rpc_task
*task
,
1506 struct nfs_pgio_header
*hdr
)
1508 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
))) {
1509 rpc_exit(task
, -EIO
);
1513 if (ff_layout_device_unavailable(hdr
->lseg
, hdr
->pgio_mirror_idx
)) {
1514 rpc_exit(task
, -EHOSTDOWN
);
1518 ff_layout_write_record_layoutstats_start(task
, hdr
);
1522 static void ff_layout_write_prepare_v3(struct rpc_task
*task
, void *data
)
1524 struct nfs_pgio_header
*hdr
= data
;
1526 if (ff_layout_write_prepare_common(task
, hdr
))
1529 rpc_call_start(task
);
1532 static void ff_layout_write_prepare_v4(struct rpc_task
*task
, void *data
)
1534 struct nfs_pgio_header
*hdr
= data
;
1536 if (nfs4_setup_sequence(hdr
->ds_clp
,
1537 &hdr
->args
.seq_args
,
1542 if (ff_layout_write_prepare_common(task
, hdr
))
1545 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
1546 hdr
->args
.lock_context
, FMODE_WRITE
) == -EIO
)
1547 rpc_exit(task
, -EIO
); /* lost lock, terminate I/O */
1550 static void ff_layout_write_call_done(struct rpc_task
*task
, void *data
)
1552 struct nfs_pgio_header
*hdr
= data
;
1554 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
) &&
1555 task
->tk_status
== 0) {
1556 nfs4_sequence_done(task
, &hdr
->res
.seq_res
);
1560 /* Note this may cause RPC to be resent */
1561 hdr
->mds_ops
->rpc_call_done(task
, hdr
);
1564 static void ff_layout_write_count_stats(struct rpc_task
*task
, void *data
)
1566 struct nfs_pgio_header
*hdr
= data
;
1568 ff_layout_write_record_layoutstats_done(task
, hdr
);
1569 rpc_count_iostats_metrics(task
,
1570 &NFS_CLIENT(hdr
->inode
)->cl_metrics
[NFSPROC4_CLNT_WRITE
]);
1573 static void ff_layout_write_release(void *data
)
1575 struct nfs_pgio_header
*hdr
= data
;
1577 ff_layout_write_record_layoutstats_done(&hdr
->task
, hdr
);
1578 pnfs_generic_rw_release(data
);
1581 static void ff_layout_commit_record_layoutstats_start(struct rpc_task
*task
,
1582 struct nfs_commit_data
*cdata
)
1584 if (test_and_set_bit(NFS_IOHDR_STAT
, &cdata
->flags
))
1586 nfs4_ff_layout_stat_io_start_write(cdata
->inode
,
1587 FF_LAYOUT_COMP(cdata
->lseg
, cdata
->ds_commit_index
),
1591 static void ff_layout_commit_record_layoutstats_done(struct rpc_task
*task
,
1592 struct nfs_commit_data
*cdata
)
1594 struct nfs_page
*req
;
1597 if (!test_and_clear_bit(NFS_IOHDR_STAT
, &cdata
->flags
))
1600 if (task
->tk_status
== 0) {
1601 list_for_each_entry(req
, &cdata
->pages
, wb_list
)
1602 count
+= req
->wb_bytes
;
1604 nfs4_ff_layout_stat_io_end_write(task
,
1605 FF_LAYOUT_COMP(cdata
->lseg
, cdata
->ds_commit_index
),
1606 count
, count
, NFS_FILE_SYNC
);
1609 static void ff_layout_commit_prepare_common(struct rpc_task
*task
,
1610 struct nfs_commit_data
*cdata
)
1612 ff_layout_commit_record_layoutstats_start(task
, cdata
);
1615 static void ff_layout_commit_prepare_v3(struct rpc_task
*task
, void *data
)
1617 ff_layout_commit_prepare_common(task
, data
);
1618 rpc_call_start(task
);
1621 static void ff_layout_commit_prepare_v4(struct rpc_task
*task
, void *data
)
1623 struct nfs_commit_data
*wdata
= data
;
1625 if (nfs4_setup_sequence(wdata
->ds_clp
,
1626 &wdata
->args
.seq_args
,
1627 &wdata
->res
.seq_res
,
1630 ff_layout_commit_prepare_common(task
, data
);
1633 static void ff_layout_commit_done(struct rpc_task
*task
, void *data
)
1635 pnfs_generic_write_commit_done(task
, data
);
1638 static void ff_layout_commit_count_stats(struct rpc_task
*task
, void *data
)
1640 struct nfs_commit_data
*cdata
= data
;
1642 ff_layout_commit_record_layoutstats_done(task
, cdata
);
1643 rpc_count_iostats_metrics(task
,
1644 &NFS_CLIENT(cdata
->inode
)->cl_metrics
[NFSPROC4_CLNT_COMMIT
]);
1647 static void ff_layout_commit_release(void *data
)
1649 struct nfs_commit_data
*cdata
= data
;
1651 ff_layout_commit_record_layoutstats_done(&cdata
->task
, cdata
);
1652 pnfs_generic_commit_release(data
);
1655 static const struct rpc_call_ops ff_layout_read_call_ops_v3
= {
1656 .rpc_call_prepare
= ff_layout_read_prepare_v3
,
1657 .rpc_call_done
= ff_layout_read_call_done
,
1658 .rpc_count_stats
= ff_layout_read_count_stats
,
1659 .rpc_release
= ff_layout_read_release
,
1662 static const struct rpc_call_ops ff_layout_read_call_ops_v4
= {
1663 .rpc_call_prepare
= ff_layout_read_prepare_v4
,
1664 .rpc_call_done
= ff_layout_read_call_done
,
1665 .rpc_count_stats
= ff_layout_read_count_stats
,
1666 .rpc_release
= ff_layout_read_release
,
1669 static const struct rpc_call_ops ff_layout_write_call_ops_v3
= {
1670 .rpc_call_prepare
= ff_layout_write_prepare_v3
,
1671 .rpc_call_done
= ff_layout_write_call_done
,
1672 .rpc_count_stats
= ff_layout_write_count_stats
,
1673 .rpc_release
= ff_layout_write_release
,
1676 static const struct rpc_call_ops ff_layout_write_call_ops_v4
= {
1677 .rpc_call_prepare
= ff_layout_write_prepare_v4
,
1678 .rpc_call_done
= ff_layout_write_call_done
,
1679 .rpc_count_stats
= ff_layout_write_count_stats
,
1680 .rpc_release
= ff_layout_write_release
,
1683 static const struct rpc_call_ops ff_layout_commit_call_ops_v3
= {
1684 .rpc_call_prepare
= ff_layout_commit_prepare_v3
,
1685 .rpc_call_done
= ff_layout_commit_done
,
1686 .rpc_count_stats
= ff_layout_commit_count_stats
,
1687 .rpc_release
= ff_layout_commit_release
,
1690 static const struct rpc_call_ops ff_layout_commit_call_ops_v4
= {
1691 .rpc_call_prepare
= ff_layout_commit_prepare_v4
,
1692 .rpc_call_done
= ff_layout_commit_done
,
1693 .rpc_count_stats
= ff_layout_commit_count_stats
,
1694 .rpc_release
= ff_layout_commit_release
,
1697 static enum pnfs_try_status
1698 ff_layout_read_pagelist(struct nfs_pgio_header
*hdr
)
1700 struct pnfs_layout_segment
*lseg
= hdr
->lseg
;
1701 struct nfs4_pnfs_ds
*ds
;
1702 struct rpc_clnt
*ds_clnt
;
1703 struct rpc_cred
*ds_cred
;
1704 loff_t offset
= hdr
->args
.offset
;
1705 u32 idx
= hdr
->pgio_mirror_idx
;
1709 dprintk("--> %s ino %lu pgbase %u req %zu@%llu\n",
1710 __func__
, hdr
->inode
->i_ino
,
1711 hdr
->args
.pgbase
, (size_t)hdr
->args
.count
, offset
);
1713 ds
= nfs4_ff_layout_prepare_ds(lseg
, idx
, false);
1717 ds_clnt
= nfs4_ff_find_or_create_ds_client(lseg
, idx
, ds
->ds_clp
,
1719 if (IS_ERR(ds_clnt
))
1722 ds_cred
= ff_layout_get_ds_cred(lseg
, idx
, hdr
->cred
);
1726 vers
= nfs4_ff_layout_ds_version(lseg
, idx
);
1728 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__
,
1729 ds
->ds_remotestr
, refcount_read(&ds
->ds_clp
->cl_count
), vers
);
1731 hdr
->pgio_done_cb
= ff_layout_read_done_cb
;
1732 refcount_inc(&ds
->ds_clp
->cl_count
);
1733 hdr
->ds_clp
= ds
->ds_clp
;
1734 fh
= nfs4_ff_layout_select_ds_fh(lseg
, idx
);
1738 * Note that if we ever decide to split across DSes,
1739 * then we may need to handle dense-like offsets.
1741 hdr
->args
.offset
= offset
;
1742 hdr
->mds_offset
= offset
;
1744 /* Perform an asynchronous read to ds */
1745 nfs_initiate_pgio(ds_clnt
, hdr
, ds_cred
, ds
->ds_clp
->rpc_ops
,
1746 vers
== 3 ? &ff_layout_read_call_ops_v3
:
1747 &ff_layout_read_call_ops_v4
,
1748 0, RPC_TASK_SOFTCONN
);
1749 put_rpccred(ds_cred
);
1750 return PNFS_ATTEMPTED
;
1753 if (ff_layout_avoid_mds_available_ds(lseg
))
1754 return PNFS_TRY_AGAIN
;
1755 return PNFS_NOT_ATTEMPTED
;
1758 /* Perform async writes. */
1759 static enum pnfs_try_status
1760 ff_layout_write_pagelist(struct nfs_pgio_header
*hdr
, int sync
)
1762 struct pnfs_layout_segment
*lseg
= hdr
->lseg
;
1763 struct nfs4_pnfs_ds
*ds
;
1764 struct rpc_clnt
*ds_clnt
;
1765 struct rpc_cred
*ds_cred
;
1766 loff_t offset
= hdr
->args
.offset
;
1769 int idx
= hdr
->pgio_mirror_idx
;
1771 ds
= nfs4_ff_layout_prepare_ds(lseg
, idx
, true);
1775 ds_clnt
= nfs4_ff_find_or_create_ds_client(lseg
, idx
, ds
->ds_clp
,
1777 if (IS_ERR(ds_clnt
))
1780 ds_cred
= ff_layout_get_ds_cred(lseg
, idx
, hdr
->cred
);
1784 vers
= nfs4_ff_layout_ds_version(lseg
, idx
);
1786 dprintk("%s ino %lu sync %d req %zu@%llu DS: %s cl_count %d vers %d\n",
1787 __func__
, hdr
->inode
->i_ino
, sync
, (size_t) hdr
->args
.count
,
1788 offset
, ds
->ds_remotestr
, refcount_read(&ds
->ds_clp
->cl_count
),
1791 hdr
->pgio_done_cb
= ff_layout_write_done_cb
;
1792 refcount_inc(&ds
->ds_clp
->cl_count
);
1793 hdr
->ds_clp
= ds
->ds_clp
;
1794 hdr
->ds_commit_idx
= idx
;
1795 fh
= nfs4_ff_layout_select_ds_fh(lseg
, idx
);
1800 * Note that if we ever decide to split across DSes,
1801 * then we may need to handle dense-like offsets.
1803 hdr
->args
.offset
= offset
;
1805 /* Perform an asynchronous write */
1806 nfs_initiate_pgio(ds_clnt
, hdr
, ds_cred
, ds
->ds_clp
->rpc_ops
,
1807 vers
== 3 ? &ff_layout_write_call_ops_v3
:
1808 &ff_layout_write_call_ops_v4
,
1809 sync
, RPC_TASK_SOFTCONN
);
1810 put_rpccred(ds_cred
);
1811 return PNFS_ATTEMPTED
;
1814 if (ff_layout_avoid_mds_available_ds(lseg
))
1815 return PNFS_TRY_AGAIN
;
1816 return PNFS_NOT_ATTEMPTED
;
1819 static u32
calc_ds_index_from_commit(struct pnfs_layout_segment
*lseg
, u32 i
)
1824 static struct nfs_fh
*
1825 select_ds_fh_from_commit(struct pnfs_layout_segment
*lseg
, u32 i
)
1827 struct nfs4_ff_layout_segment
*flseg
= FF_LAYOUT_LSEG(lseg
);
1829 /* FIXME: Assume that there is only one NFS version available
1832 return &flseg
->mirror_array
[i
]->fh_versions
[0];
1835 static int ff_layout_initiate_commit(struct nfs_commit_data
*data
, int how
)
1837 struct pnfs_layout_segment
*lseg
= data
->lseg
;
1838 struct nfs4_pnfs_ds
*ds
;
1839 struct rpc_clnt
*ds_clnt
;
1840 struct rpc_cred
*ds_cred
;
1845 if (!lseg
|| !(pnfs_is_valid_lseg(lseg
) ||
1846 test_bit(NFS_LSEG_LAYOUTRETURN
, &lseg
->pls_flags
)))
1849 idx
= calc_ds_index_from_commit(lseg
, data
->ds_commit_index
);
1850 ds
= nfs4_ff_layout_prepare_ds(lseg
, idx
, true);
1854 ds_clnt
= nfs4_ff_find_or_create_ds_client(lseg
, idx
, ds
->ds_clp
,
1856 if (IS_ERR(ds_clnt
))
1859 ds_cred
= ff_layout_get_ds_cred(lseg
, idx
, data
->cred
);
1863 vers
= nfs4_ff_layout_ds_version(lseg
, idx
);
1865 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__
,
1866 data
->inode
->i_ino
, how
, refcount_read(&ds
->ds_clp
->cl_count
),
1868 data
->commit_done_cb
= ff_layout_commit_done_cb
;
1869 data
->cred
= ds_cred
;
1870 refcount_inc(&ds
->ds_clp
->cl_count
);
1871 data
->ds_clp
= ds
->ds_clp
;
1872 fh
= select_ds_fh_from_commit(lseg
, data
->ds_commit_index
);
1876 ret
= nfs_initiate_commit(ds_clnt
, data
, ds
->ds_clp
->rpc_ops
,
1877 vers
== 3 ? &ff_layout_commit_call_ops_v3
:
1878 &ff_layout_commit_call_ops_v4
,
1879 how
, RPC_TASK_SOFTCONN
);
1880 put_rpccred(ds_cred
);
1883 pnfs_generic_prepare_to_resend_writes(data
);
1884 pnfs_generic_commit_release(data
);
1889 ff_layout_commit_pagelist(struct inode
*inode
, struct list_head
*mds_pages
,
1890 int how
, struct nfs_commit_info
*cinfo
)
1892 return pnfs_generic_commit_pagelist(inode
, mds_pages
, how
, cinfo
,
1893 ff_layout_initiate_commit
);
1896 static struct pnfs_ds_commit_info
*
1897 ff_layout_get_ds_info(struct inode
*inode
)
1899 struct pnfs_layout_hdr
*layout
= NFS_I(inode
)->layout
;
1904 return &FF_LAYOUT_FROM_HDR(layout
)->commit_info
;
1908 ff_layout_free_deviceid_node(struct nfs4_deviceid_node
*d
)
1910 nfs4_ff_layout_free_deviceid(container_of(d
, struct nfs4_ff_layout_ds
,
1914 static int ff_layout_encode_ioerr(struct xdr_stream
*xdr
,
1915 const struct nfs4_layoutreturn_args
*args
,
1916 const struct nfs4_flexfile_layoutreturn_args
*ff_args
)
1920 start
= xdr_reserve_space(xdr
, 4);
1921 if (unlikely(!start
))
1924 *start
= cpu_to_be32(ff_args
->num_errors
);
1925 /* This assume we always return _ALL_ layouts */
1926 return ff_layout_encode_ds_ioerr(xdr
, &ff_args
->errors
);
1930 encode_opaque_fixed(struct xdr_stream
*xdr
, const void *buf
, size_t len
)
1932 WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr
, buf
, len
) < 0);
1936 ff_layout_encode_ff_iostat_head(struct xdr_stream
*xdr
,
1937 const nfs4_stateid
*stateid
,
1938 const struct nfs42_layoutstat_devinfo
*devinfo
)
1942 p
= xdr_reserve_space(xdr
, 8 + 8);
1943 p
= xdr_encode_hyper(p
, devinfo
->offset
);
1944 p
= xdr_encode_hyper(p
, devinfo
->length
);
1945 encode_opaque_fixed(xdr
, stateid
->data
, NFS4_STATEID_SIZE
);
1946 p
= xdr_reserve_space(xdr
, 4*8);
1947 p
= xdr_encode_hyper(p
, devinfo
->read_count
);
1948 p
= xdr_encode_hyper(p
, devinfo
->read_bytes
);
1949 p
= xdr_encode_hyper(p
, devinfo
->write_count
);
1950 p
= xdr_encode_hyper(p
, devinfo
->write_bytes
);
1951 encode_opaque_fixed(xdr
, devinfo
->dev_id
.data
, NFS4_DEVICEID4_SIZE
);
1955 ff_layout_encode_ff_iostat(struct xdr_stream
*xdr
,
1956 const nfs4_stateid
*stateid
,
1957 const struct nfs42_layoutstat_devinfo
*devinfo
)
1959 ff_layout_encode_ff_iostat_head(xdr
, stateid
, devinfo
);
1960 ff_layout_encode_ff_layoutupdate(xdr
, devinfo
,
1961 devinfo
->ld_private
.data
);
1964 /* report nothing for now */
1965 static void ff_layout_encode_iostats_array(struct xdr_stream
*xdr
,
1966 const struct nfs4_layoutreturn_args
*args
,
1967 struct nfs4_flexfile_layoutreturn_args
*ff_args
)
1972 p
= xdr_reserve_space(xdr
, 4);
1973 *p
= cpu_to_be32(ff_args
->num_dev
);
1974 for (i
= 0; i
< ff_args
->num_dev
; i
++)
1975 ff_layout_encode_ff_iostat(xdr
,
1976 &args
->layout
->plh_stateid
,
1977 &ff_args
->devinfo
[i
]);
1981 ff_layout_free_iostats_array(struct nfs42_layoutstat_devinfo
*devinfo
,
1982 unsigned int num_entries
)
1986 for (i
= 0; i
< num_entries
; i
++) {
1987 if (!devinfo
[i
].ld_private
.ops
)
1989 if (!devinfo
[i
].ld_private
.ops
->free
)
1991 devinfo
[i
].ld_private
.ops
->free(&devinfo
[i
].ld_private
);
1995 static struct nfs4_deviceid_node
*
1996 ff_layout_alloc_deviceid_node(struct nfs_server
*server
,
1997 struct pnfs_device
*pdev
, gfp_t gfp_flags
)
1999 struct nfs4_ff_layout_ds
*dsaddr
;
2001 dsaddr
= nfs4_ff_alloc_deviceid_node(server
, pdev
, gfp_flags
);
2004 return &dsaddr
->id_node
;
2008 ff_layout_encode_layoutreturn(struct xdr_stream
*xdr
,
2009 const void *voidargs
,
2010 const struct nfs4_xdr_opaque_data
*ff_opaque
)
2012 const struct nfs4_layoutreturn_args
*args
= voidargs
;
2013 struct nfs4_flexfile_layoutreturn_args
*ff_args
= ff_opaque
->data
;
2014 struct xdr_buf tmp_buf
= {
2017 .iov_base
= page_address(ff_args
->pages
[0]),
2020 .buflen
= PAGE_SIZE
,
2022 struct xdr_stream tmp_xdr
;
2025 dprintk("%s: Begin\n", __func__
);
2027 xdr_init_encode(&tmp_xdr
, &tmp_buf
, NULL
);
2029 ff_layout_encode_ioerr(&tmp_xdr
, args
, ff_args
);
2030 ff_layout_encode_iostats_array(&tmp_xdr
, args
, ff_args
);
2032 start
= xdr_reserve_space(xdr
, 4);
2033 *start
= cpu_to_be32(tmp_buf
.len
);
2034 xdr_write_pages(xdr
, ff_args
->pages
, 0, tmp_buf
.len
);
2036 dprintk("%s: Return\n", __func__
);
2040 ff_layout_free_layoutreturn(struct nfs4_xdr_opaque_data
*args
)
2042 struct nfs4_flexfile_layoutreturn_args
*ff_args
;
2046 ff_args
= args
->data
;
2049 ff_layout_free_ds_ioerr(&ff_args
->errors
);
2050 ff_layout_free_iostats_array(ff_args
->devinfo
, ff_args
->num_dev
);
2052 put_page(ff_args
->pages
[0]);
2056 static const struct nfs4_xdr_opaque_ops layoutreturn_ops
= {
2057 .encode
= ff_layout_encode_layoutreturn
,
2058 .free
= ff_layout_free_layoutreturn
,
2062 ff_layout_prepare_layoutreturn(struct nfs4_layoutreturn_args
*args
)
2064 struct nfs4_flexfile_layoutreturn_args
*ff_args
;
2065 struct nfs4_flexfile_layout
*ff_layout
= FF_LAYOUT_FROM_HDR(args
->layout
);
2067 ff_args
= kmalloc(sizeof(*ff_args
), GFP_KERNEL
);
2070 ff_args
->pages
[0] = alloc_page(GFP_KERNEL
);
2071 if (!ff_args
->pages
[0])
2072 goto out_nomem_free
;
2074 INIT_LIST_HEAD(&ff_args
->errors
);
2075 ff_args
->num_errors
= ff_layout_fetch_ds_ioerr(args
->layout
,
2076 &args
->range
, &ff_args
->errors
,
2077 FF_LAYOUTRETURN_MAXERR
);
2079 spin_lock(&args
->inode
->i_lock
);
2080 ff_args
->num_dev
= ff_layout_mirror_prepare_stats(&ff_layout
->generic_hdr
,
2081 &ff_args
->devinfo
[0], ARRAY_SIZE(ff_args
->devinfo
));
2082 spin_unlock(&args
->inode
->i_lock
);
2084 args
->ld_private
->ops
= &layoutreturn_ops
;
2085 args
->ld_private
->data
= ff_args
;
2094 ff_layout_ntop4(const struct sockaddr
*sap
, char *buf
, const size_t buflen
)
2096 const struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
2098 return snprintf(buf
, buflen
, "%pI4", &sin
->sin_addr
);
2102 ff_layout_ntop6_noscopeid(const struct sockaddr
*sap
, char *buf
,
2105 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
2106 const struct in6_addr
*addr
= &sin6
->sin6_addr
;
2109 * RFC 4291, Section 2.2.2
2111 * Shorthanded ANY address
2113 if (ipv6_addr_any(addr
))
2114 return snprintf(buf
, buflen
, "::");
2117 * RFC 4291, Section 2.2.2
2119 * Shorthanded loopback address
2121 if (ipv6_addr_loopback(addr
))
2122 return snprintf(buf
, buflen
, "::1");
2125 * RFC 4291, Section 2.2.3
2127 * Special presentation address format for mapped v4
2130 if (ipv6_addr_v4mapped(addr
))
2131 return snprintf(buf
, buflen
, "::ffff:%pI4",
2132 &addr
->s6_addr32
[3]);
2135 * RFC 4291, Section 2.2.1
2137 return snprintf(buf
, buflen
, "%pI6c", addr
);
2140 /* Derived from rpc_sockaddr2uaddr */
2142 ff_layout_encode_netaddr(struct xdr_stream
*xdr
, struct nfs4_pnfs_ds_addr
*da
)
2144 struct sockaddr
*sap
= (struct sockaddr
*)&da
->da_addr
;
2145 char portbuf
[RPCBIND_MAXUADDRPLEN
];
2146 char addrbuf
[RPCBIND_MAXUADDRLEN
];
2148 unsigned short port
;
2152 switch (sap
->sa_family
) {
2154 if (ff_layout_ntop4(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
2156 port
= ntohs(((struct sockaddr_in
*)sap
)->sin_port
);
2161 if (ff_layout_ntop6_noscopeid(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
2163 port
= ntohs(((struct sockaddr_in6
*)sap
)->sin6_port
);
2168 /* we only support tcp and tcp6 */
2173 snprintf(portbuf
, sizeof(portbuf
), ".%u.%u", port
>> 8, port
& 0xff);
2174 len
= strlcat(addrbuf
, portbuf
, sizeof(addrbuf
));
2176 p
= xdr_reserve_space(xdr
, 4 + netid_len
);
2177 xdr_encode_opaque(p
, netid
, netid_len
);
2179 p
= xdr_reserve_space(xdr
, 4 + len
);
2180 xdr_encode_opaque(p
, addrbuf
, len
);
2184 ff_layout_encode_nfstime(struct xdr_stream
*xdr
,
2187 struct timespec64 ts
;
2190 p
= xdr_reserve_space(xdr
, 12);
2191 ts
= ktime_to_timespec64(t
);
2192 p
= xdr_encode_hyper(p
, ts
.tv_sec
);
2193 *p
++ = cpu_to_be32(ts
.tv_nsec
);
2197 ff_layout_encode_io_latency(struct xdr_stream
*xdr
,
2198 struct nfs4_ff_io_stat
*stat
)
2202 p
= xdr_reserve_space(xdr
, 5 * 8);
2203 p
= xdr_encode_hyper(p
, stat
->ops_requested
);
2204 p
= xdr_encode_hyper(p
, stat
->bytes_requested
);
2205 p
= xdr_encode_hyper(p
, stat
->ops_completed
);
2206 p
= xdr_encode_hyper(p
, stat
->bytes_completed
);
2207 p
= xdr_encode_hyper(p
, stat
->bytes_not_delivered
);
2208 ff_layout_encode_nfstime(xdr
, stat
->total_busy_time
);
2209 ff_layout_encode_nfstime(xdr
, stat
->aggregate_completion_time
);
2213 ff_layout_encode_ff_layoutupdate(struct xdr_stream
*xdr
,
2214 const struct nfs42_layoutstat_devinfo
*devinfo
,
2215 struct nfs4_ff_layout_mirror
*mirror
)
2217 struct nfs4_pnfs_ds_addr
*da
;
2218 struct nfs4_pnfs_ds
*ds
= mirror
->mirror_ds
->ds
;
2219 struct nfs_fh
*fh
= &mirror
->fh_versions
[0];
2222 da
= list_first_entry(&ds
->ds_addrs
, struct nfs4_pnfs_ds_addr
, da_node
);
2223 dprintk("%s: DS %s: encoding address %s\n",
2224 __func__
, ds
->ds_remotestr
, da
->da_remotestr
);
2226 ff_layout_encode_netaddr(xdr
, da
);
2228 p
= xdr_reserve_space(xdr
, 4 + fh
->size
);
2229 xdr_encode_opaque(p
, fh
->data
, fh
->size
);
2230 /* ff_io_latency4 read */
2231 spin_lock(&mirror
->lock
);
2232 ff_layout_encode_io_latency(xdr
, &mirror
->read_stat
.io_stat
);
2233 /* ff_io_latency4 write */
2234 ff_layout_encode_io_latency(xdr
, &mirror
->write_stat
.io_stat
);
2235 spin_unlock(&mirror
->lock
);
2237 ff_layout_encode_nfstime(xdr
, ktime_sub(ktime_get(), mirror
->start_time
));
2239 p
= xdr_reserve_space(xdr
, 4);
2240 *p
= cpu_to_be32(false);
2244 ff_layout_encode_layoutstats(struct xdr_stream
*xdr
, const void *args
,
2245 const struct nfs4_xdr_opaque_data
*opaque
)
2247 struct nfs42_layoutstat_devinfo
*devinfo
= container_of(opaque
,
2248 struct nfs42_layoutstat_devinfo
, ld_private
);
2251 /* layoutupdate length */
2252 start
= xdr_reserve_space(xdr
, 4);
2253 ff_layout_encode_ff_layoutupdate(xdr
, devinfo
, opaque
->data
);
2255 *start
= cpu_to_be32((xdr
->p
- start
- 1) * 4);
2259 ff_layout_free_layoutstats(struct nfs4_xdr_opaque_data
*opaque
)
2261 struct nfs4_ff_layout_mirror
*mirror
= opaque
->data
;
2263 ff_layout_put_mirror(mirror
);
2266 static const struct nfs4_xdr_opaque_ops layoutstat_ops
= {
2267 .encode
= ff_layout_encode_layoutstats
,
2268 .free
= ff_layout_free_layoutstats
,
2272 ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr
*lo
,
2273 struct nfs42_layoutstat_devinfo
*devinfo
,
2276 struct nfs4_flexfile_layout
*ff_layout
= FF_LAYOUT_FROM_HDR(lo
);
2277 struct nfs4_ff_layout_mirror
*mirror
;
2278 struct nfs4_deviceid_node
*dev
;
2281 list_for_each_entry(mirror
, &ff_layout
->mirrors
, mirrors
) {
2284 if (IS_ERR_OR_NULL(mirror
->mirror_ds
))
2286 if (!test_and_clear_bit(NFS4_FF_MIRROR_STAT_AVAIL
, &mirror
->flags
))
2288 /* mirror refcount put in cleanup_layoutstats */
2289 if (!refcount_inc_not_zero(&mirror
->ref
))
2291 dev
= &mirror
->mirror_ds
->id_node
;
2292 memcpy(&devinfo
->dev_id
, &dev
->deviceid
, NFS4_DEVICEID4_SIZE
);
2293 devinfo
->offset
= 0;
2294 devinfo
->length
= NFS4_MAX_UINT64
;
2295 spin_lock(&mirror
->lock
);
2296 devinfo
->read_count
= mirror
->read_stat
.io_stat
.ops_completed
;
2297 devinfo
->read_bytes
= mirror
->read_stat
.io_stat
.bytes_completed
;
2298 devinfo
->write_count
= mirror
->write_stat
.io_stat
.ops_completed
;
2299 devinfo
->write_bytes
= mirror
->write_stat
.io_stat
.bytes_completed
;
2300 spin_unlock(&mirror
->lock
);
2301 devinfo
->layout_type
= LAYOUT_FLEX_FILES
;
2302 devinfo
->ld_private
.ops
= &layoutstat_ops
;
2303 devinfo
->ld_private
.data
= mirror
;
2312 ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args
*args
)
2314 struct nfs4_flexfile_layout
*ff_layout
;
2315 const int dev_count
= PNFS_LAYOUTSTATS_MAXDEV
;
2317 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2318 args
->devinfo
= kmalloc_array(dev_count
, sizeof(*args
->devinfo
), GFP_NOIO
);
2322 spin_lock(&args
->inode
->i_lock
);
2323 ff_layout
= FF_LAYOUT_FROM_HDR(NFS_I(args
->inode
)->layout
);
2324 args
->num_dev
= ff_layout_mirror_prepare_stats(&ff_layout
->generic_hdr
,
2325 &args
->devinfo
[0], dev_count
);
2326 spin_unlock(&args
->inode
->i_lock
);
2327 if (!args
->num_dev
) {
2328 kfree(args
->devinfo
);
2329 args
->devinfo
= NULL
;
2337 ff_layout_set_layoutdriver(struct nfs_server
*server
,
2338 const struct nfs_fh
*dummy
)
2340 #if IS_ENABLED(CONFIG_NFS_V4_2)
2341 server
->caps
|= NFS_CAP_LAYOUTSTATS
;
2346 static struct pnfs_layoutdriver_type flexfilelayout_type
= {
2347 .id
= LAYOUT_FLEX_FILES
,
2348 .name
= "LAYOUT_FLEX_FILES",
2349 .owner
= THIS_MODULE
,
2350 .set_layoutdriver
= ff_layout_set_layoutdriver
,
2351 .alloc_layout_hdr
= ff_layout_alloc_layout_hdr
,
2352 .free_layout_hdr
= ff_layout_free_layout_hdr
,
2353 .alloc_lseg
= ff_layout_alloc_lseg
,
2354 .free_lseg
= ff_layout_free_lseg
,
2355 .add_lseg
= ff_layout_add_lseg
,
2356 .pg_read_ops
= &ff_layout_pg_read_ops
,
2357 .pg_write_ops
= &ff_layout_pg_write_ops
,
2358 .get_ds_info
= ff_layout_get_ds_info
,
2359 .free_deviceid_node
= ff_layout_free_deviceid_node
,
2360 .mark_request_commit
= pnfs_layout_mark_request_commit
,
2361 .clear_request_commit
= pnfs_generic_clear_request_commit
,
2362 .scan_commit_lists
= pnfs_generic_scan_commit_lists
,
2363 .recover_commit_reqs
= pnfs_generic_recover_commit_reqs
,
2364 .commit_pagelist
= ff_layout_commit_pagelist
,
2365 .read_pagelist
= ff_layout_read_pagelist
,
2366 .write_pagelist
= ff_layout_write_pagelist
,
2367 .alloc_deviceid_node
= ff_layout_alloc_deviceid_node
,
2368 .prepare_layoutreturn
= ff_layout_prepare_layoutreturn
,
2369 .sync
= pnfs_nfs_generic_sync
,
2370 .prepare_layoutstats
= ff_layout_prepare_layoutstats
,
2373 static int __init
nfs4flexfilelayout_init(void)
2375 printk(KERN_INFO
"%s: NFSv4 Flexfile Layout Driver Registering...\n",
2377 if (!ff_zero_group
) {
2378 ff_zero_group
= groups_alloc(0);
2382 return pnfs_register_layoutdriver(&flexfilelayout_type
);
2385 static void __exit
nfs4flexfilelayout_exit(void)
2387 printk(KERN_INFO
"%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2389 pnfs_unregister_layoutdriver(&flexfilelayout_type
);
2390 if (ff_zero_group
) {
2391 put_group_info(ff_zero_group
);
2392 ff_zero_group
= NULL
;
2396 MODULE_ALIAS("nfs-layouttype4-4");
2398 MODULE_LICENSE("GPL");
2399 MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2401 module_init(nfs4flexfilelayout_init
);
2402 module_exit(nfs4flexfilelayout_exit
);