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