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