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