Whamcloud - gitweb
LU-4343 tests: mkdir failing in sanity-hsm test 228
[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     3939
15 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT 34 35 36 40"
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=$(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_13() {
1159         # test needs a running copytool
1160         copytool_setup
1161
1162         local ARC_SUBDIR="import.orig"
1163         local d=""
1164         local f=""
1165
1166         # populate directory to be imported
1167         for d in $(seq 1 10); do
1168                 local CURR_DIR="$HSM_ARCHIVE/$ARC_SUBDIR/dir.$d"
1169                 do_facet $SINGLEAGT mkdir -p "$CURR_DIR"
1170                 for f in $(seq 1 10); do
1171                         CURR_FILE="$CURR_DIR/$tfile.$f"
1172                         # write file-specific data
1173                         do_facet $SINGLEAGT \
1174                                 "echo d=$d, f=$f, dir=$CURR_DIR, "\
1175                                         "file=$CURR_FILE > $CURR_FILE"
1176                 done
1177         done
1178         # import to Lustre
1179         import_file "$ARC_SUBDIR" $DIR/$tdir
1180         # diff lustre content and origin (triggers file restoration)
1181         # there must be 10x10 identical files, and no difference
1182         local cnt_ok=$(do_facet $SINGLEAGT diff -rs $HSM_ARCHIVE/$ARC_SUBDIR \
1183                        $DIR/$tdir/$ARC_SUBDIR | grep identical | wc -l)
1184         local cnt_diff=$(do_facet $SINGLEAGT diff -r $HSM_ARCHIVE/$ARC_SUBDIR \
1185                          $DIR/$tdir/$ARC_SUBDIR | wc -l)
1186
1187         [ $cnt_diff -eq 0 ] ||
1188                 error "$cnt_diff imported files differ from read data"
1189         [ $cnt_ok -eq 100 ] ||
1190                 error "not enough identical files ($cnt_ok != 100)"
1191
1192         copytool_cleanup
1193 }
1194 run_test 13 "Recursively import and restore a directory"
1195
1196 test_14() {
1197         # test needs a running copytool
1198         copytool_setup
1199
1200         # archive a file
1201         mkdir -p $DIR/$tdir
1202         local f=$DIR/$tdir/$tfile
1203         local fid=$(make_small $f)
1204         local sum=$(md5sum $f | awk '{print $1}')
1205         $LFS hsm_archive $f || error "could not archive file"
1206         wait_request_state $fid ARCHIVE SUCCEED
1207
1208         # delete the file
1209         rm -f $f
1210         # create released file (simulate llapi_hsm_import call)
1211         touch $f
1212         local fid2=$(path2fid $f)
1213         $LFS hsm_set --archived --exists $f || error "could not force hsm flags"
1214         $LFS hsm_release $f || error "could not release file"
1215
1216         # rebind the archive to the newly created file
1217         echo "rebind $fid to $fid2"
1218
1219         do_facet $SINGLEAGT \
1220                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1221                  --rebind $fid $fid2 $DIR" || error "could not rebind file"
1222
1223         # restore file and compare md5sum
1224         local sum2=$(md5sum $f | awk '{print $1}')
1225
1226         [[ $sum == $sum2 ]] || error "md5sum mismatch after restore"
1227
1228         copytool_cleanup
1229 }
1230 run_test 14 "Rebind archived file to a new fid"
1231
1232 test_15() {
1233         # test needs a running copytool
1234         copytool_setup
1235
1236         # archive files
1237         mkdir -p $DIR/$tdir
1238         local f=$DIR/$tdir/$tfile
1239         local count=5
1240         local tmpfile=$SHARED_DIRECTORY/tmp.$$
1241
1242         local fids=()
1243         local sums=()
1244         for i in $(seq 1 $count); do
1245                 fids[$i]=$(make_small $f.$i)
1246                 sums[$i]=$(md5sum $f.$i | awk '{print $1}')
1247                 $LFS hsm_archive $f.$i || error "could not archive file"
1248         done
1249         wait_all_done $(($count*60))
1250
1251         :>$tmpfile
1252         # delete the files
1253         for i in $(seq 1 $count); do
1254                 rm -f $f.$i
1255                 touch $f.$i
1256                 local fid2=$(path2fid $f.$i)
1257                 # add the rebind operation to the list
1258                 echo ${fids[$i]} $fid2 >> $tmpfile
1259
1260                 # set it released (simulate llapi_hsm_import call)
1261                 $LFS hsm_set --archived --exists $f.$i ||
1262                         error "could not force hsm flags"
1263                 $LFS hsm_release $f.$i || error "could not release file"
1264         done
1265         nl=$(wc -l < $tmpfile)
1266         [[ $nl == $count ]] || error "$nl files in list, $count expected"
1267
1268         echo "rebind list of files"
1269         do_facet $SINGLEAGT \
1270                 "$HSMTOOL --archive $HSM_ARCHIVE_NUMBER --hsm-root $HSM_ARCHIVE\
1271                  --rebind $tmpfile $DIR" || error "could not rebind file list"
1272
1273         # restore files and compare md5sum
1274         for i in $(seq 1 $count); do
1275                 local sum2=$(md5sum $f.$i | awk '{print $1}')
1276                 [[ $sum2 == ${sums[$i]} ]] ||
1277                     error "md5sum mismatch after restore ($sum2 != ${sums[$i]})"
1278         done
1279
1280         rm -f $tmpfile
1281         copytool_cleanup
1282 }
1283 run_test 15 "Rebind a list of files"
1284
1285 test_16() {
1286         # test needs a running copytool
1287         copytool_setup
1288
1289         local ref=/tmp/ref
1290         # create a known size file so we can verify transfer speed
1291         # 20 MB <-> 20s
1292         local goal=20
1293         dd if=/dev/zero of=$ref bs=1M count=20
1294
1295         mkdir -p $DIR/$tdir
1296         local f=$DIR/$tdir/$tfile
1297         local fid=$(copy_file $ref $f)
1298         rm $ref
1299         local start=$(date +%s)
1300         $LFS hsm_archive $f
1301         wait_request_state $fid ARCHIVE SUCCEED
1302         local end=$(date +%s)
1303         local duration=$((end - start))
1304
1305         [[ $duration -ge $goal ]] ||
1306                 error "Transfer is too fast $duration < $goal"
1307
1308         copytool_cleanup
1309 }
1310 run_test 16 "Test CT bandwith control option"
1311
1312 test_20() {
1313         mkdir -p $DIR/$tdir
1314
1315         local f=$DIR/$tdir/$tfile
1316         touch $f || error "touch $f failed"
1317
1318         # Could not release a non-archived file
1319         $LFS hsm_release $f && error "release should not succeed"
1320
1321         # For following tests, we must test them with HS_ARCHIVED set
1322         $LFS hsm_set --exists --archived $f || error "could not add flag"
1323
1324         # Could not release a file if no-release is set
1325         $LFS hsm_set --norelease $f || error "could not add flag"
1326         $LFS hsm_release $f && error "release should not succeed"
1327         $LFS hsm_clear --norelease $f || error "could not remove flag"
1328
1329         # Could not release a file if lost
1330         $LFS hsm_set --lost $f || error "could not add flag"
1331         $LFS hsm_release $f && error "release should not succeed"
1332         $LFS hsm_clear --lost $f || error "could not remove flag"
1333
1334         # Could not release a file if dirty
1335         $LFS hsm_set --dirty $f || error "could not add flag"
1336         $LFS hsm_release $f && error "release should not succeed"
1337         $LFS hsm_clear --dirty $f || error "could not remove flag"
1338 }
1339 run_test 20 "Release is not permitted"
1340
1341 test_21() {
1342         # test needs a running copytool
1343         copytool_setup
1344
1345         mkdir -p $DIR/$tdir
1346         local f=$DIR/$tdir/test_release
1347
1348         # Create a file and check its states
1349         local fid=$(make_small $f)
1350         check_hsm_flags $f "0x00000000"
1351
1352         # LU-4388/LU-4389 - ZFS does not report full number of blocks
1353         # used until file is flushed to disk
1354         if [  $(facet_fstype ost1) == "zfs" ]; then
1355             # this causes an OST_SYNC rpc to be sent
1356             dd if=/dev/zero of=$f bs=512 count=1 oflag=sync conv=notrunc,fsync
1357             # clear locks to reread file data
1358             cancel_lru_locks osc
1359         fi
1360
1361         local orig_size=$(stat -c "%s" $f)
1362         local orig_blocks=$(stat -c "%b" $f)
1363
1364         start_full_debug_logging
1365
1366         $LFS hsm_archive $f || error "could not archive file"
1367         wait_request_state $fid ARCHIVE SUCCEED
1368
1369         local blocks=$(stat -c "%b" $f)
1370         [ $blocks -eq $orig_blocks ] ||
1371                 error "$f: wrong block number after archive: " \
1372                       "$blocks != $orig_blocks"
1373         local size=$(stat -c "%s" $f)
1374         [ $size -eq $orig_size ] ||
1375                 error "$f: wrong size after archive: $size != $orig_size"
1376
1377         # Release and check states
1378         $LFS hsm_release $f || error "could not release file"
1379         check_hsm_flags $f "0x0000000d"
1380
1381         blocks=$(stat -c "%b" $f)
1382         [ $blocks -gt 5 ] &&
1383                 error "$f: too many blocks after release: $blocks > 5"
1384         size=$(stat -c "%s" $f)
1385         [ $size -ne $orig_size ] &&
1386                 error "$f: wrong size after release: $size != $orig_size"
1387
1388         # Check we can release an file without stripe info
1389         f=$f.nolov
1390         $MCREATE $f
1391         fid=$(path2fid $f)
1392         check_hsm_flags $f "0x00000000"
1393         $LFS hsm_archive $f || error "could not archive file"
1394         wait_request_state $fid ARCHIVE SUCCEED
1395
1396         # Release and check states
1397         $LFS hsm_release $f || error "could not release file"
1398         check_hsm_flags $f "0x0000000d"
1399
1400         # Release again a file that is already released is OK
1401         $LFS hsm_release $f || fail "second release should succeed"
1402         check_hsm_flags $f "0x0000000d"
1403
1404         stop_full_debug_logging
1405
1406         copytool_cleanup
1407 }
1408 run_test 21 "Simple release tests"
1409
1410 test_22() {
1411         # test needs a running copytool
1412         copytool_setup
1413
1414         mkdir -p $DIR/$tdir
1415
1416         local f=$DIR/$tdir/test_release
1417         local swap=$DIR/$tdir/test_swap
1418
1419         # Create a file and check its states
1420         local fid=$(make_small $f)
1421         check_hsm_flags $f "0x00000000"
1422
1423         $LFS hsm_archive $f || error "could not archive file"
1424         wait_request_state $fid ARCHIVE SUCCEED
1425
1426         # Release and check states
1427         $LFS hsm_release $f || error "could not release file"
1428         check_hsm_flags $f "0x0000000d"
1429
1430         make_small $swap
1431         $LFS swap_layouts $swap $f && error "swap_layouts should failed"
1432
1433         true
1434         copytool_cleanup
1435 }
1436 run_test 22 "Could not swap a release file"
1437
1438 test_23() {
1439         # test needs a running copytool
1440         copytool_setup
1441
1442         mkdir -p $DIR/$tdir
1443
1444         local f=$DIR/$tdir/test_mtime
1445
1446         # Create a file and check its states
1447         local fid=$(make_small $f)
1448         check_hsm_flags $f "0x00000000"
1449
1450         $LFS hsm_archive $f || error "could not archive file"
1451         wait_request_state $fid ARCHIVE SUCCEED
1452
1453         # Set modification time in the past
1454         touch -m -a -d @978261179 $f
1455
1456         # Release and check states
1457         $LFS hsm_release $f || error "could not release file"
1458         check_hsm_flags $f "0x0000000d"
1459
1460         local MTIME=$(stat -c "%Y" $f)
1461         local ATIME=$(stat -c "%X" $f)
1462         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
1463         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
1464
1465         copytool_cleanup
1466 }
1467 run_test 23 "Release does not change a/mtime (utime)"
1468
1469 test_24a() {
1470         local file=$DIR/$tdir/$tfile
1471         local fid
1472         local atime0
1473         local atime1
1474         local mtime0
1475         local mtime1
1476         local ctime0
1477         local ctime1
1478
1479         # test needs a running copytool
1480         copytool_setup
1481
1482         mkdir -p $DIR/$tdir
1483         rm -f $file
1484         fid=$(make_small $file)
1485
1486         # Create a file and check its states
1487         check_hsm_flags $file "0x00000000"
1488
1489         # Ensure atime is less than mtime and ctime.
1490         sleep 1
1491         echo >> $file
1492
1493         atime0=$(stat -c "%X" $file)
1494         mtime0=$(stat -c "%Y" $file)
1495         ctime0=$(stat -c "%Z" $file)
1496
1497         [ $atime0 -lt $mtime0 ] ||
1498                 error "atime $atime0 is not less than mtime $mtime0"
1499
1500         [ $atime0 -lt $ctime0 ] ||
1501                 error "atime $atime0 is not less than ctime $ctime0"
1502
1503         # Archive should not change any timestamps.
1504         $LFS hsm_archive $file || error "cannot archive '$file'"
1505         wait_request_state $fid ARCHIVE SUCCEED
1506
1507         atime1=$(stat -c "%X" $file)
1508         mtime1=$(stat -c "%Y" $file)
1509         ctime1=$(stat -c "%Z" $file)
1510
1511         [ $atime0 -eq $atime1 ] ||
1512                 error "archive changed atime from $atime0 to $atime1"
1513
1514         [ $mtime0 -eq $mtime1 ] ||
1515                 error "archive changed mtime from $mtime0 to $mtime1"
1516
1517         [ $ctime0 -eq $ctime1 ] ||
1518                 error "archive changed ctime from $ctime0 to $ctime1"
1519
1520         # Release should not change any timestamps.
1521         $LFS hsm_release $file || error "cannot release '$file'"
1522         check_hsm_flags $file "0x0000000d"
1523
1524         atime1=$(stat -c "%X" $file)
1525         mtime1=$(stat -c "%Y" $file)
1526         ctime1=$(stat -c "%Z" $file)
1527
1528         [ $atime0 -eq $atime1 ] ||
1529                 error "release changed atime from $atime0 to $atime1"
1530
1531         [ $mtime0 -eq $mtime1 ] ||
1532                 error "release changed mtime from $mtime0 to $mtime1"
1533
1534         [ $ctime0 -eq $ctime1 ] ||
1535                 error "release changed ctime from $ctime0 to $ctime1"
1536
1537         # Restore should not change atime or mtime and should not
1538         # decrease ctime.
1539         $LFS hsm_restore $file
1540         wait_request_state $fid RESTORE SUCCEED
1541
1542         atime1=$(stat -c "%X" $file)
1543         mtime1=$(stat -c "%Y" $file)
1544         ctime1=$(stat -c "%Z" $file)
1545
1546         [ $atime0 -eq $atime1 ] ||
1547                 error "restore changed atime from $atime0 to $atime1"
1548
1549         [ $mtime0 -eq $mtime1 ] ||
1550                 error "restore changed mtime from $mtime0 to $mtime1"
1551
1552         [ $ctime0 -le $ctime1 ] ||
1553                 error "restore changed ctime from $ctime0 to $ctime1"
1554
1555         copytool_cleanup
1556
1557         # Once more, after unmount and mount.
1558         umount_client $MOUNT || error "cannot unmount '$MOUNT'"
1559         mount_client $MOUNT || error "cannot mount '$MOUNT'"
1560
1561         atime1=$(stat -c "%X" $file)
1562         mtime1=$(stat -c "%Y" $file)
1563         ctime1=$(stat -c "%Z" $file)
1564
1565         [ $atime0 -eq $atime1 ] ||
1566                 error "remount changed atime from $atime0 to $atime1"
1567
1568         [ $mtime0 -eq $mtime1 ] ||
1569                 error "remount changed mtime from $mtime0 to $mtime1"
1570
1571         [ $ctime0 -le $ctime1 ] ||
1572                 error "remount changed ctime from $ctime0 to $ctime1"
1573 }
1574 run_test 24a "Archive, release, and restore does not change a/mtime (i/o)"
1575
1576 test_24b() {
1577         local file=$DIR/$tdir/$tfile
1578         local fid
1579         local sum0
1580         local sum1
1581         # LU-3811
1582
1583         # Test needs a running copytool.
1584         copytool_setup
1585         mkdir -p $DIR/$tdir
1586
1587         # Check that root can do HSM actions on a ordinary user's file.
1588         rm -f $file
1589         fid=$(make_small $file)
1590         sum0=$(md5sum $file)
1591
1592         chown $RUNAS_ID:$RUNAS_GID $file ||
1593                 error "cannot chown '$file' to '$RUNAS_ID'"
1594
1595         chmod ugo-w $DIR/$tdir ||
1596                 error "cannot chmod '$DIR/$tdir'"
1597
1598         $LFS hsm_archive $file
1599         wait_request_state $fid ARCHIVE SUCCEED
1600
1601         $LFS hsm_release $file
1602         check_hsm_flags $file "0x0000000d"
1603
1604         $LFS hsm_restore $file
1605         wait_request_state $fid RESTORE SUCCEED
1606
1607         # Check that ordinary user can get HSM state.
1608         $RUNAS $LFS hsm_state $file ||
1609                 error "user '$RUNAS_ID' cannot get HSM state of '$file'"
1610
1611         $LFS hsm_release $file
1612         check_hsm_flags $file "0x0000000d"
1613
1614         # Check that ordinary user can accessed released file.
1615         sum1=$($RUNAS md5sum $file) ||
1616                 error "user '$RUNAS_ID' cannot read '$file'"
1617
1618         [ "$sum0" == "$sum1" ] ||
1619                 error "md5sum mismatch for '$file'"
1620
1621         copytool_cleanup
1622 }
1623 run_test 24b "root can archive, release, and restore user files"
1624
1625 cleanup_test_24c() {
1626         trap 0
1627         set_hsm_param user_request_mask RESTORE
1628         set_hsm_param group_request_mask RESTORE
1629         set_hsm_param other_request_mask RESTORE
1630 }
1631
1632 test_24c() {
1633         local file=$DIR/$tdir/$tfile
1634         local action=archive
1635         local user_save
1636         local group_save
1637         local other_save
1638
1639         # test needs a running copytool
1640         copytool_setup
1641
1642         mkdir -p $DIR/$tdir
1643
1644         # Save the default masks and check that cleanup_24c will
1645         # restore the request masks correctly.
1646         user_save=$(get_hsm_param user_request_mask)
1647         group_save=$(get_hsm_param group_request_mask)
1648         other_save=$(get_hsm_param other_request_mask)
1649
1650         [ "$user_save" == RESTORE ] ||
1651                 error "user_request_mask is '$user_save' expected 'RESTORE'"
1652         [ "$group_save" == RESTORE ] ||
1653                 error "group_request_mask is '$group_save' expected 'RESTORE'"
1654         [ "$other_save" == RESTORE ] ||
1655                 error "other_request_mask is '$other_save' expected 'RESTORE'"
1656
1657         trap cleanup_test_24c EXIT
1658
1659         # User.
1660         rm -f $file
1661         make_small $file
1662         chown $RUNAS_ID:nobody $file ||
1663                 error "cannot chown '$file' to '$RUNAS_ID:nobody'"
1664
1665         set_hsm_param user_request_mask ""
1666         $RUNAS $LFS hsm_$action $file &&
1667                 error "$action by user should fail"
1668
1669         set_hsm_param user_request_mask $action
1670         $RUNAS $LFS hsm_$action $file ||
1671                 error "$action by user should succeed"
1672
1673         # Group.
1674         rm -f $file
1675         make_small $file
1676         chown nobody:$RUNAS_GID $file ||
1677                 error "cannot chown '$file' to 'nobody:$RUNAS_GID'"
1678
1679         set_hsm_param group_request_mask ""
1680         $RUNAS $LFS hsm_$action $file &&
1681                 error "$action by group should fail"
1682
1683         set_hsm_param group_request_mask $action
1684         $RUNAS $LFS hsm_$action $file ||
1685                 error "$action by group should succeed"
1686
1687         # Other.
1688         rm -f $file
1689         make_small $file
1690         chown nobody:nobody $file ||
1691                 error "cannot chown '$file' to 'nobody:nobody'"
1692
1693         set_hsm_param other_request_mask ""
1694         $RUNAS $LFS hsm_$action $file &&
1695                 error "$action by other should fail"
1696
1697         set_hsm_param other_request_mask $action
1698         $RUNAS $LFS hsm_$action $file ||
1699                 error "$action by other should succeed"
1700
1701         copytool_cleanup
1702         cleanup_test_24c
1703 }
1704 run_test 24c "check that user,group,other request masks work"
1705
1706 cleanup_test_24d() {
1707         trap 0
1708         mount -o remount,rw $MOUNT2
1709 }
1710
1711 test_24d() {
1712         local file1=$DIR/$tdir/$tfile
1713         local file2=$DIR2/$tdir/$tfile
1714         local fid1
1715         local fid2
1716
1717         copytool_setup
1718
1719         mkdir -p $DIR/$tdir
1720         rm -f $file1
1721         fid1=$(make_small $file1)
1722
1723         trap cleanup_test_24d EXIT
1724
1725         mount -o remount,ro $MOUNT2
1726
1727         fid2=$(path2fid $file2)
1728         [ "$fid1" == "$fid2" ] ||
1729                 error "FID mismatch '$fid1' != '$fid2'"
1730
1731         $LFS hsm_archive $file2 &&
1732                 error "archive should fail on read-only mount"
1733         check_hsm_flags $file1 "0x00000000"
1734
1735         $LFS hsm_archive $file1
1736         wait_request_state $fid1 ARCHIVE SUCCEED
1737
1738         $LFS hsm_release $file1
1739         $LFS hsm_restore $file2
1740         wait_request_state $fid1 RESTORE SUCCEED
1741
1742         $LFS hsm_release $file1 || error "cannot release '$file1'"
1743         dd if=$file2 of=/dev/null bs=1M || "cannot read '$file2'"
1744
1745         $LFS hsm_release $file2 &&
1746                 error "release should fail on read-only mount"
1747
1748         copytool_cleanup
1749         cleanup_test_24d
1750 }
1751 run_test 24d "check that read-only mounts are respected"
1752
1753 test_25a() {
1754         # test needs a running copytool
1755         copytool_setup
1756
1757         mkdir -p $DIR/$tdir
1758         copy2archive /etc/hosts $tdir/$tfile
1759
1760         local f=$DIR/$tdir/$tfile
1761
1762         import_file $tdir/$tfile $f
1763
1764         $LFS hsm_set --lost $f
1765
1766         md5sum $f
1767         local st=$?
1768
1769         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1770
1771         copytool_cleanup
1772 }
1773 run_test 25a "Restore lost file (HS_LOST flag) from import"\
1774              " (Operation not permitted)"
1775
1776 test_25b() {
1777         # test needs a running copytool
1778         copytool_setup
1779
1780         mkdir -p $DIR/$tdir
1781
1782         local f=$DIR/$tdir/$tfile
1783         local fid=$(copy_file /etc/passwd $f)
1784
1785         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1786         wait_request_state $fid ARCHIVE SUCCEED
1787
1788         $LFS hsm_release $f
1789         $LFS hsm_set --lost $f
1790         md5sum $f
1791         st=$?
1792
1793         [[ $st == 1 ]] || error "lost file access should failed (returns $st)"
1794
1795         copytool_cleanup
1796 }
1797 run_test 25b "Restore lost file (HS_LOST flag) after release"\
1798              " (Operation not permitted)"
1799
1800 test_26() {
1801         # test needs a running copytool
1802         copytool_setup
1803
1804         mkdir -p $DIR/$tdir
1805         local f=$DIR/$tdir/$tfile
1806         local fid=$(make_large_for_progress $f)
1807         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1808         wait_request_state $fid ARCHIVE SUCCEED
1809
1810         $LFS hsm_remove $f
1811         wait_request_state $fid REMOVE SUCCEED
1812
1813         check_hsm_flags $f "0x00000000"
1814
1815         copytool_cleanup
1816 }
1817 run_test 26 "Remove the archive of a valid file"
1818
1819 test_27a() {
1820         # test needs a running copytool
1821         copytool_setup
1822
1823         mkdir -p $DIR/$tdir
1824         make_archive $tdir/$tfile
1825         local f=$DIR/$tdir/$tfile
1826         import_file $tdir/$tfile $f
1827         local fid=$(path2fid $f)
1828
1829         $LFS hsm_remove $f
1830
1831         [[ $? != 0 ]] || error "Remove of a released file should fail"
1832
1833         copytool_cleanup
1834 }
1835 run_test 27a "Remove the archive of an imported file (Operation not permitted)"
1836
1837 test_27b() {
1838         # test needs a running copytool
1839         copytool_setup
1840
1841         mkdir -p $DIR/$tdir
1842         local f=$DIR/$tdir/$tfile
1843         local fid=$(make_large_for_progress $f)
1844         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1845         wait_request_state $fid ARCHIVE SUCCEED
1846         $LFS hsm_release $f
1847
1848         $LFS hsm_remove $f
1849
1850         [[ $? != 0 ]] || error "Remove of a released file should fail"
1851
1852         copytool_cleanup
1853 }
1854 run_test 27b "Remove the archive of a relased file (Operation not permitted)"
1855
1856 test_28() {
1857         # test needs a running copytool
1858         copytool_setup
1859
1860         mkdir -p $DIR/$tdir
1861         local f=$DIR/$tdir/$tfile
1862         local fid=$(make_large_for_progress $f)
1863         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1864         wait_request_state $fid ARCHIVE SUCCEED
1865
1866         cdt_disable
1867         $LFS hsm_remove $f
1868
1869         rm -f $f
1870
1871         cdt_enable
1872
1873         wait_request_state $fid REMOVE SUCCEED
1874
1875         copytool_cleanup
1876 }
1877 run_test 28 "Concurrent archive/file remove"
1878
1879 test_30a() {
1880         # restore at exec cannot work on agent node (because of Linux kernel
1881         # protection of executables)
1882         needclients 2 || return 0
1883
1884         # test needs a running copytool
1885         copytool_setup
1886
1887         mkdir -p $DIR/$tdir
1888         copy2archive /bin/true $tdir/$tfile
1889
1890         local f=$DIR/$tdir/true
1891         import_file $tdir/$tfile $f
1892
1893         local fid=$(path2fid $f)
1894
1895         # set no retry action mode
1896         cdt_set_no_retry
1897         do_node $CLIENT2 $f
1898         local st=$?
1899
1900         # cleanup
1901         # remove no try action mode
1902         cdt_clear_no_retry
1903         $LFS hsm_state $f
1904
1905         [[ $st == 0 ]] || error "Failed to exec a released file"
1906
1907         copytool_cleanup
1908 }
1909 run_test 30a "Restore at exec (import case)"
1910
1911 test_30b() {
1912         # restore at exec cannot work on agent node (because of Linux kernel
1913         # protection of executables)
1914         needclients 2 || return 0
1915
1916         # test needs a running copytool
1917         copytool_setup
1918
1919         mkdir -p $DIR/$tdir
1920         local f=$DIR/$tdir/true
1921         local fid=$(copy_file /bin/true $f)
1922         chmod 755 $f
1923         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1924         wait_request_state $fid ARCHIVE SUCCEED
1925         $LFS hsm_release $f
1926         $LFS hsm_state $f
1927         # set no retry action mode
1928         cdt_set_no_retry
1929         do_node $CLIENT2 $f
1930         local st=$?
1931
1932         # cleanup
1933         # remove no try action mode
1934         cdt_clear_no_retry
1935         $LFS hsm_state $f
1936
1937         [[ $st == 0 ]] || error "Failed to exec a released file"
1938
1939         copytool_cleanup
1940 }
1941 run_test 30b "Restore at exec (release case)"
1942
1943 test_30c() {
1944         needclients 2 || return 0
1945
1946         # test needs a running copytool
1947         copytool_setup
1948
1949         mkdir -p $DIR/$tdir
1950         local f=$DIR/$tdir/SLEEP
1951         local fid=$(copy_file /bin/sleep $f)
1952         chmod 755 $f
1953         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
1954         wait_request_state $fid ARCHIVE SUCCEED
1955         $LFS hsm_release $f
1956         check_hsm_flags $f "0x0000000d"
1957         # set no retry action mode
1958         cdt_set_no_retry
1959         do_node $CLIENT2 "$f 10" &
1960         local pid=$!
1961         sleep 3
1962         echo 'Hi!' > $f
1963         [[ $? == 0 ]] && error "Update during exec of released file must fail"
1964         wait $pid
1965         [[ $? == 0 ]] || error "Execution failed during run"
1966         cmp /bin/sleep $f
1967         [[ $? == 0 ]] || error "Binary overwritten during exec"
1968
1969         # cleanup
1970         # remove no try action mode
1971         cdt_clear_no_retry
1972         check_hsm_flags $f "0x00000009"
1973
1974         copytool_cleanup
1975 }
1976 run_test 30c "Update during exec of released file must fail"
1977
1978 restore_and_check_size() {
1979         local f=$1
1980         local fid=$2
1981         local s=$(stat -c "%s" $f)
1982         local n=$s
1983         local st=$(get_hsm_flags $f)
1984         local err=0
1985         local cpt=0
1986         $LFS hsm_restore $f
1987         while [[ "$st" != "0x00000009" && $cpt -le 10 ]]
1988         do
1989                 n=$(stat -c "%s" $f)
1990                 # we echo in both cases to show stat is not
1991                 # hang
1992                 if [[ $n != $s ]]; then
1993                         echo "size seen is $n != $s"
1994                         err=1
1995                 else
1996                         echo "size seen is right: $n == $s"
1997                 fi
1998                 st=$(get_hsm_flags $f)
1999                 sleep 10
2000                 cpt=$((cpt + 1))
2001         done
2002         if [[ $cpt -lt 10 ]]; then
2003                 echo " "done
2004         else
2005                 echo " restore is too long"
2006                 wait_request_state $fid RESTORE SUCCEED
2007         fi
2008         return $err
2009 }
2010
2011 test_31a() {
2012         # test needs a running copytool
2013         copytool_setup
2014
2015         mkdir -p $DIR/$tdir
2016
2017         make_archive $tdir/$tfile
2018         local f=$DIR/$tdir/$tfile
2019         import_file $tdir/$tfile $f
2020         local fid=$($LFS path2fid $f)
2021         HSM_ARCHIVE_PURGE=false copytool_setup
2022
2023         restore_and_check_size $f $fid
2024         local err=$?
2025
2026         [[ $err -eq 0 ]] || error "File size changed during restore"
2027
2028         copytool_cleanup
2029 }
2030 run_test 31a "Import a large file and check size during restore"
2031
2032
2033 test_31b() {
2034         # test needs a running copytool
2035         copytool_setup
2036
2037         mkdir -p $DIR/$tdir
2038
2039         local f=$DIR/$tdir/$tfile
2040         local fid=$(make_large_for_progress $f)
2041         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2042         wait_request_state $fid ARCHIVE SUCCEED
2043         $LFS hsm_release $f
2044
2045         restore_and_check_size $f $fid
2046         local err=$?
2047
2048         [[ $err -eq 0 ]] || error "File size changed during restore"
2049
2050         copytool_cleanup
2051 }
2052 run_test 31b "Restore a large unaligned file and check size during restore"
2053
2054 test_31c() {
2055         # test needs a running copytool
2056         copytool_setup
2057
2058         mkdir -p $DIR/$tdir
2059
2060         local f=$DIR/$tdir/$tfile
2061         local fid=$(make_large_for_progress_aligned $f)
2062         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2063         wait_request_state $fid ARCHIVE SUCCEED
2064         $LFS hsm_release $f
2065
2066         restore_and_check_size $f $fid
2067         local err=$?
2068
2069         [[ $err -eq 0 ]] || error "File size changed during restore"
2070
2071         copytool_cleanup
2072 }
2073 run_test 31c "Restore a large aligned file and check size during restore"
2074
2075 test_33() {
2076         # test needs a running copytool
2077         copytool_setup
2078
2079         mkdir -p $DIR/$tdir
2080
2081         local f=$DIR/$tdir/$tfile
2082         local fid=$(make_large_for_progress $f)
2083         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2084         wait_request_state $fid ARCHIVE SUCCEED
2085         $LFS hsm_release $f
2086
2087         md5sum $f >/dev/null &
2088         local pid=$!
2089         wait_request_state $fid RESTORE STARTED
2090
2091         kill -15 $pid
2092         sleep 1
2093
2094         # Check restore trigger process was killed
2095         local killed=$(ps -o pid,comm hp $pid >/dev/null)
2096
2097         $LFS hsm_cancel $f
2098
2099         wait_request_state $fid RESTORE CANCELED
2100         wait_request_state $fid CANCEL SUCCEED
2101
2102         [ -z $killed ] ||
2103                 error "Cannot kill process waiting for restore ($killed)"
2104
2105         copytool_cleanup
2106 }
2107 run_test 33 "Kill a restore waiting process"
2108
2109 test_34() {
2110         # test needs a running copytool
2111         copytool_setup
2112
2113         mkdir -p $DIR/$tdir
2114
2115         local f=$DIR/$tdir/$tfile
2116         local fid=$(make_large_for_progress $f)
2117         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2118         wait_request_state $fid ARCHIVE SUCCEED
2119         $LFS hsm_release $f
2120
2121         md5sum $f >/dev/null &
2122         local pid=$!
2123         wait_request_state $fid RESTORE STARTED
2124
2125         rm $f || error "rm $f failed"
2126         # rm must not block during restore
2127         wait_request_state $fid RESTORE STARTED
2128
2129         wait_request_state $fid RESTORE SUCCEED
2130         # check md5sum pgm finished
2131         local there=$(ps -o pid,comm hp $pid >/dev/null)
2132         [[ -z $there ]] || error "Restore initiator does not exit"
2133
2134         local rc=$(wait $pid)
2135         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2136
2137         copytool_cleanup
2138 }
2139 run_test 34 "Remove file during restore"
2140
2141 test_35() {
2142         # test needs a running copytool
2143         copytool_setup
2144
2145         mkdir -p $DIR/$tdir
2146
2147         local f=$DIR/$tdir/$tfile
2148         local f1=$DIR/$tdir/$tfile-1
2149         local fid=$(make_large_for_progress $f)
2150         local fid1=$(copy_file /etc/passwd $f1)
2151         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2152         wait_request_state $fid ARCHIVE SUCCEED
2153         $LFS hsm_release $f
2154
2155         md5sum $f >/dev/null &
2156         local pid=$!
2157         wait_request_state $fid RESTORE STARTED
2158
2159         mv $f1 $f || error "mv $f1 $f failed"
2160         # mv must not block during restore
2161         wait_request_state $fid RESTORE STARTED
2162
2163         wait_request_state $fid RESTORE SUCCEED
2164         # check md5sum pgm finished
2165         local there=$(ps -o pid,comm hp $pid >/dev/null)
2166         [[ -z $there ]] || error "Restore initiator does not exit"
2167
2168         local rc=$(wait $pid)
2169         [[ $rc -eq 0 ]] || error "Restore initiator failed with $rc"
2170
2171         fid2=$(path2fid $f)
2172         [[ $fid2 == $fid1 ]] || error "Wrong fid after mv $fid2 != $fid1"
2173
2174         copytool_cleanup
2175 }
2176 run_test 35 "Overwrite file during restore"
2177
2178 test_36() {
2179         # test needs a running copytool
2180         copytool_setup
2181
2182         mkdir -p $DIR/$tdir
2183
2184         local f=$DIR/$tdir/$tfile
2185         local fid=$(make_large_for_progress $f)
2186         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2187         wait_request_state $fid ARCHIVE SUCCEED
2188         $LFS hsm_release $f
2189
2190         md5sum $f >/dev/null &
2191         local pid=$!
2192         wait_request_state $fid RESTORE STARTED
2193
2194         mv $f $f.new
2195         # rm must not block during restore
2196         wait_request_state $fid RESTORE STARTED
2197
2198         wait_request_state $fid RESTORE SUCCEED
2199         # check md5sum pgm finished
2200         local there=$(ps -o pid,comm hp $pid >/dev/null)
2201         [[ -z $there ]] ||
2202                 error "Restore initiator does not exit"
2203
2204         local rc=$(wait $pid)
2205         [[ $rc -eq 0 ]] ||
2206                 error "Restore initiator failed with $rc"
2207
2208         copytool_cleanup
2209 }
2210 run_test 36 "Move file during restore"
2211
2212 multi_archive() {
2213         local prefix=$1
2214         local count=$2
2215         local n=""
2216
2217         for n in $(seq 1 $count); do
2218                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $prefix.$n
2219         done
2220         echo "$count archive requests submitted"
2221 }
2222
2223 test_40() {
2224         local stream_count=4
2225         local file_count=100
2226         mkdir -p $DIR/$tdir
2227         local f=$DIR/$tdir/$tfile
2228         local i=""
2229         local p=""
2230         local fid=""
2231
2232         for i in $(seq 1 $file_count); do
2233                 for p in $(seq 1 $stream_count); do
2234                         fid=$(copy_file /etc/hosts $f.$p.$i)
2235                 done
2236         done
2237         copytool_setup
2238         # to be sure wait_all_done will not be mislead by previous tests
2239         cdt_purge
2240         wait_for_grace_delay
2241         typeset -a pids
2242         # start archive streams in background (archive files in parallel)
2243         for p in $(seq 1 $stream_count); do
2244                 multi_archive $f.$p $file_count &
2245                 pids[$p]=$!
2246         done
2247         echo -n  "Wait for all requests being enqueued..."
2248         wait ${pids[*]}
2249         echo OK
2250         wait_all_done 100
2251         copytool_cleanup
2252 }
2253 run_test 40 "Parallel archive requests"
2254
2255 test_52() {
2256         # test needs a running copytool
2257         copytool_setup
2258
2259         mkdir -p $DIR/$tdir
2260         local f=$DIR/$tdir/$tfile
2261         local fid=$(copy_file /etc/motd $f 1)
2262
2263         $LFS hsm_archive $f || error "could not archive file"
2264         wait_request_state $fid ARCHIVE SUCCEED
2265         check_hsm_flags $f "0x00000009"
2266
2267         multiop_bg_pause $f O_c || error "multiop failed"
2268         local MULTIPID=$!
2269
2270         mds_evict_client
2271         client_up || client_up || true
2272
2273         kill -USR1 $MULTIPID
2274         wait $MULTIPID || error "multiop close failed"
2275
2276         check_hsm_flags $f "0x0000000b"
2277
2278         copytool_cleanup
2279 }
2280 run_test 52 "Opened for write file on an evicted client should be set dirty"
2281
2282 test_53() {
2283         # test needs a running copytool
2284         copytool_setup
2285
2286         mkdir -p $DIR/$tdir
2287         local f=$DIR/$tdir/$tfile
2288         local fid=$(copy_file /etc/motd $f 1)
2289
2290         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2291                 error "could not archive file"
2292         wait_request_state $fid ARCHIVE SUCCEED
2293         check_hsm_flags $f "0x00000009"
2294
2295         multiop_bg_pause $f o_c || error "multiop failed"
2296         MULTIPID=$!
2297
2298         mds_evict_client
2299         client_up || client_up || true
2300
2301         kill -USR1 $MULTIPID
2302         wait $MULTIPID || error "multiop close failed"
2303
2304         check_hsm_flags $f "0x00000009"
2305
2306         copytool_cleanup
2307 }
2308 run_test 53 "Opened for read file on an evicted client should not be set dirty"
2309
2310 test_54() {
2311         # test needs a running copytool
2312         copytool_setup
2313
2314         mkdir -p $DIR/$tdir
2315         local f=$DIR/$tdir/$tfile
2316         local fid=$(make_small $f)
2317
2318         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2319                 error "could not archive file"
2320         wait_request_state $fid ARCHIVE STARTED
2321
2322         check_hsm_flags $f "0x00000001"
2323
2324         # Avoid coordinator resending this request as soon it has failed.
2325         cdt_set_no_retry
2326
2327         echo "foo" >> $f
2328         sync
2329         wait_request_state $fid ARCHIVE FAILED
2330
2331         check_hsm_flags $f "0x00000003"
2332
2333         cdt_clear_no_retry
2334         copytool_cleanup
2335 }
2336 run_test 54 "Write during an archive cancels it"
2337
2338 test_55() {
2339         # test needs a running copytool
2340         copytool_setup
2341
2342         mkdir -p $DIR/$tdir
2343         local f=$DIR/$tdir/$tfile
2344         local fid=$(make_small $f)
2345
2346         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2347                 error "could not archive file"
2348         wait_request_state $fid ARCHIVE STARTED
2349
2350         check_hsm_flags $f "0x00000001"
2351
2352         # Avoid coordinator resending this request as soon it has failed.
2353         cdt_set_no_retry
2354
2355         $TRUNCATE $f 1024 || error "truncate failed"
2356         sync
2357         wait_request_state $fid ARCHIVE FAILED
2358
2359         check_hsm_flags $f "0x00000003"
2360
2361         cdt_clear_no_retry
2362         copytool_cleanup
2363 }
2364 run_test 55 "Truncate during an archive cancels it"
2365
2366 test_56() {
2367         # test needs a running copytool
2368         copytool_setup
2369
2370         mkdir -p $DIR/$tdir
2371         local f=$DIR/$tdir/$tfile
2372         local fid=$(make_large_for_progress $f)
2373
2374         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2375                 error "could not archive file"
2376         wait_request_state $fid ARCHIVE STARTED
2377
2378         check_hsm_flags $f "0x00000001"
2379
2380         # Change metadata and sync to be sure we are not changing only
2381         # in memory.
2382         chmod 644 $f
2383         chgrp sys $f
2384         sync
2385         wait_request_state $fid ARCHIVE SUCCEED
2386
2387         check_hsm_flags $f "0x00000009"
2388
2389         copytool_cleanup
2390 }
2391 run_test 56 "Setattr during an archive is ok"
2392
2393 test_57() {
2394         # Need one client for I/O, one for request
2395         needclients 2 || return 0
2396
2397         # test needs a running copytool
2398         copytool_setup
2399
2400         mkdir -p $DIR/$tdir
2401         local f=$DIR/$tdir/test_archive_remote
2402         # Create a file on a remote node
2403         do_node $CLIENT2 "dd if=/dev/urandom of=$f bs=1M "\
2404                 "count=2 conv=fsync"
2405
2406         # And archive it
2407         do_node $CLIENT2 "$LFS hsm_archive -a $HSM_ARCHIVE_NUMBER $f" ||
2408                 error "hsm_archive failed"
2409         local fid=$(path2fid $f)
2410         wait_request_state $fid ARCHIVE SUCCEED
2411
2412         # Release and implicit restore it
2413         do_node $CLIENT2 "$LFS hsm_release $f" ||
2414                 error "hsm_release failed"
2415         do_node $CLIENT2 "md5sum $f" ||
2416                 error "hsm_restore failed"
2417
2418         wait_request_state $fid RESTORE SUCCEED
2419
2420         copytool_cleanup
2421 }
2422 run_test 57 "Archive a file with dirty cache on another node"
2423
2424 truncate_released_file() {
2425         local src_file=$1
2426         local trunc_to=$2
2427
2428         local sz=$(stat -c %s $src_file)
2429         local f=$DIR/$tdir/$tfile
2430         local fid=$(copy_file $1 $f)
2431         local ref=$f-ref
2432         cp $f $f-ref
2433
2434         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f ||
2435                 error "could not archive file"
2436         wait_request_state $fid ARCHIVE SUCCEED
2437
2438         $LFS hsm_release $f || error "could not release file"
2439
2440         $TRUNCATE $f $trunc_to || error "truncate failed"
2441         sync
2442
2443         local sz1=$(stat -c %s $f)
2444         [[ $sz1 == $trunc_to ]] ||
2445                 error "size after trunc: $sz1 expect $trunc_to, original $sz"
2446
2447         $LFS hsm_state $f
2448         check_hsm_flags $f "0x0000000b"
2449
2450         local state=$(get_request_state $fid RESTORE)
2451         [[ "$state" == "SUCCEED" ]] ||
2452                 error "truncate $sz does not trig restore, state = $state"
2453
2454         $TRUNCATE $ref $trunc_to
2455         cmp $ref $f || error "file data wrong after truncate"
2456
2457         rm -f $f $f-ref
2458 }
2459
2460 test_58() {
2461         # test needs a running copytool
2462         copytool_setup
2463
2464         mkdir -p $DIR/$tdir
2465
2466         local sz=$(stat -c %s /etc/passwd)
2467
2468         echo "truncate up from $sz to $((sz*2))"
2469         truncate_released_file /etc/passwd $((sz*2))
2470
2471         echo "truncate down from $sz to $((sz/2))"
2472         truncate_released_file /etc/passwd $((sz/2))
2473
2474         echo "truncate to 0"
2475         truncate_released_file /etc/passwd 0
2476
2477         copytool_cleanup
2478 }
2479 run_test 58 "Truncate a released file will trigger restore"
2480
2481 test_90() {
2482         file_count=57
2483         mkdir -p $DIR/$tdir
2484         local f=$DIR/$tdir/$tfile
2485         local FILELIST=/tmp/filelist.txt
2486         local i=""
2487
2488         rm -f $FILELIST
2489         for i in $(seq 1 $file_count); do
2490                 fid=$(copy_file /etc/hosts $f.$i)
2491                 echo $f.$i >> $FILELIST
2492         done
2493         copytool_setup
2494         # to be sure wait_all_done will not be mislead by previous tests
2495         cdt_purge
2496         wait_for_grace_delay
2497         $LFS hsm_archive --filelist $FILELIST ||
2498                 error "cannot archive a file list"
2499         wait_all_done 100
2500         $LFS hsm_release --filelist $FILELIST ||
2501                 error "cannot release a file list"
2502         $LFS hsm_restore --filelist $FILELIST ||
2503                 error "cannot restore a file list"
2504         wait_all_done 100
2505         copytool_cleanup
2506 }
2507 run_test 90 "Archive/restore a file list"
2508
2509 double_verify_reset_hsm_param() {
2510         local p=$1
2511         echo "Testing $HSM_PARAM.$p"
2512         local val=$(get_hsm_param $p)
2513         local save=$val
2514         local val2=$(($val * 2))
2515         set_hsm_param $p $val2
2516         val=$(get_hsm_param $p)
2517         [[ $val == $val2 ]] ||
2518                 error "$HSM_PARAM.$p: $val != $val2 should be (2 * $save)"
2519         echo "Set $p to 0 must failed"
2520         set_hsm_param $p 0
2521         local rc=$?
2522         # restore value
2523         set_hsm_param $p $save
2524
2525         if [[ $rc == 0 ]]; then
2526                 error "we must not be able to set $HSM_PARAM.$p to 0"
2527         fi
2528 }
2529
2530 test_100() {
2531         double_verify_reset_hsm_param loop_period
2532         double_verify_reset_hsm_param grace_delay
2533         double_verify_reset_hsm_param active_request_timeout
2534         double_verify_reset_hsm_param max_requests
2535         double_verify_reset_hsm_param default_archive_id
2536 }
2537 run_test 100 "Set coordinator /proc tunables"
2538
2539 test_102() {
2540         cdt_disable
2541         cdt_enable
2542         cdt_restart
2543 }
2544 run_test 102 "Verify coordinator control"
2545
2546 test_103() {
2547         # test needs a running copytool
2548         copytool_setup
2549
2550         local i=""
2551         local fid=""
2552
2553         mkdir -p $DIR/$tdir
2554         for i in $(seq 1 20); do
2555                 fid=$(copy_file /etc/passwd $DIR/$tdir/$i)
2556         done
2557         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/*
2558
2559         cdt_purge
2560
2561         echo "Current requests"
2562         local res=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2563                         $HSM_PARAM.actions |\
2564                         grep -v CANCELED | grep -v SUCCEED | grep -v FAILED")
2565
2566         [[ -z "$res" ]] || error "Some request have not been canceled"
2567
2568         copytool_cleanup
2569 }
2570 run_test 103 "Purge all requests"
2571
2572 DATA=CEA
2573 DATAHEX='[434541]'
2574 test_104() {
2575         # test needs a running copytool
2576         copytool_setup
2577
2578         mkdir -p $DIR/$tdir
2579         local f=$DIR/$tdir/$tfile
2580         local fid=$(make_large_for_progress $f)
2581         # if cdt is on, it can serve too quickly the request
2582         cdt_disable
2583         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER --data $DATA $f
2584         local data1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2585                         $HSM_PARAM.actions |\
2586                         grep $fid | cut -f16 -d=")
2587         cdt_enable
2588
2589         [[ "$data1" == "$DATAHEX" ]] ||
2590                 error "Data field in records is ($data1) and not ($DATAHEX)"
2591
2592         copytool_cleanup
2593 }
2594 run_test 104 "Copy tool data field"
2595
2596 test_105() {
2597         mkdir -p $DIR/$tdir
2598         local i=""
2599
2600         cdt_disable
2601         for i in $(seq -w 1 10); do
2602                 cp /etc/passwd $DIR/$tdir/$i
2603                 $LFS hsm_archive $DIR/$tdir/$i
2604         done
2605         local reqcnt1=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2606                         $HSM_PARAM.actions |\
2607                         grep WAITING | wc -l")
2608         cdt_restart
2609         cdt_disable
2610         local reqcnt2=$(do_facet $SINGLEMDS "$LCTL get_param -n\
2611                         $HSM_PARAM.actions |\
2612                         grep WAITING | wc -l")
2613         cdt_enable
2614         cdt_purge
2615         [[ "$reqcnt1" == "$reqcnt2" ]] ||
2616                 error "Requests count after shutdown $reqcnt2 != "\
2617                       "before shutdown $reqcnt1"
2618 }
2619 run_test 105 "Restart of coordinator"
2620
2621 get_agent_by_uuid_mdt() {
2622         local uuid=$1
2623         local mdtidx=$2
2624         local mds=mds$(($mdtidx + 1))
2625         do_facet $mds "$LCTL get_param -n ${MDT_PREFIX}${mdtidx}.hsm.agents |\
2626                  grep $uuid"
2627 }
2628
2629 check_agent_registered_by_mdt() {
2630         local uuid=$1
2631         local mdtidx=$2
2632         local mds=mds$(($mdtidx + 1))
2633         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2634         if [[ ! -z "$agent" ]]; then
2635                 echo "found agent $agent on $mds"
2636         else
2637                 error "uuid $uuid not found in agent list on $mds"
2638         fi
2639 }
2640
2641 check_agent_unregistered_by_mdt() {
2642         local uuid=$1
2643         local mdtidx=$2
2644         local mds=mds$(($mdtidx + 1))
2645         local agent=$(get_agent_by_uuid_mdt $uuid $mdtidx)
2646         if [[ -z "$agent" ]]; then
2647                 echo "uuid not found in agent list on $mds"
2648         else
2649                 error "uuid found in agent list on $mds: $agent"
2650         fi
2651 }
2652
2653 check_agent_registered() {
2654         local uuid=$1
2655         local mdsno
2656         for mdsno in $(seq 1 $MDSCOUNT); do
2657                 check_agent_registered_by_mdt $uuid $((mdsno - 1))
2658         done
2659 }
2660
2661 check_agent_unregistered() {
2662         local uuid=$1
2663         local mdsno
2664         for mdsno in $(seq 1 $MDSCOUNT); do
2665                 check_agent_unregistered_by_mdt $uuid $((mdsno - 1))
2666         done
2667 }
2668
2669 test_106() {
2670         local uuid=$(do_rpc_nodes $(facet_active_host $SINGLEAGT) \
2671                 get_client_uuid $MOUNT | cut -d' ' -f2)
2672
2673         copytool_setup
2674         check_agent_registered $uuid
2675
2676         search_copytools || error "No copytool found"
2677
2678         copytool_cleanup
2679         check_agent_unregistered $uuid
2680
2681         copytool_setup
2682         check_agent_registered $uuid
2683
2684         copytool_cleanup
2685 }
2686 run_test 106 "Copytool register/unregister"
2687
2688 test_107() {
2689         # test needs a running copytool
2690         copytool_setup
2691         # create and archive file
2692         mkdir -p $DIR/$tdir
2693         local f1=$DIR/$tdir/$tfile
2694         local fid=$(copy_file /etc/passwd $f1)
2695         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
2696         wait_request_state $fid ARCHIVE SUCCEED
2697         # shutdown and restart MDS
2698         fail $SINGLEMDS
2699         # check the copytool still gets messages from MDT
2700         local f2=$DIR/$tdir/2
2701         local fid=$(copy_file /etc/passwd $f2)
2702         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f2
2703         # main check of this sanity: this request MUST succeed
2704         wait_request_state $fid ARCHIVE SUCCEED
2705         copytool_cleanup
2706 }
2707 run_test 107 "Copytool re-register after MDS restart"
2708
2709 policy_set_and_test()
2710 {
2711         local change="$1"
2712         local target="$2"
2713         do_facet $SINGLEMDS $LCTL set_param "$HSM_PARAM.policy=\\\"$change\\\""
2714         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2715         [[ "$policy" == "$target" ]] ||
2716                 error "Wrong policy after '$change': '$policy' != '$target'"
2717 }
2718
2719 test_109() {
2720         # to force default policy setting if error
2721         CDT_POLICY_HAD_CHANGED=true
2722
2723         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2724         local default="NonBlockingRestore [NoRetryAction]"
2725         [[ "$policy" == "$default" ]] ||
2726                 error "default policy has changed,"\
2727                       " '$policy' != '$default' update the test"
2728         policy_set_and_test "+NBR" "[NonBlockingRestore] [NoRetryAction]"
2729         policy_set_and_test "+NRA" "[NonBlockingRestore] [NoRetryAction]"
2730         policy_set_and_test "-NBR" "NonBlockingRestore [NoRetryAction]"
2731         policy_set_and_test "-NRA" "NonBlockingRestore NoRetryAction"
2732         policy_set_and_test "NRA NBR" "[NonBlockingRestore] [NoRetryAction]"
2733         # useless bacause we know but safer for futur changes to use real value
2734         local policy=$(do_facet $SINGLEMDS $LCTL get_param -n $HSM_PARAM.policy)
2735         echo "Next set_param must failed"
2736         policy_set_and_test "wrong" "$policy"
2737
2738         # return to default
2739         echo "Back to default policy"
2740         cdt_set_sanity_policy
2741 }
2742 run_test 109 "Policy display/change"
2743
2744 test_110a() {
2745         # test needs a running copytool
2746         copytool_setup
2747
2748         mkdir -p $DIR/$tdir
2749
2750         copy2archive /etc/passwd $tdir/$tfile
2751
2752         local f=$DIR/$tdir/$tfile
2753         import_file $tdir/$tfile $f
2754         local fid=$(path2fid $f)
2755
2756         cdt_set_non_blocking_restore
2757         md5sum $f
2758         local st=$?
2759
2760         # cleanup
2761         wait_request_state $fid RESTORE SUCCEED
2762         cdt_clear_non_blocking_restore
2763
2764         # Test result
2765         [[ $st == 1 ]] ||
2766                 error "md5sum returns $st != 1, "\
2767                         "should also perror ENODATA (No data available)"
2768
2769         copytool_cleanup
2770 }
2771 run_test 110a "Non blocking restore policy (import case)"
2772
2773 test_110b() {
2774         # test needs a running copytool
2775         copytool_setup
2776
2777         mkdir -p $DIR/$tdir
2778         local f=$DIR/$tdir/$tfile
2779         local fid=$(copy_file /etc/passwd $f)
2780         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2781         wait_request_state $fid ARCHIVE SUCCEED
2782         $LFS hsm_release $f
2783
2784         cdt_set_non_blocking_restore
2785         md5sum $f
2786         local st=$?
2787
2788         # cleanup
2789         wait_request_state $fid RESTORE SUCCEED
2790         cdt_clear_non_blocking_restore
2791
2792         # Test result
2793         [[ $st == 1 ]] ||
2794                 error "md5sum returns $st != 1, "\
2795                         "should also perror ENODATA (No data available)"
2796
2797         copytool_cleanup
2798 }
2799 run_test 110b "Non blocking restore policy (release case)"
2800
2801 test_111a() {
2802         # test needs a running copytool
2803         copytool_setup
2804
2805         mkdir -p $DIR/$tdir
2806         copy2archive /etc/passwd $tdir/$tfile
2807
2808         local f=$DIR/$tdir/$tfile
2809
2810         import_file $tdir/$tfile $f
2811         local fid=$(path2fid $f)
2812
2813         cdt_set_no_retry
2814
2815         copytool_remove_backend $fid
2816
2817         $LFS hsm_restore $f
2818         wait_request_state $fid RESTORE FAILED
2819         local st=$?
2820
2821         # cleanup
2822         cdt_clear_no_retry
2823
2824         # Test result
2825         [[ $st == 0 ]] || error "Restore does not failed"
2826
2827         copytool_cleanup
2828 }
2829 run_test 111a "No retry policy (import case), restore will error"\
2830               " (No such file or directory)"
2831
2832 test_111b() {
2833         # test needs a running copytool
2834         copytool_setup
2835
2836         mkdir -p $DIR/$tdir
2837         local f=$DIR/$tdir/$tfile
2838         local fid=$(copy_file /etc/passwd $f)
2839         cdt_set_no_retry
2840         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2841         wait_request_state $fid ARCHIVE SUCCEED
2842         $LFS hsm_release $f
2843
2844         copytool_remove_backend $fid
2845
2846         $LFS hsm_restore $f
2847         wait_request_state $fid RESTORE FAILED
2848         local st=$?
2849
2850         # cleanup
2851         cdt_clear_no_retry
2852
2853         # Test result
2854         [[ $st == 0 ]] || error "Restore does not failed"
2855
2856         copytool_cleanup
2857 }
2858 run_test 111b "No retry policy (release case), restore will error"\
2859               " (No such file or directory)"
2860
2861 test_112() {
2862         # test needs a running copytool
2863         copytool_setup
2864
2865         mkdir -p $DIR/$tdir
2866         local f=$DIR/$tdir/$tfile
2867         local fid=$(copy_file /etc/passwd $f)
2868         cdt_disable
2869         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2870         local l=$($LFS hsm_action $f)
2871         echo $l
2872         local res=$(echo $l | cut -f 2- -d" " | grep ARCHIVE)
2873
2874         # cleanup
2875         cdt_enable
2876         wait_request_state $fid ARCHIVE SUCCEED
2877
2878         # Test result
2879         [[ ! -z "$res" ]] || error "action is $l which is not an ARCHIVE"
2880
2881         copytool_cleanup
2882 }
2883 run_test 112 "State of recorded request"
2884
2885 test_200() {
2886         # test needs a running copytool
2887         copytool_setup
2888
2889         mkdir -p $DIR/$tdir
2890         local f=$DIR/$tdir/$tfile
2891         local fid=$(make_large_for_cancel $f)
2892         # test with cdt on is made in test_221
2893         cdt_disable
2894         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2895         $LFS hsm_cancel $f
2896         cdt_enable
2897         wait_request_state $fid ARCHIVE CANCELED
2898         wait_request_state $fid CANCEL SUCCEED
2899
2900         copytool_cleanup
2901 }
2902 run_test 200 "Register/Cancel archive"
2903
2904 test_201() {
2905         # test needs a running copytool
2906         copytool_setup
2907
2908         mkdir -p $DIR/$tdir
2909         local f=$DIR/$tdir/$tfile
2910         make_archive $tdir/$tfile
2911         import_file $tdir/$tfile $f
2912         local fid=$(path2fid $f)
2913
2914         # test with cdt on is made in test_222
2915         cdt_disable
2916         $LFS hsm_restore $f
2917         $LFS hsm_cancel $f
2918         cdt_enable
2919         wait_request_state $fid RESTORE CANCELED
2920         wait_request_state $fid CANCEL SUCCEED
2921
2922         copytool_cleanup
2923 }
2924 run_test 201 "Register/Cancel restore"
2925
2926 test_202() {
2927         # test needs a running copytool
2928         copytool_setup
2929
2930         mkdir -p $DIR/$tdir
2931         local f=$DIR/$tdir/$tfile
2932         local fid=$(make_large_for_progress $f)
2933         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2934         wait_request_state $fid ARCHIVE SUCCEED
2935
2936         cdt_disable
2937         $LFS hsm_remove $f
2938         $LFS hsm_cancel $f
2939         cdt_enable
2940         wait_request_state $fid REMOVE CANCELED
2941
2942         copytool_cleanup
2943 }
2944 run_test 202 "Register/Cancel remove"
2945
2946 test_220() {
2947         # test needs a running copytool
2948         copytool_setup
2949
2950         mkdir -p $DIR/$tdir
2951
2952         local f=$DIR/$tdir/$tfile
2953         local fid=$(copy_file /etc/passwd $f)
2954
2955         changelog_setup
2956
2957         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2958         wait_request_state $fid ARCHIVE SUCCEED
2959
2960         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2961         changelog_cleanup
2962
2963         local target=0x0
2964         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2965
2966         copytool_cleanup
2967 }
2968 run_test 220 "Changelog for archive"
2969
2970 test_221() {
2971         # test needs a running copytool
2972         copytool_setup
2973
2974         mkdir -p $DIR/$tdir
2975
2976         local f=$DIR/$tdir/$tfile
2977         local fid=$(make_large_for_cancel $f)
2978
2979         changelog_setup
2980
2981         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
2982         wait_request_state $fid ARCHIVE STARTED
2983         $LFS hsm_cancel $f
2984         wait_request_state $fid ARCHIVE CANCELED
2985         wait_request_state $fid CANCEL SUCCEED
2986
2987         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
2988
2989         local target=0x7d
2990         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
2991
2992         cleanup
2993 }
2994 run_test 221 "Changelog for archive canceled"
2995
2996 test_222a() {
2997         # test needs a running copytool
2998         copytool_setup
2999
3000         mkdir -p $DIR/$tdir
3001         copy2archive /etc/passwd $tdir/$tfile
3002
3003         local f=$DIR/$tdir/$tfile
3004         import_file $tdir/$tfile $f
3005         local fid=$(path2fid $f)
3006
3007         changelog_setup
3008
3009         $LFS hsm_restore $f
3010         wait_request_state $fid RESTORE SUCCEED
3011
3012         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3013
3014         local target=0x80
3015         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3016
3017         cleanup
3018 }
3019 run_test 222a "Changelog for explicit restore"
3020
3021 test_222b() {
3022         # test needs a running copytool
3023         copytool_setup
3024
3025         mkdir -p $DIR/$tdir
3026         local f=$DIR/$tdir/$tfile
3027         local fid=$(copy_file /etc/passwd $f)
3028
3029         changelog_setup
3030         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3031         wait_request_state $fid ARCHIVE SUCCEED
3032         $LFS hsm_release $f
3033
3034         md5sum $f
3035
3036         wait_request_state $fid RESTORE SUCCEED
3037
3038         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3039
3040         local target=0x80
3041         [[ $flags == $target ]] || error "Changelog flag is $flags not $target"
3042
3043         cleanup
3044 }
3045 run_test 222b "Changelog for implicit restore"
3046
3047 test_223a() {
3048         # test needs a running copytool
3049         copytool_setup
3050
3051         mkdir -p $DIR/$tdir
3052
3053         local f=$DIR/$tdir/$tfile
3054         make_archive $tdir/$tfile
3055
3056         changelog_setup
3057
3058         import_file $tdir/$tfile $f
3059         local fid=$(path2fid $f)
3060
3061         $LFS hsm_restore $f
3062         wait_request_state $fid RESTORE STARTED
3063         $LFS hsm_cancel $f
3064         wait_request_state $fid RESTORE CANCELED
3065         wait_request_state $fid CANCEL SUCCEED
3066
3067         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3068
3069         local target=0xfd
3070         [[ $flags == $target ]] ||
3071                 error "Changelog flag is $flags not $target"
3072
3073         cleanup
3074 }
3075 run_test 223a "Changelog for restore canceled (import case)"
3076
3077 test_223b() {
3078         # test needs a running copytool
3079         copytool_setup
3080
3081         mkdir -p $DIR/$tdir
3082
3083         local f=$DIR/$tdir/$tfile
3084         local fid=$(make_large_for_progress $f)
3085
3086         changelog_setup
3087         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3088         wait_request_state $fid ARCHIVE SUCCEED
3089         $LFS hsm_release $f
3090         $LFS hsm_restore $f
3091         wait_request_state $fid RESTORE STARTED
3092         $LFS hsm_cancel $f
3093         wait_request_state $fid RESTORE CANCELED
3094         wait_request_state $fid CANCEL SUCCEED
3095
3096         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -1)
3097
3098         local target=0xfd
3099         [[ $flags == $target ]] ||
3100                 error "Changelog flag is $flags not $target"
3101
3102         cleanup
3103 }
3104 run_test 223b "Changelog for restore canceled (release case)"
3105
3106 test_224() {
3107         # test needs a running copytool
3108         copytool_setup
3109
3110         mkdir -p $DIR/$tdir
3111
3112         local f=$DIR/$tdir/$tfile
3113         local fid=$(copy_file /etc/passwd $f)
3114
3115         changelog_setup
3116         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3117         wait_request_state $fid ARCHIVE SUCCEED
3118
3119         $LFS hsm_remove $f
3120         wait_request_state $fid REMOVE SUCCEED
3121
3122         local flags=$(changelog_get_flags ${MDT[0]} HSM $fid | tail -n 1)
3123
3124         local target=0x200
3125         [[ $flags == $target ]] ||
3126                 error "Changelog flag is $flags not $target"
3127
3128         cleanup
3129 }
3130 run_test 224 "Changelog for remove"
3131
3132 test_225() {
3133         # test needs a running copytool
3134         copytool_setup
3135
3136         # test is not usable because remove request is too fast
3137         # so it is always finished before cancel can be done ...
3138         echo "Test disabled"
3139         copytool_cleanup
3140         return 0
3141
3142         mkdir -p $DIR/$tdir
3143         local f=$DIR/$tdir/$tfile
3144         local fid=$(make_large_for_progress $f)
3145
3146         changelog_setup
3147         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3148         wait_request_state $fid ARCHIVE SUCCEED
3149
3150         # if cdt is on, it can serve too quickly the request
3151         cdt_disable
3152         $LFS hsm_remove $f
3153         $LFS hsm_cancel $f
3154         cdt_enable
3155         wait_request_state $fid REMOVE CANCELED
3156         wait_request_state $fid CANCEL SUCCEED
3157
3158         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3159         local flags=$($LFS changelog ${MDT[0]} | grep HSM | grep $fid |
3160                 tail -n 1 | awk '{print $5}')
3161
3162         local target=0x27d
3163         [[ $flags == $target ]] ||
3164                 error "Changelog flag is $flags not $target"
3165
3166         cleanup
3167 }
3168 run_test 225 "Changelog for remove canceled"
3169
3170 test_226() {
3171         # test needs a running copytool
3172         copytool_setup
3173
3174         mkdir -p $DIR/$tdir
3175
3176         local f1=$DIR/$tdir/$tfile-1
3177         local f2=$DIR/$tdir/$tfile-2
3178         local f3=$DIR/$tdir/$tfile-3
3179         local fid1=$(copy_file /etc/passwd $f1)
3180         local fid2=$(copy_file /etc/passwd $f2)
3181         copy_file /etc/passwd $f3
3182
3183         changelog_setup
3184         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f1
3185         wait_request_state $fid1 ARCHIVE SUCCEED
3186
3187         $LFS hsm_archive $f2
3188         wait_request_state $fid2 ARCHIVE SUCCEED
3189
3190         rm $f1 || error "rm $f1 failed"
3191
3192         local flags=$(changelog_get_flags ${MDT[0]} UNLNK $fid1)
3193
3194         local target=0x3
3195         [[ $flags == $target ]] ||
3196                 error "Changelog flag is $flags not $target"
3197
3198         mv $f3 $f2 || error "mv $f3 $f2 failed"
3199
3200         flags=$(changelog_get_flags ${MDT[0]} RENME $fid2)
3201
3202         target=0x3
3203         [[ $flags == $target ]] ||
3204                 error "Changelog flag is $flags not $target"
3205
3206         cleanup
3207 }
3208 run_test 226 "changelog for last rm/mv with exiting archive"
3209
3210 check_flags_changes() {
3211         local f=$1
3212         local fid=$2
3213         local hsm_flag=$3
3214         local fst=$4
3215         local cnt=$5
3216
3217         local target=0x280
3218         $LFS hsm_set --$hsm_flag $f ||
3219                 error "Cannot set $hsm_flag on $f"
3220         local flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3221         local seen=${#flags[*]}
3222         cnt=$((fst + cnt))
3223         [[ $seen == $cnt ]] ||
3224                 error "set $hsm_flag: Changelog events $seen != $cnt"
3225         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3226                 error "set $hsm_flag: Changelog flags are "\
3227                         "${flags[$((cnt - 1))]} not $target"
3228
3229         $LFS hsm_clear --$hsm_flag $f ||
3230                 error "Cannot clear $hsm_flag on $f"
3231         flags=($(changelog_get_flags ${MDT[0]} HSM $fid))
3232         seen=${#flags[*]}
3233         cnt=$(($cnt + 1))
3234         [[ $cnt == $seen ]] ||
3235                 error "clear $hsm_flag: Changelog events $seen != $cnt"
3236
3237         [[ ${flags[$((cnt - 1))]} == $target ]] ||
3238                 error "clear $hsm_flag: Changelog flag is "\
3239                         "${flags[$((cnt - 1))]} not $target"
3240 }
3241
3242 test_227() {
3243         # test needs a running copytool
3244         copytool_setup
3245         changelog_setup
3246
3247         mkdir -p $DIR/$tdir
3248         typeset -a flags
3249
3250         for i in norelease noarchive exists archived
3251         do
3252                 local f=$DIR/$tdir/$tfile-$i
3253                 local fid=$(copy_file /etc/passwd $f)
3254                 check_flags_changes $f $fid $i 0 1
3255         done
3256
3257         f=$DIR/$tdir/$tfile---lost
3258         fid=$(copy_file /etc/passwd $f)
3259         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3260         wait_request_state $fid ARCHIVE SUCCEED
3261         check_flags_changes $f $fid lost 3 1
3262
3263         cleanup
3264 }
3265 run_test 227 "changelog when explicit setting of HSM flags"
3266
3267 test_228() {
3268         # test needs a running copytool
3269         copytool_setup
3270
3271         local fid=$(make_small_sync $DIR/$tfile)
3272         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tfile
3273         wait_request_state $fid ARCHIVE SUCCEED
3274
3275         $LFS hsm_release $DIR/$tfile
3276         check_hsm_flags $DIR/$tfile "0x0000000d"
3277
3278         filefrag $DIR/$tfile | grep " 1 extent found" ||
3279                 error "filefrag on released file must return only one extent"
3280
3281         # only newer versions of cp detect sparse files by stat/FIEMAP
3282         # (LU-2580)
3283         cp --sparse=auto $DIR/$tfile $DIR/$tfile.2 ||
3284                 error "copying $DIR/$tfile"
3285         cmp $DIR/$tfile $DIR/$tfile.2 || error "comparing copied $DIR/$tfile"
3286
3287         $LFS hsm_release $DIR/$tfile
3288         check_hsm_flags $DIR/$tfile "0x0000000d"
3289
3290         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
3291
3292         tar cf - --sparse $DIR/$tfile | tar xvf - -C $DIR/$tdir ||
3293                 error "tar failed"
3294         cmp $DIR/$tfile $DIR/$tdir/$DIR/$tfile ||
3295                 error "comparing untarred $DIR/$tfile"
3296
3297         rm -f $DIR/$tfile $DIR/$tfile.2 ||
3298                 error "rm $DIR/$tfile or $DIR/$tfile.2 failed"
3299         copytool_cleanup
3300 }
3301 run_test 228 "On released file, return extend to FIEMAP. For [cp,tar] --sparse"
3302
3303 test_250() {
3304         # test needs a running copytool
3305         copytool_setup
3306
3307         mkdir -p $DIR/$tdir
3308         local maxrequest=$(get_hsm_param max_requests)
3309         local rqcnt=$(($maxrequest * 3))
3310         local i=""
3311
3312         cdt_disable
3313         for i in $(seq -w 1 $rqcnt); do
3314                 rm -f $DIR/$tdir/$i
3315                 dd if=/dev/urandom of=$DIR/$tdir/$i bs=1M count=10 conv=fsync
3316         done
3317         # we do it in 2 steps, so all requests arrive at the same time
3318         for i in $(seq -w 1 $rqcnt); do
3319                 $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $DIR/$tdir/$i
3320         done
3321         cdt_enable
3322         local cnt=$rqcnt
3323         local wt=$rqcnt
3324         while [[ $cnt != 0 || $wt != 0 ]]; do
3325                 sleep 1
3326                 cnt=$(do_facet $SINGLEMDS "$LCTL get_param -n\
3327                         $HSM_PARAM.actions |\
3328                         grep STARTED | grep -v CANCEL | wc -l")
3329                 [[ $cnt -le $maxrequest ]] ||
3330                         error "$cnt > $maxrequest too many started requests"
3331                 wt=$(do_facet $SINGLEMDS "$LCTL get_param\
3332                         $HSM_PARAM.actions |\
3333                         grep WAITING | wc -l")
3334                 echo "max=$maxrequest started=$cnt waiting=$wt"
3335         done
3336
3337         copytool_cleanup
3338 }
3339 run_test 250 "Coordinator max request"
3340
3341 test_251() {
3342         # test needs a running copytool
3343         copytool_setup
3344
3345         mkdir -p $DIR/$tdir
3346         local f=$DIR/$tdir/$tfile
3347         local fid=$(make_large_for_cancel $f)
3348
3349         cdt_disable
3350         # to have a short test
3351         local old_to=$(get_hsm_param active_request_timeout)
3352         set_hsm_param active_request_timeout 4
3353         # to be sure the cdt will wake up frequently so
3354         # it will be able to cancel the "old" request
3355         local old_loop=$(get_hsm_param loop_period)
3356         set_hsm_param loop_period 2
3357         cdt_enable
3358
3359         $LFS hsm_archive --archive $HSM_ARCHIVE_NUMBER $f
3360         wait_request_state $fid ARCHIVE STARTED
3361         sleep 5
3362         wait_request_state $fid ARCHIVE CANCELED
3363
3364         set_hsm_param active_request_timeout $old_to
3365         set_hsm_param loop_period $old_loop
3366
3367         copytool_cleanup
3368 }
3369 run_test 251 "Coordinator request timeout"
3370
3371 test_300() {
3372         # the only way to test ondisk conf is to restart MDS ...
3373         echo "Stop coordinator and remove coordinator state at mount"
3374         # stop coordinator
3375         cdt_shutdown
3376         # clean on disk conf set by default
3377         cdt_clear_mount_state
3378         cdt_check_state stopped
3379
3380         # check cdt still off after umount/remount
3381         fail $SINGLEMDS
3382         cdt_check_state stopped
3383
3384         echo "Set coordinator start at mount, and start coordinator"
3385         cdt_set_mount_state enabled
3386
3387         # check cdt is on
3388         cdt_check_state enabled
3389
3390         # check cdt still on after umount/remount
3391         fail $SINGLEMDS
3392         cdt_check_state enabled
3393
3394         # we are back to original state (cdt started at mount)
3395 }
3396 run_test 300 "On disk coordinator state kept between MDT umount/mount"
3397
3398 test_301() {
3399         local ai=$(get_hsm_param default_archive_id)
3400         local new=$((ai + 1))
3401
3402         set_hsm_param default_archive_id $new -P
3403         fail $SINGLEMDS
3404         local res=$(get_hsm_param default_archive_id)
3405
3406         # clear value
3407         set_hsm_param default_archive_id "" "-P -d"
3408
3409         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3410 }
3411 run_test 301 "HSM tunnable are persistent"
3412
3413 test_302() {
3414         local ai=$(get_hsm_param default_archive_id)
3415         local new=$((ai + 1))
3416
3417         # stop coordinator
3418         cdt_shutdown
3419
3420         set_hsm_param default_archive_id $new -P
3421
3422         local mdtno
3423         for mdtno in $(seq 1 $MDSCOUNT); do
3424                 fail mds${mdtno}
3425         done
3426
3427         # check cdt is on
3428         cdt_check_state enabled
3429
3430         local res=$(get_hsm_param default_archive_id)
3431
3432         # clear value
3433         set_hsm_param default_archive_id "" "-P -d"
3434
3435         [[ $new == $res ]] || error "Value after MDS restart is $res != $new"
3436 }
3437 run_test 302 "HSM tunnable are persistent when CDT is off"
3438
3439 test_400() {
3440         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3441
3442         copytool_setup
3443
3444         mkdir -p $DIR/$tdir
3445
3446         local dir_mdt0=$DIR/$tdir/mdt0
3447         local dir_mdt1=$DIR/$tdir/mdt1
3448
3449         # create 1 dir per MDT
3450         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3451         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3452
3453         # create 1 file in each MDT
3454         local fid1=$(make_small $dir_mdt0/$tfile)
3455         local fid2=$(make_small $dir_mdt1/$tfile)
3456
3457         # check that hsm request on mdt0 is sent to the right MDS
3458         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3459         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3460                 echo "archive successful on mdt0"
3461
3462         # check that hsm request on mdt1 is sent to the right MDS
3463         $LFS hsm_archive $dir_mdt1/$tfile || error "lfs hsm_archive"
3464         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3465                 echo "archive successful on mdt1"
3466
3467         copytool_cleanup
3468         # clean test files and directories
3469         rm -rf $dir_mdt0 $dir_mdt1
3470 }
3471 run_test 400 "Single request is sent to the right MDT"
3472
3473 test_401() {
3474         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3475
3476         copytool_setup
3477
3478         mkdir -p $DIR/$tdir
3479
3480         local dir_mdt0=$DIR/$tdir/mdt0
3481         local dir_mdt1=$DIR/$tdir/mdt1
3482
3483         # create 1 dir per MDT
3484         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3485         $LFS mkdir -i 1 $dir_mdt1 || error "lfs mkdir"
3486
3487         # create 1 file in each MDT
3488         local fid1=$(make_small $dir_mdt0/$tfile)
3489         local fid2=$(make_small $dir_mdt1/$tfile)
3490
3491         # check that compound requests are shunt to the rights MDTs
3492         $LFS hsm_archive $dir_mdt0/$tfile $dir_mdt1/$tfile ||
3493                 error "lfs hsm_archive"
3494         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3495                 echo "archive successful on mdt0"
3496         wait_request_state $fid2 ARCHIVE SUCCEED 1 &&
3497                 echo "archive successful on mdt1"
3498
3499         copytool_cleanup
3500         # clean test files and directories
3501         rm -rf $dir_mdt0 $dir_mdt1
3502 }
3503 run_test 401 "Compound requests split and sent to their respective MDTs"
3504
3505 mdc_change_state() # facet, MDT_pattern, activate|deactivate
3506 {
3507         local facet=$1
3508         local pattern="$2"
3509         local state=$3
3510         local node=$(facet_active_host $facet)
3511         local mdc
3512         for mdc in $(do_facet $facet "$LCTL dl | grep -E ${pattern}-mdc" |
3513                         awk '{print $4}'); do
3514                 echo "$3 $mdc on $node"
3515                 do_facet $facet "$LCTL --device $mdc $state" || return 1
3516         done
3517 }
3518
3519 test_402() {
3520         # make sure there is no running copytool
3521         copytool_cleanup
3522
3523         # deactivate all mdc on agent1
3524         mdc_change_state $SINGLEAGT "MDT000." "deactivate"
3525
3526         copytool_setup $SINGLEAGT
3527
3528         check_agent_unregistered "uuid" # match any agent
3529
3530         # no expected running copytool
3531         search_copytools $agent && error "Copytool start should have failed"
3532
3533         # reactivate MDCs
3534         mdc_change_state $SINGLEAGT "MDT000." "activate"
3535 }
3536 run_test 402 "Copytool start fails if all MDTs are inactive"
3537
3538 test_403() {
3539         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3540
3541         # make sure there is no running copytool
3542         copytool_cleanup
3543
3544         local agent=$(facet_active_host $SINGLEAGT)
3545         local uuid=$(do_rpc_nodes $agent get_client_uuid | cut -d' ' -f2)
3546
3547         # deactivate all mdc for MDT0001
3548         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3549
3550         copytool_setup
3551         # check the agent is registered on MDT0000, and not on MDT0001
3552         check_agent_registered_by_mdt $uuid 0
3553         check_agent_unregistered_by_mdt $uuid 1
3554
3555         # check running copytool process
3556         search_copytools $agent || error "No running copytools on $agent"
3557
3558         # reactivate all mdc for MDT0001
3559         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3560
3561         # make sure the copytool is now registered to all MDTs
3562         check_agent_registered $uuid
3563
3564         copytool_cleanup
3565 }
3566 run_test 403 "Copytool starts with inactive MDT and register on reconnect"
3567
3568 test_404() {
3569         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3570
3571         copytool_setup
3572
3573         # create files on both MDT0000 and MDT0001
3574         mkdir -p $DIR/$tdir
3575
3576         local dir_mdt0=$DIR/$tdir/mdt0
3577         $LFS mkdir -i 0 $dir_mdt0 || error "lfs mkdir"
3578
3579         # create 1 file on mdt0
3580         local fid1=$(make_small $dir_mdt0/$tfile)
3581
3582         # deactivate all mdc for MDT0001
3583         mdc_change_state $SINGLEAGT "MDT0001" "deactivate"
3584
3585         # send an HSM request for files in MDT0000
3586         $LFS hsm_archive $dir_mdt0/$tfile || error "lfs hsm_archive"
3587
3588         # check for completion of files in MDT0000
3589         wait_request_state $fid1 ARCHIVE SUCCEED 0 &&
3590                 echo "archive successful on mdt0"
3591
3592         # reactivate all mdc for MDT0001
3593         mdc_change_state $SINGLEAGT "MDT0001" "activate"
3594
3595         copytool_cleanup
3596         # clean test files and directories
3597         rm -rf $dir_mdt0
3598 }
3599 run_test 404 "Inactive MDT does not block requests for active MDTs"
3600
3601 copytool_cleanup
3602
3603 complete $SECONDS
3604 check_and_cleanup_lustre
3605 exit_status