Whamcloud - gitweb
LU-3834 mdt: handle swap_layouts failures during restore
[fs/lustre-release.git] / lustre / tests / sanity-hsm.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # exit on error
7 set -e
8 set +o monitor
9
10 SRCDIR=$(dirname $0)
11 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin
12
13 ONLY=${ONLY:-"$*"}
14 # bug number for skipped test:    3815
15 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT 34 35 36"
16 # bug number for skipped test:4178         4176
17 ALWAYS_EXCEPT="$ALWAYS_EXCEPT 200 221 223b 31a"
18 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
19
20 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
21
22 . $LUSTRE/tests/test-framework.sh
23 init_test_env $@
24 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
25 init_logging
26
27 MULTIOP=${MULTIOP:-multiop}
28 OPENFILE=${OPENFILE:-openfile}
29 MCREATE=${MCREATE:-mcreate}
30 MOUNT_2=${MOUNT_2:-"yes"}
31 FAIL_ON_ERROR=false
32
33 # script only handles up to 10 MDTs (because of MDT_PREFIX)
34 [ $MDSCOUNT -gt 9 ] &&
35         error "script cannot handle more than 9 MDTs, please fix" && exit
36
37 check_and_setup_lustre
38
39 if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ]]; then
40         skip_env "Need MDS version at least 2.4.53" && exit
41 fi
42
43 # $RUNAS_ID may get set incorrectly somewhere else
44 if [[ $UID -eq 0 && $RUNAS_ID -eq 0 ]]; then
45         skip_env "\$RUNAS_ID set to 0, but \$UID is also 0!" && exit
46 fi
47 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
48
49 build_test_filter
50
51 #
52 # In order to test multiple remote HSM agents, a new facet type named "AGT" and
53 # the following associated variables are added:
54 #
55 # AGTCOUNT: number of agents
56 # AGTDEV{N}: target HSM mount point (root path of the backend)
57 # agt{N}_HOST: hostname of the agent agt{N}
58 # SINGLEAGT: facet of the single agent
59 #
60 # The number of agents is initialized as the number of remote client nodes.
61 # By default, only single copytool is started on a remote client/agent. If there
62 # was no remote client, then the copytool will be started on the local client.
63 #
64 init_agt_vars() {
65         local n
66         local agent
67
68         export AGTCOUNT=${AGTCOUNT:-$((CLIENTCOUNT - 1))}
69         [[ $AGTCOUNT -gt 0 ]] || AGTCOUNT=1
70
71         export SHARED_DIRECTORY=${SHARED_DIRECTORY:-$TMP}
72         if [[ $CLIENTCOUNT -gt 1 ]] &&
73                 ! check_shared_dir $SHARED_DIRECTORY $CLIENTS; then
74                 skip_env "SHARED_DIRECTORY should be accessible"\
75                          "on all client nodes"
76                 exit 0
77         fi
78
79         for n in $(seq $AGTCOUNT); do
80                 eval export AGTDEV$n=\$\{AGTDEV$n:-"$SHARED_DIRECTORY/arc$n"\}
81                 agent=CLIENT$((n + 1))
82                 if [[ -z "${!agent}" ]]; then
83                         [[ $CLIENTCOUNT -eq 1 ]] && agent=CLIENT1 ||
84                                 agent=CLIENT2
85                 fi
86                 eval export agt${n}_HOST=\$\{agt${n}_HOST:-${!agent}\}
87         done
88
89         export SINGLEAGT=${SINGLEAGT:-agt1}
90
91         export HSMTOOL=${HSMTOOL:-"lhsmtool_posix"}
92         export HSMTOOL_VERBOSE=${HSMTOOL_VERBOSE:-""}
93         export HSMTOOL_BASE=$(basename "$HSMTOOL" | cut -f1 -d" ")
94         HSM_ARCHIVE=$(copytool_device $SINGLEAGT)
95         HSM_ARCHIVE_NUMBER=2
96
97         # The test only support up to 10 MDTs
98         MDT_PREFIX="mdt.$FSNAME-MDT000"
99         HSM_PARAM="${MDT_PREFIX}0.hsm"
100
101         # archive is purged at copytool setup
102         HSM_ARCHIVE_PURGE=true
103 }
104
105 # Get the backend root path for the given agent facet.
106 copytool_device() {
107         local facet=$1
108         local dev=AGTDEV$(facet_number $facet)
109
110         echo -n ${!dev}
111 }
112
113 # Stop copytool and unregister an existing changelog user.
114 cleanup() {
115         copytool_cleanup
116         changelog_cleanup
117         cdt_set_sanity_policy
118 }
119
120 get_mdt_devices() {
121         local mdtno
122         # get MDT device for each mdc
123         for mdtno in $(seq 1 $MDSCOUNT); do
124                 local idx=$(($mdtno - 1))
125                 MDT[$idx]=$($LCTL get_param -n \
126                         mdc.$FSNAME-MDT000${idx}-mdc-*.mds_server_uuid |
127                         awk '{gsub(/_UUID/,""); print $1}' | head -1)
128         done
129 }
130
131 search_copytools() {
132         local agents=${1:-$(facet_active_host $SINGLEAGT)}
133         do_nodesv $agents "pgrep -x $HSMTOOL_BASE"
134 }
135
136 search_and_kill_copytool() {
137         local agents=${1:-$(facet_active_host $SINGLEAGT)}
138
139         echo "Killing existing copytools on $agents"
140         do_nodesv $agents "killall -q $HSMTOOL_BASE" || true
141 }
142
143 copytool_setup() {
144         local facet=${1:-$SINGLEAGT}
145         local lustre_mntpnt=${2:-$MOUNT}
146         local arc_id=$3
147         local hsm_root=${4:-$(copytool_device $facet)}
148         local agent=$(facet_active_host $facet)
149
150         if [[ -z "$arc_id" ]] &&
151                 do_facet $facet "pkill -CONT -x $HSMTOOL_BASE"; then
152                         echo "Wakeup copytool $facet on $agent"
153                         return 0
154         fi
155
156         if $HSM_ARCHIVE_PURGE; then
157                 echo "Purging archive on $agent"
158                 do_facet $facet "rm -rf $hsm_root/*"
159         fi
160
161         echo "Starting copytool $facet on $agent"
162         do_facet $facet "mkdir -p $hsm_root" || error "mkdir '$hsm_root' failed"
163         # bandwidth is limited to 1MB/s so the copy time is known and
164         # independent of hardware
165         local cmd="$HSMTOOL $HSMTOOL_VERBOSE --daemon --hsm-root $hsm_root"
166         [[ -z "$arc_id" ]] || cmd+=" --archive $arc_id"
167         cmd+=" --bandwidth 1 $lustre_mntpnt"
168
169         # Redirect the standard output and error to a log file which
170         # can be uploaded to Maloo.
171         local prefix=$TESTLOG_PREFIX
172         [[ -z "$TESTNAME" ]] || prefix=$prefix.$TESTNAME
173         local copytool_log=$prefix.copytool${arc_id}_log.$agent.log
174
175         do_facet $facet "$cmd < /dev/null > $copytool_log 2>&1" ||
176                 error "start copytool $facet on $agent failed"
177         trap cleanup EXIT
178 }
179
180 copytool_cleanup() {
181         trap - EXIT
182         local agents=${1:-$(facet_active_host $SINGLEAGT)}
183         local mdtno
184         local idx
185         local oldstate
186         local mdt_hsmctrl
187
188         do_nodesv $agents "pkill -INT -x $HSMTOOL_BASE" || return 0
189         sleep 1
190         echo "Copytool is stopped on $agents"
191
192         # clean all CDTs orphans requests from previous tests
193         # that would otherwise need to timeout to clear.
194         for mdtno in $(seq 1 $MDSCOUNT); do
195                 idx=$(($mdtno - 1))
196                 mdt_hsmctrl="mdt.$FSNAME-MDT000${idx}.hsm_control"
197                 oldstate=$(do_facet mds${mdtno} "$LCTL get_param -n " \
198                                    "$mdt_hsmctrl")
199                 # skip already stop[ed,ing] CDTs
200                 echo $oldstate | grep stop && continue
201
202                 do_facet mds${mdtno} "$LCTL set_param $mdt_hsmctrl=shutdown"
203                 wait_result mds${mdtno} "$LCTL get_param -n $mdt_hsmctrl" \
204                         "stopped" 20 ||
205                         error "mds${mdtno} cdt state is not stopped"
206                 do_facet mds${mdtno} "$LCTL set_param $mdt_hsmctrl=$oldstate"
207                 wait_result mds${mdtno} "$LCTL get_param -n $mdt_hsmctrl" \
208                         "$oldstate" 20 ||
209                         error "mds${mdtno} cdt state is not $oldstate"
210         done
211 }
212
213 copytool_suspend() {
214         local agents=${1:-$(facet_active_host $SINGLEAGT)}
215
216         do_nodesv $agents "pkill -STOP -x $HSMTOOL_BASE" || return 0
217         echo "Copytool is suspended on $agents"
218 }
219
220 copytool_remove_backend() {
221         local fid=$1
222         local be=$(do_facet $SINGLEAGT find $HSM_ARCHIVE -name $fid)
223         echo "Remove from backend: $fid = $be"
224         do_facet $SINGLEAGT rm -f $be
225 }
226
227 import_file() {
228         do_facet $SINGLEAGT \
229                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
230                 --import $1 $2 $MOUNT" ||
231                 error "import of $1 to $2 failed"
232 }
233
234 make_archive() {
235         local file=$HSM_ARCHIVE/$1
236         do_facet $SINGLEAGT mkdir -p $(dirname $file)
237         do_facet $SINGLEAGT dd if=/dev/urandom of=$file count=32 bs=1000000 ||
238                 error "cannot create $file"
239 }
240
241 copy2archive() {
242         local file=$HSM_ARCHIVE/$2
243         do_facet $SINGLEAGT mkdir -p $(dirname $file)
244         do_facet $SINGLEAGT cp -p $1 $file || error "cannot copy $1 to $file"
245 }
246
247 mdts_set_param() {
248         local arg=$1
249         local key=$2
250         local value=$3
251         local mdtno
252         local rc=0
253         if [[ "$value" != "" ]]; then
254                 value="=$value"
255         fi
256         for mdtno in $(seq 1 $MDSCOUNT); do
257                 local idx=$(($mdtno - 1))
258                 local facet=mds${mdtno}
259                 # if $arg include -P option, run 1 set_param per MDT on the MGS
260                 # else, run set_param on each MDT
261                 [[ $arg = *"-P"* ]] && facet=mgs
262                 do_facet $facet $LCTL set_param $arg mdt.${MDT[$idx]}.$key$value
263                 [[ $? != 0 ]] && rc=1
264         done
265         return $rc
266 }
267
268 mdts_check_param() {
269         local key="$1"
270         local target="$2"
271         local timeout="$3"
272         local mdtno
273         for mdtno in $(seq 1 $MDSCOUNT); do
274                 local idx=$(($mdtno - 1))
275                 wait_result mds${mdtno} \
276                         "$LCTL get_param -n $MDT_PREFIX${idx}.$key" "$target" \
277                         $timeout ||
278                         error "$key state is not '$target' on mds${mdtno}"
279         done
280 }
281
282 changelog_setup() {
283         CL_USERS=()
284         local mdtno
285         for mdtno in $(seq 1 $MDSCOUNT); do
286                 local idx=$(($mdtno - 1))
287                 local cl_user=$(do_facet mds${mdtno} $LCTL \
288                              --device ${MDT[$idx]} \
289                              changelog_register -n)
290                 CL_USERS+=($cl_user)
291                 do_facet mds${mdtno} lctl set_param \
292                         mdd.${MDT[$idx]}.changelog_mask="+hsm"
293                 $LFS changelog_clear ${MDT[$idx]} $cl_user 0
294         done
295 }
296
297 changelog_cleanup() {
298         local mdtno
299         for mdtno in $(seq 1 $MDSCOUNT); do
300                 local idx=$(($mdtno - 1))
301                 [[ -z  ${CL_USERS[$idx]} ]] && continue
302                 $LFS changelog_clear ${MDT[$idx]} ${CL_USERS[$idx]} 0
303                 do_facet mds${mdtno} lctl --device ${MDT[$idx]} \
304                         changelog_deregister ${CL_USERS[$idx]}
305         done
306         CL_USERS=()
307 }
308
309 changelog_get_flags() {
310         local mdt=$1
311         local cltype=$2
312         local fid=$3
313
314         $LFS changelog $mdt | awk "/$cltype/ && /t=\[$fid\]/ {print \$5}"
315 }
316
317 get_hsm_param() {
318         local param=$1
319         local val=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.$param)
320         echo $val
321 }
322
323 set_hsm_param() {
324         local param=$1
325         local value=$2
326         local opt=$3
327         mdts_set_param "$opt -n" "hsm.$param" "$value"
328         return $?
329 }
330
331 set_test_state() {
332         local cmd=$1
333         local target=$2
334         mdts_set_param "" hsm_control "$cmd"
335         mdts_check_param hsm_control "$target" 10
336 }
337
338 cdt_set_sanity_policy() {
339         if [[ "$CDT_POLICY_HAD_CHANGED" ]]
340         then
341                 # clear all
342                 mdts_set_param "" hsm.policy "+NRA"
343                 mdts_set_param "" hsm.policy "-NBR"
344                 CDT_POLICY_HAD_CHANGED=
345         fi
346 }
347
348 cdt_set_no_retry() {
349         mdts_set_param "" hsm.policy "+NRA"
350         CDT_POLICY_HAD_CHANGED=true
351 }
352
353 cdt_clear_no_retry() {
354         mdts_set_param "" hsm.policy "-NRA"
355         CDT_POLICY_HAD_CHANGED=true
356 }
357
358 cdt_set_non_blocking_restore() {
359         mdts_set_param "" hsm.policy "+NBR"
360         CDT_POLICY_HAD_CHANGED=true
361 }
362
363 cdt_clear_non_blocking_restore() {
364         mdts_set_param "" hsm.policy "-NBR"
365         CDT_POLICY_HAD_CHANGED=true
366 }
367
368 cdt_clear_mount_state() {
369         mdts_set_param "-P -d" hsm_control ""
370 }
371
372 cdt_set_mount_state() {
373         mdts_set_param "-P" hsm_control "$1"
374 }
375
376 cdt_check_state() {
377         mdts_check_param hsm_control "$1" 20
378 }
379
380 cdt_disable() {
381         set_test_state disabled disabled
382 }
383
384 cdt_enable() {
385         set_test_state enabled enabled
386 }
387
388 cdt_shutdown() {
389         set_test_state shutdown stopped
390 }
391
392 cdt_purge() {
393         set_test_state purge enabled
394 }
395
396 cdt_restart() {
397         cdt_shutdown
398         cdt_enable
399         cdt_set_sanity_policy
400 }
401
402 needclients() {
403         local client_count=$1
404         if [[ $CLIENTCOUNT -lt $client_count ]]; then
405                 skip "Need $client_count or more clients, have $CLIENTCOUNT"
406                 return 1
407         fi
408         return 0
409 }
410
411 path2fid() {
412         $LFS path2fid $1 | tr -d '[]'
413 }
414
415 get_hsm_flags() {
416         local f=$1
417         local u=$2
418
419         if [[ $u == "user" ]]; then
420                 local st=$($RUNAS $LFS hsm_state $f)
421         else
422                 local st=$($LFS hsm_state $f)
423                 u=root
424         fi
425
426         [[ $? == 0 ]] || error "$LFS hsm_state $f failed (run as $u)"
427
428         st=$(echo $st | cut -f 2 -d" " | tr -d "()," )
429         echo $st
430 }
431
432 get_hsm_archive_id() {
433         local f=$1
434         local st=$($LFS hsm_state $f)
435         [[ $? == 0 ]] || error "$LFS hsm_state $f failed"
436
437         local ar=$(echo $st | grep "archive_id" | cut -f5 -d" " |
438                    cut -f2 -d:)
439         echo $ar
440 }
441
442 check_hsm_flags() {
443         local f=$1
444         local fl=$2
445
446         local st=$(get_hsm_flags $f)
447         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
448 }
449
450 check_hsm_flags_user() {
451         local f=$1
452         local fl=$2
453
454         local st=$(get_hsm_flags $f user)
455         [[ $st == $fl ]] || error "hsm flags on $f are $st != $fl"
456 }
457
458 copy_file() {
459         local f=
460
461         if [[ -d $2 ]]; then
462                 f=$2/$(basename $1)
463         else
464                 f=$2
465         fi
466
467         if [[ "$3" != 1 ]]; then
468                 f=${f/$DIR/$DIR2}
469         fi
470         rm -f $f
471         cp $1 $f || error "cannot copy $1 to $f"
472         path2fid $f || error "cannot get fid on $f"
473 }
474
475 make_small() {
476         local file2=${1/$DIR/$DIR2}
477         dd if=/dev/urandom of=$file2 count=2 bs=1M conv=fsync ||
478                 error "cannot create $file2"
479         path2fid $1 || error "cannot get fid on $1"
480 }
481
482 make_small_sync() {
483         dd if=/dev/urandom of=$1 count=1 bs=1M conv=sync ||
484                 error "cannot create $1"
485         path2fid $1 || error "cannot get fid on $1"
486 }
487
488 cleanup_large_files() {
489         local ratio=$(df -P $MOUNT | tail -1 | awk '{print $5}' |
490                       sed 's/%//g')
491         [ $ratio -gt 50 ] && find $MOUNT -size +10M -exec rm -f {} \;
492 }
493
494 make_large_for_striping() {
495         local file2=${1/$DIR/$DIR2}
496         local sz=$($LCTL get_param -n lov.*-clilov-*.stripesize | head -1)
497
498         cleanup_large_files
499
500         dd if=/dev/urandom of=$file2 count=5 bs=$sz conv=fsync ||
501                 error "cannot create $file2"
502         path2fid $1 || error "cannot get fid on $1"
503 }
504
505 make_large_for_progress() {
506         local file2=${1/$DIR/$DIR2}
507
508         cleanup_large_files
509
510         # big file is large enough, so copy time is > 30s
511         # so copytool make 1 progress
512         # size is not a multiple of 1M to avoid stripe
513         # aligment
514         dd if=/dev/urandom of=$file2 count=39 bs=1000000 conv=fsync ||
515                 error "cannot create $file2"
516         path2fid $1 || error "cannot get fid on $1"
517 }
518
519 make_large_for_progress_aligned() {
520         local file2=${1/$DIR/$DIR2}
521
522         cleanup_large_files
523
524         # big file is large enough, so copy time is > 30s
525         # so copytool make 1 progress
526         # size is a multiple of 1M to have stripe
527         # aligment
528         dd if=/dev/urandom of=$file2 count=33 bs=1M conv=fsync ||
529                 error "cannot create $file2"
530         path2fid $1 || error "cannot get fid on $1"
531 }
532
533 make_large_for_cancel() {
534         local file2=${1/$DIR/$DIR2}
535
536         cleanup_large_files
537
538         # Copy timeout is 100s. 105MB => 105s
539         dd if=/dev/urandom of=$file2 count=103 bs=1M conv=fsync ||
540                 error "cannot create $file2"
541         path2fid $1 || error "cannot get fid on $1"
542 }
543
544 wait_result() {
545         local facet=$1
546         shift
547         wait_update --verbose $(facet_active_host $facet) "$@"
548 }
549
550 wait_request_state() {
551         local fid=$1
552         local request=$2
553         local state=$3
554         # 4th arg (mdt index) is optional
555         local mdtidx=${4:-0}
556         local mds=mds$(($mdtidx + 1))
557
558         local cmd="$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.actions"
559         cmd+=" | awk '/'$fid'.*action='$request'/ {print \\\$13}' | cut -f2 -d="
560
561         wait_result $mds "$cmd" $state 100 ||
562                 error "request on $fid is not $state on $mds"
563 }
564
565 get_request_state() {
566         local fid=$1
567         local request=$2
568
569         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
570                 "awk '/'$fid'.*action='$request'/ {print \\\$13}' | cut -f2 -d="
571 }
572
573 get_request_count() {
574         local fid=$1
575         local request=$2
576
577         do_facet $SINGLEMDS "$LCTL get_param -n $HSM_PARAM.actions |"\
578                 "awk -vn=0 '/'$fid'.*action='$request'/ {n++}; END {print n}'"
579 }
580
581 wait_all_done() {
582         local timeout=$1
583
584         local cmd="$LCTL get_param -n $HSM_PARAM.actions"
585         cmd+=" | egrep 'WAITING|STARTED'"
586
587         wait_result $SINGLEMDS "$cmd" "" $timeout ||
588                 error "requests did not complete"
589 }
590
591 wait_for_grace_delay() {
592         local val=$(get_hsm_param grace_delay)
593         sleep $val
594 }
595
596 # populate MDT device array
597 get_mdt_devices
598
599 # initiate variables
600 init_agt_vars
601
602 # cleanup from previous bad setup
603 search_and_kill_copytool
604
605 # for recovery tests, coordinator needs to be started at mount
606 # so force it
607 # the lustre conf must be without hsm on (like for sanity.sh)
608 echo "Set HSM on and start"
609 cdt_set_mount_state enabled
610 cdt_check_state enabled
611
612 echo "Start copytool"
613 copytool_setup
614
615 echo "Set sanity-hsm HSM policy"
616 cdt_set_sanity_policy
617
618 # finished requests are quickly removed from list
619 set_hsm_param grace_delay 10
620
621 test_1() {
622         mkdir -p $DIR/$tdir
623         chmod 777 $DIR/$tdir
624
625         local f=$DIR/$tdir/$tfile
626         $RUNAS touch $f
627
628         # User flags
629         check_hsm_flags_user $f "0x00000000"
630
631         $RUNAS $LFS hsm_set --norelease $f ||
632                 error "user could not change hsm flags"
633         check_hsm_flags_user $f "0x00000010"
634
635         $RUNAS $LFS hsm_clear --norelease $f ||
636                 error "user could not clear hsm flags"
637         check_hsm_flags_user $f "0x00000000"
638
639         # User could not change those flags...
640         $RUNAS $LFS hsm_set --exists $f &&
641                 error "user should not set this flag"
642         check_hsm_flags_user $f "0x00000000"
643
644         # ...but root can
645         $LFS hsm_set --exists $f ||
646                 error "root could not change hsm flags"
647         check_hsm_flags_user $f "0x00000001"
648
649         $LFS hsm_clear --exists $f ||
650                 error "root could not clear hsm state"
651         check_hsm_flags_user $f "0x00000000"
652
653 }
654 run_test 1 "lfs hsm flags root/non-root access"
655
656 test_2() {
657         mkdir -p $DIR/$tdir
658         local f=$DIR/$tdir/$tfile
659         touch $f
660         # New files are not dirty
661         check_hsm_flags $f "0x00000000"
662
663         # For test, we simulate an archived file.
664         $LFS hsm_set --exists $f || error "user could not change hsm flags"
665         check_hsm_flags $f "0x00000001"
666
667         # chmod do not put the file dirty
668         chmod 600 $f || error "could not chmod test file"
669         check_hsm_flags $f "0x00000001"
670
671         # chown do not put the file dirty
672         chown $RUNAS_ID $f || error "could not chown test file"
673         check_hsm_flags $f "0x00000001"
674
675         # truncate put the file dirty
676         $TRUNCATE $f 1 || error "could not truncate test file"
677         check_hsm_flags $f "0x00000003"
678
679         $LFS hsm_clear --dirty $f || error "could not clear hsm flags"
680         check_hsm_flags $f "0x00000001"
681 }
682 run_test 2 "Check file dirtyness when doing setattr"
683
684 test_3() {
685         mkdir -p $DIR/$tdir
686         f=$DIR/$tdir/$tfile
687
688         # New files are not dirty
689         cp -p /etc/passwd $f
690         check_hsm_flags $f "0x00000000"
691
692         # For test, we simulate an archived file.
693         $LFS hsm_set --exists $f ||
694                 error "user could not change hsm flags"
695         check_hsm_flags $f "0x00000001"
696
697         # Reading a file, does not set dirty
698         cat $f > /dev/null || error "could not read file"
699         check_hsm_flags $f "0x00000001"
700
701         # Open for write without modifying data, does not set dirty
702         openfile -f O_WRONLY $f || error "could not open test file"
703         check_hsm_flags $f "0x00000001"
704
705         # Append to a file sets it dirty
706         cp -p /etc/passwd $f.append || error "could not create file"
707         $LFS hsm_set --exists $f.append ||
708                 error "user could not change hsm flags"
709         dd if=/etc/passwd of=$f.append bs=1 count=3\
710            conv=notrunc oflag=append status=noxfer ||
711                 error "could not append to test file"
712         check_hsm_flags $f.append "0x00000003"
713
714         # Modify a file sets it dirty
715         cp -p /etc/passwd $f.modify || error "could not create file"
716         $LFS hsm_set --exists $f.modify ||
717                 error "user could not change hsm flags"
718         dd if=/dev/zero of=$f.modify bs=1 count=3\
719            conv=notrunc status=noxfer ||
720                 error "could not modify test file"
721         check_hsm_flags $f.modify "0x00000003"
722
723         # Open O_TRUNC sets dirty
724         cp -p /etc/passwd $f.trunc || error "could not create file"
725         $LFS hsm_set --exists $f.trunc ||
726                 error "user could not change hsm flags"
727         cp /etc/group $f.trunc || error "could not override a file"
728         check_hsm_flags $f.trunc "0x00000003"
729
730         # Mmapped a file sets dirty
731         cp -p /etc/passwd $f.mmap || error "could not create file"
732         $LFS hsm_set --exists $f.mmap ||
733                 error "user could not change hsm flags"
734         multiop $f.mmap OSMWUc || error "could not mmap a file"
735         check_hsm_flags $f.mmap "0x00000003"
736 }
737 run_test 3 "Check file dirtyness when opening for write"
738
739 test_4() {
740         mkdir -p $DIR/$tdir
741         local f=$DIR/$tdir/$tfile
742         local fid=$(make_small $f)
743
744         $LFS hsm_cancel $f
745         local st=$(get_request_state $fid CANCEL)
746         [[ -z "$st" ]] || error "hsm_cancel must not be registered (state=$st)"
747 }
748 run_test 4 "Useless cancel must not be registered"
749
750 test_8() {
751         # test needs a running copytool
752         copytool_setup
753
754         mkdir -p $DIR/$tdir
755         local f=$DIR/$tdir/$tfile
756         local fid=$(copy_file /etc/passwd $f)
757         $LFS hsm_archive $f
758         wait_request_state $fid ARCHIVE SUCCEED
759
760         check_hsm_flags $f "0x00000009"
761
762         copytool_cleanup
763 }
764 run_test 8 "Test default archive number"
765
766 test_9() {
767         mkdir -p $DIR/$tdir
768         local f=$DIR/$tdir/$tfile
769         local fid=$(copy_file /etc/passwd $f)
770         # we do not use the default one to be sure
771         local new_an=$((HSM_ARCHIVE_NUMBER + 1))
772         copytool_cleanup
773         copytool_setup $SINGLEAGT $MOUNT $new_an
774         $LFS hsm_archive --archive $new_an $f
775         wait_request_state $fid ARCHIVE SUCCEED
776
777         check_hsm_flags $f "0x00000009"
778
779         copytool_cleanup
780 }
781 run_test 9 "Use of explict archive number, with dedicated copytool"
782
783 test_9a() {
784         needclients 3 || return 0
785
786         local n
787         local file
788         local fid
789
790         copytool_cleanup $(comma_list $(agts_nodes))
791
792         # start all of the copytools
793         for n in $(seq $AGTCOUNT); do
794                 copytool_setup agt$n
795         done
796
797         trap "copytool_cleanup $(comma_list $(agts_nodes))" EXIT
798         # archive files
799         mkdir -p $DIR/$tdir
800         for n in $(seq $AGTCOUNT); do
801                 file=$DIR/$tdir/$tfile.$n
802                 fid=$(make_small $file)
803
804                 $LFS hsm_archive $file || error "could not archive file $file"
805                 wait_request_state $fid ARCHIVE SUCCEED
806                 check_hsm_flags $file "0x00000009"
807         done
808
809         trap - EXIT
810         copytool_cleanup $(comma_list $(agts_nodes))
811 }
812 run_test 9a "Multiple remote agents"
813
814 test_10a() {
815         # test needs a running copytool
816         copytool_setup
817
818         mkdir -p $DIR/$tdir/d1
819         local f=$DIR/$tdir/$tfile
820         local fid=$(copy_file /etc/hosts $f)
821         $LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f ||
822                 error "hsm_archive failed"
823         wait_request_state $fid ARCHIVE SUCCEED
824
825         local AFILE=$(do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid) ||
826                 error "fid $fid not in archive $HSM_ARCHIVE"
827         echo "Verifying content"
828         do_facet $SINGLEAGT diff $f $AFILE || error "archived file differs"
829         echo "Verifying hsm state "
830         check_hsm_flags $f "0x00000009"
831
832         echo "Verifying archive number is $HSM_ARCHIVE_NUMBER"
833         local st=$(get_hsm_archive_id $f)
834         [[ $st == $HSM_ARCHIVE_NUMBER ]] ||
835                 error "Wrong archive number, $st != $HSM_ARCHIVE_NUMBER"
836
837         copytool_cleanup
838
839 }
840 run_test 10a "Archive a file"
841
842 test_10b() {
843         # test needs a running copytool
844         copytool_setup
845
846         mkdir -p $DIR/$tdir
847         local f=$DIR/$tdir/$tfile
848         local fid=$(copy_file /etc/hosts $f)
849         $LFS hsm_archive $f || error "archive request failed"
850         wait_request_state $fid ARCHIVE SUCCEED
851
852         $LFS hsm_archive $f || error "archive of non dirty file failed"
853         local cnt=$(get_request_count $fid ARCHIVE)
854         [[ "$cnt" == "1" ]] ||
855                 error "archive of non dirty file must not make a request"
856
857         copytool_cleanup
858 }
859 run_test 10b "Archive of non dirty file must work without doing request"
860
861 test_10c() {
862         # test needs a running copytool
863         copytool_setup
864
865         mkdir -p $DIR/$tdir
866         local f=$DIR/$tdir/$tfile
867         local fid=$(copy_file /etc/hosts $f)
868         $LFS hsm_set --noarchive $f
869         $LFS hsm_archive $f && error "archive a noarchive file must fail"
870
871         copytool_cleanup
872 }
873 run_test 10c "Check forbidden archive"
874
875 test_10d() {
876         # test needs a running copytool
877         copytool_setup
878
879         mkdir -p $DIR/$tdir
880         local f=$DIR/$tdir/$tfile
881         local fid=$(copy_file /etc/hosts $f)
882         $LFS hsm_archive $f || error "cannot archive $f"
883         wait_request_state $fid ARCHIVE SUCCEED
884
885         local ar=$(get_hsm_archive_id $f)
886         local dflt=$(get_hsm_param default_archive_id)
887         [[ $ar == $dflt ]] ||
888                 error "archived file is not on default archive: $ar != $dflt"
889
890         copytool_cleanup
891 }
892 run_test 10d "Archive a file on the default archive id"
893
894 test_11() {
895         mkdir -p $DIR/$tdir
896         copy2archive /etc/hosts $tdir/$tfile
897         local f=$DIR/$tdir/$tfile
898
899         import_file $tdir/$tfile $f
900         echo -n "Verifying released state: "
901         check_hsm_flags $f "0x0000000d"
902
903         local LSZ=$(stat -c "%s" $f)
904         local ASZ=$(do_facet $SINGLEAGT stat -c "%s" $HSM_ARCHIVE/$tdir/$tfile)
905
906         echo "Verifying imported size $LSZ=$ASZ"
907         [[ $LSZ -eq $ASZ ]] || error "Incorrect size $LSZ != $ASZ"
908         echo -n "Verifying released pattern: "
909         local PTRN=$($GETSTRIPE -L $f)
910         echo $PTRN
911         [[ $PTRN == 80000001 ]] || error "Is not released"
912         local fid=$(path2fid $f)
913         echo "Verifying new fid $fid in archive"
914
915         local AFILE=$(do_facet $SINGLEAGT ls $HSM_ARCHIVE'/*/*/*/*/*/*/'$fid) ||
916                 error "fid $fid not in archive $HSM_ARCHIVE"
917 }
918 run_test 11 "Import a file"
919
920 test_12a() {
921         # test needs a running copytool
922         copytool_setup
923
924         mkdir -p $DIR/$tdir
925         copy2archive /etc/hosts $tdir/$tfile
926
927         local f=$DIR/$tdir/$tfile
928         import_file $tdir/$tfile $f
929         local f=$DIR2/$tdir/$tfile
930         echo "Verifying released state: "
931         check_hsm_flags $f "0x0000000d"
932
933         local fid=$(path2fid $f)
934         $LFS hsm_restore $f
935         wait_request_state $fid RESTORE SUCCEED
936
937         echo "Verifying file state: "
938         check_hsm_flags $f "0x00000009"
939
940         do_facet $SINGLEAGT diff -q $HSM_ARCHIVE/$tdir/$tfile $f
941
942         [[ $? -eq 0 ]] || error "Restored file differs"
943
944         copytool_cleanup
945 }
946 run_test 12a "Restore an imported file explicitly"
947
948 test_12b() {
949         # test needs a running copytool
950         copytool_setup
951
952         mkdir -p $DIR/$tdir
953         copy2archive /etc/hosts $tdir/$tfile
954
955         local f=$DIR/$tdir/$tfile
956         import_file $tdir/$tfile $f
957         echo "Verifying released state: "
958         check_hsm_flags $f "0x0000000d"
959
960         cat $f > /dev/null || error "File read failed"
961
962         echo "Verifying file state after restore: "
963         check_hsm_flags $f "0x00000009"
964
965         do_facet $SINGLEAGT diff -q $HSM_ARCHIVE/$tdir/$tfile $f
966
967         [[ $? -eq 0 ]] || error "Restored file differs"
968
969         copytool_cleanup
970 }
971 run_test 12b "Restore an imported file implicitly"
972
973 test_12c() {
974         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
975
976         # test needs a running copytool
977         copytool_setup
978
979         mkdir -p $DIR/$tdir
980         local f=$DIR/$tdir/$tfile
981         $LFS setstripe -c 2 $f
982         local fid=$(make_large_for_striping $f)
983         local FILE_CRC=$(md5sum $f)
984
985         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
986         wait_request_state $fid ARCHIVE SUCCEED
987         $LFS hsm_release $f || error "release $f failed"
988
989         echo "$FILE_CRC" | md5sum -c
990
991         [[ $? -eq 0 ]] || error "Restored file differs"
992
993         copytool_cleanup
994 }
995 run_test 12c "Restore a file with stripe of 2"
996
997 test_12d() {
998         # test needs a running copytool
999         copytool_setup
1000
1001         mkdir -p $DIR/$tdir
1002
1003         local f=$DIR/$tdir/$tfile
1004         local fid=$(copy_file /etc/hosts $f)
1005         $LFS hsm_restore $f || error "restore of non archived file failed"
1006         local cnt=$(get_request_count $fid RESTORE)
1007         [[ "$cnt" == "0" ]] ||
1008                 error "restore non archived must not make a request"
1009         $LFS hsm_archive $f ||
1010                 error "archive request failed"
1011         wait_request_state $fid ARCHIVE SUCCEED
1012         $LFS hsm_restore $f ||
1013                 error "restore of non released file failed"
1014         local cnt=$(get_request_count $fid RESTORE)
1015         [[ "$cnt" == "0" ]] ||
1016                 error "restore a non dirty file must not make a request"
1017
1018         copytool_cleanup
1019 }
1020 run_test 12d "Restore of a non archived, non released file must work"\
1021                 " without doing request"
1022
1023 test_12e() {
1024         # test needs a running copytool
1025         copytool_setup
1026
1027         mkdir -p $DIR/$tdir $HSM_ARCHIVE/$tdir
1028         local f=$DIR/$tdir/$tfile
1029         local fid=$(copy_file /etc/hosts $f)
1030         $LFS hsm_archive $f || error "archive request failed"
1031         wait_request_state $fid ARCHIVE SUCCEED
1032
1033         # make file dirty
1034         cat /etc/hosts >> $f
1035         sync
1036         $LFS hsm_state $f
1037
1038         $LFS hsm_restore $f && error "restore a dirty file must fail"
1039
1040         copytool_cleanup
1041 }
1042 run_test 12e "Check forbidden restore"
1043
1044 test_12f() {
1045         # test needs a running copytool
1046         copytool_setup
1047
1048         mkdir -p $DIR/$tdir
1049         local f=$DIR/$tdir/$tfile
1050         local fid=$(copy_file /etc/hosts $f)
1051
1052         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1053         wait_request_state $fid ARCHIVE SUCCEED
1054         $LFS hsm_release $f || error "release of $f failed"
1055         $LFS hsm_restore $f
1056         wait_request_state $fid RESTORE SUCCEED
1057
1058         echo -n "Verifying file state: "
1059         check_hsm_flags $f "0x00000009"
1060
1061         diff -q /etc/hosts $f
1062
1063         [[ $? -eq 0 ]] || error "Restored file differs"
1064
1065         copytool_cleanup
1066 }
1067 run_test 12f "Restore a released file explicitly"
1068
1069 test_12g() {
1070         # test needs a running copytool
1071         copytool_setup
1072
1073         mkdir -p $DIR/$tdir
1074         local f=$DIR/$tdir/$tfile
1075         local fid=$(copy_file /etc/hosts $f)
1076
1077         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1078         wait_request_state $fid ARCHIVE SUCCEED
1079         $LFS hsm_release $f || error "release of $f failed"
1080
1081         diff -q /etc/hosts $f
1082         local st=$?
1083
1084         # we check we had a restore done
1085         wait_request_state $fid RESTORE SUCCEED
1086
1087         [[ $st -eq 0 ]] || error "Restored file differs"
1088
1089         copytool_cleanup
1090 }
1091 run_test 12g "Restore a released file implicitly"
1092
1093 test_12h() {
1094         needclients 2 || return 0
1095
1096         # test needs a running copytool
1097         copytool_setup
1098
1099         mkdir -p $DIR/$tdir
1100         local f=$DIR/$tdir/$tfile
1101         local fid=$(copy_file /etc/hosts $f)
1102
1103         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1104         wait_request_state $fid ARCHIVE SUCCEED
1105         $LFS hsm_release $f || error "release of $f failed"
1106
1107         do_node $CLIENT2 diff -q /etc/hosts $f
1108         local st=$?
1109
1110         # we check we had a restore done
1111         wait_request_state $fid RESTORE SUCCEED
1112
1113         [[ $st -eq 0 ]] || error "Restored file differs"
1114
1115         copytool_cleanup
1116 }
1117 run_test 12h "Restore a released file implicitly from a second node"
1118
1119 test_12m() {
1120         # test needs a running copytool
1121         copytool_setup
1122
1123         mkdir -p $DIR/$tdir
1124         local f=$DIR/$tdir/$tfile
1125         local fid=$(copy_file /etc/passwd $f)
1126         $LFS hsm_archive $f || error "archive of $f failed"
1127         wait_request_state $fid ARCHIVE SUCCEED
1128
1129         $LFS hsm_release $f || error "release of $f failed"
1130
1131         cmp /etc/passwd $f
1132
1133         [[ $? -eq 0 ]] || error "Restored file differs"
1134
1135         copytool_cleanup
1136 }
1137 run_test 12m "Archive/release/implicit restore"
1138
1139 test_12n() {
1140         # test needs a running copytool
1141         copytool_setup
1142
1143         mkdir -p $DIR/$tdir
1144         copy2archive /etc/hosts $tdir/$tfile
1145
1146         local f=$DIR/$tdir/$tfile
1147         import_file $tdir/$tfile $f
1148
1149         do_facet $SINGLEAGT cmp /etc/hosts $f ||
1150                 error "Restored file differs"
1151
1152         $LFS hsm_release $f || error "release of $f failed"
1153
1154         copytool_cleanup
1155 }
1156 run_test 12n "Import/implicit restore/release"
1157
1158 test_12o() {
1159         # test needs a running copytool
1160         copytool_setup
1161
1162         mkdir -p $DIR/$tdir
1163         local f=$DIR/$tdir/$tfile
1164         local fid=$(copy_file /etc/hosts $f)
1165
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 #define OBD_FAIL_MDS_HSM_SWAP_LAYOUTS           0x152
1171         do_facet $SINGLEMDS lctl set_param fail_loc=0x152
1172
1173         # set no retry action mode
1174         cdt_set_no_retry
1175
1176         diff -q /etc/hosts $f
1177         local st=$?
1178
1179         # we check we had a restore failure
1180         wait_request_state $fid RESTORE FAILED
1181
1182         [[ $st -eq 0 ]] && error "Restore must fail"
1183
1184         # remove no retry action mode
1185         cdt_clear_no_retry
1186
1187         # check file is still released
1188         check_hsm_flags $f "0x0000000d"
1189
1190         # retry w/o failure injection
1191         do_facet $SINGLEMDS lctl set_param fail_loc=0
1192
1193         diff -q /etc/hosts $f
1194         st=$?
1195
1196         # we check we had a restore done
1197         wait_request_state $fid RESTORE SUCCEED
1198
1199         [[ $st -eq 0 ]] || error "Restored file differs"
1200
1201         copytool_cleanup
1202 }
1203 run_test 12o "Layout-swap failure during Restore leaves file released"
1204
1205 test_13() {
1206         # test needs a running copytool
1207         copytool_setup
1208
1209         local ARC_SUBDIR="import.orig"
1210         local d=""
1211         local f=""
1212
1213         # populate directory to be imported
1214         for d in $(seq 1 10); do
1215                 local CURR_DIR="$HSM_ARCHIVE/$ARC_SUBDIR/dir.$d"
1216                 do_facet $SINGLEAGT mkdir -p "$CURR_DIR"
1217                 for f in $(seq 1 10); do
1218                         CURR_FILE="$CURR_DIR/$tfile.$f"
1219                         # write file-specific data
1220                         do_facet $SINGLEAGT \
1221                                 "echo d=$d, f=$f, dir=$CURR_DIR, "\
1222                                         "file=$CURR_FILE > $CURR_FILE"
1223                 done
1224         done
1225         # import to Lustre
1226         import_file "$ARC_SUBDIR" $DIR/$tdir
1227         # diff lustre content and origin (triggers file restoration)
1228         # there must be 10x10 identical files, and no difference
1229         local cnt_ok=$(do_facet $SINGLEAGT diff -rs $HSM_ARCHIVE/$ARC_SUBDIR \
1230                        $DIR/$tdir/$ARC_SUBDIR | grep identical | wc -l)
1231         local cnt_diff=$(do_facet $SINGLEAGT diff -r $HSM_ARCHIVE/$ARC_SUBDIR \
1232                          $DIR/$tdir/$ARC_SUBDIR | wc -l)
1233
1234         [ $cnt_diff -eq 0 ] ||
1235                 error "$cnt_diff imported files differ from read data"
1236         [ $cnt_ok -eq 100 ] ||
1237                 error "not enough identical files ($cnt_ok != 100)"
1238
1239         copytool_cleanup
1240 }
1241 run_test 13 "Recursively import and restore a directory"
1242
1243 test_14() {
1244         # test needs a running copytool
1245         copytool_setup
1246
1247         # archive a file
1248         mkdir -p $DIR/$tdir
1249         local f=$DIR/$tdir/$tfile
1250         local fid=$(make_small $f)
1251         local sum=$(md5sum $f | awk '{print $1}')
1252         $LFS hsm_archive $f || error "could not archive file"
1253         wait_request_state $fid ARCHIVE SUCCEED
1254
1255         # delete the file
1256         rm -f $f
1257         # create released file (simulate llapi_hsm_import call)
1258         touch $f
1259         local fid2=$(path2fid $f)
1260         $LFS hsm_set --archived --exists $f || error "could not force hsm flags"
1261         $LFS hsm_release $f || error "could not release file"
1262
1263         # rebind the archive to the newly created file
1264         echo "rebind $fid to $fid2"
1265
1266         do_facet $SINGLEAGT \
1267                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1268                  --rebind $fid $fid2 $DIR" || error "could not rebind file"
1269
1270         # restore file and compare md5sum
1271         local sum2=$(md5sum $f | awk '{print $1}')
1272
1273         [[ $sum == $sum2 ]] || error "md5sum mismatch after restore"
1274
1275         copytool_cleanup
1276 }
1277 run_test 14 "Rebind archived file to a new fid"
1278
1279 test_15() {
1280         # test needs a running copytool
1281         copytool_setup
1282
1283         # archive files
1284         mkdir -p $DIR/$tdir
1285         local f=$DIR/$tdir/$tfile
1286         local count=5
1287         local tmpfile=$SHARED_DIRECTORY/tmp.$$
1288
1289         local fids=()
1290         local sums=()
1291         for i in $(seq 1 $count); do
1292                 fids[$i]=$(make_small $f.$i)
1293                 sums[$i]=$(md5sum $f.$i | awk '{print $1}')
1294                 $LFS hsm_archive $f.$i || error "could not archive file"
1295         done
1296         wait_all_done $(($count*60))
1297
1298         :>$tmpfile
1299         # delete the files
1300         for i in $(seq 1 $count); do
1301                 rm -f $f.$i
1302                 touch $f.$i
1303                 local fid2=$(path2fid $f.$i)
1304                 # add the rebind operation to the list
1305                 echo ${fids[$i]} $fid2 >> $tmpfile
1306
1307                 # set it released (simulate llapi_hsm_import call)
1308                 $LFS hsm_set --archived --exists $f.$i ||
1309                         error "could not force hsm flags"
1310                 $LFS hsm_release $f.$i || error "could not release file"
1311         done
1312         nl=$(wc -l < $tmpfile)
1313         [[ $nl == $count ]] || error "$nl files in list, $count expected"
1314
1315         echo "rebind list of files"
1316         do_facet $SINGLEAGT \
1317                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1318                  --rebind $tmpfile $DIR" || error "could not rebind file list"
1319
1320         # restore files and compare md5sum
1321         for i in $(seq 1 $count); do
1322                 local sum2=$(md5sum $f.$i | awk '{print $1}')
1323                 [[ $sum2 == ${sums[$i]} ]] ||
1324                     error "md5sum mismatch after restore ($sum2 != ${sums[$i]})"
1325         done
1326
1327         rm -f $tmpfile
1328         copytool_cleanup
1329 }
1330 run_test 15 "Rebind a list of files"
1331
1332 test_16() {
1333         # test needs a running copytool
1334         copytool_setup
1335
1336         local ref=/tmp/ref
1337         # create a known size file so we can verify transfer speed
1338         # 20 MB <-> 20s
1339         local goal=20
1340         dd if=/dev/zero of=$ref bs=1M count=20
1341
1342         mkdir -p $DIR/$tdir
1343         local f=$DIR/$tdir/$tfile
1344         local fid=$(copy_file $ref $f)
1345         rm $ref
1346         local start=$(date +%s)
1347         $LFS hsm_archive $f
1348         wait_request_state $fid ARCHIVE SUCCEED
1349         local end=$(date +%s)
1350         local duration=$((end - start))
1351
1352         [[ $duration -ge $goal ]] ||
1353                 error "Transfer is too fast $duration < $goal"
1354
1355         copytool_cleanup
1356 }
1357 run_test 16 "Test CT bandwith control option"
1358
1359 test_20() {
1360         mkdir -p $DIR/$tdir
1361
1362         local f=$DIR/$tdir/$tfile
1363         touch $f || error "touch $f failed"
1364
1365         # Could not release a non-archived file
1366         $LFS hsm_release $f && error "release should not succeed"
1367
1368         # For following tests, we must test them with HS_ARCHIVED set
1369         $LFS hsm_set --exists --archived $f || error "could not add flag"
1370
1371         # Could not release a file if no-release is set
1372         $LFS hsm_set --norelease $f || error "could not add flag"
1373         $LFS hsm_release $f && error "release should not succeed"
1374         $LFS hsm_clear --norelease $f || error "could not remove flag"
1375
1376         # Could not release a file if lost
1377         $LFS hsm_set --lost $f || error "could not add flag"
1378         $LFS hsm_release $f && error "release should not succeed"
1379         $LFS hsm_clear --lost $f || error "could not remove flag"
1380
1381         # Could not release a file if dirty
1382         $LFS hsm_set --dirty $f || error "could not add flag"
1383         $LFS hsm_release $f && error "release should not succeed"
1384         $LFS hsm_clear --dirty $f || error "could not remove flag"
1385 }
1386 run_test 20 "Release is not permitted"
1387
1388 test_21() {
1389         # test needs a running copytool
1390         copytool_setup
1391
1392         mkdir -p $DIR/$tdir
1393         local f=$DIR/$tdir/test_release
1394
1395         # Create a file and check its states
1396         local fid=$(make_small $f)
1397         check_hsm_flags $f "0x00000000"
1398
1399         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1400         # used until file is flushed to disk
1401         if [  $(facet_fstype ost1) == "zfs" ]; then
1402             # this causes an OST_SYNC rpc to be sent
1403             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1404             # clear locks to reread file data
1405             cancel_lru_locks osc
1406         fi
1407
1408         local orig_size=$(stat -c "%s" $f)
1409         local orig_blocks=$(stat -c "%b" $f)
1410
1411         start_full_debug_logging
1412
1413         $LFS hsm_archive $f || error "could not archive file"
1414         wait_request_state $fid ARCHIVE SUCCEED
1415
1416         local blocks=$(stat -c "%b" $f)
1417         [ $blocks -eq $orig_blocks ] ||
1418                 error "$f: wrong block number after archive: " \
1419                       "$blocks != $orig_blocks"
1420         local size=$(stat -c "%s" $f)
1421         [ $size -eq $orig_size ] ||
1422                 error "$f: wrong size after archive: $size != $orig_size"
1423
1424         # Release and check states
1425         $LFS hsm_release $f || error "could not release file"
1426         check_hsm_flags $f "0x0000000d"
1427
1428         blocks=$(stat -c "%b" $f)
1429         [ $blocks -gt 5 ] &&
1430                 error "$f: too many blocks after release: $blocks > 5"
1431         size=$(stat -c "%s" $f)
1432         [ $size -ne $orig_size ] &&
1433                 error "$f: wrong size after release: $size != $orig_size"
1434
1435         # Check we can release an file without stripe info
1436         f=$f.nolov
1437         $MCREATE $f
1438         fid=$(path2fid $f)
1439         check_hsm_flags $f "0x00000000"
1440         $LFS hsm_archive $f || error "could not archive file"
1441         wait_request_state $fid ARCHIVE SUCCEED
1442
1443         # Release and check states
1444         $LFS hsm_release $f || error "could not release file"
1445         check_hsm_flags $f "0x0000000d"
1446
1447         # Release again a file that is already released is OK
1448         $LFS hsm_release $f || fail "second release should succeed"
1449         check_hsm_flags $f "0x0000000d"
1450
1451         stop_full_debug_logging
1452
1453         copytool_cleanup
1454 }
1455 run_test 21 "Simple release tests"
1456
1457 test_22() {
1458         # test needs a running copytool
1459         copytool_setup
1460
1461         mkdir -p $DIR/$tdir
1462
1463         local f=$DIR/$tdir/test_release
1464         local swap=$DIR/$tdir/test_swap
1465
1466         # Create a file and check its states
1467         local fid=$(make_small $f)
1468         check_hsm_flags $f "0x00000000"
1469
1470         $LFS hsm_archive $f || error "could not archive file"
1471         wait_request_state $fid ARCHIVE SUCCEED
1472
1473         # Release and check states
1474         $LFS hsm_release $f || error "could not release file"
1475         check_hsm_flags $f "0x0000000d"
1476
1477         make_small $swap
1478         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1479
1480         true
1481         copytool_cleanup
1482 }
1483 run_test 22 "Could not swap a release file"
1484
1485 test_23() {
1486         # test needs a running copytool
1487         copytool_setup
1488
1489         mkdir -p $DIR/$tdir
1490
1491         local f=$DIR/$tdir/test_mtime
1492
1493         # Create a file and check its states
1494         local fid=$(make_small $f)
1495         check_hsm_flags $f "0x00000000"
1496
1497         $LFS hsm_archive $f || error "could not archive file"
1498         wait_request_state $fid ARCHIVE SUCCEED
1499
1500         # Set modification time in the past
1501         touch -m -a -d @978261179 $f
1502
1503         # Release and check states
1504         $LFS hsm_release $f || error "could not release file"
1505         check_hsm_flags $f "0x0000000d"
1506
1507         local MTIME=$(stat -c "%Y" $f)
1508         local ATIME=$(stat -c "%X" $f)
1509         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1510         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1511
1512         copytool_cleanup
1513 }
1514 run_test 23 "Release does not change a/mtime (utime)"
1515
1516 test_24a() {
1517         local file=$DIR/$tdir/$tfile
1518         local fid
1519         local atime0
1520         local atime1
1521         local mtime0
1522         local mtime1
1523         local ctime0
1524         local ctime1
1525
1526         # test needs a running copytool
1527         copytool_setup
1528
1529         mkdir -p $DIR/$tdir
1530         rm -f $file
1531         fid=$(make_small $file)
1532
1533         # Create a file and check its states
1534         check_hsm_flags $file "0x00000000"
1535
1536         # Ensure atime is less than mtime and ctime.
1537         sleep 1
1538         echo >> $file
1539
1540         atime0=$(stat -c "%X" $file)
1541         mtime0=$(stat -c "%Y" $file)
1542         ctime0=$(stat -c "%Z" $file)
1543
1544         [ $atime0 -lt $mtime0 ] ||
1545                 error "atime $atime0 is not less than mtime $mtime0"
1546
1547         [ $atime0 -lt $ctime0 ] ||
1548                 error "atime $atime0 is not less than ctime $ctime0"
1549
1550         # Archive should not change any timestamps.
1551         $LFS hsm_archive $file || error "cannot archive '$file'"
1552         wait_request_state $fid ARCHIVE SUCCEED
1553
1554         atime1=$(stat -c "%X" $file)
1555         mtime1=$(stat -c "%Y" $file)
1556         ctime1=$(stat -c "%Z" $file)
1557
1558         [ $atime0 -eq $atime1 ] ||
1559                 error "archive changed atime from $atime0 to $atime1"
1560
1561         [ $mtime0 -eq $mtime1 ] ||
1562                 error "archive changed mtime from $mtime0 to $mtime1"
1563
1564         [ $ctime0 -eq $ctime1 ] ||
1565                 error "archive changed ctime from $ctime0 to $ctime1"
1566
1567         # Release should not change any timestamps.
1568         $LFS hsm_release $file || error "cannot release '$file'"
1569         check_hsm_flags $file "0x0000000d"
1570
1571         atime1=$(stat -c "%X" $file)
1572         mtime1=$(stat -c "%Y" $file)
1573         ctime1=$(stat -c "%Z" $file)
1574
1575         [ $atime0 -eq $atime1 ] ||
1576                 error "release changed atime from $atime0 to $atime1"
1577
1578         [ $mtime0 -eq $mtime1 ] ||
1579                 error "release changed mtime from $mtime0 to $mtime1"
1580
1581         [ $ctime0 -eq $ctime1 ] ||
1582                 error "release changed ctime from $ctime0 to $ctime1"
1583
1584         # Restore should not change atime or mtime and should not
1585         # decrease ctime.
1586         $LFS hsm_restore $file
1587         wait_request_state $fid RESTORE SUCCEED
1588
1589         atime1=$(stat -c "%X" $file)
1590         mtime1=$(stat -c "%Y" $file)
1591         ctime1=$(stat -c "%Z" $file)
1592
1593         [ $atime0 -eq $atime1 ] ||
1594                 error "restore changed atime from $atime0 to $atime1"
1595
1596         [ $mtime0 -eq $mtime1 ] ||
1597                 error "restore changed mtime from $mtime0 to $mtime1"
1598
1599         [ $ctime0 -le $ctime1 ] ||
1600                 error "restore changed ctime from $ctime0 to $ctime1"
1601
1602         copytool_cleanup
1603
1604         # Once more, after unmount and mount.
1605         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1606         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1607
1608         atime1=$(stat -c "%X" $file)
1609         mtime1=$(stat -c "%Y" $file)
1610         ctime1=$(stat -c "%Z" $file)
1611
1612         [ $atime0 -eq $atime1 ] ||
1613                 error "remount changed atime from $atime0 to $atime1"
1614
1615         [ $mtime0 -eq $mtime1 ] ||
1616                 error "remount changed mtime from $mtime0 to $mtime1"
1617
1618         [ $ctime0 -le $ctime1 ] ||
1619                 error "remount changed ctime from $ctime0 to $ctime1"
1620 }
1621 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1622
1623 test_24b() {
1624         local file=$DIR/$tdir/$tfile
1625         local fid
1626         local sum0
1627         local sum1
1628         # LU-3811
1629
1630         # Test needs a running copytool.
1631         copytool_setup
1632         mkdir -p $DIR/$tdir
1633
1634         # Check that root can do HSM actions on a ordinary user's file.
1635         rm -f $file
1636         fid=$(make_small $file)
1637         sum0=$(md5sum $file)
1638
1639         chown $RUNAS_ID:$RUNAS_GID $file ||
1640                 error "cannot chown '$file' to '$RUNAS_ID'"
1641
1642         chmod ugo-w $DIR/$tdir ||
1643                 error "cannot chmod '$DIR/$tdir'"
1644
1645         $LFS hsm_archive $file
1646         wait_request_state $fid ARCHIVE SUCCEED
1647
1648         $LFS hsm_release $file
1649         check_hsm_flags $file "0x0000000d"
1650
1651         $LFS hsm_restore $file
1652         wait_request_state $fid RESTORE SUCCEED
1653
1654         # Check that ordinary user can get HSM state.
1655         $RUNAS $LFS hsm_state $file ||
1656                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1657
1658         $LFS hsm_release $file
1659         check_hsm_flags $file "0x0000000d"
1660
1661         # Check that ordinary user can accessed released file.
1662         sum1=$($RUNAS md5sum $file) ||
1663                 error "user '$RUNAS_ID' cannot read '$file'"
1664
1665         [ "$sum0" == "$sum1" ] ||
1666                 error "md5sum mismatch for '$file'"
1667
1668         copytool_cleanup
1669 }
1670 run_test 24b "root can archive, release, and restore user files"
1671
1672 cleanup_test_24c() {
1673         trap 0
1674         set_hsm_param user_request_mask RESTORE
1675         set_hsm_param group_request_mask RESTORE
1676         set_hsm_param other_request_mask RESTORE
1677 }
1678
1679 test_24c() {
1680         local file=$DIR/$tdir/$tfile
1681         local action=archive
1682         local user_save
1683         local group_save
1684         local other_save
1685
1686         # test needs a running copytool
1687         copytool_setup
1688
1689         mkdir -p $DIR/$tdir
1690
1691         # Save the default masks and check that cleanup_24c will
1692         # restore the request masks correctly.
1693         user_save=$(get_hsm_param user_request_mask)
1694         group_save=$(get_hsm_param group_request_mask)
1695         other_save=$(get_hsm_param other_request_mask)
1696
1697         [ "$user_save" == RESTORE ] ||
1698                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1699         [ "$group_save" == RESTORE ] ||
1700                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1701         [ "$other_save" == RESTORE ] ||
1702                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1703
1704         trap cleanup_test_24c EXIT
1705
1706         # User.
1707         rm -f $file
1708         make_small $file
1709         chown $RUNAS_ID:nobody $file ||
1710                 error "cannot chown '$file' to '$RUNAS_ID:nobody'"
1711
1712         set_hsm_param user_request_mask ""
1713         $RUNAS $LFS hsm_$action $file &&
1714                 error "$action by user should fail"
1715
1716         set_hsm_param user_request_mask $action
1717         $RUNAS $LFS hsm_$action $file ||
1718                 error "$action by user should succeed"
1719
1720         # Group.
1721         rm -f $file
1722         make_small $file
1723         chown nobody:$RUNAS_GID $file ||
1724                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
1725
1726         set_hsm_param group_request_mask ""
1727         $RUNAS $LFS hsm_$action $file &&
1728                 error "$action by group should fail"
1729
1730         set_hsm_param group_request_mask $action
1731         $RUNAS $LFS hsm_$action $file ||
1732                 error "$action by group should succeed"
1733
1734         # Other.
1735         rm -f $file
1736         make_small $file
1737         chown nobody:nobody $file ||
1738                 error "cannot chown '$file' to 'nobody:nobody'"
1739
1740         set_hsm_param other_request_mask ""
1741         $RUNAS $LFS hsm_$action $file &&
1742                 error "$action by other should fail"
1743
1744         set_hsm_param other_request_mask $action
1745         $RUNAS $LFS hsm_$action $file ||
1746                 error "$action by other should succeed"
1747
1748         copytool_cleanup
1749         cleanup_test_24c
1750 }
1751 run_test 24c "check that user,group,other request masks work"
1752
1753 cleanup_test_24d() {
1754         trap 0
1755         mount -o remount,rw $MOUNT2
1756 }
1757
1758 test_24d() {
1759         local file1=$DIR/$tdir/$tfile
1760         local file2=$DIR2/$tdir/$tfile
1761         local fid1
1762         local fid2
1763
1764         copytool_setup
1765
1766         mkdir -p $DIR/$tdir
1767         rm -f $file1
1768         fid1=$(make_small $file1)
1769
1770         trap cleanup_test_24d EXIT
1771
1772         mount -o remount,ro $MOUNT2
1773
1774         fid2=$(path2fid $file2)
1775         [ "$fid1" == "$fid2" ] ||
1776                 error "FID mismatch '$fid1' != '$fid2'"
1777
1778         $LFS hsm_archive $file2 &&
1779                 error "archive should fail on read-only mount"
1780         check_hsm_flags $file1 "0x00000000"
1781
1782         $LFS hsm_archive $file1
1783         wait_request_state $fid1 ARCHIVE SUCCEED
1784
1785         $LFS hsm_release $file1
1786         $LFS hsm_restore $file2
1787         wait_request_state $fid1 RESTORE SUCCEED
1788
1789         $LFS hsm_release $file1 || error "cannot release '$file1'"
1790         dd if=$file2 of=/dev/null bs=1M || "cannot read '$file2'"
1791
1792         $LFS hsm_release $file2 &&
1793                 error "release should fail on read-only mount"
1794
1795         copytool_cleanup
1796         cleanup_test_24d
1797 }
1798 run_test 24d "check that read-only mounts are respected"
1799
1800 test_25a() {
1801         # test needs a running copytool
1802         copytool_setup
1803
1804         mkdir -p $DIR/$tdir
1805         copy2archive /etc/hosts $tdir/$tfile
1806
1807         local f=$DIR/$tdir/$tfile
1808
1809         import_file $tdir/$tfile $f
1810
1811         $LFS hsm_set --lost $f
1812
1813         md5sum $f
1814         local st=$?
1815
1816         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1817
1818         copytool_cleanup
1819 }
1820 run_test 25a "Restore lost file (HS_LOST flag) from import"\
1821              " (Operation not permitted)"
1822
1823 test_25b() {
1824         # test needs a running copytool
1825         copytool_setup
1826
1827         mkdir -p $DIR/$tdir
1828
1829         local f=$DIR/$tdir/$tfile
1830         local fid=$(copy_file /etc/passwd $f)
1831
1832         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1833         wait_request_state $fid ARCHIVE SUCCEED
1834
1835         $LFS hsm_release $f
1836         $LFS hsm_set --lost $f
1837         md5sum $f
1838         st=$?
1839
1840         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1841
1842         copytool_cleanup
1843 }
1844 run_test 25b "Restore lost file (HS_LOST flag) after release"\
1845              " (Operation not permitted)"
1846
1847 test_26() {
1848         # test needs a running copytool
1849         copytool_setup
1850
1851         mkdir -p $DIR/$tdir
1852         local f=$DIR/$tdir/$tfile
1853         local fid=$(make_large_for_progress $f)
1854         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1855         wait_request_state $fid ARCHIVE SUCCEED
1856
1857         $LFS hsm_remove $f
1858         wait_request_state $fid REMOVE SUCCEED
1859
1860         check_hsm_flags $f "0x00000000"
1861
1862         copytool_cleanup
1863 }
1864 run_test 26 "Remove the archive of a valid file"
1865
1866 test_27a() {
1867         # test needs a running copytool
1868         copytool_setup
1869
1870         mkdir -p $DIR/$tdir
1871         make_archive $tdir/$tfile
1872         local f=$DIR/$tdir/$tfile
1873         import_file $tdir/$tfile $f
1874         local fid=$(path2fid $f)
1875
1876         $LFS hsm_remove $f
1877
1878         [[ $? != 0 ]] || error "Remove of a released file should fail"
1879
1880         copytool_cleanup
1881 }
1882 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
1883
1884 test_27b() {
1885         # test needs a running copytool
1886         copytool_setup
1887
1888         mkdir -p $DIR/$tdir
1889         local f=$DIR/$tdir/$tfile
1890         local fid=$(make_large_for_progress $f)
1891         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1892         wait_request_state $fid ARCHIVE SUCCEED
1893         $LFS hsm_release $f
1894
1895         $LFS hsm_remove $f
1896
1897         [[ $? != 0 ]] || error "Remove of a released file should fail"
1898
1899         copytool_cleanup
1900 }
1901 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
1902
1903 test_28() {
1904         # test needs a running copytool
1905         copytool_setup
1906
1907         mkdir -p $DIR/$tdir
1908         local f=$DIR/$tdir/$tfile
1909         local fid=$(make_large_for_progress $f)
1910         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1911         wait_request_state $fid ARCHIVE SUCCEED
1912
1913         cdt_disable
1914         $LFS hsm_remove $f
1915
1916         rm -f $f
1917
1918         cdt_enable
1919
1920         wait_request_state $fid REMOVE SUCCEED
1921
1922         copytool_cleanup
1923 }
1924 run_test 28 "Concurrent archive/file remove"
1925
1926 test_30a() {
1927         # restore at exec cannot work on agent node (because of Linux kernel
1928         # protection of executables)
1929         needclients 2 || return 0
1930
1931         # test needs a running copytool
1932         copytool_setup
1933
1934         mkdir -p $DIR/$tdir
1935         copy2archive /bin/true $tdir/$tfile
1936
1937         local f=$DIR/$tdir/true
1938         import_file $tdir/$tfile $f
1939
1940         local fid=$(path2fid $f)
1941
1942         # set no retry action mode
1943         cdt_set_no_retry
1944         do_node $CLIENT2 $f
1945         local st=$?
1946
1947         # cleanup
1948         # remove no try action mode
1949         cdt_clear_no_retry
1950         $LFS hsm_state $f
1951
1952         [[ $st == 0 ]] || error "Failed to exec a released file"
1953
1954         copytool_cleanup
1955 }
1956 run_test 30a "Restore at exec (import case)"
1957
1958 test_30b() {
1959         # restore at exec cannot work on agent node (because of Linux kernel
1960         # protection of executables)
1961         needclients 2 || return 0
1962
1963         # test needs a running copytool
1964         copytool_setup
1965
1966         mkdir -p $DIR/$tdir
1967         local f=$DIR/$tdir/true
1968         local fid=$(copy_file /bin/true $f)
1969         chmod 755 $f
1970         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1971         wait_request_state $fid ARCHIVE SUCCEED
1972         $LFS hsm_release $f
1973         $LFS hsm_state $f
1974         # set no retry action mode
1975         cdt_set_no_retry
1976         do_node $CLIENT2 $f
1977         local st=$?
1978
1979         # cleanup
1980         # remove no try action mode
1981         cdt_clear_no_retry
1982         $LFS hsm_state $f
1983
1984         [[ $st == 0 ]] || error "Failed to exec a released file"
1985
1986         copytool_cleanup
1987 }
1988 run_test 30b "Restore at exec (release case)"
1989
1990 test_30c() {
1991         needclients 2 || return 0
1992
1993         # test needs a running copytool
1994         copytool_setup
1995
1996         mkdir -p $DIR/$tdir
1997         local f=$DIR/$tdir/SLEEP
1998         local fid=$(copy_file /bin/sleep $f)
1999         chmod 755 $f
2000         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2001         wait_request_state $fid ARCHIVE SUCCEED
2002         $LFS hsm_release $f
2003         check_hsm_flags $f "0x0000000d"
2004         # set no retry action mode
2005         cdt_set_no_retry
2006         do_node $CLIENT2 "$f 10" &
2007         local pid=$!
2008         sleep 3
2009         echo 'Hi!' > $f
2010         [[ $? == 0 ]] && error "Update during exec of released file must fail"
2011         wait $pid
2012         [[ $? == 0 ]] || error "Execution failed during run"
2013         cmp /bin/sleep $f
2014         [[ $? == 0 ]] || error "Binary overwritten during exec"
2015
2016         # cleanup
2017         # remove no try action mode
2018         cdt_clear_no_retry
2019         check_hsm_flags $f "0x00000009"
2020
2021         copytool_cleanup
2022 }
2023 run_test 30c "Update during exec of released file must fail"
2024
2025 restore_and_check_size() {
2026         local f=$1
2027         local fid=$2
2028         local s=$(stat -c "%s" $f)
2029         local n=$s
2030         local st=$(get_hsm_flags $f)
2031         local err=0
2032         local cpt=0
2033         $LFS hsm_restore $f
2034         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
2035         do
2036                 n=$(stat -c "%s" $f)
2037                 # we echo in both cases to show stat is not
2038                 # hang
2039                 if [[ $n != $s ]]; then
2040                         echo "size seen is $n != $s"
2041                         err=1
2042                 else
2043                         echo "size seen is right: $n == $s"
2044                 fi
2045                 st=$(get_hsm_flags $f)
2046                 sleep 10
2047                 cpt=$((cpt + 1))
2048         done
2049         if [[ $cpt -lt 10 ]]; then
2050                 echo " "done
2051         else
2052                 echo " restore is too long"
2053                 wait_request_state $fid RESTORE SUCCEED
2054         fi
2055         return $err
2056 }
2057
2058 test_31a() {
2059         # test needs a running copytool
2060         copytool_setup
2061
2062         mkdir -p $DIR/$tdir
2063
2064         make_archive $tdir/$tfile
2065         local f=$DIR/$tdir/$tfile
2066         import_file $tdir/$tfile $f
2067         local fid=$($LFS path2fid $f)
2068         HSM_ARCHIVE_PURGE=false copytool_setup
2069
2070         restore_and_check_size $f $fid
2071         local err=$?
2072
2073         [[ $err -eq 0 ]] || error "File size changed during restore"
2074
2075         copytool_cleanup
2076 }
2077 run_test 31a "Import a large file and check size during restore"
2078
2079
2080 test_31b() {
2081         # test needs a running copytool
2082         copytool_setup
2083
2084         mkdir -p $DIR/$tdir
2085
2086         local f=$DIR/$tdir/$tfile
2087         local fid=$(make_large_for_progress $f)
2088         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2089         wait_request_state $fid ARCHIVE SUCCEED
2090         $LFS hsm_release $f
2091
2092         restore_and_check_size $f $fid
2093         local err=$?
2094
2095         [[ $err -eq 0 ]] || error "File size changed during restore"
2096
2097         copytool_cleanup
2098 }
2099 run_test 31b "Restore a large unaligned file and check size during restore"
2100
2101 test_31c() {
2102         # test needs a running copytool
2103         copytool_setup
2104
2105         mkdir -p $DIR/$tdir
2106
2107         local f=$DIR/$tdir/$tfile
2108         local fid=$(make_large_for_progress_aligned $f)
2109         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2110         wait_request_state $fid ARCHIVE SUCCEED
2111         $LFS hsm_release $f
2112
2113         restore_and_check_size $f $fid
2114         local err=$?
2115
2116         [[ $err -eq 0 ]] || error "File size changed during restore"
2117
2118         copytool_cleanup
2119 }
2120 run_test 31c "Restore a large aligned file and check size during restore"
2121
2122 test_33() {
2123         # test needs a running copytool
2124         copytool_setup
2125
2126         mkdir -p $DIR/$tdir
2127
2128         local f=$DIR/$tdir/$tfile
2129         local fid=$(make_large_for_progress $f)
2130         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2131         wait_request_state $fid ARCHIVE SUCCEED
2132         $LFS hsm_release $f
2133
2134         md5sum $f >/dev/null &
2135         local pid=$!
2136         wait_request_state $fid RESTORE STARTED
2137
2138         kill -15 $pid
2139         sleep 1
2140
2141         # Check restore trigger process was killed
2142         local killed=$(ps -o pid,comm hp $pid >/dev/null)
2143
2144         $LFS hsm_cancel $f
2145
2146         wait_request_state $fid RESTORE CANCELED
2147         wait_request_state $fid CANCEL SUCCEED
2148
2149         [ -z $killed ] ||
2150                 error "Cannot kill process waiting for restore ($killed)"
2151
2152         copytool_cleanup
2153 }
2154 run_test 33 "Kill a restore waiting process"
2155
2156 test_34() {
2157         # test needs a running copytool
2158         copytool_setup
2159
2160         mkdir -p $DIR/$tdir
2161
2162         local f=$DIR/$tdir/$tfile
2163         local fid=$(make_large_for_progress $f)
2164         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2165         wait_request_state $fid ARCHIVE SUCCEED
2166         $LFS hsm_release $f
2167
2168         md5sum $f >/dev/null &
2169         local pid=$!
2170         wait_request_state $fid RESTORE STARTED
2171
2172         rm $f || error "rm $f failed"
2173         # rm must not block during restore
2174         wait_request_state $fid RESTORE STARTED
2175
2176         wait_request_state $fid RESTORE SUCCEED
2177         # check md5sum pgm finished
2178         local there=$(ps -o pid,comm hp $pid >/dev/null)
2179         [[ -z $there ]] || error "Restore initiator does not exit"
2180
2181         local rc=$(wait $pid)
2182         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2183
2184         copytool_cleanup
2185 }
2186 run_test 34 "Remove file during restore"
2187
2188 test_35() {
2189         # test needs a running copytool
2190         copytool_setup
2191
2192         mkdir -p $DIR/$tdir
2193
2194         local f=$DIR/$tdir/$tfile
2195         local f1=$DIR/$tdir/$tfile-1
2196         local fid=$(make_large_for_progress $f)
2197         local fid1=$(copy_file /etc/passwd $f1)
2198         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2199         wait_request_state $fid ARCHIVE SUCCEED
2200         $LFS hsm_release $f
2201
2202         md5sum $f >/dev/null &
2203         local pid=$!
2204         wait_request_state $fid RESTORE STARTED
2205
2206         mv $f1 $f || error "mv $f1 $f failed"
2207         # mv must not block during restore
2208         wait_request_state $fid RESTORE STARTED
2209
2210         wait_request_state $fid RESTORE SUCCEED
2211         # check md5sum pgm finished
2212         local there=$(ps -o pid,comm hp $pid >/dev/null)
2213         [[ -z $there ]] || error "Restore initiator does not exit"
2214
2215         local rc=$(wait $pid)
2216         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2217
2218         fid2=$(path2fid $f)
2219         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2220
2221         copytool_cleanup
2222 }
2223 run_test 35 "Overwrite file during restore"
2224
2225 test_36() {
2226         # test needs a running copytool
2227         copytool_setup
2228
2229         mkdir -p $DIR/$tdir
2230
2231         local f=$DIR/$tdir/$tfile
2232         local fid=$(make_large_for_progress $f)
2233         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2234         wait_request_state $fid ARCHIVE SUCCEED
2235         $LFS hsm_release $f
2236
2237         md5sum $f >/dev/null &
2238         local pid=$!
2239         wait_request_state $fid RESTORE STARTED
2240
2241         mv $f $f.new
2242         # rm must not block during restore
2243         wait_request_state $fid RESTORE STARTED
2244
2245         wait_request_state $fid RESTORE SUCCEED
2246         # check md5sum pgm finished
2247         local there=$(ps -o pid,comm hp $pid >/dev/null)
2248         [[ -z $there ]] ||
2249                 error "Restore initiator does not exit"
2250
2251         local rc=$(wait $pid)
2252         [[ $rc -eq 0 ]] ||
2253                 error "Restore initiator failed with $rc"
2254
2255         copytool_cleanup
2256 }
2257 run_test 36 "Move file during restore"
2258
2259 multi_archive() {
2260         local prefix=$1
2261         local count=$2
2262         local n=""
2263
2264         for n in $(seq 1 $count); do
2265                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2266         done
2267         echo "$count archive requests submitted"
2268 }
2269
2270 test_40() {
2271         local stream_count=4
2272         local file_count=100
2273         mkdir -p $DIR/$tdir
2274         local f=$DIR/$tdir/$tfile
2275         local i=""
2276         local p=""
2277         local fid=""
2278
2279         for i in $(seq 1 $file_count); do
2280                 for p in $(seq 1 $stream_count); do
2281                         fid=$(copy_file /etc/hosts $f.$p.$i)
2282                 done
2283         done
2284         # force copytool to use a local/temp archive dir to ensure best
2285         # performance vs remote/NFS mounts used in auto-tests
2286         if df --local $HSM_ARCHIVE >/dev/null 2>&1 ; then
2287                 copytool_setup
2288         else
2289                 copytool_setup $SINGLEAGT $MOUNT $HSM_ARCHIVE_NUMBER $TMP/$tdir
2290         fi
2291         # to be sure wait_all_done will not be mislead by previous tests
2292         cdt_purge
2293         wait_for_grace_delay
2294         typeset -a pids
2295         # start archive streams in background (archive files in parallel)
2296         for p in $(seq 1 $stream_count); do
2297                 multi_archive $f.$p $file_count &
2298                 pids[$p]=$!
2299         done
2300         echo -n  "Wait for all requests being enqueued..."
2301         wait ${pids[*]}
2302         echo OK
2303         wait_all_done 100
2304         copytool_cleanup
2305 }
2306 run_test 40 "Parallel archive requests"
2307
2308 test_52() {
2309         # test needs a running copytool
2310         copytool_setup
2311
2312         mkdir -p $DIR/$tdir
2313         local f=$DIR/$tdir/$tfile
2314         local fid=$(copy_file /etc/motd $f 1)
2315
2316         $LFS hsm_archive $f || error "could not archive file"
2317         wait_request_state $fid ARCHIVE SUCCEED
2318         check_hsm_flags $f "0x00000009"
2319
2320         multiop_bg_pause $f O_c || error "multiop failed"
2321         local MULTIPID=$!
2322
2323         mds_evict_client
2324         client_up || client_up || true
2325
2326         kill -USR1 $MULTIPID
2327         wait $MULTIPID || error "multiop close failed"
2328
2329         check_hsm_flags $f "0x0000000b"
2330
2331         copytool_cleanup
2332 }
2333 run_test 52 "Opened for write file on an evicted client should be set dirty"
2334
2335 test_53() {
2336         # test needs a running copytool
2337         copytool_setup
2338
2339         mkdir -p $DIR/$tdir
2340         local f=$DIR/$tdir/$tfile
2341         local fid=$(copy_file /etc/motd $f 1)
2342
2343         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2344                 error "could not archive file"
2345         wait_request_state $fid ARCHIVE SUCCEED
2346         check_hsm_flags $f "0x00000009"
2347
2348         multiop_bg_pause $f o_c || error "multiop failed"
2349         MULTIPID=$!
2350
2351         mds_evict_client
2352         client_up || client_up || true
2353
2354         kill -USR1 $MULTIPID
2355         wait $MULTIPID || error "multiop close failed"
2356
2357         check_hsm_flags $f "0x00000009"
2358
2359         copytool_cleanup
2360 }
2361 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2362
2363 test_54() {
2364         # test needs a running copytool
2365         copytool_setup
2366
2367         mkdir -p $DIR/$tdir
2368         local f=$DIR/$tdir/$tfile
2369         local fid=$(make_small $f)
2370
2371         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2372                 error "could not archive file"
2373         wait_request_state $fid ARCHIVE STARTED
2374
2375         check_hsm_flags $f "0x00000001"
2376
2377         # Avoid coordinator resending this request as soon it has failed.
2378         cdt_set_no_retry
2379
2380         echo "foo" >> $f
2381         sync
2382         wait_request_state $fid ARCHIVE FAILED
2383
2384         check_hsm_flags $f "0x00000003"
2385
2386         cdt_clear_no_retry
2387         copytool_cleanup
2388 }
2389 run_test 54 "Write during an archive cancels it"
2390
2391 test_55() {
2392         # test needs a running copytool
2393         copytool_setup
2394
2395         mkdir -p $DIR/$tdir
2396         local f=$DIR/$tdir/$tfile
2397         local fid=$(make_small $f)
2398
2399         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2400                 error "could not archive file"
2401         wait_request_state $fid ARCHIVE STARTED
2402
2403         check_hsm_flags $f "0x00000001"
2404
2405         # Avoid coordinator resending this request as soon it has failed.
2406         cdt_set_no_retry
2407
2408         $TRUNCATE $f 1024 || error "truncate failed"
2409         sync
2410         wait_request_state $fid ARCHIVE FAILED
2411
2412         check_hsm_flags $f "0x00000003"
2413
2414         cdt_clear_no_retry
2415         copytool_cleanup
2416 }
2417 run_test 55 "Truncate during an archive cancels it"
2418
2419 test_56() {
2420         # test needs a running copytool
2421         copytool_setup
2422
2423         mkdir -p $DIR/$tdir
2424         local f=$DIR/$tdir/$tfile
2425         local fid=$(make_large_for_progress $f)
2426
2427         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2428                 error "could not archive file"
2429         wait_request_state $fid ARCHIVE STARTED
2430
2431         check_hsm_flags $f "0x00000001"
2432
2433         # Change metadata and sync to be sure we are not changing only
2434         # in memory.
2435         chmod 644 $f
2436         chgrp sys $f
2437         sync
2438         wait_request_state $fid ARCHIVE SUCCEED
2439
2440         check_hsm_flags $f "0x00000009"
2441
2442         copytool_cleanup
2443 }
2444 run_test 56 "Setattr during an archive is ok"
2445
2446 test_57() {
2447         # Need one client for I/O, one for request
2448         needclients 2 || return 0
2449
2450         # test needs a running copytool
2451         copytool_setup
2452
2453         mkdir -p $DIR/$tdir
2454         local f=$DIR/$tdir/test_archive_remote
2455         # Create a file on a remote node
2456         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2457                 "count=2 conv=fsync"
2458
2459         # And archive it
2460         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2461                 error "hsm_archive failed"
2462         local fid=$(path2fid $f)
2463         wait_request_state $fid ARCHIVE SUCCEED
2464
2465         # Release and implicit restore it
2466         do_node $CLIENT2 "$LFS hsm_release $f" ||
2467                 error "hsm_release failed"
2468         do_node $CLIENT2 "md5sum $f" ||
2469                 error "hsm_restore failed"
2470
2471         wait_request_state $fid RESTORE SUCCEED
2472
2473         copytool_cleanup
2474 }
2475 run_test 57 "Archive a file with dirty cache on another node"
2476
2477 truncate_released_file() {
2478         local src_file=$1
2479         local trunc_to=$2
2480
2481         local sz=$(stat -c %s $src_file)
2482         local f=$DIR/$tdir/$tfile
2483         local fid=$(copy_file $1 $f)
2484         local ref=$f-ref
2485         cp $f $f-ref
2486
2487         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2488                 error "could not archive file"
2489         wait_request_state $fid ARCHIVE SUCCEED
2490
2491         $LFS hsm_release $f || error "could not release file"
2492
2493         $TRUNCATE $f $trunc_to || error "truncate failed"
2494         sync
2495
2496         local sz1=$(stat -c %s $f)
2497         [[ $sz1 == $trunc_to ]] ||
2498                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2499
2500         $LFS hsm_state $f
2501         check_hsm_flags $f "0x0000000b"
2502
2503         local state=$(get_request_state $fid RESTORE)
2504         [[ "$state" == "SUCCEED" ]] ||
2505                 error "truncate $sz does not trig restore, state = $state"
2506
2507         $TRUNCATE $ref $trunc_to
2508         cmp $ref $f || error "file data wrong after truncate"
2509
2510         rm -f $f $f-ref
2511 }
2512
2513 test_58() {
2514         # test needs a running copytool
2515         copytool_setup
2516
2517         mkdir -p $DIR/$tdir
2518
2519         local sz=$(stat -c %s /etc/passwd)
2520
2521         echo "truncate up from $sz to $((sz*2))"
2522         truncate_released_file /etc/passwd $((sz*2))
2523
2524         echo "truncate down from $sz to $((sz/2))"
2525         truncate_released_file /etc/passwd $((sz/2))
2526
2527         echo "truncate to 0"
2528         truncate_released_file /etc/passwd 0
2529
2530         copytool_cleanup
2531 }
2532 run_test 58 "Truncate a released file will trigger restore"
2533
2534 test_90() {
2535         file_count=57
2536         mkdir -p $DIR/$tdir
2537         local f=$DIR/$tdir/$tfile
2538         local FILELIST=/tmp/filelist.txt
2539         local i=""
2540
2541         rm -f $FILELIST
2542         for i in $(seq 1 $file_count); do
2543                 fid=$(copy_file /etc/hosts $f.$i)
2544                 echo $f.$i >> $FILELIST
2545         done
2546         copytool_setup
2547         # to be sure wait_all_done will not be mislead by previous tests
2548         cdt_purge
2549         wait_for_grace_delay
2550         $LFS hsm_archive --filelist $FILELIST ||
2551                 error "cannot archive a file list"
2552         wait_all_done 100
2553         $LFS hsm_release --filelist $FILELIST ||
2554                 error "cannot release a file list"
2555         $LFS hsm_restore --filelist $FILELIST ||
2556                 error "cannot restore a file list"
2557         wait_all_done 100
2558         copytool_cleanup
2559 }
2560 run_test 90 "Archive/restore a file list"
2561
2562 double_verify_reset_hsm_param() {
2563         local p=$1
2564         echo "Testing $HSM_PARAM.$p"
2565         local val=$(get_hsm_param $p)
2566         local save=$val
2567         local val2=$(($val * 2))
2568         set_hsm_param $p $val2
2569         val=$(get_hsm_param $p)
2570         [[ $val == $val2 ]] ||
2571                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
2572         echo "Set $p to 0 must failed"
2573         set_hsm_param $p 0
2574         local rc=$?
2575         # restore value
2576         set_hsm_param $p $save
2577
2578         if [[ $rc == 0 ]]; then
2579                 error "we must not be able to set $HSM_PARAM.$p to 0"
2580         fi
2581 }
2582
2583 test_100() {
2584         double_verify_reset_hsm_param loop_period
2585         double_verify_reset_hsm_param grace_delay
2586         double_verify_reset_hsm_param active_request_timeout
2587         double_verify_reset_hsm_param max_requests
2588         double_verify_reset_hsm_param default_archive_id
2589 }
2590 run_test 100 "Set coordinator /proc tunables"
2591
2592 test_102() {
2593         cdt_disable
2594         cdt_enable
2595         cdt_restart
2596 }
2597 run_test 102 "Verify coordinator control"
2598
2599 test_103() {
2600         # test needs a running copytool
2601         copytool_setup
2602
2603         local i=""
2604         local fid=""
2605
2606         mkdir -p $DIR/$tdir
2607         for i in $(seq 1 20); do
2608                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
2609         done
2610         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
2611
2612         cdt_purge
2613
2614         echo "Current requests"
2615         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2616                         $HSM_PARAM.actions |\
2617                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
2618
2619         [[ -z "$res" ]] || error "Some request have not been canceled"
2620
2621         copytool_cleanup
2622 }
2623 run_test 103 "Purge all requests"
2624
2625 DATA=CEA
2626 DATAHEX='[434541]'
2627 test_104() {
2628         # test needs a running copytool
2629         copytool_setup
2630
2631         mkdir -p $DIR/$tdir
2632         local f=$DIR/$tdir/$tfile
2633         local fid=$(make_large_for_progress $f)
2634         # if cdt is on, it can serve too quickly the request
2635         cdt_disable
2636         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
2637         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2638                         $HSM_PARAM.actions |\
2639                         grep $fid | cut -f16 -d=")
2640         cdt_enable
2641
2642         [[ "$data1" == "$DATAHEX" ]] ||
2643                 error "Data field in records is ($data1) and not ($DATAHEX)"
2644
2645         copytool_cleanup
2646 }
2647 run_test 104 "Copy tool data field"
2648
2649 test_105() {
2650         mkdir -p $DIR/$tdir
2651         local i=""
2652
2653         cdt_disable
2654         for i in $(seq -w 1 10); do
2655                 cp /etc/passwd $DIR/$tdir/$i
2656                 $LFS hsm_archive $DIR/$tdir/$i
2657         done
2658         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2659                         $HSM_PARAM.actions |\
2660                         grep WAITING | wc -l")
2661         cdt_restart
2662         cdt_disable
2663         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2664                         $HSM_PARAM.actions |\
2665                         grep WAITING | wc -l")
2666         cdt_enable
2667         cdt_purge
2668         [[ "$reqcnt1" == "$reqcnt2" ]] ||
2669                 error "Requests count after shutdown $reqcnt2 != "\
2670                       "before shutdown $reqcnt1"
2671 }
2672 run_test 105 "Restart of coordinator"
2673
2674 get_agent_by_uuid_mdt() {
2675         local uuid=$1
2676         local mdtidx=$2
2677         local mds=mds$(($mdtidx + 1))
2678         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
2679                  grep $uuid"
2680 }
2681
2682 check_agent_registered_by_mdt() {
2683         local uuid=$1
2684         local mdtidx=$2
2685         local mds=mds$(($mdtidx + 1))
2686         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2687         if [[ ! -z "$agent" ]]; then
2688                 echo "found agent $agent on $mds"
2689         else
2690                 error "uuid $uuid not found in agent list on $mds"
2691         fi
2692 }
2693
2694 check_agent_unregistered_by_mdt() {
2695         local uuid=$1
2696         local mdtidx=$2
2697         local mds=mds$(($mdtidx + 1))
2698         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2699         if [[ -z "$agent" ]]; then
2700                 echo "uuid not found in agent list on $mds"
2701         else
2702                 error "uuid found in agent list on $mds: $agent"
2703         fi
2704 }
2705
2706 check_agent_registered() {
2707         local uuid=$1
2708         local mdsno
2709         for mdsno in $(seq 1 $MDSCOUNT); do
2710                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
2711         done
2712 }
2713
2714 check_agent_unregistered() {
2715         local uuid=$1
2716         local mdsno
2717         for mdsno in $(seq 1 $MDSCOUNT); do
2718                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
2719         done
2720 }
2721
2722 test_106() {
2723         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
2724                 get_client_uuid $MOUNT | cut -d' ' -f2)
2725
2726         copytool_setup
2727         check_agent_registered $uuid
2728
2729         search_copytools || error "No copytool found"
2730
2731         copytool_cleanup
2732         check_agent_unregistered $uuid
2733
2734         copytool_setup
2735         check_agent_registered $uuid
2736
2737         copytool_cleanup
2738 }
2739 run_test 106 "Copytool register/unregister"
2740
2741 test_107() {
2742         # test needs a running copytool
2743         copytool_setup
2744         # create and archive file
2745         mkdir -p $DIR/$tdir
2746         local f1=$DIR/$tdir/$tfile
2747         local fid=$(copy_file /etc/passwd $f1)
2748         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2749         wait_request_state $fid ARCHIVE SUCCEED
2750         # shutdown and restart MDS
2751         fail $SINGLEMDS
2752         # check the copytool still gets messages from MDT
2753         local f2=$DIR/$tdir/2
2754         local fid=$(copy_file /etc/passwd $f2)
2755         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2756         # main check of this sanity: this request MUST succeed
2757         wait_request_state $fid ARCHIVE SUCCEED
2758         copytool_cleanup
2759 }
2760 run_test 107 "Copytool re-register after MDS restart"
2761
2762 policy_set_and_test()
2763 {
2764         local change="$1"
2765         local target="$2"
2766         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
2767         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2768         [[ "$policy" == "$target" ]] ||
2769                 error "Wrong policy after '$change': '$policy' != '$target'"
2770 }
2771
2772 test_109() {
2773         # to force default policy setting if error
2774         CDT_POLICY_HAD_CHANGED=true
2775
2776         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2777         local default="NonBlockingRestore [NoRetryAction]"
2778         [[ "$policy" == "$default" ]] ||
2779                 error "default policy has changed,"\
2780                       " '$policy' != '$default' update the test"
2781         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
2782         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
2783         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
2784         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
2785         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
2786         # useless bacause we know but safer for futur changes to use real value
2787         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2788         echo "Next set_param must failed"
2789         policy_set_and_test "wrong" "$policy"
2790
2791         # return to default
2792         echo "Back to default policy"
2793         cdt_set_sanity_policy
2794 }
2795 run_test 109 "Policy display/change"
2796
2797 test_110a() {
2798         # test needs a running copytool
2799         copytool_setup
2800
2801         mkdir -p $DIR/$tdir
2802
2803         copy2archive /etc/passwd $tdir/$tfile
2804
2805         local f=$DIR/$tdir/$tfile
2806         import_file $tdir/$tfile $f
2807         local fid=$(path2fid $f)
2808
2809         cdt_set_non_blocking_restore
2810         md5sum $f
2811         local st=$?
2812
2813         # cleanup
2814         wait_request_state $fid RESTORE SUCCEED
2815         cdt_clear_non_blocking_restore
2816
2817         # Test result
2818         [[ $st == 1 ]] ||
2819                 error "md5sum returns $st != 1, "\
2820                         "should also perror ENODATA (No data available)"
2821
2822         copytool_cleanup
2823 }
2824 run_test 110a "Non blocking restore policy (import case)"
2825
2826 test_110b() {
2827         # test needs a running copytool
2828         copytool_setup
2829
2830         mkdir -p $DIR/$tdir
2831         local f=$DIR/$tdir/$tfile
2832         local fid=$(copy_file /etc/passwd $f)
2833         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2834         wait_request_state $fid ARCHIVE SUCCEED
2835         $LFS hsm_release $f
2836
2837         cdt_set_non_blocking_restore
2838         md5sum $f
2839         local st=$?
2840
2841         # cleanup
2842         wait_request_state $fid RESTORE SUCCEED
2843         cdt_clear_non_blocking_restore
2844
2845         # Test result
2846         [[ $st == 1 ]] ||
2847                 error "md5sum returns $st != 1, "\
2848                         "should also perror ENODATA (No data available)"
2849
2850         copytool_cleanup
2851 }
2852 run_test 110b "Non blocking restore policy (release case)"
2853
2854 test_111a() {
2855         # test needs a running copytool
2856         copytool_setup
2857
2858         mkdir -p $DIR/$tdir
2859         copy2archive /etc/passwd $tdir/$tfile
2860
2861         local f=$DIR/$tdir/$tfile
2862
2863         import_file $tdir/$tfile $f
2864         local fid=$(path2fid $f)
2865
2866         cdt_set_no_retry
2867
2868         copytool_remove_backend $fid
2869
2870         $LFS hsm_restore $f
2871         wait_request_state $fid RESTORE FAILED
2872         local st=$?
2873
2874         # cleanup
2875         cdt_clear_no_retry
2876
2877         # Test result
2878         [[ $st == 0 ]] || error "Restore does not failed"
2879
2880         copytool_cleanup
2881 }
2882 run_test 111a "No retry policy (import case), restore will error"\
2883               " (No such file or directory)"
2884
2885 test_111b() {
2886         # test needs a running copytool
2887         copytool_setup
2888
2889         mkdir -p $DIR/$tdir
2890         local f=$DIR/$tdir/$tfile
2891         local fid=$(copy_file /etc/passwd $f)
2892         cdt_set_no_retry
2893         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2894         wait_request_state $fid ARCHIVE SUCCEED
2895         $LFS hsm_release $f
2896
2897         copytool_remove_backend $fid
2898
2899         $LFS hsm_restore $f
2900         wait_request_state $fid RESTORE FAILED
2901         local st=$?
2902
2903         # cleanup
2904         cdt_clear_no_retry
2905
2906         # Test result
2907         [[ $st == 0 ]] || error "Restore does not failed"
2908
2909         copytool_cleanup
2910 }
2911 run_test 111b "No retry policy (release case), restore will error"\
2912               " (No such file or directory)"
2913
2914 test_112() {
2915         # test needs a running copytool
2916         copytool_setup
2917
2918         mkdir -p $DIR/$tdir
2919         local f=$DIR/$tdir/$tfile
2920         local fid=$(copy_file /etc/passwd $f)
2921         cdt_disable
2922         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2923         local l=$($LFS hsm_action $f)
2924         echo $l
2925         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
2926
2927         # cleanup
2928         cdt_enable
2929         wait_request_state $fid ARCHIVE SUCCEED
2930
2931         # Test result
2932         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
2933
2934         copytool_cleanup
2935 }
2936 run_test 112 "State of recorded request"
2937
2938 test_200() {
2939         # test needs a running copytool
2940         copytool_setup
2941
2942         mkdir -p $DIR/$tdir
2943         local f=$DIR/$tdir/$tfile
2944         local fid=$(make_large_for_cancel $f)
2945         # test with cdt on is made in test_221
2946         cdt_disable
2947         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2948         $LFS hsm_cancel $f
2949         cdt_enable
2950         wait_request_state $fid ARCHIVE CANCELED
2951         wait_request_state $fid CANCEL SUCCEED
2952
2953         copytool_cleanup
2954 }
2955 run_test 200 "Register/Cancel archive"
2956
2957 test_201() {
2958         # test needs a running copytool
2959         copytool_setup
2960
2961         mkdir -p $DIR/$tdir
2962         local f=$DIR/$tdir/$tfile
2963         make_archive $tdir/$tfile
2964         import_file $tdir/$tfile $f
2965         local fid=$(path2fid $f)
2966
2967         # test with cdt on is made in test_222
2968         cdt_disable
2969         $LFS hsm_restore $f
2970         $LFS hsm_cancel $f
2971         cdt_enable
2972         wait_request_state $fid RESTORE CANCELED
2973         wait_request_state $fid CANCEL SUCCEED
2974
2975         copytool_cleanup
2976 }
2977 run_test 201 "Register/Cancel restore"
2978
2979 test_202() {
2980         # test needs a running copytool
2981         copytool_setup
2982
2983         mkdir -p $DIR/$tdir
2984         local f=$DIR/$tdir/$tfile
2985         local fid=$(make_large_for_progress $f)
2986         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2987         wait_request_state $fid ARCHIVE SUCCEED
2988
2989         cdt_disable
2990         $LFS hsm_remove $f
2991         $LFS hsm_cancel $f
2992         cdt_enable
2993         wait_request_state $fid REMOVE CANCELED
2994
2995         copytool_cleanup
2996 }
2997 run_test 202 "Register/Cancel remove"
2998
2999 test_220() {
3000         # test needs a running copytool
3001         copytool_setup
3002
3003         mkdir -p $DIR/$tdir
3004
3005         local f=$DIR/$tdir/$tfile
3006         local fid=$(copy_file /etc/passwd $f)
3007
3008         changelog_setup
3009
3010         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3011         wait_request_state $fid ARCHIVE SUCCEED
3012
3013         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3014         changelog_cleanup
3015
3016         local target=0x0
3017         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3018
3019         copytool_cleanup
3020 }
3021 run_test 220 "Changelog for archive"
3022
3023 test_221() {
3024         # test needs a running copytool
3025         copytool_setup
3026
3027         mkdir -p $DIR/$tdir
3028
3029         local f=$DIR/$tdir/$tfile
3030         local fid=$(make_large_for_cancel $f)
3031
3032         changelog_setup
3033
3034         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3035         wait_request_state $fid ARCHIVE STARTED
3036         $LFS hsm_cancel $f
3037         wait_request_state $fid ARCHIVE CANCELED
3038         wait_request_state $fid CANCEL SUCCEED
3039
3040         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3041
3042         local target=0x7d
3043         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3044
3045         cleanup
3046 }
3047 run_test 221 "Changelog for archive canceled"
3048
3049 test_222a() {
3050         # test needs a running copytool
3051         copytool_setup
3052
3053         mkdir -p $DIR/$tdir
3054         copy2archive /etc/passwd $tdir/$tfile
3055
3056         local f=$DIR/$tdir/$tfile
3057         import_file $tdir/$tfile $f
3058         local fid=$(path2fid $f)
3059
3060         changelog_setup
3061
3062         $LFS hsm_restore $f
3063         wait_request_state $fid RESTORE SUCCEED
3064
3065         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3066
3067         local target=0x80
3068         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3069
3070         cleanup
3071 }
3072 run_test 222a "Changelog for explicit restore"
3073
3074 test_222b() {
3075         # test needs a running copytool
3076         copytool_setup
3077
3078         mkdir -p $DIR/$tdir
3079         local f=$DIR/$tdir/$tfile
3080         local fid=$(copy_file /etc/passwd $f)
3081
3082         changelog_setup
3083         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3084         wait_request_state $fid ARCHIVE SUCCEED
3085         $LFS hsm_release $f
3086
3087         md5sum $f
3088
3089         wait_request_state $fid RESTORE SUCCEED
3090
3091         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3092
3093         local target=0x80
3094         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3095
3096         cleanup
3097 }
3098 run_test 222b "Changelog for implicit restore"
3099
3100 test_223a() {
3101         # test needs a running copytool
3102         copytool_setup
3103
3104         mkdir -p $DIR/$tdir
3105
3106         local f=$DIR/$tdir/$tfile
3107         make_archive $tdir/$tfile
3108
3109         changelog_setup
3110
3111         import_file $tdir/$tfile $f
3112         local fid=$(path2fid $f)
3113
3114         $LFS hsm_restore $f
3115         wait_request_state $fid RESTORE STARTED
3116         $LFS hsm_cancel $f
3117         wait_request_state $fid RESTORE CANCELED
3118         wait_request_state $fid CANCEL SUCCEED
3119
3120         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3121
3122         local target=0xfd
3123         [[ $flags == $target ]] ||
3124                 error "Changelog flag is $flags not $target"
3125
3126         cleanup
3127 }
3128 run_test 223a "Changelog for restore canceled (import case)"
3129
3130 test_223b() {
3131         # test needs a running copytool
3132         copytool_setup
3133
3134         mkdir -p $DIR/$tdir
3135
3136         local f=$DIR/$tdir/$tfile
3137         local fid=$(make_large_for_progress $f)
3138
3139         changelog_setup
3140         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3141         wait_request_state $fid ARCHIVE SUCCEED
3142         $LFS hsm_release $f
3143         $LFS hsm_restore $f
3144         wait_request_state $fid RESTORE STARTED
3145         $LFS hsm_cancel $f
3146         wait_request_state $fid RESTORE CANCELED
3147         wait_request_state $fid CANCEL SUCCEED
3148
3149         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3150
3151         local target=0xfd
3152         [[ $flags == $target ]] ||
3153                 error "Changelog flag is $flags not $target"
3154
3155         cleanup
3156 }
3157 run_test 223b "Changelog for restore canceled (release case)"
3158
3159 test_224() {
3160         # test needs a running copytool
3161         copytool_setup
3162
3163         mkdir -p $DIR/$tdir
3164
3165         local f=$DIR/$tdir/$tfile
3166         local fid=$(copy_file /etc/passwd $f)
3167
3168         changelog_setup
3169         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3170         wait_request_state $fid ARCHIVE SUCCEED
3171
3172         $LFS hsm_remove $f
3173         wait_request_state $fid REMOVE SUCCEED
3174
3175         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
3176
3177         local target=0x200
3178         [[ $flags == $target ]] ||
3179                 error "Changelog flag is $flags not $target"
3180
3181         cleanup
3182 }
3183 run_test 224 "Changelog for remove"
3184
3185 test_225() {
3186         # test needs a running copytool
3187         copytool_setup
3188
3189         # test is not usable because remove request is too fast
3190         # so it is always finished before cancel can be done ...
3191         echo "Test disabled"
3192         copytool_cleanup
3193         return 0
3194
3195         mkdir -p $DIR/$tdir
3196         local f=$DIR/$tdir/$tfile
3197         local fid=$(make_large_for_progress $f)
3198
3199         changelog_setup
3200         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3201         wait_request_state $fid ARCHIVE SUCCEED
3202
3203         # if cdt is on, it can serve too quickly the request
3204         cdt_disable
3205         $LFS hsm_remove $f
3206         $LFS hsm_cancel $f
3207         cdt_enable
3208         wait_request_state $fid REMOVE CANCELED
3209         wait_request_state $fid CANCEL SUCCEED
3210
3211         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3212         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
3213                 tail -n 1 | awk '{print $5}')
3214
3215         local target=0x27d
3216         [[ $flags == $target ]] ||
3217                 error "Changelog flag is $flags not $target"
3218
3219         cleanup
3220 }
3221 run_test 225 "Changelog for remove canceled"
3222
3223 test_226() {
3224         # test needs a running copytool
3225         copytool_setup
3226
3227         mkdir -p $DIR/$tdir
3228
3229         local f1=$DIR/$tdir/$tfile-1
3230         local f2=$DIR/$tdir/$tfile-2
3231         local f3=$DIR/$tdir/$tfile-3
3232         local fid1=$(copy_file /etc/passwd $f1)
3233         local fid2=$(copy_file /etc/passwd $f2)
3234         copy_file /etc/passwd $f3
3235
3236         changelog_setup
3237         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3238         wait_request_state $fid1 ARCHIVE SUCCEED
3239
3240         $LFS hsm_archive $f2
3241         wait_request_state $fid2 ARCHIVE SUCCEED
3242
3243         rm $f1 || error "rm $f1 failed"
3244
3245         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
3246
3247         local target=0x3
3248         [[ $flags == $target ]] ||
3249                 error "Changelog flag is $flags not $target"
3250
3251         mv $f3 $f2 || error "mv $f3 $f2 failed"
3252
3253         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3254
3255         target=0x3
3256         [[ $flags == $target ]] ||
3257                 error "Changelog flag is $flags not $target"
3258
3259         cleanup
3260 }
3261 run_test 226 "changelog for last rm/mv with exiting archive"
3262
3263 check_flags_changes() {
3264         local f=$1
3265         local fid=$2
3266         local hsm_flag=$3
3267         local fst=$4
3268         local cnt=$5
3269
3270         local target=0x280
3271         $LFS hsm_set --$hsm_flag $f ||
3272                 error "Cannot set $hsm_flag on $f"
3273         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3274         local seen=${#flags[*]}
3275         cnt=$((fst + cnt))
3276         [[ $seen == $cnt ]] ||
3277                 error "set $hsm_flag: Changelog events $seen != $cnt"
3278         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3279                 error "set $hsm_flag: Changelog flags are "\
3280                         "${flags[$((cnt - 1))]} not $target"
3281
3282         $LFS hsm_clear --$hsm_flag $f ||
3283                 error "Cannot clear $hsm_flag on $f"
3284         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3285         seen=${#flags[*]}
3286         cnt=$(($cnt + 1))
3287         [[ $cnt == $seen ]] ||
3288                 error "clear $hsm_flag: Changelog events $seen != $cnt"
3289
3290         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3291                 error "clear $hsm_flag: Changelog flag is "\
3292                         "${flags[$((cnt - 1))]} not $target"
3293 }
3294
3295 test_227() {
3296         # test needs a running copytool
3297         copytool_setup
3298         changelog_setup
3299
3300         mkdir -p $DIR/$tdir
3301         typeset -a flags
3302
3303         for i in norelease noarchive exists archived
3304         do
3305                 local f=$DIR/$tdir/$tfile-$i
3306                 local fid=$(copy_file /etc/passwd $f)
3307                 check_flags_changes $f $fid $i 0 1
3308         done
3309
3310         f=$DIR/$tdir/$tfile---lost
3311         fid=$(copy_file /etc/passwd $f)
3312         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3313         wait_request_state $fid ARCHIVE SUCCEED
3314         check_flags_changes $f $fid lost 3 1
3315
3316         cleanup
3317 }
3318 run_test 227 "changelog when explicit setting of HSM flags"
3319
3320 test_228() {
3321         # test needs a running copytool
3322         copytool_setup
3323
3324         local fid=$(make_small_sync $DIR/$tfile)
3325         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
3326         wait_request_state $fid ARCHIVE SUCCEED
3327
3328         $LFS hsm_release $DIR/$tfile
3329         check_hsm_flags $DIR/$tfile "0x0000000d"
3330
3331         filefrag $DIR/$tfile | grep " 1 extent found" ||
3332                 error "filefrag on released file must return only one extent"
3333
3334         # only newer versions of cp detect sparse files by stat/FIEMAP
3335         # (LU-2580)
3336         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
3337                 error "copying $DIR/$tfile"
3338         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
3339
3340         $LFS hsm_release $DIR/$tfile
3341         check_hsm_flags $DIR/$tfile "0x0000000d"
3342
3343         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
3344
3345         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
3346                 error "tar failed"
3347         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
3348                 error "comparing untarred $DIR/$tfile"
3349
3350         rm -f $DIR/$tfile $DIR/$tfile.2 ||
3351                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
3352         copytool_cleanup
3353 }
3354 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
3355
3356 test_250() {
3357         # test needs a running copytool
3358         copytool_setup
3359
3360         mkdir -p $DIR/$tdir
3361         local maxrequest=$(get_hsm_param max_requests)
3362         local rqcnt=$(($maxrequest * 3))
3363         local i=""
3364
3365         cdt_disable
3366         for i in $(seq -w 1 $rqcnt); do
3367                 rm -f $DIR/$tdir/$i
3368                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
3369         done
3370         # we do it in 2 steps, so all requests arrive at the same time
3371         for i in $(seq -w 1 $rqcnt); do
3372                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3373         done
3374         cdt_enable
3375         local cnt=$rqcnt
3376         local wt=$rqcnt
3377         while [[ $cnt != 0 || $wt != 0 ]]; do
3378                 sleep 1
3379                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3380                         $HSM_PARAM.actions |\
3381                         grep STARTED | grep -v CANCEL | wc -l")
3382                 [[ $cnt -le $maxrequest ]] ||
3383                         error "$cnt > $maxrequest too many started requests"
3384                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
3385                         $HSM_PARAM.actions |\
3386                         grep WAITING | wc -l")
3387                 echo "max=$maxrequest started=$cnt waiting=$wt"
3388         done
3389
3390         copytool_cleanup
3391 }
3392 run_test 250 "Coordinator max request"
3393
3394 test_251() {
3395         # test needs a running copytool
3396         copytool_setup
3397
3398         mkdir -p $DIR/$tdir
3399         local f=$DIR/$tdir/$tfile
3400         local fid=$(make_large_for_cancel $f)
3401
3402         cdt_disable
3403         # to have a short test
3404         local old_to=$(get_hsm_param active_request_timeout)
3405         set_hsm_param active_request_timeout 4
3406         # to be sure the cdt will wake up frequently so
3407         # it will be able to cancel the "old" request
3408         local old_loop=$(get_hsm_param loop_period)
3409         set_hsm_param loop_period 2
3410         cdt_enable
3411
3412         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3413         wait_request_state $fid ARCHIVE STARTED
3414         sleep 5
3415         wait_request_state $fid ARCHIVE CANCELED
3416
3417         set_hsm_param active_request_timeout $old_to
3418         set_hsm_param loop_period $old_loop
3419
3420         copytool_cleanup
3421 }
3422 run_test 251 "Coordinator request timeout"
3423
3424 test_300() {
3425         # the only way to test ondisk conf is to restart MDS ...
3426         echo "Stop coordinator and remove coordinator state at mount"
3427         # stop coordinator
3428         cdt_shutdown
3429         # clean on disk conf set by default
3430         cdt_clear_mount_state
3431         cdt_check_state stopped
3432
3433         # check cdt still off after umount/remount
3434         fail $SINGLEMDS
3435         cdt_check_state stopped
3436
3437         echo "Set coordinator start at mount, and start coordinator"
3438         cdt_set_mount_state enabled
3439
3440         # check cdt is on
3441         cdt_check_state enabled
3442
3443         # check cdt still on after umount/remount
3444         fail $SINGLEMDS
3445         cdt_check_state enabled
3446
3447         # we are back to original state (cdt started at mount)
3448 }
3449 run_test 300 "On disk coordinator state kept between MDT umount/mount"
3450
3451 test_301() {
3452         local ai=$(get_hsm_param default_archive_id)
3453         local new=$((ai + 1))
3454
3455         set_hsm_param default_archive_id $new -P
3456         fail $SINGLEMDS
3457         local res=$(get_hsm_param default_archive_id)
3458
3459         # clear value
3460         set_hsm_param default_archive_id "" "-P -d"
3461
3462         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3463 }
3464 run_test 301 "HSM tunnable are persistent"
3465
3466 test_302() {
3467         local ai=$(get_hsm_param default_archive_id)
3468         local new=$((ai + 1))
3469
3470         # stop coordinator
3471         cdt_shutdown
3472
3473         set_hsm_param default_archive_id $new -P
3474
3475         local mdtno
3476         for mdtno in $(seq 1 $MDSCOUNT); do
3477                 fail mds${mdtno}
3478         done
3479
3480         # check cdt is on
3481         cdt_check_state enabled
3482
3483         local res=$(get_hsm_param default_archive_id)
3484
3485         # clear value
3486         set_hsm_param default_archive_id "" "-P -d"
3487
3488         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3489 }
3490 run_test 302 "HSM tunnable are persistent when CDT is off"
3491
3492 test_400() {
3493         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3494
3495         copytool_setup
3496
3497         mkdir -p $DIR/$tdir
3498
3499         local dir_mdt0=$DIR/$tdir/mdt0
3500         local dir_mdt1=$DIR/$tdir/mdt1
3501
3502         # create 1 dir per MDT
3503         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3504         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3505
3506         # create 1 file in each MDT
3507         local fid1=$(make_small $dir_mdt0/$tfile)
3508         local fid2=$(make_small $dir_mdt1/$tfile)
3509
3510         # check that hsm request on mdt0 is sent to the right MDS
3511         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3512         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3513                 echo "archive successful on mdt0"
3514
3515         # check that hsm request on mdt1 is sent to the right MDS
3516         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
3517         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3518                 echo "archive successful on mdt1"
3519
3520         copytool_cleanup
3521         # clean test files and directories
3522         rm -rf $dir_mdt0 $dir_mdt1
3523 }
3524 run_test 400 "Single request is sent to the right MDT"
3525
3526 test_401() {
3527         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3528
3529         copytool_setup
3530
3531         mkdir -p $DIR/$tdir
3532
3533         local dir_mdt0=$DIR/$tdir/mdt0
3534         local dir_mdt1=$DIR/$tdir/mdt1
3535
3536         # create 1 dir per MDT
3537         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3538         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3539
3540         # create 1 file in each MDT
3541         local fid1=$(make_small $dir_mdt0/$tfile)
3542         local fid2=$(make_small $dir_mdt1/$tfile)
3543
3544         # check that compound requests are shunt to the rights MDTs
3545         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
3546                 error "lfs hsm_archive"
3547         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3548                 echo "archive successful on mdt0"
3549         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3550                 echo "archive successful on mdt1"
3551
3552         copytool_cleanup
3553         # clean test files and directories
3554         rm -rf $dir_mdt0 $dir_mdt1
3555 }
3556 run_test 401 "Compound requests split and sent to their respective MDTs"
3557
3558 mdc_change_state() # facet, MDT_pattern, activate|deactivate
3559 {
3560         local facet=$1
3561         local pattern="$2"
3562         local state=$3
3563         local node=$(facet_active_host $facet)
3564         local mdc
3565         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
3566                         awk '{print $4}'); do
3567                 echo "$3 $mdc on $node"
3568                 do_facet $facet "$LCTL --device $mdc $state" || return 1
3569         done
3570 }
3571
3572 test_402() {
3573         # make sure there is no running copytool
3574         copytool_cleanup
3575
3576         # deactivate all mdc on agent1
3577         mdc_change_state $SINGLEAGT "MDT000." "deactivate"
3578
3579         copytool_setup $SINGLEAGT
3580
3581         check_agent_unregistered "uuid" # match any agent
3582
3583         # no expected running copytool
3584         search_copytools $agent && error "Copytool start should have failed"
3585
3586         # reactivate MDCs
3587         mdc_change_state $SINGLEAGT "MDT000." "activate"
3588 }
3589 run_test 402 "Copytool start fails if all MDTs are inactive"
3590
3591 test_403() {
3592         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3593
3594         # make sure there is no running copytool
3595         copytool_cleanup
3596
3597         local agent=$(facet_active_host $SINGLEAGT)
3598         local uuid=$(do_rpc_nodes $agent get_client_uuid | cut -d' ' -f2)
3599
3600         # deactivate all mdc for MDT0001
3601         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3602
3603         copytool_setup
3604         # check the agent is registered on MDT0000, and not on MDT0001
3605         check_agent_registered_by_mdt $uuid 0
3606         check_agent_unregistered_by_mdt $uuid 1
3607
3608         # check running copytool process
3609         search_copytools $agent || error "No running copytools on $agent"
3610
3611         # reactivate all mdc for MDT0001
3612         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3613
3614         # make sure the copytool is now registered to all MDTs
3615         check_agent_registered $uuid
3616
3617         copytool_cleanup
3618 }
3619 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
3620
3621 test_404() {
3622         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3623
3624         copytool_setup
3625
3626         # create files on both MDT0000 and MDT0001
3627         mkdir -p $DIR/$tdir
3628
3629         local dir_mdt0=$DIR/$tdir/mdt0
3630         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3631
3632         # create 1 file on mdt0
3633         local fid1=$(make_small $dir_mdt0/$tfile)
3634
3635         # deactivate all mdc for MDT0001
3636         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3637
3638         # send an HSM request for files in MDT0000
3639         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3640
3641         # check for completion of files in MDT0000
3642         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3643                 echo "archive successful on mdt0"
3644
3645         # reactivate all mdc for MDT0001
3646         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3647
3648         copytool_cleanup
3649         # clean test files and directories
3650         rm -rf $dir_mdt0
3651 }
3652 run_test 404 "Inactive MDT does not block requests for active MDTs"
3653
3654 copytool_cleanup
3655
3656 complete $SECONDS
3657 check_and_cleanup_lustre
3658 exit_status