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