Whamcloud - gitweb
233db87a509c512e536495158410e4df68700347
[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         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1347         # used until file is flushed to disk
1348         if [  $(facet_fstype ost1) == "zfs" ]; then
1349             # this causes an OST_SYNC rpc to be sent
1350             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1351             # clear locks to reread file data
1352             cancel_lru_locks osc
1353         fi
1354
1355         local orig_size=$(stat -c "%s" $f)
1356         local orig_blocks=$(stat -c "%b" $f)
1357
1358         start_full_debug_logging
1359
1360         $LFS hsm_archive $f || error "could not archive file"
1361         wait_request_state $fid ARCHIVE SUCCEED
1362
1363         local blocks=$(stat -c "%b" $f)
1364         [ $blocks -eq $orig_blocks ] ||
1365                 error "$f: wrong block number after archive: " \
1366                       "$blocks != $orig_blocks"
1367         local size=$(stat -c "%s" $f)
1368         [ $size -eq $orig_size ] ||
1369                 error "$f: wrong size after archive: $size != $orig_size"
1370
1371         # Release and check states
1372         $LFS hsm_release $f || error "could not release file"
1373         check_hsm_flags $f "0x0000000d"
1374
1375         blocks=$(stat -c "%b" $f)
1376         [ $blocks -gt 5 ] &&
1377                 error "$f: too many blocks after release: $blocks > 5"
1378         size=$(stat -c "%s" $f)
1379         [ $size -ne $orig_size ] &&
1380                 error "$f: wrong size after release: $size != $orig_size"
1381
1382         # Check we can release an file without stripe info
1383         f=$f.nolov
1384         $MCREATE $f
1385         fid=$(path2fid $f)
1386         check_hsm_flags $f "0x00000000"
1387         $LFS hsm_archive $f || error "could not archive file"
1388         wait_request_state $fid ARCHIVE SUCCEED
1389
1390         # Release and check states
1391         $LFS hsm_release $f || error "could not release file"
1392         check_hsm_flags $f "0x0000000d"
1393
1394         # Release again a file that is already released is OK
1395         $LFS hsm_release $f || fail "second release should succeed"
1396         check_hsm_flags $f "0x0000000d"
1397
1398         stop_full_debug_logging
1399
1400         copytool_cleanup
1401 }
1402 run_test 21 "Simple release tests"
1403
1404 test_22() {
1405         # test needs a running copytool
1406         copytool_setup
1407
1408         mkdir -p $DIR/$tdir
1409
1410         local f=$DIR/$tdir/test_release
1411         local swap=$DIR/$tdir/test_swap
1412
1413         # Create a file and check its states
1414         local fid=$(make_small $f)
1415         check_hsm_flags $f "0x00000000"
1416
1417         $LFS hsm_archive $f || error "could not archive file"
1418         wait_request_state $fid ARCHIVE SUCCEED
1419
1420         # Release and check states
1421         $LFS hsm_release $f || error "could not release file"
1422         check_hsm_flags $f "0x0000000d"
1423
1424         make_small $swap
1425         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1426
1427         true
1428         copytool_cleanup
1429 }
1430 run_test 22 "Could not swap a release file"
1431
1432 test_23() {
1433         # test needs a running copytool
1434         copytool_setup
1435
1436         mkdir -p $DIR/$tdir
1437
1438         local f=$DIR/$tdir/test_mtime
1439
1440         # Create a file and check its states
1441         local fid=$(make_small $f)
1442         check_hsm_flags $f "0x00000000"
1443
1444         $LFS hsm_archive $f || error "could not archive file"
1445         wait_request_state $fid ARCHIVE SUCCEED
1446
1447         # Set modification time in the past
1448         touch -m -a -d @978261179 $f
1449
1450         # Release and check states
1451         $LFS hsm_release $f || error "could not release file"
1452         check_hsm_flags $f "0x0000000d"
1453
1454         local MTIME=$(stat -c "%Y" $f)
1455         local ATIME=$(stat -c "%X" $f)
1456         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1457         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1458
1459         copytool_cleanup
1460 }
1461 run_test 23 "Release does not change a/mtime (utime)"
1462
1463 test_24a() {
1464         local file=$DIR/$tdir/$tfile
1465         local fid
1466         local atime0
1467         local atime1
1468         local mtime0
1469         local mtime1
1470         local ctime0
1471         local ctime1
1472
1473         # test needs a running copytool
1474         copytool_setup
1475
1476         mkdir -p $DIR/$tdir
1477         rm -f $file
1478         fid=$(make_small $file)
1479
1480         # Create a file and check its states
1481         check_hsm_flags $file "0x00000000"
1482
1483         # Ensure atime is less than mtime and ctime.
1484         sleep 1
1485         echo >> $file
1486
1487         atime0=$(stat -c "%X" $file)
1488         mtime0=$(stat -c "%Y" $file)
1489         ctime0=$(stat -c "%Z" $file)
1490
1491         [ $atime0 -lt $mtime0 ] ||
1492                 error "atime $atime0 is not less than mtime $mtime0"
1493
1494         [ $atime0 -lt $ctime0 ] ||
1495                 error "atime $atime0 is not less than ctime $ctime0"
1496
1497         # Archive should not change any timestamps.
1498         $LFS hsm_archive $file || error "cannot archive '$file'"
1499         wait_request_state $fid ARCHIVE SUCCEED
1500
1501         atime1=$(stat -c "%X" $file)
1502         mtime1=$(stat -c "%Y" $file)
1503         ctime1=$(stat -c "%Z" $file)
1504
1505         [ $atime0 -eq $atime1 ] ||
1506                 error "archive changed atime from $atime0 to $atime1"
1507
1508         [ $mtime0 -eq $mtime1 ] ||
1509                 error "archive changed mtime from $mtime0 to $mtime1"
1510
1511         [ $ctime0 -eq $ctime1 ] ||
1512                 error "archive changed ctime from $ctime0 to $ctime1"
1513
1514         # Release should not change any timestamps.
1515         $LFS hsm_release $file || error "cannot release '$file'"
1516         check_hsm_flags $file "0x0000000d"
1517
1518         atime1=$(stat -c "%X" $file)
1519         mtime1=$(stat -c "%Y" $file)
1520         ctime1=$(stat -c "%Z" $file)
1521
1522         [ $atime0 -eq $atime1 ] ||
1523                 error "release changed atime from $atime0 to $atime1"
1524
1525         [ $mtime0 -eq $mtime1 ] ||
1526                 error "release changed mtime from $mtime0 to $mtime1"
1527
1528         [ $ctime0 -eq $ctime1 ] ||
1529                 error "release changed ctime from $ctime0 to $ctime1"
1530
1531         # Restore should not change atime or mtime and should not
1532         # decrease ctime.
1533         $LFS hsm_restore $file
1534         wait_request_state $fid RESTORE SUCCEED
1535
1536         atime1=$(stat -c "%X" $file)
1537         mtime1=$(stat -c "%Y" $file)
1538         ctime1=$(stat -c "%Z" $file)
1539
1540         [ $atime0 -eq $atime1 ] ||
1541                 error "restore changed atime from $atime0 to $atime1"
1542
1543         [ $mtime0 -eq $mtime1 ] ||
1544                 error "restore changed mtime from $mtime0 to $mtime1"
1545
1546         [ $ctime0 -le $ctime1 ] ||
1547                 error "restore changed ctime from $ctime0 to $ctime1"
1548
1549         copytool_cleanup
1550
1551         # Once more, after unmount and mount.
1552         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1553         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1554
1555         atime1=$(stat -c "%X" $file)
1556         mtime1=$(stat -c "%Y" $file)
1557         ctime1=$(stat -c "%Z" $file)
1558
1559         [ $atime0 -eq $atime1 ] ||
1560                 error "remount changed atime from $atime0 to $atime1"
1561
1562         [ $mtime0 -eq $mtime1 ] ||
1563                 error "remount changed mtime from $mtime0 to $mtime1"
1564
1565         [ $ctime0 -le $ctime1 ] ||
1566                 error "remount changed ctime from $ctime0 to $ctime1"
1567 }
1568 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1569
1570 test_24b() {
1571         local file=$DIR/$tdir/$tfile
1572         local fid
1573         local sum0
1574         local sum1
1575         # LU-3811
1576
1577         # Test needs a running copytool.
1578         copytool_setup
1579         mkdir -p $DIR/$tdir
1580
1581         # Check that root can do HSM actions on a ordinary user's file.
1582         rm -f $file
1583         fid=$(make_small $file)
1584         sum0=$(md5sum $file)
1585
1586         chown $RUNAS_ID:$RUNAS_GID $file ||
1587                 error "cannot chown '$file' to '$RUNAS_ID'"
1588
1589         chmod ugo-w $DIR/$tdir ||
1590                 error "cannot chmod '$DIR/$tdir'"
1591
1592         $LFS hsm_archive $file
1593         wait_request_state $fid ARCHIVE SUCCEED
1594
1595         $LFS hsm_release $file
1596         check_hsm_flags $file "0x0000000d"
1597
1598         $LFS hsm_restore $file
1599         wait_request_state $fid RESTORE SUCCEED
1600
1601         # Check that ordinary user can get HSM state.
1602         $RUNAS $LFS hsm_state $file ||
1603                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1604
1605         $LFS hsm_release $file
1606         check_hsm_flags $file "0x0000000d"
1607
1608         # Check that ordinary user can accessed released file.
1609         sum1=$($RUNAS md5sum $file) ||
1610                 error "user '$RUNAS_ID' cannot read '$file'"
1611
1612         [ "$sum0" == "$sum1" ] ||
1613                 error "md5sum mismatch for '$file'"
1614
1615         copytool_cleanup
1616 }
1617 run_test 24b "root can archive, release, and restore user files"
1618
1619 cleanup_test_24c() {
1620         trap 0
1621         set_hsm_param user_request_mask RESTORE
1622         set_hsm_param group_request_mask RESTORE
1623         set_hsm_param other_request_mask RESTORE
1624 }
1625
1626 test_24c() {
1627         local file=$DIR/$tdir/$tfile
1628         local action=archive
1629         local user_save
1630         local group_save
1631         local other_save
1632
1633         # test needs a running copytool
1634         copytool_setup
1635
1636         mkdir -p $DIR/$tdir
1637
1638         # Save the default masks and check that cleanup_24c will
1639         # restore the request masks correctly.
1640         user_save=$(get_hsm_param user_request_mask)
1641         group_save=$(get_hsm_param group_request_mask)
1642         other_save=$(get_hsm_param other_request_mask)
1643
1644         [ "$user_save" == RESTORE ] ||
1645                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1646         [ "$group_save" == RESTORE ] ||
1647                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1648         [ "$other_save" == RESTORE ] ||
1649                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1650
1651         trap cleanup_test_24c EXIT
1652
1653         # User.
1654         rm -f $file
1655         make_small $file
1656         chown $RUNAS_ID:nobody $file ||
1657                 error "cannot chown '$file' to '$RUNAS_ID:nobody'"
1658
1659         set_hsm_param user_request_mask ""
1660         $RUNAS $LFS hsm_$action $file &&
1661                 error "$action by user should fail"
1662
1663         set_hsm_param user_request_mask $action
1664         $RUNAS $LFS hsm_$action $file ||
1665                 error "$action by user should succeed"
1666
1667         # Group.
1668         rm -f $file
1669         make_small $file
1670         chown nobody:$RUNAS_GID $file ||
1671                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
1672
1673         set_hsm_param group_request_mask ""
1674         $RUNAS $LFS hsm_$action $file &&
1675                 error "$action by group should fail"
1676
1677         set_hsm_param group_request_mask $action
1678         $RUNAS $LFS hsm_$action $file ||
1679                 error "$action by group should succeed"
1680
1681         # Other.
1682         rm -f $file
1683         make_small $file
1684         chown nobody:nobody $file ||
1685                 error "cannot chown '$file' to 'nobody:nobody'"
1686
1687         set_hsm_param other_request_mask ""
1688         $RUNAS $LFS hsm_$action $file &&
1689                 error "$action by other should fail"
1690
1691         set_hsm_param other_request_mask $action
1692         $RUNAS $LFS hsm_$action $file ||
1693                 error "$action by other should succeed"
1694
1695         copytool_cleanup
1696         cleanup_test_24c
1697 }
1698 run_test 24c "check that user,group,other request masks work"
1699
1700 cleanup_test_24d() {
1701         trap 0
1702         mount -o remount,rw $MOUNT2
1703 }
1704
1705 test_24d() {
1706         local file1=$DIR/$tdir/$tfile
1707         local file2=$DIR2/$tdir/$tfile
1708         local fid1
1709         local fid2
1710
1711         copytool_setup
1712
1713         mkdir -p $DIR/$tdir
1714         rm -f $file1
1715         fid1=$(make_small $file1)
1716
1717         trap cleanup_test_24d EXIT
1718
1719         mount -o remount,ro $MOUNT2
1720
1721         fid2=$(path2fid $file2)
1722         [ "$fid1" == "$fid2" ] ||
1723                 error "FID mismatch '$fid1' != '$fid2'"
1724
1725         $LFS hsm_archive $file2 &&
1726                 error "archive should fail on read-only mount"
1727         check_hsm_flags $file1 "0x00000000"
1728
1729         $LFS hsm_archive $file1
1730         wait_request_state $fid1 ARCHIVE SUCCEED
1731
1732         $LFS hsm_release $file1
1733         $LFS hsm_restore $file2
1734         wait_request_state $fid1 RESTORE SUCCEED
1735
1736         $LFS hsm_release $file1 || error "cannot release '$file1'"
1737         dd if=$file2 of=/dev/null bs=1M || "cannot read '$file2'"
1738
1739         $LFS hsm_release $file2 &&
1740                 error "release should fail on read-only mount"
1741
1742         copytool_cleanup
1743         cleanup_test_24d
1744 }
1745 run_test 24d "check that read-only mounts are respected"
1746
1747 test_25a() {
1748         # test needs a running copytool
1749         copytool_setup
1750
1751         mkdir -p $DIR/$tdir
1752         copy2archive /etc/hosts $tdir/$tfile
1753
1754         local f=$DIR/$tdir/$tfile
1755
1756         import_file $tdir/$tfile $f
1757
1758         $LFS hsm_set --lost $f
1759
1760         md5sum $f
1761         local st=$?
1762
1763         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1764
1765         copytool_cleanup
1766 }
1767 run_test 25a "Restore lost file (HS_LOST flag) from import"\
1768              " (Operation not permitted)"
1769
1770 test_25b() {
1771         # test needs a running copytool
1772         copytool_setup
1773
1774         mkdir -p $DIR/$tdir
1775
1776         local f=$DIR/$tdir/$tfile
1777         local fid=$(copy_file /etc/passwd $f)
1778
1779         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1780         wait_request_state $fid ARCHIVE SUCCEED
1781
1782         $LFS hsm_release $f
1783         $LFS hsm_set --lost $f
1784         md5sum $f
1785         st=$?
1786
1787         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1788
1789         copytool_cleanup
1790 }
1791 run_test 25b "Restore lost file (HS_LOST flag) after release"\
1792              " (Operation not permitted)"
1793
1794 test_26() {
1795         # test needs a running copytool
1796         copytool_setup
1797
1798         mkdir -p $DIR/$tdir
1799         local f=$DIR/$tdir/$tfile
1800         local fid=$(make_large_for_progress $f)
1801         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1802         wait_request_state $fid ARCHIVE SUCCEED
1803
1804         $LFS hsm_remove $f
1805         wait_request_state $fid REMOVE SUCCEED
1806
1807         check_hsm_flags $f "0x00000000"
1808
1809         copytool_cleanup
1810 }
1811 run_test 26 "Remove the archive of a valid file"
1812
1813 test_27a() {
1814         # test needs a running copytool
1815         copytool_setup
1816
1817         mkdir -p $DIR/$tdir
1818         make_archive $tdir/$tfile
1819         local f=$DIR/$tdir/$tfile
1820         import_file $tdir/$tfile $f
1821         local fid=$(path2fid $f)
1822
1823         $LFS hsm_remove $f
1824
1825         [[ $? != 0 ]] || error "Remove of a released file should fail"
1826
1827         copytool_cleanup
1828 }
1829 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
1830
1831 test_27b() {
1832         # test needs a running copytool
1833         copytool_setup
1834
1835         mkdir -p $DIR/$tdir
1836         local f=$DIR/$tdir/$tfile
1837         local fid=$(make_large_for_progress $f)
1838         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1839         wait_request_state $fid ARCHIVE SUCCEED
1840         $LFS hsm_release $f
1841
1842         $LFS hsm_remove $f
1843
1844         [[ $? != 0 ]] || error "Remove of a released file should fail"
1845
1846         copytool_cleanup
1847 }
1848 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
1849
1850 test_28() {
1851         # test needs a running copytool
1852         copytool_setup
1853
1854         mkdir -p $DIR/$tdir
1855         local f=$DIR/$tdir/$tfile
1856         local fid=$(make_large_for_progress $f)
1857         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1858         wait_request_state $fid ARCHIVE SUCCEED
1859
1860         cdt_disable
1861         $LFS hsm_remove $f
1862
1863         rm -f $f
1864
1865         cdt_enable
1866
1867         wait_request_state $fid REMOVE SUCCEED
1868
1869         copytool_cleanup
1870 }
1871 run_test 28 "Concurrent archive/file remove"
1872
1873 test_30a() {
1874         # restore at exec cannot work on agent node (because of Linux kernel
1875         # protection of executables)
1876         needclients 2 || return 0
1877
1878         # test needs a running copytool
1879         copytool_setup
1880
1881         mkdir -p $DIR/$tdir
1882         copy2archive /bin/true $tdir/$tfile
1883
1884         local f=$DIR/$tdir/true
1885         import_file $tdir/$tfile $f
1886
1887         local fid=$(path2fid $f)
1888
1889         # set no retry action mode
1890         cdt_set_no_retry
1891         do_node $CLIENT2 $f
1892         local st=$?
1893
1894         # cleanup
1895         # remove no try action mode
1896         cdt_clear_no_retry
1897         $LFS hsm_state $f
1898
1899         [[ $st == 0 ]] || error "Failed to exec a released file"
1900
1901         copytool_cleanup
1902 }
1903 run_test 30a "Restore at exec (import case)"
1904
1905 test_30b() {
1906         # restore at exec cannot work on agent node (because of Linux kernel
1907         # protection of executables)
1908         needclients 2 || return 0
1909
1910         # test needs a running copytool
1911         copytool_setup
1912
1913         mkdir -p $DIR/$tdir
1914         local f=$DIR/$tdir/true
1915         local fid=$(copy_file /bin/true $f)
1916         chmod 755 $f
1917         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1918         wait_request_state $fid ARCHIVE SUCCEED
1919         $LFS hsm_release $f
1920         $LFS hsm_state $f
1921         # set no retry action mode
1922         cdt_set_no_retry
1923         do_node $CLIENT2 $f
1924         local st=$?
1925
1926         # cleanup
1927         # remove no try action mode
1928         cdt_clear_no_retry
1929         $LFS hsm_state $f
1930
1931         [[ $st == 0 ]] || error "Failed to exec a released file"
1932
1933         copytool_cleanup
1934 }
1935 run_test 30b "Restore at exec (release case)"
1936
1937 test_30c() {
1938         needclients 2 || return 0
1939
1940         # test needs a running copytool
1941         copytool_setup
1942
1943         mkdir -p $DIR/$tdir
1944         local f=$DIR/$tdir/SLEEP
1945         local fid=$(copy_file /bin/sleep $f)
1946         chmod 755 $f
1947         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1948         wait_request_state $fid ARCHIVE SUCCEED
1949         $LFS hsm_release $f
1950         check_hsm_flags $f "0x0000000d"
1951         # set no retry action mode
1952         cdt_set_no_retry
1953         do_node $CLIENT2 "$f 10" &
1954         local pid=$!
1955         sleep 3
1956         echo 'Hi!' > $f
1957         [[ $? == 0 ]] && error "Update during exec of released file must fail"
1958         wait $pid
1959         [[ $? == 0 ]] || error "Execution failed during run"
1960         cmp /bin/sleep $f
1961         [[ $? == 0 ]] || error "Binary overwritten during exec"
1962
1963         # cleanup
1964         # remove no try action mode
1965         cdt_clear_no_retry
1966         check_hsm_flags $f "0x00000009"
1967
1968         copytool_cleanup
1969 }
1970 run_test 30c "Update during exec of released file must fail"
1971
1972 restore_and_check_size() {
1973         local f=$1
1974         local fid=$2
1975         local s=$(stat -c "%s" $f)
1976         local n=$s
1977         local st=$(get_hsm_flags $f)
1978         local err=0
1979         local cpt=0
1980         $LFS hsm_restore $f
1981         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
1982         do
1983                 n=$(stat -c "%s" $f)
1984                 # we echo in both cases to show stat is not
1985                 # hang
1986                 if [[ $n != $s ]]; then
1987                         echo "size seen is $n != $s"
1988                         err=1
1989                 else
1990                         echo "size seen is right: $n == $s"
1991                 fi
1992                 st=$(get_hsm_flags $f)
1993                 sleep 10
1994                 cpt=$((cpt + 1))
1995         done
1996         if [[ $cpt -lt 10 ]]; then
1997                 echo " "done
1998         else
1999                 echo " restore is too long"
2000                 wait_request_state $fid RESTORE SUCCEED
2001         fi
2002         return $err
2003 }
2004
2005 test_31a() {
2006         # test needs a running copytool
2007         copytool_setup
2008
2009         mkdir -p $DIR/$tdir
2010
2011         make_archive $tdir/$tfile
2012         local f=$DIR/$tdir/$tfile
2013         import_file $tdir/$tfile $f
2014         local fid=$($LFS path2fid $f)
2015         HSM_ARCHIVE_PURGE=false copytool_setup
2016
2017         restore_and_check_size $f $fid
2018         local err=$?
2019
2020         [[ $err -eq 0 ]] || error "File size changed during restore"
2021
2022         copytool_cleanup
2023 }
2024 run_test 31a "Import a large file and check size during restore"
2025
2026
2027 test_31b() {
2028         # test needs a running copytool
2029         copytool_setup
2030
2031         mkdir -p $DIR/$tdir
2032
2033         local f=$DIR/$tdir/$tfile
2034         local fid=$(make_large_for_progress $f)
2035         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2036         wait_request_state $fid ARCHIVE SUCCEED
2037         $LFS hsm_release $f
2038
2039         restore_and_check_size $f $fid
2040         local err=$?
2041
2042         [[ $err -eq 0 ]] || error "File size changed during restore"
2043
2044         copytool_cleanup
2045 }
2046 run_test 31b "Restore a large unaligned file and check size during restore"
2047
2048 test_31c() {
2049         # test needs a running copytool
2050         copytool_setup
2051
2052         mkdir -p $DIR/$tdir
2053
2054         local f=$DIR/$tdir/$tfile
2055         local fid=$(make_large_for_progress_aligned $f)
2056         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2057         wait_request_state $fid ARCHIVE SUCCEED
2058         $LFS hsm_release $f
2059
2060         restore_and_check_size $f $fid
2061         local err=$?
2062
2063         [[ $err -eq 0 ]] || error "File size changed during restore"
2064
2065         copytool_cleanup
2066 }
2067 run_test 31c "Restore a large aligned file and check size during restore"
2068
2069 test_33() {
2070         # test needs a running copytool
2071         copytool_setup
2072
2073         mkdir -p $DIR/$tdir
2074
2075         local f=$DIR/$tdir/$tfile
2076         local fid=$(make_large_for_progress $f)
2077         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2078         wait_request_state $fid ARCHIVE SUCCEED
2079         $LFS hsm_release $f
2080
2081         md5sum $f >/dev/null &
2082         local pid=$!
2083         wait_request_state $fid RESTORE STARTED
2084
2085         kill -15 $pid
2086         sleep 1
2087
2088         # Check restore trigger process was killed
2089         local killed=$(ps -o pid,comm hp $pid >/dev/null)
2090
2091         $LFS hsm_cancel $f
2092
2093         wait_request_state $fid RESTORE CANCELED
2094         wait_request_state $fid CANCEL SUCCEED
2095
2096         [ -z $killed ] ||
2097                 error "Cannot kill process waiting for restore ($killed)"
2098
2099         copytool_cleanup
2100 }
2101 run_test 33 "Kill a restore waiting process"
2102
2103 test_34() {
2104         # test needs a running copytool
2105         copytool_setup
2106
2107         mkdir -p $DIR/$tdir
2108
2109         local f=$DIR/$tdir/$tfile
2110         local fid=$(make_large_for_progress $f)
2111         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2112         wait_request_state $fid ARCHIVE SUCCEED
2113         $LFS hsm_release $f
2114
2115         md5sum $f >/dev/null &
2116         local pid=$!
2117         wait_request_state $fid RESTORE STARTED
2118
2119         rm $f || error "rm $f failed"
2120         # rm must not block during restore
2121         wait_request_state $fid RESTORE STARTED
2122
2123         wait_request_state $fid RESTORE SUCCEED
2124         # check md5sum pgm finished
2125         local there=$(ps -o pid,comm hp $pid >/dev/null)
2126         [[ -z $there ]] || error "Restore initiator does not exit"
2127
2128         local rc=$(wait $pid)
2129         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2130
2131         copytool_cleanup
2132 }
2133 run_test 34 "Remove file during restore"
2134
2135 test_35() {
2136         # test needs a running copytool
2137         copytool_setup
2138
2139         mkdir -p $DIR/$tdir
2140
2141         local f=$DIR/$tdir/$tfile
2142         local f1=$DIR/$tdir/$tfile-1
2143         local fid=$(make_large_for_progress $f)
2144         local fid1=$(copy_file /etc/passwd $f1)
2145         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2146         wait_request_state $fid ARCHIVE SUCCEED
2147         $LFS hsm_release $f
2148
2149         md5sum $f >/dev/null &
2150         local pid=$!
2151         wait_request_state $fid RESTORE STARTED
2152
2153         mv $f1 $f || error "mv $f1 $f failed"
2154         # mv must not block during restore
2155         wait_request_state $fid RESTORE STARTED
2156
2157         wait_request_state $fid RESTORE SUCCEED
2158         # check md5sum pgm finished
2159         local there=$(ps -o pid,comm hp $pid >/dev/null)
2160         [[ -z $there ]] || error "Restore initiator does not exit"
2161
2162         local rc=$(wait $pid)
2163         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2164
2165         fid2=$(path2fid $f)
2166         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2167
2168         copytool_cleanup
2169 }
2170 run_test 35 "Overwrite file during restore"
2171
2172 test_36() {
2173         # test needs a running copytool
2174         copytool_setup
2175
2176         mkdir -p $DIR/$tdir
2177
2178         local f=$DIR/$tdir/$tfile
2179         local fid=$(make_large_for_progress $f)
2180         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2181         wait_request_state $fid ARCHIVE SUCCEED
2182         $LFS hsm_release $f
2183
2184         md5sum $f >/dev/null &
2185         local pid=$!
2186         wait_request_state $fid RESTORE STARTED
2187
2188         mv $f $f.new
2189         # rm must not block during restore
2190         wait_request_state $fid RESTORE STARTED
2191
2192         wait_request_state $fid RESTORE SUCCEED
2193         # check md5sum pgm finished
2194         local there=$(ps -o pid,comm hp $pid >/dev/null)
2195         [[ -z $there ]] ||
2196                 error "Restore initiator does not exit"
2197
2198         local rc=$(wait $pid)
2199         [[ $rc -eq 0 ]] ||
2200                 error "Restore initiator failed with $rc"
2201
2202         copytool_cleanup
2203 }
2204 run_test 36 "Move file during restore"
2205
2206 multi_archive() {
2207         local prefix=$1
2208         local count=$2
2209         local n=""
2210
2211         for n in $(seq 1 $count); do
2212                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2213         done
2214         echo "$count archive requests submitted"
2215 }
2216
2217 test_40() {
2218         local stream_count=4
2219         local file_count=100
2220         mkdir -p $DIR/$tdir
2221         local f=$DIR/$tdir/$tfile
2222         local i=""
2223         local p=""
2224         local fid=""
2225
2226         for i in $(seq 1 $file_count); do
2227                 for p in $(seq 1 $stream_count); do
2228                         fid=$(copy_file /etc/hosts $f.$p.$i)
2229                 done
2230         done
2231         copytool_setup
2232         # to be sure wait_all_done will not be mislead by previous tests
2233         cdt_purge
2234         wait_for_grace_delay
2235         typeset -a pids
2236         # start archive streams in background (archive files in parallel)
2237         for p in $(seq 1 $stream_count); do
2238                 multi_archive $f.$p $file_count &
2239                 pids[$p]=$!
2240         done
2241         echo -n  "Wait for all requests being enqueued..."
2242         wait ${pids[*]}
2243         echo OK
2244         wait_all_done 100
2245         copytool_cleanup
2246 }
2247 run_test 40 "Parallel archive requests"
2248
2249 test_52() {
2250         # test needs a running copytool
2251         copytool_setup
2252
2253         mkdir -p $DIR/$tdir
2254         local f=$DIR/$tdir/$tfile
2255         local fid=$(copy_file /etc/motd $f 1)
2256
2257         $LFS hsm_archive $f || error "could not archive file"
2258         wait_request_state $fid ARCHIVE SUCCEED
2259         check_hsm_flags $f "0x00000009"
2260
2261         multiop_bg_pause $f O_c || error "multiop failed"
2262         local MULTIPID=$!
2263
2264         mds_evict_client
2265         client_up || client_up || true
2266
2267         kill -USR1 $MULTIPID
2268         wait $MULTIPID || error "multiop close failed"
2269
2270         check_hsm_flags $f "0x0000000b"
2271
2272         copytool_cleanup
2273 }
2274 run_test 52 "Opened for write file on an evicted client should be set dirty"
2275
2276 test_53() {
2277         # test needs a running copytool
2278         copytool_setup
2279
2280         mkdir -p $DIR/$tdir
2281         local f=$DIR/$tdir/$tfile
2282         local fid=$(copy_file /etc/motd $f 1)
2283
2284         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2285                 error "could not archive file"
2286         wait_request_state $fid ARCHIVE SUCCEED
2287         check_hsm_flags $f "0x00000009"
2288
2289         multiop_bg_pause $f o_c || error "multiop failed"
2290         MULTIPID=$!
2291
2292         mds_evict_client
2293         client_up || client_up || true
2294
2295         kill -USR1 $MULTIPID
2296         wait $MULTIPID || error "multiop close failed"
2297
2298         check_hsm_flags $f "0x00000009"
2299
2300         copytool_cleanup
2301 }
2302 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2303
2304 test_54() {
2305         # test needs a running copytool
2306         copytool_setup
2307
2308         mkdir -p $DIR/$tdir
2309         local f=$DIR/$tdir/$tfile
2310         local fid=$(make_small $f)
2311
2312         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2313                 error "could not archive file"
2314         wait_request_state $fid ARCHIVE STARTED
2315
2316         check_hsm_flags $f "0x00000001"
2317
2318         # Avoid coordinator resending this request as soon it has failed.
2319         cdt_set_no_retry
2320
2321         echo "foo" >> $f
2322         sync
2323         wait_request_state $fid ARCHIVE FAILED
2324
2325         check_hsm_flags $f "0x00000003"
2326
2327         cdt_clear_no_retry
2328         copytool_cleanup
2329 }
2330 run_test 54 "Write during an archive cancels it"
2331
2332 test_55() {
2333         # test needs a running copytool
2334         copytool_setup
2335
2336         mkdir -p $DIR/$tdir
2337         local f=$DIR/$tdir/$tfile
2338         local fid=$(make_small $f)
2339
2340         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2341                 error "could not archive file"
2342         wait_request_state $fid ARCHIVE STARTED
2343
2344         check_hsm_flags $f "0x00000001"
2345
2346         # Avoid coordinator resending this request as soon it has failed.
2347         cdt_set_no_retry
2348
2349         $TRUNCATE $f 1024 || error "truncate failed"
2350         sync
2351         wait_request_state $fid ARCHIVE FAILED
2352
2353         check_hsm_flags $f "0x00000003"
2354
2355         cdt_clear_no_retry
2356         copytool_cleanup
2357 }
2358 run_test 55 "Truncate during an archive cancels it"
2359
2360 test_56() {
2361         # test needs a running copytool
2362         copytool_setup
2363
2364         mkdir -p $DIR/$tdir
2365         local f=$DIR/$tdir/$tfile
2366         local fid=$(make_large_for_progress $f)
2367
2368         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2369                 error "could not archive file"
2370         wait_request_state $fid ARCHIVE STARTED
2371
2372         check_hsm_flags $f "0x00000001"
2373
2374         # Change metadata and sync to be sure we are not changing only
2375         # in memory.
2376         chmod 644 $f
2377         chgrp sys $f
2378         sync
2379         wait_request_state $fid ARCHIVE SUCCEED
2380
2381         check_hsm_flags $f "0x00000009"
2382
2383         copytool_cleanup
2384 }
2385 run_test 56 "Setattr during an archive is ok"
2386
2387 test_57() {
2388         # Need one client for I/O, one for request
2389         needclients 2 || return 0
2390
2391         # test needs a running copytool
2392         copytool_setup
2393
2394         mkdir -p $DIR/$tdir
2395         local f=$DIR/$tdir/test_archive_remote
2396         # Create a file on a remote node
2397         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2398                 "count=2 conv=fsync"
2399
2400         # And archive it
2401         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2402                 error "hsm_archive failed"
2403         local fid=$(path2fid $f)
2404         wait_request_state $fid ARCHIVE SUCCEED
2405
2406         # Release and implicit restore it
2407         do_node $CLIENT2 "$LFS hsm_release $f" ||
2408                 error "hsm_release failed"
2409         do_node $CLIENT2 "md5sum $f" ||
2410                 error "hsm_restore failed"
2411
2412         wait_request_state $fid RESTORE SUCCEED
2413
2414         copytool_cleanup
2415 }
2416 run_test 57 "Archive a file with dirty cache on another node"
2417
2418 truncate_released_file() {
2419         local src_file=$1
2420         local trunc_to=$2
2421
2422         local sz=$(stat -c %s $src_file)
2423         local f=$DIR/$tdir/$tfile
2424         local fid=$(copy_file $1 $f)
2425         local ref=$f-ref
2426         cp $f $f-ref
2427
2428         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2429                 error "could not archive file"
2430         wait_request_state $fid ARCHIVE SUCCEED
2431
2432         $LFS hsm_release $f || error "could not release file"
2433
2434         $TRUNCATE $f $trunc_to || error "truncate failed"
2435         sync
2436
2437         local sz1=$(stat -c %s $f)
2438         [[ $sz1 == $trunc_to ]] ||
2439                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2440
2441         $LFS hsm_state $f
2442         check_hsm_flags $f "0x0000000b"
2443
2444         local state=$(get_request_state $fid RESTORE)
2445         [[ "$state" == "SUCCEED" ]] ||
2446                 error "truncate $sz does not trig restore, state = $state"
2447
2448         $TRUNCATE $ref $trunc_to
2449         cmp $ref $f || error "file data wrong after truncate"
2450
2451         rm -f $f $f-ref
2452 }
2453
2454 test_58() {
2455         # test needs a running copytool
2456         copytool_setup
2457
2458         mkdir -p $DIR/$tdir
2459
2460         local sz=$(stat -c %s /etc/passwd)
2461
2462         echo "truncate up from $sz to $((sz*2))"
2463         truncate_released_file /etc/passwd $((sz*2))
2464
2465         echo "truncate down from $sz to $((sz/2))"
2466         truncate_released_file /etc/passwd $((sz/2))
2467
2468         echo "truncate to 0"
2469         truncate_released_file /etc/passwd 0
2470
2471         copytool_cleanup
2472 }
2473 run_test 58 "Truncate a released file will trigger restore"
2474
2475 test_90() {
2476         file_count=57
2477         mkdir -p $DIR/$tdir
2478         local f=$DIR/$tdir/$tfile
2479         local FILELIST=/tmp/filelist.txt
2480         local i=""
2481
2482         rm -f $FILELIST
2483         for i in $(seq 1 $file_count); do
2484                 fid=$(copy_file /etc/hosts $f.$i)
2485                 echo $f.$i >> $FILELIST
2486         done
2487         copytool_setup
2488         # to be sure wait_all_done will not be mislead by previous tests
2489         cdt_purge
2490         wait_for_grace_delay
2491         $LFS hsm_archive --filelist $FILELIST ||
2492                 error "cannot archive a file list"
2493         wait_all_done 100
2494         $LFS hsm_release --filelist $FILELIST ||
2495                 error "cannot release a file list"
2496         $LFS hsm_restore --filelist $FILELIST ||
2497                 error "cannot restore a file list"
2498         wait_all_done 100
2499         copytool_cleanup
2500 }
2501 run_test 90 "Archive/restore a file list"
2502
2503 double_verify_reset_hsm_param() {
2504         local p=$1
2505         echo "Testing $HSM_PARAM.$p"
2506         local val=$(get_hsm_param $p)
2507         local save=$val
2508         local val2=$(($val * 2))
2509         set_hsm_param $p $val2
2510         val=$(get_hsm_param $p)
2511         [[ $val == $val2 ]] ||
2512                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
2513         echo "Set $p to 0 must failed"
2514         set_hsm_param $p 0
2515         local rc=$?
2516         # restore value
2517         set_hsm_param $p $save
2518
2519         if [[ $rc == 0 ]]; then
2520                 error "we must not be able to set $HSM_PARAM.$p to 0"
2521         fi
2522 }
2523
2524 test_100() {
2525         double_verify_reset_hsm_param loop_period
2526         double_verify_reset_hsm_param grace_delay
2527         double_verify_reset_hsm_param active_request_timeout
2528         double_verify_reset_hsm_param max_requests
2529         double_verify_reset_hsm_param default_archive_id
2530 }
2531 run_test 100 "Set coordinator /proc tunables"
2532
2533 test_102() {
2534         cdt_disable
2535         cdt_enable
2536         cdt_restart
2537 }
2538 run_test 102 "Verify coordinator control"
2539
2540 test_103() {
2541         # test needs a running copytool
2542         copytool_setup
2543
2544         local i=""
2545         local fid=""
2546
2547         mkdir -p $DIR/$tdir
2548         for i in $(seq 1 20); do
2549                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
2550         done
2551         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
2552
2553         cdt_purge
2554
2555         echo "Current requests"
2556         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2557                         $HSM_PARAM.actions |\
2558                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
2559
2560         [[ -z "$res" ]] || error "Some request have not been canceled"
2561
2562         copytool_cleanup
2563 }
2564 run_test 103 "Purge all requests"
2565
2566 DATA=CEA
2567 DATAHEX='[434541]'
2568 test_104() {
2569         # test needs a running copytool
2570         copytool_setup
2571
2572         mkdir -p $DIR/$tdir
2573         local f=$DIR/$tdir/$tfile
2574         local fid=$(make_large_for_progress $f)
2575         # if cdt is on, it can serve too quickly the request
2576         cdt_disable
2577         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
2578         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2579                         $HSM_PARAM.actions |\
2580                         grep $fid | cut -f16 -d=")
2581         cdt_enable
2582
2583         [[ "$data1" == "$DATAHEX" ]] ||
2584                 error "Data field in records is ($data1) and not ($DATAHEX)"
2585
2586         copytool_cleanup
2587 }
2588 run_test 104 "Copy tool data field"
2589
2590 test_105() {
2591         mkdir -p $DIR/$tdir
2592         local i=""
2593
2594         cdt_disable
2595         for i in $(seq -w 1 10); do
2596                 cp /etc/passwd $DIR/$tdir/$i
2597                 $LFS hsm_archive $DIR/$tdir/$i
2598         done
2599         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2600                         $HSM_PARAM.actions |\
2601                         grep WAITING | wc -l")
2602         cdt_restart
2603         cdt_disable
2604         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2605                         $HSM_PARAM.actions |\
2606                         grep WAITING | wc -l")
2607         cdt_enable
2608         cdt_purge
2609         [[ "$reqcnt1" == "$reqcnt2" ]] ||
2610                 error "Requests count after shutdown $reqcnt2 != "\
2611                       "before shutdown $reqcnt1"
2612 }
2613 run_test 105 "Restart of coordinator"
2614
2615 get_agent_by_uuid_mdt() {
2616         local uuid=$1
2617         local mdtidx=$2
2618         local mds=mds$(($mdtidx + 1))
2619         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
2620                  grep $uuid"
2621 }
2622
2623 check_agent_registered_by_mdt() {
2624         local uuid=$1
2625         local mdtidx=$2
2626         local mds=mds$(($mdtidx + 1))
2627         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2628         if [[ ! -z "$agent" ]]; then
2629                 echo "found agent $agent on $mds"
2630         else
2631                 error "uuid $uuid not found in agent list on $mds"
2632         fi
2633 }
2634
2635 check_agent_unregistered_by_mdt() {
2636         local uuid=$1
2637         local mdtidx=$2
2638         local mds=mds$(($mdtidx + 1))
2639         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2640         if [[ -z "$agent" ]]; then
2641                 echo "uuid not found in agent list on $mds"
2642         else
2643                 error "uuid found in agent list on $mds: $agent"
2644         fi
2645 }
2646
2647 check_agent_registered() {
2648         local uuid=$1
2649         local mdsno
2650         for mdsno in $(seq 1 $MDSCOUNT); do
2651                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
2652         done
2653 }
2654
2655 check_agent_unregistered() {
2656         local uuid=$1
2657         local mdsno
2658         for mdsno in $(seq 1 $MDSCOUNT); do
2659                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
2660         done
2661 }
2662
2663 test_106() {
2664         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
2665                 get_client_uuid $MOUNT | cut -d' ' -f2)
2666
2667         copytool_setup
2668         check_agent_registered $uuid
2669
2670         search_copytools || error "No copytool found"
2671
2672         copytool_cleanup
2673         check_agent_unregistered $uuid
2674
2675         copytool_setup
2676         check_agent_registered $uuid
2677
2678         copytool_cleanup
2679 }
2680 run_test 106 "Copytool register/unregister"
2681
2682 test_107() {
2683         # test needs a running copytool
2684         copytool_setup
2685         # create and archive file
2686         mkdir -p $DIR/$tdir
2687         local f1=$DIR/$tdir/$tfile
2688         local fid=$(copy_file /etc/passwd $f1)
2689         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2690         wait_request_state $fid ARCHIVE SUCCEED
2691         # shutdown and restart MDS
2692         fail $SINGLEMDS
2693         # check the copytool still gets messages from MDT
2694         local f2=$DIR/$tdir/2
2695         local fid=$(copy_file /etc/passwd $f2)
2696         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2697         # main check of this sanity: this request MUST succeed
2698         wait_request_state $fid ARCHIVE SUCCEED
2699         copytool_cleanup
2700 }
2701 run_test 107 "Copytool re-register after MDS restart"
2702
2703 policy_set_and_test()
2704 {
2705         local change="$1"
2706         local target="$2"
2707         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
2708         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2709         [[ "$policy" == "$target" ]] ||
2710                 error "Wrong policy after '$change': '$policy' != '$target'"
2711 }
2712
2713 test_109() {
2714         # to force default policy setting if error
2715         CDT_POLICY_HAD_CHANGED=true
2716
2717         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2718         local default="NonBlockingRestore [NoRetryAction]"
2719         [[ "$policy" == "$default" ]] ||
2720                 error "default policy has changed,"\
2721                       " '$policy' != '$default' update the test"
2722         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
2723         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
2724         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
2725         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
2726         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
2727         # useless bacause we know but safer for futur changes to use real value
2728         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2729         echo "Next set_param must failed"
2730         policy_set_and_test "wrong" "$policy"
2731
2732         # return to default
2733         echo "Back to default policy"
2734         cdt_set_sanity_policy
2735 }
2736 run_test 109 "Policy display/change"
2737
2738 test_110a() {
2739         # test needs a running copytool
2740         copytool_setup
2741
2742         mkdir -p $DIR/$tdir
2743
2744         copy2archive /etc/passwd $tdir/$tfile
2745
2746         local f=$DIR/$tdir/$tfile
2747         import_file $tdir/$tfile $f
2748         local fid=$(path2fid $f)
2749
2750         cdt_set_non_blocking_restore
2751         md5sum $f
2752         local st=$?
2753
2754         # cleanup
2755         wait_request_state $fid RESTORE SUCCEED
2756         cdt_clear_non_blocking_restore
2757
2758         # Test result
2759         [[ $st == 1 ]] ||
2760                 error "md5sum returns $st != 1, "\
2761                         "should also perror ENODATA (No data available)"
2762
2763         copytool_cleanup
2764 }
2765 run_test 110a "Non blocking restore policy (import case)"
2766
2767 test_110b() {
2768         # test needs a running copytool
2769         copytool_setup
2770
2771         mkdir -p $DIR/$tdir
2772         local f=$DIR/$tdir/$tfile
2773         local fid=$(copy_file /etc/passwd $f)
2774         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2775         wait_request_state $fid ARCHIVE SUCCEED
2776         $LFS hsm_release $f
2777
2778         cdt_set_non_blocking_restore
2779         md5sum $f
2780         local st=$?
2781
2782         # cleanup
2783         wait_request_state $fid RESTORE SUCCEED
2784         cdt_clear_non_blocking_restore
2785
2786         # Test result
2787         [[ $st == 1 ]] ||
2788                 error "md5sum returns $st != 1, "\
2789                         "should also perror ENODATA (No data available)"
2790
2791         copytool_cleanup
2792 }
2793 run_test 110b "Non blocking restore policy (release case)"
2794
2795 test_111a() {
2796         # test needs a running copytool
2797         copytool_setup
2798
2799         mkdir -p $DIR/$tdir
2800         copy2archive /etc/passwd $tdir/$tfile
2801
2802         local f=$DIR/$tdir/$tfile
2803
2804         import_file $tdir/$tfile $f
2805         local fid=$(path2fid $f)
2806
2807         cdt_set_no_retry
2808
2809         copytool_remove_backend $fid
2810
2811         $LFS hsm_restore $f
2812         wait_request_state $fid RESTORE FAILED
2813         local st=$?
2814
2815         # cleanup
2816         cdt_clear_no_retry
2817
2818         # Test result
2819         [[ $st == 0 ]] || error "Restore does not failed"
2820
2821         copytool_cleanup
2822 }
2823 run_test 111a "No retry policy (import case), restore will error"\
2824               " (No such file or directory)"
2825
2826 test_111b() {
2827         # test needs a running copytool
2828         copytool_setup
2829
2830         mkdir -p $DIR/$tdir
2831         local f=$DIR/$tdir/$tfile
2832         local fid=$(copy_file /etc/passwd $f)
2833         cdt_set_no_retry
2834         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2835         wait_request_state $fid ARCHIVE SUCCEED
2836         $LFS hsm_release $f
2837
2838         copytool_remove_backend $fid
2839
2840         $LFS hsm_restore $f
2841         wait_request_state $fid RESTORE FAILED
2842         local st=$?
2843
2844         # cleanup
2845         cdt_clear_no_retry
2846
2847         # Test result
2848         [[ $st == 0 ]] || error "Restore does not failed"
2849
2850         copytool_cleanup
2851 }
2852 run_test 111b "No retry policy (release case), restore will error"\
2853               " (No such file or directory)"
2854
2855 test_112() {
2856         # test needs a running copytool
2857         copytool_setup
2858
2859         mkdir -p $DIR/$tdir
2860         local f=$DIR/$tdir/$tfile
2861         local fid=$(copy_file /etc/passwd $f)
2862         cdt_disable
2863         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2864         local l=$($LFS hsm_action $f)
2865         echo $l
2866         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
2867
2868         # cleanup
2869         cdt_enable
2870         wait_request_state $fid ARCHIVE SUCCEED
2871
2872         # Test result
2873         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
2874
2875         copytool_cleanup
2876 }
2877 run_test 112 "State of recorded request"
2878
2879 test_200() {
2880         # test needs a running copytool
2881         copytool_setup
2882
2883         mkdir -p $DIR/$tdir
2884         local f=$DIR/$tdir/$tfile
2885         local fid=$(make_large_for_cancel $f)
2886         # test with cdt on is made in test_221
2887         cdt_disable
2888         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2889         $LFS hsm_cancel $f
2890         cdt_enable
2891         wait_request_state $fid ARCHIVE CANCELED
2892         wait_request_state $fid CANCEL SUCCEED
2893
2894         copytool_cleanup
2895 }
2896 run_test 200 "Register/Cancel archive"
2897
2898 test_201() {
2899         # test needs a running copytool
2900         copytool_setup
2901
2902         mkdir -p $DIR/$tdir
2903         local f=$DIR/$tdir/$tfile
2904         make_archive $tdir/$tfile
2905         import_file $tdir/$tfile $f
2906         local fid=$(path2fid $f)
2907
2908         # test with cdt on is made in test_222
2909         cdt_disable
2910         $LFS hsm_restore $f
2911         $LFS hsm_cancel $f
2912         cdt_enable
2913         wait_request_state $fid RESTORE CANCELED
2914         wait_request_state $fid CANCEL SUCCEED
2915
2916         copytool_cleanup
2917 }
2918 run_test 201 "Register/Cancel restore"
2919
2920 test_202() {
2921         # test needs a running copytool
2922         copytool_setup
2923
2924         mkdir -p $DIR/$tdir
2925         local f=$DIR/$tdir/$tfile
2926         local fid=$(make_large_for_progress $f)
2927         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2928         wait_request_state $fid ARCHIVE SUCCEED
2929
2930         cdt_disable
2931         $LFS hsm_remove $f
2932         $LFS hsm_cancel $f
2933         cdt_enable
2934         wait_request_state $fid REMOVE CANCELED
2935
2936         copytool_cleanup
2937 }
2938 run_test 202 "Register/Cancel remove"
2939
2940 test_220() {
2941         # test needs a running copytool
2942         copytool_setup
2943
2944         mkdir -p $DIR/$tdir
2945
2946         local f=$DIR/$tdir/$tfile
2947         local fid=$(copy_file /etc/passwd $f)
2948
2949         changelog_setup
2950
2951         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2952         wait_request_state $fid ARCHIVE SUCCEED
2953
2954         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2955         changelog_cleanup
2956
2957         local target=0x0
2958         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2959
2960         copytool_cleanup
2961 }
2962 run_test 220 "Changelog for archive"
2963
2964 test_221() {
2965         # test needs a running copytool
2966         copytool_setup
2967
2968         mkdir -p $DIR/$tdir
2969
2970         local f=$DIR/$tdir/$tfile
2971         local fid=$(make_large_for_cancel $f)
2972
2973         changelog_setup
2974
2975         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2976         wait_request_state $fid ARCHIVE STARTED
2977         $LFS hsm_cancel $f
2978         wait_request_state $fid ARCHIVE CANCELED
2979         wait_request_state $fid CANCEL SUCCEED
2980
2981         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2982
2983         local target=0x7d
2984         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2985
2986         cleanup
2987 }
2988 run_test 221 "Changelog for archive canceled"
2989
2990 test_222a() {
2991         # test needs a running copytool
2992         copytool_setup
2993
2994         mkdir -p $DIR/$tdir
2995         copy2archive /etc/passwd $tdir/$tfile
2996
2997         local f=$DIR/$tdir/$tfile
2998         import_file $tdir/$tfile $f
2999         local fid=$(path2fid $f)
3000
3001         changelog_setup
3002
3003         $LFS hsm_restore $f
3004         wait_request_state $fid RESTORE SUCCEED
3005
3006         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3007
3008         local target=0x80
3009         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3010
3011         cleanup
3012 }
3013 run_test 222a "Changelog for explicit restore"
3014
3015 test_222b() {
3016         # test needs a running copytool
3017         copytool_setup
3018
3019         mkdir -p $DIR/$tdir
3020         local f=$DIR/$tdir/$tfile
3021         local fid=$(copy_file /etc/passwd $f)
3022
3023         changelog_setup
3024         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3025         wait_request_state $fid ARCHIVE SUCCEED
3026         $LFS hsm_release $f
3027
3028         md5sum $f
3029
3030         wait_request_state $fid RESTORE SUCCEED
3031
3032         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3033
3034         local target=0x80
3035         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3036
3037         cleanup
3038 }
3039 run_test 222b "Changelog for implicit restore"
3040
3041 test_223a() {
3042         # test needs a running copytool
3043         copytool_setup
3044
3045         mkdir -p $DIR/$tdir
3046
3047         local f=$DIR/$tdir/$tfile
3048         make_archive $tdir/$tfile
3049
3050         changelog_setup
3051
3052         import_file $tdir/$tfile $f
3053         local fid=$(path2fid $f)
3054
3055         $LFS hsm_restore $f
3056         wait_request_state $fid RESTORE STARTED
3057         $LFS hsm_cancel $f
3058         wait_request_state $fid RESTORE CANCELED
3059         wait_request_state $fid CANCEL SUCCEED
3060
3061         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3062
3063         local target=0xfd
3064         [[ $flags == $target ]] ||
3065                 error "Changelog flag is $flags not $target"
3066
3067         cleanup
3068 }
3069 run_test 223a "Changelog for restore canceled (import case)"
3070
3071 test_223b() {
3072         # test needs a running copytool
3073         copytool_setup
3074
3075         mkdir -p $DIR/$tdir
3076
3077         local f=$DIR/$tdir/$tfile
3078         local fid=$(make_large_for_progress $f)
3079
3080         changelog_setup
3081         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3082         wait_request_state $fid ARCHIVE SUCCEED
3083         $LFS hsm_release $f
3084         $LFS hsm_restore $f
3085         wait_request_state $fid RESTORE STARTED
3086         $LFS hsm_cancel $f
3087         wait_request_state $fid RESTORE CANCELED
3088         wait_request_state $fid CANCEL SUCCEED
3089
3090         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3091
3092         local target=0xfd
3093         [[ $flags == $target ]] ||
3094                 error "Changelog flag is $flags not $target"
3095
3096         cleanup
3097 }
3098 run_test 223b "Changelog for restore canceled (release case)"
3099
3100 test_224() {
3101         # test needs a running copytool
3102         copytool_setup
3103
3104         mkdir -p $DIR/$tdir
3105
3106         local f=$DIR/$tdir/$tfile
3107         local fid=$(copy_file /etc/passwd $f)
3108
3109         changelog_setup
3110         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3111         wait_request_state $fid ARCHIVE SUCCEED
3112
3113         $LFS hsm_remove $f
3114         wait_request_state $fid REMOVE SUCCEED
3115
3116         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
3117
3118         local target=0x200
3119         [[ $flags == $target ]] ||
3120                 error "Changelog flag is $flags not $target"
3121
3122         cleanup
3123 }
3124 run_test 224 "Changelog for remove"
3125
3126 test_225() {
3127         # test needs a running copytool
3128         copytool_setup
3129
3130         # test is not usable because remove request is too fast
3131         # so it is always finished before cancel can be done ...
3132         echo "Test disabled"
3133         copytool_cleanup
3134         return 0
3135
3136         mkdir -p $DIR/$tdir
3137         local f=$DIR/$tdir/$tfile
3138         local fid=$(make_large_for_progress $f)
3139
3140         changelog_setup
3141         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3142         wait_request_state $fid ARCHIVE SUCCEED
3143
3144         # if cdt is on, it can serve too quickly the request
3145         cdt_disable
3146         $LFS hsm_remove $f
3147         $LFS hsm_cancel $f
3148         cdt_enable
3149         wait_request_state $fid REMOVE CANCELED
3150         wait_request_state $fid CANCEL SUCCEED
3151
3152         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3153         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
3154                 tail -n 1 | awk '{print $5}')
3155
3156         local target=0x27d
3157         [[ $flags == $target ]] ||
3158                 error "Changelog flag is $flags not $target"
3159
3160         cleanup
3161 }
3162 run_test 225 "Changelog for remove canceled"
3163
3164 test_226() {
3165         # test needs a running copytool
3166         copytool_setup
3167
3168         mkdir -p $DIR/$tdir
3169
3170         local f1=$DIR/$tdir/$tfile-1
3171         local f2=$DIR/$tdir/$tfile-2
3172         local f3=$DIR/$tdir/$tfile-3
3173         local fid1=$(copy_file /etc/passwd $f1)
3174         local fid2=$(copy_file /etc/passwd $f2)
3175         copy_file /etc/passwd $f3
3176
3177         changelog_setup
3178         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3179         wait_request_state $fid1 ARCHIVE SUCCEED
3180
3181         $LFS hsm_archive $f2
3182         wait_request_state $fid2 ARCHIVE SUCCEED
3183
3184         rm $f1 || error "rm $f1 failed"
3185
3186         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
3187
3188         local target=0x3
3189         [[ $flags == $target ]] ||
3190                 error "Changelog flag is $flags not $target"
3191
3192         mv $f3 $f2 || error "mv $f3 $f2 failed"
3193
3194         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3195
3196         target=0x3
3197         [[ $flags == $target ]] ||
3198                 error "Changelog flag is $flags not $target"
3199
3200         cleanup
3201 }
3202 run_test 226 "changelog for last rm/mv with exiting archive"
3203
3204 check_flags_changes() {
3205         local f=$1
3206         local fid=$2
3207         local hsm_flag=$3
3208         local fst=$4
3209         local cnt=$5
3210
3211         local target=0x280
3212         $LFS hsm_set --$hsm_flag $f ||
3213                 error "Cannot set $hsm_flag on $f"
3214         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3215         local seen=${#flags[*]}
3216         cnt=$((fst + cnt))
3217         [[ $seen == $cnt ]] ||
3218                 error "set $hsm_flag: Changelog events $seen != $cnt"
3219         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3220                 error "set $hsm_flag: Changelog flags are "\
3221                         "${flags[$((cnt - 1))]} not $target"
3222
3223         $LFS hsm_clear --$hsm_flag $f ||
3224                 error "Cannot clear $hsm_flag on $f"
3225         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3226         seen=${#flags[*]}
3227         cnt=$(($cnt + 1))
3228         [[ $cnt == $seen ]] ||
3229                 error "clear $hsm_flag: Changelog events $seen != $cnt"
3230
3231         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3232                 error "clear $hsm_flag: Changelog flag is "\
3233                         "${flags[$((cnt - 1))]} not $target"
3234 }
3235
3236 test_227() {
3237         # test needs a running copytool
3238         copytool_setup
3239         changelog_setup
3240
3241         mkdir -p $DIR/$tdir
3242         typeset -a flags
3243
3244         for i in norelease noarchive exists archived
3245         do
3246                 local f=$DIR/$tdir/$tfile-$i
3247                 local fid=$(copy_file /etc/passwd $f)
3248                 check_flags_changes $f $fid $i 0 1
3249         done
3250
3251         f=$DIR/$tdir/$tfile---lost
3252         fid=$(copy_file /etc/passwd $f)
3253         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3254         wait_request_state $fid ARCHIVE SUCCEED
3255         check_flags_changes $f $fid lost 3 1
3256
3257         cleanup
3258 }
3259 run_test 227 "changelog when explicit setting of HSM flags"
3260
3261 test_228() {
3262         # test needs a running copytool
3263         copytool_setup
3264
3265         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=1 conv=sync ||
3266                 error "creating $DIR/$tfile"
3267         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
3268         wait_request_state $(path2fid $DIR/$tfile) ARCHIVE SUCCEED
3269
3270         $LFS hsm_release $DIR/$tfile
3271         check_hsm_flags $DIR/$tfile "0x0000000d"
3272
3273         filefrag $DIR/$tfile | grep " 1 extent found" ||
3274                 error "filefrag on released file must return only one extent"
3275
3276         # only newer versions of cp detect sparse files by stat/FIEMAP
3277         # (LU-2580)
3278         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
3279                 error "copying $DIR/$tfile"
3280         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
3281
3282         $LFS hsm_release $DIR/$tfile
3283         check_hsm_flags $DIR/$tfile "0x0000000d"
3284
3285         mkdir $DIR/$tdir
3286
3287         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
3288                 error "tar failed"
3289         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
3290                 error "comparing untarred $DIR/$tfile"
3291
3292         copytool_cleanup
3293 }
3294 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
3295
3296 test_250() {
3297         # test needs a running copytool
3298         copytool_setup
3299
3300         mkdir -p $DIR/$tdir
3301         local maxrequest=$(get_hsm_param max_requests)
3302         local rqcnt=$(($maxrequest * 3))
3303         local i=""
3304
3305         cdt_disable
3306         for i in $(seq -w 1 $rqcnt); do
3307                 rm -f $DIR/$tdir/$i
3308                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
3309         done
3310         # we do it in 2 steps, so all requests arrive at the same time
3311         for i in $(seq -w 1 $rqcnt); do
3312                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3313         done
3314         cdt_enable
3315         local cnt=$rqcnt
3316         local wt=$rqcnt
3317         while [[ $cnt != 0 || $wt != 0 ]]; do
3318                 sleep 1
3319                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3320                         $HSM_PARAM.actions |\
3321                         grep STARTED | grep -v CANCEL | wc -l")
3322                 [[ $cnt -le $maxrequest ]] ||
3323                         error "$cnt > $maxrequest too many started requests"
3324                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
3325                         $HSM_PARAM.actions |\
3326                         grep WAITING | wc -l")
3327                 echo "max=$maxrequest started=$cnt waiting=$wt"
3328         done
3329
3330         copytool_cleanup
3331 }
3332 run_test 250 "Coordinator max request"
3333
3334 test_251() {
3335         # test needs a running copytool
3336         copytool_setup
3337
3338         mkdir -p $DIR/$tdir
3339         local f=$DIR/$tdir/$tfile
3340         local fid=$(make_large_for_cancel $f)
3341
3342         cdt_disable
3343         # to have a short test
3344         local old_to=$(get_hsm_param active_request_timeout)
3345         set_hsm_param active_request_timeout 4
3346         # to be sure the cdt will wake up frequently so
3347         # it will be able to cancel the "old" request
3348         local old_loop=$(get_hsm_param loop_period)
3349         set_hsm_param loop_period 2
3350         cdt_enable
3351
3352         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3353         wait_request_state $fid ARCHIVE STARTED
3354         sleep 5
3355         wait_request_state $fid ARCHIVE CANCELED
3356
3357         set_hsm_param active_request_timeout $old_to
3358         set_hsm_param loop_period $old_loop
3359
3360         copytool_cleanup
3361 }
3362 run_test 251 "Coordinator request timeout"
3363
3364 test_300() {
3365         # the only way to test ondisk conf is to restart MDS ...
3366         echo "Stop coordinator and remove coordinator state at mount"
3367         # stop coordinator
3368         cdt_shutdown
3369         # clean on disk conf set by default
3370         cdt_clear_mount_state
3371         cdt_check_state stopped
3372
3373         # check cdt still off after umount/remount
3374         fail $SINGLEMDS
3375         cdt_check_state stopped
3376
3377         echo "Set coordinator start at mount, and start coordinator"
3378         cdt_set_mount_state enabled
3379
3380         # check cdt is on
3381         cdt_check_state enabled
3382
3383         # check cdt still on after umount/remount
3384         fail $SINGLEMDS
3385         cdt_check_state enabled
3386
3387         # we are back to original state (cdt started at mount)
3388 }
3389 run_test 300 "On disk coordinator state kept between MDT umount/mount"
3390
3391 test_301() {
3392         local ai=$(get_hsm_param default_archive_id)
3393         local new=$((ai + 1))
3394
3395         set_hsm_param default_archive_id $new -P
3396         fail $SINGLEMDS
3397         local res=$(get_hsm_param default_archive_id)
3398
3399         # clear value
3400         set_hsm_param default_archive_id "" "-P -d"
3401
3402         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3403 }
3404 run_test 301 "HSM tunnable are persistent"
3405
3406 test_302() {
3407         local ai=$(get_hsm_param default_archive_id)
3408         local new=$((ai + 1))
3409
3410         # stop coordinator
3411         cdt_shutdown
3412
3413         set_hsm_param default_archive_id $new -P
3414
3415         local mdtno
3416         for mdtno in $(seq 1 $MDSCOUNT); do
3417                 fail mds${mdtno}
3418         done
3419
3420         # check cdt is on
3421         cdt_check_state enabled
3422
3423         local res=$(get_hsm_param default_archive_id)
3424
3425         # clear value
3426         set_hsm_param default_archive_id "" "-P -d"
3427
3428         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3429 }
3430 run_test 302 "HSM tunnable are persistent when CDT is off"
3431
3432 test_400() {
3433         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3434
3435         copytool_setup
3436
3437         mkdir -p $DIR/$tdir
3438
3439         local dir_mdt0=$DIR/$tdir/mdt0
3440         local dir_mdt1=$DIR/$tdir/mdt1
3441
3442         # create 1 dir per MDT
3443         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3444         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3445
3446         # create 1 file in each MDT
3447         local fid1=$(make_small $dir_mdt0/$tfile)
3448         local fid2=$(make_small $dir_mdt1/$tfile)
3449
3450         # check that hsm request on mdt0 is sent to the right MDS
3451         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3452         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3453                 echo "archive successful on mdt0"
3454
3455         # check that hsm request on mdt1 is sent to the right MDS
3456         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
3457         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3458                 echo "archive successful on mdt1"
3459
3460         copytool_cleanup
3461         # clean test files and directories
3462         rm -rf $dir_mdt0 $dir_mdt1
3463 }
3464 run_test 400 "Single request is sent to the right MDT"
3465
3466 test_401() {
3467         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3468
3469         copytool_setup
3470
3471         mkdir -p $DIR/$tdir
3472
3473         local dir_mdt0=$DIR/$tdir/mdt0
3474         local dir_mdt1=$DIR/$tdir/mdt1
3475
3476         # create 1 dir per MDT
3477         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3478         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3479
3480         # create 1 file in each MDT
3481         local fid1=$(make_small $dir_mdt0/$tfile)
3482         local fid2=$(make_small $dir_mdt1/$tfile)
3483
3484         # check that compound requests are shunt to the rights MDTs
3485         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
3486                 error "lfs hsm_archive"
3487         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3488                 echo "archive successful on mdt0"
3489         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3490                 echo "archive successful on mdt1"
3491
3492         copytool_cleanup
3493         # clean test files and directories
3494         rm -rf $dir_mdt0 $dir_mdt1
3495 }
3496 run_test 401 "Compound requests split and sent to their respective MDTs"
3497
3498 mdc_change_state() # facet, MDT_pattern, activate|deactivate
3499 {
3500         local facet=$1
3501         local pattern="$2"
3502         local state=$3
3503         local node=$(facet_active_host $facet)
3504         local mdc
3505         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
3506                         awk '{print $4}'); do
3507                 echo "$3 $mdc on $node"
3508                 do_facet $facet "$LCTL --device $mdc $state" || return 1
3509         done
3510 }
3511
3512 test_402() {
3513         # make sure there is no running copytool
3514         copytool_cleanup
3515
3516         # deactivate all mdc on agent1
3517         mdc_change_state $SINGLEAGT "MDT000." "deactivate"
3518
3519         copytool_setup $SINGLEAGT
3520
3521         check_agent_unregistered "uuid" # match any agent
3522
3523         # no expected running copytool
3524         search_copytools $agent && error "Copytool start should have failed"
3525
3526         # reactivate MDCs
3527         mdc_change_state $SINGLEAGT "MDT000." "activate"
3528 }
3529 run_test 402 "Copytool start fails if all MDTs are inactive"
3530
3531 test_403() {
3532         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3533
3534         # make sure there is no running copytool
3535         copytool_cleanup
3536
3537         local agent=$(facet_active_host $SINGLEAGT)
3538         local uuid=$(do_rpc_nodes $agent get_client_uuid | cut -d' ' -f2)
3539
3540         # deactivate all mdc for MDT0001
3541         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3542
3543         copytool_setup
3544         # check the agent is registered on MDT0000, and not on MDT0001
3545         check_agent_registered_by_mdt $uuid 0
3546         check_agent_unregistered_by_mdt $uuid 1
3547
3548         # check running copytool process
3549         search_copytools $agent || error "No running copytools on $agent"
3550
3551         # reactivate all mdc for MDT0001
3552         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3553
3554         # make sure the copytool is now registered to all MDTs
3555         check_agent_registered $uuid
3556
3557         copytool_cleanup
3558 }
3559 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
3560
3561 test_404() {
3562         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3563
3564         copytool_setup
3565
3566         # create files on both MDT0000 and MDT0001
3567         mkdir -p $DIR/$tdir
3568
3569         local dir_mdt0=$DIR/$tdir/mdt0
3570         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3571
3572         # create 1 file on mdt0
3573         local fid1=$(make_small $dir_mdt0/$tfile)
3574
3575         # deactivate all mdc for MDT0001
3576         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3577
3578         # send an HSM request for files in MDT0000
3579         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3580
3581         # check for completion of files in MDT0000
3582         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3583                 echo "archive successful on mdt0"
3584
3585         # reactivate all mdc for MDT0001
3586         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3587
3588         copytool_cleanup
3589         # clean test files and directories
3590         rm -rf $dir_mdt0
3591 }
3592 run_test 404 "Inactive MDT does not block requests for active MDTs"
3593
3594 copytool_cleanup
3595
3596 complete $SECONDS
3597 check_and_cleanup_lustre
3598 exit_status