Whamcloud - gitweb
LU-14362 tests: sanity-flr to prepare stuff before checks
[fs/lustre-release.git] / lustre / tests / sanity-flr.sh
index f21f3d8..f9bbf7f 100644 (file)
@@ -5,32 +5,44 @@
 set -e
 set +o posix
 
-SRCDIR=$(dirname $0)
-export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
 
 ONLY=${ONLY:-"$*"}
-# Bug number for skipped test:
-ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT 201"
-# UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
 
-LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
+LUSTRE=${LUSTRE:-$(dirname $0)/..}
 . $LUSTRE/tests/test-framework.sh
 init_test_env $@
-. ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
 init_logging
 
-[[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
-       { skip "Need MDS version at least 2.10.56"; exit 0; }
+ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT "
+# Bug number for skipped test:    LU-11381
+ALWAYS_EXCEPT+="                  201 "
+# UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
 
-[ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
-       error "\$RUNAS_ID set to 0, but \$UID is also 0!"
-check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
+# skip all tests for PPC until we can get sanity-pfl to pass
+if [[ $(uname -m) = ppc64 ]]; then
+       skip "Skip FLR testing for PPC clients"
+fi
+
+if [[ "$ost1_FSTYPE" == "zfs" ]]; then
+       # bug #:        LU-1941
+       ALWAYS_EXCEPT+="49a"
+fi
+
+build_test_filter
+
+[[ "$MDS1_VERSION" -ge $(version_code 2.10.56) ]] ||
+       skip "Need MDS version at least 2.10.56"
 
 check_and_setup_lustre
 DIR=${DIR:-$MOUNT}
 assert_DIR
+rm -rf $DIR/[Rdfs][0-9]*
+
+[ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
+       error "\$RUNAS_ID set to 0, but \$UID is also 0!"
+
+check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
 
-build_test_filter
 
 # global array to store mirror IDs
 declare -a mirror_array
@@ -61,7 +73,7 @@ stop_osts() {
        done
 
        for idx in "$@"; do
-               wait_osc_import_state client ost$idx DISCONN
+               wait_osc_import_state client ost$idx "\(DISCONN\|IDLE\)"
        done
 }
 
@@ -74,7 +86,7 @@ start_osts() {
        done
 
        for idx in "$@"; do
-               wait_osc_import_state client ost$idx FULL
+               wait_recovery_complete ost$idx
        done
 }
 
@@ -84,7 +96,7 @@ start_osts() {
 verify_mirror_count() {
        local tf=$1
        local expected=$2
-       local mirror_count=$(get_mirror_ids $tf)
+       local mirror_count=$($LFS getstripe -N $tf)
 
        [[ $mirror_count = $expected ]] || {
                $LFS getstripe -v $tf
@@ -140,10 +152,23 @@ verify_comp_attr() {
                local fl
                local expected_list=$(comma_list $expected)
                for fl in ${expected_list//,/ }; do
-                       echo $value | grep -q $fl || {
+                       local neg=0
+
+                       [[ ${fl:0:1} = "^" ]] && neg=1
+                       [[ $neg = 1 ]] && fl=${fl:1}
+
+                       $(echo $value | grep -q $fl)
+                       local match=$?
+                       # 0: matched; 1: not matched
+
+                       if  [[ $neg = 0 && $match != 0 ||
+                              $neg = 1 && $match = 0 ]]; then
                                $getstripe_cmd $tf
-                               error "expected flag $fl existing on $comp_id"
-                       }
+                               [[ $neg = 0 ]] && # expect the flag
+                                   error "expected flag '$fl' not in $comp_id"
+                               [[ $neg = 1 ]] && # not expect the flag
+                                   error "not expected flag '$fl' in $comp_id"
+                       fi
                done
                return
        }
@@ -205,21 +230,69 @@ verify_comp_attr_with_parent() {
 }
 
 #
-# Verify component attributes with parent directory for a given file
+# Verify component attribute with filesystem-wide default value for a given file
+# and component ID.
+#
+verify_comp_attr_with_default() {
+       local attr=$1
+       local tf=$2
+       local comp_id=$3
+       local tf_cmd="$LFS getstripe -I$comp_id"
+       local opt
+       local expected
+       local value
+
+       case $attr in
+               stripe-size)
+                       opt="-S"
+                       expected=$($LCTL get_param -n \
+                                  lov.$FSNAME-clilov-*.stripesize)
+                       ;;
+               stripe-count)
+                       opt="-c"
+                       expected=$($LCTL get_param -n \
+                                  lov.$FSNAME-clilov-*.stripecount)
+                       [[ $expected = -1 ]] && expected=$OSTCOUNT
+                       ;;
+               *) error "invalid attribute $attr";;
+       esac
+
+       value=$($tf_cmd $opt $tf)
+       [[ $value = -1 ]] && value=$OSTCOUNT
+
+       [[ $value = $expected ]] || {
+               $tf_cmd -v $tf
+               error "verify $attr failed with default value on $tf:" \
+                     "$value != $expected"
+       }
+}
+
+#
+# Verify unspecified component attributes for a given file
 # and component ID.
 #
 # This will only verify the inherited attributes:
 # stripe size, stripe count and OST pool name
 #
-verify_comp_attrs_with_parent() {
+verify_comp_attrs() {
        local tf=$1
        local comp_id=$2
 
-       verify_comp_attr_with_parent stripe-size $tf $comp_id
-       verify_comp_attr_with_parent stripe-count $tf $comp_id
+       verify_comp_attr_with_default stripe-size $tf $comp_id
+       verify_comp_attr_with_default stripe-count $tf $comp_id
        verify_comp_attr_with_parent pool $tf $comp_id
 }
 
+verify_flr_state()
+{
+       local tf=$1
+       local expected_state=$2
+
+       local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
+       [ $expected_state = $state ] ||
+               error "expected: $expected_state, actual $state"
+}
+
 # command line test cases
 test_0a() {
        local td=$DIR/$tdir
@@ -238,7 +311,7 @@ test_0a() {
        $mirror_cmd -N $tf || error "create mirrored file $tf failed"
        verify_mirror_count $tf 1
        id=$($LFS getstripe -I $tf)
-       verify_comp_attrs_with_parent $tf $id
+       verify_comp_attrs $tf $id
        verify_comp_extent $tf $id 0 EOF
 
        $mirror_cmd -N0 $tf-1 &> /dev/null && error "invalid mirror count 0"
@@ -250,7 +323,7 @@ test_0a() {
        verify_mirror_count $tf-1 $mirror_count
        ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
        for ((i = 0; i < $mirror_count; i++)); do
-               verify_comp_attrs_with_parent $tf-1 ${ids[$i]}
+               verify_comp_attrs $tf-1 ${ids[$i]}
                verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
        done
 
@@ -259,7 +332,7 @@ test_0a() {
        verify_mirror_count $tf-2 10
        ids=($($LFS getstripe $tf-2 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
        for ((i = 0; i < 10; i++)); do
-               verify_comp_attrs_with_parent $tf-2 ${ids[$i]}
+               verify_comp_attrs $tf-2 ${ids[$i]}
                verify_comp_extent $tf-2 ${ids[$i]} 0 EOF
        done
 }
@@ -274,42 +347,70 @@ test_0b() {
        local ids
        local i
 
+       # create a new OST pool
+       local pool_name=$TESTNAME
+       create_pool $FSNAME.$pool_name ||
+               error "create OST pool $pool_name failed"
+
+       # add OSTs into the pool
+       pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
+               error "add OSTs into pool $pool_name failed"
+
        # create parent directory
        mkdir $td || error "mkdir $td failed"
+       $LFS setstripe -S 8M -c -1 -p $pool_name $td ||
+               error "$LFS setstripe $td failed"
 
        # create a mirrored file with plain layout mirrors
-       $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
-                   -N -S 16M -N -c -1 -N -p archive -N --parent $tf ||
+       $mirror_cmd -N -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
+                   -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
                error "create mirrored file $tf failed"
-       verify_mirror_count $tf 5
+       verify_mirror_count $tf 6
        ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
-       for ((i = 0; i < 5; i++)); do
+       for ((i = 0; i < 6; i++)); do
                verify_comp_extent $tf ${ids[$i]} 0 EOF
        done
 
        # verify component ${ids[0]}
-       verify_comp_attr stripe-size $tf ${ids[0]} 4194304
-       verify_comp_attr stripe-count $tf ${ids[0]} 2
-       verify_comp_attr stripe-index $tf ${ids[0]} 2
-       verify_comp_attr pool $tf ${ids[0]} flash
+       verify_comp_attrs $tf ${ids[0]}
 
        # verify component ${ids[1]}
-       verify_comp_attr stripe-size $tf ${ids[1]} 16777216
+       verify_comp_attr stripe-size $tf ${ids[1]} 4194304
        verify_comp_attr stripe-count $tf ${ids[1]} 2
+       verify_comp_attr stripe-index $tf ${ids[1]} 2
        verify_comp_attr pool $tf ${ids[1]} flash
 
        # verify component ${ids[2]}
        verify_comp_attr stripe-size $tf ${ids[2]} 16777216
-       verify_comp_attr stripe-count $tf ${ids[2]} $OSTCOUNT
+       verify_comp_attr stripe-count $tf ${ids[2]} 2
        verify_comp_attr pool $tf ${ids[2]} flash
 
        # verify component ${ids[3]}
        verify_comp_attr stripe-size $tf ${ids[3]} 16777216
        verify_comp_attr stripe-count $tf ${ids[3]} $OSTCOUNT
-       verify_comp_attr pool $tf ${ids[3]} archive
+       verify_comp_attr pool $tf ${ids[3]} flash
 
        # verify component ${ids[4]}
-       verify_comp_attrs_with_parent $tf ${ids[4]}
+       verify_comp_attr stripe-size $tf ${ids[4]} 16777216
+       verify_comp_attr stripe-count $tf ${ids[4]} $OSTCOUNT
+       verify_comp_attr pool $tf ${ids[4]} archive
+
+       # verify component ${ids[5]}
+       verify_comp_attr stripe-size $tf ${ids[5]} 16777216
+       verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
+       verify_comp_attr_with_parent pool $tf ${ids[5]}
+
+       if [ $MDS1_VERSION -ge $(version_code 2.12.55) ]; then
+               # LU-11022 - remove mirror by pool name
+               local=cnt cnt=$($LFS getstripe $tf | grep archive | wc -l)
+               [ "$cnt" != "1" ] && error "unexpected mirror count $cnt"
+               $LFS mirror delete --pool archive $tf || error "delete mirror"
+               cnt=$($LFS getstripe $tf | grep archive | wc -l)
+               [ "$cnt" != "0" ] && error "mirror count after removal: $cnt"
+       fi
+
+       # destroy OST pool
+       destroy_test_pools
 }
 run_test 0b "lfs mirror create plain layout mirrors"
 
@@ -322,12 +423,23 @@ test_0c() {
        local ids
        local i
 
+       # create a new OST pool
+       local pool_name=$TESTNAME
+       create_pool $FSNAME.$pool_name ||
+               error "create OST pool $pool_name failed"
+
+       # add OSTs into the pool
+       pool_add_targets $pool_name 0 $((OSTCOUNT - 1)) ||
+               error "add OSTs into pool $pool_name failed"
+
        # create parent directory
        mkdir $td || error "mkdir $td failed"
+       $LFS setstripe -E 32M -S 8M -c -1 -p $pool_name -E eof -S 16M $td ||
+               error "$LFS setstripe $td failed"
 
        # create a mirrored file with composite layout mirrors
        $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
-                   -N --parent \
+                   -N -c 4 -p none \
                    -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
                error "create mirrored file $tf failed"
        verify_mirror_count $tf 6
@@ -335,7 +447,7 @@ test_0c() {
 
        # verify components ${ids[0]} and ${ids[2]}
        for i in 0 2; do
-               verify_comp_attr_with_parent stripe-size $tf ${ids[$i]}
+               verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
                verify_comp_attr stripe-count $tf ${ids[$i]} 2
                verify_comp_attr stripe-index $tf ${ids[$i]} 1
                verify_comp_attr pool $tf ${ids[$i]} flash
@@ -351,13 +463,15 @@ test_0c() {
        done
 
        # verify component ${ids[4]}
-       verify_comp_attrs_with_parent $tf ${ids[4]}
+       verify_comp_attr stripe-size $tf ${ids[4]} 4194304
+       verify_comp_attr stripe-count $tf ${ids[4]} 4
+       verify_comp_attr_with_parent pool $tf ${ids[4]}
        verify_comp_extent $tf ${ids[4]} 0 EOF
 
        # verify components ${ids[5]}, ${ids[7]} and ${ids[9]}
        for i in 5 7 9; do
                verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
-               verify_comp_attr_with_parent stripe-count $tf ${ids[$i]}
+               verify_comp_attr stripe-count $tf ${ids[$i]} 4
                verify_comp_attr pool $tf ${ids[$i]} archive
                verify_comp_extent $tf ${ids[$i]} 0 536870912
        done
@@ -369,6 +483,9 @@ test_0c() {
                verify_comp_attr pool $tf ${ids[$i]} archive
                verify_comp_extent $tf ${ids[$i]} 536870912 EOF
        done
+
+       # destroy OST pool
+       destroy_test_pools
 }
 run_test 0c "lfs mirror create composite layout mirrors"
 
@@ -392,7 +509,7 @@ test_0d() {
        verify_mirror_count $tf 2
        ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
        for ((i = 0; i < 2; i++)); do
-               verify_comp_attrs_with_parent $tf ${ids[$i]}
+               verify_comp_attrs $tf ${ids[$i]}
                verify_comp_extent $tf ${ids[$i]} 0 EOF
        done
 
@@ -405,15 +522,12 @@ test_0d() {
        $mirror_cmd -N -S 4M -N -f $tf-2 $tf-1 &> /dev/null &&
                error "setstripe options should not be specified with -f option"
 
-       $mirror_cmd -N -f $tf-2 -N --parent $tf-1 &> /dev/null &&
-               error "--parent option should not be specified with -f option"
-
        $mirror_cmd -N$((mirror_count - 1)) $tf-1 ||
                error "extend mirrored file $tf-1 failed"
        verify_mirror_count $tf-1 $mirror_count
        ids=($($LFS getstripe $tf-1 | awk '/lcme_id/{print $2}' | tr '\n' ' '))
        for ((i = 0; i < $mirror_count; i++)); do
-               verify_comp_attrs_with_parent $tf-1 ${ids[$i]}
+               verify_comp_attrs $tf-1 ${ids[$i]}
                verify_comp_extent $tf-1 ${ids[$i]} 0 EOF
        done
 
@@ -440,7 +554,7 @@ test_0e() {
 
        # extend the mirrored file with plain layout mirrors
        $mirror_cmd -N -S 4M -c 2 -p flash -i 2 -o 2,3 \
-                   -N -S 16M -N -c -1 -N -p archive -N --parent $tf ||
+                   -N -S 16M -N -c -1 -N -p archive -N -p none $tf ||
                error "extend mirrored file $tf failed"
        verify_mirror_count $tf 6
        ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
@@ -476,7 +590,9 @@ test_0e() {
        verify_comp_attr pool $tf ${ids[4]} archive
 
        # verify component ${ids[5]}
-       verify_comp_attrs_with_parent $tf ${ids[5]}
+       verify_comp_attr stripe-size $tf ${ids[5]} 16777216
+       verify_comp_attr stripe-count $tf ${ids[5]} $OSTCOUNT
+       verify_comp_attr_with_parent pool $tf ${ids[5]}
 }
 run_test 0e "lfs mirror extend plain layout mirrors"
 
@@ -497,56 +613,65 @@ test_0f() {
                error "create mirrored file $tf failed"
 
        # extend the mirrored file with composite layout mirrors
-       $mirror_cmd -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
-                   -N --parent \
+       $mirror_cmd -N -p archive \
+                   -N2 -E 4M -c 2 -p flash -i 1 -o 1,3 -E eof -S 4M \
+                   -N -c -1 -p none \
                    -N3 -E 512M -S 16M -p archive -E -1 -i -1 -c -1 $tf ||
                error "extend mirrored file $tf failed"
-       verify_mirror_count $tf 7
+       verify_mirror_count $tf 8
        ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
 
        # verify component ${ids[0]}
        verify_comp_attr stripe-size $tf ${ids[0]} 16777216
-       verify_comp_attr_with_parent stripe-count $tf ${ids[0]}
+       verify_comp_attr_with_default stripe-count $tf ${ids[0]}
        verify_comp_attr pool $tf ${ids[0]} ssd
        verify_comp_extent $tf ${ids[0]} 0 33554432
 
        # verify component ${ids[1]}
        verify_comp_attr stripe-size $tf ${ids[1]} 33554432
-       verify_comp_attr_with_parent stripe-count $tf ${ids[1]}
+       verify_comp_attr_with_default stripe-count $tf ${ids[1]}
        verify_comp_attr pool $tf ${ids[1]} ssd
        verify_comp_extent $tf ${ids[1]} 33554432 EOF
 
-       # verify components ${ids[2]} and ${ids[4]}
-       for i in 2 4; do
-               verify_comp_attr_with_parent stripe-size $tf ${ids[$i]}
+       # verify component ${ids[2]}
+       verify_comp_attr stripe-size $tf ${ids[0]} 16777216
+       verify_comp_attr_with_default stripe-count $tf ${ids[2]}
+       verify_comp_attr pool $tf ${ids[2]} archive
+       verify_comp_extent $tf ${ids[2]} 0 EOF
+
+       # verify components ${ids[3]} and ${ids[5]}
+       for i in 3 5; do
+               verify_comp_attr_with_default stripe-size $tf ${ids[$i]}
                verify_comp_attr stripe-count $tf ${ids[$i]} 2
                verify_comp_attr stripe-index $tf ${ids[$i]} 1
                verify_comp_attr pool $tf ${ids[$i]} flash
                verify_comp_extent $tf ${ids[$i]} 0 4194304
        done
 
-       # verify components ${ids[3]} and ${ids[5]}
-       for i in 3 5; do
+       # verify components ${ids[4]} and ${ids[6]}
+       for i in 4 6; do
                verify_comp_attr stripe-size $tf ${ids[$i]} 4194304
                verify_comp_attr stripe-count $tf ${ids[$i]} 2
                verify_comp_attr pool $tf ${ids[$i]} flash
                verify_comp_extent $tf ${ids[$i]} 4194304 EOF
        done
 
-       # verify component ${ids[6]}
-       verify_comp_attrs_with_parent $tf ${ids[6]}
-       verify_comp_extent $tf ${ids[6]} 0 EOF
+       # verify component ${ids[7]}
+       verify_comp_attr stripe-size $tf ${ids[7]} 4194304
+       verify_comp_attr stripe-count $tf ${ids[7]} $OSTCOUNT
+       verify_comp_attr_with_parent pool $tf ${ids[7]}
+       verify_comp_extent $tf ${ids[7]} 0 EOF
 
-       # verify components ${ids[7]}, ${ids[9]} and ${ids[11]}
-       for i in 7 9 11; do
+       # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
+       for i in 8 10 12; do
                verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
-               verify_comp_attr_with_parent stripe-count $tf ${ids[$i]}
+               verify_comp_attr stripe-count $tf ${ids[$i]} $OSTCOUNT
                verify_comp_attr pool $tf ${ids[$i]} archive
                verify_comp_extent $tf ${ids[$i]} 0 536870912
        done
 
-       # verify components ${ids[8]}, ${ids[10]} and ${ids[12]}
-       for i in 8 10 12; do
+       # verify components ${ids[9]}, ${ids[11]} and ${ids[13]}
+       for i in 9 11 13; do
                verify_comp_attr stripe-size $tf ${ids[$i]} 16777216
                verify_comp_attr stripe-count $tf ${ids[$i]} -1
                verify_comp_attr pool $tf ${ids[$i]} archive
@@ -558,8 +683,8 @@ run_test 0f "lfs mirror extend composite layout mirrors"
 test_0g() {
        local tf=$DIR/$tfile
 
-       $LFS mirror create -N -E 1M -o0 --flags=prefer -E eof -o1 -N -o1 $tf ||
-               error "create mirrored file $tf failed"
+       $LFS mirror create -N -E 1M -S 1M -o0 --flags=prefer -E eof -o1 \
+                          -N -o1 $tf || error "create mirrored file $tf failed"
 
        verify_comp_attr lcme_flags $tf 0x10001 prefer
        verify_comp_attr lcme_flags $tf 0x10002 prefer
@@ -577,17 +702,29 @@ test_0g() {
        cat $tf &> /dev/null || error "error reading file '$tf'"
 
        # verify that the data was provided by OST1 where mirror 1 resides
-       local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-ffff*.stats |
+       local nr_read=$($LCTL get_param -n osc.$FSNAME-OST0000-osc-[-0-9a-f]*.stats |
                        awk '/ost_read/{print $2}')
        [ -n "$nr_read" ] || error "read was not provided by OST1"
 }
 run_test 0g "lfs mirror create flags support"
 
 test_0h() {
-       local tf=$DIR/$tfile
+       [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
+               skip "Need MDS version at least 2.11.57"
 
-       $LFS mirror create -N -E 1M --flags=prefer -E eof -N2 $tf ||
-               error "create mirrored file $tf failed"
+       local td=$DIR/$tdir
+       local tf=$td/$tfile
+       local ids
+       local i
+
+       # create parent directory
+       test_mkdir $td || error "mkdir $td failed"
+
+       $LFS setstripe -N -E 1M -S 1M --flags=prefer -E eof -N2 $td ||
+               error "set default mirrored layout on directory $td failed"
+
+       # verify flags are inherited from the directory
+       touch $tf
 
        verify_comp_attr lcme_flags $tf 0x10001 prefer
        verify_comp_attr lcme_flags $tf 0x10002 prefer
@@ -610,9 +747,56 @@ test_0h() {
                error "error setting flag prefer"
 
        verify_comp_attr lcme_flags $tf 0x20003 prefer
+
+       $LFS setstripe --comp-set -I 0x20003 --comp-flags=^prefer $tf ||
+               error "error clearing prefer flag from component 0x20003"
+
+       # MDS disallows setting stale flag on the last non-stale mirror
+       [[ "$MDS1_VERSION" -ge $(version_code 2.12.57) ]] || return 0
+
+       cp /etc/hosts $tf || error "error writing file '$tf'"
+
+       verify_comp_attr lcme_flags $tf 0x10002 prefer
+       verify_comp_attr lcme_flags $tf 0x20003 stale
+       verify_comp_attr lcme_flags $tf 0x30004 stale
+
+       ! $LFS setstripe --comp-set -I 0x10002 --comp-flags=^prefer,stale $tf \
+               > /dev/null 2>&1 ||
+               error "setting stale flag on component 0x10002 should fail"
+
+       $LFS mirror resync $tf || error "error resync-ing file '$tf'"
+
+       $LFS setstripe --comp-set -I 0x10001 --comp-flags=stale $tf ||
+               error "error setting stale flag on component 0x10001"
+       $LFS setstripe --comp-set -I 0x20003 --comp-flags=stale $tf ||
+               error "error setting stale flag on component 0x20003"
+
+       ! $LFS setstripe --comp-set -I 0x30004 --comp-flags=stale $tf \
+               > /dev/null 2>&1 ||
+               error "setting stale flag on component 0x30004 should fail"
+
+       $LFS mirror resync $tf || error "error resync-ing file '$tf'"
 }
 run_test 0h "set, clear and test flags for FLR files"
 
+test_0j() {
+       $LFS mirror create -N2 $DIR/$tfile || error "create $DIR/$tfile failed"
+
+       cp /etc/hosts $DIR/$tfile || error "write to $DIR/$tfile failed"
+       $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
+       cmp /etc/hosts $DIR/$tfile || error "cmp with /etc/hosts failed"
+
+       $LFS mirror read -N2 -o $TMP/$tfile $DIR/$tfile || "read mirror failed"
+       stack_trap "rm -f $TMP/$tfile"
+       cmp $TMP/$tfile $DIR/$tfile || error "cmp with $TMP/$tfile failed"
+       $LFS mirror write -N2 -i /etc/passwd $DIR/$tfile || "write failed"
+       $LFS setstripe --comp-set -I 65537 --comp-flags=stale $DIR/$tfile ||
+               error "set component 1 stale failed"
+       $LFS mirror resync $DIR/$tfile || error "resync $DIR/$tfile failed"
+       cmp /etc/passwd $DIR/$tfile || error "cmp with /etc/passwd failed"
+}
+run_test 0j "test lfs mirror read/write commands"
+
 test_1() {
        local tf=$DIR/$tfile
        local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
@@ -650,10 +834,8 @@ test_2() {
        local tf=$DIR/$tfile
        local tf2=$DIR/$tfile-2
 
-       $LFS setstripe -E 1M -E EOF -c 1 $tf
-       $LFS setstripe -E 2M -E EOF -c -1 $tf2
-
-       local layout=$($LFS getstripe $tf2 | grep -A 4 lmm_objects)
+       $LFS setstripe -E 1M -S 1M -E EOF -c 1 $tf
+       $LFS setstripe -E 2M -S 1M -E EOF -c -1 $tf2
 
        $LFS mirror extend -N -f $tf2 $tf ||
                error "merging $tf2 into $tf failed"
@@ -695,7 +877,7 @@ test_4() {
        test_mkdir $DIR/$tdir
 
        # set mirror with setstripe options to directory
-       $LFS mirror create -N2 -E 1M -E eof $DIR/$tdir ||
+       $LFS mirror create -N2 -E 1M -S 1M -E eof $DIR/$tdir ||
                error "set mirror to directory error"
 
        [ x$($LFS getstripe -v $DIR/$tdir | awk '/lcm_flags/{print $2}') = \
@@ -740,17 +922,17 @@ run_test 5 "Make sure init size work for mirrored layout"
 test_6() {
        local tf=$DIR/$tfile
 
-       $LFS mirror create -N -E 1M -L mdt -E eof -N -E eof $tf &&
+       $LFS mirror create -N -E 1M -S 1M -L mdt -E eof -N -E eof $tf &&
                error "expect failure to create mirrored file with DoM"
 
-       $LFS mirror create -N -E 1M -E eof -N -E 1M -L mdt -E eof $tf &&
+       $LFS mirror create -N -E 1M -S 1M -E eof -N -E 1M -L mdt -E eof $tf &&
                error "expect failure to create mirrored file with DoM"
 
-       $LFS setstripe -E 1M -L mdt -E eof $tf
+       $LFS setstripe -E 1M -S 1M -L mdt -E eof $tf
        $LFS mirror extend -N2 $tf &&
                error "expect failure to extend mirror with DoM"
 
-       $LFS mirror create -N2 -E 1M -E eof $tf-2
+       $LFS mirror create -N2 -E 1M -S 1M -E eof $tf-2
        $LFS mirror extend -N -f $tf $tf-2 &&
                error "expect failure to extend mirrored file with DoM extent"
 
@@ -798,7 +980,7 @@ get_osc_lock_count() {
                local osc_name
                local count
 
-               osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'ffff*'
+               osc_name=${FSNAME}-OST$(printf "%04x" $((idx-1)))-osc-'[-0-9a-f]*'
                count=$($LCTL get_param -n ldlm.namespaces.$osc_name.lock_count)
                lock_count=$((lock_count + count))
        done
@@ -885,7 +1067,7 @@ test_32() {
                error "extending mirrored file $DIR/$tfile failed"
 
        # make sure the mirrored file was created successfully
-       [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
+       [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
                { $LFS getstripe $DIR/$tfile; error "expected 2 mirrors"; }
 
        drop_client_cache
@@ -903,7 +1085,7 @@ test_32() {
 }
 run_test 32 "data should be mirrored to newly created mirror"
 
-test_33() {
+test_33a() {
        [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
 
        rm -f $DIR/$tfile $DIR/$tfile-2
@@ -934,7 +1116,7 @@ test_33() {
                      "without verification failed"
 
        # make sure that $tfile has two mirrors and $tfile-2 does not exist
-       [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
+       [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
                { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
 
        [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
@@ -963,10 +1145,8 @@ test_33() {
        start_osts 1
 
        # read file again with ost2 failed
-       $LCTL set_param ldlm.namespaces.lustre-*-osc-ffff*.lru_size=clear
-
-       fail ost2 &
-       sleep 1
+       stop_osts 2
+       drop_client_cache
 
        # check size, glimpse should work
        $CHECKSTAT -t file -s $fsize $DIR/$tfile ||
@@ -977,9 +1157,116 @@ test_33() {
        [[ "$rs" == "ost1" ]] ||
                error "file content error: expected: \"ost1\", actual: \"$rs\""
 
-       wait_osc_import_state client ost2 FULL
+       start_osts 2
+}
+run_test 33a "read can choose available mirror to read"
+
+test_33b() {
+       [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
+
+       rm -f $DIR/$tfile
+
+       stack_trap "rm -f $DIR/$tfile" EXIT
+
+       # create a file with two mirrors on OST0000 and OST0001
+       $LFS setstripe -N -Eeof -o0 -N -Eeof -o1 $DIR/$tfile
+
+       # make sure that $tfile has two mirrors
+       [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
+               { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
+
+       # write 50M
+       dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
+               error "write failed for $DIR/$tfile"
+       $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
+       verify_flr_state $DIR/$tfile "ro"
+       drop_client_cache
+
+       ls -l $DIR/$tfile
+
+       # read file - all OSTs are available
+       echo "reading file (data can be provided by any ost)... "
+       local t1=$SECONDS
+       time cat $DIR/$tfile > /dev/null || error "read all"
+       local t2=$SECONDS
+       ra=$((t2 - t1))
+
+       # read file again with ost1 {OST0000} failed
+       stop_osts 1
+       drop_client_cache
+       echo "reading file (data should be provided by ost2)..."
+       t1=$SECONDS
+       time cat $DIR/$tfile > /dev/null || error "read ost2"
+       t2=$SECONDS
+       r1=$((t2 - t1))
+
+       # remount ost1
+       start_osts 1
+
+       # read file again with ost2 {OST0001} failed
+       stop_osts 2
+       drop_client_cache
+
+       echo "reading file (data should be provided by ost1)..."
+       t1=$SECONDS
+       time cat $DIR/$tfile > /dev/null || error "read ost1"
+       t2=$SECONDS
+       r2=$((t2 - t1))
+
+       # remount ost2
+       start_osts 2
+
+       [ $((r1 * 100)) -gt $((ra * 105)) -a $r1 -gt $((ra + 2)) ] &&
+               error "read mirror too slow without ost1, from $ra to $r1"
+       [ $((r2 * 100)) -gt $((ra * 105)) -a $r2 -gt $((ra + 2)) ] &&
+               error "read mirror too slow without ost2, from $ra to $r2"
+
+       wait_osc_import_ready client ost2
+}
+run_test 33b "avoid reading from unhealthy mirror"
+
+test_33c() {
+       [[ $OSTCOUNT -lt 3 ]] && skip "need >= 3 OSTs" && return
+
+       rm -f $DIR/$tfile
+
+       stack_trap "rm -f $DIR/$tfile" EXIT
+
+       # create a file with two mirrors
+       # mirror1: {OST0000, OST0001}
+       # mirror2: {OST0001, OST0002}
+       $LFS setstripe -N -Eeof -c2 -o0,1 -N -Eeof -c2 -o1,2 $DIR/$tfile
+
+       # make sure that $tfile has two mirrors
+       [ $($LFS getstripe -N $DIR/$tfile) -eq 2 ] ||
+               { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
+
+       # write 50M
+       dd if=/dev/urandom of=$DIR/$tfile bs=2M count=25 ||
+               error "write failed for $DIR/$tfile"
+       $LFS mirror resync $DIR/$tfile || error "resync failed for $DIR/$tfile"
+       verify_flr_state $DIR/$tfile "ro"
+       drop_client_cache
+
+       ls -l $DIR/$tfile
+
+       # read file - all OSTs are available
+       echo "reading file (data can be provided by any ost)... "
+       time cat $DIR/$tfile > /dev/null || error "read all"
+
+       # read file again with ost2 (OST0001) failed
+       stop_osts 2
+       drop_client_cache
+
+       echo "reading file (data should be provided by ost1 and ost3)..."
+       time cat $DIR/$tfile > /dev/null || error "read ost1 & ost3"
+
+       # remount ost2
+       start_osts 2
+
+       wait_osc_import_ready client ost2
 }
-run_test 33 "read can choose available mirror to read"
+run_test 33c "keep reading among unhealthy mirrors"
 
 test_34a() {
        [[ $OSTCOUNT -lt 4 ]] && skip "need >= 4 OSTs" && return
@@ -1093,8 +1380,8 @@ create_file_36() {
        local tf
 
        for tf in "$@"; do
-               $LFS setstripe -E 1M -E 2M -E 4M -E eof -c -1 $tf
-               $LFS setstripe -E 3M -E 6M -E eof -c -1 $tf-tmp
+               $LFS setstripe -E 1M -S 1M -E 2M -E 4M -E eof -c -1 $tf
+               $LFS setstripe -E 3M -S 1M -E 6M -E eof -c -1 $tf-tmp
 
                $LFS mirror extend -N -f $tf-tmp $tf ||
                        error "merging $tf-tmp into $tf failed"
@@ -1106,7 +1393,7 @@ test_36() {
 
        create_file_36 $tf $tf-2 $tf-3
 
-       [ $(get_mirror_ids $tf) -gt 1 ] || error "wrong mirror count"
+       [ $($LFS getstripe -N $tf) -gt 1 ] || error "wrong mirror count"
 
        # test case 1 - check file write and verify layout version
        $MULTIOP $tf oO_WRONLY:c ||
@@ -1122,13 +1409,13 @@ test_36() {
        verify_ost_layout_version $tf
 
        # test case 2
-       local mds_idx=mds$(($($LFS getstripe -M $tf-2) + 1))
+       local mds_facet=mds$(($($LFS getstripe -m $tf-2) + 1))
 
        local delay_sec=10
-       do_facet $mds_idx $LCTL set_param fail_val=$delay_sec
+       do_facet $mds_facet $LCTL set_param fail_val=$delay_sec
 
        #define OBD_FAIL_FLR_LV_DELAY 0x1A01
-       do_facet $mds_idx $LCTL set_param fail_loc=0x1A01
+       do_facet $mds_facet $LCTL set_param fail_loc=0x1A01
 
        # write should take at least $fail_loc seconds and succeed
        local st=$(date +%s)
@@ -1140,19 +1427,19 @@ test_36() {
        # verify OST layout version
        verify_ost_layout_version $tf
 
-       do_facet $mds_idx $LCTL set_param fail_loc=0
+       do_facet $mds_facet $LCTL set_param fail_loc=0
 
        # test case 3
-       mds_idx=mds$(($($LFS getstripe -M $tf-3) + 1))
+       mds_idx=mds$(($($LFS getstripe -m $tf-3) + 1))
 
        #define OBD_FAIL_FLR_LV_INC 0x1A02
-       do_facet $mds_idx $LCTL set_param fail_loc=0x1A02
+       do_facet $mds_facet $LCTL set_param fail_loc=0x1A02
 
        # write open file should return error
        $MULTIOP $tf-3 oO_WRONLY:O_SYNC:w1024c &&
                error "write a mirrored file succeeded" || true
 
-       do_facet $mds_idx $LCTL set_param fail_loc=0
+       do_facet $mds_facet $LCTL set_param fail_loc=0
 }
 run_test 36 "write to mirrored files"
 
@@ -1164,7 +1451,7 @@ create_files_37() {
 
        shift
        for tf in "$@"; do
-               $LFS setstripe -E 1M -c 1 -E eof -c -1 $tf
+               $LFS setstripe -E 1M -S 1M -c 1 -E eof -c -1 $tf
 
                dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
                $TRUNCATE $tf $fsize
@@ -1173,17 +1460,23 @@ create_files_37() {
 
 test_37()
 {
+       [ $MDS1_VERSION -lt $(version_code 2.11.57) ] &&
+               skip "Need MDS version at least 2.11.57"
+
        local tf=$DIR/$tfile
        local tf2=$DIR/$tfile-2
        local tf3=$DIR/$tfile-3
+       local tf4=$DIR/$tfile-4
 
        create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
+       rm -f $tf4
+       cp $tf $tf4
 
        # assume the mirror id will be 1, 2, and 3
        declare -A checksums
-       checksums[1]=$(md5sum $tf | cut -f 1 -d' ')
-       checksums[2]=$(md5sum $tf2 | cut -f 1 -d' ')
-       checksums[3]=$(md5sum $tf3 | cut -f 1 -d' ')
+       checksums[1]=$(cat $tf | md5sum)
+       checksums[2]=$(cat $tf2 | md5sum)
+       checksums[3]=$(cat $tf3 | md5sum)
 
        printf '%s\n' "${checksums[@]}"
 
@@ -1200,55 +1493,51 @@ test_37()
 
        local sum
        for i in ${mirror_array[@]}; do
-               sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
+               $LCTL set_param ldlm.namespaces.*.lru_size=clear > /dev/null
+               sum=$($LFS mirror read -N $i $tf | md5sum)
                [ "$sum" = "${checksums[$i]}" ] ||
                        error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
        done
 
+       # verify mirror write
+       echo "Verifying mirror write .."
+       $LFS mirror write -N2 $tf < $tf4
+
+       sum=$($LFS mirror read -N2 $tf | md5sum)
+       [[ "$sum" = "${checksums[1]}" ]] ||
+               error "2: mismatch \'${checksums[1]}\' vs. \'$sum\'"
+
        # verify mirror copy, write to this mirrored file will invalidate
        # the other two mirrors
        echo "Verifying mirror copy .."
 
        local osts=$(comma_list $(osts_nodes))
 
-       # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
-       do_nodes $osts lctl set_param fail_loc=0x241
-
-       mirror_io copy -i ${mirror_array[0]} \
-               -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
-                       error "mirror copy error"
-
-       do_nodes $osts lctl set_param fail_loc=0
+       $LFS mirror copy -i ${mirror_array[0]} -o-1 $tf ||
+               error "mirror copy error"
 
        # verify copying is successful by checking checksums
        remount_client $MOUNT
        for i in ${mirror_array[@]}; do
-               sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
+               sum=$($LFS mirror read -N $i $tf | md5sum)
                [ "$sum" = "${checksums[1]}" ] ||
-                       error "$i: mismatch checksum after copy"
+                       error "$i: mismatch checksum after copy \'$sum\'"
        done
 
        rm -f $tf
 }
 run_test 37 "mirror I/O API verification"
 
-verify_flr_state()
-{
-       local tf=$1
-       local expected_state=$2
-
-       local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
-       [ $expected_state = $state ] ||
-               error "expected: $expected_state, actual $state"
-}
-
 test_38() {
        local tf=$DIR/$tfile
        local ref=$DIR/${tfile}-ref
 
-       $LFS setstripe -E 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf
-       $LFS setstripe -E 2M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 $tf-2
-       $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3
+       $LFS setstripe -E 1M -S 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf ||
+               error "creating $tf failed"
+       $LFS setstripe -E 2M -S 1M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 \
+               $tf-2 || error "creating $tf-2 failed"
+       $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3 ||
+               error "creating $tf-3 failed"
 
        # instantiate all components
        $LFS mirror extend -N -f $tf-2 $tf ||
@@ -1265,13 +1554,13 @@ test_38() {
        local fsize=$((RANDOM << 8 + 1048576))
        $TRUNCATE $ref $fsize
 
-       local ref_cksum=$(md5sum $ref | cut -f 1 -d' ')
+       local ref_cksum=$(cat $ref | md5sum)
 
        # case 1: verify write to mirrored file & resync work
        cp $ref $tf || error "copy from $ref to $f error"
        verify_flr_state $tf "wp"
 
-       local file_cksum=$(md5sum $tf | cut -f 1 -d' ')
+       local file_cksum=$(cat $tf | md5sum)
        [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
 
        get_mirror_ids $tf
@@ -1279,8 +1568,7 @@ test_38() {
 
        local valid_mirror stale_mirror id mirror_cksum
        for id in "${mirror_array[@]}"; do
-               mirror_cksum=$(mirror_io dump -i $id $tf |
-                               md5sum | cut -f 1 -d' ')
+               mirror_cksum=$($LFS mirror read -N $id $tf | md5sum)
                [ "$ref_cksum" == "$mirror_cksum" ] &&
                        { valid_mirror=$id; continue; }
 
@@ -1293,8 +1581,7 @@ test_38() {
        mirror_io resync $tf || error "resync failed"
        verify_flr_state $tf "ro"
 
-       mirror_cksum=$(mirror_io dump -i $stale_mirror $tf |
-                       md5sum | cut -f 1 -d' ')
+       mirror_cksum=$($LFS mirror read -N $stale_mirror $tf | md5sum)
        [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
 
        # case 2: inject an error to make mirror_io exit after changing
@@ -1340,8 +1627,8 @@ test_40() {
        for ops in "conv=notrunc" ""; do
                rm -f $tf
 
-               $LFS mirror create -N -E2m -E4m -E-1  --flags=prefer \
-                               -N -E1m -E2m -E4m -E-1 $tf ||
+               $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 --flags=prefer \
+                                  -N -E 1M -E 2M -E 4M -E -1 $tf ||
                        error "create PFLR file $tf failed"
                dd if=/dev/zero of=$tf $ops bs=1M seek=2 count=1 ||
                        error "write PFLR file $tf failed"
@@ -1382,41 +1669,89 @@ test_41() {
        local tf=$DIR/$tfile
 
        rm -f $tf $tf-1
-       $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E3m -E-1 $tf ||
+       echo " **create two FLR files $tf $tf-1"
+       $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
+                          -N -E 1M -E 2M -E 3M -E -1 $tf ||
                error "create PFLR file $tf failed"
-       $LFS mirror create -N -E4m -E-1 -N -E2m -E3m -E-1 $tf-1 ||
+       $LFS mirror create -N -E 2M -S 1M -E eof \
+                          -N -E 1M -E eof --flags prefer \
+                          -N -E 4m -E eof $tf-1 ||
                error "create PFLR file $tf-1 failed"
 
        # file should be in ro status
+       echo " **verify files be RDONLY"
        verify_flr_state $tf "ro"
        verify_flr_state $tf-1 "ro"
 
        # write data in [0, 2M)
        dd if=/dev/zero of=$tf bs=1M count=2 conv=notrunc ||
                error "writing $tf failed"
-       dd if=/dev/zero of=$tf-1 bs=1M count=4 conv=notrunc ||
+       dd if=/dev/urandom of=$tf-1 bs=1M count=4 conv=notrunc ||
                error "writing $tf-1 failed"
 
+       local sum0=$(cat $tf-1 | md5sum)
+
+       echo " **verify files be WRITE_PENDING"
        verify_flr_state $tf "wp"
        verify_flr_state $tf-1 "wp"
 
        # file should have stale component
+       echo " **verify files have stale component"
        $LFS getstripe $tf | grep lcme_flags | grep stale > /dev/null ||
                error "after writing $tf, it does not contain stale component"
        $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
                error "after writing $tf-1, it does not contain stale component"
 
+       echo " **full resync"
        $LFS mirror resync $tf $tf-1 || error "mirror resync $tf $tf-1 failed"
 
+       echo " **verify $tf-1 data consistency in all mirrors"
+       for i in 1 2 3; do
+               local sum=$($LFS mirror read -N$i $tf-1 | md5sum)
+               [[ "$sum" = "$sum0" ]] ||
+                       error "$tf-1.$i: checksum mismatch: $sum != $sum0"
+       done
+
+       echo " **verify files be RDONLY"
        verify_flr_state $tf "ro"
        verify_flr_state $tf-1 "ro"
 
        # file should not have stale component
+       echo " **verify files do not contain stale component"
        $LFS getstripe $tf | grep lcme_flags | grep stale &&
                error "after resyncing $tf, it contains stale component"
        $LFS getstripe $tf-1 | grep lcme_flags | grep stale &&
                error "after resyncing $tf, it contains stale component"
 
+       # verify partial resync
+       echo " **write $tf-1 for partial resync test"
+       dd if=/dev/zero of=$tf-1 bs=1M count=2 conv=notrunc ||
+               error "writing $tf-1 failed"
+
+       echo " **only resync mirror 2"
+       verify_flr_state $tf-1 "wp"
+       $LFS mirror resync --only 2 $tf-1 ||
+               error "resync mirror 2 of $tf-1 failed"
+       verify_flr_state $tf "ro"
+
+       # resync synced mirror
+       echo " **resync mirror 2 again"
+       $LFS mirror resync --only 2 $tf-1 ||
+               error "resync mirror 2 of $tf-1 failed"
+       verify_flr_state $tf "ro"
+       echo " **verify $tf-1 contains stale component"
+       $LFS getstripe $tf-1 | grep lcme_flags | grep stale > /dev/null ||
+               error "after writing $tf-1, it does not contain stale component"
+
+       echo " **full resync $tf-1"
+       $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
+       verify_flr_state $tf "ro"
+       echo " **full resync $tf-1 again"
+       $LFS mirror resync $tf-1 || error "resync of $tf-1 failed"
+       echo " **verify $tf-1 does not contain stale component"
+       $LFS getstripe $tf | grep lcme_flags | grep stale &&
+               error "after resyncing $tf, it contains stale component"
+
        return 0
 }
 run_test 41 "lfs mirror resync check"
@@ -1437,13 +1772,13 @@ test_42() {
        $mirror_cmd $td &> /dev/null && error "$td is not a regular file"
 
        # create mirrored files
-       $LFS mirror create -N -E 4M -E 10M -E EOF $tf ||
+       $LFS mirror create -N -E 4M -S 1M -E 10M -E EOF $tf ||
                error "create mirrored file $tf failed"
-       $LFS mirror create -N -E 2M -E EOF \
+       $LFS mirror create -N -E 2M -S 1M -E EOF \
                           -N -E 6M -E 8M -E EOF \
                           -N -E 16M -E EOF $tf-1 ||
                error "create mirrored file $tf-1 failed"
-       $LFS mirror create -N -c 2 -o 1,3 -N -S 4M -c -1 $tf-2 ||
+       $LFS mirror create -N -c 2 -o 1,3 -N -S 2M -c -1 $tf-2 ||
                error "create mirrored file $tf-2 failed"
 
        # write data in [0, 10M)
@@ -1605,7 +1940,8 @@ test_44() {
                error "create remote directory failed"
        rm -f $tf $tf1 $tf.mirror~2
        # create file with 4 mirrors
-       $LFS mirror create -N -E2m -E4m -E-1 -N -E1m -E2m -E3m -E-1 -N2 $tf ||
+       $LFS mirror create -N -E 2M -S 1M -E 4M -E -1 \
+                          -N -E 1M -E 2M -E 3M -E -1 -N2 $tf ||
                error "create PFLR file $tf failed"
 
        # file should be in ro status
@@ -1617,6 +1953,10 @@ test_44() {
 
        verify_flr_state $tf "wp"
 
+       # disallow destroying the last non-stale mirror
+       ! $LFS mirror delete --mirror-id 1 $tf > /dev/null 2>&1 ||
+               error "destroying mirror 1 should fail"
+
        # synchronize all mirrors of the file
        $LFS mirror resync $tf || error "mirror resync $tf failed"
 
@@ -1642,8 +1982,17 @@ test_44() {
        verify_mirror_count $tf 2
        verify_mirror_count $tf.mirror~2 1
 
+       $LFS setstripe --comp-set -I 0x30008 --comp-flags=stale $tf ||
+               error "setting stale flag on component 0x30008 failed"
+
+       # disallow destroying the last non-stale mirror
+       ! $LFS mirror split --mirror-id 4 -d $tf > /dev/null 2>&1 ||
+               error "destroying mirror 4 should fail"
+
+       $LFS mirror resync $tf || error "resynchronizing $tf failed"
+
        $LFS mirror split --mirror-id 3 -d $tf ||
-               error "split and delte mirror 3 failed"
+               error "destroying mirror 3 failed"
        verify_mirror_count $tf 1
 
        # verify splitted file contains the same content as the orig file does
@@ -1653,6 +2002,355 @@ test_44() {
 }
 run_test 44 "lfs mirror split check"
 
+test_45() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
+
+       local file=$DIR/$tdir/$tfile
+       local dir=$DIR/$tdir/$dir
+       local temp=$DIR/$tdir/template
+       rm -rf $DIR/$tdir
+       test_mkdir $DIR/$tdir
+
+       $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
+               -N -E3m -S1m -Eeof -N -E8m -Eeof $file ||
+                       error "Create $file failed"
+
+       verify_yaml_layout $file $file.copy $temp "1. FLR file"
+       rm -f $file $file.copy
+
+       $LFS setstripe -N -E1m -S1m -c2 -o0,1 -E2m -Eeof -N -E4m -Eeof \
+               -N -E3m -S1m -Eeof -N -E8m --flags=prefer -Eeof $file ||
+                       error "Create $file failed"
+
+       verify_yaml_layout $file $file.copy $temp "2. FLR file with flags"
+}
+run_test 45 "Verify setstripe/getstripe with YAML with FLR file"
+
+verify_46() {
+       local src=$1
+       local dst=$2
+       local msg_prefix=$3
+
+       $LFS setstripe --copy=$src $dst || error "setstripe $dst failed"
+
+       local layout1=$(get_layout_param $src)
+       local layout2=$(get_layout_param $dst)
+       # compare their layout info
+       [ "$layout1" == "$layout2" ] ||
+               error "$msg_prefix $src <=> $dst layouts are not equal"
+}
+
+test_46() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
+
+       local file=$DIR/$tdir/$tfile
+       test_mkdir $DIR/$tdir
+
+       ########################### 1. PFL file #############################
+       echo "  ** 1. PFL file"
+       rm -f $file
+       $LFS setstripe -E1m -S 1M -c2 -o0,1 -E2m -c2 -E3m -o1,0 -E4m -c1 -E-1 \
+               $file || error "1. Create PFL $file failed"
+
+       rm -f $file.copy
+       verify_46 $file $file.copy "1. PFL file"
+
+       ########################### 2. plain file ###########################
+       echo "  ** 2. plain file"
+       rm -f $file
+       $LFS setstripe -c2 -o0,1 -i1 $file ||
+               error "2. Create plain $file failed"
+
+       rm -f $file.copy
+       verify_46 $file $file.copy "2. plain file"
+
+       ########################### 3. FLR file #############################
+       echo "  ** 3. FLR file"
+       rm -f $file
+       $LFS setstripe -N -E1m -S 1M -c2 -o0,1 -E4m -c1 -Eeof -N -E16m -Eeof \
+               $file || error "3. Create FLR $file failed"
+
+       rm -f $file.copy
+       verify_46 $file $file.copy "3. FLR file"
+
+       local dir=$DIR/$tdir/dir
+       ########################### 4. PFL dir ##############################
+       echo "  ** 4. PFL dir"
+       test_mkdir $dir
+       $LFS setstripe -E1m -S 1M -c2 -E2m -c1 -E-1 $dir ||
+               error "4. setstripe PFL $dir failed"
+
+       test_mkdir $dir.copy
+       verify_46 $dir $dir.copy "4. PFL dir"
+
+       ########################### 5. plain dir ############################
+       echo "  ** 5. plain dir"
+       $LFS setstripe -c2 -i-1 $dir || error "5. setstripe plain $dir failed"
+
+       verify_46 $dir $dir.copy "5. plain dir"
+
+       ########################### 6. FLR dir ##############################
+       echo "  ** 6. FLR dir"
+       $LFS setstripe -N -E1m -S 1M -c2 -E2m -c1 -Eeof -N -E4m -Eeof $dir ||
+               error "6. setstripe FLR $dir failed"
+
+       verify_46 $dir $dir.copy "6. FLR dir"
+
+       ########################### 7. SEL file ##############################
+       echo "  ** 7. SEL file"
+       rm -f $file
+       $LFS setstripe -E256M -S 1M -c2 -o0,1 -z 64M -E-1 -o1,0 -z 128M \
+               $file || error "Create $file failed"
+
+       rm -f $file.copy
+       verify_46 $file $file.copy "7. SEL file"
+
+       ########################### 8. SEL dir ##############################
+       echo "  ** 8. SEL dir"
+       $LFS setstripe -E256M -S 1M -c2 -z 64M -E-1 -z 128M \
+               $dir || error "setstripe $dir failed"
+
+       verify_46 $dir $dir.copy "8. SEL dir"
+
+       ########################### 9. FLR SEL file ##########################
+       echo "  ** 9. FLR SEL file"
+       rm -f $file
+       $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
+               -N -E1G -c4 -z128M -E-1 -z256M $file || error "Create $file failed"
+
+       rm -f $file.copy
+       verify_46 $file $file.copy "9. SEL file"
+
+       ########################### 10. FLR SEL dir #########################
+       echo "  ** 10. FLR SEL dir"
+       $LFS setstripe -N -E256M -c2 -z 64M -E-1 -z 128M \
+               -N -E1G -c4 -z128M -E-1 -z256M $dir || error "Create $file failed"
+
+       verify_46 $dir $dir.copy "10. SEL dir"
+}
+run_test 46 "Verify setstripe --copy option"
+
+test_47() {
+       [ $OSTCOUNT -lt 3 ] && skip "needs >= 3 OSTs" && return
+
+       local file=$DIR/$tdir/$tfile
+       local ids
+       local ost
+       local osts
+
+       test_mkdir $DIR/$tdir
+
+       # test case 1:
+       rm -f $file
+       # mirror1: [comp0]ost0,    [comp1]ost1 and ost2
+       # mirror2: [comp2]    ,    [comp3] should not use ost1 or ost2
+       $LFS mirror create -N -E2m -c1 -o0 --flags=prefer -Eeof -c2 -o1,2 \
+               -N -E2m -c1 -Eeof -c1 $file || error "create FLR $file failed"
+       ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
+
+       dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
+       $LFS mirror resync $file || error "resync $file failed"
+
+       ost=$($LFS getstripe -I${ids[2]} $file | awk '/l_ost_idx/{print $5}')
+       if [[ x$ost == "x0," ]]; then
+               $LFS getstripe $file
+               error "component ${ids[2]} objects allocated on $ost " \
+                     "shouldn't on OST0"
+       fi
+
+       ost=$($LFS getstripe -I${ids[3]} $file | awk '/l_ost_idx/{print $5}')
+       if [[ x$ost == "x1," || x$ost == "x2," ]]; then
+               $LFS getstripe $file
+               error "component ${ids[3]} objects allocated on $ost " \
+                     "shouldn't on OST1 or on OST2"
+       fi
+
+       ## test case 2:
+       rm -f $file
+       # mirror1: [comp0]    [comp1]
+       # mirror2: [comp2]    [comp3]
+       # mirror3: [comp4]    [comp5]
+       # mirror4: [comp6]    [comp7]
+       $LFS mirror create -N4 -E1m -c1 -Eeof -c1 $file ||
+               error "create FLR $file failed"
+       ids=($($LFS getstripe $file | awk '/lcme_id/{print $2}' | tr '\n' ' '))
+
+       dd if=/dev/zero of=$file bs=1M count=3 || error "dd $file failed"
+       $LFS mirror resync $file || error "resync $file failed"
+
+       for ((i = 0; i < 6; i++)); do
+               osts[$i]=$($LFS getstripe -I${ids[$i]} $file |
+                       awk '/l_ost_idx/{print $5}')
+       done
+       # comp[0],comp[2],comp[4] should use different osts
+       if [[ ${osts[0]} == ${osts[2]} || ${osts[0]} == ${osts[4]} ||
+             ${osts[2]} == ${osts[4]} ]]; then
+               $LFS getstripe $file
+               error "component ${ids[0]}, ${ids[2]}, ${ids[4]} have objects "\
+                     "allocated on duplicated OSTs"
+       fi
+       # comp[1],comp[3],comp[5] should use different osts
+       if [[ ${osts[1]} == ${osts[3]} || ${osts[1]} == ${osts[5]} ||
+             ${osts[3]} == ${osts[5]} ]]; then
+               $LFS getstripe $file
+               error "component ${ids[1]}, ${ids[3]}, ${ids[5]} have objects "\
+                     "allocated on duplicated OSTs"
+       fi
+
+       return 0
+}
+run_test 47 "Verify mirror obj alloc"
+
+test_48() {
+       [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
+               skip "Need MDS version at least 2.11.55"
+
+       local tf=$DIR/$tfile
+
+       rm -f $tf
+       echo " ** create 2 mirrors FLR file $tf"
+       $LFS mirror create -N -E2M -Eeof --flags prefer \
+                          -N -E1M -Eeof $tf ||
+               error "create FLR file $tf failed"
+
+       echo " ** write it"
+       dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
+       verify_flr_state $tf "wp"
+
+       local sum0=$(md5sum < $tf)
+
+       echo " ** resync the file"
+       $LFS mirror resync $tf
+
+       echo " ** snapshot mirror 2"
+       $LFS setstripe --comp-set -I 0x20003 --comp-flags=nosync $tf
+
+       echo " ** write it again"
+       dd if=/dev/urandom of=$tf bs=1M count=3 || error "write $tf failed"
+       echo " ** resync it again"
+       $LFS mirror resync $tf
+
+       verify_flr_state $tf "wp"
+       verify_comp_attr lcme_flags $tf 0x20003 nosync,stale
+
+       local sum1=$($LFS mirror read -N1 $tf | md5sum)
+       local sum2=$($LFS mirror read -N2 $tf | md5sum)
+
+       echo " ** verify mirror 2 doesn't change"
+       echo "original checksum: $sum0"
+       echo "mirror 1 checksum: $sum1"
+       echo "mirror 2 checksum: $sum2"
+       [[ $sum0 = $sum2 ]] ||
+               error "original checksum: $sum0, mirror 2 checksum: $sum2"
+       echo " ** mirror 2 stripe info"
+       $LFS getstripe -v --mirror-index=2 $tf
+
+       echo " ** resync mirror 2"
+       $LFS mirror resync --only 2 $tf
+
+       verify_flr_state $tf "ro"
+       verify_comp_attr lcme_flags $tf 0x20003 nosync,^stale
+
+       sum1=$($LFS mirror read -N1 $tf | md5sum)
+       sum2=$($LFS mirror read -N2 $tf | md5sum)
+
+       echo " ** verify mirror 2 resync-ed"
+       echo "original checksum: $sum0"
+       echo "mirror 1 checksum: $sum1"
+       echo "mirror 2 checksum: $sum2"
+       [[ $sum1 = $sum2 ]] ||
+               error "mirror 1 checksum: $sum1, mirror 2 checksum: $sum2"
+       echo " ** mirror 2 stripe info"
+       $LFS getstripe -v --mirror-index=2 $tf
+}
+run_test 48 "Verify snapshot mirror"
+
+OLDIFS="$IFS"
+cleanup_49() {
+       trap 0
+       IFS="$OLDIFS"
+}
+
+test_49a() {
+       [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
+
+       trap cleanup_49 EXIT RETURN
+
+       local file=$DIR/$tfile
+
+       $LFS setstripe -N -E eof -c1 -o1 -N -E eof -c1 -o0 $file ||
+               error "setstripe on $file"
+
+       dd if=/dev/zero of=$file bs=1M count=1 || error "dd failed for $file"
+       $LFS mirror resync $file
+
+       filefrag -ves $file || error "filefrag $file failed"
+       filefrag_op=$(filefrag -ve -k $file |
+                     sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
+
+#Filesystem type is: bd00bd0
+#File size of /mnt/lustre/f49a.sanity-flr is 1048576 (1024 blocks of 1024 bytes)
+# ext:     device_logical:        physical_offset: length:  dev: flags:
+#   0:        0..    1023:    1572864..   1573887:   1024: 0001: net,eof
+#   1:        0..    1023:    1572864..   1573887:   1024: 0000: last,net,eof
+#/mnt/lustre/f49a.sanity-flr: 2 extents found
+
+       last_lun=$(echo $filefrag_op | cut -d: -f5)
+       IFS=$'\n'
+       tot_len=0
+       num_luns=1
+       for line in $filefrag_op; do
+               frag_lun=$(echo $line | cut -d: -f5)
+               ext_len=$(echo $line | cut -d: -f4)
+               if [[ "$frag_lun" != "$last_lun" ]]; then
+                       if (( tot_len != 1024 )); then
+                               cleanup_49
+                               error "$file: OST$last_lun $tot_len != 1024"
+                       else
+                               (( num_luns += 1 ))
+                               tot_len=0
+                       fi
+               fi
+               (( tot_len += ext_len ))
+               last_lun=$frag_lun
+       done
+       if (( num_luns != 2 || tot_len != 1024 )); then
+               cleanup_49
+               error "$file: $num_luns != 2, $tot_len != 1024 on OST$last_lun"
+       fi
+
+       echo "FIEMAP on $file succeeded"
+}
+run_test 49a "FIEMAP upon FLR file"
+
+test_50() {    # EX-2179
+       mkdir -p $DIR/$tdir
+
+       local file=$DIR/$tdir/$tfile
+
+       $LFS setstripe -c1 -i0 $file || error "setstripe $file failed"
+
+       $LFS mirror extend -N -c1 -i1 $file ||
+               error "extending mirror for $file failed"
+
+       local olv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
+
+       fail mds1
+
+       $LFS mirror split -d --mirror-id=1 $file || error "split $file failed"
+
+       local flv=$($LFS getstripe $file | awk '/lcm_layout_gen/{print $2}')
+
+       echo "$file layout generation from $olv to $flv"
+       (( $flv != ($olv + 1) )) &&
+               error "split does not increase layout gen from $olv to $flv"
+
+       dd if=/dev/zero of=$file bs=1M count=1 || error "write $file failed"
+
+       $LFS getstripe -v $file || error "getstripe $file failed"
+}
+run_test 50 "mirror split update layout generation"
+
 ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
 lock_file=$(mktemp /var/lock/FLR.XXXXXX)
 
@@ -1709,9 +2407,13 @@ resync_file_200() {
 
                echo -n "resync file $tf with '$cmd' .."
 
-               $lock_taken && flock -x 200
-               $cmd $tf &> /dev/null && echo "done" || echo "failed"
-               $lock_taken && flock -u 200
+               if [[ $lock_taken = "true" ]]; then
+                       flock -x 200 -c "$cmd $tf &> /dev/null" &&
+                               echo "done" || echo "failed"
+                       flock -u 200
+               else
+                       $cmd $tf &> /dev/null && echo "done" || echo "failed"
+               fi
 
                sleep 0.$((RANDOM % 8 + 1))
        done
@@ -1722,8 +2424,8 @@ test_200() {
        local tf2=$DIR2/$tfile
        local tf3=$DIR3/$tfile
 
-       $LFS setstripe -E 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
-       $LFS setstripe -E 2M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
+       $LFS setstripe -E 1M -S 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
+       $LFS setstripe -E 2M -S 1M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
        $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
 
        $LFS mirror extend -N -f $tf-2 $tf ||
@@ -1740,7 +2442,7 @@ test_200() {
        #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
        $LCTL set_param fail_loc=0x1A03
 
-       local mds_idx=mds$(($($LFS getstripe -M $tf) + 1))
+       local mds_idx=mds$(($($LFS getstripe -m $tf) + 1))
        do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
 
        declare -a pids
@@ -1781,9 +2483,9 @@ test_200() {
        mirror_io resync $tf
        get_mirror_ids $tf
 
-       local csum=$(mirror_io dump -i ${mirror_array[0]} $tf | md5sum)
+       local csum=$($LFS mirror read -N ${mirror_array[0]} $tf | md5sum)
        for id in ${mirror_array[@]:1}; do
-               [ "$(mirror_io dump -i $id $tf | md5sum)" = "$csum" ] ||
+               [ "$($LFS mirror read -N $id $tf | md5sum)" = "$csum" ] ||
                        error "checksum error for mirror $id"
        done
 
@@ -1845,7 +2547,7 @@ test_202() {
        local tf=$DIR/$tfile
        local ids
 
-       $LFS setstripe -E 1M -c 1 $tf
+       $LFS setstripe -E 1M -S 1M -c 1 $tf
        ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' | tr '\n' ' '))
        verify_comp_attr stripe-count $tf ${ids[0]} 1
 
@@ -1861,6 +2563,457 @@ test_202() {
 }
 run_test 202 "lfs setstripe --add-component wide striping"
 
+test_203() {
+       [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
+               skip "Need MDS version at least 2.11.55"
+       [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
+
+       local tf=$DIR/$tfile
+
+       #create 2 mirrors
+       $LFS mirror create -N2 -c1 $tf || error "create FLR file $tf"
+       #delete first mirror
+       $LFS mirror delete --mirror-id=1 $tf || error "delete first mirror"
+
+       $LFS getstripe $tf
+       local old_id=$($LFS getstripe --mirror-id=2 -I $tf)
+       local count=$($LFS getstripe --mirror-id=2 -c $tf) ||
+               error "getstripe count of mirror 2"
+       [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
+
+       #extend a mirror with 2 OSTs
+       $LFS mirror extend -N -c2 $tf || error "extend mirror"
+       $LFS getstripe $tf
+
+       local new_id=$($LFS getstripe --mirror-id=2 -I $tf)
+       count=$($LFS getstripe --mirror-id=2 -c $tf) ||
+               error "getstripe count of mirror 2"
+       [[ x$oldid = x$newid ]] ||
+               error "mirror 2 changed ID from $old_id to $new_id"
+       [[ x$count = x1 ]] || error "mirror 2 stripe count $count is not 1"
+
+       count=$($LFS getstripe --mirror-id=3 -c $tf) ||
+               error "getstripe count of mirror 3"
+       [[ x$count = x2 ]] || error "mirror 3 stripe count $count is not 2"
+}
+run_test 203 "mirror file preserve mirror ID"
+
+# Simple test of FLR + self-extending layout, SEL in non-primary mirror
+test_204a() {
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local flg_opts=""
+       local found=""
+
+       test_mkdir $DIR/$tdir
+
+       # first mirror is 0-10M, then 10M-(-1), second mirror is 1M followed
+       # by extension space to -1
+       $LFS setstripe -N -E 10M -E-1 -N -E 1M -E-1 -z64M $comp_file ||
+               error "Create $comp_file failed"
+
+       # Write to first component, extending & staling second mirror
+       dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
+               error "dd to extend + stale failed"
+
+       $LFS getstripe $comp_file
+
+       flg_opts="--component-flags init,stale"
+       found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Second comp end incorrect"
+
+       flg_opts="--component-flags extension"
+       found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Third comp start incorrect"
+
+       # mirror resync should not change the extents
+       $LFS mirror resync $comp_file
+
+       flg_opts="--component-flags init"
+       found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: Second comp end incorrect"
+
+       flg_opts="--component-flags extension"
+       found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: Third comp start incorrect"
+
+       sel_layout_sanity $comp_file 5
+
+       rm -f $comp_file
+}
+run_test 204a "FLR write/stale/resync tests with self-extending mirror"
+
+# Simple test of FLR + self-extending layout, SEL in primary mirror
+test_204b() {
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local flg_opts=""
+       local found=""
+
+       test_mkdir $DIR/$tdir
+
+       # first mirror is 1M followed by extension space to -1, second mirror
+       # is 0-10M, then 10M-(-1),
+       $LFS setstripe -N -E 1M -E-1 -z64M -N -E 10M -E-1 $comp_file ||
+               error "Create $comp_file failed"
+
+       # Write to first component, extending first component & staling
+       # other mirror
+       dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc ||
+               error "dd to extend + stale failed"
+
+       $LFS getstripe $comp_file
+
+       flg_opts="--component-flags init"
+       found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: First comp end incorrect"
+
+       flg_opts="--component-flags extension"
+       found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Second comp start incorrect"
+
+       flg_opts="--component-flags init,stale"
+       found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: First mirror comp flags incorrect"
+
+       # This component is staled because it overlaps the extended first
+       # component of the primary mirror, even though it doesn't overlap
+       # the actual write - thus not inited.
+       flg_opts="--component-flags stale"
+       found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Second mirror comp flags incorrect"
+
+       # mirror resync should not change the extents
+       $LFS mirror resync $comp_file
+
+       $LFS getstripe $comp_file
+
+       flg_opts="--component-flags init"
+       found=$($LFS find --component-end 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: First comp end incorrect"
+
+       flg_opts="--component-flags extension"
+       found=$($LFS find --component-start 65M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: Second comp start incorrect"
+
+       flg_opts="--component-flags init"
+       found=$($LFS find --component-end 10M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: First mirror comp flags incorrect"
+
+       flg_opts="--component-flags init"
+       found=$($LFS find --component-start 10M $flg_opts $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: Second mirror comp flags incorrect"
+
+       sel_layout_sanity $comp_file 5
+
+       rm -f $comp_file
+}
+run_test 204b "FLR write/stale/resync tests with self-extending primary"
+
+# FLR + SEL failed extension & component removal
+# extension space in second mirror
+test_204c() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local found=""
+       local ost_idx1=0
+       local ost_name=$(ostname_from_index $ost_idx1)
+
+       test_mkdir $DIR/$tdir
+
+       # first mirror is is 0-10M, then 10M-(-1), second mirror is 0-1M, then
+       # extension space from 1M to 1G, then normal space to -1
+       $LFS setstripe -N -E 10M -E-1 -N -E 1M -E 1G -i $ost_idx1 -z 64M \
+               -E -1 $comp_file || error "Create $comp_file failed"
+
+       do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
+       sleep_maxage
+
+       # write to first comp (0 - 10M) of mirror 1, extending + staling
+       # first + second comp of mirror 2
+       dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
+       RC=$?
+
+       do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
+       sleep_maxage
+
+       [ $RC -eq 0 ] || error "dd to extend + stale failed"
+
+       $LFS getstripe $comp_file
+
+       found=$($LFS find --component-start 0m --component-end 1m \
+               --comp-flags init,stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: First mirror comp incorrect"
+
+       found=$($LFS find --component-start 1m --component-end EOF \
+               --comp-flags stale,^init $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Second mirror comp incorrect"
+
+       local mirror_id=$($LFS getstripe --component-start=1m   \
+                        --component-end=EOF $comp_file |       \
+                        grep lcme_mirror_id | awk '{ print $2 }')
+
+       [[ $mirror_id -eq 2 ]] ||
+               error "component not in correct mirror? $mirror_id"
+
+       $LFS mirror resync $comp_file
+
+       $LFS getstripe $comp_file
+
+       # component dimensions should not change from resync
+       found=$($LFS find --component-start 1m --component-end EOF \
+               --component-flags init $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: Second mirror comp incorrect"
+
+       sel_layout_sanity $comp_file 4
+
+       rm -f $comp_file
+}
+run_test 204c "FLR write/stale/resync test with component removal"
+
+# Successful repeated component in primary mirror
+test_204d() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local found=""
+
+       wait_delete_completed
+       wait_mds_ost_sync
+       test_mkdir $DIR/$tdir
+
+       # first mirror is 64M followed by extension space to -1, second mirror
+       # is 0-10M, then 10M-(-1)
+       $LFS setstripe -N -E-1 -z64M -N -E 10M -E-1 $comp_file ||
+               error "Create $comp_file failed"
+
+       local ost_idx1=$($LFS getstripe -I65537 -i $comp_file)
+       local ost_name=$(ostname_from_index $ost_idx1)
+       # degrade OST for first comp so we won't extend there
+       do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
+               obdfilter.$ost_name.degraded=1
+       sleep_maxage
+
+       # Write beyond first component, causing repeat & stale second mirror
+       dd if=/dev/zero bs=1M count=1 seek=66 of=$comp_file conv=notrunc
+       RC=$?
+
+       do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
+               obdfilter.$ost_name.degraded=0
+       sleep_maxage
+
+       [ $RC -eq 0 ] || error "dd to repeat & stale failed"
+
+       $LFS getstripe $comp_file
+
+       found=$($LFS find --component-start 64m --component-end 128m \
+               --component-flags init $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: Repeat comp incorrect"
+
+       local ost_idx2=$($LFS getstripe --component-start=64m           \
+                        --component-end=128m --component-flags=init    \
+                        -i $comp_file)
+       [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
+       local mirror_id=$($LFS getstripe --component-start=64m          \
+                        --component-end=128m --component-flags=init    \
+                        $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
+       [[ $mirror_id -eq 1 ]] ||
+               error "component not in correct mirror: $mirror_id, not 1"
+
+       $LFS mirror resync $comp_file
+
+       $LFS getstripe $comp_file
+
+       # component dimensions should not change from resync
+       found=$($LFS find --component-start 0m --component-end 64m \
+               --component-flags init $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: first comp incorrect"
+       found=$($LFS find --component-start 64m --component-end 128m \
+               --component-flags init $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: repeat comp incorrect"
+
+       sel_layout_sanity $comp_file 5
+
+       rm -f $comp_file
+}
+run_test 204d "FLR write/stale/resync sel test with repeated comp"
+
+# Successful repeated component, SEL in non-primary mirror
+test_204e() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local found=""
+
+       wait_delete_completed
+       wait_mds_ost_sync
+
+       test_mkdir $DIR/$tdir
+
+       # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
+       # space to -1 (-z 64M, so first comp is 0-64M)
+       # Note: we have to place both 1st components on OST0, otherwise 2 OSTs
+       # will be not enough - one will be degraded, the other is used on
+       # an overlapping mirror.
+       $LFS setstripe -N -E 100M -i 0 -E-1 -N -E-1 -i 0 -z 64M $comp_file ||
+               error "Create $comp_file failed"
+
+       local ost_idx1=$($LFS getstripe --component-start=0 \
+                        --component-end=64m -i $comp_file)
+       local ost_name=$(ostname_from_index $ost_idx1)
+       # degrade OST for first comp of 2nd mirror so we won't extend there
+       do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
+               obdfilter.$ost_name.degraded=1
+       sleep_maxage
+
+       $LFS getstripe $comp_file
+
+       # Write to first component, stale & instantiate second mirror components
+       # overlapping with the written component (0-100M);
+       dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
+       RC=$?
+
+       do_facet ost$((ost_idx1+1)) $LCTL set_param -n \
+               obdfilter.$ost_name.degraded=0
+       sleep_maxage
+       $LFS getstripe $comp_file
+
+       [ $RC -eq 0 ] || error "dd to repeat & stale failed"
+
+       found=$($LFS find --component-start 0m --component-end 64m \
+               --component-flags init,stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: first comp incorrect"
+
+       # was repeated due to degraded ost
+       found=$($LFS find --component-start 64m --component-end 128m \
+               --component-flags init,stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: repeated comp incorrect"
+
+       local ost_idx2=$($LFS getstripe --component-start=64m           \
+                        --component-end=128m --component-flags=init    \
+                        -i $comp_file)
+       [[ $ost_idx1 -eq $ost_idx2 ]] && error "$ost_idx1 == $ost_idx2"
+       local mirror_id=$($LFS getstripe --component-start=0m           \
+                        --component-end=64m --component-flags=init     \
+                        $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
+       [[ $mirror_id -eq 2 ]] ||
+               error "component not in correct mirror? $mirror_id"
+
+       $LFS mirror resync $comp_file
+
+       $LFS getstripe $comp_file
+
+       # component dimensions should not change from resync
+       found=$($LFS find --component-start 0m --component-end 64m \
+               --component-flags init,^stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: first comp incorrect"
+       found=$($LFS find --component-start 64m --component-end 128m \
+               --component-flags init,^stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: repeated comp incorrect"
+
+       sel_layout_sanity $comp_file 5
+
+       rm -f $comp_file
+}
+run_test 204e "FLR write/stale/resync sel test with repeated comp"
+
+# FLR + SEL: failed repeated component, SEL in non-primary mirror
+test_204f() {
+       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
+       [ "$MDS1_VERSION" -lt $(version_code $SEL_VER) ] &&
+               skip "skipped for lustre < $SEL_VER"
+
+       local comp_file=$DIR/$tdir/$tfile
+       local found=""
+
+       wait_delete_completed
+       wait_mds_ost_sync
+       test_mkdir $DIR/$tdir
+
+       pool_add $TESTNAME || error "Pool creation failed"
+       pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
+
+       # first mirror is is 0-100M, then 100M-(-1), second mirror is extension
+       # space to -1 (-z 64M, so first comp is 0-64M)
+       $LFS setstripe -N -E 100M -E-1 -N --pool="$TESTNAME" \
+               -E-1 -c 1 -z 64M $comp_file || error "Create $comp_file failed"
+
+       local ost_name0=$(ostname_from_index 0)
+       local ost_name1=$(ostname_from_index 1)
+
+       # degrade both OSTs in pool, so we'll try to repeat, then fail and
+       # extend original comp
+       do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=1
+       do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=1
+       sleep_maxage
+
+       # a write to the 1st component, 100M length, which will try to stale
+       # the first 100M of mirror 2, attempting to extend its 0-64M component
+       dd if=/dev/zero bs=2M count=1 of=$comp_file conv=notrunc
+       RC=$?
+
+       do_facet ost1 $LCTL set_param -n obdfilter.$ost_name0.degraded=0
+       do_facet ost2 $LCTL set_param -n obdfilter.$ost_name1.degraded=0
+       sleep_maxage
+
+       [ $RC -eq 0 ] || error "dd to extend mirror comp failed"
+
+       $LFS getstripe $comp_file
+
+       found=$($LFS find --component-start 0m --component-end 128m \
+               --component-flags init,stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "write: First mirror comp incorrect"
+
+       local mirror_id=$($LFS getstripe --component-start=0m           \
+                        --component-end=128m --component-flags=init    \
+                        $comp_file | grep lcme_mirror_id | awk '{ print $2 }')
+
+       [[ $mirror_id -eq 2 ]] ||
+               error "component not in correct mirror? $mirror_id, not 2"
+
+       $LFS mirror resync $comp_file
+
+       $LFS getstripe $comp_file
+
+       # component dimensions should not change from resync
+       found=$($LFS find --component-start 0m --component-end 128m \
+               --component-flags init,^stale $comp_file | wc -l)
+       [ $found -eq 1 ] || error "resync: First mirror comp incorrect"
+
+       sel_layout_sanity $comp_file 4
+
+       rm -f $comp_file
+}
+run_test 204f "FLR write/stale/resync sel w/forced extension"
+
+function test_205() {
+       local tf=$DIR/$tfile
+       local mirrors
+
+       $LFS setstripe -c1 $tf
+       $LFS mirror extend -N $tf
+       mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
+       (( $mirrors == 2 )) || error "no new mirror was created?"
+
+       $LFS mirror extend -N --flags=prefer $tf
+       mirrors=$($LFS getstripe $tf | grep lcme_mirror_id | wc -l )
+       (( $mirrors == 3 )) || error "no new mirror was created?"
+
+       $($LFS getstripe $tf | grep lcme_flags: | tail -1 | grep -q prefer) ||
+               error "prefer flag was not set on the new mirror"
+}
+run_test 205 "lfs mirror extend to set prefer flag"
 
 complete $SECONDS
 check_and_cleanup_lustre