Whamcloud - gitweb
LU-3726 tests: make sanity-hsm compatible with MDSCOUNT >= 2.
[fs/lustre-release.git] / lustre / tests / sanity-hsm.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # exit on error
7 set -e
8 set +o monitor
9
10 SRCDIR=$(dirname $0)
11 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin
12
13 ONLY=${ONLY:-"$*"}
14 # bug number for skipped test:    3815     3939
15 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT 34 35 36 40"
16 # 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         $LFS hsm_archive $f || error "could not archive file"
1339         wait_request_state $fid ARCHIVE SUCCEED
1340
1341         [ $(stat -c "%b" $f) -ne "1" ] || error "wrong block number"
1342         local sz=$(stat -c "%s" $f)
1343         [ $sz -ne "0" ] || error "file size should not be zero"
1344
1345         # Release and check states
1346         $LFS hsm_release $f || error "could not release file"
1347         check_hsm_flags $f "0x0000000d"
1348
1349         [ $(stat -c "%b" $f) -eq "1" ] || error "wrong block number"
1350         [ $(stat -c "%s" $f) -eq $sz ] || error "wrong file size"
1351
1352         # Check we can release an file without stripe info
1353         f=$f.nolov
1354         $MCREATE $f
1355         fid=$(path2fid $f)
1356         check_hsm_flags $f "0x00000000"
1357         $LFS hsm_archive $f || error "could not archive file"
1358         wait_request_state $fid ARCHIVE SUCCEED
1359
1360         # Release and check states
1361         $LFS hsm_release $f || error "could not release file"
1362         check_hsm_flags $f "0x0000000d"
1363
1364         # Release again a file that is already released is OK
1365         $LFS hsm_release $f || fail "second release should succeed"
1366         check_hsm_flags $f "0x0000000d"
1367
1368         copytool_cleanup
1369 }
1370 run_test 21 "Simple release tests"
1371
1372 test_22() {
1373         # test needs a running copytool
1374         copytool_setup
1375
1376         mkdir -p $DIR/$tdir
1377
1378         local f=$DIR/$tdir/test_release
1379         local swap=$DIR/$tdir/test_swap
1380
1381         # Create a file and check its states
1382         local fid=$(make_small $f)
1383         check_hsm_flags $f "0x00000000"
1384
1385         $LFS hsm_archive $f || error "could not archive file"
1386         wait_request_state $fid ARCHIVE SUCCEED
1387
1388         # Release and check states
1389         $LFS hsm_release $f || error "could not release file"
1390         check_hsm_flags $f "0x0000000d"
1391
1392         make_small $swap
1393         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1394
1395         true
1396         copytool_cleanup
1397 }
1398 run_test 22 "Could not swap a release file"
1399
1400 test_23() {
1401         # test needs a running copytool
1402         copytool_setup
1403
1404         mkdir -p $DIR/$tdir
1405
1406         local f=$DIR/$tdir/test_mtime
1407
1408         # Create a file and check its states
1409         local fid=$(make_small $f)
1410         check_hsm_flags $f "0x00000000"
1411
1412         $LFS hsm_archive $f || error "could not archive file"
1413         wait_request_state $fid ARCHIVE SUCCEED
1414
1415         # Set modification time in the past
1416         touch -m -a -d @978261179 $f
1417
1418         # Release and check states
1419         $LFS hsm_release $f || error "could not release file"
1420         check_hsm_flags $f "0x0000000d"
1421
1422         local MTIME=$(stat -c "%Y" $f)
1423         local ATIME=$(stat -c "%X" $f)
1424         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1425         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1426
1427         copytool_cleanup
1428 }
1429 run_test 23 "Release does not change a/mtime (utime)"
1430
1431 test_24a() {
1432         local file=$DIR/$tdir/$tfile
1433         local fid
1434         local atime0
1435         local atime1
1436         local mtime0
1437         local mtime1
1438         local ctime0
1439         local ctime1
1440
1441         # test needs a running copytool
1442         copytool_setup
1443
1444         mkdir -p $DIR/$tdir
1445         rm -f $file
1446         fid=$(make_small $file)
1447
1448         # Create a file and check its states
1449         check_hsm_flags $file "0x00000000"
1450
1451         # Ensure atime is less than mtime and ctime.
1452         sleep 1
1453         echo >> $file
1454
1455         atime0=$(stat -c "%X" $file)
1456         mtime0=$(stat -c "%Y" $file)
1457         ctime0=$(stat -c "%Z" $file)
1458
1459         [ $atime0 -lt $mtime0 ] ||
1460                 error "atime $atime0 is not less than mtime $mtime0"
1461
1462         [ $atime0 -lt $ctime0 ] ||
1463                 error "atime $atime0 is not less than ctime $ctime0"
1464
1465         # Archive should not change any timestamps.
1466         $LFS hsm_archive $file || error "cannot archive '$file'"
1467         wait_request_state $fid ARCHIVE SUCCEED
1468
1469         atime1=$(stat -c "%X" $file)
1470         mtime1=$(stat -c "%Y" $file)
1471         ctime1=$(stat -c "%Z" $file)
1472
1473         [ $atime0 -eq $atime1 ] ||
1474                 error "archive changed atime from $atime0 to $atime1"
1475
1476         [ $mtime0 -eq $mtime1 ] ||
1477                 error "archive changed mtime from $mtime0 to $mtime1"
1478
1479         [ $ctime0 -eq $ctime1 ] ||
1480                 error "archive changed ctime from $ctime0 to $ctime1"
1481
1482         # Release should not change any timestamps.
1483         $LFS hsm_release $file || error "cannot release '$file'"
1484         check_hsm_flags $file "0x0000000d"
1485
1486         atime1=$(stat -c "%X" $file)
1487         mtime1=$(stat -c "%Y" $file)
1488         ctime1=$(stat -c "%Z" $file)
1489
1490         [ $atime0 -eq $atime1 ] ||
1491                 error "release changed atime from $atime0 to $atime1"
1492
1493         [ $mtime0 -eq $mtime1 ] ||
1494                 error "release changed mtime from $mtime0 to $mtime1"
1495
1496         [ $ctime0 -eq $ctime1 ] ||
1497                 error "release changed ctime from $ctime0 to $ctime1"
1498
1499         # Restore should not change atime or mtime and should not
1500         # decrease ctime.
1501         $LFS hsm_restore $file
1502         wait_request_state $fid RESTORE SUCCEED
1503
1504         atime1=$(stat -c "%X" $file)
1505         mtime1=$(stat -c "%Y" $file)
1506         ctime1=$(stat -c "%Z" $file)
1507
1508         [ $atime0 -eq $atime1 ] ||
1509                 error "restore changed atime from $atime0 to $atime1"
1510
1511         [ $mtime0 -eq $mtime1 ] ||
1512                 error "restore changed mtime from $mtime0 to $mtime1"
1513
1514         [ $ctime0 -le $ctime1 ] ||
1515                 error "restore changed ctime from $ctime0 to $ctime1"
1516
1517         copytool_cleanup
1518
1519         # Once more, after unmount and mount.
1520         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1521         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1522
1523         atime1=$(stat -c "%X" $file)
1524         mtime1=$(stat -c "%Y" $file)
1525         ctime1=$(stat -c "%Z" $file)
1526
1527         [ $atime0 -eq $atime1 ] ||
1528                 error "remount changed atime from $atime0 to $atime1"
1529
1530         [ $mtime0 -eq $mtime1 ] ||
1531                 error "remount changed mtime from $mtime0 to $mtime1"
1532
1533         [ $ctime0 -le $ctime1 ] ||
1534                 error "remount changed ctime from $ctime0 to $ctime1"
1535 }
1536 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1537
1538 test_24b() {
1539         local file=$DIR/$tdir/$tfile
1540         local fid
1541         local sum0
1542         local sum1
1543         # LU-3811
1544
1545         # Test needs a running copytool.
1546         copytool_setup
1547         mkdir -p $DIR/$tdir
1548
1549         # Check that root can do HSM actions on a ordinary user's file.
1550         rm -f $file
1551         fid=$(make_small $file)
1552         sum0=$(md5sum $file)
1553
1554         chown $RUNAS_ID:$RUNAS_GID $file ||
1555                 error "cannot chown '$file' to '$RUNAS_ID'"
1556
1557         chmod ugo-w $DIR/$tdir ||
1558                 error "cannot chmod '$DIR/$tdir'"
1559
1560         $LFS hsm_archive $file
1561         wait_request_state $fid ARCHIVE SUCCEED
1562
1563         $LFS hsm_release $file
1564         check_hsm_flags $file "0x0000000d"
1565
1566         $LFS hsm_restore $file
1567         wait_request_state $fid RESTORE SUCCEED
1568
1569         # Check that ordinary user can get HSM state.
1570         $RUNAS $LFS hsm_state $file ||
1571                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1572
1573         $LFS hsm_release $file
1574         check_hsm_flags $file "0x0000000d"
1575
1576         # Check that ordinary user can accessed released file.
1577         sum1=$($RUNAS md5sum $file) ||
1578                 error "user '$RUNAS_ID' cannot read '$file'"
1579
1580         [ "$sum0" == "$sum1" ] ||
1581                 error "md5sum mismatch for '$file'"
1582
1583         copytool_cleanup
1584 }
1585 run_test 24b "root can archive, release, and restore user files"
1586
1587 cleanup_test_24c() {
1588         trap 0
1589         set_hsm_param user_request_mask RESTORE
1590         set_hsm_param group_request_mask RESTORE
1591         set_hsm_param other_request_mask RESTORE
1592 }
1593
1594 test_24c() {
1595         local file=$DIR/$tdir/$tfile
1596         local action=archive
1597         local user_save
1598         local group_save
1599         local other_save
1600
1601         # test needs a running copytool
1602         copytool_setup
1603
1604         mkdir -p $DIR/$tdir
1605
1606         # Save the default masks and check that cleanup_24c will
1607         # restore the request masks correctly.
1608         user_save=$(get_hsm_param user_request_mask)
1609         group_save=$(get_hsm_param group_request_mask)
1610         other_save=$(get_hsm_param other_request_mask)
1611
1612         [ "$user_save" == RESTORE ] ||
1613                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1614         [ "$group_save" == RESTORE ] ||
1615                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1616         [ "$other_save" == RESTORE ] ||
1617                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1618
1619         trap cleanup_test_24c EXIT
1620
1621         # User.
1622         rm -f $file
1623         make_small $file
1624         chown $RUNAS_ID:nobody $file ||
1625                 error "cannot chown '$file' to '$RUNAS_ID:nobody'"
1626
1627         set_hsm_param user_request_mask ""
1628         $RUNAS $LFS hsm_$action $file &&
1629                 error "$action by user should fail"
1630
1631         set_hsm_param user_request_mask $action
1632         $RUNAS $LFS hsm_$action $file ||
1633                 error "$action by user should succeed"
1634
1635         # Group.
1636         rm -f $file
1637         make_small $file
1638         chown nobody:$RUNAS_GID $file ||
1639                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
1640
1641         set_hsm_param group_request_mask ""
1642         $RUNAS $LFS hsm_$action $file &&
1643                 error "$action by group should fail"
1644
1645         set_hsm_param group_request_mask $action
1646         $RUNAS $LFS hsm_$action $file ||
1647                 error "$action by group should succeed"
1648
1649         # Other.
1650         rm -f $file
1651         make_small $file
1652         chown nobody:nobody $file ||
1653                 error "cannot chown '$file' to 'nobody:nobody'"
1654
1655         set_hsm_param other_request_mask ""
1656         $RUNAS $LFS hsm_$action $file &&
1657                 error "$action by other should fail"
1658
1659         set_hsm_param other_request_mask $action
1660         $RUNAS $LFS hsm_$action $file ||
1661                 error "$action by other should succeed"
1662
1663         copytool_cleanup
1664         cleanup_test_24c
1665 }
1666 run_test 24c "check that user,group,other request masks work"
1667
1668 cleanup_test_24d() {
1669         trap 0
1670         mount -o remount,rw $MOUNT2
1671 }
1672
1673 test_24d() {
1674         local file1=$DIR/$tdir/$tfile
1675         local file2=$DIR2/$tdir/$tfile
1676         local fid1
1677         local fid2
1678
1679         copytool_setup
1680
1681         mkdir -p $DIR/$tdir
1682         rm -f $file1
1683         fid1=$(make_small $file1)
1684
1685         trap cleanup_test_24d EXIT
1686
1687         mount -o remount,ro $MOUNT2
1688
1689         fid2=$(path2fid $file2)
1690         [ "$fid1" == "$fid2" ] ||
1691                 error "FID mismatch '$fid1' != '$fid2'"
1692
1693         $LFS hsm_archive $file2 &&
1694                 error "archive should fail on read-only mount"
1695         check_hsm_flags $file1 "0x00000000"
1696
1697         $LFS hsm_archive $file1
1698         wait_request_state $fid1 ARCHIVE SUCCEED
1699
1700         $LFS hsm_release $file1
1701         $LFS hsm_restore $file2
1702         wait_request_state $fid1 RESTORE SUCCEED
1703
1704         $LFS hsm_release $file1 || error "cannot release '$file1'"
1705         dd if=$file2 of=/dev/null bs=1M || "cannot read '$file2'"
1706
1707         $LFS hsm_release $file2 &&
1708                 error "release should fail on read-only mount"
1709
1710         copytool_cleanup
1711         cleanup_test_24d
1712 }
1713 run_test 24d "check that read-only mounts are respected"
1714
1715 test_25a() {
1716         # test needs a running copytool
1717         copytool_setup
1718
1719         mkdir -p $DIR/$tdir
1720         copy2archive /etc/hosts $tdir/$tfile
1721
1722         local f=$DIR/$tdir/$tfile
1723
1724         import_file $tdir/$tfile $f
1725
1726         $LFS hsm_set --lost $f
1727
1728         md5sum $f
1729         local st=$?
1730
1731         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1732
1733         copytool_cleanup
1734 }
1735 run_test 25a "Restore lost file (HS_LOST flag) from import"\
1736              " (Operation not permitted)"
1737
1738 test_25b() {
1739         # test needs a running copytool
1740         copytool_setup
1741
1742         mkdir -p $DIR/$tdir
1743
1744         local f=$DIR/$tdir/$tfile
1745         local fid=$(copy_file /etc/passwd $f)
1746
1747         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1748         wait_request_state $fid ARCHIVE SUCCEED
1749
1750         $LFS hsm_release $f
1751         $LFS hsm_set --lost $f
1752         md5sum $f
1753         st=$?
1754
1755         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1756
1757         copytool_cleanup
1758 }
1759 run_test 25b "Restore lost file (HS_LOST flag) after release"\
1760              " (Operation not permitted)"
1761
1762 test_26() {
1763         # test needs a running copytool
1764         copytool_setup
1765
1766         mkdir -p $DIR/$tdir
1767         local f=$DIR/$tdir/$tfile
1768         local fid=$(make_large_for_progress $f)
1769         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1770         wait_request_state $fid ARCHIVE SUCCEED
1771
1772         $LFS hsm_remove $f
1773         wait_request_state $fid REMOVE SUCCEED
1774
1775         check_hsm_flags $f "0x00000000"
1776
1777         copytool_cleanup
1778 }
1779 run_test 26 "Remove the archive of a valid file"
1780
1781 test_27a() {
1782         # test needs a running copytool
1783         copytool_setup
1784
1785         mkdir -p $DIR/$tdir
1786         make_archive $tdir/$tfile
1787         local f=$DIR/$tdir/$tfile
1788         import_file $tdir/$tfile $f
1789         local fid=$(path2fid $f)
1790
1791         $LFS hsm_remove $f
1792
1793         [[ $? != 0 ]] || error "Remove of a released file should fail"
1794
1795         copytool_cleanup
1796 }
1797 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
1798
1799 test_27b() {
1800         # test needs a running copytool
1801         copytool_setup
1802
1803         mkdir -p $DIR/$tdir
1804         local f=$DIR/$tdir/$tfile
1805         local fid=$(make_large_for_progress $f)
1806         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1807         wait_request_state $fid ARCHIVE SUCCEED
1808         $LFS hsm_release $f
1809
1810         $LFS hsm_remove $f
1811
1812         [[ $? != 0 ]] || error "Remove of a released file should fail"
1813
1814         copytool_cleanup
1815 }
1816 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
1817
1818 test_28() {
1819         # test needs a running copytool
1820         copytool_setup
1821
1822         mkdir -p $DIR/$tdir
1823         local f=$DIR/$tdir/$tfile
1824         local fid=$(make_large_for_progress $f)
1825         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1826         wait_request_state $fid ARCHIVE SUCCEED
1827
1828         cdt_disable
1829         $LFS hsm_remove $f
1830
1831         rm -f $f
1832
1833         cdt_enable
1834
1835         wait_request_state $fid REMOVE SUCCEED
1836
1837         copytool_cleanup
1838 }
1839 run_test 28 "Concurrent archive/file remove"
1840
1841 test_30a() {
1842         # restore at exec cannot work on agent node (because of Linux kernel
1843         # protection of executables)
1844         needclients 2 || return 0
1845
1846         # test needs a running copytool
1847         copytool_setup
1848
1849         mkdir -p $DIR/$tdir
1850         copy2archive /bin/true $tdir/$tfile
1851
1852         local f=$DIR/$tdir/true
1853         import_file $tdir/$tfile $f
1854
1855         local fid=$(path2fid $f)
1856
1857         # set no retry action mode
1858         cdt_set_no_retry
1859         do_node $CLIENT2 $f
1860         local st=$?
1861
1862         # cleanup
1863         # remove no try action mode
1864         cdt_clear_no_retry
1865         $LFS hsm_state $f
1866
1867         [[ $st == 0 ]] || error "Failed to exec a released file"
1868
1869         copytool_cleanup
1870 }
1871 run_test 30a "Restore at exec (import case)"
1872
1873 test_30b() {
1874         # restore at exec cannot work on agent node (because of Linux kernel
1875         # protection of executables)
1876         needclients 2 || return 0
1877
1878         # test needs a running copytool
1879         copytool_setup
1880
1881         mkdir -p $DIR/$tdir
1882         local f=$DIR/$tdir/true
1883         local fid=$(copy_file /bin/true $f)
1884         chmod 755 $f
1885         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1886         wait_request_state $fid ARCHIVE SUCCEED
1887         $LFS hsm_release $f
1888         $LFS hsm_state $f
1889         # set no retry action mode
1890         cdt_set_no_retry
1891         do_node $CLIENT2 $f
1892         local st=$?
1893
1894         # cleanup
1895         # remove no try action mode
1896         cdt_clear_no_retry
1897         $LFS hsm_state $f
1898
1899         [[ $st == 0 ]] || error "Failed to exec a released file"
1900
1901         copytool_cleanup
1902 }
1903 run_test 30b "Restore at exec (release case)"
1904
1905 test_30c() {
1906         needclients 2 || return 0
1907
1908         # test needs a running copytool
1909         copytool_setup
1910
1911         mkdir -p $DIR/$tdir
1912         local f=$DIR/$tdir/SLEEP
1913         local fid=$(copy_file /bin/sleep $f)
1914         chmod 755 $f
1915         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1916         wait_request_state $fid ARCHIVE SUCCEED
1917         $LFS hsm_release $f
1918         check_hsm_flags $f "0x0000000d"
1919         # set no retry action mode
1920         cdt_set_no_retry
1921         do_node $CLIENT2 "$f 10" &
1922         local pid=$!
1923         sleep 3
1924         echo 'Hi!' > $f
1925         [[ $? == 0 ]] && error "Update during exec of released file must fail"
1926         wait $pid
1927         [[ $? == 0 ]] || error "Execution failed during run"
1928         cmp /bin/sleep $f
1929         [[ $? == 0 ]] || error "Binary overwritten during exec"
1930
1931         # cleanup
1932         # remove no try action mode
1933         cdt_clear_no_retry
1934         check_hsm_flags $f "0x00000009"
1935
1936         copytool_cleanup
1937 }
1938 run_test 30c "Update during exec of released file must fail"
1939
1940 restore_and_check_size() {
1941         local f=$1
1942         local fid=$2
1943         local s=$(stat -c "%s" $f)
1944         local n=$s
1945         local st=$(get_hsm_flags $f)
1946         local err=0
1947         local cpt=0
1948         $LFS hsm_restore $f
1949         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
1950         do
1951                 n=$(stat -c "%s" $f)
1952                 # we echo in both cases to show stat is not
1953                 # hang
1954                 if [[ $n != $s ]]; then
1955                         echo "size seen is $n != $s"
1956                         err=1
1957                 else
1958                         echo "size seen is right: $n == $s"
1959                 fi
1960                 st=$(get_hsm_flags $f)
1961                 sleep 10
1962                 cpt=$((cpt + 1))
1963         done
1964         if [[ $cpt -lt 10 ]]; then
1965                 echo " "done
1966         else
1967                 echo " restore is too long"
1968                 wait_request_state $fid RESTORE SUCCEED
1969         fi
1970         return $err
1971 }
1972
1973 test_31a() {
1974         # test needs a running copytool
1975         copytool_setup
1976
1977         mkdir -p $DIR/$tdir
1978
1979         make_archive $tdir/$tfile
1980         local f=$DIR/$tdir/$tfile
1981         import_file $tdir/$tfile $f
1982         local fid=$($LFS path2fid $f)
1983         HSM_ARCHIVE_PURGE=false copytool_setup
1984
1985         restore_and_check_size $f $fid
1986         local err=$?
1987
1988         [[ $err -eq 0 ]] || error "File size changed during restore"
1989
1990         copytool_cleanup
1991 }
1992 run_test 31a "Import a large file and check size during restore"
1993
1994
1995 test_31b() {
1996         # test needs a running copytool
1997         copytool_setup
1998
1999         mkdir -p $DIR/$tdir
2000
2001         local f=$DIR/$tdir/$tfile
2002         local fid=$(make_large_for_progress $f)
2003         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2004         wait_request_state $fid ARCHIVE SUCCEED
2005         $LFS hsm_release $f
2006
2007         restore_and_check_size $f $fid
2008         local err=$?
2009
2010         [[ $err -eq 0 ]] || error "File size changed during restore"
2011
2012         copytool_cleanup
2013 }
2014 run_test 31b "Restore a large unaligned file and check size during restore"
2015
2016 test_31c() {
2017         # test needs a running copytool
2018         copytool_setup
2019
2020         mkdir -p $DIR/$tdir
2021
2022         local f=$DIR/$tdir/$tfile
2023         local fid=$(make_large_for_progress_aligned $f)
2024         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2025         wait_request_state $fid ARCHIVE SUCCEED
2026         $LFS hsm_release $f
2027
2028         restore_and_check_size $f $fid
2029         local err=$?
2030
2031         [[ $err -eq 0 ]] || error "File size changed during restore"
2032
2033         copytool_cleanup
2034 }
2035 run_test 31c "Restore a large aligned file and check size during restore"
2036
2037 test_33() {
2038         # test needs a running copytool
2039         copytool_setup
2040
2041         mkdir -p $DIR/$tdir
2042
2043         local f=$DIR/$tdir/$tfile
2044         local fid=$(make_large_for_progress $f)
2045         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2046         wait_request_state $fid ARCHIVE SUCCEED
2047         $LFS hsm_release $f
2048
2049         md5sum $f >/dev/null &
2050         local pid=$!
2051         wait_request_state $fid RESTORE STARTED
2052
2053         kill -15 $pid
2054         sleep 1
2055
2056         # Check restore trigger process was killed
2057         local killed=$(ps -o pid,comm hp $pid >/dev/null)
2058
2059         $LFS hsm_cancel $f
2060
2061         wait_request_state $fid RESTORE CANCELED
2062         wait_request_state $fid CANCEL SUCCEED
2063
2064         [ -z $killed ] ||
2065                 error "Cannot kill process waiting for restore ($killed)"
2066
2067         copytool_cleanup
2068 }
2069 run_test 33 "Kill a restore waiting process"
2070
2071 test_34() {
2072         # test needs a running copytool
2073         copytool_setup
2074
2075         mkdir -p $DIR/$tdir
2076
2077         local f=$DIR/$tdir/$tfile
2078         local fid=$(make_large_for_progress $f)
2079         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2080         wait_request_state $fid ARCHIVE SUCCEED
2081         $LFS hsm_release $f
2082
2083         md5sum $f >/dev/null &
2084         local pid=$!
2085         wait_request_state $fid RESTORE STARTED
2086
2087         rm $f || error "rm $f failed"
2088         # rm must not block during restore
2089         wait_request_state $fid RESTORE STARTED
2090
2091         wait_request_state $fid RESTORE SUCCEED
2092         # check md5sum pgm finished
2093         local there=$(ps -o pid,comm hp $pid >/dev/null)
2094         [[ -z $there ]] || error "Restore initiator does not exit"
2095
2096         local rc=$(wait $pid)
2097         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2098
2099         copytool_cleanup
2100 }
2101 run_test 34 "Remove file during restore"
2102
2103 test_35() {
2104         # test needs a running copytool
2105         copytool_setup
2106
2107         mkdir -p $DIR/$tdir
2108
2109         local f=$DIR/$tdir/$tfile
2110         local f1=$DIR/$tdir/$tfile-1
2111         local fid=$(make_large_for_progress $f)
2112         local fid1=$(copy_file /etc/passwd $f1)
2113         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2114         wait_request_state $fid ARCHIVE SUCCEED
2115         $LFS hsm_release $f
2116
2117         md5sum $f >/dev/null &
2118         local pid=$!
2119         wait_request_state $fid RESTORE STARTED
2120
2121         mv $f1 $f || error "mv $f1 $f failed"
2122         # mv must not block during restore
2123         wait_request_state $fid RESTORE STARTED
2124
2125         wait_request_state $fid RESTORE SUCCEED
2126         # check md5sum pgm finished
2127         local there=$(ps -o pid,comm hp $pid >/dev/null)
2128         [[ -z $there ]] || error "Restore initiator does not exit"
2129
2130         local rc=$(wait $pid)
2131         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2132
2133         fid2=$(path2fid $f)
2134         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2135
2136         copytool_cleanup
2137 }
2138 run_test 35 "Overwrite file during restore"
2139
2140 test_36() {
2141         # test needs a running copytool
2142         copytool_setup
2143
2144         mkdir -p $DIR/$tdir
2145
2146         local f=$DIR/$tdir/$tfile
2147         local fid=$(make_large_for_progress $f)
2148         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2149         wait_request_state $fid ARCHIVE SUCCEED
2150         $LFS hsm_release $f
2151
2152         md5sum $f >/dev/null &
2153         local pid=$!
2154         wait_request_state $fid RESTORE STARTED
2155
2156         mv $f $f.new
2157         # rm must not block during restore
2158         wait_request_state $fid RESTORE STARTED
2159
2160         wait_request_state $fid RESTORE SUCCEED
2161         # check md5sum pgm finished
2162         local there=$(ps -o pid,comm hp $pid >/dev/null)
2163         [[ -z $there ]] ||
2164                 error "Restore initiator does not exit"
2165
2166         local rc=$(wait $pid)
2167         [[ $rc -eq 0 ]] ||
2168                 error "Restore initiator failed with $rc"
2169
2170         copytool_cleanup
2171 }
2172 run_test 36 "Move file during restore"
2173
2174 multi_archive() {
2175         local prefix=$1
2176         local count=$2
2177         local n=""
2178
2179         for n in $(seq 1 $count); do
2180                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2181         done
2182         echo "$count archive requests submitted"
2183 }
2184
2185 test_40() {
2186         local stream_count=4
2187         local file_count=100
2188         mkdir -p $DIR/$tdir
2189         local f=$DIR/$tdir/$tfile
2190         local i=""
2191         local p=""
2192         local fid=""
2193
2194         for i in $(seq 1 $file_count); do
2195                 for p in $(seq 1 $stream_count); do
2196                         fid=$(copy_file /etc/hosts $f.$p.$i)
2197                 done
2198         done
2199         copytool_setup
2200         # to be sure wait_all_done will not be mislead by previous tests
2201         cdt_purge
2202         wait_for_grace_delay
2203         typeset -a pids
2204         # start archive streams in background (archive files in parallel)
2205         for p in $(seq 1 $stream_count); do
2206                 multi_archive $f.$p $file_count &
2207                 pids[$p]=$!
2208         done
2209         echo -n  "Wait for all requests being enqueued..."
2210         wait ${pids[*]}
2211         echo OK
2212         wait_all_done 100
2213         copytool_cleanup
2214 }
2215 run_test 40 "Parallel archive requests"
2216
2217 test_52() {
2218         # test needs a running copytool
2219         copytool_setup
2220
2221         mkdir -p $DIR/$tdir
2222         local f=$DIR/$tdir/$tfile
2223         local fid=$(copy_file /etc/motd $f 1)
2224
2225         $LFS hsm_archive $f || error "could not archive file"
2226         wait_request_state $fid ARCHIVE SUCCEED
2227         check_hsm_flags $f "0x00000009"
2228
2229         multiop_bg_pause $f O_c || error "multiop failed"
2230         local MULTIPID=$!
2231
2232         mds_evict_client
2233         client_up || client_up || true
2234
2235         kill -USR1 $MULTIPID
2236         wait $MULTIPID || error "multiop close failed"
2237
2238         check_hsm_flags $f "0x0000000b"
2239
2240         copytool_cleanup
2241 }
2242 run_test 52 "Opened for write file on an evicted client should be set dirty"
2243
2244 test_53() {
2245         # test needs a running copytool
2246         copytool_setup
2247
2248         mkdir -p $DIR/$tdir
2249         local f=$DIR/$tdir/$tfile
2250         local fid=$(copy_file /etc/motd $f 1)
2251
2252         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2253                 error "could not archive file"
2254         wait_request_state $fid ARCHIVE SUCCEED
2255         check_hsm_flags $f "0x00000009"
2256
2257         multiop_bg_pause $f o_c || error "multiop failed"
2258         MULTIPID=$!
2259
2260         mds_evict_client
2261         client_up || client_up || true
2262
2263         kill -USR1 $MULTIPID
2264         wait $MULTIPID || error "multiop close failed"
2265
2266         check_hsm_flags $f "0x00000009"
2267
2268         copytool_cleanup
2269 }
2270 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2271
2272 test_54() {
2273         # test needs a running copytool
2274         copytool_setup
2275
2276         mkdir -p $DIR/$tdir
2277         local f=$DIR/$tdir/$tfile
2278         local fid=$(make_small $f)
2279
2280         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2281                 error "could not archive file"
2282         wait_request_state $fid ARCHIVE STARTED
2283
2284         check_hsm_flags $f "0x00000001"
2285
2286         # Avoid coordinator resending this request as soon it has failed.
2287         cdt_set_no_retry
2288
2289         echo "foo" >> $f
2290         sync
2291         wait_request_state $fid ARCHIVE FAILED
2292
2293         check_hsm_flags $f "0x00000003"
2294
2295         cdt_clear_no_retry
2296         copytool_cleanup
2297 }
2298 run_test 54 "Write during an archive cancels it"
2299
2300 test_55() {
2301         # test needs a running copytool
2302         copytool_setup
2303
2304         mkdir -p $DIR/$tdir
2305         local f=$DIR/$tdir/$tfile
2306         local fid=$(make_small $f)
2307
2308         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2309                 error "could not archive file"
2310         wait_request_state $fid ARCHIVE STARTED
2311
2312         check_hsm_flags $f "0x00000001"
2313
2314         # Avoid coordinator resending this request as soon it has failed.
2315         cdt_set_no_retry
2316
2317         $TRUNCATE $f 1024 || error "truncate failed"
2318         sync
2319         wait_request_state $fid ARCHIVE FAILED
2320
2321         check_hsm_flags $f "0x00000003"
2322
2323         cdt_clear_no_retry
2324         copytool_cleanup
2325 }
2326 run_test 55 "Truncate during an archive cancels it"
2327
2328 test_56() {
2329         # test needs a running copytool
2330         copytool_setup
2331
2332         mkdir -p $DIR/$tdir
2333         local f=$DIR/$tdir/$tfile
2334         local fid=$(make_large_for_progress $f)
2335
2336         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2337                 error "could not archive file"
2338         wait_request_state $fid ARCHIVE STARTED
2339
2340         check_hsm_flags $f "0x00000001"
2341
2342         # Change metadata and sync to be sure we are not changing only
2343         # in memory.
2344         chmod 644 $f
2345         chgrp sys $f
2346         sync
2347         wait_request_state $fid ARCHIVE SUCCEED
2348
2349         check_hsm_flags $f "0x00000009"
2350
2351         copytool_cleanup
2352 }
2353 run_test 56 "Setattr during an archive is ok"
2354
2355 test_57() {
2356         # Need one client for I/O, one for request
2357         needclients 2 || return 0
2358
2359         # test needs a running copytool
2360         copytool_setup
2361
2362         mkdir -p $DIR/$tdir
2363         local f=$DIR/$tdir/test_archive_remote
2364         # Create a file on a remote node
2365         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2366                 "count=2 conv=fsync"
2367
2368         # And archive it
2369         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2370                 error "hsm_archive failed"
2371         local fid=$(path2fid $f)
2372         wait_request_state $fid ARCHIVE SUCCEED
2373
2374         # Release and implicit restore it
2375         do_node $CLIENT2 "$LFS hsm_release $f" ||
2376                 error "hsm_release failed"
2377         do_node $CLIENT2 "md5sum $f" ||
2378                 error "hsm_restore failed"
2379
2380         wait_request_state $fid RESTORE SUCCEED
2381
2382         copytool_cleanup
2383 }
2384 run_test 57 "Archive a file with dirty cache on another node"
2385
2386 truncate_released_file() {
2387         local src_file=$1
2388         local trunc_to=$2
2389
2390         local sz=$(stat -c %s $src_file)
2391         local f=$DIR/$tdir/$tfile
2392         local fid=$(copy_file $1 $f)
2393         local ref=$f-ref
2394         cp $f $f-ref
2395
2396         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2397                 error "could not archive file"
2398         wait_request_state $fid ARCHIVE SUCCEED
2399
2400         $LFS hsm_release $f || error "could not release file"
2401
2402         $TRUNCATE $f $trunc_to || error "truncate failed"
2403         sync
2404
2405         local sz1=$(stat -c %s $f)
2406         [[ $sz1 == $trunc_to ]] ||
2407                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2408
2409         $LFS hsm_state $f
2410         check_hsm_flags $f "0x0000000b"
2411
2412         local state=$(get_request_state $fid RESTORE)
2413         [[ "$state" == "SUCCEED" ]] ||
2414                 error "truncate $sz does not trig restore, state = $state"
2415
2416         $TRUNCATE $ref $trunc_to
2417         cmp $ref $f || error "file data wrong after truncate"
2418
2419         rm -f $f $f-ref
2420 }
2421
2422 test_58() {
2423         # test needs a running copytool
2424         copytool_setup
2425
2426         mkdir -p $DIR/$tdir
2427
2428         local sz=$(stat -c %s /etc/passwd)
2429
2430         echo "truncate up from $sz to $((sz*2))"
2431         truncate_released_file /etc/passwd $((sz*2))
2432
2433         echo "truncate down from $sz to $((sz/2))"
2434         truncate_released_file /etc/passwd $((sz/2))
2435
2436         echo "truncate to 0"
2437         truncate_released_file /etc/passwd 0
2438
2439         copytool_cleanup
2440 }
2441 run_test 58 "Truncate a released file will trigger restore"
2442
2443 test_90() {
2444         file_count=57
2445         mkdir -p $DIR/$tdir
2446         local f=$DIR/$tdir/$tfile
2447         local FILELIST=/tmp/filelist.txt
2448         local i=""
2449
2450         rm -f $FILELIST
2451         for i in $(seq 1 $file_count); do
2452                 fid=$(copy_file /etc/hosts $f.$i)
2453                 echo $f.$i >> $FILELIST
2454         done
2455         copytool_setup
2456         # to be sure wait_all_done will not be mislead by previous tests
2457         cdt_purge
2458         wait_for_grace_delay
2459         $LFS hsm_archive --filelist $FILELIST ||
2460                 error "cannot archive a file list"
2461         wait_all_done 100
2462         $LFS hsm_release --filelist $FILELIST ||
2463                 error "cannot release a file list"
2464         $LFS hsm_restore --filelist $FILELIST ||
2465                 error "cannot restore a file list"
2466         wait_all_done 100
2467         copytool_cleanup
2468 }
2469 run_test 90 "Archive/restore a file list"
2470
2471 double_verify_reset_hsm_param() {
2472         local p=$1
2473         echo "Testing $HSM_PARAM.$p"
2474         local val=$(get_hsm_param $p)
2475         local save=$val
2476         local val2=$(($val * 2))
2477         set_hsm_param $p $val2
2478         val=$(get_hsm_param $p)
2479         [[ $val == $val2 ]] ||
2480                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
2481         echo "Set $p to 0 must failed"
2482         set_hsm_param $p 0
2483         local rc=$?
2484         # restore value
2485         set_hsm_param $p $save
2486
2487         if [[ $rc == 0 ]]; then
2488                 error "we must not be able to set $HSM_PARAM.$p to 0"
2489         fi
2490 }
2491
2492 test_100() {
2493         double_verify_reset_hsm_param loop_period
2494         double_verify_reset_hsm_param grace_delay
2495         double_verify_reset_hsm_param active_request_timeout
2496         double_verify_reset_hsm_param max_requests
2497         double_verify_reset_hsm_param default_archive_id
2498 }
2499 run_test 100 "Set coordinator /proc tunables"
2500
2501 test_102() {
2502         cdt_disable
2503         cdt_enable
2504         cdt_restart
2505 }
2506 run_test 102 "Verify coordinator control"
2507
2508 test_103() {
2509         # test needs a running copytool
2510         copytool_setup
2511
2512         local i=""
2513         local fid=""
2514
2515         mkdir -p $DIR/$tdir
2516         for i in $(seq 1 20); do
2517                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
2518         done
2519         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
2520
2521         cdt_purge
2522
2523         echo "Current requests"
2524         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2525                         $HSM_PARAM.actions |\
2526                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
2527
2528         [[ -z "$res" ]] || error "Some request have not been canceled"
2529
2530         copytool_cleanup
2531 }
2532 run_test 103 "Purge all requests"
2533
2534 DATA=CEA
2535 DATAHEX='[434541]'
2536 test_104() {
2537         # test needs a running copytool
2538         copytool_setup
2539
2540         mkdir -p $DIR/$tdir
2541         local f=$DIR/$tdir/$tfile
2542         local fid=$(make_large_for_progress $f)
2543         # if cdt is on, it can serve too quickly the request
2544         cdt_disable
2545         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
2546         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2547                         $HSM_PARAM.actions |\
2548                         grep $fid | cut -f16 -d=")
2549         cdt_enable
2550
2551         [[ "$data1" == "$DATAHEX" ]] ||
2552                 error "Data field in records is ($data1) and not ($DATAHEX)"
2553
2554         copytool_cleanup
2555 }
2556 run_test 104 "Copy tool data field"
2557
2558 test_105() {
2559         mkdir -p $DIR/$tdir
2560         local i=""
2561
2562         cdt_disable
2563         for i in $(seq -w 1 10); do
2564                 cp /etc/passwd $DIR/$tdir/$i
2565                 $LFS hsm_archive $DIR/$tdir/$i
2566         done
2567         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2568                         $HSM_PARAM.actions |\
2569                         grep WAITING | wc -l")
2570         cdt_restart
2571         cdt_disable
2572         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2573                         $HSM_PARAM.actions |\
2574                         grep WAITING | wc -l")
2575         cdt_enable
2576         cdt_purge
2577         [[ "$reqcnt1" == "$reqcnt2" ]] ||
2578                 error "Requests count after shutdown $reqcnt2 != "\
2579                       "before shutdown $reqcnt1"
2580 }
2581 run_test 105 "Restart of coordinator"
2582
2583 test_106() {
2584         # test needs a running copytool
2585         copytool_setup
2586
2587         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
2588                 get_client_uuid $MOUNT | cut -d' ' -f2)
2589         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2590                 grep $uuid)
2591         copytool_cleanup
2592         [[ ! -z "$agent" ]] || error "My uuid $uuid not found in agent list"
2593         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2594                 grep $uuid)
2595         [[ -z "$agent" ]] ||
2596                 error "My uuid $uuid still found in agent list,"\
2597                       " after copytool shutdown"
2598         copytool_setup
2599         local agent=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.agents |
2600                 grep $uuid)
2601         copytool_cleanup
2602         [[ ! -z "$agent" ]] ||
2603                 error "My uuid $uuid not found in agent list after"\
2604                       " copytool restart"
2605 }
2606 run_test 106 "Copytool register/unregister"
2607
2608 test_107() {
2609         # test needs a running copytool
2610         copytool_setup
2611         # create and archive file
2612         mkdir -p $DIR/$tdir
2613         local f1=$DIR/$tdir/$tfile
2614         local fid=$(copy_file /etc/passwd $f1)
2615         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2616         wait_request_state $fid ARCHIVE SUCCEED
2617         # shutdown and restart MDS
2618         fail $SINGLEMDS
2619         # check the copytool still gets messages from MDT
2620         local f2=$DIR/$tdir/2
2621         local fid=$(copy_file /etc/passwd $f2)
2622         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2623         # main check of this sanity: this request MUST succeed
2624         wait_request_state $fid ARCHIVE SUCCEED
2625         copytool_cleanup
2626 }
2627 run_test 107 "Copytool re-register after MDS restart"
2628
2629 policy_set_and_test()
2630 {
2631         local change="$1"
2632         local target="$2"
2633         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
2634         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2635         [[ "$policy" == "$target" ]] ||
2636                 error "Wrong policy after '$change': '$policy' != '$target'"
2637 }
2638
2639 test_109() {
2640         # to force default policy setting if error
2641         CDT_POLICY_HAD_CHANGED=true
2642
2643         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2644         local default="NonBlockingRestore [NoRetryAction]"
2645         [[ "$policy" == "$default" ]] ||
2646                 error "default policy has changed,"\
2647                       " '$policy' != '$default' update the test"
2648         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
2649         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
2650         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
2651         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
2652         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
2653         # useless bacause we know but safer for futur changes to use real value
2654         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2655         echo "Next set_param must failed"
2656         policy_set_and_test "wrong" "$policy"
2657
2658         # return to default
2659         echo "Back to default policy"
2660         cdt_set_sanity_policy
2661 }
2662 run_test 109 "Policy display/change"
2663
2664 test_110a() {
2665         # test needs a running copytool
2666         copytool_setup
2667
2668         mkdir -p $DIR/$tdir
2669
2670         copy2archive /etc/passwd $tdir/$tfile
2671
2672         local f=$DIR/$tdir/$tfile
2673         import_file $tdir/$tfile $f
2674         local fid=$(path2fid $f)
2675
2676         cdt_set_non_blocking_restore
2677         md5sum $f
2678         local st=$?
2679
2680         # cleanup
2681         wait_request_state $fid RESTORE SUCCEED
2682         cdt_clear_non_blocking_restore
2683
2684         # Test result
2685         [[ $st == 1 ]] ||
2686                 error "md5sum returns $st != 1, "\
2687                         "should also perror ENODATA (No data available)"
2688
2689         copytool_cleanup
2690 }
2691 run_test 110a "Non blocking restore policy (import case)"
2692
2693 test_110b() {
2694         # test needs a running copytool
2695         copytool_setup
2696
2697         mkdir -p $DIR/$tdir
2698         local f=$DIR/$tdir/$tfile
2699         local fid=$(copy_file /etc/passwd $f)
2700         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2701         wait_request_state $fid ARCHIVE SUCCEED
2702         $LFS hsm_release $f
2703
2704         cdt_set_non_blocking_restore
2705         md5sum $f
2706         local st=$?
2707
2708         # cleanup
2709         wait_request_state $fid RESTORE SUCCEED
2710         cdt_clear_non_blocking_restore
2711
2712         # Test result
2713         [[ $st == 1 ]] ||
2714                 error "md5sum returns $st != 1, "\
2715                         "should also perror ENODATA (No data available)"
2716
2717         copytool_cleanup
2718 }
2719 run_test 110b "Non blocking restore policy (release case)"
2720
2721 test_111a() {
2722         # test needs a running copytool
2723         copytool_setup
2724
2725         mkdir -p $DIR/$tdir
2726         copy2archive /etc/passwd $tdir/$tfile
2727
2728         local f=$DIR/$tdir/$tfile
2729
2730         import_file $tdir/$tfile $f
2731         local fid=$(path2fid $f)
2732
2733         cdt_set_no_retry
2734
2735         copytool_remove_backend $fid
2736
2737         $LFS hsm_restore $f
2738         wait_request_state $fid RESTORE FAILED
2739         local st=$?
2740
2741         # cleanup
2742         cdt_clear_no_retry
2743
2744         # Test result
2745         [[ $st == 0 ]] || error "Restore does not failed"
2746
2747         copytool_cleanup
2748 }
2749 run_test 111a "No retry policy (import case), restore will error"\
2750               " (No such file or directory)"
2751
2752 test_111b() {
2753         # test needs a running copytool
2754         copytool_setup
2755
2756         mkdir -p $DIR/$tdir
2757         local f=$DIR/$tdir/$tfile
2758         local fid=$(copy_file /etc/passwd $f)
2759         cdt_set_no_retry
2760         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2761         wait_request_state $fid ARCHIVE SUCCEED
2762         $LFS hsm_release $f
2763
2764         copytool_remove_backend $fid
2765
2766         $LFS hsm_restore $f
2767         wait_request_state $fid RESTORE FAILED
2768         local st=$?
2769
2770         # cleanup
2771         cdt_clear_no_retry
2772
2773         # Test result
2774         [[ $st == 0 ]] || error "Restore does not failed"
2775
2776         copytool_cleanup
2777 }
2778 run_test 111b "No retry policy (release case), restore will error"\
2779               " (No such file or directory)"
2780
2781 test_112() {
2782         # test needs a running copytool
2783         copytool_setup
2784
2785         mkdir -p $DIR/$tdir
2786         local f=$DIR/$tdir/$tfile
2787         local fid=$(copy_file /etc/passwd $f)
2788         cdt_disable
2789         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2790         local l=$($LFS hsm_action $f)
2791         echo $l
2792         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
2793
2794         # cleanup
2795         cdt_enable
2796         wait_request_state $fid ARCHIVE SUCCEED
2797
2798         # Test result
2799         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
2800
2801         copytool_cleanup
2802 }
2803 run_test 112 "State of recorded request"
2804
2805 test_200() {
2806         # test needs a running copytool
2807         copytool_setup
2808
2809         mkdir -p $DIR/$tdir
2810         local f=$DIR/$tdir/$tfile
2811         local fid=$(make_large_for_cancel $f)
2812         # test with cdt on is made in test_221
2813         cdt_disable
2814         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2815         $LFS hsm_cancel $f
2816         cdt_enable
2817         wait_request_state $fid ARCHIVE CANCELED
2818         wait_request_state $fid CANCEL SUCCEED
2819
2820         copytool_cleanup
2821 }
2822 run_test 200 "Register/Cancel archive"
2823
2824 test_201() {
2825         # test needs a running copytool
2826         copytool_setup
2827
2828         mkdir -p $DIR/$tdir
2829         local f=$DIR/$tdir/$tfile
2830         make_archive $tdir/$tfile
2831         import_file $tdir/$tfile $f
2832         local fid=$(path2fid $f)
2833
2834         # test with cdt on is made in test_222
2835         cdt_disable
2836         $LFS hsm_restore $f
2837         $LFS hsm_cancel $f
2838         cdt_enable
2839         wait_request_state $fid RESTORE CANCELED
2840         wait_request_state $fid CANCEL SUCCEED
2841
2842         copytool_cleanup
2843 }
2844 run_test 201 "Register/Cancel restore"
2845
2846 test_202() {
2847         # test needs a running copytool
2848         copytool_setup
2849
2850         mkdir -p $DIR/$tdir
2851         local f=$DIR/$tdir/$tfile
2852         local fid=$(make_large_for_progress $f)
2853         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2854         wait_request_state $fid ARCHIVE SUCCEED
2855
2856         cdt_disable
2857         $LFS hsm_remove $f
2858         $LFS hsm_cancel $f
2859         cdt_enable
2860         wait_request_state $fid REMOVE CANCELED
2861
2862         copytool_cleanup
2863 }
2864 run_test 202 "Register/Cancel remove"
2865
2866 test_220() {
2867         # test needs a running copytool
2868         copytool_setup
2869
2870         mkdir -p $DIR/$tdir
2871
2872         local f=$DIR/$tdir/$tfile
2873         local fid=$(copy_file /etc/passwd $f)
2874
2875         changelog_setup
2876
2877         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2878         wait_request_state $fid ARCHIVE SUCCEED
2879
2880         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2881         changelog_cleanup
2882
2883         local target=0x0
2884         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2885
2886         copytool_cleanup
2887 }
2888 run_test 220 "Changelog for archive"
2889
2890 test_221() {
2891         # test needs a running copytool
2892         copytool_setup
2893
2894         mkdir -p $DIR/$tdir
2895
2896         local f=$DIR/$tdir/$tfile
2897         local fid=$(make_large_for_cancel $f)
2898
2899         changelog_setup
2900
2901         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2902         wait_request_state $fid ARCHIVE STARTED
2903         $LFS hsm_cancel $f
2904         wait_request_state $fid ARCHIVE CANCELED
2905         wait_request_state $fid CANCEL SUCCEED
2906
2907         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2908
2909         local target=0x7d
2910         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2911
2912         cleanup
2913 }
2914 run_test 221 "Changelog for archive canceled"
2915
2916 test_222a() {
2917         # test needs a running copytool
2918         copytool_setup
2919
2920         mkdir -p $DIR/$tdir
2921         copy2archive /etc/passwd $tdir/$tfile
2922
2923         local f=$DIR/$tdir/$tfile
2924         import_file $tdir/$tfile $f
2925         local fid=$(path2fid $f)
2926
2927         changelog_setup
2928
2929         $LFS hsm_restore $f
2930         wait_request_state $fid RESTORE SUCCEED
2931
2932         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2933
2934         local target=0x80
2935         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2936
2937         cleanup
2938 }
2939 run_test 222a "Changelog for explicit restore"
2940
2941 test_222b() {
2942         # test needs a running copytool
2943         copytool_setup
2944
2945         mkdir -p $DIR/$tdir
2946         local f=$DIR/$tdir/$tfile
2947         local fid=$(copy_file /etc/passwd $f)
2948
2949         changelog_setup
2950         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2951         wait_request_state $fid ARCHIVE SUCCEED
2952         $LFS hsm_release $f
2953
2954         md5sum $f
2955
2956         wait_request_state $fid RESTORE SUCCEED
2957
2958         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2959
2960         local target=0x80
2961         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2962
2963         cleanup
2964 }
2965 run_test 222b "Changelog for implicit restore"
2966
2967 test_223a() {
2968         # test needs a running copytool
2969         copytool_setup
2970
2971         mkdir -p $DIR/$tdir
2972
2973         local f=$DIR/$tdir/$tfile
2974         make_archive $tdir/$tfile
2975
2976         changelog_setup
2977
2978         import_file $tdir/$tfile $f
2979         local fid=$(path2fid $f)
2980
2981         $LFS hsm_restore $f
2982         wait_request_state $fid RESTORE STARTED
2983         $LFS hsm_cancel $f
2984         wait_request_state $fid RESTORE CANCELED
2985         wait_request_state $fid CANCEL SUCCEED
2986
2987         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2988
2989         local target=0xfd
2990         [[ $flags == $target ]] ||
2991                 error "Changelog flag is $flags not $target"
2992
2993         cleanup
2994 }
2995 run_test 223a "Changelog for restore canceled (import case)"
2996
2997 test_223b() {
2998         # test needs a running copytool
2999         copytool_setup
3000
3001         mkdir -p $DIR/$tdir
3002
3003         local f=$DIR/$tdir/$tfile
3004         local fid=$(make_large_for_progress $f)
3005
3006         changelog_setup
3007         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3008         wait_request_state $fid ARCHIVE SUCCEED
3009         $LFS hsm_release $f
3010         $LFS hsm_restore $f
3011         wait_request_state $fid RESTORE STARTED
3012         $LFS hsm_cancel $f
3013         wait_request_state $fid RESTORE CANCELED
3014         wait_request_state $fid CANCEL SUCCEED
3015
3016         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3017
3018         local target=0xfd
3019         [[ $flags == $target ]] ||
3020                 error "Changelog flag is $flags not $target"
3021
3022         cleanup
3023 }
3024 run_test 223b "Changelog for restore canceled (release case)"
3025
3026 test_224() {
3027         # test needs a running copytool
3028         copytool_setup
3029
3030         mkdir -p $DIR/$tdir
3031
3032         local f=$DIR/$tdir/$tfile
3033         local fid=$(copy_file /etc/passwd $f)
3034
3035         changelog_setup
3036         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3037         wait_request_state $fid ARCHIVE SUCCEED
3038
3039         $LFS hsm_remove $f
3040         wait_request_state $fid REMOVE SUCCEED
3041
3042         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
3043
3044         local target=0x200
3045         [[ $flags == $target ]] ||
3046                 error "Changelog flag is $flags not $target"
3047
3048         cleanup
3049 }
3050 run_test 224 "Changelog for remove"
3051
3052 test_225() {
3053         # test needs a running copytool
3054         copytool_setup
3055
3056         # test is not usable because remove request is too fast
3057         # so it is always finished before cancel can be done ...
3058         echo "Test disabled"
3059         copytool_cleanup
3060         return 0
3061
3062         mkdir -p $DIR/$tdir
3063         local f=$DIR/$tdir/$tfile
3064         local fid=$(make_large_for_progress $f)
3065
3066         changelog_setup
3067         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3068         wait_request_state $fid ARCHIVE SUCCEED
3069
3070         # if cdt is on, it can serve too quickly the request
3071         cdt_disable
3072         $LFS hsm_remove $f
3073         $LFS hsm_cancel $f
3074         cdt_enable
3075         wait_request_state $fid REMOVE CANCELED
3076         wait_request_state $fid CANCEL SUCCEED
3077
3078         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3079         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
3080                 tail -n 1 | awk '{print $5}')
3081
3082         local target=0x27d
3083         [[ $flags == $target ]] ||
3084                 error "Changelog flag is $flags not $target"
3085
3086         cleanup
3087 }
3088 run_test 225 "Changelog for remove canceled"
3089
3090 test_226() {
3091         # test needs a running copytool
3092         copytool_setup
3093
3094         mkdir -p $DIR/$tdir
3095
3096         local f1=$DIR/$tdir/$tfile-1
3097         local f2=$DIR/$tdir/$tfile-2
3098         local f3=$DIR/$tdir/$tfile-3
3099         local fid1=$(copy_file /etc/passwd $f1)
3100         local fid2=$(copy_file /etc/passwd $f2)
3101         copy_file /etc/passwd $f3
3102
3103         changelog_setup
3104         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3105         wait_request_state $fid1 ARCHIVE SUCCEED
3106
3107         $LFS hsm_archive $f2
3108         wait_request_state $fid2 ARCHIVE SUCCEED
3109
3110         rm $f1 || error "rm $f1 failed"
3111
3112         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
3113
3114         local target=0x3
3115         [[ $flags == $target ]] ||
3116                 error "Changelog flag is $flags not $target"
3117
3118         mv $f3 $f2 || error "mv $f3 $f2 failed"
3119
3120         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3121
3122         target=0x3
3123         [[ $flags == $target ]] ||
3124                 error "Changelog flag is $flags not $target"
3125
3126         cleanup
3127 }
3128 run_test 226 "changelog for last rm/mv with exiting archive"
3129
3130 check_flags_changes() {
3131         local f=$1
3132         local fid=$2
3133         local hsm_flag=$3
3134         local fst=$4
3135         local cnt=$5
3136
3137         local target=0x280
3138         $LFS hsm_set --$hsm_flag $f ||
3139                 error "Cannot set $hsm_flag on $f"
3140         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3141         local seen=${#flags[*]}
3142         cnt=$((fst + cnt))
3143         [[ $seen == $cnt ]] ||
3144                 error "set $hsm_flag: Changelog events $seen != $cnt"
3145         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3146                 error "set $hsm_flag: Changelog flags are "\
3147                         "${flags[$((cnt - 1))]} not $target"
3148
3149         $LFS hsm_clear --$hsm_flag $f ||
3150                 error "Cannot clear $hsm_flag on $f"
3151         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3152         seen=${#flags[*]}
3153         cnt=$(($cnt + 1))
3154         [[ $cnt == $seen ]] ||
3155                 error "clear $hsm_flag: Changelog events $seen != $cnt"
3156
3157         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3158                 error "clear $hsm_flag: Changelog flag is "\
3159                         "${flags[$((cnt - 1))]} not $target"
3160 }
3161
3162 test_227() {
3163         # test needs a running copytool
3164         copytool_setup
3165         changelog_setup
3166
3167         mkdir -p $DIR/$tdir
3168         typeset -a flags
3169
3170         for i in norelease noarchive exists archived
3171         do
3172                 local f=$DIR/$tdir/$tfile-$i
3173                 local fid=$(copy_file /etc/passwd $f)
3174                 check_flags_changes $f $fid $i 0 1
3175         done
3176
3177         f=$DIR/$tdir/$tfile---lost
3178         fid=$(copy_file /etc/passwd $f)
3179         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3180         wait_request_state $fid ARCHIVE SUCCEED
3181         check_flags_changes $f $fid lost 3 1
3182
3183         cleanup
3184 }
3185 run_test 227 "changelog when explicit setting of HSM flags"
3186
3187 test_228() {
3188         # test needs a running copytool
3189         copytool_setup
3190
3191         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=1 conv=sync ||
3192                 error "creating $DIR/$tfile"
3193         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
3194         wait_request_state $(path2fid $DIR/$tfile) ARCHIVE SUCCEED
3195
3196         $LFS hsm_release $DIR/$tfile
3197         check_hsm_flags $DIR/$tfile "0x0000000d"
3198
3199         filefrag $DIR/$tfile | grep " 1 extent found" ||
3200                 error "filefrag on released file must return only one extent"
3201
3202         # only newer versions of cp detect sparse files by stat/FIEMAP
3203         # (LU-2580)
3204         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
3205                 error "copying $DIR/$tfile"
3206         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
3207
3208         $LFS hsm_release $DIR/$tfile
3209         check_hsm_flags $DIR/$tfile "0x0000000d"
3210
3211         mkdir $DIR/$tdir
3212
3213         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
3214                 error "tar failed"
3215         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
3216                 error "comparing untarred $DIR/$tfile"
3217
3218         copytool_cleanup
3219 }
3220 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
3221
3222 test_250() {
3223         # test needs a running copytool
3224         copytool_setup
3225
3226         mkdir -p $DIR/$tdir
3227         local maxrequest=$(get_hsm_param max_requests)
3228         local rqcnt=$(($maxrequest * 3))
3229         local i=""
3230
3231         cdt_disable
3232         for i in $(seq -w 1 $rqcnt); do
3233                 rm -f $DIR/$tdir/$i
3234                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
3235         done
3236         # we do it in 2 steps, so all requests arrive at the same time
3237         for i in $(seq -w 1 $rqcnt); do
3238                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3239         done
3240         cdt_enable
3241         local cnt=$rqcnt
3242         local wt=$rqcnt
3243         while [[ $cnt != 0 || $wt != 0 ]]; do
3244                 sleep 1
3245                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3246                         $HSM_PARAM.actions |\
3247                         grep STARTED | grep -v CANCEL | wc -l")
3248                 [[ $cnt -le $maxrequest ]] ||
3249                         error "$cnt > $maxrequest too many started requests"
3250                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
3251                         $HSM_PARAM.actions |\
3252                         grep WAITING | wc -l")
3253                 echo "max=$maxrequest started=$cnt waiting=$wt"
3254         done
3255
3256         copytool_cleanup
3257 }
3258 run_test 250 "Coordinator max request"
3259
3260 test_251() {
3261         # test needs a running copytool
3262         copytool_setup
3263
3264         mkdir -p $DIR/$tdir
3265         local f=$DIR/$tdir/$tfile
3266         local fid=$(make_large_for_cancel $f)
3267
3268         cdt_disable
3269         # to have a short test
3270         local old_to=$(get_hsm_param active_request_timeout)
3271         set_hsm_param active_request_timeout 4
3272         # to be sure the cdt will wake up frequently so
3273         # it will be able to cancel the "old" request
3274         local old_loop=$(get_hsm_param loop_period)
3275         set_hsm_param loop_period 2
3276         cdt_enable
3277
3278         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3279         wait_request_state $fid ARCHIVE STARTED
3280         sleep 5
3281         wait_request_state $fid ARCHIVE CANCELED
3282
3283         set_hsm_param active_request_timeout $old_to
3284         set_hsm_param loop_period $old_loop
3285
3286         copytool_cleanup
3287 }
3288 run_test 251 "Coordinator request timeout"
3289
3290 test_300() {
3291         # the only way to test ondisk conf is to restart MDS ...
3292         echo "Stop coordinator and remove coordinator state at mount"
3293         # stop coordinator
3294         cdt_shutdown
3295         # clean on disk conf set by default
3296         cdt_clear_mount_state
3297         cdt_check_state stopped
3298
3299         # check cdt still off after umount/remount
3300         fail $SINGLEMDS
3301         cdt_check_state stopped
3302
3303         echo "Set coordinator start at mount, and start coordinator"
3304         cdt_set_mount_state enabled
3305
3306         # check cdt is on
3307         cdt_check_state enabled
3308
3309         # check cdt still on after umount/remount
3310         fail $SINGLEMDS
3311         cdt_check_state enabled
3312
3313         # we are back to original state (cdt started at mount)
3314 }
3315 run_test 300 "On disk coordinator state kept between MDT umount/mount"
3316
3317 test_301() {
3318         local ai=$(get_hsm_param default_archive_id)
3319         local new=$((ai + 1))
3320
3321         set_hsm_param default_archive_id $new -P
3322         fail $SINGLEMDS
3323         local res=$(get_hsm_param default_archive_id)
3324
3325         # clear value
3326         set_hsm_param default_archive_id "" "-P -d"
3327
3328         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3329 }
3330 run_test 301 "HSM tunnable are persistent"
3331
3332 test_302() {
3333         local ai=$(get_hsm_param default_archive_id)
3334         local new=$((ai + 1))
3335
3336         # stop coordinator
3337         cdt_shutdown
3338
3339         set_hsm_param default_archive_id $new -P
3340         fail $SINGLEMDS
3341
3342         # check cdt is on
3343         cdt_check_state enabled
3344
3345         local res=$(get_hsm_param default_archive_id)
3346
3347         # clear value
3348         set_hsm_param default_archive_id "" "-P -d"
3349
3350         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3351 }
3352 run_test 302 "HSM tunnable are persistent when CDT is off"
3353
3354 copytool_cleanup
3355
3356 complete $SECONDS
3357 check_and_cleanup_lustre
3358 exit_status