Whamcloud - gitweb
LU-5683 clio: add CIT_DATA_VERSION
[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 test_37() {
2529         # LU-5683: check that an archived dirty file can be rearchived.
2530         copytool_cleanup
2531         copytool_setup $SINGLEAGT $MOUNT2
2532
2533         mkdir -p $DIR/$tdir
2534         local f=$DIR/$tdir/$tfile
2535         local fid
2536
2537         fid=$(make_small $f) || error "cannot create small file"
2538
2539         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2540         wait_request_state $fid ARCHIVE SUCCEED
2541         $LFS hsm_release $f || error "cannot release $f"
2542
2543         # Dirty file.
2544         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
2545
2546         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2547         wait_request_state $fid ARCHIVE SUCCEED
2548
2549         copytool_cleanup
2550 }
2551 run_test 37 "re-archive a dirty file"
2552
2553 multi_archive() {
2554         local prefix=$1
2555         local count=$2
2556         local n=""
2557
2558         for n in $(seq 1 $count); do
2559                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2560         done
2561         echo "$count archive requests submitted"
2562 }
2563
2564 test_40() {
2565         local stream_count=4
2566         local file_count=100
2567         mkdir -p $DIR/$tdir
2568         local f=$DIR/$tdir/$tfile
2569         local i=""
2570         local p=""
2571         local fid=""
2572
2573         for i in $(seq 1 $file_count); do
2574                 for p in $(seq 1 $stream_count); do
2575                         fid=$(copy_file /etc/hosts $f.$p.$i)
2576                 done
2577         done
2578         # force copytool to use a local/temp archive dir to ensure best
2579         # performance vs remote/NFS mounts used in auto-tests
2580         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
2581                 copytool_setup
2582         else
2583                 copytool_setup $SINGLEAGT $MOUNT $HSM_ARCHIVE_NUMBER $TMP/$tdir
2584         fi
2585         # to be sure wait_all_done will not be mislead by previous tests
2586         cdt_purge
2587         wait_for_grace_delay
2588         typeset -a pids
2589         # start archive streams in background (archive files in parallel)
2590         for p in $(seq 1 $stream_count); do
2591                 multi_archive $f.$p $file_count &
2592                 pids[$p]=$!
2593         done
2594         echo -n  "Wait for all requests being enqueued..."
2595         wait ${pids[*]}
2596         echo OK
2597         wait_all_done 100
2598         copytool_cleanup
2599 }
2600 run_test 40 "Parallel archive requests"
2601
2602 test_52() {
2603         # test needs a running copytool
2604         copytool_setup
2605
2606         mkdir -p $DIR/$tdir
2607         local f=$DIR/$tdir/$tfile
2608         local fid=$(copy_file /etc/motd $f 1)
2609
2610         $LFS hsm_archive $f || error "could not archive file"
2611         wait_request_state $fid ARCHIVE SUCCEED
2612         check_hsm_flags $f "0x00000009"
2613
2614         multiop_bg_pause $f O_c || error "multiop failed"
2615         local MULTIPID=$!
2616
2617         mds_evict_client
2618         client_up || client_up || true
2619
2620         kill -USR1 $MULTIPID
2621         wait $MULTIPID || error "multiop close failed"
2622
2623         check_hsm_flags $f "0x0000000b"
2624
2625         copytool_cleanup
2626 }
2627 run_test 52 "Opened for write file on an evicted client should be set dirty"
2628
2629 test_53() {
2630         # test needs a running copytool
2631         copytool_setup
2632
2633         mkdir -p $DIR/$tdir
2634         local f=$DIR/$tdir/$tfile
2635         local fid=$(copy_file /etc/motd $f 1)
2636
2637         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2638                 error "could not archive file"
2639         wait_request_state $fid ARCHIVE SUCCEED
2640         check_hsm_flags $f "0x00000009"
2641
2642         multiop_bg_pause $f o_c || error "multiop failed"
2643         MULTIPID=$!
2644
2645         mds_evict_client
2646         client_up || client_up || true
2647
2648         kill -USR1 $MULTIPID
2649         wait $MULTIPID || error "multiop close failed"
2650
2651         check_hsm_flags $f "0x00000009"
2652
2653         copytool_cleanup
2654 }
2655 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2656
2657 test_54() {
2658         # test needs a running copytool
2659         copytool_setup
2660
2661         mkdir -p $DIR/$tdir
2662         local f=$DIR/$tdir/$tfile
2663         local fid=$(make_large_for_progress $f)
2664
2665         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2666                 error "could not archive file"
2667         wait_request_state $fid ARCHIVE STARTED
2668
2669         check_hsm_flags $f "0x00000001"
2670
2671         # Avoid coordinator resending this request as soon it has failed.
2672         cdt_set_no_retry
2673
2674         echo "foo" >> $f
2675         sync
2676         wait_request_state $fid ARCHIVE FAILED
2677
2678         check_hsm_flags $f "0x00000003"
2679
2680         cdt_clear_no_retry
2681         copytool_cleanup
2682 }
2683 run_test 54 "Write during an archive cancels it"
2684
2685 test_55() {
2686         # test needs a running copytool
2687         copytool_setup
2688
2689         mkdir -p $DIR/$tdir
2690         local f=$DIR/$tdir/$tfile
2691         local fid=$(make_large_for_progress $f)
2692
2693         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2694                 error "could not archive file"
2695         wait_request_state $fid ARCHIVE STARTED
2696
2697         check_hsm_flags $f "0x00000001"
2698
2699         # Avoid coordinator resending this request as soon it has failed.
2700         cdt_set_no_retry
2701
2702         $TRUNCATE $f 1024 || error "truncate failed"
2703         sync
2704         wait_request_state $fid ARCHIVE FAILED
2705
2706         check_hsm_flags $f "0x00000003"
2707
2708         cdt_clear_no_retry
2709         copytool_cleanup
2710 }
2711 run_test 55 "Truncate during an archive cancels it"
2712
2713 test_56() {
2714         # test needs a running copytool
2715         copytool_setup
2716
2717         mkdir -p $DIR/$tdir
2718         local f=$DIR/$tdir/$tfile
2719         local fid
2720         fid=$(make_large_for_progress $f)
2721         [ $? != 0 ] && skip "not enough free space" && return
2722
2723         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2724                 error "could not archive file"
2725         wait_request_state $fid ARCHIVE STARTED
2726
2727         check_hsm_flags $f "0x00000001"
2728
2729         # Change metadata and sync to be sure we are not changing only
2730         # in memory.
2731         chmod 644 $f
2732         chgrp sys $f
2733         sync
2734         wait_request_state $fid ARCHIVE SUCCEED
2735
2736         check_hsm_flags $f "0x00000009"
2737
2738         copytool_cleanup
2739 }
2740 run_test 56 "Setattr during an archive is ok"
2741
2742 test_57() {
2743         # Need one client for I/O, one for request
2744         needclients 2 || return 0
2745
2746         # test needs a running copytool
2747         copytool_setup
2748
2749         mkdir -p $DIR/$tdir
2750         local f=$DIR/$tdir/test_archive_remote
2751         # Create a file on a remote node
2752         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2753                 "count=2 conv=fsync"
2754
2755         # And archive it
2756         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2757                 error "hsm_archive failed"
2758         local fid=$(path2fid $f)
2759         wait_request_state $fid ARCHIVE SUCCEED
2760
2761         # Release and implicit restore it
2762         do_node $CLIENT2 "$LFS hsm_release $f" ||
2763                 error "hsm_release failed"
2764         do_node $CLIENT2 "md5sum $f" ||
2765                 error "hsm_restore failed"
2766
2767         wait_request_state $fid RESTORE SUCCEED
2768
2769         copytool_cleanup
2770 }
2771 run_test 57 "Archive a file with dirty cache on another node"
2772
2773 truncate_released_file() {
2774         local src_file=$1
2775         local trunc_to=$2
2776
2777         local sz=$(stat -c %s $src_file)
2778         local f=$DIR/$tdir/$tfile
2779         local fid=$(copy_file $1 $f)
2780         local ref=$f-ref
2781         cp $f $f-ref
2782
2783         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2784                 error "could not archive file"
2785         wait_request_state $fid ARCHIVE SUCCEED
2786
2787         $LFS hsm_release $f || error "could not release file"
2788
2789         $TRUNCATE $f $trunc_to || error "truncate failed"
2790         sync
2791
2792         local sz1=$(stat -c %s $f)
2793         [[ $sz1 == $trunc_to ]] ||
2794                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2795
2796         $LFS hsm_state $f
2797         check_hsm_flags $f "0x0000000b"
2798
2799         local state=$(get_request_state $fid RESTORE)
2800         [[ "$state" == "SUCCEED" ]] ||
2801                 error "truncate $sz does not trig restore, state = $state"
2802
2803         $TRUNCATE $ref $trunc_to
2804         cmp $ref $f || error "file data wrong after truncate"
2805
2806         rm -f $f $f-ref
2807 }
2808
2809 test_58() {
2810         # test needs a running copytool
2811         copytool_setup
2812
2813         mkdir -p $DIR/$tdir
2814
2815         local sz=$(stat -c %s /etc/passwd)
2816
2817         echo "truncate up from $sz to $((sz*2))"
2818         truncate_released_file /etc/passwd $((sz*2))
2819
2820         echo "truncate down from $sz to $((sz/2))"
2821         truncate_released_file /etc/passwd $((sz/2))
2822
2823         echo "truncate to 0"
2824         truncate_released_file /etc/passwd 0
2825
2826         copytool_cleanup
2827 }
2828 run_test 58 "Truncate a released file will trigger restore"
2829
2830 test_60() {
2831         # This test validates the fix for LU-4512. Ensure that the -u
2832         # option changes the progress reporting interval from the
2833         # default (30 seconds) to the user-specified interval.
2834         local interval=5
2835         local progress_timeout=$((interval * 4))
2836
2837         # test needs a new running copytool
2838         copytool_cleanup
2839         HSMTOOL_UPDATE_INTERVAL=$interval copytool_setup
2840
2841         mkdir -p $DIR/$tdir
2842         local f=$DIR/$tdir/$tfile
2843         local fid
2844         fid=$(make_large_for_progress $f)
2845         [ $? != 0 ] && skip "not enough free space" && return
2846
2847         local mdtidx=0
2848         local mdt=${MDT_PREFIX}${mdtidx}
2849         local mds=mds$((mdtidx + 1))
2850
2851         # Wait for copytool to register
2852         wait_update_facet $mds \
2853                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
2854                 uuid 100 || error "coyptool failed to register with $mdt"
2855
2856         local start_at=$(date +%s)
2857         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2858                 error "could not archive file"
2859
2860         local agent=$(facet_active_host $SINGLEAGT)
2861         local prefix=$TESTLOG_PREFIX
2862         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
2863         local copytool_log=$prefix.copytool_log.$agent.log
2864
2865
2866         wait_update $agent \
2867             "grep -o start.copy $copytool_log" "start copy" 100 ||
2868                 error "copytool failed to start"
2869
2870         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
2871         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
2872
2873         local RESULT
2874         local WAIT=0
2875         local sleep=1
2876
2877         echo -n "Expecting a progress update within $progress_timeout seconds... "
2878         while [ true ]; do
2879                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
2880                 if [ $RESULT -gt 0 ]; then
2881                         echo "$RESULT bytes copied in $WAIT seconds."
2882                         break
2883                 elif [ $WAIT -ge $progress_timeout ]; then
2884                         error "Timed out waiting for progress update!"
2885                         break
2886                 fi
2887                 WAIT=$((WAIT + sleep))
2888                 sleep $sleep
2889         done
2890
2891         local finish_at=$(date +%s)
2892         local elapsed=$((finish_at - start_at))
2893
2894         # Ensure that the progress update occurred within the expected window.
2895         if [ $elapsed -lt $interval ]; then
2896                 error "Expected progress update after at least $interval seconds"
2897         fi
2898
2899         cdt_clear_no_retry
2900         copytool_cleanup
2901 }
2902 run_test 60 "Changing progress update interval from default"
2903
2904 test_70() {
2905         # test needs a new running copytool
2906         copytool_cleanup
2907         copytool_monitor_setup
2908         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
2909
2910         # Just start and stop the copytool to generate events.
2911         cdt_clear_no_retry
2912
2913         # Wait for the copytool to register.
2914         wait_update --verbose $(facet_active_host mds1) \
2915                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
2916                 uuid 100 ||
2917                 error "copytool failed to register with MDT0000"
2918
2919         copytool_cleanup
2920
2921         local REGISTER_EVENT
2922         local UNREGISTER_EVENT
2923         while read event; do
2924                 local parsed=$(parse_json_event "$event")
2925                 if [ -z "$parsed" ]; then
2926                         error "Copytool sent malformed event: $event"
2927                 fi
2928                 eval $parsed
2929
2930                 if [ $event_type == "REGISTER" ]; then
2931                         REGISTER_EVENT=$event
2932                 elif [ $event_type == "UNREGISTER" ]; then
2933                         UNREGISTER_EVENT=$event
2934                 fi
2935         done < <(echo $"$(get_copytool_event_log)")
2936
2937         if [ -z "$REGISTER_EVENT" ]; then
2938                 error "Copytool failed to send register event to FIFO"
2939         fi
2940
2941         if [ -z "$UNREGISTER_EVENT" ]; then
2942                 error "Copytool failed to send unregister event to FIFO"
2943         fi
2944
2945         copytool_monitor_cleanup
2946         echo "Register/Unregister events look OK."
2947 }
2948 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
2949
2950 test_71() {
2951         # Bump progress interval for livelier events.
2952         local interval=5
2953
2954         # test needs a new running copytool
2955         copytool_cleanup
2956         copytool_monitor_setup
2957         HSMTOOL_UPDATE_INTERVAL=$interval \
2958         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
2959
2960         mkdir -p $DIR/$tdir
2961         local f=$DIR/$tdir/$tfile
2962         local fid
2963         fid=$(make_large_for_progress $f)
2964         [ $? != 0 ] && skip "not enough free space" && return
2965
2966         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2967                 error "could not archive file"
2968         wait_request_state $fid ARCHIVE SUCCEED
2969
2970         local expected_fields="event_time data_fid source_fid"
2971         expected_fields+=" total_bytes current_bytes"
2972
2973         local START_EVENT
2974         local FINISH_EVENT
2975         while read event; do
2976                 # Make sure we're not getting anything from previous events.
2977                 for field in $expected_fields; do
2978                         unset $field
2979                 done
2980
2981                 local parsed=$(parse_json_event "$event")
2982                 if [ -z "$parsed" ]; then
2983                         error "Copytool sent malformed event: $event"
2984                 fi
2985                 eval $parsed
2986
2987                 if [ $event_type == "ARCHIVE_START" ]; then
2988                         START_EVENT=$event
2989                         continue
2990                 elif [ $event_type == "ARCHIVE_FINISH" ]; then
2991                         FINISH_EVENT=$event
2992                         continue
2993                 elif [ $event_type != "ARCHIVE_RUNNING" ]; then
2994                         continue
2995                 fi
2996
2997                 # Do some simple checking of the progress update events.
2998                 for expected_field in $expected_fields; do
2999                         if [ -z ${!expected_field+x} ]; then
3000                                 error "Missing $expected_field field in event"
3001                         fi
3002                 done
3003
3004                 if [ $total_bytes -eq 0 ]; then
3005                         error "Expected total_bytes to be > 0"
3006                 fi
3007
3008                 # These should be identical throughout an archive
3009                 # operation.
3010                 if [ $source_fid != $data_fid ]; then
3011                         error "Expected source_fid to equal data_fid"
3012                 fi
3013         done < <(echo $"$(get_copytool_event_log)")
3014
3015         if [ -z "$START_EVENT" ]; then
3016                 error "Copytool failed to send archive start event to FIFO"
3017         fi
3018
3019         if [ -z "$FINISH_EVENT" ]; then
3020                 error "Copytool failed to send archive finish event to FIFO"
3021         fi
3022
3023         echo "Archive events look OK."
3024
3025         cdt_clear_no_retry
3026         copytool_cleanup
3027         copytool_monitor_cleanup
3028 }
3029 run_test 71 "Copytool logs JSON archive events to FIFO"
3030
3031 test_72() {
3032         # Bump progress interval for livelier events.
3033         local interval=5
3034
3035         # test needs a new running copytool
3036         copytool_cleanup
3037         copytool_monitor_setup
3038         HSMTOOL_UPDATE_INTERVAL=$interval \
3039         HSMTOOL_EVENT_FIFO=$HSMTOOL_MONITOR_DIR/fifo copytool_setup
3040         local test_file=$HSMTOOL_MONITOR_DIR/file
3041
3042         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3043         cmd+="conv=fsync"
3044         do_facet $SINGLEAGT "$cmd" ||
3045                 error "cannot create $test_file on $SINGLEAGT"
3046         copy2archive $test_file $tdir/$tfile
3047
3048         mkdir -p $DIR/$tdir
3049         local f=$DIR/$tdir/$tfile
3050         import_file $tdir/$tfile $f
3051         f=$DIR2/$tdir/$tfile
3052         echo "Verifying released state: "
3053         check_hsm_flags $f "0x0000000d"
3054
3055         local fid=$(path2fid $f)
3056         $LFS hsm_restore $f
3057         wait_request_state $fid RESTORE SUCCEED
3058
3059         local expected_fields="event_time data_fid source_fid"
3060         expected_fields+=" total_bytes current_bytes"
3061
3062         local START_EVENT
3063         local FINISH_EVENT
3064         while read event; do
3065                 # Make sure we're not getting anything from previous events.
3066                 for field in $expected_fields; do
3067                         unset $field
3068                 done
3069
3070                 local parsed=$(parse_json_event "$event")
3071                 if [ -z "$parsed" ]; then
3072                         error "Copytool sent malformed event: $event"
3073                 fi
3074                 eval $parsed
3075
3076                 if [ $event_type == "RESTORE_START" ]; then
3077                         START_EVENT=$event
3078                         if [ $source_fid != $data_fid ]; then
3079                                 error "source_fid should == data_fid at start"
3080                         fi
3081                         continue
3082                 elif [ $event_type == "RESTORE_FINISH" ]; then
3083                         FINISH_EVENT=$event
3084                         if [ $source_fid != $data_fid ]; then
3085                                 error "source_fid should == data_fid at finish"
3086                         fi
3087                         continue
3088                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3089                         continue
3090                 fi
3091
3092                 # Do some simple checking of the progress update events.
3093                 for expected_field in $expected_fields; do
3094                         if [ -z ${!expected_field+x} ]; then
3095                                 error "Missing $expected_field field in event"
3096                         fi
3097                 done
3098
3099                 if [ $total_bytes -eq 0 ]; then
3100                         error "Expected total_bytes to be > 0"
3101                 fi
3102
3103                 # When a restore starts out, the data fid is the same as the
3104                 # source fid. After the restore has gotten going, we learn
3105                 # the new data fid. Once the restore has finished, the source
3106                 # fid is set to the new data fid.
3107                 #
3108                 # We test this because some monitoring software may depend on
3109                 # this behavior. If it changes, then the consumers of these
3110                 # events may need to be modified.
3111                 if [ $source_fid == $data_fid ]; then
3112                         error "source_fid should != data_fid during restore"
3113                 fi
3114         done < <(echo $"$(get_copytool_event_log)")
3115
3116         if [ -z "$START_EVENT" ]; then
3117                 error "Copytool failed to send restore start event to FIFO"
3118         fi
3119
3120         if [ -z "$FINISH_EVENT" ]; then
3121                 error "Copytool failed to send restore finish event to FIFO"
3122         fi
3123
3124         echo "Restore events look OK."
3125
3126         cdt_clear_no_retry
3127         copytool_cleanup
3128         copytool_monitor_cleanup
3129
3130         rm -rf $test_dir
3131 }
3132 run_test 72 "Copytool logs JSON restore events to FIFO"
3133
3134 test_90() {
3135         file_count=51 # Max number of files constrained by LNET message size
3136         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3137         local f=$DIR/$tdir/$tfile
3138         local FILELIST=/tmp/filelist.txt
3139         local i=""
3140
3141         rm -f $FILELIST
3142         for i in $(seq 1 $file_count); do
3143                 fid=$(copy_file /etc/hosts $f.$i)
3144                 echo $f.$i >> $FILELIST
3145         done
3146         # force copytool to use a local/temp archive dir to ensure best
3147         # performance vs remote/NFS mounts used in auto-tests
3148         if do_facet $SINGLEAGT "df --local $HSM_ARCHIVE" >/dev/null 2>&1 ; then
3149                 copytool_setup
3150         else
3151                 local dai=$(get_hsm_param default_archive_id)
3152                 copytool_setup $SINGLEAGT $MOUNT $dai $TMP/$tdir
3153         fi
3154         # to be sure wait_all_done will not be mislead by previous tests
3155         cdt_purge
3156         wait_for_grace_delay
3157         $LFS hsm_archive --filelist $FILELIST ||
3158                 error "cannot archive a file list"
3159         wait_all_done 100
3160         $LFS hsm_release --filelist $FILELIST ||
3161                 error "cannot release a file list"
3162         $LFS hsm_restore --filelist $FILELIST ||
3163                 error "cannot restore a file list"
3164         wait_all_done 100
3165         copytool_cleanup
3166 }
3167 run_test 90 "Archive/restore a file list"
3168
3169 double_verify_reset_hsm_param() {
3170         local p=$1
3171         echo "Testing $HSM_PARAM.$p"
3172         local val=$(get_hsm_param $p)
3173         local save=$val
3174         local val2=$(($val * 2))
3175         set_hsm_param $p $val2
3176         val=$(get_hsm_param $p)
3177         [[ $val == $val2 ]] ||
3178                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3179         echo "Set $p to 0 must failed"
3180         set_hsm_param $p 0
3181         local rc=$?
3182         # restore value
3183         set_hsm_param $p $save
3184
3185         if [[ $rc == 0 ]]; then
3186                 error "we must not be able to set $HSM_PARAM.$p to 0"
3187         fi
3188 }
3189
3190 test_100() {
3191         double_verify_reset_hsm_param loop_period
3192         double_verify_reset_hsm_param grace_delay
3193         double_verify_reset_hsm_param active_request_timeout
3194         double_verify_reset_hsm_param max_requests
3195         double_verify_reset_hsm_param default_archive_id
3196 }
3197 run_test 100 "Set coordinator /proc tunables"
3198
3199 test_102() {
3200         cdt_disable
3201         cdt_enable
3202         cdt_restart
3203 }
3204 run_test 102 "Verify coordinator control"
3205
3206 test_103() {
3207         # test needs a running copytool
3208         copytool_setup
3209
3210         local i=""
3211         local fid=""
3212
3213         mkdir -p $DIR/$tdir
3214         for i in $(seq 1 20); do
3215                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3216         done
3217         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3218
3219         cdt_purge
3220
3221         echo "Current requests"
3222         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3223                         $HSM_PARAM.actions |\
3224                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3225
3226         [[ -z "$res" ]] || error "Some request have not been canceled"
3227
3228         copytool_cleanup
3229 }
3230 run_test 103 "Purge all requests"
3231
3232 DATA=CEA
3233 DATAHEX='[434541]'
3234 test_104() {
3235         # test needs a running copytool
3236         copytool_setup
3237
3238         mkdir -p $DIR/$tdir
3239         local f=$DIR/$tdir/$tfile
3240         local fid
3241         fid=$(make_large_for_progress $f)
3242         [ $? != 0 ] && skip "not enough free space" && return
3243
3244         # if cdt is on, it can serve too quickly the request
3245         cdt_disable
3246         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3247         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3248                         $HSM_PARAM.actions |\
3249                         grep $fid | cut -f16 -d=")
3250         cdt_enable
3251
3252         [[ "$data1" == "$DATAHEX" ]] ||
3253                 error "Data field in records is ($data1) and not ($DATAHEX)"
3254
3255         copytool_cleanup
3256 }
3257 run_test 104 "Copy tool data field"
3258
3259 test_105() {
3260         mkdir -p $DIR/$tdir
3261         local i=""
3262
3263         cdt_disable
3264         for i in $(seq -w 1 10); do
3265                 cp /etc/passwd $DIR/$tdir/$i
3266                 $LFS hsm_archive $DIR/$tdir/$i
3267         done
3268         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3269                         $HSM_PARAM.actions |\
3270                         grep WAITING | wc -l")
3271         cdt_restart
3272         cdt_disable
3273         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3274                         $HSM_PARAM.actions |\
3275                         grep WAITING | wc -l")
3276         cdt_enable
3277         cdt_purge
3278         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3279                 error "Requests count after shutdown $reqcnt2 != "\
3280                       "before shutdown $reqcnt1"
3281 }
3282 run_test 105 "Restart of coordinator"
3283
3284 get_agent_by_uuid_mdt() {
3285         local uuid=$1
3286         local mdtidx=$2
3287         local mds=mds$(($mdtidx + 1))
3288         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
3289                  grep $uuid"
3290 }
3291
3292 check_agent_registered_by_mdt() {
3293         local uuid=$1
3294         local mdtidx=$2
3295         local mds=mds$(($mdtidx + 1))
3296         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
3297         if [[ ! -z "$agent" ]]; then
3298                 echo "found agent $agent on $mds"
3299         else
3300                 error "uuid $uuid not found in agent list on $mds"
3301         fi
3302 }
3303
3304 check_agent_unregistered_by_mdt() {
3305         local uuid=$1
3306         local mdtidx=$2
3307         local mds=mds$(($mdtidx + 1))
3308         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
3309         if [[ -z "$agent" ]]; then
3310                 echo "uuid not found in agent list on $mds"
3311         else
3312                 error "uuid found in agent list on $mds: $agent"
3313         fi
3314 }
3315
3316 check_agent_registered() {
3317         local uuid=$1
3318         local mdsno
3319         for mdsno in $(seq 1 $MDSCOUNT); do
3320                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
3321         done
3322 }
3323
3324 check_agent_unregistered() {
3325         local uuid=$1
3326         local mdsno
3327         for mdsno in $(seq 1 $MDSCOUNT); do
3328                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
3329         done
3330 }
3331
3332 test_106() {
3333         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
3334                 get_client_uuid $MOUNT | cut -d' ' -f2)
3335
3336         copytool_setup
3337         check_agent_registered $uuid
3338
3339         search_copytools || error "No copytool found"
3340
3341         copytool_cleanup
3342         check_agent_unregistered $uuid
3343
3344         copytool_setup
3345         check_agent_registered $uuid
3346
3347         copytool_cleanup
3348 }
3349 run_test 106 "Copytool register/unregister"
3350
3351 test_107() {
3352         # test needs a running copytool
3353         copytool_setup
3354         # create and archive file
3355         mkdir -p $DIR/$tdir
3356         local f1=$DIR/$tdir/$tfile
3357         local fid=$(copy_file /etc/passwd $f1)
3358         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3359         wait_request_state $fid ARCHIVE SUCCEED
3360         # shutdown and restart MDS
3361         fail $SINGLEMDS
3362         # check the copytool still gets messages from MDT
3363         local f2=$DIR/$tdir/2
3364         local fid=$(copy_file /etc/passwd $f2)
3365         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3366         # main check of this sanity: this request MUST succeed
3367         wait_request_state $fid ARCHIVE SUCCEED
3368         copytool_cleanup
3369 }
3370 run_test 107 "Copytool re-register after MDS restart"
3371
3372 policy_set_and_test()
3373 {
3374         local change="$1"
3375         local target="$2"
3376         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3377         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3378         [[ "$policy" == "$target" ]] ||
3379                 error "Wrong policy after '$change': '$policy' != '$target'"
3380 }
3381
3382 test_109() {
3383         # to force default policy setting if error
3384         CDT_POLICY_HAD_CHANGED=true
3385
3386         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3387         local default="NonBlockingRestore [NoRetryAction]"
3388         [[ "$policy" == "$default" ]] ||
3389                 error "default policy has changed,"\
3390                       " '$policy' != '$default' update the test"
3391         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3392         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3393         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3394         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3395         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
3396         # useless bacause we know but safer for futur changes to use real value
3397         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3398         echo "Next set_param must failed"
3399         policy_set_and_test "wrong" "$policy"
3400
3401         # return to default
3402         echo "Back to default policy"
3403         cdt_set_sanity_policy
3404 }
3405 run_test 109 "Policy display/change"
3406
3407 test_110a() {
3408         # test needs a running copytool
3409         copytool_setup
3410
3411         mkdir -p $DIR/$tdir
3412
3413         copy2archive /etc/passwd $tdir/$tfile
3414
3415         local f=$DIR/$tdir/$tfile
3416         import_file $tdir/$tfile $f
3417         local fid=$(path2fid $f)
3418
3419         cdt_set_non_blocking_restore
3420         md5sum $f
3421         local st=$?
3422
3423         # cleanup
3424         wait_request_state $fid RESTORE SUCCEED
3425         cdt_clear_non_blocking_restore
3426
3427         # Test result
3428         [[ $st == 1 ]] ||
3429                 error "md5sum returns $st != 1, "\
3430                         "should also perror ENODATA (No data available)"
3431
3432         copytool_cleanup
3433 }
3434 run_test 110a "Non blocking restore policy (import case)"
3435
3436 test_110b() {
3437         # test needs a running copytool
3438         copytool_setup
3439
3440         mkdir -p $DIR/$tdir
3441         local f=$DIR/$tdir/$tfile
3442         local fid=$(copy_file /etc/passwd $f)
3443         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3444         wait_request_state $fid ARCHIVE SUCCEED
3445         $LFS hsm_release $f
3446
3447         cdt_set_non_blocking_restore
3448         md5sum $f
3449         local st=$?
3450
3451         # cleanup
3452         wait_request_state $fid RESTORE SUCCEED
3453         cdt_clear_non_blocking_restore
3454
3455         # Test result
3456         [[ $st == 1 ]] ||
3457                 error "md5sum returns $st != 1, "\
3458                         "should also perror ENODATA (No data available)"
3459
3460         copytool_cleanup
3461 }
3462 run_test 110b "Non blocking restore policy (release case)"
3463
3464 test_111a() {
3465         # test needs a running copytool
3466         copytool_setup
3467
3468         mkdir -p $DIR/$tdir
3469         copy2archive /etc/passwd $tdir/$tfile
3470
3471         local f=$DIR/$tdir/$tfile
3472
3473         import_file $tdir/$tfile $f
3474         local fid=$(path2fid $f)
3475
3476         cdt_set_no_retry
3477
3478         copytool_remove_backend $fid
3479
3480         $LFS hsm_restore $f
3481         wait_request_state $fid RESTORE FAILED
3482         local st=$?
3483
3484         # cleanup
3485         cdt_clear_no_retry
3486
3487         # Test result
3488         [[ $st == 0 ]] || error "Restore does not failed"
3489
3490         copytool_cleanup
3491 }
3492 run_test 111a "No retry policy (import case), restore will error"\
3493               " (No such file or directory)"
3494
3495 test_111b() {
3496         # test needs a running copytool
3497         copytool_setup
3498
3499         mkdir -p $DIR/$tdir
3500         local f=$DIR/$tdir/$tfile
3501         local fid=$(copy_file /etc/passwd $f)
3502         cdt_set_no_retry
3503         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3504         wait_request_state $fid ARCHIVE SUCCEED
3505         $LFS hsm_release $f
3506
3507         copytool_remove_backend $fid
3508
3509         $LFS hsm_restore $f
3510         wait_request_state $fid RESTORE FAILED
3511         local st=$?
3512
3513         # cleanup
3514         cdt_clear_no_retry
3515
3516         # Test result
3517         [[ $st == 0 ]] || error "Restore does not failed"
3518
3519         copytool_cleanup
3520 }
3521 run_test 111b "No retry policy (release case), restore will error"\
3522               " (No such file or directory)"
3523
3524 test_112() {
3525         # test needs a running copytool
3526         copytool_setup
3527
3528         mkdir -p $DIR/$tdir
3529         local f=$DIR/$tdir/$tfile
3530         local fid=$(copy_file /etc/passwd $f)
3531         cdt_disable
3532         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3533         local l=$($LFS hsm_action $f)
3534         echo $l
3535         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
3536
3537         # cleanup
3538         cdt_enable
3539         wait_request_state $fid ARCHIVE SUCCEED
3540
3541         # Test result
3542         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
3543
3544         copytool_cleanup
3545 }
3546 run_test 112 "State of recorded request"
3547
3548 test_200() {
3549         # test needs a running copytool
3550         copytool_setup
3551
3552         mkdir -p $DIR/$tdir
3553         local f=$DIR/$tdir/$tfile
3554         local fid
3555         fid=$(make_large_for_cancel $f)
3556         [ $? != 0 ] && skip "not enough free space" && return
3557
3558         # test with cdt on is made in test_221
3559         cdt_disable
3560         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3561         $LFS hsm_cancel $f
3562         cdt_enable
3563         wait_request_state $fid ARCHIVE CANCELED
3564         wait_request_state $fid CANCEL SUCCEED
3565
3566         copytool_cleanup
3567 }
3568 run_test 200 "Register/Cancel archive"
3569
3570 test_201() {
3571         # test needs a running copytool
3572         copytool_setup
3573
3574         mkdir -p $DIR/$tdir
3575         local f=$DIR/$tdir/$tfile
3576         make_archive $tdir/$tfile
3577         import_file $tdir/$tfile $f
3578         local fid=$(path2fid $f)
3579
3580         # test with cdt on is made in test_222
3581         cdt_disable
3582         $LFS hsm_restore $f
3583         $LFS hsm_cancel $f
3584         cdt_enable
3585         wait_request_state $fid RESTORE CANCELED
3586         wait_request_state $fid CANCEL SUCCEED
3587
3588         copytool_cleanup
3589 }
3590 run_test 201 "Register/Cancel restore"
3591
3592 test_202() {
3593         # test needs a running copytool
3594         copytool_setup
3595
3596         mkdir -p $DIR/$tdir
3597         local f=$DIR/$tdir/$tfile
3598         local fid
3599         fid=$(make_large_for_progress $f)
3600         [ $? != 0 ] && skip "not enough free space" && return
3601
3602         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3603         wait_request_state $fid ARCHIVE SUCCEED
3604
3605         cdt_disable
3606         $LFS hsm_remove $f
3607         $LFS hsm_cancel $f
3608         cdt_enable
3609         wait_request_state $fid REMOVE CANCELED
3610
3611         copytool_cleanup
3612 }
3613 run_test 202 "Register/Cancel remove"
3614
3615 test_220() {
3616         # test needs a running copytool
3617         copytool_setup
3618
3619         mkdir -p $DIR/$tdir
3620
3621         local f=$DIR/$tdir/$tfile
3622         local fid=$(copy_file /etc/passwd $f)
3623
3624         changelog_setup
3625
3626         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3627         wait_request_state $fid ARCHIVE SUCCEED
3628
3629         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3630         changelog_cleanup
3631
3632         local target=0x0
3633         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3634
3635         copytool_cleanup
3636 }
3637 run_test 220 "Changelog for archive"
3638
3639 test_221() {
3640         # test needs a running copytool
3641         copytool_setup
3642
3643         mkdir -p $DIR/$tdir
3644
3645         local f=$DIR/$tdir/$tfile
3646         local fid
3647         fid=$(make_large_for_cancel $f)
3648         [ $? != 0 ] && skip "not enough free space" && return
3649
3650         changelog_setup
3651
3652         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3653         wait_request_state $fid ARCHIVE STARTED
3654         $LFS hsm_cancel $f
3655         wait_request_state $fid ARCHIVE CANCELED
3656         wait_request_state $fid CANCEL SUCCEED
3657
3658         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3659
3660         local target=0x7d
3661         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3662
3663         cleanup
3664 }
3665 run_test 221 "Changelog for archive canceled"
3666
3667 test_222a() {
3668         # test needs a running copytool
3669         copytool_setup
3670
3671         mkdir -p $DIR/$tdir
3672         copy2archive /etc/passwd $tdir/$tfile
3673
3674         local f=$DIR/$tdir/$tfile
3675         import_file $tdir/$tfile $f
3676         local fid=$(path2fid $f)
3677
3678         changelog_setup
3679
3680         $LFS hsm_restore $f
3681         wait_request_state $fid RESTORE SUCCEED
3682
3683         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3684
3685         local target=0x80
3686         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3687
3688         cleanup
3689 }
3690 run_test 222a "Changelog for explicit restore"
3691
3692 test_222b() {
3693         # test needs a running copytool
3694         copytool_setup
3695
3696         mkdir -p $DIR/$tdir
3697         local f=$DIR/$tdir/$tfile
3698         local fid=$(copy_file /etc/passwd $f)
3699
3700         changelog_setup
3701         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3702         wait_request_state $fid ARCHIVE SUCCEED
3703         $LFS hsm_release $f
3704
3705         md5sum $f
3706
3707         wait_request_state $fid RESTORE SUCCEED
3708
3709         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3710
3711         local target=0x80
3712         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3713
3714         cleanup
3715 }
3716 run_test 222b "Changelog for implicit restore"
3717
3718 test_223a() {
3719         # test needs a running copytool
3720         copytool_setup
3721
3722         mkdir -p $DIR/$tdir
3723
3724         local f=$DIR/$tdir/$tfile
3725         make_archive $tdir/$tfile
3726
3727         changelog_setup
3728
3729         import_file $tdir/$tfile $f
3730         local fid=$(path2fid $f)
3731
3732         $LFS hsm_restore $f
3733         wait_request_state $fid RESTORE STARTED
3734         $LFS hsm_cancel $f
3735         wait_request_state $fid RESTORE CANCELED
3736         wait_request_state $fid CANCEL SUCCEED
3737
3738         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3739
3740         local target=0xfd
3741         [[ $flags == $target ]] ||
3742                 error "Changelog flag is $flags not $target"
3743
3744         cleanup
3745 }
3746 run_test 223a "Changelog for restore canceled (import case)"
3747
3748 test_223b() {
3749         # test needs a running copytool
3750         copytool_setup
3751
3752         mkdir -p $DIR/$tdir
3753
3754         local f=$DIR/$tdir/$tfile
3755         local fid
3756         fid=$(make_large_for_progress $f)
3757         [ $? != 0 ] && skip "not enough free space" && return
3758
3759         changelog_setup
3760         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3761         wait_request_state $fid ARCHIVE SUCCEED
3762         $LFS hsm_release $f
3763         $LFS hsm_restore $f
3764         wait_request_state $fid RESTORE STARTED
3765         $LFS hsm_cancel $f
3766         wait_request_state $fid RESTORE CANCELED
3767         wait_request_state $fid CANCEL SUCCEED
3768
3769         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3770
3771         local target=0xfd
3772         [[ $flags == $target ]] ||
3773                 error "Changelog flag is $flags not $target"
3774
3775         cleanup
3776 }
3777 run_test 223b "Changelog for restore canceled (release case)"
3778
3779 test_224() {
3780         # test needs a running copytool
3781         copytool_setup
3782
3783         mkdir -p $DIR/$tdir
3784
3785         local f=$DIR/$tdir/$tfile
3786         local fid=$(copy_file /etc/passwd $f)
3787
3788         changelog_setup
3789         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3790         wait_request_state $fid ARCHIVE SUCCEED
3791
3792         $LFS hsm_remove $f
3793         wait_request_state $fid REMOVE SUCCEED
3794
3795         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
3796
3797         local target=0x200
3798         [[ $flags == $target ]] ||
3799                 error "Changelog flag is $flags not $target"
3800
3801         cleanup
3802 }
3803 run_test 224 "Changelog for remove"
3804
3805 test_225() {
3806         # test needs a running copytool
3807         copytool_setup
3808
3809         # test is not usable because remove request is too fast
3810         # so it is always finished before cancel can be done ...
3811         echo "Test disabled"
3812         copytool_cleanup
3813         return 0
3814
3815         mkdir -p $DIR/$tdir
3816         local f=$DIR/$tdir/$tfile
3817         local fid
3818         fid=$(make_large_for_progress $f)
3819         [ $? != 0 ] && skip "not enough free space" && return
3820
3821         changelog_setup
3822         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3823         wait_request_state $fid ARCHIVE SUCCEED
3824
3825         # if cdt is on, it can serve too quickly the request
3826         cdt_disable
3827         $LFS hsm_remove $f
3828         $LFS hsm_cancel $f
3829         cdt_enable
3830         wait_request_state $fid REMOVE CANCELED
3831         wait_request_state $fid CANCEL SUCCEED
3832
3833         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3834         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
3835                 tail -n 1 | awk '{print $5}')
3836
3837         local target=0x27d
3838         [[ $flags == $target ]] ||
3839                 error "Changelog flag is $flags not $target"
3840
3841         cleanup
3842 }
3843 run_test 225 "Changelog for remove canceled"
3844
3845 test_226() {
3846         # test needs a running copytool
3847         copytool_setup
3848
3849         mkdir -p $DIR/$tdir
3850
3851         local f1=$DIR/$tdir/$tfile-1
3852         local f2=$DIR/$tdir/$tfile-2
3853         local f3=$DIR/$tdir/$tfile-3
3854         local fid1=$(copy_file /etc/passwd $f1)
3855         local fid2=$(copy_file /etc/passwd $f2)
3856         copy_file /etc/passwd $f3
3857
3858         changelog_setup
3859         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3860         wait_request_state $fid1 ARCHIVE SUCCEED
3861
3862         $LFS hsm_archive $f2
3863         wait_request_state $fid2 ARCHIVE SUCCEED
3864
3865         rm $f1 || error "rm $f1 failed"
3866
3867         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
3868
3869         local target=0x3
3870         [[ $flags == $target ]] ||
3871                 error "Changelog flag is $flags not $target"
3872
3873         mv $f3 $f2 || error "mv $f3 $f2 failed"
3874
3875         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3876
3877         target=0x3
3878         [[ $flags == $target ]] ||
3879                 error "Changelog flag is $flags not $target"
3880
3881         cleanup
3882 }
3883 run_test 226 "changelog for last rm/mv with exiting archive"
3884
3885 check_flags_changes() {
3886         local f=$1
3887         local fid=$2
3888         local hsm_flag=$3
3889         local fst=$4
3890         local cnt=$5
3891
3892         local target=0x280
3893         $LFS hsm_set --$hsm_flag $f ||
3894                 error "Cannot set $hsm_flag on $f"
3895         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3896         local seen=${#flags[*]}
3897         cnt=$((fst + cnt))
3898         [[ $seen == $cnt ]] ||
3899                 error "set $hsm_flag: Changelog events $seen != $cnt"
3900         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3901                 error "set $hsm_flag: Changelog flags are "\
3902                         "${flags[$((cnt - 1))]} not $target"
3903
3904         $LFS hsm_clear --$hsm_flag $f ||
3905                 error "Cannot clear $hsm_flag on $f"
3906         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3907         seen=${#flags[*]}
3908         cnt=$(($cnt + 1))
3909         [[ $cnt == $seen ]] ||
3910                 error "clear $hsm_flag: Changelog events $seen != $cnt"
3911
3912         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3913                 error "clear $hsm_flag: Changelog flag is "\
3914                         "${flags[$((cnt - 1))]} not $target"
3915 }
3916
3917 test_227() {
3918         # test needs a running copytool
3919         copytool_setup
3920         changelog_setup
3921
3922         mkdir -p $DIR/$tdir
3923         typeset -a flags
3924
3925         for i in norelease noarchive exists archived
3926         do
3927                 local f=$DIR/$tdir/$tfile-$i
3928                 local fid=$(copy_file /etc/passwd $f)
3929                 check_flags_changes $f $fid $i 0 1
3930         done
3931
3932         f=$DIR/$tdir/$tfile---lost
3933         fid=$(copy_file /etc/passwd $f)
3934         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3935         wait_request_state $fid ARCHIVE SUCCEED
3936         check_flags_changes $f $fid lost 3 1
3937
3938         cleanup
3939 }
3940 run_test 227 "changelog when explicit setting of HSM flags"
3941
3942 test_228() {
3943         # test needs a running copytool
3944         copytool_setup
3945
3946         local fid=$(make_small_sync $DIR/$tfile)
3947         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
3948         wait_request_state $fid ARCHIVE SUCCEED
3949
3950         $LFS hsm_release $DIR/$tfile
3951         check_hsm_flags $DIR/$tfile "0x0000000d"
3952
3953         filefrag $DIR/$tfile | grep " 1 extent found" ||
3954                 error "filefrag on released file must return only one extent"
3955
3956         # only newer versions of cp detect sparse files by stat/FIEMAP
3957         # (LU-2580)
3958         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
3959                 error "copying $DIR/$tfile"
3960         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
3961
3962         $LFS hsm_release $DIR/$tfile
3963         check_hsm_flags $DIR/$tfile "0x0000000d"
3964
3965         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
3966
3967         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
3968                 error "tar failed"
3969         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
3970                 error "comparing untarred $DIR/$tfile"
3971
3972         rm -f $DIR/$tfile $DIR/$tfile.2 ||
3973                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
3974         copytool_cleanup
3975 }
3976 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
3977
3978 test_250() {
3979         # test needs a running copytool
3980         copytool_setup
3981
3982         mkdir -p $DIR/$tdir
3983         local maxrequest=$(get_hsm_param max_requests)
3984         local rqcnt=$(($maxrequest * 3))
3985         local i=""
3986
3987         cdt_disable
3988         for i in $(seq -w 1 $rqcnt); do
3989                 rm -f $DIR/$tdir/$i
3990                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
3991         done
3992         # we do it in 2 steps, so all requests arrive at the same time
3993         for i in $(seq -w 1 $rqcnt); do
3994                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3995         done
3996         cdt_enable
3997         local cnt=$rqcnt
3998         local wt=$rqcnt
3999         while [[ $cnt != 0 || $wt != 0 ]]; do
4000                 sleep 1
4001                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
4002                         $HSM_PARAM.actions |\
4003                         grep STARTED | grep -v CANCEL | wc -l")
4004                 [[ $cnt -le $maxrequest ]] ||
4005                         error "$cnt > $maxrequest too many started requests"
4006                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
4007                         $HSM_PARAM.actions |\
4008                         grep WAITING | wc -l")
4009                 echo "max=$maxrequest started=$cnt waiting=$wt"
4010         done
4011
4012         copytool_cleanup
4013 }
4014 run_test 250 "Coordinator max request"
4015
4016 test_251() {
4017         # test needs a running copytool
4018         copytool_setup
4019
4020         mkdir -p $DIR/$tdir
4021         local f=$DIR/$tdir/$tfile
4022         local fid
4023         fid=$(make_large_for_cancel $f)
4024         [ $? != 0 ] && skip "not enough free space" && return
4025
4026         cdt_disable
4027         # to have a short test
4028         local old_to=$(get_hsm_param active_request_timeout)
4029         set_hsm_param active_request_timeout 4
4030         # to be sure the cdt will wake up frequently so
4031         # it will be able to cancel the "old" request
4032         local old_loop=$(get_hsm_param loop_period)
4033         set_hsm_param loop_period 2
4034         cdt_enable
4035
4036         # clear locks to avoid extra delay caused by flush/cancel
4037         # and thus prevent early copytool death to timeout.
4038         cancel_lru_locks osc
4039
4040         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4041         wait_request_state $fid ARCHIVE STARTED
4042         sleep 5
4043         wait_request_state $fid ARCHIVE CANCELED
4044
4045         set_hsm_param active_request_timeout $old_to
4046         set_hsm_param loop_period $old_loop
4047
4048         copytool_cleanup
4049 }
4050 run_test 251 "Coordinator request timeout"
4051
4052 test_300() {
4053         # the only way to test ondisk conf is to restart MDS ...
4054         echo "Stop coordinator and remove coordinator state at mount"
4055         # stop coordinator
4056         cdt_shutdown
4057         # clean on disk conf set by default
4058         cdt_clear_mount_state
4059         cdt_check_state stopped
4060
4061         # check cdt still off after umount/remount
4062         fail $SINGLEMDS
4063         cdt_check_state stopped
4064
4065         echo "Set coordinator start at mount, and start coordinator"
4066         cdt_set_mount_state enabled
4067
4068         # check cdt is on
4069         cdt_check_state enabled
4070
4071         # check cdt still on after umount/remount
4072         fail $SINGLEMDS
4073         cdt_check_state enabled
4074
4075         # we are back to original state (cdt started at mount)
4076 }
4077 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4078
4079 test_301() {
4080         local ai=$(get_hsm_param default_archive_id)
4081         local new=$((ai + 1))
4082
4083         set_hsm_param default_archive_id $new -P
4084         fail $SINGLEMDS
4085         local res=$(get_hsm_param default_archive_id)
4086
4087         # clear value
4088         set_hsm_param default_archive_id "" "-P -d"
4089
4090         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4091 }
4092 run_test 301 "HSM tunnable are persistent"
4093
4094 test_302() {
4095         local ai=$(get_hsm_param default_archive_id)
4096         local new=$((ai + 1))
4097
4098         # stop coordinator
4099         cdt_shutdown
4100
4101         set_hsm_param default_archive_id $new -P
4102
4103         local mdtno
4104         for mdtno in $(seq 1 $MDSCOUNT); do
4105                 fail mds${mdtno}
4106         done
4107
4108         # check cdt is on
4109         cdt_check_state enabled
4110
4111         local res=$(get_hsm_param default_archive_id)
4112
4113         # clear value
4114         set_hsm_param default_archive_id "" "-P -d"
4115
4116         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4117 }
4118 run_test 302 "HSM tunnable are persistent when CDT is off"
4119
4120 test_400() {
4121         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4122
4123         copytool_setup
4124
4125         mkdir -p $DIR/$tdir
4126
4127         local dir_mdt0=$DIR/$tdir/mdt0
4128         local dir_mdt1=$DIR/$tdir/mdt1
4129
4130         # create 1 dir per MDT
4131         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4132         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4133
4134         # create 1 file in each MDT
4135         local fid1=$(make_small $dir_mdt0/$tfile)
4136         local fid2=$(make_small $dir_mdt1/$tfile)
4137
4138         # check that hsm request on mdt0 is sent to the right MDS
4139         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4140         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4141                 echo "archive successful on mdt0"
4142
4143         # check that hsm request on mdt1 is sent to the right MDS
4144         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
4145         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4146                 echo "archive successful on mdt1"
4147
4148         copytool_cleanup
4149         # clean test files and directories
4150         rm -rf $dir_mdt0 $dir_mdt1
4151 }
4152 run_test 400 "Single request is sent to the right MDT"
4153
4154 test_401() {
4155         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4156
4157         copytool_setup
4158
4159         mkdir -p $DIR/$tdir
4160
4161         local dir_mdt0=$DIR/$tdir/mdt0
4162         local dir_mdt1=$DIR/$tdir/mdt1
4163
4164         # create 1 dir per MDT
4165         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4166         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4167
4168         # create 1 file in each MDT
4169         local fid1=$(make_small $dir_mdt0/$tfile)
4170         local fid2=$(make_small $dir_mdt1/$tfile)
4171
4172         # check that compound requests are shunt to the rights MDTs
4173         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
4174                 error "lfs hsm_archive"
4175         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4176                 echo "archive successful on mdt0"
4177         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4178                 echo "archive successful on mdt1"
4179
4180         copytool_cleanup
4181         # clean test files and directories
4182         rm -rf $dir_mdt0 $dir_mdt1
4183 }
4184 run_test 401 "Compound requests split and sent to their respective MDTs"
4185
4186 mdc_change_state() # facet, MDT_pattern, activate|deactivate
4187 {
4188         local facet=$1
4189         local pattern="$2"
4190         local state=$3
4191         local node=$(facet_active_host $facet)
4192         local mdc
4193         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
4194                         awk '{print $4}'); do
4195                 echo "$3 $mdc on $node"
4196                 do_facet $facet "$LCTL --device $mdc $state" || return 1
4197         done
4198 }
4199
4200 test_402() {
4201         # make sure there is no running copytool
4202         copytool_cleanup
4203
4204         # deactivate all mdc on agent1
4205         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
4206
4207         HSMTOOL_NOERROR=true copytool_setup $SINGLEAGT
4208
4209         check_agent_unregistered "uuid" # match any agent
4210
4211         # no expected running copytool
4212         search_copytools $agent && error "Copytool start should have failed"
4213
4214         # reactivate MDCs
4215         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
4216 }
4217 run_test 402 "Copytool start fails if all MDTs are inactive"
4218
4219 test_403() {
4220         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4221
4222         # make sure there is no running copytool
4223         copytool_cleanup
4224
4225         local agent=$(facet_active_host $SINGLEAGT)
4226         local uuid=$(do_rpc_nodes $agent get_client_uuid | cut -d' ' -f2)
4227
4228         # deactivate all mdc for MDT0001
4229         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4230
4231         copytool_setup
4232         # check the agent is registered on MDT0000, and not on MDT0001
4233         check_agent_registered_by_mdt $uuid 0
4234         check_agent_unregistered_by_mdt $uuid 1
4235
4236         # check running copytool process
4237         search_copytools $agent || error "No running copytools on $agent"
4238
4239         # reactivate all mdc for MDT0001
4240         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4241
4242         # make sure the copytool is now registered to all MDTs
4243         check_agent_registered $uuid
4244
4245         copytool_cleanup
4246 }
4247 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
4248
4249 test_404() {
4250         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4251
4252         copytool_setup
4253
4254         # create files on both MDT0000 and MDT0001
4255         mkdir -p $DIR/$tdir
4256
4257         local dir_mdt0=$DIR/$tdir/mdt0
4258         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4259
4260         # create 1 file on mdt0
4261         local fid1=$(make_small $dir_mdt0/$tfile)
4262
4263         # deactivate all mdc for MDT0001
4264         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4265
4266         # send an HSM request for files in MDT0000
4267         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4268
4269         # check for completion of files in MDT0000
4270         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4271                 echo "archive successful on mdt0"
4272
4273         # reactivate all mdc for MDT0001
4274         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4275
4276         copytool_cleanup
4277         # clean test files and directories
4278         rm -rf $dir_mdt0
4279 }
4280 run_test 404 "Inactive MDT does not block requests for active MDTs"
4281
4282 test_405() {
4283         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4284
4285         copytool_setup
4286
4287         mkdir -p $DIR/$tdir
4288
4289         local striped_dir=$DIR/$tdir/striped_dir
4290
4291         # create striped dir on all of MDTs
4292         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
4293
4294         local fid1=$(make_small_sync $striped_dir/${tfile}_0)
4295         local fid2=$(make_small_sync $striped_dir/${tfile}_1)
4296         local fid3=$(make_small_sync $striped_dir/${tfile}_2)
4297         local fid4=$(make_small_sync $striped_dir/${tfile}_3)
4298
4299         local idx1=$($LFS getstripe -M $striped_dir/${tfile}_0)
4300         local idx2=$($LFS getstripe -M $striped_dir/${tfile}_1)
4301         local idx3=$($LFS getstripe -M $striped_dir/${tfile}_2)
4302         local idx4=$($LFS getstripe -M $striped_dir/${tfile}_3)
4303
4304         # check that compound requests are shunt to the rights MDTs
4305         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
4306                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
4307                 error "lfs hsm_archive"
4308
4309         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
4310                 echo "archive successful on $fid1"
4311         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
4312                 echo "archive successful on $fid2"
4313         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
4314                 echo "archive successful on $fid3"
4315         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
4316                 echo "archive successful on $fid4"
4317
4318         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
4319         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
4320         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
4321         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
4322
4323         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
4324         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
4325         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
4326         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
4327
4328         copytool_cleanup
4329 }
4330 run_test 405 "archive and release under striped directory"
4331
4332 test_500()
4333 {
4334         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
4335                 skip "HSM migrate is not supported" && return
4336
4337         # Stop the existing copytool
4338         copytool_cleanup
4339
4340         test_mkdir -p $DIR/$tdir
4341         llapi_hsm_test -d $DIR/$tdir || error "One llapi HSM test failed"
4342 }
4343 run_test 500 "various LLAPI HSM tests"
4344
4345 copytool_cleanup
4346
4347 complete $SECONDS
4348 check_and_cleanup_lustre
4349 exit_status