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