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