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