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