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