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