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