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