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