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