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