Whamcloud - gitweb
8687a612559bff7214971abc0842befef1042db2
[fs/lustre-release.git] / lustre / tests / sanity-hsm.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # exit on error
7 set -e
8 set +o monitor
9
10 SRCDIR=$(dirname $0)
11 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin
12
13 ONLY=${ONLY:-"$*"}
14 # bug number for skipped test:
15 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
16 # skip test cases failed before landing - Jinshan
17
18 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT 31a 34 35 36"
19 ALWAYS_EXCEPT="$ALWAYS_EXCEPT 200 201 221 223a 223b 225"
20
21 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
22
23 . $LUSTRE/tests/test-framework.sh
24 init_test_env $@
25 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
26 init_logging
27
28 MULTIOP=${MULTIOP:-multiop}
29 OPENFILE=${OPENFILE:-openfile}
30 MCREATE=${MCREATE:-mcreate}
31 MOUNT_2=${MOUNT_2:-"yes"}
32 FAIL_ON_ERROR=false
33
34 if [[ $MDSCOUNT -ge 2 ]]; then
35         skip_env "Only run with single MDT for now" && exit
36 fi
37
38 check_and_setup_lustre
39
40 if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ]]; then
41         skip_env "Need MDS version at least 2.4.53" && exit
42 fi
43
44 # $RUNAS_ID may get set incorrectly somewhere else
45 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
46         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!" && exit
47 fi
48 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
49
50 build_test_filter
51
52 #
53 # In order to test multiple remote HSM agents, a new facet type named "AGT" and
54 # the following associated variables are added:
55 #
56 # AGTCOUNT: number of agents
57 # AGTDEV{N}: target HSM mount point (root path of the backend)
58 # agt{N}_HOST: hostname of the agent agt{N}
59 # SINGLEAGT: facet of the single agent
60 #
61 # The number of agents is initialized as the number of remote client nodes.
62 # By default, only single copytool is started on a remote client/agent. If there
63 # was no remote client, then the copytool will be started on the local client.
64 #
65 init_agt_vars() {
66         local n
67         local agent
68
69         export AGTCOUNT=${AGTCOUNT:-$((CLIENTCOUNT - 1))}
70         [[ $AGTCOUNT -gt 0 ]] || AGTCOUNT=1
71
72         export SHARED_DIRECTORY=${SHARED_DIRECTORY:-$TMP}
73         if [[ $CLIENTCOUNT -gt 1 ]] &&
74                 ! check_shared_dir $SHARED_DIRECTORY $CLIENTS; then
75                 skip_env "SHARED_DIRECTORY should be accessible"\
76                          "on all client nodes"
77                 exit 0
78         fi
79
80         for n in $(seq $AGTCOUNT); do
81                 eval export AGTDEV$n=\$\{AGTDEV$n:-"$SHARED_DIRECTORY/arc$n"\}
82                 agent=CLIENT$((n + 1))
83                 if [[ -z "${!agent}" ]]; then
84                         [[ $CLIENTCOUNT -eq 1 ]] && agent=CLIENT1 ||
85                                 agent=CLIENT2
86                 fi
87                 eval export agt${n}_HOST=\$\{agt${n}_HOST:-${!agent}\}
88         done
89
90         export SINGLEAGT=${SINGLEAGT:-agt1}
91
92         export HSMTOOL=${HSMTOOL:-"lhsmtool_posix"}
93         export HSMTOOL_VERBOSE=${HSMTOOL_VERBOSE:-""}
94         export HSMTOOL_BASE=$(basename "$HSMTOOL" | cut -f1 -d" ")
95         HSM_ARCHIVE=$(copytool_device $SINGLEAGT)
96         HSM_ARCHIVE_NUMBER=2
97
98         MDT_PARAM="mdt.$FSNAME-MDT0000"
99         HSM_PARAM="$MDT_PARAM.hsm"
100
101         # archive is purged at copytool setup
102         HSM_ARCHIVE_PURGE=true
103 }
104
105 # Get the backend root path for the given agent facet.
106 copytool_device() {
107         local facet=$1
108         local dev=AGTDEV$(facet_number $facet)
109
110         echo -n ${!dev}
111 }
112
113 # Stop copytool and unregister an existing changelog user.
114 cleanup() {
115         copytool_cleanup
116         changelog_cleanup
117         cdt_set_sanity_policy
118 }
119
120 search_and_kill_copytool() {
121         local agents=${1:-$(facet_active_host $SINGLEAGT)}
122
123         echo "Killing existing copytools on $agents"
124         do_nodesv $agents "killall -q $HSMTOOL_BASE" || true
125 }
126
127 copytool_setup() {
128         local facet=${1:-$SINGLEAGT}
129         local lustre_mntpnt=${2:-$MOUNT}
130         local arc_id=$3
131         local hsm_root=$(copytool_device $facet)
132         local agent=$(facet_active_host $facet)
133
134         if [[ -z "$arc_id" ]] &&
135                 do_facet $facet "pkill -CONT -x $HSMTOOL_BASE"; then
136                         echo "Wakeup copytool $facet on $agent"
137                         return 0
138         fi
139
140         if $HSM_ARCHIVE_PURGE; then
141                 echo "Purging archive on $agent"
142                 do_facet $facet "rm -rf $hsm_root/*"
143         fi
144
145         echo "Starting copytool $facet on $agent"
146         do_facet $facet "mkdir -p $hsm_root" || error "mkdir '$hsm_root' failed"
147         # bandwidth is limited to 1MB/s so the copy time is known and
148         # independent of hardware
149         local cmd="$HSMTOOL $HSMTOOL_VERBOSE --daemon --hsm-root $hsm_root"
150         [[ -z "$arc_id" ]] || cmd+=" --archive $arc_id"
151         cmd+=" --bandwidth 1 $lustre_mntpnt"
152
153         # Redirect the standard output and error to a log file which
154         # can be uploaded to Maloo.
155         local prefix=$TESTLOG_PREFIX
156         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
157         local copytool_log=$prefix.copytool${arc_id}_log.$agent.log
158
159         do_facet $facet "$cmd < /dev/null > $copytool_log 2>&1" ||
160                 error "start copytool $facet on $agent failed"
161         trap cleanup EXIT
162 }
163
164 copytool_cleanup() {
165         trap - EXIT
166         local agents=${1:-$(facet_active_host $SINGLEAGT)}
167
168         do_nodesv $agents "pkill -INT -x $HSMTOOL_BASE" || return 0
169         sleep 1
170         echo "Copytool is stopped on $agents"
171 }
172
173 copytool_suspend() {
174         local agents=${1:-$(facet_active_host $SINGLEAGT)}
175
176         do_nodesv $agents "pkill -STOP -x $HSMTOOL_BASE" || return 0
177         echo "Copytool is suspended on $agents"
178 }
179
180 copytool_remove_backend() {
181         local fid=$1
182         local be=$(find $HSM_ARCHIVE -name $fid)
183         echo "Remove from backend: $fid = $be"
184         do_facet $SINGLEAGT rm -f $be
185 }
186
187 import_file() {
188         do_facet $SINGLEAGT \
189                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
190                 --import $1 $2 $MOUNT" ||
191                 error "import of $1 to $2 failed"
192 }
193
194 make_archive() {
195         local file=$HSM_ARCHIVE/$1
196         do_facet $SINGLEAGT mkdir -p $(dirname $file)
197         do_facet $SINGLEAGT dd if=/dev/urandom of=$file count=32 bs=1000000 ||
198                 error "cannot create $file"
199 }
200
201 copy2archive() {
202         local file=$HSM_ARCHIVE/$2
203         do_facet $SINGLEAGT mkdir -p $(dirname $file)
204         do_facet $SINGLEAGT cp -p $1 $file || error "cannot copy $1 to $file"
205 }
206
207 changelog_setup() {
208         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0\
209                   changelog_register -n)
210         do_facet $SINGLEMDS lctl set_param mdd.$MDT0.changelog_mask="+hsm"
211         $LFS changelog_clear $MDT0 $CL_USER 0
212 }
213
214 changelog_cleanup() {
215 #       $LFS changelog $MDT0
216         [[ -n "$CL_USER" ]] || return 0
217
218         $LFS changelog_clear $MDT0 $CL_USER 0
219         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
220         CL_USER=
221 }
222
223 changelog_get_flags() {
224         local mdt=$1
225         local cltype=$2
226         local fid=$3
227
228         $LFS changelog $mdt | awk "/$cltype/ && /t=\[$fid\]/ {print \$5}"
229 }
230
231 get_hsm_param() {
232         local param=$1
233         local val=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.$param)
234         echo $val
235 }
236
237 set_hsm_param() {
238         local param=$1
239         local value=$2
240         local opt=$3
241         if [[ "$value" != "" ]]; then
242                 value="=$value"
243         fi
244         do_facet $SINGLEMDS $LCTL set_param $opt -n $HSM_PARAM.$param$value
245         return $?
246 }
247
248 set_test_state() {
249         local cmd=$1
250         local target=$2
251         do_facet $SINGLEMDS $LCTL set_param $MDT_PARAM.hsm_control=$cmd
252         wait_result $SINGLEMDS "$LCTL get_param -n $MDT_PARAM.hsm_control"\
253                 $target 10 || error "cdt state is not $target"
254 }
255
256 cdt_set_sanity_policy() {
257         if [[ "$CDT_POLICY_HAD_CHANGED" ]]
258         then
259                 # clear all
260                 do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=+NRA
261                 do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=-NBR
262                 CDT_POLICY_HAD_CHANGED=
263         fi
264 }
265
266 cdt_set_no_retry() {
267         do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=+NRA
268         CDT_POLICY_HAD_CHANGED=true
269 }
270
271 cdt_clear_no_retry() {
272         do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=-NRA
273         CDT_POLICY_HAD_CHANGED=true
274 }
275
276 cdt_set_non_blocking_restore() {
277         do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=+NBR
278         CDT_POLICY_HAD_CHANGED=true
279 }
280
281 cdt_clear_non_blocking_restore() {
282         do_facet $SINGLEMDS $LCTL set_param $HSM_PARAM.policy=-NBR
283         CDT_POLICY_HAD_CHANGED=true
284 }
285
286 cdt_clear_mount_state() {
287         do_facet $SINGLEMDS $LCTL set_param -d -P $MDT_PARAM.hsm_control
288 }
289
290 cdt_set_mount_state() {
291         do_facet $SINGLEMDS $LCTL set_param -P $MDT_PARAM.hsm_control=$1
292 }
293
294 cdt_check_state() {
295         local target=$1
296         wait_result $SINGLEMDS\
297                 "$LCTL get_param -n $MDT_PARAM.hsm_control" "$target" 20 ||
298                         error "cdt state is not $target"
299 }
300
301 cdt_disable() {
302         set_test_state disabled disabled
303 }
304
305 cdt_enable() {
306         set_test_state enabled enabled
307 }
308
309 cdt_shutdown() {
310         set_test_state shutdown stopped
311 }
312
313 cdt_purge() {
314         set_test_state purge enabled
315 }
316
317 cdt_restart() {
318         cdt_shutdown
319         cdt_enable
320         cdt_set_sanity_policy
321 }
322
323 needclients() {
324         local client_count=$1
325         if [[ $CLIENTCOUNT -lt $client_count ]]; then
326                 skip "Need $client_count or more clients, have $CLIENTCOUNT"
327                 return 1
328         fi
329         return 0
330 }
331
332 path2fid() {
333         $LFS path2fid $1 | tr -d '[]'
334 }
335
336 get_hsm_flags() {
337         local f=$1
338         local u=$2
339
340         if [[ $u == "user" ]]; then
341                 local st=$($RUNAS $LFS hsm_state $f)
342         else
343                 local st=$($LFS hsm_state $f)
344                 u=root
345         fi
346
347         [[ $? == 0 ]] || error "$LFS hsm_state $f failed (run as $u)"
348
349         st=$(echo $st | cut -f 2 -d" " | tr -d "()," )
350         echo $st
351 }
352
353 get_hsm_archive_id() {
354         local f=$1
355         local st=$($LFS hsm_state $f)
356         [[ $? == 0 ]] || error "$LFS hsm_state $f failed"
357
358         local ar=$(echo $st | grep "archive_id" | cut -f5 -d" " |
359                    cut -f2 -d:)
360         echo $ar
361 }
362
363 check_hsm_flags() {
364         local f=$1
365         local fl=$2
366
367         local st=$(get_hsm_flags $f)
368         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
369 }
370
371 check_hsm_flags_user() {
372         local f=$1
373         local fl=$2
374
375         local st=$(get_hsm_flags $f user)
376         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
377 }
378
379 copy_file() {
380         local f=
381
382         if [[ -d $2 ]]; then
383                 f=$2/$(basename $1)
384         else
385                 f=$2
386         fi
387
388         if [[ "$3" != 1 ]]; then
389                 f=${f/$DIR/$DIR2}
390         fi
391         rm -f $f
392         cp $1 $f || error "cannot copy $1 to $f"
393         path2fid $f || error "cannot get fid on $f"
394 }
395
396 make_small() {
397         local file2=${1/$DIR/$DIR2}
398         dd if=/dev/urandom of=$file2 count=2 bs=1M conv=fsync ||
399                 error "cannot create $file2"
400         path2fid $1 || error "cannot get fid on $1"
401 }
402
403 cleanup_large_files() {
404         local ratio=$(df $MOUNT |awk '{print $5}' |sed 's/%//g' |grep -v Use)
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.agent_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.agent_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.agent_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.agent_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=$(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 test_25a() {
1513         # test needs a running copytool
1514         copytool_setup
1515
1516         mkdir -p $DIR/$tdir
1517         copy2archive /etc/hosts $tdir/$tfile
1518
1519         local f=$DIR/$tdir/$tfile
1520
1521         import_file $tdir/$tfile $f
1522
1523         $LFS hsm_set --lost $f
1524
1525         md5sum $f
1526         local st=$?
1527
1528         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1529
1530         copytool_cleanup
1531 }
1532 run_test 25a "Restore lost file (HS_LOST flag) from import"\
1533              " (Operation not permitted)"
1534
1535 test_25b() {
1536         # test needs a running copytool
1537         copytool_setup
1538
1539         mkdir -p $DIR/$tdir
1540
1541         local f=$DIR/$tdir/$tfile
1542         local fid=$(copy_file /etc/passwd $f)
1543
1544         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1545         wait_request_state $fid ARCHIVE SUCCEED
1546
1547         $LFS hsm_release $f
1548         $LFS hsm_set --lost $f
1549         md5sum $f
1550         st=$?
1551
1552         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1553
1554         copytool_cleanup
1555 }
1556 run_test 25b "Restore lost file (HS_LOST flag) after release"\
1557              " (Operation not permitted)"
1558
1559 test_26() {
1560         # test needs a running copytool
1561         copytool_setup
1562
1563         mkdir -p $DIR/$tdir
1564         local f=$DIR/$tdir/$tfile
1565         local fid=$(make_large_for_progress $f)
1566         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1567         wait_request_state $fid ARCHIVE SUCCEED
1568
1569         $LFS hsm_remove $f
1570         wait_request_state $fid REMOVE SUCCEED
1571
1572         check_hsm_flags $f "0x00000000"
1573
1574         copytool_cleanup
1575 }
1576 run_test 26 "Remove the archive of a valid file"
1577
1578 test_27a() {
1579         # test needs a running copytool
1580         copytool_setup
1581
1582         mkdir -p $DIR/$tdir
1583         make_archive $tdir/$tfile
1584         local f=$DIR/$tdir/$tfile
1585         import_file $tdir/$tfile $f
1586         local fid=$(path2fid $f)
1587
1588         $LFS hsm_remove $f
1589
1590         [[ $? != 0 ]] || error "Remove of a released file should fail"
1591
1592         copytool_cleanup
1593 }
1594 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
1595
1596 test_27b() {
1597         # test needs a running copytool
1598         copytool_setup
1599
1600         mkdir -p $DIR/$tdir
1601         local f=$DIR/$tdir/$tfile
1602         local fid=$(make_large_for_progress $f)
1603         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1604         wait_request_state $fid ARCHIVE SUCCEED
1605         $LFS hsm_release $f
1606
1607         $LFS hsm_remove $f
1608
1609         [[ $? != 0 ]] || error "Remove of a released file should fail"
1610
1611         copytool_cleanup
1612 }
1613 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
1614
1615 test_28() {
1616         # test needs a running copytool
1617         copytool_setup
1618
1619         mkdir -p $DIR/$tdir
1620         local f=$DIR/$tdir/$tfile
1621         local fid=$(make_large_for_progress $f)
1622         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1623         wait_request_state $fid ARCHIVE SUCCEED
1624
1625         cdt_disable
1626         $LFS hsm_remove $f
1627
1628         rm -f $f
1629
1630         cdt_enable
1631
1632         wait_request_state $fid REMOVE SUCCEED
1633
1634         copytool_cleanup
1635 }
1636 run_test 28 "Concurrent archive/file remove"
1637
1638 test_30a() {
1639         # restore at exec cannot work on agent node (because of Linux kernel
1640         # protection of executables)
1641         needclients 2 || return 0
1642
1643         # test needs a running copytool
1644         copytool_setup
1645
1646         mkdir -p $DIR/$tdir
1647         copy2archive /bin/true $tdir/$tfile
1648
1649         local f=$DIR/$tdir/true
1650         import_file $tdir/$tfile $f
1651
1652         local fid=$(path2fid $f)
1653
1654         # set no retry action mode
1655         cdt_set_no_retry
1656         do_node $CLIENT2 $f
1657         local st=$?
1658
1659         # cleanup
1660         # remove no try action mode
1661         cdt_clear_no_retry
1662         $LFS hsm_state $f
1663
1664         [[ $st == 0 ]] || error "Failed to exec a released file"
1665
1666         copytool_cleanup
1667 }
1668 run_test 30a "Restore at exec (import case)"
1669
1670 test_30b() {
1671         # restore at exec cannot work on agent node (because of Linux kernel
1672         # protection of executables)
1673         needclients 2 || return 0
1674
1675         # test needs a running copytool
1676         copytool_setup
1677
1678         mkdir -p $DIR/$tdir
1679         local f=$DIR/$tdir/true
1680         local fid=$(copy_file /bin/true $f)
1681         chmod 755 $f
1682         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1683         wait_request_state $fid ARCHIVE SUCCEED
1684         $LFS hsm_release $f
1685         $LFS hsm_state $f
1686         # set no retry action mode
1687         cdt_set_no_retry
1688         do_node $CLIENT2 $f
1689         local st=$?
1690
1691         # cleanup
1692         # remove no try action mode
1693         cdt_clear_no_retry
1694         $LFS hsm_state $f
1695
1696         [[ $st == 0 ]] || error "Failed to exec a released file"
1697
1698         copytool_cleanup
1699 }
1700 run_test 30b "Restore at exec (release case)"
1701
1702 restore_and_check_size() {
1703         local f=$1
1704         local fid=$2
1705         local s=$(stat -c "%s" $f)
1706         local n=$s
1707         local st=$(get_hsm_flags $f)
1708         local err=0
1709         local cpt=0
1710         $LFS hsm_restore $f
1711         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
1712         do
1713                 n=$(stat -c "%s" $f)
1714                 # we echo in both cases to show stat is not
1715                 # hang
1716                 if [[ $n != $s ]]; then
1717                         echo "size seen is $n != $s"
1718                         err=1
1719                 else
1720                         echo "size seen is right: $n == $s"
1721                 fi
1722                 st=$(get_hsm_flags $f)
1723                 sleep 10
1724                 cpt=$((cpt + 1))
1725         done
1726         if [[ $cpt -lt 10 ]]; then
1727                 echo " restore is too long"
1728         else
1729                 echo " "done
1730         fi
1731         wait_request_state $fid RESTORE SUCCEED
1732         return $err
1733 }
1734
1735 test_31a() {
1736         # test needs a running copytool
1737         copytool_setup
1738
1739         mkdir -p $DIR/$tdir
1740
1741         make_archive $tdir/$tfile
1742         local f=$DIR/$tdir/$tfile
1743         import_file $tdir/$tfile $f
1744         local fid=$($LFS path2fid $f)
1745         HSM_ARCHIVE_PURGE=false copytool_setup
1746
1747         restore_and_check_size $f $fid
1748         local err=$?
1749
1750         [[ $err -eq 0 ]] || error "File size changed during restore"
1751
1752         copytool_cleanup
1753 }
1754 run_test 31a "Import a large file and check size during restore"
1755
1756
1757 test_31b() {
1758         # test needs a running copytool
1759         copytool_setup
1760
1761         mkdir -p $DIR/$tdir
1762
1763         local f=$DIR/$tdir/$tfile
1764         local fid=$(make_large_for_progress $f)
1765         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1766         wait_request_state $fid ARCHIVE SUCCEED
1767         $LFS hsm_release $f
1768
1769         restore_and_check_size $f $fid
1770         local err=$?
1771
1772         [[ $err -eq 0 ]] || error "File size changed during restore"
1773
1774         copytool_cleanup
1775 }
1776 run_test 31b "Restore a large unaligned file and check size during restore"
1777
1778 test_31c() {
1779         # test needs a running copytool
1780         copytool_setup
1781
1782         mkdir -p $DIR/$tdir
1783
1784         local f=$DIR/$tdir/$tfile
1785         local fid=$(make_large_for_progress_aligned $f)
1786         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1787         wait_request_state $fid ARCHIVE SUCCEED
1788         $LFS hsm_release $f
1789
1790         restore_and_check_size $f $fid
1791         local err=$?
1792
1793         [[ $err -eq 0 ]] || error "File size changed during restore"
1794
1795         copytool_cleanup
1796 }
1797 run_test 31c "Restore a large aligned file and check size during restore"
1798
1799 test_33() {
1800         # test needs a running copytool
1801         copytool_setup
1802
1803         mkdir -p $DIR/$tdir
1804
1805         local f=$DIR/$tdir/$tfile
1806         local fid=$(make_large_for_progress $f)
1807         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1808         wait_request_state $fid ARCHIVE SUCCEED
1809         $LFS hsm_release $f
1810
1811         md5sum $f >/dev/null &
1812         local pid=$!
1813         wait_request_state $fid RESTORE STARTED
1814
1815         kill -15 $pid
1816         sleep 1
1817
1818         # Check restore trigger process was killed
1819         local killed=$(ps -o pid,comm hp $pid >/dev/null)
1820
1821         $LFS hsm_cancel $f
1822
1823         wait_request_state $fid RESTORE CANCELED
1824         wait_request_state $fid CANCEL SUCCEED
1825
1826         [ -z $killed ] ||
1827                 error "Cannot kill process waiting for restore ($killed)"
1828
1829         copytool_cleanup
1830 }
1831 run_test 33 "Kill a restore waiting process"
1832
1833 test_34() {
1834         # test needs a running copytool
1835         copytool_setup
1836
1837         mkdir -p $DIR/$tdir
1838
1839         local f=$DIR/$tdir/$tfile
1840         local fid=$(make_large_for_progress $f)
1841         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1842         wait_request_state $fid ARCHIVE SUCCEED
1843         $LFS hsm_release $f
1844
1845         md5sum $f >/dev/null &
1846         local pid=$!
1847         wait_request_state $fid RESTORE STARTED
1848
1849         rm $f || error "rm $f failed"
1850         # rm must not block during restore
1851         wait_request_state $fid RESTORE STARTED
1852
1853         wait_request_state $fid RESTORE SUCCEED
1854         # check md5sum pgm finished
1855         local there=$(ps -o pid,comm hp $pid >/dev/null)
1856         [[ -z $there ]] || error "Restore initiator does not exit"
1857
1858         local rc=$(wait $pid)
1859         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
1860
1861         copytool_cleanup
1862 }
1863 run_test 34 "Remove file during restore"
1864
1865 test_35() {
1866         # test needs a running copytool
1867         copytool_setup
1868
1869         mkdir -p $DIR/$tdir
1870
1871         local f=$DIR/$tdir/$tfile
1872         local f1=$DIR/$tdir/$tfile-1
1873         local fid=$(make_large_for_progress $f)
1874         local fid1=$(copy_file /etc/passwd $f1)
1875         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1876         wait_request_state $fid ARCHIVE SUCCEED
1877         $LFS hsm_release $f
1878
1879         md5sum $f >/dev/null &
1880         local pid=$!
1881         wait_request_state $fid RESTORE STARTED
1882
1883         mv $f1 $f || error "mv $f1 $f failed"
1884         # mv must not block during restore
1885         wait_request_state $fid RESTORE STARTED
1886
1887         wait_request_state $fid RESTORE SUCCEED
1888         # check md5sum pgm finished
1889         local there=$(ps -o pid,comm hp $pid >/dev/null)
1890         [[ -z $there ]] || error "Restore initiator does not exit"
1891
1892         local rc=$(wait $pid)
1893         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
1894
1895         fid2=$(path2fid $f)
1896         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
1897
1898         copytool_cleanup
1899 }
1900 run_test 35 "Overwrite file during restore"
1901
1902 test_36() {
1903         # test needs a running copytool
1904         copytool_setup
1905
1906         mkdir -p $DIR/$tdir
1907
1908         local f=$DIR/$tdir/$tfile
1909         local fid=$(make_large_for_progress $f)
1910         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1911         wait_request_state $fid ARCHIVE SUCCEED
1912         $LFS hsm_release $f
1913
1914         md5sum $f >/dev/null &
1915         local pid=$!
1916         wait_request_state $fid RESTORE STARTED
1917
1918         mv $f $f.new
1919         # rm must not block during restore
1920         wait_request_state $fid RESTORE STARTED
1921
1922         wait_request_state $fid RESTORE SUCCEED
1923         # check md5sum pgm finished
1924         local there=$(ps -o pid,comm hp $pid >/dev/null)
1925         [[ -z $there ]] ||
1926                 error "Restore initiator does not exit"
1927
1928         local rc=$(wait $pid)
1929         [[ $rc -eq 0 ]] ||
1930                 error "Restore initiator failed with $rc"
1931
1932         copytool_cleanup
1933 }
1934 run_test 36 "Move file during restore"
1935
1936 multi_archive() {
1937         local prefix=$1
1938         local count=$2
1939         local n=""
1940
1941         for n in $(seq 1 $count); do
1942                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
1943         done
1944         echo "$count archive requests submitted"
1945 }
1946
1947 test_40() {
1948         local stream_count=4
1949         local file_count=100
1950         mkdir -p $DIR/$tdir
1951         local f=$DIR/$tdir/$tfile
1952         local i=""
1953         local p=""
1954         local fid=""
1955
1956         for i in $(seq 1 $file_count); do
1957                 for p in $(seq 1 $stream_count); do
1958                         fid=$(copy_file /etc/hosts $f.$p.$i)
1959                 done
1960         done
1961         copytool_setup
1962         # to be sure wait_all_done will not be mislead by previous tests
1963         cdt_purge
1964         wait_for_grace_delay
1965         typeset -a pids
1966         # start archive streams in background (archive files in parallel)
1967         for p in $(seq 1 $stream_count); do
1968                 multi_archive $f.$p $file_count &
1969                 pids[$p]=$!
1970         done
1971         echo -n  "Wait for all requests being enqueued..."
1972         wait ${pids[*]}
1973         echo OK
1974         wait_all_done 100
1975         copytool_cleanup
1976 }
1977 run_test 40 "Parallel archive requests"
1978
1979 test_52() {
1980         # test needs a running copytool
1981         copytool_setup
1982
1983         mkdir -p $DIR/$tdir
1984         local f=$DIR/$tdir/$tfile
1985         local fid=$(copy_file /etc/motd $f 1)
1986
1987         $LFS hsm_archive $f || error "could not archive file"
1988         wait_request_state $fid ARCHIVE SUCCEED
1989         check_hsm_flags $f "0x00000009"
1990
1991         multiop_bg_pause $f O_c || error "multiop failed"
1992         local MULTIPID=$!
1993
1994         mds_evict_client
1995         client_up || client_up || true
1996
1997         kill -USR1 $MULTIPID
1998         wait $MULTIPID || error "multiop close failed"
1999
2000         check_hsm_flags $f "0x0000000b"
2001
2002         copytool_cleanup
2003 }
2004 run_test 52 "Opened for write file on an evicted client should be set dirty"
2005
2006 test_53() {
2007         # test needs a running copytool
2008         copytool_setup
2009
2010         mkdir -p $DIR/$tdir
2011         local f=$DIR/$tdir/$tfile
2012         local fid=$(copy_file /etc/motd $f 1)
2013
2014         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2015                 error "could not archive file"
2016         wait_request_state $fid ARCHIVE SUCCEED
2017         check_hsm_flags $f "0x00000009"
2018
2019         multiop_bg_pause $f o_c || error "multiop failed"
2020         MULTIPID=$!
2021
2022         mds_evict_client
2023         client_up || client_up || true
2024
2025         kill -USR1 $MULTIPID
2026         wait $MULTIPID || error "multiop close failed"
2027
2028         check_hsm_flags $f "0x00000009"
2029
2030         copytool_cleanup
2031 }
2032 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2033
2034 test_54() {
2035         # test needs a running copytool
2036         copytool_setup
2037
2038         mkdir -p $DIR/$tdir
2039         local f=$DIR/$tdir/$tfile
2040         local fid=$(make_small $f)
2041
2042         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2043                 error "could not archive file"
2044         wait_request_state $fid ARCHIVE STARTED
2045
2046         check_hsm_flags $f "0x00000001"
2047
2048         # Avoid coordinator resending this request as soon it has failed.
2049         cdt_set_no_retry
2050
2051         echo "foo" >> $f
2052         sync
2053         wait_request_state $fid ARCHIVE FAILED
2054
2055         check_hsm_flags $f "0x00000003"
2056
2057         cdt_clear_no_retry
2058         copytool_cleanup
2059 }
2060 run_test 54 "Write during an archive cancels it"
2061
2062 test_55() {
2063         # test needs a running copytool
2064         copytool_setup
2065
2066         mkdir -p $DIR/$tdir
2067         local f=$DIR/$tdir/$tfile
2068         local fid=$(make_small $f)
2069
2070         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2071                 error "could not archive file"
2072         wait_request_state $fid ARCHIVE STARTED
2073
2074         check_hsm_flags $f "0x00000001"
2075
2076         # Avoid coordinator resending this request as soon it has failed.
2077         cdt_set_no_retry
2078
2079         $TRUNCATE $f 1024 || error "truncate failed"
2080         sync
2081         wait_request_state $fid ARCHIVE FAILED
2082
2083         check_hsm_flags $f "0x00000003"
2084
2085         cdt_clear_no_retry
2086         copytool_cleanup
2087 }
2088 run_test 55 "Truncate during an archive cancels it"
2089
2090 test_56() {
2091         # test needs a running copytool
2092         copytool_setup
2093
2094         mkdir -p $DIR/$tdir
2095         local f=$DIR/$tdir/$tfile
2096         local fid=$(make_large_for_progress $f)
2097
2098         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2099                 error "could not archive file"
2100         wait_request_state $fid ARCHIVE STARTED
2101
2102         check_hsm_flags $f "0x00000001"
2103
2104         # Change metadata and sync to be sure we are not changing only
2105         # in memory.
2106         chmod 644 $f
2107         chgrp sys $f
2108         sync
2109         wait_request_state $fid ARCHIVE SUCCEED
2110
2111         check_hsm_flags $f "0x00000009"
2112
2113         copytool_cleanup
2114 }
2115 run_test 56 "Setattr during an archive is ok"
2116
2117 test_57() {
2118         # Need one client for I/O, one for request
2119         needclients 2 || return 0
2120
2121         # test needs a running copytool
2122         copytool_setup
2123
2124         mkdir -p $DIR/$tdir
2125         local f=$DIR/$tdir/test_archive_remote
2126         # Create a file on a remote node
2127         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2128                 "count=2 conv=fsync"
2129
2130         # And archive it
2131         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2132                 error "hsm_archive failed"
2133         local fid=$(path2fid $f)
2134         wait_request_state $fid ARCHIVE SUCCEED
2135
2136         # Release and implicit restore it
2137         do_node $CLIENT2 "$LFS hsm_release $f" ||
2138                 error "hsm_release failed"
2139         do_node $CLIENT2 "md5sum $f" ||
2140                 error "hsm_restore failed"
2141
2142         wait_request_state $fid RESTORE SUCCEED
2143
2144         copytool_cleanup
2145 }
2146 run_test 57 "Archive a file with dirty cache on another node"
2147
2148 truncate_released_file() {
2149         local src_file=$1
2150         local trunc_to=$2
2151
2152         local sz=$(stat -c %s $src_file)
2153         local f=$DIR/$tdir/$tfile
2154         local fid=$(copy_file $1 $f)
2155         local ref=$f-ref
2156         cp $f $f-ref
2157
2158         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2159                 error "could not archive file"
2160         wait_request_state $fid ARCHIVE SUCCEED
2161
2162         $LFS hsm_release $f || error "could not release file"
2163
2164         $TRUNCATE $f $trunc_to || error "truncate failed"
2165         sync
2166
2167         local sz1=$(stat -c %s $f)
2168         [[ $sz1 == $trunc_to ]] ||
2169                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2170
2171         $LFS hsm_state $f
2172         check_hsm_flags $f "0x0000000b"
2173
2174         local state=$(get_request_state $fid RESTORE)
2175         [[ "$state" == "SUCCEED" ]] ||
2176                 error "truncate $sz does not trig restore, state = $state"
2177
2178         $TRUNCATE $ref $trunc_to
2179         cmp $ref $f || error "file data wrong after truncate"
2180
2181         rm -f $f $f-ref
2182 }
2183
2184 test_58() {
2185         # test needs a running copytool
2186         copytool_setup
2187
2188         mkdir -p $DIR/$tdir
2189
2190         local sz=$(stat -c %s /etc/passwd)
2191
2192         echo "truncate up from $sz to $((sz*2))"
2193         truncate_released_file /etc/passwd $((sz*2))
2194
2195         echo "truncate down from $sz to $((sz/2))"
2196         truncate_released_file /etc/passwd $((sz/2))
2197
2198         echo "truncate to 0"
2199         truncate_released_file /etc/passwd 0
2200
2201         copytool_cleanup
2202 }
2203 run_test 58 "Truncate a released file will trigger restore"
2204
2205 test_90() {
2206         file_count=57
2207         mkdir -p $DIR/$tdir
2208         local f=$DIR/$tdir/$tfile
2209         local FILELIST=/tmp/filelist.txt
2210         local i=""
2211
2212         rm -f $FILELIST
2213         for i in $(seq 1 $file_count); do
2214                 fid=$(copy_file /etc/hosts $f.$i)
2215                 echo $f.$i >> $FILELIST
2216         done
2217         copytool_setup
2218         # to be sure wait_all_done will not be mislead by previous tests
2219         cdt_purge
2220         wait_for_grace_delay
2221         $LFS hsm_archive --filelist $FILELIST ||
2222                 error "cannot archive a file list"
2223         wait_all_done 100
2224         $LFS hsm_release --filelist $FILELIST ||
2225                 error "cannot release a file list"
2226         $LFS hsm_restore --filelist $FILELIST ||
2227                 error "cannot restore a file list"
2228         wait_all_done 100
2229         copytool_cleanup
2230 }
2231 run_test 90 "Archive/restore a file list"
2232
2233 double_verify_reset_hsm_param() {
2234         local p=$1
2235         echo "Testing $HSM_PARAM.$p"
2236         local val=$(get_hsm_param $p)
2237         local save=$val
2238         local val2=$(($val * 2))
2239         set_hsm_param $p $val2
2240         val=$(get_hsm_param $p)
2241         [[ $val == $val2 ]] ||
2242                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
2243         echo "Set $p to 0 must failed"
2244         set_hsm_param $p 0
2245         local rc=$?
2246         # restore value
2247         set_hsm_param $p $save
2248
2249         if [[ $rc == 0 ]]; then
2250                 error "we must not be able to set $HSM_PARAM.$p to 0"
2251         fi
2252 }
2253
2254 test_100() {
2255         double_verify_reset_hsm_param loop_period
2256         double_verify_reset_hsm_param grace_delay
2257         double_verify_reset_hsm_param request_timeout
2258         double_verify_reset_hsm_param max_requests
2259         double_verify_reset_hsm_param default_archive_id
2260 }
2261 run_test 100 "Set coordinator /proc tunables"
2262
2263 test_102() {
2264         cdt_disable
2265         cdt_enable
2266         cdt_restart
2267 }
2268 run_test 102 "Verify coordinator control"
2269
2270 test_103() {
2271         # test needs a running copytool
2272         copytool_setup
2273
2274         local i=""
2275         local fid=""
2276
2277         mkdir -p $DIR/$tdir
2278         for i in $(seq 1 20); do
2279                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
2280         done
2281         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
2282
2283         cdt_purge
2284
2285         echo "Current requests"
2286         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2287                         $HSM_PARAM.agent_actions |\
2288                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
2289
2290         [[ -z "$res" ]] || error "Some request have not been canceled"
2291
2292         copytool_cleanup
2293 }
2294 run_test 103 "Purge all requests"
2295
2296 DATA=CEA
2297 DATAHEX='[434541]'
2298 test_104() {
2299         # test needs a running copytool
2300         copytool_setup
2301
2302         mkdir -p $DIR/$tdir
2303         local f=$DIR/$tdir/$tfile
2304         local fid=$(make_large_for_progress $f)
2305         # if cdt is on, it can serve too quickly the request
2306         cdt_disable
2307         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
2308         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2309                         $HSM_PARAM.agent_actions |\
2310                         grep $fid | cut -f16 -d=")
2311         cdt_enable
2312
2313         [[ "$data1" == "$DATAHEX" ]] ||
2314                 error "Data field in records is ($data1) and not ($DATAHEX)"
2315
2316         copytool_cleanup
2317 }
2318 run_test 104 "Copy tool data field"
2319
2320 test_105() {
2321         mkdir -p $DIR/$tdir
2322         local i=""
2323
2324         cdt_disable
2325         for i in $(seq -w 1 10); do
2326                 cp /etc/passwd $DIR/$tdir/$i
2327                 $LFS hsm_archive $DIR/$tdir/$i
2328         done
2329         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2330                         $HSM_PARAM.agent_actions |\
2331                         grep WAITING | wc -l")
2332         cdt_restart
2333         cdt_disable
2334         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2335                         $HSM_PARAM.agent_actions |\
2336                         grep WAITING | wc -l")
2337         cdt_enable
2338         cdt_purge
2339         [[ "$reqcnt1" == "$reqcnt2" ]] ||
2340                 error "Requests count after shutdown $reqcnt2 != "\
2341                       "before shutdown $reqcnt1"
2342 }
2343 run_test 105 "Restart of coordinator"
2344
2345 test_106() {
2346         # test needs a running copytool
2347         copytool_setup
2348
2349         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
2350                 get_client_uuid $MOUNT | cut -d' ' -f2)
2351         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2352                 grep $uuid)
2353         copytool_cleanup
2354         [[ ! -z "$agent" ]] || error "My uuid $uuid not found in agent list"
2355         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2356                 grep $uuid)
2357         [[ -z "$agent" ]] ||
2358                 error "My uuid $uuid still found in agent list,"\
2359                       " after copytool shutdown"
2360         copytool_setup
2361         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2362                 grep $uuid)
2363         copytool_cleanup
2364         [[ ! -z "$agent" ]] ||
2365                 error "My uuid $uuid not found in agent list after"\
2366                       " copytool restart"
2367 }
2368 run_test 106 "Copytool register/unregister"
2369
2370 test_107() {
2371         # test needs a running copytool
2372         copytool_setup
2373         # create and archive file
2374         mkdir -p $DIR/$tdir
2375         local f1=$DIR/$tdir/$tfile
2376         local fid=$(copy_file /etc/passwd $f1)
2377         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2378         wait_request_state $fid ARCHIVE SUCCEED
2379         # shutdown and restart MDS
2380         fail $SINGLEMDS
2381         # check the copytool still gets messages from MDT
2382         local f2=$DIR/$tdir/2
2383         local fid=$(copy_file /etc/passwd $f2)
2384         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2385         # main check of this sanity: this request MUST succeed
2386         wait_request_state $fid ARCHIVE SUCCEED
2387         copytool_cleanup
2388 }
2389 run_test 107 "Copytool re-register after MDS restart"
2390
2391 policy_set_and_test()
2392 {
2393         local change="$1"
2394         local target="$2"
2395         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
2396         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2397         [[ "$policy" == "$target" ]] ||
2398                 error "Wrong policy after '$change': '$policy' != '$target'"
2399 }
2400
2401 test_109() {
2402         # to force default policy setting if error
2403         CDT_POLICY_HAD_CHANGED=true
2404
2405         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2406         local default="NonBlockingRestore [NoRetryAction]"
2407         [[ "$policy" == "$default" ]] ||
2408                 error "default policy has changed,"\
2409                       " '$policy' != '$default' update the test"
2410         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
2411         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
2412         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
2413         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
2414         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
2415         # useless bacause we know but safer for futur changes to use real value
2416         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2417         echo "Next set_param must failed"
2418         policy_set_and_test "wrong" "$policy"
2419
2420         # return to default
2421         echo "Back to default policy"
2422         cdt_set_sanity_policy
2423 }
2424 run_test 109 "Policy display/change"
2425
2426 test_110a() {
2427         # test needs a running copytool
2428         copytool_setup
2429
2430         mkdir -p $DIR/$tdir
2431
2432         copy2archive /etc/passwd $tdir/$tfile
2433
2434         local f=$DIR/$tdir/$tfile
2435         import_file $tdir/$tfile $f
2436         local fid=$(path2fid $f)
2437
2438         cdt_set_non_blocking_restore
2439         md5sum $f
2440         local st=$?
2441
2442         # cleanup
2443         wait_request_state $fid RESTORE SUCCEED
2444         cdt_clear_non_blocking_restore
2445
2446         # Test result
2447         [[ $st == 1 ]] ||
2448                 error "md5sum returns $st != 1, "\
2449                         "should also perror ENODATA (No data available)"
2450
2451         copytool_cleanup
2452 }
2453 run_test 110a "Non blocking restore policy (import case)"
2454
2455 test_110b() {
2456         # test needs a running copytool
2457         copytool_setup
2458
2459         mkdir -p $DIR/$tdir
2460         local f=$DIR/$tdir/$tfile
2461         local fid=$(copy_file /etc/passwd $f)
2462         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2463         wait_request_state $fid ARCHIVE SUCCEED
2464         $LFS hsm_release $f
2465
2466         cdt_set_non_blocking_restore
2467         md5sum $f
2468         local st=$?
2469
2470         # cleanup
2471         wait_request_state $fid RESTORE SUCCEED
2472         cdt_clear_non_blocking_restore
2473
2474         # Test result
2475         [[ $st == 1 ]] ||
2476                 error "md5sum returns $st != 1, "\
2477                         "should also perror ENODATA (No data available)"
2478
2479         copytool_cleanup
2480 }
2481 run_test 110b "Non blocking restore policy (release case)"
2482
2483 test_111a() {
2484         # test needs a running copytool
2485         copytool_setup
2486
2487         mkdir -p $DIR/$tdir
2488         copy2archive /etc/passwd $tdir/$tfile
2489
2490         local f=$DIR/$tdir/$tfile
2491
2492         import_file $tdir/$tfile $f
2493         local fid=$(path2fid $f)
2494
2495         cdt_set_no_retry
2496
2497         copytool_remove_backend $fid
2498
2499         $LFS hsm_restore $f
2500         wait_request_state $fid RESTORE FAILED
2501         local st=$?
2502
2503         # cleanup
2504         cdt_clear_no_retry
2505
2506         # Test result
2507         [[ $st == 0 ]] || error "Restore does not failed"
2508
2509         copytool_cleanup
2510 }
2511 run_test 111a "No retry policy (import case), restore will error"\
2512               " (No such file or directory)"
2513
2514 test_111b() {
2515         # test needs a running copytool
2516         copytool_setup
2517
2518         mkdir -p $DIR/$tdir
2519         local f=$DIR/$tdir/$tfile
2520         local fid=$(copy_file /etc/passwd $f)
2521         cdt_set_no_retry
2522         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2523         wait_request_state $fid ARCHIVE SUCCEED
2524         $LFS hsm_release $f
2525
2526         copytool_remove_backend $fid
2527
2528         $LFS hsm_restore $f
2529         wait_request_state $fid RESTORE FAILED
2530         local st=$?
2531
2532         # cleanup
2533         cdt_clear_no_retry
2534
2535         # Test result
2536         [[ $st == 0 ]] || error "Restore does not failed"
2537
2538         copytool_cleanup
2539 }
2540 run_test 111b "No retry policy (release case), restore will error"\
2541               " (No such file or directory)"
2542
2543 test_112() {
2544         # test needs a running copytool
2545         copytool_setup
2546
2547         mkdir -p $DIR/$tdir
2548         local f=$DIR/$tdir/$tfile
2549         local fid=$(copy_file /etc/passwd $f)
2550         cdt_disable
2551         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2552         local l=$($LFS hsm_action $f)
2553         echo $l
2554         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
2555
2556         # cleanup
2557         cdt_enable
2558         wait_request_state $fid ARCHIVE SUCCEED
2559
2560         # Test result
2561         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
2562
2563         copytool_cleanup
2564 }
2565 run_test 112 "State of recorded request"
2566
2567 test_200() {
2568         # test needs a running copytool
2569         copytool_setup
2570
2571         mkdir -p $DIR/$tdir
2572         local f=$DIR/$tdir/$tfile
2573         local fid=$(make_large_for_cancel $f)
2574         # test with cdt on is made in test_221
2575         cdt_disable
2576         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2577         $LFS hsm_cancel $f
2578         cdt_enable
2579         wait_request_state $fid ARCHIVE CANCELED
2580         wait_request_state $fid CANCEL SUCCEED
2581
2582         copytool_cleanup
2583 }
2584 run_test 200 "Register/Cancel archive"
2585
2586 test_201() {
2587         # test needs a running copytool
2588         copytool_setup
2589
2590         mkdir -p $DIR/$tdir
2591         local f=$DIR/$tdir/$tfile
2592         make_archive $tdir/$tfile
2593         import_file $tdir/$tfile $f
2594         local fid=$(path2fid $f)
2595
2596         # test with cdt on is made in test_222
2597         cdt_disable
2598         $LFS hsm_restore $f
2599         $LFS hsm_cancel $f
2600         cdt_enable
2601         wait_request_state $fid RESTORE CANCELED
2602         wait_request_state $fid CANCEL SUCCEED
2603
2604         copytool_cleanup
2605 }
2606 run_test 201 "Register/Cancel restore"
2607
2608 test_202() {
2609         # test needs a running copytool
2610         copytool_setup
2611
2612         mkdir -p $DIR/$tdir
2613         local f=$DIR/$tdir/$tfile
2614         local fid=$(make_large_for_progress $f)
2615         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2616         wait_request_state $fid ARCHIVE SUCCEED
2617
2618         cdt_disable
2619         $LFS hsm_remove $f
2620         $LFS hsm_cancel $f
2621         cdt_enable
2622         wait_request_state $fid REMOVE CANCELED
2623
2624         copytool_cleanup
2625 }
2626 run_test 202 "Register/Cancel remove"
2627
2628 test_220() {
2629         # test needs a running copytool
2630         copytool_setup
2631
2632         mkdir -p $DIR/$tdir
2633
2634         local f=$DIR/$tdir/$tfile
2635         local fid=$(copy_file /etc/passwd $f)
2636
2637         changelog_setup
2638
2639         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2640         wait_request_state $fid ARCHIVE SUCCEED
2641
2642         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2643         changelog_cleanup
2644
2645         local target=0x0
2646         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2647
2648         copytool_cleanup
2649 }
2650 run_test 220 "Changelog for archive"
2651
2652 test_221() {
2653         # test needs a running copytool
2654         copytool_setup
2655
2656         mkdir -p $DIR/$tdir
2657
2658         local f=$DIR/$tdir/$tfile
2659         local fid=$(make_large_for_cancel $f)
2660
2661         changelog_setup
2662
2663         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2664         wait_request_state $fid ARCHIVE STARTED
2665         $LFS hsm_cancel $f
2666         wait_request_state $fid ARCHIVE CANCELED
2667         wait_request_state $fid CANCEL SUCCEED
2668
2669         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2670
2671         local target=0x7d
2672         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2673
2674         cleanup
2675 }
2676 run_test 221 "Changelog for archive canceled"
2677
2678 test_222a() {
2679         # test needs a running copytool
2680         copytool_setup
2681
2682         mkdir -p $DIR/$tdir
2683         copy2archive /etc/passwd $tdir/$tfile
2684
2685         local f=$DIR/$tdir/$tfile
2686         import_file $tdir/$tfile $f
2687         local fid=$(path2fid $f)
2688
2689         changelog_setup
2690
2691         $LFS hsm_restore $f
2692         wait_request_state $fid RESTORE SUCCEED
2693
2694         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2695
2696         local target=0x80
2697         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2698
2699         cleanup
2700 }
2701 run_test 222a "Changelog for explicit restore"
2702
2703 test_222b() {
2704         # test needs a running copytool
2705         copytool_setup
2706
2707         mkdir -p $DIR/$tdir
2708         local f=$DIR/$tdir/$tfile
2709         local fid=$(copy_file /etc/passwd $f)
2710
2711         changelog_setup
2712         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2713         wait_request_state $fid ARCHIVE SUCCEED
2714         $LFS hsm_release $f
2715
2716         md5sum $f
2717
2718         wait_request_state $fid RESTORE SUCCEED
2719
2720         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2721
2722         local target=0x80
2723         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2724
2725         cleanup
2726 }
2727 run_test 222b "Changelog for implicit restore"
2728
2729 test_223a() {
2730         # test needs a running copytool
2731         copytool_setup
2732
2733         mkdir -p $DIR/$tdir
2734
2735         local f=$DIR/$tdir/$tfile
2736         make_archive $tdir/$tfile
2737
2738         changelog_setup
2739
2740         import_file $tdir/$tfile $f
2741         local fid=$(path2fid $f)
2742
2743         $LFS hsm_restore $f
2744         wait_request_state $fid RESTORE STARTED
2745         $LFS hsm_cancel $f
2746         wait_request_state $fid RESTORE CANCELED
2747         wait_request_state $fid CANCEL SUCCEED
2748
2749         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2750
2751         local target=0xfd
2752         [[ $flags == $target ]] ||
2753                 error "Changelog flag is $flags not $target"
2754
2755         cleanup
2756 }
2757 run_test 223a "Changelog for restore canceled (import case)"
2758
2759 test_223b() {
2760         # test needs a running copytool
2761         copytool_setup
2762
2763         mkdir -p $DIR/$tdir
2764
2765         local f=$DIR/$tdir/$tfile
2766         local fid=$(make_large_for_progress $f)
2767
2768         changelog_setup
2769         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2770         wait_request_state $fid ARCHIVE SUCCEED
2771         $LFS hsm_release $f
2772         $LFS hsm_restore $f
2773         wait_request_state $fid RESTORE STARTED
2774         $LFS hsm_cancel $f
2775         wait_request_state $fid RESTORE CANCELED
2776         wait_request_state $fid CANCEL SUCCEED
2777
2778         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2779
2780         local target=0xfd
2781         [[ $flags == $target ]] ||
2782                 error "Changelog flag is $flags not $target"
2783
2784         cleanup
2785 }
2786 run_test 223b "Changelog for restore canceled (release case)"
2787
2788 test_224() {
2789         # test needs a running copytool
2790         copytool_setup
2791
2792         mkdir -p $DIR/$tdir
2793
2794         local f=$DIR/$tdir/$tfile
2795         local fid=$(copy_file /etc/passwd $f)
2796
2797         changelog_setup
2798         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2799         wait_request_state $fid ARCHIVE SUCCEED
2800
2801         $LFS hsm_remove $f
2802         wait_request_state $fid REMOVE SUCCEED
2803
2804         local flags=$(changelog_get_flags $MDT0 HSM $fid | tail -1)
2805
2806         local target=0x200
2807         [[ $flags == $target ]] ||
2808                 error "Changelog flag is $flags not $target"
2809
2810         cleanup
2811 }
2812 run_test 224 "Changelog for remove"
2813
2814 test_225() {
2815         # test needs a running copytool
2816         copytool_setup
2817
2818         # test is not usable because remove request is too fast
2819         # so it is always finished before cancel can be done ...
2820         echo "Test disabled"
2821         copytool_cleanup
2822         return 0
2823
2824         mkdir -p $DIR/$tdir
2825         local f=$DIR/$tdir/$tfile
2826         local fid=$(make_large_for_progress $f)
2827
2828         changelog_setup
2829         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2830         wait_request_state $fid ARCHIVE SUCCEED
2831
2832         # if cdt is on, it can serve too quickly the request
2833         cdt_disable
2834         $LFS hsm_remove $f
2835         $LFS hsm_cancel $f
2836         cdt_enable
2837         wait_request_state $fid REMOVE CANCELED
2838         wait_request_state $fid CANCEL SUCCEED
2839
2840         flags=$(changelog_get_flags $MDT0 RENME $fid2)
2841         local flags=$($LFS changelog $MDT0 | grep HSM | grep $fid | tail -1 |
2842                 awk '{print $5}')
2843
2844         local target=0x27d
2845         [[ $flags == $target ]] ||
2846                 error "Changelog flag is $flags not $target"
2847
2848         cleanup
2849 }
2850 run_test 225 "Changelog for remove canceled"
2851
2852 test_226() {
2853         # test needs a running copytool
2854         copytool_setup
2855
2856         mkdir -p $DIR/$tdir
2857
2858         local f1=$DIR/$tdir/$tfile-1
2859         local f2=$DIR/$tdir/$tfile-2
2860         local f3=$DIR/$tdir/$tfile-3
2861         local fid1=$(copy_file /etc/passwd $f1)
2862         local fid2=$(copy_file /etc/passwd $f2)
2863         copy_file /etc/passwd $f3
2864
2865         changelog_setup
2866         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2867         wait_request_state $fid1 ARCHIVE SUCCEED
2868
2869         $LFS hsm_archive $f2
2870         wait_request_state $fid2 ARCHIVE SUCCEED
2871
2872         rm $f1 || error "rm $f1 failed"
2873
2874         local flags=$(changelog_get_flags $MDT0 UNLNK $fid1)
2875
2876         local target=0x3
2877         [[ $flags == $target ]] ||
2878                 error "Changelog flag is $flags not $target"
2879
2880         mv $f3 $f2 || error "mv $f3 $f2 failed"
2881
2882         flags=$(changelog_get_flags $MDT0 RENME $fid2)
2883
2884         target=0x3
2885         [[ $flags == $target ]] ||
2886                 error "Changelog flag is $flags not $target"
2887
2888         cleanup
2889 }
2890 run_test 226 "changelog for last rm/mv with exiting archive"
2891
2892 check_flags_changes() {
2893         local f=$1
2894         local fid=$2
2895         local hsm_flag=$3
2896         local fst=$4
2897         local cnt=$5
2898
2899         local target=0x280
2900         $LFS hsm_set --$hsm_flag $f ||
2901                 error "Cannot set $hsm_flag on $f"
2902         local flags=($(changelog_get_flags $MDT0 HSM $fid))
2903         local seen=${#flags[*]}
2904         cnt=$((fst + cnt))
2905         [[ $seen == $cnt ]] ||
2906                 error "set $hsm_flag: Changelog events $seen != $cnt"
2907         [[ ${flags[$((cnt - 1))]} == $target ]] ||
2908                 error "set $hsm_flag: Changelog flags are "\
2909                         "${flags[$((cnt - 1))]} not $target"
2910
2911         $LFS hsm_clear --$hsm_flag $f ||
2912                 error "Cannot clear $hsm_flag on $f"
2913         flags=($(changelog_get_flags $MDT0 HSM $fid))
2914         seen=${#flags[*]}
2915         cnt=$(($cnt + 1))
2916         [[ $cnt == $seen ]] ||
2917                 error "clear $hsm_flag: Changelog events $seen != $cnt"
2918
2919         [[ ${flags[$((cnt - 1))]} == $target ]] ||
2920                 error "clear $hsm_flag: Changelog flag is "\
2921                         "${flags[$((cnt - 1))]} not $target"
2922 }
2923
2924 test_227() {
2925         # test needs a running copytool
2926         copytool_setup
2927         changelog_setup
2928
2929         mkdir -p $DIR/$tdir
2930         typeset -a flags
2931
2932         for i in norelease noarchive exists archived
2933         do
2934                 local f=$DIR/$tdir/$tfile-$i
2935                 local fid=$(copy_file /etc/passwd $f)
2936                 check_flags_changes $f $fid $i 0 1
2937         done
2938
2939         f=$DIR/$tdir/$tfile---lost
2940         fid=$(copy_file /etc/passwd $f)
2941         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2942         wait_request_state $fid ARCHIVE SUCCEED
2943         check_flags_changes $f $fid lost 3 1
2944
2945         cleanup
2946 }
2947 run_test 227 "changelog when explicit setting of HSM flags"
2948
2949 test_228() {
2950         # test needs a running copytool
2951         copytool_setup
2952
2953         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=1 conv=sync ||
2954                 error "creating $DIR/$tfile"
2955         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
2956         wait_request_state $(path2fid $DIR/$tfile) ARCHIVE SUCCEED
2957
2958         $LFS hsm_release $DIR/$tfile
2959         check_hsm_flags $DIR/$tfile "0x0000000d"
2960
2961         filefrag $DIR/$tfile | grep " 1 extent found" ||
2962                 error "filefrag on released file must return only one extent"
2963
2964         # only newer versions of cp detect sparse files by stat/FIEMAP
2965         # (LU-2580)
2966         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
2967                 error "copying $DIR/$tfile"
2968         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
2969
2970         $LFS hsm_release $DIR/$tfile
2971         check_hsm_flags $DIR/$tfile "0x0000000d"
2972
2973         mkdir $DIR/$tdir
2974
2975         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
2976                 error "tar failed"
2977         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
2978                 error "comparing untarred $DIR/$tfile"
2979
2980         copytool_cleanup
2981 }
2982 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
2983
2984 test_250() {
2985         # test needs a running copytool
2986         copytool_setup
2987
2988         mkdir -p $DIR/$tdir
2989         local maxrequest=$(get_hsm_param max_requests)
2990         local rqcnt=$(($maxrequest * 3))
2991         local i=""
2992
2993         cdt_disable
2994         for i in $(seq -w 1 $rqcnt); do
2995                 rm -f $DIR/$tdir/$i
2996                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
2997         done
2998         # we do it in 2 steps, so all requests arrive at the same time
2999         for i in $(seq -w 1 $rqcnt); do
3000                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3001         done
3002         cdt_enable
3003         local cnt=$rqcnt
3004         local wt=$rqcnt
3005         while [[ $cnt != 0 || $wt != 0 ]]; do
3006                 sleep 1
3007                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3008                         $HSM_PARAM.agent_actions |\
3009                         grep STARTED | grep -v CANCEL | wc -l")
3010                 [[ $cnt -le $maxrequest ]] ||
3011                         error "$cnt > $maxrequest too many started requests"
3012                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
3013                         $HSM_PARAM.agent_actions |\
3014                         grep WAITING | wc -l")
3015                 echo "max=$maxrequest started=$cnt waiting=$wt"
3016         done
3017
3018         copytool_cleanup
3019 }
3020 run_test 250 "Coordinator max request"
3021
3022 test_251() {
3023         # test needs a running copytool
3024         copytool_setup
3025
3026         mkdir -p $DIR/$tdir
3027         local f=$DIR/$tdir/$tfile
3028         local fid=$(make_large_for_cancel $f)
3029
3030         cdt_disable
3031         # to have a short test
3032         local old_to=$(get_hsm_param request_timeout)
3033         set_hsm_param request_timeout 4
3034         # to be sure the cdt will wake up frequently so
3035         # it will be able to cancel the "old" request
3036         local old_loop=$(get_hsm_param loop_period)
3037         set_hsm_param loop_period 2
3038         cdt_enable
3039
3040         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3041         wait_request_state $fid ARCHIVE STARTED
3042         sleep 5
3043         wait_request_state $fid ARCHIVE CANCELED
3044
3045         set_hsm_param request_timeout $old_to
3046         set_hsm_param loop_period $old_loop
3047
3048         copytool_cleanup
3049 }
3050 run_test 251 "Coordinator request timeout"
3051
3052 test_300() {
3053         # the only way to test ondisk conf is to restart MDS ...
3054         echo "Stop coordinator and remove coordinator state at mount"
3055         # stop coordinator
3056         cdt_shutdown
3057         # clean on disk conf set by default
3058         cdt_clear_mount_state
3059         cdt_check_state stopped
3060
3061         # check cdt still off after umount/remount
3062         fail $SINGLEMDS
3063         cdt_check_state stopped
3064
3065         echo "Set coordinator start at mount, and start coordinator"
3066         cdt_set_mount_state enabled
3067
3068         # check cdt is on
3069         cdt_check_state enabled
3070
3071         # check cdt still on after umount/remount
3072         fail $SINGLEMDS
3073         cdt_check_state enabled
3074
3075         # we are back to original state (cdt started at mount)
3076 }
3077 run_test 300 "On disk coordinator state kept between MDT umount/mount"
3078
3079 test_301() {
3080         local ai=$(get_hsm_param default_archive_id)
3081         local new=$((ai + 1))
3082
3083         set_hsm_param default_archive_id $new -P
3084         fail $SINGLEMDS
3085         local res=$(get_hsm_param default_archive_id)
3086
3087         # clear value
3088         set_hsm_param default_archive_id "" "-P -d"
3089
3090         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3091 }
3092 run_test 301 "HSM tunnable are persistent"
3093
3094 test_302() {
3095         local ai=$(get_hsm_param default_archive_id)
3096         local new=$((ai + 1))
3097
3098         # stop coordinator
3099         cdt_shutdown
3100
3101         set_hsm_param default_archive_id $new -P
3102         fail $SINGLEMDS
3103
3104         # check cdt is on
3105         cdt_check_state enabled
3106
3107         local res=$(get_hsm_param default_archive_id)
3108
3109         # clear value
3110         set_hsm_param default_archive_id "" "-P -d"
3111
3112         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3113 }
3114 run_test 302 "HSM tunnable are persistent when CDT is off"
3115
3116 copytool_cleanup
3117
3118 complete $SECONDS
3119 check_and_cleanup_lustre
3120 exit_status