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