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