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