Whamcloud - gitweb
Revert "LU-6867 test: detect active facet based on current state"
[fs/lustre-release.git] / lustre / tests / sanity-hsm.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # exit on error
7 set -e
8 set +o monitor
9
10 SRCDIR=$(dirname $0)
11 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin
12
13 ONLY=${ONLY:-"$*"}
14 # bug number for skipped test:
15 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT"
16 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
17
18 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
19
20 . $LUSTRE/tests/test-framework.sh
21 init_test_env $@
22 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
23 init_logging
24
25 MULTIOP=${MULTIOP:-multiop}
26 OPENFILE=${OPENFILE:-openfile}
27 MMAP_CAT=${MMAP_CAT:-mmap_cat}
28 MOUNT_2=${MOUNT_2:-"yes"}
29 FAIL_ON_ERROR=false
30
31 # script only handles up to 10 MDTs (because of MDT_PREFIX)
32 [ $MDSCOUNT -gt 9 ] &&
33         error "script cannot handle more than 9 MDTs, please fix" && exit
34
35 check_and_setup_lustre
36
37 if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ]]; then
38         skip_env "Need MDS version at least 2.4.53" && exit
39 fi
40
41 # $RUNAS_ID may get set incorrectly somewhere else
42 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
43         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!" && exit
44 fi
45 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
46 if getent group nobody; then
47         GROUP=nobody
48 elif getent group nogroup; then
49         GROUP=nogroup
50 else
51         error "No generic nobody group"
52 fi
53
54 build_test_filter
55
56 # if there is no CLIENT1 defined, some tests can be ran on localhost
57 CLIENT1=${CLIENT1:-$HOSTNAME}
58 # if CLIENT2 doesn't exist then use CLIENT1 instead
59 # All tests should use CLIENT2 with MOUNT2 only therefore it will work if
60 # $CLIENT2 == CLIENT1
61 # Exception is the test which need two separate nodes
62 CLIENT2=${CLIENT2:-$CLIENT1}
63
64 #
65 # In order to test multiple remote HSM agents, a new facet type named "AGT" and
66 # the following associated variables are added:
67 #
68 # AGTCOUNT: number of agents
69 # AGTDEV{N}: target HSM mount point (root path of the backend)
70 # agt{N}_HOST: hostname of the agent agt{N}
71 # SINGLEAGT: facet of the single agent
72 #
73 # The number of agents is initialized as the number of remote client nodes.
74 # By default, only single copytool is started on a remote client/agent. If there
75 # was no remote client, then the copytool will be started on the local client.
76 #
77 init_agt_vars() {
78         local n
79         local agent
80
81         export AGTCOUNT=${AGTCOUNT:-$((CLIENTCOUNT - 1))}
82         [[ $AGTCOUNT -gt 0 ]] || AGTCOUNT=1
83
84         export SHARED_DIRECTORY=${SHARED_DIRECTORY:-$TMP}
85         if [[ $CLIENTCOUNT -gt 1 ]] &&
86                 ! check_shared_dir $SHARED_DIRECTORY $CLIENTS; then
87                 skip_env "SHARED_DIRECTORY should be accessible"\
88                          "on all client nodes"
89                 exit 0
90         fi
91
92         # We used to put the HSM archive in $SHARED_DIRECTORY but that
93         # meant NFS issues could hose sanity-hsm sessions. So now we
94         # use $TMP instead.
95         for n in $(seq $AGTCOUNT); do
96                 eval export AGTDEV$n=\$\{AGTDEV$n:-"$TMP/arc$n"\}
97                 agent=CLIENT$((n + 1))
98                 if [[ -z "${!agent}" ]]; then
99                         [[ $CLIENTCOUNT -eq 1 ]] && agent=CLIENT1 ||
100                                 agent=CLIENT2
101                 fi
102                 eval export agt${n}_HOST=\$\{agt${n}_HOST:-${!agent}\}
103         done
104
105         export SINGLEAGT=${SINGLEAGT:-agt1}
106
107         export HSMTOOL=${HSMTOOL:-"lhsmtool_posix"}
108         export HSMTOOL_VERBOSE=${HSMTOOL_VERBOSE:-""}
109         export HSMTOOL_UPDATE_INTERVAL=${HSMTOOL_UPDATE_INTERVAL:=""}
110         export HSMTOOL_EVENT_FIFO=${HSMTOOL_EVENT_FIFO:=""}
111         export HSMTOOL_TESTDIR
112         export HSMTOOL_BASE=$(basename "$HSMTOOL" | cut -f1 -d" ")
113         # $hsm_root/$HSMTMP Makes $hsm_root dir path less generic to ensure
114         # rm -rf $hsm_root/* is safe even if $hsm_root becomes unset to avoid
115         # deleting everything in filesystem, independent of any copytool.
116         export HSMTMP=${HSMTMP:-"shsm"}
117
118         HSM_ARCHIVE=$(copytool_device $SINGLEAGT)
119
120         [ -z "${HSM_ARCHIVE// /}" ] && error "HSM_ARCHIVE is empty!"
121         HSM_ARCHIVE=$HSM_ARCHIVE/$HSMTMP
122
123         HSM_ARCHIVE_NUMBER=2
124
125         # The test only support up to 10 MDTs
126         MDT_PREFIX="mdt.$FSNAME-MDT000"
127         HSM_PARAM="${MDT_PREFIX}0.hsm"
128
129         # archive is purged at copytool setup
130         HSM_ARCHIVE_PURGE=true
131
132         # Don't allow copytool error upon start/setup
133         HSMTOOL_NOERROR=false
134 }
135
136 # Get the backend root path for the given agent facet.
137 copytool_device() {
138         local facet=$1
139         local dev=AGTDEV$(facet_number $facet)
140
141         echo -n ${!dev}
142 }
143
144 # Stop copytool and unregister an existing changelog user.
145 cleanup() {
146         copytool_monitor_cleanup
147         copytool_cleanup
148         changelog_cleanup
149         cdt_set_sanity_policy
150 }
151
152 get_mdt_devices() {
153         local mdtno
154         # get MDT device for each mdc
155         for mdtno in $(seq 1 $MDSCOUNT); do
156                 local idx=$(($mdtno - 1))
157                 MDT[$idx]=$($LCTL get_param -n \
158                         mdc.$FSNAME-MDT000${idx}-mdc-*.mds_server_uuid |
159                         awk '{gsub(/_UUID/,""); print $1}' | head -n1)
160         done
161 }
162
163 search_copytools() {
164         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
165         do_nodesv $hosts "pgrep -x $HSMTOOL_BASE"
166 }
167
168 kill_copytools() {
169         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
170
171         echo "Killing existing copytools on $hosts"
172         do_nodesv $hosts "killall -q $HSMTOOL_BASE" || true
173 }
174
175 wait_copytools() {
176         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
177         local wait_timeout=200
178         local wait_start=$SECONDS
179         local wait_end=$((wait_start + wait_timeout))
180         local sleep_time=100000 # 0.1 second
181
182         while ((SECONDS < wait_end)); do
183                 if ! search_copytools $hosts; then
184                         echo "copytools stopped in $((SECONDS - wait_start))s"
185                         return 0
186                 fi
187
188                 echo "copytools still running on $hosts"
189                 usleep $sleep_time
190                 [ $sleep_time -lt 32000000 ] && # 3.2 seconds
191                         sleep_time=$(bc <<< "$sleep_time * 2")
192         done
193
194         # try to dump Copytool's stack
195         do_nodesv $hosts "echo 1 >/proc/sys/kernel/sysrq ; " \
196                          "echo t >/proc/sysrq-trigger"
197
198         echo "copytools failed to stop in ${wait_timeout}s"
199
200         return 1
201 }
202
203 copytool_monitor_setup() {
204         local facet=${1:-$SINGLEAGT}
205         local agent=$(facet_active_host $facet)
206
207         local cmd="mktemp --tmpdir=/tmp -d ${TESTSUITE}.${TESTNAME}.XXXX"
208         local test_dir=$(do_node $agent "$cmd") ||
209                 error "Failed to create tempdir on $agent"
210         export HSMTOOL_MONITOR_DIR=$test_dir
211
212         # Create the fifo and a monitor (cat dies when copytool dies)
213         do_node $agent "mkfifo -m 0644 $test_dir/fifo" ||
214                 error "failed to create copytool fifo on $agent"
215         cmd="cat $test_dir/fifo > $test_dir/events &"
216         cmd+=" echo \\\$! > $test_dir/monitor_pid"
217
218         if [[ $PDSH == *Rmrsh* ]]; then
219                 # This is required for pdsh -Rmrsh and its handling of remote
220                 # shells.
221                 # Regular ssh and pdsh -Rssh work fine without this
222                 # backgrounded subshell nonsense.
223                 (do_node $agent "$cmd") &
224                 export HSMTOOL_MONITOR_PDSH=$!
225
226                 # Slightly racy, but just making a best-effort to catch obvious
227                 # problems.
228                 sleep 1
229                 ps -p $HSMTOOL_MONITOR_PDSH > /dev/null ||
230                         error "Failed to start copytool monitor on $agent"
231         else
232                 do_node $agent "$cmd"
233                 if [ $? != 0 ]; then
234                         error "Failed to start copytool monitor on $agent"
235                 fi
236         fi
237 }
238
239 copytool_monitor_cleanup() {
240         local facet=${1:-$SINGLEAGT}
241         local agent=$(facet_active_host $facet)
242
243         if [ -n "$HSMTOOL_MONITOR_DIR" ]; then
244                 # Should die when the copytool dies, but just in case.
245                 local cmd="kill \\\$(cat $HSMTOOL_MONITOR_DIR/monitor_pid)"
246                 cmd+=" 2>/dev/null || true"
247                 do_node $agent "$cmd"
248                 do_node $agent "rm -fr $HSMTOOL_MONITOR_DIR"
249                 export HSMTOOL_MONITOR_DIR=
250         fi
251
252         # The pdsh should die on its own when the monitor dies. Just
253         # in case, though, try to clean up to avoid any cruft.
254         if [ -n "$HSMTOOL_MONITOR_PDSH" ]; then
255                 kill $HSMTOOL_MONITOR_PDSH 2>/dev/null || true
256                 export HSMTOOL_MONITOR_PDSH=
257         fi
258 }
259
260 copytool_logfile()
261 {
262         local host="$(facet_host "$1")"
263         local prefix=$TESTLOG_PREFIX
264         [ -n "$TESTNAME" ] && prefix+=.$TESTNAME
265
266         printf "${prefix}.copytool${archive_id}_log.${host}.log"
267 }
268
269 __lhsmtool_setup()
270 {
271         local cmd="$HSMTOOL $HSMTOOL_VERBOSE --daemon --hsm-root \"$hsm_root\""
272         [ -n "$bandwidth" ] && cmd+=" --bandwidth $bandwidth"
273         [ -n "$archive_id" ] && cmd+=" --archive $archive_id"
274         [ ${#misc_options[@]} -gt 0 ] &&
275                 cmd+=" $(IFS=" " echo "$@")"
276         cmd+=" \"$mountpoint\""
277
278         echo "Starting copytool $facet on $(facet_host $facet)"
279         stack_trap "do_facet $facet \"pkill -x $HSMTOOL_BASE\" || true" EXIT
280         do_facet $facet "$cmd < /dev/null > \"$(copytool_logfile $facet)\" 2>&1"
281 }
282
283 hsm_root() {
284         local facet="${1:-$SINGLEAGT}"
285
286         printf "$(copytool_device "$facet")/${TESTSUITE}.${TESTNAME}/"
287 }
288
289 copytool()
290 {
291         local action=$1
292         shift
293
294         # Parse arguments
295         local fail_on_error=true
296         local -a misc_options
297         while [ $# -gt 0 ]; do
298                 case "$1" in
299                 -f|--facet)
300                         shift
301                         local facet="$1"
302                         ;;
303                 -m|--mountpoint)
304                         shift
305                         local mountpoint="$1"
306                         ;;
307                 -a|--archive-id)
308                         shift
309                         local archive_id="$1"
310                         ;;
311                 -b|--bwlimit)
312                         shift
313                         local bandwidth="$1" # in MB/s
314                         ;;
315                 -n|--no-fail)
316                         local fail_on_error=false
317                         ;;
318                 *)
319                         # Uncommon(/copytool dependent) option
320                         misc_options+=("$1")
321                         ;;
322                 esac
323                 shift
324         done
325
326         # Use default values if needed
327         local facet=${facet:-$SINGLEAGT}
328         local mountpoint="${mountpoint:-${MOUNT2:-$MOUNT}}"
329         local hsm_root="$(hsm_root "$facet")"
330
331         stack_trap "do_facet $facet \"rm -rf \\\"$hsm_root\\\"\"" EXIT
332         do_facet $facet "mkdir -p \"$hsm_root\"" ||
333                 error "mkdir \"$hsm_root\" failed"
334
335         case "$HSMTOOL" in
336         lhsmtool_posix)
337                 local copytool=lhsmtool
338                 ;;
339         esac
340
341         __${copytool}_${action} "${misc_options[@]}"
342         if [ $? -ne 0 ]; then
343                 local error_msg
344
345                 case $action in
346                 setup)
347                         local host="$(facet_host $facet)"
348                         error_msg="Failed to start copytool $facet on '$host'"
349                         ;;
350                 esac
351
352                 $fail_on_error && error "$error_msg" || echo "$error_msg"
353         fi
354 }
355
356 copytool_setup() {
357         local facet=${1:-$SINGLEAGT}
358         # Use MOUNT2 by default if defined
359         local lustre_mntpnt=${2:-${MOUNT2:-$MOUNT}}
360         local arc_id=$3
361         local hsm_root=${4:-$(copytool_device $facet)}
362
363         [ -z "${hsm_root// /}" ] && error "copytool_setup: hsm_root empty!"
364
365         local agent=$(facet_active_host $facet)
366
367         if $HSM_ARCHIVE_PURGE; then
368                 echo "Purging archive on $agent"
369                 do_facet $facet "rm -rf $hsm_root/$HSMTMP/*"
370         fi
371
372         echo "Starting copytool $facet on $agent"
373         do_facet $facet "mkdir -p $hsm_root/$HSMTMP/" ||
374                         error "mkdir '$hsm_root/$HSMTMP' failed"
375         # bandwidth is limited to 1MB/s so the copy time is known and
376         # independent of hardware
377         local cmd="$HSMTOOL $HSMTOOL_VERBOSE --daemon"
378         cmd+=" --hsm-root $hsm_root/$HSMTMP"
379         [[ -z "$arc_id" ]] || cmd+=" --archive $arc_id"
380         [[ -z "$HSMTOOL_UPDATE_INTERVAL" ]] ||
381                 cmd+=" --update-interval $HSMTOOL_UPDATE_INTERVAL"
382         [[ -z "$HSMTOOL_EVENT_FIFO" ]] ||
383                 cmd+=" --event-fifo $HSMTOOL_EVENT_FIFO"
384         cmd+=" --bandwidth 1 $lustre_mntpnt"
385
386         # Redirect the standard output and error to a log file which
387         # can be uploaded to Maloo.
388         local prefix=$TESTLOG_PREFIX
389         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
390         local copytool_log=$prefix.copytool${arc_id}_log.$agent.log
391
392         stack_trap cleanup EXIT
393         do_facet $facet "$cmd < /dev/null > $copytool_log 2>&1"
394         if [[ $? !=  0 ]]; then
395                 [[ $HSMTOOL_NOERROR == true ]] ||
396                         error "start copytool $facet on $agent failed"
397                 echo "start copytool $facet on $agent failed"
398         fi
399 }
400
401 get_copytool_event_log() {
402         local facet=${1:-$SINGLEAGT}
403         local agent=$(facet_active_host $facet)
404
405         [ -z "$HSMTOOL_MONITOR_DIR" ] &&
406                 error "Can't get event log: No monitor directory!"
407
408         do_node $agent "cat $HSMTOOL_MONITOR_DIR/events" ||
409                 error "Could not collect event log from $agent"
410 }
411
412 copytool_cleanup() {
413         trap - EXIT
414         local agt_facet=$SINGLEAGT
415         local agt_hosts=${1:-$(facet_active_host $agt_facet)}
416         local hsm_root=$(copytool_device $agt_facet)
417
418         [ -z "${hsm_root// /}" ] && error "copytool_cleanup: hsm_root empty!"
419
420         local i
421         local facet
422         local param
423         local -a state
424
425         kill_copytools $agt_hosts
426         wait_copytools $agt_hosts || error "copytools failed to stop"
427
428         # Clean all CDTs orphans requests from previous tests that
429         # would otherwise need to timeout to clear.
430         for ((i = 0; i < MDSCOUNT; i++)); do
431                 facet=mds$((i + 1))
432                 param=$(printf 'mdt.%s-MDT%04x.hsm_control' $FSNAME $i)
433                 state[$i]=$(do_facet $facet "$LCTL get_param -n $param")
434
435                 # Skip already stopping or stopped CDTs.
436                 [[ "${state[$i]}" =~ ^stop ]] && continue
437
438                 do_facet $facet "$LCTL set_param $param=shutdown"
439         done
440
441         for ((i = 0; i < MDSCOUNT; i++)); do
442                 # Only check and restore CDTs that we stopped in the first loop.
443                 [[ "${state[$i]}" =~ ^stop ]] && continue
444
445                 facet=mds$((i + 1))
446                 param=$(printf 'mdt.%s-MDT%04x.hsm_control' $FSNAME $i)
447
448                 wait_result $facet "$LCTL get_param -n $param" stopped 20 ||
449                         error "$facet CDT state is not stopped"
450
451                 # Restore old CDT state.
452                 do_facet $facet "$LCTL set_param $param=${state[$i]}"
453         done
454
455         for ((i = 0; i < MDSCOUNT; i++)); do
456                 # Only check CDTs that we stopped in the first loop.
457                 [[ "${state[$i]}" =~ ^stop ]] && continue
458
459                 facet=mds$((i + 1))
460                 param=$(printf 'mdt.%s-MDT%04x.hsm_control' $FSNAME $i)
461
462                 # Check that the old CDT state was restored.
463                 wait_result $facet "$LCTL get_param -n $param" "${state[$i]}" \
464                         20 || error "$facet CDT state is not '${state[$i]}'"
465         done
466
467         if do_facet $agt_facet "df $hsm_root" >/dev/null 2>&1 ; then
468                 do_facet $agt_facet "rm -rf $hsm_root/$HSMTMP/*"
469         fi
470 }
471
472 copytool_suspend() {
473         local agents=${1:-$(facet_active_host $SINGLEAGT)}
474
475         do_nodesv $agents "pkill -STOP -x $HSMTOOL_BASE" || return 0
476         echo "Copytool is suspended on $agents"
477 }
478
479 copytool_continue() {
480         local agents=${1:-$(facet_active_host $SINGLEAGT)}
481
482         do_nodesv $agents "pkill -CONT -x $HSMTOOL_BASE" || return 0
483         echo "Copytool is continued on $agents"
484 }
485
486 copytool_remove_backend() {
487         local fid=$1
488         local be=$(do_facet $SINGLEAGT find $HSM_ARCHIVE -name $fid)
489         echo "Remove from backend: $fid = $be"
490         do_facet $SINGLEAGT rm -f $be
491 }
492
493 import_file() {
494         mkdir -p "$(dirname "$2")" ||
495                 error "cannot create directory '$(dirname "$2")'"
496
497         do_facet $SINGLEAGT \
498                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
499                 --import $1 $2 $MOUNT" ||
500                 error "import of $1 to $2 failed"
501 }
502
503 file_creation_failure() {
504         local cmd=$1
505         local file=$2
506         local err=$3
507
508         case $err in
509         28)
510                 df $MOUNT $MOUNT2 >&2
511                 error "Not enough space to create $file with $cmd"
512                 ;;
513         *)
514                 error "cannot create $file with $cmd, status=$err"
515                 ;;
516         esac
517 }
518
519 # Creates a file using dd
520 create_file() {
521         local file=$1
522         local bs=$2
523         local count=$3
524         local conv=$4
525         local source=${5:-/dev/zero}
526         local args=""
527         local err
528
529         if [ -n "$conv" ]; then
530                 args+=" conv=$conv"
531         fi
532
533         # Create the directory in case it does not exist
534         mkdir -p "$(dirname "$file")"
535         # Delete the file in case it already exist
536         rm -f "$file"
537
538         if dd if="$source" of="$file" count="$count" bs="$bs" $args; then
539                 path2fid "$file" || error "cannot get FID of '$file'"
540         else
541                 err=$?
542                 echo "cannot create file '$file'" >&2;
543                 # Let the caller decide what to do on error
544                 return $err;
545         fi
546 }
547
548 create_empty_file() {
549         create_file "${1/$DIR/$DIR2}" 1M 0 ||
550                 file_creation_failure dd "${1/$DIR/$DIR2}" $?
551 }
552
553 create_small_file() {
554         local source_file=/dev/urandom
555         local count=1
556         local bs=1M
557         local conv=${2:-fsync}
558
559         create_file "${1/$DIR/$DIR2}" $bs $count $conv $source_file ||
560                 file_creation_failure dd "${1/$DIR/$DIR2}" $?
561 }
562
563 create_small_sync_file() {
564         create_small_file "$1" sync
565 }
566
567 create_archive_file() {
568         local file="$HSM_ARCHIVE/$1"
569         local count=${2:-39}
570         local source=/dev/urandom
571
572         # Create the counterpart directory of the archive
573         do_facet "$SINGLEAGT" mkdir -p "$(dirname "$file")" ||
574                 error "cannot create archive directory '$(dirname "$file")'"
575
576         do_facet "$SINGLEAGT" dd if=$source of="$file" bs=1M count=$count ||
577                 error "cannot create archive file '$file'"
578 }
579
580 copy2archive() {
581         local file=$HSM_ARCHIVE/$2
582         do_facet $SINGLEAGT mkdir -p $(dirname $file)
583         do_facet $SINGLEAGT cp -p $1 $file || error "cannot copy $1 to $file"
584 }
585
586 mdts_set_param() {
587         local arg=$1
588         local key=$2
589         local value=$3
590         local mdtno
591         local rc=0
592         if [[ "$value" != "" ]]; then
593                 value="=$value"
594         fi
595         for mdtno in $(seq 1 $MDSCOUNT); do
596                 local idx=$(($mdtno - 1))
597                 local facet=mds${mdtno}
598                 # if $arg include -P option, run 1 set_param per MDT on the MGS
599                 # else, run set_param on each MDT
600                 [[ $arg = *"-P"* ]] && facet=mgs
601                 do_facet $facet $LCTL set_param $arg mdt.${MDT[$idx]}.$key$value
602                 [[ $? != 0 ]] && rc=1
603         done
604         return $rc
605 }
606
607 mdts_check_param() {
608         local key="$1"
609         local target="$2"
610         local timeout="$3"
611         local mdtno
612         for mdtno in $(seq 1 $MDSCOUNT); do
613                 local idx=$(($mdtno - 1))
614                 wait_result mds${mdtno} \
615                         "$LCTL get_param -n $MDT_PREFIX${idx}.$key" "$target" \
616                         $timeout ||
617                         error "$key state is not '$target' on mds${mdtno}"
618         done
619 }
620
621 changelog_setup() {
622         _CL_USERS=()
623         local mdtno
624         for mdtno in $(seq 1 $MDSCOUNT); do
625                 local idx=$(($mdtno - 1))
626                 local cl_user=$(do_facet mds${mdtno} $LCTL \
627                              --device ${MDT[$idx]} \
628                              changelog_register -n)
629                 _CL_USERS+=($cl_user)
630                 do_facet mds${mdtno} lctl set_param \
631                         mdd.${MDT[$idx]}.changelog_mask="+hsm"
632                 $LFS changelog_clear ${MDT[$idx]} $cl_user 0
633         done
634 }
635
636 changelog_cleanup() {
637         local mdtno
638         for mdtno in $(seq 1 $MDSCOUNT); do
639                 local idx=$(($mdtno - 1))
640                 [[ -z  ${_CL_USERS[$idx]} ]] && continue
641                 $LFS changelog_clear ${MDT[$idx]} ${_CL_USERS[$idx]} 0
642                 do_facet mds${mdtno} lctl --device ${MDT[$idx]} \
643                         changelog_deregister ${_CL_USERS[$idx]}
644         done
645         _CL_USERS=()
646 }
647
648 changelog_get_flags() {
649         local mdt=$1
650         local cltype=$2
651         local fid=$3
652
653         $LFS changelog $mdt | awk "/$cltype/ && /t=\[$fid\]/ {print \$5}"
654 }
655
656 get_hsm_param() {
657         local param=$1
658         local val=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.$param)
659         echo $val
660 }
661
662 set_hsm_param() {
663         local param=$1
664         local value=$2
665         local opt=$3
666         mdts_set_param "$opt -n" "hsm.$param" "$value"
667         return $?
668 }
669
670 set_test_state() {
671         local cmd=$1
672         local target=$2
673         mdts_set_param "" hsm_control "$cmd"
674         mdts_check_param hsm_control "$target" 10
675 }
676
677 cdt_set_sanity_policy() {
678         if [[ "$CDT_POLICY_HAD_CHANGED" ]]
679         then
680                 # clear all
681                 mdts_set_param "" hsm.policy "+NRA"
682                 mdts_set_param "" hsm.policy "-NBR"
683                 CDT_POLICY_HAD_CHANGED=
684         fi
685 }
686
687 cdt_set_no_retry() {
688         mdts_set_param "" hsm.policy "+NRA"
689         CDT_POLICY_HAD_CHANGED=true
690 }
691
692 cdt_clear_no_retry() {
693         mdts_set_param "" hsm.policy "-NRA"
694         CDT_POLICY_HAD_CHANGED=true
695 }
696
697 cdt_set_non_blocking_restore() {
698         mdts_set_param "" hsm.policy "+NBR"
699         CDT_POLICY_HAD_CHANGED=true
700 }
701
702 cdt_clear_non_blocking_restore() {
703         mdts_set_param "" hsm.policy "-NBR"
704         CDT_POLICY_HAD_CHANGED=true
705 }
706
707 cdt_clear_mount_state() {
708         mdts_set_param "-P -d" hsm_control ""
709 }
710
711 cdt_set_mount_state() {
712         mdts_set_param "-P" hsm_control "$1"
713         # set_param -P is asynchronous operation and could race with set_param.
714         # In such case configs could be retrieved and applied at mgc after
715         # set_param -P completion. Sleep here to avoid race with set_param.
716         # We need at least 20 seconds. 10 for mgc_requeue_thread to wake up
717         # MGC_TIMEOUT_MIN_SECONDS + MGC_TIMEOUT_RAND_CENTISEC(5 + 5)
718         # and 10 seconds to retrieve config from server.
719         sleep 20
720 }
721
722 cdt_check_state() {
723         mdts_check_param hsm_control "$1" 20
724 }
725
726 cdt_disable() {
727         set_test_state disabled disabled
728 }
729
730 cdt_enable() {
731         set_test_state enabled enabled
732 }
733
734 cdt_shutdown() {
735         set_test_state shutdown stopped
736 }
737
738 cdt_purge() {
739         set_test_state purge enabled
740 }
741
742 cdt_restart() {
743         cdt_shutdown
744         cdt_enable
745         cdt_set_sanity_policy
746 }
747
748 needclients() {
749         local client_count=$1
750         if [[ $CLIENTCOUNT -lt $client_count ]]; then
751                 skip "Need $client_count or more clients, have $CLIENTCOUNT"
752                 return 1
753         fi
754         return 0
755 }
756
757 path2fid() {
758         $LFS path2fid $1 | tr -d '[]'
759         return ${PIPESTATUS[0]}
760 }
761
762 get_hsm_flags() {
763         local f=$1
764         local u=$2
765         local st
766
767         if [[ $u == "user" ]]; then
768                 st=$($RUNAS $LFS hsm_state $f)
769         else
770                 u=root
771                 st=$($LFS hsm_state $f)
772         fi
773
774         [[ $? == 0 ]] || error "$LFS hsm_state $f failed (run as $u)"
775
776         st=$(echo $st | cut -f 2 -d" " | tr -d "()," )
777         echo $st
778 }
779
780 get_hsm_archive_id() {
781         local f=$1
782         local st
783         st=$($LFS hsm_state $f)
784         [[ $? == 0 ]] || error "$LFS hsm_state $f failed"
785
786         local ar=$(echo $st | grep -oP '(?<=archive_id:).*')
787         echo $ar
788 }
789
790 check_hsm_flags() {
791         local f=$1
792         local fl=$2
793
794         local st=$(get_hsm_flags $f)
795         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
796 }
797
798 check_hsm_flags_user() {
799         local f=$1
800         local fl=$2
801
802         local st=$(get_hsm_flags $f user)
803         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
804 }
805
806 copy_file() {
807         local f=
808
809         if [[ -d $2 ]]; then
810                 f=$2/$(basename $1)
811         else
812                 f=$2
813         fi
814
815         if [[ "$3" != 1 ]]; then
816                 f=${f/$DIR/$DIR2}
817         fi
818         rm -f $f
819         cp $1 $f || file_creation_failure cp $f $?
820
821         path2fid $f || error "cannot get fid on $f"
822 }
823
824 # Delete any file bigger than 10M under $MOUNT and wait for deletes to complete
825 #
826 # Note that this might lead to surprising behaviours such as deleting an
827 # important file for the currently running test
828 delete_large_files() {
829         printf "Deleting large files...\n" >&2
830         find $MOUNT -size +10M -delete
831         wait_delete_completed
832 }
833
834 make_custom_file_for_progress() {
835         local count=${2:-"39"}
836         local bs=$($LCTL get_param -n lov.*-clilov-*.stripesize | head -n1)
837         bs=${3:-$bs}
838
839         [[ $count -gt  0 ]] || error "Invalid file size"
840         [[ $bs -gt 0 ]] || error "Invalid stripe size"
841
842         if ! create_file "${1/$DIR/$DIR2}" $bs $count fsync; then
843                 echo "The creation of '${1/$DIR/$DIR2}' failed" >&2
844                 echo "It might be due to a lack of space in the filesystem" >&2
845                 delete_large_files >&2
846                 create_file "${1/$DIR/$DIR2}" $bs $count fsync ||
847                         file_creation_failure dd "${1/$DIR/$DIR2}" $?
848         fi
849 }
850
851 wait_result() {
852         local facet=$1
853         shift
854         wait_update --verbose $(facet_active_host $facet) "$@"
855 }
856
857 wait_request_state() {
858         local fid=$1
859         local request=$2
860         local state=$3
861         # 4th arg (mdt index) is optional
862         local mdtidx=${4:-0}
863         local mds=mds$(($mdtidx + 1))
864
865         local cmd="$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.actions"
866         cmd+=" | awk '/'$fid'.*action='$request'/ {print \\\$13}' | cut -f2 -d="
867
868         wait_result $mds "$cmd" $state 200 ||
869                 error "request on $fid is not $state on $mds"
870 }
871
872 get_request_state() {
873         local fid=$1
874         local request=$2
875
876         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
877                 "awk '/'$fid'.*action='$request'/ {print \\\$13}' | cut -f2 -d="
878 }
879
880 get_request_count() {
881         local fid=$1
882         local request=$2
883
884         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
885                 "awk -vn=0 '/'$fid'.*action='$request'/ {n++}; END {print n}'"
886 }
887
888 # Ensure the number of HSM request for a given FID is correct
889 # assert_request_count FID REQUEST_TYPE COUNT [ERROR_MSG]
890 assert_request_count() {
891         local request_count=$(get_request_count $1 $2)
892         local default_error_msg=("expected $3 '$2' request(s) for '$1', found "
893                                 "'$request_count'")
894         [ $request_count -eq $3 ] || error "${4:-"${default_error_msg[@]}"}"
895 }
896
897 wait_all_done() {
898         local timeout=$1
899         local fid=$2
900
901         local cmd="$LCTL get_param -n $HSM_PARAM.actions"
902         [[ -n $fid ]] && cmd+=" | grep '$fid'"
903         cmd+=" | egrep 'WAITING|STARTED'"
904
905         wait_result $SINGLEMDS "$cmd" "" $timeout ||
906                 error "requests did not complete"
907 }
908
909 wait_for_grace_delay() {
910         local val=$(get_hsm_param grace_delay)
911         sleep $val
912 }
913
914 wait_for_loop_period() {
915         local val=$(get_hsm_param loop_period)
916         sleep $val
917 }
918
919 parse_json_event() {
920         local raw_event=$1
921
922         # python2.6 in EL6 includes an internal json module
923         local json_parser='import json; import fileinput;'
924         json_parser+=' print "\n".join(["local %s=\"%s\"" % tuple for tuple in '
925         json_parser+='json.loads([line for line in '
926         json_parser+='fileinput.input()][0]).items()])'
927
928         echo $raw_event | python -c "$json_parser"
929 }
930
931 get_agent_by_uuid_mdt() {
932         local uuid=$1
933         local mdtidx=$2
934         local mds=mds$(($mdtidx + 1))
935         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
936                  grep $uuid"
937 }
938
939 check_agent_registered_by_mdt() {
940         local uuid=$1
941         local mdtidx=$2
942         local mds=mds$(($mdtidx + 1))
943         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
944         if [[ ! -z "$agent" ]]; then
945                 echo "found agent $agent on $mds"
946         else
947                 error "uuid $uuid not found in agent list on $mds"
948         fi
949 }
950
951 check_agent_unregistered_by_mdt() {
952         local uuid=$1
953         local mdtidx=$2
954         local mds=mds$(($mdtidx + 1))
955         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
956         if [[ -z "$agent" ]]; then
957                 echo "uuid not found in agent list on $mds"
958         else
959                 error "uuid found in agent list on $mds: $agent"
960         fi
961 }
962
963 check_agent_registered() {
964         local uuid=$1
965         local mdsno
966         for mdsno in $(seq 1 $MDSCOUNT); do
967                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
968         done
969 }
970
971 check_agent_unregistered() {
972         local uuid=$1
973         local mdsno
974         for mdsno in $(seq 1 $MDSCOUNT); do
975                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
976         done
977 }
978
979 get_agent_uuid() {
980         local agent=${1:-$(facet_active_host $SINGLEAGT)}
981
982         # Lustre mount-point is mandatory and last parameter on
983         # copytool cmd-line.
984         local mntpnt=$(do_rpc_nodes $agent ps -C $HSMTOOL_BASE -o args= |
985                        awk '{print $NF}')
986         [ -n "$mntpnt" ] || error "Found no Agent or with no mount-point "\
987                                   "parameter"
988         do_rpc_nodes $agent get_client_uuid $mntpnt | cut -d' ' -f2
989 }
990
991 # initiate variables
992 init_agt_vars
993
994 # populate MDT device array
995 get_mdt_devices
996
997 # cleanup from previous bad setup
998 kill_copytools
999
1000 # for recovery tests, coordinator needs to be started at mount
1001 # so force it
1002 # the lustre conf must be without hsm on (like for sanity.sh)
1003 echo "Set HSM on and start"
1004 cdt_set_mount_state enabled
1005 cdt_check_state enabled
1006
1007 echo "Set sanity-hsm HSM policy"
1008 cdt_set_sanity_policy
1009
1010 # finished requests are quickly removed from list
1011 set_hsm_param grace_delay 10
1012
1013 test_1A() { # was test_1
1014         mkdir -p $DIR/$tdir
1015         chmod 777 $DIR/$tdir
1016
1017         local f=$DIR/$tdir/$tfile
1018         $RUNAS touch $f
1019
1020         # User flags
1021         check_hsm_flags_user $f "0x00000000"
1022
1023         $RUNAS $LFS hsm_set --norelease $f ||
1024                 error "user could not change hsm flags"
1025         check_hsm_flags_user $f "0x00000010"
1026
1027         $RUNAS $LFS hsm_clear --norelease $f ||
1028                 error "user could not clear hsm flags"
1029         check_hsm_flags_user $f "0x00000000"
1030
1031         # User could not change those flags...
1032         $RUNAS $LFS hsm_set --exists $f &&
1033                 error "user should not set this flag"
1034         check_hsm_flags_user $f "0x00000000"
1035
1036         # ...but root can
1037         $LFS hsm_set --exists $f ||
1038                 error "root could not change hsm flags"
1039         check_hsm_flags_user $f "0x00000001"
1040
1041         $LFS hsm_clear --exists $f ||
1042                 error "root could not clear hsm state"
1043         check_hsm_flags_user $f "0x00000000"
1044
1045 }
1046 run_test 1A "lfs hsm flags root/non-root access"
1047
1048 test_1a() {
1049         local f=$DIR/$tdir/$tfile
1050         local fid=$(create_small_file $f)
1051
1052         copytool setup
1053
1054         $LFS hsm_archive $f || error "could not archive file"
1055         wait_request_state $fid ARCHIVE SUCCEED
1056
1057         # Release and check states
1058         $LFS hsm_release $f || error "could not release file"
1059         echo -n "Verifying released state: "
1060         check_hsm_flags $f "0x0000000d"
1061
1062         $MMAP_CAT $f > /dev/null || error "failed mmap & cat release file"
1063 }
1064 run_test 1a "mmap & cat a HSM released file"
1065
1066 test_1b() {
1067         mkdir -p $DIR/$tdir
1068         $LFS setstripe -E 1M -S 1M -E 64M -c 2 -E -1 -c 4 $DIR/$tdir ||
1069                 error "failed to set default stripe"
1070         local f=$DIR/$tdir/$tfile
1071         rm -f $f
1072
1073         dd if=/dev/urandom of=$f bs=1M count=1 conv=sync ||
1074                 error "failed to create file"
1075         local fid=$(path2fid $f)
1076
1077         copytool setup
1078
1079         echo "archive $f"
1080         $LFS hsm_archive $f || error "could not archive file"
1081         wait_request_state $fid ARCHIVE SUCCEED
1082
1083         echo "release $f"
1084         $LFS hsm_release $f || error "could not release file"
1085         echo "verify released state: "
1086         check_hsm_flags $f "0x0000000d" && echo "pass"
1087
1088         echo "restore $f"
1089         $LFS hsm_restore $f || error "could not restore file"
1090         wait_request_state $fid RESTORE SUCCEED
1091         echo "verify restored state: "
1092         check_hsm_flags $f "0x00000009" && echo "pass"
1093 }
1094 run_test 1b "Archive, Release and Restore composite file"
1095
1096 test_1c() {
1097         mkdir -p $DIR/$tdir
1098         chmod 777 $DIR/$tdir
1099
1100         local f=$DIR/$tdir/$tfile
1101         $RUNAS touch $f
1102
1103         # Test whether we can set the maximum archive number.
1104         local LOCAL_HSM_ARCHIVE_NUMBER=32
1105         $LFS hsm_set --exists --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f ||
1106                 error "root could not change hsm flags"
1107         check_hsm_flags_user $f "0x00000001"
1108         echo "verifying archive number is $LOCAL_HSM_ARCHIVE_NUMBER"
1109         local st=$(get_hsm_archive_id $f)
1110         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
1111                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
1112
1113         # Test whether setting archive number 0 results in no change.
1114         $LFS hsm_set --exists --archive-id 0 $f ||
1115                 error "root could not change hsm flags"
1116         check_hsm_flags_user $f "0x00000001"
1117         echo "verifying archive number is still $LOCAL_HSM_ARCHIVE_NUMBER"
1118         st=$(get_hsm_archive_id $f)
1119         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
1120                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
1121
1122         # Test whether setting archive number > 32 results in error.
1123         $LFS hsm_set --exists --archive-id 33 $f &&
1124                 error "archive number is larger than 32"
1125         check_hsm_flags_user $f "0x00000001"
1126
1127         # Test whether setting archive number 16 and archived flag.
1128         LOCAL_HSM_ARCHIVE_NUMBER=16
1129         $LFS hsm_set --exists --archived \
1130              --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f ||
1131             error "root could not change hsm flags"
1132         check_hsm_flags_user $f "0x00000009"
1133         echo "verifying archive number is $LOCAL_HSM_ARCHIVE_NUMBER"
1134         st=$(get_hsm_archive_id $f)
1135         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
1136                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
1137 }
1138 run_test 1c "Check setting archive-id in lfs hsm_set"
1139
1140 test_1d() {
1141         mkdir -p $DIR/$tdir
1142         $LFS setstripe -E 1M -L mdt -E -1 -c 2 $DIR/$tdir ||
1143                 error "failed to set default stripe"
1144         local f=$DIR/$tdir/$tfile
1145         rm -f $f
1146
1147         dd if=/dev/urandom of=$f bs=1M count=1 conv=sync ||
1148                 error "failed to create file"
1149         local fid=$(path2fid $f)
1150
1151         copytool setup
1152
1153         echo "archive $f"
1154         $LFS hsm_archive $f || error "could not archive file"
1155         wait_request_state $fid ARCHIVE SUCCEED
1156
1157         echo "release $f"
1158         $LFS hsm_release $f || error "could not release file"
1159         echo "verify released state: "
1160         check_hsm_flags $f "0x0000000d" && echo "pass"
1161
1162         echo "restore $f"
1163         $LFS hsm_restore $f || error "could not restore file"
1164         wait_request_state $fid RESTORE SUCCEED
1165         echo "verify restored state: "
1166         check_hsm_flags $f "0x00000009" && echo "pass"
1167 }
1168 run_test 1d "Archive, Release and Restore DoM file"
1169
1170 test_2() {
1171         local f=$DIR/$tdir/$tfile
1172
1173         create_empty_file "$f"
1174         # New files are not dirty
1175         check_hsm_flags $f "0x00000000"
1176
1177         # For test, we simulate an archived file.
1178         $LFS hsm_set --exists $f || error "user could not change hsm flags"
1179         check_hsm_flags $f "0x00000001"
1180
1181         # chmod do not put the file dirty
1182         chmod 600 $f || error "could not chmod test file"
1183         check_hsm_flags $f "0x00000001"
1184
1185         # chown do not put the file dirty
1186         chown $RUNAS_ID $f || error "could not chown test file"
1187         check_hsm_flags $f "0x00000001"
1188
1189         # truncate put the file dirty
1190         $TRUNCATE $f 1 || error "could not truncate test file"
1191         check_hsm_flags $f "0x00000003"
1192
1193         $LFS hsm_clear --dirty $f || error "could not clear hsm flags"
1194         check_hsm_flags $f "0x00000001"
1195 }
1196 run_test 2 "Check file dirtyness when doing setattr"
1197
1198 test_3() {
1199         mkdir -p $DIR/$tdir
1200         f=$DIR/$tdir/$tfile
1201
1202         # New files are not dirty
1203         cp -p /etc/passwd $f
1204         check_hsm_flags $f "0x00000000"
1205
1206         # For test, we simulate an archived file.
1207         $LFS hsm_set --exists $f ||
1208                 error "user could not change hsm flags"
1209         check_hsm_flags $f "0x00000001"
1210
1211         # Reading a file, does not set dirty
1212         cat $f > /dev/null || error "could not read file"
1213         check_hsm_flags $f "0x00000001"
1214
1215         # Open for write without modifying data, does not set dirty
1216         openfile -f O_WRONLY $f || error "could not open test file"
1217         check_hsm_flags $f "0x00000001"
1218
1219         # Append to a file sets it dirty
1220         cp -p /etc/passwd $f.append || error "could not create file"
1221         $LFS hsm_set --exists $f.append ||
1222                 error "user could not change hsm flags"
1223         dd if=/etc/passwd of=$f.append bs=1 count=3\
1224            conv=notrunc oflag=append status=noxfer ||
1225                 file_creation_failure dd $f.append $?
1226         check_hsm_flags $f.append "0x00000003"
1227
1228         # Modify a file sets it dirty
1229         cp -p /etc/passwd $f.modify || error "could not create file"
1230         $LFS hsm_set --exists $f.modify ||
1231                 error "user could not change hsm flags"
1232         dd if=/dev/zero of=$f.modify bs=1 count=3\
1233            conv=notrunc status=noxfer ||
1234                 file_creation_failure dd $f.modify $?
1235         check_hsm_flags $f.modify "0x00000003"
1236
1237         # Open O_TRUNC sets dirty
1238         cp -p /etc/passwd $f.trunc || error "could not create file"
1239         $LFS hsm_set --exists $f.trunc ||
1240                 error "user could not change hsm flags"
1241         cp /etc/group $f.trunc || error "could not override a file"
1242         check_hsm_flags $f.trunc "0x00000003"
1243
1244         # Mmapped a file sets dirty
1245         cp -p /etc/passwd $f.mmap || error "could not create file"
1246         $LFS hsm_set --exists $f.mmap ||
1247                 error "user could not change hsm flags"
1248         multiop $f.mmap OSMWUc || error "could not mmap a file"
1249         check_hsm_flags $f.mmap "0x00000003"
1250 }
1251 run_test 3 "Check file dirtyness when opening for write"
1252
1253 test_4() {
1254         local f=$DIR/$tdir/$tfile
1255         local fid=$(create_small_file $f)
1256
1257         $LFS hsm_cancel $f
1258         local st=$(get_request_state $fid CANCEL)
1259         [[ -z "$st" ]] || error "hsm_cancel must not be registered (state=$st)"
1260 }
1261 run_test 4 "Useless cancel must not be registered"
1262
1263 test_8() {
1264         # test needs a running copytool
1265         copytool setup
1266
1267         mkdir -p $DIR/$tdir
1268         local f=$DIR/$tdir/$tfile
1269         local fid=$(copy_file /etc/passwd $f)
1270         $LFS hsm_archive $f
1271         wait_request_state $fid ARCHIVE SUCCEED
1272
1273         check_hsm_flags $f "0x00000009"
1274 }
1275 run_test 8 "Test default archive number"
1276
1277 test_9A() { # was test_9
1278         # we do not use the default one to be sure
1279         local archive_id=$((HSM_ARCHIVE_NUMBER + 1))
1280         copytool setup --archive-id $archive_id
1281
1282         # give time for CT to register with MDTs
1283         sleep $(($MDSCOUNT*2))
1284         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
1285         check_agent_registered $uuid
1286
1287         mkdir -p $DIR/$tdir
1288         local f=$DIR/$tdir/$tfile
1289         local fid=$(copy_file /etc/passwd $f)
1290         $LFS hsm_archive --archive $archive_id $f
1291         wait_request_state $fid ARCHIVE SUCCEED
1292
1293         check_hsm_flags $f "0x00000009"
1294 }
1295 run_test 9A "Use of explicit archive number, with dedicated copytool"
1296
1297 test_9a() {
1298         needclients 3 || return 0
1299
1300         local n
1301         local file
1302         local fid
1303
1304         # start all of the copytools
1305         for n in $(seq $AGTCOUNT); do
1306                 copytool setup --facet agt$n
1307         done
1308
1309         # archive files
1310         for n in $(seq $AGTCOUNT); do
1311                 file=$DIR/$tdir/$tfile.$n
1312                 fid=$(create_small_file $file)
1313
1314                 $LFS hsm_archive $file || error "could not archive file $file"
1315                 wait_request_state $fid ARCHIVE SUCCEED
1316                 check_hsm_flags $file "0x00000009"
1317         done
1318 }
1319 run_test 9a "Multiple remote agents"
1320
1321 test_10a() {
1322         # test needs a running copytool
1323         copytool_setup
1324
1325         mkdir -p $DIR/$tdir/d1
1326         local f=$DIR/$tdir/$tfile
1327         local fid=$(copy_file /etc/hosts $f)
1328         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
1329                 error "hsm_archive failed"
1330         wait_request_state $fid ARCHIVE SUCCEED
1331
1332         local AFILE=$(do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid) ||
1333                 error "fid $fid not in archive $HSM_ARCHIVE"
1334         echo "Verifying content"
1335         do_facet $SINGLEAGT diff $f $AFILE || error "archived file differs"
1336         echo "Verifying hsm state "
1337         check_hsm_flags $f "0x00000009"
1338
1339         echo "Verifying archive number is $HSM_ARCHIVE_NUMBER"
1340         local st=$(get_hsm_archive_id $f)
1341         [[ $st == $HSM_ARCHIVE_NUMBER ]] ||
1342                 error "Wrong archive number, $st != $HSM_ARCHIVE_NUMBER"
1343
1344         copytool_cleanup
1345 }
1346 run_test 10a "Archive a file"
1347
1348 test_10b() {
1349         # test needs a running copytool
1350         copytool setup
1351
1352         mkdir -p $DIR/$tdir
1353         local f=$DIR/$tdir/$tfile
1354         local fid=$(copy_file /etc/hosts $f)
1355         $LFS hsm_archive $f || error "archive request failed"
1356         wait_request_state $fid ARCHIVE SUCCEED
1357
1358         $LFS hsm_archive $f || error "archive of non dirty file failed"
1359         local cnt=$(get_request_count $fid ARCHIVE)
1360         [[ "$cnt" == "1" ]] ||
1361                 error "archive of non dirty file must not make a request"
1362 }
1363 run_test 10b "Archive of non dirty file must work without doing request"
1364
1365 test_10c() {
1366         # test needs a running copytool
1367         copytool setup
1368
1369         mkdir -p $DIR/$tdir
1370         local f=$DIR/$tdir/$tfile
1371         local fid=$(copy_file /etc/hosts $f)
1372         $LFS hsm_set --noarchive $f
1373         $LFS hsm_archive $f && error "archive a noarchive file must fail"
1374         return 0
1375 }
1376 run_test 10c "Check forbidden archive"
1377
1378 test_10d() {
1379         # test needs a running copytool
1380         copytool setup
1381
1382         mkdir -p $DIR/$tdir
1383         local f=$DIR/$tdir/$tfile
1384         local fid=$(copy_file /etc/hosts $f)
1385         $LFS hsm_archive $f || error "cannot archive $f"
1386         wait_request_state $fid ARCHIVE SUCCEED
1387
1388         local ar=$(get_hsm_archive_id $f)
1389         local dflt=$(get_hsm_param default_archive_id)
1390         [[ $ar == $dflt ]] ||
1391                 error "archived file is not on default archive: $ar != $dflt"
1392 }
1393 run_test 10d "Archive a file on the default archive id"
1394
1395 test_11a() {
1396         mkdir -p $DIR/$tdir
1397         copy2archive /etc/hosts $tdir/$tfile
1398         local f=$DIR/$tdir/$tfile
1399
1400         import_file $tdir/$tfile $f
1401         echo -n "Verifying released state: "
1402         check_hsm_flags $f "0x0000000d"
1403
1404         local LSZ=$(stat -c "%s" $f)
1405         local ASZ=$(do_facet $SINGLEAGT stat -c "%s" $HSM_ARCHIVE/$tdir/$tfile)
1406
1407         echo "Verifying imported size $LSZ=$ASZ"
1408         [[ $LSZ -eq $ASZ ]] || error "Incorrect size $LSZ != $ASZ"
1409         echo -n "Verifying released pattern: "
1410         local PTRN=$($GETSTRIPE -L $f)
1411         echo $PTRN
1412         [[ $PTRN == released ]] || error "Is not released"
1413         local fid=$(path2fid $f)
1414         echo "Verifying new fid $fid in archive"
1415
1416         local AFILE=$(do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid) ||
1417                 error "fid $fid not in archive $HSM_ARCHIVE"
1418 }
1419 run_test 11a "Import a file"
1420
1421 test_11b() {
1422         # test needs a running copytool
1423         copytool_setup
1424
1425         mkdir -p $DIR/$tdir
1426         local f=$DIR/$tdir/$tfile
1427         local fid=$(copy_file /etc/hosts $f)
1428         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
1429                 error "hsm_archive failed"
1430         wait_request_state $fid ARCHIVE SUCCEED
1431
1432         local FILE_HASH=$(md5sum $f)
1433         rm -f $f
1434
1435         import_file $fid $f
1436
1437         echo "$FILE_HASH" | md5sum -c
1438
1439         [[ $? -eq 0 ]] || error "Restored file differs"
1440
1441         copytool_cleanup
1442 }
1443 run_test 11b "Import a deleted file using its FID"
1444
1445 test_12a() {
1446         # test needs a running copytool
1447         copytool_setup
1448
1449         mkdir -p $DIR/$tdir
1450         copy2archive /etc/hosts $tdir/$tfile
1451
1452         local f=$DIR/$tdir/$tfile
1453         import_file $tdir/$tfile $f
1454         local f2=$DIR2/$tdir/$tfile
1455         echo "Verifying released state: "
1456         check_hsm_flags $f2 "0x0000000d"
1457
1458         local fid=$(path2fid $f2)
1459         $LFS hsm_restore $f2
1460         wait_request_state $fid RESTORE SUCCEED
1461
1462         echo "Verifying file state: "
1463         check_hsm_flags $f2 "0x00000009"
1464
1465         do_facet $SINGLEAGT diff -q $HSM_ARCHIVE/$tdir/$tfile $f
1466
1467         [[ $? -eq 0 ]] || error "Restored file differs"
1468
1469         copytool_cleanup
1470 }
1471 run_test 12a "Restore an imported file explicitly"
1472
1473 test_12b() {
1474         # test needs a running copytool
1475         copytool_setup
1476
1477         mkdir -p $DIR/$tdir
1478         copy2archive /etc/hosts $tdir/$tfile
1479
1480         local f=$DIR/$tdir/$tfile
1481         import_file $tdir/$tfile $f
1482         echo "Verifying released state: "
1483         check_hsm_flags $f "0x0000000d"
1484
1485         cat $f > /dev/null || error "File read failed"
1486
1487         echo "Verifying file state after restore: "
1488         check_hsm_flags $f "0x00000009"
1489
1490         do_facet $SINGLEAGT diff -q $HSM_ARCHIVE/$tdir/$tfile $f
1491
1492         [[ $? -eq 0 ]] || error "Restored file differs"
1493
1494         copytool_cleanup
1495 }
1496 run_test 12b "Restore an imported file implicitly"
1497
1498 test_12c() {
1499         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
1500
1501         # test needs a running copytool
1502         copytool setup
1503
1504         local f=$DIR/$tdir/$tfile
1505         mkdir -p $DIR/$tdir
1506         $LFS setstripe -c 2 "$f"
1507         local fid
1508         fid=$(make_custom_file_for_progress $f 5)
1509         [ $? != 0 ] && skip "not enough free space" && return
1510
1511         local FILE_CRC=$(md5sum $f)
1512
1513         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1514         wait_request_state $fid ARCHIVE SUCCEED
1515         $LFS hsm_release $f || error "release $f failed"
1516
1517         echo "$FILE_CRC" | md5sum -c
1518
1519         [[ $? -eq 0 ]] || error "Restored file differs"
1520 }
1521 run_test 12c "Restore a file with stripe of 2"
1522
1523 test_12d() {
1524         # test needs a running copytool
1525         copytool setup
1526
1527         mkdir -p $DIR/$tdir
1528
1529         local f=$DIR/$tdir/$tfile
1530         local fid=$(copy_file /etc/hosts $f)
1531         $LFS hsm_restore $f || error "restore of non archived file failed"
1532         local cnt=$(get_request_count $fid RESTORE)
1533         [[ "$cnt" == "0" ]] ||
1534                 error "restore non archived must not make a request"
1535         $LFS hsm_archive $f ||
1536                 error "archive request failed"
1537         wait_request_state $fid ARCHIVE SUCCEED
1538         $LFS hsm_restore $f ||
1539                 error "restore of non released file failed"
1540         local cnt=$(get_request_count $fid RESTORE)
1541         [[ "$cnt" == "0" ]] ||
1542                 error "restore a non dirty file must not make a request"
1543 }
1544 run_test 12d "Restore of a non archived, non released file must work"\
1545                 " without doing request"
1546
1547 test_12e() {
1548         # test needs a running copytool
1549         copytool setup
1550
1551         mkdir -p $DIR/$tdir $HSM_ARCHIVE/$tdir
1552         local f=$DIR/$tdir/$tfile
1553         local fid=$(copy_file /etc/hosts $f)
1554         $LFS hsm_archive $f || error "archive request failed"
1555         wait_request_state $fid ARCHIVE SUCCEED
1556
1557         # make file dirty
1558         cat /etc/hosts >> $f
1559         sync
1560         $LFS hsm_state $f
1561
1562         $LFS hsm_restore $f && error "restore a dirty file must fail"
1563         return 0
1564 }
1565 run_test 12e "Check forbidden restore"
1566
1567 test_12f() {
1568         # test needs a running copytool
1569         copytool setup
1570
1571         mkdir -p $DIR/$tdir
1572         local f=$DIR/$tdir/$tfile
1573         local fid=$(copy_file /etc/hosts $f)
1574
1575         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1576         wait_request_state $fid ARCHIVE SUCCEED
1577         $LFS hsm_release $f || error "release of $f failed"
1578         $LFS hsm_restore $f
1579         wait_request_state $fid RESTORE SUCCEED
1580
1581         echo -n "Verifying file state: "
1582         check_hsm_flags $f "0x00000009"
1583
1584         diff -q /etc/hosts $f
1585
1586         [[ $? -eq 0 ]] || error "Restored file differs"
1587 }
1588 run_test 12f "Restore a released file explicitly"
1589
1590 test_12g() {
1591         # test needs a running copytool
1592         copytool setup
1593
1594         mkdir -p $DIR/$tdir
1595         local f=$DIR/$tdir/$tfile
1596         local fid=$(copy_file /etc/hosts $f)
1597
1598         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1599         wait_request_state $fid ARCHIVE SUCCEED
1600         $LFS hsm_release $f || error "release of $f failed"
1601
1602         diff -q /etc/hosts $f
1603         local st=$?
1604
1605         # we check we had a restore done
1606         wait_request_state $fid RESTORE SUCCEED
1607
1608         [[ $st -eq 0 ]] || error "Restored file differs"
1609 }
1610 run_test 12g "Restore a released file implicitly"
1611
1612 test_12h() {
1613         needclients 2 || return 0
1614
1615         # test needs a running copytool
1616         copytool setup
1617
1618         mkdir -p $DIR/$tdir
1619         local f=$DIR/$tdir/$tfile
1620         local fid=$(copy_file /etc/hosts $f)
1621
1622         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1623         wait_request_state $fid ARCHIVE SUCCEED
1624         $LFS hsm_release $f || error "release of $f failed"
1625
1626         do_node $CLIENT2 diff -q /etc/hosts $f
1627         local st=$?
1628
1629         # we check we had a restore done
1630         wait_request_state $fid RESTORE SUCCEED
1631
1632         [[ $st -eq 0 ]] || error "Restored file differs"
1633 }
1634 run_test 12h "Restore a released file implicitly from a second node"
1635
1636 test_12m() {
1637         # test needs a running copytool
1638         copytool setup
1639
1640         mkdir -p $DIR/$tdir
1641         local f=$DIR/$tdir/$tfile
1642         local fid=$(copy_file /etc/passwd $f)
1643         $LFS hsm_archive $f || error "archive of $f failed"
1644         wait_request_state $fid ARCHIVE SUCCEED
1645
1646         $LFS hsm_release $f || error "release of $f failed"
1647
1648         cmp /etc/passwd $f
1649
1650         [[ $? -eq 0 ]] || error "Restored file differs"
1651 }
1652 run_test 12m "Archive/release/implicit restore"
1653
1654 test_12n() {
1655         # test needs a running copytool
1656         copytool_setup
1657
1658         mkdir -p $DIR/$tdir
1659         copy2archive /etc/hosts $tdir/$tfile
1660
1661         local f=$DIR/$tdir/$tfile
1662         import_file $tdir/$tfile $f
1663
1664         do_facet $SINGLEAGT cmp /etc/hosts $f ||
1665                 error "Restored file differs"
1666
1667         $LFS hsm_release $f || error "release of $f failed"
1668
1669         copytool_cleanup
1670 }
1671 run_test 12n "Import/implicit restore/release"
1672
1673 test_12o() {
1674         # test needs a running copytool
1675         copytool setup
1676
1677         mkdir -p $DIR/$tdir
1678         local f=$DIR/$tdir/$tfile
1679         local fid=$(copy_file /etc/hosts $f)
1680
1681         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1682         wait_request_state $fid ARCHIVE SUCCEED
1683         $LFS hsm_release $f || error "release of $f failed"
1684
1685 #define OBD_FAIL_MDS_HSM_SWAP_LAYOUTS           0x152
1686         do_facet $SINGLEMDS lctl set_param fail_loc=0x152
1687
1688         # set no retry action mode
1689         cdt_set_no_retry
1690
1691         diff -q /etc/hosts $f
1692         local st=$?
1693
1694         # we check we had a restore failure
1695         wait_request_state $fid RESTORE FAILED
1696
1697         [[ $st -eq 0 ]] && error "Restore must fail"
1698
1699         # remove no retry action mode
1700         cdt_clear_no_retry
1701
1702         # check file is still released
1703         check_hsm_flags $f "0x0000000d"
1704
1705         # retry w/o failure injection
1706         do_facet $SINGLEMDS lctl set_param fail_loc=0
1707
1708         # to be sure previous RESTORE result is gone
1709         cdt_purge
1710         wait_for_grace_delay
1711
1712         diff -q /etc/hosts $f
1713         st=$?
1714
1715         # we check we had a restore done
1716         wait_request_state $fid RESTORE SUCCEED
1717
1718         [[ $st -eq 0 ]] || error "Restored file differs"
1719 }
1720 run_test 12o "Layout-swap failure during Restore leaves file released"
1721
1722 test_12p() {
1723         # test needs a running copytool
1724         copytool setup
1725
1726         mkdir $DIR/$tdir
1727         local f=$DIR/$tdir/$tfile
1728         local fid=$(copy_file /etc/hosts $f)
1729
1730         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1731         wait_request_state $fid ARCHIVE SUCCEED
1732         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1733         $LFS hsm_release $f || error "cannot release $f"
1734         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1735         $LFS hsm_release $f || error "cannot release $f"
1736         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1737 }
1738 run_test 12p "implicit restore of a file on copytool mount point"
1739
1740 cleanup_test_12q() {
1741                 error "cannot umount $MOUNT3 on $SINGLEAGT"
1742 }
1743
1744 test_12q() {
1745         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.58) ] &&
1746                 skip "need MDS version at least 2.7.58" && return 0
1747
1748         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
1749         zconf_mount $(facet_host $SINGLEAGT) $MOUNT3 ||
1750                 error "cannot mount $MOUNT3 on $SINGLEAGT"
1751
1752         # test needs a running copytool
1753         copytool setup -m "$MOUNT3"
1754
1755         local f=$DIR/$tdir/$tfile
1756         local f2=$DIR2/$tdir/$tfile
1757         local fid=$(create_small_file $f)
1758         local orig_size=$(stat -c "%s" $f)
1759
1760         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1761         wait_request_state $fid ARCHIVE SUCCEED
1762
1763         $LFS hsm_release $f || error "could not release file"
1764         check_hsm_flags $f "0x0000000d"
1765
1766         kill_copytools
1767         wait_copytools || error "copytool failed to stop"
1768
1769         cat $f > /dev/null &
1770
1771         # wait a bit to allow implicit restore request to be handled.
1772         # if not, next stat would also block on layout-lock.
1773         sleep 5
1774
1775         local size=$(stat -c "%s" $f2)
1776         [ $size -eq $orig_size ] ||
1777                 error "$f2: wrong size after archive: $size != $orig_size"
1778
1779         copytool setup -m "$MOUNT3"
1780
1781         wait
1782
1783         size=$(stat -c "%s" $f)
1784         [ $size -eq $orig_size ] ||
1785                 error "$f: wrong size after restore: $size != $orig_size"
1786
1787         size=$(stat -c "%s" $f2)
1788         [ $size -eq $orig_size ] ||
1789                 error "$f2: wrong size after restore: $size != $orig_size"
1790
1791         :>$f
1792
1793         size=$(stat -c "%s" $f)
1794         [ $size -eq 0 ] ||
1795                 error "$f: wrong size after overwrite: $size != 0"
1796
1797         size=$(stat -c "%s" $f2)
1798         [ $size -eq 0 ] ||
1799                 error "$f2: wrong size after overwrite: $size != 0"
1800 }
1801 run_test 12q "file attributes are refreshed after restore"
1802
1803 test_13() {
1804         # test needs a running copytool
1805         copytool_setup
1806
1807         local ARC_SUBDIR="import.orig"
1808         local d=""
1809         local f=""
1810
1811         # populate directory to be imported
1812         for d in $(seq 1 10); do
1813                 local CURR_DIR="$HSM_ARCHIVE/$ARC_SUBDIR/dir.$d"
1814                 do_facet $SINGLEAGT mkdir -p "$CURR_DIR"
1815                 for f in $(seq 1 10); do
1816                         CURR_FILE="$CURR_DIR/$tfile.$f"
1817                         # write file-specific data
1818                         do_facet $SINGLEAGT \
1819                                 "echo d=$d, f=$f, dir=$CURR_DIR, "\
1820                                         "file=$CURR_FILE > $CURR_FILE"
1821                 done
1822         done
1823         # import to Lustre
1824         import_file "$ARC_SUBDIR" $DIR/$tdir
1825         # diff lustre content and origin (triggers file restoration)
1826         # there must be 10x10 identical files, and no difference
1827         local cnt_ok=$(do_facet $SINGLEAGT diff -rs $HSM_ARCHIVE/$ARC_SUBDIR \
1828                        $DIR/$tdir/$ARC_SUBDIR | grep identical | wc -l)
1829         local cnt_diff=$(do_facet $SINGLEAGT diff -r $HSM_ARCHIVE/$ARC_SUBDIR \
1830                          $DIR/$tdir/$ARC_SUBDIR | wc -l)
1831
1832         [ $cnt_diff -eq 0 ] ||
1833                 error "$cnt_diff imported files differ from read data"
1834         [ $cnt_ok -eq 100 ] ||
1835                 error "not enough identical files ($cnt_ok != 100)"
1836
1837         copytool_cleanup
1838 }
1839 run_test 13 "Recursively import and restore a directory"
1840
1841 test_14() {
1842         # test needs a running copytool
1843         copytool_setup
1844
1845         # archive a file
1846         local f=$DIR/$tdir/$tfile
1847         local fid=$(create_small_file $f)
1848         local sum=$(md5sum $f | awk '{print $1}')
1849         $LFS hsm_archive $f || error "could not archive file"
1850         wait_request_state $fid ARCHIVE SUCCEED
1851
1852         # create released file (simulate llapi_hsm_import call)
1853         local fid2=$(create_empty_file "$f")
1854         $LFS hsm_set --archived --exists $f || error "could not force hsm flags"
1855         $LFS hsm_release $f || error "could not release file"
1856
1857         # rebind the archive to the newly created file
1858         echo "rebind $fid to $fid2"
1859
1860         do_facet $SINGLEAGT \
1861                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1862                  --rebind $fid $fid2 $DIR" || error "could not rebind file"
1863
1864         # restore file and compare md5sum
1865         local sum2=$(md5sum $f | awk '{print $1}')
1866
1867         [[ $sum == $sum2 ]] || error "md5sum mismatch after restore"
1868
1869         copytool_cleanup
1870 }
1871 run_test 14 "Rebind archived file to a new fid"
1872
1873 test_15() {
1874         # test needs a running copytool
1875         copytool_setup
1876
1877         # archive files
1878         local f=$DIR/$tdir/$tfile
1879         local count=5
1880         local tmpfile=$SHARED_DIRECTORY/tmp.$$
1881
1882         local fids=()
1883         local sums=()
1884         for i in $(seq 1 $count); do
1885                 fids[$i]=$(create_small_file $f.$i)
1886                 sums[$i]=$(md5sum $f.$i | awk '{print $1}')
1887                 $LFS hsm_archive $f.$i || error "could not archive file"
1888         done
1889         wait_all_done $(($count*60))
1890
1891         :>$tmpfile
1892         # delete the files
1893         for i in $(seq 1 $count); do
1894                 local fid2=$(create_empty_file "${f}.${i}")
1895                 # add the rebind operation to the list
1896                 echo ${fids[$i]} $fid2 >> $tmpfile
1897
1898                 # set it released (simulate llapi_hsm_import call)
1899                 $LFS hsm_set --archived --exists $f.$i ||
1900                         error "could not force hsm flags"
1901                 $LFS hsm_release $f.$i || error "could not release file"
1902         done
1903         nl=$(wc -l < $tmpfile)
1904         [[ $nl == $count ]] || error "$nl files in list, $count expected"
1905
1906         echo "rebind list of files"
1907         do_facet $SINGLEAGT \
1908                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1909                  --rebind $tmpfile $DIR" || error "could not rebind file list"
1910
1911         # restore files and compare md5sum
1912         for i in $(seq 1 $count); do
1913                 local sum2=$(md5sum $f.$i | awk '{print $1}')
1914                 [[ $sum2 == ${sums[$i]} ]] ||
1915                     error "md5sum mismatch after restore ($sum2 != ${sums[$i]})"
1916         done
1917
1918         rm -f $tmpfile
1919         copytool_cleanup
1920 }
1921 run_test 15 "Rebind a list of files"
1922
1923 test_16() {
1924         # test needs a running copytool
1925         copytool setup -b 1
1926
1927         local ref=/tmp/ref
1928         # create a known size file so we can verify transfer speed
1929         # 20 MB <-> 20s
1930         local goal=20
1931         dd if=/dev/zero of=$ref bs=1M count=20
1932
1933         mkdir -p $DIR/$tdir
1934         local f=$DIR/$tdir/$tfile
1935         local fid=$(copy_file $ref $f)
1936         rm $ref
1937         local start=$(date +%s)
1938         $LFS hsm_archive $f
1939         wait_request_state $fid ARCHIVE SUCCEED
1940         local end=$(date +%s)
1941         # Add 1 to account for rounding errors between start and end (LU-8155)
1942         local duration=$((end - start + 1))
1943
1944         [[ $duration -ge $((goal - 1)) ]] ||
1945                 error "Transfer is too fast $duration < $goal"
1946 }
1947 run_test 16 "Test CT bandwith control option"
1948
1949 test_20() {
1950         local f=$DIR/$tdir/$tfile
1951         create_empty_file "$f"
1952
1953         # Could not release a non-archived file
1954         $LFS hsm_release $f && error "release should not succeed"
1955
1956         # For following tests, we must test them with HS_ARCHIVED set
1957         $LFS hsm_set --exists --archived $f || error "could not add flag"
1958
1959         # Could not release a file if no-release is set
1960         $LFS hsm_set --norelease $f || error "could not add flag"
1961         $LFS hsm_release $f && error "release should not succeed"
1962         $LFS hsm_clear --norelease $f || error "could not remove flag"
1963
1964         # Could not release a file if lost
1965         $LFS hsm_set --lost $f || error "could not add flag"
1966         $LFS hsm_release $f && error "release should not succeed"
1967         $LFS hsm_clear --lost $f || error "could not remove flag"
1968
1969         # Could not release a file if dirty
1970         $LFS hsm_set --dirty $f || error "could not add flag"
1971         $LFS hsm_release $f && error "release should not succeed"
1972         $LFS hsm_clear --dirty $f || error "could not remove flag"
1973 }
1974 run_test 20 "Release is not permitted"
1975
1976 test_21() {
1977         # test needs a running copytool
1978         copytool setup
1979
1980         local f=$DIR/$tdir/test_release
1981
1982         # Create a file and check its states
1983         local fid=$(create_small_file $f)
1984         check_hsm_flags $f "0x00000000"
1985
1986         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1987         # used until file is flushed to disk
1988         if [  $(facet_fstype ost1) == "zfs" ]; then
1989             # this causes an OST_SYNC rpc to be sent
1990             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1991             # clear locks to reread file data
1992             cancel_lru_locks osc
1993         fi
1994
1995         local orig_size=$(stat -c "%s" $f)
1996         local orig_blocks=$(stat -c "%b" $f)
1997
1998         start_full_debug_logging
1999
2000         $LFS hsm_archive $f || error "could not archive file"
2001         wait_request_state $fid ARCHIVE SUCCEED
2002
2003         local blocks=$(stat -c "%b" $f)
2004         [ $blocks -eq $orig_blocks ] ||
2005                 error "$f: wrong block number after archive: " \
2006                       "$blocks != $orig_blocks"
2007         local size=$(stat -c "%s" $f)
2008         [ $size -eq $orig_size ] ||
2009                 error "$f: wrong size after archive: $size != $orig_size"
2010
2011         # Release and check states
2012         $LFS hsm_release $f || error "could not release file"
2013         check_hsm_flags $f "0x0000000d"
2014
2015         blocks=$(stat -c "%b" $f)
2016         [ $blocks -gt 5 ] &&
2017                 error "$f: too many blocks after release: $blocks > 5"
2018         size=$(stat -c "%s" $f)
2019         [ $size -ne $orig_size ] &&
2020                 error "$f: wrong size after release: $size != $orig_size"
2021
2022         # Check we can release an file without stripe info
2023         f=$f.nolov
2024         $MCREATE $f
2025         fid=$(path2fid $f)
2026         check_hsm_flags $f "0x00000000"
2027         $LFS hsm_archive $f || error "could not archive file"
2028         wait_request_state $fid ARCHIVE SUCCEED
2029
2030         # Release and check states
2031         $LFS hsm_release $f || error "could not release file"
2032         check_hsm_flags $f "0x0000000d"
2033
2034         # Release again a file that is already released is OK
2035         $LFS hsm_release $f || fail "second release should succeed"
2036         check_hsm_flags $f "0x0000000d"
2037
2038         stop_full_debug_logging
2039 }
2040 run_test 21 "Simple release tests"
2041
2042 test_22() {
2043         # test needs a running copytool
2044         copytool setup
2045
2046         local f=$DIR/$tdir/test_release
2047         local swap=$DIR/$tdir/test_swap
2048
2049         # Create a file and check its states
2050         local fid=$(create_small_file $f)
2051         check_hsm_flags $f "0x00000000"
2052
2053         $LFS hsm_archive $f || error "could not archive file"
2054         wait_request_state $fid ARCHIVE SUCCEED
2055
2056         # Release and check states
2057         $LFS hsm_release $f || error "could not release file"
2058         check_hsm_flags $f "0x0000000d"
2059
2060         create_small_file $swap
2061         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
2062
2063         return 0
2064 }
2065 run_test 22 "Could not swap a release file"
2066
2067 test_23() {
2068         # test needs a running copytool
2069         copytool setup
2070
2071         local f=$DIR/$tdir/test_mtime
2072
2073         # Create a file and check its states
2074         local fid=$(create_small_file $f)
2075         check_hsm_flags $f "0x00000000"
2076
2077         $LFS hsm_archive $f || error "could not archive file"
2078         wait_request_state $fid ARCHIVE SUCCEED
2079
2080         # Set modification time in the past
2081         touch -m -a -d @978261179 $f
2082
2083         # Release and check states
2084         $LFS hsm_release $f || error "could not release file"
2085         check_hsm_flags $f "0x0000000d"
2086
2087         local MTIME=$(stat -c "%Y" $f)
2088         local ATIME=$(stat -c "%X" $f)
2089         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
2090         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
2091 }
2092 run_test 23 "Release does not change a/mtime (utime)"
2093
2094 test_24a() {
2095         local file=$DIR/$tdir/$tfile
2096         local fid
2097         local atime0
2098         local atime1
2099         local mtime0
2100         local mtime1
2101         local ctime0
2102         local ctime1
2103
2104         # test needs a running copytool
2105         copytool setup
2106
2107         fid=$(create_small_file $file)
2108
2109         # Create a file and check its states
2110         check_hsm_flags $file "0x00000000"
2111
2112         # Ensure atime is less than mtime and ctime.
2113         sleep 1
2114         echo >> $file
2115
2116         atime0=$(stat -c "%X" $file)
2117         mtime0=$(stat -c "%Y" $file)
2118         ctime0=$(stat -c "%Z" $file)
2119
2120         [ $atime0 -lt $mtime0 ] ||
2121                 error "atime $atime0 is not less than mtime $mtime0"
2122
2123         [ $atime0 -lt $ctime0 ] ||
2124                 error "atime $atime0 is not less than ctime $ctime0"
2125
2126         # Archive should not change any timestamps.
2127         $LFS hsm_archive $file || error "cannot archive '$file'"
2128         wait_request_state $fid ARCHIVE SUCCEED
2129
2130         atime1=$(stat -c "%X" $file)
2131         mtime1=$(stat -c "%Y" $file)
2132         ctime1=$(stat -c "%Z" $file)
2133
2134         [ $atime0 -eq $atime1 ] ||
2135                 error "archive changed atime from $atime0 to $atime1"
2136
2137         [ $mtime0 -eq $mtime1 ] ||
2138                 error "archive changed mtime from $mtime0 to $mtime1"
2139
2140         [ $ctime0 -eq $ctime1 ] ||
2141                 error "archive changed ctime from $ctime0 to $ctime1"
2142
2143         # Release should not change any timestamps.
2144         $LFS hsm_release $file || error "cannot release '$file'"
2145         check_hsm_flags $file "0x0000000d"
2146
2147         atime1=$(stat -c "%X" $file)
2148         mtime1=$(stat -c "%Y" $file)
2149         ctime1=$(stat -c "%Z" $file)
2150
2151         [ $atime0 -eq $atime1 ] ||
2152                 error "release changed atime from $atime0 to $atime1"
2153
2154         [ $mtime0 -eq $mtime1 ] ||
2155                 error "release changed mtime from $mtime0 to $mtime1"
2156
2157         [ $ctime0 -eq $ctime1 ] ||
2158                 error "release changed ctime from $ctime0 to $ctime1"
2159
2160         # Restore should not change any timestamps.
2161         $LFS hsm_restore $file
2162         wait_request_state $fid RESTORE SUCCEED
2163
2164         atime1=$(stat -c "%X" $file)
2165         mtime1=$(stat -c "%Y" $file)
2166         ctime1=$(stat -c "%Z" $file)
2167
2168         [ $atime0 -eq $atime1 ] ||
2169                 error "restore changed atime from $atime0 to $atime1"
2170
2171         [ $mtime0 -eq $mtime1 ] ||
2172                 error "restore changed mtime from $mtime0 to $mtime1"
2173
2174         [ $ctime0 -eq $ctime1 ] ||
2175                 error "restore changed ctime from $ctime0 to $ctime1"
2176
2177         kill_copytools
2178         wait_copytools || error "Copytools failed to stop"
2179
2180         # Once more, after unmount and mount.
2181         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
2182         mount_client $MOUNT || error "cannot mount '$MOUNT'"
2183
2184         atime1=$(stat -c "%X" $file)
2185         mtime1=$(stat -c "%Y" $file)
2186         ctime1=$(stat -c "%Z" $file)
2187
2188         [ $atime0 -eq $atime1 ] ||
2189                 error "remount changed atime from $atime0 to $atime1"
2190
2191         [ $mtime0 -eq $mtime1 ] ||
2192                 error "remount changed mtime from $mtime0 to $mtime1"
2193
2194         [ $ctime0 -eq $ctime1 ] ||
2195                 error "remount changed ctime from $ctime0 to $ctime1"
2196 }
2197 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
2198
2199 test_24b() {
2200         local file=$DIR/$tdir/$tfile
2201         local fid
2202         local sum0
2203         local sum1
2204         # LU-3811
2205
2206         # Test needs a running copytool.
2207         copytool setup
2208
2209         # Check that root can do HSM actions on a regular user's file.
2210         fid=$(create_small_file $file)
2211         sum0=$(md5sum $file)
2212
2213         chown $RUNAS_ID:$RUNAS_GID $file ||
2214                 error "cannot chown '$file' to '$RUNAS_ID'"
2215
2216         chmod ugo-w $DIR/$tdir ||
2217                 error "cannot chmod '$DIR/$tdir'"
2218
2219         $LFS hsm_archive $file
2220         wait_request_state $fid ARCHIVE SUCCEED
2221
2222         $LFS hsm_release $file
2223         check_hsm_flags $file "0x0000000d"
2224
2225         $LFS hsm_restore $file
2226         wait_request_state $fid RESTORE SUCCEED
2227
2228         # Check that ordinary user can get HSM state.
2229         $RUNAS $LFS hsm_state $file ||
2230                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
2231
2232         $LFS hsm_release $file
2233         check_hsm_flags $file "0x0000000d"
2234
2235         # Check that ordinary user can accessed released file.
2236         sum1=$($RUNAS md5sum $file) ||
2237                 error "user '$RUNAS_ID' cannot read '$file'"
2238
2239         [ "$sum0" == "$sum1" ] ||
2240                 error "md5sum mismatch for '$file'"
2241 }
2242 run_test 24b "root can archive, release, and restore user files"
2243
2244 test_24c() {
2245         local file=$DIR/$tdir/$tfile
2246         local action=archive
2247         local user_save
2248         local group_save
2249         local other_save
2250
2251         # test needs a running copytool
2252         copytool setup
2253
2254         mkdir -p $DIR/$tdir
2255
2256         # Save the default masks and check that cleanup_24c will
2257         # restore the request masks correctly.
2258         user_save=$(get_hsm_param user_request_mask)
2259         stack_trap "set_hsm_param user_request_mask $user_save" EXIT
2260         group_save=$(get_hsm_param group_request_mask)
2261         stack_trap "set_hsm_param user_request_mask $group_save" EXIT
2262         other_save=$(get_hsm_param other_request_mask)
2263         stack_trap "set_hsm_param user_request_mask $other_save" EXIT
2264
2265         [ "$user_save" == RESTORE ] ||
2266                 error "user_request_mask is '$user_save' expected 'RESTORE'"
2267         [ "$group_save" == RESTORE ] ||
2268                 error "group_request_mask is '$group_save' expected 'RESTORE'"
2269         [ "$other_save" == RESTORE ] ||
2270                 error "other_request_mask is '$other_save' expected 'RESTORE'"
2271
2272         # User.
2273         create_small_file $file
2274         chown $RUNAS_ID:$GROUP $file ||
2275                 error "cannot chown '$file' to '$RUNAS_ID:$GROUP'"
2276
2277         $RUNAS $LFS hsm_$action $file &&
2278                 error "$action by user should fail"
2279
2280         set_hsm_param user_request_mask $action
2281         $RUNAS $LFS hsm_$action $file ||
2282                 error "$action by user should succeed"
2283
2284         # Group.
2285         create_small_file $file
2286         chown nobody:$RUNAS_GID $file ||
2287                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
2288
2289         $RUNAS $LFS hsm_$action $file &&
2290                 error "$action by group should fail"
2291
2292         set_hsm_param group_request_mask $action
2293         $RUNAS $LFS hsm_$action $file ||
2294                 error "$action by group should succeed"
2295
2296         # Other.
2297         create_small_file $file
2298         chown nobody:$GROUP $file ||
2299                 error "cannot chown '$file' to 'nobody:$GROUP'"
2300
2301         $RUNAS $LFS hsm_$action $file &&
2302                 error "$action by other should fail"
2303
2304         set_hsm_param other_request_mask $action
2305         $RUNAS $LFS hsm_$action $file ||
2306                 error "$action by other should succeed"
2307 }
2308 run_test 24c "check that user,group,other request masks work"
2309
2310 test_24d() {
2311         local file1=$DIR/$tdir/$tfile
2312         local file2=$DIR2/$tdir/$tfile
2313         local fid1
2314         local fid2
2315
2316         fid1=$(create_small_file $file1)
2317
2318         echo $fid1
2319         $LFS getstripe $file1
2320
2321         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
2322         zconf_mount "$(facet_host $SINGLEAGT)" "$MOUNT3" ||
2323                 error "cannot mount '$MOUNT3' on '$SINGLEAGT'"
2324
2325         copytool setup -m  "$MOUNT3"
2326
2327         stack_trap "mount -o remount,rw \"$MOUNT2\"" EXIT
2328         mount -o remount,ro $MOUNT2
2329
2330         do_nodes $(comma_list $(nodes_list)) $LCTL clear
2331
2332         fid2=$(path2fid $file2)
2333         [ "$fid1" == "$fid2" ] ||
2334                 error "FID mismatch '$fid1' != '$fid2'"
2335
2336         $LFS hsm_archive $file2 &&
2337                 error "archive should fail on read-only mount"
2338         check_hsm_flags $file1 "0x00000000"
2339
2340         $LFS hsm_archive $file1 || error "Fail to archive $file1"
2341         wait_request_state $fid1 ARCHIVE SUCCEED
2342
2343         $LFS hsm_release $file1
2344         $LFS hsm_restore $file2
2345         wait_request_state $fid1 RESTORE SUCCEED
2346
2347         $LFS hsm_release $file1 || error "cannot release '$file1'"
2348         dd if=$file2 of=/dev/null bs=1M || error "cannot read '$file2'"
2349
2350         $LFS hsm_release $file2 &&
2351                 error "release should fail on read-only mount"
2352
2353         return 0
2354 }
2355 run_test 24d "check that read-only mounts are respected"
2356
2357 test_24e() {
2358         copytool setup
2359
2360         local f=$DIR/$tdir/$tfile
2361         local fid
2362
2363         fid=$(create_small_file $f) || error "cannot create $f"
2364         $LFS hsm_archive $f || error "cannot archive $f"
2365         wait_request_state $fid ARCHIVE SUCCEED
2366         $LFS hsm_release $f || error "cannot release $f"
2367         while ! $LFS hsm_state $f | grep released; do
2368                 sleep 1
2369         done
2370
2371         tar -cf $TMP/$tfile.tar $DIR/$tdir || error "cannot tar $DIR/$tdir"
2372 }
2373 run_test 24e "tar succeeds on HSM released files" # LU-6213
2374
2375 test_24f() {
2376         # test needs a running copytool
2377         copytool setup
2378
2379         mkdir -p $DIR/$tdir/d1
2380         local f=$DIR/$tdir/$tfile
2381         local fid=$(copy_file /etc/hosts $f)
2382         sum0=$(md5sum $f)
2383         echo $sum0
2384         $LFS hsm_archive $f ||
2385                 error "hsm_archive failed"
2386         wait_request_state $fid ARCHIVE SUCCEED
2387         $LFS hsm_release $f || error "cannot release $f"
2388         tar --xattrs -cvf $f.tar -C $DIR/$tdir $tfile
2389         rm -f $f
2390         sync
2391         tar --xattrs -xvf $f.tar -C $DIR/$tdir ||
2392                 error "Can not recover the tar contents"
2393         sum1=$(md5sum $f)
2394         echo "Sum0 = $sum0, sum1 = $sum1"
2395         [ "$sum0" == "$sum1" ] || error "md5sum mismatch for '$tfile'"
2396 }
2397 run_test 24f "root can archive, release, and restore tar files"
2398
2399 test_25a() {
2400         # test needs a running copytool
2401         copytool_setup
2402
2403         mkdir -p $DIR/$tdir
2404         copy2archive /etc/hosts $tdir/$tfile
2405
2406         local f=$DIR/$tdir/$tfile
2407
2408         import_file $tdir/$tfile $f
2409
2410         $LFS hsm_set --lost $f
2411
2412         md5sum $f
2413         local st=$?
2414
2415         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2416
2417         copytool_cleanup
2418 }
2419 run_test 25a "Restore lost file (HS_LOST flag) from import"\
2420              " (Operation not permitted)"
2421
2422 test_25b() {
2423         # test needs a running copytool
2424         copytool setup
2425
2426         mkdir -p $DIR/$tdir
2427
2428         local f=$DIR/$tdir/$tfile
2429         local fid=$(copy_file /etc/passwd $f)
2430
2431         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2432         wait_request_state $fid ARCHIVE SUCCEED
2433
2434         $LFS hsm_release $f
2435         $LFS hsm_set --lost $f
2436         md5sum $f
2437         st=$?
2438
2439         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2440 }
2441 run_test 25b "Restore lost file (HS_LOST flag) after release"\
2442              " (Operation not permitted)"
2443
2444 test_26A() { # was test_26
2445         # test needs a running copytool
2446         copytool setup
2447
2448         local f=$DIR/$tdir/$tfile
2449         local fid
2450         fid=$(make_custom_file_for_progress $f 39 1000000)
2451         [ $? != 0 ] && skip "not enough free space" && return
2452
2453         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2454         wait_request_state $fid ARCHIVE SUCCEED
2455
2456         $LFS hsm_remove $f
2457         wait_request_state $fid REMOVE SUCCEED
2458
2459         check_hsm_flags $f "0x00000000"
2460 }
2461 run_test 26A "Remove the archive of a valid file"
2462
2463 test_26a() {
2464         local raolu=$(get_hsm_param remove_archive_on_last_unlink)
2465         [[ $raolu -eq 0 ]] || error "RAoLU policy should be off"
2466
2467         # test needs a running copytool
2468         copytool setup
2469
2470         mkdir -p $DIR/$tdir
2471         local f=$DIR/$tdir/$tfile
2472         local fid=$(copy_file /etc/passwd $f)
2473
2474         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2475         wait_request_state $fid ARCHIVE SUCCEED
2476
2477         local f2=$DIR/$tdir/${tfile}_2
2478         local fid2=$(copy_file /etc/passwd $f2)
2479
2480         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2481         wait_request_state $fid2 ARCHIVE SUCCEED
2482
2483         local f3=$DIR/$tdir/${tfile}_3
2484         local fid3=$(copy_file /etc/passwd $f3)
2485
2486         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f3
2487         wait_request_state $fid3 ARCHIVE SUCCEED
2488
2489         # set a long grace_delay vs short loop_period
2490         local orig_loop_period=$(get_hsm_param loop_period)
2491         local orig_grace_delay=$(get_hsm_param grace_delay)
2492         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2493         set_hsm_param loop_period 10
2494         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2495         set_hsm_param grace_delay 100
2496
2497         rm -f $f
2498
2499         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2500         set_hsm_param remove_archive_on_last_unlink 1
2501
2502         ln "$f3" "$f3"_bis || error "Unable to create hard-link"
2503         rm -f $f3
2504
2505         rm -f $f2
2506
2507         set_hsm_param remove_archive_on_last_unlink 0
2508
2509         wait_request_state $fid2 REMOVE SUCCEED
2510
2511         assert_request_count $fid REMOVE 0 \
2512                 "Unexpected archived data remove request for $f"
2513         assert_request_count $fid3 REMOVE 0 \
2514                 "Unexpected archived data remove request for $f3"
2515 }
2516 run_test 26a "Remove Archive On Last Unlink (RAoLU) policy"
2517
2518 test_26b() {
2519         # test needs a running copytool
2520         copytool setup
2521
2522         mkdir -p $DIR/$tdir
2523         local f=$DIR/$tdir/$tfile
2524         local fid=$(copy_file /etc/passwd $f)
2525
2526         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2527         wait_request_state $fid ARCHIVE SUCCEED
2528
2529         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2530         set_hsm_param remove_archive_on_last_unlink 1
2531
2532         cdt_shutdown
2533         cdt_check_state stopped
2534
2535         rm -f $f
2536
2537         set_hsm_param remove_archive_on_last_unlink 0
2538
2539         wait_request_state $fid REMOVE WAITING
2540
2541         cdt_enable
2542
2543         # copytool must re-register
2544         kill_copytools
2545         wait_copytools || error "copytool failed to stop"
2546         copytool setup
2547
2548         wait_request_state $fid REMOVE SUCCEED
2549 }
2550 run_test 26b "RAoLU policy when CDT off"
2551
2552 test_26c() {
2553         # test needs a running copytool
2554         copytool setup
2555
2556         mkdir -p $DIR/$tdir
2557         local f=$DIR/$tdir/$tfile
2558         local fid=$(copy_file /etc/passwd $f)
2559
2560         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2561         wait_request_state $fid ARCHIVE SUCCEED
2562
2563         local f2=$DIR/$tdir/${tfile}_2
2564         local fid2=$(copy_file /etc/passwd $f2)
2565
2566         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2567         wait_request_state $fid2 ARCHIVE SUCCEED
2568
2569         # set a long grace_delay vs short loop_period
2570         local orig_loop_period=$(get_hsm_param loop_period)
2571         local orig_grace_delay=$(get_hsm_param grace_delay)
2572         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2573         set_hsm_param loop_period 10
2574         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2575         set_hsm_param grace_delay 100
2576
2577         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2578         set_hsm_param remove_archive_on_last_unlink 1
2579
2580         multiop_bg_pause $f O_c || error "open $f failed"
2581         local pid=$!
2582
2583         rm -f $f
2584         rm -f $f2
2585
2586         wait_request_state $fid2 REMOVE SUCCEED
2587         assert_request_count $fid REMOVE 0 \
2588                 "Unexpected archived data remove request for $f"
2589
2590         kill -USR1 $pid || error "multiop early exit"
2591         # should reach autotest timeout if multiop fails to trap
2592         # signal, close file, and exit ...
2593         wait $pid || error "wait PID $PID failed"
2594
2595         set_hsm_param remove_archive_on_last_unlink 0
2596
2597         wait_request_state $fid REMOVE SUCCEED
2598 }
2599 run_test 26c "RAoLU effective when file closed"
2600
2601 test_26d() {
2602         # test needs a running copytool
2603         copytool setup
2604
2605         mkdir -p $DIR/$tdir
2606         local f=$DIR/$tdir/$tfile
2607         local fid=$(create_small_file $f)
2608
2609         $LFS hsm_archive $f || error "could not archive file"
2610         wait_request_state $fid ARCHIVE SUCCEED
2611
2612         # set a long grace_delay vs short loop_period
2613         local orig_loop_period=$(get_hsm_param loop_period)
2614         local orig_grace_delay=$(get_hsm_param grace_delay)
2615         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2616         set_hsm_param loop_period 10
2617         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2618         set_hsm_param grace_delay 100
2619
2620         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2621         set_hsm_param remove_archive_on_last_unlink 1
2622
2623         multiop_bg_pause $f O_c || error "multiop failed"
2624         local MULTIPID=$!
2625
2626         rm -f $f
2627
2628         mds_evict_client
2629
2630         set_hsm_param remove_archive_on_last_unlink 0
2631
2632         wait_request_state $fid REMOVE SUCCEED
2633
2634         client_up || client_up || true
2635
2636         kill -USR1 $MULTIPID
2637         wait $MULTIPID || error "multiop close failed"
2638 }
2639 run_test 26d "RAoLU when Client eviction"
2640
2641 test_27a() {
2642         # test needs a running copytool
2643         copytool_setup
2644
2645         create_archive_file $tdir/$tfile
2646         local f=$DIR/$tdir/$tfile
2647         import_file $tdir/$tfile $f
2648         local fid=$(path2fid $f)
2649
2650         $LFS hsm_remove $f
2651
2652         [[ $? != 0 ]] || error "Remove of a released file should fail"
2653
2654         copytool_cleanup
2655 }
2656 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
2657
2658 test_27b() {
2659         # test needs a running copytool
2660         copytool setup
2661
2662         local f=$DIR/$tdir/$tfile
2663         local fid
2664         fid=$(make_custom_file_for_progress $f 39 1000000)
2665         [ $? != 0 ] && skip "not enough free space" && return
2666
2667         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2668         wait_request_state $fid ARCHIVE SUCCEED
2669         $LFS hsm_release $f
2670
2671         $LFS hsm_remove $f
2672
2673         [[ $? != 0 ]] || error "Remove of a released file should fail"
2674 }
2675 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
2676
2677 test_28() {
2678         # test needs a running copytool
2679         copytool setup
2680
2681         local f=$DIR/$tdir/$tfile
2682         local fid
2683         fid=$(make_custom_file_for_progress $f 39 1000000)
2684         [ $? != 0 ] && skip "not enough free space" && return
2685
2686         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2687         wait_request_state $fid ARCHIVE SUCCEED
2688
2689         cdt_disable
2690         $LFS hsm_remove $f
2691
2692         rm -f $f
2693
2694         cdt_enable
2695
2696         wait_request_state $fid REMOVE SUCCEED
2697 }
2698 run_test 28 "Concurrent archive/file remove"
2699
2700 test_29a() {
2701         # Tests --mntpath and --archive options
2702
2703         local archive_id=7
2704         copytool setup -m "$MOUNT" -a $archive_id
2705
2706         # Bad archive number
2707         $LFS hsm_remove -m "$MOUNT" -a 33 0x857765760:0x8:0x2 2>&1 |
2708                 grep "Invalid argument" ||
2709                 error "unexpected hsm_remove failure (1)"
2710
2711         # mntpath is present but file is given
2712         $LFS hsm_remove --mntpath "$MOUNT" --archive 30 /qwerty/uyt 2>&1 |
2713                 grep "hsm: '/qwerty/uyt' is not a valid FID" ||
2714                 error "unexpected hsm_remove failure (2)"
2715 }
2716 run_test 29a "Tests --mntpath and --archive options"
2717
2718 test_29b() {
2719         # test needs a running copytool
2720         copytool setup
2721
2722         local f=$DIR/$tdir/$tfile
2723         local fid=$(create_small_file $f)
2724
2725         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2726         wait_request_state $fid ARCHIVE SUCCEED
2727
2728         rm -f $f
2729
2730         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER $fid
2731         wait_request_state $fid REMOVE SUCCEED
2732 }
2733 run_test 29b "Archive/delete/remove by FID from the archive."
2734
2735 test_29c() {
2736         # test needs a running copytool
2737         copytool setup
2738
2739         local fid1=$(create_small_file $DIR/$tdir/$tfile-1)
2740         local fid2=$(create_small_file $DIR/$tdir/$tfile-2)
2741         local fid3=$(create_small_file $DIR/$tdir/$tfile-3)
2742
2743         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$tfile-[1-3]
2744         wait_request_state $fid1 ARCHIVE SUCCEED
2745         wait_request_state $fid2 ARCHIVE SUCCEED
2746         wait_request_state $fid3 ARCHIVE SUCCEED
2747
2748         rm -f $DIR/$tdir/$tfile-[1-3]
2749
2750         echo $fid1 > $DIR/$tdir/list
2751         echo $fid2 >> $DIR/$tdir/list
2752         echo $fid3 >> $DIR/$tdir/list
2753
2754         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER \
2755                 --filelist $DIR/$tdir/list
2756         wait_request_state $fid1 REMOVE SUCCEED
2757         wait_request_state $fid2 REMOVE SUCCEED
2758         wait_request_state $fid3 REMOVE SUCCEED
2759 }
2760 run_test 29c "Archive/delete/remove by FID, using a file list."
2761
2762 test_29d() {
2763         # test needs more than one CT
2764         needclients 3 || return 0
2765
2766         local n
2767         local file
2768         local fid
2769
2770         # start all of the copytools
2771         for n in $(seq $AGTCOUNT); do
2772                 copytool setup -f agt$n -a $n
2773         done
2774
2775         # archive files
2776         file=$DIR/$tdir/$tfile
2777         fid=$(create_small_file $file)
2778
2779         $LFS hsm_archive $file
2780         wait_request_state $fid ARCHIVE SUCCEED
2781         check_hsm_flags $file "0x00000009"
2782
2783         rm -f $file
2784
2785         $LFS hsm_remove --mntpath "$MOUNT" -a 0 $fid ||
2786                 error "cannot hsm_remove '$fid'"
2787
2788         # give time for CDT to handle remove request and create broadcasted
2789         sleep 2
2790
2791         # remove request has been broadcasted ?
2792         local cnt=$(get_request_count $fid REMOVE)
2793         # broadcasted requests + original
2794         [[ $cnt -eq $((AGTCOUNT + 1)) ]] ||
2795                 error "remove not broadcasted to all CTs"
2796
2797         # give time for CDT and CTs to handle broadcasted
2798         wait_for_loop_period
2799
2800         # each agent serves one different archive_id, so broadcasted
2801         # hsm_remove request should only succeed once and fail at all others
2802         local res
2803         local scnt=0
2804         local fcnt=0
2805         for n in $(seq $AGTCOUNT); do
2806                 res=$(do_facet $SINGLEMDS "$LCTL get_param -n \
2807                                $HSM_PARAM.actions | awk \
2808                                '/'$fid'.*action=REMOVE archive#='$n'/ \
2809                                {print \\\$13}' | cut -f2 -d=")
2810                 if [[ "$res" == "SUCCEED" ]]; then
2811                         scnt=$((scnt + 1))
2812                 elif [[ "$res" == "FAILED" ]]; then
2813                         fcnt=$((fcnt + 1))
2814                 fi
2815         done
2816
2817         [[ $scnt -eq 1 ]] ||
2818                 error "one and only CT should have removed successfully"
2819
2820         [[ $AGTCOUNT -eq $((scnt + fcnt)) ]] ||
2821                 error "all but one CT should have failed to remove"
2822 }
2823 run_test 29d "hsm_remove by FID with archive_id 0 for unlinked file cause "\
2824              "request to be sent once for each registered archive_id"
2825
2826 test_30a() {
2827         # restore at exec cannot work on agent node (because of Linux kernel
2828         # protection of executables)
2829         needclients 2 || return 0
2830
2831         # test needs a running copytool
2832         copytool_setup
2833
2834         mkdir -p $DIR/$tdir
2835         copy2archive /bin/true $tdir/$tfile
2836
2837         local f=$DIR/$tdir/true
2838         import_file $tdir/$tfile $f
2839
2840         local fid=$(path2fid $f)
2841
2842         # set no retry action mode
2843         cdt_set_no_retry
2844         do_node $CLIENT2 $f
2845         local st=$?
2846
2847         # cleanup
2848         # remove no try action mode
2849         cdt_clear_no_retry
2850         $LFS hsm_state $f
2851
2852         [[ $st == 0 ]] || error "Failed to exec a released file"
2853
2854         copytool_cleanup
2855 }
2856 run_test 30a "Restore at exec (import case)"
2857
2858 test_30b() {
2859         # restore at exec cannot work on agent node (because of Linux kernel
2860         # protection of executables)
2861         needclients 2 || return 0
2862
2863         # test needs a running copytool
2864         copytool setup
2865
2866         mkdir -p $DIR/$tdir
2867         local f=$DIR/$tdir/true
2868         local fid=$(copy_file /bin/true $f)
2869         chmod 755 $f
2870         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2871         wait_request_state $fid ARCHIVE SUCCEED
2872         $LFS hsm_release $f
2873         $LFS hsm_state $f
2874
2875         stack_trap cdt_clear_no_retry EXIT
2876         # set no retry action mode
2877         cdt_set_no_retry
2878
2879         do_node $CLIENT2 $f
2880         local st=$?
2881
2882         $LFS hsm_state $f
2883
2884         [[ $st == 0 ]] || error "Failed to exec a released file"
2885 }
2886 run_test 30b "Restore at exec (release case)"
2887
2888 test_30c() {
2889         needclients 2 || return 0
2890
2891         # test needs a running copytool
2892         copytool setup
2893
2894         mkdir -p $DIR/$tdir
2895         local f=$DIR/$tdir/SLEEP
2896         local slp_sum1=$(md5sum /bin/sleep)
2897         local fid=$(copy_file /bin/sleep $f)
2898         chmod 755 $f
2899         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2900         wait_request_state $fid ARCHIVE SUCCEED
2901         $LFS hsm_release $f
2902         check_hsm_flags $f "0x0000000d"
2903
2904         stack_trap cdt_clear_no_retry EXIT
2905         # set no retry action mode
2906         cdt_set_no_retry
2907
2908         do_node $CLIENT2 "$f 10" &
2909         local pid=$!
2910         sleep 3
2911         echo 'Hi!' > $f
2912         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2913         wait $pid
2914         [[ $? == 0 ]] || error "Execution failed during run"
2915         cmp /bin/sleep $f
2916         if [[ $? != 0 ]]; then
2917                 local slp_sum2=$(md5sum /bin/sleep)
2918                 # in case sleep file is modified during the test
2919                 [[ $slp_sum1 == $slp_sum2 ]] &&
2920                         error "Binary overwritten during exec"
2921         fi
2922
2923         check_hsm_flags $f "0x00000009"
2924 }
2925 run_test 30c "Update during exec of released file must fail"
2926
2927 restore_and_check_size() {
2928         local f=$1
2929         local fid=$2
2930         local s=$(stat -c "%s" $f)
2931         local n=$s
2932         local st=$(get_hsm_flags $f)
2933         local err=0
2934         local cpt=0
2935         $LFS hsm_restore $f
2936         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2937         do
2938                 n=$(stat -c "%s" $f)
2939                 # we echo in both cases to show stat is not hang
2940                 if [[ $n != $s ]]; then
2941                         echo "size seen is $n != $s"
2942                         err=1
2943                 else
2944                         echo "size seen is right: $n == $s"
2945                 fi
2946                 sleep 10
2947                 cpt=$((cpt + 1))
2948                 st=$(get_hsm_flags $f)
2949         done
2950         if [[ "$st" = "0x00000009" ]]; then
2951                 echo " "done
2952         else
2953                 echo " restore is too long"
2954                 wait_request_state $fid RESTORE SUCCEED
2955         fi
2956         return $err
2957 }
2958
2959 test_31a() {
2960         # test needs a running copytool
2961         copytool_setup
2962
2963         create_archive_file $tdir/$tfile
2964         local f=$DIR/$tdir/$tfile
2965         import_file $tdir/$tfile $f
2966         local fid=$($LFS path2fid $f)
2967         HSM_ARCHIVE_PURGE=false copytool_setup
2968
2969         restore_and_check_size $f $fid
2970         local err=$?
2971
2972         [[ $err -eq 0 ]] || error "File size changed during restore"
2973
2974         copytool_cleanup
2975 }
2976 run_test 31a "Import a large file and check size during restore"
2977
2978
2979 test_31b() {
2980         # test needs a running copytool
2981         copytool setup
2982
2983         local f=$DIR/$tdir/$tfile
2984         local fid
2985         fid=$(make_custom_file_for_progress $f 39 1000000)
2986         [ $? != 0 ] && skip "not enough free space" && return
2987
2988         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2989         wait_request_state $fid ARCHIVE SUCCEED
2990         $LFS hsm_release $f
2991
2992         restore_and_check_size $f $fid
2993         local err=$?
2994
2995         [[ $err -eq 0 ]] || error "File size changed during restore"
2996 }
2997 run_test 31b "Restore a large unaligned file and check size during restore"
2998
2999 test_31c() {
3000         # test needs a running copytool
3001         copytool setup
3002
3003         local f=$DIR/$tdir/$tfile
3004         local fid
3005         fid=$(make_custom_file_for_progress $f 33 1048576)
3006         [ $? != 0 ] && skip "not enough free space" && return
3007
3008         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3009         wait_request_state $fid ARCHIVE SUCCEED
3010         $LFS hsm_release $f
3011
3012         restore_and_check_size $f $fid
3013         local err=$?
3014
3015         [[ $err -eq 0 ]] || error "File size changed during restore"
3016 }
3017 run_test 31c "Restore a large aligned file and check size during restore"
3018
3019 test_33() {
3020         # test needs a running copytool
3021         copytool setup -b 1
3022
3023         local f=$DIR/$tdir/$tfile
3024         local fid
3025         fid=$(make_custom_file_for_progress $f 39 1000000)
3026         [ $? != 0 ] && skip "not enough free space" && return
3027
3028         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3029         wait_request_state $fid ARCHIVE SUCCEED
3030         $LFS hsm_release $f
3031
3032         # to be sure wait_all_done will not be mislead by previous tests
3033         # and ops.
3034         cdt_purge
3035         wait_for_grace_delay
3036         # Also raise grace_delay significantly so the Canceled
3037         # Restore action will stay enough long avail.
3038         local old_grace=$(get_hsm_param grace_delay)
3039         stack_trap "set_hsm_param grace_delay $old_grace" EXIT
3040         set_hsm_param grace_delay 100
3041
3042         md5sum $f >/dev/null &
3043         local pid=$!
3044         wait_request_state $fid RESTORE STARTED
3045
3046         kill -15 $pid
3047         sleep 1
3048
3049         # Check restore trigger process was killed
3050         local killed=$(ps -o pid,comm hp $pid >/dev/null)
3051
3052         $LFS hsm_cancel $f
3053
3054         # instead of waiting+checking both Restore and Cancel ops
3055         # sequentially, wait for both to be finished and then check
3056         # each results.
3057         wait_all_done 100 $fid
3058         local rstate=$(get_request_state $fid RESTORE)
3059         local cstate=$(get_request_state $fid CANCEL)
3060
3061         if [[ "$rstate" == "CANCELED" ]] ; then
3062                 [[ "$cstate" == "SUCCEED" ]] ||
3063                         error "Restore state is CANCELED and Cancel state " \
3064                                "is not SUCCEED but $cstate"
3065                 echo "Restore state is CANCELED, Cancel state is SUCCEED"
3066         elif [[ "$rstate" == "SUCCEED" ]] ; then
3067                 [[ "$cstate" == "FAILED" ]] ||
3068                         error "Restore state is SUCCEED and Cancel state " \
3069                                 "is not FAILED but $cstate"
3070                 echo "Restore state is SUCCEED, Cancel state is FAILED"
3071         else
3072                 error "Restore state is $rstate and Cancel state is $cstate"
3073         fi
3074
3075         [ -z $killed ] ||
3076                 error "Cannot kill process waiting for restore ($killed)"
3077 }
3078 run_test 33 "Kill a restore waiting process"
3079
3080 test_34() {
3081         # test needs a running copytool
3082         copytool setup -b 1
3083
3084         local f=$DIR/$tdir/$tfile
3085         local fid
3086         fid=$(make_custom_file_for_progress $f 39 1000000)
3087         [ $? != 0 ] && skip "not enough free space" && return
3088
3089         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3090         wait_request_state $fid ARCHIVE SUCCEED
3091         $LFS hsm_release $f
3092
3093         md5sum $f >/dev/null &
3094         local pid=$!
3095         wait_request_state $fid RESTORE STARTED
3096
3097         rm $f || error "rm $f failed"
3098         # rm must not block during restore
3099         wait_request_state $fid RESTORE STARTED
3100
3101         wait_request_state $fid RESTORE SUCCEED
3102         # check md5sum pgm finished
3103         local there=$(ps -o pid,comm hp $pid >/dev/null)
3104         [[ -z $there ]] || error "Restore initiator does not exit"
3105
3106         wait $pid || error "Restore initiator failed with $?"
3107 }
3108 run_test 34 "Remove file during restore"
3109
3110 test_35() {
3111         # test needs a running copytool
3112         copytool setup -b 1
3113
3114         local f=$DIR/$tdir/$tfile
3115         local f1=$DIR/$tdir/$tfile-1
3116         local fid
3117         fid=$(make_custom_file_for_progress $f 39 1000000)
3118         [ $? != 0 ] && skip "not enough free space" && return
3119
3120         local fid1=$(copy_file /etc/passwd $f1)
3121         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3122         wait_request_state $fid ARCHIVE SUCCEED
3123         $LFS hsm_release $f
3124
3125         md5sum $f >/dev/null &
3126         local pid=$!
3127         wait_request_state $fid RESTORE STARTED
3128
3129         mv $f1 $f || error "mv $f1 $f failed"
3130         # mv must not block during restore
3131         wait_request_state $fid RESTORE STARTED
3132
3133         wait_request_state $fid RESTORE SUCCEED
3134         # check md5sum pgm finished
3135         local there=$(ps -o pid,comm hp $pid >/dev/null)
3136         [[ -z $there ]] || error "Restore initiator does not exit"
3137
3138         wait $pid || error "Restore initiator failed with $?"
3139
3140         fid2=$(path2fid $f)
3141         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
3142 }
3143 run_test 35 "Overwrite file during restore"
3144
3145 test_36() {
3146         # test needs a running copytool
3147         copytool setup -b 1
3148
3149         local f=$DIR/$tdir/$tfile
3150         local fid
3151         fid=$(make_custom_file_for_progress $f 39 1000000)
3152         [ $? != 0 ] && skip "not enough free space" && return
3153
3154         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3155         wait_request_state $fid ARCHIVE SUCCEED
3156         $LFS hsm_release $f
3157
3158         md5sum $f >/dev/null &
3159         local pid=$!
3160         wait_request_state $fid RESTORE STARTED
3161
3162         mv $f $f.new
3163         # rm must not block during restore
3164         wait_request_state $fid RESTORE STARTED
3165
3166         wait_request_state $fid RESTORE SUCCEED
3167         # check md5sum pgm finished
3168         local there=$(ps -o pid,comm hp $pid >/dev/null)
3169         [[ -z $there ]] ||
3170                 error "Restore initiator does not exit"
3171
3172         wait $pid || error "Restore initiator failed with $?"
3173 }
3174 run_test 36 "Move file during restore"
3175
3176 test_37() {
3177         # LU-5683: check that an archived dirty file can be rearchived.
3178         copytool setup
3179
3180         local f=$DIR/$tdir/$tfile
3181         local fid
3182
3183         fid=$(create_small_file $f) || error "cannot create small file"
3184
3185         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3186         wait_request_state $fid ARCHIVE SUCCEED
3187         $LFS hsm_release $f || error "cannot release $f"
3188
3189         # Allow previous archive request to expire from the actions log.
3190         wait_for_grace_delay
3191
3192         # Dirty file.
3193         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
3194
3195         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3196         wait_request_state $fid ARCHIVE SUCCEED
3197 }
3198 run_test 37 "re-archive a dirty file"
3199
3200 multi_archive() {
3201         local prefix=$1
3202         local count=$2
3203         local n=""
3204
3205         for n in $(seq 1 $count); do
3206                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
3207         done
3208         echo "$count archive requests submitted"
3209 }
3210
3211 test_40() {
3212         local stream_count=4
3213         local file_count=100
3214         mkdir -p $DIR/$tdir
3215         local f=$DIR/$tdir/$tfile
3216         local i=""
3217         local p=""
3218         local fid=""
3219         local max_requests=$(get_hsm_param max_requests)
3220
3221         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3222         # Increase the number of HSM request that can be performed in
3223         # parallel. With the coordinator running once per second, this
3224         # also limits the number of requests per seconds that can be
3225         # performed, so we pick a decent number. But we also need to keep
3226         # that number low because the copytool has no rate limit and will
3227         # fail some requests if if gets too many at once.
3228         set_hsm_param max_requests 300
3229
3230         for i in $(seq 1 $file_count); do
3231                 for p in $(seq 1 $stream_count); do
3232                         fid=$(copy_file /etc/hosts $f.$p.$i)
3233                 done
3234         done
3235
3236         copytool setup
3237
3238         # to be sure wait_all_done will not be mislead by previous tests
3239         cdt_purge
3240         wait_for_grace_delay
3241         typeset -a pids
3242         # start archive streams in background (archive files in parallel)
3243         for p in $(seq 1 $stream_count); do
3244                 multi_archive $f.$p $file_count &
3245                 pids[$p]=$!
3246         done
3247         echo -n  "Wait for all requests being enqueued..."
3248         wait ${pids[*]}
3249         echo OK
3250         wait_all_done 100
3251 }
3252 run_test 40 "Parallel archive requests"
3253
3254 test_52() {
3255         # test needs a running copytool
3256         copytool setup
3257
3258         mkdir -p $DIR/$tdir
3259         local f=$DIR/$tdir/$tfile
3260         local fid=$(create_small_file $f)
3261
3262         $LFS hsm_archive $f || error "could not archive file"
3263         wait_request_state $fid ARCHIVE SUCCEED
3264         check_hsm_flags $f "0x00000009"
3265
3266         multiop_bg_pause $f O_c || error "multiop failed"
3267         local MULTIPID=$!
3268
3269         mds_evict_client
3270         client_up || client_up || true
3271
3272         kill -USR1 $MULTIPID
3273         wait $MULTIPID || error "multiop close failed"
3274
3275         check_hsm_flags $f "0x0000000b"
3276 }
3277 run_test 52 "Opened for write file on an evicted client should be set dirty"
3278
3279 test_53() {
3280         # test needs a running copytool
3281         copytool setup
3282
3283         mkdir -p $DIR/$tdir
3284         local f=$DIR/$tdir/$tfile
3285         local fid=$(create_small_file $f)
3286
3287         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3288                 error "could not archive file"
3289         wait_request_state $fid ARCHIVE SUCCEED
3290         check_hsm_flags $f "0x00000009"
3291
3292         multiop_bg_pause $f o_c || error "multiop failed"
3293         MULTIPID=$!
3294
3295         mds_evict_client
3296         client_up || client_up || true
3297
3298         kill -USR1 $MULTIPID
3299         wait $MULTIPID || error "multiop close failed"
3300
3301         check_hsm_flags $f "0x00000009"
3302 }
3303 run_test 53 "Opened for read file on an evicted client should not be set dirty"
3304
3305 test_54() {
3306         # test needs a running copytool
3307         copytool setup -b 1
3308
3309         local f=$DIR/$tdir/$tfile
3310         local fid=$(make_custom_file_for_progress $f 39 1000000)
3311
3312         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3313                 error "could not archive file"
3314         wait_request_state $fid ARCHIVE STARTED
3315
3316         check_hsm_flags $f "0x00000001"
3317
3318         stack_trap "cdt_clear_no_retry" EXIT
3319         # Avoid coordinator resending this request as soon it has failed.
3320         cdt_set_no_retry
3321
3322         echo "foo" >> $f
3323         sync
3324         wait_request_state $fid ARCHIVE FAILED
3325
3326         check_hsm_flags $f "0x00000003"
3327 }
3328 run_test 54 "Write during an archive cancels it"
3329
3330 test_55() {
3331         # test needs a running copytool
3332         copytool setup -b 1
3333
3334         local f=$DIR/$tdir/$tfile
3335         local fid=$(make_custom_file_for_progress $f 39 1000000)
3336
3337         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3338                 error "could not archive file"
3339         wait_request_state $fid ARCHIVE STARTED
3340
3341         check_hsm_flags $f "0x00000001"
3342
3343         stack_trap "cdt_clear_no_retry" EXIT
3344         # Avoid coordinator resending this request as soon it has failed.
3345         cdt_set_no_retry
3346
3347         $TRUNCATE $f 1024 || error "truncate failed"
3348         sync
3349         wait_request_state $fid ARCHIVE FAILED
3350
3351         check_hsm_flags $f "0x00000003"
3352 }
3353 run_test 55 "Truncate during an archive cancels it"
3354
3355 test_56() {
3356         # test needs a running copytool
3357         copytool setup -b 1
3358
3359         local f=$DIR/$tdir/$tfile
3360         local fid
3361         fid=$(make_custom_file_for_progress $f 39 1000000)
3362         [ $? != 0 ] && skip "not enough free space" && return
3363
3364         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3365                 error "could not archive file"
3366         wait_request_state $fid ARCHIVE STARTED
3367
3368         check_hsm_flags $f "0x00000001"
3369
3370         # Change metadata and sync to be sure we are not changing only
3371         # in memory.
3372         chmod 644 $f
3373         chgrp sys $f
3374         sync
3375         wait_request_state $fid ARCHIVE SUCCEED
3376
3377         check_hsm_flags $f "0x00000009"
3378 }
3379 run_test 56 "Setattr during an archive is ok"
3380
3381 test_57() {
3382         # Need one client for I/O, one for request
3383         needclients 2 || return 0
3384
3385         # test needs a running copytool
3386         copytool setup
3387
3388         mkdir -p $DIR/$tdir
3389         local f=$DIR/$tdir/test_archive_remote
3390         # Create a file on a remote node
3391         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
3392                 "count=2 conv=fsync"
3393
3394         # And archive it
3395         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
3396                 error "hsm_archive failed"
3397         local fid=$(path2fid $f)
3398         wait_request_state $fid ARCHIVE SUCCEED
3399
3400         # Release and implicit restore it
3401         do_node $CLIENT2 "$LFS hsm_release $f" ||
3402                 error "hsm_release failed"
3403         do_node $CLIENT2 "md5sum $f" ||
3404                 error "hsm_restore failed"
3405
3406         wait_request_state $fid RESTORE SUCCEED
3407 }
3408 run_test 57 "Archive a file with dirty cache on another node"
3409
3410 truncate_released_file() {
3411         local src_file=$1
3412         local trunc_to=$2
3413
3414         local sz=$(stat -c %s $src_file)
3415         local f=$DIR/$tdir/$tfile
3416         local fid=$(copy_file $1 $f)
3417         local ref=$f-ref
3418         cp $f $f-ref
3419
3420         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3421                 error "could not archive file"
3422         wait_request_state $fid ARCHIVE SUCCEED
3423
3424         $LFS hsm_release $f || error "could not release file"
3425
3426         $TRUNCATE $f $trunc_to || error "truncate failed"
3427         sync
3428
3429         local sz1=$(stat -c %s $f)
3430         [[ $sz1 == $trunc_to ]] ||
3431                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3432
3433         $LFS hsm_state $f
3434         check_hsm_flags $f "0x0000000b"
3435
3436         local state=$(get_request_state $fid RESTORE)
3437         [[ "$state" == "SUCCEED" ]] ||
3438                 error "truncate $sz does not trig restore, state = $state"
3439
3440         $TRUNCATE $ref $trunc_to
3441         cmp $ref $f || error "file data wrong after truncate"
3442
3443         rm -f $f $f-ref
3444 }
3445
3446 test_58() {
3447         # test needs a running copytool
3448         copytool setup
3449
3450         mkdir -p $DIR/$tdir
3451
3452         local sz=$(stat -c %s /etc/passwd)
3453
3454         echo "truncate up from $sz to $((sz*2))"
3455         truncate_released_file /etc/passwd $((sz*2))
3456
3457         echo "truncate down from $sz to $((sz/2))"
3458         truncate_released_file /etc/passwd $((sz/2))
3459
3460         echo "truncate to 0"
3461         truncate_released_file /etc/passwd 0
3462 }
3463 run_test 58 "Truncate a released file will trigger restore"
3464
3465 test_59() {
3466         local fid
3467         local server_version=$(lustre_version_code $SINGLEMDS)
3468         [[ $server_version -lt $(version_code 2.7.63) ]] &&
3469                 skip "Need MDS version at least 2.7.63" && return
3470
3471         copytool setup
3472         $MCREATE $DIR/$tfile || error "mcreate failed"
3473         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3474         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3475         fid=$(path2fid $DIR/$tfile)
3476         wait_request_state $fid ARCHIVE SUCCEED
3477         $LFS hsm_release $DIR/$tfile || error "release failed"
3478 }
3479 run_test 59 "Release stripeless file with non-zero size"
3480
3481 test_60() {
3482         # This test validates the fix for LU-4512. Ensure that the -u
3483         # option changes the progress reporting interval from the
3484         # default (30 seconds) to the user-specified interval.
3485         local interval=5
3486         local progress_timeout=$((interval * 4))
3487
3488         copytool setup -b 1 --update-interval $interval
3489
3490         local f=$DIR/$tdir/$tfile
3491         local fid
3492         fid=$(make_custom_file_for_progress $f 10)
3493         [ $? != 0 ] && skip "not enough free space" && return
3494
3495         local mdtidx=0
3496         local mdt=${MDT_PREFIX}${mdtidx}
3497         local mds=mds$((mdtidx + 1))
3498
3499         # Wait for copytool to register
3500         wait_update_facet $mds \
3501                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3502                 uuid 100 || error "coyptool failed to register with $mdt"
3503
3504         local start_at=$(date +%s)
3505         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3506                 error "could not archive file"
3507
3508         local agent=$(facet_active_host $SINGLEAGT)
3509         local logfile=$(copytool_logfile $SINGLEAGT)
3510
3511         wait_update $agent \
3512             "grep -o start.copy \"$logfile\"" "start copy" 100 ||
3513                 error "copytool failed to start"
3514
3515         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3516         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3517
3518         local RESULT
3519         local WAIT=0
3520         local sleep=1
3521
3522         echo -n "Expecting a progress update within $progress_timeout seconds... "
3523         while [ true ]; do
3524                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3525                 if [ -n "$RESULT" ] && [ "$RESULT" -gt 0 ]; then
3526                         echo "$RESULT bytes copied in $WAIT seconds."
3527                         break
3528                 elif [ $WAIT -ge $progress_timeout ]; then
3529                         error "Timed out waiting for progress update!"
3530                         break
3531                 fi
3532                 WAIT=$((WAIT + sleep))
3533                 sleep $sleep
3534         done
3535
3536         local finish_at=$(date +%s)
3537         local elapsed=$((finish_at - start_at))
3538
3539         # Ensure that the progress update occurred within the expected window.
3540         if [ $elapsed -lt $((interval - 1)) ]; then
3541                 error "Expected progress update after at least $interval seconds"
3542         fi
3543
3544         echo "Wait for on going archive hsm action to complete"
3545         wait_update $agent "grep -o copied \"$logfile\"" "copied" 10 ||
3546                 echo "File archiving not completed even after 10 secs"
3547 }
3548 run_test 60 "Changing progress update interval from default"
3549
3550 test_61() {
3551         # test needs a running copytool
3552         copytool setup
3553
3554         mkdir -p $DIR/$tdir
3555         local f=$DIR/$tdir/$tfile
3556         local fid=$(copy_file /etc/passwd $f)
3557         cdt_disable
3558         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3559         rm -f $f
3560         cdt_enable
3561         wait_request_state $fid ARCHIVE FAILED
3562 }
3563 run_test 61 "Waiting archive of a removed file should fail"
3564
3565 test_70() {
3566         # test needs a new running copytool
3567         stack_trap copytool_monitor_cleanup EXIT
3568         copytool_monitor_setup
3569         copytool setup --event-fifo "$HSMTOOL_MONITOR_DIR/fifo"
3570
3571         # Wait for the copytool to register.
3572         wait_update --verbose $(facet_active_host mds1) \
3573                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3574                 uuid 100 ||
3575                 error "copytool failed to register with MDT0000"
3576
3577         kill_copytools
3578         wait_copytools || error "Copytools failed to stop"
3579
3580         local REGISTER_EVENT
3581         local UNREGISTER_EVENT
3582         while read event; do
3583                 local parsed=$(parse_json_event "$event")
3584                 if [ -z "$parsed" ]; then
3585                         error "Copytool sent malformed event: $event"
3586                 fi
3587                 eval $parsed
3588
3589                 if [ $event_type == "REGISTER" ]; then
3590                         REGISTER_EVENT=$event
3591                 elif [ $event_type == "UNREGISTER" ]; then
3592                         UNREGISTER_EVENT=$event
3593                 fi
3594         done < <(echo $"$(get_copytool_event_log)")
3595
3596         if [ -z "$REGISTER_EVENT" ]; then
3597                 error "Copytool failed to send register event to FIFO"
3598         fi
3599
3600         if [ -z "$UNREGISTER_EVENT" ]; then
3601                 error "Copytool failed to send unregister event to FIFO"
3602         fi
3603
3604         echo "Register/Unregister events look OK."
3605 }
3606 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3607
3608 test_71() {
3609         # Bump progress interval for livelier events.
3610         local interval=5
3611
3612         # test needs a new running copytool
3613         stack_trap copytool_monitor_cleanup EXIT
3614         copytool_monitor_setup
3615         copytool setup --update-interval $interval --event-fifo \
3616                 "$HSMTOOL_MONITOR_DIR/fifo"
3617
3618         stack_trap "cdt_clear_no_retry" EXIT
3619         # Just start and stop the copytool to generate events.
3620         cdt_clear_no_retry
3621
3622         local f=$DIR/$tdir/$tfile
3623         local fid
3624         fid=$(make_custom_file_for_progress $f 39 1000000)
3625         [ $? != 0 ] && skip "not enough free space" && return
3626
3627         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3628                 error "could not archive file"
3629         wait_request_state $fid ARCHIVE SUCCEED
3630
3631         local expected_fields="event_time data_fid source_fid"
3632         expected_fields+=" total_bytes current_bytes"
3633
3634         local START_EVENT
3635         local FINISH_EVENT
3636         while read event; do
3637                 # Make sure we're not getting anything from previous events.
3638                 for field in $expected_fields; do
3639                         unset $field
3640                 done
3641
3642                 local parsed=$(parse_json_event "$event")
3643                 if [ -z "$parsed" ]; then
3644                         error "Copytool sent malformed event: $event"
3645                 fi
3646                 eval $parsed
3647
3648                 if [ $event_type == "ARCHIVE_START" ]; then
3649                         START_EVENT=$event
3650                         continue
3651                 elif [ $event_type == "ARCHIVE_FINISH" ]; then
3652                         FINISH_EVENT=$event
3653                         continue
3654                 elif [ $event_type != "ARCHIVE_RUNNING" ]; then
3655                         continue
3656                 fi
3657
3658                 # Do some simple checking of the progress update events.
3659                 for expected_field in $expected_fields; do
3660                         if [ -z ${!expected_field+x} ]; then
3661                                 error "Missing $expected_field field in event"
3662                         fi
3663                 done
3664
3665                 if [ $total_bytes -eq 0 ]; then
3666                         error "Expected total_bytes to be > 0"
3667                 fi
3668
3669                 # These should be identical throughout an archive
3670                 # operation.
3671                 if [ $source_fid != $data_fid ]; then
3672                         error "Expected source_fid to equal data_fid"
3673                 fi
3674         done < <(echo $"$(get_copytool_event_log)")
3675
3676         if [ -z "$START_EVENT" ]; then
3677                 error "Copytool failed to send archive start event to FIFO"
3678         fi
3679
3680         if [ -z "$FINISH_EVENT" ]; then
3681                 error "Copytool failed to send archive finish event to FIFO"
3682         fi
3683
3684         echo "Archive events look OK."
3685 }
3686 run_test 71 "Copytool logs JSON archive events to FIFO"
3687
3688 test_72() {
3689         # Bump progress interval for livelier events.
3690         local interval=5
3691
3692         # test needs a new running copytool
3693         copytool_cleanup
3694         copytool_monitor_setup
3695         HSMTOOL_UPDATE_INTERVAL=$interval \
3696         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3697         local test_file=$HSMTOOL_MONITOR_DIR/file
3698
3699         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3700         cmd+="conv=fsync"
3701         do_facet $SINGLEAGT "$cmd" ||
3702                 error "cannot create $test_file on $SINGLEAGT"
3703         copy2archive $test_file $tdir/$tfile
3704
3705         mkdir -p $DIR/$tdir
3706         local f=$DIR/$tdir/$tfile
3707         import_file $tdir/$tfile $f
3708         f=$DIR2/$tdir/$tfile
3709         echo "Verifying released state: "
3710         check_hsm_flags $f "0x0000000d"
3711
3712         local fid=$(path2fid $f)
3713         $LFS hsm_restore $f
3714         wait_request_state $fid RESTORE SUCCEED
3715
3716         local expected_fields="event_time data_fid source_fid"
3717         expected_fields+=" total_bytes current_bytes"
3718
3719         local START_EVENT
3720         local FINISH_EVENT
3721         while read event; do
3722                 # Make sure we're not getting anything from previous events.
3723                 for field in $expected_fields; do
3724                         unset $field
3725                 done
3726
3727                 local parsed=$(parse_json_event "$event")
3728                 if [ -z "$parsed" ]; then
3729                         error "Copytool sent malformed event: $event"
3730                 fi
3731                 eval $parsed
3732
3733                 if [ $event_type == "RESTORE_START" ]; then
3734                         START_EVENT=$event
3735                         if [ $source_fid != $data_fid ]; then
3736                                 error "source_fid should == data_fid at start"
3737                         fi
3738                         continue
3739                 elif [ $event_type == "RESTORE_FINISH" ]; then
3740                         FINISH_EVENT=$event
3741                         if [ $source_fid != $data_fid ]; then
3742                                 error "source_fid should == data_fid at finish"
3743                         fi
3744                         continue
3745                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3746                         continue
3747                 fi
3748
3749                 # Do some simple checking of the progress update events.
3750                 for expected_field in $expected_fields; do
3751                         if [ -z ${!expected_field+x} ]; then
3752                                 error "Missing $expected_field field in event"
3753                         fi
3754                 done
3755
3756                 if [ $total_bytes -eq 0 ]; then
3757                         error "Expected total_bytes to be > 0"
3758                 fi
3759
3760                 # When a restore starts out, the data fid is the same as the
3761                 # source fid. After the restore has gotten going, we learn
3762                 # the new data fid. Once the restore has finished, the source
3763                 # fid is set to the new data fid.
3764                 #
3765                 # We test this because some monitoring software may depend on
3766                 # this behavior. If it changes, then the consumers of these
3767                 # events may need to be modified.
3768                 if [ $source_fid == $data_fid ]; then
3769                         error "source_fid should != data_fid during restore"
3770                 fi
3771         done < <(echo $"$(get_copytool_event_log)")
3772
3773         if [ -z "$START_EVENT" ]; then
3774                 error "Copytool failed to send restore start event to FIFO"
3775         fi
3776
3777         if [ -z "$FINISH_EVENT" ]; then
3778                 error "Copytool failed to send restore finish event to FIFO"
3779         fi
3780
3781         echo "Restore events look OK."
3782
3783         cdt_clear_no_retry
3784         copytool_cleanup
3785         copytool_monitor_cleanup
3786
3787         rm -rf $test_dir
3788 }
3789 run_test 72 "Copytool logs JSON restore events to FIFO"
3790
3791 test_90() {
3792         file_count=51 # Max number of files constrained by LNET message size
3793         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3794         local f=$DIR/$tdir/$tfile
3795         local FILELIST=/tmp/filelist.txt
3796         local i=""
3797
3798         rm -f $FILELIST
3799         for i in $(seq 1 $file_count); do
3800                 fid=$(copy_file /etc/hosts $f.$i)
3801                 echo $f.$i >> $FILELIST
3802         done
3803
3804         copytool setup
3805         # to be sure wait_all_done will not be mislead by previous tests
3806         cdt_purge
3807         wait_for_grace_delay
3808         $LFS hsm_archive --filelist $FILELIST ||
3809                 error "cannot archive a file list"
3810         wait_all_done 100
3811         $LFS hsm_release --filelist $FILELIST ||
3812                 error "cannot release a file list"
3813         $LFS hsm_restore --filelist $FILELIST ||
3814                 error "cannot restore a file list"
3815         wait_all_done 100
3816 }
3817 run_test 90 "Archive/restore a file list"
3818
3819 double_verify_reset_hsm_param() {
3820         local p=$1
3821         echo "Testing $HSM_PARAM.$p"
3822         local val=$(get_hsm_param $p)
3823         local save=$val
3824         local val2=$(($val * 2))
3825         set_hsm_param $p $val2
3826         val=$(get_hsm_param $p)
3827         [[ $val == $val2 ]] ||
3828                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3829         echo "Set $p to 0 must failed"
3830         set_hsm_param $p 0
3831         local rc=$?
3832         # restore value
3833         set_hsm_param $p $save
3834
3835         if [[ $rc == 0 ]]; then
3836                 error "we must not be able to set $HSM_PARAM.$p to 0"
3837         fi
3838 }
3839
3840 test_100() {
3841         double_verify_reset_hsm_param loop_period
3842         double_verify_reset_hsm_param grace_delay
3843         double_verify_reset_hsm_param active_request_timeout
3844         double_verify_reset_hsm_param max_requests
3845         double_verify_reset_hsm_param default_archive_id
3846 }
3847 run_test 100 "Set coordinator /proc tunables"
3848
3849 test_102() {
3850         cdt_disable
3851         cdt_enable
3852         cdt_restart
3853 }
3854 run_test 102 "Verify coordinator control"
3855
3856 test_103() {
3857         # test needs a running copytool
3858         copytool setup
3859
3860         local i=""
3861         local fid=""
3862
3863         mkdir -p $DIR/$tdir
3864         for i in $(seq 1 20); do
3865                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3866         done
3867         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3868
3869         cdt_purge
3870
3871         echo "Current requests"
3872         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3873                         $HSM_PARAM.actions |\
3874                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3875
3876         [[ -z "$res" ]] || error "Some request have not been canceled"
3877 }
3878 run_test 103 "Purge all requests"
3879
3880 DATA=CEA
3881 DATAHEX='[434541]'
3882 test_104() {
3883         local f=$DIR/$tdir/$tfile
3884         local fid
3885         fid=$(make_custom_file_for_progress $f 39 1000000)
3886         [ $? != 0 ] && skip "not enough free space" && return
3887
3888         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3889         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3890                         $HSM_PARAM.actions |\
3891                         grep $fid | cut -f16 -d=")
3892
3893         [[ "$data1" == "$DATAHEX" ]] ||
3894                 error "Data field in records is ($data1) and not ($DATAHEX)"
3895
3896         # archive the file
3897         copytool setup
3898
3899         wait_request_state $fid ARCHIVE SUCCEED
3900 }
3901 run_test 104 "Copy tool data field"
3902
3903 test_105() {
3904         local max_requests=$(get_hsm_param max_requests)
3905         mkdir -p $DIR/$tdir
3906         local i=""
3907
3908         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3909         set_hsm_param max_requests 300
3910
3911         cdt_disable
3912         for i in $(seq -w 1 10); do
3913                 cp /etc/passwd $DIR/$tdir/$i
3914                 $LFS hsm_archive $DIR/$tdir/$i
3915         done
3916         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3917                         $HSM_PARAM.actions |\
3918                         grep WAITING | wc -l")
3919         cdt_restart
3920
3921         cdt_disable
3922         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3923                         $HSM_PARAM.actions |\
3924                         grep WAITING | wc -l")
3925         cdt_enable
3926         cdt_purge
3927         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3928                 error "Requests count after shutdown $reqcnt2 != "\
3929                       "before shutdown $reqcnt1"
3930 }
3931 run_test 105 "Restart of coordinator"
3932
3933 test_106() {
3934         # test needs a running copytool
3935         copytool setup
3936
3937         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3938
3939         check_agent_registered $uuid
3940
3941         search_copytools || error "No copytool found"
3942
3943         kill_copytools
3944         wait_copytools || error "Copytool failed to stop"
3945
3946         check_agent_unregistered $uuid
3947
3948         copytool setup
3949         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3950         check_agent_registered $uuid
3951 }
3952 run_test 106 "Copytool register/unregister"
3953
3954 test_107() {
3955         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
3956
3957         # test needs a running copytool
3958         copytool setup
3959         # create and archive file
3960         mkdir -p $DIR/$tdir
3961         local f1=$DIR/$tdir/$tfile
3962         local fid=$(copy_file /etc/passwd $f1)
3963         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3964         wait_request_state $fid ARCHIVE SUCCEED
3965         # shutdown and restart MDS
3966         fail $SINGLEMDS
3967         # check the copytool still gets messages from MDT
3968         local f2=$DIR/$tdir/2
3969         local fid=$(copy_file /etc/passwd $f2)
3970         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3971         # main check of this sanity: this request MUST succeed
3972         wait_request_state $fid ARCHIVE SUCCEED
3973 }
3974 run_test 107 "Copytool re-register after MDS restart"
3975
3976 policy_set_and_test()
3977 {
3978         local change="$1"
3979         local target="$2"
3980         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3981         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3982         [[ "$policy" == "$target" ]] ||
3983                 error "Wrong policy after '$change': '$policy' != '$target'"
3984 }
3985
3986 test_109() {
3987         # to force default policy setting if error
3988         CDT_POLICY_HAD_CHANGED=true
3989
3990         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3991         local default="NonBlockingRestore [NoRetryAction]"
3992         [[ "$policy" == "$default" ]] ||
3993                 error "default policy has changed,"\
3994                       " '$policy' != '$default' update the test"
3995         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3996         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3997         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3998         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3999         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
4000         # useless bacause we know but safer for futur changes to use real value
4001         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
4002         echo "Next set_param must failed"
4003         policy_set_and_test "wrong" "$policy"
4004
4005         # return to default
4006         echo "Back to default policy"
4007         cdt_set_sanity_policy
4008 }
4009 run_test 109 "Policy display/change"
4010
4011 test_110a() {
4012         # test needs a running copytool
4013         copytool_setup
4014
4015         mkdir -p $DIR/$tdir
4016
4017         copy2archive /etc/passwd $tdir/$tfile
4018
4019         local f=$DIR/$tdir/$tfile
4020         import_file $tdir/$tfile $f
4021         local fid=$(path2fid $f)
4022
4023         cdt_set_non_blocking_restore
4024         md5sum $f
4025         local st=$?
4026
4027         # cleanup
4028         wait_request_state $fid RESTORE SUCCEED
4029         cdt_clear_non_blocking_restore
4030
4031         # Test result
4032         [[ $st == 1 ]] ||
4033                 error "md5sum returns $st != 1, "\
4034                         "should also perror ENODATA (No data available)"
4035 }
4036 run_test 110a "Non blocking restore policy (import case)"
4037
4038 test_110b() {
4039         # test needs a running copytool
4040         copytool setup
4041
4042         mkdir -p $DIR/$tdir
4043         local f=$DIR/$tdir/$tfile
4044         local fid=$(copy_file /etc/passwd $f)
4045         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4046         wait_request_state $fid ARCHIVE SUCCEED
4047         $LFS hsm_release $f
4048
4049         cdt_set_non_blocking_restore
4050         md5sum $f
4051         local st=$?
4052
4053         # cleanup
4054         wait_request_state $fid RESTORE SUCCEED
4055         cdt_clear_non_blocking_restore
4056
4057         # Test result
4058         [[ $st == 1 ]] ||
4059                 error "md5sum returns $st != 1, "\
4060                         "should also perror ENODATA (No data available)"
4061 }
4062 run_test 110b "Non blocking restore policy (release case)"
4063
4064 test_111a() {
4065         # test needs a running copytool
4066         copytool_setup
4067
4068         mkdir -p $DIR/$tdir
4069         copy2archive /etc/passwd $tdir/$tfile
4070
4071         local f=$DIR/$tdir/$tfile
4072
4073         import_file $tdir/$tfile $f
4074         local fid=$(path2fid $f)
4075
4076         cdt_set_no_retry
4077
4078         copytool_remove_backend $fid
4079
4080         $LFS hsm_restore $f
4081         wait_request_state $fid RESTORE FAILED
4082         local st=$?
4083
4084         # cleanup
4085         cdt_clear_no_retry
4086
4087         # Test result
4088         [[ $st == 0 ]] || error "Restore does not failed"
4089
4090         copytool_cleanup
4091 }
4092 run_test 111a "No retry policy (import case), restore will error"\
4093               " (No such file or directory)"
4094
4095 test_111b() {
4096         # test needs a running copytool
4097         copytool_setup
4098
4099         mkdir -p $DIR/$tdir
4100         local f=$DIR/$tdir/$tfile
4101         local fid=$(copy_file /etc/passwd $f)
4102         cdt_set_no_retry
4103         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4104         wait_request_state $fid ARCHIVE SUCCEED
4105         $LFS hsm_release $f
4106
4107         copytool_remove_backend $fid
4108
4109         $LFS hsm_restore $f
4110         wait_request_state $fid RESTORE FAILED
4111         local st=$?
4112
4113         # cleanup
4114         cdt_clear_no_retry
4115
4116         # Test result
4117         [[ $st == 0 ]] || error "Restore does not failed"
4118
4119         copytool_cleanup
4120 }
4121 run_test 111b "No retry policy (release case), restore will error"\
4122               " (No such file or directory)"
4123
4124 test_112() {
4125         # test needs a running copytool
4126         copytool setup
4127
4128         mkdir -p $DIR/$tdir
4129         local f=$DIR/$tdir/$tfile
4130         local fid=$(copy_file /etc/passwd $f)
4131         cdt_disable
4132         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4133         local l=$($LFS hsm_action $f)
4134         echo $l
4135         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
4136
4137         cdt_enable
4138         wait_request_state $fid ARCHIVE SUCCEED
4139
4140         # Test result
4141         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
4142 }
4143 run_test 112 "State of recorded request"
4144
4145 test_200() {
4146         # test needs a running copytool
4147         copytool setup
4148
4149         local f=$DIR/$tdir/$tfile
4150         local fid
4151         fid=$(make_custom_file_for_progress $f 103 1048576)
4152         [ $? != 0 ] && skip "not enough free space" && return
4153
4154         # test with cdt on is made in test_221
4155         cdt_disable
4156         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4157         # wait archive to register at CDT
4158         wait_request_state $fid ARCHIVE WAITING
4159         $LFS hsm_cancel $f
4160         cdt_enable
4161         wait_request_state $fid ARCHIVE CANCELED
4162         wait_request_state $fid CANCEL SUCCEED
4163 }
4164 run_test 200 "Register/Cancel archive"
4165
4166 test_201() {
4167         # test needs a running copytool
4168         copytool_setup
4169
4170         local f=$DIR/$tdir/$tfile
4171         create_archive_file $tdir/$tfile
4172         import_file $tdir/$tfile $f
4173         local fid=$(path2fid $f)
4174
4175         # test with cdt on is made in test_222
4176         cdt_disable
4177         $LFS hsm_restore $f
4178         # wait restore to register at CDT
4179         wait_request_state $fid RESTORE WAITING
4180         $LFS hsm_cancel $f
4181         cdt_enable
4182         wait_request_state $fid RESTORE CANCELED
4183         wait_request_state $fid CANCEL SUCCEED
4184
4185         copytool_cleanup
4186 }
4187 run_test 201 "Register/Cancel restore"
4188
4189 test_202() {
4190         # test needs a running copytool
4191         copytool setup
4192
4193         local f=$DIR/$tdir/$tfile
4194         local fid
4195         fid=$(make_custom_file_for_progress $f 39 1000000)
4196         [ $? != 0 ] && skip "not enough free space" && return
4197
4198         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4199         wait_request_state $fid ARCHIVE SUCCEED
4200
4201         cdt_disable
4202         $LFS hsm_remove $f
4203         # wait remove to register at CDT
4204         wait_request_state $fid REMOVE WAITING
4205         $LFS hsm_cancel $f
4206         cdt_enable
4207         wait_request_state $fid REMOVE CANCELED
4208 }
4209 run_test 202 "Register/Cancel remove"
4210
4211 test_220A() { # was test_220
4212         # test needs a running copytool
4213         copytool setup
4214
4215         mkdir -p $DIR/$tdir
4216
4217         local f=$DIR/$tdir/$tfile
4218         local fid=$(copy_file /etc/passwd $f)
4219
4220         changelog_setup
4221
4222         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4223         wait_request_state $fid ARCHIVE SUCCEED
4224
4225         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4226         changelog_cleanup
4227
4228         local target=0x0
4229         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4230 }
4231 run_test 220A "Changelog for archive"
4232
4233 test_220a() {
4234         # test needs a running copytool
4235         copytool setup
4236
4237         mkdir -p $DIR/$tdir
4238
4239         local f=$DIR/$tdir/$tfile
4240         local fid=$(copy_file /etc/passwd $f)
4241
4242         changelog_setup
4243
4244         # block copytool operations to allow for HSM request to be
4245         # submitted and file be unlinked (CDT will find object removed)
4246         copytool_suspend
4247
4248         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4249
4250         # wait request to reach CT
4251         wait_request_state $fid ARCHIVE STARTED
4252
4253         rm -f $f
4254
4255         copytool_continue
4256
4257         wait_request_state $fid ARCHIVE FAILED
4258
4259         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4260         changelog_cleanup
4261
4262         # HE_ARCHIVE|ENOENT
4263         local target=0x2
4264         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4265 }
4266 run_test 220a "Changelog for failed archive"
4267
4268 test_221() {
4269         # test needs a running copytool
4270         copytool setup -b 1
4271
4272         local f=$DIR/$tdir/$tfile
4273         local fid
4274         fid=$(make_custom_file_for_progress $f 103 1048576)
4275         [ $? != 0 ] && skip "not enough free space" && return
4276
4277         changelog_setup
4278
4279         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4280         wait_request_state $fid ARCHIVE STARTED
4281         $LFS hsm_cancel $f
4282         wait_request_state $fid ARCHIVE CANCELED
4283         wait_request_state $fid CANCEL SUCCEED
4284
4285         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4286
4287         local target=0x7d
4288         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4289 }
4290 run_test 221 "Changelog for archive canceled"
4291
4292 test_222a() {
4293         # test needs a running copytool
4294         copytool_setup
4295
4296         mkdir -p $DIR/$tdir
4297         copy2archive /etc/passwd $tdir/$tfile
4298
4299         local f=$DIR/$tdir/$tfile
4300         import_file $tdir/$tfile $f
4301         local fid=$(path2fid $f)
4302
4303         changelog_setup
4304
4305         $LFS hsm_restore $f
4306         wait_request_state $fid RESTORE SUCCEED
4307
4308         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4309
4310         local target=0x80
4311         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4312
4313         copytool_cleanup
4314 }
4315 run_test 222a "Changelog for explicit restore"
4316
4317 test_222b() {
4318         # test needs a running copytool
4319         copytool setup
4320
4321         mkdir -p $DIR/$tdir
4322         local f=$DIR/$tdir/$tfile
4323         local fid=$(copy_file /etc/passwd $f)
4324
4325         changelog_setup
4326         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4327         wait_request_state $fid ARCHIVE SUCCEED
4328         $LFS hsm_release $f
4329
4330         md5sum $f
4331
4332         wait_request_state $fid RESTORE SUCCEED
4333
4334         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4335
4336         local target=0x80
4337         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4338 }
4339 run_test 222b "Changelog for implicit restore"
4340
4341 test_222c() {
4342         # test needs a running copytool
4343         copytool_setup
4344
4345         mkdir -p $DIR/$tdir
4346         copy2archive /etc/passwd $tdir/$tfile
4347
4348         local f=$DIR/$tdir/$tfile
4349         import_file $tdir/$tfile $f
4350         local fid=$(path2fid $f)
4351
4352         changelog_setup
4353
4354         # block copytool operations to allow for HSM request to be
4355         # submitted and file be unlinked (CDT will find object removed)
4356         copytool_suspend
4357
4358         $LFS hsm_restore $f
4359
4360         # wait request to reach CT
4361         wait_request_state $fid RESTORE STARTED
4362
4363         rm -f $f
4364
4365         copytool_continue
4366
4367         wait_request_state $fid RESTORE FAILED
4368
4369         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4370
4371         # HE_RESTORE|ENOENT
4372         local target=0x82
4373         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4374
4375         copytool_cleanup
4376 }
4377 run_test 222c "Changelog for failed explicit restore"
4378
4379 test_222d() {
4380         # test needs a running copytool
4381         copytool_setup
4382
4383         mkdir -p $DIR/$tdir
4384         local f=$DIR/$tdir/$tfile
4385         local fid=$(copy_file /etc/passwd $f)
4386
4387         changelog_setup
4388         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4389         wait_request_state $fid ARCHIVE SUCCEED
4390         $LFS hsm_release $f
4391
4392         copytool_remove_backend $fid
4393         md5sum $f
4394
4395         wait_request_state $fid RESTORE FAILED
4396
4397         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4398
4399         # HE_RESTORE|ENOENT
4400         local target=0x82
4401         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4402
4403         copytool_cleanup
4404 }
4405 run_test 222d "Changelog for failed implicit restore"
4406
4407 test_223a() {
4408         # test needs a running copytool
4409         copytool_setup
4410
4411         local f=$DIR/$tdir/$tfile
4412         create_archive_file $tdir/$tfile
4413
4414         changelog_setup
4415
4416         import_file $tdir/$tfile $f
4417         local fid=$(path2fid $f)
4418
4419         $LFS hsm_restore $f
4420         wait_request_state $fid RESTORE STARTED
4421         $LFS hsm_cancel $f
4422         wait_request_state $fid RESTORE CANCELED
4423         wait_request_state $fid CANCEL SUCCEED
4424
4425         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4426
4427         local target=0xfd
4428         [[ $flags == $target ]] ||
4429                 error "Changelog flag is $flags not $target"
4430
4431         cleanup
4432 }
4433 run_test 223a "Changelog for restore canceled (import case)"
4434
4435 test_223b() {
4436         # test needs a running copytool
4437         copytool setup -b 1
4438
4439         local f=$DIR/$tdir/$tfile
4440         local fid
4441         fid=$(make_custom_file_for_progress $f 39 1000000)
4442         [ $? != 0 ] && skip "not enough free space" && return
4443
4444         changelog_setup
4445         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4446         wait_request_state $fid ARCHIVE SUCCEED
4447         $LFS hsm_release $f
4448         $LFS hsm_restore $f
4449         wait_request_state $fid RESTORE STARTED
4450         $LFS hsm_cancel $f
4451         wait_request_state $fid RESTORE CANCELED
4452         wait_request_state $fid CANCEL SUCCEED
4453
4454         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4455
4456         local target=0xfd
4457         [[ $flags == $target ]] ||
4458                 error "Changelog flag is $flags not $target"
4459
4460         copytool_cleanup
4461 }
4462 run_test 223b "Changelog for restore canceled (release case)"
4463
4464 test_224A() { # was test_224
4465         # test needs a running copytool
4466         copytool setup
4467
4468         mkdir -p $DIR/$tdir
4469
4470         local f=$DIR/$tdir/$tfile
4471         local fid=$(copy_file /etc/passwd $f)
4472
4473         changelog_setup
4474         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4475         wait_request_state $fid ARCHIVE SUCCEED
4476
4477         $LFS hsm_remove $f
4478         wait_request_state $fid REMOVE SUCCEED
4479
4480         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4481
4482         local target=0x200
4483         [[ $flags == $target ]] ||
4484                 error "Changelog flag is $flags not $target"
4485 }
4486 run_test 224A "Changelog for remove"
4487
4488 test_224a() {
4489         # test needs a running copytool
4490         copytool_setup
4491
4492         mkdir -p $DIR/$tdir
4493
4494         local f=$DIR/$tdir/$tfile
4495         local fid=$(copy_file /etc/passwd $f)
4496
4497         changelog_setup
4498         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4499         wait_request_state $fid ARCHIVE SUCCEED
4500
4501         copytool_remove_backend $fid
4502
4503         # block copytool operations to allow for HSM request to be
4504         # submitted and file be unlinked (CDT will find object removed)
4505         copytool_suspend
4506
4507         $LFS hsm_remove $f
4508
4509         # wait for request to reach CT
4510         wait_request_state $fid REMOVE STARTED
4511
4512         rm -f $f
4513
4514         copytool_continue
4515
4516         wait_request_state $fid REMOVE FAILED
4517
4518         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4519
4520         # HE_REMOVE|ENOENT
4521         local target=0x202
4522         [[ $flags == $target ]] ||
4523                 error "Changelog flag is $flags not $target"
4524
4525         cleanup
4526 }
4527 run_test 224a "Changelog for failed remove"
4528
4529 test_225() {
4530         # test needs a running copytool
4531         copytool setup
4532
4533         # test is not usable because remove request is too fast
4534         # so it is always finished before cancel can be done ...
4535         echo "Test disabled"
4536         copytool_cleanup
4537         return 0
4538
4539         local f=$DIR/$tdir/$tfile
4540         local fid
4541         fid=$(make_custom_file_for_progress $f 39 1000000)
4542         [ $? != 0 ] && skip "not enough free space" && return
4543
4544         changelog_setup
4545         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4546         wait_request_state $fid ARCHIVE SUCCEED
4547
4548         # if cdt is on, it can serve too quickly the request
4549         cdt_disable
4550         $LFS hsm_remove $f
4551         $LFS hsm_cancel $f
4552         cdt_enable
4553         wait_request_state $fid REMOVE CANCELED
4554         wait_request_state $fid CANCEL SUCCEED
4555
4556         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4557         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
4558                 tail -n 1 | awk '{print $5}')
4559
4560         local target=0x27d
4561         [[ $flags == $target ]] ||
4562                 error "Changelog flag is $flags not $target"
4563 }
4564 run_test 225 "Changelog for remove canceled"
4565
4566 test_226() {
4567         # test needs a running copytool
4568         copytool setup
4569
4570         mkdir -p $DIR/$tdir
4571
4572         local f1=$DIR/$tdir/$tfile-1
4573         local f2=$DIR/$tdir/$tfile-2
4574         local f3=$DIR/$tdir/$tfile-3
4575         local fid1=$(copy_file /etc/passwd $f1)
4576         local fid2=$(copy_file /etc/passwd $f2)
4577         copy_file /etc/passwd $f3
4578
4579         changelog_setup
4580         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4581         wait_request_state $fid1 ARCHIVE SUCCEED
4582
4583         $LFS hsm_archive $f2
4584         wait_request_state $fid2 ARCHIVE SUCCEED
4585
4586         rm $f1 || error "rm $f1 failed"
4587
4588         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
4589
4590         local target=0x3
4591         [[ $flags == $target ]] ||
4592                 error "Changelog flag is $flags not $target"
4593
4594         mv $f3 $f2 || error "mv $f3 $f2 failed"
4595
4596         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4597
4598         target=0x3
4599         [[ $flags == $target ]] ||
4600                 error "Changelog flag is $flags not $target"
4601 }
4602 run_test 226 "changelog for last rm/mv with exiting archive"
4603
4604 check_flags_changes() {
4605         local f=$1
4606         local fid=$2
4607         local hsm_flag=$3
4608         local fst=$4
4609         local cnt=$5
4610
4611         local target=0x280
4612         $LFS hsm_set --$hsm_flag $f ||
4613                 error "Cannot set $hsm_flag on $f"
4614         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4615         local seen=${#flags[*]}
4616         cnt=$((fst + cnt))
4617         [[ $seen == $cnt ]] ||
4618                 error "set $hsm_flag: Changelog events $seen != $cnt"
4619         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4620                 error "set $hsm_flag: Changelog flags are "\
4621                         "${flags[$((cnt - 1))]} not $target"
4622
4623         $LFS hsm_clear --$hsm_flag $f ||
4624                 error "Cannot clear $hsm_flag on $f"
4625         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4626         seen=${#flags[*]}
4627         cnt=$(($cnt + 1))
4628         [[ $cnt == $seen ]] ||
4629                 error "clear $hsm_flag: Changelog events $seen != $cnt"
4630
4631         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4632                 error "clear $hsm_flag: Changelog flag is "\
4633                         "${flags[$((cnt - 1))]} not $target"
4634 }
4635
4636 test_227() {
4637         # test needs a running copytool
4638         copytool setup
4639         changelog_setup
4640
4641         mkdir -p $DIR/$tdir
4642         typeset -a flags
4643
4644         for i in norelease noarchive exists archived
4645         do
4646                 local f=$DIR/$tdir/$tfile-$i
4647                 local fid=$(copy_file /etc/passwd $f)
4648                 check_flags_changes $f $fid $i 0 1
4649         done
4650
4651         f=$DIR/$tdir/$tfile---lost
4652         fid=$(copy_file /etc/passwd $f)
4653         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4654         wait_request_state $fid ARCHIVE SUCCEED
4655         check_flags_changes $f $fid lost 3 1
4656 }
4657 run_test 227 "changelog when explicit setting of HSM flags"
4658
4659 test_228() {
4660         # test needs a running copytool
4661         copytool setup
4662
4663         local fid=$(create_small_sync_file $DIR/$tfile)
4664         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4665         wait_request_state $fid ARCHIVE SUCCEED
4666
4667         $LFS hsm_release $DIR/$tfile
4668         check_hsm_flags $DIR/$tfile "0x0000000d"
4669
4670         filefrag $DIR/$tfile | grep " 1 extent found" ||
4671                 error "filefrag on released file must return only one extent"
4672
4673         # only newer versions of cp detect sparse files by stat/FIEMAP
4674         # (LU-2580)
4675         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4676                 error "copying $DIR/$tfile"
4677         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4678
4679         $LFS hsm_release $DIR/$tfile
4680         check_hsm_flags $DIR/$tfile "0x0000000d"
4681
4682         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4683
4684         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4685                 error "tar failed"
4686         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4687                 error "comparing untarred $DIR/$tfile"
4688
4689         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4690                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4691 }
4692 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4693
4694 test_250() {
4695         # test needs a running copytool
4696         copytool setup
4697
4698         mkdir -p $DIR/$tdir
4699         local maxrequest=$(get_hsm_param max_requests)
4700         local rqcnt=$(($maxrequest * 3))
4701         local i=""
4702
4703         cdt_disable
4704         for i in $(seq -w 1 $rqcnt); do
4705                 rm -f $DIR/$tdir/$i
4706                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
4707         done
4708         # we do it in 2 steps, so all requests arrive at the same time
4709         for i in $(seq -w 1 $rqcnt); do
4710                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
4711         done
4712         cdt_enable
4713         local cnt=$rqcnt
4714         local wt=$rqcnt
4715         while [[ $cnt != 0 || $wt != 0 ]]; do
4716                 sleep 1
4717                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4718                         $HSM_PARAM.actions |\
4719                         grep STARTED | grep -v CANCEL | wc -l")
4720                 [[ $cnt -le $maxrequest ]] ||
4721                         error "$cnt > $maxrequest too many started requests"
4722                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4723                         $HSM_PARAM.actions |\
4724                         grep WAITING | wc -l")
4725                 echo "max=$maxrequest started=$cnt waiting=$wt"
4726         done
4727 }
4728 run_test 250 "Coordinator max request"
4729
4730 test_251() {
4731         # test needs a running copytool
4732         copytool setup -b 1
4733
4734         local f=$DIR/$tdir/$tfile
4735         local fid
4736         fid=$(make_custom_file_for_progress $f 103 1048576)
4737         [ $? != 0 ] && skip "not enough free space" && return
4738
4739         cdt_disable
4740         # to have a short test
4741         local old_to=$(get_hsm_param active_request_timeout)
4742         set_hsm_param active_request_timeout 4
4743         # to be sure the cdt will wake up frequently so
4744         # it will be able to cancel the "old" request
4745         local old_loop=$(get_hsm_param loop_period)
4746         set_hsm_param loop_period 2
4747         cdt_enable
4748
4749         # clear locks to avoid extra delay caused by flush/cancel
4750         # and thus prevent early copytool death to timeout.
4751         cancel_lru_locks osc
4752
4753         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4754         wait_request_state $fid ARCHIVE STARTED
4755         sleep 5
4756         wait_request_state $fid ARCHIVE CANCELED
4757
4758         set_hsm_param active_request_timeout $old_to
4759         set_hsm_param loop_period $old_loop
4760 }
4761 run_test 251 "Coordinator request timeout"
4762
4763 test_252() {
4764         mkdir -p $DIR/$tdir
4765         local f=$DIR/$tdir/$tfile
4766         local fid=$(create_empty_file "$f")
4767
4768         # to have a short test
4769         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4770         set_hsm_param loop_period 1
4771
4772         copytool_setup
4773         copytool_suspend
4774
4775         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4776         wait_request_state $fid ARCHIVE STARTED
4777         rm -f $f
4778
4779         stack_trap "set_hsm_param active_request_timeout \
4780                     $(get_hsm_param active_request_timeout)" EXIT
4781         set_hsm_param active_request_timeout 1
4782
4783         wait_request_state $fid ARCHIVE CANCELED
4784         copytool_continue
4785 }
4786 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4787
4788 test_253() {
4789         local rc
4790         # test needs a running copytool
4791         copytool setup
4792
4793         mkdir -p $DIR/$tdir
4794         local f=$DIR/$tdir/$tfile
4795
4796         dd if=/dev/zero of=$f bs=1MB count=10
4797         local fid=$(path2fid $f)
4798
4799         $LFS hsm_archive $f || error "could not archive file"
4800         wait_request_state $fid ARCHIVE SUCCEED
4801
4802         # clear locks to discard inode data
4803         cancel_lru_locks osc
4804
4805         #define OBD_FAIL_MDC_MERGE              0x807
4806         $LCTL set_param fail_loc=0x807
4807
4808         #expect error here, instead of release with wrong size
4809         $LFS hsm_release $f
4810         rc=$?
4811         if ((rc == 0)); then
4812                 file_size=$(stat -c '%s' $f)
4813                 if ((file_size != 10485760)); then
4814                         error "Wrong file size after hsm_release"
4815                 fi
4816         else
4817                 echo "could not release file"
4818         fi
4819 }
4820 run_test 253 "Check for wrong file size after release"
4821
4822 test_254a()
4823 {
4824         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4825                 skip "need MDS version at least 2.10.56" && return
4826
4827         # Check that the counters are initialized to 0
4828         local count
4829         for request_type in archive restore remove; do
4830                 count="$(get_hsm_param ${request_type}_count)" ||
4831                         error "Reading ${request_type}_count failed with $?"
4832
4833                 [ "$count" -eq 0 ] ||
4834                         error "Expected ${request_type}_count to be " \
4835                               "0 != '$count'"
4836         done
4837 }
4838 run_test 254a "Request counters are initialized to zero"
4839
4840 test_254b()
4841 {
4842         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4843                 skip "need MDS version at least 2.10.56" && return
4844
4845         # The number of request to launch (at least 32)
4846         local request_count=$((RANDOM % 32 + 32))
4847         printf "Will launch %i requests of each type\n" "$request_count"
4848
4849         # Launch a copytool to process requests
4850         copytool_setup
4851
4852         # Set hsm.max_requests to allow starting all requests at the same time
4853         stack_trap \
4854                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4855         set_hsm_param max_requests "$request_count"
4856
4857         local timeout
4858         local count
4859         for request_type in archive restore remove; do
4860                 printf "Checking %s requests\n" "${request_type}"
4861                 # Suspend the copytool to give us time to read the proc files
4862                 copytool_suspend
4863
4864                 for ((i = 0; i < $request_count; i++)); do
4865                         case $request_type in
4866                         archive)
4867                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4868                                         >/dev/null 2>&1
4869                                 ;;
4870                         restore)
4871                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4872                                 ;;
4873                         esac
4874                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4875                 done
4876
4877                 # Give the coordinator 10 seconds to start every request
4878                 timeout=10
4879                 while get_hsm_param actions | grep -q WAITING; do
4880                         sleep 1
4881                         let timeout-=1
4882                         [ $timeout -gt 0 ] ||
4883                                 error "${request_type^} requests took too " \
4884                                       "long to start"
4885                 done
4886
4887                 count="$(get_hsm_param ${request_type}_count)"
4888                 [ "$count" -eq "$request_count" ] ||
4889                         error "Expected '$request_count' (!= '$count') " \
4890                               "active $request_type requests"
4891
4892                 # Let the copytool process the requests
4893                 copytool_continue
4894                 # Give it 10 seconds maximum
4895                 timeout=10
4896                 while get_hsm_param actions | grep -q STARTED; do
4897                         sleep 1
4898                         let timeout-=1
4899                         [ $timeout -gt 0 ] ||
4900                                 error "${request_type^} requests took too " \
4901                                       "long to complete"
4902                 done
4903
4904                 count="$(get_hsm_param ${request_type}_count)"
4905                 [ "$count" -eq 0 ] ||
4906                         error "Expected 0 (!= '$count') " \
4907                               "active $request_type requests"
4908         done
4909 }
4910 run_test 254b "Request counters are correctly incremented and decremented"
4911
4912 test_300() {
4913         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4914
4915         # the only way to test ondisk conf is to restart MDS ...
4916         echo "Stop coordinator and remove coordinator state at mount"
4917         # stop coordinator
4918         cdt_shutdown
4919         # clean on disk conf set by default
4920         cdt_clear_mount_state
4921         cdt_check_state stopped
4922
4923         # check cdt still off after umount/remount
4924         fail $SINGLEMDS
4925         cdt_check_state stopped
4926
4927         echo "Set coordinator start at mount, and start coordinator"
4928         cdt_set_mount_state enabled
4929
4930         # check cdt is on
4931         cdt_check_state enabled
4932
4933         # check cdt still on after umount/remount
4934         fail $SINGLEMDS
4935         cdt_check_state enabled
4936
4937         # we are back to original state (cdt started at mount)
4938 }
4939 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4940
4941 test_301() {
4942         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4943
4944         local ai=$(get_hsm_param default_archive_id)
4945         local new=$((ai + 1))
4946
4947         set_hsm_param default_archive_id $new -P
4948         fail $SINGLEMDS
4949         local res=$(get_hsm_param default_archive_id)
4950
4951         # clear value
4952         set_hsm_param default_archive_id "" "-P -d"
4953
4954         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4955 }
4956 run_test 301 "HSM tunnable are persistent"
4957
4958 test_302() {
4959         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4960
4961         local ai=$(get_hsm_param default_archive_id)
4962         local new=$((ai + 1))
4963
4964         # stop coordinator
4965         cdt_shutdown
4966
4967         set_hsm_param default_archive_id $new -P
4968
4969         local mdtno
4970         for mdtno in $(seq 1 $MDSCOUNT); do
4971                 fail mds${mdtno}
4972         done
4973
4974         # check cdt is on
4975         cdt_check_state enabled
4976
4977         local res=$(get_hsm_param default_archive_id)
4978
4979         # clear value
4980         set_hsm_param default_archive_id "" "-P -d"
4981
4982         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4983 }
4984 run_test 302 "HSM tunnable are persistent when CDT is off"
4985
4986 test_400() {
4987         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4988
4989         copytool setup
4990
4991         mkdir -p $DIR/$tdir
4992
4993         local dir_mdt0=$DIR/$tdir/mdt0
4994         local dir_mdt1=$DIR/$tdir/mdt1
4995
4996         # create 1 dir per MDT
4997         stack_trap "rm -rf $dir_mdt0"
4998         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4999         stack_trap "rm -rf $dir_mdt1"
5000         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5001
5002         # create 1 file in each MDT
5003         local fid1=$(create_small_file $dir_mdt0/$tfile)
5004         local fid2=$(create_small_file $dir_mdt1/$tfile)
5005
5006         # check that hsm request on mdt0 is sent to the right MDS
5007         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5008         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5009                 echo "archive successful on mdt0"
5010
5011         # check that hsm request on mdt1 is sent to the right MDS
5012         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
5013         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5014                 echo "archive successful on mdt1"
5015 }
5016 run_test 400 "Single request is sent to the right MDT"
5017
5018 test_401() {
5019         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5020
5021         copytool setup
5022
5023         mkdir -p $DIR/$tdir
5024
5025         local dir_mdt0=$DIR/$tdir/mdt0
5026         local dir_mdt1=$DIR/$tdir/mdt1
5027
5028         # create 1 dir per MDT
5029         stack_trap "rm -rf $dir_mdt0" EXIT
5030         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5031         stack_trap "rm -rf $dir_mdt1" EXIT
5032         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5033
5034         # create 1 file in each MDT
5035         local fid1=$(create_small_file $dir_mdt0/$tfile)
5036         local fid2=$(create_small_file $dir_mdt1/$tfile)
5037
5038         # check that compound requests are shunt to the rights MDTs
5039         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
5040                 error "lfs hsm_archive"
5041         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5042                 echo "archive successful on mdt0"
5043         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5044                 echo "archive successful on mdt1"
5045 }
5046 run_test 401 "Compound requests split and sent to their respective MDTs"
5047
5048 mdc_change_state() # facet, MDT_pattern, activate|deactivate
5049 {
5050         local facet=$1
5051         local pattern="$2"
5052         local state=$3
5053         local node=$(facet_active_host $facet)
5054         local mdc
5055         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
5056                         awk '{print $4}'); do
5057                 echo "$3 $mdc on $node"
5058                 do_facet $facet "$LCTL --device $mdc $state" || return 1
5059         done
5060 }
5061
5062 test_402a() {
5063         # deactivate all mdc on agent1
5064         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
5065
5066         copytool setup --no-fail
5067
5068         check_agent_unregistered "uuid" # match any agent
5069
5070         # no expected running copytool
5071         search_copytools $agent && error "Copytool start should have failed"
5072
5073         # reactivate MDCs
5074         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
5075 }
5076 run_test 402a "Copytool start fails if all MDTs are inactive"
5077
5078 test_402b() {
5079         copytool setup
5080
5081         mkdir -p $DIR/$tdir
5082
5083         local f=$DIR/$tdir/$tfile
5084         touch $f || error "touch $f failed"
5085         local fid=$(path2fid $f)
5086
5087 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
5088         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
5089         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5090
5091         # give time for CDT to send request and to keep it for retry
5092         wait_for_loop_period
5093
5094         wait_request_state $fid ARCHIVE WAITING
5095
5096         do_facet $SINGLEAGT lctl set_param fail_loc=0
5097
5098         # request should succeed now
5099         wait_request_state $fid ARCHIVE SUCCEED
5100 }
5101 run_test 402b "CDT must retry request upon slow start of CT"
5102
5103 test_403() {
5104         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5105
5106         local agent=$(facet_active_host $SINGLEAGT)
5107
5108         # deactivate all mdc for MDT0001
5109         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5110
5111         copytool setup
5112         local uuid=$(get_agent_uuid $agent)
5113         # check the agent is registered on MDT0000, and not on MDT0001
5114         check_agent_registered_by_mdt $uuid 0
5115         check_agent_unregistered_by_mdt $uuid 1
5116
5117         # check running copytool process
5118         search_copytools $agent || error "No running copytools on $agent"
5119
5120         # reactivate all mdc for MDT0001
5121         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5122
5123         # make sure the copytool is now registered to all MDTs
5124         check_agent_registered $uuid
5125 }
5126 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
5127
5128 test_404() {
5129         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5130
5131         copytool setup
5132
5133         # create files on both MDT0000 and MDT0001
5134         mkdir -p $DIR/$tdir
5135
5136         local dir_mdt0=$DIR/$tdir/mdt0
5137         stack_trap "rm -rf $dir_mdt0" EXIT
5138         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5139
5140         # create 1 file on mdt0
5141         local fid1=$(create_small_file $dir_mdt0/$tfile)
5142
5143         # deactivate all mdc for MDT0001
5144         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5145
5146         # send an HSM request for files in MDT0000
5147         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5148
5149         # check for completion of files in MDT0000
5150         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5151                 echo "archive successful on mdt0"
5152
5153         # reactivate all mdc for MDT0001
5154         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5155 }
5156 run_test 404 "Inactive MDT does not block requests for active MDTs"
5157
5158 test_405() {
5159         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5160
5161         copytool setup
5162
5163         mkdir -p $DIR/$tdir
5164
5165         local striped_dir=$DIR/$tdir/striped_dir
5166
5167         # create striped dir on all of MDTs
5168         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
5169
5170         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
5171         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
5172         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
5173         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
5174
5175         local idx1=$($LFS getstripe -M $striped_dir/${tfile}_0)
5176         local idx2=$($LFS getstripe -M $striped_dir/${tfile}_1)
5177         local idx3=$($LFS getstripe -M $striped_dir/${tfile}_2)
5178         local idx4=$($LFS getstripe -M $striped_dir/${tfile}_3)
5179
5180         # check that compound requests are shunt to the rights MDTs
5181         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5182                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5183                 error "lfs hsm_archive"
5184
5185         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5186                 echo "archive successful on $fid1"
5187         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5188                 echo "archive successful on $fid2"
5189         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5190                 echo "archive successful on $fid3"
5191         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5192                 echo "archive successful on $fid4"
5193
5194         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5195         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5196         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5197         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5198
5199         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5200         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5201         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5202         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5203 }
5204 run_test 405 "archive and release under striped directory"
5205
5206 test_406() {
5207         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5208
5209         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
5210                 skip "need MDS version at least 2.7.64" && return 0
5211
5212         local fid
5213         local mdt_index
5214
5215         fid=$(create_small_file $DIR/$tdir/$tfile)
5216         echo "old fid $fid"
5217
5218         copytool setup
5219
5220         $LFS hsm_archive $DIR/$tdir/$tfile
5221         wait_request_state "$fid" ARCHIVE SUCCEED
5222         $LFS hsm_release $DIR/$tdir/$tfile
5223
5224         # Should migrate $tdir but not $tfile.
5225         $LFS migrate -m1 $DIR/$tdir &&
5226                 error "migrating HSM an archived file should fail"
5227
5228         $LFS hsm_restore $DIR/$tdir/$tfile
5229         wait_request_state "$fid" RESTORE SUCCEED
5230
5231         $LFS hsm_remove $DIR/$tdir/$tfile
5232         wait_request_state "$fid" REMOVE SUCCEED
5233
5234         cat $DIR/$tdir/$tfile > /dev/null ||
5235                 error "cannot read $DIR/$tdir/$tfile"
5236
5237         $LFS migrate -m1 $DIR/$tdir ||
5238                 error "cannot complete migration after HSM remove"
5239
5240         mdt_index=$($LFS getstripe -M $DIR/$tdir)
5241         if ((mdt_index != 1)); then
5242                 error "expected MDT index 1, got $mdt_index"
5243         fi
5244
5245         # Refresh fid after migration.
5246         fid=$(path2fid $DIR/$tdir/$tfile)
5247         echo "new fid $fid"
5248
5249         $LFS hsm_archive $DIR/$tdir/$tfile
5250         wait_request_state "$fid" ARCHIVE SUCCEED 1
5251
5252         lctl set_param debug=+trace
5253         $LFS hsm_release $DIR/$tdir/$tfile ||
5254                 error "cannot release $DIR/$tdir/$tfile"
5255
5256         $LFS hsm_restore $DIR/$tdir/$tfile
5257         wait_request_state "$fid" RESTORE SUCCEED 1
5258
5259         cat $DIR/$tdir/$tfile > /dev/null ||
5260                 error "cannot read $DIR/$tdir/$tfile"
5261 }
5262 run_test 406 "attempting to migrate HSM archived files is safe"
5263
5264 test_407() {
5265         needclients 2 || return 0
5266         # test needs a running copytool
5267         copytool setup
5268
5269         mkdir -p $DIR/$tdir
5270
5271         local f=$DIR/$tdir/$tfile
5272         local f2=$DIR2/$tdir/$tfile
5273         local fid
5274         fid=$(make_custom_file_for_progress $f 39 1000000)
5275         [ $? != 0 ] && skip "not enough free space" && return
5276
5277         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5278         wait_request_state $fid ARCHIVE SUCCEED
5279         $LFS hsm_release $f
5280
5281 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5282         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5283
5284         md5sum $f &
5285         # 1st request holds layout lock while appropriate
5286         # RESTORE record is still not added to llog
5287         md5sum $f2 &
5288         sleep 2
5289
5290         # after umount hsm_actions->O/x/x log shouldn't have
5291         # double RESTORE records like below
5292         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5293         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5294         sleep 30 &&
5295                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5296         fail $SINGLEMDS
5297
5298         wait_request_state $fid RESTORE SUCCEED
5299 }
5300 run_test 407 "Check for double RESTORE records in llog"
5301
5302 test_500()
5303 {
5304         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
5305                 skip "HSM migrate is not supported" && return
5306
5307         test_mkdir -p $DIR/$tdir
5308         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
5309 }
5310 run_test 500 "various LLAPI HSM tests"
5311
5312 copytool_cleanup
5313
5314 complete $SECONDS
5315 check_and_cleanup_lustre
5316 exit_status