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