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