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