Whamcloud - gitweb
LU-10997 build: add files to .gitignore
[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         wait_request_state $fid2 REMOVE SUCCEED
2508
2509         assert_request_count $fid REMOVE 0 \
2510                 "Unexpected archived data remove request for $f"
2511         assert_request_count $fid3 REMOVE 0 \
2512                 "Unexpected archived data remove request for $f3"
2513 }
2514 run_test 26a "Remove Archive On Last Unlink (RAoLU) policy"
2515
2516 test_26b() {
2517         # test needs a running copytool
2518         copytool setup
2519
2520         mkdir -p $DIR/$tdir
2521         local f=$DIR/$tdir/$tfile
2522         local fid=$(copy_file /etc/passwd $f)
2523
2524         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2525         wait_request_state $fid ARCHIVE SUCCEED
2526
2527         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2528         set_hsm_param remove_archive_on_last_unlink 1
2529
2530         cdt_shutdown
2531         cdt_check_state stopped
2532
2533         rm -f $f
2534
2535         wait_request_state $fid REMOVE WAITING
2536
2537         cdt_enable
2538
2539         # copytool must re-register
2540         kill_copytools
2541         wait_copytools || error "copytool failed to stop"
2542         copytool setup
2543
2544         wait_request_state $fid REMOVE SUCCEED
2545 }
2546 run_test 26b "RAoLU policy when CDT off"
2547
2548 test_26c() {
2549         # test needs a running copytool
2550         copytool setup
2551
2552         mkdir -p $DIR/$tdir
2553         local f=$DIR/$tdir/$tfile
2554         local fid=$(copy_file /etc/passwd $f)
2555
2556         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2557         wait_request_state $fid ARCHIVE SUCCEED
2558
2559         local f2=$DIR/$tdir/${tfile}_2
2560         local fid2=$(copy_file /etc/passwd $f2)
2561
2562         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2563         wait_request_state $fid2 ARCHIVE SUCCEED
2564
2565         # set a long grace_delay vs short loop_period
2566         local orig_loop_period=$(get_hsm_param loop_period)
2567         local orig_grace_delay=$(get_hsm_param grace_delay)
2568         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2569         set_hsm_param loop_period 10
2570         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2571         set_hsm_param grace_delay 100
2572
2573         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2574         set_hsm_param remove_archive_on_last_unlink 1
2575
2576         multiop_bg_pause $f O_c || error "open $f failed"
2577         local pid=$!
2578
2579         rm -f $f
2580         rm -f $f2
2581
2582         wait_request_state $fid2 REMOVE SUCCEED
2583         assert_request_count $fid REMOVE 0 \
2584                 "Unexpected archived data remove request for $f"
2585
2586         kill -USR1 $pid || error "multiop early exit"
2587         # should reach autotest timeout if multiop fails to trap
2588         # signal, close file, and exit ...
2589         wait $pid || error "wait PID $PID failed"
2590
2591         wait_request_state $fid REMOVE SUCCEED
2592 }
2593 run_test 26c "RAoLU effective when file closed"
2594
2595 test_26d() {
2596         # test needs a running copytool
2597         copytool setup
2598
2599         mkdir -p $DIR/$tdir
2600         local f=$DIR/$tdir/$tfile
2601         local fid=$(create_small_file $f)
2602
2603         $LFS hsm_archive $f || error "could not archive file"
2604         wait_request_state $fid ARCHIVE SUCCEED
2605
2606         # set a long grace_delay vs short loop_period
2607         local orig_loop_period=$(get_hsm_param loop_period)
2608         local orig_grace_delay=$(get_hsm_param grace_delay)
2609         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2610         set_hsm_param loop_period 10
2611         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2612         set_hsm_param grace_delay 100
2613
2614         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2615         set_hsm_param remove_archive_on_last_unlink 1
2616
2617         multiop_bg_pause $f O_c || error "multiop failed"
2618         local MULTIPID=$!
2619
2620         rm -f $f
2621
2622         mds_evict_client
2623
2624         wait_request_state $fid REMOVE SUCCEED
2625
2626         client_up || client_up || true
2627
2628         kill -USR1 $MULTIPID
2629         wait $MULTIPID || error "multiop close failed"
2630 }
2631 run_test 26d "RAoLU when Client eviction"
2632
2633 test_27a() {
2634         # test needs a running copytool
2635         copytool_setup
2636
2637         create_archive_file $tdir/$tfile
2638         local f=$DIR/$tdir/$tfile
2639         import_file $tdir/$tfile $f
2640         local fid=$(path2fid $f)
2641
2642         $LFS hsm_remove $f
2643
2644         [[ $? != 0 ]] || error "Remove of a released file should fail"
2645
2646         copytool_cleanup
2647 }
2648 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
2649
2650 test_27b() {
2651         # test needs a running copytool
2652         copytool setup
2653
2654         local f=$DIR/$tdir/$tfile
2655         local fid
2656         fid=$(make_custom_file_for_progress $f 39 1000000)
2657         [ $? != 0 ] && skip "not enough free space" && return
2658
2659         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2660         wait_request_state $fid ARCHIVE SUCCEED
2661         $LFS hsm_release $f
2662
2663         $LFS hsm_remove $f
2664
2665         [[ $? != 0 ]] || error "Remove of a released file should fail"
2666 }
2667 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
2668
2669 test_28() {
2670         # test needs a running copytool
2671         copytool setup
2672
2673         local f=$DIR/$tdir/$tfile
2674         local fid
2675         fid=$(make_custom_file_for_progress $f 39 1000000)
2676         [ $? != 0 ] && skip "not enough free space" && return
2677
2678         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2679         wait_request_state $fid ARCHIVE SUCCEED
2680
2681         cdt_disable
2682         $LFS hsm_remove $f
2683
2684         rm -f $f
2685
2686         cdt_enable
2687
2688         wait_request_state $fid REMOVE SUCCEED
2689 }
2690 run_test 28 "Concurrent archive/file remove"
2691
2692 test_29a() {
2693         # Tests --mntpath and --archive options
2694
2695         local archive_id=7
2696         copytool setup -m "$MOUNT" -a $archive_id
2697
2698         # Bad archive number
2699         $LFS hsm_remove -m "$MOUNT" -a 33 0x857765760:0x8:0x2 2>&1 |
2700                 grep "Invalid argument" ||
2701                 error "unexpected hsm_remove failure (1)"
2702
2703         # mntpath is present but file is given
2704         $LFS hsm_remove --mntpath "$MOUNT" --archive 30 /qwerty/uyt 2>&1 |
2705                 grep "hsm: '/qwerty/uyt' is not a valid FID" ||
2706                 error "unexpected hsm_remove failure (2)"
2707 }
2708 run_test 29a "Tests --mntpath and --archive options"
2709
2710 test_29b() {
2711         # test needs a running copytool
2712         copytool setup
2713
2714         local f=$DIR/$tdir/$tfile
2715         local fid=$(create_small_file $f)
2716
2717         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2718         wait_request_state $fid ARCHIVE SUCCEED
2719
2720         rm -f $f
2721
2722         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER $fid
2723         wait_request_state $fid REMOVE SUCCEED
2724 }
2725 run_test 29b "Archive/delete/remove by FID from the archive."
2726
2727 test_29c() {
2728         # test needs a running copytool
2729         copytool setup
2730
2731         local fid1=$(create_small_file $DIR/$tdir/$tfile-1)
2732         local fid2=$(create_small_file $DIR/$tdir/$tfile-2)
2733         local fid3=$(create_small_file $DIR/$tdir/$tfile-3)
2734
2735         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$tfile-[1-3]
2736         wait_request_state $fid1 ARCHIVE SUCCEED
2737         wait_request_state $fid2 ARCHIVE SUCCEED
2738         wait_request_state $fid3 ARCHIVE SUCCEED
2739
2740         rm -f $DIR/$tdir/$tfile-[1-3]
2741
2742         echo $fid1 > $DIR/$tdir/list
2743         echo $fid2 >> $DIR/$tdir/list
2744         echo $fid3 >> $DIR/$tdir/list
2745
2746         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER \
2747                 --filelist $DIR/$tdir/list
2748         wait_request_state $fid1 REMOVE SUCCEED
2749         wait_request_state $fid2 REMOVE SUCCEED
2750         wait_request_state $fid3 REMOVE SUCCEED
2751 }
2752 run_test 29c "Archive/delete/remove by FID, using a file list."
2753
2754 test_29d() {
2755         # test needs more than one CT
2756         needclients 3 || return 0
2757
2758         local n
2759         local file
2760         local fid
2761
2762         # start all of the copytools
2763         for n in $(seq $AGTCOUNT); do
2764                 copytool setup -f agt$n -a $n
2765         done
2766
2767         # archive files
2768         file=$DIR/$tdir/$tfile
2769         fid=$(create_small_file $file)
2770
2771         $LFS hsm_archive $file
2772         wait_request_state $fid ARCHIVE SUCCEED
2773         check_hsm_flags $file "0x00000009"
2774
2775         rm -f $file
2776
2777         $LFS hsm_remove --mntpath "$MOUNT" -a 0 $fid ||
2778                 error "cannot hsm_remove '$fid'"
2779
2780         # give time for CDT to handle remove request and create broadcasted
2781         sleep 2
2782
2783         # remove request has been broadcasted ?
2784         local cnt=$(get_request_count $fid REMOVE)
2785         # broadcasted requests + original
2786         [[ $cnt -eq $((AGTCOUNT + 1)) ]] ||
2787                 error "remove not broadcasted to all CTs"
2788
2789         # give time for CDT and CTs to handle broadcasted
2790         wait_for_loop_period
2791
2792         # each agent serves one different archive_id, so broadcasted
2793         # hsm_remove request should only succeed once and fail at all others
2794         local res
2795         local scnt=0
2796         local fcnt=0
2797         for n in $(seq $AGTCOUNT); do
2798                 res=$(do_facet $SINGLEMDS "$LCTL get_param -n \
2799                                $HSM_PARAM.actions | awk \
2800                                '/'$fid'.*action=REMOVE archive#='$n'/ \
2801                                {print \\\$13}' | cut -f2 -d=")
2802                 if [[ "$res" == "SUCCEED" ]]; then
2803                         scnt=$((scnt + 1))
2804                 elif [[ "$res" == "FAILED" ]]; then
2805                         fcnt=$((fcnt + 1))
2806                 fi
2807         done
2808
2809         [[ $scnt -eq 1 ]] ||
2810                 error "one and only CT should have removed successfully"
2811
2812         [[ $AGTCOUNT -eq $((scnt + fcnt)) ]] ||
2813                 error "all but one CT should have failed to remove"
2814 }
2815 run_test 29d "hsm_remove by FID with archive_id 0 for unlinked file cause "\
2816              "request to be sent once for each registered archive_id"
2817
2818 test_30a() {
2819         # restore at exec cannot work on agent node (because of Linux kernel
2820         # protection of executables)
2821         needclients 2 || return 0
2822
2823         # test needs a running copytool
2824         copytool_setup
2825
2826         mkdir -p $DIR/$tdir
2827         copy2archive /bin/true $tdir/$tfile
2828
2829         local f=$DIR/$tdir/true
2830         import_file $tdir/$tfile $f
2831
2832         local fid=$(path2fid $f)
2833
2834         # set no retry action mode
2835         cdt_set_no_retry
2836         do_node $CLIENT2 $f
2837         local st=$?
2838
2839         # cleanup
2840         # remove no try action mode
2841         cdt_clear_no_retry
2842         $LFS hsm_state $f
2843
2844         [[ $st == 0 ]] || error "Failed to exec a released file"
2845
2846         copytool_cleanup
2847 }
2848 run_test 30a "Restore at exec (import case)"
2849
2850 test_30b() {
2851         # restore at exec cannot work on agent node (because of Linux kernel
2852         # protection of executables)
2853         needclients 2 || return 0
2854
2855         # test needs a running copytool
2856         copytool setup
2857
2858         mkdir -p $DIR/$tdir
2859         local f=$DIR/$tdir/true
2860         local fid=$(copy_file /bin/true $f)
2861         chmod 755 $f
2862         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2863         wait_request_state $fid ARCHIVE SUCCEED
2864         $LFS hsm_release $f
2865         $LFS hsm_state $f
2866
2867         stack_trap cdt_clear_no_retry EXIT
2868         # set no retry action mode
2869         cdt_set_no_retry
2870
2871         do_node $CLIENT2 $f
2872         local st=$?
2873
2874         $LFS hsm_state $f
2875
2876         [[ $st == 0 ]] || error "Failed to exec a released file"
2877 }
2878 run_test 30b "Restore at exec (release case)"
2879
2880 test_30c() {
2881         needclients 2 || return 0
2882
2883         # test needs a running copytool
2884         copytool setup
2885
2886         mkdir -p $DIR/$tdir
2887         local f=$DIR/$tdir/SLEEP
2888         local slp_sum1=$(md5sum /bin/sleep)
2889         local fid=$(copy_file /bin/sleep $f)
2890         chmod 755 $f
2891         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2892         wait_request_state $fid ARCHIVE SUCCEED
2893         $LFS hsm_release $f
2894         check_hsm_flags $f "0x0000000d"
2895
2896         stack_trap cdt_clear_no_retry EXIT
2897         # set no retry action mode
2898         cdt_set_no_retry
2899
2900         do_node $CLIENT2 "$f 10" &
2901         local pid=$!
2902         sleep 3
2903         echo 'Hi!' > $f
2904         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2905         wait $pid
2906         [[ $? == 0 ]] || error "Execution failed during run"
2907         cmp /bin/sleep $f
2908         if [[ $? != 0 ]]; then
2909                 local slp_sum2=$(md5sum /bin/sleep)
2910                 # in case sleep file is modified during the test
2911                 [[ $slp_sum1 == $slp_sum2 ]] &&
2912                         error "Binary overwritten during exec"
2913         fi
2914
2915         check_hsm_flags $f "0x00000009"
2916 }
2917 run_test 30c "Update during exec of released file must fail"
2918
2919 restore_and_check_size() {
2920         local f=$1
2921         local fid=$2
2922         local s=$(stat -c "%s" $f)
2923         local n=$s
2924         local st=$(get_hsm_flags $f)
2925         local err=0
2926         local cpt=0
2927         $LFS hsm_restore $f
2928         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2929         do
2930                 n=$(stat -c "%s" $f)
2931                 # we echo in both cases to show stat is not hang
2932                 if [[ $n != $s ]]; then
2933                         echo "size seen is $n != $s"
2934                         err=1
2935                 else
2936                         echo "size seen is right: $n == $s"
2937                 fi
2938                 sleep 10
2939                 cpt=$((cpt + 1))
2940                 st=$(get_hsm_flags $f)
2941         done
2942         if [[ "$st" = "0x00000009" ]]; then
2943                 echo " "done
2944         else
2945                 echo " restore is too long"
2946                 wait_request_state $fid RESTORE SUCCEED
2947         fi
2948         return $err
2949 }
2950
2951 test_31a() {
2952         # test needs a running copytool
2953         copytool_setup
2954
2955         create_archive_file $tdir/$tfile
2956         local f=$DIR/$tdir/$tfile
2957         import_file $tdir/$tfile $f
2958         local fid=$($LFS path2fid $f)
2959         HSM_ARCHIVE_PURGE=false copytool_setup
2960
2961         restore_and_check_size $f $fid
2962         local err=$?
2963
2964         [[ $err -eq 0 ]] || error "File size changed during restore"
2965
2966         copytool_cleanup
2967 }
2968 run_test 31a "Import a large file and check size during restore"
2969
2970
2971 test_31b() {
2972         # test needs a running copytool
2973         copytool setup
2974
2975         local f=$DIR/$tdir/$tfile
2976         local fid
2977         fid=$(make_custom_file_for_progress $f 39 1000000)
2978         [ $? != 0 ] && skip "not enough free space" && return
2979
2980         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2981         wait_request_state $fid ARCHIVE SUCCEED
2982         $LFS hsm_release $f
2983
2984         restore_and_check_size $f $fid
2985         local err=$?
2986
2987         [[ $err -eq 0 ]] || error "File size changed during restore"
2988 }
2989 run_test 31b "Restore a large unaligned file and check size during restore"
2990
2991 test_31c() {
2992         # test needs a running copytool
2993         copytool setup
2994
2995         local f=$DIR/$tdir/$tfile
2996         local fid
2997         fid=$(make_custom_file_for_progress $f 33 1048576)
2998         [ $? != 0 ] && skip "not enough free space" && return
2999
3000         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3001         wait_request_state $fid ARCHIVE SUCCEED
3002         $LFS hsm_release $f
3003
3004         restore_and_check_size $f $fid
3005         local err=$?
3006
3007         [[ $err -eq 0 ]] || error "File size changed during restore"
3008 }
3009 run_test 31c "Restore a large aligned file and check size during restore"
3010
3011 test_33() {
3012         # test needs a running copytool
3013         copytool setup -b 1
3014
3015         local f=$DIR/$tdir/$tfile
3016         local fid
3017         fid=$(make_custom_file_for_progress $f 39 1000000)
3018         [ $? != 0 ] && skip "not enough free space" && return
3019
3020         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3021         wait_request_state $fid ARCHIVE SUCCEED
3022         $LFS hsm_release $f
3023
3024         # to be sure wait_all_done will not be mislead by previous tests
3025         # and ops.
3026         cdt_purge
3027         wait_for_grace_delay
3028         # Also raise grace_delay significantly so the Canceled
3029         # Restore action will stay enough long avail.
3030         local old_grace=$(get_hsm_param grace_delay)
3031         stack_trap "set_hsm_param grace_delay $old_grace" EXIT
3032         set_hsm_param grace_delay 100
3033
3034         md5sum $f >/dev/null &
3035         local pid=$!
3036         wait_request_state $fid RESTORE STARTED
3037
3038         kill -15 $pid
3039         sleep 1
3040
3041         # Check restore trigger process was killed
3042         local killed=$(ps -o pid,comm hp $pid >/dev/null)
3043
3044         $LFS hsm_cancel $f
3045
3046         # instead of waiting+checking both Restore and Cancel ops
3047         # sequentially, wait for both to be finished and then check
3048         # each results.
3049         wait_all_done 100 $fid
3050         local rstate=$(get_request_state $fid RESTORE)
3051         local cstate=$(get_request_state $fid CANCEL)
3052
3053         if [[ "$rstate" == "CANCELED" ]] ; then
3054                 [[ "$cstate" == "SUCCEED" ]] ||
3055                         error "Restore state is CANCELED and Cancel state " \
3056                                "is not SUCCEED but $cstate"
3057                 echo "Restore state is CANCELED, Cancel state is SUCCEED"
3058         elif [[ "$rstate" == "SUCCEED" ]] ; then
3059                 [[ "$cstate" == "FAILED" ]] ||
3060                         error "Restore state is SUCCEED and Cancel state " \
3061                                 "is not FAILED but $cstate"
3062                 echo "Restore state is SUCCEED, Cancel state is FAILED"
3063         else
3064                 error "Restore state is $rstate and Cancel state is $cstate"
3065         fi
3066
3067         [ -z $killed ] ||
3068                 error "Cannot kill process waiting for restore ($killed)"
3069 }
3070 run_test 33 "Kill a restore waiting process"
3071
3072 test_34() {
3073         # test needs a running copytool
3074         copytool setup -b 1
3075
3076         local f=$DIR/$tdir/$tfile
3077         local fid
3078         fid=$(make_custom_file_for_progress $f 39 1000000)
3079         [ $? != 0 ] && skip "not enough free space" && return
3080
3081         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3082         wait_request_state $fid ARCHIVE SUCCEED
3083         $LFS hsm_release $f
3084
3085         md5sum $f >/dev/null &
3086         local pid=$!
3087         wait_request_state $fid RESTORE STARTED
3088
3089         rm $f || error "rm $f failed"
3090         # rm must not block during restore
3091         wait_request_state $fid RESTORE STARTED
3092
3093         wait_request_state $fid RESTORE SUCCEED
3094         # check md5sum pgm finished
3095         local there=$(ps -o pid,comm hp $pid >/dev/null)
3096         [[ -z $there ]] || error "Restore initiator does not exit"
3097
3098         wait $pid || error "Restore initiator failed with $?"
3099 }
3100 run_test 34 "Remove file during restore"
3101
3102 test_35() {
3103         # test needs a running copytool
3104         copytool setup -b 1
3105
3106         local f=$DIR/$tdir/$tfile
3107         local f1=$DIR/$tdir/$tfile-1
3108         local fid
3109         fid=$(make_custom_file_for_progress $f 39 1000000)
3110         [ $? != 0 ] && skip "not enough free space" && return
3111
3112         local fid1=$(copy_file /etc/passwd $f1)
3113         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3114         wait_request_state $fid ARCHIVE SUCCEED
3115         $LFS hsm_release $f
3116
3117         md5sum $f >/dev/null &
3118         local pid=$!
3119         wait_request_state $fid RESTORE STARTED
3120
3121         mv $f1 $f || error "mv $f1 $f failed"
3122         # mv must not block during restore
3123         wait_request_state $fid RESTORE STARTED
3124
3125         wait_request_state $fid RESTORE SUCCEED
3126         # check md5sum pgm finished
3127         local there=$(ps -o pid,comm hp $pid >/dev/null)
3128         [[ -z $there ]] || error "Restore initiator does not exit"
3129
3130         wait $pid || error "Restore initiator failed with $?"
3131
3132         fid2=$(path2fid $f)
3133         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
3134 }
3135 run_test 35 "Overwrite file during restore"
3136
3137 test_36() {
3138         # test needs a running copytool
3139         copytool setup -b 1
3140
3141         local f=$DIR/$tdir/$tfile
3142         local fid
3143         fid=$(make_custom_file_for_progress $f 39 1000000)
3144         [ $? != 0 ] && skip "not enough free space" && return
3145
3146         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3147         wait_request_state $fid ARCHIVE SUCCEED
3148         $LFS hsm_release $f
3149
3150         md5sum $f >/dev/null &
3151         local pid=$!
3152         wait_request_state $fid RESTORE STARTED
3153
3154         mv $f $f.new
3155         # rm must not block during restore
3156         wait_request_state $fid RESTORE STARTED
3157
3158         wait_request_state $fid RESTORE SUCCEED
3159         # check md5sum pgm finished
3160         local there=$(ps -o pid,comm hp $pid >/dev/null)
3161         [[ -z $there ]] ||
3162                 error "Restore initiator does not exit"
3163
3164         wait $pid || error "Restore initiator failed with $?"
3165 }
3166 run_test 36 "Move file during restore"
3167
3168 test_37() {
3169         # LU-5683: check that an archived dirty file can be rearchived.
3170         copytool setup
3171
3172         local f=$DIR/$tdir/$tfile
3173         local fid
3174
3175         fid=$(create_small_file $f) || error "cannot create small file"
3176
3177         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3178         wait_request_state $fid ARCHIVE SUCCEED
3179         $LFS hsm_release $f || error "cannot release $f"
3180
3181         # Allow previous archive request to expire from the actions log.
3182         wait_for_grace_delay
3183
3184         # Dirty file.
3185         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
3186
3187         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3188         wait_request_state $fid ARCHIVE SUCCEED
3189 }
3190 run_test 37 "re-archive a dirty file"
3191
3192 multi_archive() {
3193         local prefix=$1
3194         local count=$2
3195         local n=""
3196
3197         for n in $(seq 1 $count); do
3198                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
3199         done
3200         echo "$count archive requests submitted"
3201 }
3202
3203 test_40() {
3204         local stream_count=4
3205         local file_count=100
3206         mkdir -p $DIR/$tdir
3207         local f=$DIR/$tdir/$tfile
3208         local i=""
3209         local p=""
3210         local fid=""
3211         local max_requests=$(get_hsm_param max_requests)
3212
3213         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3214         # Increase the number of HSM request that can be performed in
3215         # parallel. With the coordinator running once per second, this
3216         # also limits the number of requests per seconds that can be
3217         # performed, so we pick a decent number. But we also need to keep
3218         # that number low because the copytool has no rate limit and will
3219         # fail some requests if if gets too many at once.
3220         set_hsm_param max_requests 300
3221
3222         for i in $(seq 1 $file_count); do
3223                 for p in $(seq 1 $stream_count); do
3224                         fid=$(copy_file /etc/hosts $f.$p.$i)
3225                 done
3226         done
3227
3228         copytool setup
3229
3230         # to be sure wait_all_done will not be mislead by previous tests
3231         cdt_purge
3232         wait_for_grace_delay
3233         typeset -a pids
3234         # start archive streams in background (archive files in parallel)
3235         for p in $(seq 1 $stream_count); do
3236                 multi_archive $f.$p $file_count &
3237                 pids[$p]=$!
3238         done
3239         echo -n  "Wait for all requests being enqueued..."
3240         wait ${pids[*]}
3241         echo OK
3242         wait_all_done 100
3243 }
3244 run_test 40 "Parallel archive requests"
3245
3246 test_52() {
3247         # test needs a running copytool
3248         copytool setup
3249
3250         mkdir -p $DIR/$tdir
3251         local f=$DIR/$tdir/$tfile
3252         local fid=$(create_small_file $f)
3253
3254         $LFS hsm_archive $f || error "could not archive file"
3255         wait_request_state $fid ARCHIVE SUCCEED
3256         check_hsm_flags $f "0x00000009"
3257
3258         multiop_bg_pause $f O_c || error "multiop failed"
3259         local MULTIPID=$!
3260
3261         mds_evict_client
3262         client_up || client_up || true
3263
3264         kill -USR1 $MULTIPID
3265         wait $MULTIPID || error "multiop close failed"
3266
3267         check_hsm_flags $f "0x0000000b"
3268 }
3269 run_test 52 "Opened for write file on an evicted client should be set dirty"
3270
3271 test_53() {
3272         # test needs a running copytool
3273         copytool setup
3274
3275         mkdir -p $DIR/$tdir
3276         local f=$DIR/$tdir/$tfile
3277         local fid=$(create_small_file $f)
3278
3279         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3280                 error "could not archive file"
3281         wait_request_state $fid ARCHIVE SUCCEED
3282         check_hsm_flags $f "0x00000009"
3283
3284         multiop_bg_pause $f o_c || error "multiop failed"
3285         MULTIPID=$!
3286
3287         mds_evict_client
3288         client_up || client_up || true
3289
3290         kill -USR1 $MULTIPID
3291         wait $MULTIPID || error "multiop close failed"
3292
3293         check_hsm_flags $f "0x00000009"
3294 }
3295 run_test 53 "Opened for read file on an evicted client should not be set dirty"
3296
3297 test_54() {
3298         # test needs a running copytool
3299         copytool setup -b 1
3300
3301         local f=$DIR/$tdir/$tfile
3302         local fid=$(make_custom_file_for_progress $f 39 1000000)
3303
3304         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3305                 error "could not archive file"
3306         wait_request_state $fid ARCHIVE STARTED
3307
3308         check_hsm_flags $f "0x00000001"
3309
3310         stack_trap "cdt_clear_no_retry" EXIT
3311         # Avoid coordinator resending this request as soon it has failed.
3312         cdt_set_no_retry
3313
3314         echo "foo" >> $f
3315         sync
3316         wait_request_state $fid ARCHIVE FAILED
3317
3318         check_hsm_flags $f "0x00000003"
3319 }
3320 run_test 54 "Write during an archive cancels it"
3321
3322 test_55() {
3323         # test needs a running copytool
3324         copytool setup -b 1
3325
3326         local f=$DIR/$tdir/$tfile
3327         local fid=$(make_custom_file_for_progress $f 39 1000000)
3328
3329         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3330                 error "could not archive file"
3331         wait_request_state $fid ARCHIVE STARTED
3332
3333         check_hsm_flags $f "0x00000001"
3334
3335         stack_trap "cdt_clear_no_retry" EXIT
3336         # Avoid coordinator resending this request as soon it has failed.
3337         cdt_set_no_retry
3338
3339         $TRUNCATE $f 1024 || error "truncate failed"
3340         sync
3341         wait_request_state $fid ARCHIVE FAILED
3342
3343         check_hsm_flags $f "0x00000003"
3344 }
3345 run_test 55 "Truncate during an archive cancels it"
3346
3347 test_56() {
3348         # test needs a running copytool
3349         copytool setup -b 1
3350
3351         local f=$DIR/$tdir/$tfile
3352         local fid
3353         fid=$(make_custom_file_for_progress $f 39 1000000)
3354         [ $? != 0 ] && skip "not enough free space" && return
3355
3356         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3357                 error "could not archive file"
3358         wait_request_state $fid ARCHIVE STARTED
3359
3360         check_hsm_flags $f "0x00000001"
3361
3362         # Change metadata and sync to be sure we are not changing only
3363         # in memory.
3364         chmod 644 $f
3365         chgrp sys $f
3366         sync
3367         wait_request_state $fid ARCHIVE SUCCEED
3368
3369         check_hsm_flags $f "0x00000009"
3370 }
3371 run_test 56 "Setattr during an archive is ok"
3372
3373 test_57() {
3374         # Need one client for I/O, one for request
3375         needclients 2 || return 0
3376
3377         # test needs a running copytool
3378         copytool setup
3379
3380         mkdir -p $DIR/$tdir
3381         local f=$DIR/$tdir/test_archive_remote
3382         # Create a file on a remote node
3383         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
3384                 "count=2 conv=fsync"
3385
3386         # And archive it
3387         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
3388                 error "hsm_archive failed"
3389         local fid=$(path2fid $f)
3390         wait_request_state $fid ARCHIVE SUCCEED
3391
3392         # Release and implicit restore it
3393         do_node $CLIENT2 "$LFS hsm_release $f" ||
3394                 error "hsm_release failed"
3395         do_node $CLIENT2 "md5sum $f" ||
3396                 error "hsm_restore failed"
3397
3398         wait_request_state $fid RESTORE SUCCEED
3399 }
3400 run_test 57 "Archive a file with dirty cache on another node"
3401
3402 truncate_released_file() {
3403         local src_file=$1
3404         local trunc_to=$2
3405
3406         local sz=$(stat -c %s $src_file)
3407         local f=$DIR/$tdir/$tfile
3408         local fid=$(copy_file $1 $f)
3409         local ref=$f-ref
3410         cp $f $f-ref
3411
3412         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3413                 error "could not archive file"
3414         wait_request_state $fid ARCHIVE SUCCEED
3415
3416         $LFS hsm_release $f || error "could not release file"
3417
3418         $TRUNCATE $f $trunc_to || error "truncate failed"
3419         sync
3420
3421         local sz1=$(stat -c %s $f)
3422         [[ $sz1 == $trunc_to ]] ||
3423                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3424
3425         $LFS hsm_state $f
3426         check_hsm_flags $f "0x0000000b"
3427
3428         local state=$(get_request_state $fid RESTORE)
3429         [[ "$state" == "SUCCEED" ]] ||
3430                 error "truncate $sz does not trig restore, state = $state"
3431
3432         $TRUNCATE $ref $trunc_to
3433         cmp $ref $f || error "file data wrong after truncate"
3434
3435         rm -f $f $f-ref
3436 }
3437
3438 test_58() {
3439         # test needs a running copytool
3440         copytool setup
3441
3442         mkdir -p $DIR/$tdir
3443
3444         local sz=$(stat -c %s /etc/passwd)
3445
3446         echo "truncate up from $sz to $((sz*2))"
3447         truncate_released_file /etc/passwd $((sz*2))
3448
3449         echo "truncate down from $sz to $((sz/2))"
3450         truncate_released_file /etc/passwd $((sz/2))
3451
3452         echo "truncate to 0"
3453         truncate_released_file /etc/passwd 0
3454 }
3455 run_test 58 "Truncate a released file will trigger restore"
3456
3457 test_59() {
3458         local fid
3459         local server_version=$(lustre_version_code $SINGLEMDS)
3460         [[ $server_version -lt $(version_code 2.7.63) ]] &&
3461                 skip "Need MDS version at least 2.7.63" && return
3462
3463         copytool setup
3464         $MCREATE $DIR/$tfile || error "mcreate failed"
3465         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3466         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3467         fid=$(path2fid $DIR/$tfile)
3468         wait_request_state $fid ARCHIVE SUCCEED
3469         $LFS hsm_release $DIR/$tfile || error "release failed"
3470 }
3471 run_test 59 "Release stripeless file with non-zero size"
3472
3473 test_60() {
3474         # This test validates the fix for LU-4512. Ensure that the -u
3475         # option changes the progress reporting interval from the
3476         # default (30 seconds) to the user-specified interval.
3477         local interval=5
3478         local progress_timeout=$((interval * 4))
3479
3480         copytool setup -b 1 --update-interval $interval
3481
3482         local f=$DIR/$tdir/$tfile
3483         local fid
3484         fid=$(make_custom_file_for_progress $f 10)
3485         [ $? != 0 ] && skip "not enough free space" && return
3486
3487         local mdtidx=0
3488         local mdt=${MDT_PREFIX}${mdtidx}
3489         local mds=mds$((mdtidx + 1))
3490
3491         # Wait for copytool to register
3492         wait_update_facet $mds \
3493                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3494                 uuid 100 || error "coyptool failed to register with $mdt"
3495
3496         local start_at=$(date +%s)
3497         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3498                 error "could not archive file"
3499
3500         local agent=$(facet_active_host $SINGLEAGT)
3501         local logfile=$(copytool_logfile $SINGLEAGT)
3502
3503         wait_update $agent \
3504             "grep -o start.copy \"$logfile\"" "start copy" 100 ||
3505                 error "copytool failed to start"
3506
3507         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3508         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3509
3510         local RESULT
3511         local WAIT=0
3512         local sleep=1
3513
3514         echo -n "Expecting a progress update within $progress_timeout seconds... "
3515         while [ true ]; do
3516                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3517                 if [ -n "$RESULT" ] && [ "$RESULT" -gt 0 ]; then
3518                         echo "$RESULT bytes copied in $WAIT seconds."
3519                         break
3520                 elif [ $WAIT -ge $progress_timeout ]; then
3521                         error "Timed out waiting for progress update!"
3522                         break
3523                 fi
3524                 WAIT=$((WAIT + sleep))
3525                 sleep $sleep
3526         done
3527
3528         local finish_at=$(date +%s)
3529         local elapsed=$((finish_at - start_at))
3530
3531         # Ensure that the progress update occurred within the expected window.
3532         if [ $elapsed -lt $((interval - 1)) ]; then
3533                 error "Expected progress update after at least $interval seconds"
3534         fi
3535
3536         echo "Wait for on going archive hsm action to complete"
3537         wait_update $agent "grep -o copied \"$logfile\"" "copied" 10 ||
3538                 echo "File archiving not completed even after 10 secs"
3539 }
3540 run_test 60 "Changing progress update interval from default"
3541
3542 test_61() {
3543         # test needs a running copytool
3544         copytool setup
3545
3546         mkdir -p $DIR/$tdir
3547         local f=$DIR/$tdir/$tfile
3548         local fid=$(copy_file /etc/passwd $f)
3549         cdt_disable
3550         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3551         rm -f $f
3552         cdt_enable
3553         wait_request_state $fid ARCHIVE FAILED
3554 }
3555 run_test 61 "Waiting archive of a removed file should fail"
3556
3557 test_70() {
3558         # test needs a new running copytool
3559         stack_trap copytool_monitor_cleanup EXIT
3560         copytool_monitor_setup
3561         copytool setup --event-fifo "$HSMTOOL_MONITOR_DIR/fifo"
3562
3563         # Wait for the copytool to register.
3564         wait_update --verbose $(facet_active_host mds1) \
3565                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3566                 uuid 100 ||
3567                 error "copytool failed to register with MDT0000"
3568
3569         kill_copytools
3570         wait_copytools || error "Copytools failed to stop"
3571
3572         local REGISTER_EVENT
3573         local UNREGISTER_EVENT
3574         while read event; do
3575                 local parsed=$(parse_json_event "$event")
3576                 if [ -z "$parsed" ]; then
3577                         error "Copytool sent malformed event: $event"
3578                 fi
3579                 eval $parsed
3580
3581                 if [ $event_type == "REGISTER" ]; then
3582                         REGISTER_EVENT=$event
3583                 elif [ $event_type == "UNREGISTER" ]; then
3584                         UNREGISTER_EVENT=$event
3585                 fi
3586         done < <(echo $"$(get_copytool_event_log)")
3587
3588         if [ -z "$REGISTER_EVENT" ]; then
3589                 error "Copytool failed to send register event to FIFO"
3590         fi
3591
3592         if [ -z "$UNREGISTER_EVENT" ]; then
3593                 error "Copytool failed to send unregister event to FIFO"
3594         fi
3595
3596         echo "Register/Unregister events look OK."
3597 }
3598 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3599
3600 test_71() {
3601         # Bump progress interval for livelier events.
3602         local interval=5
3603
3604         # test needs a new running copytool
3605         stack_trap copytool_monitor_cleanup EXIT
3606         copytool_monitor_setup
3607         copytool setup --update-interval $interval --event-fifo \
3608                 "$HSMTOOL_MONITOR_DIR/fifo"
3609
3610         stack_trap "cdt_clear_no_retry" EXIT
3611         # Just start and stop the copytool to generate events.
3612         cdt_clear_no_retry
3613
3614         local f=$DIR/$tdir/$tfile
3615         local fid
3616         fid=$(make_custom_file_for_progress $f 39 1000000)
3617         [ $? != 0 ] && skip "not enough free space" && return
3618
3619         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3620                 error "could not archive file"
3621         wait_request_state $fid ARCHIVE SUCCEED
3622
3623         local expected_fields="event_time data_fid source_fid"
3624         expected_fields+=" total_bytes current_bytes"
3625
3626         local START_EVENT
3627         local FINISH_EVENT
3628         while read event; do
3629                 # Make sure we're not getting anything from previous events.
3630                 for field in $expected_fields; do
3631                         unset $field
3632                 done
3633
3634                 local parsed=$(parse_json_event "$event")
3635                 if [ -z "$parsed" ]; then
3636                         error "Copytool sent malformed event: $event"
3637                 fi
3638                 eval $parsed
3639
3640                 if [ $event_type == "ARCHIVE_START" ]; then
3641                         START_EVENT=$event
3642                         continue
3643                 elif [ $event_type == "ARCHIVE_FINISH" ]; then
3644                         FINISH_EVENT=$event
3645                         continue
3646                 elif [ $event_type != "ARCHIVE_RUNNING" ]; then
3647                         continue
3648                 fi
3649
3650                 # Do some simple checking of the progress update events.
3651                 for expected_field in $expected_fields; do
3652                         if [ -z ${!expected_field+x} ]; then
3653                                 error "Missing $expected_field field in event"
3654                         fi
3655                 done
3656
3657                 if [ $total_bytes -eq 0 ]; then
3658                         error "Expected total_bytes to be > 0"
3659                 fi
3660
3661                 # These should be identical throughout an archive
3662                 # operation.
3663                 if [ $source_fid != $data_fid ]; then
3664                         error "Expected source_fid to equal data_fid"
3665                 fi
3666         done < <(echo $"$(get_copytool_event_log)")
3667
3668         if [ -z "$START_EVENT" ]; then
3669                 error "Copytool failed to send archive start event to FIFO"
3670         fi
3671
3672         if [ -z "$FINISH_EVENT" ]; then
3673                 error "Copytool failed to send archive finish event to FIFO"
3674         fi
3675
3676         echo "Archive events look OK."
3677 }
3678 run_test 71 "Copytool logs JSON archive events to FIFO"
3679
3680 test_72() {
3681         # Bump progress interval for livelier events.
3682         local interval=5
3683
3684         # test needs a new running copytool
3685         copytool_cleanup
3686         copytool_monitor_setup
3687         HSMTOOL_UPDATE_INTERVAL=$interval \
3688         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3689         local test_file=$HSMTOOL_MONITOR_DIR/file
3690
3691         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3692         cmd+="conv=fsync"
3693         do_facet $SINGLEAGT "$cmd" ||
3694                 error "cannot create $test_file on $SINGLEAGT"
3695         copy2archive $test_file $tdir/$tfile
3696
3697         mkdir -p $DIR/$tdir
3698         local f=$DIR/$tdir/$tfile
3699         import_file $tdir/$tfile $f
3700         f=$DIR2/$tdir/$tfile
3701         echo "Verifying released state: "
3702         check_hsm_flags $f "0x0000000d"
3703
3704         local fid=$(path2fid $f)
3705         $LFS hsm_restore $f
3706         wait_request_state $fid RESTORE SUCCEED
3707
3708         local expected_fields="event_time data_fid source_fid"
3709         expected_fields+=" total_bytes current_bytes"
3710
3711         local START_EVENT
3712         local FINISH_EVENT
3713         while read event; do
3714                 # Make sure we're not getting anything from previous events.
3715                 for field in $expected_fields; do
3716                         unset $field
3717                 done
3718
3719                 local parsed=$(parse_json_event "$event")
3720                 if [ -z "$parsed" ]; then
3721                         error "Copytool sent malformed event: $event"
3722                 fi
3723                 eval $parsed
3724
3725                 if [ $event_type == "RESTORE_START" ]; then
3726                         START_EVENT=$event
3727                         if [ $source_fid != $data_fid ]; then
3728                                 error "source_fid should == data_fid at start"
3729                         fi
3730                         continue
3731                 elif [ $event_type == "RESTORE_FINISH" ]; then
3732                         FINISH_EVENT=$event
3733                         if [ $source_fid != $data_fid ]; then
3734                                 error "source_fid should == data_fid at finish"
3735                         fi
3736                         continue
3737                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3738                         continue
3739                 fi
3740
3741                 # Do some simple checking of the progress update events.
3742                 for expected_field in $expected_fields; do
3743                         if [ -z ${!expected_field+x} ]; then
3744                                 error "Missing $expected_field field in event"
3745                         fi
3746                 done
3747
3748                 if [ $total_bytes -eq 0 ]; then
3749                         error "Expected total_bytes to be > 0"
3750                 fi
3751
3752                 # When a restore starts out, the data fid is the same as the
3753                 # source fid. After the restore has gotten going, we learn
3754                 # the new data fid. Once the restore has finished, the source
3755                 # fid is set to the new data fid.
3756                 #
3757                 # We test this because some monitoring software may depend on
3758                 # this behavior. If it changes, then the consumers of these
3759                 # events may need to be modified.
3760                 if [ $source_fid == $data_fid ]; then
3761                         error "source_fid should != data_fid during restore"
3762                 fi
3763         done < <(echo $"$(get_copytool_event_log)")
3764
3765         if [ -z "$START_EVENT" ]; then
3766                 error "Copytool failed to send restore start event to FIFO"
3767         fi
3768
3769         if [ -z "$FINISH_EVENT" ]; then
3770                 error "Copytool failed to send restore finish event to FIFO"
3771         fi
3772
3773         echo "Restore events look OK."
3774
3775         cdt_clear_no_retry
3776         copytool_cleanup
3777         copytool_monitor_cleanup
3778
3779         rm -rf $test_dir
3780 }
3781 run_test 72 "Copytool logs JSON restore events to FIFO"
3782
3783 test_90() {
3784         file_count=51 # Max number of files constrained by LNET message size
3785         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3786         local f=$DIR/$tdir/$tfile
3787         local FILELIST=/tmp/filelist.txt
3788         local i=""
3789
3790         rm -f $FILELIST
3791         for i in $(seq 1 $file_count); do
3792                 fid=$(copy_file /etc/hosts $f.$i)
3793                 echo $f.$i >> $FILELIST
3794         done
3795
3796         copytool setup
3797         # to be sure wait_all_done will not be mislead by previous tests
3798         cdt_purge
3799         wait_for_grace_delay
3800         $LFS hsm_archive --filelist $FILELIST ||
3801                 error "cannot archive a file list"
3802         wait_all_done 100
3803         $LFS hsm_release --filelist $FILELIST ||
3804                 error "cannot release a file list"
3805         $LFS hsm_restore --filelist $FILELIST ||
3806                 error "cannot restore a file list"
3807         wait_all_done 100
3808 }
3809 run_test 90 "Archive/restore a file list"
3810
3811 double_verify_reset_hsm_param() {
3812         local p=$1
3813         echo "Testing $HSM_PARAM.$p"
3814         local val=$(get_hsm_param $p)
3815         local save=$val
3816         local val2=$(($val * 2))
3817         set_hsm_param $p $val2
3818         val=$(get_hsm_param $p)
3819         [[ $val == $val2 ]] ||
3820                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3821         echo "Set $p to 0 must failed"
3822         set_hsm_param $p 0
3823         local rc=$?
3824         # restore value
3825         set_hsm_param $p $save
3826
3827         if [[ $rc == 0 ]]; then
3828                 error "we must not be able to set $HSM_PARAM.$p to 0"
3829         fi
3830 }
3831
3832 test_100() {
3833         double_verify_reset_hsm_param loop_period
3834         double_verify_reset_hsm_param grace_delay
3835         double_verify_reset_hsm_param active_request_timeout
3836         double_verify_reset_hsm_param max_requests
3837         double_verify_reset_hsm_param default_archive_id
3838 }
3839 run_test 100 "Set coordinator /proc tunables"
3840
3841 test_102() {
3842         cdt_disable
3843         cdt_enable
3844         cdt_restart
3845 }
3846 run_test 102 "Verify coordinator control"
3847
3848 test_103() {
3849         # test needs a running copytool
3850         copytool setup
3851
3852         local i=""
3853         local fid=""
3854
3855         mkdir -p $DIR/$tdir
3856         for i in $(seq 1 20); do
3857                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3858         done
3859         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3860
3861         cdt_purge
3862
3863         echo "Current requests"
3864         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3865                         $HSM_PARAM.actions |\
3866                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3867
3868         [[ -z "$res" ]] || error "Some request have not been canceled"
3869 }
3870 run_test 103 "Purge all requests"
3871
3872 DATA=CEA
3873 DATAHEX='[434541]'
3874 test_104() {
3875         local f=$DIR/$tdir/$tfile
3876         local fid
3877         fid=$(make_custom_file_for_progress $f 39 1000000)
3878         [ $? != 0 ] && skip "not enough free space" && return
3879
3880         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3881         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3882                         $HSM_PARAM.actions |\
3883                         grep $fid | cut -f16 -d=")
3884
3885         [[ "$data1" == "$DATAHEX" ]] ||
3886                 error "Data field in records is ($data1) and not ($DATAHEX)"
3887
3888         # archive the file
3889         copytool setup
3890
3891         wait_request_state $fid ARCHIVE SUCCEED
3892 }
3893 run_test 104 "Copy tool data field"
3894
3895 test_105() {
3896         local max_requests=$(get_hsm_param max_requests)
3897         mkdir -p $DIR/$tdir
3898         local i=""
3899
3900         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3901         set_hsm_param max_requests 300
3902
3903         cdt_disable
3904         for i in $(seq -w 1 10); do
3905                 cp /etc/passwd $DIR/$tdir/$i
3906                 $LFS hsm_archive $DIR/$tdir/$i
3907         done
3908         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3909                         $HSM_PARAM.actions |\
3910                         grep WAITING | wc -l")
3911         cdt_restart
3912
3913         cdt_disable
3914         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3915                         $HSM_PARAM.actions |\
3916                         grep WAITING | wc -l")
3917         cdt_enable
3918         cdt_purge
3919         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3920                 error "Requests count after shutdown $reqcnt2 != "\
3921                       "before shutdown $reqcnt1"
3922 }
3923 run_test 105 "Restart of coordinator"
3924
3925 test_106() {
3926         # test needs a running copytool
3927         copytool setup
3928
3929         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3930
3931         check_agent_registered $uuid
3932
3933         search_copytools || error "No copytool found"
3934
3935         kill_copytools
3936         wait_copytools || error "Copytool failed to stop"
3937
3938         check_agent_unregistered $uuid
3939
3940         copytool setup
3941         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3942         check_agent_registered $uuid
3943 }
3944 run_test 106 "Copytool register/unregister"
3945
3946 test_107() {
3947         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
3948
3949         # test needs a running copytool
3950         copytool setup
3951         # create and archive file
3952         mkdir -p $DIR/$tdir
3953         local f1=$DIR/$tdir/$tfile
3954         local fid=$(copy_file /etc/passwd $f1)
3955         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3956         wait_request_state $fid ARCHIVE SUCCEED
3957         # shutdown and restart MDS
3958         fail $SINGLEMDS
3959         # check the copytool still gets messages from MDT
3960         local f2=$DIR/$tdir/2
3961         local fid=$(copy_file /etc/passwd $f2)
3962         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3963         # main check of this sanity: this request MUST succeed
3964         wait_request_state $fid ARCHIVE SUCCEED
3965 }
3966 run_test 107 "Copytool re-register after MDS restart"
3967
3968 policy_set_and_test()
3969 {
3970         local change="$1"
3971         local target="$2"
3972         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3973         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3974         [[ "$policy" == "$target" ]] ||
3975                 error "Wrong policy after '$change': '$policy' != '$target'"
3976 }
3977
3978 test_109() {
3979         # to force default policy setting if error
3980         CDT_POLICY_HAD_CHANGED=true
3981
3982         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3983         local default="NonBlockingRestore [NoRetryAction]"
3984         [[ "$policy" == "$default" ]] ||
3985                 error "default policy has changed,"\
3986                       " '$policy' != '$default' update the test"
3987         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3988         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3989         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3990         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3991         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
3992         # useless bacause we know but safer for futur changes to use real value
3993         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3994         echo "Next set_param must failed"
3995         policy_set_and_test "wrong" "$policy"
3996
3997         # return to default
3998         echo "Back to default policy"
3999         cdt_set_sanity_policy
4000 }
4001 run_test 109 "Policy display/change"
4002
4003 test_110a() {
4004         # test needs a running copytool
4005         copytool_setup
4006
4007         mkdir -p $DIR/$tdir
4008
4009         copy2archive /etc/passwd $tdir/$tfile
4010
4011         local f=$DIR/$tdir/$tfile
4012         import_file $tdir/$tfile $f
4013         local fid=$(path2fid $f)
4014
4015         cdt_set_non_blocking_restore
4016         md5sum $f
4017         local st=$?
4018
4019         # cleanup
4020         wait_request_state $fid RESTORE SUCCEED
4021         cdt_clear_non_blocking_restore
4022
4023         # Test result
4024         [[ $st == 1 ]] ||
4025                 error "md5sum returns $st != 1, "\
4026                         "should also perror ENODATA (No data available)"
4027 }
4028 run_test 110a "Non blocking restore policy (import case)"
4029
4030 test_110b() {
4031         # test needs a running copytool
4032         copytool setup
4033
4034         mkdir -p $DIR/$tdir
4035         local f=$DIR/$tdir/$tfile
4036         local fid=$(copy_file /etc/passwd $f)
4037         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4038         wait_request_state $fid ARCHIVE SUCCEED
4039         $LFS hsm_release $f
4040
4041         cdt_set_non_blocking_restore
4042         md5sum $f
4043         local st=$?
4044
4045         # cleanup
4046         wait_request_state $fid RESTORE SUCCEED
4047         cdt_clear_non_blocking_restore
4048
4049         # Test result
4050         [[ $st == 1 ]] ||
4051                 error "md5sum returns $st != 1, "\
4052                         "should also perror ENODATA (No data available)"
4053 }
4054 run_test 110b "Non blocking restore policy (release case)"
4055
4056 test_111a() {
4057         # test needs a running copytool
4058         copytool_setup
4059
4060         mkdir -p $DIR/$tdir
4061         copy2archive /etc/passwd $tdir/$tfile
4062
4063         local f=$DIR/$tdir/$tfile
4064
4065         import_file $tdir/$tfile $f
4066         local fid=$(path2fid $f)
4067
4068         cdt_set_no_retry
4069
4070         copytool_remove_backend $fid
4071
4072         $LFS hsm_restore $f
4073         wait_request_state $fid RESTORE FAILED
4074         local st=$?
4075
4076         # cleanup
4077         cdt_clear_no_retry
4078
4079         # Test result
4080         [[ $st == 0 ]] || error "Restore does not failed"
4081
4082         copytool_cleanup
4083 }
4084 run_test 111a "No retry policy (import case), restore will error"\
4085               " (No such file or directory)"
4086
4087 test_111b() {
4088         # test needs a running copytool
4089         copytool_setup
4090
4091         mkdir -p $DIR/$tdir
4092         local f=$DIR/$tdir/$tfile
4093         local fid=$(copy_file /etc/passwd $f)
4094         cdt_set_no_retry
4095         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4096         wait_request_state $fid ARCHIVE SUCCEED
4097         $LFS hsm_release $f
4098
4099         copytool_remove_backend $fid
4100
4101         $LFS hsm_restore $f
4102         wait_request_state $fid RESTORE FAILED
4103         local st=$?
4104
4105         # cleanup
4106         cdt_clear_no_retry
4107
4108         # Test result
4109         [[ $st == 0 ]] || error "Restore does not failed"
4110
4111         copytool_cleanup
4112 }
4113 run_test 111b "No retry policy (release case), restore will error"\
4114               " (No such file or directory)"
4115
4116 test_112() {
4117         # test needs a running copytool
4118         copytool setup
4119
4120         mkdir -p $DIR/$tdir
4121         local f=$DIR/$tdir/$tfile
4122         local fid=$(copy_file /etc/passwd $f)
4123         cdt_disable
4124         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4125         local l=$($LFS hsm_action $f)
4126         echo $l
4127         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
4128
4129         cdt_enable
4130         wait_request_state $fid ARCHIVE SUCCEED
4131
4132         # Test result
4133         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
4134 }
4135 run_test 112 "State of recorded request"
4136
4137 test_200() {
4138         # test needs a running copytool
4139         copytool setup
4140
4141         local f=$DIR/$tdir/$tfile
4142         local fid
4143         fid=$(make_custom_file_for_progress $f 103 1048576)
4144         [ $? != 0 ] && skip "not enough free space" && return
4145
4146         # test with cdt on is made in test_221
4147         cdt_disable
4148         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4149         # wait archive to register at CDT
4150         wait_request_state $fid ARCHIVE WAITING
4151         $LFS hsm_cancel $f
4152         cdt_enable
4153         wait_request_state $fid ARCHIVE CANCELED
4154         wait_request_state $fid CANCEL SUCCEED
4155 }
4156 run_test 200 "Register/Cancel archive"
4157
4158 test_201() {
4159         # test needs a running copytool
4160         copytool_setup
4161
4162         local f=$DIR/$tdir/$tfile
4163         create_archive_file $tdir/$tfile
4164         import_file $tdir/$tfile $f
4165         local fid=$(path2fid $f)
4166
4167         # test with cdt on is made in test_222
4168         cdt_disable
4169         $LFS hsm_restore $f
4170         # wait restore to register at CDT
4171         wait_request_state $fid RESTORE WAITING
4172         $LFS hsm_cancel $f
4173         cdt_enable
4174         wait_request_state $fid RESTORE CANCELED
4175         wait_request_state $fid CANCEL SUCCEED
4176
4177         copytool_cleanup
4178 }
4179 run_test 201 "Register/Cancel restore"
4180
4181 test_202() {
4182         # test needs a running copytool
4183         copytool setup
4184
4185         local f=$DIR/$tdir/$tfile
4186         local fid
4187         fid=$(make_custom_file_for_progress $f 39 1000000)
4188         [ $? != 0 ] && skip "not enough free space" && return
4189
4190         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4191         wait_request_state $fid ARCHIVE SUCCEED
4192
4193         cdt_disable
4194         $LFS hsm_remove $f
4195         # wait remove to register at CDT
4196         wait_request_state $fid REMOVE WAITING
4197         $LFS hsm_cancel $f
4198         cdt_enable
4199         wait_request_state $fid REMOVE CANCELED
4200 }
4201 run_test 202 "Register/Cancel remove"
4202
4203 test_220A() { # was test_220
4204         # test needs a running copytool
4205         copytool setup
4206
4207         mkdir -p $DIR/$tdir
4208
4209         local f=$DIR/$tdir/$tfile
4210         local fid=$(copy_file /etc/passwd $f)
4211
4212         changelog_setup
4213
4214         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4215         wait_request_state $fid ARCHIVE SUCCEED
4216
4217         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4218         changelog_cleanup
4219
4220         local target=0x0
4221         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4222 }
4223 run_test 220A "Changelog for archive"
4224
4225 test_220a() {
4226         # test needs a running copytool
4227         copytool setup
4228
4229         mkdir -p $DIR/$tdir
4230
4231         local f=$DIR/$tdir/$tfile
4232         local fid=$(copy_file /etc/passwd $f)
4233
4234         changelog_setup
4235
4236         # block copytool operations to allow for HSM request to be
4237         # submitted and file be unlinked (CDT will find object removed)
4238         copytool_suspend
4239
4240         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4241
4242         # wait request to reach CT
4243         wait_request_state $fid ARCHIVE STARTED
4244
4245         rm -f $f
4246
4247         copytool_continue
4248
4249         wait_request_state $fid ARCHIVE FAILED
4250
4251         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4252         changelog_cleanup
4253
4254         # HE_ARCHIVE|ENOENT
4255         local target=0x2
4256         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4257 }
4258 run_test 220a "Changelog for failed archive"
4259
4260 test_221() {
4261         # test needs a running copytool
4262         copytool setup -b 1
4263
4264         local f=$DIR/$tdir/$tfile
4265         local fid
4266         fid=$(make_custom_file_for_progress $f 103 1048576)
4267         [ $? != 0 ] && skip "not enough free space" && return
4268
4269         changelog_setup
4270
4271         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4272         wait_request_state $fid ARCHIVE STARTED
4273         $LFS hsm_cancel $f
4274         wait_request_state $fid ARCHIVE CANCELED
4275         wait_request_state $fid CANCEL SUCCEED
4276
4277         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4278
4279         local target=0x7d
4280         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4281 }
4282 run_test 221 "Changelog for archive canceled"
4283
4284 test_222a() {
4285         # test needs a running copytool
4286         copytool_setup
4287
4288         mkdir -p $DIR/$tdir
4289         copy2archive /etc/passwd $tdir/$tfile
4290
4291         local f=$DIR/$tdir/$tfile
4292         import_file $tdir/$tfile $f
4293         local fid=$(path2fid $f)
4294
4295         changelog_setup
4296
4297         $LFS hsm_restore $f
4298         wait_request_state $fid RESTORE SUCCEED
4299
4300         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4301
4302         local target=0x80
4303         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4304
4305         copytool_cleanup
4306 }
4307 run_test 222a "Changelog for explicit restore"
4308
4309 test_222b() {
4310         # test needs a running copytool
4311         copytool setup
4312
4313         mkdir -p $DIR/$tdir
4314         local f=$DIR/$tdir/$tfile
4315         local fid=$(copy_file /etc/passwd $f)
4316
4317         changelog_setup
4318         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4319         wait_request_state $fid ARCHIVE SUCCEED
4320         $LFS hsm_release $f
4321
4322         md5sum $f
4323
4324         wait_request_state $fid RESTORE SUCCEED
4325
4326         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4327
4328         local target=0x80
4329         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4330 }
4331 run_test 222b "Changelog for implicit restore"
4332
4333 test_222c() {
4334         # test needs a running copytool
4335         copytool_setup
4336
4337         mkdir -p $DIR/$tdir
4338         copy2archive /etc/passwd $tdir/$tfile
4339
4340         local f=$DIR/$tdir/$tfile
4341         import_file $tdir/$tfile $f
4342         local fid=$(path2fid $f)
4343
4344         changelog_setup
4345
4346         # block copytool operations to allow for HSM request to be
4347         # submitted and file be unlinked (CDT will find object removed)
4348         copytool_suspend
4349
4350         $LFS hsm_restore $f
4351
4352         # wait request to reach CT
4353         wait_request_state $fid RESTORE STARTED
4354
4355         rm -f $f
4356
4357         copytool_continue
4358
4359         wait_request_state $fid RESTORE FAILED
4360
4361         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4362
4363         # HE_RESTORE|ENOENT
4364         local target=0x82
4365         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4366
4367         copytool_cleanup
4368 }
4369 run_test 222c "Changelog for failed explicit restore"
4370
4371 test_222d() {
4372         # test needs a running copytool
4373         copytool_setup
4374
4375         mkdir -p $DIR/$tdir
4376         local f=$DIR/$tdir/$tfile
4377         local fid=$(copy_file /etc/passwd $f)
4378
4379         changelog_setup
4380         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4381         wait_request_state $fid ARCHIVE SUCCEED
4382         $LFS hsm_release $f
4383
4384         copytool_remove_backend $fid
4385         md5sum $f
4386
4387         wait_request_state $fid RESTORE FAILED
4388
4389         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4390
4391         # HE_RESTORE|ENOENT
4392         local target=0x82
4393         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4394
4395         copytool_cleanup
4396 }
4397 run_test 222d "Changelog for failed implicit restore"
4398
4399 test_223a() {
4400         # test needs a running copytool
4401         copytool_setup
4402
4403         local f=$DIR/$tdir/$tfile
4404         create_archive_file $tdir/$tfile
4405
4406         changelog_setup
4407
4408         import_file $tdir/$tfile $f
4409         local fid=$(path2fid $f)
4410
4411         $LFS hsm_restore $f
4412         wait_request_state $fid RESTORE STARTED
4413         $LFS hsm_cancel $f
4414         wait_request_state $fid RESTORE CANCELED
4415         wait_request_state $fid CANCEL SUCCEED
4416
4417         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4418
4419         local target=0xfd
4420         [[ $flags == $target ]] ||
4421                 error "Changelog flag is $flags not $target"
4422
4423         cleanup
4424 }
4425 run_test 223a "Changelog for restore canceled (import case)"
4426
4427 test_223b() {
4428         # test needs a running copytool
4429         copytool setup -b 1
4430
4431         local f=$DIR/$tdir/$tfile
4432         local fid
4433         fid=$(make_custom_file_for_progress $f 39 1000000)
4434         [ $? != 0 ] && skip "not enough free space" && return
4435
4436         changelog_setup
4437         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4438         wait_request_state $fid ARCHIVE SUCCEED
4439         $LFS hsm_release $f
4440         $LFS hsm_restore $f
4441         wait_request_state $fid RESTORE STARTED
4442         $LFS hsm_cancel $f
4443         wait_request_state $fid RESTORE CANCELED
4444         wait_request_state $fid CANCEL SUCCEED
4445
4446         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4447
4448         local target=0xfd
4449         [[ $flags == $target ]] ||
4450                 error "Changelog flag is $flags not $target"
4451
4452         copytool_cleanup
4453 }
4454 run_test 223b "Changelog for restore canceled (release case)"
4455
4456 test_224A() { # was test_224
4457         # test needs a running copytool
4458         copytool setup
4459
4460         mkdir -p $DIR/$tdir
4461
4462         local f=$DIR/$tdir/$tfile
4463         local fid=$(copy_file /etc/passwd $f)
4464
4465         changelog_setup
4466         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4467         wait_request_state $fid ARCHIVE SUCCEED
4468
4469         $LFS hsm_remove $f
4470         wait_request_state $fid REMOVE SUCCEED
4471
4472         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4473
4474         local target=0x200
4475         [[ $flags == $target ]] ||
4476                 error "Changelog flag is $flags not $target"
4477 }
4478 run_test 224A "Changelog for remove"
4479
4480 test_224a() {
4481         # test needs a running copytool
4482         copytool_setup
4483
4484         mkdir -p $DIR/$tdir
4485
4486         local f=$DIR/$tdir/$tfile
4487         local fid=$(copy_file /etc/passwd $f)
4488
4489         changelog_setup
4490         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4491         wait_request_state $fid ARCHIVE SUCCEED
4492
4493         copytool_remove_backend $fid
4494
4495         # block copytool operations to allow for HSM request to be
4496         # submitted and file be unlinked (CDT will find object removed)
4497         copytool_suspend
4498
4499         $LFS hsm_remove $f
4500
4501         # wait for request to reach CT
4502         wait_request_state $fid REMOVE STARTED
4503
4504         rm -f $f
4505
4506         copytool_continue
4507
4508         wait_request_state $fid REMOVE FAILED
4509
4510         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4511
4512         # HE_REMOVE|ENOENT
4513         local target=0x202
4514         [[ $flags == $target ]] ||
4515                 error "Changelog flag is $flags not $target"
4516
4517         cleanup
4518 }
4519 run_test 224a "Changelog for failed remove"
4520
4521 test_225() {
4522         # test needs a running copytool
4523         copytool setup
4524
4525         # test is not usable because remove request is too fast
4526         # so it is always finished before cancel can be done ...
4527         echo "Test disabled"
4528         copytool_cleanup
4529         return 0
4530
4531         local f=$DIR/$tdir/$tfile
4532         local fid
4533         fid=$(make_custom_file_for_progress $f 39 1000000)
4534         [ $? != 0 ] && skip "not enough free space" && return
4535
4536         changelog_setup
4537         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4538         wait_request_state $fid ARCHIVE SUCCEED
4539
4540         # if cdt is on, it can serve too quickly the request
4541         cdt_disable
4542         $LFS hsm_remove $f
4543         $LFS hsm_cancel $f
4544         cdt_enable
4545         wait_request_state $fid REMOVE CANCELED
4546         wait_request_state $fid CANCEL SUCCEED
4547
4548         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4549         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
4550                 tail -n 1 | awk '{print $5}')
4551
4552         local target=0x27d
4553         [[ $flags == $target ]] ||
4554                 error "Changelog flag is $flags not $target"
4555 }
4556 run_test 225 "Changelog for remove canceled"
4557
4558 test_226() {
4559         # test needs a running copytool
4560         copytool setup
4561
4562         mkdir -p $DIR/$tdir
4563
4564         local f1=$DIR/$tdir/$tfile-1
4565         local f2=$DIR/$tdir/$tfile-2
4566         local f3=$DIR/$tdir/$tfile-3
4567         local fid1=$(copy_file /etc/passwd $f1)
4568         local fid2=$(copy_file /etc/passwd $f2)
4569         copy_file /etc/passwd $f3
4570
4571         changelog_setup
4572         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4573         wait_request_state $fid1 ARCHIVE SUCCEED
4574
4575         $LFS hsm_archive $f2
4576         wait_request_state $fid2 ARCHIVE SUCCEED
4577
4578         rm $f1 || error "rm $f1 failed"
4579
4580         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
4581
4582         local target=0x3
4583         [[ $flags == $target ]] ||
4584                 error "Changelog flag is $flags not $target"
4585
4586         mv $f3 $f2 || error "mv $f3 $f2 failed"
4587
4588         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4589
4590         target=0x3
4591         [[ $flags == $target ]] ||
4592                 error "Changelog flag is $flags not $target"
4593 }
4594 run_test 226 "changelog for last rm/mv with exiting archive"
4595
4596 check_flags_changes() {
4597         local f=$1
4598         local fid=$2
4599         local hsm_flag=$3
4600         local fst=$4
4601         local cnt=$5
4602
4603         local target=0x280
4604         $LFS hsm_set --$hsm_flag $f ||
4605                 error "Cannot set $hsm_flag on $f"
4606         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4607         local seen=${#flags[*]}
4608         cnt=$((fst + cnt))
4609         [[ $seen == $cnt ]] ||
4610                 error "set $hsm_flag: Changelog events $seen != $cnt"
4611         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4612                 error "set $hsm_flag: Changelog flags are "\
4613                         "${flags[$((cnt - 1))]} not $target"
4614
4615         $LFS hsm_clear --$hsm_flag $f ||
4616                 error "Cannot clear $hsm_flag on $f"
4617         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4618         seen=${#flags[*]}
4619         cnt=$(($cnt + 1))
4620         [[ $cnt == $seen ]] ||
4621                 error "clear $hsm_flag: Changelog events $seen != $cnt"
4622
4623         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4624                 error "clear $hsm_flag: Changelog flag is "\
4625                         "${flags[$((cnt - 1))]} not $target"
4626 }
4627
4628 test_227() {
4629         # test needs a running copytool
4630         copytool setup
4631         changelog_setup
4632
4633         mkdir -p $DIR/$tdir
4634         typeset -a flags
4635
4636         for i in norelease noarchive exists archived
4637         do
4638                 local f=$DIR/$tdir/$tfile-$i
4639                 local fid=$(copy_file /etc/passwd $f)
4640                 check_flags_changes $f $fid $i 0 1
4641         done
4642
4643         f=$DIR/$tdir/$tfile---lost
4644         fid=$(copy_file /etc/passwd $f)
4645         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4646         wait_request_state $fid ARCHIVE SUCCEED
4647         check_flags_changes $f $fid lost 3 1
4648 }
4649 run_test 227 "changelog when explicit setting of HSM flags"
4650
4651 test_228() {
4652         # test needs a running copytool
4653         copytool setup
4654
4655         local fid=$(create_small_sync_file $DIR/$tfile)
4656         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4657         wait_request_state $fid ARCHIVE SUCCEED
4658
4659         $LFS hsm_release $DIR/$tfile
4660         check_hsm_flags $DIR/$tfile "0x0000000d"
4661
4662         filefrag $DIR/$tfile | grep " 1 extent found" ||
4663                 error "filefrag on released file must return only one extent"
4664
4665         # only newer versions of cp detect sparse files by stat/FIEMAP
4666         # (LU-2580)
4667         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4668                 error "copying $DIR/$tfile"
4669         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4670
4671         $LFS hsm_release $DIR/$tfile
4672         check_hsm_flags $DIR/$tfile "0x0000000d"
4673
4674         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4675
4676         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4677                 error "tar failed"
4678         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4679                 error "comparing untarred $DIR/$tfile"
4680
4681         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4682                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4683 }
4684 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4685
4686 test_250() {
4687         # test needs a running copytool
4688         copytool setup
4689
4690         mkdir -p $DIR/$tdir
4691         local maxrequest=$(get_hsm_param max_requests)
4692         local rqcnt=$(($maxrequest * 3))
4693         local i=""
4694
4695         cdt_disable
4696         for i in $(seq -w 1 $rqcnt); do
4697                 rm -f $DIR/$tdir/$i
4698                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
4699         done
4700         # we do it in 2 steps, so all requests arrive at the same time
4701         for i in $(seq -w 1 $rqcnt); do
4702                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
4703         done
4704         cdt_enable
4705         local cnt=$rqcnt
4706         local wt=$rqcnt
4707         while [[ $cnt != 0 || $wt != 0 ]]; do
4708                 sleep 1
4709                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4710                         $HSM_PARAM.actions |\
4711                         grep STARTED | grep -v CANCEL | wc -l")
4712                 [[ $cnt -le $maxrequest ]] ||
4713                         error "$cnt > $maxrequest too many started requests"
4714                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4715                         $HSM_PARAM.actions |\
4716                         grep WAITING | wc -l")
4717                 echo "max=$maxrequest started=$cnt waiting=$wt"
4718         done
4719 }
4720 run_test 250 "Coordinator max request"
4721
4722 test_251() {
4723         # test needs a running copytool
4724         copytool setup -b 1
4725
4726         local f=$DIR/$tdir/$tfile
4727         local fid
4728         fid=$(make_custom_file_for_progress $f 103 1048576)
4729         [ $? != 0 ] && skip "not enough free space" && return
4730
4731         cdt_disable
4732         # to have a short test
4733         local old_to=$(get_hsm_param active_request_timeout)
4734         set_hsm_param active_request_timeout 4
4735         # to be sure the cdt will wake up frequently so
4736         # it will be able to cancel the "old" request
4737         local old_loop=$(get_hsm_param loop_period)
4738         set_hsm_param loop_period 2
4739         cdt_enable
4740
4741         # clear locks to avoid extra delay caused by flush/cancel
4742         # and thus prevent early copytool death to timeout.
4743         cancel_lru_locks osc
4744
4745         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4746         wait_request_state $fid ARCHIVE STARTED
4747         sleep 5
4748         wait_request_state $fid ARCHIVE CANCELED
4749
4750         set_hsm_param active_request_timeout $old_to
4751         set_hsm_param loop_period $old_loop
4752 }
4753 run_test 251 "Coordinator request timeout"
4754
4755 test_252() {
4756         mkdir -p $DIR/$tdir
4757         local f=$DIR/$tdir/$tfile
4758         local fid=$(create_empty_file "$f")
4759
4760         # to have a short test
4761         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4762         set_hsm_param loop_period 1
4763
4764         copytool_setup
4765         copytool_suspend
4766
4767         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4768         wait_request_state $fid ARCHIVE STARTED
4769         rm -f $f
4770
4771         stack_trap "set_hsm_param active_request_timeout \
4772                     $(get_hsm_param active_request_timeout)" EXIT
4773         set_hsm_param active_request_timeout 1
4774
4775         wait_request_state $fid ARCHIVE CANCELED
4776         copytool_continue
4777 }
4778 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4779
4780 test_253() {
4781         local rc
4782         # test needs a running copytool
4783         copytool setup
4784
4785         mkdir -p $DIR/$tdir
4786         local f=$DIR/$tdir/$tfile
4787
4788         dd if=/dev/zero of=$f bs=1MB count=10
4789         local fid=$(path2fid $f)
4790
4791         $LFS hsm_archive $f || error "could not archive file"
4792         wait_request_state $fid ARCHIVE SUCCEED
4793
4794         # clear locks to discard inode data
4795         cancel_lru_locks osc
4796
4797         #define OBD_FAIL_MDC_MERGE              0x807
4798         $LCTL set_param fail_loc=0x807
4799
4800         #expect error here, instead of release with wrong size
4801         $LFS hsm_release $f
4802         rc=$?
4803         if ((rc == 0)); then
4804                 file_size=$(stat -c '%s' $f)
4805                 if ((file_size != 10485760)); then
4806                         error "Wrong file size after hsm_release"
4807                 fi
4808         else
4809                 echo "could not release file"
4810         fi
4811 }
4812 run_test 253 "Check for wrong file size after release"
4813
4814 test_254a()
4815 {
4816         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4817                 skip "need MDS version at least 2.10.56" && return
4818
4819         # Check that the counters are initialized to 0
4820         local count
4821         for request_type in archive restore remove; do
4822                 count="$(get_hsm_param ${request_type}_count)" ||
4823                         error "Reading ${request_type}_count failed with $?"
4824
4825                 [ "$count" -eq 0 ] ||
4826                         error "Expected ${request_type}_count to be " \
4827                               "0 != '$count'"
4828         done
4829 }
4830 run_test 254a "Request counters are initialized to zero"
4831
4832 test_254b()
4833 {
4834         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4835                 skip "need MDS version at least 2.10.56" && return
4836
4837         # The number of request to launch (at least 32)
4838         local request_count=$((RANDOM % 32 + 32))
4839         printf "Will launch %i requests of each type\n" "$request_count"
4840
4841         # Launch a copytool to process requests
4842         copytool_setup
4843
4844         # Set hsm.max_requests to allow starting all requests at the same time
4845         stack_trap \
4846                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4847         set_hsm_param max_requests "$request_count"
4848
4849         local timeout
4850         local count
4851         for request_type in archive restore remove; do
4852                 printf "Checking %s requests\n" "${request_type}"
4853                 # Suspend the copytool to give us time to read the proc files
4854                 copytool_suspend
4855
4856                 for ((i = 0; i < $request_count; i++)); do
4857                         case $request_type in
4858                         archive)
4859                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4860                                         >/dev/null 2>&1
4861                                 ;;
4862                         restore)
4863                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4864                                 ;;
4865                         esac
4866                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4867                 done
4868
4869                 # Give the coordinator 10 seconds to start every request
4870                 timeout=10
4871                 while get_hsm_param actions | grep -q WAITING; do
4872                         sleep 1
4873                         let timeout-=1
4874                         [ $timeout -gt 0 ] ||
4875                                 error "${request_type^} requests took too " \
4876                                       "long to start"
4877                 done
4878
4879                 count="$(get_hsm_param ${request_type}_count)"
4880                 [ "$count" -eq "$request_count" ] ||
4881                         error "Expected '$request_count' (!= '$count') " \
4882                               "active $request_type requests"
4883
4884                 # Let the copytool process the requests
4885                 copytool_continue
4886                 # Give it 10 seconds maximum
4887                 timeout=10
4888                 while get_hsm_param actions | grep -q STARTED; do
4889                         sleep 1
4890                         let timeout-=1
4891                         [ $timeout -gt 0 ] ||
4892                                 error "${request_type^} requests took too " \
4893                                       "long to complete"
4894                 done
4895
4896                 count="$(get_hsm_param ${request_type}_count)"
4897                 [ "$count" -eq 0 ] ||
4898                         error "Expected 0 (!= '$count') " \
4899                               "active $request_type requests"
4900         done
4901 }
4902 run_test 254b "Request counters are correctly incremented and decremented"
4903
4904 test_300() {
4905         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4906
4907         # the only way to test ondisk conf is to restart MDS ...
4908         echo "Stop coordinator and remove coordinator state at mount"
4909         # stop coordinator
4910         cdt_shutdown
4911         # clean on disk conf set by default
4912         cdt_clear_mount_state
4913         cdt_check_state stopped
4914
4915         # check cdt still off after umount/remount
4916         fail $SINGLEMDS
4917         cdt_check_state stopped
4918
4919         echo "Set coordinator start at mount, and start coordinator"
4920         cdt_set_mount_state enabled
4921
4922         # check cdt is on
4923         cdt_check_state enabled
4924
4925         # check cdt still on after umount/remount
4926         fail $SINGLEMDS
4927         cdt_check_state enabled
4928
4929         # we are back to original state (cdt started at mount)
4930 }
4931 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4932
4933 test_301() {
4934         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4935
4936         local ai=$(get_hsm_param default_archive_id)
4937         local new=$((ai + 1))
4938
4939         set_hsm_param default_archive_id $new -P
4940         fail $SINGLEMDS
4941         local res=$(get_hsm_param default_archive_id)
4942
4943         # clear value
4944         set_hsm_param default_archive_id "" "-P -d"
4945
4946         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4947 }
4948 run_test 301 "HSM tunnable are persistent"
4949
4950 test_302() {
4951         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4952
4953         local ai=$(get_hsm_param default_archive_id)
4954         local new=$((ai + 1))
4955
4956         # stop coordinator
4957         cdt_shutdown
4958
4959         set_hsm_param default_archive_id $new -P
4960
4961         local mdtno
4962         for mdtno in $(seq 1 $MDSCOUNT); do
4963                 fail mds${mdtno}
4964         done
4965
4966         # check cdt is on
4967         cdt_check_state enabled
4968
4969         local res=$(get_hsm_param default_archive_id)
4970
4971         # clear value
4972         set_hsm_param default_archive_id "" "-P -d"
4973
4974         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4975 }
4976 run_test 302 "HSM tunnable are persistent when CDT is off"
4977
4978 test_400() {
4979         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4980
4981         copytool setup
4982
4983         mkdir -p $DIR/$tdir
4984
4985         local dir_mdt0=$DIR/$tdir/mdt0
4986         local dir_mdt1=$DIR/$tdir/mdt1
4987
4988         # create 1 dir per MDT
4989         stack_trap "rm -rf $dir_mdt0"
4990         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4991         stack_trap "rm -rf $dir_mdt1"
4992         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4993
4994         # create 1 file in each MDT
4995         local fid1=$(create_small_file $dir_mdt0/$tfile)
4996         local fid2=$(create_small_file $dir_mdt1/$tfile)
4997
4998         # check that hsm request on mdt0 is sent to the right MDS
4999         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5000         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5001                 echo "archive successful on mdt0"
5002
5003         # check that hsm request on mdt1 is sent to the right MDS
5004         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
5005         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5006                 echo "archive successful on mdt1"
5007 }
5008 run_test 400 "Single request is sent to the right MDT"
5009
5010 test_401() {
5011         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5012
5013         copytool setup
5014
5015         mkdir -p $DIR/$tdir
5016
5017         local dir_mdt0=$DIR/$tdir/mdt0
5018         local dir_mdt1=$DIR/$tdir/mdt1
5019
5020         # create 1 dir per MDT
5021         stack_trap "rm -rf $dir_mdt0" EXIT
5022         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5023         stack_trap "rm -rf $dir_mdt1" EXIT
5024         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5025
5026         # create 1 file in each MDT
5027         local fid1=$(create_small_file $dir_mdt0/$tfile)
5028         local fid2=$(create_small_file $dir_mdt1/$tfile)
5029
5030         # check that compound requests are shunt to the rights MDTs
5031         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
5032                 error "lfs hsm_archive"
5033         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5034                 echo "archive successful on mdt0"
5035         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5036                 echo "archive successful on mdt1"
5037 }
5038 run_test 401 "Compound requests split and sent to their respective MDTs"
5039
5040 mdc_change_state() # facet, MDT_pattern, activate|deactivate
5041 {
5042         local facet=$1
5043         local pattern="$2"
5044         local state=$3
5045         local node=$(facet_active_host $facet)
5046         local mdc
5047         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
5048                         awk '{print $4}'); do
5049                 echo "$3 $mdc on $node"
5050                 do_facet $facet "$LCTL --device $mdc $state" || return 1
5051         done
5052 }
5053
5054 test_402a() {
5055         # deactivate all mdc on agent1
5056         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
5057
5058         copytool setup --no-fail
5059
5060         check_agent_unregistered "uuid" # match any agent
5061
5062         # no expected running copytool
5063         search_copytools $agent && error "Copytool start should have failed"
5064
5065         # reactivate MDCs
5066         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
5067 }
5068 run_test 402a "Copytool start fails if all MDTs are inactive"
5069
5070 test_402b() {
5071         copytool setup
5072
5073         mkdir -p $DIR/$tdir
5074
5075         local f=$DIR/$tdir/$tfile
5076         touch $f || error "touch $f failed"
5077         local fid=$(path2fid $f)
5078
5079 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
5080         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
5081         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5082
5083         # give time for CDT to send request and to keep it for retry
5084         wait_for_loop_period
5085
5086         wait_request_state $fid ARCHIVE WAITING
5087
5088         do_facet $SINGLEAGT lctl set_param fail_loc=0
5089
5090         # request should succeed now
5091         wait_request_state $fid ARCHIVE SUCCEED
5092 }
5093 run_test 402b "CDT must retry request upon slow start of CT"
5094
5095 test_403() {
5096         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5097
5098         local agent=$(facet_active_host $SINGLEAGT)
5099
5100         # deactivate all mdc for MDT0001
5101         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5102
5103         copytool setup
5104         local uuid=$(get_agent_uuid $agent)
5105         # check the agent is registered on MDT0000, and not on MDT0001
5106         check_agent_registered_by_mdt $uuid 0
5107         check_agent_unregistered_by_mdt $uuid 1
5108
5109         # check running copytool process
5110         search_copytools $agent || error "No running copytools on $agent"
5111
5112         # reactivate all mdc for MDT0001
5113         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5114
5115         # make sure the copytool is now registered to all MDTs
5116         check_agent_registered $uuid
5117 }
5118 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
5119
5120 test_404() {
5121         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5122
5123         copytool setup
5124
5125         # create files on both MDT0000 and MDT0001
5126         mkdir -p $DIR/$tdir
5127
5128         local dir_mdt0=$DIR/$tdir/mdt0
5129         stack_trap "rm -rf $dir_mdt0" EXIT
5130         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5131
5132         # create 1 file on mdt0
5133         local fid1=$(create_small_file $dir_mdt0/$tfile)
5134
5135         # deactivate all mdc for MDT0001
5136         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5137
5138         # send an HSM request for files in MDT0000
5139         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5140
5141         # check for completion of files in MDT0000
5142         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5143                 echo "archive successful on mdt0"
5144
5145         # reactivate all mdc for MDT0001
5146         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5147 }
5148 run_test 404 "Inactive MDT does not block requests for active MDTs"
5149
5150 test_405() {
5151         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5152
5153         copytool setup
5154
5155         mkdir -p $DIR/$tdir
5156
5157         local striped_dir=$DIR/$tdir/striped_dir
5158
5159         # create striped dir on all of MDTs
5160         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
5161
5162         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
5163         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
5164         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
5165         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
5166
5167         local idx1=$($LFS getstripe -m $striped_dir/${tfile}_0)
5168         local idx2=$($LFS getstripe -m $striped_dir/${tfile}_1)
5169         local idx3=$($LFS getstripe -m $striped_dir/${tfile}_2)
5170         local idx4=$($LFS getstripe -m $striped_dir/${tfile}_3)
5171
5172         # check that compound requests are shunt to the rights MDTs
5173         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5174                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5175                 error "lfs hsm_archive"
5176
5177         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5178                 echo "archive successful on $fid1"
5179         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5180                 echo "archive successful on $fid2"
5181         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5182                 echo "archive successful on $fid3"
5183         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5184                 echo "archive successful on $fid4"
5185
5186         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5187         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5188         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5189         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5190
5191         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5192         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5193         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5194         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5195 }
5196 run_test 405 "archive and release under striped directory"
5197
5198 test_406() {
5199         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5200
5201         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
5202                 skip "need MDS version at least 2.7.64" && return 0
5203
5204         local fid
5205         local mdt_index
5206
5207         fid=$(create_small_file $DIR/$tdir/$tfile)
5208         echo "old fid $fid"
5209
5210         copytool setup
5211
5212         $LFS hsm_archive $DIR/$tdir/$tfile
5213         wait_request_state "$fid" ARCHIVE SUCCEED
5214         $LFS hsm_release $DIR/$tdir/$tfile
5215
5216         # Should migrate $tdir but not $tfile.
5217         $LFS migrate -m1 $DIR/$tdir &&
5218                 error "migrating HSM an archived file should fail"
5219
5220         $LFS hsm_restore $DIR/$tdir/$tfile
5221         wait_request_state "$fid" RESTORE SUCCEED
5222
5223         $LFS hsm_remove $DIR/$tdir/$tfile
5224         wait_request_state "$fid" REMOVE SUCCEED
5225
5226         cat $DIR/$tdir/$tfile > /dev/null ||
5227                 error "cannot read $DIR/$tdir/$tfile"
5228
5229         $LFS migrate -m1 $DIR/$tdir ||
5230                 error "cannot complete migration after HSM remove"
5231
5232         mdt_index=$($LFS getstripe -m $DIR/$tdir)
5233         if ((mdt_index != 1)); then
5234                 error "expected MDT index 1, got $mdt_index"
5235         fi
5236
5237         # Refresh fid after migration.
5238         fid=$(path2fid $DIR/$tdir/$tfile)
5239         echo "new fid $fid"
5240
5241         $LFS hsm_archive $DIR/$tdir/$tfile
5242         wait_request_state "$fid" ARCHIVE SUCCEED 1
5243
5244         lctl set_param debug=+trace
5245         $LFS hsm_release $DIR/$tdir/$tfile ||
5246                 error "cannot release $DIR/$tdir/$tfile"
5247
5248         $LFS hsm_restore $DIR/$tdir/$tfile
5249         wait_request_state "$fid" RESTORE SUCCEED 1
5250
5251         cat $DIR/$tdir/$tfile > /dev/null ||
5252                 error "cannot read $DIR/$tdir/$tfile"
5253 }
5254 run_test 406 "attempting to migrate HSM archived files is safe"
5255
5256 test_407() {
5257         needclients 2 || return 0
5258         # test needs a running copytool
5259         copytool setup
5260
5261         mkdir -p $DIR/$tdir
5262
5263         local f=$DIR/$tdir/$tfile
5264         local f2=$DIR2/$tdir/$tfile
5265         local fid
5266         fid=$(make_custom_file_for_progress $f 39 1000000)
5267         [ $? != 0 ] && skip "not enough free space" && return
5268
5269         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5270         wait_request_state $fid ARCHIVE SUCCEED
5271         $LFS hsm_release $f
5272
5273 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5274         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5275
5276         md5sum $f &
5277         # 1st request holds layout lock while appropriate
5278         # RESTORE record is still not added to llog
5279         md5sum $f2 &
5280         sleep 2
5281
5282         # after umount hsm_actions->O/x/x log shouldn't have
5283         # double RESTORE records like below
5284         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5285         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5286         sleep 30 &&
5287                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5288         fail $SINGLEMDS
5289
5290         wait_request_state $fid RESTORE SUCCEED
5291 }
5292 run_test 407 "Check for double RESTORE records in llog"
5293
5294 test_500()
5295 {
5296         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
5297                 skip "HSM migrate is not supported" && return
5298
5299         test_mkdir -p $DIR/$tdir
5300         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
5301 }
5302 run_test 500 "various LLAPI HSM tests"
5303
5304 copytool_cleanup
5305
5306 complete $SECONDS
5307 check_and_cleanup_lustre
5308 exit_status