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