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