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