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