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