Whamcloud - gitweb
LU-1538 tests: use $TESTSUITE instead of $0
[fs/lustre-release.git] / lustre / tests / sanity-quota-old.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # Run test by setting NOSETUP=true when ltest has setup env for us
7 set -e
8
9 SRCDIR=`dirname $0`
10 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
11
12 ONLY=${ONLY:-"$*"}
13 # test_11 has been used to protect a kernel bug(bz10912), now it isn't
14 # useful any more. Then add it to ALWAYS_EXCEPT. b=19835
15 # We have changed the mechanism of quota, test_12 is meanless now.
16 # b=20877
17 ALWAYS_EXCEPT="10 12 $SANITY_QUOTA_EXCEPT"
18 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
19
20 [ "$ALWAYS_EXCEPT$EXCEPT" ] && \
21         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
22
23 TMP=${TMP:-/tmp}
24
25 ORIG_PWD=${PWD}
26 TSTID=${TSTID:-60000}
27 TSTID2=${TSTID2:-60001}
28 TSTUSR=${TSTUSR:-"quota_usr"}
29 TSTUSR2=${TSTUSR2:-"quota_2usr"}
30 BLK_SZ=1024
31 BUNIT_SZ=${BUNIT_SZ:-1024}      # min block quota unit(kB)
32 IUNIT_SZ=${IUNIT_SZ:-10}        # min inode quota unit
33 MAX_DQ_TIME=604800
34 MAX_IQ_TIME=604800
35 SANITY_QUOTA_USERS="quota15_1 quota15_2 quota15_3 quota15_4 quota15_5 quota15_6 \
36                     quota15_7 quota15_8 quota15_9 quota15_10 quota15_11 quota15_12 \
37                     quota15_13 quota15_14 quota15_15 quota15_16 quota15_17 quota15_18 \
38                     quota15_19 quota15_20 quota15_21 quota15_22 quota15_23 quota15_24 \
39                     quota15_25 quota15_26 quota15_27 quota15_28 quota15_29 quota15_30"
40
41 export MULTIOP=${MULTIOP:-multiop}
42 TRACE=${TRACE:-""}
43 LUSTRE=${LUSTRE:-`dirname $0`/..}
44 . $LUSTRE/tests/test-framework.sh
45 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
46 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
47
48 [ $MDSCOUNT -gt 1 ] && skip "CMD case" && exit 0
49
50 require_dsh_mds || exit 0
51 require_dsh_ost || exit 0
52
53 [ "$SLOW" = "no" ] && EXCEPT_SLOW="9 10 11 18b 21"
54
55 QUOTALOG=${TESTSUITELOG:-$TMP/sanity-quota.log}
56
57 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
58
59 DIR=${DIR:-$MOUNT}
60 DIR2=${DIR2:-$MOUNT2}
61
62 QUOTA_AUTO_OLD=$QUOTA_AUTO
63 export QUOTA_AUTO=0
64
65 check_and_setup_lustre
66
67 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
68 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
69
70 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
71 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
72 SHOW_QUOTA_USER2="$LFS quota -v -u $TSTUSR2 $DIR"
73 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
74 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
75 SHOW_QUOTA_GROUP2="$LFS quota -v -g $TSTUSR2 $DIR"
76 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
77 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
78
79 # control the time of tests
80 cycle=30
81 [ "$SLOW" = "no" ] && cycle=10
82
83 build_test_filter
84
85 # set_blk_tunables(btune_sz)
86 set_blk_tunesz() {
87         local btune=$(($1 * BLK_SZ))
88         # set btune size on all obdfilters
89         do_nodes $(comma_list $(osts_nodes)) "lctl set_param lquota.${FSNAME}-OST*.quota_btune_sz=$btune"
90         # set btune size on mds
91         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.quota_btune_sz=$btune"
92 }
93
94 # set_blk_unitsz(bunit_sz)
95 set_blk_unitsz() {
96         local bunit=$(($1 * BLK_SZ))
97         # set bunit size on all obdfilters
98         do_nodes $(comma_list $(osts_nodes)) "lctl set_param lquota.${FSNAME}-OST*.quota_bunit_sz=$bunit"
99         # set bunit size on mds
100         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.quota_bunit_sz=$bunit"
101 }
102
103 # set_file_tunesz(itune_sz)
104 set_file_tunesz() {
105         local itune=$1
106         # set itune size on mds
107         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.quota_itune_sz=$itune"
108 }
109
110 # set_file_unitsz(iunit_sz)
111 set_file_unitsz() {
112         local iunit=$1
113         # set iunit size on mds
114         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.quota_iunit_sz=$iunit"
115 }
116
117 lustre_fail() {
118         local fail_node=$1
119         local fail_loc=$2
120         local fail_val=${3:-0}
121
122         if [ $fail_node == "mds" ] || [ $fail_node == "mds_ost" ]; then
123             if [ $((fail_loc & 0x10000000)) -ne 0  -a $fail_val -gt 0 ] || \
124                 [ $((fail_loc)) -eq 0 ]; then
125                 do_facet $SINGLEMDS "lctl set_param fail_val=$fail_val"
126             fi
127             do_facet $SINGLEMDS "lctl set_param fail_loc=$fail_loc"
128         fi
129         if [ $fail_node == "ost" ] || [ $fail_node == "mds_ost" ]; then
130             for num in `seq $OSTCOUNT`; do
131                 if [ $((fail_loc & 0x10000000)) -ne 0 -a $fail_val -gt 0 ] || \
132                     [ $((fail_loc)) -eq 0 ]; then
133                     do_facet ost$num "lctl set_param fail_val=$fail_val"
134                 fi
135                 do_facet ost$num "lctl set_param fail_loc=$fail_loc"
136             done
137         fi
138 }
139
140 RUNAS="runas -u $TSTID -g $TSTID"
141 RUNAS2="runas -u $TSTID2 -g $TSTID2"
142 FAIL_ON_ERROR=true check_runas_id $TSTID $TSTID $RUNAS
143 FAIL_ON_ERROR=true check_runas_id $TSTID2 $TSTID2 $RUNAS2
144
145 FAIL_ON_ERROR=false
146
147 run_test_with_stat() {
148         (($# != 2)) && error "the number of arguments is wrong"
149
150         if [ "$USE_OFD" == "yes" ]; then
151                 run_test "$@"
152                 return
153         fi
154
155         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.stats=0" > /dev/null
156         for j in `seq $OSTCOUNT`; do
157             do_facet ost$j "lctl set_param lquota.${FSNAME}-OST*.stats=0" > /dev/null
158         done
159         run_test "$@"
160         if [ ${STAT:-"yes"} != "no" -a -z "$LAST_SKIPPED" ]; then
161             echo "statistics info begin ***************************************"
162             do_facet $SINGLEMDS "lctl get_param lquota.mdd_obd-${FSNAME}-MDT*.stats"
163             for j in `seq $OSTCOUNT`; do
164                 do_facet ost$j "lctl get_param lquota.${FSNAME}-OST*.stats"
165             done
166             echo "statistics info end   ***************************************"
167         fi
168 }
169
170 #
171 # clear quota limits for a user or a group
172 # usage: resetquota -u username
173 #        resetquota -g groupname
174
175 resetquota() {
176         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
177         [ "$1" != "-u" -a "$1" != "-g" ] && error "resetquota: wrong specifier $1 passed"
178
179         count=0
180         if at_is_enabled; then
181             timeout=$(at_max_get mds)
182         else
183             timeout=$(lctl get_param -n timeout)
184         fi
185
186         while [ $((count++)) -lt $timeout ]; do
187                 local RC=0
188                 OUTPUT=`$LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT 2>&1` || RC=${PIPESTATUS[0]}
189                 if [ $RC -ne 0 ]; then
190                         if echo "$OUTPUT" | grep -q busy; then
191                                 log "resetquota is blocked for quota master recovery, retry after $((count * 3)) sec"
192                                 sleep 3
193                                 continue
194                         else
195                                 error "resetquota failed"
196                         fi
197                 fi
198                 break
199         done
200
201         [ $count -lt $timeout ] || error "resetquota timeout: $timeout"
202 }
203
204 quota_scan() {
205         LOCAL_UG=$1
206         LOCAL_ID=$2
207
208         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "u" ]; then
209                 log "Files for user ($LOCAL_ID):"
210                 ($LFS find -user $LOCAL_ID $DIR | xargs stat 2>/dev/null)
211         fi
212
213         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "g" ]; then
214                 log "Files for group ($LOCAL_ID):"
215                 ($LFS find -group $LOCAL_ID $DIR | xargs stat 2>/dev/null)
216         fi
217 }
218
219 quota_error() {
220         quota_scan $1 $2
221         shift 2
222         error "$*"
223 }
224
225 quota_log() {
226         quota_scan $1 $2
227         shift 2
228         log "$*"
229 }
230
231 #
232 # get quota info for a user or a group
233 # usage: getquota -u|-g <username>|<groupname> global|<obd_uuid> bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
234 #
235 getquota() {
236         local spec
237         local uuid
238
239         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
240         [ "$1" != "-u" -a "$1" != "-g" ] && error "getquota: wrong u/g specifier $1 passed"
241
242         uuid="$3"
243
244         case "$4" in
245                 curspace)   spec=1;;
246                 bsoftlimit) spec=2;;
247                 bhardlimit) spec=3;;
248                 bgrace)     spec=4;;
249                 curinodes)  spec=5;;
250                 isoftlimit) spec=6;;
251                 ihardlimit) spec=7;;
252                 igrace)     spec=8;;
253                 *)          error "unknown quota parameter $4";;
254         esac
255
256         [ "$uuid" = "global" ] && uuid=$DIR
257
258         $LFS quota -v "$1" "$2" $DIR | awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") { if (NF == 1) { getline } else { num++ } ; print $num;} }' | tr -d "*"
259 }
260
261 quota_show_check() {
262         LOCAL_BF=$1
263         LOCAL_UG=$2
264         LOCAL_ID=$3
265         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
266
267         $LFS quota -v -$LOCAL_UG $LOCAL_ID $DIR
268
269         if [ "$LOCAL_BF" == "a" -o "$LOCAL_BF" == "b" ]; then
270                 USAGE=`getquota -$LOCAL_UG $LOCAL_ID global curspace`
271                 if [ -z $USAGE ]; then
272                         quota_error $LOCAL_UG $LOCAL_ID "System is error when query quota for block ($LOCAL_UG:$LOCAL_ID)."
273                 else
274                         [ $USAGE -ne 0 ] && quota_log $LOCAL_UG $LOCAL_ID "System is not clean for block ($LOCAL_UG:$LOCAL_ID:$USAGE)."
275                 fi
276         fi
277
278         if [ "$LOCAL_BF" == "a" -o "$LOCAL_BF" == "f" ]; then
279                 USAGE=`getquota -$LOCAL_UG $LOCAL_ID global curinodes`
280                 if [ -z $USAGE ]; then
281                         quota_error $LOCAL_UG $LOCAL_ID "System is error when query quota for file ($LOCAL_UG:$LOCAL_ID)."
282                 else
283                         [ $USAGE -ne 0 ] && quota_log $LOCAL_UG $LOCAL_ID "System is not clean for file ($LOCAL_UG:$LOCAL_ID:$USAGE)."
284                 fi
285         fi
286 }
287
288 # set quota
289 quota_init() {
290         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
291
292         log "do the quotacheck ..."
293         $LFS quotacheck -ug $DIR
294
295         resetquota -u $TSTUSR
296         resetquota -g $TSTUSR
297 }
298 quota_init
299
300 test_quota_performance() {
301         TESTFILE="$DIR/$tdir/$tfile-0"
302         local size=$1
303         local stime=`date +%s`
304         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1M count=$size || quota_error u $TSTUSR "write failure"
305         local etime=`date +%s`
306         delta=$((etime - stime))
307         if [ $delta -gt 0 ]; then
308             rate=$((size * 1024 / delta))
309             [ $rate -gt 1024 ] || error "SLOW IO for $TSTUSR (user): $rate KB/sec"
310         fi
311         rm -f $TESTFILE
312 }
313
314 # test basic quota performance b=21696
315 test_0() {
316         mkdir -p $DIR/$tdir
317         chmod 0777 $DIR/$tdir
318         MB=100
319         [ "$SLOW" = "no" ] && MB=10
320
321         test_quota_performance $MB
322
323         $LFS setquota -u $TSTUSR -b 0 -B $((1024*1024)) -i 0 -I 0 $DIR
324         test_quota_performance $MB
325
326         resetquota -u $TSTUSR
327 }
328 run_test_with_stat 0 "Test basic quota performance ==="
329
330 # test for specific quota limitation, qunit, qtune $1=block_quota_limit
331 test_1_sub() {
332         LIMIT=$1
333         mkdir -p $DIR/$tdir
334         chmod 0777 $DIR/$tdir
335         TESTFILE="$DIR/$tdir/$tfile-0"
336
337         wait_delete_completed
338
339         # test for user
340         log "  User quota (limit: $LIMIT kbytes)"
341         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
342         sleep 3
343         quota_show_check b u $TSTUSR
344
345         $LFS setstripe $TESTFILE -c 1
346         chown $TSTUSR.$TSTUSR $TESTFILE
347
348         log "    Write ..."
349         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) || quota_error u $TSTUSR "(usr) write failure, but expect success"
350         log "    Done"
351         log "    Write out of block quota ..."
352         # this time maybe cache write,  ignore it's failure
353         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) seek=$(($LIMIT/2)) || true
354         # flush cache, ensure noquota flag is setted on client
355         cancel_lru_locks osc
356         $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"
357
358         rm -f $TESTFILE
359         sync; sleep 1; sync;
360         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
361         OST0_QUOTA_USED=`getquota -u $TSTUSR $OST0_UUID curspace`
362         echo $OST0_QUOTA_USED
363         [ $OST0_QUOTA_USED -ne 0 ] && \
364             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "(usr) quota deleted isn't released")
365         $SHOW_QUOTA_USER
366         resetquota -u $TSTUSR
367
368         # test for group
369         log "--------------------------------------"
370         log "  Group quota (limit: $LIMIT kbytes)"
371         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
372         sleep 3
373         quota_show_check b g $TSTUSR
374         TESTFILE="$DIR/$tdir/$tfile-1"
375
376         $LFS setstripe $TESTFILE -c 1
377         chown $TSTUSR.$TSTUSR $TESTFILE
378
379         log "    Write ..."
380         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) || quota_error g $TSTUSR "(grp) write failure, but expect success"
381         log "    Done"
382         log "    Write out of block quota ..."
383         # this time maybe cache write, ignore it's failure
384         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT/2)) seek=$(($LIMIT/2)) || true
385         cancel_lru_locks osc
386         $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"
387
388         # cleanup
389         rm -f $TESTFILE
390         sync; sleep 1; sync;
391         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
392         OST0_QUOTA_USED=`getquota -g $TSTUSR $OST0_UUID curspace`
393         echo $OST0_QUOTA_USED
394         [ $OST0_QUOTA_USED -ne 0 ] && \
395             ($SHOW_QUOTA_GROUP; quota_error g $TSTUSR "(grp) quota deleted isn't released")
396         $SHOW_QUOTA_GROUP
397         resetquota -g $TSTUSR
398 }
399
400 # block hard limit (normal use and out of quota)
401 test_1() {
402         for i in `seq 1 $cycle`; do
403             # define blk_qunit is between 1M and 4M
404             blk_qunit=$(( $RANDOM % 3072 + 1024 ))
405             blk_qtune=$(( $RANDOM % $blk_qunit ))
406             # other osts and mds will occupy at 1M blk quota
407             b_limit=$(( ($RANDOM - 16384) / 8 +  ($OSTCOUNT + 1) * $blk_qunit * 4 ))
408             set_blk_tunesz $blk_qtune
409             set_blk_unitsz $blk_qunit
410             echo "cycle: $i(total $cycle) bunit:$blk_qunit, btune:$blk_qtune, blimit:$b_limit"
411             test_1_sub $b_limit
412             echo "=================================================="
413             set_blk_unitsz $((128 * 1024))
414             set_blk_tunesz $((128 * 1024 / 2))
415         done
416 }
417 run_test_with_stat 1 "Block hard limit (normal use and out of quota) ==="
418
419 # test for specific quota limitation, qunit, qtune $1=block_quota_limit
420 test_2_sub() {
421         LIMIT=$1
422         mkdir -p $DIR/$tdir
423         chmod 0777 $DIR/$tdir
424         TESTFILE="$DIR/$tdir/$tfile-0"
425
426         wait_delete_completed
427
428         # test for user
429         log "  User quota (limit: $LIMIT files)"
430         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
431         sleep 3
432         quota_show_check f u $TSTUSR
433
434         log "    Create $LIMIT files ..."
435         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
436                 quota_error u $TSTUSR "(usr) create failure, but expect success"
437         log "    Done"
438         log "    Create out of file quota ..."
439         $RUNAS touch ${TESTFILE}_xxx && \
440                 quota_error u $TSTUSR "(usr) touch success, but expect EDQUOT"
441
442         unlinkmany ${TESTFILE} $LIMIT
443         rm -f ${TESTFILE}_xxx
444         sync; sleep 1; sync;
445
446         MDS_UUID=`do_facet $SINGLEMDS $LCTL dl | grep -m1 " mdt " | awk '{print $((NF-1))}'`
447         MDS_QUOTA_USED=`getquota -u $TSTUSR $MDS_UUID curinodes`
448         echo $MDS_QUOTA_USED
449         [ $MDS_QUOTA_USED -ne 0 ] && \
450             ($SHOW_QUOTA_USER; quota_error u $TSTUSR "(usr) quota deleted isn't released")
451         $SHOW_QUOTA_USER
452         resetquota -u $TSTUSR
453
454         # test for group
455         log "--------------------------------------"
456         log "  Group quota (limit: $LIMIT FILE)"
457         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
458         sleep 3
459         quota_show_check f g $TSTUSR
460         TESTFILE=$DIR/$tdir/$tfile-1
461
462         log "    Create $LIMIT files ..."
463         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
464                 quota_error g $TSTUSR "(grp) create failure, but expect success"
465         log "    Done"
466         log "    Create out of file quota ..."
467         $RUNAS touch ${TESTFILE}_xxx && \
468                 quota_error g $TSTUSR "(grp) touch success, but expect EDQUOT"
469
470         unlinkmany ${TESTFILE} $LIMIT
471         rm -f ${TESTFILE}_xxx
472         sync; sleep 1; sync;
473
474         MDS_UUID=`do_facet $SINGLEMDS $LCTL dl | grep -m1 " mdt " | awk '{print $((NF-1))}'`
475         MDS_QUOTA_USED=`getquota -g $TSTUSR $MDS_UUID curinodes`
476         echo $MDS_QUOTA_USED
477         [ $MDS_QUOTA_USED -ne 0 ] && \
478             ($SHOW_QUOTA_GROUP; quota_error g $TSTUSR "(grp) quota deleted isn't released")
479         $SHOW_QUOTA_GROUP
480         resetquota -g $TSTUSR
481 }
482
483 # file hard limit (normal use and out of quota)
484 test_2() {
485         for i in `seq 1 $cycle`; do
486             if [ $i -eq 1 ]; then
487                 ino_qunit=52
488                 ino_qtune=41
489                 i_limit=11
490             else
491                 # define ino_qunit is between 10 and 100
492                 ino_qunit=$(( $RANDOM % 90 + 10 ))
493                 ino_qtune=$(( $RANDOM % $ino_qunit ))
494                 # RANDOM's maxium is 32767
495                 i_limit=$(( $RANDOM % 990 + 10 ))
496             fi
497
498             set_file_tunesz $ino_qtune
499             set_file_unitsz $ino_qunit
500             echo "cycle: $i(total $cycle) iunit:$ino_qunit, itune:$ino_qtune, ilimit:$i_limit"
501             test_2_sub $i_limit
502             echo "=================================================="
503             set_file_unitsz 5120
504             set_file_tunesz 2560
505         done
506 }
507 run_test_with_stat 2 "File hard limit (normal use and out of quota) ==="
508
509 test_block_soft() {
510         TESTFILE=$1
511         TIMER=$(($2 * 3 / 2))
512         OFFSET=0
513
514         wait_delete_completed
515
516         echo "    Write to exceed soft limit"
517         RUNDD="$RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ"
518         $RUNDD count=$((BUNIT_SZ+1)) || \
519                 quota_error a $TSTUSR "write failure, but expect success"
520         OFFSET=$((OFFSET + BUNIT_SZ + 1))
521         cancel_lru_locks osc
522
523         $SHOW_QUOTA_USER
524         $SHOW_QUOTA_GROUP
525         $SHOW_QUOTA_INFO_USER
526         $SHOW_QUOTA_INFO_GROUP
527
528         echo "    Write before timer goes off"
529         $RUNDD count=$BUNIT_SZ seek=$OFFSET || \
530                 quota_error a $TSTUSR "write failure, but expect success"
531         OFFSET=$((OFFSET + BUNIT_SZ))
532         cancel_lru_locks osc
533         echo "    Done"
534
535         echo "    Sleep $TIMER seconds ..."
536         sleep $TIMER
537
538         $SHOW_QUOTA_USER
539         $SHOW_QUOTA_GROUP
540         $SHOW_QUOTA_INFO_USER
541         $SHOW_QUOTA_INFO_GROUP
542
543         echo "    Write after timer goes off"
544         # maybe cache write, ignore.
545         $RUNDD count=$BUNIT_SZ seek=$OFFSET || true
546         OFFSET=$((OFFSET + BUNIT_SZ))
547         cancel_lru_locks osc
548         $RUNDD count=$BUNIT_SZ seek=$OFFSET && \
549                 quota_error a $TSTUSR "write success, but expect EDQUOT"
550
551         $SHOW_QUOTA_USER
552         $SHOW_QUOTA_GROUP
553         $SHOW_QUOTA_INFO_USER
554         $SHOW_QUOTA_INFO_GROUP
555
556         echo "    Unlink file to stop timer"
557         rm -f $TESTFILE
558         sync; sleep 1; sync
559         echo "    Done"
560
561         $SHOW_QUOTA_USER
562         $SHOW_QUOTA_GROUP
563         $SHOW_QUOTA_INFO_USER
564         $SHOW_QUOTA_INFO_GROUP
565
566         echo "    Write ..."
567         $RUNDD count=$BUNIT_SZ || quota_error a $TSTUSR "write failure, but expect success"
568         echo "    Done"
569
570         # cleanup
571         rm -f $TESTFILE
572         sync; sleep 3; sync;
573 }
574
575 # block soft limit (start timer, timer goes off, stop timer)
576 test_3() {
577         mkdir -p $DIR/$tdir
578         chmod 0777 $DIR/$tdir
579
580         # 1 bunit on mds and 1 bunit on every ost
581         LIMIT=$(( $BUNIT_SZ * ($OSTCOUNT + 1) ))
582         GRACE=10
583
584         echo "  User quota (soft limit: $LIMIT kbytes  grace: $GRACE seconds)"
585         TESTFILE=$DIR/$tdir/$tfile-0
586
587         $LFS setstripe $TESTFILE -c 1
588         chown $TSTUSR.$TSTUSR $TESTFILE
589
590         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
591         $LFS setquota -u $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
592
593         test_block_soft $TESTFILE $GRACE
594         resetquota -u $TSTUSR
595
596         echo "  Group quota (soft limit: $LIMIT kbytes  grace: $GRACE seconds)"
597         TESTFILE=$DIR/$tdir/$tfile-1
598
599         $LFS setstripe $TESTFILE -c 1
600         chown $TSTUSR.$TSTUSR $TESTFILE
601
602         $LFS setquota -t -g --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
603         $LFS setquota -g $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
604
605         test_block_soft $TESTFILE $GRACE
606         resetquota -g $TSTUSR
607 }
608 run_test_with_stat 3 "Block soft limit (start timer, timer goes off, stop timer) ==="
609
610 test_file_soft() {
611         TESTFILE=$1
612         LIMIT=$2
613         TIMER=$(($3 * 3 / 2))
614
615         wait_delete_completed
616
617         echo "    Create files to exceed soft limit"
618         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) || \
619                 quota_error a $TSTUSR "create failure, but expect success"
620         sync; sleep 1; sync
621         echo "    Done"
622
623         echo "    Create file before timer goes off"
624         $RUNAS touch ${TESTFILE}_before || \
625                 quota_error a $TSTUSR "failed create before timer expired, but expect success"
626         sync; sleep 1; sync
627         echo "    Done"
628
629         echo "    Sleep $TIMER seconds ..."
630         sleep $TIMER
631
632         $SHOW_QUOTA_USER
633         $SHOW_QUOTA_GROUP
634         $SHOW_QUOTA_INFO_USER
635         $SHOW_QUOTA_INFO_GROUP
636
637         echo "    Create file after timer goes off"
638         # the least of inode qunit is 2, so there are at most 3(qunit:2+qtune:1)
639         # inode quota left here
640         $RUNAS touch ${TESTFILE}_after ${TESTFILE}_after1 ${TESTFILE}_after2 || true
641         sync; sleep 1; sync
642         $RUNAS touch ${TESTFILE}_after3 && \
643                 quota_error a $TSTUSR "create after timer expired, but expect EDQUOT"
644         sync; sleep 1; sync
645
646         $SHOW_QUOTA_USER
647         $SHOW_QUOTA_GROUP
648         $SHOW_QUOTA_INFO_USER
649         $SHOW_QUOTA_INFO_GROUP
650
651         echo "    Unlink files to stop timer"
652         find `dirname $TESTFILE` -name "`basename ${TESTFILE}`*" | xargs rm -f
653         echo "    Done"
654
655         echo "    Create file"
656         $RUNAS touch ${TESTFILE}_xxx || \
657                 quota_error a $TSTUSR "touch after timer stop failure, but expect success"
658         sync; sleep 1; sync
659         echo "    Done"
660
661         # cleanup
662         rm -f ${TESTFILE}_xxx
663         sync; sleep 3; sync;
664 }
665
666 # file soft limit (start timer, timer goes off, stop timer)
667 test_4a() {     # was test_4
668         mkdir -p $DIR/$tdir
669         chmod 0777 $DIR/$tdir
670         LIMIT=$(($IUNIT_SZ * 10))       # 10 iunits on mds
671         TESTFILE=$DIR/$tdir/$tfile-0
672
673         GRACE=5
674
675         echo "  User quota (soft limit: $LIMIT files  grace: $GRACE seconds)"
676         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
677         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
678         quota_show_check f u $TSTUSR
679
680         test_file_soft $TESTFILE $LIMIT $GRACE
681         resetquota -u $TSTUSR
682
683         echo "  Group quota (soft limit: $LIMIT files  grace: $GRACE seconds)"
684         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
685         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
686         quota_show_check f g $TSTUSR
687         TESTFILE=$DIR/$tdir/$tfile-1
688
689         test_file_soft $TESTFILE $LIMIT $GRACE
690         resetquota -g $TSTUSR
691
692         # cleanup
693         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
694         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
695 }
696 run_test_with_stat 4a "File soft limit (start timer, timer goes off, stop timer) ==="
697
698 test_4b() {     # was test_4a
699         GR_STR1="1w3d"
700         GR_STR2="1000s"
701         GR_STR3="5s"
702         GR_STR4="1w2d3h4m5s"
703         GR_STR5="5c"
704         GR_STR6="1111111111111111"
705
706         wait_delete_completed
707
708         # test of valid grace strings handling
709         echo "  Valid grace strings test"
710         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace $GR_STR2 $DIR
711         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
712         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace $GR_STR4 $DIR
713         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
714
715         # test of invalid grace strings handling
716         echo "  Invalid grace strings test"
717         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
718         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
719
720         # cleanup
721         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
722         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
723 }
724 run_test_with_stat 4b "Grace time strings handling ==="
725
726 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
727 test_5() {
728         mkdir -p $DIR/$tdir
729         BLIMIT=$(( $BUNIT_SZ * $((OSTCOUNT + 1)) * 10)) # 10 bunits on each server
730         ILIMIT=$(( $IUNIT_SZ * 10 )) # 10 iunits on mds
731
732         wait_delete_completed
733
734         echo "  Set quota limit (0 $BLIMIT 0 $ILIMIT) for $TSTUSR.$TSTUSR"
735         $LFS setquota -u $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $DIR
736         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $DIR
737         quota_show_check a u $TSTUSR
738         quota_show_check a g $TSTUSR
739
740         echo "  Create more than $ILIMIT files and more than $BLIMIT kbytes ..."
741         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) || \
742                 error "touch failure, expect success"
743         dd if=/dev/zero of=$DIR/$tdir/$tfile-0_1 bs=$BLK_SZ count=$((BLIMIT+1)) || error "write failure, expect success"
744
745         echo "  Chown files to $TSTUSR.$TSTUSR ..."
746         for i in `seq 0 $ILIMIT`; do
747         chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i || \
748                         quota_error a $TSTUSR "chown failure, but expect success"
749         done
750
751         # cleanup
752         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
753         sync; sleep 3; sync;
754
755         resetquota -u $TSTUSR
756         resetquota -g $TSTUSR
757 }
758 run_test_with_stat 5 "Chown & chgrp successfully even out of block/file quota ==="
759
760 # block quota acquire & release
761 test_6() {
762         if [ $OSTCOUNT -lt 2 ]; then
763                 skip_env "$OSTCOUNT < 2, too few osts"
764                 return 0;
765         fi
766
767         wait_delete_completed
768
769         mkdir -p $DIR/$tdir
770         chmod 0777 $DIR/$tdir
771
772         LIMIT=$((BUNIT_SZ * (OSTCOUNT + 1) * 5)) # 5 bunits per server
773         FILEA="$DIR/$tdir/$tfile-0_a"
774         FILEB="$DIR/$tdir/$tfile-0_b"
775
776         echo "  Set block limit $LIMIT kbytes to $TSTUSR.$TSTUSR"
777         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
778         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
779         quota_show_check b u $TSTUSR
780         quota_show_check b g $TSTUSR
781
782         echo "  Create filea on OST0 and fileb on OST1"
783         $LFS setstripe $FILEA -i 0 -c 1
784         $LFS setstripe $FILEB -i 1 -c 1
785         chown $TSTUSR.$TSTUSR $FILEA
786         chown $TSTUSR.$TSTUSR $FILEB
787
788         echo "  Exceed quota limit ..."
789         RUNDD="$RUNAS dd if=/dev/zero of=$FILEA bs=$BLK_SZ"
790         $RUNDD count=$((LIMIT - BUNIT_SZ * OSTCOUNT)) || \
791                 quota_error a $TSTUSR "write filea failure, but expect success"
792
793         cancel_lru_locks osc
794         $SHOW_QUOTA_USER
795         $SHOW_QUOTA_GROUP
796         $RUNDD seek=$LIMIT count=$((BUNIT_SZ * OSTCOUNT)) && \
797                 quota_error a $TSTUSR "write filea success, but expect EDQUOT"
798         cancel_lru_locks osc
799         echo "  Write to OST1 return EDQUOT"
800         # this write maybe cache write, ignore it's failure
801         RUNDD="$RUNAS dd if=/dev/zero of=$FILEB bs=$BLK_SZ"
802         $RUNDD count=$(($BUNIT_SZ * 2)) || true
803         cancel_lru_locks osc
804         $SHOW_QUOTA_USER
805         $SHOW_QUOTA_GROUP
806         $RUNDD count=$((BUNIT_SZ * 2)) seek=$((BUNIT_SZ *2)) && \
807                 quota_error a $TSTUSR "write fileb success, but expect EDQUOT"
808
809         echo "  Remove filea to let OST0 release quota"
810         rm -f $FILEA
811
812         if at_is_enabled; then
813             timeout=$(at_max_get mds)
814         else
815             timeout=$(lctl get_param -n timeout)
816         fi
817         count=$((timeout / 5))
818         OST0_UUID=`do_facet ost1 $LCTL dl | grep -m1 obdfilter | awk '{print $((NF-1))}'`
819
820         while [ $((count--)) -gt 0 ]; do
821                 sync && sleep 5
822
823                 OST0_QUOTA_HOLD=`getquota -u $TSTUSR $OST0_UUID bhardlimit`
824                 if [ -z $OST0_QUOTA_HOLD ]; then
825                         error "System is error when query quota for block (U:$TSTUSR)."
826                 else
827                         [ $OST0_QUOTA_HOLD -gt $BUNIT_SZ ] && continue
828                 fi
829
830                 break
831         done
832
833         [ ! $count -gt 0 ] && error "Release quota for block timeout (U:$TSTUSR)."
834         $SHOW_QUOTA_USER
835
836         while [ $((count--)) -gt 0 ]; do
837                 sync && sleep 5
838
839                 OST0_QUOTA_HOLD=`getquota -g $TSTUSR $OST0_UUID bhardlimit`
840                 if [ -z $OST0_QUOTA_HOLD ]; then
841                         error "System is error when query quota for block (G:$TSTUSR)."
842                 else
843                         [ $OST0_QUOTA_HOLD -gt $BUNIT_SZ ] && continue
844                 fi
845
846                 break
847         done
848
849         [ ! $count -gt 0 ] && error "Release quota for block timeout (G:$TSTUSR)."
850         $SHOW_QUOTA_GROUP
851
852         echo "  Write to OST1"
853         $RUNDD count=$((LIMIT - BUNIT_SZ * OSTCOUNT)) || \
854                 quota_error a $TSTUSR "write fileb failure, expect success"
855         echo "  Done"
856
857         # cleanup
858         rm -f $FILEB
859         sync; sleep 3; sync;
860
861         resetquota -u $TSTUSR
862         resetquota -g $TSTUSR
863         return 0
864 }
865 run_test_with_stat 6 "Block quota acquire & release ========="
866
867 # quota recovery (block quota only by now)
868 test_7()
869 {
870         mkdir -p $DIR/$tdir
871         chmod 0777 $DIR/$tdir
872
873         wait_delete_completed
874
875         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) ))
876         TESTFILE="$DIR/$tdir/$tfile-0"
877
878         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
879
880         $LFS setstripe $TESTFILE -c 1
881         chown $TSTUSR.$TSTUSR $TESTFILE
882
883         echo "  Write to OST0..."
884         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$BUNIT_SZ || \
885                 quota_error u $TSTUSR "write failure, but expect success"
886
887         #define OBD_FAIL_OBD_DQACQ               0x604
888         lustre_fail mds  0x604
889         echo "  Remove files on OST0"
890         rm -f $TESTFILE
891         lustre_fail mds  0
892
893         echo "  Trigger recovery..."
894         OSC0_UUID="`$LCTL dl | awk '$3 ~ /osc/ { print $1 }'`"
895         for i in $OSC0_UUID; do
896                 $LCTL --device $i activate || error "activate osc failed!"
897         done
898
899         # sleep a while to wait for recovery done
900         sleep 20
901
902         # check limits
903         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
904         TOTAL_LIMIT=`getquota -u $TSTUSR global bhardlimit`
905         [ $TOTAL_LIMIT -eq $LIMIT ] || error "total limits not recovery!"
906         echo "  total limits = $TOTAL_LIMIT"
907
908         OST0_UUID=`do_facet ost1 "$LCTL dl | grep -m1 obdfilter" | awk '{print $((NF-1))}'`
909         [ -z "$OST0_UUID" ] && OST0_UUID=`do_facet ost1 "$LCTL dl | grep -m1 obdfilter" | awk '{print $((NF-1))}'`
910         OST0_LIMIT=`getquota  -u $TSTUSR $OST0_UUID bhardlimit`
911         [ $OST0_LIMIT -eq $BUNIT_SZ ] || error "high limits not released!"
912         echo "  limits on $OST0_UUID = $OST0_LIMIT"
913
914         # cleanup
915         resetquota -u $TSTUSR
916 }
917 run_test_with_stat 7 "Quota recovery (only block limit) ======"
918
919 # run dbench with quota enabled
920 test_8() {
921         mkdir -p $DIR/$tdir
922         BLK_LIMIT=$((100 * 1024 * 1024)) # 100G
923         FILE_LIMIT=1000000
924
925         wait_delete_completed
926
927         echo "  Set enough high limit for user: $TSTUSR"
928         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
929         echo "  Set enough high limit for group: $TSTUSR"
930         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
931
932         chmod 0777 $DIR/$tdir
933         local duration=""
934         [ "$SLOW" = "no" ] && duration=" -t 120"
935         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration || quota_error a $TSTUSR "dbench failed!"
936
937         rm -rf $DIR/$tdir
938         sync; sleep 3; sync;
939
940         return 0
941 }
942 run_test_with_stat 8 "Run dbench with quota enabled ==========="
943
944 # run for fixing bug10707, it needs a big room. test for 64bit
945 KB=1024
946 GB=$((KB * 1024 * 1024))
947 # Use this as dd bs to decrease time
948 # inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
949 blksize=$((1 << 21)) # 2Mb
950 size_file=$((GB * 9 / 2))
951 # this check is just for test_9
952 OST0_MIN=4900000 #4.67G
953 check_whether_skip () {
954     OST0_SIZE=`$LFS df $DIR | awk '/\[OST:0\]/ {print $4}'`
955     log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
956     if [ $OST0_SIZE -lt $OST0_MIN ]; then
957         echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
958         return 0
959     else
960         return 1
961     fi
962 }
963
964 test_9() {
965         check_whether_skip && return 0
966
967         wait_delete_completed
968
969         set_blk_tunesz 512
970         set_blk_unitsz 1024
971
972         mkdir -p $DIR/$tdir
973         chmod 0777 $DIR/$tdir
974         TESTFILE="$DIR/$tdir/$tfile-0"
975
976         BLK_LIMIT=$((100 * KB * KB)) # 100G
977         FILE_LIMIT=1000000
978         echo "  Set block limit $BLK_LIMIT kbytes to $TSTUSR.$TSTUSR"
979
980         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for user: $TSTUSR"
981         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
982         log "  Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT) for group: $TSTUSR"
983         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
984
985         quota_show_check a u $TSTUSR
986         quota_show_check a g $TSTUSR
987
988         echo "  Set stripe"
989         $LFS setstripe $TESTFILE -c 1 -i 0
990         touch $TESTFILE
991         chown $TSTUSR.$TSTUSR $TESTFILE
992
993         log "    Write the big file of 4.5G ..."
994         $RUNAS dd if=/dev/zero of=$TESTFILE  bs=$blksize count=$((size_file / blksize)) || \
995                quota_error a $TSTUSR "(usr) write 4.5G file failure, but expect success"
996
997         $SHOW_QUOTA_USER
998         $SHOW_QUOTA_GROUP
999
1000         log "    delete the big file of 4.5G..."
1001         $RUNAS rm -f $TESTFILE
1002         sync; sleep 3; sync;
1003
1004         $SHOW_QUOTA_USER
1005         $SHOW_QUOTA_GROUP
1006
1007         RC=$?
1008
1009         set_blk_unitsz $((128 * 1024))
1010         set_blk_tunesz $((128 * 1024 / 2))
1011
1012         return $RC
1013 }
1014 run_test_with_stat 9 "run for fixing bug10707(64bit) ==========="
1015
1016 # 2.0 version does not support 32 bit qd_count,
1017 # test_10 "run for fixing bug10707(32bit) " is obsolete
1018
1019 # test a deadlock between quota and journal b=11693
1020 test_12() {
1021         mkdir -p $DIR/$tdir
1022         chmod 0777 $DIR/$tdir
1023
1024         [ "$(grep $DIR2 /proc/mounts)" ] || mount_client $DIR2 || \
1025                 { skip_env "Need lustre mounted on $MOUNT2 " && retutn 0; }
1026
1027         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) * 10)) # 10 bunits each sever
1028         TESTFILE="$DIR/$tdir/$tfile-0"
1029         TESTFILE2="$DIR2/$tdir/$tfile-1"
1030
1031         wait_delete_completed
1032
1033         echo "   User quota (limit: $LIMIT kbytes)"
1034         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1035
1036         $LFS setstripe $TESTFILE -i 0 -c 1
1037         chown $TSTUSR.$TSTUSR $TESTFILE
1038         $LFS setstripe $TESTFILE2 -i 0 -c 1
1039         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
1040
1041         #define OBD_FAIL_OST_HOLD_WRITE_RPC      0x21f
1042         #define OBD_FAIL_SOME        0x10000000 /* fail N times */
1043         lustre_fail ost $((0x0000021f | 0x10000000)) 1
1044
1045         echo "   step1: write out of block quota ..."
1046         $RUNAS2 dd if=/dev/zero of=$TESTFILE2 bs=$BLK_SZ count=102400 &
1047         DDPID1=$!
1048         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$(($LIMIT*2)) &
1049         DDPID=$!
1050
1051         echo  "   step2: testing ......"
1052         local last_size=$(stat -c %s $TESTFILE2) 
1053         local stall_secs=0
1054         local start_secs=$SECONDS
1055         while [ -d /proc/${DDPID1} ]; do
1056             local size=$(stat -c %s $TESTFILE2) 
1057             if [ $size -eq $last_size ]; then
1058                 stall_secs=$[stall_secs+1]
1059             else
1060                 stall_secs=0
1061             fi
1062             if [ $stall_secs -gt 30 ]; then
1063                 lustre_fail ost 0
1064                 quota_error u $TSTUSR2 "giving up: dd stalled (i.e. made no progress) for 30 seconds!"
1065             fi
1066             last_size=$size
1067             sleep 1
1068         done
1069         echo "(dd_pid=$DDPID1, time=$((SECONDS-start_secs)))successful"
1070
1071         #Recover fail_loc and dd will finish soon
1072         lustre_fail ost 0
1073
1074         echo  "   step3: testing ......"
1075         count=0
1076         while [ true ]; do
1077             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1078             count=$[count+1]
1079             if [ $count -gt 150 ]; then
1080                 quota_error u $TSTUSR "dd should be finished!"
1081             fi
1082             sleep 1
1083         done
1084         echo "(dd_pid=$DDPID, time=$count)successful"
1085
1086         rm -f $TESTFILE $TESTFILE2
1087         sync; sleep 3; sync;
1088
1089         resetquota -u $TSTUSR
1090 }
1091 run_test_with_stat 12 "test a deadlock between quota and journal ==="
1092
1093 # test multiple clients write block quota b=11693
1094 test_13() {
1095         mkdir -p $DIR/$tdir
1096         wait_delete_completed
1097
1098         # one OST * 10 + (mds + other OSTs)
1099         LIMIT=$((BUNIT_SZ * 10 + (BUNIT_SZ * OSTCOUNT)))
1100         TESTFILE="$DIR/$tdir/$tfile"
1101
1102         echo "   User quota (limit: $LIMIT kbytes)"
1103         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1104         quota_show_check b u $TSTUSR
1105
1106         $LFS setstripe $TESTFILE -i 0 -c 1
1107         chown $TSTUSR.$TSTUSR $TESTFILE
1108         $LFS setstripe $TESTFILE.2 -i 0 -c 1
1109         chown $TSTUSR.$TSTUSR $TESTFILE.2
1110
1111         echo "   step1: write out of block quota ..."
1112         # one bunit will give mds
1113         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=$[($LIMIT - $BUNIT_SZ) / 2] &
1114         DDPID=$!
1115         $RUNAS dd if=/dev/zero of=$TESTFILE.2 bs=$BLK_SZ count=$[($LIMIT - $BUNIT_SZ) / 2] &
1116         DDPID1=$!
1117
1118         echo  "   step2: testing ......"
1119         count=0
1120         while [ true ]; do
1121             if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1122             count=$[count+1]
1123             if [ $count -gt 64 ]; then
1124                 quota_error u $TSTUSR "dd should be finished!"
1125             fi
1126             sleep 1
1127         done
1128         echo "(dd_pid=$DDPID, time=$count)successful"
1129
1130         count=0
1131         while [ true ]; do
1132             if ! ps -p ${DDPID1} > /dev/null 2>&1 ; then break; fi
1133             count=$[count+1]
1134             if [ $count -gt 64 ]; then
1135                 quota_error u $TSTUSR "dd should be finished!"
1136             fi
1137             sleep 1
1138         done
1139         echo "(dd_pid=$DDPID1, time=$count)successful"
1140
1141         sync; sleep 5; sync;
1142
1143         echo  "   step3: checking ......"
1144         fz=`stat -c %s $TESTFILE`
1145         fz2=`stat -c %s $TESTFILE.2`
1146         $SHOW_QUOTA_USER
1147         [ $((fz + fz2)) -lt $((BUNIT_SZ * BLK_SZ * 10)) ] && \
1148                 quota_error u $TSTUSR "files too small $fz + $fz2 < $((BUNIT_SZ * BLK_SZ * 10))"
1149
1150         rm -f $TESTFILE $TESTFILE.2
1151         sync; sleep 3; sync;
1152
1153         resetquota -u $TSTUSR
1154 }
1155 run_test_with_stat 13 "test multiple clients write block quota ==="
1156
1157 check_if_quota_zero(){
1158         line=`$LFS quota -v -$1 $2 $DIR | wc -l`
1159         for i in `seq 3 $line`; do
1160             if [ $i -eq 3 ]; then
1161                 field="3 4 7 8"
1162             else
1163                 field="3 6"
1164             fi
1165             for j in $field; do
1166                 tmp=`$LFS quota -v -$1 $2 $DIR | sed -n ${i}p |
1167                      awk  '{print $'"$j"'}'`
1168                 [ -n "$tmp" ] && [ $tmp -ne 0 ] && $LFS quota -v -$1 $2 $DIR && \
1169                     error "quota on $2 isn't clean"
1170             done
1171         done
1172         echo "pass check_if_quota_zero"
1173 }
1174
1175 test_14a() {    # was test_14 b=12223 -- setting quota on root
1176         TESTFILE="$DIR/$tdir/$tfile"
1177
1178         # reboot the lustre
1179         sync; sleep 5; sync
1180         cleanup_and_setup_lustre
1181         quota_init
1182
1183         mkdir -p $DIR/$tdir
1184
1185         # out of root's file and block quota
1186         $LFS setquota -u root -b 10 -B 10 -i 10 -I 10 $DIR
1187         createmany -m ${TESTFILE} 20 || \
1188             quota_error u root "unexpected: user(root) create files failly!"
1189         $MULTIOP ${TESTFILE} oO_CREAT:O_WRONLY:O_DIRECT:w$((4096 * 4096))c || \
1190             quota_error u root "unexpected: user(root) write files failly!"
1191         chmod 666 $TESTFILE
1192         $RUNAS $MULTIOP ${TESTFILE} oO_WRONLY:O_APPEND:O_DIRECT:w$((4096 * 4096))c && \
1193             quota_error u root "unexpected: user(quota_usr) write a file successfully!"
1194
1195         # trigger the llog
1196         chmod 777 $DIR
1197         for i in `seq 1 10`; do $RUNAS touch ${TESTFILE}a_$i; done
1198         for i in `seq 1 10`; do $RUNAS rm -f ${TESTFILE}a_$i; done
1199
1200         # do the check
1201         dmesg | tail | grep "\-122" |grep llog_obd_origin_add && error "err -122 not found in dmesg"
1202         resetquota -u root
1203         #check_if_quota_zero u root
1204
1205         # clean
1206         unlinkmany ${TESTFILE} 15
1207         rm -f $TESTFILE
1208         sync; sleep 3; sync;
1209 }
1210 run_test_with_stat 14a "test setting quota on root ==="
1211
1212 test_15(){
1213         LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1214         PATTERN="`echo $DIR | sed 's/\//\\\\\//g'`"
1215
1216         wait_delete_completed
1217
1218         # test for user
1219         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1220         TOTAL_LIMIT=`getquota -u $TSTUSR global bhardlimit`
1221         [ $TOTAL_LIMIT -eq $LIMIT ] || error "  (user)total limits = $TOTAL_LIMIT; limit = $LIMIT, failed!"
1222         echo "  (user)total limits = $TOTAL_LIMIT; limit = $LIMIT, successful!"
1223         resetquota -u $TSTUSR
1224
1225         # test for group
1226         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1227         TOTAL_LIMIT=`getquota -g $TSTUSR global bhardlimit`
1228         [ $TOTAL_LIMIT -eq $LIMIT ] || error "  (group)total limits = $TOTAL_LIMIT; limit = $LIMIT, failed!"
1229         echo "  (group)total limits = $TOTAL_LIMIT; limit = $LIMIT, successful!"
1230         resetquota -g $TSTUSR
1231         $LFS quotaoff -ug $DIR
1232         do_facet $SINGLEMDS "lctl set_param lquota.mdd_obd-${FSNAME}-MDT*.quota_type=ug" | grep "error writing" && \
1233                 error "fail to set version for $SINGLEMDS"
1234         for j in `seq $OSTCOUNT`; do
1235                 do_facet ost$j "lctl set_param lquota.${FSNAME}-OST*.quota_type=ug" | grep "error writing" && \
1236                         error "fail to set version for ost$j"
1237         done
1238
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 at_max_val
1996         local pid
1997         local waited
1998         local deadline
1999
2000         at_max_val=10
2001
2002         if at_is_enabled; then
2003                 timeout=$(at_max_get client)
2004                 [ $(at_min_get client) -gt $at_max_val ] &&
2005                         at_max_val=$(at_min_get client)
2006                 at_max_set $at_max_val client
2007         else
2008                 timeout=$(lctl get_param -n timeout)
2009                 lctl set_param timeout=$at_max_val
2010         fi
2011         # actually send a RPC to make service at_current confined within at_max
2012         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR ||
2013                 error "should succeed"
2014
2015         #define OBD_FAIL_MDS_QUOTACTL_NET 0x12e
2016         lustre_fail mds 0x12e
2017
2018         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I 0 $DIR & pid=$!
2019
2020         deadline=$((2 * (($at_max_val << 1) + ($at_max_val >> 2) + 5)))
2021         echo "wait at most 2 * ($at_max_val * 2.25 + 5) = $deadline seconds," \
2022              "it is server process time add the network latency."
2023
2024         waited=0
2025         while [ $waited -lt $deadline ]; do
2026                 echo -n "."
2027                 sleep 1
2028                 waited=$(($waited + 1))
2029                 ps -p $pid > /dev/null || break
2030         done
2031         echo "waited $waited seconds"
2032         ps -p $pid && error "lfs hadn't finished by $deadline seconds"
2033         wait $pid && error "succeeded, but should have failed"
2034
2035         lustre_fail mds 0
2036
2037         if at_is_enabled; then
2038                 at_max_set $timeout client
2039         else
2040                 lctl set_param timeout=$timeout
2041         fi
2042
2043         resetquota -u $TSTUSR
2044 }
2045 run_test_with_stat 29 "unhandled quotactls must not hang lustre client (19778) ========"
2046
2047 test_30()
2048 {
2049         local output
2050         local LIMIT=1024
2051         local TESTFILE="$DIR/$tdir/$tfile"
2052         local GRACE=10
2053
2054         set_blk_tunesz 512
2055         set_blk_unitsz 1024
2056
2057         mkdir -p $DIR/$tdir
2058         chmod 0777 $DIR/$tdir
2059
2060         $LFS setstripe $TESTFILE -i 0 -c 1
2061         chown $TSTUSR.$TSTUSR $TESTFILE
2062
2063         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
2064         $LFS setquota -u $TSTUSR -b $LIMIT -B 0 -i 0 -I 0 $DIR
2065         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1024 count=$((LIMIT * 2)) || true
2066         cancel_lru_locks osc
2067         sleep $GRACE
2068         $LFS setquota -u $TSTUSR -B 0 $DIR
2069         # over-quota flag has not yet settled since we do not trigger async events
2070         # based on grace time period expiration
2071         $SHOW_QUOTA_USER
2072         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 || true
2073         cancel_lru_locks osc
2074         # now over-quota flag should be settled and further writes should fail
2075         $SHOW_QUOTA_USER
2076         $RUNAS dd if=/dev/zero of=$TESTFILE conv=notrunc oflag=append bs=1048576 count=1 && error "grace times were reset"
2077         rm -f $TESTFILE
2078         resetquota -u $TSTUSR
2079         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $MAX_IQ_TIME $DIR
2080
2081         set_blk_unitsz $((128 * 1024))
2082         set_blk_tunesz $((128 * 1024 / 2))
2083 }
2084 run_test_with_stat 30 "hard limit updates should not reset grace times ================"
2085
2086 # test duplicate quota releases b=18630
2087 test_31() {
2088         mkdir -p $DIR/$tdir
2089         chmod 0777 $DIR/$tdir
2090
2091         LIMIT=$(( $BUNIT_SZ * $(($OSTCOUNT + 1)) * 10)) # 10 bunits each sever
2092         TESTFILE="$DIR/$tdir/$tfile-0"
2093         TESTFILE2="$DIR/$tdir/$tfile-1"
2094
2095         wait_delete_completed
2096
2097         log "   User quota (limit: $LIMIT kbytes)"
2098         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
2099
2100         $LFS setstripe $TESTFILE -i 0 -c 1
2101         chown $TSTUSR.$TSTUSR $TESTFILE
2102         $LFS setstripe $TESTFILE2 -i 0 -c 1
2103         chown $TSTUSR.$TSTUSR $TESTFILE2
2104
2105         log "   step1: write out of block quota ..."
2106         $RUNAS dd if=/dev/zero of=$TESTFILE bs=$BLK_SZ count=5120
2107         $RUNAS dd if=/dev/zero of=$TESTFILE2 bs=$BLK_SZ count=5120
2108
2109         #define OBD_FAIL_QUOTA_DELAY_SD      0xA04
2110         #define OBD_FAIL_SOME        0x10000000 /* fail N times */
2111         lustre_fail ost $((0x00000A04 | 0x10000000)) 1
2112
2113         log "   step2: delete two files so that triggering duplicate quota release ..."
2114         rm -f $TESTFILE $TESTFILE2
2115         sync; sleep 5; sync      #  OBD_FAIL_QUOTA_DELAY_SD will delay for 5 seconds
2116         wait_delete_completed
2117
2118         log "   step3: verify if the ost failed"
2119         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2120         watchdog=`awk '/test 31/ {start = 1;}
2121                        /release quota error/ {
2122                                if (start) {
2123                                        print;
2124                                }
2125                        }' $TMP/lustre-log-${TESTNAME}.log`
2126         [ "$watchdog" ] && error "$watchdog"
2127         rm -f $TMP/lustre-log-${TESTNAME}.log
2128
2129         lustre_fail ost 0
2130         resetquota -u $TSTUSR
2131 }
2132 run_test_with_stat 31 "test duplicate quota releases ==="
2133
2134 # check hash_cur_bits
2135 check_quota_hash_cur_bits() {
2136         local bits=$1
2137
2138         # check quota_hash_cur_bits on all obdfilters
2139         for num in `seq $OSTCOUNT`; do
2140             cb=`do_facet ost$num "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2141             if [ $cb -gt $bits ]; then
2142                 echo "hash_lqs_cur_bits of ost$num is too large(cur_bits=$cb)"
2143                 return 1;
2144             fi
2145         done
2146         # check quota_hash_cur_bits on mds
2147         cb=`do_facet $SINGLEMDS "cat /sys/module/lquota/parameters/hash_lqs_cur_bits"`
2148         if [ $cb -gt $bits ]; then
2149             echo "hash_lqs_cur_bits of mds is too large(cur_bits=$cb)"
2150             return 1;
2151         fi
2152         return 0;
2153 }
2154
2155 # check lqs hash
2156 check_lqs_hash() {
2157         # check distribution of all obdfilters
2158         for num in `seq $OSTCOUNT`; do
2159             do_facet ost$num "lctl get_param obdfilter.${FSNAME}-OST*.hash_stats | grep LQS_HASH" | while read line; do
2160                 rehash_count=`echo $line | awk '{print $9}'`
2161                 if [ $rehash_count -eq 0 ]; then
2162                     echo -e "ost$num:\n $line"
2163                     error "Rehearsh didn't happen"
2164                 fi
2165             done
2166         done
2167         # check distribution of mds
2168         do_facet $SINGLEMDS "lctl get_param mdt.${FSNAME}-MDT*.hash_stats | grep LQS_HASH" | while read line; do
2169             rehash_count=`echo $line | awk '{print $9}'`
2170             if [ $rehash_count -eq 0 ]; then
2171                 echo -e "mdt:\n $line"
2172                 error "Rehearsh didn't happen"
2173             fi
2174         done
2175 }
2176
2177 test_32()
2178 {
2179         # reset system so that quota_hash_cur_bits==3
2180         echo "Reset system ..."
2181         local LMR_orig=$LOAD_MODULES_REMOTE
2182         LOAD_MODULES_REMOTE=true
2183         cleanup_and_setup_lustre
2184         LOAD_MODULES_REMOTE=$LMR_orig
2185
2186         client_up
2187         wait_mds_ost_sync
2188         quota_init
2189
2190         for user in $SANITY_QUOTA_USERS; do
2191             check_runas_id_ret $user quota_usr "runas -u $user -g quota_usr" >/dev/null 2>/dev/null || \
2192                 missing_users="$missing_users $user"
2193         done
2194         [ -n "$missing_users" ] && { skip_env "the following users are missing: $missing_users" ; return 0 ; }
2195         check_quota_hash_cur_bits 3 || { skip_env "hash_lqs_cur_bits isn't set properly"; return 0;}
2196
2197         $LFS quotaoff -ug $DIR
2198         $LFS quotacheck -ug $DIR
2199
2200         for user in $SANITY_QUOTA_USERS; do
2201             $LFS setquota -u $user --block-hardlimit 1048576 $DIR
2202         done
2203
2204         check_lqs_hash
2205
2206         for user in $SANITY_QUOTA_USERS; do
2207             resetquota -u $user
2208         done
2209 }
2210 run_test 32 "check lqs hash(bug 21846) =========================================="
2211
2212 cleanup_quota_test() {
2213         trap 0
2214         echo "Delete files..."
2215         rm -rf $DIR/$tdir
2216 }
2217
2218 # basic usage tracking for user & group
2219 test_33() {
2220         mkdir -p $DIR/$tdir
2221         chmod 0777 $DIR/$tdir
2222         INODES=10
2223         BLK_CNT=1024
2224         TOTAL_BLKS=$(($INODES * $BLK_CNT))
2225
2226         trap cleanup_quota_test EXIT
2227
2228         # make sure the system is clean
2229         USED=`getquota -u $TSTID global curspace`
2230         [ $USED -ne 0 ] && \
2231                 error "Used space ($USED) for user $TSTID isn't 0."
2232         USED=`getquota -g $TSTID global curspace`
2233         [ $USED -ne 0 ] && \
2234                 error "Used space ($USED) for group $TSTID isn't 0."
2235
2236         for i in `seq 0 $INODES`; do
2237                 $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile-$i conv=fsync \
2238                         bs=$((BLK_SZ * BLK_CNT)) count=1 2>/dev/null ||
2239                         error "write failed"
2240                 echo "Iteration $i/$INODES completed"
2241         done
2242         sync; sync_all_data;
2243
2244         echo "Verify disk usage after write"
2245         USED=`getquota -u $TSTID global curspace`
2246         [ $USED -lt $TOTAL_BLKS ] && \
2247                error "Used space for user $TSTID is $USED, expected $TOTAL_BLKS"
2248         USED=`getquota -u $TSTID global curinodes`
2249         [ $USED -lt $INODES ] && \
2250                error "Used inode for user $TSTID is $USED, expected $INODES"
2251
2252         USED=`getquota -g $TSTID global curspace`
2253         [ $USED -lt $TOTAL_BLKS ] && \
2254               error "Used space for group $TSTID is $USED, expected $TOTAL_BLKS"
2255         USED=`getquota -g $TSTID global curinodes`
2256         [ $USED -lt $INODES ] && \
2257               error "Used inode for group $TSTID is $USED, expected $INODES"
2258
2259         cleanup_quota_test
2260
2261         echo "Verify disk usage after delete"
2262         wait_delete_completed
2263         sync; sync_all_data;
2264
2265         USED=`getquota -u $TSTID global curspace`
2266         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2267         USED=`getquota -u $TSTID global curinodes`
2268         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2269         USED=`getquota -g $TSTID global curspace`
2270         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2271         USED=`getquota -g $TSTID global curinodes`
2272         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2273 }
2274 run_test 33 "basic usage tracking for user & group =============================="
2275
2276 # usage transfer test for user & group
2277 test_34() {
2278         BLK_CNT=1024
2279         mkdir -p $DIR/$tdir
2280         chmod 0777 $DIR/$tdir
2281
2282         trap cleanup_quota_test EXIT
2283
2284         # make sure the system is clean
2285         USED=`getquota -u $TSTID global curspace`
2286         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2287         USED=`getquota -g $TSTID global curspace`
2288         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2289
2290         echo "Write file..."
2291         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) count=1 \
2292                 conv=fsync 2>/dev/null || error "write failed"
2293         sync; sync_all_data;
2294
2295         echo "chown the file to user $TSTID"
2296         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2297
2298         echo "Wait for setattr on objects finished..."
2299         wait_delete_completed
2300
2301         echo "Verify disk usage for user $TSTID"
2302         USED=`getquota -u $TSTID global curspace`
2303         [ $USED -lt $BLK_CNT ] && \
2304                 error "Used space for user $TSTID is $USED, expected $BLK_CNT"
2305         USED=`getquota -u $TSTID global curinodes`
2306         [ $USED -ne 1 ] && \
2307                 error "Used inodes for user $TSTID is $USED, expected 1"
2308
2309         echo "chgrp the file to group $TSTID"
2310         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2311
2312         echo "Wait for setattr on objects finished..."
2313         wait_delete_completed
2314
2315         echo "Verify disk usage for group $TSTID"
2316         USED=`getquota -g $TSTID global curspace`
2317         [ $USED -ge $BLK_CNT ] || \
2318                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2319         USED=`getquota -g $TSTID global curinodes`
2320         [ $USED -eq 1 ] || \
2321                 error "Used inodes for group $TSTID is $USED, expected 1"
2322
2323         cleanup_quota_test
2324 }
2325 run_test 34 "usage transfer for user & group ===================================="
2326
2327 # usage is still accessible across restart
2328 test_35() {
2329         mkdir -p $DIR/$tdir
2330         chmod 0777 $DIR/$tdir
2331         BLK_CNT=1024
2332
2333         trap cleanup_quota_test EXIT
2334
2335         echo "Write file..."
2336         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) \
2337                 count=1 conv=fsync 2>/dev/null || error "write failed"
2338         sync; sync_all_data;
2339
2340         echo "Save disk usage before restart"
2341         ORIG_USR_SPACE=`getquota -u $TSTID global curspace`
2342         [ $ORIG_USR_SPACE -eq 0 ] && \
2343                 error "Used space for user $TSTID is 0, expected $BLK_CNT"
2344         ORIG_USR_INODES=`getquota -u $TSTID global curinodes`
2345         [ $ORIG_USR_INODES -eq 0 ] && \
2346                 error "Used inodes for user $TSTID is 0, expected 1"
2347         ORIG_GRP_SPACE=`getquota -g $TSTID global curspace`
2348         [ $ORIG_GRP_SPACE -eq 0 ] && \
2349                 error "Used space for group $TSTID is 0, expected $BLK_CNT"
2350         ORIG_GRP_INODES=`getquota -g $TSTID global curinodes`
2351         [ $ORIG_GRP_INODES -eq 0 ] && \
2352                 error "Used inodes for group $TSTID is 0, expected 1"
2353
2354         log "Restart..."
2355         local ORIG_REFORMAT=$REFORMAT
2356         REFORMAT=""
2357         cleanup_and_setup_lustre
2358         REFORMAT=$ORIG_REFORMAT
2359         quota_init
2360
2361         echo "Verify disk usage after restart"
2362         USED=`getquota -u $TSTID global curspace`
2363         [ $USED -eq $ORIG_USR_SPACE ] || \
2364                 error "Used space for user $TSTID changed from " \
2365                         "$ORIG_USR_SPACE to $USED"
2366         USED=`getquota -u $TSTID global curinodes`
2367         [ $USED -eq $ORIG_USR_INODES ] || \
2368                 error "Used inodes for user $TSTID changed from " \
2369                         "$ORIG_USR_INODES to $USED"
2370         USED=`getquota -g $TSTID global curspace`
2371         [ $USED -eq $ORIG_GRP_SPACE ] || \
2372                 error "Used space for group $TSTID changed from " \
2373                         "$ORIG_GRP_SPACE to $USED"
2374         USED=`getquota -g $TSTID global curinodes`
2375         [ $USED -eq $ORIG_GRP_INODES ] || \
2376                 error "Used inodes for group $TSTID changed from " \
2377                         "$ORIG_GRP_INODES to $USED"
2378
2379         # check if the vfs_dq_init() is called before writing
2380         echo "Append to the same file..."
2381         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$((BLK_SZ * BLK_CNT)) \
2382                 count=1 seek=1 conv=fsync 2>/dev/null || error "write failed"
2383         sync; sync_all_data;
2384
2385         echo "Verify space usage is increased"
2386         USED=`getquota -u $TSTID global curspace`
2387         [ $USED -gt $ORIG_USR_SPACE ] ||
2388                 error "Used space for user $TSTID isn't increased" \
2389                         "orig:$ORIG_USR_SPACE, now:$USED"
2390         USED=`getquota -g $TSTID global curspace`
2391         [ $USED -gt $ORIG_GRP_SPACE ] ||
2392                 error "Used space for group $TSTID isn't increased" \
2393                         "orig:$ORIG_GRP_SPACE, now:$USED"
2394
2395         cleanup_quota_test
2396 }
2397 run_test 35 "usage is still accessible across reboot ============================"
2398
2399 # turn off quota
2400 quota_fini()
2401 {
2402         $LFS quotaoff $DIR
2403         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2404 }
2405 quota_fini
2406
2407 cd $ORIG_PWD
2408
2409 # override TESTSUITE so Maloo keeps only one set of sanity-quota results
2410 TESTSUITE=sanity-quota.sh
2411 complete $SECONDS
2412 check_and_cleanup_lustre
2413 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2414 exit_status