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