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