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