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