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