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