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