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