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