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