Whamcloud - gitweb
a92bf03ee6d77df0c6403b88cd1fb9e7f8a141f3
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c     77k"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 ..
16 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
17
18 if $SHARED_KEY; then
19 # bug number for skipped tests: LU-9795 (all below)
20         ALWAYS_EXCEPT="$ALWAYS_EXCEPT   17n     60a     133g    300f"
21 fi
22
23 # Check Grants after these tests
24 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
25 SRCDIR=$(cd $(dirname $0); echo $PWD)
26 export PATH=$PATH:/sbin
27
28 TMP=${TMP:-/tmp}
29 OSC=${OSC:-"osc"}
30
31 CC=${CC:-cc}
32 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
33 CREATETEST=${CREATETEST:-createtest}
34 LFS=${LFS:-lfs}
35 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
36 LCTL=${LCTL:-lctl}
37 OPENFILE=${OPENFILE:-openfile}
38 OPENUNLINK=${OPENUNLINK:-openunlink}
39 export MULTIOP=${MULTIOP:-multiop}
40 READS=${READS:-"reads"}
41 MUNLINK=${MUNLINK:-munlink}
42 SOCKETSERVER=${SOCKETSERVER:-socketserver}
43 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
44 MEMHOG=${MEMHOG:-memhog}
45 DIRECTIO=${DIRECTIO:-directio}
46 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
47 DEF_STRIPE_COUNT=-1
48 CHECK_GRANT=${CHECK_GRANT:-"yes"}
49 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
50 export PARALLEL=${PARALLEL:-"no"}
51
52 export NAME=${NAME:-local}
53
54 SAVE_PWD=$PWD
55
56 CLEANUP=${CLEANUP:-:}
57 SETUP=${SETUP:-:}
58 TRACE=${TRACE:-""}
59 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
60 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
61 . $LUSTRE/tests/test-framework.sh
62 init_test_env $@
63 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
64 init_logging
65
66 if [[ $MDSCOUNT -gt 1 ]]; then
67         # bug number:    LU-11161
68         ALWAYS_EXCEPT+=" 160g"
69 fi
70
71 #                                  5          12          (min)"
72 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
73
74 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
75         # bug number for skipped test: LU-1957
76         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
77         #                                               13    (min)"
78         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
79 fi
80
81 # Get the SLES distro version
82 #
83 # Returns a version string that should only be used in comparing
84 # strings returned by version_code()
85 sles_version_code()
86 {
87         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
88
89         # All SuSE Linux versions have one decimal. version_code expects two
90         local sles_version=$version.0
91         version_code $sles_version
92 }
93
94 # Check if we are running on Ubuntu or SLES so we can make decisions on
95 # what tests to run
96 if [ -r /etc/SuSE-release ]; then
97         sles_version=$(sles_version_code)
98         [ $sles_version -lt $(version_code 11.4.0) ] &&
99                 # bug number for skipped test: LU-4341
100                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
101         [ $sles_version -lt $(version_code 12.0.0) ] &&
102                 # bug number for skipped test: LU-3703
103                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
104 elif [ -r /etc/os-release ]; then
105         if grep -qi ubuntu /etc/os-release; then
106                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
107                                                 -e 's/^VERSION=//p' \
108                                                 /etc/os-release |
109                                                 awk '{ print $1 }'))
110
111                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
112                         # bug number for skipped test:
113                         #                LU-10334 LU-10335 LU-10335 LU-10335
114                         ALWAYS_EXCEPT+=" 103a     130a     130b     130c"
115                         #                LU-10335 LU-10335 LU-10366
116                         ALWAYS_EXCEPT+=" 130d     130e     410"
117                 fi
118         fi
119 fi
120
121 FAIL_ON_ERROR=false
122
123 cleanup() {
124         echo -n "cln.."
125         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
126         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
127 }
128 setup() {
129         echo -n "mnt.."
130         load_modules
131         setupall || exit 10
132         echo "done"
133 }
134
135 check_swap_layouts_support()
136 {
137         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
138                 skip "Does not support layout lock."
139 }
140
141 check_and_setup_lustre
142 DIR=${DIR:-$MOUNT}
143 assert_DIR
144
145 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
146
147 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
148 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
149 rm -rf $DIR/[Rdfs][0-9]*
150
151 # $RUNAS_ID may get set incorrectly somewhere else
152 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
153
154 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
155
156 build_test_filter
157
158 if [ "${ONLY}" = "MOUNT" ] ; then
159         echo "Lustre is up, please go on"
160         exit
161 fi
162
163 echo "preparing for tests involving mounts"
164 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
165 touch $EXT2_DEV
166 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
167 echo # add a newline after mke2fs.
168
169 umask 077
170
171 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
172 lctl set_param debug=-1 2> /dev/null || true
173 test_0a() {
174         touch $DIR/$tfile
175         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
176         rm $DIR/$tfile
177         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
178 }
179 run_test 0a "touch; rm ====================="
180
181 test_0b() {
182         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
183         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
184 }
185 run_test 0b "chmod 0755 $DIR ============================="
186
187 test_0c() {
188         $LCTL get_param mdc.*.import | grep "state: FULL" ||
189                 error "import not FULL"
190         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
191                 error "bad target"
192 }
193 run_test 0c "check import proc"
194
195 test_0d() { # LU-3397
196         [ $(lustre_version_code mgs) -lt $(version_code 2.10.57) ] &&
197                 skip "proc exports not supported before 2.10.57"
198
199         local mgs_exp="mgs.MGS.exports"
200         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
201         local exp_client_nid
202         local exp_client_version
203         local exp_val
204         local imp_val
205         local temp_imp=$DIR/$tfile.import
206         local temp_exp=$DIR/$tfile.export
207
208         # save mgc import file to $temp_imp
209         $LCTL get_param mgc.*.import | tee $temp_imp
210         # Check if client uuid is found in MGS export
211         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
212                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
213                         $client_uuid ] &&
214                         break;
215         done
216         # save mgs export file to $temp_exp
217         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
218
219         # Compare the value of field "connect_flags"
220         imp_val=$(grep "connect_flags" $temp_imp)
221         exp_val=$(grep "connect_flags" $temp_exp)
222         [ "$exp_val" == "$imp_val" ] ||
223                 error "export flags '$exp_val' != import flags '$imp_val'"
224
225         # Compare the value of client version
226         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
227         exp_val=$(version_code $exp_client_version)
228         imp_val=$(lustre_version_code client)
229         [ "$exp_val" == "$imp_val" ] ||
230                 error "export client version '$exp_val' != '$imp_val'"
231 }
232 run_test 0d "check export proc ============================="
233
234 test_1() {
235         test_mkdir $DIR/$tdir
236         test_mkdir $DIR/$tdir/d2
237         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
238         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
239         rmdir $DIR/$tdir/d2
240         rmdir $DIR/$tdir
241         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
242 }
243 run_test 1 "mkdir; remkdir; rmdir"
244
245 test_2() {
246         test_mkdir $DIR/$tdir
247         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
248         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
249         rm -r $DIR/$tdir
250         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
251 }
252 run_test 2 "mkdir; touch; rmdir; check file"
253
254 test_3() {
255         test_mkdir $DIR/$tdir
256         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
257         touch $DIR/$tdir/$tfile
258         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
259         rm -r $DIR/$tdir
260         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
261 }
262 run_test 3 "mkdir; touch; rmdir; check dir"
263
264 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
265 test_4() {
266         test_mkdir -i 1 $DIR/$tdir
267
268         touch $DIR/$tdir/$tfile ||
269                 error "Create file under remote directory failed"
270
271         rmdir $DIR/$tdir &&
272                 error "Expect error removing in-use dir $DIR/$tdir"
273
274         test -d $DIR/$tdir || error "Remote directory disappeared"
275
276         rm -rf $DIR/$tdir || error "remove remote dir error"
277 }
278 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
279
280 test_5() {
281         test_mkdir $DIR/$tdir
282         test_mkdir $DIR/$tdir/d2
283         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
284         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
285         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
286 }
287 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
288
289 test_6a() {
290         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
291         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
292         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
293                 error "$tfile does not have perm 0666 or UID $UID"
294         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
295         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
296                 error "$tfile should be 0666 and owned by UID $UID"
297 }
298 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
299
300 test_6c() {
301         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
302
303         touch $DIR/$tfile
304         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
305         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
306                 error "$tfile should be owned by UID $RUNAS_ID"
307         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
308         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
309                 error "$tfile should be owned by UID $RUNAS_ID"
310 }
311 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
312
313 test_6e() {
314         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
315
316         touch $DIR/$tfile
317         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
318         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
319                 error "$tfile should be owned by GID $UID"
320         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
321         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
322                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
323 }
324 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
325
326 test_6g() {
327         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
328
329         test_mkdir $DIR/$tdir
330         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
331         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
332         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
333         test_mkdir $DIR/$tdir/d/subdir
334         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
335                 error "$tdir/d/subdir should be GID $RUNAS_GID"
336         if [[ $MDSCOUNT -gt 1 ]]; then
337                 # check remote dir sgid inherite
338                 $LFS mkdir -i 0 $DIR/$tdir.local ||
339                         error "mkdir $tdir.local failed"
340                 chmod g+s $DIR/$tdir.local ||
341                         error "chmod $tdir.local failed"
342                 chgrp $RUNAS_GID $DIR/$tdir.local ||
343                         error "chgrp $tdir.local failed"
344                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
345                         error "mkdir $tdir.remote failed"
346                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
347                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
348                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
349                         error "$tdir.remote should be mode 02755"
350         fi
351 }
352 run_test 6g "verify new dir in sgid dir inherits group"
353
354 test_6h() { # bug 7331
355         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
356
357         touch $DIR/$tfile || error "touch failed"
358         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
359         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
360                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
361         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
362                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
363 }
364 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
365
366 test_7a() {
367         test_mkdir $DIR/$tdir
368         $MCREATE $DIR/$tdir/$tfile
369         chmod 0666 $DIR/$tdir/$tfile
370         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
371                 error "$tdir/$tfile should be mode 0666"
372 }
373 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
374
375 test_7b() {
376         if [ ! -d $DIR/$tdir ]; then
377                 test_mkdir $DIR/$tdir
378         fi
379         $MCREATE $DIR/$tdir/$tfile
380         echo -n foo > $DIR/$tdir/$tfile
381         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
382         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
383 }
384 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
385
386 test_8() {
387         test_mkdir $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         chmod 0666 $DIR/$tdir/$tfile
390         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
391                 error "$tfile mode not 0666"
392 }
393 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
394
395 test_9() {
396         test_mkdir $DIR/$tdir
397         test_mkdir $DIR/$tdir/d2
398         test_mkdir $DIR/$tdir/d2/d3
399         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
400 }
401 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
402
403 test_10() {
404         test_mkdir $DIR/$tdir
405         test_mkdir $DIR/$tdir/d2
406         touch $DIR/$tdir/d2/$tfile
407         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
408                 error "$tdir/d2/$tfile not a file"
409 }
410 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
411
412 test_11() {
413         test_mkdir $DIR/$tdir
414         test_mkdir $DIR/$tdir/d2
415         chmod 0666 $DIR/$tdir/d2
416         chmod 0705 $DIR/$tdir/d2
417         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
418                 error "$tdir/d2 mode not 0705"
419 }
420 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
421
422 test_12() {
423         test_mkdir $DIR/$tdir
424         touch $DIR/$tdir/$tfile
425         chmod 0666 $DIR/$tdir/$tfile
426         chmod 0654 $DIR/$tdir/$tfile
427         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
428                 error "$tdir/d2 mode not 0654"
429 }
430 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
431
432 test_13() {
433         test_mkdir $DIR/$tdir
434         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
435         >  $DIR/$tdir/$tfile
436         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
437                 error "$tdir/$tfile size not 0 after truncate"
438 }
439 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
440
441 test_14() {
442         test_mkdir $DIR/$tdir
443         touch $DIR/$tdir/$tfile
444         rm $DIR/$tdir/$tfile
445         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
446 }
447 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
448
449 test_15() {
450         test_mkdir $DIR/$tdir
451         touch $DIR/$tdir/$tfile
452         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
453         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
454                 error "$tdir/${tfile_2} not a file after rename"
455         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
456 }
457 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
458
459 test_16() {
460         test_mkdir $DIR/$tdir
461         touch $DIR/$tdir/$tfile
462         rm -rf $DIR/$tdir/$tfile
463         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
464 }
465 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
466
467 test_17a() {
468         test_mkdir $DIR/$tdir
469         touch $DIR/$tdir/$tfile
470         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
471         ls -l $DIR/$tdir
472         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
473                 error "$tdir/l-exist not a symlink"
474         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
475                 error "$tdir/l-exist not referencing a file"
476         rm -f $DIR/$tdir/l-exist
477         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
478 }
479 run_test 17a "symlinks: create, remove (real)"
480
481 test_17b() {
482         test_mkdir $DIR/$tdir
483         ln -s no-such-file $DIR/$tdir/l-dangle
484         ls -l $DIR/$tdir
485         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
486                 error "$tdir/l-dangle not referencing no-such-file"
487         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
488                 error "$tdir/l-dangle not referencing non-existent file"
489         rm -f $DIR/$tdir/l-dangle
490         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
491 }
492 run_test 17b "symlinks: create, remove (dangling)"
493
494 test_17c() { # bug 3440 - don't save failed open RPC for replay
495         test_mkdir $DIR/$tdir
496         ln -s foo $DIR/$tdir/$tfile
497         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
498 }
499 run_test 17c "symlinks: open dangling (should return error)"
500
501 test_17d() {
502         test_mkdir $DIR/$tdir
503         ln -s foo $DIR/$tdir/$tfile
504         touch $DIR/$tdir/$tfile || error "creating to new symlink"
505 }
506 run_test 17d "symlinks: create dangling"
507
508 test_17e() {
509         test_mkdir $DIR/$tdir
510         local foo=$DIR/$tdir/$tfile
511         ln -s $foo $foo || error "create symlink failed"
512         ls -l $foo || error "ls -l failed"
513         ls $foo && error "ls not failed" || true
514 }
515 run_test 17e "symlinks: create recursive symlink (should return error)"
516
517 test_17f() {
518         test_mkdir $DIR/$tdir
519         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
520         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
521         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
522         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
523         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
524         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
525         ls -l  $DIR/$tdir
526 }
527 run_test 17f "symlinks: long and very long symlink name"
528
529 # str_repeat(S, N) generate a string that is string S repeated N times
530 str_repeat() {
531         local s=$1
532         local n=$2
533         local ret=''
534         while [ $((n -= 1)) -ge 0 ]; do
535                 ret=$ret$s
536         done
537         echo $ret
538 }
539
540 # Long symlinks and LU-2241
541 test_17g() {
542         test_mkdir $DIR/$tdir
543         local TESTS="59 60 61 4094 4095"
544
545         # Fix for inode size boundary in 2.1.4
546         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
547                 TESTS="4094 4095"
548
549         # Patch not applied to 2.2 or 2.3 branches
550         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
551         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
552                 TESTS="4094 4095"
553
554         # skip long symlink name for rhel6.5.
555         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
556         grep -q '6.5' /etc/redhat-release &>/dev/null &&
557                 TESTS="59 60 61 4062 4063"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
637                 skip_env "ldiskfs only test"
638         remote_mds_nodsh && skip "remote MDS with nodsh"
639         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
640         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
641                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
642
643         local short_sym="0123456789"
644         local wdir=$DIR/$tdir
645         local i
646
647         test_mkdir $wdir
648         long_sym=$short_sym
649         # create a long symlink file
650         for ((i = 0; i < 4; ++i)); do
651                 long_sym=${long_sym}${long_sym}
652         done
653
654         echo "create 512 short and long symlink files under $wdir"
655         for ((i = 0; i < 256; ++i)); do
656                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
657                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
658         done
659
660         echo "erase them"
661         rm -f $wdir/*
662         sync
663         wait_delete_completed
664
665         echo "recreate the 512 symlink files with a shorter string"
666         for ((i = 0; i < 512; ++i)); do
667                 # rewrite the symlink file with a shorter string
668                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
669                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
670         done
671
672         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
673         local devname=$(mdsdevname $mds_index)
674
675         echo "stop and checking mds${mds_index}:"
676         # e2fsck should not return error
677         stop mds${mds_index}
678         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
679         rc=$?
680
681         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
682                 error "start mds${mds_index} failed"
683         df $MOUNT > /dev/null 2>&1
684         [ $rc -eq 0 ] ||
685                 error "e2fsck detected error for short/long symlink: rc=$rc"
686         rm -f $wdir/*
687 }
688 run_test 17m "run e2fsck against MDT which contains short/long symlink"
689
690 check_fs_consistency_17n() {
691         local mdt_index
692         local rc=0
693
694         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
695         # so it only check MDT1/MDT2 instead of all of MDTs.
696         for mdt_index in 1 2; do
697                 local devname=$(mdsdevname $mdt_index)
698                 # e2fsck should not return error
699                 stop mds${mdt_index}
700                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
701                         rc=$((rc + $?))
702
703                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
704                         error "mount mds$mdt_index failed"
705                 df $MOUNT > /dev/null 2>&1
706         done
707         return $rc
708 }
709
710 test_17n() {
711         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
712         [ $PARALLEL == "yes" ] && skip "skip parallel run"
713         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
714                 skip_env "ldiskfs only test"
715         remote_mds_nodsh && skip "remote MDS with nodsh"
716         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
717         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
718                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
719
720         local i
721
722         test_mkdir $DIR/$tdir
723         for ((i=0; i<10; i++)); do
724                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
725                         error "create remote dir error $i"
726                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
727                         error "create files under remote dir failed $i"
728         done
729
730         check_fs_consistency_17n ||
731                 error "e2fsck report error after create files under remote dir"
732
733         for ((i = 0; i < 10; i++)); do
734                 rm -rf $DIR/$tdir/remote_dir_${i} ||
735                         error "destroy remote dir error $i"
736         done
737
738         check_fs_consistency_17n ||
739                 error "e2fsck report error after unlink files under remote dir"
740
741         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
742                 skip "lustre < 2.4.50 does not support migrate mv"
743
744         for ((i = 0; i < 10; i++)); do
745                 mkdir -p $DIR/$tdir/remote_dir_${i}
746                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
747                         error "create files under remote dir failed $i"
748                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
749                         error "migrate remote dir error $i"
750         done
751         check_fs_consistency_17n || error "e2fsck report error after migration"
752
753         for ((i = 0; i < 10; i++)); do
754                 rm -rf $DIR/$tdir/remote_dir_${i} ||
755                         error "destroy remote dir error $i"
756         done
757
758         check_fs_consistency_17n || error "e2fsck report error after unlink"
759 }
760 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
761
762 test_17o() {
763         remote_mds_nodsh && skip "remote MDS with nodsh"
764         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
765                 skip "Need MDS version at least 2.3.64"
766
767         local wdir=$DIR/${tdir}o
768         local mdt_index
769         local rc=0
770
771         test_mkdir $wdir
772         touch $wdir/$tfile
773         mdt_index=$($LFS getstripe -m $wdir/$tfile)
774         mdt_index=$((mdt_index + 1))
775
776         cancel_lru_locks mdc
777         #fail mds will wait the failover finish then set
778         #following fail_loc to avoid interfer the recovery process.
779         fail mds${mdt_index}
780
781         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
782         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
783         ls -l $wdir/$tfile && rc=1
784         do_facet mds${mdt_index} lctl set_param fail_loc=0
785         [[ $rc -eq 0 ]] || error "stat file should fail"
786 }
787 run_test 17o "stat file with incompat LMA feature"
788
789 test_18() {
790         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
791         ls $DIR || error "Failed to ls $DIR: $?"
792 }
793 run_test 18 "touch .../f ; ls ... =============================="
794
795 test_19a() {
796         touch $DIR/$tfile
797         ls -l $DIR
798         rm $DIR/$tfile
799         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
800 }
801 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
802
803 test_19b() {
804         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
805 }
806 run_test 19b "ls -l .../f19 (should return error) =============="
807
808 test_19c() {
809         [ $RUNAS_ID -eq $UID ] &&
810                 skip_env "RUNAS_ID = UID = $UID -- skipping"
811
812         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
813 }
814 run_test 19c "$RUNAS touch .../f19 (should return error) =="
815
816 test_19d() {
817         cat $DIR/f19 && error || true
818 }
819 run_test 19d "cat .../f19 (should return error) =============="
820
821 test_20() {
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         touch $DIR/$tfile
827         rm $DIR/$tfile
828         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
829 }
830 run_test 20 "touch .../f ; ls -l ..."
831
832 test_21() {
833         test_mkdir $DIR/$tdir
834         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
835         ln -s dangle $DIR/$tdir/link
836         echo foo >> $DIR/$tdir/link
837         cat $DIR/$tdir/dangle
838         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
839         $CHECKSTAT -f -t file $DIR/$tdir/link ||
840                 error "$tdir/link not linked to a file"
841 }
842 run_test 21 "write to dangling link"
843
844 test_22() {
845         local wdir=$DIR/$tdir
846         test_mkdir $wdir
847         chown $RUNAS_ID:$RUNAS_GID $wdir
848         (cd $wdir || error "cd $wdir failed";
849                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
850                 $RUNAS tar xf -)
851         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
852         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
853         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
854                 error "checkstat -u failed"
855 }
856 run_test 22 "unpack tar archive as non-root user"
857
858 # was test_23
859 test_23a() {
860         test_mkdir $DIR/$tdir
861         local file=$DIR/$tdir/$tfile
862
863         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
864         openfile -f O_CREAT:O_EXCL $file &&
865                 error "$file recreate succeeded" || true
866 }
867 run_test 23a "O_CREAT|O_EXCL in subdir"
868
869 test_23b() { # bug 18988
870         test_mkdir $DIR/$tdir
871         local file=$DIR/$tdir/$tfile
872
873         rm -f $file
874         echo foo > $file || error "write filed"
875         echo bar >> $file || error "append filed"
876         $CHECKSTAT -s 8 $file || error "wrong size"
877         rm $file
878 }
879 run_test 23b "O_APPEND check"
880
881 # LU-9409, size with O_APPEND and tiny writes
882 test_23c() {
883         local file=$DIR/$tfile
884
885         # single dd
886         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
887         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
888         rm -f $file
889
890         # racing tiny writes
891         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
892         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
893         wait
894         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
895         rm -f $file
896
897         #racing tiny & normal writes
898         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
899         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
900         wait
901         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
902         rm -f $file
903
904         #racing tiny & normal writes 2, ugly numbers
905         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
906         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
907         wait
908         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
909         rm -f $file
910 }
911 run_test 23c "O_APPEND size checks for tiny writes"
912
913 # LU-11069 file offset is correct after appending writes
914 test_23d() {
915         local file=$DIR/$tfile
916         local offset
917
918         echo CentaurHauls > $file
919         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
920         if ((offset != 26)); then
921                 error "wrong offset, expected 26, got '$offset'"
922         fi
923 }
924 run_test 23d "file offset is correct after appending writes"
925
926 # rename sanity
927 test_24a() {
928         echo '-- same directory rename'
929         test_mkdir $DIR/$tdir
930         touch $DIR/$tdir/$tfile.1
931         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
932         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
933 }
934 run_test 24a "rename file to non-existent target"
935
936 test_24b() {
937         test_mkdir $DIR/$tdir
938         touch $DIR/$tdir/$tfile.{1,2}
939         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
940         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
941         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
942 }
943 run_test 24b "rename file to existing target"
944
945 test_24c() {
946         test_mkdir $DIR/$tdir
947         test_mkdir $DIR/$tdir/d$testnum.1
948         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
949         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
950         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
951 }
952 run_test 24c "rename directory to non-existent target"
953
954 test_24d() {
955         test_mkdir -c1 $DIR/$tdir
956         test_mkdir -c1 $DIR/$tdir/d$testnum.1
957         test_mkdir -c1 $DIR/$tdir/d$testnum.2
958         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
959         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
960         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
961 }
962 run_test 24d "rename directory to existing target"
963
964 test_24e() {
965         echo '-- cross directory renames --'
966         test_mkdir $DIR/R5a
967         test_mkdir $DIR/R5b
968         touch $DIR/R5a/f
969         mv $DIR/R5a/f $DIR/R5b/g
970         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
971         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
972 }
973 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
974
975 test_24f() {
976         test_mkdir $DIR/R6a
977         test_mkdir $DIR/R6b
978         touch $DIR/R6a/f $DIR/R6b/g
979         mv $DIR/R6a/f $DIR/R6b/g
980         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
981         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
982 }
983 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
984
985 test_24g() {
986         test_mkdir $DIR/R7a
987         test_mkdir $DIR/R7b
988         test_mkdir $DIR/R7a/d
989         mv $DIR/R7a/d $DIR/R7b/e
990         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
991         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
992 }
993 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
994
995 test_24h() {
996         test_mkdir -c1 $DIR/R8a
997         test_mkdir -c1 $DIR/R8b
998         test_mkdir -c1 $DIR/R8a/d
999         test_mkdir -c1 $DIR/R8b/e
1000         mrename $DIR/R8a/d $DIR/R8b/e
1001         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1002         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1003 }
1004 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1005
1006 test_24i() {
1007         echo "-- rename error cases"
1008         test_mkdir $DIR/R9
1009         test_mkdir $DIR/R9/a
1010         touch $DIR/R9/f
1011         mrename $DIR/R9/f $DIR/R9/a
1012         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1013         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1014         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1015 }
1016 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1017
1018 test_24j() {
1019         test_mkdir $DIR/R10
1020         mrename $DIR/R10/f $DIR/R10/g
1021         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1022         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1023         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1024 }
1025 run_test 24j "source does not exist ============================"
1026
1027 test_24k() {
1028         test_mkdir $DIR/R11a
1029         test_mkdir $DIR/R11a/d
1030         touch $DIR/R11a/f
1031         mv $DIR/R11a/f $DIR/R11a/d
1032         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1033         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1034 }
1035 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1036
1037 # bug 2429 - rename foo foo foo creates invalid file
1038 test_24l() {
1039         f="$DIR/f24l"
1040         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1041 }
1042 run_test 24l "Renaming a file to itself ========================"
1043
1044 test_24m() {
1045         f="$DIR/f24m"
1046         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1047         # on ext3 this does not remove either the source or target files
1048         # though the "expected" operation would be to remove the source
1049         $CHECKSTAT -t file ${f} || error "${f} missing"
1050         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1051 }
1052 run_test 24m "Renaming a file to a hard link to itself ========="
1053
1054 test_24n() {
1055     f="$DIR/f24n"
1056     # this stats the old file after it was renamed, so it should fail
1057     touch ${f}
1058     $CHECKSTAT ${f} || error "${f} missing"
1059     mv ${f} ${f}.rename
1060     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1061     $CHECKSTAT -a ${f} || error "${f} exists"
1062 }
1063 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1064
1065 test_24o() {
1066         test_mkdir $DIR/$tdir
1067         rename_many -s random -v -n 10 $DIR/$tdir
1068 }
1069 run_test 24o "rename of files during htree split"
1070
1071 test_24p() {
1072         test_mkdir $DIR/R12a
1073         test_mkdir $DIR/R12b
1074         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1075         mrename $DIR/R12a $DIR/R12b
1076         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1077         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1078         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1079         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1080 }
1081 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1082
1083 cleanup_multiop_pause() {
1084         trap 0
1085         kill -USR1 $MULTIPID
1086 }
1087
1088 test_24q() {
1089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1090
1091         test_mkdir $DIR/R13a
1092         test_mkdir $DIR/R13b
1093         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1094         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1095         MULTIPID=$!
1096
1097         trap cleanup_multiop_pause EXIT
1098         mrename $DIR/R13a $DIR/R13b
1099         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1100         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1101         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1102         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1103         cleanup_multiop_pause
1104         wait $MULTIPID || error "multiop close failed"
1105 }
1106 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1107
1108 test_24r() { #bug 3789
1109         test_mkdir $DIR/R14a
1110         test_mkdir $DIR/R14a/b
1111         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1112         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1113         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1114 }
1115 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1116
1117 test_24s() {
1118         test_mkdir $DIR/R15a
1119         test_mkdir $DIR/R15a/b
1120         test_mkdir $DIR/R15a/b/c
1121         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1122         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1123         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1124 }
1125 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1126 test_24t() {
1127         test_mkdir $DIR/R16a
1128         test_mkdir $DIR/R16a/b
1129         test_mkdir $DIR/R16a/b/c
1130         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1131         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1132         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1133 }
1134 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1135
1136 test_24u() { # bug12192
1137         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1138         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1139 }
1140 run_test 24u "create stripe file"
1141
1142 simple_cleanup_common() {
1143         local rc=0
1144         trap 0
1145         [ -z "$DIR" -o -z "$tdir" ] && return 0
1146
1147         local start=$SECONDS
1148         rm -rf $DIR/$tdir
1149         rc=$?
1150         wait_delete_completed
1151         echo "cleanup time $((SECONDS - start))"
1152         return $rc
1153 }
1154
1155 max_pages_per_rpc() {
1156         local mdtname="$(printf "MDT%04x" ${1:-0})"
1157         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1158 }
1159
1160 test_24v() {
1161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1162
1163         local nrfiles=${COUNT:-100000}
1164         local fname="$DIR/$tdir/$tfile"
1165
1166         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1167         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1168
1169         test_mkdir "$(dirname $fname)"
1170         # assume MDT0000 has the fewest inodes
1171         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1172         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1173         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1174
1175         trap simple_cleanup_common EXIT
1176
1177         createmany -m "$fname" $nrfiles
1178
1179         cancel_lru_locks mdc
1180         lctl set_param mdc.*.stats clear
1181
1182         # was previously test_24D: LU-6101
1183         # readdir() returns correct number of entries after cursor reload
1184         local num_ls=$(ls $DIR/$tdir | wc -l)
1185         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1186         local num_all=$(ls -a $DIR/$tdir | wc -l)
1187         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1188              $num_all -ne $((nrfiles + 2)) ]; then
1189                 error "Expected $nrfiles files, got $num_ls " \
1190                         "($num_uniq unique $num_all .&..)"
1191         fi
1192         # LU-5 large readdir
1193         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1194         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1195         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1196         # take into account of overhead in lu_dirpage header and end mark in
1197         # each page, plus one in rpc_num calculation.
1198         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1199         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1200         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1201         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1202         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1203         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1204         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1205         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1206                 error "large readdir doesn't take effect: " \
1207                       "$mds_readpage should be about $rpc_max"
1208
1209         simple_cleanup_common
1210 }
1211 run_test 24v "list large directory (test hash collision, b=17560)"
1212
1213 test_24w() { # bug21506
1214         SZ1=234852
1215         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1216         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1217         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1218         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1219         [[ "$SZ1" -eq "$SZ2" ]] ||
1220                 error "Error reading at the end of the file $tfile"
1221 }
1222 run_test 24w "Reading a file larger than 4Gb"
1223
1224 test_24x() {
1225         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1227         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1228                 skip "Need MDS version at least 2.7.56"
1229
1230         local MDTIDX=1
1231         local remote_dir=$DIR/$tdir/remote_dir
1232
1233         test_mkdir $DIR/$tdir
1234         $LFS mkdir -i $MDTIDX $remote_dir ||
1235                 error "create remote directory failed"
1236
1237         test_mkdir $DIR/$tdir/src_dir
1238         touch $DIR/$tdir/src_file
1239         test_mkdir $remote_dir/tgt_dir
1240         touch $remote_dir/tgt_file
1241
1242         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1243                 error "rename dir cross MDT failed!"
1244
1245         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1246                 error "rename file cross MDT failed!"
1247
1248         touch $DIR/$tdir/ln_file
1249         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1250                 error "ln file cross MDT failed"
1251
1252         rm -rf $DIR/$tdir || error "Can not delete directories"
1253 }
1254 run_test 24x "cross MDT rename/link"
1255
1256 test_24y() {
1257         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1259
1260         local remote_dir=$DIR/$tdir/remote_dir
1261         local mdtidx=1
1262
1263         test_mkdir $DIR/$tdir
1264         $LFS mkdir -i $mdtidx $remote_dir ||
1265                 error "create remote directory failed"
1266
1267         test_mkdir $remote_dir/src_dir
1268         touch $remote_dir/src_file
1269         test_mkdir $remote_dir/tgt_dir
1270         touch $remote_dir/tgt_file
1271
1272         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1273                 error "rename subdir in the same remote dir failed!"
1274
1275         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1276                 error "rename files in the same remote dir failed!"
1277
1278         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1279                 error "link files in the same remote dir failed!"
1280
1281         rm -rf $DIR/$tdir || error "Can not delete directories"
1282 }
1283 run_test 24y "rename/link on the same dir should succeed"
1284
1285 test_24A() { # LU-3182
1286         local NFILES=5000
1287
1288         rm -rf $DIR/$tdir
1289         test_mkdir $DIR/$tdir
1290         trap simple_cleanup_common EXIT
1291         createmany -m $DIR/$tdir/$tfile $NFILES
1292         local t=$(ls $DIR/$tdir | wc -l)
1293         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1294         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1295         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1296                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1297         fi
1298
1299         simple_cleanup_common || error "Can not delete directories"
1300 }
1301 run_test 24A "readdir() returns correct number of entries."
1302
1303 test_24B() { # LU-4805
1304         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1305
1306         local count
1307
1308         test_mkdir $DIR/$tdir
1309         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1310                 error "create striped dir failed"
1311
1312         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1313         [ $count -eq 2 ] || error "Expected 2, got $count"
1314
1315         touch $DIR/$tdir/striped_dir/a
1316
1317         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1318         [ $count -eq 3 ] || error "Expected 3, got $count"
1319
1320         touch $DIR/$tdir/striped_dir/.f
1321
1322         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1323         [ $count -eq 4 ] || error "Expected 4, got $count"
1324
1325         rm -rf $DIR/$tdir || error "Can not delete directories"
1326 }
1327 run_test 24B "readdir for striped dir return correct number of entries"
1328
1329 test_24C() {
1330         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1331
1332         mkdir $DIR/$tdir
1333         mkdir $DIR/$tdir/d0
1334         mkdir $DIR/$tdir/d1
1335
1336         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1337                 error "create striped dir failed"
1338
1339         cd $DIR/$tdir/d0/striped_dir
1340
1341         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1342         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1343         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1344
1345         [ "$d0_ino" = "$parent_ino" ] ||
1346                 error ".. wrong, expect $d0_ino, get $parent_ino"
1347
1348         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1349                 error "mv striped dir failed"
1350
1351         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1352
1353         [ "$d1_ino" = "$parent_ino" ] ||
1354                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1355 }
1356 run_test 24C "check .. in striped dir"
1357
1358 test_24E() {
1359         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1360         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1361
1362         mkdir -p $DIR/$tdir
1363         mkdir $DIR/$tdir/src_dir
1364         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1365                 error "create remote source failed"
1366
1367         touch $DIR/$tdir/src_dir/src_child/a
1368
1369         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1370                 error "create remote target dir failed"
1371
1372         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1373                 error "create remote target child failed"
1374
1375         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1376                 error "rename dir cross MDT failed!"
1377
1378         find $DIR/$tdir
1379
1380         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1381                 error "src_child still exists after rename"
1382
1383         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1384                 error "missing file(a) after rename"
1385
1386         rm -rf $DIR/$tdir || error "Can not delete directories"
1387 }
1388 run_test 24E "cross MDT rename/link"
1389
1390 test_25a() {
1391         echo '== symlink sanity ============================================='
1392
1393         test_mkdir $DIR/d25
1394         ln -s d25 $DIR/s25
1395         touch $DIR/s25/foo ||
1396                 error "File creation in symlinked directory failed"
1397 }
1398 run_test 25a "create file in symlinked directory ==============="
1399
1400 test_25b() {
1401         [ ! -d $DIR/d25 ] && test_25a
1402         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1403 }
1404 run_test 25b "lookup file in symlinked directory ==============="
1405
1406 test_26a() {
1407         test_mkdir $DIR/d26
1408         test_mkdir $DIR/d26/d26-2
1409         ln -s d26/d26-2 $DIR/s26
1410         touch $DIR/s26/foo || error "File creation failed"
1411 }
1412 run_test 26a "multiple component symlink ======================="
1413
1414 test_26b() {
1415         test_mkdir -p $DIR/$tdir/d26-2
1416         ln -s $tdir/d26-2/foo $DIR/s26-2
1417         touch $DIR/s26-2 || error "File creation failed"
1418 }
1419 run_test 26b "multiple component symlink at end of lookup ======"
1420
1421 test_26c() {
1422         test_mkdir $DIR/d26.2
1423         touch $DIR/d26.2/foo
1424         ln -s d26.2 $DIR/s26.2-1
1425         ln -s s26.2-1 $DIR/s26.2-2
1426         ln -s s26.2-2 $DIR/s26.2-3
1427         chmod 0666 $DIR/s26.2-3/foo
1428 }
1429 run_test 26c "chain of symlinks"
1430
1431 # recursive symlinks (bug 439)
1432 test_26d() {
1433         ln -s d26-3/foo $DIR/d26-3
1434 }
1435 run_test 26d "create multiple component recursive symlink"
1436
1437 test_26e() {
1438         [ ! -h $DIR/d26-3 ] && test_26d
1439         rm $DIR/d26-3
1440 }
1441 run_test 26e "unlink multiple component recursive symlink"
1442
1443 # recursive symlinks (bug 7022)
1444 test_26f() {
1445         test_mkdir $DIR/$tdir
1446         test_mkdir $DIR/$tdir/$tfile
1447         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1448         test_mkdir -p lndir/bar1
1449         test_mkdir $DIR/$tdir/$tfile/$tfile
1450         cd $tfile                || error "cd $tfile failed"
1451         ln -s .. dotdot          || error "ln dotdot failed"
1452         ln -s dotdot/lndir lndir || error "ln lndir failed"
1453         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1454         output=`ls $tfile/$tfile/lndir/bar1`
1455         [ "$output" = bar1 ] && error "unexpected output"
1456         rm -r $tfile             || error "rm $tfile failed"
1457         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1458 }
1459 run_test 26f "rm -r of a directory which has recursive symlink"
1460
1461 test_27a() {
1462         test_mkdir $DIR/$tdir
1463         $LFS getstripe $DIR/$tdir
1464         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1465         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1466         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1467 }
1468 run_test 27a "one stripe file"
1469
1470 test_27b() {
1471         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1472
1473         test_mkdir $DIR/$tdir
1474         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1475         $LFS getstripe -c $DIR/$tdir/$tfile
1476         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1477                 error "two-stripe file doesn't have two stripes"
1478
1479         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1480 }
1481 run_test 27b "create and write to two stripe file"
1482
1483 test_27d() {
1484         test_mkdir $DIR/$tdir
1485         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1486                 error "setstripe failed"
1487         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1488         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1489 }
1490 run_test 27d "create file with default settings"
1491
1492 test_27e() {
1493         # LU-5839 adds check for existed layout before setting it
1494         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1495                 skip "Need MDS version at least 2.7.56"
1496
1497         test_mkdir $DIR/$tdir
1498         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1499         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1500         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1501 }
1502 run_test 27e "setstripe existing file (should return error)"
1503
1504 test_27f() {
1505         test_mkdir $DIR/$tdir
1506         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1507                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1508         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1509                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1510         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1511         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1512 }
1513 run_test 27f "setstripe with bad stripe size (should return error)"
1514
1515 test_27g() {
1516         test_mkdir $DIR/$tdir
1517         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1518         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1519                 error "$DIR/$tdir/$tfile has object"
1520 }
1521 run_test 27g "$LFS getstripe with no objects"
1522
1523 test_27i() {
1524         test_mkdir $DIR/$tdir
1525         touch $DIR/$tdir/$tfile || error "touch failed"
1526         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1527                 error "missing objects"
1528 }
1529 run_test 27i "$LFS getstripe with some objects"
1530
1531 test_27j() {
1532         test_mkdir $DIR/$tdir
1533         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1534                 error "setstripe failed" || true
1535 }
1536 run_test 27j "setstripe with bad stripe offset (should return error)"
1537
1538 test_27k() { # bug 2844
1539         test_mkdir $DIR/$tdir
1540         local file=$DIR/$tdir/$tfile
1541         local ll_max_blksize=$((4 * 1024 * 1024))
1542         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1543         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1544         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1545         dd if=/dev/zero of=$file bs=4k count=1
1546         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1547         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1548 }
1549 run_test 27k "limit i_blksize for broken user apps"
1550
1551 test_27l() {
1552         mcreate $DIR/$tfile || error "creating file"
1553         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1554                 error "setstripe should have failed" || true
1555 }
1556 run_test 27l "check setstripe permissions (should return error)"
1557
1558 test_27m() {
1559         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1560
1561         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1562                    head -n1)
1563         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1564                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1565         fi
1566         trap simple_cleanup_common EXIT
1567         test_mkdir $DIR/$tdir
1568         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1569         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1570                 error "dd should fill OST0"
1571         i=2
1572         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1573                 i=$((i + 1))
1574                 [ $i -gt 256 ] && break
1575         done
1576         i=$((i + 1))
1577         touch $DIR/$tdir/$tfile.$i
1578         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1579             awk '{print $1}'| grep -w "0") ] &&
1580                 error "OST0 was full but new created file still use it"
1581         i=$((i + 1))
1582         touch $DIR/$tdir/$tfile.$i
1583         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1584             awk '{print $1}'| grep -w "0") ] &&
1585                 error "OST0 was full but new created file still use it"
1586         simple_cleanup_common
1587 }
1588 run_test 27m "create file while OST0 was full"
1589
1590 sleep_maxage() {
1591         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1592                       awk '{ print $1 * 2; exit; }')
1593         sleep $delay
1594 }
1595
1596 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1597 # if the OST isn't full anymore.
1598 reset_enospc() {
1599         local OSTIDX=${1:-""}
1600
1601         local list=$(comma_list $(osts_nodes))
1602         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1603
1604         do_nodes $list lctl set_param fail_loc=0
1605         sync    # initiate all OST_DESTROYs from MDS to OST
1606         sleep_maxage
1607 }
1608
1609 exhaust_precreations() {
1610         local OSTIDX=$1
1611         local FAILLOC=$2
1612         local FAILIDX=${3:-$OSTIDX}
1613         local ofacet=ost$((OSTIDX + 1))
1614
1615         test_mkdir -p -c1 $DIR/$tdir
1616         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1617         local mfacet=mds$((mdtidx + 1))
1618         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1619
1620         local OST=$(ostname_from_index $OSTIDX)
1621
1622         # on the mdt's osc
1623         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1624         local last_id=$(do_facet $mfacet lctl get_param -n \
1625                         osc.$mdtosc_proc1.prealloc_last_id)
1626         local next_id=$(do_facet $mfacet lctl get_param -n \
1627                         osc.$mdtosc_proc1.prealloc_next_id)
1628
1629         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1630         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1631
1632         test_mkdir -p $DIR/$tdir/${OST}
1633         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1634 #define OBD_FAIL_OST_ENOSPC              0x215
1635         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1636         echo "Creating to objid $last_id on ost $OST..."
1637         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1638         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1639         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1640         sleep_maxage
1641 }
1642
1643 exhaust_all_precreations() {
1644         local i
1645         for (( i=0; i < OSTCOUNT; i++ )) ; do
1646                 exhaust_precreations $i $1 -1
1647         done
1648 }
1649
1650 test_27n() {
1651         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1653         remote_mds_nodsh && skip "remote MDS with nodsh"
1654         remote_ost_nodsh && skip "remote OST with nodsh"
1655
1656         reset_enospc
1657         rm -f $DIR/$tdir/$tfile
1658         exhaust_precreations 0 0x80000215
1659         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1660         touch $DIR/$tdir/$tfile || error "touch failed"
1661         $LFS getstripe $DIR/$tdir/$tfile
1662         reset_enospc
1663 }
1664 run_test 27n "create file with some full OSTs"
1665
1666 test_27o() {
1667         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1668         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1669         remote_mds_nodsh && skip "remote MDS with nodsh"
1670         remote_ost_nodsh && skip "remote OST with nodsh"
1671
1672         reset_enospc
1673         rm -f $DIR/$tdir/$tfile
1674         exhaust_all_precreations 0x215
1675
1676         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1677
1678         reset_enospc
1679         rm -rf $DIR/$tdir/*
1680 }
1681 run_test 27o "create file with all full OSTs (should error)"
1682
1683 test_27p() {
1684         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1686         remote_mds_nodsh && skip "remote MDS with nodsh"
1687         remote_ost_nodsh && skip "remote OST with nodsh"
1688
1689         reset_enospc
1690         rm -f $DIR/$tdir/$tfile
1691         test_mkdir $DIR/$tdir
1692
1693         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1694         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1695         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1696
1697         exhaust_precreations 0 0x80000215
1698         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1699         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1700         $LFS getstripe $DIR/$tdir/$tfile
1701
1702         reset_enospc
1703 }
1704 run_test 27p "append to a truncated file with some full OSTs"
1705
1706 test_27q() {
1707         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1709         remote_mds_nodsh && skip "remote MDS with nodsh"
1710         remote_ost_nodsh && skip "remote OST with nodsh"
1711
1712         reset_enospc
1713         rm -f $DIR/$tdir/$tfile
1714
1715         test_mkdir $DIR/$tdir
1716         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1717         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1718                 error "truncate $DIR/$tdir/$tfile failed"
1719         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1720
1721         exhaust_all_precreations 0x215
1722
1723         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1724         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1725
1726         reset_enospc
1727 }
1728 run_test 27q "append to truncated file with all OSTs full (should error)"
1729
1730 test_27r() {
1731         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1732         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1733         remote_mds_nodsh && skip "remote MDS with nodsh"
1734         remote_ost_nodsh && skip "remote OST with nodsh"
1735
1736         reset_enospc
1737         rm -f $DIR/$tdir/$tfile
1738         exhaust_precreations 0 0x80000215
1739
1740         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1741
1742         reset_enospc
1743 }
1744 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1745
1746 test_27s() { # bug 10725
1747         test_mkdir $DIR/$tdir
1748         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1749         local stripe_count=0
1750         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1751         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1752                 error "stripe width >= 2^32 succeeded" || true
1753
1754 }
1755 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1756
1757 test_27t() { # bug 10864
1758         WDIR=$(pwd)
1759         WLFS=$(which lfs)
1760         cd $DIR
1761         touch $tfile
1762         $WLFS getstripe $tfile
1763         cd $WDIR
1764 }
1765 run_test 27t "check that utils parse path correctly"
1766
1767 test_27u() { # bug 4900
1768         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1769         remote_mds_nodsh && skip "remote MDS with nodsh"
1770
1771         local index
1772         local list=$(comma_list $(mdts_nodes))
1773
1774 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1775         do_nodes $list $LCTL set_param fail_loc=0x139
1776         test_mkdir -p $DIR/$tdir
1777         trap simple_cleanup_common EXIT
1778         createmany -o $DIR/$tdir/t- 1000
1779         do_nodes $list $LCTL set_param fail_loc=0
1780
1781         TLOG=$TMP/$tfile.getstripe
1782         $LFS getstripe $DIR/$tdir > $TLOG
1783         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1784         unlinkmany $DIR/$tdir/t- 1000
1785         trap 0
1786         [[ $OBJS -gt 0 ]] &&
1787                 error "$OBJS objects created on OST-0. See $TLOG" ||
1788                 rm -f $TLOG
1789 }
1790 run_test 27u "skip object creation on OSC w/o objects"
1791
1792 test_27v() { # bug 4900
1793         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1795         remote_mds_nodsh && skip "remote MDS with nodsh"
1796         remote_ost_nodsh && skip "remote OST with nodsh"
1797
1798         exhaust_all_precreations 0x215
1799         reset_enospc
1800
1801         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1802
1803         touch $DIR/$tdir/$tfile
1804         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1805         # all except ost1
1806         for (( i=1; i < OSTCOUNT; i++ )); do
1807                 do_facet ost$i lctl set_param fail_loc=0x705
1808         done
1809         local START=`date +%s`
1810         createmany -o $DIR/$tdir/$tfile 32
1811
1812         local FINISH=`date +%s`
1813         local TIMEOUT=`lctl get_param -n timeout`
1814         local PROCESS=$((FINISH - START))
1815         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1816                error "$FINISH - $START >= $TIMEOUT / 2"
1817         sleep $((TIMEOUT / 2 - PROCESS))
1818         reset_enospc
1819 }
1820 run_test 27v "skip object creation on slow OST"
1821
1822 test_27w() { # bug 10997
1823         test_mkdir $DIR/$tdir
1824         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1825         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1826                 error "stripe size $size != 65536" || true
1827         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1828                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1829 }
1830 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1831
1832 test_27wa() {
1833         [[ $OSTCOUNT -lt 2 ]] &&
1834                 skip_env "skipping multiple stripe count/offset test"
1835
1836         test_mkdir $DIR/$tdir
1837         for i in $(seq 1 $OSTCOUNT); do
1838                 offset=$((i - 1))
1839                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1840                         error "setstripe -c $i -i $offset failed"
1841                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1842                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1843                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1844                 [ $index -ne $offset ] &&
1845                         error "stripe offset $index != $offset" || true
1846         done
1847 }
1848 run_test 27wa "check $LFS setstripe -c -i options"
1849
1850 test_27x() {
1851         remote_ost_nodsh && skip "remote OST with nodsh"
1852         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1854
1855         OFFSET=$(($OSTCOUNT - 1))
1856         OSTIDX=0
1857         local OST=$(ostname_from_index $OSTIDX)
1858
1859         test_mkdir $DIR/$tdir
1860         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1861         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1862         sleep_maxage
1863         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1864         for i in $(seq 0 $OFFSET); do
1865                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1866                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1867                 error "OST0 was degraded but new created file still use it"
1868         done
1869         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1870 }
1871 run_test 27x "create files while OST0 is degraded"
1872
1873 test_27y() {
1874         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1875         remote_mds_nodsh && skip "remote MDS with nodsh"
1876         remote_ost_nodsh && skip "remote OST with nodsh"
1877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1878
1879         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1880         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1881                 osc.$mdtosc.prealloc_last_id)
1882         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1883                 osc.$mdtosc.prealloc_next_id)
1884         local fcount=$((last_id - next_id))
1885         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1886         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1887
1888         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1889                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1890         local OST_DEACTIVE_IDX=-1
1891         local OSC
1892         local OSTIDX
1893         local OST
1894
1895         for OSC in $MDS_OSCS; do
1896                 OST=$(osc_to_ost $OSC)
1897                 OSTIDX=$(index_from_ostuuid $OST)
1898                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1899                         OST_DEACTIVE_IDX=$OSTIDX
1900                 fi
1901                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1902                         echo $OSC "is Deactivated:"
1903                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1904                 fi
1905         done
1906
1907         OSTIDX=$(index_from_ostuuid $OST)
1908         test_mkdir $DIR/$tdir
1909         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1910
1911         for OSC in $MDS_OSCS; do
1912                 OST=$(osc_to_ost $OSC)
1913                 OSTIDX=$(index_from_ostuuid $OST)
1914                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1915                         echo $OST "is degraded:"
1916                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1917                                                 obdfilter.$OST.degraded=1
1918                 fi
1919         done
1920
1921         sleep_maxage
1922         createmany -o $DIR/$tdir/$tfile $fcount
1923
1924         for OSC in $MDS_OSCS; do
1925                 OST=$(osc_to_ost $OSC)
1926                 OSTIDX=$(index_from_ostuuid $OST)
1927                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1928                         echo $OST "is recovered from degraded:"
1929                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1930                                                 obdfilter.$OST.degraded=0
1931                 else
1932                         do_facet $SINGLEMDS lctl --device %$OSC activate
1933                 fi
1934         done
1935
1936         # all osp devices get activated, hence -1 stripe count restored
1937         local stripe_count=0
1938
1939         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1940         # devices get activated.
1941         sleep_maxage
1942         $LFS setstripe -c -1 $DIR/$tfile
1943         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1944         rm -f $DIR/$tfile
1945         [ $stripe_count -ne $OSTCOUNT ] &&
1946                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1947         return 0
1948 }
1949 run_test 27y "create files while OST0 is degraded and the rest inactive"
1950
1951 check_seq_oid()
1952 {
1953         log "check file $1"
1954
1955         lmm_count=$($GETSTRIPE -c $1)
1956         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1957         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1958
1959         local old_ifs="$IFS"
1960         IFS=$'[:]'
1961         fid=($($LFS path2fid $1))
1962         IFS="$old_ifs"
1963
1964         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1965         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1966
1967         # compare lmm_seq and lu_fid->f_seq
1968         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1969         # compare lmm_object_id and lu_fid->oid
1970         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1971
1972         # check the trusted.fid attribute of the OST objects of the file
1973         local have_obdidx=false
1974         local stripe_nr=0
1975         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1976                 # skip lines up to and including "obdidx"
1977                 [ -z "$obdidx" ] && break
1978                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1979                 $have_obdidx || continue
1980
1981                 local ost=$((obdidx + 1))
1982                 local dev=$(ostdevname $ost)
1983                 local oid_hex
1984
1985                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1986
1987                 seq=$(echo $seq | sed -e "s/^0x//g")
1988                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1989                         oid_hex=$(echo $oid)
1990                 else
1991                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1992                 fi
1993                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1994
1995                 local ff=""
1996                 #
1997                 # Don't unmount/remount the OSTs if we don't need to do that.
1998                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1999                 # update too, until that use mount/ll_decode_filter_fid/mount.
2000                 # Re-enable when debugfs will understand new filter_fid.
2001                 #
2002                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2003                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2004                                 $dev 2>/dev/null" | grep "parent=")
2005                 fi
2006                 if [ -z "$ff" ]; then
2007                         stop ost$ost
2008                         mount_fstype ost$ost
2009                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2010                                 $(facet_mntpt ost$ost)/$obj_file)
2011                         unmount_fstype ost$ost
2012                         start ost$ost $dev $OST_MOUNT_OPTS
2013                         clients_up
2014                 fi
2015
2016                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2017
2018                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2019
2020                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2021                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2022                 #
2023                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2024                 #       stripe_size=1048576 component_id=1 component_start=0 \
2025                 #       component_end=33554432
2026                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2027                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2028                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2029                 local ff_pstripe
2030                 if grep -q 'stripe=' <<<$ff; then
2031                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2032                 else
2033                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2034                         # into f_ver in this case.  See comment on ff_parent.
2035                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2036                 fi
2037
2038                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2039                 [ $ff_pseq = $lmm_seq ] ||
2040                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2041                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2042                 [ $ff_poid = $lmm_oid ] ||
2043                         error "FF parent OID $ff_poid != $lmm_oid"
2044                 (($ff_pstripe == $stripe_nr)) ||
2045                         error "FF stripe $ff_pstripe != $stripe_nr"
2046
2047                 stripe_nr=$((stripe_nr + 1))
2048                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2049                         continue
2050                 if grep -q 'stripe_count=' <<<$ff; then
2051                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2052                                             -e 's/ .*//' <<<$ff)
2053                         [ $lmm_count = $ff_scnt ] ||
2054                                 error "FF stripe count $lmm_count != $ff_scnt"
2055                 fi
2056         done
2057 }
2058
2059 test_27z() {
2060         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2061         remote_ost_nodsh && skip "remote OST with nodsh"
2062
2063         test_mkdir $DIR/$tdir
2064         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2065                 { error "setstripe -c -1 failed"; return 1; }
2066         # We need to send a write to every object to get parent FID info set.
2067         # This _should_ also work for setattr, but does not currently.
2068         # touch $DIR/$tdir/$tfile-1 ||
2069         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2070                 { error "dd $tfile-1 failed"; return 2; }
2071         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2072                 { error "setstripe -c -1 failed"; return 3; }
2073         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2074                 { error "dd $tfile-2 failed"; return 4; }
2075
2076         # make sure write RPCs have been sent to OSTs
2077         sync; sleep 5; sync
2078
2079         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2080         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2081 }
2082 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2083
2084 test_27A() { # b=19102
2085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2086
2087         save_layout_restore_at_exit $MOUNT
2088         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2089         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2090                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2091         local default_size=$($GETSTRIPE -S $MOUNT)
2092         local default_offset=$($GETSTRIPE -i $MOUNT)
2093         local dsize=$(do_facet $SINGLEMDS \
2094                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2095         [ $default_size -eq $dsize ] ||
2096                 error "stripe size $default_size != $dsize"
2097         [ $default_offset -eq -1 ] ||
2098                 error "stripe offset $default_offset != -1"
2099 }
2100 run_test 27A "check filesystem-wide default LOV EA values"
2101
2102 test_27B() { # LU-2523
2103         test_mkdir $DIR/$tdir
2104         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2105         touch $DIR/$tdir/f0
2106         # open f1 with O_LOV_DELAY_CREATE
2107         # rename f0 onto f1
2108         # call setstripe ioctl on open file descriptor for f1
2109         # close
2110         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2111                 $DIR/$tdir/f0
2112
2113         rm -f $DIR/$tdir/f1
2114         # open f1 with O_LOV_DELAY_CREATE
2115         # unlink f1
2116         # call setstripe ioctl on open file descriptor for f1
2117         # close
2118         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2119
2120         # Allow multiop to fail in imitation of NFS's busted semantics.
2121         true
2122 }
2123 run_test 27B "call setstripe on open unlinked file/rename victim"
2124
2125 test_27C() { #LU-2871
2126         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2127
2128         declare -a ost_idx
2129         local index
2130         local found
2131         local i
2132         local j
2133
2134         test_mkdir $DIR/$tdir
2135         cd $DIR/$tdir
2136         for i in $(seq 0 $((OSTCOUNT - 1))); do
2137                 # set stripe across all OSTs starting from OST$i
2138                 $SETSTRIPE -i $i -c -1 $tfile$i
2139                 # get striping information
2140                 ost_idx=($($GETSTRIPE $tfile$i |
2141                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2142                 echo ${ost_idx[@]}
2143
2144                 # check the layout
2145                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2146                         error "${#ost_idx[@]} != $OSTCOUNT"
2147
2148                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2149                         found=0
2150                         for j in $(echo ${ost_idx[@]}); do
2151                                 if [ $index -eq $j ]; then
2152                                         found=1
2153                                         break
2154                                 fi
2155                         done
2156                         [ $found = 1 ] ||
2157                                 error "Can not find $index in ${ost_idx[@]}"
2158                 done
2159         done
2160 }
2161 run_test 27C "check full striping across all OSTs"
2162
2163 test_27D() {
2164         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2165         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2166         remote_mds_nodsh && skip "remote MDS with nodsh"
2167
2168         local POOL=${POOL:-testpool}
2169         local first_ost=0
2170         local last_ost=$(($OSTCOUNT - 1))
2171         local ost_step=1
2172         local ost_list=$(seq $first_ost $ost_step $last_ost)
2173         local ost_range="$first_ost $last_ost $ost_step"
2174
2175         if ! combined_mgs_mds ; then
2176                 mount_mgs_client
2177         fi
2178
2179         test_mkdir $DIR/$tdir
2180         pool_add $POOL || error "pool_add failed"
2181         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2182
2183         local skip27D
2184         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2185                 skip27D+="-s 29"
2186         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2187           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2188                 skip27D+=" -s 30,31"
2189         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2190                 error "llapi_layout_test failed"
2191
2192         destroy_test_pools || error "destroy test pools failed"
2193
2194         if ! combined_mgs_mds ; then
2195                 umount_mgs_client
2196         fi
2197 }
2198 run_test 27D "validate llapi_layout API"
2199
2200 # Verify that default_easize is increased from its initial value after
2201 # accessing a widely striped file.
2202 test_27E() {
2203         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2204         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2205                 skip "client does not have LU-3338 fix"
2206
2207         # 72 bytes is the minimum space required to store striping
2208         # information for a file striped across one OST:
2209         # (sizeof(struct lov_user_md_v3) +
2210         #  sizeof(struct lov_user_ost_data_v1))
2211         local min_easize=72
2212         $LCTL set_param -n llite.*.default_easize $min_easize ||
2213                 error "lctl set_param failed"
2214         local easize=$($LCTL get_param -n llite.*.default_easize)
2215
2216         [ $easize -eq $min_easize ] ||
2217                 error "failed to set default_easize"
2218
2219         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2220                 error "setstripe failed"
2221         cat $DIR/$tfile
2222         rm $DIR/$tfile
2223
2224         easize=$($LCTL get_param -n llite.*.default_easize)
2225
2226         [ $easize -gt $min_easize ] ||
2227                 error "default_easize not updated"
2228 }
2229 run_test 27E "check that default extended attribute size properly increases"
2230
2231 test_27F() { # LU-5346/LU-7975
2232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2233         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2234         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2235                 skip "Need MDS version at least 2.8.51"
2236         remote_ost_nodsh && skip "remote OST with nodsh"
2237
2238         test_mkdir $DIR/$tdir
2239         rm -f $DIR/$tdir/f0
2240         $SETSTRIPE -c 2 $DIR/$tdir
2241
2242         # stop all OSTs to reproduce situation for LU-7975 ticket
2243         for num in $(seq $OSTCOUNT); do
2244                 stop ost$num
2245         done
2246
2247         # open/create f0 with O_LOV_DELAY_CREATE
2248         # truncate f0 to a non-0 size
2249         # close
2250         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2251
2252         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2253         # open/write it again to force delayed layout creation
2254         cat /etc/hosts > $DIR/$tdir/f0 &
2255         catpid=$!
2256
2257         # restart OSTs
2258         for num in $(seq $OSTCOUNT); do
2259                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2260                         error "ost$num failed to start"
2261         done
2262
2263         wait $catpid || error "cat failed"
2264
2265         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2266         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2267
2268 }
2269 run_test 27F "Client resend delayed layout creation with non-zero size"
2270
2271 test_27G() { #LU-10629
2272         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2273         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2274         local POOL=${POOL:-testpool}
2275         local ostrange="0 0 1"
2276
2277         test_mkdir $DIR/$tdir
2278         pool_add $POOL || error "pool_add failed"
2279         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2280         $LFS setstripe -p $POOL $DIR/$tdir
2281
2282         local pool=$($LFS getstripe -p $DIR/$tdir)
2283
2284         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2285
2286         $LFS setstripe -d $DIR/$tdir
2287
2288         pool=$($LFS getstripe -p $DIR/$tdir)
2289
2290         rmdir $DIR/$tdir
2291
2292         [ -z "$pool" ] || error "'$pool' is not empty"
2293 }
2294 run_test 27G "Clear OST pool from stripe"
2295
2296 test_27H() {
2297         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.11.54) ]] &&
2298                 skip "Need MDS version newer than 2.11.54"
2299         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2300         test_mkdir $DIR/$tdir
2301         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2302         touch $DIR/$tdir/$tfile
2303         $LFS getstripe -c $DIR/$tdir/$tfile
2304         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2305                 error "two-stripe file doesn't have two stripes"
2306
2307         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2308         $LFS getstripe -y $DIR/$tdir/$tfile
2309         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2310              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2311                 error "expected l_ost_idx: [02]$ not matched"
2312
2313         # make sure ost list have been cleared
2314         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
2315         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2316                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2317         touch $DIR/$tdir/f3
2318         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2319 }
2320 run_test 27H "Set specific OSTs stripe"
2321
2322 # createtest also checks that device nodes are created and
2323 # then visible correctly (#2091)
2324 test_28() { # bug 2091
2325         test_mkdir $DIR/d28
2326         $CREATETEST $DIR/d28/ct || error "createtest failed"
2327 }
2328 run_test 28 "create/mknod/mkdir with bad file types ============"
2329
2330 test_29() {
2331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2332
2333         sync; sleep 1; sync # flush out any dirty pages from previous tests
2334         cancel_lru_locks
2335         test_mkdir $DIR/d29
2336         touch $DIR/d29/foo
2337         log 'first d29'
2338         ls -l $DIR/d29
2339
2340         declare -i LOCKCOUNTORIG=0
2341         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2342                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2343         done
2344         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2345
2346         declare -i LOCKUNUSEDCOUNTORIG=0
2347         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2348                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2349         done
2350
2351         log 'second d29'
2352         ls -l $DIR/d29
2353         log 'done'
2354
2355         declare -i LOCKCOUNTCURRENT=0
2356         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2357                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2358         done
2359
2360         declare -i LOCKUNUSEDCOUNTCURRENT=0
2361         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2362                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2363         done
2364
2365         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2366                 $LCTL set_param -n ldlm.dump_namespaces ""
2367                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2368                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2369                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2370                 return 2
2371         fi
2372         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2373                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2374                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2375                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2376                 return 3
2377         fi
2378 }
2379 run_test 29 "IT_GETATTR regression  ============================"
2380
2381 test_30a() { # was test_30
2382         cp $(which ls) $DIR || cp /bin/ls $DIR
2383         $DIR/ls / || error "Can't execute binary from lustre"
2384         rm $DIR/ls
2385 }
2386 run_test 30a "execute binary from Lustre (execve) =============="
2387
2388 test_30b() {
2389         cp `which ls` $DIR || cp /bin/ls $DIR
2390         chmod go+rx $DIR/ls
2391         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2392         rm $DIR/ls
2393 }
2394 run_test 30b "execute binary from Lustre as non-root ==========="
2395
2396 test_30c() { # b=22376
2397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2398
2399         cp `which ls` $DIR || cp /bin/ls $DIR
2400         chmod a-rw $DIR/ls
2401         cancel_lru_locks mdc
2402         cancel_lru_locks osc
2403         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2404         rm -f $DIR/ls
2405 }
2406 run_test 30c "execute binary from Lustre without read perms ===="
2407
2408 test_31a() {
2409         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2410         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2411 }
2412 run_test 31a "open-unlink file =================================="
2413
2414 test_31b() {
2415         touch $DIR/f31 || error "touch $DIR/f31 failed"
2416         ln $DIR/f31 $DIR/f31b || error "ln failed"
2417         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2418         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2419 }
2420 run_test 31b "unlink file with multiple links while open ======="
2421
2422 test_31c() {
2423         touch $DIR/f31 || error "touch $DIR/f31 failed"
2424         ln $DIR/f31 $DIR/f31c || error "ln failed"
2425         multiop_bg_pause $DIR/f31 O_uc ||
2426                 error "multiop_bg_pause for $DIR/f31 failed"
2427         MULTIPID=$!
2428         $MULTIOP $DIR/f31c Ouc
2429         kill -USR1 $MULTIPID
2430         wait $MULTIPID
2431 }
2432 run_test 31c "open-unlink file with multiple links ============="
2433
2434 test_31d() {
2435         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2436         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2437 }
2438 run_test 31d "remove of open directory ========================="
2439
2440 test_31e() { # bug 2904
2441         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2442 }
2443 run_test 31e "remove of open non-empty directory ==============="
2444
2445 test_31f() { # bug 4554
2446         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2447
2448         set -vx
2449         test_mkdir $DIR/d31f
2450         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2451         cp /etc/hosts $DIR/d31f
2452         ls -l $DIR/d31f
2453         $GETSTRIPE $DIR/d31f/hosts
2454         multiop_bg_pause $DIR/d31f D_c || return 1
2455         MULTIPID=$!
2456
2457         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2458         test_mkdir $DIR/d31f
2459         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2460         cp /etc/hosts $DIR/d31f
2461         ls -l $DIR/d31f
2462         $GETSTRIPE $DIR/d31f/hosts
2463         multiop_bg_pause $DIR/d31f D_c || return 1
2464         MULTIPID2=$!
2465
2466         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2467         wait $MULTIPID || error "first opendir $MULTIPID failed"
2468
2469         sleep 6
2470
2471         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2472         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2473         set +vx
2474 }
2475 run_test 31f "remove of open directory with open-unlink file ==="
2476
2477 test_31g() {
2478         echo "-- cross directory link --"
2479         test_mkdir -c1 $DIR/${tdir}ga
2480         test_mkdir -c1 $DIR/${tdir}gb
2481         touch $DIR/${tdir}ga/f
2482         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2483         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2484         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2485         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2486         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2487 }
2488 run_test 31g "cross directory link==============="
2489
2490 test_31h() {
2491         echo "-- cross directory link --"
2492         test_mkdir -c1 $DIR/${tdir}
2493         test_mkdir -c1 $DIR/${tdir}/dir
2494         touch $DIR/${tdir}/f
2495         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2496         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2497         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2498         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2499         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2500 }
2501 run_test 31h "cross directory link under child==============="
2502
2503 test_31i() {
2504         echo "-- cross directory link --"
2505         test_mkdir -c1 $DIR/$tdir
2506         test_mkdir -c1 $DIR/$tdir/dir
2507         touch $DIR/$tdir/dir/f
2508         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2509         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2510         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2511         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2512         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2513 }
2514 run_test 31i "cross directory link under parent==============="
2515
2516 test_31j() {
2517         test_mkdir -c1 -p $DIR/$tdir
2518         test_mkdir -c1 -p $DIR/$tdir/dir1
2519         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2520         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2521         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2522         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2523         return 0
2524 }
2525 run_test 31j "link for directory==============="
2526
2527 test_31k() {
2528         test_mkdir -c1 -p $DIR/$tdir
2529         touch $DIR/$tdir/s
2530         touch $DIR/$tdir/exist
2531         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2532         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2533         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2534         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2535         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2536         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2537         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2538         return 0
2539 }
2540 run_test 31k "link to file: the same, non-existing, dir==============="
2541
2542 test_31m() {
2543         mkdir $DIR/d31m
2544         touch $DIR/d31m/s
2545         mkdir $DIR/d31m2
2546         touch $DIR/d31m2/exist
2547         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2548         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2549         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2550         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2551         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2552         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2553         return 0
2554 }
2555 run_test 31m "link to file: the same, non-existing, dir==============="
2556
2557 test_31n() {
2558         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2559         nlink=$(stat --format=%h $DIR/$tfile)
2560         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2561         local fd=$(free_fd)
2562         local cmd="exec $fd<$DIR/$tfile"
2563         eval $cmd
2564         cmd="exec $fd<&-"
2565         trap "eval $cmd" EXIT
2566         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2567         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2568         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2569         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2570         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2571         eval $cmd
2572 }
2573 run_test 31n "check link count of unlinked file"
2574
2575 link_one() {
2576         local TEMPNAME=$(mktemp $1_XXXXXX)
2577         mlink $TEMPNAME $1 2> /dev/null &&
2578                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2579         munlink $TEMPNAME
2580 }
2581
2582 test_31o() { # LU-2901
2583         test_mkdir $DIR/$tdir
2584         for LOOP in $(seq 100); do
2585                 rm -f $DIR/$tdir/$tfile*
2586                 for THREAD in $(seq 8); do
2587                         link_one $DIR/$tdir/$tfile.$LOOP &
2588                 done
2589                 wait
2590                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2591                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2592                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2593                         break || true
2594         done
2595 }
2596 run_test 31o "duplicate hard links with same filename"
2597
2598 test_31p() {
2599         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2600
2601         test_mkdir $DIR/$tdir
2602         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2603         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2604
2605         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2606                 error "open unlink test1 failed"
2607         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2608                 error "open unlink test2 failed"
2609
2610         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2611                 error "test1 still exists"
2612         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2613                 error "test2 still exists"
2614 }
2615 run_test 31p "remove of open striped directory"
2616
2617 cleanup_test32_mount() {
2618         local rc=0
2619         trap 0
2620         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2621         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2622         losetup -d $loopdev || true
2623         rm -rf $DIR/$tdir
2624         return $rc
2625 }
2626
2627 test_32a() {
2628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2629
2630         echo "== more mountpoints and symlinks ================="
2631         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2632         trap cleanup_test32_mount EXIT
2633         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2634         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2635                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2636         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2637                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2638         cleanup_test32_mount
2639 }
2640 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2641
2642 test_32b() {
2643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2644
2645         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2646         trap cleanup_test32_mount EXIT
2647         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2648         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2649                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2650         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2651                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2652         cleanup_test32_mount
2653 }
2654 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2655
2656 test_32c() {
2657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2658
2659         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2660         trap cleanup_test32_mount EXIT
2661         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2662         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2663                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2664         test_mkdir -p $DIR/$tdir/d2/test_dir
2665         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2666                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2667         cleanup_test32_mount
2668 }
2669 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2670
2671 test_32d() {
2672         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2673
2674         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2675         trap cleanup_test32_mount EXIT
2676         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2677         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2678                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2679         test_mkdir -p $DIR/$tdir/d2/test_dir
2680         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2681                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2682         cleanup_test32_mount
2683 }
2684 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2685
2686 test_32e() {
2687         rm -fr $DIR/$tdir
2688         test_mkdir -p $DIR/$tdir/tmp
2689         local tmp_dir=$DIR/$tdir/tmp
2690         ln -s $DIR/$tdir $tmp_dir/symlink11
2691         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2692         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2693         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2694 }
2695 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2696
2697 test_32f() {
2698         rm -fr $DIR/$tdir
2699         test_mkdir -p $DIR/$tdir/tmp
2700         local tmp_dir=$DIR/$tdir/tmp
2701         ln -s $DIR/$tdir $tmp_dir/symlink11
2702         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2703         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2704         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2705 }
2706 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2707
2708 test_32g() {
2709         local tmp_dir=$DIR/$tdir/tmp
2710         test_mkdir -p $tmp_dir
2711         test_mkdir $DIR/${tdir}2
2712         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2713         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2714         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2715         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2716         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2717         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2718 }
2719 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2720
2721 test_32h() {
2722         rm -fr $DIR/$tdir $DIR/${tdir}2
2723         tmp_dir=$DIR/$tdir/tmp
2724         test_mkdir -p $tmp_dir
2725         test_mkdir $DIR/${tdir}2
2726         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2727         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2728         ls $tmp_dir/symlink12 || error "listing symlink12"
2729         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2730 }
2731 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2732
2733 test_32i() {
2734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2735
2736         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2737         trap cleanup_test32_mount EXIT
2738         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2739         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2740                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2741         touch $DIR/$tdir/test_file
2742         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2743                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2744         cleanup_test32_mount
2745 }
2746 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2747
2748 test_32j() {
2749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2750
2751         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2752         trap cleanup_test32_mount EXIT
2753         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2754         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2755                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2756         touch $DIR/$tdir/test_file
2757         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2758                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2759         cleanup_test32_mount
2760 }
2761 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2762
2763 test_32k() {
2764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2765
2766         rm -fr $DIR/$tdir
2767         trap cleanup_test32_mount EXIT
2768         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2769         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2770                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2771         test_mkdir -p $DIR/$tdir/d2
2772         touch $DIR/$tdir/d2/test_file || error "touch failed"
2773         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2774                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
2775         cleanup_test32_mount
2776 }
2777 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2778
2779 test_32l() {
2780         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2781
2782         rm -fr $DIR/$tdir
2783         trap cleanup_test32_mount EXIT
2784         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2785         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2786                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2787         test_mkdir -p $DIR/$tdir/d2
2788         touch $DIR/$tdir/d2/test_file || error "touch failed"
2789         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2790                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
2791         cleanup_test32_mount
2792 }
2793 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2794
2795 test_32m() {
2796         rm -fr $DIR/d32m
2797         test_mkdir -p $DIR/d32m/tmp
2798         TMP_DIR=$DIR/d32m/tmp
2799         ln -s $DIR $TMP_DIR/symlink11
2800         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2801         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
2802                 error "symlink11 not a link"
2803         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
2804                 error "symlink01 not a link"
2805 }
2806 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2807
2808 test_32n() {
2809         rm -fr $DIR/d32n
2810         test_mkdir -p $DIR/d32n/tmp
2811         TMP_DIR=$DIR/d32n/tmp
2812         ln -s $DIR $TMP_DIR/symlink11
2813         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2814         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
2815         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
2816 }
2817 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2818
2819 test_32o() {
2820         touch $DIR/$tfile
2821         test_mkdir -p $DIR/d32o/tmp
2822         TMP_DIR=$DIR/d32o/tmp
2823         ln -s $DIR/$tfile $TMP_DIR/symlink12
2824         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2825         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
2826                 error "symlink12 not a link"
2827         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
2828         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
2829                 error "$DIR/d32o/tmp/symlink12 not file type"
2830         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
2831                 error "$DIR/d32o/symlink02 not file type"
2832 }
2833 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2834
2835 test_32p() {
2836         log 32p_1
2837         rm -fr $DIR/d32p
2838         log 32p_2
2839         rm -f $DIR/$tfile
2840         log 32p_3
2841         touch $DIR/$tfile
2842         log 32p_4
2843         test_mkdir -p $DIR/d32p/tmp
2844         log 32p_5
2845         TMP_DIR=$DIR/d32p/tmp
2846         log 32p_6
2847         ln -s $DIR/$tfile $TMP_DIR/symlink12
2848         log 32p_7
2849         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2850         log 32p_8
2851         cat $DIR/d32p/tmp/symlink12 ||
2852                 error "Can't open $DIR/d32p/tmp/symlink12"
2853         log 32p_9
2854         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
2855         log 32p_10
2856 }
2857 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2858
2859 test_32q() {
2860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2861
2862         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2863         trap cleanup_test32_mount EXIT
2864         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2865         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2866         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2867                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2868         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2869         cleanup_test32_mount
2870 }
2871 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2872
2873 test_32r() {
2874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2875
2876         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2877         trap cleanup_test32_mount EXIT
2878         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2879         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2880         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2881                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2882         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2883         cleanup_test32_mount
2884 }
2885 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2886
2887 test_33aa() {
2888         rm -f $DIR/$tfile
2889         touch $DIR/$tfile
2890         chmod 444 $DIR/$tfile
2891         chown $RUNAS_ID $DIR/$tfile
2892         log 33_1
2893         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2894         log 33_2
2895 }
2896 run_test 33aa "write file with mode 444 (should return error)"
2897
2898 test_33a() {
2899         rm -fr $DIR/$tdir
2900         test_mkdir $DIR/$tdir
2901         chown $RUNAS_ID $DIR/$tdir
2902         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2903                 error "$RUNAS create $tdir/$tfile failed"
2904         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2905                 error "open RDWR" || true
2906 }
2907 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2908
2909 test_33b() {
2910         rm -fr $DIR/$tdir
2911         test_mkdir $DIR/$tdir
2912         chown $RUNAS_ID $DIR/$tdir
2913         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2914 }
2915 run_test 33b "test open file with malformed flags (No panic)"
2916
2917 test_33c() {
2918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2919         remote_ost_nodsh && skip "remote OST with nodsh"
2920
2921         local ostnum
2922         local ostname
2923         local write_bytes
2924         local all_zeros
2925
2926         all_zeros=:
2927         rm -fr $DIR/$tdir
2928         test_mkdir $DIR/$tdir
2929         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2930
2931         sync
2932         for ostnum in $(seq $OSTCOUNT); do
2933                 # test-framework's OST numbering is one-based, while Lustre's
2934                 # is zero-based
2935                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2936                 # Parsing llobdstat's output sucks; we could grep the /proc
2937                 # path, but that's likely to not be as portable as using the
2938                 # llobdstat utility.  So we parse lctl output instead.
2939                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2940                         obdfilter/$ostname/stats |
2941                         awk '/^write_bytes/ {print $7}' )
2942                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2943                 if (( ${write_bytes:-0} > 0 ))
2944                 then
2945                         all_zeros=false
2946                         break;
2947                 fi
2948         done
2949
2950         $all_zeros || return 0
2951
2952         # Write four bytes
2953         echo foo > $DIR/$tdir/bar
2954         # Really write them
2955         sync
2956
2957         # Total up write_bytes after writing.  We'd better find non-zeros.
2958         for ostnum in $(seq $OSTCOUNT); do
2959                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2960                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2961                         obdfilter/$ostname/stats |
2962                         awk '/^write_bytes/ {print $7}' )
2963                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2964                 if (( ${write_bytes:-0} > 0 ))
2965                 then
2966                         all_zeros=false
2967                         break;
2968                 fi
2969         done
2970
2971         if $all_zeros
2972         then
2973                 for ostnum in $(seq $OSTCOUNT); do
2974                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2975                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2976                         do_facet ost$ostnum lctl get_param -n \
2977                                 obdfilter/$ostname/stats
2978                 done
2979                 error "OST not keeping write_bytes stats (b22312)"
2980         fi
2981 }
2982 run_test 33c "test llobdstat and write_bytes"
2983
2984 test_33d() {
2985         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
2986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2987
2988         local MDTIDX=1
2989         local remote_dir=$DIR/$tdir/remote_dir
2990
2991         test_mkdir $DIR/$tdir
2992         $LFS mkdir -i $MDTIDX $remote_dir ||
2993                 error "create remote directory failed"
2994
2995         touch $remote_dir/$tfile
2996         chmod 444 $remote_dir/$tfile
2997         chown $RUNAS_ID $remote_dir/$tfile
2998
2999         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3000
3001         chown $RUNAS_ID $remote_dir
3002         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3003                                         error "create" || true
3004         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3005                                     error "open RDWR" || true
3006         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3007 }
3008 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3009
3010 test_33e() {
3011         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3012
3013         mkdir $DIR/$tdir
3014
3015         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3016         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3017         mkdir $DIR/$tdir/local_dir
3018
3019         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3020         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3021         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3022
3023         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3024                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3025
3026         rmdir $DIR/$tdir/* || error "rmdir failed"
3027
3028         umask 777
3029         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3030         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3031         mkdir $DIR/$tdir/local_dir
3032
3033         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3034         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3035         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3036
3037         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3038                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3039
3040         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3041
3042         umask 000
3043         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3044         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3045         mkdir $DIR/$tdir/local_dir
3046
3047         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3048         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3049         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3050
3051         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3052                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3053 }
3054 run_test 33e "mkdir and striped directory should have same mode"
3055
3056 cleanup_33f() {
3057         trap 0
3058         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3059 }
3060
3061 test_33f() {
3062         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3063         remote_mds_nodsh && skip "remote MDS with nodsh"
3064
3065         mkdir $DIR/$tdir
3066         chmod go+rwx $DIR/$tdir
3067         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3068         trap cleanup_33f EXIT
3069
3070         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3071                 error "cannot create striped directory"
3072
3073         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3074                 error "cannot create files in striped directory"
3075
3076         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3077                 error "cannot remove files in striped directory"
3078
3079         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3080                 error "cannot remove striped directory"
3081
3082         cleanup_33f
3083 }
3084 run_test 33f "nonroot user can create, access, and remove a striped directory"
3085
3086 test_33g() {
3087         mkdir -p $DIR/$tdir/dir2
3088
3089         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3090         echo $err
3091         [[ $err =~ "exists" ]] || error "Not exists error"
3092 }
3093 run_test 33g "nonroot user create already existing root created file"
3094
3095 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3096 test_34a() {
3097         rm -f $DIR/f34
3098         $MCREATE $DIR/f34 || error "mcreate failed"
3099         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3100                 error "getstripe failed"
3101         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3102         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3103                 error "getstripe failed"
3104         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3105                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3106 }
3107 run_test 34a "truncate file that has not been opened ==========="
3108
3109 test_34b() {
3110         [ ! -f $DIR/f34 ] && test_34a
3111         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3112                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3113         $OPENFILE -f O_RDONLY $DIR/f34
3114         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3115                 error "getstripe failed"
3116         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3117                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3118 }
3119 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3120
3121 test_34c() {
3122         [ ! -f $DIR/f34 ] && test_34a
3123         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3124                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3125         $OPENFILE -f O_RDWR $DIR/f34
3126         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3127         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3128                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3129 }
3130 run_test 34c "O_RDWR opening file-with-size works =============="
3131
3132 test_34d() {
3133         [ ! -f $DIR/f34 ] && test_34a
3134         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3135                 error "dd failed"
3136         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3137                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3138         rm $DIR/f34
3139 }
3140 run_test 34d "write to sparse file ============================="
3141
3142 test_34e() {
3143         rm -f $DIR/f34e
3144         $MCREATE $DIR/f34e || error "mcreate failed"
3145         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3146         $CHECKSTAT -s 1000 $DIR/f34e ||
3147                 error "Size of $DIR/f34e not equal to 1000 bytes"
3148         $OPENFILE -f O_RDWR $DIR/f34e
3149         $CHECKSTAT -s 1000 $DIR/f34e ||
3150                 error "Size of $DIR/f34e not equal to 1000 bytes"
3151 }
3152 run_test 34e "create objects, some with size and some without =="
3153
3154 test_34f() { # bug 6242, 6243
3155         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3156
3157         SIZE34F=48000
3158         rm -f $DIR/f34f
3159         $MCREATE $DIR/f34f || error "mcreate failed"
3160         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3161         dd if=$DIR/f34f of=$TMP/f34f
3162         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3163         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3164         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3165         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3166         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3167 }
3168 run_test 34f "read from a file with no objects until EOF ======="
3169
3170 test_34g() {
3171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3172
3173         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3174                 error "dd failed"
3175         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3176         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3177                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3178         cancel_lru_locks osc
3179         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3180                 error "wrong size after lock cancel"
3181
3182         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3183         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3184                 error "expanding truncate failed"
3185         cancel_lru_locks osc
3186         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3187                 error "wrong expanded size after lock cancel"
3188 }
3189 run_test 34g "truncate long file ==============================="
3190
3191 test_34h() {
3192         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3193
3194         local gid=10
3195         local sz=1000
3196
3197         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3198         sync # Flush the cache so that multiop below does not block on cache
3199              # flush when getting the group lock
3200         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3201         MULTIPID=$!
3202
3203         # Since just timed wait is not good enough, let's do a sync write
3204         # that way we are sure enough time for a roundtrip + processing
3205         # passed + 2 seconds of extra margin.
3206         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3207         rm $DIR/${tfile}-1
3208         sleep 2
3209
3210         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3211                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3212                 kill -9 $MULTIPID
3213         fi
3214         wait $MULTIPID
3215         local nsz=`stat -c %s $DIR/$tfile`
3216         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3217 }
3218 run_test 34h "ftruncate file under grouplock should not block"
3219
3220 test_35a() {
3221         cp /bin/sh $DIR/f35a
3222         chmod 444 $DIR/f35a
3223         chown $RUNAS_ID $DIR/f35a
3224         $RUNAS $DIR/f35a && error || true
3225         rm $DIR/f35a
3226 }
3227 run_test 35a "exec file with mode 444 (should return and not leak)"
3228
3229 test_36a() {
3230         rm -f $DIR/f36
3231         utime $DIR/f36 || error "utime failed for MDS"
3232 }
3233 run_test 36a "MDS utime check (mknod, utime)"
3234
3235 test_36b() {
3236         echo "" > $DIR/f36
3237         utime $DIR/f36 || error "utime failed for OST"
3238 }
3239 run_test 36b "OST utime check (open, utime)"
3240
3241 test_36c() {
3242         rm -f $DIR/d36/f36
3243         test_mkdir $DIR/d36
3244         chown $RUNAS_ID $DIR/d36
3245         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3246 }
3247 run_test 36c "non-root MDS utime check (mknod, utime)"
3248
3249 test_36d() {
3250         [ ! -d $DIR/d36 ] && test_36c
3251         echo "" > $DIR/d36/f36
3252         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3253 }
3254 run_test 36d "non-root OST utime check (open, utime)"
3255
3256 test_36e() {
3257         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3258
3259         test_mkdir $DIR/$tdir
3260         touch $DIR/$tdir/$tfile
3261         $RUNAS utime $DIR/$tdir/$tfile &&
3262                 error "utime worked, expected failure" || true
3263 }
3264 run_test 36e "utime on non-owned file (should return error)"
3265
3266 subr_36fh() {
3267         local fl="$1"
3268         local LANG_SAVE=$LANG
3269         local LC_LANG_SAVE=$LC_LANG
3270         export LANG=C LC_LANG=C # for date language
3271
3272         DATESTR="Dec 20  2000"
3273         test_mkdir $DIR/$tdir
3274         lctl set_param fail_loc=$fl
3275         date; date +%s
3276         cp /etc/hosts $DIR/$tdir/$tfile
3277         sync & # write RPC generated with "current" inode timestamp, but delayed
3278         sleep 1
3279         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3280         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3281         cancel_lru_locks osc
3282         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3283         date; date +%s
3284         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3285                 echo "BEFORE: $LS_BEFORE" && \
3286                 echo "AFTER : $LS_AFTER" && \
3287                 echo "WANT  : $DATESTR" && \
3288                 error "$DIR/$tdir/$tfile timestamps changed" || true
3289
3290         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3291 }
3292
3293 test_36f() {
3294         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3295
3296         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3297         subr_36fh "0x80000214"
3298 }
3299 run_test 36f "utime on file racing with OST BRW write =========="
3300
3301 test_36g() {
3302         remote_ost_nodsh && skip "remote OST with nodsh"
3303         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3304
3305         local fmd_max_age
3306         local fmd_before
3307         local fmd_after
3308
3309         test_mkdir $DIR/$tdir
3310         fmd_max_age=$(do_facet ost1 \
3311                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3312                 head -n 1")
3313
3314         fmd_before=$(do_facet ost1 \
3315                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3316         touch $DIR/$tdir/$tfile
3317         sleep $((fmd_max_age + 12))
3318         fmd_after=$(do_facet ost1 \
3319                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3320
3321         echo "fmd_before: $fmd_before"
3322         echo "fmd_after: $fmd_after"
3323         [[ $fmd_after -gt $fmd_before ]] &&
3324                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3325                 error "fmd didn't expire after ping" || true
3326 }
3327 run_test 36g "filter mod data cache expiry ====================="
3328
3329 test_36h() {
3330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3331
3332         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3333         subr_36fh "0x80000227"
3334 }
3335 run_test 36h "utime on file racing with OST BRW write =========="
3336
3337 test_36i() {
3338         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3339
3340         test_mkdir $DIR/$tdir
3341         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3342
3343         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3344         local new_mtime=$((mtime + 200))
3345
3346         #change Modify time of striped dir
3347         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3348                         error "change mtime failed"
3349
3350         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3351
3352         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3353 }
3354 run_test 36i "change mtime on striped directory"
3355
3356 # test_37 - duplicate with tests 32q 32r
3357
3358 test_38() {
3359         local file=$DIR/$tfile
3360         touch $file
3361         openfile -f O_DIRECTORY $file
3362         local RC=$?
3363         local ENOTDIR=20
3364         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3365         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3366 }
3367 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3368
3369 test_39a() { # was test_39
3370         touch $DIR/$tfile
3371         touch $DIR/${tfile}2
3372 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3373 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3374 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3375         sleep 2
3376         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3377         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3378                 echo "mtime"
3379                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3380                 echo "atime"
3381                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3382                 echo "ctime"
3383                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3384                 error "O_TRUNC didn't change timestamps"
3385         fi
3386 }
3387 run_test 39a "mtime changed on create"
3388
3389 test_39b() {
3390         test_mkdir -c1 $DIR/$tdir
3391         cp -p /etc/passwd $DIR/$tdir/fopen
3392         cp -p /etc/passwd $DIR/$tdir/flink
3393         cp -p /etc/passwd $DIR/$tdir/funlink
3394         cp -p /etc/passwd $DIR/$tdir/frename
3395         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3396
3397         sleep 1
3398         echo "aaaaaa" >> $DIR/$tdir/fopen
3399         echo "aaaaaa" >> $DIR/$tdir/flink
3400         echo "aaaaaa" >> $DIR/$tdir/funlink
3401         echo "aaaaaa" >> $DIR/$tdir/frename
3402
3403         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3404         local link_new=`stat -c %Y $DIR/$tdir/flink`
3405         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3406         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3407
3408         cat $DIR/$tdir/fopen > /dev/null
3409         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3410         rm -f $DIR/$tdir/funlink2
3411         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3412
3413         for (( i=0; i < 2; i++ )) ; do
3414                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3415                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3416                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3417                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3418
3419                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3420                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3421                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3422                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3423
3424                 cancel_lru_locks osc
3425                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3426         done
3427 }
3428 run_test 39b "mtime change on open, link, unlink, rename  ======"
3429
3430 # this should be set to past
3431 TEST_39_MTIME=`date -d "1 year ago" +%s`
3432
3433 # bug 11063
3434 test_39c() {
3435         touch $DIR1/$tfile
3436         sleep 2
3437         local mtime0=`stat -c %Y $DIR1/$tfile`
3438
3439         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3440         local mtime1=`stat -c %Y $DIR1/$tfile`
3441         [ "$mtime1" = $TEST_39_MTIME ] || \
3442                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3443
3444         local d1=`date +%s`
3445         echo hello >> $DIR1/$tfile
3446         local d2=`date +%s`
3447         local mtime2=`stat -c %Y $DIR1/$tfile`
3448         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3449                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3450
3451         mv $DIR1/$tfile $DIR1/$tfile-1
3452
3453         for (( i=0; i < 2; i++ )) ; do
3454                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3455                 [ "$mtime2" = "$mtime3" ] || \
3456                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3457
3458                 cancel_lru_locks osc
3459                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3460         done
3461 }
3462 run_test 39c "mtime change on rename ==========================="
3463
3464 # bug 21114
3465 test_39d() {
3466         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3467
3468         touch $DIR1/$tfile
3469         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3470
3471         for (( i=0; i < 2; i++ )) ; do
3472                 local mtime=`stat -c %Y $DIR1/$tfile`
3473                 [ $mtime = $TEST_39_MTIME ] || \
3474                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3475
3476                 cancel_lru_locks osc
3477                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3478         done
3479 }
3480 run_test 39d "create, utime, stat =============================="
3481
3482 # bug 21114
3483 test_39e() {
3484         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3485
3486         touch $DIR1/$tfile
3487         local mtime1=`stat -c %Y $DIR1/$tfile`
3488
3489         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3490
3491         for (( i=0; i < 2; i++ )) ; do
3492                 local mtime2=`stat -c %Y $DIR1/$tfile`
3493                 [ $mtime2 = $TEST_39_MTIME ] || \
3494                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3495
3496                 cancel_lru_locks osc
3497                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3498         done
3499 }
3500 run_test 39e "create, stat, utime, stat ========================"
3501
3502 # bug 21114
3503 test_39f() {
3504         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3505
3506         touch $DIR1/$tfile
3507         mtime1=`stat -c %Y $DIR1/$tfile`
3508
3509         sleep 2
3510         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3511
3512         for (( i=0; i < 2; i++ )) ; do
3513                 local mtime2=`stat -c %Y $DIR1/$tfile`
3514                 [ $mtime2 = $TEST_39_MTIME ] || \
3515                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3516
3517                 cancel_lru_locks osc
3518                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3519         done
3520 }
3521 run_test 39f "create, stat, sleep, utime, stat ================="
3522
3523 # bug 11063
3524 test_39g() {
3525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3526
3527         echo hello >> $DIR1/$tfile
3528         local mtime1=`stat -c %Y $DIR1/$tfile`
3529
3530         sleep 2
3531         chmod o+r $DIR1/$tfile
3532
3533         for (( i=0; i < 2; i++ )) ; do
3534                 local mtime2=`stat -c %Y $DIR1/$tfile`
3535                 [ "$mtime1" = "$mtime2" ] || \
3536                         error "lost mtime: $mtime2, should be $mtime1"
3537
3538                 cancel_lru_locks osc
3539                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3540         done
3541 }
3542 run_test 39g "write, chmod, stat ==============================="
3543
3544 # bug 11063
3545 test_39h() {
3546         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3547
3548         touch $DIR1/$tfile
3549         sleep 1
3550
3551         local d1=`date`
3552         echo hello >> $DIR1/$tfile
3553         local mtime1=`stat -c %Y $DIR1/$tfile`
3554
3555         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3556         local d2=`date`
3557         if [ "$d1" != "$d2" ]; then
3558                 echo "write and touch not within one second"
3559         else
3560                 for (( i=0; i < 2; i++ )) ; do
3561                         local mtime2=`stat -c %Y $DIR1/$tfile`
3562                         [ "$mtime2" = $TEST_39_MTIME ] || \
3563                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3564
3565                         cancel_lru_locks osc
3566                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3567                 done
3568         fi
3569 }
3570 run_test 39h "write, utime within one second, stat ============="
3571
3572 test_39i() {
3573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3574
3575         touch $DIR1/$tfile
3576         sleep 1
3577
3578         echo hello >> $DIR1/$tfile
3579         local mtime1=`stat -c %Y $DIR1/$tfile`
3580
3581         mv $DIR1/$tfile $DIR1/$tfile-1
3582
3583         for (( i=0; i < 2; i++ )) ; do
3584                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3585
3586                 [ "$mtime1" = "$mtime2" ] || \
3587                         error "lost mtime: $mtime2, should be $mtime1"
3588
3589                 cancel_lru_locks osc
3590                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3591         done
3592 }
3593 run_test 39i "write, rename, stat =============================="
3594
3595 test_39j() {
3596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3597
3598         start_full_debug_logging
3599         touch $DIR1/$tfile
3600         sleep 1
3601
3602         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3603         lctl set_param fail_loc=0x80000412
3604         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3605                 error "multiop failed"
3606         local multipid=$!
3607         local mtime1=`stat -c %Y $DIR1/$tfile`
3608
3609         mv $DIR1/$tfile $DIR1/$tfile-1
3610
3611         kill -USR1 $multipid
3612         wait $multipid || error "multiop close failed"
3613
3614         for (( i=0; i < 2; i++ )) ; do
3615                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3616                 [ "$mtime1" = "$mtime2" ] ||
3617                         error "mtime is lost on close: $mtime2, " \
3618                               "should be $mtime1"
3619
3620                 cancel_lru_locks osc
3621                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3622         done
3623         lctl set_param fail_loc=0
3624         stop_full_debug_logging
3625 }
3626 run_test 39j "write, rename, close, stat ======================="
3627
3628 test_39k() {
3629         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3630
3631         touch $DIR1/$tfile
3632         sleep 1
3633
3634         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3635         local multipid=$!
3636         local mtime1=`stat -c %Y $DIR1/$tfile`
3637
3638         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3639
3640         kill -USR1 $multipid
3641         wait $multipid || error "multiop close failed"
3642
3643         for (( i=0; i < 2; i++ )) ; do
3644                 local mtime2=`stat -c %Y $DIR1/$tfile`
3645
3646                 [ "$mtime2" = $TEST_39_MTIME ] || \
3647                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3648
3649                 cancel_lru_locks osc
3650                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3651         done
3652 }
3653 run_test 39k "write, utime, close, stat ========================"
3654
3655 # this should be set to future
3656 TEST_39_ATIME=`date -d "1 year" +%s`
3657
3658 test_39l() {
3659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3660         remote_mds_nodsh && skip "remote MDS with nodsh"
3661
3662         local atime_diff=$(do_facet $SINGLEMDS \
3663                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3664         rm -rf $DIR/$tdir
3665         mkdir -p $DIR/$tdir
3666
3667         # test setting directory atime to future
3668         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3669         local atime=$(stat -c %X $DIR/$tdir)
3670         [ "$atime" = $TEST_39_ATIME ] ||
3671                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3672
3673         # test setting directory atime from future to now
3674         local now=$(date +%s)
3675         touch -a -d @$now $DIR/$tdir
3676
3677         atime=$(stat -c %X $DIR/$tdir)
3678         [ "$atime" -eq "$now"  ] ||
3679                 error "atime is not updated from future: $atime, $now"
3680
3681         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3682         sleep 3
3683
3684         # test setting directory atime when now > dir atime + atime_diff
3685         local d1=$(date +%s)
3686         ls $DIR/$tdir
3687         local d2=$(date +%s)
3688         cancel_lru_locks mdc
3689         atime=$(stat -c %X $DIR/$tdir)
3690         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3691                 error "atime is not updated  : $atime, should be $d2"
3692
3693         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3694         sleep 3
3695
3696         # test not setting directory atime when now < dir atime + atime_diff
3697         ls $DIR/$tdir
3698         cancel_lru_locks mdc
3699         atime=$(stat -c %X $DIR/$tdir)
3700         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3701                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3702
3703         do_facet $SINGLEMDS \
3704                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3705 }
3706 run_test 39l "directory atime update ==========================="
3707
3708 test_39m() {
3709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3710
3711         touch $DIR1/$tfile
3712         sleep 2
3713         local far_past_mtime=$(date -d "May 29 1953" +%s)
3714         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3715
3716         touch -m -d @$far_past_mtime $DIR1/$tfile
3717         touch -a -d @$far_past_atime $DIR1/$tfile
3718
3719         for (( i=0; i < 2; i++ )) ; do
3720                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3721                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3722                         error "atime or mtime set incorrectly"
3723
3724                 cancel_lru_locks osc
3725                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3726         done
3727 }
3728 run_test 39m "test atime and mtime before 1970"
3729
3730 test_39n() { # LU-3832
3731         remote_mds_nodsh && skip "remote MDS with nodsh"
3732
3733         local atime_diff=$(do_facet $SINGLEMDS \
3734                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3735         local atime0
3736         local atime1
3737         local atime2
3738
3739         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3740
3741         rm -rf $DIR/$tfile
3742         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3743         atime0=$(stat -c %X $DIR/$tfile)
3744
3745         sleep 5
3746         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3747         atime1=$(stat -c %X $DIR/$tfile)
3748
3749         sleep 5
3750         cancel_lru_locks mdc
3751         cancel_lru_locks osc
3752         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3753         atime2=$(stat -c %X $DIR/$tfile)
3754
3755         do_facet $SINGLEMDS \
3756                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3757
3758         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3759         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3760 }
3761 run_test 39n "check that O_NOATIME is honored"
3762
3763 test_39o() {
3764         TESTDIR=$DIR/$tdir/$tfile
3765         [ -e $TESTDIR ] && rm -rf $TESTDIR
3766         mkdir -p $TESTDIR
3767         cd $TESTDIR
3768         links1=2
3769         ls
3770         mkdir a b
3771         ls
3772         links2=$(stat -c %h .)
3773         [ $(($links1 + 2)) != $links2 ] &&
3774                 error "wrong links count $(($links1 + 2)) != $links2"
3775         rmdir b
3776         links3=$(stat -c %h .)
3777         [ $(($links1 + 1)) != $links3 ] &&
3778                 error "wrong links count $links1 != $links3"
3779         return 0
3780 }
3781 run_test 39o "directory cached attributes updated after create"
3782
3783 test_39p() {
3784         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3785
3786         local MDTIDX=1
3787         TESTDIR=$DIR/$tdir/$tdir
3788         [ -e $TESTDIR ] && rm -rf $TESTDIR
3789         test_mkdir -p $TESTDIR
3790         cd $TESTDIR
3791         links1=2
3792         ls
3793         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3794         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3795         ls
3796         links2=$(stat -c %h .)
3797         [ $(($links1 + 2)) != $links2 ] &&
3798                 error "wrong links count $(($links1 + 2)) != $links2"
3799         rmdir remote_dir2
3800         links3=$(stat -c %h .)
3801         [ $(($links1 + 1)) != $links3 ] &&
3802                 error "wrong links count $links1 != $links3"
3803         return 0
3804 }
3805 run_test 39p "remote directory cached attributes updated after create ========"
3806
3807
3808 test_39q() { # LU-8041
3809         local testdir=$DIR/$tdir
3810         mkdir -p $testdir
3811         multiop_bg_pause $testdir D_c || error "multiop failed"
3812         local multipid=$!
3813         cancel_lru_locks mdc
3814         kill -USR1 $multipid
3815         local atime=$(stat -c %X $testdir)
3816         [ "$atime" -ne 0 ] || error "atime is zero"
3817 }
3818 run_test 39q "close won't zero out atime"
3819
3820 test_40() {
3821         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3822         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3823                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3824         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3825                 error "$tfile is not 4096 bytes in size"
3826 }
3827 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3828
3829 test_41() {
3830         # bug 1553
3831         small_write $DIR/f41 18
3832 }
3833 run_test 41 "test small file write + fstat ====================="
3834
3835 count_ost_writes() {
3836         lctl get_param -n ${OSC}.*.stats |
3837                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3838                         END { printf("%0.0f", writes) }'
3839 }
3840
3841 # decent default
3842 WRITEBACK_SAVE=500
3843 DIRTY_RATIO_SAVE=40
3844 MAX_DIRTY_RATIO=50
3845 BG_DIRTY_RATIO_SAVE=10
3846 MAX_BG_DIRTY_RATIO=25
3847
3848 start_writeback() {
3849         trap 0
3850         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3851         # dirty_ratio, dirty_background_ratio
3852         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3853                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3854                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3855                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3856         else
3857                 # if file not here, we are a 2.4 kernel
3858                 kill -CONT `pidof kupdated`
3859         fi
3860 }
3861
3862 stop_writeback() {
3863         # setup the trap first, so someone cannot exit the test at the
3864         # exact wrong time and mess up a machine
3865         trap start_writeback EXIT
3866         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3867         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3868                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3869                 sysctl -w vm.dirty_writeback_centisecs=0
3870                 sysctl -w vm.dirty_writeback_centisecs=0
3871                 # save and increase /proc/sys/vm/dirty_ratio
3872                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3873                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3874                 # save and increase /proc/sys/vm/dirty_background_ratio
3875                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3876                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3877         else
3878                 # if file not here, we are a 2.4 kernel
3879                 kill -STOP `pidof kupdated`
3880         fi
3881 }
3882
3883 # ensure that all stripes have some grant before we test client-side cache
3884 setup_test42() {
3885         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3886                 dd if=/dev/zero of=$i bs=4k count=1
3887                 rm $i
3888         done
3889 }
3890
3891 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3892 # file truncation, and file removal.
3893 test_42a() {
3894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3895
3896         setup_test42
3897         cancel_lru_locks $OSC
3898         stop_writeback
3899         sync; sleep 1; sync # just to be safe
3900         BEFOREWRITES=`count_ost_writes`
3901         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3902         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3903         AFTERWRITES=`count_ost_writes`
3904         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3905                 error "$BEFOREWRITES < $AFTERWRITES"
3906         start_writeback
3907 }
3908 run_test 42a "ensure that we don't flush on close"
3909
3910 test_42b() {
3911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3912
3913         setup_test42
3914         cancel_lru_locks $OSC
3915         stop_writeback
3916         sync
3917         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3918         BEFOREWRITES=$(count_ost_writes)
3919         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3920         AFTERWRITES=$(count_ost_writes)
3921         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3922                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3923         fi
3924         BEFOREWRITES=$(count_ost_writes)
3925         sync || error "sync: $?"
3926         AFTERWRITES=$(count_ost_writes)
3927         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3928                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3929         fi
3930         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3931         start_writeback
3932         return 0
3933 }
3934 run_test 42b "test destroy of file with cached dirty data ======"
3935
3936 # if these tests just want to test the effect of truncation,
3937 # they have to be very careful.  consider:
3938 # - the first open gets a {0,EOF}PR lock
3939 # - the first write conflicts and gets a {0, count-1}PW
3940 # - the rest of the writes are under {count,EOF}PW
3941 # - the open for truncate tries to match a {0,EOF}PR
3942 #   for the filesize and cancels the PWs.
3943 # any number of fixes (don't get {0,EOF} on open, match
3944 # composite locks, do smarter file size management) fix
3945 # this, but for now we want these tests to verify that
3946 # the cancellation with truncate intent works, so we
3947 # start the file with a full-file pw lock to match against
3948 # until the truncate.
3949 trunc_test() {
3950         test=$1
3951         file=$DIR/$test
3952         offset=$2
3953         cancel_lru_locks $OSC
3954      &