Whamcloud - gitweb
LU-12472 tests: update sanity-krb5.sh
[fs/lustre-release.git] / lustre / tests / sanity-pcc.sh
index 7517005..e18ad79 100644 (file)
@@ -135,7 +135,8 @@ check_lpcc_state()
        local lustre_path="$1"
        local expected_state="$2"
        local facet=${3:-$SINGLEAGT}
-       local state=$(do_facet $facet $LFS pcc state $lustre_path |
+       local myRUNAS="$4"
+       local state=$(do_facet $facet $myRUNAS $LFS pcc state $lustre_path |
                        awk -F 'type: ' '{print $2}' | awk -F ',' '{print $1}')
 
        [[ "x$state" == "x$expected_state" ]] || error \
@@ -230,6 +231,7 @@ lpcc_rw_test() {
        check_hsm_flags $file "0x0000000d"
        check_lpcc_data $SINGLEAGT $lpcc_path $file "file_data"
 
+       echo "Restore testing..."
        if [ $CLIENTCOUNT -lt 2 -o $restore ]; then
                $LFS hsm_restore $file || error \
                        "failed to restore $file"
@@ -266,9 +268,11 @@ lpcc_rw_test() {
        echo "Start to detach the $file"
        do_facet $SINGLEAGT $LFS pcc detach $file ||
                error "PCC detach $file failed"
+       wait_request_state $(path2fid $file) REMOVE SUCCEED
+
        check_lpcc_state $file "none"
-       # HSM released exists archived status
-       check_hsm_flags $file "0x0000000d"
+       # The file is removed from PCC
+       check_hsm_flags $file "0x00000000"
        check_file_data $SINGLEAGT $file "attach_detach"
 }
 
@@ -299,6 +303,7 @@ test_1e() {
 
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
+       $LCTL pcc list $MOUNT
        mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
        chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
 
@@ -317,6 +322,10 @@ test_1e() {
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
 
+       do_facet $SINGLEAGT $RUNAS $LFS pcc detach -k $file ||
+               error "failed to detach file $file"
+       check_lpcc_state $file "none"
+
        # non-root user is forbidden to access PCC file directly
        lpcc_path=$(lpcc_fid2path $hsm_root $file)
        do_facet $SINGLEAGT $RUNAS touch $lpcc_path &&
@@ -330,9 +339,14 @@ test_1e() {
 
        [[ $perm == "0" ]] || error "PCC file permission ($perm) is not zero"
 
+       do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "failed to attach file $file"
+       check_lpcc_state $file "readwrite"
+
        do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
+       wait_request_state $(path2fid $file) REMOVE SUCCEED
 }
 run_test 1e "Test RW-PCC with non-root user"
 
@@ -379,6 +393,7 @@ test_1f() {
        do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
+       wait_request_state $(path2fid $file) REMOVE SUCCEED
 }
 run_test 1f "Test auto RW-PCC cache with non-root user"
 
@@ -407,6 +422,7 @@ test_1g() {
        do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
+       wait_request_state $(path2fid $file) REMOVE SUCCEED
        do_facet $SINGLEAGT $RUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
                error "non-root user cannot read to $file with permisson (777)"
 }
@@ -554,7 +570,7 @@ test_3a() {
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
 
@@ -562,7 +578,7 @@ test_3a() {
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
 }
@@ -588,7 +604,7 @@ test_3b() {
        do_facet agt1 $LFS pcc attach -i 1 $file ||
                error "failed to attach file $file"
        check_lpcc_state $file "readwrite" agt1
-       do_facet agt1 $LFS pcc detach $file ||
+       do_facet agt1 $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none" agt1
 
@@ -596,7 +612,7 @@ test_3b() {
        do_facet agt2 $LFS pcc attach -i 2 $file ||
                error "failed to attach file $file"
        check_lpcc_state $file "readwrite" agt2
-       do_facet agt2 $LFS pcc detach $file ||
+       do_facet agt2 $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none" agt2
 
@@ -610,7 +626,7 @@ test_3b() {
        # The later attach PCC agent should succeed,
        # the former agent should be detached automatically.
        check_lpcc_state $file "none" agt1
-       do_facet agt2 $LFS pcc detach $file ||
+       do_facet agt2 $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none" agt2
 }
@@ -640,12 +656,11 @@ test_4() {
 run_test 4 "Auto cache test for mmap"
 
 test_5() {
-       local file=$DIR/$tdir/$tfile
+       local file=$DIR/$tfile
 
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
        do_facet $SINGLEAGT "echo -n attach_mmap_data > $file" ||
                error "echo $file failed"
 
@@ -668,15 +683,35 @@ test_5() {
 }
 run_test 5 "Mmap & cat a RW-PCC cached file"
 
+setup_loopdev() {
+       local facet=$1
+       local file=$2
+       local mntpt=$3
+       local size=${4:-50}
+
+       do_facet $facet mkdir -p $mntpt || error "mkdir -p $hsm_root failed"
+       stack_trap "do_facet $facet rm -rf $mntpt" EXIT
+       do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
+       stack_trap "do_facet $facet rm -f $file" EXIT
+       do_facet $facet mkfs.ext4 $file ||
+               error "mkfs.ext4 $file failed"
+       do_facet $facet file $file
+       do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
+               error "mount -o loop,usrquota,grpquota $file $mntpt failed"
+       stack_trap "do_facet $facet $UMOUNT $mntpt" EXIT
+}
+
 test_6() {
-       local file=$DIR/$tdir/$tfile
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
        local content
 
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
-
        echo -n mmap_write_data > $file || error "echo write $file failed"
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
@@ -692,7 +727,7 @@ test_6() {
                error "mmap write data mismatch: $content"
        check_lpcc_state $file "readwrite"
 
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach file $file"
 
        content=$(do_facet $SINGLEAGT $MMAP_CAT $file)
@@ -702,13 +737,16 @@ test_6() {
 run_test 6 "Test mmap write on RW-PCC "
 
 test_7a() {
-       local file=$DIR/$tdir/$tfile
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
        local content
 
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
        echo "QQQQQ" > $file
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
@@ -729,14 +767,17 @@ test_7a() {
 run_test 7a "Fake file detached between fault() and page_mkwrite() for RW-PCC"
 
 test_7b() {
-       local file=$DIR/$tdir/$tfile
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
        local content
        local pid
 
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
        echo "QQQQQ" > $file
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
@@ -747,11 +788,11 @@ test_7b() {
        # HSM released exists archived status
        check_hsm_flags $file "0x0000000d"
 
-       # multiop mmap write increase the first character of each page with 1
+       # multiop mmap write increases the first character of each page with 1
        do_facet $SINGLEAGT $MULTIOP $file OSMWUc &
        pid=$!
 
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach file $file"
 
        wait $pid || error "multiop mmap write failed"
@@ -762,13 +803,11 @@ test_7b() {
 run_test 7b "Test the race with concurrent mkwrite and detach"
 
 test_8() {
-       local file=$DIR/$tdir/$tfile
+       local file=$DIR/$tfile
 
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir -p $DIR/$tdir || error "mkdir $DIR/$tdir failed"
-
        echo "QQQQQ" > $file
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "failed to attach file $file"
@@ -785,24 +824,6 @@ test_8() {
 }
 run_test 8 "Test fake -ENOSPC tolerance for RW-PCC"
 
-setup_loopdev() {
-       local facet=$1
-       local file=$2
-       local mntpt=$3
-       local size=${4:-50}
-
-       do_facet $facet mkdir -p $mntpt || error "mkdir -p $hsm_root failed"
-       stack_trap "do_facet $facet rm -rf $mntpt" EXIT
-       do_facet $facet dd if=/dev/zero of=$file bs=1M count=$size
-       stack_trap "do_facet $facet rm -f $file" EXIT
-       do_facet $facet mkfs.ext4 $file ||
-               error "mkfs.ext4 $file failed"
-       do_facet $facet file $file
-       do_facet $facet mount -t ext4 -o loop,usrquota,grpquota $file $mntpt ||
-               error "mount -o loop,usrquota,grpquota $file $mntpt failed"
-       stack_trap "do_facet $facet $UMOUNT $mntpt" EXIT
-}
-
 test_9() {
        local loopfile="$TMP/$tfile"
        local mntpt="/mnt/pcc.9a"
@@ -873,7 +894,7 @@ test_usrgrp_quota() {
                error "dd write $file1 failed"
        # -EDQUOT error should be tolerated via fallback to normal Lustre path.
        check_lpcc_state $file1 "none"
-       do_facet $SINGLEAGT $LFS pcc detach $file1 ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file1 ||
                error "failed to detach file $file"
        rm $file1 $file2
 }
@@ -889,16 +910,17 @@ test_10b() {
 run_test 10b "Test RW-PCC with group quota on loop PCC device"
 
 test_11() {
-       local file=$DIR/$tdir/$tfile
-       local hsm_root=$(hsm_root)
-       local file=$DIR/$tdir/$tfile
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
        local -a lpcc_path
        local lpcc_dir
 
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
-       mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
        do_facet $SINGLEAGT "echo -n QQQQQ > $file"
        lpcc_path=$(lpcc_fid2path $hsm_root $file)
        lpcc_dir=$(dirname $lpcc_path)
@@ -909,7 +931,7 @@ test_11() {
                error "failed to attach $file"
        check_lpcc_state $file "readwrite"
        check_file_data $SINGLEAGT $file "QQQQQ"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach $file"
        rm $file || error "rm $file failed"
 
@@ -941,15 +963,19 @@ run_test 11 "Test attach fault injection with simulated PCC file path"
 
 test_12() {
        local file=$DIR/$tfile
+       local hsm_root=$(hsm_root)
+       local -a lpcc_path
        local pid
 
        copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
        setup_pcc_mapping
 
        echo  -n race_rw_attach_hsmremove > $file
+       lpcc_path=$(lpcc_fid2path $hsm_root $file)
        do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER $file ||
                error "attach $file failed"
-       do_facet $SINGLEAGT $LFS pcc detach $file || error "detach $file failed"
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
+               error "detach $file failed"
        # HSM released exists archived status
        check_hsm_flags $file "0x0000000d"
        # define OBD_FAIL_LLITE_PCC_ATTACH_PAUSE        0x1414
@@ -961,6 +987,8 @@ test_12() {
        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"
+       do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
+               error "RW-PCC cached file '$lpcc_path' should be removed"
 
        return 0
 }
@@ -992,11 +1020,9 @@ test_rule_id() {
                error "failed to dd write from $file"
        check_lpcc_state $file "readwrite"
 
-       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
-
-       cleanup_pcc_mapping
 }
 
 test_13a() {
@@ -1021,7 +1047,7 @@ test_13b() {
        do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
        rm $file || error "rm $file failed"
@@ -1030,7 +1056,7 @@ test_13b() {
        do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
        rm $file || error "rm $file failed"
@@ -1039,7 +1065,7 @@ test_13b() {
        do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
                error "failed to detach file $file"
        check_lpcc_state $file "none"
        rm $file || error "rm $file failed"
@@ -1049,8 +1075,6 @@ test_13b() {
                error "failed to dd write to $file"
        check_lpcc_state $file "none"
        rm $file || error "rm $file failed"
-
-       cleanup_pcc_mapping
 }
 run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
 
@@ -1086,7 +1110,7 @@ test_13c() {
        do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach $file"
        rm $file || error "rm $file failed"
 
@@ -1100,7 +1124,7 @@ test_13c() {
        do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
                error "failed to dd write to $file"
        check_lpcc_state $file "readwrite"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach $file"
        rm $file || error "rm $file failed"
 
@@ -1109,14 +1133,146 @@ test_13c() {
        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"
-       do_facet $SINGLEAGT $LFS pcc detach $file ||
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
                error "failed to detach $file"
        rm $file || error "rm $file failed"
-
-       cleanup_pcc_mapping
 }
 run_test 13c "Check auto RW-PCC create caching for UID/GID/ProjID/fname rule"
 
+test_14() {
+       local file=$DIR/$tdir/$tfile
+
+       copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
+       setup_pcc_mapping
+
+       mkdir -p $DIR/$tdir || error "mkdir -p $DIR/$tdir failed"
+       do_facet $SINGLEAGT "echo -n autodetach_data > $file"
+       do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "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 "autodetach_data"
+       check_lpcc_state $file "none"
+}
+run_test 14 "Revocation of the layout lock should detach the file automatically"
+
+test_15() {
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tdir/$tfile
+
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
+       copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
+       setup_pcc_mapping $SINGLEAGT \
+               "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1"
+
+       mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
+       chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
+
+       echo "Check open attach for non-root user"
+       do_facet $SINGLEAGT $RUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
+               error "failed to dd write to $file"
+       do_facet $SINGLEAGT $RUNAS $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "failed to attach file $file"
+       do_facet $SINGLEAGT $RUNAS $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 $RUNAS $LFS pcc detach -k $file ||
+               error "PCC detach $file failed"
+       check_lpcc_state $file "readwrite" $SINGLEAGT "$RUNAS"
+       do_facet $SINGLEAGT $RUNAS $LFS pcc detach $file ||
+               error "PCC detach $file failed"
+       rm $file || error "rm $file failed"
+
+       echo "check open attach for root user"
+       do_facet $SINGLEAGT "echo -n autoattach_data > $file"
+       do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "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"
+       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 "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"
+
+       # 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 15 "Test auto attach at open when file is still valid cached"
+
+test_16() {
+       local loopfile="$TMP/$tfile"
+       local mntpt="/mnt/pcc.$tdir"
+       local hsm_root="$mntpt/$tdir"
+       local file=$DIR/$tfile
+       local -a lpcc_path
+
+       setup_loopdev $SINGLEAGT $loopfile $mntpt 50
+       copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
+       setup_pcc_mapping $SINGLEAGT \
+               "projid={100}\ rwid=$HSM_ARCHIVE_NUMBER\ open_attach=1"
+
+       do_facet $SINGLEAGT "echo -n detach_data > $file"
+       lpcc_path=$(lpcc_fid2path $hsm_root $file)
+       do_facet $SINGLEAGT $LFS pcc attach -i $HSM_ARCHIVE_NUMBER \
+               $file || error "PCC attach $file failed"
+       check_lpcc_state $file "readwrite"
+       # HSM released exists archived status
+       check_hsm_flags $file "0x0000000d"
+
+       echo "Test for reusing valid PCC cache"
+       # Valid PCC cache can be reused
+       do_facet $SINGLEAGT $LFS pcc detach -k $file ||
+               error "PCC detach $file failed"
+       check_lpcc_state $file "readwrite"
+       # HSM released exists archived status
+       check_hsm_flags $file "0x0000000d"
+
+       echo "Test for the default detach"
+       # Permanent detach by default, it will remove the PCC copy
+       do_facet $SINGLEAGT $LFS pcc detach $file ||
+               error "PCC detach $file failed"
+       wait_request_state $(path2fid $file) REMOVE SUCCEED
+       check_lpcc_state $file "none"
+       # File is removed from PCC backend
+       check_hsm_flags $file "0x00000000"
+       do_facet $SINGLEAGT "[ -f $lpcc_path ]" &&
+               error "RW-PCC cached file '$lpcc_path' should be removed"
+
+       return 0
+}
+run_test 16 "Test detach with different options"
+
 complete $SECONDS
 check_and_cleanup_lustre
 exit_status