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