Whamcloud - gitweb
0126b78addc9626e3e69c1ec941b02abefa5e5aa
[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
1435         zconf_mount $(facet_host $SINGLEAGT) $MOUNT3 ||
1436                 error "cannot mount $MOUNT3 on $SINGLEAGT"
1437
1438         trap cleanup_test_12q EXIT
1439
1440         # test needs a running copytool
1441         copytool_setup $SINGLEAGT $MOUNT3
1442
1443         mkdir $DIR/$tdir
1444         local f=$DIR/$tdir/$tfile
1445         local f2=$DIR2/$tdir/$tfile
1446         local fid=$(make_small $f)
1447         local orig_size=$(stat -c "%s" $f)
1448
1449         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1450         wait_request_state $fid ARCHIVE SUCCEED
1451
1452         $LFS hsm_release $f || error "could not release file"
1453         check_hsm_flags $f "0x0000000d"
1454
1455         search_and_kill_copytool
1456         sleep 5
1457         search_copytools && error "Copytool should have stopped"
1458
1459         cat $f > /dev/null &
1460
1461         # wait a bit to allow implicit restore request to be handled.
1462         # if not, next stat would also block on layout-lock.
1463         sleep 5
1464
1465         local size=$(stat -c "%s" $f2)
1466         [ $size -eq $orig_size ] ||
1467                 error "$f2: wrong size after archive: $size != $orig_size"
1468
1469         HSM_ARCHIVE_PURGE=false copytool_setup $SINGLEAGT /mnt/lustre3
1470
1471         wait
1472
1473         size=$(stat -c "%s" $f)
1474         [ $size -eq $orig_size ] ||
1475                 error "$f: wrong size after restore: $size != $orig_size"
1476
1477         size=$(stat -c "%s" $f2)
1478         [ $size -eq $orig_size ] ||
1479                 error "$f2: wrong size after restore: $size != $orig_size"
1480
1481         :>$f
1482
1483         size=$(stat -c "%s" $f)
1484         [ $size -eq 0 ] ||
1485                 error "$f: wrong size after overwrite: $size != 0"
1486
1487         size=$(stat -c "%s" $f2)
1488         [ $size -eq 0 ] ||
1489                 error "$f2: wrong size after overwrite: $size != 0"
1490
1491         copytool_cleanup
1492         zconf_umount $(facet_host $SINGLEAGT) $MOUNT3 ||
1493                 error "cannot umount $MOUNT3 on $SINGLEAGT"
1494 }
1495 run_test 12q "file attributes are refreshed after restore"
1496
1497 test_13() {
1498         # test needs a running copytool
1499         copytool_setup
1500
1501         local ARC_SUBDIR="import.orig"
1502         local d=""
1503         local f=""
1504
1505         # populate directory to be imported
1506         for d in $(seq 1 10); do
1507                 local CURR_DIR="$HSM_ARCHIVE/$ARC_SUBDIR/dir.$d"
1508                 do_facet $SINGLEAGT mkdir -p "$CURR_DIR"
1509                 for f in $(seq 1 10); do
1510                         CURR_FILE="$CURR_DIR/$tfile.$f"
1511                         # write file-specific data
1512                         do_facet $SINGLEAGT \
1513                                 "echo d=$d, f=$f, dir=$CURR_DIR, "\
1514                                         "file=$CURR_FILE > $CURR_FILE"
1515                 done
1516         done
1517         # import to Lustre
1518         import_file "$ARC_SUBDIR" $DIR/$tdir
1519         # diff lustre content and origin (triggers file restoration)
1520         # there must be 10x10 identical files, and no difference
1521         local cnt_ok=$(do_facet $SINGLEAGT diff -rs $HSM_ARCHIVE/$ARC_SUBDIR \
1522                        $DIR/$tdir/$ARC_SUBDIR | grep identical | wc -l)
1523         local cnt_diff=$(do_facet $SINGLEAGT diff -r $HSM_ARCHIVE/$ARC_SUBDIR \
1524                          $DIR/$tdir/$ARC_SUBDIR | wc -l)
1525
1526         [ $cnt_diff -eq 0 ] ||
1527                 error "$cnt_diff imported files differ from read data"
1528         [ $cnt_ok -eq 100 ] ||
1529                 error "not enough identical files ($cnt_ok != 100)"
1530
1531         copytool_cleanup
1532 }
1533 run_test 13 "Recursively import and restore a directory"
1534
1535 test_14() {
1536         # test needs a running copytool
1537         copytool_setup
1538
1539         # archive a file
1540         mkdir -p $DIR/$tdir
1541         local f=$DIR/$tdir/$tfile
1542         local fid=$(make_small $f)
1543         local sum=$(md5sum $f | awk '{print $1}')
1544         $LFS hsm_archive $f || error "could not archive file"
1545         wait_request_state $fid ARCHIVE SUCCEED
1546
1547         # delete the file
1548         rm -f $f
1549         # create released file (simulate llapi_hsm_import call)
1550         touch $f
1551         local fid2=$(path2fid $f)
1552         $LFS hsm_set --archived --exists $f || error "could not force hsm flags"
1553         $LFS hsm_release $f || error "could not release file"
1554
1555         # rebind the archive to the newly created file
1556         echo "rebind $fid to $fid2"
1557
1558         do_facet $SINGLEAGT \
1559                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1560                  --rebind $fid $fid2 $DIR" || error "could not rebind file"
1561
1562         # restore file and compare md5sum
1563         local sum2=$(md5sum $f | awk '{print $1}')
1564
1565         [[ $sum == $sum2 ]] || error "md5sum mismatch after restore"
1566
1567         copytool_cleanup
1568 }
1569 run_test 14 "Rebind archived file to a new fid"
1570
1571 test_15() {
1572         # test needs a running copytool
1573         copytool_setup
1574
1575         # archive files
1576         mkdir -p $DIR/$tdir
1577         local f=$DIR/$tdir/$tfile
1578         local count=5
1579         local tmpfile=$SHARED_DIRECTORY/tmp.$$
1580
1581         local fids=()
1582         local sums=()
1583         for i in $(seq 1 $count); do
1584                 fids[$i]=$(make_small $f.$i)
1585                 sums[$i]=$(md5sum $f.$i | awk '{print $1}')
1586                 $LFS hsm_archive $f.$i || error "could not archive file"
1587         done
1588         wait_all_done $(($count*60))
1589
1590         :>$tmpfile
1591         # delete the files
1592         for i in $(seq 1 $count); do
1593                 rm -f $f.$i
1594                 touch $f.$i
1595                 local fid2=$(path2fid $f.$i)
1596                 # add the rebind operation to the list
1597                 echo ${fids[$i]} $fid2 >> $tmpfile
1598
1599                 # set it released (simulate llapi_hsm_import call)
1600                 $LFS hsm_set --archived --exists $f.$i ||
1601                         error "could not force hsm flags"
1602                 $LFS hsm_release $f.$i || error "could not release file"
1603         done
1604         nl=$(wc -l < $tmpfile)
1605         [[ $nl == $count ]] || error "$nl files in list, $count expected"
1606
1607         echo "rebind list of files"
1608         do_facet $SINGLEAGT \
1609                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1610                  --rebind $tmpfile $DIR" || error "could not rebind file list"
1611
1612         # restore files and compare md5sum
1613         for i in $(seq 1 $count); do
1614                 local sum2=$(md5sum $f.$i | awk '{print $1}')
1615                 [[ $sum2 == ${sums[$i]} ]] ||
1616                     error "md5sum mismatch after restore ($sum2 != ${sums[$i]})"
1617         done
1618
1619         rm -f $tmpfile
1620         copytool_cleanup
1621 }
1622 run_test 15 "Rebind a list of files"
1623
1624 test_16() {
1625         # test needs a running copytool
1626         copytool_setup
1627
1628         local ref=/tmp/ref
1629         # create a known size file so we can verify transfer speed
1630         # 20 MB <-> 20s
1631         local goal=20
1632         dd if=/dev/zero of=$ref bs=1M count=20
1633
1634         mkdir -p $DIR/$tdir
1635         local f=$DIR/$tdir/$tfile
1636         local fid=$(copy_file $ref $f)
1637         rm $ref
1638         local start=$(date +%s)
1639         $LFS hsm_archive $f
1640         wait_request_state $fid ARCHIVE SUCCEED
1641         local end=$(date +%s)
1642         local duration=$((end - start))
1643
1644         [[ $duration -ge $goal ]] ||
1645                 error "Transfer is too fast $duration < $goal"
1646
1647         copytool_cleanup
1648 }
1649 run_test 16 "Test CT bandwith control option"
1650
1651 test_20() {
1652         mkdir -p $DIR/$tdir
1653
1654         local f=$DIR/$tdir/$tfile
1655         touch $f || error "touch $f failed"
1656
1657         # Could not release a non-archived file
1658         $LFS hsm_release $f && error "release should not succeed"
1659
1660         # For following tests, we must test them with HS_ARCHIVED set
1661         $LFS hsm_set --exists --archived $f || error "could not add flag"
1662
1663         # Could not release a file if no-release is set
1664         $LFS hsm_set --norelease $f || error "could not add flag"
1665         $LFS hsm_release $f && error "release should not succeed"
1666         $LFS hsm_clear --norelease $f || error "could not remove flag"
1667
1668         # Could not release a file if lost
1669         $LFS hsm_set --lost $f || error "could not add flag"
1670         $LFS hsm_release $f && error "release should not succeed"
1671         $LFS hsm_clear --lost $f || error "could not remove flag"
1672
1673         # Could not release a file if dirty
1674         $LFS hsm_set --dirty $f || error "could not add flag"
1675         $LFS hsm_release $f && error "release should not succeed"
1676         $LFS hsm_clear --dirty $f || error "could not remove flag"
1677 }
1678 run_test 20 "Release is not permitted"
1679
1680 test_21() {
1681         # test needs a running copytool
1682         copytool_setup
1683
1684         mkdir -p $DIR/$tdir
1685         local f=$DIR/$tdir/test_release
1686
1687         # Create a file and check its states
1688         local fid=$(make_small $f)
1689         check_hsm_flags $f "0x00000000"
1690
1691         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1692         # used until file is flushed to disk
1693         if [  $(facet_fstype ost1) == "zfs" ]; then
1694             # this causes an OST_SYNC rpc to be sent
1695             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1696             # clear locks to reread file data
1697             cancel_lru_locks osc
1698         fi
1699
1700         local orig_size=$(stat -c "%s" $f)
1701         local orig_blocks=$(stat -c "%b" $f)
1702
1703         start_full_debug_logging
1704
1705         $LFS hsm_archive $f || error "could not archive file"
1706         wait_request_state $fid ARCHIVE SUCCEED
1707
1708         local blocks=$(stat -c "%b" $f)
1709         [ $blocks -eq $orig_blocks ] ||
1710                 error "$f: wrong block number after archive: " \
1711                       "$blocks != $orig_blocks"
1712         local size=$(stat -c "%s" $f)
1713         [ $size -eq $orig_size ] ||
1714                 error "$f: wrong size after archive: $size != $orig_size"
1715
1716         # Release and check states
1717         $LFS hsm_release $f || error "could not release file"
1718         check_hsm_flags $f "0x0000000d"
1719
1720         blocks=$(stat -c "%b" $f)
1721         [ $blocks -gt 5 ] &&
1722                 error "$f: too many blocks after release: $blocks > 5"
1723         size=$(stat -c "%s" $f)
1724         [ $size -ne $orig_size ] &&
1725                 error "$f: wrong size after release: $size != $orig_size"
1726
1727         # Check we can release an file without stripe info
1728         f=$f.nolov
1729         $MCREATE $f
1730         fid=$(path2fid $f)
1731         check_hsm_flags $f "0x00000000"
1732         $LFS hsm_archive $f || error "could not archive file"
1733         wait_request_state $fid ARCHIVE SUCCEED
1734
1735         # Release and check states
1736         $LFS hsm_release $f || error "could not release file"
1737         check_hsm_flags $f "0x0000000d"
1738
1739         # Release again a file that is already released is OK
1740         $LFS hsm_release $f || fail "second release should succeed"
1741         check_hsm_flags $f "0x0000000d"
1742
1743         stop_full_debug_logging
1744
1745         copytool_cleanup
1746 }
1747 run_test 21 "Simple release tests"
1748
1749 test_22() {
1750         # test needs a running copytool
1751         copytool_setup
1752
1753         mkdir -p $DIR/$tdir
1754
1755         local f=$DIR/$tdir/test_release
1756         local swap=$DIR/$tdir/test_swap
1757
1758         # Create a file and check its states
1759         local fid=$(make_small $f)
1760         check_hsm_flags $f "0x00000000"
1761
1762         $LFS hsm_archive $f || error "could not archive file"
1763         wait_request_state $fid ARCHIVE SUCCEED
1764
1765         # Release and check states
1766         $LFS hsm_release $f || error "could not release file"
1767         check_hsm_flags $f "0x0000000d"
1768
1769         make_small $swap
1770         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1771
1772         true
1773         copytool_cleanup
1774 }
1775 run_test 22 "Could not swap a release file"
1776
1777 test_23() {
1778         # test needs a running copytool
1779         copytool_setup
1780
1781         mkdir -p $DIR/$tdir
1782
1783         local f=$DIR/$tdir/test_mtime
1784
1785         # Create a file and check its states
1786         local fid=$(make_small $f)
1787         check_hsm_flags $f "0x00000000"
1788
1789         $LFS hsm_archive $f || error "could not archive file"
1790         wait_request_state $fid ARCHIVE SUCCEED
1791
1792         # Set modification time in the past
1793         touch -m -a -d @978261179 $f
1794
1795         # Release and check states
1796         $LFS hsm_release $f || error "could not release file"
1797         check_hsm_flags $f "0x0000000d"
1798
1799         local MTIME=$(stat -c "%Y" $f)
1800         local ATIME=$(stat -c "%X" $f)
1801         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1802         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1803
1804         copytool_cleanup
1805 }
1806 run_test 23 "Release does not change a/mtime (utime)"
1807
1808 test_24a() {
1809         local file=$DIR/$tdir/$tfile
1810         local fid
1811         local atime0
1812         local atime1
1813         local mtime0
1814         local mtime1
1815         local ctime0
1816         local ctime1
1817
1818         # test needs a running copytool
1819         copytool_setup
1820
1821         mkdir -p $DIR/$tdir
1822         rm -f $file
1823         fid=$(make_small $file)
1824
1825         # Create a file and check its states
1826         check_hsm_flags $file "0x00000000"
1827
1828         # Ensure atime is less than mtime and ctime.
1829         sleep 1
1830         echo >> $file
1831
1832         atime0=$(stat -c "%X" $file)
1833         mtime0=$(stat -c "%Y" $file)
1834         ctime0=$(stat -c "%Z" $file)
1835
1836         [ $atime0 -lt $mtime0 ] ||
1837                 error "atime $atime0 is not less than mtime $mtime0"
1838
1839         [ $atime0 -lt $ctime0 ] ||
1840                 error "atime $atime0 is not less than ctime $ctime0"
1841
1842         # Archive should not change any timestamps.
1843         $LFS hsm_archive $file || error "cannot archive '$file'"
1844         wait_request_state $fid ARCHIVE SUCCEED
1845
1846         atime1=$(stat -c "%X" $file)
1847         mtime1=$(stat -c "%Y" $file)
1848         ctime1=$(stat -c "%Z" $file)
1849
1850         [ $atime0 -eq $atime1 ] ||
1851                 error "archive changed atime from $atime0 to $atime1"
1852
1853         [ $mtime0 -eq $mtime1 ] ||
1854                 error "archive changed mtime from $mtime0 to $mtime1"
1855
1856         [ $ctime0 -eq $ctime1 ] ||
1857                 error "archive changed ctime from $ctime0 to $ctime1"
1858
1859         # Release should not change any timestamps.
1860         $LFS hsm_release $file || error "cannot release '$file'"
1861         check_hsm_flags $file "0x0000000d"
1862
1863         atime1=$(stat -c "%X" $file)
1864         mtime1=$(stat -c "%Y" $file)
1865         ctime1=$(stat -c "%Z" $file)
1866
1867         [ $atime0 -eq $atime1 ] ||
1868                 error "release changed atime from $atime0 to $atime1"
1869
1870         [ $mtime0 -eq $mtime1 ] ||
1871                 error "release changed mtime from $mtime0 to $mtime1"
1872
1873         [ $ctime0 -eq $ctime1 ] ||
1874                 error "release changed ctime from $ctime0 to $ctime1"
1875
1876         # Restore should not change atime or mtime and should not
1877         # decrease ctime.
1878         $LFS hsm_restore $file
1879         wait_request_state $fid RESTORE SUCCEED
1880
1881         atime1=$(stat -c "%X" $file)
1882         mtime1=$(stat -c "%Y" $file)
1883         ctime1=$(stat -c "%Z" $file)
1884
1885         [ $atime0 -eq $atime1 ] ||
1886                 error "restore changed atime from $atime0 to $atime1"
1887
1888         [ $mtime0 -eq $mtime1 ] ||
1889                 error "restore changed mtime from $mtime0 to $mtime1"
1890
1891         [ $ctime0 -eq $ctime1 ] ||
1892                 error "restore changed ctime from $ctime0 to $ctime1"
1893
1894         copytool_cleanup
1895
1896         # Once more, after unmount and mount.
1897         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1898         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1899
1900         atime1=$(stat -c "%X" $file)
1901         mtime1=$(stat -c "%Y" $file)
1902         ctime1=$(stat -c "%Z" $file)
1903
1904         [ $atime0 -eq $atime1 ] ||
1905                 error "remount changed atime from $atime0 to $atime1"
1906
1907         [ $mtime0 -eq $mtime1 ] ||
1908                 error "remount changed mtime from $mtime0 to $mtime1"
1909
1910         [ $ctime0 -eq $ctime1 ] ||
1911                 error "remount changed ctime from $ctime0 to $ctime1"
1912 }
1913 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1914
1915 test_24b() {
1916         local file=$DIR/$tdir/$tfile
1917         local fid
1918         local sum0
1919         local sum1
1920         # LU-3811
1921
1922         # Test needs a running copytool.
1923         copytool_setup
1924         mkdir -p $DIR/$tdir
1925
1926         # Check that root can do HSM actions on a regular user's file.
1927         rm -f $file
1928         fid=$(make_small $file)
1929         sum0=$(md5sum $file)
1930
1931         chown $RUNAS_ID:$RUNAS_GID $file ||
1932                 error "cannot chown '$file' to '$RUNAS_ID'"
1933
1934         chmod ugo-w $DIR/$tdir ||
1935                 error "cannot chmod '$DIR/$tdir'"
1936
1937         $LFS hsm_archive $file
1938         wait_request_state $fid ARCHIVE SUCCEED
1939
1940         $LFS hsm_release $file
1941         check_hsm_flags $file "0x0000000d"
1942
1943         $LFS hsm_restore $file
1944         wait_request_state $fid RESTORE SUCCEED
1945
1946         # Check that ordinary user can get HSM state.
1947         $RUNAS $LFS hsm_state $file ||
1948                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1949
1950         $LFS hsm_release $file
1951         check_hsm_flags $file "0x0000000d"
1952
1953         # Check that ordinary user can accessed released file.
1954         sum1=$($RUNAS md5sum $file) ||
1955                 error "user '$RUNAS_ID' cannot read '$file'"
1956
1957         [ "$sum0" == "$sum1" ] ||
1958                 error "md5sum mismatch for '$file'"
1959
1960         copytool_cleanup
1961 }
1962 run_test 24b "root can archive, release, and restore user files"
1963
1964 cleanup_test_24c() {
1965         trap 0
1966         set_hsm_param user_request_mask RESTORE
1967         set_hsm_param group_request_mask RESTORE
1968         set_hsm_param other_request_mask RESTORE
1969 }
1970
1971 test_24c() {
1972         local file=$DIR/$tdir/$tfile
1973         local action=archive
1974         local user_save
1975         local group_save
1976         local other_save
1977
1978         # test needs a running copytool
1979         copytool_setup
1980
1981         mkdir -p $DIR/$tdir
1982
1983         # Save the default masks and check that cleanup_24c will
1984         # restore the request masks correctly.
1985         user_save=$(get_hsm_param user_request_mask)
1986         group_save=$(get_hsm_param group_request_mask)
1987         other_save=$(get_hsm_param other_request_mask)
1988
1989         [ "$user_save" == RESTORE ] ||
1990                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1991         [ "$group_save" == RESTORE ] ||
1992                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1993         [ "$other_save" == RESTORE ] ||
1994                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1995
1996         trap cleanup_test_24c EXIT
1997
1998         # User.
1999         rm -f $file
2000         make_small $file
2001         chown $RUNAS_ID:nobody $file ||
2002                 error "cannot chown '$file' to '$RUNAS_ID:nobody'"
2003
2004         set_hsm_param user_request_mask ""
2005         $RUNAS $LFS hsm_$action $file &&
2006                 error "$action by user should fail"
2007
2008         set_hsm_param user_request_mask $action
2009         $RUNAS $LFS hsm_$action $file ||
2010                 error "$action by user should succeed"
2011
2012         # Group.
2013         rm -f $file
2014         make_small $file
2015         chown nobody:$RUNAS_GID $file ||
2016                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
2017
2018         set_hsm_param group_request_mask ""
2019         $RUNAS $LFS hsm_$action $file &&
2020                 error "$action by group should fail"
2021
2022         set_hsm_param group_request_mask $action
2023         $RUNAS $LFS hsm_$action $file ||
2024                 error "$action by group should succeed"
2025
2026         # Other.
2027         rm -f $file
2028         make_small $file
2029         chown nobody:nobody $file ||
2030                 error "cannot chown '$file' to 'nobody:nobody'"
2031
2032         set_hsm_param other_request_mask ""
2033         $RUNAS $LFS hsm_$action $file &&
2034                 error "$action by other should fail"
2035
2036         set_hsm_param other_request_mask $action
2037         $RUNAS $LFS hsm_$action $file ||
2038                 error "$action by other should succeed"
2039
2040         copytool_cleanup
2041         cleanup_test_24c
2042 }
2043 run_test 24c "check that user,group,other request masks work"
2044
2045 cleanup_test_24d() {
2046         trap 0
2047         mount -o remount,rw $MOUNT2
2048 }
2049
2050 test_24d() {
2051         local file1=$DIR/$tdir/$tfile
2052         local file2=$DIR2/$tdir/$tfile
2053         local fid1
2054         local fid2
2055
2056         copytool_setup
2057
2058         mkdir -p $DIR/$tdir
2059         rm -f $file1
2060         fid1=$(make_small $file1)
2061
2062         trap cleanup_test_24d EXIT
2063
2064         mount -o remount,ro $MOUNT2
2065
2066         fid2=$(path2fid $file2)
2067         [ "$fid1" == "$fid2" ] ||
2068                 error "FID mismatch '$fid1' != '$fid2'"
2069
2070         $LFS hsm_archive $file2 &&
2071                 error "archive should fail on read-only mount"
2072         check_hsm_flags $file1 "0x00000000"
2073
2074         $LFS hsm_archive $file1
2075         wait_request_state $fid1 ARCHIVE SUCCEED
2076
2077         $LFS hsm_release $file1
2078         $LFS hsm_restore $file2
2079         wait_request_state $fid1 RESTORE SUCCEED
2080
2081         $LFS hsm_release $file1 || error "cannot release '$file1'"
2082         dd if=$file2 of=/dev/null bs=1M || "cannot read '$file2'"
2083
2084         $LFS hsm_release $file2 &&
2085                 error "release should fail on read-only mount"
2086
2087         copytool_cleanup
2088         cleanup_test_24d
2089 }
2090 run_test 24d "check that read-only mounts are respected"
2091
2092 test_24e() {
2093         copytool_setup
2094
2095         mkdir -p $DIR/$tdir
2096
2097         local f=$DIR/$tdir/$tfile
2098         local fid
2099
2100         fid=$(make_small $f) || error "cannot create $f"
2101         $LFS hsm_archive $f || error "cannot archive $f"
2102         wait_request_state $fid ARCHIVE SUCCEED
2103         $LFS hsm_release $f || error "cannot release $f"
2104         while ! $LFS hsm_state $f | grep released; do
2105                 sleep 1
2106         done
2107
2108         tar -cf $TMP/$tfile.tar $DIR/$tdir || error "cannot tar $DIR/$tdir"
2109
2110         copytool_cleanup
2111 }
2112 run_test 24e "tar succeeds on HSM released files" # LU-6213
2113
2114 test_24f() {
2115
2116         # test needs a running copytool
2117         copytool_setup
2118
2119         mkdir -p $DIR/$tdir/d1
2120         local f=$DIR/$tdir/$tfile
2121         local fid=$(copy_file /etc/hosts $f)
2122         sum0=$(md5sum $f)
2123         echo $sum0
2124         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
2125                 error "hsm_archive failed"
2126         wait_request_state $fid ARCHIVE SUCCEED
2127         $LFS hsm_release $f || error "cannot release $f"
2128         tar --xattrs -cvf $f.tar -C $DIR/$tdir $tfile
2129         rm -f $f
2130         sync
2131         tar --xattrs -xvf $f.tar -C $DIR/$tdir ||
2132                 error "Can not recover the tar contents"
2133         sum1=$(md5sum $f)
2134         echo "Sum0 = $sum0, sum1 = $sum1"
2135         [ "$sum0" == "$sum1" ] || error "md5sum mismatch for '$tfile'"
2136
2137         copytool_cleanup
2138 }
2139 run_test 24f "root can archive, release, and restore tar files"
2140
2141 test_25a() {
2142         # test needs a running copytool
2143         copytool_setup
2144
2145         mkdir -p $DIR/$tdir
2146         copy2archive /etc/hosts $tdir/$tfile
2147
2148         local f=$DIR/$tdir/$tfile
2149
2150         import_file $tdir/$tfile $f
2151
2152         $LFS hsm_set --lost $f
2153
2154         md5sum $f
2155         local st=$?
2156
2157         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2158
2159         copytool_cleanup
2160 }
2161 run_test 25a "Restore lost file (HS_LOST flag) from import"\
2162              " (Operation not permitted)"
2163
2164 test_25b() {
2165         # test needs a running copytool
2166         copytool_setup
2167
2168         mkdir -p $DIR/$tdir
2169
2170         local f=$DIR/$tdir/$tfile
2171         local fid=$(copy_file /etc/passwd $f)
2172
2173         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2174         wait_request_state $fid ARCHIVE SUCCEED
2175
2176         $LFS hsm_release $f
2177         $LFS hsm_set --lost $f
2178         md5sum $f
2179         st=$?
2180
2181         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2182
2183         copytool_cleanup
2184 }
2185 run_test 25b "Restore lost file (HS_LOST flag) after release"\
2186              " (Operation not permitted)"
2187
2188 test_26() {
2189         # test needs a running copytool
2190         copytool_setup
2191
2192         mkdir -p $DIR/$tdir
2193         local f=$DIR/$tdir/$tfile
2194         local fid
2195         fid=$(make_large_for_progress $f)
2196         [ $? != 0 ] && skip "not enough free space" && return
2197
2198         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2199         wait_request_state $fid ARCHIVE SUCCEED
2200
2201         $LFS hsm_remove $f
2202         wait_request_state $fid REMOVE SUCCEED
2203
2204         check_hsm_flags $f "0x00000000"
2205
2206         copytool_cleanup
2207 }
2208 run_test 26 "Remove the archive of a valid file"
2209
2210 test_27a() {
2211         # test needs a running copytool
2212         copytool_setup
2213
2214         mkdir -p $DIR/$tdir
2215         make_archive $tdir/$tfile
2216         local f=$DIR/$tdir/$tfile
2217         import_file $tdir/$tfile $f
2218         local fid=$(path2fid $f)
2219
2220         $LFS hsm_remove $f
2221
2222         [[ $? != 0 ]] || error "Remove of a released file should fail"
2223
2224         copytool_cleanup
2225 }
2226 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
2227
2228 test_27b() {
2229         # test needs a running copytool
2230         copytool_setup
2231
2232         mkdir -p $DIR/$tdir
2233         local f=$DIR/$tdir/$tfile
2234         local fid
2235         fid=$(make_large_for_progress $f)
2236         [ $? != 0 ] && skip "not enough free space" && return
2237
2238         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2239         wait_request_state $fid ARCHIVE SUCCEED
2240         $LFS hsm_release $f
2241
2242         $LFS hsm_remove $f
2243
2244         [[ $? != 0 ]] || error "Remove of a released file should fail"
2245
2246         copytool_cleanup
2247 }
2248 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
2249
2250 test_28() {
2251         # test needs a running copytool
2252         copytool_setup
2253
2254         mkdir -p $DIR/$tdir
2255         local f=$DIR/$tdir/$tfile
2256         local fid
2257         fid=$(make_large_for_progress $f)
2258         [ $? != 0 ] && skip "not enough free space" && return
2259
2260         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2261         wait_request_state $fid ARCHIVE SUCCEED
2262
2263         cdt_disable
2264         $LFS hsm_remove $f
2265
2266         rm -f $f
2267
2268         cdt_enable
2269
2270         wait_request_state $fid REMOVE SUCCEED
2271
2272         copytool_cleanup
2273 }
2274 run_test 28 "Concurrent archive/file remove"
2275
2276 test_29a() {
2277         # Tests --mntpath and --archive options
2278
2279         local archive_id=7
2280         copytool_setup $SINGLEAGT $MOUNT $archive_id
2281
2282         # Bad archive number
2283         $LFS hsm_remove -m $MOUNT -a 33 0x857765760:0x8:0x2 2>&1 |
2284                 grep "Invalid argument" ||
2285                 error "unexpected hsm_remove failure (1)"
2286
2287         # mntpath is present but file is given
2288         $LFS hsm_remove --mntpath $MOUNT --archive 30 /qwerty/uyt 2>&1 |
2289                 grep "hsm: '/qwerty/uyt' is not a valid FID" ||
2290                 error "unexpected hsm_remove failure (2)"
2291
2292         copytool_cleanup
2293 }
2294 run_test 29a "Tests --mntpath and --archive options"
2295
2296 test_29b() {
2297         # test needs a running copytool
2298         copytool_setup
2299
2300         mkdir -p $DIR/$tdir
2301         local f=$DIR/$tdir/$tfile
2302         local fid=$(make_small $f)
2303
2304         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2305         wait_request_state $fid ARCHIVE SUCCEED
2306
2307         rm -f $f
2308
2309         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER $fid
2310         wait_request_state $fid REMOVE SUCCEED
2311
2312         copytool_cleanup
2313 }
2314 run_test 29b "Archive/delete/remove by FID from the archive."
2315
2316 test_29c() {
2317         # test needs a running copytool
2318         copytool_setup
2319
2320         mkdir -p $DIR/$tdir
2321         local fid1=$(make_small $DIR/$tdir/$tfile-1)
2322         local fid2=$(make_small $DIR/$tdir/$tfile-2)
2323         local fid3=$(make_small $DIR/$tdir/$tfile-3)
2324
2325         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$tfile-[1-3]
2326         wait_request_state $fid1 ARCHIVE SUCCEED
2327         wait_request_state $fid2 ARCHIVE SUCCEED
2328         wait_request_state $fid3 ARCHIVE SUCCEED
2329
2330         rm -f $DIR/$tdir/$tfile-[1-3]
2331
2332         echo $fid1 > $DIR/$tdir/list
2333         echo $fid2 >> $DIR/$tdir/list
2334         echo $fid3 >> $DIR/$tdir/list
2335
2336         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER \
2337                 --filelist $DIR/$tdir/list
2338         wait_request_state $fid1 REMOVE SUCCEED
2339         wait_request_state $fid2 REMOVE SUCCEED
2340         wait_request_state $fid3 REMOVE SUCCEED
2341
2342         copytool_cleanup
2343 }
2344 run_test 29c "Archive/delete/remove by FID, using a file list."
2345
2346 test_30a() {
2347         # restore at exec cannot work on agent node (because of Linux kernel
2348         # protection of executables)
2349         needclients 2 || return 0
2350
2351         # test needs a running copytool
2352         copytool_setup
2353
2354         mkdir -p $DIR/$tdir
2355         copy2archive /bin/true $tdir/$tfile
2356
2357         local f=$DIR/$tdir/true
2358         import_file $tdir/$tfile $f
2359
2360         local fid=$(path2fid $f)
2361
2362         # set no retry action mode
2363         cdt_set_no_retry
2364         do_node $CLIENT2 $f
2365         local st=$?
2366
2367         # cleanup
2368         # remove no try action mode
2369         cdt_clear_no_retry
2370         $LFS hsm_state $f
2371
2372         [[ $st == 0 ]] || error "Failed to exec a released file"
2373
2374         copytool_cleanup
2375 }
2376 run_test 30a "Restore at exec (import case)"
2377
2378 test_30b() {
2379         # restore at exec cannot work on agent node (because of Linux kernel
2380         # protection of executables)
2381         needclients 2 || return 0
2382
2383         # test needs a running copytool
2384         copytool_setup
2385
2386         mkdir -p $DIR/$tdir
2387         local f=$DIR/$tdir/true
2388         local fid=$(copy_file /bin/true $f)
2389         chmod 755 $f
2390         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2391         wait_request_state $fid ARCHIVE SUCCEED
2392         $LFS hsm_release $f
2393         $LFS hsm_state $f
2394         # set no retry action mode
2395         cdt_set_no_retry
2396         do_node $CLIENT2 $f
2397         local st=$?
2398
2399         # cleanup
2400         # remove no try action mode
2401         cdt_clear_no_retry
2402         $LFS hsm_state $f
2403
2404         [[ $st == 0 ]] || error "Failed to exec a released file"
2405
2406         copytool_cleanup
2407 }
2408 run_test 30b "Restore at exec (release case)"
2409
2410 test_30c() {
2411         needclients 2 || return 0
2412
2413         # test needs a running copytool
2414         copytool_setup
2415
2416         mkdir -p $DIR/$tdir
2417         local f=$DIR/$tdir/SLEEP
2418         local slp_sum1=$(md5sum /bin/sleep)
2419         local fid=$(copy_file /bin/sleep $f)
2420         chmod 755 $f
2421         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2422         wait_request_state $fid ARCHIVE SUCCEED
2423         $LFS hsm_release $f
2424         check_hsm_flags $f "0x0000000d"
2425         # set no retry action mode
2426         cdt_set_no_retry
2427         do_node $CLIENT2 "$f 10" &
2428         local pid=$!
2429         sleep 3
2430         echo 'Hi!' > $f
2431         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2432         wait $pid
2433         [[ $? == 0 ]] || error "Execution failed during run"
2434         cmp /bin/sleep $f
2435         if [[ $? != 0 ]]; then
2436                 local slp_sum2=$(md5sum /bin/sleep)
2437                 # in case sleep file is modified during the test
2438                 [[ $slp_sum1 == $slp_sum2 ]] &&
2439                         error "Binary overwritten during exec"
2440         fi
2441
2442         # cleanup
2443         # remove no try action mode
2444         cdt_clear_no_retry
2445         check_hsm_flags $f "0x00000009"
2446
2447         copytool_cleanup
2448 }
2449 run_test 30c "Update during exec of released file must fail"
2450
2451 restore_and_check_size() {
2452         local f=$1
2453         local fid=$2
2454         local s=$(stat -c "%s" $f)
2455         local n=$s
2456         local st=$(get_hsm_flags $f)
2457         local err=0
2458         local cpt=0
2459         $LFS hsm_restore $f
2460         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2461         do
2462                 n=$(stat -c "%s" $f)
2463                 # we echo in both cases to show stat is not
2464                 # hang
2465                 if [[ $n != $s ]]; then
2466                         echo "size seen is $n != $s"
2467                         err=1
2468                 else
2469                         echo "size seen is right: $n == $s"
2470                 fi
2471                 st=$(get_hsm_flags $f)
2472                 sleep 10
2473                 cpt=$((cpt + 1))
2474         done
2475         if [[ $cpt -lt 10 ]]; then
2476                 echo " "done
2477         else
2478                 echo " restore is too long"
2479                 wait_request_state $fid RESTORE SUCCEED
2480         fi
2481         return $err
2482 }
2483
2484 test_31a() {
2485         # test needs a running copytool
2486         copytool_setup
2487
2488         mkdir -p $DIR/$tdir
2489
2490         make_archive $tdir/$tfile
2491         local f=$DIR/$tdir/$tfile
2492         import_file $tdir/$tfile $f
2493         local fid=$($LFS path2fid $f)
2494         HSM_ARCHIVE_PURGE=false copytool_setup
2495
2496         restore_and_check_size $f $fid
2497         local err=$?
2498
2499         [[ $err -eq 0 ]] || error "File size changed during restore"
2500
2501         copytool_cleanup
2502 }
2503 run_test 31a "Import a large file and check size during restore"
2504
2505
2506 test_31b() {
2507         # test needs a running copytool
2508         copytool_setup
2509
2510         mkdir -p $DIR/$tdir
2511
2512         local f=$DIR/$tdir/$tfile
2513         local fid
2514         fid=$(make_large_for_progress $f)
2515         [ $? != 0 ] && skip "not enough free space" && return
2516
2517         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2518         wait_request_state $fid ARCHIVE SUCCEED
2519         $LFS hsm_release $f
2520
2521         restore_and_check_size $f $fid
2522         local err=$?
2523
2524         [[ $err -eq 0 ]] || error "File size changed during restore"
2525
2526         copytool_cleanup
2527 }
2528 run_test 31b "Restore a large unaligned file and check size during restore"
2529
2530 test_31c() {
2531         # test needs a running copytool
2532         copytool_setup
2533
2534         mkdir -p $DIR/$tdir
2535
2536         local f=$DIR/$tdir/$tfile
2537         local fid
2538         fid=$(make_large_for_progress_aligned $f)
2539         [ $? != 0 ] && skip "not enough free space" && return
2540
2541         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2542         wait_request_state $fid ARCHIVE SUCCEED
2543         $LFS hsm_release $f
2544
2545         restore_and_check_size $f $fid
2546         local err=$?
2547
2548         [[ $err -eq 0 ]] || error "File size changed during restore"
2549
2550         copytool_cleanup
2551 }
2552 run_test 31c "Restore a large aligned file and check size during restore"
2553
2554 test_33() {
2555         # test needs a running copytool
2556         copytool_setup
2557
2558         mkdir -p $DIR/$tdir
2559
2560         local f=$DIR/$tdir/$tfile
2561         local fid
2562         fid=$(make_large_for_progress $f)
2563         [ $? != 0 ] && skip "not enough free space" && return
2564
2565         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2566         wait_request_state $fid ARCHIVE SUCCEED
2567         $LFS hsm_release $f
2568
2569         # to be sure wait_all_done will not be mislead by previous tests
2570         # and ops.
2571         cdt_purge
2572         wait_for_grace_delay
2573         # Also raise grace_delay significantly so the Canceled
2574         # Restore action will stay enough long avail.
2575         local old_grace=$(get_hsm_param grace_delay)
2576         set_hsm_param grace_delay 100
2577
2578         md5sum $f >/dev/null &
2579         local pid=$!
2580         wait_request_state $fid RESTORE STARTED
2581
2582         kill -15 $pid
2583         sleep 1
2584
2585         # Check restore trigger process was killed
2586         local killed=$(ps -o pid,comm hp $pid >/dev/null)
2587
2588         $LFS hsm_cancel $f
2589
2590         # instead of waiting+checking both Restore and Cancel ops
2591         # sequentially, wait for both to be finished and then check
2592         # each results.
2593         wait_all_done 100 $fid
2594         local rstate=$(get_request_state $fid RESTORE)
2595         local cstate=$(get_request_state $fid CANCEL)
2596
2597         # restore orig grace_delay.
2598         set_hsm_param grace_delay $old_grace
2599
2600         if [[ "$rstate" == "CANCELED" ]] ; then
2601                 [[ "$cstate" == "SUCCEED" ]] ||
2602                         error "Restore state is CANCELED and Cancel state " \
2603                                "is not SUCCEED but $cstate"
2604                 echo "Restore state is CANCELED, Cancel state is SUCCEED"
2605         elif [[ "$rstate" == "SUCCEED" ]] ; then
2606                 [[ "$cstate" == "FAILED" ]] ||
2607                         error "Restore state is SUCCEED and Cancel state " \
2608                                 "is not FAILED but $cstate"
2609                 echo "Restore state is SUCCEED, Cancel state is FAILED"
2610         else
2611                 error "Restore state is $rstate and Cancel state is $cstate"
2612         fi
2613
2614         [ -z $killed ] ||
2615                 error "Cannot kill process waiting for restore ($killed)"
2616
2617         copytool_cleanup
2618 }
2619 run_test 33 "Kill a restore waiting process"
2620
2621 test_34() {
2622         # test needs a running copytool
2623         copytool_setup
2624
2625         mkdir -p $DIR/$tdir
2626
2627         local f=$DIR/$tdir/$tfile
2628         local fid
2629         fid=$(make_large_for_progress $f)
2630         [ $? != 0 ] && skip "not enough free space" && return
2631
2632         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2633         wait_request_state $fid ARCHIVE SUCCEED
2634         $LFS hsm_release $f
2635
2636         md5sum $f >/dev/null &
2637         local pid=$!
2638         wait_request_state $fid RESTORE STARTED
2639
2640         rm $f || error "rm $f failed"
2641         # rm must not block during restore
2642         wait_request_state $fid RESTORE STARTED
2643
2644         wait_request_state $fid RESTORE SUCCEED
2645         # check md5sum pgm finished
2646         local there=$(ps -o pid,comm hp $pid >/dev/null)
2647         [[ -z $there ]] || error "Restore initiator does not exit"
2648
2649         local rc=$(wait $pid)
2650         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2651
2652         copytool_cleanup
2653 }
2654 run_test 34 "Remove file during restore"
2655
2656 test_35() {
2657         # test needs a running copytool
2658         copytool_setup
2659
2660         mkdir -p $DIR/$tdir
2661
2662         local f=$DIR/$tdir/$tfile
2663         local f1=$DIR/$tdir/$tfile-1
2664         local fid
2665         fid=$(make_large_for_progress $f)
2666         [ $? != 0 ] && skip "not enough free space" && return
2667
2668         local fid1=$(copy_file /etc/passwd $f1)
2669         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2670         wait_request_state $fid ARCHIVE SUCCEED
2671         $LFS hsm_release $f
2672
2673         md5sum $f >/dev/null &
2674         local pid=$!
2675         wait_request_state $fid RESTORE STARTED
2676
2677         mv $f1 $f || error "mv $f1 $f failed"
2678         # mv must not block during restore
2679         wait_request_state $fid RESTORE STARTED
2680
2681         wait_request_state $fid RESTORE SUCCEED
2682         # check md5sum pgm finished
2683         local there=$(ps -o pid,comm hp $pid >/dev/null)
2684         [[ -z $there ]] || error "Restore initiator does not exit"
2685
2686         local rc=$(wait $pid)
2687         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2688
2689         fid2=$(path2fid $f)
2690         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2691
2692         copytool_cleanup
2693 }
2694 run_test 35 "Overwrite file during restore"
2695
2696 test_36() {
2697         # test needs a running copytool
2698         copytool_setup
2699
2700         mkdir -p $DIR/$tdir
2701
2702         local f=$DIR/$tdir/$tfile
2703         local fid
2704         fid=$(make_large_for_progress $f)
2705         [ $? != 0 ] && skip "not enough free space" && return
2706
2707         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2708         wait_request_state $fid ARCHIVE SUCCEED
2709         $LFS hsm_release $f
2710
2711         md5sum $f >/dev/null &
2712         local pid=$!
2713         wait_request_state $fid RESTORE STARTED
2714
2715         mv $f $f.new
2716         # rm must not block during restore
2717         wait_request_state $fid RESTORE STARTED
2718
2719         wait_request_state $fid RESTORE SUCCEED
2720         # check md5sum pgm finished
2721         local there=$(ps -o pid,comm hp $pid >/dev/null)
2722         [[ -z $there ]] ||
2723                 error "Restore initiator does not exit"
2724
2725         local rc=$(wait $pid)
2726         [[ $rc -eq 0 ]] ||
2727                 error "Restore initiator failed with $rc"
2728
2729         copytool_cleanup
2730 }
2731 run_test 36 "Move file during restore"
2732
2733 test_37() {
2734         # LU-5683: check that an archived dirty file can be rearchived.
2735         copytool_cleanup
2736         copytool_setup $SINGLEAGT $MOUNT2
2737
2738         mkdir -p $DIR/$tdir
2739         local f=$DIR/$tdir/$tfile
2740         local fid
2741
2742         fid=$(make_small $f) || error "cannot create small file"
2743
2744         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2745         wait_request_state $fid ARCHIVE SUCCEED
2746         $LFS hsm_release $f || error "cannot release $f"
2747
2748         # Dirty file.
2749         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
2750
2751         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2752         wait_request_state $fid ARCHIVE SUCCEED
2753
2754         copytool_cleanup
2755 }
2756 run_test 37 "re-archive a dirty file"
2757
2758 multi_archive() {
2759         local prefix=$1
2760         local count=$2
2761         local n=""
2762
2763         for n in $(seq 1 $count); do
2764                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2765         done
2766         echo "$count archive requests submitted"
2767 }
2768
2769 test_40() {
2770         local stream_count=4
2771         local file_count=100
2772         mkdir -p $DIR/$tdir
2773         local f=$DIR/$tdir/$tfile
2774         local i=""
2775         local p=""
2776         local fid=""
2777
2778         for i in $(seq 1 $file_count); do
2779                 for p in $(seq 1 $stream_count); do
2780                         fid=$(copy_file /etc/hosts $f.$p.$i)
2781                 done
2782         done
2783         # force copytool to use a local/temp archive dir to ensure best
2784         # performance vs remote/NFS mounts used in auto-tests
2785         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
2786                 copytool_setup
2787         else
2788                 copytool_setup $SINGLEAGT $MOUNT $HSM_ARCHIVE_NUMBER $TMP/$tdir
2789         fi
2790         # to be sure wait_all_done will not be mislead by previous tests
2791         cdt_purge
2792         wait_for_grace_delay
2793         typeset -a pids
2794         # start archive streams in background (archive files in parallel)
2795         for p in $(seq 1 $stream_count); do
2796                 multi_archive $f.$p $file_count &
2797                 pids[$p]=$!
2798         done
2799         echo -n  "Wait for all requests being enqueued..."
2800         wait ${pids[*]}
2801         echo OK
2802         wait_all_done 100
2803         copytool_cleanup
2804 }
2805 run_test 40 "Parallel archive requests"
2806
2807 test_52() {
2808         # test needs a running copytool
2809         copytool_setup
2810
2811         mkdir -p $DIR/$tdir
2812         local f=$DIR/$tdir/$tfile
2813         local fid=$(copy_file /etc/motd $f 1)
2814
2815         $LFS hsm_archive $f || error "could not archive file"
2816         wait_request_state $fid ARCHIVE SUCCEED
2817         check_hsm_flags $f "0x00000009"
2818
2819         multiop_bg_pause $f O_c || error "multiop failed"
2820         local MULTIPID=$!
2821
2822         mds_evict_client
2823         client_up || client_up || true
2824
2825         kill -USR1 $MULTIPID
2826         wait $MULTIPID || error "multiop close failed"
2827
2828         check_hsm_flags $f "0x0000000b"
2829
2830         copytool_cleanup
2831 }
2832 run_test 52 "Opened for write file on an evicted client should be set dirty"
2833
2834 test_53() {
2835         # test needs a running copytool
2836         copytool_setup
2837
2838         mkdir -p $DIR/$tdir
2839         local f=$DIR/$tdir/$tfile
2840         local fid=$(copy_file /etc/motd $f 1)
2841
2842         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2843                 error "could not archive file"
2844         wait_request_state $fid ARCHIVE SUCCEED
2845         check_hsm_flags $f "0x00000009"
2846
2847         multiop_bg_pause $f o_c || error "multiop failed"
2848         MULTIPID=$!
2849
2850         mds_evict_client
2851         client_up || client_up || true
2852
2853         kill -USR1 $MULTIPID
2854         wait $MULTIPID || error "multiop close failed"
2855
2856         check_hsm_flags $f "0x00000009"
2857
2858         copytool_cleanup
2859 }
2860 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2861
2862 test_54() {
2863         # test needs a running copytool
2864         copytool_setup
2865
2866         mkdir -p $DIR/$tdir
2867         local f=$DIR/$tdir/$tfile
2868         local fid=$(make_large_for_progress $f)
2869
2870         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2871                 error "could not archive file"
2872         wait_request_state $fid ARCHIVE STARTED
2873
2874         check_hsm_flags $f "0x00000001"
2875
2876         # Avoid coordinator resending this request as soon it has failed.
2877         cdt_set_no_retry
2878
2879         echo "foo" >> $f
2880         sync
2881         wait_request_state $fid ARCHIVE FAILED
2882
2883         check_hsm_flags $f "0x00000003"
2884
2885         cdt_clear_no_retry
2886         copytool_cleanup
2887 }
2888 run_test 54 "Write during an archive cancels it"
2889
2890 test_55() {
2891         # test needs a running copytool
2892         copytool_setup
2893
2894         mkdir -p $DIR/$tdir
2895         local f=$DIR/$tdir/$tfile
2896         local fid=$(make_large_for_progress $f)
2897
2898         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2899                 error "could not archive file"
2900         wait_request_state $fid ARCHIVE STARTED
2901
2902         check_hsm_flags $f "0x00000001"
2903
2904         # Avoid coordinator resending this request as soon it has failed.
2905         cdt_set_no_retry
2906
2907         $TRUNCATE $f 1024 || error "truncate failed"
2908         sync
2909         wait_request_state $fid ARCHIVE FAILED
2910
2911         check_hsm_flags $f "0x00000003"
2912
2913         cdt_clear_no_retry
2914         copytool_cleanup
2915 }
2916 run_test 55 "Truncate during an archive cancels it"
2917
2918 test_56() {
2919         # test needs a running copytool
2920         copytool_setup
2921
2922         mkdir -p $DIR/$tdir
2923         local f=$DIR/$tdir/$tfile
2924         local fid
2925         fid=$(make_large_for_progress $f)
2926         [ $? != 0 ] && skip "not enough free space" && return
2927
2928         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2929                 error "could not archive file"
2930         wait_request_state $fid ARCHIVE STARTED
2931
2932         check_hsm_flags $f "0x00000001"
2933
2934         # Change metadata and sync to be sure we are not changing only
2935         # in memory.
2936         chmod 644 $f
2937         chgrp sys $f
2938         sync
2939         wait_request_state $fid ARCHIVE SUCCEED
2940
2941         check_hsm_flags $f "0x00000009"
2942
2943         copytool_cleanup
2944 }
2945 run_test 56 "Setattr during an archive is ok"
2946
2947 test_57() {
2948         # Need one client for I/O, one for request
2949         needclients 2 || return 0
2950
2951         # test needs a running copytool
2952         copytool_setup
2953
2954         mkdir -p $DIR/$tdir
2955         local f=$DIR/$tdir/test_archive_remote
2956         # Create a file on a remote node
2957         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2958                 "count=2 conv=fsync"
2959
2960         # And archive it
2961         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2962                 error "hsm_archive failed"
2963         local fid=$(path2fid $f)
2964         wait_request_state $fid ARCHIVE SUCCEED
2965
2966         # Release and implicit restore it
2967         do_node $CLIENT2 "$LFS hsm_release $f" ||
2968                 error "hsm_release failed"
2969         do_node $CLIENT2 "md5sum $f" ||
2970                 error "hsm_restore failed"
2971
2972         wait_request_state $fid RESTORE SUCCEED
2973
2974         copytool_cleanup
2975 }
2976 run_test 57 "Archive a file with dirty cache on another node"
2977
2978 truncate_released_file() {
2979         local src_file=$1
2980         local trunc_to=$2
2981
2982         local sz=$(stat -c %s $src_file)
2983         local f=$DIR/$tdir/$tfile
2984         local fid=$(copy_file $1 $f)
2985         local ref=$f-ref
2986         cp $f $f-ref
2987
2988         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2989                 error "could not archive file"
2990         wait_request_state $fid ARCHIVE SUCCEED
2991
2992         $LFS hsm_release $f || error "could not release file"
2993
2994         $TRUNCATE $f $trunc_to || error "truncate failed"
2995         sync
2996
2997         local sz1=$(stat -c %s $f)
2998         [[ $sz1 == $trunc_to ]] ||
2999                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3000
3001         $LFS hsm_state $f
3002         check_hsm_flags $f "0x0000000b"
3003
3004         local state=$(get_request_state $fid RESTORE)
3005         [[ "$state" == "SUCCEED" ]] ||
3006                 error "truncate $sz does not trig restore, state = $state"
3007
3008         $TRUNCATE $ref $trunc_to
3009         cmp $ref $f || error "file data wrong after truncate"
3010
3011         rm -f $f $f-ref
3012 }
3013
3014 test_58() {
3015         # test needs a running copytool
3016         copytool_setup
3017
3018         mkdir -p $DIR/$tdir
3019
3020         local sz=$(stat -c %s /etc/passwd)
3021
3022         echo "truncate up from $sz to $((sz*2))"
3023         truncate_released_file /etc/passwd $((sz*2))
3024
3025         echo "truncate down from $sz to $((sz/2))"
3026         truncate_released_file /etc/passwd $((sz/2))
3027
3028         echo "truncate to 0"
3029         truncate_released_file /etc/passwd 0
3030
3031         copytool_cleanup
3032 }
3033 run_test 58 "Truncate a released file will trigger restore"
3034
3035 test_59() {
3036         local fid
3037         copytool_setup
3038         $MCREATE $DIR/$tfile || error "mcreate failed"
3039         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3040         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3041         fid=$(path2fid $DIR/$tfile)
3042         wait_request_state $fid ARCHIVE SUCCEED
3043         $LFS hsm_release $DIR/$tfile || error "release failed"
3044         copytool_cleanup
3045 }
3046 run_test 59 "Release stripeless file with non-zero size"
3047
3048 test_60() {
3049         # This test validates the fix for LU-4512. Ensure that the -u
3050         # option changes the progress reporting interval from the
3051         # default (30 seconds) to the user-specified interval.
3052         local interval=5
3053         local progress_timeout=$((interval * 4))
3054
3055         # test needs a new running copytool
3056         copytool_cleanup
3057         HSMTOOL_UPDATE_INTERVAL=$interval copytool_setup
3058
3059         mkdir -p $DIR/$tdir
3060         local f=$DIR/$tdir/$tfile
3061         local fid
3062         fid=$(make_large_for_progress $f)
3063         [ $? != 0 ] && skip "not enough free space" && return
3064
3065         local mdtidx=0
3066         local mdt=${MDT_PREFIX}${mdtidx}
3067         local mds=mds$((mdtidx + 1))
3068
3069         # Wait for copytool to register
3070         wait_update_facet $mds \
3071                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3072                 uuid 100 || error "coyptool failed to register with $mdt"
3073
3074         local start_at=$(date +%s)
3075         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3076                 error "could not archive file"
3077
3078         local agent=$(facet_active_host $SINGLEAGT)
3079         local prefix=$TESTLOG_PREFIX
3080         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
3081         local copytool_log=$prefix.copytool_log.$agent.log
3082
3083
3084         wait_update $agent \
3085             "grep -o start.copy $copytool_log" "start copy" 100 ||
3086                 error "copytool failed to start"
3087
3088         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3089         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3090
3091         local RESULT
3092         local WAIT=0
3093         local sleep=1
3094
3095         echo -n "Expecting a progress update within $progress_timeout seconds... "
3096         while [ true ]; do
3097                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3098                 if [ $RESULT -gt 0 ]; then
3099                         echo "$RESULT bytes copied in $WAIT seconds."
3100                         break
3101                 elif [ $WAIT -ge $progress_timeout ]; then
3102                         error "Timed out waiting for progress update!"
3103                         break
3104                 fi
3105                 WAIT=$((WAIT + sleep))
3106                 sleep $sleep
3107         done
3108
3109         local finish_at=$(date +%s)
3110         local elapsed=$((finish_at - start_at))
3111
3112         # Ensure that the progress update occurred within the expected window.
3113         if [ $elapsed -lt $interval ]; then
3114                 error "Expected progress update after at least $interval seconds"
3115         fi
3116
3117         cdt_clear_no_retry
3118         copytool_cleanup
3119 }
3120 run_test 60 "Changing progress update interval from default"
3121
3122 test_70() {
3123         # test needs a new running copytool
3124         copytool_cleanup
3125         copytool_monitor_setup
3126         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3127
3128         # Just start and stop the copytool to generate events.
3129         cdt_clear_no_retry
3130
3131         # Wait for the copytool to register.
3132         wait_update --verbose $(facet_active_host mds1) \
3133                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3134                 uuid 100 ||
3135                 error "copytool failed to register with MDT0000"
3136
3137         copytool_cleanup
3138
3139         local REGISTER_EVENT
3140         local UNREGISTER_EVENT
3141         while read event; do
3142                 local parsed=$(parse_json_event "$event")
3143                 if [ -z "$parsed" ]; then
3144                         error "Copytool sent malformed event: $event"
3145                 fi
3146                 eval $parsed
3147
3148                 if [ $event_type == "REGISTER" ]; then
3149                         REGISTER_EVENT=$event
3150                 elif [ $event_type == "UNREGISTER" ]; then
3151                         UNREGISTER_EVENT=$event
3152                 fi
3153         done < <(echo $"$(get_copytool_event_log)")
3154
3155         if [ -z "$REGISTER_EVENT" ]; then
3156                 error "Copytool failed to send register event to FIFO"
3157         fi
3158
3159         if [ -z "$UNREGISTER_EVENT" ]; then
3160                 error "Copytool failed to send unregister event to FIFO"
3161         fi
3162
3163         copytool_monitor_cleanup
3164         echo "Register/Unregister events look OK."
3165 }
3166 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3167
3168 test_71() {
3169         # Bump progress interval for livelier events.
3170         local interval=5
3171
3172         # test needs a new running copytool
3173         copytool_cleanup
3174         copytool_monitor_setup
3175         HSMTOOL_UPDATE_INTERVAL=$interval \
3176         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3177
3178         mkdir -p $DIR/$tdir
3179         local f=$DIR/$tdir/$tfile
3180         local fid
3181         fid=$(make_large_for_progress $f)
3182         [ $? != 0 ] && skip "not enough free space" && return
3183
3184         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3185                 error "could not archive file"
3186         wait_request_state $fid ARCHIVE SUCCEED
3187
3188         local expected_fields="event_time data_fid source_fid"
3189         expected_fields+=" total_bytes current_bytes"
3190
3191         local START_EVENT
3192         local FINISH_EVENT
3193         while read event; do
3194                 # Make sure we're not getting anything from previous events.
3195                 for field in $expected_fields; do
3196                         unset $field
3197                 done
3198
3199                 local parsed=$(parse_json_event "$event")
3200                 if [ -z "$parsed" ]; then
3201                         error "Copytool sent malformed event: $event"
3202                 fi
3203                 eval $parsed
3204
3205                 if [ $event_type == "ARCHIVE_START" ]; then
3206                         START_EVENT=$event
3207                         continue
3208                 elif [ $event_type == "ARCHIVE_FINISH" ]; then
3209                         FINISH_EVENT=$event
3210                         continue
3211                 elif [ $event_type != "ARCHIVE_RUNNING" ]; then
3212                         continue
3213                 fi
3214
3215                 # Do some simple checking of the progress update events.
3216                 for expected_field in $expected_fields; do
3217                         if [ -z ${!expected_field+x} ]; then
3218                                 error "Missing $expected_field field in event"
3219                         fi
3220                 done
3221
3222                 if [ $total_bytes -eq 0 ]; then
3223                         error "Expected total_bytes to be > 0"
3224                 fi
3225
3226                 # These should be identical throughout an archive
3227                 # operation.
3228                 if [ $source_fid != $data_fid ]; then
3229                         error "Expected source_fid to equal data_fid"
3230                 fi
3231         done < <(echo $"$(get_copytool_event_log)")
3232
3233         if [ -z "$START_EVENT" ]; then
3234                 error "Copytool failed to send archive start event to FIFO"
3235         fi
3236
3237         if [ -z "$FINISH_EVENT" ]; then
3238                 error "Copytool failed to send archive finish event to FIFO"
3239         fi
3240
3241         echo "Archive events look OK."
3242
3243         cdt_clear_no_retry
3244         copytool_cleanup
3245         copytool_monitor_cleanup
3246 }
3247 run_test 71 "Copytool logs JSON archive events to FIFO"
3248
3249 test_72() {
3250         # Bump progress interval for livelier events.
3251         local interval=5
3252
3253         # test needs a new running copytool
3254         copytool_cleanup
3255         copytool_monitor_setup
3256         HSMTOOL_UPDATE_INTERVAL=$interval \
3257         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3258         local test_file=$HSMTOOL_MONITOR_DIR/file
3259
3260         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3261         cmd+="conv=fsync"
3262         do_facet $SINGLEAGT "$cmd" ||
3263                 error "cannot create $test_file on $SINGLEAGT"
3264         copy2archive $test_file $tdir/$tfile
3265
3266         mkdir -p $DIR/$tdir
3267         local f=$DIR/$tdir/$tfile
3268         import_file $tdir/$tfile $f
3269         f=$DIR2/$tdir/$tfile
3270         echo "Verifying released state: "
3271         check_hsm_flags $f "0x0000000d"
3272
3273         local fid=$(path2fid $f)
3274         $LFS hsm_restore $f
3275         wait_request_state $fid RESTORE SUCCEED
3276
3277         local expected_fields="event_time data_fid source_fid"
3278         expected_fields+=" total_bytes current_bytes"
3279
3280         local START_EVENT
3281         local FINISH_EVENT
3282         while read event; do
3283                 # Make sure we're not getting anything from previous events.
3284                 for field in $expected_fields; do
3285                         unset $field
3286                 done
3287
3288                 local parsed=$(parse_json_event "$event")
3289                 if [ -z "$parsed" ]; then
3290                         error "Copytool sent malformed event: $event"
3291                 fi
3292                 eval $parsed
3293
3294                 if [ $event_type == "RESTORE_START" ]; then
3295                         START_EVENT=$event
3296                         if [ $source_fid != $data_fid ]; then
3297                                 error "source_fid should == data_fid at start"
3298                         fi
3299                         continue
3300                 elif [ $event_type == "RESTORE_FINISH" ]; then
3301                         FINISH_EVENT=$event
3302                         if [ $source_fid != $data_fid ]; then
3303                                 error "source_fid should == data_fid at finish"
3304                         fi
3305                         continue
3306                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3307                         continue
3308                 fi
3309
3310                 # Do some simple checking of the progress update events.
3311                 for expected_field in $expected_fields; do
3312                         if [ -z ${!expected_field+x} ]; then
3313                                 error "Missing $expected_field field in event"
3314                         fi
3315                 done
3316
3317                 if [ $total_bytes -eq 0 ]; then
3318                         error "Expected total_bytes to be > 0"
3319                 fi
3320
3321                 # When a restore starts out, the data fid is the same as the
3322                 # source fid. After the restore has gotten going, we learn
3323                 # the new data fid. Once the restore has finished, the source
3324                 # fid is set to the new data fid.
3325                 #
3326                 # We test this because some monitoring software may depend on
3327                 # this behavior. If it changes, then the consumers of these
3328                 # events may need to be modified.
3329                 if [ $source_fid == $data_fid ]; then
3330                         error "source_fid should != data_fid during restore"
3331                 fi
3332         done < <(echo $"$(get_copytool_event_log)")
3333
3334         if [ -z "$START_EVENT" ]; then
3335                 error "Copytool failed to send restore start event to FIFO"
3336         fi
3337
3338         if [ -z "$FINISH_EVENT" ]; then
3339                 error "Copytool failed to send restore finish event to FIFO"
3340         fi
3341
3342         echo "Restore events look OK."
3343
3344         cdt_clear_no_retry
3345         copytool_cleanup
3346         copytool_monitor_cleanup
3347
3348         rm -rf $test_dir
3349 }
3350 run_test 72 "Copytool logs JSON restore events to FIFO"
3351
3352 test_90() {
3353         file_count=51 # Max number of files constrained by LNET message size
3354         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3355         local f=$DIR/$tdir/$tfile
3356         local FILELIST=/tmp/filelist.txt
3357         local i=""
3358
3359         rm -f $FILELIST
3360         for i in $(seq 1 $file_count); do
3361                 fid=$(copy_file /etc/hosts $f.$i)
3362                 echo $f.$i >> $FILELIST
3363         done
3364         # force copytool to use a local/temp archive dir to ensure best
3365         # performance vs remote/NFS mounts used in auto-tests
3366         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
3367                 copytool_setup
3368         else
3369                 local dai=$(get_hsm_param default_archive_id)
3370                 copytool_setup $SINGLEAGT $MOUNT $dai $TMP/$tdir
3371         fi
3372         # to be sure wait_all_done will not be mislead by previous tests
3373         cdt_purge
3374         wait_for_grace_delay
3375         $LFS hsm_archive --filelist $FILELIST ||
3376                 error "cannot archive a file list"
3377         wait_all_done 100
3378         $LFS hsm_release --filelist $FILELIST ||
3379                 error "cannot release a file list"
3380         $LFS hsm_restore --filelist $FILELIST ||
3381                 error "cannot restore a file list"
3382         wait_all_done 100
3383         copytool_cleanup
3384 }
3385 run_test 90 "Archive/restore a file list"
3386
3387 double_verify_reset_hsm_param() {
3388         local p=$1
3389         echo "Testing $HSM_PARAM.$p"
3390         local val=$(get_hsm_param $p)
3391         local save=$val
3392         local val2=$(($val * 2))
3393         set_hsm_param $p $val2
3394         val=$(get_hsm_param $p)
3395         [[ $val == $val2 ]] ||
3396                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3397         echo "Set $p to 0 must failed"
3398         set_hsm_param $p 0
3399         local rc=$?
3400         # restore value
3401         set_hsm_param $p $save
3402
3403         if [[ $rc == 0 ]]; then
3404                 error "we must not be able to set $HSM_PARAM.$p to 0"
3405         fi
3406 }
3407
3408 test_100() {
3409         double_verify_reset_hsm_param loop_period
3410         double_verify_reset_hsm_param grace_delay
3411         double_verify_reset_hsm_param active_request_timeout
3412         double_verify_reset_hsm_param max_requests
3413         double_verify_reset_hsm_param default_archive_id
3414 }
3415 run_test 100 "Set coordinator /proc tunables"
3416
3417 test_102() {
3418         cdt_disable
3419         cdt_enable
3420         cdt_restart
3421 }
3422 run_test 102 "Verify coordinator control"
3423
3424 test_103() {
3425         # test needs a running copytool
3426         copytool_setup
3427
3428         local i=""
3429         local fid=""
3430
3431         mkdir -p $DIR/$tdir
3432         for i in $(seq 1 20); do
3433                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3434         done
3435         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3436
3437         cdt_purge
3438
3439         echo "Current requests"
3440         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3441                         $HSM_PARAM.actions |\
3442                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3443
3444         [[ -z "$res" ]] || error "Some request have not been canceled"
3445
3446         copytool_cleanup
3447 }
3448 run_test 103 "Purge all requests"
3449
3450 DATA=CEA
3451 DATAHEX='[434541]'
3452 test_104() {
3453         # test needs a running copytool
3454         copytool_setup
3455
3456         mkdir -p $DIR/$tdir
3457         local f=$DIR/$tdir/$tfile
3458         local fid
3459         fid=$(make_large_for_progress $f)
3460         [ $? != 0 ] && skip "not enough free space" && return
3461
3462         # if cdt is on, it can serve too quickly the request
3463         cdt_disable
3464         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3465         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3466                         $HSM_PARAM.actions |\
3467                         grep $fid | cut -f16 -d=")
3468         cdt_enable
3469
3470         [[ "$data1" == "$DATAHEX" ]] ||
3471                 error "Data field in records is ($data1) and not ($DATAHEX)"
3472
3473         copytool_cleanup
3474 }
3475 run_test 104 "Copy tool data field"
3476
3477 test_105() {
3478         mkdir -p $DIR/$tdir
3479         local i=""
3480
3481         cdt_disable
3482         for i in $(seq -w 1 10); do
3483                 cp /etc/passwd $DIR/$tdir/$i
3484                 $LFS hsm_archive $DIR/$tdir/$i
3485         done
3486         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3487                         $HSM_PARAM.actions |\
3488                         grep WAITING | wc -l")
3489         cdt_restart
3490         cdt_disable
3491         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3492                         $HSM_PARAM.actions |\
3493                         grep WAITING | wc -l")
3494         cdt_enable
3495         cdt_purge
3496         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3497                 error "Requests count after shutdown $reqcnt2 != "\
3498                       "before shutdown $reqcnt1"
3499 }
3500 run_test 105 "Restart of coordinator"
3501
3502 get_agent_by_uuid_mdt() {
3503         local uuid=$1
3504         local mdtidx=$2
3505         local mds=mds$(($mdtidx + 1))
3506         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
3507                  grep $uuid"
3508 }
3509
3510 check_agent_registered_by_mdt() {
3511         local uuid=$1
3512         local mdtidx=$2
3513         local mds=mds$(($mdtidx + 1))
3514         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
3515         if [[ ! -z "$agent" ]]; then
3516                 echo "found agent $agent on $mds"
3517         else
3518                 error "uuid $uuid not found in agent list on $mds"
3519         fi
3520 }
3521
3522 check_agent_unregistered_by_mdt() {
3523         local uuid=$1
3524         local mdtidx=$2
3525         local mds=mds$(($mdtidx + 1))
3526         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
3527         if [[ -z "$agent" ]]; then
3528                 echo "uuid not found in agent list on $mds"
3529         else
3530                 error "uuid found in agent list on $mds: $agent"
3531         fi
3532 }
3533
3534 check_agent_registered() {
3535         local uuid=$1
3536         local mdsno
3537         for mdsno in $(seq 1 $MDSCOUNT); do
3538                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
3539         done
3540 }
3541
3542 check_agent_unregistered() {
3543         local uuid=$1
3544         local mdsno
3545         for mdsno in $(seq 1 $MDSCOUNT); do
3546                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
3547         done
3548 }
3549
3550 get_agent_uuid() {
3551         local agent=${1:-$(facet_active_host $SINGLEAGT)}
3552
3553         # Lustre mount-point is mandatory and last parameter on
3554         # copytool cmd-line.
3555         local mntpnt=$(do_rpc_nodes $agent pgrep -fl $HSMTOOL_BASE |
3556                        grep -v pgrep | awk '{print $NF}')
3557         [ -n "$mntpnt" ] || error "Found no Agent or with no mount-point "\
3558                                   "parameter"
3559         do_rpc_nodes $agent get_client_uuid $mntpnt | cut -d' ' -f2
3560 }
3561
3562 test_106() {
3563         # test needs a running copytool
3564         copytool_setup
3565
3566         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3567
3568         check_agent_registered $uuid
3569
3570         search_copytools || error "No copytool found"
3571
3572         copytool_cleanup
3573         check_agent_unregistered $uuid
3574
3575         copytool_setup
3576         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3577         check_agent_registered $uuid
3578
3579         copytool_cleanup
3580 }
3581 run_test 106 "Copytool register/unregister"
3582
3583 test_107() {
3584         # test needs a running copytool
3585         copytool_setup
3586         # create and archive file
3587         mkdir -p $DIR/$tdir
3588         local f1=$DIR/$tdir/$tfile
3589         local fid=$(copy_file /etc/passwd $f1)
3590         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3591         wait_request_state $fid ARCHIVE SUCCEED
3592         # shutdown and restart MDS
3593         fail $SINGLEMDS
3594         # check the copytool still gets messages from MDT
3595         local f2=$DIR/$tdir/2
3596         local fid=$(copy_file /etc/passwd $f2)
3597         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3598         # main check of this sanity: this request MUST succeed
3599         wait_request_state $fid ARCHIVE SUCCEED
3600         copytool_cleanup
3601 }
3602 run_test 107 "Copytool re-register after MDS restart"
3603
3604 policy_set_and_test()
3605 {
3606         local change="$1"
3607         local target="$2"
3608         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3609         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3610         [[ "$policy" == "$target" ]] ||
3611                 error "Wrong policy after '$change': '$policy' != '$target'"
3612 }
3613
3614 test_109() {
3615         # to force default policy setting if error
3616         CDT_POLICY_HAD_CHANGED=true
3617
3618         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3619         local default="NonBlockingRestore [NoRetryAction]"
3620         [[ "$policy" == "$default" ]] ||
3621                 error "default policy has changed,"\
3622                       " '$policy' != '$default' update the test"
3623         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3624         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3625         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3626         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3627         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
3628         # useless bacause we know but safer for futur changes to use real value
3629         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3630         echo "Next set_param must failed"
3631         policy_set_and_test "wrong" "$policy"
3632
3633         # return to default
3634         echo "Back to default policy"
3635         cdt_set_sanity_policy
3636 }
3637 run_test 109 "Policy display/change"
3638
3639 test_110a() {
3640         # test needs a running copytool
3641         copytool_setup
3642
3643         mkdir -p $DIR/$tdir
3644
3645         copy2archive /etc/passwd $tdir/$tfile
3646
3647         local f=$DIR/$tdir/$tfile
3648         import_file $tdir/$tfile $f
3649         local fid=$(path2fid $f)
3650
3651         cdt_set_non_blocking_restore
3652         md5sum $f
3653         local st=$?
3654
3655         # cleanup
3656         wait_request_state $fid RESTORE SUCCEED
3657         cdt_clear_non_blocking_restore
3658
3659         # Test result
3660         [[ $st == 1 ]] ||
3661                 error "md5sum returns $st != 1, "\
3662                         "should also perror ENODATA (No data available)"
3663
3664         copytool_cleanup
3665 }
3666 run_test 110a "Non blocking restore policy (import case)"
3667
3668 test_110b() {
3669         # test needs a running copytool
3670         copytool_setup
3671
3672         mkdir -p $DIR/$tdir
3673         local f=$DIR/$tdir/$tfile
3674         local fid=$(copy_file /etc/passwd $f)
3675         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3676         wait_request_state $fid ARCHIVE SUCCEED
3677         $LFS hsm_release $f
3678
3679         cdt_set_non_blocking_restore
3680         md5sum $f
3681         local st=$?
3682
3683         # cleanup
3684         wait_request_state $fid RESTORE SUCCEED
3685         cdt_clear_non_blocking_restore
3686
3687         # Test result
3688         [[ $st == 1 ]] ||
3689                 error "md5sum returns $st != 1, "\
3690                         "should also perror ENODATA (No data available)"
3691
3692         copytool_cleanup
3693 }
3694 run_test 110b "Non blocking restore policy (release case)"
3695
3696 test_111a() {
3697         # test needs a running copytool
3698         copytool_setup
3699
3700         mkdir -p $DIR/$tdir
3701         copy2archive /etc/passwd $tdir/$tfile
3702
3703         local f=$DIR/$tdir/$tfile
3704
3705         import_file $tdir/$tfile $f
3706         local fid=$(path2fid $f)
3707
3708         cdt_set_no_retry
3709
3710         copytool_remove_backend $fid
3711
3712         $LFS hsm_restore $f
3713         wait_request_state $fid RESTORE FAILED
3714         local st=$?
3715
3716         # cleanup
3717         cdt_clear_no_retry
3718
3719         # Test result
3720         [[ $st == 0 ]] || error "Restore does not failed"
3721
3722         copytool_cleanup
3723 }
3724 run_test 111a "No retry policy (import case), restore will error"\
3725               " (No such file or directory)"
3726
3727 test_111b() {
3728         # test needs a running copytool
3729         copytool_setup
3730
3731         mkdir -p $DIR/$tdir
3732         local f=$DIR/$tdir/$tfile
3733         local fid=$(copy_file /etc/passwd $f)
3734         cdt_set_no_retry
3735         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3736         wait_request_state $fid ARCHIVE SUCCEED
3737         $LFS hsm_release $f
3738
3739         copytool_remove_backend $fid
3740
3741         $LFS hsm_restore $f
3742         wait_request_state $fid RESTORE FAILED
3743         local st=$?
3744
3745         # cleanup
3746         cdt_clear_no_retry
3747
3748         # Test result
3749         [[ $st == 0 ]] || error "Restore does not failed"
3750
3751         copytool_cleanup
3752 }
3753 run_test 111b "No retry policy (release case), restore will error"\
3754               " (No such file or directory)"
3755
3756 test_112() {
3757         # test needs a running copytool
3758         copytool_setup
3759
3760         mkdir -p $DIR/$tdir
3761         local f=$DIR/$tdir/$tfile
3762         local fid=$(copy_file /etc/passwd $f)
3763         cdt_disable
3764         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3765         local l=$($LFS hsm_action $f)
3766         echo $l
3767         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
3768
3769         # cleanup
3770         cdt_enable
3771         wait_request_state $fid ARCHIVE SUCCEED
3772
3773         # Test result
3774         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
3775
3776         copytool_cleanup
3777 }
3778 run_test 112 "State of recorded request"
3779
3780 test_200() {
3781         # test needs a running copytool
3782         copytool_setup
3783
3784         mkdir -p $DIR/$tdir
3785         local f=$DIR/$tdir/$tfile
3786         local fid
3787         fid=$(make_large_for_cancel $f)
3788         [ $? != 0 ] && skip "not enough free space" && return
3789
3790         # test with cdt on is made in test_221
3791         cdt_disable
3792         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3793         # wait archive to register at CDT
3794         wait_request_state $fid ARCHIVE WAITING
3795         $LFS hsm_cancel $f
3796         cdt_enable
3797         wait_request_state $fid ARCHIVE CANCELED
3798         wait_request_state $fid CANCEL SUCCEED
3799
3800         copytool_cleanup
3801 }
3802 run_test 200 "Register/Cancel archive"
3803
3804 test_201() {
3805         # test needs a running copytool
3806         copytool_setup
3807
3808         mkdir -p $DIR/$tdir
3809         local f=$DIR/$tdir/$tfile
3810         make_archive $tdir/$tfile
3811         import_file $tdir/$tfile $f
3812         local fid=$(path2fid $f)
3813
3814         # test with cdt on is made in test_222
3815         cdt_disable
3816         $LFS hsm_restore $f
3817         # wait restore to register at CDT
3818         wait_request_state $fid RESTORE WAITING
3819         $LFS hsm_cancel $f
3820         cdt_enable
3821         wait_request_state $fid RESTORE CANCELED
3822         wait_request_state $fid CANCEL SUCCEED
3823
3824         copytool_cleanup
3825 }
3826 run_test 201 "Register/Cancel restore"
3827
3828 test_202() {
3829         # test needs a running copytool
3830         copytool_setup
3831
3832         mkdir -p $DIR/$tdir
3833         local f=$DIR/$tdir/$tfile
3834         local fid
3835         fid=$(make_large_for_progress $f)
3836         [ $? != 0 ] && skip "not enough free space" && return
3837
3838         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3839         wait_request_state $fid ARCHIVE SUCCEED
3840
3841         cdt_disable
3842         $LFS hsm_remove $f
3843         # wait remove to register at CDT
3844         wait_request_state $fid REMOVE WAITING
3845         $LFS hsm_cancel $f
3846         cdt_enable
3847         wait_request_state $fid REMOVE CANCELED
3848
3849         copytool_cleanup
3850 }
3851 run_test 202 "Register/Cancel remove"
3852
3853 test_220() {
3854         # test needs a running copytool
3855         copytool_setup
3856
3857         mkdir -p $DIR/$tdir
3858
3859         local f=$DIR/$tdir/$tfile
3860         local fid=$(copy_file /etc/passwd $f)
3861
3862         changelog_setup
3863
3864         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3865         wait_request_state $fid ARCHIVE SUCCEED
3866
3867         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3868         changelog_cleanup
3869
3870         local target=0x0
3871         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3872
3873         copytool_cleanup
3874 }
3875 run_test 220 "Changelog for archive"
3876
3877 test_221() {
3878         # test needs a running copytool
3879         copytool_setup
3880
3881         mkdir -p $DIR/$tdir
3882
3883         local f=$DIR/$tdir/$tfile
3884         local fid
3885         fid=$(make_large_for_cancel $f)
3886         [ $? != 0 ] && skip "not enough free space" && return
3887
3888         changelog_setup
3889
3890         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3891         wait_request_state $fid ARCHIVE STARTED
3892         $LFS hsm_cancel $f
3893         wait_request_state $fid ARCHIVE CANCELED
3894         wait_request_state $fid CANCEL SUCCEED
3895
3896         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3897
3898         local target=0x7d
3899         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3900
3901         cleanup
3902 }
3903 run_test 221 "Changelog for archive canceled"
3904
3905 test_222a() {
3906         # test needs a running copytool
3907         copytool_setup
3908
3909         mkdir -p $DIR/$tdir
3910         copy2archive /etc/passwd $tdir/$tfile
3911
3912         local f=$DIR/$tdir/$tfile
3913         import_file $tdir/$tfile $f
3914         local fid=$(path2fid $f)
3915
3916         changelog_setup
3917
3918         $LFS hsm_restore $f
3919         wait_request_state $fid RESTORE SUCCEED
3920
3921         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3922
3923         local target=0x80
3924         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3925
3926         cleanup
3927 }
3928 run_test 222a "Changelog for explicit restore"
3929
3930 test_222b() {
3931         # test needs a running copytool
3932         copytool_setup
3933
3934         mkdir -p $DIR/$tdir
3935         local f=$DIR/$tdir/$tfile
3936         local fid=$(copy_file /etc/passwd $f)
3937
3938         changelog_setup
3939         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3940         wait_request_state $fid ARCHIVE SUCCEED
3941         $LFS hsm_release $f
3942
3943         md5sum $f
3944
3945         wait_request_state $fid RESTORE SUCCEED
3946
3947         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3948
3949         local target=0x80
3950         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3951
3952         cleanup
3953 }
3954 run_test 222b "Changelog for implicit restore"
3955
3956 test_223a() {
3957         # test needs a running copytool
3958         copytool_setup
3959
3960         mkdir -p $DIR/$tdir
3961
3962         local f=$DIR/$tdir/$tfile
3963         make_archive $tdir/$tfile
3964
3965         changelog_setup
3966
3967         import_file $tdir/$tfile $f
3968         local fid=$(path2fid $f)
3969
3970         $LFS hsm_restore $f
3971         wait_request_state $fid RESTORE STARTED
3972         $LFS hsm_cancel $f
3973         wait_request_state $fid RESTORE CANCELED
3974         wait_request_state $fid CANCEL SUCCEED
3975
3976         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3977
3978         local target=0xfd
3979         [[ $flags == $target ]] ||
3980                 error "Changelog flag is $flags not $target"
3981
3982         cleanup
3983 }
3984 run_test 223a "Changelog for restore canceled (import case)"
3985
3986 test_223b() {
3987         # test needs a running copytool
3988         copytool_setup
3989
3990         mkdir -p $DIR/$tdir
3991
3992         local f=$DIR/$tdir/$tfile
3993         local fid
3994         fid=$(make_large_for_progress $f)
3995         [ $? != 0 ] && skip "not enough free space" && return
3996
3997         changelog_setup
3998         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3999         wait_request_state $fid ARCHIVE SUCCEED
4000         $LFS hsm_release $f
4001         $LFS hsm_restore $f
4002         wait_request_state $fid RESTORE STARTED
4003         $LFS hsm_cancel $f
4004         wait_request_state $fid RESTORE CANCELED
4005         wait_request_state $fid CANCEL SUCCEED
4006
4007         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
4008
4009         local target=0xfd
4010         [[ $flags == $target ]] ||
4011                 error "Changelog flag is $flags not $target"
4012
4013         cleanup
4014 }
4015 run_test 223b "Changelog for restore canceled (release case)"
4016
4017 test_224() {
4018         # test needs a running copytool
4019         copytool_setup
4020
4021         mkdir -p $DIR/$tdir
4022
4023         local f=$DIR/$tdir/$tfile
4024         local fid=$(copy_file /etc/passwd $f)
4025
4026         changelog_setup
4027         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4028         wait_request_state $fid ARCHIVE SUCCEED
4029
4030         $LFS hsm_remove $f
4031         wait_request_state $fid REMOVE SUCCEED
4032
4033         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
4034
4035         local target=0x200
4036         [[ $flags == $target ]] ||
4037                 error "Changelog flag is $flags not $target"
4038
4039         cleanup
4040 }
4041 run_test 224 "Changelog for remove"
4042
4043 test_225() {
4044         # test needs a running copytool
4045         copytool_setup
4046
4047         # test is not usable because remove request is too fast
4048         # so it is always finished before cancel can be done ...
4049         echo "Test disabled"
4050         copytool_cleanup
4051         return 0
4052
4053         mkdir -p $DIR/$tdir
4054         local f=$DIR/$tdir/$tfile
4055         local fid
4056         fid=$(make_large_for_progress $f)
4057         [ $? != 0 ] && skip "not enough free space" && return
4058
4059         changelog_setup
4060         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4061         wait_request_state $fid ARCHIVE SUCCEED
4062
4063         # if cdt is on, it can serve too quickly the request
4064         cdt_disable
4065         $LFS hsm_remove $f
4066         $LFS hsm_cancel $f
4067         cdt_enable
4068         wait_request_state $fid REMOVE CANCELED
4069         wait_request_state $fid CANCEL SUCCEED
4070
4071         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4072         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
4073                 tail -n 1 | awk '{print $5}')
4074
4075         local target=0x27d
4076         [[ $flags == $target ]] ||
4077                 error "Changelog flag is $flags not $target"
4078
4079         cleanup
4080 }
4081 run_test 225 "Changelog for remove canceled"
4082
4083 test_226() {
4084         # test needs a running copytool
4085         copytool_setup
4086
4087         mkdir -p $DIR/$tdir
4088
4089         local f1=$DIR/$tdir/$tfile-1
4090         local f2=$DIR/$tdir/$tfile-2
4091         local f3=$DIR/$tdir/$tfile-3
4092         local fid1=$(copy_file /etc/passwd $f1)
4093         local fid2=$(copy_file /etc/passwd $f2)
4094         copy_file /etc/passwd $f3
4095
4096         changelog_setup
4097         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4098         wait_request_state $fid1 ARCHIVE SUCCEED
4099
4100         $LFS hsm_archive $f2
4101         wait_request_state $fid2 ARCHIVE SUCCEED
4102
4103         rm $f1 || error "rm $f1 failed"
4104
4105         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
4106
4107         local target=0x3
4108         [[ $flags == $target ]] ||
4109                 error "Changelog flag is $flags not $target"
4110
4111         mv $f3 $f2 || error "mv $f3 $f2 failed"
4112
4113         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
4114
4115         target=0x3
4116         [[ $flags == $target ]] ||
4117                 error "Changelog flag is $flags not $target"
4118
4119         cleanup
4120 }
4121 run_test 226 "changelog for last rm/mv with exiting archive"
4122
4123 check_flags_changes() {
4124         local f=$1
4125         local fid=$2
4126         local hsm_flag=$3
4127         local fst=$4
4128         local cnt=$5
4129
4130         local target=0x280
4131         $LFS hsm_set --$hsm_flag $f ||
4132                 error "Cannot set $hsm_flag on $f"
4133         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4134         local seen=${#flags[*]}
4135         cnt=$((fst + cnt))
4136         [[ $seen == $cnt ]] ||
4137                 error "set $hsm_flag: Changelog events $seen != $cnt"
4138         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4139                 error "set $hsm_flag: Changelog flags are "\
4140                         "${flags[$((cnt - 1))]} not $target"
4141
4142         $LFS hsm_clear --$hsm_flag $f ||
4143                 error "Cannot clear $hsm_flag on $f"
4144         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
4145         seen=${#flags[*]}
4146         cnt=$(($cnt + 1))
4147         [[ $cnt == $seen ]] ||
4148                 error "clear $hsm_flag: Changelog events $seen != $cnt"
4149
4150         [[ ${flags[$((cnt - 1))]} == $target ]] ||
4151                 error "clear $hsm_flag: Changelog flag is "\
4152                         "${flags[$((cnt - 1))]} not $target"
4153 }
4154
4155 test_227() {
4156         # test needs a running copytool
4157         copytool_setup
4158         changelog_setup
4159
4160         mkdir -p $DIR/$tdir
4161         typeset -a flags
4162
4163         for i in norelease noarchive exists archived
4164         do
4165                 local f=$DIR/$tdir/$tfile-$i
4166                 local fid=$(copy_file /etc/passwd $f)
4167                 check_flags_changes $f $fid $i 0 1
4168         done
4169
4170         f=$DIR/$tdir/$tfile---lost
4171         fid=$(copy_file /etc/passwd $f)
4172         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4173         wait_request_state $fid ARCHIVE SUCCEED
4174         check_flags_changes $f $fid lost 3 1
4175
4176         cleanup
4177 }
4178 run_test 227 "changelog when explicit setting of HSM flags"
4179
4180 test_228() {
4181         # test needs a running copytool
4182         copytool_setup
4183
4184         local fid=$(make_small_sync $DIR/$tfile)
4185         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4186         wait_request_state $fid ARCHIVE SUCCEED
4187
4188         $LFS hsm_release $DIR/$tfile
4189         check_hsm_flags $DIR/$tfile "0x0000000d"
4190
4191         filefrag $DIR/$tfile | grep " 1 extent found" ||
4192                 error "filefrag on released file must return only one extent"
4193
4194         # only newer versions of cp detect sparse files by stat/FIEMAP
4195         # (LU-2580)
4196         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4197                 error "copying $DIR/$tfile"
4198         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4199
4200         $LFS hsm_release $DIR/$tfile
4201         check_hsm_flags $DIR/$tfile "0x0000000d"
4202
4203         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4204
4205         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4206                 error "tar failed"
4207         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4208                 error "comparing untarred $DIR/$tfile"
4209
4210         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4211                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4212         copytool_cleanup
4213 }
4214 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4215
4216 test_250() {
4217         # test needs a running copytool
4218         copytool_setup
4219
4220         mkdir -p $DIR/$tdir
4221         local maxrequest=$(get_hsm_param max_requests)
4222         local rqcnt=$(($maxrequest * 3))
4223         local i=""
4224
4225         cdt_disable
4226         for i in $(seq -w 1 $rqcnt); do
4227                 rm -f $DIR/$tdir/$i
4228                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
4229         done
4230         # we do it in 2 steps, so all requests arrive at the same time
4231         for i in $(seq -w 1 $rqcnt); do
4232                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
4233         done
4234         cdt_enable
4235         local cnt=$rqcnt
4236         local wt=$rqcnt
4237         while [[ $cnt != 0 || $wt != 0 ]]; do
4238                 sleep 1
4239                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4240                         $HSM_PARAM.actions |\
4241                         grep STARTED | grep -v CANCEL | wc -l")
4242                 [[ $cnt -le $maxrequest ]] ||
4243                         error "$cnt > $maxrequest too many started requests"
4244                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4245                         $HSM_PARAM.actions |\
4246                         grep WAITING | wc -l")
4247                 echo "max=$maxrequest started=$cnt waiting=$wt"
4248         done
4249
4250         copytool_cleanup
4251 }
4252 run_test 250 "Coordinator max request"
4253
4254 test_251() {
4255         # test needs a running copytool
4256         copytool_setup
4257
4258         mkdir -p $DIR/$tdir
4259         local f=$DIR/$tdir/$tfile
4260         local fid
4261         fid=$(make_large_for_cancel $f)
4262         [ $? != 0 ] && skip "not enough free space" && return
4263
4264         cdt_disable
4265         # to have a short test
4266         local old_to=$(get_hsm_param active_request_timeout)
4267         set_hsm_param active_request_timeout 4
4268         # to be sure the cdt will wake up frequently so
4269         # it will be able to cancel the "old" request
4270         local old_loop=$(get_hsm_param loop_period)
4271         set_hsm_param loop_period 2
4272         cdt_enable
4273
4274         # clear locks to avoid extra delay caused by flush/cancel
4275         # and thus prevent early copytool death to timeout.
4276         cancel_lru_locks osc
4277
4278         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4279         wait_request_state $fid ARCHIVE STARTED
4280         sleep 5
4281         wait_request_state $fid ARCHIVE CANCELED
4282
4283         set_hsm_param active_request_timeout $old_to
4284         set_hsm_param loop_period $old_loop
4285
4286         copytool_cleanup
4287 }
4288 run_test 251 "Coordinator request timeout"
4289
4290 test_300() {
4291         # the only way to test ondisk conf is to restart MDS ...
4292         echo "Stop coordinator and remove coordinator state at mount"
4293         # stop coordinator
4294         cdt_shutdown
4295         # clean on disk conf set by default
4296         cdt_clear_mount_state
4297         cdt_check_state stopped
4298
4299         # check cdt still off after umount/remount
4300         fail $SINGLEMDS
4301         cdt_check_state stopped
4302
4303         echo "Set coordinator start at mount, and start coordinator"
4304         cdt_set_mount_state enabled
4305
4306         # check cdt is on
4307         cdt_check_state enabled
4308
4309         # check cdt still on after umount/remount
4310         fail $SINGLEMDS
4311         cdt_check_state enabled
4312
4313         # we are back to original state (cdt started at mount)
4314 }
4315 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4316
4317 test_301() {
4318         local ai=$(get_hsm_param default_archive_id)
4319         local new=$((ai + 1))
4320
4321         set_hsm_param default_archive_id $new -P
4322         fail $SINGLEMDS
4323         local res=$(get_hsm_param default_archive_id)
4324
4325         # clear value
4326         set_hsm_param default_archive_id "" "-P -d"
4327
4328         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4329 }
4330 run_test 301 "HSM tunnable are persistent"
4331
4332 test_302() {
4333         local ai=$(get_hsm_param default_archive_id)
4334         local new=$((ai + 1))
4335
4336         # stop coordinator
4337         cdt_shutdown
4338
4339         set_hsm_param default_archive_id $new -P
4340
4341         local mdtno
4342         for mdtno in $(seq 1 $MDSCOUNT); do
4343                 fail mds${mdtno}
4344         done
4345
4346         # check cdt is on
4347         cdt_check_state enabled
4348
4349         local res=$(get_hsm_param default_archive_id)
4350
4351         # clear value
4352         set_hsm_param default_archive_id "" "-P -d"
4353
4354         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4355 }
4356 run_test 302 "HSM tunnable are persistent when CDT is off"
4357
4358 test_400() {
4359         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4360
4361         copytool_setup
4362
4363         mkdir -p $DIR/$tdir
4364
4365         local dir_mdt0=$DIR/$tdir/mdt0
4366         local dir_mdt1=$DIR/$tdir/mdt1
4367
4368         # create 1 dir per MDT
4369         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4370         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4371
4372         # create 1 file in each MDT
4373         local fid1=$(make_small $dir_mdt0/$tfile)
4374         local fid2=$(make_small $dir_mdt1/$tfile)
4375
4376         # check that hsm request on mdt0 is sent to the right MDS
4377         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4378         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4379                 echo "archive successful on mdt0"
4380
4381         # check that hsm request on mdt1 is sent to the right MDS
4382         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
4383         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4384                 echo "archive successful on mdt1"
4385
4386         copytool_cleanup
4387         # clean test files and directories
4388         rm -rf $dir_mdt0 $dir_mdt1
4389 }
4390 run_test 400 "Single request is sent to the right MDT"
4391
4392 test_401() {
4393         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4394
4395         copytool_setup
4396
4397         mkdir -p $DIR/$tdir
4398
4399         local dir_mdt0=$DIR/$tdir/mdt0
4400         local dir_mdt1=$DIR/$tdir/mdt1
4401
4402         # create 1 dir per MDT
4403         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4404         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4405
4406         # create 1 file in each MDT
4407         local fid1=$(make_small $dir_mdt0/$tfile)
4408         local fid2=$(make_small $dir_mdt1/$tfile)
4409
4410         # check that compound requests are shunt to the rights MDTs
4411         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
4412                 error "lfs hsm_archive"
4413         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4414                 echo "archive successful on mdt0"
4415         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4416                 echo "archive successful on mdt1"
4417
4418         copytool_cleanup
4419         # clean test files and directories
4420         rm -rf $dir_mdt0 $dir_mdt1
4421 }
4422 run_test 401 "Compound requests split and sent to their respective MDTs"
4423
4424 mdc_change_state() # facet, MDT_pattern, activate|deactivate
4425 {
4426         local facet=$1
4427         local pattern="$2"
4428         local state=$3
4429         local node=$(facet_active_host $facet)
4430         local mdc
4431         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
4432                         awk '{print $4}'); do
4433                 echo "$3 $mdc on $node"
4434                 do_facet $facet "$LCTL --device $mdc $state" || return 1
4435         done
4436 }
4437
4438 test_402() {
4439         # make sure there is no running copytool
4440         copytool_cleanup
4441
4442         # deactivate all mdc on agent1
4443         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
4444
4445         HSMTOOL_NOERROR=true copytool_setup $SINGLEAGT
4446
4447         check_agent_unregistered "uuid" # match any agent
4448
4449         # no expected running copytool
4450         search_copytools $agent && error "Copytool start should have failed"
4451
4452         # reactivate MDCs
4453         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
4454 }
4455 run_test 402 "Copytool start fails if all MDTs are inactive"
4456
4457 test_403() {
4458         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4459
4460         # make sure there is no running copytool
4461         copytool_cleanup
4462
4463         local agent=$(facet_active_host $SINGLEAGT)
4464
4465         # deactivate all mdc for MDT0001
4466         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4467
4468         copytool_setup
4469         local uuid=$(get_agent_uuid $agent)
4470         # check the agent is registered on MDT0000, and not on MDT0001
4471         check_agent_registered_by_mdt $uuid 0
4472         check_agent_unregistered_by_mdt $uuid 1
4473
4474         # check running copytool process
4475         search_copytools $agent || error "No running copytools on $agent"
4476
4477         # reactivate all mdc for MDT0001
4478         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4479
4480         # make sure the copytool is now registered to all MDTs
4481         check_agent_registered $uuid
4482
4483         copytool_cleanup
4484 }
4485 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
4486
4487 test_404() {
4488         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4489
4490         copytool_setup
4491
4492         # create files on both MDT0000 and MDT0001
4493         mkdir -p $DIR/$tdir
4494
4495         local dir_mdt0=$DIR/$tdir/mdt0
4496         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4497
4498         # create 1 file on mdt0
4499         local fid1=$(make_small $dir_mdt0/$tfile)
4500
4501         # deactivate all mdc for MDT0001
4502         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4503
4504         # send an HSM request for files in MDT0000
4505         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4506
4507         # check for completion of files in MDT0000
4508         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4509                 echo "archive successful on mdt0"
4510
4511         # reactivate all mdc for MDT0001
4512         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4513
4514         copytool_cleanup
4515         # clean test files and directories
4516         rm -rf $dir_mdt0
4517 }
4518 run_test 404 "Inactive MDT does not block requests for active MDTs"
4519
4520 test_405() {
4521         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4522
4523         copytool_setup
4524
4525         mkdir -p $DIR/$tdir
4526
4527         local striped_dir=$DIR/$tdir/striped_dir
4528
4529         # create striped dir on all of MDTs
4530         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
4531
4532         local fid1=$(make_small_sync $striped_dir/${tfile}_0)
4533         local fid2=$(make_small_sync $striped_dir/${tfile}_1)
4534         local fid3=$(make_small_sync $striped_dir/${tfile}_2)
4535         local fid4=$(make_small_sync $striped_dir/${tfile}_3)
4536
4537         local idx1=$($LFS getstripe -M $striped_dir/${tfile}_0)
4538         local idx2=$($LFS getstripe -M $striped_dir/${tfile}_1)
4539         local idx3=$($LFS getstripe -M $striped_dir/${tfile}_2)
4540         local idx4=$($LFS getstripe -M $striped_dir/${tfile}_3)
4541
4542         # check that compound requests are shunt to the rights MDTs
4543         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
4544                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
4545                 error "lfs hsm_archive"
4546
4547         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
4548                 echo "archive successful on $fid1"
4549         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
4550                 echo "archive successful on $fid2"
4551         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
4552                 echo "archive successful on $fid3"
4553         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
4554                 echo "archive successful on $fid4"
4555
4556         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
4557         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
4558         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
4559         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
4560
4561         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
4562         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
4563         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
4564         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
4565
4566         copytool_cleanup
4567 }
4568 run_test 405 "archive and release under striped directory"
4569
4570 test_500()
4571 {
4572         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
4573                 skip "HSM migrate is not supported" && return
4574
4575         # Stop the existing copytool
4576         copytool_cleanup
4577
4578         test_mkdir -p $DIR/$tdir
4579         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
4580 }
4581 run_test 500 "various LLAPI HSM tests"
4582
4583 copytool_cleanup
4584
4585 complete $SECONDS
4586 check_and_cleanup_lustre
4587 exit_status