Whamcloud - gitweb
LU-797 tests: process lfs df output properly
[fs/lustre-release.git] / lustre / tests / sanity-quota.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # Run test by setting NOSETUP=true when ltest has setup env for us
7 set -e
8
9 SRCDIR=`dirname $0`
10 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
11
12 ONLY=${ONLY:-"$*"}
13 ALWAYS_EXCEPT="$SANITY_QUOTA_EXCEPT"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 [ "$ALWAYS_EXCEPT$EXCEPT" ] &&
17         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
18
19 TMP=${TMP:-/tmp}
20
21 ORIG_PWD=${PWD}
22 TSTID=${TSTID:-60000}
23 TSTID2=${TSTID2:-60001}
24 TSTUSR=${TSTUSR:-"quota_usr"}
25 TSTUSR2=${TSTUSR2:-"quota_2usr"}
26 BLK_SZ=1024
27 MAX_DQ_TIME=604800
28 MAX_IQ_TIME=604800
29
30 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
31 . $LUSTRE/tests/test-framework.sh
32 init_test_env $@
33 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
34 init_logging
35 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
36
37 [ $MDSCOUNT -gt 1 ] && skip "CMD case" && exit 0
38
39 require_dsh_mds || exit 0
40 require_dsh_ost || exit 0
41
42 # XXX Once we drop the interoperability with old server (< 2.3.50), the
43 #     sanity-quota-old.sh should be removed.
44 if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.50) ]; then
45         exec $LUSTRE/tests/sanity-quota-old.sh
46 fi
47
48 # if e2fsprogs support quota feature?
49 if [ $(facet_fstype $SINGLEMDS) == ldiskfs ] && \
50         ! $DEBUGFS -c -R supported_features | grep -q 'quota'; then
51         skip "e2fsprogs doesn't support quota" && exit 0
52 fi
53
54 [ "$SLOW" = "no" ] && EXCEPT_SLOW="9 18 21"
55
56 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
57
58 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
59
60 DIR=${DIR:-$MOUNT}
61 DIR2=${DIR2:-$MOUNT2}
62
63 QUOTA_AUTO_OLD=$QUOTA_AUTO
64 export QUOTA_AUTO=0
65
66 check_and_setup_lustre
67
68 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
69 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
70
71 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
72 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
73 SHOW_QUOTA_USER2="$LFS quota -v -u $TSTUSR2 $DIR"
74 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
75 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
76 SHOW_QUOTA_GROUP2="$LFS quota -v -g $TSTUSR2 $DIR"
77 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
78 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
79
80 build_test_filter
81
82 lustre_fail() {
83         local fail_node=$1
84         local fail_loc=$2
85         local fail_val=${3:-0}
86
87         if [ $fail_node == "mds" ] || [ $fail_node == "mds_ost" ]; then
88                 do_facet $SINGLEMDS "lctl set_param fail_val=$fail_val"
89                 do_facet $SINGLEMDS "lctl set_param fail_loc=$fail_loc"
90         fi
91
92         if [ $fail_node == "ost" ] || [ $fail_node == "mds_ost" ]; then
93                 for num in `seq $OSTCOUNT`; do
94                         do_facet ost$num "lctl set_param fail_val=$fail_val"
95                         do_facet ost$num "lctl set_param fail_loc=$fail_loc"
96                 done
97         fi
98 }
99
100 RUNAS="runas -u $TSTID -g $TSTID"
101 RUNAS2="runas -u $TSTID2 -g $TSTID2"
102 DD="dd if=/dev/zero bs=1M"
103
104 FAIL_ON_ERROR=false
105
106 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
107         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
108 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
109         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
110
111 # clear quota limits for a user or a group
112 # usage: resetquota -u username
113 #        resetquota -g groupname
114
115 resetquota() {
116         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
117         [ "$1" != "-u" -a "$1" != "-g" ] &&
118                 error "resetquota: wrong specifier $1 passed"
119
120         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
121                 error "clear quota for [type:$1 name:$2] failed"
122         # give a chance to slave to release space
123         sleep 1
124 }
125
126 quota_scan() {
127         local LOCAL_UG=$1
128         local LOCAL_ID=$2
129
130         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "u" ]; then
131                 $LFS quota -v -u $LOCAL_ID $DIR
132                 log "Files for user ($LOCAL_ID):"
133                 ($LFS find -user $LOCAL_ID $DIR | head -n 4 |
134                         xargs stat 2>/dev/null)
135         fi
136
137         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "g" ]; then
138                 $LFS quota -v -u $LOCAL_ID $DIR
139                 log "Files for group ($LOCAL_ID):"
140                 ($LFS find -group $LOCAL_ID $DIR | head -n 4 |
141                         xargs stat 2>/dev/null)
142         fi
143 }
144
145 quota_error() {
146         quota_scan $1 $2
147         shift 2
148         error "$*"
149 }
150
151 quota_log() {
152         quota_scan $1 $2
153         shift 2
154         log "$*"
155 }
156
157 # get quota for a user or a group
158 # usage: getquota -u|-g <username>|<groupname> global|<obd_uuid> \
159 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
160 #
161 getquota() {
162         local spec
163         local uuid
164
165         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
166         [ "$1" != "-u" -a "$1" != "-g" ] &&
167                 error "getquota: wrong u/g specifier $1 passed"
168
169         uuid="$3"
170
171         case "$4" in
172                 curspace)   spec=1;;
173                 bsoftlimit) spec=2;;
174                 bhardlimit) spec=3;;
175                 bgrace)     spec=4;;
176                 curinodes)  spec=5;;
177                 isoftlimit) spec=6;;
178                 ihardlimit) spec=7;;
179                 igrace)     spec=8;;
180                 *)          error "unknown quota parameter $4";;
181         esac
182
183         [ "$uuid" = "global" ] && uuid=$DIR
184
185         $LFS quota -v "$1" "$2" $DIR |
186                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
187                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
188                 | tr -d "*"
189 }
190
191 # set mdt quota type
192 # usage: set_mdt_qtype ug|u|g|none
193 set_mdt_qtype() {
194         local qtype=$1
195         local varsvc=${SINGLEMDS}_svc
196         local cmd
197         do_facet mgs $LCTL conf_param $FSNAME.quota.mdt=$qtype
198         if $(facet_up $SINGLEMDS); then
199                 cmd="$LCTL get_param -n "
200                 cmd=${cmd}osd-$(facet_fstype $SINGLEMDS).${!varsvc}
201                 cmd=${cmd}.quota_slave.enabled
202
203                 wait_update_facet $SINGLEMDS "$cmd" "$qtype" || return 1
204         fi
205         return 0
206 }
207
208 # set ost quota type
209 # usage: set_ost_quota_type ug|u|g|none
210 set_ost_qtype() {
211         local qtype=$1
212         local varsvc
213         local osts=$(get_facets OST)
214         local cmd
215         do_facet mgs $LCTL conf_param $FSNAME.quota.ost=$qtype
216         # we have to make sure each OST received config changes
217         for ost in ${osts//,/ }; do
218                 varsvc=${ost}_svc
219                 cmd="$LCTL get_param -n "
220                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
221                 cmd=${cmd}.quota_slave.enabled
222
223                 if $(facet_up $ost); then
224                         wait_update_facet $ost "$cmd" "$qtype" || return 1
225                 fi
226         done
227         return 0
228 }
229
230 wait_reintegration() {
231         local ntype=$1
232         local qtype=$2
233         local result="glb[1],slv[1],reint[0]"
234         local varsvc
235         local cmd
236
237         if [ $ntype == "mdt" ]; then
238                 varsvc=${SINGLEMDS}_svc
239                 cmd="$LCTL get_param -n "
240                 cmd=${cmd}osd-$(facet_fstype $SINGLEMDS).${!varsvc}
241                 cmd=${cmd}.quota_slave.info
242
243                 if $(facet_up $SINGLEMDS); then
244                         wait_update_facet $SINGLEMDS "$cmd |
245                         grep "$qtype" | awk '{ print \\\$3 }'" "$result" ||
246                                 return 1
247                 fi
248         else
249                 local osts=$(get_facets OST)
250                 for ost in ${osts//,/ }; do
251                         varsvc=${ost}_svc
252                         cmd="$LCTL get_param -n "
253                         cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
254                         cmd=${cmd}.quota_slave.info
255
256                         if $(facet_up $ost); then
257                                 wait_update_facet $ost "$cmd |
258                                 grep "$qtype" | awk '{ print \\\$3 }'" \
259                                         "$result" || return 1
260                         fi
261                 done
262         fi
263         return 0
264 }
265
266 wait_mdt_reint() {
267         local qtype=$1
268
269         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
270                 wait_reintegration "mdt" "user" || return 1
271         fi
272
273         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
274                 wait_reintegration "mdt" "group" || return 1
275         fi
276         return 0
277 }
278
279 wait_ost_reint() {
280         local qtype=$1
281
282         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
283                 wait_reintegration "ost" "user" || return 1
284         fi
285
286         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
287                 wait_reintegration "ost" "group" || return 1
288         fi
289         return 0
290 }
291
292 setup_quota_test() {
293         rm -rf $DIR/$tdir
294         wait_delete_completed
295         echo "Creating test directory"
296         mkdir -p $DIR/$tdir
297         chmod 077 $DIR/$tdir
298         # always clear fail_loc in case of fail_loc isn't cleared
299         # properly when previous test failed
300         lustre_fail mds_ost 0
301 }
302
303 cleanup_quota_test() {
304         trap 0
305         echo "Delete files..."
306         rm -rf $DIR/$tdir
307         echo "Wait for unlink objects finished..."
308         wait_delete_completed
309         sync_all_data || true
310 }
311
312 quota_show_check() {
313         local bf=$1
314         local ug=$2
315         local qid=$3
316         local usage
317
318         $LFS quota -v -$ug $qid $DIR
319
320         if [ "$bf" == "a" -o "$bf" == "b" ]; then
321                 usage=$(getquota -$ug $qid global curspace)
322                 if [ -z $usage ]; then
323                         quota_error $ug $qid \
324                                 "Query block quota failed ($ug:$qid)."
325                 else
326                         [ $usage -ne 0 ] && quota_log $ug $qid \
327                                 "Block quota isn't 0 ($ug:$qid:$usage)."
328                 fi
329         fi
330
331         if [ "$bf" == "a" -o "$bf" == "f" ]; then
332                 usage=$(getquota -$ug $qid global curinodes)
333                 if [ -z $usage ]; then
334                         quota_error $ug $qid \
335                                 "Query file quota failed ($ug:$qid)."
336                 else
337                         [ $usage -ne 0 ] && quota_log $ug $qid \
338                                 "File quota isn't 0 ($ug:$qid:$usage)."
339                 fi
340         fi
341 }
342
343 # enable quota debug
344 quota_init() {
345         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
346 }
347 quota_init
348
349 test_quota_performance() {
350         local TESTFILE="$DIR/$tdir/$tfile-0"
351         local size=$1 # in MB
352         local stime=$(date +%s)
353         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
354                 quota_error u $TSTUSR "write failure"
355         local etime=$(date +%s)
356         delta=$((etime - stime))
357         if [ $delta -gt 0 ]; then
358             rate=$((size * 1024 / delta))
359             [ $rate -gt 1024 ] ||
360                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
361         fi
362         rm -f $TESTFILE
363 }
364
365 # test basic quota performance b=21696
366 test_0() {
367         local MB=100 # 100M
368         [ "$SLOW" = "no" ] && MB=10
369
370         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
371         [ $free_space -le $((MB * 1024)) ] &&
372                 skip "not enough space ${free_space} KB, " \
373                         "required $((MB * 1024)) KB" && return
374         setup_quota_test
375         trap cleanup_quota_test EXIT
376
377         set_ost_qtype "none" || error "disable ost quota failed"
378         test_quota_performance $MB
379
380         set_ost_qtype "ug" || error "enable ost quota failed"
381         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
382                 error "set quota failed"
383         test_quota_performance $MB
384
385         cleanup_quota_test
386         resetquota -u $TSTUSR
387 }
388 run_test 0 "Test basic quota performance"
389
390 # test block hardlimit
391 test_1() {
392         local LIMIT=10  # 10M
393         local TESTFILE="$DIR/$tdir/$tfile-0"
394
395         setup_quota_test
396         trap cleanup_quota_test EXIT
397
398         # enable ost quota
399         set_ost_qtype "ug" || "enable ost quota failed"
400
401         # test for user
402         log "User quota (block hardlimit:$LIMIT MB)"
403         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
404                 error "set user quota failed"
405
406         # make sure the system is clean
407         local USED=$(getquota -u $TSTUSR global curspace)
408         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
409
410         $LFS setstripe $TESTFILE -c 1
411         chown $TSTUSR.$TSTUSR $TESTFILE
412
413         log "Write..."
414         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
415                 quota_error u $TSTUSR "user write failure, but expect success"
416         log "Write out of block quota ..."
417         # this time maybe cache write,  ignore it's failure
418         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
419         # flush cache, ensure noquota flag is set on client
420         cancel_lru_locks osc
421         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
422                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
423
424         rm -f $TESTFILE
425         wait_delete_completed
426         sync_all_data || true
427         USED=$(getquota -u $TSTUSR global curspace)
428         [ $USED -ne 0 ] && quota_error u $TSTUSR \
429                 "user quota isn't released after deletion"
430         resetquota -u $TSTUSR
431
432         # test for group
433         log "--------------------------------------"
434         log "Group quota (block hardlimit:$LIMIT MB)"
435         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
436                 error "set group quota failed"
437
438         TESTFILE="$DIR/$tdir/$tfile-1"
439         # make sure the system is clean
440         USED=$(getquota -g $TSTUSR global curspace)
441         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0"
442
443         $LFS setstripe $TESTFILE -c 1
444         chown $TSTUSR.$TSTUSR $TESTFILE
445
446         log "Write ..."
447         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
448                 quota_error g $TSTUSR "group write failure, but expect success"
449         log "Write out of block quota ..."
450         # this time maybe cache write, ignore it's failure
451         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
452         cancel_lru_locks osc
453         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
454                 quota_error g $TSTUSR "group write success, but expect EDQUOT"
455
456         # cleanup
457         cleanup_quota_test
458
459         USED=$(getquota -g $TSTUSR global curspace)
460         [ $USED -ne 0 ] && quota_error g $TSTUSR \
461                 "group quota isn't released after deletion"
462
463         resetquota -g $TSTUSR
464 }
465 run_test 1 "Block hard limit (normal use and out of quota)"
466
467 # test inode hardlimit
468 test_2() {
469         local LIMIT=$((1024 * 1024)) # 1M inodes
470         local TESTFILE="$DIR/$tdir/$tfile-0"
471
472         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
473
474         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
475         [ $FREE_INODES -lt $LIMIT ] &&
476                 skip "not enough free inodes $FREE_INODES required $LIMIT" &&
477                 return
478
479         setup_quota_test
480         trap cleanup_quota_test EXIT
481
482         # enable mdt quota
483         set_mdt_qtype "ug" || "enable mdt quota failed"
484
485         # test for user
486         log "User quota (inode hardlimit:$LIMIT files)"
487         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
488                 error "set user quota failed"
489
490         # make sure the system is clean
491         local USED=$(getquota -u $TSTUSR global curinodes)
492         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
493
494         log "Create $LIMIT files ..."
495         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
496                 quota_error u $TSTUSR "user create failure, but expect success"
497         log "Create out of file quota ..."
498         $RUNAS touch ${TESTFILE}_xxx && \
499                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
500
501         # cleanup
502         unlinkmany ${TESTFILE} $LIMIT
503         rm -f ${TESTFILE}_xxx
504         wait_delete_completed
505
506         USED=$(getquota -u $TSTUSR global curinodes)
507         [ $USED -ne 0 ] && quota_error u $TSTUSR \
508                 "user quota isn't released after deletion"
509         resetquota -u $TSTUSR
510
511         # test for group
512         log "--------------------------------------"
513         log "Group quota (inode hardlimit:$LIMIT files)"
514         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
515                 error "set group quota failed"
516
517         TESTFILE=$DIR/$tdir/$tfile-1
518         # make sure the system is clean
519         USED=$(getquota -g $TSTUSR global curinodes)
520         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
521
522         log "Create $LIMIT files ..."
523         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
524                 quota_error g $TSTUSR "group create failure, but expect success"
525         log "Create out of file quota ..."
526         $RUNAS touch ${TESTFILE}_xxx && \
527                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
528
529         # cleanup
530         unlinkmany ${TESTFILE} $LIMIT
531         rm -f ${TESTFILE}_xxx
532         wait_delete_completed
533
534         USED=$(getquota -g $TSTUSR global curinodes)
535         [ $USED -ne 0 ] && quota_error g $TSTUSR \
536                 "user quota isn't released after deletion"
537
538         cleanup_quota_test
539         resetquota -g $TSTUSR
540 }
541 run_test 2 "File hard limit (normal use and out of quota)"
542
543 test_block_soft() {
544         local TESTFILE=$1
545         local TIMER=$(($2 * 3 / 2))
546         local LIMIT=$3
547         local OFFSET=0
548
549         setup_quota_test
550         trap cleanup_quota_test EXIT
551
552         $LFS setstripe $TESTFILE -c 1 -i 0
553         chown $TSTUSR.$TSTUSR $TESTFILE
554
555         echo "Write up to soft limit"
556         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
557                 quota_error a $TSTUSR "write failure, but expect success"
558         OFFSET=$((LIMIT * 1024))
559         cancel_lru_locks osc
560
561         echo "Write to exceed soft limit"
562         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
563                 quota_error a $TSTUSR "write failure, but expect success"
564         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
565         cancel_lru_locks osc
566
567         $SHOW_QUOTA_USER
568         $SHOW_QUOTA_GROUP
569         $SHOW_QUOTA_INFO_USER
570         $SHOW_QUOTA_INFO_GROUP
571
572         echo "Write before timer goes off"
573         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
574                 quota_error a $TSTUSR "write failure, but expect success"
575         OFFSET=$((OFFSET + 1024))
576         cancel_lru_locks osc
577
578         echo "Sleep $TIMER seconds ..."
579         sleep $TIMER
580
581         $SHOW_QUOTA_USER
582         $SHOW_QUOTA_GROUP
583         $SHOW_QUOTA_INFO_USER
584         $SHOW_QUOTA_INFO_GROUP
585
586         echo "Write after timer goes off"
587         # maybe cache write, ignore.
588         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
589         OFFSET=$((OFFSET + 1024))
590         cancel_lru_locks osc
591         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
592                 quota_error a $TSTUSR "write success, but expect EDQUOT"
593
594         $SHOW_QUOTA_USER
595         $SHOW_QUOTA_GROUP
596         $SHOW_QUOTA_INFO_USER
597         $SHOW_QUOTA_INFO_GROUP
598
599         echo "Unlink file to stop timer"
600         rm -f $TESTFILE
601         wait_delete_completed
602         sync_all_data || true
603
604         $SHOW_QUOTA_USER
605         $SHOW_QUOTA_GROUP
606         $SHOW_QUOTA_INFO_USER
607         $SHOW_QUOTA_INFO_GROUP
608
609         $LFS setstripe $TESTFILE -c 1 -i 0
610         chown $TSTUSR.$TSTUSR $TESTFILE
611
612         echo "Write ..."
613         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
614                 quota_error a $TSTUSR "write failure, but expect success"
615         # cleanup
616         cleanup_quota_test
617 }
618
619 # block soft limit
620 test_3() {
621         local LIMIT=1  # 1MB
622         local GRACE=10 # 10s
623         local TESTFILE=$DIR/$tdir/$tfile-0
624
625         set_ost_qtype "ug" || error "enable ost quota failed"
626
627         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
628         # make sure the system is clean
629         local USED=$(getquota -u $TSTUSR global curspace)
630         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
631
632         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
633                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
634         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
635                 error "set user quota failed"
636
637         test_block_soft $TESTFILE $GRACE $LIMIT
638         resetquota -u $TSTUSR
639
640         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
641         TESTFILE=$DIR/$tdir/$tfile-1
642         # make sure the system is clean
643         USED=$(getquota -g $TSTUSR global curspace)
644         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
645
646         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
647                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
648         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
649                 error "set group quota failed"
650
651         test_block_soft $TESTFILE $GRACE $LIMIT
652         resetquota -g $TSTUSR
653
654         # cleanup
655         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
656                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
657         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
658                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
659 }
660 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
661
662 test_file_soft() {
663         local TESTFILE=$1
664         local LIMIT=$2
665         local TIMER=$(($3 * 3 / 2))
666
667         setup_quota_test
668         trap cleanup_quota_test EXIT
669
670         echo "Create files to exceed soft limit"
671         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
672                 quota_error a $TSTUSR "create failure, but expect success"
673         sync; sleep 1; sync
674
675         echo "Create file before timer goes off"
676         $RUNAS touch ${TESTFILE}_before ||
677                 quota_error a $TSTUSR "failed create before timer expired," \
678                         "but expect success"
679         sync; sleep 1; sync
680
681         echo "Sleep $TIMER seconds ..."
682         sleep $TIMER
683
684         $SHOW_QUOTA_USER
685         $SHOW_QUOTA_GROUP
686         $SHOW_QUOTA_INFO_USER
687         $SHOW_QUOTA_INFO_GROUP
688
689         echo "Create file after timer goes off"
690         # There is a window that space is accounted in the quota usage but
691         # hasn't been decreased from the pending write, if we acquire quota
692         # in this window, we'll acquire more than we needed.
693         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
694         sync; sleep 1; sync
695         $RUNAS touch ${TESTFILE}_after_3 &&
696                 quota_error a $TSTUSR "create after timer expired," \
697                         "but expect EDQUOT"
698         sync; sleep 1; sync
699
700         $SHOW_QUOTA_USER
701         $SHOW_QUOTA_GROUP
702         $SHOW_QUOTA_INFO_USER
703         $SHOW_QUOTA_INFO_GROUP
704
705         echo "Unlink files to stop timer"
706         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
707         wait_delete_completed
708
709         echo "Create file"
710         $RUNAS touch ${TESTFILE}_xxx ||
711                 quota_error a $TSTUSR "touch after timer stop failure," \
712                         "but expect success"
713         sync; sleep 1; sync
714
715         # cleanup
716         cleanup_quota_test
717 }
718
719 # file soft limit
720 test_4a() {
721         local LIMIT=10 # inodes
722         local TESTFILE=$DIR/$tdir/$tfile-0
723         local GRACE=5
724
725         set_mdt_qtype "ug" || error "enable mdt quota failed"
726
727         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
728         # make sure the system is clean
729         local USED=$(getquota -u $TSTUSR global curinodes)
730         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
731
732         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
733                 $GRACE $DIR || error "set user grace time failed"
734         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
735                 error "set user quota failed"
736
737         test_file_soft $TESTFILE $LIMIT $GRACE
738         resetquota -u $TSTUSR
739
740         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
741         # make sure the system is clean
742         USED=$(getquota -g $TSTUSR global curinodes)
743         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
744
745         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
746                 $GRACE $DIR || error "set group grace time failed"
747         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
748                 error "set group quota failed"
749         TESTFILE=$DIR/$tdir/$tfile-1
750
751         test_file_soft $TESTFILE $LIMIT $GRACE
752         resetquota -g $TSTUSR
753
754         # cleanup
755         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
756                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
757         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
758                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
759 }
760 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
761
762 test_4b() {
763         local GR_STR1="1w3d"
764         local GR_STR2="1000s"
765         local GR_STR3="5s"
766         local GR_STR4="1w2d3h4m5s"
767         local GR_STR5="5c"
768         local GR_STR6="1111111111111111"
769
770         wait_delete_completed
771
772         # test of valid grace strings handling
773         echo "Valid grace strings test"
774         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
775                 $GR_STR2 $DIR || error "set user grace time failed"
776         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
777         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
778                 $GR_STR4 $DIR || error "set group grace time quota failed"
779         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
780
781         # test of invalid grace strings handling
782         echo "  Invalid grace strings test"
783         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
784         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
785
786         # cleanup
787         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
788                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
789         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
790                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
791 }
792 run_test 4b "Grace time strings handling"
793
794 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
795 test_5() {
796         local BLIMIT=10 # 10M
797         local ILIMIT=10 # 10 inodes
798
799         setup_quota_test
800         trap cleanup_quota_test EXIT
801
802         set_mdt_qtype "ug" || error "enable mdt quota failed"
803         set_ost_qtype "ug" || error "enable ost quota failed"
804
805         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
806         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
807                 error "set user quota failed"
808         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
809                 error "set group quota failed"
810
811         # make sure the system is clean
812         local USED=$(getquota -u $TSTUSR global curinodes)
813         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
814         USED=$(getquota -g $TSTUSR global curinodes)
815         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
816         USED=$(getquota -u $TSTUSR global curspace)
817         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
818         USED=$(getquota -g $TSTUSR global curspace)
819         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
820
821         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
822         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
823                 error "create failure, expect success"
824         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
825                 error "write failure, expect success"
826
827         echo "Chown files to $TSTUSR.$TSTUSR ..."
828         for i in `seq 0 $ILIMIT`; do
829                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
830                         quota_error a $TSTUSR "chown failure, expect success"
831         done
832
833         # cleanup
834         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
835         cleanup_quota_test
836
837         resetquota -u $TSTUSR
838         resetquota -g $TSTUSR
839 }
840 run_test 5 "Chown & chgrp successfully even out of block/file quota"
841
842 # test dropping acquire request on master
843 test_6() {
844         local LIMIT=3 # 3M
845
846         setup_quota_test
847         trap cleanup_quota_test EXIT
848
849         # make sure the system is clean
850         local USED=$(getquota -u $TSTUSR global curspace)
851         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
852
853         # make sure no granted quota on ost
854         set_ost_qtype "ug" || error "enable ost quota failed"
855         resetquota -u $TSTUSR
856
857         # create file for $TSTUSR
858         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
859         $LFS setstripe $TESTFILE -c 1 -i 0
860         chown $TSTUSR.$TSTUSR $TESTFILE
861
862         # create file for $TSTUSR2
863         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
864         $LFS setstripe $TESTFILE2 -c 1 -i 0
865         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
866
867         # cache per-ID lock for $TSTUSR on slave
868         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
869                 error "set quota failed"
870         $RUNAS $DD of=$TESTFILE count=1 ||
871                 error "write $TESTFILE failure, expect success"
872         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
873                 error "write $TESTFILE2 failure, expect success"
874         sync; sync
875         sync_all_data || true
876
877         #define QUOTA_DQACQ 601
878         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
879         lustre_fail mds 0x513 601
880
881         # write to un-enforced ID ($TSTUSR2) should succeed
882         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
883                 error "write failure, expect success"
884
885         # write to enforced ID ($TSTUSR) in background, exceeding limit
886         # to make sure DQACQ is sent
887         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
888         DDPID=$!
889
890         # watchdog timer uses a factor of 2
891         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
892         sleep $((TIMEOUT * 2 + 1))
893
894         # write should be blocked and never finished
895         if ! ps -p $DDPID  > /dev/null 2>&1; then
896                 lustre_fail mds 0 0
897                 error "write finished incorrectly!"
898         fi
899
900         lustre_fail mds 0 0
901
902         # no watchdog is triggered
903         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
904         watchdog=$(awk '/sanity-quota test 6/ {start = 1;}
905                        /Service thread pid/ && /was inactive/ {
906                                if (start) {
907                                        print;
908                                }
909                        }' $TMP/lustre-log-${TESTNAME}.log)
910         [ -z "$watchdog" ] || error "$watchdog"
911
912         rm -f $TMP/lustre-log-${TESTNAME}.log
913
914         # write should continue & succeed
915         local count=0
916         while [ true ]; do
917                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
918                 if [ $count -ge 120 ]; then
919                         quota_error u $TSTUSR "dd not finished in $count secs"
920                 fi
921                 count=$((count + 1))
922                 [ $((count % 10)) -eq 0 ] && echo "Waiting $count secs"
923                 sleep 1
924         done
925
926         cleanup_quota_test
927         resetquota -u $TSTUSR
928 }
929 run_test 6 "Test dropping acquire request on master"
930
931 # quota reintegration (global index)
932 test_7a() {
933         local TESTFILE=$DIR/$tdir/$tfile
934         local LIMIT=20 # 20M
935
936         [ "$SLOW" = "no" ] && LIMIT=5
937
938         setup_quota_test
939         trap cleanup_quota_test EXIT
940
941         # make sure the system is clean
942         local USED=$(getquota -u $TSTUSR global curspace)
943         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
944
945         # make sure no granted quota on ost1
946         set_ost_qtype "ug" || error "enable ost quota failed"
947         resetquota -u $TSTUSR
948         set_ost_qtype "none" || error "disable ost quota failed"
949
950         local OSTUUID=$(ostuuid_from_index 0)
951         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
952         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
953                 "$TSTUSR isn't 0."
954
955         # create test file
956         $LFS setstripe $TESTFILE -c 1 -i 0
957         chown $TSTUSR.$TSTUSR $TESTFILE
958
959         echo "Stop ost1..."
960         stop ost1
961
962         echo "Enable quota & set quota limit for $TSTUSR"
963         set_ost_qtype "ug" || error "enable ost quota failed"
964         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
965                 error "set quota failed"
966
967         echo "Start ost1..."
968         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
969         quota_init
970
971         wait_ost_reint "ug" || error "reintegration failed"
972
973         # hardlimit should have been fetched by slave during global
974         # reintegration, write will exceed quota
975         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
976                 quota_error u $TSTUSR "write success, but expect EDQUOT"
977
978         rm -f $TESTFILE
979         wait_delete_completed
980         sync_all_data || true
981         sleep 3
982
983         echo "Stop ost1..."
984         stop ost1
985
986         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
987                 error "clear quota failed"
988
989         echo "Start ost1..."
990         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
991         quota_init
992
993         wait_ost_reint "ug" || error "reintegration failed"
994
995         # hardlimit should be cleared on slave during reintegration
996         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
997                 quota_error u $TSTUSR "write error, but expect success"
998
999         cleanup_quota_test
1000         resetquota -u $TSTUSR
1001 }
1002 run_test 7a "Quota reintegration (global index)"
1003
1004 # quota reintegration (slave index)
1005 test_7b() {
1006         local LIMIT="100G"
1007         local TESTFILE=$DIR/$tdir/$tfile
1008
1009         setup_quota_test
1010         trap cleanup_quota_test EXIT
1011
1012         # make sure the system is clean
1013         local USED=$(getquota -u $TSTUSR global curspace)
1014         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1015
1016         # make sure no granted quota on ost1
1017         set_ost_qtype "ug" || error "enable ost quota failed"
1018         resetquota -u $TSTUSR
1019         set_ost_qtype "none" || error "disable ost quota failed"
1020
1021         local OSTUUID=$(ostuuid_from_index 0)
1022         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1023         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
1024                 "$TSTUSR isn't 0."
1025
1026         # create test file
1027         $LFS setstripe $TESTFILE -c 1 -i 0
1028         chown $TSTUSR.$TSTUSR $TESTFILE
1029
1030         # consume some space to make sure the granted space will not
1031         # be released during reconciliation
1032         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1033                 error "consume space failure, expect success"
1034
1035         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1036         lustre_fail mds 0xa02
1037
1038         set_ost_qtype "ug" || error "enable ost quota failed"
1039         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1040                 error "set quota failed"
1041
1042         # ignore the write error
1043         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1044
1045         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1046
1047         lustre_fail mds 0
1048
1049         echo "Restart ost to trigger reintegration..."
1050         stop ost1
1051         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
1052         quota_init
1053
1054         wait_ost_reint "ug" || error "reintegration failed"
1055
1056         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1057         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1058
1059         cleanup_quota_test
1060         resetquota -u $TSTUSR
1061         $SHOW_QUOTA_USER
1062 }
1063 run_test 7b "Quota reintegration (slave index)"
1064
1065 # quota reintegration (restart mds during reintegration)
1066 test_7c() {
1067         local LIMIT=20 # 20M
1068         local TESTFILE=$DIR/$tdir/$tfile
1069
1070         [ "$SLOW" = "no" ] && LIMIT=5
1071
1072         setup_quota_test
1073         trap cleanup_quota_test EXIT
1074
1075         # make sure the system is clean
1076         local USED=$(getquota -u $TSTUSR global curspace)
1077         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1078
1079         set_ost_qtype "none" || error "disable ost quota failed"
1080         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1081                 error "set quota failed"
1082
1083         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1084         lustre_fail ost 0xa03
1085
1086         # enable ost quota to trigger reintegration
1087         set_ost_qtype "ug" || error "enable ost quota failed"
1088
1089         echo "Stop mds..."
1090         stop mds1
1091
1092         lustre_fail ost 0
1093
1094         echo "Start mds..."
1095         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1096         quota_init
1097
1098         wait_ost_reint "ug" || error "reintegration failed"
1099
1100         # hardlimit should have been fetched by slave during global
1101         # reintegration, write will exceed quota
1102         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1103                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1104
1105         cleanup_quota_test
1106         resetquota -u $TSTUSR
1107 }
1108 run_test 7c "Quota reintegration (restart mds during reintegration)"
1109
1110 # Quota reintegration (Transfer index in multiple bulks)
1111 test_7d(){
1112         local TESTFILE=$DIR/$tdir/$tfile
1113         local TESTFILE1="$DIR/$tdir/$tfile"-1
1114         local limit=20 #20M
1115
1116         setup_quota_test
1117         trap cleanup_quota_test EXIT
1118
1119         set_ost_qtype "none" || error "disable ost quota failed"
1120         # LU-2284. Enable trace for debug log.
1121         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+trace"
1122         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1123                 error "set quota for $TSTUSR failed"
1124         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1125                 error "set quota for $TSTUSR2 failed"
1126         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-trace"
1127
1128         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1129         lustre_fail mds 0x608 0
1130
1131         # enable quota to tirgger reintegration
1132         set_ost_qtype "u" || error "enable ost quota failed"
1133         wait_ost_reint "u" || error "reintegration failed"
1134
1135         lustre_fail mds 0
1136
1137         # hardlimit should have been fetched by slave during global
1138         # reintegration, write will exceed quota
1139         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1140                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1141
1142         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1143                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1144
1145         cleanup_quota_test
1146         resetquota -u $TSTUSR
1147         resetquota -u $TSTUSR2
1148 }
1149 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1150
1151 # run dbench with quota enabled
1152 test_8() {
1153         local BLK_LIMIT="100g" #100G
1154         local FILE_LIMIT=1000000
1155
1156         setup_quota_test
1157         trap cleanup_quota_test EXIT
1158
1159         set_mdt_qtype "ug" || error "enable mdt quota failed"
1160         set_ost_qtype "ug" || error "enable ost quota failed"
1161
1162         echo "Set enough high limit for user: $TSTUSR"
1163         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1164                 error "set user quota failed"
1165         echo "Set enough high limit for group: $TSTUSR"
1166         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1167                 error "set group quota failed"
1168
1169         local duration=""
1170         [ "$SLOW" = "no" ] && duration=" -t 120"
1171         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1172                 quota_error a $TSTUSR "dbench failed!"
1173
1174         cleanup_quota_test
1175         resetquota -u $TSTUSR
1176         resetquota -g $TSTUSR
1177 }
1178 run_test 8 "Run dbench with quota enabled"
1179
1180 # this check is just for test_9
1181 OST0_MIN=4900000 #4.67G
1182
1183 check_whether_skip () {
1184         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1185         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1186         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1187                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1188                 return 0
1189         else
1190                 return 1
1191         fi
1192 }
1193
1194 # run for fixing bug10707, it needs a big room. test for 64bit
1195 test_9() {
1196         local filesize=$((1024 * 1024 * 1024 * 9 / 2)) # 4.5G
1197
1198         check_whether_skip && return 0
1199
1200         setup_quota_test
1201         trap cleanup_quota_test EXIT
1202
1203         set_ost_qtype "ug" || error "enable ost quota failed"
1204
1205         local TESTFILE="$DIR/$tdir/$tfile-0"
1206         local BLK_LIMIT=100G #100G
1207         local FILE_LIMIT=1000000
1208
1209         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1210
1211         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1212                 "for user: $TSTUSR"
1213         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1214                 error "set user quota failed"
1215
1216         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1217                 "for group: $TSTUSR"
1218         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1219                 error "set group quota failed"
1220
1221         quota_show_check a u $TSTUSR
1222         quota_show_check a g $TSTUSR
1223
1224         echo "Create test file"
1225         $LFS setstripe $TESTFILE -c 1 -i 0
1226         chown $TSTUSR.$TSTUSR $TESTFILE
1227
1228         log "Write the big file of 4.5G ..."
1229         $RUNAS $DD of=$TESTFILE count=$filesize ||
1230                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1231
1232         $SHOW_QUOTA_USER
1233         $SHOW_QUOTA_GROUP
1234
1235         cleanup_quota_test
1236         resetquota -u $TSTUSR
1237         resetquota -g $TSTUSR
1238
1239         $SHOW_QUOTA_USER
1240         $SHOW_QUOTA_GROUP
1241 }
1242 run_test 9 "Block limit larger than 4GB (b10707)"
1243
1244 test_10() {
1245         local TESTFILE=$DIR/$tdir/$tfile
1246
1247         setup_quota_test
1248         trap cleanup_quota_test EXIT
1249
1250         # set limit to root user should fail
1251         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1252                 error "set limit for root user successfully, expect failure"
1253         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1254                 error "set limit for root group successfully, expect failure"
1255
1256         # root user can overrun quota
1257         set_ost_qtype "ug" || "enable ost quota failed"
1258
1259         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1260                 error "set quota failed"
1261         quota_show_check b u $TSTUSR
1262
1263         $LFS setstripe $TESTFILE -c 1
1264         chown $TSTUSR.$TSTUSR $TESTFILE
1265
1266         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1267                 error "write failure, expect success"
1268
1269         cleanup_quota_test
1270         resetquota -u $TSTUSR
1271 }
1272 run_test 10 "Test quota for root user"
1273
1274 test_11() {
1275         local TESTFILE=$DIR/$tdir/$tfile
1276         setup_quota_test
1277         trap cleanup_quota_test EXIT
1278
1279         set_mdt_qtype "ug" || "enable mdt quota failed"
1280         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1281                 error "set quota failed"
1282
1283         touch "$TESTFILE"-0
1284         touch "$TESTFILE"-1
1285
1286         chown $TSTUSR.$TSTUSR "$TESTFILE"-0
1287         chown $TSTUSR.$TSTUSR "$TESTFILE"-1
1288
1289         $SHOW_QUOTA_USER
1290         local USED=$(getquota -u $TSTUSR global curinodes)
1291         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1292
1293         cleanup_quota_test
1294         resetquota -u $TSTUSR
1295 }
1296 run_test 11 "Chown/chgrp ignores quota"
1297
1298 test_12() {
1299         [ "$OSTCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1300
1301         local blimit=22 # 22M
1302         local blk_cnt=$((blimit - 5))
1303         local TESTFILE0="$DIR/$tdir/$tfile"-0
1304         local TESTFILE1="$DIR/$tdir/$tfile"-1
1305
1306         setup_quota_test
1307         trap cleanup_quota_test EXIT
1308
1309         set_ost_qtype "u" || "enable ost quota failed"
1310         quota_show_check b u $TSTUSR
1311
1312         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1313                 error "set quota failed"
1314
1315         $LFS setstripe $TESTFILE0 -c 1 -i 0
1316         $LFS setstripe $TESTFILE1 -c 1 -i 1
1317         chown $TSTUSR.$TSTUSR $TESTFILE0
1318         chown $TSTUSR.$TSTUSR $TESTFILE1
1319
1320         echo "Write to ost0..."
1321         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1322                 quota_error a $TSTUSR "dd failed"
1323
1324         echo "Write to ost1..."
1325         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1326                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1327
1328         echo "Free space from ost0..."
1329         rm -f $TESTFILE0
1330         wait_delete_completed
1331         sync_all_data || true
1332
1333         echo "Write to ost1 after space freed from ost0..."
1334         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1335                 quota_error a $TSTUSR "rebalancing failed"
1336
1337         cleanup_quota_test
1338         resetquota -u $TSTUSR
1339 }
1340 run_test 12 "Block quota rebalancing"
1341
1342 test_13(){
1343         local TESTFILE=$DIR/$tdir/$tfile
1344         # the name of osp on ost1 name is MDT0000-osp-OST0000
1345         local procf="ldlm.namespaces.*MDT0000-osp-OST0000.lru_size"
1346
1347         setup_quota_test
1348         trap cleanup_quota_test EXIT
1349
1350         set_ost_qtype "u" || "enable ost quota failed"
1351         quota_show_check b u $TSTUSR
1352
1353         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1354                 error "set quota failed"
1355         $LFS setstripe $TESTFILE -c 1 -i 0
1356         chown $TSTUSR.$TSTUSR $TESTFILE
1357
1358         # clear the locks in cache first
1359         do_facet ost1 $LCTL set_param -n $procf=clear
1360         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1361         [ $nlock -eq 0 ] || error "$nlock cached locks"
1362
1363         # write to acquire the per-ID lock
1364         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1365                 quota_error a $TSTUSR "dd failed"
1366
1367         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1368         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1369
1370         # clear quota doesn't trigger per-ID lock cancellation
1371         resetquota -u $TSTUSR
1372         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1373         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1374
1375         # clear the per-ID lock
1376         do_facet ost1 $LCTL set_param -n $procf=clear
1377         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1378         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1379
1380         # spare quota should be released
1381         local OSTUUID=$(ostuuid_from_index 0)
1382         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1383         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1384         [ $limit -le $space ] ||
1385                 error "spare quota isn't released, limit:$limit, space:$space"
1386
1387         cleanup_quota_test
1388 }
1389 run_test 13 "Cancel per-ID lock in the LRU list"
1390
1391 test_15(){
1392         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1393
1394         wait_delete_completed
1395         sync_all_data || true
1396
1397         # test for user
1398         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1399                 error "set user quota failed"
1400         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1401         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1402                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1403         resetquota -u $TSTUSR
1404
1405         # test for group
1406         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1407                 error "set group quota failed"
1408         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1409         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1410                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1411         resetquota -g $TSTUSR
1412 }
1413 run_test 15 "Set over 4T block quota"
1414
1415 test_17sub() {
1416         local err_code=$1
1417         local BLKS=1    # 1M less than limit
1418         local TESTFILE=$DIR/$tdir/$tfile
1419
1420         setup_quota_test
1421         trap cleanup_quota_test EXIT
1422
1423         # make sure the system is clean
1424         local USED=$(getquota -u $TSTUSR global curspace)
1425         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1426
1427         set_ost_qtype "ug" || error "enable ost quota failed"
1428         # make sure no granted quota on ost
1429         resetquota -u $TSTUSR
1430         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1431                 error "set quota failed"
1432
1433         quota_show_check b u $TSTUSR
1434
1435         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1436         lustre_fail mds 0xa04 $err_code
1437
1438         # write in background
1439         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1440         local DDPID=$!
1441
1442         sleep 2
1443         # write should be blocked and never finished
1444         if ! ps -p $DDPID  > /dev/null 2>&1; then
1445                 lustre_fail mds 0 0
1446                 quota_error u $TSTUSR "write finished incorrectly!"
1447         fi
1448
1449         lustre_fail mds 0 0
1450
1451         local count=0
1452         local timeout=30
1453         while [ true ]; do
1454                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1455                 count=$((count+1))
1456                 if [ $count -gt $timeout ]; then
1457                         quota_error u $TSTUSR "dd is not finished!"
1458                 fi
1459                 sleep 1
1460         done
1461
1462         sync; sync_all_data || true
1463
1464         USED=$(getquota -u $TSTUSR global curspace)
1465         [ $USED -ge $(($BLKS * 1024)) ] || quota_error u $TSTUSR \
1466                 "Used space(${USED}K) is less than ${BLKS}M"
1467
1468         cleanup_quota_test
1469         resetquota -u $TSTUSR
1470 }
1471
1472 # DQACQ return recoverable error
1473 test_17() {
1474         echo "DQACQ return -ENOLCK"
1475         #define ENOLCK  37
1476         test_17sub 37 || error "Handle -ENOLCK failed"
1477
1478         echo "DQACQ return -EAGAIN"
1479         #define EAGAIN  11
1480         test_17sub 11 || error "Handle -EAGAIN failed"
1481
1482         echo "DQACQ return -ETIMEDOUT"
1483         #define ETIMEDOUT 110
1484         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1485
1486         echo "DQACQ return -ENOTCONN"
1487         #define ENOTCONN 107
1488         test_17sub 107 || error "Handle -ENOTCONN failed"
1489 }
1490
1491 run_test 17 "DQACQ return recoverable error"
1492
1493 test_18_sub () {
1494         local io_type=$1
1495         local blimit="200m" # 200M
1496         local TESTFILE="$DIR/$tdir/$tfile"
1497
1498         setup_quota_test
1499         trap cleanup_quota_test EXIT
1500
1501         set_ost_qtype "u" || error "enable ost quota failed"
1502         log "User quota (limit: $blimit)"
1503         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1504                 error "set quota failed"
1505         quota_show_check b u $TSTUSR
1506
1507         $LFS setstripe $TESTFILE -i 0 -c 1
1508         chown $TSTUSR.$TSTUSR $TESTFILE
1509
1510         local timeout=$(sysctl -n lustre.timeout)
1511
1512         if [ $io_type = "directio" ]; then
1513                 log "Write 100M (directio) ..."
1514                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1515         else
1516                 log "Write 100M (buffered) ..."
1517                 $RUNAS $DD of=$TESTFILE count=100 &
1518         fi
1519         local DDPID=$!
1520
1521         replay_barrier $SINGLEMDS
1522         log "Fail mds for $((2 * timeout)) seconds"
1523         fail $SINGLEMDS $((2 * timeout))
1524
1525         local count=0
1526         if at_is_enabled; then
1527                 timeout=$(at_max_get mds)
1528         else
1529                 timeout=$(lctl get_param -n timeout)
1530         fi
1531
1532         while [ true ]; do
1533                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1534                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1535                         log "it took $count second"
1536                 fi
1537                 sleep 1
1538         done
1539
1540         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1541         sync
1542         cancel_lru_locks mdc
1543         cancel_lru_locks osc
1544         $SHOW_QUOTA_USER
1545
1546         local testfile_size=$(stat -c %s $TESTFILE)
1547         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1548                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)),"
1549                         "got ${testfile_size}. Verifying file failed!"
1550         fi
1551         cleanup_quota_test
1552         resetquota -u $TSTUSR
1553 }
1554
1555 # test when mds does failover, the ost still could work well
1556 # this test shouldn't trigger watchdog b=14840
1557 test_18() {
1558         test_18_sub normal
1559         test_18_sub directio
1560
1561         # check if watchdog is triggered
1562         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1563         local watchdog=$(awk '/sanity-quota test 18/ {start = 1;}
1564                        /Service thread pid/ && /was inactive/ {
1565                                if (start) {
1566                                        print;
1567                                }
1568                        }' $TMP/lustre-log-${TESTNAME}.log)
1569         [ -z "$watchdog" ] || error "$watchdog"
1570         rm -f $TMP/lustre-log-${TESTNAME}.log
1571 }
1572 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1573
1574 test_19() {
1575         local blimit=5 # 5M
1576         local TESTFILE=$DIR/$tdir/$tfile
1577
1578         setup_quota_test
1579         trap cleanup_quota_test EXIT
1580
1581         set_ost_qtype "ug" || error "enable ost quota failed"
1582
1583         # bind file to a single OST
1584         $LFS setstripe -c 1 $TESTFILE
1585         chown $TSTUSR.$TSTUSR $TESTFILE
1586
1587         echo "Set user quota (limit: "$blimit"M)"
1588         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1589                 error "set user quota failed"
1590         quota_show_check b u $TSTUSR
1591         echo "Update quota limits"
1592         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1593                 error "set group quota failed"
1594         quota_show_check b u $TSTUSR
1595
1596         # first wirte might be cached
1597         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
1598         cancel_lru_locks osc
1599         $SHOW_QUOTA_USER
1600         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
1601                 quota_error u $TSTUSR "Write success, expect failure"
1602         $SHOW_QUOTA_USER
1603
1604         cleanup_quota_test
1605         resetquota -u $TSTUSR
1606 }
1607 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
1608
1609 test_20() { # b15754
1610         local LSTR=(2g 1t 4k 3m) # limits strings
1611         # limits values
1612         local LVAL=($[2*1024*1024] $[1*1024*1024*1024] $[4*1024] $[3*1024*1024])
1613
1614         resetquota -u $TSTUSR
1615
1616         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1617                 $MOUNT || error "could not set quota limits"
1618         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1619                                 --inode-softlimit ${LSTR[2]} \
1620                                 --inode-hardlimit ${LSTR[3]} \
1621                                 $MOUNT || error "could not set quota limits"
1622
1623         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
1624                 error "bsoftlimit was not set properly"
1625         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
1626                 error "bhardlimit was not set properly"
1627         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
1628                 error "isoftlimit was not set properly"
1629         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
1630                 error "ihardlimit was not set properly"
1631
1632         resetquota -u $TSTUSR
1633 }
1634 run_test 20 "Test if setquota specifiers work properly (b15754)"
1635
1636 test_21_sub() {
1637         local testfile=$1
1638         local blk_number=$2
1639         local seconds=$3
1640
1641         local time=$(($(date +%s) + seconds))
1642         while [ $(date +%s) -lt $time ]; do
1643                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
1644         done
1645 }
1646
1647 # run for fixing bug16053, setquota shouldn't fail when writing and
1648 # deleting are happening
1649 test_21() {
1650         local TESTFILE="$DIR/$tdir/$tfile"
1651         local BLIMIT=10 # 10G
1652         local ILIMIT=1000000
1653
1654         setup_quota_test
1655         trap cleanup_quota_test EXIT
1656
1657         set_ost_qtype "ug" || error "Enable ost quota failed"
1658
1659         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
1660         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
1661                 error "set user quota failed"
1662         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
1663         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
1664                 error "set group quota failed"
1665
1666         # repeat writing on a 1M file
1667         test_21_sub ${TESTFILE}_1 1 30 &
1668         local DDPID1=$!
1669         # repeat writing on a 128M file
1670         test_21_sub ${TESTFILE}_2 128 30 &
1671         local DDPID2=$!
1672
1673         local time=$(($(date +%s) + 30))
1674         local i=1
1675         while [ $(date +%s) -lt $time ]; do
1676                 log "Set quota for $i times"
1677                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1678                         -I $((ILIMIT + i)) $MOUNT ||
1679                                 error "Set user quota failed"
1680                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1681                         -I $((ILIMIT + i)) $MOUNT ||
1682                                 error "Set group quota failed"
1683                 i=$((i+1))
1684                 sleep 1
1685         done
1686
1687         local count=0
1688         while [ true ]; do
1689                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1690                 count=$((count+1))
1691                 if [ $count -gt 60 ]; then
1692                         quota_error a $TSTUSR "dd should be finished!"
1693                 fi
1694                 sleep 1
1695         done
1696         echo "(dd_pid=$DDPID1, time=$count)successful"
1697
1698         count=0
1699         while [ true ]; do
1700                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1701                 count=$((count+1))
1702                 if [ $count -gt 60 ]; then
1703                         quota_error a $TSTUSR "dd should be finished!"
1704                 fi
1705                 sleep 1
1706         done
1707         echo "(dd_pid=$DDPID2, time=$count)successful"
1708
1709         cleanup_quota_test
1710         resetquota -u $TSTUSR
1711         resetquota -g $TSTUSR
1712 }
1713 run_test 21 "Setquota while writing & deleting (b16053)"
1714
1715 # enable/disable quota enforcement permanently
1716 test_22() {
1717         echo "Set both mdt & ost quota type as ug"
1718         set_mdt_qtype "ug" || error "enable mdt quota failed"
1719         set_ost_qtype "ug" || error "enable ost quota failed"
1720
1721         echo "Restart..."
1722         stopall
1723         mount
1724         setupall
1725
1726         echo "Verify if quota is enabled"
1727         local qtype=$(mdt_quota_type)
1728         [ $qtype != "ug" ] && error "mdt quota setting is lost"
1729         qtype=$(ost_quota_type)
1730         [ $qtype != "ug" ] && error "ost quota setting is lost"
1731
1732         echo "Set both mdt & ost quota type as none"
1733         set_mdt_qtype "none" || error "disable mdt quota failed"
1734         set_ost_qtype "none" || error "disable ost quota failed"
1735
1736         echo "Restart..."
1737         stopall
1738         mount
1739         setupall
1740         quota_init
1741
1742         echo "Verify if quota is disabled"
1743         qtype=$(mdt_quota_type)
1744         [ $qtype != "none" ] && error "mdt quota setting is lost"
1745         qtype=$(ost_quota_type)
1746         [ $qtype != "none" ] && error "ost quota setting is lost"
1747
1748         return 0
1749 }
1750 run_test 22 "enable/disable quota by 'lctl conf_param'"
1751
1752 test_23_sub() {
1753         local TESTFILE="$DIR/$tdir/$tfile"
1754         local LIMIT=$1
1755
1756         setup_quota_test
1757         trap cleanup_quota_test EXIT
1758
1759         set_ost_qtype "ug" || error "Enable ost quota failed"
1760
1761         # test for user
1762         log "User quota (limit: $LIMIT MB)"
1763         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
1764                 error "set quota failed"
1765         quota_show_check b u $TSTUSR
1766
1767         $LFS setstripe $TESTFILE -c 1 -i 0
1768         chown $TSTUSR.$TSTUSR $TESTFILE
1769
1770         log "Step1: trigger EDQUOT with O_DIRECT"
1771         log "Write half of file"
1772         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
1773                 quota_error u $TSTUSR "(1) Write failure, expect success." \
1774                         "limit=$LIMIT"
1775         log "Write out of block quota ..."
1776         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
1777                 oflag=direct conv=notrunc &&
1778                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
1779                         "limit=$LIMIT"
1780         log "Step1: done"
1781
1782         log "Step2: rewrite should succeed"
1783         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
1784                 quota_error u $TSTUSR "(3) Write failure, expect success." \
1785                         "limit=$LIMIT"
1786         log "Step2: done"
1787
1788         cleanup_quota_test
1789
1790         local OST0_UUID=$(ostuuid_from_index 0)
1791         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
1792         [ $OST0_QUOTA_USED -ne 0 ] &&
1793                 ($SHOW_QUOTA_USER; \
1794                 quota_error u $TSTUSR "quota isn't released")
1795         $SHOW_QUOTA_USER
1796         resetquota -u $TSTUSR
1797 }
1798
1799 test_23() {
1800         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
1801         check_whether_skip && return 0
1802         log "run for 4MB test file"
1803         test_23_sub 4
1804
1805         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
1806         check_whether_skip && return 0
1807         log "run for 40MB test file"
1808         test_23_sub 40
1809 }
1810 run_test 23 "Quota should be honored with directIO (b16125)"
1811
1812 test_24() {
1813         local blimit=5 # 5M
1814         local TESTFILE="$DIR/$tdir/$tfile"
1815
1816         setup_quota_test
1817         trap cleanup_quota_test EXIT
1818
1819         set_ost_qtype "ug" || error "enable ost quota failed"
1820
1821         # bind file to a single OST
1822         $LFS setstripe -c 1 $TESTFILE
1823         chown $TSTUSR.$TSTUSR $TESTFILE
1824
1825         echo "Set user quota (limit: "$blimit"M)"
1826         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1827                 error "set quota failed"
1828
1829         # overrun quota by root user
1830         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
1831                 error "write failure, expect success"
1832         cancel_lru_locks osc
1833         sync_all_data || true
1834
1835         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1836
1837         cleanup_quota_test
1838         resetquota -u $TSTUSR
1839 }
1840 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
1841
1842 test_27a() { # b19612
1843         $LFS quota $TSTUSR $DIR &&
1844                 error "lfs succeeded with no type, but should have failed"
1845         $LFS setquota $TSTUSR $DIR &&
1846                 error "lfs succeeded with no type, but should have failed"
1847         return 0
1848 }
1849 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
1850
1851 test_27b() { # b20200
1852         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1853                 error "lfs setquota failed with uid argument"
1854         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1855                 error "lfs stequota failed with gid argument"
1856         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
1857         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
1858         resetquota -u $TSTUSR
1859         resetquota -g $TSTUSR
1860         return 0
1861 }
1862 run_test 27b "lfs quota/setquota should handle user/group ID (b20200)"
1863
1864 test_30() {
1865         local output
1866         local LIMIT=4 # 4MB
1867         local TESTFILE="$DIR/$tdir/$tfile"
1868         local GRACE=10
1869
1870         setup_quota_test
1871         trap cleanup_quota_test EXIT
1872
1873         set_ost_qtype "u" || error "enable ost quota failed"
1874
1875         $LFS setstripe $TESTFILE -i 0 -c 1
1876         chown $TSTUSR.$TSTUSR $TESTFILE
1877
1878         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
1879                 $MAX_IQ_TIME $DIR || error "set grace time failed"
1880         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
1881                 error "set quota failed"
1882         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
1883         cancel_lru_locks osc
1884         sleep $GRACE
1885         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
1886         # over-quota flag has not yet settled since we do not trigger async
1887         # events based on grace time period expiration
1888         $SHOW_QUOTA_USER
1889         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 || true
1890         cancel_lru_locks osc
1891         # now over-quota flag should be settled and further writes should fail
1892         $SHOW_QUOTA_USER
1893         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 &&
1894                 error "grace times were reset"
1895         # cleanup
1896         cleanup_quota_test
1897         resetquota -u $TSTUSR
1898         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1899                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
1900 }
1901 run_test 30 "Hard limit updates should not reset grace times"
1902
1903 # basic usage tracking for user & group
1904 test_33() {
1905         local INODES=10 # 10 files
1906         local BLK_CNT=2 # of 2M each
1907         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
1908
1909         setup_quota_test
1910         trap cleanup_quota_test EXIT
1911
1912         # make sure the system is clean
1913         local USED=$(getquota -u $TSTID global curspace)
1914         [ $USED -ne 0 ] &&
1915                 error "Used space ($USED) for user $TSTID isn't 0."
1916         USED=$(getquota -g $TSTID global curspace)
1917         [ $USED -ne 0 ] &&
1918                 error "Used space ($USED) for group $TSTID isn't 0."
1919
1920         echo "Write files..."
1921         for i in `seq 0 $INODES`; do
1922                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
1923                         error "write failed"
1924                 echo "Iteration $i/$INODES completed"
1925         done
1926         cancel_lru_locks osc
1927         sync; sync_all_data || true
1928
1929         echo "Verify disk usage after write"
1930         USED=$(getquota -u $TSTID global curspace)
1931         [ $USED -lt $TOTAL_BLKS ] &&
1932                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
1933         USED=$(getquota -g $TSTID global curspace)
1934         [ $USED -lt $TOTAL_BLKS ] &&
1935                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
1936
1937         echo "Verify inode usage after write"
1938         USED=$(getquota -u $TSTID global curinodes)
1939         [ $USED -lt $INODES ] &&
1940                 error "Used inode for user $TSTID is $USED, expected $INODES"
1941         USED=$(getquota -g $TSTID global curinodes)
1942         [ $USED -lt $INODES ] &&
1943                 error "Used inode for group $TSTID is $USED, expected $INODES"
1944
1945         cleanup_quota_test
1946
1947         echo "Verify disk usage after delete"
1948         USED=$(getquota -u $TSTID global curspace)
1949         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
1950         USED=$(getquota -u $TSTID global curinodes)
1951         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
1952         USED=$(getquota -g $TSTID global curspace)
1953         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
1954         USED=$(getquota -g $TSTID global curinodes)
1955         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
1956 }
1957 run_test 33 "Basic usage tracking for user & group"
1958
1959 # usage transfer test for user & group
1960 test_34() {
1961         local BLK_CNT=2 # 2MB
1962
1963         setup_quota_test
1964         trap cleanup_quota_test EXIT
1965
1966         # make sure the system is clean
1967         local USED=$(getquota -u $TSTID global curspace)
1968         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
1969         USED=$(getquota -g $TSTID global curspace)
1970         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
1971
1972         echo "Write file..."
1973         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
1974                 error "write failed"
1975         cancel_lru_locks osc
1976         sync; sync_all_data || true
1977
1978         echo "chown the file to user $TSTID"
1979         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
1980
1981         echo "Wait for setattr on objects finished..."
1982         wait_delete_completed
1983
1984         BLK_CNT=$((BLK_CNT * 1024))
1985
1986         echo "Verify disk usage for user $TSTID"
1987         USED=$(getquota -u $TSTID global curspace)
1988         [ $USED -lt $BLK_CNT ] &&
1989                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
1990         USED=$(getquota -u $TSTID global curinodes)
1991         [ $USED -ne 1 ] &&
1992                 error "Used inodes for user $TSTID is $USED, expected 1"
1993
1994         echo "chgrp the file to group $TSTID"
1995         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
1996
1997         echo "Wait for setattr on objects finished..."
1998         wait_delete_completed
1999
2000         echo "Verify disk usage for group $TSTID"
2001         USED=$(getquota -g $TSTID global curspace)
2002         [ $USED -ge $BLK_CNT ] ||
2003                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2004         USED=$(getquota -g $TSTID global curinodes)
2005         [ $USED -eq 1 ] ||
2006                 error "Used inodes for group $TSTID is $USED, expected 1"
2007
2008         cleanup_quota_test
2009 }
2010 run_test 34 "Usage transfer for user & group"
2011
2012 # usage is still accessible across restart
2013 test_35() {
2014         local BLK_CNT=2 # 2 MB
2015
2016         setup_quota_test
2017         trap cleanup_quota_test EXIT
2018
2019         echo "Write file..."
2020         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2021                 error "write failed"
2022         cancel_lru_locks osc
2023         sync; sync_all_data || true
2024
2025         echo "Save disk usage before restart"
2026         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2027         [ $ORIG_USR_SPACE -eq 0 ] &&
2028                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2029         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2030         [ $ORIG_USR_INODES -eq 0 ] &&
2031                 error "Used inodes for user $TSTID is 0, expected 1"
2032         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2033         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2034         [ $ORIG_GRP_SPACE -eq 0 ] &&
2035                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2036         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2037         [ $ORIG_GRP_INODES -eq 0 ] &&
2038                 error "Used inodes for group $TSTID is 0, expected 1"
2039         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2040
2041         log "Restart..."
2042         local ORIG_REFORMAT=$REFORMAT
2043         REFORMAT=""
2044         cleanup_and_setup_lustre
2045         REFORMAT=$ORIG_REFORMAT
2046         quota_init
2047
2048         echo "Verify disk usage after restart"
2049         local USED=$(getquota -u $TSTID global curspace)
2050         [ $USED -eq $ORIG_USR_SPACE ] ||
2051                 error "Used space for user $TSTID changed from " \
2052                         "$ORIG_USR_SPACE to $USED"
2053         USED=$(getquota -u $TSTID global curinodes)
2054         [ $USED -eq $ORIG_USR_INODES ] ||
2055                 error "Used inodes for user $TSTID changed from " \
2056                         "$ORIG_USR_INODES to $USED"
2057         USED=$(getquota -g $TSTID global curspace)
2058         [ $USED -eq $ORIG_GRP_SPACE ] ||
2059                 error "Used space for group $TSTID changed from " \
2060                         "$ORIG_GRP_SPACE to $USED"
2061         USED=$(getquota -g $TSTID global curinodes)
2062         [ $USED -eq $ORIG_GRP_INODES ] ||
2063                 error "Used inodes for group $TSTID changed from " \
2064                         "$ORIG_GRP_INODES to $USED"
2065
2066         # check if the vfs_dq_init() is called before writing
2067         echo "Append to the same file..."
2068         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2069                 error "write failed"
2070         cancel_lru_locks osc
2071         sync; sync_all_data || true
2072
2073         echo "Verify space usage is increased"
2074         USED=$(getquota -u $TSTID global curspace)
2075         [ $USED -gt $ORIG_USR_SPACE ] ||
2076                 error "Used space for user $TSTID isn't increased" \
2077                         "orig:$ORIG_USR_SPACE, now:$USED"
2078         USED=$(getquota -g $TSTID global curspace)
2079         [ $USED -gt $ORIG_GRP_SPACE ] ||
2080                 error "Used space for group $TSTID isn't increased" \
2081                         "orig:$ORIG_GRP_SPACE, now:$USED"
2082
2083         cleanup_quota_test
2084 }
2085 run_test 35 "Usage is still accessible across reboot"
2086
2087 # test migrating old amdin quota files (in Linux quota file format v2) into new
2088 # quota global index (in IAM format)
2089 test_36() {
2090         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] && \
2091                 skip "skipping migration test" && return
2092
2093         # get the mdt0 device name
2094         local mdt0_node=$(facet_active_host $SINGLEMDS)
2095         local mdt0_dev=$(mdsdevname ${SINGLEMDS//mds/})
2096
2097         echo "Reformat..."
2098         formatall
2099
2100         echo "Copy admin quota files into MDT0..."
2101         local mntpt=$(facet_mntpt $SINGLEMDS)
2102         local mdt0_fstype=$(facet_fstype $SINGLEMDS)
2103         local opt
2104         if ! do_node $mdt0_node test -b $mdt0_fstype; then
2105                 opt="-o loop"
2106         fi
2107         echo "$mdt0_node, $mdt0_dev, $mntpt, $opt"
2108         do_node $mdt0_node mount -t $mdt0_fstype $opt $mdt0_dev $mntpt
2109         do_node $mdt0_node mkdir $mntpt/OBJECTS
2110         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.usr $mntpt/OBJECTS
2111         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.grp $mntpt/OBJECTS
2112         do_node $mdt0_node umount -f $mntpt
2113
2114         echo "Setup all..."
2115         setupall
2116
2117         echo "Verify global limits..."
2118         local id_cnt
2119         local limit
2120
2121         local proc="qmt.*.md-0x0.glb-usr"
2122         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2123         [ $id_cnt -eq 403 ] || error "Migrate inode user limit failed: $id_cnt"
2124         limit=$(getquota -u 1 global isoftlimit)
2125         [ $limit -eq 1024 ] || error "User inode softlimit: $limit"
2126         limit=$(getquota -u 1 global ihardlimit)
2127         [ $limit -eq 2048 ] || error "User inode hardlimit: $limit"
2128
2129         proc="qmt.*.md-0x0.glb-grp"
2130         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2131         [ $id_cnt -eq 403 ] || error "Migrate inode group limit failed: $id_cnt"
2132         limit=$(getquota -g 1 global isoftlimit)
2133         [ $limit -eq 1024 ] || error "Group inode softlimit: $limit"
2134         limit=$(getquota -g 1 global ihardlimit)
2135         [ $limit -eq 2048 ] || error "Group inode hardlimit: $limit"
2136
2137         proc=" qmt.*.dt-0x0.glb-usr"
2138         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2139         [ $id_cnt -eq 403 ] || error "Migrate block user limit failed: $id_cnt"
2140         limit=$(getquota -u 60001 global bsoftlimit)
2141         [ $limit -eq 10485760 ] || error "User block softlimit: $limit"
2142         limit=$(getquota -u 60001 global bhardlimit)
2143         [ $limit -eq 20971520 ] || error "User block hardlimit: $limit"
2144
2145         proc="qmt.*.dt-0x0.glb-grp"
2146         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2147         [ $id_cnt -eq 403 ] || error "Migrate block user limit failed: $id_cnt"
2148         limit=$(getquota -g 60001 global bsoftlimit)
2149         [ $limit -eq 10485760 ] || error "Group block softlimit: $limit"
2150         limit=$(getquota -g 60001 global bhardlimit)
2151         [ $limit -eq 20971520 ] || error "Group block hardlimit: $limit"
2152
2153         echo "Cleanup..."
2154         formatall
2155         setupall
2156 }
2157 run_test 36 "Migrate old admin files into new global indexes"
2158
2159 quota_fini()
2160 {
2161         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2162 }
2163 quota_fini
2164
2165 cd $ORIG_PWD
2166 complete $SECONDS
2167 check_and_cleanup_lustre
2168 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2169 exit_status