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