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