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