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