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