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