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