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