+test_rule_id() {
+ local idstr="${1}id"
+ local rule="${idstr}={$2}"
+ local myRUNAS="$3"
+ local file=$DIR/$tdir/$tfile
+
+ setup_pcc_mapping $SINGLEAGT "$rule\ 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"
+
+ rm -f $file || error "rm $file failed"
+ 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 $myRUNAS dd if=$file of=/dev/null bs=1024 count=1 ||
+ error "failed to dd read from $file"
+ do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 256 ||
+ error "failed to truncate $file"
+ do_facet $SINGLEAGT $myRUNAS $TRUNCATE $file 2048 ||
+ error "failed to truncate $file"
+ do_facet $SINGLEAGT $myRUNAS dd if=/dev/zero of=$file bs=1024 count=1 ||
+ error "failed to dd write from $file"
+ check_lpcc_state $file "readwrite"
+
+ do_facet $SINGLEAGT $myRUNAS $LFS pcc detach -k $file ||
+ error "failed to detach file $file"
+ check_lpcc_state $file "none"
+}
+
+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"
+}
+run_test 13a "Test auto RW-PCC create caching for UID/GID rule"
+
+test_13b() {
+ local file
+
+ copytool setup -m "$MOUNT" -a "$HSM_ARCHIVE_NUMBER"
+ setup_pcc_mapping $SINGLEAGT \
+ "fname={*.h5\ suffix.*\ Mid*dle}\ 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"
+
+ file=$DIR/$tdir/prefix.h5
+ 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 -k $file ||
+ error "failed to detach file $file"
+ check_lpcc_state $file "none"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/suffix.doc
+ 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 -k $file ||
+ error "failed to detach file $file"
+ check_lpcc_state $file "none"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/MidPADdle
+ 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 -k $file ||
+ error "failed to detach file $file"
+ check_lpcc_state $file "none"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/Midpad
+ 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 "none"
+ rm $file || error "rm $file failed"
+}
+run_test 13b "Test auto RW-PCC create caching for file name with wildcard"
+
+test_13c() {
+ local file
+ local myRUNAS
+
+ ! is_project_quota_supported &&
+ echo "Skip project quota is not supported" && return 0
+
+ enable_project_quota
+ 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"
+ $LCTL pcc list $MOUNT
+ do_facet $SINGLEAGT mkdir -p $DIR/$tdir
+ chmod 777 $DIR/$tdir || error "chmod 0777 $DIR/$tdir failed"
+
+ mkdir -p $DIR/$tdir/proj || error "mkdir $DIR/$tdir/proj failed"
+ mkdir -p $DIR/$tdir/proj2 || error "mkdir $DIR/$tdir/proj2 failed"
+ $LFS project -sp 100 $DIR/$tdir/proj ||
+ error "failed to set project for $DIR/$tdir/proj"
+ $LFS project -sp 200 $DIR/$tdir/proj2 ||
+ error "failed to set project for $DIR/$tdir/proj2"
+
+ file=$DIR/$tdir/proj/notcache
+ do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
+ error "failed to dd write to $file"
+ check_lpcc_state $file "none"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/proj/autocache.h5
+ 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 -k $file ||
+ error "failed to detach $file"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/proj2/notcache
+ do_facet $SINGLEAGT dd if=/dev/zero of=$file bs=1024 count=1 ||
+ error "failed to dd write to $file"
+ check_lpcc_state $file "none"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/proj2/autocache.h5
+ 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 -k $file ||
+ error "failed to detach $file"
+ rm $file || error "rm $file failed"
+
+ file=$DIR/$tdir/ugidcache
+ myRUNAS="runas -u 500 -g 1000"
+ 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 -k $file ||
+ error "failed to detach $file"
+ rm $file || error "rm $file failed"
+}
+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"
+