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