Whamcloud - gitweb
LU-12511 utils: Move utilies specific values out of Lustre UAPI headers
[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 ONLY=${ONLY:-"$*"}
11
12 LUSTRE=${LUSTRE:-$(dirname $0)/..}
13 . $LUSTRE/tests/test-framework.sh
14 init_test_env $@
15 init_logging
16
17 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT "
18 if $SHARED_KEY; then
19 # bug number for skipped tests: LU-9795 LU-9795
20         ALWAYS_EXCEPT+="        13      402b "
21 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
22 fi
23
24 # Skip tests for PPC that fail frequently
25 if [[ $(uname -m) = ppc64 ]]; then
26         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
27         ALWAYS_EXCEPT+=" 1a       1b       1d       1e       12c      12f "
28         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
29         ALWAYS_EXCEPT+=" 12g      12h      12m      12n      12o      12p "
30         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
31         ALWAYS_EXCEPT+=" 12q      21       22       23       24a      24b "
32         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
33         ALWAYS_EXCEPT+=" 24d      24e      24f      25b      30c      37 "
34         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
35         ALWAYS_EXCEPT+=" 57       58       90       110b     111b     113 "
36         # bug number:    LU-12251 LU-12251 LU-12251 LU-12251 LU-12251 LU-12251
37         ALWAYS_EXCEPT+=" 222b     222d     228      260a     260b     260c "
38         # bug number:    LU-12252 LU-12252 LU-12252 LU-12252 LU-12252 LU-12252
39         ALWAYS_EXCEPT+=" 220A     220a     221      222a     222c     223a "
40         # bug number:    LU-12252 LU-12252 LU-12252 LU-12252 LU-12252 LU-12252
41         ALWAYS_EXCEPT+=" 223b     224A     224a     226      227      600"
42         # bug number:    LU-12252 LU-12252 LU-12252 LU-12252 LU-12252 LU-12252
43         ALWAYS_EXCEPT+=" 601      602      603      604      605 "
44 fi
45
46 build_test_filter
47
48 [ -n "$FILESET" ] && skip "Not functional for FILESET set"
49
50 OPENFILE=${OPENFILE:-openfile}
51 MMAP_CAT=${MMAP_CAT:-mmap_cat}
52 MOUNT_2=${MOUNT_2:-"yes"}
53 FAIL_ON_ERROR=false
54
55 # script only handles up to 10 MDTs (because of MDT_PREFIX)
56 [ $MDSCOUNT -gt 9 ] &&
57         error "script cannot handle more than 9 MDTs, please fix"
58
59 check_and_setup_lustre
60
61 if [[ $MDS1_VERSION -lt $(version_code 2.4.53) ]]; then
62         skip_env "Need MDS version at least 2.4.53"
63 fi
64
65 # $RUNAS_ID may get set incorrectly somewhere else
66 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
67         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!"
68 fi
69 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
70 if getent group nobody; then
71         GROUP=nobody
72 elif getent group nogroup; then
73         GROUP=nogroup
74 else
75         error "No generic nobody group"
76 fi
77
78 # if there is no CLIENT1 defined, some tests can be ran on localhost
79 CLIENT1=${CLIENT1:-$HOSTNAME}
80 # if CLIENT2 doesn't exist then use CLIENT1 instead
81 # All tests should use CLIENT2 with MOUNT2 only therefore it will work if
82 # $CLIENT2 == CLIENT1
83 # Exception is the test which need two separate nodes
84 CLIENT2=${CLIENT2:-$CLIENT1}
85
86 #
87 # In order to test multiple remote HSM agents, a new facet type named "AGT" and
88 # the following associated variables are added:
89 #
90 # AGTCOUNT: number of agents
91 # AGTDEV{N}: target HSM mount point (root path of the backend)
92 # agt{N}_HOST: hostname of the agent agt{N}
93 # SINGLEAGT: facet of the single agent
94 #
95 # The number of agents is initialized as the number of remote client nodes.
96 # By default, only single copytool is started on a remote client/agent. If there
97 # was no remote client, then the copytool will be started on the local client.
98 #
99 init_agt_vars() {
100         local n
101         local agent
102
103         export AGTCOUNT=${AGTCOUNT:-$((CLIENTCOUNT - 1))}
104         [[ $AGTCOUNT -gt 0 ]] || AGTCOUNT=1
105
106         export SHARED_DIRECTORY=${SHARED_DIRECTORY:-$TMP}
107         if [[ $CLIENTCOUNT -gt 1 ]] &&
108                 ! check_shared_dir $SHARED_DIRECTORY $CLIENTS; then
109                 skip_env "SHARED_DIRECTORY should be accessible"\
110                          "on all client nodes"
111                 exit 0
112         fi
113
114         # We used to put the HSM archive in $SHARED_DIRECTORY but that
115         # meant NFS issues could hose sanity-hsm sessions. So now we
116         # use $TMP instead.
117         for n in $(seq $AGTCOUNT); do
118                 eval export AGTDEV$n=\$\{AGTDEV$n:-"$TMP/arc$n"\}
119                 agent=CLIENT$((n + 1))
120                 if [[ -z "${!agent}" ]]; then
121                         [[ $CLIENTCOUNT -eq 1 ]] && agent=CLIENT1 ||
122                                 agent=CLIENT2
123                 fi
124                 eval export agt${n}_HOST=\$\{agt${n}_HOST:-${!agent}\}
125         done
126
127         export SINGLEAGT=${SINGLEAGT:-agt1}
128
129         export HSMTOOL=${HSMTOOL:-"lhsmtool_posix"}
130         export HSMTOOL_VERBOSE=${HSMTOOL_VERBOSE:-""}
131         export HSMTOOL_UPDATE_INTERVAL=${HSMTOOL_UPDATE_INTERVAL:=""}
132         export HSMTOOL_EVENT_FIFO=${HSMTOOL_EVENT_FIFO:=""}
133         export HSMTOOL_TESTDIR
134
135         HSM_ARCHIVE_NUMBER=2
136
137         # The test only support up to 10 MDTs
138         MDT_PREFIX="mdt.$FSNAME-MDT000"
139         HSM_PARAM="${MDT_PREFIX}0.hsm"
140
141         # archive is purged at copytool setup
142         HSM_ARCHIVE_PURGE=true
143
144         # Don't allow copytool error upon start/setup
145         HSMTOOL_NOERROR=false
146 }
147
148 # Get the backend root path for the given agent facet.
149 copytool_device() {
150         local facet=$1
151         local dev=AGTDEV$(facet_number $facet)
152
153         echo -n ${!dev}
154 }
155
156 get_mdt_devices() {
157         local mdtno
158         # get MDT device for each mdc
159         for mdtno in $(seq 1 $MDSCOUNT); do
160                 local idx=$(($mdtno - 1))
161                 MDT[$idx]=$($LCTL get_param -n \
162                         mdc.$FSNAME-MDT000${idx}-mdc-*.mds_server_uuid |
163                         awk '{gsub(/_UUID/,""); print $1}' | head -n1)
164         done
165 }
166
167 search_copytools() {
168         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
169         do_nodesv $hosts "libtool execute pgrep -x $HSMTOOL"
170 }
171
172 kill_copytools() {
173         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
174
175         echo "Killing existing copytools on $hosts"
176         do_nodesv $hosts "libtool execute killall -q $HSMTOOL" || true
177         copytool_continue "$hosts"
178 }
179
180 wait_copytools() {
181         local hosts=${1:-$(facet_active_host $SINGLEAGT)}
182         local wait_timeout=200
183         local wait_start=$SECONDS
184         local wait_end=$((wait_start + wait_timeout))
185         local sleep_time=100000 # 0.1 second
186
187         while ((SECONDS < wait_end)); do
188                 if ! search_copytools $hosts; then
189                         echo "copytools stopped in $((SECONDS - wait_start))s"
190                         return 0
191                 fi
192
193                 echo "copytools still running on $hosts"
194                 usleep $sleep_time
195                 [ $sleep_time -lt 32000000 ] && # 3.2 seconds
196                         sleep_time=$(bc <<< "$sleep_time * 2")
197         done
198
199         # try to dump Copytool's stack
200         do_nodesv $hosts "echo 1 >/proc/sys/kernel/sysrq ; " \
201                          "echo t >/proc/sysrq-trigger"
202
203         echo "copytools failed to stop in ${wait_timeout}s"
204
205         return 1
206 }
207
208 copytool_monitor_setup() {
209         local facet=${1:-$SINGLEAGT}
210         local agent=$(facet_active_host $facet)
211
212         local cmd="mktemp --tmpdir=/tmp -d ${TESTSUITE}.${TESTNAME}.XXXX"
213         local test_dir=$(do_node $agent "$cmd") ||
214                 error "Failed to create tempdir on $agent"
215         export HSMTOOL_MONITOR_DIR=$test_dir
216
217         # Create the fifo and a monitor (cat dies when copytool dies)
218         do_node $agent "mkfifo -m 0644 $test_dir/fifo" ||
219                 error "failed to create copytool fifo on $agent"
220         cmd="cat $test_dir/fifo > $test_dir/events &"
221         cmd+=" echo \\\$! > $test_dir/monitor_pid"
222
223         # This background subshell nonsense is required when pdsh/ssh decides
224         # to wait for the cat process to exit on the remote client
225         (do_node $agent "$cmd") &
226         export HSMTOOL_MONITOR_PDSH=$!
227
228         # Slightly racy, but just making a best-effort to catch obvious
229         # problems.
230         sleep 1
231         do_node $agent "stat $HSMTOOL_MONITOR_DIR/monitor_pid 2>&1 > /dev/null"
232         if [ $? != 0 ]; then
233                 error "Failed to start copytool monitor on $agent"
234         fi
235 }
236
237 fid2archive()
238 {
239         local fid="$1"
240
241         case "$HSMTOOL" in
242         lhsmtool_posix)
243                 printf "%s" "$(hsm_root)/*/*/*/*/*/*/$fid"
244                 ;;
245         esac
246 }
247
248 get_copytool_event_log() {
249         local facet=${1:-$SINGLEAGT}
250         local agent=$(facet_active_host $facet)
251
252         [ -z "$HSMTOOL_MONITOR_DIR" ] &&
253                 error "Can't get event log: No monitor directory!"
254
255         do_node $agent "cat $HSMTOOL_MONITOR_DIR/events" ||
256                 error "Could not collect event log from $agent"
257 }
258
259 copytool_suspend() {
260         local agents=${1:-$(facet_active_host $SINGLEAGT)}
261
262         stack_trap \
263                 "do_nodesv $agents libtool execute pkill -CONT -x '$HSMTOOL' || true" EXIT
264         do_nodesv $agents "libtool execute pkill -STOP -x $HSMTOOL" || return 0
265         echo "Copytool is suspended on $agents"
266 }
267
268 copytool_continue() {
269         local agents=${1:-$(facet_active_host $SINGLEAGT)}
270
271         do_nodesv $agents "libtool execute pkill -CONT -x $HSMTOOL" || return 0
272         echo "Copytool is continued on $agents"
273 }
274
275 copytool_remove_backend() {
276         local fid=$1
277         local be=$(do_facet $SINGLEAGT find "$(hsm_root)" -name $fid)
278         echo "Remove from backend: $fid = $be"
279         do_facet $SINGLEAGT rm -f $be
280 }
281
282 file_creation_failure() {
283         local cmd=$1
284         local file=$2
285         local err=$3
286
287         case $err in
288         28)
289                 df $MOUNT $MOUNT2 >&2
290                 error "Not enough space to create $file with $cmd"
291                 ;;
292         *)
293                 error "cannot create $file with $cmd, status=$err"
294                 ;;
295         esac
296 }
297
298 # Creates a file using dd
299 create_file() {
300         local file=$1
301         local bs=$2
302         local count=$3
303         local conv=$4
304         local source=${5:-/dev/zero}
305         local args=""
306         local err
307
308         if [ -n "$conv" ]; then
309                 args+=" conv=$conv"
310         fi
311
312         # Create the directory in case it does not exist
313         mkdir -p "$(dirname "$file")"
314         # Delete the file in case it already exist
315         rm -f "$file"
316
317         if dd if="$source" of="$file" count="$count" bs="$bs" $args; then
318                 path2fid "$file" || error "cannot get FID of '$file'"
319         else
320                 err=$?
321                 echo "cannot create file '$file'" >&2;
322                 # Let the caller decide what to do on error
323                 return $err;
324         fi
325 }
326
327 create_empty_file() {
328         create_file "${1/$DIR/$DIR2}" 1M 0 ||
329                 file_creation_failure dd "${1/$DIR/$DIR2}" $?
330 }
331
332 create_small_file() {
333         local source_file=/dev/urandom
334         local count=1
335         local bs=1M
336         local conv=${2:-fsync}
337
338         create_file "${1/$DIR/$DIR2}" $bs $count $conv $source_file ||
339                 file_creation_failure dd "${1/$DIR/$DIR2}" $?
340 }
341
342 create_small_sync_file() {
343         create_small_file "$1" sync
344 }
345
346 create_archive_file() {
347         local file="$(hsm_root)/$1"
348         local count=${2:-39}
349         local source=/dev/urandom
350
351         # Create the counterpart directory of the archive
352         do_facet "$SINGLEAGT" mkdir -p "$(dirname "$file")" ||
353                 error "cannot create archive directory '$(dirname "$file")'"
354
355         do_facet "$SINGLEAGT" dd if=$source of="$file" bs=1M count=$count ||
356                 error "cannot create archive file '$file'"
357 }
358
359 copy2archive() {
360         local hsm_root="$(hsm_root)"
361         local file="$hsm_root/$2"
362
363         stack_trap "do_facet $SINGLEAGT rm -rf '$hsm_root'" EXIT
364         do_facet $SINGLEAGT mkdir -p "$(dirname "$file")" ||
365             error "mkdir '$(dirname "$file")' failed"
366         do_facet $SINGLEAGT cp -p "$1" "$file" ||
367                 error "cannot copy '$1' to '$file'"
368 }
369
370 get_hsm_param() {
371         local param=$1
372         local val=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.$param)
373         echo $val
374 }
375
376 set_test_state() {
377         local cmd=$1
378         local target=$2
379         mdts_set_param "" hsm_control "$cmd"
380         mdts_check_param hsm_control "$target" 10
381 }
382
383
384 cdt_set_no_retry() {
385         mdts_set_param "" hsm.policy "+NRA"
386         CDT_POLICY_HAD_CHANGED=true
387 }
388
389 cdt_clear_no_retry() {
390         mdts_set_param "" hsm.policy "-NRA"
391         CDT_POLICY_HAD_CHANGED=true
392 }
393
394 cdt_set_non_blocking_restore() {
395         mdts_set_param "" hsm.policy "+NBR"
396         CDT_POLICY_HAD_CHANGED=true
397 }
398
399 cdt_clear_non_blocking_restore() {
400         mdts_set_param "" hsm.policy "-NBR"
401         CDT_POLICY_HAD_CHANGED=true
402 }
403
404 cdt_clear_mount_state() {
405         mdts_set_param "-P -d" hsm_control ""
406 }
407
408 cdt_disable() {
409         set_test_state disabled disabled
410 }
411
412 cdt_enable() {
413         set_test_state enabled enabled
414 }
415
416 cdt_shutdown() {
417         set_test_state shutdown stopped
418 }
419
420 cdt_purge() {
421         set_test_state purge enabled
422 }
423
424 cdt_restart() {
425         cdt_shutdown
426         cdt_enable
427         cdt_set_sanity_policy
428 }
429
430
431 get_hsm_archive_id() {
432         local f=$1
433         local st
434         st=$($LFS hsm_state $f)
435         [[ $? == 0 ]] || error "$LFS hsm_state $f failed"
436
437         local ar=$(echo $st | grep -oP '(?<=archive_id:).*')
438         echo $ar
439 }
440
441 check_hsm_flags_user() {
442         local f=$1
443         local fl=$2
444
445         local st=$(get_hsm_flags $f user)
446         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
447 }
448
449 copy_file() {
450         local f=
451
452         if [[ -d $2 ]]; then
453                 f=$2/$(basename $1)
454         else
455                 f=$2
456         fi
457
458         if [[ "$3" != 1 ]]; then
459                 f=${f/$DIR/$DIR2}
460         fi
461         rm -f $f
462         cp $1 $f || file_creation_failure cp $f $?
463
464         path2fid $f || error "cannot get fid on $f"
465 }
466
467 # Delete any file bigger than 10M under $MOUNT and wait for deletes to complete
468 #
469 # Note that this might lead to surprising behaviours such as deleting an
470 # important file for the currently running test
471 delete_large_files() {
472         printf "Deleting large files...\n" >&2
473         find $MOUNT -size +10M -delete
474         wait_delete_completed
475 }
476
477 get_request_state() {
478         local fid=$1
479         local request=$2
480
481         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
482                 "awk '/'$fid'.*action='$request'/ {print \\\$13}' | cut -f2 -d="
483 }
484
485 get_request_count() {
486         local fid=$1
487         local request=$2
488
489         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
490                 "awk -vn=0 '/'$fid'.*action='$request'/ {n++}; END {print n}'"
491 }
492
493 # Ensure the number of HSM request for a given FID is correct
494 # assert_request_count FID REQUEST_TYPE COUNT [ERROR_MSG]
495 assert_request_count() {
496         local request_count=$(get_request_count $1 $2)
497         local default_error_msg=("expected $3 '$2' request(s) for '$1', found "
498                                 "'$request_count'")
499         [ $request_count -eq $3 ] || error "${4:-"${default_error_msg[@]}"}"
500 }
501
502 wait_all_done() {
503         local timeout=$1
504         local fid=$2
505
506         local cmd="$LCTL get_param -n $HSM_PARAM.actions"
507         [[ -n $fid ]] && cmd+=" | grep '$fid'"
508         cmd+=" | egrep 'WAITING|STARTED'"
509
510         wait_result $SINGLEMDS "$cmd" "" $timeout ||
511                 error "requests did not complete"
512 }
513
514 wait_for_grace_delay() {
515         local val=$(get_hsm_param grace_delay)
516         sleep $val
517 }
518
519 wait_for_loop_period() {
520         local val=$(get_hsm_param loop_period)
521         sleep $val
522 }
523
524 parse_json_event() {
525         local raw_event=$1
526
527         # python2.6 in EL6 includes an internal json module
528         local json_parser='import json; import fileinput;'
529         json_parser+=' print "\n".join(["local %s=\"%s\"" % tuple for tuple in '
530         json_parser+='json.loads([line for line in '
531         json_parser+='fileinput.input()][0]).items()])'
532
533         echo $raw_event | python -c "$json_parser"
534 }
535
536 get_agent_by_uuid_mdt() {
537         local uuid=$1
538         local mdtidx=$2
539         local mds=mds$(($mdtidx + 1))
540         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
541                  grep $uuid"
542 }
543
544 check_agent_registered_by_mdt() {
545         local uuid=$1
546         local mdtidx=$2
547         local mds=mds$(($mdtidx + 1))
548         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
549         if [[ ! -z "$agent" ]]; then
550                 echo "found agent $agent on $mds"
551         else
552                 error "uuid $uuid not found in agent list on $mds"
553         fi
554 }
555
556 check_agent_unregistered_by_mdt() {
557         local uuid=$1
558         local mdtidx=$2
559         local mds=mds$(($mdtidx + 1))
560         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
561         if [[ -z "$agent" ]]; then
562                 echo "uuid not found in agent list on $mds"
563         else
564                 error "uuid found in agent list on $mds: $agent"
565         fi
566 }
567
568 check_agent_registered() {
569         local uuid=$1
570         local mdsno
571         for mdsno in $(seq 1 $MDSCOUNT); do
572                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
573         done
574 }
575
576 check_agent_unregistered() {
577         local uuid=$1
578         local mdsno
579         for mdsno in $(seq 1 $MDSCOUNT); do
580                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
581         done
582 }
583
584 get_agent_uuid() {
585         local agent=${1:-$(facet_active_host $SINGLEAGT)}
586
587         # Lustre mount-point is mandatory and last parameter on
588         # copytool cmd-line.
589         local mntpnt=$(do_rpc_nodes $agent libtool execute ps -C $HSMTOOL -o args= |
590                        awk '{print $NF}')
591         [ -n "$mntpnt" ] || error "Found no Agent or with no mount-point "\
592                                   "parameter"
593         do_rpc_nodes $agent get_client_uuid $mntpnt | cut -d' ' -f2
594 }
595
596 # initiate variables
597 init_agt_vars
598
599 # populate MDT device array
600 get_mdt_devices
601
602 # cleanup from previous bad setup
603 kill_copytools
604
605 # for recovery tests, coordinator needs to be started at mount
606 # so force it
607 # the lustre conf must be without hsm on (like for sanity.sh)
608 echo "Set HSM on and start"
609 cdt_set_mount_state enabled
610 cdt_check_state enabled
611
612 echo "Set sanity-hsm HSM policy"
613 cdt_set_sanity_policy
614
615 # finished requests are quickly removed from list
616 set_hsm_param grace_delay 10
617
618 CLIENT_NIDS=( $($LCTL list_nids all) )
619
620 test_1A() { # was test_1
621         mkdir -p $DIR/$tdir
622         chmod 777 $DIR/$tdir
623
624         local f=$DIR/$tdir/$tfile
625         $RUNAS touch $f
626
627         # User flags
628         check_hsm_flags_user $f "0x00000000"
629
630         $RUNAS $LFS hsm_set --norelease $f ||
631                 error "user could not change hsm flags"
632         check_hsm_flags_user $f "0x00000010"
633
634         $RUNAS $LFS hsm_clear --norelease $f ||
635                 error "user could not clear hsm flags"
636         check_hsm_flags_user $f "0x00000000"
637
638         # User could not change those flags...
639         $RUNAS $LFS hsm_set --exists $f &&
640                 error "user should not set this flag"
641         check_hsm_flags_user $f "0x00000000"
642
643         # ...but root can
644         $LFS hsm_set --exists $f ||
645                 error "root could not change hsm flags"
646         check_hsm_flags_user $f "0x00000001"
647
648         $LFS hsm_clear --exists $f ||
649                 error "root could not clear hsm state"
650         check_hsm_flags_user $f "0x00000000"
651
652 }
653 run_test 1A "lfs hsm flags root/non-root access"
654
655 test_1a() {
656         local f=$DIR/$tdir/$tfile
657         local fid=$(create_small_file $f)
658
659         copytool setup
660
661         $LFS hsm_archive $f || error "could not archive file"
662         wait_request_state $fid ARCHIVE SUCCEED
663
664         # Release and check states
665         $LFS hsm_release $f || error "could not release file"
666         echo -n "Verifying released state: "
667         check_hsm_flags $f "0x0000000d"
668
669         $MMAP_CAT $f > /dev/null || error "failed mmap & cat release file"
670 }
671 run_test 1a "mmap & cat a HSM released file"
672
673 test_1bde_base() {
674         local f=$1
675         rm -f $f
676
677         dd if=/dev/urandom of=$f bs=1M count=1 conv=sync ||
678                 error "failed to create file"
679         local fid=$(path2fid $f)
680
681         copytool setup
682
683         echo "archive $f"
684         $LFS hsm_archive $f || error "could not archive file"
685         wait_request_state $fid ARCHIVE SUCCEED
686
687         echo "release $f"
688         $LFS hsm_release $f || error "could not release file"
689         echo "verify released state: "
690         check_hsm_flags $f "0x0000000d" && echo "pass"
691
692         echo "restore $f"
693         $LFS hsm_restore $f || error "could not restore file"
694         wait_request_state $fid RESTORE SUCCEED
695         echo "verify restored state: "
696         check_hsm_flags $f "0x00000009" && echo "pass"
697 }
698
699 test_1b() {
700         mkdir -p $DIR/$tdir
701         $LFS setstripe -E 1M -S 1M -E 64M -c 2 -E -1 -c 4 $DIR/$tdir ||
702                 error "failed to set default stripe"
703         local f=$DIR/$tdir/$tfile
704
705         test_1bde_base $f
706 }
707 run_test 1b "Archive, Release and Restore composite file"
708
709 test_1c() {
710         mkdir -p $DIR/$tdir
711         chmod 777 $DIR/$tdir
712
713         local f=$DIR/$tdir/$tfile
714         $RUNAS touch $f
715
716         # Test whether we can set the maximum archive number.
717         local LOCAL_HSM_ARCHIVE_NUMBER=32
718         $LFS hsm_set --exists --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f ||
719                 error "root could not change hsm flags"
720         check_hsm_flags_user $f "0x00000001"
721         echo "verifying archive number is $LOCAL_HSM_ARCHIVE_NUMBER"
722         local st=$(get_hsm_archive_id $f)
723         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
724                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
725
726         # Test whether setting archive number 0 results in no change.
727         $LFS hsm_set --exists --archive-id 0 $f ||
728                 error "root could not change hsm flags"
729         check_hsm_flags_user $f "0x00000001"
730         echo "verifying archive number is still $LOCAL_HSM_ARCHIVE_NUMBER"
731         st=$(get_hsm_archive_id $f)
732         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
733                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
734
735         LOCAL_HSM_ARCHIVE_NUMBER=33
736         if [ $(lustre_version_code client) -ge $(version_code 2.11.56) ] &&
737            [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.11.56) ]; then
738                 # lustre in the new version supports unlimited archiveID.
739                 # Test whether setting archive number > 32 is supported
740                 $LFS hsm_set --exists --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f ||
741                         error "archive ID $LOCAL_HSM_ARCHIVE_NUMBER too large?"
742                 check_hsm_flags_user $f "0x00000001"
743
744                 echo "verifying archive number is $LOCAL_HSM_ARCHIVE_NUMBER"
745                 st=$(get_hsm_archive_id $f)
746                 [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
747                         error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
748         else
749                 # old client or old mds can only support at most 32 archiveID
750                 # test whether setting archive number > 32 results in error.
751                 $LFS hsm_set --exists --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f &&
752                         error "bitmap archive number is larger than 32"
753                 check_hsm_flags_user $f "0x00000001"
754         fi
755
756         # Test whether setting archive number 16 and archived flag.
757         LOCAL_HSM_ARCHIVE_NUMBER=16
758         $LFS hsm_set --exists --archived \
759              --archive-id $LOCAL_HSM_ARCHIVE_NUMBER $f ||
760             error "root could not change hsm flags"
761         check_hsm_flags_user $f "0x00000009"
762         echo "verifying archive number is $LOCAL_HSM_ARCHIVE_NUMBER"
763         st=$(get_hsm_archive_id $f)
764         [[ $st == $LOCAL_HSM_ARCHIVE_NUMBER ]] ||
765                 error "wrong archive number, $st != $LOCAL_HSM_ARCHIVE_NUMBER"
766 }
767 run_test 1c "Check setting archive-id in lfs hsm_set"
768
769 test_1d() {
770         [ $MDS1_VERSION -lt $(version_code 2.10.59) ] &&
771                 skip "need MDS version at least 2.10.59"
772
773         mkdir -p $DIR/$tdir
774         $LFS setstripe -E 1M -L mdt -E -1 -c 2 $DIR/$tdir ||
775                 error "failed to set default stripe"
776         local f=$DIR/$tdir/$tfile
777
778         test_1bde_base $f
779 }
780 run_test 1d "Archive, Release and Restore DoM file"
781
782 test_1e() {
783         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
784                 skip "skipped for lustre < $SEL_VER"
785
786         mkdir -p $DIR/$tdir
787         $LFS setstripe -E 1G -z 64M -E 10G -z 512M -E -1 -z 1G $DIR/$tdir ||
788                 error "failed to set default stripe"
789         local comp_file=$DIR/$tdir/$tfile
790
791         test_1bde_base $comp_file
792
793         local flg_opts="--comp-start 0 -E 64M --comp-flags init"
794         local found=$($LFS find $flg_opts $comp_file | wc -l)
795         [ $found -eq 1 ] || error "1st component not found"
796
797         flg_opts="--comp-start 64M -E 1G --comp-flags extension"
798         found=$($LFS find $flg_opts $comp_file | wc -l)
799         [ $found -eq 1 ] || error "2nd component not found"
800
801         flg_opts="--comp-start 1G -E 1G --comp-flags ^init"
802         found=$($LFS find $flg_opts $comp_file | wc -l)
803         [ $found -eq 1 ] || error "3rd component not found"
804
805         flg_opts="--comp-start 1G -E 10G --comp-flags extension"
806         found=$($LFS find $flg_opts $comp_file | wc -l)
807         [ $found -eq 1 ] || error "4th component not found"
808
809         flg_opts="--comp-start 10G -E 10G --comp-flags ^init"
810         found=$($LFS find $flg_opts $comp_file | wc -l)
811         [ $found -eq 1 ] || error "5th component not found"
812
813         flg_opts="--comp-start 10G -E EOF --comp-flags extension"
814         found=$($LFS find $flg_opts $comp_file | wc -l)
815         [ $found -eq 1 ] || error "6th component not found"
816
817         sel_layout_sanity $comp_file 6
818 }
819 run_test 1e "Archive, Release and Restore SEL file"
820
821 test_2() {
822         local f=$DIR/$tdir/$tfile
823
824         create_empty_file "$f"
825         # New files are not dirty
826         check_hsm_flags $f "0x00000000"
827
828         # For test, we simulate an archived file.
829         $LFS hsm_set --exists $f || error "user could not change hsm flags"
830         check_hsm_flags $f "0x00000001"
831
832         # chmod do not put the file dirty
833         chmod 600 $f || error "could not chmod test file"
834         check_hsm_flags $f "0x00000001"
835
836         # chown do not put the file dirty
837         chown $RUNAS_ID $f || error "could not chown test file"
838         check_hsm_flags $f "0x00000001"
839
840         # truncate put the file dirty
841         $TRUNCATE $f 1 || error "could not truncate test file"
842         check_hsm_flags $f "0x00000003"
843
844         $LFS hsm_clear --dirty $f || error "could not clear hsm flags"
845         check_hsm_flags $f "0x00000001"
846 }
847 run_test 2 "Check file dirtyness when doing setattr"
848
849 test_3() {
850         mkdir -p $DIR/$tdir
851         f=$DIR/$tdir/$tfile
852
853         # New files are not dirty
854         cp -p /etc/passwd $f
855         check_hsm_flags $f "0x00000000"
856
857         # For test, we simulate an archived file.
858         $LFS hsm_set --exists $f ||
859                 error "user could not change hsm flags"
860         check_hsm_flags $f "0x00000001"
861
862         # Reading a file, does not set dirty
863         cat $f > /dev/null || error "could not read file"
864         check_hsm_flags $f "0x00000001"
865
866         # Open for write without modifying data, does not set dirty
867         openfile -f O_WRONLY $f || error "could not open test file"
868         check_hsm_flags $f "0x00000001"
869
870         # Append to a file sets it dirty
871         cp -p /etc/passwd $f.append || error "could not create file"
872         $LFS hsm_set --exists $f.append ||
873                 error "user could not change hsm flags"
874         dd if=/etc/passwd of=$f.append bs=1 count=3\
875            conv=notrunc oflag=append status=noxfer ||
876                 file_creation_failure dd $f.append $?
877         check_hsm_flags $f.append "0x00000003"
878
879         # Modify a file sets it dirty
880         cp -p /etc/passwd $f.modify || error "could not create file"
881         $LFS hsm_set --exists $f.modify ||
882                 error "user could not change hsm flags"
883         dd if=/dev/zero of=$f.modify bs=1 count=3\
884            conv=notrunc status=noxfer ||
885                 file_creation_failure dd $f.modify $?
886         check_hsm_flags $f.modify "0x00000003"
887
888         # Open O_TRUNC sets dirty
889         cp -p /etc/passwd $f.trunc || error "could not create file"
890         $LFS hsm_set --exists $f.trunc ||
891                 error "user could not change hsm flags"
892         cp /etc/group $f.trunc || error "could not override a file"
893         check_hsm_flags $f.trunc "0x00000003"
894
895         # Mmapped a file sets dirty
896         cp -p /etc/passwd $f.mmap || error "could not create file"
897         $LFS hsm_set --exists $f.mmap ||
898                 error "user could not change hsm flags"
899         multiop $f.mmap OSMWUc || error "could not mmap a file"
900         check_hsm_flags $f.mmap "0x00000003"
901 }
902 run_test 3 "Check file dirtyness when opening for write"
903
904 test_4() {
905         local f=$DIR/$tdir/$tfile
906         local fid=$(create_small_file $f)
907
908         $LFS hsm_cancel $f
909         local st=$(get_request_state $fid CANCEL)
910         [[ -z "$st" ]] || error "hsm_cancel must not be registered (state=$st)"
911 }
912 run_test 4 "Useless cancel must not be registered"
913
914 test_8() {
915         # test needs a running copytool
916         copytool setup
917
918         mkdir -p $DIR/$tdir
919         local f=$DIR/$tdir/$tfile
920         local fid=$(copy_file /etc/passwd $f)
921         $LFS hsm_archive $f
922         wait_request_state $fid ARCHIVE SUCCEED
923
924         check_hsm_flags $f "0x00000009"
925 }
926 run_test 8 "Test default archive number"
927
928 test_9A() { # was test_9
929         # we do not use the default one to be sure
930         local archive_id=$((HSM_ARCHIVE_NUMBER + 1))
931         copytool setup --archive-id $archive_id
932
933         # give time for CT to register with MDTs
934         sleep $(($MDSCOUNT*2))
935         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
936         check_agent_registered $uuid
937
938         mkdir -p $DIR/$tdir
939         local f=$DIR/$tdir/$tfile
940         local fid=$(copy_file /etc/passwd $f)
941         $LFS hsm_archive --archive $archive_id $f
942         wait_request_state $fid ARCHIVE SUCCEED
943
944         check_hsm_flags $f "0x00000009"
945 }
946 run_test 9A "Use of explicit archive number, with dedicated copytool"
947
948 test_9a() {
949         needclients 3 || return 0
950
951         local n
952         local file
953         local fid
954
955         # start all of the copytools
956         for n in $(seq $AGTCOUNT); do
957                 copytool setup --facet agt$n
958         done
959
960         # archive files
961         for n in $(seq $AGTCOUNT); do
962                 file=$DIR/$tdir/$tfile.$n
963                 fid=$(create_small_file $file)
964
965                 $LFS hsm_archive $file || error "could not archive file $file"
966                 wait_request_state $fid ARCHIVE SUCCEED
967                 check_hsm_flags $file "0x00000009"
968         done
969 }
970 run_test 9a "Multiple remote agents"
971
972 test_10a() {
973         # test needs a running copytool
974         copytool setup
975
976         mkdir -p $DIR/$tdir/d1
977         local f=$DIR/$tdir/$tfile
978         local fid=$(copy_file /etc/hosts $f)
979         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
980                 error "hsm_archive failed"
981         wait_request_state $fid ARCHIVE SUCCEED
982
983         local hsm_root="$(copytool_device $SINGLEAGT)"
984         local archive="$(do_facet $SINGLEAGT \
985                          find "$hsm_root" -name "$fid" -print0)"
986         [ -n "$archive" ] || error "fid '$fid' not in archive '$hsm_root'"
987
988         echo "Verifying content"
989         do_facet $SINGLEAGT diff $f $archive || error "archived file differs"
990         echo "Verifying hsm state "
991         check_hsm_flags $f "0x00000009"
992
993         echo "Verifying archive number is $HSM_ARCHIVE_NUMBER"
994         local st=$(get_hsm_archive_id $f)
995         [[ $st == $HSM_ARCHIVE_NUMBER ]] ||
996                 error "Wrong archive number, $st != $HSM_ARCHIVE_NUMBER"
997 }
998 run_test 10a "Archive a file"
999
1000 test_10b() {
1001         # test needs a running copytool
1002         copytool setup
1003
1004         mkdir -p $DIR/$tdir
1005         local f=$DIR/$tdir/$tfile
1006         local fid=$(copy_file /etc/hosts $f)
1007         $LFS hsm_archive $f || error "archive request failed"
1008         wait_request_state $fid ARCHIVE SUCCEED
1009
1010         $LFS hsm_archive $f || error "archive of non dirty file failed"
1011         local cnt=$(get_request_count $fid ARCHIVE)
1012         [[ "$cnt" == "1" ]] ||
1013                 error "archive of non dirty file must not make a request"
1014 }
1015 run_test 10b "Archive of non dirty file must work without doing request"
1016
1017 test_10c() {
1018         # test needs a running copytool
1019         copytool setup
1020
1021         mkdir -p $DIR/$tdir
1022         local f=$DIR/$tdir/$tfile
1023         local fid=$(copy_file /etc/hosts $f)
1024         $LFS hsm_set --noarchive $f
1025         $LFS hsm_archive $f && error "archive a noarchive file must fail"
1026         return 0
1027 }
1028 run_test 10c "Check forbidden archive"
1029
1030 test_10d() {
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         $LFS hsm_archive $f || error "cannot archive $f"
1038         wait_request_state $fid ARCHIVE SUCCEED
1039
1040         local ar=$(get_hsm_archive_id $f)
1041         local dflt=$(get_hsm_param default_archive_id)
1042         [[ $ar == $dflt ]] ||
1043                 error "archived file is not on default archive: $ar != $dflt"
1044 }
1045 run_test 10d "Archive a file on the default archive id"
1046
1047 test_11a() {
1048         mkdir -p $DIR/$tdir
1049         copy2archive /etc/hosts $tdir/$tfile
1050         local f=$DIR/$tdir/$tfile
1051
1052         copytool import $tdir/$tfile $f
1053         echo -n "Verifying released state: "
1054         check_hsm_flags $f "0x0000000d"
1055
1056         local LSZ=$(stat -c "%s" $f)
1057         local ASZ=$(do_facet $SINGLEAGT stat -c "%s" "$(hsm_root)/$tdir/$tfile")
1058
1059         echo "Verifying imported size $LSZ=$ASZ"
1060         [[ $LSZ -eq $ASZ ]] || error "Incorrect size $LSZ != $ASZ"
1061         echo -n "Verifying released pattern: "
1062         local PTRN=$($LFS getstripe -L $f)
1063         echo $PTRN
1064         [[ $PTRN == released ]] || error "Is not released"
1065         local fid=$(path2fid $f)
1066         echo "Verifying new fid $fid in archive"
1067
1068         do_facet $SINGLEAGT "[ -f \"$(fid2archive "$fid")\" ]" ||
1069                 error "No archive for fid $fid"
1070 }
1071 run_test 11a "Import a file"
1072
1073 test_11b() {
1074         # test needs a running copytool
1075         copytool setup
1076
1077         mkdir -p $DIR/$tdir
1078         local f=$DIR/$tdir/$tfile
1079         local fid=$(copy_file /etc/hosts $f)
1080         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
1081                 error "hsm_archive failed"
1082         wait_request_state $fid ARCHIVE SUCCEED
1083
1084         local FILE_HASH=$(md5sum $f)
1085         rm -f $f
1086
1087         copytool import $fid $f
1088
1089         echo "$FILE_HASH" | md5sum -c
1090
1091         [[ $? -eq 0 ]] || error "Restored file differs"
1092 }
1093 run_test 11b "Import a deleted file using its FID"
1094
1095 test_12a() {
1096         # test needs a running copytool
1097         copytool setup
1098
1099         mkdir -p $DIR/$tdir
1100         copy2archive /etc/hosts $tdir/$tfile
1101
1102         local f=$DIR/$tdir/$tfile
1103         copytool import $tdir/$tfile $f
1104         local f2=$DIR2/$tdir/$tfile
1105         echo "Verifying released state: "
1106         check_hsm_flags $f2 "0x0000000d"
1107
1108         local fid=$(path2fid $f2)
1109         $LFS hsm_restore $f2
1110         wait_request_state $fid RESTORE SUCCEED
1111
1112         echo "Verifying file state: "
1113         check_hsm_flags $f2 "0x00000009"
1114
1115         do_facet $SINGLEAGT diff -q $(hsm_root)/$tdir/$tfile $f
1116
1117         [[ $? -eq 0 ]] || error "Restored file differs"
1118 }
1119 run_test 12a "Restore an imported file explicitly"
1120
1121 test_12b() {
1122         # test needs a running copytool
1123         copytool setup
1124
1125         mkdir -p $DIR/$tdir
1126         copy2archive /etc/hosts $tdir/$tfile
1127
1128         local f=$DIR/$tdir/$tfile
1129         copytool import $tdir/$tfile $f
1130         echo "Verifying released state: "
1131         check_hsm_flags $f "0x0000000d"
1132
1133         cat $f > /dev/null || error "File read failed"
1134
1135         echo "Verifying file state after restore: "
1136         check_hsm_flags $f "0x00000009"
1137
1138         do_facet $SINGLEAGT diff -q $(hsm_root)/$tdir/$tfile $f
1139
1140         [[ $? -eq 0 ]] || error "Restored file differs"
1141 }
1142 run_test 12b "Restore an imported file implicitly"
1143
1144 test_12c() {
1145         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
1146
1147         # test needs a running copytool
1148         copytool setup
1149
1150         local f=$DIR/$tdir/$tfile
1151         mkdir -p $DIR/$tdir
1152         $LFS setstripe -c 2 "$f"
1153         local fid=$(create_file "$f" 1M 5)
1154
1155         local FILE_CRC=$(md5sum $f)
1156
1157         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1158         wait_request_state $fid ARCHIVE SUCCEED
1159         $LFS hsm_release $f || error "release $f failed"
1160
1161         echo "$FILE_CRC" | md5sum -c
1162
1163         [[ $? -eq 0 ]] || error "Restored file differs"
1164 }
1165 run_test 12c "Restore a file with stripe of 2"
1166
1167 test_12d() {
1168         # test needs a running copytool
1169         copytool setup
1170
1171         mkdir -p $DIR/$tdir
1172
1173         local f=$DIR/$tdir/$tfile
1174         local fid=$(copy_file /etc/hosts $f)
1175         $LFS hsm_restore $f || error "restore of non archived file failed"
1176         local cnt=$(get_request_count $fid RESTORE)
1177         [[ "$cnt" == "0" ]] ||
1178                 error "restore non archived must not make a request"
1179         $LFS hsm_archive $f ||
1180                 error "archive request failed"
1181         wait_request_state $fid ARCHIVE SUCCEED
1182         $LFS hsm_restore $f ||
1183                 error "restore of non released file failed"
1184         local cnt=$(get_request_count $fid RESTORE)
1185         [[ "$cnt" == "0" ]] ||
1186                 error "restore a non dirty file must not make a request"
1187 }
1188 run_test 12d "Restore of a non archived, non released file must work"\
1189                 " without doing request"
1190
1191 test_12e() {
1192         # test needs a running copytool
1193         copytool setup
1194
1195         mkdir -p $DIR/$tdir
1196         local f=$DIR/$tdir/$tfile
1197         local fid=$(copy_file /etc/hosts $f)
1198         $LFS hsm_archive $f || error "archive request failed"
1199         wait_request_state $fid ARCHIVE SUCCEED
1200
1201         # make file dirty
1202         cat /etc/hosts >> $f
1203         sync
1204         $LFS hsm_state $f
1205
1206         $LFS hsm_restore $f && error "restore a dirty file must fail"
1207         return 0
1208 }
1209 run_test 12e "Check forbidden restore"
1210
1211 test_12f() {
1212         # test needs a running copytool
1213         copytool setup
1214
1215         mkdir -p $DIR/$tdir
1216         local f=$DIR/$tdir/$tfile
1217         local fid=$(copy_file /etc/hosts $f)
1218
1219         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1220         wait_request_state $fid ARCHIVE SUCCEED
1221         $LFS hsm_release $f || error "release of $f failed"
1222         $LFS hsm_restore $f
1223         wait_request_state $fid RESTORE SUCCEED
1224
1225         echo -n "Verifying file state: "
1226         check_hsm_flags $f "0x00000009"
1227
1228         diff -q /etc/hosts $f
1229
1230         [[ $? -eq 0 ]] || error "Restored file differs"
1231 }
1232 run_test 12f "Restore a released file explicitly"
1233
1234 test_12g() {
1235         # test needs a running copytool
1236         copytool setup
1237
1238         mkdir -p $DIR/$tdir
1239         local f=$DIR/$tdir/$tfile
1240         local fid=$(copy_file /etc/hosts $f)
1241
1242         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1243         wait_request_state $fid ARCHIVE SUCCEED
1244         $LFS hsm_release $f || error "release of $f failed"
1245
1246         diff -q /etc/hosts $f
1247         local st=$?
1248
1249         # we check we had a restore done
1250         wait_request_state $fid RESTORE SUCCEED
1251
1252         [[ $st -eq 0 ]] || error "Restored file differs"
1253 }
1254 run_test 12g "Restore a released file implicitly"
1255
1256 test_12h() {
1257         needclients 2 || return 0
1258
1259         # test needs a running copytool
1260         copytool setup
1261
1262         mkdir -p $DIR/$tdir
1263         local f=$DIR/$tdir/$tfile
1264         local fid=$(copy_file /etc/hosts $f)
1265
1266         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1267         wait_request_state $fid ARCHIVE SUCCEED
1268         $LFS hsm_release $f || error "release of $f failed"
1269
1270         do_node $CLIENT2 diff -q /etc/hosts $f
1271         local st=$?
1272
1273         # we check we had a restore done
1274         wait_request_state $fid RESTORE SUCCEED
1275
1276         [[ $st -eq 0 ]] || error "Restored file differs"
1277 }
1278 run_test 12h "Restore a released file implicitly from a second node"
1279
1280 test_12m() {
1281         # test needs a running copytool
1282         copytool setup
1283
1284         mkdir -p $DIR/$tdir
1285         local f=$DIR/$tdir/$tfile
1286         local fid=$(copy_file /etc/passwd $f)
1287         $LFS hsm_archive $f || error "archive of $f failed"
1288         wait_request_state $fid ARCHIVE SUCCEED
1289
1290         $LFS hsm_release $f || error "release of $f failed"
1291
1292         cmp /etc/passwd $f
1293
1294         [[ $? -eq 0 ]] || error "Restored file differs"
1295 }
1296 run_test 12m "Archive/release/implicit restore"
1297
1298 test_12n() {
1299         # test needs a running copytool
1300         copytool setup
1301
1302         mkdir -p $DIR/$tdir
1303         copy2archive /etc/hosts $tdir/$tfile
1304
1305         local f=$DIR/$tdir/$tfile
1306         copytool import $tdir/$tfile $f
1307
1308         do_facet $SINGLEAGT cmp /etc/hosts $f ||
1309                 error "Restored file differs"
1310
1311         $LFS hsm_release $f || error "release of $f failed"
1312 }
1313 run_test 12n "Import/implicit restore/release"
1314
1315 test_12o() {
1316         # test needs a running copytool
1317         copytool setup
1318
1319         mkdir -p $DIR/$tdir
1320         local f=$DIR/$tdir/$tfile
1321         local fid=$(copy_file /etc/hosts $f)
1322
1323         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1324         wait_request_state $fid ARCHIVE SUCCEED
1325         $LFS hsm_release $f || error "release of $f failed"
1326
1327 #define OBD_FAIL_MDS_HSM_SWAP_LAYOUTS           0x152
1328         do_facet $SINGLEMDS lctl set_param fail_loc=0x152
1329
1330         # set no retry action mode
1331         cdt_set_no_retry
1332
1333         diff -q /etc/hosts $f
1334         local st=$?
1335
1336         # we check we had a restore failure
1337         wait_request_state $fid RESTORE FAILED
1338
1339         [[ $st -eq 0 ]] && error "Restore must fail"
1340
1341         # remove no retry action mode
1342         cdt_clear_no_retry
1343
1344         # check file is still released
1345         check_hsm_flags $f "0x0000000d"
1346
1347         # retry w/o failure injection
1348         do_facet $SINGLEMDS lctl set_param fail_loc=0
1349
1350         # to be sure previous RESTORE result is gone
1351         cdt_purge
1352         wait_for_grace_delay
1353
1354         diff -q /etc/hosts $f
1355         st=$?
1356
1357         # we check we had a restore done
1358         wait_request_state $fid RESTORE SUCCEED
1359
1360         [[ $st -eq 0 ]] || error "Restored file differs"
1361 }
1362 run_test 12o "Layout-swap failure during Restore leaves file released"
1363
1364 test_12p() {
1365         # test needs a running copytool
1366         copytool setup
1367
1368         mkdir $DIR/$tdir
1369         local f=$DIR/$tdir/$tfile
1370         local fid=$(copy_file /etc/hosts $f)
1371
1372         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1373         wait_request_state $fid ARCHIVE SUCCEED
1374         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1375         $LFS hsm_release $f || error "cannot release $f"
1376         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1377         $LFS hsm_release $f || error "cannot release $f"
1378         do_facet $SINGLEAGT cat $f > /dev/null || error "cannot cat $f"
1379 }
1380 run_test 12p "implicit restore of a file on copytool mount point"
1381
1382 test_12q() {
1383         [ $MDS1_VERSION -lt $(version_code 2.7.58) ] &&
1384                 skip "need MDS version at least 2.7.58"
1385
1386         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
1387         zconf_mount $(facet_host $SINGLEAGT) $MOUNT3 ||
1388                 error "cannot mount $MOUNT3 on $SINGLEAGT"
1389
1390         # test needs a running copytool
1391         copytool setup -m "$MOUNT3"
1392
1393         local f=$DIR/$tdir/$tfile
1394         local f2=$DIR2/$tdir/$tfile
1395         local fid=$(create_small_file $f)
1396         local orig_size=$(stat -c "%s" $f)
1397
1398         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1399         wait_request_state $fid ARCHIVE SUCCEED
1400
1401         $LFS hsm_release $f || error "could not release file"
1402         check_hsm_flags $f "0x0000000d"
1403
1404         kill_copytools
1405         wait_copytools || error "copytool failed to stop"
1406
1407         cat $f > /dev/null &
1408
1409         # wait a bit to allow implicit restore request to be handled.
1410         # if not, next stat would also block on layout-lock.
1411         sleep 5
1412
1413         local size=$(stat -c "%s" $f2)
1414         [ $size -eq $orig_size ] ||
1415                 error "$f2: wrong size after archive: $size != $orig_size"
1416
1417         copytool setup -m "$MOUNT3"
1418
1419         wait
1420
1421         size=$(stat -c "%s" $f)
1422         [ $size -eq $orig_size ] ||
1423                 error "$f: wrong size after restore: $size != $orig_size"
1424
1425         size=$(stat -c "%s" $f2)
1426         [ $size -eq $orig_size ] ||
1427                 error "$f2: wrong size after restore: $size != $orig_size"
1428
1429         :>$f
1430
1431         size=$(stat -c "%s" $f)
1432         [ $size -eq 0 ] ||
1433                 error "$f: wrong size after overwrite: $size != 0"
1434
1435         size=$(stat -c "%s" $f2)
1436         [ $size -eq 0 ] ||
1437                 error "$f2: wrong size after overwrite: $size != 0"
1438 }
1439 run_test 12q "file attributes are refreshed after restore"
1440
1441 test_13() {
1442         local -i i j k=0
1443         for i in {1..10}; do
1444                 local archive_dir="$(hsm_root)"/subdir/dir.$i
1445
1446                 do_facet $SINGLEAGT mkdir -p "$archive_dir"
1447                 for j in {1..10}; do
1448                         local archive_file="$archive_dir"/file.$j
1449
1450                         do_facet $SINGLEAGT "echo $k > \"$archive_dir\"/file.$j"
1451                         k+=1
1452                 done
1453         done
1454
1455         # import to Lustre
1456         copytool import "subdir" "$DIR/$tdir"
1457
1458         # To check the import, the test uses diff with the -r flag
1459         # This is nice, but diff only checks files one by one, and triggering
1460         # an implicit restore for one file at a time will consume as many
1461         # seconds as there are files to compare. To speed this up, a restore
1462         # operation is triggered manually first.
1463         copytool setup
1464         find "$DIR/$tdir"/subdir -type f -exec $LFS hsm_restore {} \;
1465
1466         # Compare the imported data
1467         do_facet $SINGLEAGT \
1468                 diff -r "$(hsm_root)"/subdir "$DIR/$tdir"/subdir ||
1469                 error "imported files differ from archived data"
1470 }
1471 run_test 13 "Recursively import and restore a directory"
1472
1473 test_14() {
1474         # test needs a running copytool
1475         copytool setup
1476
1477         # archive a file
1478         local f=$DIR/$tdir/$tfile
1479         local fid=$(create_small_file $f)
1480         local sum=$(md5sum $f | awk '{print $1}')
1481         $LFS hsm_archive $f || error "could not archive file"
1482         wait_request_state $fid ARCHIVE SUCCEED
1483
1484         # create released file (simulate llapi_hsm_import call)
1485         local fid2=$(create_empty_file "$f")
1486         $LFS hsm_set --archived --exists $f || error "could not force hsm flags"
1487         $LFS hsm_release $f || error "could not release file"
1488
1489         # rebind the archive to the newly created file
1490         echo "rebind $fid to $fid2"
1491
1492         copytool rebind $fid $fid2
1493
1494         # restore file and compare md5sum
1495         local sum2=$(md5sum $f | awk '{print $1}')
1496
1497         [[ $sum == $sum2 ]] || error "md5sum mismatch after restore"
1498 }
1499 run_test 14 "Rebind archived file to a new fid"
1500
1501 test_15() {
1502         # test needs a running copytool
1503         copytool setup
1504
1505         # archive files
1506         local f=$DIR/$tdir/$tfile
1507         local count=5
1508         local tmpfile=$SHARED_DIRECTORY/tmp.$$
1509
1510         local fids=()
1511         local sums=()
1512         for i in $(seq 1 $count); do
1513                 fids[$i]=$(create_small_file $f.$i)
1514                 sums[$i]=$(md5sum $f.$i | awk '{print $1}')
1515                 $LFS hsm_archive $f.$i || error "could not archive file"
1516         done
1517         wait_all_done $(($count*60))
1518
1519         stack_trap "rm -f $tmpfile" EXIT
1520         :>$tmpfile
1521         # delete the files
1522         for i in $(seq 1 $count); do
1523                 local fid2=$(create_empty_file "${f}.${i}")
1524                 # add the rebind operation to the list
1525                 echo ${fids[$i]} $fid2 >> $tmpfile
1526
1527                 # set it released (simulate llapi_hsm_import call)
1528                 $LFS hsm_set --archived --exists $f.$i ||
1529                         error "could not force hsm flags"
1530                 $LFS hsm_release $f.$i || error "could not release file"
1531         done
1532         nl=$(wc -l < $tmpfile)
1533         [[ $nl == $count ]] || error "$nl files in list, $count expected"
1534
1535         echo "rebind list of files"
1536         copytool rebind "$tmpfile"
1537
1538         # restore files and compare md5sum
1539         for i in $(seq 1 $count); do
1540                 local sum2=$(md5sum $f.$i | awk '{print $1}')
1541                 [[ $sum2 == ${sums[$i]} ]] ||
1542                     error "md5sum mismatch after restore ($sum2 != ${sums[$i]})"
1543         done
1544 }
1545 run_test 15 "Rebind a list of files"
1546
1547 test_16() {
1548         # test needs a running copytool
1549         copytool setup -b 1
1550
1551         local ref=/tmp/ref
1552         # create a known size file so we can verify transfer speed
1553         # 20 MB <-> 20s
1554         local goal=20
1555         dd if=/dev/zero of=$ref bs=1M count=20
1556
1557         mkdir -p $DIR/$tdir
1558         local f=$DIR/$tdir/$tfile
1559         local fid=$(copy_file $ref $f)
1560         rm $ref
1561         local start=$(date +%s)
1562         $LFS hsm_archive $f
1563         wait_request_state $fid ARCHIVE SUCCEED
1564         local end=$(date +%s)
1565         # Add 1 to account for rounding errors between start and end (LU-8155)
1566         local duration=$((end - start + 1))
1567
1568         [[ $duration -ge $((goal - 1)) ]] ||
1569                 error "Transfer is too fast $duration < $goal"
1570 }
1571 run_test 16 "Test CT bandwith control option"
1572
1573 test_20() {
1574         local f=$DIR/$tdir/$tfile
1575         create_empty_file "$f"
1576
1577         # Could not release a non-archived file
1578         $LFS hsm_release $f && error "release should not succeed"
1579
1580         # For following tests, we must test them with HS_ARCHIVED set
1581         $LFS hsm_set --exists --archived $f || error "could not add flag"
1582
1583         # Could not release a file if no-release is set
1584         $LFS hsm_set --norelease $f || error "could not add flag"
1585         $LFS hsm_release $f && error "release should not succeed"
1586         $LFS hsm_clear --norelease $f || error "could not remove flag"
1587
1588         # Could not release a file if lost
1589         $LFS hsm_set --lost $f || error "could not add flag"
1590         $LFS hsm_release $f && error "release should not succeed"
1591         $LFS hsm_clear --lost $f || error "could not remove flag"
1592
1593         # Could not release a file if dirty
1594         $LFS hsm_set --dirty $f || error "could not add flag"
1595         $LFS hsm_release $f && error "release should not succeed"
1596         $LFS hsm_clear --dirty $f || error "could not remove flag"
1597 }
1598 run_test 20 "Release is not permitted"
1599
1600 test_21() {
1601         # test needs a running copytool
1602         copytool setup
1603
1604         local f=$DIR/$tdir/test_release
1605
1606         # Create a file and check its states
1607         local fid=$(create_small_file $f)
1608         check_hsm_flags $f "0x00000000"
1609
1610         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1611         # used until file is flushed to disk
1612         if [  $(facet_fstype ost1) == "zfs" ]; then
1613             # this causes an OST_SYNC rpc to be sent
1614             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1615             # clear locks to reread file data
1616             cancel_lru_locks osc
1617         fi
1618
1619         local orig_size=$(stat -c "%s" $f)
1620         local orig_blocks=$(stat -c "%b" $f)
1621
1622         start_full_debug_logging
1623
1624         $LFS hsm_archive $f || error "could not archive file"
1625         wait_request_state $fid ARCHIVE SUCCEED
1626
1627         local blocks=$(stat -c "%b" $f)
1628         [ $blocks -eq $orig_blocks ] ||
1629                 error "$f: wrong block number after archive: " \
1630                       "$blocks != $orig_blocks"
1631         local size=$(stat -c "%s" $f)
1632         [ $size -eq $orig_size ] ||
1633                 error "$f: wrong size after archive: $size != $orig_size"
1634
1635         # Release and check states
1636         $LFS hsm_release $f || error "could not release file"
1637         check_hsm_flags $f "0x0000000d"
1638
1639         blocks=$(stat -c "%b" $f)
1640         [ $blocks -gt 5 ] &&
1641                 error "$f: too many blocks after release: $blocks > 5"
1642         size=$(stat -c "%s" $f)
1643         [ $size -ne $orig_size ] &&
1644                 error "$f: wrong size after release: $size != $orig_size"
1645
1646         # Check we can release an file without stripe info
1647         f=$f.nolov
1648         $MCREATE $f
1649         fid=$(path2fid $f)
1650         check_hsm_flags $f "0x00000000"
1651         $LFS hsm_archive $f || error "could not archive file"
1652         wait_request_state $fid ARCHIVE SUCCEED
1653
1654         # Release and check states
1655         $LFS hsm_release $f || error "could not release file"
1656         check_hsm_flags $f "0x0000000d"
1657
1658         # Release again a file that is already released is OK
1659         $LFS hsm_release $f || fail "second release should succeed"
1660         check_hsm_flags $f "0x0000000d"
1661
1662         stop_full_debug_logging
1663 }
1664 run_test 21 "Simple release tests"
1665
1666 test_22() {
1667         # test needs a running copytool
1668         copytool setup
1669
1670         local f=$DIR/$tdir/test_release
1671         local swap=$DIR/$tdir/test_swap
1672
1673         # Create a file and check its states
1674         local fid=$(create_small_file $f)
1675         check_hsm_flags $f "0x00000000"
1676
1677         $LFS hsm_archive $f || error "could not archive file"
1678         wait_request_state $fid ARCHIVE SUCCEED
1679
1680         # Release and check states
1681         $LFS hsm_release $f || error "could not release file"
1682         check_hsm_flags $f "0x0000000d"
1683
1684         create_small_file $swap
1685         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1686
1687         return 0
1688 }
1689 run_test 22 "Could not swap a release file"
1690
1691 test_23() {
1692         # test needs a running copytool
1693         copytool setup
1694
1695         local f=$DIR/$tdir/test_mtime
1696
1697         # Create a file and check its states
1698         local fid=$(create_small_file $f)
1699         check_hsm_flags $f "0x00000000"
1700
1701         $LFS hsm_archive $f || error "could not archive file"
1702         wait_request_state $fid ARCHIVE SUCCEED
1703
1704         # Set modification time in the past
1705         touch -m -a -d @978261179 $f
1706
1707         # Release and check states
1708         $LFS hsm_release $f || error "could not release file"
1709         check_hsm_flags $f "0x0000000d"
1710
1711         local MTIME=$(stat -c "%Y" $f)
1712         local ATIME=$(stat -c "%X" $f)
1713         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1714         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1715 }
1716 run_test 23 "Release does not change a/mtime (utime)"
1717
1718 test_24a() {
1719         local file=$DIR/$tdir/$tfile
1720         local fid
1721         local atime0
1722         local atime1
1723         local mtime0
1724         local mtime1
1725         local ctime0
1726         local ctime1
1727
1728         # test needs a running copytool
1729         copytool setup
1730
1731         fid=$(create_small_file $file)
1732
1733         # Create a file and check its states
1734         check_hsm_flags $file "0x00000000"
1735
1736         # Ensure atime is less than mtime and ctime.
1737         sleep 1
1738         echo >> $file
1739
1740         atime0=$(stat -c "%X" $file)
1741         mtime0=$(stat -c "%Y" $file)
1742         ctime0=$(stat -c "%Z" $file)
1743
1744         [ $atime0 -lt $mtime0 ] ||
1745                 error "atime $atime0 is not less than mtime $mtime0"
1746
1747         [ $atime0 -lt $ctime0 ] ||
1748                 error "atime $atime0 is not less than ctime $ctime0"
1749
1750         # Archive should not change any timestamps.
1751         $LFS hsm_archive $file || error "cannot archive '$file'"
1752         wait_request_state $fid ARCHIVE SUCCEED
1753
1754         atime1=$(stat -c "%X" $file)
1755         mtime1=$(stat -c "%Y" $file)
1756         ctime1=$(stat -c "%Z" $file)
1757
1758         [ $atime0 -eq $atime1 ] ||
1759                 error "archive changed atime from $atime0 to $atime1"
1760
1761         [ $mtime0 -eq $mtime1 ] ||
1762                 error "archive changed mtime from $mtime0 to $mtime1"
1763
1764         [ $ctime0 -eq $ctime1 ] ||
1765                 error "archive changed ctime from $ctime0 to $ctime1"
1766
1767         # Release should not change any timestamps.
1768         $LFS hsm_release $file || error "cannot release '$file'"
1769         check_hsm_flags $file "0x0000000d"
1770
1771         atime1=$(stat -c "%X" $file)
1772         mtime1=$(stat -c "%Y" $file)
1773         ctime1=$(stat -c "%Z" $file)
1774
1775         [ $atime0 -eq $atime1 ] ||
1776                 error "release changed atime from $atime0 to $atime1"
1777
1778         [ $mtime0 -eq $mtime1 ] ||
1779                 error "release changed mtime from $mtime0 to $mtime1"
1780
1781         [ $ctime0 -eq $ctime1 ] ||
1782                 error "release changed ctime from $ctime0 to $ctime1"
1783
1784         # Restore should not change any timestamps.
1785         $LFS hsm_restore $file
1786         wait_request_state $fid RESTORE SUCCEED
1787
1788         atime1=$(stat -c "%X" $file)
1789         mtime1=$(stat -c "%Y" $file)
1790         ctime1=$(stat -c "%Z" $file)
1791
1792         [ $atime0 -eq $atime1 ] ||
1793                 error "restore changed atime from $atime0 to $atime1"
1794
1795         [ $mtime0 -eq $mtime1 ] ||
1796                 error "restore changed mtime from $mtime0 to $mtime1"
1797
1798         [ $ctime0 -eq $ctime1 ] ||
1799                 error "restore changed ctime from $ctime0 to $ctime1"
1800
1801         kill_copytools
1802         wait_copytools || error "Copytools failed to stop"
1803
1804         # Once more, after unmount and mount.
1805         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1806         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1807
1808         atime1=$(stat -c "%X" $file)
1809         mtime1=$(stat -c "%Y" $file)
1810         ctime1=$(stat -c "%Z" $file)
1811
1812         [ $atime0 -eq $atime1 ] ||
1813                 error "remount changed atime from $atime0 to $atime1"
1814
1815         [ $mtime0 -eq $mtime1 ] ||
1816                 error "remount changed mtime from $mtime0 to $mtime1"
1817
1818         [ $ctime0 -eq $ctime1 ] ||
1819                 error "remount changed ctime from $ctime0 to $ctime1"
1820 }
1821 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1822
1823 test_24b() {
1824         local file=$DIR/$tdir/$tfile
1825         local fid
1826         local sum0
1827         local sum1
1828         # LU-3811
1829
1830         # Test needs a running copytool.
1831         copytool setup
1832
1833         # Check that root can do HSM actions on a regular user's file.
1834         fid=$(create_small_file $file)
1835         sum0=$(md5sum $file)
1836
1837         chown $RUNAS_ID:$RUNAS_GID $file ||
1838                 error "cannot chown '$file' to '$RUNAS_ID'"
1839
1840         chmod ugo-w $DIR/$tdir ||
1841                 error "cannot chmod '$DIR/$tdir'"
1842
1843         $LFS hsm_archive $file
1844         wait_request_state $fid ARCHIVE SUCCEED
1845
1846         $LFS hsm_release $file
1847         check_hsm_flags $file "0x0000000d"
1848
1849         $LFS hsm_restore $file
1850         wait_request_state $fid RESTORE SUCCEED
1851
1852         # Check that ordinary user can get HSM state.
1853         $RUNAS $LFS hsm_state $file ||
1854                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1855
1856         $LFS hsm_release $file
1857         check_hsm_flags $file "0x0000000d"
1858
1859         # Check that ordinary user can accessed released file.
1860         sum1=$($RUNAS md5sum $file) ||
1861                 error "user '$RUNAS_ID' cannot read '$file'"
1862
1863         [ "$sum0" == "$sum1" ] ||
1864                 error "md5sum mismatch for '$file'"
1865 }
1866 run_test 24b "root can archive, release, and restore user files"
1867
1868 test_24c() {
1869         local file=$DIR/$tdir/$tfile
1870         local action=archive
1871         local user_save
1872         local group_save
1873         local other_save
1874
1875         # test needs a running copytool
1876         copytool setup
1877
1878         mkdir -p $DIR/$tdir
1879
1880         # Save the default masks and check that cleanup_24c will
1881         # restore the request masks correctly.
1882         user_save=$(get_hsm_param user_request_mask)
1883         stack_trap "set_hsm_param user_request_mask $user_save" EXIT
1884         group_save=$(get_hsm_param group_request_mask)
1885         stack_trap "set_hsm_param user_request_mask $group_save" EXIT
1886         other_save=$(get_hsm_param other_request_mask)
1887         stack_trap "set_hsm_param user_request_mask $other_save" EXIT
1888
1889         [ "$user_save" == RESTORE ] ||
1890                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1891         [ "$group_save" == RESTORE ] ||
1892                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1893         [ "$other_save" == RESTORE ] ||
1894                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1895
1896         # User.
1897         create_small_file $file
1898         chown $RUNAS_ID:$GROUP $file ||
1899                 error "cannot chown '$file' to '$RUNAS_ID:$GROUP'"
1900
1901         $RUNAS $LFS hsm_$action $file &&
1902                 error "$action by user should fail"
1903
1904         set_hsm_param user_request_mask $action
1905         $RUNAS $LFS hsm_$action $file ||
1906                 error "$action by user should succeed"
1907
1908         # Group.
1909         create_small_file $file
1910         chown nobody:$RUNAS_GID $file ||
1911                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
1912
1913         $RUNAS $LFS hsm_$action $file &&
1914                 error "$action by group should fail"
1915
1916         set_hsm_param group_request_mask $action
1917         $RUNAS $LFS hsm_$action $file ||
1918                 error "$action by group should succeed"
1919
1920         # Other.
1921         create_small_file $file
1922         chown nobody:$GROUP $file ||
1923                 error "cannot chown '$file' to 'nobody:$GROUP'"
1924
1925         $RUNAS $LFS hsm_$action $file &&
1926                 error "$action by other should fail"
1927
1928         set_hsm_param other_request_mask $action
1929         $RUNAS $LFS hsm_$action $file ||
1930                 error "$action by other should succeed"
1931 }
1932 run_test 24c "check that user,group,other request masks work"
1933
1934 test_24d() {
1935         local file1=$DIR/$tdir/$tfile
1936         local file2=$DIR2/$tdir/$tfile
1937         local fid1
1938         local fid2
1939
1940         fid1=$(create_small_file $file1)
1941
1942         echo $fid1
1943         $LFS getstripe $file1
1944
1945         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
1946         zconf_mount "$(facet_host $SINGLEAGT)" "$MOUNT3" ||
1947                 error "cannot mount '$MOUNT3' on '$SINGLEAGT'"
1948
1949         copytool setup -m  "$MOUNT3"
1950
1951         stack_trap "mount -o remount,rw \"$MOUNT2\"" EXIT
1952         mount -o remount,ro $MOUNT2
1953
1954         do_nodes $(comma_list $(nodes_list)) $LCTL clear
1955
1956         fid2=$(path2fid $file2)
1957         [ "$fid1" == "$fid2" ] ||
1958                 error "FID mismatch '$fid1' != '$fid2'"
1959
1960         $LFS hsm_archive $file2 &&
1961                 error "archive should fail on read-only mount"
1962         check_hsm_flags $file1 "0x00000000"
1963
1964         $LFS hsm_archive $file1 || error "Fail to archive $file1"
1965         wait_request_state $fid1 ARCHIVE SUCCEED
1966
1967         $LFS hsm_release $file1
1968         $LFS hsm_restore $file2
1969         wait_request_state $fid1 RESTORE SUCCEED
1970
1971         $LFS hsm_release $file1 || error "cannot release '$file1'"
1972         dd if=$file2 of=/dev/null bs=1M || error "cannot read '$file2'"
1973
1974         $LFS hsm_release $file2 &&
1975                 error "release should fail on read-only mount"
1976
1977         return 0
1978 }
1979 run_test 24d "check that read-only mounts are respected"
1980
1981 test_24e() {
1982         copytool setup
1983
1984         local f=$DIR/$tdir/$tfile
1985         local fid
1986
1987         fid=$(create_small_file $f) || error "cannot create $f"
1988         $LFS hsm_archive $f || error "cannot archive $f"
1989         wait_request_state $fid ARCHIVE SUCCEED
1990         $LFS hsm_release $f || error "cannot release $f"
1991         while ! $LFS hsm_state $f | grep released; do
1992                 sleep 1
1993         done
1994
1995         tar -cf $TMP/$tfile.tar $DIR/$tdir || error "cannot tar $DIR/$tdir"
1996 }
1997 run_test 24e "tar succeeds on HSM released files" # LU-6213
1998
1999 test_24f() {
2000         # test needs a running copytool
2001         copytool setup
2002
2003         mkdir -p $DIR/$tdir/d1
2004         local f=$DIR/$tdir/$tfile
2005         local fid=$(copy_file /etc/hosts $f)
2006         sum0=$(md5sum $f)
2007         echo $sum0
2008         $LFS hsm_archive $f ||
2009                 error "hsm_archive failed"
2010         wait_request_state $fid ARCHIVE SUCCEED
2011         $LFS hsm_release $f || error "cannot release $f"
2012         tar --xattrs -cvf $f.tar -C $DIR/$tdir $tfile
2013         rm -f $f
2014         sync
2015         tar --xattrs -xvf $f.tar -C $DIR/$tdir ||
2016                 error "Can not recover the tar contents"
2017         sum1=$(md5sum $f)
2018         echo "Sum0 = $sum0, sum1 = $sum1"
2019         [ "$sum0" == "$sum1" ] || error "md5sum mismatch for '$tfile'"
2020 }
2021 run_test 24f "root can archive, release, and restore tar files"
2022
2023 test_24g() {
2024         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
2025                 skip "need MDS version 2.11.56 or later"
2026
2027         local file=$DIR/$tdir/$tfile
2028         local fid
2029
2030         echo "RUNAS = '$RUNAS'"
2031
2032         copytool setup
2033
2034         mkdir -p $DIR/$tdir
2035         chmod ugo+rwx $DIR/$tdir
2036
2037         echo "Please listen carefully as our options have changed." | tee $file
2038         fid=$(path2fid $file)
2039         chmod ugo+rw $file
2040
2041         $LFS hsm_archive $file
2042         wait_request_state $fid ARCHIVE SUCCEED
2043         check_hsm_flags $file 0x00000009 # exists archived
2044
2045         echo "To be electrocuted by your telephone, press #." | $RUNAS tee $file
2046         check_hsm_flags $file 0x0000000b # exists dirty archived
2047 }
2048 run_test 24g "write by non-owner still sets dirty" # LU-11369
2049
2050 test_25a() {
2051         # test needs a running copytool
2052         copytool setup
2053
2054         mkdir -p $DIR/$tdir
2055         copy2archive /etc/hosts $tdir/$tfile
2056
2057         local f=$DIR/$tdir/$tfile
2058
2059         copytool import $tdir/$tfile $f
2060
2061         $LFS hsm_set --lost $f
2062
2063         md5sum $f
2064         local st=$?
2065
2066         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2067 }
2068 run_test 25a "Restore lost file (HS_LOST flag) from import"\
2069              " (Operation not permitted)"
2070
2071 test_25b() {
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=$(copy_file /etc/passwd $f)
2079
2080         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2081         wait_request_state $fid ARCHIVE SUCCEED
2082
2083         $LFS hsm_release $f
2084         $LFS hsm_set --lost $f
2085         md5sum $f
2086         st=$?
2087
2088         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
2089 }
2090 run_test 25b "Restore lost file (HS_LOST flag) after release"\
2091              " (Operation not permitted)"
2092
2093 test_26A() { # was test_26
2094         # test needs a running copytool
2095         copytool setup
2096
2097         local f=$DIR/$tdir/$tfile
2098         local fid=$(create_empty_file "$f")
2099
2100         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2101         wait_request_state $fid ARCHIVE SUCCEED
2102
2103         $LFS hsm_remove $f
2104         wait_request_state $fid REMOVE SUCCEED
2105
2106         check_hsm_flags $f "0x00000000"
2107 }
2108 run_test 26A "Remove the archive of a valid file"
2109
2110 test_26a() {
2111         local raolu=$(get_hsm_param remove_archive_on_last_unlink)
2112         [[ $raolu -eq 0 ]] || error "RAoLU policy should be off"
2113
2114         # test needs a running copytool
2115         copytool setup
2116
2117         mkdir -p $DIR/$tdir
2118         local f=$DIR/$tdir/$tfile
2119         local fid=$(copy_file /etc/passwd $f)
2120
2121         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2122         wait_request_state $fid ARCHIVE SUCCEED
2123
2124         local f2=$DIR/$tdir/${tfile}_2
2125         local fid2=$(copy_file /etc/passwd $f2)
2126
2127         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2128         wait_request_state $fid2 ARCHIVE SUCCEED
2129
2130         local f3=$DIR/$tdir/${tfile}_3
2131         local fid3=$(copy_file /etc/passwd $f3)
2132
2133         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f3
2134         wait_request_state $fid3 ARCHIVE SUCCEED
2135
2136         # set a long grace_delay vs short loop_period
2137         local orig_loop_period=$(get_hsm_param loop_period)
2138         local orig_grace_delay=$(get_hsm_param grace_delay)
2139         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2140         set_hsm_param loop_period 10
2141         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2142         set_hsm_param grace_delay 100
2143
2144         rm -f $f
2145
2146         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2147         set_hsm_param remove_archive_on_last_unlink 1
2148
2149         ln "$f3" "$f3"_bis || error "Unable to create hard-link"
2150         rm -f $f3
2151
2152         rm -f $f2
2153
2154         wait_request_state $fid2 REMOVE SUCCEED
2155
2156         assert_request_count $fid REMOVE 0 \
2157                 "Unexpected archived data remove request for $f"
2158         assert_request_count $fid3 REMOVE 0 \
2159                 "Unexpected archived data remove request for $f3"
2160 }
2161 run_test 26a "Remove Archive On Last Unlink (RAoLU) policy"
2162
2163 test_26b() {
2164         # test needs a running copytool
2165         copytool setup
2166
2167         mkdir -p $DIR/$tdir
2168         local f=$DIR/$tdir/$tfile
2169         local fid=$(copy_file /etc/passwd $f)
2170
2171         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2172         wait_request_state $fid ARCHIVE SUCCEED
2173
2174         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2175         set_hsm_param remove_archive_on_last_unlink 1
2176
2177         cdt_shutdown
2178         cdt_check_state stopped
2179
2180         rm -f $f
2181
2182         wait_request_state $fid REMOVE WAITING
2183
2184         cdt_enable
2185
2186         # copytool must re-register
2187         kill_copytools
2188         wait_copytools || error "copytool failed to stop"
2189         copytool setup
2190
2191         wait_request_state $fid REMOVE SUCCEED
2192 }
2193 run_test 26b "RAoLU policy when CDT off"
2194
2195 test_26c() {
2196         # test needs a running copytool
2197         copytool setup
2198
2199         mkdir -p $DIR/$tdir
2200         local f=$DIR/$tdir/$tfile
2201         local fid=$(copy_file /etc/passwd $f)
2202
2203         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2204         wait_request_state $fid ARCHIVE SUCCEED
2205
2206         local f2=$DIR/$tdir/${tfile}_2
2207         local fid2=$(copy_file /etc/passwd $f2)
2208
2209         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2210         wait_request_state $fid2 ARCHIVE SUCCEED
2211
2212         # set a long grace_delay vs short loop_period
2213         local orig_loop_period=$(get_hsm_param loop_period)
2214         local orig_grace_delay=$(get_hsm_param grace_delay)
2215         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2216         set_hsm_param loop_period 10
2217         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2218         set_hsm_param grace_delay 100
2219
2220         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2221         set_hsm_param remove_archive_on_last_unlink 1
2222
2223         multiop_bg_pause $f O_c || error "open $f failed"
2224         local pid=$!
2225
2226         rm -f $f
2227         rm -f $f2
2228
2229         wait_request_state $fid2 REMOVE SUCCEED
2230         assert_request_count $fid REMOVE 0 \
2231                 "Unexpected archived data remove request for $f"
2232
2233         kill -USR1 $pid || error "multiop early exit"
2234         # should reach autotest timeout if multiop fails to trap
2235         # signal, close file, and exit ...
2236         wait $pid || error "wait PID $PID failed"
2237
2238         wait_request_state $fid REMOVE SUCCEED
2239 }
2240 run_test 26c "RAoLU effective when file closed"
2241
2242 test_26d() {
2243         # test needs a running copytool
2244         copytool setup
2245
2246         mkdir -p $DIR/$tdir
2247         local f=$DIR/$tdir/$tfile
2248         local fid=$(create_small_file $f)
2249
2250         $LFS hsm_archive $f || error "could not archive file"
2251         wait_request_state $fid ARCHIVE SUCCEED
2252
2253         # set a long grace_delay vs short loop_period
2254         local orig_loop_period=$(get_hsm_param loop_period)
2255         local orig_grace_delay=$(get_hsm_param grace_delay)
2256         stack_trap "set_hsm_param loop_period $orig_loop_period" EXIT
2257         set_hsm_param loop_period 10
2258         stack_trap "set_hsm_param grace_delay $orig_grace_delay" EXIT
2259         set_hsm_param grace_delay 100
2260
2261         stack_trap "set_hsm_param remove_archive_on_last_unlink 0" EXIT
2262         set_hsm_param remove_archive_on_last_unlink 1
2263
2264         multiop_bg_pause $f O_c || error "multiop failed"
2265         local MULTIPID=$!
2266
2267         rm -f $f
2268
2269         mds_evict_client
2270
2271         wait_request_state $fid REMOVE SUCCEED
2272
2273         client_up || client_up || true
2274
2275         kill -USR1 $MULTIPID
2276         wait $MULTIPID || error "multiop close failed"
2277 }
2278 run_test 26d "RAoLU when Client eviction"
2279
2280 test_27a() {
2281         # test needs a running copytool
2282         copytool setup
2283
2284         create_archive_file $tdir/$tfile
2285         local f=$DIR/$tdir/$tfile
2286         copytool import $tdir/$tfile $f
2287         local fid=$(path2fid $f)
2288
2289         $LFS hsm_remove $f
2290
2291         [[ $? != 0 ]] || error "Remove of a released file should fail"
2292 }
2293 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
2294
2295 test_27b() {
2296         # test needs a running copytool
2297         copytool setup
2298
2299         local f=$DIR/$tdir/$tfile
2300         local fid=$(create_empty_file "$f")
2301
2302         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2303         wait_request_state $fid ARCHIVE SUCCEED
2304         $LFS hsm_release $f
2305
2306         $LFS hsm_remove $f
2307
2308         [[ $? != 0 ]] || error "Remove of a released file should fail"
2309 }
2310 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
2311
2312 test_28() {
2313         # test needs a running copytool
2314         copytool setup
2315
2316         local f=$DIR/$tdir/$tfile
2317         local fid=$(create_empty_file "$f")
2318
2319         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2320         wait_request_state $fid ARCHIVE SUCCEED
2321
2322         cdt_disable
2323         $LFS hsm_remove $f
2324
2325         rm -f $f
2326
2327         cdt_enable
2328
2329         wait_request_state $fid REMOVE SUCCEED
2330 }
2331 run_test 28 "Concurrent archive/file remove"
2332
2333 test_29a() {
2334         # Tests --mntpath and --archive options
2335
2336         local archive_id=7
2337         copytool setup -m "$MOUNT" -a $archive_id
2338
2339         # Bad archive number
2340         $LFS hsm_remove -m "$MOUNT" -a 33 0x857765760:0x8:0x2 2>&1 |
2341                 grep "Invalid argument" ||
2342                 error "unexpected hsm_remove failure (1)"
2343
2344         # mntpath is present but file is given
2345         $LFS hsm_remove --mntpath "$MOUNT" --archive 30 /qwerty/uyt 2>&1 |
2346                 grep "hsm: '/qwerty/uyt' is not a valid FID" ||
2347                 error "unexpected hsm_remove failure (2)"
2348 }
2349 run_test 29a "Tests --mntpath and --archive options"
2350
2351 test_29b() {
2352         # test needs a running copytool
2353         copytool setup
2354
2355         local f=$DIR/$tdir/$tfile
2356         local fid=$(create_small_file $f)
2357
2358         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2359         wait_request_state $fid ARCHIVE SUCCEED
2360
2361         rm -f $f
2362
2363         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER $fid
2364         wait_request_state $fid REMOVE SUCCEED
2365 }
2366 run_test 29b "Archive/delete/remove by FID from the archive."
2367
2368 test_29c() {
2369         # test needs a running copytool
2370         copytool setup
2371
2372         local fid1=$(create_small_file $DIR/$tdir/$tfile-1)
2373         local fid2=$(create_small_file $DIR/$tdir/$tfile-2)
2374         local fid3=$(create_small_file $DIR/$tdir/$tfile-3)
2375
2376         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$tfile-[1-3]
2377         wait_request_state $fid1 ARCHIVE SUCCEED
2378         wait_request_state $fid2 ARCHIVE SUCCEED
2379         wait_request_state $fid3 ARCHIVE SUCCEED
2380
2381         rm -f $DIR/$tdir/$tfile-[1-3]
2382
2383         echo $fid1 > $DIR/$tdir/list
2384         echo $fid2 >> $DIR/$tdir/list
2385         echo $fid3 >> $DIR/$tdir/list
2386
2387         $LFS hsm_remove -m $MOUNT -a $HSM_ARCHIVE_NUMBER \
2388                 --filelist $DIR/$tdir/list
2389         wait_request_state $fid1 REMOVE SUCCEED
2390         wait_request_state $fid2 REMOVE SUCCEED
2391         wait_request_state $fid3 REMOVE SUCCEED
2392 }
2393 run_test 29c "Archive/delete/remove by FID, using a file list."
2394
2395 test_29d() {
2396         # test needs more than one CT
2397         needclients 3 || return 0
2398
2399         local n
2400         local file
2401         local fid
2402
2403         # start all of the copytools
2404         for n in $(seq $AGTCOUNT); do
2405                 copytool setup -f agt$n -a $n
2406         done
2407
2408         # archive files
2409         file=$DIR/$tdir/$tfile
2410         fid=$(create_small_file $file)
2411
2412         $LFS hsm_archive $file
2413         wait_request_state $fid ARCHIVE SUCCEED
2414         check_hsm_flags $file "0x00000009"
2415
2416         rm -f $file
2417
2418         $LFS hsm_remove --mntpath "$MOUNT" -a 0 $fid ||
2419                 error "cannot hsm_remove '$fid'"
2420
2421         # give time for CDT to handle remove request and create broadcasted
2422         sleep 2
2423
2424         # remove request has been broadcasted ?
2425         local cnt=$(get_request_count $fid REMOVE)
2426         # broadcasted requests + original
2427         [[ $cnt -eq $((AGTCOUNT + 1)) ]] ||
2428                 error "remove not broadcasted to all CTs"
2429
2430         # give time for CDT and CTs to handle broadcasted
2431         wait_for_loop_period
2432
2433         # each agent serves one different archive_id, so broadcasted
2434         # hsm_remove request should only succeed once and fail at all others
2435         local res
2436         local scnt=0
2437         local fcnt=0
2438         for n in $(seq $AGTCOUNT); do
2439                 res=$(do_facet $SINGLEMDS "$LCTL get_param -n \
2440                                $HSM_PARAM.actions | awk \
2441                                '/'$fid'.*action=REMOVE archive#='$n'/ \
2442                                {print \\\$13}' | cut -f2 -d=")
2443                 if [[ "$res" == "SUCCEED" ]]; then
2444                         scnt=$((scnt + 1))
2445                 elif [[ "$res" == "FAILED" ]]; then
2446                         fcnt=$((fcnt + 1))
2447                 fi
2448         done
2449
2450         [[ $scnt -eq 1 ]] ||
2451                 error "one and only CT should have removed successfully"
2452
2453         [[ $AGTCOUNT -eq $((scnt + fcnt)) ]] ||
2454                 error "all but one CT should have failed to remove"
2455 }
2456 run_test 29d "hsm_remove by FID with archive_id 0 for unlinked file cause "\
2457              "request to be sent once for each registered archive_id"
2458
2459 test_30a() {
2460         # restore at exec cannot work on agent node (because of Linux kernel
2461         # protection of executables)
2462         needclients 2 || return 0
2463
2464         # test needs a running copytool
2465         copytool setup
2466
2467         mkdir -p $DIR/$tdir
2468         copy2archive /bin/true $tdir/$tfile
2469
2470         local f=$DIR/$tdir/true
2471         copytool import $tdir/$tfile $f
2472
2473         local fid=$(path2fid $f)
2474
2475         stack_trap "cdt_clear_no_retry" EXIT
2476         # set no retry action mode
2477         cdt_set_no_retry
2478         do_node $CLIENT2 $f
2479         local st=$?
2480
2481         $LFS hsm_state $f
2482
2483         [[ $st == 0 ]] || error "Failed to exec a released file"
2484 }
2485 run_test 30a "Restore at exec (import case)"
2486
2487 test_30b() {
2488         # restore at exec cannot work on agent node (because of Linux kernel
2489         # protection of executables)
2490         needclients 2 || return 0
2491
2492         # test needs a running copytool
2493         copytool setup
2494
2495         mkdir -p $DIR/$tdir
2496         local f=$DIR/$tdir/true
2497         local fid=$(copy_file /bin/true $f)
2498         chmod 755 $f
2499         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2500         wait_request_state $fid ARCHIVE SUCCEED
2501         $LFS hsm_release $f
2502         $LFS hsm_state $f
2503
2504         stack_trap cdt_clear_no_retry EXIT
2505         # set no retry action mode
2506         cdt_set_no_retry
2507
2508         do_node $CLIENT2 $f
2509         local st=$?
2510
2511         $LFS hsm_state $f
2512
2513         [[ $st == 0 ]] || error "Failed to exec a released file"
2514 }
2515 run_test 30b "Restore at exec (release case)"
2516
2517 test_30c() {
2518         needclients 2 || return 0
2519
2520         # test needs a running copytool
2521         copytool setup
2522
2523         mkdir -p $DIR/$tdir
2524         local f=$DIR/$tdir/SLEEP
2525         local slp_sum1=$(md5sum /bin/sleep)
2526         local fid=$(copy_file /bin/sleep $f)
2527         chmod 755 $f
2528         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2529         wait_request_state $fid ARCHIVE SUCCEED
2530         $LFS hsm_release $f
2531         check_hsm_flags $f "0x0000000d"
2532
2533         stack_trap cdt_clear_no_retry EXIT
2534         # set no retry action mode
2535         cdt_set_no_retry
2536
2537         do_node $CLIENT2 "$f 10" &
2538         local pid=$!
2539         sleep 3
2540         echo 'Hi!' > $f
2541         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2542         wait $pid
2543         [[ $? == 0 ]] || error "Execution failed during run"
2544         cmp /bin/sleep $f
2545         if [[ $? != 0 ]]; then
2546                 local slp_sum2=$(md5sum /bin/sleep)
2547                 # in case sleep file is modified during the test
2548                 [[ $slp_sum1 == $slp_sum2 ]] &&
2549                         error "Binary overwritten during exec"
2550         fi
2551
2552         check_hsm_flags $f "0x00000009"
2553 }
2554 run_test 30c "Update during exec of released file must fail"
2555
2556 restore_and_check_size() {
2557         local f=$1
2558         local fid=$2
2559         local s=$(stat -c "%s" $f)
2560         local n=$s
2561         local st=$(get_hsm_flags $f)
2562         local err=0
2563         local cpt=0
2564         $LFS hsm_restore $f
2565         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2566         do
2567                 n=$(stat -c "%s" $f)
2568                 # we echo in both cases to show stat is not hang
2569                 if [[ $n != $s ]]; then
2570                         echo "size seen is $n != $s"
2571                         err=1
2572                 else
2573                         echo "size seen is right: $n == $s"
2574                 fi
2575                 sleep 10
2576                 cpt=$((cpt + 1))
2577                 st=$(get_hsm_flags $f)
2578         done
2579         if [[ "$st" = "0x00000009" ]]; then
2580                 echo " "done
2581         else
2582                 echo " restore is too long"
2583                 wait_request_state $fid RESTORE SUCCEED
2584         fi
2585         return $err
2586 }
2587
2588 test_31a() {
2589         # test needs a running copytool
2590         copytool setup
2591
2592         create_archive_file $tdir/$tfile
2593         local f=$DIR/$tdir/$tfile
2594         copytool import $tdir/$tfile $f
2595         local fid=$($LFS path2fid $f)
2596         copytool setup
2597
2598         restore_and_check_size $f $fid
2599         local err=$?
2600
2601         [[ $err -eq 0 ]] || error "File size changed during restore"
2602 }
2603 run_test 31a "Import a large file and check size during restore"
2604
2605
2606 test_31b() {
2607         # test needs a running copytool
2608         copytool setup
2609
2610         local f=$DIR/$tdir/$tfile
2611         local fid=$(create_file "$f" 1MB 39)
2612
2613         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2614         wait_request_state $fid ARCHIVE SUCCEED
2615         $LFS hsm_release $f
2616
2617         restore_and_check_size $f $fid
2618         local err=$?
2619
2620         [[ $err -eq 0 ]] || error "File size changed during restore"
2621 }
2622 run_test 31b "Restore a large unaligned file and check size during restore"
2623
2624 test_31c() {
2625         # test needs a running copytool
2626         copytool setup
2627
2628         local f=$DIR/$tdir/$tfile
2629         local fid=$(create_file "$f" 1M 39)
2630
2631         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2632         wait_request_state $fid ARCHIVE SUCCEED
2633         $LFS hsm_release $f
2634
2635         restore_and_check_size $f $fid
2636         local err=$?
2637
2638         [[ $err -eq 0 ]] || error "File size changed during restore"
2639 }
2640 run_test 31c "Restore a large aligned file and check size during restore"
2641
2642 test_33() {
2643         local f=$DIR/$tdir/$tfile
2644         local fid=$(create_empty_file "$f")
2645
2646         copytool setup
2647
2648         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2649         wait_request_state $fid ARCHIVE SUCCEED
2650         $LFS hsm_release $f
2651
2652         # Prevent restore from completing
2653         copytool_suspend
2654
2655         # Implicit restore
2656         md5sum $f >/dev/null &
2657         local pid=$!
2658
2659         wait_request_state $fid RESTORE STARTED
2660         kill -15 $pid
2661
2662         copytool_continue
2663
2664         # Check restore trigger process was killed
2665         wait $pid
2666         [ $? -eq 143 ] || error "md5sum was not 'Terminated'"
2667 }
2668 run_test 33 "Kill a restore waiting process"
2669
2670 test_34() {
2671         # test needs a running copytool
2672         copytool setup -b 1
2673
2674         local f=$DIR/$tdir/$tfile
2675         local fid=$(create_empty_file "$f")
2676
2677         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2678         wait_request_state $fid ARCHIVE SUCCEED
2679         $LFS hsm_release $f
2680
2681         # Prevent restore from completing
2682         copytool_suspend
2683
2684         md5sum $f >/dev/null &
2685         local pid=$!
2686
2687         wait_request_state $fid RESTORE STARTED
2688
2689         # rm must not block during restore
2690         timeout --signal=KILL 1 rm "$f" || error "rm $f failed"
2691
2692         copytool_continue
2693         wait_request_state $fid RESTORE SUCCEED
2694
2695         # Check md5sum pgm finished
2696         kill -0 $pid && error "Restore initiatior still running"
2697         wait $pid || error "Restore initiator failed with $?"
2698
2699         # Check the file was actually deleted
2700         [ ! -f "$f" ] || error "$f was not deleted"
2701 }
2702 run_test 34 "Remove file during restore"
2703
2704 test_35() {
2705         # test needs a running copytool
2706         copytool setup -b 1
2707
2708         local f=$DIR/$tdir/$tfile
2709         local f1=$DIR/$tdir/$tfile-1
2710         local fid=$(create_empty_file "$f")
2711         local fid1=$(copy_file /etc/passwd $f1)
2712
2713         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2714         wait_request_state $fid ARCHIVE SUCCEED
2715         $LFS hsm_release $f
2716
2717         # Prevent restore from completing
2718         copytool_suspend
2719
2720         md5sum $f >/dev/null &
2721         local pid=$!
2722
2723         wait_request_state $fid RESTORE STARTED
2724
2725         # mv must not block during restore
2726         timeout --signal=KILL 1 mv "$f1" "$f" || error "mv $f1 $f failed"
2727
2728         copytool_continue
2729         wait_request_state $fid RESTORE SUCCEED
2730
2731         # Check md5sum pgm finished
2732         kill -0 $pid && error "Restore initiatior still running"
2733         wait $pid || error "Restore initiator failed with $?"
2734
2735         local fid2=$(path2fid $f)
2736         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2737 }
2738 run_test 35 "Overwrite file during restore"
2739
2740 test_36() {
2741         # test needs a running copytool
2742         copytool setup -b 1
2743
2744         local f=$DIR/$tdir/$tfile
2745         local fid=$(create_empty_file "$f")
2746
2747         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2748         wait_request_state $fid ARCHIVE SUCCEED
2749         $LFS hsm_release $f
2750
2751         # Prevent restore from completing
2752         copytool_suspend
2753
2754         md5sum $f >/dev/null &
2755         local pid=$!
2756
2757         wait_request_state $fid RESTORE STARTED
2758
2759         # mv must not block during restore
2760         timeout --signal=KILL 10 mv "$f" "$f.new" ||
2761                 error "mv '$f' '$f.new' failed with rc=$?"
2762
2763         copytool_continue
2764         wait_request_state $fid RESTORE SUCCEED
2765
2766         # Check md5sum pgm finished
2767         kill -0 $pid && error "Restore initiator is still running"
2768         wait $pid || error "Restore initiator failed with $?"
2769 }
2770 run_test 36 "Move file during restore"
2771
2772 test_37() {
2773         # LU-5683: check that an archived dirty file can be rearchived.
2774         copytool setup
2775
2776         local f=$DIR/$tdir/$tfile
2777         local fid
2778
2779         fid=$(create_small_file $f) || error "cannot create small file"
2780
2781         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2782         wait_request_state $fid ARCHIVE SUCCEED
2783         $LFS hsm_release $f || error "cannot release $f"
2784
2785         # Allow previous archive request to expire from the actions log.
2786         wait_for_grace_delay
2787
2788         # Dirty file.
2789         dd if=/dev/urandom of=$f bs=1M count=1 || error "cannot dirty file"
2790
2791         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2792         wait_request_state $fid ARCHIVE SUCCEED
2793 }
2794 run_test 37 "re-archive a dirty file"
2795
2796 multi_archive() {
2797         local prefix=$1
2798         local count=$2
2799         local n=""
2800
2801         for n in $(seq 1 $count); do
2802                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2803         done
2804         echo "$count archive requests submitted"
2805 }
2806
2807 test_40() {
2808         local stream_count=4
2809         local file_count=100
2810         mkdir -p $DIR/$tdir
2811         local f=$DIR/$tdir/$tfile
2812         local i=""
2813         local p=""
2814         local fid=""
2815         local max_requests=$(get_hsm_param max_requests)
2816
2817         stack_trap "set_hsm_param max_requests $max_requests" EXIT
2818         # Increase the number of HSM request that can be performed in
2819         # parallel. With the coordinator running once per second, this
2820         # also limits the number of requests per seconds that can be
2821         # performed, so we pick a decent number. But we also need to keep
2822         # that number low because the copytool has no rate limit and will
2823         # fail some requests if if gets too many at once.
2824         set_hsm_param max_requests 300
2825
2826         for i in $(seq 1 $file_count); do
2827                 for p in $(seq 1 $stream_count); do
2828                         fid=$(copy_file /etc/hosts $f.$p.$i)
2829                 done
2830         done
2831
2832         copytool setup
2833
2834         # to be sure wait_all_done will not be mislead by previous tests
2835         cdt_purge
2836         wait_for_grace_delay
2837         typeset -a pids
2838         # start archive streams in background (archive files in parallel)
2839         for p in $(seq 1 $stream_count); do
2840                 multi_archive $f.$p $file_count &
2841                 pids[$p]=$!
2842         done
2843         echo -n  "Wait for all requests being enqueued..."
2844         wait ${pids[*]}
2845         echo OK
2846         wait_all_done 100
2847 }
2848 run_test 40 "Parallel archive requests"
2849
2850 test_52() {
2851         # test needs a running copytool
2852         copytool setup
2853
2854         mkdir -p $DIR/$tdir
2855         local f=$DIR/$tdir/$tfile
2856         local fid=$(create_small_file $f)
2857
2858         $LFS hsm_archive $f || error "could not archive file"
2859         wait_request_state $fid ARCHIVE SUCCEED
2860         check_hsm_flags $f "0x00000009"
2861
2862         multiop_bg_pause $f O_c || error "multiop failed"
2863         local MULTIPID=$!
2864
2865         mds_evict_client
2866         client_up || client_up || true
2867
2868         kill -USR1 $MULTIPID
2869         wait $MULTIPID || error "multiop close failed"
2870
2871         check_hsm_flags $f "0x0000000b"
2872 }
2873 run_test 52 "Opened for write file on an evicted client should be set dirty"
2874
2875 test_53() {
2876         # test needs a running copytool
2877         copytool setup
2878
2879         mkdir -p $DIR/$tdir
2880         local f=$DIR/$tdir/$tfile
2881         local fid=$(create_small_file $f)
2882
2883         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2884                 error "could not archive file"
2885         wait_request_state $fid ARCHIVE SUCCEED
2886         check_hsm_flags $f "0x00000009"
2887
2888         multiop_bg_pause $f o_c || error "multiop failed"
2889         MULTIPID=$!
2890
2891         mds_evict_client
2892         client_up || client_up || true
2893
2894         kill -USR1 $MULTIPID
2895         wait $MULTIPID || error "multiop close failed"
2896
2897         check_hsm_flags $f "0x00000009"
2898 }
2899 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2900
2901 test_54() {
2902         local f=$DIR/$tdir/$tfile
2903         local fid=$(create_file "$f" 1MB 39)
2904
2905         copytool setup -b 1
2906
2907         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2908                 error "could not archive file"
2909         wait_request_state $fid ARCHIVE STARTED
2910
2911         check_hsm_flags $f "0x00000001"
2912
2913         stack_trap "cdt_clear_no_retry" EXIT
2914         # Avoid coordinator resending this request as soon it has failed.
2915         cdt_set_no_retry
2916
2917         echo "foo" >> $f
2918         sync
2919         wait_request_state $fid ARCHIVE FAILED
2920
2921         check_hsm_flags $f "0x00000003"
2922 }
2923 run_test 54 "Write during an archive cancels it"
2924
2925 test_55() {
2926         local f=$DIR/$tdir/$tfile
2927         local fid=$(create_file "$f" 1MB 39)
2928
2929         copytool setup -b 1
2930
2931         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2932                 error "could not archive file"
2933         wait_request_state $fid ARCHIVE STARTED
2934
2935         check_hsm_flags $f "0x00000001"
2936
2937         stack_trap "cdt_clear_no_retry" EXIT
2938         # Avoid coordinator resending this request as soon it has failed.
2939         cdt_set_no_retry
2940
2941         $TRUNCATE $f 1024 || error "truncate failed"
2942         sync
2943         wait_request_state $fid ARCHIVE FAILED
2944
2945         check_hsm_flags $f "0x00000003"
2946 }
2947 run_test 55 "Truncate during an archive cancels it"
2948
2949 test_56() {
2950         local f=$DIR/$tdir/$tfile
2951         local fid=$(create_file "$f" 1MB 39)
2952
2953         copytool setup -b 1
2954
2955         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2956                 error "could not archive file"
2957         wait_request_state $fid ARCHIVE STARTED
2958
2959         check_hsm_flags $f "0x00000001"
2960
2961         # Change metadata and sync to be sure we are not changing only
2962         # in memory.
2963         chmod 644 $f
2964         chgrp sys $f
2965         sync
2966         wait_request_state $fid ARCHIVE SUCCEED
2967
2968         check_hsm_flags $f "0x00000009"
2969 }
2970 run_test 56 "Setattr during an archive is ok"
2971
2972 test_57() {
2973         # Need one client for I/O, one for request
2974         needclients 2 || return 0
2975
2976         # test needs a running copytool
2977         copytool setup
2978
2979         mkdir -p $DIR/$tdir
2980         local f=$DIR/$tdir/test_archive_remote
2981         # Create a file on a remote node
2982         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2983                 "count=2 conv=fsync"
2984
2985         # And archive it
2986         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2987                 error "hsm_archive failed"
2988         local fid=$(path2fid $f)
2989         wait_request_state $fid ARCHIVE SUCCEED
2990
2991         # Release and implicit restore it
2992         do_node $CLIENT2 "$LFS hsm_release $f" ||
2993                 error "hsm_release failed"
2994         do_node $CLIENT2 "md5sum $f" ||
2995                 error "hsm_restore failed"
2996
2997         wait_request_state $fid RESTORE SUCCEED
2998 }
2999 run_test 57 "Archive a file with dirty cache on another node"
3000
3001 truncate_released_file() {
3002         local src_file=$1
3003         local trunc_to=$2
3004
3005         local sz=$(stat -c %s $src_file)
3006         local f=$DIR/$tdir/$tfile
3007         local fid=$(copy_file $1 $f)
3008         local ref=$f-ref
3009         cp $f $f-ref
3010
3011         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3012                 error "could not archive file"
3013         wait_request_state $fid ARCHIVE SUCCEED
3014
3015         $LFS hsm_release $f || error "could not release file"
3016
3017         $TRUNCATE $f $trunc_to || error "truncate failed"
3018         sync
3019
3020         local sz1=$(stat -c %s $f)
3021         [[ $sz1 == $trunc_to ]] ||
3022                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
3023
3024         $LFS hsm_state $f
3025         check_hsm_flags $f "0x0000000b"
3026
3027         local state=$(get_request_state $fid RESTORE)
3028         [[ "$state" == "SUCCEED" ]] ||
3029                 error "truncate $sz does not trig restore, state = $state"
3030
3031         $TRUNCATE $ref $trunc_to
3032         cmp $ref $f || error "file data wrong after truncate"
3033
3034         rm -f $f $f-ref
3035 }
3036
3037 test_58() {
3038         # test needs a running copytool
3039         copytool setup
3040
3041         mkdir -p $DIR/$tdir
3042
3043         local sz=$(stat -c %s /etc/passwd)
3044
3045         echo "truncate up from $sz to $((sz*2))"
3046         truncate_released_file /etc/passwd $((sz*2))
3047
3048         echo "truncate down from $sz to $((sz/2))"
3049         truncate_released_file /etc/passwd $((sz/2))
3050
3051         echo "truncate to 0"
3052         truncate_released_file /etc/passwd 0
3053 }
3054 run_test 58 "Truncate a released file will trigger restore"
3055
3056 test_59() {
3057         local fid
3058         [[ $MDS1_VERSION -lt $(version_code 2.7.63) ]] &&
3059                 skip "Need MDS version at least 2.7.63"
3060
3061         copytool setup
3062         $MCREATE $DIR/$tfile || error "mcreate failed"
3063         $TRUNCATE $DIR/$tfile 42 || error "truncate failed"
3064         $LFS hsm_archive $DIR/$tfile || error "archive request failed"
3065         fid=$(path2fid $DIR/$tfile)
3066         wait_request_state $fid ARCHIVE SUCCEED
3067         $LFS hsm_release $DIR/$tfile || error "release failed"
3068 }
3069 run_test 59 "Release stripeless file with non-zero size"
3070
3071 test_60() {
3072         # This test validates the fix for LU-4512. Ensure that the -u
3073         # option changes the progress reporting interval from the
3074         # default (30 seconds) to the user-specified interval.
3075         local f=$DIR/$tdir/$tfile
3076         local fid=$(create_file "$f" 1M 10)
3077
3078         local interval=5
3079         local progress_timeout=$((interval * 4))
3080         copytool setup -b 1 --update-interval $interval
3081
3082         local mdtidx=0
3083         local mdt=${MDT_PREFIX}${mdtidx}
3084         local mds=mds$((mdtidx + 1))
3085
3086         # Wait for copytool to register
3087         wait_update_facet $mds \
3088                 "$LCTL get_param -n ${mdt}.hsm.agents | grep -o ^uuid" \
3089                 uuid 100 || error "coyptool failed to register with $mdt"
3090
3091         local start_at=$(date +%s)
3092         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3093                 error "could not archive file"
3094
3095         local agent=$(facet_active_host $SINGLEAGT)
3096         local logfile=$(copytool_logfile $SINGLEAGT)
3097
3098         wait_update $agent \
3099             "grep -o start.copy \"$logfile\"" "start copy" 100 ||
3100                 error "copytool failed to start"
3101
3102         local cmd="$LCTL get_param -n ${mdt}.hsm.active_requests"
3103         cmd+=" | awk '/'$fid'.*action=ARCHIVE/ {print \\\$12}' | cut -f2 -d="
3104
3105         local RESULT
3106         local WAIT=0
3107         local sleep=1
3108
3109         echo -n "Expecting a progress update within $progress_timeout seconds... "
3110         while [ true ]; do
3111                 RESULT=$(do_node $(facet_active_host $mds) "$cmd")
3112                 if [ -n "$RESULT" ] && [ "$RESULT" -gt 0 ]; then
3113                         echo "$RESULT bytes copied in $WAIT seconds."
3114                         break
3115                 elif [ $WAIT -ge $progress_timeout ]; then
3116                         error "Timed out waiting for progress update!"
3117                         break
3118                 fi
3119                 WAIT=$((WAIT + sleep))
3120                 sleep $sleep
3121         done
3122
3123         local finish_at=$(date +%s)
3124         local elapsed=$((finish_at - start_at))
3125
3126         # Ensure that the progress update occurred within the expected window.
3127         if [ $elapsed -lt $((interval - 1)) ]; then
3128                 error "Expected progress update after at least $interval seconds"
3129         fi
3130
3131         echo "Wait for on going archive hsm action to complete"
3132         wait_update $agent "grep -o copied \"$logfile\"" "copied" 10 ||
3133                 echo "File archiving not completed even after 10 secs"
3134 }
3135 run_test 60 "Changing progress update interval from default"
3136
3137 test_61() {
3138         # test needs a running copytool
3139         copytool setup
3140
3141         mkdir -p $DIR/$tdir
3142         local f=$DIR/$tdir/$tfile
3143         local fid=$(copy_file /etc/passwd $f)
3144         cdt_disable
3145         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3146         rm -f $f
3147         cdt_enable
3148         wait_request_state $fid ARCHIVE FAILED
3149 }
3150 run_test 61 "Waiting archive of a removed file should fail"
3151
3152 test_70() {
3153         # test needs a new running copytool
3154         stack_trap copytool_monitor_cleanup EXIT
3155         copytool_monitor_setup
3156         copytool setup --event-fifo "$HSMTOOL_MONITOR_DIR/fifo"
3157
3158         # Wait for the copytool to register.
3159         wait_update --verbose $(facet_active_host mds1) \
3160                 "$LCTL get_param -n ${MDT_PREFIX}0.hsm.agents | grep -o ^uuid" \
3161                 uuid 100 ||
3162                 error "copytool failed to register with MDT0000"
3163
3164         kill_copytools
3165         wait_copytools || error "Copytools failed to stop"
3166
3167         local REGISTER_EVENT
3168         local UNREGISTER_EVENT
3169         while read event; do
3170                 local parsed=$(parse_json_event "$event")
3171                 if [ -z "$parsed" ]; then
3172                         error "Copytool sent malformed event: $event"
3173                 fi
3174                 eval $parsed
3175
3176                 if [ $event_type == "REGISTER" ]; then
3177                         REGISTER_EVENT=$event
3178                 elif [ $event_type == "UNREGISTER" ]; then
3179                         UNREGISTER_EVENT=$event
3180                 fi
3181         done < <(echo $"$(get_copytool_event_log)")
3182
3183         if [ -z "$REGISTER_EVENT" ]; then
3184                 error "Copytool failed to send register event to FIFO"
3185         fi
3186
3187         if [ -z "$UNREGISTER_EVENT" ]; then
3188                 error "Copytool failed to send unregister event to FIFO"
3189         fi
3190
3191         echo "Register/Unregister events look OK."
3192 }
3193 run_test 70 "Copytool logs JSON register/unregister events to FIFO"
3194
3195 test_71() {
3196         # Bump progress interval for livelier events.
3197         local interval=5
3198
3199         # test needs a new running copytool
3200         stack_trap copytool_monitor_cleanup EXIT
3201         copytool_monitor_setup
3202         copytool setup --update-interval $interval --event-fifo \
3203                 "$HSMTOOL_MONITOR_DIR/fifo"
3204
3205         stack_trap "cdt_clear_no_retry" EXIT
3206         # Just start and stop the copytool to generate events.
3207         cdt_clear_no_retry
3208
3209         local f=$DIR/$tdir/$tfile
3210         local fid=$(create_small_file "$f")
3211
3212         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
3213                 error "could not archive file"
3214         wait_request_state $fid ARCHIVE SUCCEED
3215
3216         local expected_fields="event_time data_fid source_fid"
3217         expected_fields+=" total_bytes current_bytes"
3218
3219         local -A events=(
3220                 [ARCHIVE_START]=false
3221                 [ARCHIVE_FINISH]=false
3222                 [ARCHIVE_RUNNING]=false
3223                 )
3224         while read event; do
3225                 # Make sure we're not getting anything from previous events.
3226                 for field in $expected_fields; do
3227                         unset $field
3228                 done
3229
3230                 local parsed=$(parse_json_event "$event")
3231                 if [ -z "$parsed" ]; then
3232                         error "Copytool sent malformed event: $event"
3233                 fi
3234                 eval $parsed
3235
3236                 events["$event_type"]=true
3237
3238                 [ "$event_type" != ARCHIVE_RUNNING ] && continue
3239
3240                 # Do some simple checking of the progress update events.
3241                 for expected_field in $expected_fields; do
3242                         if [ -z ${!expected_field+x} ]; then
3243                                 error "Missing $expected_field field in event"
3244                         fi
3245                 done
3246
3247                 [ $total_bytes -gt 0 ] || error "Expected total_bytes to be > 0"
3248
3249                 # These should be identical throughout an archive operation
3250                 [ $source_fid == $data_fid ] ||
3251                         error "Expected source_fid to equal data_fid"
3252         done < <(echo $"$(get_copytool_event_log)")
3253
3254         # Check we received every type of events we were expecting
3255         for event in "${!events[@]}"; do
3256                 ${events["$event"]} ||
3257                         error "Copytool failed to send '$event' event to FIFO"
3258         done
3259
3260         echo "Archive events look OK."
3261 }
3262 run_test 71 "Copytool logs JSON archive events to FIFO"
3263
3264 test_72() {
3265         # Bump progress interval for livelier events.
3266         local interval=5
3267
3268         # test needs a new running copytool
3269         stack_trap copytool_monitor_cleanup EXIT
3270         copytool_monitor_setup
3271         copytool setup --update-interval $interval --event-fifo \
3272                 "$HSMTOOL_MONITOR_DIR/fifo"
3273         local test_file=$HSMTOOL_MONITOR_DIR/file
3274
3275         local cmd="dd if=/dev/urandom of=$test_file count=16 bs=1000000 "
3276         cmd+="conv=fsync"
3277         do_facet $SINGLEAGT "$cmd" ||
3278                 error "cannot create $test_file on $SINGLEAGT"
3279         copy2archive $test_file $tdir/$tfile
3280
3281         mkdir -p $DIR/$tdir
3282         local f=$DIR/$tdir/$tfile
3283         copytool import $tdir/$tfile $f
3284         f=$DIR2/$tdir/$tfile
3285         echo "Verifying released state: "
3286         check_hsm_flags $f "0x0000000d"
3287
3288         local fid=$(path2fid $f)
3289         $LFS hsm_restore $f
3290         wait_request_state $fid RESTORE SUCCEED
3291
3292         local expected_fields="event_time data_fid source_fid"
3293         expected_fields+=" total_bytes current_bytes"
3294
3295         local START_EVENT
3296         local FINISH_EVENT
3297         while read event; do
3298                 # Make sure we're not getting anything from previous events.
3299                 for field in $expected_fields; do
3300                         unset $field
3301                 done
3302
3303                 local parsed=$(parse_json_event "$event")
3304                 if [ -z "$parsed" ]; then
3305                         error "Copytool sent malformed event: $event"
3306                 fi
3307                 eval $parsed
3308
3309                 if [ $event_type == "RESTORE_START" ]; then
3310                         START_EVENT=$event
3311                         if [ $source_fid != $data_fid ]; then
3312                                 error "source_fid should == data_fid at start"
3313                         fi
3314                         continue
3315                 elif [ $event_type == "RESTORE_FINISH" ]; then
3316                         FINISH_EVENT=$event
3317                         if [ $source_fid != $data_fid ]; then
3318                                 error "source_fid should == data_fid at finish"
3319                         fi
3320                         continue
3321                 elif [ $event_type != "RESTORE_RUNNING" ]; then
3322                         continue
3323                 fi
3324
3325                 # Do some simple checking of the progress update events.
3326                 for expected_field in $expected_fields; do
3327                         if [ -z ${!expected_field+x} ]; then
3328                                 error "Missing $expected_field field in event"
3329                         fi
3330                 done
3331
3332                 if [ $total_bytes -eq 0 ]; then
3333                         error "Expected total_bytes to be > 0"
3334                 fi
3335
3336                 # When a restore starts out, the data fid is the same as the
3337                 # source fid. After the restore has gotten going, we learn
3338                 # the new data fid. Once the restore has finished, the source
3339                 # fid is set to the new data fid.
3340                 #
3341                 # We test this because some monitoring software may depend on
3342                 # this behavior. If it changes, then the consumers of these
3343                 # events may need to be modified.
3344                 if [ $source_fid == $data_fid ]; then
3345                         error "source_fid should != data_fid during restore"
3346                 fi
3347         done < <(echo $"$(get_copytool_event_log)")
3348
3349         if [ -z "$START_EVENT" ]; then
3350                 error "Copytool failed to send restore start event to FIFO"
3351         fi
3352
3353         if [ -z "$FINISH_EVENT" ]; then
3354                 error "Copytool failed to send restore finish event to FIFO"
3355         fi
3356
3357         echo "Restore events look OK."
3358 }
3359 run_test 72 "Copytool logs JSON restore events to FIFO"
3360
3361 test_90() {
3362         file_count=51 # Max number of files constrained by LNET message size
3363         mkdir $DIR/$tdir || error "mkdir $DIR/$tdir failed"
3364         local f=$DIR/$tdir/$tfile
3365         local FILELIST=/tmp/filelist.txt
3366         local i=""
3367
3368         rm -f $FILELIST
3369         for i in $(seq 1 $file_count); do
3370                 fid=$(copy_file /etc/hosts $f.$i)
3371                 echo $f.$i >> $FILELIST
3372         done
3373
3374         copytool setup
3375         # to be sure wait_all_done will not be mislead by previous tests
3376         cdt_purge
3377         wait_for_grace_delay
3378         $LFS hsm_archive --filelist $FILELIST ||
3379                 error "cannot archive a file list"
3380         wait_all_done 100
3381         $LFS hsm_release --filelist $FILELIST ||
3382                 error "cannot release a file list"
3383         $LFS hsm_restore --filelist $FILELIST ||
3384                 error "cannot restore a file list"
3385         wait_all_done 100
3386 }
3387 run_test 90 "Archive/restore a file list"
3388
3389 double_verify_reset_hsm_param() {
3390         local p=$1
3391         echo "Testing $HSM_PARAM.$p"
3392         local val=$(get_hsm_param $p)
3393         local save=$val
3394         local val2=$(($val * 2))
3395         set_hsm_param $p $val2
3396         val=$(get_hsm_param $p)
3397         [[ $val == $val2 ]] ||
3398                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
3399         echo "Set $p to 0 must failed"
3400         set_hsm_param $p 0
3401         local rc=$?
3402         # restore value
3403         set_hsm_param $p $save
3404
3405         if [[ $rc == 0 ]]; then
3406                 error "we must not be able to set $HSM_PARAM.$p to 0"
3407         fi
3408 }
3409
3410 test_100() {
3411         double_verify_reset_hsm_param loop_period
3412         double_verify_reset_hsm_param grace_delay
3413         double_verify_reset_hsm_param active_request_timeout
3414         double_verify_reset_hsm_param max_requests
3415         double_verify_reset_hsm_param default_archive_id
3416 }
3417 run_test 100 "Set coordinator /proc tunables"
3418
3419 test_102() {
3420         cdt_disable
3421         cdt_enable
3422         cdt_restart
3423 }
3424 run_test 102 "Verify coordinator control"
3425
3426 test_103() {
3427         # test needs a running copytool
3428         copytool setup
3429
3430         local i=""
3431         local fid=""
3432
3433         mkdir -p $DIR/$tdir
3434         for i in $(seq 1 20); do
3435                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
3436         done
3437         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
3438
3439         cdt_purge
3440
3441         echo "Current requests"
3442         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3443                         $HSM_PARAM.actions |\
3444                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
3445
3446         [[ -z "$res" ]] || error "Some request have not been canceled"
3447 }
3448 run_test 103 "Purge all requests"
3449
3450 DATA=CEA
3451 DATAHEX='[434541]'
3452 test_104() {
3453         local f=$DIR/$tdir/$tfile
3454         local fid=$(create_empty_file "$f")
3455
3456         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
3457         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3458                         $HSM_PARAM.actions |\
3459                         grep $fid | cut -f16 -d=")
3460
3461         [[ "$data1" == "$DATAHEX" ]] ||
3462                 error "Data field in records is ($data1) and not ($DATAHEX)"
3463
3464         cdt_purge
3465 }
3466 run_test 104 "Copy tool data field"
3467
3468 test_105() {
3469         local max_requests=$(get_hsm_param max_requests)
3470         mkdir -p $DIR/$tdir
3471         local i=""
3472
3473         stack_trap "set_hsm_param max_requests $max_requests" EXIT
3474         set_hsm_param max_requests 300
3475
3476         cdt_disable
3477         for i in $(seq -w 1 10); do
3478                 cp /etc/passwd $DIR/$tdir/$i
3479                 $LFS hsm_archive $DIR/$tdir/$i
3480         done
3481         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3482                         $HSM_PARAM.actions |\
3483                         grep WAITING | wc -l")
3484         cdt_restart
3485
3486         cdt_disable
3487         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3488                         $HSM_PARAM.actions |\
3489                         grep WAITING | wc -l")
3490         cdt_enable
3491         cdt_purge
3492         [[ "$reqcnt1" == "$reqcnt2" ]] ||
3493                 error "Requests count after shutdown $reqcnt2 != "\
3494                       "before shutdown $reqcnt1"
3495 }
3496 run_test 105 "Restart of coordinator"
3497
3498 test_106() {
3499         # test needs a running copytool
3500         copytool setup
3501
3502         local uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3503
3504         check_agent_registered $uuid
3505
3506         search_copytools || error "No copytool found"
3507
3508         kill_copytools
3509         wait_copytools || error "Copytool failed to stop"
3510
3511         check_agent_unregistered $uuid
3512
3513         copytool setup
3514         uuid=$(get_agent_uuid $(facet_active_host $SINGLEAGT))
3515         check_agent_registered $uuid
3516 }
3517 run_test 106 "Copytool register/unregister"
3518
3519 test_107() {
3520         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
3521
3522         # test needs a running copytool
3523         copytool setup
3524         # create and archive file
3525         mkdir -p $DIR/$tdir
3526         local f1=$DIR/$tdir/$tfile
3527         local fid=$(copy_file /etc/passwd $f1)
3528         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3529         wait_request_state $fid ARCHIVE SUCCEED
3530         # shutdown and restart MDS
3531         fail $SINGLEMDS
3532         # check the copytool still gets messages from MDT
3533         local f2=$DIR/$tdir/2
3534         local fid=$(copy_file /etc/passwd $f2)
3535         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
3536         # main check of this sanity: this request MUST succeed
3537         wait_request_state $fid ARCHIVE SUCCEED
3538 }
3539 run_test 107 "Copytool re-register after MDS restart"
3540
3541 policy_set_and_test()
3542 {
3543         local change="$1"
3544         local target="$2"
3545         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
3546         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3547         [[ "$policy" == "$target" ]] ||
3548                 error "Wrong policy after '$change': '$policy' != '$target'"
3549 }
3550
3551 test_109() {
3552         # to force default policy setting if error
3553         CDT_POLICY_HAD_CHANGED=true
3554
3555         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3556         local default="NonBlockingRestore [NoRetryAction]"
3557         [[ "$policy" == "$default" ]] ||
3558                 error "default policy has changed,"\
3559                       " '$policy' != '$default' update the test"
3560         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
3561         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
3562         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
3563         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
3564         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
3565         # useless bacause we know but safer for futur changes to use real value
3566         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
3567         echo "Next set_param must failed"
3568         policy_set_and_test "wrong" "$policy"
3569
3570         # return to default
3571         echo "Back to default policy"
3572         cdt_set_sanity_policy
3573 }
3574 run_test 109 "Policy display/change"
3575
3576 test_110a() {
3577         # test needs a running copytool
3578         copytool setup
3579
3580         mkdir -p $DIR/$tdir
3581
3582         copy2archive /etc/passwd $tdir/$tfile
3583
3584         local f=$DIR/$tdir/$tfile
3585         copytool import $tdir/$tfile $f
3586         local fid=$(path2fid $f)
3587
3588         cdt_set_non_blocking_restore
3589         md5sum $f
3590         local st=$?
3591
3592         # cleanup
3593         wait_request_state $fid RESTORE SUCCEED
3594         cdt_clear_non_blocking_restore
3595
3596         # Test result
3597         [[ $st == 1 ]] ||
3598                 error "md5sum returns $st != 1, "\
3599                         "should also perror ENODATA (No data available)"
3600 }
3601 run_test 110a "Non blocking restore policy (import case)"
3602
3603 test_110b() {
3604         # test needs a running copytool
3605         copytool setup
3606
3607         mkdir -p $DIR/$tdir
3608         local f=$DIR/$tdir/$tfile
3609         local fid=$(copy_file /etc/passwd $f)
3610         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3611         wait_request_state $fid ARCHIVE SUCCEED
3612         $LFS hsm_release $f
3613
3614         cdt_set_non_blocking_restore
3615         md5sum $f
3616         local st=$?
3617
3618         # cleanup
3619         wait_request_state $fid RESTORE SUCCEED
3620         cdt_clear_non_blocking_restore
3621
3622         # Test result
3623         [[ $st == 1 ]] ||
3624                 error "md5sum returns $st != 1, "\
3625                         "should also perror ENODATA (No data available)"
3626 }
3627 run_test 110b "Non blocking restore policy (release case)"
3628
3629 test_111a() {
3630         # test needs a running copytool
3631         copytool setup
3632
3633         mkdir -p $DIR/$tdir
3634         copy2archive /etc/passwd $tdir/$tfile
3635
3636         local f=$DIR/$tdir/$tfile
3637
3638         copytool import $tdir/$tfile $f
3639         local fid=$(path2fid $f)
3640
3641         cdt_set_no_retry
3642
3643         copytool_remove_backend $fid
3644
3645         $LFS hsm_restore $f
3646         wait_request_state $fid RESTORE FAILED
3647         local st=$?
3648
3649         # cleanup
3650         cdt_clear_no_retry
3651
3652         # Test result
3653         [[ $st == 0 ]] || error "Restore does not failed"
3654 }
3655 run_test 111a "No retry policy (import case), restore will error"\
3656               " (No such file or directory)"
3657
3658 test_111b() {
3659         # test needs a running copytool
3660         copytool setup
3661
3662         mkdir -p $DIR/$tdir
3663         local f=$DIR/$tdir/$tfile
3664         local fid=$(copy_file /etc/passwd $f)
3665         stack_trap cdt_clear_no_retry EXIT
3666         cdt_set_no_retry
3667         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3668         wait_request_state $fid ARCHIVE SUCCEED
3669         $LFS hsm_release $f
3670
3671         copytool_remove_backend $fid
3672
3673         $LFS hsm_restore $f
3674         wait_request_state $fid RESTORE FAILED
3675         local st=$?
3676
3677         # Test result
3678         [[ $st == 0 ]] || error "Restore does not failed"
3679 }
3680 run_test 111b "No retry policy (release case), restore will error"\
3681               " (No such file or directory)"
3682
3683 test_112() {
3684         # test needs a running copytool
3685         copytool setup
3686
3687         mkdir -p $DIR/$tdir
3688         local f=$DIR/$tdir/$tfile
3689         local fid=$(copy_file /etc/passwd $f)
3690         cdt_disable
3691         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3692         local l=$($LFS hsm_action $f)
3693         echo $l
3694         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
3695
3696         cdt_enable
3697         wait_request_state $fid ARCHIVE SUCCEED
3698
3699         # Test result
3700         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
3701 }
3702 run_test 112 "State of recorded request"
3703
3704 test_113() {
3705         local file1=$DIR/$tdir/$tfile
3706         local file2=$DIR2/$tdir/$tfile
3707
3708         local fid=$(create_small_sync_file $file1)
3709
3710         stack_trap "zconf_umount \"$(facet_host $SINGLEAGT)\" \"$MOUNT3\"" EXIT
3711         zconf_mount "$(facet_host $SINGLEAGT)" "$MOUNT3" ||
3712                 error "cannot mount '$MOUNT3' on '$SINGLEAGT'"
3713
3714         copytool setup -m  "$MOUNT3"
3715
3716         do_nodes $(comma_list $(nodes_list)) $LCTL clear
3717
3718         $LFS hsm_archive $file1 || error "Fail to archive $file1"
3719         wait_request_state $fid ARCHIVE SUCCEED
3720
3721         $LFS hsm_release $file1
3722         echo "Verifying released state: "
3723         check_hsm_flags $file1 "0x0000000d"
3724
3725         multiop_bg_pause $file1 oO_WRONLY:O_APPEND:_w4c || error "multiop failed"
3726         MULTIPID=$!
3727         stat $file2 &
3728         kill -USR1 $MULTIPID
3729
3730         wait
3731         sync
3732
3733         local size1=$(stat -c "%s" $file1)
3734         local size2=$(stat -c "%s" $file2)
3735
3736         [ $size1 -eq $size2 ] || error "sizes are different $size1 $size2"
3737 }
3738 run_test 113 "wrong stat after restore"
3739
3740 test_200() {
3741         local f=$DIR/$tdir/$tfile
3742         local fid=$(create_empty_file "$f")
3743
3744         copytool setup
3745
3746         # Prevent archive from completing
3747         copytool_suspend
3748
3749         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3750         # wait archive to register at CDT
3751         wait_request_state $fid ARCHIVE STARTED
3752
3753         # Cancel the archive
3754         $LFS hsm_cancel "$f"
3755
3756         wait_request_state $fid ARCHIVE CANCELED
3757
3758         copytool_continue
3759         wait_request_state $fid CANCEL SUCCEED
3760 }
3761 run_test 200 "Register/Cancel archive"
3762
3763 test_201() {
3764         # test needs a running copytool
3765         copytool setup
3766
3767         local f=$DIR/$tdir/$tfile
3768         create_archive_file $tdir/$tfile
3769         copytool import $tdir/$tfile $f
3770         local fid=$(path2fid $f)
3771
3772         # test with cdt on is made in test_222
3773         cdt_disable
3774         $LFS hsm_restore $f
3775         # wait restore to register at CDT
3776         wait_request_state $fid RESTORE WAITING
3777         $LFS hsm_cancel $f
3778         cdt_enable
3779         wait_request_state $fid RESTORE CANCELED
3780         wait_request_state $fid CANCEL SUCCEED
3781 }
3782 run_test 201 "Register/Cancel restore"
3783
3784 test_202() {
3785         local f=$DIR/$tdir/$tfile
3786         local fid=$(create_empty_file "$f")
3787
3788         # test needs a running copytool
3789         copytool setup
3790
3791         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3792         wait_request_state $fid ARCHIVE SUCCEED
3793
3794         copytool_suspend
3795         $LFS hsm_remove $f
3796         # wait remove to register at CDT
3797         wait_request_state $fid REMOVE STARTED
3798         $LFS hsm_cancel $f
3799
3800         wait_request_state $fid REMOVE CANCELED
3801 }
3802 run_test 202 "Register/Cancel remove"
3803
3804 test_220A() { # was test_220
3805         # test needs a running copytool
3806         copytool setup
3807
3808         mkdir -p $DIR/$tdir
3809
3810         local f=$DIR/$tdir/$tfile
3811         local fid=$(copy_file /etc/passwd $f)
3812
3813         changelog_register
3814
3815         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3816         wait_request_state $fid ARCHIVE SUCCEED
3817
3818         changelog_find -type HSM -target-fid $fid -flags 0x0 ||
3819                 error "The expected changelog was not emitted"
3820 }
3821 run_test 220A "Changelog for archive"
3822
3823 test_220a() {
3824         # test needs a running copytool
3825         copytool setup
3826
3827         mkdir -p $DIR/$tdir
3828
3829         local f=$DIR/$tdir/$tfile
3830         local fid=$(copy_file /etc/passwd $f)
3831
3832         changelog_register
3833
3834         # block copytool operations to allow for HSM request to be
3835         # submitted and file be unlinked (CDT will find object removed)
3836         copytool_suspend
3837
3838         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3839
3840         # wait request to reach CT
3841         wait_request_state $fid ARCHIVE STARTED
3842
3843         rm -f $f
3844
3845         copytool_continue
3846
3847         wait_request_state $fid ARCHIVE FAILED
3848
3849         # HE_ARCHIVE|ENOENT
3850         changelog_find -type HSM -target-fid $fid -flags 0x2 ||
3851                 error "The expected changelog was not emitted"
3852 }
3853 run_test 220a "Changelog for failed archive"
3854
3855 test_221() {
3856         local f=$DIR/$tdir/$tfile
3857         local fid=$(create_empty_file "$f")
3858
3859         copytool setup -b 1
3860         changelog_register
3861
3862         # Prevent archive from completing
3863         copytool_suspend
3864         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3865         wait_request_state $fid ARCHIVE STARTED
3866
3867         $LFS hsm_cancel $f
3868         wait_request_state $fid ARCHIVE CANCELED
3869
3870         copytool_continue
3871         wait_request_state $fid CANCEL SUCCEED
3872
3873         changelog_find -type HSM -target-fid $fid -flags 0x7d ||
3874                 error "The expected changelog was not emitted"
3875 }
3876 run_test 221 "Changelog for archive canceled"
3877
3878 test_222a() {
3879         # test needs a running copytool
3880         copytool setup
3881
3882         mkdir -p $DIR/$tdir
3883         copy2archive /etc/passwd $tdir/$tfile
3884
3885         local f=$DIR/$tdir/$tfile
3886         copytool import $tdir/$tfile $f
3887         local fid=$(path2fid $f)
3888
3889         changelog_register
3890
3891         $LFS hsm_restore $f
3892         wait_request_state $fid RESTORE SUCCEED
3893
3894         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
3895                 error "The expected changelog was not emitted"
3896 }
3897 run_test 222a "Changelog for explicit restore"
3898
3899 test_222b() {
3900         # test needs a running copytool
3901         copytool setup
3902
3903         mkdir -p $DIR/$tdir
3904         local f=$DIR/$tdir/$tfile
3905         local fid=$(copy_file /etc/passwd $f)
3906
3907         changelog_register
3908         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3909         wait_request_state $fid ARCHIVE SUCCEED
3910         $LFS hsm_release $f
3911
3912         md5sum $f
3913
3914         wait_request_state $fid RESTORE SUCCEED
3915
3916         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
3917                 error "The expected changelog was not emitted"
3918 }
3919 run_test 222b "Changelog for implicit restore"
3920
3921 test_222c() {
3922         # test needs a running copytool
3923         copytool setup
3924
3925         mkdir -p $DIR/$tdir
3926         copy2archive /etc/passwd $tdir/$tfile
3927
3928         local f=$DIR/$tdir/$tfile
3929         copytool import $tdir/$tfile $f
3930         local fid=$(path2fid $f)
3931
3932         changelog_register
3933
3934         # block copytool operations to allow for HSM request to be
3935         # submitted and file be unlinked (CDT will find object removed)
3936         copytool_suspend
3937
3938         $LFS hsm_restore $f
3939
3940         # wait request to reach CT
3941         wait_request_state $fid RESTORE STARTED
3942
3943         rm -f $f
3944
3945         copytool_continue
3946
3947         wait_request_state $fid RESTORE FAILED
3948
3949         # HE_RESTORE|ENOENT
3950         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
3951                 error "The expected changelog was not emitted"
3952 }
3953 run_test 222c "Changelog for failed explicit restore"
3954
3955 test_222d() {
3956         # test needs a running copytool
3957         copytool setup
3958
3959         mkdir -p $DIR/$tdir
3960         local f=$DIR/$tdir/$tfile
3961         local fid=$(copy_file /etc/passwd $f)
3962
3963         changelog_register
3964         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3965         wait_request_state $fid ARCHIVE SUCCEED
3966         $LFS hsm_release $f
3967
3968         copytool_remove_backend $fid
3969         md5sum $f
3970
3971         wait_request_state $fid RESTORE FAILED
3972
3973         # HE_RESTORE|ENOENT
3974         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
3975                 error "The expected changelog was not emitted"
3976 }
3977 run_test 222d "Changelog for failed implicit restore"
3978
3979 test_223a() {
3980         # test needs a running copytool
3981         copytool setup -b 1
3982
3983         local f=$DIR/$tdir/$tfile
3984         create_archive_file $tdir/$tfile
3985
3986         changelog_register
3987
3988         copytool import $tdir/$tfile $f
3989         local fid=$(path2fid $f)
3990
3991         $LFS hsm_restore $f
3992         wait_request_state $fid RESTORE STARTED
3993         $LFS hsm_cancel $f
3994         wait_request_state $fid RESTORE CANCELED
3995         wait_request_state $fid CANCEL SUCCEED
3996
3997         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
3998                 error "The expected changelog was not emitted"
3999 }
4000 run_test 223a "Changelog for restore canceled (import case)"
4001
4002 test_223b() {
4003         local f=$DIR/$tdir/$tfile
4004         local fid=$(create_empty_file "$f")
4005
4006         copytool setup -b 1
4007         changelog_register
4008
4009         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4010         wait_request_state $fid ARCHIVE SUCCEED
4011         $LFS hsm_release $f
4012
4013         # Prevent restore from completing
4014         copytool_suspend
4015         $LFS hsm_restore $f
4016         wait_request_state $fid RESTORE STARTED
4017
4018         $LFS hsm_cancel $f
4019         wait_request_state $fid RESTORE CANCELED
4020
4021         copytool_continue
4022         wait_request_state $fid CANCEL SUCCEED
4023
4024         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4025                 error "The expected changelog was not emitted"
4026 }
4027 run_test 223b "Changelog for restore canceled (release case)"
4028
4029 test_224A() { # was test_224
4030         # test needs a running copytool
4031         copytool setup
4032
4033         mkdir -p $DIR/$tdir
4034
4035         local f=$DIR/$tdir/$tfile
4036         local fid=$(copy_file /etc/passwd $f)
4037
4038         changelog_register
4039         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4040         wait_request_state $fid ARCHIVE SUCCEED
4041
4042         $LFS hsm_remove $f
4043         wait_request_state $fid REMOVE SUCCEED
4044
4045         changelog_find -type HSM -target-fid $fid -flags 0x200 ||
4046                 error "The expected changelog was not emitted"
4047 }
4048 run_test 224A "Changelog for remove"
4049
4050 test_224a() {
4051         # test needs a running copytool
4052         copytool setup
4053
4054         mkdir -p $DIR/$tdir
4055
4056         local f=$DIR/$tdir/$tfile
4057         local fid=$(copy_file /etc/passwd $f)
4058
4059         changelog_register
4060         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4061         wait_request_state $fid ARCHIVE SUCCEED
4062
4063         copytool_remove_backend $fid
4064
4065         # block copytool operations to allow for HSM request to be
4066         # submitted and file be unlinked (CDT will find object removed)
4067         copytool_suspend
4068
4069         $LFS hsm_remove $f
4070
4071         # wait for request to reach CT
4072         wait_request_state $fid REMOVE STARTED
4073
4074         rm -f $f
4075
4076         copytool_continue
4077
4078         wait_request_state $fid REMOVE FAILED
4079
4080         # HE_REMOVE|ENOENT=0x202
4081         changelog_find -type HSM -target-fid $fid -flags 0x202 ||
4082                 error "The expected changelog was not emitted"
4083 }
4084 run_test 224a "Changelog for failed remove"
4085
4086 test_225() {
4087         # test is not usable because remove request is too fast
4088         # so it is always finished before cancel can be done ...
4089         echo "Test disabled"
4090         return 0
4091
4092         # test needs a running copytool
4093         copytool setup
4094
4095         local f=$DIR/$tdir/$tfile
4096         local fid=$(create_empty_file "$f")
4097
4098         changelog_register
4099         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4100         wait_request_state $fid ARCHIVE SUCCEED
4101
4102         # Prevent restore from completing
4103         copytool_suspend
4104         $LFS hsm_remove $f
4105
4106         $LFS hsm_cancel $f
4107         wait_request_state $fid REMOVE CANCELED
4108
4109         copytool_continue
4110         wait_request_state $fid CANCEL SUCCEED
4111
4112         changelog_find -type HSM -target-fid $fid -flags 0x27d
4113                 error "The expected changelog was not emitted"
4114 }
4115 run_test 225 "Changelog for remove canceled"
4116
4117 test_226() {
4118         # test needs a running copytool
4119         copytool setup
4120
4121         mkdir -p $DIR/$tdir
4122
4123         local f1=$DIR/$tdir/$tfile-1
4124         local f2=$DIR/$tdir/$tfile-2
4125         local f3=$DIR/$tdir/$tfile-3
4126         local fid1=$(copy_file /etc/passwd $f1)
4127         local fid2=$(copy_file /etc/passwd $f2)
4128         copy_file /etc/passwd $f3
4129
4130         changelog_register
4131         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4132         wait_request_state $fid1 ARCHIVE SUCCEED
4133
4134         $LFS hsm_archive $f2
4135         wait_request_state $fid2 ARCHIVE SUCCEED
4136
4137         rm $f1 || error "rm $f1 failed"
4138
4139         changelog_dump
4140         changelog_find -type UNLNK -target-fid $fid1 -flags 0x3 ||
4141                 error "The expected changelog was not emitted"
4142
4143         mv $f3 $f2 || error "mv $f3 $f2 failed"
4144
4145         changelog_find -type RENME -target-fid $fid2 -flags 0x3 ||
4146                 error "The expected changelog was not emitted"
4147 }
4148 run_test 226 "changelog for last rm/mv with exiting archive"
4149
4150 # This is just a utility function to clarify what test_227 does
4151 __test_227()
4152 {
4153         local target=0x280
4154
4155         "$LFS" "$action" --$flag "$file" ||
4156                 error "Cannot ${action#hsm_} $flag on '$file'"
4157
4158         # Only one changelog should be produced
4159         local entries="$(changelog_find -type HSM -target-fid $fid)"
4160         [ $(wc -l <<< "$entries") -eq $((++count)) ] ||
4161                 error "lfs $action --$flag '$file' produced more than one" \
4162                       "changelog record"
4163
4164         # Parse the last changelog record
4165         local entry="$(tail -n 1 <<< "$entries")"
4166         eval local -A changelog=$(changelog2array $entry)
4167
4168         # Also check the flags match what is expected
4169         [[ ${changelog[flags]} == $target ]] ||
4170                 error "Changelog flag is '${changelog[flags]}', not $target"
4171 }
4172
4173 test_227() {
4174         local file="$DIR/$tdir/$tfile"
4175         local fid=$(create_empty_file "$file")
4176         local count=0
4177
4178         changelog_register
4179
4180         for flag in norelease noarchive exists archived lost; do
4181                 if [ "$flag" == lost ]; then
4182                         # The flag "lost" only works on an archived file
4183                         "$LFS" hsm_set --archived "$file"
4184                         ((count++))
4185                 fi
4186
4187                 action="hsm_set" __test_227
4188                 action="hsm_clear" __test_227
4189         done
4190 }
4191 run_test 227 "changelog when explicit setting of HSM flags"
4192
4193 test_228() {
4194         # test needs a running copytool
4195         copytool setup
4196
4197         local fid=$(create_small_sync_file $DIR/$tfile)
4198         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4199         wait_request_state $fid ARCHIVE SUCCEED
4200
4201         $LFS hsm_release $DIR/$tfile
4202         check_hsm_flags $DIR/$tfile "0x0000000d"
4203
4204         filefrag $DIR/$tfile | grep " 1 extent found" ||
4205                 error "filefrag on released file must return only one extent"
4206
4207         # only newer versions of cp detect sparse files by stat/FIEMAP
4208         # (LU-2580)
4209         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4210                 error "copying $DIR/$tfile"
4211         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4212
4213         $LFS hsm_release $DIR/$tfile
4214         check_hsm_flags $DIR/$tfile "0x0000000d"
4215
4216         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4217
4218         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4219                 error "tar failed"
4220         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4221                 error "comparing untarred $DIR/$tfile"
4222
4223         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4224                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4225 }
4226 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4227
4228 test_250() {
4229         local file="$DIR/$tdir/$tfile"
4230
4231         # set max_requests to allow one request of each type to be started (3)
4232         stack_trap \
4233                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4234         set_hsm_param max_requests 3
4235         # speed up test
4236         stack_trap \
4237                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4238         set_hsm_param loop_period 1
4239
4240         # send 1 requests of each kind twice
4241         copytool setup
4242         # setup the files
4243         for action in archive restore remove; do
4244                 local filepath="$file"-to-$action
4245                 local fid=$(create_empty_file "$filepath")
4246                 local fid2=$(create_empty_file "$filepath".bis)
4247
4248                 if [ "$action" != archive ]; then
4249                         "$LFS" hsm_archive "$filepath"
4250                         wait_request_state $fid ARCHIVE SUCCEED
4251                         "$LFS" hsm_archive "$filepath".bis
4252                         wait_request_state $fid2 ARCHIVE SUCCEED
4253                 fi
4254                 if [ "$action" == restore ]; then
4255                         "$LFS" hsm_release "$filepath"
4256                         "$LFS" hsm_release "$filepath".bis
4257                 fi
4258         done
4259
4260         # suspend the copytool to prevent requests from completing
4261         stack_trap "copytool_continue" EXIT
4262         copytool_suspend
4263
4264         # send `max_requests' requests (one of each kind)
4265         for action in archive restore remove; do
4266                 filepath="$file"-to-$action
4267                 "$LFS" hsm_${action} "$filepath"
4268                 wait_request_state $(path2fid "$filepath") "${action^^}" STARTED
4269         done
4270
4271         # send another batch of requests
4272         for action in archive restore remove; do
4273                 "$LFS" hsm_${action} "$file-to-$action".bis
4274         done
4275         # wait for `loop_period' seconds to make sure the coordinator has time
4276         # to register those, even though it should not
4277         sleep 1
4278
4279         # only the first batch of request should be started
4280         local -i count
4281         count=$(do_facet $SINGLEMDS "$LCTL" get_param -n $HSM_PARAM.actions |
4282                 grep -c STARTED)
4283
4284         ((count == 3)) ||
4285                 error "expected 3 STARTED requests, found $count"
4286 }
4287 run_test 250 "Coordinator max request"
4288
4289 test_251() {
4290         local f=$DIR/$tdir/$tfile
4291         local fid=$(create_empty_file "$f")
4292
4293         cdt_disable
4294         # to have a short test
4295         local old_to=$(get_hsm_param active_request_timeout)
4296         set_hsm_param active_request_timeout 1
4297         # to be sure the cdt will wake up frequently so
4298         # it will be able to cancel the "old" request
4299         local old_loop=$(get_hsm_param loop_period)
4300         set_hsm_param loop_period 1
4301         cdt_enable
4302
4303         copytool setup
4304
4305         # Prevent archive from completing
4306         copytool_suspend
4307         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4308         wait_request_state $fid ARCHIVE STARTED
4309
4310         # Let the request timeout
4311         wait_request_state $fid ARCHIVE CANCELED
4312
4313         set_hsm_param active_request_timeout $old_to
4314         set_hsm_param loop_period $old_loop
4315 }
4316 run_test 251 "Coordinator request timeout"
4317
4318 test_252() {
4319         local f=$DIR/$tdir/$tfile
4320         local fid=$(create_empty_file "$f")
4321
4322         # to have a short test
4323         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4324         set_hsm_param loop_period 1
4325
4326         copytool setup
4327
4328         # Prevent archive from completing
4329         copytool_suspend
4330         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4331         wait_request_state $fid ARCHIVE STARTED
4332         rm -f "$f"
4333
4334         stack_trap "set_hsm_param active_request_timeout \
4335                     $(get_hsm_param active_request_timeout)" EXIT
4336         set_hsm_param active_request_timeout 1
4337
4338         wait_request_state $fid ARCHIVE CANCELED
4339         copytool_continue
4340 }
4341 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4342
4343 test_253() {
4344         local rc
4345         # test needs a running copytool
4346         copytool setup
4347
4348         mkdir -p $DIR/$tdir
4349         local f=$DIR/$tdir/$tfile
4350
4351         dd if=/dev/zero of=$f bs=1MB count=10
4352         local fid=$(path2fid $f)
4353
4354         $LFS hsm_archive $f || error "could not archive file"
4355         wait_request_state $fid ARCHIVE SUCCEED
4356
4357         # clear locks to discard inode data
4358         cancel_lru_locks osc
4359
4360         #define OBD_FAIL_MDC_MERGE              0x807
4361         $LCTL set_param fail_loc=0x807
4362
4363         #expect error here, instead of release with wrong size
4364         $LFS hsm_release $f
4365         rc=$?
4366         if ((rc == 0)); then
4367                 file_size=$(stat -c '%s' $f)
4368                 if ((file_size != 10485760)); then
4369                         error "Wrong file size after hsm_release"
4370                 fi
4371         else
4372                 echo "could not release file"
4373         fi
4374 }
4375 run_test 253 "Check for wrong file size after release"
4376
4377 test_254a()
4378 {
4379         [ $MDS1_VERSION -lt $(version_code 2.10.56) ] &&
4380                 skip "need MDS version at least 2.10.56"
4381
4382         # Check that the counters are initialized to 0
4383         local count
4384         for request_type in archive restore remove; do
4385                 count="$(get_hsm_param ${request_type}_count)" ||
4386                         error "Reading ${request_type}_count failed with $?"
4387
4388                 [ "$count" -eq 0 ] ||
4389                         error "Expected ${request_type}_count to be " \
4390                               "0 != '$count'"
4391         done
4392 }
4393 run_test 254a "Request counters are initialized to zero"
4394
4395 test_254b()
4396 {
4397         [ $MDS1_VERSION -lt $(version_code 2.10.56) ] &&
4398                 skip "need MDS version at least 2.10.56"
4399
4400         # The number of request to launch (at least 32)
4401         local request_count=$((RANDOM % 32 + 32))
4402         printf "Will launch %i requests of each type\n" "$request_count"
4403
4404         # Launch a copytool to process requests
4405         copytool setup
4406
4407         # Set hsm.max_requests to allow starting all requests at the same time
4408         stack_trap \
4409                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4410         set_hsm_param max_requests "$request_count"
4411
4412         local timeout
4413         local count
4414         for request_type in archive restore remove; do
4415                 printf "Checking %s requests\n" "${request_type}"
4416                 # Suspend the copytool to give us time to read the proc files
4417                 copytool_suspend
4418
4419                 for ((i = 0; i < $request_count; i++)); do
4420                         case $request_type in
4421                         archive)
4422                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4423                                         >/dev/null 2>&1
4424                                 ;;
4425                         restore)
4426                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4427                                 ;;
4428                         esac
4429                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4430                 done
4431
4432                 # Give the coordinator 10 seconds to start every request
4433                 timeout=10
4434                 while get_hsm_param actions | grep -q WAITING; do
4435                         sleep 1
4436                         let timeout-=1
4437                         [ $timeout -gt 0 ] ||
4438                                 error "${request_type^} requests took too " \
4439                                       "long to start"
4440                 done
4441
4442                 count="$(get_hsm_param ${request_type}_count)"
4443                 [ "$count" -eq "$request_count" ] ||
4444                         error "Expected '$request_count' (!= '$count') " \
4445                               "active $request_type requests"
4446
4447                 # Let the copytool process the requests
4448                 copytool_continue
4449                 # Give it 10 seconds maximum
4450                 timeout=10
4451                 while get_hsm_param actions | grep -q STARTED; do
4452                         sleep 1
4453                         let timeout-=1
4454                         [ $timeout -gt 0 ] ||
4455                                 error "${request_type^} requests took too " \
4456                                       "long to complete"
4457                 done
4458
4459                 count="$(get_hsm_param ${request_type}_count)"
4460                 [ "$count" -eq 0 ] ||
4461                         error "Expected 0 (!= '$count') " \
4462                               "active $request_type requests"
4463         done
4464 }
4465 run_test 254b "Request counters are correctly incremented and decremented"
4466
4467 test_255()
4468 {
4469         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4470                 skip "Need MDS version at least 2.12.0"
4471
4472         local file="$DIR/$tdir/$tfile"
4473         local fid=$(create_empty_file "$file")
4474
4475         # How do you make sure the coordinator has consumed any outstanding
4476         # event, without triggering an event yourself?
4477         #
4478         # You wait for a request to disappear from the coordinator's llog.
4479
4480         # Warning: the setup represents 90% of this test
4481
4482         # Create and process an HSM request
4483         copytool setup
4484         "$LFS" hsm_archive "$file"
4485         wait_request_state $fid ARCHIVE SUCCEED
4486
4487         kill_copytools
4488         wait_copytools || error "failed to stop copytools"
4489
4490         # Launch a new HSM request
4491         rm "$file"
4492         create_empty_file "$file"
4493         "$LFS" hsm_archive "$file"
4494
4495         cdt_shutdown
4496
4497         # Have the completed request be removed as soon as the cdt wakes up
4498         stack_trap "set_hsm_param grace_delay $(get_hsm_param grace_delay)" EXIT
4499         set_hsm_param grace_delay 1
4500         # (Hopefully, time on the MDS will behave nicely)
4501         do_facet $SINGLEMDS sleep 2 &
4502
4503         # Increase `loop_period' as a mean to prevent the coordinator from
4504         # waking itself up to do some housekeeping.
4505         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4506         set_hsm_param loop_period 1000
4507
4508         wait $! || error "waiting failed"
4509         cdt_enable
4510         wait_request_state $fid ARCHIVE ""
4511         # The coordinator will not wake up on its own for ~`loop_period' secs...
4512
4513         # ... Unless a copytool registers. Now the real test begins
4514         copytool setup
4515         wait_request_state $(path2fid "$file") ARCHIVE SUCCEED
4516 }
4517 run_test 255 "Copytool registration wakes the coordinator up"
4518
4519 # tests 260[a-c] rely on the parsing of the copytool's log file, they might
4520 # break in the future because of that.
4521 test_260a()
4522 {
4523         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
4524                 skip "need MDS version 2.11.56 or later"
4525
4526         local -a files=("$DIR/$tdir/$tfile".{0..15})
4527         local file
4528
4529         for file in "${files[@]}"; do
4530                 create_small_file "$file"
4531         done
4532
4533         # Set a few hsm parameters
4534         stack_trap \
4535                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4536         set_hsm_param loop_period 1
4537         stack_trap \
4538                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4539         set_hsm_param max_requests 3
4540
4541         # Release one file
4542         copytool setup
4543         "$LFS" hsm_archive "${files[0]}"
4544         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4545         "$LFS" hsm_release "${files[0]}"
4546
4547         # Stop the copytool
4548         kill_copytools
4549         wait_copytools || error "copytools failed to stop"
4550
4551         # Send several archive requests
4552         for file in "${files[@]:1}"; do
4553                 "$LFS" hsm_archive "$file"
4554         done
4555
4556         # Send one restore request
4557         "$LFS" hsm_restore "${files[0]}"
4558
4559         # Launch a copytool
4560         copytool setup
4561
4562         # Wait for all the requests to complete
4563         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4564         for file in "${files[@]:1}"; do
4565                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4566         done
4567
4568         # Collect the actions in the order in which the copytool processed them
4569         local -a actions=(
4570                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4571                         "$(copytool_logfile "$SINGLEAGT")")
4572                 )
4573
4574         printf '%s\n' "${actions[@]}"
4575
4576         local action
4577         for action in "${actions[@]:0:3}"; do
4578                 [ "$action" == RESTORE ] && return
4579         done
4580
4581         error "Too many ARCHIVE requests were run before the RESTORE request"
4582 }
4583 run_test 260a "Restore request have priority over other requests"
4584
4585 # This test is very much tied to the implementation of the current priorisation
4586 # mechanism in the coordinator. It might not make sense to keep it in the future
4587 test_260b()
4588 {
4589         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
4590                 skip "need MDS version 2.11.56 or later"
4591
4592         local -a files=("$DIR/$tdir/$tfile".{0..15})
4593         local file
4594
4595         for file in "${files[@]}"; do
4596                 create_small_file "$file"
4597         done
4598
4599         # Set a few hsm parameters
4600         stack_trap \
4601                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4602         set_hsm_param loop_period 1
4603         stack_trap \
4604                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4605         set_hsm_param max_requests 3
4606
4607         # Release one file
4608         copytool setup --archive-id 2
4609         "$LFS" hsm_archive --archive 2 "${files[0]}"
4610         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4611         "$LFS" hsm_release "${files[0]}"
4612
4613         # Stop the copytool
4614         kill_copytools
4615         wait_copytools || error "copytools failed to stop"
4616
4617         # Send several archive requests
4618         for file in "${files[@]:1}"; do
4619                 "$LFS" hsm_archive "$file"
4620         done
4621
4622         # Send one restore request
4623         "$LFS" hsm_restore "${files[0]}"
4624
4625         # Launch a copytool
4626         copytool setup
4627         copytool setup --archive-id 2
4628
4629         # Wait for all the requests to complete
4630         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4631         for file in "${files[@]:1}"; do
4632                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4633         done
4634
4635         # Collect the actions in the order in which the copytool processed them
4636         local -a actions=(
4637                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4638                         "$(copytool_logfile "$SINGLEAGT")")
4639                 )
4640
4641         printf '%s\n' "${actions[@]}"
4642
4643         local action
4644         for action in "${actions[@]:0:3}"; do
4645                 [ "$action" == RESTORE ] && return
4646         done
4647
4648         error "Too many ARCHIVE requests were run before the RESTORE request"
4649 }
4650 run_test 260b "Restore request have priority over other requests"
4651
4652 # This test is very much tied to the implementation of the current priorisation
4653 # mechanism in the coordinator. It might not make sense to keep it in the future
4654 test_260c()
4655 {
4656         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4657                 skip "Need MDS version at least 2.12.0"
4658
4659         local -a files=("$DIR/$tdir/$tfile".{0..15})
4660         local file
4661
4662         for file in "${files[@]}"; do
4663                 create_small_file "$file"
4664         done
4665
4666         # Set a few hsm parameters
4667         stack_trap \
4668                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4669         set_hsm_param loop_period 1000
4670         stack_trap \
4671                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4672         set_hsm_param max_requests 3
4673
4674         # Release one file
4675         copytool setup --archive-id 2
4676         "$LFS" hsm_archive --archive 2 "${files[0]}"
4677         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4678         "$LFS" hsm_release "${files[0]}"
4679
4680         # Stop the copytool
4681         kill_copytools
4682         wait_copytools || error "copytools failed to stop"
4683
4684         # Force the next coordinator run to do housekeeping
4685         cdt_shutdown
4686         cdt_enable
4687
4688         "$LFS" hsm_archive "${files[1]}"
4689
4690         # Launch a copytool
4691         copytool setup
4692         copytool setup --archive-id 2
4693
4694         wait_request_state "$(path2fid "${files[1]}")" ARCHIVE SUCCEED
4695         # The coordinator just did a housekeeping run it won't do another one
4696         # for around `loop_period' seconds => requests will not be reordered
4697         # if it costs too much (ie. when the coordinator has to discard a whole
4698         # hal)
4699
4700         # Send several archive requests
4701         for file in "${files[@]:2}"; do
4702                 "$LFS" hsm_archive "$file"
4703         done
4704
4705         # Send one restore request
4706         "$LFS" hsm_restore "${files[0]}"
4707
4708         # Wait for all the requests to complete
4709         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4710         for file in "${files[@]:2}"; do
4711                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4712         done
4713
4714         # Collect the actions in the order in which the copytool processed them
4715         local -a actions=(
4716                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4717                         "$(copytool_logfile "$SINGLEAGT")")
4718                 )
4719
4720         printf '%s\n' "${actions[@]}"
4721
4722         local action
4723         for action in "${actions[@]:0:3}"; do
4724                 [ "$action" == RESTORE ] &&
4725                         error "Restore requests should not be prioritised" \
4726                               "unless the coordinator is doing housekeeping"
4727         done
4728         return 0
4729 }
4730 run_test 260c "Requests are not reordered on the 'hot' path of the coordinator"
4731
4732 test_300() {
4733         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4734
4735         # the only way to test ondisk conf is to restart MDS ...
4736         echo "Stop coordinator and remove coordinator state at mount"
4737         # stop coordinator
4738         cdt_shutdown
4739         # clean on disk conf set by default
4740         cdt_clear_mount_state
4741         cdt_check_state stopped
4742
4743         # check cdt still off after umount/remount
4744         fail $SINGLEMDS
4745         cdt_check_state stopped
4746
4747         echo "Set coordinator start at mount, and start coordinator"
4748         cdt_set_mount_state enabled
4749
4750         # check cdt is on
4751         cdt_check_state enabled
4752
4753         # check cdt still on after umount/remount
4754         fail $SINGLEMDS
4755         cdt_check_state enabled
4756
4757         # we are back to original state (cdt started at mount)
4758 }
4759 run_test 300 "On disk coordinator state kept between MDT umount/mount"
4760
4761 test_301() {
4762         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4763
4764         local ai=$(get_hsm_param default_archive_id)
4765         local new=$((ai + 1))
4766
4767         set_hsm_param default_archive_id $new -P
4768         fail $SINGLEMDS
4769         local res=$(get_hsm_param default_archive_id)
4770
4771         # clear value
4772         set_hsm_param default_archive_id "" "-P -d"
4773
4774         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4775 }
4776 run_test 301 "HSM tunnable are persistent"
4777
4778 test_302() {
4779         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4780
4781         local ai=$(get_hsm_param default_archive_id)
4782         local new=$((ai + 1))
4783
4784         # stop coordinator
4785         cdt_shutdown
4786
4787         set_hsm_param default_archive_id $new -P
4788
4789         local mdtno
4790         for mdtno in $(seq 1 $MDSCOUNT); do
4791                 fail mds${mdtno}
4792         done
4793
4794         # check cdt is on
4795         cdt_check_state enabled
4796
4797         local res=$(get_hsm_param default_archive_id)
4798
4799         # clear value
4800         set_hsm_param default_archive_id "" "-P -d"
4801
4802         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
4803 }
4804 run_test 302 "HSM tunnable are persistent when CDT is off"
4805
4806 test_400() {
4807         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4808
4809         copytool setup
4810
4811         mkdir -p $DIR/$tdir
4812
4813         local dir_mdt0=$DIR/$tdir/mdt0
4814         local dir_mdt1=$DIR/$tdir/mdt1
4815
4816         # create 1 dir per MDT
4817         stack_trap "rm -rf $dir_mdt0" EXIT
4818         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4819         stack_trap "rm -rf $dir_mdt1" EXIT
4820         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4821
4822         # create 1 file in each MDT
4823         local fid1=$(create_small_file $dir_mdt0/$tfile)
4824         local fid2=$(create_small_file $dir_mdt1/$tfile)
4825
4826         # check that hsm request on mdt0 is sent to the right MDS
4827         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4828         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4829                 echo "archive successful on mdt0"
4830
4831         # check that hsm request on mdt1 is sent to the right MDS
4832         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
4833         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4834                 echo "archive successful on mdt1"
4835 }
4836 run_test 400 "Single request is sent to the right MDT"
4837
4838 test_401() {
4839         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4840
4841         copytool setup
4842
4843         mkdir -p $DIR/$tdir
4844
4845         local dir_mdt0=$DIR/$tdir/mdt0
4846         local dir_mdt1=$DIR/$tdir/mdt1
4847
4848         # create 1 dir per MDT
4849         stack_trap "rm -rf $dir_mdt0" EXIT
4850         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4851         stack_trap "rm -rf $dir_mdt1" EXIT
4852         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
4853
4854         # create 1 file in each MDT
4855         local fid1=$(create_small_file $dir_mdt0/$tfile)
4856         local fid2=$(create_small_file $dir_mdt1/$tfile)
4857
4858         # check that compound requests are shunt to the rights MDTs
4859         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
4860                 error "lfs hsm_archive"
4861         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4862                 echo "archive successful on mdt0"
4863         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
4864                 echo "archive successful on mdt1"
4865 }
4866 run_test 401 "Compound requests split and sent to their respective MDTs"
4867
4868 mdc_change_state() # facet, MDT_pattern, activate|deactivate
4869 {
4870         local facet=$1
4871         local pattern="$2"
4872         local state=$3
4873         local node=$(facet_active_host $facet)
4874         local mdc
4875         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
4876                         awk '{print $4}'); do
4877                 echo "$3 $mdc on $node"
4878                 do_facet $facet "$LCTL --device $mdc $state" || return 1
4879         done
4880 }
4881
4882 test_402a() {
4883         # deactivate all mdc on agent1
4884         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
4885
4886         copytool setup --no-fail
4887
4888         check_agent_unregistered "uuid" # match any agent
4889
4890         # no expected running copytool
4891         search_copytools $agent && error "Copytool start should have failed"
4892
4893         # reactivate MDCs
4894         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
4895 }
4896 run_test 402a "Copytool start fails if all MDTs are inactive"
4897
4898 test_402b() {
4899         copytool setup
4900
4901         mkdir -p $DIR/$tdir
4902
4903         local f=$DIR/$tdir/$tfile
4904         touch $f || error "touch $f failed"
4905         local fid=$(path2fid $f)
4906
4907 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
4908         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
4909         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4910
4911         # give time for CDT to send request and to keep it for retry
4912         wait_for_loop_period
4913
4914         wait_request_state $fid ARCHIVE WAITING
4915
4916         do_facet $SINGLEAGT lctl set_param fail_loc=0
4917
4918         # request should succeed now
4919         wait_request_state $fid ARCHIVE SUCCEED
4920 }
4921 run_test 402b "CDT must retry request upon slow start of CT"
4922
4923 test_403() {
4924         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4925
4926         local agent=$(facet_active_host $SINGLEAGT)
4927
4928         # deactivate all mdc for MDT0001
4929         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4930
4931         copytool setup
4932         local uuid=$(get_agent_uuid $agent)
4933         # check the agent is registered on MDT0000, and not on MDT0001
4934         check_agent_registered_by_mdt $uuid 0
4935         check_agent_unregistered_by_mdt $uuid 1
4936
4937         # check running copytool process
4938         search_copytools $agent || error "No running copytools on $agent"
4939
4940         # reactivate all mdc for MDT0001
4941         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4942
4943         # make sure the copytool is now registered to all MDTs
4944         check_agent_registered $uuid
4945 }
4946 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
4947
4948 test_404() {
4949         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4950
4951         copytool setup
4952
4953         # create files on both MDT0000 and MDT0001
4954         mkdir -p $DIR/$tdir
4955
4956         local dir_mdt0=$DIR/$tdir/mdt0
4957         stack_trap "rm -rf $dir_mdt0" EXIT
4958         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
4959
4960         # create 1 file on mdt0
4961         local fid1=$(create_small_file $dir_mdt0/$tfile)
4962
4963         # deactivate all mdc for MDT0001
4964         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
4965
4966         # send an HSM request for files in MDT0000
4967         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
4968
4969         # check for completion of files in MDT0000
4970         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
4971                 echo "archive successful on mdt0"
4972
4973         # reactivate all mdc for MDT0001
4974         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
4975 }
4976 run_test 404 "Inactive MDT does not block requests for active MDTs"
4977
4978 test_405() {
4979         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
4980
4981         copytool setup
4982
4983         mkdir -p $DIR/$tdir
4984
4985         local striped_dir=$DIR/$tdir/striped_dir
4986
4987         # create striped dir on all of MDTs
4988         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
4989
4990         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
4991         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
4992         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
4993         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
4994
4995         local idx1=$($LFS getstripe -m $striped_dir/${tfile}_0)
4996         local idx2=$($LFS getstripe -m $striped_dir/${tfile}_1)
4997         local idx3=$($LFS getstripe -m $striped_dir/${tfile}_2)
4998         local idx4=$($LFS getstripe -m $striped_dir/${tfile}_3)
4999
5000         # check that compound requests are shunt to the rights MDTs
5001         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5002                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5003                 error "lfs hsm_archive"
5004
5005         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5006                 echo "archive successful on $fid1"
5007         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5008                 echo "archive successful on $fid2"
5009         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5010                 echo "archive successful on $fid3"
5011         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5012                 echo "archive successful on $fid4"
5013
5014         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5015         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5016         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5017         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5018
5019         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5020         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5021         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5022         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5023 }
5024 run_test 405 "archive and release under striped directory"
5025
5026 test_406() {
5027         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5028
5029         [ $MDS1_VERSION -lt $(version_code 2.7.64) ] &&
5030                 skip "need MDS version at least 2.7.64"
5031
5032         local fid
5033         local mdt_index
5034
5035         fid=$(create_small_file $DIR/$tdir/$tfile)
5036         echo "old fid $fid"
5037
5038         copytool setup
5039
5040         $LFS hsm_archive $DIR/$tdir/$tfile
5041         wait_request_state "$fid" ARCHIVE SUCCEED
5042         $LFS hsm_release $DIR/$tdir/$tfile
5043
5044         # Should migrate $tdir but not $tfile.
5045         $LFS migrate -m1 $DIR/$tdir &&
5046                 error "migrating HSM an archived file should fail"
5047
5048         $LFS hsm_restore $DIR/$tdir/$tfile
5049         wait_request_state "$fid" RESTORE SUCCEED
5050
5051         $LFS hsm_remove $DIR/$tdir/$tfile
5052         wait_request_state "$fid" REMOVE SUCCEED
5053
5054         cat $DIR/$tdir/$tfile > /dev/null ||
5055                 error "cannot read $DIR/$tdir/$tfile"
5056
5057         $LFS migrate -m1 $DIR/$tdir ||
5058                 error "cannot complete migration after HSM remove"
5059
5060         mdt_index=$($LFS getstripe -m $DIR/$tdir)
5061         if ((mdt_index != 1)); then
5062                 error "expected MDT index 1, got $mdt_index"
5063         fi
5064
5065         # Refresh fid after migration.
5066         fid=$(path2fid $DIR/$tdir/$tfile)
5067         echo "new fid $fid"
5068
5069         $LFS hsm_archive $DIR/$tdir/$tfile
5070         wait_request_state "$fid" ARCHIVE SUCCEED 1
5071
5072         lctl set_param debug=+trace
5073         $LFS hsm_release $DIR/$tdir/$tfile ||
5074                 error "cannot release $DIR/$tdir/$tfile"
5075
5076         $LFS hsm_restore $DIR/$tdir/$tfile
5077         wait_request_state "$fid" RESTORE SUCCEED 1
5078
5079         cat $DIR/$tdir/$tfile > /dev/null ||
5080                 error "cannot read $DIR/$tdir/$tfile"
5081 }
5082 run_test 406 "attempting to migrate HSM archived files is safe"
5083
5084 test_407() {
5085         local f=$DIR/$tdir/$tfile
5086         local f2=$DIR2/$tdir/$tfile
5087         local fid=$(create_empty_file "$f")
5088
5089         copytool setup
5090
5091         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5092         wait_request_state $fid ARCHIVE SUCCEED
5093         $LFS hsm_release $f
5094
5095 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5096         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5097
5098         # Prevent restore from completing
5099         copytool_suspend
5100
5101         md5sum $f &
5102         # 1st request holds layout lock while appropriate
5103         # RESTORE record is still not added to llog
5104         md5sum $f2 &
5105         sleep 2
5106
5107         # after umount hsm_actions->O/x/x log shouldn't have
5108         # double RESTORE records like below
5109         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5110         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5111         sleep 30 &&
5112                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5113         fail $SINGLEMDS
5114
5115         copytool_continue
5116         wait_request_state $fid RESTORE SUCCEED
5117 }
5118 run_test 407 "Check for double RESTORE records in llog"
5119
5120 test_500()
5121 {
5122         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
5123                 skip "HSM migrate is not supported"
5124
5125         test_mkdir -p $DIR/$tdir
5126
5127         if [ $(lustre_version_code client) -lt $(version_code 2.11.56) ] ||
5128              [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ];
5129         then
5130                 llapi_hsm_test -d $DIR/$tdir -b ||
5131                         error "One llapi HSM test failed"
5132         else
5133                 llapi_hsm_test -d $DIR/$tdir ||
5134                         error "One llapi HSM test failed"
5135         fi
5136 }
5137 run_test 500 "various LLAPI HSM tests"
5138
5139 test_600() {
5140         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5141                 skip "need MDS version at least 2.10.58"
5142
5143         mkdir -p $DIR/$tdir
5144
5145         local f=$DIR/$tdir/$tfile
5146
5147         changelog_register
5148         # set changelog_mask to ALL
5149         changelog_chmask "ALL"
5150
5151         chmod 777 $DIR/$tdir
5152         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5153         local fid=$(path2fid $f)
5154
5155         local entry
5156         entry=$(changelog_find -type CREAT -target-fid $fid -uid "$RUNAS_ID" \
5157                                -gid "$RUNAS_GID") ||
5158                 error "No matching CREAT entry"
5159
5160         # Parse the changelog
5161         eval local -A changelog=$(changelog2array $entry)
5162         local nid="${changelog[nid]}"
5163
5164         # Check its NID
5165         echo "Got NID '$nid'"
5166         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5167                 error "nid '$nid' does not match any client NID:" \
5168                       "${CLIENT_NIDS[@]}"
5169 }
5170 run_test 600 "Changelog fields 'u=' and 'nid='"
5171
5172 test_601() {
5173         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5174                 skip "need MDS version at least 2.10.58"
5175
5176         mkdir -p $DIR/$tdir
5177
5178         local f=$DIR/$tdir/$tfile
5179
5180         changelog_register
5181         # set changelog_mask to ALL
5182         changelog_chmask "ALL"
5183
5184         touch $f || error "touch $f failed"
5185         local fid=$(path2fid $f)
5186
5187         changelog_clear
5188         cat $f || error "cat $f failed"
5189
5190         changelog_find -type OPEN -target-fid $fid -mode "r--" ||
5191                 error "No matching OPEN entry"
5192 }
5193 run_test 601 "OPEN Changelog entry"
5194
5195 test_602() {
5196         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5197                 skip "need MDS version at least 2.10.58"
5198
5199         mkdir -p $DIR/$tdir
5200
5201         local f=$DIR/$tdir/$tfile
5202
5203         changelog_register
5204         # set changelog_mask to ALL
5205         changelog_chmask "ALL"
5206
5207         touch $f || error "touch $f failed"
5208         local fid=$(path2fid $f)
5209
5210         changelog_clear
5211         cat $f || error "cat $f failed"
5212
5213         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5214
5215         changelog_clear
5216         changelog_dump
5217         echo f > $f || error "write $f failed"
5218         changelog_dump
5219
5220         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5221
5222         # remove OPEN from changelog_mask
5223         changelog_chmask "-OPEN"
5224
5225         changelog_clear
5226         changelog_dump
5227         cat $f || error "cat $f failed"
5228         changelog_dump
5229
5230         changelog_find -type CLOSE -target-fid $fid &&
5231                 error "There should be no CLOSE entry"
5232
5233         changelog_clear
5234         changelog_dump
5235         echo f > $f || error "write $f failed"
5236         changelog_dump
5237
5238         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5239 }
5240 run_test 602 "Changelog record CLOSE only if open+write or OPEN recorded"
5241
5242 test_603() {
5243         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5244                 skip "need MDS version at least 2.10.58"
5245
5246         mkdir -p $DIR/$tdir
5247
5248         local f=$DIR/$tdir/$tfile
5249
5250         changelog_register
5251         # set changelog_mask to ALL
5252         changelog_chmask "ALL"
5253
5254         touch $f || error "touch $f failed"
5255         local fid=$(path2fid $f)
5256
5257         setfattr -n user.xattr1 -v "value1" $f || error "setfattr $f failed"
5258
5259         changelog_clear
5260         getfattr -n user.xattr1 $f || error "getfattr $f failed"
5261
5262         changelog_find -type GXATR -target-fid $fid -xattr "user.xattr1" ||
5263                 error "No matching GXATR entry"
5264 }
5265 run_test 603 "GETXATTR Changelog entry"
5266
5267 test_604() {
5268         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5269                 skip "need MDS version at least 2.10.58"
5270
5271         mkdir -p $DIR/$tdir
5272
5273         local f=$DIR/$tdir/$tfile
5274         local f2=$DIR2/$tdir/$tfile
5275         local procname="mdd.$FSNAME-MDT0000.changelog_deniednext"
5276         local timeout
5277         timeout="$(do_facet mds1 "$LCTL" get_param -n "$procname")"
5278         stack_trap "do_facet mds1 '$LCTL' set_param '$procname=$timeout'" EXIT
5279         do_facet mds1 lctl set_param "$procname=20"
5280
5281
5282         changelog_register
5283         # set changelog_mask to ALL
5284         changelog_chmask "ALL"
5285
5286         touch $f || error "touch $f failed"
5287         local fid=$(path2fid $f)
5288
5289         chmod 600 $f
5290
5291         changelog_clear
5292         changelog_dump
5293         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5294         changelog_dump
5295
5296         local entry
5297         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5298                                -gid "$RUNAS_GID" -mode "r--") ||
5299                 error "No matching NOPEN entry"
5300
5301         # Parse the changelog
5302         eval local -A changelog=$(changelog2array $entry)
5303         local nid="${changelog[nid]}"
5304
5305         # Check its NID
5306         echo "Got NID '$nid'"
5307         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5308                 error "nid '$nid' does not match any client NID:" \
5309                       "${CLIENT_NIDS[@]}"
5310
5311         changelog_clear
5312         changelog_dump
5313         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5314         changelog_dump
5315
5316         changelog_find -type NOPEN -target-fid $fid &&
5317                 error "There should be no NOPEN entry"
5318
5319         # Sleep for `changelog_deniednext` seconds
5320         sleep 20
5321
5322         changelog_clear
5323         changelog_dump
5324         $RUNAS cat $f2 && error "cat $f by user $RUNAS_ID should have failed"
5325         changelog_dump
5326
5327         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5328                                -gid "$RUNAS_GID" -mode "r--") ||
5329                 error "No matching NOPEN entry"
5330
5331         # Parse the changelog
5332         eval local -A changelog=$(changelog2array $entry)
5333         local nid="${changelog[nid]}"
5334
5335         # Check the NID
5336         echo "Got NID '$nid'"
5337         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5338                 error "nid '$nid' does not match any client NID:" \
5339                       "${CLIENT_NIDS[@]}"
5340 }
5341 run_test 604 "NOPEN Changelog entry"
5342
5343 test_605() {
5344         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5345                 skip "need MDS version at least 2.10.58"
5346
5347         mkdir -p $DIR/$tdir
5348
5349         local f=$DIR/$tdir/$tfile
5350         local f2=$DIR2/$tdir/$tfile
5351
5352         changelog_register
5353         # set changelog_mask to ALL
5354         changelog_chmask "ALL"
5355
5356         touch $f || error "touch $f failed"
5357         local fid=$(path2fid $f)
5358
5359         changelog_clear
5360         changelog_dump
5361         exec 3<> $f || error "open $f failed"
5362         changelog_dump
5363
5364         local entry
5365         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5366
5367         changelog_clear
5368         changelog_dump
5369         exec 4<> $f || error "open $f failed"
5370         changelog_dump
5371
5372         changelog_find -type OPEN -target-fid $fid &&
5373                 error "There should be no OPEN entry"
5374
5375         exec 4>&- || error "close $f failed"
5376         changelog_dump
5377
5378         changelog_find -type CLOSE -target-fid $fid &&
5379                 error "There should be no CLOSE entry"
5380
5381         changelog_clear
5382         changelog_dump
5383         # access in rw, so different access mode should generate entries
5384         cat $f || error "cat $f failed"
5385         changelog_dump
5386
5387         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5388
5389         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5390
5391         changelog_clear
5392         changelog_dump
5393         # same access as first one, should not generate new entries
5394         exec 4<> $f || error "open $f failed"
5395         changelog_dump
5396
5397         changelog_find -type OPEN -target-fid $fid &&
5398                 error "There should be no OPEN entry"
5399
5400         exec 4>&- || error "close $f failed"
5401         changelog_dump
5402
5403         changelog_find -type CLOSE -target-fid $fid &&
5404                 error "There should be no CLOSE entry"
5405
5406         changelog_clear
5407         changelog_dump
5408         # access by different user should generate new entries
5409         $RUNAS cat $f || error "cat $f by user $RUNAS_ID failed"
5410         changelog_dump
5411
5412         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5413
5414         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5415
5416         changelog_clear
5417         changelog_dump
5418         exec 3>&- || error "close $f failed"
5419         changelog_dump
5420
5421         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5422 }
5423 run_test 605 "Test OPEN and CLOSE rate limit in Changelogs"
5424
5425 test_606() {
5426         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5427                 skip "need MDS version at least 2.10.58"
5428
5429         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5430         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5431         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5432                         skip_env "missing llog_reader" && return
5433         local fstype=$(facet_fstype mds1)
5434
5435         mkdir -p $DIR/$tdir
5436
5437         local f=$DIR/$tdir/$tfile
5438
5439         changelog_register
5440         # set changelog_mask to ALL
5441         changelog_chmask "ALL"
5442
5443         chmod 777 $DIR/$tdir
5444         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5445         local fid=$(path2fid $f)
5446         rm $f || error "rm $f failed"
5447
5448         local mntpt=$(facet_mntpt mds1)
5449         local pass=true
5450         local entry
5451
5452         #remount mds1 as ldiskfs or zfs type
5453         stack_trap "stop mds1; start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS" EXIT
5454         stop mds1 || error "stop mds1 failed"
5455         mount_fstype mds1 || error "remount mds1 failed"
5456
5457         for ((i = 0; i < 1; i++)); do
5458                 do_facet mds1 $llog_reader $mntpt/changelog_catalog
5459                 local cat_file=$(do_facet mds1 $llog_reader \
5460                                 $mntpt/changelog_catalog | awk \
5461                                 '{match($0,"path=([^ ]+)",a)}END{print a[1]}')
5462                 [ -n "$cat_file" ] || error "no catalog file"
5463
5464                 entry=$(do_facet mds1 $llog_reader $mntpt/$cat_file |
5465                         awk "/CREAT/ && /target:\[$fid\]/ {print}")
5466                 [ -n "$entry" ] || error "no CREAT entry"
5467         done
5468
5469         local uidgid=$(echo $entry |
5470                 sed 's+.*\ user:\([0-9][0-9]*:[0-9][0-9]*\)\ .*+\1+')
5471         [ -n "$uidgid" ] || error "uidgid is empty"
5472         echo "Got UID/GID $uidgid"
5473         [ "$uidgid" = "$RUNAS_ID:$RUNAS_GID" ] ||
5474                 error "uidgid '$uidgid' != '$RUNAS_ID:$RUNAS_GID'"
5475         local nid=$(echo $entry |
5476                 sed 's+.*\ nid:\(\S\S*@\S\S*\)\ .*+\1+')
5477         [ -n "$nid" ] || error "nid is empty"
5478         echo "Got NID $nid"
5479         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5480                 error "nid '$nid' does not match any NID ${CLIENT_NIDS[@]}"
5481 }
5482 run_test 606 "llog_reader groks changelog fields"
5483
5484 complete $SECONDS
5485 check_and_cleanup_lustre
5486 exit_status