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