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