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