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