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