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