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