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