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