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