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