Whamcloud - gitweb
LU-11768 test: make at_max to take effect
[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 # Bug number for skipped test:      LU-5152
14 ALWAYS_EXCEPT="$SANITY_QUOTA_EXCEPT 55"
15 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
16
17 [ "$ALWAYS_EXCEPT$EXCEPT" ] &&
18         echo "Skipping tests: $ALWAYS_EXCEPT $EXCEPT"
19
20 TMP=${TMP:-/tmp}
21
22 ORIG_PWD=${PWD}
23 TSTID=${TSTID:-60000}
24 TSTID2=${TSTID2:-60001}
25 TSTUSR=${TSTUSR:-"quota_usr"}
26 TSTUSR2=${TSTUSR2:-"quota_2usr"}
27 TSTPRJID=${TSTPRJID:-1000}
28 BLK_SZ=1024
29 MAX_DQ_TIME=604800
30 MAX_IQ_TIME=604800
31 QTYPE="ugp"
32
33 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
34 . $LUSTRE/tests/test-framework.sh
35 init_test_env $@
36 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
37 get_lustre_env
38 init_logging
39 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
40
41 require_dsh_mds || exit 0
42 require_dsh_ost || exit 0
43
44 # Does e2fsprogs support quota feature?
45 if [ $(facet_fstype $SINGLEMDS) == ldiskfs ] &&
46         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
47                 grep -q 'quota'"; then
48         skip_env "e2fsprogs doesn't support quota" && exit 0
49 fi
50
51 # Test duration:                   30 min
52 [ "$SLOW" = "no" ] && EXCEPT_SLOW="61"
53
54 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
55         # bug number for skipped test: LU-6836
56         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  4a"
57
58         # bug number:                        LU-2887
59         # Test duration:                     21      9 min"
60         [ "$SLOW" = "no" ] && EXCEPT_SLOW+=" 12a     9"
61 fi
62
63 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
64
65 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
66
67 DIR=${DIR:-$MOUNT}
68 DIR2=${DIR2:-$MOUNT2}
69
70 QUOTA_AUTO_OLD=$QUOTA_AUTO
71 export QUOTA_AUTO=0
72
73 check_and_setup_lustre
74
75 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
76 is_project_quota_supported() {
77         $ENABLE_PROJECT_QUOTAS || return 1
78         [ "$(facet_fstype $SINGLEMDS)" == "ldiskfs" ] &&
79                 [ $(lustre_version_code $SINGLEMDS) -gt \
80                 $(version_code 2.9.55) ] &&
81                 lfs --help | grep project >&/dev/null &&
82                 egrep -q "7." /etc/redhat-release && return 0
83
84         if [ "$(facet_fstype $SINGLEMDS)" == "zfs" ]; then
85                 [ $(lustre_version_code $SINGLEMDS) -le \
86                         $(version_code 2.10.53) ] && return 1
87
88                 do_facet mds1 $ZPOOL upgrade -v |
89                         grep project_quota && return 0
90         fi
91
92         return 1
93 }
94
95 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
96 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
97 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
98 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
99 SHOW_QUOTA_PROJID="eval is_project_quota_supported && $LFS quota -v -p $TSTPRJID $DIR"
100 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
101 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
102 SHOW_QUOTA_INFO_PROJID="eval is_project_quota_supported && $LFS quota -t -p $DIR"
103
104 build_test_filter
105
106 lustre_fail() {
107         local fail_node=$1
108         local fail_loc=$2
109         local fail_val=${3:-0}
110         local NODES=
111
112         case $fail_node in
113         mds_ost|mdt_ost) NODES="$(comma_list $(mdts_nodes) $(osts_nodes))";;
114         mds|mdt) NODES="$(comma_list $(mdts_nodes))";;
115         ost) NODES="$(comma_list $(osts_nodes))";;
116         esac
117
118         do_nodes $NODES "lctl set_param fail_val=$fail_val fail_loc=$fail_loc"
119 }
120
121 change_project()
122 {
123         echo "lfs project $*"
124         lfs project $* || error "lfs project $* failed"
125 }
126
127 RUNAS="runas -u $TSTID -g $TSTID"
128 RUNAS2="runas -u $TSTID2 -g $TSTID2"
129 DD="dd if=/dev/zero bs=1M"
130
131 FAIL_ON_ERROR=false
132
133 # clear quota limits for a user or a group
134 # usage: resetquota -u username
135 #        resetquota -g groupname
136 #        resetquota -p projid
137
138 resetquota() {
139         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
140         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
141                 error "resetquota: wrong specifier $1 passed"
142
143         if [ $1 == "-p" ]; then
144                 is_project_quota_supported || return 0
145         fi
146
147         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
148                 error "clear quota for [type:$1 name:$2] failed"
149         # give a chance to slave to release space
150         sleep 1
151 }
152
153 quota_scan() {
154         local local_ugp=$1
155         local local_id=$2
156
157         if [ "$local_ugp" == "a" -o "$local_ugp" == "u" ]; then
158                 $LFS quota -v -u $local_id $DIR
159                 log "Files for user ($local_id):"
160                 ($LFS find --user $local_id $DIR | head -n 4 |
161                         xargs stat 2>/dev/null)
162         fi
163
164         if [ "$local_ugp" == "a" -o "$local_ugp" == "g" ]; then
165                 $LFS quota -v -g $local_id $DIR
166                 log "Files for group ($local_id):"
167                 ($LFS find --group $local_id $DIR | head -n 4 |
168                         xargs stat 2>/dev/null)
169         fi
170
171         if [ "$local_ugp" == "a" -o "$local_ugp" == "p" ]; then
172                 $LFS quota -v -p $TSTPRJID $DIR
173                 log "Files for project ($TSTPRJID):"
174                 ($LFS find --projid $TSTPRJID $DIR | head -n 4 |
175                         xargs stat 2>/dev/null)
176         fi
177 }
178
179 quota_error() {
180         quota_scan $1 $2
181         shift 2
182         error "$*"
183 }
184
185 quota_log() {
186         quota_scan $1 $2
187         shift 2
188         log "$*"
189 }
190
191 # get quota for a user or a group
192 # usage: getquota -u|-g|-p <username>|<groupname>|<projid> global|<obd_uuid> \
193 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
194 getquota() {
195         local spec
196         local uuid
197
198         sync_all_data > /dev/null 2>&1 || true
199
200         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
201         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
202                 error "getquota: wrong u/g/p specifier $1 passed"
203
204         uuid="$3"
205
206         case "$4" in
207                 curspace)   spec=1;;
208                 bsoftlimit) spec=2;;
209                 bhardlimit) spec=3;;
210                 bgrace)     spec=4;;
211                 curinodes)  spec=5;;
212                 isoftlimit) spec=6;;
213                 ihardlimit) spec=7;;
214                 igrace)     spec=8;;
215                 *)          error "unknown quota parameter $4";;
216         esac
217
218         [ "$uuid" = "global" ] && uuid=$DIR
219
220         $LFS quota -v "$1" "$2" $DIR |
221                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
222                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
223                 | tr -d "*"
224 }
225
226 # set mdt quota type
227 # usage: set_mdt_qtype ugp|u|g|p|none
228 set_mdt_qtype() {
229         local qtype=$1
230         local varsvc
231         local mdts=$(get_facets MDS)
232         local cmd
233         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
234                 qtype=$(tr -d 'p' <<<$qtype)
235
236         if [[ $PERM_CMD = *"set_param -P"* ]]; then
237                 do_facet mgs $PERM_CMD \
238                         osd-*.$FSNAME-MDT*.quota_slave.enable=$qtype
239         else
240                 do_facet mgs $PERM_CMD $FSNAME.quota.mdt=$qtype
241         fi
242         # we have to make sure each MDT received config changes
243         for mdt in ${mdts//,/ }; do
244                 varsvc=${mdt}_svc
245                 cmd="$LCTL get_param -n "
246                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
247                 cmd=${cmd}.quota_slave.enabled
248
249                 if $(facet_up $mdt); then
250                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
251                 fi
252         done
253         return 0
254 }
255
256 # set ost quota type
257 # usage: set_ost_qtype ugp|u|g|p|none
258 set_ost_qtype() {
259         local qtype=$1
260         local varsvc
261         local osts=$(get_facets OST)
262         local cmd
263         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
264                 qtype=$(tr -d 'p' <<<$qtype)
265
266         if [[ $PERM_CMD = *"set_param -P"* ]]; then
267                 do_facet mgs $PERM_CMD \
268                         osd-*.$FSNAME-OST*.quota_slave.enable=$qtype
269         else
270                 do_facet mgs $PERM_CMD $FSNAME.quota.ost=$qtype
271         fi
272         # we have to make sure each OST received config changes
273         for ost in ${osts//,/ }; do
274                 varsvc=${ost}_svc
275                 cmd="$LCTL get_param -n "
276                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
277                 cmd=${cmd}.quota_slave.enabled
278
279                 if $(facet_up $ost); then
280                         wait_update_facet $ost "$cmd" "$qtype" || return 1
281                 fi
282         done
283         return 0
284 }
285
286 wait_reintegration() {
287         local ntype=$1
288         local qtype=$2
289         local max=$3
290         local result="glb[1],slv[1],reint[0]"
291         local varsvc
292         local cmd
293         local tgts
294
295         if [ $ntype == "mdt" ]; then
296                 tgts=$(get_facets MDS)
297         else
298                 tgts=$(get_facets OST)
299         fi
300
301         for tgt in ${tgts//,/ }; do
302                 varsvc=${tgt}_svc
303                 cmd="$LCTL get_param -n "
304                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
305                 cmd=${cmd}.quota_slave.info
306
307                 if $(facet_up $tgt); then
308                         wait_update_facet $tgt "$cmd |
309                                 grep "$qtype" | awk '{ print \\\$3 }'" \
310                                         "$result" $max || return 1
311                 fi
312         done
313         return 0
314 }
315
316 wait_mdt_reint() {
317         local qtype=$1
318         local max=${2:-90}
319
320         if [[ "$qtype" =~ "u" ]]; then
321                 wait_reintegration "mdt" "user" $max || return 1
322         fi
323
324         if [[ "$qtype" =~ "g" ]]; then
325                 wait_reintegration "mdt" "group" $max || return 1
326         fi
327
328         if [[ "$qtype" =~ "p" ]]; then
329                 ! is_project_quota_supported && return 0
330                 wait_reintegration "mdt" "project" $max || return 1
331         fi
332         return 0
333 }
334
335 wait_ost_reint() {
336         local qtype=$1
337         local max=${2:-90}
338
339         if [[ "$qtype" =~ "u" ]]; then
340                 wait_reintegration "ost" "user" $max || return 1
341         fi
342
343         if [[ "$qtype" =~ "g" ]]; then
344                 wait_reintegration "ost" "group" $max || return 1
345         fi
346
347         if [[ "$qtype" =~ "p" ]]; then
348                 ! is_project_quota_supported && return 0
349                 wait_reintegration "ost" "project" $max || return 1
350         fi
351         return 0
352 }
353
354 disable_project_quota() {
355         is_project_quota_supported || return 0
356         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] && return 0
357         stopall || error "failed to stopall (1)"
358
359         for num in $(seq $MDSCOUNT); do
360                 do_facet mds$num $TUNE2FS -Q ^prj $(mdsdevname $num) ||
361                         error "tune2fs $(mdsdevname $num) failed"
362         done
363
364         for num in $(seq $OSTCOUNT); do
365                 do_facet ost$num $TUNE2FS -Q ^prj $(ostdevname $num) ||
366                         error "tune2fs $(ostdevname $num) failed"
367         done
368
369         mount
370         setupall
371 }
372
373 wait_grace_time() {
374         local qtype=$1
375         local flavour=$2
376         local extrasleep=${3:-5}
377         local qarg
378
379         case $qtype in
380                 u|g) qarg=$TSTUSR ;;
381                 p) qarg=$TSTPRJID ;;
382                 *) error "get_grace_time: Invalid quota type: $qtype"
383         esac
384
385         case $flavour in
386                 block)
387                         time=$(lfs quota -$qtype $qarg $DIR|
388                                    awk 'NR == 3{ print $5 }'| sed 's/s$//')
389                         ;;
390                 file)
391                         time=$(lfs quota -$qtype $qarg $DIR|
392                                    awk 'NR == 3{ print $9 }'| sed 's/s$//')
393                         ;;
394                 *)
395                         error "Unknown quota type: $flavour"
396                         ;;
397         esac
398
399         echo "Sleep through grace ..."
400         [ "$time" == "-" ] &&
401             error "Grace timeout was not set or quota not exceeded"
402         if [ "$time" == "none" ]; then
403             echo "...Grace timeout already expired"
404         else
405                 let time+=$extrasleep
406                 echo "...sleep $time seconds"
407                 sleep $time
408         fi
409 }
410
411 setup_quota_test() {
412         wait_delete_completed
413         echo "Creating test directory"
414         mkdir $DIR/$tdir || return 1
415         chmod 0777 $DIR/$tdir || return 2
416         # always clear fail_loc in case of fail_loc isn't cleared
417         # properly when previous test failed
418         lustre_fail mds_ost 0
419 }
420
421 cleanup_quota_test() {
422         trap 0
423         echo "Delete files..."
424         rm -rf $DIR/$tdir
425         echo "Wait for unlink objects finished..."
426         wait_delete_completed
427         sync_all_data || true
428 }
429
430 quota_show_check() {
431         local bf=$1
432         local ugp=$2
433         local qid=$3
434         local usage
435
436         $LFS quota -v -$ugp $qid $DIR
437
438         if [ "$bf" == "a" -o "$bf" == "b" ]; then
439                 usage=$(getquota -$ugp $qid global curspace)
440                 if [ -z $usage ]; then
441                         quota_error $ugp $qid \
442                                 "Query block quota failed ($ugp:$qid)."
443                 else
444                         [ $usage -ne 0 ] && quota_log $ugp $qid \
445                                 "Block quota isn't 0 ($ugp:$qid:$usage)."
446                 fi
447         fi
448
449         if [ "$bf" == "a" -o "$bf" == "f" ]; then
450                 usage=$(getquota -$ugp $qid global curinodes)
451                 if [ -z $usage ]; then
452                         quota_error $ugp $qid \
453                                 "Query file quota failed ($ugp:$qid)."
454                 else
455                         [ $usage -ne 0 ] && quota_log $ugp $qid \
456                                 "File quota isn't 0 ($ugp:$qid:$usage)."
457                 fi
458         fi
459 }
460
461 enable_project_quota() {
462         is_project_quota_supported || return 0
463         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] && return 0
464         stopall || error "failed to stopall (1)"
465
466         for num in $(seq $MDSCOUNT); do
467                 do_facet mds$num $TUNE2FS -O project $(mdsdevname $num) ||
468                         error "tune2fs $(mdsdevname $num) failed"
469         done
470
471         for num in $(seq $OSTCOUNT); do
472                 do_facet ost$num $TUNE2FS -O project $(ostdevname $num) ||
473                         error "tune2fs $(ostdevname $num) failed"
474         done
475
476         mount
477         setupall
478 }
479 enable_project_quota
480
481 reset_quota_settings() {
482         resetquota -u $TSTUSR
483         resetquota -g $TSTUSR
484         resetquota -u $TSTUSR2
485         resetquota -g $TSTUSR2
486         resetquota -p $TSTPRJID
487 }
488
489 # enable quota debug
490 quota_init() {
491         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
492 }
493 quota_init
494 reset_quota_settings
495
496 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
497         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
498 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
499         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
500
501 test_quota_performance() {
502         local TESTFILE="$DIR/$tdir/$tfile-0"
503         local size=$1 # in MB
504         local stime=$(date +%s)
505         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
506                 quota_error u $TSTUSR "write failure"
507         local etime=$(date +%s)
508         delta=$((etime - stime))
509         if [ $delta -gt 0 ]; then
510             rate=$((size * 1024 / delta))
511             if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
512                 # LU-2872 - see LU-2887 for fix
513                 [ $rate -gt 64 ] ||
514                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
515             else
516                 [ $rate -gt 1024 ] ||
517                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
518             fi
519         fi
520         rm -f $TESTFILE
521 }
522
523 # test basic quota performance b=21696
524 test_0() {
525         local MB=100 # 100M
526         [ "$SLOW" = "no" ] && MB=10
527
528         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
529         [ $free_space -le $((MB * 1024)) ] &&
530                 skip "not enough space ${free_space} KB, " \
531                         "required $((MB * 1024)) KB"
532         setup_quota_test || error "setup quota failed with $?"
533         trap cleanup_quota_test EXIT
534
535         set_ost_qtype "none" || error "disable ost quota failed"
536         test_quota_performance $MB
537
538         set_ost_qtype $QTYPE || error "enable ost quota failed"
539         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
540                 error "set quota failed"
541         test_quota_performance $MB
542
543         cleanup_quota_test
544         resetquota -u $TSTUSR
545 }
546 run_test 0 "Test basic quota performance"
547
548 # test block hardlimit
549 test_1() {
550         local LIMIT=10  # 10M
551         local TESTFILE="$DIR/$tdir/$tfile-0"
552
553         setup_quota_test || error "setup quota failed with $?"
554         trap cleanup_quota_test EXIT
555
556         # enable ost quota
557         set_ost_qtype $QTYPE || error "enable ost quota failed"
558
559         # test for user
560         log "User quota (block hardlimit:$LIMIT MB)"
561         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
562                 error "set user quota failed"
563
564         # make sure the system is clean
565         local USED=$(getquota -u $TSTUSR global curspace)
566         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
567
568         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
569         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
570
571         log "Write..."
572         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
573                 quota_error u $TSTUSR "user write failure, but expect success"
574         log "Write out of block quota ..."
575         # this time maybe cache write,  ignore it's failure
576         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
577         # flush cache, ensure noquota flag is set on client
578         cancel_lru_locks osc
579         sync; sync_all_data || true
580         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
581                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
582
583         rm -f $TESTFILE
584         wait_delete_completed || error "wait_delete_completed failed"
585         sync_all_data || true
586         USED=$(getquota -u $TSTUSR global curspace)
587         [ $USED -ne 0 ] && quota_error u $TSTUSR \
588                 "user quota isn't released after deletion"
589         resetquota -u $TSTUSR
590
591         # test for group
592         log "--------------------------------------"
593         log "Group quota (block hardlimit:$LIMIT MB)"
594         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
595                 error "set group quota failed"
596
597         TESTFILE="$DIR/$tdir/$tfile-1"
598         # make sure the system is clean
599         USED=$(getquota -g $TSTUSR global curspace)
600         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTUSR isn't 0"
601
602         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
603         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
604
605         log "Write ..."
606         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
607                 quota_error g $TSTUSR "Group write failure, but expect success"
608         log "Write out of block quota ..."
609         # this time maybe cache write, ignore it's failure
610         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
611         cancel_lru_locks osc
612         sync; sync_all_data || true
613         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
614                 quota_error g $TSTUSR "Group write success, but expect EDQUOT"
615         rm -f $TESTFILE
616         wait_delete_completed || error "wait_delete_completed failed"
617         sync_all_data || true
618         USED=$(getquota -g $TSTUSR global curspace)
619         [ $USED -ne 0 ] && quota_error g $TSTUSR \
620                                 "Group quota isn't released after deletion"
621         resetquota -g $TSTUSR
622
623         if ! is_project_quota_supported; then
624                 echo "Project quota is not supported"
625                 cleanup_quota_test
626                 return 0
627         fi
628
629         TESTFILE="$DIR/$tdir/$tfile-2"
630         # make sure the system is clean
631         USED=$(getquota -p $TSTPRJID global curspace)
632         [ $USED -ne 0 ] &&
633                 error "used space($USED) for project $TSTPRJID isn't 0"
634
635         # test for Project
636         log "--------------------------------------"
637         log "Project quota (block hardlimit:$LIMIT mb)"
638         $LFS setquota -p $TSTPRJID -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
639                 error "set project quota failed"
640
641         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
642         chown $TSTUSR:$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
643         change_project -p $TSTPRJID $TESTFILE
644
645         log "write ..."
646         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) || quota_error p $TSTPRJID \
647                 "project write failure, but expect success"
648         log "write out of block quota ..."
649         # this time maybe cache write, ignore it's failure
650         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
651         cancel_lru_locks osc
652         sync; sync_all_data || true
653         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT && quota_error p \
654                 $TSTPRJID "project write success, but expect EDQUOT"
655
656         # cleanup
657         cleanup_quota_test
658
659         USED=$(getquota -p $TSTPRJID global curspace)
660         [ $USED -ne 0 ] && quota_error p $TSTPRJID \
661                 "project quota isn't released after deletion"
662
663         resetquota -p $TSTPRJID
664 }
665 run_test 1 "Block hard limit (normal use and out of quota)"
666
667 # test inode hardlimit
668 test_2() {
669         local TESTFILE="$DIR/$tdir/$tfile-0"
670         local LIMIT=$(do_facet mds1 $LCTL get_param -n \
671                 qmt.$FSNAME-QMT0000.md-0x0.info |
672                 awk '/least qunit/{ print $3 }')
673         local L2=$(do_facet mds1 $LCTL get_param -n \
674                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
675
676         [ $L2 -le $LIMIT ] || LIMIT=$L2
677
678         [ "$SLOW" = "no" ] || LIMIT=$((LIMIT * 1024))
679
680         local FREE_INODES=$(mdt_free_inodes 0)
681         echo "$FREE_INODES free inodes on master MDT"
682         [ $FREE_INODES -lt $LIMIT ] &&
683                 skip "not enough free inodes $FREE_INODES required $LIMIT"
684
685         setup_quota_test || error "setup quota failed with $?"
686         trap cleanup_quota_test EXIT
687
688         # enable mdt quota
689         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
690
691         # test for user
692         log "User quota (inode hardlimit:$LIMIT files)"
693         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
694                 error "set user quota failed"
695
696         # make sure the system is clean
697         local USED=$(getquota -u $TSTUSR global curinodes)
698         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
699
700         log "Create $LIMIT files ..."
701         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
702                 quota_error u $TSTUSR "user create failure, but expect success"
703         log "Create out of file quota ..."
704         $RUNAS touch ${TESTFILE}_xxx &&
705                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
706
707         # cleanup
708         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
709         rm -f ${TESTFILE}_xxx
710         wait_delete_completed
711
712         USED=$(getquota -u $TSTUSR global curinodes)
713         [ $USED -ne 0 ] && quota_error u $TSTUSR \
714                 "user quota isn't released after deletion"
715         resetquota -u $TSTUSR
716
717         # test for group
718         log "--------------------------------------"
719         log "Group quota (inode hardlimit:$LIMIT files)"
720         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
721                 error "set group quota failed"
722
723         TESTFILE=$DIR/$tdir/$tfile-1
724         # make sure the system is clean
725         USED=$(getquota -g $TSTUSR global curinodes)
726         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
727
728         log "Create $LIMIT files ..."
729         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
730                 quota_error g $TSTUSR "group create failure, but expect success"
731         log "Create out of file quota ..."
732         $RUNAS touch ${TESTFILE}_xxx &&
733                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
734
735         # cleanup
736         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
737         rm -f ${TESTFILE}_xxx
738         wait_delete_completed
739
740         USED=$(getquota -g $TSTUSR global curinodes)
741         [ $USED -ne 0 ] && quota_error g $TSTUSR \
742                 "user quota isn't released after deletion"
743
744         resetquota -g $TSTUSR
745         ! is_project_quota_supported && cleanup_quota_test &&
746                 echo "Skip project quota is not supported" && return 0
747
748         # test for project
749         log "--------------------------------------"
750         log "Project quota (inode hardlimit:$LIMIT files)"
751         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
752                 error "set project quota failed"
753
754         TESTFILE=$DIR/$tdir/$tfile-1
755         # make sure the system is clean
756         USED=$(getquota -p $TSTPRJID global curinodes)
757         [ $USED -ne 0 ] &&
758                 error "Used inodes($USED) for project $TSTPRJID isn't 0"
759
760         change_project -sp $TSTPRJID $DIR/$tdir
761         log "Create $LIMIT files ..."
762         $RUNAS createmany -m ${TESTFILE} $((LIMIT-1)) || quota_error p \
763                 $TSTPRJID "project create fail, but expect success"
764         log "Create out of file quota ..."
765         $RUNAS touch ${TESTFILE}_xxx && quota_error p $TSTPRJID \
766                 "project create success, but expect EDQUOT"
767         change_project -C $DIR/$tdir
768
769         cleanup_quota_test
770         USED=$(getquota -p $TSTPRJID global curinodes)
771         [ $USED -ne 0 ] && quota_error p $TSTPRJID \
772                 "project quota isn't released after deletion"
773
774         resetquota -p $TSTPRJID
775
776 }
777 run_test 2 "File hard limit (normal use and out of quota)"
778
779 test_block_soft() {
780         local TESTFILE=$1
781         local GRACE=$2
782         local LIMIT=$3
783         local OFFSET=0
784         local qtype=$4
785
786         setup_quota_test
787         trap cleanup_quota_test EXIT
788
789         $SETSTRIPE $TESTFILE -c 1 -i 0
790         chown $TSTUSR.$TSTUSR $TESTFILE
791         [ "$qtype" == "p" ] && is_project_quota_supported &&
792                 change_project -p $TSTPRJID $TESTFILE
793
794         echo "Write up to soft limit"
795         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
796                 quota_error a $TSTUSR "write failure, but expect success"
797         OFFSET=$((LIMIT * 1024))
798         cancel_lru_locks osc
799
800         echo "Write to exceed soft limit"
801         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
802                 quota_error a $TSTUSR "write failure, but expect success"
803         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
804         cancel_lru_locks osc
805
806         echo "mmap write when over soft limit"
807         $RUNAS $MULTIOP $TESTFILE.mmap OT40960SMW ||
808                 quota_error a $TSTUSR "mmap write failure, but expect success"
809         cancel_lru_locks osc
810
811         $SHOW_QUOTA_USER
812         $SHOW_QUOTA_GROUP
813         $SHOW_QUOTA_PROJID
814         $SHOW_QUOTA_INFO_USER
815         $SHOW_QUOTA_INFO_GROUP
816         $SHOW_QUOTA_INFO_PROJID
817
818         echo "Write before timer goes off"
819         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
820                 quota_error a $TSTUSR "write failure, but expect success"
821         OFFSET=$((OFFSET + 1024))
822         cancel_lru_locks osc
823
824         wait_grace_time $qtype "block"
825
826         $SHOW_QUOTA_USER
827         $SHOW_QUOTA_GROUP
828         $SHOW_QUOTA_PROJID
829         $SHOW_QUOTA_INFO_USER
830         $SHOW_QUOTA_INFO_GROUP
831         $SHOW_QUOTA_INFO_PROJID
832
833         echo "Write after timer goes off"
834         # maybe cache write, ignore.
835         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
836         OFFSET=$((OFFSET + 1024))
837         cancel_lru_locks osc
838         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
839                 quota_error a $TSTUSR "write success, but expect EDQUOT"
840
841         $SHOW_QUOTA_USER
842         $SHOW_QUOTA_GROUP
843         $SHOW_QUOTA_PROJID
844         $SHOW_QUOTA_INFO_USER
845         $SHOW_QUOTA_INFO_GROUP
846         $SHOW_QUOTA_INFO_PROJID
847
848         echo "Unlink file to stop timer"
849         rm -f $TESTFILE
850         wait_delete_completed
851         sync_all_data || true
852
853         $SHOW_QUOTA_USER
854         $SHOW_QUOTA_GROUP
855         $SHOW_QUOTA_PROJID
856         $SHOW_QUOTA_INFO_USER
857         $SHOW_QUOTA_INFO_GROUP
858         $SHOW_QUOTA_INFO_PROJID
859
860         $SETSTRIPE $TESTFILE -c 1 -i 0
861         chown $TSTUSR.$TSTUSR $TESTFILE
862         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $TESTFILE
863
864         echo "Write ..."
865         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
866                 quota_error a $TSTUSR "write failure, but expect success"
867         # cleanup
868         cleanup_quota_test
869 }
870
871 # block soft limit
872 test_3() {
873         local GRACE=20 # 20s
874         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
875             GRACE=60
876         fi
877         local TESTFILE=$DIR/$tdir/$tfile-0
878
879         # get minimum soft qunit size
880         local LIMIT=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
881                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
882
883         set_ost_qtype $QTYPE || error "enable ost quota failed"
884
885         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
886         # make sure the system is clean
887         local USED=$(getquota -u $TSTUSR global curspace)
888         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
889
890         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
891                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
892         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
893                 error "set user quota failed"
894
895         test_block_soft $TESTFILE $GRACE $LIMIT "u"
896         resetquota -u $TSTUSR
897
898         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
899         TESTFILE=$DIR/$tdir/$tfile-1
900         # make sure the system is clean
901         USED=$(getquota -g $TSTUSR global curspace)
902         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
903
904         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
905                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
906         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
907                 error "set group quota failed"
908
909         test_block_soft $TESTFILE $GRACE $LIMIT "g"
910         resetquota -g $TSTUSR
911
912         if is_project_quota_supported; then
913                 echo "Project quota (soft limit:$LIMIT MB  grace:$GRACE sec)"
914                 TESTFILE=$DIR/$tdir/$tfile-2
915                 # make sure the system is clean
916                 USED=$(getquota -p $TSTPRJID global curspace)
917                 [ $USED -ne 0 ] && error \
918                         "Used space($USED) for project $TSTPRJID isn't 0."
919
920                 $LFS setquota -t -p --block-grace $GRACE --inode-grace \
921                         $MAX_IQ_TIME $DIR ||
922                                 error "set project grace time failed"
923                 $LFS setquota -p $TSTPRJID -b ${LIMIT}M -B 0 -i 0 -I 0 \
924                         $DIR || error "set project quota failed"
925
926                 test_block_soft $TESTFILE $GRACE $LIMIT "p"
927                 resetquota -p $TSTPRJID
928                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
929                         $MAX_IQ_TIME $DIR ||
930                                 error "restore project grace time failed"
931         fi
932
933         # cleanup
934         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
935                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
936         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
937                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
938 }
939 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
940
941 test_file_soft() {
942         local TESTFILE=$1
943         local LIMIT=$2
944         local grace=$3
945         local qtype=$4
946
947         setup_quota_test
948         trap cleanup_quota_test EXIT
949         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
950
951         echo "Create files to exceed soft limit"
952         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
953                 quota_error a $TSTUSR "create failure, but expect success"
954         local trigger_time=$(date +%s)
955
956         sync_all_data || true
957
958         local cur_time=$(date +%s)
959         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
960                 error "Passed grace time $grace, $trigger_time, $cur_time"
961
962         echo "Create file before timer goes off"
963         $RUNAS touch ${TESTFILE}_before ||
964                 quota_error a $TSTUSR "failed create before timer expired," \
965                         "but expect success. $trigger_time, $cur_time"
966         sync_all_data || true
967
968         wait_grace_time $qtype "file"
969
970         $SHOW_QUOTA_USER
971         $SHOW_QUOTA_GROUP
972         $SHOW_QUOTA_PROJID
973         $SHOW_QUOTA_INFO_USER
974         $SHOW_QUOTA_INFO_GROUP
975         $SHOW_QUOTA_INFO_PROJID
976
977         echo "Create file after timer goes off"
978         # There is a window that space is accounted in the quota usage but
979         # hasn't been decreased from the pending write, if we acquire quota
980         # in this window, we'll acquire more than we needed.
981         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
982         sync_all_data || true
983         $RUNAS touch ${TESTFILE}_after_3 &&
984                 quota_error a $TSTUSR "create after timer expired," \
985                         "but expect EDQUOT"
986         sync_all_data || true
987
988         $SHOW_QUOTA_USER
989         $SHOW_QUOTA_GROUP
990         $SHOW_QUOTA_PROJID
991         $SHOW_QUOTA_INFO_USER
992         $SHOW_QUOTA_INFO_GROUP
993         $SHOW_QUOTA_INFO_PROJID
994
995         echo "Unlink files to stop timer"
996         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
997         wait_delete_completed
998
999         echo "Create file"
1000         $RUNAS touch ${TESTFILE}_xxx ||
1001                 quota_error a $TSTUSR "touch after timer stop failure," \
1002                         "but expect success"
1003         sync_all_data || true
1004
1005         # cleanup
1006         cleanup_quota_test
1007 }
1008
1009 # file soft limit
1010 test_4a() {
1011         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1012                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1013         local TESTFILE=$DIR/$tdir/$tfile-0
1014         local GRACE=12
1015
1016         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1017
1018         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1019         # make sure the system is clean
1020         local USED=$(getquota -u $TSTUSR global curinodes)
1021         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1022
1023         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1024                 $GRACE $DIR || error "set user grace time failed"
1025         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1026                 error "set user quota failed"
1027
1028         test_file_soft $TESTFILE $LIMIT $GRACE "u"
1029         resetquota -u $TSTUSR
1030
1031         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1032         # make sure the system is clean
1033         USED=$(getquota -g $TSTUSR global curinodes)
1034         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
1035
1036         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1037                 $GRACE $DIR || error "set group grace time failed"
1038         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1039                 error "set group quota failed"
1040         TESTFILE=$DIR/$tdir/$tfile-1
1041
1042         test_file_soft $TESTFILE $LIMIT $GRACE "g"
1043         resetquota -g $TSTUSR
1044
1045         if is_project_quota_supported; then
1046                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
1047                 # make sure the system is clean
1048                 USED=$(getquota -p $TSTPRJID global curinodes)
1049                 [ $USED -ne 0 ] && error \
1050                         "Used space($USED) for project $TSTPRJID isn't 0."
1051
1052                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1053                         $GRACE $DIR || error "set project grace time failed"
1054                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1055                         error "set project quota failed"
1056
1057                 TESTFILE=$DIR/$tdir/$tfile-1
1058                 # one less than limit, because of parent directory included.
1059                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
1060                 resetquota -p $TSTPRJID
1061                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1062                         $MAX_IQ_TIME $DIR ||
1063                                 error "restore project grace time failed"
1064         fi
1065
1066         # cleanup
1067         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1068                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1069         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1070                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1071 }
1072 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1073
1074 test_4b() {
1075         local GR_STR1="1w3d"
1076         local GR_STR2="1000s"
1077         local GR_STR3="5s"
1078         local GR_STR4="1w2d3h4m5s"
1079         local GR_STR5="5c"
1080         local GR_STR6="18446744073709551615"
1081         local GR_STR7="-1"
1082
1083         wait_delete_completed
1084
1085         # test of valid grace strings handling
1086         echo "Valid grace strings test"
1087         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1088                 $GR_STR2 $DIR || error "set user grace time failed"
1089         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1090         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1091                 $GR_STR4 $DIR || error "set group grace time quota failed"
1092         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1093
1094         # test of invalid grace strings handling
1095         echo "  Invalid grace strings test"
1096         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1097         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1098         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1099                 $GR_STR7 $DIR
1100
1101         # cleanup
1102         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1103                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1104         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1105                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1106 }
1107 run_test 4b "Grace time strings handling"
1108
1109 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1110 test_5() {
1111         local BLIMIT=10 # 10M
1112         local ILIMIT=10 # 10 inodes
1113
1114         setup_quota_test || error "setup quota failed with $?"
1115         trap cleanup_quota_test EXIT
1116
1117         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1118         set_ost_qtype $QTYPE || error "enable ost quota failed"
1119
1120         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1121         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1122                 error "set user quota failed"
1123         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1124         if is_project_quota_supported; then
1125                 error "set group quota failed"
1126                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1127                         -I $ILIMIT $DIR || error "set project quota failed"
1128         fi
1129
1130         # make sure the system is clean
1131         local USED=$(getquota -u $TSTUSR global curinodes)
1132         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1133         USED=$(getquota -g $TSTUSR global curinodes)
1134         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1135         USED=$(getquota -u $TSTUSR global curspace)
1136         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1137         USED=$(getquota -g $TSTUSR global curspace)
1138         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1139         if is_project_quota_supported; then
1140                 USED=$(getquota -p $TSTPRJID global curinodes)
1141                 [ $USED -ne 0 ] &&
1142                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1143                 USED=$(getquota -p $TSTPRJID global curspace)
1144                 [ $USED -ne 0 ] &&
1145                         error "Used block($USED) for project $TSTPRJID isn't 0."
1146         fi
1147
1148         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1149         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1150                 error "create failure, expect success"
1151         if is_project_quota_supported; then
1152                 touch $DIR/$tdir/$tfile-0_1
1153                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1154         fi
1155         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1156                 error "write failure, expect success"
1157
1158         echo "Chown files to $TSTUSR.$TSTUSR ..."
1159         for i in $(seq 0 $ILIMIT); do
1160                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1161                         quota_error a $TSTUSR "chown failure, expect success"
1162         done
1163
1164         # cleanup
1165         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1166                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1167         cleanup_quota_test
1168
1169         resetquota -u $TSTUSR
1170         resetquota -g $TSTUSR
1171         resetquota -p $TSTPRJID
1172 }
1173 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1174
1175 # test dropping acquire request on master
1176 test_6() {
1177         local LIMIT=3 # 3M
1178
1179         # Clear dmesg so watchdog is not triggered by previous
1180         # test output
1181         do_facet ost1 dmesg -c > /dev/null
1182
1183         setup_quota_test || error "setup quota failed with $?"
1184         trap cleanup_quota_test EXIT
1185
1186         # make sure the system is clean
1187         local USED=$(getquota -u $TSTUSR global curspace)
1188         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1189
1190         # make sure no granted quota on ost
1191         set_ost_qtype $QTYPE || error "enable ost quota failed"
1192         resetquota -u $TSTUSR
1193
1194         # create file for $TSTUSR
1195         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1196         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1197         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1198
1199         # create file for $TSTUSR2
1200         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1201         $SETSTRIPE $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1202         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1203
1204         # cache per-ID lock for $TSTUSR on slave
1205         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1206                 error "set quota failed"
1207         $RUNAS $DD of=$TESTFILE count=1 ||
1208                 error "write $TESTFILE failure, expect success"
1209         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1210                 error "write $TESTFILE2 failure, expect success"
1211
1212         if at_is_enabled; then
1213                 at_max_saved=$(at_max_get ost1)
1214                 at_max_set $TIMEOUT ost1
1215
1216                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
1217                 # DQACQ is sent, which makes at_max to take effect
1218                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
1219                                                                 conv=notrunc
1220                 rm -f $TESTFILE
1221                 wait_delete_completed
1222         fi
1223
1224         sync; sync
1225         sync_all_data || true
1226
1227         #define QUOTA_DQACQ 601
1228         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1229         lustre_fail mds 0x513 601
1230
1231         do_facet ost1 $LCTL set_param \
1232                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1233
1234         # write to un-enforced ID ($TSTUSR2) should succeed
1235         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1236                 error "write failure, expect success"
1237
1238         # write to enforced ID ($TSTUSR) in background, exceeding limit
1239         # to make sure DQACQ is sent
1240         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1241         DDPID=$!
1242
1243         # watchdog timer uses a factor of 2
1244         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1245         sleep $((TIMEOUT * 2 + 1))
1246
1247         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1248
1249         # write should be blocked and never finished
1250         if ! ps -p $DDPID  > /dev/null 2>&1; then
1251                 lustre_fail mds 0 0
1252                 error "write finished incorrectly!"
1253         fi
1254
1255         lustre_fail mds 0 0
1256
1257         # no watchdog is triggered
1258         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1259         watchdog=$(awk '/Service thread pid/ && /was inactive/ \
1260                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1261         [ -z "$watchdog" ] || error "$watchdog"
1262
1263         rm -f $TMP/lustre-log-${TESTNAME}.log
1264
1265         # write should continue then fail with EDQUOT
1266         local count=0
1267         local c_size
1268         while [ true ]; do
1269                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1270                 if [ $count -ge 240 ]; then
1271                         quota_error u $TSTUSR "dd not finished in $count secs"
1272                 fi
1273                 count=$((count + 1))
1274                 if [ $((count % 30)) -eq 0 ]; then
1275                         c_size=$(stat -c %s $TESTFILE)
1276                         echo "Waiting $count secs. $c_size"
1277                         $SHOW_QUOTA_USER
1278                 fi
1279                 sleep 1
1280         done
1281
1282         cleanup_quota_test
1283         resetquota -u $TSTUSR
1284 }
1285 run_test 6 "Test dropping acquire request on master"
1286
1287 # quota reintegration (global index)
1288 test_7a() {
1289         local TESTFILE=$DIR/$tdir/$tfile
1290         local LIMIT=20 # 20M
1291
1292         [ "$SLOW" = "no" ] && LIMIT=5
1293
1294         setup_quota_test || error "setup quota failed with $?"
1295         trap cleanup_quota_test EXIT
1296
1297         # make sure the system is clean
1298         local USED=$(getquota -u $TSTUSR global curspace)
1299         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1300
1301         # make sure no granted quota on ost1
1302         set_ost_qtype $QTYPE || error "enable ost quota failed"
1303         resetquota -u $TSTUSR
1304         set_ost_qtype "none" || error "disable ost quota failed"
1305
1306         local OSTUUID=$(ostuuid_from_index 0)
1307         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1308         [ $USED -ne 0 ] &&
1309                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1310
1311         # create test file
1312         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1313         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1314
1315         echo "Stop ost1..."
1316         stop ost1
1317
1318         echo "Enable quota & set quota limit for $TSTUSR"
1319         set_ost_qtype $QTYPE || error "enable ost quota failed"
1320         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1321                 error "set quota failed"
1322
1323         echo "Start ost1..."
1324         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1325         quota_init
1326
1327         wait_ost_reint $QTYPE || error "reintegration failed"
1328
1329         # hardlimit should have been fetched by slave during global
1330         # reintegration, write will exceed quota
1331         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1332                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1333
1334         rm -f $TESTFILE
1335         wait_delete_completed
1336         sync_all_data || true
1337         sleep 3
1338
1339         echo "Stop ost1..."
1340         stop ost1
1341
1342         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1343                 error "clear quota failed"
1344
1345         echo "Start ost1..."
1346         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1347         quota_init
1348
1349         wait_ost_reint $QTYPE || error "reintegration failed"
1350
1351         # hardlimit should be cleared on slave during reintegration
1352         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1353                 quota_error u $TSTUSR "write error, but expect success"
1354
1355         cleanup_quota_test
1356         resetquota -u $TSTUSR
1357 }
1358 run_test 7a "Quota reintegration (global index)"
1359
1360 # quota reintegration (slave index)
1361 test_7b() {
1362         local LIMIT="100G"
1363         local TESTFILE=$DIR/$tdir/$tfile
1364
1365         setup_quota_test || error "setup quota failed with $?"
1366         trap cleanup_quota_test EXIT
1367
1368         # make sure the system is clean
1369         local USED=$(getquota -u $TSTUSR global curspace)
1370         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1371
1372         # make sure no granted quota on ost1
1373         set_ost_qtype $QTYPE || error "enable ost quota failed"
1374         resetquota -u $TSTUSR
1375         set_ost_qtype "none" || error "disable ost quota failed"
1376
1377         local OSTUUID=$(ostuuid_from_index 0)
1378         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1379         [ $USED -ne 0 ] &&
1380                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1381
1382         # create test file
1383         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1384         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1385
1386         # consume some space to make sure the granted space will not
1387         # be released during reconciliation
1388         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1389                 error "consume space failure, expect success"
1390
1391         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1392         lustre_fail mds 0xa02
1393
1394         set_ost_qtype $QTYPE || error "enable ost quota failed"
1395         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1396                 error "set quota failed"
1397
1398         # ignore the write error
1399         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1400
1401         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1402
1403         lustre_fail mds 0
1404
1405         echo "Restart ost to trigger reintegration..."
1406         stop ost1
1407         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1408         quota_init
1409
1410         wait_ost_reint $QTYPE || error "reintegration failed"
1411
1412         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1413         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1414
1415         cleanup_quota_test
1416         resetquota -u $TSTUSR
1417         $SHOW_QUOTA_USER
1418 }
1419 run_test 7b "Quota reintegration (slave index)"
1420
1421 # quota reintegration (restart mds during reintegration)
1422 test_7c() {
1423         local LIMIT=20 # 20M
1424         local TESTFILE=$DIR/$tdir/$tfile
1425
1426         [ "$SLOW" = "no" ] && LIMIT=5
1427
1428         setup_quota_test || error "setup quota failed with $?"
1429         trap cleanup_quota_test EXIT
1430
1431         # make sure the system is clean
1432         local USED=$(getquota -u $TSTUSR global curspace)
1433         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1434
1435         set_ost_qtype "none" || error "disable ost quota failed"
1436         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1437                 error "set quota failed"
1438
1439         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1440         lustre_fail ost 0xa03
1441
1442         # enable ost quota
1443         set_ost_qtype $QTYPE || error "enable ost quota failed"
1444         # trigger reintegration
1445         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
1446         procf=${procf}quota_slave.force_reint
1447         do_facet ost1 $LCTL set_param $procf=1 ||
1448                 error "force reintegration failed"
1449
1450         echo "Stop mds..."
1451         stop mds1
1452
1453         lustre_fail ost 0
1454
1455         echo "Start mds..."
1456         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1457         quota_init
1458
1459         # wait longer than usual to make sure the reintegration
1460         # is triggered by quota wb thread.
1461         wait_ost_reint $QTYPE 200 || error "reintegration failed"
1462
1463         # hardlimit should have been fetched by slave during global
1464         # reintegration, write will exceed quota
1465         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1466                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1467
1468         cleanup_quota_test
1469         resetquota -u $TSTUSR
1470 }
1471 run_test 7c "Quota reintegration (restart mds during reintegration)"
1472
1473 # Quota reintegration (Transfer index in multiple bulks)
1474 test_7d(){
1475         local TESTFILE=$DIR/$tdir/$tfile
1476         local TESTFILE1="$DIR/$tdir/$tfile"-1
1477         local limit=20 #20M
1478
1479         setup_quota_test || error "setup quota failed with $?"
1480         trap cleanup_quota_test EXIT
1481
1482         set_ost_qtype "none" || error "disable ost quota failed"
1483         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1484                 error "set quota for $TSTUSR failed"
1485         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1486                 error "set quota for $TSTUSR2 failed"
1487
1488         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1489         lustre_fail mds 0x608 0
1490
1491         # enable quota to tirgger reintegration
1492         set_ost_qtype "u" || error "enable ost quota failed"
1493         wait_ost_reint "u" || error "reintegration failed"
1494
1495         lustre_fail mds 0
1496
1497         # hardlimit should have been fetched by slave during global
1498         # reintegration, write will exceed quota
1499         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1500                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1501
1502         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1503                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1504
1505         cleanup_quota_test
1506         resetquota -u $TSTUSR
1507         resetquota -u $TSTUSR2
1508 }
1509 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1510
1511 # quota reintegration (inode limits)
1512 test_7e() {
1513         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1514
1515         # LU-2435: skip this quota test if underlying zfs version has not
1516         # supported native dnode accounting
1517         [ "$(facet_fstype mds1)" == "zfs" ] && {
1518                 local F="feature@userobj_accounting"
1519                 local pool=$(zpool_name mds1)
1520                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
1521
1522                 [[ "$feature" != *" active "* ]] &&
1523                         skip "requires zpool with active userobj_accounting"
1524         }
1525
1526         local ilimit=$((1024 * 2)) # 2k inodes
1527         local TESTFILE=$DIR/${tdir}-1/$tfile
1528
1529         setup_quota_test || error "setup quota failed with $?"
1530         trap cleanup_quota_test EXIT
1531
1532         # make sure the system is clean
1533         local USED=$(getquota -u $TSTUSR global curinodes)
1534         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1535
1536         # make sure no granted quota on mdt1
1537         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1538         resetquota -u $TSTUSR
1539         set_mdt_qtype "none" || error "disable mdt quota failed"
1540
1541         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1542         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1543         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1544                 "$TSTUSR isn't 0."
1545
1546         echo "Stop mds${MDSCOUNT}..."
1547         stop mds${MDSCOUNT}
1548
1549         echo "Enable quota & set quota limit for $TSTUSR"
1550         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1551         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1552                 error "set quota failed"
1553
1554         echo "Start mds${MDSCOUNT}..."
1555         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1556         quota_init
1557
1558         wait_mdt_reint $QTYPE || error "reintegration failed"
1559
1560         echo "create remote dir"
1561         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1562                 error "create remote dir failed"
1563         chmod 0777 $DIR/${tdir}-1
1564
1565         # hardlimit should have been fetched by slave during global
1566         # reintegration, create will exceed quota
1567         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1568                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1569
1570         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1571         wait_delete_completed
1572         sync_all_data || true
1573
1574         echo "Stop mds${MDSCOUNT}..."
1575         stop mds${MDSCOUNT}
1576
1577         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1578                 error "clear quota failed"
1579
1580         echo "Start mds${MDSCOUNT}..."
1581         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1582         quota_init
1583
1584         wait_mdt_reint $QTYPE || error "reintegration failed"
1585
1586         # hardlimit should be cleared on slave during reintegration
1587         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1588                 quota_error u $TSTUSR "create failed, expect success"
1589
1590         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1591         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1592
1593         cleanup_quota_test
1594         resetquota -u $TSTUSR
1595 }
1596 run_test 7e "Quota reintegration (inode limits)"
1597
1598 # run dbench with quota enabled
1599 test_8() {
1600         local BLK_LIMIT="100g" #100G
1601         local FILE_LIMIT=1000000
1602
1603         setup_quota_test || error "setup quota failed with $?"
1604         trap cleanup_quota_test EXIT
1605
1606         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1607         set_ost_qtype $QTYPE || error "enable ost quota failed"
1608
1609         echo "Set enough high limit for user: $TSTUSR"
1610         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1611                 error "set user quota failed"
1612         echo "Set enough high limit for group: $TSTUSR"
1613         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1614                 error "set group quota failed"
1615         if is_project_quota_supported; then
1616                 change_project -sp $TSTPRJID $DIR/$tdir
1617                 echo "Set enough high limit for project: $TSTPRJID"
1618                 $LFS setquota -p $TSTPRJID -b 0 \
1619                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1620                         error "set project quota failed"
1621         fi
1622
1623         local duration=""
1624         [ "$SLOW" = "no" ] && duration=" -t 120"
1625         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1626                 quota_error a $TSTUSR "dbench failed!"
1627
1628         is_project_quota_supported && change_project -C $DIR/$tdir
1629         cleanup_quota_test
1630         resetquota -u $TSTUSR
1631         resetquota -g $TSTUSR
1632         resetquota -p $TSTPRJID
1633 }
1634 run_test 8 "Run dbench with quota enabled"
1635
1636 # this check is just for test_9
1637 OST0_MIN=4900000 #4.67G
1638
1639 check_whether_skip () {
1640         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1641         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1642         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1643                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1644                 return 0
1645         else
1646                 return 1
1647         fi
1648 }
1649
1650 # run for fixing bug10707, it needs a big room. test for 64bit
1651 test_9() {
1652         local filesize=$((1024 * 9 / 2)) # 4.5G
1653
1654         check_whether_skip && return 0
1655
1656         setup_quota_test || error "setup quota failed with $?"
1657         trap cleanup_quota_test EXIT
1658
1659         set_ost_qtype "ug" || error "enable ost quota failed"
1660
1661         local TESTFILE="$DIR/$tdir/$tfile-0"
1662         local BLK_LIMIT=100G #100G
1663         local FILE_LIMIT=1000000
1664
1665         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1666
1667         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1668                 "for user: $TSTUSR"
1669         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1670                 error "set user quota failed"
1671
1672         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1673                 "for group: $TSTUSR"
1674         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1675                 error "set group quota failed"
1676
1677         quota_show_check a u $TSTUSR
1678         quota_show_check a g $TSTUSR
1679
1680         echo "Create test file"
1681         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1682         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1683
1684         log "Write the big file of 4.5G ..."
1685         $RUNAS $DD of=$TESTFILE count=$filesize ||
1686                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1687
1688         $SHOW_QUOTA_USER
1689         $SHOW_QUOTA_GROUP
1690
1691         cleanup_quota_test
1692         resetquota -u $TSTUSR
1693         resetquota -g $TSTUSR
1694
1695         $SHOW_QUOTA_USER
1696         $SHOW_QUOTA_GROUP
1697 }
1698 run_test 9 "Block limit larger than 4GB (b10707)"
1699
1700 test_10() {
1701         local TESTFILE=$DIR/$tdir/$tfile
1702
1703         setup_quota_test || error "setup quota failed with $?"
1704         trap cleanup_quota_test EXIT
1705
1706         # set limit to root user should fail
1707         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1708                 error "set limit for root user successfully, expect failure"
1709         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1710                 error "set limit for root group successfully, expect failure"
1711         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
1712                 error "set limit for project 0 successfully, expect failure"
1713
1714         # root user can overrun quota
1715         set_ost_qtype "ug" || error "enable ost quota failed"
1716
1717         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1718                 error "set quota failed"
1719         quota_show_check b u $TSTUSR
1720
1721         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1722         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1723
1724         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1725                 error "write failure, expect success"
1726
1727         cleanup_quota_test
1728         resetquota -u $TSTUSR
1729 }
1730 run_test 10 "Test quota for root user"
1731
1732 test_11() {
1733         local TESTFILE=$DIR/$tdir/$tfile
1734         setup_quota_test || error "setup quota failed with $?"
1735         trap cleanup_quota_test EXIT
1736
1737         set_mdt_qtype "ug" || error "enable mdt quota failed"
1738         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1739                 error "set quota failed"
1740
1741         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1742         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1743
1744         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1745         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1746
1747         $SHOW_QUOTA_USER
1748         local USED=$(getquota -u $TSTUSR global curinodes)
1749         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1750
1751         cleanup_quota_test
1752         resetquota -u $TSTUSR
1753 }
1754 run_test 11 "Chown/chgrp ignores quota"
1755
1756 test_12a() {
1757         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
1758
1759         local blimit=22 # 22M
1760         local blk_cnt=$((blimit - 5))
1761         local TESTFILE0="$DIR/$tdir/$tfile"-0
1762         local TESTFILE1="$DIR/$tdir/$tfile"-1
1763
1764         setup_quota_test || error "setup quota failed with $?"
1765         trap cleanup_quota_test EXIT
1766
1767         set_ost_qtype "u" || error "enable ost quota failed"
1768         quota_show_check b u $TSTUSR
1769
1770         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1771                 error "set quota failed"
1772
1773         $SETSTRIPE $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1774         $SETSTRIPE $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1775         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1776         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1777
1778         echo "Write to ost0..."
1779         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1780                 quota_error a $TSTUSR "dd failed"
1781
1782         echo "Write to ost1..."
1783         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1784                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1785
1786         echo "Free space from ost0..."
1787         rm -f $TESTFILE0
1788         wait_delete_completed
1789         sync_all_data || true
1790
1791         echo "Write to ost1 after space freed from ost0..."
1792         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1793                 quota_error a $TSTUSR "rebalancing failed"
1794
1795         cleanup_quota_test
1796         resetquota -u $TSTUSR
1797 }
1798 run_test 12a "Block quota rebalancing"
1799
1800 test_12b() {
1801         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1802
1803         local ilimit=$((1024 * 2)) # 2k inodes
1804         local TESTFILE0=$DIR/$tdir/$tfile
1805         local TESTFILE1=$DIR/${tdir}-1/$tfile
1806
1807         setup_quota_test || error "setup quota failed with $?"
1808         trap cleanup_quota_test EXIT
1809
1810         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1811         chmod 0777 $DIR/${tdir}-1
1812
1813         set_mdt_qtype "u" || error "enable mdt quota failed"
1814         quota_show_check f u $TSTUSR
1815
1816         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1817                 error "set quota failed"
1818
1819         echo "Create $ilimit files on mdt0..."
1820         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1821                 quota_error u $TSTUSR "create failed, but expect success"
1822
1823         echo "Create files on mdt1..."
1824         $RUNAS createmany -m $TESTFILE1 1 &&
1825                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1826
1827         echo "Free space from mdt0..."
1828         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1829         wait_delete_completed
1830         sync_all_data || true
1831
1832         echo "Create files on mdt1 after space freed from mdt0..."
1833         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1834                 quota_error a $TSTUSR "rebalancing failed"
1835
1836         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1837                 error "unlink mdt1 files failed"
1838         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1839
1840         cleanup_quota_test
1841         resetquota -u $TSTUSR
1842 }
1843 run_test 12b "Inode quota rebalancing"
1844
1845 test_13(){
1846         local TESTFILE=$DIR/$tdir/$tfile
1847         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1848         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1849
1850         setup_quota_test || error "setup quota failed with $?"
1851         trap cleanup_quota_test EXIT
1852
1853         set_ost_qtype "u" || error "enable ost quota failed"
1854         quota_show_check b u $TSTUSR
1855
1856         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1857                 error "set quota failed"
1858         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1859         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1860
1861         # clear the locks in cache first
1862         do_facet ost1 $LCTL set_param -n $procf=clear
1863         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1864         [ $nlock -eq 0 ] || error "$nlock cached locks"
1865
1866         # write to acquire the per-ID lock
1867         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1868                 quota_error a $TSTUSR "dd failed"
1869
1870         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1871         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1872
1873         # clear quota doesn't trigger per-ID lock cancellation
1874         resetquota -u $TSTUSR
1875         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1876         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1877
1878         # clear the per-ID lock
1879         do_facet ost1 $LCTL set_param -n $procf=clear
1880         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1881         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1882
1883         # spare quota should be released
1884         local OSTUUID=$(ostuuid_from_index 0)
1885         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1886         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1887         [ $limit -le $space ] ||
1888                 error "spare quota isn't released, limit:$limit, space:$space"
1889
1890         cleanup_quota_test
1891 }
1892 run_test 13 "Cancel per-ID lock in the LRU list"
1893
1894 test_15(){
1895         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1896
1897         wait_delete_completed
1898         sync_all_data || true
1899
1900         # test for user
1901         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1902                 error "set user quota failed"
1903         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1904         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1905                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1906         resetquota -u $TSTUSR
1907
1908         # test for group
1909         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1910                 error "set group quota failed"
1911         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1912         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1913                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1914         resetquota -g $TSTUSR
1915 }
1916 run_test 15 "Set over 4T block quota"
1917
1918 test_17sub() {
1919         local err_code=$1
1920         local BLKS=1    # 1M less than limit
1921         local TESTFILE=$DIR/$tdir/$tfile
1922
1923         setup_quota_test || error "setup quota failed with $?"
1924         trap cleanup_quota_test EXIT
1925
1926         # make sure the system is clean
1927         local USED=$(getquota -u $TSTUSR global curspace)
1928         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1929
1930         set_ost_qtype "ug" || error "enable ost quota failed"
1931         # make sure no granted quota on ost
1932         resetquota -u $TSTUSR
1933         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1934                 error "set quota failed"
1935
1936         quota_show_check b u $TSTUSR
1937
1938         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1939         lustre_fail mds 0xa04 $err_code
1940
1941         # write in background
1942         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1943         local DDPID=$!
1944
1945         sleep 2
1946         # write should be blocked and never finished
1947         if ! ps -p $DDPID  > /dev/null 2>&1; then
1948                 lustre_fail mds 0 0
1949                 quota_error u $TSTUSR "write finished incorrectly!"
1950         fi
1951
1952         lustre_fail mds 0 0
1953
1954         local count=0
1955         local timeout=30
1956         while [ true ]; do
1957                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1958                 count=$((count+1))
1959                 if [ $count -gt $timeout ]; then
1960                         quota_error u $TSTUSR "dd is not finished!"
1961                 fi
1962                 sleep 1
1963         done
1964
1965         sync; sync_all_data || true
1966
1967         USED=$(getquota -u $TSTUSR global curspace)
1968         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1969                 "Used space(${USED}K) is less than ${BLKS}M"
1970
1971         cleanup_quota_test
1972         resetquota -u $TSTUSR
1973 }
1974
1975 # DQACQ return recoverable error
1976 test_17() {
1977         echo "DQACQ return -ENOLCK"
1978         #define ENOLCK  37
1979         test_17sub 37 || error "Handle -ENOLCK failed"
1980
1981         echo "DQACQ return -EAGAIN"
1982         #define EAGAIN  11
1983         test_17sub 11 || error "Handle -EAGAIN failed"
1984
1985         echo "DQACQ return -ETIMEDOUT"
1986         #define ETIMEDOUT 110
1987         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1988
1989         echo "DQACQ return -ENOTCONN"
1990         #define ENOTCONN 107
1991         test_17sub 107 || error "Handle -ENOTCONN failed"
1992 }
1993
1994 run_test 17 "DQACQ return recoverable error"
1995
1996 test_18_sub () {
1997         local io_type=$1
1998         local blimit="200m" # 200M
1999         local TESTFILE="$DIR/$tdir/$tfile"
2000
2001         setup_quota_test || error "setup quota failed with $?"
2002         trap cleanup_quota_test EXIT
2003
2004         set_ost_qtype "u" || error "enable ost quota failed"
2005         log "User quota (limit: $blimit)"
2006         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
2007                 error "set quota failed"
2008         quota_show_check b u $TSTUSR
2009
2010         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2011         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2012
2013         local timeout=$(sysctl -n lustre.timeout)
2014
2015         if [ $io_type = "directio" ]; then
2016                 log "Write 100M (directio) ..."
2017                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
2018         else
2019                 log "Write 100M (buffered) ..."
2020                 $RUNAS $DD of=$TESTFILE count=100 &
2021         fi
2022         local DDPID=$!
2023
2024         replay_barrier $SINGLEMDS
2025         log "Fail mds for $((2 * timeout)) seconds"
2026         fail $SINGLEMDS $((2 * timeout))
2027
2028         local count=0
2029         if at_is_enabled; then
2030                 timeout=$(at_max_get mds)
2031         else
2032                 timeout=$(lctl get_param -n timeout)
2033         fi
2034
2035         while [ true ]; do
2036                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2037                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
2038                         log "it took $count second"
2039                 fi
2040                 sleep 1
2041         done
2042
2043         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
2044         sync
2045         cancel_lru_locks mdc
2046         cancel_lru_locks osc
2047         $SHOW_QUOTA_USER
2048
2049         local testfile_size=$(stat -c %s $TESTFILE)
2050         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
2051                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
2052                         "got ${testfile_size}. Verifying file failed!"
2053         fi
2054         cleanup_quota_test
2055         resetquota -u $TSTUSR
2056 }
2057
2058 # test when mds does failover, the ost still could work well
2059 # this test shouldn't trigger watchdog b=14840
2060 test_18() {
2061         # Clear dmesg so watchdog is not triggered by previous
2062         # test output
2063         do_facet ost1 dmesg -c > /dev/null
2064
2065         test_18_sub normal
2066         test_18_sub directio
2067
2068         # check if watchdog is triggered
2069         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2070         local watchdog=$(awk '/Service thread pid/ && /was inactive/ \
2071                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
2072         [ -z "$watchdog" ] || error "$watchdog"
2073         rm -f $TMP/lustre-log-${TESTNAME}.log
2074 }
2075 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
2076
2077 test_19() {
2078         local blimit=5 # 5M
2079         local TESTFILE=$DIR/$tdir/$tfile
2080
2081         setup_quota_test || error "setup quota failed with $?"
2082         trap cleanup_quota_test EXIT
2083
2084         set_ost_qtype $QTYPE || error "enable ost quota failed"
2085
2086         # bind file to a single OST
2087         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2088         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2089
2090         echo "Set user quota (limit: ${blimit}M)"
2091         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2092                 error "set user quota failed"
2093         quota_show_check b u $TSTUSR
2094         echo "Update quota limits"
2095         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2096                 error "set group quota failed"
2097         quota_show_check b u $TSTUSR
2098
2099         # first wirte might be cached
2100         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2101         cancel_lru_locks osc
2102         $SHOW_QUOTA_USER
2103         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2104                 quota_error u $TSTUSR "Write success, expect failure"
2105         $SHOW_QUOTA_USER
2106
2107         cleanup_quota_test
2108         resetquota -u $TSTUSR
2109 }
2110 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2111
2112 test_20() { # b15754
2113         local LSTR=(2g 1t 4k 3m) # limits strings
2114         # limits values
2115         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2116                     $((3*1024*1024)))
2117
2118         resetquota -u $TSTUSR
2119
2120         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2121                 $MOUNT || error "could not set quota limits"
2122         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2123                                 --inode-softlimit ${LSTR[2]} \
2124                                 --inode-hardlimit ${LSTR[3]} \
2125                                 $MOUNT || error "could not set quota limits"
2126
2127         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2128                 error "bsoftlimit was not set properly"
2129         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2130                 error "bhardlimit was not set properly"
2131         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2132                 error "isoftlimit was not set properly"
2133         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2134                 error "ihardlimit was not set properly"
2135
2136         resetquota -u $TSTUSR
2137 }
2138 run_test 20 "Test if setquota specifiers work properly (b15754)"
2139
2140 test_21_sub() {
2141         local testfile=$1
2142         local blk_number=$2
2143         local seconds=$3
2144
2145         local time=$(($(date +%s) + seconds))
2146         while [ $(date +%s) -lt $time ]; do
2147                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2148         done
2149 }
2150
2151 # run for fixing bug16053, setquota shouldn't fail when writing and
2152 # deleting are happening
2153 test_21() {
2154         local TESTFILE="$DIR/$tdir/$tfile"
2155         local BLIMIT=10 # 10G
2156         local ILIMIT=1000000
2157
2158         setup_quota_test || error "setup quota failed with $?"
2159         trap cleanup_quota_test EXIT
2160
2161         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2162
2163         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2164         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2165                 error "set user quota failed"
2166         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2167         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2168                 error "set group quota failed"
2169         if is_project_quota_supported; then
2170                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2171                         "project: $TSTPRJID"
2172                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2173                          $MOUNT || error "set project quota failed"
2174         fi
2175
2176         # repeat writing on a 1M file
2177         test_21_sub ${TESTFILE}_1 1 30 &
2178         local DDPID1=$!
2179         # repeat writing on a 128M file
2180         test_21_sub ${TESTFILE}_2 128 30 &
2181         local DDPID2=$!
2182
2183         local time=$(($(date +%s) + 30))
2184         local i=1
2185         while [ $(date +%s) -lt $time ]; do
2186                 log "Set quota for $i times"
2187                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2188                         -I $((ILIMIT + i)) $MOUNT ||
2189                                 error "Set user quota failed"
2190                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2191                         -I $((ILIMIT + i)) $MOUNT ||
2192                                 error "Set group quota failed"
2193                 if is_project_quota_supported; then
2194                         $LFS setquota -p $TSTPRJID -b 0 -B \
2195                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2196                                 error "Set project quota failed"
2197                 fi
2198                 i=$((i+1))
2199                 sleep 1
2200         done
2201
2202         local count=0
2203         while [ true ]; do
2204                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2205                 count=$((count+1))
2206                 if [ $count -gt 60 ]; then
2207                         quota_error a $TSTUSR "dd should be finished!"
2208                 fi
2209                 sleep 1
2210         done
2211         echo "(dd_pid=$DDPID1, time=$count)successful"
2212
2213         count=0
2214         while [ true ]; do
2215                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2216                 count=$((count+1))
2217                 if [ $count -gt 60 ]; then
2218                         quota_error a $TSTUSR "dd should be finished!"
2219                 fi
2220                 sleep 1
2221         done
2222         echo "(dd_pid=$DDPID2, time=$count)successful"
2223
2224         cleanup_quota_test
2225         resetquota -u $TSTUSR
2226         resetquota -g $TSTUSR
2227         resetquota -p $TSTPRJID
2228 }
2229 run_test 21 "Setquota while writing & deleting (b16053)"
2230
2231 # enable/disable quota enforcement permanently
2232 test_22() {
2233         echo "Set both mdt & ost quota type as ug"
2234         local qtype="ug"
2235         is_project_quota_supported && qtype=$QTYPE
2236         set_mdt_qtype $qtype || error "enable mdt quota failed"
2237         set_ost_qtype $qtype || error "enable ost quota failed"
2238
2239         echo "Restart..."
2240         stopall || error "failed to stopall (1)"
2241         mount
2242         setupall
2243
2244         echo "Verify if quota is enabled"
2245         local qtype1=$(mdt_quota_type)
2246         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2247         qtype=$(ost_quota_type)
2248         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2249
2250         echo "Set both mdt & ost quota type as none"
2251         set_mdt_qtype "none" || error "disable mdt quota failed"
2252         set_ost_qtype "none" || error "disable ost quota failed"
2253
2254         echo "Restart..."
2255         stopall || error "failed to stopall (2)"
2256         mount
2257         setupall
2258         quota_init
2259
2260         echo "Verify if quota is disabled"
2261         qtype=$(mdt_quota_type)
2262         [ $qtype != "none" ] && error "mdt quota setting is lost"
2263         qtype=$(ost_quota_type)
2264         [ $qtype != "none" ] && error "ost quota setting is lost"
2265
2266         return 0
2267 }
2268 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2269
2270 test_23_sub() {
2271         local TESTFILE="$DIR/$tdir/$tfile"
2272         local LIMIT=$1
2273
2274         setup_quota_test || error "setup quota failed with $?"
2275         trap cleanup_quota_test EXIT
2276
2277         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2278
2279         # test for user
2280         log "User quota (limit: $LIMIT MB)"
2281         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2282                 error "set quota failed"
2283         quota_show_check b u $TSTUSR
2284
2285         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2286         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2287
2288         log "Step1: trigger EDQUOT with O_DIRECT"
2289         log "Write half of file"
2290         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2291                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2292                         "limit=$LIMIT"
2293         log "Write out of block quota ..."
2294         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2295                 oflag=direct conv=notrunc &&
2296                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2297                         "limit=$LIMIT"
2298         log "Step1: done"
2299
2300         log "Step2: rewrite should succeed"
2301         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2302                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2303                         "limit=$LIMIT"
2304         log "Step2: done"
2305
2306         cleanup_quota_test
2307
2308         local OST0_UUID=$(ostuuid_from_index 0)
2309         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2310         [ $OST0_QUOTA_USED -ne 0 ] &&
2311                 ($SHOW_QUOTA_USER; \
2312                 quota_error u $TSTUSR "quota isn't released")
2313         $SHOW_QUOTA_USER
2314         resetquota -u $TSTUSR
2315 }
2316
2317 test_23() {
2318         [ $(facet_fstype ost1) == "zfs" ] &&
2319                 skip "Overwrite in place is not guaranteed to be " \
2320                 "space neutral on ZFS"
2321
2322         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2323         check_whether_skip && return 0
2324         log "run for 4MB test file"
2325         test_23_sub 4
2326
2327         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2328         check_whether_skip && return 0
2329         log "run for 40MB test file"
2330         test_23_sub 40
2331 }
2332 run_test 23 "Quota should be honored with directIO (b16125)"
2333
2334 test_24() {
2335         local blimit=5 # 5M
2336         local TESTFILE="$DIR/$tdir/$tfile"
2337
2338         setup_quota_test || error "setup quota failed with $?"
2339         trap cleanup_quota_test EXIT
2340
2341         set_ost_qtype $QTYPE || error "enable ost quota failed"
2342
2343         # bind file to a single OST
2344         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2345         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2346
2347         echo "Set user quota (limit: ${blimit}M)"
2348         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2349                 error "set quota failed"
2350
2351         # overrun quota by root user
2352         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2353                 error "write failure, expect success"
2354         cancel_lru_locks osc
2355         sync_all_data || true
2356
2357         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2358
2359         cleanup_quota_test
2360         resetquota -u $TSTUSR
2361 }
2362 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2363
2364 test_27a() { # b19612
2365         $LFS quota $TSTUSR $DIR &&
2366                 error "lfs succeeded with no type, but should have failed"
2367         $LFS setquota $TSTUSR $DIR &&
2368                 error "lfs succeeded with no type, but should have failed"
2369         return 0
2370 }
2371 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2372
2373 test_27b() { # b20200
2374         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2375                 error "lfs setquota failed with uid argument"
2376         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2377                 error "lfs stequota failed with gid argument"
2378         if is_project_quota_supported; then
2379                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2380                         1000 $DIR || error \
2381                                 "lfs stequota failed with projid argument"
2382         fi
2383         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2384         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2385         if is_project_quota_supported; then
2386                 $SHOW_QUOTA_PROJID ||
2387                         error "lfs quota failed with projid argument"
2388         fi
2389         resetquota -u $TSTUSR
2390         resetquota -g $TSTUSR
2391         resetquota -p $TSTPRJID
2392         return 0
2393 }
2394 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2395
2396 test_27c() {
2397         local limit
2398
2399         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2400                 error "lfs setquota failed"
2401
2402         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2403         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2404         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2405         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2406
2407         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2408                 error "lfs setquota for $TSTID failed"
2409
2410         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2411         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2412         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2413         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2414
2415         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2416                 error "total allocated inode/block limit not printed"
2417
2418         resetquota -u $TSTUSR
2419 }
2420 run_test 27c "lfs quota should support human-readable output"
2421
2422 test_27d() {
2423         local softlimit=1.5
2424         local hardlimit=2.3
2425         local limit
2426
2427         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2428                 error "set fraction block limit failed"
2429         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2430         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2431         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2432         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2433
2434         resetquota -u $TSTUSR
2435 }
2436 run_test 27d "lfs setquota should support fraction block limit"
2437
2438 test_30() {
2439         local LIMIT=4 # 4MB
2440         local TESTFILE="$DIR/$tdir/$tfile"
2441         local GRACE=10
2442
2443         setup_quota_test || error "setup quota failed with $?"
2444         trap cleanup_quota_test EXIT
2445
2446         set_ost_qtype "u" || error "enable ost quota failed"
2447
2448         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2449         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2450
2451         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2452                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2453         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2454                 error "set quota failed"
2455         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2456         cancel_lru_locks osc
2457         sleep $GRACE
2458         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2459         # over-quota flag has not yet settled since we do not trigger async
2460         # events based on grace time period expiration
2461         $SHOW_QUOTA_USER
2462         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2463         cancel_lru_locks osc
2464         # now over-quota flag should be settled and further writes should fail
2465         $SHOW_QUOTA_USER
2466         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2467                 error "grace times were reset"
2468         # cleanup
2469         cleanup_quota_test
2470         resetquota -u $TSTUSR
2471         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2472                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2473 }
2474 run_test 30 "Hard limit updates should not reset grace times"
2475
2476 # basic usage tracking for user & group
2477 test_33() {
2478         local INODES=10 # 10 files
2479         local BLK_CNT=2 # of 2M each
2480         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2481
2482         setup_quota_test || error "setup quota failed with $?"
2483         trap cleanup_quota_test EXIT
2484
2485         # make sure the system is clean
2486         local USED=$(getquota -u $TSTID global curspace)
2487         [ $USED -ne 0 ] &&
2488                 error "Used space ($USED) for user $TSTID isn't 0."
2489         USED=$(getquota -g $TSTID global curspace)
2490         [ $USED -ne 0 ] &&
2491                 error "Used space ($USED) for group $TSTID isn't 0."
2492         if is_project_quota_supported; then
2493                 USED=$(getquota -p $TSTPRJID global curspace)
2494                 [ $USED -ne 0 ] && error \
2495                         "Used space ($USED) for project $TSTPRJID isn't 0."
2496         fi
2497
2498         echo "Write files..."
2499         for i in $(seq 0 $INODES); do
2500                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2501                         error "write failed"
2502                         is_project_quota_supported &&
2503                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
2504                 echo "Iteration $i/$INODES completed"
2505         done
2506         cancel_lru_locks osc
2507
2508         echo "Wait for setattr on objects finished..."
2509         wait_delete_completed
2510
2511         sync; sync_all_data || true
2512
2513         echo "Verify disk usage after write"
2514         USED=$(getquota -u $TSTID global curspace)
2515         [ $USED -lt $TOTAL_BLKS ] &&
2516                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2517         USED=$(getquota -g $TSTID global curspace)
2518         [ $USED -lt $TOTAL_BLKS ] &&
2519                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2520         if is_project_quota_supported; then
2521                 USED=$(getquota -p $TSTPRJID global curspace)
2522                 [ $USED -lt $TOTAL_BLKS ] && error \
2523                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
2524         fi
2525
2526         echo "Verify inode usage after write"
2527         USED=$(getquota -u $TSTID global curinodes)
2528         [ $USED -lt $INODES ] &&
2529                 error "Used inode for user $TSTID is $USED, expected $INODES"
2530         USED=$(getquota -g $TSTID global curinodes)
2531         [ $USED -lt $INODES ] &&
2532                 error "Used inode for group $TSTID is $USED, expected $INODES"
2533         if is_project_quota_supported; then
2534                 USED=$(getquota -p $TSTPRJID global curinodes)
2535                 [ $USED -lt $INODES ] && error \
2536                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
2537         fi
2538
2539         cleanup_quota_test
2540
2541         echo "Verify disk usage after delete"
2542         USED=$(getquota -u $TSTID global curspace)
2543         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2544         USED=$(getquota -u $TSTID global curinodes)
2545         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2546         USED=$(getquota -g $TSTID global curspace)
2547         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2548         USED=$(getquota -g $TSTID global curinodes)
2549         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2550         if is_project_quota_supported; then
2551                 USED=$(getquota -p $TSTPRJID global curspace)
2552                 [ $USED -eq 0 ] ||
2553                         error "Used space for project $TSTPRJID isn't 0. $USED"
2554                 USED=$(getquota -p $TSTPRJID global curinodes)
2555                 [ $USED -eq 0 ] ||
2556                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
2557         fi
2558 }
2559 run_test 33 "Basic usage tracking for user & group & project"
2560
2561 # usage transfer test for user & group & project
2562 test_34() {
2563         local BLK_CNT=2 # 2MB
2564         local project_supported="no"
2565
2566         is_project_quota_supported && project_supported="yes"
2567         setup_quota_test || error "setup quota failed with $?"
2568         trap cleanup_quota_test EXIT
2569
2570         # make sure the system is clean
2571         local USED=$(getquota -u $TSTID global curspace)
2572         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2573         USED=$(getquota -g $TSTID global curspace)
2574         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2575
2576         local USED=$(getquota -u $TSTID2 global curspace)
2577         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2578         if [ $project_supported == "yes" ]; then
2579                 USED=$(getquota -p $TSTPRJID global curspace)
2580                 [ $USED -ne 0 ] && error \
2581                         "Used space ($USED) for Project $TSTPRJID isn't 0."
2582         fi
2583
2584         echo "Write file..."
2585         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2586                 error "write failed"
2587         cancel_lru_locks osc
2588         sync; sync_all_data || true
2589
2590         echo "chown the file to user $TSTID"
2591         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2592
2593         echo "Wait for setattr on objects finished..."
2594         wait_delete_completed
2595
2596         BLK_CNT=$((BLK_CNT * 1024))
2597
2598         echo "Verify disk usage for user $TSTID"
2599         USED=$(getquota -u $TSTID global curspace)
2600         [ $USED -lt $BLK_CNT ] &&
2601                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2602         USED=$(getquota -u $TSTID global curinodes)
2603         [ $USED -ne 1 ] &&
2604                 error "Used inodes for user $TSTID is $USED, expected 1"
2605
2606         echo "chgrp the file to group $TSTID"
2607         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2608
2609         echo "Wait for setattr on objects finished..."
2610         wait_delete_completed
2611
2612         echo "Verify disk usage for group $TSTID"
2613         USED=$(getquota -g $TSTID global curspace)
2614         [ $USED -ge $BLK_CNT ] ||
2615                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2616         USED=$(getquota -g $TSTID global curinodes)
2617         [ $USED -eq 1 ] ||
2618                 error "Used inodes for group $TSTID is $USED, expected 1"
2619
2620         # chown won't change the ost object group. LU-4345 */
2621         echo "chown the file to user $TSTID2"
2622         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2623
2624         echo "Wait for setattr on objects finished..."
2625         wait_delete_completed
2626
2627         echo "change_project project id to $TSTPRJID"
2628         [ $project_supported == "yes" ] &&
2629                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2630         echo "Wait for setattr on objects finished..."
2631         wait_delete_completed
2632
2633         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2634         USED=$(getquota -u $TSTID2 global curspace)
2635         [ $USED -lt $BLK_CNT ] &&
2636                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2637         USED=$(getquota -u $TSTID global curspace)
2638         [ $USED -ne 0 ] &&
2639                 error "Used space for user $TSTID is $USED, expected 0"
2640         USED=$(getquota -g $TSTID global curspace)
2641         [ $USED -lt $BLK_CNT ] &&
2642                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2643         if [ $project_supported == "yes" ]; then
2644                 USED=$(getquota -p $TSTPRJID global curspace)
2645                 [ $USED -lt $BLK_CNT ] && error \
2646                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
2647         fi
2648
2649         cleanup_quota_test
2650 }
2651 run_test 34 "Usage transfer for user & group & project"
2652
2653 # usage is still accessible across restart
2654 test_35() {
2655         local BLK_CNT=2 # 2 MB
2656
2657         setup_quota_test || error "setup quota failed with $?"
2658         trap cleanup_quota_test EXIT
2659
2660         echo "Write file..."
2661         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2662                 error "write failed"
2663         is_project_quota_supported &&
2664                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2665         cancel_lru_locks osc
2666
2667         echo "Wait for setattr on objects finished..."
2668         wait_delete_completed
2669
2670         sync; sync_all_data || true
2671
2672         echo "Save disk usage before restart"
2673         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2674         [ $ORIG_USR_SPACE -eq 0 ] &&
2675                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2676         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2677         [ $ORIG_USR_INODES -eq 0 ] &&
2678                 error "Used inodes for user $TSTID is 0, expected 1"
2679         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2680         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2681         [ $ORIG_GRP_SPACE -eq 0 ] &&
2682                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2683         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2684         [ $ORIG_GRP_INODES -eq 0 ] &&
2685                 error "Used inodes for group $TSTID is 0, expected 1"
2686         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2687
2688         if is_project_quota_supported; then
2689                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
2690                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
2691                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
2692                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
2693                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
2694                         "Used inodes for project $TSTPRJID is 0, expected 1"
2695                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
2696         fi
2697
2698         log "Restart..."
2699         stopall
2700         setupall
2701         quota_init
2702
2703         echo "Verify disk usage after restart"
2704         local USED=$(getquota -u $TSTID global curspace)
2705         [ $USED -eq $ORIG_USR_SPACE ] ||
2706                 error "Used space for user $TSTID changed from " \
2707                         "$ORIG_USR_SPACE to $USED"
2708         USED=$(getquota -u $TSTID global curinodes)
2709         [ $USED -eq $ORIG_USR_INODES ] ||
2710                 error "Used inodes for user $TSTID changed from " \
2711                         "$ORIG_USR_INODES to $USED"
2712         USED=$(getquota -g $TSTID global curspace)
2713         [ $USED -eq $ORIG_GRP_SPACE ] ||
2714                 error "Used space for group $TSTID changed from " \
2715                         "$ORIG_GRP_SPACE to $USED"
2716         USED=$(getquota -g $TSTID global curinodes)
2717         [ $USED -eq $ORIG_GRP_INODES ] ||
2718                 error "Used inodes for group $TSTID changed from " \
2719                         "$ORIG_GRP_INODES to $USED"
2720         if [ $project_supported == "yes" ]; then
2721                 USED=$(getquota -p $TSTPRJID global curinodes)
2722                 [ $USED -eq $ORIG_PRJ_INODES ] ||
2723                         error "Used inodes for project $TSTPRJID " \
2724                                 "changed from $ORIG_PRJ_INODES to $USED"
2725                 USED=$(getquota -p $TSTPRJID global curspace)
2726                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
2727                         error "Used space for project $TSTPRJID "\
2728                                 "changed from $ORIG_PRJ_SPACE to $USED"
2729         fi
2730
2731         # check if the vfs_dq_init() is called before writing
2732         echo "Append to the same file..."
2733         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2734                 error "write failed"
2735         cancel_lru_locks osc
2736         sync; sync_all_data || true
2737
2738         echo "Verify space usage is increased"
2739         USED=$(getquota -u $TSTID global curspace)
2740         [ $USED -gt $ORIG_USR_SPACE ] ||
2741                 error "Used space for user $TSTID isn't increased" \
2742                         "orig:$ORIG_USR_SPACE, now:$USED"
2743         USED=$(getquota -g $TSTID global curspace)
2744         [ $USED -gt $ORIG_GRP_SPACE ] ||
2745                 error "Used space for group $TSTID isn't increased" \
2746                         "orig:$ORIG_GRP_SPACE, now:$USED"
2747         if [ $project_supported == "yes" ]; then
2748                 USED=$(getquota -p $TSTPRJID global curspace)
2749                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
2750                         error "Used space for project $TSTPRJID isn't " \
2751                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
2752         fi
2753
2754         cleanup_quota_test
2755 }
2756 run_test 35 "Usage is still accessible across reboot"
2757
2758 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2759 # LU-5006
2760 test_37() {
2761         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
2762                 skip "Old server doesn't have LU-5006 fix."
2763
2764         setup_quota_test || error "setup quota failed with $?"
2765         trap cleanup_quota_test EXIT
2766
2767         # make sure the system is clean
2768         local USED=$(getquota -u $TSTID global curspace)
2769         [ $USED -ne 0 ] &&
2770                 error "Used space ($USED) for user $TSTID isn't 0."
2771
2772         # create file with MDS_OPEN_DELAY_CREATE flag
2773         $SETSTRIPE -c 1 -i 0 $DIR/$tdir/$tfile ||
2774                 error "Create file failed"
2775         # write to file
2776         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2777                 oflag=sync || error "Write file failed"
2778         # chown to the file
2779         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2780
2781         # wait for setattr on objects finished..."
2782         wait_delete_completed
2783
2784         USED=$(getquota -u $TSTID global curspace)
2785         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2786
2787         cleanup_quota_test
2788 }
2789 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2790
2791 # LU-8801
2792 test_38() {
2793         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.60) ] &&
2794                 skip "Old server doesn't have LU-8801 fix."
2795
2796         [ "$UID" != 0 ] && skip_env "must run as root" && return
2797
2798         setup_quota_test || error "setup quota failed with $?"
2799         trap cleanup_quota_test EXIT
2800
2801         # make sure the system is clean
2802         local USED=$(getquota -u $TSTID global curspace)
2803         [ $USED -ne 0 ] &&
2804                 error "Used space ($USED) for user $TSTID isn't 0."
2805         USED=$(getquota -u $TSTID2 global curspace)
2806         [ $USED -ne 0 ] &&
2807                 error "Used space ($USED) for user $TSTID2 isn't 0."
2808
2809         local TESTFILE="$DIR/$tdir/$tfile"
2810         local file_cnt=10000
2811
2812         # Generate id entries in accounting file
2813         echo "Create $file_cnt files..."
2814         for i in `seq $file_cnt`; do
2815                 touch $TESTFILE-$i
2816                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
2817                         error "failed to chown $TESTFILE-$i"
2818         done
2819         cancel_lru_locks osc
2820         sync; sync_all_data || true
2821
2822         local procf="osd-$(facet_fstype $SINGLEMDS).$FSNAME-MDT0000"
2823         procf=${procf}.quota_slave.acct_user
2824         local accnt_cnt
2825
2826         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
2827                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
2828         echo "Found $acct_cnt id entries"
2829
2830         [ $file_cnt -eq $acct_cnt ] || {
2831                 do_facet mds1 $LCTL get_param $procf
2832                 error "skipped id entries"
2833         }
2834
2835         cleanup_quota_test
2836 }
2837 run_test 38 "Quota accounting iterator doesn't skip id entries"
2838
2839 test_39() {
2840         local TESTFILE="$DIR/$tdir/project"
2841         ! is_project_quota_supported &&
2842                 skip "Project quota is not supported"
2843
2844         setup_quota_test || error "setup quota failed with $?"
2845
2846         touch $TESTFILE
2847         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2848         [ $projectid -ne 0 ] &&
2849                 error "Project id should be 0 not $projectid"
2850         change_project -p 1024 $TESTFILE
2851         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2852         [ $projectid -ne 1024 ] &&
2853                 error "Project id should be 1024 not $projectid"
2854
2855         stopall || error "failed to stopall (1)"
2856         mount
2857         setupall
2858         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2859         [ $projectid -ne 1024 ] &&
2860                 error "Project id should be 1024 not $projectid"
2861
2862         cleanup_quota_test
2863 }
2864 run_test 39 "Project ID interface works correctly"
2865
2866 test_40a() {
2867         ! is_project_quota_supported &&
2868                 skip "Project quota is not supported"
2869         local dir1="$DIR/$tdir/dir1"
2870         local dir2="$DIR/$tdir/dir2"
2871
2872         setup_quota_test || error "setup quota failed with $?"
2873
2874         mkdir -p $dir1 $dir2
2875         change_project -sp 1 $dir1 && touch $dir1/1
2876         change_project -sp 2 $dir2
2877
2878         ln $dir1/1 $dir2/1_link &&
2879                 error "Hard link across different project quota should fail"
2880         rm -rf $dir1 $dir2
2881
2882         cleanup_quota_test
2883 }
2884 run_test 40a "Hard link across different project ID"
2885
2886 test_40b() {
2887         ! is_project_quota_supported &&
2888                 skip "Project quota is not supported"
2889         local dir1="$DIR/$tdir/dir1"
2890         local dir2="$DIR/$tdir/dir2"
2891
2892         setup_quota_test || error "setup quota failed with $?"
2893         mkdir -p $dir1 $dir2
2894         change_project -sp 1 $dir1 && touch $dir1/1
2895         change_project -sp 2 $dir2
2896
2897         mv $dir1/1 $dir2/2 || error "mv failed $?"
2898         local projid=$(lfs project $dir2/2 | awk '{print $1}')
2899         if [ "$projid" != "2" ]; then
2900                 error "project id expected 2 not $projid"
2901         fi
2902         rm -rf $dir1 $dir2
2903         cleanup_quota_test
2904 }
2905 run_test 40b "Mv across different project ID"
2906
2907 test_40c() {
2908         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2909                 ! is_project_quota_supported &&
2910                         skip "Project quota is not supported"
2911
2912         setup_quota_test || error "setup quota failed with $?"
2913         local dir="$DIR/$tdir/dir"
2914
2915         mkdir -p $dir && change_project -sp 1 $dir
2916         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
2917         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
2918         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
2919         touch $dir/remote_dir/file
2920         #verify inherit works file for remote dir.
2921         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
2922         [ "$projid" != "1" ] &&
2923                 error "file under remote dir expected 1 not $projid"
2924
2925         #Agent inode should be ignored for project quota
2926         USED=$(getquota -p 1 global curinodes)
2927         [ "$USED" != "3" ] &&
2928                 error "file count expected 3 got $USED"
2929
2930         rm -rf $dir
2931         cleanup_quota_test
2932         return 0
2933 }
2934 run_test 40c "Remote child Dir inherit project quota properly"
2935
2936 test_50() {
2937         ! is_project_quota_supported &&
2938                 skip "Project quota is not supported"
2939
2940         setup_quota_test || error "setup quota failed with $?"
2941         local dir1="$DIR/$tdir/dir1"
2942         local dir2="$DIR/$tdir/dir2"
2943
2944         mkdir -p $dir1 && change_project -sp 1 $dir1
2945         mkdir -p $dir2 && change_project -sp 2 $dir2
2946         for num in $(seq 1 10); do
2947                 touch $dir1/file_$num $dir2/file_$num
2948                 ln -s $dir1/file_$num $dir1/file_$num"_link"
2949                 ln -s $dir2/file_$num $dir2/file_$num"_link"
2950         done
2951
2952         count=$($LFS find --projid 1 $DIR | wc -l)
2953         [ "$count" != 21 ] && error "expected 21 but got $count"
2954
2955         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
2956         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
2957         [ "$count" != 22 ] && error "expected 22 but got $count"
2958
2959         rm -rf $dir1 $dir2
2960         cleanup_quota_test
2961 }
2962 run_test 50 "Test if lfs find --projid works"
2963
2964 test_51() {
2965         ! is_project_quota_supported &&
2966                 skip "Project quota is not supported"
2967         setup_quota_test || error "setup quota failed with $?"
2968         local dir="$DIR/$tdir/dir"
2969
2970         mkdir $dir && change_project -sp 1 $dir
2971         local used=$(getquota -p 1 global curinodes)
2972         [ $used != "1" ] && error "expected 1 got $used"
2973
2974         touch $dir/1
2975         touch $dir/2
2976         cp $dir/2 $dir/3
2977         used=$(getquota -p 1 global curinodes)
2978         [ $used != "4" ] && error "expected 4 got $used"
2979
2980         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
2981         #try cp to dir
2982         cp $DIR/$tdir/6 $dir/6
2983         used=$(getquota -p 1 global curinodes)
2984         [ $used != "5" ] && error "expected 5 got $used"
2985
2986         #try mv to dir
2987         mv $DIR/$tdir/6 $dir/7
2988         used=$(getquota -p 1 global curinodes)
2989         [ $used != "6" ] && error "expected 6 got $used"
2990
2991         rm -rf $dir
2992         cleanup_quota_test
2993 }
2994 run_test 51 "Test project accounting with mv/cp"
2995
2996 test_52() {
2997         ! is_project_quota_supported &&
2998                 skip "Project quota is not supported"
2999         setup_quota_test || error "setup quota failed with $?"
3000         local dir="$DIR/$tdir/dir"
3001         mkdir $dir && change_project -sp 1 $dir
3002
3003         touch $DIR/$tdir/file
3004         #Try renaming a file into the project.  This should fail.
3005         for num in $(seq 1 2000); do
3006                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
3007                         error "rename should fail"
3008         done
3009         rm -rf $dir
3010         cleanup_quota_test
3011 }
3012 run_test 52 "Rename across different project ID"
3013
3014 test_53() {
3015         ! is_project_quota_supported &&
3016                 skip "Project quota is not supported"
3017         setup_quota_test || error "setup quota failed with $?"
3018         local dir="$DIR/$tdir/dir"
3019         mkdir $dir && change_project -s $dir
3020         lfs project -d $dir | grep P || error "inherit attribute should be set"
3021
3022         change_project -C $dir
3023         lfs project -d $dir | grep P &&
3024                 error "inherit attribute should be cleared"
3025
3026         rm -rf $dir
3027         cleanup_quota_test
3028 }
3029 run_test 53 "Project inherit attribute could be cleared"
3030
3031 test_54() {
3032         ! is_project_quota_supported &&
3033                 skip "Project quota is not supported"
3034         setup_quota_test || error "setup quota failed with $?"
3035         trap cleanup_quota_test EXIT
3036         local testfile="$DIR/$tdir/$tfile-0"
3037
3038         #set project ID/inherit attribute
3039         change_project -sp $TSTPRJID $DIR/$tdir
3040         $RUNAS createmany -m ${testfile} 100 ||
3041                 error "create many files failed"
3042
3043         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
3044         # one more count for directory itself */
3045         ((proj_count++))
3046
3047         #check project
3048         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3049         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
3050
3051         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
3052         [ $proj_count1 -eq $proj_count ] ||
3053                         error "c2: expected $proj_count got $proj_count1"
3054
3055         #clear project but with kept projid
3056         change_project -rCk $DIR/$tdir
3057         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3058         [ $proj_count1 -eq 1 ] ||
3059                         error "c3: expected 1 got $proj_count1"
3060
3061         #verify projid untouched.
3062         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3063         ((proj_count1++))
3064         [ $proj_count1 -eq $proj_count ] ||
3065                         error "c4: expected $proj_count got $proj_count1"
3066
3067         # test -0 option
3068         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
3069         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3070         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
3071
3072         #this time clear all
3073         change_project -rC $DIR/$tdir
3074         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3075         [ $proj_count1 -eq 0 ] ||
3076                         error "c6: expected 0 got $proj_count1"
3077         #cleanup
3078         unlinkmany ${testfile} 100 ||
3079                 error "unlink many files failed"
3080
3081         cleanup_quota_test
3082 }
3083 run_test 54 "basic lfs project interface test"
3084
3085 test_55() {
3086         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
3087                 skip "Not supported before 2.10.58."
3088         setup_quota_test || error "setup quota failed with $?"
3089
3090         set_ost_qtype $QTYPE || error "enable ost quota failed"
3091         quota_init
3092
3093         #add second group to TSTUSR
3094         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
3095
3096         #prepare test file
3097         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3098         error "failed to dd"
3099
3100         cancel_lru_locks osc
3101         sync; sync_all_data || true
3102
3103         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3104         error "failed to setquota on group $TSTUSR2"
3105
3106         $LFS quota -v -g $TSTUSR2 $DIR
3107
3108         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3109         error "chgrp should failed with -EDQUOT"
3110
3111         USED=$(getquota -g $TSTUSR2 global curspace)
3112         echo "$USED"
3113
3114         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3115         error "failed to setquota on group $TSTUSR2"
3116
3117         $LFS quota -v -g $TSTUSR2 $DIR
3118
3119         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3120         error "chgrp should succeed"
3121
3122         $LFS quota -v -g $TSTUSR2 $DIR
3123
3124         resetquota -g $TSTUSR2
3125         cleanup_quota_test
3126 }
3127 run_test 55 "Chgrp should be affected by group quota"
3128
3129 test_56() {
3130         setup_quota_test || error "setup quota failed with $?"
3131
3132         set_ost_qtype $QTYPE || error "enable ost quota failed"
3133         quota_init
3134
3135         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3136                 erro "failed to set grace time for usr quota"
3137         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3138                      awk '{print $4 $8}')
3139         if [ "x$grace_time" != "x10s;10s" ]; then
3140                 $LFS quota -t -u $DIR
3141                 error "expected grace time: 10s;10s, got:$grace_time"
3142         fi
3143
3144         cleanup_quota_test
3145 }
3146 run_test 56 "lfs quota -t should work well"
3147
3148 test_57() {
3149         setup_quota_test || error "setup quota failed with $?"
3150
3151         local dir="$DIR/$tdir/dir"
3152         mkdir -p $dir
3153         mkfifo $dir/pipe
3154         #try to change pipe file should not hang and return failure
3155         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3156                 awk -F ':' '{ print \\\$2 }'" \
3157                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3158         #command can process further if it hit some errors
3159         touch $dir/aaa $dir/bbb
3160         mkdir $dir/subdir -p
3161         touch $dir/subdir/aaa $dir/subdir/bbb
3162         #create one invalid link file
3163         ln -s $dir/not_exist_file $dir/ccc
3164         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3165         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3166
3167         cleanup_quota_test
3168 }
3169 run_test 57 "lfs project could tolerate errors"
3170
3171 test_59() {
3172         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
3173                 skip "ldiskfs only test"
3174         disable_project_quota
3175         setup_quota_test || error "setup quota failed with $?"
3176         quota_init
3177
3178         local testfile="$DIR/$tdir/$tfile-0"
3179         #make sure it did not crash kernel
3180         touch $testfile && lfs project -sp 1 $testfile
3181
3182         enable_project_quota
3183         cleanup_quota_test
3184 }
3185 run_test 59 "lfs project dosen't crash kernel with project disabled"
3186
3187 test_60() {
3188         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3189                 skip "Needs MDS version 2.11.53 or later."
3190         setup_quota_test || error "setup quota failed with $?"
3191         trap cleanup_quota_test EXIT
3192
3193         local testfile=$DIR/$tdir/$tfile
3194         local limit=100
3195
3196         set_mdt_qtype "ug" || error "enable mdt quota failed"
3197
3198         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3199                 error "set quota failed"
3200         quota_show_check a g $TSTUSR
3201
3202         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3203         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3204         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3205                 error "create many files failed"
3206
3207         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3208
3209         # root user can overrun quota
3210         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3211                 error "root user should succeed"
3212
3213         cleanup_quota_test
3214         resetquota -g $TSTUSR
3215 }
3216 run_test 60 "Test quota for root with setgid"
3217
3218 # test default quota
3219 test_default_quota() {
3220         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.51) ] &&
3221                 skip "Not supported before 2.11.51."
3222
3223         local qtype=$1
3224         local qpool=$2
3225         local qid=$TSTUSR
3226         local qprjid=$TSTPRJID
3227         local qdtype="-U"
3228         local qs="-b"
3229         local qh="-B"
3230         local LIMIT=20480 #20M disk space
3231         local TESTFILE="$DIR/$tdir/$tfile-0"
3232
3233         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3234                 echo "Project quota is not supported" && return 0
3235
3236         [ $qtype == "-u" ] && qdtype="-U"
3237         [ $qtype == "-g" ] && qdtype="-G"
3238         [ $qtype == "-p" ] && {
3239                 qdtype="-P"
3240                 qid=$qprjid
3241         }
3242
3243         [ $qpool == "meta" ] && {
3244                 LIMIT=10240 #10K inodes
3245                 qs="-i"
3246                 qh="-I"
3247         }
3248
3249         setup_quota_test || error "setup quota failed with $?"
3250         trap cleanup_quota_test EXIT
3251
3252         quota_init
3253
3254         # enable mdt/ost quota
3255         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3256         set_ost_qtype $QTYPE || error "enable ost quota failed"
3257
3258         log "set to use default quota"
3259         $LFS setquota $qtype $qid -d $DIR ||
3260                 error "set $qid to use default quota failed"
3261
3262         log "set default quota"
3263         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3264                 error "set $qid default quota failed"
3265
3266         log "get default quota"
3267         $LFS quota $qdtype $DIR || error "get default quota failed"
3268
3269         if [ $qpool == "data" ]; then
3270                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3271                                                         awk '{print $2}')
3272                 [ $SLIMIT -eq $LIMIT ] ||
3273                         error "the returned default quota is wrong"
3274         else
3275                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3276                                                         awk '{print $5}')
3277                 [ $SLIMIT -eq $LIMIT ] ||
3278                         error "the returned default quota is wrong"
3279         fi
3280
3281         # make sure the system is clean
3282         local USED=$(getquota $qtype $qid global curspace)
3283         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3284
3285         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3286         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3287
3288         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3289
3290         log "Test not out of quota"
3291         if [ $qpool == "data" ]; then
3292                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3293                         quota_error $qtype $qid "write failed, expect succeed"
3294         else
3295                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3296                         quota_error $qtype $qid "create failed, expect succeed"
3297
3298                 unlinkmany $TESTFILE $((LIMIT/2))
3299         fi
3300
3301         log "Test out of quota"
3302         # flush cache, ensure noquota flag is set on client
3303         cancel_lru_locks osc
3304         cancel_lru_locks mdc
3305         sync; sync_all_data || true
3306         if [ $qpool == "data" ]; then
3307                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3308                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3309         else
3310                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3311                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3312
3313                 unlinkmany $TESTFILE $((LIMIT*2))
3314         fi
3315
3316         log "Increase default quota"
3317         # increase default quota
3318         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3319                 error "set default quota failed"
3320
3321         cancel_lru_locks osc
3322         cancel_lru_locks mdc
3323         sync; sync_all_data || true
3324         if [ $qpool == "data" ]; then
3325                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3326                         quota_error $qtype $qid "write failed, expect succeed"
3327         else
3328                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3329                         quota_error $qtype $qid "create failed, expect succeed"
3330
3331                 unlinkmany $TESTFILE $((LIMIT*2))
3332         fi
3333
3334         log "Set quota to override default quota"
3335         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3336                 error "set $qid quota failed"
3337
3338         cancel_lru_locks osc
3339         cancel_lru_locks mdc
3340         sync; sync_all_data || true
3341         if [ $qpool == "data" ]; then
3342                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3343                         quota_error $qtype $qid "write succeed, expect EQUOT"
3344         else
3345                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3346                         quota_error $qtype $qid "create succeed, expect EQUOT"
3347
3348                 unlinkmany $TESTFILE $((LIMIT*2))
3349         fi
3350
3351         log "Set to use default quota again"
3352         $LFS setquota $qtype $qid -d $DIR ||
3353                 error "set $qid to use default quota failed"
3354
3355         cancel_lru_locks osc
3356         cancel_lru_locks mdc
3357         sync; sync_all_data || true
3358         if [ $qpool == "data" ]; then
3359                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3360                         quota_error $qtype $qid "write failed, expect succeed"
3361         else
3362                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3363                         quota_error $qtype $qid "create failed, expect succeed"
3364
3365                 unlinkmany $TESTFILE $((LIMIT*2))
3366         fi
3367
3368         log "Cleanup"
3369         rm -f $TESTFILE
3370         wait_delete_completed || error "wait_delete_completed failed"
3371         sync_all_data || true
3372         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3373                 error "reset default quota failed"
3374         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3375                 error "reset quota failed"
3376
3377         cleanup_quota_test
3378 }
3379
3380 test_61() {
3381         test_default_quota "-u" "data"
3382         test_default_quota "-u" "meta"
3383         test_default_quota "-g" "data"
3384         test_default_quota "-g" "meta"
3385         test_default_quota "-p" "data"
3386         test_default_quota "-p" "meta"
3387 }
3388 run_test 61 "default quota tests"
3389
3390 test_62() {
3391         ! is_project_quota_supported &&
3392                 skip "Project quota is not supported"
3393          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
3394                 skip "chattr did not support project quota"
3395         setup_quota_test || error "setup quota failed with $?"
3396         local testdir=$DIR/$tdir/
3397
3398         $RUNAS mkdir -p $testdir || error "failed to mkdir"
3399         change_project -s $testdir
3400         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3401                 error "inherit attribute should be set"
3402         # chattr used FS_IOC_SETFLAGS ioctl
3403         $RUNAS chattr -P $testdir &&
3404                 error "regular user clear inherit should fail"
3405         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3406                 error "inherit attribute should still be set"
3407         chattr -P $testdir || error "root failed to clear inherit"
3408         [[ $($LFS project -d $testdir) =~ "P" ]] &&
3409                 error "inherit attribute should be cleared"
3410         cleanup_quota_test
3411 }
3412 run_test 62 "Project inherit should be only changed by root"
3413
3414 test_dom() {
3415         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.55) ] &&
3416                 skip "Not supported before 2.11.55" && return
3417
3418         local qtype=$1
3419         local qid=$TSTUSR
3420         local dd_failed=false
3421         local tdir_dom=${tdir}_dom
3422         local LIMIT=20480 #20M
3423
3424         [ $qtype == "p" ] && ! is_project_quota_supported &&
3425                 echo "Project quota is not supported" && return 0
3426
3427         [ $qtype == "p" ] && qid=$TSTPRJID
3428
3429         setup_quota_test || error "setup quota failed with $?"
3430         trap cleanup_quota_test EXIT
3431
3432         quota_init
3433
3434         # enable mdt/ost quota
3435         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3436         set_ost_qtype $QTYPE || error "enable ost quota failed"
3437
3438         # make sure the system is clean
3439         local USED=$(getquota -$qtype $qid global curspace)
3440         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3441
3442         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
3443
3444         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
3445         $SETSTRIPE -E 1M -L mdt $DIR/$tdir_dom ||
3446                 error "setstripe $tdir_dom failed"
3447         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
3448
3449         [ $qtype == "p" ] && {
3450                 change_project -sp $TSTPRJID $DIR/$tdir
3451                 change_project -sp $TSTPRJID $DIR/$tdir_dom
3452         }
3453
3454         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
3455                 error "set $qid quota failed"
3456
3457         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3458                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3459                                                                 dd_failed=true
3460         done
3461
3462         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3463
3464         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
3465                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3466                                                                 dd_failed=true
3467         done
3468
3469         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
3470
3471         rm -f $DIR/$tdir_dom/*
3472
3473         # flush cache, ensure noquota flag is set on client
3474         cancel_lru_locks osc
3475         cancel_lru_locks mdc
3476         sync; sync_all_data || true
3477
3478         dd_failed=false
3479
3480         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
3481                 quota_error $qtype $qid "write failed, expect succeed"
3482
3483         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
3484                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3485                                                                 dd_failed=true
3486         done
3487
3488         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
3489
3490         rm -f $DIR/$tdir/*
3491         rm -f $DIR/$tdir_dom/*
3492
3493         # flush cache, ensure noquota flag is set on client
3494         cancel_lru_locks osc
3495         cancel_lru_locks mdc
3496         sync; sync_all_data || true
3497
3498         dd_failed=false
3499
3500         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3501                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3502                                                                 dd_failed=true
3503         done
3504
3505         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3506
3507         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
3508                 quota_error $qtype $qid "write succeed, expect EDQUOT"
3509
3510         rm -f $DIR/$tdir/*
3511         rm -fr $DIR/$tdir_dom
3512
3513         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
3514                 error "reset usr quota failed"
3515
3516         cleanup_quota_test
3517 }
3518
3519 test_63() {
3520         test_dom "u"
3521         test_dom "g"
3522         test_dom "p"
3523 }
3524 run_test 63 "quota on DoM tests"
3525
3526 test_64() {
3527         ! is_project_quota_supported &&
3528                 skip "Project quota is not supported"
3529         setup_quota_test || error "setup quota failed with $?"
3530         local dir1="$DIR/$tdir/"
3531
3532         touch $dir1/file
3533         ln -s $dir1/file $dir1/file_link
3534
3535         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
3536                 error "set symlink file's project should fail"
3537
3538         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
3539                 error "get symlink file's project should fail"
3540
3541         cleanup_quota_test
3542 }
3543 run_test 64 "lfs project on symlink files should fail"
3544
3545 test_66() {
3546         ! is_project_quota_supported &&
3547                 skip "Project quota is not supported"
3548         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
3549                 skip "Not supported before 2.12.4"
3550         setup_quota_test || error "setup quota failed with $?"
3551         stack_trap cleanup_quota_test EXIT
3552         local old=$(do_facet mds1 $LCTL get_param -n \
3553                     mdt.*.enable_chprojid_gid | head -1)
3554         local testdir=$DIR/$tdir/foo
3555
3556         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
3557         stack_trap "do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0" \
3558                 EXIT
3559
3560         test_mkdir -i 0 -c 1 $testdir || error "failed to mkdir"
3561         chown -R $TSTID:$TSTID $testdir
3562         change_project -sp $TSTPRJID $testdir
3563         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
3564
3565         $RUNAS lfs project -p 0 $testdir/foo &&
3566                 error "nonroot user should fail to set projid"
3567
3568         $RUNAS lfs project -C $testdir/foo &&
3569                 error "nonroot user should fail to clear projid"
3570
3571         change_project -C $testdir/foo || error "failed to clear project"
3572
3573         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
3574         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
3575         "failed to set projid with normal user when enable_chprojid_gid=-1"
3576
3577         $RUNAS lfs project -rC $testdir/ || error \
3578 "failed to clear project state with normal user when enable_chprojid_gid=-1"
3579
3580         touch $testdir/bar || error "failed touch $testdir/bar"
3581         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
3582         "normal user should not be able to set projid on root owned file"
3583
3584         change_project -p $TSTPRJID $testdir/bar || error \
3585                 "root should be able to change its own file's projid"
3586
3587         cleanup_quota_test
3588 }
3589 run_test 66 "nonroot user can not change project state in default"
3590
3591 quota_fini()
3592 {
3593         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
3594         disable_project_quota
3595 }
3596 reset_quota_settings
3597 quota_fini
3598
3599 cd $ORIG_PWD
3600 complete $SECONDS
3601 check_and_cleanup_lustre
3602 export QUOTA_AUTO=$QUOTA_AUTO_OLD
3603 exit_status