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