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