Whamcloud - gitweb
LU-15 slow IO with read intense application
[fs/lustre-release.git] / lustre / tests / sanity-quota.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # Run test by setting NOSETUP=true when ltest has setup env for us
7 set -e
8
9 #kernel 2.4.x doesn't support quota
10 K_VER=`uname --kernel-release | cut -b 1-3`
11 if [ $K_VER = "2.4" ]; then
12     echo "Kernel 2.4 doesn't support quota"
13     exit 0
14 fi
15
16 SRCDIR=`dirname $0`
17 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
18
19 ONLY=${ONLY:-"$*"}
20 # test_11 has been used to protect a kernel bug(bz10912), now it isn't
21 # useful any more. Then add it to ALWAYS_EXCEPT. b=19835
22 ALWAYS_EXCEPT="10 11 $SANITY_QUOTA_EXCEPT"
23 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
24
25 case `uname -r` in
26 2.6*) FSTYPE=${FSTYPE:-ldiskfs};;
27 *) error "unsupported kernel" ;;
28 esac
29
30 [ "$ALWAYS_EXCEPT$EXCEPT" ] && \
31         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
32
33 TMP=${TMP:-/tmp}
34
35 ORIG_PWD=${PWD}
36 TSTID=${TSTID:-60000}
37 TSTID2=${TSTID2:-60001}
38 TSTUSR=${TSTUSR:-"quota_usr"}
39 TSTUSR2=${TSTUSR2:-"quota_2usr"}
40 BLK_SZ=1024
41 BUNIT_SZ=${BUNIT_SZ:-1024}      # min block quota unit(kB)
42 IUNIT_SZ=${IUNIT_SZ:-10}        # min inode quota unit
43 MAX_DQ_TIME=604800
44 MAX_IQ_TIME=604800
45 SANITY_QUOTA_USERS="quota15_1 quota15_2 quota15_3 quota15_4 quota15_5 quota15_6 \
46                     quota15_7 quota15_8 quota15_9 quota15_10 quota15_11 quota15_12 \
47                     quota15_13 quota15_14 quota15_15 quota15_16 quota15_17 quota15_18 \
48                     quota15_19 quota15_20 quota15_21 quota15_22 quota15_23 quota15_24 \
49                     quota15_25 quota15_26 quota15_27 quota15_28 quota15_29 quota15_30"
50
51 TRACE=${TRACE:-""}
52 LUSTRE=${LUSTRE:-`dirname $0`/..}
53 . $LUSTRE/tests/test-framework.sh
54 init_test_env $@
55 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
56 init_logging
57 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
58
59 unset ENABLE_QUOTA
60
61 require_dsh_mds || exit 0
62 require_dsh_ost || exit 0
63
64 [ "$SLOW" = "no" ] && EXCEPT_SLOW="9 10 11 18b 21"
65
66 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
67
68 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
69
70 DIR=${DIR:-$MOUNT}
71 DIR2=${DIR2:-$MOUNT2}
72
73 check_and_setup_lustre
74
75 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
76 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
77
78 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
79 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
80 SHOW_QUOTA_USER2="$LFS quota -v -u $TSTUSR2 $DIR"
81 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
82 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
83 SHOW_QUOTA_GROUP2="$LFS quota -v -g $TSTUSR2 $DIR"
84 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
85 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
86
87 # control the time of tests
88 cycle=30
89 [ "$SLOW" = "no" ] && cycle=10
90
91 build_test_filter
92
93 eval ONLY_0=true
94 eval ONLY_99=true
95
96 # set_blk_tunables(btune_sz)
97 set_blk_tunesz() {
98         local btune=$(($1 * BLK_SZ))
99         # set btune size on all obdfilters
100         do_nodes $(comma_list $(osts_nodes)) "lctl set_param lquota.${FSNAME}-OST*.quota_btune_sz=$btune"
101         # set btune size on mds
102         do_facet mds  "lctl set_param lquota.*${FSNAME}-MDT*.quota_btune_sz=$btune"
103 }
104
105 # set_blk_unitsz(bunit_sz)
106 set_blk_unitsz() {
107         local bunit=$(($1 * BLK_SZ))
108         # set bunit size on all obdfilters
109         do_nodes $(comma_list $(osts_nodes)) "lctl set_param lquota.${FSNAME}-OST*.quota_bunit_sz=$bunit"
110         # set bunit size on mds
111         do_facet mds  "lctl set_param lquota.*${FSNAME}-MDT*.quota_bunit_sz=$bunit"
112 }
113
114 # set_file_tunesz(itune_sz)
115 set_file_tunesz() {
116         local itune=$1
117         # set itune size on mds
118         do_facet mds  "lctl set_param lquota.*${FSNAME}-MDT*.quota_itune_sz=$itune"
119 }
120
121 # set_file_unitsz(iunit_sz)
122 set_file_unitsz() {
123         local iunit=$1
124         # set iunit size on mds
125         do_facet mds  "lctl set_param lquota.*${FSNAME}-MDT*.quota_iunit_sz=$iunit"
126 }
127
128 lustre_fail() {
129         local fail_node=$1
130         local fail_loc=$2
131         local fail_val=${3:-0}
132
133         if [ $fail_node == "mds" ] || [ $fail_node == "mds_ost" ]; then
134             if [ $((fail_loc & 0x10000000)) -ne 0  -a $fail_val -gt 0 ] || \
135                 [ $((fail_loc)) -eq 0 ]; then
136                 do_facet mds "lctl set_param fail_val=$fail_val"
137             fi
138             do_facet mds "lctl set_param fail_loc=$fail_loc"
139         fi
140         if [ $fail_node == "ost" ] || [ $fail_node == "mds_ost" ]; then
141             for num in `seq $OSTCOUNT`; do
142                 if [ $((fail_loc & 0x10000000)) -ne 0 -a $fail_val -gt 0 ] || \
143                     [ $((fail_loc)) -eq 0 ]; then
144                     do_facet ost$num "lctl set_param fail_val=$fail_val"
145                 fi
146                 do_facet ost$num "lctl set_param fail_loc=$fail_loc"
147             done
148         fi
149 }
150
151 RUNAS="runas -u $TSTID -g $TSTID"
152 RUNAS2="runas -u $TSTID2 -g $TSTID2"
153 FAIL_ON_ERROR=true check_runas_id $TSTID $TSTID $RUNAS
154 FAIL_ON_ERROR=true check_runas_id $TSTID2 $TSTID2 $RUNAS2
155
156 FAIL_ON_ERROR=false
157
158 run_test_with_stat() {
159         (($# != 2)) && error "the number of arguments is wrong"
160
161         do_facet mds  "lctl set_param lquota.*${FSNAME}-MDT*.stats=0" > /dev/null || true
162         for j in `seq $OSTCOUNT`; do
163             do_facet ost$j "lctl set_param lquota.${FSNAME}-OST*.stats=0" > /dev/null || true
164         done
165         run_test "$@"
166         if [ ${STAT:-"yes"} != "no" -a -z "$LAST_SKIPPED" ]; then
167             echo "statistics info begin ***************************************"
168             do_facet mds  "lctl get_param lquota.*${FSNAME}-MDT*.stats"
169             for j in `seq $OSTCOUNT`; do
170                 do_facet ost$j "lctl get_param lquota.${FSNAME}-OST*.stats"
171             done
172             echo "statistics info end   ***************************************"
173         fi
174 }
175
176 #
177 # clear quota limits for a user or a group
178 # usage: resetquota -u username
179 #        resetquota -g groupname
180
181 resetquota() {
182         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
183         [ "$1" != "-u" -a "$1" != "-g" ] && error "resetquota: wrong specifier $1 passed"
184
185         count=0
186         if at_is_enabled; then
187             timeout=$(at_max_get mds)
188         else
189             timeout=$(lctl get_param -n timeout)
190         fi
191
192         while [ $((count++)) -lt $timeout ]; do
193                 $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT
194                 RC=$?
195                 if [ $RC -ne 0 ]; then
196                         if [ $RC -eq 240 ]; then # 240 means -EBUSY
197                                 log "resetquota is blocked for quota master recovery, retry after 1 sec"
198                                 sleep 1
199                                 continue
200                         else
201                                 error "resetquota failed: $RC"
202                         fi
203                 fi
204                 break
205         done
206
207         [ $count -lt $timeout ] || error "resetquota timeout: $timeout"
208 }
209
210 quota_scan() {
211         LOCAL_UG=$1
212         LOCAL_ID=$2
213
214         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "u" ]; then
215                 log "Files for user ($LOCAL_ID):"
216                 ($LFS find -user $LOCAL_ID $DIR | xargs stat 2>/dev/null)
217         fi
218
219         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "g" ]; then
220                 log "Files for group ($LOCAL_ID):"
221                 ($LFS find -group $LOCAL_ID $DIR | xargs stat 2>/dev/null)
222         fi
223 }
224
225 quota_error() {
226         quota_scan $1 $2
227         shift 2
228         error "$*"
229 }
230
231 quota_log() {
232         quota_scan $1 $2
233         shift 2
234         log "$*"
235 }
236
237 #
238 # get quota info for a user or a group
239 # usage: getquota -u|-g <username>|<groupname> global|<obd_uuid> bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
240 #
241 getquota() {
242         local spec
243         local uuid
244
245         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
246         [ "$1" != "-u" -a "$1" != "-g" ] && error "getquota: wrong u/g specifier $1 passed"
247
248         uuid="$3"
249
250         case $4 in
251                 curspace)   spec=1;;
252                 bsoftlimit) spec=2;;
253                 bhardlimit) spec=3;;
254                 bgrace)     spec=4;;
255                 curinodes)  spec=5;;
256                 isoftlimit) spec=6;;
257                 ihardlimit) spec=7;;
258                 igrace)     spec=8;;
259                 *)          error "unknown quota parameter $4";;
260         esac
261
262         [ "$uuid" = "global" ] && uuid=$DIR
263
264         $LFS quota -v "$1" "$2" $DIR | awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") { if (NF == 1) { getline } else { num++ } ; print $num;} }'
265 }
266
267 quota_show_check() {
268         LOCAL_BF=$1
269         LOCAL_UG=$2
270         LOCAL_ID=$3
271         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
272
273         $LFS quota -v -$LOCAL_UG $LOCAL_ID $DIR
274
275         if [ "$LOCAL_BF" == "a" -o "$LOCAL_BF" == "b" ]; then
276                 USAGE=`getquota -$LOCAL_UG $LOCAL_ID global curspace`
277                 if [ -z $USAGE ]; then
278                         quota_error $LOCAL_UG $LOCAL_ID "System is error when query quota for block ($LOCAL_UG:$LOCAL_ID)."
279                 else
280                         [ $USAGE -ne 0 ] && quota_log $LOCAL_UG $LOCAL_ID "System is not clean for block ($LOCAL_UG:$LOCAL_ID:$USAGE)."
281                 fi
282         fi
283
284         if [ "$LOCAL_BF" == "a" -o "$LOCAL_BF" == "f" ]; then
285                 USAGE=`getquota -$LOCAL_UG $LOCAL_ID global curinodes`
286                 if [ -z $USAGE ]; then
287                         quota_error $LOCAL_UG $LOCAL_ID "System is error when query quota for file ($LOCAL_UG:$LOCAL_ID)."
288                 else
289                         [ $USAGE -ne 0 ] && quota_log $LOCAL_UG $LOCAL_ID "System is not clean for file ($LOCAL_UG:$LOCAL_ID:$USAGE)."
290                 fi
291         fi
292 }
293
294 # set quota
295 test_0() {
296         lctl set_param debug="+quota"
297         do_facet mds "lctl set_param debug=+quota"
298         for num in `seq $OSTCOUNT`; do
299             do_facet ost$num "lctl set_param debug=+quota"
300         done
301
302         log "do the quotacheck ..."
303         $LFS quotacheck -ug $DIR
304
305         resetquota -u $TSTUSR
306         resetquota -g $TSTUSR
307 }
308 run_test_with_stat 0 "Set quota ============================="
309
310 # test for specific quota limitation, qunit, qtune $1=block_quota_limit
311 test_1_sub() {
312         LIMIT=$1
313         mkdir -p $DIR/$tdir
314         chmod 0777 $DIR/$tdir
315         TESTFILE="$DIR/$tdir/$tfile-0"
316
317         wait_delete_completed
318
319         # test for user
320         log "  User quota (limit: $LIMIT kbytes)"
321         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
322         sleep 3
323         quota_show_check b u $TSTUSR
324
325         $LFS setstripe $TESTFILE -c 1
326         chown $TSTUSR.$TSTUSR $TESTFILE
327
328         log "    Write ..."
329         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) || quota_error u $TSTUSR "(usr) write failure, but expect success"
330         log "    Done"
331         log "    Write out of block quota ..."
332         # this time maybe cache write,  ignore it's failure
333         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) seek=$(($LIMIT/2)) || true
334         # flush cache, ensure noquota flag is setted on client
335         cancel_lru_locks osc
336         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ seek=$LIMIT && quota_error u $TSTUSR "(usr) write success, but expect EDQUOT"
337
338         rm -f $TESTFILE
339         sync; sleep 1; sync;
340         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
341         OST0_QUOTA_USED=`getquota -u $TSTUSR $OST0_UUID curspace`
342         echo $OST0_QUOTA_USED
343         [ $OST0_QUOTA_USED -ne 0 ] && \
344             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "(usr) quota deleted isn't released")
345         $SHOW_QUOTA_USER
346         resetquota -u $TSTUSR
347
348         # test for group
349         log "--------------------------------------"
350         log "  Group quota (limit: $LIMIT kbytes)"
351         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
352         sleep 3
353         quota_show_check b g $TSTUSR
354         TESTFILE="$DIR/$tdir/$tfile-1"
355
356         $LFS setstripe $TESTFILE -c 1
357         chown $TSTUSR.$TSTUSR $TESTFILE
358
359         log "    Write ..."
360         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) || quota_error g $TSTUSR "(grp) write failure, but expect success"
361         log "    Done"
362         log "    Write out of block quota ..."
363         # this time maybe cache write, ignore it's failure
364         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) seek=$(($LIMIT/2)) || true
365         cancel_lru_locks osc
366         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ seek=$LIMIT && quota_error g $TSTUSR "(grp) write success, but expect EDQUOT"
367
368         # cleanup
369         rm -f $TESTFILE
370         sync; sleep 1; sync;
371         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
372         OST0_QUOTA_USED=`getquota -g $TSTUSR $OST0_UUID curspace`
373         echo $OST0_QUOTA_USED
374         [ $OST0_QUOTA_USED -ne 0 ] && \
375             ($SHOW_QUOTA_GROUP; quota_error g $TSTUSR "(grp) quota deleted isn't released")
376         $SHOW_QUOTA_GROUP
377         resetquota -g $TSTUSR
378 }
379
380 # block hard limit (normal use and out of quota)
381 test_1() {
382         for i in `seq 1 $cycle`; do
383             # define blk_qunit is between 1M and 4M
384             blk_qunit=$(( $RANDOM % 3072 + 1024 ))
385             blk_qtune=$(( $RANDOM % $blk_qunit ))
386             # other osts and mds will occupy at 1M blk quota
387             b_limit=$(( ($RANDOM - 16384) / 8 +  $OSTCOUNT * $blk_qunit * 4 ))
388             set_blk_tunesz $blk_qtune
389             set_blk_unitsz $blk_qunit
390             echo "cycle: $i(total $cycle) bunit:$blk_qunit, btune:$blk_qtune, blimit:$b_limit"
391             test_1_sub $b_limit
392             echo "=================================================="
393             set_blk_unitsz $((128 * 1024))
394             set_blk_tunesz $((128 * 1024 / 2))
395         done
396 }
397 run_test_with_stat 1 "Block hard limit (normal use and out of quota) ==="
398
399 # test for specific quota limitation, qunit, qtune $1=block_quota_limit
400 test_2_sub() {
401         LIMIT=$1
402         mkdir -p $DIR/$tdir
403         chmod 0777 $DIR/$tdir
404         TESTFILE="$DIR/$tdir/$tfile-0"
405
406         wait_delete_completed
407
408         # test for user
409         log "  User quota (limit: $LIMIT files)"
410         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
411         sleep 3
412         quota_show_check f u $TSTUSR
413
414         log "    Create $LIMIT files ..."
415         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
416                 quota_error u $TSTUSR "(usr) create failure, but expect success"
417         log "    Done"
418         log "    Create out of file quota ..."
419         $RUNAS touch ${TESTFILE}_xxx && \
420                 quota_error u $TSTUSR "(usr) touch success, but expect EDQUOT"
421
422         unlinkmany ${TESTFILE} $LIMIT
423         rm -f ${TESTFILE}_xxx
424         sync; sleep 1; sync;
425
426         # interop 18 <-> 20
427         local dev=$(get_mds_mdt_device_proc_path)
428         MDS_UUID=`do_facet mds $LCTL dl | grep -m1 " $dev " | awk '{print $((NF-1))}'`
429         MDS_QUOTA_USED=`getquota -u $TSTUSR $MDS_UUID curinodes`
430
431         echo $MDS_QUOTA_USED
432         [ $MDS_QUOTA_USED -ne 0 ] && \
433             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "(usr) quota deleted isn't released")
434         $SHOW_QUOTA_USER
435         resetquota -u $TSTUSR
436
437         # test for group
438         log "--------------------------------------"
439         log "  Group quota (limit: $LIMIT FILE)"
440         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
441         sleep 3
442         quota_show_check f g $TSTUSR
443         TESTFILE=$DIR/$tdir/$tfile-1
444
445         log "    Create $LIMIT files ..."
446         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
447                 quota_error g $TSTUSR "(grp) create failure, but expect success"
448         log "    Done"
449         log "    Create out of file quota ..."
450         $RUNAS touch ${TESTFILE}_xxx && \
451                 quota_error g $TSTUSR "(grp) touch success, but expect EDQUOT"
452
453         unlinkmany ${TESTFILE} $LIMIT
454         rm -f ${TESTFILE}_xxx
455         sync; sleep 1; sync;
456
457         MDS_UUID=`do_facet mds $LCTL dl | grep -m1 " $dev " | awk '{print $((NF-1))}'`
458         MDS_QUOTA_USED=`getquota -g $TSTUSR $MDS_UUID curinodes`
459
460         echo $MDS_QUOTA_USED
461         [ $MDS_QUOTA_USED -ne 0 ] && \
462             ($SHOW_QUOTA_GROUP; quota_error g $TSTUSR "(grp) quota deleted isn't released")
463         $SHOW_QUOTA_GROUP
464         resetquota -g $TSTUSR
465 }
466
467 # file hard limit (normal use and out of quota)
468 test_2() {
469         for i in `seq 1 $cycle`; do
470             if [ $i -eq 1 ]; then
471                 ino_qunit=52
472                 ino_qtune=41
473                 i_limit=11
474             else
475                 # define ino_qunit is between 10 and 100
476                 ino_qunit=$(( $RANDOM % 90 + 10 ))
477                 ino_qtune=$(( $RANDOM % $ino_qunit ))
478                 # RANDOM's maxium is 32767
479                 i_limit=$(( $RANDOM % 990 + 10 ))
480             fi
481
482             set_file_tunesz $ino_qtune
483             set_file_unitsz $ino_qunit
484             echo "cycle: $i(total $cycle) iunit:$ino_qunit, itune:$ino_qtune, ilimit:$i_limit"
485             test_2_sub $i_limit
486             echo "=================================================="
487             set_file_unitsz 5120
488             set_file_tunesz 2560
489         done
490 }
491 run_test_with_stat 2 "File hard limit (normal use and out of quota) ==="
492
493 test_block_soft() {
494         TESTFILE=$1
495         TIMER=$(($2 * 3 / 2))
496         OFFSET=0
497
498         wait_delete_completed
499
500         echo "    Write to exceed soft limit"
501         RUNDD="$RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ"
502         $RUNDD count=$((BUNIT_SZ+1)) || \
503                 quota_error a $TSTUSR "write failure, but expect success"
504         OFFSET=$((OFFSET + BUNIT_SZ + 1))
505         cancel_lru_locks osc
506
507         $SHOW_QUOTA_USER
508         $SHOW_QUOTA_GROUP
509         $SHOW_QUOTA_INFO_USER
510         $SHOW_QUOTA_INFO_GROUP
511
512         echo "    Write before timer goes off"
513         $RUNDD count=$BUNIT_SZ seek=$OFFSET || \
514                 quota_error a $TSTUSR "write failure, but expect success"
515         OFFSET=$((OFFSET + BUNIT_SZ))
516         cancel_lru_locks osc
517         echo "    Done"
518
519         echo "    Sleep $TIMER seconds ..."
520         sleep $TIMER
521
522         $SHOW_QUOTA_USER
523         $SHOW_QUOTA_GROUP
524         $SHOW_QUOTA_INFO_USER
525         $SHOW_QUOTA_INFO_GROUP
526
527         echo "    Write after timer goes off"
528         # maybe cache write, ignore.
529         $RUNDD count=$BUNIT_SZ seek=$OFFSET || true
530         OFFSET=$((OFFSET + BUNIT_SZ))
531         cancel_lru_locks osc
532         $RUNDD count=$BUNIT_SZ seek=$OFFSET && \
533                 quota_error a $TSTUSR "write success, but expect EDQUOT"
534
535         $SHOW_QUOTA_USER
536         $SHOW_QUOTA_GROUP
537         $SHOW_QUOTA_INFO_USER
538         $SHOW_QUOTA_INFO_GROUP
539
540         echo "    Unlink file to stop timer"
541         rm -f $TESTFILE
542         sync; sleep 1; sync
543         echo "    Done"
544
545         $SHOW_QUOTA_USER
546         $SHOW_QUOTA_GROUP
547         $SHOW_QUOTA_INFO_USER
548         $SHOW_QUOTA_INFO_GROUP
549
550         echo "    Write ..."
551         $RUNDD count=$BUNIT_SZ || quota_error a $TSTUSR "write failure, but expect success"
552         echo "    Done"
553
554         # cleanup
555         rm -f $TESTFILE
556         sync; sleep 3; sync;
557 }
558
559 # block soft limit (start timer, timer goes off, stop timer)
560 test_3() {
561         mkdir -p $DIR/$tdir
562         chmod 0777 $DIR/$tdir
563
564         # 1 bunit on mds and 1 bunit on every ost
565         LIMIT=$(( $BUNIT_SZ * ($OSTCOUNT + 1) ))
566         GRACE=10
567
568         echo "  User quota (soft limit: $LIMIT kbytes  grace: $GRACE seconds)"
569         TESTFILE=$DIR/$tdir/$tfile-0
570
571         $LFS setstripe $TESTFILE -c 1
572         chown $TSTUSR.$TSTUSR $TESTFILE
573
574         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
575         $LFS setquota -u $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
576
577         test_block_soft $TESTFILE $GRACE
578         resetquota -u $TSTUSR
579
580         echo "  Group quota (soft limit: $LIMIT kbytes  grace: $GRACE seconds)"
581         TESTFILE=$DIR/$tdir/$tfile-1
582
583         $LFS setstripe $TESTFILE -c 1
584         chown $TSTUSR.$TSTUSR $TESTFILE
585
586         $LFS setquota -t -g --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
587         $LFS setquota -g $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
588
589         test_block_soft $TESTFILE $GRACE
590         resetquota -g $TSTUSR
591 }
592 run_test_with_stat 3 "Block soft limit (start timer, timer goes off, stop timer) ==="
593
594 test_file_soft() {
595         TESTFILE=$1
596         LIMIT=$2
597         TIMER=$(($3 * 3 / 2))
598
599         wait_delete_completed
600
601         echo "    Create files to exceed soft limit"
602         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) || \
603                 quota_error a $TSTUSR "create failure, but expect success"
604         sync; sleep 1; sync
605         echo "    Done"
606
607         echo "    Create file before timer goes off"
608         $RUNAS touch ${TESTFILE}_before || \
609                 quota_error a $TSTUSR "failed create before timer expired, but expect success"
610         sync; sleep 1; sync
611         echo "    Done"
612
613         echo "    Sleep $TIMER seconds ..."
614         sleep $TIMER
615
616         $SHOW_QUOTA_USER
617         $SHOW_QUOTA_GROUP
618         $SHOW_QUOTA_INFO_USER
619         $SHOW_QUOTA_INFO_GROUP
620
621         echo "    Create file after timer goes off"
622         # the least of inode qunit is 2, so there are at most 3(qunit:2+qtune:1)
623         # inode quota left here
624         $RUNAS touch ${TESTFILE}_after ${TESTFILE}_after1 ${TESTFILE}_after2 || true
625         sync; sleep 1; sync
626         $RUNAS touch ${TESTFILE}_after3 && \
627                 quota_error a $TSTUSR "create after timer expired, but expect EDQUOT"
628         sync; sleep 1; sync
629
630         $SHOW_QUOTA_USER
631         $SHOW_QUOTA_GROUP
632         $SHOW_QUOTA_INFO_USER
633         $SHOW_QUOTA_INFO_GROUP
634
635         echo "    Unlink files to stop timer"
636         find `dirname $TESTFILE` -name "`basename ${TESTFILE}`*" | xargs rm -f
637         echo "    Done"
638
639         echo "    Create file"
640         $RUNAS touch ${TESTFILE}_xxx || \
641                 quota_error a $TSTUSR "touch after timer stop failure, but expect success"
642         sync; sleep 1; sync
643         echo "    Done"
644
645         # cleanup
646         rm -f ${TESTFILE}_xxx
647         sync; sleep 3; sync;
648 }
649
650 # file soft limit (start timer, timer goes off, stop timer)
651 test_4a() {        # was test_4
652         mkdir -p $DIR/$tdir
653         chmod 0777 $DIR/$tdir
654         LIMIT=$(($IUNIT_SZ * 10))        # 10 iunits on mds
655         TESTFILE=$DIR/$tdir/$tfile-0
656
657         GRACE=5
658
659         echo "  User quota (soft limit: $LIMIT files  grace: $GRACE seconds)"
660         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
661         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
662         quota_show_check f u $TSTUSR
663
664         test_file_soft $TESTFILE $LIMIT $GRACE
665         resetquota -u $TSTUSR
666
667         echo "  Group quota (soft limit: $LIMIT files  grace: $GRACE seconds)"
668         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
669         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
670         quota_show_check f g $TSTUSR
671         TESTFILE=$DIR/$tdir/$tfile-1
672
673         test_file_soft $TESTFILE $LIMIT $GRACE
674         resetquota -g $TSTUSR
675
676         # cleanup
677         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
678         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
679 }
680 run_test_with_stat 4a "File soft limit (start timer, timer goes off, stop timer) ==="
681
682 test_4b() {        # was test_4a
683         GR_STR1="1w3d"
684         GR_STR2="1000s"
685         GR_STR3="5s"
686         GR_STR4="1w2d3h4m5s"
687         GR_STR5="5c"
688         GR_STR6="1111111111111111"
689
690         wait_delete_completed
691
692         # test of valid grace strings handling
693         echo "  Valid grace strings test"
694         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace $GR_STR2 $DIR
695         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
696         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace $GR_STR4 $DIR
697         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
698
699         # test of invalid grace strings handling
700         echo "  Invalid grace strings test"
701         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
702         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
703
704         # cleanup
705         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
706         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
707 }
708 run_test_with_stat 4b "Grace time strings handling ==="
709
710 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
711 test_5() {
712         mkdir -p $DIR/$tdir
713         BLIMIT=$(( $BUNIT_SZ * $((OSTCOUNT + 1)) * 10)) # 10 bunits on each server
714         ILIMIT=$(( $IUNIT_SZ * 10 )) # 10 iunits on mds
715
716         wait_delete_completed
717
718         echo "  Set quota limit (0 $BLIMIT 0 $ILIMIT) for $TSTUSR.$TSTUSR"
719         $LFS setquota -u $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $DIR
720         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $DIR
721         quota_show_check a u $TSTUSR
722         quota_show_check a g $TSTUSR
723
724         echo "  Create more than $ILIMIT files and more than $BLIMIT kbytes ..."
725         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) || \
726                 error "touch failure, expect success"
727         dd if=/dev/zero of=$DIR/$tdir/$tfile-0_1 bs=$BLK_SZ count=$((BLIMIT+1)) || error "write failure, expect success"
728
729         echo "  Chown files to $TSTUSR.$TSTUSR ..."
730         for i in `seq 0 $ILIMIT`; do
731         chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i || \
732                         quota_error a $TSTUSR "chown failure, but expect success"
733         done
734
735         # cleanup
736         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
737         sync; sleep 3; sync;
738
739         resetquota -u $TSTUSR
740         resetquota -g $TSTUSR
741 }
742 run_test_with_stat 5 "Chown & chgrp successfully even out of block/file quota ==="
743
744 # block quota acquire & release
745 test_6() {
746         if [ $OSTCOUNT -lt 2 ]; then
747                 skip_env "$OSTCOUNT < 2, too few osts"
748                 return 0;
749         fi
750
751         wait_delete_completed
752
753         mkdir -p $DIR/$tdir
754         chmod 0777 $DIR/$tdir
755
756         LIMIT=$((BUNIT_SZ * (OSTCOUNT + 1) * 5)) # 5 bunits per server
757         FILEA="$DIR/$tdir/$tfile-0_a"
758         FILEB="$DIR/$tdir/$tfile-0_b"
759
760         echo "  Set block limit $LIMIT kbytes to $TSTUSR.$TSTUSR"
761         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
762         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
763         quota_show_check b u $TSTUSR
764         quota_show_check b g $TSTUSR
765
766         echo "  Create filea on OST0 and fileb on OST1"
767         $LFS setstripe $FILEA -i 0 -c 1
768         $LFS setstripe $FILEB -i 1 -c 1
769         chown $TSTUSR.$TSTUSR $FILEA
770         chown $TSTUSR.$TSTUSR $FILEB
771
772         echo "  Exceed quota limit ..."
773         RUNDD="$RUNAS dd if=/dev/zero of=$FILEA bs=$BLK_SZ"
774         $RUNDD count=$((LIMIT - BUNIT_SZ * OSTCOUNT)) || \
775                 quota_error a $TSTUSR "write filea failure, but expect success"
776
777         cancel_lru_locks osc
778         $SHOW_QUOTA_USER
779         $SHOW_QUOTA_GROUP
780         $RUNDD seek=$LIMIT count=$((BUNIT_SZ * OSTCOUNT)) && \
781                 quota_error a $TSTUSR "write filea success, but expect EDQUOT"
782         cancel_lru_locks osc
783         echo "  Write to OST1 return EDQUOT"
784         # this write maybe cache write, ignore it's failure
785         RUNDD="$RUNAS dd if=/dev/zero of=$FILEB bs=$BLK_SZ"
786         $RUNDD count=$(($BUNIT_SZ * 2)) || true
787         cancel_lru_locks osc
788         $SHOW_QUOTA_USER
789         $SHOW_QUOTA_GROUP
790         $RUNDD count=$((BUNIT_SZ * 2)) seek=$((BUNIT_SZ *2)) && \
791                 quota_error a $TSTUSR "write fileb success, but expect EDQUOT"
792
793         echo "  Remove filea to let OST0 release quota"
794         rm -f $FILEA
795
796         if at_is_enabled; then
797             timeout=$(at_max_get mds)
798         else
799             timeout=$(lctl get_param -n timeout)
800         fi
801         count=$((timeout / 5))
802         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
803
804         while [ $((count--)) -gt 0 ]; do
805                 sync && sleep 5
806
807                 OST0_QUOTA_HOLD=`getquota -u $TSTUSR $OST0_UUID bhardlimit`
808                 if [ -z $OST0_QUOTA_HOLD ]; then
809                         error "System is error when query quota for block (U:$TSTUSR)."
810                 else
811                         [ $OST0_QUOTA_HOLD -gt $BUNIT_SZ ] && continue
812                 fi
813
814                 break
815         done
816
817         [ ! $count -gt 0 ] && error "Release quota for block timeout (U:$TSTUSR)."
818         $SHOW_QUOTA_USER
819
820         while [ $((count--)) -gt 0 ]; do
821                 sync && sleep 5
822
823                 OST0_QUOTA_HOLD=`getquota -g $TSTUSR $OST0_UUID bhardlimit`
824                 if [ -z $OST0_QUOTA_HOLD ]; then
825                         error "System is error when query quota for block (G:$TSTUSR)."
826                 else
827                         [ $OST0_QUOTA_HOLD -gt $BUNIT_SZ ] && continue
828                 fi
829
830                 break
831         done
832
833         [ ! $count -gt 0 ] && error "Release quota for block timeout (G:$TSTUSR)."
834         $SHOW_QUOTA_GROUP
835
836         echo "  Write to OST1"
837         $RUNDD count=$((LIMIT - BUNIT_SZ * OSTCOUNT)) || \
838                 quota_error a $TSTUSR "write fileb failure, expect success"
839         echo "  Done"
840
841         # cleanup
842         rm -f $FILEB
843         sync; sleep 3; sync;
844
845         resetquota -u $TSTUSR
846         resetquota -g $TSTUSR
847         return 0
848 }
849 run_test_with_stat 6 "Block quota acquire & release ========="
850
851 # quota recovery (block quota only by now)
852 test_7()
853 {
854         mkdir -p $DIR/$tdir
855         chmod 0777 $DIR/$tdir
856
857         wait_delete_completed
858
859         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) ))
860         TESTFILE="$DIR/$tdir/$tfile-0"
861
862         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
863
864         $LFS setstripe $TESTFILE -c 1
865         chown $TSTUSR.$TSTUSR $TESTFILE
866
867         echo "  Write to OST0..."
868         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ || \
869                 quota_error u $TSTUSR "write failure, but expect success"
870
871         #define OBD_FAIL_OBD_DQACQ               0x604
872         lustre_fail mds  0x604
873         echo "  Remove files on OST0"
874         rm -f $TESTFILE
875         lustre_fail mds  0
876
877         echo "  Trigger recovery..."
878         OSC0_UUID="`$LCTL dl | awk '$3 ~ /osc/ { print $1 }'`"
879         for i in $OSC0_UUID; do
880                 $LCTL --device $i activate || error "activate osc failed!"
881         done
882
883         # sleep a while to wait for recovery done
884         sleep 20
885
886         # check limits
887         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
888         TOTAL_LIMIT=`getquota -u $TSTUSR global bhardlimit`
889         [ $TOTAL_LIMIT -eq $LIMIT ] || error "total limits not recovery!"
890         echo "  total limits = $TOTAL_LIMIT"
891
892         OST0_UUID=`do_facet ost1 "$LCTL dl | grep -m1 obdfilter" | awk '{print $((NF-1))}'`
893         [ -z "$OST0_UUID" ] && OST0_UUID=`do_facet ost1 "$LCTL dl | grep -m1 obdfilter" | awk '{print $((NF-1))}'`
894         OST0_LIMIT=`getquota  -u $TSTUSR $OST0_UUID bhardlimit`
895         [ $OST0_LIMIT -eq $BUNIT_SZ ] || error "high limits not released!"
896         echo "  limits on $OST0_UUID = $OST0_LIMIT"
897
898         # cleanup
899         resetquota -u $TSTUSR
900 }
901 run_test_with_stat 7 "Quota recovery (only block limit) ======"
902
903 # run dbench with quota enabled
904 test_8() {
905         mkdir -p $DIR/$tdir
906         BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
907         FILE_LIMIT=1000000
908
909         wait_delete_completed
910
911         echo "  Set enough high limit for user: $TSTUSR"
912         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
913         echo "  Set enough high limit for group: $TSTUSR"
914         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
915
916         chmod 0777 $DIR/$tdir
917         local duration=""
918         [ "$SLOW" = "no" ] && duration=" -t 120"
919         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration || quota_error a $TSTUSR "dbench failed!"
920
921         rm -rf $DIR/$tdir
922         sync; sleep 3; sync;
923
924         return 0
925 }
926 run_test_with_stat 8 "Run dbench with quota enabled ==========="
927
928 # run for fixing bug10707, it needs a big room. test for 64bit
929 KB=1024
930 GB=$((KB * 1024 * 1024))
931 # Use this as dd bs to decrease time
932 # inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
933 blksize=$((1 << 21)) # 2Mb
934 size_file=$((GB * 9 / 2))
935 # this check is just for test9 and test10
936 OST0_MIN=4900000 #4.67G
937 check_whether_skip () {
938     OST0_SIZE=`$LFS df $DIR | awk '/\[OST:0\]/ {print $4}'`
939     log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
940     if [ $OST0_SIZE -lt $OST0_MIN ]; then
941         echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
942         return 0
943     else
944         return 1
945     fi
946 }
947
948 test_9() {
949         check_whether_skip && return 0
950
951         wait_delete_completed
952
953         set_blk_tunesz 512
954         set_blk_unitsz 1024
955
956         mkdir -p $DIR/$tdir
957         chmod 0777 $DIR/$tdir
958         TESTFILE="$DIR/$tdir/$tfile-0"
959
960         BLK_LIMIT=$((100 * KB * KB)) # 100G
961         FILE_LIMIT=1000000
962         echo "  Set block limit $BLK_LIMIT kbytes to $TSTUSR.$TSTUSR"
963
964         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for user: $TSTUSR"
965         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
966         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for group: $TSTUSR"
967         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
968
969         quota_show_check a u $TSTUSR
970         quota_show_check a g $TSTUSR
971
972         echo "  Set stripe"
973         $LFS setstripe $TESTFILE -c 1
974         touch $TESTFILE
975         chown $TSTUSR.$TSTUSR $TESTFILE
976
977         log "    Write the big file of 4.5G ..."
978         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$blksize count=$((size_file / blksize)) || \
979                quota_error a $TSTUSR "(usr) write 4.5G file failure, but expect success"
980
981         $SHOW_QUOTA_USER
982         $SHOW_QUOTA_GROUP
983
984         log "    delete the big file of 4.5G..."
985         $RUNAS rm -f $TESTFILE
986         sync; sleep 3; sync;
987
988         $SHOW_QUOTA_USER
989         $SHOW_QUOTA_GROUP
990
991         RC=$?
992
993         set_blk_unitsz $((128 * 1024))
994         set_blk_tunesz $((128 * 1024 / 2))
995
996         return $RC
997 }
998 run_test_with_stat 9 "run for fixing bug10707(64bit) ==========="
999
1000 # run for fixing bug10707, it need a big room. test for 32bit
1001 test_10() {
1002         mkdir -p $DIR/$tdir
1003         chmod 0777 $DIR/$tdir
1004         check_whether_skip && return 0
1005
1006         wait_delete_completed
1007
1008         set_blk_tunesz 512
1009         set_blk_unitsz 1024
1010
1011         # make qd_count 32 bit
1012         lustre_fail mds_ost 0xA00
1013
1014         TESTFILE="$DIR/$tdir/$tfile-0"
1015
1016         BLK_LIMIT=$((100 * KB * KB)) # 100G
1017         FILE_LIMIT=1000000
1018
1019         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for user: $TSTUSR"
1020         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1021         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for group: $TSTUSR"
1022         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1023
1024         quota_show_check a u $TSTUSR
1025         quota_show_check a g $TSTUSR
1026
1027         echo "  Set stripe"
1028         $LFS setstripe $TESTFILE -c 1
1029         touch $TESTFILE
1030         chown $TSTUSR.$TSTUSR $TESTFILE
1031
1032         log "    Write the big file of 4.5 G ..."
1033         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$blksize count=$((size_file / blksize)) || \
1034                 quota_error a $TSTUSR "(usr) write 4.5 G file failure, but expect success"
1035
1036         $SHOW_QUOTA_USER
1037         $SHOW_QUOTA_GROUP
1038
1039         log "    delete the big file of 4.5 G..."
1040         $RUNAS rm -f $TESTFILE
1041         sync; sleep 3; sync;
1042
1043         $SHOW_QUOTA_USER
1044         $SHOW_QUOTA_GROUP
1045
1046         RC=$?
1047
1048         # make qd_count 64 bit
1049         lustre_fail mds_ost 0
1050
1051         set_blk_unitsz $((128 * 1024))
1052         set_blk_tunesz $((128 * 1024 / 2))
1053
1054         return $RC
1055 }
1056 run_test_with_stat 10 "run for fixing bug10707(32bit) ==========="
1057
1058 test_11() {
1059        wait_delete_completed
1060
1061        #prepare the test
1062        block_limit=`(echo 0; df -t lustre -P | awk '{print $(NF - 4)}') | tail -n 1`
1063        echo $block_limit
1064        orig_dbr=`sysctl -n vm.dirty_background_ratio`
1065        orig_dec=`sysctl -n vm.dirty_expire_centisecs`
1066        orig_dr=`sysctl -n vm.dirty_ratio`
1067        orig_dwc=`sysctl -n vm.dirty_writeback_centisecs`
1068        sysctl -w vm.dirty_background_ratio=1
1069        sysctl -w vm.dirty_expire_centisecs=30
1070        sysctl -w vm.dirty_ratio=1
1071        sysctl -w vm.dirty_writeback_centisecs=50
1072        TESTDIR="$DIR/$tdir"
1073        local RV=0
1074
1075        #do the test
1076        local SECS=0
1077        local REPS=3
1078        [ "$SLOW" = no ] && REPS=1
1079        local sleep=20
1080        local i=1
1081        while [ $i -le $REPS ]; do
1082            echo "test: cycle($i of $REPS) start at $(date)"
1083            mkdir -p $TESTDIR && chmod 777 $TESTDIR
1084            echo -n "    create a file for uid "
1085            for j in `seq 1 30`; do
1086                echo -n "$j "
1087                # 30MB per dd for a total of 900MB (if space even permits)
1088                runas -u $j dd if=/dev/zero of=$TESTDIR/$tfile  bs=$blksize count=15 > /dev/null 2>&1 &
1089            done
1090            echo ""
1091            PROCS=$(ps -ef | grep -v grep | grep "dd if /dev/zero of $TESTDIR" | wc -l)
1092            LAST_USED=0
1093            while [ $PROCS -gt 0 ]; do
1094              sleep 20
1095              SECS=$((SECS + sleep))
1096              PROCS=$(ps -ef | grep -v grep | grep "dd if /dev/zero of $TESTDIR" | wc -l)
1097              USED=$(du -s $TESTDIR | awk '{print $1}')
1098              PCT=$(($USED * 100 / $block_limit))
1099              echo "${i}/${REPS} ${PCT}% p${PROCS} t${SECS}  "
1100              if [ $USED -le $LAST_USED ]; then
1101                  kill -9 $(ps -ef | grep "dd if /dev/zero of $TESTDIR" | grep -v grep | awk '{ print $2 }')
1102                  i=$REPS
1103                  RV=2
1104                  break
1105              fi
1106              LAST_USED=$USED
1107            done
1108            echo "    removing the test files..."
1109            rm -f $TESTDIR/$tfile
1110            echo "cycle $i done at $(date)"
1111            i=$[$i+1]
1112        done
1113        echo "Test took $SECS sec"
1114
1115        #clean
1116        sysctl -w vm.dirty_background_ratio=$orig_dbr
1117        sysctl -w vm.dirty_expire_centisecs=$orig_dec
1118        sysctl -w vm.dirty_ratio=$orig_dr
1119        sysctl -w vm.dirty_writeback_centisecs=$orig_dwc
1120        if [ $RV -ne 0 ]; then
1121            error "Nothing was written for $SECS sec ... aborting"
1122        fi
1123        return $RV
1124 }
1125 run_test_with_stat 11 "run for fixing bug10912 ==========="
1126
1127 # test multiple clients write block quota b=11693
1128 test_13() {
1129         mkdir -p $DIR/$tdir
1130         wait_delete_completed
1131
1132         # one OST * 10 + (mds + other OSTs)
1133         LIMIT=$((BUNIT_SZ * 10 + (BUNIT_SZ * OSTCOUNT)))
1134         TESTFILE="$DIR/$tdir/$tfile"
1135
1136         echo "   User quota (limit: $LIMIT kbytes)"
1137         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1138         quota_show_check b u $TSTUSR
1139
1140         $LFS setstripe $TESTFILE -i 0 -c 1
1141         chown $TSTUSR.$TSTUSR $TESTFILE
1142         $LFS setstripe $TESTFILE.2 -i 0 -c 1
1143         chown $TSTUSR.$TSTUSR $TESTFILE.2
1144
1145         echo "   step1: write out of block quota ..."
1146         # one bunit will give mds
1147         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$[($LIMIT - $BUNIT_SZ) / 2] &
1148         DDPID=$!
1149         $RUNAS dd if=/dev/zero of=$TESTFILE.2 bs=$BLK_SZ count=$[($LIMIT - $BUNIT_SZ) / 2] &
1150         DDPID1=$!
1151
1152         echo  "   step2: testing ......"
1153         count=0
1154         while [ true ]; do
1155             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1156             count=$[count+1]
1157             if [ $count -gt 64 ]; then
1158                 quota_error u $TSTUSR "dd should be finished!"
1159             fi
1160             sleep 1
1161         done
1162         echo "(dd_pid=$DDPID, time=$count)successful"
1163
1164         count=0
1165         while [ true ]; do
1166             if ! ps -p ${DDPID1} > /dev/null 2>&1 ; then break; fi
1167             count=$[count+1]
1168             if [ $count -gt 64 ]; then
1169                 quota_error u $TSTUSR "dd should be finished!"
1170             fi
1171             sleep 1
1172         done
1173         echo "(dd_pid=$DDPID1, time=$count)successful"
1174
1175         sync; sleep 5; sync;
1176
1177         echo  "   step3: checking ......"
1178         fz=`stat -c %s $TESTFILE`
1179         fz2=`stat -c %s $TESTFILE.2`
1180         $SHOW_QUOTA_USER
1181         [ $((fz + fz2)) -lt $((BUNIT_SZ * BLK_SZ * 10)) ] && \
1182                 quota_error u $TSTUSR "files too small $fz + $fz2 < $((BUNIT_SZ * BLK_SZ * 10))"
1183
1184         rm -f $TESTFILE $TESTFILE.2
1185         sync; sleep 3; sync;
1186
1187         resetquota -u $TSTUSR
1188 }
1189 run_test_with_stat 13 "test multiple clients write block quota ==="
1190
1191 check_if_quota_zero(){
1192         line=`$LFS quota -v -$1 $2 $DIR | wc -l`
1193         for i in `seq 3 $line`; do
1194             if [ $i -eq 3 ]; then
1195                 field="3 4 7 8"
1196             else
1197                 field="3 6"
1198             fi
1199             for j in $field; do
1200                 tmp=`$LFS quota -v -$1 $2 $DIR | sed -n ${i}p |
1201                      awk  '{print $'"$j"'}'`
1202                 [ -n "$tmp" ] && [ $tmp -ne 0 ] && $LFS quota -v -$1 $2 $DIR && \
1203                     error "quota on $2 isn't clean"
1204             done
1205         done
1206         echo "pass check_if_quota_zero"
1207 }
1208
1209 test_14a() {        # was test_14 b=12223 -- setting quota on root
1210         TESTFILE="$DIR/$tdir/$tfile"
1211
1212         # reboot the lustre
1213         sync; sleep 5; sync
1214         cleanup_and_setup_lustre
1215         test_0
1216
1217         mkdir -p $DIR/$tdir
1218
1219         # out of root's file and block quota
1220         $LFS setquota -u root -b 10 -B 10 -i 10 -I 10 $DIR
1221         createmany -m ${TESTFILE} 20 || \
1222             quota_error u root "unexpected: user(root) create files failly!"
1223         multiop ${TESTFILE} oO_CREAT:O_WRONLY:O_DIRECT:w$((4096 * 4096))c || \
1224             quota_error u root "unexpected: user(root) write files failly!"
1225         chmod 666 $TESTFILE
1226         $RUNAS multiop ${TESTFILE} oO_WRONLY:O_APPEND:O_DIRECT:w$((4096 * 4096))c && \
1227             quota_error u root "unexpected: user(quota_usr) write a file successfully!"
1228
1229         # trigger the llog
1230         chmod 777 $DIR
1231         for i in `seq 1 10`; do $RUNAS touch ${TESTFILE}a_$i; done
1232         for i in `seq 1 10`; do $RUNAS rm -f ${TESTFILE}a_$i; done
1233
1234         # do the check
1235         dmesg | tail | grep "\-122" |grep llog_obd_origin_add && error "err -122 not found in dmesg"
1236         resetquota -u root
1237         #check_if_quota_zero u root
1238
1239         # clean
1240         unlinkmany ${TESTFILE} 15
1241         rm -f $TESTFILE
1242         sync; sleep 3; sync;
1243 }
1244 run_test_with_stat 14a "test setting quota on root ==="
1245
1246 test_15(){
1247         LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1248         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
1249
1250         wait_delete_completed
1251
1252         # force using the latest version in case 14b was omitted
1253         $LFS quotaoff -ug $DIR
1254         quota_set_version 3 2>&1 | grep "Invalid argument" && quota_set_version 2
1255         $LFS quotacheck -ug $DIR || error "quotacheck failed"
1256
1257         # test for user
1258         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1259         TOTAL_LIMIT=`getquota -u $TSTUSR global bhardlimit`
1260         [ $TOTAL_LIMIT -eq $LIMIT ] || error "  (user)total limits = $TOTAL_LIMIT; limit = $LIMIT, failed!"
1261         echo "  (user)total limits = $TOTAL_LIMIT; limit = $LIMIT, successful!"
1262         resetquota -u $TSTUSR
1263
1264         # test for group
1265         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1266         TOTAL_LIMIT=`getquota -g $TSTUSR global bhardlimit`
1267         [ $TOTAL_LIMIT -eq $LIMIT ] || error "  (group)total limits = $TOTAL_LIMIT; limit = $LIMIT, failed!"
1268         echo "  (group)total limits = $TOTAL_LIMIT; limit = $LIMIT, successful!"
1269
1270         resetquota -g $TSTUSR
1271
1272         # interop 18 <-> 20
1273         local lustre_version=$(get_lustre_version mds)
1274         if [[ $lustre_version != 1.8* ]]; then
1275                 echo mds running $lustre_version, skip quota v1 testing
1276                 return 0
1277         fi
1278
1279         quota_save_version "ug1"
1280
1281         echo "Testing that >4GB quota limits fail on volume with quota v1"
1282         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR && error "no error from setquota, but should have failed"
1283
1284         return 0
1285 }
1286 run_test_with_stat 15 "set block quota more than 4T ==="
1287
1288 # $1=u/g $2=with qunit adjust or not
1289 test_16_tub() {
1290         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) * 4))
1291         TESTFILE="$DIR/$tdir/$tfile"
1292         mkdir -p $DIR/$tdir
1293
1294         wait_delete_completed
1295
1296         echo "  User quota (limit: $LIMIT kbytes)"
1297         if [ $1 == "u" ]; then
1298             $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1299             quota_show_check b u $TSTUSR
1300         else
1301             $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1302             quota_show_check b g $TSTUSR
1303         fi
1304
1305         $LFS setstripe $TESTFILE -c 1
1306         chown $TSTUSR.$TSTUSR $TESTFILE
1307
1308         echo "    Write ..."
1309         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$((BUNIT_SZ * 4)) || \
1310             quota_error a $TSTUSR "(usr) write failure, but expect success"
1311         echo "    Done"
1312         echo "    Write out of block quota ..."
1313         # this time maybe cache write,  ignore it's failure
1314         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ seek=$((BUNIT_SZ * 4)) || true
1315         # flush cache, ensure noquota flag is setted on client
1316         cancel_lru_locks osc
1317         if [ $2 -eq 1 ]; then
1318             $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ seek=$((BUNIT_SZ * 4)) || \
1319                 quota_error a $TSTUSR "(write failure, but expect success"
1320         else
1321             $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ seek=$((BUNIT_SZ * 4)) && \
1322                 quota_error a $TSTUSR "(write success, but expect EDQUOT"
1323         fi
1324
1325         rm -f $TESTFILE
1326         sync; sleep 3; sync;
1327         resetquota -$1 $TSTUSR
1328 }
1329
1330 # test without adjusting qunit
1331 test_16 () {
1332         # interop 18 <-> 20
1333         # 2.0 version does not support WITHOUT_CHANGE_QS, so such test is obsolete
1334         local lustre_version=$(get_lustre_version mds)
1335         if [[ $lustre_version != 1.8* ]]; then
1336                 skip mds running $lustre_version, WITHOUT_CHANGE_QS is not supported
1337                 return 0
1338         fi
1339         
1340         set_blk_tunesz $((BUNIT_SZ * 2))
1341         set_blk_unitsz $((BUNIT_SZ * 4))
1342         for i in u g; do
1343             for j in 0 1; do
1344                 # define OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS    0xA01
1345                 echo " grp/usr: $i, adjust qunit: $j"
1346                 echo "-------------------------------"
1347                 [ $j -eq 1 ] && lustre_fail mds_ost 0
1348                 [ $j -eq 0 ] && lustre_fail mds_ost 0xA01
1349                 test_16_tub $i $j
1350             done
1351         done
1352         set_blk_unitsz $((128 * 1024))
1353         set_blk_tunesz $((128 * 1024 / 2))
1354 }
1355 run_test_with_stat 16 "test without adjusting qunit"
1356
1357 # run for fixing bug14526, failed returned quota reqs shouldn't ruin lustre.
1358 test_17() {
1359         set_blk_tunesz 512
1360         set_blk_unitsz 1024
1361
1362         wait_delete_completed
1363
1364         #define OBD_FAIL_QUOTA_RET_QDATA | OBD_FAIL_ONCE
1365         lustre_fail ost 0x80000A02
1366
1367         TESTFILE="$DIR/$tdir/$tfile-a"
1368         TESTFILE2="$DIR/$tdir/$tfile-b"
1369         mkdir -p $DIR/$tdir
1370
1371         BLK_LIMIT=$((100 * 1024)) # 100M
1372
1373         log "  Set enough high limit(block:$BLK_LIMIT) for user: $TSTUSR"
1374         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1375         log "  Set enough high limit(block:$BLK_LIMIT) for group: $TSTUSR"
1376         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1377
1378         quota_show_check b u $TSTUSR
1379         quota_show_check b g $TSTUSR
1380
1381         touch $TESTFILE
1382         chown $TSTUSR.$TSTUSR $TESTFILE
1383         touch $TESTFILE2
1384         chown $TSTUSR.$TSTUSR $TESTFILE2
1385
1386         log "    Write the test file1 ..."
1387         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$BLK_SZ count=$(( 10 * 1024 )) \
1388             || quota_error a $TSTUSR "write 10M file failure"
1389
1390         $SHOW_QUOTA_USER
1391         $SHOW_QUOTA_GROUP
1392
1393         log "    write the test file2 ..."
1394         $RUNAS dd if=/dev/zero of=$TESTFILE2  bs=$BLK_SZ count=$(( 10 * 1024 )) \
1395             || quota_error a $TSTUSR "write 10M file failure"
1396
1397         $SHOW_QUOTA_USER
1398         $SHOW_QUOTA_GROUP
1399
1400         rm -f $TESTFILE $TESTFILE2
1401         RC=$?
1402         sync; sleep 3; sync;
1403
1404         # make qd_count 64 bit
1405         lustre_fail ost 0
1406
1407         set_blk_unitsz $((128 * 1024))
1408         set_blk_tunesz $((128 * 1024 / 2))
1409
1410         resetquota -u $TSTUSR
1411         resetquota -g $TSTUSR
1412
1413         return $RC
1414 }
1415 run_test_with_stat 17 "run for fixing bug14526 ==========="
1416
1417 # test when mds takes a long time to handle a quota req so that
1418 # the ost has dropped it, the ost still could work well b=14840
1419 test_18() {
1420         LIMIT=$((100 * 1024 * 1024)) # 100G
1421         TESTFILE="$DIR/$tdir/$tfile"
1422         mkdir -p $DIR/$tdir
1423
1424         wait_delete_completed
1425
1426         set_blk_tunesz 512
1427         set_blk_unitsz 1024
1428
1429         log "   User quota (limit: $LIMIT kbytes)"
1430         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1431         quota_show_check b u $TSTUSR
1432
1433         $LFS setstripe $TESTFILE -i 0 -c 1
1434         chown $TSTUSR.$TSTUSR $TESTFILE
1435
1436         #define OBD_FAIL_MDS_BLOCK_QUOTA_REQ      0x13c
1437         lustre_fail mds 0x13c
1438
1439         log "   step1: write 100M block ..."
1440         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$((1024 * 100)) &
1441         DDPID=$!
1442
1443         sleep 5
1444         lustre_fail mds 0
1445
1446         echo  "   step2: testing ......"
1447         count=0
1448         if at_is_enabled; then
1449             timeout=$(at_max_get mds)
1450         else
1451             timeout=$(lctl get_param -n timeout)
1452         fi
1453         while [ true ]; do
1454             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1455             count=$[count+1]
1456             if [ $count -gt $((4 * $timeout)) ]; then
1457                 quota_error u $TSTUSR "count=$count dd should be finished!"
1458             fi
1459             sleep 1
1460         done
1461         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1462         sync
1463         cancel_lru_locks mdc
1464         cancel_lru_locks osc
1465
1466         testfile_size=$(stat -c %s $TESTFILE)
1467         [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] && \
1468             quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)), got ${testfile_size}. Verifying file failed!"
1469         $SHOW_QUOTA_USER
1470         rm -f $TESTFILE
1471         sync
1472
1473         resetquota -u $TSTUSR
1474         set_blk_unitsz $((128 * 1024))
1475         set_blk_tunesz $((128 * 1024 / 2))
1476 }
1477 run_test_with_stat 18 "run for fixing bug14840 ==========="
1478
1479 # test when mds drops a quota req, the ost still could work well b=14840
1480 test_18a() {
1481         LIMIT=$((100 * 1024 * 1024)) # 100G
1482         TESTFILE="$DIR/$tdir/$tfile-a"
1483         mkdir -p $DIR/$tdir
1484
1485         wait_delete_completed
1486
1487         set_blk_tunesz 512
1488         set_blk_unitsz 1024
1489
1490         log "   User quota (limit: $LIMIT kbytes)"
1491         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1492         quota_show_check b u $TSTUSR
1493
1494         $LFS setstripe $TESTFILE -i 0 -c 1
1495         chown $TSTUSR.$TSTUSR $TESTFILE
1496
1497         #define OBD_FAIL_MDS_DROP_QUOTA_REQ | OBD_FAIL_ONCE   0x8000013d
1498         lustre_fail mds 0x8000013d
1499
1500         log "   step1: write 100M block ..."
1501         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$((1024 * 100)) &
1502         DDPID=$!
1503
1504         echo  "   step2: testing ......"
1505         count=0
1506         if at_is_enabled; then
1507             timeout=$(at_max_get mds)
1508         else
1509             timeout=$(lctl get_param -n timeout)
1510         fi
1511         while [ true ]; do
1512             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1513             count=$[count+1]
1514             if [ $count -gt $((3 * $timeout)) ]; then
1515                 lustre_fail mds 0
1516                 quota_error u $TSTUSR "count=$count dd should be finished!"
1517             fi
1518             sleep 1
1519         done
1520         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1521
1522         lustre_fail mds 0
1523         rm -f $TESTFILE
1524         sync
1525
1526         resetquota -u $TSTUSR
1527         set_blk_unitsz $((128 * 1024))
1528         set_blk_tunesz $((128 * 1024 / 2))
1529 }
1530 run_test_with_stat 18a "run for fixing bug14840 ==========="
1531
1532 # test when mds do failover, the ost still could work well without trigger
1533 # watchdog b=14840
1534 test_18bc_sub() {
1535         type=$1
1536
1537         LIMIT=$(((100 + $OSTCOUNT * 3) * 1024))
1538         TESTFILE="$DIR/$tdir/$tfile"
1539         mkdir -p $DIR/$tdir
1540
1541         wait_delete_completed
1542
1543         set_blk_tunesz 512
1544         set_blk_unitsz 1024
1545
1546         log "   User quota (limit: $LIMIT kbytes)"
1547         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1548         quota_show_check b u $TSTUSR
1549
1550         $LFS setstripe $TESTFILE -i 0 -c 1
1551         chown $TSTUSR.$TSTUSR $TESTFILE
1552
1553         timeout=$(sysctl -n lustre.timeout)
1554
1555         if [ $type = "directio" ]; then
1556             log "   write 100M block(directio) ..."
1557             $RUNAS $DIRECTIO write $TESTFILE 0 100 $((BLK_SZ * 1024)) &
1558         else
1559             log "   write 100M block(normal) ..."
1560             $RUNAS dd if=/dev/zero of=$TESTFILE bs=$((BLK_SZ * 1024)) count=100 &
1561         fi
1562
1563         DDPID=$!
1564         do_facet mds "$LCTL conf_param ${FSNAME}-MDT*.$(get_md_name).quota_type=ug"
1565
1566         replay_barrier mds
1567
1568         log "failing mds for $((2 * timeout)) seconds"
1569         fail mds $((2 * timeout))
1570
1571         # check if quotaon successful
1572         $LFS quota -u $TSTUSR $MOUNT 2>&1 | grep -q "quotas are not enabled"
1573         if [ $? -eq 0 ]; then
1574             rm -rf $TESTFILE
1575             error "quotaon failed!"
1576             return
1577         fi
1578
1579         count=0
1580         if at_is_enabled; then
1581             timeout=$(at_max_get mds)
1582         else
1583             timeout=$(lctl get_param -n timeout)
1584         fi
1585         while [ true ]; do
1586             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1587             if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1588                 log "it took $count second"
1589             fi
1590             sleep 1
1591         done
1592         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1593         sync
1594         cancel_lru_locks mdc
1595         cancel_lru_locks osc
1596         $SHOW_QUOTA_USER
1597
1598         resetquota -u $TSTUSR
1599         set_blk_unitsz $((128 * 1024))
1600         set_blk_tunesz $((128 * 1024 / 2))
1601         testfile_size=$(stat -c %s $TESTFILE)
1602         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1603             rm -f $TESTFILE
1604             quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)), got ${testfile_size}. Verifying file failed!"
1605         fi
1606         rm -f $TESTFILE
1607 }
1608
1609 # test when mds does failover, the ost still could work well
1610 # this test shouldn't trigger watchdog b=14840
1611 test_18b() {
1612         test_18bc_sub normal
1613         test_18bc_sub directio
1614         # check if watchdog is triggered
1615         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1616         watchdog=`awk '/test 18b/ {start = 1;}
1617                        /Service thread pid/ && /was inactive/{
1618                                if (start) {
1619                                        print;
1620                                }
1621                        }' $TMP/lustre-log-${TESTNAME}.log`
1622         [ `echo "$watchdog" | wc -l` -ge 3 ] && error "$watchdog"
1623         rm -f $TMP/lustre-log-${TESTNAME}.log
1624 }
1625 run_test_with_stat 18b "run for fixing bug14840(mds failover, no watchdog) ==========="
1626
1627 # test when mds does failover, the ost still could work well
1628 # this test will prevent OST_DISCONNET from happening b=14840
1629 test_18c() {
1630         # define OBD_FAIL_OST_DISCONNECT_NET 0x202(disable ost_disconnect for osts)
1631         lustre_fail ost  0x202
1632         test_18bc_sub normal
1633         test_18bc_sub directio
1634         lustre_fail ost  0
1635 }
1636 run_test_with_stat 18c "run for fixing bug14840(mds failover, OST_DISCONNECT is disabled) ==========="
1637
1638 run_to_block_limit() {
1639         local LIMIT=$((($OSTCOUNT + 1) * $BUNIT_SZ))
1640         local TESTFILE=$1
1641         wait_delete_completed
1642
1643         # set 1 Mb quota unit size
1644         set_blk_tunesz 512
1645         set_blk_unitsz 1024
1646
1647         # bind file to a single OST
1648         $LFS setstripe -c 1 $TESTFILE
1649         chown $TSTUSR.$TSTUSR $TESTFILE
1650
1651         echo "  User quota (limit: $LIMIT kbytes)"
1652         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1653         quota_show_check b u $TSTUSR
1654         echo "  Updating quota limits"
1655         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1656         quota_show_check b u $TSTUSR
1657
1658         RUNDD="$RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ"
1659         $RUNDD count=$BUNIT_SZ || quota_error u $TSTUSR "(usr) write failure, but expect success"
1660         # for now page cache of TESTFILE may still be dirty,
1661         # let's push it to the corresponding OST, this will also
1662         # cache NOQUOTA on the client from OST's reply
1663         cancel_lru_locks osc
1664         $RUNDD seek=$BUNIT_SZ && quota_error u $TSTUSR "(usr) write success, should be EDQUOT"
1665 }
1666
1667 test_19() {
1668         # 1 Mb bunit per each MDS/OSS
1669         local TESTFILE="$DIR/$tdir/$tfile"
1670         mkdir -p $DIR/$tdir
1671
1672         run_to_block_limit $TESTFILE
1673         $SHOW_QUOTA_USER
1674
1675         # cleanup
1676         rm -f $TESTFILE
1677         resetquota -u $TSTUSR
1678
1679         set_blk_unitsz $((128 * 1024))
1680         set_blk_tunesz $((128 * 1024 / 2))
1681
1682 }
1683 run_test_with_stat 19 "test if administrative limits updates do not zero operational limits (14790) ==="
1684
1685 test_20()
1686 {
1687         LSTR=(1t 2g 3m 4k) # limits strings
1688         LVAL=($[1*1024*1024*1024] $[2*1024*1024] $[3*1024*1024] $[4*1024]) # limits values
1689
1690         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1691                                  $MOUNT || error "could not set quota limits"
1692
1693         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1694                                  --inode-softlimit ${LSTR[2]} \
1695                                  --inode-hardlimit ${LSTR[3]} \
1696                                  $MOUNT || error "could not set quota limits"
1697
1698         [ "`getquota -u $TSTUSR global bsoftlimit`" = "${LVAL[0]}" ] || error "bsoftlimit was not set properly"
1699         [ "`getquota -u $TSTUSR global bhardlimit`" = "${LVAL[1]}" ] || error "bhardlimit was not set properly"
1700         [ "`getquota -u $TSTUSR global isoftlimit`" = "${LVAL[2]}" ] || error "isoftlimit was not set properly"
1701         [ "`getquota -u $TSTUSR global ihardlimit`" = "${LVAL[3]}" ] || error "ihardlimit was not set properly"
1702
1703         resetquota -u $TSTUSR
1704 }
1705 run_test_with_stat 20 "test if setquota specifiers work properly (15754)"
1706
1707 test_21_sub() {
1708         local testfile=$1
1709         local blk_number=$2
1710         local seconds=$3
1711
1712         time=$(($(date +%s) + seconds))
1713         while [ $(date +%s) -lt $time ]; do
1714             $RUNAS dd if=/dev/zero of=$testfile  bs=$BLK_SZ count=$blk_number > /dev/null 2>&1
1715             rm -f $testfile
1716         done
1717 }
1718
1719 # run for fixing bug16053, setquota shouldn't fail when writing and
1720 # deleting are happening
1721 test_21() {
1722         set_blk_tunesz 512
1723         set_blk_unitsz 1024
1724
1725         wait_delete_completed
1726
1727         TESTFILE="$DIR/$tdir/$tfile"
1728
1729         BLK_LIMIT=$((10 * 1024 * 1024)) # 10G
1730         FILE_LIMIT=1000000
1731
1732         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for user: $TSTUSR"
1733         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $MOUNT
1734         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for group: $TSTUSR"
1735         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $MOUNT
1736
1737         # repeat writing on a 1M file
1738         test_21_sub ${TESTFILE}_1 1024 30 &
1739         DDPID1=$!
1740         # repeat writing on a 128M file
1741         test_21_sub ${TESTFILE}_2 $((1024 * 128)) 30 &
1742         DDPID2=$!
1743
1744         time=$(($(date +%s) + 30))
1745         i=1
1746         while [ $(date +%s) -lt $time ]; do
1747             log "  Set quota for $i times"
1748             $LFS setquota -u $TSTUSR -b 0 -B $((BLK_LIMIT + 1024 * i)) -i 0 -I $((FILE_LIMIT + i)) $MOUNT
1749             $LFS setquota -g $TSTUSR -b 0 -B $((BLK_LIMIT + 1024 * i)) -i 0 -I $((FILE_LIMIT + i)) $MOUNT
1750             i=$((i+1))
1751             sleep 1
1752         done
1753
1754         count=0
1755         while [ true ]; do
1756             if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1757             count=$[count+1]
1758             if [ $count -gt 60 ]; then
1759                 quota_error a $TSTUSR "dd should be finished!"
1760             fi
1761             sleep 1
1762         done
1763         echo "(dd_pid=$DDPID1, time=$count)successful"
1764
1765         count=0
1766         while [ true ]; do
1767             if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1768             count=$[count+1]
1769             if [ $count -gt 60 ]; then
1770                 quota_error a $TSTUSR "dd should be finished!"
1771             fi
1772             sleep 1
1773         done
1774         echo "(dd_pid=$DDPID2, time=$count)successful"
1775
1776         set_blk_unitsz $((128 * 1024))
1777         set_blk_tunesz $((128 * 1024 / 2))
1778         resetquota -u $TSTUSR
1779         resetquota -g $TSTUSR
1780
1781         return $RC
1782 }
1783 run_test_with_stat 21 "run for fixing bug16053 ==========="
1784
1785 test_22() {
1786         local lustre_version=$(get_lustre_version mds)
1787         if [[ $lustre_version != 1.8* ]] ; then
1788                 skip mds running $lustre_version, skip quota v1 testing
1789                 return
1790         fi
1791         quota_save_version "ug1"
1792
1793         stopall
1794         mount
1795         setupall
1796
1797         echo "checking parameters"
1798
1799         do_facet mds "lctl get_param mds.${FSNAME}-MDT*.quota_type" | grep "ug1" || error "admin failure"
1800         do_facet ost1 "lctl get_param obdfilter.*.quota_type" | grep "ug1" || error "op failure"
1801
1802         run_test 0 "reboot lustre"
1803 }
1804 run_test_with_stat 22 "test if quota_type saved as permanent parameter ===="
1805
1806 test_23_sub() {
1807         mkdir -p $DIR/$tdir
1808         chmod 0777 $DIR/$tdir
1809         TESTFILE="$DIR/$tdir/$tfile-0"
1810         rm -f $TESTFILE
1811         local bs_unit=$((1024*1024))
1812         LIMIT=$1
1813
1814         wait_delete_completed
1815
1816         # test for user
1817         log "  User quota (limit: $LIMIT kbytes)"
1818         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1819         sleep 3
1820         quota_show_check b u $TSTUSR
1821
1822         $LFS setstripe $TESTFILE -c 1
1823         chown $TSTUSR.$TSTUSR $TESTFILE
1824
1825         log "    Step1: trigger quota with 0_DIRECT"
1826         log "      Write half of file"
1827         $RUNAS $DIRECTIO write $TESTFILE 0 $(($LIMIT/1024/2)) $bs_unit || quota_error u $TSTUSR "(1) write failure, but expect success: $LIMIT"
1828         log "      Write out of block quota ..."
1829         $RUNAS $DIRECTIO write $TESTFILE $(($LIMIT/1024/2)) $(($LIMIT/1024/2)) $bs_unit && quota_error u $TSTUSR "(2) write success, but expect EDQUOT: $LIMIT"
1830         log "    Step1: done"
1831
1832         log "    Step2: rewrite should succeed"
1833         $RUNAS $DIRECTIO write $TESTFILE 0 1 $bs_unit || quota_error u $TSTUSR "(3) write failure, but expect success: $LIMIT"
1834         log "    Step2: done"
1835
1836         rm -f $TESTFILE
1837         wait_delete_completed
1838         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
1839         OST0_QUOTA_USED=`getquota -u $TSTUSR $OST0_UUID curspace`
1840         echo $OST0_QUOTA_USED
1841         [ $OST0_QUOTA_USED -ne 0 ] && \
1842             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "quota deleted isn't released")
1843         $SHOW_QUOTA_USER
1844         resetquota -u $TSTUSR
1845 }
1846
1847 test_23() {
1848         log "run for $((OSTCOUNT * 3))MB test file"
1849         test_23_sub $((OSTCOUNT * 3 * 1024))
1850
1851         OST0_MIN=120000
1852         check_whether_skip && return 0
1853         log "run for $((OSTCOUNT * 30))MB test file"
1854         test_23_sub $((OSTCOUNT * 30 * 1024))
1855 }
1856 run_test_with_stat 23 "run for fixing bug16125 ==========="
1857
1858 test_24() {
1859         local TESTFILE="$DIR/$tdir/$tfile"
1860         mkdir -p $DIR/$tdir
1861
1862         run_to_block_limit $TESTFILE
1863         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1864
1865         # cleanup
1866         rm -f $TESTFILE
1867         resetquota -u $TSTUSR
1868
1869         set_blk_unitsz $((128 * 1024))
1870         set_blk_tunesz $((128 * 1024 / 2))
1871 }
1872 run_test_with_stat 24 "test if lfs draws an asterix when limit is reached (16646) ==========="
1873
1874 show_quota() {
1875         if [ $1 = "-u" ]; then
1876                 if [ $2 = "$TSTUSR" ]; then
1877                         $SHOW_QUOTA_USER
1878                 else
1879                         $SHOW_QUOTA_USER2
1880                 fi
1881         else
1882                 if [ $2 = "$TSTUSR" ]; then
1883                         $SHOW_QUOTA_GROUP
1884                 else
1885                         $SHOW_QUOTA_GROUP2
1886                 fi
1887         fi
1888 }
1889
1890 test_25_sub() {
1891         mkdir -p $DIR/$tdir
1892         chmod 0777 $DIR/$tdir
1893         TESTFILE="$DIR/$tdir/$tfile-0"
1894         rm -f $TESTFILE
1895         LIMIT=$(( $BUNIT_SZ * ($OSTCOUNT + 1) + 4096 ))
1896
1897         wait_delete_completed
1898
1899         # set quota for $TSTUSR
1900         log "setquota for $TSTUSR"
1901         $LFS setquota $1 $TSTUSR -b $LIMIT -B $LIMIT -i 10 -I 10 $DIR
1902         sleep 3
1903         if [ "$1" == "-u" ]; then
1904                 quota_show_check a u $TSTUSR
1905         else
1906                 quota_show_check a g $TSTUSR
1907         fi
1908
1909         # set quota for $TSTUSR2
1910         log "setquota for $TSTUSR2"
1911         $LFS setquota $1 $TSTUSR2 -b $LIMIT -B $LIMIT -i 10 -I 10 $DIR
1912         sleep 3
1913         if [ "$1" == "-u" ]; then
1914                 quota_show_check a u $TSTUSR2
1915         else
1916                 quota_show_check a g $TSTUSR2
1917         fi
1918
1919         # set stripe index to 0
1920         log "setstripe for $DIR/$tdir to 0"
1921         $LFS setstripe $DIR/$tdir -c 1 -i 0
1922
1923         # interop 18 <-> 20
1924         local dev=$(get_mds_mdt_device_proc_path)
1925         MDS_UUID=`do_facet mds $LCTL dl | grep -m1 " $dev " | awk '{print $((NF-1))}'`
1926         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
1927
1928         MDS_QUOTA_USED_OLD=`getquota $1 $TSTUSR $MDS_UUID curinodes`
1929         OST0_QUOTA_USED_OLD=`getquota $1 $TSTUSR $OST0_UUID curspace`
1930         MDS_QUOTA_USED2_OLD=`getquota $1 $TSTUSR2 $MDS_UUID curinodes`
1931         OST0_QUOTA_USED2_OLD=`getquota $1 $TSTUSR2 $OST0_UUID curspace`
1932
1933         # TSTUSR write 4M
1934         log "$TSTUSR write 4M to $TESTFILE"
1935         $RUNAS dd if=/dev/zero of=$TESTFILE bs=4K count=1K || quota_error a $TSTUSR "dd failed"
1936         sync
1937         show_quota $1 $TSTUSR
1938         show_quota $1 $TSTUSR2
1939         MDS_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $MDS_UUID curinodes`
1940         [ $MDS_QUOTA_USED_NEW -ne $((MDS_QUOTA_USED_OLD + 1)) ] && \
1941                 quota_error a $TSTUSR "$TSTUSR inode quota usage error: [$MDS_QUOTA_USED_OLD|$MDS_QUOTA_USED_NEW]"
1942         OST0_QUOTA_USED_NEW=`getquota   $1 $TSTUSR $OST0_UUID curspace`
1943         OST0_QUOTA_USED_DELTA=$((OST0_QUOTA_USED_NEW - OST0_QUOTA_USED_OLD))
1944         [ $OST0_QUOTA_USED_DELTA -lt 4096 ] && \
1945                 quota_error a $TSTUSR "$TSTUSR block quota usage error: [$OST0_QUOTA_USED_OLD|$OST0_QUOTA_USED_NEW]"
1946
1947         # chown/chgrp from $TSTUSR to $TSTUSR2
1948         if [ $1 = "-u" ]; then
1949                 log "chown from $TSTUSR to $TSTUSR2"
1950                 chown $TSTUSR2 $TESTFILE || quota_error u $TSTUSR2 "chown failed"
1951         else
1952                 log "chgrp from $TSTUSR to $TSTUSR2"
1953                 chgrp $TSTUSR2 $TESTFILE || quota_error g $TSTUSR2 "chgrp failed"
1954         fi
1955         sync
1956         show_quota $1 $TSTUSR
1957         show_quota $1 $TSTUSR2
1958         MDS_QUOTA_USED2_NEW=`getquota $1 $TSTUSR2 $MDS_UUID curinodes`
1959         [ $MDS_QUOTA_USED2_NEW -ne $((MDS_QUOTA_USED2_OLD + 1)) ] && \
1960                 quota_error a $TSTUSR2 "$TSTUSR2 inode quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$MDS_QUOTA_USED2_OLD|$MDS_QUOTA_USED2_NEW]"
1961         OST0_QUOTA_USED2_NEW=`getquota  $1 $TSTUSR2 $OST0_UUID curspace`
1962         # when chown, the quota on ost could be displayed out of quota temporarily. Delete the '*' in this situation. b=20433
1963         OST0_QUOTA_USED2_NEW=${OST0_QUOTA_USED2_NEW%\*}
1964         OST0_QUOTA_USED2_DELTA=$((OST0_QUOTA_USED2_NEW - OST0_QUOTA_USED2_OLD))
1965         [ $OST0_QUOTA_USED2_DELTA -ne $OST0_QUOTA_USED_DELTA ] && \
1966                 quota_error a $TSTUSR2 "$TSTUSR2 block quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$OST0_QUOTA_USED2_OLD|$OST0_QUOTA_USED2_NEW]"
1967         MDS_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $MDS_UUID curinodes`
1968         [ $MDS_QUOTA_USED_NEW -ne $MDS_QUOTA_USED_OLD ] && \
1969                 quota_error a $TSTUSR "$TSTUSR inode quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$MDS_QUOTA_USED_OLD|$MDS_QUOTA_USED_NEW]"
1970         OST0_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $OST0_UUID curspace`
1971         [ $OST0_QUOTA_USED_NEW -ne $OST0_QUOTA_USED_OLD ] && \
1972                 quota_error a $TSTUSR "$TSTUSR block quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$OST0_QUOTA_USED_OLD|$OST0_QUOTA_USED_NEW]"
1973
1974         rm -f $TESTFILE
1975         wait_delete_completed
1976         resetquota $1 $TSTUSR
1977         resetquota $1 $TSTUSR2
1978 }
1979
1980 test_25() {
1981         log "run for chown case"
1982         test_25_sub -u
1983
1984         log "run for chgrp case"
1985         test_25_sub -g
1986 }
1987 run_test_with_stat 25 "test whether quota usage is transfered when chown/chgrp (18081) ==========="
1988
1989 test_26() {
1990         mkdir -p $DIR/$tdir
1991         chmod 0777 $DIR/$tdir
1992         TESTFILE="$DIR/$tdir/$tfile-0"
1993         TESTFILE2="$DIR/$tdir/$tfile-1"
1994         set_blk_tunesz 512
1995         set_blk_unitsz 1024
1996
1997         wait_delete_completed
1998
1999         # every quota slave gets 20MB
2000         b_limit=$((OSTCOUNT * 20 * 1024))
2001         log "limit: ${b_limit}KB"
2002         $LFS setquota -u $TSTUSR -b 0 -B $b_limit -i 0 -I 0 $DIR
2003         sleep 3
2004         quota_show_check b u $TSTUSR
2005
2006         $LFS setstripe $TESTFILE  -c 1 -i 0
2007         $LFS setstripe $TESTFILE2 -c 1 -i 0
2008         chown $TSTUSR.$TSTUSR $TESTFILE
2009         chown $TSTUSR.$TSTUSR $TESTFILE2
2010
2011         #define OBD_FAIL_QUOTA_DELAY_REL         0xA03
2012         lustre_fail ost 0xA03
2013
2014         log "    Write the first file..."
2015         $RUNAS $DIRECTIO write $TESTFILE 0 10 $((BLK_SZ * 1024)) || quota_error u $TSTUSR "write failure, but expect success"
2016         log "    Delete the first file..."
2017         rm -f $TESTFILE
2018
2019
2020         wait_delete_completed
2021
2022         log "    Write the second file..."
2023         $RUNAS $DIRECTIO write $TESTFILE2 0 10 $((BLK_SZ * 1024)) || quota_error u $TSTUSR "write failure, but expect success"
2024         log "    Delete the second file..."
2025         rm -f $TESTFILE2
2026
2027         lustre_fail ost 0
2028         set_blk_unitsz $((128 * 1024))
2029         set_blk_tunesz $((128 * 1024 / 2))
2030         resetquota -u $TSTUSR
2031 }
2032 run_test_with_stat 26 "test for false quota error(bz18491) ======================================"
2033
2034 test_27a() {
2035         $LFS quota $TSTUSR $DIR && error "lfs succeeded with no type, but should have failed"
2036         $LFS setquota $TSTUSR $DIR && error "lfs succeeded with no type, but should have failed"
2037         return 0
2038 }
2039 run_test_with_stat 27a "lfs quota/setquota should handle wrong arguments (19612) ================="
2040
2041 test_27b() {
2042         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR || \
2043                 error "lfs setquota failed with uid argument"
2044         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR || \
2045                 error "lfs stequota failed with gid argument"
2046         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2047         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2048         resetquota -u $TSTUSR
2049         resetquota -g $TSTUSR
2050         return 0
2051 }
2052 run_test 27b "lfs quota/setquota should handle user/group ID (20200) ================="
2053
2054 test_28() {
2055         BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
2056         echo "Step 1: set enough high limit for user [$TSTUSR:$BLK_LIMIT]"
2057         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
2058         $SHOW_QUOTA_USER
2059
2060         echo "Step 2: reset system ..."
2061         cleanup_and_setup_lustre
2062         test_0
2063
2064         echo "Step 3: change qunit for user [$TSTUSR:512:1024]"
2065         set_blk_tunesz 512
2066         set_blk_unitsz 1024
2067
2068         wait_delete_completed
2069
2070         #define OBD_FAIL_QUOTA_RET_QDATA | OBD_FAIL_ONCE
2071         lustre_fail ost 0x80000A02
2072
2073         TESTFILE="$DIR/$tdir/$tfile"
2074         mkdir -p $DIR/$tdir
2075
2076         BLK_LIMIT=$((100 * 1024)) # 100M
2077         echo "Step 4: set enough high limit for user [$TSTUSR:$BLK_LIMIT]"
2078         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
2079         $SHOW_QUOTA_USER
2080
2081         touch $TESTFILE
2082         chown $TSTUSR.$TSTUSR $TESTFILE
2083
2084         echo "Step 5: write the test file1 [10M] ..."
2085         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$BLK_SZ count=$(( 10 * 1024 )) \
2086             || quota_error a $TSTUSR "write 10M file failure"
2087         $SHOW_QUOTA_USER
2088
2089         rm -f $TESTFILE
2090         sync; sleep 3; sync;
2091
2092         # make qd_count 64 bit
2093         lustre_fail ost 0
2094
2095         set_blk_unitsz $((128 * 1024))
2096         set_blk_tunesz $((128 * 1024 / 2))
2097
2098         resetquota -u $TSTUSR
2099 }
2100 run_test_with_stat 28 "test for consistency for qunit when setquota (18574) ==========="
2101
2102 at_max_enforce()
2103 {
2104         local timeout="$1"
2105         # flush AT history, enforce at_max immediately
2106         do_facet mgs "lctl conf_param $FSNAME.sys.at_max=$timeout"
2107         cleanupall
2108         setupall
2109         test_0
2110 }
2111
2112 test_29()
2113 {
2114         local BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
2115         local newtimeo=10 # the default ptlrpc AT value
2116         local oldtimeo
2117         local pid
2118
2119         if at_is_enabled; then
2120                 oldtimeo=$(at_max_get client)
2121                 at_max_enforce $newtimeo
2122         else
2123                 oldtimeo=$(lctl get_param -n timeout)
2124                 lctl set_param timeout=$newtimeo
2125         fi
2126
2127         #define OBD_FAIL_MDS_QUOTACTL_NET 0x12e
2128         lustre_fail mds 0x12e
2129
2130         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR & pid=$!
2131
2132         # 1.25 * at_max + 5 + net_latency
2133         echo "sleeping for $((newtimeo * 9 / 4 + 5)) seconds"
2134         sleep $((newtimeo * 9 / 4 + 5))
2135         ps -p $pid && error "lfs hadn't finished by timeout"
2136         wait $pid && error "succeeded, but should have failed"
2137
2138         lustre_fail mds 0
2139
2140         if at_is_enabled; then
2141                 at_max_enforce $oldtimeo
2142         else
2143                 lctl set_param timeout=$oldtimeo
2144         fi
2145
2146         resetquota -u $TSTUSR
2147 }
2148 run_test_with_stat 29 "unhandled quotactls must not hang lustre client (19778) ========"
2149
2150 test_30()
2151 {
2152         local output
2153         local LIMIT=1024
2154         local TESTFILE="$DIR/$tdir/$tfile"
2155         local GRACE=10
2156
2157         set_blk_tunesz 512
2158         set_blk_unitsz 1024
2159
2160         mkdir -p $DIR/$tdir
2161         chmod 0777 $DIR/$tdir
2162
2163         $LFS setstripe $TESTFILE -i 0 -c 1
2164         chown $TSTUSR.$TSTUSR $TESTFILE
2165
2166         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
2167         $LFS setquota -u $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
2168         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1024 count=$((LIMIT * 2)) || true
2169         cancel_lru_locks osc
2170         sleep $GRACE
2171         $LFS setquota -u $TSTUSR -B 0 $DIR
2172         # over-quota flag has not yet settled since we do not trigger async events
2173         # based on grace time period expiration
2174         $SHOW_QUOTA_USER
2175         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 || true
2176         cancel_lru_locks osc
2177         # now over-quota flag should be settled and further writes should fail
2178         $SHOW_QUOTA_USER
2179         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 && error "grace times were reset"
2180         rm -f $TESTFILE
2181         resetquota -u $TSTUSR
2182         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
2183
2184         set_blk_unitsz $((128 * 1024))
2185         set_blk_tunesz $((128 * 1024 / 2))
2186 }
2187 run_test_with_stat 30 "hard limit updates should not reset grace times ================"
2188
2189 # test duplicate quota releases b=18630
2190 test_31() {
2191         mkdir -p $DIR/$tdir
2192         chmod 0777 $DIR/$tdir
2193
2194         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) * 10)) # 10 bunits each sever
2195         TESTFILE="$DIR/$tdir/$tfile-0"
2196         TESTFILE2="$DIR/$tdir/$tfile-1"
2197
2198         wait_delete_completed
2199
2200         log "   User quota (limit: $LIMIT kbytes)"
2201         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
2202
2203         $LFS setstripe $TESTFILE -i 0 -c 1
2204         chown $TSTUSR.$TSTUSR $TESTFILE
2205         $LFS setstripe $TESTFILE2 -i 0 -c 1
2206         chown $TSTUSR.$TSTUSR $TESTFILE2
2207
2208         log "   step1: write out of block quota ..."
2209         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=5120
2210         $RUNAS dd if=/dev/zero of=$TESTFILE2 bs=$BLK_SZ count=5120
2211
2212         #define OBD_FAIL_QUOTA_DELAY_SD      0xA04
2213         #define OBD_FAIL_SOME        0x10000000 /* fail N times */
2214         lustre_fail ost $((0x00000A04 | 0x10000000)) 1
2215
2216         log "   step2: delete two files so that triggering duplicate quota release ..."
2217         rm -f $TESTFILE $TESTFILE2
2218         sync; sleep 5; sync      #  OBD_FAIL_QUOTA_DELAY_SD will delay for 5 seconds
2219         wait_delete_completed
2220
2221         log "   step3: verify if the ost failed"
2222         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2223         watchdog=`awk '/test 31/ {start = 1;}
2224                        /release quota error/ {
2225                                if (start) {
2226                                        print;
2227                                }
2228                        }' $TMP/lustre-log-${TESTNAME}.log`
2229         [ "$watchdog" ] && error "$watchdog"
2230         rm -f $TMP/lustre-log-${TESTNAME}.log
2231
2232         lustre_fail ost 0
2233         resetquota -u $TSTUSR
2234 }
2235 run_test_with_stat 31 "test duplicate quota releases ==="
2236
2237 # check hash_cur_bits
2238 check_quota_hash_cur_bits() {
2239         local bits=$1
2240
2241         # check quota_hash_cur_bits on all obdfilters
2242         for num in `seq $OSTCOUNT`; do
2243             cb=`do_facet ost$num "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2244             if [ $cb -gt $bits ]; then
2245                 echo "hash_lqs_cur_bits of ost$num is too large(cur_bits=$cb)"
2246                 return 1;
2247             fi
2248         done
2249         # check quota_hash_cur_bits on mds
2250         cb=`do_facet mds  "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2251         if [ $cb -gt $bits ]; then
2252             echo "hash_lqs_cur_bits of mds is too large(cur_bits=$cb)"
2253             return 1;
2254         fi
2255         return 0;
2256 }
2257
2258 # check lqs hash
2259 check_lqs_hash() {
2260         # check distribution of all obdfilters
2261         for num in `seq $OSTCOUNT`; do
2262             do_facet ost$num "lctl get_param obdfilter.${FSNAME}-OST*.hash_stats | grep LQS_HASH" | while read line; do
2263                 rehash_count=`echo $line | awk '{print $9}'`
2264                 if [ $rehash_count -eq 0 ]; then
2265                     echo -e "ost$num:\n $line"
2266                     error "Rehearsh didn't happen"
2267                 fi
2268             done
2269         done
2270         # check distribution of mds
2271         do_facet mds "lctl get_param mds.${FSNAME}-MDT*.hash_stats | grep LQS_HASH" | while read line; do
2272             rehash_count=`echo $line | awk '{print $9}'`
2273             if [ $rehash_count -eq 0 ]; then
2274                 echo -e "mdt:\n $line"
2275                 error "Rehearsh didn't happen"
2276             fi
2277         done
2278 }
2279
2280 test_32()
2281 {
2282         # reset system so that quota_hash_cur_bits==3
2283         echo "Reset system ..."
2284         local LMR_orig=$LOAD_MODULES_REMOTE
2285         LOAD_MODULES_REMOTE=true
2286         cleanup_and_setup_lustre
2287         LOAD_MODULES_REMOTE=$LMR_orig
2288
2289         for user in $SANITY_QUOTA_USERS; do
2290             check_runas_id_ret $user quota_usr "runas -u $user -g quota_usr" >/dev/null 2>/dev/null || \
2291                 missing_users="$missing_users $user"
2292         done
2293         [ -n "$missing_users" ] && { skip_env "the following users are missing: $missing_users" ; return 0 ; }
2294         check_quota_hash_cur_bits 3 || { skip_env "hash_lqs_cur_bits isn't set properly"; return 0;}
2295
2296         $LFS quotaoff -ug $DIR
2297         $LFS quotacheck -ug $DIR
2298
2299         for user in $SANITY_QUOTA_USERS; do
2300             $LFS setquota -u $user --block-hardlimit 1048576 $DIR
2301         done
2302
2303         check_lqs_hash
2304
2305         for user in $SANITY_QUOTA_USERS; do
2306             resetquota -u $user
2307         done
2308 }
2309 run_test 32 "check lqs hash(bug 21846) =========================================="
2310
2311 #
2312 # run 98 at the end because of reformatall
2313 #
2314
2315 test_98()
2316 {
2317         local num
2318         local user
2319         local missing_users=""
2320
2321         for user in $SANITY_QUOTA_USERS; do
2322                 check_runas_id_ret $user quota_usr "runas -u $user -g quota_usr" || \
2323                        missing_users="$missing_users $user"
2324         done
2325         [ -n "$missing_users" ] && { skip_env "different uid-s on client and servers, or missing users: $missing_users" ; return 0 ; }
2326
2327         cleanupall
2328         formatall
2329         setupall
2330         quota_set_version 1 # set version before the first quota check
2331         test_0
2332
2333         set_blk_tunesz 2
2334         set_blk_unitsz 4
2335
2336         num=8
2337         for user in $SANITY_QUOTA_USERS; do
2338                 $LFS setquota -u $user --block-hardlimit $((2 * num + 4 * OSTCOUNT)) $DIR || error "first phase setquota failure"
2339                 $LFS setstripe -c 1 $DIR/${tfile}${num} || error "single-stripe file creation failed"
2340                 chown $user $DIR/${tfile}${num} || error "chown has failed"
2341                 runas -u $user -g quota_usr multiop $DIR/${tfile}${num} oO_WRONLY:O_DIRECT:w$((num * 1024))c || \
2342                         error "unexpected error when writing the file for $user"
2343                 num=$((num + 8))
2344         done
2345
2346         $LFS quotaoff -ug $DIR
2347         quota_set_version 3 2>&1 | grep "Invalid argument" && quota_set_version 2
2348         $LFS quotaon -ug $DIR
2349
2350         num=8
2351         for user in $SANITY_QUOTA_USERS; do
2352                 runas -u $user -g quota_usr multiop $DIR/${tfile}${num} oO_WRONLY:O_DIRECT:O_APPEND:w$((num * 1024))c || \
2353                         error "unexpected error when appending the file for $user"
2354                 runas -u $user -g quota_usr multiop $DIR/${tfile}${num} oO_WRONLY:O_DIRECT:O_APPEND:w8192c && \
2355                         error "unexpected success when appending the file for $user"
2356                 $LFS setquota -u $user --block-hardlimit 0 $DIR || error "second phase setquota failure"
2357                 rm -rf $DIR/${tfile}${num}
2358                 num=$((num + 8))
2359         done
2360
2361         set_blk_unitsz $((128 * 1024))
2362         set_blk_tunesz $((128 * 1024 / 2))
2363 }
2364 run_test_with_stat 98 "quotaon autoconversion ==============================="
2365
2366 # turn off quota
2367 test_99()
2368 {
2369         $LFS quotaoff $DIR
2370         lctl set_param debug="-quota"
2371
2372         return 0
2373 }
2374 run_test_with_stat 99 "Quota off ==============================="
2375
2376 cd $ORIG_PWD
2377 complete $(basename $0) $SECONDS
2378 check_and_cleanup_lustre
2379 exit_status