}
run_test 26 "Remove the archive of a valid file"
+cleanup_test_26a() {
+ trap 0
+ set_hsm_param remove_archive_on_last_unlink 0
+ set_hsm_param loop_period $orig_loop_period
+ set_hsm_param grace_delay $orig_grace_delay
+ copytool_cleanup
+}
+
+test_26a() {
+ local raolu=$(get_hsm_param remove_archive_on_last_unlink)
+ [[ $raolu -eq 0 ]] || error "RAoLU policy should be off"
+
+ # test needs a running copytool
+ copytool_setup
+
+ mkdir -p $DIR/$tdir
+ local f=$DIR/$tdir/$tfile
+ local fid=$(copy_file /etc/passwd $f)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
+ wait_request_state $fid ARCHIVE SUCCEED
+
+ rm -f $f
+
+ local f2=$DIR/$tdir/${tfile}_2
+ local fid2=$(copy_file /etc/passwd $f2)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
+ wait_request_state $fid2 ARCHIVE SUCCEED
+
+ cat $f2 > /dev/null
+
+ local f3=$DIR/$tdir/${tfile}_3
+ local fid3=$(copy_file /etc/passwd $f3)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f3
+ wait_request_state $fid3 ARCHIVE SUCCEED
+
+ local f4=$DIR/$tdir/${tfile}_4
+ local fid4=$(copy_file /etc/passwd $f4)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f4
+ wait_request_state $fid4 ARCHIVE SUCCEED
+
+ trap cleanup_test_26a EXIT
+
+ # set a long grace_delay vs short loop_period
+ local orig_loop_period=$(get_hsm_param loop_period)
+ local orig_grace_delay=$(get_hsm_param grace_delay)
+ set_hsm_param loop_period 10
+ set_hsm_param grace_delay 100
+
+ set_hsm_param remove_archive_on_last_unlink 1
+
+ rm -f $f3
+ cat $f4 > /dev/null
+ local f4bis=$DIR/$tdir/${tfile}_4bis
+ ln $f4 $f4bis
+ [[ $? -eq 0 ]] || error "Unable to create hard-link"
+ rm -f $f4
+
+ # Since CDT is not signaled for RAoLU requests to be sure it
+ # will wake-up to send remove request and copytool will process
+ # it, wait for loop_period + some extra-time.
+ local loop_period=$(get_hsm_param loop_period)
+ sleep $((loop_period + 5))
+
+ set_hsm_param remove_archive_on_last_unlink 0
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid
+ [[ $? -eq 0 ]] || error "File being removed on archive"
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid2
+ [[ $? -eq 0 ]] || error "File being removed on archive"
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid3
+ [[ $? -eq 0 ]] && error "File not being removed on archive"
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid4
+ [[ $? -eq 0 ]] || error "File being removed on archive"
+
+ # previous actions elapsed time should be < grace_delay
+ wait_request_state $fid3 REMOVE SUCCEED
+
+ cleanup_test_26a
+}
+run_test 26a "Remove Archive On Last Unlink (RAoLU) policy"
+
+cleanup_test_26b() {
+ trap 0
+ set_hsm_param remove_archive_on_last_unlink 0
+ copytool_cleanup
+}
+
+test_26b() {
+
+ # test needs a running copytool
+ copytool_setup
+
+ mkdir -p $DIR/$tdir
+ local f=$DIR/$tdir/$tfile
+ local fid=$(copy_file /etc/passwd $f)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
+ wait_request_state $fid ARCHIVE SUCCEED
+
+ trap cleanup_test_26b EXIT
+
+ set_hsm_param remove_archive_on_last_unlink 1
+
+ cdt_shutdown
+ cdt_check_state stopped
+
+ rm -f $f
+
+ set_hsm_param remove_archive_on_last_unlink 0
+
+ wait_request_state $fid REMOVE WAITING
+
+ cdt_enable
+ # copytool must re-register
+ search_and_kill_copytool
+ sleep 5
+ search_copytools && error "Copytool should have stopped"
+ HSM_ARCHIVE_PURGE=false copytool_setup
+
+ wait_request_state $fid REMOVE SUCCEED
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid
+ [[ $? -eq 0 ]] && error "File not being removed on archive"
+
+ cleanup_test_26b
+}
+run_test 26b "RAoLU policy when CDT off"
+
+cleanup_test_26c() {
+ trap 0
+ set_hsm_param remove_archive_on_last_unlink 0
+ set_hsm_param loop_period $orig_loop_period
+ set_hsm_param grace_delay $orig_grace_delay
+ copytool_cleanup
+}
+
+test_26c() {
+
+ # test needs a running copytool
+ copytool_setup
+
+ mkdir -p $DIR/$tdir
+ local f=$DIR/$tdir/$tfile
+ local fid=$(copy_file /etc/passwd $f)
+
+ $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
+ wait_request_state $fid ARCHIVE SUCCEED
+
+ trap cleanup_test_26c EXIT
+
+ # set a long grace_delay vs short loop_period
+ local orig_loop_period=$(get_hsm_param loop_period)
+ local orig_grace_delay=$(get_hsm_param grace_delay)
+ set_hsm_param loop_period 10
+ set_hsm_param grace_delay 100
+
+ set_hsm_param remove_archive_on_last_unlink 1
+
+ multiop_bg_pause $f O_c || error "open $f failed"
+ local pid=$!
+ # give multiop a chance to open
+ sleep 2
+
+ rm -f $f
+
+ # Since CDT is not signaled for RAoLU requests to be sure it
+ # will wake-up to send remove request and copytool will process
+ # it, wait for loop_period + some extra-time.
+ local loop_period=$(get_hsm_param loop_period)
+ sleep $((loop_period + 5))
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid
+ [[ $? -eq 0 ]] || error "File being removed on archive"
+
+ kill -USR1 $pid || error "multiop early exit"
+ # should reach autotest timeout if multiop fails to trap
+ # signal, close file, and exit ...
+ wait $pid || error
+
+ # again, wait for loop_period + some extra-time, to allow
+ # CDT enough time to handle remove request.
+ sleep $((loop_period + 5))
+
+ set_hsm_param remove_archive_on_last_unlink 0
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid
+ [[ $? -eq 0 ]] && error "File not being removed on archive"
+
+ # previous actions elapsed time should be < grace_delay
+ wait_request_state $fid REMOVE SUCCEED
+
+ cleanup_test_26c
+}
+run_test 26c "RAoLU effective when file closed"
+
+cleanup_test_26d() {
+ trap 0
+ set_hsm_param remove_archive_on_last_unlink 0
+ set_hsm_param loop_period $orig_loop_period
+ set_hsm_param grace_delay $orig_grace_delay
+ copytool_cleanup
+}
+
+test_26d() {
+
+ # test needs a running copytool
+ copytool_setup
+
+ mkdir -p $DIR/$tdir
+ local f=$DIR/$tdir/$tfile
+ local fid=$(copy_file /etc/motd $f 1)
+
+ $LFS hsm_archive $f || error "could not archive file"
+ wait_request_state $fid ARCHIVE SUCCEED
+ check_hsm_flags $f "0x00000009"
+
+ trap cleanup_test_26d EXIT
+
+ # set a long grace_delay vs short loop_period
+ local orig_loop_period=$(get_hsm_param loop_period)
+ local orig_grace_delay=$(get_hsm_param grace_delay)
+ set_hsm_param loop_period 10
+ set_hsm_param grace_delay 100
+
+ set_hsm_param remove_archive_on_last_unlink 1
+
+ multiop_bg_pause $f O_c || error "multiop failed"
+ local MULTIPID=$!
+ # give multiop a chance to open
+ sleep 2
+
+ rm -f $f
+
+ mds_evict_client
+
+ # Since CDT is not signaled for RAoLU requests to be sure it
+ # will wake-up to send remove request and copytool will process
+ # it, wait for loop_period + some extra-time.
+ local loop_period=$(get_hsm_param loop_period)
+ sleep $((loop_period + 5))
+
+ set_hsm_param remove_archive_on_last_unlink 0
+
+ do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid
+ [[ $? -eq 0 ]] && error "File not being removed on archive"
+
+ # previous actions elapsed time should be < grace_delay
+ wait_request_state $fid REMOVE SUCCEED
+
+ client_up || client_up || true
+
+ kill -USR1 $MULTIPID
+ wait $MULTIPID || error "multiop close failed"
+
+ cleanup_test_26d
+}
+run_test 26d "RAoLU when Client eviction"
+
test_27a() {
# test needs a running copytool
copytool_setup