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