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