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