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