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