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