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