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