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