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