Whamcloud - gitweb
LU-1346 libcfs: cleanup macros in kp30.h
[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 # Run test by setting NOSETUP=true when ltest has setup env for us
7 #
8 # exit on error
9 set -e
10 set +o monitor
11
12 SRCDIR=`dirname $0`
13 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/utils:$PATH:/sbin:/usr/sbin/
14
15 ONLY=${ONLY:-"$*"}
16 SANITY_HSM_EXCEPT=${SANITY_HSM_EXCEPT:-""}
17 ALWAYS_EXCEPT="$SANITY_HSM_EXCEPT"
18 # bug number for skipped test:
19 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
20
21 [ "$ALWAYS_EXCEPT$EXCEPT" ] &&
22         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
23
24 TMP=${TMP:-/tmp}
25
26 ORIG_PWD=${PWD}
27 MCREATE=${MCREATE:-mcreate}
28
29 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
30
31 . $LUSTRE/tests/test-framework.sh
32 init_test_env $@
33 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
34 init_logging
35
36 SANITYLOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
37 FAIL_ON_ERROR=false
38
39 [ "$SANITYLOG" ] && rm -f $SANITYLOG || true
40 check_and_setup_lustre
41
42 if [ $MDSCOUNT -ge 2 ]; then
43         skip_env "Only run with single MDT for now" && exit
44 fi
45
46 if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.61) ]; then
47         skip_env "Need MDS version at least 2.3.61" && exit
48 fi
49
50 DIR=${DIR:-$MOUNT}
51 assert_DIR
52
53 build_test_filter
54
55 # $RUNAS_ID may get set incorrectly somewhere else
56 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
57         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
58
59 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
60
61 copytool_cleanup() {
62         # TODO: add copytool cleanup code here!
63         return
64 }
65
66 copytool_setup() {
67         rm -rf $HSM_ARCHIVE
68         mkdir -p $HSM_ARCHIVE
69 }
70
71 fail() {
72         copytool_cleanup
73         error $*
74 }
75
76 export HSMTOOL=${HSMTOOL:-"lhsmtool_posix"}
77 export HSMTOOL_VERBOSE=${HSMTOOL_VERBOSE:-""}
78 HSM_ARCHIVE=${HSM_ARCHIVE:-$TMP/arc}
79 HSM_ARCHIVE_NUMBER=2
80
81 path2fid() {
82         $LFS path2fid $1 | tr -d '[]'
83 }
84
85 make_small() {
86         local file2=${1/$DIR/$DIR}
87         dd if=/dev/urandom of=$file2 count=2 bs=1M
88                 path2fid $1
89 }
90
91 test_1() {
92         mkdir -p $DIR/$tdir
93         chmod 777 $DIR/$tdir
94
95         TESTFILE=$DIR/$tdir/file
96         $RUNAS touch $TESTFILE
97
98         # User flags
99         local state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
100         [[ $state == "(0x00000000)" ]] ||
101                 error "wrong initial hsm state $state"
102
103         $RUNAS $LFS hsm_set --norelease $TESTFILE ||
104                 error "user could not change hsm flags"
105         state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
106         [[ $state == "(0x00000010)" ]] ||
107                 error "wrong hsm state $state, should be: --norelease"
108
109         $RUNAS $LFS hsm_clear --norelease $TESTFILE ||
110                 error "user could not clear hsm flags"
111         state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
112         [[ $state == "(0x00000000)" ]] ||
113                 error "wrong hsm state $state, should be empty"
114
115         # User could not change those flags...
116         $RUNAS $LFS hsm_set --exists $TESTFILE &&
117                 error "user should not set this flag"
118         state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
119         [[ $state == "(0x00000000)" ]] ||
120                 error "wrong hsm state $state, should be empty"
121
122         # ...but root can
123         $LFS hsm_set --exists $TESTFILE ||
124                 error "root could not change hsm flags"
125         state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
126         [[ $state == "(0x00000001)" ]] ||
127                 error "wrong hsm state $state, should be: --exists"
128
129         $LFS hsm_clear --exists $TESTFILE ||
130                 error "root could not clear hsm state"
131         state=$($RUNAS $LFS hsm_state $TESTFILE | cut -f 2 -d" ")
132         [[ $state == "(0x00000000)" ]] ||
133                 error "wrong hsm state $state, should be empty"
134 }
135 run_test 1 "lfs hsm flags root/non-root access"
136
137 test_2() {
138         mkdir -p $DIR/$tdir
139         TESTFILE=$DIR/$tdir/file
140         touch $TESTFILE
141
142         # New files are not dirty
143         local state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
144         [[ $state == "(0x00000000)" ]] ||
145                 error "wrong hsm state $state, should be empty"
146
147         # For test, we simulate an archived file.
148         $LFS hsm_set --exists $TESTFILE || error "user could not change hsm flags"
149         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
150         [[ $state == "(0x00000001)" ]] ||
151                 error "wrong hsm state $state, should be: --exists"
152
153         # chmod do not put the file dirty
154         chmod 600 $TESTFILE || error "could not chmod test file"
155         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
156         [[ $state == "(0x00000001)" ]] ||
157                 error "wrong hsm state $state, should be: --exists"
158
159         # chown do not put the file dirty
160         chown $RUNAS_ID $TESTFILE || error "could not chown test file"
161         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
162         [[ $state == "(0x00000001)" ]] ||
163                 error "wrong hsm state $state, should be: --exists"
164
165         # truncate put the file dirty
166         $TRUNCATE $TESTFILE 1 || error "could not truncate test file"
167         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
168         [[ $state == "(0x00000003)" ]] ||
169                 error "wrong hsm state $state, should be 0x00000003"
170
171         $LFS hsm_clear --dirty $TESTFILE || error "could not clear hsm flags"
172         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
173         [[ $state == "(0x00000001)" ]] ||
174                 error "wrong hsm state $state, should be: --exists"
175 }
176 run_test 2 "Check file dirtyness when doing setattr"
177
178 test_3() {
179         mkdir -p $DIR/$tdir
180         TESTFILE=$DIR/$tdir/file
181
182         # New files are not dirty
183         cp -p /etc/passwd $TESTFILE
184         local state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
185         [[ $state == "(0x00000000)" ]] ||
186                 error "wrong hsm state $state, should be empty"
187
188         # For test, we simulate an archived file.
189         $LFS hsm_set --exists $TESTFILE ||
190                 error "user could not change hsm flags"
191         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
192         [[ $state == "(0x00000001)" ]] ||
193                 error "wrong hsm state $state, should be: --exists"
194
195         # Reading a file, does not set dirty
196         cat $TESTFILE > /dev/null || error "could not read file"
197         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
198         [[ $state == "(0x00000001)" ]] ||
199                 error "wrong hsm state $state, should be: --exists"
200
201         # Open for write without modifying data, does not set dirty
202         openfile -f O_WRONLY $TESTFILE || error "could not open test file"
203         state=$($LFS hsm_state $TESTFILE | cut -f 2 -d" ")
204         [[ $state == "(0x00000001)" ]] ||
205                 error "wrong hsm state $state, should be: --exists"
206
207         # Append to a file sets it dirty
208         cp -p /etc/passwd $TESTFILE.append || error "could not create file"
209         $LFS hsm_set --exists $TESTFILE.append ||
210                 error "user could not change hsm flags"
211         dd if=/etc/passwd of=$TESTFILE.append bs=1 count=3 \
212            conv=notrunc oflag=append status=noxfer ||
213                 error "could not append to test file"
214         state=$($LFS hsm_state $TESTFILE.append | cut -f 2 -d" ")
215         [[ $state == "(0x00000003)" ]] ||
216                 error "wrong hsm state $state, should be 0x00000003"
217
218         # Modify a file sets it dirty
219         cp -p /etc/passwd $TESTFILE.modify || error "could not create file"
220         $LFS hsm_set --exists $TESTFILE.modify ||
221                 error "user could not change hsm flags"
222         dd if=/dev/zero of=$TESTFILE.modify bs=1 count=3 \
223            conv=notrunc status=noxfer ||
224                 error "could not modify test file"
225         state=$($LFS hsm_state $TESTFILE.modify | cut -f 2 -d" ")
226         [[ $state == "(0x00000003)" ]] ||
227                 error "wrong hsm state $state, should be 0x00000003"
228
229         # Open O_TRUNC sets dirty
230         cp -p /etc/passwd $TESTFILE.trunc || error "could not create file"
231         $LFS hsm_set --exists $TESTFILE.trunc ||
232                 error "user could not change hsm flags"
233         cp /etc/group $TESTFILE.trunc || error "could not override a file"
234         state=$($LFS hsm_state $TESTFILE.trunc | cut -f 2 -d" ")
235         [[ $state == "(0x00000003)" ]] ||
236                 error "wrong hsm state $state, should be 0x00000003"
237
238         # Mmapped a file sets dirty
239         cp -p /etc/passwd $TESTFILE.mmap || error "could not create file"
240         $LFS hsm_set --exists $TESTFILE.mmap ||
241                 error "user could not change hsm flags"
242         multiop $TESTFILE.mmap OSMWUc || error "could not mmap a file"
243         state=$($LFS hsm_state $TESTFILE.mmap | cut -f 2 -d" ")
244         [[ $state == "(0x00000003)" ]] ||
245                 error "wrong hsm state $state, should be 0x00000003"
246 }
247 run_test 3 "Check file dirtyness when opening for write"
248
249 test_11() {
250         mkdir -p $DIR/$tdir $HSM_ARCHIVE/$tdir
251         cp /etc/hosts $HSM_ARCHIVE/$tdir/$tfile
252         local f=$DIR/$tdir/$tfile
253         $HSMTOOL $HSMTOOL_VERBOSE --archive $HSM_ARCHIVE_NUMBER \
254                 --hsm_root $HSM_ARCHIVE --import $tdir/$tfile $f $MOUNT ||
255                 error "import failed"
256         echo -n "Verifying released state: "
257         $LFS hsm_state $f
258         $LFS hsm_state $f | grep -q "released exists archived" ||
259                 error "flags not set"
260         local LSZ=$(stat -c "%s" $f)
261         local ASZ=$(stat -c "%s" $HSM_ARCHIVE/$tdir/$tfile)
262         echo "Verifying imported size $LSZ=$ASZ"
263         [[ $LSZ -eq $ASZ ]] || error "Incorrect size $LSZ != $ASZ"
264         echo -n "Verifying released pattern: "
265         local PTRN=$($GETSTRIPE -L $f)
266         echo $PTRN
267         [[ $PTRN == 80000001 ]] || error "Is not released"
268         local fid=$(path2fid $f)
269         echo "Verifying new fid $fid in archive"
270         local AFILE=$(ls $HSM_ARCHIVE/*/*/*/*/*/*/$fid) || \
271                 error "fid $fid not in archive $HSM_ARCHIVE"
272 }
273 run_test 11 "Import a file"
274
275 test_20() {
276         mkdir -p $DIR/$tdir
277
278         local f=$DIR/$tdir/sample
279         touch $f
280
281         # Could not release a non-archived file
282         $LFS hsm_release $f && error "release should not succeed"
283
284         # For following tests, we must test them with HS_ARCHIVED set
285         $LFS hsm_set --exists --archived $f || error "could not add flag"
286
287         # Could not release a file if no-release is set
288         $LFS hsm_set --norelease $f || error "could not add flag"
289         $LFS hsm_release $f && error "release should not succeed"
290         $LFS hsm_clear --norelease $f || error "could not remove flag"
291
292         # Could not release a file if lost
293         $LFS hsm_set --lost $f || error "could not add flag"
294         $LFS hsm_release $f && error "release should not succeed"
295         $LFS hsm_clear --lost $f || error "could not remove flag"
296
297         # Could not release a file if dirty
298         $LFS hsm_set --dirty $f || error "could not add flag"
299         $LFS hsm_release $f && error "release should not succeed"
300         $LFS hsm_clear --dirty $f || error "could not remove flag"
301
302 }
303 run_test 20 "Release is not permitted"
304
305 test_21() {
306         # test needs a running copytool
307         copytool_setup
308
309         mkdir -p $DIR/$tdir
310         local f=$DIR/$tdir/test_release
311
312         # Create a file and check its states
313         local fid=$(make_small $f)
314         $LFS hsm_state $f | grep -q " (0x00000000)" ||
315                 fail "wrong clean hsm state"
316
317 #       $LFS hsm_archive $f || fail "could not archive file"
318 #       wait_request_state $fid ARCHIVE SUCCEED
319         $LFS hsm_set --archived --exist $f || fail "could not archive file"
320
321         [ $(stat -c "%b" $f) -ne "0" ] || fail "wrong block number"
322         local sz=$(stat -c "%s" $f)
323         [ $sz -ne "0" ] || fail "file size should not be zero"
324
325         # Release and check states
326         $LFS hsm_release $f || fail "could not release file"
327         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
328                 fail "wrong released hsm state"
329         [ $(stat -c "%b" $f) -eq "0" ] || fail "wrong block number"
330         [ $(stat -c "%s" $f) -eq $sz ] || fail "wrong file size"
331
332         # Check we can release an file without stripe info
333         f=$f.nolov
334         $MCREATE $f
335         fid=$(path2fid $f)
336         $LFS hsm_state $f | grep -q " (0x00000000)" ||
337                 fail "wrong clean hsm state"
338
339 #       $LFS hsm_archive $f || fail "could not archive file"
340 #       wait_request_state $fid ARCHIVE SUCCEED
341         $LFS hsm_set --archived --exist $f || fail "could not archive file"
342
343         # Release and check states
344         $LFS hsm_release $f || fail "could not release file"
345         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
346                 fail "wrong released hsm state"
347
348         # Release again a file that is already released is OK
349         $LFS hsm_release $f || fail "second release should succeed"
350         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
351                 fail "wrong released hsm state"
352
353         copytool_cleanup
354 }
355 run_test 21 "Simple release tests"
356
357 test_22() {
358         # test needs a running copytool
359         copytool_setup
360
361         mkdir -p $DIR/$tdir
362
363         local f=$DIR/$tdir/test_release
364         local swap=$DIR/$tdir/test_swap
365
366         # Create a file and check its states
367         local fid=$(make_small $f)
368         $LFS hsm_state $f | grep -q " (0x00000000)" ||
369                 fail "wrong clean hsm state"
370
371 #       $LFS hsm_archive $f || fail "could not archive file"
372 #       wait_request_state $fid ARCHIVE SUCCEED
373         $LFS hsm_set --archived --exist $f || fail "could not archive file"
374
375         # Release and check states
376         $LFS hsm_release $f || fail "could not release file"
377         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
378                 fail "wrong released hsm state"
379
380         make_small $swap || fail "could not create $swap"
381         $LFS swap_layouts $swap $f && fail "swap_layouts should failed"
382
383         true
384         copytool_cleanup
385 }
386 run_test 22 "Could not swap a release file"
387
388
389 test_23() {
390         # test needs a running copytool
391         copytool_setup
392
393         mkdir -p $DIR/$tdir
394
395         local f=$DIR/$tdir/test_mtime
396
397         # Create a file and check its states
398         local fid=$(make_small $f)
399         $LFS hsm_state $f | grep -q " (0x00000000)"  ||
400                 fail "wrong clean hsm state"
401
402 #       $LFS hsm_archive $f || fail "could not archive file"
403 #       wait_request_state $fid ARCHIVE SUCCEED
404         $LFS hsm_set --archived --exist $f || fail "could not archive file"
405
406         # Set modification time in the past
407         touch -m -a -d @978261179 $f
408
409         # Release and check states
410         $LFS hsm_release $f || fail "could not release file"
411         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
412                 fail "wrong released hsm state"
413         local MTIME=$(stat -c "%Y" $f)
414         local ATIME=$(stat -c "%X" $f)
415         [ $MTIME -eq "978261179" ] || fail "bad mtime: $MTIME"
416         [ $ATIME -eq "978261179" ] || fail "bad atime: $ATIME"
417
418         copytool_cleanup
419 }
420 run_test 23 "Release does not change a/mtime (utime)"
421
422 test_24() {
423         # test needs a running copytool
424         copytool_setup
425
426         mkdir -p $DIR/$tdir
427
428         local f=$DIR/$tdir/test_mtime
429
430         # Create a file and check its states
431         local fid=$(make_small $f)
432         $LFS hsm_state $f | grep -q " (0x00000000)" ||
433                 fail "wrong clean hsm state"
434
435         # ensure mtime is different
436         sleep 1
437         echo "append" >> $f
438         local MTIME=$(stat -c "%Y" $f)
439         local ATIME=$(stat -c "%X" $f)
440
441 #       $LFS hsm_archive $f || fail "could not archive file"
442 #       wait_request_state $fid ARCHIVE SUCCEED
443         $LFS hsm_set --archived --exist $f || fail "could not archive file"
444
445         # Release and check states
446         $LFS hsm_release $f || fail "could not release file"
447         $LFS hsm_state $f | grep -q " (0x0000000d)" ||
448                 fail "wrong released hsm state"
449
450         [ "$(stat -c "%Y" $f)" -eq "$MTIME" ] ||
451                 fail "mtime should be $MTIME"
452
453 #       [ "$(stat -c "%X" $f)" -eq "$ATIME" ] ||
454 #               fail "atime should be $ATIME"
455
456         copytool_cleanup
457 }
458 run_test 24 "Release does not change a/mtime (i/o)"
459
460 log "cleanup: ======================================================"
461 cd $ORIG_PWD
462 check_and_cleanup_lustre
463 echo '=========================== finished ==============================='
464 [ -f "$SANITYLOG" ] && cat $SANITYLOG && grep -q FAIL $SANITYLOG && exit 1 ||
465         true
466 echo "$0: completed"