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