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