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