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