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