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