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