Whamcloud - gitweb
LU-9474 tests: add the stack_trap() utility function
[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 == 80000001 ]] || 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         local rc=$(wait $pid)
3073         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
3074
3075         copytool_cleanup
3076 }
3077 run_test 34 "Remove file during restore"
3078
3079 test_35() {
3080         # test needs a running copytool
3081         copytool_setup
3082
3083         mkdir -p $DIR/$tdir
3084
3085         local f=$DIR/$tdir/$tfile
3086         local f1=$DIR/$tdir/$tfile-1
3087         local fid
3088         fid=$(make_custom_file_for_progress $f 39 1000000)
3089         [ $? != 0 ] && skip "not enough free space" && return
3090
3091         local fid1=$(copy_file /etc/passwd $f1)
3092         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3093         wait_request_state $fid ARCHIVE SUCCEED
3094         $LFS hsm_release $f
3095
3096         md5sum $f >/dev/null &
3097         local pid=$!
3098         wait_request_state $fid RESTORE STARTED
3099
3100         mv $f1 $f || error "mv $f1 $f failed"
3101         # mv must not block during restore
3102         wait_request_state $fid RESTORE STARTED
3103
3104         wait_request_state $fid RESTORE SUCCEED
3105         # check md5sum pgm finished
3106         local there=$(ps -o pid,comm hp $pid >/dev/null)
3107         [[ -z $there ]] || error "Restore initiator does not exit"
3108
3109         local rc=$(wait $pid)
3110         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
3111
3112         fid2=$(path2fid $f)
3113         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
3114
3115         copytool_cleanup
3116 }
3117 run_test 35 "Overwrite file during restore"
3118
3119 test_36() {
3120         # test needs a running copytool
3121         copytool_setup
3122
3123         mkdir -p $DIR/$tdir
3124
3125         local f=$DIR/$tdir/$tfile
3126         local fid
3127         fid=$(make_custom_file_for_progress $f 39 1000000)
3128         [ $? != 0 ] && skip "not enough free space" && return
3129
3130         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3131         wait_request_state $fid ARCHIVE SUCCEED
3132         $LFS hsm_release $f
3133
3134         md5sum $f >/dev/null &
3135         local pid=$!
3136         wait_request_state $fid RESTORE STARTED
3137
3138         mv $f $f.new
3139         # rm must not block during restore
3140         wait_request_state $fid RESTORE STARTED
3141
3142         wait_request_state $fid RESTORE SUCCEED
3143         # check md5sum pgm finished
3144         local there=$(ps -o pid,comm hp $pid >/dev/null)
3145         [[ -z $there ]] ||
3146                 error "Restore initiator does not exit"
3147
3148         local rc=$(wait $pid)
3149         [[ $rc -eq 0 ]] ||
3150                 error "Restore initiator failed with $rc"
3151
3152         copytool_cleanup
3153 }
3154 run_test 36 "Move file during restore"
3155
3156 test_37() {
3157         # LU-5683: check that an archived dirty file can be rearchived.
3158         copytool_cleanup
3159         copytool_setup $SINGLEAGT $MOUNT2
3160
3161         mkdir -p $DIR/$tdir
3162         local f=$DIR/$tdir/$tfile
3163         local fid
3164
3165         fid=$(make_small $f) || error "cannot create small file"
3166
3167         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3168         wait_request_state $fid ARCHIVE SUCCEED
3169         $LFS hsm_release $f || error "cannot release $f"
3170
3171         # Allow previous archive request to expire from the actions log.
3172         wait_for_grace_delay
3173
3174         # Dirty file.
3175         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
3176
3177         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3178         wait_request_state $fid ARCHIVE SUCCEED
3179
3180         copytool_cleanup
3181 }
3182 run_test 37 "re-archive a dirty file"
3183
3184 multi_archive() {
3185         local prefix=$1
3186         local count=$2
3187         local n=""
3188
3189         for n in $(seq 1 $count); do
3190                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
3191         done
3192         echo "$count archive requests submitted"
3193 }
3194
3195 cleanup_test_40() {
3196         trap 0
3197         set_hsm_param max_requests $max_requests
3198         copytool_cleanup
3199 }
3200
3201 test_40() {
3202         local stream_count=4
3203         local file_count=100
3204         mkdir -p $DIR/$tdir
3205         local f=$DIR/$tdir/$tfile
3206         local i=""
3207         local p=""
3208         local fid=""
3209         local max_requests=$(get_hsm_param max_requests)
3210
3211         # Increase the number of HSM request that can be performed in
3212         # parallel. With the coordinator running once per second, this
3213         # also limits the number of requests per seconds that can be
3214         # performed, so we pick a decent number. But we also need to keep
3215         # that number low because the copytool has no rate limit and will
3216         # fail some requests if if gets too many at once.
3217         set_hsm_param max_requests 300
3218
3219         trap cleanup_test_40 EXIT
3220
3221         for i in $(seq 1 $file_count); do
3222                 for p in $(seq 1 $stream_count); do
3223                         fid=$(copy_file /etc/hosts $f.$p.$i)
3224                 done
3225         done
3226         # force copytool to use a local/temp archive dir to ensure best
3227         # performance vs remote/NFS mounts used in auto-tests
3228         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
3229                 copytool_setup
3230         else
3231                 copytool_setup $SINGLEAGT $MOUNT $HSM_ARCHIVE_NUMBER $TMP/$tdir
3232         fi
3233         # to be sure wait_all_done will not be mislead by previous tests
3234         cdt_purge
3235         wait_for_grace_delay
3236         typeset -a pids
3237         # start archive streams in background (archive files in parallel)
3238         for p in $(seq 1 $stream_count); do
3239                 multi_archive $f.$p $file_count &
3240                 pids[$p]=$!
3241         done
3242         echo -n  "Wait for all requests being enqueued..."
3243         wait ${pids[*]}
3244         echo OK
3245         wait_all_done 100
3246
3247         cleanup_test_40
3248 }
3249 run_test 40 "Parallel archive requests"
3250
3251 test_52() {
3252         # test needs a running copytool
3253         copytool_setup
3254
3255         mkdir -p $DIR/$tdir
3256         local f=$DIR/$tdir/$tfile
3257         local fid=$(copy_file /etc/motd $f 1)
3258
3259         $LFS hsm_archive $f || error "could not archive file"
3260         wait_request_state $fid ARCHIVE SUCCEED
3261         check_hsm_flags $f "0x00000009"
3262
3263         multiop_bg_pause $f O_c || error "multiop failed"
3264         local MULTIPID=$!
3265
3266         mds_evict_client
3267         client_up || client_up || true
3268
3269         kill -USR1 $MULTIPID
3270         wait $MULTIPID || error "multiop close failed"
3271
3272         check_hsm_flags $f "0x0000000b"
3273
3274         copytool_cleanup
3275 }
3276 run_test 52 "Opened for write file on an evicted client should be set dirty"
3277
3278 test_53() {
3279         # test needs a running copytool
3280         copytool_setup
3281
3282         mkdir -p $DIR/$tdir
3283         local f=$DIR/$tdir/$tfile
3284         local fid=$(copy_file /etc/motd $f 1)
3285
3286         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3287                 error "could not archive file"
3288         wait_request_state $fid ARCHIVE SUCCEED
3289         check_hsm_flags $f "0x00000009"
3290
3291         multiop_bg_pause $f o_c || error "multiop failed"
3292         MULTIPID=$!
3293
3294         mds_evict_client
3295         client_up || client_up || true
3296
3297         kill -USR1 $MULTIPID
3298         wait $MULTIPID || error "multiop close failed"
3299
3300         check_hsm_flags $f "0x00000009"
3301
3302         copytool_cleanup
3303 }
3304 run_test 53 "Opened for read file on an evicted client should not be set dirty"
3305
3306 test_54() {
3307         # test needs a running copytool
3308         copytool_setup
3309
3310         mkdir -p $DIR/$tdir
3311         local f=$DIR/$tdir/$tfile
3312         local fid=$(make_custom_file_for_progress $f 39 1000000)
3313
3314         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3315                 error "could not archive file"
3316         wait_request_state $fid ARCHIVE STARTED
3317
3318         check_hsm_flags $f "0x00000001"
3319
3320         # Avoid coordinator resending this request as soon it has failed.
3321         cdt_set_no_retry
3322
3323         echo "foo" >> $f
3324         sync
3325         wait_request_state $fid ARCHIVE FAILED
3326
3327         check_hsm_flags $f "0x00000003"
3328
3329         cdt_clear_no_retry
3330         copytool_cleanup
3331 }
3332 run_test 54 "Write during an archive cancels it"
3333
3334 test_55() {
3335         # test needs a running copytool
3336         copytool_setup
3337
3338         mkdir -p $DIR/$tdir
3339         local f=$DIR/$tdir/$tfile
3340         local fid=$(make_custom_file_for_progress $f 39 1000000)
3341
3342         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3343                 error "could not archive file"
3344         wait_request_state $fid ARCHIVE STARTED
3345
3346         check_hsm_flags $f "0x00000001"
3347
3348         # Avoid coordinator resending this request as soon it has failed.
3349         cdt_set_no_retry
3350
3351         $TRUNCATE $f 1024 || error "truncate failed"
3352         sync
3353         wait_request_state $fid ARCHIVE FAILED
3354
3355         check_hsm_flags $f "0x00000003"
3356
3357         cdt_clear_no_retry
3358         copytool_cleanup
3359 }
3360 run_test 55 "Truncate during an archive cancels it"
3361
3362 test_56() {
3363         # test needs a running copytool
3364         copytool_setup
3365
3366         mkdir -p $DIR/$tdir
3367         local f=$DIR/$tdir/$tfile
3368         local fid
3369         fid=$(make_custom_file_for_progress $f 39 1000000)
3370         [ $? != 0 ] && skip "not enough free space" && return
3371
3372         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3373                 error "could not archive file"
3374         wait_request_state $fid ARCHIVE STARTED
3375
3376         check_hsm_flags $f "0x00000001"
3377
3378         # Change metadata and sync to be sure we are not changing only
3379         # in memory.
3380         chmod 644 $f
3381         chgrp sys $f
3382         sync
3383         wait_request_state $fid ARCHIVE SUCCEED
3384
3385         check_hsm_flags $f "0x00000009"
3386
3387         copytool_cleanup
3388 }
3389 run_test 56 "Setattr during an archive is ok"
3390
3391 test_57() {
3392         # Need one client for I/O, one for request
3393         needclients 2 || return 0
3394
3395         # test needs a running copytool
3396         copytool_setup
3397
3398         mkdir -p $DIR/$tdir
3399         local f=$DIR/$tdir/test_archive_remote
3400         # Create a file on a remote node
3401         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
3402                 "count=2 conv=fsync"
3403
3404         # And archive it
3405         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
3406                 error "hsm_archive failed"
3407         local fid=$(path2fid $f)
3408         wait_request_state $fid ARCHIVE SUCCEED
3409
3410         # Release and implicit restore it
3411         do_node $CLIENT2 "$LFS hsm_release $f" ||
3412                 error "hsm_release failed"
3413         do_node $CLIENT2 "md5sum $f" ||
3414                 error "hsm_restore failed"
3415
3416         wait_request_state $fid RESTORE SUCCEED
3417
3418         copytool_cleanup
3419 }
3420 run_test 57 "Archive a file with dirty cache on another node"
3421
3422 truncate_released_file() {
3423         local src_file=$1
3424         local trunc_to=$2
3425
3426         local sz=$(stat -c %s $src_file)
3427         local f=$DIR/$tdir/$tfile
3428         local fid=$(copy_file $1 $f)
3429         local ref=$f-ref
3430         cp $f $f-ref
3431
3432         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3433                 error "could not archive file"
3434         wait_request_state $fid ARCHIVE SUCCEED
3435
3436         $LFS hsm_release $f || error "could not release file"
3437
3438         $TRUNCATE $f $trunc_to || error "truncate failed"
3439         sync
3440
3441         local sz1=$(stat -c %s $f)
3442         [[ $sz1 == $trunc_to ]] ||
3443                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3444
3445         $LFS hsm_state $f
3446         check_hsm_flags $f "0x0000000b"
3447
3448         local state=$(get_request_state $fid RESTORE)
3449         [[ "$state" == "SUCCEED" ]] ||
3450                 error "truncate $sz does not trig restore, state = $state"
3451
3452         $TRUNCATE $ref $trunc_to
3453         cmp $ref $f || error "file data wrong after truncate"
3454
3455         rm -f $f $f-ref
3456 }
3457
3458 test_58() {
3459         # test needs a running copytool
3460         copytool_setup
3461
3462         mkdir -p $DIR/$tdir
3463
3464         local sz=$(stat -c %s /etc/passwd)
3465
3466         echo "truncate up from $sz to $((sz*2))"
3467         truncate_released_file /etc/passwd $((sz*2))
3468
3469         echo "truncate down from $sz to $((sz/2))"
3470         truncate_released_file /etc/passwd $((sz/2))
3471
3472         echo "truncate to 0"
3473         truncate_released_file /etc/passwd 0
3474
3475         copytool_cleanup
3476 }
3477 run_test 58 "Truncate a released file will trigger restore"
3478
3479 test_59() {
3480         local fid
3481         local server_version=$(lustre_version_code $SINGLEMDS)
3482         [[ $server_version -lt $(version_code 2.7.63) ]] &&
3483                 skip "Need MDS version at least 2.7.63" && return
3484
3485         copytool_setup
3486         $MCREATE $DIR/$tfile || error "mcreate failed"
3487         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3488         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3489         fid=$(path2fid $DIR/$tfile)
3490         wait_request_state $fid ARCHIVE SUCCEED
3491         $LFS hsm_release $DIR/$tfile || error "release failed"
3492         copytool_cleanup
3493 }
3494 run_test 59 "Release stripeless file with non-zero size"
3495
3496 test_60() {
3497         # This test validates the fix for LU-4512. Ensure that the -u
3498         # option changes the progress reporting interval from the
3499         # default (30 seconds) to the user-specified interval.
3500         local interval=5
3501         local progress_timeout=$((interval * 4))
3502
3503         # test needs a new running copytool
3504         copytool_cleanup
3505         HSMTOOL_UPDATE_INTERVAL=$interval copytool_setup
3506
3507         mkdir -p $DIR/$tdir
3508         local f=$DIR/$tdir/$tfile
3509         local fid
3510         fid=$(make_custom_file_for_progress $f 10)
3511         [ $? != 0 ] && skip "not enough free space" && return
3512
3513         local mdtidx=0
3514         local mdt=${MDT_PREFIX}${mdtidx}
3515         local mds=mds$((mdtidx + 1))
3516
3517         # Wait for copytool to register
3518         wait_update_facet $mds \
3519                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3520                 uuid 100 || error "coyptool failed to register with $mdt"
3521
3522         local start_at=$(date +%s)
3523         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3524                 error "could not archive file"
3525
3526         local agent=$(facet_active_host $SINGLEAGT)
3527         local prefix=$TESTLOG_PREFIX
3528         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
3529         local copytool_log=$prefix.copytool_log.$agent.log
3530
3531
3532         wait_update $agent \
3533             "grep -o start.copy $copytool_log" "start copy" 100 ||
3534                 error "copytool failed to start"
3535
3536         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3537         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3538
3539         local RESULT
3540         local WAIT=0
3541         local sleep=1
3542
3543         echo -n "Expecting a progress update within $progress_timeout seconds... "
3544         while [ true ]; do
3545                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3546                 if [ $RESULT -gt 0 ]; then
3547                         echo "$RESULT bytes copied in $WAIT seconds."
3548                         break
3549                 elif [ $WAIT -ge $progress_timeout ]; then
3550                         error "Timed out waiting for progress update!"
3551                         break
3552                 fi
3553                 WAIT=$((WAIT + sleep))
3554                 sleep $sleep
3555         done
3556
3557         local finish_at=$(date +%s)
3558         local elapsed=$((finish_at - start_at))
3559
3560         # Ensure that the progress update occurred within the expected window.
3561         if [ $elapsed -lt $((interval - 1)) ]; then
3562                 error "Expected progress update after at least $interval seconds"
3563         fi
3564
3565         echo "Wait for on going archive hsm action to complete"
3566         wait_update $agent "grep -o copied $copytool_log" "copied" 10 ||
3567                 echo "File archiving not completed even after 10 secs"
3568
3569         cdt_clear_no_retry
3570         copytool_cleanup
3571 }
3572 run_test 60 "Changing progress update interval from default"
3573
3574 test_61() {
3575         # test needs a running copytool
3576         copytool_setup
3577
3578         mkdir -p $DIR/$tdir
3579         local f=$DIR/$tdir/$tfile
3580         local fid=$(copy_file /etc/passwd $f)
3581         cdt_disable
3582         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3583         rm -f $f
3584         cdt_enable
3585         wait_request_state $fid ARCHIVE FAILED
3586
3587         copytool_cleanup
3588 }
3589 run_test 61 "Waiting archive of a removed file should fail"
3590
3591 test_70() {
3592         # test needs a new running copytool
3593         copytool_cleanup
3594         copytool_monitor_setup
3595         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3596
3597         # Just start and stop the copytool to generate events.
3598         cdt_clear_no_retry
3599
3600         # Wait for the copytool to register.
3601         wait_update --verbose $(facet_active_host mds1) \
3602                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3603                 uuid 100 ||
3604                 error "copytool failed to register with MDT0000"
3605
3606         copytool_cleanup
3607
3608         local REGISTER_EVENT
3609         local UNREGISTER_EVENT
3610         while read event; do
3611                 local parsed=$(parse_json_event "$event")
3612                 if [ -z "$parsed" ]; then
3613                         error "Copytool sent malformed event: $event"
3614                 fi
3615                 eval $parsed
3616
3617                 if [ $event_type == "REGISTER" ]; then
3618                         REGISTER_EVENT=$event
3619                 elif [ $event_type == "UNREGISTER" ]; then
3620                         UNREGISTER_EVENT=$event
3621                 fi
3622         done < <(echo $"$(get_copytool_event_log)")
3623
3624         if [ -z "$REGISTER_EVENT" ]; then
3625                 error "Copytool failed to send register event to FIFO"
3626         fi
3627
3628         if [ -z "$UNREGISTER_EVENT" ]; then
3629                 error "Copytool failed to send unregister event to FIFO"
3630         fi
3631
3632         copytool_monitor_cleanup
3633         echo "Register/Unregister events look OK."
3634 }
3635 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3636
3637 test_71() {
3638         # Bump progress interval for livelier events.
3639         local interval=5
3640
3641         # test needs a new running copytool
3642         copytool_cleanup
3643         copytool_monitor_setup
3644         HSMTOOL_UPDATE_INTERVAL=$interval \
3645         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3646
3647         mkdir -p $DIR/$tdir
3648         local f=$DIR/$tdir/$tfile
3649         local fid
3650         fid=$(make_custom_file_for_progress $f 39 1000000)
3651         [ $? != 0 ] && skip "not enough free space" && return
3652
3653         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3654                 error "could not archive file"
3655         wait_request_state $fid ARCHIVE SUCCEED
3656
3657         local expected_fields="event_time data_fid source_fid"
3658         expected_fields+=" total_bytes current_bytes"
3659
3660         local START_EVENT
3661         local FINISH_EVENT
3662         while read event; do
3663                 # Make sure we're not getting anything from previous events.
3664                 for field in $expected_fields; do
3665                         unset $field
3666                 done
3667
3668                 local parsed=$(parse_json_event "$event")
3669                 if [ -z "$parsed" ]; then
3670                         error "Copytool sent malformed event: $event"
3671                 fi
3672                 eval $parsed
3673
3674                 if [ $event_type == "ARCHIVE_START" ]; then
3675                         START_EVENT=$event
3676                         continue
3677                 elif [ $event_type == "ARCHIVE_FINISH" ]; then
3678                         FINISH_EVENT=$event
3679                         continue
3680                 elif [ $event_type != "ARCHIVE_RUNNING" ]; then
3681                         continue
3682                 fi
3683
3684                 # Do some simple checking of the progress update events.
3685                 for expected_field in $expected_fields; do
3686                         if [ -z ${!expected_field+x} ]; then
3687                                 error "Missing $expected_field field in event"
3688                         fi
3689                 done
3690
3691                 if [ $total_bytes -eq 0 ]; then
3692                         error "Expected total_bytes to be > 0"
3693                 fi
3694
3695                 # These should be identical throughout an archive
3696                 # operation.
3697                 if [ $source_fid != $data_fid ]; then
3698                         error "Expected source_fid to equal data_fid"
3699                 fi
3700         done < <(echo $"$(get_copytool_event_log)")
3701
3702         if [ -z "$START_EVENT" ]; then
3703                 error "Copytool failed to send archive start event to FIFO"
3704         fi
3705
3706         if [ -z "$FINISH_EVENT" ]; then
3707                 error "Copytool failed to send archive finish event to FIFO"
3708         fi
3709
3710         echo "Archive events look OK."
3711
3712         cdt_clear_no_retry
3713         copytool_cleanup
3714         copytool_monitor_cleanup
3715 }
3716 run_test 71 "Copytool logs JSON archive events to FIFO"
3717
3718 test_72() {
3719         # Bump progress interval for livelier events.
3720         local interval=5
3721
3722         # test needs a new running copytool
3723         copytool_cleanup
3724         copytool_monitor_setup
3725         HSMTOOL_UPDATE_INTERVAL=$interval \
3726         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3727         local test_file=$HSMTOOL_MONITOR_DIR/file
3728
3729         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3730         cmd+="conv=fsync"
3731         do_facet $SINGLEAGT "$cmd" ||
3732                 error "cannot create $test_file on $SINGLEAGT"
3733         copy2archive $test_file $tdir/$tfile
3734
3735         mkdir -p $DIR/$tdir
3736         local f=$DIR/$tdir/$tfile
3737         import_file $tdir/$tfile $f
3738         f=$DIR2/$tdir/$tfile
3739         echo "Verifying released state: "
3740         check_hsm_flags $f "0x0000000d"
3741
3742         local fid=$(path2fid $f)
3743         $LFS hsm_restore $f
3744         wait_request_state $fid RESTORE SUCCEED
3745
3746         local expected_fields="event_time data_fid source_fid"
3747         expected_fields+=" total_bytes current_bytes"
3748
3749         local START_EVENT
3750         local FINISH_EVENT
3751         while read event; do
3752                 # Make sure we're not getting anything from previous events.
3753                 for field in $expected_fields; do
3754                         unset $field
3755                 done
3756
3757                 local parsed=$(parse_json_event "$event")
3758                 if [ -z "$parsed" ]; then
3759                         error "Copytool sent malformed event: $event"
3760                 fi
3761                 eval $parsed
3762
3763                 if [ $event_type == "RESTORE_START" ]; then
3764                         START_EVENT=$event
3765                         if [ $source_fid != $data_fid ]; then
3766                                 error "source_fid should == data_fid at start"
3767                         fi
3768                         continue
3769                 elif [ $event_type == "RESTORE_FINISH" ]; then
3770                         FINISH_EVENT=$event
3771                         if [ $source_fid != $data_fid ]; then
3772                                 error "source_fid should == data_fid at finish"
3773                         fi
3774                         continue
3775                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3776                         continue
3777                 fi
3778
3779                 # Do some simple checking of the progress update events.
3780                 for expected_field in $expected_fields; do
3781                         if [ -z ${!expected_field+x} ]; then
3782                                 error "Missing $expected_field field in event"
3783                         fi
3784                 done
3785
3786                 if [ $total_bytes -eq 0 ]; then
3787                         error "Expected total_bytes to be > 0"
3788                 fi
3789
3790                 # When a restore starts out, the data fid is the same as the
3791                 # source fid. After the restore has gotten going, we learn
3792                 # the new data fid. Once the restore has finished, the source
3793                 # fid is set to the new data fid.
3794                 #
3795                 # We test this because some monitoring software may depend on
3796                 # this behavior. If it changes, then the consumers of these
3797                 # events may need to be modified.
3798                 if [ $source_fid == $data_fid ]; then
3799                         error "source_fid should != data_fid during restore"
3800                 fi
3801         done < <(echo $"$(get_copytool_event_log)")
3802
3803         if [ -z "$START_EVENT" ]; then
3804                 error "Copytool failed to send restore start event to FIFO"
3805         fi
3806
3807         if [ -z "$FINISH_EVENT" ]; then
3808                 error "Copytool failed to send restore finish event to FIFO"
3809         fi
3810
3811         echo "Restore events look OK."
3812
3813         cdt_clear_no_retry
3814         copytool_cleanup
3815         copytool_monitor_cleanup
3816
3817         rm -rf $test_dir
3818 }
3819 run_test 72 "Copytool logs JSON restore events to FIFO"
3820
3821 test_90() {
3822         file_count=51 # Max number of files constrained by LNET message size
3823         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3824         local f=$DIR/$tdir/$tfile
3825         local FILELIST=/tmp/filelist.txt
3826         local i=""
3827
3828         rm -f $FILELIST
3829         for i in $(seq 1 $file_count); do
3830                 fid=$(copy_file /etc/hosts $f.$i)
3831                 echo $f.$i >> $FILELIST
3832         done
3833         # force copytool to use a local/temp archive dir to ensure best
3834         # performance vs remote/NFS mounts used in auto-tests
3835         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
3836                 copytool_setup
3837         else
3838                 local dai=$(get_hsm_param default_archive_id)
3839                 copytool_setup $SINGLEAGT $MOUNT $dai $TMP/$tdir
3840         fi
3841         # to be sure wait_all_done will not be mislead by previous tests
3842         cdt_purge
3843         wait_for_grace_delay
3844         $LFS hsm_archive --filelist $FILELIST ||
3845                 error "cannot archive a file list"
3846         wait_all_done 100
3847         $LFS hsm_release --filelist $FILELIST ||
3848                 error "cannot release a file list"
3849         $LFS hsm_restore --filelist $FILELIST ||
3850                 error "cannot restore a file list"
3851         wait_all_done 100
3852         copytool_cleanup
3853 }
3854 run_test 90 "Archive/restore a file list"
3855
3856 double_verify_reset_hsm_param() {
3857         local p=$1
3858         echo "Testing $HSM_PARAM.$p"
3859         local val=$(get_hsm_param $p)
3860         local save=$val
3861         local val2=$(($val * 2))
3862         set_hsm_param $p $val2
3863         val=$(get_hsm_param $p)
3864         [[ $val == $val2 ]] ||
3865                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3866         echo "Set $p to 0 must failed"
3867         set_hsm_param $p 0
3868         local rc=$?
3869         # restore value
3870         set_hsm_param $p $save
3871
3872         if [[ $rc == 0 ]]; then
3873                 error "we must not be able to set $HSM_PARAM.$p to 0"
3874         fi
3875 }
3876
3877 test_100() {
3878         double_verify_reset_hsm_param loop_period
3879         double_verify_reset_hsm_param grace_delay
3880         double_verify_reset_hsm_param active_request_timeout
3881         double_verify_reset_hsm_param max_requests
3882         double_verify_reset_hsm_param default_archive_id
3883 }
3884 run_test 100 "Set coordinator /proc tunables"
3885
3886 test_102() {
3887         cdt_disable
3888         cdt_enable
3889         cdt_restart
3890 }
3891 run_test 102 "Verify coordinator control"
3892
3893 test_103() {
3894         # test needs a running copytool
3895         copytool_setup
3896
3897         local i=""
3898         local fid=""
3899
3900         mkdir -p $DIR/$tdir
3901         for i in $(seq 1 20); do
3902                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3903         done
3904         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3905
3906         cdt_purge
3907
3908         echo "Current requests"
3909         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3910                         $HSM_PARAM.actions |\
3911                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3912
3913         [[ -z "$res" ]] || error "Some request have not been canceled"
3914
3915         copytool_cleanup
3916 }
3917 run_test 103 "Purge all requests"
3918
3919 DATA=CEA
3920 DATAHEX='[434541]'
3921 test_104() {
3922
3923         mkdir -p $DIR/$tdir
3924         local f=$DIR/$tdir/$tfile
3925         local fid
3926         fid=$(make_custom_file_for_progress $f 39 1000000)
3927         [ $? != 0 ] && skip "not enough free space" && return
3928
3929         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3930         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3931                         $HSM_PARAM.actions |\
3932                         grep $fid | cut -f16 -d=")
3933
3934         [[ "$data1" == "$DATAHEX" ]] ||
3935                 error "Data field in records is ($data1) and not ($DATAHEX)"
3936
3937         # archive the file
3938         copytool_setup
3939
3940         wait_request_state $fid ARCHIVE SUCCEED
3941
3942         copytool_cleanup
3943 }
3944 run_test 104 "Copy tool data field"
3945
3946 cleanup_test_105() {
3947         trap 0
3948         set_hsm_param max_requests $max_requests
3949         copytool_cleanup
3950 }
3951
3952 test_105() {
3953         local max_requests=$(get_hsm_param max_requests)
3954         mkdir -p $DIR/$tdir
3955         local i=""
3956
3957         set_hsm_param max_requests 300
3958
3959         trap cleanup_test_105 EXIT
3960
3961         cdt_disable
3962         for i in $(seq -w 1 10); do
3963                 cp /etc/passwd $DIR/$tdir/$i
3964                 $LFS hsm_archive $DIR/$tdir/$i
3965         done
3966         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3967                         $HSM_PARAM.actions |\
3968                         grep WAITING | wc -l")
3969         cdt_restart
3970
3971         cdt_disable
3972         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3973                         $HSM_PARAM.actions |\
3974                         grep WAITING | wc -l")
3975         cdt_enable
3976         cdt_purge
3977         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3978                 error "Requests count after shutdown $reqcnt2 != "\
3979                       "before shutdown $reqcnt1"
3980
3981         cleanup_test_105
3982 }
3983 run_test 105 "Restart of coordinator"
3984
3985 test_106() {
3986         # test needs a running copytool
3987         copytool_setup
3988
3989         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3990
3991         check_agent_registered $uuid
3992
3993         search_copytools || error "No copytool found"
3994
3995         copytool_cleanup
3996         check_agent_unregistered $uuid
3997
3998         copytool_setup
3999         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
4000         check_agent_registered $uuid
4001
4002         copytool_cleanup
4003 }
4004 run_test 106 "Copytool register/unregister"
4005
4006 test_107() {
4007         # test needs a running copytool
4008         copytool_setup
4009         # create and archive file
4010         mkdir -p $DIR/$tdir
4011         local f1=$DIR/$tdir/$tfile
4012         local fid=$(copy_file /etc/passwd $f1)
4013         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4014         wait_request_state $fid ARCHIVE SUCCEED
4015         # shutdown and restart MDS
4016         fail $SINGLEMDS
4017         # check the copytool still gets messages from MDT
4018         local f2=$DIR/$tdir/2
4019         local fid=$(copy_file /etc/passwd $f2)
4020         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
4021         # main check of this sanity: this request MUST succeed
4022         wait_request_state $fid ARCHIVE SUCCEED
4023         copytool_cleanup
4024 }
4025 run_test 107 "Copytool re-register after MDS restart"
4026
4027 policy_set_and_test()
4028 {
4029         local change="$1"
4030         local target="$2"
4031         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
4032         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
4033         [[ "$policy" == "$target" ]] ||
4034                 error "Wrong policy after '$change': '$policy' != '$target'"
4035 }
4036
4037 test_109() {
4038         # to force default policy setting if error
4039         CDT_POLICY_HAD_CHANGED=true
4040
4041         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
4042         local default="NonBlockingRestore [NoRetryAction]"
4043         [[ "$policy" == "$default" ]] ||
4044                 error "default policy has changed,"\
4045                       " '$policy' != '$default' update the test"
4046         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
4047         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
4048         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
4049         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
4050         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
4051         # useless bacause we know but safer for futur changes to use real value
4052         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
4053         echo "Next set_param must failed"
4054         policy_set_and_test "wrong" "$policy"
4055
4056         # return to default
4057         echo "Back to default policy"
4058         cdt_set_sanity_policy
4059 }
4060 run_test 109 "Policy display/change"
4061
4062 test_110a() {
4063         # test needs a running copytool
4064         copytool_setup
4065
4066         mkdir -p $DIR/$tdir
4067
4068         copy2archive /etc/passwd $tdir/$tfile
4069
4070         local f=$DIR/$tdir/$tfile
4071         import_file $tdir/$tfile $f
4072         local fid=$(path2fid $f)
4073
4074         cdt_set_non_blocking_restore
4075         md5sum $f
4076         local st=$?
4077
4078         # cleanup
4079         wait_request_state $fid RESTORE SUCCEED
4080         cdt_clear_non_blocking_restore
4081
4082         # Test result
4083         [[ $st == 1 ]] ||
4084                 error "md5sum returns $st != 1, "\
4085                         "should also perror ENODATA (No data available)"
4086
4087         copytool_cleanup
4088 }
4089 run_test 110a "Non blocking restore policy (import case)"
4090
4091 test_110b() {
4092         # test needs a running copytool
4093         copytool_setup
4094
4095         mkdir -p $DIR/$tdir
4096         local f=$DIR/$tdir/$tfile
4097         local fid=$(copy_file /etc/passwd $f)
4098         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4099         wait_request_state $fid ARCHIVE SUCCEED
4100         $LFS hsm_release $f
4101
4102         cdt_set_non_blocking_restore
4103         md5sum $f
4104         local st=$?
4105
4106         # cleanup
4107         wait_request_state $fid RESTORE SUCCEED
4108         cdt_clear_non_blocking_restore
4109
4110         # Test result
4111         [[ $st == 1 ]] ||
4112                 error "md5sum returns $st != 1, "\
4113                         "should also perror ENODATA (No data available)"
4114
4115         copytool_cleanup
4116 }
4117 run_test 110b "Non blocking restore policy (release case)"
4118
4119 test_111a() {
4120         # test needs a running copytool
4121         copytool_setup
4122
4123         mkdir -p $DIR/$tdir
4124         copy2archive /etc/passwd $tdir/$tfile
4125
4126         local f=$DIR/$tdir/$tfile
4127
4128         import_file $tdir/$tfile $f
4129         local fid=$(path2fid $f)
4130
4131         cdt_set_no_retry
4132
4133         copytool_remove_backend $fid
4134
4135         $LFS hsm_restore $f
4136         wait_request_state $fid RESTORE FAILED
4137         local st=$?
4138
4139         # cleanup
4140         cdt_clear_no_retry
4141
4142         # Test result
4143         [[ $st == 0 ]] || error "Restore does not failed"
4144
4145         copytool_cleanup
4146 }
4147 run_test 111a "No retry policy (import case), restore will error"\
4148               " (No such file or directory)"
4149
4150 test_111b() {
4151         # test needs a running copytool
4152         copytool_setup
4153
4154         mkdir -p $DIR/$tdir
4155         local f=$DIR/$tdir/$tfile
4156         local fid=$(copy_file /etc/passwd $f)
4157         cdt_set_no_retry
4158         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4159         wait_request_state $fid ARCHIVE SUCCEED
4160         $LFS hsm_release $f
4161
4162         copytool_remove_backend $fid
4163
4164         $LFS hsm_restore $f
4165         wait_request_state $fid RESTORE FAILED
4166         local st=$?
4167
4168         # cleanup
4169         cdt_clear_no_retry
4170
4171         # Test result
4172         [[ $st == 0 ]] || error "Restore does not failed"
4173
4174         copytool_cleanup
4175 }
4176 run_test 111b "No retry policy (release case), restore will error"\
4177               " (No such file or directory)"
4178
4179 test_112() {
4180         # test needs a running copytool
4181         copytool_setup
4182
4183         mkdir -p $DIR/$tdir
4184         local f=$DIR/$tdir/$tfile
4185         local fid=$(copy_file /etc/passwd $f)
4186         cdt_disable
4187         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4188         local l=$($LFS hsm_action $f)
4189         echo $l
4190         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
4191
4192         # cleanup
4193         cdt_enable
4194         wait_request_state $fid ARCHIVE SUCCEED
4195
4196         # Test result
4197         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
4198
4199         copytool_cleanup
4200 }
4201 run_test 112 "State of recorded request"
4202
4203 test_200() {
4204         # test needs a running copytool
4205         copytool_setup
4206
4207         mkdir -p $DIR/$tdir
4208         local f=$DIR/$tdir/$tfile
4209         local fid
4210         fid=$(make_custom_file_for_progress $f 103 1048576)
4211         [ $? != 0 ] && skip "not enough free space" && return
4212
4213         # test with cdt on is made in test_221
4214         cdt_disable
4215         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4216         # wait archive to register at CDT
4217         wait_request_state $fid ARCHIVE WAITING
4218         $LFS hsm_cancel $f
4219         cdt_enable
4220         wait_request_state $fid ARCHIVE CANCELED
4221         wait_request_state $fid CANCEL SUCCEED
4222
4223         copytool_cleanup
4224 }
4225 run_test 200 "Register/Cancel archive"
4226
4227 test_201() {
4228         # test needs a running copytool
4229         copytool_setup
4230
4231         mkdir -p $DIR/$tdir
4232         local f=$DIR/$tdir/$tfile
4233         make_archive $tdir/$tfile
4234         import_file $tdir/$tfile $f
4235         local fid=$(path2fid $f)
4236
4237         # test with cdt on is made in test_222
4238         cdt_disable
4239         $LFS hsm_restore $f
4240         # wait restore to register at CDT
4241         wait_request_state $fid RESTORE WAITING
4242         $LFS hsm_cancel $f
4243         cdt_enable
4244         wait_request_state $fid RESTORE CANCELED
4245         wait_request_state $fid CANCEL SUCCEED
4246
4247         copytool_cleanup
4248 }
4249 run_test 201 "Register/Cancel restore"
4250
4251 test_202() {
4252         # test needs a running copytool
4253         copytool_setup
4254
4255         mkdir -p $DIR/$tdir
4256         local f=$DIR/$tdir/$tfile
4257         local fid
4258         fid=$(make_custom_file_for_progress $f 39 1000000)
4259         [ $? != 0 ] && skip "not enough free space" && return
4260
4261         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4262         wait_request_state $fid ARCHIVE SUCCEED
4263
4264         cdt_disable
4265         $LFS hsm_remove $f
4266         # wait remove to register at CDT
4267         wait_request_state $fid REMOVE WAITING
4268         $LFS hsm_cancel $f
4269         cdt_enable
4270         wait_request_state $fid REMOVE CANCELED
4271
4272         copytool_cleanup
4273 }
4274 run_test 202 "Register/Cancel remove"
4275
4276 test_220() {
4277         # test needs a running copytool
4278         copytool_setup
4279
4280         mkdir -p $DIR/$tdir
4281
4282         local f=$DIR/$tdir/$tfile
4283         local fid=$(copy_file /etc/passwd $f)
4284
4285         changelog_setup
4286
4287         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4288         wait_request_state $fid ARCHIVE SUCCEED
4289
4290         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4291         changelog_cleanup
4292
4293         local target=0x0
4294         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4295
4296         copytool_cleanup
4297 }
4298 run_test 220 "Changelog for archive"
4299
4300 test_220a() {
4301         # test needs a running copytool
4302         copytool_setup
4303
4304         mkdir -p $DIR/$tdir
4305
4306         local f=$DIR/$tdir/$tfile
4307         local fid=$(copy_file /etc/passwd $f)
4308
4309         changelog_setup
4310
4311         # block copytool operations to allow for HSM request to be
4312         # submitted and file be unlinked (CDT will find object removed)
4313         copytool_suspend
4314
4315         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4316
4317         # wait request to reach CT
4318         wait_request_state $fid ARCHIVE STARTED
4319
4320         rm -f $f
4321
4322         copytool_continue
4323
4324         wait_request_state $fid ARCHIVE FAILED
4325
4326         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4327         changelog_cleanup
4328
4329         # HE_ARCHIVE|ENOENT
4330         local target=0x2
4331         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4332
4333         copytool_cleanup
4334 }
4335 run_test 220a "Changelog for failed archive"
4336
4337 test_221() {
4338         # test needs a running copytool
4339         copytool_setup
4340
4341         mkdir -p $DIR/$tdir
4342
4343         local f=$DIR/$tdir/$tfile
4344         local fid
4345         fid=$(make_custom_file_for_progress $f 103 1048576)
4346         [ $? != 0 ] && skip "not enough free space" && return
4347
4348         changelog_setup
4349
4350         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4351         wait_request_state $fid ARCHIVE STARTED
4352         $LFS hsm_cancel $f
4353         wait_request_state $fid ARCHIVE CANCELED
4354         wait_request_state $fid CANCEL SUCCEED
4355
4356         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4357
4358         local target=0x7d
4359         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4360
4361         cleanup
4362 }
4363 run_test 221 "Changelog for archive canceled"
4364
4365 test_222a() {
4366         # test needs a running copytool
4367         copytool_setup
4368
4369         mkdir -p $DIR/$tdir
4370         copy2archive /etc/passwd $tdir/$tfile
4371
4372         local f=$DIR/$tdir/$tfile
4373         import_file $tdir/$tfile $f
4374         local fid=$(path2fid $f)
4375
4376         changelog_setup
4377
4378         $LFS hsm_restore $f
4379         wait_request_state $fid RESTORE SUCCEED
4380
4381         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4382
4383         local target=0x80
4384         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4385
4386         cleanup
4387 }
4388 run_test 222a "Changelog for explicit restore"
4389
4390 test_222b() {
4391         # test needs a running copytool
4392         copytool_setup
4393
4394         mkdir -p $DIR/$tdir
4395         local f=$DIR/$tdir/$tfile
4396         local fid=$(copy_file /etc/passwd $f)
4397
4398         changelog_setup
4399         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4400         wait_request_state $fid ARCHIVE SUCCEED
4401         $LFS hsm_release $f
4402
4403         md5sum $f
4404
4405         wait_request_state $fid RESTORE SUCCEED
4406
4407         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4408
4409         local target=0x80
4410         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4411
4412         cleanup
4413 }
4414 run_test 222b "Changelog for implicit restore"
4415
4416 test_222c() {
4417         # test needs a running copytool
4418         copytool_setup
4419
4420         mkdir -p $DIR/$tdir
4421         copy2archive /etc/passwd $tdir/$tfile
4422
4423         local f=$DIR/$tdir/$tfile
4424         import_file $tdir/$tfile $f
4425         local fid=$(path2fid $f)
4426
4427         changelog_setup
4428
4429         # block copytool operations to allow for HSM request to be
4430         # submitted and file be unlinked (CDT will find object removed)
4431         copytool_suspend
4432
4433         $LFS hsm_restore $f
4434
4435         # wait request to reach CT
4436         wait_request_state $fid RESTORE STARTED
4437
4438         rm -f $f
4439
4440         copytool_continue
4441
4442         wait_request_state $fid RESTORE FAILED
4443
4444         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4445
4446         # HE_RESTORE|ENOENT
4447         local target=0x82
4448         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4449
4450         cleanup
4451 }
4452 run_test 222c "Changelog for failed explicit restore"
4453
4454 test_222d() {
4455         # test needs a running copytool
4456         copytool_setup
4457
4458         mkdir -p $DIR/$tdir
4459         local f=$DIR/$tdir/$tfile
4460         local fid=$(copy_file /etc/passwd $f)
4461
4462         changelog_setup
4463         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4464         wait_request_state $fid ARCHIVE SUCCEED
4465         $LFS hsm_release $f
4466
4467         copytool_remove_backend $fid
4468         md5sum $f
4469
4470         wait_request_state $fid RESTORE FAILED
4471
4472         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4473
4474         # HE_RESTORE|ENOENT
4475         local target=0x82
4476         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
4477
4478         cleanup
4479 }
4480 run_test 222d "Changelog for failed implicit restore"
4481
4482 test_223a() {
4483         # test needs a running copytool
4484         copytool_setup
4485
4486         mkdir -p $DIR/$tdir
4487
4488         local f=$DIR/$tdir/$tfile
4489         make_archive $tdir/$tfile
4490
4491         changelog_setup
4492
4493         import_file $tdir/$tfile $f
4494         local fid=$(path2fid $f)
4495
4496         $LFS hsm_restore $f
4497         wait_request_state $fid RESTORE STARTED
4498         $LFS hsm_cancel $f
4499         wait_request_state $fid RESTORE CANCELED
4500         wait_request_state $fid CANCEL SUCCEED
4501
4502         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4503
4504         local target=0xfd
4505         [[ $flags == $target ]] ||
4506                 error "Changelog flag is $flags not $target"
4507
4508         cleanup
4509 }
4510 run_test 223a "Changelog for restore canceled (import case)"
4511
4512 test_223b() {
4513         # test needs a running copytool
4514         copytool_setup
4515
4516         mkdir -p $DIR/$tdir
4517
4518         local f=$DIR/$tdir/$tfile
4519         local fid
4520         fid=$(make_custom_file_for_progress $f 39 1000000)
4521         [ $? != 0 ] && skip "not enough free space" && return
4522
4523         changelog_setup
4524         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4525         wait_request_state $fid ARCHIVE SUCCEED
4526         $LFS hsm_release $f
4527         $LFS hsm_restore $f
4528         wait_request_state $fid RESTORE STARTED
4529         $LFS hsm_cancel $f
4530         wait_request_state $fid RESTORE CANCELED
4531         wait_request_state $fid CANCEL SUCCEED
4532
4533         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4534
4535         local target=0xfd
4536         [[ $flags == $target ]] ||
4537                 error "Changelog flag is $flags not $target"
4538
4539         cleanup
4540 }
4541 run_test 223b "Changelog for restore canceled (release case)"
4542
4543 test_224() {
4544         # test needs a running copytool
4545         copytool_setup
4546
4547         mkdir -p $DIR/$tdir
4548
4549         local f=$DIR/$tdir/$tfile
4550         local fid=$(copy_file /etc/passwd $f)
4551
4552         changelog_setup
4553         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4554         wait_request_state $fid ARCHIVE SUCCEED
4555
4556         $LFS hsm_remove $f
4557         wait_request_state $fid REMOVE SUCCEED
4558
4559         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4560
4561         local target=0x200
4562         [[ $flags == $target ]] ||
4563                 error "Changelog flag is $flags not $target"
4564
4565         cleanup
4566 }
4567 run_test 224 "Changelog for remove"
4568
4569 test_224a() {
4570         # test needs a running copytool
4571         copytool_setup
4572
4573         mkdir -p $DIR/$tdir
4574
4575         local f=$DIR/$tdir/$tfile
4576         local fid=$(copy_file /etc/passwd $f)
4577
4578         changelog_setup
4579         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4580         wait_request_state $fid ARCHIVE SUCCEED
4581
4582         copytool_remove_backend $fid
4583
4584         # block copytool operations to allow for HSM request to be
4585         # submitted and file be unlinked (CDT will find object removed)
4586         copytool_suspend
4587
4588         $LFS hsm_remove $f
4589
4590         # wait for request to reach CT
4591         wait_request_state $fid REMOVE STARTED
4592
4593         rm -f $f
4594
4595         copytool_continue
4596
4597         wait_request_state $fid REMOVE FAILED
4598
4599         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4600
4601         # HE_REMOVE|ENOENT
4602         local target=0x202
4603         [[ $flags == $target ]] ||
4604                 error "Changelog flag is $flags not $target"
4605
4606         cleanup
4607 }
4608 run_test 224a "Changelog for failed remove"
4609
4610 test_225() {
4611         # test needs a running copytool
4612         copytool_setup
4613
4614         # test is not usable because remove request is too fast
4615         # so it is always finished before cancel can be done ...
4616         echo "Test disabled"
4617         copytool_cleanup
4618         return 0
4619
4620         mkdir -p $DIR/$tdir
4621         local f=$DIR/$tdir/$tfile
4622         local fid
4623         fid=$(make_custom_file_for_progress $f 39 1000000)
4624         [ $? != 0 ] && skip "not enough free space" && return
4625
4626         changelog_setup
4627         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4628         wait_request_state $fid ARCHIVE SUCCEED
4629
4630         # if cdt is on, it can serve too quickly the request
4631         cdt_disable
4632         $LFS hsm_remove $f
4633         $LFS hsm_cancel $f
4634         cdt_enable
4635         wait_request_state $fid REMOVE CANCELED
4636         wait_request_state $fid CANCEL SUCCEED
4637
4638         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4639         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
4640                 tail -n 1 | awk '{print $5}')
4641
4642         local target=0x27d
4643         [[ $flags == $target ]] ||
4644                 error "Changelog flag is $flags not $target"
4645
4646         cleanup
4647 }
4648 run_test 225 "Changelog for remove canceled"
4649
4650 test_226() {
4651         # test needs a running copytool
4652         copytool_setup
4653
4654         mkdir -p $DIR/$tdir
4655
4656         local f1=$DIR/$tdir/$tfile-1
4657         local f2=$DIR/$tdir/$tfile-2
4658         local f3=$DIR/$tdir/$tfile-3
4659         local fid1=$(copy_file /etc/passwd $f1)
4660         local fid2=$(copy_file /etc/passwd $f2)
4661         copy_file /etc/passwd $f3
4662
4663         changelog_setup
4664         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4665         wait_request_state $fid1 ARCHIVE SUCCEED
4666
4667         $LFS hsm_archive $f2
4668         wait_request_state $fid2 ARCHIVE SUCCEED
4669
4670         rm $f1 || error "rm $f1 failed"
4671
4672         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
4673
4674         local target=0x3
4675         [[ $flags == $target ]] ||
4676                 error "Changelog flag is $flags not $target"
4677
4678         mv $f3 $f2 || error "mv $f3 $f2 failed"
4679
4680         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4681
4682         target=0x3
4683         [[ $flags == $target ]] ||
4684                 error "Changelog flag is $flags not $target"
4685
4686         cleanup
4687 }
4688 run_test 226 "changelog for last rm/mv with exiting archive"
4689
4690 check_flags_changes() {
4691         local f=$1
4692         local fid=$2
4693         local hsm_flag=$3
4694         local fst=$4
4695         local cnt=$5
4696
4697         local target=0x280
4698         $LFS hsm_set --$hsm_flag $f ||
4699                 error "Cannot set $hsm_flag on $f"
4700         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4701         local seen=${#flags[*]}
4702         cnt=$((fst + cnt))
4703         [[ $seen == $cnt ]] ||
4704                 error "set $hsm_flag: Changelog events $seen != $cnt"
4705         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4706                 error "set $hsm_flag: Changelog flags are "\
4707                         "${flags[$((cnt - 1))]} not $target"
4708
4709         $LFS hsm_clear --$hsm_flag $f ||
4710                 error "Cannot clear $hsm_flag on $f"
4711         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4712         seen=${#flags[*]}
4713         cnt=$(($cnt + 1))
4714         [[ $cnt == $seen ]] ||
4715                 error "clear $hsm_flag: Changelog events $seen != $cnt"
4716
4717         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4718                 error "clear $hsm_flag: Changelog flag is "\
4719                         "${flags[$((cnt - 1))]} not $target"
4720 }
4721
4722 test_227() {
4723         # test needs a running copytool
4724         copytool_setup
4725         changelog_setup
4726
4727         mkdir -p $DIR/$tdir
4728         typeset -a flags
4729
4730         for i in norelease noarchive exists archived
4731         do
4732                 local f=$DIR/$tdir/$tfile-$i
4733                 local fid=$(copy_file /etc/passwd $f)
4734                 check_flags_changes $f $fid $i 0 1
4735         done
4736
4737         f=$DIR/$tdir/$tfile---lost
4738         fid=$(copy_file /etc/passwd $f)
4739         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4740         wait_request_state $fid ARCHIVE SUCCEED
4741         check_flags_changes $f $fid lost 3 1
4742
4743         cleanup
4744 }
4745 run_test 227 "changelog when explicit setting of HSM flags"
4746
4747 test_228() {
4748         # test needs a running copytool
4749         copytool_setup
4750
4751         local fid=$(make_small_sync $DIR/$tfile)
4752         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4753         wait_request_state $fid ARCHIVE SUCCEED
4754
4755         $LFS hsm_release $DIR/$tfile
4756         check_hsm_flags $DIR/$tfile "0x0000000d"
4757
4758         filefrag $DIR/$tfile | grep " 1 extent found" ||
4759                 error "filefrag on released file must return only one extent"
4760
4761         # only newer versions of cp detect sparse files by stat/FIEMAP
4762         # (LU-2580)
4763         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4764                 error "copying $DIR/$tfile"
4765         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4766
4767         $LFS hsm_release $DIR/$tfile
4768         check_hsm_flags $DIR/$tfile "0x0000000d"
4769
4770         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4771
4772         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4773                 error "tar failed"
4774         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4775                 error "comparing untarred $DIR/$tfile"
4776
4777         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4778                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4779         copytool_cleanup
4780 }
4781 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4782
4783 test_250() {
4784         # test needs a running copytool
4785         copytool_setup
4786
4787         mkdir -p $DIR/$tdir
4788         local maxrequest=$(get_hsm_param max_requests)
4789         local rqcnt=$(($maxrequest * 3))
4790         local i=""
4791
4792         cdt_disable
4793         for i in $(seq -w 1 $rqcnt); do
4794                 rm -f $DIR/$tdir/$i
4795                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
4796         done
4797         # we do it in 2 steps, so all requests arrive at the same time
4798         for i in $(seq -w 1 $rqcnt); do
4799                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
4800         done
4801         cdt_enable
4802         local cnt=$rqcnt
4803         local wt=$rqcnt
4804         while [[ $cnt != 0 || $wt != 0 ]]; do
4805                 sleep 1
4806                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4807                         $HSM_PARAM.actions |\
4808                         grep STARTED | grep -v CANCEL | wc -l")
4809                 [[ $cnt -le $maxrequest ]] ||
4810                         error "$cnt > $maxrequest too many started requests"
4811                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4812                         $HSM_PARAM.actions |\
4813                         grep WAITING | wc -l")
4814                 echo "max=$maxrequest started=$cnt waiting=$wt"
4815         done
4816
4817         copytool_cleanup
4818 }
4819 run_test 250 "Coordinator max request"
4820
4821 test_251() {
4822         # test needs a running copytool
4823         copytool_setup
4824
4825         mkdir -p $DIR/$tdir
4826         local f=$DIR/$tdir/$tfile
4827         local fid
4828         fid=$(make_custom_file_for_progress $f 103 1048576)
4829         [ $? != 0 ] && skip "not enough free space" && return
4830
4831         cdt_disable
4832         # to have a short test
4833         local old_to=$(get_hsm_param active_request_timeout)
4834         set_hsm_param active_request_timeout 4
4835         # to be sure the cdt will wake up frequently so
4836         # it will be able to cancel the "old" request
4837         local old_loop=$(get_hsm_param loop_period)
4838         set_hsm_param loop_period 2
4839         cdt_enable
4840
4841         # clear locks to avoid extra delay caused by flush/cancel
4842         # and thus prevent early copytool death to timeout.
4843         cancel_lru_locks osc
4844
4845         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4846         wait_request_state $fid ARCHIVE STARTED
4847         sleep 5
4848         wait_request_state $fid ARCHIVE CANCELED
4849
4850         set_hsm_param active_request_timeout $old_to
4851         set_hsm_param loop_period $old_loop
4852
4853         copytool_cleanup
4854 }
4855 run_test 251 "Coordinator request timeout"
4856
4857 test_252() {
4858         # test needs a running copytool
4859         copytool_setup
4860
4861         mkdir -p $DIR/$tdir
4862         local f=$DIR/$tdir/$tfile
4863         local fid=$(make_custom_file_for_progress $f 103 1048576)
4864
4865         cdt_disable
4866         # to have a short test
4867         local old_to=$(get_hsm_param active_request_timeout)
4868         set_hsm_param active_request_timeout 20
4869         # to be sure the cdt will wake up frequently so
4870         # it will be able to cancel the "old" request
4871         local old_loop=$(get_hsm_param loop_period)
4872         set_hsm_param loop_period 2
4873         cdt_enable
4874
4875         # clear locks to avoid extra delay caused by flush/cancel
4876         # and thus prevent early copytool death to timeout.
4877         cancel_lru_locks osc
4878
4879         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4880         wait_request_state $fid ARCHIVE STARTED
4881         rm -f $f
4882
4883         # wait but less than active_request_timeout+grace_delay
4884         sleep 25
4885         wait_request_state $fid ARCHIVE CANCELED
4886
4887         set_hsm_param active_request_timeout $old_to
4888         set_hsm_param loop_period $old_loop
4889
4890         copytool_cleanup
4891 }
4892 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4893
4894 test_300() {
4895         # the only way to test ondisk conf is to restart MDS ...
4896         echo "Stop coordinator and remove coordinator state at mount"
4897         # stop coordinator
4898         cdt_shutdown
4899         # clean on disk conf set by default
4900         cdt_clear_mount_state
4901         cdt_check_state stopped
4902
4903         # check cdt still off after umount/remount
4904         fail $SINGLEMDS
4905         cdt_check_state stopped
4906
4907         echo "Set coordinator start at mount, and start coordinator"
4908         cdt_set_mount_state enabled
4909
4910         # check cdt is on
4911         cdt_check_state enabled
4912
4913         # check cdt still on after umount/remount
4914         fail $SINGLEMDS
4915         cdt_check_state enabled
4916
4917         # we are back to original state (cdt started at mount)
4918 }
4919 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4920
4921 test_301() {
4922         local ai=$(get_hsm_param default_archive_id)
4923         local new=$((ai + 1))
4924
4925         set_hsm_param default_archive_id $new -P
4926         fail $SINGLEMDS
4927         local res=$(get_hsm_param default_archive_id)
4928
4929         # clear value
4930         set_hsm_param default_archive_id "" "-P -d"
4931
4932         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4933 }
4934 run_test 301 "HSM tunnable are persistent"
4935
4936 test_302() {
4937         local ai=$(get_hsm_param default_archive_id)
4938         local new=$((ai + 1))
4939
4940         # stop coordinator
4941         cdt_shutdown
4942
4943         set_hsm_param default_archive_id $new -P
4944
4945         local mdtno
4946         for mdtno in $(seq 1 $MDSCOUNT); do
4947                 fail mds${mdtno}
4948         done
4949
4950         # check cdt is on
4951         cdt_check_state enabled
4952
4953         local res=$(get_hsm_param default_archive_id)
4954
4955         # clear value
4956         set_hsm_param default_archive_id "" "-P -d"
4957
4958         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4959 }
4960 run_test 302 "HSM tunnable are persistent when CDT is off"
4961
4962 test_400() {
4963         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4964
4965         copytool_setup
4966
4967         mkdir -p $DIR/$tdir
4968
4969         local dir_mdt0=$DIR/$tdir/mdt0
4970         local dir_mdt1=$DIR/$tdir/mdt1
4971
4972         # create 1 dir per MDT
4973         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4974         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4975
4976         # create 1 file in each MDT
4977         local fid1=$(make_small $dir_mdt0/$tfile)
4978         local fid2=$(make_small $dir_mdt1/$tfile)
4979
4980         # check that hsm request on mdt0 is sent to the right MDS
4981         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4982         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4983                 echo "archive successful on mdt0"
4984
4985         # check that hsm request on mdt1 is sent to the right MDS
4986         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
4987         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4988                 echo "archive successful on mdt1"
4989
4990         copytool_cleanup
4991         # clean test files and directories
4992         rm -rf $dir_mdt0 $dir_mdt1
4993 }
4994 run_test 400 "Single request is sent to the right MDT"
4995
4996 test_401() {
4997         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4998
4999         copytool_setup
5000
5001         mkdir -p $DIR/$tdir
5002
5003         local dir_mdt0=$DIR/$tdir/mdt0
5004         local dir_mdt1=$DIR/$tdir/mdt1
5005
5006         # create 1 dir per MDT
5007         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5008         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5009
5010         # create 1 file in each MDT
5011         local fid1=$(make_small $dir_mdt0/$tfile)
5012         local fid2=$(make_small $dir_mdt1/$tfile)
5013
5014         # check that compound requests are shunt to the rights MDTs
5015         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
5016                 error "lfs hsm_archive"
5017         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5018                 echo "archive successful on mdt0"
5019         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5020                 echo "archive successful on mdt1"
5021
5022         copytool_cleanup
5023         # clean test files and directories
5024         rm -rf $dir_mdt0 $dir_mdt1
5025 }
5026 run_test 401 "Compound requests split and sent to their respective MDTs"
5027
5028 mdc_change_state() # facet, MDT_pattern, activate|deactivate
5029 {
5030         local facet=$1
5031         local pattern="$2"
5032         local state=$3
5033         local node=$(facet_active_host $facet)
5034         local mdc
5035         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
5036                         awk '{print $4}'); do
5037                 echo "$3 $mdc on $node"
5038                 do_facet $facet "$LCTL --device $mdc $state" || return 1
5039         done
5040 }
5041
5042 test_402a() {
5043         # make sure there is no running copytool
5044         copytool_cleanup
5045
5046         # deactivate all mdc on agent1
5047         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
5048
5049         HSMTOOL_NOERROR=true copytool_setup $SINGLEAGT
5050
5051         check_agent_unregistered "uuid" # match any agent
5052
5053         # no expected running copytool
5054         search_copytools $agent && error "Copytool start should have failed"
5055
5056         # reactivate MDCs
5057         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
5058 }
5059 run_test 402a "Copytool start fails if all MDTs are inactive"
5060
5061 test_402b() {
5062         copytool_setup
5063
5064         mkdir -p $DIR/$tdir
5065
5066         local f=$DIR/$tdir/$tfile
5067         touch $f || error "touch $f failed"
5068         local fid=$(path2fid $f)
5069
5070 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
5071         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
5072         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5073
5074         # give time for CDT to send request and to keep it for retry
5075         wait_for_loop_period
5076
5077         wait_request_state $fid ARCHIVE WAITING
5078
5079         do_facet $SINGLEAGT lctl set_param fail_loc=0
5080
5081         # request should succeed now
5082         wait_request_state $fid ARCHIVE SUCCEED
5083
5084         copytool_cleanup
5085 }
5086 run_test 402b "CDT must retry request upon slow start of CT"
5087
5088 test_403() {
5089         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5090
5091         # make sure there is no running copytool
5092         copytool_cleanup
5093
5094         local agent=$(facet_active_host $SINGLEAGT)
5095
5096         # deactivate all mdc for MDT0001
5097         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5098
5099         copytool_setup
5100         local uuid=$(get_agent_uuid $agent)
5101         # check the agent is registered on MDT0000, and not on MDT0001
5102         check_agent_registered_by_mdt $uuid 0
5103         check_agent_unregistered_by_mdt $uuid 1
5104
5105         # check running copytool process
5106         search_copytools $agent || error "No running copytools on $agent"
5107
5108         # reactivate all mdc for MDT0001
5109         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5110
5111         # make sure the copytool is now registered to all MDTs
5112         check_agent_registered $uuid
5113
5114         copytool_cleanup
5115 }
5116 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
5117
5118 test_404() {
5119         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5120
5121         copytool_setup
5122
5123         # create files on both MDT0000 and MDT0001
5124         mkdir -p $DIR/$tdir
5125
5126         local dir_mdt0=$DIR/$tdir/mdt0
5127         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5128
5129         # create 1 file on mdt0
5130         local fid1=$(make_small $dir_mdt0/$tfile)
5131
5132         # deactivate all mdc for MDT0001
5133         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5134
5135         # send an HSM request for files in MDT0000
5136         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5137
5138         # check for completion of files in MDT0000
5139         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5140                 echo "archive successful on mdt0"
5141
5142         # reactivate all mdc for MDT0001
5143         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5144
5145         copytool_cleanup
5146         # clean test files and directories
5147         rm -rf $dir_mdt0
5148 }
5149 run_test 404 "Inactive MDT does not block requests for active MDTs"
5150
5151 test_405() {
5152         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5153
5154         copytool_setup
5155
5156         mkdir -p $DIR/$tdir
5157
5158         local striped_dir=$DIR/$tdir/striped_dir
5159
5160         # create striped dir on all of MDTs
5161         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
5162
5163         local fid1=$(make_small_sync $striped_dir/${tfile}_0)
5164         local fid2=$(make_small_sync $striped_dir/${tfile}_1)
5165         local fid3=$(make_small_sync $striped_dir/${tfile}_2)
5166         local fid4=$(make_small_sync $striped_dir/${tfile}_3)
5167
5168         local idx1=$($LFS getstripe -M $striped_dir/${tfile}_0)
5169         local idx2=$($LFS getstripe -M $striped_dir/${tfile}_1)
5170         local idx3=$($LFS getstripe -M $striped_dir/${tfile}_2)
5171         local idx4=$($LFS getstripe -M $striped_dir/${tfile}_3)
5172
5173         # check that compound requests are shunt to the rights MDTs
5174         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5175                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5176                 error "lfs hsm_archive"
5177
5178         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5179                 echo "archive successful on $fid1"
5180         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5181                 echo "archive successful on $fid2"
5182         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5183                 echo "archive successful on $fid3"
5184         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5185                 echo "archive successful on $fid4"
5186
5187         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5188         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5189         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5190         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5191
5192         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5193         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5194         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5195         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5196
5197         copytool_cleanup
5198 }
5199 run_test 405 "archive and release under striped directory"
5200
5201 test_406() {
5202         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5203
5204         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
5205                 skip "need MDS version at least 2.7.64" && return 0
5206
5207         local fid
5208         local mdt_index
5209
5210         copytool_setup
5211         mkdir -p $DIR/$tdir
5212         fid=$(make_small $DIR/$tdir/$tfile)
5213         echo "old fid $fid"
5214
5215         $LFS hsm_archive $DIR/$tdir/$tfile
5216         wait_request_state "$fid" ARCHIVE SUCCEED
5217         $LFS hsm_release $DIR/$tdir/$tfile
5218
5219         # Should migrate $tdir but not $tfile.
5220         $LFS mv -M1 $DIR/$tdir &&
5221                 error "migrating HSM an archived file should fail"
5222
5223         $LFS hsm_restore $DIR/$tdir/$tfile
5224         wait_request_state "$fid" RESTORE SUCCEED
5225
5226         $LFS hsm_remove $DIR/$tdir/$tfile
5227         wait_request_state "$fid" REMOVE SUCCEED
5228
5229         cat $DIR/$tdir/$tfile > /dev/null ||
5230                 error "cannot read $DIR/$tdir/$tfile"
5231
5232         $LFS mv -M1 $DIR/$tdir ||
5233                 error "cannot complete migration after HSM remove"
5234
5235         mdt_index=$($LFS getstripe -M $DIR/$tdir)
5236         if ((mdt_index != 1)); then
5237                 error "expected MDT index 1, got $mdt_index"
5238         fi
5239
5240         # Refresh fid after migration.
5241         fid=$(path2fid $DIR/$tdir/$tfile)
5242         echo "new fid $fid"
5243
5244         $LFS hsm_archive $DIR/$tdir/$tfile
5245         wait_request_state "$fid" ARCHIVE SUCCEED 1
5246
5247         lctl set_param debug=+trace
5248         $LFS hsm_release $DIR/$tdir/$tfile ||
5249                 error "cannot release $DIR/$tdir/$tfile"
5250
5251         $LFS hsm_restore $DIR/$tdir/$tfile
5252         wait_request_state "$fid" RESTORE SUCCEED 1
5253
5254         cat $DIR/$tdir/$tfile > /dev/null ||
5255                 error "cannot read $DIR/$tdir/$tfile"
5256
5257         copytool_cleanup
5258 }
5259 run_test 406 "attempting to migrate HSM archived files is safe"
5260
5261 test_407() {
5262         needclients 2 || return 0
5263         # test needs a running copytool
5264         copytool_setup
5265
5266         mkdir -p $DIR/$tdir
5267
5268         local f=$DIR/$tdir/$tfile
5269         local f2=$DIR2/$tdir/$tfile
5270         local fid
5271         fid=$(make_custom_file_for_progress $f 39 1000000)
5272         [ $? != 0 ] && skip "not enough free space" && return
5273
5274         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5275         wait_request_state $fid ARCHIVE SUCCEED
5276         $LFS hsm_release $f
5277
5278 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5279         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5280
5281         md5sum $f &
5282         # 1st request holds layout lock while appropriate
5283         # RESTORE record is still not added to llog
5284         md5sum $f2 &
5285         sleep 2
5286
5287         # after umount hsm_actions->O/x/x log shouldn't have
5288         # double RESTORE records like below
5289         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5290         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5291         sleep 30 &&
5292                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5293         fail $SINGLEMDS
5294
5295         wait_request_state $fid RESTORE SUCCEED
5296         copytool_cleanup
5297 }
5298 run_test 407 "Check for double RESTORE records in llog"
5299
5300 test_500()
5301 {
5302         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
5303                 skip "HSM migrate is not supported" && return
5304
5305         # Stop the existing copytool
5306         copytool_cleanup
5307
5308         test_mkdir -p $DIR/$tdir
5309         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
5310 }
5311 run_test 500 "various LLAPI HSM tests"
5312
5313 copytool_cleanup
5314
5315 complete $SECONDS
5316 check_and_cleanup_lustre
5317 exit_status