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