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