Whamcloud - gitweb
LU-12610 tests: remove OBD_ -> CFS_ macros
[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 CFS_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 group_request_mask '$group_save'" EXIT
1879         other_save=$(get_hsm_param other_request_mask)
1880         stack_trap "set_hsm_param other_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_114() {
3913         mkdir_on_mdt0 $DIR/$tdir
3914
3915         local f1=$DIR/$tdir/${tfile}1
3916         local f2=$DIR/$tdir/${tfile}2
3917         local fid1=$(create_empty_file "$f1")
3918         local fid2=$(create_empty_file "$f2")
3919
3920         copytool setup
3921
3922         # Prevent archive from completing
3923         cdt_disable
3924
3925         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1 $f2
3926         # wait archive to register at CDT
3927         wait_request_state "$fid1" ARCHIVE WAITING
3928
3929         # Set f2 in an incompatible state
3930         $LFS hsm_set --noarchive $f2
3931
3932         cdt_enable
3933
3934         wait_request_state "$fid1" ARCHIVE SUCCEED
3935         wait_request_state "$fid2" ARCHIVE FAILED
3936 }
3937 run_test 114 "Incompatible request does not set other requests as STARTED"
3938
3939 test_200() {
3940         mkdir_on_mdt0 $DIR/$tdir
3941
3942         local f=$DIR/$tdir/$tfile
3943         local fid=$(create_empty_file "$f")
3944
3945         copytool setup
3946
3947         # Prevent archive from completing
3948         copytool_suspend
3949
3950         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3951         # wait archive to register at CDT
3952         wait_request_state $fid ARCHIVE STARTED
3953
3954         # Cancel the archive
3955         $LFS hsm_cancel "$f"
3956
3957         wait_request_state $fid ARCHIVE CANCELED
3958
3959         copytool_continue
3960         wait_request_state $fid CANCEL SUCCEED
3961 }
3962 run_test 200 "Register/Cancel archive"
3963
3964 test_201() {
3965         # test needs a running copytool
3966         copytool setup
3967         mkdir_on_mdt0 $DIR/$tdir
3968
3969         local f=$DIR/$tdir/$tfile
3970         create_archive_file $tdir/$tfile
3971         copytool import $tdir/$tfile $f
3972         local fid=$(path2fid $f)
3973
3974         # test with cdt on is made in test_222
3975         cdt_disable
3976         $LFS hsm_restore $f
3977         # wait restore to register at CDT
3978         wait_request_state $fid RESTORE WAITING
3979         $LFS hsm_cancel $f
3980         cdt_enable
3981         wait_request_state $fid RESTORE CANCELED
3982         wait_request_state $fid CANCEL SUCCEED
3983 }
3984 run_test 201 "Register/Cancel restore"
3985
3986 test_202() {
3987         mkdir_on_mdt0 $DIR/$tdir
3988
3989         local f=$DIR/$tdir/$tfile
3990         local fid=$(create_empty_file "$f")
3991
3992         # test needs a running copytool
3993         copytool setup
3994
3995         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3996         wait_request_state $fid ARCHIVE SUCCEED
3997
3998         copytool_suspend
3999         $LFS hsm_remove $f
4000         # wait remove to register at CDT
4001         wait_request_state $fid REMOVE STARTED
4002         $LFS hsm_cancel $f
4003
4004         wait_request_state $fid REMOVE CANCELED
4005 }
4006 run_test 202 "Register/Cancel remove"
4007
4008 test_220A() { # was test_220
4009         # test needs a running copytool
4010         copytool setup
4011
4012         mkdir_on_mdt0 $DIR/$tdir
4013
4014         local f=$DIR/$tdir/$tfile
4015         local fid=$(copy_file /etc/passwd $f)
4016
4017         changelog_register
4018
4019         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4020         wait_request_state $fid ARCHIVE SUCCEED
4021
4022         changelog_find -type HSM -target-fid $fid -flags 0x0 ||
4023                 error "The expected changelog was not emitted"
4024 }
4025 run_test 220A "Changelog for archive"
4026
4027 test_220a() {
4028         # test needs a running copytool
4029         copytool setup
4030
4031         mkdir_on_mdt0 $DIR/$tdir
4032
4033         local f=$DIR/$tdir/$tfile
4034         local fid=$(copy_file /etc/passwd $f)
4035
4036         changelog_register
4037
4038         # block copytool operations to allow for HSM request to be
4039         # submitted and file be unlinked (CDT will find object removed)
4040         copytool_suspend
4041
4042         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4043
4044         # wait request to reach CT
4045         wait_request_state $fid ARCHIVE STARTED
4046
4047         rm -f $f
4048
4049         copytool_continue
4050
4051         wait_request_state $fid ARCHIVE FAILED
4052
4053         # HE_ARCHIVE|ENOENT
4054         changelog_find -type HSM -target-fid $fid -flags 0x2 ||
4055                 error "The expected changelog was not emitted"
4056 }
4057 run_test 220a "Changelog for failed archive"
4058
4059 test_221() {
4060         mkdir_on_mdt0 $DIR/$tdir
4061
4062         local f=$DIR/$tdir/$tfile
4063         local fid=$(create_empty_file "$f")
4064
4065         copytool setup -b 1
4066         changelog_register
4067
4068         # Prevent archive from completing
4069         copytool_suspend
4070         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4071         wait_request_state $fid ARCHIVE STARTED
4072
4073         $LFS hsm_cancel $f
4074         wait_request_state $fid ARCHIVE CANCELED
4075
4076         copytool_continue
4077         wait_request_state $fid CANCEL SUCCEED
4078
4079         changelog_find -type HSM -target-fid $fid -flags 0x7d ||
4080                 error "The expected changelog was not emitted"
4081 }
4082 run_test 221 "Changelog for archive canceled"
4083
4084 test_222a() {
4085         # test needs a running copytool
4086         copytool setup
4087
4088         mkdir_on_mdt0 $DIR/$tdir
4089         copy2archive /etc/passwd $tdir/$tfile
4090
4091         local f=$DIR/$tdir/$tfile
4092         copytool import $tdir/$tfile $f
4093         local fid=$(path2fid $f)
4094
4095         changelog_register
4096
4097         $LFS hsm_restore $f
4098         wait_request_state $fid RESTORE SUCCEED
4099
4100         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4101                 error "The expected changelog was not emitted"
4102 }
4103 run_test 222a "Changelog for explicit restore"
4104
4105 test_222b() {
4106         # test needs a running copytool
4107         copytool setup
4108
4109         mkdir_on_mdt0 $DIR/$tdir
4110         local f=$DIR/$tdir/$tfile
4111         local fid=$(copy_file /etc/passwd $f)
4112
4113         changelog_register
4114         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4115         wait_request_state $fid ARCHIVE SUCCEED
4116         $LFS hsm_release $f
4117
4118         md5sum $f
4119
4120         wait_request_state $fid RESTORE SUCCEED
4121
4122         changelog_find -type HSM -target-fid $fid -flags 0x80 ||
4123                 error "The expected changelog was not emitted"
4124 }
4125 run_test 222b "Changelog for implicit restore"
4126
4127 test_222c() {
4128         # test needs a running copytool
4129         copytool setup
4130
4131         mkdir_on_mdt0 $DIR/$tdir
4132         copy2archive /etc/passwd $tdir/$tfile
4133
4134         local f=$DIR/$tdir/$tfile
4135         copytool import $tdir/$tfile $f
4136         local fid=$(path2fid $f)
4137
4138         changelog_register
4139
4140         # block copytool operations to allow for HSM request to be
4141         # submitted and file be unlinked (CDT will find object removed)
4142         copytool_suspend
4143
4144         $LFS hsm_restore $f
4145
4146         # wait request to reach CT
4147         wait_request_state $fid RESTORE STARTED
4148
4149         rm -f $f
4150
4151         copytool_continue
4152
4153         wait_request_state $fid RESTORE FAILED
4154
4155         # HE_RESTORE|ENOENT
4156         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
4157                 error "The expected changelog was not emitted"
4158 }
4159 run_test 222c "Changelog for failed explicit restore"
4160
4161 test_222d() {
4162         # test needs a running copytool
4163         copytool setup
4164
4165         mkdir_on_mdt0 $DIR/$tdir
4166         local f=$DIR/$tdir/$tfile
4167         local fid=$(copy_file /etc/passwd $f)
4168
4169         changelog_register
4170         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4171         wait_request_state $fid ARCHIVE SUCCEED
4172         $LFS hsm_release $f
4173
4174         copytool_remove_backend $fid
4175         md5sum $f
4176
4177         wait_request_state $fid RESTORE FAILED
4178
4179         # HE_RESTORE|ENOENT
4180         changelog_find -type HSM -target-fid $fid -flags 0x82 ||
4181                 error "The expected changelog was not emitted"
4182 }
4183 run_test 222d "Changelog for failed implicit restore"
4184
4185 test_223a() {
4186         # test needs a running copytool
4187         copytool setup -b 1
4188         mkdir_on_mdt0 $DIR/$tdir
4189
4190         local f=$DIR/$tdir/$tfile
4191         create_archive_file $tdir/$tfile
4192
4193         changelog_register
4194
4195         copytool import $tdir/$tfile $f
4196         local fid=$(path2fid $f)
4197
4198         $LFS hsm_restore $f
4199         wait_request_state $fid RESTORE STARTED
4200         $LFS hsm_cancel $f
4201         wait_request_state $fid RESTORE CANCELED
4202         wait_request_state $fid CANCEL SUCCEED
4203
4204         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4205                 error "The expected changelog was not emitted"
4206 }
4207 run_test 223a "Changelog for restore canceled (import case)"
4208
4209 test_223b() {
4210         mkdir_on_mdt0 $DIR/$tdir
4211
4212         local f=$DIR/$tdir/$tfile
4213         local fid=$(create_empty_file "$f")
4214
4215         copytool setup -b 1
4216         changelog_register
4217
4218         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4219         wait_request_state $fid ARCHIVE SUCCEED
4220         $LFS hsm_release $f
4221
4222         # Prevent restore from completing
4223         copytool_suspend
4224         $LFS hsm_restore $f
4225         wait_request_state $fid RESTORE STARTED
4226
4227         $LFS hsm_cancel $f
4228         wait_request_state $fid RESTORE CANCELED
4229
4230         copytool_continue
4231         wait_request_state $fid CANCEL SUCCEED
4232
4233         changelog_find -type HSM -target-fid $fid -flags 0xfd ||
4234                 error "The expected changelog was not emitted"
4235 }
4236 run_test 223b "Changelog for restore canceled (release case)"
4237
4238 test_224A() { # was test_224
4239         # test needs a running copytool
4240         copytool setup
4241
4242         mkdir_on_mdt0 $DIR/$tdir
4243
4244         local f=$DIR/$tdir/$tfile
4245         local fid=$(copy_file /etc/passwd $f)
4246
4247         changelog_register
4248         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4249         wait_request_state $fid ARCHIVE SUCCEED
4250
4251         $LFS hsm_remove $f
4252         wait_request_state $fid REMOVE SUCCEED
4253
4254         changelog_find -type HSM -target-fid $fid -flags 0x200 ||
4255                 error "The expected changelog was not emitted"
4256 }
4257 run_test 224A "Changelog for remove"
4258
4259 test_224a() {
4260         # test needs a running copytool
4261         copytool setup
4262
4263         mkdir_on_mdt0 $DIR/$tdir
4264
4265         local f=$DIR/$tdir/$tfile
4266         local fid=$(copy_file /etc/passwd $f)
4267
4268         changelog_register
4269         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4270         wait_request_state $fid ARCHIVE SUCCEED
4271
4272         copytool_remove_backend $fid
4273
4274         # block copytool operations to allow for HSM request to be
4275         # submitted and file be unlinked (CDT will find object removed)
4276         copytool_suspend
4277
4278         $LFS hsm_remove $f
4279
4280         # wait for request to reach CT
4281         wait_request_state $fid REMOVE STARTED
4282
4283         rm -f $f
4284
4285         copytool_continue
4286
4287         wait_request_state $fid REMOVE FAILED
4288
4289         # HE_REMOVE|ENOENT=0x202
4290         changelog_find -type HSM -target-fid $fid -flags 0x202 ||
4291                 error "The expected changelog was not emitted"
4292 }
4293 run_test 224a "Changelog for failed remove"
4294
4295 test_225() {
4296         # test is not usable because remove request is too fast
4297         # so it is always finished before cancel can be done ...
4298         echo "Test disabled"
4299         return 0
4300
4301         # test needs a running copytool
4302         copytool setup
4303         mkdir_on_mdt0 $DIR/$tdir
4304
4305         local f=$DIR/$tdir/$tfile
4306         local fid=$(create_empty_file "$f")
4307
4308         changelog_register
4309         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4310         wait_request_state $fid ARCHIVE SUCCEED
4311
4312         # Prevent restore from completing
4313         copytool_suspend
4314         $LFS hsm_remove $f
4315
4316         $LFS hsm_cancel $f
4317         wait_request_state $fid REMOVE CANCELED
4318
4319         copytool_continue
4320         wait_request_state $fid CANCEL SUCCEED
4321
4322         changelog_find -type HSM -target-fid $fid -flags 0x27d
4323                 error "The expected changelog was not emitted"
4324 }
4325 run_test 225 "Changelog for remove canceled"
4326
4327 test_226() {
4328         # test needs a running copytool
4329         copytool setup
4330
4331         mkdir_on_mdt0 $DIR/$tdir
4332
4333         local f1=$DIR/$tdir/$tfile-1
4334         local f2=$DIR/$tdir/$tfile-2
4335         local f3=$DIR/$tdir/$tfile-3
4336         local fid1=$(copy_file /etc/passwd $f1)
4337         local fid2=$(copy_file /etc/passwd $f2)
4338         copy_file /etc/passwd $f3
4339
4340         changelog_register
4341         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
4342         wait_request_state $fid1 ARCHIVE SUCCEED
4343
4344         $LFS hsm_archive $f2
4345         wait_request_state $fid2 ARCHIVE SUCCEED
4346
4347         rm $f1 || error "rm $f1 failed"
4348
4349         changelog_dump
4350         changelog_find -type UNLNK -target-fid $fid1 -flags 0x3 ||
4351                 error "The expected changelog was not emitted"
4352
4353         mv $f3 $f2 || error "mv $f3 $f2 failed"
4354
4355         changelog_find -type RENME -target-fid $fid2 -flags 0x3 ||
4356                 error "The expected changelog was not emitted"
4357 }
4358 run_test 226 "changelog for last rm/mv with exiting archive"
4359
4360 # This is just a utility function to clarify what test_227 does
4361 __test_227()
4362 {
4363         local target=0x280
4364
4365         "$LFS" "$action" --$flag "$file" ||
4366                 error "Cannot ${action#hsm_} $flag on '$file'"
4367
4368         # Only one changelog should be produced
4369         local entries="$(changelog_find -type HSM -target-fid $fid)"
4370         [ $(wc -l <<< "$entries") -eq $((++count)) ] ||
4371                 error "lfs $action --$flag '$file' produced more than one" \
4372                       "changelog record"
4373
4374         # Parse the last changelog record
4375         local entry="$(tail -n 1 <<< "$entries")"
4376         eval local -A changelog=$(changelog2array $entry)
4377
4378         # Also check the flags match what is expected
4379         [[ ${changelog[flags]} == $target ]] ||
4380                 error "Changelog flag is '${changelog[flags]}', not $target"
4381 }
4382
4383 test_227() {
4384         local file="$DIR/$tdir/$tfile"
4385         local fid=$(create_empty_file "$file")
4386         local count=0
4387
4388         changelog_register
4389
4390         for flag in norelease noarchive exists archived lost; do
4391                 if [ "$flag" == lost ]; then
4392                         # The flag "lost" only works on an archived file
4393                         "$LFS" hsm_set --archived "$file"
4394                         ((count++))
4395                 fi
4396
4397                 action="hsm_set" __test_227
4398                 action="hsm_clear" __test_227
4399         done
4400 }
4401 run_test 227 "changelog when explicit setting of HSM flags"
4402
4403 test_228() {
4404         # test needs a running copytool
4405         copytool setup
4406         mkdir_on_mdt0 $DIR/$tdir
4407
4408         local fid=$(create_small_sync_file $DIR/$tfile)
4409         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
4410         wait_request_state $fid ARCHIVE SUCCEED
4411
4412         $LFS hsm_release $DIR/$tfile
4413         check_hsm_flags $DIR/$tfile "0x0000000d"
4414
4415         filefrag $DIR/$tfile | grep " 1 extent found" ||
4416                 error "filefrag on released file must return only one extent"
4417
4418         # only newer versions of cp detect sparse files by stat/FIEMAP
4419         # (LU-2580)
4420         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
4421                 error "copying $DIR/$tfile"
4422         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
4423
4424         $LFS hsm_release $DIR/$tfile
4425         check_hsm_flags $DIR/$tfile "0x0000000d"
4426
4427         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
4428
4429         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
4430                 error "tar failed"
4431         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
4432                 error "comparing untarred $DIR/$tfile"
4433
4434         rm -f $DIR/$tfile $DIR/$tfile.2 ||
4435                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
4436 }
4437 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
4438
4439 test_250() {
4440         local file="$DIR/$tdir/$tfile"
4441
4442         # set max_requests to allow one request of each type to be started (3)
4443         stack_trap \
4444                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4445         set_hsm_param max_requests 3
4446         # speed up test
4447         stack_trap \
4448                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4449         set_hsm_param loop_period 1
4450
4451         # send 1 requests of each kind twice
4452         copytool setup
4453
4454         mkdir_on_mdt0 $DIR/$tdir
4455
4456         # setup the files
4457         for action in archive restore remove; do
4458                 local filepath="$file"-to-$action
4459                 local fid=$(create_empty_file "$filepath")
4460                 local fid2=$(create_empty_file "$filepath".bis)
4461
4462                 if [ "$action" != archive ]; then
4463                         "$LFS" hsm_archive "$filepath"
4464                         wait_request_state $fid ARCHIVE SUCCEED
4465                         "$LFS" hsm_archive "$filepath".bis
4466                         wait_request_state $fid2 ARCHIVE SUCCEED
4467                 fi
4468                 if [ "$action" == restore ]; then
4469                         "$LFS" hsm_release "$filepath"
4470                         "$LFS" hsm_release "$filepath".bis
4471                 fi
4472         done
4473
4474         # suspend the copytool to prevent requests from completing
4475         stack_trap "copytool_continue" EXIT
4476         copytool_suspend
4477
4478         # send `max_requests' requests (one of each kind)
4479         for action in archive restore remove; do
4480                 filepath="$file"-to-$action
4481                 "$LFS" hsm_${action} "$filepath"
4482                 wait_request_state $(path2fid "$filepath") "${action^^}" STARTED
4483         done
4484
4485         # send another batch of requests
4486         for action in archive restore remove; do
4487                 "$LFS" hsm_${action} "$file-to-$action".bis
4488         done
4489         # wait for `loop_period' seconds to make sure the coordinator has time
4490         # to register those, even though it should not
4491         sleep 1
4492
4493         # only the first batch of request should be started
4494         local -i count
4495         count=$(do_facet $SINGLEMDS "$LCTL" get_param -n $HSM_PARAM.actions |
4496                 grep -c STARTED)
4497
4498         ((count == 3)) ||
4499                 error "expected 3 STARTED requests, found $count"
4500 }
4501 run_test 250 "Coordinator max request"
4502
4503 test_251() {
4504         mkdir_on_mdt0 $DIR/$tdir
4505
4506         local f=$DIR/$tdir/$tfile
4507         local fid=$(create_empty_file "$f")
4508
4509         cdt_disable
4510         # to have a short test
4511         local old_to=$(get_hsm_param active_request_timeout)
4512         set_hsm_param active_request_timeout 1
4513         # to be sure the cdt will wake up frequently so
4514         # it will be able to cancel the "old" request
4515         local old_loop=$(get_hsm_param loop_period)
4516         set_hsm_param loop_period 1
4517         cdt_enable
4518
4519         copytool setup
4520
4521         # Prevent archive from completing
4522         copytool_suspend
4523         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4524         wait_request_state $fid ARCHIVE STARTED
4525
4526         # Let the request timeout
4527         wait_request_state $fid ARCHIVE CANCELED
4528
4529         set_hsm_param active_request_timeout $old_to
4530         set_hsm_param loop_period $old_loop
4531 }
4532 run_test 251 "Coordinator request timeout"
4533
4534 test_252() {
4535         mkdir_on_mdt0 $DIR/$tdir
4536
4537         local f=$DIR/$tdir/$tfile
4538         local fid=$(create_empty_file "$f")
4539
4540         # to have a short test
4541         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4542         set_hsm_param loop_period 1
4543
4544         copytool setup
4545
4546         # Prevent archive from completing
4547         copytool_suspend
4548         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
4549         wait_request_state $fid ARCHIVE STARTED
4550         rm -f "$f"
4551
4552         stack_trap "set_hsm_param active_request_timeout \
4553                     $(get_hsm_param active_request_timeout)" EXIT
4554         set_hsm_param active_request_timeout 1
4555
4556         wait_request_state $fid ARCHIVE CANCELED
4557         copytool_continue
4558 }
4559 run_test 252 "Timeout'ed running archive of a removed file should be canceled"
4560
4561 test_253() {
4562         local rc
4563         # test needs a running copytool
4564         copytool setup
4565
4566         mkdir_on_mdt0 $DIR/$tdir
4567         local f=$DIR/$tdir/$tfile
4568
4569         dd if=/dev/zero of=$f bs=1MB count=10
4570         local fid=$(path2fid $f)
4571
4572         $LFS hsm_archive $f || error "could not archive file"
4573         wait_request_state $fid ARCHIVE SUCCEED
4574
4575         # clear locks to discard inode data
4576         cancel_lru_locks osc
4577
4578         #define OBD_FAIL_MDC_MERGE              0x807
4579         $LCTL set_param fail_loc=0x807
4580
4581         #expect error here, instead of release with wrong size
4582         $LFS hsm_release $f
4583         rc=$?
4584         if ((rc == 0)); then
4585                 file_size=$(stat -c '%s' $f)
4586                 if ((file_size != 10485760)); then
4587                         error "Wrong file size after hsm_release"
4588                 fi
4589         else
4590                 echo "could not release file"
4591         fi
4592 }
4593 run_test 253 "Check for wrong file size after release"
4594
4595 test_254a()
4596 {
4597         [ $MDS1_VERSION -lt $(version_code 2.10.56) ] &&
4598                 skip "need MDS version at least 2.10.56"
4599
4600         # Check that the counters are initialized to 0
4601         local count
4602         for request_type in archive restore remove; do
4603                 count="$(get_hsm_param ${request_type}_count)" ||
4604                         error "Reading ${request_type}_count failed with $?"
4605
4606                 [ "$count" -eq 0 ] ||
4607                         error "Expected ${request_type}_count to be " \
4608                               "0 != '$count'"
4609         done
4610 }
4611 run_test 254a "Request counters are initialized to zero"
4612
4613 test_254b()
4614 {
4615         [ $MDS1_VERSION -lt $(version_code 2.10.56) ] &&
4616                 skip "need MDS version at least 2.10.56"
4617
4618         # The number of request to launch (at least 32)
4619         local request_count=$((RANDOM % 32 + 32))
4620         printf "Will launch %i requests of each type\n" "$request_count"
4621
4622         # Launch a copytool to process requests
4623         copytool setup
4624
4625         # Set hsm.max_requests to allow starting all requests at the same time
4626         stack_trap \
4627                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4628         set_hsm_param max_requests "$request_count"
4629
4630         mkdir_on_mdt0 $DIR/$tdir
4631
4632         local timeout
4633         local count
4634         for request_type in archive restore remove; do
4635                 printf "Checking %s requests\n" "${request_type}"
4636                 # Suspend the copytool to give us time to read the proc files
4637                 copytool_suspend
4638
4639                 for ((i = 0; i < $request_count; i++)); do
4640                         case $request_type in
4641                         archive)
4642                                 create_empty_file "$DIR/$tdir/$tfile-$i" \
4643                                         >/dev/null 2>&1
4644                                 ;;
4645                         restore)
4646                                 lfs hsm_release "$DIR/$tdir/$tfile-$i"
4647                                 ;;
4648                         esac
4649                         $LFS hsm_${request_type} "$DIR/$tdir/$tfile-$i"
4650                 done
4651
4652                 # Give the coordinator 10 seconds to start every request
4653                 timeout=10
4654                 while get_hsm_param actions | grep -q WAITING; do
4655                         sleep 1
4656                         let timeout-=1
4657                         [ $timeout -gt 0 ] ||
4658                                 error "${request_type^} requests took too " \
4659                                       "long to start"
4660                 done
4661
4662                 count="$(get_hsm_param ${request_type}_count)"
4663                 [ "$count" -eq "$request_count" ] ||
4664                         error "Expected '$request_count' (!= '$count') " \
4665                               "active $request_type requests"
4666
4667                 # Let the copytool process the requests
4668                 copytool_continue
4669                 # Give it 10 seconds maximum
4670                 timeout=10
4671                 while get_hsm_param actions | grep -q STARTED; do
4672                         sleep 1
4673                         let timeout-=1
4674                         [ $timeout -gt 0 ] ||
4675                                 error "${request_type^} requests took too " \
4676                                       "long to complete"
4677                 done
4678
4679                 count="$(get_hsm_param ${request_type}_count)"
4680                 [ "$count" -eq 0 ] ||
4681                         error "Expected 0 (!= '$count') " \
4682                               "active $request_type requests"
4683         done
4684 }
4685 run_test 254b "Request counters are correctly incremented and decremented"
4686
4687 test_255()
4688 {
4689         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4690                 skip "Need MDS version at least 2.12.0"
4691
4692         mkdir_on_mdt0 $DIR/$tdir
4693
4694         local file="$DIR/$tdir/$tfile"
4695         local fid=$(create_empty_file "$file")
4696
4697         # How do you make sure the coordinator has consumed any outstanding
4698         # event, without triggering an event yourself?
4699         #
4700         # You wait for a request to disappear from the coordinator's llog.
4701
4702         # Warning: the setup represents 90% of this test
4703
4704         # Create and process an HSM request
4705         copytool setup
4706         "$LFS" hsm_archive "$file"
4707         wait_request_state $fid ARCHIVE SUCCEED
4708
4709         kill_copytools
4710         wait_copytools || error "failed to stop copytools"
4711
4712         # Launch a new HSM request
4713         rm "$file"
4714         create_empty_file "$file"
4715         "$LFS" hsm_archive "$file"
4716
4717         cdt_shutdown
4718
4719         # Have the completed request be removed as soon as the cdt wakes up
4720         stack_trap "set_hsm_param grace_delay $(get_hsm_param grace_delay)" EXIT
4721         set_hsm_param grace_delay 1
4722         # (Hopefully, time on the MDS will behave nicely)
4723         do_facet $SINGLEMDS sleep 2 &
4724
4725         # Increase `loop_period' as a mean to prevent the coordinator from
4726         # waking itself up to do some housekeeping.
4727         stack_trap "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4728         set_hsm_param loop_period 1000
4729
4730         wait $! || error "waiting failed"
4731         cdt_enable
4732         wait_request_state $fid ARCHIVE ""
4733         # The coordinator will not wake up on its own for ~`loop_period' secs...
4734
4735         # ... Unless a copytool registers. Now the real test begins
4736         copytool setup
4737         wait_request_state $(path2fid "$file") ARCHIVE SUCCEED
4738 }
4739 run_test 255 "Copytool registration wakes the coordinator up"
4740
4741 # tests 260[a-c] rely on the parsing of the copytool's log file, they might
4742 # break in the future because of that.
4743 test_260a()
4744 {
4745         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
4746                 skip "need MDS version 2.11.56 or later"
4747
4748         local -a files=("$DIR/$tdir/$tfile".{0..15})
4749         local file
4750
4751         mkdir_on_mdt0 $DIR/$tdir
4752
4753         for file in "${files[@]}"; do
4754                 create_small_file "$file"
4755         done
4756
4757         # Set a few hsm parameters
4758         stack_trap \
4759                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4760         set_hsm_param loop_period 1
4761         stack_trap \
4762                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4763         set_hsm_param max_requests 3
4764
4765         # Release one file
4766         copytool setup
4767         "$LFS" hsm_archive "${files[0]}"
4768         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4769         "$LFS" hsm_release "${files[0]}"
4770
4771         # Stop the copytool
4772         kill_copytools
4773         wait_copytools || error "copytools failed to stop"
4774
4775         # Send several archive requests
4776         for file in "${files[@]:1}"; do
4777                 "$LFS" hsm_archive "$file"
4778         done
4779
4780         # Send one restore request
4781         "$LFS" hsm_restore "${files[0]}"
4782
4783         # Launch a copytool
4784         copytool setup
4785
4786         # Wait for all the requests to complete
4787         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4788         for file in "${files[@]:1}"; do
4789                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4790         done
4791
4792         # Collect the actions in the order in which the copytool processed them
4793         local -a actions=(
4794                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4795                         "$(copytool_logfile "$SINGLEAGT")")
4796                 )
4797
4798         printf '%s\n' "${actions[@]}"
4799
4800         local action
4801         for action in "${actions[@]:0:3}"; do
4802                 [ "$action" == RESTORE ] && return
4803         done
4804
4805         error "Too many ARCHIVE requests were run before the RESTORE request"
4806 }
4807 run_test 260a "Restore request have priority over other requests"
4808
4809 # This test is very much tied to the implementation of the current priorisation
4810 # mechanism in the coordinator. It might not make sense to keep it in the future
4811 test_260b()
4812 {
4813         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
4814                 skip "need MDS version 2.11.56 or later"
4815
4816         local -a files=("$DIR/$tdir/$tfile".{0..15})
4817         local file
4818
4819         mkdir_on_mdt0 $DIR/$tdir
4820
4821         for file in "${files[@]}"; do
4822                 create_small_file "$file"
4823         done
4824
4825         # Set a few hsm parameters
4826         stack_trap \
4827                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4828         set_hsm_param loop_period 1
4829         stack_trap \
4830                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4831         set_hsm_param max_requests 3
4832
4833         # Release one file
4834         copytool setup --archive-id 2
4835         "$LFS" hsm_archive --archive 2 "${files[0]}"
4836         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4837         "$LFS" hsm_release "${files[0]}"
4838
4839         # Stop the copytool
4840         kill_copytools
4841         wait_copytools || error "copytools failed to stop"
4842
4843         # Send several archive requests
4844         for file in "${files[@]:1}"; do
4845                 "$LFS" hsm_archive "$file"
4846         done
4847
4848         # Send one restore request
4849         "$LFS" hsm_restore "${files[0]}"
4850
4851         # Launch a copytool
4852         copytool setup
4853         copytool setup --archive-id 2
4854
4855         # Wait for all the requests to complete
4856         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4857         for file in "${files[@]:1}"; do
4858                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4859         done
4860
4861         # Collect the actions in the order in which the copytool processed them
4862         local -a actions=(
4863                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4864                         "$(copytool_logfile "$SINGLEAGT")")
4865                 )
4866
4867         printf '%s\n' "${actions[@]}"
4868
4869         local action
4870         for action in "${actions[@]:0:3}"; do
4871                 [ "$action" == RESTORE ] && return
4872         done
4873
4874         error "Too many ARCHIVE requests were run before the RESTORE request"
4875 }
4876 run_test 260b "Restore request have priority over other requests"
4877
4878 # This test is very much tied to the implementation of the current priorisation
4879 # mechanism in the coordinator. It might not make sense to keep it in the future
4880 test_260c()
4881 {
4882         [ $MDS1_VERSION -lt $(version_code 2.12.0) ] &&
4883                 skip "Need MDS version at least 2.12.0"
4884
4885         local -a files=("$DIR/$tdir/$tfile".{0..15})
4886         local file
4887
4888         mkdir_on_mdt0 $DIR/$tdir
4889
4890         for file in "${files[@]}"; do
4891                 create_small_file "$file"
4892         done
4893
4894         # Set a few hsm parameters
4895         stack_trap \
4896                 "set_hsm_param loop_period $(get_hsm_param loop_period)" EXIT
4897         set_hsm_param loop_period 1000
4898         stack_trap \
4899                 "set_hsm_param max_requests $(get_hsm_param max_requests)" EXIT
4900         set_hsm_param max_requests 3
4901
4902         # Release one file
4903         copytool setup --archive-id 2
4904         "$LFS" hsm_archive --archive 2 "${files[0]}"
4905         wait_request_state "$(path2fid "${files[0]}")" ARCHIVE SUCCEED
4906         "$LFS" hsm_release "${files[0]}"
4907
4908         # Stop the copytool
4909         kill_copytools
4910         wait_copytools || error "copytools failed to stop"
4911
4912         # Force the next coordinator run to do housekeeping
4913         cdt_shutdown
4914         cdt_enable
4915
4916         "$LFS" hsm_archive "${files[1]}"
4917
4918         # Launch a copytool
4919         copytool setup
4920         copytool setup --archive-id 2
4921
4922         wait_request_state "$(path2fid "${files[1]}")" ARCHIVE SUCCEED
4923         # The coordinator just did a housekeeping run it won't do another one
4924         # for around `loop_period' seconds => requests will not be reordered
4925         # if it costs too much (ie. when the coordinator has to discard a whole
4926         # hal)
4927
4928         # Send several archive requests
4929         for file in "${files[@]:2}"; do
4930                 "$LFS" hsm_archive "$file"
4931         done
4932
4933         # Send one restore request
4934         "$LFS" hsm_restore "${files[0]}"
4935
4936         # Wait for all the requests to complete
4937         wait_request_state "$(path2fid "${files[0]}")" RESTORE SUCCEED
4938         for file in "${files[@]:2}"; do
4939                 wait_request_state "$(path2fid "$file")" ARCHIVE SUCCEED
4940         done
4941
4942         # Collect the actions in the order in which the copytool processed them
4943         local -a actions=(
4944                 $(do_facet "$SINGLEAGT" grep -o '\"RESTORE\\|ARCHIVE\"' \
4945                         "$(copytool_logfile "$SINGLEAGT")")
4946                 )
4947
4948         printf '%s\n' "${actions[@]}"
4949
4950         local action
4951         for action in "${actions[@]:0:3}"; do
4952                 [ "$action" == RESTORE ] &&
4953                         error "Restore requests should not be prioritised" \
4954                               "unless the coordinator is doing housekeeping"
4955         done
4956         return 0
4957 }
4958 run_test 260c "Requests are not reordered on the 'hot' path of the coordinator"
4959
4960 test_261() {
4961         local file=$DIR/$tdir/$tfile
4962         local size
4963         local fid
4964
4965         copytool setup
4966         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $DIR/$tdir failed"
4967
4968         dd if=/dev/zero of=$file bs=4k count=2 || error "Write $file failed"
4969         fid=$(path2fid $file)
4970         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $file
4971         wait_request_state $fid ARCHIVE SUCCEED
4972
4973         $LFS hsm_state $file
4974         $LFS hsm_release $file
4975         $LFS hsm_restore $file
4976         wait_request_state $fid RESTORE SUCCEED
4977         $LFS hsm_release $file
4978         size=$(stat -c %s $file)
4979         [[ $size == 8192 ]] || error "Size after HSM release: $size"
4980
4981         $LFS hsm_release $file
4982         $LFS hsm_restore $file
4983         $LFS hsm_release $file
4984         size=$(stat -c %s $file)
4985         [[ $size == 8192 ]] || error "Size after HSM release: $size"
4986         $LFS hsm_state $file
4987 }
4988 run_test 261 "Report 0 bytes size after HSM release"
4989
4990 test_300() {
4991         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
4992
4993         # the only way to test ondisk conf is to restart MDS ...
4994         echo "Stop coordinator and remove coordinator state at mount"
4995         # stop coordinator
4996         cdt_shutdown
4997         # clean on disk conf set by default
4998         cdt_clear_mount_state
4999         cdt_check_state stopped
5000
5001         # check cdt still off after umount/remount
5002         fail $SINGLEMDS
5003         cdt_check_state stopped
5004
5005         echo "Set coordinator start at mount, and start coordinator"
5006         cdt_set_mount_state enabled
5007
5008         # check cdt is on
5009         cdt_check_state enabled
5010
5011         # check cdt still on after umount/remount
5012         fail $SINGLEMDS
5013         cdt_check_state enabled
5014
5015         # we are back to original state (cdt started at mount)
5016 }
5017 run_test 300 "On disk coordinator state kept between MDT umount/mount"
5018
5019 test_301() {
5020         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
5021
5022         local ai=$(get_hsm_param default_archive_id)
5023         local new=$((ai + 1))
5024
5025         set_hsm_param default_archive_id $new -P
5026         fail $SINGLEMDS
5027         local res=$(get_hsm_param default_archive_id)
5028
5029         # clear value
5030         set_hsm_param default_archive_id "" "-P -d"
5031
5032         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
5033 }
5034 run_test 301 "HSM tunnable are persistent"
5035
5036 test_302() {
5037         [ "$CLIENTONLY" ] && skip "CLIENTONLY mode" && return
5038
5039         local ai=$(get_hsm_param default_archive_id)
5040         local new=$((ai + 1))
5041
5042         # stop coordinator
5043         cdt_shutdown
5044
5045         set_hsm_param default_archive_id $new -P
5046
5047         local mdtno
5048         for mdtno in $(seq 1 $MDSCOUNT); do
5049                 fail mds${mdtno}
5050         done
5051
5052         # check cdt is on
5053         cdt_check_state enabled
5054
5055         local res=$(get_hsm_param default_archive_id)
5056
5057         # clear value
5058         set_hsm_param default_archive_id "" "-P -d"
5059
5060         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
5061 }
5062 run_test 302 "HSM tunnable are persistent when CDT is off"
5063
5064 test_400() {
5065         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5066
5067         copytool setup
5068
5069         mkdir_on_mdt0 $DIR/$tdir
5070
5071         local dir_mdt0=$DIR/$tdir/mdt0
5072         local dir_mdt1=$DIR/$tdir/mdt1
5073
5074         # create 1 dir per MDT
5075         stack_trap "rm -rf $dir_mdt0" EXIT
5076         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5077         stack_trap "rm -rf $dir_mdt1" EXIT
5078         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5079
5080         # create 1 file in each MDT
5081         local fid1=$(create_small_file $dir_mdt0/$tfile)
5082         local fid2=$(create_small_file $dir_mdt1/$tfile)
5083
5084         # check that hsm request on mdt0 is sent to the right MDS
5085         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5086         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5087                 echo "archive successful on mdt0"
5088
5089         # check that hsm request on mdt1 is sent to the right MDS
5090         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
5091         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5092                 echo "archive successful on mdt1"
5093 }
5094 run_test 400 "Single request is sent to the right MDT"
5095
5096 test_401() {
5097         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5098
5099         copytool setup
5100
5101         mkdir_on_mdt0 $DIR/$tdir
5102
5103         local dir_mdt0=$DIR/$tdir/mdt0
5104         local dir_mdt1=$DIR/$tdir/mdt1
5105
5106         # create 1 dir per MDT
5107         stack_trap "rm -rf $dir_mdt0" EXIT
5108         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5109         stack_trap "rm -rf $dir_mdt1" EXIT
5110         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
5111
5112         # create 1 file in each MDT
5113         local fid1=$(create_small_file $dir_mdt0/$tfile)
5114         local fid2=$(create_small_file $dir_mdt1/$tfile)
5115
5116         # check that compound requests are shunt to the rights MDTs
5117         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
5118                 error "lfs hsm_archive"
5119         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5120                 echo "archive successful on mdt0"
5121         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
5122                 echo "archive successful on mdt1"
5123 }
5124 run_test 401 "Compound requests split and sent to their respective MDTs"
5125
5126 mdc_change_state() # facet, MDT_pattern, activate|deactivate
5127 {
5128         local facet=$1
5129         local pattern="$2"
5130         local state=$3
5131         local node=$(facet_active_host $facet)
5132         local mdc
5133         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
5134                         awk '{print $4}'); do
5135                 echo "$3 $mdc on $node"
5136                 do_facet $facet "$LCTL --device $mdc $state" || return 1
5137         done
5138 }
5139
5140 test_402a() {
5141         # deactivate all mdc on agent1
5142         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "deactivate"
5143
5144         copytool setup --no-fail
5145
5146         check_agent_unregistered "uuid" # match any agent
5147
5148         # no expected running copytool
5149         search_copytools $agent && error "Copytool start should have failed"
5150
5151         # reactivate MDCs
5152         mdc_change_state $SINGLEAGT "$FSNAME-MDT000." "activate"
5153 }
5154 run_test 402a "Copytool start fails if all MDTs are inactive"
5155
5156 test_402b() {
5157         copytool setup
5158
5159         mkdir_on_mdt0 $DIR/$tdir
5160
5161         local f=$DIR/$tdir/$tfile
5162         touch $f || error "touch $f failed"
5163         local fid=$(path2fid $f)
5164
5165 #define OBD_FAIL_MDS_HSM_CT_REGISTER_NET        0x14d
5166         do_facet $SINGLEAGT lctl set_param fail_loc=0x14d
5167         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5168
5169         # give time for CDT to send request and to keep it for retry
5170         wait_for_loop_period
5171
5172         wait_request_state $fid ARCHIVE WAITING
5173
5174         do_facet $SINGLEAGT lctl set_param fail_loc=0
5175
5176         # request should succeed now
5177         wait_request_state $fid ARCHIVE SUCCEED
5178 }
5179 run_test 402b "CDT must retry request upon slow start of CT"
5180
5181 test_403() {
5182         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5183
5184         local agent=$(facet_active_host $SINGLEAGT)
5185
5186         # deactivate all mdc for MDT0001
5187         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5188
5189         copytool setup
5190         local uuid=$(get_agent_uuid $agent)
5191         # check the agent is registered on MDT0000, and not on MDT0001
5192         check_agent_registered_by_mdt $uuid 0
5193         check_agent_unregistered_by_mdt $uuid 1
5194
5195         # check running copytool process
5196         search_copytools $agent || error "No running copytools on $agent"
5197
5198         # reactivate all mdc for MDT0001
5199         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5200
5201         # make sure the copytool is now registered to all MDTs
5202         check_agent_registered $uuid
5203 }
5204 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
5205
5206 test_404() {
5207         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5208
5209         copytool setup
5210
5211         # create files on both MDT0000 and MDT0001
5212         mkdir_on_mdt0 $DIR/$tdir
5213
5214         local dir_mdt0=$DIR/$tdir/mdt0
5215         stack_trap "rm -rf $dir_mdt0" EXIT
5216         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
5217
5218         # create 1 file on mdt0
5219         local fid1=$(create_small_file $dir_mdt0/$tfile)
5220
5221         # deactivate all mdc for MDT0001
5222         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "deactivate"
5223
5224         # send an HSM request for files in MDT0000
5225         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
5226
5227         # check for completion of files in MDT0000
5228         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
5229                 echo "archive successful on mdt0"
5230
5231         # reactivate all mdc for MDT0001
5232         mdc_change_state $SINGLEAGT "$FSNAME-MDT0001" "activate"
5233 }
5234 run_test 404 "Inactive MDT does not block requests for active MDTs"
5235
5236 test_405() {
5237         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5238
5239         copytool setup
5240
5241         mkdir_on_mdt0 $DIR/$tdir
5242
5243         local striped_dir=$DIR/$tdir/striped_dir
5244
5245         # create striped dir on all of MDTs
5246         $LFS mkdir -i 0 -c $MDSCOUNT $striped_dir || error "lfs mkdir"
5247
5248         local fid1=$(create_small_sync_file $striped_dir/${tfile}_0)
5249         local fid2=$(create_small_sync_file $striped_dir/${tfile}_1)
5250         local fid3=$(create_small_sync_file $striped_dir/${tfile}_2)
5251         local fid4=$(create_small_sync_file $striped_dir/${tfile}_3)
5252
5253         local idx1=$($LFS getstripe -m $striped_dir/${tfile}_0)
5254         local idx2=$($LFS getstripe -m $striped_dir/${tfile}_1)
5255         local idx3=$($LFS getstripe -m $striped_dir/${tfile}_2)
5256         local idx4=$($LFS getstripe -m $striped_dir/${tfile}_3)
5257
5258         # check that compound requests are shunt to the rights MDTs
5259         $LFS hsm_archive $striped_dir/${tfile}_0 $striped_dir/${tfile}_1  \
5260                          $striped_dir/${tfile}_2 $striped_dir/${tfile}_3 ||
5261                 error "lfs hsm_archive"
5262
5263         wait_request_state $fid1 ARCHIVE SUCCEED $idx1 &&
5264                 echo "archive successful on $fid1"
5265         wait_request_state $fid2 ARCHIVE SUCCEED $idx2 &&
5266                 echo "archive successful on $fid2"
5267         wait_request_state $fid3 ARCHIVE SUCCEED $idx3 &&
5268                 echo "archive successful on $fid3"
5269         wait_request_state $fid4 ARCHIVE SUCCEED $idx4 &&
5270                 echo "archive successful on $fid4"
5271
5272         $LFS hsm_release $striped_dir/${tfile}_0 || error "lfs hsm_release 1"
5273         $LFS hsm_release $striped_dir/${tfile}_1 || error "lfs hsm_release 2"
5274         $LFS hsm_release $striped_dir/${tfile}_2 || error "lfs hsm_release 3"
5275         $LFS hsm_release $striped_dir/${tfile}_3 || error "lfs hsm_release 4"
5276
5277         cat $striped_dir/${tfile}_0 > /dev/null || error "cat ${tfile}_0 failed"
5278         cat $striped_dir/${tfile}_1 > /dev/null || error "cat ${tfile}_1 failed"
5279         cat $striped_dir/${tfile}_2 > /dev/null || error "cat ${tfile}_2 failed"
5280         cat $striped_dir/${tfile}_3 > /dev/null || error "cat ${tfile}_3 failed"
5281 }
5282 run_test 405 "archive and release under striped directory"
5283
5284 test_406() {
5285         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
5286
5287         [ $MDS1_VERSION -lt $(version_code 2.7.64) ] &&
5288                 skip "need MDS version at least 2.7.64"
5289
5290         local fid
5291         local mdt_index
5292
5293         mkdir_on_mdt0 $DIR/$tdir
5294
5295         fid=$(create_small_file $DIR/$tdir/$tfile)
5296         echo "old fid $fid"
5297
5298         copytool setup
5299
5300         $LFS hsm_archive $DIR/$tdir/$tfile
5301         wait_request_state "$fid" ARCHIVE SUCCEED
5302         $LFS hsm_release $DIR/$tdir/$tfile
5303
5304         # Should migrate $tdir but not $tfile.
5305         $LFS migrate -m1 $DIR/$tdir &&
5306                 error "migrating HSM an archived file should fail"
5307
5308         $LFS hsm_restore $DIR/$tdir/$tfile
5309         wait_request_state "$fid" RESTORE SUCCEED
5310
5311         $LFS hsm_remove $DIR/$tdir/$tfile
5312         wait_request_state "$fid" REMOVE SUCCEED
5313
5314         cat $DIR/$tdir/$tfile > /dev/null ||
5315                 error "cannot read $DIR/$tdir/$tfile"
5316
5317         $LFS migrate -m1 $DIR/$tdir ||
5318                 error "cannot complete migration after HSM remove"
5319
5320         mdt_index=$($LFS getstripe -m $DIR/$tdir)
5321         if ((mdt_index != 1)); then
5322                 error "expected MDT index 1, got $mdt_index"
5323         fi
5324
5325         # Refresh fid after migration.
5326         fid=$(path2fid $DIR/$tdir/$tfile)
5327         echo "new fid $fid"
5328
5329         $LFS hsm_archive $DIR/$tdir/$tfile
5330         wait_request_state "$fid" ARCHIVE SUCCEED 1
5331
5332         lctl set_param debug=+trace
5333         $LFS hsm_release $DIR/$tdir/$tfile ||
5334                 error "cannot release $DIR/$tdir/$tfile"
5335
5336         $LFS hsm_restore $DIR/$tdir/$tfile
5337         wait_request_state "$fid" RESTORE SUCCEED 1
5338
5339         cat $DIR/$tdir/$tfile > /dev/null ||
5340                 error "cannot read $DIR/$tdir/$tfile"
5341 }
5342 run_test 406 "attempting to migrate HSM archived files is safe"
5343
5344 test_407() {
5345         mkdir_on_mdt0 $DIR/$tdir
5346
5347         local f=$DIR/$tdir/$tfile
5348         local f2=$DIR2/$tdir/$tfile
5349         local fid=$(create_empty_file "$f")
5350
5351         copytool setup
5352
5353         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
5354         wait_request_state $fid ARCHIVE SUCCEED
5355         $LFS hsm_release $f
5356
5357 #define OBD_FAIL_MDS_HSM_CDT_DELAY      0x164
5358         do_facet $SINGLEMDS $LCTL set_param fail_val=5 fail_loc=0x164
5359
5360         # Prevent restore from completing
5361         copytool_suspend
5362
5363         md5sum $f &
5364         # 1st request holds layout lock while appropriate
5365         # RESTORE record is still not added to llog
5366         md5sum $f2 &
5367         sleep 2
5368
5369         do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"
5370         # after umount hsm_actions->O/x/x log shouldn't have
5371         # double RESTORE records like below
5372         #[0x200000401:0x1:0x0]...0x58d03a0d/0x58d03a0c action=RESTORE...WAITING
5373         #[0x200000401:0x1:0x0]...0x58d03a0c/0x58d03a0d action=RESTORE...WAITING
5374         sleep 30 &&
5375                 do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"&
5376         fail $SINGLEMDS
5377         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
5378
5379         do_facet $SINGLEMDS "$LCTL get_param $HSM_PARAM.actions"
5380
5381         copytool_continue
5382         wait_all_done 100 $fid
5383 }
5384 run_test 407 "Check for double RESTORE records in llog"
5385
5386 test_500()
5387 {
5388         [ "$MDS1_VERSION" -lt $(version_code 2.6.92) ] &&
5389                 skip "HSM migrate is not supported"
5390
5391         test_mkdir -p $DIR/$tdir
5392
5393         if [ "$CLIENT_VERSION" -lt $(version_code 2.11.56) ] ||
5394              [ "$MDS1_VERSION" -lt $(version_code 2.11.56) ];
5395         then
5396                 llapi_hsm_test -d $DIR/$tdir -b ||
5397                         error "One llapi HSM test failed"
5398         else
5399                 llapi_hsm_test -d $DIR/$tdir ||
5400                         error "One llapi HSM test failed"
5401         fi
5402 }
5403 run_test 500 "various LLAPI HSM tests"
5404
5405 test_600() {
5406         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
5407                 skip "need MDS version at least 2.10.58"
5408
5409         mkdir -p $DIR/$tdir
5410
5411         local f=$DIR/$tdir/$tfile
5412
5413         changelog_register
5414         # set changelog_mask to ALL
5415         changelog_chmask "ALL"
5416
5417         chmod 777 $DIR/$tdir
5418         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5419         local fid=$(path2fid $f)
5420
5421         local entry
5422         entry=$(changelog_find -type CREAT -target-fid $fid -uid "$RUNAS_ID" \
5423                                -gid "$RUNAS_GID") ||
5424                 error "No matching CREAT entry"
5425
5426         # Parse the changelog
5427         eval local -A changelog=$(changelog2array $entry)
5428         local nid="${changelog[nid]}"
5429
5430         # Check its NID
5431         echo "Got NID '$nid'"
5432         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5433                 error "nid '$nid' does not match any client NID:" \
5434                       "${CLIENT_NIDS[@]}"
5435 }
5436 run_test 600 "Changelog fields 'u=' and 'nid='"
5437
5438 test_601() {
5439         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5440                 skip "need MDS version at least 2.10.58"
5441
5442         mkdir -p $DIR/$tdir
5443
5444         local f=$DIR/$tdir/$tfile
5445
5446         changelog_register
5447         # set changelog_mask to ALL
5448         changelog_chmask "ALL"
5449
5450         touch $f || error "touch $f failed"
5451         local fid=$(path2fid $f)
5452
5453         changelog_clear
5454         cat $f || error "cat $f failed"
5455
5456         changelog_find -type OPEN -target-fid $fid -mode "r--" ||
5457                 error "No matching OPEN entry"
5458 }
5459 run_test 601 "OPEN Changelog entry"
5460
5461 test_602() {
5462         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5463                 skip "need MDS version at least 2.10.58"
5464
5465         stack_trap "restore_opencache" EXIT
5466         disable_opencache
5467
5468         mkdir -p $DIR/$tdir
5469
5470         local f=$DIR/$tdir/$tfile
5471
5472         changelog_register
5473         # set changelog_mask to ALL
5474         changelog_chmask "ALL"
5475
5476         touch $f || error "touch $f failed"
5477         local fid=$(path2fid $f)
5478
5479         changelog_clear
5480         cat $f || error "cat $f failed"
5481
5482         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5483
5484         changelog_clear
5485         changelog_dump
5486         echo f > $f || error "write $f failed"
5487         changelog_dump
5488
5489         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5490
5491         # remove OPEN from changelog_mask
5492         changelog_chmask "-OPEN"
5493
5494         changelog_clear
5495         changelog_dump
5496         cat $f || error "cat $f failed"
5497         changelog_dump
5498
5499         changelog_find -type CLOSE -target-fid $fid &&
5500                 error "There should be no CLOSE entry"
5501
5502         changelog_clear
5503         changelog_dump
5504         echo f > $f || error "write $f failed"
5505         changelog_dump
5506
5507         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5508 }
5509 run_test 602 "Changelog record CLOSE only if open+write or OPEN recorded"
5510
5511 test_603() {
5512         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5513                 skip "need MDS version at least 2.10.58"
5514
5515         mkdir -p $DIR/$tdir
5516
5517         local f=$DIR/$tdir/$tfile
5518
5519         changelog_register
5520         # set changelog_mask to ALL
5521         changelog_chmask "ALL"
5522
5523         touch $f || error "touch $f failed"
5524         local fid=$(path2fid $f)
5525
5526         setfattr -n user.xattr1 -v "value1" $f || error "setfattr $f failed"
5527
5528         changelog_clear
5529         getfattr -n user.xattr1 $f || error "getfattr $f failed"
5530
5531         changelog_find -type GXATR -target-fid $fid -xattr "user.xattr1" ||
5532                 error "No matching GXATR entry"
5533 }
5534 run_test 603 "GETXATTR Changelog entry"
5535
5536 test_604() {
5537         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5538                 skip "need MDS version at least 2.10.58"
5539
5540         mkdir_on_mdt0 $DIR/$tdir
5541
5542         local f=$DIR/$tdir/$tfile
5543         local f2=$DIR2/$tdir/$tfile
5544         local procname="mdd.$FSNAME-MDT0000.changelog_deniednext"
5545         local timeout
5546         timeout="$(do_facet mds1 "$LCTL" get_param -n "$procname")"
5547         stack_trap "do_facet mds1 '$LCTL' set_param '$procname=$timeout'" EXIT
5548         do_facet mds1 lctl set_param "$procname=20"
5549
5550
5551         changelog_register
5552         # set changelog_mask to ALL
5553         changelog_chmask "ALL"
5554
5555         touch $f || error "touch $f failed"
5556         local fid=$(path2fid $f)
5557
5558         chmod 600 $f
5559
5560         changelog_clear
5561         changelog_dump
5562         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5563         changelog_dump
5564
5565         local entry
5566         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5567                                -gid "$RUNAS_GID" -mode "r--") ||
5568                 error "No matching NOPEN entry"
5569
5570         # Parse the changelog
5571         eval local -A changelog=$(changelog2array $entry)
5572         local nid="${changelog[nid]}"
5573
5574         # Check its NID
5575         echo "Got NID '$nid'"
5576         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5577                 error "nid '$nid' does not match any client NID:" \
5578                       "${CLIENT_NIDS[@]}"
5579
5580         changelog_clear
5581         changelog_dump
5582         $RUNAS cat $f2 && error "cat $f2 by user $RUNAS_ID should have failed"
5583         changelog_dump
5584
5585         changelog_find -type NOPEN -target-fid $fid &&
5586                 error "There should be no NOPEN entry"
5587
5588         # Sleep for `changelog_deniednext` seconds
5589         sleep 20
5590
5591         changelog_clear
5592         changelog_dump
5593         $RUNAS cat $f2 && error "cat $f by user $RUNAS_ID should have failed"
5594         changelog_dump
5595
5596         entry=$(changelog_find -type NOPEN -target-fid $fid -uid "$RUNAS_ID" \
5597                                -gid "$RUNAS_GID" -mode "r--") ||
5598                 error "No matching NOPEN entry"
5599
5600         # Parse the changelog
5601         eval local -A changelog=$(changelog2array $entry)
5602         local nid="${changelog[nid]}"
5603
5604         # Check the NID
5605         echo "Got NID '$nid'"
5606         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5607                 error "nid '$nid' does not match any client NID:" \
5608                       "${CLIENT_NIDS[@]}"
5609 }
5610 run_test 604 "NOPEN Changelog entry"
5611
5612 test_605() {
5613         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5614                 skip "need MDS version at least 2.10.58"
5615
5616         stack_trap "restore_opencache" EXIT
5617         disable_opencache
5618
5619         mkdir -p $DIR/$tdir
5620
5621         local f=$DIR/$tdir/$tfile
5622         local f2=$DIR2/$tdir/$tfile
5623
5624         changelog_register
5625         # set changelog_mask to ALL
5626         changelog_chmask "ALL"
5627
5628         touch $f || error "touch $f failed"
5629         local fid=$(path2fid $f)
5630
5631         changelog_clear
5632         changelog_dump
5633         exec 3<> $f || error "open $f failed"
5634         changelog_dump
5635
5636         local entry
5637         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5638
5639         changelog_clear
5640         changelog_dump
5641         exec 4<> $f || error "open $f failed"
5642         changelog_dump
5643
5644         changelog_find -type OPEN -target-fid $fid &&
5645                 error "There should be no OPEN entry"
5646
5647         exec 4>&- || error "close $f failed"
5648         changelog_dump
5649
5650         changelog_find -type CLOSE -target-fid $fid &&
5651                 error "There should be no CLOSE entry"
5652
5653         changelog_clear
5654         changelog_dump
5655         # access in rw, so different access mode should generate entries
5656         cat $f || error "cat $f failed"
5657         changelog_dump
5658
5659         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5660
5661         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5662
5663         changelog_clear
5664         changelog_dump
5665         # same access as first one, should not generate new entries
5666         exec 4<> $f || error "open $f failed"
5667         changelog_dump
5668
5669         changelog_find -type OPEN -target-fid $fid &&
5670                 error "There should be no OPEN entry"
5671
5672         exec 4>&- || error "close $f failed"
5673         changelog_dump
5674
5675         changelog_find -type CLOSE -target-fid $fid &&
5676                 error "There should be no CLOSE entry"
5677
5678         changelog_clear
5679         changelog_dump
5680         # access by different user should generate new entries
5681         $RUNAS cat $f || error "cat $f by user $RUNAS_ID failed"
5682         changelog_dump
5683
5684         changelog_find -type OPEN -target-fid $fid || error "No OPEN entry"
5685
5686         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5687
5688         changelog_clear
5689         changelog_dump
5690         exec 3>&- || error "close $f failed"
5691         changelog_dump
5692
5693         changelog_find -type CLOSE -target-fid $fid || error "No CLOSE entry"
5694 }
5695 run_test 605 "Test OPEN and CLOSE rate limit in Changelogs"
5696
5697 test_606() {
5698         [ $MDS1_VERSION -lt $(version_code 2.10.58) ] &&
5699                 skip "need MDS version at least 2.10.58"
5700
5701         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5702         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5703         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5704                         skip_env "missing llog_reader"
5705
5706         mkdir_on_mdt0 $DIR/$tdir
5707
5708         local f=$DIR/$tdir/$tfile
5709
5710         changelog_register
5711         # set changelog_mask to ALL
5712         changelog_chmask "ALL"
5713
5714         chmod 777 $DIR/$tdir
5715         $RUNAS touch $f || error "touch $f failed as $RUNAS_ID"
5716         local fid=$(path2fid $f)
5717         rm $f || error "rm $f failed"
5718
5719         local mntpt=$(facet_mntpt mds1)
5720         local pass=true
5721         local entry
5722
5723         #remount mds1 as ldiskfs or zfs type
5724         stop mds1 || error "stop mds1 failed"
5725         stack_trap "unmount_fstype mds1; start mds1 $(mdsdevname 1)\
5726                 $MDS_MOUNT_OPTS" EXIT
5727         mount_fstype mds1 || error "remount mds1 failed"
5728
5729         for ((i = 0; i < 1; i++)); do
5730                 do_facet mds1 $llog_reader $mntpt/changelog_catalog
5731                 local cat_file=$(do_facet mds1 $llog_reader \
5732                                 $mntpt/changelog_catalog | awk \
5733                                 '{match($0,"path=([^ ]+)",a)}END{print a[1]}')
5734                 [ -n "$cat_file" ] || error "no catalog file"
5735
5736                 entry=$(do_facet mds1 $llog_reader $mntpt/$cat_file |
5737                         awk "/CREAT/ && /target:\[$fid\]/ {print}")
5738                 [ -n "$entry" ] || error "no CREAT entry"
5739         done
5740
5741         local uidgid=$(echo $entry |
5742                 sed 's+.*\ user:\([0-9][0-9]*:[0-9][0-9]*\)\ .*+\1+')
5743         [ -n "$uidgid" ] || error "uidgid is empty"
5744         echo "Got UID/GID $uidgid"
5745         [ "$uidgid" = "$RUNAS_ID:$RUNAS_GID" ] ||
5746                 error "uidgid '$uidgid' != '$RUNAS_ID:$RUNAS_GID'"
5747         local nid=$(echo $entry |
5748                 sed 's+.*\ nid:\(\S\S*@\S\S*\)\ .*+\1+')
5749         [ -n "$nid" ] || error "nid is empty"
5750         echo "Got NID $nid"
5751         [ -n "$nid" ] && [[ "${CLIENT_NIDS[*]}" =~ $nid ]] ||
5752                 error "nid '$nid' does not match any NID ${CLIENT_NIDS[*]}"
5753 }
5754 run_test 606 "llog_reader groks changelog fields"
5755
5756 complete $SECONDS
5757 check_and_cleanup_lustre
5758 exit_status