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