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