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