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