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