+# Compare file size between first & second mount, ensuring the client correctly
+# glimpses even with unused speculative locks - LU-11670
+test_103() {
+ [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
+ skip "Lockahead needs OST version at least 2.10.50"
+
+ local locktest=23
+
+ test_mkdir -p $DIR/$tdir
+
+ # Force file on to OST0
+ $LFS setstripe -i 0 $DIR/$tdir
+
+ # Do not check multiple locks on glimpse
+ # OBD_FAIL_OSC_NO_SIZE_DATA 0x415
+ $LCTL set_param fail_loc=0x415
+
+ # Delay write commit by 2 seconds to guarantee glimpse wins race
+ # The same fail_loc is used on client & server so it can work in the
+ # single node sanity setup
+ do_facet ost1 $LCTL set_param fail_loc=0x415 fail_val=2
+
+ echo "Incorrect size expected (no glimpse fix):"
+ lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
+ rc=$?
+ if [ $rc -eq 0 ]; then
+ echo "This doesn't work 100%, but this is just reproducing the bug, not testing the fix, so OK to not fail test."
+ fi
+
+ # guarantee write commit timeout has expired
+ sleep 2
+
+ # Clear fail_loc on client
+ $LCTL set_param fail_loc=0
+
+ # Delay write commit by 2 seconds to guarantee glimpse wins race
+ # OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
+ do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
+
+ # Write commit is still delayed by 2 seconds
+ lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
+ rc=$?
+ [ $rc -eq 0 ] || error "Lockahead test$locktest failed, $rc"
+
+ # guarantee write commit timeout has expired
+ sleep 2
+
+ rm -f $DIR/$tfile || error "unable to delete $DIR/$tfile"
+}
+run_test 103 "Test size correctness with lockahead"
+
+get_stat_xtimes()
+{
+ local xtimes
+
+ xtimes=$(stat -c "%X %Y %Z" $DIR/$tfile)
+
+ echo ${xtimes[*]}
+}
+
+get_mdt_xtimes()
+{
+ local mdtdev=$1
+ local output
+ local xtimes
+
+ output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
+ ((xtimes[0]=$(awk -F ':' /atime/'{ print $2 }' <<< "$output")))
+ ((xtimes[1]=$(awk -F ':' /mtime/'{ print $2 }' <<< "$output")))
+ ((xtimes[2]=$(awk -F ':' /ctime/'{ print $2 }' <<< "$output")))
+
+ echo ${xtimes[*]}
+}
+
+check_mdt_xtimes()
+{
+ local mdtdev=$1
+ local xtimes=($(get_stat_xtimes))
+ local mdt_xtimes=($(get_mdt_xtimes $mdtdev))
+
+ echo "STAT a|m|ctime ${xtimes[*]}"
+ echo "MDT a|m|ctime ${mdt_xtimes[*]}"
+ [[ ${xtimes[0]} == ${mdt_xtimes[0]} ]] ||
+ error "$DIR/$tfile atime (${xtimes[0]}:${mdt_xtimes[0]}) diff"
+ [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
+ error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
+ [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
+ error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
+}
+
+test_104() {
+ [ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"
+ [ $MDS1_VERSION -lt $(version_code 2.12.4) ] &&
+ skip "Need MDS version at least 2.12.4"
+
+ local pid
+ local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
+ local atime_diff=$(do_facet $SINGLEMDS \
+ lctl get_param -n mdd.*MDT0000*.atime_diff)
+
+ do_facet $SINGLEMDS \
+ lctl set_param -n mdd.*MDT0000*.atime_diff=0
+
+ stack_trap "do_facet $SINGLEMDS \
+ lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff" EXIT
+
+ dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
+ check_mdt_xtimes $mdtdev
+ sleep 2
+
+ dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
+ check_mdt_xtimes $mdtdev
+ sleep 2
+ $MULTIOP $DIR2/$tfile Oz8192w8192_c &
+ pid=$!
+ sleep 2
+ dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
+ sleep 2
+ kill -USR1 $pid && wait $pid || error "multiop failure"
+ check_mdt_xtimes $mdtdev
+
+ local xtimes
+ local mdt_xtimes
+
+ # Verify mtime/ctime is NOT upated on MDS when there is no modification
+ # on the client side
+ xtimes=($(get_stat_xtimes))
+ $MULTIOP $DIR/$tfile O_c &
+ pid=$!
+ sleep 2
+ kill -USR1 $pid && wait $pid || error "multiop failure"
+ mdt_xtimes=($(get_mdt_xtimes $mdtdev))
+ [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
+ error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
+ [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
+ error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
+ check_mdt_xtimes $mdtdev
+
+ sleep 2
+ # Change ctime via chmod
+ $MULTIOP $DIR/$tfile o_tc &
+ pid=$!
+ sleep 2
+ kill -USR1 $pid && wait $pid || error "multiop failure"
+ check_mdt_xtimes $mdtdev
+}
+run_test 104 "Verify that MDS stores atime/mtime/ctime during close"
+
+test_105() {
+ test_mkdir -p $DIR/$tdir
+ echo test > $DIR/$tdir/$tfile
+ $LCTL set_param fail_loc=0x416
+ cancel_lru_locks osc & sleep 1
+ fsize1=$(stat -c %s $DIR2/$tdir/$tfile)
+ wait
+ [[ $fsize1 = 5 ]] || error "Glimpse returned wrong file size $fsize1"
+}
+run_test 105 "Glimpse and lock cancel race"
+
+test_106a() {
+ [ "$mds1_FSTYPE" == "ldiskfs" ] && statx_supported ||
+ skip_env "Test only for ldiskfs and statx() supported"
+
+ local btime
+ local mdt_btime
+ local output
+ local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
+
+ dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
+ btime=$($STATX -c %W $DIR/$tfile)
+ output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
+ echo $output
+ ((mdt_btime=$(awk -F ':' /crtime/'{ print $2 }' <<< "$output")))
+ [[ $btime == $mdt_btime ]] ||
+ error "$DIR/$tfile btime ($btime:$mdt_btime) diff"
+
+}
+run_test 106a "Verify the btime via statx()"
+
+test_106b() {
+ statx_supported || skip_env "statx() only test"
+
+ local rpcs_before
+ local rpcs_after
+
+ $LFS setstripe -c 1 $DIR/$tfile || error "$DIR/$tfile setstripe failed"
+ dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ $STATX $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
+ error "$STATX should send 1 glimpse RPC to $OSC"
+
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ # %n: FILENAME; %i: STATX_INO; %A STATX_MODE; %h STATX_NLINK;
+ # %u: STATX_UID; %g: STATX_GID; %W STATX_BTIME; %X STATX_ATIME;
+ # %Z: STATX_CTIME
+ $STATX -c "%n %i %A %h %u %g %W %X %Z" $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $rpcs_before ] ||
+ error "$STATX should not send glimpse RPCs to $OSC"
+
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ $STATX --cached=always $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $rpcs_before ] ||
+ error "$STATX should not send glimpse RPCs to $OSC"
+
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ $STATX -c %Y $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
+ error "$STATX -c %Y should send 1 glimpse RPC to $OSC"
+
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ $STATX -c %s $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
+ error "$STATX -c %s should send 1 glimpse RPC to $OSC"
+
+ cancel_lru_locks $OSC
+ rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ $STATX -c %b $DIR/$tfile
+ rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
+ [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
+ error "$STATX -c %b should send 1 glimpse RPC to $OSC"
+}
+run_test 106b "Glimpse RPCs test for statx"
+
+test_106c() {
+ statx_supported || skip_env "statx() only test"
+
+ local mask
+
+ touch $DIR/$tfile
+ # Mask supported in stx_attributes by Lustre is
+ # STATX_ATTR_IMMUTABLE(0x10) | STATX_ATTR_APPEND(0x20) : (0x30).
+ mask=$($STATX -c %p $DIR/$tfile)
+ [[ $mask == "30" ]] ||
+ error "supported stx_attributes: got '$mask', expected '30'"
+ chattr +i $DIR/$tfile || error "chattr +i $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "10" ]] ||
+ error "got immutable flags '$mask', expected '10'"
+ chattr -i $DIR/$tfile || error "chattr -i $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
+ chattr +a $DIR/$tfile || error "chattr +a $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "20" ]] || error "got flags '$mask', expected '20'"
+ chattr -a $DIR/$tfile || error "chattr -a $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
+ chattr +ia $DIR/$tfile || error "chattr +ia $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "30" ]] || error "got flags '$mask', expected '30'"
+ chattr -ia $DIR/$tfile || error "chattr -ia $DIR/$tfile failed"
+ mask=$($STATX -c %r $DIR/$tfile)
+ [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
+}
+run_test 106c "Verify statx attributes mask"
+
+test_107a() { # LU-1031
+ dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
+ local gid1=14091995
+ local gid2=16022000
+
+ $LFS getstripe $DIR1/$tfile
+
+ multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
+ local MULTIPID1=$!
+ multiop_bg_pause $DIR2/$tfile O_G${gid2}r10g${gid2}c || return 2
+ local MULTIPID2=$!
+ kill -USR1 $MULTIPID2
+ sleep 2
+ if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
+ error "First grouplock does not block second one"
+ else
+ echo "First grouplock blocks second one"
+ fi
+ kill -USR1 $MULTIPID1
+ wait $MULTIPID1
+ wait $MULTIPID2
+}
+run_test 107a "Basic grouplock conflict"
+
+test_107b() {
+ dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
+ local gid1=14091995
+ local gid2=16022000
+
+ $LFS getstripe $DIR1/$tfile
+
+ multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
+ local MULTIPID1=$!
+ multiop $DIR2/$tfile Or10c &
+ local MULTIPID2=$!
+ sleep 2
+
+ if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
+ error "Grouplock does not block IO"
+ else
+ echo "Grouplock blocks IO"
+ fi
+
+ multiop $DIR2/$tfile OG${gid2}_g${gid2}c &
+ local MULTIPID3=$!
+ sleep 2
+ if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
+ error "First grouplock does not block second one"
+ else
+ echo "First grouplock blocks second one"
+ fi
+
+ kill -USR1 $MULTIPID1
+ sleep 2
+
+ if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
+ error "Second grouplock thread disappeared"
+ fi
+
+ if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
+ error "Second grouplock does not block IO"
+ else
+ echo "Second grouplock blocks IO"
+ fi
+
+ kill -USR1 $MULTIPID3
+ wait $MULTIPID1
+ wait $MULTIPID2
+ wait $MULTIPID3
+}
+run_test 107b "Grouplock is added to the head of waiting list"
+
+test_108a() {
+ local offset
+
+ $LFS setstripe -E 1M -c 1 -E -1 $DIR1/$tfile ||
+ error "Create $DIR1/$tfile failed"
+
+ dd if=/dev/zero of=$DIR1/$tfile bs=10000 count=1 ||
+ error "dd $DIR1/$tfile failed"
+ offset=$(lseek_test -d 5000 $DIR2/$tfile)
+ [[ $offset == 5000 ]] || error "offset $offset != 5000"
+
+ $TRUNCATE $DIR1/$tfile 2000
+ offset=$(lseek_test -l 1000 $DIR2/$tfile)
+ [[ $offset == 2000 ]] || error "offset $offset != 2000"
+
+ #define OBD_FAIL_OSC_DELAY_IO 0x414
+ $LCTL set_param fail_val=4 fail_loc=0x80000414
+ dd if=/dev/zero of=$DIR1/$tfile count=1 bs=8M conv=notrunc oflag=dsync &
+ local pid=$!
+ sleep 2
+
+ offset=$(lseek_test -l 8000 $DIR2/$tfile)
+ wait $pid
+ [[ $offset == 8388608 ]] || error "offset $offset != 8388608"
+}
+run_test 108a "lseek: parallel updates"
+
+# LU-14110
+test_109() {
+ local i
+ local pid1 pid2
+
+ ! local_mode ||
+ skip "Clients need to be on different nodes than the servers"
+
+ umount_client $MOUNT
+ umount_client $MOUNT2
+
+ echo "Starting race between client mount instances (50 iterations):"
+ for i in {1..50}; do
+ log "Iteration $i"
+
+#define OBD_FAIL_ONCE|OBD_FAIL_LLITE_RACE_MOUNT 0x80001417
+ $LCTL set_param -n fail_loc=0x80001417
+
+ mount_client $MOUNT & pid1=$!
+ mount_client $MOUNT2 & pid2=$!
+ wait $pid1 || error "Mount $MOUNT fails with $?"
+ wait $pid2 || error "Mount $MOUNT2 fails with $?"
+
+ umount_client $MOUNT & pid1=$!
+ umount_client $MOUNT2 & pid2=$!
+ wait $pid1 || error "Umount $MOUNT fails with $?"
+ wait $pid2 || error "Umount $MOUNT2 fails with $?"
+
+ $LUSTRE_RMMOD || error "Fail to remove lustre modules"
+ load_modules
+ echo
+ done
+
+ mount_client $MOUNT
+ mount_client $MOUNT2
+}
+
+run_test 109 "Race with several mount instances on 1 node"
+
+test_110() {
+ local before=$(date +%s)
+ local evict
+
+ mkdir -p $DIR/$tdir
+ touch $DIR/$tdir/f1
+ touch $DIR/$tfile
+
+ #define OBD_FAIL_PTLRPC_RESEND_RACE 0x525
+ do_facet mds1 lctl set_param fail_loc=0x525 fail_val=3
+
+ # disable last_xid logic by dropping link reply
+ ln $DIR/$tdir/f1 $DIR/$tdir/f2 &
+ sleep 1
+
+ #define OBD_FAIL_PTLRPC_ENQ_RESEND 0x534
+ do_facet mds1 lctl set_param fail_loc=0x534
+
+ # RPC will race with its Resend and the Resend will sleep to let
+ # the original lock to get granted & cancelled.
+ #
+ # AST_SENT is set artificially, so an explicit conflict is not needed
+ #
+ # The woken up Resend gets a new lock, but client does not wait for it
+ stat $DIR/$tfile
+ sleep $TIMEOUT
+ do_facet mds1 lctl set_param fail_loc=0 fail_val=0
+
+ # Take a conflict to wait long enough to see the eviction
+ touch $DIR2/$tfile
+
+ # let the client reconnect
+ client_reconnect
+ evict=$(do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state |
+ awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
+
+ [ -z "$evict" ] || [[ $evict -le $before ]] ||
+ (do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state;
+ error "eviction happened: $evict before:$before")
+}
+run_test 110 "do not grant another lock on resend"
+
+test_111() {
+ [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
+ [[ $(facet_active_host mds1) = $(facet_active_host mds2) ]] ||
+ skip "MDT0 and MDT1 should be on the same node"
+
+ mkdir $DIR1/$tdir
+ $LFS mkdir -i 0 $DIR1/$tdir/mdt0dir
+ $LFS mkdir -i 1 $DIR1/$tdir/mdt1dir
+
+ mkdir $DIR1/$tdir/mdt0dir/foodir
+ touch $DIR1/$tdir/mdt0dir/foodir/{file1,file2}
+
+ $MULTIOP $DIR2/$tdir/mdt0dir/foodir/file2 Ow4096_c &
+ MULTIOP_PID=$!
+ ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2
+
+ #define OBD_FAIL_MDS_LINK_RENAME_RACE 0x18a
+ do_facet mds1 $LCTL set_param fail_loc=0x8000018a
+
+ ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2x &
+ sleep 1
+
+ rm $DIR2/$tdir/mdt1dir/file2
+ sleep 1
+
+ mv $DIR2/$tdir/mdt0dir/foodir/file1 $DIR2/$tdir/mdt0dir/foodir/file2
+ sleep 1
+
+ kill $MULTIOP_PID
+ wait
+ rm -r $DIR1/$tdir || error "Removing test dir failed"
+}
+run_test 111 "A racy rename/link an open file should not cause fs corruption"
+
+test_112() {
+ (( MDSCOUNT >= 2 )) ||
+ skip "We need at least 2 MDTs for this test"
+
+ (( MDS1_VERSION >= $(version_code 2.14.54) )) ||
+ skip "Need server version at least 2.14.54"
+
+ local rr
+ local count
+
+ rr=$($LCTL get_param -n lmv.*.qos_threshold_rr | head -n1)
+ rr=${rr%%%}
+ stack_trap "$LCTL set_param lmv.*.qos_threshold_rr=$rr > /dev/null"
+
+ mkdir -p $DIR1/$tdir/s1/s2 || error "mkdir s2 failed"
+ $LFS mkdir -i 0 $DIR1/$tdir/s1/s2/s3 || error "mkdir s3 failed"
+ $LFS setdirstripe -D -i -1 --max-inherit-rr=0 $DIR1/$tdir/s1/s2/s3 ||
+ error "setdirstripe s3 failed"
+ $LCTL set_param lmv.*.qos_threshold_rr=90
+ mkdir $DIR2/$tdir/s1/s2/s3/d{1..64}
+ count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/d* | grep ^0 | wc -l)
+ (( count == 64 )) || error "only $count subdirs created on MDT0"
+
+ $LFS setdirstripe -D -i -1 --max-inherit-rr=3 $DIR1/$tdir/s1/s2/s3 ||
+ error "setdirstripe s3 failed"
+ mkdir $DIR2/$tdir/s1/s2/s3/s{1..64}
+ count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/s* | grep ^0 | wc -l)
+ (( count == 64 / MDSCOUNT )) || error "$count subdirs created on MDT0"
+}
+run_test 112 "update max-inherit in default LMV"
+