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