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