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