Whamcloud - gitweb
LU-1842 quota: lfs quota commands
[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         $LFS quotacheck -ug $DIR
1258 }
1259 run_test_with_stat 15 "set block quota more than 4T ==="
1260
1261 # 2.0 version does not support WITHOUT_CHANGE_QS,
1262 # test_16 "test without adjusting qunit" is obsolete
1263
1264 # run for fixing bug14526, failed returned quota reqs shouldn't ruin lustre.
1265 test_17() {
1266         set_blk_tunesz 512
1267         set_blk_unitsz 1024
1268
1269         wait_delete_completed
1270
1271         #define OBD_FAIL_QUOTA_RET_QDATA | OBD_FAIL_ONCE
1272         lustre_fail ost 0x80000A02
1273
1274         TESTFILE="$DIR/$tdir/$tfile-a"
1275         TESTFILE2="$DIR/$tdir/$tfile-b"
1276         mkdir -p $DIR/$tdir
1277
1278         BLK_LIMIT=$((100 * 1024)) # 100M
1279
1280         log "  Set enough high limit(block:$BLK_LIMIT) for user: $TSTUSR"
1281         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1282         log "  Set enough high limit(block:$BLK_LIMIT) for group: $TSTUSR"
1283         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1284
1285         quota_show_check b u $TSTUSR
1286         quota_show_check b g $TSTUSR
1287
1288         touch $TESTFILE
1289         chown $TSTUSR.$TSTUSR $TESTFILE
1290         touch $TESTFILE2
1291         chown $TSTUSR.$TSTUSR $TESTFILE2
1292
1293         log "    Write the test file1 ..."
1294         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$BLK_SZ count=$(( 10 * 1024 )) \
1295             || quota_error a $TSTUSR "write 10M file failure"
1296
1297         $SHOW_QUOTA_USER
1298         $SHOW_QUOTA_GROUP
1299
1300         log "    write the test file2 ..."
1301         $RUNAS dd if=/dev/zero of=$TESTFILE2  bs=$BLK_SZ count=$(( 10 * 1024 )) \
1302             || quota_error a $TSTUSR "write 10M file failure"
1303
1304         $SHOW_QUOTA_USER
1305         $SHOW_QUOTA_GROUP
1306
1307         rm -f $TESTFILE $TESTFILE2
1308         RC=$?
1309         sync; sleep 3; sync;
1310
1311         # make qd_count 64 bit
1312         lustre_fail ost 0
1313
1314         set_blk_unitsz $((128 * 1024))
1315         set_blk_tunesz $((128 * 1024 / 2))
1316
1317         resetquota -u $TSTUSR
1318         resetquota -g $TSTUSR
1319
1320         return $RC
1321 }
1322 run_test_with_stat 17 "run for fixing bug14526 ==========="
1323
1324 # test when mds takes a long time to handle a quota req so that
1325 # the ost has dropped it, the ost still could work well b=14840
1326 test_18() {
1327         LIMIT=$((100 * 1024 * 1024)) # 100G
1328         TESTFILE="$DIR/$tdir/$tfile"
1329         mkdir -p $DIR/$tdir
1330
1331         wait_delete_completed
1332
1333         set_blk_tunesz 512
1334         set_blk_unitsz 1024
1335
1336         log "   User quota (limit: $LIMIT kbytes)"
1337         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1338         quota_show_check b u $TSTUSR
1339
1340         $LFS setstripe $TESTFILE -i 0 -c 1
1341         chown $TSTUSR.$TSTUSR $TESTFILE
1342
1343         #define OBD_FAIL_MDS_BLOCK_QUOTA_REQ      0x13c
1344         lustre_fail mds 0x13c
1345
1346         log "   step1: write 100M block ..."
1347         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$((1024 * 100)) &
1348         DDPID=$!
1349
1350         sleep 5
1351         lustre_fail mds 0
1352
1353         echo  "   step2: testing ......"
1354         count=0
1355         if at_is_enabled; then
1356             timeout=$(at_max_get mds)
1357         else
1358             timeout=$(lctl get_param -n timeout)
1359         fi
1360         while [ true ]; do
1361             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1362             count=$[count+1]
1363             if [ $count -gt $((4 * $timeout)) ]; then
1364                 quota_error u $TSTUSR "count=$count dd should be finished!"
1365             fi
1366             sleep 1
1367         done
1368         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1369         sync
1370         cancel_lru_locks mdc
1371         cancel_lru_locks osc
1372
1373         testfile_size=$(stat -c %s $TESTFILE)
1374         [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] && \
1375             quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)), got ${testfile_size}. Verifying file failed!"
1376         $SHOW_QUOTA_USER
1377         rm -f $TESTFILE
1378         sync
1379
1380         resetquota -u $TSTUSR
1381         set_blk_unitsz $((128 * 1024))
1382         set_blk_tunesz $((128 * 1024 / 2))
1383 }
1384 run_test_with_stat 18 "run for fixing bug14840 ==========="
1385
1386 # test when mds drops a quota req, the ost still could work well b=14840
1387 test_18a() {
1388         LIMIT=$((100 * 1024 * 1024)) # 100G
1389         TESTFILE="$DIR/$tdir/$tfile-a"
1390         mkdir -p $DIR/$tdir
1391
1392         wait_delete_completed
1393
1394         set_blk_tunesz 512
1395         set_blk_unitsz 1024
1396
1397         log "   User quota (limit: $LIMIT kbytes)"
1398         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1399         quota_show_check b u $TSTUSR
1400
1401         $LFS setstripe $TESTFILE -i 0 -c 1
1402         chown $TSTUSR.$TSTUSR $TESTFILE
1403
1404         #define OBD_FAIL_MDS_DROP_QUOTA_REQ | OBD_FAIL_ONCE   0x8000013d
1405         lustre_fail mds 0x8000013d
1406
1407         log "   step1: write 100M block ..."
1408         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$((1024 * 100)) &
1409         DDPID=$!
1410
1411         echo  "   step2: testing ......"
1412         count=0
1413         if at_is_enabled; then
1414             timeout=$(at_max_get mds)
1415         else
1416             timeout=$(lctl get_param -n timeout)
1417         fi
1418         while [ true ]; do
1419             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1420             count=$[count+1]
1421             if [ $count -gt $((6 * $timeout)) ]; then
1422                 lustre_fail mds 0
1423                 quota_error u $TSTUSR "count=$count dd should be finished!"
1424             fi
1425             sleep 1
1426         done
1427         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1428
1429         lustre_fail mds 0
1430         rm -f $TESTFILE
1431         sync
1432
1433         resetquota -u $TSTUSR
1434         set_blk_unitsz $((128 * 1024))
1435         set_blk_tunesz $((128 * 1024 / 2))
1436 }
1437 run_test_with_stat 18a "run for fixing bug14840 ==========="
1438
1439 # test when mds do failover, the ost still could work well without trigger
1440 # watchdog b=14840
1441 test_18bc_sub() {
1442         type=$1
1443
1444         LIMIT=$(((100 + $OSTCOUNT * 3) * 1024))
1445         TESTFILE="$DIR/$tdir/$tfile"
1446         mkdir -p $DIR/$tdir
1447
1448         wait_delete_completed
1449
1450         set_blk_tunesz 512
1451         set_blk_unitsz 1024
1452
1453         log "   User quota (limit: $LIMIT kbytes)"
1454         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1455         quota_show_check b u $TSTUSR
1456
1457         $LFS setstripe $TESTFILE -i 0 -c 1
1458         chown $TSTUSR.$TSTUSR $TESTFILE
1459
1460         timeout=$(sysctl -n lustre.timeout)
1461
1462         if [ $type = "directio" ]; then
1463             log "   write 100M block(directio) ..."
1464             $RUNAS $DIRECTIO write $TESTFILE 0 100 $((BLK_SZ * 1024)) &
1465         else
1466             log "   write 100M block(normal) ..."
1467             $RUNAS dd if=/dev/zero of=$TESTFILE bs=$((BLK_SZ * 1024)) count=100 &
1468         fi
1469
1470         DDPID=$!
1471         do_facet $SINGLEMDS "$LCTL conf_param ${FSNAME}-MDT*.mdd.quota_type=ug"
1472
1473         replay_barrier $SINGLEMDS
1474
1475         log "failing mds for $((2 * timeout)) seconds"
1476         fail $SINGLEMDS $((2 * timeout))
1477
1478         # check if quotaon successful
1479         $LFS quota -u $TSTUSR $MOUNT 2>&1 | grep -q "quotas are not enabled"
1480         if [ $? -eq 0 ]; then
1481             rm -rf $TESTFILE
1482             error "quotaon failed!"
1483             return
1484         fi
1485
1486         count=0
1487         if at_is_enabled; then
1488             timeout=$(at_max_get mds)
1489         else
1490             timeout=$(lctl get_param -n timeout)
1491         fi
1492         while [ true ]; do
1493             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1494             if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1495                 log "it took $count second"
1496             fi
1497             sleep 1
1498         done
1499         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1500         sync
1501         cancel_lru_locks mdc
1502         cancel_lru_locks osc
1503         $SHOW_QUOTA_USER
1504
1505         resetquota -u $TSTUSR
1506         set_blk_unitsz $((128 * 1024))
1507         set_blk_tunesz $((128 * 1024 / 2))
1508         testfile_size=$(stat -c %s $TESTFILE)
1509         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1510              rm -f $TESTFILE
1511              quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)), got ${testfile_size}. Verifying file failed!"
1512         fi
1513         rm -f $TESTFILE
1514
1515 }
1516
1517 # test when mds does failover, the ost still could work well
1518 # this test shouldn't trigger watchdog b=14840
1519 test_18b() {
1520         test_18bc_sub normal
1521         test_18bc_sub directio
1522         # check if watchdog is triggered
1523         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1524         watchdog=`awk '/test 18b/ {start = 1;}
1525                        /Service thread pid/ && /was inactive/ {
1526                                if (start) {
1527                                        print;
1528                                }
1529                        }' $TMP/lustre-log-${TESTNAME}.log`
1530         [ `echo "$watchdog" | wc -l` -ge 3 ] && error "$watchdog"
1531         rm -f $TMP/lustre-log-${TESTNAME}.log
1532 }
1533 run_test_with_stat 18b "run for fixing bug14840(mds failover, no watchdog) ==========="
1534
1535 # test when mds does failover, the ost still could work well
1536 # this test will prevent OST_DISCONNET from happening b=14840
1537 test_18c() {
1538         # define OBD_FAIL_OST_DISCONNECT_NET 0x202(disable ost_disconnect for osts)
1539         lustre_fail ost  0x202
1540         test_18bc_sub normal
1541         test_18bc_sub directio
1542         lustre_fail ost  0
1543 }
1544 run_test_with_stat 18c "run for fixing bug14840(mds failover, OST_DISCONNECT is disabled) ==========="
1545
1546 run_to_block_limit() {
1547         local LIMIT=$((($OSTCOUNT + 1) * $BUNIT_SZ))
1548         local TESTFILE=$1
1549         wait_delete_completed
1550
1551         # set 1 Mb quota unit size
1552         set_blk_tunesz 512
1553         set_blk_unitsz 1024
1554
1555         # bind file to a single OST
1556         $LFS setstripe -c 1 $TESTFILE
1557         chown $TSTUSR.$TSTUSR $TESTFILE
1558
1559         echo "  User quota (limit: $LIMIT kbytes)"
1560         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1561         quota_show_check b u $TSTUSR
1562         echo "  Updating quota limits"
1563         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $MOUNT
1564         quota_show_check b u $TSTUSR
1565
1566         RUNDD="$RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ"
1567         $RUNDD count=$BUNIT_SZ || quota_error u $TSTUSR "(usr) write failure, but expect success"
1568         # for now page cache of TESTFILE may still be dirty,
1569         # let's push it to the corresponding OST, this will also
1570         # cache NOQUOTA on the client from OST's reply
1571         cancel_lru_locks osc
1572         $RUNDD seek=$BUNIT_SZ && quota_error u $TSTUSR "(usr) write success, should be EDQUOT"
1573 }
1574
1575 test_19() {
1576         # 1 Mb bunit per each MDS/OSS
1577         local TESTFILE="$DIR/$tdir/$tfile"
1578         mkdir -p $DIR/$tdir
1579
1580         run_to_block_limit $TESTFILE
1581         $SHOW_QUOTA_USER
1582
1583         # cleanup
1584         rm -f $TESTFILE
1585         resetquota -u $TSTUSR
1586
1587         set_blk_unitsz $((128 * 1024))
1588         set_blk_tunesz $((128 * 1024 / 2))
1589
1590 }
1591 run_test_with_stat 19 "test if administrative limits updates do not zero operational limits (14790) ==="
1592
1593 test_20()
1594 {
1595         LSTR=(1t 2g 3m 4k) # limits strings
1596         LVAL=($[1*1024*1024*1024] $[2*1024*1024] $[3*1024*1024] $[4*1024]) # limits values
1597
1598         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1599                                  $MOUNT || error "could not set quota limits"
1600
1601         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1602                                  --inode-softlimit ${LSTR[2]} \
1603                                  --inode-hardlimit ${LSTR[3]} \
1604                                  $MOUNT || error "could not set quota limits"
1605
1606         [ "`getquota -u $TSTUSR global bsoftlimit`" = "${LVAL[0]}" ] || error "bsoftlimit was not set properly"
1607         [ "`getquota -u $TSTUSR global bhardlimit`" = "${LVAL[1]}" ] || error "bhardlimit was not set properly"
1608         [ "`getquota -u $TSTUSR global isoftlimit`" = "${LVAL[2]}" ] || error "isoftlimit was not set properly"
1609         [ "`getquota -u $TSTUSR global ihardlimit`" = "${LVAL[3]}" ] || error "ihardlimit was not set properly"
1610
1611         resetquota -u $TSTUSR
1612 }
1613 run_test_with_stat 20 "test if setquota specifiers work properly (15754)"
1614
1615 test_21_sub() {
1616         local testfile=$1
1617         local blk_number=$2
1618         local seconds=$3
1619
1620         time=$(($(date +%s) + seconds))
1621         while [ $(date +%s) -lt $time ]; do
1622             $RUNAS dd if=/dev/zero of=$testfile  bs=$BLK_SZ count=$blk_number > /dev/null 2>&1
1623             rm -f $testfile
1624         done
1625 }
1626
1627 # run for fixing bug16053, setquota shouldn't fail when writing and
1628 # deleting are happening
1629 test_21() {
1630         set_blk_tunesz 512
1631         set_blk_unitsz 1024
1632
1633         wait_delete_completed
1634
1635         TESTFILE="$DIR/$tdir/$tfile"
1636
1637         BLK_LIMIT=$((10 * 1024 * 1024)) # 10G
1638         FILE_LIMIT=1000000
1639
1640         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for user: $TSTUSR"
1641         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $MOUNT
1642         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for group: $TSTUSR"
1643         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $MOUNT
1644
1645         # repeat writing on a 1M file
1646         test_21_sub ${TESTFILE}_1 1024 30 &
1647         DDPID1=$!
1648         # repeat writing on a 128M file
1649         test_21_sub ${TESTFILE}_2 $((1024 * 128)) 30 &
1650         DDPID2=$!
1651
1652         time=$(($(date +%s) + 30))
1653         i=1
1654         while [ $(date +%s) -lt $time ]; do
1655             log "  Set quota for $i times"
1656             $LFS setquota -u $TSTUSR -b 0 -B $((BLK_LIMIT + 1024 * i)) -i 0 -I $((FILE_LIMIT + i)) $MOUNT
1657             $LFS setquota -g $TSTUSR -b 0 -B $((BLK_LIMIT + 1024 * i)) -i 0 -I $((FILE_LIMIT + i)) $MOUNT
1658             i=$((i+1))
1659             sleep 1
1660         done
1661
1662         count=0
1663         while [ true ]; do
1664             if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1665             count=$[count+1]
1666             if [ $count -gt 60 ]; then
1667                 quota_error a $TSTUSR "dd should be finished!"
1668             fi
1669             sleep 1
1670         done
1671         echo "(dd_pid=$DDPID1, time=$count)successful"
1672
1673         count=0
1674         while [ true ]; do
1675             if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1676             count=$[count+1]
1677             if [ $count -gt 60 ]; then
1678                 quota_error a $TSTUSR "dd should be finished!"
1679             fi
1680             sleep 1
1681         done
1682         echo "(dd_pid=$DDPID2, time=$count)successful"
1683
1684         set_blk_unitsz $((128 * 1024))
1685         set_blk_tunesz $((128 * 1024 / 2))
1686         resetquota -u $TSTUSR
1687         resetquota -g $TSTUSR
1688
1689         return $RC
1690 }
1691 run_test_with_stat 21 "run for fixing bug16053 ==========="
1692
1693 test_22() {
1694         quota_save_version "ug3"
1695
1696         stopall
1697         mount
1698         setupall
1699
1700         echo "checking parameters"
1701
1702         do_facet $SINGLEMDS "lctl get_param mdd.${FSNAME}-MDT*.quota_type" | grep "ug3" || error "admin failure"
1703         do_facet ost1 "lctl get_param obdfilter.*.quota_type" | grep "ug3" || error "op failure"
1704
1705         quota_init
1706 }
1707 run_test_with_stat 22 "test if quota_type saved as permanent parameter ===="
1708
1709 test_23_sub() {
1710         mkdir -p $DIR/$tdir
1711         chmod 0777 $DIR/$tdir
1712         TESTFILE="$DIR/$tdir/$tfile-0"
1713         rm -f $TESTFILE
1714         local bs_unit=$((1024*1024))
1715         LIMIT=$1
1716
1717         wait_delete_completed
1718
1719         # test for user
1720         log "  User quota (limit: $LIMIT kbytes)"
1721         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1722         sleep 3
1723         quota_show_check b u $TSTUSR
1724
1725         $LFS setstripe $TESTFILE -c 1 -i 0
1726         chown $TSTUSR.$TSTUSR $TESTFILE
1727
1728         log "    Step1: trigger quota with 0_DIRECT"
1729         log "      Write half of file"
1730         $RUNAS $DIRECTIO write $TESTFILE 0 $(($LIMIT/1024/2)) $bs_unit || \
1731                 quota_error u $TSTUSR "(1) write failure, but expect success: $LIMIT"
1732         log "      Write out of block quota ..."
1733         $RUNAS $DIRECTIO write $TESTFILE $(($LIMIT/1024/2)) $(($LIMIT/1024/2)) $bs_unit && \
1734                 quota_error u $TSTUSR "(2) write success, but expect EDQUOT: $LIMIT"
1735         log "    Step1: done"
1736
1737         log "    Step2: rewrite should succeed"
1738         $RUNAS $DIRECTIO write $TESTFILE 0 1 $bs_unit || \
1739                 quota_error u $TSTUSR "(3) write failure, but expect success: $LIMIT"
1740         log "    Step2: done"
1741
1742         rm -f $TESTFILE
1743         wait_delete_completed
1744         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
1745         OST0_QUOTA_USED=`getquota -u $TSTUSR $OST0_UUID curspace`
1746         echo $OST0_QUOTA_USED
1747         [ $OST0_QUOTA_USED -ne 0 ] && \
1748             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "quota deleted isn't released")
1749         $SHOW_QUOTA_USER
1750         resetquota -u $TSTUSR
1751 }
1752
1753 test_23() {
1754         local slave_cnt=$((OSTCOUNT + 1)) # 1 mds, n osts
1755
1756         OST0_MIN=$((6 * $slave_cnt * 1024)) # extra space for meta blocks.
1757         check_whether_skip && return 0
1758         log "run for $((4 * $slave_cnt))MB test file"
1759         test_23_sub $((4 * $slave_cnt * 1024))
1760
1761         OST0_MIN=$((60 * $slave_cnt * 1024)) # extra space for meta blocks.
1762         check_whether_skip && return 0
1763         log "run for $((40 * $slave_cnt))MB test file"
1764         test_23_sub $((40 * $slave_cnt * 1024))
1765 }
1766 run_test_with_stat 23 "run for fixing bug16125 ==========="
1767
1768 test_24() {
1769         local TESTFILE="$DIR/$tdir/$tfile"
1770         mkdir -p $DIR/$tdir
1771
1772         run_to_block_limit $TESTFILE
1773         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1774
1775         # cleanup
1776         rm -f $TESTFILE
1777         resetquota -u $TSTUSR
1778
1779         set_blk_unitsz $((128 * 1024))
1780         set_blk_tunesz $((128 * 1024 / 2))
1781         
1782 }
1783 run_test_with_stat 24 "test if lfs draws an asterix when limit is reached (16646) ==========="
1784
1785 show_quota() {
1786         if [ $1 = "-u" ]; then
1787                 if [ $2 = "$TSTUSR" ]; then
1788                         $SHOW_QUOTA_USER
1789                 else
1790                         $SHOW_QUOTA_USER2
1791                 fi
1792         else
1793                 if [ $2 = "$TSTUSR" ]; then
1794                         $SHOW_QUOTA_GROUP
1795                 else
1796                         $SHOW_QUOTA_GROUP2
1797                 fi
1798         fi
1799 }
1800
1801 test_25_sub() {
1802         mkdir -p $DIR/$tdir
1803         chmod 0777 $DIR/$tdir
1804         TESTFILE="$DIR/$tdir/$tfile-0"
1805         rm -f $TESTFILE
1806         LIMIT=$(( $BUNIT_SZ * ($OSTCOUNT + 1) + 4096 ))
1807
1808         wait_delete_completed
1809
1810         # set quota for $TSTUSR
1811         log "setquota for $TSTUSR"
1812         $LFS setquota $1 $TSTUSR -b $LIMIT -B $LIMIT -i 10 -I 10 $DIR
1813         sleep 3
1814         if [ "$1" == "-u" ]; then
1815                 quota_show_check a u $TSTUSR
1816         else
1817                 quota_show_check a g $TSTUSR
1818         fi
1819
1820         # set quota for $TSTUSR2
1821         log "setquota for $TSTUSR2"
1822         $LFS setquota $1 $TSTUSR2 -b $LIMIT -B $LIMIT -i 10 -I 10 $DIR
1823         sleep 3
1824         if [ "$1" == "-u" ]; then
1825                 quota_show_check a u $TSTUSR2
1826         else
1827                 quota_show_check a g $TSTUSR2
1828         fi
1829
1830         # set stripe index to 0
1831         log "setstripe for $DIR/$tdir to 0"
1832         $LFS setstripe $DIR/$tdir -c 1 -i 0
1833         MDS_UUID=`do_facet $SINGLEMDS $LCTL dl | grep -m1 " mdt " | awk '{print $((NF-1))}'`
1834         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
1835         MDS_QUOTA_USED_OLD=`getquota $1 $TSTUSR $MDS_UUID curinodes`
1836         OST0_QUOTA_USED_OLD=`getquota $1 $TSTUSR $OST0_UUID curspace`
1837         MDS_QUOTA_USED2_OLD=`getquota $1 $TSTUSR2 $MDS_UUID curinodes`
1838         OST0_QUOTA_USED2_OLD=`getquota $1 $TSTUSR2 $OST0_UUID curspace`
1839
1840         # TSTUSR write 4M
1841         log "$TSTUSR write 4M to $TESTFILE"
1842         $RUNAS dd if=/dev/zero of=$TESTFILE bs=4K count=1K || quota_error a $TSTUSR "dd failed"
1843         sync
1844         show_quota $1 $TSTUSR
1845         show_quota $1 $TSTUSR2
1846         MDS_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $MDS_UUID curinodes`
1847         [ $MDS_QUOTA_USED_NEW -ne $((MDS_QUOTA_USED_OLD + 1)) ] && \
1848                 quota_error a $TSTUSR "$TSTUSR inode quota usage error: [$MDS_QUOTA_USED_OLD|$MDS_QUOTA_USED_NEW]"
1849         OST0_QUOTA_USED_NEW=`getquota   $1 $TSTUSR $OST0_UUID curspace`
1850         OST0_QUOTA_USED_DELTA=$((OST0_QUOTA_USED_NEW - OST0_QUOTA_USED_OLD))
1851         [ $OST0_QUOTA_USED_DELTA -lt 4096 ] && \
1852                 quota_error a $TSTUSR "$TSTUSR block quota usage error: [$OST0_QUOTA_USED_OLD|$OST0_QUOTA_USED_NEW]"
1853
1854         # chown/chgrp from $TSTUSR to $TSTUSR2
1855         if [ $1 = "-u" ]; then
1856                 log "chown from $TSTUSR to $TSTUSR2"
1857                 chown $TSTUSR2 $TESTFILE || quota_error u $TSTUSR2 "chown failed"
1858         else
1859                 log "chgrp from $TSTUSR to $TSTUSR2"
1860                 chgrp $TSTUSR2 $TESTFILE || quota_error g $TSTUSR2 "chgrp failed"
1861         fi
1862         sync
1863         show_quota $1 $TSTUSR
1864         show_quota $1 $TSTUSR2
1865         MDS_QUOTA_USED2_NEW=`getquota $1 $TSTUSR2 $MDS_UUID curinodes`
1866         [ $MDS_QUOTA_USED2_NEW -ne $((MDS_QUOTA_USED2_OLD + 1)) ] && \
1867                 quota_error a $TSTUSR2 "$TSTUSR2 inode quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$MDS_QUOTA_USED2_OLD|$MDS_QUOTA_USED2_NEW]"
1868         OST0_QUOTA_USED2_NEW=`getquota  $1 $TSTUSR2 $OST0_UUID curspace`
1869         # when chown, the quota on ost could be displayed out of quota temporarily. Delete the '*' in this situation. b=20433
1870         OST0_QUOTA_USED2_NEW=${OST0_QUOTA_USED2_NEW%\*}
1871         OST0_QUOTA_USED2_DELTA=$((OST0_QUOTA_USED2_NEW - OST0_QUOTA_USED2_OLD))
1872         [ $OST0_QUOTA_USED2_DELTA -ne $OST0_QUOTA_USED_DELTA ] && \
1873                 quota_error a $TSTUSR2 "$TSTUSR2 block quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$OST0_QUOTA_USED2_OLD|$OST0_QUOTA_USED2_NEW]"
1874         MDS_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $MDS_UUID curinodes`
1875         [ $MDS_QUOTA_USED_NEW -ne $MDS_QUOTA_USED_OLD ] && \
1876                 quota_error a $TSTUSR "$TSTUSR inode quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$MDS_QUOTA_USED_OLD|$MDS_QUOTA_USED_NEW]"
1877         OST0_QUOTA_USED_NEW=`getquota  $1 $TSTUSR $OST0_UUID curspace`
1878         [ $OST0_QUOTA_USED_NEW -ne $OST0_QUOTA_USED_OLD ] && \
1879                 quota_error a $TSTUSR "$TSTUSR block quota usage transfer from $TSTUSR to $TSTUSR2 failed: [$OST0_QUOTA_USED_OLD|$OST0_QUOTA_USED_NEW]"
1880
1881         rm -f $TESTFILE
1882         wait_delete_completed
1883         resetquota $1 $TSTUSR
1884         resetquota $1 $TSTUSR2
1885 }
1886
1887 test_25() {
1888         log "run for chown case"
1889         test_25_sub -u
1890
1891         log "run for chgrp case"
1892         test_25_sub -g
1893 }
1894 run_test_with_stat 25 "test whether quota usage is transfered when chown/chgrp (18081) ==========="
1895
1896 test_26() {
1897         mkdir -p $DIR/$tdir
1898         chmod 0777 $DIR/$tdir
1899         TESTFILE="$DIR/$tdir/$tfile-0"
1900         TESTFILE2="$DIR/$tdir/$tfile-1"
1901         set_blk_tunesz 512
1902         set_blk_unitsz 1024
1903
1904         wait_delete_completed
1905
1906         # every quota slave gets 20MB
1907         b_limit=$(((OSTCOUNT + 1) * 20 * 1024))
1908         log "limit: ${b_limit}KB"
1909         $LFS setquota -u $TSTUSR -b 0 -B $b_limit -i 0 -I 0 $DIR
1910         sleep 3
1911         quota_show_check b u $TSTUSR
1912
1913         $LFS setstripe $TESTFILE  -c 1 -i 0
1914         $LFS setstripe $TESTFILE2 -c 1 -i 0
1915         chown $TSTUSR.$TSTUSR $TESTFILE
1916         chown $TSTUSR.$TSTUSR $TESTFILE2
1917
1918         #define OBD_FAIL_QUOTA_DELAY_REL         0xA03
1919         lustre_fail ost 0xA03
1920
1921         log "    Write the first file..."
1922         $RUNAS $DIRECTIO write $TESTFILE 0 10 $((BLK_SZ * 1024)) || quota_error u $TSTUSR "write failure, but expect success"
1923         log "    Delete the first file..."
1924         rm -f $TESTFILE
1925
1926
1927         wait_delete_completed
1928
1929         log "    Write the second file..."
1930         $RUNAS $DIRECTIO write $TESTFILE2 0 10 $((BLK_SZ * 1024)) || quota_error u $TSTUSR "write failure, but expect success"
1931         log "    Delete the second file..."
1932         rm -f $TESTFILE2
1933
1934         lustre_fail ost 0
1935         set_blk_unitsz $((128 * 1024))
1936         set_blk_tunesz $((128 * 1024 / 2))
1937         resetquota -u $TSTUSR
1938 }
1939 run_test_with_stat 26 "test for false quota error(bz18491) ======================================"
1940
1941 test_27a() {
1942         $LFS quota $TSTUSR $DIR && error "lfs succeeded with no type, but should have failed"
1943         $LFS setquota $TSTUSR $DIR && error "lfs succeeded with no type, but should have failed"
1944         return 0
1945 }
1946 run_test_with_stat 27a "lfs quota/setquota should handle wrong arguments (19612) ================="
1947
1948 test_27b() {
1949         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR || \
1950                 error "lfs setquota failed with uid argument"
1951         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR || \
1952                 error "lfs stequota failed with gid argument"
1953         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
1954         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
1955         resetquota -u $TSTUSR
1956         resetquota -g $TSTUSR
1957         return 0
1958 }
1959 run_test 27b "lfs quota/setquota should handle user/group ID (20200) ================="
1960
1961 test_28() {
1962         BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
1963         echo "Step 1: set enough high limit for user [$TSTUSR:$BLK_LIMIT]"
1964         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1965         $SHOW_QUOTA_USER
1966
1967         echo "Step 2: reset system ..."
1968         cleanup_and_setup_lustre
1969         quota_init
1970
1971         echo "Step 3: change qunit for user [$TSTUSR:512:1024]"
1972         set_blk_tunesz 512
1973         set_blk_unitsz 1024
1974
1975         wait_delete_completed
1976
1977         #define OBD_FAIL_QUOTA_RET_QDATA | OBD_FAIL_ONCE
1978         lustre_fail ost 0x80000A02
1979
1980         TESTFILE="$DIR/$tdir/$tfile"
1981         mkdir -p $DIR/$tdir
1982
1983         BLK_LIMIT=$((100 * 1024)) # 100M
1984         echo "Step 4: set enough high limit for user [$TSTUSR:$BLK_LIMIT]"
1985         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR
1986         $SHOW_QUOTA_USER
1987
1988         touch $TESTFILE
1989         chown $TSTUSR.$TSTUSR $TESTFILE
1990
1991         echo "Step 5: write the test file1 [10M] ..."
1992         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$BLK_SZ count=$(( 10 * 1024 )) \
1993             || quota_error a $TSTUSR "write 10M file failure"
1994         $SHOW_QUOTA_USER
1995
1996         rm -f $TESTFILE
1997         sync; sleep 3; sync;
1998
1999         # make qd_count 64 bit
2000         lustre_fail ost 0
2001
2002         set_blk_unitsz $((128 * 1024))
2003         set_blk_tunesz $((128 * 1024 / 2))
2004
2005         resetquota -u $TSTUSR
2006 }
2007 run_test_with_stat 28 "test for consistency for qunit when setquota (18574) ==========="
2008
2009 test_29()
2010 {
2011         local BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
2012         local timeout
2013         local at_max_val
2014         local pid
2015         local waited
2016         local deadline
2017
2018         at_max_val=10
2019
2020         if at_is_enabled; then
2021                 timeout=$(at_max_get client)
2022                 [ $(at_min_get client) -gt $at_max_val ] &&
2023                         at_max_val=$(at_min_get client)
2024                 at_max_set $at_max_val client
2025         else
2026                 timeout=$(lctl get_param -n timeout)
2027                 lctl set_param timeout=$at_max_val
2028         fi
2029         # actually send a RPC to make service at_current confined within at_max
2030         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR ||
2031                 error "should succeed"
2032
2033         #define OBD_FAIL_MDS_QUOTACTL_NET 0x12e
2034         lustre_fail mds 0x12e
2035
2036         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR & pid=$!
2037
2038         deadline=$((2 * (($at_max_val << 1) + ($at_max_val >> 2) + 5)))
2039         echo "wait at most 2 * ($at_max_val * 2.25 + 5) = $deadline seconds," \
2040              "it is server process time add the network latency."
2041
2042         waited=0
2043         while [ $waited -lt $deadline ]; do
2044                 echo -n "."
2045                 sleep 1
2046                 waited=$(($waited + 1))
2047                 ps -p $pid > /dev/null || break
2048         done
2049         echo "waited $waited seconds"
2050         ps -p $pid && error "lfs hadn't finished by $deadline seconds"
2051         wait $pid && error "succeeded, but should have failed"
2052
2053         lustre_fail mds 0
2054
2055         if at_is_enabled; then
2056                 at_max_set $timeout client
2057         else
2058                 lctl set_param timeout=$timeout
2059         fi
2060
2061         resetquota -u $TSTUSR
2062 }
2063 run_test_with_stat 29 "unhandled quotactls must not hang lustre client (19778) ========"
2064
2065 test_30()
2066 {
2067         local output
2068         local LIMIT=1024
2069         local TESTFILE="$DIR/$tdir/$tfile"
2070         local GRACE=10
2071
2072         set_blk_tunesz 512
2073         set_blk_unitsz 1024
2074
2075         mkdir -p $DIR/$tdir
2076         chmod 0777 $DIR/$tdir
2077
2078         $LFS setstripe $TESTFILE -i 0 -c 1
2079         chown $TSTUSR.$TSTUSR $TESTFILE
2080
2081         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
2082         $LFS setquota -u $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
2083         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1024 count=$((LIMIT * 2)) || true
2084         cancel_lru_locks osc
2085         sleep $GRACE
2086         $LFS setquota -u $TSTUSR -B 0 $DIR
2087         # over-quota flag has not yet settled since we do not trigger async events
2088         # based on grace time period expiration
2089         $SHOW_QUOTA_USER
2090         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 || true
2091         cancel_lru_locks osc
2092         # now over-quota flag should be settled and further writes should fail
2093         $SHOW_QUOTA_USER
2094         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 && error "grace times were reset"
2095         rm -f $TESTFILE
2096         resetquota -u $TSTUSR
2097         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
2098
2099         set_blk_unitsz $((128 * 1024))
2100         set_blk_tunesz $((128 * 1024 / 2))
2101 }
2102 run_test_with_stat 30 "hard limit updates should not reset grace times ================"
2103
2104 # test duplicate quota releases b=18630
2105 test_31() {
2106         mkdir -p $DIR/$tdir
2107         chmod 0777 $DIR/$tdir
2108
2109         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) * 10)) # 10 bunits each sever
2110         TESTFILE="$DIR/$tdir/$tfile-0"
2111         TESTFILE2="$DIR/$tdir/$tfile-1"
2112
2113         wait_delete_completed
2114
2115         log "   User quota (limit: $LIMIT kbytes)"
2116         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
2117
2118         $LFS setstripe $TESTFILE -i 0 -c 1
2119         chown $TSTUSR.$TSTUSR $TESTFILE
2120         $LFS setstripe $TESTFILE2 -i 0 -c 1
2121         chown $TSTUSR.$TSTUSR $TESTFILE2
2122
2123         log "   step1: write out of block quota ..."
2124         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=5120
2125         $RUNAS dd if=/dev/zero of=$TESTFILE2 bs=$BLK_SZ count=5120
2126
2127         #define OBD_FAIL_QUOTA_DELAY_SD      0xA04
2128         #define OBD_FAIL_SOME        0x10000000 /* fail N times */
2129         lustre_fail ost $((0x00000A04 | 0x10000000)) 1
2130
2131         log "   step2: delete two files so that triggering duplicate quota release ..."
2132         rm -f $TESTFILE $TESTFILE2
2133         sync; sleep 5; sync      #  OBD_FAIL_QUOTA_DELAY_SD will delay for 5 seconds
2134         wait_delete_completed
2135
2136         log "   step3: verify if the ost failed"
2137         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2138         watchdog=`awk '/test 31/ {start = 1;}
2139                        /release quota error/ {
2140                                if (start) {
2141                                        print;
2142                                }
2143                        }' $TMP/lustre-log-${TESTNAME}.log`
2144         [ "$watchdog" ] && error "$watchdog"
2145         rm -f $TMP/lustre-log-${TESTNAME}.log
2146
2147         lustre_fail ost 0
2148         resetquota -u $TSTUSR
2149 }
2150 run_test_with_stat 31 "test duplicate quota releases ==="
2151
2152 # check hash_cur_bits
2153 check_quota_hash_cur_bits() {
2154         local bits=$1
2155
2156         # check quota_hash_cur_bits on all obdfilters
2157         for num in `seq $OSTCOUNT`; do
2158             cb=`do_facet ost$num "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2159             if [ $cb -gt $bits ]; then
2160                 echo "hash_lqs_cur_bits of ost$num is too large(cur_bits=$cb)"
2161                 return 1;
2162             fi
2163         done
2164         # check quota_hash_cur_bits on mds
2165         cb=`do_facet $SINGLEMDS "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2166         if [ $cb -gt $bits ]; then
2167             echo "hash_lqs_cur_bits of mds is too large(cur_bits=$cb)"
2168             return 1;
2169         fi
2170         return 0;
2171 }
2172
2173 # check lqs hash
2174 check_lqs_hash() {
2175         # check distribution of all obdfilters
2176         for num in `seq $OSTCOUNT`; do
2177             do_facet ost$num "lctl get_param obdfilter.${FSNAME}-OST*.hash_stats | grep LQS_HASH" | while read line; do
2178                 rehash_count=`echo $line | awk '{print $9}'`
2179                 if [ $rehash_count -eq 0 ]; then
2180                     echo -e "ost$num:\n $line"
2181                     error "Rehearsh didn't happen"
2182                 fi
2183             done
2184         done
2185         # check distribution of mds
2186         do_facet $SINGLEMDS "lctl get_param mdt.${FSNAME}-MDT*.hash_stats | grep LQS_HASH" | while read line; do
2187             rehash_count=`echo $line | awk '{print $9}'`
2188             if [ $rehash_count -eq 0 ]; then
2189                 echo -e "mdt:\n $line"
2190                 error "Rehearsh didn't happen"
2191             fi
2192         done
2193 }
2194
2195 test_32()
2196 {
2197         # reset system so that quota_hash_cur_bits==3
2198         echo "Reset system ..."
2199         local LMR_orig=$LOAD_MODULES_REMOTE
2200         LOAD_MODULES_REMOTE=true
2201         cleanup_and_setup_lustre
2202         LOAD_MODULES_REMOTE=$LMR_orig
2203
2204         client_up
2205         wait_mds_ost_sync
2206         quota_init
2207
2208         for user in $SANITY_QUOTA_USERS; do
2209             check_runas_id_ret $user quota_usr "runas -u $user -g quota_usr" >/dev/null 2>/dev/null || \
2210                 missing_users="$missing_users $user"
2211         done
2212         [ -n "$missing_users" ] && { skip_env "the following users are missing: $missing_users" ; return 0 ; }
2213         check_quota_hash_cur_bits 3 || { skip_env "hash_lqs_cur_bits isn't set properly"; return 0;}
2214
2215         $LFS quotaoff -ug $DIR
2216         $LFS quotacheck -ug $DIR
2217
2218         for user in $SANITY_QUOTA_USERS; do
2219             $LFS setquota -u $user --block-hardlimit 1048576 $DIR
2220         done
2221
2222         check_lqs_hash
2223
2224         for user in $SANITY_QUOTA_USERS; do
2225             resetquota -u $user
2226         done
2227 }
2228 run_test 32 "check lqs hash(bug 21846) =========================================="
2229
2230 cleanup_quota_test() {
2231         trap 0
2232         echo "Delete files..."
2233         rm -rf $DIR/$tdir
2234 }
2235
2236 # basic usage tracking for user & group
2237 test_33() {
2238         mkdir -p $DIR/$tdir
2239         chmod 0777 $DIR/$tdir
2240         INODES=10
2241         BLK_CNT=1024
2242         TOTAL_BLKS=$(($INODES * $BLK_CNT))
2243
2244         trap cleanup_quota_test EXIT
2245
2246         # make sure the system is clean
2247         USED=`getquota -u $TSTID global curspace`
2248         [ $USED -ne 0 ] && \
2249                 error "Used space ($USED) for user $TSTID isn't 0."
2250         USED=`getquota -g $TSTID global curspace`
2251         [ $USED -ne 0 ] && \
2252                 error "Used space ($USED) for group $TSTID isn't 0."
2253
2254         for i in `seq 0 $INODES`; do
2255                 $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile-$i conv=fsync \
2256                         bs=$((BLK_SZ * BLK_CNT)) count=1 2>/dev/null ||
2257                         error "write failed"
2258                 echo "Iteration $i/$INODES completed"
2259         done
2260         sync; sync_all_data;
2261
2262         echo "Verify disk usage after write"
2263         USED=`getquota -u $TSTID global curspace`
2264         [ $USED -lt $TOTAL_BLKS ] && \
2265                error "Used space for user $TSTID is $USED, expected $TOTAL_BLKS"
2266         USED=`getquota -u $TSTID global curinodes`
2267         [ $USED -lt $INODES ] && \
2268                error "Used inode for user $TSTID is $USED, expected $INODES"
2269
2270         USED=`getquota -g $TSTID global curspace`
2271         [ $USED -lt $TOTAL_BLKS ] && \
2272               error "Used space for group $TSTID is $USED, expected $TOTAL_BLKS"
2273         USED=`getquota -g $TSTID global curinodes`
2274         [ $USED -lt $INODES ] && \
2275               error "Used inode for group $TSTID is $USED, expected $INODES"
2276
2277         cleanup_quota_test
2278
2279         echo "Verify disk usage after delete"
2280         wait_delete_completed
2281         USED=`getquota -u $TSTID global curspace`
2282         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2283         USED=`getquota -u $TSTID global curinodes`
2284         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2285         USED=`getquota -g $TSTID global curspace`
2286         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2287         USED=`getquota -g $TSTID global curinodes`
2288         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2289 }
2290 run_test 33 "basic usage tracking for user & group =============================="
2291
2292 # usage transfer test for user & group
2293 test_34() {
2294         BLK_CNT=1024
2295         mkdir -p $DIR/$tdir
2296         chmod 0777 $DIR/$tdir
2297
2298         trap cleanup_quota_test EXIT
2299
2300         # make sure the system is clean
2301         USED=`getquota -u $TSTID global curspace`
2302         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2303         USED=`getquota -g $TSTID global curspace`
2304         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2305
2306         echo "Write file..."
2307         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) count=1 \
2308                 conv=fsync 2>/dev/null || error "write failed"
2309         sync; sync_all_data;
2310
2311         echo "chown the file to user $TSTID"
2312         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2313
2314         echo "Wait for setattr on objects finished..."
2315         wait_delete_completed
2316
2317         echo "Verify disk usage for user $TSTID"
2318         USED=`getquota -u $TSTID global curspace`
2319         [ $USED -lt $BLK_CNT ] && \
2320                 error "Used space for user $TSTID is $USED, expected $BLK_CNT"
2321         USED=`getquota -u $TSTID global curinodes`
2322         [ $USED -ne 1 ] && \
2323                 error "Used inodes for user $TSTID is $USED, expected 1"
2324
2325         echo "chgrp the file to group $TSTID"
2326         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2327
2328         echo "Wait for setattr on objects finished..."
2329         wait_delete_completed
2330
2331         echo "Verify disk usage for group $TSTID"
2332         USED=`getquota -g $TSTID global curspace`
2333         [ $USED -ge $BLK_CNT ] || \
2334                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2335         USED=`getquota -g $TSTID global curinodes`
2336         [ $USED -eq 1 ] || \
2337                 error "Used inodes for group $TSTID is $USED, expected 1"
2338
2339         cleanup_quota_test
2340 }
2341 run_test 34 "usage transfer for user & group ===================================="
2342
2343 # usage is still accessible across restart
2344 test_35() {
2345         mkdir -p $DIR/$tdir
2346         chmod 0777 $DIR/$tdir
2347         BLK_CNT=1024
2348
2349         trap cleanup_quota_test EXIT
2350
2351         echo "Write file..."
2352         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) \
2353                 count=1 conv=fsync 2>/dev/null || error "write failed"
2354         sync; sync_all_data;
2355
2356         echo "Save disk usage before restart"
2357         ORIG_USR_SPACE=`getquota -u $TSTID global curspace`
2358         [ $ORIG_USR_SPACE -eq 0 ] && \
2359                 error "Used space for user $TSTID is 0, expected $BLK_CNT"
2360         ORIG_USR_INODES=`getquota -u $TSTID global curinodes`
2361         [ $ORIG_USR_INODES -eq 0 ] && \
2362                 error "Used inodes for user $TSTID is 0, expected 1"
2363         ORIG_GRP_SPACE=`getquota -g $TSTID global curspace`
2364         [ $ORIG_GRP_SPACE -eq 0 ] && \
2365                 error "Used space for group $TSTID is 0, expected $BLK_CNT"
2366         ORIG_GRP_INODES=`getquota -g $TSTID global curinodes`
2367         [ $ORIG_GRP_INODES -eq 0 ] && \
2368                 error "Used inodes for group $TSTID is 0, expected 1"
2369
2370         log "Restart..."
2371         local ORIG_REFORMAT=$REFORMAT
2372         REFORMAT=""
2373         cleanup_and_setup_lustre
2374         REFORMAT=$ORIG_REFORMAT
2375         quota_init
2376
2377         echo "Verify disk usage after restart"
2378         USED=`getquota -u $TSTID global curspace`
2379         [ $USED -eq $ORIG_USR_SPACE ] || \
2380                 error "Used space for user $TSTID changed from " \
2381                         "$ORIG_USR_SPACE to $USED"
2382         USED=`getquota -u $TSTID global curinodes`
2383         [ $USED -eq $ORIG_USR_INODES ] || \
2384                 error "Used inodes for user $TSTID changed from " \
2385                         "$ORIG_USR_INODES to $USED"
2386         USED=`getquota -g $TSTID global curspace`
2387         [ $USED -eq $ORIG_GRP_SPACE ] || \
2388                 error "Used space for group $TSTID changed from " \
2389                         "$ORIG_GRP_SPACE to $USED"
2390         USED=`getquota -g $TSTID global curinodes`
2391         [ $USED -eq $ORIG_GRP_INODES ] || \
2392                 error "Used inodes for group $TSTID changed from " \
2393                         "$ORIG_GRP_INODES to $USED"
2394
2395         # check if the vfs_dq_init() is called before writing
2396         echo "Append to the same file..."
2397         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) \
2398                 count=1 seek=1 conv=fsync 2>/dev/null || error "write failed"
2399         sync; sync_all_data;
2400
2401         echo "Verify space usage is increased"
2402         USED=`getquota -u $TSTID global curspace`
2403         [ $USED -gt $ORIG_USR_SPACE ] ||
2404                 error "Used space for user $TSTID isn't increased" \
2405                         "orig:$ORIG_USR_SPACE, now:$USED"
2406         USED=`getquota -g $TSTID global curspace`
2407         [ $USED -gt $ORIG_GRP_SPACE ] ||
2408                 error "Used space for group $TSTID isn't increased" \
2409                         "orig:$ORIG_GRP_SPACE, now:$USED"
2410
2411         cleanup_quota_test
2412 }
2413 run_test 35 "usage is still accessible across reboot ============================"
2414
2415 # turn off quota
2416 quota_fini()
2417 {
2418         $LFS quotaoff $DIR
2419         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2420 }
2421 quota_fini
2422
2423 cd $ORIG_PWD
2424 complete $(basename $0) $SECONDS
2425 check_and_cleanup_lustre
2426 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2427 exit_status