Whamcloud - gitweb
c178c08e1fecaef66c42f426d4d8f4f56ee7f5cc
[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_25a() {
2242         # test needs a running copytool
2243         copytool setup
2244
2245         mkdir -p $DIR/$tdir
2246         copy2archive /etc/hosts $tdir/$tfile
2247
2248         local f=$DIR/$tdir/$tfile
2249
2250         copytool import $tdir/$tfile $f
2251
2252         $LFS hsm_set --lost $f
2253
2254         md5sum $f
2255         local st=$?
2256
2257         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2258 }
2259 run_test 25a "Restore lost file (HS_LOST flag) from import"\
2260              " (Operation not permitted)"
2261
2262 test_25b() {
2263         # test needs a running copytool
2264         copytool setup
2265
2266         mkdir -p $DIR/$tdir
2267
2268         local f=$DIR/$tdir/$tfile
2269         local fid=$(copy_file /etc/passwd $f)
2270
2271         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2272         wait_request_state $fid ARCHIVE SUCCEED
2273
2274         $LFS hsm_release $f
2275         $LFS hsm_set --lost $f
2276         md5sum $f
2277         st=$?
2278
2279         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2280 }
2281 run_test 25b "Restore lost file (HS_LOST flag) after release"\
2282              " (Operation not permitted)"
2283
2284 test_26A() { # was test_26
2285         # test needs a running copytool
2286         copytool setup
2287
2288         local f=$DIR/$tdir/$tfile
2289         local fid=$(create_empty_file "$f")
2290
2291         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2292         wait_request_state $fid ARCHIVE SUCCEED
2293
2294         $LFS hsm_remove $f
2295         wait_request_state $fid REMOVE SUCCEED
2296
2297         check_hsm_flags $f "0x00000000"
2298 }
2299 run_test 26A "Remove the archive of a valid file"
2300
2301 test_26a() {
2302         local raolu=$(get_hsm_param remove_archive_on_last_unlink)
2303         [[ $raolu -eq 0 ]] || error "RAoLU policy should be off"
2304
2305         # test needs a running copytool
2306         copytool setup
2307
2308         mkdir -p $DIR/$tdir
2309         local f=$DIR/$tdir/$tfile
2310         local fid=$(copy_file /etc/passwd $f)
2311
2312         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2313         wait_request_state $fid ARCHIVE SUCCEED
2314
2315         local f2=$DIR/$tdir/${tfile}_2
2316         local fid2=$(copy_file /etc/passwd $f2)
2317
2318         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2319         wait_request_state $fid2 ARCHIVE SUCCEED
2320
2321         local f3=$DIR/$tdir/${tfile}_3
2322         local fid3=$(copy_file /etc/passwd $f3)
2323
2324         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f3
2325         wait_request_state $fid3 ARCHIVE SUCCEED
2326
2327         # set a long grace_delay vs short loop_period
2328         local orig_loop_period=$(get_hsm_param loop_period)
2329         local orig_grace_delay=$(get_hsm_param grace_delay)
2330         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2331         set_hsm_param loop_period 10
2332         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2333         set_hsm_param grace_delay 100
2334
2335         rm -f $f
2336
2337         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2338         set_hsm_param remove_archive_on_last_unlink 1
2339
2340         ln "$f3" "$f3"_bis || error "Unable to create hard-link"
2341         rm -f $f3
2342
2343         rm -f $f2
2344
2345         wait_request_state $fid2 REMOVE SUCCEED
2346
2347         assert_request_count $fid REMOVE 0 \
2348                 "Unexpected archived data remove request for $f"
2349         assert_request_count $fid3 REMOVE 0 \
2350                 "Unexpected archived data remove request for $f3"
2351 }
2352 run_test 26a "Remove Archive On Last Unlink (RAoLU) policy"
2353
2354 test_26b() {
2355         # test needs a running copytool
2356         copytool setup
2357
2358         mkdir -p $DIR/$tdir
2359         local f=$DIR/$tdir/$tfile
2360         local fid=$(copy_file /etc/passwd $f)
2361
2362         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2363         wait_request_state $fid ARCHIVE SUCCEED
2364
2365         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2366         set_hsm_param remove_archive_on_last_unlink 1
2367
2368         cdt_shutdown
2369         cdt_check_state stopped
2370
2371         rm -f $f
2372
2373         wait_request_state $fid REMOVE WAITING
2374
2375         cdt_enable
2376
2377         # copytool must re-register
2378         kill_copytools
2379         wait_copytools || error "copytool failed to stop"
2380         copytool setup
2381
2382         wait_request_state $fid REMOVE SUCCEED
2383 }
2384 run_test 26b "RAoLU policy when CDT off"
2385
2386 test_26c() {
2387         # test needs a running copytool
2388         copytool setup
2389
2390         mkdir -p $DIR/$tdir
2391         local f=$DIR/$tdir/$tfile
2392         local fid=$(copy_file /etc/passwd $f)
2393
2394         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2395         wait_request_state $fid ARCHIVE SUCCEED
2396
2397         local f2=$DIR/$tdir/${tfile}_2
2398         local fid2=$(copy_file /etc/passwd $f2)
2399
2400         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2401         wait_request_state $fid2 ARCHIVE SUCCEED
2402
2403         # set a long grace_delay vs short loop_period
2404         local orig_loop_period=$(get_hsm_param loop_period)
2405         local orig_grace_delay=$(get_hsm_param grace_delay)
2406         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2407         set_hsm_param loop_period 10
2408         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2409         set_hsm_param grace_delay 100
2410
2411         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2412         set_hsm_param remove_archive_on_last_unlink 1
2413
2414         multiop_bg_pause $f O_c || error "open $f failed"
2415         local pid=$!
2416
2417         rm -f $f
2418         rm -f $f2
2419
2420         wait_request_state $fid2 REMOVE SUCCEED
2421         assert_request_count $fid REMOVE 0 \
2422                 "Unexpected archived data remove request for $f"
2423
2424         kill -USR1 $pid || error "multiop early exit"
2425         # should reach autotest timeout if multiop fails to trap
2426         # signal, close file, and exit ...
2427         wait $pid || error "wait PID $PID failed"
2428
2429         wait_request_state $fid REMOVE SUCCEED
2430 }
2431 run_test 26c "RAoLU effective when file closed"
2432
2433 test_26d() {
2434         # test needs a running copytool
2435         copytool setup
2436
2437         mkdir -p $DIR/$tdir
2438         local f=$DIR/$tdir/$tfile
2439         local fid=$(create_small_file $f)
2440
2441         $LFS hsm_archive $f || error "could not archive file"
2442         wait_request_state $fid ARCHIVE SUCCEED
2443
2444         # set a long grace_delay vs short loop_period
2445         local orig_loop_period=$(get_hsm_param loop_period)
2446         local orig_grace_delay=$(get_hsm_param grace_delay)
2447         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2448         set_hsm_param loop_period 10
2449         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2450         set_hsm_param grace_delay 100
2451
2452         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2453         set_hsm_param remove_archive_on_last_unlink 1
2454
2455         multiop_bg_pause $f O_c || error "multiop failed"
2456         local MULTIPID=$!
2457
2458         rm -f $f
2459
2460         mds_evict_client
2461
2462         wait_request_state $fid REMOVE SUCCEED
2463
2464         client_up || client_up || true
2465
2466         kill -USR1 $MULTIPID
2467         wait $MULTIPID || error "multiop close failed"
2468 }
2469 run_test 26d "RAoLU when Client eviction"
2470
2471 test_27a() {
2472         # test needs a running copytool
2473         copytool setup
2474
2475         create_archive_file $tdir/$tfile
2476         local f=$DIR/$tdir/$tfile
2477         copytool import $tdir/$tfile $f
2478         local fid=$(path2fid $f)
2479
2480         $LFS hsm_remove $f
2481
2482         [[ $? != 0 ]] || error "Remove of a released file should fail"
2483 }
2484 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
2485
2486 test_27b() {
2487         # test needs a running copytool
2488         copytool setup
2489
2490         local f=$DIR/$tdir/$tfile
2491         local fid=$(create_empty_file "$f")
2492
2493         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2494         wait_request_state $fid ARCHIVE SUCCEED
2495         $LFS hsm_release $f
2496
2497         $LFS hsm_remove $f
2498
2499         [[ $? != 0 ]] || error "Remove of a released file should fail"
2500 }
2501 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
2502
2503 test_28() {
2504         # test needs a running copytool
2505         copytool setup
2506
2507         local f=$DIR/$tdir/$tfile
2508         local fid=$(create_empty_file "$f")
2509
2510         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2511         wait_request_state $fid ARCHIVE SUCCEED
2512
2513         cdt_disable
2514         $LFS hsm_remove $f
2515
2516         rm -f $f
2517
2518         cdt_enable
2519
2520         wait_request_state $fid REMOVE SUCCEED
2521 }
2522 run_test 28 "Concurrent archive/file remove"
2523
2524 test_29a() {
2525         # Tests --mntpath and --archive options
2526
2527         local archive_id=7
2528         copytool setup -m "$MOUNT" -a $archive_id
2529
2530         # Bad archive number
2531         $LFS hsm_remove -m "$MOUNT" -a 33 0x857765760:0x8:0x2 2>&1 |
2532                 grep "Invalid argument" ||
2533                 error "unexpected hsm_remove failure (1)"
2534
2535         # mntpath is present but file is given
2536         $LFS hsm_remove --mntpath "$MOUNT" --archive 30 /qwerty/uyt 2>&1 |
2537                 grep "hsm: '/qwerty/uyt' is not a valid FID" ||
2538                 error "unexpected hsm_remove failure (2)"
2539 }
2540 run_test 29a "Tests --mntpath and --archive options"
2541
2542 test_29b() {
2543         # test needs a running copytool
2544         copytool setup
2545
2546         local f=$DIR/$tdir/$tfile
2547         local fid=$(create_small_file $f)
2548
2549         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2550         wait_request_state $fid ARCHIVE SUCCEED
2551
2552         rm -f $f
2553
2554         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER $fid
2555         wait_request_state $fid REMOVE SUCCEED
2556 }
2557 run_test 29b "Archive/delete/remove by FID from the archive."
2558
2559 test_29c() {
2560         # test needs a running copytool
2561         copytool setup
2562
2563         local fid1=$(create_small_file $DIR/$tdir/$tfile-1)
2564         local fid2=$(create_small_file $DIR/$tdir/$tfile-2)
2565         local fid3=$(create_small_file $DIR/$tdir/$tfile-3)
2566
2567         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$tfile-[1-3]
2568         wait_request_state $fid1 ARCHIVE SUCCEED
2569         wait_request_state $fid2 ARCHIVE SUCCEED
2570         wait_request_state $fid3 ARCHIVE SUCCEED
2571
2572         rm -f $DIR/$tdir/$tfile-[1-3]
2573
2574         echo $fid1 > $DIR/$tdir/list
2575         echo $fid2 >> $DIR/$tdir/list
2576         echo $fid3 >> $DIR/$tdir/list
2577
2578         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER \
2579                 --filelist $DIR/$tdir/list
2580         wait_request_state $fid1 REMOVE SUCCEED
2581         wait_request_state $fid2 REMOVE SUCCEED
2582         wait_request_state $fid3 REMOVE SUCCEED
2583 }
2584 run_test 29c "Archive/delete/remove by FID, using a file list."
2585
2586 test_29d() {
2587         # test needs more than one CT
2588         needclients 3 || return 0
2589
2590         local n
2591         local file
2592         local fid
2593
2594         # start all of the copytools
2595         for n in $(seq $AGTCOUNT); do
2596                 copytool setup -f agt$n -a $n
2597         done
2598
2599         # archive files
2600         file=$DIR/$tdir/$tfile
2601         fid=$(create_small_file $file)
2602
2603         $LFS hsm_archive $file
2604         wait_request_state $fid ARCHIVE SUCCEED
2605         check_hsm_flags $file "0x00000009"
2606
2607         rm -f $file
2608
2609         $LFS hsm_remove --mntpath "$MOUNT" -a 0 $fid ||
2610                 error "cannot hsm_remove '$fid'"
2611
2612         # give time for CDT to handle remove request and create broadcasted
2613         sleep 2
2614
2615         # remove request has been broadcasted ?
2616         local cnt=$(get_request_count $fid REMOVE)
2617         # broadcasted requests + original
2618         [[ $cnt -eq $((AGTCOUNT + 1)) ]] ||
2619                 error "remove not broadcasted to all CTs"
2620
2621         # give time for CDT and CTs to handle broadcasted
2622         wait_for_loop_period
2623
2624         # each agent serves one different archive_id, so broadcasted
2625         # hsm_remove request should only succeed once and fail at all others
2626         local res
2627         local scnt=0
2628         local fcnt=0
2629         for n in $(seq $AGTCOUNT); do
2630                 res=$(do_facet $SINGLEMDS "$LCTL get_param -n \
2631                                $HSM_PARAM.actions | awk \
2632                                '/'$fid'.*action=REMOVE archive#='$n'/ \
2633                                {print \\\$13}' | cut -f2 -d=")
2634                 if [[ "$res" == "SUCCEED" ]]; then
2635                         scnt=$((scnt + 1))
2636                 elif [[ "$res" == "FAILED" ]]; then
2637                         fcnt=$((fcnt + 1))
2638                 fi
2639         done
2640
2641         [[ $scnt -eq 1 ]] ||
2642                 error "one and only CT should have removed successfully"
2643
2644         [[ $AGTCOUNT -eq $((scnt + fcnt)) ]] ||
2645                 error "all but one CT should have failed to remove"
2646 }
2647 run_test 29d "hsm_remove by FID with archive_id 0 for unlinked file cause "\
2648              "request to be sent once for each registered archive_id"
2649
2650 test_30a() {
2651         # restore at exec cannot work on agent node (because of Linux kernel
2652         # protection of executables)
2653         needclients 2 || return 0
2654
2655         # test needs a running copytool
2656         copytool setup
2657
2658         mkdir -p $DIR/$tdir
2659         copy2archive /bin/true $tdir/$tfile
2660
2661         local f=$DIR/$tdir/true
2662         copytool import $tdir/$tfile $f
2663
2664         local fid=$(path2fid $f)
2665
2666         stack_trap "cdt_clear_no_retry" EXIT
2667         # set no retry action mode
2668         cdt_set_no_retry
2669         do_node $CLIENT2 $f
2670         local st=$?
2671
2672         $LFS hsm_state $f
2673
2674         [[ $st == 0 ]] || error "Failed to exec a released file"
2675 }
2676 run_test 30a "Restore at exec (import case)"
2677
2678 test_30b() {
2679         # restore at exec cannot work on agent node (because of Linux kernel
2680         # protection of executables)
2681         needclients 2 || return 0
2682
2683         # test needs a running copytool
2684         copytool setup
2685
2686         mkdir -p $DIR/$tdir
2687         local f=$DIR/$tdir/true
2688         local fid=$(copy_file /bin/true $f)
2689         chmod 755 $f
2690         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2691         wait_request_state $fid ARCHIVE SUCCEED
2692         $LFS hsm_release $f
2693         $LFS hsm_state $f
2694
2695         stack_trap cdt_clear_no_retry EXIT
2696         # set no retry action mode
2697         cdt_set_no_retry
2698
2699         do_node $CLIENT2 $f
2700         local st=$?
2701
2702         $LFS hsm_state $f
2703
2704         [[ $st == 0 ]] || error "Failed to exec a released file"
2705 }
2706 run_test 30b "Restore at exec (release case)"
2707
2708 test_30c() {
2709         needclients 2 || return 0
2710
2711         # test needs a running copytool
2712         copytool setup
2713
2714         mkdir -p $DIR/$tdir
2715         local f=$DIR/$tdir/SLEEP
2716         local slp_sum1=$(md5sum /bin/sleep)
2717         local fid=$(copy_file /bin/sleep $f)
2718         chmod 755 $f
2719         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2720         wait_request_state $fid ARCHIVE SUCCEED
2721         $LFS hsm_release $f
2722         check_hsm_flags $f "0x0000000d"
2723
2724         stack_trap cdt_clear_no_retry EXIT
2725         # set no retry action mode
2726         cdt_set_no_retry
2727
2728         do_node $CLIENT2 "$f 10" &
2729         local pid=$!
2730         sleep 3
2731         echo 'Hi!' > $f
2732         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2733         wait $pid
2734         [[ $? == 0 ]] || error "Execution failed during run"
2735         cmp /bin/sleep $f
2736         if [[ $? != 0 ]]; then
2737                 local slp_sum2=$(md5sum /bin/sleep)
2738                 # in case sleep file is modified during the test
2739                 [[ $slp_sum1 == $slp_sum2 ]] &&
2740                         error "Binary overwritten during exec"
2741         fi
2742
2743         check_hsm_flags $f "0x00000009"
2744 }
2745 run_test 30c "Update during exec of released file must fail"
2746
2747 restore_and_check_size() {
2748         local f=$1
2749         local fid=$2
2750         local s=$(stat -c "%s" $f)
2751         local n=$s
2752         local st=$(get_hsm_flags $f)
2753         local err=0
2754         local cpt=0
2755         $LFS hsm_restore $f
2756         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2757         do
2758                 n=$(stat -c "%s" $f)
2759                 # we echo in both cases to show stat is not hang
2760                 if [[ $n != $s ]]; then
2761                         echo "size seen is $n != $s"
2762                         err=1
2763                 else
2764                         echo "size seen is right: $n == $s"
2765                 fi
2766                 sleep 10
2767                 cpt=$((cpt + 1))
2768                 st=$(get_hsm_flags $f)
2769         done
2770         if [[ "$st" = "0x00000009" ]]; then
2771                 echo " "done
2772         else
2773                 echo " restore is too long"
2774                 wait_request_state $fid RESTORE SUCCEED
2775         fi
2776         return $err
2777 }
2778
2779 test_31a() {
2780         # test needs a running copytool
2781         copytool setup
2782
2783         create_archive_file $tdir/$tfile
2784         local f=$DIR/$tdir/$tfile
2785         copytool import $tdir/$tfile $f
2786         local fid=$($LFS path2fid $f)
2787         copytool setup
2788
2789         restore_and_check_size $f $fid
2790         local err=$?
2791
2792         [[ $err -eq 0 ]] || error "File size changed during restore"
2793 }
2794 run_test 31a "Import a large file and check size during restore"
2795
2796
2797 test_31b() {
2798         # test needs a running copytool
2799         copytool setup
2800
2801         local f=$DIR/$tdir/$tfile
2802         local fid=$(create_file "$f" 1MB 39)
2803
2804         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2805         wait_request_state $fid ARCHIVE SUCCEED
2806         $LFS hsm_release $f
2807
2808         restore_and_check_size $f $fid
2809         local err=$?
2810
2811         [[ $err -eq 0 ]] || error "File size changed during restore"
2812 }
2813 run_test 31b "Restore a large unaligned file and check size during restore"
2814
2815 test_31c() {
2816         # test needs a running copytool
2817         copytool setup
2818
2819         local f=$DIR/$tdir/$tfile
2820         local fid=$(create_file "$f" 1M 39)
2821
2822         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2823         wait_request_state $fid ARCHIVE SUCCEED
2824         $LFS hsm_release $f
2825
2826         restore_and_check_size $f $fid
2827         local err=$?
2828
2829         [[ $err -eq 0 ]] || error "File size changed during restore"
2830 }
2831 run_test 31c "Restore a large aligned file and check size during restore"
2832
2833 test_33() {
2834         local f=$DIR/$tdir/$tfile
2835         local fid=$(create_empty_file "$f")
2836
2837         copytool setup
2838
2839         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2840         wait_request_state $fid ARCHIVE SUCCEED
2841         $LFS hsm_release $f
2842
2843         # Prevent restore from completing
2844         copytool_suspend
2845
2846         # Implicit restore
2847         md5sum $f >/dev/null &
2848         local pid=$!
2849
2850         wait_request_state $fid RESTORE STARTED
2851         kill -15 $pid
2852
2853         copytool_continue
2854
2855         # Check restore trigger process was killed
2856         wait $pid
2857         [ $? -eq 143 ] || error "md5sum was not 'Terminated'"
2858 }
2859 run_test 33 "Kill a restore waiting process"
2860
2861 test_34() {
2862         # test needs a running copytool
2863         copytool setup -b 1
2864
2865         local f=$DIR/$tdir/$tfile
2866         local fid=$(create_empty_file "$f")
2867
2868         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2869         wait_request_state $fid ARCHIVE SUCCEED
2870         $LFS hsm_release $f
2871
2872         # Prevent restore from completing
2873         copytool_suspend
2874
2875         md5sum $f >/dev/null &
2876         local pid=$!
2877
2878         wait_request_state $fid RESTORE STARTED
2879
2880         # rm must not block during restore
2881         timeout --signal=KILL 1 rm "$f" || error "rm $f failed"
2882
2883         copytool_continue
2884         wait_request_state $fid RESTORE SUCCEED
2885
2886         # Check md5sum pgm finished
2887         kill -0 $pid && error "Restore initiatior still running"
2888         wait $pid || error "Restore initiator failed with $?"
2889
2890         # Check the file was actually deleted
2891         [ ! -f "$f" ] || error "$f was not deleted"
2892 }
2893 run_test 34 "Remove file during restore"
2894
2895 test_35() {
2896         # test needs a running copytool
2897         copytool setup -b 1
2898
2899         local f=$DIR/$tdir/$tfile
2900         local f1=$DIR/$tdir/$tfile-1
2901         local fid=$(create_empty_file "$f")
2902         local fid1=$(copy_file /etc/passwd $f1)
2903
2904         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2905         wait_request_state $fid ARCHIVE SUCCEED
2906         $LFS hsm_release $f
2907
2908         # Prevent restore from completing
2909         copytool_suspend
2910
2911         md5sum $f >/dev/null &
2912         local pid=$!
2913
2914         wait_request_state $fid RESTORE STARTED
2915
2916         # mv must not block during restore
2917         timeout --signal=KILL 1 mv "$f1" "$f" || error "mv $f1 $f failed"
2918
2919         copytool_continue
2920         wait_request_state $fid RESTORE SUCCEED
2921
2922         # Check md5sum pgm finished
2923         kill -0 $pid && error "Restore initiatior still running"
2924         wait $pid || error "Restore initiator failed with $?"
2925
2926         local fid2=$(path2fid $f)
2927         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2928 }
2929 run_test 35 "Overwrite file during restore"
2930
2931 test_36() {
2932         # test needs a running copytool
2933         copytool setup -b 1
2934
2935         local f=$DIR/$tdir/$tfile
2936         local fid=$(create_empty_file "$f")
2937
2938         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2939         wait_request_state $fid ARCHIVE SUCCEED
2940         $LFS hsm_release $f
2941
2942         # Prevent restore from completing
2943         copytool_suspend
2944
2945         md5sum $f >/dev/null &
2946         local pid=$!
2947
2948         wait_request_state $fid RESTORE STARTED
2949
2950         # mv must not block during restore
2951         timeout --signal=KILL 10 mv "$f" "$f.new" ||
2952                 error "mv '$f' '$f.new' failed with rc=$?"
2953
2954         copytool_continue
2955         wait_request_state $fid RESTORE SUCCEED
2956
2957         # Check md5sum pgm finished
2958         kill -0 $pid && error "Restore initiator is still running"
2959         wait $pid || error "Restore initiator failed with $?"
2960 }
2961 run_test 36 "Move file during restore"
2962
2963 test_37() {
2964         # LU-5683: check that an archived dirty file can be rearchived.
2965         copytool setup
2966
2967         local f=$DIR/$tdir/$tfile
2968         local fid
2969
2970         fid=$(create_small_file $f) || error "cannot create small file"
2971
2972         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2973         wait_request_state $fid ARCHIVE SUCCEED
2974         $LFS hsm_release $f || error "cannot release $f"
2975
2976         # Allow previous archive request to expire from the actions log.
2977         wait_for_grace_delay
2978
2979         # Dirty file.
2980         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
2981
2982         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2983         wait_request_state $fid ARCHIVE SUCCEED
2984 }
2985 run_test 37 "re-archive a dirty file"
2986
2987 multi_archive() {
2988         local prefix=$1
2989         local count=$2
2990         local n=""
2991
2992         for n in $(seq 1 $count); do
2993                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2994         done
2995         echo "$count archive requests submitted"
2996 }
2997
2998 test_40() {
2999         local stream_count=4
3000         local file_count=100
3001         mkdir -p $DIR/$tdir
3002         local f=$DIR/$tdir/$tfile
3003         local i=""
3004         local p=""
3005         local fid=""
3006         local max_requests=$(get_hsm_param max_requests)
3007
3008         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3009         # Increase the number of HSM request that can be performed in
3010         # parallel. With the coordinator running once per second, this
3011         # also limits the number of requests per seconds that can be
3012         # performed, so we pick a decent number. But we also need to keep
3013         # that number low because the copytool has no rate limit and will
3014         # fail some requests if if gets too many at once.
3015         set_hsm_param max_requests 300
3016
3017         for i in $(seq 1 $file_count); do
3018                 for p in $(seq 1 $stream_count); do
3019                         fid=$(copy_file /etc/hosts $f.$p.$i)
3020                 done
3021         done
3022
3023         copytool setup
3024
3025         # to be sure wait_all_done will not be mislead by previous tests
3026         cdt_purge
3027         wait_for_grace_delay
3028         typeset -a pids
3029         # start archive streams in background (archive files in parallel)
3030         for p in $(seq 1 $stream_count); do
3031                 multi_archive $f.$p $file_count &
3032                 pids[$p]=$!
3033         done
3034         echo -n  "Wait for all requests being enqueued..."
3035         wait ${pids[*]}
3036         echo OK
3037         wait_all_done 100
3038 }
3039 run_test 40 "Parallel archive requests"
3040
3041 test_52() {
3042         # test needs a running copytool
3043         copytool setup
3044
3045         mkdir -p $DIR/$tdir
3046         local f=$DIR/$tdir/$tfile
3047         local fid=$(create_small_file $f)
3048
3049         $LFS hsm_archive $f || error "could not archive file"
3050         wait_request_state $fid ARCHIVE SUCCEED
3051         check_hsm_flags $f "0x00000009"
3052
3053         multiop_bg_pause $f O_c || error "multiop failed"
3054         local MULTIPID=$!
3055
3056         mds_evict_client
3057         client_up || client_up || true
3058
3059         kill -USR1 $MULTIPID
3060         wait $MULTIPID || error "multiop close failed"
3061
3062         check_hsm_flags $f "0x0000000b"
3063 }
3064 run_test 52 "Opened for write file on an evicted client should be set dirty"
3065
3066 test_53() {
3067         # test needs a running copytool
3068         copytool setup
3069
3070         mkdir -p $DIR/$tdir
3071         local f=$DIR/$tdir/$tfile
3072         local fid=$(create_small_file $f)
3073
3074         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3075                 error "could not archive file"
3076         wait_request_state $fid ARCHIVE SUCCEED
3077         check_hsm_flags $f "0x00000009"
3078
3079         multiop_bg_pause $f o_c || error "multiop failed"
3080         MULTIPID=$!
3081
3082         mds_evict_client
3083         client_up || client_up || true
3084
3085         kill -USR1 $MULTIPID
3086         wait $MULTIPID || error "multiop close failed"
3087
3088         check_hsm_flags $f "0x00000009"
3089 }
3090 run_test 53 "Opened for read file on an evicted client should not be set dirty"
3091
3092 test_54() {
3093         local f=$DIR/$tdir/$tfile
3094         local fid=$(create_file "$f" 1MB 39)
3095
3096         copytool setup -b 1
3097
3098         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3099                 error "could not archive file"
3100         wait_request_state $fid ARCHIVE STARTED
3101
3102         check_hsm_flags $f "0x00000001"
3103
3104         stack_trap "cdt_clear_no_retry" EXIT
3105         # Avoid coordinator resending this request as soon it has failed.
3106         cdt_set_no_retry
3107
3108         echo "foo" >> $f
3109         sync
3110         wait_request_state $fid ARCHIVE FAILED
3111
3112         check_hsm_flags $f "0x00000003"
3113 }
3114 run_test 54 "Write during an archive cancels it"
3115
3116 test_55() {
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         $TRUNCATE $f 1024 || error "truncate failed"
3133         sync
3134         wait_request_state $fid ARCHIVE FAILED
3135
3136         check_hsm_flags $f "0x00000003"
3137 }
3138 run_test 55 "Truncate during an archive cancels it"
3139
3140 test_56() {
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         # Change metadata and sync to be sure we are not changing only
3153         # in memory.
3154         chmod 644 $f
3155         chgrp sys $f
3156         sync
3157         wait_request_state $fid ARCHIVE SUCCEED
3158
3159         check_hsm_flags $f "0x00000009"
3160 }
3161 run_test 56 "Setattr during an archive is ok"
3162
3163 test_57() {
3164         # Need one client for I/O, one for request
3165         needclients 2 || return 0
3166
3167         # test needs a running copytool
3168         copytool setup
3169
3170         mkdir -p $DIR/$tdir
3171         local f=$DIR/$tdir/test_archive_remote
3172         # Create a file on a remote node
3173         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
3174                 "count=2 conv=fsync"
3175
3176         # And archive it
3177         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
3178                 error "hsm_archive failed"
3179         local fid=$(path2fid $f)
3180         wait_request_state $fid ARCHIVE SUCCEED
3181
3182         # Release and implicit restore it
3183         do_node $CLIENT2 "$LFS hsm_release $f" ||
3184                 error "hsm_release failed"
3185         do_node $CLIENT2 "md5sum $f" ||
3186                 error "hsm_restore failed"
3187
3188         wait_request_state $fid RESTORE SUCCEED
3189 }
3190 run_test 57 "Archive a file with dirty cache on another node"
3191
3192 truncate_released_file() {
3193         local src_file=$1
3194         local trunc_to=$2
3195
3196         local sz=$(stat -c %s $src_file)
3197         local f=$DIR/$tdir/$tfile
3198         local fid=$(copy_file $1 $f)
3199         local ref=$f-ref
3200         cp $f $f-ref
3201
3202         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3203                 error "could not archive file"
3204         wait_request_state $fid ARCHIVE SUCCEED
3205
3206         $LFS hsm_release $f || error "could not release file"
3207
3208         $TRUNCATE $f $trunc_to || error "truncate failed"
3209         sync
3210
3211         local sz1=$(stat -c %s $f)
3212         [[ $sz1 == $trunc_to ]] ||
3213                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3214
3215         $LFS hsm_state $f
3216         check_hsm_flags $f "0x0000000b"
3217
3218         local state=$(get_request_state $fid RESTORE)
3219         [[ "$state" == "SUCCEED" ]] ||
3220                 error "truncate $sz does not trig restore, state = $state"
3221
3222         $TRUNCATE $ref $trunc_to
3223         cmp $ref $f || error "file data wrong after truncate"
3224
3225         rm -f $f $f-ref
3226 }
3227
3228 test_58() {
3229         # test needs a running copytool
3230         copytool setup
3231
3232         mkdir -p $DIR/$tdir
3233
3234         local sz=$(stat -c %s /etc/passwd)
3235
3236         echo "truncate up from $sz to $((sz*2))"
3237         truncate_released_file /etc/passwd $((sz*2))
3238
3239         echo "truncate down from $sz to $((sz/2))"
3240         truncate_released_file /etc/passwd $((sz/2))
3241
3242         echo "truncate to 0"
3243         truncate_released_file /etc/passwd 0
3244 }
3245 run_test 58 "Truncate a released file will trigger restore"
3246
3247 test_59() {
3248         local fid
3249         local server_version=$(lustre_version_code $SINGLEMDS)
3250         [[ $server_version -lt $(version_code 2.7.63) ]] &&
3251                 skip "Need MDS version at least 2.7.63" && return
3252
3253         copytool setup
3254         $MCREATE $DIR/$tfile || error "mcreate failed"
3255         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3256         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3257         fid=$(path2fid $DIR/$tfile)
3258         wait_request_state $fid ARCHIVE SUCCEED
3259         $LFS hsm_release $DIR/$tfile || error "release failed"
3260 }
3261 run_test 59 "Release stripeless file with non-zero size"
3262
3263 test_60() {
3264         # This test validates the fix for LU-4512. Ensure that the -u
3265         # option changes the progress reporting interval from the
3266         # default (30 seconds) to the user-specified interval.
3267         local f=$DIR/$tdir/$tfile
3268         local fid=$(create_file "$f" 1M 10)
3269
3270         local interval=5
3271         local progress_timeout=$((interval * 4))
3272         copytool setup -b 1 --update-interval $interval
3273
3274         local mdtidx=0
3275         local mdt=${MDT_PREFIX}${mdtidx}
3276         local mds=mds$((mdtidx + 1))
3277
3278         # Wait for copytool to register
3279         wait_update_facet $mds \
3280                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3281                 uuid 100 || error "coyptool failed to register with $mdt"
3282
3283         local start_at=$(date +%s)
3284         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3285                 error "could not archive file"
3286
3287         local agent=$(facet_active_host $SINGLEAGT)
3288         local logfile=$(copytool_logfile $SINGLEAGT)
3289
3290         wait_update $agent \
3291             "grep -o start.copy \"$logfile\"" "start copy" 100 ||
3292                 error "copytool failed to start"
3293
3294         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3295         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3296
3297         local RESULT
3298         local WAIT=0
3299         local sleep=1
3300
3301         echo -n "Expecting a progress update within $progress_timeout seconds... "
3302         while [ true ]; do
3303                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3304                 if [ -n "$RESULT" ] && [ "$RESULT" -gt 0 ]; then
3305                         echo "$RESULT bytes copied in $WAIT seconds."
3306                         break
3307                 elif [ $WAIT -ge $progress_timeout ]; then
3308                         error "Timed out waiting for progress update!"
3309                         break
3310                 fi
3311                 WAIT=$((WAIT + sleep))
3312                 sleep $sleep
3313         done
3314
3315         local finish_at=$(date +%s)
3316         local elapsed=$((finish_at - start_at))
3317
3318         # Ensure that the progress update occurred within the expected window.
3319         if [ $elapsed -lt $((interval - 1)) ]; then
3320                 error "Expected progress update after at least $interval seconds"
3321         fi
3322
3323         echo "Wait for on going archive hsm action to complete"
3324         wait_update $agent "grep -o copied \"$logfile\"" "copied" 10 ||
3325                 echo "File archiving not completed even after 10 secs"
3326 }
3327 run_test 60 "Changing progress update interval from default"
3328
3329 test_61() {
3330         # test needs a running copytool
3331         copytool setup
3332
3333         mkdir -p $DIR/$tdir
3334         local f=$DIR/$tdir/$tfile
3335         local fid=$(copy_file /etc/passwd $f)
3336         cdt_disable
3337         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3338         rm -f $f
3339         cdt_enable
3340         wait_request_state $fid ARCHIVE FAILED
3341 }
3342 run_test 61 "Waiting archive of a removed file should fail"
3343
3344 test_70() {
3345         # test needs a new running copytool
3346         stack_trap copytool_monitor_cleanup EXIT
3347         copytool_monitor_setup
3348         copytool setup --event-fifo "$HSMTOOL_MONITOR_DIR/fifo"
3349
3350         # Wait for the copytool to register.
3351         wait_update --verbose $(facet_active_host mds1) \
3352                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3353                 uuid 100 ||
3354                 error "copytool failed to register with MDT0000"
3355
3356         kill_copytools
3357         wait_copytools || error "Copytools failed to stop"
3358
3359         local REGISTER_EVENT
3360         local UNREGISTER_EVENT
3361         while read event; do
3362                 local parsed=$(parse_json_event "$event")
3363                 if [ -z "$parsed" ]; then
3364                         error "Copytool sent malformed event: $event"
3365                 fi
3366                 eval $parsed
3367
3368                 if [ $event_type == "REGISTER" ]; then
3369                         REGISTER_EVENT=$event
3370                 elif [ $event_type == "UNREGISTER" ]; then
3371                         UNREGISTER_EVENT=$event
3372                 fi
3373         done < <(echo $"$(get_copytool_event_log)")
3374
3375         if [ -z "$REGISTER_EVENT" ]; then
3376                 error "Copytool failed to send register event to FIFO"
3377         fi
3378
3379         if [ -z "$UNREGISTER_EVENT" ]; then
3380                 error "Copytool failed to send unregister event to FIFO"
3381         fi
3382
3383         echo "Register/Unregister events look OK."
3384 }
3385 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3386
3387 test_71() {
3388         # Bump progress interval for livelier events.
3389         local interval=5
3390
3391         # test needs a new running copytool
3392         stack_trap copytool_monitor_cleanup EXIT
3393         copytool_monitor_setup
3394         copytool setup --update-interval $interval --event-fifo \
3395                 "$HSMTOOL_MONITOR_DIR/fifo"
3396
3397         stack_trap "cdt_clear_no_retry" EXIT
3398         # Just start and stop the copytool to generate events.
3399         cdt_clear_no_retry
3400
3401         local f=$DIR/$tdir/$tfile
3402         local fid=$(create_small_file "$f")
3403
3404         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3405                 error "could not archive file"
3406         wait_request_state $fid ARCHIVE SUCCEED
3407
3408         local expected_fields="event_time data_fid source_fid"
3409         expected_fields+=" total_bytes current_bytes"
3410
3411         local -A events=(
3412                 [ARCHIVE_START]=false
3413                 [ARCHIVE_FINISH]=false
3414                 [ARCHIVE_RUNNING]=false
3415                 )
3416         while read event; do
3417                 # Make sure we're not getting anything from previous events.
3418                 for field in $expected_fields; do
3419                         unset $field
3420                 done
3421
3422                 local parsed=$(parse_json_event "$event")
3423                 if [ -z "$parsed" ]; then
3424                         error "Copytool sent malformed event: $event"
3425                 fi
3426                 eval $parsed
3427
3428                 events["$event_type"]=true
3429
3430                 [ "$event_type" != ARCHIVE_RUNNING ] && continue
3431
3432                 # Do some simple checking of the progress update events.
3433                 for expected_field in $expected_fields; do
3434                         if [ -z ${!expected_field+x} ]; then
3435                                 error "Missing $expected_field field in event"
3436                         fi
3437                 done
3438
3439                 [ $total_bytes -gt 0 ] || error "Expected total_bytes to be > 0"
3440
3441                 # These should be identical throughout an archive operation
3442                 [ $source_fid == $data_fid ] ||
3443                         error "Expected source_fid to equal data_fid"
3444         done < <(echo $"$(get_copytool_event_log)")
3445
3446         # Check we received every type of events we were expecting
3447         for event in "${!events[@]}"; do
3448                 ${events["$event"]} ||
3449                         error "Copytool failed to send '$event' event to FIFO"
3450         done
3451
3452         echo "Archive events look OK."
3453 }
3454 run_test 71 "Copytool logs JSON archive events to FIFO"
3455
3456 test_72() {
3457         # Bump progress interval for livelier events.
3458         local interval=5
3459
3460         # test needs a new running copytool
3461         stack_trap copytool_monitor_cleanup EXIT
3462         copytool_monitor_setup
3463         copytool setup --update-interval $interval --event-fifo \
3464                 "$HSMTOOL_MONITOR_DIR/fifo"
3465         local test_file=$HSMTOOL_MONITOR_DIR/file
3466
3467         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3468         cmd+="conv=fsync"
3469         do_facet $SINGLEAGT "$cmd" ||
3470                 error "cannot create $test_file on $SINGLEAGT"
3471         copy2archive $test_file $tdir/$tfile
3472
3473         mkdir -p $DIR/$tdir
3474         local f=$DIR/$tdir/$tfile
3475         copytool import $tdir/$tfile $f
3476         f=$DIR2/$tdir/$tfile
3477         echo "Verifying released state: "
3478         check_hsm_flags $f "0x0000000d"
3479
3480         local fid=$(path2fid $f)
3481         $LFS hsm_restore $f
3482         wait_request_state $fid RESTORE SUCCEED
3483
3484         local expected_fields="event_time data_fid source_fid"
3485         expected_fields+=" total_bytes current_bytes"
3486
3487         local START_EVENT
3488         local FINISH_EVENT
3489         while read event; do
3490                 # Make sure we're not getting anything from previous events.
3491                 for field in $expected_fields; do
3492                         unset $field
3493                 done
3494
3495                 local parsed=$(parse_json_event "$event")
3496                 if [ -z "$parsed" ]; then
3497                         error "Copytool sent malformed event: $event"
3498                 fi
3499                 eval $parsed
3500
3501                 if [ $event_type == "RESTORE_START" ]; then
3502                         START_EVENT=$event
3503                         if [ $source_fid != $data_fid ]; then
3504                                 error "source_fid should == data_fid at start"
3505                         fi
3506                         continue
3507                 elif [ $event_type == "RESTORE_FINISH" ]; then
3508                         FINISH_EVENT=$event
3509                         if [ $source_fid != $data_fid ]; then
3510                                 error "source_fid should == data_fid at finish"
3511                         fi
3512                         continue
3513                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3514                         continue
3515                 fi
3516
3517                 # Do some simple checking of the progress update events.
3518                 for expected_field in $expected_fields; do
3519                         if [ -z ${!expected_field+x} ]; then
3520                                 error "Missing $expected_field field in event"
3521                         fi
3522                 done
3523
3524                 if [ $total_bytes -eq 0 ]; then
3525                         error "Expected total_bytes to be > 0"
3526                 fi
3527
3528                 # When a restore starts out, the data fid is the same as the
3529                 # source fid. After the restore has gotten going, we learn
3530                 # the new data fid. Once the restore has finished, the source
3531                 # fid is set to the new data fid.
3532                 #
3533                 # We test this because some monitoring software may depend on
3534                 # this behavior. If it changes, then the consumers of these
3535                 # events may need to be modified.
3536                 if [ $source_fid == $data_fid ]; then
3537                         error "source_fid should != data_fid during restore"
3538                 fi
3539         done < <(echo $"$(get_copytool_event_log)")
3540
3541         if [ -z "$START_EVENT" ]; then
3542                 error "Copytool failed to send restore start event to FIFO"
3543         fi
3544
3545         if [ -z "$FINISH_EVENT" ]; then
3546                 error "Copytool failed to send restore finish event to FIFO"
3547         fi
3548
3549         echo "Restore events look OK."
3550 }
3551 run_test 72 "Copytool logs JSON restore events to FIFO"
3552
3553 test_90() {
3554         file_count=51 # Max number of files constrained by LNET message size
3555         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3556         local f=$DIR/$tdir/$tfile
3557         local FILELIST=/tmp/filelist.txt
3558         local i=""
3559
3560         rm -f $FILELIST
3561         for i in $(seq 1 $file_count); do
3562                 fid=$(copy_file /etc/hosts $f.$i)
3563                 echo $f.$i >> $FILELIST
3564         done
3565
3566         copytool setup
3567         # to be sure wait_all_done will not be mislead by previous tests
3568         cdt_purge
3569         wait_for_grace_delay
3570         $LFS hsm_archive --filelist $FILELIST ||
3571                 error "cannot archive a file list"
3572         wait_all_done 100
3573         $LFS hsm_release --filelist $FILELIST ||
3574                 error "cannot release a file list"
3575         $LFS hsm_restore --filelist $FILELIST ||
3576                 error "cannot restore a file list"
3577         wait_all_done 100
3578 }
3579 run_test 90 "Archive/restore a file list"
3580
3581 double_verify_reset_hsm_param() {
3582         local p=$1
3583         echo "Testing $HSM_PARAM.$p"
3584         local val=$(get_hsm_param $p)
3585         local save=$val
3586         local val2=$(($val * 2))
3587         set_hsm_param $p $val2
3588         val=$(get_hsm_param $p)
3589         [[ $val == $val2 ]] ||
3590                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3591         echo "Set $p to 0 must failed"
3592         set_hsm_param $p 0
3593         local rc=$?
3594         # restore value
3595         set_hsm_param $p $save
3596
3597         if [[ $rc == 0 ]]; then
3598                 error "we must not be able to set $HSM_PARAM.$p to 0"
3599         fi
3600 }
3601
3602 test_100() {
3603         double_verify_reset_hsm_param loop_period
3604         double_verify_reset_hsm_param grace_delay
3605         double_verify_reset_hsm_param active_request_timeout
3606         double_verify_reset_hsm_param max_requests
3607         double_verify_reset_hsm_param default_archive_id
3608 }
3609 run_test 100 "Set coordinator /proc tunables"
3610
3611 test_102() {
3612         cdt_disable
3613         cdt_enable
3614         cdt_restart
3615 }
3616 run_test 102 "Verify coordinator control"
3617
3618 test_103() {
3619         # test needs a running copytool
3620         copytool setup
3621
3622         local i=""
3623         local fid=""
3624
3625         mkdir -p $DIR/$tdir
3626         for i in $(seq 1 20); do
3627                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3628         done
3629         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3630
3631         cdt_purge
3632
3633         echo "Current requests"
3634         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3635                         $HSM_PARAM.actions |\
3636                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3637
3638         [[ -z "$res" ]] || error "Some request have not been canceled"
3639 }
3640 run_test 103 "Purge all requests"
3641
3642 DATA=CEA
3643 DATAHEX='[434541]'
3644 test_104() {
3645         local f=$DIR/$tdir/$tfile
3646         local fid=$(create_empty_file "$f")
3647
3648         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3649         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3650                         $HSM_PARAM.actions |\
3651                         grep $fid | cut -f16 -d=")
3652
3653         [[ "$data1" == "$DATAHEX" ]] ||
3654                 error "Data field in records is ($data1) and not ($DATAHEX)"
3655
3656         cdt_purge
3657 }
3658 run_test 104 "Copy tool data field"
3659
3660 test_105() {
3661         local max_requests=$(get_hsm_param max_requests)
3662         mkdir -p $DIR/$tdir
3663         local i=""
3664
3665         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3666         set_hsm_param max_requests 300
3667
3668         cdt_disable
3669         for i in $(seq -w 1 10); do
3670                 cp /etc/passwd $DIR/$tdir/$i
3671                 $LFS hsm_archive $DIR/$tdir/$i
3672         done
3673         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3674                         $HSM_PARAM.actions |\
3675                         grep WAITING | wc -l")
3676         cdt_restart
3677
3678         cdt_disable
3679         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3680                         $HSM_PARAM.actions |\
3681                         grep WAITING | wc -l")
3682         cdt_enable
3683         cdt_purge
3684         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3685                 error "Requests count after shutdown $reqcnt2 != "\
3686                       "before shutdown $reqcnt1"
3687 }
3688 run_test 105 "Restart of coordinator"
3689
3690 test_106() {
3691         # test needs a running copytool
3692         copytool setup
3693
3694         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3695
3696         check_agent_registered $uuid
3697
3698         search_copytools || error "No copytool found"
3699
3700         kill_copytools
3701         wait_copytools || error "Copytool failed to stop"
3702
3703         check_agent_unregistered $uuid
3704
3705         copytool setup
3706         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3707         check_agent_registered $uuid
3708 }
3709 run_test 106 "Copytool register/unregister"
3710
3711 test_107() {
3712         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
3713
3714         # test needs a running copytool
3715         copytool setup
3716         # create and archive file
3717         mkdir -p $DIR/$tdir
3718         local f1=$DIR/$tdir/$tfile
3719         local fid=$(copy_file /etc/passwd $f1)
3720         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3721         wait_request_state $fid ARCHIVE SUCCEED
3722         # shutdown and restart MDS
3723         fail $SINGLEMDS
3724         # check the copytool still gets messages from MDT
3725         local f2=$DIR/$tdir/2
3726         local fid=$(copy_file /etc/passwd $f2)
3727         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3728         # main check of this sanity: this request MUST succeed
3729         wait_request_state $fid ARCHIVE SUCCEED
3730 }
3731 run_test 107 "Copytool re-register after MDS restart"
3732
3733 policy_set_and_test()
3734 {
3735         local change="$1"
3736         local target="$2"
3737         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3738         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3739         [[ "$policy" == "$target" ]] ||
3740                 error "Wrong policy after '$change': '$policy' != '$target'"
3741 }
3742
3743 test_109() {
3744         # to force default policy setting if error
3745         CDT_POLICY_HAD_CHANGED=true
3746
3747         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3748         local default="NonBlockingRestore [NoRetryAction]"
3749         [[ "$policy" == "$default" ]] ||
3750                 error "default policy has changed,"\
3751                       " '$policy' != '$default' update the test"
3752         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3753         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3754         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3755         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3756         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
3757         # useless bacause we know but safer for futur changes to use real value
3758         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3759         echo "Next set_param must failed"
3760         policy_set_and_test "wrong" "$policy"
3761
3762         # return to default
3763         echo "Back to default policy"
3764         cdt_set_sanity_policy
3765 }
3766 run_test 109 "Policy display/change"
3767
3768 test_110a() {
3769         # test needs a running copytool
3770         copytool setup
3771
3772         mkdir -p $DIR/$tdir
3773
3774         copy2archive /etc/passwd $tdir/$tfile
3775
3776         local f=$DIR/$tdir/$tfile
3777         copytool import $tdir/$tfile $f
3778         local fid=$(path2fid $f)
3779
3780         cdt_set_non_blocking_restore
3781         md5sum $f
3782         local st=$?
3783
3784         # cleanup
3785         wait_request_state $fid RESTORE SUCCEED
3786         cdt_clear_non_blocking_restore
3787
3788         # Test result
3789         [[ $st == 1 ]] ||
3790                 error "md5sum returns $st != 1, "\
3791                         "should also perror ENODATA (No data available)"
3792 }
3793 run_test 110a "Non blocking restore policy (import case)"
3794
3795 test_110b() {
3796         # test needs a running copytool
3797         copytool setup
3798
3799         mkdir -p $DIR/$tdir
3800         local f=$DIR/$tdir/$tfile
3801         local fid=$(copy_file /etc/passwd $f)
3802         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3803         wait_request_state $fid ARCHIVE SUCCEED
3804         $LFS hsm_release $f
3805
3806         cdt_set_non_blocking_restore
3807         md5sum $f
3808         local st=$?
3809
3810         # cleanup
3811         wait_request_state $fid RESTORE SUCCEED
3812         cdt_clear_non_blocking_restore
3813
3814         # Test result
3815         [[ $st == 1 ]] ||
3816                 error "md5sum returns $st != 1, "\
3817                         "should also perror ENODATA (No data available)"
3818 }
3819 run_test 110b "Non blocking restore policy (release case)"
3820
3821 test_111a() {
3822         # test needs a running copytool
3823         copytool setup
3824
3825         mkdir -p $DIR/$tdir
3826         copy2archive /etc/passwd $tdir/$tfile
3827
3828         local f=$DIR/$tdir/$tfile
3829
3830         copytool import $tdir/$tfile $f
3831         local fid=$(path2fid $f)
3832
3833         cdt_set_no_retry
3834
3835         copytool_remove_backend $fid
3836
3837         $LFS hsm_restore $f
3838         wait_request_state $fid RESTORE FAILED
3839         local st=$?
3840
3841         # cleanup
3842         cdt_clear_no_retry
3843
3844         # Test result
3845         [[ $st == 0 ]] || error "Restore does not failed"
3846 }
3847 run_test 111a "No retry policy (import case), restore will error"\
3848               " (No such file or directory)"
3849
3850 test_111b() {
3851         # test needs a running copytool
3852         copytool setup
3853
3854         mkdir -p $DIR/$tdir
3855         local f=$DIR/$tdir/$tfile
3856         local fid=$(copy_file /etc/passwd $f)
3857         stack_trap cdt_clear_no_retry EXIT
3858         cdt_set_no_retry
3859         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3860         wait_request_state $fid ARCHIVE SUCCEED
3861         $LFS hsm_release $f
3862
3863         copytool_remove_backend $fid
3864
3865         $LFS hsm_restore $f
3866         wait_request_state $fid RESTORE FAILED
3867         local st=$?
3868
3869         # Test result
3870         [[ $st == 0 ]] || error "Restore does not failed"
3871 }
3872 run_test 111b "No retry policy (release case), restore will error"\
3873               " (No such file or directory)"
3874
3875 test_112() {
3876         # test needs a running copytool
3877         copytool setup
3878
3879         mkdir -p $DIR/$tdir
3880         local f=$DIR/$tdir/$tfile
3881         local fid=$(copy_file /etc/passwd $f)
3882         cdt_disable
3883         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3884         local l=$($LFS hsm_action $f)
3885         echo $l
3886         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
3887
3888         cdt_enable
3889         wait_request_state $fid ARCHIVE SUCCEED
3890
3891         # Test result
3892         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
3893 }
3894 run_test 112 "State of recorded request"
3895
3896 test_200() {
3897         local f=$DIR/$tdir/$tfile
3898         local fid=$(create_empty_file "$f")
3899
3900         copytool setup
3901
3902         # Prevent archive from completing
3903         copytool_suspend
3904
3905         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3906         # wait archive to register at CDT
3907         wait_request_state $fid ARCHIVE STARTED
3908
3909         # Cancel the archive
3910         $LFS hsm_cancel "$f"
3911
3912         wait_request_state $fid ARCHIVE CANCELED
3913
3914         copytool_continue
3915         wait_request_state $fid CANCEL SUCCEED
3916 }
3917 run_test 200 "Register/Cancel archive"
3918
3919 test_201() {
3920         # test needs a running copytool
3921         copytool setup
3922
3923         local f=$DIR/$tdir/$tfile
3924         create_archive_file $tdir/$tfile
3925         copytool import $tdir/$tfile $f
3926         local fid=$(path2fid $f)
3927
3928         # test with cdt on is made in test_222
3929         cdt_disable
3930         $LFS hsm_restore $f
3931         # wait restore to register at CDT
3932         wait_request_state $fid RESTORE WAITING
3933         $LFS hsm_cancel $f
3934         cdt_enable
3935         wait_request_state $fid RESTORE CANCELED
3936         wait_request_state $fid CANCEL SUCCEED
3937 }
3938 run_test 201 "Register/Cancel restore"
3939
3940 test_202() {
3941         local f=$DIR/$tdir/$tfile
3942         local fid=$(create_empty_file "$f")
3943
3944         # test needs a running copytool
3945         copytool setup
3946
3947         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3948         wait_request_state $fid ARCHIVE SUCCEED
3949
3950         copytool_suspend
3951         $LFS hsm_remove $f
3952         # wait remove to register at CDT
3953         wait_request_state $fid REMOVE STARTED
3954         $LFS hsm_cancel $f
3955
3956         wait_request_state $fid REMOVE CANCELED
3957 }
3958 run_test 202 "Register/Cancel remove"
3959
3960 test_220A() { # was test_220
3961         # test needs a running copytool
3962         copytool setup
3963
3964         mkdir -p $DIR/$tdir
3965
3966         local f=$DIR/$tdir/$tfile
3967         local fid=$(copy_file /etc/passwd $f)
3968
3969         changelog_register
3970
3971         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3972         wait_request_state $fid ARCHIVE SUCCEED
3973
3974         changelog_find -type HSM -target-fid $fid -flags 0x0 ||
3975                 error "The expected changelog was not emitted"
3976 }
3977 run_test 220A "Changelog for archive"
3978
3979 test_220a() {
3980         # test needs a running copytool
3981         copytool setup
3982
3983         mkdir -p $DIR/$tdir
3984
3985         local f=$DIR/$tdir/$tfile
3986         local fid=$(copy_file /etc/passwd $f)
3987
3988         changelog_register
3989
3990         # block copytool operations to allow for HSM request to be
3991         # submitted and file be unlinked (CDT will find object removed)
3992         copytool_suspend
3993
3994         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3995
3996         # wait request to reach CT
3997         wait_request_state $fid ARCHIVE STARTED
3998
3999         rm -f $f
4000
4001         copytool_continue
4002
4003         wait_request_state $fid ARCHIVE FAILED
4004
4005         # HE_ARCHIVE|ENOENT
4006         changelog_find -type HSM -target-fid $fid -flags 0x2 ||
4007                 error "The expected changelog was not emitted"
4008 }
4009 run_test 220a "Changelog for failed archive"
4010
4011 test_221() {
4012         local f=$DIR/$tdir/$tfile
4013         local fid=$(create_empty_file "$f")
4014
4015         copytool setup -b 1
4016         changelog_register
4017
4018         # Prevent archive from completing
4019         copytool_suspend
4020         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4021         wait_request_state $fid ARCHIVE STARTED
4022
4023         $LFS hsm_cancel $f
4024         wait_request_state $fid ARCHIVE CANCELED
4025
4026         copytool_continue
4027         wait_request_state $fid CANCEL SUCCEED
4028
4029         changelog_find -type HSM -target-fid $fid -flags 0x7d ||
4030                 error "The expected changelog was not emitted"
4031 }
4032 run_test 221 "Changelog for archive canceled"
4033
4034 test_222a() {
4035         # test needs a running copytool
4036         copytool setup
4037
4038         mkdir -p $DIR/$tdir
4039         copy2archive /etc/passwd $tdir/$tfile
4040
4041         local f=$DIR/$tdir/$tfile
4042         copytool import $tdir/$tfile $f
4043         local fid=$(path2fid $f)
4044
4045         changelog_register
4046
4047         $LFS hsm_restore $f
4048         wait_request_state $fid RESTORE SUCCEED
4049
4050         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4051                 error "The expected changelog was not emitted"
4052 }
4053 run_test 222a "Changelog for explicit restore"
4054
4055 test_222b() {
4056         # test needs a running copytool
4057         copytool setup
4058
4059         mkdir -p $DIR/$tdir
4060         local f=$DIR/$tdir/$tfile
4061         local fid=$(copy_file /etc/passwd $f)
4062
4063         changelog_register
4064         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4065         wait_request_state $fid ARCHIVE SUCCEED
4066         $LFS hsm_release $f
4067
4068         md5sum $f
4069
4070         wait_request_state $fid RESTORE SUCCEED
4071
4072         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4073                 error "The expected changelog was not emitted"
4074 }
4075 run_test 222b "Changelog for implicit restore"
4076
4077 test_222c() {
4078         # test needs a running copytool
4079         copytool setup
4080
4081         mkdir -p $DIR/$tdir
4082         copy2archive /etc/passwd $tdir/$tfile
4083
4084         local f=$DIR/$tdir/$tfile
4085         copytool import $tdir/$tfile $f
4086         local fid=$(path2fid $f)
4087
4088         changelog_register
4089
4090         # block copytool operations to allow for HSM request to be
4091         # submitted and file be unlinked (CDT will find object removed)
4092         copytool_suspend
4093
4094         $LFS hsm_restore $f
4095
4096         # wait request to reach CT
4097         wait_request_state $fid RESTORE STARTED
4098
4099         rm -f $f
4100
4101         copytool_continue
4102
4103         wait_request_state $fid RESTORE FAILED
4104
4105         # HE_RESTORE|ENOENT
4106         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
4107                 error "The expected changelog was not emitted"
4108 }
4109 run_test 222c "Changelog for failed explicit restore"
4110
4111 test_222d() {
4112         # test needs a running copytool
4113         copytool setup
4114
4115         mkdir -p $DIR/$tdir
4116         local f=$DIR/$tdir/$tfile
4117         local fid=$(copy_file /etc/passwd $f)
4118
4119         changelog_register
4120         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4121         wait_request_state $fid ARCHIVE SUCCEED
4122         $LFS hsm_release $f
4123
4124         copytool_remove_backend $fid
4125         md5sum $f
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 222d "Changelog for failed implicit restore"
4134
4135 test_223a() {
4136         # test needs a running copytool
4137         copytool setup -b 1
4138
4139         local f=$DIR/$tdir/$tfile
4140         create_archive_file $tdir/$tfile
4141
4142         changelog_register
4143
4144         copytool import $tdir/$tfile $f
4145         local fid=$(path2fid $f)
4146
4147         $LFS hsm_restore $f
4148         wait_request_state $fid RESTORE STARTED
4149         $LFS hsm_cancel $f
4150         wait_request_state $fid RESTORE CANCELED
4151         wait_request_state $fid CANCEL SUCCEED
4152
4153         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4154                 error "The expected changelog was not emitted"
4155 }
4156 run_test 223a "Changelog for restore canceled (import case)"
4157
4158 test_223b() {
4159         local f=$DIR/$tdir/$tfile
4160         local fid=$(create_empty_file "$f")
4161
4162         copytool setup -b 1
4163         changelog_register
4164
4165         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4166         wait_request_state $fid ARCHIVE SUCCEED
4167         $LFS hsm_release $f
4168
4169         # Prevent restore from completing
4170         copytool_suspend
4171         $LFS hsm_restore $f
4172         wait_request_state $fid RESTORE STARTED
4173
4174         $LFS hsm_cancel $f
4175         wait_request_state $fid RESTORE CANCELED
4176
4177         copytool_continue
4178         wait_request_state $fid CANCEL SUCCEED
4179
4180         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4181                 error "The expected changelog was not emitted"
4182 }
4183 run_test 223b "Changelog for restore canceled (release case)"
4184
4185 test_224A() { # was test_224
4186         # test needs a running copytool
4187         copytool setup
4188
4189         mkdir -p $DIR/$tdir
4190
4191         local f=$DIR/$tdir/$tfile
4192         local fid=$(copy_file /etc/passwd $f)
4193
4194         changelog_register
4195         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4196         wait_request_state $fid ARCHIVE SUCCEED
4197
4198         $LFS hsm_remove $f
4199         wait_request_state $fid REMOVE SUCCEED
4200
4201         changelog_find -type HSM -target-fid $fid -flags 0x200 ||
4202                 error "The expected changelog was not emitted"
4203 }
4204 run_test 224A "Changelog for remove"
4205
4206 test_224a() {
4207         # test needs a running copytool
4208         copytool setup
4209
4210         mkdir -p $DIR/$tdir
4211
4212         local f=$DIR/$tdir/$tfile
4213         local fid=$(copy_file /etc/passwd $f)
4214
4215         changelog_register
4216         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4217         wait_request_state $fid ARCHIVE SUCCEED
4218
4219         copytool_remove_backend $fid
4220
4221         # block copytool operations to allow for HSM request to be
4222         # submitted and file be unlinked (CDT will find object removed)
4223         copytool_suspend
4224
4225         $LFS hsm_remove $f
4226
4227         # wait for request to reach CT
4228         wait_request_state $fid REMOVE STARTED
4229
4230         rm -f $f
4231
4232         copytool_continue
4233
4234         wait_request_state $fid REMOVE FAILED
4235
4236         # HE_REMOVE|ENOENT=0x202
4237         changelog_find -type HSM -target-fid $fid -flags 0x202 ||
4238                 error "The expected changelog was not emitted"
4239 }
4240 run_test 224a "Changelog for failed remove"
4241
4242 test_225() {
4243         # test is not usable because remove request is too fast
4244         # so it is always finished before cancel can be done ...
4245         echo "Test disabled"
4246         return 0
4247
4248         # test needs a running copytool
4249         copytool setup
4250
4251         local f=$DIR/$tdir/$tfile
4252         local fid=$(create_empty_file "$f")
4253
4254         changelog_register
4255         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4256         wait_request_state $fid ARCHIVE SUCCEED
4257
4258         # Prevent restore from completing
4259         copytool_suspend
4260         $LFS hsm_remove $f
4261
4262         $LFS hsm_cancel $f
4263         wait_request_state $fid REMOVE CANCELED
4264
4265         copytool_continue
4266         wait_request_state $fid CANCEL SUCCEED
4267
4268         changelog_find -type HSM -target-fid $fid -flags 0x27d
4269                 error "The expected changelog was not emitted"
4270 }
4271 run_test 225 "Changelog for remove canceled"
4272
4273 test_226() {
4274         # test needs a running copytool
4275         copytool setup
4276
4277         mkdir -p $DIR/$tdir
4278
4279         local f1=$DIR/$tdir/$tfile-1
4280         local f2=$DIR/$tdir/$tfile-2
4281         local f3=$DIR/$tdir/$tfile-3
4282         local fid1=$(copy_file /etc/passwd $f1)
4283         local fid2=$(copy_file /etc/passwd $f2)
4284         copy_file /etc/passwd $f3
4285
4286         changelog_register
4287         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4288         wait_request_state $fid1 ARCHIVE SUCCEED
4289
4290         $LFS hsm_archive $f2
4291         wait_request_state $fid2 ARCHIVE SUCCEED
4292
4293         rm $f1 || error "rm $f1 failed"
4294
4295         changelog_dump
4296         changelog_find -type UNLNK -target-fid $fid1 -flags 0x3 ||
4297                 error "The expected changelog was not emitted"
4298
4299         mv $f3 $f2 || error "mv $f3 $f2 failed"
4300
4301         changelog_find -type RENME -target-fid $fid2 -flags 0x3 ||
4302                 error "The expected changelog was not emitted"
4303 }
4304 run_test 226 "changelog for last rm/mv with exiting archive"
4305
4306 # This is just a utility function to clarify what test_227 does
4307 __test_227()
4308 {
4309         local target=0x280
4310
4311         "$LFS" "$action" --$flag "$file" ||
4312                 error "Cannot ${action#hsm_} $flag on '$file'"
4313
4314         # Only one changelog should be produced
4315         local entries="$(changelog_find -type HSM -target-fid $fid)"
4316         [ $(wc -l <<< "$entries") -eq $((++count)) ] ||
4317                 error "lfs $action --$flag '$file' produced more than one" \
4318                       "changelog record"
4319
4320         # Parse the last changelog record
4321         local entry="$(tail -n 1 <<< "$entries")"
4322         eval local -A changelog=$(changelog2array $entry)
4323
4324         # Also check the flags match what is expected
4325         [[ ${changelog[flags]} == $target ]] ||
4326                 error "Changelog flag is '${changelog[flags]}', not $target"
4327 }
4328
4329 test_227() {
4330         local file="$DIR/$tdir/$tfile"
4331         local fid=$(create_empty_file "$file")
4332         local count=0
4333
4334         changelog_register
4335
4336         for flag in norelease noarchive exists archived lost; do
4337                 if [ "$flag" == lost ]; then
4338                         # The flag "lost" only works on an archived file
4339                         "$LFS" hsm_set --archived "$file"
4340                         ((count++))
4341                 fi
4342
4343                 action="hsm_set" __test_227
4344                 action="hsm_clear" __test_227
4345         done
4346 }
4347 run_test 227 "changelog when explicit setting of HSM flags"
4348
4349 test_228() {
4350         # test needs a running copytool
4351         copytool setup
4352
4353         local fid=$(create_small_sync_file $DIR/$tfile)
4354         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4355         wait_request_state $fid ARCHIVE SUCCEED
4356
4357         $LFS hsm_release $DIR/$tfile
4358         check_hsm_flags $DIR/$tfile "0x0000000d"
4359
4360         filefrag $DIR/$tfile | grep " 1 extent found" ||
4361                 error "filefrag on released file must return only one extent"
4362
4363         # only newer versions of cp detect sparse files by stat/FIEMAP
4364         # (LU-2580)
4365         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4366                 error "copying $DIR/$tfile"
4367         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4368
4369         $LFS hsm_release $DIR/$tfile
4370         check_hsm_flags $DIR/$tfile "0x0000000d"
4371
4372         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4373
4374         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4375                 error "tar failed"
4376         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4377                 error "comparing untarred $DIR/$tfile"
4378
4379         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4380                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4381 }
4382 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4383
4384 test_250() {
4385         # test needs a running copytool
4386         copytool setup
4387
4388         mkdir -p $DIR/$tdir
4389         local maxrequest=$(get_hsm_param max_requests)
4390         local rqcnt=$(($maxrequest * 3))
4391         local i=""
4392
4393         cdt_disable
4394         for i in $(seq -w 1 $rqcnt); do
4395                 rm -f $DIR/$tdir/$i
4396                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
4397         done
4398         # we do it in 2 steps, so all requests arrive at the same time
4399         for i in $(seq -w 1 $rqcnt); do
4400                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
4401         done
4402         cdt_enable
4403         local cnt=$rqcnt
4404         local wt=$rqcnt
4405         while [[ $cnt != 0 || $wt != 0 ]]; do
4406                 sleep 1
4407                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4408                         $HSM_PARAM.actions |\
4409                         grep STARTED | grep -v CANCEL | wc -l")
4410                 [[ $cnt -le $maxrequest ]] ||
4411                         error "$cnt > $maxrequest too many started requests"
4412                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4413                         $HSM_PARAM.actions |\
4414                         grep WAITING | wc -l")
4415                 echo "max=$maxrequest started=$cnt waiting=$wt"
4416         done
4417 }
4418 run_test 250 "Coordinator max request"
4419
4420 test_251() {
4421         local f=$DIR/$tdir/$tfile
4422         local fid=$(create_empty_file "$f")
4423
4424         cdt_disable
4425         # to have a short test
4426         local old_to=$(get_hsm_param active_request_timeout)
4427         set_hsm_param active_request_timeout 1
4428         # to be sure the cdt will wake up frequently so
4429         # it will be able to cancel the "old" request
4430         local old_loop=$(get_hsm_param loop_period)
4431         set_hsm_param loop_period 1
4432         cdt_enable
4433
4434         copytool setup
4435
4436         # Prevent archive from completing
4437         copytool_suspend
4438         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4439         wait_request_state $fid ARCHIVE STARTED
4440
4441         # Let the request timeout
4442         wait_request_state $fid ARCHIVE CANCELED
4443
4444         set_hsm_param active_request_timeout $old_to
4445         set_hsm_param loop_period $old_loop
4446 }
4447 run_test 251 "Coordinator request timeout"
4448
4449 test_252() {
4450         local f=$DIR/$tdir/$tfile
4451         local fid=$(create_empty_file "$f")
4452
4453         # to have a short test
4454         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4455         set_hsm_param loop_period 1
4456
4457         copytool setup
4458
4459         # Prevent archive from completing
4460         copytool_suspend
4461         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4462         wait_request_state $fid ARCHIVE STARTED
4463         rm -f "$f"
4464
4465         stack_trap "set_hsm_param active_request_timeout \
4466                     $(get_hsm_param active_request_timeout)" EXIT
4467         set_hsm_param active_request_timeout 1
4468
4469         wait_request_state $fid ARCHIVE CANCELED
4470         copytool_continue
4471 }
4472 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4473
4474 test_253() {
4475         local rc
4476         # test needs a running copytool
4477         copytool setup
4478
4479         mkdir -p $DIR/$tdir
4480         local f=$DIR/$tdir/$tfile
4481
4482         dd if=/dev/zero of=$f bs=1MB count=10
4483         local fid=$(path2fid $f)
4484
4485         $LFS hsm_archive $f || error "could not archive file"
4486         wait_request_state $fid ARCHIVE SUCCEED
4487
4488         # clear locks to discard inode data
4489         cancel_lru_locks osc
4490
4491         #define OBD_FAIL_MDC_MERGE              0x807
4492         $LCTL set_param fail_loc=0x807
4493
4494         #expect error here, instead of release with wrong size
4495         $LFS hsm_release $f
4496         rc=$?
4497         if ((rc == 0)); then
4498                 file_size=$(stat -c '%s' $f)
4499                 if ((file_size != 10485760)); then
4500                         error "Wrong file size after hsm_release"
4501                 fi
4502         else
4503                 echo "could not release file"
4504         fi
4505 }
4506 run_test 253 "Check for wrong file size after release"
4507
4508 test_254a()
4509 {
4510         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4511                 skip "need MDS version at least 2.10.56" && return
4512
4513         # Check that the counters are initialized to 0
4514         local count
4515         for request_type in archive restore remove; do
4516                 count="$(get_hsm_param ${request_type}_count)" ||
4517                         error "Reading ${request_type}_count failed with $?"
4518
4519                 [ "$count" -eq 0 ] ||
4520                         error "Expected ${request_type}_count to be " \
4521                               "0 != '$count'"
4522         done
4523 }
4524 run_test 254a "Request counters are initialized to zero"
4525
4526 test_254b()
4527 {
4528         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.56) ] &&
4529                 skip "need MDS version at least 2.10.56" && return
4530
4531         # The number of request to launch (at least 32)
4532         local request_count=$((RANDOM % 32 + 32))
4533         printf "Will launch %i requests of each type\n" "$request_count"
4534
4535         # Launch a copytool to process requests
4536         copytool setup
4537
4538         # Set hsm.max_requests to allow starting all requests at the same time
4539         stack_trap \
4540                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4541         set_hsm_param max_requests "$request_count"
4542
4543         local timeout
4544         local count
4545         for request_type in archive restore remove; do
4546                 printf "Checking %s requests\n" "${request_type}"
4547                 # Suspend the copytool to give us time to read the proc files
4548                 copytool_suspend
4549
4550                 for ((i = 0; i < $request_count; i++)); do
4551                         case $request_type in
4552                         archive)
4553                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4554                                         >/dev/null 2>&1
4555                                 ;;
4556                         restore)
4557                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4558                                 ;;
4559                         esac
4560                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4561                 done
4562
4563                 # Give the coordinator 10 seconds to start every request
4564                 timeout=10
4565                 while get_hsm_param actions | grep -q WAITING; do
4566                         sleep 1
4567                         let timeout-=1
4568                         [ $timeout -gt 0 ] ||
4569                                 error "${request_type^} requests took too " \
4570                                       "long to start"
4571                 done
4572
4573                 count="$(get_hsm_param ${request_type}_count)"
4574                 [ "$count" -eq "$request_count" ] ||
4575                         error "Expected '$request_count' (!= '$count') " \
4576                               "active $request_type requests"
4577
4578                 # Let the copytool process the requests
4579                 copytool_continue
4580                 # Give it 10 seconds maximum
4581                 timeout=10
4582                 while get_hsm_param actions | grep -q STARTED; do
4583                         sleep 1
4584                         let timeout-=1
4585                         [ $timeout -gt 0 ] ||
4586                                 error "${request_type^} requests took too " \
4587                                       "long to complete"
4588                 done
4589
4590                 count="$(get_hsm_param ${request_type}_count)"
4591                 [ "$count" -eq 0 ] ||
4592                         error "Expected 0 (!= '$count') " \
4593                               "active $request_type requests"
4594         done
4595 }
4596 run_test 254b "Request counters are correctly incremented and decremented"
4597
4598 test_300() {
4599         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4600
4601         # the only way to test ondisk conf is to restart MDS ...
4602         echo "Stop coordinator and remove coordinator state at mount"
4603         # stop coordinator
4604         cdt_shutdown
4605         # clean on disk conf set by default
4606         cdt_clear_mount_state
4607         cdt_check_state stopped
4608
4609         # check cdt still off after umount/remount
4610         fail $SINGLEMDS
4611         cdt_check_state stopped
4612
4613         echo "Set coordinator start at mount, and start coordinator"
4614         cdt_set_mount_state enabled
4615
4616         # check cdt is on
4617         cdt_check_state enabled
4618
4619         # check cdt still on after umount/remount
4620         fail $SINGLEMDS
4621         cdt_check_state enabled
4622
4623         # we are back to original state (cdt started at mount)
4624 }
4625 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4626
4627 test_301() {
4628         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4629
4630         local ai=$(get_hsm_param default_archive_id)
4631         local new=$((ai + 1))
4632
4633         set_hsm_param default_archive_id $new -P
4634         fail $SINGLEMDS
4635         local res=$(get_hsm_param default_archive_id)
4636
4637         # clear value
4638         set_hsm_param default_archive_id "" "-P -d"
4639
4640         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4641 }
4642 run_test 301 "HSM tunnable are persistent"
4643
4644 test_302() {
4645         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4646
4647         local ai=$(get_hsm_param default_archive_id)
4648         local new=$((ai + 1))
4649
4650         # stop coordinator
4651         cdt_shutdown
4652
4653         set_hsm_param default_archive_id $new -P
4654
4655         local mdtno
4656         for mdtno in $(seq 1 $MDSCOUNT); do
4657                 fail mds${mdtno}
4658         done
4659
4660         # check cdt is on
4661         cdt_check_state enabled
4662
4663         local res=$(get_hsm_param default_archive_id)
4664
4665         # clear value
4666         set_hsm_param default_archive_id "" "-P -d"
4667
4668         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4669 }
4670 run_test 302 "HSM tunnable are persistent when CDT is off"
4671
4672 test_400() {
4673         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4674
4675         copytool setup
4676
4677         mkdir -p $DIR/$tdir
4678
4679         local dir_mdt0=$DIR/$tdir/mdt0
4680         local dir_mdt1=$DIR/$tdir/mdt1
4681
4682         # create 1 dir per MDT
4683         stack_trap "rm -rf $dir_mdt0" EXIT
4684         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4685         stack_trap "rm -rf $dir_mdt1" EXIT
4686         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4687
4688         # create 1 file in each MDT
4689         local fid1=$(create_small_file $dir_mdt0/$tfile)
4690         local fid2=$(create_small_file $dir_mdt1/$tfile)
4691
4692         # check that hsm request on mdt0 is sent to the right MDS
4693         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4694         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4695                 echo "archive successful on mdt0"
4696
4697         # check that hsm request on mdt1 is sent to the right MDS
4698         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
4699         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4700                 echo "archive successful on mdt1"
4701 }
4702 run_test 400 "Single request is sent to the right MDT"
4703
4704 test_401() {
4705         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4706
4707         copytool setup
4708
4709         mkdir -p $DIR/$tdir
4710
4711         local dir_mdt0=$DIR/$tdir/mdt0
4712         local dir_mdt1=$DIR/$tdir/mdt1
4713
4714         # create 1 dir per MDT
4715         stack_trap "rm -rf $dir_mdt0" EXIT
4716         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4717         stack_trap "rm -rf $dir_mdt1" EXIT
4718         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4719
4720         # create 1 file in each MDT
4721         local fid1=$(create_small_file $dir_mdt0/$tfile)
4722         local fid2=$(create_small_file $dir_mdt1/$tfile)
4723
4724         # check that compound requests are shunt to the rights MDTs
4725         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
4726                 error "lfs hsm_archive"
4727         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4728                 echo "archive successful on mdt0"
4729         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4730                 echo "archive successful on mdt1"
4731 }
4732 run_test 401 "Compound requests split and sent to their respective MDTs"
4733
4734 mdc_change_state() # facet, MDT_pattern, activate|deactivate
4735 {
4736         local facet=$1
4737         local pattern="$2"
4738         local state=$3
4739         local node=$(facet_active_host $facet)
4740         local mdc
4741         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
4742                         awk '{print $4}'); do
4743                 echo "$3 $mdc on $node"
4744                 do_facet $facet "$LCTL --device $mdc $state" || return 1
4745         done
4746 }
4747
4748 test_402a() {
4749         # deactivate all mdc on agent1
4750         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
4751
4752         copytool setup --no-fail
4753
4754         check_agent_unregistered "uuid" # match any agent
4755
4756         # no expected running copytool
4757         search_copytools $agent && error "Copytool start should have failed"
4758
4759         # reactivate MDCs
4760         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
4761 }
4762 run_test 402a "Copytool start fails if all MDTs are inactive"
4763
4764 test_402b() {
4765         copytool setup
4766
4767         mkdir -p $DIR/$tdir
4768
4769         local f=$DIR/$tdir/$tfile
4770         touch $f || error "touch $f failed"
4771         local fid=$(path2fid $f)
4772
4773 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
4774         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
4775         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4776
4777         # give time for CDT to send request and to keep it for retry
4778         wait_for_loop_period
4779
4780         wait_request_state $fid ARCHIVE WAITING
4781
4782         do_facet $SINGLEAGT lctl set_param fail_loc=0
4783
4784         # request should succeed now
4785         wait_request_state $fid ARCHIVE SUCCEED
4786 }
4787 run_test 402b "CDT must retry request upon slow start of CT"
4788
4789 test_403() {
4790         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4791
4792         local agent=$(facet_active_host $SINGLEAGT)
4793
4794         # deactivate all mdc for MDT0001
4795         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4796
4797         copytool setup
4798         local uuid=$(get_agent_uuid $agent)
4799         # check the agent is registered on MDT0000, and not on MDT0001
4800         check_agent_registered_by_mdt $uuid 0
4801         check_agent_unregistered_by_mdt $uuid 1
4802
4803         # check running copytool process
4804         search_copytools $agent || error "No running copytools on $agent"
4805
4806         # reactivate all mdc for MDT0001
4807         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4808
4809         # make sure the copytool is now registered to all MDTs
4810         check_agent_registered $uuid
4811 }
4812 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
4813
4814 test_404() {
4815         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4816
4817         copytool setup
4818
4819         # create files on both MDT0000 and MDT0001
4820         mkdir -p $DIR/$tdir
4821
4822         local dir_mdt0=$DIR/$tdir/mdt0
4823         stack_trap "rm -rf $dir_mdt0" EXIT
4824         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4825
4826         # create 1 file on mdt0
4827         local fid1=$(create_small_file $dir_mdt0/$tfile)
4828
4829         # deactivate all mdc for MDT0001
4830         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4831
4832         # send an HSM request for files in MDT0000
4833         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4834
4835         # check for completion of files in MDT0000
4836         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4837                 echo "archive successful on mdt0"
4838
4839         # reactivate all mdc for MDT0001
4840         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4841 }
4842 run_test 404 "Inactive MDT does not block requests for active MDTs"
4843
4844 test_405() {
4845         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4846
4847         copytool setup
4848
4849         mkdir -p $DIR/$tdir
4850
4851         local striped_dir=$DIR/$tdir/striped_dir
4852
4853         # create striped dir on all of MDTs
4854         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
4855
4856         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
4857         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
4858         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
4859         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
4860
4861         local idx1=$($LFS getstripe -m $striped_dir/${tfile}_0)
4862         local idx2=$($LFS getstripe -m $striped_dir/${tfile}_1)
4863         local idx3=$($LFS getstripe -m $striped_dir/${tfile}_2)
4864         local idx4=$($LFS getstripe -m $striped_dir/${tfile}_3)
4865
4866         # check that compound requests are shunt to the rights MDTs
4867         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
4868                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
4869                 error "lfs hsm_archive"
4870
4871         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
4872                 echo "archive successful on $fid1"
4873         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
4874                 echo "archive successful on $fid2"
4875         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
4876                 echo "archive successful on $fid3"
4877         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
4878                 echo "archive successful on $fid4"
4879
4880         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
4881         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
4882         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
4883         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
4884
4885         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
4886         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
4887         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
4888         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
4889 }
4890 run_test 405 "archive and release under striped directory"
4891
4892 test_406() {
4893         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
4894
4895         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
4896                 skip "need MDS version at least 2.7.64" && return 0
4897
4898         local fid
4899         local mdt_index
4900
4901         fid=$(create_small_file $DIR/$tdir/$tfile)
4902         echo "old fid $fid"
4903
4904         copytool setup
4905
4906         $LFS hsm_archive $DIR/$tdir/$tfile
4907         wait_request_state "$fid" ARCHIVE SUCCEED
4908         $LFS hsm_release $DIR/$tdir/$tfile
4909
4910         # Should migrate $tdir but not $tfile.
4911         $LFS migrate -m1 $DIR/$tdir &&
4912                 error "migrating HSM an archived file should fail"
4913
4914         $LFS hsm_restore $DIR/$tdir/$tfile
4915         wait_request_state "$fid" RESTORE SUCCEED
4916
4917         $LFS hsm_remove $DIR/$tdir/$tfile
4918         wait_request_state "$fid" REMOVE SUCCEED
4919
4920         cat $DIR/$tdir/$tfile > /dev/null ||
4921                 error "cannot read $DIR/$tdir/$tfile"
4922
4923         $LFS migrate -m1 $DIR/$tdir ||
4924                 error "cannot complete migration after HSM remove"
4925
4926         mdt_index=$($LFS getstripe -m $DIR/$tdir)
4927         if ((mdt_index != 1)); then
4928                 error "expected MDT index 1, got $mdt_index"
4929         fi
4930
4931         # Refresh fid after migration.
4932         fid=$(path2fid $DIR/$tdir/$tfile)
4933         echo "new fid $fid"
4934
4935         $LFS hsm_archive $DIR/$tdir/$tfile
4936         wait_request_state "$fid" ARCHIVE SUCCEED 1
4937
4938         lctl set_param debug=+trace
4939         $LFS hsm_release $DIR/$tdir/$tfile ||
4940                 error "cannot release $DIR/$tdir/$tfile"
4941
4942         $LFS hsm_restore $DIR/$tdir/$tfile
4943         wait_request_state "$fid" RESTORE SUCCEED 1
4944
4945         cat $DIR/$tdir/$tfile > /dev/null ||
4946                 error "cannot read $DIR/$tdir/$tfile"
4947 }
4948 run_test 406 "attempting to migrate HSM archived files is safe"
4949
4950 test_407() {
4951         local f=$DIR/$tdir/$tfile
4952         local f2=$DIR2/$tdir/$tfile
4953         local fid=$(create_empty_file "$f")
4954
4955         copytool setup
4956
4957         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4958         wait_request_state $fid ARCHIVE SUCCEED
4959         $LFS hsm_release $f
4960
4961 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
4962         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
4963
4964         # Prevent restore from completing
4965         copytool_suspend
4966
4967         md5sum $f &
4968         # 1st request holds layout lock while appropriate
4969         # RESTORE record is still not added to llog
4970         md5sum $f2 &
4971         sleep 2
4972
4973         # after umount hsm_actions->O/x/x log shouldn't have
4974         # double RESTORE records like below
4975         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
4976         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
4977         sleep 30 &&
4978                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
4979         fail $SINGLEMDS
4980
4981         copytool_continue
4982         wait_request_state $fid RESTORE SUCCEED
4983 }
4984 run_test 407 "Check for double RESTORE records in llog"
4985
4986 test_500()
4987 {
4988         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
4989                 skip "HSM migrate is not supported" && return
4990
4991         test_mkdir -p $DIR/$tdir
4992         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
4993 }
4994 run_test 500 "various LLAPI HSM tests"
4995
4996 test_600() {
4997         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
4998                 skip "need MDS version at least 2.10.58" && return 0
4999
5000         mkdir -p $DIR/$tdir
5001
5002         local f=$DIR/$tdir/$tfile
5003
5004         changelog_register
5005         # set changelog_mask to ALL
5006         changelog_chmask "ALL"
5007
5008         chmod 777 $DIR/$tdir
5009         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5010         local fid=$(path2fid $f)
5011
5012         local entry
5013         entry=$(changelog_find -type CREAT -target-fid $fid -uid "$RUNAS_ID" \
5014                                -gid "$RUNAS_GID") ||
5015                 error "No matching CREAT entry"
5016
5017         # Parse the changelog
5018         eval local -A changelog=$(changelog2array $entry)
5019         local nid="${changelog[nid]}"
5020
5021         # Check its NID
5022         echo "Got NID '$nid'"
5023         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5024                 error "nid '$nid' does not match any client NID:" \
5025                       "${CLIENT_NIDS[@]}"
5026 }
5027 run_test 600 "Changelog fields 'u=' and 'nid='"
5028
5029 test_601() {
5030         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5031                 skip "need MDS version at least 2.10.58" && return 0
5032
5033         mkdir -p $DIR/$tdir
5034
5035         local f=$DIR/$tdir/$tfile
5036
5037         changelog_register
5038         # set changelog_mask to ALL
5039         changelog_chmask "ALL"
5040
5041         touch $f || error "touch $f failed"
5042         local fid=$(path2fid $f)
5043
5044         changelog_clear
5045         cat $f || error "cat $f failed"
5046
5047         changelog_find -type OPEN -target-fid $fid -mode "r--" ||
5048                 error "No matching OPEN entry"
5049 }
5050 run_test 601 "OPEN Changelog entry"
5051
5052 test_602() {
5053         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5054                 skip "need MDS version at least 2.10.58" && return 0
5055
5056         mkdir -p $DIR/$tdir
5057
5058         local f=$DIR/$tdir/$tfile
5059
5060         changelog_register
5061         # set changelog_mask to ALL
5062         changelog_chmask "ALL"
5063
5064         touch $f || error "touch $f failed"
5065         local fid=$(path2fid $f)
5066
5067         changelog_clear
5068         cat $f || error "cat $f failed"
5069
5070         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5071
5072         changelog_clear
5073         changelog_dump
5074         echo f > $f || error "write $f failed"
5075         changelog_dump
5076
5077         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5078
5079         # remove OPEN from changelog_mask
5080         changelog_chmask "-OPEN"
5081
5082         changelog_clear
5083         changelog_dump
5084         cat $f || error "cat $f failed"
5085         changelog_dump
5086
5087         changelog_find -type CLOSE -target-fid $fid &&
5088                 error "There should be no CLOSE entry"
5089
5090         changelog_clear
5091         changelog_dump
5092         echo f > $f || error "write $f failed"
5093         changelog_dump
5094
5095         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5096 }
5097 run_test 602 "Changelog record CLOSE only if open+write or OPEN recorded"
5098
5099 test_603() {
5100         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5101                 skip "need MDS version at least 2.10.58" && return 0
5102
5103         mkdir -p $DIR/$tdir
5104
5105         local f=$DIR/$tdir/$tfile
5106
5107         changelog_register
5108         # set changelog_mask to ALL
5109         changelog_chmask "ALL"
5110
5111         touch $f || error "touch $f failed"
5112         local fid=$(path2fid $f)
5113
5114         setfattr -n user.xattr1 -v "value1" $f || error "setfattr $f failed"
5115
5116         changelog_clear
5117         getfattr -n user.xattr1 $f || error "getfattr $f failed"
5118
5119         changelog_find -type GXATR -target-fid $fid -xattr "user.xattr1" ||
5120                 error "No matching GXATR entry"
5121 }
5122 run_test 603 "GETXATTR Changelog entry"
5123
5124 test_604() {
5125         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5126                 skip "need MDS version at least 2.10.58" && return 0
5127
5128         mkdir -p $DIR/$tdir
5129
5130         local f=$DIR/$tdir/$tfile
5131         local f2=$DIR2/$tdir/$tfile
5132         local procname="mdd.$FSNAME-MDT0000.changelog_deniednext"
5133         local timeout
5134         timeout="$(do_facet mds1 "$LCTL" get_param -n "$procname")"
5135         stack_trap "do_facet mds1 '$LCTL' set_param '$procname=$timeout'" EXIT
5136         do_facet mds1 lctl set_param "$procname=20"
5137
5138
5139         changelog_register
5140         # set changelog_mask to ALL
5141         changelog_chmask "ALL"
5142
5143         touch $f || error "touch $f failed"
5144         local fid=$(path2fid $f)
5145
5146         chmod 600 $f
5147
5148         changelog_clear
5149         changelog_dump
5150         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5151         changelog_dump
5152
5153         local entry
5154         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5155                                -gid "$RUNAS_GID" -mode "r--") ||
5156                 error "No matching NOPEN entry"
5157
5158         # Parse the changelog
5159         eval local -A changelog=$(changelog2array $entry)
5160         local nid="${changelog[nid]}"
5161
5162         # Check its NID
5163         echo "Got NID '$nid'"
5164         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5165                 error "nid '$nid' does not match any client NID:" \
5166                       "${CLIENT_NIDS[@]}"
5167
5168         changelog_clear
5169         changelog_dump
5170         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5171         changelog_dump
5172
5173         changelog_find -type NOPEN -target-fid $fid &&
5174                 error "There should be no NOPEN entry"
5175
5176         # Sleep for `changelog_deniednext` seconds
5177         sleep 20
5178
5179         changelog_clear
5180         changelog_dump
5181         $RUNAS cat $f2 && error "cat $f by user $RUNAS_ID should have failed"
5182         changelog_dump
5183
5184         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5185                                -gid "$RUNAS_GID" -mode "r--") ||
5186                 error "No matching NOPEN entry"
5187
5188         # Parse the changelog
5189         eval local -A changelog=$(changelog2array $entry)
5190         local nid="${changelog[nid]}"
5191
5192         # Check the NID
5193         echo "Got NID '$nid'"
5194         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5195                 error "nid '$nid' does not match any client NID:" \
5196                       "${CLIENT_NIDS[@]}"
5197 }
5198 run_test 604 "NOPEN Changelog entry"
5199
5200 test_605() {
5201         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5202                 skip "need MDS version at least 2.10.58" && return 0
5203
5204         mkdir -p $DIR/$tdir
5205
5206         local f=$DIR/$tdir/$tfile
5207         local f2=$DIR2/$tdir/$tfile
5208
5209         changelog_register
5210         # set changelog_mask to ALL
5211         changelog_chmask "ALL"
5212
5213         touch $f || error "touch $f failed"
5214         local fid=$(path2fid $f)
5215
5216         changelog_clear
5217         changelog_dump
5218         exec 3<> $f || error "open $f failed"
5219         changelog_dump
5220
5221         local entry
5222         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5223
5224         changelog_clear
5225         changelog_dump
5226         exec 4<> $f || error "open $f failed"
5227         changelog_dump
5228
5229         changelog_find -type OPEN -target-fid $fid &&
5230                 error "There should be no OPEN entry"
5231
5232         exec 4>&- || error "close $f failed"
5233         changelog_dump
5234
5235         changelog_find -type CLOSE -target-fid $fid &&
5236                 error "There should be no CLOSE entry"
5237
5238         changelog_clear
5239         changelog_dump
5240         # access in rw, so different access mode should generate entries
5241         cat $f || error "cat $f failed"
5242         changelog_dump
5243
5244         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5245
5246         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5247
5248         changelog_clear
5249         changelog_dump
5250         # same access as first one, should not generate new entries
5251         exec 4<> $f || error "open $f failed"
5252         changelog_dump
5253
5254         changelog_find -type OPEN -target-fid $fid &&
5255                 error "There should be no OPEN entry"
5256
5257         exec 4>&- || error "close $f failed"
5258         changelog_dump
5259
5260         changelog_find -type CLOSE -target-fid $fid &&
5261                 error "There should be no CLOSE entry"
5262
5263         changelog_clear
5264         changelog_dump
5265         # access by different user should generate new entries
5266         $RUNAS cat $f || error "cat $f by user $RUNAS_ID failed"
5267         changelog_dump
5268
5269         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5270
5271         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5272
5273         changelog_clear
5274         changelog_dump
5275         exec 3>&- || error "close $f failed"
5276         changelog_dump
5277
5278         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5279 }
5280 run_test 605 "Test OPEN and CLOSE rate limit in Changelogs"
5281
5282 test_606() {
5283         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
5284                 skip "need MDS version at least 2.10.58" && return 0
5285
5286         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5287         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5288         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5289                         skip_env "missing llog_reader" && return
5290         local fstype=$(facet_fstype mds1)
5291
5292         mkdir -p $DIR/$tdir
5293
5294         local f=$DIR/$tdir/$tfile
5295
5296         changelog_register
5297         # set changelog_mask to ALL
5298         changelog_chmask "ALL"
5299
5300         chmod 777 $DIR/$tdir
5301         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5302         local fid=$(path2fid $f)
5303         rm $f || error "rm $f failed"
5304
5305         local mntpt=$(facet_mntpt mds1)
5306         local pass=true
5307         local entry
5308
5309         #remount mds1 as ldiskfs or zfs type
5310         stack_trap "stop mds1; start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS" EXIT
5311         stop mds1 || error "stop mds1 failed"
5312         mount_fstype mds1 || error "remount mds1 failed"
5313
5314         for ((i = 0; i < 1; i++)); do
5315                 do_facet mds1 $llog_reader $mntpt/changelog_catalog
5316                 local cat_file=$(do_facet mds1 $llog_reader \
5317                                 $mntpt/changelog_catalog | awk \
5318                                 '{match($0,"path=([^ ]+)",a)}END{print a[1]}')
5319                 [ -n "$cat_file" ] || error "no catalog file"
5320
5321                 entry=$(do_facet mds1 $llog_reader $mntpt/$cat_file |
5322                         awk "/CREAT/ && /target:\[$fid\]/ {print}")
5323                 [ -n "$entry" ] || error "no CREAT entry"
5324         done
5325
5326         local uidgid=$(echo $entry |
5327                 sed 's+.*\ user:\([0-9][0-9]*:[0-9][0-9]*\)\ .*+\1+')
5328         [ -n "$uidgid" ] || error "uidgid is empty"
5329         echo "Got UID/GID $uidgid"
5330         [ "$uidgid" = "$RUNAS_ID:$RUNAS_GID" ] ||
5331                 error "uidgid '$uidgid' != '$RUNAS_ID:$RUNAS_GID'"
5332         local nid=$(echo $entry |
5333                 sed 's+.*\ nid:\(\S\S*@\S\S*\)\ .*+\1+')
5334         [ -n "$nid" ] || error "nid is empty"
5335         echo "Got NID $nid"
5336         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5337                 error "nid '$nid' does not match any NID ${CLIENT_NIDS[@]}"
5338 }
5339 run_test 606 "llog_reader groks changelog fields"
5340
5341 complete $SECONDS
5342 check_and_cleanup_lustre
5343 exit_status