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