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