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