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