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