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