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