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