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