Whamcloud - gitweb
LU-17097 osc: delete items in Xarray before its destroy
[fs/lustre-release.git] / lustre / tests / sanity-pcc.sh
old mode 100644 (file)
new mode 100755 (executable)
index 5e6dce5..24a875a
@@ -22,7 +22,7 @@ HSMTOOL_ARCHIVE_FORMAT=v1
 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
 
 . $LUSTRE/tests/test-framework.sh
-init_test_env $@
+init_test_env "$@"
 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
 init_logging
 
@@ -91,10 +91,12 @@ check_file_data()
        local client="$1"
        local path="$2"
        local expected_data="$3"
+       local pid=$4
 
-       path_data=$(do_facet $client cat $path)
-       [[ "x$path_data" == "x$expected_data" ]] || error \
-               "expected $path: $expected_data, got: $path_data"
+       # if $pid is set, then run command within namespace for that process
+       path_data=$(do_facet $client ${pid:+nsenter -t $pid -U -m} cat $path)
+       [[ "x$path_data" == "x$expected_data" ]] ||
+               error "expected $path: $expected_data, got: $path_data"
 }
 
 check_lpcc_data()
@@ -447,7 +449,7 @@ test_1g() {
        check_lpcc_state $file "readwrite"
        do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 &&
                error "non-root user can dd write to $file"
-       chmod 777 $file || error "chmod 777 $file failed"
+       chmod 777 $DIR2/$tfile || error "chmod 777 $DIR2/$tfile failed"
        do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "non-root user cannot write $file with permission (777)"
 
@@ -1048,7 +1050,7 @@ test_12() {
        sleep 3
        wait_request_state $(path2fid $file) RESTORE SUCCEED
        $LFS hsm_remove $file || error "hsm remove $file failed"
-       wait $pid && error "RW-PCC attach $file should fail"
+       wait $pid
        do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
                error "RW-PCC cached file '$lpcc_path' should be removed"
 
@@ -1093,8 +1095,8 @@ test_rule_id() {
 
 test_13a() {
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
-       test_rule_id "u" "500" "runas -u 500"
-       test_rule_id "g" "500" "runas -u 500 -g 500"
+       test_rule_id "u" "$RUNAS_ID" "runas -u $RUNAS_ID"
+       test_rule_id "g" "$RUNAS_GID" "runas -u $RUNAS_ID -g $RUNAS_GID"
 }
 run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
 
@@ -1158,7 +1160,7 @@ test_13c() {
        setup_loopdev $SINGLEAGT $loopfile $mntpt 50
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping $SINGLEAGT \
-               "projid={100\ 200}\&fname={*.h5},uid={500}\&gid={1000}\ rwid=$HSM_ARCHIVE_NUMBER"
+               "projid={100\ 200}\&fname={*.h5},uid={$RUNAS_ID}\&gid={$RUNAS_GID}\ rwid=$HSM_ARCHIVE_NUMBER"
        $LCTL pcc list $MOUNT
        do_facet $SINGLEAGT mkdir -p $DIR/$tdir
        chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
@@ -1199,7 +1201,7 @@ test_13c() {
        rm $file || error "rm $file failed"
 
        file=$DIR/$tdir/ugidcache
-       myRUNAS="runas -u 500 -g 1000"
+       myRUNAS="runas -u $RUNAS_ID -g $RUNAS_GID"
        do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
@@ -1489,6 +1491,171 @@ test_20() {
 }
 run_test 20 "Auto attach works after the inode was once evicted from cache"
 
-complete $SECONDS
+#test 101: containers and PCC
+#LU-15170: Test mount namespaces with PCC
+#This tests the cases where the PCC mount is not present in the container by
+#creating a mount namespace without the PCC mount in it (this is probably the
+#standard config for most containers)
+test_101a() {
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tdir/$tfile
+
+       # Some kernels such as RHEL7 default to 0 user namespaces
+       local maxuserns=$(do_facet $SINGLEAGT cat /proc/sys/user/max_user_namespaces)
+       do_facet $SINGLEAGT "echo 10 > /proc/sys/user/max_user_namespaces"
+       stack_trap "do_facet $SINGLEAGT 'echo $maxuserns > /proc/sys/user/max_user_namespaces'"
+
+       echo "creating user namespace for $RUNAS_ID"
+       # Create a mount and user namespace with this command, and leave the
+       # process running so we can do the rest of our steps
+       local start=$SECONDS
+       local PID=$(do_facet $SINGLEAGT \
+                   "$RUNAS unshare -Um sleep 600 &>/dev/null & echo \\\$!")
+       local elapsed=$((SECONDS - start))
+       local count=0
+
+       do_facet $SINGLEAGT ps auxww | grep sleep
+       echo "Created NS: child (sleep) pid=$PID in $elapsed seconds"
+       [[ -n "$PID" ]] || error "remote sleep start failed"
+       stack_trap "do_facet $SINGLEAGT kill -9 $PID" EXIT
+       (( elapsed < 300 )) || error "remote sleep took $elapsed sec to start"
+
+       # Map 'RUNAS' to root in the namespace, so it has rights to do whatever
+       # This is handled by '-r' in unshare in newer versions
+       do_facet $SINGLEAGT $RUNAS newuidmap $PID 0 $RUNAS_ID 1 ||
+               error "could not map uid $RUNAS_ID to root in namespace"
+       do_facet $SINGLEAGT $RUNAS newgidmap $PID 0 $RUNAS_GID 1 ||
+               error "could not map gid $RUNAS_GID to root in namespace"
+
+       # Create PCC after creating namespace; namespace will not have PCC
+       # mount
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
+
+       # Create a temp file inside the PCC mount to verify mount namespace
+       do_facet $SINGLEAGT touch $mntpt/$tfile.tmp
+       stack_trap "do_facet $SINGLEAGT rm -f $mntpt/$tfile.tmp" EXIT
+       echo "Check for temp file in PCC mount"
+       do_facet $SINGLEAGT test -f $mntpt/$tfile.tmp ||
+               error "Should see $mntpt/$tfile.tmp"
+       echo "Check for temp file in PCC mount from inside namespace"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m test -f $mntpt/$tfile.tmp &&
+               error "Should not see $mntpt/$tfile.tmp from namespace"
+       rm -f $mntpt/$tfile.tmp
+
+       # Finish PCC setup
+       copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
+       setup_pcc_mapping $SINGLEAGT "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER"
+
+       mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
+       chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
+
+       echo "Verify open attach from inside mount namespace"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
+               error "failed to dd write to $file"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach \
+               -i $HSM_ARCHIVE_NUMBER $file || error "cannot attach $file"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc state $file
+
+       check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
+       # Revoke the layout lock, the PCC-cached file will be
+       # detached automatically.
+       do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
+       check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
+       # Detach the file but keep the cache, as the file layout generation
+       # is not changed, so the file is still valid cached in PCC, and can
+       # be reused from PCC cache directly.
+       do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach -k $file ||
+               error "PCC detach $file failed"
+       check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc detach $file ||
+               error "PCC detach $file failed"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m dd if=/dev/zero of=$file bs=1024 count=1 ||
+               error "failed to dd write to $file"
+       rm -f $file || error "rm $file failed"
+
+       echo "Verify auto attach at open from inside NS for RW-PCC"
+       # nsenter has strange behavior with echo, which means we have to place
+       # this in a script so we can use sh, otherwise it doesn't execute echo
+       # in the namespace
+       # NB: using /bin/echo instead of the shell built in does not help
+       echo "echo -n autoattach_data > $file" > $DIR/$tdir/$tfile.shell
+       # File is owned by root, make it accessible to RUNAS user
+       chmod a+rw $DIR/$tdir/$tfile.shell
+       stack_trap 'rm -f $DIR/$tdir/$tfile.shell' EXIT
+       do_facet $SINGLEAGT nsenter -t $PID -U -m "bash $DIR/$tdir/$tfile.shell"
+       do_facet $SINGLEAGT nsenter -t $PID -U -m $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "RW-PCC attach $file failed"
+       check_lpcc_state $file "readwrite"
+
+       # Revoke the layout lock, the PCC-cached file will be
+       # detached automatically.
+       do_facet $SINGLEAGT $LCTL set_param ldlm.namespaces.*mdc*.lru_size=clear
+       check_file_data $SINGLEAGT $file "autoattach_data" $PID
+       check_lpcc_state $file "readwrite"
+
+       # Detach the file with -k option, as the file layout generation
+       # is not changed, so the file is still valid cached in PCC,
+       # and can be reused from PCC cache directly.
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
+               error "RW-PCC detach $file failed"
+       check_lpcc_state $file "readwrite"
+       # HSM released exists archived status
+       check_hsm_flags $file "0x0000000d"
+       check_file_data $SINGLEAGT $file "autoattach_data" $PID
+
+       # HSM restore the PCC cached file, the layout generation
+       # was changed, so the file can not be auto attached.
+       $LFS hsm_restore $file || error "failed to restore $file"
+       wait_request_state $(path2fid $file) RESTORE SUCCEED
+       check_lpcc_state $file "none"
+       # HSM exists archived status
+       check_hsm_flags $file "0x00000009"
+}
+run_test 101a "Test auto attach in mount namespace (simulated container)"
+
+test_102() {
+       grep -q io_uring_setup /proc/kallsyms ||
+               skip "Client OS does not support io_uring I/O engine"
+       io_uring_probe || skip "kernel does not support io_uring fully"
+
+       $LCTL get_param -n mdc.*.connect_flags | grep -q pcc_ro ||
+               skip "Server does not support PCC-RO"
+
+       which fio || skip_env "no fio installed"
+       fio --enghelp | grep -q io_uring ||
+               skip_env "fio does not support io_uring I/O engine"
+
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
+
+       setup_loopdev client $loopfile $mntpt 60
+       mkdir $hsm_root || error "mkdir $hsm_root failed"
+       setup_pcc_mapping client \
+               "projid={0}\ roid=$HSM_ARCHIVE_NUMBER\ pccro=1"
+       do_facet $SINGLEAGT $LCTL set_param llite.*.pcc_async_threshold=0
+
+       local ioengine="io_uring"
+       local numjobs=2
+       local size=10M
+
+       do_facet $SINGLEAGT fio --name=seqwrite --ioengine=$ioengine    \
+               --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
+               --iodepth=64 --size=$size --filename=$file --rw=write ||
+               error "fio seqwrite $file failed"
+
+       # Read the file will trigger the buffered read from Lustre OSTs and
+       # write to PCC copy as @pcc_async_threshold is set with 0.
+       do_facet $SINGLEAGT fio --name=seqread --ioengine=$ioengine     \
+               --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
+               --iodepth=64 --size=$size --filename=$file --rw=read ||
+               error "fio seqread $file failed"
+}
+run_test 102 "PCC-RO should not hange for io_uring I/O engine"
+
+complete_test $SECONDS
 check_and_cleanup_lustre
 exit_status