Whamcloud - gitweb
LU-10120 lsnapshot: handle dash in fsname
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c     77k"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 LU-10734 ..
16 ALWAYS_EXCEPT="  407     253     312     160g     $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 SRCDIR=$(cd $(dirname $0); echo $PWD)
22 export PATH=$PATH:/sbin
23
24 TMP=${TMP:-/tmp}
25 OSC=${OSC:-"osc"}
26
27 CC=${CC:-cc}
28 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
29 CREATETEST=${CREATETEST:-createtest}
30 LFS=${LFS:-lfs}
31 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
32 LCTL=${LCTL:-lctl}
33 OPENFILE=${OPENFILE:-openfile}
34 OPENUNLINK=${OPENUNLINK:-openunlink}
35 export MULTIOP=${MULTIOP:-multiop}
36 READS=${READS:-"reads"}
37 MUNLINK=${MUNLINK:-munlink}
38 SOCKETSERVER=${SOCKETSERVER:-socketserver}
39 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
40 MEMHOG=${MEMHOG:-memhog}
41 DIRECTIO=${DIRECTIO:-directio}
42 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
43 DEF_STRIPE_COUNT=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
57 . $LUSTRE/tests/test-framework.sh
58 init_test_env $@
59 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
60 init_logging
61
62 #                                  5          12          (min)"
63 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
64
65 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
66         # bug number for skipped test: LU-1957
67         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
68         #                                               13    (min)"
69         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
70 fi
71
72 # Get the SLES distro version
73 #
74 # Returns a version string that should only be used in comparing
75 # strings returned by version_code()
76 sles_version_code()
77 {
78         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
79
80         # All SuSE Linux versions have one decimal. version_code expects two
81         local sles_version=$version.0
82         version_code $sles_version
83 }
84
85 # Check if we are running on Ubuntu or SLES so we can make decisions on
86 # what tests to run
87 if [ -r /etc/SuSE-release ]; then
88         sles_version=$(sles_version_code)
89         [ $sles_version -lt $(version_code 11.4.0) ] &&
90                 # bug number for skipped test: LU-4341
91                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
92         [ $sles_version -lt $(version_code 12.0.0) ] &&
93                 # bug number for skipped test: LU-3703
94                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
95 elif [ -r /etc/os-release ]; then
96         if grep -qi ubuntu /etc/os-release; then
97                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
98                                                 -e 's/^VERSION=//p' \
99                                                 /etc/os-release |
100                                                 awk '{ print $1 }'))
101
102                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
103                         # bug number for skipped test:
104                         #                LU-10334 LU-10335 LU-10335 LU-10335
105                         ALWAYS_EXCEPT+=" 103a     130a     130b     130c"
106                         #                LU-10335 LU-10335 LU-10365 LU-10366
107                         ALWAYS_EXCEPT+=" 130d     130e     400a     410"
108                 fi
109         fi
110 fi
111
112 FAIL_ON_ERROR=false
113
114 cleanup() {
115         echo -n "cln.."
116         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
117         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
118 }
119 setup() {
120         echo -n "mnt.."
121         load_modules
122         setupall || exit 10
123         echo "done"
124 }
125
126 check_swap_layouts_support()
127 {
128         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
129                 skip "Does not support layout lock."
130 }
131
132 check_and_setup_lustre
133 DIR=${DIR:-$MOUNT}
134 assert_DIR
135
136 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
137
138 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
139 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
140 rm -rf $DIR/[Rdfs][0-9]*
141
142 # $RUNAS_ID may get set incorrectly somewhere else
143 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
144
145 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
146
147 build_test_filter
148
149 if [ "${ONLY}" = "MOUNT" ] ; then
150         echo "Lustre is up, please go on"
151         exit
152 fi
153
154 echo "preparing for tests involving mounts"
155 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
156 touch $EXT2_DEV
157 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
158 echo # add a newline after mke2fs.
159
160 umask 077
161
162 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
163 lctl set_param debug=-1 2> /dev/null || true
164 test_0a() {
165         touch $DIR/$tfile
166         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
167         rm $DIR/$tfile
168         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
169 }
170 run_test 0a "touch; rm ====================="
171
172 test_0b() {
173         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
174         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
175 }
176 run_test 0b "chmod 0755 $DIR ============================="
177
178 test_0c() {
179         $LCTL get_param mdc.*.import | grep "state: FULL" ||
180                 error "import not FULL"
181         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
182                 error "bad target"
183 }
184 run_test 0c "check import proc"
185
186 test_0d() { # LU-3397
187         [ $(lustre_version_code mgs) -lt $(version_code 2.10.57) ] &&
188                 skip "proc exports not supported before 2.10.57"
189
190         local mgs_exp="mgs.MGS.exports"
191         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
192         local exp_client_nid
193         local exp_client_version
194         local exp_val
195         local imp_val
196         local temp_imp=$DIR/$tfile.import
197         local temp_exp=$DIR/$tfile.export
198
199         # save mgc import file to $temp_imp
200         $LCTL get_param mgc.*.import | tee $temp_imp
201         # Check if client uuid is found in MGS export
202         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
203                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
204                         $client_uuid ] &&
205                         break;
206         done
207         # save mgs export file to $temp_exp
208         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
209
210         # Compare the value of field "connect_flags"
211         imp_val=$(grep "connect_flags" $temp_imp)
212         exp_val=$(grep "connect_flags" $temp_exp)
213         [ "$exp_val" == "$imp_val" ] ||
214                 error "export flags '$exp_val' != import flags '$imp_val'"
215
216         # Compare the value of client version
217         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
218         exp_val=$(version_code $exp_client_version)
219         imp_val=$(lustre_version_code client)
220         [ "$exp_val" == "$imp_val" ] ||
221                 error "export client version '$exp_val' != '$imp_val'"
222 }
223 run_test 0d "check export proc ============================="
224
225 test_1() {
226         test_mkdir $DIR/$tdir
227         test_mkdir $DIR/$tdir/d2
228         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
229         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
230         rmdir $DIR/$tdir/d2
231         rmdir $DIR/$tdir
232         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
233 }
234 run_test 1 "mkdir; remkdir; rmdir"
235
236 test_2() {
237         test_mkdir $DIR/$tdir
238         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
239         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
240         rm -r $DIR/$tdir
241         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
242 }
243 run_test 2 "mkdir; touch; rmdir; check file"
244
245 test_3() {
246         test_mkdir $DIR/$tdir
247         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
248         touch $DIR/$tdir/$tfile
249         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
250         rm -r $DIR/$tdir
251         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
252 }
253 run_test 3 "mkdir; touch; rmdir; check dir"
254
255 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
256 test_4() {
257         test_mkdir -i 1 $DIR/$tdir
258
259         touch $DIR/$tdir/$tfile ||
260                 error "Create file under remote directory failed"
261
262         rmdir $DIR/$tdir &&
263                 error "Expect error removing in-use dir $DIR/$tdir"
264
265         test -d $DIR/$tdir || error "Remote directory disappeared"
266
267         rm -rf $DIR/$tdir || error "remove remote dir error"
268 }
269 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
270
271 test_5() {
272         test_mkdir $DIR/$tdir
273         test_mkdir $DIR/$tdir/d2
274         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
275         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
276         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
277 }
278 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
279
280 test_6a() {
281         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
282         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
283         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
284                 error "$tfile does not have perm 0666 or UID $UID"
285         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
286         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
287                 error "$tfile should be 0666 and owned by UID $UID"
288 }
289 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
290
291 test_6c() {
292         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
293
294         touch $DIR/$tfile
295         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
296         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
297                 error "$tfile should be owned by UID $RUNAS_ID"
298         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
299         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
300                 error "$tfile should be owned by UID $RUNAS_ID"
301 }
302 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
303
304 test_6e() {
305         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
306
307         touch $DIR/$tfile
308         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
309         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
310                 error "$tfile should be owned by GID $UID"
311         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
312         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
313                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
314 }
315 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
316
317 test_6g() {
318         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
319
320         test_mkdir $DIR/$tdir
321         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
322         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
323         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
324         test_mkdir $DIR/$tdir/d/subdir
325         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
326                 error "$tdir/d/subdir should be GID $RUNAS_GID"
327         if [[ $MDSCOUNT -gt 1 ]]; then
328                 # check remote dir sgid inherite
329                 $LFS mkdir -i 0 $DIR/$tdir.local ||
330                         error "mkdir $tdir.local failed"
331                 chmod g+s $DIR/$tdir.local ||
332                         error "chmod $tdir.local failed"
333                 chgrp $RUNAS_GID $DIR/$tdir.local ||
334                         error "chgrp $tdir.local failed"
335                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
336                         error "mkdir $tdir.remote failed"
337                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
338                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
339                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
340                         error "$tdir.remote should be mode 02755"
341         fi
342 }
343 run_test 6g "verify new dir in sgid dir inherits group"
344
345 test_6h() { # bug 7331
346         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
347
348         touch $DIR/$tfile || error "touch failed"
349         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
350         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
351                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
352         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
353                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
354 }
355 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
356
357 test_7a() {
358         test_mkdir $DIR/$tdir
359         $MCREATE $DIR/$tdir/$tfile
360         chmod 0666 $DIR/$tdir/$tfile
361         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
362                 error "$tdir/$tfile should be mode 0666"
363 }
364 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
365
366 test_7b() {
367         if [ ! -d $DIR/$tdir ]; then
368                 test_mkdir $DIR/$tdir
369         fi
370         $MCREATE $DIR/$tdir/$tfile
371         echo -n foo > $DIR/$tdir/$tfile
372         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
373         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
374 }
375 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
376
377 test_8() {
378         test_mkdir $DIR/$tdir
379         touch $DIR/$tdir/$tfile
380         chmod 0666 $DIR/$tdir/$tfile
381         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
382                 error "$tfile mode not 0666"
383 }
384 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
385
386 test_9() {
387         test_mkdir $DIR/$tdir
388         test_mkdir $DIR/$tdir/d2
389         test_mkdir $DIR/$tdir/d2/d3
390         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
391 }
392 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
393
394 test_10() {
395         test_mkdir $DIR/$tdir
396         test_mkdir $DIR/$tdir/d2
397         touch $DIR/$tdir/d2/$tfile
398         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
399                 error "$tdir/d2/$tfile not a file"
400 }
401 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
402
403 test_11() {
404         test_mkdir $DIR/$tdir
405         test_mkdir $DIR/$tdir/d2
406         chmod 0666 $DIR/$tdir/d2
407         chmod 0705 $DIR/$tdir/d2
408         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
409                 error "$tdir/d2 mode not 0705"
410 }
411 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
412
413 test_12() {
414         test_mkdir $DIR/$tdir
415         touch $DIR/$tdir/$tfile
416         chmod 0666 $DIR/$tdir/$tfile
417         chmod 0654 $DIR/$tdir/$tfile
418         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
419                 error "$tdir/d2 mode not 0654"
420 }
421 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
422
423 test_13() {
424         test_mkdir $DIR/$tdir
425         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
426         >  $DIR/$tdir/$tfile
427         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
428                 error "$tdir/$tfile size not 0 after truncate"
429 }
430 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
431
432 test_14() {
433         test_mkdir $DIR/$tdir
434         touch $DIR/$tdir/$tfile
435         rm $DIR/$tdir/$tfile
436         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
437 }
438 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
439
440 test_15() {
441         test_mkdir $DIR/$tdir
442         touch $DIR/$tdir/$tfile
443         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
444         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
445                 error "$tdir/${tfile_2} not a file after rename"
446         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
447 }
448 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
449
450 test_16() {
451         test_mkdir $DIR/$tdir
452         touch $DIR/$tdir/$tfile
453         rm -rf $DIR/$tdir/$tfile
454         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
455 }
456 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
457
458 test_17a() {
459         test_mkdir $DIR/$tdir
460         touch $DIR/$tdir/$tfile
461         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
462         ls -l $DIR/$tdir
463         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
464                 error "$tdir/l-exist not a symlink"
465         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
466                 error "$tdir/l-exist not referencing a file"
467         rm -f $DIR/$tdir/l-exist
468         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
469 }
470 run_test 17a "symlinks: create, remove (real)"
471
472 test_17b() {
473         test_mkdir $DIR/$tdir
474         ln -s no-such-file $DIR/$tdir/l-dangle
475         ls -l $DIR/$tdir
476         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
477                 error "$tdir/l-dangle not referencing no-such-file"
478         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
479                 error "$tdir/l-dangle not referencing non-existent file"
480         rm -f $DIR/$tdir/l-dangle
481         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
482 }
483 run_test 17b "symlinks: create, remove (dangling)"
484
485 test_17c() { # bug 3440 - don't save failed open RPC for replay
486         test_mkdir $DIR/$tdir
487         ln -s foo $DIR/$tdir/$tfile
488         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
489 }
490 run_test 17c "symlinks: open dangling (should return error)"
491
492 test_17d() {
493         test_mkdir $DIR/$tdir
494         ln -s foo $DIR/$tdir/$tfile
495         touch $DIR/$tdir/$tfile || error "creating to new symlink"
496 }
497 run_test 17d "symlinks: create dangling"
498
499 test_17e() {
500         test_mkdir $DIR/$tdir
501         local foo=$DIR/$tdir/$tfile
502         ln -s $foo $foo || error "create symlink failed"
503         ls -l $foo || error "ls -l failed"
504         ls $foo && error "ls not failed" || true
505 }
506 run_test 17e "symlinks: create recursive symlink (should return error)"
507
508 test_17f() {
509         test_mkdir $DIR/$tdir
510         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
511         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
512         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
513         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
514         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
515         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
516         ls -l  $DIR/$tdir
517 }
518 run_test 17f "symlinks: long and very long symlink name"
519
520 # str_repeat(S, N) generate a string that is string S repeated N times
521 str_repeat() {
522         local s=$1
523         local n=$2
524         local ret=''
525         while [ $((n -= 1)) -ge 0 ]; do
526                 ret=$ret$s
527         done
528         echo $ret
529 }
530
531 # Long symlinks and LU-2241
532 test_17g() {
533         test_mkdir $DIR/$tdir
534         local TESTS="59 60 61 4094 4095"
535
536         # Fix for inode size boundary in 2.1.4
537         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
538                 TESTS="4094 4095"
539
540         # Patch not applied to 2.2 or 2.3 branches
541         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
542         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
543                 TESTS="4094 4095"
544
545         # skip long symlink name for rhel6.5.
546         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
547         grep -q '6.5' /etc/redhat-release &>/dev/null &&
548                 TESTS="59 60 61 4062 4063"
549
550         for i in $TESTS; do
551                 local SYMNAME=$(str_repeat 'x' $i)
552                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
553                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
554         done
555 }
556 run_test 17g "symlinks: really long symlink name and inode boundaries"
557
558 test_17h() { #bug 17378
559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
560         remote_mds_nodsh && skip "remote MDS with nodsh"
561
562         local mdt_idx
563
564         test_mkdir $DIR/$tdir
565         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
566         $LFS setstripe -c -1 $DIR/$tdir
567         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
568         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
569         touch $DIR/$tdir/$tfile || true
570 }
571 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
572
573 test_17i() { #bug 20018
574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
575         remote_mds_nodsh && skip "remote MDS with nodsh"
576
577         local foo=$DIR/$tdir/$tfile
578         local mdt_idx
579
580         test_mkdir -c1 $DIR/$tdir
581         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
582         ln -s $foo $foo || error "create symlink failed"
583 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
584         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
585         ls -l $foo && error "error not detected"
586         return 0
587 }
588 run_test 17i "don't panic on short symlink (should return error)"
589
590 test_17k() { #bug 22301
591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
592         [[ -z "$(which rsync 2>/dev/null)" ]] &&
593                 skip "no rsync command"
594         rsync --help | grep -q xattr ||
595                 skip_env "$(rsync --version | head -n1) does not support xattrs"
596         test_mkdir $DIR/$tdir
597         test_mkdir $DIR/$tdir.new
598         touch $DIR/$tdir/$tfile
599         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
600         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
601                 error "rsync failed with xattrs enabled"
602 }
603 run_test 17k "symlinks: rsync with xattrs enabled"
604
605 test_17l() { # LU-279
606         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
607                 skip "no getfattr command"
608
609         test_mkdir $DIR/$tdir
610         touch $DIR/$tdir/$tfile
611         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
612         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
613                 # -h to not follow symlinks. -m '' to list all the xattrs.
614                 # grep to remove first line: '# file: $path'.
615                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
616                 do
617                         lgetxattr_size_check $path $xattr ||
618                                 error "lgetxattr_size_check $path $xattr failed"
619                 done
620         done
621 }
622 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
623
624 # LU-1540
625 test_17m() {
626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
627         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
628                 skip_env "ldiskfs only test"
629         remote_mds_nodsh && skip "remote MDS with nodsh"
630         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
631         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
632                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
633
634         local short_sym="0123456789"
635         local wdir=$DIR/$tdir
636         local i
637
638         test_mkdir $wdir
639         long_sym=$short_sym
640         # create a long symlink file
641         for ((i = 0; i < 4; ++i)); do
642                 long_sym=${long_sym}${long_sym}
643         done
644
645         echo "create 512 short and long symlink files under $wdir"
646         for ((i = 0; i < 256; ++i)); do
647                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
648                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
649         done
650
651         echo "erase them"
652         rm -f $wdir/*
653         sync
654         wait_delete_completed
655
656         echo "recreate the 512 symlink files with a shorter string"
657         for ((i = 0; i < 512; ++i)); do
658                 # rewrite the symlink file with a shorter string
659                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
660                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
661         done
662
663         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
664         local devname=$(mdsdevname $mds_index)
665
666         echo "stop and checking mds${mds_index}:"
667         # e2fsck should not return error
668         stop mds${mds_index}
669         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
670         rc=$?
671
672         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
673                 error "start mds${mds_index} failed"
674         df $MOUNT > /dev/null 2>&1
675         [ $rc -eq 0 ] ||
676                 error "e2fsck detected error for short/long symlink: rc=$rc"
677         rm -f $wdir/*
678 }
679 run_test 17m "run e2fsck against MDT which contains short/long symlink"
680
681 check_fs_consistency_17n() {
682         local mdt_index
683         local rc=0
684
685         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
686         # so it only check MDT1/MDT2 instead of all of MDTs.
687         for mdt_index in 1 2; do
688                 local devname=$(mdsdevname $mdt_index)
689                 # e2fsck should not return error
690                 stop mds${mdt_index}
691                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
692                         rc=$((rc + $?))
693
694                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
695                         error "mount mds$mdt_index failed"
696                 df $MOUNT > /dev/null 2>&1
697         done
698         return $rc
699 }
700
701 test_17n() {
702         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
704         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
705                 skip_env "ldiskfs only test"
706         remote_mds_nodsh && skip "remote MDS with nodsh"
707         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
708         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
709                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
710
711         local i
712
713         test_mkdir $DIR/$tdir
714         for ((i=0; i<10; i++)); do
715                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
716                         error "create remote dir error $i"
717                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
718                         error "create files under remote dir failed $i"
719         done
720
721         check_fs_consistency_17n ||
722                 error "e2fsck report error after create files under remote dir"
723
724         for ((i = 0; i < 10; i++)); do
725                 rm -rf $DIR/$tdir/remote_dir_${i} ||
726                         error "destroy remote dir error $i"
727         done
728
729         check_fs_consistency_17n ||
730                 error "e2fsck report error after unlink files under remote dir"
731
732         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
733                 skip "lustre < 2.4.50 does not support migrate mv"
734
735         for ((i = 0; i < 10; i++)); do
736                 mkdir -p $DIR/$tdir/remote_dir_${i}
737                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
738                         error "create files under remote dir failed $i"
739                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
740                         error "migrate remote dir error $i"
741         done
742         check_fs_consistency_17n || error "e2fsck report error after migration"
743
744         for ((i = 0; i < 10; i++)); do
745                 rm -rf $DIR/$tdir/remote_dir_${i} ||
746                         error "destroy remote dir error $i"
747         done
748
749         check_fs_consistency_17n || error "e2fsck report error after unlink"
750 }
751 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
752
753 test_17o() {
754         remote_mds_nodsh && skip "remote MDS with nodsh"
755         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
756                 skip "Need MDS version at least 2.3.64"
757
758         local wdir=$DIR/${tdir}o
759         local mdt_index
760         local rc=0
761
762         test_mkdir $wdir
763         touch $wdir/$tfile
764         mdt_index=$($LFS getstripe -m $wdir/$tfile)
765         mdt_index=$((mdt_index + 1))
766
767         cancel_lru_locks mdc
768         #fail mds will wait the failover finish then set
769         #following fail_loc to avoid interfer the recovery process.
770         fail mds${mdt_index}
771
772         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
773         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
774         ls -l $wdir/$tfile && rc=1
775         do_facet mds${mdt_index} lctl set_param fail_loc=0
776         [[ $rc -eq 0 ]] || error "stat file should fail"
777 }
778 run_test 17o "stat file with incompat LMA feature"
779
780 test_18() {
781         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
782         ls $DIR || error "Failed to ls $DIR: $?"
783 }
784 run_test 18 "touch .../f ; ls ... =============================="
785
786 test_19a() {
787         touch $DIR/$tfile
788         ls -l $DIR
789         rm $DIR/$tfile
790         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
791 }
792 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
793
794 test_19b() {
795         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
796 }
797 run_test 19b "ls -l .../f19 (should return error) =============="
798
799 test_19c() {
800         [ $RUNAS_ID -eq $UID ] &&
801                 skip_env "RUNAS_ID = UID = $UID -- skipping"
802
803         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
804 }
805 run_test 19c "$RUNAS touch .../f19 (should return error) =="
806
807 test_19d() {
808         cat $DIR/f19 && error || true
809 }
810 run_test 19d "cat .../f19 (should return error) =============="
811
812 test_20() {
813         touch $DIR/$tfile
814         rm $DIR/$tfile
815         touch $DIR/$tfile
816         rm $DIR/$tfile
817         touch $DIR/$tfile
818         rm $DIR/$tfile
819         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
820 }
821 run_test 20 "touch .../f ; ls -l ..."
822
823 test_21() {
824         test_mkdir $DIR/$tdir
825         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
826         ln -s dangle $DIR/$tdir/link
827         echo foo >> $DIR/$tdir/link
828         cat $DIR/$tdir/dangle
829         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
830         $CHECKSTAT -f -t file $DIR/$tdir/link ||
831                 error "$tdir/link not linked to a file"
832 }
833 run_test 21 "write to dangling link"
834
835 test_22() {
836         local wdir=$DIR/$tdir
837         test_mkdir $wdir
838         chown $RUNAS_ID:$RUNAS_GID $wdir
839         (cd $wdir || error "cd $wdir failed";
840                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
841                 $RUNAS tar xf -)
842         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
843         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
844         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
845                 error "checkstat -u failed"
846 }
847 run_test 22 "unpack tar archive as non-root user"
848
849 # was test_23
850 test_23a() {
851         test_mkdir $DIR/$tdir
852         local file=$DIR/$tdir/$tfile
853
854         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
855         openfile -f O_CREAT:O_EXCL $file &&
856                 error "$file recreate succeeded" || true
857 }
858 run_test 23a "O_CREAT|O_EXCL in subdir"
859
860 test_23b() { # bug 18988
861         test_mkdir $DIR/$tdir
862         local file=$DIR/$tdir/$tfile
863
864         rm -f $file
865         echo foo > $file || error "write filed"
866         echo bar >> $file || error "append filed"
867         $CHECKSTAT -s 8 $file || error "wrong size"
868         rm $file
869 }
870 run_test 23b "O_APPEND check"
871
872 # LU-9409, size with O_APPEND and tiny writes
873 test_23c() {
874         local file=$DIR/$tfile
875
876         # single dd
877         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
878         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
879         rm -f $file
880
881         # racing tiny writes
882         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
883         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
884         wait
885         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
886         rm -f $file
887
888         #racing tiny & normal writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
891         wait
892         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
893         rm -f $file
894
895         #racing tiny & normal writes 2, ugly numbers
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
898         wait
899         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
900         rm -f $file
901 }
902 run_test 23c "O_APPEND size checks for tiny writes"
903
904 # LU-11069 file offset is correct after appending writes
905 test_23d() {
906         local file=$DIR/$tfile
907         local offset
908
909         echo CentaurHauls > $file
910         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
911         if ((offset != 26)); then
912                 error "wrong offset, expected 26, got '$offset'"
913         fi
914 }
915 run_test 23d "file offset is correct after appending writes"
916
917 # rename sanity
918 test_24a() {
919         echo '-- same directory rename'
920         test_mkdir $DIR/$tdir
921         touch $DIR/$tdir/$tfile.1
922         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
923         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
924 }
925 run_test 24a "rename file to non-existent target"
926
927 test_24b() {
928         test_mkdir $DIR/$tdir
929         touch $DIR/$tdir/$tfile.{1,2}
930         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
931         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
932         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
933 }
934 run_test 24b "rename file to existing target"
935
936 test_24c() {
937         test_mkdir $DIR/$tdir
938         test_mkdir $DIR/$tdir/d$testnum.1
939         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
940         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
941         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
942 }
943 run_test 24c "rename directory to non-existent target"
944
945 test_24d() {
946         test_mkdir -c1 $DIR/$tdir
947         test_mkdir -c1 $DIR/$tdir/d$testnum.1
948         test_mkdir -c1 $DIR/$tdir/d$testnum.2
949         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
950         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
951         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
952 }
953 run_test 24d "rename directory to existing target"
954
955 test_24e() {
956         echo '-- cross directory renames --'
957         test_mkdir $DIR/R5a
958         test_mkdir $DIR/R5b
959         touch $DIR/R5a/f
960         mv $DIR/R5a/f $DIR/R5b/g
961         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
962         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
963 }
964 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
965
966 test_24f() {
967         test_mkdir $DIR/R6a
968         test_mkdir $DIR/R6b
969         touch $DIR/R6a/f $DIR/R6b/g
970         mv $DIR/R6a/f $DIR/R6b/g
971         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
972         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
973 }
974 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
975
976 test_24g() {
977         test_mkdir $DIR/R7a
978         test_mkdir $DIR/R7b
979         test_mkdir $DIR/R7a/d
980         mv $DIR/R7a/d $DIR/R7b/e
981         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
982         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
983 }
984 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
985
986 test_24h() {
987         test_mkdir -c1 $DIR/R8a
988         test_mkdir -c1 $DIR/R8b
989         test_mkdir -c1 $DIR/R8a/d
990         test_mkdir -c1 $DIR/R8b/e
991         mrename $DIR/R8a/d $DIR/R8b/e
992         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
993         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
994 }
995 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
996
997 test_24i() {
998         echo "-- rename error cases"
999         test_mkdir $DIR/R9
1000         test_mkdir $DIR/R9/a
1001         touch $DIR/R9/f
1002         mrename $DIR/R9/f $DIR/R9/a
1003         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1004         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1005         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1006 }
1007 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1008
1009 test_24j() {
1010         test_mkdir $DIR/R10
1011         mrename $DIR/R10/f $DIR/R10/g
1012         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1013         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1014         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1015 }
1016 run_test 24j "source does not exist ============================"
1017
1018 test_24k() {
1019         test_mkdir $DIR/R11a
1020         test_mkdir $DIR/R11a/d
1021         touch $DIR/R11a/f
1022         mv $DIR/R11a/f $DIR/R11a/d
1023         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1024         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1025 }
1026 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1027
1028 # bug 2429 - rename foo foo foo creates invalid file
1029 test_24l() {
1030         f="$DIR/f24l"
1031         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1032 }
1033 run_test 24l "Renaming a file to itself ========================"
1034
1035 test_24m() {
1036         f="$DIR/f24m"
1037         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1038         # on ext3 this does not remove either the source or target files
1039         # though the "expected" operation would be to remove the source
1040         $CHECKSTAT -t file ${f} || error "${f} missing"
1041         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1042 }
1043 run_test 24m "Renaming a file to a hard link to itself ========="
1044
1045 test_24n() {
1046     f="$DIR/f24n"
1047     # this stats the old file after it was renamed, so it should fail
1048     touch ${f}
1049     $CHECKSTAT ${f} || error "${f} missing"
1050     mv ${f} ${f}.rename
1051     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1052     $CHECKSTAT -a ${f} || error "${f} exists"
1053 }
1054 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1055
1056 test_24o() {
1057         test_mkdir $DIR/$tdir
1058         rename_many -s random -v -n 10 $DIR/$tdir
1059 }
1060 run_test 24o "rename of files during htree split"
1061
1062 test_24p() {
1063         test_mkdir $DIR/R12a
1064         test_mkdir $DIR/R12b
1065         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1066         mrename $DIR/R12a $DIR/R12b
1067         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1068         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1069         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1070         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1071 }
1072 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1073
1074 cleanup_multiop_pause() {
1075         trap 0
1076         kill -USR1 $MULTIPID
1077 }
1078
1079 test_24q() {
1080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1081
1082         test_mkdir $DIR/R13a
1083         test_mkdir $DIR/R13b
1084         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1085         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1086         MULTIPID=$!
1087
1088         trap cleanup_multiop_pause EXIT
1089         mrename $DIR/R13a $DIR/R13b
1090         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1091         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1092         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1093         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1094         cleanup_multiop_pause
1095         wait $MULTIPID || error "multiop close failed"
1096 }
1097 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1098
1099 test_24r() { #bug 3789
1100         test_mkdir $DIR/R14a
1101         test_mkdir $DIR/R14a/b
1102         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1103         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1104         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1105 }
1106 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1107
1108 test_24s() {
1109         test_mkdir $DIR/R15a
1110         test_mkdir $DIR/R15a/b
1111         test_mkdir $DIR/R15a/b/c
1112         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1113         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1114         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1115 }
1116 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1117 test_24t() {
1118         test_mkdir $DIR/R16a
1119         test_mkdir $DIR/R16a/b
1120         test_mkdir $DIR/R16a/b/c
1121         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1122         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1123         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1124 }
1125 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1126
1127 test_24u() { # bug12192
1128         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1129         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1130 }
1131 run_test 24u "create stripe file"
1132
1133 page_size() {
1134         local size
1135         size=$(getconf PAGE_SIZE 2>/dev/null)
1136         echo -n ${size:-4096}
1137 }
1138
1139 simple_cleanup_common() {
1140         local rc=0
1141         trap 0
1142         [ -z "$DIR" -o -z "$tdir" ] && return 0
1143
1144         local start=$SECONDS
1145         rm -rf $DIR/$tdir
1146         rc=$?
1147         wait_delete_completed
1148         echo "cleanup time $((SECONDS - start))"
1149         return $rc
1150 }
1151
1152 max_pages_per_rpc() {
1153         local mdtname="$(printf "MDT%04x" ${1:-0})"
1154         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1155 }
1156
1157 test_24v() {
1158         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1159
1160         local nrfiles=${COUNT:-100000}
1161         local fname="$DIR/$tdir/$tfile"
1162
1163         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1164         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1165
1166         test_mkdir "$(dirname $fname)"
1167         # assume MDT0000 has the fewest inodes
1168         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1169         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1170         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1171
1172         trap simple_cleanup_common EXIT
1173
1174         createmany -m "$fname" $nrfiles
1175
1176         cancel_lru_locks mdc
1177         lctl set_param mdc.*.stats clear
1178
1179         # was previously test_24D: LU-6101
1180         # readdir() returns correct number of entries after cursor reload
1181         local num_ls=$(ls $DIR/$tdir | wc -l)
1182         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1183         local num_all=$(ls -a $DIR/$tdir | wc -l)
1184         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1185              $num_all -ne $((nrfiles + 2)) ]; then
1186                 error "Expected $nrfiles files, got $num_ls " \
1187                         "($num_uniq unique $num_all .&..)"
1188         fi
1189         # LU-5 large readdir
1190         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1191         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1192         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1193         # take into account of overhead in lu_dirpage header and end mark in
1194         # each page, plus one in rpc_num calculation.
1195         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1196         local page_entries=$((($(page_size) - 24) / dirent_size))
1197         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1198         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1199         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1200         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1201         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1202         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1203                 error "large readdir doesn't take effect: " \
1204                       "$mds_readpage should be about $rpc_max"
1205
1206         simple_cleanup_common
1207 }
1208 run_test 24v "list large directory (test hash collision, b=17560)"
1209
1210 test_24w() { # bug21506
1211         SZ1=234852
1212         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1213         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1214         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1215         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1216         [[ "$SZ1" -eq "$SZ2" ]] ||
1217                 error "Error reading at the end of the file $tfile"
1218 }
1219 run_test 24w "Reading a file larger than 4Gb"
1220
1221 test_24x() {
1222         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1223         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1224         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1225                 skip "Need MDS version at least 2.7.56"
1226
1227         local MDTIDX=1
1228         local remote_dir=$DIR/$tdir/remote_dir
1229
1230         test_mkdir $DIR/$tdir
1231         $LFS mkdir -i $MDTIDX $remote_dir ||
1232                 error "create remote directory failed"
1233
1234         test_mkdir $DIR/$tdir/src_dir
1235         touch $DIR/$tdir/src_file
1236         test_mkdir $remote_dir/tgt_dir
1237         touch $remote_dir/tgt_file
1238
1239         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1240                 error "rename dir cross MDT failed!"
1241
1242         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1243                 error "rename file cross MDT failed!"
1244
1245         touch $DIR/$tdir/ln_file
1246         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1247                 error "ln file cross MDT failed"
1248
1249         rm -rf $DIR/$tdir || error "Can not delete directories"
1250 }
1251 run_test 24x "cross MDT rename/link"
1252
1253 test_24y() {
1254         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1256
1257         local remote_dir=$DIR/$tdir/remote_dir
1258         local mdtidx=1
1259
1260         test_mkdir $DIR/$tdir
1261         $LFS mkdir -i $mdtidx $remote_dir ||
1262                 error "create remote directory failed"
1263
1264         test_mkdir $remote_dir/src_dir
1265         touch $remote_dir/src_file
1266         test_mkdir $remote_dir/tgt_dir
1267         touch $remote_dir/tgt_file
1268
1269         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1270                 error "rename subdir in the same remote dir failed!"
1271
1272         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1273                 error "rename files in the same remote dir failed!"
1274
1275         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1276                 error "link files in the same remote dir failed!"
1277
1278         rm -rf $DIR/$tdir || error "Can not delete directories"
1279 }
1280 run_test 24y "rename/link on the same dir should succeed"
1281
1282 test_24A() { # LU-3182
1283         local NFILES=5000
1284
1285         rm -rf $DIR/$tdir
1286         test_mkdir $DIR/$tdir
1287         trap simple_cleanup_common EXIT
1288         createmany -m $DIR/$tdir/$tfile $NFILES
1289         local t=$(ls $DIR/$tdir | wc -l)
1290         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1291         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1292         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1293                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1294         fi
1295
1296         simple_cleanup_common || error "Can not delete directories"
1297 }
1298 run_test 24A "readdir() returns correct number of entries."
1299
1300 test_24B() { # LU-4805
1301         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1302
1303         local count
1304
1305         test_mkdir $DIR/$tdir
1306         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1307                 error "create striped dir failed"
1308
1309         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1310         [ $count -eq 2 ] || error "Expected 2, got $count"
1311
1312         touch $DIR/$tdir/striped_dir/a
1313
1314         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1315         [ $count -eq 3 ] || error "Expected 3, got $count"
1316
1317         touch $DIR/$tdir/striped_dir/.f
1318
1319         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1320         [ $count -eq 4 ] || error "Expected 4, got $count"
1321
1322         rm -rf $DIR/$tdir || error "Can not delete directories"
1323 }
1324 run_test 24B "readdir for striped dir return correct number of entries"
1325
1326 test_24C() {
1327         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1328
1329         mkdir $DIR/$tdir
1330         mkdir $DIR/$tdir/d0
1331         mkdir $DIR/$tdir/d1
1332
1333         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1334                 error "create striped dir failed"
1335
1336         cd $DIR/$tdir/d0/striped_dir
1337
1338         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1339         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1340         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1341
1342         [ "$d0_ino" = "$parent_ino" ] ||
1343                 error ".. wrong, expect $d0_ino, get $parent_ino"
1344
1345         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1346                 error "mv striped dir failed"
1347
1348         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1349
1350         [ "$d1_ino" = "$parent_ino" ] ||
1351                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1352 }
1353 run_test 24C "check .. in striped dir"
1354
1355 test_24E() {
1356         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1358
1359         mkdir -p $DIR/$tdir
1360         mkdir $DIR/$tdir/src_dir
1361         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1362                 error "create remote source failed"
1363
1364         touch $DIR/$tdir/src_dir/src_child/a
1365
1366         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1367                 error "create remote target dir failed"
1368
1369         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1370                 error "create remote target child failed"
1371
1372         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1373                 error "rename dir cross MDT failed!"
1374
1375         find $DIR/$tdir
1376
1377         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1378                 error "src_child still exists after rename"
1379
1380         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1381                 error "missing file(a) after rename"
1382
1383         rm -rf $DIR/$tdir || error "Can not delete directories"
1384 }
1385 run_test 24E "cross MDT rename/link"
1386
1387 test_25a() {
1388         echo '== symlink sanity ============================================='
1389
1390         test_mkdir $DIR/d25
1391         ln -s d25 $DIR/s25
1392         touch $DIR/s25/foo ||
1393                 error "File creation in symlinked directory failed"
1394 }
1395 run_test 25a "create file in symlinked directory ==============="
1396
1397 test_25b() {
1398         [ ! -d $DIR/d25 ] && test_25a
1399         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1400 }
1401 run_test 25b "lookup file in symlinked directory ==============="
1402
1403 test_26a() {
1404         test_mkdir $DIR/d26
1405         test_mkdir $DIR/d26/d26-2
1406         ln -s d26/d26-2 $DIR/s26
1407         touch $DIR/s26/foo || error "File creation failed"
1408 }
1409 run_test 26a "multiple component symlink ======================="
1410
1411 test_26b() {
1412         test_mkdir -p $DIR/$tdir/d26-2
1413         ln -s $tdir/d26-2/foo $DIR/s26-2
1414         touch $DIR/s26-2 || error "File creation failed"
1415 }
1416 run_test 26b "multiple component symlink at end of lookup ======"
1417
1418 test_26c() {
1419         test_mkdir $DIR/d26.2
1420         touch $DIR/d26.2/foo
1421         ln -s d26.2 $DIR/s26.2-1
1422         ln -s s26.2-1 $DIR/s26.2-2
1423         ln -s s26.2-2 $DIR/s26.2-3
1424         chmod 0666 $DIR/s26.2-3/foo
1425 }
1426 run_test 26c "chain of symlinks"
1427
1428 # recursive symlinks (bug 439)
1429 test_26d() {
1430         ln -s d26-3/foo $DIR/d26-3
1431 }
1432 run_test 26d "create multiple component recursive symlink"
1433
1434 test_26e() {
1435         [ ! -h $DIR/d26-3 ] && test_26d
1436         rm $DIR/d26-3
1437 }
1438 run_test 26e "unlink multiple component recursive symlink"
1439
1440 # recursive symlinks (bug 7022)
1441 test_26f() {
1442         test_mkdir $DIR/$tdir
1443         test_mkdir $DIR/$tdir/$tfile
1444         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1445         test_mkdir -p lndir/bar1
1446         test_mkdir $DIR/$tdir/$tfile/$tfile
1447         cd $tfile                || error "cd $tfile failed"
1448         ln -s .. dotdot          || error "ln dotdot failed"
1449         ln -s dotdot/lndir lndir || error "ln lndir failed"
1450         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1451         output=`ls $tfile/$tfile/lndir/bar1`
1452         [ "$output" = bar1 ] && error "unexpected output"
1453         rm -r $tfile             || error "rm $tfile failed"
1454         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1455 }
1456 run_test 26f "rm -r of a directory which has recursive symlink"
1457
1458 test_27a() {
1459         test_mkdir $DIR/$tdir
1460         $LFS getstripe $DIR/$tdir
1461         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1462         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1463         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1464 }
1465 run_test 27a "one stripe file"
1466
1467 test_27b() {
1468         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1469
1470         test_mkdir $DIR/$tdir
1471         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1472         $LFS getstripe -c $DIR/$tdir/$tfile
1473         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1474                 error "two-stripe file doesn't have two stripes"
1475
1476         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1477 }
1478 run_test 27b "create and write to two stripe file"
1479
1480 test_27d() {
1481         test_mkdir $DIR/$tdir
1482         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1483                 error "setstripe failed"
1484         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1485         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1486 }
1487 run_test 27d "create file with default settings"
1488
1489 test_27e() {
1490         # LU-5839 adds check for existed layout before setting it
1491         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1492                 skip "Need MDS version at least 2.7.56"
1493
1494         test_mkdir $DIR/$tdir
1495         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1496         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1497         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1498 }
1499 run_test 27e "setstripe existing file (should return error)"
1500
1501 test_27f() {
1502         test_mkdir $DIR/$tdir
1503         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1504                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1505         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1506                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1507         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1508         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1509 }
1510 run_test 27f "setstripe with bad stripe size (should return error)"
1511
1512 test_27g() {
1513         test_mkdir $DIR/$tdir
1514         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1515         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1516                 error "$DIR/$tdir/$tfile has object"
1517 }
1518 run_test 27g "$LFS getstripe with no objects"
1519
1520 test_27i() {
1521         test_mkdir $DIR/$tdir
1522         touch $DIR/$tdir/$tfile || error "touch failed"
1523         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1524                 error "missing objects"
1525 }
1526 run_test 27i "$LFS getstripe with some objects"
1527
1528 test_27j() {
1529         test_mkdir $DIR/$tdir
1530         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1531                 error "setstripe failed" || true
1532 }
1533 run_test 27j "setstripe with bad stripe offset (should return error)"
1534
1535 test_27k() { # bug 2844
1536         test_mkdir $DIR/$tdir
1537         local file=$DIR/$tdir/$tfile
1538         local ll_max_blksize=$((4 * 1024 * 1024))
1539         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1540         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1541         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1542         dd if=/dev/zero of=$file bs=4k count=1
1543         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1544         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1545 }
1546 run_test 27k "limit i_blksize for broken user apps"
1547
1548 test_27l() {
1549         mcreate $DIR/$tfile || error "creating file"
1550         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1551                 error "setstripe should have failed" || true
1552 }
1553 run_test 27l "check setstripe permissions (should return error)"
1554
1555 test_27m() {
1556         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1557
1558         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1559                    head -n1)
1560         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1561                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1562         fi
1563         trap simple_cleanup_common EXIT
1564         test_mkdir $DIR/$tdir
1565         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1566         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1567                 error "dd should fill OST0"
1568         i=2
1569         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1570                 i=$((i + 1))
1571                 [ $i -gt 256 ] && break
1572         done
1573         i=$((i + 1))
1574         touch $DIR/$tdir/$tfile.$i
1575         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1576             awk '{print $1}'| grep -w "0") ] &&
1577                 error "OST0 was full but new created file still use it"
1578         i=$((i + 1))
1579         touch $DIR/$tdir/$tfile.$i
1580         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1581             awk '{print $1}'| grep -w "0") ] &&
1582                 error "OST0 was full but new created file still use it"
1583         simple_cleanup_common
1584 }
1585 run_test 27m "create file while OST0 was full"
1586
1587 sleep_maxage() {
1588         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1589                       awk '{ print $1 * 2; exit; }')
1590         sleep $delay
1591 }
1592
1593 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1594 # if the OST isn't full anymore.
1595 reset_enospc() {
1596         local OSTIDX=${1:-""}
1597
1598         local list=$(comma_list $(osts_nodes))
1599         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1600
1601         do_nodes $list lctl set_param fail_loc=0
1602         sync    # initiate all OST_DESTROYs from MDS to OST
1603         sleep_maxage
1604 }
1605
1606 exhaust_precreations() {
1607         local OSTIDX=$1
1608         local FAILLOC=$2
1609         local FAILIDX=${3:-$OSTIDX}
1610         local ofacet=ost$((OSTIDX + 1))
1611
1612         test_mkdir -p -c1 $DIR/$tdir
1613         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1614         local mfacet=mds$((mdtidx + 1))
1615         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1616
1617         local OST=$(ostname_from_index $OSTIDX)
1618
1619         # on the mdt's osc
1620         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1621         local last_id=$(do_facet $mfacet lctl get_param -n \
1622                         osc.$mdtosc_proc1.prealloc_last_id)
1623         local next_id=$(do_facet $mfacet lctl get_param -n \
1624                         osc.$mdtosc_proc1.prealloc_next_id)
1625
1626         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1627         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1628
1629         test_mkdir -p $DIR/$tdir/${OST}
1630         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1631 #define OBD_FAIL_OST_ENOSPC              0x215
1632         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1633         echo "Creating to objid $last_id on ost $OST..."
1634         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1635         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1636         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1637         sleep_maxage
1638 }
1639
1640 exhaust_all_precreations() {
1641         local i
1642         for (( i=0; i < OSTCOUNT; i++ )) ; do
1643                 exhaust_precreations $i $1 -1
1644         done
1645 }
1646
1647 test_27n() {
1648         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1650         remote_mds_nodsh && skip "remote MDS with nodsh"
1651         remote_ost_nodsh && skip "remote OST with nodsh"
1652
1653         reset_enospc
1654         rm -f $DIR/$tdir/$tfile
1655         exhaust_precreations 0 0x80000215
1656         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1657         touch $DIR/$tdir/$tfile || error "touch failed"
1658         $LFS getstripe $DIR/$tdir/$tfile
1659         reset_enospc
1660 }
1661 run_test 27n "create file with some full OSTs"
1662
1663 test_27o() {
1664         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1666         remote_mds_nodsh && skip "remote MDS with nodsh"
1667         remote_ost_nodsh && skip "remote OST with nodsh"
1668
1669         reset_enospc
1670         rm -f $DIR/$tdir/$tfile
1671         exhaust_all_precreations 0x215
1672
1673         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1674
1675         reset_enospc
1676         rm -rf $DIR/$tdir/*
1677 }
1678 run_test 27o "create file with all full OSTs (should error)"
1679
1680 test_27p() {
1681         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1682         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1683         remote_mds_nodsh && skip "remote MDS with nodsh"
1684         remote_ost_nodsh && skip "remote OST with nodsh"
1685
1686         reset_enospc
1687         rm -f $DIR/$tdir/$tfile
1688         test_mkdir $DIR/$tdir
1689
1690         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1691         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1692         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1693
1694         exhaust_precreations 0 0x80000215
1695         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1696         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1697         $LFS getstripe $DIR/$tdir/$tfile
1698
1699         reset_enospc
1700 }
1701 run_test 27p "append to a truncated file with some full OSTs"
1702
1703 test_27q() {
1704         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1706         remote_mds_nodsh && skip "remote MDS with nodsh"
1707         remote_ost_nodsh && skip "remote OST with nodsh"
1708
1709         reset_enospc
1710         rm -f $DIR/$tdir/$tfile
1711
1712         test_mkdir $DIR/$tdir
1713         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1714         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1715                 error "truncate $DIR/$tdir/$tfile failed"
1716         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1717
1718         exhaust_all_precreations 0x215
1719
1720         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1721         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1722
1723         reset_enospc
1724 }
1725 run_test 27q "append to truncated file with all OSTs full (should error)"
1726
1727 test_27r() {
1728         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1730         remote_mds_nodsh && skip "remote MDS with nodsh"
1731         remote_ost_nodsh && skip "remote OST with nodsh"
1732
1733         reset_enospc
1734         rm -f $DIR/$tdir/$tfile
1735         exhaust_precreations 0 0x80000215
1736
1737         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1738
1739         reset_enospc
1740 }
1741 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1742
1743 test_27s() { # bug 10725
1744         test_mkdir $DIR/$tdir
1745         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1746         local stripe_count=0
1747         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1748         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1749                 error "stripe width >= 2^32 succeeded" || true
1750
1751 }
1752 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1753
1754 test_27t() { # bug 10864
1755         WDIR=$(pwd)
1756         WLFS=$(which lfs)
1757         cd $DIR
1758         touch $tfile
1759         $WLFS getstripe $tfile
1760         cd $WDIR
1761 }
1762 run_test 27t "check that utils parse path correctly"
1763
1764 test_27u() { # bug 4900
1765         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1766         remote_mds_nodsh && skip "remote MDS with nodsh"
1767
1768         local index
1769         local list=$(comma_list $(mdts_nodes))
1770
1771 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1772         do_nodes $list $LCTL set_param fail_loc=0x139
1773         test_mkdir -p $DIR/$tdir
1774         trap simple_cleanup_common EXIT
1775         createmany -o $DIR/$tdir/t- 1000
1776         do_nodes $list $LCTL set_param fail_loc=0
1777
1778         TLOG=$TMP/$tfile.getstripe
1779         $LFS getstripe $DIR/$tdir > $TLOG
1780         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1781         unlinkmany $DIR/$tdir/t- 1000
1782         trap 0
1783         [[ $OBJS -gt 0 ]] &&
1784                 error "$OBJS objects created on OST-0. See $TLOG" ||
1785                 rm -f $TLOG
1786 }
1787 run_test 27u "skip object creation on OSC w/o objects"
1788
1789 test_27v() { # bug 4900
1790         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1792         remote_mds_nodsh && skip "remote MDS with nodsh"
1793         remote_ost_nodsh && skip "remote OST with nodsh"
1794
1795         exhaust_all_precreations 0x215
1796         reset_enospc
1797
1798         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1799
1800         touch $DIR/$tdir/$tfile
1801         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1802         # all except ost1
1803         for (( i=1; i < OSTCOUNT; i++ )); do
1804                 do_facet ost$i lctl set_param fail_loc=0x705
1805         done
1806         local START=`date +%s`
1807         createmany -o $DIR/$tdir/$tfile 32
1808
1809         local FINISH=`date +%s`
1810         local TIMEOUT=`lctl get_param -n timeout`
1811         local PROCESS=$((FINISH - START))
1812         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1813                error "$FINISH - $START >= $TIMEOUT / 2"
1814         sleep $((TIMEOUT / 2 - PROCESS))
1815         reset_enospc
1816 }
1817 run_test 27v "skip object creation on slow OST"
1818
1819 test_27w() { # bug 10997
1820         test_mkdir $DIR/$tdir
1821         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1822         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1823                 error "stripe size $size != 65536" || true
1824         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1825                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1826 }
1827 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1828
1829 test_27wa() {
1830         [[ $OSTCOUNT -lt 2 ]] &&
1831                 skip_env "skipping multiple stripe count/offset test"
1832
1833         test_mkdir $DIR/$tdir
1834         for i in $(seq 1 $OSTCOUNT); do
1835                 offset=$((i - 1))
1836                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1837                         error "setstripe -c $i -i $offset failed"
1838                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1839                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1840                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1841                 [ $index -ne $offset ] &&
1842                         error "stripe offset $index != $offset" || true
1843         done
1844 }
1845 run_test 27wa "check $LFS setstripe -c -i options"
1846
1847 test_27x() {
1848         remote_ost_nodsh && skip "remote OST with nodsh"
1849         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1851
1852         OFFSET=$(($OSTCOUNT - 1))
1853         OSTIDX=0
1854         local OST=$(ostname_from_index $OSTIDX)
1855
1856         test_mkdir $DIR/$tdir
1857         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1858         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1859         sleep_maxage
1860         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1861         for i in $(seq 0 $OFFSET); do
1862                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1863                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1864                 error "OST0 was degraded but new created file still use it"
1865         done
1866         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1867 }
1868 run_test 27x "create files while OST0 is degraded"
1869
1870 test_27y() {
1871         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1872         remote_mds_nodsh && skip "remote MDS with nodsh"
1873         remote_ost_nodsh && skip "remote OST with nodsh"
1874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1875
1876         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1877         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1878                 osc.$mdtosc.prealloc_last_id)
1879         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1880                 osc.$mdtosc.prealloc_next_id)
1881         local fcount=$((last_id - next_id))
1882         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1883         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1884
1885         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1886                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1887         local OST_DEACTIVE_IDX=-1
1888         local OSC
1889         local OSTIDX
1890         local OST
1891
1892         for OSC in $MDS_OSCS; do
1893                 OST=$(osc_to_ost $OSC)
1894                 OSTIDX=$(index_from_ostuuid $OST)
1895                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1896                         OST_DEACTIVE_IDX=$OSTIDX
1897                 fi
1898                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1899                         echo $OSC "is Deactivated:"
1900                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1901                 fi
1902         done
1903
1904         OSTIDX=$(index_from_ostuuid $OST)
1905         test_mkdir $DIR/$tdir
1906         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1907
1908         for OSC in $MDS_OSCS; do
1909                 OST=$(osc_to_ost $OSC)
1910                 OSTIDX=$(index_from_ostuuid $OST)
1911                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1912                         echo $OST "is degraded:"
1913                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1914                                                 obdfilter.$OST.degraded=1
1915                 fi
1916         done
1917
1918         sleep_maxage
1919         createmany -o $DIR/$tdir/$tfile $fcount
1920
1921         for OSC in $MDS_OSCS; do
1922                 OST=$(osc_to_ost $OSC)
1923                 OSTIDX=$(index_from_ostuuid $OST)
1924                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1925                         echo $OST "is recovered from degraded:"
1926                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1927                                                 obdfilter.$OST.degraded=0
1928                 else
1929                         do_facet $SINGLEMDS lctl --device %$OSC activate
1930                 fi
1931         done
1932
1933         # all osp devices get activated, hence -1 stripe count restored
1934         local stripe_count=0
1935
1936         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1937         # devices get activated.
1938         sleep_maxage
1939         $LFS setstripe -c -1 $DIR/$tfile
1940         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1941         rm -f $DIR/$tfile
1942         [ $stripe_count -ne $OSTCOUNT ] &&
1943                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1944         return 0
1945 }
1946 run_test 27y "create files while OST0 is degraded and the rest inactive"
1947
1948 check_seq_oid()
1949 {
1950         log "check file $1"
1951
1952         lmm_count=$($GETSTRIPE -c $1)
1953         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1954         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1955
1956         local old_ifs="$IFS"
1957         IFS=$'[:]'
1958         fid=($($LFS path2fid $1))
1959         IFS="$old_ifs"
1960
1961         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1962         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1963
1964         # compare lmm_seq and lu_fid->f_seq
1965         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1966         # compare lmm_object_id and lu_fid->oid
1967         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1968
1969         # check the trusted.fid attribute of the OST objects of the file
1970         local have_obdidx=false
1971         local stripe_nr=0
1972         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1973                 # skip lines up to and including "obdidx"
1974                 [ -z "$obdidx" ] && break
1975                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1976                 $have_obdidx || continue
1977
1978                 local ost=$((obdidx + 1))
1979                 local dev=$(ostdevname $ost)
1980                 local oid_hex
1981
1982                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1983
1984                 seq=$(echo $seq | sed -e "s/^0x//g")
1985                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1986                         oid_hex=$(echo $oid)
1987                 else
1988                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1989                 fi
1990                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1991
1992                 local ff=""
1993                 #
1994                 # Don't unmount/remount the OSTs if we don't need to do that.
1995                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1996                 # update too, until that use mount/ll_decode_filter_fid/mount.
1997                 # Re-enable when debugfs will understand new filter_fid.
1998                 #
1999                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2000                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2001                                 $dev 2>/dev/null" | grep "parent=")
2002                 fi
2003                 if [ -z "$ff" ]; then
2004                         stop ost$ost
2005                         mount_fstype ost$ost
2006                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2007                                 $(facet_mntpt ost$ost)/$obj_file)
2008                         unmount_fstype ost$ost
2009                         start ost$ost $dev $OST_MOUNT_OPTS
2010                         clients_up
2011                 fi
2012
2013                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2014
2015                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2016
2017                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2018                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2019                 #
2020                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2021                 #       stripe_size=1048576 component_id=1 component_start=0 \
2022                 #       component_end=33554432
2023                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2024                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2025                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2026                 local ff_pstripe
2027                 if grep -q 'stripe=' <<<$ff; then
2028                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2029                 else
2030                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2031                         # into f_ver in this case.  See comment on ff_parent.
2032                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2033                 fi
2034
2035                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2036                 [ $ff_pseq = $lmm_seq ] ||
2037                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2038                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2039                 [ $ff_poid = $lmm_oid ] ||
2040                         error "FF parent OID $ff_poid != $lmm_oid"
2041                 (($ff_pstripe == $stripe_nr)) ||
2042                         error "FF stripe $ff_pstripe != $stripe_nr"
2043
2044                 stripe_nr=$((stripe_nr + 1))
2045                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2046                         continue
2047                 if grep -q 'stripe_count=' <<<$ff; then
2048                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2049                                             -e 's/ .*//' <<<$ff)
2050                         [ $lmm_count = $ff_scnt ] ||
2051                                 error "FF stripe count $lmm_count != $ff_scnt"
2052                 fi
2053         done
2054 }
2055
2056 test_27z() {
2057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2058         remote_ost_nodsh && skip "remote OST with nodsh"
2059
2060         test_mkdir $DIR/$tdir
2061         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2062                 { error "setstripe -c -1 failed"; return 1; }
2063         # We need to send a write to every object to get parent FID info set.
2064         # This _should_ also work for setattr, but does not currently.
2065         # touch $DIR/$tdir/$tfile-1 ||
2066         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2067                 { error "dd $tfile-1 failed"; return 2; }
2068         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2069                 { error "setstripe -c -1 failed"; return 3; }
2070         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2071                 { error "dd $tfile-2 failed"; return 4; }
2072
2073         # make sure write RPCs have been sent to OSTs
2074         sync; sleep 5; sync
2075
2076         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2077         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2078 }
2079 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2080
2081 test_27A() { # b=19102
2082         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2083
2084         save_layout_restore_at_exit $MOUNT
2085         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2086         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2087                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2088         local default_size=$($GETSTRIPE -S $MOUNT)
2089         local default_offset=$($GETSTRIPE -i $MOUNT)
2090         local dsize=$(do_facet $SINGLEMDS \
2091                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2092         [ $default_size -eq $dsize ] ||
2093                 error "stripe size $default_size != $dsize"
2094         [ $default_offset -eq -1 ] ||
2095                 error "stripe offset $default_offset != -1"
2096 }
2097 run_test 27A "check filesystem-wide default LOV EA values"
2098
2099 test_27B() { # LU-2523
2100         test_mkdir $DIR/$tdir
2101         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2102         touch $DIR/$tdir/f0
2103         # open f1 with O_LOV_DELAY_CREATE
2104         # rename f0 onto f1
2105         # call setstripe ioctl on open file descriptor for f1
2106         # close
2107         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2108                 $DIR/$tdir/f0
2109
2110         rm -f $DIR/$tdir/f1
2111         # open f1 with O_LOV_DELAY_CREATE
2112         # unlink f1
2113         # call setstripe ioctl on open file descriptor for f1
2114         # close
2115         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2116
2117         # Allow multiop to fail in imitation of NFS's busted semantics.
2118         true
2119 }
2120 run_test 27B "call setstripe on open unlinked file/rename victim"
2121
2122 test_27C() { #LU-2871
2123         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2124
2125         declare -a ost_idx
2126         local index
2127         local found
2128         local i
2129         local j
2130
2131         test_mkdir $DIR/$tdir
2132         cd $DIR/$tdir
2133         for i in $(seq 0 $((OSTCOUNT - 1))); do
2134                 # set stripe across all OSTs starting from OST$i
2135                 $SETSTRIPE -i $i -c -1 $tfile$i
2136                 # get striping information
2137                 ost_idx=($($GETSTRIPE $tfile$i |
2138                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2139                 echo ${ost_idx[@]}
2140
2141                 # check the layout
2142                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2143                         error "${#ost_idx[@]} != $OSTCOUNT"
2144
2145                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2146                         found=0
2147                         for j in $(echo ${ost_idx[@]}); do
2148                                 if [ $index -eq $j ]; then
2149                                         found=1
2150                                         break
2151                                 fi
2152                         done
2153                         [ $found = 1 ] ||
2154                                 error "Can not find $index in ${ost_idx[@]}"
2155                 done
2156         done
2157 }
2158 run_test 27C "check full striping across all OSTs"
2159
2160 test_27D() {
2161         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2162         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2163         remote_mds_nodsh && skip "remote MDS with nodsh"
2164
2165         local POOL=${POOL:-testpool}
2166         local first_ost=0
2167         local last_ost=$(($OSTCOUNT - 1))
2168         local ost_step=1
2169         local ost_list=$(seq $first_ost $ost_step $last_ost)
2170         local ost_range="$first_ost $last_ost $ost_step"
2171
2172         if ! combined_mgs_mds ; then
2173                 mount_mgs_client
2174         fi
2175
2176         test_mkdir $DIR/$tdir
2177         pool_add $POOL || error "pool_add failed"
2178         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2179
2180         local skip27D
2181         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2182                 skip27D+="-s 29"
2183         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2184           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2185                 skip27D+=" -s 30,31"
2186         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2187                 error "llapi_layout_test failed"
2188
2189         destroy_test_pools || error "destroy test pools failed"
2190
2191         if ! combined_mgs_mds ; then
2192                 umount_mgs_client
2193         fi
2194 }
2195 run_test 27D "validate llapi_layout API"
2196
2197 # Verify that default_easize is increased from its initial value after
2198 # accessing a widely striped file.
2199 test_27E() {
2200         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2201         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2202                 skip "client does not have LU-3338 fix"
2203
2204         # 72 bytes is the minimum space required to store striping
2205         # information for a file striped across one OST:
2206         # (sizeof(struct lov_user_md_v3) +
2207         #  sizeof(struct lov_user_ost_data_v1))
2208         local min_easize=72
2209         $LCTL set_param -n llite.*.default_easize $min_easize ||
2210                 error "lctl set_param failed"
2211         local easize=$($LCTL get_param -n llite.*.default_easize)
2212
2213         [ $easize -eq $min_easize ] ||
2214                 error "failed to set default_easize"
2215
2216         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2217                 error "setstripe failed"
2218         cat $DIR/$tfile
2219         rm $DIR/$tfile
2220
2221         easize=$($LCTL get_param -n llite.*.default_easize)
2222
2223         [ $easize -gt $min_easize ] ||
2224                 error "default_easize not updated"
2225 }
2226 run_test 27E "check that default extended attribute size properly increases"
2227
2228 test_27F() { # LU-5346/LU-7975
2229         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2230         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2231         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2232                 skip "Need MDS version at least 2.8.51"
2233         remote_ost_nodsh && skip "remote OST with nodsh"
2234
2235         test_mkdir $DIR/$tdir
2236         rm -f $DIR/$tdir/f0
2237         $SETSTRIPE -c 2 $DIR/$tdir
2238
2239         # stop all OSTs to reproduce situation for LU-7975 ticket
2240         for num in $(seq $OSTCOUNT); do
2241                 stop ost$num
2242         done
2243
2244         # open/create f0 with O_LOV_DELAY_CREATE
2245         # truncate f0 to a non-0 size
2246         # close
2247         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2248
2249         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2250         # open/write it again to force delayed layout creation
2251         cat /etc/hosts > $DIR/$tdir/f0 &
2252         catpid=$!
2253
2254         # restart OSTs
2255         for num in $(seq $OSTCOUNT); do
2256                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2257                         error "ost$num failed to start"
2258         done
2259
2260         wait $catpid || error "cat failed"
2261
2262         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2263         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2264
2265 }
2266 run_test 27F "Client resend delayed layout creation with non-zero size"
2267
2268 test_27G() { #LU-10629
2269         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2270         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2271         local POOL=${POOL:-testpool}
2272         local ostrange="0 0 1"
2273
2274         test_mkdir $DIR/$tdir
2275         pool_add $POOL || error "pool_add failed"
2276         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2277         $LFS setstripe -p $POOL $DIR/$tdir
2278
2279         local pool=$($LFS getstripe -p $DIR/$tdir)
2280
2281         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2282
2283         $LFS setstripe -d $DIR/$tdir
2284
2285         pool=$($LFS getstripe -p $DIR/$tdir)
2286
2287         rmdir $DIR/$tdir
2288
2289         [ -z "$pool" ] || error "'$pool' is not empty"
2290 }
2291 run_test 27G "Clear OST pool from stripe"
2292
2293 # createtest also checks that device nodes are created and
2294 # then visible correctly (#2091)
2295 test_28() { # bug 2091
2296         test_mkdir $DIR/d28
2297         $CREATETEST $DIR/d28/ct || error "createtest failed"
2298 }
2299 run_test 28 "create/mknod/mkdir with bad file types ============"
2300
2301 test_29() {
2302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2303
2304         sync; sleep 1; sync # flush out any dirty pages from previous tests
2305         cancel_lru_locks
2306         test_mkdir $DIR/d29
2307         touch $DIR/d29/foo
2308         log 'first d29'
2309         ls -l $DIR/d29
2310
2311         declare -i LOCKCOUNTORIG=0
2312         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2313                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2314         done
2315         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2316
2317         declare -i LOCKUNUSEDCOUNTORIG=0
2318         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2319                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2320         done
2321
2322         log 'second d29'
2323         ls -l $DIR/d29
2324         log 'done'
2325
2326         declare -i LOCKCOUNTCURRENT=0
2327         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2328                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2329         done
2330
2331         declare -i LOCKUNUSEDCOUNTCURRENT=0
2332         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2333                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2334         done
2335
2336         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2337                 $LCTL set_param -n ldlm.dump_namespaces ""
2338                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2339                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2340                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2341                 return 2
2342         fi
2343         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2344                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2345                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2346                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2347                 return 3
2348         fi
2349 }
2350 run_test 29 "IT_GETATTR regression  ============================"
2351
2352 test_30a() { # was test_30
2353         cp $(which ls) $DIR || cp /bin/ls $DIR
2354         $DIR/ls / || error "Can't execute binary from lustre"
2355         rm $DIR/ls
2356 }
2357 run_test 30a "execute binary from Lustre (execve) =============="
2358
2359 test_30b() {
2360         cp `which ls` $DIR || cp /bin/ls $DIR
2361         chmod go+rx $DIR/ls
2362         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2363         rm $DIR/ls
2364 }
2365 run_test 30b "execute binary from Lustre as non-root ==========="
2366
2367 test_30c() { # b=22376
2368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2369
2370         cp `which ls` $DIR || cp /bin/ls $DIR
2371         chmod a-rw $DIR/ls
2372         cancel_lru_locks mdc
2373         cancel_lru_locks osc
2374         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2375         rm -f $DIR/ls
2376 }
2377 run_test 30c "execute binary from Lustre without read perms ===="
2378
2379 test_31a() {
2380         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2381         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2382 }
2383 run_test 31a "open-unlink file =================================="
2384
2385 test_31b() {
2386         touch $DIR/f31 || error "touch $DIR/f31 failed"
2387         ln $DIR/f31 $DIR/f31b || error "ln failed"
2388         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2389         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2390 }
2391 run_test 31b "unlink file with multiple links while open ======="
2392
2393 test_31c() {
2394         touch $DIR/f31 || error "touch $DIR/f31 failed"
2395         ln $DIR/f31 $DIR/f31c || error "ln failed"
2396         multiop_bg_pause $DIR/f31 O_uc ||
2397                 error "multiop_bg_pause for $DIR/f31 failed"
2398         MULTIPID=$!
2399         $MULTIOP $DIR/f31c Ouc
2400         kill -USR1 $MULTIPID
2401         wait $MULTIPID
2402 }
2403 run_test 31c "open-unlink file with multiple links ============="
2404
2405 test_31d() {
2406         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2407         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2408 }
2409 run_test 31d "remove of open directory ========================="
2410
2411 test_31e() { # bug 2904
2412         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2413 }
2414 run_test 31e "remove of open non-empty directory ==============="
2415
2416 test_31f() { # bug 4554
2417         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2418
2419         set -vx
2420         test_mkdir $DIR/d31f
2421         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2422         cp /etc/hosts $DIR/d31f
2423         ls -l $DIR/d31f
2424         $GETSTRIPE $DIR/d31f/hosts
2425         multiop_bg_pause $DIR/d31f D_c || return 1
2426         MULTIPID=$!
2427
2428         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2429         test_mkdir $DIR/d31f
2430         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2431         cp /etc/hosts $DIR/d31f
2432         ls -l $DIR/d31f
2433         $GETSTRIPE $DIR/d31f/hosts
2434         multiop_bg_pause $DIR/d31f D_c || return 1
2435         MULTIPID2=$!
2436
2437         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2438         wait $MULTIPID || error "first opendir $MULTIPID failed"
2439
2440         sleep 6
2441
2442         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2443         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2444         set +vx
2445 }
2446 run_test 31f "remove of open directory with open-unlink file ==="
2447
2448 test_31g() {
2449         echo "-- cross directory link --"
2450         test_mkdir -c1 $DIR/${tdir}ga
2451         test_mkdir -c1 $DIR/${tdir}gb
2452         touch $DIR/${tdir}ga/f
2453         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2454         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2455         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2456         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2457         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2458 }
2459 run_test 31g "cross directory link==============="
2460
2461 test_31h() {
2462         echo "-- cross directory link --"
2463         test_mkdir -c1 $DIR/${tdir}
2464         test_mkdir -c1 $DIR/${tdir}/dir
2465         touch $DIR/${tdir}/f
2466         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2467         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2468         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2469         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2470         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2471 }
2472 run_test 31h "cross directory link under child==============="
2473
2474 test_31i() {
2475         echo "-- cross directory link --"
2476         test_mkdir -c1 $DIR/$tdir
2477         test_mkdir -c1 $DIR/$tdir/dir
2478         touch $DIR/$tdir/dir/f
2479         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2480         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2481         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2482         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2483         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2484 }
2485 run_test 31i "cross directory link under parent==============="
2486
2487 test_31j() {
2488         test_mkdir -c1 -p $DIR/$tdir
2489         test_mkdir -c1 -p $DIR/$tdir/dir1
2490         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2491         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2492         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2493         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2494         return 0
2495 }
2496 run_test 31j "link for directory==============="
2497
2498 test_31k() {
2499         test_mkdir -c1 -p $DIR/$tdir
2500         touch $DIR/$tdir/s
2501         touch $DIR/$tdir/exist
2502         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2503         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2504         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2505         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2506         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2507         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2508         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2509         return 0
2510 }
2511 run_test 31k "link to file: the same, non-existing, dir==============="
2512
2513 test_31m() {
2514         mkdir $DIR/d31m
2515         touch $DIR/d31m/s
2516         mkdir $DIR/d31m2
2517         touch $DIR/d31m2/exist
2518         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2519         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2520         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2521         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2522         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2523         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2524         return 0
2525 }
2526 run_test 31m "link to file: the same, non-existing, dir==============="
2527
2528 test_31n() {
2529         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2530         nlink=$(stat --format=%h $DIR/$tfile)
2531         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2532         local fd=$(free_fd)
2533         local cmd="exec $fd<$DIR/$tfile"
2534         eval $cmd
2535         cmd="exec $fd<&-"
2536         trap "eval $cmd" EXIT
2537         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2538         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2539         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2540         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2541         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2542         eval $cmd
2543 }
2544 run_test 31n "check link count of unlinked file"
2545
2546 link_one() {
2547         local TEMPNAME=$(mktemp $1_XXXXXX)
2548         mlink $TEMPNAME $1 2> /dev/null &&
2549                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2550         munlink $TEMPNAME
2551 }
2552
2553 test_31o() { # LU-2901
2554         test_mkdir $DIR/$tdir
2555         for LOOP in $(seq 100); do
2556                 rm -f $DIR/$tdir/$tfile*
2557                 for THREAD in $(seq 8); do
2558                         link_one $DIR/$tdir/$tfile.$LOOP &
2559                 done
2560                 wait
2561                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2562                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2563                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2564                         break || true
2565         done
2566 }
2567 run_test 31o "duplicate hard links with same filename"
2568
2569 test_31p() {
2570         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2571
2572         test_mkdir $DIR/$tdir
2573         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2574         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2575
2576         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2577                 error "open unlink test1 failed"
2578         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2579                 error "open unlink test2 failed"
2580
2581         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2582                 error "test1 still exists"
2583         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2584                 error "test2 still exists"
2585 }
2586 run_test 31p "remove of open striped directory"
2587
2588 cleanup_test32_mount() {
2589         local rc=0
2590         trap 0
2591         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2592         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2593         losetup -d $loopdev || true
2594         rm -rf $DIR/$tdir
2595         return $rc
2596 }
2597
2598 test_32a() {
2599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2600
2601         echo "== more mountpoints and symlinks ================="
2602         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2603         trap cleanup_test32_mount EXIT
2604         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2605         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2606                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2607         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2608                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2609         cleanup_test32_mount
2610 }
2611 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2612
2613 test_32b() {
2614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2615
2616         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2617         trap cleanup_test32_mount EXIT
2618         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2619         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2620                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2621         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2622                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2623         cleanup_test32_mount
2624 }
2625 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2626
2627 test_32c() {
2628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2629
2630         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2631         trap cleanup_test32_mount EXIT
2632         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2633         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2634                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2635         test_mkdir -p $DIR/$tdir/d2/test_dir
2636         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2637                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2638         cleanup_test32_mount
2639 }
2640 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2641
2642 test_32d() {
2643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2644
2645         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2646         trap cleanup_test32_mount EXIT
2647         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2648         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2649                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2650         test_mkdir -p $DIR/$tdir/d2/test_dir
2651         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2652                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2653         cleanup_test32_mount
2654 }
2655 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2656
2657 test_32e() {
2658         rm -fr $DIR/$tdir
2659         test_mkdir -p $DIR/$tdir/tmp
2660         local tmp_dir=$DIR/$tdir/tmp
2661         ln -s $DIR/$tdir $tmp_dir/symlink11
2662         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2663         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2664         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2665 }
2666 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2667
2668 test_32f() {
2669         rm -fr $DIR/$tdir
2670         test_mkdir -p $DIR/$tdir/tmp
2671         local tmp_dir=$DIR/$tdir/tmp
2672         ln -s $DIR/$tdir $tmp_dir/symlink11
2673         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2674         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2675         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2676 }
2677 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2678
2679 test_32g() {
2680         local tmp_dir=$DIR/$tdir/tmp
2681         test_mkdir -p $tmp_dir
2682         test_mkdir $DIR/${tdir}2
2683         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2684         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2685         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2686         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2687         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2688         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2689 }
2690 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2691
2692 test_32h() {
2693         rm -fr $DIR/$tdir $DIR/${tdir}2
2694         tmp_dir=$DIR/$tdir/tmp
2695         test_mkdir -p $tmp_dir
2696         test_mkdir $DIR/${tdir}2
2697         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2698         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2699         ls $tmp_dir/symlink12 || error "listing symlink12"
2700         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2701 }
2702 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2703
2704 test_32i() {
2705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2706
2707         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2708         trap cleanup_test32_mount EXIT
2709         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2710         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2711                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2712         touch $DIR/$tdir/test_file
2713         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2714                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2715         cleanup_test32_mount
2716 }
2717 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2718
2719 test_32j() {
2720         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2721
2722         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2723         trap cleanup_test32_mount EXIT
2724         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2725         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2726                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2727         touch $DIR/$tdir/test_file
2728         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2729                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2730         cleanup_test32_mount
2731 }
2732 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2733
2734 test_32k() {
2735         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2736
2737         rm -fr $DIR/$tdir
2738         trap cleanup_test32_mount EXIT
2739         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2740         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2741                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2742         test_mkdir -p $DIR/$tdir/d2
2743         touch $DIR/$tdir/d2/test_file || error "touch failed"
2744         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2745                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
2746         cleanup_test32_mount
2747 }
2748 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2749
2750 test_32l() {
2751         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2752
2753         rm -fr $DIR/$tdir
2754         trap cleanup_test32_mount EXIT
2755         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2756         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2757                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2758         test_mkdir -p $DIR/$tdir/d2
2759         touch $DIR/$tdir/d2/test_file || error "touch failed"
2760         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2761                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
2762         cleanup_test32_mount
2763 }
2764 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2765
2766 test_32m() {
2767         rm -fr $DIR/d32m
2768         test_mkdir -p $DIR/d32m/tmp
2769         TMP_DIR=$DIR/d32m/tmp
2770         ln -s $DIR $TMP_DIR/symlink11
2771         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2772         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
2773                 error "symlink11 not a link"
2774         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
2775                 error "symlink01 not a link"
2776 }
2777 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2778
2779 test_32n() {
2780         rm -fr $DIR/d32n
2781         test_mkdir -p $DIR/d32n/tmp
2782         TMP_DIR=$DIR/d32n/tmp
2783         ln -s $DIR $TMP_DIR/symlink11
2784         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2785         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
2786         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
2787 }
2788 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2789
2790 test_32o() {
2791         touch $DIR/$tfile
2792         test_mkdir -p $DIR/d32o/tmp
2793         TMP_DIR=$DIR/d32o/tmp
2794         ln -s $DIR/$tfile $TMP_DIR/symlink12
2795         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2796         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
2797                 error "symlink12 not a link"
2798         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
2799         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
2800                 error "$DIR/d32o/tmp/symlink12 not file type"
2801         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
2802                 error "$DIR/d32o/symlink02 not file type"
2803 }
2804 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2805
2806 test_32p() {
2807         log 32p_1
2808         rm -fr $DIR/d32p
2809         log 32p_2
2810         rm -f $DIR/$tfile
2811         log 32p_3
2812         touch $DIR/$tfile
2813         log 32p_4
2814         test_mkdir -p $DIR/d32p/tmp
2815         log 32p_5
2816         TMP_DIR=$DIR/d32p/tmp
2817         log 32p_6
2818         ln -s $DIR/$tfile $TMP_DIR/symlink12
2819         log 32p_7
2820         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2821         log 32p_8
2822         cat $DIR/d32p/tmp/symlink12 ||
2823                 error "Can't open $DIR/d32p/tmp/symlink12"
2824         log 32p_9
2825         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
2826         log 32p_10
2827 }
2828 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2829
2830 test_32q() {
2831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2832
2833         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2834         trap cleanup_test32_mount EXIT
2835         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2836         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2837         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2838                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2839         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2840         cleanup_test32_mount
2841 }
2842 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2843
2844 test_32r() {
2845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2846
2847         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2848         trap cleanup_test32_mount EXIT
2849         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2850         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2851         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2852                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2853         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2854         cleanup_test32_mount
2855 }
2856 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2857
2858 test_33aa() {
2859         rm -f $DIR/$tfile
2860         touch $DIR/$tfile
2861         chmod 444 $DIR/$tfile
2862         chown $RUNAS_ID $DIR/$tfile
2863         log 33_1
2864         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2865         log 33_2
2866 }
2867 run_test 33aa "write file with mode 444 (should return error)"
2868
2869 test_33a() {
2870         rm -fr $DIR/$tdir
2871         test_mkdir $DIR/$tdir
2872         chown $RUNAS_ID $DIR/$tdir
2873         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2874                 error "$RUNAS create $tdir/$tfile failed"
2875         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2876                 error "open RDWR" || true
2877 }
2878 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2879
2880 test_33b() {
2881         rm -fr $DIR/$tdir
2882         test_mkdir $DIR/$tdir
2883         chown $RUNAS_ID $DIR/$tdir
2884         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2885 }
2886 run_test 33b "test open file with malformed flags (No panic)"
2887
2888 test_33c() {
2889         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2890         remote_ost_nodsh && skip "remote OST with nodsh"
2891
2892         local ostnum
2893         local ostname
2894         local write_bytes
2895         local all_zeros
2896
2897         all_zeros=:
2898         rm -fr $DIR/$tdir
2899         test_mkdir $DIR/$tdir
2900         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2901
2902         sync
2903         for ostnum in $(seq $OSTCOUNT); do
2904                 # test-framework's OST numbering is one-based, while Lustre's
2905                 # is zero-based
2906                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2907                 # Parsing llobdstat's output sucks; we could grep the /proc
2908                 # path, but that's likely to not be as portable as using the
2909                 # llobdstat utility.  So we parse lctl output instead.
2910                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2911                         obdfilter/$ostname/stats |
2912                         awk '/^write_bytes/ {print $7}' )
2913                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2914                 if (( ${write_bytes:-0} > 0 ))
2915                 then
2916                         all_zeros=false
2917                         break;
2918                 fi
2919         done
2920
2921         $all_zeros || return 0
2922
2923         # Write four bytes
2924         echo foo > $DIR/$tdir/bar
2925         # Really write them
2926         sync
2927
2928         # Total up write_bytes after writing.  We'd better find non-zeros.
2929         for ostnum in $(seq $OSTCOUNT); do
2930                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2931                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2932                         obdfilter/$ostname/stats |
2933                         awk '/^write_bytes/ {print $7}' )
2934                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2935                 if (( ${write_bytes:-0} > 0 ))
2936                 then
2937                         all_zeros=false
2938                         break;
2939                 fi
2940         done
2941
2942         if $all_zeros
2943         then
2944                 for ostnum in $(seq $OSTCOUNT); do
2945                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2946                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2947                         do_facet ost$ostnum lctl get_param -n \
2948                                 obdfilter/$ostname/stats
2949                 done
2950                 error "OST not keeping write_bytes stats (b22312)"
2951         fi
2952 }
2953 run_test 33c "test llobdstat and write_bytes"
2954
2955 test_33d() {
2956         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
2957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2958
2959         local MDTIDX=1
2960         local remote_dir=$DIR/$tdir/remote_dir
2961
2962         test_mkdir $DIR/$tdir
2963         $LFS mkdir -i $MDTIDX $remote_dir ||
2964                 error "create remote directory failed"
2965
2966         touch $remote_dir/$tfile
2967         chmod 444 $remote_dir/$tfile
2968         chown $RUNAS_ID $remote_dir/$tfile
2969
2970         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2971
2972         chown $RUNAS_ID $remote_dir
2973         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2974                                         error "create" || true
2975         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2976                                     error "open RDWR" || true
2977         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2978 }
2979 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2980
2981 test_33e() {
2982         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2983
2984         mkdir $DIR/$tdir
2985
2986         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2987         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2988         mkdir $DIR/$tdir/local_dir
2989
2990         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2991         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2992         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2993
2994         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2995                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2996
2997         rmdir $DIR/$tdir/* || error "rmdir failed"
2998
2999         umask 777
3000         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3001         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3002         mkdir $DIR/$tdir/local_dir
3003
3004         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3005         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3006         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3007
3008         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3009                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3010
3011         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3012
3013         umask 000
3014         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3015         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3016         mkdir $DIR/$tdir/local_dir
3017
3018         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3019         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3020         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3021
3022         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3023                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3024 }
3025 run_test 33e "mkdir and striped directory should have same mode"
3026
3027 cleanup_33f() {
3028         trap 0
3029         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3030 }
3031
3032 test_33f() {
3033         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3034         remote_mds_nodsh && skip "remote MDS with nodsh"
3035
3036         mkdir $DIR/$tdir
3037         chmod go+rwx $DIR/$tdir
3038         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3039         trap cleanup_33f EXIT
3040
3041         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3042                 error "cannot create striped directory"
3043
3044         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3045                 error "cannot create files in striped directory"
3046
3047         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3048                 error "cannot remove files in striped directory"
3049
3050         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3051                 error "cannot remove striped directory"
3052
3053         cleanup_33f
3054 }
3055 run_test 33f "nonroot user can create, access, and remove a striped directory"
3056
3057 test_33g() {
3058         mkdir -p $DIR/$tdir/dir2
3059
3060         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3061         echo $err
3062         [[ $err =~ "exists" ]] || error "Not exists error"
3063 }
3064 run_test 33g "nonroot user create already existing root created file"
3065
3066 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3067 test_34a() {
3068         rm -f $DIR/f34
3069         $MCREATE $DIR/f34 || error "mcreate failed"
3070         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3071                 error "getstripe failed"
3072         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3073         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3074                 error "getstripe failed"
3075         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3076                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3077 }
3078 run_test 34a "truncate file that has not been opened ==========="
3079
3080 test_34b() {
3081         [ ! -f $DIR/f34 ] && test_34a
3082         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3083                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3084         $OPENFILE -f O_RDONLY $DIR/f34
3085         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3086                 error "getstripe failed"
3087         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3088                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3089 }
3090 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3091
3092 test_34c() {
3093         [ ! -f $DIR/f34 ] && test_34a
3094         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3095                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3096         $OPENFILE -f O_RDWR $DIR/f34
3097         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3098         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3099                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3100 }
3101 run_test 34c "O_RDWR opening file-with-size works =============="
3102
3103 test_34d() {
3104         [ ! -f $DIR/f34 ] && test_34a
3105         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3106                 error "dd failed"
3107         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3108                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3109         rm $DIR/f34
3110 }
3111 run_test 34d "write to sparse file ============================="
3112
3113 test_34e() {
3114         rm -f $DIR/f34e
3115         $MCREATE $DIR/f34e || error "mcreate failed"
3116         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3117         $CHECKSTAT -s 1000 $DIR/f34e ||
3118                 error "Size of $DIR/f34e not equal to 1000 bytes"
3119         $OPENFILE -f O_RDWR $DIR/f34e
3120         $CHECKSTAT -s 1000 $DIR/f34e ||
3121                 error "Size of $DIR/f34e not equal to 1000 bytes"
3122 }
3123 run_test 34e "create objects, some with size and some without =="
3124
3125 test_34f() { # bug 6242, 6243
3126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3127
3128         SIZE34F=48000
3129         rm -f $DIR/f34f
3130         $MCREATE $DIR/f34f || error "mcreate failed"
3131         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3132         dd if=$DIR/f34f of=$TMP/f34f
3133         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3134         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3135         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3136         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3137         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3138 }
3139 run_test 34f "read from a file with no objects until EOF ======="
3140
3141 test_34g() {
3142         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3143
3144         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3145                 error "dd failed"
3146         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3147         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3148                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3149         cancel_lru_locks osc
3150         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3151                 error "wrong size after lock cancel"
3152
3153         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3154         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3155                 error "expanding truncate failed"
3156         cancel_lru_locks osc
3157         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3158                 error "wrong expanded size after lock cancel"
3159 }
3160 run_test 34g "truncate long file ==============================="
3161
3162 test_34h() {
3163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3164
3165         local gid=10
3166         local sz=1000
3167
3168         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3169         sync # Flush the cache so that multiop below does not block on cache
3170              # flush when getting the group lock
3171         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3172         MULTIPID=$!
3173
3174         # Since just timed wait is not good enough, let's do a sync write
3175         # that way we are sure enough time for a roundtrip + processing
3176         # passed + 2 seconds of extra margin.
3177         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
3178         rm $DIR/${tfile}-1
3179         sleep 2
3180
3181         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3182                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3183                 kill -9 $MULTIPID
3184         fi
3185         wait $MULTIPID
3186         local nsz=`stat -c %s $DIR/$tfile`
3187         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3188 }
3189 run_test 34h "ftruncate file under grouplock should not block"
3190
3191 test_35a() {
3192         cp /bin/sh $DIR/f35a
3193         chmod 444 $DIR/f35a
3194         chown $RUNAS_ID $DIR/f35a
3195         $RUNAS $DIR/f35a && error || true
3196         rm $DIR/f35a
3197 }
3198 run_test 35a "exec file with mode 444 (should return and not leak)"
3199
3200 test_36a() {
3201         rm -f $DIR/f36
3202         utime $DIR/f36 || error "utime failed for MDS"
3203 }
3204 run_test 36a "MDS utime check (mknod, utime)"
3205
3206 test_36b() {
3207         echo "" > $DIR/f36
3208         utime $DIR/f36 || error "utime failed for OST"
3209 }
3210 run_test 36b "OST utime check (open, utime)"
3211
3212 test_36c() {
3213         rm -f $DIR/d36/f36
3214         test_mkdir $DIR/d36
3215         chown $RUNAS_ID $DIR/d36
3216         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3217 }
3218 run_test 36c "non-root MDS utime check (mknod, utime)"
3219
3220 test_36d() {
3221         [ ! -d $DIR/d36 ] && test_36c
3222         echo "" > $DIR/d36/f36
3223         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3224 }
3225 run_test 36d "non-root OST utime check (open, utime)"
3226
3227 test_36e() {
3228         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3229
3230         test_mkdir $DIR/$tdir
3231         touch $DIR/$tdir/$tfile
3232         $RUNAS utime $DIR/$tdir/$tfile &&
3233                 error "utime worked, expected failure" || true
3234 }
3235 run_test 36e "utime on non-owned file (should return error)"
3236
3237 subr_36fh() {
3238         local fl="$1"
3239         local LANG_SAVE=$LANG
3240         local LC_LANG_SAVE=$LC_LANG
3241         export LANG=C LC_LANG=C # for date language
3242
3243         DATESTR="Dec 20  2000"
3244         test_mkdir $DIR/$tdir
3245         lctl set_param fail_loc=$fl
3246         date; date +%s
3247         cp /etc/hosts $DIR/$tdir/$tfile
3248         sync & # write RPC generated with "current" inode timestamp, but delayed
3249         sleep 1
3250         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3251         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3252         cancel_lru_locks osc
3253         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3254         date; date +%s
3255         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3256                 echo "BEFORE: $LS_BEFORE" && \
3257                 echo "AFTER : $LS_AFTER" && \
3258                 echo "WANT  : $DATESTR" && \
3259                 error "$DIR/$tdir/$tfile timestamps changed" || true
3260
3261         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3262 }
3263
3264 test_36f() {
3265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3266
3267         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3268         subr_36fh "0x80000214"
3269 }
3270 run_test 36f "utime on file racing with OST BRW write =========="
3271
3272 test_36g() {
3273         remote_ost_nodsh && skip "remote OST with nodsh"
3274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3275
3276         local fmd_max_age
3277         local fmd_before
3278         local fmd_after
3279
3280         test_mkdir $DIR/$tdir
3281         fmd_max_age=$(do_facet ost1 \
3282                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3283                 head -n 1")
3284
3285         fmd_before=$(do_facet ost1 \
3286                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3287         touch $DIR/$tdir/$tfile
3288         sleep $((fmd_max_age + 12))
3289         fmd_after=$(do_facet ost1 \
3290                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3291
3292         echo "fmd_before: $fmd_before"
3293         echo "fmd_after: $fmd_after"
3294         [[ $fmd_after -gt $fmd_before ]] &&
3295                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3296                 error "fmd didn't expire after ping" || true
3297 }
3298 run_test 36g "filter mod data cache expiry ====================="
3299
3300 test_36h() {
3301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3302
3303         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3304         subr_36fh "0x80000227"
3305 }
3306 run_test 36h "utime on file racing with OST BRW write =========="
3307
3308 test_36i() {
3309         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3310
3311         test_mkdir $DIR/$tdir
3312         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3313
3314         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3315         local new_mtime=$((mtime + 200))
3316
3317         #change Modify time of striped dir
3318         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3319                         error "change mtime failed"
3320
3321         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3322
3323         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3324 }
3325 run_test 36i "change mtime on striped directory"
3326
3327 # test_37 - duplicate with tests 32q 32r
3328
3329 test_38() {
3330         local file=$DIR/$tfile
3331         touch $file
3332         openfile -f O_DIRECTORY $file
3333         local RC=$?
3334         local ENOTDIR=20
3335         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3336         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3337 }
3338 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3339
3340 test_39a() { # was test_39
3341         touch $DIR/$tfile
3342         touch $DIR/${tfile}2
3343 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3344 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3345 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3346         sleep 2
3347         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3348         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3349                 echo "mtime"
3350                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3351                 echo "atime"
3352                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3353                 echo "ctime"
3354                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3355                 error "O_TRUNC didn't change timestamps"
3356         fi
3357 }
3358 run_test 39a "mtime changed on create"
3359
3360 test_39b() {
3361         test_mkdir -c1 $DIR/$tdir
3362         cp -p /etc/passwd $DIR/$tdir/fopen
3363         cp -p /etc/passwd $DIR/$tdir/flink
3364         cp -p /etc/passwd $DIR/$tdir/funlink
3365         cp -p /etc/passwd $DIR/$tdir/frename
3366         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3367
3368         sleep 1
3369         echo "aaaaaa" >> $DIR/$tdir/fopen
3370         echo "aaaaaa" >> $DIR/$tdir/flink
3371         echo "aaaaaa" >> $DIR/$tdir/funlink
3372         echo "aaaaaa" >> $DIR/$tdir/frename
3373
3374         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3375         local link_new=`stat -c %Y $DIR/$tdir/flink`
3376         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3377         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3378
3379         cat $DIR/$tdir/fopen > /dev/null
3380         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3381         rm -f $DIR/$tdir/funlink2
3382         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3383
3384         for (( i=0; i < 2; i++ )) ; do
3385                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3386                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3387                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3388                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3389
3390                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3391                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3392                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3393                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3394
3395                 cancel_lru_locks osc
3396                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3397         done
3398 }
3399 run_test 39b "mtime change on open, link, unlink, rename  ======"
3400
3401 # this should be set to past
3402 TEST_39_MTIME=`date -d "1 year ago" +%s`
3403
3404 # bug 11063
3405 test_39c() {
3406         touch $DIR1/$tfile
3407         sleep 2
3408         local mtime0=`stat -c %Y $DIR1/$tfile`
3409
3410         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3411         local mtime1=`stat -c %Y $DIR1/$tfile`
3412         [ "$mtime1" = $TEST_39_MTIME ] || \
3413                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3414
3415         local d1=`date +%s`
3416         echo hello >> $DIR1/$tfile
3417         local d2=`date +%s`
3418         local mtime2=`stat -c %Y $DIR1/$tfile`
3419         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3420                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3421
3422         mv $DIR1/$tfile $DIR1/$tfile-1
3423
3424         for (( i=0; i < 2; i++ )) ; do
3425                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3426                 [ "$mtime2" = "$mtime3" ] || \
3427                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3428
3429                 cancel_lru_locks osc
3430                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3431         done
3432 }
3433 run_test 39c "mtime change on rename ==========================="
3434
3435 # bug 21114
3436 test_39d() {
3437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3438
3439         touch $DIR1/$tfile
3440         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3441
3442         for (( i=0; i < 2; i++ )) ; do
3443                 local mtime=`stat -c %Y $DIR1/$tfile`
3444                 [ $mtime = $TEST_39_MTIME ] || \
3445                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3446
3447                 cancel_lru_locks osc
3448                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3449         done
3450 }
3451 run_test 39d "create, utime, stat =============================="
3452
3453 # bug 21114
3454 test_39e() {
3455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3456
3457         touch $DIR1/$tfile
3458         local mtime1=`stat -c %Y $DIR1/$tfile`
3459
3460         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3461
3462         for (( i=0; i < 2; i++ )) ; do
3463                 local mtime2=`stat -c %Y $DIR1/$tfile`
3464                 [ $mtime2 = $TEST_39_MTIME ] || \
3465                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3466
3467                 cancel_lru_locks osc
3468                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3469         done
3470 }
3471 run_test 39e "create, stat, utime, stat ========================"
3472
3473 # bug 21114
3474 test_39f() {
3475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3476
3477         touch $DIR1/$tfile
3478         mtime1=`stat -c %Y $DIR1/$tfile`
3479
3480         sleep 2
3481         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3482
3483         for (( i=0; i < 2; i++ )) ; do
3484                 local mtime2=`stat -c %Y $DIR1/$tfile`
3485                 [ $mtime2 = $TEST_39_MTIME ] || \
3486                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3487
3488                 cancel_lru_locks osc
3489                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3490         done
3491 }
3492 run_test 39f "create, stat, sleep, utime, stat ================="
3493
3494 # bug 11063
3495 test_39g() {
3496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3497
3498         echo hello >> $DIR1/$tfile
3499         local mtime1=`stat -c %Y $DIR1/$tfile`
3500
3501         sleep 2
3502         chmod o+r $DIR1/$tfile
3503
3504         for (( i=0; i < 2; i++ )) ; do
3505                 local mtime2=`stat -c %Y $DIR1/$tfile`
3506                 [ "$mtime1" = "$mtime2" ] || \
3507                         error "lost mtime: $mtime2, should be $mtime1"
3508
3509                 cancel_lru_locks osc
3510                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3511         done
3512 }
3513 run_test 39g "write, chmod, stat ==============================="
3514
3515 # bug 11063
3516 test_39h() {
3517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3518
3519         touch $DIR1/$tfile
3520         sleep 1
3521
3522         local d1=`date`
3523         echo hello >> $DIR1/$tfile
3524         local mtime1=`stat -c %Y $DIR1/$tfile`
3525
3526         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3527         local d2=`date`
3528         if [ "$d1" != "$d2" ]; then
3529                 echo "write and touch not within one second"
3530         else
3531                 for (( i=0; i < 2; i++ )) ; do
3532                         local mtime2=`stat -c %Y $DIR1/$tfile`
3533                         [ "$mtime2" = $TEST_39_MTIME ] || \
3534                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3535
3536                         cancel_lru_locks osc
3537                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3538                 done
3539         fi
3540 }
3541 run_test 39h "write, utime within one second, stat ============="
3542
3543 test_39i() {
3544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3545
3546         touch $DIR1/$tfile
3547         sleep 1
3548
3549         echo hello >> $DIR1/$tfile
3550         local mtime1=`stat -c %Y $DIR1/$tfile`
3551
3552         mv $DIR1/$tfile $DIR1/$tfile-1
3553
3554         for (( i=0; i < 2; i++ )) ; do
3555                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3556
3557                 [ "$mtime1" = "$mtime2" ] || \
3558                         error "lost mtime: $mtime2, should be $mtime1"
3559
3560                 cancel_lru_locks osc
3561                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3562         done
3563 }
3564 run_test 39i "write, rename, stat =============================="
3565
3566 test_39j() {
3567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3568
3569         start_full_debug_logging
3570         touch $DIR1/$tfile
3571         sleep 1
3572
3573         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3574         lctl set_param fail_loc=0x80000412
3575         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3576                 error "multiop failed"
3577         local multipid=$!
3578         local mtime1=`stat -c %Y $DIR1/$tfile`
3579
3580         mv $DIR1/$tfile $DIR1/$tfile-1
3581
3582         kill -USR1 $multipid
3583         wait $multipid || error "multiop close failed"
3584
3585         for (( i=0; i < 2; i++ )) ; do
3586                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3587                 [ "$mtime1" = "$mtime2" ] ||
3588                         error "mtime is lost on close: $mtime2, " \
3589                               "should be $mtime1"
3590
3591                 cancel_lru_locks osc
3592                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3593         done
3594         lctl set_param fail_loc=0
3595         stop_full_debug_logging
3596 }
3597 run_test 39j "write, rename, close, stat ======================="
3598
3599 test_39k() {
3600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3601
3602         touch $DIR1/$tfile
3603         sleep 1
3604
3605         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3606         local multipid=$!
3607         local mtime1=`stat -c %Y $DIR1/$tfile`
3608
3609         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3610
3611         kill -USR1 $multipid
3612         wait $multipid || error "multiop close failed"
3613
3614         for (( i=0; i < 2; i++ )) ; do
3615                 local mtime2=`stat -c %Y $DIR1/$tfile`
3616
3617                 [ "$mtime2" = $TEST_39_MTIME ] || \
3618                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3619
3620                 cancel_lru_locks osc
3621                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3622         done
3623 }
3624 run_test 39k "write, utime, close, stat ========================"
3625
3626 # this should be set to future
3627 TEST_39_ATIME=`date -d "1 year" +%s`
3628
3629 test_39l() {
3630         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3631         remote_mds_nodsh && skip "remote MDS with nodsh"
3632
3633         local atime_diff=$(do_facet $SINGLEMDS \
3634                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3635         rm -rf $DIR/$tdir
3636         mkdir -p $DIR/$tdir
3637
3638         # test setting directory atime to future
3639         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3640         local atime=$(stat -c %X $DIR/$tdir)
3641         [ "$atime" = $TEST_39_ATIME ] ||
3642                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3643
3644         # test setting directory atime from future to now
3645         local now=$(date +%s)
3646         touch -a -d @$now $DIR/$tdir
3647
3648         atime=$(stat -c %X $DIR/$tdir)
3649         [ "$atime" -eq "$now"  ] ||
3650                 error "atime is not updated from future: $atime, $now"
3651
3652         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3653         sleep 3
3654
3655         # test setting directory atime when now > dir atime + atime_diff
3656         local d1=$(date +%s)
3657         ls $DIR/$tdir
3658         local d2=$(date +%s)
3659         cancel_lru_locks mdc
3660         atime=$(stat -c %X $DIR/$tdir)
3661         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3662                 error "atime is not updated  : $atime, should be $d2"
3663
3664         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3665         sleep 3
3666
3667         # test not setting directory atime when now < dir atime + atime_diff
3668         ls $DIR/$tdir
3669         cancel_lru_locks mdc
3670         atime=$(stat -c %X $DIR/$tdir)
3671         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3672                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3673
3674         do_facet $SINGLEMDS \
3675                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3676 }
3677 run_test 39l "directory atime update ==========================="
3678
3679 test_39m() {
3680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3681
3682         touch $DIR1/$tfile
3683         sleep 2
3684         local far_past_mtime=$(date -d "May 29 1953" +%s)
3685         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3686
3687         touch -m -d @$far_past_mtime $DIR1/$tfile
3688         touch -a -d @$far_past_atime $DIR1/$tfile
3689
3690         for (( i=0; i < 2; i++ )) ; do
3691                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3692                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3693                         error "atime or mtime set incorrectly"
3694
3695                 cancel_lru_locks osc
3696                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3697         done
3698 }
3699 run_test 39m "test atime and mtime before 1970"
3700
3701 test_39n() { # LU-3832
3702         remote_mds_nodsh && skip "remote MDS with nodsh"
3703
3704         local atime_diff=$(do_facet $SINGLEMDS \
3705                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3706         local atime0
3707         local atime1
3708         local atime2
3709
3710         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3711
3712         rm -rf $DIR/$tfile
3713         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3714         atime0=$(stat -c %X $DIR/$tfile)
3715
3716         sleep 5
3717         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3718         atime1=$(stat -c %X $DIR/$tfile)
3719
3720         sleep 5
3721         cancel_lru_locks mdc
3722         cancel_lru_locks osc
3723         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3724         atime2=$(stat -c %X $DIR/$tfile)
3725
3726         do_facet $SINGLEMDS \
3727                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3728
3729         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3730         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3731 }
3732 run_test 39n "check that O_NOATIME is honored"
3733
3734 test_39o() {
3735         TESTDIR=$DIR/$tdir/$tfile
3736         [ -e $TESTDIR ] && rm -rf $TESTDIR
3737         mkdir -p $TESTDIR
3738         cd $TESTDIR
3739         links1=2
3740         ls
3741         mkdir a b
3742         ls
3743         links2=$(stat -c %h .)
3744         [ $(($links1 + 2)) != $links2 ] &&
3745                 error "wrong links count $(($links1 + 2)) != $links2"
3746         rmdir b
3747         links3=$(stat -c %h .)
3748         [ $(($links1 + 1)) != $links3 ] &&
3749                 error "wrong links count $links1 != $links3"
3750         return 0
3751 }
3752 run_test 39o "directory cached attributes updated after create"
3753
3754 test_39p() {
3755         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3756
3757         local MDTIDX=1
3758         TESTDIR=$DIR/$tdir/$tdir
3759         [ -e $TESTDIR ] && rm -rf $TESTDIR
3760         test_mkdir -p $TESTDIR
3761         cd $TESTDIR
3762         links1=2
3763         ls
3764         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3765         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3766         ls
3767         links2=$(stat -c %h .)
3768         [ $(($links1 + 2)) != $links2 ] &&
3769                 error "wrong links count $(($links1 + 2)) != $links2"
3770         rmdir remote_dir2
3771         links3=$(stat -c %h .)
3772         [ $(($links1 + 1)) != $links3 ] &&
3773                 error "wrong links count $links1 != $links3"
3774         return 0
3775 }
3776 run_test 39p "remote directory cached attributes updated after create ========"
3777
3778
3779 test_39q() { # LU-8041
3780         local testdir=$DIR/$tdir
3781         mkdir -p $testdir
3782         multiop_bg_pause $testdir D_c || error "multiop failed"
3783         local multipid=$!
3784         cancel_lru_locks mdc
3785         kill -USR1 $multipid
3786         local atime=$(stat -c %X $testdir)
3787         [ "$atime" -ne 0 ] || error "atime is zero"
3788 }
3789 run_test 39q "close won't zero out atime"
3790
3791 test_40() {
3792         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3793         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3794                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3795         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3796                 error "$tfile is not 4096 bytes in size"
3797 }
3798 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3799
3800 test_41() {
3801         # bug 1553
3802         small_write $DIR/f41 18
3803 }
3804 run_test 41 "test small file write + fstat ====================="
3805
3806 count_ost_writes() {
3807         lctl get_param -n ${OSC}.*.stats |
3808                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3809                         END { printf("%0.0f", writes) }'
3810 }
3811
3812 # decent default
3813 WRITEBACK_SAVE=500
3814 DIRTY_RATIO_SAVE=40
3815 MAX_DIRTY_RATIO=50
3816 BG_DIRTY_RATIO_SAVE=10
3817 MAX_BG_DIRTY_RATIO=25
3818
3819 start_writeback() {
3820         trap 0
3821         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3822         # dirty_ratio, dirty_background_ratio
3823         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3824                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3825                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3826                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3827         else
3828                 # if file not here, we are a 2.4 kernel
3829                 kill -CONT `pidof kupdated`
3830         fi
3831 }
3832
3833 stop_writeback() {
3834         # setup the trap first, so someone cannot exit the test at the
3835         # exact wrong time and mess up a machine
3836         trap start_writeback EXIT
3837         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3838         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3839                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3840                 sysctl -w vm.dirty_writeback_centisecs=0
3841                 sysctl -w vm.dirty_writeback_centisecs=0
3842                 # save and increase /proc/sys/vm/dirty_ratio
3843                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3844                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3845                 # save and increase /proc/sys/vm/dirty_background_ratio
3846                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3847                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3848         else
3849                 # if file not here, we are a 2.4 kernel
3850                 kill -STOP `pidof kupdated`
3851         fi
3852 }
3853
3854 # ensure that all stripes have some grant before we test client-side cache
3855 setup_test42() {
3856         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3857                 dd if=/dev/zero of=$i bs=4k count=1
3858                 rm $i
3859         done
3860 }
3861
3862 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3863 # file truncation, and file removal.
3864 test_42a() {
3865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3866
3867         setup_test42
3868         cancel_lru_locks $OSC
3869         stop_writeback
3870         sync; sleep 1; sync # just to be safe
3871         BEFOREWRITES=`count_ost_writes`
3872         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3873         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3874         AFTERWRITES=`count_ost_writes`
3875         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3876                 error "$BEFOREWRITES < $AFTERWRITES"
3877         start_writeback
3878 }
3879 run_test 42a "ensure that we don't flush on close"
3880
3881 test_42b() {
3882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3883
3884         setup_test42
3885         cancel_lru_locks $OSC
3886         stop_writeback
3887         sync
3888         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3889         BEFOREWRITES=$(count_ost_writes)
3890         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3891         AFTERWRITES=$(count_ost_writes)
3892         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3893                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3894         fi
3895         BEFOREWRITES=$(count_ost_writes)
3896         sync || error "sync: $?"
3897         AFTERWRITES=$(count_ost_writes)
3898         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3899                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3900         fi
3901         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3902         start_writeback
3903         return 0
3904 }
3905 run_test 42b "test destroy of file with cached dirty data ======"
3906
3907 # if these tests just want to test the effect of truncation,
3908 # they have to be very careful.  consider:
3909 # - the first open gets a {0,EOF}PR lock
3910 # - the first write conflicts and gets a {0, count-1}PW
3911 # - the rest of the writes are under {count,EOF}PW
3912 # - the open for truncate tries to match a {0,EOF}PR
3913 #   for the filesize and cancels the PWs.
3914 # any number of fixes (don't get {0,EOF} on open, match
3915 # composite locks, do smarter file size management) fix
3916 # this, but for now we want these tests to verify that
3917 # the cancellation with truncate intent works, so we
3918 # start the file with a full-file pw lock to match against
3919 # until the truncate.
3920 trunc_test() {
3921         test=$1
3922         file=$DIR/$test
3923         offset=$2
3924         cancel_lru_locks $OSC
3925         stop_writeback
3926         # prime the file with 0,EOF PW to match
3927         touch $file
3928         $TRUNCATE $file 0
3929         sync; sync
3930         # now the real test..
3931         dd if=/dev/zero of=$file bs=1024 count=100
3932         BEFOREWRITES=`count_ost_writes`
3933         $TRUNCATE $file $offset
3934         cancel_lru_locks $OSC
3935         AFTERWRITES=`count_ost_writes`
3936         start_writeback
3937 }
3938
3939 test_42c() {
3940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3941
3942         trunc_test 42c 1024
3943         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
3944                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3945         rm $file
3946 }
3947 run_test 42c "test partial truncate of file with cached dirty data"
3948
3949 test_42d() {
3950         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3951
3952         trunc_test 42d 0
3953         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
3954                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3955         rm $file
3956 }
3957 run_test 42d "test complete truncate of file with cached dirty data"
3958
3959 test_42e() { # bug22074
3960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3961
3962         local TDIR=$DIR/${tdir}e
3963         local pagesz=$(page_size)
3964         local pages=16 # hardcoded 16 pages, don't change it.
3965         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3966         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3967         local max_dirty_mb
3968         local warmup_files
3969
3970         test_mkdir $DIR/${tdir}e
3971         $SETSTRIPE -c 1 $TDIR
3972         createmany -o $TDIR/f $files
3973
3974         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3975
3976         # we assume that with $OSTCOUNT files, at least one of them will
3977         # be allocated on OST0.
3978         warmup_files=$((OSTCOUNT * max_dirty_mb))
3979         createmany -o $TDIR/w $warmup_files
3980
3981         # write a large amount of data into one file and sync, to get good
3982         # avail_grant number from OST.
3983         for ((i=0; i<$warmup_files; i++)); do
3984                 idx=$($GETSTRIPE -i $TDIR/w$i)
3985                 [ $idx -ne 0 ] && continue
3986                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3987                 break
3988         done
3989         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3990         sync
3991         $LCTL get_param $proc_osc0/cur_dirty_bytes
3992         $LCTL get_param $proc_osc0/cur_grant_bytes
3993
3994         # create as much dirty pages as we can while not to trigger the actual
3995         # RPCs directly. but depends on the env, VFS may trigger flush during this
3996         # period, hopefully we are good.
3997         for ((i=0; i<$warmup_files; i++)); do
3998                 idx=$($GETSTRIPE -i $TDIR/w$i)
3999                 [ $idx -ne 0 ] && continue
4000                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4001         done
4002         $LCTL get_param $proc_osc0/cur_dirty_bytes
4003         $LCTL get_param $proc_osc0/cur_grant_bytes
4004
4005         # perform the real test
4006         $LCTL set_param $proc_osc0/rpc_stats 0
4007         for ((;i<$files; i++)); do
4008                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4009                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
4010         done
4011         sync
4012         $LCTL get_param $proc_osc0/rpc_stats
4013
4014         local percent=0
4015         local have_ppr=false
4016         $LCTL get_param $proc_osc0/rpc_stats |
4017                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4018                         # skip lines until we are at the RPC histogram data
4019                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4020                         $have_ppr || continue
4021
4022                         # we only want the percent stat for < 16 pages
4023                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4024
4025                         percent=$((percent + WPCT))
4026                         if [[ $percent -gt 15 ]]; then
4027                                 error "less than 16-pages write RPCs" \
4028                                       "$percent% > 15%"
4029                                 break
4030                         fi
4031                 done
4032         rm -rf $TDIR
4033 }
4034 run_test 42e "verify sub-RPC writes are not done synchronously"
4035
4036 test_43A() { # was test_43
4037         test_mkdir $DIR/$tdir
4038         cp -p /bin/ls $DIR/$tdir/$tfile
4039         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4040         pid=$!
4041         # give multiop a chance to open
4042         sleep 1
4043
4044         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4045         kill -USR1 $pid
4046 }
4047 run_test 43A "execution of file opened for write should return -ETXTBSY"
4048
4049 test_43a() {
4050         test_mkdir $DIR/$tdir
4051         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4052                 cp -p multiop $DIR/$tdir/multiop
4053         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4054                 error "multiop open $TMP/$tfile.junk failed"
4055         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4056         MULTIOP_PID=$!
4057         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
4058         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4059         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4060 }
4061 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4062
4063 test_43b() {
4064         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4065
4066         test_mkdir $DIR/$tdir
4067         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4068                 cp -p multiop $DIR/$tdir/multiop
4069         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4070                 error "multiop open $TMP/$tfile.junk failed"
4071         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4072         MULTIOP_PID=$!
4073         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
4074         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4075         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4076 }
4077 run_test 43b "truncate of file being executed should return -ETXTBSY"
4078
4079 test_43c() {
4080         local testdir="$DIR/$tdir"
4081         test_mkdir $testdir
4082         cp $SHELL $testdir/
4083         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4084                 ( cd $testdir && md5sum -c )
4085 }
4086 run_test 43c "md5sum of copy into lustre"
4087
4088 test_44A() { # was test_44
4089         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4090
4091         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4092         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4093 }
4094 run_test 44A "zero length read from a sparse stripe"
4095
4096 test_44a() {
4097         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4098                 awk '{ print $2 }')
4099         [ -z "$nstripe" ] && skip "can't get stripe info"
4100         [[ $nstripe -gt $OSTCOUNT ]] &&
4101                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4102
4103         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4104                 awk '{ print $2 }')
4105         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4106                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4107                         awk '{ print $2 }')
4108         fi
4109
4110         OFFSETS="0 $((stride/2)) $((stride-1))"
4111         for offset in $OFFSETS; do
4112                 for i in $(seq 0 $((nstripe-1))); do
4113                         local GLOBALOFFSETS=""
4114                         # size in Bytes
4115                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4116                         local myfn=$DIR/d44a-$size
4117                         echo "--------writing $myfn at $size"
4118                         ll_sparseness_write $myfn $size ||
4119                                 error "ll_sparseness_write"
4120                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4121                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4122                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4123
4124                         for j in $(seq 0 $((nstripe-1))); do
4125                                 # size in Bytes
4126                                 size=$((((j + $nstripe )*$stride + $offset)))
4127                                 ll_sparseness_write $myfn $size ||
4128                                         error "ll_sparseness_write"
4129                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4130                         done
4131                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4132                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4133                         rm -f $myfn
4134                 done
4135         done
4136 }
4137 run_test 44a "test sparse pwrite ==============================="
4138
4139 dirty_osc_total() {
4140         tot=0
4141         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4142                 tot=$(($tot + $d))
4143         done
4144         echo $tot
4145 }
4146 do_dirty_record() {
4147         before=`dirty_osc_total`
4148         echo executing "\"$*\""
4149         eval $*
4150         after=`dirty_osc_total`
4151         echo before $before, after $after
4152 }
4153 test_45() {
4154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4155
4156         f="$DIR/f45"
4157         # Obtain grants from OST if it supports it
4158         echo blah > ${f}_grant
4159         stop_writeback
4160         sync
4161         do_dirty_record "echo blah > $f"
4162         [[ $before -eq $after ]] && error "write wasn't cached"
4163         do_dirty_record "> $f"
4164         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4165         do_dirty_record "echo blah > $f"
4166         [[ $before -eq $after ]] && error "write wasn't cached"
4167         do_dirty_record "sync"
4168         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4169         do_dirty_record "echo blah > $f"
4170         [[ $before -eq $after ]] && error "write wasn't cached"
4171         do_dirty_record "cancel_lru_locks osc"
4172         [[ $before -gt $after ]] ||
4173                 error "lock cancellation didn't lower dirty count"
4174         start_writeback
4175 }
4176 run_test 45 "osc io page accounting ============================"
4177
4178 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4179 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4180 # objects offset and an assert hit when an rpc was built with 1023's mapped
4181 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4182 test_46() {
4183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4184
4185         f="$DIR/f46"
4186         stop_writeback
4187         sync
4188         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4189         sync
4190         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
4191         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4192         sync
4193         start_writeback
4194 }
4195 run_test 46 "dirtying a previously written page ================"
4196
4197 # test_47 is removed "Device nodes check" is moved to test_28
4198
4199 test_48a() { # bug 2399
4200         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
4201         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
4202                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4203
4204         test_mkdir $DIR/$tdir
4205         cd $DIR/$tdir
4206         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4207         test_mkdir $DIR/$tdir
4208         touch foo || error "'touch foo' failed after recreating cwd"
4209         test_mkdir bar
4210         touch .foo || error "'touch .foo' failed after recreating cwd"
4211         test_mkdir .bar
4212         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4213         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4214         cd . || error "'cd .' failed after recreating cwd"
4215         mkdir . && error "'mkdir .' worked after recreating cwd"
4216         rmdir . && error "'rmdir .' worked after recreating cwd"
4217         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4218         cd .. || error "'cd ..' failed after recreating cwd"
4219 }
4220 run_test 48a "Access renamed working dir (should return errors)="
4221
4222 test_48b() { # bug 2399
4223         rm -rf $DIR/$tdir
4224         test_mkdir $DIR/$tdir
4225         cd $DIR/$tdir
4226         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4227         touch foo && error "'touch foo' worked after removing cwd"
4228         mkdir foo && error "'mkdir foo' worked after removing cwd"
4229         touch .foo && error "'touch .foo' worked after removing cwd"
4230         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4231         ls . > /dev/null && error "'ls .' worked after removing cwd"
4232         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4233         mkdir . && error "'mkdir .' worked after removing cwd"
4234         rmdir . && error "'rmdir .' worked after removing cwd"
4235         ln -s . foo && error "'ln -s .' worked after removing cwd"
4236         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4237 }
4238 run_test 48b "Access removed working dir (should return errors)="
4239
4240 test_48c() { # bug 2350
4241         #lctl set_param debug=-1
4242         #set -vx
4243         rm -rf $DIR/$tdir
4244         test_mkdir -p $DIR/$tdir/dir
4245         cd $DIR/$tdir/dir
4246         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4247         $TRACE touch foo && error "touch foo worked after removing cwd"
4248         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4249         touch .foo && error "touch .foo worked after removing cwd"
4250         mkdir .foo && error "mkdir .foo worked after removing cwd"
4251         $TRACE ls . && error "'ls .' worked after removing cwd"
4252         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4253         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4254         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4255         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4256         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4257 }
4258 run_test 48c "Access removed working subdir (should return errors)"
4259
4260 test_48d() { # bug 2350
4261         #lctl set_param debug=-1
4262         #set -vx
4263         rm -rf $DIR/$tdir
4264         test_mkdir -p $DIR/$tdir/dir
4265         cd $DIR/$tdir/dir
4266         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4267         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4268         $TRACE touch foo && error "'touch foo' worked after removing parent"
4269         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4270         touch .foo && error "'touch .foo' worked after removing parent"
4271         mkdir .foo && error "mkdir .foo worked after removing parent"
4272         $TRACE ls . && error "'ls .' worked after removing parent"
4273         $TRACE ls .. && error "'ls ..' worked after removing parent"
4274         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4275         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4276         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4277         true
4278 }
4279 run_test 48d "Access removed parent subdir (should return errors)"
4280
4281 test_48e() { # bug 4134
4282         #lctl set_param debug=-1
4283         #set -vx
4284         rm -rf $DIR/$tdir
4285         test_mkdir -p $DIR/$tdir/dir
4286         cd $DIR/$tdir/dir
4287         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4288         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4289         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4290         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4291         # On a buggy kernel addition of "touch foo" after cd .. will
4292         # produce kernel oops in lookup_hash_it
4293         touch ../foo && error "'cd ..' worked after recreate parent"
4294         cd $DIR
4295         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4296 }
4297 run_test 48e "Access to recreated parent subdir (should return errors)"
4298
4299 test_49() { # LU-1030
4300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4301         remote_ost_nodsh && skip "remote OST with nodsh"
4302
4303         # get ost1 size - lustre-OST0000
4304         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4305                 awk '{ print $4 }')
4306         # write 800M at maximum
4307         [[ $ost1_size -lt 2 ]] && ost1_size=2
4308         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4309
4310         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4311         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4312         local dd_pid=$!
4313
4314         # change max_pages_per_rpc while writing the file
4315         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4316         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4317         # loop until dd process exits
4318         while ps ax -opid | grep -wq $dd_pid; do
4319                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4320                 sleep $((RANDOM % 5 + 1))
4321         done
4322         # restore original max_pages_per_rpc
4323         $LCTL set_param $osc1_mppc=$orig_mppc
4324         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4325 }
4326 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4327
4328 test_50() {
4329         # bug 1485
4330         test_mkdir $DIR/$tdir
4331         cd $DIR/$tdir
4332         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4333 }
4334 run_test 50 "special situations: /proc symlinks  ==============="
4335
4336 test_51a() {    # was test_51
4337         # bug 1516 - create an empty entry right after ".." then split dir
4338         test_mkdir -c1 $DIR/$tdir
4339         touch $DIR/$tdir/foo
4340         $MCREATE $DIR/$tdir/bar
4341         rm $DIR/$tdir/foo
4342         createmany -m $DIR/$tdir/longfile 201
4343         FNUM=202
4344         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4345                 $MCREATE $DIR/$tdir/longfile$FNUM
4346                 FNUM=$(($FNUM + 1))
4347                 echo -n "+"
4348         done
4349         echo
4350         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4351 }
4352 run_test 51a "special situations: split htree with empty entry =="
4353
4354 cleanup_print_lfs_df () {
4355         trap 0
4356         $LFS df
4357         $LFS df -i
4358 }
4359
4360 test_51b() {
4361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4362
4363         local dir=$DIR/$tdir
4364         local nrdirs=$((65536 + 100))
4365
4366         # cleanup the directory
4367         rm -fr $dir
4368
4369         test_mkdir -c1 $dir
4370
4371         $LFS df
4372         $LFS df -i
4373         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4374         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4375         [[ $numfree -lt $nrdirs ]] &&
4376                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4377
4378         # need to check free space for the directories as well
4379         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4380         numfree=$(( blkfree / $(fs_inode_ksize) ))
4381         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4382
4383         trap cleanup_print_lfs_df EXIT
4384
4385         # create files
4386         createmany -d $dir/d $nrdirs || {
4387                 unlinkmany $dir/d $nrdirs
4388                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4389         }
4390
4391         # really created :
4392         nrdirs=$(ls -U $dir | wc -l)
4393
4394         # unlink all but 100 subdirectories, then check it still works
4395         local left=100
4396         local delete=$((nrdirs - left))
4397
4398         $LFS df
4399         $LFS df -i
4400
4401         # for ldiskfs the nlink count should be 1, but this is OSD specific
4402         # and so this is listed for informational purposes only
4403         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4404         unlinkmany -d $dir/d $delete ||
4405                 error "unlink of first $delete subdirs failed"
4406
4407         echo "nlink between: $(stat -c %h $dir)"
4408         local found=$(ls -U $dir | wc -l)
4409         [ $found -ne $left ] &&
4410                 error "can't find subdirs: found only $found, expected $left"
4411
4412         unlinkmany -d $dir/d $delete $left ||
4413                 error "unlink of second $left subdirs failed"
4414         # regardless of whether the backing filesystem tracks nlink accurately
4415         # or not, the nlink count shouldn't be more than "." and ".." here
4416         local after=$(stat -c %h $dir)
4417         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4418                 echo "nlink after: $after"
4419
4420         cleanup_print_lfs_df
4421 }
4422 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4423
4424 test_51d() {
4425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4426         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4427
4428         test_mkdir $DIR/$tdir
4429         createmany -o $DIR/$tdir/t- 1000
4430         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4431         for N in $(seq 0 $((OSTCOUNT - 1))); do
4432                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4433                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4434                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4435                         '($1 == '$N') { objs += 1 } \
4436                         END { printf("%0.0f", objs) }')
4437                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4438         done
4439         unlinkmany $DIR/$tdir/t- 1000
4440
4441         NLAST=0
4442         for N in $(seq 1 $((OSTCOUNT - 1))); do
4443                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4444                         error "OST $N has less objects vs OST $NLAST" \
4445                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4446                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4447                         error "OST $N has less objects vs OST $NLAST" \
4448                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4449
4450                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4451                         error "OST $N has less #0 objects vs OST $NLAST" \
4452                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4453                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4454                         error "OST $N has less #0 objects vs OST $NLAST" \
4455                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4456                 NLAST=$N
4457         done
4458         rm -f $TMP/$tfile
4459 }
4460 run_test 51d "check object distribution"
4461
4462 test_51e() {
4463         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4464                 skip_env "ldiskfs only test"
4465         fi
4466
4467         test_mkdir -c1 $DIR/$tdir
4468         test_mkdir -c1 $DIR/$tdir/d0
4469
4470         touch $DIR/$tdir/d0/foo
4471         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4472                 error "file exceed 65000 nlink limit!"
4473         unlinkmany $DIR/$tdir/d0/f- 65001
4474         return 0
4475 }
4476 run_test 51e "check file nlink limit"
4477
4478 test_51f() {
4479         test_mkdir $DIR/$tdir
4480
4481         local max=100000
4482         local ulimit_old=$(ulimit -n)
4483         local spare=20 # number of spare fd's for scripts/libraries, etc.
4484         local mdt=$($LFS getstripe -m $DIR/$tdir)
4485         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4486
4487         echo "MDT$mdt numfree=$numfree, max=$max"
4488         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4489         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4490                 while ! ulimit -n $((numfree + spare)); do
4491                         numfree=$((numfree * 3 / 4))
4492                 done
4493                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4494         else
4495                 echo "left ulimit at $ulimit_old"
4496         fi
4497
4498         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4499                 unlinkmany $DIR/$tdir/f $numfree
4500                 error "create+open $numfree files in $DIR/$tdir failed"
4501         }
4502         ulimit -n $ulimit_old
4503
4504         # if createmany exits at 120s there will be fewer than $numfree files
4505         unlinkmany $DIR/$tdir/f $numfree || true
4506 }
4507 run_test 51f "check many open files limit"
4508
4509 test_52a() {
4510         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4511         test_mkdir $DIR/$tdir
4512         touch $DIR/$tdir/foo
4513         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4514         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4515         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4516         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4517         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4518                                         error "link worked"
4519         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4520         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4521         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4522                                                      error "lsattr"
4523         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4524         cp -r $DIR/$tdir $TMP/
4525         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4526 }
4527 run_test 52a "append-only flag test (should return errors)"
4528
4529 test_52b() {
4530         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4531         test_mkdir $DIR/$tdir
4532         touch $DIR/$tdir/foo
4533         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4534         cat test > $DIR/$tdir/foo && error "cat test worked"
4535         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4536         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4537         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4538                                         error "link worked"
4539         echo foo >> $DIR/$tdir/foo && error "echo worked"
4540         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4541         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4542         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4543         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4544                                                         error "lsattr"
4545         chattr -i $DIR/$tdir/foo || error "chattr failed"
4546
4547         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4548 }
4549 run_test 52b "immutable flag test (should return errors) ======="
4550
4551 test_53() {
4552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4553         remote_mds_nodsh && skip "remote MDS with nodsh"
4554         remote_ost_nodsh && skip "remote OST with nodsh"
4555
4556         local param
4557         local param_seq
4558         local ostname
4559         local mds_last
4560         local mds_last_seq
4561         local ost_last
4562         local ost_last_seq
4563         local ost_last_id
4564         local ostnum
4565         local node
4566         local found=false
4567         local support_last_seq=true
4568
4569         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4570                 support_last_seq=false
4571
4572         # only test MDT0000
4573         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4574         local value
4575         for value in $(do_facet $SINGLEMDS \
4576                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4577                 param=$(echo ${value[0]} | cut -d "=" -f1)
4578                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4579
4580                 if $support_last_seq; then
4581                         param_seq=$(echo $param |
4582                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4583                         mds_last_seq=$(do_facet $SINGLEMDS \
4584                                        $LCTL get_param -n $param_seq)
4585                 fi
4586                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4587
4588                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4589                 node=$(facet_active_host ost$((ostnum+1)))
4590                 param="obdfilter.$ostname.last_id"
4591                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4592                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4593                         ost_last_id=$ost_last
4594
4595                         if $support_last_seq; then
4596                                 ost_last_id=$(echo $ost_last |
4597                                               awk -F':' '{print $2}' |
4598                                               sed -e "s/^0x//g")
4599                                 ost_last_seq=$(echo $ost_last |
4600                                                awk -F':' '{print $1}')
4601                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4602                         fi
4603
4604                         if [[ $ost_last_id != $mds_last ]]; then
4605                                 error "$ost_last_id != $mds_last"
4606                         else
4607                                 found=true
4608                                 break
4609                         fi
4610                 done
4611         done
4612         $found || error "can not match last_seq/last_id for $mdtosc"
4613         return 0
4614 }
4615 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4616
4617 test_54a() {
4618         perl -MSocket -e ';' || skip "no Socket perl module installed"
4619
4620         $SOCKETSERVER $DIR/socket ||
4621                 error "$SOCKETSERVER $DIR/socket failed: $?"
4622         $SOCKETCLIENT $DIR/socket ||
4623                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4624         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4625 }
4626 run_test 54a "unix domain socket test =========================="
4627
4628 test_54b() {
4629         f="$DIR/f54b"
4630         mknod $f c 1 3
4631         chmod 0666 $f
4632         dd if=/dev/zero of=$f bs=$(page_size) count=1
4633 }
4634 run_test 54b "char device works in lustre ======================"
4635
4636 find_loop_dev() {
4637         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4638         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4639         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4640
4641         for i in $(seq 3 7); do
4642                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4643                 LOOPDEV=$LOOPBASE$i
4644                 LOOPNUM=$i
4645                 break
4646         done
4647 }
4648
4649 cleanup_54c() {
4650         local rc=0
4651         loopdev="$DIR/loop54c"
4652
4653         trap 0
4654         $UMOUNT $DIR/$tdir || rc=$?
4655         losetup -d $loopdev || true
4656         losetup -d $LOOPDEV || true
4657         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4658         return $rc
4659 }
4660
4661 test_54c() {
4662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4663
4664         loopdev="$DIR/loop54c"
4665
4666         find_loop_dev
4667         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4668         trap cleanup_54c EXIT
4669         mknod $loopdev b 7 $LOOPNUM
4670         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4671         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4672         losetup $loopdev $DIR/$tfile ||
4673                 error "can't set up $loopdev for $DIR/$tfile"
4674         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4675         test_mkdir $DIR/$tdir
4676         mount -t ext2 $loopdev $DIR/$tdir ||
4677                 error "error mounting $loopdev on $DIR/$tdir"
4678         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4679                 error "dd write"
4680         df $DIR/$tdir
4681         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4682                 error "dd read"
4683         cleanup_54c
4684 }
4685 run_test 54c "block device works in lustre ====================="
4686
4687 test_54d() {
4688         f="$DIR/f54d"
4689         string="aaaaaa"
4690         mknod $f p
4691         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4692 }
4693 run_test 54d "fifo device works in lustre ======================"
4694
4695 test_54e() {
4696         f="$DIR/f54e"
4697         string="aaaaaa"
4698         cp -aL /dev/console $f
4699         echo $string > $f || error "echo $string to $f failed"
4700 }
4701 run_test 54e "console/tty device works in lustre ======================"
4702
4703 test_56a() {
4704         local numfiles=3
4705         local dir=$DIR/$tdir
4706
4707         rm -rf $dir
4708         test_mkdir -p $dir/dir
4709         for i in $(seq $numfiles); do
4710                 touch $dir/file$i
4711                 touch $dir/dir/file$i
4712         done
4713
4714         local numcomp=$($LFS getstripe --component-count $dir)
4715
4716         [[ $numcomp == 0 ]] && numcomp=1
4717
4718         # test lfs getstripe with --recursive
4719         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4720
4721         [[ $filenum -eq $((numfiles * 2)) ]] ||
4722                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4723         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4724         [[ $filenum -eq $numfiles ]] ||
4725                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4726         echo "$LFS getstripe showed obdidx or l_ost_idx"
4727
4728         # test lfs getstripe with file instead of dir
4729         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4730         [[ $filenum -eq 1 ]] ||
4731                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
4732         echo "$LFS getstripe file1 passed"
4733
4734         #test lfs getstripe with --verbose
4735         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
4736         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4737                 error "$LFS getstripe --verbose $dir: "\
4738                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
4739         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
4740                 error "$LFS getstripe $dir: showed lmm_magic"
4741
4742         #test lfs getstripe with -v prints lmm_fid
4743         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
4744         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4745                 error "$LFS getstripe -v $dir: "\
4746                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
4747         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
4748                 error "$LFS getstripe $dir: showed lmm_fid by default"
4749         echo "$LFS getstripe --verbose passed"
4750
4751         #check for FID information
4752         local fid1=$($LFS getstripe --fid $dir/file1)
4753         local fid2=$($LFS getstripe --verbose $dir/file1 |
4754                      awk '/lmm_fid: / { print $2; exit; }')
4755         local fid3=$($LFS path2fid $dir/file1)
4756
4757         [ "$fid1" != "$fid2" ] &&
4758                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
4759         [ "$fid1" != "$fid3" ] &&
4760                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
4761         echo "$LFS getstripe --fid passed"
4762
4763         #test lfs getstripe with --obd
4764         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
4765                 error "$LFS getstripe --obd wrong_uuid: should return error"
4766
4767         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4768
4769         local ostidx=1
4770         local obduuid=$(ostuuid_from_index $ostidx)
4771         local found=$($LFS getstripe -r --obd $obduuid $dir |
4772                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
4773
4774         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
4775         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
4776                 ((filenum--))
4777         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
4778                 ((filenum--))
4779
4780         [[ $found -eq $filenum ]] ||
4781                 error "$LFS getstripe --obd: found $found expect $filenum"
4782         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
4783                 sed '/^[         ]*'${ostidx}'[  ]/d' |
4784                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4785                 error "$LFS getstripe --obd: should not show file on other obd"
4786         echo "$LFS getstripe --obd passed"
4787 }
4788 run_test 56a "check $LFS getstripe"
4789
4790 test_56b() {
4791         local dir=$DIR/$tdir
4792         local numdirs=3
4793
4794         test_mkdir $dir
4795         for i in $(seq $numdirs); do
4796                 test_mkdir $dir/dir$i
4797         done
4798
4799         # test lfs getdirstripe default mode is non-recursion, which is
4800         # different from lfs getstripe
4801         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
4802
4803         [[ $dircnt -eq 1 ]] ||
4804                 error "$LFS getdirstripe: found $dircnt, not 1"
4805         dircnt=$($LFS getdirstripe --recursive $dir |
4806                 grep -c lmv_stripe_count)
4807         [[ $dircnt -eq $((numdirs + 1)) ]] ||
4808                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
4809 }
4810 run_test 56b "check $LFS getdirstripe"
4811
4812 test_56c() {
4813         remote_ost_nodsh && skip "remote OST with nodsh"
4814
4815         local ost_idx=0
4816         local ost_name=$(ostname_from_index $ost_idx)
4817         local old_status=$(ost_dev_status $ost_idx)
4818
4819         [[ -z "$old_status" ]] ||
4820                 skip_env "OST $ost_name is in $old_status status"
4821
4822         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4823         sleep_maxage
4824
4825         local new_status=$(ost_dev_status $ost_idx)
4826
4827         [[ "$new_status" = "D" ]] ||
4828                 error "OST $ost_name is in status of '$new_status', not 'D'"
4829
4830         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4831         sleep_maxage
4832
4833         new_status=$(ost_dev_status $ost_idx)
4834         [[ -z "$new_status" ]] ||
4835                 error "OST $ost_name is in status of '$new_status', not ''"
4836 }
4837 run_test 56c "check 'lfs df' showing device status"
4838
4839 NUMFILES=3
4840 NUMDIRS=3
4841 setup_56() {
4842         local local_tdir="$1"
4843         local local_numfiles="$2"
4844         local local_numdirs="$3"
4845         local dir_params="$4"
4846         local dir_stripe_params="$5"
4847
4848         if [ ! -d "$local_tdir" ] ; then
4849                 test_mkdir -p $dir_stripe_params $local_tdir
4850                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
4851                 for i in $(seq $local_numfiles) ; do
4852                         touch $local_tdir/file$i
4853                 done
4854                 for i in $(seq $local_numdirs) ; do
4855                         test_mkdir $dir_stripe_params $local_tdir/dir$i
4856                         for j in $(seq $local_numfiles) ; do
4857                                 touch $local_tdir/dir$i/file$j
4858                         done
4859                 done
4860         fi
4861 }
4862
4863 setup_56_special() {
4864         local local_tdir=$1
4865         local local_numfiles=$2
4866         local local_numdirs=$3
4867
4868         setup_56 $local_tdir $local_numfiles $local_numdirs
4869
4870         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
4871                 for i in $(seq $local_numfiles) ; do
4872                         mknod $local_tdir/loop${i}b b 7 $i
4873                         mknod $local_tdir/null${i}c c 1 3
4874                         ln -s $local_tdir/file1 $local_tdir/link${i}
4875                 done
4876                 for i in $(seq $local_numdirs) ; do
4877                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
4878                         mknod $local_tdir/dir$i/null${i}c c 1 3
4879                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
4880                 done
4881         fi
4882 }
4883
4884 test_56g() {
4885         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4886         local expected=$(($NUMDIRS + 2))
4887
4888         setup_56 $dir $NUMFILES $NUMDIRS
4889
4890         # test lfs find with -name
4891         for i in $(seq $NUMFILES) ; do
4892                 local nums=$($LFS find -name "*$i" $dir | wc -l)
4893
4894                 [ $nums -eq $expected ] ||
4895                         error "lfs find -name '*$i' $dir wrong: "\
4896                               "found $nums, expected $expected"
4897         done
4898 }
4899 run_test 56g "check lfs find -name"
4900
4901 test_56h() {
4902         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4903         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4904
4905         setup_56 $dir $NUMFILES $NUMDIRS
4906
4907         # test lfs find with ! -name
4908         for i in $(seq $NUMFILES) ; do
4909                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
4910
4911                 [ $nums -eq $expected ] ||
4912                         error "lfs find ! -name '*$i' $dir wrong: "\
4913                               "found $nums, expected $expected"
4914         done
4915 }
4916 run_test 56h "check lfs find ! -name"
4917
4918 test_56i() {
4919         local dir=$DIR/$tdir
4920
4921         test_mkdir $dir
4922
4923         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
4924         local out=$($cmd)
4925
4926         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
4927 }
4928 run_test 56i "check 'lfs find -ost UUID' skips directories"
4929
4930 test_56j() {
4931         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4932
4933         setup_56_special $dir $NUMFILES $NUMDIRS
4934
4935         local expected=$((NUMDIRS + 1))
4936         local cmd="$LFS find -type d $dir"
4937         local nums=$($cmd | wc -l)
4938
4939         [ $nums -eq $expected ] ||
4940                 error "'$cmd' wrong: found $nums, expected $expected"
4941 }
4942 run_test 56j "check lfs find -type d"
4943
4944 test_56k() {
4945         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4946
4947         setup_56_special $dir $NUMFILES $NUMDIRS
4948
4949         local expected=$(((NUMDIRS + 1) * NUMFILES))
4950         local cmd="$LFS find -type f $dir"
4951         local nums=$($cmd | wc -l)
4952
4953         [ $nums -eq $expected ] ||
4954                 error "'$cmd' wrong: found $nums, expected $expected"
4955 }
4956 run_test 56k "check lfs find -type f"
4957
4958 test_56l() {
4959         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4960
4961         setup_56_special $dir $NUMFILES $NUMDIRS
4962
4963         local expected=$((NUMDIRS + NUMFILES))
4964         local cmd="$LFS find -type b $dir"
4965         local nums=$($cmd | wc -l)
4966
4967         [ $nums -eq $expected ] ||
4968                 error "'$cmd' wrong: found $nums, expected $expected"
4969 }
4970 run_test 56l "check lfs find -type b"
4971
4972 test_56m() {
4973         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4974
4975         setup_56_special $dir $NUMFILES $NUMDIRS
4976
4977         local expected=$((NUMDIRS + NUMFILES))
4978         local cmd="$LFS find -type c $dir"
4979         local nums=$($cmd | wc -l)
4980         [ $nums -eq $expected ] ||
4981                 error "'$cmd' wrong: found $nums, expected $expected"
4982 }
4983 run_test 56m "check lfs find -type c"
4984
4985 test_56n() {
4986         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4987         setup_56_special $dir $NUMFILES $NUMDIRS
4988
4989         local expected=$((NUMDIRS + NUMFILES))
4990         local cmd="$LFS find -type l $dir"
4991         local nums=$($cmd | wc -l)
4992
4993         [ $nums -eq $expected ] ||
4994                 error "'$cmd' wrong: found $nums, expected $expected"
4995 }
4996 run_test 56n "check lfs find -type l"
4997
4998 test_56o() {
4999         local dir=$DIR/$tdir
5000
5001         setup_56 $dir $NUMFILES $NUMDIRS
5002         utime $dir/file1 > /dev/null || error "utime (1)"
5003         utime $dir/file2 > /dev/null || error "utime (2)"
5004         utime $dir/dir1 > /dev/null || error "utime (3)"
5005         utime $dir/dir2 > /dev/null || error "utime (4)"
5006         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5007         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5008
5009         local expected=4
5010         local nums=$($LFS find -mtime +0 $dir | wc -l)
5011
5012         [ $nums -eq $expected ] ||
5013                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5014
5015         expected=12
5016         cmd="$LFS find -mtime 0 $dir"
5017         nums=$($cmd | wc -l)
5018         [ $nums -eq $expected ] ||
5019                 error "'$cmd' wrong: found $nums, expected $expected"
5020 }
5021 run_test 56o "check lfs find -mtime for old files"
5022
5023 test_56p() {
5024         [ $RUNAS_ID -eq $UID ] &&
5025                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5026
5027         local dir=$DIR/$tdir
5028
5029         setup_56 $dir $NUMFILES $NUMDIRS
5030         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5031
5032         local expected=$NUMFILES
5033         local cmd="$LFS find -uid $RUNAS_ID $dir"
5034         local nums=$($cmd | wc -l)
5035
5036         [ $nums -eq $expected ] ||
5037                 error "'$cmd' wrong: found $nums, expected $expected"
5038
5039         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5040         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5041         nums=$($cmd | wc -l)
5042         [ $nums -eq $expected ] ||
5043                 error "'$cmd' wrong: found $nums, expected $expected"
5044 }
5045 run_test 56p "check lfs find -uid and ! -uid"
5046
5047 test_56q() {
5048         [ $RUNAS_ID -eq $UID ] &&
5049                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5050
5051         local dir=$DIR/$tdir
5052
5053         setup_56 $dir $NUMFILES $NUMDIRS
5054         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5055
5056         local expected=$NUMFILES
5057         local cmd="$LFS find -gid $RUNAS_GID $dir"
5058         local nums=$($cmd | wc -l)
5059
5060         [ $nums -eq $expected ] ||
5061                 error "'$cmd' wrong: found $nums, expected $expected"
5062
5063         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5064         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5065         nums=$($cmd | wc -l)
5066         [ $nums -eq $expected ] ||
5067                 error "'$cmd' wrong: found $nums, expected $expected"
5068 }
5069 run_test 56q "check lfs find -gid and ! -gid"
5070
5071 test_56r() {
5072         local dir=$DIR/$tdir
5073
5074         setup_56 $dir $NUMFILES $NUMDIRS
5075
5076         local expected=12
5077         local cmd="$LFS find -size 0 -type f $dir"
5078         local nums=$($cmd | wc -l)
5079
5080         [ $nums -eq $expected ] ||
5081                 error "'$cmd' wrong: found $nums, expected $expected"
5082         expected=0
5083         cmd="$LFS find ! -size 0 -type f $dir"
5084         nums=$($cmd | wc -l)
5085         [ $nums -eq $expected ] ||
5086                 error "'$cmd' wrong: found $nums, expected $expected"
5087         echo "test" > $dir/$tfile
5088         echo "test2" > $dir/$tfile.2 && sync
5089         expected=1
5090         cmd="$LFS find -size 5 -type f $dir"
5091         nums=$($cmd | wc -l)
5092         [ $nums -eq $expected ] ||
5093                 error "'$cmd' wrong: found $nums, expected $expected"
5094         expected=1
5095         cmd="$LFS find -size +5 -type f $dir"
5096         nums=$($cmd | wc -l)
5097         [ $nums -eq $expected ] ||
5098                 error "'$cmd' wrong: found $nums, expected $expected"
5099         expected=2
5100         cmd="$LFS find -size +0 -type f $dir"
5101         nums=$($cmd | wc -l)
5102         [ $nums -eq $expected ] ||
5103                 error "'$cmd' wrong: found $nums, expected $expected"
5104         expected=2
5105         cmd="$LFS find ! -size -5 -type f $dir"
5106         nums=$($cmd | wc -l)
5107         [ $nums -eq $expected ] ||
5108                 error "'$cmd' wrong: found $nums, expected $expected"
5109         expected=12
5110         cmd="$LFS find -size -5 -type f $dir"
5111         nums=$($cmd | wc -l)
5112         [ $nums -eq $expected ] ||
5113                 error "'$cmd' wrong: found $nums, expected $expected"
5114 }
5115 run_test 56r "check lfs find -size works"
5116
5117 test_56s() { # LU-611 #LU-9369
5118         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5119
5120         local dir=$DIR/$tdir
5121         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5122
5123         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5124         for i in $(seq $NUMDIRS); do
5125                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5126         done
5127
5128         local expected=$NUMDIRS
5129         local cmd="$LFS find -c $OSTCOUNT $dir"
5130         local nums=$($cmd | wc -l)
5131
5132         [ $nums -eq $expected ] || {
5133                 $LFS getstripe -R $dir
5134                 error "'$cmd' wrong: found $nums, expected $expected"
5135         }
5136
5137         expected=$((NUMDIRS + onestripe))
5138         cmd="$LFS find -stripe-count +0 -type f $dir"
5139         nums=$($cmd | wc -l)
5140         [ $nums -eq $expected ] || {
5141                 $LFS getstripe -R $dir
5142                 error "'$cmd' wrong: found $nums, expected $expected"
5143         }
5144
5145         expected=$onestripe
5146         cmd="$LFS find -stripe-count 1 -type f $dir"
5147         nums=$($cmd | wc -l)
5148         [ $nums -eq $expected ] || {
5149                 $LFS getstripe -R $dir
5150                 error "'$cmd' wrong: found $nums, expected $expected"
5151         }
5152
5153         cmd="$LFS find -stripe-count -2 -type f $dir"
5154         nums=$($cmd | wc -l)
5155         [ $nums -eq $expected ] || {
5156                 $LFS getstripe -R $dir
5157                 error "'$cmd' wrong: found $nums, expected $expected"
5158         }
5159
5160         expected=0
5161         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5162         nums=$($cmd | wc -l)
5163         [ $nums -eq $expected ] || {
5164                 $LFS getstripe -R $dir
5165                 error "'$cmd' wrong: found $nums, expected $expected"
5166         }
5167 }
5168 run_test 56s "check lfs find -stripe-count works"
5169
5170 test_56t() { # LU-611 #LU-9369
5171         local dir=$DIR/$tdir
5172
5173         setup_56 $dir 0 $NUMDIRS
5174         for i in $(seq $NUMDIRS); do
5175                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5176         done
5177
5178         local expected=$NUMDIRS
5179         local cmd="$LFS find -S 8M $dir"
5180         local nums=$($cmd | wc -l)
5181
5182         [ $nums -eq $expected ] || {
5183                 $LFS getstripe -R $dir
5184                 error "'$cmd' wrong: found $nums, expected $expected"
5185         }
5186         rm -rf $dir
5187
5188         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5189
5190         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5191
5192         expected=$(((NUMDIRS + 1) * NUMFILES))
5193         cmd="$LFS find -stripe-size 512k -type f $dir"
5194         nums=$($cmd | wc -l)
5195         [ $nums -eq $expected ] ||
5196                 error "'$cmd' wrong: found $nums, expected $expected"
5197
5198         cmd="$LFS find -stripe-size +320k -type f $dir"
5199         nums=$($cmd | wc -l)
5200         [ $nums -eq $expected ] ||
5201                 error "'$cmd' wrong: found $nums, expected $expected"
5202
5203         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5204         cmd="$LFS find -stripe-size +200k -type f $dir"
5205         nums=$($cmd | wc -l)
5206         [ $nums -eq $expected ] ||
5207                 error "'$cmd' wrong: found $nums, expected $expected"
5208
5209         cmd="$LFS find -stripe-size -640k -type f $dir"
5210         nums=$($cmd | wc -l)
5211         [ $nums -eq $expected ] ||
5212                 error "'$cmd' wrong: found $nums, expected $expected"
5213
5214         expected=4
5215         cmd="$LFS find -stripe-size 256k -type f $dir"
5216         nums=$($cmd | wc -l)
5217         [ $nums -eq $expected ] ||
5218                 error "'$cmd' wrong: found $nums, expected $expected"
5219
5220         cmd="$LFS find -stripe-size -320k -type f $dir"
5221         nums=$($cmd | wc -l)
5222         [ $nums -eq $expected ] ||
5223                 error "'$cmd' wrong: found $nums, expected $expected"
5224
5225         expected=0
5226         cmd="$LFS find -stripe-size 1024k -type f $dir"
5227         nums=$($cmd | wc -l)
5228         [ $nums -eq $expected ] ||
5229                 error "'$cmd' wrong: found $nums, expected $expected"
5230 }
5231 run_test 56t "check lfs find -stripe-size works"
5232
5233 test_56u() { # LU-611
5234         local dir=$DIR/$tdir
5235
5236         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5237
5238         if [[ $OSTCOUNT -gt 1 ]]; then
5239                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5240                 onestripe=4
5241         else
5242                 onestripe=0
5243         fi
5244
5245         local expected=$(((NUMDIRS + 1) * NUMFILES))
5246         local cmd="$LFS find -stripe-index 0 -type f $dir"
5247         local nums=$($cmd | wc -l)
5248
5249         [ $nums -eq $expected ] ||
5250                 error "'$cmd' wrong: found $nums, expected $expected"
5251
5252         expected=$onestripe
5253         cmd="$LFS find -stripe-index 1 -type f $dir"
5254         nums=$($cmd | wc -l)
5255         [ $nums -eq $expected ] ||
5256                 error "'$cmd' wrong: found $nums, expected $expected"
5257
5258         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5259         nums=$($cmd | wc -l)
5260         [ $nums -eq $expected ] ||
5261                 error "'$cmd' wrong: found $nums, expected $expected"
5262
5263         expected=0
5264         # This should produce an error and not return any files
5265         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5266         nums=$($cmd 2>/dev/null | wc -l)
5267         [ $nums -eq $expected ] ||
5268                 error "'$cmd' wrong: found $nums, expected $expected"
5269
5270         if [[ $OSTCOUNT -gt 1 ]]; then
5271                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5272                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5273                 nums=$($cmd | wc -l)
5274                 [ $nums -eq $expected ] ||
5275                         error "'$cmd' wrong: found $nums, expected $expected"
5276         fi
5277 }
5278 run_test 56u "check lfs find -stripe-index works"
5279
5280 test_56v() {
5281         local mdt_idx=0
5282         local dir=$DIR/$tdir
5283
5284         setup_56 $dir $NUMFILES $NUMDIRS
5285
5286         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5287         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5288
5289         for file in $($LFS find -m $UUID $dir); do
5290                 file_midx=$($LFS getstripe -m $file)
5291                 [ $file_midx -eq $mdt_idx ] ||
5292                         error "lfs find -m $UUID != getstripe -m $file_midx"
5293         done
5294 }
5295 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5296
5297 test_56w() {
5298         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5299         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5300
5301         local dir=$DIR/$tdir
5302
5303         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5304
5305         local stripe_size=$($LFS getstripe -S -d $dir) ||
5306                 error "$LFS getstripe -S -d $dir failed"
5307         stripe_size=${stripe_size%% *}
5308
5309         local file_size=$((stripe_size * OSTCOUNT))
5310         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5311         local required_space=$((file_num * file_size))
5312         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5313                            head -n1)
5314         [[ $free_space -le $((required_space / 1024)) ]] &&
5315                 skip_env "need $required_space, have $free_space kbytes"
5316
5317         local dd_bs=65536
5318         local dd_count=$((file_size / dd_bs))
5319
5320         # write data into the files
5321         local i
5322         local j
5323         local file
5324
5325         for i in $(seq $NUMFILES); do
5326                 file=$dir/file$i
5327                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5328                         error "write data into $file failed"
5329         done
5330         for i in $(seq $NUMDIRS); do
5331                 for j in $(seq $NUMFILES); do
5332                         file=$dir/dir$i/file$j
5333                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5334                                 error "write data into $file failed"
5335                 done
5336         done
5337
5338         # $LFS_MIGRATE will fail if hard link migration is unsupported
5339         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5340                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5341                         error "creating links to $dir/dir1/file1 failed"
5342         fi
5343
5344         local expected=-1
5345
5346         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5347
5348         # lfs_migrate file
5349         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5350
5351         echo "$cmd"
5352         eval $cmd || error "$cmd failed"
5353
5354         check_stripe_count $dir/file1 $expected
5355
5356         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5357         then
5358                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5359                 # OST 1 if it is on OST 0. This file is small enough to
5360                 # be on only one stripe.
5361                 file=$dir/migr_1_ost
5362                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5363                         error "write data into $file failed"
5364                 local obdidx=$($LFS getstripe -i $file)
5365                 local oldmd5=$(md5sum $file)
5366                 local newobdidx=0
5367
5368                 [[ $obdidx -eq 0 ]] && newobdidx=1
5369                 cmd="$LFS migrate -i $newobdidx $file"
5370                 echo $cmd
5371                 eval $cmd || error "$cmd failed"
5372
5373                 local realobdix=$($LFS getstripe -i $file)
5374                 local newmd5=$(md5sum $file)
5375
5376                 [[ $newobdidx -ne $realobdix ]] &&
5377                         error "new OST is different (was=$obdidx, "\
5378                               "wanted=$newobdidx, got=$realobdix)"
5379                 [[ "$oldmd5" != "$newmd5" ]] &&
5380                         error "md5sum differ: $oldmd5, $newmd5"
5381         fi
5382
5383         # lfs_migrate dir
5384         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5385         echo "$cmd"
5386         eval $cmd || error "$cmd failed"
5387
5388         for j in $(seq $NUMFILES); do
5389                 check_stripe_count $dir/dir1/file$j $expected
5390         done
5391
5392         # lfs_migrate works with lfs find
5393         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5394              $LFS_MIGRATE -y -c $expected"
5395         echo "$cmd"
5396         eval $cmd || error "$cmd failed"
5397
5398         for i in $(seq 2 $NUMFILES); do
5399                 check_stripe_count $dir/file$i $expected
5400         done
5401         for i in $(seq 2 $NUMDIRS); do
5402                 for j in $(seq $NUMFILES); do
5403                 check_stripe_count $dir/dir$i/file$j $expected
5404                 done
5405         done
5406 }
5407 run_test 56w "check lfs_migrate -c stripe_count works"
5408
5409 test_56wb() {
5410         local file1=$DIR/$tdir/file1
5411         local create_pool=false
5412         local initial_pool=$($LFS getstripe -p $DIR)
5413         local pool_list=()
5414         local pool=""
5415
5416         echo -n "Creating test dir..."
5417         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5418         echo "done."
5419
5420         echo -n "Creating test file..."
5421         touch $file1 || error "cannot create file"
5422         echo "done."
5423
5424         echo -n "Detecting existing pools..."
5425         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5426
5427         if [ ${#pool_list[@]} -gt 0 ]; then
5428                 echo "${pool_list[@]}"
5429                 for thispool in "${pool_list[@]}"; do
5430                         if [[ -z "$initial_pool" ||
5431                               "$initial_pool" != "$thispool" ]]; then
5432                                 pool="$thispool"
5433                                 echo "Using existing pool '$pool'"
5434                                 break
5435                         fi
5436                 done
5437         else
5438                 echo "none detected."
5439         fi
5440         if [ -z "$pool" ]; then
5441                 pool=${POOL:-testpool}
5442                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5443                 echo -n "Creating pool '$pool'..."
5444                 create_pool=true
5445                 pool_add $pool &> /dev/null ||
5446                         error "pool_add failed"
5447                 echo "done."
5448
5449                 echo -n "Adding target to pool..."
5450                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5451                         error "pool_add_targets failed"
5452                 echo "done."
5453         fi
5454
5455         echo -n "Setting pool using -p option..."
5456         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5457                 error "migrate failed rc = $?"
5458         echo "done."
5459
5460         echo -n "Verifying test file is in pool after migrating..."
5461         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5462                 error "file was not migrated to pool $pool"
5463         echo "done."
5464
5465         echo -n "Removing test file from pool '$pool'..."
5466         $LFS migrate $file1 &> /dev/null ||
5467                 error "cannot remove from pool"
5468         [ "$($LFS getstripe -p $file1)" ] &&
5469                 error "pool still set"
5470         echo "done."
5471
5472         echo -n "Setting pool using --pool option..."
5473         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5474                 error "migrate failed rc = $?"
5475         echo "done."
5476
5477         # Clean up
5478         rm -f $file1
5479         if $create_pool; then
5480                 destroy_test_pools 2> /dev/null ||
5481                         error "destroy test pools failed"
5482         fi
5483 }
5484 run_test 56wb "check lfs_migrate pool support"
5485
5486 test_56wc() {
5487         local file1="$DIR/$tdir/file 1"
5488
5489         echo -n "Creating test dir..."
5490         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5491         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5492         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5493                 error "cannot set stripe"
5494         echo "done"
5495
5496         echo -n "Setting initial stripe for test file..."
5497         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5498                 error "cannot set stripe"
5499         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5500                 error "stripe size not set"
5501         echo "done."
5502
5503         # File currently set to -S 512K -c 1
5504
5505         # Ensure -c and -S options are rejected when -R is set
5506         echo -n "Verifying incompatible options are detected..."
5507         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5508                 error "incompatible -c and -R options not detected"
5509         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5510                 error "incompatible -S and -R options not detected"
5511         echo "done."
5512
5513         # Ensure unrecognized options are passed through to 'lfs migrate'
5514         echo -n "Verifying -S option is passed through to lfs migrate..."
5515         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5516                 error "migration failed"
5517         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5518                 error "file was not restriped"
5519         echo "done."
5520
5521         # File currently set to -S 1M -c 1
5522
5523         # Ensure long options are supported
5524         echo -n "Verifying long options supported..."
5525         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5526                 error "long option without argument not supported"
5527         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5528                 error "long option with argument not supported"
5529         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5530                 error "file not restriped with --stripe-size option"
5531         echo "done."
5532
5533         # File currently set to -S 512K -c 1
5534
5535         if [ "$OSTCOUNT" -gt 1 ]; then
5536                 echo -n "Verifying explicit stripe count can be set..."
5537                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5538                         error "migrate failed"
5539                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5540                         error "file not restriped to explicit count"
5541                 echo "done."
5542         fi
5543
5544         # File currently set to -S 512K -c 1 or -S 512K -c 2
5545
5546         # Ensure parent striping is used if -R is set, and no stripe
5547         # count or size is specified
5548         echo -n "Setting stripe for parent directory..."
5549         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5550                 error "cannot set stripe"
5551         echo "done."
5552
5553         echo -n "Verifying restripe option uses parent stripe settings..."
5554         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5555                 error "migrate failed"
5556         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
5557                 error "file not restriped to parent settings"
5558         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5559                 error "file not restriped to parent settings"
5560         echo "done."
5561
5562         # File currently set to -S 1M -c 1
5563
5564         # Ensure striping is preserved if -R is not set, and no stripe
5565         # count or size is specified
5566         echo -n "Verifying striping size preserved when not specified..."
5567         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
5568         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5569                 error "cannot set stripe on parent directory"
5570         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5571                 error "migrate failed"
5572         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
5573                 error "file was restriped"
5574         echo "done."
5575
5576         # Ensure file name properly detected when final option has no argument
5577         echo -n "Verifying file name properly detected..."
5578         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5579                 error "file name interpreted as option argument"
5580         echo "done."
5581
5582         # Clean up
5583         rm -f "$file1"
5584 }
5585 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5586
5587 test_56wd() {
5588         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5589
5590         local file1=$DIR/$tdir/file1
5591
5592         echo -n "Creating test dir..."
5593         test_mkdir $DIR/$tdir || error "cannot create dir"
5594         echo "done."
5595
5596         echo -n "Creating test file..."
5597         touch $file1
5598         echo "done."
5599
5600         # Ensure 'lfs migrate' will fail by using a non-existent option,
5601         # and make sure rsync is not called to recover
5602         echo -n "Make sure --no-rsync option works..."
5603         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5604                 grep -q 'refusing to fall back to rsync' ||
5605                 error "rsync was called with --no-rsync set"
5606         echo "done."
5607
5608         # Ensure rsync is called without trying 'lfs migrate' first
5609         echo -n "Make sure --rsync option works..."
5610         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5611                 grep -q 'falling back to rsync' &&
5612                 error "lfs migrate was called with --rsync set"
5613         echo "done."
5614
5615         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5616         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5617                 grep -q 'at the same time' ||
5618                 error "--rsync and --no-rsync accepted concurrently"
5619         echo "done."
5620
5621         # Clean up
5622         rm -f $file1
5623 }
5624 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5625
5626 test_56x() {
5627         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5628         check_swap_layouts_support
5629
5630         local dir=$DIR/$tdir
5631         local ref1=/etc/passwd
5632         local file1=$dir/file1
5633
5634         test_mkdir $dir || error "creating dir $dir"
5635         $LFS setstripe -c 2 $file1
5636         cp $ref1 $file1
5637         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5638         stripe=$($LFS getstripe -c $file1)
5639         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5640         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5641
5642         # clean up
5643         rm -f $file1
5644 }
5645 run_test 56x "lfs migration support"
5646
5647 test_56xa() {
5648         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5649         check_swap_layouts_support
5650
5651         local dir=$DIR/$tdir/$testnum
5652
5653         test_mkdir -p $dir
5654
5655         local ref1=/etc/passwd
5656         local file1=$dir/file1
5657
5658         $LFS setstripe -c 2 $file1
5659         cp $ref1 $file1
5660         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5661
5662         local stripe=$($LFS getstripe -c $file1)
5663
5664         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5665         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5666
5667         # clean up
5668         rm -f $file1
5669 }
5670 run_test 56xa "lfs migration --block support"
5671
5672 check_migrate_links() {
5673         local dir="$1"
5674         local file1="$dir/file1"
5675         local begin="$2"
5676         local count="$3"
5677         local total_count=$(($begin + $count - 1))
5678         local symlink_count=10
5679         local uniq_count=10
5680
5681         if [ ! -f "$file1" ]; then
5682                 echo -n "creating initial file..."
5683                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5684                         error "cannot setstripe initial file"
5685                 echo "done"
5686
5687                 echo -n "creating symlinks..."
5688                 for s in $(seq 1 $symlink_count); do
5689                         ln -s "$file1" "$dir/slink$s" ||
5690                                 error "cannot create symlinks"
5691                 done
5692                 echo "done"
5693
5694                 echo -n "creating nonlinked files..."
5695                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5696                         error "cannot create nonlinked files"
5697                 echo "done"
5698         fi
5699
5700         # create hard links
5701         if [ ! -f "$dir/file$total_count" ]; then
5702                 echo -n "creating hard links $begin:$total_count..."
5703                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5704                         /dev/null || error "cannot create hard links"
5705                 echo "done"
5706         fi
5707
5708         echo -n "checking number of hard links listed in xattrs..."
5709         local fid=$($LFS getstripe -F "$file1")
5710         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5711
5712         echo "${#paths[*]}"
5713         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5714                         skip "hard link list has unexpected size, skipping test"
5715         fi
5716         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5717                         error "link names should exceed xattrs size"
5718         fi
5719
5720         echo -n "migrating files..."
5721         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5722         local rc=$?
5723         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5724         echo "done"
5725
5726         # make sure all links have been properly migrated
5727         echo -n "verifying files..."
5728         fid=$($LFS getstripe -F "$file1") ||
5729                 error "cannot get fid for file $file1"
5730         for i in $(seq 2 $total_count); do
5731                 local fid2=$($LFS getstripe -F $dir/file$i)
5732
5733                 [ "$fid2" == "$fid" ] ||
5734                         error "migrated hard link has mismatched FID"
5735         done
5736
5737         # make sure hard links were properly detected, and migration was
5738         # performed only once for the entire link set; nonlinked files should
5739         # also be migrated
5740         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5741         local expected=$(($uniq_count + 1))
5742
5743         [ "$actual" -eq  "$expected" ] ||
5744                 error "hard links individually migrated ($actual != $expected)"
5745
5746         # make sure the correct number of hard links are present
5747         local hardlinks=$(stat -c '%h' "$file1")
5748
5749         [ $hardlinks -eq $total_count ] ||
5750                 error "num hard links $hardlinks != $total_count"
5751         echo "done"
5752
5753         return 0
5754 }
5755
5756 test_56xb() {
5757         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
5758                 skip "Need MDS version at least 2.10.55"
5759
5760         local dir="$DIR/$tdir"
5761
5762         test_mkdir "$dir" || error "cannot create dir $dir"
5763
5764         echo "testing lfs migrate mode when all links fit within xattrs"
5765         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 2 99
5766
5767         echo "testing rsync mode when all links fit within xattrs"
5768         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 2 99
5769
5770         echo "testing lfs migrate mode when all links do not fit within xattrs"
5771         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 101 100
5772
5773         echo "testing rsync mode when all links do not fit within xattrs"
5774         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 101 100
5775
5776         # clean up
5777         rm -rf $dir
5778 }
5779 run_test 56xb "lfs migration hard link support"
5780
5781 test_56y() {
5782         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5783                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
5784
5785         local res=""
5786         local dir=$DIR/$tdir
5787         local f1=$dir/file1
5788         local f2=$dir/file2
5789
5790         test_mkdir -p $dir || error "creating dir $dir"
5791         touch $f1 || error "creating std file $f1"
5792         $MULTIOP $f2 H2c || error "creating released file $f2"
5793
5794         # a directory can be raid0, so ask only for files
5795         res=$($LFS find $dir -L raid0 -type f | wc -l)
5796         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5797
5798         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
5799         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5800
5801         # only files can be released, so no need to force file search
5802         res=$($LFS find $dir -L released)
5803         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5804
5805         res=$($LFS find $dir -type f \! -L released)
5806         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5807 }
5808 run_test 56y "lfs find -L raid0|released"
5809
5810 test_56z() { # LU-4824
5811         # This checks to make sure 'lfs find' continues after errors
5812         # There are two classes of errors that should be caught:
5813         # - If multiple paths are provided, all should be searched even if one
5814         #   errors out
5815         # - If errors are encountered during the search, it should not terminate
5816         #   early
5817         local dir=$DIR/$tdir
5818         local i
5819
5820         test_mkdir $dir
5821         for i in d{0..9}; do
5822                 test_mkdir $dir/$i
5823         done
5824         touch $dir/d{0..9}/$tfile
5825         $LFS find $DIR/non_existent_dir $dir &&
5826                 error "$LFS find did not return an error"
5827         # Make a directory unsearchable. This should NOT be the last entry in
5828         # directory order.  Arbitrarily pick the 6th entry
5829         chmod 700 $($LFS find $dir -type d | sed '6!d')
5830
5831         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
5832
5833         # The user should be able to see 10 directories and 9 files
5834         [ $count == 19 ] || error "$LFS find did not continue after error"
5835 }
5836 run_test 56z "lfs find should continue after an error"
5837
5838 test_56aa() { # LU-5937
5839         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
5840
5841         local dir=$DIR/$tdir
5842
5843         mkdir $dir
5844         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
5845
5846         createmany -o $dir/striped_dir/${tfile}- 1024
5847         local dirs=$($LFS find --size +8k $dir/)
5848
5849         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5850 }
5851 run_test 56aa "lfs find --size under striped dir"
5852
5853 test_56ab() { # LU-10705
5854         test_mkdir $DIR/$tdir
5855         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
5856         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
5857         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
5858         # Flush writes to ensure valid blocks.  Need to be more thorough for
5859         # ZFS, since blocks are not allocated/returned to client immediately.
5860         sync_all_data
5861         wait_zfs_commit ost1 2
5862         cancel_lru_locks osc
5863         ls -ls $DIR/$tdir
5864
5865         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
5866
5867         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
5868
5869         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
5870         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
5871
5872         rm -f $DIR/$tdir/$tfile.[123]
5873 }
5874 run_test 56ab "lfs find --blocks"
5875
5876 test_56ba() {
5877         # Create composite files with one component
5878         local dir=$DIR/$tdir
5879
5880         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
5881         # Create composite files with three components
5882         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
5883         # Create non-composite files
5884         createmany -o $dir/${tfile}- 10
5885
5886         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
5887
5888         [[ $nfiles == 10 ]] ||
5889                 error "lfs find -E 1M found $nfiles != 10 files"
5890
5891         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
5892         [[ $nfiles == 25 ]] ||
5893                 error "lfs find ! -E 1M found $nfiles != 25 files"
5894
5895         # All files have a component that starts at 0
5896         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
5897         [[ $nfiles == 35 ]] ||
5898                 error "lfs find --component-start 0 - $nfiles != 35 files"
5899
5900         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
5901         [[ $nfiles == 15 ]] ||
5902                 error "lfs find --component-start 2M - $nfiles != 15 files"
5903
5904         # All files created here have a componenet that does not starts at 2M
5905         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
5906         [[ $nfiles == 35 ]] ||
5907                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
5908
5909         # Find files with a specified number of components
5910         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
5911         [[ $nfiles == 15 ]] ||
5912                 error "lfs find --component-count 3 - $nfiles != 15 files"
5913
5914         # Remember non-composite files have a component count of zero
5915         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
5916         [[ $nfiles == 10 ]] ||
5917                 error "lfs find --component-count 0 - $nfiles != 10 files"
5918
5919         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
5920         [[ $nfiles == 20 ]] ||
5921                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
5922
5923         # All files have a flag called "init"
5924         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
5925         [[ $nfiles == 35 ]] ||
5926                 error "lfs find --component-flags init - $nfiles != 35 files"
5927
5928         # Multi-component files will have a component not initialized
5929         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
5930         [[ $nfiles == 15 ]] ||
5931                 error "lfs find !--component-flags init - $nfiles != 15 files"
5932
5933         rm -rf $dir
5934
5935 }
5936 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5937
5938 test_56ca() {
5939         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
5940                 skip "Need MDS version at least 2.10.57"
5941
5942         local td=$DIR/$tdir
5943         local tf=$td/$tfile
5944         local dir
5945         local nfiles
5946         local cmd
5947         local i
5948         local j
5949
5950         # create mirrored directories and mirrored files
5951         mkdir $td || error "mkdir $td failed"
5952         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
5953         createmany -o $tf- 10 || error "create $tf- failed"
5954
5955         for i in $(seq 2); do
5956                 dir=$td/dir$i
5957                 mkdir $dir || error "mkdir $dir failed"
5958                 $LFS mirror create -N$((3 + i)) $dir ||
5959                         error "create mirrored dir $dir failed"
5960                 createmany -o $dir/$tfile- 10 ||
5961                         error "create $dir/$tfile- failed"
5962         done
5963
5964         # change the states of some mirrored files
5965         echo foo > $tf-6
5966         for i in $(seq 2); do
5967                 dir=$td/dir$i
5968                 for j in $(seq 4 9); do
5969                         echo foo > $dir/$tfile-$j
5970                 done
5971         done
5972
5973         # find mirrored files with specific mirror count
5974         cmd="$LFS find --mirror-count 3 --type f $td"
5975         nfiles=$($cmd | wc -l)
5976         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
5977
5978         cmd="$LFS find ! --mirror-count 3 --type f $td"
5979         nfiles=$($cmd | wc -l)
5980         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
5981
5982         cmd="$LFS find --mirror-count +2 --type f $td"
5983         nfiles=$($cmd | wc -l)
5984         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
5985
5986         cmd="$LFS find --mirror-count -6 --type f $td"
5987         nfiles=$($cmd | wc -l)
5988         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
5989
5990         # find mirrored files with specific file state
5991         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
5992         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
5993
5994         cmd="$LFS find --mirror-state=ro --type f $td"
5995         nfiles=$($cmd | wc -l)
5996         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
5997
5998         cmd="$LFS find ! --mirror-state=ro --type f $td"
5999         nfiles=$($cmd | wc -l)
6000         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6001
6002         cmd="$LFS find --mirror-state=wp --type f $td"
6003         nfiles=$($cmd | wc -l)
6004         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6005
6006         cmd="$LFS find ! --mirror-state=sp --type f $td"
6007         nfiles=$($cmd | wc -l)
6008         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6009 }
6010 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6011
6012 test_57a() {
6013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6014         # note test will not do anything if MDS is not local
6015         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
6016                 skip_env "ldiskfs only test"
6017         fi
6018         remote_mds_nodsh && skip "remote MDS with nodsh"
6019
6020         local MNTDEV="osd*.*MDT*.mntdev"
6021         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6022         [ -z "$DEV" ] && error "can't access $MNTDEV"
6023         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6024                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6025                         error "can't access $DEV"
6026                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6027                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6028                 rm $TMP/t57a.dump
6029         done
6030 }
6031 run_test 57a "verify MDS filesystem created with large inodes =="
6032
6033 test_57b() {
6034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6035         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
6036                 skip_env "ldiskfs only test"
6037         fi
6038         remote_mds_nodsh && skip "remote MDS with nodsh"
6039
6040         local dir=$DIR/$tdir
6041         local filecount=100
6042         local file1=$dir/f1
6043         local fileN=$dir/f$filecount
6044
6045         rm -rf $dir || error "removing $dir"
6046         test_mkdir -c1 $dir
6047         local mdtidx=$($LFS getstripe -m $dir)
6048         local mdtname=MDT$(printf %04x $mdtidx)
6049         local facet=mds$((mdtidx + 1))
6050
6051         echo "mcreating $filecount files"
6052         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6053
6054         # verify that files do not have EAs yet
6055         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6056                 error "$file1 has an EA"
6057         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6058                 error "$fileN has an EA"
6059
6060         sync
6061         sleep 1
6062         df $dir  #make sure we get new statfs data
6063         local mdsfree=$(do_facet $facet \
6064                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6065         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6066         local file
6067
6068         echo "opening files to create objects/EAs"
6069         for file in $(seq -f $dir/f%g 1 $filecount); do
6070                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6071                         error "opening $file"
6072         done
6073
6074         # verify that files have EAs now
6075         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6076         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6077
6078         sleep 1  #make sure we get new statfs data
6079         df $dir
6080         local mdsfree2=$(do_facet $facet \
6081                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6082         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6083
6084         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6085                 if [ "$mdsfree" != "$mdsfree2" ]; then
6086                         error "MDC before $mdcfree != after $mdcfree2"
6087                 else
6088                         echo "MDC before $mdcfree != after $mdcfree2"
6089                         echo "unable to confirm if MDS has large inodes"
6090                 fi
6091         fi
6092         rm -rf $dir
6093 }
6094 run_test 57b "default LOV EAs are stored inside large inodes ==="
6095
6096 test_58() {
6097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6098         [ -z "$(which wiretest 2>/dev/null)" ] &&
6099                         skip_env "could not find wiretest"
6100
6101         wiretest
6102 }
6103 run_test 58 "verify cross-platform wire constants =============="
6104
6105 test_59() {
6106         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6107
6108         echo "touch 130 files"
6109         createmany -o $DIR/f59- 130
6110         echo "rm 130 files"
6111         unlinkmany $DIR/f59- 130
6112         sync
6113         # wait for commitment of removal
6114         wait_delete_completed
6115 }
6116 run_test 59 "verify cancellation of llog records async ========="
6117
6118 TEST60_HEAD="test_60 run $RANDOM"
6119 test_60a() {
6120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6121         remote_mgs_nodsh && skip "remote MGS with nodsh"
6122         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6123                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6124                         skip_env "missing subtest run-llog.sh"
6125
6126         log "$TEST60_HEAD - from kernel mode"
6127         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
6128         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6129         do_facet mgs $LCTL dk > $TMP/$tfile
6130
6131         # LU-6388: test llog_reader
6132         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6133         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6134         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6135                         skip_env "missing llog_reader"
6136         local fstype=$(facet_fstype mgs)
6137         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6138                 skip_env "Only for ldiskfs or zfs type mgs"
6139
6140         local mntpt=$(facet_mntpt mgs)
6141         local mgsdev=$(mgsdevname 1)
6142         local fid_list
6143         local fid
6144         local rec_list
6145         local rec
6146         local rec_type
6147         local obj_file
6148         local path
6149         local seq
6150         local oid
6151         local pass=true
6152
6153         #get fid and record list
6154         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6155                 tail -n 4))
6156         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6157                 tail -n 4))
6158         #remount mgs as ldiskfs or zfs type
6159         stop mgs || error "stop mgs failed"
6160         mount_fstype mgs || error "remount mgs failed"
6161         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6162                 fid=${fid_list[i]}
6163                 rec=${rec_list[i]}
6164                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6165                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6166                 oid=$((16#$oid))
6167
6168                 case $fstype in
6169                         ldiskfs )
6170                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6171                         zfs )
6172                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6173                 esac
6174                 echo "obj_file is $obj_file"
6175                 do_facet mgs $llog_reader $obj_file
6176
6177                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6178                         awk '{ print $3 }' | sed -e "s/^type=//g")
6179                 if [ $rec_type != $rec ]; then
6180                         echo "FAILED test_60a wrong record type $rec_type," \
6181                               "should be $rec"
6182                         pass=false
6183                         break
6184                 fi
6185
6186                 #check obj path if record type is LLOG_LOGID_MAGIC
6187                 if [ "$rec" == "1064553b" ]; then
6188                         path=$(do_facet mgs $llog_reader $obj_file |
6189                                 grep "path=" | awk '{ print $NF }' |
6190                                 sed -e "s/^path=//g")
6191                         if [ $obj_file != $mntpt/$path ]; then
6192                                 echo "FAILED test_60a wrong obj path" \
6193                                       "$montpt/$path, should be $obj_file"
6194                                 pass=false
6195                                 break
6196                         fi
6197                 fi
6198         done
6199         rm -f $TMP/$tfile
6200         #restart mgs before "error", otherwise it will block the next test
6201         stop mgs || error "stop mgs failed"
6202         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6203         $pass || error "test failed, see FAILED test_60a messages for specifics"
6204 }
6205 run_test 60a "llog_test run from kernel module and test llog_reader"
6206
6207 test_60aa() {
6208         remote_mgs_nodsh && skip "remote MGS with nodsh"
6209
6210         # test old logid format
6211         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
6212                 do_facet mgs $LCTL dl | grep MGS
6213                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
6214                         error "old llog_print failed"
6215         fi
6216
6217         # test new logid format
6218         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
6219                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
6220                         error "new llog_print failed"
6221         fi
6222 }
6223 run_test 60aa "llog_print works with FIDs and simple names"
6224
6225 test_60ab() {
6226         # test llog_print with params
6227
6228         [[ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.11.51) ]] ||
6229                 skip "Need server version greater than 2.11.51"
6230
6231         local yaml
6232         local orig_val
6233
6234         orig_val=$(do_facet mgs $LCTL get_param jobid_name)
6235         do_facet mgs $LCTL set_param -P jobid_name="testname"
6236
6237         yaml=$(do_facet mgs $LCTL --device MGS llog_print params |
6238             grep jobid_name | tail -n 1)
6239
6240         local param=`awk '{ print $10 }' <<< "$yaml"`
6241         local val=`awk '{ print $12 }' <<< "$yaml"`
6242         #return to the default
6243         do_facet mgs $LCTL set_param -P jobid_name=$orig_val
6244         [ $val = "testname" ] || error "bad value: $val"
6245         [ $param = "jobid_name," ] || error "Bad param: $param"
6246 }
6247 run_test 60ab "llog_print params output values from set_param -P"
6248
6249 test_60b() { # bug 6411
6250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6251
6252         dmesg > $DIR/$tfile
6253         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6254                                 /llog.test/ {
6255                                         if (marker)
6256                                                 from_marker++
6257                                         from_begin++
6258                                 }
6259                                 END {
6260                                         if (marker)
6261                                                 print from_marker
6262                                         else
6263                                                 print from_begin
6264                                 }")
6265         [[ $LLOG_COUNT -gt 100 ]] &&
6266                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6267 }
6268 run_test 60b "limit repeated messages from CERROR/CWARN ========"
6269
6270 test_60c() {
6271         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6272
6273         echo "create 5000 files"
6274         createmany -o $DIR/f60c- 5000
6275 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6276         lctl set_param fail_loc=0x80000137
6277         unlinkmany $DIR/f60c- 5000
6278         lctl set_param fail_loc=0
6279 }
6280 run_test 60c "unlink file when mds full"
6281
6282 test_60d() {
6283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6284
6285         SAVEPRINTK=$(lctl get_param -n printk)
6286         # verify "lctl mark" is even working"
6287         MESSAGE="test message ID $RANDOM $$"
6288         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6289         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6290
6291         lctl set_param printk=0 || error "set lnet.printk failed"
6292         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6293         MESSAGE="new test message ID $RANDOM $$"
6294         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6295         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6296         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6297
6298         lctl set_param -n printk="$SAVEPRINTK"
6299 }
6300 run_test 60d "test printk console message masking"
6301
6302 test_60e() {
6303         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6304         remote_mds_nodsh && skip "remote MDS with nodsh"
6305
6306         touch $DIR/$tfile
6307 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6308         do_facet mds1 lctl set_param fail_loc=0x15b
6309         rm $DIR/$tfile
6310 }
6311 run_test 60e "no space while new llog is being created"
6312
6313 test_61() {
6314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6315
6316         f="$DIR/f61"
6317         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
6318         cancel_lru_locks osc
6319         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6320         sync
6321 }
6322 run_test 61 "mmap() writes don't make sync hang ================"
6323
6324 # bug 2330 - insufficient obd_match error checking causes LBUG
6325 test_62() {
6326         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6327
6328         f="$DIR/f62"
6329         echo foo > $f
6330         cancel_lru_locks osc
6331         lctl set_param fail_loc=0x405
6332         cat $f && error "cat succeeded, expect -EIO"
6333         lctl set_param fail_loc=0
6334 }
6335 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6336 # match every page all of the time.
6337 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6338
6339 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6340 # Though this test is irrelevant anymore, it helped to reveal some
6341 # other grant bugs (LU-4482), let's keep it.
6342 test_63a() {   # was test_63
6343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6344
6345         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6346
6347         for i in `seq 10` ; do
6348                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6349                 sleep 5
6350                 kill $!
6351                 sleep 1
6352         done
6353
6354         rm -f $DIR/f63 || true
6355 }
6356 run_test 63a "Verify oig_wait interruption does not crash ======="
6357
6358 # bug 2248 - async write errors didn't return to application on sync
6359 # bug 3677 - async write errors left page locked
6360 test_63b() {
6361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6362
6363         debugsave
6364         lctl set_param debug=-1
6365
6366         # ensure we have a grant to do async writes
6367         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6368         rm $DIR/$tfile
6369
6370         sync    # sync lest earlier test intercept the fail_loc
6371
6372         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6373         lctl set_param fail_loc=0x80000406
6374         $MULTIOP $DIR/$tfile Owy && \
6375                 error "sync didn't return ENOMEM"
6376         sync; sleep 2; sync     # do a real sync this time to flush page
6377         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6378                 error "locked page left in cache after async error" || true
6379         debugrestore
6380 }
6381 run_test 63b "async write errors should be returned to fsync ==="
6382
6383 test_64a () {
6384         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6385
6386         df $DIR
6387         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6388 }
6389 run_test 64a "verify filter grant calculations (in kernel) ====="
6390
6391 test_64b () {
6392         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6393
6394         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6395 }
6396 run_test 64b "check out-of-space detection on client"
6397
6398 test_64c() {
6399         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6400 }
6401 run_test 64c "verify grant shrink"
6402
6403 # this does exactly what osc_request.c:osc_announce_cached() does in
6404 # order to calculate max amount of grants to ask from server
6405 want_grant() {
6406         local tgt=$1
6407
6408         local page_size=$(get_page_size client)
6409
6410         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6411         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6412
6413         ((rpc_in_flight ++));
6414         nrpages=$((nrpages * rpc_in_flight))
6415
6416         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6417
6418         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / page_size))
6419
6420         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6421         local undirty=$((nrpages * page_size))
6422
6423         local max_extent_pages
6424         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6425             grep grant_max_extent_size | awk '{print $2}')
6426         max_extent_pages=$((max_extent_pages / page_size))
6427         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6428         local grant_extent_tax
6429         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6430             grep grant_extent_tax | awk '{print $2}')
6431
6432         undirty=$((undirty + nrextents * grant_extent_tax))
6433
6434         echo $undirty
6435 }
6436
6437 # this is size of unit for grant allocation. It should be equal to
6438 # what tgt_grant.c:tgt_grant_chunk() calculates
6439 grant_chunk() {
6440         local tgt=$1
6441         local max_brw_size
6442         local grant_extent_tax
6443
6444         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6445             grep max_brw_size | awk '{print $2}')
6446
6447         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6448             grep grant_extent_tax | awk '{print $2}')
6449
6450         echo $(((max_brw_size + grant_extent_tax) * 2))
6451 }
6452
6453 test_64d() {
6454         [ $(lustre_version_code ost1) -lt $(version_code 2.10.56) ] &&
6455                 skip "OST < 2.10.55 doesn't limit grants enough"
6456
6457         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6458         local file=$DIR/$tfile
6459
6460         [[ $($LCTL get_param osc.${tgt}.import |
6461              grep "connect_flags:.*grant_param") ]] ||
6462                 skip "no grant_param connect flag"
6463
6464         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6465
6466         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6467
6468         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6469         stack_trap "rm -f $file" EXIT
6470
6471         $SETSTRIPE $file -i 0 -c 1
6472         dd if=/dev/zero of=$file bs=1M count=1000 &
6473         ddpid=$!
6474
6475         while true
6476         do
6477                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6478                 if [[ $cur_grant -gt $max_cur_granted ]]
6479                 then
6480                         kill $ddpid
6481                         error "cur_grant $cur_grant > $max_cur_granted"
6482                 fi
6483                 kill -0 $ddpid
6484                 [[ $? -ne 0 ]] && break;
6485                 sleep 2
6486         done
6487
6488         rm -f $DIR/$tfile
6489         wait_delete_completed
6490         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6491 }
6492 run_test 64d "check grant limit exceed"
6493
6494 # bug 1414 - set/get directories' stripe info
6495 test_65a() {
6496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6497
6498         test_mkdir $DIR/$tdir
6499         touch $DIR/$tdir/f1
6500         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6501 }
6502 run_test 65a "directory with no stripe info"
6503
6504 test_65b() {
6505         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6506
6507         test_mkdir $DIR/$tdir
6508         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6509
6510         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6511                                                 error "setstripe"
6512         touch $DIR/$tdir/f2
6513         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6514 }
6515 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6516
6517 test_65c() {
6518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6519         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
6520
6521         test_mkdir $DIR/$tdir
6522         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
6523
6524         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6525                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6526         touch $DIR/$tdir/f3
6527         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6528 }
6529 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6530
6531 test_65d() {
6532         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6533
6534         test_mkdir $DIR/$tdir
6535         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
6536         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6537
6538         if [[ $STRIPECOUNT -le 0 ]]; then
6539                 sc=1
6540         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6541 #LOV_MAX_STRIPE_COUNT is 2000
6542                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6543         else
6544                 sc=$(($STRIPECOUNT - 1))
6545         fi
6546         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6547         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6548         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6549                 error "lverify failed"
6550 }
6551 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6552
6553 test_65e() {
6554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6555
6556         test_mkdir $DIR/$tdir
6557
6558         $SETSTRIPE $DIR/$tdir || error "setstripe"
6559         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
6560                                         error "no stripe info failed"
6561         touch $DIR/$tdir/f6
6562         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6563 }
6564 run_test 65e "directory setstripe defaults"
6565
6566 test_65f() {
6567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6568
6569         test_mkdir $DIR/${tdir}f
6570         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
6571 }
6572 run_test 65f "dir setstripe permission (should return error) ==="
6573
6574 test_65g() {
6575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6576
6577         test_mkdir $DIR/$tdir
6578         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6579
6580         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6581                 error "setstripe -S failed"
6582         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6583         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6584                 error "delete default stripe failed"
6585 }
6586 run_test 65g "directory setstripe -d"
6587
6588 test_65h() {
6589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6590
6591         test_mkdir $DIR/$tdir
6592         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6593
6594         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6595                 error "setstripe -S failed"
6596         test_mkdir $DIR/$tdir/dd1
6597         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
6598                 error "stripe info inherit failed"
6599 }
6600 run_test 65h "directory stripe info inherit ===================="
6601
6602 test_65i() {
6603         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6604
6605         save_layout_restore_at_exit $MOUNT
6606
6607         # bug6367: set non-default striping on root directory
6608         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
6609
6610         # bug12836: getstripe on -1 default directory striping
6611         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
6612
6613         # bug12836: getstripe -v on -1 default directory striping
6614         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
6615
6616         # bug12836: new find on -1 default directory striping
6617         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6618 }
6619 run_test 65i "various tests to set root directory striping"
6620
6621 test_65j() { # bug6367
6622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6623
6624         sync; sleep 1
6625
6626         # if we aren't already remounting for each test, do so for this test
6627         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6628                 cleanup || error "failed to unmount"
6629                 setup
6630         fi
6631
6632         save_layout_restore_at_exit $MOUNT
6633
6634         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6635 }
6636 run_test 65j "set default striping on root directory (bug 6367)="
6637
6638 cleaup_65k() {
6639         rm -rf $DIR/$tdir
6640         wait_delete_completed
6641         do_facet $SINGLEMDS "lctl set_param -n \
6642                 osp.$ost*MDT0000.max_create_count=$max_count"
6643         do_facet $SINGLEMDS "lctl set_param -n \
6644                 osp.$ost*MDT0000.create_count=$count"
6645         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6646         echo $INACTIVE_OSC "is Activate"
6647
6648         wait_osc_import_state mds ost$ostnum FULL
6649 }
6650
6651 test_65k() { # bug11679
6652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6653         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6654         remote_mds_nodsh && skip "remote MDS with nodsh"
6655
6656         local disable_precreate=true
6657         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
6658                 disable_precreate=false
6659
6660         echo "Check OST status: "
6661         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6662                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6663
6664         for OSC in $MDS_OSCS; do
6665                 echo $OSC "is active"
6666                 do_facet $SINGLEMDS lctl --device %$OSC activate
6667         done
6668
6669         for INACTIVE_OSC in $MDS_OSCS; do
6670                 local ost=$(osc_to_ost $INACTIVE_OSC)
6671                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6672                                lov.*md*.target_obd |
6673                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6674
6675                 mkdir -p $DIR/$tdir
6676                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6677                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6678
6679                 echo "Deactivate: " $INACTIVE_OSC
6680                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6681
6682                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6683                               osp.$ost*MDT0000.create_count")
6684                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6685                                   osp.$ost*MDT0000.max_create_count")
6686                 $disable_precreate &&
6687                         do_facet $SINGLEMDS "lctl set_param -n \
6688                                 osp.$ost*MDT0000.max_create_count=0"
6689
6690                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6691                         [ -f $DIR/$tdir/$idx ] && continue
6692                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6693                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6694                                 { cleanup_65k;
6695                                   error "setstripe $idx should succeed"; }
6696                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6697                 done
6698                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6699                 rmdir $DIR/$tdir
6700
6701                 do_facet $SINGLEMDS "lctl set_param -n \
6702                         osp.$ost*MDT0000.max_create_count=$max_count"
6703                 do_facet $SINGLEMDS "lctl set_param -n \
6704                         osp.$ost*MDT0000.create_count=$count"
6705                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6706                 echo $INACTIVE_OSC "is Activate"
6707
6708                 wait_osc_import_state mds ost$ostnum FULL
6709         done
6710 }
6711 run_test 65k "validate manual striping works properly with deactivated OSCs"
6712
6713 test_65l() { # bug 12836
6714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6715
6716         test_mkdir -p $DIR/$tdir/test_dir
6717         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6718         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6719 }
6720 run_test 65l "lfs find on -1 stripe dir ========================"
6721
6722 test_65m() {
6723         local layout=$(save_layout $MOUNT)
6724         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
6725                 restore_layout $MOUNT $layout
6726                 error "setstripe should fail by non-root users"
6727         }
6728         true
6729 }
6730 run_test 65m "normal user can't set filesystem default stripe"
6731
6732 # bug 2543 - update blocks count on client
6733 test_66() {
6734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6735
6736         COUNT=${COUNT:-8}
6737         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6738         sync; sync_all_data; sync; sync_all_data
6739         cancel_lru_locks osc
6740         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6741         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6742 }
6743 run_test 66 "update inode blocks count on client ==============="
6744
6745 meminfo() {
6746         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6747 }
6748
6749 swap_used() {
6750         swapon -s | awk '($1 == "'$1'") { print $4 }'
6751 }
6752
6753 # bug5265, obdfilter oa2dentry return -ENOENT
6754 # #define OBD_FAIL_SRV_ENOENT 0x217
6755 test_69() {
6756         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6757         remote_ost_nodsh && skip "remote OST with nodsh"
6758
6759         f="$DIR/$tfile"
6760         $SETSTRIPE -c 1 -i 0 $f
6761
6762         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6763
6764         do_facet ost1 lctl set_param fail_loc=0x217
6765         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6766         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6767
6768         do_facet ost1 lctl set_param fail_loc=0
6769         $DIRECTIO write $f 0 2 || error "write error"
6770
6771         cancel_lru_locks osc
6772         $DIRECTIO read $f 0 1 || error "read error"
6773
6774         do_facet ost1 lctl set_param fail_loc=0x217
6775         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6776
6777         do_facet ost1 lctl set_param fail_loc=0
6778         rm -f $f
6779 }
6780 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6781
6782 test_71() {
6783         test_mkdir $DIR/$tdir
6784         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6785         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6786 }
6787 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6788
6789 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6791         [ "$RUNAS_ID" = "$UID" ] &&
6792                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6793         # Check that testing environment is properly set up. Skip if not
6794         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
6795                 skip_env "User $RUNAS_ID does not exist - skipping"
6796
6797         touch $DIR/$tfile
6798         chmod 777 $DIR/$tfile
6799         chmod ug+s $DIR/$tfile
6800         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6801                 error "$RUNAS dd $DIR/$tfile failed"
6802         # See if we are still setuid/sgid
6803         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6804                 error "S/gid is not dropped on write"
6805         # Now test that MDS is updated too
6806         cancel_lru_locks mdc
6807         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6808                 error "S/gid is not dropped on MDS"
6809         rm -f $DIR/$tfile
6810 }
6811 run_test 72a "Test that remove suid works properly (bug5695) ===="
6812
6813 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6814         local perm
6815
6816         [ "$RUNAS_ID" = "$UID" ] &&
6817                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6818         [ "$RUNAS_ID" -eq 0 ] &&
6819                 skip_env "RUNAS_ID = 0 -- skipping"
6820         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6821         # Check that testing environment is properly set up. Skip if not
6822         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
6823                 skip_env "User $RUNAS_ID does not exist - skipping"
6824
6825         touch $DIR/${tfile}-f{g,u}
6826         test_mkdir $DIR/${tfile}-dg
6827         test_mkdir $DIR/${tfile}-du
6828         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6829         chmod g+s $DIR/${tfile}-{f,d}g
6830         chmod u+s $DIR/${tfile}-{f,d}u
6831         for perm in 777 2777 4777; do
6832                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6833                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6834                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6835                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6836         done
6837         true
6838 }
6839 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6840
6841 # bug 3462 - multiple simultaneous MDC requests
6842 test_73() {
6843         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6844
6845         test_mkdir $DIR/d73-1
6846         test_mkdir $DIR/d73-2
6847         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6848         pid1=$!
6849
6850         lctl set_param fail_loc=0x80000129
6851         $MULTIOP $DIR/d73-1/f73-2 Oc &
6852         sleep 1
6853         lctl set_param fail_loc=0
6854
6855         $MULTIOP $DIR/d73-2/f73-3 Oc &
6856         pid3=$!
6857
6858         kill -USR1 $pid1
6859         wait $pid1 || return 1
6860
6861         sleep 25
6862
6863         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6864         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6865         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6866
6867         rm -rf $DIR/d73-*
6868 }
6869 run_test 73 "multiple MDC requests (should not deadlock)"
6870
6871 test_74a() { # bug 6149, 6184
6872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6873
6874         touch $DIR/f74a
6875         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6876         #
6877         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6878         # will spin in a tight reconnection loop
6879         $LCTL set_param fail_loc=0x8000030e
6880         # get any lock that won't be difficult - lookup works.
6881         ls $DIR/f74a
6882         $LCTL set_param fail_loc=0
6883         rm -f $DIR/f74a
6884         true
6885 }
6886 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6887
6888 test_74b() { # bug 13310
6889         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6890
6891         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6892         #
6893         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6894         # will spin in a tight reconnection loop
6895         $LCTL set_param fail_loc=0x8000030e
6896         # get a "difficult" lock
6897         touch $DIR/f74b
6898         $LCTL set_param fail_loc=0
6899         rm -f $DIR/f74b
6900         true
6901 }
6902 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6903
6904 test_74c() {
6905         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6906
6907         #define OBD_FAIL_LDLM_NEW_LOCK
6908         $LCTL set_param fail_loc=0x319
6909         touch $DIR/$tfile && error "touch successful"
6910         $LCTL set_param fail_loc=0
6911         true
6912 }
6913 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6914
6915 num_inodes() {
6916         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6917 }
6918
6919 test_76() { # Now for bug 20433, added originally in bug 1443
6920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6921
6922         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6923
6924         cancel_lru_locks osc
6925         BEFORE_INODES=$(num_inodes)
6926         echo "before inodes: $BEFORE_INODES"
6927         local COUNT=1000
6928         [ "$SLOW" = "no" ] && COUNT=100
6929         for i in $(seq $COUNT); do
6930                 touch $DIR/$tfile
6931                 rm -f $DIR/$tfile
6932         done
6933         cancel_lru_locks osc
6934         AFTER_INODES=$(num_inodes)
6935         echo "after inodes: $AFTER_INODES"
6936         local wait=0
6937         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6938                 sleep 2
6939                 AFTER_INODES=$(num_inodes)
6940                 wait=$((wait+2))
6941                 echo "wait $wait seconds inodes: $AFTER_INODES"
6942                 if [ $wait -gt 30 ]; then
6943                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6944                 fi
6945         done
6946 }
6947 run_test 76 "confirm clients recycle inodes properly ===="
6948
6949
6950 export ORIG_CSUM=""
6951 set_checksums()
6952 {
6953         # Note: in sptlrpc modes which enable its own bulk checksum, the
6954         # original crc32_le bulk checksum will be automatically disabled,
6955         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6956         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6957         # In this case set_checksums() will not be no-op, because sptlrpc
6958         # bulk checksum will be enabled all through the test.
6959
6960         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6961         lctl set_param -n osc.*.checksums $1
6962         return 0
6963 }
6964
6965 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6966                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6967 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6968                              tr -d [] | head -n1)}
6969 set_checksum_type()
6970 {
6971         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6972         log "set checksum type to $1"
6973         return 0
6974 }
6975 F77_TMP=$TMP/f77-temp
6976 F77SZ=8
6977 setup_f77() {
6978         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6979                 error "error writing to $F77_TMP"
6980 }
6981
6982 test_77a() { # bug 10889
6983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6984         $GSS && skip_env "could not run with gss"
6985
6986         [ ! -f $F77_TMP ] && setup_f77
6987         set_checksums 1
6988         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6989         set_checksums 0
6990         rm -f $DIR/$tfile
6991 }
6992 run_test 77a "normal checksum read/write operation"
6993
6994 test_77b() { # bug 10889
6995         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6996         $GSS && skip_env "could not run with gss"
6997
6998         [ ! -f $F77_TMP ] && setup_f77
6999         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7000         $LCTL set_param fail_loc=0x80000409
7001         set_checksums 1
7002
7003         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7004                 error "dd error: $?"
7005         $LCTL set_param fail_loc=0
7006
7007         for algo in $CKSUM_TYPES; do
7008                 cancel_lru_locks osc
7009                 set_checksum_type $algo
7010                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7011                 $LCTL set_param fail_loc=0x80000408
7012                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7013                 $LCTL set_param fail_loc=0
7014         done
7015         set_checksums 0
7016         set_checksum_type $ORIG_CSUM_TYPE
7017         rm -f $DIR/$tfile
7018 }
7019 run_test 77b "checksum error on client write, read"
7020
7021 cleanup_77c() {
7022         trap 0
7023         set_checksums 0
7024         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7025         $check_ost &&
7026                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7027         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7028         $check_ost && [ -n "$ost_file_prefix" ] &&
7029                 do_facet ost1 rm -f ${ost_file_prefix}\*
7030 }
7031
7032 test_77c() {
7033         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7034         $GSS && skip_env "could not run with gss"
7035         remote_ost_nodsh && skip "remote OST with nodsh"
7036
7037         local bad1
7038         local osc_file_prefix
7039         local osc_file
7040         local check_ost=false
7041         local ost_file_prefix
7042         local ost_file
7043         local orig_cksum
7044         local dump_cksum
7045         local fid
7046
7047         # ensure corruption will occur on first OSS/OST
7048         $LFS setstripe -i 0 $DIR/$tfile
7049
7050         [ ! -f $F77_TMP ] && setup_f77
7051         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7052                 error "dd write error: $?"
7053         fid=$($LFS path2fid $DIR/$tfile)
7054
7055         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
7056         then
7057                 check_ost=true
7058                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7059                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7060         else
7061                 echo "OSS do not support bulk pages dump upon error"
7062         fi
7063
7064         osc_file_prefix=$($LCTL get_param -n debug_path)
7065         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7066
7067         trap cleanup_77c EXIT
7068
7069         set_checksums 1
7070         # enable bulk pages dump upon error on Client
7071         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7072         # enable bulk pages dump upon error on OSS
7073         $check_ost &&
7074                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7075
7076         # flush Client cache to allow next read to reach OSS
7077         cancel_lru_locks osc
7078
7079         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7080         $LCTL set_param fail_loc=0x80000408
7081         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7082         $LCTL set_param fail_loc=0
7083
7084         rm -f $DIR/$tfile
7085
7086         # check cksum dump on Client
7087         osc_file=$(ls ${osc_file_prefix}*)
7088         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7089         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7090         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7091         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7092         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7093                      cksum)
7094         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7095         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7096                 error "dump content does not match on Client"
7097
7098         $check_ost || skip "No need to check cksum dump on OSS"
7099
7100         # check cksum dump on OSS
7101         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7102         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7103         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7104         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7105         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7106                 error "dump content does not match on OSS"
7107
7108         cleanup_77c
7109 }
7110 run_test 77c "checksum error on client read with debug"
7111
7112 test_77d() { # bug 10889
7113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7114         $GSS && skip_env "could not run with gss"
7115
7116         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7117         $LCTL set_param fail_loc=0x80000409
7118         set_checksums 1
7119         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7120                 error "direct write: rc=$?"
7121         $LCTL set_param fail_loc=0
7122         set_checksums 0
7123
7124         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7125         $LCTL set_param fail_loc=0x80000408
7126         set_checksums 1
7127         cancel_lru_locks osc
7128         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7129                 error "direct read: rc=$?"
7130         $LCTL set_param fail_loc=0
7131         set_checksums 0
7132 }
7133 run_test 77d "checksum error on OST direct write, read"
7134
7135 test_77f() { # bug 10889
7136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7137         $GSS && skip_env "could not run with gss"
7138
7139         set_checksums 1
7140         for algo in $CKSUM_TYPES; do
7141                 cancel_lru_locks osc
7142                 set_checksum_type $algo
7143                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7144                 $LCTL set_param fail_loc=0x409
7145                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7146                         error "direct write succeeded"
7147                 $LCTL set_param fail_loc=0
7148         done
7149         set_checksum_type $ORIG_CSUM_TYPE
7150         set_checksums 0
7151 }
7152 run_test 77f "repeat checksum error on write (expect error)"
7153
7154 test_77g() { # bug 10889
7155         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7156         $GSS && skip_env "could not run with gss"
7157         remote_ost_nodsh && skip "remote OST with nodsh"
7158
7159         [ ! -f $F77_TMP ] && setup_f77
7160
7161         local file=$DIR/$tfile
7162         stack_trap "rm -f $file" EXIT
7163
7164         $SETSTRIPE -c 1 -i 0 $file
7165         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7166         do_facet ost1 lctl set_param fail_loc=0x8000021a
7167         set_checksums 1
7168         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7169                 error "write error: rc=$?"
7170         do_facet ost1 lctl set_param fail_loc=0
7171         set_checksums 0
7172
7173         cancel_lru_locks osc
7174         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7175         do_facet ost1 lctl set_param fail_loc=0x8000021b
7176         set_checksums 1
7177         cmp $F77_TMP $file || error "file compare failed"
7178         do_facet ost1 lctl set_param fail_loc=0
7179         set_checksums 0
7180 }
7181 run_test 77g "checksum error on OST write, read"
7182
7183 test_77k() { # LU-10906
7184         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7185         $GSS && skip_env "could not run with gss"
7186
7187         local cksum_param="osc.$FSNAME*.checksums"
7188         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7189         local checksum
7190         local i
7191
7192         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7193         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7194         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7195                 EXIT
7196
7197         for i in 0 1; do
7198                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7199                         error "failed to set checksum=$i on MGS"
7200                 wait_update $HOSTNAME "$get_checksum" $i
7201                 #remount
7202                 echo "remount client, checksum should be $i"
7203                 remount_client $MOUNT || "failed to remount client"
7204                 checksum=$(eval $get_checksum)
7205                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7206         done
7207
7208         for opt in "checksum" "nochecksum"; do
7209                 #remount with mount option
7210                 echo "remount client with option $opt, checksum should be $i"
7211                 umount_client $MOUNT || "failed to umount client"
7212                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7213                         "failed to mount client with option '$opt'"
7214                 checksum=$(eval $get_checksum)
7215                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7216                 i=$((i - 1))
7217         done
7218
7219         remount_client $MOUNT || "failed to remount client"
7220 }
7221 run_test 77k "enable/disable checksum correctly"
7222
7223 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7224 rm -f $F77_TMP
7225 unset F77_TMP
7226
7227 cleanup_test_78() {
7228         trap 0
7229         rm -f $DIR/$tfile
7230 }
7231
7232 test_78() { # bug 10901
7233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7234         remote_ost || skip_env "local OST"
7235
7236         NSEQ=5
7237         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7238         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7239         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7240         echo "MemTotal: $MEMTOTAL"
7241
7242         # reserve 256MB of memory for the kernel and other running processes,
7243         # and then take 1/2 of the remaining memory for the read/write buffers.
7244         if [ $MEMTOTAL -gt 512 ] ;then
7245                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7246         else
7247                 # for those poor memory-starved high-end clusters...
7248                 MEMTOTAL=$((MEMTOTAL / 2))
7249         fi
7250         echo "Mem to use for directio: $MEMTOTAL"
7251
7252         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7253         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7254         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7255         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7256                 head -n1)
7257         echo "Smallest OST: $SMALLESTOST"
7258         [[ $SMALLESTOST -lt 10240 ]] &&
7259                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7260
7261         trap cleanup_test_78 EXIT
7262
7263         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7264                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7265
7266         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7267         echo "File size: $F78SIZE"
7268         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7269         for i in $(seq 1 $NSEQ); do
7270                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7271                 echo directIO rdwr round $i of $NSEQ
7272                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7273         done
7274
7275         cleanup_test_78
7276 }
7277 run_test 78 "handle large O_DIRECT writes correctly ============"
7278
7279 test_79() { # bug 12743
7280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7281
7282         wait_delete_completed
7283
7284         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7285         BKFREE=$(calc_osc_kbytes kbytesfree)
7286         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7287
7288         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7289         DFTOTAL=`echo $STRING | cut -d, -f1`
7290         DFUSED=`echo $STRING  | cut -d, -f2`
7291         DFAVAIL=`echo $STRING | cut -d, -f3`
7292         DFFREE=$(($DFTOTAL - $DFUSED))
7293
7294         ALLOWANCE=$((64 * $OSTCOUNT))
7295
7296         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7297            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7298                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7299         fi
7300         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7301            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7302                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7303         fi
7304         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7305            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7306                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7307         fi
7308 }
7309 run_test 79 "df report consistency check ======================="
7310
7311 test_80() { # bug 10718
7312         remote_ost_nodsh && skip "remote OST with nodsh"
7313         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7314
7315         # relax strong synchronous semantics for slow backends like ZFS
7316         local soc="obdfilter.*.sync_on_lock_cancel"
7317         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
7318         local hosts=
7319         if [ "$soc_old" != "never" ] &&
7320                 [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
7321                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
7322                                 facet_active_host $host; done | sort -u)
7323                         do_nodes $hosts lctl set_param $soc=never
7324         fi
7325
7326         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7327         sync; sleep 1; sync
7328         local BEFORE=`date +%s`
7329         cancel_lru_locks osc
7330         local AFTER=`date +%s`
7331         local DIFF=$((AFTER-BEFORE))
7332         if [ $DIFF -gt 1 ] ; then
7333                 error "elapsed for 1M@1T = $DIFF"
7334         fi
7335
7336         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
7337
7338         rm -f $DIR/$tfile
7339 }
7340 run_test 80 "Page eviction is equally fast at high offsets too  ===="
7341
7342 test_81a() { # LU-456
7343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7344         remote_ost_nodsh && skip "remote OST with nodsh"
7345
7346         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7347         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
7348         do_facet ost1 lctl set_param fail_loc=0x80000228
7349
7350         # write should trigger a retry and success
7351         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7352         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7353         RC=$?
7354         if [ $RC -ne 0 ] ; then
7355                 error "write should success, but failed for $RC"
7356         fi
7357 }
7358 run_test 81a "OST should retry write when get -ENOSPC ==============="
7359
7360 test_81b() { # LU-456
7361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7362         remote_ost_nodsh && skip "remote OST with nodsh"
7363
7364         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7365         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
7366         do_facet ost1 lctl set_param fail_loc=0x228
7367
7368         # write should retry several times and return -ENOSPC finally
7369         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7370         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7371         RC=$?
7372         ENOSPC=28
7373         if [ $RC -ne $ENOSPC ] ; then
7374                 error "dd should fail for -ENOSPC, but succeed."
7375         fi
7376 }
7377 run_test 81b "OST should return -ENOSPC when retry still fails ======="
7378
7379 test_82() { # LU-1031
7380         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
7381         local gid1=14091995
7382         local gid2=16022000
7383
7384         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
7385         local MULTIPID1=$!
7386         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
7387         local MULTIPID2=$!
7388         kill -USR1 $MULTIPID2
7389         sleep 2
7390         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
7391                 error "First grouplock does not block second one"
7392         else
7393                 echo "Second grouplock blocks first one"
7394         fi
7395         kill -USR1 $MULTIPID1
7396         wait $MULTIPID1
7397         wait $MULTIPID2
7398 }
7399 run_test 82 "Basic grouplock test"
7400
7401 test_83() {
7402         local sfile="/boot/System.map-$(uname -r)"
7403         [ ! -f $sfile ] && skip "No $sfile found"
7404         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
7405         $LCTL set_param fail_loc=0x140d
7406         cp $sfile $DIR/$tfile || error "write failed"
7407         diff -c $sfile $DIR/$tfile || error "files are different"
7408         $LCTL set_param fail_loc=0
7409         rm -f $DIR/$tfile
7410 }
7411 run_test 83 "Short write in ptask ==============================="
7412
7413 test_99() {
7414         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
7415
7416         test_mkdir $DIR/$tdir.cvsroot
7417         chown $RUNAS_ID $DIR/$tdir.cvsroot
7418
7419         cd $TMP
7420         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7421
7422         cd /etc/init.d
7423         # some versions of cvs import exit(1) when asked to import links or
7424         # files they can't read.  ignore those files.
7425         local toignore=$(find . -type l -printf '-I %f\n' -o \
7426                          ! -perm /4 -printf '-I %f\n')
7427         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7428                 $tdir.reposname vtag rtag
7429
7430         cd $DIR
7431         test_mkdir $DIR/$tdir.reposname
7432         chown $RUNAS_ID $DIR/$tdir.reposname
7433         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7434
7435         cd $DIR/$tdir.reposname
7436         $RUNAS touch foo99
7437         $RUNAS cvs add -m 'addmsg' foo99
7438         $RUNAS cvs update
7439         $RUNAS cvs commit -m 'nomsg' foo99
7440         rm -fr $DIR/$tdir.cvsroot
7441 }
7442 run_test 99 "cvs strange file/directory operations"
7443
7444 test_100() {
7445         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7446         [[ "$NETTYPE" =~ tcp ]] ||
7447                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
7448         remote_ost_nodsh && skip "remote OST with nodsh"
7449         remote_mds_nodsh && skip "remote MDS with nodsh"
7450         remote_servers ||
7451                 skip "useless for local single node setup"
7452
7453         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7454                 [ "$PROT" != "tcp" ] && continue
7455                 RPORT=$(echo $REMOTE | cut -d: -f2)
7456                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7457
7458                 rc=0
7459                 LPORT=`echo $LOCAL | cut -d: -f2`
7460                 if [ $LPORT -ge 1024 ]; then
7461                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7462                         netstat -tna
7463                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7464                 fi
7465         done
7466         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7467 }
7468 run_test 100 "check local port using privileged port ==========="
7469
7470 function get_named_value()
7471 {
7472     local tag
7473
7474     tag=$1
7475     while read ;do
7476         line=$REPLY
7477         case $line in
7478         $tag*)
7479             echo $line | sed "s/^$tag[ ]*//"
7480             break
7481             ;;
7482         esac
7483     done
7484 }
7485
7486 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7487                    awk '/^max_cached_mb/ { print $2 }')
7488
7489 cleanup_101a() {
7490         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7491         trap 0
7492 }
7493
7494 test_101a() {
7495         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7496         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
7497
7498         local s
7499         local discard
7500         local nreads=10000
7501         local cache_limit=32
7502
7503         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7504         trap cleanup_101a EXIT
7505         $LCTL set_param -n llite.*.read_ahead_stats 0
7506         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7507
7508         #
7509         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7510         #
7511         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7512         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7513
7514         discard=0
7515         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7516                 get_named_value 'read but discarded' | cut -d" " -f1); do
7517                         discard=$(($discard + $s))
7518         done
7519         cleanup_101a
7520
7521         if [[ $(($discard * 10)) -gt $nreads ]]; then
7522                 $LCTL get_param osc.*-osc*.rpc_stats
7523                 $LCTL get_param llite.*.read_ahead_stats
7524                 error "too many ($discard) discarded pages"
7525         fi
7526         rm -f $DIR/$tfile || true
7527 }
7528 run_test 101a "check read-ahead for random reads"
7529
7530 setup_test101bc() {
7531         test_mkdir $DIR/$tdir
7532         local STRIPE_SIZE=$1
7533         local FILE_LENGTH=$2
7534         STRIPE_OFFSET=0
7535
7536         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
7537
7538         local list=$(comma_list $(osts_nodes))
7539         set_osd_param $list '' read_cache_enable 0
7540         set_osd_param $list '' writethrough_cache_enable 0
7541
7542         trap cleanup_test101bc EXIT
7543         # prepare the read-ahead file
7544         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
7545
7546         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
7547                                 count=$FILE_SIZE_MB 2> /dev/null
7548
7549 }
7550
7551 cleanup_test101bc() {
7552         trap 0
7553         rm -rf $DIR/$tdir
7554         rm -f $DIR/$tfile
7555
7556         local list=$(comma_list $(osts_nodes))
7557         set_osd_param $list '' read_cache_enable 1
7558         set_osd_param $list '' writethrough_cache_enable 1
7559 }
7560
7561 calc_total() {
7562         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
7563 }
7564
7565 ra_check_101() {
7566         local READ_SIZE=$1
7567         local STRIPE_SIZE=$2
7568         local FILE_LENGTH=$3
7569         local RA_INC=1048576
7570         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
7571         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
7572                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
7573         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
7574                         get_named_value 'read but discarded' |
7575                         cut -d" " -f1 | calc_total)
7576         if [[ $DISCARD -gt $discard_limit ]]; then
7577                 $LCTL get_param llite.*.read_ahead_stats
7578                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
7579         else
7580                 echo "Read-ahead success for size ${READ_SIZE}"
7581         fi
7582 }
7583
7584 test_101b() {
7585         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7586         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7587
7588         local STRIPE_SIZE=1048576
7589         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
7590
7591         if [ $SLOW == "yes" ]; then
7592                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
7593         else
7594                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
7595         fi
7596
7597         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
7598
7599         # prepare the read-ahead file
7600         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7601         cancel_lru_locks osc
7602         for BIDX in 2 4 8 16 32 64 128 256
7603         do
7604                 local BSIZE=$((BIDX*4096))
7605                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
7606                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
7607                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
7608                 $LCTL set_param -n llite.*.read_ahead_stats 0
7609                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
7610                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
7611                 cancel_lru_locks osc
7612                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
7613         done
7614         cleanup_test101bc
7615         true
7616 }
7617 run_test 101b "check stride-io mode read-ahead ================="
7618
7619 test_101c() {
7620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7621
7622         local STRIPE_SIZE=1048576
7623         local FILE_LENGTH=$((STRIPE_SIZE*100))
7624         local nreads=10000
7625         local osc_rpc_stats
7626
7627         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7628
7629         cancel_lru_locks osc
7630         $LCTL set_param osc.*.rpc_stats 0
7631         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
7632         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
7633                 local stats=$($LCTL get_param -n $osc_rpc_stats)
7634                 local lines=$(echo "$stats" | awk 'END {print NR;}')
7635                 local size
7636
7637                 if [ $lines -le 20 ]; then
7638                         continue
7639                 fi
7640                 for size in 1 2 4 8; do
7641                         local rpc=$(echo "$stats" |
7642                                     awk '($1 == "'$size':") {print $2; exit; }')
7643                         [ $rpc != 0 ] &&
7644                                 error "Small $((size*4))k read IO $rpc !"
7645                 done
7646                 echo "$osc_rpc_stats check passed!"
7647         done
7648         cleanup_test101bc
7649         true
7650 }
7651 run_test 101c "check stripe_size aligned read-ahead ================="
7652
7653 set_read_ahead() {
7654         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
7655         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
7656 }
7657
7658 test_101d() {
7659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7660
7661         local file=$DIR/$tfile
7662         local sz_MB=${FILESIZE_101d:-500}
7663         local ra_MB=${READAHEAD_MB:-40}
7664
7665         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
7666         [ $free_MB -lt $sz_MB ] &&
7667                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
7668
7669         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
7670         $SETSTRIPE -c -1 $file || error "setstripe failed"
7671
7672         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
7673         echo Cancel LRU locks on lustre client to flush the client cache
7674         cancel_lru_locks osc
7675
7676         echo Disable read-ahead
7677         local old_READAHEAD=$(set_read_ahead 0)
7678
7679         echo Reading the test file $file with read-ahead disabled
7680         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7681
7682         echo Cancel LRU locks on lustre client to flush the client cache
7683         cancel_lru_locks osc
7684         echo Enable read-ahead with ${ra_MB}MB
7685         set_read_ahead $ra_MB
7686
7687         echo Reading the test file $file with read-ahead enabled
7688         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7689
7690         echo "read-ahead disabled time read $raOFF"
7691         echo "read-ahead enabled  time read $raON"
7692
7693         set_read_ahead $old_READAHEAD
7694         rm -f $file
7695         wait_delete_completed
7696
7697         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7698                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7699 }
7700 run_test 101d "file read with and without read-ahead enabled"
7701
7702 test_101e() {
7703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7704
7705         local file=$DIR/$tfile
7706         local size_KB=500  #KB
7707         local count=100
7708         local bsize=1024
7709
7710         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7711         local need_KB=$((count * size_KB))
7712         [[ $free_KB -le $need_KB ]] &&
7713                 skip_env "Need free space $need_KB, have $free_KB"
7714
7715         echo "Creating $count ${size_KB}K test files"
7716         for ((i = 0; i < $count; i++)); do
7717                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7718         done
7719
7720         echo "Cancel LRU locks on lustre client to flush the client cache"
7721         cancel_lru_locks $OSC
7722
7723         echo "Reset readahead stats"
7724         $LCTL set_param -n llite.*.read_ahead_stats 0
7725
7726         for ((i = 0; i < $count; i++)); do
7727                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7728         done
7729
7730         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7731                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7732
7733         for ((i = 0; i < $count; i++)); do
7734                 rm -rf $file.$i 2>/dev/null
7735         done
7736
7737         #10000 means 20% reads are missing in readahead
7738         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7739 }
7740 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7741
7742 test_101f() {
7743         which iozone || skip_env "no iozone installed"
7744
7745         local old_debug=$($LCTL get_param debug)
7746         old_debug=${old_debug#*=}
7747         $LCTL set_param debug="reada mmap"
7748
7749         # create a test file
7750         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7751
7752         echo Cancel LRU locks on lustre client to flush the client cache
7753         cancel_lru_locks osc
7754
7755         echo Reset readahead stats
7756         $LCTL set_param -n llite.*.read_ahead_stats 0
7757
7758         echo mmap read the file with small block size
7759         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7760                 > /dev/null 2>&1
7761
7762         echo checking missing pages
7763         $LCTL get_param llite.*.read_ahead_stats
7764         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7765                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7766
7767         $LCTL set_param debug="$old_debug"
7768         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7769         rm -f $DIR/$tfile
7770 }
7771 run_test 101f "check mmap read performance"
7772
7773 test_101g_brw_size_test() {
7774         local mb=$1
7775         local pages=$((mb * 1048576 / $(page_size)))
7776         local file=$DIR/$tfile
7777
7778         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7779                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7780         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7781                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7782                         return 2
7783         done
7784
7785         stack_trap "rm -f $file" EXIT
7786         $LCTL set_param -n osc.*.rpc_stats=0
7787
7788         # 10 RPCs should be enough for the test
7789         local count=10
7790         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
7791                 { error "dd write ${mb} MB blocks failed"; return 3; }
7792         cancel_lru_locks osc
7793         dd of=/dev/null if=$file bs=${mb}M count=$count ||
7794                 { error "dd write ${mb} MB blocks failed"; return 4; }
7795
7796         # calculate number of full-sized read and write RPCs
7797         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7798                 sed -n '/pages per rpc/,/^$/p' |
7799                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7800                 END { print reads,writes }'))
7801         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7802                 return 5
7803         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7804                 return 6
7805
7806         return 0
7807 }
7808
7809 test_101g() {
7810         remote_ost_nodsh && skip "remote OST with nodsh"
7811
7812         local rpcs
7813         local osts=$(get_facets OST)
7814         local list=$(comma_list $(osts_nodes))
7815         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7816         local brw_size="obdfilter.*.brw_size"
7817         local ostver=$(lustre_version_code ost1)
7818         local cliver=$(lustre_version_code client)
7819
7820         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7821
7822         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7823         if [ $ostver -ge $(version_code 2.8.52) -o \
7824              \( $ostver -ge $(version_code 2.7.17) -a \
7825                 $ostver -lt $(version_code 2.7.50) \) ] &&
7826            [ $cliver -ge $(version_code 2.8.52) -o \
7827              \( $cliver -ge $(version_code 2.7.17) -a \
7828                 $cliver -lt $(version_code 2.7.50) \) ]; then
7829                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7830                 if [[ $orig_mb -lt 16 ]]; then
7831                         save_lustre_params $osts "$brw_size" > $p
7832                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7833                                 error "set 16MB RPC size failed"
7834
7835                         echo "remount client to enable new RPC size"
7836                         remount_client $MOUNT || error "remount_client failed"
7837                 fi
7838
7839                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7840                 # should be able to set brw_size=12, but no rpc_stats for that
7841                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7842         fi
7843
7844         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7845
7846         if [[ $orig_mb -lt 16 ]]; then
7847                 restore_lustre_params < $p
7848                 remount_client $MOUNT || error "remount_client restore failed"
7849         fi
7850
7851         rm -f $p $DIR/$tfile
7852 }
7853 run_test 101g "Big bulk(4/16 MiB) readahead"
7854
7855 setup_test102() {
7856         test_mkdir $DIR/$tdir
7857         chown $RUNAS_ID $DIR/$tdir
7858         STRIPE_SIZE=65536
7859         STRIPE_OFFSET=1
7860         STRIPE_COUNT=$OSTCOUNT
7861         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7862
7863         trap cleanup_test102 EXIT
7864         cd $DIR
7865         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7866         cd $DIR/$tdir
7867         for num in 1 2 3 4; do
7868                 for count in $(seq 1 $STRIPE_COUNT); do
7869                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7870                                 local size=`expr $STRIPE_SIZE \* $num`
7871                                 local file=file"$num-$idx-$count"
7872                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7873                         done
7874                 done
7875         done
7876
7877         cd $DIR
7878         $1 tar cf $TMP/f102.tar $tdir --xattrs
7879 }
7880
7881 cleanup_test102() {
7882         trap 0
7883         rm -f $TMP/f102.tar
7884         rm -rf $DIR/d0.sanity/d102
7885 }
7886
7887 test_102a() {
7888         [ "$UID" != 0 ] && skip "must run as root"
7889         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7890                 skip_env "must have user_xattr"
7891
7892         [ -z "$(which setfattr 2>/dev/null)" ] &&
7893                 skip_env "could not find setfattr"
7894
7895         local testfile=$DIR/$tfile
7896
7897         touch $testfile
7898         echo "set/get xattr..."
7899         setfattr -n trusted.name1 -v value1 $testfile ||
7900                 error "setfattr -n trusted.name1=value1 $testfile failed"
7901         getfattr -n trusted.name1 $testfile 2> /dev/null |
7902           grep "trusted.name1=.value1" ||
7903                 error "$testfile missing trusted.name1=value1"
7904
7905         setfattr -n user.author1 -v author1 $testfile ||
7906                 error "setfattr -n user.author1=author1 $testfile failed"
7907         getfattr -n user.author1 $testfile 2> /dev/null |
7908           grep "user.author1=.author1" ||
7909                 error "$testfile missing trusted.author1=author1"
7910
7911         echo "listxattr..."
7912         setfattr -n trusted.name2 -v value2 $testfile ||
7913                 error "$testfile unable to set trusted.name2"
7914         setfattr -n trusted.name3 -v value3 $testfile ||
7915                 error "$testfile unable to set trusted.name3"
7916         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7917             grep "trusted.name" | wc -l) -eq 3 ] ||
7918                 error "$testfile missing 3 trusted.name xattrs"
7919
7920         setfattr -n user.author2 -v author2 $testfile ||
7921                 error "$testfile unable to set user.author2"
7922         setfattr -n user.author3 -v author3 $testfile ||
7923                 error "$testfile unable to set user.author3"
7924         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7925             grep "user.author" | wc -l) -eq 3 ] ||
7926                 error "$testfile missing 3 user.author xattrs"
7927
7928         echo "remove xattr..."
7929         setfattr -x trusted.name1 $testfile ||
7930                 error "$testfile error deleting trusted.name1"
7931         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7932                 error "$testfile did not delete trusted.name1 xattr"
7933
7934         setfattr -x user.author1 $testfile ||
7935                 error "$testfile error deleting user.author1"
7936         echo "set lustre special xattr ..."
7937         $LFS setstripe -c1 $testfile
7938         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7939                 awk -F "=" '/trusted.lov/ { print $2 }' )
7940         setfattr -n "trusted.lov" -v $lovea $testfile ||
7941                 error "$testfile doesn't ignore setting trusted.lov again"
7942         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7943                 error "$testfile allow setting invalid trusted.lov"
7944         rm -f $testfile
7945 }
7946 run_test 102a "user xattr test =================================="
7947
7948 test_102b() {
7949         [ -z "$(which setfattr 2>/dev/null)" ] &&
7950                 skip_env "could not find setfattr"
7951         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7952
7953         # b10930: get/set/list trusted.lov xattr
7954         echo "get/set/list trusted.lov xattr ..."
7955         local testfile=$DIR/$tfile
7956         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7957                 error "setstripe failed"
7958         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7959                 error "getstripe failed"
7960         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7961                 error "can't get trusted.lov from $testfile"
7962
7963         local testfile2=${testfile}2
7964         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7965                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7966
7967         $MCREATE $testfile2
7968         setfattr -n trusted.lov -v $value $testfile2
7969         local stripe_size=$($GETSTRIPE -S $testfile2)
7970         local stripe_count=$($GETSTRIPE -c $testfile2)
7971         [[ $stripe_size -eq 65536 ]] ||
7972                 error "stripe size $stripe_size != 65536"
7973         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7974                 error "stripe count $stripe_count != $STRIPECOUNT"
7975         rm -f $DIR/$tfile
7976 }
7977 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7978
7979 test_102c() {
7980         [ -z "$(which setfattr 2>/dev/null)" ] &&
7981                 skip_env "could not find setfattr"
7982         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7983
7984         # b10930: get/set/list lustre.lov xattr
7985         echo "get/set/list lustre.lov xattr ..."
7986         test_mkdir $DIR/$tdir
7987         chown $RUNAS_ID $DIR/$tdir
7988         local testfile=$DIR/$tdir/$tfile
7989         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7990                 error "setstripe failed"
7991         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7992                 error "getstripe failed"
7993         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7994         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7995
7996         local testfile2=${testfile}2
7997         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7998                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7999
8000         $RUNAS $MCREATE $testfile2
8001         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8002         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8003         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8004         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8005         [ $stripe_count -eq $STRIPECOUNT ] ||
8006                 error "stripe count $stripe_count != $STRIPECOUNT"
8007 }
8008 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8009
8010 compare_stripe_info1() {
8011         local stripe_index_all_zero=true
8012
8013         for num in 1 2 3 4; do
8014                 for count in $(seq 1 $STRIPE_COUNT); do
8015                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8016                                 local size=$((STRIPE_SIZE * num))
8017                                 local file=file"$num-$offset-$count"
8018                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8019                                 [[ $stripe_size -ne $size ]] &&
8020                                     error "$file: size $stripe_size != $size"
8021                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8022                                 # allow fewer stripes to be created, ORI-601
8023                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8024                                     error "$file: count $stripe_count != $count"
8025                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8026                                 [[ $stripe_index -ne 0 ]] &&
8027                                         stripe_index_all_zero=false
8028                         done
8029                 done
8030         done
8031         $stripe_index_all_zero &&
8032                 error "all files are being extracted starting from OST index 0"
8033         return 0
8034 }
8035
8036 have_xattrs_include() {
8037         tar --help | grep -q xattrs-include &&
8038                 echo --xattrs-include="lustre.*"
8039 }
8040
8041 test_102d() {
8042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8043         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8044
8045         XINC=$(have_xattrs_include)
8046         setup_test102
8047         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8048         cd $DIR/$tdir/$tdir
8049         compare_stripe_info1
8050 }
8051 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8052
8053 test_102f() {
8054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8055         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8056
8057         XINC=$(have_xattrs_include)
8058         setup_test102
8059         test_mkdir $DIR/$tdir.restore
8060         cd $DIR
8061         tar cf - --xattrs $tdir | tar xf - \
8062                 -C $DIR/$tdir.restore --xattrs $XINC
8063         cd $DIR/$tdir.restore/$tdir
8064         compare_stripe_info1
8065 }
8066 run_test 102f "tar copy files, not keep osts"
8067
8068 grow_xattr() {
8069         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8070                 skip "must have user_xattr"
8071         [ -z "$(which setfattr 2>/dev/null)" ] &&
8072                 skip_env "could not find setfattr"
8073         [ -z "$(which getfattr 2>/dev/null)" ] &&
8074                 skip_env "could not find getfattr"
8075
8076         local xsize=${1:-1024}  # in bytes
8077         local file=$DIR/$tfile
8078         local value="$(generate_string $xsize)"
8079         local xbig=trusted.big
8080
8081         touch $file
8082         log "save $xbig on $file"
8083         setfattr -n $xbig -v $value $file ||
8084                 error "saving $xbig on $file failed"
8085
8086         local orig=$(get_xattr_value $xbig $file)
8087         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8088
8089         local xsml=trusted.sml
8090         log "save $xsml on $file"
8091         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8092
8093         local new=$(get_xattr_value $xbig $file)
8094         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8095
8096         log "grow $xsml on $file"
8097         setfattr -n $xsml -v "$value" $file ||
8098                 error "growing $xsml on $file failed"
8099
8100         new=$(get_xattr_value $xbig $file)
8101         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8102         log "$xbig still valid after growing $xsml"
8103
8104         rm -f $file
8105 }
8106
8107 test_102h() { # bug 15777
8108         grow_xattr 1024
8109 }
8110 run_test 102h "grow xattr from inside inode to external block"
8111
8112 test_102ha() {
8113         large_xattr_enabled || skip_env "large_xattr disabled"
8114
8115         grow_xattr $(max_xattr_size)
8116 }
8117 run_test 102ha "grow xattr from inside inode to external inode"
8118
8119 test_102i() { # bug 17038
8120         [ -z "$(which getfattr 2>/dev/null)" ] &&
8121                 skip "could not find getfattr"
8122
8123         touch $DIR/$tfile
8124         ln -s $DIR/$tfile $DIR/${tfile}link
8125         getfattr -n trusted.lov $DIR/$tfile ||
8126                 error "lgetxattr on $DIR/$tfile failed"
8127         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8128                 grep -i "no such attr" ||
8129                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8130         rm -f $DIR/$tfile $DIR/${tfile}link
8131 }
8132 run_test 102i "lgetxattr test on symbolic link ============"
8133
8134 test_102j() {
8135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8136         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8137
8138         XINC=$(have_xattrs_include)
8139         setup_test102 "$RUNAS"
8140         chown $RUNAS_ID $DIR/$tdir
8141         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8142         cd $DIR/$tdir/$tdir
8143         compare_stripe_info1 "$RUNAS"
8144 }
8145 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8146
8147 test_102k() {
8148         [ -z "$(which setfattr 2>/dev/null)" ] &&
8149                 skip "could not find setfattr"
8150
8151         touch $DIR/$tfile
8152         # b22187 just check that does not crash for regular file.
8153         setfattr -n trusted.lov $DIR/$tfile
8154         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8155         local test_kdir=$DIR/$tdir
8156         test_mkdir $test_kdir
8157         local default_size=$($LFS getstripe -S $test_kdir)
8158         local default_count=$($LFS getstripe -c $test_kdir)
8159         local default_offset=$($LFS getstripe -i $test_kdir)
8160         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8161                 error 'dir setstripe failed'
8162         setfattr -n trusted.lov $test_kdir
8163         local stripe_size=$($LFS getstripe -S $test_kdir)
8164         local stripe_count=$($LFS getstripe -c $test_kdir)
8165         local stripe_offset=$($LFS getstripe -i $test_kdir)
8166         [ $stripe_size -eq $default_size ] ||
8167                 error "stripe size $stripe_size != $default_size"
8168         [ $stripe_count -eq $default_count ] ||
8169                 error "stripe count $stripe_count != $default_count"
8170         [ $stripe_offset -eq $default_offset ] ||
8171                 error "stripe offset $stripe_offset != $default_offset"
8172         rm -rf $DIR/$tfile $test_kdir
8173 }
8174 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8175
8176 test_102l() {
8177         [ -z "$(which getfattr 2>/dev/null)" ] &&
8178                 skip "could not find getfattr"
8179
8180         # LU-532 trusted. xattr is invisible to non-root
8181         local testfile=$DIR/$tfile
8182
8183         touch $testfile
8184
8185         echo "listxattr as user..."
8186         chown $RUNAS_ID $testfile
8187         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8188             grep -q "trusted" &&
8189                 error "$testfile trusted xattrs are user visible"
8190
8191         return 0;
8192 }
8193 run_test 102l "listxattr size test =================================="
8194
8195 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8196         local path=$DIR/$tfile
8197         touch $path
8198
8199         listxattr_size_check $path || error "listattr_size_check $path failed"
8200 }
8201 run_test 102m "Ensure listxattr fails on small bufffer ========"
8202
8203 cleanup_test102
8204
8205 getxattr() { # getxattr path name
8206         # Return the base64 encoding of the value of xattr name on path.
8207         local path=$1
8208         local name=$2
8209
8210         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8211         # file: $path
8212         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8213         #
8214         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8215
8216         getfattr --absolute-names --encoding=base64 --name=$name $path |
8217                 awk -F= -v name=$name '$1 == name {
8218                         print substr($0, index($0, "=") + 1);
8219         }'
8220 }
8221
8222 test_102n() { # LU-4101 mdt: protect internal xattrs
8223         [ -z "$(which setfattr 2>/dev/null)" ] &&
8224                 skip "could not find setfattr"
8225         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
8226         then
8227                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8228         fi
8229
8230         local file0=$DIR/$tfile.0
8231         local file1=$DIR/$tfile.1
8232         local xattr0=$TMP/$tfile.0
8233         local xattr1=$TMP/$tfile.1
8234         local namelist="lov lma lmv link fid version som hsm"
8235         local name
8236         local value
8237
8238         rm -rf $file0 $file1 $xattr0 $xattr1
8239         touch $file0 $file1
8240
8241         # Get 'before' xattrs of $file1.
8242         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8243
8244         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
8245                 namelist+=" lfsck_namespace"
8246         for name in $namelist; do
8247                 # Try to copy xattr from $file0 to $file1.
8248                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8249
8250                 setfattr --name=trusted.$name --value="$value" $file1 ||
8251                         error "setxattr 'trusted.$name' failed"
8252
8253                 # Try to set a garbage xattr.
8254                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8255
8256                 if [[ x$name == "xlov" ]]; then
8257                         setfattr --name=trusted.lov --value="$value" $file1 &&
8258                         error "setxattr invalid 'trusted.lov' success"
8259                 else
8260                         setfattr --name=trusted.$name --value="$value" $file1 ||
8261                                 error "setxattr invalid 'trusted.$name' failed"
8262                 fi
8263
8264                 # Try to remove the xattr from $file1. We don't care if this
8265                 # appears to succeed or fail, we just don't want there to be
8266                 # any changes or crashes.
8267                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8268         done
8269
8270         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
8271         then
8272                 name="lfsck_ns"
8273                 # Try to copy xattr from $file0 to $file1.
8274                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8275
8276                 setfattr --name=trusted.$name --value="$value" $file1 ||
8277                         error "setxattr 'trusted.$name' failed"
8278
8279                 # Try to set a garbage xattr.
8280                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8281
8282                 setfattr --name=trusted.$name --value="$value" $file1 ||
8283                         error "setxattr 'trusted.$name' failed"
8284
8285                 # Try to remove the xattr from $file1. We don't care if this
8286                 # appears to succeed or fail, we just don't want there to be
8287                 # any changes or crashes.
8288                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8289         fi
8290
8291         # Get 'after' xattrs of file1.
8292         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8293
8294         if ! diff $xattr0 $xattr1; then
8295                 error "before and after xattrs of '$file1' differ"
8296         fi
8297
8298         rm -rf $file0 $file1 $xattr0 $xattr1
8299
8300         return 0
8301 }
8302 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8303
8304 test_102p() { # LU-4703 setxattr did not check ownership
8305         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
8306                 skip "MDS needs to be at least 2.5.56"
8307
8308         local testfile=$DIR/$tfile
8309
8310         touch $testfile
8311
8312         echo "setfacl as user..."
8313         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8314         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8315
8316         echo "setfattr as user..."
8317         setfacl -m "u:$RUNAS_ID:---" $testfile
8318         $RUNAS setfattr -x system.posix_acl_access $testfile
8319         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8320 }
8321 run_test 102p "check setxattr(2) correctly fails without permission"
8322
8323 test_102q() {
8324         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
8325                 skip "MDS needs to be at least 2.6.92"
8326
8327         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8328 }
8329 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8330
8331 test_102r() {
8332         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
8333                 skip "MDS needs to be at least 2.6.93"
8334
8335         touch $DIR/$tfile || error "touch"
8336         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8337         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8338         rm $DIR/$tfile || error "rm"
8339
8340         #normal directory
8341         mkdir -p $DIR/$tdir || error "mkdir"
8342         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8343         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8344         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8345                 error "$testfile error deleting user.author1"
8346         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8347                 grep "user.$(basename $tdir)" &&
8348                 error "$tdir did not delete user.$(basename $tdir)"
8349         rmdir $DIR/$tdir || error "rmdir"
8350
8351         #striped directory
8352         test_mkdir $DIR/$tdir
8353         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8354         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8355         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8356                 error "$testfile error deleting user.author1"
8357         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8358                 grep "user.$(basename $tdir)" &&
8359                 error "$tdir did not delete user.$(basename $tdir)"
8360         rmdir $DIR/$tdir || error "rm striped dir"
8361 }
8362 run_test 102r "set EAs with empty values"
8363
8364 run_acl_subtest()
8365 {
8366     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
8367     return $?
8368 }
8369
8370 test_103a() {
8371         [ "$UID" != 0 ] && skip "must run as root"
8372         $GSS && skip_env "could not run under gss"
8373         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
8374                 skip_env "must have acl enabled"
8375         [ -z "$(which setfacl 2>/dev/null)" ] &&
8376                 skip_env "could not find setfacl"
8377         remote_mds_nodsh && skip "remote MDS with nodsh"
8378
8379         gpasswd -a daemon bin                           # LU-5641
8380         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
8381
8382         declare -a identity_old
8383
8384         for num in $(seq $MDSCOUNT); do
8385                 switch_identity $num true || identity_old[$num]=$?
8386         done
8387
8388         SAVE_UMASK=$(umask)
8389         umask 0022
8390         mkdir -p $DIR/$tdir
8391         cd $DIR/$tdir
8392
8393         echo "performing cp ..."
8394         run_acl_subtest cp || error "run_acl_subtest cp failed"
8395         echo "performing getfacl-noacl..."
8396         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
8397         echo "performing misc..."
8398         run_acl_subtest misc || error  "misc test failed"
8399         echo "performing permissions..."
8400         run_acl_subtest permissions || error "permissions failed"
8401         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
8402         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
8403              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
8404              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
8405         then
8406                 echo "performing permissions xattr..."
8407                 run_acl_subtest permissions_xattr ||
8408                         error "permissions_xattr failed"
8409         fi
8410         echo "performing setfacl..."
8411         run_acl_subtest setfacl || error  "setfacl test failed"
8412
8413         # inheritance test got from HP
8414         echo "performing inheritance..."
8415         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
8416         chmod +x make-tree || error "chmod +x failed"
8417         run_acl_subtest inheritance || error "inheritance test failed"
8418         rm -f make-tree
8419
8420         echo "LU-974 ignore umask when acl is enabled..."
8421         run_acl_subtest 974 || error "LU-974 umask test failed"
8422         if [ $MDSCOUNT -ge 2 ]; then
8423                 run_acl_subtest 974_remote ||
8424                         error "LU-974 umask test failed under remote dir"
8425         fi
8426
8427         echo "LU-2561 newly created file is same size as directory..."
8428         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
8429                 run_acl_subtest 2561 || error "LU-2561 test failed"
8430         else
8431                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
8432         fi
8433
8434         run_acl_subtest 4924 || error "LU-4924 test failed"
8435
8436         cd $SAVE_PWD
8437         umask $SAVE_UMASK
8438
8439         for num in $(seq $MDSCOUNT); do
8440                 if [ "${identity_old[$num]}" = 1 ]; then
8441                         switch_identity $num false || identity_old[$num]=$?
8442                 fi
8443         done
8444 }
8445 run_test 103a "acl test"
8446
8447 test_103b() {
8448         local U
8449
8450         for U in {0..511}; do
8451                 {
8452                 local O=$(printf "%04o" $U)
8453
8454                 umask $(printf "%04o" $((511 ^ $O)))
8455                 $LFS setstripe -c 1 $DIR/$tfile.s$O
8456                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
8457
8458                 (( $S == ($O & 0666) )) ||
8459                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
8460
8461                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
8462                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
8463                 (( $S == ($O & 0666) )) ||
8464                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
8465
8466                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
8467                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
8468                 (( $S == ($O & 0666) )) ||
8469                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
8470                 rm -f $DIR/$tfile.[smp]$0
8471                 } &
8472         done
8473         wait
8474 }
8475 run_test 103b "umask lfs setstripe"
8476
8477 test_103c() {
8478         mkdir -p $DIR/$tdir
8479         cp -rp $DIR/$tdir $DIR/$tdir.bak
8480
8481         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
8482                 error "$DIR/$tdir shouldn't contain default ACL"
8483         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
8484                 error "$DIR/$tdir.bak shouldn't contain default ACL"
8485         true
8486 }
8487 run_test 103c "'cp -rp' won't set empty acl"
8488
8489 test_104a() {
8490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8491
8492         touch $DIR/$tfile
8493         lfs df || error "lfs df failed"
8494         lfs df -ih || error "lfs df -ih failed"
8495         lfs df -h $DIR || error "lfs df -h $DIR failed"
8496         lfs df -i $DIR || error "lfs df -i $DIR failed"
8497         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
8498         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
8499
8500         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
8501         lctl --device %$OSC deactivate
8502         lfs df || error "lfs df with deactivated OSC failed"
8503         lctl --device %$OSC activate
8504         # wait the osc back to normal
8505         wait_osc_import_state client ost FULL
8506
8507         lfs df || error "lfs df with reactivated OSC failed"
8508         rm -f $DIR/$tfile
8509 }
8510 run_test 104a "lfs df [-ih] [path] test ========================="
8511
8512 test_104b() {
8513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8514         [ $RUNAS_ID -eq $UID ] &&
8515                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8516
8517         chmod 666 /dev/obd
8518         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
8519                         grep "Permission denied" | wc -l)))
8520         if [ $denied_cnt -ne 0 ]; then
8521                 error "lfs check servers test failed"
8522         fi
8523 }
8524 run_test 104b "$RUNAS lfs check servers test ===================="
8525
8526 test_105a() {
8527         # doesn't work on 2.4 kernels
8528         touch $DIR/$tfile
8529         if $(flock_is_enabled); then
8530                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
8531         else
8532                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
8533         fi
8534         rm -f $DIR/$tfile
8535 }
8536 run_test 105a "flock when mounted without -o flock test ========"
8537
8538 test_105b() {
8539         touch $DIR/$tfile
8540         if $(flock_is_enabled); then
8541                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
8542         else
8543                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
8544         fi
8545         rm -f $DIR/$tfile
8546 }
8547 run_test 105b "fcntl when mounted without -o flock test ========"
8548
8549 test_105c() {
8550         touch $DIR/$tfile
8551         if $(flock_is_enabled); then
8552                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
8553         else
8554                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
8555         fi
8556         rm -f $DIR/$tfile
8557 }
8558 run_test 105c "lockf when mounted without -o flock test"
8559
8560 test_105d() { # bug 15924
8561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8562
8563         test_mkdir $DIR/$tdir
8564         flock_is_enabled || skip_env "mount w/o flock enabled"
8565         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
8566         $LCTL set_param fail_loc=0x80000315
8567         flocks_test 2 $DIR/$tdir
8568 }
8569 run_test 105d "flock race (should not freeze) ========"
8570
8571 test_105e() { # bug 22660 && 22040
8572         flock_is_enabled || skip_env "mount w/o flock enabled"
8573
8574         touch $DIR/$tfile
8575         flocks_test 3 $DIR/$tfile
8576 }
8577 run_test 105e "Two conflicting flocks from same process"
8578
8579 test_106() { #bug 10921
8580         test_mkdir $DIR/$tdir
8581         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
8582         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8583 }
8584 run_test 106 "attempt exec of dir followed by chown of that dir"
8585
8586 test_107() {
8587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8588
8589         CDIR=`pwd`
8590         local file=core
8591
8592         cd $DIR
8593         rm -f $file
8594
8595         local save_pattern=$(sysctl -n kernel.core_pattern)
8596         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
8597         sysctl -w kernel.core_pattern=$file
8598         sysctl -w kernel.core_uses_pid=0
8599
8600         ulimit -c unlimited
8601         sleep 60 &
8602         SLEEPPID=$!
8603
8604         sleep 1
8605
8606         kill -s 11 $SLEEPPID
8607         wait $SLEEPPID
8608         if [ -e $file ]; then
8609                 size=`stat -c%s $file`
8610                 [ $size -eq 0 ] && error "Fail to create core file $file"
8611         else
8612                 error "Fail to create core file $file"
8613         fi
8614         rm -f $file
8615         sysctl -w kernel.core_pattern=$save_pattern
8616         sysctl -w kernel.core_uses_pid=$save_uses_pid
8617         cd $CDIR
8618 }
8619 run_test 107 "Coredump on SIG"
8620
8621 test_110() {
8622         test_mkdir $DIR/$tdir
8623         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
8624         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
8625                 error "mkdir with 256 char should fail, but did not"
8626         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
8627                 error "create with 255 char failed"
8628         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
8629                 error "create with 256 char should fail, but did not"
8630
8631         ls -l $DIR/$tdir
8632         rm -rf $DIR/$tdir
8633 }
8634 run_test 110 "filename length checking"
8635
8636 #
8637 # Purpose: To verify dynamic thread (OSS) creation.
8638 #
8639 test_115() {
8640         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8641         remote_ost_nodsh && skip "remote OST with nodsh"
8642
8643         # Lustre does not stop service threads once they are started.
8644         # Reset number of running threads to default.
8645         stopall
8646         setupall
8647
8648         local OSTIO_pre
8649         local save_params="$TMP/sanity-$TESTNAME.parameters"
8650
8651         # Get ll_ost_io count before I/O
8652         OSTIO_pre=$(do_facet ost1 \
8653                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
8654         # Exit if lustre is not running (ll_ost_io not running).
8655         [ -z "$OSTIO_pre" ] && error "no OSS threads"
8656
8657         echo "Starting with $OSTIO_pre threads"
8658         local thread_max=$((OSTIO_pre * 2))
8659         local rpc_in_flight=$((thread_max * 2))
8660         # Number of I/O Process proposed to be started.
8661         local nfiles
8662         local facets=$(get_facets OST)
8663
8664         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
8665         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
8666
8667         # Set in_flight to $rpc_in_flight
8668         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
8669                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
8670         nfiles=${rpc_in_flight}
8671         # Set ost thread_max to $thread_max
8672         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
8673
8674         # 5 Minutes should be sufficient for max number of OSS
8675         # threads(thread_max) to be created.
8676         local timeout=300
8677
8678         # Start I/O.
8679         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
8680         test_mkdir $DIR/$tdir
8681         for i in $(seq $nfiles); do
8682                 local file=$DIR/$tdir/${tfile}-$i
8683                 $LFS setstripe -c -1 -i 0 $file
8684                 ($WTL $file $timeout)&
8685         done
8686
8687         # I/O Started - Wait for thread_started to reach thread_max or report
8688         # error if thread_started is more than thread_max.
8689         echo "Waiting for thread_started to reach thread_max"
8690         local thread_started=0
8691         local end_time=$((SECONDS + timeout))
8692
8693         while [ $SECONDS -le $end_time ] ; do
8694                 echo -n "."
8695                 # Get ost i/o thread_started count.
8696                 thread_started=$(do_facet ost1 \
8697                         "$LCTL get_param \
8698                         ost.OSS.ost_io.threads_started | cut -d= -f2")
8699                 # Break out if thread_started is equal/greater than thread_max
8700                 if [[ $thread_started -ge $thread_max ]]; then
8701                         echo ll_ost_io thread_started $thread_started, \
8702                                 equal/greater than thread_max $thread_max
8703                         break
8704                 fi
8705                 sleep 1
8706         done
8707
8708         # Cleanup - We have the numbers, Kill i/o jobs if running.
8709         jobcount=($(jobs -p))
8710         for i in $(seq 0 $((${#jobcount[@]}-1)))
8711         do
8712                 kill -9 ${jobcount[$i]}
8713                 if [ $? -ne 0 ] ; then
8714                         echo Warning: \
8715                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
8716                 fi
8717         done
8718
8719         # Cleanup files left by WTL binary.
8720         for i in $(seq $nfiles); do
8721                 local file=$DIR/$tdir/${tfile}-$i
8722                 rm -rf $file
8723                 if [ $? -ne 0 ] ; then
8724                         echo "Warning: Failed to delete file $file"
8725                 fi
8726         done
8727
8728         restore_lustre_params <$save_params
8729         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8730
8731         # Error out if no new thread has started or Thread started is greater
8732         # than thread max.
8733         if [[ $thread_started -le $OSTIO_pre ||
8734                         $thread_started -gt $thread_max ]]; then
8735                 error "ll_ost_io: thread_started $thread_started" \
8736                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8737                       "No new thread started or thread started greater " \
8738                       "than thread_max."
8739         fi
8740 }
8741 run_test 115 "verify dynamic thread creation===================="
8742
8743 free_min_max () {
8744         wait_delete_completed
8745         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8746         echo "OST kbytes available: ${AVAIL[@]}"
8747         MAXV=${AVAIL[0]}
8748         MAXI=0
8749         MINV=${AVAIL[0]}
8750         MINI=0
8751         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8752                 #echo OST $i: ${AVAIL[i]}kb
8753                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8754                         MAXV=${AVAIL[i]}
8755                         MAXI=$i
8756                 fi
8757                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8758                         MINV=${AVAIL[i]}
8759                         MINI=$i
8760                 fi
8761         done
8762         echo "Min free space: OST $MINI: $MINV"
8763         echo "Max free space: OST $MAXI: $MAXV"
8764 }
8765
8766 test_116a() { # was previously test_116()
8767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8768         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8769         remote_mds_nodsh && skip "remote MDS with nodsh"
8770
8771         echo -n "Free space priority "
8772         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8773                 head -n1
8774         declare -a AVAIL
8775         free_min_max
8776
8777         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
8778         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
8779         trap simple_cleanup_common EXIT
8780
8781         # Check if we need to generate uneven OSTs
8782         test_mkdir -p $DIR/$tdir/OST${MINI}
8783         local FILL=$((MINV / 4))
8784         local DIFF=$((MAXV - MINV))
8785         local DIFF2=$((DIFF * 100 / MINV))
8786
8787         local threshold=$(do_facet $SINGLEMDS \
8788                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8789         threshold=${threshold%%%}
8790         echo -n "Check for uneven OSTs: "
8791         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8792
8793         if [[ $DIFF2 -gt $threshold ]]; then
8794                 echo "ok"
8795                 echo "Don't need to fill OST$MINI"
8796         else
8797                 # generate uneven OSTs. Write 2% over the QOS threshold value
8798                 echo "no"
8799                 DIFF=$((threshold - DIFF2 + 2))
8800                 DIFF2=$((MINV * DIFF / 100))
8801                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8802                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8803                         error "setstripe failed"
8804                 DIFF=$((DIFF2 / 2048))
8805                 i=0
8806                 while [ $i -lt $DIFF ]; do
8807                         i=$((i + 1))
8808                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8809                                 bs=2M count=1 2>/dev/null
8810                         echo -n .
8811                 done
8812                 echo .
8813                 sync
8814                 sleep_maxage
8815                 free_min_max
8816         fi
8817
8818         DIFF=$((MAXV - MINV))
8819         DIFF2=$((DIFF * 100 / MINV))
8820         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8821         if [ $DIFF2 -gt $threshold ]; then
8822                 echo "ok"
8823         else
8824                 echo "failed - QOS mode won't be used"
8825                 simple_cleanup_common
8826                 skip "QOS imbalance criteria not met"
8827         fi
8828
8829         MINI1=$MINI
8830         MINV1=$MINV
8831         MAXI1=$MAXI
8832         MAXV1=$MAXV
8833
8834         # now fill using QOS
8835         $SETSTRIPE -c 1 $DIR/$tdir
8836         FILL=$((FILL / 200))
8837         if [ $FILL -gt 600 ]; then
8838                 FILL=600
8839         fi
8840         echo "writing $FILL files to QOS-assigned OSTs"
8841         i=0
8842         while [ $i -lt $FILL ]; do
8843                 i=$((i + 1))
8844                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8845                         count=1 2>/dev/null
8846                 echo -n .
8847         done
8848         echo "wrote $i 200k files"
8849         sync
8850         sleep_maxage
8851
8852         echo "Note: free space may not be updated, so measurements might be off"
8853         free_min_max
8854         DIFF2=$((MAXV - MINV))
8855         echo "free space delta: orig $DIFF final $DIFF2"
8856         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8857         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8858         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8859         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8860         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8861         if [[ $DIFF -gt 0 ]]; then
8862                 FILL=$((DIFF2 * 100 / DIFF - 100))
8863                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8864         fi
8865
8866         # Figure out which files were written where
8867         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8868                awk '/'$MINI1': / {print $2; exit}')
8869         echo $UUID
8870         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8871         echo "$MINC files created on smaller OST $MINI1"
8872         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8873                awk '/'$MAXI1': / {print $2; exit}')
8874         echo $UUID
8875         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8876         echo "$MAXC files created on larger OST $MAXI1"
8877         if [[ $MINC -gt 0 ]]; then
8878                 FILL=$((MAXC * 100 / MINC - 100))
8879                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8880         fi
8881         [[ $MAXC -gt $MINC ]] ||
8882                 error_ignore LU-9 "stripe QOS didn't balance free space"
8883         simple_cleanup_common
8884 }
8885 run_test 116a "stripe QOS: free space balance ==================="
8886
8887 test_116b() { # LU-2093
8888         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8889         remote_mds_nodsh && skip "remote MDS with nodsh"
8890
8891 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8892         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8893                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8894         [ -z "$old_rr" ] && skip "no QOS"
8895         do_facet $SINGLEMDS lctl set_param \
8896                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8897         mkdir -p $DIR/$tdir
8898         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8899         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8900         do_facet $SINGLEMDS lctl set_param fail_loc=0
8901         rm -rf $DIR/$tdir
8902         do_facet $SINGLEMDS lctl set_param \
8903                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8904 }
8905 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8906
8907 test_117() # bug 10891
8908 {
8909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8910
8911         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8912         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8913         lctl set_param fail_loc=0x21e
8914         > $DIR/$tfile || error "truncate failed"
8915         lctl set_param fail_loc=0
8916         echo "Truncate succeeded."
8917         rm -f $DIR/$tfile
8918 }
8919 run_test 117 "verify osd extend =========="
8920
8921 NO_SLOW_RESENDCOUNT=4
8922 export OLD_RESENDCOUNT=""
8923 set_resend_count () {
8924         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8925         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8926         lctl set_param -n $PROC_RESENDCOUNT $1
8927         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8928 }
8929
8930 # for reduce test_118* time (b=14842)
8931 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8932
8933 # Reset async IO behavior after error case
8934 reset_async() {
8935         FILE=$DIR/reset_async
8936
8937         # Ensure all OSCs are cleared
8938         $SETSTRIPE -c -1 $FILE
8939         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8940         sync
8941         rm $FILE
8942 }
8943
8944 test_118a() #bug 11710
8945 {
8946         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8947
8948         reset_async
8949
8950         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8951         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8952         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8953
8954         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8955                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8956                 return 1;
8957         fi
8958         rm -f $DIR/$tfile
8959 }
8960 run_test 118a "verify O_SYNC works =========="
8961
8962 test_118b()
8963 {
8964         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8965         remote_ost_nodsh && skip "remote OST with nodsh"
8966
8967         reset_async
8968
8969         #define OBD_FAIL_SRV_ENOENT 0x217
8970         set_nodes_failloc "$(osts_nodes)" 0x217
8971         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8972         RC=$?
8973         set_nodes_failloc "$(osts_nodes)" 0
8974         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8975         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8976                     grep -c writeback)
8977
8978         if [[ $RC -eq 0 ]]; then
8979                 error "Must return error due to dropped pages, rc=$RC"
8980                 return 1;
8981         fi
8982
8983         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8984                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8985                 return 1;
8986         fi
8987
8988         echo "Dirty pages not leaked on ENOENT"
8989
8990         # Due to the above error the OSC will issue all RPCs syncronously
8991         # until a subsequent RPC completes successfully without error.
8992         $MULTIOP $DIR/$tfile Ow4096yc
8993         rm -f $DIR/$tfile
8994
8995         return 0
8996 }
8997 run_test 118b "Reclaim dirty pages on fatal error =========="
8998
8999 test_118c()
9000 {
9001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9002
9003         # for 118c, restore the original resend count, LU-1940
9004         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9005                                 set_resend_count $OLD_RESENDCOUNT
9006         remote_ost_nodsh && skip "remote OST with nodsh"
9007
9008         reset_async
9009
9010         #define OBD_FAIL_OST_EROFS               0x216
9011         set_nodes_failloc "$(osts_nodes)" 0x216
9012
9013         # multiop should block due to fsync until pages are written
9014         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9015         MULTIPID=$!
9016         sleep 1
9017
9018         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9019                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9020         fi
9021
9022         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9023                     grep -c writeback)
9024         if [[ $WRITEBACK -eq 0 ]]; then
9025                 error "No page in writeback, writeback=$WRITEBACK"
9026         fi
9027
9028         set_nodes_failloc "$(osts_nodes)" 0
9029         wait $MULTIPID
9030         RC=$?
9031         if [[ $RC -ne 0 ]]; then
9032                 error "Multiop fsync failed, rc=$RC"
9033         fi
9034
9035         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9036         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9037                     grep -c writeback)
9038         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9039                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9040         fi
9041
9042         rm -f $DIR/$tfile
9043         echo "Dirty pages flushed via fsync on EROFS"
9044         return 0
9045 }
9046 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9047
9048 # continue to use small resend count to reduce test_118* time (b=14842)
9049 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9050
9051 test_118d()
9052 {
9053         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9054         remote_ost_nodsh && skip "remote OST with nodsh"
9055
9056         reset_async
9057
9058         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9059         set_nodes_failloc "$(osts_nodes)" 0x214
9060         # multiop should block due to fsync until pages are written
9061         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9062         MULTIPID=$!
9063         sleep 1
9064
9065         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9066                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9067         fi
9068
9069         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9070                     grep -c writeback)
9071         if [[ $WRITEBACK -eq 0 ]]; then
9072                 error "No page in writeback, writeback=$WRITEBACK"
9073         fi
9074
9075         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9076         set_nodes_failloc "$(osts_nodes)" 0
9077
9078         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9079         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9080                     grep -c writeback)
9081         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9082                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9083         fi
9084
9085         rm -f $DIR/$tfile
9086         echo "Dirty pages gaurenteed flushed via fsync"
9087         return 0
9088 }
9089 run_test 118d "Fsync validation inject a delay of the bulk =========="
9090
9091 test_118f() {
9092         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9093
9094         reset_async
9095
9096         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9097         lctl set_param fail_loc=0x8000040a
9098
9099         # Should simulate EINVAL error which is fatal
9100         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9101         RC=$?
9102         if [[ $RC -eq 0 ]]; then
9103                 error "Must return error due to dropped pages, rc=$RC"
9104         fi
9105
9106         lctl set_param fail_loc=0x0
9107
9108         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9109         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9110         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9111                     grep -c writeback)
9112         if [[ $LOCKED -ne 0 ]]; then
9113                 error "Locked pages remain in cache, locked=$LOCKED"
9114         fi
9115
9116         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9117                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9118         fi
9119
9120         rm -f $DIR/$tfile
9121         echo "No pages locked after fsync"
9122
9123         reset_async
9124         return 0
9125 }
9126 run_test 118f "Simulate unrecoverable OSC side error =========="
9127
9128 test_118g() {
9129         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9130
9131         reset_async
9132
9133         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9134         lctl set_param fail_loc=0x406
9135
9136         # simulate local -ENOMEM
9137         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9138         RC=$?
9139
9140         lctl set_param fail_loc=0
9141         if [[ $RC -eq 0 ]]; then
9142                 error "Must return error due to dropped pages, rc=$RC"
9143         fi
9144
9145         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9146         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9147         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9148                         grep -c writeback)
9149         if [[ $LOCKED -ne 0 ]]; then
9150                 error "Locked pages remain in cache, locked=$LOCKED"
9151         fi
9152
9153         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9154                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9155         fi
9156
9157         rm -f $DIR/$tfile
9158         echo "No pages locked after fsync"
9159
9160         reset_async
9161         return 0
9162 }
9163 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9164
9165 test_118h() {
9166         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9167         remote_ost_nodsh && skip "remote OST with nodsh"
9168
9169         reset_async
9170
9171         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9172         set_nodes_failloc "$(osts_nodes)" 0x20e
9173         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9174         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9175         RC=$?
9176
9177         set_nodes_failloc "$(osts_nodes)" 0
9178         if [[ $RC -eq 0 ]]; then
9179                 error "Must return error due to dropped pages, rc=$RC"
9180         fi
9181
9182         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9183         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9184         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9185                     grep -c writeback)
9186         if [[ $LOCKED -ne 0 ]]; then
9187                 error "Locked pages remain in cache, locked=$LOCKED"
9188         fi
9189
9190         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9191                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9192         fi
9193
9194         rm -f $DIR/$tfile
9195         echo "No pages locked after fsync"
9196
9197         return 0
9198 }
9199 run_test 118h "Verify timeout in handling recoverables errors  =========="
9200
9201 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9202
9203 test_118i() {
9204         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9205         remote_ost_nodsh && skip "remote OST with nodsh"
9206
9207         reset_async
9208
9209         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9210         set_nodes_failloc "$(osts_nodes)" 0x20e
9211
9212         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9213         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9214         PID=$!
9215         sleep 5
9216         set_nodes_failloc "$(osts_nodes)" 0
9217
9218         wait $PID
9219         RC=$?
9220         if [[ $RC -ne 0 ]]; then
9221                 error "got error, but should be not, rc=$RC"
9222         fi
9223
9224         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9225         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9226         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9227         if [[ $LOCKED -ne 0 ]]; then
9228                 error "Locked pages remain in cache, locked=$LOCKED"
9229         fi
9230
9231         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9232                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9233         fi
9234
9235         rm -f $DIR/$tfile
9236         echo "No pages locked after fsync"
9237
9238         return 0
9239 }
9240 run_test 118i "Fix error before timeout in recoverable error  =========="
9241
9242 [ "$SLOW" = "no" ] && set_resend_count 4
9243
9244 test_118j() {
9245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9246         remote_ost_nodsh && skip "remote OST with nodsh"
9247
9248         reset_async
9249
9250         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9251         set_nodes_failloc "$(osts_nodes)" 0x220
9252
9253         # return -EIO from OST
9254         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9255         RC=$?
9256         set_nodes_failloc "$(osts_nodes)" 0x0
9257         if [[ $RC -eq 0 ]]; then
9258                 error "Must return error due to dropped pages, rc=$RC"
9259         fi
9260
9261         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9262         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9263         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9264         if [[ $LOCKED -ne 0 ]]; then
9265                 error "Locked pages remain in cache, locked=$LOCKED"
9266         fi
9267
9268         # in recoverable error on OST we want resend and stay until it finished
9269         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9270                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9271         fi
9272
9273         rm -f $DIR/$tfile
9274         echo "No pages locked after fsync"
9275
9276         return 0
9277 }
9278 run_test 118j "Simulate unrecoverable OST side error =========="
9279
9280 test_118k()
9281 {
9282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9283         remote_ost_nodsh && skip "remote OSTs with nodsh"
9284
9285         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9286         set_nodes_failloc "$(osts_nodes)" 0x20e
9287         test_mkdir $DIR/$tdir
9288
9289         for ((i=0;i<10;i++)); do
9290                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9291                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9292                 SLEEPPID=$!
9293                 sleep 0.500s
9294                 kill $SLEEPPID
9295                 wait $SLEEPPID
9296         done
9297
9298         set_nodes_failloc "$(osts_nodes)" 0
9299         rm -rf $DIR/$tdir
9300 }
9301 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9302
9303 test_118l() # LU-646
9304 {
9305         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9306
9307         test_mkdir $DIR/$tdir
9308         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
9309         rm -rf $DIR/$tdir
9310 }
9311 run_test 118l "fsync dir"
9312
9313 test_118m() # LU-3066
9314 {
9315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9316
9317         test_mkdir $DIR/$tdir
9318         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
9319         rm -rf $DIR/$tdir
9320 }
9321 run_test 118m "fdatasync dir ========="
9322
9323 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9324
9325 test_118n()
9326 {
9327         local begin
9328         local end
9329
9330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9331         remote_ost_nodsh && skip "remote OSTs with nodsh"
9332
9333         # Sleep to avoid a cached response.
9334         #define OBD_STATFS_CACHE_SECONDS 1
9335         sleep 2
9336
9337         # Inject a 10 second delay in the OST_STATFS handler.
9338         #define OBD_FAIL_OST_STATFS_DELAY 0x242
9339         set_nodes_failloc "$(osts_nodes)" 0x242
9340
9341         begin=$SECONDS
9342         stat --file-system $MOUNT > /dev/null
9343         end=$SECONDS
9344
9345         set_nodes_failloc "$(osts_nodes)" 0
9346
9347         if ((end - begin > 20)); then
9348             error "statfs took $((end - begin)) seconds, expected 10"
9349         fi
9350 }
9351 run_test 118n "statfs() sends OST_STATFS requests in parallel"
9352
9353 test_119a() # bug 11737
9354 {
9355         BSIZE=$((512 * 1024))
9356         directio write $DIR/$tfile 0 1 $BSIZE
9357         # We ask to read two blocks, which is more than a file size.
9358         # directio will indicate an error when requested and actual
9359         # sizes aren't equeal (a normal situation in this case) and
9360         # print actual read amount.
9361         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
9362         if [ "$NOB" != "$BSIZE" ]; then
9363                 error "read $NOB bytes instead of $BSIZE"
9364         fi
9365         rm -f $DIR/$tfile
9366 }
9367 run_test 119a "Short directIO read must return actual read amount"
9368
9369 test_119b() # bug 11737
9370 {
9371         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9372
9373         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
9374         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
9375         sync
9376         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
9377                 error "direct read failed"
9378         rm -f $DIR/$tfile
9379 }
9380 run_test 119b "Sparse directIO read must return actual read amount"
9381
9382 test_119c() # bug 13099
9383 {
9384         BSIZE=1048576
9385         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
9386         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
9387         rm -f $DIR/$tfile
9388 }
9389 run_test 119c "Testing for direct read hitting hole"
9390
9391 test_119d() # bug 15950
9392 {
9393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9394
9395         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
9396         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
9397         BSIZE=1048576
9398         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
9399         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
9400         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
9401         lctl set_param fail_loc=0x40d
9402         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
9403         pid_dio=$!
9404         sleep 1
9405         cat $DIR/$tfile > /dev/null &
9406         lctl set_param fail_loc=0
9407         pid_reads=$!
9408         wait $pid_dio
9409         log "the DIO writes have completed, now wait for the reads (should not block very long)"
9410         sleep 2
9411         [ -n "`ps h -p $pid_reads -o comm`" ] && \
9412         error "the read rpcs have not completed in 2s"
9413         rm -f $DIR/$tfile
9414         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
9415 }
9416 run_test 119d "The DIO path should try to send a new rpc once one is completed"
9417
9418 test_120a() {
9419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9420         remote_mds_nodsh && skip "remote MDS with nodsh"
9421         test_mkdir $DIR/$tdir
9422         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9423                 skip_env "no early lock cancel on server"
9424
9425         lru_resize_disable mdc
9426         lru_resize_disable osc
9427         cancel_lru_locks mdc
9428         # asynchronous object destroy at MDT could cause bl ast to client
9429         cancel_lru_locks osc
9430
9431         stat $DIR/$tdir > /dev/null
9432         can1=$(do_facet $SINGLEMDS \
9433                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9434                awk '/ldlm_cancel/ {print $2}')
9435         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9436                awk '/ldlm_bl_callback/ {print $2}')
9437         test_mkdir -c1 $DIR/$tdir/d1
9438         can2=$(do_facet $SINGLEMDS \
9439                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9440                awk '/ldlm_cancel/ {print $2}')
9441         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9442                awk '/ldlm_bl_callback/ {print $2}')
9443         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9444         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9445         lru_resize_enable mdc
9446         lru_resize_enable osc
9447 }
9448 run_test 120a "Early Lock Cancel: mkdir test"
9449
9450 test_120b() {
9451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9452         remote_mds_nodsh && skip "remote MDS with nodsh"
9453         test_mkdir $DIR/$tdir
9454         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9455                 skip_env "no early lock cancel on server"
9456
9457         lru_resize_disable mdc
9458         lru_resize_disable osc
9459         cancel_lru_locks mdc
9460         stat $DIR/$tdir > /dev/null
9461         can1=$(do_facet $SINGLEMDS \
9462                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9463                awk '/ldlm_cancel/ {print $2}')
9464         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9465                awk '/ldlm_bl_callback/ {print $2}')
9466         touch $DIR/$tdir/f1
9467         can2=$(do_facet $SINGLEMDS \
9468                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9469                awk '/ldlm_cancel/ {print $2}')
9470         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9471                awk '/ldlm_bl_callback/ {print $2}')
9472         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9473         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9474         lru_resize_enable mdc
9475         lru_resize_enable osc
9476 }
9477 run_test 120b "Early Lock Cancel: create test"
9478
9479 test_120c() {
9480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9481         remote_mds_nodsh && skip "remote MDS with nodsh"
9482         test_mkdir -c1 $DIR/$tdir
9483         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9484                 skip "no early lock cancel on server"
9485
9486         lru_resize_disable mdc
9487         lru_resize_disable osc
9488         test_mkdir -c1 $DIR/$tdir/d1
9489         test_mkdir -c1 $DIR/$tdir/d2
9490         touch $DIR/$tdir/d1/f1
9491         cancel_lru_locks mdc
9492         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
9493         can1=$(do_facet $SINGLEMDS \
9494                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9495                awk '/ldlm_cancel/ {print $2}')
9496         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9497                awk '/ldlm_bl_callback/ {print $2}')
9498         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9499         can2=$(do_facet $SINGLEMDS \
9500                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9501                awk '/ldlm_cancel/ {print $2}')
9502         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9503                awk '/ldlm_bl_callback/ {print $2}')
9504         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9505         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9506         lru_resize_enable mdc
9507         lru_resize_enable osc
9508 }
9509 run_test 120c "Early Lock Cancel: link test"
9510
9511 test_120d() {
9512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9513         remote_mds_nodsh && skip "remote MDS with nodsh"
9514         test_mkdir -c1 $DIR/$tdir
9515         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9516                 skip_env "no early lock cancel on server"
9517
9518         lru_resize_disable mdc
9519         lru_resize_disable osc
9520         touch $DIR/$tdir
9521         cancel_lru_locks mdc
9522         stat $DIR/$tdir > /dev/null
9523         can1=$(do_facet $SINGLEMDS \
9524                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9525                awk '/ldlm_cancel/ {print $2}')
9526         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9527                awk '/ldlm_bl_callback/ {print $2}')
9528         chmod a+x $DIR/$tdir
9529         can2=$(do_facet $SINGLEMDS \
9530                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9531                awk '/ldlm_cancel/ {print $2}')
9532         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9533                awk '/ldlm_bl_callback/ {print $2}')
9534         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9535         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9536         lru_resize_enable mdc
9537         lru_resize_enable osc
9538 }
9539 run_test 120d "Early Lock Cancel: setattr test"
9540
9541 test_120e() {
9542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9543         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9544                 skip_env "no early lock cancel on server"
9545         remote_mds_nodsh && skip "remote MDS with nodsh"
9546
9547         local dlmtrace_set=false
9548
9549         test_mkdir -c1 $DIR/$tdir
9550         lru_resize_disable mdc
9551         lru_resize_disable osc
9552         ! $LCTL get_param debug | grep -q dlmtrace &&
9553                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
9554         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
9555         cancel_lru_locks mdc
9556         cancel_lru_locks osc
9557         dd if=$DIR/$tdir/f1 of=/dev/null
9558         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
9559         # XXX client can not do early lock cancel of OST lock
9560         # during unlink (LU-4206), so cancel osc lock now.
9561         sleep 2
9562         cancel_lru_locks osc
9563         can1=$(do_facet $SINGLEMDS \
9564                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9565                awk '/ldlm_cancel/ {print $2}')
9566         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9567                awk '/ldlm_bl_callback/ {print $2}')
9568         unlink $DIR/$tdir/f1
9569         sleep 5
9570         can2=$(do_facet $SINGLEMDS \
9571                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9572                awk '/ldlm_cancel/ {print $2}')
9573         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9574                awk '/ldlm_bl_callback/ {print $2}')
9575         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
9576                 $LCTL dk $TMP/cancel.debug.txt
9577         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
9578                 $LCTL dk $TMP/blocking.debug.txt
9579         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
9580         lru_resize_enable mdc
9581         lru_resize_enable osc
9582 }
9583 run_test 120e "Early Lock Cancel: unlink test"
9584
9585 test_120f() {
9586         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9587         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9588                 skip_env "no early lock cancel on server"
9589         remote_mds_nodsh && skip "remote MDS with nodsh"
9590
9591         test_mkdir -c1 $DIR/$tdir
9592         lru_resize_disable mdc
9593         lru_resize_disable osc
9594         test_mkdir -c1 $DIR/$tdir/d1
9595         test_mkdir -c1 $DIR/$tdir/d2
9596         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
9597         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
9598         cancel_lru_locks mdc
9599         cancel_lru_locks osc
9600         dd if=$DIR/$tdir/d1/f1 of=/dev/null
9601         dd if=$DIR/$tdir/d2/f2 of=/dev/null
9602         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
9603         # XXX client can not do early lock cancel of OST lock
9604         # during rename (LU-4206), so cancel osc lock now.
9605         sleep 2
9606         cancel_lru_locks osc
9607         can1=$(do_facet $SINGLEMDS \
9608                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9609                awk '/ldlm_cancel/ {print $2}')
9610         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9611                awk '/ldlm_bl_callback/ {print $2}')
9612         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9613         sleep 5
9614         can2=$(do_facet $SINGLEMDS \
9615                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9616                awk '/ldlm_cancel/ {print $2}')
9617         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9618                awk '/ldlm_bl_callback/ {print $2}')
9619         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9620         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9621         lru_resize_enable mdc
9622         lru_resize_enable osc
9623 }
9624 run_test 120f "Early Lock Cancel: rename test"
9625
9626 test_120g() {
9627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9628         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9629                 skip_env "no early lock cancel on server"
9630         remote_mds_nodsh && skip "remote MDS with nodsh"
9631
9632         lru_resize_disable mdc
9633         lru_resize_disable osc
9634         count=10000
9635         echo create $count files
9636         test_mkdir $DIR/$tdir
9637         cancel_lru_locks mdc
9638         cancel_lru_locks osc
9639         t0=$(date +%s)
9640
9641         can0=$(do_facet $SINGLEMDS \
9642                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9643                awk '/ldlm_cancel/ {print $2}')
9644         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9645                awk '/ldlm_bl_callback/ {print $2}')
9646         createmany -o $DIR/$tdir/f $count
9647         sync
9648         can1=$(do_facet $SINGLEMDS \
9649                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9650                awk '/ldlm_cancel/ {print $2}')
9651         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9652                awk '/ldlm_bl_callback/ {print $2}')
9653         t1=$(date +%s)
9654         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
9655         echo rm $count files
9656         rm -r $DIR/$tdir
9657         sync
9658         can2=$(do_facet $SINGLEMDS \
9659                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9660                awk '/ldlm_cancel/ {print $2}')
9661         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9662                awk '/ldlm_bl_callback/ {print $2}')
9663         t2=$(date +%s)
9664         echo total: $count removes in $((t2-t1))
9665         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
9666         sleep 2
9667         # wait for commitment of removal
9668         lru_resize_enable mdc
9669         lru_resize_enable osc
9670 }
9671 run_test 120g "Early Lock Cancel: performance test"
9672
9673 test_121() { #bug #10589
9674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9675
9676         rm -rf $DIR/$tfile
9677         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
9678 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
9679         lctl set_param fail_loc=0x310
9680         cancel_lru_locks osc > /dev/null
9681         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
9682         lctl set_param fail_loc=0
9683         [[ $reads -eq $writes ]] ||
9684                 error "read $reads blocks, must be $writes blocks"
9685 }
9686 run_test 121 "read cancel race ========="
9687
9688 test_123a() { # was test 123, statahead(bug 11401)
9689         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9690
9691         SLOWOK=0
9692         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
9693                 log "testing UP system. Performance may be lower than expected."
9694                 SLOWOK=1
9695         fi
9696
9697         rm -rf $DIR/$tdir
9698         test_mkdir $DIR/$tdir
9699         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
9700         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
9701         MULT=10
9702         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
9703                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
9704
9705                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9706                 lctl set_param -n llite.*.statahead_max 0
9707                 lctl get_param llite.*.statahead_max
9708                 cancel_lru_locks mdc
9709                 cancel_lru_locks osc
9710                 stime=`date +%s`
9711                 time ls -l $DIR/$tdir | wc -l
9712                 etime=`date +%s`
9713                 delta=$((etime - stime))
9714                 log "ls $i files without statahead: $delta sec"
9715                 lctl set_param llite.*.statahead_max=$max
9716
9717                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9718                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
9719                 cancel_lru_locks mdc
9720                 cancel_lru_locks osc
9721                 stime=`date +%s`
9722                 time ls -l $DIR/$tdir | wc -l
9723                 etime=`date +%s`
9724                 delta_sa=$((etime - stime))
9725                 log "ls $i files with statahead: $delta_sa sec"
9726                 lctl get_param -n llite.*.statahead_stats
9727                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9728
9729                 [[ $swrong -lt $ewrong ]] &&
9730                         log "statahead was stopped, maybe too many locks held!"
9731                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
9732
9733                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9734                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9735                     lctl set_param -n llite.*.statahead_max 0
9736                     lctl get_param llite.*.statahead_max
9737                     cancel_lru_locks mdc
9738                     cancel_lru_locks osc
9739                     stime=`date +%s`
9740                     time ls -l $DIR/$tdir | wc -l
9741                     etime=`date +%s`
9742                     delta=$((etime - stime))
9743                     log "ls $i files again without statahead: $delta sec"
9744                     lctl set_param llite.*.statahead_max=$max
9745                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9746                         if [  $SLOWOK -eq 0 ]; then
9747                                 error "ls $i files is slower with statahead!"
9748                         else
9749                                 log "ls $i files is slower with statahead!"
9750                         fi
9751                         break
9752                     fi
9753                 fi
9754
9755                 [ $delta -gt 20 ] && break
9756                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9757                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9758         done
9759         log "ls done"
9760
9761         stime=`date +%s`
9762         rm -r $DIR/$tdir
9763         sync
9764         etime=`date +%s`
9765         delta=$((etime - stime))
9766         log "rm -r $DIR/$tdir/: $delta seconds"
9767         log "rm done"
9768         lctl get_param -n llite.*.statahead_stats
9769 }
9770 run_test 123a "verify statahead work"
9771
9772 test_123b () { # statahead(bug 15027)
9773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9774
9775         test_mkdir $DIR/$tdir
9776         createmany -o $DIR/$tdir/$tfile-%d 1000
9777
9778         cancel_lru_locks mdc
9779         cancel_lru_locks osc
9780
9781 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9782         lctl set_param fail_loc=0x80000803
9783         ls -lR $DIR/$tdir > /dev/null
9784         log "ls done"
9785         lctl set_param fail_loc=0x0
9786         lctl get_param -n llite.*.statahead_stats
9787         rm -r $DIR/$tdir
9788         sync
9789
9790 }
9791 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9792
9793 test_124a() {
9794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9795         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9796                 skip_env "no lru resize on server"
9797
9798         local NR=2000
9799
9800         test_mkdir $DIR/$tdir
9801
9802         log "create $NR files at $DIR/$tdir"
9803         createmany -o $DIR/$tdir/f $NR ||
9804                 error "failed to create $NR files in $DIR/$tdir"
9805
9806         cancel_lru_locks mdc
9807         ls -l $DIR/$tdir > /dev/null
9808
9809         local NSDIR=""
9810         local LRU_SIZE=0
9811         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9812                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9813                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9814                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9815                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9816                         log "NSDIR=$NSDIR"
9817                         log "NS=$(basename $NSDIR)"
9818                         break
9819                 fi
9820         done
9821
9822         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9823                 skip "Not enough cached locks created!"
9824         fi
9825         log "LRU=$LRU_SIZE"
9826
9827         local SLEEP=30
9828
9829         # We know that lru resize allows one client to hold $LIMIT locks
9830         # for 10h. After that locks begin to be killed by client.
9831         local MAX_HRS=10
9832         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9833         log "LIMIT=$LIMIT"
9834         if [ $LIMIT -lt $LRU_SIZE ]; then
9835                 skip "Limit is too small $LIMIT"
9836         fi
9837
9838         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9839         # killing locks. Some time was spent for creating locks. This means
9840         # that up to the moment of sleep finish we must have killed some of
9841         # them (10-100 locks). This depends on how fast ther were created.
9842         # Many of them were touched in almost the same moment and thus will
9843         # be killed in groups.
9844         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9845
9846         # Use $LRU_SIZE_B here to take into account real number of locks
9847         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9848         local LRU_SIZE_B=$LRU_SIZE
9849         log "LVF=$LVF"
9850         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9851         log "OLD_LVF=$OLD_LVF"
9852         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9853
9854         # Let's make sure that we really have some margin. Client checks
9855         # cached locks every 10 sec.
9856         SLEEP=$((SLEEP+20))
9857         log "Sleep ${SLEEP} sec"
9858         local SEC=0
9859         while ((SEC<$SLEEP)); do
9860                 echo -n "..."
9861                 sleep 5
9862                 SEC=$((SEC+5))
9863                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9864                 echo -n "$LRU_SIZE"
9865         done
9866         echo ""
9867         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9868         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9869
9870         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9871                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9872                 unlinkmany $DIR/$tdir/f $NR
9873                 return
9874         }
9875
9876         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9877         log "unlink $NR files at $DIR/$tdir"
9878         unlinkmany $DIR/$tdir/f $NR
9879 }
9880 run_test 124a "lru resize ======================================="
9881
9882 get_max_pool_limit()
9883 {
9884         local limit=$($LCTL get_param \
9885                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9886         local max=0
9887         for l in $limit; do
9888                 if [[ $l -gt $max ]]; then
9889                         max=$l
9890                 fi
9891         done
9892         echo $max
9893 }
9894
9895 test_124b() {
9896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9897         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9898                 skip_env "no lru resize on server"
9899
9900         LIMIT=$(get_max_pool_limit)
9901
9902         NR=$(($(default_lru_size)*20))
9903         if [[ $NR -gt $LIMIT ]]; then
9904                 log "Limit lock number by $LIMIT locks"
9905                 NR=$LIMIT
9906         fi
9907
9908         IFree=$(mdsrate_inodes_available)
9909         if [ $IFree -lt $NR ]; then
9910                 log "Limit lock number by $IFree inodes"
9911                 NR=$IFree
9912         fi
9913
9914         lru_resize_disable mdc
9915         test_mkdir -p $DIR/$tdir/disable_lru_resize
9916
9917         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9918         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9919         cancel_lru_locks mdc
9920         stime=`date +%s`
9921         PID=""
9922         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9923         PID="$PID $!"
9924         sleep 2
9925         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9926         PID="$PID $!"
9927         sleep 2
9928         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9929         PID="$PID $!"
9930         wait $PID
9931         etime=`date +%s`
9932         nolruresize_delta=$((etime-stime))
9933         log "ls -la time: $nolruresize_delta seconds"
9934         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9935         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9936
9937         lru_resize_enable mdc
9938         test_mkdir -p $DIR/$tdir/enable_lru_resize
9939
9940         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9941         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9942         cancel_lru_locks mdc
9943         stime=`date +%s`
9944         PID=""
9945         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9946         PID="$PID $!"
9947         sleep 2
9948         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9949         PID="$PID $!"
9950         sleep 2
9951         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9952         PID="$PID $!"
9953         wait $PID
9954         etime=`date +%s`
9955         lruresize_delta=$((etime-stime))
9956         log "ls -la time: $lruresize_delta seconds"
9957         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9958
9959         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9960                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9961         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9962                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9963         else
9964                 log "lru resize performs the same with no lru resize"
9965         fi
9966         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9967 }
9968 run_test 124b "lru resize (performance test) ======================="
9969
9970 test_124c() {
9971         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9972         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9973                 skip_env "no lru resize on server"
9974
9975         # cache ununsed locks on client
9976         local nr=100
9977         cancel_lru_locks mdc
9978         test_mkdir $DIR/$tdir
9979         createmany -o $DIR/$tdir/f $nr ||
9980                 error "failed to create $nr files in $DIR/$tdir"
9981         ls -l $DIR/$tdir > /dev/null
9982
9983         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9984         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9985         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9986         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9987         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9988
9989         # set lru_max_age to 1 sec
9990         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9991         echo "sleep $((recalc_p * 2)) seconds..."
9992         sleep $((recalc_p * 2))
9993
9994         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9995         # restore lru_max_age
9996         $LCTL set_param -n $nsdir.lru_max_age $max_age
9997         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9998         unlinkmany $DIR/$tdir/f $nr
9999 }
10000 run_test 124c "LRUR cancel very aged locks"
10001
10002 test_125() { # 13358
10003         $LCTL get_param -n llite.*.client_type | grep -q local ||
10004                 skip "must run as local client"
10005         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10006                 skip_env "must have acl enabled"
10007         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10008
10009         test_mkdir $DIR/$tdir
10010         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10011         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10012         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10013 }
10014 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10015
10016 test_126() { # bug 12829/13455
10017         $GSS && skip_env "must run as gss disabled"
10018         $LCTL get_param -n llite.*.client_type | grep -q local ||
10019                 skip "must run as local client"
10020         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10021
10022         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10023         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10024         rm -f $DIR/$tfile
10025         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10026 }
10027 run_test 126 "check that the fsgid provided by the client is taken into account"
10028
10029 test_127a() { # bug 15521
10030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10031
10032         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10033         $LCTL set_param osc.*.stats=0
10034         FSIZE=$((2048 * 1024))
10035         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10036         cancel_lru_locks osc
10037         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10038
10039         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10040         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10041                 echo "got $COUNT $NAME"
10042                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10043                 eval $NAME=$COUNT || error "Wrong proc format"
10044
10045                 case $NAME in
10046                         read_bytes|write_bytes)
10047                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10048                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10049                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10050                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10051                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10052                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10053                                 error "sumsquare is too small: $SUMSQ"
10054                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10055                                 error "sumsquare is too big: $SUMSQ"
10056                         ;;
10057                         *) ;;
10058                 esac
10059         done < $DIR/${tfile}.tmp
10060
10061         #check that we actually got some stats
10062         [ "$read_bytes" ] || error "Missing read_bytes stats"
10063         [ "$write_bytes" ] || error "Missing write_bytes stats"
10064         [ "$read_bytes" != 0 ] || error "no read done"
10065         [ "$write_bytes" != 0 ] || error "no write done"
10066 }
10067 run_test 127a "verify the client stats are sane"
10068
10069 test_127b() { # bug LU-333
10070         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10071
10072         $LCTL set_param llite.*.stats=0
10073         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
10074
10075         # perform 2 reads and writes so MAX is different from SUM.
10076         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10077         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10078         cancel_lru_locks osc
10079         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
10080         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
10081
10082         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
10083         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10084                 echo "got $COUNT $NAME"
10085                 eval $NAME=$COUNT || error "Wrong proc format"
10086
10087         case $NAME in
10088                 read_bytes)
10089                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
10090                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
10091                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
10092                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
10093                         ;;
10094                 write_bytes)
10095                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
10096                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
10097                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
10098                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
10099                         ;;
10100                         *) ;;
10101                 esac
10102         done < $TMP/${tfile}.tmp
10103
10104         #check that we actually got some stats
10105         [ "$read_bytes" ] || error "Missing read_bytes stats"
10106         [ "$write_bytes" ] || error "Missing write_bytes stats"
10107         [ "$read_bytes" != 0 ] || error "no read done"
10108         [ "$write_bytes" != 0 ] || error "no write done"
10109
10110         rm -f $TMP/${tfile}.tmp
10111 }
10112 run_test 127b "verify the llite client stats are sane"
10113
10114 test_128() { # bug 15212
10115         touch $DIR/$tfile
10116         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10117                 find $DIR/$tfile
10118                 find $DIR/$tfile
10119         EOF
10120
10121         result=$(grep error $TMP/$tfile.log)
10122         rm -f $DIR/$tfile $TMP/$tfile.log
10123         [ -z "$result" ] ||
10124                 error "consecutive find's under interactive lfs failed"
10125 }
10126 run_test 128 "interactive lfs for 2 consecutive find's"
10127
10128 set_dir_limits () {
10129         local mntdev
10130         local canondev
10131         local node
10132
10133         local ldproc=/proc/fs/ldiskfs
10134         local facets=$(get_facets MDS)
10135
10136         for facet in ${facets//,/ }; do
10137                 canondev=$(ldiskfs_canon \
10138                            *.$(convert_facet2label $facet).mntdev $facet)
10139                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10140                         ldproc=/sys/fs/ldiskfs
10141                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10142                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10143         done
10144 }
10145
10146 check_mds_dmesg() {
10147         local facets=$(get_facets MDS)
10148         for facet in ${facets//,/ }; do
10149                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10150         done
10151         return 1
10152 }
10153
10154 test_129() {
10155         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10156         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
10157                 skip "Need MDS version with at least 2.5.56"
10158         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
10159                 skip_env "ldiskfs only test"
10160         fi
10161         remote_mds_nodsh && skip "remote MDS with nodsh"
10162
10163         local ENOSPC=28
10164         local EFBIG=27
10165         local has_warning=false
10166
10167         rm -rf $DIR/$tdir
10168         mkdir -p $DIR/$tdir
10169
10170         # block size of mds1
10171         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
10172         set_dir_limits $maxsize $maxsize
10173         local dirsize=$(stat -c%s "$DIR/$tdir")
10174         local nfiles=0
10175         while [[ $dirsize -le $maxsize ]]; do
10176                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
10177                 rc=$?
10178                 if ! $has_warning; then
10179                         check_mds_dmesg '"is approaching"' && has_warning=true
10180                 fi
10181                 # check two errors:
10182                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
10183                 # EFBIG for previous versions included in ldiskfs series
10184                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
10185                         set_dir_limits 0 0
10186                         echo "return code $rc received as expected"
10187
10188                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
10189                                 error_exit "create failed w/o dir size limit"
10190
10191                         check_mds_dmesg '"has reached"' ||
10192                                 error_exit "reached message should be output"
10193
10194                         [ $has_warning = "false" ] &&
10195                                 error_exit "warning message should be output"
10196
10197                         dirsize=$(stat -c%s "$DIR/$tdir")
10198
10199                         [[ $dirsize -ge $maxsize ]] && return 0
10200                         error_exit "current dir size $dirsize, " \
10201                                    "previous limit $maxsize"
10202                 elif [ $rc -ne 0 ]; then
10203                         set_dir_limits 0 0
10204                         error_exit "return $rc received instead of expected " \
10205                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
10206                 fi
10207                 nfiles=$((nfiles + 1))
10208                 dirsize=$(stat -c%s "$DIR/$tdir")
10209         done
10210
10211         set_dir_limits 0 0
10212         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
10213 }
10214 run_test 129 "test directory size limit ========================"
10215
10216 OLDIFS="$IFS"
10217 cleanup_130() {
10218         trap 0
10219         IFS="$OLDIFS"
10220 }
10221
10222 test_130a() {
10223         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10224         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10225
10226         trap cleanup_130 EXIT RETURN
10227
10228         local fm_file=$DIR/$tfile
10229         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
10230         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
10231                 error "dd failed for $fm_file"
10232
10233         # LU-1795: test filefrag/FIEMAP once, even if unsupported
10234         filefrag -ves $fm_file
10235         RC=$?
10236         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10237                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10238         [ $RC != 0 ] && error "filefrag $fm_file failed"
10239
10240         filefrag_op=$(filefrag -ve -k $fm_file |
10241                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10242         lun=$($GETSTRIPE -i $fm_file)
10243
10244         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
10245         IFS=$'\n'
10246         tot_len=0
10247         for line in $filefrag_op
10248         do
10249                 frag_lun=`echo $line | cut -d: -f5`
10250                 ext_len=`echo $line | cut -d: -f4`
10251                 if (( $frag_lun != $lun )); then
10252                         cleanup_130
10253                         error "FIEMAP on 1-stripe file($fm_file) failed"
10254                         return
10255                 fi
10256                 (( tot_len += ext_len ))
10257         done
10258
10259         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
10260                 cleanup_130
10261                 error "FIEMAP on 1-stripe file($fm_file) failed;"
10262                 return
10263         fi
10264
10265         cleanup_130
10266
10267         echo "FIEMAP on single striped file succeeded"
10268 }
10269 run_test 130a "FIEMAP (1-stripe file)"
10270
10271 test_130b() {
10272         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10273
10274         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10275         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10276
10277         trap cleanup_130 EXIT RETURN
10278
10279         local fm_file=$DIR/$tfile
10280         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10281                         error "setstripe on $fm_file"
10282         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10283                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10284
10285         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
10286                 error "dd failed on $fm_file"
10287
10288         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10289         filefrag_op=$(filefrag -ve -k $fm_file |
10290                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10291
10292         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10293                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10294
10295         IFS=$'\n'
10296         tot_len=0
10297         num_luns=1
10298         for line in $filefrag_op
10299         do
10300                 frag_lun=$(echo $line | cut -d: -f5 |
10301                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10302                 ext_len=$(echo $line | cut -d: -f4)
10303                 if (( $frag_lun != $last_lun )); then
10304                         if (( tot_len != 1024 )); then
10305                                 cleanup_130
10306                                 error "FIEMAP on $fm_file failed; returned " \
10307                                 "len $tot_len for OST $last_lun instead of 1024"
10308                                 return
10309                         else
10310                                 (( num_luns += 1 ))
10311                                 tot_len=0
10312                         fi
10313                 fi
10314                 (( tot_len += ext_len ))
10315                 last_lun=$frag_lun
10316         done
10317         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
10318                 cleanup_130
10319                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10320                         "luns or wrong len for OST $last_lun"
10321                 return
10322         fi
10323
10324         cleanup_130
10325
10326         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
10327 }
10328 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
10329
10330 test_130c() {
10331         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10332
10333         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10334         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10335
10336         trap cleanup_130 EXIT RETURN
10337
10338         local fm_file=$DIR/$tfile
10339         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
10340         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10341                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10342
10343         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
10344                         error "dd failed on $fm_file"
10345
10346         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10347         filefrag_op=$(filefrag -ve -k $fm_file |
10348                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10349
10350         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10351                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10352
10353         IFS=$'\n'
10354         tot_len=0
10355         num_luns=1
10356         for line in $filefrag_op
10357         do
10358                 frag_lun=$(echo $line | cut -d: -f5 |
10359                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10360                 ext_len=$(echo $line | cut -d: -f4)
10361                 if (( $frag_lun != $last_lun )); then
10362                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
10363                         if (( logical != 512 )); then
10364                                 cleanup_130
10365                                 error "FIEMAP on $fm_file failed; returned " \
10366                                 "logical start for lun $logical instead of 512"
10367                                 return
10368                         fi
10369                         if (( tot_len != 512 )); then
10370                                 cleanup_130
10371                                 error "FIEMAP on $fm_file failed; returned " \
10372                                 "len $tot_len for OST $last_lun instead of 1024"
10373                                 return
10374                         else
10375                                 (( num_luns += 1 ))
10376                                 tot_len=0
10377                         fi
10378                 fi
10379                 (( tot_len += ext_len ))
10380                 last_lun=$frag_lun
10381         done
10382         if (( num_luns != 2 || tot_len != 512 )); then
10383                 cleanup_130
10384                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10385                         "luns or wrong len for OST $last_lun"
10386                 return
10387         fi
10388
10389         cleanup_130
10390
10391         echo "FIEMAP on 2-stripe file with hole succeeded"
10392 }
10393 run_test 130c "FIEMAP (2-stripe file with hole)"
10394
10395 test_130d() {
10396         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
10397
10398         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10399         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10400
10401         trap cleanup_130 EXIT RETURN
10402
10403         local fm_file=$DIR/$tfile
10404         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10405                         error "setstripe on $fm_file"
10406         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10407                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10408
10409         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
10410         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
10411                 error "dd failed on $fm_file"
10412
10413         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10414         filefrag_op=$(filefrag -ve -k $fm_file |
10415                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10416
10417         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10418                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10419
10420         IFS=$'\n'
10421         tot_len=0
10422         num_luns=1
10423         for line in $filefrag_op
10424         do
10425                 frag_lun=$(echo $line | cut -d: -f5 |
10426                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10427                 ext_len=$(echo $line | cut -d: -f4)
10428                 if (( $frag_lun != $last_lun )); then
10429                         if (( tot_len != 1024 )); then
10430                                 cleanup_130
10431                                 error "FIEMAP on $fm_file failed; returned " \
10432                                 "len $tot_len for OST $last_lun instead of 1024"
10433                                 return
10434                         else
10435                                 (( num_luns += 1 ))
10436                                 tot_len=0
10437                         fi
10438                 fi
10439                 (( tot_len += ext_len ))
10440                 last_lun=$frag_lun
10441         done
10442         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
10443                 cleanup_130
10444                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10445                         "luns or wrong len for OST $last_lun"
10446                 return
10447         fi
10448
10449         cleanup_130
10450
10451         echo "FIEMAP on N-stripe file succeeded"
10452 }
10453 run_test 130d "FIEMAP (N-stripe file)"
10454
10455 test_130e() {
10456         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10457
10458         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10459         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10460
10461         trap cleanup_130 EXIT RETURN
10462
10463         local fm_file=$DIR/$tfile
10464         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
10465         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10466                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10467
10468         NUM_BLKS=512
10469         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
10470         for ((i = 0; i < $NUM_BLKS; i++))
10471         do
10472                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
10473         done
10474
10475         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10476         filefrag_op=$(filefrag -ve -k $fm_file |
10477                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10478
10479         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10480                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10481
10482         IFS=$'\n'
10483         tot_len=0
10484         num_luns=1
10485         for line in $filefrag_op
10486         do
10487                 frag_lun=$(echo $line | cut -d: -f5 |
10488                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10489                 ext_len=$(echo $line | cut -d: -f4)
10490                 if (( $frag_lun != $last_lun )); then
10491                         if (( tot_len != $EXPECTED_LEN )); then
10492                                 cleanup_130
10493                                 error "FIEMAP on $fm_file failed; returned " \
10494                                 "len $tot_len for OST $last_lun instead " \
10495                                 "of $EXPECTED_LEN"
10496                                 return
10497                         else
10498                                 (( num_luns += 1 ))
10499                                 tot_len=0
10500                         fi
10501                 fi
10502                 (( tot_len += ext_len ))
10503                 last_lun=$frag_lun
10504         done
10505         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
10506                 cleanup_130
10507                 error "FIEMAP on $fm_file failed; returned wrong number " \
10508                         "of luns or wrong len for OST $last_lun"
10509                 return
10510         fi
10511
10512         cleanup_130
10513
10514         echo "FIEMAP with continuation calls succeeded"
10515 }
10516 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
10517
10518 test_130f() {
10519         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10520         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10521
10522         local fm_file=$DIR/$tfile
10523         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
10524                 error "multiop create with lov_delay_create on $fm_file"
10525
10526         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10527         filefrag_extents=$(filefrag -vek $fm_file |
10528                            awk '/extents? found/ { print $2 }')
10529         if [[ "$filefrag_extents" != "0" ]]; then
10530                 error "FIEMAP on $fm_file failed; " \
10531                       "returned $filefrag_extents expected 0"
10532         fi
10533
10534         rm -f $fm_file
10535 }
10536 run_test 130f "FIEMAP (unstriped file)"
10537
10538 # Test for writev/readv
10539 test_131a() {
10540         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10541                 error "writev test failed"
10542         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10543                 error "readv failed"
10544         rm -f $DIR/$tfile
10545 }
10546 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10547
10548 test_131b() {
10549         local fsize=$((524288 + 1048576 + 1572864))
10550         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10551                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10552                         error "append writev test failed"
10553
10554         ((fsize += 1572864 + 1048576))
10555         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10556                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10557                         error "append writev test failed"
10558         rm -f $DIR/$tfile
10559 }
10560 run_test 131b "test append writev"
10561
10562 test_131c() {
10563         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10564         error "NOT PASS"
10565 }
10566 run_test 131c "test read/write on file w/o objects"
10567
10568 test_131d() {
10569         rwv -f $DIR/$tfile -w -n 1 1572864
10570         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10571         if [ "$NOB" != 1572864 ]; then
10572                 error "Short read filed: read $NOB bytes instead of 1572864"
10573         fi
10574         rm -f $DIR/$tfile
10575 }
10576 run_test 131d "test short read"
10577
10578 test_131e() {
10579         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10580         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10581         error "read hitting hole failed"
10582         rm -f $DIR/$tfile
10583 }
10584 run_test 131e "test read hitting hole"
10585
10586 check_stats() {
10587         local facet=$1
10588         local op=$2
10589         local want=${3:-0}
10590         local res
10591
10592         case $facet in
10593         mds*) res=$(do_facet $facet \
10594                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10595                  ;;
10596         ost*) res=$(do_facet $facet \
10597                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10598                  ;;
10599         *) error "Wrong facet '$facet'" ;;
10600         esac
10601         echo $res
10602         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10603         # if the argument $3 is zero, it means any stat increment is ok.
10604         if [[ $want -gt 0 ]]; then
10605                 local count=$(echo $res | awk '{ print $2 }')
10606                 [[ $count -ne $want ]] &&
10607                         error "The $op counter on $facet is $count, not $want"
10608         fi
10609 }
10610
10611 test_133a() {
10612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10613         remote_ost_nodsh && skip "remote OST with nodsh"
10614         remote_mds_nodsh && skip "remote MDS with nodsh"
10615         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10616                 skip_env "MDS doesn't support rename stats"
10617
10618         local testdir=$DIR/${tdir}/stats_testdir
10619
10620         mkdir -p $DIR/${tdir}
10621
10622         # clear stats.
10623         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10624         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10625
10626         # verify mdt stats first.
10627         mkdir ${testdir} || error "mkdir failed"
10628         check_stats $SINGLEMDS "mkdir" 1
10629         touch ${testdir}/${tfile} || error "touch failed"
10630         check_stats $SINGLEMDS "open" 1
10631         check_stats $SINGLEMDS "close" 1
10632         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
10633                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10634                 check_stats $SINGLEMDS "mknod" 2
10635         }
10636         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10637         check_stats $SINGLEMDS "unlink" 1
10638         rm -f ${testdir}/${tfile} || error "file remove failed"
10639         check_stats $SINGLEMDS "unlink" 2
10640
10641         # remove working dir and check mdt stats again.
10642         rmdir ${testdir} || error "rmdir failed"
10643         check_stats $SINGLEMDS "rmdir" 1
10644
10645         local testdir1=$DIR/${tdir}/stats_testdir1
10646         mkdir -p ${testdir}
10647         mkdir -p ${testdir1}
10648         touch ${testdir1}/test1
10649         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10650         check_stats $SINGLEMDS "crossdir_rename" 1
10651
10652         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10653         check_stats $SINGLEMDS "samedir_rename" 1
10654
10655         rm -rf $DIR/${tdir}
10656 }
10657 run_test 133a "Verifying MDT stats ========================================"
10658
10659 test_133b() {
10660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10661         remote_ost_nodsh && skip "remote OST with nodsh"
10662         remote_mds_nodsh && skip "remote MDS with nodsh"
10663
10664         local testdir=$DIR/${tdir}/stats_testdir
10665
10666         mkdir -p ${testdir} || error "mkdir failed"
10667         touch ${testdir}/${tfile} || error "touch failed"
10668         cancel_lru_locks mdc
10669
10670         # clear stats.
10671         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10672         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10673
10674         # extra mdt stats verification.
10675         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10676         check_stats $SINGLEMDS "setattr" 1
10677         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10678         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
10679         then            # LU-1740
10680                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10681                 check_stats $SINGLEMDS "getattr" 1
10682         fi
10683         $LFS df || error "lfs failed"
10684         check_stats $SINGLEMDS "statfs" 1
10685
10686         rm -rf $DIR/${tdir}
10687 }
10688 run_test 133b "Verifying extra MDT stats =================================="
10689
10690 test_133c() {
10691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10692         remote_ost_nodsh && skip "remote OST with nodsh"
10693         remote_mds_nodsh && skip "remote MDS with nodsh"
10694
10695         local testdir=$DIR/$tdir/stats_testdir
10696
10697         test_mkdir -p $testdir
10698
10699         # verify obdfilter stats.
10700         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10701         sync
10702         cancel_lru_locks osc
10703         wait_delete_completed
10704
10705         # clear stats.
10706         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10707         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10708
10709         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10710                 error "dd failed"
10711         sync
10712         cancel_lru_locks osc
10713         check_stats ost1 "write" 1
10714
10715         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10716         check_stats ost1 "read" 1
10717
10718         > $testdir/$tfile || error "truncate failed"
10719         check_stats ost1 "punch" 1
10720
10721         rm -f $testdir/$tfile || error "file remove failed"
10722         wait_delete_completed
10723         check_stats ost1 "destroy" 1
10724
10725         rm -rf $DIR/$tdir
10726 }
10727 run_test 133c "Verifying OST stats ========================================"
10728
10729 order_2() {
10730         local value=$1
10731         local orig=$value
10732         local order=1
10733
10734         while [ $value -ge 2 ]; do
10735                 order=$((order*2))
10736                 value=$((value/2))
10737         done
10738
10739         if [ $orig -gt $order ]; then
10740                 order=$((order*2))
10741         fi
10742         echo $order
10743 }
10744
10745 size_in_KMGT() {
10746     local value=$1
10747     local size=('K' 'M' 'G' 'T');
10748     local i=0
10749     local size_string=$value
10750
10751     while [ $value -ge 1024 ]; do
10752         if [ $i -gt 3 ]; then
10753             #T is the biggest unit we get here, if that is bigger,
10754             #just return XXXT
10755             size_string=${value}T
10756             break
10757         fi
10758         value=$((value >> 10))
10759         if [ $value -lt 1024 ]; then
10760             size_string=${value}${size[$i]}
10761             break
10762         fi
10763         i=$((i + 1))
10764     done
10765
10766     echo $size_string
10767 }
10768
10769 get_rename_size() {
10770         local size=$1
10771         local context=${2:-.}
10772         local sample=$(do_facet $SINGLEMDS $LCTL \
10773                 get_param mdt.$FSNAME-MDT0000.rename_stats |
10774                 grep -A1 $context |
10775                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10776         echo $sample
10777 }
10778
10779 test_133d() {
10780         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10781         remote_ost_nodsh && skip "remote OST with nodsh"
10782         remote_mds_nodsh && skip "remote MDS with nodsh"
10783         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10784                 skip_env "MDS doesn't support rename stats"
10785
10786         local testdir1=$DIR/${tdir}/stats_testdir1
10787         local testdir2=$DIR/${tdir}/stats_testdir2
10788         mkdir -p $DIR/${tdir}
10789
10790         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10791
10792         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
10793         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
10794
10795         createmany -o $testdir1/test 512 || error "createmany failed"
10796
10797         # check samedir rename size
10798         mv ${testdir1}/test0 ${testdir1}/test_0
10799
10800         local testdir1_size=$(ls -l $DIR/${tdir} |
10801                 awk '/stats_testdir1/ {print $5}')
10802         local testdir2_size=$(ls -l $DIR/${tdir} |
10803                 awk '/stats_testdir2/ {print $5}')
10804
10805         testdir1_size=$(order_2 $testdir1_size)
10806         testdir2_size=$(order_2 $testdir2_size)
10807
10808         testdir1_size=$(size_in_KMGT $testdir1_size)
10809         testdir2_size=$(size_in_KMGT $testdir2_size)
10810
10811         echo "source rename dir size: ${testdir1_size}"
10812         echo "target rename dir size: ${testdir2_size}"
10813
10814         local cmd="do_facet $SINGLEMDS $LCTL "
10815         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
10816
10817         eval $cmd || error "$cmd failed"
10818         local samedir=$($cmd | grep 'same_dir')
10819         local same_sample=$(get_rename_size $testdir1_size)
10820         [ -z "$samedir" ] && error "samedir_rename_size count error"
10821         [[ $same_sample -eq 1 ]] ||
10822                 error "samedir_rename_size error $same_sample"
10823         echo "Check same dir rename stats success"
10824
10825         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10826
10827         # check crossdir rename size
10828         mv ${testdir1}/test_0 ${testdir2}/test_0
10829
10830         testdir1_size=$(ls -l $DIR/${tdir} |
10831                 awk '/stats_testdir1/ {print $5}')
10832         testdir2_size=$(ls -l $DIR/${tdir} |
10833                 awk '/stats_testdir2/ {print $5}')
10834
10835         testdir1_size=$(order_2 $testdir1_size)
10836         testdir2_size=$(order_2 $testdir2_size)
10837
10838         testdir1_size=$(size_in_KMGT $testdir1_size)
10839         testdir2_size=$(size_in_KMGT $testdir2_size)
10840
10841         echo "source rename dir size: ${testdir1_size}"
10842         echo "target rename dir size: ${testdir2_size}"
10843
10844         eval $cmd || error "$cmd failed"
10845         local crossdir=$($cmd | grep 'crossdir')
10846         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10847         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10848         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10849         [[ $src_sample -eq 1 ]] ||
10850                 error "crossdir_rename_size error $src_sample"
10851         [[ $tgt_sample -eq 1 ]] ||
10852                 error "crossdir_rename_size error $tgt_sample"
10853         echo "Check cross dir rename stats success"
10854         rm -rf $DIR/${tdir}
10855 }
10856 run_test 133d "Verifying rename_stats ========================================"
10857
10858 test_133e() {
10859         remote_mds_nodsh && skip "remote MDS with nodsh"
10860         remote_ost_nodsh && skip "remote OST with nodsh"
10861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10862
10863         local testdir=$DIR/${tdir}/stats_testdir
10864         local ctr f0 f1 bs=32768 count=42 sum
10865
10866         mkdir -p ${testdir} || error "mkdir failed"
10867
10868         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10869
10870         for ctr in {write,read}_bytes; do
10871                 sync
10872                 cancel_lru_locks osc
10873
10874                 do_facet ost1 $LCTL set_param -n \
10875                         "obdfilter.*.exports.clear=clear"
10876
10877                 if [ $ctr = write_bytes ]; then
10878                         f0=/dev/zero
10879                         f1=${testdir}/${tfile}
10880                 else
10881                         f0=${testdir}/${tfile}
10882                         f1=/dev/null
10883                 fi
10884
10885                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10886                         error "dd failed"
10887                 sync
10888                 cancel_lru_locks osc
10889
10890                 sum=$(do_facet ost1 $LCTL get_param \
10891                         "obdfilter.*.exports.*.stats" |
10892                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10893                                 $1 == ctr { sum += $7 }
10894                                 END { printf("%0.0f", sum) }')
10895
10896                 if ((sum != bs * count)); then
10897                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10898                 fi
10899         done
10900
10901         rm -rf $DIR/${tdir}
10902 }
10903 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10904
10905 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10906
10907 test_133f() {
10908         # First without trusting modes.
10909         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10910         echo "proc_dirs='$proc_dirs'"
10911         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10912         find $proc_dirs -exec cat '{}' \; &> /dev/null
10913
10914         # Second verifying readability.
10915         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10916
10917         # Verifing writability with badarea_io.
10918         find $proc_dirs \
10919                 -ignore_readdir_race \
10920                 -type f \
10921                 -not -name force_lbug \
10922                 -not -name changelog_mask \
10923                 -exec badarea_io '{}' \; ||
10924                         error "find $proc_dirs failed"
10925 }
10926 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
10927
10928 test_133g() {
10929         remote_mds_nodsh && skip "remote MDS with nodsh"
10930         remote_ost_nodsh && skip "remote OST with nodsh"
10931
10932         # eventually, this can also be replaced with "lctl get_param -R",
10933         # but not until that option is always available on the server
10934         local facet
10935         for facet in mds1 ost1; do
10936                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10937                         skip_noexit "Too old lustre on $facet"
10938                 local facet_proc_dirs=$(do_facet $facet \
10939                                         \\\ls -d $proc_regexp 2>/dev/null)
10940                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10941                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10942                 do_facet $facet find $facet_proc_dirs \
10943                         ! -name req_history \
10944                         -exec cat '{}' \\\; &> /dev/null
10945
10946                 do_facet $facet find $facet_proc_dirs \
10947                         ! -name req_history \
10948                         -type f \
10949                         -exec cat '{}' \\\; &> /dev/null ||
10950                                 error "proc file read failed"
10951
10952                 do_facet $facet find $facet_proc_dirs \
10953                         -ignore_readdir_race \
10954                         -type f \
10955                         -not -name force_lbug \
10956                         -not -name changelog_mask \
10957                         -exec badarea_io '{}' \\\; ||
10958                                 error "$facet find $facet_proc_dirs failed"
10959         done
10960
10961         # remount the FS in case writes/reads /proc break the FS
10962         cleanup || error "failed to unmount"
10963         setup || error "failed to setup"
10964         true
10965 }
10966 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
10967
10968 test_133h() {
10969         remote_mds_nodsh && skip "remote MDS with nodsh"
10970         remote_ost_nodsh && skip "remote OST with nodsh"
10971         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10972                 skip "Need MDS version at least 2.9.54"
10973
10974         local facet
10975
10976         for facet in client mds1 ost1; do
10977                 local facet_proc_dirs=$(do_facet $facet \
10978                                         \\\ls -d $proc_regexp 2> /dev/null)
10979                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10980                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10981                 # Get the list of files that are missing the terminating newline
10982                 local missing=($(do_facet $facet \
10983                         find ${facet_proc_dirs} -type f \|              \
10984                                 while read F\; do                       \
10985                                         awk -v FS='\v' -v RS='\v\v'     \
10986                                         "'END { if(NR>0 &&              \
10987                                         \\\$NF !~ /.*\\\n\$/)           \
10988                                                 print FILENAME}'"       \
10989                                         '\$F'\;                         \
10990                                 done 2>/dev/null))
10991                 [ ${#missing[*]} -eq 0 ] ||
10992                         error "files do not end with newline: ${missing[*]}"
10993         done
10994 }
10995 run_test 133h "Proc files should end with newlines"
10996
10997 test_134a() {
10998         remote_mds_nodsh && skip "remote MDS with nodsh"
10999         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
11000                 skip "Need MDS version at least 2.7.54"
11001
11002         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11003         cancel_lru_locks mdc
11004
11005         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11006         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11007         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11008
11009         local nr=1000
11010         createmany -o $DIR/$tdir/f $nr ||
11011                 error "failed to create $nr files in $DIR/$tdir"
11012         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11013
11014         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11015         do_facet mds1 $LCTL set_param fail_loc=0x327
11016         do_facet mds1 $LCTL set_param fail_val=500
11017         touch $DIR/$tdir/m
11018
11019         echo "sleep 10 seconds ..."
11020         sleep 10
11021         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11022
11023         do_facet mds1 $LCTL set_param fail_loc=0
11024         do_facet mds1 $LCTL set_param fail_val=0
11025         [ $lck_cnt -lt $unused ] ||
11026                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11027
11028         rm $DIR/$tdir/m
11029         unlinkmany $DIR/$tdir/f $nr
11030 }
11031 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11032
11033 test_134b() {
11034         remote_mds_nodsh && skip "remote MDS with nodsh"
11035         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
11036                 skip "Need MDS version at least 2.7.54"
11037
11038         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11039         cancel_lru_locks mdc
11040
11041         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11042                         ldlm.lock_reclaim_threshold_mb)
11043         # disable reclaim temporarily
11044         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11045
11046         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11047         do_facet mds1 $LCTL set_param fail_loc=0x328
11048         do_facet mds1 $LCTL set_param fail_val=500
11049
11050         $LCTL set_param debug=+trace
11051
11052         local nr=600
11053         createmany -o $DIR/$tdir/f $nr &
11054         local create_pid=$!
11055
11056         echo "Sleep $TIMEOUT seconds ..."
11057         sleep $TIMEOUT
11058         if ! ps -p $create_pid  > /dev/null 2>&1; then
11059                 do_facet mds1 $LCTL set_param fail_loc=0
11060                 do_facet mds1 $LCTL set_param fail_val=0
11061                 do_facet mds1 $LCTL set_param \
11062                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11063                 error "createmany finished incorrectly!"
11064         fi
11065         do_facet mds1 $LCTL set_param fail_loc=0
11066         do_facet mds1 $LCTL set_param fail_val=0
11067         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11068         wait $create_pid || return 1
11069
11070         unlinkmany $DIR/$tdir/f $nr
11071 }
11072 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11073
11074 test_140() { #bug-17379
11075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11076
11077         test_mkdir $DIR/$tdir
11078         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11079         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11080
11081         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11082         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11083         local i=0
11084         while i=$((i + 1)); do
11085                 test_mkdir $i
11086                 cd $i || error "Changing to $i"
11087                 ln -s ../stat stat || error "Creating stat symlink"
11088                 # Read the symlink until ELOOP present,
11089                 # not LBUGing the system is considered success,
11090                 # we didn't overrun the stack.
11091                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11092                 if [ $ret -ne 0 ]; then
11093                         if [ $ret -eq 40 ]; then
11094                                 break  # -ELOOP
11095                         else
11096                                 error "Open stat symlink"
11097                                         return
11098                         fi
11099                 fi
11100         done
11101         i=$((i - 1))
11102         echo "The symlink depth = $i"
11103         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
11104                                         error "Invalid symlink depth"
11105
11106         # Test recursive symlink
11107         ln -s symlink_self symlink_self
11108         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11109         echo "open symlink_self returns $ret"
11110         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11111 }
11112 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11113
11114 test_150() {
11115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11116
11117         local TF="$TMP/$tfile"
11118
11119         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11120         cp $TF $DIR/$tfile
11121         cancel_lru_locks $OSC
11122         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
11123         remount_client $MOUNT
11124         df -P $MOUNT
11125         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
11126
11127         $TRUNCATE $TF 6000
11128         $TRUNCATE $DIR/$tfile 6000
11129         cancel_lru_locks $OSC
11130         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
11131
11132         echo "12345" >>$TF
11133         echo "12345" >>$DIR/$tfile
11134         cancel_lru_locks $OSC
11135         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
11136
11137         echo "12345" >>$TF
11138         echo "12345" >>$DIR/$tfile
11139         cancel_lru_locks $OSC
11140         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
11141
11142         rm -f $TF
11143         true
11144 }
11145 run_test 150 "truncate/append tests"
11146
11147 #LU-2902 roc_hit was not able to read all values from lproc
11148 function roc_hit_init() {
11149         local list=$(comma_list $(osts_nodes))
11150         local dir=$DIR/$tdir-check
11151         local file=$dir/$tfile
11152         local BEFORE
11153         local AFTER
11154         local idx
11155
11156         test_mkdir $dir
11157         #use setstripe to do a write to every ost
11158         for i in $(seq 0 $((OSTCOUNT-1))); do
11159                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
11160                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
11161                 idx=$(printf %04x $i)
11162                 BEFORE=$(get_osd_param $list *OST*$idx stats |
11163                         awk '$1 == "cache_access" {sum += $7}
11164                                 END { printf("%0.0f", sum) }')
11165
11166                 cancel_lru_locks osc
11167                 cat $file >/dev/null
11168
11169                 AFTER=$(get_osd_param $list *OST*$idx stats |
11170                         awk '$1 == "cache_access" {sum += $7}
11171                                 END { printf("%0.0f", sum) }')
11172
11173                 echo BEFORE:$BEFORE AFTER:$AFTER
11174                 if ! let "AFTER - BEFORE == 4"; then
11175                         rm -rf $dir
11176                         error "roc_hit is not safe to use"
11177                 fi
11178                 rm $file
11179         done
11180
11181         rm -rf $dir
11182 }
11183
11184 function roc_hit() {
11185         local list=$(comma_list $(osts_nodes))
11186         echo $(get_osd_param $list '' stats |
11187                 awk '$1 == "cache_hit" {sum += $7}
11188                         END { printf("%0.0f", sum) }')
11189 }
11190
11191 function set_cache() {
11192         local on=1
11193
11194         if [ "$2" == "off" ]; then
11195                 on=0;
11196         fi
11197         local list=$(comma_list $(osts_nodes))
11198         set_osd_param $list '' $1_cache_enable $on
11199
11200         cancel_lru_locks osc
11201 }
11202
11203 test_151() {
11204         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11205         remote_ost_nodsh && skip "remote OST with nodsh"
11206
11207         local CPAGES=3
11208         local list=$(comma_list $(osts_nodes))
11209
11210         # check whether obdfilter is cache capable at all
11211         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
11212                 skip "not cache-capable obdfilter"
11213         fi
11214
11215         # check cache is enabled on all obdfilters
11216         if get_osd_param $list '' read_cache_enable | grep 0; then
11217                 skip "oss cache is disabled"
11218         fi
11219
11220         set_osd_param $list '' writethrough_cache_enable 1
11221
11222         # check write cache is enabled on all obdfilters
11223         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
11224                 skip "oss write cache is NOT enabled"
11225         fi
11226
11227         roc_hit_init
11228
11229         #define OBD_FAIL_OBD_NO_LRU  0x609
11230         do_nodes $list $LCTL set_param fail_loc=0x609
11231
11232         # pages should be in the case right after write
11233         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
11234                 error "dd failed"
11235
11236         local BEFORE=$(roc_hit)
11237         cancel_lru_locks osc
11238         cat $DIR/$tfile >/dev/null
11239         local AFTER=$(roc_hit)
11240
11241         do_nodes $list $LCTL set_param fail_loc=0
11242
11243         if ! let "AFTER - BEFORE == CPAGES"; then
11244                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11245         fi
11246
11247         # the following read invalidates the cache
11248         cancel_lru_locks osc
11249         set_osd_param $list '' read_cache_enable 0
11250         cat $DIR/$tfile >/dev/null
11251
11252         # now data shouldn't be found in the cache
11253         BEFORE=$(roc_hit)
11254         cancel_lru_locks osc
11255         cat $DIR/$tfile >/dev/null
11256         AFTER=$(roc_hit)
11257         if let "AFTER - BEFORE != 0"; then
11258                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11259         fi
11260
11261         set_osd_param $list '' read_cache_enable 1
11262         rm -f $DIR/$tfile
11263 }
11264 run_test 151 "test cache on oss and controls ==============================="
11265
11266 test_152() {
11267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11268
11269         local TF="$TMP/$tfile"
11270
11271         # simulate ENOMEM during write
11272 #define OBD_FAIL_OST_NOMEM      0x226
11273         lctl set_param fail_loc=0x80000226
11274         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11275         cp $TF $DIR/$tfile
11276         sync || error "sync failed"
11277         lctl set_param fail_loc=0
11278
11279         # discard client's cache
11280         cancel_lru_locks osc
11281
11282         # simulate ENOMEM during read
11283         lctl set_param fail_loc=0x80000226
11284         cmp $TF $DIR/$tfile || error "cmp failed"
11285         lctl set_param fail_loc=0
11286
11287         rm -f $TF
11288 }
11289 run_test 152 "test read/write with enomem ============================"
11290
11291 test_153() {
11292         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
11293 }
11294 run_test 153 "test if fdatasync does not crash ======================="
11295
11296 dot_lustre_fid_permission_check() {
11297         local fid=$1
11298         local ffid=$MOUNT/.lustre/fid/$fid
11299         local test_dir=$2
11300
11301         echo "stat fid $fid"
11302         stat $ffid > /dev/null || error "stat $ffid failed."
11303         echo "touch fid $fid"
11304         touch $ffid || error "touch $ffid failed."
11305         echo "write to fid $fid"
11306         cat /etc/hosts > $ffid || error "write $ffid failed."
11307         echo "read fid $fid"
11308         diff /etc/hosts $ffid || error "read $ffid failed."
11309         echo "append write to fid $fid"
11310         cat /etc/hosts >> $ffid || error "append write $ffid failed."
11311         echo "rename fid $fid"
11312         mv $ffid $test_dir/$tfile.1 &&
11313                 error "rename $ffid to $tfile.1 should fail."
11314         touch $test_dir/$tfile.1
11315         mv $test_dir/$tfile.1 $ffid &&
11316                 error "rename $tfile.1 to $ffid should fail."
11317         rm -f $test_dir/$tfile.1
11318         echo "truncate fid $fid"
11319         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
11320         echo "link fid $fid"
11321         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
11322         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
11323                 echo "setfacl fid $fid"
11324                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
11325                 echo "getfacl fid $fid"
11326                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
11327         fi
11328         echo "unlink fid $fid"
11329         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
11330         echo "mknod fid $fid"
11331         mknod $ffid c 1 3 && error "mknod $ffid should fail."
11332
11333         fid=[0xf00000400:0x1:0x0]
11334         ffid=$MOUNT/.lustre/fid/$fid
11335
11336         echo "stat non-exist fid $fid"
11337         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
11338         echo "write to non-exist fid $fid"
11339         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
11340         echo "link new fid $fid"
11341         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
11342
11343         mkdir -p $test_dir/$tdir
11344         touch $test_dir/$tdir/$tfile
11345         fid=$($LFS path2fid $test_dir/$tdir)
11346         rc=$?
11347         [ $rc -ne 0 ] &&
11348                 error "error: could not get fid for $test_dir/$dir/$tfile."
11349
11350         ffid=$MOUNT/.lustre/fid/$fid
11351
11352         echo "ls $fid"
11353         ls $ffid > /dev/null || error "ls $ffid failed."
11354         echo "touch $fid/$tfile.1"
11355         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
11356
11357         echo "touch $MOUNT/.lustre/fid/$tfile"
11358         touch $MOUNT/.lustre/fid/$tfile && \
11359                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
11360
11361         echo "setxattr to $MOUNT/.lustre/fid"
11362         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
11363
11364         echo "listxattr for $MOUNT/.lustre/fid"
11365         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
11366
11367         echo "delxattr from $MOUNT/.lustre/fid"
11368         setfattr -x trusted.name1 $MOUNT/.lustre/fid
11369
11370         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
11371         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
11372                 error "touch invalid fid should fail."
11373
11374         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
11375         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
11376                 error "touch non-normal fid should fail."
11377
11378         echo "rename $tdir to $MOUNT/.lustre/fid"
11379         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
11380                 error "rename to $MOUNT/.lustre/fid should fail."
11381
11382         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
11383         then            # LU-3547
11384                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
11385                 local new_obf_mode=777
11386
11387                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
11388                 chmod $new_obf_mode $DIR/.lustre/fid ||
11389                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
11390
11391                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
11392                 [ $obf_mode -eq $new_obf_mode ] ||
11393                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
11394
11395                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
11396                 chmod $old_obf_mode $DIR/.lustre/fid ||
11397                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
11398         fi
11399
11400         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
11401         fid=$($LFS path2fid $test_dir/$tfile-2)
11402
11403         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
11404         then # LU-5424
11405                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
11406                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
11407                         error "create lov data thru .lustre failed"
11408         fi
11409         echo "cp /etc/passwd $test_dir/$tfile-2"
11410         cp /etc/passwd $test_dir/$tfile-2 ||
11411                 error "copy to $test_dir/$tfile-2 failed."
11412         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
11413         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
11414                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
11415
11416         rm -rf $test_dir/tfile.lnk
11417         rm -rf $test_dir/$tfile-2
11418 }
11419
11420 test_154A() {
11421         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11422                 skip "Need MDS version at least 2.4.1"
11423
11424         local tf=$DIR/$tfile
11425         touch $tf
11426
11427         local fid=$($LFS path2fid $tf)
11428         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
11429
11430         # check that we get the same pathname back
11431         local found=$($LFS fid2path $MOUNT "$fid")
11432         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
11433         [ "$found" == "$tf" ] ||
11434                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
11435 }
11436 run_test 154A "lfs path2fid and fid2path basic checks"
11437
11438 test_154B() {
11439         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11440                 skip "Need MDS version at least 2.4.1"
11441
11442         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11443         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11444         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
11445         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
11446
11447         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
11448         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
11449
11450         # check that we get the same pathname
11451         echo "PFID: $PFID, name: $name"
11452         local FOUND=$($LFS fid2path $MOUNT "$PFID")
11453         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
11454         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
11455                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
11456
11457         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
11458 }
11459 run_test 154B "verify the ll_decode_linkea tool"
11460
11461 test_154a() {
11462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11463         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11464         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11465                 skip "Need MDS version at least 2.2.51"
11466         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11467
11468         cp /etc/hosts $DIR/$tfile
11469
11470         fid=$($LFS path2fid $DIR/$tfile)
11471         rc=$?
11472         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
11473
11474         dot_lustre_fid_permission_check "$fid" $DIR ||
11475                 error "dot lustre permission check $fid failed"
11476
11477         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
11478
11479         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
11480
11481         touch $MOUNT/.lustre/file &&
11482                 error "creation is not allowed under .lustre"
11483
11484         mkdir $MOUNT/.lustre/dir &&
11485                 error "mkdir is not allowed under .lustre"
11486
11487         rm -rf $DIR/$tfile
11488 }
11489 run_test 154a "Open-by-FID"
11490
11491 test_154b() {
11492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11493         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11494         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
11495         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11496                 skip "Need MDS version at least 2.2.51"
11497
11498         local remote_dir=$DIR/$tdir/remote_dir
11499         local MDTIDX=1
11500         local rc=0
11501
11502         mkdir -p $DIR/$tdir
11503         $LFS mkdir -i $MDTIDX $remote_dir ||
11504                 error "create remote directory failed"
11505
11506         cp /etc/hosts $remote_dir/$tfile
11507
11508         fid=$($LFS path2fid $remote_dir/$tfile)
11509         rc=$?
11510         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
11511
11512         dot_lustre_fid_permission_check "$fid" $remote_dir ||
11513                 error "dot lustre permission check $fid failed"
11514         rm -rf $DIR/$tdir
11515 }
11516 run_test 154b "Open-by-FID for remote directory"
11517
11518 test_154c() {
11519         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11520                 skip "Need MDS version at least 2.4.1"
11521
11522         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
11523         local FID1=$($LFS path2fid $DIR/$tfile.1)
11524         local FID2=$($LFS path2fid $DIR/$tfile.2)
11525         local FID3=$($LFS path2fid $DIR/$tfile.3)
11526
11527         local N=1
11528         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
11529                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
11530                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
11531                 local want=FID$N
11532                 [ "$FID" = "${!want}" ] ||
11533                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
11534                 N=$((N + 1))
11535         done
11536
11537         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11538         do
11539                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11540                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11541                 N=$((N + 1))
11542         done
11543 }
11544 run_test 154c "lfs path2fid and fid2path multiple arguments"
11545
11546 test_154d() {
11547         remote_mds_nodsh && skip "remote MDS with nodsh"
11548         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
11549                 skip "Need MDS version at least 2.5.53"
11550
11551         if remote_mds; then
11552                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11553         else
11554                 nid="0@lo"
11555         fi
11556         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11557         local fd
11558         local cmd
11559
11560         rm -f $DIR/$tfile
11561         touch $DIR/$tfile
11562
11563         local fid=$($LFS path2fid $DIR/$tfile)
11564         # Open the file
11565         fd=$(free_fd)
11566         cmd="exec $fd<$DIR/$tfile"
11567         eval $cmd
11568         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11569         echo "$fid_list" | grep "$fid"
11570         rc=$?
11571
11572         cmd="exec $fd>/dev/null"
11573         eval $cmd
11574         if [ $rc -ne 0 ]; then
11575                 error "FID $fid not found in open files list $fid_list"
11576         fi
11577 }
11578 run_test 154d "Verify open file fid"
11579
11580 test_154e()
11581 {
11582         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
11583                 skip "Need MDS version at least 2.6.50"
11584
11585         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11586                 error ".lustre returned by readdir"
11587         fi
11588 }
11589 run_test 154e ".lustre is not returned by readdir"
11590
11591 test_154f() {
11592         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11593
11594         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11595         test_mkdir -p -c1 $DIR/$tdir/d
11596         # test dirs inherit from its stripe
11597         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11598         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11599         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11600         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11601         touch $DIR/f
11602
11603         # get fid of parents
11604         local FID0=$($LFS path2fid $DIR/$tdir/d)
11605         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11606         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11607         local FID3=$($LFS path2fid $DIR)
11608
11609         # check that path2fid --parents returns expected <parent_fid>/name
11610         # 1) test for a directory (single parent)
11611         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11612         [ "$parent" == "$FID0/foo1" ] ||
11613                 error "expected parent: $FID0/foo1, got: $parent"
11614
11615         # 2) test for a file with nlink > 1 (multiple parents)
11616         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11617         echo "$parent" | grep -F "$FID1/$tfile" ||
11618                 error "$FID1/$tfile not returned in parent list"
11619         echo "$parent" | grep -F "$FID2/link" ||
11620                 error "$FID2/link not returned in parent list"
11621
11622         # 3) get parent by fid
11623         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11624         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11625         echo "$parent" | grep -F "$FID1/$tfile" ||
11626                 error "$FID1/$tfile not returned in parent list (by fid)"
11627         echo "$parent" | grep -F "$FID2/link" ||
11628                 error "$FID2/link not returned in parent list (by fid)"
11629
11630         # 4) test for entry in root directory
11631         parent=$($LFS path2fid --parents $DIR/f)
11632         echo "$parent" | grep -F "$FID3/f" ||
11633                 error "$FID3/f not returned in parent list"
11634
11635         # 5) test it on root directory
11636         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11637                 error "$MOUNT should not have parents"
11638
11639         # enable xattr caching and check that linkea is correctly updated
11640         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11641         save_lustre_params client "llite.*.xattr_cache" > $save
11642         lctl set_param llite.*.xattr_cache 1
11643
11644         # 6.1) linkea update on rename
11645         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11646
11647         # get parents by fid
11648         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11649         # foo1 should no longer be returned in parent list
11650         echo "$parent" | grep -F "$FID1" &&
11651                 error "$FID1 should no longer be in parent list"
11652         # the new path should appear
11653         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11654                 error "$FID2/$tfile.moved is not in parent list"
11655
11656         # 6.2) linkea update on unlink
11657         rm -f $DIR/$tdir/d/foo2/link
11658         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11659         # foo2/link should no longer be returned in parent list
11660         echo "$parent" | grep -F "$FID2/link" &&
11661                 error "$FID2/link should no longer be in parent list"
11662         true
11663
11664         rm -f $DIR/f
11665         restore_lustre_params < $save
11666         rm -f $save
11667 }
11668 run_test 154f "get parent fids by reading link ea"
11669
11670 test_154g()
11671 {
11672         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11673         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
11674            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
11675                 skip "Need MDS version at least 2.6.92"
11676
11677         mkdir -p $DIR/$tdir
11678         llapi_fid_test -d $DIR/$tdir
11679 }
11680 run_test 154g "various llapi FID tests"
11681
11682 test_155_small_load() {
11683     local temp=$TMP/$tfile
11684     local file=$DIR/$tfile
11685
11686     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11687         error "dd of=$temp bs=6096 count=1 failed"
11688     cp $temp $file
11689     cancel_lru_locks $OSC
11690     cmp $temp $file || error "$temp $file differ"
11691
11692     $TRUNCATE $temp 6000
11693     $TRUNCATE $file 6000
11694     cmp $temp $file || error "$temp $file differ (truncate1)"
11695
11696     echo "12345" >>$temp
11697     echo "12345" >>$file
11698     cmp $temp $file || error "$temp $file differ (append1)"
11699
11700     echo "12345" >>$temp
11701     echo "12345" >>$file
11702     cmp $temp $file || error "$temp $file differ (append2)"
11703
11704     rm -f $temp $file
11705     true
11706 }
11707
11708 test_155_big_load() {
11709         remote_ost_nodsh && skip "remote OST with nodsh"
11710
11711         local temp=$TMP/$tfile
11712         local file=$DIR/$tfile
11713
11714         free_min_max
11715         local cache_size=$(do_facet ost$((MAXI+1)) \
11716                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11717         local large_file_size=$((cache_size * 2))
11718
11719         echo "OSS cache size: $cache_size KB"
11720         echo "Large file size: $large_file_size KB"
11721
11722         [ $MAXV -le $large_file_size ] &&
11723                 skip_env "max available OST size needs > $large_file_size KB"
11724
11725         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
11726
11727         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
11728                 error "dd of=$temp bs=$large_file_size count=1k failed"
11729         cp $temp $file
11730         ls -lh $temp $file
11731         cancel_lru_locks osc
11732         cmp $temp $file || error "$temp $file differ"
11733
11734         rm -f $temp $file
11735         true
11736 }
11737
11738 save_writethrough() {
11739         local facets=$(get_facets OST)
11740
11741         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
11742         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
11743 }
11744
11745 test_155a() {
11746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11747
11748         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11749
11750         save_writethrough $p
11751
11752         set_cache read on
11753         set_cache writethrough on
11754         test_155_small_load
11755         restore_lustre_params < $p
11756         rm -f $p
11757 }
11758 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11759
11760 test_155b() {
11761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11762
11763         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11764
11765         save_writethrough $p
11766
11767         set_cache read on
11768         set_cache writethrough off
11769         test_155_small_load
11770         restore_lustre_params < $p
11771         rm -f $p
11772 }
11773 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11774
11775 test_155c() {
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11777
11778         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11779
11780         save_writethrough $p
11781
11782         set_cache read off
11783         set_cache writethrough on
11784         test_155_small_load
11785         restore_lustre_params < $p
11786         rm -f $p
11787 }
11788 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11789
11790 test_155d() {
11791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11792
11793         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11794
11795         save_writethrough $p
11796
11797         set_cache read off
11798         set_cache writethrough off
11799         test_155_small_load
11800         restore_lustre_params < $p
11801         rm -f $p
11802 }
11803 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11804
11805 test_155e() {
11806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11807
11808         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11809
11810         save_writethrough $p
11811
11812         set_cache read on
11813         set_cache writethrough on
11814         test_155_big_load
11815         restore_lustre_params < $p
11816         rm -f $p
11817 }
11818 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11819
11820 test_155f() {
11821         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11822
11823         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11824
11825         save_writethrough $p
11826
11827         set_cache read on
11828         set_cache writethrough off
11829         test_155_big_load
11830         restore_lustre_params < $p
11831         rm -f $p
11832 }
11833 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11834
11835 test_155g() {
11836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11837
11838         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11839
11840         save_writethrough $p
11841
11842         set_cache read off
11843         set_cache writethrough on
11844         test_155_big_load
11845         restore_lustre_params < $p
11846         rm -f $p
11847 }
11848 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11849
11850 test_155h() {
11851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11852
11853         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11854
11855         save_writethrough $p
11856
11857         set_cache read off
11858         set_cache writethrough off
11859         test_155_big_load
11860         restore_lustre_params < $p
11861         rm -f $p
11862 }
11863 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11864
11865 test_156() {
11866         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11867         remote_ost_nodsh && skip "remote OST with nodsh"
11868         [ "$(facet_fstype ost1)" = "zfs" -a \
11869            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11870                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
11871
11872         local CPAGES=3
11873         local BEFORE
11874         local AFTER
11875         local file="$DIR/$tfile"
11876         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11877
11878         save_writethrough $p
11879         roc_hit_init
11880
11881         log "Turn on read and write cache"
11882         set_cache read on
11883         set_cache writethrough on
11884
11885         log "Write data and read it back."
11886         log "Read should be satisfied from the cache."
11887         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11888         BEFORE=$(roc_hit)
11889         cancel_lru_locks osc
11890         cat $file >/dev/null
11891         AFTER=$(roc_hit)
11892         if ! let "AFTER - BEFORE == CPAGES"; then
11893                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11894         else
11895                 log "cache hits:: before: $BEFORE, after: $AFTER"
11896         fi
11897
11898         log "Read again; it should be satisfied from the cache."
11899         BEFORE=$AFTER
11900         cancel_lru_locks osc
11901         cat $file >/dev/null
11902         AFTER=$(roc_hit)
11903         if ! let "AFTER - BEFORE == CPAGES"; then
11904                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11905         else
11906                 log "cache hits:: before: $BEFORE, after: $AFTER"
11907         fi
11908
11909         log "Turn off the read cache and turn on the write cache"
11910         set_cache read off
11911         set_cache writethrough on
11912
11913         log "Read again; it should be satisfied from the cache."
11914         BEFORE=$(roc_hit)
11915         cancel_lru_locks osc
11916         cat $file >/dev/null
11917         AFTER=$(roc_hit)
11918         if ! let "AFTER - BEFORE == CPAGES"; then
11919                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11920         else
11921                 log "cache hits:: before: $BEFORE, after: $AFTER"
11922         fi
11923
11924         log "Read again; it should not be satisfied from the cache."
11925         BEFORE=$AFTER
11926         cancel_lru_locks osc
11927         cat $file >/dev/null
11928         AFTER=$(roc_hit)
11929         if ! let "AFTER - BEFORE == 0"; then
11930                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11931         else
11932                 log "cache hits:: before: $BEFORE, after: $AFTER"
11933         fi
11934
11935         log "Write data and read it back."
11936         log "Read should be satisfied from the cache."
11937         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11938         BEFORE=$(roc_hit)
11939         cancel_lru_locks osc
11940         cat $file >/dev/null
11941         AFTER=$(roc_hit)
11942         if ! let "AFTER - BEFORE == CPAGES"; then
11943                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11944         else
11945                 log "cache hits:: before: $BEFORE, after: $AFTER"
11946         fi
11947
11948         log "Read again; it should not be satisfied from the cache."
11949         BEFORE=$AFTER
11950         cancel_lru_locks osc
11951         cat $file >/dev/null
11952         AFTER=$(roc_hit)
11953         if ! let "AFTER - BEFORE == 0"; then
11954                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11955         else
11956                 log "cache hits:: before: $BEFORE, after: $AFTER"
11957         fi
11958
11959         log "Turn off read and write cache"
11960         set_cache read off
11961         set_cache writethrough off
11962
11963         log "Write data and read it back"
11964         log "It should not be satisfied from the cache."
11965         rm -f $file
11966         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11967         cancel_lru_locks osc
11968         BEFORE=$(roc_hit)
11969         cat $file >/dev/null
11970         AFTER=$(roc_hit)
11971         if ! let "AFTER - BEFORE == 0"; then
11972                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11973         else
11974                 log "cache hits:: before: $BEFORE, after: $AFTER"
11975         fi
11976
11977         log "Turn on the read cache and turn off the write cache"
11978         set_cache read on
11979         set_cache writethrough off
11980
11981         log "Write data and read it back"
11982         log "It should not be satisfied from the cache."
11983         rm -f $file
11984         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11985         BEFORE=$(roc_hit)
11986         cancel_lru_locks osc
11987         cat $file >/dev/null
11988         AFTER=$(roc_hit)
11989         if ! let "AFTER - BEFORE == 0"; then
11990                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11991         else
11992                 log "cache hits:: before: $BEFORE, after: $AFTER"
11993         fi
11994
11995         log "Read again; it should be satisfied from the cache."
11996         BEFORE=$(roc_hit)
11997         cancel_lru_locks osc
11998         cat $file >/dev/null
11999         AFTER=$(roc_hit)
12000         if ! let "AFTER - BEFORE == CPAGES"; then
12001                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12002         else
12003                 log "cache hits:: before: $BEFORE, after: $AFTER"
12004         fi
12005
12006         restore_lustre_params < $p
12007         rm -f $p $file
12008 }
12009 run_test 156 "Verification of tunables"
12010
12011 test_160a() {
12012         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12013         remote_mds_nodsh && skip "remote MDS with nodsh"
12014         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
12015                 skip "Need MDS version at least 2.2.0"
12016
12017         changelog_register || error "changelog_register failed"
12018         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12019         changelog_users $SINGLEMDS | grep -q $cl_user ||
12020                 error "User $cl_user not found in changelog_users"
12021
12022         # change something
12023         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12024         changelog_clear 0 || error "changelog_clear failed"
12025         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12026         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12027         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12028         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12029         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12030         rm $DIR/$tdir/pics/desktop.jpg
12031
12032         changelog_dump | tail -10
12033
12034         echo "verifying changelog mask"
12035         changelog_chmask "-MKDIR"
12036         changelog_chmask "-CLOSE"
12037
12038         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12039         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12040
12041         changelog_chmask "+MKDIR"
12042         changelog_chmask "+CLOSE"
12043
12044         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12045         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12046
12047         changelog_dump | tail -10
12048         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12049         CLOSES=$(changelog_dump | grep -c "CLOSE")
12050         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12051         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12052
12053         # verify contents
12054         echo "verifying target fid"
12055         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12056         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12057         [ "$fidc" == "$fidf" ] ||
12058                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12059         echo "verifying parent fid"
12060         # The FID returned from the Changelog may be the directory shard on
12061         # a different MDT, and not the FID returned by path2fid on the parent.
12062         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12063         # since this is what will matter when recreating this file in the tree.
12064         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12065         local pathp=$($LFS fid2path $MOUNT "$fidp")
12066         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12067                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12068
12069         echo "getting records for $cl_user"
12070         changelog_users $SINGLEMDS
12071         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12072         local nclr=3
12073         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12074                 error "changelog_clear failed"
12075         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12076         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12077         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12078                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12079
12080         local min0_rec=$(changelog_users $SINGLEMDS |
12081                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12082         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12083                           awk '{ print $1; exit; }')
12084
12085         changelog_dump | tail -n 5
12086         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12087         [ $first_rec == $((min0_rec + 1)) ] ||
12088                 error "first index should be $min0_rec + 1 not $first_rec"
12089
12090         # LU-3446 changelog index reset on MDT restart
12091         local cur_rec1=$(changelog_users $SINGLEMDS |
12092                          awk '/^current.index:/ { print $NF }')
12093         changelog_clear 0 ||
12094                 error "clear all changelog records for $cl_user failed"
12095         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12096         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12097                 error "Fail to start $SINGLEMDS"
12098         local cur_rec2=$(changelog_users $SINGLEMDS |
12099                          awk '/^current.index:/ { print $NF }')
12100         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12101         [ $cur_rec1 == $cur_rec2 ] ||
12102                 error "current index should be $cur_rec1 not $cur_rec2"
12103
12104         echo "verifying users from this test are deregistered"
12105         changelog_deregister || error "changelog_deregister failed"
12106         changelog_users $SINGLEMDS | grep -q $cl_user &&
12107                 error "User '$cl_user' still in changelog_users"
12108
12109         # lctl get_param -n mdd.*.changelog_users
12110         # current index: 144
12111         # ID    index (idle seconds)
12112         # cl3   144 (2)
12113         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12114                 # this is the normal case where all users were deregistered
12115                 # make sure no new records are added when no users are present
12116                 local last_rec1=$(changelog_users $SINGLEMDS |
12117                                   awk '/^current.index:/ { print $NF }')
12118                 touch $DIR/$tdir/chloe
12119                 local last_rec2=$(changelog_users $SINGLEMDS |
12120                                   awk '/^current.index:/ { print $NF }')
12121                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
12122                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
12123         else
12124                 # any changelog users must be leftovers from a previous test
12125                 changelog_users $SINGLEMDS
12126                 echo "other changelog users; can't verify off"
12127         fi
12128 }
12129 run_test 160a "changelog sanity"
12130
12131 test_160b() { # LU-3587
12132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12133         remote_mds_nodsh && skip "remote MDS with nodsh"
12134         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
12135                 skip "Need MDS version at least 2.2.0"
12136
12137         changelog_register || error "changelog_register failed"
12138         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12139         changelog_users $SINGLEMDS | grep -q $cl_user ||
12140                 error "User '$cl_user' not found in changelog_users"
12141
12142         local longname1=$(str_repeat a 255)
12143         local longname2=$(str_repeat b 255)
12144
12145         cd $DIR
12146         echo "creating very long named file"
12147         touch $longname1 || error "create of '$longname1' failed"
12148         echo "renaming very long named file"
12149         mv $longname1 $longname2
12150
12151         changelog_dump | grep RENME | tail -n 5
12152         rm -f $longname2
12153 }
12154 run_test 160b "Verify that very long rename doesn't crash in changelog"
12155
12156 test_160c() {
12157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12158         remote_mds_nodsh && skip "remote MDS with nodsh"
12159
12160         local rc=0
12161         local server_version=$(lustre_version_code $SINGLEMDS)
12162
12163         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12164                 [[ $server_version -gt $(version_code 2.5.1) &&
12165                    $server_version -lt $(version_code 2.5.50) ]] ||
12166                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
12167
12168         # Registration step
12169         changelog_register || error "changelog_register failed"
12170
12171         rm -rf $DIR/$tdir
12172         mkdir -p $DIR/$tdir
12173         $MCREATE $DIR/$tdir/foo_160c
12174         changelog_chmask "-TRUNC"
12175         $TRUNCATE $DIR/$tdir/foo_160c 200
12176         changelog_chmask "+TRUNC"
12177         $TRUNCATE $DIR/$tdir/foo_160c 199
12178         changelog_dump | tail -n 5
12179         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
12180         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
12181 }
12182 run_test 160c "verify that changelog log catch the truncate event"
12183
12184 test_160d() {
12185         remote_mds_nodsh && skip "remote MDS with nodsh"
12186         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12188         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.60) ]] ||
12189                 skip "Need MDS version at least 2.7.60"
12190
12191         # Registration step
12192         changelog_register || error "changelog_register failed"
12193
12194         mkdir -p $DIR/$tdir/migrate_dir
12195         changelog_clear 0 || error "changelog_clear failed"
12196
12197         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
12198         changelog_dump | tail -n 5
12199         local migrates=$(changelog_dump | grep -c "MIGRT")
12200         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
12201 }
12202 run_test 160d "verify that changelog log catch the migrate event"
12203
12204 test_160e() {
12205         remote_mds_nodsh && skip "remote MDS with nodsh"
12206
12207         # Create a user
12208         changelog_register || error "changelog_register failed"
12209
12210         # Delete a future user (expect fail)
12211         local MDT0=$(facet_svc $SINGLEMDS)
12212         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
12213         local rc=$?
12214
12215         if [ $rc -eq 0 ]; then
12216                 error "Deleted non-existant user cl77"
12217         elif [ $rc -ne 2 ]; then
12218                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
12219         fi
12220
12221         # Clear to a bad index (1 billion should be safe)
12222         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
12223         rc=$?
12224
12225         if [ $rc -eq 0 ]; then
12226                 error "Successfully cleared to invalid CL index"
12227         elif [ $rc -ne 22 ]; then
12228                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
12229         fi
12230 }
12231 run_test 160e "changelog negative testing (should return errors)"
12232
12233 test_160f() {
12234         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12235         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12236                 { skip "Need MDS version at least 2.10.56"; return 0; }
12237
12238         local mdts=$(comma_list $(mdts_nodes))
12239
12240         # Create a user
12241         changelog_register || error "first changelog_register failed"
12242         changelog_register || error "second changelog_register failed"
12243         local cl_users
12244         declare -A cl_user1
12245         declare -A cl_user2
12246         local user_rec1
12247         local user_rec2
12248         local i
12249
12250         # generate some changelog records to accumulate on each MDT
12251         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12252         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12253                 error "create $DIR/$tdir/$tfile failed"
12254
12255         # check changelogs have been generated
12256         local nbcl=$(changelog_dump | wc -l)
12257         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12258
12259         for param in "changelog_max_idle_time=10" \
12260                      "changelog_gc=1" \
12261                      "changelog_min_gc_interval=2" \
12262                      "changelog_min_free_cat_entries=3"; do
12263                 local MDT0=$(facet_svc $SINGLEMDS)
12264                 local var="${param%=*}"
12265                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12266
12267                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12268                 do_nodes $mdts $LCTL set_param mdd.*.$param
12269         done
12270
12271         # force cl_user2 to be idle (1st part)
12272         sleep 9
12273
12274         # simulate changelog catalog almost full
12275         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12276         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12277
12278         for i in $(seq $MDSCOUNT); do
12279                 cl_users=(${CL_USERS[mds$i]})
12280                 cl_user1[mds$i]="${cl_users[0]}"
12281                 cl_user2[mds$i]="${cl_users[1]}"
12282
12283                 [ -n "${cl_user1[mds$i]}" ] ||
12284                         error "mds$i: no user registered"
12285                 [ -n "${cl_user2[mds$i]}" ] ||
12286                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12287
12288                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12289                 [ -n "$user_rec1" ] ||
12290                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12291                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12292                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12293                 [ -n "$user_rec2" ] ||
12294                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12295                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12296                      "$user_rec1 + 2 == $user_rec2"
12297                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12298                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12299                               "$user_rec1 + 2, but is $user_rec2"
12300                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12301                 [ -n "$user_rec2" ] ||
12302                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12303                 [ $user_rec1 == $user_rec2 ] ||
12304                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12305                               "$user_rec1, but is $user_rec2"
12306         done
12307
12308         # force cl_user2 to be idle (2nd part) and to reach
12309         # changelog_max_idle_time
12310         sleep 2
12311
12312         # generate one more changelog to trigger fail_loc
12313         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12314                 error "create $DIR/$tdir/${tfile}bis failed"
12315
12316         # ensure gc thread is done
12317         for i in $(mdts_nodes); do
12318                 wait_update $i \
12319                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12320                         error "$i: GC-thread not done"
12321         done
12322
12323         local first_rec
12324         for i in $(seq $MDSCOUNT); do
12325                 # check cl_user1 still registered
12326                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12327                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12328                 # check cl_user2 unregistered
12329                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12330                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12331
12332                 # check changelogs are present and starting at $user_rec1 + 1
12333                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12334                 [ -n "$user_rec1" ] ||
12335                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12336                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12337                             awk '{ print $1; exit; }')
12338
12339                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12340                 [ $((user_rec1 + 1)) == $first_rec ] ||
12341                         error "mds$i: first index should be $user_rec1 + 1, " \
12342                               "but is $first_rec"
12343         done
12344 }
12345 run_test 160f "changelog garbage collect (timestamped users)"
12346
12347 test_160g() {
12348         remote_mds_nodsh && skip "remote MDS with nodsh"
12349         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12350                 skip "Need MDS version at least 2.10.56"
12351
12352         local mdts=$(comma_list $(mdts_nodes))
12353
12354         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
12355         do_nodes $mdts $LCTL set_param fail_loc=0x1314
12356
12357         # Create a user
12358         changelog_register || error "first changelog_register failed"
12359         changelog_register || error "second changelog_register failed"
12360         local cl_users=(${CL_USERS[$SINGLEMDS]})
12361         local cl_user1="${cl_users[0]}"
12362         local cl_user2="${cl_users[1]}"
12363
12364         # generate some changelog records to accumulate on each MDT
12365         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12366         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12367                 error "create $DIR/$tdir/$tfile failed"
12368
12369         # check changelogs have been generated
12370         nbcl=$(changelog_dump | wc -l)
12371         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12372
12373         # changelog_gc=1 should be set by default
12374         for param in "changelog_max_idle_indexes=$((nbcl / 2))" \
12375                      "changelog_min_gc_interval=2" \
12376                      "changelog_min_free_cat_entries=3"; do
12377                 local MDT0=$(facet_svc $SINGLEMDS)
12378                 local var="${param%=*}"
12379                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12380
12381                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12382                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
12383                         error "unable to set mdd.*.$param"
12384         done
12385
12386         # simulate changelog catalog almost full
12387         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12388         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12389
12390         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user1)
12391
12392         __changelog_clear $SINGLEMDS $cl_user1 +3
12393
12394         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user1)
12395
12396         echo "verifying user clear: $user_rec1 + 3 == $user_rec2"
12397         [ $((user_rec1 + 3)) == $user_rec2 ] ||
12398                 error "user index expected $user_rec1 + 3, but is $user_rec2"
12399
12400         # generate one more changelog to trigger fail_loc
12401         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
12402
12403         # ensure gc thread is done
12404         wait_update_facet $SINGLEMDS \
12405                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
12406
12407         # check user still registered
12408         [ -n "$(changelog_user_rec $SINGLEMDS $cl_user1)" ] ||
12409                 error "User $cl_user1 not found in changelog_users"
12410         # check user2 unregistered
12411         [ -z "$(changelog_user_rec $SINGLEMDS $cl_user2)" ] ||
12412                 error "User $cl_user2 still found in changelog_users"
12413
12414         # check changelogs are present and starting at $user_rec2 + 1
12415         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12416                           awk '{ print $1; exit; }')
12417
12418         echo "verifying min purge: $user_rec2 + 1 == $first_rec"
12419         [ $((user_rec2 + 1)) == $first_rec ] ||
12420                 error "first index should be $user_rec2 + 1, but is $first_rec"
12421 }
12422 run_test 160g "changelog garbage collect (old users)"
12423
12424 test_160h() {
12425         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12426         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12427                 { skip "Need MDS version at least 2.10.56"; return 0; }
12428
12429         local mdts=$(comma_list $(mdts_nodes))
12430
12431         # Create a user
12432         changelog_register || error "first changelog_register failed"
12433         changelog_register || error "second changelog_register failed"
12434         local cl_users
12435         declare -A cl_user1
12436         declare -A cl_user2
12437         local user_rec1
12438         local user_rec2
12439         local i
12440
12441         # generate some changelog records to accumulate on each MDT
12442         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12443         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12444                 error "create $DIR/$tdir/$tfile failed"
12445
12446         # check changelogs have been generated
12447         local nbcl=$(changelog_dump | wc -l)
12448         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12449
12450         for param in "changelog_max_idle_time=10" \
12451                      "changelog_gc=1" \
12452                      "changelog_min_gc_interval=2"; do
12453                 local MDT0=$(facet_svc $SINGLEMDS)
12454                 local var="${param%=*}"
12455                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12456
12457                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12458                 do_nodes $mdts $LCTL set_param mdd.*.$param
12459         done
12460
12461         # force cl_user2 to be idle (1st part)
12462         sleep 9
12463
12464         for i in $(seq $MDSCOUNT); do
12465                 cl_users=(${CL_USERS[mds$i]})
12466                 cl_user1[mds$i]="${cl_users[0]}"
12467                 cl_user2[mds$i]="${cl_users[1]}"
12468
12469                 [ -n "${cl_user1[mds$i]}" ] ||
12470                         error "mds$i: no user registered"
12471                 [ -n "${cl_user2[mds$i]}" ] ||
12472                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12473
12474                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12475                 [ -n "$user_rec1" ] ||
12476                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12477                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12478                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12479                 [ -n "$user_rec2" ] ||
12480                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12481                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12482                      "$user_rec1 + 2 == $user_rec2"
12483                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12484                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12485                               "$user_rec1 + 2, but is $user_rec2"
12486                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12487                 [ -n "$user_rec2" ] ||
12488                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12489                 [ $user_rec1 == $user_rec2 ] ||
12490                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12491                               "$user_rec1, but is $user_rec2"
12492         done
12493
12494         # force cl_user2 to be idle (2nd part) and to reach
12495         # changelog_max_idle_time
12496         sleep 2
12497
12498         # force each GC-thread start and block then
12499         # one per MDT/MDD, set fail_val accordingly
12500         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
12501         do_nodes $mdts $LCTL set_param fail_loc=0x1316
12502
12503         # generate more changelogs to trigger fail_loc
12504         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12505                 error "create $DIR/$tdir/${tfile}bis failed"
12506
12507         # stop MDT to stop GC-thread, should be done in back-ground as it will
12508         # block waiting for the thread to be released and exit
12509         declare -A stop_pids
12510         for i in $(seq $MDSCOUNT); do
12511                 stop mds$i &
12512                 stop_pids[mds$i]=$!
12513         done
12514
12515         for i in $(mdts_nodes); do
12516                 local facet
12517                 local nb=0
12518                 local facets=$(facets_up_on_host $i)
12519
12520                 for facet in ${facets//,/ }; do
12521                         if [[ $facet == mds* ]]; then
12522                                 nb=$((nb + 1))
12523                         fi
12524                 done
12525                 # ensure each MDS's gc threads are still present and all in "R"
12526                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
12527                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
12528                         error "$i: expected $nb GC-thread"
12529                 wait_update $i \
12530                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
12531                         "R" 20 ||
12532                         error "$i: GC-thread not found in R-state"
12533                 # check umounts of each MDT on MDS have reached kthread_stop()
12534                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
12535                         error "$i: expected $nb umount"
12536                 wait_update $i \
12537                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
12538                         error "$i: umount not found in D-state"
12539         done
12540
12541         # release all GC-threads
12542         do_nodes $mdts $LCTL set_param fail_loc=0
12543
12544         # wait for MDT stop to complete
12545         for i in $(seq $MDSCOUNT); do
12546                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
12547         done
12548
12549         # XXX
12550         # may try to check if any orphan changelog records are present
12551         # via ldiskfs/zfs and llog_reader...
12552
12553         # re-start/mount MDTs
12554         for i in $(seq $MDSCOUNT); do
12555                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
12556                         error "Fail to start mds$i"
12557         done
12558
12559         local first_rec
12560         for i in $(seq $MDSCOUNT); do
12561                 # check cl_user1 still registered
12562                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12563                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12564                 # check cl_user2 unregistered
12565                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12566                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12567
12568                 # check changelogs are present and starting at $user_rec1 + 1
12569                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12570                 [ -n "$user_rec1" ] ||
12571                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12572                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12573                             awk '{ print $1; exit; }')
12574
12575                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12576                 [ $((user_rec1 + 1)) == $first_rec ] ||
12577                         error "mds$i: first index should be $user_rec1 + 1, " \
12578                               "but is $first_rec"
12579         done
12580 }
12581 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
12582               "during mount"
12583
12584 test_160i() {
12585
12586         local mdts=$(comma_list $(mdts_nodes))
12587
12588         changelog_register || error "first changelog_register failed"
12589
12590         # generate some changelog records to accumulate on each MDT
12591         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12592         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12593                 error "create $DIR/$tdir/$tfile failed"
12594
12595         # check changelogs have been generated
12596         local nbcl=$(changelog_dump | wc -l)
12597         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12598
12599         # simulate race between register and unregister
12600         # XXX as fail_loc is set per-MDS, with DNE configs the race
12601         # simulation will only occur for one MDT per MDS and for the
12602         # others the normal race scenario will take place
12603         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
12604         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
12605         do_nodes $mdts $LCTL set_param fail_val=1
12606
12607         # unregister 1st user
12608         changelog_deregister &
12609         local pid1=$!
12610         # wait some time for deregister work to reach race rdv
12611         sleep 2
12612         # register 2nd user
12613         changelog_register || error "2nd user register failed"
12614
12615         wait $pid1 || error "1st user deregister failed"
12616
12617         local i
12618         local last_rec
12619         declare -A LAST_REC
12620         for i in $(seq $MDSCOUNT); do
12621                 if changelog_users mds$i | grep "^cl"; then
12622                         # make sure new records are added with one user present
12623                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
12624                                           awk '/^current.index:/ { print $NF }')
12625                 else
12626                         error "mds$i has no user registered"
12627                 fi
12628         done
12629
12630         # generate more changelog records to accumulate on each MDT
12631         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12632                 error "create $DIR/$tdir/${tfile}bis failed"
12633
12634         for i in $(seq $MDSCOUNT); do
12635                 last_rec=$(changelog_users $SINGLEMDS |
12636                            awk '/^current.index:/ { print $NF }')
12637                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
12638                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
12639                         error "changelogs are off on mds$i"
12640         done
12641 }
12642 run_test 160i "changelog user register/unregister race"
12643
12644 test_161a() {
12645         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12646
12647         test_mkdir -c1 $DIR/$tdir
12648         cp /etc/hosts $DIR/$tdir/$tfile
12649         test_mkdir -c1 $DIR/$tdir/foo1
12650         test_mkdir -c1 $DIR/$tdir/foo2
12651         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
12652         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
12653         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
12654         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
12655         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
12656         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12657                 $LFS fid2path $DIR $FID
12658                 error "bad link ea"
12659         fi
12660         # middle
12661         rm $DIR/$tdir/foo2/zachary
12662         # last
12663         rm $DIR/$tdir/foo2/thor
12664         # first
12665         rm $DIR/$tdir/$tfile
12666         # rename
12667         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
12668         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
12669                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
12670         rm $DIR/$tdir/foo2/maggie
12671
12672         # overflow the EA
12673         local longname=$tfile.avg_len_is_thirty_two_
12674         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
12675                 error_noexit 'failed to unlink many hardlinks'" EXIT
12676         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
12677                 error "failed to hardlink many files"
12678         links=$($LFS fid2path $DIR $FID | wc -l)
12679         echo -n "${links}/1000 links in link EA"
12680         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
12681 }
12682 run_test 161a "link ea sanity"
12683
12684 test_161b() {
12685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12686         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
12687
12688         local MDTIDX=1
12689         local remote_dir=$DIR/$tdir/remote_dir
12690
12691         mkdir -p $DIR/$tdir
12692         $LFS mkdir -i $MDTIDX $remote_dir ||
12693                 error "create remote directory failed"
12694
12695         cp /etc/hosts $remote_dir/$tfile
12696         mkdir -p $remote_dir/foo1
12697         mkdir -p $remote_dir/foo2
12698         ln $remote_dir/$tfile $remote_dir/foo1/sofia
12699         ln $remote_dir/$tfile $remote_dir/foo2/zachary
12700         ln $remote_dir/$tfile $remote_dir/foo1/luna
12701         ln $remote_dir/$tfile $remote_dir/foo2/thor
12702
12703         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
12704                      tr -d ']')
12705         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12706                 $LFS fid2path $DIR $FID
12707                 error "bad link ea"
12708         fi
12709         # middle
12710         rm $remote_dir/foo2/zachary
12711         # last
12712         rm $remote_dir/foo2/thor
12713         # first
12714         rm $remote_dir/$tfile
12715         # rename
12716         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
12717         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
12718         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
12719                 $LFS fid2path $DIR $FID
12720                 error "bad link rename"
12721         fi
12722         rm $remote_dir/foo2/maggie
12723
12724         # overflow the EA
12725         local longname=filename_avg_len_is_thirty_two_
12726         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
12727                 error "failed to hardlink many files"
12728         links=$($LFS fid2path $DIR $FID | wc -l)
12729         echo -n "${links}/1000 links in link EA"
12730         [[ ${links} -gt 60 ]] ||
12731                 error "expected at least 60 links in link EA"
12732         unlinkmany $remote_dir/foo2/$longname 1000 ||
12733         error "failed to unlink many hardlinks"
12734 }
12735 run_test 161b "link ea sanity under remote directory"
12736
12737 test_161c() {
12738         remote_mds_nodsh && skip "remote MDS with nodsh"
12739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12740         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
12741                 skip "Need MDS version at least 2.1.5"
12742
12743         # define CLF_RENAME_LAST 0x0001
12744         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
12745         changelog_register || error "changelog_register failed"
12746
12747         rm -rf $DIR/$tdir
12748         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
12749         touch $DIR/$tdir/foo_161c
12750         touch $DIR/$tdir/bar_161c
12751         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12752         changelog_dump | grep RENME | tail -n 5
12753         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
12754         changelog_clear 0 || error "changelog_clear failed"
12755         if [ x$flags != "x0x1" ]; then
12756                 error "flag $flags is not 0x1"
12757         fi
12758
12759         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
12760         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
12761         touch $DIR/$tdir/foo_161c
12762         touch $DIR/$tdir/bar_161c
12763         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12764         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12765         changelog_dump | grep RENME | tail -n 5
12766         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
12767         changelog_clear 0 || error "changelog_clear failed"
12768         if [ x$flags != "x0x0" ]; then
12769                 error "flag $flags is not 0x0"
12770         fi
12771         echo "rename overwrite a target having nlink > 1," \
12772                 "changelog record has flags of $flags"
12773
12774         # rename doesn't overwrite a target (changelog flag 0x0)
12775         touch $DIR/$tdir/foo_161c
12776         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
12777         changelog_dump | grep RENME | tail -n 5
12778         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
12779         changelog_clear 0 || error "changelog_clear failed"
12780         if [ x$flags != "x0x0" ]; then
12781                 error "flag $flags is not 0x0"
12782         fi
12783         echo "rename doesn't overwrite a target," \
12784                 "changelog record has flags of $flags"
12785
12786         # define CLF_UNLINK_LAST 0x0001
12787         # unlink a file having nlink = 1 (changelog flag 0x1)
12788         rm -f $DIR/$tdir/foo2_161c
12789         changelog_dump | grep UNLNK | tail -n 5
12790         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
12791         changelog_clear 0 || error "changelog_clear failed"
12792         if [ x$flags != "x0x1" ]; then
12793                 error "flag $flags is not 0x1"
12794         fi
12795         echo "unlink a file having nlink = 1," \
12796                 "changelog record has flags of $flags"
12797
12798         # unlink a file having nlink > 1 (changelog flag 0x0)
12799         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12800         rm -f $DIR/$tdir/foobar_161c
12801         changelog_dump | grep UNLNK | tail -n 5
12802         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
12803         changelog_clear 0 || error "changelog_clear failed"
12804         if [ x$flags != "x0x0" ]; then
12805                 error "flag $flags is not 0x0"
12806         fi
12807         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
12808 }
12809 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
12810
12811 test_161d() {
12812         remote_mds_nodsh && skip "remote MDS with nodsh"
12813
12814         local pid
12815         local fid
12816
12817         changelog_register || error "changelog_register failed"
12818
12819         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
12820         # interfer with $MOUNT/.lustre/fid/ access
12821         mkdir $DIR/$tdir
12822         [[ $? -eq 0 ]] || error "mkdir failed"
12823
12824         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
12825         $LCTL set_param fail_loc=0x8000140c
12826         # 5s pause
12827         $LCTL set_param fail_val=5
12828
12829         # create file
12830         echo foofoo > $DIR/$tdir/$tfile &
12831         pid=$!
12832
12833         # wait for create to be delayed
12834         sleep 2
12835
12836         ps -p $pid
12837         [[ $? -eq 0 ]] || error "create should be blocked"
12838
12839         local tempfile=$(mktemp)
12840         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
12841         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
12842         # some delay may occur during ChangeLog publishing and file read just
12843         # above, that could allow file write to happen finally
12844         [[ -s $tempfile ]] && echo "file should be empty"
12845
12846         $LCTL set_param fail_loc=0
12847
12848         wait $pid
12849         [[ $? -eq 0 ]] || error "create failed"
12850 }
12851 run_test 161d "create with concurrent .lustre/fid access"
12852
12853 check_path() {
12854         local expected="$1"
12855         shift
12856         local fid="$2"
12857
12858         local path
12859         path=$($LFS fid2path "$@")
12860         local rc=$?
12861
12862         if [ $rc -ne 0 ]; then
12863                 error "path looked up of '$expected' failed: rc=$rc"
12864         elif [ "$path" != "$expected" ]; then
12865                 error "path looked up '$path' instead of '$expected'"
12866         else
12867                 echo "FID '$fid' resolves to path '$path' as expected"
12868         fi
12869 }
12870
12871 test_162a() { # was test_162
12872         test_mkdir -p -c1 $DIR/$tdir/d2
12873         touch $DIR/$tdir/d2/$tfile
12874         touch $DIR/$tdir/d2/x1
12875         touch $DIR/$tdir/d2/x2
12876         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
12877         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
12878         # regular file
12879         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
12880         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
12881
12882         # softlink
12883         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
12884         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
12885         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
12886
12887         # softlink to wrong file
12888         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
12889         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
12890         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
12891
12892         # hardlink
12893         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
12894         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
12895         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
12896         # fid2path dir/fsname should both work
12897         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
12898         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
12899
12900         # hardlink count: check that there are 2 links
12901         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
12902         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
12903
12904         # hardlink indexing: remove the first link
12905         rm $DIR/$tdir/d2/p/q/r/hlink
12906         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
12907 }
12908 run_test 162a "path lookup sanity"
12909
12910 test_162b() {
12911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12912         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12913
12914         mkdir $DIR/$tdir
12915         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
12916                                 error "create striped dir failed"
12917
12918         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
12919                                         tail -n 1 | awk '{print $2}')
12920         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
12921
12922         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
12923         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
12924
12925         # regular file
12926         for ((i=0;i<5;i++)); do
12927                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12928                         error "get fid for f$i failed"
12929                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
12930
12931                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12932                         error "get fid for d$i failed"
12933                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
12934         done
12935
12936         return 0
12937 }
12938 run_test 162b "striped directory path lookup sanity"
12939
12940 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12941 test_162c() {
12942         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12943                 skip "Need MDS version at least 2.7.51"
12944
12945         local lpath=$tdir.local
12946         local rpath=$tdir.remote
12947
12948         test_mkdir $DIR/$lpath
12949         test_mkdir $DIR/$rpath
12950
12951         for ((i = 0; i <= 101; i++)); do
12952                 lpath="$lpath/$i"
12953                 mkdir $DIR/$lpath
12954                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12955                         error "get fid for local directory $DIR/$lpath failed"
12956                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
12957
12958                 rpath="$rpath/$i"
12959                 test_mkdir $DIR/$rpath
12960                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
12961                         error "get fid for remote directory $DIR/$rpath failed"
12962                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
12963         done
12964
12965         return 0
12966 }
12967 run_test 162c "fid2path works with paths 100 or more directories deep"
12968
12969 test_169() {
12970         # do directio so as not to populate the page cache
12971         log "creating a 10 Mb file"
12972         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12973         log "starting reads"
12974         dd if=$DIR/$tfile of=/dev/null bs=4096 &
12975         log "truncating the file"
12976         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12977         log "killing dd"
12978         kill %+ || true # reads might have finished
12979         echo "wait until dd is finished"
12980         wait
12981         log "removing the temporary file"
12982         rm -rf $DIR/$tfile || error "tmp file removal failed"
12983 }
12984 run_test 169 "parallel read and truncate should not deadlock"
12985
12986 test_170() {
12987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12988
12989         $LCTL clear     # bug 18514
12990         $LCTL debug_daemon start $TMP/${tfile}_log_good
12991         touch $DIR/$tfile
12992         $LCTL debug_daemon stop
12993         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
12994                 error "sed failed to read log_good"
12995
12996         $LCTL debug_daemon start $TMP/${tfile}_log_good
12997         rm -rf $DIR/$tfile
12998         $LCTL debug_daemon stop
12999
13000         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13001                error "lctl df log_bad failed"
13002
13003         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13004         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13005
13006         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13007         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13008
13009         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13010                 error "bad_line good_line1 good_line2 are empty"
13011
13012         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13013         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
13014         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13015
13016         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
13017         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13018         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13019
13020         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
13021                 error "bad_line_new good_line_new are empty"
13022
13023         local expected_good=$((good_line1 + good_line2*2))
13024
13025         rm -f $TMP/${tfile}*
13026         # LU-231, short malformed line may not be counted into bad lines
13027         if [ $bad_line -ne $bad_line_new ] &&
13028                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
13029                 error "expected $bad_line bad lines, but got $bad_line_new"
13030                 return 1
13031         fi
13032
13033         if [ $expected_good -ne $good_line_new ]; then
13034                 error "expected $expected_good good lines, but got $good_line_new"
13035                 return 2
13036         fi
13037         true
13038 }
13039 run_test 170 "test lctl df to handle corrupted log ====================="
13040
13041 test_171() { # bug20592
13042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13043
13044         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
13045         $LCTL set_param fail_loc=0x50e
13046         $LCTL set_param fail_val=3000
13047         multiop_bg_pause $DIR/$tfile O_s || true
13048         local MULTIPID=$!
13049         kill -USR1 $MULTIPID
13050         # cause log dump
13051         sleep 3
13052         wait $MULTIPID
13053         if dmesg | grep "recursive fault"; then
13054                 error "caught a recursive fault"
13055         fi
13056         $LCTL set_param fail_loc=0
13057         true
13058 }
13059 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
13060
13061 # it would be good to share it with obdfilter-survey/iokit-libecho code
13062 setup_obdecho_osc () {
13063         local rc=0
13064         local ost_nid=$1
13065         local obdfilter_name=$2
13066         echo "Creating new osc for $obdfilter_name on $ost_nid"
13067         # make sure we can find loopback nid
13068         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
13069
13070         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
13071                            ${obdfilter_name}_osc_UUID || rc=2; }
13072         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
13073                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
13074         return $rc
13075 }
13076
13077 cleanup_obdecho_osc () {
13078         local obdfilter_name=$1
13079         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
13080         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
13081         return 0
13082 }
13083
13084 obdecho_test() {
13085         local OBD=$1
13086         local node=$2
13087         local pages=${3:-64}
13088         local rc=0
13089         local id
13090
13091         local count=10
13092         local obd_size=$(get_obd_size $node $OBD)
13093         local page_size=$(get_page_size $node)
13094         if [[ -n "$obd_size" ]]; then
13095                 local new_count=$((obd_size / (pages * page_size / 1024)))
13096                 [[ $new_count -ge $count ]] || count=$new_count
13097         fi
13098
13099         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
13100         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
13101                            rc=2; }
13102         if [ $rc -eq 0 ]; then
13103             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
13104             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
13105         fi
13106         echo "New object id is $id"
13107         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
13108                            rc=4; }
13109         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
13110                            "test_brw $count w v $pages $id" || rc=4; }
13111         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
13112                            rc=4; }
13113         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
13114                                         "cleanup" || rc=5; }
13115         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
13116                                         "detach" || rc=6; }
13117         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
13118         return $rc
13119 }
13120
13121 test_180a() {
13122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13123
13124         if ! module_loaded obdecho; then
13125                 load_module obdecho/obdecho &&
13126                         stack_trap "rmmod obdecho" EXIT ||
13127                         error "unable to load obdecho on client"
13128         fi
13129
13130         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
13131         local host=$($LCTL get_param -n osc.$osc.import |
13132                      awk '/current_connection:/ { print $2 }' )
13133         local target=$($LCTL get_param -n osc.$osc.import |
13134                        awk '/target:/ { print $2 }' )
13135         target=${target%_UUID}
13136
13137         if [ -n "$target" ]; then
13138                 setup_obdecho_osc $host $target &&
13139                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
13140                         { error "obdecho setup failed with $?"; return; }
13141
13142                 obdecho_test ${target}_osc client ||
13143                         error "obdecho_test failed on ${target}_osc"
13144         else
13145                 $LCTL get_param osc.$osc.import
13146                 error "there is no osc.$osc.import target"
13147         fi
13148 }
13149 run_test 180a "test obdecho on osc"
13150
13151 test_180b() {
13152         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13153         remote_ost_nodsh && skip "remote OST with nodsh"
13154
13155         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13156                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13157                 error "failed to load module obdecho"
13158
13159         local target=$(do_facet ost1 $LCTL dl |
13160                        awk '/obdfilter/ { print $4; exit; }')
13161
13162         if [ -n "$target" ]; then
13163                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
13164         else
13165                 do_facet ost1 $LCTL dl
13166                 error "there is no obdfilter target on ost1"
13167         fi
13168 }
13169 run_test 180b "test obdecho directly on obdfilter"
13170
13171 test_180c() { # LU-2598
13172         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13173         remote_ost_nodsh && skip "remote OST with nodsh"
13174         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
13175                 skip "Need MDS version at least 2.4.0"
13176
13177         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13178                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13179                 error "failed to load module obdecho"
13180
13181         local target=$(do_facet ost1 $LCTL dl |
13182                        awk '/obdfilter/ { print $4; exit; }')
13183
13184         if [ -n "$target" ]; then
13185                 local pages=16384 # 64MB bulk I/O RPC size
13186
13187                 obdecho_test "$target" ost1 "$pages" ||
13188                         error "obdecho_test with pages=$pages failed with $?"
13189         else
13190                 do_facet ost1 $LCTL dl
13191                 error "there is no obdfilter target on ost1"
13192         fi
13193 }
13194 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
13195
13196 test_181() { # bug 22177
13197         test_mkdir $DIR/$tdir
13198         # create enough files to index the directory
13199         createmany -o $DIR/$tdir/foobar 4000
13200         # print attributes for debug purpose
13201         lsattr -d .
13202         # open dir
13203         multiop_bg_pause $DIR/$tdir D_Sc || return 1
13204         MULTIPID=$!
13205         # remove the files & current working dir
13206         unlinkmany $DIR/$tdir/foobar 4000
13207         rmdir $DIR/$tdir
13208         kill -USR1 $MULTIPID
13209         wait $MULTIPID
13210         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
13211         return 0
13212 }
13213 run_test 181 "Test open-unlinked dir ========================"
13214
13215 test_182() {
13216         local fcount=1000
13217         local tcount=10
13218
13219         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13220
13221         $LCTL set_param mdc.*.rpc_stats=clear
13222
13223         for (( i = 0; i < $tcount; i++ )) ; do
13224                 mkdir $DIR/$tdir/$i
13225         done
13226
13227         for (( i = 0; i < $tcount; i++ )) ; do
13228                 createmany -o $DIR/$tdir/$i/f- $fcount &
13229         done
13230         wait
13231
13232         for (( i = 0; i < $tcount; i++ )) ; do
13233                 unlinkmany $DIR/$tdir/$i/f- $fcount &
13234         done
13235         wait
13236
13237         $LCTL get_param mdc.*.rpc_stats
13238
13239         rm -rf $DIR/$tdir
13240 }
13241 run_test 182 "Test parallel modify metadata operations ================"
13242
13243 test_183() { # LU-2275
13244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13245         remote_mds_nodsh && skip "remote MDS with nodsh"
13246         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
13247                 skip "Need MDS version at least 2.3.56"
13248
13249         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13250         echo aaa > $DIR/$tdir/$tfile
13251
13252 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
13253         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
13254
13255         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
13256         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
13257
13258         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13259
13260         # Flush negative dentry cache
13261         touch $DIR/$tdir/$tfile
13262
13263         # We are not checking for any leaked references here, they'll
13264         # become evident next time we do cleanup with module unload.
13265         rm -rf $DIR/$tdir
13266 }
13267 run_test 183 "No crash or request leak in case of strange dispositions ========"
13268
13269 # test suite 184 is for LU-2016, LU-2017
13270 test_184a() {
13271         check_swap_layouts_support
13272
13273         dir0=$DIR/$tdir/$testnum
13274         test_mkdir -p -c1 $dir0
13275         ref1=/etc/passwd
13276         ref2=/etc/group
13277         file1=$dir0/f1
13278         file2=$dir0/f2
13279         $SETSTRIPE -c1 $file1
13280         cp $ref1 $file1
13281         $SETSTRIPE -c2 $file2
13282         cp $ref2 $file2
13283         gen1=$($GETSTRIPE -g $file1)
13284         gen2=$($GETSTRIPE -g $file2)
13285
13286         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
13287         gen=$($GETSTRIPE -g $file1)
13288         [[ $gen1 != $gen ]] ||
13289                 "Layout generation on $file1 does not change"
13290         gen=$($GETSTRIPE -g $file2)
13291         [[ $gen2 != $gen ]] ||
13292                 "Layout generation on $file2 does not change"
13293
13294         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
13295         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13296
13297         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
13298 }
13299 run_test 184a "Basic layout swap"
13300
13301 test_184b() {
13302         check_swap_layouts_support
13303
13304         dir0=$DIR/$tdir/$testnum
13305         mkdir -p $dir0 || error "creating dir $dir0"
13306         file1=$dir0/f1
13307         file2=$dir0/f2
13308         file3=$dir0/f3
13309         dir1=$dir0/d1
13310         dir2=$dir0/d2
13311         mkdir $dir1 $dir2
13312         $SETSTRIPE -c1 $file1
13313         $SETSTRIPE -c2 $file2
13314         $SETSTRIPE -c1 $file3
13315         chown $RUNAS_ID $file3
13316         gen1=$($GETSTRIPE -g $file1)
13317         gen2=$($GETSTRIPE -g $file2)
13318
13319         $LFS swap_layouts $dir1 $dir2 &&
13320                 error "swap of directories layouts should fail"
13321         $LFS swap_layouts $dir1 $file1 &&
13322                 error "swap of directory and file layouts should fail"
13323         $RUNAS $LFS swap_layouts $file1 $file2 &&
13324                 error "swap of file we cannot write should fail"
13325         $LFS swap_layouts $file1 $file3 &&
13326                 error "swap of file with different owner should fail"
13327         /bin/true # to clear error code
13328 }
13329 run_test 184b "Forbidden layout swap (will generate errors)"
13330
13331 test_184c() {
13332         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
13333         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
13334         check_swap_layouts_support
13335
13336         local dir0=$DIR/$tdir/$testnum
13337         mkdir -p $dir0 || error "creating dir $dir0"
13338
13339         local ref1=$dir0/ref1
13340         local ref2=$dir0/ref2
13341         local file1=$dir0/file1
13342         local file2=$dir0/file2
13343         # create a file large enough for the concurrent test
13344         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
13345         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
13346         echo "ref file size: ref1($(stat -c %s $ref1))," \
13347              "ref2($(stat -c %s $ref2))"
13348
13349         cp $ref2 $file2
13350         dd if=$ref1 of=$file1 bs=16k &
13351         local DD_PID=$!
13352
13353         # Make sure dd starts to copy file
13354         while [ ! -f $file1 ]; do sleep 0.1; done
13355
13356         $LFS swap_layouts $file1 $file2
13357         local rc=$?
13358         wait $DD_PID
13359         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
13360         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
13361
13362         # how many bytes copied before swapping layout
13363         local copied=$(stat -c %s $file2)
13364         local remaining=$(stat -c %s $ref1)
13365         remaining=$((remaining - copied))
13366         echo "Copied $copied bytes before swapping layout..."
13367
13368         cmp -n $copied $file1 $ref2 | grep differ &&
13369                 error "Content mismatch [0, $copied) of ref2 and file1"
13370         cmp -n $copied $file2 $ref1 ||
13371                 error "Content mismatch [0, $copied) of ref1 and file2"
13372         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
13373                 error "Content mismatch [$copied, EOF) of ref1 and file1"
13374
13375         # clean up
13376         rm -f $ref1 $ref2 $file1 $file2
13377 }
13378 run_test 184c "Concurrent write and layout swap"
13379
13380 test_184d() {
13381         check_swap_layouts_support
13382         [ -z "$(which getfattr 2>/dev/null)" ] &&
13383                 skip_env "no getfattr command"
13384
13385         local file1=$DIR/$tdir/$tfile-1
13386         local file2=$DIR/$tdir/$tfile-2
13387         local file3=$DIR/$tdir/$tfile-3
13388         local lovea1
13389         local lovea2
13390
13391         mkdir -p $DIR/$tdir
13392         touch $file1 || error "create $file1 failed"
13393         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13394                 error "create $file2 failed"
13395         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13396                 error "create $file3 failed"
13397         lovea1=$(get_layout_param $file1)
13398
13399         $LFS swap_layouts $file2 $file3 ||
13400                 error "swap $file2 $file3 layouts failed"
13401         $LFS swap_layouts $file1 $file2 ||
13402                 error "swap $file1 $file2 layouts failed"
13403
13404         lovea2=$(get_layout_param $file2)
13405         echo "$lovea1"
13406         echo "$lovea2"
13407         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
13408
13409         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13410         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
13411 }
13412 run_test 184d "allow stripeless layouts swap"
13413
13414 test_184e() {
13415         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
13416                 skip "Need MDS version at least 2.6.94"
13417         check_swap_layouts_support
13418         [ -z "$(which getfattr 2>/dev/null)" ] &&
13419                 skip_env "no getfattr command"
13420
13421         local file1=$DIR/$tdir/$tfile-1
13422         local file2=$DIR/$tdir/$tfile-2
13423         local file3=$DIR/$tdir/$tfile-3
13424         local lovea
13425
13426         mkdir -p $DIR/$tdir
13427         touch $file1 || error "create $file1 failed"
13428         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13429                 error "create $file2 failed"
13430         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13431                 error "create $file3 failed"
13432
13433         $LFS swap_layouts $file1 $file2 ||
13434                 error "swap $file1 $file2 layouts failed"
13435
13436         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13437         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
13438
13439         echo 123 > $file1 || error "Should be able to write into $file1"
13440
13441         $LFS swap_layouts $file1 $file3 ||
13442                 error "swap $file1 $file3 layouts failed"
13443
13444         echo 123 > $file1 || error "Should be able to write into $file1"
13445
13446         rm -rf $file1 $file2 $file3
13447 }
13448 run_test 184e "Recreate layout after stripeless layout swaps"
13449
13450 test_185() { # LU-2441
13451         # LU-3553 - no volatile file support in old servers
13452         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
13453                 skip "Need MDS version at least 2.3.60"
13454
13455         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13456         touch $DIR/$tdir/spoo
13457         local mtime1=$(stat -c "%Y" $DIR/$tdir)
13458         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
13459                 error "cannot create/write a volatile file"
13460         [ "$FILESET" == "" ] &&
13461         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
13462                 error "FID is still valid after close"
13463
13464         multiop_bg_pause $DIR/$tdir vVw4096_c
13465         local multi_pid=$!
13466
13467         local OLD_IFS=$IFS
13468         IFS=":"
13469         local fidv=($fid)
13470         IFS=$OLD_IFS
13471         # assume that the next FID for this client is sequential, since stdout
13472         # is unfortunately eaten by multiop_bg_pause
13473         local n=$((${fidv[1]} + 1))
13474         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
13475         if [ "$FILESET" == "" ]; then
13476                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
13477                         error "FID is missing before close"
13478         fi
13479         kill -USR1 $multi_pid
13480         # 1 second delay, so if mtime change we will see it
13481         sleep 1
13482         local mtime2=$(stat -c "%Y" $DIR/$tdir)
13483         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
13484 }
13485 run_test 185 "Volatile file support"
13486
13487 test_187a() {
13488         remote_mds_nodsh && skip "remote MDS with nodsh"
13489         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
13490                 skip "Need MDS version at least 2.3.0"
13491
13492         local dir0=$DIR/$tdir/$testnum
13493         mkdir -p $dir0 || error "creating dir $dir0"
13494
13495         local file=$dir0/file1
13496         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
13497         local dv1=$($LFS data_version $file)
13498         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
13499         local dv2=$($LFS data_version $file)
13500         [[ $dv1 != $dv2 ]] ||
13501                 error "data version did not change on write $dv1 == $dv2"
13502
13503         # clean up
13504         rm -f $file1
13505 }
13506 run_test 187a "Test data version change"
13507
13508 test_187b() {
13509         remote_mds_nodsh && skip "remote MDS with nodsh"
13510         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
13511                 skip "Need MDS version at least 2.3.0"
13512
13513         local dir0=$DIR/$tdir/$testnum
13514         mkdir -p $dir0 || error "creating dir $dir0"
13515
13516         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
13517         [[ ${DV[0]} != ${DV[1]} ]] ||
13518                 error "data version did not change on write"\
13519                       " ${DV[0]} == ${DV[1]}"
13520
13521         # clean up
13522         rm -f $file1
13523 }
13524 run_test 187b "Test data version change on volatile file"
13525
13526 test_200() {
13527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13528         remote_mgs_nodsh && skip "remote MGS with nodsh"
13529         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13530
13531         local POOL=${POOL:-cea1}
13532         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
13533         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
13534         # Pool OST targets
13535         local first_ost=0
13536         local last_ost=$(($OSTCOUNT - 1))
13537         local ost_step=2
13538         local ost_list=$(seq $first_ost $ost_step $last_ost)
13539         local ost_range="$first_ost $last_ost $ost_step"
13540         local test_path=$POOL_ROOT/$POOL_DIR_NAME
13541         local file_dir=$POOL_ROOT/file_tst
13542         local subdir=$test_path/subdir
13543         local rc=0
13544
13545         if ! combined_mgs_mds ; then
13546                 mount_mgs_client
13547         fi
13548
13549         while : ; do
13550                 # former test_200a test_200b
13551                 pool_add $POOL                          || { rc=$? ; break; }
13552                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
13553                 # former test_200c test_200d
13554                 mkdir -p $test_path
13555                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
13556                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
13557                 mkdir -p $subdir
13558                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
13559                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
13560                                                         || { rc=$? ; break; }
13561                 # former test_200e test_200f
13562                 local files=$((OSTCOUNT*3))
13563                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
13564                                                         || { rc=$? ; break; }
13565                 pool_create_files $POOL $file_dir $files "$ost_list" \
13566                                                         || { rc=$? ; break; }
13567                 # former test_200g test_200h
13568                 pool_lfs_df $POOL                       || { rc=$? ; break; }
13569                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
13570
13571                 # former test_201a test_201b test_201c
13572                 pool_remove_first_target $POOL          || { rc=$? ; break; }
13573
13574                 local f=$test_path/$tfile
13575                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
13576                 pool_remove $POOL $f                    || { rc=$? ; break; }
13577                 break
13578         done
13579
13580         destroy_test_pools
13581
13582         if ! combined_mgs_mds ; then
13583                 umount_mgs_client
13584         fi
13585         return $rc
13586 }
13587 run_test 200 "OST pools"
13588
13589 # usage: default_attr <count | size | offset>
13590 default_attr() {
13591         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
13592 }
13593
13594 # usage: check_default_stripe_attr
13595 check_default_stripe_attr() {
13596         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
13597         case $1 in
13598         --stripe-count|-c)
13599                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
13600         --stripe-size|-S)
13601                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
13602         --stripe-index|-i)
13603                 EXPECTED=-1;;
13604         *)
13605                 error "unknown getstripe attr '$1'"
13606         esac
13607
13608         [ $ACTUAL == $EXPECTED ] ||
13609                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
13610 }
13611
13612 test_204a() {
13613         test_mkdir $DIR/$tdir
13614         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
13615
13616         check_default_stripe_attr --stripe-count
13617         check_default_stripe_attr --stripe-size
13618         check_default_stripe_attr --stripe-index
13619 }
13620 run_test 204a "Print default stripe attributes"
13621
13622 test_204b() {
13623         test_mkdir $DIR/$tdir
13624         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13625
13626         check_default_stripe_attr --stripe-size
13627         check_default_stripe_attr --stripe-index
13628 }
13629 run_test 204b "Print default stripe size and offset"
13630
13631 test_204c() {
13632         test_mkdir $DIR/$tdir
13633         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13634
13635         check_default_stripe_attr --stripe-count
13636         check_default_stripe_attr --stripe-index
13637 }
13638 run_test 204c "Print default stripe count and offset"
13639
13640 test_204d() {
13641         test_mkdir $DIR/$tdir
13642         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13643
13644         check_default_stripe_attr --stripe-count
13645         check_default_stripe_attr --stripe-size
13646 }
13647 run_test 204d "Print default stripe count and size"
13648
13649 test_204e() {
13650         test_mkdir $DIR/$tdir
13651         $SETSTRIPE -d $DIR/$tdir
13652
13653         check_default_stripe_attr --stripe-count --raw
13654         check_default_stripe_attr --stripe-size --raw
13655         check_default_stripe_attr --stripe-index --raw
13656 }
13657 run_test 204e "Print raw stripe attributes"
13658
13659 test_204f() {
13660         test_mkdir $DIR/$tdir
13661         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13662
13663         check_default_stripe_attr --stripe-size --raw
13664         check_default_stripe_attr --stripe-index --raw
13665 }
13666 run_test 204f "Print raw stripe size and offset"
13667
13668 test_204g() {
13669         test_mkdir $DIR/$tdir
13670         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13671
13672         check_default_stripe_attr --stripe-count --raw
13673         check_default_stripe_attr --stripe-index --raw
13674 }
13675 run_test 204g "Print raw stripe count and offset"
13676
13677 test_204h() {
13678         test_mkdir $DIR/$tdir
13679         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13680
13681         check_default_stripe_attr --stripe-count --raw
13682         check_default_stripe_attr --stripe-size --raw
13683 }
13684 run_test 204h "Print raw stripe count and size"
13685
13686 # Figure out which job scheduler is being used, if any,
13687 # or use a fake one
13688 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
13689         JOBENV=SLURM_JOB_ID
13690 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
13691         JOBENV=LSB_JOBID
13692 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
13693         JOBENV=PBS_JOBID
13694 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
13695         JOBENV=LOADL_STEP_ID
13696 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
13697         JOBENV=JOB_ID
13698 else
13699         $LCTL list_param jobid_name > /dev/null 2>&1
13700         if [ $? -eq 0 ]; then
13701                 JOBENV=nodelocal
13702         else
13703                 JOBENV=FAKE_JOBID
13704         fi
13705 fi
13706 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
13707
13708 verify_jobstats() {
13709         local cmd=($1)
13710         shift
13711         local facets="$@"
13712
13713 # we don't really need to clear the stats for this test to work, since each
13714 # command has a unique jobid, but it makes debugging easier if needed.
13715 #       for facet in $facets; do
13716 #               local dev=$(convert_facet2label $facet)
13717 #               # clear old jobstats
13718 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
13719 #       done
13720
13721         # use a new JobID for each test, or we might see an old one
13722         [ "$JOBENV" = "FAKE_JOBID" ] &&
13723                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13724
13725         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
13726
13727         [ "$JOBENV" = "nodelocal" ] && {
13728                 FAKE_JOBID=id.$testnum.%e.$RANDOM
13729                 $LCTL set_param jobid_name=$FAKE_JOBID
13730                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
13731         }
13732
13733         log "Test: ${cmd[*]}"
13734         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
13735
13736         if [ $JOBENV = "FAKE_JOBID" ]; then
13737                 FAKE_JOBID=$JOBVAL ${cmd[*]}
13738         else
13739                 ${cmd[*]}
13740         fi
13741
13742         # all files are created on OST0000
13743         for facet in $facets; do
13744                 local stats="*.$(convert_facet2label $facet).job_stats"
13745
13746                 # strip out libtool wrappers for in-tree executables
13747                 if [ $(do_facet $facet lctl get_param $stats |
13748                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
13749                         do_facet $facet lctl get_param $stats
13750                         error "No jobstats for $JOBVAL found on $facet::$stats"
13751                 fi
13752         done
13753 }
13754
13755 jobstats_set() {
13756         local new_jobenv=$1
13757
13758         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$new_jobenv
13759         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $new_jobenv
13760 }
13761
13762 test_205() { # Job stats
13763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13764         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
13765                 skip "Need MDS version with at least 2.7.1"
13766         remote_mgs_nodsh && skip "remote MGS with nodsh"
13767         remote_mds_nodsh && skip "remote MDS with nodsh"
13768         remote_ost_nodsh && skip "remote OST with nodsh"
13769         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
13770                 skip "Server doesn't support jobstats"
13771         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
13772
13773         local old_jobenv=$($LCTL get_param -n jobid_var)
13774         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
13775         stack_trap "do_facet mgs \
13776                 $LCTL conf_param $FSNAME.sys.jobid_var=$old_jobenv" EXIT
13777
13778         changelog_register
13779
13780         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
13781                                 mdt.*.job_cleanup_interval | head -n 1)
13782         local new_interval=5
13783         do_facet $SINGLEMDS \
13784                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
13785         stack_trap "do_facet $SINGLEMDS \
13786                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
13787         local start=$SECONDS
13788
13789         local cmd
13790         # mkdir
13791         cmd="mkdir $DIR/$tdir"
13792         verify_jobstats "$cmd" "$SINGLEMDS"
13793         # rmdir
13794         cmd="rmdir $DIR/$tdir"
13795         verify_jobstats "$cmd" "$SINGLEMDS"
13796         # mkdir on secondary MDT
13797         if [ $MDSCOUNT -gt 1 ]; then
13798                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
13799                 verify_jobstats "$cmd" "mds2"
13800         fi
13801         # mknod
13802         cmd="mknod $DIR/$tfile c 1 3"
13803         verify_jobstats "$cmd" "$SINGLEMDS"
13804         # unlink
13805         cmd="rm -f $DIR/$tfile"
13806         verify_jobstats "$cmd" "$SINGLEMDS"
13807         # create all files on OST0000 so verify_jobstats can find OST stats
13808         # open & close
13809         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
13810         verify_jobstats "$cmd" "$SINGLEMDS"
13811         # setattr
13812         cmd="touch $DIR/$tfile"
13813         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13814         # write
13815         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
13816         verify_jobstats "$cmd" "ost1"
13817         # read
13818         cancel_lru_locks osc
13819         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
13820         verify_jobstats "$cmd" "ost1"
13821         # truncate
13822         cmd="$TRUNCATE $DIR/$tfile 0"
13823         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13824         # rename
13825         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
13826         verify_jobstats "$cmd" "$SINGLEMDS"
13827         # jobstats expiry - sleep until old stats should be expired
13828         local left=$((new_interval + 5 - (SECONDS - start)))
13829         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
13830                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
13831                         "0" $left
13832         cmd="mkdir $DIR/$tdir.expire"
13833         verify_jobstats "$cmd" "$SINGLEMDS"
13834         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
13835             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
13836
13837         # Ensure that jobid are present in changelog (if supported by MDS)
13838         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ];then
13839                 changelog_dump | tail -10
13840                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
13841                 [ $jobids -eq 9 ] ||
13842                         error "Wrong changelog jobid count $jobids != 9"
13843
13844                 # LU-5862
13845                 JOBENV="disable"
13846                 jobstats_set $JOBENV
13847                 touch $DIR/$tfile
13848                 changelog_dump | grep $tfile
13849                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
13850                 [ $jobids -eq 0 ] ||
13851                         error "Unexpected jobids when jobid_var=$JOBENV"
13852         fi
13853
13854         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
13855         JOBENV="JOBCOMPLEX"
13856         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
13857
13858         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
13859 }
13860 run_test 205 "Verify job stats"
13861
13862 # LU-1480, LU-1773 and LU-1657
13863 test_206() {
13864         mkdir -p $DIR/$tdir
13865         $SETSTRIPE -c -1 $DIR/$tdir
13866 #define OBD_FAIL_LOV_INIT 0x1403
13867         $LCTL set_param fail_loc=0xa0001403
13868         $LCTL set_param fail_val=1
13869         touch $DIR/$tdir/$tfile || true
13870 }
13871 run_test 206 "fail lov_init_raid0() doesn't lbug"
13872
13873 test_207a() {
13874         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13875         local fsz=`stat -c %s $DIR/$tfile`
13876         cancel_lru_locks mdc
13877
13878         # do not return layout in getattr intent
13879 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
13880         $LCTL set_param fail_loc=0x170
13881         local sz=`stat -c %s $DIR/$tfile`
13882
13883         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
13884
13885         rm -rf $DIR/$tfile
13886 }
13887 run_test 207a "can refresh layout at glimpse"
13888
13889 test_207b() {
13890         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13891         local cksum=`md5sum $DIR/$tfile`
13892         local fsz=`stat -c %s $DIR/$tfile`
13893         cancel_lru_locks mdc
13894         cancel_lru_locks osc
13895
13896         # do not return layout in getattr intent
13897 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
13898         $LCTL set_param fail_loc=0x171
13899
13900         # it will refresh layout after the file is opened but before read issues
13901         echo checksum is "$cksum"
13902         echo "$cksum" |md5sum -c --quiet || error "file differs"
13903
13904         rm -rf $DIR/$tfile
13905 }
13906 run_test 207b "can refresh layout at open"
13907
13908 test_208() {
13909         # FIXME: in this test suite, only RD lease is used. This is okay
13910         # for now as only exclusive open is supported. After generic lease
13911         # is done, this test suite should be revised. - Jinshan
13912
13913         remote_mds_nodsh && skip "remote MDS with nodsh"
13914         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
13915                 skip "Need MDS version at least 2.4.52"
13916
13917         echo "==== test 1: verify get lease work"
13918         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
13919
13920         echo "==== test 2: verify lease can be broken by upcoming open"
13921         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13922         local PID=$!
13923         sleep 1
13924
13925         $MULTIOP $DIR/$tfile oO_RDONLY:c
13926         kill -USR1 $PID && wait $PID || error "break lease error"
13927
13928         echo "==== test 3: verify lease can't be granted if an open already exists"
13929         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13930         local PID=$!
13931         sleep 1
13932
13933         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13934         kill -USR1 $PID && wait $PID || error "open file error"
13935
13936         echo "==== test 4: lease can sustain over recovery"
13937         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13938         PID=$!
13939         sleep 1
13940
13941         fail mds1
13942
13943         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13944
13945         echo "==== test 5: lease broken can't be regained by replay"
13946         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13947         PID=$!
13948         sleep 1
13949
13950         # open file to break lease and then recovery
13951         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13952         fail mds1
13953
13954         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13955
13956         rm -f $DIR/$tfile
13957 }
13958 run_test 208 "Exclusive open"
13959
13960 test_209() {
13961         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
13962                 skip_env "must have disp_stripe"
13963
13964         touch $DIR/$tfile
13965         sync; sleep 5; sync;
13966
13967         echo 3 > /proc/sys/vm/drop_caches
13968         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13969
13970         # open/close 500 times
13971         for i in $(seq 500); do
13972                 cat $DIR/$tfile
13973         done
13974
13975         echo 3 > /proc/sys/vm/drop_caches
13976         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13977
13978         echo "before: $req_before, after: $req_after"
13979         [ $((req_after - req_before)) -ge 300 ] &&
13980                 error "open/close requests are not freed"
13981         return 0
13982 }
13983 run_test 209 "read-only open/close requests should be freed promptly"
13984
13985 test_212() {
13986         size=`date +%s`
13987         size=$((size % 8192 + 1))
13988         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
13989         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
13990         rm -f $DIR/f212 $DIR/f212.xyz
13991 }
13992 run_test 212 "Sendfile test ============================================"
13993
13994 test_213() {
13995         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
13996         cancel_lru_locks osc
13997         lctl set_param fail_loc=0x8000040f
13998         # generate a read lock
13999         cat $DIR/$tfile > /dev/null
14000         # write to the file, it will try to cancel the above read lock.
14001         cat /etc/hosts >> $DIR/$tfile
14002 }
14003 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
14004
14005 test_214() { # for bug 20133
14006         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
14007         for (( i=0; i < 340; i++ )) ; do
14008                 touch $DIR/$tdir/d214c/a$i
14009         done
14010
14011         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
14012         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
14013         ls $DIR/d214c || error "ls $DIR/d214c failed"
14014         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
14015         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
14016 }
14017 run_test 214 "hash-indexed directory test - bug 20133"
14018
14019 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
14020 create_lnet_proc_files() {
14021         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
14022 }
14023
14024 # counterpart of create_lnet_proc_files
14025 remove_lnet_proc_files() {
14026         rm -f $TMP/lnet_$1.sys
14027 }
14028
14029 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14030 # 3rd arg as regexp for body
14031 check_lnet_proc_stats() {
14032         local l=$(cat "$TMP/lnet_$1" |wc -l)
14033         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
14034
14035         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
14036 }
14037
14038 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14039 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
14040 # optional and can be regexp for 2nd line (lnet.routes case)
14041 check_lnet_proc_entry() {
14042         local blp=2          # blp stands for 'position of 1st line of body'
14043         [ -z "$5" ] || blp=3 # lnet.routes case
14044
14045         local l=$(cat "$TMP/lnet_$1" |wc -l)
14046         # subtracting one from $blp because the body can be empty
14047         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
14048
14049         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
14050                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
14051
14052         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
14053                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
14054
14055         # bail out if any unexpected line happened
14056         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
14057         [ "$?" != 0 ] || error "$2 misformatted"
14058 }
14059
14060 test_215() { # for bugs 18102, 21079, 21517
14061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14062
14063         local N='(0|[1-9][0-9]*)'       # non-negative numeric
14064         local P='[1-9][0-9]*'           # positive numeric
14065         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
14066         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
14067         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
14068         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
14069
14070         local L1 # regexp for 1st line
14071         local L2 # regexp for 2nd line (optional)
14072         local BR # regexp for the rest (body)
14073
14074         # lnet.stats should look as 11 space-separated non-negative numerics
14075         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
14076         create_lnet_proc_files "stats"
14077         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
14078         remove_lnet_proc_files "stats"
14079
14080         # lnet.routes should look like this:
14081         # Routing disabled/enabled
14082         # net hops priority state router
14083         # where net is a string like tcp0, hops > 0, priority >= 0,
14084         # state is up/down,
14085         # router is a string like 192.168.1.1@tcp2
14086         L1="^Routing (disabled|enabled)$"
14087         L2="^net +hops +priority +state +router$"
14088         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
14089         create_lnet_proc_files "routes"
14090         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
14091         remove_lnet_proc_files "routes"
14092
14093         # lnet.routers should look like this:
14094         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
14095         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
14096         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
14097         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
14098         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
14099         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
14100         create_lnet_proc_files "routers"
14101         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
14102         remove_lnet_proc_files "routers"
14103
14104         # lnet.peers should look like this:
14105         # nid refs state last max rtr min tx min queue
14106         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
14107         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
14108         # numeric (0 or >0 or <0), queue >= 0.
14109         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
14110         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
14111         create_lnet_proc_files "peers"
14112         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
14113         remove_lnet_proc_files "peers"
14114
14115         # lnet.buffers  should look like this:
14116         # pages count credits min
14117         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
14118         L1="^pages +count +credits +min$"
14119         BR="^ +$N +$N +$I +$I$"
14120         create_lnet_proc_files "buffers"
14121         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
14122         remove_lnet_proc_files "buffers"
14123
14124         # lnet.nis should look like this:
14125         # nid status alive refs peer rtr max tx min
14126         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
14127         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
14128         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
14129         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
14130         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
14131         create_lnet_proc_files "nis"
14132         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
14133         remove_lnet_proc_files "nis"
14134
14135         # can we successfully write to lnet.stats?
14136         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
14137 }
14138 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
14139
14140 test_216() { # bug 20317
14141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14142         remote_ost_nodsh && skip "remote OST with nodsh"
14143
14144         local node
14145         local facets=$(get_facets OST)
14146         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14147
14148         save_lustre_params client "osc.*.contention_seconds" > $p
14149         save_lustre_params $facets \
14150                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
14151         save_lustre_params $facets \
14152                 "ldlm.namespaces.filter-*.contended_locks" >> $p
14153         save_lustre_params $facets \
14154                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
14155         clear_stats osc.*.osc_stats
14156
14157         # agressive lockless i/o settings
14158         do_nodes $(comma_list $(osts_nodes)) \
14159                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
14160                         ldlm.namespaces.filter-*.contended_locks=0 \
14161                         ldlm.namespaces.filter-*.contention_seconds=60"
14162         lctl set_param -n osc.*.contention_seconds=60
14163
14164         $DIRECTIO write $DIR/$tfile 0 10 4096
14165         $CHECKSTAT -s 40960 $DIR/$tfile
14166
14167         # disable lockless i/o
14168         do_nodes $(comma_list $(osts_nodes)) \
14169                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
14170                         ldlm.namespaces.filter-*.contended_locks=32 \
14171                         ldlm.namespaces.filter-*.contention_seconds=0"
14172         lctl set_param -n osc.*.contention_seconds=0
14173         clear_stats osc.*.osc_stats
14174
14175         dd if=/dev/zero of=$DIR/$tfile count=0
14176         $CHECKSTAT -s 0 $DIR/$tfile
14177
14178         restore_lustre_params <$p
14179         rm -f $p
14180         rm $DIR/$tfile
14181 }
14182 run_test 216 "check lockless direct write updates file size and kms correctly"
14183
14184 test_217() { # bug 22430
14185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14186
14187         local node
14188         local nid
14189
14190         for node in $(nodes_list); do
14191                 nid=$(host_nids_address $node $NETTYPE)
14192                 if [[ $nid = *-* ]] ; then
14193                         echo "lctl ping $(h2nettype $nid)"
14194                         lctl ping $(h2nettype $nid)
14195                 else
14196                         echo "skipping $node (no hyphen detected)"
14197                 fi
14198         done
14199 }
14200 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
14201
14202 test_218() {
14203        # do directio so as not to populate the page cache
14204        log "creating a 10 Mb file"
14205        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14206        log "starting reads"
14207        dd if=$DIR/$tfile of=/dev/null bs=4096 &
14208        log "truncating the file"
14209        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14210        log "killing dd"
14211        kill %+ || true # reads might have finished
14212        echo "wait until dd is finished"
14213        wait
14214        log "removing the temporary file"
14215        rm -rf $DIR/$tfile || error "tmp file removal failed"
14216 }
14217 run_test 218 "parallel read and truncate should not deadlock"
14218
14219 test_219() {
14220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14221
14222         # write one partial page
14223         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
14224         # set no grant so vvp_io_commit_write will do sync write
14225         $LCTL set_param fail_loc=0x411
14226         # write a full page at the end of file
14227         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
14228
14229         $LCTL set_param fail_loc=0
14230         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
14231         $LCTL set_param fail_loc=0x411
14232         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
14233
14234         # LU-4201
14235         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
14236         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
14237 }
14238 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
14239
14240 test_220() { #LU-325
14241         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14242         remote_ost_nodsh && skip "remote OST with nodsh"
14243         remote_mds_nodsh && skip "remote MDS with nodsh"
14244         remote_mgs_nodsh && skip "remote MGS with nodsh"
14245
14246         local OSTIDX=0
14247
14248         # create on MDT0000 so the last_id and next_id are correct
14249         mkdir $DIR/$tdir
14250         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
14251         OST=${OST%_UUID}
14252
14253         # on the mdt's osc
14254         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
14255         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
14256                         osc.$mdtosc_proc1.prealloc_last_id)
14257         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
14258                         osc.$mdtosc_proc1.prealloc_next_id)
14259
14260         $LFS df -i
14261
14262         if ! combined_mgs_mds ; then
14263                 mount_mgs_client
14264         fi
14265
14266         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
14267         #define OBD_FAIL_OST_ENOINO              0x229
14268         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
14269         create_pool $FSNAME.$TESTNAME || return 1
14270         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
14271
14272         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
14273
14274         MDSOBJS=$((last_id - next_id))
14275         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
14276
14277         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
14278         echo "OST still has $count kbytes free"
14279
14280         echo "create $MDSOBJS files @next_id..."
14281         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
14282
14283         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14284                         osc.$mdtosc_proc1.prealloc_last_id)
14285         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14286                         osc.$mdtosc_proc1.prealloc_next_id)
14287
14288         echo "after creation, last_id=$last_id2, next_id=$next_id2"
14289         $LFS df -i
14290
14291         echo "cleanup..."
14292
14293         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
14294         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
14295
14296         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
14297                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
14298         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14299                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
14300         echo "unlink $MDSOBJS files @$next_id..."
14301         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
14302
14303         if ! combined_mgs_mds ; then
14304                 umount_mgs_client
14305         fi
14306 }
14307 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
14308
14309 test_221() {
14310         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14311
14312         dd if=`which date` of=$MOUNT/date oflag=sync
14313         chmod +x $MOUNT/date
14314
14315         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
14316         $LCTL set_param fail_loc=0x80001401
14317
14318         $MOUNT/date > /dev/null
14319         rm -f $MOUNT/date
14320 }
14321 run_test 221 "make sure fault and truncate race to not cause OOM"
14322
14323 test_222a () {
14324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14325
14326         rm -rf $DIR/$tdir
14327         test_mkdir $DIR/$tdir
14328         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14329         createmany -o $DIR/$tdir/$tfile 10
14330         cancel_lru_locks mdc
14331         cancel_lru_locks osc
14332         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14333         $LCTL set_param fail_loc=0x31a
14334         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
14335         $LCTL set_param fail_loc=0
14336         rm -r $DIR/$tdir
14337 }
14338 run_test 222a "AGL for ls should not trigger CLIO lock failure"
14339
14340 test_222b () {
14341         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14342
14343         rm -rf $DIR/$tdir
14344         test_mkdir $DIR/$tdir
14345         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14346         createmany -o $DIR/$tdir/$tfile 10
14347         cancel_lru_locks mdc
14348         cancel_lru_locks osc
14349         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14350         $LCTL set_param fail_loc=0x31a
14351         rm -r $DIR/$tdir || error "AGL for rmdir failed"
14352         $LCTL set_param fail_loc=0
14353 }
14354 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
14355
14356 test_223 () {
14357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14358
14359         rm -rf $DIR/$tdir
14360         test_mkdir $DIR/$tdir
14361         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14362         createmany -o $DIR/$tdir/$tfile 10
14363         cancel_lru_locks mdc
14364         cancel_lru_locks osc
14365         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
14366         $LCTL set_param fail_loc=0x31b
14367         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
14368         $LCTL set_param fail_loc=0
14369         rm -r $DIR/$tdir
14370 }
14371 run_test 223 "osc reenqueue if without AGL lock granted ======================="
14372
14373 test_224a() { # LU-1039, MRP-303
14374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14375
14376         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
14377         $LCTL set_param fail_loc=0x508
14378         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
14379         $LCTL set_param fail_loc=0
14380         df $DIR
14381 }
14382 run_test 224a "Don't panic on bulk IO failure"
14383
14384 test_224b() { # LU-1039, MRP-303
14385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14386
14387         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
14388         cancel_lru_locks osc
14389         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
14390         $LCTL set_param fail_loc=0x515
14391         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
14392         $LCTL set_param fail_loc=0
14393         df $DIR
14394 }
14395 run_test 224b "Don't panic on bulk IO failure"
14396
14397 test_224c() { # LU-6441
14398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14399         remote_mds_nodsh && skip "remote MDS with nodsh"
14400
14401         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14402         save_writethrough $p
14403         set_cache writethrough on
14404
14405         local pages_per_rpc=$($LCTL get_param \
14406                                 osc.*.max_pages_per_rpc)
14407         local at_max=$($LCTL get_param -n at_max)
14408         local timeout=$($LCTL get_param -n timeout)
14409         local test_at="$LCTL get_param -n at_max"
14410         local param_at="$FSNAME.sys.at_max"
14411         local test_timeout="$LCTL get_param -n timeout"
14412         local param_timeout="$FSNAME.sys.timeout"
14413
14414         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
14415
14416         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
14417                 error "conf_param at_max=0 failed"
14418         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
14419                 error "conf_param timeout=5 failed"
14420
14421         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
14422         do_facet ost1 $LCTL set_param fail_loc=0x520
14423         $LFS setstripe -c 1 -i 0 $DIR/$tfile
14424         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
14425         sync
14426         do_facet ost1 $LCTL set_param fail_loc=0
14427
14428         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
14429                 error "conf_param at_max=$at_max failed"
14430         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
14431                 $timeout || error "conf_param timeout=$timeout failed"
14432
14433         $LCTL set_param -n $pages_per_rpc
14434         restore_lustre_params < $p
14435         rm -f $p
14436 }
14437 run_test 224c "Don't hang if one of md lost during large bulk RPC"
14438
14439 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
14440 test_225a () {
14441         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14442         if [ -z ${MDSSURVEY} ]; then
14443                 skip_env "mds-survey not found"
14444         fi
14445         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
14446                 skip "Need MDS version at least 2.2.51"
14447
14448         local mds=$(facet_host $SINGLEMDS)
14449         local target=$(do_nodes $mds 'lctl dl' |
14450                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14451
14452         local cmd1="file_count=1000 thrhi=4"
14453         local cmd2="dir_count=2 layer=mdd stripe_count=0"
14454         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14455         local cmd="$cmd1 $cmd2 $cmd3"
14456
14457         rm -f ${TMP}/mds_survey*
14458         echo + $cmd
14459         eval $cmd || error "mds-survey with zero-stripe failed"
14460         cat ${TMP}/mds_survey*
14461         rm -f ${TMP}/mds_survey*
14462 }
14463 run_test 225a "Metadata survey sanity with zero-stripe"
14464
14465 test_225b () {
14466         if [ -z ${MDSSURVEY} ]; then
14467                 skip_env "mds-survey not found"
14468         fi
14469         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
14470                 skip "Need MDS version at least 2.2.51"
14471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14472         remote_mds_nodsh && skip "remote MDS with nodsh"
14473         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
14474                 skip_env "Need to mount OST to test"
14475         fi
14476
14477         local mds=$(facet_host $SINGLEMDS)
14478         local target=$(do_nodes $mds 'lctl dl' |
14479                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14480
14481         local cmd1="file_count=1000 thrhi=4"
14482         local cmd2="dir_count=2 layer=mdd stripe_count=1"
14483         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14484         local cmd="$cmd1 $cmd2 $cmd3"
14485
14486         rm -f ${TMP}/mds_survey*
14487         echo + $cmd
14488         eval $cmd || error "mds-survey with stripe_count failed"
14489         cat ${TMP}/mds_survey*
14490         rm -f ${TMP}/mds_survey*
14491 }
14492 run_test 225b "Metadata survey sanity with stripe_count = 1"
14493
14494 mcreate_path2fid () {
14495         local mode=$1
14496         local major=$2
14497         local minor=$3
14498         local name=$4
14499         local desc=$5
14500         local path=$DIR/$tdir/$name
14501         local fid
14502         local rc
14503         local fid_path
14504
14505         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
14506                 error "cannot create $desc"
14507
14508         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
14509         rc=$?
14510         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
14511
14512         fid_path=$($LFS fid2path $MOUNT $fid)
14513         rc=$?
14514         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
14515
14516         [ "$path" == "$fid_path" ] ||
14517                 error "fid2path returned $fid_path, expected $path"
14518
14519         echo "pass with $path and $fid"
14520 }
14521
14522 test_226a () {
14523         rm -rf $DIR/$tdir
14524         mkdir -p $DIR/$tdir
14525
14526         mcreate_path2fid 0010666 0 0 fifo "FIFO"
14527         mcreate_path2fid 0020666 1 3 null "character special file (null)"
14528         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
14529         mcreate_path2fid 0040666 0 0 dir "directory"
14530         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
14531         mcreate_path2fid 0100666 0 0 file "regular file"
14532         mcreate_path2fid 0120666 0 0 link "symbolic link"
14533         mcreate_path2fid 0140666 0 0 sock "socket"
14534 }
14535 run_test 226a "call path2fid and fid2path on files of all type"
14536
14537 test_226b () {
14538         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14539
14540         local MDTIDX=1
14541
14542         rm -rf $DIR/$tdir
14543         mkdir -p $DIR/$tdir
14544         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
14545                 error "create remote directory failed"
14546         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
14547         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
14548                                 "character special file (null)"
14549         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
14550                                 "character special file (no device)"
14551         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
14552         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
14553                                 "block special file (loop)"
14554         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
14555         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
14556         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
14557 }
14558 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
14559
14560 # LU-1299 Executing or running ldd on a truncated executable does not
14561 # cause an out-of-memory condition.
14562 test_227() {
14563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14564         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
14565
14566         dd if=$(which date) of=$MOUNT/date bs=1k count=1
14567         chmod +x $MOUNT/date
14568
14569         $MOUNT/date > /dev/null
14570         ldd $MOUNT/date > /dev/null
14571         rm -f $MOUNT/date
14572 }
14573 run_test 227 "running truncated executable does not cause OOM"
14574
14575 # LU-1512 try to reuse idle OI blocks
14576 test_228a() {
14577         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14578         remote_mds_nodsh && skip "remote MDS with nodsh"
14579         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14580                 skip_env "ldiskfs only test"
14581
14582         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14583         local myDIR=$DIR/$tdir
14584
14585         mkdir -p $myDIR
14586         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14587         $LCTL set_param fail_loc=0x80001002
14588         createmany -o $myDIR/t- 10000
14589         $LCTL set_param fail_loc=0
14590         # The guard is current the largest FID holder
14591         touch $myDIR/guard
14592         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14593                     tr -d '[')
14594         local IDX=$(($SEQ % 64))
14595
14596         do_facet $SINGLEMDS sync
14597         # Make sure journal flushed.
14598         sleep 6
14599         local blk1=$(do_facet $SINGLEMDS \
14600                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14601                      grep Blockcount | awk '{print $4}')
14602
14603         # Remove old files, some OI blocks will become idle.
14604         unlinkmany $myDIR/t- 10000
14605         # Create new files, idle OI blocks should be reused.
14606         createmany -o $myDIR/t- 2000
14607         do_facet $SINGLEMDS sync
14608         # Make sure journal flushed.
14609         sleep 6
14610         local blk2=$(do_facet $SINGLEMDS \
14611                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14612                      grep Blockcount | awk '{print $4}')
14613
14614         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14615 }
14616 run_test 228a "try to reuse idle OI blocks"
14617
14618 test_228b() {
14619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14620         remote_mds_nodsh && skip "remote MDS with nodsh"
14621         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14622                 skip_env "ldiskfs only test"
14623
14624         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14625         local myDIR=$DIR/$tdir
14626
14627         mkdir -p $myDIR
14628         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14629         $LCTL set_param fail_loc=0x80001002
14630         createmany -o $myDIR/t- 10000
14631         $LCTL set_param fail_loc=0
14632         # The guard is current the largest FID holder
14633         touch $myDIR/guard
14634         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14635                     tr -d '[')
14636         local IDX=$(($SEQ % 64))
14637
14638         do_facet $SINGLEMDS sync
14639         # Make sure journal flushed.
14640         sleep 6
14641         local blk1=$(do_facet $SINGLEMDS \
14642                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14643                      grep Blockcount | awk '{print $4}')
14644
14645         # Remove old files, some OI blocks will become idle.
14646         unlinkmany $myDIR/t- 10000
14647
14648         # stop the MDT
14649         stop $SINGLEMDS || error "Fail to stop MDT."
14650         # remount the MDT
14651         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
14652
14653         df $MOUNT || error "Fail to df."
14654         # Create new files, idle OI blocks should be reused.
14655         createmany -o $myDIR/t- 2000
14656         do_facet $SINGLEMDS sync
14657         # Make sure journal flushed.
14658         sleep 6
14659         local blk2=$(do_facet $SINGLEMDS \
14660                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14661                      grep Blockcount | awk '{print $4}')
14662
14663         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14664 }
14665 run_test 228b "idle OI blocks can be reused after MDT restart"
14666
14667 #LU-1881
14668 test_228c() {
14669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14670         remote_mds_nodsh && skip "remote MDS with nodsh"
14671         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14672                 skip_env "ldiskfs only test"
14673
14674         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14675         local myDIR=$DIR/$tdir
14676
14677         mkdir -p $myDIR
14678         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14679         $LCTL set_param fail_loc=0x80001002
14680         # 20000 files can guarantee there are index nodes in the OI file
14681         createmany -o $myDIR/t- 20000
14682         $LCTL set_param fail_loc=0
14683         # The guard is current the largest FID holder
14684         touch $myDIR/guard
14685         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14686                     tr -d '[')
14687         local IDX=$(($SEQ % 64))
14688
14689         do_facet $SINGLEMDS sync
14690         # Make sure journal flushed.
14691         sleep 6
14692         local blk1=$(do_facet $SINGLEMDS \
14693                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14694                      grep Blockcount | awk '{print $4}')
14695
14696         # Remove old files, some OI blocks will become idle.
14697         unlinkmany $myDIR/t- 20000
14698         rm -f $myDIR/guard
14699         # The OI file should become empty now
14700
14701         # Create new files, idle OI blocks should be reused.
14702         createmany -o $myDIR/t- 2000
14703         do_facet $SINGLEMDS sync
14704         # Make sure journal flushed.
14705         sleep 6
14706         local blk2=$(do_facet $SINGLEMDS \
14707                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14708                      grep Blockcount | awk '{print $4}')
14709
14710         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14711 }
14712 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
14713
14714 test_229() { # LU-2482, LU-3448
14715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14716         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
14717         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
14718                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
14719
14720         rm -f $DIR/$tfile
14721
14722         # Create a file with a released layout and stripe count 2.
14723         $MULTIOP $DIR/$tfile H2c ||
14724                 error "failed to create file with released layout"
14725
14726         $GETSTRIPE -v $DIR/$tfile
14727
14728         local pattern=$($GETSTRIPE -L $DIR/$tfile)
14729         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
14730
14731         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
14732         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
14733         stat $DIR/$tfile || error "failed to stat released file"
14734
14735         chown $RUNAS_ID $DIR/$tfile ||
14736                 error "chown $RUNAS_ID $DIR/$tfile failed"
14737
14738         chgrp $RUNAS_ID $DIR/$tfile ||
14739                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
14740
14741         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
14742         rm $DIR/$tfile || error "failed to remove released file"
14743 }
14744 run_test 229 "getstripe/stat/rm/attr changes work on released files"
14745
14746 test_230a() {
14747         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14748         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14749
14750         local MDTIDX=1
14751
14752         test_mkdir $DIR/$tdir
14753         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
14754         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
14755         [ $mdt_idx -ne 0 ] &&
14756                 error "create local directory on wrong MDT $mdt_idx"
14757
14758         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
14759                         error "create remote directory failed"
14760         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
14761         [ $mdt_idx -ne $MDTIDX ] &&
14762                 error "create remote directory on wrong MDT $mdt_idx"
14763
14764         createmany -o $DIR/$tdir/test_230/t- 10 ||
14765                 error "create files on remote directory failed"
14766         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
14767         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
14768         rm -r $DIR/$tdir || error "unlink remote directory failed"
14769 }
14770 run_test 230a "Create remote directory and files under the remote directory"
14771
14772 test_230b() {
14773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14774         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14775
14776         local MDTIDX=1
14777         local mdt_index
14778         local i
14779         local file
14780         local pid
14781         local stripe_count
14782         local migrate_dir=$DIR/$tdir/migrate_dir
14783         local other_dir=$DIR/$tdir/other_dir
14784
14785         test_mkdir $DIR/$tdir
14786         test_mkdir -i0 -c1 $migrate_dir
14787         test_mkdir -i0 -c1 $other_dir
14788         for ((i=0; i<10; i++)); do
14789                 mkdir -p $migrate_dir/dir_${i}
14790                 createmany -o $migrate_dir/dir_${i}/f 10 ||
14791                         error "create files under remote dir failed $i"
14792         done
14793
14794         cp /etc/passwd $migrate_dir/$tfile
14795         cp /etc/passwd $other_dir/$tfile
14796         chattr +SAD $migrate_dir
14797         chattr +SAD $migrate_dir/$tfile
14798
14799         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14800         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14801         local old_dir_mode=$(stat -c%f $migrate_dir)
14802         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
14803
14804         mkdir -p $migrate_dir/dir_default_stripe2
14805         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
14806         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
14807
14808         mkdir -p $other_dir
14809         ln $migrate_dir/$tfile $other_dir/luna
14810         ln $migrate_dir/$tfile $migrate_dir/sofia
14811         ln $other_dir/$tfile $migrate_dir/david
14812         ln -s $migrate_dir/$tfile $other_dir/zachary
14813         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
14814         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
14815
14816         $LFS migrate -m $MDTIDX $migrate_dir ||
14817                 error "fails on migrating remote dir to MDT1"
14818
14819         echo "migratate to MDT1, then checking.."
14820         for ((i = 0; i < 10; i++)); do
14821                 for file in $(find $migrate_dir/dir_${i}); do
14822                         mdt_index=$($LFS getstripe -m $file)
14823                         [ $mdt_index == $MDTIDX ] ||
14824                                 error "$file is not on MDT${MDTIDX}"
14825                 done
14826         done
14827
14828         # the multiple link file should still in MDT0
14829         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
14830         [ $mdt_index == 0 ] ||
14831                 error "$file is not on MDT${MDTIDX}"
14832
14833         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14834         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14835                 error " expect $old_dir_flag get $new_dir_flag"
14836
14837         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14838         [ "$old_file_flag" = "$new_file_flag" ] ||
14839                 error " expect $old_file_flag get $new_file_flag"
14840
14841         local new_dir_mode=$(stat -c%f $migrate_dir)
14842         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14843                 error "expect mode $old_dir_mode get $new_dir_mode"
14844
14845         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14846         [ "$old_file_mode" = "$new_file_mode" ] ||
14847                 error "expect mode $old_file_mode get $new_file_mode"
14848
14849         diff /etc/passwd $migrate_dir/$tfile ||
14850                 error "$tfile different after migration"
14851
14852         diff /etc/passwd $other_dir/luna ||
14853                 error "luna different after migration"
14854
14855         diff /etc/passwd $migrate_dir/sofia ||
14856                 error "sofia different after migration"
14857
14858         diff /etc/passwd $migrate_dir/david ||
14859                 error "david different after migration"
14860
14861         diff /etc/passwd $other_dir/zachary ||
14862                 error "zachary different after migration"
14863
14864         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14865                 error "${tfile}_ln different after migration"
14866
14867         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14868                 error "${tfile}_ln_other different after migration"
14869
14870         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
14871         [ $stripe_count = 2 ] ||
14872                 error "dir strpe_count $d != 2 after migration."
14873
14874         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
14875         [ $stripe_count = 2 ] ||
14876                 error "file strpe_count $d != 2 after migration."
14877
14878         #migrate back to MDT0
14879         MDTIDX=0
14880
14881         $LFS migrate -m $MDTIDX $migrate_dir ||
14882                 error "fails on migrating remote dir to MDT0"
14883
14884         echo "migrate back to MDT0, checking.."
14885         for file in $(find $migrate_dir); do
14886                 mdt_index=$($LFS getstripe -m $file)
14887                 [ $mdt_index == $MDTIDX ] ||
14888                         error "$file is not on MDT${MDTIDX}"
14889         done
14890
14891         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14892         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14893                 error " expect $old_dir_flag get $new_dir_flag"
14894
14895         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14896         [ "$old_file_flag" = "$new_file_flag" ] ||
14897                 error " expect $old_file_flag get $new_file_flag"
14898
14899         local new_dir_mode=$(stat -c%f $migrate_dir)
14900         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14901                 error "expect mode $old_dir_mode get $new_dir_mode"
14902
14903         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14904         [ "$old_file_mode" = "$new_file_mode" ] ||
14905                 error "expect mode $old_file_mode get $new_file_mode"
14906
14907         diff /etc/passwd ${migrate_dir}/$tfile ||
14908                 error "$tfile different after migration"
14909
14910         diff /etc/passwd ${other_dir}/luna ||
14911                 error "luna different after migration"
14912
14913         diff /etc/passwd ${migrate_dir}/sofia ||
14914                 error "sofia different after migration"
14915
14916         diff /etc/passwd ${other_dir}/zachary ||
14917                 error "zachary different after migration"
14918
14919         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14920                 error "${tfile}_ln different after migration"
14921
14922         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14923                 error "${tfile}_ln_other different after migration"
14924
14925         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
14926         [ $stripe_count = 2 ] ||
14927                 error "dir strpe_count $d != 2 after migration."
14928
14929         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
14930         [ $stripe_count = 2 ] ||
14931                 error "file strpe_count $d != 2 after migration."
14932
14933         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14934 }
14935 run_test 230b "migrate directory"
14936
14937 test_230c() {
14938         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14939         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14940         remote_mds_nodsh && skip "remote MDS with nodsh"
14941
14942         local MDTIDX=1
14943         local mdt_index
14944         local file
14945         local migrate_dir=$DIR/$tdir/migrate_dir
14946
14947         #If migrating directory fails in the middle, all entries of
14948         #the directory is still accessiable.
14949         test_mkdir $DIR/$tdir
14950         test_mkdir -i0 -c1 $migrate_dir
14951         stat $migrate_dir
14952         createmany -o $migrate_dir/f 10 ||
14953                 error "create files under ${migrate_dir} failed"
14954
14955         #failed after migrating 5 entries
14956         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14957         do_facet mds1 lctl set_param fail_loc=0x20001801
14958         do_facet mds1 lctl  set_param fail_val=5
14959         local t=$(ls $migrate_dir | wc -l)
14960         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
14961                 error "migrate should fail after 5 entries"
14962
14963         mkdir $migrate_dir/dir &&
14964                 error "mkdir succeeds under migrating directory"
14965         touch $migrate_dir/file &&
14966                 error "touch file succeeds under migrating directory"
14967
14968         local u=$(ls $migrate_dir | wc -l)
14969         [ "$u" == "$t" ] || error "$u != $t during migration"
14970
14971         for file in $(find $migrate_dir); do
14972                 stat $file || error "stat $file failed"
14973         done
14974
14975         do_facet mds1 lctl set_param fail_loc=0
14976         do_facet mds1 lctl set_param fail_val=0
14977
14978         $LFS migrate -m $MDTIDX $migrate_dir ||
14979                 error "migrate open files should failed with open files"
14980
14981         echo "Finish migration, then checking.."
14982         for file in $(find $migrate_dir); do
14983                 mdt_index=$($LFS getstripe -m $file)
14984                 [ $mdt_index == $MDTIDX ] ||
14985                         error "$file is not on MDT${MDTIDX}"
14986         done
14987
14988         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14989 }
14990 run_test 230c "check directory accessiblity if migration is failed"
14991
14992 test_230d() {
14993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14994         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14995
14996         local MDTIDX=1
14997         local mdt_index
14998         local migrate_dir=$DIR/$tdir/migrate_dir
14999         local i
15000         local j
15001
15002         test_mkdir $DIR/$tdir
15003         test_mkdir -i0 -c1 $migrate_dir
15004
15005         for ((i=0; i<100; i++)); do
15006                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
15007                 createmany -o $migrate_dir/dir_${i}/f 100 ||
15008                         error "create files under remote dir failed $i"
15009         done
15010
15011         $LFS migrate -m $MDTIDX $migrate_dir ||
15012                 error "migrate remote dir error"
15013
15014         echo "Finish migration, then checking.."
15015         for file in $(find $migrate_dir); do
15016                 mdt_index=$($LFS getstripe -m $file)
15017                 [ $mdt_index == $MDTIDX ] ||
15018                         error "$file is not on MDT${MDTIDX}"
15019         done
15020
15021         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15022 }
15023 run_test 230d "check migrate big directory"
15024
15025 test_230e() {
15026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15027         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15028
15029         local i
15030         local j
15031         local a_fid
15032         local b_fid
15033
15034         mkdir -p $DIR/$tdir
15035         mkdir $DIR/$tdir/migrate_dir
15036         mkdir $DIR/$tdir/other_dir
15037         touch $DIR/$tdir/migrate_dir/a
15038         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
15039         ls $DIR/$tdir/other_dir
15040
15041         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15042                 error "migrate dir fails"
15043
15044         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15045         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15046
15047         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15048         [ $mdt_index == 0 ] || error "a is not on MDT0"
15049
15050         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
15051                 error "migrate dir fails"
15052
15053         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
15054         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
15055
15056         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15057         [ $mdt_index == 1 ] || error "a is not on MDT1"
15058
15059         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
15060         [ $mdt_index == 1 ] || error "b is not on MDT1"
15061
15062         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15063         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
15064
15065         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
15066
15067         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15068 }
15069 run_test 230e "migrate mulitple local link files"
15070
15071 test_230f() {
15072         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15073         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15074
15075         local a_fid
15076         local ln_fid
15077
15078         mkdir -p $DIR/$tdir
15079         mkdir $DIR/$tdir/migrate_dir
15080         $LFS mkdir -i1 $DIR/$tdir/other_dir
15081         touch $DIR/$tdir/migrate_dir/a
15082         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
15083         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
15084         ls $DIR/$tdir/other_dir
15085
15086         # a should be migrated to MDT1, since no other links on MDT0
15087         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15088                 error "#1 migrate dir fails"
15089         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15090         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15091         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15092         [ $mdt_index == 1 ] || error "a is not on MDT1"
15093
15094         # a should stay on MDT1, because it is a mulitple link file
15095         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15096                 error "#2 migrate dir fails"
15097         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15098         [ $mdt_index == 1 ] || error "a is not on MDT1"
15099
15100         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15101                 error "#3 migrate dir fails"
15102
15103         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15104         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
15105         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
15106
15107         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
15108         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
15109
15110         # a should be migrated to MDT0, since no other links on MDT1
15111         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15112                 error "#4 migrate dir fails"
15113         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15114         [ $mdt_index == 0 ] || error "a is not on MDT0"
15115
15116         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15117 }
15118 run_test 230f "migrate mulitple remote link files"
15119
15120 test_230g() {
15121         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15122         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15123
15124         mkdir -p $DIR/$tdir/migrate_dir
15125
15126         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
15127                 error "migrating dir to non-exist MDT succeeds"
15128         true
15129 }
15130 run_test 230g "migrate dir to non-exist MDT"
15131
15132 test_230h() {
15133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15134         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15135         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
15136                 skip "Need MDS version at least 2.7.64"
15137
15138         local mdt_index
15139
15140         mkdir -p $DIR/$tdir/migrate_dir
15141
15142         $LFS migrate -m1 $DIR &&
15143                 error "migrating mountpoint1 should fail"
15144
15145         $LFS migrate -m1 $DIR/$tdir/.. &&
15146                 error "migrating mountpoint2 should fail"
15147
15148         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
15149                 error "migrating $tdir fail"
15150
15151         mdt_index=$($LFS getstripe -m $DIR/$tdir)
15152         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
15153
15154         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15155         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
15156
15157 }
15158 run_test 230h "migrate .. and root"
15159
15160 test_230i() {
15161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15162         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15163
15164         mkdir -p $DIR/$tdir/migrate_dir
15165
15166         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
15167                 error "migration fails with a tailing slash"
15168
15169         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
15170                 error "migration fails with two tailing slashes"
15171 }
15172 run_test 230i "lfs migrate -m tolerates trailing slashes"
15173
15174 test_231a()
15175 {
15176         # For simplicity this test assumes that max_pages_per_rpc
15177         # is the same across all OSCs
15178         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
15179         local bulk_size=$((max_pages * 4096))
15180         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
15181                                        head -n 1)
15182
15183         mkdir -p $DIR/$tdir
15184         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
15185                 error "failed to set stripe with -S ${brw_size}M option"
15186
15187         # clear the OSC stats
15188         $LCTL set_param osc.*.stats=0 &>/dev/null
15189         stop_writeback
15190
15191         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
15192         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
15193                 oflag=direct &>/dev/null || error "dd failed"
15194
15195         sync; sleep 1; sync # just to be safe
15196         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
15197         if [ x$nrpcs != "x1" ]; then
15198                 $LCTL get_param osc.*.stats
15199                 error "found $nrpcs ost_write RPCs, not 1 as expected"
15200         fi
15201
15202         start_writeback
15203         # Drop the OSC cache, otherwise we will read from it
15204         cancel_lru_locks osc
15205
15206         # clear the OSC stats
15207         $LCTL set_param osc.*.stats=0 &>/dev/null
15208
15209         # Client reads $bulk_size.
15210         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
15211                 iflag=direct &>/dev/null || error "dd failed"
15212
15213         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
15214         if [ x$nrpcs != "x1" ]; then
15215                 $LCTL get_param osc.*.stats
15216                 error "found $nrpcs ost_read RPCs, not 1 as expected"
15217         fi
15218 }
15219 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
15220
15221 test_231b() {
15222         mkdir -p $DIR/$tdir
15223         local i
15224         for i in {0..1023}; do
15225                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
15226                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
15227                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
15228         done
15229         sync
15230 }
15231 run_test 231b "must not assert on fully utilized OST request buffer"
15232
15233 test_232a() {
15234         mkdir -p $DIR/$tdir
15235         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15236
15237         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15238         do_facet ost1 $LCTL set_param fail_loc=0x31c
15239
15240         # ignore dd failure
15241         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
15242
15243         do_facet ost1 $LCTL set_param fail_loc=0
15244         umount_client $MOUNT || error "umount failed"
15245         mount_client $MOUNT || error "mount failed"
15246         stop ost1 || error "cannot stop ost1"
15247         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15248 }
15249 run_test 232a "failed lock should not block umount"
15250
15251 test_232b() {
15252         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.58) ] ||
15253                 skip "Need MDS version at least 2.10.58"
15254
15255         mkdir -p $DIR/$tdir
15256         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15257         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
15258         sync
15259         cancel_lru_locks osc
15260
15261         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15262         do_facet ost1 $LCTL set_param fail_loc=0x31c
15263
15264         # ignore failure
15265         $LFS data_version $DIR/$tdir/$tfile || true
15266
15267         do_facet ost1 $LCTL set_param fail_loc=0
15268         umount_client $MOUNT || error "umount failed"
15269         mount_client $MOUNT || error "mount failed"
15270         stop ost1 || error "cannot stop ost1"
15271         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15272 }
15273 run_test 232b "failed data version lock should not block umount"
15274
15275 test_233a() {
15276         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
15277                 skip "Need MDS version at least 2.3.64"
15278         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15279
15280         local fid=$($LFS path2fid $MOUNT)
15281
15282         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15283                 error "cannot access $MOUNT using its FID '$fid'"
15284 }
15285 run_test 233a "checking that OBF of the FS root succeeds"
15286
15287 test_233b() {
15288         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
15289                 skip "Need MDS version at least 2.5.90"
15290         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15291
15292         local fid=$($LFS path2fid $MOUNT/.lustre)
15293
15294         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15295                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
15296
15297         fid=$($LFS path2fid $MOUNT/.lustre/fid)
15298         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15299                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
15300 }
15301 run_test 233b "checking that OBF of the FS .lustre succeeds"
15302
15303 test_234() {
15304         local p="$TMP/sanityN-$TESTNAME.parameters"
15305         save_lustre_params client "llite.*.xattr_cache" > $p
15306         lctl set_param llite.*.xattr_cache 1 ||
15307                 skip_env "xattr cache is not supported"
15308
15309         mkdir -p $DIR/$tdir || error "mkdir failed"
15310         touch $DIR/$tdir/$tfile || error "touch failed"
15311         # OBD_FAIL_LLITE_XATTR_ENOMEM
15312         $LCTL set_param fail_loc=0x1405
15313         getfattr -n user.attr $DIR/$tdir/$tfile &&
15314                 error "getfattr should have failed with ENOMEM"
15315         $LCTL set_param fail_loc=0x0
15316         rm -rf $DIR/$tdir
15317
15318         restore_lustre_params < $p
15319         rm -f $p
15320 }
15321 run_test 234 "xattr cache should not crash on ENOMEM"
15322
15323 test_235() {
15324         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
15325                 skip "Need MDS version at least 2.4.52"
15326
15327         flock_deadlock $DIR/$tfile
15328         local RC=$?
15329         case $RC in
15330                 0)
15331                 ;;
15332                 124) error "process hangs on a deadlock"
15333                 ;;
15334                 *) error "error executing flock_deadlock $DIR/$tfile"
15335                 ;;
15336         esac
15337 }
15338 run_test 235 "LU-1715: flock deadlock detection does not work properly"
15339
15340 #LU-2935
15341 test_236() {
15342         check_swap_layouts_support
15343
15344         local ref1=/etc/passwd
15345         local ref2=/etc/group
15346         local file1=$DIR/$tdir/f1
15347         local file2=$DIR/$tdir/f2
15348
15349         test_mkdir -c1 $DIR/$tdir
15350         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
15351         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
15352         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
15353         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
15354         local fd=$(free_fd)
15355         local cmd="exec $fd<>$file2"
15356         eval $cmd
15357         rm $file2
15358         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
15359                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
15360         cmd="exec $fd>&-"
15361         eval $cmd
15362         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15363
15364         #cleanup
15365         rm -rf $DIR/$tdir
15366 }
15367 run_test 236 "Layout swap on open unlinked file"
15368
15369 # test to verify file handle related system calls
15370 # (name_to_handle_at/open_by_handle_at)
15371 # The new system calls are supported in glibc >= 2.14.
15372
15373 test_237() {
15374         echo "Test file_handle syscalls" > $DIR/$tfile ||
15375                 error "write failed"
15376         check_fhandle_syscalls $DIR/$tfile ||
15377                 error "check_fhandle_syscalls failed"
15378 }
15379 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
15380
15381 # LU-4659 linkea consistency
15382 test_238() {
15383         local server_version=$(lustre_version_code $SINGLEMDS)
15384
15385         [[ $server_version -gt $(version_code 2.5.57) ]] ||
15386                 [[ $server_version -gt $(version_code 2.5.1) &&
15387                    $server_version -lt $(version_code 2.5.50) ]] ||
15388                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15389
15390         touch $DIR/$tfile
15391         ln $DIR/$tfile $DIR/$tfile.lnk
15392         touch $DIR/$tfile.new
15393         mv $DIR/$tfile.new $DIR/$tfile
15394         local fid1=$($LFS path2fid $DIR/$tfile)
15395         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
15396         local path1=$($LFS fid2path $FSNAME "$fid1")
15397         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
15398         local path2=$($LFS fid2path $FSNAME "$fid2")
15399         [ $tfile.lnk == $path2 ] ||
15400                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
15401         rm -f $DIR/$tfile*
15402 }
15403 run_test 238 "Verify linkea consistency"
15404
15405 test_239A() { # was test_239
15406         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
15407                 skip "Need MDS version at least 2.5.60"
15408
15409         local list=$(comma_list $(mdts_nodes))
15410
15411         mkdir -p $DIR/$tdir
15412         createmany -o $DIR/$tdir/f- 5000
15413         unlinkmany $DIR/$tdir/f- 5000
15414         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
15415                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
15416         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
15417                         osp.*MDT*.sync_in_flight" | calc_sum)
15418         [ "$changes" -eq 0 ] || error "$changes not synced"
15419 }
15420 run_test 239A "osp_sync test"
15421
15422 test_239a() { #LU-5297
15423         remote_mds_nodsh && skip "remote MDS with nodsh"
15424
15425         touch $DIR/$tfile
15426         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
15427         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
15428         chgrp $RUNAS_GID $DIR/$tfile
15429         wait_delete_completed
15430 }
15431 run_test 239a "process invalid osp sync record correctly"
15432
15433 test_239b() { #LU-5297
15434         remote_mds_nodsh && skip "remote MDS with nodsh"
15435
15436         touch $DIR/$tfile1
15437         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
15438         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
15439         chgrp $RUNAS_GID $DIR/$tfile1
15440         wait_delete_completed
15441         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15442         touch $DIR/$tfile2
15443         chgrp $RUNAS_GID $DIR/$tfile2
15444         wait_delete_completed
15445 }
15446 run_test 239b "process osp sync record with ENOMEM error correctly"
15447
15448 test_240() {
15449         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15450         remote_mds_nodsh && skip "remote MDS with nodsh"
15451
15452         mkdir -p $DIR/$tdir
15453
15454         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
15455                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
15456         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
15457                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
15458
15459         umount_client $MOUNT || error "umount failed"
15460         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
15461         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
15462         mount_client $MOUNT || error "failed to mount client"
15463
15464         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
15465         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
15466 }
15467 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
15468
15469 test_241_bio() {
15470         for LOOP in $(seq $1); do
15471                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
15472                 cancel_lru_locks $OSC || true
15473         done
15474 }
15475
15476 test_241_dio() {
15477         for LOOP in $(seq $1); do
15478                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
15479                                                 iflag=direct 2>/dev/null
15480         done
15481 }
15482
15483 test_241a() { # was test_241
15484         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
15485         ls -la $DIR/$tfile
15486         cancel_lru_locks $OSC
15487         test_241_bio 1000 &
15488         PID=$!
15489         test_241_dio 1000
15490         wait $PID
15491 }
15492 run_test 241a "bio vs dio"
15493
15494 test_241b() {
15495         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
15496         ls -la $DIR/$tfile
15497         test_241_dio 1000 &
15498         PID=$!
15499         test_241_dio 1000
15500         wait $PID
15501 }
15502 run_test 241b "dio vs dio"
15503
15504 test_242() {
15505         remote_mds_nodsh && skip "remote MDS with nodsh"
15506
15507         mkdir -p $DIR/$tdir
15508         touch $DIR/$tdir/$tfile
15509
15510         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
15511         do_facet mds1 lctl set_param fail_loc=0x105
15512         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
15513
15514         do_facet mds1 lctl set_param fail_loc=0
15515         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
15516 }
15517 run_test 242 "mdt_readpage failure should not cause directory unreadable"
15518
15519 test_243()
15520 {
15521         test_mkdir $DIR/$tdir
15522         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
15523 }
15524 run_test 243 "various group lock tests"
15525
15526 test_244()
15527 {
15528         test_mkdir $DIR/$tdir
15529         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
15530         sendfile_grouplock $DIR/$tdir/$tfile || \
15531                 error "sendfile+grouplock failed"
15532         rm -rf $DIR/$tdir
15533 }
15534 run_test 244 "sendfile with group lock tests"
15535
15536 test_245() {
15537         local flagname="multi_mod_rpcs"
15538         local connect_data_name="max_mod_rpcs"
15539         local out
15540
15541         # check if multiple modify RPCs flag is set
15542         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
15543                 grep "connect_flags:")
15544         echo "$out"
15545
15546         echo "$out" | grep -qw $flagname
15547         if [ $? -ne 0 ]; then
15548                 echo "connect flag $flagname is not set"
15549                 return
15550         fi
15551
15552         # check if multiple modify RPCs data is set
15553         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
15554         echo "$out"
15555
15556         echo "$out" | grep -qw $connect_data_name ||
15557                 error "import should have connect data $connect_data_name"
15558 }
15559 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
15560
15561 test_246() { # LU-7371
15562         remote_ost_nodsh && skip "remote OST with nodsh"
15563         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
15564                 skip "Need OST version >= 2.7.62"
15565
15566         do_facet ost1 $LCTL set_param fail_val=4095
15567 #define OBD_FAIL_OST_READ_SIZE          0x234
15568         do_facet ost1 $LCTL set_param fail_loc=0x234
15569         $LFS setstripe $DIR/$tfile -i 0 -c 1
15570         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
15571         cancel_lru_locks $FSNAME-OST0000
15572         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
15573 }
15574 run_test 246 "Read file of size 4095 should return right length"
15575
15576 cleanup_247() {
15577         local submount=$1
15578
15579         trap 0
15580         umount_client $submount
15581         rmdir $submount
15582 }
15583
15584 test_247a() {
15585         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
15586                 grep -q subtree ||
15587                 skip_env "Fileset feature is not supported"
15588
15589         local submount=${MOUNT}_$tdir
15590
15591         mkdir $MOUNT/$tdir
15592         mkdir -p $submount || error "mkdir $submount failed"
15593         FILESET="$FILESET/$tdir" mount_client $submount ||
15594                 error "mount $submount failed"
15595         trap "cleanup_247 $submount" EXIT
15596         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
15597         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
15598                 error "read $MOUNT/$tdir/$tfile failed"
15599         cleanup_247 $submount
15600 }
15601 run_test 247a "mount subdir as fileset"
15602
15603 test_247b() {
15604         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15605                 skip_env "Fileset feature is not supported"
15606
15607         local submount=${MOUNT}_$tdir
15608
15609         rm -rf $MOUNT/$tdir
15610         mkdir -p $submount || error "mkdir $submount failed"
15611         SKIP_FILESET=1
15612         FILESET="$FILESET/$tdir" mount_client $submount &&
15613                 error "mount $submount should fail"
15614         rmdir $submount
15615 }
15616 run_test 247b "mount subdir that dose not exist"
15617
15618 test_247c() {
15619         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15620                 skip_env "Fileset feature is not supported"
15621
15622         local submount=${MOUNT}_$tdir
15623
15624         mkdir -p $MOUNT/$tdir/dir1
15625         mkdir -p $submount || error "mkdir $submount failed"
15626         trap "cleanup_247 $submount" EXIT
15627         FILESET="$FILESET/$tdir" mount_client $submount ||
15628                 error "mount $submount failed"
15629         local fid=$($LFS path2fid $MOUNT/)
15630         $LFS fid2path $submount $fid && error "fid2path should fail"
15631         cleanup_247 $submount
15632 }
15633 run_test 247c "running fid2path outside root"
15634
15635 test_247d() {
15636         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15637                 skip "Fileset feature is not supported"
15638
15639         local submount=${MOUNT}_$tdir
15640
15641         mkdir -p $MOUNT/$tdir/dir1
15642         mkdir -p $submount || error "mkdir $submount failed"
15643         FILESET="$FILESET/$tdir" mount_client $submount ||
15644                 error "mount $submount failed"
15645         trap "cleanup_247 $submount" EXIT
15646         local fid=$($LFS path2fid $submount/dir1)
15647         $LFS fid2path $submount $fid || error "fid2path should succeed"
15648         cleanup_247 $submount
15649 }
15650 run_test 247d "running fid2path inside root"
15651
15652 # LU-8037
15653 test_247e() {
15654         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
15655                 grep -q subtree ||
15656                 skip "Fileset feature is not supported"
15657
15658         local submount=${MOUNT}_$tdir
15659
15660         mkdir $MOUNT/$tdir
15661         mkdir -p $submount || error "mkdir $submount failed"
15662         FILESET="$FILESET/.." mount_client $submount &&
15663                 error "mount $submount should fail"
15664         rmdir $submount
15665 }
15666 run_test 247e "mount .. as fileset"
15667
15668 test_248() {
15669         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
15670         [ -z "$fast_read_sav" ] && skip "no fast read support"
15671
15672         # create a large file for fast read verification
15673         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
15674
15675         # make sure the file is created correctly
15676         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
15677                 { rm -f $DIR/$tfile; skip "file creation error"; }
15678
15679         echo "Test 1: verify that fast read is 4 times faster on cache read"
15680
15681         # small read with fast read enabled
15682         $LCTL set_param -n llite.*.fast_read=1
15683         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15684                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15685                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15686         # small read with fast read disabled
15687         $LCTL set_param -n llite.*.fast_read=0
15688         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15689                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15690                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15691
15692         # verify that fast read is 4 times faster for cache read
15693         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
15694                 error_not_in_vm "fast read was not 4 times faster: " \
15695                            "$t_fast vs $t_slow"
15696
15697         echo "Test 2: verify the performance between big and small read"
15698         $LCTL set_param -n llite.*.fast_read=1
15699
15700         # 1k non-cache read
15701         cancel_lru_locks osc
15702         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15703                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15704                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15705
15706         # 1M non-cache read
15707         cancel_lru_locks osc
15708         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15709                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15710                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15711
15712         # verify that big IO is not 4 times faster than small IO
15713         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
15714                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
15715
15716         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
15717         rm -f $DIR/$tfile
15718 }
15719 run_test 248 "fast read verification"
15720
15721 test_249() { # LU-7890
15722         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
15723                 skip "Need at least version 2.8.54"
15724
15725         rm -f $DIR/$tfile
15726         $SETSTRIPE -c 1 $DIR/$tfile
15727         # Offset 2T == 4k * 512M
15728         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
15729                 error "dd to 2T offset failed"
15730 }
15731 run_test 249 "Write above 2T file size"
15732
15733 test_250() {
15734         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
15735          && skip "no 16TB file size limit on ZFS"
15736
15737         $SETSTRIPE -c 1 $DIR/$tfile
15738         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
15739         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
15740         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
15741         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
15742                 conv=notrunc,fsync && error "append succeeded"
15743         return 0
15744 }
15745 run_test 250 "Write above 16T limit"
15746
15747 test_251() {
15748         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
15749
15750         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
15751         #Skip once - writing the first stripe will succeed
15752         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15753         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
15754                 error "short write happened"
15755
15756         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15757         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
15758                 error "short read happened"
15759
15760         rm -f $DIR/$tfile
15761 }
15762 run_test 251 "Handling short read and write correctly"
15763
15764 test_252() {
15765         remote_mds_nodsh && skip "remote MDS with nodsh"
15766         remote_ost_nodsh && skip "remote OST with nodsh"
15767         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
15768              "$(facet_fstype mds1)" != "ldiskfs" ]; then
15769                 skip_env "ldiskfs only test"
15770         fi
15771
15772         local tgt
15773         local dev
15774         local out
15775         local uuid
15776         local num
15777         local gen
15778
15779         # check lr_reader on OST0000
15780         tgt=ost1
15781         dev=$(facet_device $tgt)
15782         out=$(do_facet $tgt $LR_READER $dev)
15783         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15784         echo "$out"
15785         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
15786         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
15787                 error "Invalid uuid returned by $LR_READER on target $tgt"
15788         echo -e "uuid returned by $LR_READER is '$uuid'\n"
15789
15790         # check lr_reader -c on MDT0000
15791         tgt=mds1
15792         dev=$(facet_device $tgt)
15793         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
15794                 skip "$LR_READER does not support additional options"
15795         fi
15796         out=$(do_facet $tgt $LR_READER -c $dev)
15797         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15798         echo "$out"
15799         num=$(echo "$out" | grep -c "mdtlov")
15800         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
15801                 error "Invalid number of mdtlov clients returned by $LR_READER"
15802         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
15803
15804         # check lr_reader -cr on MDT0000
15805         out=$(do_facet $tgt $LR_READER -cr $dev)
15806         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15807         echo "$out"
15808         echo "$out" | grep -q "^reply_data:$" ||
15809                 error "$LR_READER should have returned 'reply_data' section"
15810         num=$(echo "$out" | grep -c "client_generation")
15811         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
15812 }
15813 run_test 252 "check lr_reader tool"
15814
15815 test_253_fill_ost() {
15816         local size_mb #how many MB should we write to pass watermark
15817         local lwm=$3  #low watermark
15818         local free_10mb #10% of free space
15819
15820         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15821         size_mb=$((free_kb / 1024 - lwm))
15822         free_10mb=$((free_kb / 10240))
15823         #If 10% of free space cross low watermark use it
15824         if (( free_10mb > size_mb )); then
15825                 size_mb=$free_10mb
15826         else
15827                 #At least we need to store 1.1 of difference between
15828                 #free space and low watermark
15829                 size_mb=$((size_mb + size_mb / 10))
15830         fi
15831         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
15832                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
15833                          oflag=append conv=notrunc
15834         fi
15835
15836         sleep_maxage
15837
15838         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15839         echo "OST still has $((free_kb / 1024)) mbytes free"
15840 }
15841
15842 test_253() {
15843         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15844         remote_mds_nodsh && skip "remote MDS with nodsh"
15845         remote_mgs_nodsh && skip "remote MGS with nodsh"
15846
15847         local ostidx=0
15848         local rc=0
15849
15850         local ost_name=$($LFS osts |
15851                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
15852         # on the mdt's osc
15853         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
15854         do_facet $SINGLEMDS $LCTL get_param -n \
15855                 osp.$mdtosc_proc1.reserved_mb_high ||
15856                 skip  "remote MDS does not support reserved_mb_high"
15857
15858         rm -rf $DIR/$tdir
15859         wait_mds_ost_sync
15860         wait_delete_completed
15861         mkdir $DIR/$tdir
15862
15863         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
15864                         osp.$mdtosc_proc1.reserved_mb_high)
15865         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
15866                         osp.$mdtosc_proc1.reserved_mb_low)
15867         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
15868
15869         if ! combined_mgs_mds ; then
15870                 mount_mgs_client
15871         fi
15872         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
15873         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
15874                 error "Adding $ost_name to pool failed"
15875
15876         # Wait for client to see a OST at pool
15877         wait_update $HOSTNAME "$LCTL get_param -n
15878                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
15879                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
15880                 error "Client can not see the pool"
15881         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
15882                 error "Setstripe failed"
15883
15884         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
15885         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
15886         echo "OST still has $((blocks/1024)) mbytes free"
15887
15888         local new_lwm=$((blocks/1024-10))
15889         do_facet $SINGLEMDS $LCTL set_param \
15890                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
15891         do_facet $SINGLEMDS $LCTL set_param \
15892                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
15893
15894         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15895
15896         #First enospc could execute orphan deletion so repeat.
15897         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15898
15899         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15900                         osp.$mdtosc_proc1.prealloc_status)
15901         echo "prealloc_status $oa_status"
15902
15903         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
15904                 error "File creation should fail"
15905         #object allocation was stopped, but we still able to append files
15906         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
15907                 error "Append failed"
15908         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
15909
15910         wait_delete_completed
15911
15912         sleep_maxage
15913
15914         for i in $(seq 10 12); do
15915                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
15916                         error "File creation failed after rm";
15917         done
15918
15919         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15920                         osp.$mdtosc_proc1.prealloc_status)
15921         echo "prealloc_status $oa_status"
15922
15923         if (( oa_status != 0 )); then
15924                 error "Object allocation still disable after rm"
15925         fi
15926         do_facet $SINGLEMDS $LCTL set_param \
15927                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
15928         do_facet $SINGLEMDS $LCTL set_param \
15929                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
15930
15931
15932         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
15933                 error "Remove $ost_name from pool failed"
15934         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15935                 error "Pool destroy fialed"
15936
15937         if ! combined_mgs_mds ; then
15938                 umount_mgs_client
15939         fi
15940 }
15941 run_test 253 "Check object allocation limit"
15942
15943 test_254() {
15944         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15945         remote_mds_nodsh && skip "remote MDS with nodsh"
15946         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
15947                 skip "MDS does not support changelog_size"
15948
15949         local cl_user
15950         local MDT0=$(facet_svc $SINGLEMDS)
15951
15952         changelog_register || error "changelog_register failed"
15953
15954         changelog_clear 0 || error "changelog_clear failed"
15955
15956         local size1=$(do_facet $SINGLEMDS \
15957                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15958         echo "Changelog size $size1"
15959
15960         rm -rf $DIR/$tdir
15961         $LFS mkdir -i 0 $DIR/$tdir
15962         # change something
15963         mkdir -p $DIR/$tdir/pics/2008/zachy
15964         touch $DIR/$tdir/pics/2008/zachy/timestamp
15965         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
15966         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15967         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15968         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15969         rm $DIR/$tdir/pics/desktop.jpg
15970
15971         local size2=$(do_facet $SINGLEMDS \
15972                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15973         echo "Changelog size after work $size2"
15974
15975         (( $size2 > $size1 )) ||
15976                 error "new Changelog size=$size2 less than old size=$size1"
15977 }
15978 run_test 254 "Check changelog size"
15979
15980 ladvise_no_type()
15981 {
15982         local type=$1
15983         local file=$2
15984
15985         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
15986                 awk -F: '{print $2}' | grep $type > /dev/null
15987         if [ $? -ne 0 ]; then
15988                 return 0
15989         fi
15990         return 1
15991 }
15992
15993 ladvise_no_ioctl()
15994 {
15995         local file=$1
15996
15997         lfs ladvise -a willread $file > /dev/null 2>&1
15998         if [ $? -eq 0 ]; then
15999                 return 1
16000         fi
16001
16002         lfs ladvise -a willread $file 2>&1 |
16003                 grep "Inappropriate ioctl for device" > /dev/null
16004         if [ $? -eq 0 ]; then
16005                 return 0
16006         fi
16007         return 1
16008 }
16009
16010 percent() {
16011         bc <<<"scale=2; ($1 - $2) * 100 / $2"
16012 }
16013
16014 # run a random read IO workload
16015 # usage: random_read_iops <filename> <filesize> <iosize>
16016 random_read_iops() {
16017         local file=$1
16018         local fsize=$2
16019         local iosize=${3:-4096}
16020
16021         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
16022                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
16023 }
16024
16025 drop_file_oss_cache() {
16026         local file="$1"
16027         local nodes="$2"
16028
16029         $LFS ladvise -a dontneed $file 2>/dev/null ||
16030                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
16031 }
16032
16033 ladvise_willread_performance()
16034 {
16035         local repeat=10
16036         local average_origin=0
16037         local average_cache=0
16038         local average_ladvise=0
16039
16040         for ((i = 1; i <= $repeat; i++)); do
16041                 echo "Iter $i/$repeat: reading without willread hint"
16042                 cancel_lru_locks osc
16043                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16044                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
16045                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
16046                 average_origin=$(bc <<<"$average_origin + $speed_origin")
16047
16048                 cancel_lru_locks osc
16049                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
16050                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
16051                 average_cache=$(bc <<<"$average_cache + $speed_cache")
16052
16053                 cancel_lru_locks osc
16054                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16055                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
16056                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
16057                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
16058                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
16059         done
16060         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
16061         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
16062         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
16063
16064         speedup_cache=$(percent $average_cache $average_origin)
16065         speedup_ladvise=$(percent $average_ladvise $average_origin)
16066
16067         echo "Average uncached read: $average_origin"
16068         echo "Average speedup with OSS cached read: " \
16069                 "$average_cache = +$speedup_cache%"
16070         echo "Average speedup with ladvise willread: " \
16071                 "$average_ladvise = +$speedup_ladvise%"
16072
16073         local lowest_speedup=20
16074         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
16075                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
16076                         "got $average_cache%. Skipping ladvise willread check."
16077                 return 0
16078         fi
16079
16080         # the test won't work on ZFS until it supports 'ladvise dontneed', but
16081         # it is still good to run until then to exercise 'ladvise willread'
16082         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16083                 [ "$(facet_fstype ost1)" = "zfs" ] &&
16084                 echo "osd-zfs does not support dontneed or drop_caches" &&
16085                 return 0
16086
16087         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
16088         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
16089                 error_not_in_vm "Speedup with willread is less than " \
16090                         "$lowest_speedup%, got $average_ladvise%"
16091 }
16092
16093 test_255a() {
16094         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
16095                 skip "lustre < 2.8.54 does not support ladvise "
16096         remote_ost_nodsh && skip "remote OST with nodsh"
16097
16098         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
16099
16100         ladvise_no_type willread $DIR/$tfile &&
16101                 skip "willread ladvise is not supported"
16102
16103         ladvise_no_ioctl $DIR/$tfile &&
16104                 skip "ladvise ioctl is not supported"
16105
16106         local size_mb=100
16107         local size=$((size_mb * 1048576))
16108         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16109                 error "dd to $DIR/$tfile failed"
16110
16111         lfs ladvise -a willread $DIR/$tfile ||
16112                 error "Ladvise failed with no range argument"
16113
16114         lfs ladvise -a willread -s 0 $DIR/$tfile ||
16115                 error "Ladvise failed with no -l or -e argument"
16116
16117         lfs ladvise -a willread -e 1 $DIR/$tfile ||
16118                 error "Ladvise failed with only -e argument"
16119
16120         lfs ladvise -a willread -l 1 $DIR/$tfile ||
16121                 error "Ladvise failed with only -l argument"
16122
16123         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
16124                 error "End offset should not be smaller than start offset"
16125
16126         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
16127                 error "End offset should not be equal to start offset"
16128
16129         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
16130                 error "Ladvise failed with overflowing -s argument"
16131
16132         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
16133                 error "Ladvise failed with overflowing -e argument"
16134
16135         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
16136                 error "Ladvise failed with overflowing -l argument"
16137
16138         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
16139                 error "Ladvise succeeded with conflicting -l and -e arguments"
16140
16141         echo "Synchronous ladvise should wait"
16142         local delay=4
16143 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
16144         do_nodes $(comma_list $(osts_nodes)) \
16145                 $LCTL set_param fail_val=$delay fail_loc=0x237
16146
16147         local start_ts=$SECONDS
16148         lfs ladvise -a willread $DIR/$tfile ||
16149                 error "Ladvise failed with no range argument"
16150         local end_ts=$SECONDS
16151         local inteval_ts=$((end_ts - start_ts))
16152
16153         if [ $inteval_ts -lt $(($delay - 1)) ]; then
16154                 error "Synchronous advice didn't wait reply"
16155         fi
16156
16157         echo "Asynchronous ladvise shouldn't wait"
16158         local start_ts=$SECONDS
16159         lfs ladvise -a willread -b $DIR/$tfile ||
16160                 error "Ladvise failed with no range argument"
16161         local end_ts=$SECONDS
16162         local inteval_ts=$((end_ts - start_ts))
16163
16164         if [ $inteval_ts -gt $(($delay / 2)) ]; then
16165                 error "Asynchronous advice blocked"
16166         fi
16167
16168         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
16169         ladvise_willread_performance
16170 }
16171 run_test 255a "check 'lfs ladvise -a willread'"
16172
16173 facet_meminfo() {
16174         local facet=$1
16175         local info=$2
16176
16177         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
16178 }
16179
16180 test_255b() {
16181         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
16182                 skip "lustre < 2.8.54 does not support ladvise "
16183         remote_ost_nodsh && skip "remote OST with nodsh"
16184
16185         lfs setstripe -c 1 -i 0 $DIR/$tfile
16186
16187         ladvise_no_type dontneed $DIR/$tfile &&
16188                 skip "dontneed ladvise is not supported"
16189
16190         ladvise_no_ioctl $DIR/$tfile &&
16191                 skip "ladvise ioctl is not supported"
16192
16193         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16194                 [ "$(facet_fstype ost1)" = "zfs" ] &&
16195                 skip "zfs-osd does not support 'ladvise dontneed'"
16196
16197         local size_mb=100
16198         local size=$((size_mb * 1048576))
16199         # In order to prevent disturbance of other processes, only check 3/4
16200         # of the memory usage
16201         local kibibytes=$((size_mb * 1024 * 3 / 4))
16202
16203         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16204                 error "dd to $DIR/$tfile failed"
16205
16206         local total=$(facet_meminfo ost1 MemTotal)
16207         echo "Total memory: $total KiB"
16208
16209         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
16210         local before_read=$(facet_meminfo ost1 Cached)
16211         echo "Cache used before read: $before_read KiB"
16212
16213         lfs ladvise -a willread $DIR/$tfile ||
16214                 error "Ladvise willread failed"
16215         local after_read=$(facet_meminfo ost1 Cached)
16216         echo "Cache used after read: $after_read KiB"
16217
16218         lfs ladvise -a dontneed $DIR/$tfile ||
16219                 error "Ladvise dontneed again failed"
16220         local no_read=$(facet_meminfo ost1 Cached)
16221         echo "Cache used after dontneed ladvise: $no_read KiB"
16222
16223         if [ $total -lt $((before_read + kibibytes)) ]; then
16224                 echo "Memory is too small, abort checking"
16225                 return 0
16226         fi
16227
16228         if [ $((before_read + kibibytes)) -gt $after_read ]; then
16229                 error "Ladvise willread should use more memory" \
16230                         "than $kibibytes KiB"
16231         fi
16232
16233         if [ $((no_read + kibibytes)) -gt $after_read ]; then
16234                 error "Ladvise dontneed should release more memory" \
16235                         "than $kibibytes KiB"
16236         fi
16237 }
16238 run_test 255b "check 'lfs ladvise -a dontneed'"
16239
16240 test_255c() {
16241         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
16242                 skip "lustre < 2.10.53 does not support lockahead"
16243
16244         local count
16245         local new_count
16246         local difference
16247         local i
16248         local rc
16249
16250         test_mkdir -p $DIR/$tdir
16251         $SETSTRIPE -i 0 $DIR/$tdir
16252
16253         #test 10 returns only success/failure
16254         i=10
16255         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16256         rc=$?
16257         if [ $rc -eq 255 ]; then
16258                 error "Ladvise test${i} failed, ${rc}"
16259         fi
16260
16261         #test 11 counts lock enqueue requests, all others count new locks
16262         i=11
16263         count=$(do_facet ost1 \
16264                 $LCTL get_param -n ost.OSS.ost.stats)
16265         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
16266
16267         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16268         rc=$?
16269         if [ $rc -eq 255 ]; then
16270                 error "Ladvise test${i} failed, ${rc}"
16271         fi
16272
16273         new_count=$(do_facet ost1 \
16274                 $LCTL get_param -n ost.OSS.ost.stats)
16275         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
16276                    awk '{ print $2 }')
16277
16278         difference="$((new_count - count))"
16279         if [ $difference -ne $rc ]; then
16280                 error "Ladvise test${i}, bad enqueue count, returned " \
16281                       "${rc}, actual ${difference}"
16282         fi
16283
16284         for i in $(seq 12 21); do
16285                 # If we do not do this, we run the risk of having too many
16286                 # locks and starting lock cancellation while we are checking
16287                 # lock counts.
16288                 cancel_lru_locks osc
16289
16290                 count=$($LCTL get_param -n \
16291                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
16292
16293                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
16294                 rc=$?
16295                 if [ $rc -eq 255 ]; then
16296                         error "Ladvise test ${i} failed, ${rc}"
16297                 fi
16298
16299                 new_count=$($LCTL get_param -n \
16300                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
16301                 difference="$((new_count - count))"
16302
16303                 # Test 15 output is divided by 100 to map down to valid return
16304                 if [ $i -eq 15 ]; then
16305                         rc="$((rc * 100))"
16306                 fi
16307
16308                 if [ $difference -ne $rc ]; then
16309                         error "Ladvise test ${i}, bad lock count, returned " \
16310                               "${rc}, actual ${difference}"
16311                 fi
16312         done
16313
16314         #test 22 returns only success/failure
16315         i=22
16316         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16317         rc=$?
16318         if [ $rc -eq 255 ]; then
16319                 error "Ladvise test${i} failed, ${rc}"
16320         fi
16321 }
16322 run_test 255c "suite of ladvise lockahead tests"
16323
16324 test_256() {
16325         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16326         remote_mds_nodsh && skip "remote MDS with nodsh"
16327         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
16328                 skip "ldiskfs only test"
16329         changelog_users $SINGLEMDS | grep "^cl" &&
16330                 skip "active changelog user"
16331
16332         local cl_user
16333         local cat_sl
16334         local mdt_dev
16335
16336         mdt_dev=$(mdsdevname 1)
16337         echo $mdt_dev
16338
16339         changelog_register || error "changelog_register failed"
16340
16341         rm -rf $DIR/$tdir
16342         mkdir -p $DIR/$tdir
16343
16344         changelog_clear 0 || error "changelog_clear failed"
16345
16346         # change something
16347         touch $DIR/$tdir/{1..10}
16348
16349         # stop the MDT
16350         stop $SINGLEMDS || error "Fail to stop MDT"
16351
16352         # remount the MDT
16353
16354         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
16355
16356         #after mount new plainllog is used
16357         touch $DIR/$tdir/{11..19}
16358         local tmpfile=$(mktemp -u $tfile.XXXXXX)
16359         cat_sl=$(do_facet $SINGLEMDS "sync; \
16360                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16361                  llog_reader $tmpfile | grep -c type=1064553b")
16362         do_facet $SINGLEMDS llog_reader $tmpfile
16363
16364         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
16365
16366         changelog_clear 0 || error "changelog_clear failed"
16367
16368         cat_sl=$(do_facet $SINGLEMDS "sync; \
16369                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16370                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
16371
16372         if (( cat_sl == 2 )); then
16373                 error "Empty plain llog was not deleted from changelog catalog"
16374         elif (( cat_sl != 1 )); then
16375                 error "Active plain llog shouldn't be deleted from catalog"
16376         fi
16377 }
16378 run_test 256 "Check llog delete for empty and not full state"
16379
16380 test_257() {
16381         remote_mds_nodsh && skip "remote MDS with nodsh"
16382         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16383                 skip "Need MDS version at least 2.8.55"
16384
16385         test_mkdir $DIR/$tdir
16386
16387         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
16388                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
16389         stat $DIR/$tdir
16390
16391 #define OBD_FAIL_MDS_XATTR_REP                  0x161
16392         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
16393         local facet=mds$((mdtidx + 1))
16394         set_nodes_failloc $(facet_active_host $facet) 0x80000161
16395         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
16396
16397         stop $facet || error "stop MDS failed"
16398         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
16399                 error "start MDS fail"
16400 }
16401 run_test 257 "xattr locks are not lost"
16402
16403 # Verify we take the i_mutex when security requires it
16404 test_258a() {
16405 #define OBD_FAIL_IMUTEX_SEC 0x141c
16406         $LCTL set_param fail_loc=0x141c
16407         touch $DIR/$tfile
16408         chmod u+s $DIR/$tfile
16409         chmod a+rwx $DIR/$tfile
16410         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16411         RC=$?
16412         if [ $RC -ne 0 ]; then
16413                 error "error, failed to take i_mutex, rc=$?"
16414         fi
16415         rm -f $DIR/$tfile
16416 }
16417 run_test 258a
16418
16419 # Verify we do NOT take the i_mutex in the normal case
16420 test_258b() {
16421 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
16422         $LCTL set_param fail_loc=0x141d
16423         touch $DIR/$tfile
16424         chmod a+rwx $DIR
16425         chmod a+rw $DIR/$tfile
16426         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16427         RC=$?
16428         if [ $RC -ne 0 ]; then
16429                 error "error, took i_mutex unnecessarily, rc=$?"
16430         fi
16431         rm -f $DIR/$tfile
16432
16433 }
16434 run_test 258b "verify i_mutex security behavior"
16435
16436 test_259() {
16437         local file=$DIR/$tfile
16438         local before
16439         local after
16440
16441         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
16442                 skip "ldiskfs only test" && return
16443
16444         stack_trap "rm -f $file" EXIT
16445
16446         wait_delete_completed
16447         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16448         echo "before: $before"
16449
16450         $LFS setstripe -i 0 -c 1 $file
16451         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
16452         sync_all_data
16453         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16454         echo "after write: $after"
16455
16456 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
16457         do_facet ost1 $LCTL set_param fail_loc=0x2301
16458         $TRUNCATE $file 0
16459         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16460         echo "after truncate: $after"
16461
16462         stop ost1
16463         do_facet ost1 $LCTL set_param fail_loc=0
16464         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16465         sleep 2
16466         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16467         echo "after restart: $after"
16468         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
16469                 error "missing truncate?"
16470
16471         return 0
16472 }
16473 run_test 259 "crash at delayed truncate"
16474
16475 test_260() {
16476 #define OBD_FAIL_MDC_CLOSE               0x806
16477         $LCTL set_param fail_loc=0x80000806
16478         touch $DIR/$tfile
16479
16480 }
16481 run_test 260 "Check mdc_close fail"
16482
16483 ### Data-on-MDT sanity tests ###
16484 test_270a() {
16485         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16486                 skip "Need MDS version at least 2.10.55"
16487
16488         # create DoM file
16489         local dom=$DIR/$tdir/dom_file
16490         local tmp=$DIR/$tdir/tmp_file
16491
16492         mkdir -p $DIR/$tdir
16493
16494         # basic checks for DoM component creation
16495         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
16496                 error "Can set MDT layout to non-first entry"
16497
16498         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
16499                 error "Can define multiple entries as MDT layout"
16500
16501         $LFS setstripe -E 1M -L mdt $dom ||
16502                 error "Can't create DoM layout"
16503
16504         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
16505         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
16506         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
16507
16508         local mdtidx=$($LFS getstripe -m $dom)
16509         local mdtname=MDT$(printf %04x $mdtidx)
16510         local facet=mds$((mdtidx + 1))
16511         local space_check=1
16512
16513         # Skip free space checks with ZFS
16514         if [ "$(facet_fstype $facet)" == "zfs" ]; then
16515                 space_check=0
16516         fi
16517
16518         # write
16519         sync
16520         local mdtfree1=$(do_facet $facet \
16521                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16522         dd if=/dev/urandom of=$tmp bs=1024 count=100
16523         # check also direct IO along write
16524         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
16525         sync
16526         cmp $tmp $dom || error "file data is different"
16527         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
16528         if [ $space_check == 1 ]; then
16529                 local mdtfree2=$(do_facet $facet \
16530                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16531                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
16532                         error "MDT free space is wrong after write"
16533         fi
16534
16535         # truncate
16536         $TRUNCATE $dom 10000
16537         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
16538         if [ $space_check == 1 ]; then
16539                 mdtfree1=$(do_facet $facet \
16540                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16541                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
16542                         error "MDT free space is wrong after truncate"
16543         fi
16544
16545         # append
16546         cat $tmp >> $dom
16547         sync
16548         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
16549         if [ $space_check == 1 ]; then
16550                 mdtfree2=$(do_facet $facet \
16551                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16552                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
16553                         error "MDT free space is wrong after append"
16554         fi
16555
16556         # delete
16557         rm $dom
16558         if [ $space_check == 1 ]; then
16559                 mdtfree1=$(do_facet $facet \
16560                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16561                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
16562                         error "MDT free space is wrong after removal"
16563         fi
16564
16565         # combined striping
16566         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
16567                 error "Can't create DoM + OST striping"
16568
16569         dd if=/dev/urandom of=$tmp bs=1024 count=2000
16570         # check also direct IO along write
16571         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
16572         sync
16573         cmp $tmp $dom || error "file data is different"
16574         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
16575         rm $dom
16576         rm $tmp
16577
16578         return 0
16579 }
16580 run_test 270a "DoM: basic functionality tests"
16581
16582 test_270b() {
16583         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16584                 skip "Need MDS version at least 2.10.55"
16585
16586         local dom=$DIR/$tdir/dom_file
16587         local max_size=1048576
16588
16589         mkdir -p $DIR/$tdir
16590         $LFS setstripe -E $max_size -L mdt $dom
16591
16592         # truncate over the limit
16593         $TRUNCATE $dom $(($max_size + 1)) &&
16594                 error "successful truncate over the maximum size"
16595         # write over the limit
16596         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
16597                 error "successful write over the maximum size"
16598         # append over the limit
16599         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
16600         echo "12345" >> $dom && error "successful append over the maximum size"
16601         rm $dom
16602
16603         return 0
16604 }
16605 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
16606
16607 test_270c() {
16608         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16609                 skip "Need MDS version at least 2.10.55"
16610
16611         mkdir -p $DIR/$tdir
16612         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16613
16614         # check files inherit DoM EA
16615         touch $DIR/$tdir/first
16616         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
16617                 error "bad pattern"
16618         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
16619                 error "bad stripe count"
16620         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
16621                 error "bad stripe size"
16622
16623         # check directory inherits DoM EA and uses it as default
16624         mkdir $DIR/$tdir/subdir
16625         touch $DIR/$tdir/subdir/second
16626         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
16627                 error "bad pattern in sub-directory"
16628         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
16629                 error "bad stripe count in sub-directory"
16630         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
16631                 error "bad stripe size in sub-directory"
16632         return 0
16633 }
16634 run_test 270c "DoM: DoM EA inheritance tests"
16635
16636 test_270d() {
16637         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16638                 skip "Need MDS version at least 2.10.55"
16639
16640         mkdir -p $DIR/$tdir
16641         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16642
16643         # inherit default DoM striping
16644         mkdir $DIR/$tdir/subdir
16645         touch $DIR/$tdir/subdir/f1
16646
16647         # change default directory striping
16648         $LFS setstripe -c 1 $DIR/$tdir/subdir
16649         touch $DIR/$tdir/subdir/f2
16650         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
16651                 error "wrong default striping in file 2"
16652         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
16653                 error "bad pattern in file 2"
16654         return 0
16655 }
16656 run_test 270d "DoM: change striping from DoM to RAID0"
16657
16658 test_270e() {
16659         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16660                 skip "Need MDS version at least 2.10.55"
16661
16662         mkdir -p $DIR/$tdir/dom
16663         mkdir -p $DIR/$tdir/norm
16664         DOMFILES=20
16665         NORMFILES=10
16666         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
16667         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
16668
16669         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
16670         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
16671
16672         # find DoM files by layout
16673         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
16674         [ $NUM -eq  $DOMFILES ] ||
16675                 error "lfs find -L: found $NUM, expected $DOMFILES"
16676         echo "Test 1: lfs find 20 DOM files by layout: OK"
16677
16678         # there should be 1 dir with default DOM striping
16679         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
16680         [ $NUM -eq  1 ] ||
16681                 error "lfs find -L: found $NUM, expected 1 dir"
16682         echo "Test 2: lfs find 1 DOM dir by layout: OK"
16683
16684         # find DoM files by stripe size
16685         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
16686         [ $NUM -eq  $DOMFILES ] ||
16687                 error "lfs find -S: found $NUM, expected $DOMFILES"
16688         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
16689
16690         # find files by stripe offset except DoM files
16691         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
16692         [ $NUM -eq  $NORMFILES ] ||
16693                 error "lfs find -i: found $NUM, expected $NORMFILES"
16694         echo "Test 5: lfs find no DOM files by stripe index: OK"
16695         return 0
16696 }
16697 run_test 270e "DoM: lfs find with DoM files test"
16698
16699 test_270f() {
16700         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16701                 skip "Need MDS version at least 2.10.55"
16702
16703         local mdtname=${FSNAME}-MDT0000-mdtlov
16704         local dom=$DIR/$tdir/dom_file
16705         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
16706                                                 lod.$mdtname.dom_stripesize)
16707         local dom_limit=131072
16708
16709         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
16710         local dom_current=$(do_facet mds1 $LCTL get_param -n \
16711                                                 lod.$mdtname.dom_stripesize)
16712         [ ${dom_limit} -eq ${dom_current} ] ||
16713                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
16714
16715         $LFS mkdir -i 0 -c 1 $DIR/$tdir
16716         $LFS setstripe -d $DIR/$tdir
16717         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
16718                 error "Can't set directory default striping"
16719
16720         # exceed maximum stripe size
16721         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16722                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
16723         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
16724                 error "Able to create DoM component size more than LOD limit"
16725
16726         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
16727         dom_current=$(do_facet mds1 $LCTL get_param -n \
16728                                                 lod.$mdtname.dom_stripesize)
16729         [ 0 -eq ${dom_current} ] ||
16730                 error "Can't set zero DoM stripe limit"
16731         rm $dom
16732
16733         # attempt to create DoM file on server with disabled DoM should
16734         # remove DoM entry from layout and be succeed
16735         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
16736                 error "Can't create DoM file (DoM is disabled)"
16737         [ $($LFS getstripe -L $dom) == "mdt" ] &&
16738                 error "File has DoM component while DoM is disabled"
16739         rm $dom
16740
16741         # attempt to create DoM file with only DoM stripe should return error
16742         $LFS setstripe -E $dom_limit -L mdt $dom &&
16743                 error "Able to create DoM-only file while DoM is disabled"
16744
16745         # too low values to be aligned with smallest stripe size 64K
16746         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
16747         dom_current=$(do_facet mds1 $LCTL get_param -n \
16748                                                 lod.$mdtname.dom_stripesize)
16749         [ 30000 -eq ${dom_current} ] &&
16750                 error "Can set too small DoM stripe limit"
16751
16752         # 64K is a minimal stripe size in Lustre, expect limit of that size
16753         [ 65536 -eq ${dom_current} ] ||
16754                 error "Limit is not set to 64K but ${dom_current}"
16755
16756         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
16757         dom_current=$(do_facet mds1 $LCTL get_param -n \
16758                                                 lod.$mdtname.dom_stripesize)
16759         echo $dom_current
16760         [ 2147483648 -eq ${dom_current} ] &&
16761                 error "Can set too large DoM stripe limit"
16762
16763         do_facet mds1 $LCTL set_param -n \
16764                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
16765         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16766                 error "Can't create DoM component size after limit change"
16767         do_facet mds1 $LCTL set_param -n \
16768                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
16769         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
16770                 error "Can't create DoM file after limit decrease"
16771         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
16772                 error "Can create big DoM component after limit decrease"
16773         touch ${dom}_def ||
16774                 error "Can't create file with old default layout"
16775
16776         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
16777         return 0
16778 }
16779 run_test 270f "DoM: maximum DoM stripe size checks"
16780
16781 test_271a() {
16782         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16783                 skip "Need MDS version at least 2.10.55"
16784
16785         local dom=$DIR/$tdir/dom
16786
16787         mkdir -p $DIR/$tdir
16788
16789         $LFS setstripe -E 1024K -L mdt $dom
16790
16791         lctl set_param -n mdc.*.stats=clear
16792         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16793         cat $dom > /dev/null
16794         local reads=$(lctl get_param -n mdc.*.stats |
16795                         awk '/ost_read/ {print $2}')
16796         [ -z $reads ] || error "Unexpected $reads READ RPCs"
16797         ls $dom
16798         rm -f $dom
16799 }
16800 run_test 271a "DoM: data is cached for read after write"
16801
16802 test_271b() {
16803         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16804                 skip "Need MDS version at least 2.10.55"
16805
16806         local dom=$DIR/$tdir/dom
16807
16808         mkdir -p $DIR/$tdir
16809
16810         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16811
16812         lctl set_param -n mdc.*.stats=clear
16813         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16814         cancel_lru_locks mdc
16815         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
16816         # second stat to check size is cached on client
16817         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
16818         local gls=$(lctl get_param -n mdc.*.stats |
16819                         awk '/ldlm_glimpse/ {print $2}')
16820         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16821         rm -f $dom
16822 }
16823 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
16824
16825 test_271ba() {
16826         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16827                 skip "Need MDS version at least 2.10.55"
16828
16829         local dom=$DIR/$tdir/dom
16830
16831         mkdir -p $DIR/$tdir
16832
16833         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16834
16835         lctl set_param -n mdc.*.stats=clear
16836         lctl set_param -n osc.*.stats=clear
16837         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
16838         cancel_lru_locks mdc
16839         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16840         # second stat to check size is cached on client
16841         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16842         local gls=$(lctl get_param -n mdc.*.stats |
16843                         awk '/ldlm_glimpse/ {print $2}')
16844         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16845         local gls=$(lctl get_param -n osc.*.stats |
16846                         awk '/ldlm_glimpse/ {print $2}')
16847         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
16848         rm -f $dom
16849 }
16850 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
16851
16852 test_271c() {
16853         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16854                 skip "Need MDS version at least 2.10.55"
16855
16856         local dom=$DIR/$tdir/dom
16857
16858         mkdir -p $DIR/$tdir
16859
16860         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16861
16862         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
16863         local facet=mds$((mdtidx + 1))
16864
16865         cancel_lru_locks mdc
16866         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
16867         createmany -o $dom 1000
16868         lctl set_param -n mdc.*.stats=clear
16869         smalliomany -w $dom 1000 200
16870         lctl get_param -n mdc.*.stats
16871         local enq=$(lctl get_param -n mdc.*.stats |
16872                         awk '/ldlm_ibits_enqueue/ {print $2}')
16873         # Each file has 1 open, 1 IO enqueues, total 2000
16874         # but now we have also +1 getxattr for security.capability, total 3000
16875         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
16876         unlinkmany $dom 1000
16877
16878         cancel_lru_locks mdc
16879         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
16880         createmany -o $dom 1000
16881         lctl set_param -n mdc.*.stats=clear
16882         smalliomany -w $dom 1000 200
16883         lctl get_param -n mdc.*.stats
16884         local enq_2=$(lctl get_param -n mdc.*.stats |
16885                         awk '/ldlm_ibits_enqueue/ {print $2}')
16886         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
16887         # for OPEN and IO lock.
16888         [ $((enq - enq_2)) -ge 1000 ] ||
16889                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
16890         unlinkmany $dom 1000
16891         return 0
16892 }
16893 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
16894
16895 test_275() {
16896         remote_ost_nodsh && skip "remote OST with nodsh"
16897         [ $(lustre_version_code ost1) -lt $(version_code 2.10.57) ] &&
16898                 skip "Need OST version >= 2.10.57"
16899
16900         local file=$DIR/$tfile
16901         local oss
16902
16903         oss=$(comma_list $(osts_nodes))
16904
16905         dd if=/dev/urandom of=$file bs=1M count=2 ||
16906                 error "failed to create a file"
16907         cancel_lru_locks osc
16908
16909         #lock 1
16910         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
16911                 error "failed to read a file"
16912
16913 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
16914         $LCTL set_param fail_loc=0x8000031f
16915
16916         cancel_lru_locks osc &
16917         sleep 1
16918
16919 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
16920         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
16921         #IO takes another lock, but matches the PENDING one
16922         #and places it to the IO RPC
16923         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
16924                 error "failed to read a file with PENDING lock"
16925 }
16926 run_test 275 "Read on a canceled duplicate lock"
16927
16928 test_276() {
16929         remote_ost_nodsh && skip "remote OST with nodsh"
16930         local pid
16931
16932         do_facet ost1 "(while true; do \
16933                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
16934                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
16935         pid=$!
16936
16937         for LOOP in $(seq 20); do
16938                 stop ost1
16939                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
16940         done
16941         kill -9 $pid
16942         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
16943                 rm $TMP/sanity_276_pid"
16944 }
16945 run_test 276 "Race between mount and obd_statfs"
16946
16947 cleanup_test_300() {
16948         trap 0
16949         umask $SAVE_UMASK
16950 }
16951 test_striped_dir() {
16952         local mdt_index=$1
16953         local stripe_count
16954         local stripe_index
16955
16956         mkdir -p $DIR/$tdir
16957
16958         SAVE_UMASK=$(umask)
16959         trap cleanup_test_300 RETURN EXIT
16960
16961         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
16962                                                 $DIR/$tdir/striped_dir ||
16963                 error "set striped dir error"
16964
16965         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
16966         [ "$mode" = "755" ] || error "expect 755 got $mode"
16967
16968         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
16969                 error "getdirstripe failed"
16970         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
16971         if [ "$stripe_count" != "2" ]; then
16972                 error "1:stripe_count is $stripe_count, expect 2"
16973         fi
16974         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
16975         if [ "$stripe_count" != "2" ]; then
16976                 error "2:stripe_count is $stripe_count, expect 2"
16977         fi
16978
16979         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
16980         if [ "$stripe_index" != "$mdt_index" ]; then
16981                 error "stripe_index is $stripe_index, expect $mdt_index"
16982         fi
16983
16984         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16985                 error "nlink error after create striped dir"
16986
16987         mkdir $DIR/$tdir/striped_dir/a
16988         mkdir $DIR/$tdir/striped_dir/b
16989
16990         stat $DIR/$tdir/striped_dir/a ||
16991                 error "create dir under striped dir failed"
16992         stat $DIR/$tdir/striped_dir/b ||
16993                 error "create dir under striped dir failed"
16994
16995         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
16996                 error "nlink error after mkdir"
16997
16998         rmdir $DIR/$tdir/striped_dir/a
16999         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
17000                 error "nlink error after rmdir"
17001
17002         rmdir $DIR/$tdir/striped_dir/b
17003         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
17004                 error "nlink error after rmdir"
17005
17006         chattr +i $DIR/$tdir/striped_dir
17007         createmany -o $DIR/$tdir/striped_dir/f 10 &&
17008                 error "immutable flags not working under striped dir!"
17009         chattr -i $DIR/$tdir/striped_dir
17010
17011         rmdir $DIR/$tdir/striped_dir ||
17012                 error "rmdir striped dir error"
17013
17014         cleanup_test_300
17015
17016         true
17017 }
17018
17019 test_300a() {
17020         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17021                 skip "skipped for lustre < 2.7.0"
17022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17023         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17024
17025         test_striped_dir 0 || error "failed on striped dir on MDT0"
17026         test_striped_dir 1 || error "failed on striped dir on MDT0"
17027 }
17028 run_test 300a "basic striped dir sanity test"
17029
17030 test_300b() {
17031         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17032                 skip "skipped for lustre < 2.7.0"
17033         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17034         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17035
17036         local i
17037         local mtime1
17038         local mtime2
17039         local mtime3
17040
17041         test_mkdir $DIR/$tdir || error "mkdir fail"
17042         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17043                 error "set striped dir error"
17044         for i in {0..9}; do
17045                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
17046                 sleep 1
17047                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
17048                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
17049                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
17050                 sleep 1
17051                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
17052                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
17053                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
17054         done
17055         true
17056 }
17057 run_test 300b "check ctime/mtime for striped dir"
17058
17059 test_300c() {
17060         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17061                 skip "skipped for lustre < 2.7.0"
17062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17063         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17064
17065         local file_count
17066
17067         mkdir -p $DIR/$tdir
17068         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
17069                 error "set striped dir error"
17070
17071         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
17072                 error "chown striped dir failed"
17073
17074         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
17075                 error "create 5k files failed"
17076
17077         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
17078
17079         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
17080
17081         rm -rf $DIR/$tdir
17082 }
17083 run_test 300c "chown && check ls under striped directory"
17084
17085 test_300d() {
17086         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17087                 skip "skipped for lustre < 2.7.0"
17088         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17089         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17090
17091         local stripe_count
17092         local file
17093
17094         mkdir -p $DIR/$tdir
17095         $SETSTRIPE -c 2 $DIR/$tdir
17096
17097         #local striped directory
17098         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17099                 error "set striped dir error"
17100         createmany -o $DIR/$tdir/striped_dir/f 10 ||
17101                 error "create 10 files failed"
17102
17103         #remote striped directory
17104         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
17105                 error "set striped dir error"
17106         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
17107                 error "create 10 files failed"
17108
17109         for file in $(find $DIR/$tdir); do
17110                 stripe_count=$($LFS getstripe -c $file)
17111                 [ $stripe_count -eq 2 ] ||
17112                         error "wrong stripe $stripe_count for $file"
17113         done
17114
17115         rm -rf $DIR/$tdir
17116 }
17117 run_test 300d "check default stripe under striped directory"
17118
17119 test_300e() {
17120         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17121                 skip "Need MDS version at least 2.7.55"
17122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17123         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17124
17125         local stripe_count
17126         local file
17127
17128         mkdir -p $DIR/$tdir
17129
17130         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17131                 error "set striped dir error"
17132
17133         touch $DIR/$tdir/striped_dir/a
17134         touch $DIR/$tdir/striped_dir/b
17135         touch $DIR/$tdir/striped_dir/c
17136
17137         mkdir $DIR/$tdir/striped_dir/dir_a
17138         mkdir $DIR/$tdir/striped_dir/dir_b
17139         mkdir $DIR/$tdir/striped_dir/dir_c
17140
17141         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
17142                 error "set striped adir under striped dir error"
17143
17144         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
17145                 error "set striped bdir under striped dir error"
17146
17147         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
17148                 error "set striped cdir under striped dir error"
17149
17150         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
17151                 error "rename dir under striped dir fails"
17152
17153         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
17154                 error "rename dir under different stripes fails"
17155
17156         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
17157                 error "rename file under striped dir should succeed"
17158
17159         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
17160                 error "rename dir under striped dir should succeed"
17161
17162         rm -rf $DIR/$tdir
17163 }
17164 run_test 300e "check rename under striped directory"
17165
17166 test_300f() {
17167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17168         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17169         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17170                 skip "Need MDS version at least 2.7.55"
17171
17172         local stripe_count
17173         local file
17174
17175         rm -rf $DIR/$tdir
17176         mkdir -p $DIR/$tdir
17177
17178         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17179                 error "set striped dir error"
17180
17181         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
17182                 error "set striped dir error"
17183
17184         touch $DIR/$tdir/striped_dir/a
17185         mkdir $DIR/$tdir/striped_dir/dir_a
17186         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
17187                 error "create striped dir under striped dir fails"
17188
17189         touch $DIR/$tdir/striped_dir1/b
17190         mkdir $DIR/$tdir/striped_dir1/dir_b
17191         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
17192                 error "create striped dir under striped dir fails"
17193
17194         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
17195                 error "rename dir under different striped dir should fail"
17196
17197         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
17198                 error "rename striped dir under diff striped dir should fail"
17199
17200         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
17201                 error "rename file under diff striped dirs fails"
17202
17203         rm -rf $DIR/$tdir
17204 }
17205 run_test 300f "check rename cross striped directory"
17206
17207 test_300_check_default_striped_dir()
17208 {
17209         local dirname=$1
17210         local default_count=$2
17211         local default_index=$3
17212         local stripe_count
17213         local stripe_index
17214         local dir_stripe_index
17215         local dir
17216
17217         echo "checking $dirname $default_count $default_index"
17218         $LFS setdirstripe -D -c $default_count -i $default_index \
17219                                 -t all_char $DIR/$tdir/$dirname ||
17220                 error "set default stripe on striped dir error"
17221         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
17222         [ $stripe_count -eq $default_count ] ||
17223                 error "expect $default_count get $stripe_count for $dirname"
17224
17225         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
17226         [ $stripe_index -eq $default_index ] ||
17227                 error "expect $default_index get $stripe_index for $dirname"
17228
17229         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
17230                                                 error "create dirs failed"
17231
17232         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
17233         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
17234         for dir in $(find $DIR/$tdir/$dirname/*); do
17235                 stripe_count=$($LFS getdirstripe -c $dir)
17236                 [ $stripe_count -eq $default_count ] ||
17237                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
17238                 error "stripe count $default_count != $stripe_count for $dir"
17239
17240                 stripe_index=$($LFS getdirstripe -i $dir)
17241                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
17242                         error "$stripe_index != $default_index for $dir"
17243
17244                 #check default stripe
17245                 stripe_count=$($LFS getdirstripe -D -c $dir)
17246                 [ $stripe_count -eq $default_count ] ||
17247                 error "default count $default_count != $stripe_count for $dir"
17248
17249                 stripe_index=$($LFS getdirstripe -D -i $dir)
17250                 [ $stripe_index -eq $default_index ] ||
17251                 error "default index $default_index != $stripe_index for $dir"
17252         done
17253         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
17254 }
17255
17256 test_300g() {
17257         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17258         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17259                 skip "Need MDS version at least 2.7.55"
17260
17261         local dir
17262         local stripe_count
17263         local stripe_index
17264
17265         mkdir $DIR/$tdir
17266         mkdir $DIR/$tdir/normal_dir
17267
17268         #Checking when client cache stripe index
17269         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
17270         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
17271                 error "create striped_dir failed"
17272
17273         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
17274                 error "create dir0 fails"
17275         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
17276         [ $stripe_index -eq 0 ] ||
17277                 error "dir0 expect index 0 got $stripe_index"
17278
17279         mkdir $DIR/$tdir/striped_dir/dir1 ||
17280                 error "create dir1 fails"
17281         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
17282         [ $stripe_index -eq 1 ] ||
17283                 error "dir1 expect index 1 got $stripe_index"
17284
17285         #check default stripe count/stripe index
17286         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
17287         test_300_check_default_striped_dir normal_dir 1 0
17288         test_300_check_default_striped_dir normal_dir 2 1
17289         test_300_check_default_striped_dir normal_dir 2 -1
17290
17291         #delete default stripe information
17292         echo "delete default stripeEA"
17293         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
17294                 error "set default stripe on striped dir error"
17295
17296         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
17297         for dir in $(find $DIR/$tdir/normal_dir/*); do
17298                 stripe_count=$($LFS getdirstripe -c $dir)
17299                 [ $stripe_count -eq 0 ] ||
17300                         error "expect 1 get $stripe_count for $dir"
17301                 stripe_index=$($LFS getdirstripe -i $dir)
17302                 [ $stripe_index -eq 0 ] ||
17303                         error "expect 0 get $stripe_index for $dir"
17304         done
17305 }
17306 run_test 300g "check default striped directory for normal directory"
17307
17308 test_300h() {
17309         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17310         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17311                 skip "Need MDS version at least 2.7.55"
17312
17313         local dir
17314         local stripe_count
17315
17316         mkdir $DIR/$tdir
17317         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17318                 error "set striped dir error"
17319
17320         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
17321         test_300_check_default_striped_dir striped_dir 1 0
17322         test_300_check_default_striped_dir striped_dir 2 1
17323         test_300_check_default_striped_dir striped_dir 2 -1
17324
17325         #delete default stripe information
17326         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
17327                 error "set default stripe on striped dir error"
17328
17329         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
17330         for dir in $(find $DIR/$tdir/striped_dir/*); do
17331                 stripe_count=$($LFS getdirstripe -c $dir)
17332                 [ $stripe_count -eq 0 ] ||
17333                         error "expect 1 get $stripe_count for $dir"
17334         done
17335 }
17336 run_test 300h "check default striped directory for striped directory"
17337
17338 test_300i() {
17339         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17340         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17341         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17342                 skip "Need MDS version at least 2.7.55"
17343
17344         local stripe_count
17345         local file
17346
17347         mkdir $DIR/$tdir
17348
17349         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17350                 error "set striped dir error"
17351
17352         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
17353                 error "create files under striped dir failed"
17354
17355         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
17356                 error "set striped hashdir error"
17357
17358         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
17359                 error "create dir0 under hash dir failed"
17360         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
17361                 error "create dir1 under hash dir failed"
17362
17363         # unfortunately, we need to umount to clear dir layout cache for now
17364         # once we fully implement dir layout, we can drop this
17365         umount_client $MOUNT || error "umount failed"
17366         mount_client $MOUNT || error "mount failed"
17367
17368         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
17369         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
17370         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
17371
17372         #set the stripe to be unknown hash type
17373         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
17374         $LCTL set_param fail_loc=0x1901
17375         for ((i = 0; i < 10; i++)); do
17376                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
17377                         error "stat f-$i failed"
17378                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
17379         done
17380
17381         touch $DIR/$tdir/striped_dir/f0 &&
17382                 error "create under striped dir with unknown hash should fail"
17383
17384         $LCTL set_param fail_loc=0
17385
17386         umount_client $MOUNT || error "umount failed"
17387         mount_client $MOUNT || error "mount failed"
17388
17389         return 0
17390 }
17391 run_test 300i "client handle unknown hash type striped directory"
17392
17393 test_300j() {
17394         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17396         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17397                 skip "Need MDS version at least 2.7.55"
17398
17399         local stripe_count
17400         local file
17401
17402         mkdir $DIR/$tdir
17403
17404         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
17405         $LCTL set_param fail_loc=0x1702
17406         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17407                 error "set striped dir error"
17408
17409         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
17410                 error "create files under striped dir failed"
17411
17412         $LCTL set_param fail_loc=0
17413
17414         rm -rf $DIR/$tdir || error "unlink striped dir fails"
17415
17416         return 0
17417 }
17418 run_test 300j "test large update record"
17419
17420 test_300k() {
17421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17422         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17423         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17424                 skip "Need MDS version at least 2.7.55"
17425
17426         local stripe_count
17427         local file
17428
17429         mkdir $DIR/$tdir
17430
17431         #define OBD_FAIL_LARGE_STRIPE   0x1703
17432         $LCTL set_param fail_loc=0x1703
17433         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
17434                 error "set striped dir error"
17435         $LCTL set_param fail_loc=0
17436
17437         $LFS getdirstripe $DIR/$tdir/striped_dir ||
17438                 error "getstripeddir fails"
17439         rm -rf $DIR/$tdir/striped_dir ||
17440                 error "unlink striped dir fails"
17441
17442         return 0
17443 }
17444 run_test 300k "test large striped directory"
17445
17446 test_300l() {
17447         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17448         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17449         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17450                 skip "Need MDS version at least 2.7.55"
17451
17452         local stripe_index
17453
17454         test_mkdir -p $DIR/$tdir/striped_dir
17455         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
17456                         error "chown $RUNAS_ID failed"
17457         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
17458                 error "set default striped dir failed"
17459
17460         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
17461         $LCTL set_param fail_loc=0x80000158
17462         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
17463
17464         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
17465         [ $stripe_index -eq 1 ] ||
17466                 error "expect 1 get $stripe_index for $dir"
17467 }
17468 run_test 300l "non-root user to create dir under striped dir with stale layout"
17469
17470 test_300m() {
17471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17472         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
17473         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17474                 skip "Need MDS version at least 2.7.55"
17475
17476         mkdir -p $DIR/$tdir/striped_dir
17477         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
17478                 error "set default stripes dir error"
17479
17480         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
17481
17482         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
17483         [ $stripe_count -eq 0 ] ||
17484                         error "expect 0 get $stripe_count for a"
17485
17486         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
17487                 error "set default stripes dir error"
17488
17489         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
17490
17491         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
17492         [ $stripe_count -eq 0 ] ||
17493                         error "expect 0 get $stripe_count for b"
17494
17495         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
17496                 error "set default stripes dir error"
17497
17498         mkdir $DIR/$tdir/striped_dir/c &&
17499                 error "default stripe_index is invalid, mkdir c should fails"
17500
17501         rm -rf $DIR/$tdir || error "rmdir fails"
17502 }
17503 run_test 300m "setstriped directory on single MDT FS"
17504
17505 cleanup_300n() {
17506         local list=$(comma_list $(mdts_nodes))
17507
17508         trap 0
17509         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17510 }
17511
17512 test_300n() {
17513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17514         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17515         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17516                 skip "Need MDS version at least 2.7.55"
17517         remote_mds_nodsh && skip "remote MDS with nodsh"
17518
17519         local stripe_index
17520         local list=$(comma_list $(mdts_nodes))
17521
17522         trap cleanup_300n RETURN EXIT
17523         mkdir -p $DIR/$tdir
17524         chmod 777 $DIR/$tdir
17525         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
17526                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
17527                 error "create striped dir succeeds with gid=0"
17528
17529         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
17530         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
17531                 error "create striped dir fails with gid=-1"
17532
17533         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17534         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
17535                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
17536                 error "set default striped dir succeeds with gid=0"
17537
17538
17539         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
17540         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
17541                 error "set default striped dir fails with gid=-1"
17542
17543
17544         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17545         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
17546                                         error "create test_dir fails"
17547         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
17548                                         error "create test_dir1 fails"
17549         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
17550                                         error "create test_dir2 fails"
17551         cleanup_300n
17552 }
17553 run_test 300n "non-root user to create dir under striped dir with default EA"
17554
17555 test_300o() {
17556         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17557         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17558         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17559                 skip "Need MDS version at least 2.7.55"
17560
17561         local numfree1
17562         local numfree2
17563
17564         mkdir -p $DIR/$tdir
17565
17566         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
17567         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
17568         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
17569                 skip "not enough free inodes $numfree1 $numfree2"
17570         fi
17571
17572         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
17573         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
17574         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
17575                 skip "not enough free space $numfree1 $numfree2"
17576         fi
17577
17578         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
17579                 error "setdirstripe fails"
17580
17581         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
17582                 error "create dirs fails"
17583
17584         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
17585         ls $DIR/$tdir/striped_dir > /dev/null ||
17586                 error "ls striped dir fails"
17587         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
17588                 error "unlink big striped dir fails"
17589 }
17590 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
17591
17592 test_300p() {
17593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17594         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17595         remote_mds_nodsh && skip "remote MDS with nodsh"
17596
17597         mkdir -p $DIR/$tdir
17598
17599         #define OBD_FAIL_OUT_ENOSPC     0x1704
17600         do_facet mds2 lctl set_param fail_loc=0x80001704
17601         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
17602                  && error "create striped directory should fail"
17603
17604         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
17605
17606         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
17607         true
17608 }
17609 run_test 300p "create striped directory without space"
17610
17611 test_300q() {
17612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17613         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17614
17615         local fd=$(free_fd)
17616         local cmd="exec $fd<$tdir"
17617         cd $DIR
17618         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
17619         eval $cmd
17620         cmd="exec $fd<&-"
17621         trap "eval $cmd" EXIT
17622         cd $tdir || error "cd $tdir fails"
17623         rmdir  ../$tdir || error "rmdir $tdir fails"
17624         mkdir local_dir && error "create dir succeeds"
17625         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
17626         eval $cmd
17627         return 0
17628 }
17629 run_test 300q "create remote directory under orphan directory"
17630
17631 prepare_remote_file() {
17632         mkdir $DIR/$tdir/src_dir ||
17633                 error "create remote source failed"
17634
17635         cp /etc/hosts $DIR/$tdir/src_dir/a ||
17636                  error "cp to remote source failed"
17637         touch $DIR/$tdir/src_dir/a
17638
17639         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
17640                 error "create remote target dir failed"
17641
17642         touch $DIR/$tdir/tgt_dir/b
17643
17644         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
17645                 error "rename dir cross MDT failed!"
17646
17647         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
17648                 error "src_child still exists after rename"
17649
17650         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
17651                 error "missing file(a) after rename"
17652
17653         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
17654                 error "diff after rename"
17655 }
17656
17657 test_310a() {
17658         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
17659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17660
17661         local remote_file=$DIR/$tdir/tgt_dir/b
17662
17663         mkdir -p $DIR/$tdir
17664
17665         prepare_remote_file || error "prepare remote file failed"
17666
17667         #open-unlink file
17668         $OPENUNLINK $remote_file $remote_file ||
17669                 error "openunlink $remote_file failed"
17670         $CHECKSTAT -a $remote_file || error "$remote_file exists"
17671 }
17672 run_test 310a "open unlink remote file"
17673
17674 test_310b() {
17675         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
17676         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17677
17678         local remote_file=$DIR/$tdir/tgt_dir/b
17679
17680         mkdir -p $DIR/$tdir
17681
17682         prepare_remote_file || error "prepare remote file failed"
17683
17684         ln $remote_file $DIR/$tfile || error "link failed for remote file"
17685         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
17686         $CHECKSTAT -t file $remote_file || error "check file failed"
17687 }
17688 run_test 310b "unlink remote file with multiple links while open"
17689
17690 test_310c() {
17691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17692         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
17693
17694         local remote_file=$DIR/$tdir/tgt_dir/b
17695
17696         mkdir -p $DIR/$tdir
17697
17698         prepare_remote_file || error "prepare remote file failed"
17699
17700         ln $remote_file $DIR/$tfile || error "link failed for remote file"
17701         multiop_bg_pause $remote_file O_uc ||
17702                         error "mulitop failed for remote file"
17703         MULTIPID=$!
17704         $MULTIOP $DIR/$tfile Ouc
17705         kill -USR1 $MULTIPID
17706         wait $MULTIPID
17707 }
17708 run_test 310c "open-unlink remote file with multiple links"
17709
17710 #LU-4825
17711 test_311() {
17712         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17713         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
17714         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
17715                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
17716         remote_mds_nodsh && skip "remote MDS with nodsh"
17717
17718         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
17719
17720         mkdir -p $DIR/$tdir
17721         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
17722         createmany -o $DIR/$tdir/$tfile. 1000
17723
17724         # statfs data is not real time, let's just calculate it
17725         old_iused=$((old_iused + 1000))
17726
17727         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
17728                         osp.*OST0000*MDT0000.create_count")
17729         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
17730                                 osp.*OST0000*MDT0000.max_create_count")
17731         for idx in $(seq $MDSCOUNT); do
17732                 do_facet mds$idx "lctl set_param -n \
17733                         osp.*OST0000*MDT000?.max_create_count=0"
17734         done
17735
17736         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
17737         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
17738         [ $index -ne 0 ] || error "$tfile stripe index is 0"
17739
17740         unlinkmany $DIR/$tdir/$tfile. 1000
17741
17742         for idx in $(seq $MDSCOUNT); do
17743                 do_facet mds$idx "lctl set_param -n \
17744                         osp.*OST0000*MDT000?.max_create_count=$max_count"
17745                 do_facet mds$idx "lctl set_param -n \
17746                         osp.*OST0000*MDT000?.create_count=$count"
17747         done
17748
17749         local new_iused
17750         for i in $(seq 120); do
17751                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
17752                 # system may be too busy to destroy all objs in time, use
17753                 # a somewhat small value to not fail autotest
17754                 [ $((old_iused - new_iused)) -gt 400 ] && break
17755                 sleep 1
17756         done
17757
17758         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
17759         [ $((old_iused - new_iused)) -gt 400 ] ||
17760                 error "objs not destroyed after unlink"
17761 }
17762 run_test 311 "disable OSP precreate, and unlink should destroy objs"
17763
17764 zfs_oid_to_objid()
17765 {
17766         local ost=$1
17767         local objid=$2
17768
17769         local vdevdir=$(dirname $(facet_vdevice $ost))
17770         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
17771         local zfs_zapid=$(do_facet $ost $cmd |
17772                           grep -w "/O/0/d$((objid%32))" -C 5 |
17773                           awk '/Object/{getline; print $1}')
17774         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
17775                           awk "/$objid = /"'{printf $3}')
17776
17777         echo $zfs_objid
17778 }
17779
17780 zfs_object_blksz() {
17781         local ost=$1
17782         local objid=$2
17783
17784         local vdevdir=$(dirname $(facet_vdevice $ost))
17785         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
17786         local blksz=$(do_facet $ost $cmd $objid |
17787                       awk '/dblk/{getline; printf $4}')
17788
17789         case "${blksz: -1}" in
17790                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
17791                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
17792                 *) ;;
17793         esac
17794
17795         echo $blksz
17796 }
17797
17798 test_312() { # LU-4856
17799         remote_ost_nodsh && skip "remote OST with nodsh"
17800         [ $(facet_fstype ost1) = "zfs" ] ||
17801                 skip_env "the test only applies to zfs"
17802
17803         local max_blksz=$(do_facet ost1 \
17804                           $ZFS get -p recordsize $(facet_device ost1) |
17805                           awk '!/VALUE/{print $3}')
17806         local min_blksz=$(getconf PAGE_SIZE)
17807
17808         # to make life a little bit easier
17809         $LFS mkdir -c 1 -i 0 $DIR/$tdir
17810         $LFS setstripe -c 1 -i 0 $DIR/$tdir
17811
17812         local tf=$DIR/$tdir/$tfile
17813         touch $tf
17814         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17815
17816         # Get ZFS object id
17817         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17818
17819         # block size change by sequential over write
17820         local blksz
17821         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
17822                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
17823
17824                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17825                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
17826         done
17827         rm -f $tf
17828
17829         # block size change by sequential append write
17830         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
17831         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17832         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17833
17834         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
17835                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
17836                         oflag=sync conv=notrunc
17837
17838                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17839                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
17840                         error "blksz error, actual $blksz, "    \
17841                                 "expected: 2 * $count * $min_blksz"
17842         done
17843         rm -f $tf
17844
17845         # random write
17846         touch $tf
17847         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17848         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17849
17850         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
17851         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17852         [ $blksz -eq $min_blksz ] ||
17853                 error "blksz error: $blksz, expected: $min_blksz"
17854
17855         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
17856         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17857         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
17858
17859         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
17860         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17861         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
17862 }
17863 run_test 312 "make sure ZFS adjusts its block size by write pattern"
17864
17865 test_313() {
17866         remote_ost_nodsh && skip "remote OST with nodsh"
17867
17868         local file=$DIR/$tfile
17869         rm -f $file
17870         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
17871
17872         # define OBD_FAIL_TGT_RCVD_EIO           0x720
17873         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17874         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
17875                 error "write should failed"
17876         do_facet ost1 "$LCTL set_param fail_loc=0"
17877         rm -f $file
17878 }
17879 run_test 313 "io should fail after last_rcvd update fail"
17880
17881 test_314() {
17882         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
17883
17884         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
17885         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17886         rm -f $DIR/$tfile
17887         wait_delete_completed
17888         do_facet ost1 "$LCTL set_param fail_loc=0"
17889 }
17890 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
17891
17892 test_315() { # LU-618
17893         local file=$DIR/$tfile
17894         rm -f $file
17895
17896         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
17897         $MULTIOP $file oO_RDONLY:r4096000_c &
17898         PID=$!
17899
17900         sleep 2
17901
17902         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
17903         kill -USR1 $PID
17904
17905         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
17906         rm -f $file
17907 }
17908 run_test 315 "read should be accounted"
17909
17910 test_316() {
17911         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17912         large_xattr_enabled || skip_env "large_xattr disabled"
17913
17914         rm -rf $DIR/$tdir/d
17915         mkdir -p $DIR/$tdir/d
17916         chown nobody $DIR/$tdir/d
17917         touch $DIR/$tdir/d/file
17918
17919         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
17920 }
17921 run_test 316 "lfs mv"
17922
17923 test_fake_rw() {
17924         local read_write=$1
17925         if [ "$read_write" = "write" ]; then
17926                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
17927         elif [ "$read_write" = "read" ]; then
17928                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
17929         else
17930                 error "argument error"
17931         fi
17932
17933         # turn off debug for performance testing
17934         local saved_debug=$($LCTL get_param -n debug)
17935         $LCTL set_param debug=0
17936
17937         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
17938
17939         # get ost1 size - lustre-OST0000
17940         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
17941         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
17942         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
17943
17944         if [ "$read_write" = "read" ]; then
17945                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
17946         fi
17947
17948         local start_time=$(date +%s.%N)
17949         $dd_cmd bs=1M count=$blocks oflag=sync ||
17950                 error "real dd $read_write error"
17951         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
17952
17953         if [ "$read_write" = "write" ]; then
17954                 rm -f $DIR/$tfile
17955         fi
17956
17957         # define OBD_FAIL_OST_FAKE_RW           0x238
17958         do_facet ost1 $LCTL set_param fail_loc=0x238
17959
17960         local start_time=$(date +%s.%N)
17961         $dd_cmd bs=1M count=$blocks oflag=sync ||
17962                 error "fake dd $read_write error"
17963         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
17964
17965         if [ "$read_write" = "write" ]; then
17966                 # verify file size
17967                 cancel_lru_locks osc
17968                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
17969                         error "$tfile size not $blocks MB"
17970         fi
17971         do_facet ost1 $LCTL set_param fail_loc=0
17972
17973         echo "fake $read_write $duration_fake vs. normal $read_write" \
17974                 "$duration in seconds"
17975         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
17976                 error_not_in_vm "fake write is slower"
17977
17978         $LCTL set_param -n debug="$saved_debug"
17979         rm -f $DIR/$tfile
17980 }
17981 test_399a() { # LU-7655 for OST fake write
17982         remote_ost_nodsh && skip "remote OST with nodsh"
17983
17984         test_fake_rw write
17985 }
17986 run_test 399a "fake write should not be slower than normal write"
17987
17988 test_399b() { # LU-8726 for OST fake read
17989         remote_ost_nodsh && skip "remote OST with nodsh"
17990         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
17991                 skip_env "ldiskfs only test"
17992         fi
17993
17994         test_fake_rw read
17995 }
17996 run_test 399b "fake read should not be slower than normal read"
17997
17998 test_400a() { # LU-1606, was conf-sanity test_74
17999         if ! which $CC > /dev/null 2>&1; then
18000                 skip_env "$CC is not installed"
18001         fi
18002
18003         local extra_flags=''
18004         local out=$TMP/$tfile
18005         local prefix=/usr/include/lustre
18006         local prog
18007
18008         if ! [[ -d $prefix ]]; then
18009                 # Assume we're running in tree and fixup the include path.
18010                 extra_flags+=" -I$LUSTRE/include"
18011                 extra_flags+=" -L$LUSTRE/utils"
18012         fi
18013
18014         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
18015                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
18016                         error "client api broken"
18017         done
18018         rm -f $out
18019 }
18020 run_test 400a "Lustre client api program can compile and link"
18021
18022 test_400b() { # LU-1606, LU-5011
18023         local header
18024         local out=$TMP/$tfile
18025         local prefix=/usr/include/linux/lustre
18026
18027         # We use a hard coded prefix so that this test will not fail
18028         # when run in tree. There are headers in lustre/include/lustre/
18029         # that are not packaged (like lustre_idl.h) and have more
18030         # complicated include dependencies (like config.h and lnet/types.h).
18031         # Since this test about correct packaging we just skip them when
18032         # they don't exist (see below) rather than try to fixup cppflags.
18033
18034         if ! which $CC > /dev/null 2>&1; then
18035                 skip_env "$CC is not installed"
18036         fi
18037
18038         for header in $prefix/*.h; do
18039                 if ! [[ -f "$header" ]]; then
18040                         continue
18041                 fi
18042
18043                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
18044                         continue # lustre_ioctl.h is internal header
18045                 fi
18046
18047                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
18048                         error "cannot compile '$header'"
18049         done
18050         rm -f $out
18051 }
18052 run_test 400b "packaged headers can be compiled"
18053
18054 test_401a() { #LU-7437
18055         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
18056         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
18057
18058         #count the number of parameters by "list_param -R"
18059         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
18060         #count the number of parameters by listing proc files
18061         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
18062         echo "proc_dirs='$proc_dirs'"
18063         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
18064         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
18065                       sort -u | wc -l)
18066
18067         [ $params -eq $procs ] ||
18068                 error "found $params parameters vs. $procs proc files"
18069
18070         # test the list_param -D option only returns directories
18071         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
18072         #count the number of parameters by listing proc directories
18073         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
18074                 sort -u | wc -l)
18075
18076         [ $params -eq $procs ] ||
18077                 error "found $params parameters vs. $procs proc files"
18078 }
18079 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
18080
18081 test_401b() {
18082         local save=$($LCTL get_param -n jobid_var)
18083         local tmp=testing
18084
18085         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
18086                 error "no error returned when setting bad parameters"
18087
18088         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
18089         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
18090
18091         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
18092         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
18093         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
18094 }
18095 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
18096
18097 test_401c() {
18098         local jobid_var_old=$($LCTL get_param -n jobid_var)
18099         local jobid_var_new
18100
18101         $LCTL set_param jobid_var= &&
18102                 error "no error returned for 'set_param a='"
18103
18104         jobid_var_new=$($LCTL get_param -n jobid_var)
18105         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18106                 error "jobid_var was changed by setting without value"
18107
18108         $LCTL set_param jobid_var &&
18109                 error "no error returned for 'set_param a'"
18110
18111         jobid_var_new=$($LCTL get_param -n jobid_var)
18112         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18113                 error "jobid_var was changed by setting without value"
18114 }
18115 run_test 401c "Verify 'lctl set_param' without value fails in either format."
18116
18117 test_401d() {
18118         local jobid_var_old=$($LCTL get_param -n jobid_var)
18119         local jobid_var_new
18120         local new_value="foo=bar"
18121
18122         $LCTL set_param jobid_var=$new_value ||
18123                 error "'set_param a=b' did not accept a value containing '='"
18124
18125         jobid_var_new=$($LCTL get_param -n jobid_var)
18126         [[ "$jobid_var_new" == "$new_value" ]] ||
18127                 error "'set_param a=b' failed on a value containing '='"
18128
18129         # Reset the jobid_var to test the other format
18130         $LCTL set_param jobid_var=$jobid_var_old
18131         jobid_var_new=$($LCTL get_param -n jobid_var)
18132         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
18133                 error "failed to reset jobid_var"
18134
18135         $LCTL set_param jobid_var $new_value ||
18136                 error "'set_param a b' did not accept a value containing '='"
18137
18138         jobid_var_new=$($LCTL get_param -n jobid_var)
18139         [[ "$jobid_var_new" == "$new_value" ]] ||
18140                 error "'set_param a b' failed on a value containing '='"
18141
18142         $LCTL set_param jobid_var $jobid_var_old
18143         jobid_var_new=$($LCTL get_param -n jobid_var)
18144         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
18145                 error "failed to reset jobid_var"
18146 }
18147 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
18148
18149 test_402() {
18150         local server_version=$(lustre_version_code $SINGLEMDS)
18151         [[ $server_version -ge $(version_code 2.7.66) ]] ||
18152         [[ $server_version -ge $(version_code 2.7.18.4) &&
18153                 $server_version -lt $(version_code 2.7.50) ]] ||
18154         [[ $server_version -ge $(version_code 2.7.2) &&
18155                 $server_version -lt $(version_code 2.7.11) ]] ||
18156                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
18157         remote_mds_nodsh && skip "remote MDS with nodsh"
18158
18159         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
18160 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
18161         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
18162         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
18163                 echo "Touch failed - OK"
18164 }
18165 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
18166
18167 test_403() {
18168         local file1=$DIR/$tfile.1
18169         local file2=$DIR/$tfile.2
18170         local tfile=$TMP/$tfile
18171
18172         rm -f $file1 $file2 $tfile
18173
18174         touch $file1
18175         ln $file1 $file2
18176
18177         # 30 sec OBD_TIMEOUT in ll_getattr()
18178         # right before populating st_nlink
18179         $LCTL set_param fail_loc=0x80001409
18180         stat -c %h $file1 > $tfile &
18181
18182         # create an alias, drop all locks and reclaim the dentry
18183         < $file2
18184         cancel_lru_locks mdc
18185         cancel_lru_locks osc
18186         sysctl -w vm.drop_caches=2
18187
18188         wait
18189
18190         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
18191
18192         rm -f $tfile $file1 $file2
18193 }
18194 run_test 403 "i_nlink should not drop to zero due to aliasing"
18195
18196 test_404() { # LU-6601
18197         local server_version=$(lustre_version_code $SINGLEMDS)
18198         [[ $server_version -ge $(version_code 2.8.53) ]] ||
18199                 skip "Need server version newer than 2.8.52"
18200         remote_mds_nodsh && skip "remote MDS with nodsh"
18201
18202         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
18203                 awk '/osp .*-osc-MDT/ { print $4}')
18204
18205         local osp
18206         for osp in $mosps; do
18207                 echo "Deactivate: " $osp
18208                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
18209                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
18210                         awk -vp=$osp '$4 == p { print $2 }')
18211                 [ $stat = IN ] || {
18212                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
18213                         error "deactivate error"
18214                 }
18215                 echo "Activate: " $osp
18216                 do_facet $SINGLEMDS $LCTL --device %$osp activate
18217                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
18218                         awk -vp=$osp '$4 == p { print $2 }')
18219                 [ $stat = UP ] || {
18220                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
18221                         error "activate error"
18222                 }
18223         done
18224 }
18225 run_test 404 "validate manual {de}activated works properly for OSPs"
18226
18227 test_405() {
18228         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
18229         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
18230                 skip "Layout swap lock is not supported"
18231         check_swap_layouts_support
18232
18233         test_mkdir $DIR/$tdir
18234         swap_lock_test -d $DIR/$tdir ||
18235                 error "One layout swap locked test failed"
18236 }
18237 run_test 405 "Various layout swap lock tests"
18238
18239 test_406() {
18240         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18241         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
18242         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
18243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18244         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
18245                 skip "Need MDS version at least 2.8.50"
18246
18247         local def_stripe_size=$($LFS getstripe -S $MOUNT)
18248         local test_pool=$TESTNAME
18249
18250         if ! combined_mgs_mds ; then
18251                 mount_mgs_client
18252         fi
18253         pool_add $test_pool || error "pool_add failed"
18254         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
18255                 error "pool_add_targets failed"
18256
18257         save_layout_restore_at_exit $MOUNT
18258
18259         # parent set default stripe count only, child will stripe from both
18260         # parent and fs default
18261         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
18262                 error "setstripe $MOUNT failed"
18263         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
18264         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
18265         for i in $(seq 10); do
18266                 local f=$DIR/$tdir/$tfile.$i
18267                 touch $f || error "touch failed"
18268                 local count=$($LFS getstripe -c $f)
18269                 [ $count -eq $OSTCOUNT ] ||
18270                         error "$f stripe count $count != $OSTCOUNT"
18271                 local offset=$($LFS getstripe -i $f)
18272                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
18273                 local size=$($LFS getstripe -S $f)
18274                 [ $size -eq $((def_stripe_size * 2)) ] ||
18275                         error "$f stripe size $size != $((def_stripe_size * 2))"
18276                 local pool=$($LFS getstripe -p $f)
18277                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
18278         done
18279
18280         # change fs default striping, delete parent default striping, now child
18281         # will stripe from new fs default striping only
18282         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
18283                 error "change $MOUNT default stripe failed"
18284         $LFS setstripe -c 0 $DIR/$tdir ||
18285                 error "delete $tdir default stripe failed"
18286         for i in $(seq 11 20); do
18287                 local f=$DIR/$tdir/$tfile.$i
18288                 touch $f || error "touch $f failed"
18289                 local count=$($LFS getstripe -c $f)
18290                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
18291                 local offset=$($LFS getstripe -i $f)
18292                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
18293                 local size=$($LFS getstripe -S $f)
18294                 [ $size -eq $def_stripe_size ] ||
18295                         error "$f stripe size $size != $def_stripe_size"
18296                 local pool=$($LFS getstripe -p $f)
18297                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
18298         done
18299
18300         unlinkmany $DIR/$tdir/$tfile. 1 20
18301
18302         local f=$DIR/$tdir/$tfile
18303         pool_remove_all_targets $test_pool $f
18304         pool_remove $test_pool $f
18305
18306         if ! combined_mgs_mds ; then
18307                 umount_mgs_client
18308         fi
18309 }
18310 run_test 406 "DNE support fs default striping"
18311
18312 test_407() {
18313         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18314         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
18315                 skip "Need MDS version at least 2.8.55"
18316         remote_mds_nodsh && skip "remote MDS with nodsh"
18317
18318         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
18319                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
18320         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
18321                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
18322         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
18323
18324         #define OBD_FAIL_DT_TXN_STOP    0x2019
18325         for idx in $(seq $MDSCOUNT); do
18326                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
18327         done
18328         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
18329         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
18330                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
18331         true
18332 }
18333 run_test 407 "transaction fail should cause operation fail"
18334
18335 test_408() {
18336         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
18337
18338         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
18339         lctl set_param fail_loc=0x8000040a
18340         # let ll_prepare_partial_page() fail
18341         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
18342
18343         rm -f $DIR/$tfile
18344
18345         # create at least 100 unused inodes so that
18346         # shrink_icache_memory(0) should not return 0
18347         touch $DIR/$tfile-{0..100}
18348         rm -f $DIR/$tfile-{0..100}
18349         sync
18350
18351         echo 2 > /proc/sys/vm/drop_caches
18352 }
18353 run_test 408 "drop_caches should not hang due to page leaks"
18354
18355 test_409()
18356 {
18357         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18358         check_mount_and_prep
18359
18360         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
18361         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
18362         touch $DIR/$tdir/guard || error "(2) Fail to create"
18363
18364         local PREFIX=$(str_repeat 'A' 128)
18365         echo "Create 1K hard links start at $(date)"
18366         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
18367                 error "(3) Fail to hard link"
18368
18369         echo "Links count should be right although linkEA overflow"
18370         stat $DIR/$tdir/guard || error "(4) Fail to stat"
18371         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
18372         [ $linkcount -eq 1001 ] ||
18373                 error "(5) Unexpected hard links count: $linkcount"
18374
18375         echo "List all links start at $(date)"
18376         ls -l $DIR/$tdir/foo > /dev/null ||
18377                 error "(6) Fail to list $DIR/$tdir/foo"
18378
18379         echo "Unlink hard links start at $(date)"
18380         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
18381                 error "(7) Fail to unlink"
18382 }
18383 run_test 409 "Large amount of cross-MDTs hard links on the same file"
18384
18385 test_410()
18386 {
18387         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
18388                 skip "Need client version at least 2.9.59"
18389
18390         # Create a file, and stat it from the kernel
18391         local testfile=$DIR/$tfile
18392         touch $testfile
18393
18394         local run_id=$RANDOM
18395         local my_ino=$(stat --format "%i" $testfile)
18396
18397         # Try to insert the module. This will always fail as the
18398         # module is designed to not be inserted.
18399         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
18400             &> /dev/null
18401
18402         # Anything but success is a test failure
18403         dmesg | grep -q \
18404             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
18405             error "no inode match"
18406 }
18407 run_test 410 "Test inode number returned from kernel thread"
18408
18409 cleanup_test411_cgroup() {
18410         trap 0
18411         rmdir "$1"
18412 }
18413
18414 test_411() {
18415         local cg_basedir=/sys/fs/cgroup/memory
18416         # LU-9966
18417         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
18418                 skip "no setup for cgroup"
18419
18420         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
18421                 error "test file creation failed"
18422         cancel_lru_locks osc
18423
18424         # Create a very small memory cgroup to force a slab allocation error
18425         local cgdir=$cg_basedir/osc_slab_alloc
18426         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
18427         trap "cleanup_test411_cgroup $cgdir" EXIT
18428         echo 2M > $cgdir/memory.kmem.limit_in_bytes
18429         echo 1M > $cgdir/memory.limit_in_bytes
18430
18431         # Should not LBUG, just be killed by oom-killer
18432         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
18433                 error "fail to trigger a memory allocation error"
18434
18435         cleanup_test411_cgroup $cgdir
18436
18437         return 0
18438 }
18439 run_test 411 "Slab allocation error with cgroup does not LBUG"
18440
18441 test_412() {
18442         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18443         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
18444                 skip "Need server version at least 2.10.55"
18445         fi
18446
18447         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
18448                 error "mkdir failed"
18449         $LFS getdirstripe $DIR/$tdir
18450         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
18451         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
18452                 error "expect $((MDSCOUT - 1)) get $stripe_index"
18453         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
18454         [ $stripe_count -eq 2 ] ||
18455                 error "expect 2 get $stripe_count"
18456 }
18457 run_test 412 "mkdir on specific MDTs"
18458
18459 test_413() {
18460         [ $MDSCOUNT -lt 2 ] &&
18461                 skip "We need at least 2 MDTs for this test"
18462
18463         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
18464                 skip "Need server version at least 2.10.55"
18465         fi
18466
18467         mkdir $DIR/$tdir || error "mkdir failed"
18468
18469         # find MDT that is the most full
18470         local max=$($LFS df | grep MDT |
18471                 awk 'BEGIN { a=0 }
18472                         { sub("%", "", $5)
18473                           if (0+$5 >= a)
18474                           {
18475                                 a = $5
18476                                 b = $6
18477                           }
18478                         }
18479                      END { split(b, c, ":")
18480                            sub("]", "", c[2])
18481                            print c[2]
18482                          }')
18483
18484         for i in $(seq $((MDSCOUNT - 1))); do
18485                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
18486                         error "mkdir d$i failed"
18487                 $LFS getdirstripe $DIR/$tdir/d$i
18488                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
18489                 [ $stripe_index -ne $max ] ||
18490                         error "don't expect $max"
18491         done
18492 }
18493 run_test 413 "mkdir on less full MDTs"
18494
18495 test_414() {
18496 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
18497         $LCTL set_param fail_loc=0x80000521
18498         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
18499         rm -f $DIR/$tfile
18500 }
18501 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
18502
18503 prep_801() {
18504         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
18505         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
18506                 skip "Need server version at least 2.9.55"
18507
18508         start_full_debug_logging
18509 }
18510
18511 post_801() {
18512         stop_full_debug_logging
18513 }
18514
18515 barrier_stat() {
18516         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
18517                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
18518                            awk '/The barrier for/ { print $7 }')
18519                 echo $st
18520         else
18521                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
18522                 echo \'$st\'
18523         fi
18524 }
18525
18526 barrier_expired() {
18527         local expired
18528
18529         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
18530                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
18531                           awk '/will be expired/ { print $7 }')
18532         else
18533                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
18534         fi
18535
18536         echo $expired
18537 }
18538
18539 test_801a() {
18540         prep_801
18541
18542         echo "Start barrier_freeze at: $(date)"
18543         #define OBD_FAIL_BARRIER_DELAY          0x2202
18544         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
18545         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
18546
18547         sleep 2
18548         local b_status=$(barrier_stat)
18549         echo "Got barrier status at: $(date)"
18550         [ "$b_status" = "'freezing_p1'" ] ||
18551                 error "(1) unexpected barrier status $b_status"
18552
18553         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
18554         wait
18555         b_status=$(barrier_stat)
18556         [ "$b_status" = "'frozen'" ] ||
18557                 error "(2) unexpected barrier status $b_status"
18558
18559         local expired=$(barrier_expired)
18560         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
18561         sleep $((expired + 3))
18562
18563         b_status=$(barrier_stat)
18564         [ "$b_status" = "'expired'" ] ||
18565                 error "(3) unexpected barrier status $b_status"
18566
18567         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
18568                 error "(4) fail to freeze barrier"
18569
18570         b_status=$(barrier_stat)
18571         [ "$b_status" = "'frozen'" ] ||
18572                 error "(5) unexpected barrier status $b_status"
18573
18574         echo "Start barrier_thaw at: $(date)"
18575         #define OBD_FAIL_BARRIER_DELAY          0x2202
18576         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
18577         do_facet mgs $LCTL barrier_thaw $FSNAME &
18578
18579         sleep 2
18580         b_status=$(barrier_stat)
18581         echo "Got barrier status at: $(date)"
18582         [ "$b_status" = "'thawing'" ] ||
18583                 error "(6) unexpected barrier status $b_status"
18584
18585         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
18586         wait
18587         b_status=$(barrier_stat)
18588         [ "$b_status" = "'thawed'" ] ||
18589                 error "(7) unexpected barrier status $b_status"
18590
18591         #define OBD_FAIL_BARRIER_FAILURE        0x2203
18592         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
18593         do_facet mgs $LCTL barrier_freeze $FSNAME
18594
18595         b_status=$(barrier_stat)
18596         [ "$b_status" = "'failed'" ] ||
18597                 error "(8) unexpected barrier status $b_status"
18598
18599         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18600         do_facet mgs $LCTL barrier_thaw $FSNAME
18601
18602         post_801
18603 }
18604 run_test 801a "write barrier user interfaces and stat machine"
18605
18606 test_801b() {
18607         prep_801
18608
18609         mkdir $DIR/$tdir || error "(1) fail to mkdir"
18610         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
18611         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
18612         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
18613         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
18614
18615         cancel_lru_locks mdc
18616
18617         # 180 seconds should be long enough
18618         do_facet mgs $LCTL barrier_freeze $FSNAME 180
18619
18620         local b_status=$(barrier_stat)
18621         [ "$b_status" = "'frozen'" ] ||
18622                 error "(6) unexpected barrier status $b_status"
18623
18624         mkdir $DIR/$tdir/d0/d10 &
18625         mkdir_pid=$!
18626
18627         touch $DIR/$tdir/d1/f13 &
18628         touch_pid=$!
18629
18630         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
18631         ln_pid=$!
18632
18633         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
18634         mv_pid=$!
18635
18636         rm -f $DIR/$tdir/d4/f12 &
18637         rm_pid=$!
18638
18639         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
18640
18641         # To guarantee taht the 'stat' is not blocked
18642         b_status=$(barrier_stat)
18643         [ "$b_status" = "'frozen'" ] ||
18644                 error "(8) unexpected barrier status $b_status"
18645
18646         # let above commands to run at background
18647         sleep 5
18648
18649         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
18650         ps -p $touch_pid || error "(10) touch should be blocked"
18651         ps -p $ln_pid || error "(11) link should be blocked"
18652         ps -p $mv_pid || error "(12) rename should be blocked"
18653         ps -p $rm_pid || error "(13) unlink should be blocked"
18654
18655         b_status=$(barrier_stat)
18656         [ "$b_status" = "'frozen'" ] ||
18657                 error "(14) unexpected barrier status $b_status"
18658
18659         do_facet mgs $LCTL barrier_thaw $FSNAME
18660         b_status=$(barrier_stat)
18661         [ "$b_status" = "'thawed'" ] ||
18662                 error "(15) unexpected barrier status $b_status"
18663
18664         wait $mkdir_pid || error "(16) mkdir should succeed"
18665         wait $touch_pid || error "(17) touch should succeed"
18666         wait $ln_pid || error "(18) link should succeed"
18667         wait $mv_pid || error "(19) rename should succeed"
18668         wait $rm_pid || error "(20) unlink should succeed"
18669
18670         post_801
18671 }
18672 run_test 801b "modification will be blocked by write barrier"
18673
18674 test_801c() {
18675         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18676
18677         prep_801
18678
18679         stop mds2 || error "(1) Fail to stop mds2"
18680
18681         do_facet mgs $LCTL barrier_freeze $FSNAME 30
18682
18683         local b_status=$(barrier_stat)
18684         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
18685                 do_facet mgs $LCTL barrier_thaw $FSNAME
18686                 error "(2) unexpected barrier status $b_status"
18687         }
18688
18689         do_facet mgs $LCTL barrier_rescan $FSNAME ||
18690                 error "(3) Fail to rescan barrier bitmap"
18691
18692         do_facet mgs $LCTL barrier_freeze $FSNAME 10
18693
18694         b_status=$(barrier_stat)
18695         [ "$b_status" = "'frozen'" ] ||
18696                 error "(4) unexpected barrier status $b_status"
18697
18698         do_facet mgs $LCTL barrier_thaw $FSNAME
18699         b_status=$(barrier_stat)
18700         [ "$b_status" = "'thawed'" ] ||
18701                 error "(5) unexpected barrier status $b_status"
18702
18703         local devname=$(mdsdevname 2)
18704
18705         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
18706
18707         do_facet mgs $LCTL barrier_rescan $FSNAME ||
18708                 error "(7) Fail to rescan barrier bitmap"
18709
18710         post_801
18711 }
18712 run_test 801c "rescan barrier bitmap"
18713
18714 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
18715 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
18716 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
18717
18718 cleanup_802() {
18719         trap 0
18720
18721         stopall
18722         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
18723         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
18724         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
18725         setupall
18726 }
18727
18728 test_802() {
18729
18730         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
18731         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
18732                 skip "Need server version at least 2.9.55"
18733
18734         mkdir $DIR/$tdir || error "(1) fail to mkdir"
18735
18736         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
18737                 error "(2) Fail to copy"
18738
18739         trap cleanup_802 EXIT
18740
18741         # sync by force before remount as readonly
18742         sync; sync_all_data; sleep 3; sync_all_data
18743
18744         stopall
18745
18746         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
18747         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
18748         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
18749
18750         echo "Mount the server as read only"
18751         setupall server_only || error "(3) Fail to start servers"
18752
18753         echo "Mount client without ro should fail"
18754         mount_client $MOUNT &&
18755                 error "(4) Mount client without 'ro' should fail"
18756
18757         echo "Mount client with ro should succeed"
18758         mount_client $MOUNT ro ||
18759                 error "(5) Mount client with 'ro' should succeed"
18760
18761         echo "Modify should be refused"
18762         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
18763
18764         echo "Read should be allowed"
18765         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
18766                 error "(7) Read should succeed under ro mode"
18767
18768         cleanup_802
18769 }
18770 run_test 802 "simulate readonly device"
18771
18772 test_803() {
18773         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18774         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
18775                 skip "MDS needs to be newer than 2.10.54"
18776
18777         mkdir -p $DIR/$tdir
18778         # Create some objects on all MDTs to trigger related logs objects
18779         for idx in $(seq $MDSCOUNT); do
18780                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
18781                         $DIR/$tdir/dir${idx} ||
18782                         error "Fail to create $DIR/$tdir/dir${idx}"
18783         done
18784
18785         sync; sleep 3
18786         echo "before create:"
18787         $LFS df -i $MOUNT
18788         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18789
18790         for ((i=0; i<10; i++)); do
18791                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
18792                         error "Fail to create $DIR/$tdir/foo$i"
18793         done
18794
18795         sync; sleep 3
18796         echo "after create:"
18797         $LFS df -i $MOUNT
18798         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18799
18800         [ $after_used -ge $((before_used + 10)) ] ||
18801                 error "before ($before_used) + 10 > after ($after_used)"
18802
18803         for ((i=0; i<10; i++)); do
18804                 rm -rf $DIR/$tdir/foo$i ||
18805                         error "Fail to remove $DIR/$tdir/foo$i"
18806         done
18807
18808         sleep 3 # avoid MDT return cached statfs
18809         wait_delete_completed
18810         echo "after unlink:"
18811         $LFS df -i $MOUNT
18812         before_used=$after_used
18813         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18814
18815         [ $after_used -le $((before_used - 8)) ] ||
18816                 error "before ($before_used) - 8 < after ($after_used)"
18817 }
18818 run_test 803 "verify agent object for remote object"
18819
18820 test_804() {
18821         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18822         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
18823                 skip "MDS needs to be newer than 2.10.54"
18824         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
18825                 skip_env "ldiskfs only test"
18826
18827         mkdir -p $DIR/$tdir
18828         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
18829                 error "Fail to create $DIR/$tdir/dir0"
18830
18831         local fid=$($LFS path2fid $DIR/$tdir/dir0)
18832         local dev=$(mdsdevname 2)
18833
18834         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18835                 grep ${fid} || error "NOT found agent entry for dir0"
18836
18837         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
18838                 error "Fail to create $DIR/$tdir/dir1"
18839
18840         touch $DIR/$tdir/dir1/foo0 ||
18841                 error "Fail to create $DIR/$tdir/dir1/foo0"
18842         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
18843         local rc=0
18844
18845         for idx in $(seq $MDSCOUNT); do
18846                 dev=$(mdsdevname $idx)
18847                 do_facet mds${idx} \
18848                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18849                         grep ${fid} && rc=$idx
18850         done
18851
18852         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
18853                 error "Fail to rename foo0 to foo1"
18854         if [ $rc -eq 0 ]; then
18855                 for idx in $(seq $MDSCOUNT); do
18856                         dev=$(mdsdevname $idx)
18857                         do_facet mds${idx} \
18858                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18859                         grep ${fid} && rc=$idx
18860                 done
18861         fi
18862
18863         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
18864                 error "Fail to rename foo1 to foo2"
18865         if [ $rc -eq 0 ]; then
18866                 for idx in $(seq $MDSCOUNT); do
18867                         dev=$(mdsdevname $idx)
18868                         do_facet mds${idx} \
18869                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18870                         grep ${fid} && rc=$idx
18871                 done
18872         fi
18873
18874         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
18875
18876         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
18877                 error "Fail to link to $DIR/$tdir/dir1/foo2"
18878         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
18879                 error "Fail to rename foo2 to foo0"
18880         unlink $DIR/$tdir/dir1/foo0 ||
18881                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
18882         rm -rf $DIR/$tdir/dir0 ||
18883                 error "Fail to rm $DIR/$tdir/dir0"
18884
18885         for idx in $(seq $MDSCOUNT); do
18886                 dev=$(mdsdevname $idx)
18887                 rc=0
18888
18889                 stop mds${idx}
18890                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
18891                         rc=$?
18892                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
18893                         error "mount mds$idx failed"
18894                 df $MOUNT > /dev/null 2>&1
18895
18896                 # e2fsck should not return error
18897                 [ $rc -eq 0 ] ||
18898                         error "e2fsck detected error on MDT${idx}: rc=$rc"
18899         done
18900 }
18901 run_test 804 "verify agent entry for remote entry"
18902
18903 cleanup_805() {
18904         do_facet $SINGLEMDS zfs set quota=$old $fsset
18905         unlinkmany $DIR/$tdir/f- 1000000
18906         trap 0
18907 }
18908
18909 test_805() {
18910         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
18911         [ "$(facet_fstype mds1)" != "zfs" ] &&
18912                 skip "ZFS specific test"
18913         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
18914                 skip "netfree not implemented before 0.7"
18915         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
18916                 skip "Need MDS version at least 2.10.57"
18917
18918         local fsset
18919         local freekb
18920         local usedkb
18921         local old
18922         local quota
18923         local pref="osd-zfs.lustre-MDT0000."
18924
18925         # limit available space on MDS dataset to meet nospace issue
18926         # quickly. then ZFS 0.7.2 can use reserved space if asked
18927         # properly (using netfree flag in osd_declare_destroy()
18928         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
18929         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
18930                 gawk '{print $3}')
18931         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
18932         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
18933         let "usedkb=usedkb-freekb"
18934         let "freekb=freekb/2"
18935         if let "freekb > 5000"; then
18936                 let "freekb=5000"
18937         fi
18938         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
18939         trap cleanup_805 EXIT
18940         mkdir $DIR/$tdir
18941         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
18942         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
18943         rm -rf $DIR/$tdir || error "not able to remove"
18944         do_facet $SINGLEMDS zfs set quota=$old $fsset
18945         trap 0
18946 }
18947 run_test 805 "ZFS can remove from full fs"
18948
18949 #
18950 # tests that do cleanup/setup should be run at the end
18951 #
18952
18953 test_900() {
18954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18955         local ls
18956
18957         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
18958         $LCTL set_param fail_loc=0x903
18959
18960         cancel_lru_locks MGC
18961
18962         FAIL_ON_ERROR=true cleanup
18963         FAIL_ON_ERROR=true setup
18964 }
18965 run_test 900 "umount should not race with any mgc requeue thread"
18966
18967 complete $SECONDS
18968 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
18969 check_and_cleanup_lustre
18970 if [ "$I_MOUNTED" != "yes" ]; then
18971         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
18972 fi
18973 exit_status