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