Whamcloud - gitweb
LU-14119 lfsck: check linkea if it's newly added
[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         (( $MDS1_VERSION >= $(version_code 2.12.3) )) ||
3941                 skip "Need MDS version at least 2.12.3"
3942
3943         local file1=$DIR/$tdir/$tfile
3944         local file2=$DIR2/$tdir/$tfile
3945
3946         local fid=$(create_small_sync_file $file1)
3947
3948         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
3949         zconf_mount "$(facet_host $SINGLEAGT)" "$MOUNT3" ||
3950                 error "cannot mount '$MOUNT3' on '$SINGLEAGT'"
3951
3952         copytool setup -m  "$MOUNT3"
3953
3954         do_nodes $(comma_list $(nodes_list)) $LCTL clear
3955
3956         $LFS hsm_archive $file1 || error "Fail to archive $file1"
3957         wait_request_state $fid ARCHIVE SUCCEED
3958
3959         $LFS hsm_release $file1
3960         echo "Verifying released state: "
3961         check_hsm_flags $file1 "0x0000000d"
3962
3963         multiop_bg_pause $file1 oO_WRONLY:O_APPEND:_w4c || error "multiop failed"
3964         MULTIPID=$!
3965         stat $file2 &
3966         kill -USR1 $MULTIPID
3967
3968         wait
3969         sync
3970
3971         local size1=$(stat -c "%s" $file1)
3972         local size2=$(stat -c "%s" $file2)
3973
3974         [ $size1 -eq $size2 ] || error "sizes are different $size1 $size2"
3975 }
3976 run_test 113 "wrong stat after restore"
3977
3978 test_200() {
3979         local f=$DIR/$tdir/$tfile
3980         local fid=$(create_empty_file "$f")
3981
3982         copytool setup
3983
3984         # Prevent archive from completing
3985         copytool_suspend
3986
3987         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3988         # wait archive to register at CDT
3989         wait_request_state $fid ARCHIVE STARTED
3990
3991         # Cancel the archive
3992         $LFS hsm_cancel "$f"
3993
3994         wait_request_state $fid ARCHIVE CANCELED
3995
3996         copytool_continue
3997         wait_request_state $fid CANCEL SUCCEED
3998 }
3999 run_test 200 "Register/Cancel archive"
4000
4001 test_201() {
4002         # test needs a running copytool
4003         copytool setup
4004
4005         local f=$DIR/$tdir/$tfile
4006         create_archive_file $tdir/$tfile
4007         copytool import $tdir/$tfile $f
4008         local fid=$(path2fid $f)
4009
4010         # test with cdt on is made in test_222
4011         cdt_disable
4012         $LFS hsm_restore $f
4013         # wait restore to register at CDT
4014         wait_request_state $fid RESTORE WAITING
4015         $LFS hsm_cancel $f
4016         cdt_enable
4017         wait_request_state $fid RESTORE CANCELED
4018         wait_request_state $fid CANCEL SUCCEED
4019 }
4020 run_test 201 "Register/Cancel restore"
4021
4022 test_202() {
4023         local f=$DIR/$tdir/$tfile
4024         local fid=$(create_empty_file "$f")
4025
4026         # test needs a running copytool
4027         copytool setup
4028
4029         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4030         wait_request_state $fid ARCHIVE SUCCEED
4031
4032         copytool_suspend
4033         $LFS hsm_remove $f
4034         # wait remove to register at CDT
4035         wait_request_state $fid REMOVE STARTED
4036         $LFS hsm_cancel $f
4037
4038         wait_request_state $fid REMOVE CANCELED
4039 }
4040 run_test 202 "Register/Cancel remove"
4041
4042 test_220A() { # was test_220
4043         # test needs a running copytool
4044         copytool setup
4045
4046         mkdir -p $DIR/$tdir
4047
4048         local f=$DIR/$tdir/$tfile
4049         local fid=$(copy_file /etc/passwd $f)
4050
4051         changelog_register
4052
4053         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4054         wait_request_state $fid ARCHIVE SUCCEED
4055
4056         changelog_find -type HSM -target-fid $fid -flags 0x0 ||
4057                 error "The expected changelog was not emitted"
4058 }
4059 run_test 220A "Changelog for archive"
4060
4061 test_220a() {
4062         # test needs a running copytool
4063         copytool setup
4064
4065         mkdir -p $DIR/$tdir
4066
4067         local f=$DIR/$tdir/$tfile
4068         local fid=$(copy_file /etc/passwd $f)
4069
4070         changelog_register
4071
4072         # block copytool operations to allow for HSM request to be
4073         # submitted and file be unlinked (CDT will find object removed)
4074         copytool_suspend
4075
4076         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4077
4078         # wait request to reach CT
4079         wait_request_state $fid ARCHIVE STARTED
4080
4081         rm -f $f
4082
4083         copytool_continue
4084
4085         wait_request_state $fid ARCHIVE FAILED
4086
4087         # HE_ARCHIVE|ENOENT
4088         changelog_find -type HSM -target-fid $fid -flags 0x2 ||
4089                 error "The expected changelog was not emitted"
4090 }
4091 run_test 220a "Changelog for failed archive"
4092
4093 test_221() {
4094         local f=$DIR/$tdir/$tfile
4095         local fid=$(create_empty_file "$f")
4096
4097         copytool setup -b 1
4098         changelog_register
4099
4100         # Prevent archive from completing
4101         copytool_suspend
4102         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4103         wait_request_state $fid ARCHIVE STARTED
4104
4105         $LFS hsm_cancel $f
4106         wait_request_state $fid ARCHIVE CANCELED
4107
4108         copytool_continue
4109         wait_request_state $fid CANCEL SUCCEED
4110
4111         changelog_find -type HSM -target-fid $fid -flags 0x7d ||
4112                 error "The expected changelog was not emitted"
4113 }
4114 run_test 221 "Changelog for archive canceled"
4115
4116 test_222a() {
4117         # test needs a running copytool
4118         copytool setup
4119
4120         mkdir -p $DIR/$tdir
4121         copy2archive /etc/passwd $tdir/$tfile
4122
4123         local f=$DIR/$tdir/$tfile
4124         copytool import $tdir/$tfile $f
4125         local fid=$(path2fid $f)
4126
4127         changelog_register
4128
4129         $LFS hsm_restore $f
4130         wait_request_state $fid RESTORE SUCCEED
4131
4132         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4133                 error "The expected changelog was not emitted"
4134 }
4135 run_test 222a "Changelog for explicit restore"
4136
4137 test_222b() {
4138         # test needs a running copytool
4139         copytool setup
4140
4141         mkdir -p $DIR/$tdir
4142         local f=$DIR/$tdir/$tfile
4143         local fid=$(copy_file /etc/passwd $f)
4144
4145         changelog_register
4146         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4147         wait_request_state $fid ARCHIVE SUCCEED
4148         $LFS hsm_release $f
4149
4150         md5sum $f
4151
4152         wait_request_state $fid RESTORE SUCCEED
4153
4154         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4155                 error "The expected changelog was not emitted"
4156 }
4157 run_test 222b "Changelog for implicit restore"
4158
4159 test_222c() {
4160         # test needs a running copytool
4161         copytool setup
4162
4163         mkdir -p $DIR/$tdir
4164         copy2archive /etc/passwd $tdir/$tfile
4165
4166         local f=$DIR/$tdir/$tfile
4167         copytool import $tdir/$tfile $f
4168         local fid=$(path2fid $f)
4169
4170         changelog_register
4171
4172         # block copytool operations to allow for HSM request to be
4173         # submitted and file be unlinked (CDT will find object removed)
4174         copytool_suspend
4175
4176         $LFS hsm_restore $f
4177
4178         # wait request to reach CT
4179         wait_request_state $fid RESTORE STARTED
4180
4181         rm -f $f
4182
4183         copytool_continue
4184
4185         wait_request_state $fid RESTORE FAILED
4186
4187         # HE_RESTORE|ENOENT
4188         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
4189                 error "The expected changelog was not emitted"
4190 }
4191 run_test 222c "Changelog for failed explicit restore"
4192
4193 test_222d() {
4194         # test needs a running copytool
4195         copytool setup
4196
4197         mkdir -p $DIR/$tdir
4198         local f=$DIR/$tdir/$tfile
4199         local fid=$(copy_file /etc/passwd $f)
4200
4201         changelog_register
4202         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4203         wait_request_state $fid ARCHIVE SUCCEED
4204         $LFS hsm_release $f
4205
4206         copytool_remove_backend $fid
4207         md5sum $f
4208
4209         wait_request_state $fid RESTORE FAILED
4210
4211         # HE_RESTORE|ENOENT
4212         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
4213                 error "The expected changelog was not emitted"
4214 }
4215 run_test 222d "Changelog for failed implicit restore"
4216
4217 test_223a() {
4218         # test needs a running copytool
4219         copytool setup -b 1
4220
4221         local f=$DIR/$tdir/$tfile
4222         create_archive_file $tdir/$tfile
4223
4224         changelog_register
4225
4226         copytool import $tdir/$tfile $f
4227         local fid=$(path2fid $f)
4228
4229         $LFS hsm_restore $f
4230         wait_request_state $fid RESTORE STARTED
4231         $LFS hsm_cancel $f
4232         wait_request_state $fid RESTORE CANCELED
4233         wait_request_state $fid CANCEL SUCCEED
4234
4235         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4236                 error "The expected changelog was not emitted"
4237 }
4238 run_test 223a "Changelog for restore canceled (import case)"
4239
4240 test_223b() {
4241         local f=$DIR/$tdir/$tfile
4242         local fid=$(create_empty_file "$f")
4243
4244         copytool setup -b 1
4245         changelog_register
4246
4247         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4248         wait_request_state $fid ARCHIVE SUCCEED
4249         $LFS hsm_release $f
4250
4251         # Prevent restore from completing
4252         copytool_suspend
4253         $LFS hsm_restore $f
4254         wait_request_state $fid RESTORE STARTED
4255
4256         $LFS hsm_cancel $f
4257         wait_request_state $fid RESTORE CANCELED
4258
4259         copytool_continue
4260         wait_request_state $fid CANCEL SUCCEED
4261
4262         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4263                 error "The expected changelog was not emitted"
4264 }
4265 run_test 223b "Changelog for restore canceled (release case)"
4266
4267 test_224A() { # was test_224
4268         # test needs a running copytool
4269         copytool setup
4270
4271         mkdir -p $DIR/$tdir
4272
4273         local f=$DIR/$tdir/$tfile
4274         local fid=$(copy_file /etc/passwd $f)
4275
4276         changelog_register
4277         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4278         wait_request_state $fid ARCHIVE SUCCEED
4279
4280         $LFS hsm_remove $f
4281         wait_request_state $fid REMOVE SUCCEED
4282
4283         changelog_find -type HSM -target-fid $fid -flags 0x200 ||
4284                 error "The expected changelog was not emitted"
4285 }
4286 run_test 224A "Changelog for remove"
4287
4288 test_224a() {
4289         # test needs a running copytool
4290         copytool setup
4291
4292         mkdir -p $DIR/$tdir
4293
4294         local f=$DIR/$tdir/$tfile
4295         local fid=$(copy_file /etc/passwd $f)
4296
4297         changelog_register
4298         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4299         wait_request_state $fid ARCHIVE SUCCEED
4300
4301         copytool_remove_backend $fid
4302
4303         # block copytool operations to allow for HSM request to be
4304         # submitted and file be unlinked (CDT will find object removed)
4305         copytool_suspend
4306
4307         $LFS hsm_remove $f
4308
4309         # wait for request to reach CT
4310         wait_request_state $fid REMOVE STARTED
4311
4312         rm -f $f
4313
4314         copytool_continue
4315
4316         wait_request_state $fid REMOVE FAILED
4317
4318         # HE_REMOVE|ENOENT=0x202
4319         changelog_find -type HSM -target-fid $fid -flags 0x202 ||
4320                 error "The expected changelog was not emitted"
4321 }
4322 run_test 224a "Changelog for failed remove"
4323
4324 test_225() {
4325         # test is not usable because remove request is too fast
4326         # so it is always finished before cancel can be done ...
4327         echo "Test disabled"
4328         return 0
4329
4330         # test needs a running copytool
4331         copytool setup
4332
4333         local f=$DIR/$tdir/$tfile
4334         local fid=$(create_empty_file "$f")
4335
4336         changelog_register
4337         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4338         wait_request_state $fid ARCHIVE SUCCEED
4339
4340         # Prevent restore from completing
4341         copytool_suspend
4342         $LFS hsm_remove $f
4343
4344         $LFS hsm_cancel $f
4345         wait_request_state $fid REMOVE CANCELED
4346
4347         copytool_continue
4348         wait_request_state $fid CANCEL SUCCEED
4349
4350         changelog_find -type HSM -target-fid $fid -flags 0x27d
4351                 error "The expected changelog was not emitted"
4352 }
4353 run_test 225 "Changelog for remove canceled"
4354
4355 test_226() {
4356         # test needs a running copytool
4357         copytool setup
4358
4359         mkdir -p $DIR/$tdir
4360
4361         local f1=$DIR/$tdir/$tfile-1
4362         local f2=$DIR/$tdir/$tfile-2
4363         local f3=$DIR/$tdir/$tfile-3
4364         local fid1=$(copy_file /etc/passwd $f1)
4365         local fid2=$(copy_file /etc/passwd $f2)
4366         copy_file /etc/passwd $f3
4367
4368         changelog_register
4369         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4370         wait_request_state $fid1 ARCHIVE SUCCEED
4371
4372         $LFS hsm_archive $f2
4373         wait_request_state $fid2 ARCHIVE SUCCEED
4374
4375         rm $f1 || error "rm $f1 failed"
4376
4377         changelog_dump
4378         changelog_find -type UNLNK -target-fid $fid1 -flags 0x3 ||
4379                 error "The expected changelog was not emitted"
4380
4381         mv $f3 $f2 || error "mv $f3 $f2 failed"
4382
4383         changelog_find -type RENME -target-fid $fid2 -flags 0x3 ||
4384                 error "The expected changelog was not emitted"
4385 }
4386 run_test 226 "changelog for last rm/mv with exiting archive"
4387
4388 # This is just a utility function to clarify what test_227 does
4389 __test_227()
4390 {
4391         local target=0x280
4392
4393         "$LFS" "$action" --$flag "$file" ||
4394                 error "Cannot ${action#hsm_} $flag on '$file'"
4395
4396         # Only one changelog should be produced
4397         local entries="$(changelog_find -type HSM -target-fid $fid)"
4398         [ $(wc -l <<< "$entries") -eq $((++count)) ] ||
4399                 error "lfs $action --$flag '$file' produced more than one" \
4400                       "changelog record"
4401
4402         # Parse the last changelog record
4403         local entry="$(tail -n 1 <<< "$entries")"
4404         eval local -A changelog=$(changelog2array $entry)
4405
4406         # Also check the flags match what is expected
4407         [[ ${changelog[flags]} == $target ]] ||
4408                 error "Changelog flag is '${changelog[flags]}', not $target"
4409 }
4410
4411 test_227() {
4412         local file="$DIR/$tdir/$tfile"
4413         local fid=$(create_empty_file "$file")
4414         local count=0
4415
4416         changelog_register
4417
4418         for flag in norelease noarchive exists archived lost; do
4419                 if [ "$flag" == lost ]; then
4420                         # The flag "lost" only works on an archived file
4421                         "$LFS" hsm_set --archived "$file"
4422                         ((count++))
4423                 fi
4424
4425                 action="hsm_set" __test_227
4426                 action="hsm_clear" __test_227
4427         done
4428 }
4429 run_test 227 "changelog when explicit setting of HSM flags"
4430
4431 test_228() {
4432         # test needs a running copytool
4433         copytool setup
4434
4435         local fid=$(create_small_sync_file $DIR/$tfile)
4436         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4437         wait_request_state $fid ARCHIVE SUCCEED
4438
4439         $LFS hsm_release $DIR/$tfile
4440         check_hsm_flags $DIR/$tfile "0x0000000d"
4441
4442         filefrag $DIR/$tfile | grep " 1 extent found" ||
4443                 error "filefrag on released file must return only one extent"
4444
4445         # only newer versions of cp detect sparse files by stat/FIEMAP
4446         # (LU-2580)
4447         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4448                 error "copying $DIR/$tfile"
4449         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4450
4451         $LFS hsm_release $DIR/$tfile
4452         check_hsm_flags $DIR/$tfile "0x0000000d"
4453
4454         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4455
4456         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4457                 error "tar failed"
4458         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4459                 error "comparing untarred $DIR/$tfile"
4460
4461         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4462                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4463 }
4464 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4465
4466 test_250() {
4467         local file="$DIR/$tdir/$tfile"
4468
4469         # set max_requests to allow one request of each type to be started (3)
4470         stack_trap \
4471                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4472         set_hsm_param max_requests 3
4473         # speed up test
4474         stack_trap \
4475                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4476         set_hsm_param loop_period 1
4477
4478         # send 1 requests of each kind twice
4479         copytool setup
4480         # setup the files
4481         for action in archive restore remove; do
4482                 local filepath="$file"-to-$action
4483                 local fid=$(create_empty_file "$filepath")
4484                 local fid2=$(create_empty_file "$filepath".bis)
4485
4486                 if [ "$action" != archive ]; then
4487                         "$LFS" hsm_archive "$filepath"
4488                         wait_request_state $fid ARCHIVE SUCCEED
4489                         "$LFS" hsm_archive "$filepath".bis
4490                         wait_request_state $fid2 ARCHIVE SUCCEED
4491                 fi
4492                 if [ "$action" == restore ]; then
4493                         "$LFS" hsm_release "$filepath"
4494                         "$LFS" hsm_release "$filepath".bis
4495                 fi
4496         done
4497
4498         # suspend the copytool to prevent requests from completing
4499         stack_trap "copytool_continue" EXIT
4500         copytool_suspend
4501
4502         # send `max_requests' requests (one of each kind)
4503         for action in archive restore remove; do
4504                 filepath="$file"-to-$action
4505                 "$LFS" hsm_${action} "$filepath"
4506                 wait_request_state $(path2fid "$filepath") "${action^^}" STARTED
4507         done
4508
4509         # send another batch of requests
4510         for action in archive restore remove; do
4511                 "$LFS" hsm_${action} "$file-to-$action".bis
4512         done
4513         # wait for `loop_period' seconds to make sure the coordinator has time
4514         # to register those, even though it should not
4515         sleep 1
4516
4517         # only the first batch of request should be started
4518         local -i count
4519         count=$(do_facet $SINGLEMDS "$LCTL" get_param -n $HSM_PARAM.actions |
4520                 grep -c STARTED)
4521
4522         ((count == 3)) ||
4523                 error "expected 3 STARTED requests, found $count"
4524 }
4525 run_test 250 "Coordinator max request"
4526
4527 test_251() {
4528         local f=$DIR/$tdir/$tfile
4529         local fid=$(create_empty_file "$f")
4530
4531         cdt_disable
4532         # to have a short test
4533         local old_to=$(get_hsm_param active_request_timeout)
4534         set_hsm_param active_request_timeout 1
4535         # to be sure the cdt will wake up frequently so
4536         # it will be able to cancel the "old" request
4537         local old_loop=$(get_hsm_param loop_period)
4538         set_hsm_param loop_period 1
4539         cdt_enable
4540
4541         copytool setup
4542
4543         # Prevent archive from completing
4544         copytool_suspend
4545         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4546         wait_request_state $fid ARCHIVE STARTED
4547
4548         # Let the request timeout
4549         wait_request_state $fid ARCHIVE CANCELED
4550
4551         set_hsm_param active_request_timeout $old_to
4552         set_hsm_param loop_period $old_loop
4553 }
4554 run_test 251 "Coordinator request timeout"
4555
4556 test_252() {
4557         local f=$DIR/$tdir/$tfile
4558         local fid=$(create_empty_file "$f")
4559
4560         # to have a short test
4561         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4562         set_hsm_param loop_period 1
4563
4564         copytool setup
4565
4566         # Prevent archive from completing
4567         copytool_suspend
4568         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4569         wait_request_state $fid ARCHIVE STARTED
4570         rm -f "$f"
4571
4572         stack_trap "set_hsm_param active_request_timeout \
4573                     $(get_hsm_param active_request_timeout)" EXIT
4574         set_hsm_param active_request_timeout 1
4575
4576         wait_request_state $fid ARCHIVE CANCELED
4577         copytool_continue
4578 }
4579 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4580
4581 test_253() {
4582         local rc
4583         # test needs a running copytool
4584         copytool setup
4585
4586         mkdir -p $DIR/$tdir
4587         local f=$DIR/$tdir/$tfile
4588
4589         dd if=/dev/zero of=$f bs=1MB count=10
4590         local fid=$(path2fid $f)
4591
4592         $LFS hsm_archive $f || error "could not archive file"
4593         wait_request_state $fid ARCHIVE SUCCEED
4594
4595         # clear locks to discard inode data
4596         cancel_lru_locks osc
4597
4598         #define OBD_FAIL_MDC_MERGE              0x807
4599         $LCTL set_param fail_loc=0x807
4600
4601         #expect error here, instead of release with wrong size
4602         $LFS hsm_release $f
4603         rc=$?
4604         if ((rc == 0)); then
4605                 file_size=$(stat -c '%s' $f)
4606                 if ((file_size != 10485760)); then
4607                         error "Wrong file size after hsm_release"
4608                 fi
4609         else
4610                 echo "could not release file"
4611         fi
4612 }
4613 run_test 253 "Check for wrong file size after release"
4614
4615 test_254a()
4616 {
4617         [ $MDS_VERSION_CODE -lt $(version_code 2.10.56) ] &&
4618                 skip "need MDS version at least 2.10.56"
4619
4620         # Check that the counters are initialized to 0
4621         local count
4622         for request_type in archive restore remove; do
4623                 count="$(get_hsm_param ${request_type}_count)" ||
4624                         error "Reading ${request_type}_count failed with $?"
4625
4626                 [ "$count" -eq 0 ] ||
4627                         error "Expected ${request_type}_count to be " \
4628                               "0 != '$count'"
4629         done
4630 }
4631 run_test 254a "Request counters are initialized to zero"
4632
4633 test_254b()
4634 {
4635         [ $MDS_VERSION_CODE -lt $(version_code 2.10.56) ] &&
4636                 skip "need MDS version at least 2.10.56"
4637
4638         # The number of request to launch (at least 32)
4639         local request_count=$((RANDOM % 32 + 32))
4640         printf "Will launch %i requests of each type\n" "$request_count"
4641
4642         # Launch a copytool to process requests
4643         copytool setup
4644
4645         # Set hsm.max_requests to allow starting all requests at the same time
4646         stack_trap \
4647                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4648         set_hsm_param max_requests "$request_count"
4649
4650         local timeout
4651         local count
4652         for request_type in archive restore remove; do
4653                 printf "Checking %s requests\n" "${request_type}"
4654                 # Suspend the copytool to give us time to read the proc files
4655                 copytool_suspend
4656
4657                 for ((i = 0; i < $request_count; i++)); do
4658                         case $request_type in
4659                         archive)
4660                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4661                                         >/dev/null 2>&1
4662                                 ;;
4663                         restore)
4664                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4665                                 ;;
4666                         esac
4667                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4668                 done
4669
4670                 # Give the coordinator 10 seconds to start every request
4671                 timeout=10
4672                 while get_hsm_param actions | grep -q WAITING; do
4673                         sleep 1
4674                         let timeout-=1
4675                         [ $timeout -gt 0 ] ||
4676                                 error "${request_type^} requests took too " \
4677                                       "long to start"
4678                 done
4679
4680                 count="$(get_hsm_param ${request_type}_count)"
4681                 [ "$count" -eq "$request_count" ] ||
4682                         error "Expected '$request_count' (!= '$count') " \
4683                               "active $request_type requests"
4684
4685                 # Let the copytool process the requests
4686                 copytool_continue
4687                 # Give it 10 seconds maximum
4688                 timeout=10
4689                 while get_hsm_param actions | grep -q STARTED; do
4690                         sleep 1
4691                         let timeout-=1
4692                         [ $timeout -gt 0 ] ||
4693                                 error "${request_type^} requests took too " \
4694                                       "long to complete"
4695                 done
4696
4697                 count="$(get_hsm_param ${request_type}_count)"
4698                 [ "$count" -eq 0 ] ||
4699                         error "Expected 0 (!= '$count') " \
4700                               "active $request_type requests"
4701         done
4702 }
4703 run_test 254b "Request counters are correctly incremented and decremented"
4704
4705 test_255()
4706 {
4707         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4708                 skip "Need MDS version at least 2.12.0"
4709
4710         local file="$DIR/$tdir/$tfile"
4711         local fid=$(create_empty_file "$file")
4712
4713         # How do you make sure the coordinator has consumed any outstanding
4714         # event, without triggering an event yourself?
4715         #
4716         # You wait for a request to disappear from the coordinator's llog.
4717
4718         # Warning: the setup represents 90% of this test
4719
4720         # Create and process an HSM request
4721         copytool setup
4722         "$LFS" hsm_archive "$file"
4723         wait_request_state $fid ARCHIVE SUCCEED
4724
4725         kill_copytools
4726         wait_copytools || error "failed to stop copytools"
4727
4728         # Launch a new HSM request
4729         rm "$file"
4730         create_empty_file "$file"
4731         "$LFS" hsm_archive "$file"
4732
4733         cdt_shutdown
4734
4735         # Have the completed request be removed as soon as the cdt wakes up
4736         stack_trap "set_hsm_param grace_delay $(get_hsm_param grace_delay)" EXIT
4737         set_hsm_param grace_delay 1
4738         # (Hopefully, time on the MDS will behave nicely)
4739         do_facet $SINGLEMDS sleep 2 &
4740
4741         # Increase `loop_period' as a mean to prevent the coordinator from
4742         # waking itself up to do some housekeeping.
4743         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4744         set_hsm_param loop_period 1000
4745
4746         wait $! || error "waiting failed"
4747         cdt_enable
4748         wait_request_state $fid ARCHIVE ""
4749         # The coordinator will not wake up on its own for ~`loop_period' secs...
4750
4751         # ... Unless a copytool registers. Now the real test begins
4752         copytool setup
4753         wait_request_state $(path2fid "$file") ARCHIVE SUCCEED
4754 }
4755 run_test 255 "Copytool registration wakes the coordinator up"
4756
4757 # tests 260[a-c] rely on the parsing of the copytool's log file, they might
4758 # break in the future because of that.
4759 test_260a()
4760 {
4761         [ $MDS_VERSION_CODE -lt $(version_code 2.11.56) ] &&
4762                 skip "need MDS version 2.11.56 or later"
4763
4764         local -a files=("$DIR/$tdir/$tfile".{0..15})
4765         local file
4766
4767         for file in "${files[@]}"; do
4768                 create_small_file "$file"
4769         done
4770
4771         # Set a few hsm parameters
4772         stack_trap \
4773                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4774         set_hsm_param loop_period 1
4775         stack_trap \
4776                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4777         set_hsm_param max_requests 3
4778
4779         # Release one file
4780         copytool setup
4781         "$LFS" hsm_archive "${files[0]}"
4782         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4783         "$LFS" hsm_release "${files[0]}"
4784
4785         # Stop the copytool
4786         kill_copytools
4787         wait_copytools || error "copytools failed to stop"
4788
4789         # Send several archive requests
4790         for file in "${files[@]:1}"; do
4791                 "$LFS" hsm_archive "$file"
4792         done
4793
4794         # Send one restore request
4795         "$LFS" hsm_restore "${files[0]}"
4796
4797         # Launch a copytool
4798         copytool setup
4799
4800         # Wait for all the requests to complete
4801         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4802         for file in "${files[@]:1}"; do
4803                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4804         done
4805
4806         # Collect the actions in the order in which the copytool processed them
4807         local -a actions=(
4808                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4809                         "$(copytool_logfile "$SINGLEAGT")")
4810                 )
4811
4812         printf '%s\n' "${actions[@]}"
4813
4814         local action
4815         for action in "${actions[@]:0:3}"; do
4816                 [ "$action" == RESTORE ] && return
4817         done
4818
4819         error "Too many ARCHIVE requests were run before the RESTORE request"
4820 }
4821 run_test 260a "Restore request have priority over other requests"
4822
4823 # This test is very much tied to the implementation of the current priorisation
4824 # mechanism in the coordinator. It might not make sense to keep it in the future
4825 test_260b()
4826 {
4827         [ $MDS_VERSION_CODE -lt $(version_code 2.11.56) ] &&
4828                 skip "need MDS version 2.11.56 or later"
4829
4830         local -a files=("$DIR/$tdir/$tfile".{0..15})
4831         local file
4832
4833         for file in "${files[@]}"; do
4834                 create_small_file "$file"
4835         done
4836
4837         # Set a few hsm parameters
4838         stack_trap \
4839                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4840         set_hsm_param loop_period 1
4841         stack_trap \
4842                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4843         set_hsm_param max_requests 3
4844
4845         # Release one file
4846         copytool setup --archive-id 2
4847         "$LFS" hsm_archive --archive 2 "${files[0]}"
4848         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4849         "$LFS" hsm_release "${files[0]}"
4850
4851         # Stop the copytool
4852         kill_copytools
4853         wait_copytools || error "copytools failed to stop"
4854
4855         # Send several archive requests
4856         for file in "${files[@]:1}"; do
4857                 "$LFS" hsm_archive "$file"
4858         done
4859
4860         # Send one restore request
4861         "$LFS" hsm_restore "${files[0]}"
4862
4863         # Launch a copytool
4864         copytool setup
4865         copytool setup --archive-id 2
4866
4867         # Wait for all the requests to complete
4868         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4869         for file in "${files[@]:1}"; do
4870                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4871         done
4872
4873         # Collect the actions in the order in which the copytool processed them
4874         local -a actions=(
4875                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4876                         "$(copytool_logfile "$SINGLEAGT")")
4877                 )
4878
4879         printf '%s\n' "${actions[@]}"
4880
4881         local action
4882         for action in "${actions[@]:0:3}"; do
4883                 [ "$action" == RESTORE ] && return
4884         done
4885
4886         error "Too many ARCHIVE requests were run before the RESTORE request"
4887 }
4888 run_test 260b "Restore request have priority over other requests"
4889
4890 # This test is very much tied to the implementation of the current priorisation
4891 # mechanism in the coordinator. It might not make sense to keep it in the future
4892 test_260c()
4893 {
4894         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4895                 skip "Need MDS version at least 2.12.0"
4896
4897         local -a files=("$DIR/$tdir/$tfile".{0..15})
4898         local file
4899
4900         for file in "${files[@]}"; do
4901                 create_small_file "$file"
4902         done
4903
4904         # Set a few hsm parameters
4905         stack_trap \
4906                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4907         set_hsm_param loop_period 1000
4908         stack_trap \
4909                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4910         set_hsm_param max_requests 3
4911
4912         # Release one file
4913         copytool setup --archive-id 2
4914         "$LFS" hsm_archive --archive 2 "${files[0]}"
4915         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4916         "$LFS" hsm_release "${files[0]}"
4917
4918         # Stop the copytool
4919         kill_copytools
4920         wait_copytools || error "copytools failed to stop"
4921
4922         # Force the next coordinator run to do housekeeping
4923         cdt_shutdown
4924         cdt_enable
4925
4926         "$LFS" hsm_archive "${files[1]}"
4927
4928         # Launch a copytool
4929         copytool setup
4930         copytool setup --archive-id 2
4931
4932         wait_request_state "$(path2fid "${files[1]}")" ARCHIVE SUCCEED
4933         # The coordinator just did a housekeeping run it won't do another one
4934         # for around `loop_period' seconds => requests will not be reordered
4935         # if it costs too much (ie. when the coordinator has to discard a whole
4936         # hal)
4937
4938         # Send several archive requests
4939         for file in "${files[@]:2}"; do
4940                 "$LFS" hsm_archive "$file"
4941         done
4942
4943         # Send one restore request
4944         "$LFS" hsm_restore "${files[0]}"
4945
4946         # Wait for all the requests to complete
4947         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4948         for file in "${files[@]:2}"; do
4949                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4950         done
4951
4952         # Collect the actions in the order in which the copytool processed them
4953         local -a actions=(
4954                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4955                         "$(copytool_logfile "$SINGLEAGT")")
4956                 )
4957
4958         printf '%s\n' "${actions[@]}"
4959
4960         local action
4961         for action in "${actions[@]:0:3}"; do
4962                 [ "$action" == RESTORE ] &&
4963                         error "Restore requests should not be prioritised" \
4964                               "unless the coordinator is doing housekeeping"
4965         done
4966         return 0
4967 }
4968 run_test 260c "Requests are not reordered on the 'hot' path of the coordinator"
4969
4970 test_300() {
4971         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4972
4973         # the only way to test ondisk conf is to restart MDS ...
4974         echo "Stop coordinator and remove coordinator state at mount"
4975         # stop coordinator
4976         cdt_shutdown
4977         # clean on disk conf set by default
4978         cdt_clear_mount_state
4979         cdt_check_state stopped
4980
4981         # check cdt still off after umount/remount
4982         fail $SINGLEMDS
4983         cdt_check_state stopped
4984
4985         echo "Set coordinator start at mount, and start coordinator"
4986         cdt_set_mount_state enabled
4987
4988         # check cdt is on
4989         cdt_check_state enabled
4990
4991         # check cdt still on after umount/remount
4992         fail $SINGLEMDS
4993         cdt_check_state enabled
4994
4995         # we are back to original state (cdt started at mount)
4996 }
4997 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4998
4999 test_301() {
5000         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
5001
5002         local ai=$(get_hsm_param default_archive_id)
5003         local new=$((ai + 1))
5004
5005         set_hsm_param default_archive_id $new -P
5006         fail $SINGLEMDS
5007         local res=$(get_hsm_param default_archive_id)
5008
5009         # clear value
5010         set_hsm_param default_archive_id "" "-P -d"
5011
5012         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
5013 }
5014 run_test 301 "HSM tunnable are persistent"
5015
5016 test_302() {
5017         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
5018
5019         local ai=$(get_hsm_param default_archive_id)
5020         local new=$((ai + 1))
5021
5022         # stop coordinator
5023         cdt_shutdown
5024
5025         set_hsm_param default_archive_id $new -P
5026
5027         local mdtno
5028         for mdtno in $(seq 1 $MDSCOUNT); do
5029                 fail mds${mdtno}
5030         done
5031
5032         # check cdt is on
5033         cdt_check_state enabled
5034
5035         local res=$(get_hsm_param default_archive_id)
5036
5037         # clear value
5038         set_hsm_param default_archive_id "" "-P -d"
5039
5040         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
5041 }
5042 run_test 302 "HSM tunnable are persistent when CDT is off"
5043
5044 test_400() {
5045         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5046
5047         copytool setup
5048
5049         mkdir -p $DIR/$tdir
5050
5051         local dir_mdt0=$DIR/$tdir/mdt0
5052         local dir_mdt1=$DIR/$tdir/mdt1
5053
5054         # create 1 dir per MDT
5055         stack_trap "rm -rf $dir_mdt0" EXIT
5056         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5057         stack_trap "rm -rf $dir_mdt1" EXIT
5058         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5059
5060         # create 1 file in each MDT
5061         local fid1=$(create_small_file $dir_mdt0/$tfile)
5062         local fid2=$(create_small_file $dir_mdt1/$tfile)
5063
5064         # check that hsm request on mdt0 is sent to the right MDS
5065         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5066         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5067                 echo "archive successful on mdt0"
5068
5069         # check that hsm request on mdt1 is sent to the right MDS
5070         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
5071         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5072                 echo "archive successful on mdt1"
5073 }
5074 run_test 400 "Single request is sent to the right MDT"
5075
5076 test_401() {
5077         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5078
5079         copytool setup
5080
5081         mkdir -p $DIR/$tdir
5082
5083         local dir_mdt0=$DIR/$tdir/mdt0
5084         local dir_mdt1=$DIR/$tdir/mdt1
5085
5086         # create 1 dir per MDT
5087         stack_trap "rm -rf $dir_mdt0" EXIT
5088         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5089         stack_trap "rm -rf $dir_mdt1" EXIT
5090         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5091
5092         # create 1 file in each MDT
5093         local fid1=$(create_small_file $dir_mdt0/$tfile)
5094         local fid2=$(create_small_file $dir_mdt1/$tfile)
5095
5096         # check that compound requests are shunt to the rights MDTs
5097         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
5098                 error "lfs hsm_archive"
5099         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5100                 echo "archive successful on mdt0"
5101         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5102                 echo "archive successful on mdt1"
5103 }
5104 run_test 401 "Compound requests split and sent to their respective MDTs"
5105
5106 mdc_change_state() # facet, MDT_pattern, activate|deactivate
5107 {
5108         local facet=$1
5109         local pattern="$2"
5110         local state=$3
5111         local node=$(facet_active_host $facet)
5112         local mdc
5113         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
5114                         awk '{print $4}'); do
5115                 echo "$3 $mdc on $node"
5116                 do_facet $facet "$LCTL --device $mdc $state" || return 1
5117         done
5118 }
5119
5120 test_402a() {
5121         # deactivate all mdc on agent1
5122         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
5123
5124         copytool setup --no-fail
5125
5126         check_agent_unregistered "uuid" # match any agent
5127
5128         # no expected running copytool
5129         search_copytools $agent && error "Copytool start should have failed"
5130
5131         # reactivate MDCs
5132         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
5133 }
5134 run_test 402a "Copytool start fails if all MDTs are inactive"
5135
5136 test_402b() {
5137         copytool setup
5138
5139         mkdir -p $DIR/$tdir
5140
5141         local f=$DIR/$tdir/$tfile
5142         touch $f || error "touch $f failed"
5143         local fid=$(path2fid $f)
5144
5145 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
5146         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
5147         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5148
5149         # give time for CDT to send request and to keep it for retry
5150         wait_for_loop_period
5151
5152         wait_request_state $fid ARCHIVE WAITING
5153
5154         do_facet $SINGLEAGT lctl set_param fail_loc=0
5155
5156         # request should succeed now
5157         wait_request_state $fid ARCHIVE SUCCEED
5158 }
5159 run_test 402b "CDT must retry request upon slow start of CT"
5160
5161 test_403() {
5162         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5163
5164         local agent=$(facet_active_host $SINGLEAGT)
5165
5166         # deactivate all mdc for MDT0001
5167         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5168
5169         copytool setup
5170         local uuid=$(get_agent_uuid $agent)
5171         # check the agent is registered on MDT0000, and not on MDT0001
5172         check_agent_registered_by_mdt $uuid 0
5173         check_agent_unregistered_by_mdt $uuid 1
5174
5175         # check running copytool process
5176         search_copytools $agent || error "No running copytools on $agent"
5177
5178         # reactivate all mdc for MDT0001
5179         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5180
5181         # make sure the copytool is now registered to all MDTs
5182         check_agent_registered $uuid
5183 }
5184 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
5185
5186 test_404() {
5187         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5188
5189         copytool setup
5190
5191         # create files on both MDT0000 and MDT0001
5192         mkdir -p $DIR/$tdir
5193
5194         local dir_mdt0=$DIR/$tdir/mdt0
5195         stack_trap "rm -rf $dir_mdt0" EXIT
5196         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5197
5198         # create 1 file on mdt0
5199         local fid1=$(create_small_file $dir_mdt0/$tfile)
5200
5201         # deactivate all mdc for MDT0001
5202         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5203
5204         # send an HSM request for files in MDT0000
5205         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5206
5207         # check for completion of files in MDT0000
5208         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5209                 echo "archive successful on mdt0"
5210
5211         # reactivate all mdc for MDT0001
5212         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5213 }
5214 run_test 404 "Inactive MDT does not block requests for active MDTs"
5215
5216 test_405() {
5217         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5218
5219         copytool setup
5220
5221         mkdir -p $DIR/$tdir
5222
5223         local striped_dir=$DIR/$tdir/striped_dir
5224
5225         # create striped dir on all of MDTs
5226         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
5227
5228         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
5229         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
5230         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
5231         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
5232
5233         local idx1=$($LFS getstripe -m $striped_dir/${tfile}_0)
5234         local idx2=$($LFS getstripe -m $striped_dir/${tfile}_1)
5235         local idx3=$($LFS getstripe -m $striped_dir/${tfile}_2)
5236         local idx4=$($LFS getstripe -m $striped_dir/${tfile}_3)
5237
5238         # check that compound requests are shunt to the rights MDTs
5239         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5240                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5241                 error "lfs hsm_archive"
5242
5243         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5244                 echo "archive successful on $fid1"
5245         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5246                 echo "archive successful on $fid2"
5247         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5248                 echo "archive successful on $fid3"
5249         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5250                 echo "archive successful on $fid4"
5251
5252         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5253         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5254         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5255         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5256
5257         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5258         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5259         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5260         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5261 }
5262 run_test 405 "archive and release under striped directory"
5263
5264 test_406() {
5265         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5266
5267         [ $MDS_VERSION_CODE -lt $(version_code 2.7.64) ] &&
5268                 skip "need MDS version at least 2.7.64"
5269
5270         local fid
5271         local mdt_index
5272
5273         fid=$(create_small_file $DIR/$tdir/$tfile)
5274         echo "old fid $fid"
5275
5276         copytool setup
5277
5278         $LFS hsm_archive $DIR/$tdir/$tfile
5279         wait_request_state "$fid" ARCHIVE SUCCEED
5280         $LFS hsm_release $DIR/$tdir/$tfile
5281
5282         # Should migrate $tdir but not $tfile.
5283         $LFS migrate -m1 $DIR/$tdir &&
5284                 error "migrating HSM an archived file should fail"
5285
5286         $LFS hsm_restore $DIR/$tdir/$tfile
5287         wait_request_state "$fid" RESTORE SUCCEED
5288
5289         $LFS hsm_remove $DIR/$tdir/$tfile
5290         wait_request_state "$fid" REMOVE SUCCEED
5291
5292         cat $DIR/$tdir/$tfile > /dev/null ||
5293                 error "cannot read $DIR/$tdir/$tfile"
5294
5295         $LFS migrate -m1 $DIR/$tdir ||
5296                 error "cannot complete migration after HSM remove"
5297
5298         mdt_index=$($LFS getstripe -m $DIR/$tdir)
5299         if ((mdt_index != 1)); then
5300                 error "expected MDT index 1, got $mdt_index"
5301         fi
5302
5303         # Refresh fid after migration.
5304         fid=$(path2fid $DIR/$tdir/$tfile)
5305         echo "new fid $fid"
5306
5307         $LFS hsm_archive $DIR/$tdir/$tfile
5308         wait_request_state "$fid" ARCHIVE SUCCEED 1
5309
5310         lctl set_param debug=+trace
5311         $LFS hsm_release $DIR/$tdir/$tfile ||
5312                 error "cannot release $DIR/$tdir/$tfile"
5313
5314         $LFS hsm_restore $DIR/$tdir/$tfile
5315         wait_request_state "$fid" RESTORE SUCCEED 1
5316
5317         cat $DIR/$tdir/$tfile > /dev/null ||
5318                 error "cannot read $DIR/$tdir/$tfile"
5319 }
5320 run_test 406 "attempting to migrate HSM archived files is safe"
5321
5322 test_407() {
5323         local f=$DIR/$tdir/$tfile
5324         local f2=$DIR2/$tdir/$tfile
5325         local fid=$(create_empty_file "$f")
5326
5327         copytool setup
5328
5329         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5330         wait_request_state $fid ARCHIVE SUCCEED
5331         $LFS hsm_release $f
5332
5333 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5334         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5335
5336         # Prevent restore from completing
5337         copytool_suspend
5338
5339         md5sum $f &
5340         # 1st request holds layout lock while appropriate
5341         # RESTORE record is still not added to llog
5342         md5sum $f2 &
5343         sleep 2
5344
5345         # after umount hsm_actions->O/x/x log shouldn't have
5346         # double RESTORE records like below
5347         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5348         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5349         sleep 30 &&
5350                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5351         fail $SINGLEMDS
5352
5353         copytool_continue
5354         wait_request_state $fid RESTORE SUCCEED
5355 }
5356 run_test 407 "Check for double RESTORE records in llog"
5357
5358 test_500()
5359 {
5360         [ $MDS_VERSION_CODE -lt $(version_code 2.6.92) ] &&
5361                 skip "HSM migrate is not supported"
5362
5363         test_mkdir -p $DIR/$tdir
5364
5365         if [ $(lustre_version_code client) -lt $(version_code 2.11.56) ] ||
5366              [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ];
5367         then
5368                 llapi_hsm_test -d $DIR/$tdir -b ||
5369                         error "One llapi HSM test failed"
5370         else
5371                 llapi_hsm_test -d $DIR/$tdir ||
5372                         error "One llapi HSM test failed"
5373         fi
5374 }
5375 run_test 500 "various LLAPI HSM tests"
5376
5377 test_600() {
5378         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5379                 skip "need MDS version at least 2.10.58"
5380
5381         mkdir -p $DIR/$tdir
5382
5383         local f=$DIR/$tdir/$tfile
5384
5385         changelog_register
5386         # set changelog_mask to ALL
5387         changelog_chmask "ALL"
5388
5389         chmod 777 $DIR/$tdir
5390         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5391         local fid=$(path2fid $f)
5392
5393         local entry
5394         entry=$(changelog_find -type CREAT -target-fid $fid -uid "$RUNAS_ID" \
5395                                -gid "$RUNAS_GID") ||
5396                 error "No matching CREAT entry"
5397
5398         # Parse the changelog
5399         eval local -A changelog=$(changelog2array $entry)
5400         local nid="${changelog[nid]}"
5401
5402         # Check its NID
5403         echo "Got NID '$nid'"
5404         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5405                 error "nid '$nid' does not match any client NID:" \
5406                       "${CLIENT_NIDS[@]}"
5407 }
5408 run_test 600 "Changelog fields 'u=' and 'nid='"
5409
5410 test_601() {
5411         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5412                 skip "need MDS version at least 2.10.58"
5413
5414         mkdir -p $DIR/$tdir
5415
5416         local f=$DIR/$tdir/$tfile
5417
5418         changelog_register
5419         # set changelog_mask to ALL
5420         changelog_chmask "ALL"
5421
5422         touch $f || error "touch $f failed"
5423         local fid=$(path2fid $f)
5424
5425         changelog_clear
5426         cat $f || error "cat $f failed"
5427
5428         changelog_find -type OPEN -target-fid $fid -mode "r--" ||
5429                 error "No matching OPEN entry"
5430 }
5431 run_test 601 "OPEN Changelog entry"
5432
5433 test_602() {
5434         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5435                 skip "need MDS version at least 2.10.58"
5436
5437         mkdir -p $DIR/$tdir
5438
5439         local f=$DIR/$tdir/$tfile
5440
5441         changelog_register
5442         # set changelog_mask to ALL
5443         changelog_chmask "ALL"
5444
5445         touch $f || error "touch $f failed"
5446         local fid=$(path2fid $f)
5447
5448         changelog_clear
5449         cat $f || error "cat $f failed"
5450
5451         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5452
5453         changelog_clear
5454         changelog_dump
5455         echo f > $f || error "write $f failed"
5456         changelog_dump
5457
5458         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5459
5460         # remove OPEN from changelog_mask
5461         changelog_chmask "-OPEN"
5462
5463         changelog_clear
5464         changelog_dump
5465         cat $f || error "cat $f failed"
5466         changelog_dump
5467
5468         changelog_find -type CLOSE -target-fid $fid &&
5469                 error "There should be no CLOSE entry"
5470
5471         changelog_clear
5472         changelog_dump
5473         echo f > $f || error "write $f failed"
5474         changelog_dump
5475
5476         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5477 }
5478 run_test 602 "Changelog record CLOSE only if open+write or OPEN recorded"
5479
5480 test_603() {
5481         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5482                 skip "need MDS version at least 2.10.58"
5483
5484         mkdir -p $DIR/$tdir
5485
5486         local f=$DIR/$tdir/$tfile
5487
5488         changelog_register
5489         # set changelog_mask to ALL
5490         changelog_chmask "ALL"
5491
5492         touch $f || error "touch $f failed"
5493         local fid=$(path2fid $f)
5494
5495         setfattr -n user.xattr1 -v "value1" $f || error "setfattr $f failed"
5496
5497         changelog_clear
5498         getfattr -n user.xattr1 $f || error "getfattr $f failed"
5499
5500         changelog_find -type GXATR -target-fid $fid -xattr "user.xattr1" ||
5501                 error "No matching GXATR entry"
5502 }
5503 run_test 603 "GETXATTR Changelog entry"
5504
5505 test_604() {
5506         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5507                 skip "need MDS version at least 2.10.58"
5508
5509         mkdir -p $DIR/$tdir
5510
5511         local f=$DIR/$tdir/$tfile
5512         local f2=$DIR2/$tdir/$tfile
5513         local procname="mdd.$FSNAME-MDT0000.changelog_deniednext"
5514         local timeout
5515         timeout="$(do_facet mds1 "$LCTL" get_param -n "$procname")"
5516         stack_trap "do_facet mds1 '$LCTL' set_param '$procname=$timeout'" EXIT
5517         do_facet mds1 lctl set_param "$procname=20"
5518
5519
5520         changelog_register
5521         # set changelog_mask to ALL
5522         changelog_chmask "ALL"
5523
5524         touch $f || error "touch $f failed"
5525         local fid=$(path2fid $f)
5526
5527         chmod 600 $f
5528
5529         changelog_clear
5530         changelog_dump
5531         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5532         changelog_dump
5533
5534         local entry
5535         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5536                                -gid "$RUNAS_GID" -mode "r--") ||
5537                 error "No matching NOPEN entry"
5538
5539         # Parse the changelog
5540         eval local -A changelog=$(changelog2array $entry)
5541         local nid="${changelog[nid]}"
5542
5543         # Check its NID
5544         echo "Got NID '$nid'"
5545         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5546                 error "nid '$nid' does not match any client NID:" \
5547                       "${CLIENT_NIDS[@]}"
5548
5549         changelog_clear
5550         changelog_dump
5551         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5552         changelog_dump
5553
5554         changelog_find -type NOPEN -target-fid $fid &&
5555                 error "There should be no NOPEN entry"
5556
5557         # Sleep for `changelog_deniednext` seconds
5558         sleep 20
5559
5560         changelog_clear
5561         changelog_dump
5562         $RUNAS cat $f2 && error "cat $f by user $RUNAS_ID should have failed"
5563         changelog_dump
5564
5565         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5566                                -gid "$RUNAS_GID" -mode "r--") ||
5567                 error "No matching NOPEN entry"
5568
5569         # Parse the changelog
5570         eval local -A changelog=$(changelog2array $entry)
5571         local nid="${changelog[nid]}"
5572
5573         # Check the NID
5574         echo "Got NID '$nid'"
5575         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5576                 error "nid '$nid' does not match any client NID:" \
5577                       "${CLIENT_NIDS[@]}"
5578 }
5579 run_test 604 "NOPEN Changelog entry"
5580
5581 test_605() {
5582         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5583                 skip "need MDS version at least 2.10.58"
5584
5585         mkdir -p $DIR/$tdir
5586
5587         local f=$DIR/$tdir/$tfile
5588         local f2=$DIR2/$tdir/$tfile
5589
5590         changelog_register
5591         # set changelog_mask to ALL
5592         changelog_chmask "ALL"
5593
5594         touch $f || error "touch $f failed"
5595         local fid=$(path2fid $f)
5596
5597         changelog_clear
5598         changelog_dump
5599         exec 3<> $f || error "open $f failed"
5600         changelog_dump
5601
5602         local entry
5603         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5604
5605         changelog_clear
5606         changelog_dump
5607         exec 4<> $f || error "open $f failed"
5608         changelog_dump
5609
5610         changelog_find -type OPEN -target-fid $fid &&
5611                 error "There should be no OPEN entry"
5612
5613         exec 4>&- || error "close $f failed"
5614         changelog_dump
5615
5616         changelog_find -type CLOSE -target-fid $fid &&
5617                 error "There should be no CLOSE entry"
5618
5619         changelog_clear
5620         changelog_dump
5621         # access in rw, so different access mode should generate entries
5622         cat $f || error "cat $f failed"
5623         changelog_dump
5624
5625         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5626
5627         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5628
5629         changelog_clear
5630         changelog_dump
5631         # same access as first one, should not generate new entries
5632         exec 4<> $f || error "open $f failed"
5633         changelog_dump
5634
5635         changelog_find -type OPEN -target-fid $fid &&
5636                 error "There should be no OPEN entry"
5637
5638         exec 4>&- || error "close $f failed"
5639         changelog_dump
5640
5641         changelog_find -type CLOSE -target-fid $fid &&
5642                 error "There should be no CLOSE entry"
5643
5644         changelog_clear
5645         changelog_dump
5646         # access by different user should generate new entries
5647         $RUNAS cat $f || error "cat $f by user $RUNAS_ID failed"
5648         changelog_dump
5649
5650         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5651
5652         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5653
5654         changelog_clear
5655         changelog_dump
5656         exec 3>&- || error "close $f failed"
5657         changelog_dump
5658
5659         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5660 }
5661 run_test 605 "Test OPEN and CLOSE rate limit in Changelogs"
5662
5663 test_606() {
5664         [ $MDS_VERSION_CODE -lt $(version_code 2.10.58) ] &&
5665                 skip "need MDS version at least 2.10.58"
5666
5667         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5668         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5669         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5670                         skip_env "missing llog_reader" && return
5671         local fstype=$(facet_fstype mds1)
5672
5673         mkdir -p $DIR/$tdir
5674
5675         local f=$DIR/$tdir/$tfile
5676
5677         changelog_register
5678         # set changelog_mask to ALL
5679         changelog_chmask "ALL"
5680
5681         chmod 777 $DIR/$tdir
5682         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5683         local fid=$(path2fid $f)
5684         rm $f || error "rm $f failed"
5685
5686         local mntpt=$(facet_mntpt mds1)
5687         local pass=true
5688         local entry
5689
5690         #remount mds1 as ldiskfs or zfs type
5691         stack_trap "stop mds1; start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS" EXIT
5692         stop mds1 || error "stop mds1 failed"
5693         mount_fstype mds1 || error "remount mds1 failed"
5694
5695         for ((i = 0; i < 1; i++)); do
5696                 do_facet mds1 $llog_reader $mntpt/changelog_catalog
5697                 local cat_file=$(do_facet mds1 $llog_reader \
5698                                 $mntpt/changelog_catalog | awk \
5699                                 '{match($0,"path=([^ ]+)",a)}END{print a[1]}')
5700                 [ -n "$cat_file" ] || error "no catalog file"
5701
5702                 entry=$(do_facet mds1 $llog_reader $mntpt/$cat_file |
5703                         awk "/CREAT/ && /target:\[$fid\]/ {print}")
5704                 [ -n "$entry" ] || error "no CREAT entry"
5705         done
5706
5707         local uidgid=$(echo $entry |
5708                 sed 's+.*\ user:\([0-9][0-9]*:[0-9][0-9]*\)\ .*+\1+')
5709         [ -n "$uidgid" ] || error "uidgid is empty"
5710         echo "Got UID/GID $uidgid"
5711         [ "$uidgid" = "$RUNAS_ID:$RUNAS_GID" ] ||
5712                 error "uidgid '$uidgid' != '$RUNAS_ID:$RUNAS_GID'"
5713         local nid=$(echo $entry |
5714                 sed 's+.*\ nid:\(\S\S*@\S\S*\)\ .*+\1+')
5715         [ -n "$nid" ] || error "nid is empty"
5716         echo "Got NID $nid"
5717         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5718                 error "nid '$nid' does not match any NID ${CLIENT_NIDS[@]}"
5719 }
5720 run_test 606 "llog_reader groks changelog fields"
5721
5722 complete $SECONDS
5723 check_and_cleanup_lustre
5724 exit_status