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