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