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