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