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