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