Whamcloud - gitweb
LU-4563 Fix unsafe userspace access in many proc files
[fs/lustre-release.git] / lustre / tests / sanity.sh
index a679db8..d81cf53 100644 (file)
@@ -1,5 +1,5 @@
 #!/bin/bash
-# -*- tab-width: 4; indent-tabs-mode: t; -*-
+# -*- tab-width: 8; indent-tabs-mode: t; -*-
 #
 # Run select tests by setting ONLY, or as arguments to the script.
 # Skip specific tests by setting EXCEPT.
@@ -16,6 +16,22 @@ ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCE
 # bug number for skipped tests: LU-2036
 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
 
+is_sles11()                                            # LU-4351
+{
+       if [ -r /etc/SuSE-release ]
+       then
+               local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
+               if [ $vers -eq 11 ]
+               then
+                       return 0
+               fi
+       fi
+       return 1
+}
+
+if is_sles11; then                                     # LU-4351
+       ALWAYS_EXCEPT="$ALWAYS_EXCEPT 54c"
+fi
 
 SRCDIR=$(cd $(dirname $0); echo $PWD)
 export PATH=$PATH:/sbin
@@ -58,7 +74,7 @@ init_test_env $@
 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
 init_logging
 
-[ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b"
+[ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
 
 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
@@ -106,8 +122,8 @@ check_and_setup_lustre
 DIR=${DIR:-$MOUNT}
 assert_DIR
 
-MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
-    awk '{gsub(/_UUID/,""); print $1}' | head -1)
+MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
+       awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
@@ -193,6 +209,25 @@ test_3() {
 }
 run_test 3 "mkdir; touch; rmdir; check dir ====================="
 
+# LU-4471 - failed rmdir on remote directories still removes directory on MDT0
+test_4() {
+       local MDTIDX=1
+       local remote_dir=remote_dir
+
+       [ $MDSCOUNT -ge 2 ] && skip "skip now for LU-4690" && return #LU-4690
+       test_mkdir $DIR/$remote_dir ||
+               error "Create remote directory failed"
+
+       touch $DIR/$remote_dir/$tfile ||
+               error "Create file under remote directory failed"
+
+       rmdir $DIR/$remote_dir &&
+               error "Expect error removing in-use dir $DIR/$remote_dir"
+
+       test -d $DIR/$remote_dir || error "Remote directory disappeared"
+}
+run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
+
 test_5() {
        test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
        test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
@@ -447,6 +482,11 @@ test_17g() {
        [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
                TESTS="4094 4095"
 
+       # skip long symlink name for rhel6.5.
+       # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
+       grep -q '6.5' /etc/redhat-release &>/dev/null &&
+               TESTS="59 60 61 4062 4063"
+
        for i in $TESTS; do
                local SYMNAME=$(str_repeat 'x' $i)
                ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
@@ -460,7 +500,7 @@ test_17h() { #bug 17378
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local mdt_idx
         test_mkdir -p $DIR/$tdir
-       if [ $MDSCOUNT -gt 1 ]; then
+       if [[ $MDSCOUNT -gt 1 ]]; then
                mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
        else
                mdt_idx=0
@@ -478,7 +518,7 @@ test_17i() { #bug 20018
        test_mkdir -p $DIR/$tdir
        local foo=$DIR/$tdir/$tfile
        local mdt_idx
-       if [ $MDSCOUNT -gt 1 ]; then
+       if [[ $MDSCOUNT -gt 1 ]]; then
                mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
        else
                mdt_idx=0
@@ -492,9 +532,11 @@ test_17i() { #bug 20018
 run_test 17i "don't panic on short symlink"
 
 test_17k() { #bug 22301
-        rsync --help | grep -q xattr ||
-                skip_env "$(rsync --version| head -1) does not support xattrs"
-       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [[ -z "$(which rsync 2>/dev/null)" ]] &&
+               skip "no rsync command" && return 0
+       rsync --help | grep -q xattr ||
+               skip_env "$(rsync --version | head -n1) does not support xattrs"
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
        test_mkdir -p $DIR/$tdir
        test_mkdir -p $DIR/$tdir.new
        touch $DIR/$tdir/$tfile
@@ -505,6 +547,8 @@ test_17k() { #bug 22301
 run_test 17k "symlinks: rsync with xattrs enabled ========================="
 
 test_17l() { # LU-279
+       [[ -z "$(which getfattr 2>/dev/null)" ]] &&
+               skip "no getfattr command" && return 0
        mkdir -p $DIR/$tdir
        touch $DIR/$tdir/$tfile
        ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
@@ -571,7 +615,7 @@ test_17m() {
 
        echo "stop and checking mds${mds_index}: $cmd"
        # e2fsck should not return error
-       stop mds${mds_index} -f
+       stop mds${mds_index}
        do_facet mds${mds_index} $cmd || rc=$?
 
        start mds${mds_index} $devname $MDS_MOUNT_OPTS
@@ -588,7 +632,9 @@ check_fs_consistency_17n() {
        local cmd
        local rc=0
 
-       for mdt_index in $(seq 1 $MDSCOUNT); do
+       # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
+       # so it only check MDT1/MDT2 instead of all of MDTs.
+       for mdt_index in $(seq 1 2); do
                devname=$(mdsdevname $mdt_index)
                cmd="$E2FSCK -fnvd $devname"
 
@@ -614,7 +660,7 @@ test_17n() {
        [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
                skip "only for ldiskfs MDT" && return 0
 
-       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
 
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
 
@@ -626,18 +672,42 @@ test_17n() {
                        error "create files under remote dir failed $i"
        done
 
-       check_fs_consistency_17n || error "e2fsck report error"
+       check_fs_consistency_17n ||
+               error "e2fsck report error after create files under remote dir"
+
+       for ((i=0;i<10;i++)); do
+               rm -rf $DIR/$tdir/remote_dir_${i} ||
+                       error "destroy remote dir error $i"
+       done
+
+       check_fs_consistency_17n ||
+               error "e2fsck report error after unlink files under remote dir"
+
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
+               skip "lustre < 2.4.50 does not support migrate mv " && return
+
+       for ((i=0; i<10; i++)); do
+               mkdir -p $DIR/$tdir/remote_dir_${i}
+               createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
+                       error "create files under remote dir failed $i"
+               $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
+                       error "migrate remote dir error $i"
+       done
+       check_fs_consistency_17n || error "e2fsck report error after migration"
 
        for ((i=0;i<10;i++)); do
                rm -rf $DIR/$tdir/remote_dir_${i} ||
                        error "destroy remote dir error $i"
        done
 
-       check_fs_consistency_17n || error "e2fsck report error"
+       check_fs_consistency_17n || error "e2fsck report error after unlink"
 }
 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
 
 test_17o() {
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
+               skip "Need MDS version at least 2.3.64" && return
+
        local WDIR=$DIR/${tdir}o
        local mdt_index
        local mdtdevname
@@ -783,9 +853,9 @@ test_24c() {
 run_test 24c "rename directory to non-existent target"
 
 test_24d() {
-       test_mkdir $DIR/$tdir
-       test_mkdir $DIR/$tdir/d$testnum.1
-       test_mkdir $DIR/$tdir/d$ttestnum.2
+       test_mkdir -c1 $DIR/$tdir
+       test_mkdir -c1 $DIR/$tdir/d$testnum.1
+       test_mkdir -c1 $DIR/$tdir/d$testnum.2
        mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
        $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
        $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
@@ -824,10 +894,10 @@ test_24g() {
 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
 
 test_24h() {
-       test_mkdir $DIR/R8a
-       test_mkdir $DIR/R8b
-       test_mkdir $DIR/R8a/d
-       test_mkdir $DIR/R8b/e
+       test_mkdir -c1 $DIR/R8a
+       test_mkdir -c1 $DIR/R8b
+       test_mkdir -c1 $DIR/R8a/d
+       test_mkdir -c1 $DIR/R8b/e
        mrename $DIR/R8a/d $DIR/R8b/e
        $CHECKSTAT -a $DIR/R8a/d || error
        $CHECKSTAT -t dir $DIR/R8b/e || error
@@ -982,19 +1052,22 @@ simple_cleanup_common() {
 }
 
 max_pages_per_rpc() {
-       $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
+       $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
 }
 
 test_24v() {
        local NRFILES=100000
-       local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
-       [ $FREE_INODES -lt $NRFILES ] && \
+       local FREE_INODES=$(mdt_free_inodes 0)
+       [[ $FREE_INODES -lt $NRFILES ]] &&
                skip "not enough free inodes $FREE_INODES required $NRFILES" &&
                return
 
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        trap simple_cleanup_common EXIT
 
+       # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
+       [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
+
        mkdir -p $DIR/$tdir
        createmany -m $DIR/$tdir/$tfile $NRFILES
 
@@ -1012,9 +1085,9 @@ test_24v() {
        DIRENT_SIZE=48
        RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
        RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
-       mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
+       mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
                                awk '/^mds_readpage/ {print $2}')
-       [ $mds_readpage -gt $RPC_NUM ] && \
+       [[ $mds_readpage -gt $RPC_NUM ]] &&
                error "large readdir doesn't take effect"
 
        simple_cleanup_common
@@ -1033,7 +1106,7 @@ test_24w() { # bug21506
 run_test 24w "Reading a file larger than 4Gb"
 
 test_24x() {
-       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local MDTIDX=1
        local remote_dir=$DIR/$tdir/remote_dir
@@ -1047,6 +1120,9 @@ test_24x() {
        mkdir -p $remote_dir/tgt_dir
        touch $remote_dir/tgt_file
 
+       mrename $remote_dir $DIR/ &&
+               error "rename dir cross MDT works!"
+
        mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
                error "rename dir cross MDT works!"
 
@@ -1061,7 +1137,7 @@ test_24x() {
 run_test 24x "cross rename/link should be failed"
 
 test_24y() {
-       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local MDTIDX=1
        local remote_dir=$DIR/$tdir/remote_dir
@@ -1089,7 +1165,7 @@ test_24y() {
 run_test 24y "rename/link on the same dir should succeed"
 
 test_24z() {
-       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local MDTIDX=1
        local remote_src=$DIR/$tdir/remote_dir
@@ -1117,6 +1193,7 @@ run_test 24z "rename one remote dir to another remote dir should fail"
 test_24A() { # LU-3182
        local NFILES=5000
 
+       rm -rf $DIR/$tdir
        mkdir -p $DIR/$tdir
        createmany -m $DIR/$tdir/$tfile $NFILES
        local t=`ls $DIR/$tdir | wc -l`
@@ -1205,28 +1282,22 @@ test_27a() {
        $GETSTRIPE $DIR/d27
        $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
        $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
-       pass
        log "== test_27a: write to one stripe file ========================="
        cp /etc/hosts $DIR/d27/f0 || error
 }
 run_test 27a "one stripe file =================================="
 
 test_27b() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
        test_mkdir -p $DIR/d27
        $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
        $GETSTRIPE -c $DIR/d27/f01
        [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
                error "two-stripe file doesn't have two stripes"
-}
-run_test 27b "create two stripe file"
-
-test_27c() {
-       [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
 
        dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
 }
-run_test 27c "write to two stripe file"
+run_test 27b "create and write to two stripe file"
 
 test_27d() {
        test_mkdir -p $DIR/d27
@@ -1262,7 +1333,7 @@ run_test 27g "$GETSTRIPE with no objects"
 
 test_27i() {
        touch $DIR/d27/fsome || error "touch failed"
-       [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
+       [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
 }
 run_test 27i "$GETSTRIPE with some objects"
 
@@ -1279,10 +1350,10 @@ test_27k() { # bug 2844
        [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
        $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
        BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
-       [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
+       [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
        dd if=/dev/zero of=$FILE bs=4k count=1
        BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
-       [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
+       [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
 }
 run_test 27k "limit i_blksize for broken user apps ============="
 
@@ -1295,9 +1366,9 @@ test_27l() {
 run_test 27l "check setstripe permissions (should return error)"
 
 test_27m() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
                return
-       if [ $ORIGFREE -gt $MAXFREE ]; then
+       if [[ $ORIGFREE -gt $MAXFREE ]]; then
                skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
                return
        fi
@@ -1308,14 +1379,14 @@ test_27m() {
                error "dd should fill OST0"
        i=2
        while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
-               i=`expr $i + 1`
+               i=$((i + 1))
                [ $i -gt 256 ] && break
        done
-       i=`expr $i + 1`
+       i=$((i + 1))
        touch $DIR/$tdir/f27m_$i
        [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
                error "OST0 was full but new created file still use it"
-       i=`expr $i + 1`
+       i=$((i + 1))
        touch $DIR/$tdir/f27m_$i
        [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
                error "OST0 was full but new created file still use it"
@@ -1357,9 +1428,9 @@ exhaust_precreations() {
        # on the mdt's osc
        local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
        local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
-        osc.$mdtosc_proc1.prealloc_last_id)
+                       osc.$mdtosc_proc1.prealloc_last_id)
        local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
-        osc.$mdtosc_proc1.prealloc_next_id)
+                       osc.$mdtosc_proc1.prealloc_next_id)
 
        local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
        do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
@@ -1384,7 +1455,7 @@ exhaust_all_precreations() {
 }
 
 test_27n() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1400,7 +1471,7 @@ test_27n() {
 run_test 27n "create file with some full OSTs =================="
 
 test_27o() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1417,7 +1488,7 @@ test_27o() {
 run_test 27o "create file with all full OSTs (should error) ===="
 
 test_27p() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1440,7 +1511,7 @@ test_27p() {
 run_test 27p "append to a truncated file with some full OSTs ==="
 
 test_27q() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1463,7 +1534,7 @@ test_27q() {
 run_test 27q "append to truncated file with all OSTs full (should error) ==="
 
 test_27r() {
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1490,17 +1561,17 @@ test_27s() { # bug 10725
 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
 
 test_27t() { # bug 10864
-        WDIR=`pwd`
-        WLFS=`which lfs`
-        cd $DIR
-        touch $tfile
-        $WLFS getstripe $tfile
-        cd $WDIR
+       WDIR=$(pwd)
+       WLFS=$(which lfs)
+       cd $DIR
+       touch $tfile
+       $WLFS getstripe $tfile
+       cd $WDIR
 }
 run_test 27t "check that utils parse path correctly"
 
 test_27u() { # bug 4900
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        local index
        local list=$(comma_list $(mdts_nodes))
@@ -1514,15 +1585,15 @@ test_27u() { # bug 4900
 
        TLOG=$DIR/$tfile.getstripe
        $GETSTRIPE $DIR/$tdir > $TLOG
-       OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
+       OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
        unlinkmany $DIR/$tdir/t- 1000
-       [ $OBJS -gt 0 ] && \
-               error "$OBJS objects created on OST-0.  See $TLOG" || pass
+       [[ $OBJS -gt 0 ]] &&
+               error "$OBJS objects created on OST-0. See $TLOG" || pass
 }
 run_test 27u "skip object creation on OSC w/o objects =========="
 
 test_27v() { # bug 4900
-        [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
         remote_mds_nodsh && skip "remote MDS with nodsh" && return
         remote_ost_nodsh && skip "remote OST with nodsh" && return
@@ -1563,8 +1634,8 @@ test_27w() { # bug 10997
 run_test 27w "check $SETSTRIPE -S option"
 
 test_27wa() {
-        [ "$OSTCOUNT" -lt "2" ] &&
-                skip_env "skipping multiple stripe count/offset test" && return
+       [[ $OSTCOUNT -lt 2 ]] &&
+               skip_env "skipping multiple stripe count/offset test" && return
 
         test_mkdir -p $DIR/$tdir || error "mkdir failed"
         for i in $(seq 1 $OSTCOUNT); do
@@ -1582,7 +1653,7 @@ run_test 27wa "check $SETSTRIPE -c -i options"
 
 test_27x() {
        remote_ost_nodsh && skip "remote OST with nodsh" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        OFFSET=$(($OSTCOUNT - 1))
        OSTIDX=0
@@ -1602,7 +1673,8 @@ test_27x() {
 run_test 27x "create files while OST0 is degraded"
 
 test_27y() {
-        [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
+       [[ $OSTCOUNT -lt 2 ]] &&
+               skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
         remote_mds_nodsh && skip "remote MDS with nodsh" && return
         remote_ost_nodsh && skip "remote OST with nodsh" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -1613,8 +1685,8 @@ test_27y() {
         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
             osc.$mdtosc.prealloc_next_id)
         local fcount=$((last_id - next_id))
-        [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
-        [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
+       [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
+       [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
 
        local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
                         awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
@@ -1713,26 +1785,7 @@ check_seq_oid()
                local dev=$(ostdevname $ost)
                local oid_hex
 
-               if [ $(facet_fstype ost$ost) != ldiskfs ]; then
-                       echo "Currently only works with ldiskfs-based OSTs"
-                       continue
-               fi
-
-                log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
-
-                #don't unmount/remount the OSTs if we don't need to do that
-               # LU-2577 changes filter_fid to be smaller, so debugfs needs
-               # update too, until that use mount/ll_decode_filter_fid/mount
-               local dir=$(facet_mntpt ost$ost)
-               local opts=${OST_MOUNT_OPTS}
-
-               if !  do_facet ost$ost test -b ${dev}; then
-                       opts=$(csa_add "$opts" -o loop)
-               fi
-
-               stop ost$ost
-               do_facet ost$ost mount -t $(facet_fstype ost$ost) $opts $dev $dir ||
-                       { error "mounting $dev as $FSTYPE failed"; return 3; }
+               log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
 
                seq=$(echo $seq | sed -e "s/^0x//g")
                if [ $seq == 0 ]; then
@@ -1740,25 +1793,48 @@ check_seq_oid()
                else
                        oid_hex=$(echo $hex | sed -e "s/^0x//g")
                fi
-               local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid_hex)
-               local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
-               do_facet ost$ost umount -d $dir
-               start ost$ost $dev $OST_MOUNT_OPTS
+               local obj_file="O/$seq/d$((oid %32))/$oid_hex"
 
-               # re-enable when debugfs will understand new filter_fid
-               #local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
-                #           $dev 2>/dev/null" | grep "parent=")
+               local ff
+               #
+               # Don't unmount/remount the OSTs if we don't need to do that.
+               # LU-2577 changes filter_fid to be smaller, so debugfs needs
+               # update too, until that use mount/ll_decode_filter_fid/mount.
+               # Re-enable when debugfs will understand new filter_fid.
+               #
+               if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
+                       ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
+                               $dev 2>/dev/null" | grep "parent=")
+               else
+                       stop ost$ost
+                       mount_fstype ost$ost
+                       ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
+                               $(facet_mntpt ost$ost)/$obj_file)
+                       unmount_fstype ost$ost
+                       start ost$ost $dev $OST_MOUNT_OPTS
+               fi
 
-                [ -z "$ff" ] && error "$obj_file: no filter_fid info"
+               [ -z "$ff" ] && error "$obj_file: no filter_fid info"
 
-                echo "$ff" | sed -e 's#.*objid=#got: objid=#'
+               echo "$ff" | sed -e 's#.*objid=#got: objid=#'
 
-                # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
-                # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
-                local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
-                local ff_pseq=$(echo $ff_parent | cut -d: -f1)
-                local ff_poid=$(echo $ff_parent | cut -d: -f2)
-                local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
+               # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
+               # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
+               local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
+               local ff_pseq=$(echo $ff_parent | cut -d: -f1)
+               local ff_poid=$(echo $ff_parent | cut -d: -f2)
+               local ff_pstripe
+               if echo $ff_parent | grep -q 'stripe='; then
+                       ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
+               else
+                       #
+                       # $LL_DECODE_FILTER_FID does not print "stripe="; look
+                       # into f_ver in this case.  See the comment on
+                       # ff_parent.
+                       #
+                       ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
+                               sed -e 's/\]//')
+               fi
 
                 # compare lmm_seq and filter_fid->ff_parent.f_seq
                 [ $ff_pseq = $lmm_seq ] ||
@@ -1766,7 +1842,7 @@ check_seq_oid()
                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
                 [ $ff_poid = $lmm_oid ] ||
                         error "FF parent OID $ff_poid != $lmm_oid"
-                [ $ff_pstripe = $stripe_nr ] ||
+               (($ff_pstripe == $stripe_nr)) ||
                         error "FF stripe $ff_pstripe != $stripe_nr"
 
                 stripe_nr=$((stripe_nr + 1))
@@ -1804,13 +1880,13 @@ test_27A() { # b=19102
         local restore_count=$($GETSTRIPE -c $MOUNT)
         local restore_offset=$($GETSTRIPE -i $MOUNT)
         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
+        wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
+                error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
         local default_size=$($GETSTRIPE -S $MOUNT)
-        local default_count=$($GETSTRIPE -c $MOUNT)
         local default_offset=$($GETSTRIPE -i $MOUNT)
         local dsize=$((1024 * 1024))
         [ $default_size -eq $dsize ] ||
                 error "stripe size $default_size != $dsize"
-        [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
 }
@@ -1840,7 +1916,7 @@ test_27B() { # LU-2523
 run_test 27B "call setstripe on open unlinked file/rename victim"
 
 test_27C() { #LU-2871
-       [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
 
        declare -a ost_idx
        local index
@@ -1918,14 +1994,14 @@ test_29() {
                let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
        done
 
-       if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
-               lctl set_param -n ldlm.dump_namespaces ""
+       if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
+               $LCTL set_param -n ldlm.dump_namespaces ""
                error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
                $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
                log "dumped log to $TMP/test_29.dk (bug 5793)"
                return 2
        fi
-       if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
+       if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
                error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
                $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
                log "dumped log to $TMP/test_29.dk (bug 5793)"
@@ -1935,7 +2011,7 @@ test_29() {
 run_test 29 "IT_GETATTR regression  ============================"
 
 test_30a() { # was test_30
-       cp `which ls` $DIR || cp /bin/ls $DIR
+       cp $(which ls) $DIR || cp /bin/ls $DIR
        $DIR/ls / || error
        rm $DIR/ls
 }
@@ -2029,76 +2105,74 @@ test_31f() { # bug 4554
 run_test 31f "remove of open directory with open-unlink file ==="
 
 test_31g() {
-        echo "-- cross directory link --"
-        test_mkdir $DIR/d31ga
-        test_mkdir $DIR/d31gb
-        touch $DIR/d31ga/f
-        ln $DIR/d31ga/f $DIR/d31gb/g
-        $CHECKSTAT -t file $DIR/d31ga/f || error "source"
-        [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
-        $CHECKSTAT -t file $DIR/d31gb/g || error "target"
-        [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
+       echo "-- cross directory link --"
+       test_mkdir -c1 $DIR/${tdir}ga
+       test_mkdir -c1 $DIR/${tdir}gb
+       touch $DIR/${tdir}ga/f
+       ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
+       $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
+       [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
+       $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
+       [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
 }
 run_test 31g "cross directory link==============="
 
 test_31h() {
-        echo "-- cross directory link --"
-        test_mkdir $DIR/d31h
-        test_mkdir $DIR/d31h/dir
-        touch $DIR/d31h/f
-        ln $DIR/d31h/f $DIR/d31h/dir/g
-        $CHECKSTAT -t file $DIR/d31h/f || error "source"
-        [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
-        $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
-        [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
+       echo "-- cross directory link --"
+       test_mkdir -c1 $DIR/${tdir}
+       test_mkdir -c1 $DIR/${tdir}/dir
+       touch $DIR/${tdir}/f
+       ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
+       $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
+       [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
+       $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
+       [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
 }
 run_test 31h "cross directory link under child==============="
 
 test_31i() {
-        echo "-- cross directory link --"
-        test_mkdir $DIR/d31i
-        test_mkdir $DIR/d31i/dir
-        touch $DIR/d31i/dir/f
-        ln $DIR/d31i/dir/f $DIR/d31i/g
-        $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
-        [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
-        $CHECKSTAT -t file $DIR/d31i/g || error "target"
-        [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
+       echo "-- cross directory link --"
+       test_mkdir -c1 $DIR/$tdir
+       test_mkdir -c1 $DIR/$tdir/dir
+       touch $DIR/$tdir/dir/f
+       ln $DIR/$tdir/dir/f $DIR/$tdir/g
+       $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
+       [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
+       $CHECKSTAT -t file $DIR/$tdir/g || error "target"
+       [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
 }
 run_test 31i "cross directory link under parent==============="
 
-
 test_31j() {
-        test_mkdir $DIR/d31j
-        test_mkdir $DIR/d31j/dir1
-        ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
-        link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
-        mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
-        mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
+       test_mkdir -c1 -p $DIR/$tdir
+       test_mkdir -c1 -p $DIR/$tdir/dir1
+       ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
+       link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
+       mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
+       mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
        return 0
 }
 run_test 31j "link for directory==============="
 
-
 test_31k() {
-        test_mkdir $DIR/d31k
-        touch $DIR/d31k/s
-        touch $DIR/d31k/exist
-        mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
-        mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
-        mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
-        mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
-        mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
-        mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
-        mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
+        test_mkdir -c1 -p $DIR/$tdir
+        touch $DIR/$tdir/s
+        touch $DIR/$tdir/exist
+        mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
+        mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
+        mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
+        mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
+        mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
+        mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
+        mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
        return 0
 }
 run_test 31k "link to file: the same, non-existing, dir==============="
 
 test_31m() {
-        test_mkdir $DIR/d31m
+        mkdir $DIR/d31m
         touch $DIR/d31m/s
-        test_mkdir $DIR/d31m2
+        mkdir $DIR/d31m2
         touch $DIR/d31m2/exist
         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
@@ -2111,18 +2185,20 @@ test_31m() {
 run_test 31m "link to file: the same, non-existing, dir==============="
 
 test_31n() {
-       [ -e /proc/self/fd/173 ] && echo "skipping, fd 173 is in use" && return
        touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
        nlink=$(stat --format=%h $DIR/$tfile)
        [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
-       exec 173<$DIR/$tfile
-       trap "exec 173<&-" EXIT
-       nlink=$(stat --dereference --format=%h /proc/self/fd/173)
+       local fd=$(free_fd)
+       local cmd="exec $fd<$DIR/$tfile"
+       eval $cmd
+       cmd="exec $fd<&-"
+       trap "eval $cmd" EXIT
+       nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
        [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
        rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
-       nlink=$(stat --dereference --format=%h /proc/self/fd/173)
+       nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
        [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
-       exec 173<&-
+       eval $cmd
 }
 run_test 31n "check link count of unlinked file"
 
@@ -2142,7 +2218,7 @@ test_31o() { # LU-2901
                done
                wait
                local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
-               [ $LINKS -gt 1 ] && ls $DIR/$tdir &&
+               [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
                        error "$LINKS duplicate links to $tfile.$LOOP" &&
                        break || true
        done
@@ -2151,7 +2227,7 @@ run_test 31o "duplicate hard links with same filename"
 
 cleanup_test32_mount() {
        trap 0
-       $UMOUNT $DIR/$tdir/ext2-mountpoint
+       $UMOUNT -d $DIR/$tdir/ext2-mountpoint
 }
 
 test_32a() {
@@ -2359,7 +2435,7 @@ run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
 
 cleanup_testdir_mount() {
        trap 0
-       $UMOUNT $DIR/$tdir
+       $UMOUNT -d $DIR/$tdir
 }
 
 test_32q() {
@@ -2482,7 +2558,7 @@ test_33c() {
 run_test 33c "test llobdstat and write_bytes"
 
 test_33d() {
-       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local MDTIDX=1
        local remote_dir=$DIR/$tdir/remote_dir
@@ -2715,8 +2791,8 @@ test_36g() {
 
        echo "fmd_before: $fmd_before"
        echo "fmd_after: $fmd_after"
-       [ "$fmd_after" -gt "$fmd_before" ] && \
-               echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
+       [[ $fmd_after -gt $fmd_before ]] &&
+               echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
                error "fmd didn't expire after ping" || true
 }
 run_test 36g "filter mod data cache expiry ====================="
@@ -3095,6 +3171,81 @@ test_39m() {
 }
 run_test 39m "test atime and mtime before 1970"
 
+test_39n() { # LU-3832
+       local atime_diff=$(do_facet $SINGLEMDS \
+               lctl get_param -n mdd.*MDT0000*.atime_diff)
+       local atime0
+       local atime1
+       local atime2
+
+       do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
+
+       rm -rf $DIR/$tfile
+       dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
+       atime0=$(stat -c %X $DIR/$tfile)
+
+       sleep 5
+       $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
+       atime1=$(stat -c %X $DIR/$tfile)
+
+       sleep 5
+       cancel_lru_locks mdc
+       cancel_lru_locks osc
+       $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
+       atime2=$(stat -c %X $DIR/$tfile)
+
+       do_facet $SINGLEMDS \
+               lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
+
+       [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
+       [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
+}
+run_test 39n "check that O_NOATIME is honored"
+
+test_39o() {
+       TESTDIR=$DIR/$tdir/$tfile
+       [ -e $TESTDIR ] && rm -rf $TESTDIR
+       test_mkdir -p $TESTDIR
+       cd $TESTDIR
+       links1=2
+       ls
+       mkdir a b
+       ls
+       links2=$(stat -c %h .)
+       [ $(($links1 + 2)) != $links2 ] &&
+               error "wrong links count $(($links1 + 2)) != $links2"
+       rmdir b
+       links3=$(stat -c %h .)
+       [ $(($links1 + 1)) != $links3 ] &&
+               error "wrong links count $links1 != $links3"
+       return 0
+}
+run_test 39o "directory cached attributes updated after create ========"
+
+test_39p() {
+       [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
+       local MDTIDX=1
+       TESTDIR=$DIR/$tdir/$tfile
+       [ -e $TESTDIR ] && rm -rf $TESTDIR
+       mkdir -p $TESTDIR
+       cd $TESTDIR
+       links1=2
+       ls
+       $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
+       $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
+       ls
+       links2=$(stat -c %h .)
+       [ $(($links1 + 2)) != $links2 ] &&
+               error "wrong links count $(($links1 + 2)) != $links2"
+       rmdir remote_dir2
+       links3=$(stat -c %h .)
+       [ $(($links1 + 1)) != $links3 ] &&
+               error "wrong links count $links1 != $links3"
+       return 0
+}
+run_test 39p "remote directory cached attributes updated after create ========"
+
+
 test_40() {
        dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
        $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
@@ -3111,8 +3262,9 @@ test_41() {
 run_test 41 "test small file write + fstat ====================="
 
 count_ost_writes() {
-        lctl get_param -n osc.*.stats |
-            awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
+       lctl get_param -n osc.*.stats |
+               awk -vwrites=0 '/ost_write/ { writes += $2 } \
+                       END { printf("%0.0f", writes) }'
 }
 
 # decent default
@@ -3188,23 +3340,23 @@ test_42b() {
        setup_test42
        cancel_lru_locks osc
        stop_writeback
-        sync
-        dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
-        BEFOREWRITES=`count_ost_writes`
-        $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
-        AFTERWRITES=`count_ost_writes`
-        if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
-                error "$BEFOREWRITES < $AFTERWRITES on unlink"
-        fi
-        BEFOREWRITES=`count_ost_writes`
-        sync || error "sync: $?"
-        AFTERWRITES=`count_ost_writes`
-        if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
-                error "$BEFOREWRITES < $AFTERWRITES on sync"
-        fi
-        dmesg | grep 'error from obd_brw_async' && error 'error writing back'
+       sync
+       dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
+       BEFOREWRITES=$(count_ost_writes)
+       $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
+       AFTERWRITES=$(count_ost_writes)
+       if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
+               error "$BEFOREWRITES < $AFTERWRITES on unlink"
+       fi
+       BEFOREWRITES=$(count_ost_writes)
+       sync || error "sync: $?"
+       AFTERWRITES=$(count_ost_writes)
+       if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
+               error "$BEFOREWRITES < $AFTERWRITES on sync"
+       fi
+       dmesg | grep 'error from obd_brw_async' && error 'error writing back'
        start_writeback
-        return 0
+       return 0
 }
 run_test 42b "test destroy of file with cached dirty data ======"
 
@@ -3287,7 +3439,7 @@ test_42e() { # bug22074
                dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
                break
        done
-       [ $i -gt $warmup_files ] && error "OST0 is still cold"
+       [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
        sync
        $LCTL get_param $proc_osc0/cur_dirty_bytes
        $LCTL get_param $proc_osc0/cur_grant_bytes
@@ -3321,10 +3473,10 @@ test_42e() { # bug22074
                         $have_ppr || continue
 
                         # we only want the percent stat for < 16 pages
-                        [ $(echo $PPR | tr -d ':') -ge $pages ] && break
+                       [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
 
                         percent=$((percent + WPCT))
-                        if [ $percent -gt 15 ]; then
+                       if [[ $percent -gt 15 ]]; then
                                 error "less than 16-pages write RPCs" \
                                       "$percent% > 15%"
                                 break
@@ -3387,45 +3539,52 @@ test_43c() {
 run_test 43c "md5sum of copy into lustre========================"
 
 test_44() {
-       [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
        dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
        dd if=$DIR/f1 bs=4k count=1 > /dev/null
 }
 run_test 44 "zero length read from a sparse stripe ============="
 
 test_44a() {
-    local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
-                         awk '{print $2}'`
-    [ -z "$nstripe" ] && skip "can't get stripe info" && return
-    [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
-    local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
-                      awk '{print $2}'`
-    if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
-        nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
-    fi
+       local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
+               awk '{ print $2 }')
+       [ -z "$nstripe" ] && skip "can't get stripe info" && return
+       [[ $nstripe -gt $OSTCOUNT ]] &&
+           skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
+           return
+       local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
+               awk '{ print $2 }')
+       if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
+               nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
+                       awk '{ print $2 }')
+       fi
 
-    OFFSETS="0 $((stride/2)) $((stride-1))"
-    for offset in $OFFSETS ; do
-      for i in `seq 0 $((nstripe-1))`; do
-        local GLOBALOFFSETS=""
-        local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
-       local myfn=$DIR/d44a-$size
-       echo "--------writing $myfn at $size"
-        ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
-        GLOBALOFFSETS="$GLOBALOFFSETS $size"
-        ll_sparseness_verify $myfn $GLOBALOFFSETS \
-                            || error "ll_sparseness_verify $GLOBALOFFSETS"
-
-        for j in `seq 0 $((nstripe-1))`; do
-            size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
-            ll_sparseness_write $myfn $size || error "ll_sparseness_write"
-            GLOBALOFFSETS="$GLOBALOFFSETS $size"
-        done
-        ll_sparseness_verify $myfn $GLOBALOFFSETS \
-                            || error "ll_sparseness_verify $GLOBALOFFSETS"
-       rm -f $myfn
-      done
-    done
+       OFFSETS="0 $((stride/2)) $((stride-1))"
+       for offset in $OFFSETS; do
+               for i in $(seq 0 $((nstripe-1))); do
+                       local GLOBALOFFSETS=""
+                       # size in Bytes
+                       local size=$((((i + 2 * $nstripe )*$stride + $offset)))
+                       local myfn=$DIR/d44a-$size
+                       echo "--------writing $myfn at $size"
+                       ll_sparseness_write $myfn $size ||
+                               error "ll_sparseness_write"
+                       GLOBALOFFSETS="$GLOBALOFFSETS $size"
+                       ll_sparseness_verify $myfn $GLOBALOFFSETS ||
+                               error "ll_sparseness_verify $GLOBALOFFSETS"
+
+                       for j in $(seq 0 $((nstripe-1))); do
+                               # size in Bytes
+                               size=$((((j + $nstripe )*$stride + $offset)))
+                               ll_sparseness_write $myfn $size ||
+                                       error "ll_sparseness_write"
+                               GLOBALOFFSETS="$GLOBALOFFSETS $size"
+                       done
+                       ll_sparseness_verify $myfn $GLOBALOFFSETS ||
+                               error "ll_sparseness_verify $GLOBALOFFSETS"
+                       rm -f $myfn
+               done
+       done
 }
 run_test 44a "test sparse pwrite ==============================="
 
@@ -3451,17 +3610,18 @@ test_45() {
        stop_writeback
        sync
        do_dirty_record "echo blah > $f"
-       [ $before -eq $after ] && error "write wasn't cached"
+       [[ $before -eq $after ]] && error "write wasn't cached"
        do_dirty_record "> $f"
-       [ $before -gt $after ] || error "truncate didn't lower dirty count"
+       [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
        do_dirty_record "echo blah > $f"
-       [ $before -eq $after ] && error "write wasn't cached"
+       [[ $before -eq $after ]] && error "write wasn't cached"
        do_dirty_record "sync"
-       [ $before -gt $after ] || error "writeback didn't lower dirty count"
+       [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
        do_dirty_record "echo blah > $f"
-       [ $before -eq $after ] && error "write wasn't cached"
+       [[ $before -eq $after ]] && error "write wasn't cached"
        do_dirty_record "cancel_lru_locks osc"
-       [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
+       [[ $before -gt $after ]] ||
+               error "lock cancellation didn't lower dirty count"
        start_writeback
 }
 run_test 45 "osc io page accounting ============================"
@@ -3609,17 +3769,19 @@ run_test 48e "Access to recreated parent subdir (should return errors)"
 test_49() { # LU-1030
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        # get ost1 size - lustre-OST0000
-       ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
+       ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
+               awk '{ print $4 }')
        # write 800M at maximum
-       [ $ost1_size -gt 819200 ] && ost1_size=819200
+       [[ $ost1_size -lt 2 ]] && ost1_size=2
+       [[ $ost1_size -gt 819200 ]] && ost1_size=819200
 
-       lfs setstripe -c 1 -i 0 $DIR/$tfile
+       $SETSTRIPE -c 1 -i 0 $DIR/$tfile
        dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
        local dd_pid=$!
 
        # change max_pages_per_rpc while writing the file
        local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
-       local orig_mppc=`$LCTL get_param -n $osc1_mppc`
+       local orig_mppc=$($LCTL get_param -n $osc1_mppc)
        # loop until dd process exits
        while ps ax -opid | grep -wq $dd_pid; do
                $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
@@ -3647,7 +3809,7 @@ test_51a() {      # was test_51
        rm $DIR/$tdir/foo
        createmany -m $DIR/$tdir/longfile 201
        FNUM=202
-       while [ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]; do
+       while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
                $MCREATE $DIR/$tdir/longfile$FNUM
                FNUM=$(($FNUM + 1))
                echo -n "+"
@@ -3660,7 +3822,7 @@ run_test 51a "special situations: split htree with empty entry =="
 export NUMTEST=70000
 test_51b() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       local BASE=$DIR/$tdir
+       local BASE=$DIR/d${base}.${TESTSUITE}
 
        # cleanup the directory
        rm -fr $BASE
@@ -3669,16 +3831,16 @@ test_51b() {
 
        local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
        local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
-       [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
+       [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
                return
 
-       [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
+       [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
                echo "reduced count to $NUMTEST due to inodes"
 
        # need to check free space for the directories as well
        local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
        numfree=$((blkfree / 4))
-       [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
+       [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
                echo "reduced count to $NUMTEST due to blocks"
 
        createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
@@ -3687,7 +3849,7 @@ test_51b() {
 run_test 51b "exceed 64k subdirectory nlink limit"
 
 test_51ba() { # LU-993
-       local BASE=$DIR/$tdir
+       local BASE=$DIR/d${base}.${TESTSUITE}
        # unlink all but 100 subdirectories, then check it still works
        local LEFT=100
        [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
@@ -3726,37 +3888,45 @@ test_51ba() { # LU-993
        # regardless of whether the backing filesystem tracks nlink accurately
        # or not, the nlink count shouldn't be more than "." and ".." here
        local AFTER=$(stat -c %h $BASE)
-       [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
+       [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
                echo "nlink after: $AFTER"
 }
 run_test 51ba "verify nlink for many subdirectory cleanup"
 
 test_51d() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
-        test_mkdir -p $DIR/$tdir
-        createmany -o $DIR/$tdir/t- 1000
-        $GETSTRIPE $DIR/$tdir > $TMP/files
-        for N in `seq 0 $((OSTCOUNT - 1))`; do
-           OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
-           OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
-           log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
-        done
-        unlinkmany $DIR/$tdir/t- 1000
-
-        NLAST=0
-        for N in `seq 1 $((OSTCOUNT - 1))`; do
-           [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
-               error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
-           [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
-               error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
-
-           [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
-               error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
-           [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
-               error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
-           NLAST=$N
-        done
+       [[ $OSTCOUNT -lt 3 ]] &&
+               skip_env "skipping test with few OSTs" && return
+       test_mkdir -p $DIR/$tdir
+       createmany -o $DIR/$tdir/t- 1000
+       $GETSTRIPE $DIR/$tdir > $TMP/files
+       for N in $(seq 0 $((OSTCOUNT - 1))); do
+               OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
+                       END { printf("%0.0f", objs) }' $TMP/files)
+               OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
+                       '($1 == '$N') { objs += 1 } \
+                       END { printf("%0.0f", objs) }')
+               log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
+       done
+       unlinkmany $DIR/$tdir/t- 1000
+
+       NLAST=0
+       for N in $(seq 1 $((OSTCOUNT - 1))); do
+               [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
+                       error "OST $N has less objects vs OST $NLAST" \
+                             " (${OBJS[$N]} < ${OBJS[$NLAST]}"
+               [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
+                       error "OST $N has less objects vs OST $NLAST" \
+                             " (${OBJS[$N]} < ${OBJS[$NLAST]}"
+
+               [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
+                       error "OST $N has less #0 objects vs OST $NLAST" \
+                             " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
+               [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
+                       error "OST $N has less #0 objects vs OST $NLAST" \
+                             " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
+               NLAST=$N
+       done
 }
 run_test 51d "check object distribution ===================="
 
@@ -3817,37 +3987,53 @@ test_53() {
        local ost_last_id
        local ostnum
        local node
-       local found=0
+       local found=false
+       local support_last_seq=true
+
+       [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
+               support_last_seq=false
 
        # only test MDT0000
-        local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
-        for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
-                param=$(echo ${value[0]} | cut -d "=" -f1)
-                ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
-                param_seq=$(echo ${param} |
-                           sed -e s/prealloc_last_id/prealloc_last_seq/g)
-               mds_last_seq=$(do_facet $SINGLEMDS lctl get_param -n $param_seq)
-               mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
+       local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
+       local value
+       for value in $(do_facet $SINGLEMDS \
+                      $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
+               param=$(echo ${value[0]} | cut -d "=" -f1)
+               ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
+
+               if $support_last_seq; then
+                       param_seq=$(echo $param |
+                               sed -e s/prealloc_last_id/prealloc_last_seq/g)
+                       mds_last_seq=$(do_facet $SINGLEMDS \
+                                      $LCTL get_param -n $param_seq)
+               fi
+               mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
 
                ostnum=$(index_from_ostuuid ${ostname}_UUID)
                node=$(facet_active_host ost$((ostnum+1)))
                param="obdfilter.$ostname.last_id"
-               for ost_last in $(do_node $node lctl get_param -n $param) ; do
-                       echo "$ostname.last_id=$ost_last ;MDS.last_id=$mds_last"
-                       ost_last_id=$(echo $ost_last | awk -F':' '{print $2}' |
-                                     sed -e "s/^0x//g")
-                       ost_last_seq=$(echo $ost_last | awk -F':' '{print $1}')
-                       if [ $ost_last_seq = $mds_last_seq ]; then
-                               if [ $ost_last_id != $mds_last ]; then
-                                       error "$ost_last != $mds_last_id"
-                               else
-                                       found=1
-                                       break
-                               fi
+               for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
+                       echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
+                       ost_last_id=$ost_last
+
+                       if $support_last_seq; then
+                               ost_last_id=$(echo $ost_last |
+                                             awk -F':' '{print $2}' |
+                                             sed -e "s/^0x//g")
+                               ost_last_seq=$(echo $ost_last |
+                                              awk -F':' '{print $1}')
+                               [[ $ost_last_seq = $mds_last_seq ]] || continue
+                       fi
+
+                       if [[ $ost_last_id != $mds_last ]]; then
+                               error "$ost_last_id != $mds_last"
+                       else
+                               found=true
+                               break
                        fi
                done
-        done
-       [ $found = 0 ] && error "can not match last_seq/last_id for $mdtosc"
+       done
+       $found || error "can not match last_seq/last_id for $mdtosc"
        return 0
 }
 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
@@ -3882,6 +4068,16 @@ find_loop_dev() {
        done
 }
 
+cleanup_54c() {
+       loopdev="$DIR/loop54c"
+
+       trap 0
+       $UMOUNT -d $tdir || rc=$?
+       losetup -d $loopdev || true
+       rm $loopdev
+       return $rc
+}
+
 test_54c() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        tfile="$DIR/f54c"
@@ -3892,17 +4088,16 @@ test_54c() {
        [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
        mknod $loopdev b 7 $LOOPNUM
        echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
-       dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
+       dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
        losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
+       trap cleanup_54c EXIT
        mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
        test_mkdir -p $tdir
        mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
        dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
        df $tdir
        dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
-       $UMOUNT $tdir
-       losetup -d $loopdev
-       rm $loopdev
+       cleanup_54c
 }
 run_test 54c "block device works in lustre ====================="
 
@@ -3929,80 +4124,80 @@ run_test 54e "console/tty device works in lustre ======================"
 test_56a() {   # was test_56
         rm -rf $DIR/$tdir
         $SETSTRIPE -d $DIR
-       test_mkdir $DIR/$tdir
-        test_mkdir $DIR/$tdir/dir
+        test_mkdir -p $DIR/$tdir/dir
         NUMFILES=3
         NUMFILESx2=$(($NUMFILES * 2))
-        for i in `seq 1 $NUMFILES` ; do
+       for i in $(seq 1 $NUMFILES); do
                 touch $DIR/$tdir/file$i
                 touch $DIR/$tdir/dir/file$i
         done
 
         # test lfs getstripe with --recursive
-        FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
-        [ $FILENUM -eq $NUMFILESx2 ] ||
-                error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
-        FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
-        [ $FILENUM -eq $NUMFILES ] ||
-                error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
-        echo "$GETSTRIPE --recursive passed."
+       FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
+       [[ $FILENUM -eq $NUMFILESx2 ]] ||
+               error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
+       FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
+       [[ $FILENUM -eq $NUMFILES ]] ||
+               error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
+       echo "$GETSTRIPE --recursive passed."
 
         # test lfs getstripe with file instead of dir
-        FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
-        [ $FILENUM  -eq 1 ] || error \
-                 "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
-        echo "$GETSTRIPE file1 passed."
+       FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
+       [[ $FILENUM -eq 1 ]] ||
+               error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
+       echo "$GETSTRIPE file1 passed."
 
         #test lfs getstripe with --verbose
-        [ `$GETSTRIPE --verbose $DIR/$tdir |
-                       grep -c lmm_magic` -eq $NUMFILES ] ||
-                error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
-        [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
-            error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
-        echo "$GETSTRIPE --verbose passed."
+       [[ $($GETSTRIPE --verbose $DIR/$tdir |
+               grep -c lmm_magic) -eq $NUMFILES ]] ||
+               error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
+       [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
+               rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
+       echo "$GETSTRIPE --verbose passed."
 
         #test lfs getstripe with --obd
         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
-                                       grep -q "unknown obduuid" ||
-                error "$GETSTRIPE --obd wrong_uuid should return error message"
+               grep -q "unknown obduuid" ||
+               error "$GETSTRIPE --obd wrong_uuid should return error message"
 
-        [  "$OSTCOUNT" -lt 2 ] &&
+       [[ $OSTCOUNT -lt 2 ]] &&
                 skip_env "skipping other $GETSTRIPE --obd test" && return
 
         OSTIDX=1
         OBDUUID=$(ostuuid_from_index $OSTIDX)
-        FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
-        FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
-        [ $FOUND -eq $FILENUM ] ||
-                error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
-        [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
-                sed '/^[        ]*'${OSTIDX}'[  ]/d' |
-                sed -n '/^[     ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
-                error "$GETSTRIPE --obd: should not show file on other obd"
-        echo "$GETSTRIPE --obd passed"
+       FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
+       FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
+       [[ $FOUND -eq $FILENUM ]] ||
+               error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
+       [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
+               sed '/^[         ]*'${OSTIDX}'[  ]/d' |
+               sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
+               error "$GETSTRIPE --obd: should not show file on other obd"
+       echo "$GETSTRIPE --obd passed"
 }
 run_test 56a "check $GETSTRIPE"
 
 NUMFILES=3
 NUMDIRS=3
 setup_56() {
-        local LOCAL_NUMFILES="$1"
-        local LOCAL_NUMDIRS="$2"
-        local MKDIR_PARAMS="$3"
-
-        if [ ! -d "$TDIR" ] ; then
-                test_mkdir -p $TDIR
-                [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
-                for i in `seq 1 $LOCAL_NUMFILES` ; do
-                        touch $TDIR/file$i
-                done
-                for i in `seq 1 $LOCAL_NUMDIRS` ; do
-                        test_mkdir $TDIR/dir$i
-                        for j in `seq 1 $LOCAL_NUMFILES` ; do
-                                touch $TDIR/dir$i/file$j
-                        done
-                done
-        fi
+       local LOCAL_NUMFILES="$1"
+       local LOCAL_NUMDIRS="$2"
+       local MKDIR_PARAMS="$3"
+       local DIR_STRIPE_PARAMS="$4"
+
+       if [ ! -d "$TDIR" ] ; then
+               test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
+               [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
+               for i in `seq 1 $LOCAL_NUMFILES` ; do
+                       touch $TDIR/file$i
+               done
+               for i in `seq 1 $LOCAL_NUMDIRS` ; do
+                       test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
+                       for j in `seq 1 $LOCAL_NUMFILES` ; do
+                               touch $TDIR/dir$i/file$j
+                       done
+               done
+       fi
 }
 
 setup_56_special() {
@@ -4130,7 +4325,6 @@ run_test 56n "check lfs find -type l ============================="
 test_56o() {
        TDIR=$DIR/${tdir}o
        setup_56 $NUMFILES $NUMDIRS
-
        utime $TDIR/file1 > /dev/null || error "utime (1)"
        utime $TDIR/file2 > /dev/null || error "utime (2)"
        utime $TDIR/dir1 > /dev/null || error "utime (3)"
@@ -4244,7 +4438,7 @@ test_56s() { # LU-611
        TDIR=$DIR/${tdir}s
        setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
 
-       if [ $OSTCOUNT -gt 1 ]; then
+       if [[ $OSTCOUNT -gt 1 ]]; then
                $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
                ONESTRIPE=4
                EXTRA=4
@@ -4335,7 +4529,7 @@ test_56u() { # LU-611
        TDIR=$DIR/${tdir}u
        setup_56 $NUMFILES $NUMDIRS "-i 0"
 
-       if [ $OSTCOUNT -gt 1 ]; then
+       if [[ $OSTCOUNT -gt 1 ]]; then
                $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
                ONESTRIPE=4
        else
@@ -4366,7 +4560,7 @@ test_56u() { # LU-611
        [ $NUMS -eq $EXPECTED ] ||
                error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
 
-       if [ $OSTCOUNT -gt 1 ]; then
+       if [[ $OSTCOUNT -gt 1 ]]; then
                EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
                CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
                NUMS=$($CMD | wc -l)
@@ -4421,7 +4615,7 @@ test_56w() {
        TDIR=$DIR/${tdir}w
 
     rm -rf $TDIR || error "remove $TDIR failed"
-    setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
+    setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
 
     local stripe_size
     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
@@ -4495,7 +4689,7 @@ run_test 56w "check lfs_migrate -c stripe_count works"
 
 test_56x() {
        check_swap_layouts_support && return 0
-       [ "$OSTCOUNT" -lt "2" ] &&
+       [[ $OSTCOUNT -lt 2 ]] &&
                skip_env "need 2 OST, skipping test" && return
 
        local dir0=$DIR/$tdir/$testnum
@@ -4516,6 +4710,37 @@ test_56x() {
 }
 run_test 56x "lfs migration support"
 
+test_56y() {
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
+               skip "No HSM support on MDS of $(get_lustre_version)," \
+                        "need 2.4.53 at least" && return
+       local res=""
+
+       local dir0=$DIR/$tdir/$testnum
+       mkdir -p $dir0 || error "creating dir $dir0"
+       local f1=$dir0/file1
+       local f2=$dir0/file2
+
+       touch $f1 || error "creating std file $f1"
+       $MULTIOP $f2 H2c || error "creating released file $f2"
+
+       # a directory can be raid0, so ask only for files
+       res=$($LFIND $dir0 -L raid0 -type f | wc -l)
+       [[ $res == 2 ]] || error "search raid0: found $res files != 2"
+
+       res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
+       [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
+
+       # only files can be released, so no need to force file search
+       res=$($LFIND $dir0 -L released)
+       [[ $res == $f2 ]] || error "search released: found $res != $f2"
+
+       res=$($LFIND $dir0 \! -L released)
+       [[ $res == $f1 ]] || error "search !released: found $res != $f1"
+
+}
+run_test 56y "lfs find -L raid0|released"
+
 test_57a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        # note test will not do anything if MDS is not local
@@ -4531,8 +4756,8 @@ test_57a() {
        for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
                do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
                        error "can't access $DEV"
-               DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
-               [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
+               DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
+               [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
                rm $TMP/t57a.dump
        done
 }
@@ -4586,7 +4811,7 @@ test_57b() {
        local MDSFREE2=$(do_facet $mymds \
                lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
        local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
-       if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
+       if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
                if [ "$MDSFREE" != "$MDSFREE2" ]; then
                        error "MDC before $MDCFREE != after $MDCFREE2"
                else
@@ -4644,7 +4869,8 @@ test_60b() { # bug 6411
                                         else
                                                 print from_begin
                                 }"`
-       [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
+       [[ $LLOG_COUNT -gt 50 ]] &&
+               error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
 }
 run_test 60b "limit repeated messages from CERROR/CWARN ========"
 
@@ -4704,10 +4930,11 @@ test_62() {
 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
 
 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
-test_63a() {   # was test_63
+# Though this test is irrelevant anymore, it helped to reveal some
+# other grant bugs (LU-4482), let's keep it.
+test_63a() {   # was test_63
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
-       lctl set_param -n osc.*.max_dirty_mb 0
        for i in `seq 10` ; do
                dd if=/dev/zero of=$DIR/f63 bs=8k &
                sleep 5
@@ -4715,7 +4942,6 @@ test_63a() {      # was test_63
                sleep 1
        done
 
-       lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
        rm -f $DIR/f63 || true
 }
 run_test 63a "Verify oig_wait interruption does not crash ======="
@@ -4781,7 +5007,7 @@ run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
 
 test_65c() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       if [ $OSTCOUNT -gt 1 ]; then
+       if [[ $OSTCOUNT -gt 1 ]]; then
                test_mkdir -p $DIR/$tdir
                $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
                        -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
@@ -4794,18 +5020,18 @@ run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
 test_65d() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        test_mkdir -p $DIR/$tdir
-       if [ $STRIPECOUNT -le 0 ]; then
+       if [[ $STRIPECOUNT -le 0 ]]; then
                sc=1
-       elif [ $STRIPECOUNT -gt 2000 ]; then
+       elif [[ $STRIPECOUNT -gt 2000 ]]; then
 #LOV_MAX_STRIPE_COUNT is 2000
-               [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
+               [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
        else
                sc=$(($STRIPECOUNT - 1))
        fi
        $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
        touch $DIR/$tdir/f4 $DIR/$tdir/f5
        $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
-                                               error "lverify failed"
+               error "lverify failed"
 }
 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
 
@@ -4888,7 +5114,7 @@ run_test 65j "set default striping on root directory (bug 6367)="
 
 test_65k() { # bug11679
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
 
     echo "Check OST status: "
@@ -5168,12 +5394,12 @@ test_74a() { # bug 6149, 6184
        # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
        # will spin in a tight reconnection loop
        touch $DIR/f74a
-       lctl set_param fail_loc=0x8000030e
+       $LCTL set_param fail_loc=0x8000030e
        # get any lock that won't be difficult - lookup works.
        ls $DIR/f74a
-       lctl set_param fail_loc=0
-       true
+       $LCTL set_param fail_loc=0
        rm -f $DIR/f74a
+       true
 }
 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
 
@@ -5183,20 +5409,21 @@ test_74b() { # bug 13310
        #
        # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
        # will spin in a tight reconnection loop
-       lctl set_param fail_loc=0x8000030e
+       $LCTL set_param fail_loc=0x8000030e
        # get a "difficult" lock
        touch $DIR/f74b
-       lctl set_param fail_loc=0
-       true
+       $LCTL set_param fail_loc=0
        rm -f $DIR/f74b
+       true
 }
 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
 
 test_74c() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-#define OBD_FAIL_LDLM_NEW_LOCK
-       lctl set_param fail_loc=0x80000319
-       touch $DIR/$tfile && error "Touch successful"
+       #define OBD_FAIL_LDLM_NEW_LOCK
+       $LCTL set_param fail_loc=0x319
+       touch $DIR/$tfile && error "touch successful"
+       $LCTL set_param fail_loc=0
        true
 }
 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
@@ -5221,21 +5448,21 @@ test_76() { # Now for bug 20433, added originally in bug 1443
        # per-cpu cache is still allowed
        set_inode_slab_tunables "1 1 0"
        cancel_lru_locks osc
-       BEFORE_INODES=`num_inodes`
+       BEFORE_INODES=$(num_inodes)
        echo "before inodes: $BEFORE_INODES"
        local COUNT=1000
        [ "$SLOW" = "no" ] && COUNT=100
-       for i in `seq $COUNT`; do
+       for i in $(seq $COUNT); do
                touch $DIR/$tfile
                rm -f $DIR/$tfile
        done
        cancel_lru_locks osc
-       AFTER_INODES=`num_inodes`
+       AFTER_INODES=$(num_inodes)
        echo "after inodes: $AFTER_INODES"
        local wait=0
-       while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
+       while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
                sleep 2
-               AFTER_INODES=`num_inodes`
+               AFTER_INODES=$(num_inodes)
                wait=$((wait+2))
                echo "wait $wait seconds inodes: $AFTER_INODES"
                if [ $wait -gt 30 ]; then
@@ -5288,68 +5515,55 @@ test_77a() { # bug 10889
        set_checksums 0
        rm -f $DIR/$tfile
 }
-run_test 77a "normal checksum read/write operation ============="
+run_test 77a "normal checksum read/write operation"
 
 test_77b() { # bug 10889
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        $GSS && skip "could not run with gss" && return
        [ ! -f $F77_TMP ] && setup_f77
        #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
-       lctl set_param fail_loc=0x80000409
+       $LCTL set_param fail_loc=0x80000409
        set_checksums 1
-       dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
+
+       dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
                error "dd error: $?"
-       lctl set_param fail_loc=0
-       set_checksums 0
-}
-run_test 77b "checksum error on client write ===================="
+       $LCTL set_param fail_loc=0
 
-test_77c() { # bug 10889
-       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       $GSS && skip "could not run with gss" && return
-       [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
-       set_checksums 1
        for algo in $CKSUM_TYPES; do
                cancel_lru_locks osc
                set_checksum_type $algo
                #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
-               lctl set_param fail_loc=0x80000408
-               cmp $F77_TMP $DIR/f77b || error "file compare failed"
-               lctl set_param fail_loc=0
+               $LCTL set_param fail_loc=0x80000408
+               cmp $F77_TMP $DIR/$tfile || error "file compare failed"
+               $LCTL set_param fail_loc=0
        done
        set_checksums 0
        set_checksum_type $ORIG_CSUM_TYPE
-       rm -f $DIR/f77b
+       rm -f $DIR/$tfile
 }
-run_test 77c "checksum error on client read ==================="
+run_test 77b "checksum error on client write, read"
 
 test_77d() { # bug 10889
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        $GSS && skip "could not run with gss" && return
        #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
-       lctl set_param fail_loc=0x80000409
+       $LCTL set_param fail_loc=0x80000409
        set_checksums 1
-       directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
+       $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
                error "direct write: rc=$?"
-       lctl set_param fail_loc=0
+       $LCTL set_param fail_loc=0
        set_checksums 0
-}
-run_test 77d "checksum error on OST direct write ==============="
 
-test_77e() { # bug 10889
-       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       $GSS && skip "could not run with gss" && return
-       [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
        #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
-       lctl set_param fail_loc=0x80000408
+       $LCTL set_param fail_loc=0x80000408
        set_checksums 1
        cancel_lru_locks osc
-       directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
+       $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
                error "direct read: rc=$?"
-       lctl set_param fail_loc=0
+       $LCTL set_param fail_loc=0
        set_checksums 0
 }
-run_test 77e "checksum error on OST direct read ================"
+run_test 77d "checksum error on OST direct write, read"
 
 test_77f() { # bug 10889
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -5359,15 +5573,15 @@ test_77f() { # bug 10889
                cancel_lru_locks osc
                set_checksum_type $algo
                #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
-               lctl set_param fail_loc=0x409
-               directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
+               $LCTL set_param fail_loc=0x409
+               $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
                        error "direct write succeeded"
-               lctl set_param fail_loc=0
+               $LCTL set_param fail_loc=0
        done
        set_checksum_type $ORIG_CSUM_TYPE
        set_checksums 0
 }
-run_test 77f "repeat checksum error on write (expect error) ===="
+run_test 77f "repeat checksum error on write (expect error)"
 
 test_77g() { # bug 10889
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -5376,32 +5590,24 @@ test_77g() { # bug 10889
 
        [ ! -f $F77_TMP ] && setup_f77
 
-       $SETSTRIPE -c 1 -i 0 $DIR/f77g
+       $SETSTRIPE -c 1 -i 0 $DIR/$tfile
        #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
        do_facet ost1 lctl set_param fail_loc=0x8000021a
        set_checksums 1
-       dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
+       dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
                error "write error: rc=$?"
        do_facet ost1 lctl set_param fail_loc=0
        set_checksums 0
-}
-run_test 77g "checksum error on OST write ======================"
-
-test_77h() { # bug 10889
-       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       $GSS && skip "could not run with gss" && return
-       remote_ost_nodsh && skip "remote OST with nodsh" && return
 
-       [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
        cancel_lru_locks osc
        #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
        do_facet ost1 lctl set_param fail_loc=0x8000021b
        set_checksums 1
-       cmp $F77_TMP $DIR/f77g || error "file compare failed"
+       cmp $F77_TMP $DIR/$tfile || error "file compare failed"
        do_facet ost1 lctl set_param fail_loc=0
        set_checksums 0
 }
-run_test 77h "checksum error on OST read ======================="
+run_test 77g "checksum error on OST write, read"
 
 test_77i() { # bug 13805
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -5417,7 +5623,7 @@ test_77i() { # bug 13805
        done
        remount_client $MOUNT
 }
-run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
+run_test 77i "client not supporting OSD_CONNECT_CKSUM"
 
 test_77j() { # bug 13805
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -5434,7 +5640,7 @@ test_77j() { # bug 13805
        done
        remount_client $MOUNT
 }
-run_test 77j "client only supporting ADLER32 ===================="
+run_test 77j "client only supporting ADLER32"
 
 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
 rm -f $F77_TMP
@@ -5449,35 +5655,37 @@ test_78() { # bug 10901
        echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
        MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
        echo "MemTotal: $MEMTOTAL"
-# reserve 256MB of memory for the kernel and other running processes,
-# and then take 1/2 of the remaining memory for the read/write buffers.
-    if [ $MEMTOTAL -gt 512 ] ;then
-        MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
-    else
-        # for those poor memory-starved high-end clusters...
-        MEMTOTAL=$((MEMTOTAL / 2))
-    fi
+
+       # reserve 256MB of memory for the kernel and other running processes,
+       # and then take 1/2 of the remaining memory for the read/write buffers.
+       if [ $MEMTOTAL -gt 512 ] ;then
+               MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
+       else
+               # for those poor memory-starved high-end clusters...
+               MEMTOTAL=$((MEMTOTAL / 2))
+       fi
        echo "Mem to use for directio: $MEMTOTAL"
-       [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
-       [ $F78SIZE -gt 512 ] && F78SIZE=512
-       [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
-       SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
+
+       [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
+       [[ $F78SIZE -gt 512 ]] && F78SIZE=512
+       [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
+       SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
+               head -n1)
        echo "Smallest OST: $SMALLESTOST"
-       [ $SMALLESTOST -lt 10240 ] && \
+       [[ $SMALLESTOST -lt 10240 ]] &&
                skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
 
-       [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
+       [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
                F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
 
        [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
        echo "File size: $F78SIZE"
        $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
-       for i in `seq 1 $NSEQ`
-       do
-               FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
-               echo directIO rdwr round $i of $NSEQ
-               $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
-       done
+       for i in $(seq 1 $NSEQ); do
+               FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
+               echo directIO rdwr round $i of $NSEQ
+               $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
+       done
 
        rm -f $DIR/$tfile
 }
@@ -5716,6 +5924,7 @@ cleanup_101a() {
 
 test_101a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
        local s
        local discard
        local nreads=10000
@@ -5733,13 +5942,13 @@ test_101a() {
        $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
 
        discard=0
-        for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
-               get_named_value 'read but discarded' | cut -d" " -f1`; do
+       for s in $($LCTL get_param -n llite.*.read_ahead_stats |
+               get_named_value 'read but discarded' | cut -d" " -f1); do
                        discard=$(($discard + $s))
        done
        cleanup_101a
 
-       if [ $(($discard * 10)) -gt $nreads ] ;then
+       if [[ $(($discard * 10)) -gt $nreads ]]; then
                $LCTL get_param osc.*-osc*.rpc_stats
                $LCTL get_param llite.*.read_ahead_stats
                error "too many ($discard) discarded pages"
@@ -5750,10 +5959,12 @@ run_test 101a "check read-ahead for random reads ================"
 
 setup_test101bc() {
        test_mkdir -p $DIR/$tdir
-       STRIPE_SIZE=1048576
-       STRIPE_COUNT=$OSTCOUNT
+       local STRIPE_SIZE=$1
+       local FILE_LENGTH=$2
        STRIPE_OFFSET=0
 
+       local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
+
        local list=$(comma_list $(osts_nodes))
        set_osd_param $list '' read_cache_enable 0
        set_osd_param $list '' writethrough_cache_enable 0
@@ -5762,7 +5973,9 @@ setup_test101bc() {
        # prepare the read-ahead file
        $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
 
-       dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
+       dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
+                               count=$FILE_SIZE_MB 2> /dev/null
+
 }
 
 cleanup_test101bc() {
@@ -5781,16 +5994,16 @@ calc_total() {
 
 ra_check_101() {
        local READ_SIZE=$1
-       local STRIPE_SIZE=1048576
+       local STRIPE_SIZE=$2
+       local FILE_LENGTH=$3
        local RA_INC=1048576
        local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
-       local FILE_LENGTH=$((64*100))
        local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
                             (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
-       DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
-                       get_named_value 'read but discarded' | \
-                       cut -d" " -f1 | calc_total`
-       if [ $DISCARD -gt $discard_limit ]; then
+       DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
+                       get_named_value 'read but discarded' |
+                       cut -d" " -f1 | calc_total)
+       if [[ $DISCARD -gt $discard_limit ]]; then
                $LCTL get_param llite.*.read_ahead_stats
                error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
        else
@@ -5800,13 +6013,20 @@ ra_check_101() {
 
 test_101b() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
+       [[ $OSTCOUNT -lt 2 ]] &&
+               skip_env "skipping stride IO stride-ahead test" && return
        local STRIPE_SIZE=1048576
        local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
-       local FILE_LENGTH=$((STRIPE_SIZE*100))
-       local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
+       if [ $SLOW == "yes" ]; then
+               local FILE_LENGTH=$((STRIDE_SIZE * 64))
+       else
+               local FILE_LENGTH=$((STRIDE_SIZE * 8))
+       fi
+
+       local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
+
        # prepare the read-ahead file
-       setup_test101bc
+       setup_test101bc $STRIPE_SIZE $FILE_LENGTH
        cancel_lru_locks osc
        for BIDX in 2 4 8 16 32 64 128 256
        do
@@ -5818,7 +6038,7 @@ test_101b() {
                $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
                              -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
                cancel_lru_locks osc
-               ra_check_101 $BSIZE
+               ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
        done
        cleanup_test101bc
        true
@@ -5830,126 +6050,118 @@ test_101c() {
        local STRIPE_SIZE=1048576
        local FILE_LENGTH=$((STRIPE_SIZE*100))
        local nreads=10000
-       local osc
-
-    setup_test101bc
+       local osc_rpc_stats
 
-    cancel_lru_locks osc
-    $LCTL set_param osc.*.rpc_stats 0
-    $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
-    for osc in $($LCTL get_param -N osc.*); do
-        if [ "$osc" == "osc.num_refs" ]; then
-            continue
-        fi
-
-        local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
-        if [ $lines -le 20 ]; then
-            continue
-        fi
+       setup_test101bc $STRIPE_SIZE $FILE_LENGTH
 
-        local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
-                                     awk '$1 == "1:" { print $2; exit; }')
-        local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
-                                     awk '$1 == "2:" { print $2; exit; }')
-        local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
-                                     awk '$1 == "4:" { print $2; exit; }')
-        local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
-                                     awk '$1 == "8:" { print $2; exit; }')
-
-        [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
-        [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
-        [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
-        [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
-        echo "${osc} rpc check passed!"
-    done
-    cleanup_test101bc
-    true
+       cancel_lru_locks osc
+       $LCTL set_param osc.*.rpc_stats 0
+       $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
+       for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
+               local stats=$($LCTL get_param -n $osc_rpc_stats)
+               local lines=$(echo "$stats" | awk 'END {print NR;}')
+               local size
+
+               if [ $lines -le 20 ]; then
+                       continue
+               fi
+               for size in 1 2 4 8; do
+                       local rpc=$(echo "$stats" |
+                                   awk '($1 == "'$size':") {print $2; exit; }')
+                       [ $rpc != 0 ] &&
+                               error "Small $((size*4))k read IO $rpc !"
+               done
+               echo "$osc_rpc_stats check passed!"
+       done
+       cleanup_test101bc
+       true
 }
 run_test 101c "check stripe_size aligned read-ahead ================="
 
 set_read_ahead() {
-   $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
-   $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
+       $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
+       $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
 }
 
 test_101d() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        local file=$DIR/$tfile
-       local size=${FILESIZE_101c:-500}
+       local sz_MB=${FILESIZE_101d:-500}
        local ra_MB=${READAHEAD_MB:-40}
 
-       local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
-       [ $space -gt $((size * 1024)) ] ||
-               { skip "Need free space ${size}M, have $space" && return; }
+       local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
+       [ $free_MB -lt $sz_MB ] &&
+               skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
 
-    echo Creating ${size}M test file $file
-    dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
-    echo Cancel LRU locks on lustre client to flush the client cache
-    cancel_lru_locks osc
+       echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
+       $SETSTRIPE -c -1 $file || error "setstripe failed"
+
+       dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
+       echo Cancel LRU locks on lustre client to flush the client cache
+       cancel_lru_locks osc
 
-    echo Disable read-ahead
-    local old_READAHEAD=$(set_read_ahead 0)
+       echo Disable read-ahead
+       local old_READAHEAD=$(set_read_ahead 0)
 
-    echo Reading the test file $file with read-ahead disabled
-    time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
+       echo Reading the test file $file with read-ahead disabled
+       local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
 
-    echo Cancel LRU locks on lustre client to flush the client cache
-    cancel_lru_locks osc
-    echo Enable read-ahead with ${ra_MB}MB
-    set_read_ahead $ra_MB
+       echo Cancel LRU locks on lustre client to flush the client cache
+       cancel_lru_locks osc
+       echo Enable read-ahead with ${ra_MB}MB
+       set_read_ahead $ra_MB
 
-    echo Reading the test file $file with read-ahead enabled
-    time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
+       echo Reading the test file $file with read-ahead enabled
+       local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
 
-    echo read-ahead disabled time read $time_ra_OFF
-    echo read-ahead enabled  time read $time_ra_ON
+       echo "read-ahead disabled time read $raOFF"
+       echo "read-ahead enabled  time read $raON"
 
        set_read_ahead $old_READAHEAD
        rm -f $file
        wait_delete_completed
 
-    [ $time_ra_ON -lt $time_ra_OFF ] ||
-        error "read-ahead enabled  time read (${time_ra_ON}s) is more than
-               read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
+       [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
+               error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
 }
-run_test 101d "file read with and without read-ahead enabled  ================="
+run_test 101d "file read with and without read-ahead enabled"
 
 test_101e() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-    local file=$DIR/$tfile
-    local size=500  #KB
-    local count=100
-    local blksize=1024
-
-    local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
-    local need_space=$((count * size))
-    [ $space -gt $need_space ] ||
-        { skip_env "Need free space $need_space, have $space" && return; }
-
-    echo Creating $count ${size}K test files
-    for ((i = 0; i < $count; i++)); do
-        dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
-    done
+       local file=$DIR/$tfile
+       local size_KB=500  #KB
+       local count=100
+       local bsize=1024
+
+       local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
+       local need_KB=$((count * size_KB))
+       [[ $free_KB -le $need_KB ]] &&
+               skip_env "Need free space $need_KB, have $free_KB" && return
+
+       echo "Creating $count ${size_KB}K test files"
+       for ((i = 0; i < $count; i++)); do
+               dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
+       done
 
-    echo Cancel LRU locks on lustre client to flush the client cache
-    cancel_lru_locks osc
+       echo "Cancel LRU locks on lustre client to flush the client cache"
+       cancel_lru_locks osc
 
-    echo Reset readahead stats
-    $LCTL set_param -n llite.*.read_ahead_stats 0
+       echo "Reset readahead stats"
+       $LCTL set_param -n llite.*.read_ahead_stats 0
 
-    for ((i = 0; i < $count; i++)); do
-        dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
-    done
+       for ((i = 0; i < $count; i++)); do
+               dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
+       done
 
-    local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
-          get_named_value 'misses' | cut -d" " -f1 | calc_total)
+       local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
+                    get_named_value 'misses' | cut -d" " -f1 | calc_total)
 
-    for ((i = 0; i < $count; i++)); do
-        rm -rf ${file}_${i} 2>/dev/null
-    done
+       for ((i = 0; i < $count; i++)); do
+               rm -rf $file.$i 2>/dev/null
+       done
 
-    #10000 means 20% reads are missing in readahead
-    [ $miss -lt 10000 ] ||  error "misses too much for small reads"
+       #10000 means 20% reads are missing in readahead
+       [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
 }
 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
 
@@ -5995,7 +6207,7 @@ setup_test102() {
        STRIPE_SIZE=65536
        STRIPE_OFFSET=1
        STRIPE_COUNT=$OSTCOUNT
-       [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
+       [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
 
        trap cleanup_test102 EXIT
        cd $DIR
@@ -6082,36 +6294,43 @@ test_102a() {
 run_test 102a "user xattr test =================================="
 
 test_102b() {
+       [ -z "$(which setfattr 2>/dev/null)" ] &&
+               skip_env "could not find setfattr" && return
+
        # b10930: get/set/list trusted.lov xattr
        echo "get/set/list trusted.lov xattr ..."
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
        local testfile=$DIR/$tfile
        $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
                error "setstripe failed"
        local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
                error "getstripe failed"
-       getfattr -d -m "^trusted" $testfile 2> /dev/null | \
-       grep "trusted.lov" || error "can't get trusted.lov from $testfile"
+       getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
+               error "can't get trusted.lov from $testfile"
 
        local testfile2=${testfile}2
-       local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
-                    grep "trusted.lov" |sed -e 's/[^=]\+=//'`
+       local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
+                       grep "trusted.lov" | sed -e 's/[^=]\+=//')
 
        $MCREATE $testfile2
        setfattr -n trusted.lov -v $value $testfile2
        local stripe_size=$($GETSTRIPE -S $testfile2)
        local stripe_count=$($GETSTRIPE -c $testfile2)
-       [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
-       [ $stripe_count -eq $STRIPECOUNT ] ||
+       [[ $stripe_size -eq 65536 ]] ||
+               error "stripe size $stripe_size != 65536"
+       [[ $stripe_count -eq $STRIPECOUNT ]] ||
                error "stripe count $stripe_count != $STRIPECOUNT"
        rm -f $DIR/$tfile
 }
 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
 
 test_102c() {
+       [ -z "$(which setfattr 2>/dev/null)" ] &&
+               skip_env "could not find setfattr" && return
+
        # b10930: get/set/list lustre.lov xattr
        echo "get/set/list lustre.lov xattr ..."
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
        test_mkdir -p $DIR/$tdir
        chown $RUNAS_ID $DIR/$tdir
        local testfile=$DIR/$tdir/$tfile
@@ -6145,14 +6364,14 @@ compare_stripe_info1() {
                                local size=$((STRIPE_SIZE * num))
                                local file=file"$num-$offset-$count"
                                stripe_size=$(lfs getstripe -S $PWD/$file)
-                               [ $stripe_size -ne $size ] &&
+                               [[ $stripe_size -ne $size ]] &&
                                    error "$file: size $stripe_size != $size"
                                stripe_count=$(lfs getstripe -c $PWD/$file)
                                # allow fewer stripes to be created, ORI-601
-                               [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
+                               [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
                                    error "$file: count $stripe_count != $count"
                                stripe_index=$(lfs getstripe -i $PWD/$file)
-                               [ $stripe_index -ne 0 ] &&
+                               [[ $stripe_index -ne 0 ]] &&
                                        stripe_index_all_zero=false
                        done
                done
@@ -6172,7 +6391,7 @@ test_102d() {
        # b10930: tar test for trusted.lov xattr
        TAR=$(find_lustre_tar)
        [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
        setup_test102
        test_mkdir -p $DIR/d102d
        $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
@@ -6186,7 +6405,7 @@ test_102f() {
        # b10930: tar test for trusted.lov xattr
        TAR=$(find_lustre_tar)
        [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
        setup_test102
        test_mkdir -p $DIR/d102f
        cd $DIR
@@ -6249,11 +6468,16 @@ test_102ha() {
 run_test 102ha "grow xattr from inside inode to external inode"
 
 test_102i() { # bug 17038
-        touch $DIR/$tfile
-        ln -s $DIR/$tfile $DIR/${tfile}link
-        getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
-        getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 | grep -i "no such attr" || error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
-        rm -f $DIR/$tfile $DIR/${tfile}link
+       [ -z "$(which getfattr 2>/dev/null)" ] &&
+               skip "could not find getfattr" && return
+       touch $DIR/$tfile
+       ln -s $DIR/$tfile $DIR/${tfile}link
+       getfattr -n trusted.lov $DIR/$tfile ||
+               error "lgetxattr on $DIR/$tfile failed"
+       getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
+               grep -i "no such attr" ||
+               error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
+       rm -f $DIR/$tfile $DIR/${tfile}link
 }
 run_test 102i "lgetxattr test on symbolic link ============"
 
@@ -6261,7 +6485,7 @@ test_102j() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        TAR=$(find_lustre_tar)
        [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
        setup_test102 "$RUNAS"
        test_mkdir -p $DIR/d102j
        chown $RUNAS_ID $DIR/d102j
@@ -6272,6 +6496,8 @@ test_102j() {
 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
 
 test_102k() {
+       [ -z "$(which setfattr 2>/dev/null)" ] &&
+               skip "could not find setfattr" && return
         touch $DIR/$tfile
         # b22187 just check that does not crash for regular file.
         setfattr -n trusted.lov $DIR/$tfile
@@ -6298,6 +6524,9 @@ test_102k() {
 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
 
 test_102l() {
+       [ -z "$(which getfattr 2>/dev/null)" ] &&
+               skip "could not find getfattr" && return
+
        # LU-532 trusted. xattr is invisible to non-root
        local testfile=$DIR/$tfile
 
@@ -6311,10 +6540,109 @@ test_102l() {
 
        return 0;
 }
-run_test 102l "listxattr filter test =================================="
+run_test 102l "listxattr size test =================================="
+
+test_102m() { # LU-3403 llite: error of listxattr when buffer is small
+       local path=$DIR/$tfile
+       touch $path
+
+       listxattr_size_check $path || error "listattr_size_check $path failed"
+}
+run_test 102m "Ensure listxattr fails on small bufffer ========"
 
 cleanup_test102
 
+getxattr() { # getxattr path name
+       # Return the base64 encoding of the value of xattr name on path.
+       local path=$1
+       local name=$2
+
+       # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
+       # file: $path
+       # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
+       #
+       # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
+
+       getfattr --absolute-names --encoding=base64 --name=$name $path |
+               awk -F= -v name=$name '$1 == name {
+                       print substr($0, index($0, "=") + 1);
+       }'
+}
+
+test_102n() { # LU-4101 mdt: protect internal xattrs
+       local file0=$DIR/$tfile.0
+       local file1=$DIR/$tfile.1
+       local xattr0=$TMP/$tfile.0
+       local xattr1=$TMP/$tfile.1
+       local name
+       local value
+
+       [ -z "$(which setfattr 2>/dev/null)" ] &&
+               skip "could not find setfattr" && return
+
+       if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
+       then
+               skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
+               return
+       fi
+
+       rm -rf $file0 $file1 $xattr0 $xattr1
+       touch $file0 $file1
+
+       # Get 'before' xattrs of $file1.
+       getfattr --absolute-names --dump --match=- $file1 > $xattr0
+
+       for name in lov lma lmv link fid version som hsm lfsck_namespace; do
+               # Try to copy xattr from $file0 to $file1.
+               value=$(getxattr $file0 trusted.$name 2> /dev/null)
+
+               setfattr --name=trusted.$name --value="$value" $file1 ||
+                       error "setxattr 'trusted.$name' failed"
+
+               # Try to set a garbage xattr.
+               value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
+
+               setfattr --name=trusted.$name --value="$value" $file1 ||
+                       error "setxattr 'trusted.$name' failed"
+
+               # Try to remove the xattr from $file1. We don't care if this
+               # appears to succeed or fail, we just don't want there to be
+               # any changes or crashes.
+               setfattr --remove=$trusted.$name $file1 2> /dev/null
+       done
+
+       # Get 'after' xattrs of file1.
+       getfattr --absolute-names --dump --match=- $file1 > $xattr1
+
+       if ! diff $xattr0 $xattr1; then
+               error "before and after xattrs of '$file1' differ"
+       fi
+
+       rm -rf $file0 $file1 $xattr0 $xattr1
+
+       return 0
+}
+run_test 102n "silently ignore setxattr on internal trusted xattrs"
+
+test_102p() { # LU-4703 setxattr did not check ownership
+       local testfile=$DIR/$tfile
+
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
+               skip "MDS needs to be at least 2.5.56" && return
+
+       touch $testfile
+
+       echo "setfacl as user..."
+       $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
+       [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
+
+       echo "setfattr as user..."
+       setfacl -m "u:$RUNAS_ID:---" $testfile
+       $RUNAS setfattr -x system.posix_acl_access $testfile
+       [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
+}
+run_test 102p "check setxattr(2) correctly fails without permission"
+
 run_acl_subtest()
 {
     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
@@ -6365,7 +6693,11 @@ test_103 () {
        fi
 
        echo "LU-2561 newly created file is same size as directory..."
-       run_acl_subtest 2561 || error "LU-2561 test failed"
+       if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
+               run_acl_subtest 2561 || error "LU-2561 test failed"
+       else
+               run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
+       fi
 
        cd $SAVE_PWD
        umask $SAVE_UMASK
@@ -6525,30 +6857,29 @@ run_test 110 "filename length checking"
 
 test_115() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
-           cut -c11-20)
-        [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
-           return
-        echo "Starting with $OSTIO_pre threads"
+       OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
+               tail -1 | cut -c11-20)
+       [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
+       echo "Starting with $OSTIO_pre threads"
 
        NUMTEST=20000
-       NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
-       [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
+       NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
+       [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
        echo "$NUMTEST creates/unlinks"
        test_mkdir -p $DIR/$tdir
        createmany -o $DIR/$tdir/$tfile $NUMTEST
        unlinkmany $DIR/$tdir/$tfile $NUMTEST
 
-       OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
-           cut -c11-20)
+       OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
+               tail -1 | cut -c11-20)
 
        # don't return an error
-        [ $OSTIO_post == $OSTIO_pre ] && echo \
+       [ $OSTIO_post == $OSTIO_pre ] && echo \
            "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
            echo "This may be fine, depending on what ran before this test" &&
            echo "and how fast this system is." && return
 
-        echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
+       echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
 }
 run_test 115 "verify dynamic thread creation===================="
 
@@ -6559,13 +6890,13 @@ free_min_max () {
        MAXI=0; MAXV=${AVAIL[0]}
        MINI=0; MINV=${AVAIL[0]}
        for ((i = 0; i < ${#AVAIL[@]}; i++)); do
-           #echo OST $i: ${AVAIL[i]}kb
-           if [ ${AVAIL[i]} -gt $MAXV ]; then
-               MAXV=${AVAIL[i]}; MAXI=$i
-           fi
-           if [ ${AVAIL[i]} -lt $MINV ]; then
-               MINV=${AVAIL[i]}; MINI=$i
-           fi
+               #echo OST $i: ${AVAIL[i]}kb
+               if [[ ${AVAIL[i]} -gt $MAXV ]]; then
+                       MAXV=${AVAIL[i]}; MAXI=$i
+               fi
+               if [[ ${AVAIL[i]} -lt $MINV ]]; then
+                       MINV=${AVAIL[i]}; MINI=$i
+               fi
        done
        echo Min free space: OST $MINI: $MINV
        echo Max free space: OST $MAXI: $MAXV
@@ -6573,56 +6904,85 @@ free_min_max () {
 
 test_116a() { # was previously test_116()
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
+       [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
 
        echo -n "Free space priority "
-       lctl get_param -n lov.*-clilov-*.qos_prio_free
+       do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
+               head -n1
        declare -a AVAIL
        free_min_max
-       [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
-               return
 
-       # generate uneven OSTs
+       [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
+       [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
+               && return
+       trap simple_cleanup_common EXIT
+
+
+       # Check if we need to generate uneven OSTs
        test_mkdir -p $DIR/$tdir/OST${MINI}
-       declare -i FILL
-       FILL=$(($MINV / 4))
-       echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
-       $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
-       i=0
-       while [ $FILL -gt 0 ]; do
-           i=$(($i + 1))
-           dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
-           FILL=$(($FILL - 2048))
-           echo -n .
-       done
-       FILL=$(($MINV / 4))
-       sync
-       sleep_maxage
+       local FILL=$(($MINV / 4))
+       local DIFF=$(($MAXV - $MINV))
+       local DIFF2=$(($DIFF * 100 / $MINV))
+
+       local threshold=$(do_facet $SINGLEMDS \
+               lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
+       threshold=${threshold%%%}
+       echo -n "Check for uneven OSTs: "
+       echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
+
+       if [[ $DIFF2 -gt $threshold ]]; then
+               echo "ok"
+               echo "Don't need to fill OST$MINI"
+       else
+               # generate uneven OSTs. Write 2% over the QOS threshold value
+               echo "no"
+               DIFF=$(($threshold - $DIFF2 + 2))
+               DIFF2=$(( ($MINV * $DIFF)/100 ))
+               echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
+               $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
+                       error "setstripe failed"
+               DIFF=$(($DIFF2 / 2048))
+               i=0
+               while [ $i -lt $DIFF ]; do
+                       i=$(($i + 1))
+                       dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
+                               bs=2M count=1 2>/dev/null
+                       echo -n .
+               done
+               echo .
+               sync
+               sleep_maxage
+               free_min_max
+       fi
 
-       free_min_max
        DIFF=$(($MAXV - $MINV))
        DIFF2=$(($DIFF * 100 / $MINV))
-       echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
-       if [ $DIFF2 -gt 20 ]; then
-           echo "ok"
+       echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
+       if [[ $DIFF2 -gt $threshold ]]; then
+               echo "ok"
        else
-           echo "failed - QOS mode won't be used"
-           error_ignore "QOS imbalance criteria not met"
-           return
+               echo "failed - QOS mode won't be used"
+               skip "QOS imbalance criteria not met"
+               simple_cleanup_common
+               return
        fi
 
        MINI1=$MINI; MINV1=$MINV
        MAXI1=$MAXI; MAXV1=$MAXV
 
        # now fill using QOS
-       echo writing a bunch of files to QOS-assigned OSTs
        $SETSTRIPE -c 1 $DIR/$tdir
+       FILL=$(($FILL / 200))
+       if [ $FILL -gt 600 ]; then
+               FILL=600
+       fi
+       echo "writing $FILL files to QOS-assigned OSTs"
        i=0
-       while [ $FILL -gt 0 ]; do
-           i=$(($i + 1))
-           dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
-           FILL=$(($FILL - 200))
-           echo -n .
+       while [ $i -lt $FILL ]; do
+               i=$((i + 1))
+               dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
+                       count=1 2>/dev/null
+               echo -n .
        done
        echo "wrote $i 200k files"
        sync
@@ -6634,41 +6994,50 @@ test_116a() { # was previously test_116()
        echo "free space delta: orig $DIFF final $DIFF2"
        [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
        DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
-       echo "Wrote $DIFF to smaller OST $MINI1"
+       echo "Wrote ${DIFF}KB to smaller OST $MINI1"
        DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
-       echo "Wrote $DIFF2 to larger OST $MAXI1"
-       [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
+       echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
+       FILL=$(($DIFF2 * 100 / $DIFF - 100))
+       [ $DIFF -gt 0 ] &&
+               echo "Wrote ${FILL}% more data to larger OST $MAXI1"
 
        # Figure out which files were written where
        UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
-               awk '/'$MINI1': / {print $2; exit}')
+                 awk '/'$MINI1': / {print $2; exit}')
        echo $UUID
-        MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
+       MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
        echo "$MINC files created on smaller OST $MINI1"
        UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
-               awk '/'$MAXI1': / {print $2; exit}')
+                 awk '/'$MAXI1': / {print $2; exit}')
        echo $UUID
-        MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
+       MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
        echo "$MAXC files created on larger OST $MAXI1"
-       [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
-       [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
-
-       rm -rf $DIR/$tdir
+       FILL=$(($MAXC * 100 / $MINC - 100))
+       [[ $MINC -gt 0 ]] &&
+               echo "Wrote ${FILL}% more files to larger OST $MAXI1"
+       [[ $MAXC -gt $MINC ]] ||
+               error_ignore LU-9 "stripe QOS didn't balance free space"
+       simple_cleanup_common
 }
 run_test 116a "stripe QOS: free space balance ==================="
 
 test_116b() { # LU-2093
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
+               head -1 2>/dev/null)" ] && skip "no QOS" && return
 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
        local old_rr
-       old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
-       do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
+       old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
+               lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
+       do_facet $SINGLEMDS lctl set_param \
+               lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
        mkdir -p $DIR/$tdir
        do_facet $SINGLEMDS lctl set_param fail_loc=0x147
        createmany -o $DIR/$tdir/f- 20 || error "can't create"
        do_facet $SINGLEMDS lctl set_param fail_loc=0
        rm -rf $DIR/$tdir
-       do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
+       do_facet $SINGLEMDS lctl set_param \
+               lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
 }
 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
 
@@ -6683,13 +7052,13 @@ test_117() # bug 10891
         echo "Truncate succeeded."
        rm -f $DIR/$tfile
 }
-run_test 117 "verify fsfilt_extend =========="
+run_test 117 "verify osd extend =========="
 
 NO_SLOW_RESENDCOUNT=4
 export OLD_RESENDCOUNT=""
 set_resend_count () {
        local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
-       OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
+       OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
        lctl set_param -n $PROC_RESENDCOUNT $1
        echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
 }
@@ -7151,7 +7520,7 @@ run_test 119d "The DIO path should try to send a new rpc once one is completed"
 
 test_120a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        test_mkdir -p $DIR/$tdir
+       test_mkdir -p $DIR/$tdir
         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
                skip "no early lock cancel on server" && return 0
 
@@ -7161,89 +7530,113 @@ test_120a() {
        # asynchronous object destroy at MDT could cause bl ast to client
        cancel_lru_locks osc
 
-        stat $DIR/$tdir > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        test_mkdir $DIR/$tdir/d1
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       stat $DIR/$tdir > /dev/null
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       test_mkdir -c1 $DIR/$tdir/d1
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120a "Early Lock Cancel: mkdir test"
 
 test_120b() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        test_mkdir -p $DIR/$tdir
-        [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
+        test_mkdir $DIR/$tdir
+        [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
                skip "no early lock cancel on server" && return 0
         lru_resize_disable mdc
         lru_resize_disable osc
         cancel_lru_locks mdc
         stat $DIR/$tdir > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        touch $DIR/$tdir/f1
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       touch $DIR/$tdir/f1
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120b "Early Lock Cancel: create test"
 
 test_120c() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        test_mkdir -p $DIR/$tdir
-        [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
-               skip "no early lock cancel on server" && return 0
+       test_mkdir -c1 $DIR/$tdir
+       [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
+              skip "no early lock cancel on server" && return 0
         lru_resize_disable mdc
         lru_resize_disable osc
-       test_mkdir -p $DIR/$tdir/d1
-       test_mkdir -p $DIR/$tdir/d2
-        touch $DIR/$tdir/d1/f1
-        cancel_lru_locks mdc
-        stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       test_mkdir -p -c1 $DIR/$tdir/d1
+       test_mkdir -p -c1 $DIR/$tdir/d2
+       touch $DIR/$tdir/d1/f1
+       cancel_lru_locks mdc
+       stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120c "Early Lock Cancel: link test"
 
 test_120d() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        test_mkdir -p $DIR/$tdir
-        [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
-               skip "no early lock cancel on server" && return 0
-        lru_resize_disable mdc
-        lru_resize_disable osc
-        touch $DIR/$tdir
-        cancel_lru_locks mdc
-        stat $DIR/$tdir > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        chmod a+x $DIR/$tdir
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       test_mkdir -p -c1 $DIR/$tdir
+       [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
+              skip "no early lock cancel on server" && return 0
+       lru_resize_disable mdc
+       lru_resize_disable osc
+       touch $DIR/$tdir
+       cancel_lru_locks mdc
+       stat $DIR/$tdir > /dev/null
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       chmod a+x $DIR/$tdir
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120d "Early Lock Cancel: setattr test"
 
 test_120e() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-        test_mkdir -p $DIR/$tdir
+        test_mkdir -p -c1 $DIR/$tdir
         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
                skip "no early lock cancel on server" && return 0
         lru_resize_disable mdc
@@ -7253,19 +7646,21 @@ test_120e() {
         cancel_lru_locks osc
         dd if=$DIR/$tdir/f1 of=/dev/null
         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        unlink $DIR/$tdir/f1
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       unlink $DIR/$tdir/f1
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120e "Early Lock Cancel: unlink test"
 
@@ -7273,11 +7668,11 @@ test_120f() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
                skip "no early lock cancel on server" && return 0
-        test_mkdir -p $DIR/$tdir
+        test_mkdir -p -c1 $DIR/$tdir
         lru_resize_disable mdc
         lru_resize_disable osc
-       test_mkdir -p $DIR/$tdir/d1
-       test_mkdir -p $DIR/$tdir/d2
+       test_mkdir -p -c1 $DIR/$tdir/d1
+       test_mkdir -p -c1 $DIR/$tdir/d2
         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
         cancel_lru_locks mdc
@@ -7285,19 +7680,21 @@ test_120f() {
         dd if=$DIR/$tdir/d1/f1 of=/dev/null
         dd if=$DIR/$tdir/d2/f2 of=/dev/null
         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
-        [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
+       [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120f "Early Lock Cancel: rename test"
 
@@ -7314,32 +7711,35 @@ test_120g() {
         cancel_lru_locks osc
         t0=`date +%s`
 
-        can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        createmany -o $DIR/$tdir/f $count
-        sync
-        can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        t1=`date +%s`
-        echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
-        echo rm $count files
-        rm -r $DIR/$tdir
-        sync
-        can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
-              awk '/ldlm_cancel/ {print $2}'`
-        blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
-              awk '/ldlm_bl_callback/ {print $2}'`
-        t2=`date +%s`
-        echo total: $count removes in $((t2-t1))
-        echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
-        sleep 2
-        # wait for commitment of removal
-        lru_resize_enable mdc
-        lru_resize_enable osc
+       can0=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       createmany -o $DIR/$tdir/f $count
+       sync
+       can1=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       t1=$(date +%s)
+       echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
+       echo rm $count files
+       rm -r $DIR/$tdir
+       sync
+       can2=$(do_facet $SINGLEMDS \
+              "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
+              awk '/ldlm_cancel/ {print $2}')
+       blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
+              awk '/ldlm_bl_callback/ {print $2}')
+       t2=$(date +%s)
+       echo total: $count removes in $((t2-t1))
+       echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
+       sleep 2
+       # wait for commitment of removal
+       lru_resize_enable mdc
+       lru_resize_enable osc
 }
 run_test 120g "Early Lock Cancel: performance test"
 
@@ -7352,7 +7752,8 @@ test_121() { #bug #10589
        cancel_lru_locks osc > /dev/null
        reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
        lctl set_param fail_loc=0
-       [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
+       [[ $reads -eq $writes ]] ||
+               error "read $reads blocks, must be $writes blocks"
 }
 run_test 121 "read cancel race ========="
 
@@ -7366,8 +7767,8 @@ test_123a() { # was test 123, statahead(bug 11401)
 
         rm -rf $DIR/$tdir
         test_mkdir -p $DIR/$tdir
-        NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
-        [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
+       NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
+       [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
         MULT=10
         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
@@ -7396,8 +7797,9 @@ test_123a() { # was test 123, statahead(bug 11401)
                 lctl get_param -n llite.*.statahead_stats
                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
 
-                [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
-                [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
+               [[ $swrong -lt $ewrong ]] &&
+                       log "statahead was stopped, maybe too many locks held!"
+               [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
 
                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
@@ -7460,8 +7862,8 @@ run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
 
 test_124a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
-               skip "no lru resize on server" && return 0
+       [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
+               skip "no lru resize on server" && return 0
         local NR=2000
         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
 
@@ -7474,18 +7876,18 @@ test_124a() {
 
         local NSDIR=""
         local LRU_SIZE=0
-        for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
-                local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
-                LRU_SIZE=$(lctl get_param -n $PARAM)
-                if [ $LRU_SIZE -gt $(default_lru_size) ]; then
-                        NSDIR=$(echo $PARAM | cut -d "." -f1-3)
-                                               log "NSDIR=$NSDIR"
+       for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
+               local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
+               LRU_SIZE=$($LCTL get_param -n $PARAM)
+               if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
+                       NSDIR=$(echo $PARAM | cut -d "." -f1-3)
+                       log "NSDIR=$NSDIR"
                         log "NS=$(basename $NSDIR)"
                         break
                 fi
         done
 
-        if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
+       if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
                 skip "Not enough cached locks created!"
                 return 0
         fi
@@ -7496,8 +7898,8 @@ test_124a() {
         # We know that lru resize allows one client to hold $LIMIT locks
         # for 10h. After that locks begin to be killed by client.
         local MAX_HRS=10
-        local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
-               log "LIMIT=$LIMIT"
+       local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
+       log "LIMIT=$LIMIT"
 
         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
         # killing locks. Some time was spent for creating locks. This means
@@ -7511,9 +7913,9 @@ test_124a() {
         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
         local LRU_SIZE_B=$LRU_SIZE
         log "LVF=$LVF"
-        local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
-               log "OLD_LVF=$OLD_LVF"
-        lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
+       local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
+       log "OLD_LVF=$OLD_LVF"
+       $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
 
         # Let's make sure that we really have some margin. Client checks
         # cached locks every 10 sec.
@@ -7524,14 +7926,14 @@ test_124a() {
                 echo -n "..."
                 sleep 5
                 SEC=$((SEC+5))
-                LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
+               LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
                 echo -n "$LRU_SIZE"
         done
         echo ""
-        lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
-        local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
+       $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
+       local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
 
-        [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
+       [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
                 unlinkmany $DIR/$tdir/f $NR
                 return
@@ -7545,25 +7947,26 @@ run_test 124a "lru resize ======================================="
 
 get_max_pool_limit()
 {
-        local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
-        local max=0
-        for l in $limit; do
-                if test $l -gt $max; then
-                        max=$l
-                fi
-        done
-        echo $max
+       local limit=$($LCTL get_param \
+                     -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
+       local max=0
+       for l in $limit; do
+               if [[ $l -gt $max ]]; then
+                       max=$l
+               fi
+       done
+       echo $max
 }
 
 test_124b() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
-               skip "no lru resize on server" && return 0
+       [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
+               skip "no lru resize on server" && return 0
 
-        LIMIT=`get_max_pool_limit`
+       LIMIT=$(get_max_pool_limit)
 
-        NR=$(($(default_lru_size)*20))
-        if [ $NR -gt $LIMIT ]; then
+       NR=$(($(default_lru_size)*20))
+       if [[ $NR -gt $LIMIT ]]; then
                 log "Limit lock number by $LIMIT locks"
                 NR=$LIMIT
         fi
@@ -7628,6 +8031,7 @@ run_test 124b "lru resize (performance test) ======================="
 test_125() { # 13358
        [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
        [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
+       [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
        test_mkdir -p $DIR/d125 || error "mkdir failed"
        $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
        setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
@@ -7764,34 +8168,44 @@ test_129() {
                return
        fi
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
-
        ENOSPC=28
        EFBIG=27
 
+       rm -rf $DIR/$tdir
        test_mkdir -p $DIR/$tdir
 
-       MAX=$(stat -c%s "$DIR/$tdir")
+       # block size of mds1
+       local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
+       local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
+       local MAX=$((MDSBLOCKSIZE * 3))
        set_dir_limits $MAX
-       local I=0
+       local I=$(stat -c%s "$DIR/$tdir")
        local J=0
-       while [ ! $I -gt $MAX ]; do
+       local STRIPE_COUNT=1
+       [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
+       MAX=$((MAX*STRIPE_COUNT))
+       while [[ $I -le $MAX ]]; do
                $MULTIOP $DIR/$tdir/$J Oc
                rc=$?
                #check two errors ENOSPC for new version of ext4 max_dir_size patch
                #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
                #and EFBIG for previous versions
-               if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ] && [ $I -gt 0 ]; then
+               if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
                        set_dir_limits 0
                        echo "return code $rc received as expected"
-                       multiop $DIR/$tdir/$J Oc
-                       rc=$?
+                       multiop $DIR/$tdir/$J Oc ||
+                               error_exit "multiop failed w/o dir size limit"
+
                        I=$(stat -c%s "$DIR/$tdir")
-                       if [ $I -gt $MAX ] && [ $rc -eq 0 ]; then
-                               return 0
+
+                       if [ $(lustre_version_code $SINGLEMDS) -lt \
+                                       $(version_code 2.4.51) ]
+                       then
+                               [[ $I -eq $MAX ]] && return 0
                        else
-                               error_exit "return code $rc current dir size $I " \
-                                          "previous limit $MAX"
+                               [[ $I -gt $MAX ]] && return 0
                        fi
+                       error_exit "current dir size $I, previous limit $MAX"
                elif [ $rc -ne 0 ]; then
                        set_dir_limits 0
                        error_exit "return code $rc received instead of expected " \
@@ -7802,7 +8216,7 @@ test_129() {
        done
 
        set_dir_limits 0
-       error "exceeded dir size limit $MAX x $MDSCOUNT $MAX : $I bytes"
+       error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
 }
 run_test 129 "test directory size limit ========================"
 
@@ -7999,10 +8413,14 @@ test_130d() {
        $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
        [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
                skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
-       dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
+
+       local actual_stripecnt=$($GETSTRIPE -c $fm_file)
+       dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
+               error "dd failed on $fm_file"
 
        filefrag -ves $fm_file || error "filefrag $fm_file failed"
-       filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
+       filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
+               grep -v "ext:" | grep -v "found"`
 
        last_lun=`echo $filefrag_op | cut -d: -f5`
 
@@ -8026,7 +8444,7 @@ test_130d() {
                (( tot_len += ext_len ))
                last_lun=$frag_lun
        done
-       if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
+       if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
                cleanup_130
                error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
                return
@@ -8226,11 +8644,12 @@ check_stats() {
        *) error "Wrong argument $1" ;;
        esac
        echo $res
-       count=`echo $res | awk '{print $2}'`
        [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
        # if the argument $3 is zero, it means any stat increment is ok.
-       if [ $3 -gt 0 ] ; then
-               [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
+       if [[ $3 -gt 0 ]]; then
+               count=$(echo $res | awk '{ print $2 }')
+               [[ $count -ne $3 ]] &&
+                       error "The $2 counter on $1 is wrong - expected $3"
        fi
 }
 
@@ -8345,19 +8764,19 @@ test_133c() {
 run_test 133c "Verifying OST stats ========================================"
 
 order_2() {
-    local value=$1
-    local orig=$value
-    local order=1
+       local value=$1
+       local orig=$value
+       local order=1
 
-    while [ $value -ge 2 ]; do
-        order=$((order*2))
-        value=$((value/2))
-    done
+       while [ $value -ge 2 ]; do
+               order=$((order*2))
+               value=$((value/2))
+       done
 
-    if [ $orig -gt $order ]; then
-        order=$((order*2))
-    fi
-    echo $order
+       if [ $orig -gt $order ]; then
+               order=$((order*2))
+       fi
+       echo $order
 }
 
 size_in_KMGT() {
@@ -8432,7 +8851,8 @@ test_133d() {
        local samedir=$($cmd | grep 'same_dir')
        local same_sample=$(get_rename_size $testdir1_size)
        [ -z "$samedir" ] && error "samedir_rename_size count error"
-       [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
+       [[ $same_sample -eq 1 ]] ||
+               error "samedir_rename_size error $same_sample"
        echo "Check same dir rename stats success"
 
        do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
@@ -8459,8 +8879,10 @@ test_133d() {
        local src_sample=$(get_rename_size $testdir1_size crossdir_src)
        local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
        [ -z "$crossdir" ] && error "crossdir_rename_size count error"
-       [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
-       [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
+       [[ $src_sample -eq 1 ]] ||
+               error "crossdir_rename_size error $src_sample"
+       [[ $tgt_sample -eq 1 ]] ||
+               error "crossdir_rename_size error $tgt_sample"
        echo "Check cross dir rename stats success"
        rm -rf $DIR/${tdir}
 }
@@ -8497,11 +8919,10 @@ test_133e() {
                cancel_lru_locks osc
 
                sum=$(do_facet ost1 $LCTL get_param \
-                               "obdfilter.*.exports.*.stats" | \
-                         awk -v ctr=$ctr '\
-                               BEGIN { sum = 0 }
+                       "obdfilter.*.exports.*.stats" |
+                       awk -v ctr=$ctr 'BEGIN { sum = 0 }
                                $1 == ctr { sum += $7 }
-                               END { print sum }')
+                               END { printf("%0.0f", sum) }')
 
                if ((sum != bs * count)); then
                        error "Bad $ctr sum, expected $((bs * count)), got $sum"
@@ -8517,31 +8938,58 @@ test_133f() {
        local facet
 
        # First without trusting modes.
-       find $proc_dirs \
-               -exec cat '{}' \; &> /dev/null
+       find $proc_dirs -exec cat '{}' \; &> /dev/null
 
        # Second verifying readability.
        find $proc_dirs \
                -type f \
-               -readable \
-               -exec cat '{}' \; > /dev/null ||
+               -exec cat '{}' \; &> /dev/null ||
                        error "proc file read failed"
 
        for facet in $SINGLEMDS ost1; do
                do_facet $facet find $proc_dirs \
-                       -not -name req_history \
+                       ! -name req_history \
                        -exec cat '{}' \\\; &> /dev/null
 
-           do_facet $facet     find $proc_dirs \
-                       -not -name req_history \
+               do_facet $facet find $proc_dirs \
+                       ! -name req_history \
                        -type f \
-                       -readable \
-                       -exec cat '{}' \\\; > /dev/null ||
+                       -exec cat '{}' \\\; &> /dev/null ||
                                error "proc file read failed"
        done
 }
 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
 
+test_133g() {
+       local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
+       local facet
+
+       # Second verifying readability.
+       find $proc_dirs \
+               -type f \
+               -not -name force_lbug \
+               -not -name changelog_mask \
+               -exec badarea_io '{}' \; > /dev/null
+
+       [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
+               skip "Too old lustre on MDS"
+
+       [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
+               skip "Too old lustre on ost1"
+
+       for facet in $SINGLEMDS ost1; do
+               do_facet $facet find $proc_dirs \
+                       -type f \
+                       -not -name force_lbug \
+                       -not -name changelog_mask \
+                       -exec badarea_io '{}' \\\; &> /dev/null
+
+       done
+
+       true
+}
+run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
+
 test_140() { #bug-17379
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
@@ -8613,12 +9061,48 @@ test_150() {
 }
 run_test 150 "truncate/append tests"
 
+#LU-2902 roc_hit was not able to read all values from lproc
+function roc_hit_init() {
+       local list=$(comma_list $(osts_nodes))
+       local dir=$DIR/$tdir-check
+       local file=$dir/file
+       local BEFORE
+       local AFTER
+       local idx
+
+       test_mkdir -p $dir
+       #use setstripe to do a write to every ost
+       for i in $(seq 0 $((OSTCOUNT-1))); do
+               $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
+               dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
+               idx=$(printf %04x $i)
+               BEFORE=$(get_osd_param $list *OST*$idx stats |
+                       awk '$1 == "cache_access" {sum += $2}
+                               END { printf("%0.0f", sum) }')
+
+               cancel_lru_locks osc
+               cat $file >/dev/null
+
+               AFTER=$(get_osd_param $list *OST*$idx stats |
+                       awk '$1 == "cache_access" {sum += $2}
+                               END { printf("%0.0f", sum) }')
+
+               echo BEFORE:$BEFORE AFTER:$AFTER
+               if ! let "AFTER - BEFORE == 4"; then
+                       rm -rf $dir
+                       error "roc_hit is not safe to use"
+               fi
+               rm $file
+       done
+
+       rm -rf $dir
+}
+
 function roc_hit() {
        local list=$(comma_list $(osts_nodes))
-       #debug temp debug for LU-2902: lets see what values we get back
-       echo $(get_osd_param $list '' stats) 1>&2
        echo $(get_osd_param $list '' stats |
-              awk '/'cache_hit'/ {sum+=$2} END {print sum}')
+               awk '$1 == "cache_hit" {sum += $2}
+                       END { printf("%0.0f", sum) }')
 }
 
 function set_cache() {
@@ -8660,6 +9144,8 @@ test_151() {
                return 0
        fi
 
+       roc_hit_init
+
        #define OBD_FAIL_OBD_NO_LRU  0x609
        do_nodes $list $LCTL set_param fail_loc=0x609
 
@@ -8750,8 +9236,10 @@ dot_lustre_fid_permission_check() {
        rm -f $test_dir/$tfile.1
        echo "truncate fid $fid"
        $TRUNCATE $ffid 777 || error "truncate $ffid failed."
-       echo "link fid $fid"
-       ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
+       if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
+               echo "link fid $fid"
+               ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
+       fi
        if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
                echo "setfacl fid $fid"
                setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
@@ -8846,10 +9334,29 @@ dot_lustre_fid_permission_check() {
        rm -rf $test_dir/$tfile-2
 }
 
+test_154A() {
+       [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
+               skip "Need MDS version at least 2.4.1" && return
+
+       touch $DIR/$tfile
+       local FID=$($LFS path2fid $DIR/$tfile)
+       [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
+
+       # check that we get the same pathname back
+       local FOUND=$($LFS fid2path $MOUNT $FID)
+       [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
+       [ "$FOUND" != "$DIR/$tfile" ] &&
+               error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
+
+       rm -rf $DIR/$tfile
+}
+run_test 154A "lfs path2fid and fid2path basic checks"
+
 test_154a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
                { skip "Need MDS version at least 2.2.51"; return 0; }
+       [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
 
        cp /etc/hosts $DIR/$tfile
 
@@ -8884,7 +9391,7 @@ test_154b() {
        local rc=0
 
        mkdir -p $DIR/$tdir
-       $LFS mkdir -i $MDTIDX $remote_dir ||
+       $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
                error "create remote directory failed"
 
        cp /etc/hosts $remote_dir/$tfile
@@ -8899,6 +9406,66 @@ test_154b() {
 }
 run_test 154b "Open-by-FID for remote directory"
 
+test_154c() {
+       [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
+               skip "Need MDS version at least 2.4.1" && return
+
+       touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
+       local FID1=$($LFS path2fid $DIR/$tfile.1)
+       local FID2=$($LFS path2fid $DIR/$tfile.2)
+       local FID3=$($LFS path2fid $DIR/$tfile.3)
+
+       local N=1
+       $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
+               [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
+                       error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
+               local want=FID$N
+               [ "$FID" = "${!want}" ] ||
+                       error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
+               N=$((N + 1))
+       done
+
+       $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
+               [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
+                       error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
+               N=$((N + 1))
+       done
+}
+run_test 154c "lfs path2fid and fid2path multiple arguments"
+
+test_154d() {
+       [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
+               skip "Need MDS version at least 2.5.53" && return
+
+       if remote_mds; then
+               nid=$($LCTL list_nids | sed  "s/\./\\\./g")
+       else
+               nid="0@lo"
+       fi
+       local proc_ofile="mdt.*.exports.'$nid'.open_files"
+       local fd
+       local cmd
+
+       rm -f $DIR/$tfile
+       touch $DIR/$tfile
+
+       fid=$($LFS path2fid $DIR/$tfile)
+       # Open the file
+       fd=$(free_fd)
+       cmd="exec $fd<$DIR/$tfile"
+       eval $cmd
+       fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
+       echo $fid_list | grep $fid
+       rc=$?
+
+       cmd="exec $fd>/dev/null"
+       eval $cmd
+       if [ $rc -ne 0 ]; then
+               error "FID $fid not found in open files list $fid_list"
+       fi
+}
+run_test 154d "Verify open file fid"
+
 test_155_small_load() {
     local temp=$TMP/$tfile
     local file=$DIR/$tfile
@@ -9030,6 +9597,8 @@ test_156() {
                skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
                return
 
+       roc_hit_init
+
     log "Turn on read and write cache"
     set_cache read on
     set_cache writethrough on
@@ -9122,12 +9691,11 @@ test_156() {
     BEFORE=`roc_hit`
     cat $file >/dev/null
     AFTER=`roc_hit`
-    if ! let "AFTER - BEFORE == 0"; then
-        error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
-    else
-        log "cache hits:: before: $BEFORE, after: $AFTER"
-    fi
-
+       if ! let "AFTER - BEFORE == 0"; then
+               error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
+       else
+               log "cache hits:: before: $BEFORE, after: $AFTER"
+       fi
 
     log "Turn on the read cache and turn off the write cache"
     set_cache read on
@@ -9141,11 +9709,11 @@ test_156() {
     cancel_lru_locks osc
     cat $file >/dev/null
     AFTER=`roc_hit`
-    if ! let "AFTER - BEFORE == 0"; then
-        error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
-    else
-        log "cache hits:: before: $BEFORE, after: $AFTER"
-    fi
+       if ! let "AFTER - BEFORE == 0"; then
+               error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
+       else
+               log "cache hits:: before: $BEFORE, after: $AFTER"
+       fi
 
     log "Read again; it should be satisfied from the cache."
     BEFORE=`roc_hit`
@@ -9164,8 +9732,8 @@ run_test 156 "Verification of tunables ============================"
 
 #Changelogs
 err17935 () {
-       if [ $MDSCOUNT -gt 1 ]; then
-               error_ignore 17935 $*
+       if [[ $MDSCOUNT -gt 1 ]]; then
+               error_ignore bz17935 $*
        else
                error $*
        fi
@@ -9184,7 +9752,18 @@ changelog_chmask()
        fi
 }
 
-test_160() {
+changelog_extract_field() {
+       local mdt=$1
+       local cltype=$2
+       local file=$3
+       local identifier=$4
+
+       $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
+               print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
+               tail -1
+}
+
+test_160a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        remote_mds_nodsh && skip "remote MDS with nodsh" && return
        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
@@ -9229,14 +9808,14 @@ test_160() {
 
        # verify contents
        echo "verifying target fid"
-       fidc=$($LFS changelog $MDT0|awk '/CREAT.*timestamp$/{print $6}'|tail -1)
+       fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
        fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
-       [ "$fidc" == "t=$fidf" ] ||
+       [ "$fidc" == "$fidf" ] ||
                err17935 "fid in changelog $fidc != file fid $fidf"
        echo "verifying parent fid"
-       fidc=$($LFS changelog $MDT0|awk '/CREAT.*timestamp$/{print $7}'|tail -1)
+       fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
        fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
-       [ "$fidc" == "p=$fidf" ] ||
+       [ "$fidc" == "$fidf" ] ||
                err17935 "pfid in changelog $fidc != dir fid $fidf"
 
        USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
@@ -9248,18 +9827,18 @@ test_160() {
 
        MIN_REC=$($GET_CL_USERS |
                awk 'min == "" || $2 < min {min = $2}; END {print min}')
-       FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
+       FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
        echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
        [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
                err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
 
        # LU-3446 changelog index reset on MDT restart
        local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
-       CUR_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
+       CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
        $LFS changelog_clear $MDT0 $USER 0
        stop $SINGLEMDS || error "Fail to stop MDT."
        start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
-       CUR_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
+       CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
        echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
        [ $CUR_REC1 == $CUR_REC2 ] ||
                err17935 "current index should be $CUR_REC1 is $CUR_REC2"
@@ -9271,27 +9850,84 @@ test_160() {
 
        USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
        if [ $USERS -eq 0 ]; then
-               LAST_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
+               LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
                touch $DIR/$tdir/chloe
-               LAST_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
+               LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
                echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
                [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
        else
                echo "$USERS other changelog users; can't verify off"
        fi
 }
-run_test 160 "changelog sanity"
+run_test 160a "changelog sanity"
+
+test_160b() { # LU-3587
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       remote_mds_nodsh && skip "remote MDS with nodsh" && return
+       [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
+               { skip "Need MDS version at least 2.2.0"; return; }
+
+       local CL_USERS="mdd.$MDT0.changelog_users"
+       local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
+       USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
+       echo "Registered as changelog user $USER"
+       $GET_CL_USERS | grep -q $USER ||
+               error "User $USER not found in changelog_users"
+
+       local LONGNAME1=$(str_repeat a 255)
+       local LONGNAME2=$(str_repeat b 255)
+
+       cd $DIR
+       echo "creating very long named file"
+       touch $LONGNAME1 || error "create of $LONGNAME1 failed"
+       echo "moving very long named file"
+       mv $LONGNAME1 $LONGNAME2
+
+       $LFS changelog $MDT0 | grep RENME
+
+       echo "deregistering $USER"
+       do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
+
+       rm -f $LONGNAME2
+}
+run_test 160b "Verify that very long rename doesn't crash in changelog"
+
+test_160c() {
+       local rc=0
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+
+       # Registration step
+       local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
+               changelog_register -n)
+
+       rm -rf $DIR/$tdir
+       mkdir -p $DIR/$tdir
+       $MCREATE $DIR/$tdir/foo_160c
+       changelog_chmask "TRUNC"
+       $TRUNCATE $DIR/$tdir/foo_160c 200
+       changelog_chmask "TRUNC"
+       $TRUNCATE $DIR/$tdir/foo_160c 199
+       $LFS changelog $MDT0
+       TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
+       [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
+       $LFS changelog_clear $MDT0 $USER 0
+
+       # Deregistration step
+       echo "deregistering $USER"
+       do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
+}
+run_test 160c "verify that changelog log catch the truncate event"
 
 test_161a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-    test_mkdir -p $DIR/$tdir
-    cp /etc/hosts $DIR/$tdir/$tfile
-    test_mkdir $DIR/$tdir/foo1
-    test_mkdir $DIR/$tdir/foo2
-    ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
-    ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
-    ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
-    ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
+       test_mkdir -p -c1 $DIR/$tdir
+       cp /etc/hosts $DIR/$tdir/$tfile
+       test_mkdir -c1 $DIR/$tdir/foo1
+       test_mkdir -c1 $DIR/$tdir/foo2
+       ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
+       ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
+       ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
+       ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
        local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
        if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
                $LFS fid2path $DIR $FID
@@ -9312,15 +9948,16 @@ test_161a() {
     fi
     rm $DIR/$tdir/foo2/maggie
 
-    # overflow the EA
-    local longname=filename_avg_len_is_thirty_two_
-    createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
-       error "failed to hardlink many files"
-    links=$($LFS fid2path $DIR $FID | wc -l)
-    echo -n "${links}/1000 links in link EA"
-    [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
-    unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
-       error "failed to unlink many hardlinks"
+       # overflow the EA
+       local longname=filename_avg_len_is_thirty_two_
+       createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
+               error "failed to hardlink many files"
+       links=$($LFS fid2path $DIR $FID | wc -l)
+       echo -n "${links}/1000 links in link EA"
+       [[ $links -gt 60 ]] ||
+               err17935 "expected at least 60 links in link EA"
+       unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
+               error "failed to unlink many hardlinks"
 }
 run_test 161a "link ea sanity"
 
@@ -9370,12 +10007,100 @@ test_161b() {
                error "failed to hardlink many files"
        links=$($LFS fid2path $DIR $FID | wc -l)
        echo -n "${links}/1000 links in link EA"
-       [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
+       [[ ${links} -gt 60 ]] ||
+               err17935 "expected at least 60 links in link EA"
        unlinkmany $remote_dir/foo2/$longname 1000 ||
        error "failed to unlink many hardlinks"
 }
 run_test 161b "link ea sanity under remote directory"
 
+test_161c() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
+               skip "Need MDS version at least 2.1.5" && return
+
+       # define CLF_RENAME_LAST 0x0001
+       # rename overwrite a target having nlink = 1 (changelog flag 0x1)
+       local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
+               changelog_register -n)
+       rm -rf $DIR/$tdir
+       mkdir -p $DIR/$tdir
+       touch $DIR/$tdir/foo_161c
+       touch $DIR/$tdir/bar_161c
+       mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
+       $LFS changelog $MDT0 | grep RENME
+       local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
+               cut -f5 -d' ')
+       $LFS changelog_clear $MDT0 $USER 0
+       if [ x$flags != "x0x1" ]; then
+               do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
+                       $USER
+               error "flag $flags is not 0x1"
+       fi
+       echo "rename overwrite a target having nlink = 1," \
+               "changelog record has flags of $flags"
+
+       # rename overwrite a target having nlink > 1 (changelog flag 0x0)
+       touch $DIR/$tdir/foo_161c
+       touch $DIR/$tdir/bar_161c
+       ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
+       mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
+       $LFS changelog $MDT0 | grep RENME
+       flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
+       $LFS changelog_clear $MDT0 $USER 0
+       if [ x$flags != "x0x0" ]; then
+               do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
+                       $USER
+               error "flag $flags is not 0x0"
+       fi
+       echo "rename overwrite a target having nlink > 1," \
+               "changelog record has flags of $flags"
+
+       # rename doesn't overwrite a target (changelog flag 0x0)
+       touch $DIR/$tdir/foo_161c
+       mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
+       $LFS changelog $MDT0 | grep RENME
+       flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
+       $LFS changelog_clear $MDT0 $USER 0
+       if [ x$flags != "x0x0" ]; then
+               do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
+                       $USER
+               error "flag $flags is not 0x0"
+       fi
+       echo "rename doesn't overwrite a target," \
+               "changelog record has flags of $flags"
+
+       # define CLF_UNLINK_LAST 0x0001
+       # unlink a file having nlink = 1 (changelog flag 0x1)
+       rm -f $DIR/$tdir/foo2_161c
+       $LFS changelog $MDT0 | grep UNLNK
+       flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
+       $LFS changelog_clear $MDT0 $USER 0
+       if [ x$flags != "x0x1" ]; then
+               do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
+                       $USER
+               error "flag $flags is not 0x1"
+       fi
+       echo "unlink a file having nlink = 1," \
+               "changelog record has flags of $flags"
+
+       # unlink a file having nlink > 1 (changelog flag 0x0)
+       ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
+       rm -f $DIR/$tdir/foobar_161c
+       $LFS changelog $MDT0 | grep UNLNK
+       flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
+       $LFS changelog_clear $MDT0 $USER 0
+       if [ x$flags != "x0x0" ]; then
+               do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
+                       $USER
+               error "flag $flags is not 0x0"
+       fi
+       echo "unlink a file having nlink > 1," \
+               "changelog record has flags of $flags"
+       do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
+}
+run_test 161c "check CL_RENME[UNLINK] changelog record flags"
+
 check_path() {
     local expected=$1
     shift
@@ -9438,20 +10163,6 @@ test_162() {
 }
 run_test 162 "path lookup sanity"
 
-test_163() {
-       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       remote_mds_nodsh && skip "remote MDS with nodsh" && return
-       copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
-       copytool $FSNAME &
-       sleep 1
-       local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
-       # this proc file is temporary and linux-only
-       do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
-         error "kernel->userspace send failed"
-       kill -INT $!
-}
-run_test 163 "kernel <-> userspace comms"
-
 test_169() {
        # do directio so as not to populate the page cache
        log "creating a 10 Mb file"
@@ -9571,6 +10282,15 @@ obdecho_test() {
        local pages=${3:-64}
         local rc=0
         local id
+
+       local count=10
+       local obd_size=$(get_obd_size $node $OBD)
+       local page_size=$(get_page_size $node)
+       if [[ -n "$obd_size" ]]; then
+               local new_count=$((obd_size / (pages * page_size / 1024)))
+               [[ $new_count -ge $count ]] || count=$new_count
+       fi
+
         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
                            rc=2; }
@@ -9582,7 +10302,7 @@ obdecho_test() {
        [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
                           rc=4; }
        [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
-                          "test_brw 10 w v $pages $id" || rc=4; }
+                          "test_brw $count w v $pages $id" || rc=4; }
        [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
                           rc=4; }
        [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
@@ -9648,10 +10368,10 @@ test_180c() { # LU-2598
        do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
                rmmod_remote=true || error "failed to load module obdecho"
 
-       target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4}'|head -1)
-       if [[ -n $target ]]; then
-               obdecho_test "$target" ost1 "$pages" ||
-                       rc=${PIPESTATUS[0]}
+       target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
+               head -n1)
+       if [ -n "$target" ]; then
+               obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
        else
                echo "there is no obdfilter target on ost1"
                rc=2
@@ -9708,6 +10428,9 @@ test_182() {
 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
 
 test_183() { # LU-2275
+       [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
+               skip "Need MDS version at least 2.3.56" && return
+
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
        echo aaa > $DIR/$tdir/$tfile
@@ -9734,7 +10457,7 @@ test_184a() {
        check_swap_layouts_support && return 0
 
        dir0=$DIR/$tdir/$testnum
-       test_mkdir -p $dir0 || error "creating dir $dir0"
+       test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
        ref1=/etc/passwd
        ref2=/etc/group
        file1=$dir0/f1
@@ -9802,7 +10525,8 @@ test_184c() {
        # create a file large enough for the concurent test
        dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
        dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
-       echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
+       echo "ref file size: ref1($(stat -c %s $ref1))," \
+            "ref2($(stat -c %s $ref2))"
 
        cp $ref2 $file2
        dd if=$ref1 of=$file1 bs=16k &
@@ -9818,8 +10542,8 @@ test_184c() {
        [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
 
        # how many bytes copied before swapping layout
-       local copied=`stat -c %s $file2`
-       local remaining=`stat -c %s $ref1`
+       local copied=$(stat -c %s $file2)
+       local remaining=$(stat -c %s $ref1)
        remaining=$((remaining - copied))
        echo "Copied $copied bytes before swapping layout..."
 
@@ -9837,6 +10561,8 @@ run_test 184c "Concurrent write and layout swap"
 
 test_184d() {
        check_swap_layouts_support && return 0
+       [ -z "$(which getfattr 2>/dev/null)" ] &&
+               skip "no getfattr command" && return 0
 
        local file1=$DIR/$tdir/$tfile-1
        local file2=$DIR/$tdir/$tfile-2
@@ -9861,12 +10587,16 @@ test_184d() {
        [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
 
        lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
-       [ -z $lovea1 ] || error "$file1 shouldn't have lovea"
+       [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
 }
 run_test 184d "allow stripeless layouts swap"
 
 
 test_185() { # LU-2441
+       # LU-3553 - no volatile file support in old servers
+       [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
+               { skip "Need MDS version at least 2.3.60"; return 0; }
+
        mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
        touch $DIR/$tdir/spoo
        local mtime1=$(stat -c "%Y" $DIR/$tdir)
@@ -10104,7 +10834,7 @@ pool_remove_first_target() {
        local pool=$1
 
        local pname="lov.$FSNAME-*.pools.$pool"
-       local t=$($LCTL get_param -n $pname | head -1)
+       local t=$($LCTL get_param -n $pname | head -n1)
        do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
        wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
                error_noexit "$t not removed from $FSNAME.$pool"
@@ -10125,7 +10855,7 @@ pool_remove_all_targets() {
                error_noexit "Pool $FSNAME.$pool cannot be drained"
                return 1
        }
-       # striping on an empty/nonexistant pool should fall back 
+       # striping on an empty/nonexistant pool should fall back
        # to "pool of everything"
        touch $file || {
                error_noexit "failed to use fallback striping for empty pool"
@@ -10396,9 +11126,10 @@ test_205() { # Job stats
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
                skip "Server doesn't support jobstats" && return 0
+       [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
 
        local cmd
-       OLD_JOBENV=`$LCTL get_param -n jobid_var`
+       OLD_JOBENV=$($LCTL get_param -n jobid_var)
        if [ $OLD_JOBENV != $JOBENV ]; then
                jobstats_set $JOBENV
                trap jobstats_set EXIT
@@ -10445,7 +11176,7 @@ run_test 205 "Verify job stats"
 # LU-1480, LU-1773 and LU-1657
 test_206() {
        mkdir -p $DIR/$tdir
-       lfs setstripe -c -1 $DIR/$tdir
+       $SETSTRIPE -c -1 $DIR/$tdir
 #define OBD_FAIL_LOV_INIT 0x1403
        $LCTL set_param fail_loc=0xa0001403
        $LCTL set_param fail_val=1
@@ -10488,6 +11219,82 @@ test_207b() {
 }
 run_test 207b "can refresh layout at open"
 
+test_208() {
+       # FIXME: in this test suite, only RD lease is used. This is okay
+       # for now as only exclusive open is supported. After generic lease
+       # is done, this test suite should be revised. - Jinshan
+
+       [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
+               { skip "Need MDS version at least 2.4.52"; return 0; }
+
+       echo "==== test 1: verify get lease work"
+       $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
+
+       echo "==== test 2: verify lease can be broken by upcoming open"
+       $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
+       local PID=$!
+       sleep 1
+
+       $MULTIOP $DIR/$tfile oO_RDONLY:c
+       kill -USR1 $PID && wait $PID || error "break lease error"
+
+       echo "==== test 3: verify lease can't be granted if an open already exists"
+       $MULTIOP $DIR/$tfile oO_RDONLY:_c &
+       local PID=$!
+       sleep 1
+
+       $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
+       kill -USR1 $PID && wait $PID || error "open file error"
+
+       echo "==== test 4: lease can sustain over recovery"
+       $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
+       PID=$!
+       sleep 1
+
+       fail mds1
+
+       kill -USR1 $PID && wait $PID || error "lease broken over recovery"
+
+       echo "==== test 5: lease broken can't be regained by replay"
+       $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
+       PID=$!
+       sleep 1
+
+       # open file to break lease and then recovery
+       $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
+       fail mds1
+
+       kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
+
+       rm -f $DIR/$tfile
+}
+run_test 208 "Exclusive open"
+
+test_209() {
+       [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
+               skip_env "must have disp_stripe" && return
+
+       touch $DIR/$tfile
+       sync; sleep 5; sync;
+
+       echo 3 > /proc/sys/vm/drop_caches
+       req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
+
+       # open/close 500 times
+       for i in $(seq 500); do
+               cat $DIR/$tfile
+       done
+
+       echo 3 > /proc/sys/vm/drop_caches
+       req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
+
+       echo "before: $req_before, after: $req_after"
+       [ $((req_after - req_before)) -ge 300 ] &&
+               error "open/close requests are not freed"
+       return 0
+}
+run_test 209 "read-only open/close requests should be freed promptly"
+
 test_212() {
        size=`date +%s`
        size=$((size % 8192 + 1))
@@ -10509,14 +11316,15 @@ test_213() {
 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
 
 test_214() { # for bug 20133
-       test_mkdir -p $DIR/d214p/d214c
+       mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
        for (( i=0; i < 340; i++ )) ; do
-               touch $DIR/d214p/d214c/a$i
+               touch $DIR/$tdir/d214c/a$i
        done
 
-       ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
-       mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
+       ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
+       mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
        ls $DIR/d214c || error "ls $DIR/d214c failed"
+       rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
        rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
 }
 run_test 214 "hash-indexed directory test - bug 20133"
@@ -10548,8 +11356,8 @@ check_lnet_proc_stats() {
 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
 # optional and can be regexp for 2nd line (lnet.routes case)
 check_lnet_proc_entry() {
-       local blp=2            # blp stands for 'position of 1st line of body'
-       [ "$5" = "" ] || blp=3 # lnet.routes case
+       local blp=2          # blp stands for 'position of 1st line of body'
+       [ -z "$5" ] || blp=3 # lnet.routes case
 
        local l=$(cat "$TMP/lnet_$1" |wc -l)
        # subtracting one from $blp because the body can be empty
@@ -10562,7 +11370,7 @@ check_lnet_proc_entry() {
                (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
 
        # bail out if any unexpected line happened
-       sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
+       sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
        [ "$?" != 0 ] || error "$2 misformatted"
 }
 
@@ -10588,12 +11396,13 @@ test_215() { # for bugs 18102, 21079, 21517
 
        # /proc/sys/lnet/routes should look like this:
        # Routing disabled/enabled
-       # net hops state router
-       # where net is a string like tcp0, hops >= 0, state is up/down,
+       # net hops priority state router
+       # where net is a string like tcp0, hops > 0, priority >= 0,
+       # state is up/down,
        # router is a string like 192.168.1.1@tcp2
        L1="^Routing (disabled|enabled)$"
-       L2="^net +hops +state +router$"
-       BR="^$NET +$N +(up|down) +$NID$"
+       L2="^net +hops +priority +state +router$"
+       BR="^$NET +$N +(0|1) +(up|down) +$NID$"
        create_lnet_proc_files "routes"
        check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
        check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
@@ -10740,6 +11549,10 @@ test_219() {
         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
         $LCTL set_param fail_loc=0x411
         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
+
+       # LU-4201
+       dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
+       $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
 }
 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
 
@@ -11011,7 +11824,8 @@ run_test 226b "call path2fid and fid2path on files of all type under remote dir"
 # cause an out-of-memory condition.
 test_227() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
-       dd if=`which date` of=$MOUNT/date bs=1k count=1
+       [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
+       dd if=$(which date) of=$MOUNT/date bs=1k count=1
        chmod +x $MOUNT/date
 
        $MOUNT/date > /dev/null
@@ -11157,6 +11971,9 @@ test_228c() {
 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
 
 test_229() { # LU-2482, LU-3448
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
+               skip "No HSM support on MDS of $(get_lustre_version)," \
+                        "need 2.4.53 at least" && return
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
 
@@ -11168,29 +11985,23 @@ test_229() { # LU-2482, LU-3448
 
        $GETSTRIPE -v $DIR/$tfile
 
-       local pattern=$($GETSTRIPE -v $DIR/$tfile |
-                       grep lmm_stripe_pattern | awk '{print $2}')
+       local pattern=$($GETSTRIPE -L $DIR/$tfile)
        [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
 
        local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
        [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
        stat $DIR/$tfile || error "failed to stat released file"
 
-       # Truncate should fail.
-       $TRUNCATE $DIR/$tfile 200000 &&
-               error "truncate of released file should fail"
-
-       # Ensure that nothing happened anyway.
-       $CHECKSTAT -s 0 $DIR/$tfile ||
-               error "released file size should not change"
+       chown $RUNAS_ID $DIR/$tfile ||
+               error "chown $RUNAS_ID $DIR/$tfile failed"
 
-       # Stripe count should be no change after truncate
-       stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe failed"
-       [ $stripe_count -eq 2 ] || error "after trunc: ($stripe_count)"
+       chgrp $RUNAS_ID $DIR/$tfile ||
+               error "chgrp $RUNAS_ID $DIR/$tfile failed"
 
+       touch $DIR/$tfile || error "touch $DIR/$tfile failed"
        rm $DIR/$tfile || error "failed to remove released file"
 }
-run_test 229 "getstripe/stat/rm work on released files (stripe count = 2)"
+run_test 229 "getstripe/stat/rm/attr changes work on released files"
 
 test_230a() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
@@ -11220,45 +12031,203 @@ test_230b() {
        [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
        [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
        local MDTIDX=1
-       local remote_dir=$DIR/$tdir/remote_dir
-       local rc=0
+       local mdt_index
+       local i
+       local file
+       local pid
+       local stripe_count
+       local migrate_dir=$DIR/$tdir/migrate_dir
+       local other_dir=$DIR/$tdir/other_dir
+
+       mkdir -p $migrate_dir
+       mkdir -p $other_dir
+       for ((i=0; i<10; i++)); do
+               mkdir -p $migrate_dir/dir_${i}
+               createmany -o $migrate_dir/dir_${i}/f 10 ||
+                       error "create files under remote dir failed $i"
+       done
+
+       cp /etc/passwd $migrate_dir/$tfile
+       cp /etc/passwd $other_dir/$tfile
+       mkdir -p $migrate_dir/dir_default_stripe2
+       $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
+       $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
+
+       mkdir -p $other_dir
+       ln $migrate_dir/$tfile $other_dir/luna
+       ln $migrate_dir/$tfile $migrate_dir/sofia
+       ln $other_dir/$tfile $migrate_dir/david
+       ln -s $migrate_dir/$tfile $other_dir/zachary
+       ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
+       ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
+
+       $LFS mv -v -M $MDTIDX $migrate_dir ||
+                       error "migrate remote dir error"
+
+       echo "migratate to MDT1, then checking.."
+       for ((i=0; i<10; i++)); do
+               for file in $(find $migrate_dir/dir_${i}); do
+                       mdt_index=$($LFS getstripe -M $file)
+                       [ $mdt_index == $MDTIDX ] ||
+                               error "$file is not on MDT${MDTIDX}"
+               done
+       done
+
+       # the multiple link file should still in MDT0
+       mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
+       [ $mdt_index == 0 ] ||
+               error "$file is not on MDT${MDTIDX}"
+
+       diff /etc/passwd $migrate_dir/$tfile ||
+               error "$tfile different after migration"
+
+       diff /etc/passwd $other_dir/luna ||
+               error "luna different after migration"
+
+       diff /etc/passwd $migrate_dir/sofia ||
+               error "sofia different after migration"
+
+       diff /etc/passwd $migrate_dir/david ||
+               error "david different after migration"
+
+       diff /etc/passwd $other_dir/zachary ||
+               error "zachary different after migration"
+
+       diff /etc/passwd $migrate_dir/${tfile}_ln ||
+               error "${tfile}_ln different after migration"
+
+       diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
+               error "${tfile}_ln_other different after migration"
+
+       stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
+       [ $stripe_count = 2 ] ||
+                       error "dir strpe_count $d != 2 after migration."
+
+       stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
+       [ $stripe_count = 2 ] ||
+                       error "file strpe_count $d != 2 after migration."
+
+       #migrate back to MDT0
+       MDTIDX=0
+       $LFS mv -v -M $MDTIDX $migrate_dir ||
+                       error "migrate remote dir error"
+
+       echo "migrate back to MDT0, checking.."
+       for file in $(find $migrate_dir); do
+               mdt_index=$($LFS getstripe -M $file)
+               [ $mdt_index == $MDTIDX ] ||
+                       error "$file is not on MDT${MDTIDX}"
+       done
+
+       diff /etc/passwd ${migrate_dir}/$tfile ||
+               error "$tfile different after migration"
+
+       diff /etc/passwd ${other_dir}/luna ||
+               error "luna different after migration"
 
+       diff /etc/passwd ${migrate_dir}/sofia ||
+               error "sofia different after migration"
+
+       diff /etc/passwd ${other_dir}/zachary ||
+               error "zachary different after migration"
+
+       diff /etc/passwd $migrate_dir/${tfile}_ln ||
+               error "${tfile}_ln different after migration"
+
+       diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
+               error "${tfile}_ln_other different after migration"
+
+       stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
+       [ $stripe_count = 2 ] ||
+               error "dir strpe_count $d != 2 after migration."
+
+       stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
+       [ $stripe_count = 2 ] ||
+               error "file strpe_count $d != 2 after migration."
+
+       rm -rf $DIR/$tdir || error "rm dir failed after migration"
+}
+run_test 230b "migrate directory"
+
+test_230c() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local MDTIDX=1
+       local mdt_index
+       local file
+
+       #If migrating directory fails in the middle, all entries of
+       #the directory is still accessiable.
        mkdir -p $DIR/$tdir
-       $LFS mkdir -i $MDTIDX $remote_dir ||
-               error "create remote directory failed"
+       stat $DIR/$tdir
+       createmany -o $DIR/$tdir/f 10 ||
+               error "create files under ${tdir} failed"
+
+       #failed after migrating 5 entries
+       #OBD_FAIL_MIGRATE_ENTRIES       0x1801
+       do_facet mds1 lctl set_param fail_loc=0x20001801
+       do_facet mds1 lctl  set_param fail_val=5
+       local t=`ls $DIR/$tdir | wc -l`
+       $LFS mv -M $MDTIDX $DIR/$tdir &&
+               error "migrate should failed after 5 entries"
+       local u=`ls $DIR/$tdir | wc -l`
+       [ "$u" == "$t" ] || error "$u != $t during migration"
 
-       $LFS mkdir -i 0 $remote_dir/new_dir &&
-               error "nested remote directory create succeed!"
+       for file in $(find $DIR/$tdir); do
+               stat $file || error "stat $file failed"
+       done
 
-       do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
-       $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
-       do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
+       do_facet mds1 lctl set_param fail_loc=0
+       do_facet mds1 lctl set_param fail_val=0
 
-       [ $rc -ne 0 ] &&
-          error "create remote directory failed after set enable_remote_dir"
+       $LFS mv -M $MDTIDX $DIR/$tdir ||
+               error "migrate open files should failed with open files"
 
-       rm -rf $remote_dir || error "first unlink remote directory failed"
+       echo "Finish migration, then checking.."
+       for file in $(find $DIR/$tdir); do
+               mdt_index=$($LFS getstripe -M $file)
+               [ $mdt_index == $MDTIDX ] ||
+                       error "$file is not on MDT${MDTIDX}"
+       done
 
-       $RUNAS -G$RUNAS_GID $LFS mkdir -i $MDTIDX $DIR/$tfile &&
-                                                       error "chown worked"
+       rm -rf $DIR/$tdir || error "rm dir failed after migration"
+}
+run_test 230c "check directory accessiblity if migration is failed"
 
-       do_facet mds$MDTIDX lctl set_param \
-                               mdt.*.enable_remote_dir_gid=$RUNAS_GID
-       $LFS mkdir -i $MDTIDX $remote_dir || rc=$?
-       do_facet mds$MDTIDX lctl set_param mdt.*.enable_remote_dir_gid=0
+test_230d() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local MDTIDX=1
+       local mdt_index
+       local i
+       local j
 
-       [ $rc -ne 0 ] &&
-          error "create remote dir failed after set enable_remote_dir_gid"
+       mkdir -p $DIR/$tdir
+
+       for ((i=0; i<100; i++)); do
+               mkdir -p $DIR/$tdir/dir_${i}
+               createmany -o $DIR/$tdir/dir_${i}/f 100 ||
+                       error "create files under remote dir failed $i"
+       done
+
+       $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
+
+       echo "Finish migration, then checking.."
+       for file in $(find $DIR/$tdir); do
+               mdt_index=$($LFS getstripe -M $file)
+               [ $mdt_index == $MDTIDX ] ||
+                       error "$file is not on MDT${MDTIDX}"
+       done
 
-       rm -r $DIR/$tdir || error "second unlink remote directory failed"
+       rm -rf $DIR/$tdir || error "rm dir failed after migration"
 }
-run_test 230b "nested remote directory should be failed"
+run_test 230d "check migrate big directory"
 
 test_231a()
 {
        # For simplicity this test assumes that max_pages_per_rpc
        # is the same across all OSCs
-       local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
+       local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
        local bulk_size=$((max_pages * 4096))
 
        mkdir -p $DIR/$tdir
@@ -11318,12 +12287,356 @@ test_232() {
 }
 run_test 232 "failed lock should not block umount"
 
-test_233() {
+test_233a() {
+       [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
+       { skip "Need MDS version at least 2.3.64"; return; }
+
        local fid=$($LFS path2fid $MOUNT)
        stat $MOUNT/.lustre/fid/$fid > /dev/null ||
                error "cannot access $MOUNT using its FID '$fid'"
 }
-run_test 233 "checking that OBF of the FS root succeeds"
+run_test 233a "checking that OBF of the FS root succeeds"
+
+test_233b() {
+       [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
+       { skip "Need MDS version at least 2.5.90"; return; }
+
+       local fid=$($LFS path2fid $MOUNT/.lustre)
+       stat $MOUNT/.lustre/fid/$fid > /dev/null ||
+               error "cannot access $MOUNT/.lustre using its FID '$fid'"
+
+       fid=$($LFS path2fid $MOUNT/.lustre/fid)
+       stat $MOUNT/.lustre/fid/$fid > /dev/null ||
+               error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
+}
+run_test 233b "checking that OBF of the FS .lustre succeeds"
+
+test_234() {
+       local p="$TMP/sanityN-$TESTNAME.parameters"
+       save_lustre_params client "llite.*.xattr_cache" > $p
+       lctl set_param llite.*.xattr_cache 1 ||
+               { skip "xattr cache is not supported"; return 0; }
+
+       mkdir -p $DIR/$tdir || error "mkdir failed"
+       touch $DIR/$tdir/$tfile || error "touch failed"
+       # OBD_FAIL_LLITE_XATTR_ENOMEM
+       $LCTL set_param fail_loc=0x1405
+       if [ ! -f /etc/SuSE-release ]; then
+               # attr pre-2.4.44-7 had a bug with rc
+               # LU-3703 - SLES clients have older attr
+               getfattr -n user.attr $DIR/$tdir/$tfile &&
+                       error "getfattr should have failed with ENOMEM"
+       fi
+       $LCTL set_param fail_loc=0x0
+       rm -rf $DIR/$tdir
+
+       restore_lustre_params < $p
+       rm -f $p
+}
+run_test 234 "xattr cache should not crash on ENOMEM"
+
+test_235() {
+       [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
+               skip "Need MDS version at least 2.4.52" && return
+       flock_deadlock $DIR/$tfile
+       local RC=$?
+       case $RC in
+               0)
+               ;;
+               124) error "process hangs on a deadlock"
+               ;;
+               *) error "error executing flock_deadlock $DIR/$tfile"
+               ;;
+       esac
+}
+run_test 235 "LU-1715: flock deadlock detection does not work properly"
+
+#LU-2935
+test_236() {
+       check_swap_layouts_support && return 0
+       test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
+
+       local ref1=/etc/passwd
+       local ref2=/etc/group
+       local file1=$DIR/$tdir/f1
+       local file2=$DIR/$tdir/f2
+
+       $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
+       cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
+       $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
+       cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
+       local fd=$(free_fd)
+       local cmd="exec $fd<>$file2"
+       eval $cmd
+       rm $file2
+       $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
+               error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
+       cmd="exec $fd>&-"
+       eval $cmd
+       cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
+
+       #cleanup
+       rm -rf $DIR/$tdir
+}
+run_test 236 "Layout swap on open unlinked file"
+
+# test to verify file handle related system calls
+# (name_to_handle_at/open_by_handle_at)
+# The new system calls are supported in glibc >= 2.14.
+
+test_237() {
+       echo "Test file_handle syscalls" > $DIR/$tfile
+       check_fhandle_syscalls $DIR/$tfile ||
+               error "check_fhandle_syscalls failed"
+}
+run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
+
+# LU-4659 linkea consistency
+test_238() {
+       touch $DIR/$tfile
+       ln $DIR/$tfile $DIR/$tfile.lnk
+       touch $DIR/$tfile.new
+       mv $DIR/$tfile.new $DIR/$tfile
+       local fid1=$(lfs path2fid $DIR/$tfile)
+       local fid2=$(lfs path2fid $DIR/$tfile.lnk)
+       local path1=$(lfs fid2path $FSNAME $fid1)
+       [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
+       local path2=$(lfs fid2path $FSNAME $fid2)
+       [ $tfile.lnk == $path2 ] ||
+               error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
+       rm -f $DIR/$tfile*
+}
+run_test 238 "Verify linkea consistency"
+
+test_striped_dir() {
+       local mdt_index=$1
+       local stripe_count
+       local stripe_index
+
+       mkdir -p $DIR/$tdir
+       $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+
+       stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
+       if [ "$stripe_count" != "2" ]; then
+               error "stripe_count is $stripe_count, expect 2"
+       fi
+
+       stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
+       if [ "$stripe_index" != "$mdt_index" ]; then
+               error "stripe_index is $stripe_index, expect $mdt_index"
+       fi
+
+       [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
+               error "nlink error after create striped dir"
+
+       mkdir $DIR/$tdir/striped_dir/a
+       mkdir $DIR/$tdir/striped_dir/b
+
+       stat $DIR/$tdir/striped_dir/a ||
+               error "create dir under striped dir failed"
+       stat $DIR/$tdir/striped_dir/b ||
+               error "create dir under striped dir failed"
+
+       [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
+               error "nlink error after mkdir"
+
+       rmdir $DIR/$tdir/striped_dir/a
+       [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
+               error "nlink error after rmdir"
+
+       rmdir $DIR/$tdir/striped_dir/b
+       [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
+               error "nlink error after rmdir"
+
+       rmdir $DIR/$tdir/striped_dir ||
+               error "rmdir striped dir error"
+       true
+}
+
+test_300a() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+
+       test_striped_dir 0 || error "failed on striped dir on MDT0"
+       test_striped_dir 1 || error "failed on striped dir on MDT0"
+}
+run_test 300a "basic striped dir sanity test"
+
+test_300b() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local i
+       local mtime1
+       local mtime2
+       local mtime3
+
+       test_mkdir $DIR/$tdir || error "mkdir fail"
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+       for ((i=0; i<10; i++)); do
+               mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
+               sleep 1
+               touch $DIR/$tdir/striped_dir/file_$i ||
+                                       error "touch error $i"
+               mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
+               [ $mtime1 -eq $mtime2 ] &&
+                       error "mtime not change after create"
+               sleep 1
+               rm -f $DIR/$tdir/striped_dir/file_$i ||
+                                       error "unlink error $i"
+               mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
+               [ $mtime2 -eq $mtime3 ] &&
+                       error "mtime did not change after unlink"
+       done
+       true
+}
+run_test 300b "check ctime/mtime for striped dir"
+
+test_300c() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local file_count
+
+       mkdir -p $DIR/$tdir
+       $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+
+       chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
+               error "chown striped dir failed"
+
+       $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
+               error "create 5k files failed"
+
+       file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
+
+       [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
+
+       rm -rf $DIR/$tdir
+}
+run_test 300c "chown && check ls under striped directory"
+
+test_300d() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local stripe_count
+       local file
+
+       mkdir -p $DIR/$tdir
+       $SETSTRIPE -c 2 $DIR/$tdir
+
+       #local striped directory
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+       createmany -o $DIR/$tdir/striped_dir/f 10 ||
+               error "create 10 files failed"
+
+       #remote striped directory
+       $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
+               error "set striped dir error"
+       createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
+               error "create 10 files failed"
+
+       for file in $(find $DIR/$tdir); do
+               stripe_count=$($GETSTRIPE -c $file)
+               [ $stripe_count -eq 2 ] ||
+                       error "wrong stripe $stripe_count for $file"
+       done
+
+       rm -rf $DIR/$tdir
+}
+run_test 300d "check default stripe under striped directory"
+
+test_300e() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local stripe_count
+       local file
+
+       mkdir -p $DIR/$tdir
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+
+       touch $DIR/$tdir/striped_dir/a
+       touch $DIR/$tdir/striped_dir/b
+       touch $DIR/$tdir/striped_dir/c
+
+       mkdir $DIR/$tdir/striped_dir/dir_a
+       mkdir $DIR/$tdir/striped_dir/dir_b
+       mkdir $DIR/$tdir/striped_dir/dir_c
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
+               error "set striped dir under striped dir error"
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
+               error "set striped dir under striped dir error"
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
+               error "set striped dir under striped dir error"
+
+       mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
+               error "rename file under striped dir should fail"
+
+       mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
+               error "rename dir under striped dir should fail"
+
+       mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
+               error "rename dir under different stripes should fail"
+
+       mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
+               error "rename file under striped dir should succeed"
+
+       mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
+               error "rename dir under striped dir should succeed"
+
+       rm -rf $DIR/$tdir
+}
+run_test 300e "check rename under striped directory"
+
+test_300f() {
+       [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
+       [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
+       local stripe_count
+       local file
+
+       rm -rf $DIR/$tdir
+       mkdir -p $DIR/$tdir
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
+               error "set striped dir error"
+
+       $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
+               error "set striped dir error"
+
+       touch $DIR/$tdir/striped_dir/a
+       mkdir $DIR/$tdir/striped_dir/dir_a
+       $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
+               error "create striped dir under striped dir fails"
+
+       touch $DIR/$tdir/striped_dir1/b
+       mkdir $DIR/$tdir/striped_dir1/dir_b
+       $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
+               error "create striped dir under striped dir fails"
+
+       mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
+               error "rename file under different striped dir should fail"
+
+       mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
+               error "rename dir under different striped dir should fail"
+
+       mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
+               error "rename striped dir under diff striped dir should fail"
+
+       mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
+               error "rename file under diff striped dirs fails"
+
+       mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
+               error "rename dir under diff striped dirs fails"
+
+       rm -rf $DIR/$tdir
+}
+run_test 300f "check rename cross striped directory"
 
 #
 # tests that do cleanup/setup should be run at the end
@@ -11344,6 +12657,7 @@ test_900() {
 run_test 900 "umount should not race with any mgc requeue thread"
 
 complete $SECONDS
+[ -f $EXT2_DEV ] && rm $EXT2_DEV || true
 check_and_cleanup_lustre
 if [ "$I_MOUNTED" != "yes" ]; then
        lctl set_param debug="$OLDDEBUG" 2> /dev/null || true