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