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