Whamcloud - gitweb
LU-17038 tests: remove mlink utility
[fs/lustre-release.git] / lustre / tests / sanityn.sh
1 #!/bin/bash
2
3 set -e
4
5 ONLY=${ONLY:-"$*"}
6
7 SIZE=${SIZE:-40960}
8 OPENFILE=${OPENFILE:-openfile}
9 OPENUNLINK=${OPENUNLINK:-openunlink}
10 export TMP=${TMP:-/tmp}
11 MOUNT_2=${MOUNT_2:-"yes"}
12 CHECK_GRANT=${CHECK_GRANT:-"yes"}
13 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
14
15 LUSTRE=${LUSTRE:-$(dirname $0)/..}
16 . $LUSTRE/tests/test-framework.sh
17 init_test_env $@
18 init_logging
19
20 ALWAYS_EXCEPT="$SANITYN_EXCEPT "
21 # bug number for skipped test:  LU-7105
22 ALWAYS_EXCEPT+="                28"
23 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
24
25 if [ $mds1_FSTYPE = "zfs" ]; then
26         # bug number:    LU-15757 (test_102() causes crash in umount later)
27         ALWAYS_EXCEPT+=" 102"
28         # LU-2829 / LU-2887 - make allowances for ZFS slowness
29         TEST33_NFILES=${TEST33_NFILES:-1000}
30 fi
31
32 #                                  23   (min)"
33 [ "$SLOW" = "no" ] && EXCEPT_SLOW="33a"
34
35 build_test_filter
36
37 FAIL_ON_ERROR=false
38
39 SETUP=${SETUP:-:}
40 TRACE=${TRACE:-""}
41
42 check_and_setup_lustre
43
44 OSC=${OSC:-"osc"}
45
46 assert_DIR
47 rm -rf $DIR1/[df][0-9]* $DIR1/lnk $DIR/[df].${TESTSUITE}*
48
49 SAMPLE_FILE=$TMP/$(basename $0 .sh).junk
50 dd if=/dev/urandom of=$SAMPLE_FILE bs=1M count=1
51
52 # $RUNAS_ID may get set incorrectly somewhere else
53 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
54
55 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
56
57 test_0() {
58         local client2=${CLIENT2:-$HOSTNAME}
59         local tmp=$(mktemp)
60
61         printf 'a b\n' > $tmp
62
63         do_node_vp "$HOSTNAME" printf 'a b\n' |
64         diff $tmp - || error "do_node_vp mismatch"
65
66         do_node_vp "$client2" printf 'a b\n' |
67         diff $tmp - || error "do_node_vp mismatch"
68
69         do_facet_vp mds1 printf 'a b\n' |
70         diff $tmp - || error "do_facet_vp mismatch"
71
72         printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' > $tmp
73
74         do_node_vp "$HOSTNAME" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
75         diff $tmp - || error "do_node_vp mismatch"
76
77         do_node_vp "$client2" printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
78         diff $tmp - || error "do_node_vp mismatch"
79
80         do_facet_vp mds1 printf '%s' 1 2 3 4 5 6 7 8 \ 9 10 ' ' '"' "'" \! \' \( \) $'\n' |
81         diff $tmp - || error "do_facet_vp mismatch"
82 }
83 run_test 0 "do_node_vp() and do_facet_vp() do the right thing"
84
85 test_1() {
86         touch $DIR1/$tfile
87         [ -f $DIR2/$tfile ] || error "Check create"
88         chmod 777 $DIR2/$tfile
89         $CHECKSTAT -t file -p 0777 $DIR1/$tfile ||
90                 error "Check attribute update for 0777"
91
92         chmod a-x $DIR2/$tfile
93         $CHECKSTAT -t file -p 0666 $DIR1/$tfile ||
94                 error "Check attribute update for 0666"
95
96         rm $DIR2/$tfile
97         $CHECKSTAT -a $DIR1/$tfile ||
98                 error "Check unlink - removes file on other mountpoint"
99 }
100 run_test 1 "Check attribute updates on 2 mount points"
101
102 test_2a() {
103         touch $DIR1/f2a
104         ls -l $DIR2/f2a
105         chmod 777 $DIR2/f2a
106         $CHECKSTAT -t file -p 0777 $DIR1/f2a ||
107                 error "Either not file type or perms not 0777"
108 }
109 run_test 2a "check cached attribute updates on 2 mtpt's ========"
110
111 test_2b() {
112         touch $DIR1/f2b
113         ls -l $DIR2/f2b
114         chmod 777 $DIR1/f2b
115         $CHECKSTAT -t file -p 0777 $DIR2/f2b ||
116                 error "Either not file type or perms not 0777"
117 }
118 run_test 2b "check cached attribute updates on 2 mtpt's ========"
119
120 # NEED TO SAVE ROOT DIR MODE
121 test_2c() {
122         chmod 777 $DIR1
123         $CHECKSTAT -t dir -p 0777 $DIR2 ||
124                 error "Either not dir type or perms not 0777"
125 }
126 run_test 2c "check cached attribute updates on 2 mtpt's root ==="
127
128 test_2d() {
129         chmod 755 $DIR1
130         $CHECKSTAT -t dir -p 0755 $DIR2 ||
131                 error "Either not file type or perms not 0775"
132 }
133 run_test 2d "check cached attribute updates on 2 mtpt's root ==="
134
135 test_2e() {
136         chmod 755 $DIR1
137         ls -l $DIR1
138         ls -l $DIR2
139         chmod 777 $DIR1
140                 $RUNAS dd if=/dev/zero of=$DIR2/$tfile count=1 ||
141                         error "dd failed"
142 }
143 run_test 2e "check chmod on root is propagated to others"
144
145 test_2f() {
146         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
147         local MDTIDX=1
148         local remote_dir=$tdir/remote_dir
149
150         mkdir -p $DIR1/$tdir
151         $LFS mkdir -i $MDTIDX $DIR1/$remote_dir ||
152                    error "Create remote directory failed"
153
154         touch $DIR1/$remote_dir/$tfile ||
155                 error "Create file under remote directory failed"
156         chmod 777 $DIR1/$remote_dir/$tfile ||
157                 error "Chmod file under remote directory failed"
158
159         $CHECKSTAT -t file -p 0777 $DIR2/$remote_dir/$tfile ||
160                 error "Check attr of file under remote directory failed"
161
162         chown $RUNAS_ID:$RUNAS_GID $DIR1/$remote_dir/$tfile ||
163                 error "Chown file under remote directory failed"
164
165         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR2/$remote_dir/$tfile ||
166                 error "Check owner of file under remote directory failed"
167
168         cd $DIR2/$remote_dir || error "enter remote dir"
169         rm -rf $DIR1/$remote_dir/$tfile ||
170                 error "Unlink remote directory failed"
171
172         $CHECKSTAT -t file $DIR2/$remote_dir/$tfile &&
173                 error "unlink file still exists!"
174
175         cd $DIR2/$tdir || error "exit remote dir"
176         rm -rf $DIR1/$tdir || error "unlink directory failed"
177 }
178 run_test 2f "check attr/owner updates on DNE with 2 mtpt's"
179
180 test_2g() {
181         dd if=/dev/zero of=$DIR1/$tfile oflag=sync bs=1M count=2
182
183         local block1=$(stat $DIR1/$tfile | awk '/Blocks/ {print $4} ')
184         cancel_lru_locks osc
185         local block2=$(stat $DIR2/$tfile | awk '/Blocks/ {print $4} ')
186         echo "$DIR1/$tfile has $block1 blocks"
187         echo "$DIR2/$tfile has $block2 blocks"
188         [ $block1 -eq $block2 ] || error "$block1 not equal to $block2"
189 }
190 run_test 2g "check blocks update on sync write"
191
192 test_3() {
193         local target="this/is/good"
194         ln -s $target $DIR1/$tfile || error "ln -s $target $DIR1/$tfile failed"
195         [ "$(ls -l $DIR2/$tfile | sed -e 's/.* -> //')" = "$target" ] ||
196                 error "link $DIR2/$tfile not as expected"
197 }
198 run_test 3 "symlink on one mtpt, readlink on another ==========="
199
200 test_4() {
201         multifstat $DIR1/f4 $DIR2/f4
202 }
203 run_test 4 "fstat validation on multiple mount points =========="
204
205 test_5() {
206         mcreate $DIR1/f5
207         $TRUNCATE $DIR2/f5 100
208         $CHECKSTAT -t file -s 100 $DIR1/f5 ||
209                 error "Either not file type or size not equal to 100 bytes"
210         rm $DIR1/f5
211 }
212 run_test 5 "create a file on one mount, truncate it on the other"
213
214 test_6() {
215         openunlink $DIR1/$tfile $DIR2/$tfile || \
216                 error "openunlink $DIR1/$tfile $DIR2/$tfile"
217 }
218 run_test 6 "remove of open file on other node =================="
219
220 test_7() {
221         local dir=d7
222         opendirunlink $DIR1/$dir $DIR2/$dir || \
223                 error "opendirunlink $DIR1/$dir $DIR2/$dir"
224 }
225 run_test 7 "remove of open directory on other node ============="
226
227 test_8() {
228         opendevunlink $DIR1/$tfile $DIR2/$tfile || \
229                 error "opendevunlink $DIR1/$tfile $DIR2/$tfile"
230 }
231 run_test 8 "remove of open special file on other node =========="
232
233 test_9a() {
234         MTPT=1
235         local dir
236         > $DIR2/f9
237         for C in a b c d e f g h i j k l; do
238                 dir=`eval echo \\$DIR$MTPT`
239                 echo -n $C >> $dir/f9
240                 [ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
241         done
242         [ "`cat $DIR1/f9`" = "abcdefghijkl" ] || \
243                 error "`od -a $DIR1/f9` != abcdefghijkl"
244 }
245 run_test 9a "append of file with sub-page size on multiple mounts"
246
247 #LU-10681 - tiny writes & appending to sparse striped file
248 test_9b() {
249         [[ $OSTCOUNT -ge 2 ]] || { skip "needs >= 2 OSTs"; return; }
250
251         $LFS setstripe -c 2 -S 1M $DIR/$tfile
252         echo "foo" >> $DIR/$tfile
253         dd if=/dev/zero of=$DIR2/$tfile bs=1M count=1 seek=1 conv=notrunc ||
254                 error "sparse dd $DIR2/$tfile failed"
255         echo "foo" >> $DIR/$tfile
256
257         data=$(dd if=$DIR2/$tfile bs=1 count=3 skip=$((2 * 1048576)) conv=notrunc)
258         echo "Data read (expecting 'foo')": $data
259         [ "$data" = "foo" ] || error "append to sparse striped file failed"
260 }
261 run_test 9b "append to striped sparse file"
262
263 test_10a() {
264         MTPT=1
265         local dir
266         OFFSET=0
267         > $DIR2/f10
268         for C in a b c d e f g h i j k l; do
269                 dir=`eval echo \\$DIR$MTPT`
270                 echo -n $C | dd of=$dir/f10 bs=1 seek=$OFFSET count=1
271                 [ "$MTPT" -eq 1 ] && MTPT=2 || MTPT=1
272                 OFFSET=`expr $OFFSET + 1`
273         done
274         [ "`cat $DIR1/f10`" = "abcdefghijkl" ] || \
275                 error "`od -a $DIR1/f10` != abcdefghijkl"
276 }
277 run_test 10a "write of file with sub-page size on multiple mounts "
278
279 test_10b() {
280         # create a seed file
281         yes "R" | head -c 4000 >$TMP/f10b-seed
282         dd if=$TMP/f10b-seed of=$DIR1/f10b bs=3k count=1 || error "dd $DIR1"
283
284         $TRUNCATE $DIR1/f10b 4096 || error "truncate 4096"
285
286         dd if=$DIR2/f10b of=$TMP/f10b-lustre bs=4k count=1 || error "dd $DIR2"
287
288         # create a test file locally to compare
289         dd if=$TMP/f10b-seed of=$TMP/f10b bs=3k count=1 || error "dd random"
290         $TRUNCATE $TMP/f10b 4096 || error "truncate 4096"
291         cmp $TMP/f10b $TMP/f10b-lustre || error "file miscompare"
292         rm $TMP/f10b $TMP/f10b-lustre $TMP/f10b-seed
293 }
294 run_test 10b "write of file with sub-page size on multiple mounts "
295
296 test_11() {
297         test_mkdir $DIR1/d11
298         multiop_bg_pause $DIR1/d11/f O_c || return 1
299         MULTIPID=$!
300         cp -p /bin/ls $DIR1/d11/f
301         $DIR2/d11/f
302         RC=$?
303         kill -USR1 $MULTIPID
304         wait $MULTIPID || error "wait for PID $MULTIPID failed"
305         [ $RC -eq 0 ] && error || true
306 }
307 run_test 11 "execution of file opened for write should return error ===="
308
309 test_12() {
310         DIR=$DIR DIR2=$DIR2 bash lockorder.sh
311 }
312 run_test 12 "test lock ordering (link, stat, unlink)"
313
314 test_13() {     # bug 2451 - directory coherency
315         test_mkdir $DIR1/d13
316         cd $DIR1/d13 || error "cd to $DIR1/d13 failed"
317         ls
318         ( touch $DIR1/d13/f13 ) # needs to be a separate shell
319         ls
320         rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
321         ls 2>&1 | grep f13 && error "f13 shouldn't return an error (1)" || true
322         # need to run it twice
323         ( touch $DIR1/d13/f13 ) # needs to be a separate shell
324         ls
325         rm -f $DIR2/d13/f13 || error "Cannot remove $DIR2/d13/f13"
326         ls 2>&1 | grep f13 && error "f13 shouldn't return an error (2)" || true
327 }
328 run_test 13 "test directory page revocation"
329
330 test_14aa() {
331         test_mkdir $DIR1/$tdir
332         cp -p /bin/ls $DIR1/$tdir/$tfile
333         multiop_bg_pause $DIR1/$tdir/$tfile Ow_c || return 1
334         MULTIPID=$!
335
336         $DIR2/$tdir/$tfile && error || true
337         kill $MULTIPID
338 }
339 run_test 14aa "execution of file open for write returns -ETXTBSY"
340
341 test_14ab() {
342         test_mkdir $DIR1/$tdir
343         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
344         $DIR1/$tdir/sleep 60 &
345         SLEEP_PID=$!
346         $MULTIOP $DIR2/$tdir/sleep Oc && error "expected error, got success"
347         kill $SLEEP_PID
348 }
349 run_test 14ab "open(RDWR) of executing file returns -ETXTBSY"
350
351 test_14b() { # bug 3192, 7040
352         test_mkdir $DIR1/$tdir
353         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
354         $DIR1/$tdir/sleep 60 &
355         SLEEP_PID=$!
356         $TRUNCATE $DIR2/$tdir/sleep 60 && kill -9 $SLEEP_PID && \
357                 error "expected truncate error, got success"
358         kill $SLEEP_PID
359         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
360 }
361 run_test 14b "truncate of executing file returns -ETXTBSY ======"
362
363 test_14c() { # bug 3430, 7040
364         test_mkdir $DIR1/$tdir
365         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
366         $DIR1/$tdir/sleep 60 &
367         SLEEP_PID=$!
368         cp /etc/hosts $DIR2/$tdir/sleep && error "expected error, got success"
369         kill $SLEEP_PID
370         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
371 }
372 run_test 14c "open(O_TRUNC) of executing file return -ETXTBSY =="
373
374 test_14d() { # bug 10921
375         test_mkdir $DIR1/$tdir
376         cp -p $(which sleep) $DIR1/$tdir/sleep || error "cp failed"
377         $DIR1/$tdir/sleep 60 &
378         SLEEP_PID=$!
379         log chmod
380         chmod 600 $DIR1/$tdir/sleep || error "chmod failed"
381         kill $SLEEP_PID
382         cmp $(which sleep) $DIR1/$tdir/sleep || error "binary changed"
383 }
384 run_test 14d "chmod of executing file is still possible ========"
385
386 test_15() {     # bug 974 - ENOSPC
387         echo "PATH=$PATH"
388         bash oos2.sh $MOUNT1 $MOUNT2
389         wait_delete_completed
390         grant_error=$(dmesg | grep "< tot_grant")
391         [ -z "$grant_error" ] || error "$grant_error"
392 }
393 run_test 15 "test out-of-space with multiple writers ==========="
394
395 COUNT=${COUNT:-2500}
396 # The FSXNUM reduction for ZFS is needed until ORI-487 is fixed.
397 # We don't want to skip it entirely, but ZFS is VERY slow and cannot
398 # pass a 2500 operation dual-mount run within the time limit.
399 if [ "$ost1_FSTYPE" = "zfs" ]; then
400         FSXNUM=$((COUNT / 5))
401         FSXP=1
402 elif [ "$SLOW" = "yes" ]; then
403         FSXNUM=$((COUNT * 5))
404         FSXP=500
405 else
406         FSXNUM=$COUNT
407         FSXP=100
408 fi
409
410 test_16a() {
411         local file1=$DIR1/$tfile
412         local file2=$DIR2/$tfile
413         local stripe_size=$(do_facet $SINGLEMDS \
414                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
415
416         check_set_fallocate
417
418         # to allocate grant because it may run out due to test_15.
419         $LFS setstripe -c -1 $file1
420         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
421         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
422         rm -f $file1
423
424         $LFS setstripe -c -1 $file1 # b=10919
425         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
426                 error "fsx failed"
427         rm -f $file1
428
429         # O_DIRECT reads and writes must be aligned to the PAGE_SIZE.
430         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 -Z -r $PAGE_SIZE \
431                 -w $PAGE_SIZE $file1 $file2 || error "fsx with O_DIRECT failed."
432 }
433 run_test 16a "$FSXNUM iterations of dual-mount fsx"
434
435 # Consistency check for tiny writes, LU-9409
436 test_16b() {
437         local file1=$DIR1/$tfile
438         local file2=$DIR2/$tfile
439         local stripe_size=($($LFS getstripe -S $DIR))
440
441         check_set_fallocate
442
443         # to allocate grant because it may run out due to test_15.
444         lfs setstripe -c -1 $file1
445         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
446                 error "dd failed writing to file=$file1"
447         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync ||
448                 error "dd failed writing to file=$file2"
449         rm -f $file1
450
451         lfs setstripe -c -1 $file1 # b=10919
452         # -o is set to 8192 because writes < 1 page and between 1 and 2 pages
453         # create a mix of tiny writes & normal writes
454         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -o 8192 -S 0 \
455                 $file1 $file2 || error "fsx with tiny write failed."
456 }
457 run_test 16b "$FSXNUM iterations of dual-mount fsx at small size"
458
459 test_16c() {
460         local file1=$DIR1/$tfile
461         local file2=$DIR2/$tfile
462         local stripe_size=$(do_facet $SINGLEMDS \
463                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
464
465         [ "$ost1_FSTYPE" != ldiskfs ] && skip "dio on ldiskfs only"
466
467         check_set_fallocate
468
469         # to allocate grant because it may run out due to test_15.
470         $LFS setstripe -c -1 $file1
471         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
472         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
473         rm -f $file1
474         wait_delete_completed
475
476         local list=$(comma_list $(osts_nodes))
477         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
478                 skip "not cache-capable obdfilter"
479         fi
480
481         set_osd_param $list '' read_cache_enable 0
482         set_osd_param $list '' writethrough_cache_enable 0
483
484         $LFS setstripe -c -1 $file1 # b=10919
485         $FSX -c 50 -p $FSXP -N $FSXNUM -l $((SIZE * 256)) -S 0 $file1 $file2 ||
486                 error "fsx failed"
487         rm -f $file1
488
489         set_osd_param $list '' read_cache_enable 1
490         set_osd_param $list '' writethrough_cache_enable 1
491
492         return 0
493 }
494 run_test 16c "verify data consistency on ldiskfs with cache disabled (b=17397)"
495
496 test_16d() {
497         local file1=$DIR1/$tfile
498         local file2=$DIR2/$tfile
499         local file3=$DIR1/file
500         local tmpfile=$(mktemp)
501         local stripe_size=$(do_facet $SINGLEMDS \
502                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
503
504         # to allocate grant because it may run out due to test_15.
505         $LFS setstripe -c -1 $file1
506         stack_trap "rm -f $file1 $file2 $file3 $tmpfile"
507         dd if=/dev/zero of=$file1 bs=$stripe_size count=$OSTCOUNT oflag=sync
508         dd if=/dev/zero of=$file2 bs=$stripe_size count=$OSTCOUNT oflag=sync
509         rm -f $file1
510
511         $LFS setstripe -c -1 $file1 # b=10919
512         $LCTL set_param ldlm.namespaces.*.lru_size=clear
513
514         # direct write on one client and direct read from another
515         dd if=/dev/urandom of=$file1 bs=1M count=100 oflag=direct
516         dd if=$file2 of=$tmpfile iflag=direct bs=1M
517         diff $file1 $tmpfile || error "file different(1)"
518         rm -f $file1
519
520         # buffer write on one client, but direct read from another
521         dd if=$tmpfile of=$file1 bs=1M count=100
522         dd if=$file2 of=$file3 bs=1M iflag=direct count=100
523         diff $file3 $tmpfile || error "file different(2)"
524
525         rm -f $file3 $file2 $file1
526         # direct write on one client
527         dd if=$tmpfile of=$file1 bs=1M count=100 oflag=direct
528         # buffer read from another client
529         dd if=$file2 of=$file3 bs=1M count=100
530         diff $file3 $tmpfile || error "file different(3)"
531 }
532 run_test 16d "Verify DIO and buffer IO with two clients"
533
534 test_16e() { # LU-13227
535         # issue:        LU-14314
536
537         (( "$MDS1_VERSION" >= $(version_code 2.13.53) )) ||
538                 skip "Need MDS version at least 2.13.53"
539
540         local file1=$DIR1/$tfile
541         local file2=$DIR2/$tfile
542
543         # client1 write 10M data
544         dd if=/dev/zero of=$file1 bs=1M count=10
545         # drop locks
546         cancel_lru_locks osc > /dev/null
547         # use lockahead to generate one PW lock to keep LVB loaded.
548         $LFS ladvise -a lockahead --start 0 --length 1M \
549                 --mode WRITE $file1
550         # direct write to extend file size on client2
551         dd if=/dev/zero of=$file2 bs=1M seek=20 count=1 \
552                 oflag=direct conv=notrunc
553         local filesize=$(stat -c %s $file2)
554         [ "$filesize" -eq 22020096 ] ||
555                 error "expected filesize 22020096 got $filesize"
556         rm -f $file1
557 }
558 run_test 16e "Verify size consistency for O_DIRECT write"
559
560 test_16f() { # LU-14541
561         local file1=$DIR1/$tfile
562         local file2=$DIR2/$tfile
563         local duration=20
564         local status
565
566         (( $MDS1_VERSION > $(version_code 2.15.51) )) ||
567                 skip "Need MDS version at least 2.15.51"
568
569         timeout --preserve-status --signal=USR1 $duration \
570                 rw_seq_cst_vs_drop_caches $file1 $file2
571         status=$?
572
573         case $((status & 0x7f)) in
574                 0)
575                         echo OK # Computers must be fast now.
576                         ;;
577                 6) # SIGABRT
578                         error "sequential consistency violation detected"
579                         ;;
580                 10) # SIGUSR1
581                         echo TIMEOUT # This is fine.
582                         ;;
583                 *)
584                         error "strange status '$status'"
585                         ;;
586         esac
587
588         rm -f $file1
589 }
590 run_test 16f "rw sequential consistency vs drop_caches"
591
592 test_16g() {
593         local file1=$DIR1/$tfile
594         local file2=$DIR2/$tfile
595         local duration=20
596         local status
597
598         timeout --preserve-status --signal=USR1 $duration \
599                 rw_seq_cst_vs_drop_caches -m $file1 $file2
600         status=$?
601
602         case $((status & 0x7f)) in
603                 0)
604                         echo OK # Computers must be fast now.
605                         ;;
606                 6) # SIGABRT
607                         error "sequential consistency violation detected"
608                         ;;
609                 10) # SIGUSR1
610                         echo TIMEOUT # This is fine.
611                         ;;
612                 *)
613                         error "strange status '$status'"
614                         ;;
615         esac
616
617         rm -f $file1
618 }
619 run_test 16g "mmap rw sequential consistency vs drop_caches"
620
621 test_16h() {
622         local tf=$DIR/$tdir/$tfile
623         local tf2=$DIR2/$tdir/$tfile
624         local cmd="$MMAP_CAT $tf | od -x | tail -q -n4"
625         local cmd2="$MMAP_CAT $tf2 | od -x | tail -q -n4"
626
627         test_mkdir $DIR/$tdir
628
629         # create file and populate data
630         cp /etc/passwd $tf || error "cp failed"
631
632         local size=$(stat -c %s $tf)
633         c1=$(eval $cmd)
634         c2=$(eval $cmd2)
635         if [[ "$c1" != "$c2" ]]; then
636                 echo "  ------- mount 1 read --------"
637                 echo $c1
638                 echo "  ------- mount 2 read --------"
639                 echo $c2
640                 error "content mismatch"
641         fi
642
643         echo "  ------- before truncate --------"
644         echo $c1
645
646         # truncate file
647         $TRUNCATE $tf $((size / 2)) || error "truncate file"
648
649         #cancel_lru_locks
650         echo "  ------- after truncate --------"
651
652         # repeat the comparison
653         c1=$(eval $cmd)
654         c2=$(eval $cmd2)
655         if [[ "$c1" != "$c2" ]]; then
656                 echo "  ------- mount 1 read --------"
657                 echo $c1
658                 echo "  ------- mount 2 read --------"
659                 echo $c2
660                 error "content mismatch after truncate"
661         fi
662         echo $c2
663 }
664 run_test 16h "mmap read after truncate file"
665
666 test_16i() {
667         local tf=$DIR/$tdir/$tfile
668         local tf2=$DIR2/$tdir/$tfile
669
670         test_mkdir $DIR/$tdir
671
672         # create file and populate data
673         cp /etc/passwd $tf || error "cp failed"
674
675         local size=$(stat -c %s $tf)
676
677         c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
678         c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)
679
680         if [[ "$c1" != "$c2" ]]; then
681                 echo "  ------- mount 1 read --------"
682                 echo $c1
683                 echo "  ------- mount 2 read --------"
684                 echo $c2
685                 error "content mismatch"
686         fi
687
688         echo "  ------- before truncate --------"
689         echo $c1
690
691         # truncate file
692         $TRUNCATE $tf $((size / 2)) || error "truncate file"
693
694         echo "  ------- after truncate --------"
695
696         # repeat the comparison
697         c1=$(dd if=$tf bs=1 2>/dev/null | od -x | tail -q -n4)
698         c2=$(dd if=$tf2 bs=1 2>/dev/null | od -x | tail -q -n4)
699
700         if [[ "$c1" != "$c2" ]]; then
701                 echo "  ------- mount 1 read --------"
702                 echo $c1
703                 echo "  ------- mount 2 read --------"
704                 echo $c2
705                 error "content mismatch after truncate"
706         fi
707         echo $c2
708 }
709 run_test 16i "read after truncate file"
710
711 test_16j()
712 {
713         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
714
715         local stripe_size=$((1024 * 1024)) #1 MiB
716         # Max i/o below is ~ 4 * stripe_size, so this gives ~5 i/os
717         local file_size=$((25 * stripe_size))
718         local bsizes
719
720         $LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.1
721         stack_trap "rm -f $DIR/$tfile.1"
722
723         # Just a bit bigger than the largest size in the test set below
724         dd if=/dev/urandom bs=$file_size count=1 of=$DIR/$tfile.1 ||
725                 error "buffered i/o to create file failed"
726
727         if zfs_or_rotational; then
728                 # DIO on ZFS can take up to 2 seconds per IO
729                 # rotational is better, but still slow.
730                 # Limit testing on those media to larger sizes
731                 bsizes="$((stripe_size - PAGE_SIZE)) $stripe_size"
732         else
733                 bsizes="$PAGE_SIZE $((PAGE_SIZE * 4)) $stripe_size \
734                         $((stripe_size * 4))"
735         fi
736
737         # 1 process (BIO or DIO) on each client
738         for bs in $bsizes; do
739                 $LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.2
740                 echo "bs: $bs, file_size $file_size"
741                 dd if=$DIR/$tfile.1 bs=$((bs *2 )) of=$DIR/tfile.2 \
742                         conv=notrunc oflag=direct iflag=direct &
743                 pid_dio1=$!
744                 # Buffered I/O with similar but not the same block size
745                 dd if=$DIR2/$tfile.1 bs=$((bs * 2)) of=$DIR2/$tfile.2 \
746                         conv=notrunc &
747                 pid_bio2=$!
748                 wait $pid_dio1
749                 rc1=$?
750                 wait $pid_bio2
751                 rc2=$?
752                 if (( rc1 != 0 )); then
753                         error "dio copy 1 w/bsize $bs failed: $rc1"
754                 fi
755                 if (( rc2 != 0 )); then
756                         error "buffered copy 2 w/bsize $bs failed: $rc2"
757                 fi
758
759                 $CHECKSTAT -t file -s $file_size $DIR/$tfile.2 ||
760                         error "size incorrect"
761                 $CHECKSTAT -t file -s $file_size $DIR2/$tfile.2 ||
762                         error "size incorrect - mount 2"
763                 cmp --verbose $DIR/$tfile.1 $DIR/$tfile.2 ||
764                         error "files differ, bsize $bs"
765                 cmp --verbose $DIR2/$tfile.1 $DIR2/$tfile.2 ||
766                         error "files differ, bsize $bs - mount 2"
767                 rm -f $DIR/$tfile.2
768         done
769
770         # 2 processes - both DIO and BIO - on each client
771         for bs in $bsizes; do
772                 $LFS setstripe -c 2 -S $stripe_size $DIR/$tfile.2
773                 echo "bs: $bs, file_size $file_size"
774                 # Client 1
775                 dd if=$DIR/$tfile.1 bs=$((bs *2 )) of=$DIR/tfile.2 \
776                         conv=notrunc oflag=direct iflag=direct &
777                 pid_dio1=$!
778                 # Buffered I/O with similar but not the same block size
779                 dd if=$DIR/$tfile.1 bs=$((bs * 2)) of=$DIR/$tfile.2 \
780                         conv=notrunc &
781                 pid_bio2=$!
782                 # Client 2
783                 dd if=$DIR2/$tfile.1 bs=$((bs *2 )) of=$DIR2/tfile.2 \
784                         conv=notrunc oflag=direct iflag=direct &
785                 pid_dio3=$!
786                 # Buffered I/O with similar but not the same block size
787                 dd if=$DIR2/$tfile.1 bs=$((bs * 2)) of=$DIR2/$tfile.2 \
788                         conv=notrunc &
789                 pid_bio4=$!
790                 wait $pid_dio1
791                 rc1=$?
792                 wait $pid_bio2
793                 rc2=$?
794                 wait $pid_dio3
795                 rc3=$?
796                 wait $pid_bio4
797                 rc4=$?
798
799                 if (( rc1 != 0 )); then
800                         error "dio copy 1 w/bsize $bs failed: $rc1"
801                 fi
802                 if (( rc2 != 0 )); then
803                         error "buffered copy 2 w/bsize $bs failed: $rc2"
804                 fi
805                 if (( rc3 != 0 )); then
806                         error "dio copy 3 w/bsize $bs failed: $rc1"
807                 fi
808                 if (( rc4 != 0 )); then
809                         error "buffered copy 4 w/bsize $bs failed: $rc2"
810                 fi
811
812                 $CHECKSTAT -t file -s $file_size $DIR/$tfile.2 ||
813                         error "size incorrect"
814                 $CHECKSTAT -t file -s $file_size $DIR2/$tfile.2 ||
815                         error "size incorrect - mount 2"
816                 cmp --verbose $DIR/$tfile.1 $DIR/$tfile.2 ||
817                         error "files differ, bsize $bs"
818                 cmp --verbose $DIR2/$tfile.1 $DIR2/$tfile.2 ||
819                         error "files differ, bsize $bs - mount 2"
820                 rm -f $DIR/$tfile.2
821         done
822 }
823 run_test 16j "race dio with buffered i/o"
824
825 test_17() { # bug 3513, 3667
826         remote_ost_nodsh && skip "remote OST with nodsh" && return
827
828         lfs setstripe $DIR1/$tfile -i 0 -c 1
829         cp $SAMPLE_FILE $DIR1/$tfile
830         cancel_lru_locks osc > /dev/null
831         #define CFS_FAIL_ONCE|OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
832         do_facet ost1 lctl set_param fail_loc=0x8000030a
833         ls -ls $DIR1/$tfile | awk '{ print $1,$6 }' > $DIR1/$tfile-1 & \
834         ls -ls $DIR2/$tfile | awk '{ print $1,$6 }' > $DIR2/$tfile-2
835         wait
836         diff -u $DIR1/$tfile-1 $DIR2/$tfile-2 || error "files are different"
837 }
838 run_test 17 "resource creation/LVB creation race ==============="
839
840 test_18() {
841         # turn e.g. ALWAYS_EXCEPT="18c" into "-e 3"
842         local idx
843         local excepts=
844         for idx in {a..z}; do
845                 local ptr=EXCEPT_ALWAYS_18$idx
846                 [ x${!ptr} = xtrue ] || continue
847
848                 excepts="$excepts -e $(($(printf %d \'$idx)-96))"
849         done
850
851         excepts="$excepts -e 7 -e 8 -e 9"
852         $LUSTRE/tests/mmap_sanity -d $MOUNT1 -m $MOUNT2 $excepts ||
853                 error "mmap_sanity test failed"
854         sync; sleep 1; sync
855 }
856 run_test 18 "mmap sanity check ================================="
857
858 test_19() { # bug3811
859         local node=$(facet_active_host ost1)
860         local device="$FSNAME-OST*"
861
862         [ "x$DOM" = "xyes" ] && node=$(facet_active_host $SINGLEMDS) &&
863                 device="$FSNAME-MDT*"
864
865         # check whether obdfilter is cache capable at all
866         get_osd_param $node $device read_cache_enable >/dev/null ||
867                 skip "not cache-capable obdfilter"
868
869         local max=$(get_osd_param $node $device readcache_max_filesize |\
870                 head -n 1)
871         set_osd_param $node $device readcache_max_filesize 4096
872         dd if=/dev/urandom of=$TMP/$tfile bs=512k count=32
873         local SUM=$(cksum $TMP/$tfile | cut -d" " -f 1,2)
874         cp $TMP/$tfile $DIR1/$tfile
875         for i in `seq 1 20`; do
876                 [ $((i % 5)) -eq 0 ] && log "$testname loop $i"
877                 cancel_lru_locks $OSC > /dev/null
878                 cksum $DIR1/$tfile | cut -d" " -f 1,2 > $TMP/sum1 & \
879                 cksum $DIR2/$tfile | cut -d" " -f 1,2 > $TMP/sum2
880                 wait
881                 [ "$(cat $TMP/sum1)" = "$SUM" ] || \
882                         error "$DIR1/$tfile $(cat $TMP/sum1) != $SUM"
883                 [ "$(cat $TMP/sum2)" = "$SUM" ] || \
884                         error "$DIR2/$tfile $(cat $TMP/sum2) != $SUM"
885         done
886         set_osd_param $node $device readcache_max_filesize $max
887         rm $DIR1/$tfile
888 }
889 run_test 19 "test concurrent uncached read races ==============="
890
891 test_20() {
892         test_mkdir $DIR1/$tdir
893         cancel_lru_locks
894         CNT=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
895         $MULTIOP $DIR1/$tdir/$tfile Ow8190c
896         $MULTIOP $DIR2/$tdir/$tfile Oz8194w8190c
897         $MULTIOP $DIR1/$tdir/$tfile Oz0r8190c
898         cancel_lru_locks
899         CNT2=$($LCTL get_param -n llite.*.dump_page_cache | wc -l)
900         [[ $CNT2 == $CNT ]] ||
901                 error $((CNT2 - CNT))" page left in cache after lock cancel"
902 }
903 run_test 20 "test extra readahead page left in cache ===="
904
905 cleanup_21() {
906         trap 0
907         umount $DIR1/$tdir
908 }
909
910 test_21() { # Bug 5907
911         test_mkdir $DIR1/$tdir
912         mount /etc $DIR1/$tdir --bind || error "mount failed" # Poor man's mount.
913         trap cleanup_21 EXIT
914         rmdir -v $DIR1/$tdir && error "Removed mounted directory"
915         rmdir -v $DIR2/$tdir && echo "Removed mounted directory from another mountpoint, needs to be fixed"
916         test -d $DIR1/$tdir || error "Mounted directory disappeared"
917         cleanup_21
918         test -d $DIR2/$tdir || test -d $DIR1/$tdir && error "Removed dir still visible after umount"
919         true
920 }
921 run_test 21 " Try to remove mountpoint on another dir ===="
922
923 test_23() { # Bug 5972
924         local at_diff=$(do_facet $SINGLEMDS \
925                 $LCTL get_param -n mdd.*MDT0000*.atime_diff | head -n1)
926         echo "atime should be updated while another read" > $DIR1/$tfile
927
928         # clear the lock(mode: LCK_PW) gotten from creating operation
929         cancel_lru_locks $OSC
930         time1=$(date +%s)
931         echo "now is $time1"
932         sleep $((at_diff + 1))
933
934         echo "starting reads"
935         multiop_bg_pause $DIR1/$tfile or20_c || return 1
936         # with SOM and opencache enabled, we need to close a file and cancel
937         # open lock to get atime propogated to MDS
938         kill -USR1 $! || return 2
939         cancel_lru_locks mdc
940
941         time2=$(stat -c "%X" $DIR/$tfile)
942         echo "new atime is $time2"
943
944         [ $time2 -gt $time1 ] || error "atime was not updated"
945         rm -f $DIR1/$tfile || error "rm -f $DIR1/$tfile failed"
946         true
947 }
948 run_test 23 " others should see updated atime while another read===="
949
950 test_24a() {
951         touch $DIR1/$tfile
952         lfs df || error "lfs df failed"
953         lfs df -ih || error "lfs df -ih failed"
954         lfs df -h $DIR1 || error "lfs df -h $DIR1 failed"
955         lfs df -i $DIR2 || error "lfs df -i $DIR2 failed"
956         lfs df $DIR1/$tfile || error "lfs df $DIR1/$tfile failed"
957         lfs df -ih $DIR2/$tfile || error "lfs df -ih $DIR2/$tfile failed"
958
959         OSC=`lctl dl | awk '/-osc-|OSC.*MNT/ {print $4}' | head -n 1`
960 #       OSC=`lctl dl | awk '/-osc-/ {print $4}' | head -n 1`
961         lctl --device %osc deactivate
962         lfs df -i || error "lfs df -i with deactivated OSC failed"
963         lctl --device %osc activate
964         lfs df || error "lfs df with reactivated OSC failed"
965 }
966 run_test 24a "lfs df [-ih] [path] test ========================="
967
968 test_24b() {
969         touch $DIR1/$tfile
970         fsnum=$(lfs_df | grep -c "summary")
971         [ $fsnum -eq 2 ] || error "lfs df shows $fsnum != 2 filesystems."
972 }
973 run_test 24b "lfs df should show both filesystems ==============="
974
975 test_25a() {
976         local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
977                                                                 grep -c acl)
978         [ "$acl" -lt 1 ] && skip "must have acl, skipping" && return
979
980         mkdir -p $DIR1/$tdir
981         touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
982         chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"
983
984         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
985         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
986                 error "setfacl $DIR2/$tdir #1"
987         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
988         setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
989                 error "setfacl $DIR2/$tdir #2"
990         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
991         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
992                 error "setfacl $DIR2/$tdir #3"
993         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
994         setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
995                 error "setfacl $DIR2/$tdir #4"
996         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"
997
998         rm -rf $DIR1/$tdir
999 }
1000 run_test 25a "change ACL on one mountpoint be seen on another ==="
1001
1002 test_25b() {
1003         local acl=$(lctl get_param -n mdc.*MDT0000-mdc-*.connect_flags |
1004                                                         grep -c acl)
1005         [ "$acl" -lt 1 ] && skip "must have acl, skipping" && return
1006
1007         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1008
1009         rm -rf $DIR1/$tdir
1010         $LFS mkdir -i 1 $DIR1/$tdir
1011         touch $DIR1/$tdir/f1 || error "touch $DIR1/$tdir/f1"
1012         chmod 0755 $DIR1/$tdir/f1 || error "chmod 0755 $DIR1/$tdir/f1"
1013
1014         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #1"
1015         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
1016                 error "setfacl $DIR2/$tdir #1"
1017         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #2"
1018         setfacl -m u:$RUNAS_ID:r-x -m g:$RUNAS_GID:r-x $DIR1/$tdir ||
1019                 error "setfacl $DIR2/$tdir #2"
1020         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #3"
1021         setfacl -m u:$RUNAS_ID:--- -m g:$RUNAS_GID:--- $DIR1/$tdir ||
1022                 error "setfacl $DIR2/$tdir #3"
1023         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 && error "checkstat $DIR2/$tdir/f1 #4"
1024         setfacl -x u:$RUNAS_ID: -x g:$RUNAS_GID: $DIR1/$tdir ||
1025                 error "setfacl $DIR2/$tdir #4"
1026         $RUNAS $CHECKSTAT $DIR2/$tdir/f1 || error "checkstat $DIR2/$tdir/f1 #5"
1027
1028         rm -rf $DIR1/$tdir
1029 }
1030 run_test 25b "change ACL under remote dir on one mountpoint be seen on another"
1031
1032 test_26a() {
1033         utime $DIR1/f26a -s $DIR2/f26a || error "utime failed for $DIR1/f26a"
1034 }
1035 run_test 26a "allow mtime to get older"
1036
1037 test_26b() {
1038         touch $DIR1/$tfile
1039         sleep 1
1040         echo "aaa" >> $DIR1/$tfile
1041         sleep 1
1042         chmod a+x $DIR2/$tfile
1043         mt1=`stat -c %Y $DIR1/$tfile`
1044         mt2=`stat -c %Y $DIR2/$tfile`
1045
1046         if [ x"$mt1" != x"$mt2" ]; then
1047                 error "not equal mtime, client1: "$mt1", client2: "$mt2"."
1048         fi
1049 }
1050 run_test 26b "sync mtime between ost and mds"
1051
1052 test_27() {
1053         cancel_lru_locks $OSC
1054         lctl clear
1055         dd if=/dev/zero of=$DIR2/$tfile bs=$((4096+4))k conv=notrunc count=4 seek=3 &
1056         DD2_PID=$!
1057         sleep 0.5
1058         log "dd 1 started"
1059
1060         dd if=/dev/zero of=$DIR1/$tfile bs=$((16384-1024))k conv=notrunc count=1 seek=4 &
1061         DD1_PID=$!
1062         log "dd 2 started"
1063
1064         sleep 1
1065         dd if=/dev/zero of=$DIR1/$tfile bs=8k conv=notrunc count=1 seek=0
1066         log "dd 3 finished"
1067         lctl set_param -n ldlm.dump_namespaces ""
1068         wait $DD1_PID $DD2_PID
1069         [ $? -ne 0 ] && lctl dk $TMP/debug || true
1070 }
1071 run_test 27 "align non-overlapping extent locks from request ==="
1072
1073 test_28() { # bug 9977
1074         skip "echo_client on osc is no longer supported"
1075 }
1076 run_test 28 "read/write/truncate file with lost stripes"
1077
1078 test_30() { #b=11110, LU-2523
1079         test_mkdir $DIR1/$tdir
1080         cp -f /bin/bash $DIR1/$tdir/bash
1081         bash -c 'sleep 1; rm -f $DIR2/$tdir/bash; cp /bin/bash $DIR2/$tdir' &
1082         $DIR1/$tdir/bash -c 'sleep 2;
1083                 openfile -f O_RDONLY /proc/$$/exe >& /dev/null; echo $?'
1084         wait
1085         true
1086 }
1087 run_test 30 "recreate file race"
1088
1089 test_31a() {
1090         test_mkdir $DIR1/$tdir
1091         local writes=$(LANG=C dd if=/dev/zero of=$DIR/$tdir/$tfile \
1092                        count=1 2>&1 | awk 'BEGIN { FS="+" } /out/ {print $1}')
1093         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
1094         lctl set_param fail_loc=0x314
1095         local reads=$(LANG=C dd if=$DIR2/$tdir/$tfile of=/dev/null 2>&1 |
1096                       awk 'BEGIN { FS="+" } /in/ {print $1}')
1097         [ $reads -eq $writes ] || error "read" $reads "blocks, must be" $writes
1098 }
1099 run_test 31a "voluntary cancel / blocking ast race=============="
1100
1101 test_31b() {
1102         remote_ost || { skip "local OST" && return 0; }
1103         remote_ost_nodsh && skip "remote OST w/o dsh" && return 0
1104
1105         # make sure there is no local locks due to destroy
1106         wait_mds_ost_sync || error "wait_mds_ost_sync()"
1107         wait_delete_completed || error "wait_delete_completed()"
1108
1109         test_mkdir $DIR1/$tdir
1110         lfs setstripe $DIR/$tdir/$tfile -i 0 -c 1
1111         cp /etc/hosts $DIR/$tdir/$tfile
1112         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE   0x314
1113         lctl set_param fail_loc=0x314
1114         #define OBD_FAIL_LDLM_OST_FAIL_RACE      0x316
1115         do_facet ost1 lctl set_param fail_loc=0x316
1116         # Don't crash kernel
1117         cat $DIR2/$tdir/$tfile > /dev/null 2>&1
1118         lctl set_param fail_loc=0
1119         do_facet ost1 lctl set_param fail_loc=0
1120         # cleanup: reconnect the client back
1121         df $DIR2
1122 }
1123 run_test 31b "voluntary OST cancel / blocking ast race=============="
1124
1125 #LU-14949 - multi-client version of the test 31r in sanity.
1126 test_31r() {
1127         touch $DIR/$tfile.target
1128         touch $DIR/$tfile.source
1129
1130         ls -l $DIR/$tfile.target # cache it for sure
1131
1132         #OBD_FAIL_LLITE_OPEN_DELAY 0x1419
1133         $LCTL set_param fail_loc=0x1419 fail_val=3
1134         cat $DIR/$tfile.target &
1135         CATPID=$!
1136
1137         # Guarantee open is waiting before we get here
1138         sleep 1
1139         mv $DIR2/$tfile.source $DIR2/$tfile.target
1140
1141         wait $CATPID
1142         RC=$?
1143         if [[ $RC -ne 0 ]]; then
1144                 error "open with cat failed, rc=$RC"
1145         fi
1146 }
1147 run_test 31r "open-rename(replace) race"
1148
1149 test_32b() { # bug 11270
1150         remote_ost_nodsh && skip "remote OST with nodsh" && return
1151
1152         local node
1153         local facets=$(get_facets OST)
1154         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
1155
1156         save_lustre_params client "osc.*.contention_seconds" > $p
1157         save_lustre_params $facets \
1158                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
1159         save_lustre_params $facets \
1160                 "ldlm.namespaces.filter-*.contended_locks" >> $p
1161         save_lustre_params $facets \
1162                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
1163         clear_stats $OSC.*.${OSC}_stats
1164
1165         # agressive lockless i/o settings
1166         do_nodes $(comma_list $(osts_nodes)) \
1167                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
1168                         ldlm.namespaces.filter-*.contended_locks=0 \
1169                         ldlm.namespaces.filter-*.contention_seconds=60"
1170         lctl set_param -n $OSC.*.contention_seconds=60
1171         for i in {1..5}; do
1172                 dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
1173                         /dev/null 2>&1
1174                 dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
1175                         /dev/null 2>&1
1176         done
1177         [ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -ne 0 ] ||
1178                 error "lockless i/o was not triggered"
1179         # disable lockless i/o (it is disabled by default)
1180         do_nodes $(comma_list $(osts_nodes)) \
1181                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
1182                         ldlm.namespaces.filter-*.contended_locks=32 \
1183                         ldlm.namespaces.filter-*.contention_seconds=0"
1184         # set contention_seconds to 0 at client too, otherwise Lustre still
1185         # remembers lock contention
1186         lctl set_param -n $OSC.*.contention_seconds=0
1187         clear_stats $OSC.*.${OSC}_stats
1188         for i in {1..1}; do
1189                 dd if=/dev/zero of=$DIR1/$tfile bs=4k count=1 conv=notrunc > \
1190                         /dev/null 2>&1
1191                 dd if=/dev/zero of=$DIR2/$tfile bs=4k count=1 conv=notrunc > \
1192                         /dev/null 2>&1
1193         done
1194         [ $(calc_stats $OSC.*.${OSC}_stats lockless_write_bytes) -eq 0 ] ||
1195                 error "lockless i/o works when disabled"
1196         rm -f $DIR1/$tfile
1197         restore_lustre_params <$p
1198         rm -f $p
1199 }
1200 # Disable test 32b prior to full removal
1201 #run_test 32b "lockless i/o"
1202
1203 print_jbd_stat () {
1204         local mdts=$(get_facets MDS)
1205         local stat=0
1206         local varsvc
1207         local dev
1208         local mds
1209
1210         for mds in ${mdts//,/ }; do
1211                 varsvc=${mds}_svc
1212
1213                 dev=$(basename $(do_facet $mds "lctl get_param -n \
1214                         osd*.${!varsvc}.mntdev | xargs readlink -f"))
1215                 val=$(do_facet $mds "cat /proc/fs/jbd*/${dev}{,:*,-*}/info \
1216                         2>/dev/null | head -n1")
1217                 val=${val%% *};
1218                 stat=$((stat + val))
1219         done
1220         echo $stat
1221 }
1222
1223 # commit on sharing tests
1224 test_33a() {
1225         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1226
1227         [ $CLIENTCOUNT -lt 2 ] &&
1228                 skip "Need two or more clients, have $CLIENTCOUNT"
1229
1230         local nfiles=${TEST33_NFILES:-10000}
1231         local param_file=$TMP/$tfile-params
1232         local COS
1233         local jbdold="N/A"
1234         local jbdnew="N/A"
1235         local jbd
1236
1237         save_lustre_params $(get_facets MDS) \
1238                 "mdt.*.commit_on_sharing" > $param_file
1239
1240         for COS in 0 1; do
1241                 do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
1242                 avgjbd=0
1243                 avgtime=0
1244                 for i in 1 2 3; do
1245                         do_nodes $CLIENT1,$CLIENT2 "mkdir -p $DIR1/$tdir-\\\$(hostname)-$i"
1246
1247                 [ "$mds1_FSTYPE" = ldiskfs ] && jbdold=$(print_jbd_stat)
1248                 echo "=== START createmany old: $jbdold transaction"
1249                 local elapsed=$(do_and_time "do_nodes $CLIENT1,$CLIENT2 createmany -o $DIR1/$tdir-\\\$(hostname)-$i/f- -r$DIR2/$tdir-\\\$(hostname)-$i/f- $nfiles > /dev/null 2>&1")
1250                 [ "$mds1_FSTYPE" = ldiskfs ] && jbdnew=$(print_jbd_stat)
1251                 [ "$mds1_FSTYPE" = ldiskfs ] && jbd=$(( jbdnew - jbdold ))
1252                 echo "=== END   createmany new: $jbdnew transaction :  $jbd transactions  nfiles $nfiles time $elapsed COS=$COS"
1253                 [ "$mds1_FSTYPE" = ldiskfs ] && avgjbd=$(( avgjbd + jbd ))
1254                 avgtime=$(( avgtime + elapsed ))
1255                 done
1256         eval cos${COS}_jbd=$((avgjbd / 3))
1257         eval cos${COS}_time=$((avgtime / 3))
1258         done
1259
1260         echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
1261         echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
1262         [ "$cos0_jbd" != 0 ] &&
1263                 echo "COS=1 vs COS=0 jbd:  $((((cos1_jbd/cos0_jbd - 1)) * 100 )) %"
1264         [ "$cos0_time" != 0 ] &&
1265                 echo "COS=1 vs COS=0 time: $((((cos1_time/cos0_time - 1)) * 100 )) %"
1266
1267         restore_lustre_params < $param_file
1268         rm -f $param_file
1269         return 0
1270 }
1271 run_test 33a "commit on sharing, cross crete/delete, 2 clients, benchmark"
1272
1273 # commit on sharing tests
1274 test_33b() {
1275         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1276
1277         [ $CLIENTCOUNT -ge 2 ] ||
1278                 { skip "Need two or more clients, have $CLIENTCOUNT" &&
1279                                                                 return 0; }
1280         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1281
1282         local nfiles=${TEST33_NFILES:-10000}
1283         local param_file=$TMP/$tfile-params
1284
1285         save_lustre_params $(get_facets MDS) \
1286                 "mdt.*.commit_on_sharing" > $param_file
1287
1288         local COS
1289         local jbdold
1290         local jbdnew
1291         local jbd
1292         local MDTIDX=1
1293
1294         for COS in 0 1; do
1295                 do_facet $SINGLEMDS lctl set_param mdt.*.commit_on_sharing=$COS
1296                 avgjbd=0
1297                 avgtime=0
1298                 for i in 1 2 3; do
1299                         do_node $CLIENT1 "$LFS mkdir -i $MDTIDX \
1300                                           $DIR1/$tdir-\\\$(hostname)-$i"
1301
1302                         jbdold=$(print_jbd_stat)
1303                         echo "=== START createmany old: $jbdold transaction"
1304                         local elapsed=$(do_and_time "do_nodes $CLIENT1,$CLIENT2\
1305                                 createmany -o $DIR1/$tdir-\\\$(hostname)-$i/f- \
1306                                 -r$DIR2/$tdir-\\\$(hostname)-$i/f- $nfiles > \
1307                                                                 /dev/null 2>&1")
1308                         jbdnew=$(print_jbd_stat)
1309                         jbd=$(( jbdnew - jbdold ))
1310                         echo "=== END   createmany new: $jbdnew transaction : \
1311                         $jbd transactions nfiles $nfiles time $elapsed COS=$COS"
1312                         avgjbd=$(( avgjbd + jbd ))
1313                         avgtime=$(( avgtime + elapsed ))
1314                 done
1315                 eval cos${COS}_jbd=$((avgjbd / 3))
1316                 eval cos${COS}_time=$((avgtime / 3))
1317         done
1318
1319         echo "COS=0 transactions (avg): $cos0_jbd  time (avg): $cos0_time"
1320         echo "COS=1 transactions (avg): $cos1_jbd  time (avg): $cos1_time"
1321         [ "$cos0_jbd" != 0 ] &&
1322             echo "COS=1 vs COS=0 jbd: $(((cos1_jbd/cos0_jbd - 1) * 100)) %"
1323         [ "$cos0_time" != 0 ] &&
1324             echo "COS=1 vs COS=0 time: $(((cos1_time/cos0_time - 1) * 100)) %"
1325
1326         restore_lustre_params < $param_file
1327         rm -f $param_file
1328         return 0
1329 }
1330 run_test 33b "COS: cross create/delete, 2 clients, benchmark under remote dir"
1331
1332 # arg1 is description, arg2 is operations before Sync-on-Lock-Cancel, arg3 is
1333 # the operation that triggers SoLC
1334 op_trigger_solc() {
1335         local sync_count
1336         local total=0
1337         local nodes=$(comma_list $(mdts_nodes))
1338
1339         sync_all_data
1340
1341         # trigger CoS twice in case transaction commit before unlock
1342         for i in 1 2; do
1343                 bash -c "$2"
1344                 do_nodes $nodes "$LCTL set_param -n mdt.*.sync_count=0"
1345                 bash -c "$3"
1346                 sync_count=$(do_nodes $nodes \
1347                         "lctl get_param -n mdt.*MDT*.sync_count" | calc_sum)
1348                 total=$((total + sync_count));
1349                 rm -rf $DIR/$tdir/*
1350                 sync_all_data
1351         done
1352
1353         echo $1
1354         echo "  $2"
1355         echo "  $3"
1356         echo "  SoLC count $total"
1357         (( total > 0 )) || error "$3 didn't trigger SoLC"
1358 }
1359
1360 test_33_run() {
1361         echo $1
1362         echo "  $2"
1363         eval $2
1364 }
1365
1366 test_33c() {
1367         (( MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
1368         (( MDS1_VERSION >= $(version_code 2.7.63) )) ||
1369                 skip "DNE CoS not supported"
1370
1371         # LU-13522
1372         stop mds1
1373         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS || error "start mds1 failed"
1374
1375         mkdir_on_mdt0 $DIR/$tdir
1376         sync_all_data
1377
1378         if (( MDS1_VERSION < $(version_code 2.15.55.204) )); then
1379                 op_trigger_solc "create remote dir and local dir" \
1380                         "$LFS mkdir -i 1 $DIR/$tdir/remote" \
1381                         "$LFS mkdir -i 0 $DIR/$tdir/local"
1382                 (( MDSCOUNT > 2 )) &&
1383                 op_trigger_solc "create remote dirs on different MDTs" \
1384                         "$LFS mkdir -i 1 $DIR/$tdir/remote.1" \
1385                         "$LFS mkdir -i 2 $DIR/$tdir/remote.2"
1386         fi
1387         op_trigger_solc "create file on 2nd stripe under striped directory" \
1388                 "$LFS mkdir -i 0 -c 2 $DIR/$tdir/striped" \
1389                 "touch $DIR2/$tdir/striped/subfile"
1390
1391         echo
1392         echo "Below operations shouldn't trigger Solc:"
1393         $LFS mkdir -i 0 -c 2 $DIR/$tdir/striped
1394         sync_all_data
1395         do_facet mds1 "lctl set_param -n mdt.*.sync_count=0"
1396         if (( MDS1_VERSION >= $(version_code 2.15.55.204) )); then
1397                 test_33_run "create file on 2nd stripe after setattr" \
1398                         "chmod 777 $DIR/$tdir/striped; \
1399                          touch $DIR2/$tdir/striped/subfile"
1400                 test_33_run "create remote dir and local dir" \
1401                         "$LFS mkdir -i 1 $DIR/$tdir/remote" \
1402                         "$LFS mkdir -i 0 $DIR/$tdir/local"
1403                 (( MDSCOUNT > 2 )) &&
1404                 test_33_run "create remote dirs on different MDTs" \
1405                         "$LFS mkdir -i 1 $DIR/$tdir/remote.1" \
1406                         "$LFS mkdir -i 2 $DIR/$tdir/remote.2"
1407         fi
1408         test_33_run "create local dir after remote dir creation transaction commit" \
1409                 "$LFS mkdir -i 1 $DIR/$tdir/remote.3; \
1410                  do_facet mds2 $LCTL set_param -n osd*.*MDT0001.force_sync 1;
1411                  mkdir $DIR/$tdir/local.3"
1412         sync_count=$(do_facet mds1 "lctl get_param -n mdt.*MDT0000.sync_count")
1413         echo "Solc count $sync_count"
1414         [ $sync_count -eq 0 ] || error "Sync-Lock-Cancel triggered"
1415 }
1416 run_test 33c "Cancel cross-MDT lock should trigger Sync-on-Lock-Cancel"
1417
1418 # arg1 is description, arg2 is operations done before CoS, arg3 is the operation
1419 # that triggers CoS
1420 op_trigger_cos() {
1421         local commit_nr
1422         local total=0
1423         local nodes=$(comma_list $(mdts_nodes))
1424
1425         sync_all_data
1426
1427         # trigger CoS twice in case transaction commit before unlock
1428         for i in 1 2; do
1429                 bash -c "$2"
1430                 do_nodes $nodes "lctl set_param -n mdt.*.async_commit_count=0"
1431                 bash -c "$3"
1432                 commit_nr=$(do_nodes $nodes \
1433                         "lctl get_param -n mdt.*.async_commit_count" | calc_sum)
1434                 total=$((total + commit_nr));
1435                 rm -rf $DIR/$tdir
1436                 sync_all_data
1437         done
1438
1439         echo $1
1440         echo "  $2"
1441         echo "  $3"
1442         echo "  CoS count $total"
1443         (( total > 0 )) || error "$3 didn't trigger CoS"
1444 }
1445
1446 test_33d() {
1447         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
1448         (( MDS1_VERSION >= $(version_code 2.7.63) )) ||
1449                 skip "DNE CoS not supported"
1450
1451         if (( $MDS1_VERSION < $(version_code 2.15.55.133) )); then
1452                 op_trigger_cos "remote directory unlink" \
1453                         "$LFS mkdir -i 1 $DIR/$tdir" "rmdir $DIR2/$tdir"
1454                 op_trigger_cos "striped directory create" "mkdir $DIR/$tdir" \
1455                         "$LFS mkdir -c 2 $DIR2/$tdir/subdir"
1456                 op_trigger_cos "striped directory setattr" \
1457                         "$LFS mkdir -c 2 $DIR/$tdir" "chmod 713 $DIR2/$tdir"
1458                 op_trigger_cos "striped directory unlink" \
1459                         "$LFS mkdir -c 2 $DIR/$tdir" "rmdir $DIR2/$tdir"
1460                 op_trigger_cos "cross-MDT link" \
1461                         "mkdir $DIR/$tdir; \
1462                         $LFS mkdir -i 0 $DIR/$tdir/d1; \
1463                         $LFS mkdir -i 1 $DIR/$tdir/d2; \
1464                         touch $DIR/$tdir/d1/tgt" \
1465                         "ln $DIR2/$tdir/d1/tgt $DIR2/$tdir/d2/src"
1466         fi
1467
1468         op_trigger_cos "remote directory create" "$LFS mkdir -i 0 $DIR/$tdir" \
1469                 "$LFS mkdir -i 1 $DIR2/$tdir/subdir"
1470         op_trigger_cos "cross-MDT rename" \
1471                 "mkdir $DIR/$tdir; \
1472                 $LFS mkdir -i 0 $DIR/$tdir/d1; \
1473                 $LFS mkdir -i 1 $DIR/$tdir/d2; \
1474                 touch $DIR/$tdir/d1/src" \
1475                 "mv $DIR2/$tdir/d1/src $DIR2/$tdir/d2/tgt"
1476         op_trigger_cos "migrate" \
1477                 "$LFS mkdir -i 0 $DIR/$tdir" \
1478                 "$LFS migrate -m 1 $DIR2/$tdir"
1479
1480         return 0
1481 }
1482 run_test 33d "dependent transactions should trigger COS"
1483
1484 test_33e() {
1485         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
1486         (( MDS1_VERSION >= $(version_code 2.7.63) )) ||
1487                 skip "DNE CoS not supported"
1488
1489         $LFS mkdir -i 0 $DIR/$tdir
1490         $LFS mkdir -i 0 $DIR/$tdir/d1
1491         $LFS mkdir -i 1 $DIR/$tdir/d2
1492
1493         local nodes=$(comma_list $(mdts_nodes))
1494         do_nodes $nodes "lctl set_param -n mdt.*.async_commit_count=0"
1495
1496         test_33_run "plain dir creation" "mkdir $DIR2/$tdir/plain"
1497         test_33_run "open file and write" "echo abc > $DIR2/$tdir/$tfile"
1498         test_33_run "append write" "echo dfg >> $DIR2/$tdir/$tfile"
1499         test_33_run "setattr" "touch $DIR2/$tdir/$tfile"
1500         test_33_run "file unlink" "rm $DIR2/$tdir/$tfile"
1501         test_33_run "plain dir unlink" "rmdir $DIR2/$tdir/plain"
1502         if (( MDS1_VERSION >= $(version_code 2.15.55.133) )); then
1503                 test_33_run "striped directory creation" \
1504                         "$LFS mkdir -i 0 -c 2 $DIR2/$tdir/striped"
1505                 test_33_run "set default LMV to create striped subdir" \
1506                         "$LFS setdirstripe -D -c 2 $DIR/$tdir"
1507                 test_33_run "striped subdir creation" \
1508                         "createmany -d $DIR/$tdir/subdir 100"
1509                 test_33_run "sub file creation and write" \
1510                         "createmany -o $DIR/$tdir/subfile 100; \
1511                         echo abc > $DIR/$tdir/subfile1"
1512                 test_33_run "sub file append write" \
1513                         "echo dfg >> $DIR2/$tdir/subfile2"
1514                 test_33_run "subdir setatttr" "touch $DIR2/$tdir/subdir1"
1515                 test_33_run "subdir unlink" \
1516                         "unlinkmany -d $DIR/$tdir/subdir 100"
1517                 test_33_run "sub file unlink" \
1518                         "unlinkmany $DIR2/$tdir/subfile 100"
1519                 test_33_run "sub file creation follows striped dir chmod" \
1520                         "chmod 777 $DIR/$tdir/striped; \
1521                          touch $DIR/$tdir/striped/subfile"
1522                 test_33_run "striped directory unlink" \
1523                         "rm -rf $DIR2/$tdir/striped"
1524         fi
1525
1526         test_33_run "directory unlink" "rm -rf $DIR2/$tdir"
1527
1528         local async_commit_count=$(do_nodes $nodes \
1529                 "lctl get_param -n mdt.*.async_commit_count" | calc_sum)
1530         echo "CoS count $async_commit_count"
1531         (( async_commit_count == 0 )) || error "CoS triggerred"
1532 }
1533 run_test 33e "independent transactions shouldn't trigger COS"
1534
1535 # End commit on sharing tests
1536
1537 get_ost_lock_timeouts() {
1538     local nodes=${1:-$(comma_list $(osts_nodes))}
1539
1540     local locks=$(do_nodes $nodes \
1541         "lctl get_param -n ldlm.namespaces.filter-*.lock_timeouts" | calc_sum)
1542
1543     echo $locks
1544 }
1545
1546 cleanup_34() {
1547         local i
1548         trap 0
1549         do_nodes $(comma_list $(osts_nodes)) \
1550                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1551         for i in $(seq $OSTCOUNT); do
1552                 wait_osc_import_ready client ost$i
1553         done
1554 }
1555
1556 test_34() { #16129
1557         remote_ost_nodsh && skip "remote OST with nodsh" && return
1558         local OPER
1559         local lock_in
1560         local lock_out
1561         trap cleanup_34 EXIT RETURN
1562         for OPER in notimeout timeout ; do
1563                 rm $DIR1/$tfile 2>/dev/null
1564                 lock_in=$(get_ost_lock_timeouts)
1565                 if [ $OPER == "timeout" ] ; then
1566                         for j in `seq $OSTCOUNT`; do
1567                                 #define OBD_FAIL_PTLRPC_HPREQ_TIMEOUT    0x511
1568                                 do_facet ost$j lctl set_param fail_loc=0x511
1569                         done
1570                         echo lock should expire
1571                 else
1572                         for j in `seq $OSTCOUNT`; do
1573                                 #define OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT  0x512
1574                                 do_facet ost$j lctl set_param fail_loc=0x512
1575                         done
1576                         echo lock should not expire
1577                 fi
1578                 echo writing on client1
1579                 dd if=/dev/zero of=$DIR1/$tfile count=100 conv=notrunc > /dev/null 2>&1
1580                 sync &
1581                 echo reading on client2
1582                 dd of=/dev/null if=$DIR2/$tfile > /dev/null 2>&1
1583                 # wait for a lock timeout
1584                 sleep 4
1585                 lock_out=$(get_ost_lock_timeouts)
1586                 if [ $OPER == "timeout" ] ; then
1587                         if [ $lock_in == $lock_out ]; then
1588                                 error "no lock timeout happened"
1589                         else
1590                                 echo "success"
1591                         fi
1592                 else
1593                         if [ $lock_in != $lock_out ]; then
1594                                 error "lock timeout happened"
1595                         else
1596                                 echo "success"
1597                         fi
1598                 fi
1599         done
1600         cleanup_34
1601 }
1602 run_test 34 "no lock timeout under IO"
1603
1604 test_35() { # bug 17645
1605         local generation=[]
1606         local count=0
1607         gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
1608               awk '/generation/{print $2}')
1609         for g in $gen; do
1610                 generation[count]=$g
1611                 let count=count+1
1612         done
1613
1614         test_mkdir $MOUNT1/$tdir
1615         cancel_lru_locks mdc
1616
1617         # Let's initiate -EINTR situation by setting fail_loc and take
1618         # write lock on same file from same client. This will not cause
1619         # bl_ast yet as lock is already in local cache.
1620         #define OBD_FAIL_LDLM_INTR_CP_AST        0x317
1621         do_facet client "lctl set_param fail_loc=0x80000317"
1622         local timeout=$(do_facet $SINGLEMDS lctl get_param  -n timeout)
1623         let timeout=timeout*3
1624         local nr=0
1625         while test $nr -lt 10; do
1626                 log "Race attempt $nr"
1627                 local blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
1628                              awk '/ldlm_bl_callback/ { print $2 }')
1629                 test "x$blk1" = "x" && blk1=0
1630                 createmany -o $MOUNT2/$tdir/a 4000 &
1631                 pid1=$!
1632                 sleep 1
1633
1634                 # Let's make conflict and bl_ast
1635                 ls -la $MOUNT1/$tdir > /dev/null &
1636                 pid2=$!
1637
1638                 log "Wait for $pid1 $pid2 for $timeout sec..."
1639                 sleep $timeout
1640                 kill -9 $pid1 $pid2 > /dev/null 2>&1
1641                 wait
1642                 local blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
1643                              awk '/ldlm_bl_callback/ { print $2 }')
1644                 test "x$blk2" = "x" && blk2=0
1645                 test $blk2 -gt $blk1 && break
1646                 rm -fr $MOUNT1/$tdir
1647                 cancel_lru_locks mdc
1648                 let nr=nr+1
1649         done
1650         do_facet client "lctl set_param fail_loc=0x0"
1651         df -h $MOUNT1 $MOUNT2
1652         count=0
1653         gen=$(lctl get_param mdc.$FSNAME-MDT*-mdc-*.import | grep generation |
1654                 awk '/generation/{print $2}')
1655         for g in $gen; do
1656             if ! test "$g" -eq "${generation[count]}"; then
1657                 list=$(lctl list_param mdc.$FSNAME-MDT*-mdc-*.import)
1658                 local c=0
1659                 for imp in $list; do
1660                         if [ $c = $count ]; then
1661                                 break
1662                         fi
1663                         c=c+1
1664                 done
1665                 imp=$(echo "$imp" | awk -F"." '{print $2}')
1666                 error "Eviction happened on import $imp"
1667             fi
1668             let count=count+1
1669         done
1670 }
1671 run_test 35 "-EINTR cp_ast vs. bl_ast race does not evict client"
1672
1673 test_36() { #bug 16417
1674         local SIZE
1675         local SIZE_B
1676         local i
1677
1678         test_mkdir $DIR1/$tdir
1679         $LFS setstripe -c -1 $DIR1/$tdir
1680         i=0
1681         SIZE=50
1682         let SIZE_B=SIZE*1024*1024
1683         sync; sleep 2; sync # wait for delete thread
1684         wait_mds_ost_sync || error "wait_mds_ost_sync failed"
1685         wait_destroy_complete || error "wait_destroy_complete failed"
1686
1687         while [ $i -le 10 ]; do
1688                 lctl mark "start test - cycle ($i)"
1689                 local before=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1690                 dd if=/dev/zero of=$DIR1/$tdir/$tfile bs=1M count=$SIZE ||
1691                         error "dd $DIR1/$tdir/$tfile ${SIZE}MB failed"
1692                 sync          # sync data from client cache
1693                 sync_all_data # sync data from server cache (delayed allocation)
1694                 sleep 2
1695                 local after_dd=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1696                 multiop_bg_pause $DIR2/$tdir/$tfile O_r${SIZE_B}c || return 3
1697                 read_pid=$!
1698                 rm -f $DIR1/$tdir/$tfile
1699                 kill -USR1 $read_pid
1700                 wait $read_pid
1701                 sync; sleep 2; sync # Ensure new statfs
1702                 wait_delete_completed
1703                 local after=$(lfs_df $MOUNT1 | awk '/^filesystem/{ print $4; exit }')
1704                 echo "*** cycle($i) *** before($before) after_dd($after_dd)" \
1705                         "after($after)"
1706                 # this free space! not used
1707                 (( $after_dd <= $after)) ||
1708                         error "space leaked after_dd:$after_dd > after:$after"
1709                 let i=i+1
1710         done
1711 }
1712 run_test 36 "handle ESTALE/open-unlink correctly"
1713
1714 test_37() { # bug 18695
1715         test_mkdir $DIR1/$tdir
1716         multiop_bg_pause $DIR1/$tdir D_c || return 1
1717         MULTIPID=$!
1718         # create large directory (32kB seems enough from e2fsck, ~= 1000 files)
1719         createmany -m $DIR2/$tdir/f 10000
1720         # set mtime/atime backward
1721         touch -t 198001010000 $DIR2/$tdir
1722         kill -USR1 $MULTIPID
1723         nr_files=`lfs find $DIR1/$tdir -type f | wc -l`
1724         [ $nr_files -eq 10000 ] || error "$nr_files != 10000 truncated directory?"
1725
1726 }
1727 run_test 37 "check i_size is not updated for directory on close (bug 18695) =============="
1728
1729 # this should be set to past
1730 TEST_39_MTIME=`date -d "1 year ago" +%s`
1731
1732 # bug 11063
1733 test_39a() {
1734         local client1=${CLIENT1:-`hostname`}
1735         local client2=${CLIENT2:-`hostname`}
1736
1737         do_node $client1 "touch $DIR1/$tfile"
1738
1739         do_node $client1 "touch -m -d @$TEST_39_MTIME $DIR1/$tfile"
1740         local mtime1=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1741         [ "$mtime1" = $TEST_39_MTIME ] || \
1742                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
1743
1744         local d1=`do_node $client1 date +%s`
1745         do_node $client1 'echo hello >> '$DIR1/$tfile
1746         local d2=`do_node $client1 date +%s`
1747
1748         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1749         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
1750                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
1751
1752         do_node $client1 "mv $DIR1/$tfile $DIR1/$tfile-1"
1753
1754         for (( i=0; i < 2; i++ )) ; do
1755                 local mtime3=`do_node $client2 "stat -c %Y $DIR1/$tfile-1"`
1756                 [ "$mtime2" = "$mtime3" ] || \
1757                         error "mtime ($mtime2) changed (to $mtime3) on rename"
1758
1759                 cancel_lru_locks osc
1760                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1761         done
1762 }
1763 run_test 39a "test from 11063 =================================="
1764
1765 test_39b() {
1766         local client1=${CLIENT1:-`hostname`}
1767         local client2=${CLIENT2:-`hostname`}
1768
1769         touch $DIR1/$tfile
1770
1771         local mtime1=`stat -c %Y $DIR1/$tfile`
1772         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1773
1774         sleep 1
1775         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
1776
1777         for (( i=0; i < 2; i++ )) ; do
1778                 local mtime3=`stat -c %Y $DIR1/$tfile`
1779                 local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1780
1781                 [ "$mtime3" = "$mtime4" ] || \
1782                         error "different mtime on clients: $mtime3, $mtime4"
1783                 [ "$mtime3" = $TEST_39_MTIME ] || \
1784                         error "lost mtime: $mtime3, should be $TEST_39_MTIME"
1785
1786                 cancel_lru_locks osc
1787                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1788         done
1789 }
1790 run_test 39b "11063 problem 1 =================================="
1791
1792 test_39c() {
1793         local client1=${CLIENT1:-`hostname`}
1794         local client2=${CLIENT2:-`hostname`}
1795
1796         echo hello > $DIR1/$tfile
1797
1798         local mtime1=`stat -c %Y $DIR1/$tfile`
1799         local mtime2=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1800         [ "$mtime1" = "$mtime2" ] || \
1801                 error "create: different mtime on clients: $mtime1, $mtime2"
1802
1803         sleep 1
1804         $TRUNCATE $DIR1/$tfile 1
1805
1806         for (( i=0; i < 2; i++ )) ; do
1807                 local mtime3=`stat -c %Y $DIR1/$tfile`
1808                 local mtime4=`do_node $client2 "stat -c %Y $DIR1/$tfile"`
1809
1810                 [ "$mtime3" = "$mtime4" ] || \
1811                         error "different mtime on clients: $mtime3, $mtime4"
1812                 [ "$mtime3" -gt $mtime2 ] || \
1813                         error "truncate did not update mtime: $mtime2, $mtime3"
1814
1815                 cancel_lru_locks osc
1816                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
1817         done
1818 }
1819 run_test 39c "check truncate mtime update ======================"
1820
1821 test_39d() { # LU-7310
1822         touch $DIR1/$tfile
1823         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
1824
1825         local mtime1=$(stat -c %Y $DIR2/$tfile)
1826         [ "$mtime1" = $TEST_39_MTIME ] ||
1827                 error "mtime: $mtime1, should be $TEST_39_MTIME"
1828
1829         # force sync write
1830         # define OBD_FAIL_OSC_NO_GRANT 0x411
1831         $LCTL set_param fail_loc=0x411
1832
1833         local d1=$(date +%s)
1834         echo hello >> $DIR1/$tfile
1835         local d2=$(date +%s)
1836
1837         $LCTL set_param fail_loc=0
1838
1839         cancel_lru_locks $OSC
1840
1841         local mtime2=$(stat -c %Y $DIR2/$tfile)
1842         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] ||
1843                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
1844 }
1845 run_test 39d "sync write should update mtime"
1846
1847 pdo_sched() {
1848         # how long 40-47 take with specific delay
1849         # sleep 0.1 # 78s
1850         # sleep 0.2 # 103s
1851         # sleep 0.3 # 124s
1852         sleep 0.5 # 164s
1853 }
1854
1855 # for pdo testing, we must cancel MDT-MDT locks as well as client locks to
1856 # avoid unexpected delays due to previous tests
1857 pdo_lru_clear() {
1858         cancel_lru_locks mdc
1859         do_nodes $(comma_list $(mdts_nodes)) \
1860                 $LCTL set_param -n ldlm.namespaces.*mdt*.lru_size=clear
1861         do_nodes $(comma_list $(mdts_nodes)) \
1862                 $LCTL get_param ldlm.namespaces.*mdt*.lock_unused_count \
1863                         ldlm.namespaces.*mdt*.lock_count | grep -v '=0'
1864 }
1865
1866 # check that pid exists hence second operation wasn't blocked by first one
1867 # if it is so then there is no conflict, return 0
1868 # else second operation is conflicting with first one, return 1
1869 check_pdo_conflict() {
1870         local pid=$1
1871         local conflict=0
1872         pdo_sched # to ensure OP1 is finished on client if OP2 is blocked by OP1
1873         if [[ `ps --pid $pid | wc -l` == 1 ]]; then
1874                 conflict=1
1875                 echo "Conflict"
1876         else
1877                 echo "No conflict"
1878         fi
1879         return $conflict
1880 }
1881
1882 # pdirop tests
1883 # test 40: check non-blocking operations
1884 test_40a() {
1885         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1886
1887         mkdir_on_mdt0 $DIR2/$tdir
1888         pdo_lru_clear
1889 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1890         do_nodes $(comma_list $(mdts_nodes)) \
1891                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1892         mkdir $DIR1/$tdir/$tfile &
1893         PID1=$!; pdo_sched
1894         touch $DIR2/$tdir/$tfile-2
1895         check_pdo_conflict $PID1 || error "create is blocked"
1896         mkdir $DIR2/$tdir/$tfile-3
1897         check_pdo_conflict $PID1 || error "mkdir is blocked"
1898         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1899         check_pdo_conflict $PID1 || error "link is blocked"
1900         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1901         check_pdo_conflict $PID1 || error "rename is blocked"
1902         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1903         check_pdo_conflict $PID1 || error "getattr is blocked"
1904         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1905         rmdir $DIR2/$tdir/$tfile-3
1906         check_pdo_conflict $PID1 || error "unlink is blocked"
1907
1908         #  all operations above shouldn't wait the first one
1909         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1910         do_nodes $(comma_list $(mdts_nodes)) \
1911                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1912         wait $PID1
1913         rm -rf $DIR/$tdir
1914         return 0
1915 }
1916 run_test 40a "pdirops: create vs others =============="
1917
1918 test_40b() {
1919         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1920
1921         mkdir_on_mdt0 $DIR2/$tdir
1922         pdo_lru_clear
1923 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1924         do_nodes $(comma_list $(mdts_nodes)) \
1925                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1926         touch $DIR1/$tdir/$tfile &
1927         PID1=$!; pdo_sched
1928         # open|create
1929         touch $DIR2/$tdir/$tfile-2
1930         check_pdo_conflict $PID1 || error "create is blocked"
1931         mkdir $DIR2/$tdir/$tfile-3
1932         check_pdo_conflict $PID1 || error "mkdir is blocked"
1933         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1934         check_pdo_conflict $PID1 || error "link is blocked"
1935         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1936         check_pdo_conflict $PID1 || error "rename is blocked"
1937         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1938         check_pdo_conflict $PID1 || error "getattr is blocked"
1939         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1940         rmdir $DIR2/$tdir/$tfile-3
1941         check_pdo_conflict $PID1 || error "unlink is blocked"
1942         # all operations above shouldn't wait the first one
1943
1944         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1945         do_nodes $(comma_list $(mdts_nodes)) \
1946                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1947         wait $PID1
1948         rm -rf $DIR/$tdir
1949         return 0
1950 }
1951 run_test 40b "pdirops: open|create and others =============="
1952
1953 test_40c() {
1954         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1955
1956         mkdir_on_mdt0 $DIR2/$tdir
1957         pdo_lru_clear
1958         touch $DIR1/$tdir/$tfile
1959 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1960         do_nodes $(comma_list $(mdts_nodes)) \
1961                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1962         link $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
1963         PID1=$!; pdo_sched
1964         # open|create
1965         touch $DIR2/$tdir/$tfile-2
1966         check_pdo_conflict $PID1 || error "create is blocked"
1967         mkdir $DIR2/$tdir/$tfile-3
1968         check_pdo_conflict $PID1 || error "mkdir is blocked"
1969         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
1970         check_pdo_conflict $PID1 || error "link is blocked"
1971         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
1972         check_pdo_conflict $PID1 || error "rename is blocked"
1973         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
1974         check_pdo_conflict $PID1 || error "getattr is blocked"
1975         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
1976         rmdir $DIR2/$tdir/$tfile-3
1977         check_pdo_conflict $PID1 || error "unlink is blocked"
1978
1979         # all operations above shouldn't wait the first one
1980         check_pdo_conflict $PID1 || error "parallel operation is blocked"
1981         do_nodes $(comma_list $(mdts_nodes)) \
1982                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
1983         wait $PID1
1984         rm -rf $DIR/$tdir
1985         return 0
1986 }
1987 run_test 40c "pdirops: link and others =============="
1988
1989 test_40d() {
1990         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1991
1992         mkdir_on_mdt0 $DIR2/$tdir
1993         pdo_lru_clear
1994         touch $DIR1/$tdir/$tfile
1995 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
1996         do_nodes $(comma_list $(mdts_nodes)) \
1997                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
1998         rm $DIR1/$tdir/$tfile &
1999         PID1=$!; pdo_sched
2000         # open|create
2001         touch $DIR2/$tdir/$tfile-2
2002         check_pdo_conflict $PID1 || error "create is blocked"
2003         mkdir $DIR2/$tdir/$tfile-3
2004         check_pdo_conflict $PID1 || error "mkdir is blocked"
2005         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
2006         check_pdo_conflict $PID1 || error "link is blocked"
2007         mv $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-5
2008         check_pdo_conflict $PID1 || error "rename is blocked"
2009         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
2010         check_pdo_conflict $PID1 || error "getattr is blocked"
2011         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-5
2012         rmdir $DIR2/$tdir/$tfile-3
2013         check_pdo_conflict $PID1 || error "unlink is blocked"
2014
2015         # all operations above shouldn't wait the first one
2016         check_pdo_conflict $PID1 || error "parallel operation is blocked"
2017         do_nodes $(comma_list $(mdts_nodes)) \
2018                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2019         wait $PID1
2020         return 0
2021 }
2022 run_test 40d "pdirops: unlink and others =============="
2023
2024 test_40e() {
2025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2026
2027         mkdir_on_mdt0 $DIR2/$tdir
2028         pdo_lru_clear
2029         touch $DIR1/$tdir/$tfile
2030 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2031         do_nodes $(comma_list $(mdts_nodes)) \
2032                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2033         mv $DIR1/$tdir/$tfile $DIR1/$tdir/$tfile-0 &
2034         PID1=$!; pdo_sched
2035         # open|create
2036         touch $DIR2/$tdir/$tfile-2
2037         check_pdo_conflict $PID1 || error "create is blocked"
2038         mkdir $DIR2/$tdir/$tfile-3
2039         check_pdo_conflict $PID1 || error "mkdir is blocked"
2040         link $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile-4
2041         check_pdo_conflict $PID1 || error "link is blocked"
2042         stat $DIR2/$tdir/$tfile-3 $DIR2/$tdir/$tfile-4 > /dev/null
2043         check_pdo_conflict $PID1 || error "getattr is blocked"
2044         rm $DIR2/$tdir/$tfile-4 $DIR2/$tdir/$tfile-2
2045         rmdir $DIR2/$tdir/$tfile-3
2046         check_pdo_conflict $PID1 || error "unlink is blocked"
2047
2048        # all operations above shouldn't wait the first one
2049         check_pdo_conflict $PID1 || error "parallel operation is blocked"
2050         do_nodes $(comma_list $(mdts_nodes)) \
2051                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2052         wait $PID1
2053         rm -rf $DIR/$tdir
2054         return 0
2055 }
2056 run_test 40e "pdirops: rename and others =============="
2057
2058 # test 41: create blocking operations
2059 test_41a() {
2060         pdo_lru_clear
2061 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2062         do_nodes $(comma_list $(mdts_nodes)) \
2063                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2064         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2065         PID1=$! ; pdo_sched
2066         mkdir $DIR2/$tfile &
2067         PID2=$! ; pdo_sched
2068         do_nodes $(comma_list $(mdts_nodes)) \
2069                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2070         check_pdo_conflict $PID1 && { wait $PID1; echo "mkdir isn't blocked"; }
2071         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2072         rm -rf $DIR/$tfile*
2073         return 0
2074 }
2075 run_test 41a "pdirops: create vs mkdir =============="
2076
2077 test_41b() {
2078         pdo_lru_clear
2079 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2080         do_nodes $(comma_list $(mdts_nodes)) \
2081                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2082         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2083         PID1=$! ; pdo_sched
2084         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2085         PID2=$! ; pdo_sched
2086         do_nodes $(comma_list $(mdts_nodes)) \
2087                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2088         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2089         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2090         rm -rf $DIR/$tfile*
2091         return 0
2092 }
2093 run_test 41b "pdirops: create vs create =============="
2094
2095 test_41c() {
2096         pdo_lru_clear
2097         touch $DIR1/$tfile-2
2098 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2099         do_nodes $(comma_list $(mdts_nodes)) \
2100                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2101         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2102         PID1=$! ; pdo_sched
2103         link $DIR2/$tfile-2 $DIR2/$tfile &
2104         PID2=$! ; pdo_sched
2105         do_nodes $(comma_list $(mdts_nodes)) \
2106                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2107         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2108         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2109         rm -rf $DIR/$tfile*
2110         return 0
2111 }
2112 run_test 41c "pdirops: create vs link =============="
2113
2114 test_41d() {
2115         pdo_lru_clear
2116 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2117         do_nodes $(comma_list $(mdts_nodes)) \
2118                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2119         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2120         PID1=$! ; pdo_sched
2121         rm $DIR2/$tfile &
2122         PID2=$! ; pdo_sched
2123         do_nodes $(comma_list $(mdts_nodes)) \
2124                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2125         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2126         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2127         rm -rf $DIR/$tfile*
2128         return 0
2129 }
2130 run_test 41d "pdirops: create vs unlink =============="
2131
2132 test_41e() {
2133         pdo_lru_clear
2134         touch $DIR1/$tfile-2
2135 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2136         do_nodes $(comma_list $(mdts_nodes)) \
2137                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2138         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2139         PID1=$! ; pdo_sched
2140         mv $DIR2/$tfile-2 $DIR2/$tfile &
2141         PID2=$! ; pdo_sched
2142         do_nodes $(comma_list $(mdts_nodes)) \
2143                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2144         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2145         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2146         rm -rf $DIR/$tfile*
2147         return 0
2148 }
2149 run_test 41e "pdirops: create and rename (tgt) =============="
2150
2151 test_41f() {
2152         pdo_lru_clear
2153 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2154         do_nodes $(comma_list $(mdts_nodes)) \
2155                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2156         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2157         PID1=$! ; pdo_sched
2158         mv $DIR2/$tfile $DIR2/$tfile-2 &
2159         PID2=$! ; pdo_sched
2160         do_nodes $(comma_list $(mdts_nodes)) \
2161                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2162         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2163         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2164         rm -rf $DIR/$tfile*
2165         return 0
2166 }
2167 run_test 41f "pdirops: create and rename (src) =============="
2168
2169 test_41g() {
2170         pdo_lru_clear
2171 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2172         do_nodes $(comma_list $(mdts_nodes)) \
2173                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2174         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2175         PID1=$! ; pdo_sched
2176         stat $DIR2/$tfile > /dev/null &
2177         PID2=$! ; pdo_sched
2178         do_nodes $(comma_list $(mdts_nodes)) \
2179                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2180         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2181         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2182         rm -rf $DIR/$tfile*
2183         return 0
2184 }
2185 run_test 41g "pdirops: create vs getattr =============="
2186
2187 test_41h() {
2188         pdo_lru_clear
2189 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2190         do_nodes $(comma_list $(mdts_nodes)) \
2191                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2192         $MULTIOP $DIR1/$tfile oO_CREAT:O_RDWR:c &
2193         PID1=$! ; pdo_sched
2194         ls -lia $DIR2/ > /dev/null &
2195         PID2=$! ; pdo_sched
2196         do_nodes $(comma_list $(mdts_nodes)) \
2197                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2198         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2199         wait $PID2
2200         rm -rf $DIR/$tfile*
2201         return 0
2202 }
2203 run_test 41h "pdirops: create vs readdir =============="
2204
2205 sub_test_41i() {
2206         local PID1 PID2
2207         local fail_loc="$1"
2208         local ret=0
2209
2210         do_nodes $(comma_list $(mdts_nodes)) \
2211                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
2212
2213         $MULTIOP $DIR1/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
2214         PID1=$!
2215         sleep 0.2
2216         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c 2>/dev/null &
2217         PID2=$!
2218
2219         if ! wait $PID1 && ! wait $PID2; then
2220                 echo "Both creates failed (1 should fail, 1 should succeed)"
2221                 ret=1
2222         elif wait $PID1 && wait $PID2; then
2223                 echo "Both creates succeeded (1 should fail, 1 should succeed)"
2224                 ret=2
2225         fi
2226
2227         #Clean
2228         do_nodes $(comma_list $(mdts_nodes)) \
2229                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
2230         rm -f $DIR/$tfile
2231
2232         return $ret
2233 }
2234
2235 test_41i() {
2236         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
2237                 skip "Need MDS version newer than 2.13.56"
2238         local msg fail_loc
2239
2240 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
2241 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
2242         for fail_loc in "0x80000169" "0x8000016a"; do
2243                 echo "Begin 100 tests with fail_loc=$fail_loc"
2244                 printf "Progress: "
2245                 for i in {1..100}; do
2246                         printf "*"
2247                         msg=$(sub_test_41i "$fail_loc") ||
2248                                 { echo; error "iter=$i : $msg"; }
2249                 done
2250                 echo
2251         done
2252 }
2253 run_test 41i "reint_open: create vs create"
2254
2255
2256 # test 42: unlink and blocking operations
2257 test_42a() {
2258         pdo_lru_clear
2259 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2260         do_nodes $(comma_list $(mdts_nodes)) \
2261                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2262         mkdir $DIR1/$tfile &
2263         PID1=$! ; pdo_sched
2264         mkdir $DIR2/$tfile &
2265         PID2=$! ; pdo_sched
2266         do_nodes $(comma_list $(mdts_nodes)) \
2267                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2268         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
2269         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2270         rm -rf $DIR/$tfile*
2271         return 0
2272 }
2273 run_test 42a "pdirops: mkdir vs mkdir =============="
2274
2275 test_42b() {
2276         pdo_lru_clear
2277 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2278         do_nodes $(comma_list $(mdts_nodes)) \
2279                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2280         mkdir $DIR1/$tfile &
2281         PID1=$! ; pdo_sched
2282         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2283         PID2=$! ; pdo_sched
2284         do_nodes $(comma_list $(mdts_nodes)) \
2285                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2286         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2287         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2288         rm -rf $DIR/$tfile*
2289         return 0
2290 }
2291 run_test 42b "pdirops: mkdir vs create =============="
2292
2293 test_42c() {
2294         pdo_lru_clear
2295         touch $DIR1/$tfile-2
2296 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2297         do_nodes $(comma_list $(mdts_nodes)) \
2298                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2299         mkdir $DIR1/$tfile &
2300         PID1=$! ; pdo_sched
2301         link $DIR2/$tfile-2 $DIR2/$tfile &
2302         PID2=$! ; pdo_sched
2303         do_nodes $(comma_list $(mdts_nodes)) \
2304                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2305         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2306         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2307         rm -rf $DIR/$tfile*
2308         return 0
2309 }
2310 run_test 42c "pdirops: mkdir vs link =============="
2311
2312 test_42d() {
2313         pdo_lru_clear
2314 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2315         do_nodes $(comma_list $(mdts_nodes)) \
2316                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2317         mkdir $DIR1/$tfile &
2318         PID1=$! ; pdo_sched
2319         rmdir $DIR2/$tfile &
2320         PID2=$! ; pdo_sched
2321         do_nodes $(comma_list $(mdts_nodes)) \
2322                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2323         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2324         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2325         rm -rf $DIR/$tfile*
2326         return 0
2327 }
2328 run_test 42d "pdirops: mkdir vs unlink =============="
2329
2330 test_42e() {
2331         pdo_lru_clear
2332         touch $DIR1/$tfile-2
2333 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2334         do_nodes $(comma_list $(mdts_nodes)) \
2335                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2336         mkdir $DIR1/$tfile &
2337         PID1=$! ; pdo_sched
2338         mv -T $DIR2/$tfile-2 $DIR2/$tfile &
2339         PID2=$! ; pdo_sched
2340         do_nodes $(comma_list $(mdts_nodes)) \
2341                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2342         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2343         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2344         rm -rf $DIR/$tfile*
2345         return 0
2346 }
2347 run_test 42e "pdirops: mkdir and rename (tgt) =============="
2348
2349 test_42f() {
2350         pdo_lru_clear
2351 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2352         do_nodes $(comma_list $(mdts_nodes)) \
2353                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2354         mkdir $DIR1/$tfile &
2355         PID1=$! ; pdo_sched
2356         mv $DIR2/$tfile $DIR2/$tfile-2 &
2357         PID2=$! ; pdo_sched
2358         do_nodes $(comma_list $(mdts_nodes)) \
2359                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2360         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2361         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2362         rm -rf $DIR/$tfile*
2363         return 0
2364 }
2365 run_test 42f "pdirops: mkdir and rename (src) =============="
2366
2367 test_42g() {
2368         mkdir_on_mdt0 $DIR1/$tdir
2369         pdo_lru_clear
2370 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2371         do_nodes $(comma_list $(mdts_nodes)) \
2372                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2373         mkdir $DIR1/$tdir/$tfile &
2374         PID1=$! ; pdo_sched
2375         stat $DIR2/$tdir/$tfile > /dev/null &
2376         PID2=$! ; pdo_sched
2377         do_nodes $(comma_list $(mdts_nodes)) \
2378                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2379         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2380         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2381         rm -rf $DIR/$tdir
2382 }
2383 run_test 42g "pdirops: mkdir vs getattr =============="
2384
2385 test_42h() {
2386         pdo_lru_clear
2387 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2388         do_nodes $(comma_list $(mdts_nodes)) \
2389                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2390         mkdir $DIR1/$tfile &
2391         PID1=$! ; pdo_sched
2392         ls -lia $DIR2/ > /dev/null &
2393         PID2=$! ; pdo_sched
2394         do_nodes $(comma_list $(mdts_nodes)) \
2395                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2396         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2397         wait $PID2
2398         rm -rf $DIR/$tfile*
2399         return 0
2400 }
2401 run_test 42h "pdirops: mkdir vs readdir =============="
2402
2403 # test 43: rmdir,mkdir won't return -EEXIST
2404 test_43a() {
2405         for i in {1..1000}; do
2406                 mkdir $DIR1/$tdir || error "mkdir $tdir failed"
2407                 rmdir $DIR2/$tdir || error "rmdir $tdir failed"
2408         done
2409         return 0
2410 }
2411 run_test 43a "rmdir,mkdir doesn't return -EEXIST =============="
2412
2413 test_43b() {
2414         pdo_lru_clear
2415         touch $DIR1/$tfile
2416 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2417         do_nodes $(comma_list $(mdts_nodes)) \
2418                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2419         rm $DIR1/$tfile &
2420         PID1=$! ; pdo_sched
2421         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2422         PID2=$! ; pdo_sched
2423         do_nodes $(comma_list $(mdts_nodes)) \
2424                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2425         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2426         wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
2427         rm -rf $DIR/$tfile*
2428         return 0
2429 }
2430 run_test 43b "pdirops: unlink vs create =============="
2431
2432 test_43c() {
2433         pdo_lru_clear
2434         touch $DIR1/$tfile
2435         touch $DIR1/$tfile-2
2436 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2437         do_nodes $(comma_list $(mdts_nodes)) \
2438                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2439         rm $DIR1/$tfile &
2440         PID1=$! ; pdo_sched
2441         link $DIR2/$tfile-2 $DIR2/$tfile &
2442         PID2=$! ; pdo_sched
2443         do_nodes $(comma_list $(mdts_nodes)) \
2444                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2445         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2446         wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
2447         rm -rf $DIR/$tfile*
2448         return 0
2449 }
2450 run_test 43c "pdirops: unlink vs link =============="
2451
2452 test_43d() {
2453         pdo_lru_clear
2454         touch $DIR1/$tfile
2455 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2456         do_nodes $(comma_list $(mdts_nodes)) \
2457                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2458         rm $DIR1/$tfile &
2459         PID1=$! ; pdo_sched
2460         rm $DIR2/$tfile &
2461         PID2=$! ; pdo_sched
2462         do_nodes $(comma_list $(mdts_nodes)) \
2463                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2464         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2465         wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
2466         rm -rf $DIR/$tfile*
2467         return 0
2468 }
2469 run_test 43d "pdirops: unlink vs unlink =============="
2470
2471 test_43e() {
2472         pdo_lru_clear
2473         touch $DIR1/$tfile
2474         touch $DIR1/$tfile-2
2475 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2476         do_nodes $(comma_list $(mdts_nodes)) \
2477                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2478         rm $DIR1/$tfile &
2479         PID1=$! ; pdo_sched
2480         mv -u $DIR2/$tfile-2 $DIR2/$tfile &
2481         PID2=$! ; pdo_sched
2482         do_nodes $(comma_list $(mdts_nodes)) \
2483                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2484         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2485         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2486         rm -rf $DIR/$tfile*
2487         return 0
2488 }
2489 run_test 43e "pdirops: unlink and rename (tgt) =============="
2490
2491 test_43f() {
2492         pdo_lru_clear
2493         touch $DIR1/$tfile
2494 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2495         do_nodes $(comma_list $(mdts_nodes)) \
2496                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2497         rm $DIR1/$tfile &
2498         PID1=$! ; pdo_sched
2499         mv $DIR2/$tfile $DIR2/$tfile-2 &
2500         PID2=$! ; pdo_sched
2501         do_nodes $(comma_list $(mdts_nodes)) \
2502                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2503         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2504         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2505         rm -rf $DIR/$tfile*
2506         return 0
2507 }
2508 run_test 43f "pdirops: unlink and rename (src) =============="
2509
2510 test_43g() {
2511         pdo_lru_clear
2512         touch $DIR1/$tfile
2513 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2514         do_nodes $(comma_list $(mdts_nodes)) \
2515                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2516         rm $DIR1/$tfile &
2517         PID1=$! ; pdo_sched
2518         stat $DIR2/$tfile > /dev/null &
2519         PID2=$! ; pdo_sched
2520         do_nodes $(comma_list $(mdts_nodes)) \
2521                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2522         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2523         wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
2524         rm -rf $DIR/$tfile*
2525         return 0
2526 }
2527 run_test 43g "pdirops: unlink vs getattr =============="
2528
2529 test_43h() {
2530         pdo_lru_clear
2531         touch $DIR1/$tfile
2532 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2533         do_nodes $(comma_list $(mdts_nodes)) \
2534                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2535         rm $DIR1/$tfile &
2536         PID1=$! ; pdo_sched
2537         ls -lia $DIR2/ > /dev/null &
2538         PID2=$! ; pdo_sched
2539         do_nodes $(comma_list $(mdts_nodes)) \
2540                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2541         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2542         wait $PID2
2543         rm -rf $DIR/$tfile*
2544         return 0
2545 }
2546 run_test 43h "pdirops: unlink vs readdir =============="
2547
2548 test_43i() {
2549         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2550         pdo_lru_clear
2551         touch $DIR1/$tfile
2552 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2553         do_nodes $(comma_list $(mdts_nodes)) \
2554                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2555         rm $DIR1/$tfile &
2556         PID1=$! ; pdo_sched
2557         $LFS mkdir -i 1 $DIR2/$tfile &
2558         PID2=$! ; pdo_sched
2559         do_nodes $(comma_list $(mdts_nodes)) \
2560                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2561         check_pdo_conflict $PID1 &&
2562                 { wait $PID1; error "remote mkdir isn't blocked"; }
2563         wait $PID2 ; [ $? -eq 0 ] || error "remote mkdir must succeed"
2564         rm -rf $DIR/$tfile*
2565         return 0
2566 }
2567 run_test 43i "pdirops: unlink vs remote mkdir"
2568
2569 test_43j() {
2570         [[ $MDS1_VERSION -lt $(version_code 2.13.52) ]] &&
2571                 skip "Need MDS version newer than 2.13.52"
2572
2573         mkdir_on_mdt0 $DIR1/$tdir
2574         for i in {1..100}; do
2575 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_CREATE_RACE         0x167
2576                 do_nodes $(comma_list $(mdts_nodes)) \
2577                         "lctl set_param -n fail_loc=0x80000167 2>/dev/null ||
2578                                 true"
2579                 OK=0
2580                 mkdir $DIR1/$tdir/sub &
2581                 PID1=$!
2582                 mkdir $DIR2/$tdir/sub && ((OK++))
2583                 wait $PID1 && ((OK++))
2584                 (( OK == 1 )) || error "exactly one mkdir should succeed"
2585
2586                 rmdir $DIR1/$tdir/sub || error "rmdir failed"
2587         done
2588         return 0
2589 }
2590 run_test 43j "racy mkdir return EEXIST =============="
2591
2592 sub_test_43k() {
2593         local PID1 PID2
2594         local fail_loc="$1"
2595         local ret=0
2596
2597         # We test in a separate directory to be able to unblock server thread in
2598         # cfs_race() if LCK_PW is taken on the parent by mdt_reint_unlink.
2599         test_mkdir $DIR2/$tdir
2600         touch $DIR2/$tdir/$tfile
2601         pdo_lru_clear
2602
2603         do_nodes $(comma_list $(mdts_nodes)) \
2604                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
2605         echo content > $DIR1/$tdir/$tfile & PID1=$!
2606         pdo_sched
2607         multiop $DIR2/$tdir/$tfile u & PID2=$!
2608
2609         wait $PID1 ||
2610                 { ret=$?; \
2611                 echo -n "overwriting $tfile should succeed (err=$ret); "; }
2612         wait $PID2 ||
2613                 { ret=$?; \
2614                 echo -n "unlinking $tfile should succeed (err=$ret);"; }
2615
2616         #Clean
2617         do_nodes $(comma_list $(mdts_nodes)) \
2618                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
2619         rm -rf $DIR/$tdir
2620
2621         return $ret
2622 }
2623
2624 test_43k() {
2625         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
2626                 skip "Need MDS version newer than 2.13.56"
2627         local msg fail_loc
2628
2629 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
2630 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
2631         for fail_loc in "0x80000169" "0x8000016a"; do
2632                 echo "Begin 100 tests with fail_loc=$fail_loc"
2633                 printf "Progress: "
2634                 for i in {1..100}; do
2635                         printf "*"
2636                         msg=$(sub_test_43k "$fail_loc") ||
2637                                 { echo; error "iter=$i : $msg"; }
2638                 done
2639                 echo
2640         done
2641
2642         #Clean
2643         reset_fail_loc
2644
2645         return 0
2646 }
2647 run_test 43k "unlink vs create"
2648
2649 # test 44: rename tgt and blocking operations
2650 test_44a() {
2651         pdo_lru_clear
2652         touch $DIR1/$tfile-2
2653 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
2654         do_nodes $(comma_list $(mdts_nodes)) \
2655                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2656         mv $DIR1/$tfile-2 $DIR1/$tfile &
2657         PID1=$! ; pdo_sched
2658         mkdir $DIR2/$tfile &
2659         PID2=$! ; pdo_sched
2660         do_nodes $(comma_list $(mdts_nodes)) \
2661                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2662         check_pdo_conflict $PID1 && { wait $PID1; date;error "mkdir isn't blocked"; }
2663         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
2664         date
2665         rm -rf $DIR/$tfile*
2666         return 0
2667 }
2668 run_test 44a "pdirops: rename tgt vs mkdir =============="
2669
2670 test_44b() {
2671         pdo_lru_clear
2672         touch $DIR1/$tfile-2
2673 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2674         do_nodes $(comma_list $(mdts_nodes)) \
2675                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2676         mv $DIR1/$tfile-2 $DIR1/$tfile &
2677         PID1=$! ; pdo_sched
2678         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2679         PID2=$! ; pdo_sched
2680         do_nodes $(comma_list $(mdts_nodes)) \
2681                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2682         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2683         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
2684         rm -rf $DIR/$tfile*
2685         return 0
2686 }
2687 run_test 44b "pdirops: rename tgt vs create =============="
2688
2689 test_44c() {
2690         pdo_lru_clear
2691         touch $DIR1/$tfile-2
2692         touch $DIR1/$tfile-3
2693 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2694         do_nodes $(comma_list $(mdts_nodes)) \
2695                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2696         mv $DIR1/$tfile-2 $DIR1/$tfile &
2697         PID1=$! ; pdo_sched
2698         link $DIR2/$tfile-3 $DIR2/$tfile &
2699         PID2=$! ; pdo_sched
2700         do_nodes $(comma_list $(mdts_nodes)) \
2701                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2702         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2703         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
2704         rm -rf $DIR/$tfile*
2705         return 0
2706 }
2707 run_test 44c "pdirops: rename tgt vs link =============="
2708
2709 test_44d() {
2710         pdo_lru_clear
2711         touch $DIR1/$tfile-2
2712 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2713         do_nodes $(comma_list $(mdts_nodes)) \
2714                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2715         mv $DIR1/$tfile-2 $DIR1/$tfile &
2716         PID1=$! ; pdo_sched
2717         rm $DIR2/$tfile &
2718         PID2=$! ; pdo_sched
2719         do_nodes $(comma_list $(mdts_nodes)) \
2720                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2721         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2722         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
2723         rm -rf $DIR/$tfile*
2724         return 0
2725 }
2726 run_test 44d "pdirops: rename tgt vs unlink =============="
2727
2728 test_44e() {
2729         pdo_lru_clear
2730         touch $DIR1/$tfile
2731         touch $DIR1/$tfile-2
2732         touch $DIR1/$tfile-3
2733 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2734         do_nodes $(comma_list $(mdts_nodes)) \
2735                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2736         mv $DIR1/$tfile-2 $DIR1/$tfile &
2737         PID1=$! ; pdo_sched
2738         mv $DIR2/$tfile-3 $DIR2/$tfile &
2739         PID2=$! ; pdo_sched
2740         do_nodes $(comma_list $(mdts_nodes)) \
2741                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2742         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2743         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2744         rm -rf $DIR/$tfile*
2745         return 0
2746 }
2747 run_test 44e "pdirops: rename tgt and rename (tgt) =============="
2748
2749 test_44f() {
2750         pdo_lru_clear
2751         touch $DIR1/$tfile-2
2752         touch $DIR1/$tfile-3
2753 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2754         do_nodes $(comma_list $(mdts_nodes)) \
2755                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2756         mv $DIR1/$tfile-2 $DIR1/$tfile &
2757         PID1=$! ; pdo_sched
2758         mv $DIR2/$tfile $DIR2/$tfile-3 &
2759         PID2=$! ; pdo_sched
2760         do_nodes $(comma_list $(mdts_nodes)) \
2761                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2762         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2763         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2764         rm -rf $DIR/$tfile*
2765         return 0
2766 }
2767 run_test 44f "pdirops: rename tgt and rename (src) =============="
2768
2769 test_44g() {
2770         pdo_lru_clear
2771         touch $DIR1/$tfile-2
2772 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2773         do_nodes $(comma_list $(mdts_nodes)) \
2774                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2775         mv $DIR1/$tfile-2 $DIR1/$tfile &
2776         PID1=$! ; pdo_sched
2777         stat $DIR2/$tfile > /dev/null &
2778         PID2=$! ; pdo_sched
2779         do_nodes $(comma_list $(mdts_nodes)) \
2780                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2781         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2782         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
2783         rm -rf $DIR/$tfile*
2784         return 0
2785 }
2786 run_test 44g "pdirops: rename tgt vs getattr =============="
2787
2788 test_44h() {
2789         pdo_lru_clear
2790         touch $DIR1/$tfile-2
2791 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2    0x146
2792         do_nodes $(comma_list $(mdts_nodes)) \
2793                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2794         mv $DIR1/$tfile-2 $DIR1/$tfile &
2795         PID1=$! ; pdo_sched
2796         ls -lia $DIR2/ > /dev/null &
2797         PID2=$! ; pdo_sched
2798         do_nodes $(comma_list $(mdts_nodes)) \
2799                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2800         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2801         wait $PID2
2802         rm -rf $DIR/$tfile*
2803         return 0
2804 }
2805 run_test 44h "pdirops: rename tgt vs readdir =============="
2806
2807 # test 44: rename tgt and blocking operations
2808 test_44i() {
2809         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2810         pdo_lru_clear
2811         touch $DIR1/$tfile-2
2812 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK2   0x146
2813         do_nodes $(comma_list $(mdts_nodes)) \
2814                 "lctl set_param -n fail_loc=0x80000146 2>/dev/null || true"
2815         mv $DIR1/$tfile-2 $DIR1/$tfile &
2816         PID1=$! ; pdo_sched
2817         $LFS mkdir -i 1 $DIR2/$tfile &
2818         PID2=$! ; pdo_sched
2819         do_nodes $(comma_list $(mdts_nodes)) \
2820                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2821         check_pdo_conflict $PID1 && { wait $PID1;
2822                                 error "remote mkdir isn't blocked"; }
2823         wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
2824         rm -rf $DIR/$tfile*
2825         return 0
2826 }
2827 run_test 44i "pdirops: rename tgt vs remote mkdir"
2828
2829 # test 45: rename,mkdir doesn't fail with -EEXIST
2830 test_45a() {
2831         for i in {1..1000}; do
2832                 mkdir $DIR1/$tdir || error "mkdir $tdir failed"
2833                 mrename $DIR2/$tdir $DIR2/$tdir.$i > /dev/null ||
2834                         error "mrename to $tdir.$i failed"
2835         done
2836         rm -rf $DIR/$tdir*
2837         return 0
2838 }
2839 run_test 45a "rename,mkdir doesn't return -EEXIST =============="
2840
2841 test_45b() {
2842         pdo_lru_clear
2843         touch $DIR1/$tfile
2844 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2845         do_nodes $(comma_list $(mdts_nodes)) \
2846                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2847         mv $DIR1/$tfile $DIR1/$tfile-2 &
2848         PID1=$! ; pdo_sched
2849         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
2850         PID2=$! ; pdo_sched
2851         do_nodes $(comma_list $(mdts_nodes)) \
2852                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2853         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
2854         wait $PID2 ; [ $? -eq 0 ] || error "create must succeed"
2855         rm -rf $DIR/$tfile*
2856         return 0
2857 }
2858 run_test 45b "pdirops: rename src vs create =============="
2859
2860 test_45c() {
2861         pdo_lru_clear
2862         touch $DIR1/$tfile
2863         touch $DIR1/$tfile-3
2864 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2865         do_nodes $(comma_list $(mdts_nodes)) \
2866                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2867         mv $DIR1/$tfile $DIR1/$tfile-2 &
2868         PID1=$! ; pdo_sched
2869         link $DIR2/$tfile-3 $DIR2/$tfile &
2870         PID2=$! ; pdo_sched
2871         do_nodes $(comma_list $(mdts_nodes)) \
2872                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2873         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
2874         wait $PID2 ; [ $? -eq 0 ] || error "link must succeed"
2875         rm -rf $DIR/$tfile*
2876         return 0
2877 }
2878 run_test 45c "pdirops: rename src vs link =============="
2879
2880 test_45d() {
2881         pdo_lru_clear
2882         touch $DIR1/$tfile
2883 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2884         do_nodes $(comma_list $(mdts_nodes)) \
2885                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2886         mv $DIR1/$tfile $DIR1/$tfile-2 &
2887         PID1=$! ; pdo_sched
2888         rm $DIR2/$tfile &
2889         PID2=$! ; pdo_sched
2890         do_nodes $(comma_list $(mdts_nodes)) \
2891                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2892         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
2893         wait $PID2 ; [ $? -ne 0 ] || error "unlink must fail"
2894         rm -rf $DIR/$tfile*
2895         return 0
2896 }
2897 run_test 45d "pdirops: rename src vs unlink =============="
2898
2899 test_45e() {
2900         pdo_lru_clear
2901         touch $DIR1/$tfile
2902         touch $DIR1/$tfile-3
2903 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2904         do_nodes $(comma_list $(mdts_nodes)) \
2905                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2906         mv $DIR1/$tfile $DIR1/$tfile-2 &
2907         PID1=$! ; pdo_sched
2908         mv $DIR2/$tfile-3 $DIR2/$tfile &
2909         PID2=$! ; pdo_sched
2910         do_nodes $(comma_list $(mdts_nodes)) \
2911                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2912         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2913         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
2914         rm -rf $DIR/$tfile*
2915         return 0
2916 }
2917 run_test 45e "pdirops: rename src and rename (tgt) =============="
2918
2919 test_45f() {
2920         pdo_lru_clear
2921         touch $DIR1/$tfile
2922 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2923         do_nodes $(comma_list $(mdts_nodes)) \
2924                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2925         mv $DIR1/$tfile $DIR1/$tfile-2 &
2926         PID1=$! ; pdo_sched
2927         mv $DIR2/$tfile $DIR2/$tfile-3 &
2928         PID2=$! ; pdo_sched
2929         do_nodes $(comma_list $(mdts_nodes)) \
2930                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2931         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
2932         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
2933         rm -rf $DIR/$tfile*
2934         return 0
2935 }
2936 run_test 45f "pdirops: rename src and rename (src) =============="
2937
2938 test_45g() {
2939         pdo_lru_clear
2940         touch $DIR1/$tfile
2941 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2942         do_nodes $(comma_list $(mdts_nodes)) \
2943                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2944         mv $DIR1/$tfile $DIR1/$tfile-2 &
2945         PID1=$! ; pdo_sched
2946         stat $DIR2/$tfile > /dev/null &
2947         PID2=$! ; pdo_sched
2948         do_nodes $(comma_list $(mdts_nodes)) \
2949                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2950         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
2951         wait $PID2 ; [ $? -ne 0 ] || error "stat must fail"
2952         rm -rf $DIR/$tfile*
2953         return 0
2954 }
2955 run_test 45g "pdirops: rename src vs getattr =============="
2956
2957 test_45h() {
2958         pdo_lru_clear
2959         touch $DIR1/$tfile
2960 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2961         do_nodes $(comma_list $(mdts_nodes)) \
2962                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2963         mv $DIR1/$tfile $DIR1/$tfile-2 &
2964         PID1=$! ; pdo_sched
2965         ls -lia $DIR2/ > /dev/null &
2966         do_nodes $(comma_list $(mdts_nodes)) \
2967                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2968         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
2969         wait $PID2
2970         rm -rf $DIR/$tfile*
2971         return 0
2972 }
2973 run_test 45h "pdirops: unlink vs readdir =============="
2974
2975 test_45i() {
2976         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2977         pdo_lru_clear
2978         touch $DIR1/$tfile
2979 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
2980         do_nodes $(comma_list $(mdts_nodes)) \
2981                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
2982         mv $DIR1/$tfile $DIR1/$tfile-2 &
2983         PID1=$! ; pdo_sched
2984         $LFS mkdir -i 1 $DIR2/$tfile &
2985         PID2=$! ; pdo_sched
2986         do_nodes $(comma_list $(mdts_nodes)) \
2987                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
2988         check_pdo_conflict $PID1 && { wait $PID1;
2989                                 error "create remote dir isn't blocked"; }
2990         wait $PID2 ; [ $? -eq 0 ] || error "create remote dir must succeed"
2991         rm -rf $DIR/$tfile*
2992         return 0
2993 }
2994 run_test 45i "pdirops: rename src vs remote mkdir"
2995
2996 sub_test_45j() {
2997         local PID1 PID2
2998         local fail_loc="$1"
2999         local ret=0
3000
3001         # We test in a sparate directory to be able to unblock server thread in
3002         # cfs_race if LCK_PW is taken on the parent by mdt_reint_rename.
3003         test_mkdir $DIR2/$tdir
3004         echo file1 > $DIR2/$tdir/$tfile
3005         echo file2 > $DIR2/$tdir/$tfile-2
3006         pdo_lru_clear
3007
3008         do_nodes $(comma_list $(mdts_nodes)) \
3009                 "lctl set_param -n fail_loc=${fail_loc} || true" &>/dev/null
3010
3011         cat $DIR1/$tdir/$tfile >/dev/null &
3012         PID1=$!
3013         pdo_sched
3014         mrename $DIR2/$tdir/$tfile-2 $DIR2/$tdir/$tfile > /dev/null &
3015         PID2=$!
3016
3017         wait $PID1 ||
3018                 { ret=$?; echo -n "cat $tfile should succeed (err=$ret); "; }
3019         wait $PID2 ||
3020                 { ret=$?; \
3021                 echo -n "mrename $tfile-2 to $tfile failed (err=$ret);"; }
3022
3023         #Clean
3024         do_nodes $(comma_list $(mdts_nodes)) \
3025                 "lctl set_param -n fail_loc=0x0 || true" &>/dev/null
3026         rm -rf $DIR/$tdir
3027
3028         return $ret
3029 }
3030
3031 test_45j() {
3032         (( $MDS1_VERSION >= $(version_code 2.13.56) )) ||
3033                 skip "Need MDS version newer than 2.13.56"
3034         local msg fail_loc
3035
3036 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN         0x169
3037 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_REINT_OPEN2        0x16a
3038         for fail_loc in "0x80000169" "0x8000016a"; do
3039                 echo "Begin 100 tests with fail_loc=$fail_loc"
3040                 printf "Progress: "
3041                 for i in {1..100}; do
3042                         printf "*"
3043                         msg=$(sub_test_45j "$fail_loc") ||
3044                                 { echo; error "iter=$i : $msg"; }
3045                 done
3046                 echo
3047         done
3048 }
3049 run_test 45j "read vs rename =============="
3050
3051 # test 46: link and blocking operations
3052 test_46a() {
3053         pdo_lru_clear
3054         touch $DIR1/$tfile-2
3055 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3056         do_nodes $(comma_list $(mdts_nodes)) \
3057                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3058         link $DIR1/$tfile-2 $DIR1/$tfile &
3059         PID1=$! ; pdo_sched
3060         mkdir $DIR2/$tfile &
3061         PID2=$! ; pdo_sched
3062         do_nodes $(comma_list $(mdts_nodes)) \
3063                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3064         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
3065         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
3066         rm -rf $DIR/$tfile*
3067         return 0
3068 }
3069 run_test 46a "pdirops: link vs mkdir =============="
3070
3071 test_46b() {
3072         pdo_lru_clear
3073         touch $DIR1/$tfile-2
3074 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3075         do_nodes $(comma_list $(mdts_nodes)) \
3076                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3077         link $DIR1/$tfile-2 $DIR1/$tfile &
3078         PID1=$! ; pdo_sched
3079         $MULTIOP $DIR2/$tfile oO_CREAT:O_EXCL:c &
3080         PID2=$! ; pdo_sched
3081         do_nodes $(comma_list $(mdts_nodes)) \
3082                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3083         check_pdo_conflict $PID1 && { wait $PID1; error "create isn't blocked"; }
3084         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
3085         rm -rf $DIR/$tfile*
3086         return 0
3087 }
3088 run_test 46b "pdirops: link vs create =============="
3089
3090 test_46c() {
3091         pdo_lru_clear
3092         touch $DIR1/$tfile-2
3093 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3094         do_nodes $(comma_list $(mdts_nodes)) \
3095                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3096         link $DIR1/$tfile-2 $DIR1/$tfile &
3097         PID1=$! ; pdo_sched
3098         link $DIR2/$tfile $DIR2/$tfile &
3099         PID2=$! ; pdo_sched
3100         do_nodes $(comma_list $(mdts_nodes)) \
3101                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3102         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
3103         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
3104         rm -rf $DIR/$tfile*
3105         return 0
3106 }
3107 run_test 46c "pdirops: link vs link =============="
3108
3109 test_46d() {
3110         pdo_lru_clear
3111         touch $DIR1/$tfile-2
3112 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3113         do_nodes $(comma_list $(mdts_nodes)) \
3114                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3115         link $DIR1/$tfile-2 $DIR1/$tfile &
3116         PID1=$! ; pdo_sched
3117         rm $DIR2/$tfile &
3118         PID2=$! ; pdo_sched
3119         do_nodes $(comma_list $(mdts_nodes)) \
3120                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3121         check_pdo_conflict $PID1 && { wait $PID1; error "unlink isn't blocked"; }
3122         wait $PID2 ; [ $? -eq 0 ] || error "unlink must succeed"
3123         rm -rf $DIR/$tfile*
3124         return 0
3125 }
3126 run_test 46d "pdirops: link vs unlink =============="
3127
3128 test_46e() {
3129         pdo_lru_clear
3130         touch $DIR1/$tfile-2
3131         touch $DIR1/$tfile-3
3132 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3133         do_nodes $(comma_list $(mdts_nodes)) \
3134                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3135         link $DIR1/$tfile-2 $DIR1/$tfile &
3136         PID1=$! ; pdo_sched
3137         mv $DIR2/$tfile-3 $DIR2/$tfile &
3138         PID2=$! ; pdo_sched
3139         do_nodes $(comma_list $(mdts_nodes)) \
3140                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3141         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
3142         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
3143         rm -rf $DIR/$tfile*
3144         return 0
3145 }
3146 run_test 46e "pdirops: link and rename (tgt) =============="
3147
3148 test_46f() {
3149         pdo_lru_clear
3150         touch $DIR1/$tfile-2
3151         touch $DIR1/$tfile-3
3152 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3153         do_nodes $(comma_list $(mdts_nodes)) \
3154                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3155         link $DIR1/$tfile-2 $DIR1/$tfile &
3156         PID1=$! ; pdo_sched
3157         mv $DIR2/$tfile $DIR2/$tfile-3 &
3158         PID2=$! ; pdo_sched
3159         do_nodes $(comma_list $(mdts_nodes)) \
3160                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3161         check_pdo_conflict $PID1 && { wait $PID1; error "rename isn't blocked"; }
3162         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
3163         rm -rf $DIR/$tfile*
3164         return 0
3165 }
3166 run_test 46f "pdirops: link and rename (src) =============="
3167
3168 test_46g() {
3169         pdo_lru_clear
3170         touch $DIR1/$tfile-2
3171 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3172         do_nodes $(comma_list $(mdts_nodes)) \
3173                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3174         link $DIR1/$tfile-2 $DIR1/$tfile &
3175         PID1=$! ; pdo_sched
3176         stat $DIR2/$tfile > /dev/null &
3177         PID2=$! ; pdo_sched
3178         do_nodes $(comma_list $(mdts_nodes)) \
3179                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3180         check_pdo_conflict $PID1 && { wait $PID1; error "getattr isn't blocked"; }
3181         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
3182         rm -rf $DIR/$tfile*
3183         return 0
3184 }
3185 run_test 46g "pdirops: link vs getattr =============="
3186
3187 test_46h() {
3188         pdo_lru_clear
3189         touch $DIR1/$tfile-2
3190 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3191         do_nodes $(comma_list $(mdts_nodes)) \
3192                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3193         link $DIR1/$tfile-2 $DIR1/$tfile &
3194         PID1=$! ; pdo_sched
3195         ls -lia $DIR2/ > /dev/null &
3196         PID2=$! ; pdo_sched
3197         do_nodes $(comma_list $(mdts_nodes)) \
3198                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3199         check_pdo_conflict $PID1 && { wait $PID1; error "readdir isn't blocked"; }
3200         wait $PID2
3201         rm -rf $DIR/$tfile*
3202         return 0
3203 }
3204 run_test 46h "pdirops: link vs readdir =============="
3205
3206 test_46i() {
3207         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3208         pdo_lru_clear
3209         touch $DIR1/$tfile-2
3210 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3211         do_nodes $(comma_list $(mdts_nodes)) \
3212                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3213         link $DIR1/$tfile-2 $DIR1/$tfile &
3214         PID1=$! ; pdo_sched
3215         $LFS mkdir -i 1 $DIR2/$tfile &
3216         PID2=$! ; pdo_sched
3217         do_nodes $(comma_list $(mdts_nodes)) \
3218                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3219         check_pdo_conflict $PID1 && { wait $PID1;
3220                                 error "remote mkdir isn't blocked"; }
3221         wait $PID2 ; [ $? -ne 0 ] || error "remote mkdir must fail"
3222         rm -rf $DIR/$tfile*
3223         return 0
3224 }
3225 run_test 46i "pdirops: link vs remote mkdir"
3226
3227 # test 47: remote mkdir and blocking operations
3228 test_47a() {
3229 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3230         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3231         pdo_lru_clear
3232         do_nodes $(comma_list $(mdts_nodes)) \
3233                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3234         $LFS mkdir -i 1 $DIR1/$tfile &
3235         PID1=$! ; pdo_sched
3236         mkdir $DIR2/$tfile &
3237         PID2=$! ; pdo_sched
3238         do_nodes $(comma_list $(mdts_nodes)) \
3239                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3240         check_pdo_conflict $PID1 && { wait $PID1; error "mkdir isn't blocked"; }
3241         wait $PID2 ; [ $? -ne 0 ] || error "mkdir must fail"
3242         rm -rf $DIR/$tfile*
3243         return 0
3244 }
3245 run_test 47a "pdirops: remote mkdir vs mkdir"
3246
3247 test_47b() {
3248 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3249         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3250         pdo_lru_clear
3251         do_nodes $(comma_list $(mdts_nodes)) \
3252                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3253         $LFS mkdir -i 1 $DIR1/$tfile &
3254         PID1=$! ; pdo_sched
3255         sleep 1 # please do not remove this sleep, see LU-10754
3256         multiop $DIR2/$tfile oO_CREAT:O_EXCL:c &
3257         PID2=$! ; pdo_sched
3258         do_nodes $(comma_list $(mdts_nodes)) \
3259                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3260         check_pdo_conflict $PID1 && { wait $PID1;
3261                                         error "create isn't blocked"; }
3262         wait $PID2 ; [ $? -ne 0 ] || error "create must fail"
3263         rm -rf $DIR/$tfile*
3264         return 0
3265 }
3266 run_test 47b "pdirops: remote mkdir vs create"
3267
3268 test_47c() {
3269         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3270         pdo_lru_clear
3271         touch $DIR1/$tfile-2
3272 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3273         do_nodes $(comma_list $(mdts_nodes)) \
3274                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3275         $LFS mkdir -i 1 $DIR1/$tfile &
3276         PID1=$! ; pdo_sched
3277         link $DIR2/$tfile-2 $DIR2/$tfile &
3278         PID2=$! ; pdo_sched
3279         do_nodes $(comma_list $(mdts_nodes)) \
3280                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3281         check_pdo_conflict $PID1 && { wait $PID1; error "link isn't blocked"; }
3282         wait $PID2 ; [ $? -ne 0 ] || error "link must fail"
3283         rm -rf $DIR/$tfile*
3284         return 0
3285 }
3286 run_test 47c "pdirops: remote mkdir vs link"
3287
3288 test_47d() {
3289         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3290         pdo_lru_clear
3291 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3292         do_nodes $(comma_list $(mdts_nodes)) \
3293                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3294         $LFS mkdir -i 1 $DIR1/$tfile &
3295         PID1=$! ; pdo_sched
3296         rmdir $DIR2/$tfile &
3297         PID2=$! ; pdo_sched
3298         do_nodes $(comma_list $(mdts_nodes)) \
3299                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3300         check_pdo_conflict $PID1 && { wait $PID1;
3301                                         error "unlink isn't blocked"; }
3302         wait $PID2 ; [ $? -eq 0 ] || error "rmdir must succeed"
3303         rm -rf $DIR/$tfile*
3304         return 0
3305 }
3306 run_test 47d "pdirops: remote mkdir vs unlink"
3307
3308 test_47e() {
3309         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3310         pdo_lru_clear
3311         touch $DIR1/$tfile-2
3312 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3313         do_nodes $(comma_list $(mdts_nodes)) \
3314                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3315         $LFS mkdir -i 1 $DIR1/$tfile &
3316         PID1=$! ; pdo_sched
3317         mv -T $DIR2/$tfile-2 $DIR2/$tfile &
3318         PID2=$! ; pdo_sched
3319         do_nodes $(comma_list $(mdts_nodes)) \
3320                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3321         check_pdo_conflict $PID1 && { wait $PID1;
3322                                         error "rename isn't blocked"; }
3323         wait $PID2 ; [ $? -ne 0 ] || error "rename must fail"
3324         rm -rf $DIR/$tfile*
3325         return 0
3326 }
3327 run_test 47e "pdirops: remote mkdir and rename (tgt)"
3328
3329 test_47f() {
3330         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3331         pdo_lru_clear
3332 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3333         do_nodes $(comma_list $(mdts_nodes)) \
3334                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3335         $LFS mkdir -i 1 $DIR1/$tfile &
3336         PID1=$! ; pdo_sched
3337         mv $DIR2/$tfile $DIR2/$tfile-2 &
3338         PID2=$! ; pdo_sched
3339         do_nodes $(comma_list $(mdts_nodes)) \
3340                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3341         check_pdo_conflict $PID1 && { wait $PID1;
3342                                         error "rename isn't blocked"; }
3343         wait $PID2 ; [ $? -eq 0 ] || error "rename must succeed"
3344         rm -rf $DIR/$tfile*
3345         return 0
3346 }
3347 run_test 47f "pdirops: remote mkdir and rename (src)"
3348
3349 test_47g() {
3350         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3351         sync
3352         sync_all_data
3353         pdo_lru_clear
3354 #define CFS_FAIL_ONCE|OBD_FAIL_MDS_PDO_LOCK    0x145
3355         do_nodes $(comma_list $(mdts_nodes)) \
3356                 "lctl set_param -n fail_loc=0x80000145 2>/dev/null || true"
3357         $LFS mkdir -i 1 $DIR1/$tfile &
3358         PID1=$! ; pdo_sched
3359         stat $DIR2/$tfile > /dev/null &
3360         PID2=$! ; pdo_sched
3361         do_nodes $(comma_list $(mdts_nodes)) \
3362                 "lctl set_param -n fail_loc=0 2>/dev/null || true"
3363         check_pdo_conflict $PID1 && { wait $PID1;
3364                                         error "getattr isn't blocked"; }
3365         wait $PID2 ; [ $? -eq 0 ] || error "stat must succeed"
3366         rm -rf $DIR/$tfile*
3367         return 0
3368 }
3369 run_test 47g "pdirops: remote mkdir vs getattr"
3370
3371 test_50() {
3372         trunc_size=4096
3373         dd if=/dev/zero of=$DIR1/$tfile bs=1K count=10
3374 #define OBD_FAIL_OSC_CP_ENQ_RACE         0x410
3375         do_facet client "lctl set_param fail_loc=0x410"
3376         $TRUNCATE $DIR2/$tfile $trunc_size
3377         do_facet client "lctl set_param fail_loc=0x0"
3378         sleep 3
3379         size=`stat -c %s $DIR2/$tfile`
3380         [ $size -eq $trunc_size ] || error "wrong size"
3381 }
3382 run_test 50 "osc lvb attrs: enqueue vs. CP AST =============="
3383
3384 test_51a() {
3385         local filesize
3386         local origfile=/etc/hosts
3387
3388         filesize=$(stat -c %s $origfile)
3389
3390         # create an empty file
3391         $MCREATE $DIR1/$tfile || error "can't create $DIR1/$tfile"
3392         # cache layout lock on both mount point
3393         stat $DIR1/$tfile > /dev/null || error "stat $DIR1/$tfile failed"
3394         stat $DIR2/$tfile > /dev/null || error "stat $DIR2/$tfile failed"
3395
3396         # open and sleep 2 seconds then read
3397         $MULTIOP $DIR2/$tfile o_2r${filesize}c &
3398         local pid=$!
3399         sleep 1
3400
3401         # create the layout of testing file
3402         dd if=$origfile of=$DIR1/$tfile conv=notrunc > /dev/null ||
3403                 error "dd $DIR1/$tfile failed"
3404
3405         # MULTIOP proc should be able to read enough bytes and exit
3406         for ((i = 0; i < 6; i++)); do
3407                 sleep 1
3408                 kill -0 $pid || break
3409         done
3410         kill -0 $pid 2> /dev/null && error "multiop is still there"
3411         cmp $origfile $DIR2/$tfile || error "$origfile and $DIR2/$tfile differs"
3412
3413         rm -f $DIR1/$tfile
3414 }
3415 run_test 51a "layout lock: refresh layout should work"
3416
3417 test_51b() {
3418         (( $MDS1_VERSION >= $(version_code 2.3.59) )) ||
3419                 skip "Need MDS version at least 2.3.59"
3420
3421         local tmpfile=`mktemp`
3422
3423         $LFS setstripe -E 1m -S 1M -c 1 -E -1 -c 1 $DIR1/$tfile ||
3424                 error "Create $DIR1/$tfile failed"
3425
3426         dd if=/dev/zero of=$DIR1/$tfile bs=1k count=1 conv=notrunc ||
3427                 error "dd $DIR1/$tfile failed"
3428
3429         # delay glimpse so that layout has changed when glimpse finish
3430 #define OBD_FAIL_GLIMPSE_DELAY 0x1404
3431         $LCTL set_param fail_loc=0x1404 fail_val=4
3432         stat -c %s $DIR2/$tfile |tee $tmpfile &
3433         local pid=$!
3434         sleep 0.2
3435
3436         # extend layout of testing file
3437         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=1 seek=2 conv=notrunc ||
3438                 error "dd $DIR1/$tfile failed"
3439
3440         wait $pid
3441         local fsize=$(cat $tmpfile)
3442
3443         [ x$fsize = x3145728 ] || error "file size is $fsize, should be 3145728"
3444
3445         rm -f $DIR1/$tfile $tmpfile
3446 }
3447 run_test 51b "layout lock: glimpse should be able to restart if layout changed"
3448
3449 test_51c() {
3450         [ $OSTCOUNT -ge 2 ] || { skip "needs >= 2 osts"; return; }
3451
3452         # set default layout to have 1 stripe
3453         mkdir $DIR1/$tdir
3454         $LFS setstripe -c 1 $DIR1/$tdir
3455
3456         # create a file with empty layout
3457         $MCREATE $DIR1/$tdir/$tfile ||
3458                 error "$MCREATE $DIR1/$tdir/$tfile failed"
3459
3460 #define OBD_FAIL_MDS_LL_BLOCK 0x172
3461         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x172
3462
3463         # change the layout of testing file
3464         echo "Setting layout to have $OSTCOUNT stripes ..."
3465         $LFS setstripe -c $OSTCOUNT $DIR1/$tdir/$tfile &
3466         pid=$!
3467         sleep 1
3468
3469         # write something to the file, it should be blocked on fetching layout
3470         dd if=/dev/zero of=$DIR2/$tdir/$tfile bs=1k count=1 conv=notrunc
3471         local stripe_count=$($LFS getstripe -c $DIR2/$tdir/$tfile)
3472         wait $pid
3473
3474         # lod_qos.c::min_stripe_count() allows setstripe with a default stripe
3475         # count to succeed with only 3/4 of the number of stripes (rounded up),
3476         # so creating striped files does not fail if an OST is offline or full
3477         [ $stripe_count -ge $((OSTCOUNT - $OSTCOUNT / 4)) ] ||
3478                 error "bad layout: getstripe -c $stripe_count < $OSTCOUNT * 3/4"
3479
3480         rm -fr $DIR1/$tdir
3481 }
3482 run_test 51c "layout lock: IT_LAYOUT blocked and correct layout can be returned"
3483
3484 test_51d() {
3485         dd if=/dev/zero of=/$DIR1/$tfile bs=1M count=1
3486         cancel_lru_locks mdc
3487
3488         # open should grant LAYOUT lock, mmap and read will install pages
3489         $MULTIOP $DIR1/$tfile oO_RDWR:SMR_Uc &
3490         local PID=$!
3491         sleep 1
3492
3493         # rss before revoking
3494         local br=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')
3495         echo "Before revoking layout lock: $br KB mapped"
3496
3497         # cancel layout lock manually
3498         cancel_lru_locks mdc
3499
3500         # rss after revoking
3501         local ar=$(grep -A 10 $tfile /proc/$PID/smaps | awk '/^Rss/{print $2}')
3502
3503         kill -USR1 $PID
3504         wait $PID || error "wait PID $PID failed"
3505
3506         [ $ar -eq 0 ] || error "rss before: $br, after $ar, some pages remained"
3507 }
3508 run_test 51d "layout lock: losing layout lock should clean up memory map region"
3509
3510 test_51e() {
3511         (( $MDS1_VERSION >= $(version_code 2.13.54.148) )) ||
3512                 skip "MDS version must be at least 2.13.54.148"
3513
3514         local pid
3515
3516         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
3517         pid=$!
3518         sleep 1
3519
3520         $LFS getstripe $DIR2/$tfile
3521         kill -USR1 $pid
3522         wait $pid || error "multiop failed"
3523
3524         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
3525         pid=$!
3526         sleep 1
3527
3528         $LFS getstripe $DIR2/$tfile
3529         kill -USR1 $pid
3530         wait $pid || error "multiop failed"
3531 }
3532 run_test 51e "lfs getstripe does not break leases, part 2"
3533
3534 test_54_part1()
3535 {
3536         echo "==> rename vs getattr vs setxattr should not deadlock"
3537         mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"
3538
3539         do_facet mds1 $LCTL set_param fail_loc=$1
3540
3541         mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
3542         PID1=$!
3543         sleep 1
3544
3545         stat $DIR/d1/d2 &
3546         PID2=$!
3547         sleep 1
3548
3549         setfattr -n user.attr1 -v value1 $DIR2/d1 || error "(2) setfattr failed"
3550         wait $PID1 || error "(3) mv failed"
3551         wait $PID2 || error "(4) stat failed"
3552         echo
3553
3554         rm -rf $DIR/d1
3555 }
3556
3557 test_54_part2() {
3558         echo "==> rename vs getattr vs open vs getattr should not deadlock"
3559         mkdir -p $DIR/d1/d2/d3 || error "(1) mkdir failed"
3560
3561         do_facet mds1 $LCTL set_param fail_loc=$1
3562
3563         mv -T $DIR/d1/d2/d3 $DIR/d1/d3 &
3564         PID1=$!
3565         sleep 1
3566
3567         stat $DIR/d1/d2 &
3568         PID2=$!
3569         sleep 1
3570
3571         $MULTIOP $DIR2/d1/d2 Oc &
3572         PID3=$!
3573         sleep 1
3574
3575         stat $DIR/d1 || error "(2) stat failed"
3576
3577         wait $PID1 || error "(3) mv failed"
3578         wait $PID2 || error "(4) stat failed"
3579         wait $PID3 && error "(5) multiop failed"
3580         echo
3581         rm -rf $DIR/d1
3582 }
3583
3584 test_54() {
3585         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
3586         save_lustre_params client "llite.*.xattr_cache" > $p
3587         lctl set_param llite.*.xattr_cache 1 ||
3588                 { skip "xattr cache is not supported"; return 0; }
3589
3590 #define OBD_FAIL_MDS_RENAME              0x153
3591 #define OBD_FAIL_MDS_RENAME2             0x154
3592         test_54_part1 0x80000153 || error 10
3593         test_54_part1 0x80000154 || error 11
3594         test_54_part2 0x80000153 || error 12
3595         test_54_part2 0x80000154 || error 13
3596
3597         restore_lustre_params < $p
3598         rm -f $p
3599 }
3600 run_test 54 "rename locking"
3601
3602 test_55a() {
3603         mkdir_on_mdt0 $DIR/$tdir
3604         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3605
3606 #define OBD_FAIL_MDS_RENAME4              0x156
3607         do_facet mds1 $LCTL set_param fail_loc=0x80000156
3608
3609         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3610         PID1=$!
3611         sleep 1
3612
3613         rm -r $DIR2/$tdir/d3
3614         wait $PID1 && error "(2) mv succeeded"
3615
3616         rm -rf $DIR/$tdir
3617 }
3618 run_test 55a "rename vs unlink target dir"
3619
3620 test_55b()
3621 {
3622         mkdir_on_mdt0 $DIR/$tdir
3623         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3624
3625 #define OBD_FAIL_MDS_RENAME4             0x156
3626         do_facet mds1 $LCTL set_param fail_loc=0x80000156
3627
3628         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3629         PID1=$!
3630         sleep 1
3631
3632         rm -r $DIR2/$tdir/d1
3633         wait $PID1 && error "(2) mv succeeded"
3634
3635         rm -rf $DIR/$tdir
3636 }
3637 run_test 55b "rename vs unlink source dir"
3638
3639 test_55c()
3640 {
3641         mkdir_on_mdt0 $DIR/$tdir
3642         mkdir -p $DIR/$tdir/d1/d2 $DIR/$tdir/d3 || error "(1) mkdir failed"
3643
3644 #define OBD_FAIL_MDS_RENAME4              0x156
3645         do_facet mds1 $LCTL set_param fail_loc=0x156
3646
3647         mv -T $DIR/$tdir/d1/d2 $DIR/$tdir/d3/d2 &
3648         PID1=$!
3649         sleep 1
3650
3651         # while rename is sleeping, open and remove d3
3652         $MULTIOP $DIR2/$tdir/d3 D_c &
3653         PID2=$!
3654         sleep 1
3655         rm -rf $DIR2/$tdir/d3
3656         sleep 5
3657
3658         # while rename is sleeping 2nd time, close d3
3659         kill -USR1 $PID2
3660         wait $PID2 || error "(3) multiop failed"
3661
3662         wait $PID1 && error "(2) mv succeeded"
3663
3664         rm -rf $DIR/$tdir
3665 }
3666 run_test 55c "rename vs unlink orphan target dir"
3667
3668 test_55d()
3669 {
3670         mkdir_on_mdt0 $DIR/$tdir
3671
3672         touch $DIR/$tdir/f1
3673
3674 #define OBD_FAIL_MDS_RENAME3              0x155
3675         do_facet mds1 $LCTL set_param fail_loc=0x155
3676         mv $DIR/$tdir/f1 $DIR/$tdir/$tdir &
3677         PID1=$!
3678         sleep 2
3679
3680         # while rename is sleeping, create $tdir, but as a directory
3681         mkdir -p $DIR2/$tdir/$tdir || error "(1) mkdir failed"
3682
3683         # link in reverse locking order
3684         ln $DIR2/$tdir/f1 $DIR2/$tdir/$tdir/f1 || error "(2) ln failed"
3685
3686         ! wait $PID1 || error "(3) mv succeeded"
3687         rm -rf $DIR/$tdir
3688 }
3689 run_test 55d "rename file vs link"
3690
3691 test_56a() {
3692         $LFS setstripe -c 1 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
3693         stack_trap "rm -f $MOUNT/$tfile"
3694         echo "run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k"
3695         run_llverdev $MOUNT/$tfile -p -s $((16000000)) -c 4k ||
3696                 error "llverdev failed with rc=$?"
3697 }
3698 run_test 56a "test llverdev with single large stripe"
3699
3700 test_56b() {
3701         $LFS setstripe -C 2000 $MOUNT/$tfile || error "creating $MOUNT/$tfile"
3702         stack_trap "rm -f $MOUNT/$tfile"
3703         echo "run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k"
3704         run_llverdev $MOUNT/$tfile -p -s $((16000000 * OSTCOUNT)) -c 4k ||
3705                 error "llverdev failed with rc=$?"
3706 }
3707 run_test 56b "test llverdev and partial verify of wide stripe file"
3708
3709 test_60() {
3710         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
3711                 skip "MDS version must be >= 2.3.0"
3712
3713         # Create a file
3714         test_mkdir $DIR1/$tdir
3715         file1=$DIR1/$tdir/file
3716         file2=$DIR2/$tdir/file
3717
3718         echo orig > $file2 || error "Could not create $file2"
3719         version=$($LFS data_version $file1)
3720
3721         # Append data
3722         echo append >> $file2 || error "Could not append to $file2"
3723         version2=$($LFS data_version $file1)
3724         [ "$version" != "$version2" ] ||
3725             error "append did not change data version: $version"
3726
3727         # Overwrite data
3728         echo overwrite > $file2 || error "Could not overwrite $file2"
3729         version3=$($LFS data_version $file1)
3730         [ "$version2" != "$version3" ] ||
3731             error "overwrite did not change data version: $version2"
3732
3733         # Truncate before EOF
3734         $TRUNCATE $file2 3 || error "Could not truncate $file2"
3735         version4=$($LFS data_version $file1)
3736         [ "$version3" != "$version4" ] ||
3737             error "truncate did not change data version: $version3"
3738
3739         # Truncate after EOF
3740         $TRUNCATE $file2 123456 || error "Could not truncate $file2"
3741         version5=$($LFS data_version $file1)
3742         [ "$version4" != "$version5" ] ||
3743             error "truncate did not change data version: $version4"
3744
3745         # Chmod do not change version
3746         chmod 400 $file2 || error "Could not chmod 400 $file2"
3747         version6=$($LFS data_version $file1)
3748         [ "$version5" == "$version6" ] ||
3749             error "chmod should not change data version: $version5 != $version6"
3750
3751         # Chown do not change version
3752         chown $RUNAS_ID $file2 || error "Could not chown $RUNAS_ID $file2"
3753         version7=$($LFS data_version $file1)
3754         [ "$version5" == "$version7" ] ||
3755             error "chown should not change data version: $version5 != $version7"
3756 }
3757 run_test 60 "Verify data_version behaviour"
3758
3759 test_70a() {
3760         local test_dir=$tdir/test_dir
3761
3762         mkdir -p $DIR1/$tdir
3763         if [ $MDSCOUNT -ge 2 ]; then
3764                 local MDTIDX=1
3765                 $LFS mkdir -i $MDTIDX $DIR1/$test_dir ||
3766                         error "Create remote directory failed"
3767         else
3768                 mkdir -p $DIR1/$test_dir
3769         fi
3770         cd $DIR2/$test_dir || error "cd directory failed"
3771         rm -rf $DIR1/$test_dir || error "unlink directory failed"
3772
3773         cd $DIR2/$tdir || error "exit directory"
3774 }
3775 run_test 70a "cd directory && rm directory"
3776
3777 test_70b() { # LU-2781
3778         local i
3779         mkdir -p $DIR1/$tdir
3780
3781         touch $DIR1/$tdir/file
3782         for ((i = 0; i < 32; i++)); do
3783             $LFS rm_entry $DIR1/$tdir/non_existent_dir &>/dev/null
3784         done
3785         rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"
3786
3787         touch $DIR1/$tdir/file
3788         $LFS mkdir -i0 $DIR1/$tdir/test_dir
3789         $LFS rm_entry $DIR1/$tdir/test_dir &>/dev/null
3790         rm -rf $DIR1/$tdir/test_dir ||
3791                 error "cannot remove directory after rm_entry"
3792         rm $DIR1/$tdir/file || error "cannot remove file after rm_entry"
3793 }
3794 run_test 70b "remove files after calling rm_entry"
3795
3796 test_71a() {
3797         [[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
3798                 skip "Need MDS version at least 2.1.6"
3799
3800         # Patch not applied to 2.2 and 2.3 branches
3801         [[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
3802         [[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
3803                 skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"
3804
3805         checkfiemap --test ||
3806                 skip "checkfiemap not runnable: $?"
3807         # write data this way: hole - data - hole - data
3808         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
3809         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tfile) + 1)))" = \
3810                 "zfs" ] &&
3811                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
3812         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
3813         GET_STAT="lctl get_param -n ldlm.services.ldlm_cbd.stats"
3814         stat $DIR2/$tfile
3815         local can1=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3816         echo $can1
3817         checkfiemap $DIR2/$tfile 131072 ||
3818                 error "data is not flushed from client"
3819         local can2=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3820         echo $can2
3821
3822         # common case of "create file, copy file" on a single node
3823         # should not flush data from ost
3824         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=1 count=1
3825         dd if=/dev/urandom of=$DIR1/$tfile bs=64K seek=3 count=1
3826         stat $DIR1/$tfile
3827         local can3=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3828         echo $can3
3829         checkfiemap $DIR1/$tfile 131072 ||
3830         error 4
3831         local can4=$($GET_STAT | awk '/ldlm_bl_callback/ {print $2}')
3832         echo $can2
3833         [ $can3 -eq $can4 ] || error $((can2-can1)) "cancel RPC occured."
3834 }
3835 run_test 71a "correct file map just after write operation is finished"
3836
3837 test_71b() {
3838         [[ "$MDS1_VERSION" -lt $(version_code 2.1.6) ]] &&
3839                 skip "Need MDS version at least 2.1.6"
3840
3841         # Patch not applied to 2.2 and 2.3 branches
3842         [[ "$MDS1_VERSION" -ge $(version_code 2.2.0) ]] &&
3843         [[ "$MDS1_VERSION" -lt $(version_code 2.4.0) ]] &&
3844                 skip "Need MDS version earlier than 2.2.0 or at least 2.4.0"
3845         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
3846
3847         checkfiemap --test ||
3848                 skip "error $?: checkfiemap failed"
3849
3850         mkdir -p $DIR1/$tdir
3851
3852         $LFS setstripe -c -1 $DIR1/$tdir || error "setstripe failed"
3853         dd if=/dev/urandom of=$DIR1/$tdir/$tfile bs=64K count=1
3854         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR1/$tdir/$tfile) + 1)))" = \
3855                 "zfs" ] &&
3856                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return 0
3857         checkfiemap $DIR1/$tdir/$tfile 65536 || error "checkfiemap failed"
3858 }
3859 run_test 71b "check fiemap support for stripecount > 1"
3860
3861 _check_last_flag_with_filefrag()
3862 {
3863         local file=$1
3864         local count=$2
3865         local i
3866         local offset
3867
3868         echo "check last flag for file with $count extents"
3869         rm -f $file
3870         for ((i=0; i<$count; i++)); do
3871                 offset=$((i * 256))
3872                 dd if=/dev/zero of=$file bs=4K count=1 seek=$offset 2> /dev/null
3873         done
3874
3875         filefrag -s -v $file | grep "last" ||
3876                 error "test file with $i extents failed"
3877
3878         rm -f $file
3879 }
3880
3881 test_71c() {
3882         local file="$DIR1/$tdir/$tfile"
3883
3884         (( $CLIENT_VERSION >= $(version_code 2.15.57) )) ||
3885                 skip "Need client version >= 2.15.57"
3886         [ $(facet_fstype ost1) = "ldiskfs" ] ||
3887                 skip "support only ldiskfs ost"
3888         filefrag -V | grep wc ||
3889                 skip "need whamcloud version of e2fsprogs"
3890
3891         mkdir -p $DIR1/$tdir
3892
3893         # filefrag uses u64[2028] buffer to fetch fiemap. The number of extents
3894         # in the buffer is (8 * 2048 - 32) / 56 = 292. Test file with 291, 292
3895         # and 293 extents
3896         _check_last_flag_with_filefrag $file 291
3897         _check_last_flag_with_filefrag $file 292
3898         _check_last_flag_with_filefrag $file 293
3899 }
3900 run_test 71c "check FIEMAP_EXTENT_LAST flag with different extents number"
3901
3902 test_72() {
3903         local p="$TMP/sanityN-$TESTNAME.parameters"
3904         local tlink1
3905         local tlink2
3906         save_lustre_params client "llite.*.xattr_cache" > $p
3907         lctl set_param llite.*.xattr_cache 1 ||
3908                 { skip "xattr cache is not supported"; return 0; }
3909
3910         touch $DIR1/$tfile
3911         setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
3912                 error "setfattr1 failed"
3913         getfattr -n user.attr1 $DIR2/$tfile | grep value1 ||
3914                 error "getfattr1 failed"
3915         setfattr -n user.attr1 -v value2 $DIR2/$tfile ||
3916                 error "setfattr2 failed"
3917         getfattr -n user.attr1 $DIR1/$tfile | grep value2 ||
3918                 error "getfattr2 failed"
3919
3920         # check that trusted.link is consistent
3921         tlink1=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
3922         ln $DIR2/$tfile $DIR2/$tfile-2 || error "failed to link"
3923         tlink2=$(getfattr -n trusted.link $DIR1/$tfile | md5sum)
3924         echo "$tlink1 $tlink2"
3925         [ "$tlink1" = "$tlink2" ] && error "trusted.link should have changed!"
3926
3927         rm -f $DIR2/$tfile
3928
3929         restore_lustre_params < $p
3930         rm -f $p
3931 }
3932 run_test 72 "getxattr/setxattr cache should be consistent between nodes"
3933
3934 test_73() {
3935         local p="$TMP/sanityN-$TESTNAME.parameters"
3936         save_lustre_params client "llite.*.xattr_cache" > $p
3937         lctl set_param llite.*.xattr_cache 1 ||
3938                 { skip "xattr cache is not supported"; return 0; }
3939
3940         touch $DIR1/$tfile
3941         setfattr -n user.attr1 -v value1 $DIR1/$tfile ||
3942                 error "setfattr1 failed"
3943         getfattr -n user.attr1 $DIR2/$tfile || error "getfattr1 failed"
3944         getfattr -n user.attr1 $DIR1/$tfile || error "getfattr2 failed"
3945         clear_stats llite.*.stats
3946         # PR lock should be cached by now on both clients
3947         getfattr -n user.attr1 $DIR1/$tfile || error "getfattr3 failed"
3948         # 2 hits for getfattr(0)+getfattr(size)
3949         [ $(calc_stats llite.*.stats getxattr_hits) -eq 2 ] ||
3950                 error "not cached in $DIR1"
3951         getfattr -n user.attr1 $DIR2/$tfile || error "getfattr4 failed"
3952         # 4 hits for more getfattr(0)+getfattr(size)
3953         [ $(calc_stats llite.*.stats getxattr_hits) -eq 4 ] ||
3954                 error "not cached in $DIR2"
3955         rm -f $DIR2/$tfile
3956
3957         restore_lustre_params < $p
3958         rm -f $p
3959 }
3960 run_test 73 "getxattr should not cause xattr lock cancellation"
3961
3962 test_74() {
3963         [ "$MDS1_VERSION" -lt $(version_code 2.4.93) ] &&
3964                 skip "Need MDS version at least 2.4.93"
3965
3966         dd if=/dev/zero of=$DIR1/$tfile-1 bs=1K count=1
3967         dd if=/dev/zero of=$DIR1/$tfile-2 bs=1K count=1
3968         flocks_test 4 $DIR1/$tfile-1 $DIR2/$tfile-2
3969 }
3970 run_test 74 "flock deadlock: different mounts =============="
3971
3972 # LU-3889
3973 test_75() {
3974         $LFS setstripe -c 2 -S 1m -i 0 $DIR1/$tfile
3975         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=2
3976         cancel_lru_locks osc
3977
3978         dd of=$DIR1/$tfile if=/dev/zero bs=1M count=1 seek=1 conv=notrunc
3979         sync
3980
3981         # define OBD_FAIL_LDLM_ENQUEUE_HANG 0x31d
3982         $LCTL set_param fail_loc=0x31d
3983         stat -c %s $DIR1/$tfile &
3984         local pid=$!
3985         sleep 1
3986         kill -9 $pid
3987
3988         # For bad lock error handler we should ASSERT and got kernel panic here
3989         sleep 4
3990         $LCTL set_param fail_loc=0
3991 }
3992 run_test 75 "osc: upcall after unuse lock==================="
3993
3994 test_76() { #LU-946
3995         [[ "$MDS1_VERSION" -lt $(version_code 2.5.53) ]] &&
3996                 skip "Need MDS version at least 2.5.53"
3997
3998         remote_mds_nodsh && skip "remote MDS with nodsh"
3999         local fcount=$((MDSCOUNT * 256))
4000         declare -a fd_list
4001         declare -a fid_list
4002
4003         if remote_mds; then
4004                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
4005         else
4006                 nid="0@lo"
4007         fi
4008
4009         rm -rf $DIR/$tdir
4010         test_mkdir $DIR/$tdir
4011
4012         # drop all open locks and close any cached "open" files on the client
4013         cancel_lru_locks mdc
4014
4015         local open_fids_cmd="$LCTL get_param -n mdt.*.exports.'$nid'.open_files"
4016         local fid_list=($(do_nodes $(comma_list $(mdts_nodes)) $open_fids_cmd))
4017         local already=${#fid_list[@]}
4018         for (( i = 0; i < $already; i++ )) ; do
4019                 log "already open[$i]: $($LFS fid2path $DIR2 ${fid_list[i]})"
4020         done
4021
4022         echo -n "opening files: "
4023         ulimit -n $((fcount + 50))
4024         for ((i = 0; i < $fcount; i++)); do
4025                 touch $DIR/$tdir/f_$i
4026                 local fd=$(free_fd ${fd_list[i]})
4027                 local open_cmd="exec $fd<$DIR/$tdir/f_$i"
4028                 eval $open_cmd
4029
4030                 fd_list[i]=$fd
4031
4032                 (( $i % 32 == 0 )) && echo -n "."
4033         done
4034         echo
4035
4036         fid_list=($(do_nodes $(comma_list $(mdts_nodes)) $open_fids_cmd))
4037
4038         # Possible errors in openfiles FID list.
4039         # 1. Missing FIDs. Check 1
4040         # 2. Extra FIDs. Check 1
4041         # 3. Duplicated FID. Check 2
4042         # 4. Invalid FIDs. Check 2
4043         # 5. Valid FID, points to some other file. Check 3
4044
4045         # Check 1
4046         [ ${#fid_list[@]} -ne $((fcount + already)) ] &&
4047                 error "${#fid_list[@]} != $fcount (+$already old) open files"
4048
4049         echo -n "closing files: "
4050         for (( fd = 0, fid = 0; fd < $fcount; fd++, fid++ )) ; do
4051                 local close_cmd="exec ${fd_list[fd]}<&-"
4052                 eval $close_cmd
4053                 filename=$($LFS fid2path $DIR2 ${fid_list[fid]})
4054
4055                 while [[ ! "$filename" =~ "$DIR2/$tdir/f_" ]]; do
4056                         echo "skip old open file $filename"
4057                         ((fid++))
4058                         filename=$($LFS fid2path $DIR2 ${fid_list[fid]})
4059                 done
4060
4061                 # Check 2
4062                 rm --interactive=no $filename
4063                 [ $? -ne 0 ] &&
4064                         error "Nonexisting fid ${fid_list[fid]} listed."
4065                 (( $fd % 32 == 0 )) && echo -n "."
4066         done
4067         echo
4068
4069         # Check 3
4070         ls_op=$(ls $DIR2/$tdir | wc -l)
4071         [ $ls_op -ne 0 ] &&
4072                 error "Some openfiles are missing in lproc output"
4073
4074         rm -rf $DIR/$tdir
4075 }
4076 run_test 76 "Verify MDT open_files listing"
4077
4078 nrs_write_read() {
4079         local n=16
4080         local dir=$DIR/$tdir
4081         local myRUNAS="$1"
4082         local create_as="$2"
4083
4084         mkdir $dir || error "mkdir $dir failed"
4085         $LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
4086         chmod 777 $dir
4087
4088         if [[ -n "$create_as" ]]; then
4089                 do_nodes $CLIENTS $create_as "touch $dir/nrs_r_\$HOSTNAME;" ||
4090                         error "touch failed for $dir/nrs_r_*"
4091                 do_nodes $CLIENTS $create_as touch "$dir/nrs_w_\$HOSTNAME" ||
4092                         error "touch failed for $dir/nrs_w_*"
4093                 do_nodes $CLIENTS $create_as "chmod 777 $dir/nrs_*_\$HOSTNAME;" ||
4094                         error "chmod failed for $dir/nrs_*"
4095         fi
4096
4097         do_nodes $CLIENTS $myRUNAS \
4098                 dd if=/dev/zero of="$dir/nrs_r_\$HOSTNAME" bs=1M count=$n ||
4099                 error "dd at 0 on client failed (1)"
4100
4101         do_nodes $CLIENTS $myRUNAS \
4102                 "declare -a pids_w;
4103                 for ((i = 0; i < $n; i++)); do
4104                         dd if=/dev/zero of=$dir/nrs_w_\$HOSTNAME bs=1M \
4105 seek=\\\$i count=1 conv=notrunc &
4106                         pids_w[\\\$i]=\\\$!;
4107                 done;
4108                 rc_w=0;
4109                 for ((i = 0; i < $n; i++)); do
4110                         wait \\\${pids_w[\\\$i]};
4111                         newrc=\\\$?;
4112                         [ \\\$newrc -gt \\\$rc_w ] && rc_w=\\\$newrc;
4113                 done;
4114                 exit \\\$rc_w" &
4115         local pid_w=$!
4116         do_nodes $CLIENTS sync;
4117         cancel_lru_locks osc
4118
4119         do_nodes $CLIENTS $myRUNAS \
4120                 "declare -a pids_r;
4121                 for ((i = 0; i < $n; i++)); do
4122                         dd if=$dir/nrs_r_\$HOSTNAME bs=1M of=/dev/null \
4123 seek=\\\$i count=1 &
4124                         pids_r[\\\$i]=\\\$!;
4125                 done;
4126                 rc_r=0;
4127                 for ((i = 0; i < $n; i++)); do
4128                         wait \\\${pids_r[\\\$i]};
4129                         newrc=\\\$?;
4130                         [ \\\$newrc -gt \\\$rc_r ] && rc_r=\\\$newrc;
4131                 done;
4132                 exit \\\$rc_r" &
4133         local pid_r=$!
4134         cancel_lru_locks osc
4135
4136         wait $pid_w || error "dd (write) failed (2)"
4137         wait $pid_r || error "dd (read) failed (3)"
4138         rm -rvf $dir || error "rm -rf $dir failed"
4139 }
4140
4141 test_77a() { #LU-3266
4142         local rc
4143
4144         oss=$(comma_list $(osts_nodes))
4145         do_nodes $oss lctl set_param ost.OSS.*.nrs_policies="fifo" ||
4146                 rc=$?
4147         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
4148         [[ $rc -ne 0 ]] && error "failed to set fifo policy"
4149         nrs_write_read
4150
4151         return 0
4152 }
4153 run_test 77a "check FIFO NRS policy"
4154
4155 test_77b() { #LU-3266
4156         local rc
4157
4158         oss=$(comma_list $(osts_nodes))
4159
4160         do_nodes $oss lctl set_param ost.OSS.*.nrs_policies="crrn" \
4161                 ost.OSS.*.nrs_crrn_quantum=1 || rc=$?
4162         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
4163         [[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 1"
4164
4165         echo "policy: crr-n, crrn_quantum 1"
4166         nrs_write_read
4167
4168         do_nodes $oss lctl set_param \
4169                 ost.OSS.*.nrs_crrn_quantum=64 || rc=$?
4170         [[ $rc -ne 0 ]] && error "failed to set crrn_quantum to 64"
4171
4172         echo "policy: crr-n, crrn_quantum 64"
4173         nrs_write_read
4174
4175         # cleanup
4176         do_nodes $oss lctl set_param \
4177                 ost.OSS.ost_io.nrs_policies="fifo" || rc=$?
4178         [[ $rc -ne 0 ]] && error "failed to set fifo policy"
4179         return 0
4180 }
4181 run_test 77b "check CRR-N NRS policy"
4182
4183 orr_trr() {
4184         local policy=$1
4185
4186         oss=$(comma_list $(osts_nodes))
4187
4188         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies=$policy \
4189                 ost.OSS.*.nrs_"$policy"_quantum=1 \
4190                 ost.OSS.*.nrs_"$policy"_offset_type="physical" \
4191                 ost.OSS.*.nrs_"$policy"_supported="reads" || return $?
4192
4193         echo "policy: $policy, ${policy}_quantum 1, ${policy}_offset_type " \
4194                 "physical, ${policy}_supported reads"
4195         nrs_write_read
4196
4197         do_nodes $oss lctl set_param \
4198                 ost.OSS.*.nrs_${policy}_supported="writes" \
4199                 ost.OSS.*.nrs_${policy}_quantum=64 || return $?
4200
4201         echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
4202                 "physical, ${policy}_supported writes"
4203         nrs_write_read
4204
4205         do_nodes $oss lctl set_param \
4206                 ost.OSS.*.nrs_${policy}_supported="reads_and_writes" \
4207                 ost.OSS.*.nrs_${policy}_offset_type="logical" || return $?
4208         echo "policy: $policy, ${policy}_quantum 64, ${policy}_offset_type " \
4209                 "logical, ${policy}_supported reads_and_writes"
4210         nrs_write_read
4211
4212         # cleanup
4213         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo" ||
4214                 return $?
4215         return 0
4216 }
4217
4218 test_77c() { #LU-3266
4219         local rc
4220         orr_trr "orr" || rc=$?
4221         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
4222         [[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
4223         return 0
4224 }
4225 run_test 77c "check ORR NRS policy"
4226
4227 test_77d() { #LU-3266
4228         local rc
4229         orr_trr "trr" || rc=$?
4230         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
4231         [[ $rc -ne 0 ]] && error "orr_trr failed rc:$rc"
4232         return 0
4233 }
4234 run_test 77d "check TRR nrs policy"
4235
4236 tbf_rule_operate()
4237 {
4238         local facet=$1
4239         shift 1
4240
4241         do_facet $facet lctl set_param \
4242                 ost.OSS.ost_io.nrs_tbf_rule="$*"
4243         [ $? -ne 0 ] &&
4244                 error "failed to run operate '$*' on TBF rules"
4245 }
4246
4247 cleanup_tbf_verify()
4248 {
4249         local rc=0
4250         trap 0
4251         echo "cleanup_tbf $DIR/$tdir"
4252         rm -rf $DIR/$tdir
4253         rc=$?
4254         wait_delete_completed
4255         return $rc
4256 }
4257
4258 tbf_verify() {
4259         local dir=$DIR/$tdir
4260         local client1=${CLIENT1:-$(hostname)}
4261         local myRUNAS="$3"
4262         local create_as="$4"
4263
4264         local np=$(check_cpt_number ost1)
4265         [ $np -gt 0 ] || error "CPU partitions should not be $np."
4266         echo "cpu_npartitions on ost1 is $np"
4267
4268         mkdir $dir || error "mkdir $dir failed"
4269         $LFS setstripe -c 1 -i 0 $dir || error "setstripe to $dir failed"
4270         chmod 777 $dir
4271
4272         if [[ -n "$create_as" ]]; then
4273                 $create_as touch $dir/tbf
4274                 chmod 777 $dir/tbf
4275         fi
4276
4277         trap cleanup_tbf_verify EXIT
4278         echo "Limited write rate: $1, read rate: $2"
4279         echo "Verify the write rate is under TBF control"
4280         local start=$SECONDS
4281         do_node $client1 $myRUNAS dd if=/dev/zero of=$dir/tbf \
4282                 bs=1M count=100 oflag=direct 2>&1
4283         local runtime=$((SECONDS - start + 1))
4284         local rate=$(bc <<< "scale=6; 100 / $runtime")
4285         echo "Write runtime is $runtime s, speed is $rate IOPS"
4286
4287         # verify the write rate does not exceed TBF rate limit
4288         [ $(bc <<< "$rate < 1.1 * $np * $1") -eq 1 ] ||
4289                 error "The write rate ($rate) exceeds 110% of rate limit ($1 * $np)"
4290
4291         cancel_lru_locks osc
4292
4293         echo "Verify the read rate is under TBF control"
4294         start=$SECONDS
4295         do_node $client1 $myRUNAS dd if=$dir/tbf of=/dev/null \
4296                 bs=1M count=100 iflag=direct 2>&1
4297         runtime=$((SECONDS - start + 1))
4298         rate=$(bc <<< "scale=6; 100 / $runtime")
4299         echo "Read runtime is $runtime s, speed is $rate IOPS"
4300
4301         # verify the read rate does not exceed TBF rate limit
4302         [ $(bc <<< "$rate < 1.1 * $np * $2") -eq 1 ] ||
4303                 error "The read rate ($rate) exceeds 110% of rate limit ($2 * $np)"
4304
4305         cancel_lru_locks osc
4306         cleanup_tbf_verify || error "rm -rf $dir failed"
4307 }
4308
4309 test_77e() {
4310         local rc
4311
4312         oss=$(comma_list $(osts_nodes))
4313
4314         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
4315                 rc=$?
4316         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4317         [[ $rc -ne 0 ]] && error "failed to set TBF NID policy"
4318
4319         local idis
4320         local rateis
4321         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4322                 idis="nid="
4323                 rateis="rate="
4324         fi
4325
4326         # Only operate rules on ost1 since OSTs might run on the same OSS
4327         # Add some rules
4328         tbf_rule_operate ost1 "start\ localhost\ ${idis}{0@lo}\ ${rateis}1000"
4329         local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
4330         local client_nids=$(nids_list $address "\\")
4331         tbf_rule_operate ost1 "start\ clients\ ${idis}{$client_nids}\ ${rateis}100"
4332         tbf_rule_operate ost1 "start\ others\ ${idis}{*.*.*.*@$NETTYPE}\ ${rateis}50"
4333         nrs_write_read
4334
4335         # Change the rules
4336         tbf_rule_operate ost1 "change\ localhost\ ${rateis}1001"
4337         tbf_rule_operate ost1 "change\ clients\ ${rateis}101"
4338         tbf_rule_operate ost1 "change\ others\ ${rateis}51"
4339         nrs_write_read
4340
4341         # Stop the rules
4342         tbf_rule_operate ost1 "stop\ localhost"
4343         tbf_rule_operate ost1 "stop\ clients"
4344         tbf_rule_operate ost1 "stop\ others"
4345         nrs_write_read
4346
4347         # Cleanup the TBF policy
4348         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4349         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4350         nrs_write_read
4351         return 0
4352 }
4353 run_test 77e "check TBF NID nrs policy"
4354
4355 test_77f() {
4356         local rc
4357
4358         oss=$(comma_list $(osts_nodes))
4359
4360         do_nodes $oss $LCTL set_param \
4361                 ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
4362         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4363         [[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"
4364
4365         # Configure jobid_var
4366         local saved_jobid_var=$($LCTL get_param -n jobid_var)
4367         rc=$?
4368         [[ $rc -eq 3 ]] && skip "jobid_var not found" && return
4369         [[ $rc -ne 0 ]] && error "failed to get param jobid_var"
4370         if [ $saved_jobid_var != procname_uid ]; then
4371                 set_persistent_param_and_check client \
4372                         "jobid_var" "$FSNAME.sys.jobid_var" procname_uid
4373         fi
4374
4375         local idis
4376         local rateis
4377         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4378                 idis="jobid="
4379                 rateis="rate="
4380         fi
4381
4382         # Only operate rules on ost1 since OSTs might run on the same OSS
4383         # Add some rules
4384         tbf_rule_operate ost1 "start\ runas\ ${idis}{iozone.$RUNAS_ID\ dd.$RUNAS_ID\ tiotest.$RUNAS_ID}\ ${rateis}1000"
4385         tbf_rule_operate ost1 "start\ iozone_runas\ ${idis}{iozone.$RUNAS_ID}\ ${rateis}100"
4386         tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"
4387         nrs_write_read "$RUNAS"
4388
4389         # Change the rules
4390         tbf_rule_operate ost1 "change\ runas\ ${rateis}1001"
4391         tbf_rule_operate ost1 "change\ iozone_runas\ ${rateis}101"
4392         tbf_rule_operate ost1 "change\ dd_runas\ ${rateis}51"
4393         nrs_write_read "$RUNAS"
4394
4395         # Stop the rules
4396         tbf_rule_operate ost1 "stop\ runas"
4397         tbf_rule_operate ost1 "stop\ iozone_runas"
4398         tbf_rule_operate ost1 "stop\ dd_runas"
4399         nrs_write_read "$RUNAS"
4400
4401         # Cleanup the TBF policy
4402         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4403         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4404         nrs_write_read "$RUNAS"
4405
4406         local current_jobid_var=$($LCTL get_param -n jobid_var)
4407         [[ $? -ne 0 ]] && error "failed to get param jobid_var"
4408         if [ $saved_jobid_var != $current_jobid_var ]; then
4409                 set_persistent_param_and_check client \
4410                         "jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
4411         fi
4412         return 0
4413 }
4414 run_test 77f "check TBF JobID nrs policy"
4415
4416 test_77g() {
4417         local rc=0
4418
4419         oss=$(comma_list $(osts_nodes))
4420
4421         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid" ||
4422                 rc=$?
4423         [[ $rc -eq 3 ]] && skip "no NRS TBF exists" && return
4424         [[ $rc -ne 0 ]] && error "failed to set TBF NID policy"
4425
4426         do_nodes $oss lctl set_param \
4427                 ost.OSS.ost_io.nrs_policies="tbf\ jobid" || rc=$?
4428         [[ $rc -ne 0 ]] && error "failed to set TBF JOBID policy"
4429
4430         local idis
4431         local rateis
4432         if [ "$OST1_VERSION" -ge $(version_code 2.8.54) ]; then
4433                 idis="jobid="
4434                 rateis="rate="
4435         fi
4436
4437         # Add a rule that only valid for Jobid TBF. If direct change between
4438         # TBF types is not supported, this operation will fail.
4439         tbf_rule_operate ost1 "start\ dd_runas\ ${idis}{dd.$RUNAS_ID}\ ${rateis}50"
4440
4441         # Cleanup the TBF policy
4442         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4443         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4444         return 0
4445 }
4446 run_test 77g "Change TBF type directly"
4447
4448 test_77h() {
4449         [ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
4450                 skip "Need OST version at least 2.8.55"
4451
4452         local old_policy=$(do_facet ost1 \
4453                 lctl get_param ost.OSS.ost_io.nrs_policies)
4454         local new_policy
4455
4456         do_facet ost1 lctl set_param \
4457                 ost.OSS.ost_io.nrs_policies="abc"
4458         [ $? -eq 0 ] && error "should return error"
4459
4460         do_facet ost1 lctl set_param \
4461                 ost.OSS.ost_io.nrs_policies="tbf\ abc"
4462         [ $? -eq 0 ] && error "should return error"
4463
4464         do_facet ost1 lctl set_param \
4465                 ost.OSS.ost_io.nrs_policies="tbf\ reg\ abc"
4466         [ $? -eq 0 ] && error "should return error"
4467
4468         do_facet ost1 lctl set_param \
4469                 ost.OSS.ost_io.nrs_policies="tbf\ abc\ efg"
4470         [ $? -eq 0 ] && error "should return error"
4471
4472         new_policy=$(do_facet ost1 lctl get_param ost.OSS.ost_io.nrs_policies)
4473         [ $? -eq 0 ] || error "shouldn't LBUG"
4474
4475         [ "$old_policy" = "$new_policy" ] || error "NRS policy should be same"
4476
4477         return 0
4478 }
4479 run_test 77h "Wrong policy name should report error, not LBUG"
4480
4481 tbf_rule_check()
4482 {
4483         local facet=$1
4484         local expected=$2
4485         local error_message=$3
4486         local rule_number=0
4487         for rule in $expected; do
4488                 rule_number=$((rule_number + 1))
4489         done
4490         local stop_line=$(($rule_number + 3))
4491         local awk_command="awk 'NR >= 4 && NR <= $stop_line {print \$1}'"
4492
4493         local output=$(do_facet $facet lctl get_param \
4494                 ost.OSS.ost_io.nrs_tbf_rule |
4495                 eval $awk_command |
4496                 tr "\n" " " |
4497                 sed 's/[ ]*$//')
4498         if [ "$output" != "$expected" ]; then
4499                 error "$error_message, expected '$expected', got '$output'"
4500         fi
4501 }
4502
4503 test_77i() {
4504         [ "$OST1_VERSION" -ge $(version_code 2.8.55) ] ||
4505                 skip "Need OST version at least 2.8.55"
4506
4507         for i in $(seq 1 $OSTCOUNT)
4508         do
4509                 do_facet ost"$i" lctl set_param \
4510                         ost.OSS.ost_io.nrs_policies="tbf\ jobid"
4511                 [ $? -ne 0 ] &&
4512                         error "failed to set TBF policy"
4513         done
4514
4515         tbf_rule_check ost1 "default" "error before inserting any rule"
4516
4517         tbf_rule_operate ost1 "start\ before\ jobid={jobid}\ rate=1000"
4518         tbf_rule_check ost1 "before default" \
4519                 "error when inserting rule 'before'"
4520
4521         tbf_rule_operate ost1 "start\ after\ jobid={jobid}\ rate=1000\ rank=default"
4522         tbf_rule_check ost1 "before after default" \
4523                 "error when inserting rule 'after'"
4524
4525         tbf_rule_operate ost1 "start\ target\ jobid={jobid}\ rate=1000\ rank=after"
4526         tbf_rule_check ost1 "before target after default" \
4527                 "error when inserting rule 'target'"
4528
4529         echo "Move before itself"
4530         tbf_rule_operate ost1 "change\ target\ rank=target"
4531         tbf_rule_check ost1 "before target after default" \
4532                 "error when moving before itself"
4533
4534         echo "Move to higher rank"
4535         tbf_rule_operate ost1 "change\ target\ rank=before"
4536         tbf_rule_check ost1 "target before after default" \
4537                 "error when moving to higher rank"
4538
4539         echo "Move to lower rank"
4540         tbf_rule_operate ost1 "change\ target\ rank=after"
4541         tbf_rule_check ost1 "before target after default" \
4542                 "error when moving to lower rank"
4543
4544         echo "Move before default"
4545         tbf_rule_operate ost1 "change\ target\ rank=default"
4546         tbf_rule_check ost1 "before after target default" \
4547                 error "error when moving before default"
4548
4549         # Cleanup the TBF policy
4550         do_nodes $(comma_list $(osts_nodes)) \
4551                 $LCTL set_param ost.OSS.ost_io.nrs_policies=fifo
4552         return 0
4553 }
4554 run_test 77i "Change rank of TBF rule"
4555
4556 test_77j() {
4557         local idis
4558         local rateis
4559
4560         [ "$OST1_VERSION" -ge $(version_code 2.9.53) ] ||
4561                 skip "Need OST version at least 2.9.53"
4562         if [ "$OST1_VERSION" -ge $(version_code 2.8.60) ]; then
4563                 idis="opcode="
4564                 rateis="rate="
4565         fi
4566
4567         do_nodes $(comma_list $(osts_nodes)) \
4568                 lctl set_param jobid_var=procname_uid \
4569                         ost.OSS.ost_io.nrs_policies="tbf\ opcode" \
4570                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_r\ ${idis}{ost_read}\ ${rateis}5" \
4571                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_w\ ${idis}{ost_write}\ ${rateis}20"
4572         [ $? -ne 0 ] && error "failed to set TBF OPCode policy"
4573
4574         nrs_write_read
4575         tbf_verify 20 5
4576
4577         do_nodes $(comma_list $(osts_nodes)) \
4578                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_r" \
4579                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_w" \
4580                         ost.OSS.ost_io.nrs_policies="fifo"
4581
4582         # sleep 3 seconds to wait the tbf policy stop completely,
4583         # or the next test case is possible get -EAGAIN when
4584         # setting the tbf policy
4585         sleep 3
4586 }
4587 run_test 77j "check TBF-OPCode NRS policy"
4588
4589 test_id() {
4590         local idstr="${1}id"
4591         local policy="${idstr}={$2}"
4592         local rate="rate=$3"
4593         local runas_args="$4"
4594         local createas_args="${5:-$runas_args}"
4595
4596         do_nodes $(comma_list $(osts_nodes)) \
4597                 lctl set_param jobid_var=procname_uid \
4598                         ost.OSS.ost_io.nrs_policies="tbf\ ${idstr}" \
4599                         ost.OSS.ost_io.nrs_tbf_rule="start\ ost_${idstr}\ ${policy}\ ${rate}"
4600         [ $? -ne 0 ] && error "failed to set tbf ${idstr} policy"
4601
4602         nrs_write_read "runas $runas_args" "runas $createas_args"
4603         tbf_verify $3 $3 "runas $runas_args" "runas $createas_args"
4604
4605         do_nodes $(comma_list $(osts_nodes)) \
4606                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ost_${idstr}" \
4607                         ost.OSS.ost_io.nrs_policies="fifo"
4608
4609         # sleep 3 seconds to wait the tbf policy stop completely,
4610         # or the next test case is possible get -eagain when
4611         # setting the tbf policy
4612         sleep 3
4613 }
4614
4615 test_77ja(){
4616         if [ "$OST1_VERSION" -lt $(version_code 2.11.50) ]; then
4617                 skip "Need OST version at least 2.11.50"
4618         fi
4619
4620         test_id "u" "$RUNAS_ID" "5" "-u $RUNAS_ID"
4621         test_id "g" "$RUNAS_GID" "5" "-u $RUNAS_ID -g $RUNAS_GID"
4622 }
4623 run_test 77ja "check TBF-UID/GID NRS policy"
4624
4625 test_77jb() { # LU-16077
4626         (( "$OST1_VERSION" >= $(version_code 2.15.51) )) ||
4627                 skip "Need OST version at least 2.15.51"
4628
4629         test_id "u" "$RUNAS_ID" "5" "-u $RUNAS_ID" "-u 0 -g $RUNAS_GID"
4630         test_id "g" "$RUNAS_GID" "5" "-u $RUNAS_ID -g $RUNAS_GID" "-u $RUNAS_ID -g 0"
4631 }
4632 run_test 77jb "check TBF-UID/GID NRS policy on files that don't belong to us"
4633
4634 cleanup_77k()
4635 {
4636         local rule_lists=$1
4637         local old_nrs=$2
4638
4639         trap 0
4640         for rule in $rule_lists; do
4641                 do_nodes $(comma_list $(osts_nodes)) \
4642                         lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ $rule"
4643         done
4644
4645         do_nodes $(comma_list $(osts_nodes)) \
4646                 lctl set_param ost.OSS.ost_io.nrs_policies="$old_nrs"
4647
4648         sleep 3
4649 }
4650
4651 test_77k() {
4652         [[ "$OST1_VERSION" -ge $(version_code 2.9.53) ]] ||
4653                 skip "Need OST version at least 2.9.53"
4654
4655         do_nodes $(comma_list $(osts_nodes)) \
4656                 lctl set_param ost.OSS.ost_io.nrs_policies="tbf" \
4657                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ jobid={dd.$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
4658                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ jobid={dd.$RUNAS_ID}\&opcode={ost_read}\ rate=10"
4659
4660         nrs_write_read "$RUNAS"
4661         tbf_verify 20 10 "$RUNAS"
4662
4663         local address=$(comma_list "$(host_nids_address $CLIENTS $NETTYPE)")
4664         local client_nids=$(nids_list $address "\\")
4665         do_nodes $(comma_list $(osts_nodes)) \
4666                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
4667                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
4668                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_w\ nid={0@lo\ $client_nids}\&opcode={ost_write}\ rate=20" \
4669                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_r\ nid={0@lo\ $client_nids}\&opcode={ost_read}\ rate=10"
4670
4671         nrs_write_read
4672         tbf_verify 20 10
4673
4674         do_nodes $(comma_list $(osts_nodes)) \
4675                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_w" \
4676                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_r" \
4677                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext\ nid={0@lo\ $client_nids}\&jobid={dd.$RUNAS_ID}\ rate=20"
4678
4679         nrs_write_read "$RUNAS"
4680         tbf_verify 20 20 "$RUNAS"
4681
4682         do_nodes $(comma_list $(osts_nodes)) \
4683                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext" \
4684                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ jobid={dd.$RUNAS_ID},opcode={ost_write}\ rate=20" \
4685                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ jobid={dd.$RUNAS_ID},opcode={ost_read}\ rate=10"
4686
4687         nrs_write_read "$RUNAS"
4688         # with parameter "RUNAS", it will match the latest rule
4689         # "ext_b" first, so the limited write rate is 10.
4690         tbf_verify 10 10 "$RUNAS"
4691         tbf_verify 20 10
4692
4693         trap "cleanup_77k \"ext_a ext_b\" \"fifo\"" EXIT
4694
4695         [[ "$OST1_VERSION" -ge $(version_code 2.10.58) ]] ||
4696                 skip "Need OST version at least 2.10.58"
4697
4698         do_nodes $(comma_list $(osts_nodes)) \
4699                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_a" \
4700                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_b" \
4701                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ug\ uid={$RUNAS_ID}\&gid={$RUNAS_GID}\ rate=5"
4702         nrs_write_read "runas -u $RUNAS_ID -g $RUNAS_GID"
4703         tbf_verify 5 5 "runas -u $RUNAS_ID -g $RUNAS_GID"
4704
4705         do_nodes $(comma_list $(osts_nodes)) \
4706                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ug" \
4707                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_uw\ uid={$RUNAS_ID}\&opcode={ost_write}\ rate=20" \
4708                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_ur\ uid={$RUNAS_ID}\&opcode={ost_read}\ rate=10"
4709
4710         nrs_write_read "runas -u $RUNAS_ID"
4711         tbf_verify 20 10 "runas -u $RUNAS_ID"
4712
4713         do_nodes $(comma_list $(osts_nodes)) \
4714                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_uw" \
4715                         ost.OSS.ost_io.nrs_tbf_rule="stop\ ext_ur" \
4716                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_a\ uid={$RUNAS_ID},opcode={ost_write}\ rate=20" \
4717                         ost.OSS.ost_io.nrs_tbf_rule="start\ ext_b\ uid={$RUNAS_ID},opcode={ost_read}\ rate=10"
4718         nrs_write_read "runas -u $RUNAS_ID"
4719         tbf_verify 10 10 "runas -u $RUNAS_ID"
4720         tbf_verify 20 10 "runas -u $RUNAS_ID"
4721         cleanup_77k "ext_a ext_b" "fifo"
4722 }
4723 run_test 77k "check TBF policy with NID/JobID/OPCode expression"
4724
4725 test_77l() {
4726         [[ "$OST1_VERSION" -ge $(version_code 2.10.56) ]] ||
4727                 skip "Need OST version at least 2.10.56"
4728
4729         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ nid"
4730         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="tbf"
4731
4732         local output=$(do_facet ost1 lctl get_param \
4733                         ost.OSS.ost_io.nrs_policies | \
4734                         awk '/name: tbf/ {print;exit}' | \
4735                         awk -F ': ' '{print $2}')
4736
4737         if [ "$output" != "tbf" ]; then
4738                 error "The generic TBF output is '$output', not 'tbf'"
4739         fi
4740
4741         do_facet ost1 lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4742 }
4743 run_test 77l "check the output of NRS policies for generic TBF"
4744
4745 test_77m() {
4746         if [ "$OST1_VERSION" -lt $(version_code 2.9.54) ]; then
4747                 skip "Need OST version at least 2.9.54"
4748         fi
4749
4750         local dir=$DIR/$tdir
4751
4752         mkdir $dir || error "mkdir $dir failed"
4753         $LFS setstripe -c $OSTCOUNT $dir || error "setstripe to $dir failed"
4754         chmod 777 $dir
4755
4756         local nodes=$(comma_list $(osts_nodes))
4757         do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies=delay \
4758                                        ost.OSS.ost_io.nrs_delay_min=4 \
4759                                        ost.OSS.ost_io.nrs_delay_max=4 \
4760                                        ost.OSS.ost_io.nrs_delay_pct=100
4761         [ $? -ne 0 ] && error "Failed to set delay policy"
4762
4763         local start=$SECONDS
4764         do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
4765                  dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=1 \
4766                    oflag=direct conv=fdatasync ||
4767                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4768                   error "dd on client failed (1)"; }
4769         local elapsed=$((SECONDS - start))
4770
4771         # NRS delay doesn't do sub-second timing, so a request enqueued at
4772         # 0.9 seconds can be dequeued at 4.0
4773         [ $elapsed -lt 3 ] &&
4774                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4775                   error "Single 1M write should take at least 3 seconds"; }
4776
4777         start=$SECONDS
4778         do_nodes "${SINGLECLIENT:-$HOSTNAME}" "$RUNAS" \
4779                  dd if=/dev/zero of="$dir/nrs_delay_$HOSTNAME" bs=1M count=10 \
4780                    oflag=direct conv=fdatasync ||
4781                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4782                   error "dd on client failed (2)"; }
4783         elapsed=$((SECONDS - start))
4784
4785         [ $elapsed -lt 30 ] &&
4786                 { do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo";
4787                   error "Ten 1M writes should take at least 30 seconds"; }
4788
4789         do_nodes $nodes lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
4790         [ $? -ne 0 ] && error "failed to set policy back to fifo"
4791
4792         return 0
4793 }
4794 run_test 77m "check NRS Delay slows write RPC processing"
4795
4796 test_77n() { #LU-10802
4797         if [ "$OST1_VERSION" -lt $(version_code 2.10.58) ]; then
4798                 skip "Need OST version at least 2.10.58"
4799         fi
4800
4801         # Configure jobid_var
4802         local saved_jobid_var=$($LCTL get_param -n jobid_var)
4803         if [ $saved_jobid_var != procname_uid ]; then
4804                 set_persistent_param_and_check client \
4805                         "jobid_var" "$FSNAME.sys.jobid_var" procname_uid
4806         fi
4807
4808         do_nodes $(comma_list $(osts_nodes)) \
4809                 lctl set_param ost.OSS.ost_io.nrs_policies="tbf\ jobid" \
4810                         ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4811                         ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={*.$RUNAS_ID}\ rate=20"
4812
4813         nrs_write_read
4814         tbf_verify 20 20 "$RUNAS"
4815
4816         do_nodes $(comma_list $(osts_nodes)) \
4817                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4818                         ost.OSS.ost_io.nrs_tbf_rule="start\ dd_runas\ jobid={dd.*}\ rate=20"
4819
4820         nrs_write_read
4821         tbf_verify 20 20
4822
4823         do_nodes $(comma_list $(osts_nodes)) \
4824                 lctl set_param ost.OSS.ost_io.nrs_tbf_rule="stop\ dd_runas" \
4825                         ost.OSS.ost_io.nrs_policies="fifo"
4826
4827         sleep 3
4828
4829         local current_jobid_var=$($LCTL get_param -n jobid_var)
4830         if [ $saved_jobid_var != $current_jobid_var ]; then
4831                 set_persistent_param_and_check client \
4832                         "jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid_var
4833         fi
4834 }
4835 run_test 77n "check wildcard support for TBF JobID NRS policy"
4836
4837 test_77o() {
4838         (( $OST1_VERSION > $(version_code 2.14.54) )) ||
4839                 skip "need OST > 2.14.54"
4840
4841         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf\ nid"
4842         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name\ nid={192.168.*.*@tcp}\ rate=10000"
4843         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ name1\ nid={192.168.*.*@tcp}\ rate=10000"
4844         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="change\ name1\ rank=name"
4845         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="stop\ name"
4846         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="fifo"
4847 }
4848 run_test 77o "Changing rank should not panic"
4849
4850 test_77q() {
4851         local i
4852
4853         (( $MDS1_VERSION > $(version_code 2.14.54) )) ||
4854                 skip "need MDS >= 2.14.54"
4855
4856         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
4857         stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"
4858
4859         for i in {1..50}; do
4860                 local pid1 pid2
4861
4862                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_1 uid={ 500  11 3}&gid={500 10 33   100 } rate=100'" &
4863                 pid1=$!
4864                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start rule77q_2 uid={1000}&gid={1000} rate=100'" &
4865                 pid2=$!
4866                 wait $pid1 || error "$i: Fail to start TBF rule 'rule77q_1'"
4867                 wait $pid2 || error "$i: Fail to start TBF rule 'rule77q_2'"
4868
4869                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_1'" &
4870                 pid1=$!
4871                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'stop rule77q_2'" &
4872                 pid2=$!
4873                 wait $pid1 || error "$i: Fail to stop TBF rule 'rule77q_1'"
4874                 wait $pid2 || error "$i: Fail to stop TBF rule 'rule77q_2'"
4875         done
4876 }
4877 run_test 77q "Parallel TBF rule definitions should not panic"
4878
4879 test_77p() {
4880         local c
4881         local -a spec_chars=(
4882                 '@' '.' '~' '#' '/' '^' '%' '*' ';' ',' '?' '<' '>' ':'
4883                 '+' '=' ')' '(' '{' '}' '|' '[' ']' '!' '&' '\$' '\`' '\\')
4884
4885         (( $MDS1_VERSION > $(version_code 2.14.54) )) ||
4886                 skip "need MDS >= 2.14.54"
4887
4888         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies="tbf"
4889         stack_trap "do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_policies=fifo"
4890
4891         # TBF rule name size is 16 bytes
4892         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_overflo\ uid={500}\ rate=500" &&
4893                 error "The length of tbf rule name is not checked" || true
4894         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ \ uid={500}\ rate=500" &&
4895                 error "The server should not accept empty tbf rule name" || true
4896         do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="start\ test_77p_empty" &&
4897                 error "The server should not accept 'start <tbf_rule_name>' without an expression" || true
4898
4899         # Test with special chars
4900         for c in "${spec_chars[@]}"; do
4901                 do_facet mds1 $LCTL set_param mds.MDS.mdt.nrs_tbf_rule="'start test77p${c}spec uid={500} rate=500'" &&
4902                 error "Special char '${c}' should not be accepted in a tbf rule name" || true
4903         done
4904
4905 }
4906 run_test 77p "Check validity of rule names for TBF policies"
4907
4908 cleanup_77r() {
4909         local pid=$1
4910         local saved_jobid=$2
4911         local current_jobid_var
4912
4913         echo "cleanup 77r $pid"
4914
4915         do_facet mds1 $LCTL set_param -n mds.MDS.mdt.nrs_policies=fifo
4916         kill $pid || echo "fail to kill md thread"
4917
4918         current_jobid_var=$($LCTL get_param -n jobid_var)
4919         if [ $saved_jobid != $current_jobid_var ]; then
4920                 set_persistent_param_and_check client \
4921                         "jobid_var" "$FSNAME.sys.jobid_var" $saved_jobid
4922         fi
4923
4924         sleep 2
4925         rm -rf $DIR1/$tdir
4926 }
4927
4928 md_thread_run="true"
4929 md_thread_77r() {
4930         local pid
4931
4932         while $md_thread_run; do
4933                 printf '%s\n' {$DIR1,$DIR2}/$tdir/${tfile}-{01..20} |
4934                         xargs -P20 -I{} $RUNAS bash -c 'touch {}; rm -f {}' \
4935                                 &> /dev/null & pid=$!
4936                 trap "echo kill md_thread xargs; md_thread_run=false; kill $pid" INT TERM
4937                 wait $pid
4938         done
4939 }
4940
4941 wait_policy_state() {
4942         local state="$1"
4943         local policy="$2"
4944         local change_pid="$3"
4945         local time
4946
4947         for time in {1..60}; do
4948                 local nbr_started
4949
4950                 nbr_started=$(do_facet mds1 $LCTL get_param mds.MDS.mdt.nrs_policies |
4951                         egrep -A2 "name: ${policy}$" | grep -c "state: $state")
4952
4953                 [[ "$nbr_started" != 2 ]] || return 0
4954                 sleep 1
4955         done
4956
4957         [[ -z "$change_pid" ]] || kill $change_pid || true
4958         return 1
4959 }
4960
4961 test_77r() { #LU-14976
4962         local pid
4963         local -A rules
4964         local -a policies
4965         local saved_jobid_var
4966
4967         rules["tbf uid"]="start md_rule uid={$RUNAS_ID} rate=1"
4968         rules["tbf gid"]="start md_rule gid={$RUNAS_GID} rate=1"
4969         rules["tbf jobid"]="start md_rule jobid={*.$RUNAS_ID} rate=1"
4970         rules["tbf"]="start md_rule uid={$RUNAS_ID} rate=1"
4971         policies=(
4972         "tbf uid"
4973         "tbf gid"
4974         "tbf jobid"
4975         "tbf"
4976         "fifo"
4977         )
4978
4979         test_mkdir -i 0 -c 1 $DIR1/$tdir
4980         chmod 777 $DIR1/$tdir
4981
4982         # Configure jobid_var
4983         saved_jobid_var=$($LCTL get_param -n jobid_var)
4984         if [ $saved_jobid_var != procname_uid ]; then
4985                 set_persistent_param_and_check client \
4986                         "jobid_var" "$FSNAME.sys.jobid_var" procname_uid
4987         fi
4988
4989         # start md thread
4990         md_thread_77r & pid=$!
4991         stack_trap "cleanup_77r $pid '$saved_jobid_var'"
4992
4993         local policy
4994         for policy in "${policies[@]}"; do
4995                 local change_pid
4996
4997                 # wait to queue requests
4998                 sleep 5
4999
5000                 do_facet mds1 "$LCTL set_param mds.MDS.mdt.nrs_policies='$policy'" &
5001                 change_pid=$!
5002
5003                 wait_policy_state "started" "$policy" "$change_pid" ||
5004                         error "timeout to start '$policy' policy"
5005
5006                 [[ -n "${rules[$policy]}" ]] || continue
5007
5008                 do_facet mds1 "$LCTL set_param mds.MDS.mdt.nrs_tbf_rule='${rules[$policy]}'" ||
5009                         error "fail to set rule '${rules[$policy]}' to '$policy'"
5010         done
5011
5012         wait_policy_state "stopped" "tbf" ||
5013                 error "fail to stop tbf policy"
5014
5015         echo "check the number of requests in queue:"
5016         local awkcmd='/name: / {last = $3} '
5017         awkcmd+='/queued: / {printf "    %s: %d\n", last, $2;'
5018         awkcmd+='       if (last == "tbf" && $2 > 0) exit 1;}'
5019         do_facet mds1 $LCTL get_param mds.MDS.mdt.nrs_policies | awk "$awkcmd" ||
5020                 error "request leak in tbf policies"
5021 }
5022 run_test 77r "Change type of tbf policy at run time"
5023
5024 test_78() { #LU-6673
5025         local rc
5026
5027         oss=$(comma_list $(osts_nodes))
5028         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="orr" &
5029         do_nodes $oss lctl set_param ost.OSS.*.nrs_orr_quantum=1
5030         rc=$?
5031         [[ $rc -eq 3 ]] && skip "no NRS exists" && return
5032         # Valid return codes are:
5033         # 0: Tuning succeeded
5034         # ENODEV: Policy is still stopped
5035         # EAGAIN: Policy is being initialized
5036         [ $rc -eq 0 -o $rc -eq 19 -o $rc -eq 11 ] ||
5037                 error "Expected set_param to return 0|ENODEV|EAGAIN"
5038
5039         # Cleanup the ORR policy
5040         do_nodes $oss lctl set_param ost.OSS.ost_io.nrs_policies="fifo"
5041         [ $? -ne 0 ] && error "failed to set policy back to fifo"
5042         return 0
5043 }
5044 run_test 78 "Enable policy and specify tunings right away"
5045
5046 test_79() {
5047         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5048         test_mkdir $DIR/$tdir
5049
5050         # Prevent interference from layout intent RPCs due to
5051         # asynchronous writeback. These will be tested in 130c below.
5052         do_nodes ${CLIENTS:-$HOSTNAME} sync
5053
5054         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
5055                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
5056
5057 #define OBD_FAIL_MDS_INTENT_DELAY               0x160
5058         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
5059         local facet=mds$((mdtidx + 1))
5060         stat $DIR/$tdir
5061         set_nodes_failloc $(facet_active_host $facet) 0x80000160
5062         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null  &
5063         local pid=$!
5064         sleep 2
5065
5066 #define OBD_FAIL_MDS_GETXATTR_PACK       0x131
5067         set_nodes_failloc $(facet_active_host $facet) 0x80000131
5068
5069         wait $pid
5070         return 0
5071 }
5072 run_test 79 "xattr: intent error"
5073
5074 test_80a() {
5075         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5076         local MDTIDX=1
5077         local mdt_index
5078         local i
5079         local file
5080         local pid
5081
5082         mkdir_on_mdt0 $DIR1/$tdir
5083         mkdir -p $DIR1/$tdir/dir
5084         createmany -o $DIR1/$tdir/dir/f 10 ||
5085                 error "create files under remote dir failed $i"
5086
5087         cp /etc/passwd $DIR1/$tdir/$tfile
5088
5089         #migrate open file should fails
5090         multiop_bg_pause $DIR2/$tdir/$tfile O_c || error "open $file failed"
5091         pid=$!
5092         # give multiop a chance to open
5093         sleep 1
5094
5095         $LFS migrate -m $MDTIDX $DIR1/$tdir &&
5096                 error "migrate open files should failed with open files"
5097
5098         kill -USR1 $pid
5099
5100         $LFS migrate -m $MDTIDX $DIR1/$tdir ||
5101                         error "migrate remote dir error"
5102
5103         echo "Finish migration, then checking.."
5104         for file in $(find $DIR1/$tdir); do
5105                 mdt_index=$($LFS getstripe -m $file)
5106                 [ $mdt_index == $MDTIDX ] ||
5107                         error "$file is not on MDT${MDTIDX}"
5108         done
5109
5110         diff /etc/passwd $DIR1/$tdir/$tfile ||
5111                 error "file different after migration"
5112
5113         rm -rf $DIR1/$tdir || error "rm dir failed after migration"
5114 }
5115 run_test 80a "migrate directory when some children is being opened"
5116
5117 cleanup_80b() {
5118         trap 0
5119         kill -9 $migrate_pid
5120 }
5121
5122 success_count=0
5123 failure_count=0
5124
5125 run_and_count()
5126 {
5127         eval $@ &>/dev/null && success_count=$((success_count + 1)) ||
5128                 failure_count=$((failure_count + 1))
5129 }
5130
5131 test_80b() {
5132         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5133         local migdir1=$DIR1/$tdir/migrate_dir
5134         local migdir2=$DIR2/$tdir/migrate_dir
5135         local start_time
5136         local mdt_idx
5137         local rc=0
5138         local rc1=0
5139
5140         trap cleanup_80b EXIT
5141         # prepare migrate directory
5142         mkdir -p $migdir1
5143         for F in {1,2,3,4,5}; do
5144                 echo "$F$F$F$F$F" > $migdir1/file$F
5145                 echo "$F$F$F$F$F" > $DIR/$tdir/file$F
5146         done
5147
5148         # migrate the directories among MDTs
5149         (
5150                 while true; do
5151                         mdt_idx=$((RANDOM % MDSCOUNT))
5152                         # migrate may fail
5153                         $LFS migrate -m $mdt_idx $migdir1 &>/dev/null
5154                 done
5155         ) &
5156         migrate_pid=$!
5157
5158         echo "start migration thread $migrate_pid"
5159         # access the files at the same time
5160         start_time=$SECONDS
5161         echo "accessing the migrating directory for 1 minute..."
5162         while ((SECONDS - start_time < 60)); do
5163                 run_and_count ls $migdir2
5164                 run_and_count diff -u $DIR2/$tdir/file1 $migdir2/file1
5165                 run_and_count "cat $migdir2/file2 > $migdir2/file3"
5166                 run_and_count "echo "aaaaa" > $migdir2/file4"
5167                 run_and_count stat $migdir2/file5
5168                 run_and_count touch $migdir2/source_file
5169                 if [ -e $migdir2/source_file ]; then
5170                         run_and_count ln $migdir2/source_file \
5171                                 $migdir2/link_file
5172
5173                         [ -e $migdir2/link_file ] &&
5174                                 rm -rf $migdir2/link_file
5175
5176                         run_and_count mrename $migdir2/source_file \
5177                                 $migdir2/target_file
5178
5179                         [ -e $migdir2/target_file ] &&
5180                                 run_and_count rm -rf $migdir2/target_file ||
5181                                 run_and_count rm -rf $migdir2/source_file
5182                 fi
5183
5184                 kill -0 $migrate_pid || {
5185                         echo "migration stopped 1"
5186                         break
5187                 }
5188         done
5189
5190         # check migration are still there
5191         kill -0 $migrate_pid || error "migration stopped 2"
5192         cleanup_80b
5193         # access during migration may fail
5194         echo "concurrent access $failure_count failures, $success_count successes"
5195 }
5196 run_test 80b "Accessing directory during migration"
5197
5198 test_81a() {
5199         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5200
5201         rm -rf $DIR1/$tdir
5202
5203         mkdir -p $DIR1/$tdir
5204
5205         $LFS setdirstripe -i0 -c$MDSCOUNT  $DIR1/$tdir/d0
5206         $LFS setdirstripe -i0 -c$MDSCOUNT  $DIR1/$tdir/d1
5207
5208         cd $DIR1/$tdir
5209         touch d0/0      || error "create 0 failed"
5210         mv d0/0 d1/0    || error "rename d0/0 d1/0 failed"
5211         stat d0/0       && error "stat mv filed succeed"
5212         mv $DIR2/$tdir/d1/0 $DIR2/$tdir/d0/0 || error "rename d1/0 d0/0 failed"
5213         stat d0/0       || error "stat failed"
5214
5215         local t=$(ls -ai $DIR1/$tdir/d0 | sort -u | wc -l)
5216
5217         if [ $t -ne 3 ]; then
5218                 ls -ai $DIR1/$tdir/d0
5219                 error "expect 3 get $t"
5220         fi
5221
5222         return 0
5223 }
5224 run_test 81a "rename and stat under striped directory"
5225
5226 test_81b() {
5227         [ $MDSCOUNT -lt 2 ] &&
5228                 skip "We need at least 2 MDTs for this test"
5229
5230         local total
5231         local setattr_pid
5232
5233         total=1000
5234
5235         $LFS mkdir -c $MDSCOUNT $DIR1/$tdir || error "$LFS mkdir"
5236         createmany -o $DIR1/$tdir/$tfile. $total || error "createmany"
5237
5238         (
5239                 while true; do
5240                         touch $DIR1/$tdir
5241                 done
5242         ) &
5243         setattr_pid=$!
5244
5245         for i in $(seq $total); do
5246                 mrename $DIR2/$tdir/$tfile.$i $DIR2/$tdir/$tfile-new.$i \
5247                         > /dev/null
5248         done
5249
5250         kill -9 $setattr_pid
5251 }
5252 run_test 81b "rename under striped directory doesn't deadlock"
5253
5254 test_81c() {
5255         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
5256         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
5257                 skip "Need MDS version at least 2.13.52"
5258
5259         # source is local, source parent is remote
5260         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
5261         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
5262         $LFS mkdir -i 3 $DIR1/${tdir}_src/sub || error "mkdir sub"
5263         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
5264         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
5265         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
5266         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
5267         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
5268
5269         # source is remote, source parent is local
5270         $LFS mkdir -i 3 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
5271         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
5272         $LFS mkdir -i 0 $DIR1/${tdir}_src/sub || error "mkdir sub"
5273         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
5274         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
5275         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
5276         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
5277         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
5278
5279         # source and source parent are remote
5280         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
5281         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
5282         mkdir $DIR1/${tdir}_src/sub || error "mkdir sub"
5283         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
5284         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
5285         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
5286         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone"
5287         rm -rf $DIR1/${tdir}_src $DIR1/${tdir}_tgt
5288
5289         # source and source parent are remote, and source is remote object
5290         $LFS mkdir -i 0 $DIR1/${tdir}_src || error "mkdir ${tdir}_src"
5291         $LFS mkdir -i 1 $DIR1/${tdir}_tgt || error "mkdir ${tdir}_tgt"
5292         $LFS mkdir -i 2 $DIR1/${tdir}_src/sub || error "mkdir sub"
5293         $LFS mkdir -i 3 $DIR1/${tdir}_tgt/sub || error "mkdir sub"
5294         stat $DIR2/${tdir}_src/sub || error "stat sub failed"
5295         mv $DIR1/${tdir}_src/sub $DIR1/${tdir}_tgt/ || error "mv failed"
5296         [ -f $DIR2/${tdir}_src/sub ] && error "sub should be gone" || true
5297 }
5298 run_test 81c "rename revoke LOOKUP lock for remote object"
5299
5300 test_82() {
5301         [[ "$MDS1_VERSION" -gt $(version_code 2.6.91) ]] ||
5302                 skip "Need MDS version at least 2.6.92"
5303
5304         # Client 1 creates a file.
5305         multiop_bg_pause $DIR1/$tfile O_ac || error "multiop_bg_pause 1"
5306         pid1=$!
5307         # Client 2 opens the file.
5308         multiop_bg_pause $DIR2/$tfile o_Ac || error "multiop_bg_pause 2"
5309         pid2=$!
5310         # Client 1 makes the file an orphan.
5311         rm $DIR1/$tfile || error "rm"
5312         # Client 2 sets EA "user.multiop".
5313         kill -s USR1 $pid2
5314         wait $pid2 || error "multiop 2"
5315         # Client 1 gets EA "user.multiop".  This used to fail because the EA
5316         # cache refill would get "trusted.link" from mdd_xattr_list() but
5317         # -ENOENT when trying to get "trusted.link"'s value.  See also sanity
5318         # 102q.
5319         kill -s USR1 $pid1
5320         wait $pid1 || error "multiop 1"
5321 }
5322 run_test 82 "fsetxattr and fgetxattr on orphan files"
5323
5324 test_83() {
5325         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5326         local pid1
5327         local pid2
5328
5329         (
5330                 cd $DIR1
5331                 while true; do
5332                         $LFS mkdir -i1 -c2 $tdir
5333                         rmdir $tdir
5334                 done
5335         ) &
5336         pid1=$!
5337         echo "start pid $pid1 to create/unlink striped directory"
5338
5339         # Access the directory at the same time
5340         (
5341                 cd $DIR2
5342                 while true; do
5343                         stat $tdir > /dev/null 2>&1
5344                 done
5345         ) &
5346         pid2=$!
5347         echo "start pid $pid2 to stat striped directory"
5348
5349         sleep 120
5350         kill $pid1 $pid2
5351         wait $pid1 $pid2
5352
5353         return 0
5354 }
5355 run_test 83 "access striped directory while it is being created/unlinked"
5356
5357 test_84() {
5358         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
5359                 skip "lustre < 2.12.55 does not contain LU-12485 fix"
5360
5361         local mtime
5362
5363         $MULTIOP $DIR/$tfile oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:c ||
5364                 error "create $tfile failed"
5365         mtime=$(stat -c%Y $DIR/$tfile)
5366         mtime=$((mtime + 200))
5367
5368         #define OBD_FAIL_OBD_0NLINK_RACE  0x60b
5369         do_facet mds1 $LCTL set_param fail_loc=0x8000060b
5370
5371         touch -c -m $mtime $DIR/$tfile &
5372         setattr_pid=$!
5373         # sleep a while to let 'touch' run first
5374         sleep 5
5375         rm -f $DIR2/$tfile || error "unlink $tfile failed"
5376
5377         # touch may fail
5378         wait $setattr_pid || true
5379 }
5380 run_test 84 "0-nlink race in lu_object_find()"
5381
5382 test_85() {
5383         llapi_root_test $DIR/$tfile $DIR2/$tfile
5384 }
5385 run_test 85 "Lustre API root cache race"
5386
5387 test_90() {
5388         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5389         local pid1
5390         local pid2
5391         local duration=180
5392
5393         [ "$SLOW" = "yes" ] && duration=600
5394         # Open/Create under striped directory
5395         (
5396                 cd $DIR1
5397                 while true; do
5398                         $LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
5399                         touch $tdir/f{0..3} > /dev/null 2>&1
5400                 done
5401         ) &
5402         pid1=$!
5403         echo "start pid $pid1 to open/create under striped directory"
5404
5405         # unlink the striped directory at the same time
5406         (
5407                 cd $DIR2
5408                 while true; do
5409                         rm -rf $tdir > /dev/null 2>&1
5410                 done
5411         ) &
5412         pid2=$!
5413         echo "start pid $pid2 to unlink striped directory"
5414
5415         sleep $duration
5416
5417         kill $pid1 $pid2
5418         wait $pid1 $pid2
5419
5420         return 0
5421 }
5422 run_test 90 "open/create and unlink striped directory"
5423
5424 test_91() {
5425         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5426         local pid1
5427         local pid2
5428         local duration=180
5429
5430         [ "$SLOW" = "yes" ] && duration=600
5431         # chmod striped directory
5432         (
5433                 cd $DIR1
5434                 while true; do
5435                         $LFS mkdir -c$MDSCOUNT $tdir > /dev/null 2>&1
5436                         chmod go+w $tdir > /dev/null 2>&1
5437                 done
5438         ) &
5439         pid1=$!
5440         echo "start pid $pid1 to chmod striped directory"
5441
5442         # unlink the striped directory at the same time
5443         (
5444                 cd $DIR2
5445                 while true; do
5446                         rm -rf $tdir > /dev/null 2>&1
5447                 done
5448         ) &
5449         pid2=$!
5450         echo "start pid $pid2 to unlink striped directory"
5451
5452         sleep $duration
5453
5454         kill $pid1 $pid2
5455         wait $pid1 $pid2
5456
5457         return 0
5458 }
5459 run_test 91 "chmod and unlink striped directory"
5460
5461 test_92() {
5462         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5463
5464         local fd=$(free_fd)
5465         local cmd="exec $fd<$DIR1/$tdir"
5466         $LFS setdirstripe -c$MDSCOUNT $DIR1/$tdir || error "mkdir $tdir fails"
5467         eval $cmd
5468         cmd="exec $fd<&-"
5469         trap "eval $cmd" EXIT
5470         cd $DIR1/$tdir || error "cd $DIR1/$tdir fails"
5471         rmdir ../$tdir || error "rmdir ../$tdir fails"
5472
5473         #define OBD_FAIL_LLITE_NO_CHECK_DEAD  0x1408
5474         $LCTL set_param fail_loc=0x1408
5475         mkdir $DIR2/$tdir/dir && error "create dir succeeds"
5476         $LFS setdirstripe -i1 $DIR2/$tdir/remote_dir &&
5477                 error "create remote dir succeeds"
5478         $LCTL set_param fail_loc=0
5479         eval $cmd
5480         return 0
5481 }
5482 run_test 92 "create remote directory under orphan directory"
5483
5484 test_93() {
5485         local rc1=0
5486         local rc2=0
5487         local old_rr
5488
5489         mkdir -p $DIR1/$tfile-1/
5490         mkdir -p $DIR2/$tfile-2/
5491         local old_rr=$(do_facet $SINGLEMDS "$LCTL get_param -n \
5492                 lod.$FSNAME-MDT0000-*/qos_threshold_rr" | sed -e 's/%//')
5493         do_facet $SINGLEMDS "$LCTL set_param -n \
5494                 lod.$FSNAME-MDT0000-*/qos_threshold_rr=100"
5495         #define OBD_FAIL_MDS_LOV_CREATE_RACE     0x163
5496         do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x00000163"
5497
5498         $LFS setstripe -c -1 $DIR1/$tfile-1/file1 &
5499         local PID1=$!
5500         sleep 1
5501         $LFS setstripe -c -1 $DIR2/$tfile-2/file2 &
5502         local PID2=$!
5503         wait $PID2
5504         wait $PID1
5505         do_facet $SINGLEMDS "$LCTL set_param fail_loc=0x0"
5506         do_facet $SINGLEMDS "$LCTL set_param -n \
5507                 lod.$FSNAME-MDT0000-*/qos_threshold_rr=$old_rr"
5508
5509         $LFS getstripe $DIR1/$tfile-1/file1
5510         rc1=$($LFS getstripe -q $DIR1/$tfile-1/file1 |
5511                 awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
5512         $LFS getstripe $DIR2/$tfile-2/file2
5513         rc2=$($LFS getstripe -q $DIR2/$tfile-2/file2 |
5514                 awk '{if (/[0-9]/) print $1 }' | sort | uniq -d | wc -l)
5515         echo "rc1=$rc1 and rc2=$rc2 "
5516         [ $rc1 -eq 0 ] && [ $rc2 -eq 0 ] ||
5517                 error "object allocate on same ost detected"
5518 }
5519 run_test 93 "alloc_rr should not allocate on same ost"
5520
5521 test_94() {
5522         $LCTL set_param osc.*.idle_timeout=0
5523         dd if=/dev/zero of=$DIR2/$tfile bs=4k count=2 conv=fsync
5524
5525         local before=$(date +%s)
5526         local evict
5527
5528         $LCTL mark write
5529 #define OBD_FAIL_LDLM_PAUSE_CANCEL       0x312
5530         $LCTL set_param fail_val=5 fail_loc=0x80000312
5531         dd if=/dev/zero of=$DIR/$tfile conv=notrunc oflag=append bs=4k count=1 &
5532         local pid=$!
5533         sleep 2
5534
5535 #define OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL 0x329
5536         $LCTL set_param fail_val=6 fail_loc=0x80000329
5537         $LCTL mark kill $pid
5538         kill -ALRM $pid
5539
5540         dd if=/dev/zero of=$DIR2/$tfile conv=notrunc oflag=append bs=4k count=1
5541
5542         wait $pid
5543         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 conv=fsync
5544
5545         evict=$(do_facet client $LCTL get_param \
5546                 osc.$FSNAME-OST*-osc-*/state |
5547             awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')
5548
5549         [ -z "$evict" ] || [[ $evict -le $before ]] ||
5550                 (do_facet client $LCTL get_param \
5551                         osc.$FSNAME-OST*-osc-*/state;
5552                     error "eviction happened: $evict before:$before")
5553         $LCTL set_param osc.*.idle_timeout=debug
5554 }
5555 run_test 94 "signal vs CP callback race"
5556
5557 test_95a() {
5558         local file=$DIR/$tfile
5559         local file2=$DIR2/$tfile
5560         local fast_read_save
5561         local pid
5562
5563         fast_read_save=$($LCTL get_param -n llite.*.fast_read | head -n 1)
5564         [ -z "$fast_read_save" ] && skip "no fast read support"
5565
5566         stack_trap "$LCTL set_param llite.*.fast_read=$fast_read_save" EXIT
5567         $LCTL set_param llite.*.fast_read=0
5568
5569         $LFS setstripe -c $OSTCOUNT $file || error "failed to setstripe $file"
5570         dd if=/dev/zero of=$file bs=1M count=2 || error "failed to write $file"
5571         cancel_lru_locks $OSC
5572         $MULTIOP $file Oz1048576w4096c || error "failed to write $file"
5573         $MULTIOP $file oz1044480r4096c || error "failed to read $file"
5574
5575         # OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE  0x1421
5576         $LCTL set_param fail_loc=0x80001421 fail_val=7
5577         $MULTIOP $file2 Oz1048576w4096_c &
5578         pid=$!
5579
5580         sleep 2
5581         # OBD_FAIL_LLITE_READPAGE_PAUSE         0x1422
5582         $LCTL set_param fail_loc=0x80001422 fail_val=10
5583         $MULTIOP $file oz1044480r4096c || error "failed to read $file"
5584
5585         kill -USR1 $pid && wait $pid || error "wait for PID $pid failed"
5586 }
5587 run_test 95a "Check readpage() on a page that was removed from page cache"
5588
5589 test_95b() {
5590         local file=$DIR/$tfile
5591         local file2=$DIR2/$tfile
5592         local fast_read_save
5593         local pid
5594
5595         fast_read_save=$($LCTL get_param -n llite.*.fast_read | head -n 1)
5596         [ -z "$fast_read_save" ] && skip "no fast read support"
5597
5598         stack_trap "$LCTL set_param llite.*.fast_read=$fast_read_save" EXIT
5599         $LCTL set_param llite.*.fast_read=0
5600
5601         $LFS setstripe -c $OSTCOUNT $file || error "failed to setstripe $file"
5602         dd if=/dev/zero of=$file bs=$((PAGE_SIZE * 3)) count=1 ||
5603                 error "failed to write $file"
5604
5605         # This does the read from the second mount, so this flushes the pages
5606         # the first mount and creates new ones on the second mount
5607         # OBD_FAIL_LLITE_READPAGE_PAUSE2        0x1424
5608         $LCTL set_param fail_loc=0x80001424 fail_val=5
5609         $MULTIOP $file2 or${PAGE_SIZE}c &
5610         pid=$!
5611
5612         sleep 2
5613         fadvise_dontneed_helper $file2
5614         $LCTL set_param fail_loc=0
5615         sleep 4
5616         wait $pid || error "failed to read file"
5617 }
5618 run_test 95b "Check readpage() on a page that is no longer uptodate"
5619
5620 # Data-on-MDT tests
5621 test_100a() {
5622         skip "Reserved for glimpse-ahead" && return
5623         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5624                 skip "Need MDS version at least 2.10.55"
5625
5626         mkdir -p $DIR/$tdir
5627
5628         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5629
5630         lctl set_param -n mdc.*.stats=clear
5631         dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1
5632
5633         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
5634         # first stat from server should return size data and save glimpse
5635         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5636         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
5637         # second stat to check size is NOT cached on client without IO lock
5638         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"
5639
5640         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5641         [ $gls -ge 1 ] || error "Expect glimpse RPCs but none"
5642         rm -f $dom
5643 }
5644 run_test 100a "DoM: glimpse RPCs for stat without IO lock (DoM only file)"
5645
5646 test_100b() {
5647         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5648                 skip "Need MDS version at least 2.10.55"
5649
5650         mkdir -p $DIR/$tdir
5651
5652         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5653
5654         lctl set_param -n mdc.*.stats=clear
5655         dd if=/dev/zero of=$DIR2/$tdir/dom bs=4096 count=1 || return 1
5656         cancel_lru_locks mdc
5657         # first stat data from server should have size
5658         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #1"
5659         # second stat to check size is cached on client
5660         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom || error "stat #2"
5661
5662         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
5663         # both stats should cause no glimpse requests
5664         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
5665         rm -f $dom
5666 }
5667 run_test 100b "DoM: no glimpse RPC for stat with IO lock (DoM only file)"
5668
5669 test_100c() {
5670         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5671                 skip "Need MDS version at least 2.10.55"
5672
5673         mkdir -p $DIR/$tdir
5674
5675         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5676
5677         lctl set_param -n mdc.*.stats=clear
5678         lctl set_param -n osc.*.stats=clear
5679         dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1
5680
5681         # check that size is merged from MDT and OST correctly
5682         $CHECKSTAT -t file -s 2097152 $DIR/$tdir/dom ||
5683                 error "Wrong size from stat #1"
5684
5685         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
5686         [ $gls -eq 0 ] && error "Expect OST glimpse RPCs but got none"
5687
5688         rm -f $dom
5689 }
5690 run_test 100c "DoM: write vs stat without IO lock (combined file)"
5691
5692 test_100d() {
5693         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5694                 skip "Need MDS version at least 2.10.55"
5695
5696         mkdir -p $DIR/$tdir
5697
5698         $LFS setstripe -E 1024K -L mdt -E EOF $DIR/$tdir/dom
5699
5700
5701         dd if=/dev/zero of=$DIR2/$tdir/dom bs=2048K count=1 || return 1
5702         lctl set_param -n mdc.*.stats=clear
5703         $TRUNCATE $DIR2/$tdir/dom 4096
5704
5705         # check that reported size is valid after file grows to OST and
5706         # is truncated back to MDT stripe size
5707         $CHECKSTAT -t file -s 4096 $DIR/$tdir/dom ||
5708                 error "Wrong size from stat #1"
5709
5710         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
5711         [ $gls -eq 0 ] && error "Expect OST glimpse but got none"
5712
5713         rm -f $dom
5714 }
5715 run_test 100d "DoM: write+truncate vs stat without IO lock (combined file)"
5716
5717 test_100e() {
5718         [ "$MDS1_VERSION" -lt $(version_code 2.11.50) ] &&
5719                 skip "Need MDS version at least 2.11.50"
5720
5721         local dom=$DIR/$tdir/dom
5722         local dom2=$DIR2/$tdir/dom
5723         mkdir -p $DIR/$tdir
5724
5725         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
5726
5727         cancel_lru_locks mdc
5728         dd if=/dev/urandom of=$dom bs=12000 count=1
5729         $TRUNCATE $dom2 6000
5730         cancel_lru_locks mdc
5731         lctl set_param -n mdc.*.stats=clear
5732         # expect read-on-open to return all data before write
5733         cat /etc/hosts >> $dom
5734         local read=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
5735         [[ $read -eq 0 ]] || error "Unexpected $read READ RPCs"
5736 }
5737 run_test 100e "DoM: read on open and file size"
5738
5739 test_101a() {
5740         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5741                 skip "Need MDS version at least 2.10.55"
5742
5743         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5744         # to get layout
5745         $CHECKSTAT -t file $DIR1/$tfile
5746
5747         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5748         sysctl -wq vm.dirty_writeback_centisecs=0
5749
5750         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5751
5752         # open + IO lock
5753         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 ||
5754                 error_noexit "Write fails"
5755         # must discard pages
5756         lctl set_param -n mdc.*.stats=clear
5757         rm $DIR2/$tfile || error "Unlink fails"
5758
5759         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5760         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5761 }
5762 run_test 101a "Discard DoM data on unlink"
5763
5764 test_101b() {
5765         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5766                 skip "Need MDS version at least 2.10.55"
5767
5768         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5769         touch $DIR1/${tfile}_2
5770         # to get layout
5771         $CHECKSTAT -t file $DIR1/$tfile
5772
5773         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5774         sysctl -wq vm.dirty_writeback_centisecs=0
5775
5776         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5777
5778         # open + IO lock
5779         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
5780         # must discard pages
5781         lctl set_param -n mdc.*.stats=clear
5782         mv $DIR2/${tfile}_2 $DIR2/$tfile || error "Rename fails"
5783
5784         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5785         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5786 }
5787 run_test 101b "Discard DoM data on rename"
5788
5789 test_101c() {
5790         [ "$MDS1_VERSION" -lt $(version_code 2.10.55) ] &&
5791                 skip "Need MDS version at least 2.10.55"
5792
5793         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
5794         # to get layout
5795         $CHECKSTAT -t file $DIR1/$tfile
5796
5797         local old_wb=$(sysctl -n vm.dirty_writeback_centisecs)
5798         sysctl -wq vm.dirty_writeback_centisecs=0
5799
5800         trap "sysctl -wq vm.dirty_writeback_centisecs=$old_wb" EXIT
5801
5802         # open + IO lock
5803         dd if=/dev/zero of=$DIR1/$tfile bs=4096 count=1 || error "Write fails"
5804         $MULTIOP $DIR1/$tfile O_c &
5805         MULTIOP_PID=$!
5806         sleep 1
5807         lctl set_param -n mdc.*.stats=clear
5808         rm $DIR2/$tfile > /dev/null || error "Unlink fails for opened file"
5809         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
5810
5811         local writes=$(lctl get_param -n mdc.*.stats | grep -c ost_write)
5812         [ $writes -eq 0 ] || error "Found WRITE RPC but expect none"
5813 }
5814 run_test 101c "Discard DoM data on close-unlink"
5815
5816 # test to verify file handle related system calls
5817 # (name_to_handle_at/open_by_handle_at)
5818 # The new system calls are supported in glibc >= 2.14.
5819
5820 # test to verify we can open by handle an unlinked file from > 1 client
5821 # This test opens the file normally on $DIR1, which is on one mount, and then
5822 # opens it by handle on $DIR2, which is on a different mount.
5823 test_102() {
5824         (( "$MDS1_VERSION" >= $(version_code 2.11.57) )) ||
5825                 skip "Needs MDS version 2.11.57 or later"
5826
5827         echo "Test file_handle syscalls" > $DIR/$tfile ||
5828                 error "write failed"
5829         check_fhandle_syscalls $DIR/$tfile $DIR2 ||
5830                 error "check_fhandle_syscalls $tfile failed"
5831
5832         # test this is working on DNE directories also
5833         if (( MDSCOUNT > 1 && MDS1_VERSION >= $(version_code 2.14.52) )); then
5834                 $LFS mkdir -i 1 $DIR/$tdir.remote
5835                 cancel_lru_locks mdc
5836                 check_fhandle_syscalls $DIR/$tdir.remote $DIR2 ||
5837                         error "check_fhandle_syscalls $tdir.remote failed"
5838                 $LFS mkdir -c -1 $DIR/$tdir.remote/subdir
5839                 cancel_lru_locks mdc
5840                 check_fhandle_syscalls $DIR/$tdir.remote/subdir $DIR2 ||
5841                         error "check_fhandle_syscalls $tdir.remote/subdir fail"
5842
5843                 $LFS mkdir -c -1 $DIR/$tdir.stripe
5844                 cancel_lru_locks mdc
5845                 check_fhandle_syscalls $DIR/$tdir.stripe $DIR2 ||
5846                         error "check_fhandle_syscalls $tdir.stripe failed"
5847                 $LFS mkdir -c -1 $DIR/$tdir.stripe/subdir
5848                 cancel_lru_locks mdc
5849                 check_fhandle_syscalls $DIR/$tdir.stripe/subdir $DIR2 ||
5850                         error "check_fhandle_syscalls $tdir.stripe/subdir fail"
5851         fi
5852 }
5853 run_test 102 "Test open by handle of unlinked file"
5854
5855 # Compare file size between first & second mount, ensuring the client correctly
5856 # glimpses even with unused speculative locks - LU-11670
5857 test_103() {
5858         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
5859                 skip "Lockahead needs OST version at least 2.10.50"
5860
5861         local locktest=23
5862
5863         test_mkdir -p $DIR/$tdir
5864
5865         # Force file on to OST0
5866         $LFS setstripe -i 0 $DIR/$tdir
5867
5868         # Do not check multiple locks on glimpse
5869         # OBD_FAIL_OSC_NO_SIZE_DATA 0x415
5870         $LCTL set_param fail_loc=0x415
5871
5872         # Delay write commit by 2 seconds to guarantee glimpse wins race
5873         # The same fail_loc is used on client & server so it can work in the
5874         # single node sanity setup
5875         do_facet ost1 $LCTL set_param fail_loc=0x415 fail_val=2
5876
5877         echo "Incorrect size expected (no glimpse fix):"
5878         lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
5879         rc=$?
5880         if [ $rc -eq 0 ]; then
5881                 echo "This doesn't work 100%, but this is just reproducing the bug, not testing the fix, so OK to not fail test."
5882         fi
5883
5884         # guarantee write commit timeout has expired
5885         sleep 2
5886
5887         # Clear fail_loc on client
5888         $LCTL set_param fail_loc=0
5889
5890         # Delay write commit by 2 seconds to guarantee glimpse wins race
5891         # OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
5892         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
5893
5894         # Write commit is still delayed by 2 seconds
5895         lockahead_test -d $DIR/$tdir -D $DIR2/$tdir -t $locktest -f $tfile
5896         rc=$?
5897         [ $rc -eq 0 ] || error "Lockahead test$locktest failed, $rc"
5898
5899         # guarantee write commit timeout has expired
5900         sleep 2
5901
5902         rm -f $DIR/$tfile || error "unable to delete $DIR/$tfile"
5903 }
5904 run_test 103 "Test size correctness with lockahead"
5905
5906 get_stat_xtimes()
5907 {
5908         local xtimes
5909
5910         xtimes=$(stat -c "%X %Y %Z" $DIR/$tfile)
5911
5912         echo ${xtimes[*]}
5913 }
5914
5915 get_mdt_xtimes()
5916 {
5917         local mdtdev=$1
5918         local output
5919         local xtimes
5920
5921         output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
5922         ((xtimes[0]=$(awk -F ':' /atime/'{ print $2 }' <<< "$output")))
5923         ((xtimes[1]=$(awk -F ':' /mtime/'{ print $2 }' <<< "$output")))
5924         ((xtimes[2]=$(awk -F ':' /ctime/'{ print $2 }' <<< "$output")))
5925
5926         echo ${xtimes[*]}
5927 }
5928
5929 check_mdt_xtimes()
5930 {
5931         local mdtdev=$1
5932         local xtimes=($(get_stat_xtimes))
5933         local mdt_xtimes=($(get_mdt_xtimes $mdtdev))
5934
5935         echo "STAT a|m|ctime ${xtimes[*]}"
5936         echo "MDT a|m|ctime ${mdt_xtimes[*]}"
5937         [[ ${xtimes[0]} == ${mdt_xtimes[0]} ]] ||
5938                 error "$DIR/$tfile atime (${xtimes[0]}:${mdt_xtimes[0]}) diff"
5939         [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
5940                 error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
5941         [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
5942                 error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
5943 }
5944
5945 test_104() {
5946         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"
5947         [ $MDS1_VERSION -lt $(version_code 2.12.4) ] &&
5948                 skip "Need MDS version at least 2.12.4"
5949
5950         local pid
5951         local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
5952         local atime_diff=$(do_facet $SINGLEMDS \
5953                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5954
5955         do_facet $SINGLEMDS \
5956                 lctl set_param -n mdd.*MDT0000*.atime_diff=0
5957
5958         stack_trap "do_facet $SINGLEMDS \
5959                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff" EXIT
5960
5961         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5962         check_mdt_xtimes $mdtdev
5963         sleep 2
5964
5965         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5966         check_mdt_xtimes $mdtdev
5967         sleep 2
5968         $MULTIOP $DIR2/$tfile Oz8192w8192_c &
5969         pid=$!
5970         sleep 2
5971         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
5972         sleep 2
5973         kill -USR1 $pid && wait $pid || error "multiop failure"
5974         check_mdt_xtimes $mdtdev
5975
5976         local xtimes
5977         local mdt_xtimes
5978
5979         # Verify mtime/ctime is NOT upated on MDS when there is no modification
5980         # on the client side
5981         xtimes=($(get_stat_xtimes))
5982         $MULTIOP $DIR/$tfile O_c &
5983         pid=$!
5984         sleep 2
5985         kill -USR1 $pid && wait $pid || error "multiop failure"
5986         mdt_xtimes=($(get_mdt_xtimes $mdtdev))
5987         [[ ${xtimes[1]} == ${mdt_xtimes[1]} ]] ||
5988                 error "$DIR/$tfile mtime (${xtimes[1]}:${mdt_xtimes[1]}) diff"
5989         [[ ${xtimes[2]} == ${mdt_xtimes[2]} ]] ||
5990                 error "$DIR/$tfile ctime (${xtimes[2]}:${mdt_xtimes[2]}) diff"
5991         check_mdt_xtimes $mdtdev
5992
5993         sleep 2
5994         # Change ctime via chmod
5995         $MULTIOP $DIR/$tfile o_tc &
5996         pid=$!
5997         sleep 2
5998         kill -USR1 $pid && wait $pid || error "multiop failure"
5999         check_mdt_xtimes $mdtdev
6000 }
6001 run_test 104 "Verify that MDS stores atime/mtime/ctime during close"
6002
6003 test_105() {
6004         test_mkdir -p $DIR/$tdir
6005         echo test > $DIR/$tdir/$tfile
6006         $LCTL set_param fail_loc=0x416
6007         cancel_lru_locks osc & sleep 1
6008         fsize1=$(stat -c %s $DIR2/$tdir/$tfile)
6009         wait
6010         [[ $fsize1 = 5 ]] ||  error "Glimpse returned wrong file size $fsize1"
6011 }
6012 run_test 105 "Glimpse and lock cancel race"
6013
6014 test_106a() {
6015         [ "$mds1_FSTYPE" == "ldiskfs" ] && statx_supported ||
6016                 skip_env "Test only for ldiskfs and statx() supported"
6017
6018         local btime
6019         local mdt_btime
6020         local output
6021         local mdtdev=$(mdsdevname ${SINGLEMDS//mds/})
6022
6023         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
6024         btime=$($STATX -c %W $DIR/$tfile)
6025         output=$(do_facet mds1 "$DEBUGFS -c -R 'stat ROOT/$tfile' $mdtdev")
6026         echo $output
6027         ((mdt_btime=$(awk -F ':' /crtime/'{ print $2 }' <<< "$output")))
6028         [[ $btime == $mdt_btime ]] ||
6029                 error "$DIR/$tfile btime ($btime:$mdt_btime) diff"
6030
6031 }
6032 run_test 106a "Verify the btime via statx()"
6033
6034 test_106b() {
6035         statx_supported || skip_env "statx() only test"
6036
6037         local rpcs_before
6038         local rpcs_after
6039
6040         $LFS setstripe -c 1 $DIR/$tfile || error "$DIR/$tfile setstripe failed"
6041         dd if=/dev/zero of=$DIR/$tfile bs=1k count=1 conv=notrunc
6042         cancel_lru_locks $OSC
6043         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6044         $STATX $DIR/$tfile
6045         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6046         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
6047                 error "$STATX should send 1 glimpse RPC to $OSC"
6048
6049         cancel_lru_locks $OSC
6050         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6051         # %n: FILENAME; %i: STATX_INO; %A STATX_MODE; %h STATX_NLINK;
6052         # %u: STATX_UID; %g: STATX_GID; %W STATX_BTIME; %X STATX_ATIME;
6053         # %Z: STATX_CTIME
6054         $STATX -c "%n %i %A %h %u %g %W %X %Z" $DIR/$tfile
6055         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6056         [ $rpcs_after -eq $rpcs_before ] ||
6057                 error "$STATX should not send glimpse RPCs to $OSC"
6058
6059         cancel_lru_locks $OSC
6060         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6061         $STATX --cached=always $DIR/$tfile
6062         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6063         [ $rpcs_after -eq $rpcs_before ] ||
6064                 error "$STATX should not send glimpse RPCs to $OSC"
6065
6066         cancel_lru_locks $OSC
6067         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6068         $STATX -c %Y $DIR/$tfile
6069         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6070         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
6071                 error "$STATX -c %Y should send 1 glimpse RPC to $OSC"
6072
6073         cancel_lru_locks $OSC
6074         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6075         $STATX -c %s $DIR/$tfile
6076         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6077         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
6078                 error "$STATX -c %s should send 1 glimpse RPC to $OSC"
6079
6080         cancel_lru_locks $OSC
6081         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6082         $STATX -c %b $DIR/$tfile
6083         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6084         [ $rpcs_after -eq $((rpcs_before + 1)) ] ||
6085                 error "$STATX -c %b should send 1 glimpse RPC to $OSC"
6086 }
6087 run_test 106b "Glimpse RPCs test for statx"
6088
6089 test_106c() {
6090         statx_supported || skip_env "statx() only test"
6091
6092         local mask
6093
6094         touch $DIR/$tfile
6095         # Mask supported in stx_attributes by Lustre is
6096         # STATX_ATTR_IMMUTABLE(0x10) | STATX_ATTR_APPEND(0x20) : (0x30).
6097         mask=$($STATX -c %p $DIR/$tfile)
6098         (( (0x$mask & 0x30) == 0x30 )) ||
6099                 error "supported stx_attributes: got '0x$mask', expected '0x30' at least"
6100         chattr +i $DIR/$tfile || error "chattr +i $DIR/$tfile failed"
6101         mask=$($STATX -c %r $DIR/$tfile)
6102         [[ $mask == "10" ]] ||
6103                 error "got immutable flags '$mask', expected '10'"
6104         chattr -i $DIR/$tfile || error "chattr -i $DIR/$tfile failed"
6105         mask=$($STATX -c %r $DIR/$tfile)
6106         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
6107         chattr +a $DIR/$tfile || error "chattr +a $DIR/$tfile failed"
6108         mask=$($STATX -c %r $DIR/$tfile)
6109         [[ $mask == "20" ]] || error "got flags '$mask', expected '20'"
6110         chattr -a $DIR/$tfile || error "chattr -a $DIR/$tfile failed"
6111         mask=$($STATX -c %r $DIR/$tfile)
6112         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
6113         chattr +ia $DIR/$tfile || error "chattr +ia $DIR/$tfile failed"
6114         mask=$($STATX -c %r $DIR/$tfile)
6115         [[ $mask == "30" ]] || error "got flags '$mask', expected '30'"
6116         chattr -ia $DIR/$tfile || error "chattr -ia $DIR/$tfile failed"
6117         mask=$($STATX -c %r $DIR/$tfile)
6118         [[ $mask == "0" ]] || error "got flags '$mask', expected '0'"
6119 }
6120 run_test 106c "Verify statx attributes mask"
6121
6122 test_107a() { # LU-1031
6123         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
6124         local gid1=14091995
6125         local gid2=16022000
6126
6127         $LFS getstripe $DIR1/$tfile
6128
6129         multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
6130         local MULTIPID1=$!
6131         multiop_bg_pause $DIR2/$tfile O_G${gid2}r10g${gid2}c || return 2
6132         local MULTIPID2=$!
6133         kill -USR1 $MULTIPID2
6134         sleep 2
6135         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
6136                 error "First grouplock does not block second one"
6137         else
6138                 echo "First grouplock blocks second one"
6139         fi
6140         kill -USR1 $MULTIPID1
6141         wait $MULTIPID1
6142         wait $MULTIPID2
6143 }
6144 run_test 107a "Basic grouplock conflict"
6145
6146 test_107b() {
6147         dd if=/dev/zero of=$DIR1/$tfile bs=1M count=10
6148         local gid1=14091995
6149         local gid2=16022000
6150
6151         $LFS getstripe $DIR1/$tfile
6152
6153         multiop_bg_pause $DIR1/$tfile OG${gid1}_g${gid1}c || return 1
6154         local MULTIPID1=$!
6155         multiop $DIR2/$tfile Or10c &
6156         local MULTIPID2=$!
6157         sleep 2
6158
6159         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
6160                 error "Grouplock does not block IO"
6161         else
6162                 echo "Grouplock blocks IO"
6163         fi
6164
6165         multiop $DIR2/$tfile OG${gid2}_g${gid2}c &
6166         local MULTIPID3=$!
6167         sleep 2
6168         if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
6169                 error "First grouplock does not block second one"
6170         else
6171                 echo "First grouplock blocks second one"
6172         fi
6173
6174         kill -USR1 $MULTIPID1
6175         sleep 2
6176
6177         if [[ $(ps h -o comm -p $MULTIPID3) == "" ]]; then
6178                 error "Second grouplock thread disappeared"
6179         fi
6180
6181         if [[ $(ps h -o comm -p $MULTIPID2) == "" ]]; then
6182                 error "Second grouplock does not block IO"
6183         else
6184                 echo "Second grouplock blocks IO"
6185         fi
6186
6187         kill -USR1 $MULTIPID3
6188         wait $MULTIPID1
6189         wait $MULTIPID2
6190         wait $MULTIPID3
6191 }
6192 run_test 107b "Grouplock is added to the head of waiting list"
6193
6194 test_108a() {
6195         local offset
6196
6197         $LFS setstripe -E 1M -c 1 -E -1 $DIR1/$tfile ||
6198                 error "Create $DIR1/$tfile failed"
6199
6200         dd if=/dev/zero of=$DIR1/$tfile bs=10000 count=1 ||
6201                 error "dd $DIR1/$tfile failed"
6202         offset=$(lseek_test -d 5000 $DIR2/$tfile)
6203         [[ $offset == 5000 ]] || error "offset $offset != 5000"
6204
6205         $TRUNCATE $DIR1/$tfile 2000
6206         offset=$(lseek_test -l 1000 $DIR2/$tfile)
6207         [[ $offset == 2000 ]] || error "offset $offset != 2000"
6208
6209         #define OBD_FAIL_OSC_DELAY_IO 0x414
6210         $LCTL set_param fail_val=4 fail_loc=0x80000414
6211         dd if=/dev/zero of=$DIR1/$tfile count=1 bs=8M conv=notrunc oflag=dsync &
6212         local pid=$!
6213         sleep 2
6214
6215         offset=$(lseek_test -l 8000 $DIR2/$tfile)
6216         wait $pid
6217         [[ $offset == 8388608 ]] || error "offset $offset != 8388608"
6218 }
6219 run_test 108a "lseek: parallel updates"
6220
6221 # LU-14110
6222 test_109() {
6223         local i
6224         local pid1 pid2
6225
6226         ! local_mode ||
6227                 skip "Clients need to be on different nodes than the servers"
6228
6229         umount_client $MOUNT
6230         umount_client $MOUNT2
6231
6232         echo "Starting race between client mount instances (50 iterations):"
6233         for i in {1..50}; do
6234                 log "Iteration $i"
6235
6236 #define CFS_FAIL_ONCE|OBD_FAIL_LLITE_RACE_MOUNT        0x80001417
6237                 $LCTL set_param -n fail_loc=0x80001417
6238
6239                 mount_client $MOUNT  & pid1=$!
6240                 mount_client $MOUNT2 & pid2=$!
6241                 wait $pid1 || error "Mount $MOUNT fails with $?"
6242                 wait $pid2 || error "Mount $MOUNT2 fails with $?"
6243
6244                 umount_client $MOUNT  & pid1=$!
6245                 umount_client $MOUNT2 & pid2=$!
6246                 wait $pid1 || error "Umount $MOUNT fails with $?"
6247                 wait $pid2 || error "Umount $MOUNT2 fails with $?"
6248
6249                 $LUSTRE_RMMOD || error "Fail to remove lustre modules"
6250                 load_modules
6251                 echo
6252         done
6253
6254         mount_client $MOUNT
6255         mount_client $MOUNT2
6256 }
6257
6258 run_test 109 "Race with several mount instances on 1 node"
6259
6260 test_110() {
6261         local before=$(date +%s)
6262         local evict
6263
6264         mkdir -p $DIR/$tdir
6265         touch $DIR/$tdir/f1
6266         touch $DIR/$tfile
6267
6268         #define OBD_FAIL_PTLRPC_RESEND_RACE      0x525
6269         do_facet mds1 lctl set_param fail_loc=0x525 fail_val=3
6270
6271         # disable last_xid logic by dropping link reply
6272         ln $DIR/$tdir/f1 $DIR/$tdir/f2 &
6273         sleep 1
6274
6275         #define OBD_FAIL_PTLRPC_ENQ_RESEND      0x534
6276         do_facet mds1 lctl set_param fail_loc=0x534
6277
6278         # RPC will race with its Resend and the Resend will sleep to let
6279         # the original lock to get granted & cancelled.
6280         #
6281         # AST_SENT is set artificially, so an explicit conflict is not needed
6282         #
6283         # The woken up Resend gets a new lock, but client does not wait for it
6284         stat $DIR/$tfile
6285         sleep $TIMEOUT
6286         do_facet mds1 lctl set_param fail_loc=0 fail_val=0
6287
6288         # Take a conflict to wait long enough to see the eviction
6289         touch $DIR2/$tfile
6290
6291         # let the client reconnect
6292         client_reconnect
6293         evict=$(do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state |
6294           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
6295
6296         [ -z "$evict" ] || [[ $evict -le $before ]] ||
6297                 (do_facet client $LCTL get_param mdc.$FSNAME-MDT*.state;
6298                     error "eviction happened: $evict before:$before")
6299 }
6300 run_test 110 "do not grant another lock on resend"
6301
6302 test_111() {
6303         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
6304         [[ $(facet_active_host mds1) = $(facet_active_host mds2) ]] ||
6305                 skip "MDT0 and MDT1 should be on the same node"
6306
6307         mkdir $DIR1/$tdir
6308         $LFS mkdir -i 0 $DIR1/$tdir/mdt0dir
6309         $LFS mkdir -i 1 $DIR1/$tdir/mdt1dir
6310
6311         mkdir $DIR1/$tdir/mdt0dir/foodir
6312         touch $DIR1/$tdir/mdt0dir/foodir/{file1,file2}
6313
6314         $MULTIOP $DIR2/$tdir/mdt0dir/foodir/file2 Ow4096_c &
6315         MULTIOP_PID=$!
6316         ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2
6317
6318         #define OBD_FAIL_MDS_LINK_RENAME_RACE   0x18a
6319         do_facet mds1 $LCTL set_param fail_loc=0x8000018a
6320
6321         ln $DIR1/$tdir/mdt0dir/foodir/file2 $DIR1/$tdir/mdt1dir/file2x &
6322         sleep 1
6323
6324         rm $DIR2/$tdir/mdt1dir/file2
6325         sleep 1
6326
6327         mv $DIR2/$tdir/mdt0dir/foodir/file1 $DIR2/$tdir/mdt0dir/foodir/file2
6328         sleep 1
6329
6330         kill $MULTIOP_PID
6331         wait
6332         rm -r $DIR1/$tdir || error "Removing test dir failed"
6333 }
6334 run_test 111 "A racy rename/link an open file should not cause fs corruption"
6335
6336 test_112() {
6337         (( MDSCOUNT >= 2 )) ||
6338                 skip "We need at least 2 MDTs for this test"
6339
6340         (( MDS1_VERSION >= $(version_code 2.14.54) )) ||
6341                 skip "Need server version at least 2.14.54"
6342
6343         local rr
6344         local count
6345
6346         rr=$($LCTL get_param -n lmv.*.qos_threshold_rr | head -n1)
6347         rr=${rr%%%}
6348         stack_trap "$LCTL set_param lmv.*.qos_threshold_rr=$rr > /dev/null"
6349
6350         mkdir -p $DIR1/$tdir/s1/s2 || error "mkdir s2 failed"
6351         $LFS mkdir -i 0 $DIR1/$tdir/s1/s2/s3 || error "mkdir s3 failed"
6352         $LFS setdirstripe -D -i -1 --max-inherit-rr=0 $DIR1/$tdir/s1/s2/s3 ||
6353                 error "setdirstripe s3 failed"
6354         $LCTL set_param lmv.*.qos_threshold_rr=90
6355         mkdir $DIR2/$tdir/s1/s2/s3/d{1..64}
6356         count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/d* | grep ^0 | wc -l)
6357         (( count == 64 )) || error "only $count subdirs created on MDT0"
6358
6359         $LFS setdirstripe -D -i -1 --max-inherit-rr=3 $DIR1/$tdir/s1/s2/s3 ||
6360                 error "setdirstripe s3 failed"
6361         mkdir $DIR2/$tdir/s1/s2/s3/s{1..64}
6362         count=$($LFS getstripe -m $DIR2/$tdir/s1/s2/s3/s* | grep ^0 | wc -l)
6363         (( count == 64 / MDSCOUNT )) || error "$count subdirs created on MDT0"
6364 }
6365 run_test 112 "update max-inherit in default LMV"
6366
6367 test_113 () {
6368         (( MDS1_VERSION >= $(version_code 2.15.50) )) ||
6369                 skip "Need server version at least 2.15.50"
6370
6371         local instance
6372         local nid
6373
6374         instance=$($LFS getname -i $DIR1) ||
6375                 error "cannot get instance of $DIR1"
6376
6377         $LFS check osts $DIR1 | grep $instance ||
6378                 error "cannot find OSTs of instance $instance"
6379
6380         $LFS check osts $DIR1 | grep -v $instance
6381         if (( $? == 0 )); then
6382                 error "find OSTs other than instance $instance"
6383         fi
6384
6385         $LFS check osts | grep $instance ||
6386                 error "cannot find other OSTs"
6387
6388         nid=$(df $DIR2 | tail -1 | sed 's%:/.*%%') ||
6389                 error "cannot parse nid for $DIR2"
6390
6391         $LFS check mgts $DIR2 | grep MGC$nid ||
6392                 error "cannot find mgc of $nid"
6393
6394         $LFS check mgts $DIR2 | grep -v MGC$nid
6395         if (( $? == 0 )); then
6396                 error "find MGTs other than nid $nid"
6397         fi
6398 }
6399 run_test 113 "check servers of specified fs"
6400
6401 check_default_lmv() {
6402         local dir=$1
6403
6404         local enabled
6405         local dmv
6406         local index
6407         local count
6408         local inherit
6409         local inherit_rr
6410         local raw
6411
6412         enabled=$(do_facet mds1 \
6413                 $LCTL get_param -n mdt.*-MDT0000*.enable_dmv_implicit_inherit)
6414
6415         dmv=$($LFS getdirstripe -D $dir)
6416         echo $dir $dmv
6417         index=$(echo $dmv | awk '{ print $4 }')
6418         (( index == $2 )) || error "$dir default stripe index $index != $2"
6419
6420         count=$(echo $dmv | awk '{ print $2 }')
6421         (( count == $3 )) || error "$dir default stripe count $count != $3"
6422
6423         inherit=$(echo $dmv | awk '{ print $8 }')
6424         (( inherit == $4 )) || error "$dir default max-inherit $inherit != $4"
6425
6426         if [ $index -eq -1 ]; then
6427                 inherit_rr=$(echo $dmv | awk '{ print $10 }')
6428                 (( inherit_rr == $5 )) ||
6429                         error "$dir default max-inherit-rr $inherit_rr != $5"
6430         fi
6431
6432         # with --raw, print default LMV stored in inode, otherwise print nothing
6433         raw=$($LFS getdirstripe -D --raw $dir)
6434         if (( enabled == 1 )); then
6435                 [ -z $raw ] ||
6436                         error "implicit inherited DMV is printed with --raw"
6437         else
6438                 # if disabled, dmv is stored in inode, which will always
6439                 # print max-inherit-rr
6440                 echo $dir $raw
6441                 [[ $raw =~ $dmv.* ]] || error "$dir raw $raw != dmv $dmv"
6442         fi
6443 }
6444
6445 test_dmv_imp_inherit() {
6446         local dmv
6447         local raw
6448         local index
6449         local count
6450         local inherit
6451         local inherit_rr
6452
6453         rm -rf $DIR/$tdir || error "rm $tdir failed"
6454         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
6455
6456         # set dir default LMV
6457         $LFS setdirstripe -D -c1 -X4 --max-inherit-rr 2 $DIR/$tdir ||
6458                 error "setdirstripe -D $tdir failed"
6459         dmv=$($LFS getdirstripe -D $DIR/$tdir)
6460         raw=$($LFS getdirstripe -D --raw $DIR/$tdir)
6461         [ "$dmv" == "$raw" ] || error "$dmv != $raw"
6462
6463         mkdir -p $DIR/$tdir/l1/l2/l3 || error "mkdir $DIR/$tdir/l1/l2/l3 failed"
6464         check_default_lmv $DIR/$tdir/l1/l2/l3 -1 1 1 0
6465         check_default_lmv $DIR2/$tdir/l1/l2/l3 -1 1 1 0
6466
6467         # below tests are valid only when this feature is enabled
6468         local enabled=$(do_facet mds1 \
6469                 $LCTL get_param -n mdt.*-MDT0000*.enable_dmv_implicit_inherit)
6470
6471         (( enabled == 1 )) || return 0
6472
6473         # set l2 default LMV, dmv of l3 should change immediately
6474         $LFS setdirstripe -D -i1 -c2 -X4 $DIR/$tdir/l1/l2 ||
6475                 error "setdirstripe -D $tdir/l1/l2 failed"
6476
6477         check_default_lmv $DIR/$tdir/l1/l2/l3 1 2 3
6478         check_default_lmv $DIR2/$tdir/l1/l2/l3 1 2 3
6479
6480         # change tdir default LMV, dmv of l3 should be unchanged because dmv
6481         # of l2 is explicitly set
6482         $LFS setdirstripe -D -i2 -c2 -X3 $DIR/$tdir ||
6483                 error "setdirstripe -D $tdir failed"
6484
6485         check_default_lmv $DIR/$tdir/l1 2 2 2
6486         check_default_lmv $DIR2/$tdir/l1 2 2 2
6487         check_default_lmv $DIR/$tdir/l1/l2/l3 1 2 3
6488         check_default_lmv $DIR2/$tdir/l1/l2/l3 1 2 3
6489 }
6490
6491 test_114() {
6492         (( MDSCOUNT >= 2 )) ||
6493                 skip "We need at least 2 MDTs for this test"
6494
6495         (( MDS1_VERSION >= $(version_code 2.15.55.45) )) ||
6496                 skip "Need server version at least 2.15.54.45"
6497
6498         test_dmv_imp_inherit
6499
6500         # disable dmv_imp_inherit to simulate old client
6501         local mdts=$(comma_list $(mdts_nodes))
6502
6503         do_nodes $mdts $LCTL set_param -n \
6504                 mdt.*MDT*.enable_dmv_implicit_inherit=0
6505         test_dmv_imp_inherit
6506         do_nodes $mdts $LCTL set_param -n \
6507                 mdt.*MDT*.enable_dmv_implicit_inherit=1
6508 }
6509 run_test 114 "implicit default LMV inherit"
6510
6511 test_115() {
6512         local td=$DIR/$tdir
6513
6514         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip_env "ldiskfs only test"
6515
6516         mkdir_on_mdt0 $td || error "can't mkdir"
6517         # turn it htree (don't really needed)
6518         createmany -m $td/f 3000 || error "can't createmany"
6519
6520         # here is an example of debugfs output for htree command:
6521         # Entry #0: Hash 0x00000000, block 27
6522         # Reading directory block 27, phys 16760
6523         # 938 0x0016fb58-7f3d21f5 (32) f775   834 0x001db8c8-d31a4e0e (32) f671
6524         # 1085 0x0040cb70-4498abd4 (32) f922   1850 0x0066a1e6-f6f0dc69 (32) f1687
6525         # 2005 0x006c1a46-ef466058 (32) f1842   2025 0x007e64d4-8b28b734 (32) f1862
6526         # 642 0x008b53a0-77adc601 (32) f479   447 0x009ec152-af54eea3 (32) f284
6527         # 1740 0x00c38f56-ed310e61 (32) f1577   2165 0x00cdfd66-f429a93f (32) f2002
6528         # 930 0x00d7ada4-b80421c9 (32) f767   1946 0x00da6a7a-e8080600 (32) f1783
6529         # 273 0x00f8ea00-760bf97c (32) f110   1589 0x0103c4ee-94fad5dd (32) f1426
6530         # 1383 0x01193516-83120b48 (32) f1220   2379 0x01431e3c-e85b5bd9 (32) f2216
6531         #
6532         # find couple names in a same htree block of the same size
6533         mdt_dev=$(facet_device $SINGLEMDS)
6534         de=( $(do_facet $SINGLEMDS "debugfs -c -R 'htree /ROOT/$tdir' $mdt_dev" |
6535                 awk '/Reading directory block/ { getline; print $4,$8; exit; }' ))
6536         local de1=${de[0]}
6537         local de2=${de[1]}
6538         [[ $de1 == "" || $de2 == "" ]] && error "de1=$de1 de2=$de2"
6539         echo "USE: $de1 $de2"
6540         # release one mkdir will lookup
6541         rm $DIR/$tdir/$de2
6542 #define OBD_FAIL_MDS_PAUSE_CREATE_AFTER_LOOKUP  0x2401
6543         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x80002401 fail_val=5
6544         mkdir $DIR/$tdir/$de2 &
6545         sleep 0.3
6546         local PID1=$!
6547         # recreate $de2
6548         mkdir $DIR2/$tdir/$de2
6549         # release space $de1 (should be enough to save $de2)
6550         rm $DIR2/$tdir/$de1
6551         # ready to create a dup of $de2
6552         wait $PID1
6553         local found=$(ls $DIR/$tdir/|grep "^$de2\$"|wc -l)
6554         (( $found == 1 )) || error "found $found"
6555 }
6556 run_test 115 "ldiskfs doesn't check direntry for uniqueness"
6557
6558 log "cleanup: ======================================================"
6559
6560 # kill and wait in each test only guarentee script finish, but command in script
6561 # like 'rm' 'chmod' may still be running, wait for all commands to finish
6562 # otherwise umount below will fail
6563 [ "$(mount | grep $MOUNT2)" ] && wait_update $HOSTNAME "fuser -m $MOUNT2" "" ||
6564         true
6565
6566 complete_test $SECONDS
6567 rm -f $SAMPLE_FILE
6568 check_and_cleanup_lustre
6569 exit_status