Whamcloud - gitweb
LU-17160 build: Use PyConfig_InitPythonConfig 3.11 and later
[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:-"$(id -u $TSTUSR)"}
34 TSTID2=${TSTID2:-"$(id -u $TSTUSR2)"}
35 TSTPRJID=${TSTPRJID:-1000}
36 BLK_SZ=1024
37 MAX_DQ_TIME=604800
38 MAX_IQ_TIME=604800
39 QTYPE="ugp"
40 VERSION_WITH_QP="2.13.53"
41 mds_supports_qp() {
42         [ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ] &&
43                 skip "Needs MDS version $VERSION_WITH_QP or later."
44 }
45
46 require_dsh_mds || exit 0
47 require_dsh_ost || exit 0
48
49 # Does e2fsprogs support quota feature?
50 if [ "$mds1_FSTYPE" == ldiskfs ] &&
51         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
52                 grep -q 'quota'"; then
53         skip_env "e2fsprogs doesn't support quota"
54 fi
55
56 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
57
58 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
59
60 DIR=${DIR:-$MOUNT}
61 DIR2=${DIR2:-$MOUNT2}
62
63 QUOTA_AUTO_OLD=$QUOTA_AUTO
64 export QUOTA_AUTO=0
65
66 check_and_setup_lustre
67
68 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
69 is_project_quota_supported || echo "project quota not supported/enabled"
70
71 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
72 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
73 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
74 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
75 SHOW_QUOTA_PROJID="eval is_project_quota_supported && $LFS quota -v -p $TSTPRJID $DIR"
76 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
77 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
78 SHOW_QUOTA_INFO_PROJID="eval is_project_quota_supported && $LFS quota -t -p $DIR"
79
80 lustre_fail() {
81         local fail_node=$1
82         local fail_loc=$2
83         local fail_val=${3:-0}
84         local NODES=
85
86         case $fail_node in
87         mds_ost|mdt_ost) NODES="$(comma_list $(mdts_nodes) $(osts_nodes))";;
88         mds|mdt) NODES="$(comma_list $(mdts_nodes))";;
89         ost) NODES="$(comma_list $(osts_nodes))";;
90         esac
91
92         do_nodes $NODES "lctl set_param fail_val=$fail_val fail_loc=$fail_loc"
93 }
94
95 change_project()
96 {
97         echo "lfs project $*"
98         lfs project $* || error "lfs project $* failed"
99 }
100
101 RUNAS="runas -u $TSTID -g $TSTID"
102 RUNAS2="runas -u $TSTID2 -g $TSTID2"
103 DD="dd if=/dev/zero bs=1M"
104
105 FAIL_ON_ERROR=false
106
107 # clear quota limits for a user or a group
108 # usage: resetquota -u username
109 #        resetquota -g groupname
110 #        resetquota -p projid
111
112 resetquota_one() {
113         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
114                 error "clear quota for [type:$1 name:$2] failed"
115 }
116
117 resetquota() {
118         (( "$#" == 2 )) || error "resetquota: wrong number of arguments: '$*'"
119         [[ "$1" == "-u" || "$1" == "-g" || "$1" == "-p" ]] ||
120                 error "resetquota: wrong quota type '$1' passed"
121
122         resetquota_one "$1" "$2"
123
124         # give a chance to slave to release space
125         sleep 1
126 }
127
128 quota_scan() {
129         local local_ugp=$1
130         local local_id=$2
131         local count
132
133         if [ "$local_ugp" == "a" -o "$local_ugp" == "u" ]; then
134                 $LFS quota -v -u $local_id $DIR
135                 count=$($LFS find --user $local_id $DIR | wc -l)
136                 log "Files for user ($local_id), count=$count:"
137                 ($LFS find --user $local_id $DIR | head -n 4 |
138                         xargs stat 2>/dev/null)
139         fi
140
141         if [ "$local_ugp" == "a" -o "$local_ugp" == "g" ]; then
142                 $LFS quota -v -g $local_id $DIR
143                 count=$($LFS find --group $local_id $DIR | wc -l)
144                 log "Files for group ($local_id), count=$count:"
145                 ($LFS find --group $local_id $DIR | head -n 4 |
146                         xargs stat 2>/dev/null)
147         fi
148
149         is_project_quota_supported || return 0
150         if [ "$local_ugp" == "a" -o "$local_ugp" == "p" ]; then
151                 $LFS quota -v -p $TSTPRJID $DIR
152                 count=$($LFS find --projid $TSTPRJID $DIR | wc -l)
153                 log "Files for project ($TSTPRJID), count=$count:"
154                 ($LFS find --projid $TSTPRJID $DIR | head -n 4 |
155                         xargs stat 2>/dev/null)
156         fi
157 }
158
159 quota_error() {
160         quota_scan $1 $2
161         shift 2
162         error "$*"
163 }
164
165 quota_log() {
166         quota_scan $1 $2
167         shift 2
168         log "$*"
169 }
170
171 # get quota for a user or a group
172 # usage: getquota -u|-g|-p <username>|<groupname>|<projid> global|<obd_uuid> \
173 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace \
174 #                 <pool_name>
175 getquota() {
176         local spec
177         local uuid
178         local pool_arg
179
180         sync_all_data > /dev/null 2>&1 || true
181
182         [ "$#" != 4 -a "$#" != 5 ] &&
183                 error "getquota: wrong number of arguments: $#"
184         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
185                 error "getquota: wrong u/g/p specifier $1 passed"
186
187         uuid="$3"
188
189         case "$4" in
190                 curspace)   spec=1;;
191                 bsoftlimit) spec=2;;
192                 bhardlimit) spec=3;;
193                 bgrace)     spec=4;;
194                 curinodes)  spec=5;;
195                 isoftlimit) spec=6;;
196                 ihardlimit) spec=7;;
197                 igrace)     spec=8;;
198                 *)          error "unknown quota parameter $4";;
199         esac
200
201         [ ! -z "$5" ] && pool_arg="--pool $5 "
202         [ "$uuid" = "global" ] && uuid=$DIR
203
204         $LFS quota -v "$1" "$2" $pool_arg $DIR |
205                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
206                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
207                 | tr -d "*"
208 }
209
210 # set mdt quota type
211 # usage: set_mdt_qtype ugp|u|g|p|none
212 set_mdt_qtype() {
213         local qtype=$1
214         local varsvc
215         local mdts=$(get_facets MDS)
216         local cmd
217         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
218                 qtype=$(tr -d 'p' <<<$qtype)
219
220         if [[ $PERM_CMD == *"set_param -P"* ]]; then
221                 do_facet mgs $PERM_CMD \
222                         osd-*.$FSNAME-MDT*.quota_slave.enabled=$qtype
223         else
224                 do_facet mgs $PERM_CMD $FSNAME.quota.mdt=$qtype
225         fi
226         # we have to make sure each MDT received config changes
227         for mdt in ${mdts//,/ }; do
228                 varsvc=${mdt}_svc
229                 cmd="$LCTL get_param -n "
230                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
231                 cmd=${cmd}.quota_slave.enabled
232
233                 if $(facet_up $mdt); then
234                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
235                 fi
236         done
237         return 0
238 }
239
240 # set ost quota type
241 # usage: set_ost_qtype ugp|u|g|p|none
242 set_ost_qtype() {
243         local qtype=$1
244         local varsvc
245         local osts=$(get_facets OST)
246         local cmd
247         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
248                 qtype=$(tr -d 'p' <<<$qtype)
249
250         if [[ $PERM_CMD == *"set_param -P"* ]]; then
251                 do_facet mgs $PERM_CMD \
252                         osd-*.$FSNAME-OST*.quota_slave.enabled=$qtype
253         else
254                 do_facet mgs $PERM_CMD $FSNAME.quota.ost=$qtype
255         fi
256         # we have to make sure each OST received config changes
257         for ost in ${osts//,/ }; do
258                 varsvc=${ost}_svc
259                 cmd="$LCTL get_param -n "
260                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
261                 cmd=${cmd}.quota_slave.enabled
262
263                 if $(facet_up $ost); then
264                         wait_update_facet $ost "$cmd" "$qtype" || return 1
265                 fi
266         done
267         return 0
268 }
269
270 wait_reintegration() {
271         local ntype=$1
272         local qtype=$2
273         local max=$3
274         local result="glb[1],slv[1],reint[0]"
275         local varsvc
276         local cmd
277         local tgts
278
279         if [ $ntype == "mdt" ]; then
280                 tgts=$(get_facets MDS)
281         else
282                 tgts=$(get_facets OST)
283         fi
284
285         for tgt in ${tgts//,/ }; do
286                 varsvc=${tgt}_svc
287                 cmd="$LCTL get_param -n "
288                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
289                 cmd=${cmd}.quota_slave.info
290
291                 if $(facet_up $tgt); then
292                         # reintegration starts after recovery completion
293                         wait_recovery_complete $tgt
294                         wait_update_facet $tgt "$cmd |
295                                 grep "$qtype" | awk '{ print \\\$3 }'" \
296                                         "$result" $max || return 1
297                 fi
298         done
299         return 0
300 }
301
302 wait_mdt_reint() {
303         local qtype=$1
304         local max=${2:-90}
305
306         if [[ "$qtype" =~ "u" ]]; then
307                 wait_reintegration "mdt" "user" $max || return 1
308         fi
309
310         if [[ "$qtype" =~ "g" ]]; then
311                 wait_reintegration "mdt" "group" $max || return 1
312         fi
313
314         if [[ "$qtype" =~ "p" ]]; then
315                 ! is_project_quota_supported && return 0
316                 wait_reintegration "mdt" "project" $max || return 1
317         fi
318         return 0
319 }
320
321 wait_ost_reint() {
322         local qtype=$1
323         local max=${2:-90}
324
325         if [[ "$qtype" =~ "u" ]]; then
326                 wait_reintegration "ost" "user" $max || return 1
327         fi
328
329         if [[ "$qtype" =~ "g" ]]; then
330                 wait_reintegration "ost" "group" $max || return 1
331         fi
332
333         if [[ "$qtype" =~ "p" ]]; then
334                 ! is_project_quota_supported && return 0
335                 wait_reintegration "ost" "project" $max || return 1
336         fi
337         return 0
338 }
339
340 wait_grace_time() {
341         local qtype=$1
342         local flavour=$2
343         local pool=${3:-}
344         local extrasleep=${4:-5}
345         local qarg
346         local parg
347
348         case $qtype in
349                 u|g) qarg=$TSTUSR ;;
350                 p) qarg=$TSTPRJID ;;
351                 *) error "get_grace_time: Invalid quota type: $qtype"
352         esac
353
354         if [ $pool ]; then
355                 parg="--pool "$pool
356                 echo "Quota info for $pool:"
357                 $LFS quota -$qtype $qarg $parg $DIR
358         fi
359
360         case $flavour in
361                 block)
362                         time=$(lfs quota -$qtype $qarg $parg $DIR|
363                                    awk 'NR == 3{ print $5 }')
364                         ;;
365                 file)
366                         time=$(lfs quota -$qtype $qarg $DIR|
367                                    awk 'NR == 3{ print $9 }')
368                         ;;
369                 *)
370                         error "Unknown quota type: $flavour"
371                         ;;
372         esac
373
374         local sleep_seconds=0
375         local orig_time=$time
376
377         echo "Grace time is $time"
378         # from lfs.c:__sec2str()
379         # const char spec[] = "smhdw";
380         # {1, 60, 60*60, 24*60*60, 7*24*60*60};
381         [[ $time == *w* ]] && w_time=${time%w*} &&
382                 let sleep_seconds+=$((w_time*7*24*60*60));
383         time=${time#*w}
384         [[ $time == *d* ]] && d_time=${time%d*} &&
385                 let sleep_seconds+=$((d_time*24*60*60));
386         time=${time#*d}
387         [[ $time == *h* ]] && h_time=${time%h*} &&
388                 let sleep_seconds+=$((h_time*60*60));
389         time=${time#*h}
390         [[ $time == *m* ]] && m_time=${time%m*} &&
391                 let sleep_seconds+=$((m_time*60));
392         time=${time#*m}
393         [[ $time == *s* ]] && s_time=${time%s*} &&
394                 let sleep_seconds+=$s_time
395
396         echo "Sleep through grace ..."
397         [ "$orig_time" == "-" ] &&
398             error "Grace timeout was not set or quota not exceeded"
399         if [ "$orig_time" == "expired" -o "$orig_time" == "none" ]; then
400             echo "...Grace timeout already expired"
401         else
402                 let sleep_seconds+=$extrasleep
403                 echo "...sleep $sleep_seconds seconds"
404                 sleep $sleep_seconds
405         fi
406 }
407
408 setup_quota_test() {
409         wait_delete_completed
410         echo "Creating test directory"
411         mkdir_on_mdt0 $DIR/$tdir || return 1
412         chmod 0777 $DIR/$tdir || return 2
413         # always clear fail_loc in case of fail_loc isn't cleared
414         # properly when previous test failed
415         lustre_fail mds_ost 0
416         stack_trap cleanup_quota_test EXIT
417 }
418
419 cleanup_quota_test() {
420         echo "Delete files..."
421         rm -rf $DIR/$tdir
422         [ -d $DIR/${tdir}_dom ] && rm -rf $DIR/${tdir}_dom
423         echo "Wait for unlink objects finished..."
424         wait_delete_completed
425         sync_all_data || true
426         reset_quota_settings
427 }
428
429 quota_show_check() {
430         local bf=$1
431         local ugp=$2
432         local qid=$3
433         local usage
434
435         $LFS quota -v -$ugp $qid $DIR
436
437         if [ "$bf" == "a" -o "$bf" == "b" ]; then
438                 usage=$(getquota -$ugp $qid global curspace)
439                 if [ -z $usage ]; then
440                         quota_error $ugp $qid \
441                                 "Query block quota failed ($ugp:$qid)."
442                 else
443                         [ $usage -ne 0 ] && quota_log $ugp $qid \
444                                 "Block quota isn't 0 ($ugp:$qid:$usage)."
445                 fi
446         fi
447
448         if [ "$bf" == "a" -o "$bf" == "f" ]; then
449                 usage=$(getquota -$ugp $qid global curinodes)
450                 if [ -z $usage ]; then
451                         quota_error $ugp $qid \
452                                 "Query file quota failed ($ugp:$qid)."
453                 else
454                         [ $usage -ne 0 ] && quota_log $ugp $qid \
455                                 "File quota isn't 0 ($ugp:$qid:$usage)."
456                 fi
457         fi
458 }
459
460 project_quota_enabled () {
461         local rc=0
462         local zfeat="feature@project_quota"
463
464         for facet in $(seq -f mds%g $MDSCOUNT) $(seq -f ost%g $OSTCOUNT); do
465                 local facet_fstype=${facet:0:3}1_FSTYPE
466                 local devname
467
468                 if [ "${!facet_fstype}" = "zfs" ]; then
469                         devname=$(zpool_name ${facet})
470                         do_facet ${facet} $ZPOOL get -H "$zfeat" $devname |
471                                 grep -wq active || rc=1
472                 else
473                         [ ${facet:0:3} == "mds" ] &&
474                                 devname=$(mdsdevname ${facet:3}) ||
475                                 devname=$(ostdevname ${facet:3})
476                         do_facet ${facet} $DEBUGFS -R features $devname |
477                                 grep -q project || rc=1
478                 fi
479         done
480         [ $rc -eq 0 ] && PQ_CLEANUP=false || PQ_CLEANUP=true
481         return $rc
482 }
483
484 project_quota_enabled || enable_project_quota
485
486 reset_quota_settings() {
487         resetquota_one -u $TSTUSR
488         [[ $(id -u $TSTUSR) == $TSTID ]] || resetquota_one -u $TSTID
489         resetquota_one -g $TSTUSR
490         [[ $(id -g $TSTUSR) == $TSTID ]] || resetquota_one -g $TSTID
491         resetquota_one -u $TSTUSR2
492         [[ $(id -u $TSTUSR2) == $TSTID2 ]] || resetquota_one -u $TSTID2
493         resetquota_one -g $TSTUSR2
494         [[ $(id -g $TSTUSR2) == $TSTID2 ]] || resetquota_one -g $TSTID2
495         is_project_quota_supported && resetquota_one -p $TSTPRJID
496
497         sleep 1
498 }
499
500 get_quota_on_qsd() {
501         local facet
502         local device
503         local spec
504         local qid
505         local qtype
506         local output
507
508         facet=$1
509         device=$2
510         case "$3" in
511                 usr) qtype="limit_user";;
512                 grp) qtype="limit_group";;
513                 prj) qtype="limit_project";;
514                 *)         error "unknown quota parameter $3";;
515         esac
516
517         qid=$4
518         case "$5" in
519                 hardlimit) spec=4;;
520                 softlimit) spec=6;;
521                 *)         error "unknown quota parameter $5";;
522         esac
523
524         do_facet $facet $LCTL get_param osd-*.*-${device}.quota_slave.$qtype |
525                 awk '($3 == '$qid') {getline; print $'$spec'; exit;}' | tr -d ,
526 }
527
528 wait_quota_synced() {
529         local value
530         local facet=$1
531         local device=$2
532         local qtype=$3
533         local qid=$4
534         local limit_type=$5
535         local limit_val=$6
536         local interval=0
537
538         value=$(get_quota_on_qsd $facet $device $qtype $qid $limit_type)
539         while [[ $value != $limit_val ]]; do
540                 (( interval != 0 )) ||
541                         do_facet $facet $LCTL set_param \
542                                 osd-*.*-${device}.quota_slave.force_reint=1
543
544                 echo $value
545                 (( interval <= 20 )) ||
546                         error "quota ($value) don't update on QSD, $limit_val"
547
548                 interval=$((interval + 1))
549                 sleep 1
550
551                 value=$(get_quota_on_qsd $facet $device $qtype $qid $limit_type)
552         done
553 }
554
555 # enable quota debug
556 quota_init() {
557         do_nodes $(comma_list $(nodes_list)) \
558                 "$LCTL set_param -n debug=+quota+trace"
559 }
560 quota_init
561 reset_quota_settings
562
563 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
564         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
565 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
566         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
567
568 test_quota_performance() {
569         local TESTFILE="$DIR/$tdir/$tfile-0"
570         local size=$1 # in MB
571         local stime=$(date +%s)
572         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
573                 quota_error u $TSTUSR "write failure"
574         local etime=$(date +%s)
575         delta=$((etime - stime))
576         if [ $delta -gt 0 ]; then
577                 rate=$((size * 1024 / delta))
578                 if [ "$mds1_FSTYPE" = zfs ]; then
579                         # LU-2872 - see LU-2887 for fix
580                         [ $rate -gt 64 ] ||
581                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
582                 else
583                         [ $rate -gt 1024 ] ||
584                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
585                 fi
586         fi
587         rm -f $TESTFILE
588 }
589
590 # test basic quota performance b=21696
591 test_0() {
592         local MB=100 # MB
593         [ "$SLOW" = "no" ] && MB=10
594
595         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
596         [ $free_space -le $((MB * 1024)) ] &&
597                 skip "not enough space ${free_space} KB, " \
598                         "required $((MB * 1024)) KB"
599         setup_quota_test || error "setup quota failed with $?"
600
601         set_ost_qtype "none" || error "disable ost quota failed"
602         test_quota_performance $MB
603
604         set_ost_qtype $QTYPE || error "enable ost quota failed"
605         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
606                 error "set quota failed"
607         test_quota_performance $MB
608 }
609 run_test 0 "Test basic quota performance"
610
611 # usage: test_1_check_write tfile user|group|project
612 test_1_check_write() {
613         local testfile="$1"
614         local qtype="$2"
615         local limit=$3
616         local short_qtype=${qtype:0:1}
617
618         log "Write..."
619         $RUNAS $DD of=$testfile count=$((limit/2)) ||
620                 quota_error $short_qtype $TSTUSR \
621                         "$qtype write failure, but expect success"
622         log "Write out of block quota ..."
623         # this time maybe cache write, ignore it's failure
624         $RUNAS $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
625         # flush cache, ensure noquota flag is set on client
626         cancel_lru_locks osc
627         sync; sync_all_data || true
628         # sync means client wrote all it's cache, but id doesn't
629         # guarantee that slave received new edquot through glimpse.
630         # so wait a little to be sure slave got it.
631         sleep 5
632         $RUNAS $DD of=$testfile count=1 seek=$limit &&
633                 quota_error $short_qtype $TSTUSR \
634                         "user write success, but expect EDQUOT"
635         return 0
636 }
637
638 check_write_fallocate() {
639         local testfile="$1"
640         local qtype="$2"
641         local limit=$3
642         local short_qtype=${qtype:0:1}
643
644         count=$((limit/2))
645         log "Write ${count}MiB Using Fallocate"
646         $RUNAS fallocate -l${count}MiB $testfile ||
647                 quota_error $short_qtype $TSTUSR "Write ${count}MiB fail"
648
649         cancel_lru_locks osc
650         sync; sync_all_data || true
651         sleep 2
652
653         count=$((limit + 1))
654         log "Write ${count}MiB Using Fallocate"
655         $RUNAS fallocate -l${count}MiB $testfile &&
656                 quota_error $short_qtype $TSTUSR \
657                 "Write success, expect EDQUOT" || true
658 }
659
660 # test block hardlimit
661 test_1a() {
662         local limit=10 # MB
663         local testfile="$DIR/$tdir/$tfile-0"
664
665         setup_quota_test || error "setup quota failed with $?"
666
667         # enable ost quota
668         set_ost_qtype $QTYPE || error "enable ost quota failed"
669
670         # test for user
671         log "User quota (block hardlimit:$limit MB)"
672         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
673                 error "set user quota failed"
674
675         # make sure the system is clean
676         local used=$(getquota -u $TSTUSR global curspace)
677         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
678
679         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
680         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
681
682         test_1_check_write $testfile "user" $limit
683
684         rm -f $testfile
685         wait_delete_completed || error "wait_delete_completed failed"
686         sync_all_data || true
687         used=$(getquota -u $TSTUSR global curspace)
688         [ $used -ne 0 ] && quota_error u $TSTUSR \
689                 "user quota isn't released after deletion"
690         resetquota -u $TSTUSR
691
692         # test for group
693         log "--------------------------------------"
694         log "Group quota (block hardlimit:$limit MB)"
695         $LFS setquota -g $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
696                 error "set group quota failed"
697
698         testfile="$DIR/$tdir/$tfile-1"
699         # make sure the system is clean
700         used=$(getquota -g $TSTUSR global curspace)
701         [ $used -ne 0 ] && error "Used space ($used) for group $TSTUSR isn't 0"
702
703         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
704         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
705
706         test_1_check_write $testfile "group" $limit
707         rm -f $testfile
708         wait_delete_completed || error "wait_delete_completed failed"
709         sync_all_data || true
710         used=$(getquota -g $TSTUSR global curspace)
711         [ $used -ne 0 ] && quota_error g $TSTUSR \
712                                 "Group quota isn't released after deletion"
713         resetquota -g $TSTUSR
714
715         if ! is_project_quota_supported; then
716                 echo "Project quota is not supported"
717                 return 0
718         fi
719
720         testfile="$DIR/$tdir/$tfile-2"
721         # make sure the system is clean
722         used=$(getquota -p $TSTPRJID global curspace)
723         [ $used -ne 0 ] &&
724                 error "used space($used) for project $TSTPRJID isn't 0"
725
726         # test for Project
727         log "--------------------------------------"
728         log "Project quota (block hardlimit:$limit mb)"
729         $LFS setquota -p $TSTPRJID -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
730                 error "set project quota failed"
731
732         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
733         chown $TSTUSR:$TSTUSR $testfile || error "chown $testfile failed"
734         change_project -p $TSTPRJID $testfile
735
736         test_1_check_write $testfile "project" $limit
737
738         # cleanup
739         cleanup_quota_test
740
741         used=$(getquota -p $TSTPRJID global curspace)
742         [ $used -ne 0 ] && quota_error p $TSTPRJID \
743                 "project quota isn't released after deletion"
744
745         resetquota -p $TSTPRJID
746 }
747 run_test 1a "Block hard limit (normal use and out of quota)"
748
749 test_1b() {
750         (( MDS1_VERSION >= $(version_code 2.15.55) )) ||
751                 skip "Need MDS version at least 2.15.55"
752
753         local limit=10 # MB
754         local global_limit=20 # MB
755         local testfile="$DIR/$tdir/$tfile-0"
756         local qpool="qpool1"
757
758         mds_supports_qp
759         setup_quota_test || error "setup quota failed with $?"
760
761         # enable ost quota
762         set_ost_qtype $QTYPE || error "enable ost quota failed"
763
764         # test for user
765         log "User quota (block hardlimit:$global_limit MB)"
766         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
767                 error "set user quota failed"
768
769         pool_add $qpool || error "pool_add failed"
770         pool_add_targets $qpool 0 $(($OSTCOUNT - 1)) ||
771                 error "pool_add_targets failed"
772
773         # check qmt_pool_add dmesg error
774         local msg_rgx="QMT0000: can't add to $FSNAME-OST0000.*pool.*$qpool"
775         local dmesg_err
776         dmesg_err=$(do_facet mds1 dmesg | grep "$msg_rgx" | tail -1)
777         [[ -z "$dmesg_err" ]] || error "found qmt_pool_add error: $dmesg_err"
778
779         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
780                 error "set user quota failed"
781
782         # make sure the system is clean
783         local used=$(getquota -u $TSTUSR global curspace)
784         echo "used $used"
785         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
786
787         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
788
789         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
790         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
791
792         test_1_check_write $testfile "user" $limit
793
794         rm -f $testfile
795         wait_delete_completed || error "wait_delete_completed failed"
796         sync_all_data || true
797         used=$(getquota -u $TSTUSR global curspace $qpool)
798         [ $used -ne 0 ] && quota_error u $TSTUSR \
799                 "user quota isn't released after deletion"
800         resetquota -u $TSTUSR
801
802         # test for group
803         log "--------------------------------------"
804         log "Group quota (block hardlimit:$global_limit MB)"
805         $LFS setquota -g $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
806                 error "set group quota failed"
807
808         $LFS setquota -g $TSTUSR -b 0 -B ${limit}M --pool $qpool $DIR ||
809                 error "set group quota failed"
810
811         testfile="$DIR/$tdir/$tfile-1"
812         # make sure the system is clean
813         used=$(getquota -g $TSTUSR global curspace $qpool)
814         [ $used -ne 0 ] && error "Used space ($used) for group $TSTUSR isn't 0"
815
816         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
817         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
818
819         test_1_check_write $testfile "group" $limit
820
821         rm -f $testfile
822         wait_delete_completed || error "wait_delete_completed failed"
823         sync_all_data || true
824         used=$(getquota -g $TSTUSR global curspace $qpool)
825         [ $used -ne 0 ] && quota_error g $TSTUSR \
826                                 "Group quota isn't released after deletion"
827         resetquota -g $TSTUSR
828
829         if ! is_project_quota_supported; then
830                 echo "Project quota is not supported"
831                 return 0
832         fi
833
834         testfile="$DIR/$tdir/$tfile-2"
835         # make sure the system is clean
836         used=$(getquota -p $TSTPRJID global curspace $qpool)
837         [ $used -ne 0 ] &&
838                 error "used space($used) for project $TSTPRJID isn't 0"
839
840         # test for Project
841         log "--------------------------------------"
842         log "Project quota (block hardlimit:$global_limit mb)"
843         $LFS setquota -p $TSTPRJID -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
844                 error "set project quota failed"
845
846         $LFS setquota -p $TSTPRJID -b 0 -B ${limit}M --pool $qpool $DIR ||
847                 error "set project quota failed"
848
849
850         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
851         chown $TSTUSR:$TSTUSR $testfile || error "chown $testfile failed"
852         change_project -p $TSTPRJID $testfile
853
854         test_1_check_write $testfile "project" $limit
855
856         # cleanup
857         cleanup_quota_test
858
859         used=$(getquota -p $TSTPRJID global curspace)
860         [ $used -eq 0 ] || quota_error p $TSTPRJID \
861                 "project quota isn't released after deletion"
862 }
863 run_test 1b "Quota pools: Block hard limit (normal use and out of quota)"
864
865 test_1c() {
866         local global_limit=20 # MB
867         local testfile="$DIR/$tdir/$tfile-0"
868         local qpool1="qpool1"
869         local qpool2="qpool2"
870
871         mds_supports_qp
872         setup_quota_test || error "setup quota failed with $?"
873
874         # enable ost quota
875         set_ost_qtype $QTYPE || error "enable ost quota failed"
876
877         # test for user
878         log "User quota (block hardlimit:$global_limit MB)"
879         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
880                 error "set user quota failed"
881
882         pool_add $qpool1 || error "pool_add failed"
883         pool_add_targets $qpool1 0 $(($OSTCOUNT - 1)) ||
884                 error "pool_add_targets failed"
885
886         pool_add $qpool2 || error "pool_add failed"
887         pool_add_targets $qpool2 0 $(($OSTCOUNT - 1)) ||
888                 error "pool_add_targets failed"
889
890         # create pools without hard limit
891         # initially such case raised several bugs
892         $LFS setquota -u $TSTUSR -B 0M --pool $qpool1 $DIR ||
893                 error "set user quota failed"
894
895         $LFS setquota -u $TSTUSR -B 0M --pool $qpool2 $DIR ||
896                 error "set user quota failed"
897
898         # make sure the system is clean
899         local used=$(getquota -u $TSTUSR global curspace)
900         echo "used $used"
901         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
902
903         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
904
905         test_1_check_write $testfile "user" $global_limit
906
907         used=$(getquota -u $TSTUSR global curspace $qpool1)
908         echo "qpool1 used $used"
909         used=$(getquota -u $TSTUSR global curspace $qpool2)
910         echo "qpool2 used $used"
911
912         rm -f $testfile
913         wait_delete_completed || error "wait_delete_completed failed"
914         sync_all_data || true
915
916         used=$(getquota -u $TSTUSR global curspace $qpool1)
917         [ $used -eq 0 ] || quota_error u $TSTUSR \
918                 "user quota isn't released after deletion"
919 }
920 run_test 1c "Quota pools: check 3 pools with hardlimit only for global"
921
922 test_1d() {
923         local limit1=10 # MB
924         local limit2=12 # MB
925         local global_limit=20 # MB
926         local testfile="$DIR/$tdir/$tfile-0"
927         local qpool1="qpool1"
928         local qpool2="qpool2"
929
930         mds_supports_qp
931         setup_quota_test || error "setup quota failed with $?"
932
933         # enable ost quota
934         set_ost_qtype $QTYPE || error "enable ost quota failed"
935
936         # test for user
937         log "User quota (block hardlimit:$global_limit MB)"
938         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
939                 error "set user quota failed"
940
941         pool_add $qpool1 || error "pool_add failed"
942         pool_add_targets $qpool1 0 $(($OSTCOUNT - 1)) ||
943                 error "pool_add_targets failed"
944
945         pool_add $qpool2 || error "pool_add failed"
946         pool_add_targets $qpool2 0 $(($OSTCOUNT - 1)) ||
947                 error "pool_add_targets failed"
948
949         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
950                 error "set user quota failed"
951
952         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
953         error "set user quota failed"
954
955         # make sure the system is clean
956         local used=$(getquota -u $TSTUSR global curspace)
957         echo "used $used"
958         [ $used -ne 0 ] && error "used space($used) for user $TSTUSR isn't 0."
959
960         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
961
962         test_1_check_write $testfile "user" $limit1
963
964         used=$(getquota -u $TSTUSR global curspace $qpool1)
965         echo "qpool1 used $used"
966         used=$(getquota -u $TSTUSR global curspace $qpool2)
967         echo "qpool2 used $used"
968
969         rm -f $testfile
970         wait_delete_completed || error "wait_delete_completed failed"
971         sync_all_data || true
972
973         used=$(getquota -u $TSTUSR global curspace $qpool1)
974         [ $used -eq 0 ] || quota_error u $TSTUSR \
975                 "user quota isn't released after deletion"
976 }
977 run_test 1d "Quota pools: check block hardlimit on different pools"
978
979 test_1e() {
980         local limit1=10 # MB
981         local global_limit=53000000 # MB
982         local testfile="$DIR/$tdir/$tfile-0"
983         local testfile2="$DIR/$tdir/$tfile-1"
984         local qpool1="qpool1"
985
986         mds_supports_qp
987         setup_quota_test || error "setup quota failed with $?"
988
989         # enable ost quota
990         set_ost_qtype $QTYPE || error "enable ost quota failed"
991
992         # global_limit is much greater than limit1 to get
993         # different qunit's on osts. Since 1st qunit shrinking
994         # on OST1(that belongs to qpool1), this qunit should
995         # be sent to OST1.
996         log "User quota (block hardlimit:$global_limit MB)"
997         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
998                 error "set user quota failed"
999
1000         pool_add $qpool1 || error "pool_add failed"
1001         pool_add_targets $qpool1 1 1 ||
1002                 error "pool_add_targets failed"
1003
1004         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
1005                 error "set user quota failed"
1006
1007         # make sure the system is clean
1008         local used=$(getquota -u $TSTUSR global curspace)
1009         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1010
1011         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
1012         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
1013
1014         test_1_check_write $testfile "user" $limit1
1015
1016         $LFS setstripe $testfile2 -c 1 -i 0 ||
1017                 error "setstripe $testfile2 failed"
1018         chown $TSTUSR.$TSTUSR $testfile2 || error "chown $testfile2 failed"
1019         # Now write to file with a stripe on OST0, that doesn't belong to qpool1
1020         log "Write..."
1021         $RUNAS $DD of=$testfile2 count=20 ||
1022                 quota_error u $TSTUSR \
1023                         "$qtype write failure, but expect success"
1024
1025         rm -f $testfile
1026         rm -f $testfile2
1027         wait_delete_completed || error "wait_delete_completed failed"
1028         sync_all_data || true
1029
1030         used=$(getquota -u $TSTUSR global curspace $qpool1)
1031         [ $used -eq 0 ] || quota_error u $TSTUSR \
1032                 "user quota isn't released after deletion"
1033 }
1034 run_test 1e "Quota pools: global pool high block limit vs quota pool with small"
1035
1036 test_1f() {
1037         local global_limit=200 # MB
1038         local limit1=10 # MB
1039         local TESTDIR="$DIR/$tdir/"
1040         local testfile="$TESTDIR/$tfile-0"
1041         local qpool1="qpool1"
1042
1043         mds_supports_qp
1044         setup_quota_test || error "setup quota failed with $?"
1045
1046         # enable ost quota
1047         set_ost_qtype $QTYPE || error "enable ost quota failed"
1048
1049         log "User quota (block hardlimit:$global_limit MB)"
1050         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
1051                 error "set user quota failed"
1052
1053         pool_add $qpool1 || error "pool_add failed"
1054         pool_add_targets $qpool1 0 0 ||
1055                 error "pool_add_targets failed"
1056
1057         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
1058                 error "set user quota failed"
1059
1060         # make sure the system is clean
1061         local used=$(getquota -u $TSTUSR global curspace)
1062         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1063
1064         $LFS setstripe $TESTDIR -c 1 -i 0 || error "setstripe $TESTDIR failed"
1065
1066         test_1_check_write $testfile "user" $limit1
1067
1068         pool_remove_target $qpool1 0
1069         rm -f $testfile
1070         wait_delete_completed || error "wait_delete_completed failed"
1071         sync_all_data || true
1072
1073         pool_add_targets $qpool1 0 0 || error "pool_add_targets failed"
1074         # qunit for appropriate element in lgd array should be set
1075         # correctly(4096). Earlier it was not changed continuing to be 1024.
1076         # This caused write to hung when it hit limit1 - qunit shrinking to 1024
1077         # for qpool1 lqe didn't cause changing qunit for OST0 in gld array
1078         # as it already was 1024. As flag "need_update" for this qunit was
1079         # not set, new qunit wasn't sent to OST0. Thus revoke was not set
1080         # for "qpool1" lqe and it couldn't set EDQUOT despite granted
1081         # became > 10M. QMT returned EINPROGRESS in a loop.
1082         # Check that it doesn't hung anymore.
1083         test_1_check_write $testfile "user" $limit1
1084 }
1085 run_test 1f "Quota pools: correct qunit after removing/adding OST"
1086
1087 test_1g() {
1088         local limit=20 # MB
1089         local global_limit=40 # MB
1090         local testfile="$DIR/$tdir/$tfile-0"
1091         local qpool="qpool1"
1092         local mdmb_param="osc.*.max_dirty_mb"
1093         local max_dirty_mb=$($LCTL get_param -n $mdmb_param | head -1)
1094
1095         mds_supports_qp
1096         setup_quota_test || error "setup quota failed with $?"
1097         $LCTL set_param $mdmb_param=1
1098         stack_trap "$LCTL set_param $mdmb_param=$max_dirty_mb" EXIT
1099
1100         # enable ost quota
1101         set_ost_qtype $QTYPE || error "enable ost quota failed"
1102
1103         # test for user
1104         log "User quota (block hardlimit:$global_limit MB)"
1105         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
1106                 error "set user quota failed"
1107
1108         pool_add $qpool || error "pool_add failed"
1109         pool_add_targets $qpool 0 $(($OSTCOUNT - 1)) ||
1110                 error "pool_add_targets failed"
1111
1112         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
1113                 error "set user quota failed"
1114
1115         # make sure the system is clean
1116         local used=$(getquota -u $TSTUSR global curspace)
1117         echo "used $used"
1118         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1119
1120         $LFS setstripe $testfile -C 200 || error "setstripe $testfile failed"
1121         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
1122
1123         log "Write..."
1124         $RUNAS $DD of=$testfile count=$((limit/2)) ||
1125                 quota_error u $TSTUSR \
1126                         "$qtype write failure, but expect success"
1127         log "Write out of block quota ..."
1128         # this time maybe cache write,  ignore it's failure
1129         $RUNAS $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
1130         # flush cache, ensure noquota flag is set on client
1131         cancel_lru_locks osc
1132         sync; sync_all_data || true
1133         sleep 5
1134         $RUNAS $DD of=$testfile count=$((OSTCOUNT*3)) seek=$limit &&
1135                 quota_error u $TSTUSR \
1136                         "user write success, but expect EDQUOT"
1137
1138         rm -f $testfile
1139         wait_delete_completed || error "wait_delete_completed failed"
1140         sync_all_data || true
1141
1142         used=$(getquota -u $TSTUSR global curspace $qpool)
1143         [ $used -ne 0 ] && quota_error u $TSTUSR \
1144                 "user quota isn't released after deletion"
1145         return 0
1146 }
1147 run_test 1g "Quota pools: Block hard limit with wide striping"
1148
1149 test_1h() {
1150         local limit=10 # MB
1151         local testfile="$DIR/$tdir/$tfile-0"
1152
1153         check_set_fallocate_or_skip
1154
1155         setup_quota_test || error "setup quota failed with $?"
1156
1157         # enable ost quota
1158         set_ost_qtype $QTYPE || error "enable ost quota failed"
1159
1160         # test for user
1161         log "User quota (block hardlimit:$limit MB)"
1162         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
1163                 error "set user quota failed"
1164
1165         # make sure the system is clean
1166         local used=$(getquota -u $TSTUSR global curspace)
1167         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1168
1169         $LFS setstripe $testfile -i 0 -c 1 || error "setstripe $testfile failed"
1170         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
1171
1172         wait_quota_synced ost1 OST0000 usr $TSTID hardlimit $((limit*1024))
1173
1174         check_write_fallocate $testfile "user" $limit
1175
1176         rm -f $testfile
1177         wait_delete_completed || error "wait_delete_completed failed"
1178         sync_all_data || true
1179         used=$(getquota -u $TSTUSR global curspace)
1180         [ $used -eq 0 ] || quota_error u $TSTUSR \
1181                 "user quota isn't released after deletion"
1182 }
1183 run_test 1h "Block hard limit test using fallocate"
1184
1185 test_1i() {
1186         local global_limit=200  # 200M
1187         local limit1=10  # 10M
1188         local TESTDIR="$DIR/$tdir/"
1189         local testfile="$TESTDIR/$tfile-0"
1190         local testfile1="$TESTDIR/$tfile-1"
1191         local testfile2="$TESTDIR/$tfile-2"
1192         local qpool1="qpool1"
1193
1194         mds_supports_qp
1195         setup_quota_test || error "setup quota failed with $?"
1196
1197         # enable ost quota
1198         set_ost_qtype $QTYPE || error "enable ost quota failed"
1199
1200         log "User quota (block hardlimit:$global_limit MB)"
1201         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
1202                 error "set user quota failed"
1203
1204         pool_add $qpool1 || error "pool_add failed"
1205         pool_add_targets $qpool1 0 0 ||
1206                 error "pool_add_targets failed"
1207
1208         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
1209                 error "set user quota failed"
1210
1211         # make sure the system is clean
1212         local used=$(getquota -u $TSTUSR global curspace)
1213         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1214
1215         $LFS setstripe $TESTDIR -c 1 -i 0 || error "setstripe $TESTDIR failed"
1216
1217         # hit pool limit
1218         test_1_check_write $testfile "user" $limit1
1219         $LFS setquota -u $TSTUSR -B 0 --pool $qpool1 $DIR ||
1220                 error "set user quota failed"
1221
1222         $LFS quota -uv $TSTUSR --pool $qpool1 $DIR
1223         $RUNAS $DD of=$testfile1 count=$((limit1/2)) ||
1224                 quota_error u $TSTUSR "write failure, but expect success"
1225
1226         rm -f $testfile
1227         rm -f $testfile1
1228         wait_delete_completed || error "wait_delete_completed failed"
1229         sync_all_data || true
1230
1231         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
1232                 error "set user quota failed"
1233         test_1_check_write $testfile "user" $limit1
1234         local tmp_limit=$(($limit1*2))
1235         # increase pool limit
1236         $LFS setquota -u $TSTUSR -B ${tmp_limit}M --pool $qpool1 $DIR ||
1237                 error "set user quota failed"
1238         # now write shouldn't fail
1239         $RUNAS $DD of=$testfile1 count=$((limit1/3)) ||
1240                 quota_error u $TSTUSR "write failure, but expect success"
1241         # decrease pool limit
1242         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool1 $DIR ||
1243                 error "set user quota failed"
1244         $RUNAS $DD of=$testfile2 count=$((limit1/3))
1245         # flush cache, ensure noquota flag is set on client
1246         cancel_lru_locks osc
1247         sync; sync_all_data || true
1248         $RUNAS $DD of=$testfile2 seek=$((limit1/3)) count=1 &&
1249                 quota_error u $TSTUSR "write success, but expect failure"
1250         return 0
1251 }
1252 run_test 1i "Quota pools: different limit and usage relations"
1253
1254 test_1j() {
1255         local limit=20 # MB
1256         local testfile="$DIR/$tdir/$tfile-0"
1257
1258         (( $OST1_VERSION >= $(version_code 2.15.52.206) )) ||
1259                 skip "need OST at least 2.15.52.206"
1260
1261         is_project_quota_supported ||
1262                 skip "skip project quota unsupported"
1263
1264         setup_quota_test || error "setup quota failed with $?"
1265
1266         # enable ost quota
1267         set_ost_qtype $QTYPE || error "enable ost quota failed"
1268
1269         # test for Project
1270         log "--------------------------------------"
1271         log "Project quota (block hardlimit:$limit mb)"
1272         $LFS setquota -p $TSTPRJID -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
1273                 error "set project quota failed"
1274
1275         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
1276         change_project -p $TSTPRJID $testfile
1277
1278         local procf=osd-$ost1_FSTYPE.$FSNAME-OST0000.quota_slave.root_prj_enable
1279         do_facet ost1 $LCTL set_param $procf=1 ||
1280                 error "enable root quotas for project failed"
1281         stack_trap "do_facet ost1 $LCTL set_param $procf=0"
1282
1283         runas -u 0 -g 0 $DD of=$testfile count=$limit oflag=direct || true
1284         runas -u 0 -g 0 $DD of=$testfile count=$((limit/2)) seek=$limit oflag=direct &&
1285                 quota_error "project" $TSTPRJID "root write to project success"
1286
1287         do_facet ost1 $LCTL set_param $procf=0 ||
1288                 error "disable root quotas for project failed"
1289
1290         runas -u 0 -g 0 $DD of=$testfile count=$limit seek=$limit oflag=direct ||
1291                 quota_error "project" $TSTPRJID "root write to project failed"
1292
1293         # cleanup
1294         cleanup_quota_test
1295
1296         used=$(getquota -p $TSTPRJID global curspace)
1297         (( $used == 0 )) || quota_error p $TSTPRJID \
1298                 "project quota isn't released after deletion"
1299 }
1300 run_test 1j "Enable project quota enforcement for root"
1301
1302 # test inode hardlimit
1303 test_2() {
1304         local testfile="$DIR/$tdir/$tfile-0"
1305         local least_qunit=$(do_facet mds1 $LCTL get_param -n \
1306                 qmt.$FSNAME-QMT0000.md-0x0.info |
1307                 sed -e 's/least qunit/least_qunit/' |
1308                 awk '/least_qunit/{ print $2 }')
1309         local limit
1310
1311         [ "$SLOW" = "no" ] && limit=$((least_qunit * 2)) ||
1312                 limit=$((least_qunit * 1024))
1313         echo "least_qunit: '$least_qunit', limit: '$limit'"
1314
1315         local free_inodes=$(mdt_free_inodes 0)
1316         echo "$free_inodes free inodes on master MDT"
1317         [ $free_inodes -lt $limit ] &&
1318                 skip "not enough free inodes $free_inodes required $limit"
1319
1320         setup_quota_test || error "setup quota failed with $?"
1321
1322         # enable mdt quota
1323         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1324
1325         # test for user
1326         log "User quota (inode hardlimit:$limit files)"
1327         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
1328                 error "set user quota failed"
1329
1330         # make sure the system is clean
1331         local used=$(getquota -u $TSTUSR global curinodes)
1332         [ $used -ne 0 ] && error "Used inodes($used) for user $TSTUSR isn't 0."
1333
1334         log "Create $((limit - least_qunit)) files ..."
1335         $RUNAS createmany -m ${testfile} $((limit - least_qunit)) ||
1336                 quota_error u $TSTUSR "user create failure, but expect success"
1337         # it is ok, if it fails on the last qunit
1338         $RUNAS createmany -m ${testfile}_yyy $least_qunit || true
1339         log "Create out of file quota ..."
1340         $RUNAS touch ${testfile}_xxx &&
1341                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
1342
1343         # cleanup
1344         unlinkmany ${testfile} $((limit - least_qunit)) ||
1345                 error "unlinkmany $testfile failed"
1346         # if 2nd createmany got EDQUOT, not all of nodes would be created
1347         unlinkmany ${testfile}_yyy $least_qunit || true
1348         rm -f ${testfile}_xxx
1349         wait_delete_completed
1350
1351         used=$(getquota -u $TSTUSR global curinodes)
1352         [ $used -ne 0 ] && quota_error u $TSTUSR \
1353                 "user quota isn't released after deletion"
1354         resetquota -u $TSTUSR
1355
1356         # test for group
1357         log "--------------------------------------"
1358         log "Group quota (inode hardlimit:$limit files)"
1359         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
1360                 error "set group quota failed"
1361
1362         testfile=$DIR/$tdir/$tfile-1
1363         # make sure the system is clean
1364         used=$(getquota -g $TSTUSR global curinodes)
1365         [ $used -ne 0 ] && error "Used inodes($used) for group $TSTUSR isn't 0."
1366
1367         log "Create $limit files ..."
1368         $RUNAS createmany -m ${testfile} $((limit - least_qunit)) ||
1369                 quota_error g $TSTUSR "group create failure, but expect success"
1370         $RUNAS createmany -m ${testfile}_yyy $least_qunit ||
1371         log "Create out of file quota ..."
1372         $RUNAS touch ${testfile}_xxx &&
1373                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
1374
1375         # cleanup
1376         unlinkmany ${testfile} $((limit - least_qunit)) ||
1377                 error "unlinkmany $testfile failed"
1378         unlinkmany ${testfile}_yyy $least_qunit || true
1379         rm -f ${testfile}_xxx
1380         wait_delete_completed
1381
1382         used=$(getquota -g $TSTUSR global curinodes)
1383         [ $used -ne 0 ] && quota_error g $TSTUSR \
1384                 "user quota isn't released after deletion"
1385
1386         resetquota -g $TSTUSR
1387         ! is_project_quota_supported &&
1388                 echo "Skip project quota is not supported" && return 0
1389
1390         # test for project
1391         log "--------------------------------------"
1392         log "Project quota (inode hardlimit:$limit files)"
1393         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $limit $DIR ||
1394                 error "set project quota failed"
1395
1396         testfile=$DIR/$tdir/$tfile-1
1397         # make sure the system is clean
1398         used=$(getquota -p $TSTPRJID global curinodes)
1399         [ $used -ne 0 ] &&
1400                 error "Used inodes($used) for project $TSTPRJID isn't 0"
1401
1402         change_project -sp $TSTPRJID $DIR/$tdir
1403         log "Create $limit files ..."
1404         $RUNAS createmany -m ${testfile} $((limit-least_qunit)) ||
1405                 quota_error p $TSTPRJID \
1406                         "project create fail, but expect success"
1407         $RUNAS createmany -m ${testfile}_yyy $least_qunit || true
1408         log "Create out of file quota ..."
1409         $RUNAS touch ${testfile}_xxx && quota_error p $TSTPRJID \
1410                 "project create success, but expect EDQUOT"
1411         change_project -C $DIR/$tdir
1412
1413         cleanup_quota_test
1414         used=$(getquota -p $TSTPRJID global curinodes)
1415         [ $used -eq 0 ] || quota_error p $TSTPRJID \
1416                 "project quota isn't released after deletion"
1417
1418 }
1419 run_test 2 "File hard limit (normal use and out of quota)"
1420
1421 test_block_soft() {
1422         local testfile=$1
1423         local grace=$2
1424         local limit=$3
1425         local OFFSET=0
1426         local qtype=$4
1427         local pool=$5
1428         local soft_limit=$(do_facet $SINGLEMDS $LCTL get_param -n \
1429                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit)
1430
1431         setup_quota_test
1432
1433         $LFS setstripe $testfile -c 1 -i 0
1434         chown $TSTUSR.$TSTUSR $testfile
1435         [ "$qtype" == "p" ] && is_project_quota_supported &&
1436                 change_project -p $TSTPRJID $testfile
1437
1438         echo "Write up to soft limit"
1439         $RUNAS $DD of=$testfile count=$limit ||
1440                 quota_error a $TSTUSR "write failure, but expect success"
1441         OFFSET=$((limit * 1024))
1442         cancel_lru_locks osc
1443
1444         echo "Write to exceed soft limit"
1445         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
1446                 quota_error a $TSTUSR "write failure, but expect success"
1447         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
1448         cancel_lru_locks osc
1449
1450         echo "mmap write when over soft limit"
1451         $RUNAS $MULTIOP $testfile.mmap OT40960SMW ||
1452                 quota_error a $TSTUSR "mmap write failure, but expect success"
1453         cancel_lru_locks osc
1454
1455         $SHOW_QUOTA_USER
1456         $SHOW_QUOTA_GROUP
1457         $SHOW_QUOTA_PROJID
1458         $SHOW_QUOTA_INFO_USER
1459         $SHOW_QUOTA_INFO_GROUP
1460         $SHOW_QUOTA_INFO_PROJID
1461
1462         echo "Write before timer goes off"
1463         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
1464                 quota_error a $TSTUSR "write failure, but expect success"
1465         OFFSET=$((OFFSET + 1024))
1466         cancel_lru_locks osc
1467
1468         wait_grace_time $qtype "block" $pool
1469
1470         $SHOW_QUOTA_USER
1471         $SHOW_QUOTA_GROUP
1472         $SHOW_QUOTA_PROJID
1473         $SHOW_QUOTA_INFO_USER
1474         $SHOW_QUOTA_INFO_GROUP
1475         $SHOW_QUOTA_INFO_PROJID
1476
1477         log "Write after timer goes off"
1478         # maybe cache write, ignore.
1479         # write up to soft least quint to consume all
1480         # possible slave granted space.
1481         $RUNAS dd if=/dev/zero of=$testfile bs=1K \
1482                 count=$soft_limit seek=$OFFSET || true
1483         OFFSET=$((OFFSET + soft_limit))
1484         cancel_lru_locks osc
1485         log "Write after cancel lru locks"
1486         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET &&
1487                 quota_error a $TSTUSR "write success, but expect EDQUOT"
1488
1489         $SHOW_QUOTA_USER
1490         $SHOW_QUOTA_GROUP
1491         $SHOW_QUOTA_PROJID
1492         $SHOW_QUOTA_INFO_USER
1493         $SHOW_QUOTA_INFO_GROUP
1494         $SHOW_QUOTA_INFO_PROJID
1495
1496         echo "Unlink file to stop timer"
1497         rm -f $testfile
1498         wait_delete_completed
1499         sync_all_data || true
1500
1501         $SHOW_QUOTA_USER
1502         $SHOW_QUOTA_GROUP
1503         $SHOW_QUOTA_PROJID
1504         $SHOW_QUOTA_INFO_USER
1505         $SHOW_QUOTA_INFO_GROUP
1506         $SHOW_QUOTA_INFO_PROJID
1507
1508         $LFS setstripe $testfile -c 1 -i 0
1509         chown $TSTUSR.$TSTUSR $testfile
1510         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $testfile
1511
1512         echo "Write ..."
1513         $RUNAS $DD of=$testfile count=$limit ||
1514                 quota_error a $TSTUSR "write failure, but expect success"
1515         # cleanup
1516         cleanup_quota_test
1517 }
1518
1519 # block soft limit
1520 test_3a() {
1521         local grace=20 # seconds
1522         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1523             grace=60
1524         fi
1525         local testfile=$DIR/$tdir/$tfile-0
1526
1527         # get minimum soft qunit size
1528         local limit=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
1529                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
1530
1531         set_ost_qtype $QTYPE || error "enable ost quota failed"
1532
1533         echo "User quota (soft limit:$limit MB  grace:$grace seconds)"
1534         # make sure the system is clean
1535         local used=$(getquota -u $TSTUSR global curspace)
1536         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1537
1538         $LFS setquota -t -u --block-grace $grace --inode-grace \
1539                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1540         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 -i 0 -I 0 $DIR ||
1541                 error "set user quota failed"
1542
1543         test_block_soft $testfile $grace $limit "u"
1544
1545         echo "Group quota (soft limit:$limit MB  grace:$grace seconds)"
1546         testfile=$DIR/$tdir/$tfile-1
1547         # make sure the system is clean
1548         used=$(getquota -g $TSTUSR global curspace)
1549         [ $used -ne 0 ] && error "Used space($used) for group $TSTUSR isn't 0."
1550
1551         $LFS setquota -t -g --block-grace $grace --inode-grace \
1552                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
1553         $LFS setquota -g $TSTUSR -b ${limit}M -B 0 -i 0 -I 0 $DIR ||
1554                 error "set group quota failed"
1555
1556         test_block_soft $testfile $grace $limit "g"
1557
1558         if is_project_quota_supported; then
1559                 echo "Project quota (soft limit:$limit MB  grace:$grace sec)"
1560                 testfile=$DIR/$tdir/$tfile-2
1561                 # make sure the system is clean
1562                 used=$(getquota -p $TSTPRJID global curspace)
1563                 [ $used -ne 0 ] && error \
1564                         "Used space($used) for project $TSTPRJID isn't 0."
1565
1566                 $LFS setquota -t -p --block-grace $grace --inode-grace \
1567                         $MAX_IQ_TIME $DIR ||
1568                                 error "set project grace time failed"
1569                 $LFS setquota -p $TSTPRJID -b ${limit}M -B 0 -i 0 -I 0 \
1570                         $DIR || error "set project quota failed"
1571
1572                 test_block_soft $testfile $grace $limit "p"
1573                 resetquota -p $TSTPRJID
1574                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1575                         $MAX_IQ_TIME $DIR ||
1576                                 error "restore project grace time failed"
1577         fi
1578
1579         # cleanup
1580         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1581                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1582         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1583                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1584 }
1585 run_test 3a "Block soft limit (start timer, timer goes off, stop timer)"
1586
1587 test_3b() {
1588         local grace=20 # seconds
1589         local qpool="qpool1"
1590         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1591                 grace=60
1592         fi
1593         local testfile=$DIR/$tdir/$tfile-0
1594
1595         mds_supports_qp
1596         # get minimum soft qunit size
1597         local limit=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
1598                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
1599         local glbl_limit=$((2*limit))
1600         local glbl_grace=$((2*grace))
1601         echo "limit $limit glbl_limit $glbl_limit"
1602         echo "grace $grace glbl_grace $glbl_grace"
1603
1604         set_ost_qtype $QTYPE || error "enable ost quota failed"
1605
1606         echo "User quota in $qpool(soft limit:$limit MB  grace:$grace seconds)"
1607         # make sure the system is clean
1608         local used=$(getquota -u $TSTUSR global curspace)
1609         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1610
1611         pool_add $qpool || error "pool_add failed"
1612         pool_add_targets $qpool 0 1 ||
1613                 error "pool_add_targets failed"
1614
1615         $LFS setquota -t -u --block-grace $glbl_grace --inode-grace \
1616                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1617         $LFS setquota -t -u --block-grace $grace \
1618                 --pool $qpool $DIR || error "set user grace time failed"
1619
1620         $LFS setquota -u $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1621                 error "set user quota failed"
1622         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1623                 error "set user quota failed"
1624
1625         test_block_soft $testfile $grace $limit "u" $qpool
1626
1627         echo "Group quota in $qpool(soft limit:$limit MB  grace:$grace seconds)"
1628         testfile=$DIR/$tdir/$tfile-1
1629         # make sure the system is clean
1630         used=$(getquota -g $TSTUSR global curspace)
1631         [ $used -ne 0 ] && error "Used space($used) for group $TSTUSR isn't 0."
1632
1633         $LFS setquota -t -g --block-grace $glbl_grace --inode-grace \
1634                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
1635         $LFS setquota -t -g --block-grace $grace \
1636                 --pool $qpool $DIR || error "set group grace time failed"
1637
1638         $LFS setquota -g $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1639                 error "set group quota failed"
1640         $LFS setquota -g $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1641                 error "set group quota failed"
1642
1643         test_block_soft $testfile $grace $limit "g" $qpool
1644
1645         if is_project_quota_supported; then
1646                 echo "Project quota in $qpool(soft:$limit MB  grace:$grace sec)"
1647                 testfile=$DIR/$tdir/$tfile-2
1648                 # make sure the system is clean
1649                 used=$(getquota -p $TSTPRJID global curspace)
1650                 [ $used -ne 0 ] && error \
1651                         "Used space($used) for project $TSTPRJID isn't 0."
1652
1653                 $LFS setquota -t -p --block-grace $glbl_grace --inode-grace \
1654                         $MAX_IQ_TIME $DIR ||
1655                                 error "set project grace time failed"
1656                 $LFS setquota -t -p --block-grace $grace \
1657                         --pool $qpool $DIR ||
1658                                 error "set project grace time failed"
1659
1660                 $LFS setquota -p $TSTPRJID -b ${glbl_limit}M -B 0 -i 0 -I 0 \
1661                         $DIR || error "set project quota failed"
1662                 $LFS setquota -p $TSTPRJID -b ${limit}M -B 0 \
1663                         --pool $qpool $DIR || error "set project quota failed"
1664
1665                 test_block_soft $testfile $grace $limit "p" $qpool
1666                 resetquota -p $TSTPRJID
1667                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1668                         $MAX_IQ_TIME $DIR ||
1669                                 error "restore project grace time failed"
1670                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --pool $qpool \
1671                         $DIR || error "set project grace time failed"
1672         fi
1673
1674         # cleanup
1675         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1676                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1677         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1678                 --pool $qpool $DIR || error "restore user grace time failed"
1679         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1680                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1681         $LFS setquota -t -g --block-grace $MAX_DQ_TIME \
1682                 --pool $qpool $DIR || error "restore group grace time failed"
1683 }
1684 run_test 3b "Quota pools: Block soft limit (start timer, expires, stop timer)"
1685
1686 test_3c() {
1687         local grace=20 # seconds
1688         local qpool="qpool1"
1689         local qpool2="qpool2"
1690         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1691                 grace=60
1692         fi
1693         local testfile=$DIR/$tdir/$tfile-0
1694
1695         mds_supports_qp
1696         # get minimum soft qunit size
1697         local limit=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
1698                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
1699         local limit2=$((limit+4))
1700         local glbl_limit=$((limit+8))
1701         local grace1=$((grace+10))
1702         local grace2=$grace
1703         local glbl_grace=$((grace+20))
1704         echo "limit $limit limit2 $limit2 glbl_limit $glbl_limit"
1705         echo "grace1 $grace1 grace2 $grace2 glbl_grace $glbl_grace"
1706
1707         set_ost_qtype $QTYPE || error "enable ost quota failed"
1708
1709         echo "User quota in qpool2(soft:$limit2 MB grace:$grace2 seconds)"
1710         # make sure the system is clean
1711         local used=$(getquota -u $TSTUSR global curspace)
1712         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1713
1714         pool_add $qpool || error "pool_add failed"
1715         pool_add_targets $qpool 0 1 ||
1716                 error "pool_add_targets failed"
1717
1718         pool_add $qpool2 || error "pool_add failed"
1719         pool_add_targets $qpool2 0 1 ||
1720                 error "pool_add_targets failed"
1721
1722         $LFS setquota -t -u --block-grace $glbl_grace --inode-grace \
1723                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1724         $LFS setquota -t -u --block-grace $grace1 \
1725                 --pool $qpool $DIR || error "set user grace time failed"
1726         $LFS setquota -t -u --block-grace $grace2 \
1727                 --pool $qpool2 $DIR || error "set user grace time failed"
1728
1729         $LFS setquota -u $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1730                 error "set user quota failed"
1731         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1732                 error "set user quota failed"
1733         # qpool has minimum soft limit, but its grace is greater than
1734         # the grace period of qpool2. Thus write shouldn't fail when
1735         # hit qpool soft limit - only when reaches up qpool2 limit
1736         # after grace2 seconds.
1737         $LFS setquota -u $TSTUSR -b ${limit2}M -B 0 --pool $qpool2 $DIR ||
1738                 error "set user quota failed"
1739
1740         test_block_soft $testfile $grace2 $limit2 "u" $qpool2
1741
1742         # cleanup
1743         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1744                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1745         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1746                 --pool $qpool $DIR || error "restore user grace time failed"
1747         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1748                 --pool $qpool2 $DIR || error "restore user grace time failed"
1749 }
1750 run_test 3c "Quota pools: check block soft limit on different pools"
1751
1752 test_file_soft() {
1753         local TESTFILE=$1
1754         local LIMIT=$2
1755         local grace=$3
1756         local qtype=$4
1757         local SOFT_LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1758                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1759
1760         setup_quota_test
1761         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
1762
1763         echo "Create files to exceed soft limit"
1764         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
1765                 quota_error a $TSTUSR "create failure, but expect success"
1766         local trigger_time=$(date +%s)
1767
1768         sync_all_data || true
1769
1770         local cur_time=$(date +%s)
1771         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
1772                 error "Passed grace time $grace, $trigger_time, $cur_time"
1773
1774         echo "Create file before timer goes off"
1775         $RUNAS touch ${TESTFILE}_before ||
1776                 quota_error a $TSTUSR "failed create before timer expired," \
1777                         "but expect success. $trigger_time, $cur_time"
1778         sync_all_data || true
1779
1780         wait_grace_time $qtype "file"
1781
1782         $SHOW_QUOTA_USER
1783         $SHOW_QUOTA_GROUP
1784         $SHOW_QUOTA_PROJID
1785         $SHOW_QUOTA_INFO_USER
1786         $SHOW_QUOTA_INFO_GROUP
1787         $SHOW_QUOTA_INFO_PROJID
1788
1789         echo "Create file after timer goes off"
1790         # exceed least soft limit is possible
1791         $RUNAS createmany -m ${TESTFILE}_after_3 $((SOFT_LIMIT + 1)) &&
1792                 quota_error a $TSTUSR "create after timer expired," \
1793                         "but expect EDQUOT"
1794         sync_all_data || true
1795
1796         $SHOW_QUOTA_USER
1797         $SHOW_QUOTA_GROUP
1798         $SHOW_QUOTA_PROJID
1799         $SHOW_QUOTA_INFO_USER
1800         $SHOW_QUOTA_INFO_GROUP
1801         $SHOW_QUOTA_INFO_PROJID
1802
1803         echo "Unlink files to stop timer"
1804         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
1805         wait_delete_completed
1806
1807         echo "Create file"
1808         $RUNAS touch ${TESTFILE}_xxx ||
1809                 quota_error a $TSTUSR "touch after timer stop failure," \
1810                         "but expect success"
1811         sync_all_data || true
1812
1813         # cleanup
1814         cleanup_quota_test
1815 }
1816
1817 # file soft limit
1818 test_4a() {
1819         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1820                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1821         local TESTFILE=$DIR/$tdir/$tfile-0
1822         local GRACE=12
1823
1824         [ "$mds1_FSTYPE" = zfs ] && GRACE=20
1825         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1826
1827         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1828         # make sure the system is clean
1829         local USED=$(getquota -u $TSTUSR global curinodes)
1830         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1831
1832         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1833                 $GRACE $DIR || error "set user grace time failed"
1834         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1835                 error "set user quota failed"
1836
1837         test_file_soft $TESTFILE $LIMIT $GRACE "u"
1838
1839         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1840         # make sure the system is clean
1841         USED=$(getquota -g $TSTUSR global curinodes)
1842         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
1843
1844         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1845                 $GRACE $DIR || error "set group grace time failed"
1846         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1847                 error "set group quota failed"
1848         TESTFILE=$DIR/$tdir/$tfile-1
1849
1850         test_file_soft $TESTFILE $LIMIT $GRACE "g"
1851
1852         if is_project_quota_supported; then
1853                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
1854                 # make sure the system is clean
1855                 USED=$(getquota -p $TSTPRJID global curinodes)
1856                 [ $USED -ne 0 ] && error \
1857                         "Used space($USED) for project $TSTPRJID isn't 0."
1858
1859                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1860                         $GRACE $DIR || error "set project grace time failed"
1861                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1862                         error "set project quota failed"
1863
1864                 TESTFILE=$DIR/$tdir/$tfile-1
1865                 # one less than limit, because of parent directory included.
1866                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
1867                 resetquota -p $TSTPRJID
1868                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1869                         $MAX_IQ_TIME $DIR ||
1870                                 error "restore project grace time failed"
1871         fi
1872
1873         # cleanup
1874         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1875                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1876         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1877                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1878 }
1879 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1880
1881 test_4b() {
1882         local GR_STR1="1w3d"
1883         local GR_STR2="1000s"
1884         local GR_STR3="5s"
1885         local GR_STR4="1w2d3h4m5s"
1886         local GR_STR5="5c"
1887         local GR_STR6="18446744073709551615"
1888         local GR_STR7="-1"
1889
1890         wait_delete_completed
1891
1892         # test of valid grace strings handling
1893         echo "Valid grace strings test"
1894         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1895                 $GR_STR2 $DIR || error "set user grace time failed"
1896         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1897         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1898                 $GR_STR4 $DIR || error "set group grace time quota failed"
1899         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1900
1901         # test of invalid grace strings handling
1902         echo "  Invalid grace strings test"
1903         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1904         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1905         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1906                 $GR_STR7 $DIR
1907
1908         # cleanup
1909         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1910                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1911         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1912                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1913 }
1914 run_test 4b "Grace time strings handling"
1915
1916 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1917 test_5() {
1918         local BLIMIT=10 # MB
1919         local ILIMIT=10 # inodes
1920
1921         setup_quota_test || error "setup quota failed with $?"
1922
1923         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1924         set_ost_qtype $QTYPE || error "enable ost quota failed"
1925
1926         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1927         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1928                 error "set user quota failed"
1929         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1930         if is_project_quota_supported; then
1931                 error "set group quota failed"
1932                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1933                         -I $ILIMIT $DIR || error "set project quota failed"
1934         fi
1935
1936         # make sure the system is clean
1937         local USED=$(getquota -u $TSTUSR global curinodes)
1938         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1939         USED=$(getquota -g $TSTUSR global curinodes)
1940         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1941         USED=$(getquota -u $TSTUSR global curspace)
1942         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1943         USED=$(getquota -g $TSTUSR global curspace)
1944         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1945         if is_project_quota_supported; then
1946                 USED=$(getquota -p $TSTPRJID global curinodes)
1947                 [ $USED -ne 0 ] &&
1948                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1949                 USED=$(getquota -p $TSTPRJID global curspace)
1950                 [ $USED -ne 0 ] &&
1951                         error "Used block($USED) for project $TSTPRJID isn't 0."
1952         fi
1953
1954         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1955         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1956                 error "create failure, expect success"
1957         if is_project_quota_supported; then
1958                 touch $DIR/$tdir/$tfile-0_1
1959                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1960         fi
1961         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1962                 error "write failure, expect success"
1963
1964         echo "Chown files to $TSTUSR.$TSTUSR ..."
1965         for i in $(seq 0 $ILIMIT); do
1966                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1967                         quota_error a $TSTUSR "chown failure, expect success"
1968         done
1969
1970         # cleanup
1971         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1972                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1973 }
1974 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1975
1976 # test dropping acquire request on master
1977 test_6() {
1978         local LIMIT=3 # MB
1979
1980         # Clear dmesg so watchdog is not triggered by previous
1981         # test output
1982         do_facet ost1 dmesg -c > /dev/null
1983
1984         setup_quota_test || error "setup quota failed with $?"
1985
1986         # make sure the system is clean
1987         local USED=$(getquota -u $TSTUSR global curspace)
1988         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1989
1990         # make sure no granted quota on ost
1991         set_ost_qtype $QTYPE || error "enable ost quota failed"
1992         resetquota -u $TSTUSR
1993
1994         # create file for $TSTUSR
1995         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1996         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1997         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1998
1999         # create file for $TSTUSR2
2000         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
2001         $LFS setstripe $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
2002         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
2003
2004         # cache per-ID lock for $TSTUSR on slave
2005         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
2006                 error "set quota failed"
2007         $RUNAS $DD of=$TESTFILE count=1 ||
2008                 error "write $TESTFILE failure, expect success"
2009         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
2010                 error "write $TESTFILE2 failure, expect success"
2011
2012         if at_is_enabled; then
2013                 at_max_saved=$(at_max_get ost1)
2014                 at_max_set $TIMEOUT ost1
2015
2016                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
2017                 # DQACQ is sent, which makes at_max to take effect
2018                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
2019                                                                 conv=notrunc
2020                 rm -f $TESTFILE
2021                 wait_delete_completed
2022         fi
2023
2024         sync; sync
2025         sync_all_data || true
2026
2027         #define QUOTA_DQACQ 601
2028         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
2029         lustre_fail mds 0x513 601
2030
2031         do_facet ost1 $LCTL set_param \
2032                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
2033
2034         # write to un-enforced ID ($TSTUSR2) should succeed
2035         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
2036                 error "write failure, expect success"
2037
2038         # write to enforced ID ($TSTUSR) in background, exceeding limit
2039         # to make sure DQACQ is sent
2040         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
2041         DDPID=$!
2042
2043         # watchdog timer uses a factor of 2
2044         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
2045         sleep $((TIMEOUT * 2 + 1))
2046
2047         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
2048
2049         # write should be blocked and never finished
2050         if ! ps -p $DDPID  > /dev/null 2>&1; then
2051                 lustre_fail mds 0 0
2052                 error "write finished incorrectly!"
2053         fi
2054
2055         lustre_fail mds 0 0
2056
2057         # no watchdog is triggered
2058         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2059         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
2060                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
2061         [ -z "$watchdog" ] || error "$watchdog"
2062
2063         rm -f $TMP/lustre-log-${TESTNAME}.log
2064
2065         # write should continue then fail with EDQUOT
2066         local count=0
2067         local c_size
2068         while [ true ]; do
2069                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2070                 if [ $count -ge 240 ]; then
2071                         quota_error u $TSTUSR "dd not finished in $count secs"
2072                 fi
2073                 count=$((count + 1))
2074                 if [ $((count % 30)) -eq 0 ]; then
2075                         c_size=$(stat -c %s $TESTFILE)
2076                         echo "Waiting $count secs. $c_size"
2077                         $SHOW_QUOTA_USER
2078                 fi
2079                 sleep 1
2080         done
2081 }
2082 run_test 6 "Test dropping acquire request on master"
2083
2084 # quota reintegration (global index)
2085 test_7a() {
2086         local TESTFILE=$DIR/$tdir/$tfile
2087         local LIMIT=20 # MB
2088
2089         [ "$SLOW" = "no" ] && LIMIT=5
2090
2091         setup_quota_test || error "setup quota failed with $?"
2092
2093         # make sure the system is clean
2094         local USED=$(getquota -u $TSTUSR global curspace)
2095         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2096
2097         # make sure no granted quota on ost1
2098         set_ost_qtype $QTYPE || error "enable ost quota failed"
2099         resetquota -u $TSTUSR
2100         set_ost_qtype "none" || error "disable ost quota failed"
2101
2102         local OSTUUID=$(ostuuid_from_index 0)
2103         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2104         [ $USED -ne 0 ] &&
2105                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
2106
2107         # create test file
2108         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2109         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2110
2111         echo "Stop ost1..."
2112         stop ost1
2113
2114         echo "Enable quota & set quota limit for $TSTUSR"
2115         set_ost_qtype $QTYPE || error "enable ost quota failed"
2116         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
2117                 error "set quota failed"
2118
2119         echo "Start ost1..."
2120         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
2121         quota_init
2122
2123         wait_ost_reint $QTYPE || error "reintegration failed"
2124
2125         # hardlimit should have been fetched by slave during global
2126         # reintegration, write will exceed quota
2127         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
2128                 quota_error u $TSTUSR "write success, but expect EDQUOT"
2129
2130         rm -f $TESTFILE
2131         wait_delete_completed
2132         sync_all_data || true
2133         sleep 3
2134
2135         echo "Stop ost1..."
2136         stop ost1
2137
2138         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
2139                 error "clear quota failed"
2140
2141         echo "Start ost1..."
2142         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
2143         quota_init
2144
2145         wait_ost_reint $QTYPE || error "reintegration failed"
2146
2147         # hardlimit should be cleared on slave during reintegration
2148         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
2149                 quota_error u $TSTUSR "write error, but expect success"
2150 }
2151 run_test 7a "Quota reintegration (global index)"
2152
2153 # quota reintegration (slave index)
2154 test_7b() {
2155         local limit=100000 # MB
2156         local TESTFILE=$DIR/$tdir/$tfile
2157
2158         setup_quota_test || error "setup quota failed with $?"
2159
2160         # make sure the system is clean
2161         local USED=$(getquota -u $TSTUSR global curspace)
2162         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2163
2164         # make sure no granted quota on ost1
2165         set_ost_qtype $QTYPE || error "enable ost quota failed"
2166         resetquota -u $TSTUSR
2167         set_ost_qtype "none" || error "disable ost quota failed"
2168
2169         local OSTUUID=$(ostuuid_from_index 0)
2170         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2171         [ $USED -ne 0 ] &&
2172                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
2173
2174         # create test file
2175         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2176         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2177
2178         # consume some space to make sure the granted space will not
2179         # be released during reconciliation
2180         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
2181                 error "consume space failure, expect success"
2182
2183         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
2184         lustre_fail mds 0xa02
2185
2186         set_ost_qtype $QTYPE || error "enable ost quota failed"
2187         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
2188                 error "set quota failed"
2189
2190         # ignore the write error
2191         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
2192
2193         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2194
2195         lustre_fail mds 0
2196
2197         echo "Restart ost to trigger reintegration..."
2198         stop ost1
2199         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
2200         quota_init
2201
2202         wait_ost_reint $QTYPE || error "reintegration failed"
2203
2204         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2205         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
2206
2207         cleanup_quota_test
2208         $SHOW_QUOTA_USER
2209 }
2210 run_test 7b "Quota reintegration (slave index)"
2211
2212 # quota reintegration (restart mds during reintegration)
2213 test_7c() {
2214         local LIMIT=20 # MB
2215         local TESTFILE=$DIR/$tdir/$tfile
2216
2217         [ "$SLOW" = "no" ] && LIMIT=5
2218
2219         setup_quota_test || error "setup quota failed with $?"
2220
2221         # make sure the system is clean
2222         local USED=$(getquota -u $TSTUSR global curspace)
2223         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2224
2225         set_ost_qtype "none" || error "disable ost quota failed"
2226         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
2227                 error "set quota failed"
2228
2229         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
2230         lustre_fail ost 0xa03
2231
2232         # enable ost quota
2233         set_ost_qtype $QTYPE || error "enable ost quota failed"
2234         # trigger reintegration
2235         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
2236         procf=${procf}quota_slave.force_reint
2237         do_facet ost1 $LCTL set_param $procf=1 ||
2238                 error "force reintegration failed"
2239
2240         echo "Stop mds..."
2241         stop mds1
2242
2243         lustre_fail ost 0
2244
2245         echo "Start mds..."
2246         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
2247         quota_init
2248
2249         # wait longer than usual to make sure the reintegration
2250         # is triggered by quota wb thread.
2251         wait_ost_reint $QTYPE 200 || error "reintegration failed"
2252
2253         # hardlimit should have been fetched by slave during global
2254         # reintegration, write will exceed quota
2255         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
2256                 quota_error u $TSTUSR "write success, but expect EDQUOT"
2257         return 0
2258 }
2259 run_test 7c "Quota reintegration (restart mds during reintegration)"
2260
2261 # Quota reintegration (Transfer index in multiple bulks)
2262 test_7d(){
2263         local TESTFILE=$DIR/$tdir/$tfile
2264         local TESTFILE1="$DIR/$tdir/$tfile"-1
2265         local limit=20 # MB
2266
2267         setup_quota_test || error "setup quota failed with $?"
2268
2269         set_ost_qtype "none" || error "disable ost quota failed"
2270         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
2271                 error "set quota for $TSTUSR failed"
2272         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
2273                 error "set quota for $TSTUSR2 failed"
2274
2275         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
2276         lustre_fail mds 0x608 0
2277
2278         # enable quota to tirgger reintegration
2279         set_ost_qtype "u" || error "enable ost quota failed"
2280         wait_ost_reint "u" || error "reintegration failed"
2281
2282         lustre_fail mds 0
2283
2284         # hardlimit should have been fetched by slave during global
2285         # reintegration, write will exceed quota
2286         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
2287                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
2288
2289         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
2290                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
2291         return 0
2292 }
2293 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
2294
2295 # quota reintegration (inode limits)
2296 test_7e() {
2297         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2298
2299         # LU-2435: skip this quota test if underlying zfs version has not
2300         # supported native dnode accounting
2301         [ "$mds1_FSTYPE" == zfs ] && {
2302                 local F="feature@userobj_accounting"
2303                 local pool=$(zpool_name mds1)
2304                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
2305
2306                 [[ "$feature" != *" active "* ]] &&
2307                         skip "requires zpool with active userobj_accounting"
2308         }
2309
2310         local ilimit=$((1024 * 2)) # inodes
2311         local TESTFILE=$DIR/${tdir}-1/$tfile
2312
2313         setup_quota_test || error "setup quota failed with $?"
2314
2315         # make sure the system is clean
2316         local USED=$(getquota -u $TSTUSR global curinodes)
2317         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
2318
2319         # make sure no granted quota on mdt1
2320         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2321         resetquota -u $TSTUSR
2322         set_mdt_qtype "none" || error "disable mdt quota failed"
2323
2324         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
2325         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
2326         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
2327                 "$TSTUSR isn't 0."
2328
2329         echo "Stop mds${MDSCOUNT}..."
2330         stop mds${MDSCOUNT}
2331
2332         echo "Enable quota & set quota limit for $TSTUSR"
2333         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2334         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2335                 error "set quota failed"
2336
2337         echo "Start mds${MDSCOUNT}..."
2338         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2339         quota_init
2340
2341         wait_mdt_reint $QTYPE || error "reintegration failed"
2342
2343         echo "create remote dir"
2344         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
2345                 error "create remote dir failed"
2346         chmod 0777 $DIR/${tdir}-1
2347
2348         # hardlimit should have been fetched by slave during global
2349         # reintegration, create will exceed quota
2350         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
2351                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
2352
2353         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
2354         wait_delete_completed
2355         sync_all_data || true
2356
2357         echo "Stop mds${MDSCOUNT}..."
2358         stop mds${MDSCOUNT}
2359
2360         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
2361                 error "clear quota failed"
2362
2363         echo "Start mds${MDSCOUNT}..."
2364         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2365         quota_init
2366
2367         wait_mdt_reint $QTYPE || error "reintegration failed"
2368
2369         # hardlimit should be cleared on slave during reintegration
2370         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
2371                 quota_error u $TSTUSR "create failed, expect success"
2372
2373         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
2374         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2375 }
2376 run_test 7e "Quota reintegration (inode limits)"
2377
2378 # run dbench with quota enabled
2379 test_8() {
2380         local BLK_LIMIT="100g" #100G
2381         local FILE_LIMIT=1000000
2382
2383         setup_quota_test || error "setup quota failed with $?"
2384
2385         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2386         set_ost_qtype $QTYPE || error "enable ost quota failed"
2387
2388         echo "Set enough high limit for user: $TSTUSR"
2389         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2390                 error "set user quota failed"
2391         echo "Set enough high limit for group: $TSTUSR"
2392         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2393                 error "set group quota failed"
2394         if is_project_quota_supported; then
2395                 change_project -sp $TSTPRJID $DIR/$tdir
2396                 echo "Set enough high limit for project: $TSTPRJID"
2397                 $LFS setquota -p $TSTPRJID -b 0 \
2398                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2399                         error "set project quota failed"
2400         fi
2401
2402         local duration=""
2403         [ "$SLOW" = "no" ] && duration=" -t 120"
2404         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
2405                 quota_error a $TSTUSR "dbench failed!"
2406
2407         is_project_quota_supported && change_project -C $DIR/$tdir
2408         return 0
2409 }
2410 run_test 8 "Run dbench with quota enabled"
2411
2412 # this check is just for test_9
2413 OST0_MIN=4900000 #4.67G
2414
2415 check_whether_skip () {
2416         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
2417         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
2418         if [ $OST0_SIZE -lt $OST0_MIN ]; then
2419                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
2420                 return 0
2421         else
2422                 return 1
2423         fi
2424 }
2425
2426 # run for fixing bug10707, it needs a big room. test for 64bit
2427 test_9() {
2428         local filesize=$((1024 * 9 / 2)) # 4.5G
2429
2430         check_whether_skip && return 0
2431
2432         setup_quota_test || error "setup quota failed with $?"
2433
2434         set_ost_qtype "ug" || error "enable ost quota failed"
2435
2436         local TESTFILE="$DIR/$tdir/$tfile-0"
2437         local BLK_LIMIT=100G #100G
2438         local FILE_LIMIT=1000000
2439
2440         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
2441
2442         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2443                 "for user: $TSTUSR"
2444         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2445                 error "set user quota failed"
2446
2447         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2448                 "for group: $TSTUSR"
2449         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2450                 error "set group quota failed"
2451
2452         quota_show_check a u $TSTUSR
2453         quota_show_check a g $TSTUSR
2454
2455         echo "Create test file"
2456         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2457         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2458
2459         log "Write the big file of 4.5G ..."
2460         $RUNAS $DD of=$TESTFILE count=$filesize ||
2461                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
2462
2463         $SHOW_QUOTA_USER
2464         $SHOW_QUOTA_GROUP
2465
2466         cleanup_quota_test
2467
2468         $SHOW_QUOTA_USER
2469         $SHOW_QUOTA_GROUP
2470 }
2471 run_test 9 "Block limit larger than 4GB (b10707)"
2472
2473 test_10() {
2474         local TESTFILE=$DIR/$tdir/$tfile
2475
2476         setup_quota_test || error "setup quota failed with $?"
2477
2478         # set limit to root user should fail
2479         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
2480                 error "set limit for root user successfully, expect failure"
2481         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
2482                 error "set limit for root group successfully, expect failure"
2483         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
2484                 error "set limit for project 0 successfully, expect failure"
2485
2486         # root user can overrun quota
2487         set_ost_qtype "ug" || error "enable ost quota failed"
2488
2489         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
2490                 error "set quota failed"
2491         quota_show_check b u $TSTUSR
2492
2493         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
2494         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2495
2496         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
2497                 error "write failure, expect success"
2498 }
2499 run_test 10 "Test quota for root user"
2500
2501 test_11() {
2502         local TESTFILE=$DIR/$tdir/$tfile
2503         setup_quota_test || error "setup quota failed with $?"
2504
2505         set_mdt_qtype "ug" || error "enable mdt quota failed"
2506         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
2507                 error "set quota failed"
2508
2509         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
2510         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
2511
2512         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
2513         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
2514
2515         $SHOW_QUOTA_USER
2516         local USED=$(getquota -u $TSTUSR global curinodes)
2517         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
2518 }
2519 run_test 11 "Chown/chgrp ignores quota"
2520
2521 test_12a() {
2522         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
2523
2524         local blimit=22 # MB
2525         local blk_cnt=$((blimit - 5))
2526         local TESTFILE0="$DIR/$tdir/$tfile"-0
2527         local TESTFILE1="$DIR/$tdir/$tfile"-1
2528
2529         setup_quota_test || error "setup quota failed with $?"
2530
2531         set_ost_qtype "u" || error "enable ost quota failed"
2532         quota_show_check b u $TSTUSR
2533
2534         $LFS setquota -u $TSTUSR -b 0 -B ${blimit}M -i 0 -I 0 $DIR ||
2535                 error "set quota failed"
2536
2537         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
2538         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
2539         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
2540         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
2541
2542         echo "Write to ost0..."
2543         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
2544                 quota_error a $TSTUSR "dd failed"
2545
2546         echo "Write to ost1..."
2547         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
2548                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
2549
2550         echo "Free space from ost0..."
2551         rm -f $TESTFILE0
2552         wait_delete_completed
2553         sync_all_data || true
2554
2555         echo "Write to ost1 after space freed from ost0..."
2556         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
2557                 quota_error a $TSTUSR "rebalancing failed"
2558 }
2559 run_test 12a "Block quota rebalancing"
2560
2561 test_12b() {
2562         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2563
2564         local ilimit=$((1024 * 2)) # inodes
2565         local TESTFILE0=$DIR/$tdir/$tfile
2566         local TESTFILE1=$DIR/${tdir}-1/$tfile
2567
2568         setup_quota_test || error "setup quota failed with $?"
2569
2570         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
2571         chmod 0777 $DIR/${tdir}-1
2572
2573         set_mdt_qtype "u" || error "enable mdt quota failed"
2574         quota_show_check f u $TSTUSR
2575
2576         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2577                 error "set quota failed"
2578
2579         echo "Create $ilimit files on mdt0..."
2580         $RUNAS createmany -m $TESTFILE0 $ilimit ||
2581                 quota_error u $TSTUSR "create failed, but expect success"
2582
2583         echo "Create files on mdt1..."
2584         $RUNAS createmany -m $TESTFILE1 1 &&
2585                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
2586
2587         echo "Free space from mdt0..."
2588         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
2589         wait_delete_completed
2590         sync_all_data || true
2591
2592         echo "Create files on mdt1 after space freed from mdt0..."
2593         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
2594                 quota_error a $TSTUSR "rebalancing failed"
2595
2596         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
2597                 error "unlink mdt1 files failed"
2598         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2599 }
2600 run_test 12b "Inode quota rebalancing"
2601
2602 test_13(){
2603         local TESTFILE=$DIR/$tdir/$tfile
2604         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
2605         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
2606
2607         setup_quota_test || error "setup quota failed with $?"
2608
2609         set_ost_qtype "u" || error "enable ost quota failed"
2610         quota_show_check b u $TSTUSR
2611
2612         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2613                 error "set quota failed"
2614         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2615         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2616
2617         # clear the locks in cache first
2618         do_facet ost1 $LCTL set_param -n $procf=clear
2619         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2620         [ $nlock -eq 0 ] || error "$nlock cached locks"
2621
2622         # write to acquire the per-ID lock
2623         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
2624                 quota_error a $TSTUSR "dd failed"
2625
2626         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2627         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
2628
2629         # clear quota doesn't trigger per-ID lock cancellation
2630         resetquota -u $TSTUSR
2631         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2632         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
2633
2634         # clear the per-ID lock
2635         do_facet ost1 $LCTL set_param -n $procf=clear
2636         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2637         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
2638
2639         # spare quota should be released
2640         local OSTUUID=$(ostuuid_from_index 0)
2641         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2642         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
2643         [ $limit -le $space ] ||
2644                 error "spare quota isn't released, limit:$limit, space:$space"
2645 }
2646 run_test 13 "Cancel per-ID lock in the LRU list"
2647
2648 test_14()
2649 {
2650         (( $MDS1_VERSION >= $(version_code 2.15.54.67) )) ||
2651                 skip "Need MDS >= v2_15_54-67-gdfe7d for qmt_site_recalc_cb fix"
2652
2653         local qpool="qpool1"
2654         local tfile1="$DIR/$tdir/$tfile-0"
2655
2656         mds_supports_qp
2657         setup_quota_test || error "setup quota failed with $?"
2658         # enable ost quota
2659         set_ost_qtype $QTYPE || error "enable ost quota failed"
2660
2661         $LFS setquota -u $TSTUSR -b 0 -B 100M -i 0 -I 0 $DIR ||
2662                 error "set user quota failed"
2663         pool_add $qpool || error "pool_add failed"
2664         pool_add_targets $qpool 0 ||
2665                 error "pool_add_targets failed"
2666         $LFS setstripe -p $qpool $DIR/$tdir || error "cannot set stripe"
2667         $LFS setquota -u $TSTUSR -B 30M --pool $qpool $DIR ||
2668                 error "set user quota failed"
2669
2670         # don't care about returned value
2671         $RUNAS $DD of=$tfile1 count=10 oflag=direct
2672
2673         echo "Stop ost1..."
2674         stop ost1
2675         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
2676                 error "set user quota failed"
2677
2678         # no panic after removing OST0000 from the pool
2679         pool_remove_target $qpool 0
2680         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
2681 }
2682 run_test 14 "check panic in qmt_site_recalc_cb"
2683
2684 test_15(){
2685         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
2686
2687         wait_delete_completed
2688         sync_all_data || true
2689
2690         # test for user
2691         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2692                 error "set user quota failed"
2693         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
2694         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2695                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2696         resetquota -u $TSTUSR
2697
2698         # test for group
2699         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2700                 error "set group quota failed"
2701         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
2702         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2703                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2704         resetquota -g $TSTUSR
2705 }
2706 run_test 15 "Set over 4T block quota"
2707
2708 test_16a()
2709 {
2710         (( $CLIENT_VERSION < $(version_code 2.14.55) )) &&
2711                 skip "Not supported Lustre client before 2.14.55"
2712
2713         setup_quota_test || error "setup quota failed with $?"
2714
2715         $LFS setquota -u $TSTUSR -B 500M -I 10K $MOUNT ||
2716                 error "failed to set quota for user $TSTUSR"
2717         $LFS setquota -g $TSTUSR -B 500M -I 10K $MOUNT ||
2718                 error "failed to set quota for group $TSTUSR"
2719
2720         $RUNAS $DD of=$DIR/$tdir/$tfile count=50 ||
2721                 quota_error u $TSTUSR "write failure"
2722
2723         $LFS quota -u $TSTUSR $MOUNT ||
2724                 quota_error u $TSTUSR "failed to get quota"
2725
2726         local OSC=$($LCTL dl | grep OST0000-osc-[^M] | awk '{print $4}')
2727
2728         $LCTL --device %$OSC deactivate
2729         stack_trap "$LCTL --device %$OSC activate"
2730
2731         $LFS quota -v -u $TSTUSR $MOUNT ||
2732                 quota_error u $TSTUSR "failed to get quota after deactivate OSC"
2733         $LFS quota -v -g $TSTUSR $MOUNT ||
2734                 quota_error g $TSTUSR "failed to get quota after deactivate OSC"
2735
2736         (( $MDSCOUNT > 1 )) || return 0
2737
2738         local MDC=$($LCTL dl | grep MDT0001-mdc-[^M] | awk '{print $4}')
2739
2740         $LCTL --device %$MDC deactivate
2741         stack_trap "$LCTL --device %$MDC activate"
2742
2743         $LFS quota -v -u $TSTUSR $MOUNT ||
2744                 quota_error u $TSTUSR "failed to get quota after deactivate MDC"
2745         $LFS quota -v -g $TSTUSR $MOUNT ||
2746                 quota_error g $TSTUSR "failed to get quota after deactivate OSC"
2747 }
2748 run_test 16a "lfs quota should skip the inactive MDT/OST"
2749
2750 cleanup_16b()
2751 {
2752         stopall
2753         formatall
2754         setupall
2755 }
2756
2757 test_16b()
2758 {
2759         (( $CLIENT_VERSION < $(version_code 2.14.55) )) &&
2760                 skip "Not supported Lustre client before 2.14.55"
2761
2762         (( $MDSCOUNT >= 3 )) || skip "needs >= 3 MDTs"
2763
2764         stopall
2765         if ! combined_mgs_mds ; then
2766                 format_mgs
2767                 start_mgs
2768         fi
2769
2770         add mds1 $(mkfs_opts mds1 $(mdsdevname 1)) --index=0 --reformat \
2771                 $(mdsdevname 1) $(mdsvdevname 1)
2772         add mds2 $(mkfs_opts mds2 $(mdsdevname 2)) --index=1 --reformat \
2773                 $(mdsdevname 2) $(mdsvdevname 2)
2774         add mds3 $(mkfs_opts mds3 $(mdsdevname 3)) --index=100 --reformat \
2775                 $(mdsdevname 3) $(mdsvdevname 3)
2776
2777         add ost1 $(mkfs_opts ost1 $(ostdevname 1)) --index=0 --reformat \
2778                 $(ostdevname 1) $(ostvdevname 1)
2779         add ost2 $(mkfs_opts ost2 $(ostdevname 2)) --index=100 --reformat \
2780                 $(ostdevname 2) $(ostvdevname 2)
2781
2782         stack_trap cleanup_16b
2783
2784         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS || error "MDT1 start failed"
2785         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS || error "MDT2 start failed"
2786         start mds3 $(mdsdevname 3) $MDS_MOUNT_OPTS || error "MDT3 start failed"
2787         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "OST1 start failed"
2788         start ost2 $(ostdevname 2) $OST_MOUNT_OPTS || error "OST2 start failed"
2789
2790         mount_client $MOUNT || error "Unable to mount client"
2791
2792         setup_quota_test || error "setup quota failed with $?"
2793
2794         $LFS setquota -u $TSTUSR -B 100M -I 10K $MOUNT ||
2795                 error "failed to set quota for user $TSTUSR"
2796         $LFS setquota -g $TSTUSR -B 100M -I 10K $MOUNT ||
2797                 error "failed to set quota for group $TSTUSR"
2798
2799         $RUNAS $DD of=$DIR/$tdir/$tfile count=10 ||
2800                 quota_error u $TSTUSR "write failure"
2801
2802         cnt=$($LFS quota -v -u $TSTUSR $MOUNT | grep -ce "^$FSNAME-[MD|OS]T*")
2803         [ $cnt -le 5 ] || quota_error u $TSTUSR "failed to get user quota"
2804         cnt=$($LFS quota -v -g $TSTUSR $MOUNT | grep -ce "^$FSNAME-[MD|OS]T*")
2805         [ $cnt -le 5 ] || quota_error g $TSTUSR "failed to get group quota"
2806 }
2807 run_test 16b "lfs quota should skip the nonexistent MDT/OST"
2808
2809 test_17sub() {
2810         local err_code=$1
2811         local BLKS=1    # 1M less than limit
2812         local TESTFILE=$DIR/$tdir/$tfile
2813
2814         setup_quota_test || error "setup quota failed with $?"
2815
2816         # make sure the system is clean
2817         local USED=$(getquota -u $TSTUSR global curspace)
2818         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2819
2820         set_ost_qtype "ug" || error "enable ost quota failed"
2821         # make sure no granted quota on ost
2822         resetquota -u $TSTUSR
2823         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2824                 error "set quota failed"
2825
2826         quota_show_check b u $TSTUSR
2827
2828         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
2829         lustre_fail mds 0xa04 $err_code
2830
2831         # write in background
2832         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
2833         local DDPID=$!
2834
2835         sleep 2
2836         # write should be blocked and never finished
2837         if ! ps -p $DDPID  > /dev/null 2>&1; then
2838                 lustre_fail mds 0 0
2839                 quota_error u $TSTUSR "write finished incorrectly!"
2840         fi
2841
2842         lustre_fail mds 0 0
2843
2844         local count=0
2845         local timeout=30
2846         while [ true ]; do
2847                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2848                 count=$((count+1))
2849                 if [ $count -gt $timeout ]; then
2850                         quota_error u $TSTUSR "dd is not finished!"
2851                 fi
2852                 sleep 1
2853         done
2854
2855         sync; sync_all_data || true
2856
2857         USED=$(getquota -u $TSTUSR global curspace)
2858         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
2859                 "Used space(${USED}K) is less than ${BLKS}M"
2860
2861         cleanup_quota_test
2862 }
2863
2864 # DQACQ return recoverable error
2865 test_17() {
2866         echo "DQACQ return -ENOLCK"
2867         #define ENOLCK  37
2868         test_17sub 37 || error "Handle -ENOLCK failed"
2869
2870         echo "DQACQ return -EAGAIN"
2871         #define EAGAIN  11
2872         test_17sub 11 || error "Handle -EAGAIN failed"
2873
2874         echo "DQACQ return -ETIMEDOUT"
2875         #define ETIMEDOUT 110
2876         test_17sub 110 || error "Handle -ETIMEDOUT failed"
2877
2878         echo "DQACQ return -ENOTCONN"
2879         #define ENOTCONN 107
2880         test_17sub 107 || error "Handle -ENOTCONN failed"
2881 }
2882
2883 run_test 17 "DQACQ return recoverable error"
2884
2885 test_18_sub () {
2886         local io_type=$1
2887         local blimit=200 # MB
2888         local TESTFILE="$DIR/$tdir/$tfile"
2889
2890         setup_quota_test || error "setup quota failed with $?"
2891
2892         set_ost_qtype "u" || error "enable ost quota failed"
2893         log "User quota (limit: $blimit)"
2894         $LFS setquota -u $TSTUSR -b 0 -B ${blimit}M -i 0 -I 0 $MOUNT ||
2895                 error "set quota failed"
2896         quota_show_check b u $TSTUSR
2897
2898         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2899         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2900
2901         local timeout=$($LCTL get_param -n timeout)
2902
2903         if [ $io_type = "directio" ]; then
2904                 log "Write 100M (directio) ..."
2905                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
2906         else
2907                 log "Write 100M (buffered) ..."
2908                 $RUNAS $DD of=$TESTFILE count=100 &
2909         fi
2910         local DDPID=$!
2911
2912         replay_barrier $SINGLEMDS
2913         log "Fail mds for $((2 * timeout)) seconds"
2914         fail $SINGLEMDS $((2 * timeout))
2915
2916         local count=0
2917         if at_is_enabled; then
2918                 timeout=$(at_max_get mds)
2919         else
2920                 timeout=$($LCTL get_param -n timeout)
2921         fi
2922
2923         while [ true ]; do
2924                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2925                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
2926                         log "it took $count second"
2927                 fi
2928                 sleep 1
2929         done
2930
2931         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
2932         sync
2933         cancel_lru_locks mdc
2934         cancel_lru_locks osc
2935         $SHOW_QUOTA_USER
2936
2937         local testfile_size=$(stat -c %s $TESTFILE)
2938         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
2939                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
2940                         "got ${testfile_size}. Verifying file failed!"
2941         fi
2942         cleanup_quota_test
2943 }
2944
2945 # test when mds does failover, the ost still could work well
2946 # this test shouldn't trigger watchdog b=14840
2947 test_18() {
2948         # Clear dmesg so watchdog is not triggered by previous
2949         # test output
2950         do_facet ost1 dmesg -c > /dev/null
2951
2952         test_18_sub normal
2953         test_18_sub directio
2954
2955         # check if watchdog is triggered
2956         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2957         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
2958                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
2959         [ -z "$watchdog" ] || error "$watchdog"
2960         rm -f $TMP/lustre-log-${TESTNAME}.log
2961 }
2962 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
2963
2964 test_19() {
2965         local blimit=5 # MB
2966         local TESTFILE=$DIR/$tdir/$tfile
2967
2968         setup_quota_test || error "setup quota failed with $?"
2969
2970         set_ost_qtype $QTYPE || error "enable ost quota failed"
2971
2972         # bind file to a single OST
2973         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2974         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2975
2976         echo "Set user quota (limit: ${blimit}M)"
2977         $LFS setquota -u $TSTUSR -b 0 -B ${blimit}M -i 0 -I 0 $MOUNT ||
2978                 error "set user quota failed"
2979         quota_show_check b u $TSTUSR
2980         echo "Update quota limits"
2981         $LFS setquota -u $TSTUSR -b 0 -B ${blimit}M -i 0 -I 0 $MOUNT ||
2982                 error "set group quota failed"
2983         quota_show_check b u $TSTUSR
2984
2985         # first wirte might be cached
2986         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2987         cancel_lru_locks osc
2988         $SHOW_QUOTA_USER
2989         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2990                 quota_error u $TSTUSR "Write success, expect failure"
2991         $SHOW_QUOTA_USER
2992 }
2993 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2994
2995 test_20() { # b15754
2996         local LSTR=(2g 1t 4k 3m) # limits strings
2997         # limits values
2998         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2999                     $((3*1024*1024)))
3000
3001         resetquota -u $TSTUSR
3002
3003         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
3004                 $MOUNT || error "could not set quota limits"
3005         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
3006                                 --inode-softlimit ${LSTR[2]} \
3007                                 --inode-hardlimit ${LSTR[3]} \
3008                                 $MOUNT || error "could not set quota limits"
3009
3010         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
3011                 error "bsoftlimit was not set properly"
3012         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
3013                 error "bhardlimit was not set properly"
3014         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
3015                 error "isoftlimit was not set properly"
3016         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
3017                 error "ihardlimit was not set properly"
3018
3019         resetquota -u $TSTUSR
3020 }
3021 run_test 20 "Test if setquota specifiers work properly (b15754)"
3022
3023 test_21_sub() {
3024         local testfile=$1
3025         local blk_number=$2
3026         local seconds=$3
3027
3028         local time=$(($(date +%s) + seconds))
3029         while [ $(date +%s) -lt $time ]; do
3030                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
3031         done
3032 }
3033
3034 # run for fixing bug16053, setquota shouldn't fail when writing and
3035 # deleting are happening
3036 test_21() {
3037         local TESTFILE="$DIR/$tdir/$tfile"
3038         local BLIMIT=10 # 10G
3039         local ILIMIT=1000000
3040
3041         setup_quota_test || error "setup quota failed with $?"
3042
3043         set_ost_qtype $QTYPE || error "Enable ost quota failed"
3044
3045         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
3046         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
3047                 error "set user quota failed"
3048         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
3049         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
3050                 error "set group quota failed"
3051         if is_project_quota_supported; then
3052                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
3053                         "project: $TSTPRJID"
3054                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
3055                          $MOUNT || error "set project quota failed"
3056         fi
3057
3058         # repeat writing on a 1M file
3059         test_21_sub ${TESTFILE}_1 1 30 &
3060         local DDPID1=$!
3061         # repeat writing on a 128M file
3062         test_21_sub ${TESTFILE}_2 128 30 &
3063         local DDPID2=$!
3064
3065         local time=$(($(date +%s) + 30))
3066         local i=1
3067         while [ $(date +%s) -lt $time ]; do
3068                 log "Set quota for $i times"
3069                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
3070                         -I $((ILIMIT + i)) $MOUNT ||
3071                                 error "Set user quota failed"
3072                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
3073                         -I $((ILIMIT + i)) $MOUNT ||
3074                                 error "Set group quota failed"
3075                 if is_project_quota_supported; then
3076                         $LFS setquota -p $TSTPRJID -b 0 -B \
3077                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
3078                                 error "Set project quota failed"
3079                 fi
3080                 i=$((i+1))
3081                 sleep 1
3082         done
3083
3084         local count=0
3085         while [ true ]; do
3086                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
3087                 count=$((count+1))
3088                 if [ $count -gt 60 ]; then
3089                         quota_error a $TSTUSR "dd should be finished!"
3090                 fi
3091                 sleep 1
3092         done
3093         echo "(dd_pid=$DDPID1, time=$count)successful"
3094
3095         count=0
3096         while [ true ]; do
3097                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
3098                 count=$((count+1))
3099                 if [ $count -gt 60 ]; then
3100                         quota_error a $TSTUSR "dd should be finished!"
3101                 fi
3102                 sleep 1
3103         done
3104         echo "(dd_pid=$DDPID2, time=$count)successful"
3105 }
3106 run_test 21 "Setquota while writing & deleting (b16053)"
3107
3108 # enable/disable quota enforcement permanently
3109 test_22() {
3110         echo "Set both mdt & ost quota type as ug"
3111         local qtype="ug"
3112         is_project_quota_supported && qtype=$QTYPE
3113         set_mdt_qtype $qtype || error "enable mdt quota failed"
3114         set_ost_qtype $qtype || error "enable ost quota failed"
3115
3116         echo "Restart..."
3117         stopall || error "failed to stopall (1)"
3118         mount
3119         setupall
3120
3121         echo "Verify if quota is enabled"
3122         local qtype1=$(mdt_quota_type)
3123         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
3124         qtype=$(ost_quota_type)
3125         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
3126
3127         echo "Set both mdt & ost quota type as none"
3128         set_mdt_qtype "none" || error "disable mdt quota failed"
3129         set_ost_qtype "none" || error "disable ost quota failed"
3130
3131         echo "Restart..."
3132         stopall || error "failed to stopall (2)"
3133         mount
3134         setupall
3135         quota_init
3136
3137         echo "Verify if quota is disabled"
3138         qtype=$(mdt_quota_type)
3139         [ $qtype != "none" ] && error "mdt quota setting is lost"
3140         qtype=$(ost_quota_type)
3141         [ $qtype != "none" ] && error "ost quota setting is lost"
3142
3143         return 0
3144 }
3145 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
3146
3147 test_23_sub() {
3148         local TESTFILE="$DIR/$tdir/$tfile"
3149         local LIMIT=$1
3150
3151         setup_quota_test || error "setup quota failed with $?"
3152
3153         set_ost_qtype $QTYPE || error "Enable ost quota failed"
3154
3155         # test for user
3156         log "User quota (limit: $LIMIT MB)"
3157         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
3158                 error "set quota failed"
3159         quota_show_check b u $TSTUSR
3160
3161         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
3162         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3163
3164         log "Step1: trigger EDQUOT with O_DIRECT"
3165         log "Write half of file"
3166         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
3167                 quota_error u $TSTUSR "(1) Write failure, expect success." \
3168                         "limit=$LIMIT"
3169         log "Write out of block quota ..."
3170         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
3171                 oflag=direct conv=notrunc &&
3172                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
3173                         "limit=$LIMIT"
3174         log "Step1: done"
3175
3176         log "Step2: rewrite should succeed"
3177         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
3178                 quota_error u $TSTUSR "(3) Write failure, expect success." \
3179                         "limit=$LIMIT"
3180         log "Step2: done"
3181
3182         cleanup_quota_test
3183
3184         local OST0_UUID=$(ostuuid_from_index 0)
3185         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
3186         [ $OST0_QUOTA_USED -ne 0 ] &&
3187                 ($SHOW_QUOTA_USER; \
3188                 quota_error u $TSTUSR "quota isn't released")
3189         $SHOW_QUOTA_USER
3190 }
3191
3192 test_23() {
3193         [ "$ost1_FSTYPE" == zfs ] &&
3194                 skip "Overwrite in place is not guaranteed to be " \
3195                 "space neutral on ZFS"
3196
3197         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
3198         check_whether_skip && return 0
3199         log "run for 4MB test file"
3200         test_23_sub 4
3201
3202         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
3203         check_whether_skip && return 0
3204         log "run for 40MB test file"
3205         test_23_sub 40
3206 }
3207 run_test 23 "Quota should be honored with directIO (b16125)"
3208
3209 test_24() {
3210         local blimit=5 # MB
3211         local TESTFILE="$DIR/$tdir/$tfile"
3212
3213         setup_quota_test || error "setup quota failed with $?"
3214
3215         set_ost_qtype $QTYPE || error "enable ost quota failed"
3216
3217         # bind file to a single OST
3218         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
3219         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3220
3221         echo "Set user quota (limit: ${blimit}M)"
3222         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
3223                 error "set quota failed"
3224
3225         # overrun quota by root user
3226         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
3227                 error "write failure, expect success"
3228         cancel_lru_locks osc
3229         sync_all_data || true
3230
3231         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
3232 }
3233 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
3234
3235 test_25()
3236 {
3237         (( $MDS1_VERSION >= $(version_code 2.15.55.145) )) ||
3238                 skip "need MDS >= v2_15_55-145-g513b1cdbc for index version fix"
3239
3240         local limit=10  # 10M
3241         local testfile="$DIR/$tdir/$tfile-0"
3242         local qpool="qpool1"
3243
3244         mds_supports_qp
3245         setup_quota_test || error "setup quota failed with $?"
3246
3247         # enable ost quota
3248         set_ost_qtype $QTYPE || error "enable ost quota failed"
3249
3250         $LFS setquota -u $TSTUSR -b 0 -B 50T -i 0 -I 0 $DIR ||
3251                 error "set user quota failed"
3252
3253         pool_add $qpool || error "pool_add failed"
3254         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
3255                 error "pool_add_targets failed"
3256
3257         # increase ost index version to +200
3258         for i in {1..200}; do
3259                 $LFS setquota -u $TSTUSR -B ${i}G --pool $qpool $DIR ||
3260                         error "set user quota failed"
3261         done
3262         $LFS setquota -u $TSTUSR -b 0 -B 0 --pool $qpool $DIR ||
3263                 error "set user quota failed"
3264
3265         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
3266                 error "set user quota failed"
3267
3268         local used=$(getquota -u $TSTUSR global curspace)
3269         (( used == 0)) || error "Used space($used) for user $TSTUSR isn't 0."
3270
3271         $LFS setstripe $testfile -c 1 || error "setstripe $testfile failed"
3272         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
3273
3274         test_1_check_write $testfile "user" $limit
3275         return 0
3276 }
3277 run_test 25 "check indexes versions"
3278
3279 test_27a() { # b19612
3280         $LFS quota $TSTUSR $DIR &&
3281                 error "lfs succeeded with no type, but should have failed"
3282         $LFS setquota $TSTUSR $DIR &&
3283                 error "lfs succeeded with no type, but should have failed"
3284         return 0
3285 }
3286 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
3287
3288 test_27b() { # b20200
3289         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
3290                 error "lfs setquota failed with uid argument"
3291         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
3292                 error "lfs stequota failed with gid argument"
3293         if is_project_quota_supported; then
3294                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
3295                         1000 $DIR || error \
3296                                 "lfs stequota failed with projid argument"
3297         fi
3298         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
3299         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
3300         if is_project_quota_supported; then
3301                 $SHOW_QUOTA_PROJID ||
3302                         error "lfs quota failed with projid argument"
3303                 resetquota_one -p $TSTPRJID
3304         fi
3305         resetquota -u $TSTID
3306         resetquota -g $TSTID
3307         return 0
3308 }
3309 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
3310
3311 test_27c() {
3312         local limit
3313
3314         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
3315                 error "lfs setquota failed"
3316
3317         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
3318         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
3319         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
3320         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
3321
3322         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
3323                 error "lfs setquota for $TSTID failed"
3324
3325         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
3326         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
3327         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
3328         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
3329
3330         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
3331                 error "total allocated inode/block limit not printed"
3332
3333         resetquota -u $TSTUSR
3334 }
3335 run_test 27c "lfs quota should support human-readable output"
3336
3337 test_27d() {
3338         local softlimit=1.5
3339         local hardlimit=2.3
3340         local limit
3341
3342         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
3343                 error "set fraction block limit failed"
3344         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
3345         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
3346         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
3347         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
3348
3349         resetquota -u $TSTUSR
3350 }
3351 run_test 27d "lfs setquota should support fraction block limit"
3352
3353 test_30()
3354 {
3355         (( $MDS1_VERSION >= $(version_code 2.15.51.29) )) ||
3356                 skip "need MDS >= v2_15_51-29-gd4978678b4 for grace time fix"
3357
3358         local LIMIT=4 # MB
3359         local TESTFILE="$DIR/$tdir/$tfile"
3360         local GRACE=10
3361
3362         setup_quota_test || error "setup quota failed with $?"
3363
3364         set_ost_qtype "u" || error "enable ost quota failed"
3365
3366         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
3367         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3368
3369         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
3370                 $MAX_IQ_TIME $DIR || error "set grace time failed"
3371         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
3372                 error "set quota failed"
3373         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
3374         cancel_lru_locks osc
3375         sleep $GRACE
3376         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
3377         # over-quota flag has not yet settled since we do not trigger async
3378         # events based on grace time period expiration
3379         $SHOW_QUOTA_USER
3380         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
3381         cancel_lru_locks osc
3382         # now over-quota flag should be settled and further writes should fail
3383         $SHOW_QUOTA_USER
3384         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
3385                 error "grace times were reset"
3386         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
3387                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
3388 }
3389 run_test 30 "Hard limit updates should not reset grace times"
3390
3391 # basic usage tracking for user & group
3392 test_33() {
3393         local INODES=10 # files
3394         local BLK_CNT=2 # MB each
3395         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
3396
3397         setup_quota_test || error "setup quota failed with $?"
3398
3399         # make sure the system is clean
3400         local USED=$(getquota -u $TSTID global curspace)
3401         [ $USED -ne 0 ] &&
3402                 error "Used space ($USED) for user $TSTID isn't 0."
3403         USED=$(getquota -g $TSTID global curspace)
3404         [ $USED -ne 0 ] &&
3405                 error "Used space ($USED) for group $TSTID isn't 0."
3406         if is_project_quota_supported; then
3407                 USED=$(getquota -p $TSTPRJID global curspace)
3408                 [ $USED -ne 0 ] && error \
3409                         "Used space ($USED) for project $TSTPRJID isn't 0."
3410         fi
3411
3412         echo "Write files..."
3413         for i in $(seq 0 $INODES); do
3414                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
3415                         error "write failed"
3416                         is_project_quota_supported &&
3417                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
3418                 echo "Iteration $i/$INODES completed"
3419         done
3420         cancel_lru_locks osc
3421
3422         echo "Wait for setattr on objects finished..."
3423         wait_delete_completed
3424
3425         sync; sync_all_data || true
3426
3427         echo "Verify disk usage after write"
3428         USED=$(getquota -u $TSTID global curspace)
3429         [ $USED -lt $TOTAL_BLKS ] &&
3430                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
3431         USED=$(getquota -g $TSTID global curspace)
3432         [ $USED -lt $TOTAL_BLKS ] &&
3433                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
3434         if is_project_quota_supported; then
3435                 USED=$(getquota -p $TSTPRJID global curspace)
3436                 [ $USED -lt $TOTAL_BLKS ] && error \
3437                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
3438         fi
3439
3440         echo "Verify inode usage after write"
3441         USED=$(getquota -u $TSTID global curinodes)
3442         [ $USED -lt $INODES ] &&
3443                 error "Used inode for user $TSTID is $USED, expected $INODES"
3444         USED=$(getquota -g $TSTID global curinodes)
3445         [ $USED -lt $INODES ] &&
3446                 error "Used inode for group $TSTID is $USED, expected $INODES"
3447         if is_project_quota_supported; then
3448                 USED=$(getquota -p $TSTPRJID global curinodes)
3449                 [ $USED -lt $INODES ] && error \
3450                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
3451         fi
3452
3453         cleanup_quota_test
3454
3455         echo "Verify disk usage after delete"
3456         USED=$(getquota -u $TSTID global curspace)
3457         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
3458         USED=$(getquota -u $TSTID global curinodes)
3459         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
3460         USED=$(getquota -g $TSTID global curspace)
3461         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
3462         USED=$(getquota -g $TSTID global curinodes)
3463         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
3464         if is_project_quota_supported; then
3465                 USED=$(getquota -p $TSTPRJID global curspace)
3466                 [ $USED -eq 0 ] ||
3467                         error "Used space for project $TSTPRJID isn't 0. $USED"
3468                 USED=$(getquota -p $TSTPRJID global curinodes)
3469                 [ $USED -eq 0 ] ||
3470                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
3471         fi
3472 }
3473 run_test 33 "Basic usage tracking for user & group & project"
3474
3475 # usage transfer test for user & group & project
3476 test_34() {
3477         local BLK_CNT=2 # MB
3478         local project_supported="no"
3479
3480         is_project_quota_supported && project_supported="yes"
3481         setup_quota_test || error "setup quota failed with $?"
3482
3483         # make sure the system is clean
3484         local USED=$(getquota -u $TSTID global curspace)
3485         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
3486         USED=$(getquota -g $TSTID global curspace)
3487         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
3488
3489         local USED=$(getquota -u $TSTID2 global curspace)
3490         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
3491         if [ $project_supported == "yes" ]; then
3492                 USED=$(getquota -p $TSTPRJID global curspace)
3493                 [ $USED -ne 0 ] && error \
3494                         "Used space ($USED) for Project $TSTPRJID isn't 0."
3495         fi
3496
3497         echo "Write file..."
3498         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3499                 error "write failed"
3500         cancel_lru_locks osc
3501         sync; sync_all_data || true
3502
3503         echo "chown the file to user $TSTID"
3504         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
3505
3506         echo "Wait for setattr on objects finished..."
3507         wait_delete_completed
3508
3509         BLK_CNT=$((BLK_CNT * 1024))
3510
3511         echo "Verify disk usage for user $TSTID"
3512         USED=$(getquota -u $TSTID global curspace)
3513         [ $USED -lt $BLK_CNT ] &&
3514                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
3515         USED=$(getquota -u $TSTID global curinodes)
3516         [ $USED -ne 1 ] &&
3517                 error "Used inodes for user $TSTID is $USED, expected 1"
3518
3519         echo "chgrp the file to group $TSTID"
3520         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
3521
3522         echo "Wait for setattr on objects finished..."
3523         wait_delete_completed
3524
3525         echo "Verify disk usage for group $TSTID"
3526         USED=$(getquota -g $TSTID global curspace)
3527         [ $USED -ge $BLK_CNT ] ||
3528                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3529         USED=$(getquota -g $TSTID global curinodes)
3530         [ $USED -eq 1 ] ||
3531                 error "Used inodes for group $TSTID is $USED, expected 1"
3532
3533         # chown won't change the ost object group. LU-4345 */
3534         echo "chown the file to user $TSTID2"
3535         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
3536
3537         echo "Wait for setattr on objects finished..."
3538         wait_delete_completed
3539
3540         echo "change_project project id to $TSTPRJID"
3541         [ $project_supported == "yes" ] &&
3542                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3543         echo "Wait for setattr on objects finished..."
3544         wait_delete_completed
3545
3546         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
3547         USED=$(getquota -u $TSTID2 global curspace)
3548         [ $USED -lt $BLK_CNT ] &&
3549                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
3550         USED=$(getquota -u $TSTID global curspace)
3551         [ $USED -ne 0 ] &&
3552                 error "Used space for user $TSTID is $USED, expected 0"
3553         USED=$(getquota -g $TSTID global curspace)
3554         [ $USED -lt $BLK_CNT ] &&
3555                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3556         if [ $project_supported == "yes" ]; then
3557                 USED=$(getquota -p $TSTPRJID global curspace)
3558                 [ $USED -lt $BLK_CNT ] && error \
3559                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
3560         fi
3561         return 0
3562 }
3563 run_test 34 "Usage transfer for user & group & project"
3564
3565 # usage is still accessible across restart
3566 test_35() {
3567         local BLK_CNT=2 # MB
3568
3569         setup_quota_test || error "setup quota failed with $?"
3570
3571         echo "Write file..."
3572         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3573                 error "write failed"
3574         is_project_quota_supported &&
3575                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3576         cancel_lru_locks
3577
3578         echo "Wait for setattr on objects finished..."
3579         wait_delete_completed
3580
3581         sync; sync_all_data || true
3582
3583         echo "Save disk usage before restart"
3584         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
3585         [ $ORIG_USR_SPACE -eq 0 ] &&
3586                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
3587         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
3588         [ $ORIG_USR_INODES -eq 0 ] &&
3589                 error "Used inodes for user $TSTID is 0, expected 1"
3590         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
3591         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
3592         [ $ORIG_GRP_SPACE -eq 0 ] &&
3593                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
3594         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
3595         [ $ORIG_GRP_INODES -eq 0 ] &&
3596                 error "Used inodes for group $TSTID is 0, expected 1"
3597         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
3598
3599         if is_project_quota_supported; then
3600                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
3601                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
3602                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
3603                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
3604                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
3605                         "Used inodes for project $TSTPRJID is 0, expected 1"
3606                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
3607         fi
3608
3609         log "Restart..."
3610         stopall
3611         setupall
3612         wait_recovery_complete
3613         quota_init
3614
3615         echo "Verify disk usage after restart"
3616         local USED=$(getquota -u $TSTID global curspace)
3617         (( $USED == $ORIG_USR_SPACE )) || {
3618                 ls -al $DIR/$tdir/$tfile
3619                 $LFS quota -v -u $TSTID $DIR
3620                 error "Used space for user $TSTID changed from " \
3621                         "$ORIG_USR_SPACE to $USED"
3622         }
3623         USED=$(getquota -u $TSTID global curinodes)
3624         [ $USED -eq $ORIG_USR_INODES ] ||
3625                 error "Used inodes for user $TSTID changed from " \
3626                         "$ORIG_USR_INODES to $USED"
3627         USED=$(getquota -g $TSTID global curspace)
3628         [ $USED -eq $ORIG_GRP_SPACE ] ||
3629                 error "Used space for group $TSTID changed from " \
3630                         "$ORIG_GRP_SPACE to $USED"
3631         USED=$(getquota -g $TSTID global curinodes)
3632         [ $USED -eq $ORIG_GRP_INODES ] ||
3633                 error "Used inodes for group $TSTID changed from " \
3634                         "$ORIG_GRP_INODES to $USED"
3635         if [[ $project_supported == "yes" ]]; then
3636                 USED=$(getquota -p $TSTPRJID global curinodes)
3637                 (( $USED == $ORIG_PRJ_INODES )) ||
3638                         error "Used inodes for project $TSTPRJID " \
3639                                 "changed from $ORIG_PRJ_INODES to $USED"
3640                 USED=$(getquota -p $TSTPRJID global curspace)
3641                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
3642                         error "Used space for project $TSTPRJID "\
3643                                 "changed from $ORIG_PRJ_SPACE to $USED"
3644         fi
3645
3646         # check if the vfs_dq_init() is called before writing
3647         echo "Append to the same file..."
3648         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
3649                 error "write failed"
3650         cancel_lru_locks osc
3651         sync; sync_all_data || true
3652
3653         echo "Verify space usage is increased"
3654         USED=$(getquota -u $TSTID global curspace)
3655         [ $USED -gt $ORIG_USR_SPACE ] ||
3656                 error "Used space for user $TSTID isn't increased" \
3657                         "orig:$ORIG_USR_SPACE, now:$USED"
3658         USED=$(getquota -g $TSTID global curspace)
3659         [ $USED -gt $ORIG_GRP_SPACE ] ||
3660                 error "Used space for group $TSTID isn't increased" \
3661                         "orig:$ORIG_GRP_SPACE, now:$USED"
3662         if [[ $project_supported == "yes" ]]; then
3663                 USED=$(getquota -p $TSTPRJID global curspace)
3664                 (( $USED > $ORIG_PRJ_SPACE )) ||
3665                         error "Used space for project $TSTPRJID isn't " \
3666                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
3667         fi
3668 }
3669 run_test 35 "Usage is still accessible across reboot"
3670
3671 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
3672 # LU-5006
3673 test_37() {
3674         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
3675                 skip "Old server doesn't have LU-5006 fix."
3676
3677         setup_quota_test || error "setup quota failed with $?"
3678
3679         # make sure the system is clean
3680         local USED=$(getquota -u $TSTID global curspace)
3681         [ $USED -ne 0 ] &&
3682                 error "Used space ($USED) for user $TSTID isn't 0."
3683
3684         # create file with MDS_OPEN_DELAY_CREATE flag
3685         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
3686                 error "Create file failed"
3687         # write to file
3688         $DD of=$DIR/$tdir/$tfile count=1 conv=notrunc \
3689                 oflag=sync || error "Write file failed"
3690         # chown to the file
3691         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
3692
3693         # wait for setattr on objects finished..."
3694         wait_delete_completed
3695
3696         USED=$(getquota -u $TSTID global curspace)
3697         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
3698 }
3699 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
3700
3701 # LU-8801
3702 test_38() {
3703         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
3704                 skip "Old server doesn't have LU-8801 fix."
3705
3706         [ "$UID" != 0 ] && skip_env "must run as root" && return
3707
3708         setup_quota_test || error "setup quota failed with $?"
3709
3710         # make sure the system is clean
3711         local USED=$(getquota -u $TSTID global curspace)
3712         [ $USED -ne 0 ] &&
3713                 error "Used space ($USED) for user $TSTID isn't 0."
3714         USED=$(getquota -u $TSTID2 global curspace)
3715         [ $USED -ne 0 ] &&
3716                 error "Used space ($USED) for user $TSTID2 isn't 0."
3717
3718         local TESTFILE="$DIR/$tdir/$tfile"
3719         local file_cnt=10000
3720
3721         # Generate id entries in accounting file
3722         echo "Create $file_cnt files..."
3723         for i in `seq $file_cnt`; do
3724                 touch $TESTFILE-$i
3725                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
3726                         error "failed to chown $TESTFILE-$i"
3727         done
3728         cancel_lru_locks osc
3729         sync; sync_all_data || true
3730
3731         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
3732         procf=${procf}.quota_slave.acct_user
3733         local acct_cnt
3734
3735         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
3736                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
3737         echo "Found $acct_cnt id entries"
3738
3739         [ $file_cnt -eq $acct_cnt ] || {
3740                 do_facet mds1 $LCTL get_param $procf
3741                 error "skipped id entries"
3742         }
3743 }
3744 run_test 38 "Quota accounting iterator doesn't skip id entries"
3745
3746 test_39() {
3747         local TESTFILE="$DIR/$tdir/project"
3748         ! is_project_quota_supported &&
3749                 skip "Project quota is not supported"
3750
3751         setup_quota_test || error "setup quota failed with $?"
3752
3753         touch $TESTFILE
3754         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3755         [ $projectid -ne 0 ] &&
3756                 error "Project id should be 0 not $projectid"
3757         change_project -p 1024 $TESTFILE
3758         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3759         [ $projectid -ne 1024 ] &&
3760                 error "Project id should be 1024 not $projectid"
3761
3762         stopall || error "failed to stopall (1)"
3763         mount
3764         setupall
3765         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3766         [ $projectid -eq 1024 ] ||
3767                 error "Project id should be 1024 not $projectid"
3768 }
3769 run_test 39 "Project ID interface works correctly"
3770
3771 test_40a() {
3772         ! is_project_quota_supported &&
3773                 skip "Project quota is not supported"
3774         local dir1="$DIR/$tdir/dir1"
3775         local dir2="$DIR/$tdir/dir2"
3776
3777         setup_quota_test || error "setup quota failed with $?"
3778
3779         mkdir -p $dir1 $dir2
3780         change_project -sp 1 $dir1 && touch $dir1/1
3781         change_project -sp 2 $dir2
3782
3783         ln $dir1/1 $dir2/1_link &&
3784                 error "Hard link across different project quota should fail"
3785         return 0
3786 }
3787 run_test 40a "Hard link across different project ID"
3788
3789 test_40b() {
3790         ! is_project_quota_supported &&
3791                 skip "Project quota is not supported"
3792         local dir1="$DIR/$tdir/dir1"
3793         local dir2="$DIR/$tdir/dir2"
3794
3795         setup_quota_test || error "setup quota failed with $?"
3796         mkdir -p $dir1 $dir2
3797         change_project -sp 1 $dir1 && touch $dir1/1
3798         change_project -sp 2 $dir2
3799
3800         mv $dir1/1 $dir2/2 || error "mv failed $?"
3801         local projid=$(lfs project $dir2/2 | awk '{print $1}')
3802         [ "$projid" -eq 2 ] || error "project id expected 2 not $projid"
3803 }
3804 run_test 40b "Mv across different project ID"
3805
3806 test_40c() {
3807         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
3808                 ! is_project_quota_supported &&
3809                         skip "Project quota is not supported"
3810
3811         setup_quota_test || error "setup quota failed with $?"
3812         local dir="$DIR/$tdir/dir"
3813
3814         mkdir -p $dir && change_project -sp 1 $dir
3815         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
3816         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
3817         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
3818         touch $dir/remote_dir/file
3819         #verify inherit works file for remote dir.
3820         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
3821         [ "$projid" != "1" ] &&
3822                 error "file under remote dir expected 1 not $projid"
3823
3824         #Agent inode should be ignored for project quota
3825         local used=$(getquota -p 1 global curinodes)
3826         [ $used -eq 3 ] ||
3827                 error "file count expected 3 got $used"
3828 }
3829 run_test 40c "Remote child Dir inherit project quota properly"
3830
3831 test_40d() {
3832         [ "$MDSCOUNT" -lt "2" ] && skip_env "needs >= 2 MDTs"
3833         is_project_quota_supported || skip "Project quota is not supported"
3834
3835         setup_quota_test || error "setup quota failed with $?"
3836         local dir="$DIR/$tdir/dir"
3837
3838         mkdir -p $dir
3839         $LFS setdirstripe -D -c 2 -i -1 $dir || error "setdirstripe failed"
3840         change_project -sp $TSTPRJID $dir ||
3841                 error "change project on $dir failed"
3842         for i in $(seq 5); do
3843                 mkdir -p $dir/d$i/d$i ||
3844                         error "mkdir $dir/d$i/d$i failed"
3845                 local projid=$($LFS project -d $dir/d$i/d$i |
3846                                awk '{print $1}')
3847                 [ "$projid" == "$TSTPRJID" ] ||
3848                         error "projid id expected $TSTPRJID not $projid"
3849                 touch $dir/d$i/d$i/file
3850                 #verify inherit works file for stripe dir.
3851                 local projid=$($LFS project -d $dir/d$i/d$i/file | awk '{print $1}')
3852                 [ "$projid" == "$TSTPRJID" ] ||
3853                         error "file under remote dir expected 1 not $projid"
3854         done
3855
3856         # account should be 1 + (2 + 1) *10 + 1 * 5
3857         local used=$(getquota -p $TSTPRJID global curinodes)
3858         [ $used -eq 36 ] ||
3859                 error "file count expected 36 got $used"
3860 }
3861 run_test 40d "Stripe Directory inherit project quota properly"
3862
3863 test_41() {
3864         is_project_quota_supported ||
3865                 skip "Project quota is not supported"
3866         setup_quota_test || error "setup quota failed with $?"
3867         local dir="$DIR/$tdir/dir"
3868         local blimit=102400
3869         local ilimit=4096
3870         local projid=$((testnum * 1000))
3871
3872         quota_init
3873
3874         # enable mdt/ost quota
3875         set_mdt_qtype ugp || error "enable mdt quota failed"
3876         set_ost_qtype ugp || error "enable ost quota failed"
3877
3878         local statfs_prj_orig=$($LCTL get_param -n llite.*.statfs_project)
3879         (( statfs_prj_orig == 1 )) ||
3880                 $LCTL set_param llite.*.statfs_project=1
3881         stack_trap "$LCTL set_param llite.*.statfs_project=$statfs_prj_orig"
3882
3883         test_mkdir -p $dir && change_project -sp $projid $dir
3884         $LFS setquota -p $projid -b 0 -B ${blimit}K -i 0 -I $ilimit $dir ||
3885                 error "set project quota failed"
3886
3887         sync; sync_all_data
3888         sleep_maxage
3889
3890         # check if df output works as expected
3891         echo "== global statfs: $MOUNT =="
3892         df -kP $MOUNT; df -iP $MOUNT; $LFS quota -p $projid $dir
3893         echo
3894         echo "== project statfs (prjid=$projid): $dir =="
3895         df -kP $dir; df -iP $dir
3896         local bused=$(getquota -p $projid global curspace)
3897         local iused=$(getquota -p $projid global curinodes)
3898         local expected="$ilimit$iused"
3899
3900         wait_update $HOSTNAME \
3901                 "df -iP $dir | awk \\\"/$FSNAME/\\\"'{print \\\$2 \\\$3}'" \
3902                 "$expected" ||
3903                 error "failed to get correct statfs for project quota"
3904
3905         expected=$(df -kP $dir | awk "/$FSNAME/"' {print $2}')
3906         (( expected == blimit )) ||
3907                 error "blimit mismatch: $expected != $blimit"
3908
3909         # zfs block size is 4K, while quota is printed in 1K, df result may be
3910         # larger than quota result, but it's no more than 3K
3911         expected=$(df -kP $dir | awk "/$FSNAME/"' {print $3}')
3912         (( expected - bused < 4)) || error "bused mismatch: $expected != $bused"
3913
3914         # disable statfs_project and check again
3915         $LCTL set_param llite.*.statfs_project=0
3916
3917         expected=$({ df -kP $MOUNT; df -iP $MOUNT; } | \
3918                 awk '/'$FSNAME'/ { printf "%d %d ", $2,$3 }')
3919
3920         wait_update $HOSTNAME \
3921                 "{ df -kP $dir; df -iP $dir; } |
3922                  awk '/$FSNAME/ { printf \\\"%d %d \\\", \\\$2,\\\$3 }'" \
3923                 "$expected" ||
3924                 error "failed to get correct statfs when statfs_project=0"
3925 }
3926 run_test 41 "df should return projid-specific values"
3927
3928 test_delete_qid()
3929 {
3930         local qslv_file=$1
3931         local qtype_file=$2
3932         local qtype=$3
3933         local qid=$4
3934         local osd="osd-ldiskfs"
3935
3936         [ "$ost1_FSTYPE" = zfs ] && osd="osd-zfs"
3937
3938         rm -f $DIR/$tdir/$tfile
3939         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile
3940         chmod a+rw $DIR/$tdir/$tfile
3941
3942         $LFS setquota $qtype $qid -B 300M $MOUNT
3943         $RUNAS $DD of=$DIR/$tdir/$tfile count=1 || error "failed to dd"
3944
3945         do_facet $SINGLEMDS \
3946                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3947                  grep -E 'id: *$qid'" || error "QMT: no qid $qid is found"
3948         echo $osd
3949         do_facet ost1 \
3950                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3951                  grep -E 'id: *$qid'" || error "QSD: no qid $qid is found"
3952
3953         $LFS setquota $qtype $qid --delete $MOUNT
3954         do_facet $SINGLEMDS \
3955                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3956                  grep -E 'id: *$qid'" && error "QMT: qid $qid is not deleted"
3957         sleep 5
3958         do_facet ost1 \
3959                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3960                  grep -E 'id: *$qid'" && error "QSD: qid $qid is not deleted"
3961
3962         $LFS setquota $qtype $qid -B 500M $MOUNT
3963         $RUNAS $DD of=$DIR/$tdir/$tfile count=1 || error "failed to dd"
3964         do_facet $SINGLEMDS \
3965                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3966                  grep -E 'id: *$qid'" || error "QMT: qid $pid is not recreated"
3967         cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file
3968         do_facet ost1 \
3969                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3970                  grep -E 'id: *$qid'" || error "QSD: qid $qid is not recreated"
3971 }
3972
3973 test_48()
3974 {
3975         setup_quota_test || error "setup quota failed with $?"
3976         set_ost_qtype $QTYPE || error "enable ost quota failed"
3977         quota_init
3978
3979         test_delete_qid "quota_slave/limit_user" "glb-usr" "-u" $TSTID
3980         test_delete_qid "quota_slave/limit_group" "glb-grp" "-g" $TSTID
3981         is_project_quota_supported &&
3982             test_delete_qid "quota_slave/limit_project" "glb-prj" "-p" "10000"
3983
3984         cleanup_quota_test
3985 }
3986 run_test 48 "lfs quota --delete should delete quota project ID"
3987
3988 test_50() {
3989         ! is_project_quota_supported &&
3990                 skip "Project quota is not supported"
3991
3992         setup_quota_test || error "setup quota failed with $?"
3993         local dir1="$DIR/$tdir/dir1"
3994         local dir2="$DIR/$tdir/dir2"
3995
3996         mkdir -p $dir1 && change_project -sp 1 $dir1
3997         mkdir -p $dir2 && change_project -sp 2 $dir2
3998         for num in $(seq 1 10); do
3999                 touch $dir1/file_$num $dir2/file_$num
4000                 ln -s $dir1/file_$num $dir1/file_$num"_link"
4001                 ln -s $dir2/file_$num $dir2/file_$num"_link"
4002         done
4003
4004         count=$($LFS find --projid 1 $DIR | wc -l)
4005         [ "$count" != 21 ] && error "expected 21 but got $count"
4006
4007         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
4008         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
4009         [ $count -eq 22 ] || error "expected 22 but got $count"
4010 }
4011 run_test 50 "Test if lfs find --projid works"
4012
4013 test_51() {
4014         ! is_project_quota_supported &&
4015                 skip "Project quota is not supported"
4016         setup_quota_test || error "setup quota failed with $?"
4017         local dir="$DIR/$tdir/dir"
4018
4019         mkdir $dir && change_project -sp 1 $dir
4020         local used=$(getquota -p 1 global curinodes)
4021         [ $used != "1" ] && error "expected 1 got $used"
4022
4023         touch $dir/1
4024         touch $dir/2
4025         cp $dir/2 $dir/3
4026         used=$(getquota -p 1 global curinodes)
4027         [ $used != "4" ] && error "expected 4 got $used"
4028
4029         $DD of=$DIR/$tdir/6 count=1
4030         #try cp to dir
4031         cp $DIR/$tdir/6 $dir/6
4032         used=$(getquota -p 1 global curinodes)
4033         [ $used != "5" ] && error "expected 5 got $used"
4034
4035         #try mv to dir
4036         mv $DIR/$tdir/6 $dir/7
4037         used=$(getquota -p 1 global curinodes)
4038         [ $used -eq 6 ] || error "expected 6 got $used"
4039 }
4040 run_test 51 "Test project accounting with mv/cp"
4041
4042 test_52() {
4043         ! is_project_quota_supported &&
4044                 skip "Project quota is not supported"
4045
4046         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
4047                 skip "Need MDS version at least 2.14.55"
4048
4049         setup_quota_test || error "setup quota failed with $?"
4050
4051         local dir1=$DIR/$tdir/t52_dir1
4052         local dir2=$DIR/$tdir/t52_dir2
4053
4054         mkdir $dir1 || error "failed to mkdir $dir1"
4055         mkdir $dir2 || error "failed to mkdir $dir2"
4056
4057         $LFS project -sp 1000 $dir1 || error "fail to set project on $dir1"
4058         $LFS project -sp 1001 $dir2 || error "fail to set project on $dir2"
4059
4060         $DD of=$dir1/$tfile count=100 ||
4061                 error "failed to create and write $dir1/$tfile"
4062
4063         cancel_lru_locks osc
4064         sync; sync_all_data || true
4065
4066         local attrs=($(lsattr -p $dir1/$tfile))
4067         (( ${attrs[0]} == 1000 )) ||
4068                 error "project ID on $dir1/$tfile is not inherited"
4069
4070         $LFS quota -p 1000 $DIR
4071         $LFS quota -p 1001 $DIR
4072
4073         local prev_used=$(getquota -p 1000 global curspace)
4074         local prev_used2=$(getquota -p 1001 global curspace)
4075
4076         mrename $dir1 $dir2/tdir || log "rename directory return $?"
4077
4078         local inum_before=$(ls -i $dir1/$tfile | awk '{print $1}')
4079         mrename $dir1/$tfile $dir2/$tfile || error "failed to rename file"
4080         local inum_after=$(ls -i $dir2/$tfile | awk '{print $1}')
4081
4082         attrs=($(lsattr -p $dir2/$tfile))
4083         (( ${attrs[0]} == 1001 )) ||
4084                 error "project ID is not updated after rename"
4085
4086         (( $inum_before == $inum_after )) ||
4087                 error "inode is changed after rename: $inum_before, $inum_after"
4088
4089         sync_all_data || true
4090
4091         $LFS quota -p 1000 $DIR
4092         $LFS quota -p 1001 $DIR
4093
4094         local new_used=$(getquota -p 1000 global curspace)
4095         local new_used2=$(getquota -p 1001 global curspace)
4096
4097         (( $prev_used >= $new_used + 102400 )) ||
4098                 error "quota is not deducted from old project ID"
4099         (( $prev_used2 <= $new_used2 - 102400 )) ||
4100                 error "quota is not added for the new project ID"
4101 }
4102 run_test 52 "Rename normal file across project ID"
4103
4104 test_53() {
4105         ! is_project_quota_supported &&
4106                 skip "Project quota is not supported"
4107         setup_quota_test || error "setup quota failed with $?"
4108         local dir="$DIR/$tdir/dir"
4109         mkdir $dir && change_project -s $dir
4110         [[ $($LFS project -d $dir) =~ " P " ]] ||
4111                 error "inherit attribute should be set"
4112
4113         change_project -C $dir
4114         [[ $($LFS project -d $dir) =~ " - " ]] ||
4115                 error "inherit attribute should be cleared"
4116 }
4117 run_test 53 "Project inherit attribute could be cleared"
4118
4119 test_54() {
4120         ! is_project_quota_supported &&
4121                 skip "Project quota is not supported"
4122         setup_quota_test || error "setup quota failed with $?"
4123         local testfile="$DIR/$tdir/$tfile-0"
4124
4125         #set project ID/inherit attribute
4126         change_project -sp $TSTPRJID $DIR/$tdir
4127         $RUNAS createmany -m ${testfile} 100 ||
4128                 error "create many files failed"
4129
4130         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
4131         # one more count for directory itself */
4132         ((proj_count++))
4133
4134         #check project
4135         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4136         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
4137
4138         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
4139         [ $proj_count1 -eq $proj_count ] ||
4140                         error "c2: expected $proj_count got $proj_count1"
4141
4142         #clear project but with kept projid
4143         change_project -rCk $DIR/$tdir
4144         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4145         [ $proj_count1 -eq 1 ] ||
4146                         error "c3: expected 1 got $proj_count1"
4147
4148         #verify projid untouched.
4149         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
4150         ((proj_count1++))
4151         [ $proj_count1 -eq $proj_count ] ||
4152                         error "c4: expected $proj_count got $proj_count1"
4153
4154         # test -0 option
4155         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
4156         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4157         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
4158
4159         #this time clear all
4160         change_project -rC $DIR/$tdir
4161         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
4162         [ $proj_count1 -eq 0 ] ||
4163                         error "c6: expected 0 got $proj_count1"
4164         #cleanup
4165         unlinkmany ${testfile} 100 ||
4166                 error "unlink many files failed"
4167 }
4168 run_test 54 "basic lfs project interface test"
4169
4170 test_55() {
4171         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
4172                 skip "Not supported before 2.10.58."
4173         setup_quota_test || error "setup quota failed with $?"
4174
4175         set_ost_qtype $QTYPE || error "enable ost quota failed"
4176         quota_init
4177
4178         #add second group to TSTUSR
4179         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
4180
4181         #prepare test file
4182         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
4183         error "failed to dd"
4184
4185         cancel_lru_locks osc
4186         sync; sync_all_data || true
4187
4188         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
4189         error "failed to setquota on group $TSTUSR2"
4190
4191         $LFS quota -v -g $TSTUSR2 $DIR
4192
4193         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
4194         error "chgrp should failed with -EDQUOT"
4195
4196         USED=$(getquota -g $TSTUSR2 global curspace)
4197         echo "$USED"
4198
4199         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
4200         error "failed to setquota on group $TSTUSR2"
4201
4202         $LFS quota -v -g $TSTUSR2 $DIR
4203
4204         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
4205         error "chgrp should succeed"
4206
4207         $LFS quota -v -g $TSTUSR2 $DIR
4208 }
4209 run_test 55 "Chgrp should be affected by group quota"
4210
4211 test_56() {
4212         setup_quota_test || error "setup quota failed with $?"
4213
4214         set_ost_qtype $QTYPE || error "enable ost quota failed"
4215         quota_init
4216
4217         $LFS setquota -t -u -b 10 -i 10 $DIR ||
4218                 erro "failed to set grace time for usr quota"
4219         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
4220                      awk '{print $4 $8}')
4221         if [ "x$grace_time" != "x10s;10s" ]; then
4222                 $LFS quota -t -u $DIR
4223                 error "expected grace time: 10s;10s, got:$grace_time"
4224         fi
4225 }
4226 run_test 56 "lfs quota -t should work well"
4227
4228 test_57() {
4229         setup_quota_test || error "setup quota failed with $?"
4230
4231         local dir="$DIR/$tdir/dir"
4232         mkdir -p $dir
4233         mkfifo $dir/pipe
4234         #command can process further if it hit some errors
4235         $LFS project -sp 1 $dir/pipe
4236         touch $dir/aaa $dir/bbb
4237         mkdir $dir/subdir -p
4238         touch $dir/subdir/aaa $dir/subdir/bbb
4239         #create one invalid link file
4240         ln -s $dir/not_exist_file $dir/ccc
4241         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
4242         [ $cnt -eq 7 ] || error "expected 7 got $cnt"
4243 }
4244 run_test 57 "lfs project could tolerate errors"
4245
4246 # LU-16988
4247 test_mirror()
4248 {
4249         local projid=$1
4250         local testfile=$2
4251         local mirrorfile=$3
4252
4253         # create mirror
4254         $LFS mirror extend -N2 $mirrorfile || error "failed to create mirror"
4255
4256         local mirrors=$($LFS getstripe -N $testfile)
4257         [[ $mirrors == 3 ]] || error "mirror count $mirrors is wrong"
4258
4259         cancel_lru_locks osc
4260         cancel_lru_locks mdc
4261         sync; sync_all_data || true
4262
4263         local prev_usage=$(getquota -p $projid global curspace)
4264
4265         $RUNAS $DD of=$testfile count=50 conv=nocreat oflag=direct ||
4266                         quota_error p $projid "write failed, expect succeed"
4267
4268         cancel_lru_locks osc
4269         cancel_lru_locks mdc
4270         sync; sync_all_data || true
4271
4272         $RUNAS $LFS mirror resync $testfile || error "failed to resync mirror"
4273
4274         local usage=$(getquota -p $projid global curspace)
4275         (( usage >= prev_usage + 150*1024 )) ||
4276                                 error "project quota $usage is wrong"
4277
4278         $RUNAS $DD of=$testfile count=30 conv=nocreat seek=50 oflag=direct ||
4279                         quota_error p $projid "write failed, expect succeed"
4280
4281         $RUNAS $LFS mirror resync $testfile &&
4282                         error "resync mirror succeed, expect EDQUOT"
4283
4284         $LFS mirror delete --mirror-id 2 $testfile ||
4285                         error "failed to delete the second mirror"
4286         $LFS mirror delete --mirror-id 3 $testfile ||
4287                         error "failed to delete the third mirror"
4288 }
4289
4290 test_58() {
4291         (( $MDS1_VERSION >= $(version_code 2.15.56) )) ||
4292                 skip "need MDS 2.15.56 or later"
4293
4294         is_project_quota_supported || skip "Project quota is not supported"
4295
4296         local testdir="$DIR/$tdir"
4297         local testfile="$DIR/$tdir/$tfile"
4298         local projid=1000
4299         local projid2=1001
4300
4301         setup_quota_test || error "setup quota failed with $?"
4302
4303         USED=$(getquota -p $projid global curspace)
4304         [ $USED -ne 0 ] && error "Used space ($USED) for proj $projid isn't 0"
4305
4306         USED=$(getquota -p $projid2 global curspace)
4307         [ $USED -ne 0 ] && error "Used space ($USED) for proj $projid2 isn't 0"
4308
4309         chown $TSTUSR.$TSTUSR $testdir || error "chown $testdir failed"
4310         quota_init
4311         set_ost_qtype ugp || error "enable ost quota failed"
4312
4313         $LFS project -sp $projid $testdir || error "failed to set project ID"
4314         $LFS setquota -p $projid -B 200M $DIR ||
4315                                 error "failed to to set prj $projid quota"
4316
4317         $RUNAS touch $testfile
4318
4319         local id=$(lfs project -d $testfile | awk '{print $1}')
4320         [ "$id" != "$projid" ] && error "projid $projid is not inherited $id"
4321
4322         echo "test by mirror created with normal file"
4323         test_mirror $projid $testfile $testfile
4324
4325         $TRUNCATE $testfile 0
4326         wait_delete_completed || error "wait_delete_completed failed"
4327         sync_all_data || true
4328
4329         $LFS project -sp $projid2 $testdir ||
4330                                 error "failed to set directory project ID"
4331         $LFS project -p $projid2 $testfile ||
4332                                 error "failed to set file project ID"
4333         $LFS setquota -p $projid -b 0 -B 0 $DIR ||
4334                                 error "failed to to reset prj quota"
4335         $LFS setquota -p $projid2 -B 200M $DIR ||
4336                                 error "failed to to set prj $projid2 quota"
4337
4338         local fid=$($LFS path2fid $testfile)
4339
4340         echo "test by mirror created with FID"
4341         test_mirror $projid2 $testfile $MOUNT/.lustre/fid/$fid
4342 }
4343 run_test 58 "project ID should be kept for new mirrors created by FID"
4344
4345 test_59() {
4346         [ "$mds1_FSTYPE" != ldiskfs ] &&
4347                 skip "ldiskfs only test"
4348         disable_project_quota
4349         setup_quota_test || error "setup quota failed with $?"
4350         quota_init
4351
4352         local testfile="$DIR/$tdir/$tfile-0"
4353         #make sure it did not crash kernel
4354         touch $testfile && lfs project -sp 1 $testfile
4355
4356         enable_project_quota
4357 }
4358 run_test 59 "lfs project dosen't crash kernel with project disabled"
4359
4360 test_60() {
4361         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
4362                 skip "Needs MDS version 2.11.53 or later."
4363         setup_quota_test || error "setup quota failed with $?"
4364
4365         local testfile=$DIR/$tdir/$tfile
4366         local limit=100
4367
4368         set_mdt_qtype "ug" || error "enable mdt quota failed"
4369
4370         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
4371                 error "set quota failed"
4372         quota_show_check a g $TSTUSR
4373
4374         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
4375         chmod g+s $DIR/$tdir || error "chmod g+s failed"
4376         $RUNAS createmany -m ${testfile} $((limit-1)) ||
4377                 error "create many files failed"
4378
4379         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
4380
4381         # root user can overrun quota
4382         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
4383                 error "root user should succeed"
4384 }
4385 run_test 60 "Test quota for root with setgid"
4386
4387 # test default quota
4388 test_default_quota() {
4389         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
4390                 skip "Not supported before 2.11.51."
4391
4392         local qtype=$1
4393         local qres_type=$2
4394         local qid=$TSTUSR
4395         local qprjid=$TSTPRJID
4396         local qdtype="-U"
4397         local qs="-b"
4398         local qh="-B"
4399         local LIMIT=20480 #20M disk space
4400         local TESTFILE="$DIR/$tdir/$tfile-0"
4401         local $qpool_cmd
4402
4403         [ $qtype == "-p" ] && ! is_project_quota_supported &&
4404                 echo "Project quota is not supported" && return 0
4405
4406         [ $qtype == "-u" ] && qdtype="-U"
4407         [ $qtype == "-g" ] && qdtype="-G"
4408         [ $qtype == "-p" ] && {
4409                 qdtype="-P"
4410                 qid=$qprjid
4411         }
4412
4413         [ $qres_type == "meta" ] && {
4414                 LIMIT=10240 #10K inodes
4415                 qs="-i"
4416                 qh="-I"
4417         }
4418         [ ! -z "$3" ] && {
4419                 qpool_cmd="--pool $3"
4420                 # pool quotas don't work properly without global limit
4421                 $LFS setquota $qtype $qid -B1T -b1T $DIR ||
4422                         error "set global limit failed"
4423         }
4424
4425         setup_quota_test || error "setup quota failed with $?"
4426
4427         quota_init
4428
4429         # enable mdt/ost quota
4430         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4431         set_ost_qtype $QTYPE || error "enable ost quota failed"
4432
4433         log "set to use default quota"
4434         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4435                 error "set $qid to use default quota failed"
4436
4437         log "set default quota"
4438         $LFS setquota $qdtype $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4439                 error "set $qid default quota failed"
4440
4441         log "get default quota"
4442         $LFS quota $qdtype $DIR || error "get default quota failed"
4443
4444         if [ $qres_type == "data" ]; then
4445                 local SLIMIT=$($LFS quota $qpool_cmd $qdtype $DIR | \
4446                                 grep "$MOUNT" | awk '{print $2}')
4447                 [ $SLIMIT -eq $LIMIT ] ||
4448                         error "the returned default quota is wrong"
4449         else
4450                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
4451                                                         awk '{print $5}')
4452                 [ $SLIMIT -eq $LIMIT ] ||
4453                         error "the returned default quota is wrong"
4454         fi
4455
4456         # make sure the system is clean
4457         local USED=$(getquota $qtype $qid global curspace)
4458         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4459
4460         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4461                         error "setstripe $TESTFILE failed"
4462         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4463
4464         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
4465
4466         log "Test not out of quota"
4467         if [ $qres_type == "data" ]; then
4468                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
4469                         quota_error $qtype $qid "write failed, expect succeed"
4470         else
4471                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
4472                         quota_error $qtype $qid "create failed, expect succeed"
4473
4474                 unlinkmany $TESTFILE $((LIMIT/2))
4475         fi
4476
4477         log "Test out of quota"
4478         # flush cache, ensure noquota flag is set on client
4479         cancel_lru_locks osc
4480         cancel_lru_locks mdc
4481         sync; sync_all_data || true
4482         if [ $qres_type == "data" ]; then
4483                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4484                         quota_error $qtype $qid "write succeed, expect EDQUOT"
4485         else
4486                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4487                         quota_error $qtype $qid "create succeed, expect EDQUOT"
4488
4489                 unlinkmany $TESTFILE $((LIMIT*2))
4490         fi
4491
4492         rm -f $TESTFILE
4493         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4494                         error "setstripe $TESTFILE failed"
4495         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4496
4497         log "Increase default quota"
4498
4499         # LU-4505: sleep 5 seconds to enable quota acquire
4500         sleep 5
4501
4502         # increase default quota
4503         $LFS setquota $qdtype $qpool_cmd $qs $((LIMIT*3)) \
4504                 $qh $((LIMIT*3)) $DIR || error "set default quota failed"
4505
4506         cancel_lru_locks osc
4507         cancel_lru_locks mdc
4508         sync; sync_all_data || true
4509         if [ $qres_type == "data" ]; then
4510                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4511                         quota_error $qtype $qid "write failed, expect succeed"
4512         else
4513                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4514                         quota_error $qtype $qid "create failed, expect succeed"
4515
4516                 unlinkmany $TESTFILE $((LIMIT*2))
4517         fi
4518
4519         log "Set quota to override default quota"
4520         $LFS setquota $qtype $qid $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4521                 error "set $qid quota failed"
4522
4523         cancel_lru_locks osc
4524         cancel_lru_locks mdc
4525         sync; sync_all_data || true
4526         if [ $qres_type == "data" ]; then
4527                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4528                         quota_error $qtype $qid "write succeed, expect EQUOT"
4529         else
4530                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4531                         quota_error $qtype $qid "create succeed, expect EQUOT"
4532
4533                 unlinkmany $TESTFILE $((LIMIT*2))
4534         fi
4535
4536         log "Set to use default quota again"
4537
4538         # LU-4505: sleep 5 seconds to enable quota acquire
4539         sleep 5
4540
4541         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4542                 error "set $qid to use default quota failed"
4543
4544         cancel_lru_locks osc
4545         cancel_lru_locks mdc
4546         sync; sync_all_data || true
4547         if [ $qres_type == "data" ]; then
4548                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4549                         quota_error $qtype $qid "write failed, expect succeed"
4550         else
4551                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4552                         quota_error $qtype $qid "create failed, expect succeed"
4553
4554                 unlinkmany $TESTFILE $((LIMIT*2))
4555         fi
4556
4557         log "Cleanup"
4558         rm -f $TESTFILE
4559         wait_delete_completed || error "wait_delete_completed failed"
4560         sync_all_data || true
4561
4562         $LFS setquota $qdtype $qpool_cmd $qs 0 $qh 0 $DIR ||
4563                 error "reset default quota failed"
4564         $LFS setquota $qtype $qid $qpool_cmd $qs 0 $qh 0 $DIR ||
4565                 error "reset quota failed"
4566         cleanup_quota_test
4567 }
4568
4569 test_61() {
4570         test_default_quota "-u" "data"
4571         test_default_quota "-u" "meta"
4572         test_default_quota "-g" "data"
4573         test_default_quota "-g" "meta"
4574         test_default_quota "-p" "data"
4575         test_default_quota "-p" "meta"
4576 }
4577 run_test 61 "default quota tests"
4578
4579 test_62() {
4580         ! is_project_quota_supported &&
4581                 skip "Project quota is not supported"
4582         [[ "$(chattr -h 2>&1)" =~ "project" ||
4583            "$(chattr -h 2>&1)" =~ "pRVf" ]] ||
4584                 skip "chattr did not support project quota"
4585         setup_quota_test || error "setup quota failed with $?"
4586         local testdir=$DIR/$tdir/
4587
4588         $RUNAS mkdir -p $testdir || error "failed to mkdir"
4589         change_project -s $testdir
4590         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4591                 error "inherit attribute should be set"
4592         # chattr used FS_IOC_SETFLAGS ioctl
4593         $RUNAS chattr -P $testdir &&
4594                 error "regular user clear inherit should fail"
4595         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4596                 error "inherit attribute should still be set"
4597         chattr -P $testdir || error "root failed to clear inherit"
4598         [[ $($LFS project -d $testdir) =~ "P" ]] &&
4599                 error "inherit attribute should be cleared"
4600         return 0
4601 }
4602 run_test 62 "Project inherit should be only changed by root"
4603
4604 test_dom() {
4605         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
4606                 skip "Not supported before 2.11.55"
4607
4608         local qtype=$1
4609         local qid=$TSTUSR
4610         local dd_failed=false
4611         local tdir_dom=${tdir}_dom
4612         local LIMIT=20480 #20M
4613
4614         [ $qtype == "p" ] && ! is_project_quota_supported &&
4615                 echo "Project quota is not supported" && return 0
4616
4617         [ $qtype == "p" ] && qid=$TSTPRJID
4618
4619         setup_quota_test || error "setup quota failed with $?"
4620
4621         quota_init
4622
4623         # enable mdt/ost quota
4624         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4625         set_ost_qtype $QTYPE || error "enable ost quota failed"
4626
4627         # make sure the system is clean
4628         local USED=$(getquota -$qtype $qid global curspace)
4629         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4630
4631         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
4632
4633         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
4634         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
4635                 error "setstripe $tdir_dom failed"
4636         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
4637
4638         [ $qtype == "p" ] && {
4639                 change_project -sp $TSTPRJID $DIR/$tdir
4640                 change_project -sp $TSTPRJID $DIR/$tdir_dom
4641         }
4642
4643         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
4644                 error "set $qid quota failed"
4645
4646         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4647                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4648                                                                 dd_failed=true
4649         done
4650
4651         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4652
4653         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
4654                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4655                                                                 dd_failed=true
4656         done
4657
4658         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4659
4660         rm -f $DIR/$tdir_dom/*
4661
4662         # flush cache, ensure noquota flag is set on client
4663         cancel_lru_locks osc
4664         cancel_lru_locks mdc
4665         sync; sync_all_data || true
4666
4667         dd_failed=false
4668
4669         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4670                 quota_error $qtype $qid "write failed, expect succeed"
4671
4672         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4673                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4674                                                                 dd_failed=true
4675         done
4676
4677         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4678
4679         rm -f $DIR/$tdir/*
4680         rm -f $DIR/$tdir_dom/*
4681
4682         # flush cache, ensure noquota flag is set on client
4683         cancel_lru_locks osc
4684         cancel_lru_locks mdc
4685         sync; sync_all_data || true
4686
4687         dd_failed=false
4688
4689         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4690                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4691                                                                 dd_failed=true
4692         done
4693
4694         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4695
4696         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4697                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4698
4699         rm -fr $DIR/$tdir
4700         rm -fr $DIR/$tdir_dom
4701
4702         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4703                 error "reset usr quota failed"
4704 }
4705
4706 test_63() {
4707         test_dom "u"
4708         test_dom "g"
4709         test_dom "p"
4710 }
4711 run_test 63 "quota on DoM tests"
4712
4713 test_64() {
4714         ! is_project_quota_supported &&
4715                 skip "Project quota is not supported"
4716         setup_quota_test || error "setup quota failed with $?"
4717         local dir1="$DIR/$tdir/"
4718
4719         touch $dir1/file
4720         ln -s $dir1/file $dir1/file_link
4721         mkfifo $dir1/fifo
4722
4723         $LFS project -srp $TSTPRJID $dir1 >&/dev/null ||
4724                 error "set project should succeed"
4725
4726         used=$(getquota -p $TSTPRJID global curinodes)
4727         [ $used -eq 4 ] || error "expected 4 got $used"
4728         $LFS project -rC $dir1 >&/dev/null ||
4729                 error "clear project should succeed"
4730
4731         used=$(getquota -p $TSTPRJID global curinodes)
4732         [ $used -eq 0 ] || error "expected 0 got $used"
4733 }
4734 run_test 64 "lfs project on non dir/files should succeed"
4735
4736 test_65() {
4737         local SIZE=10 # MB
4738         local TESTFILE="$DIR/$tdir/$tfile-0"
4739
4740         setup_quota_test || error "setup quota failed with $?"
4741         set_ost_qtype $QTYPE || error "enable ost quota failed"
4742         quota_init
4743
4744         echo "Write..."
4745         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4746                 error "failed to write"
4747         # flush cache, ensure noquota flag is set on client
4748         cancel_lru_locks osc
4749         sync; sync_all_data || true
4750
4751         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4752         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4753         local quota_all=$($RUNAS $LFS quota $DIR)
4754
4755         [ "$(echo "$quota_all" | head -n3)" == "$quota_u" ] ||
4756                 error "usr quota not match"
4757         [ "$(echo "$quota_all" | tail -n3)" == "$quota_g" ] ||
4758                 error "grp quota not match"
4759 }
4760 run_test 65 "Check lfs quota result"
4761
4762 test_66() {
4763         ! is_project_quota_supported &&
4764                 skip "Project quota is not supported"
4765         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4766                 skip "Not supported before 2.12.4"
4767         setup_quota_test || error "setup quota failed with $?"
4768         local old=$(do_facet mds1 $LCTL get_param -n \
4769                     mdt.*.enable_chprojid_gid | head -1)
4770         local testdir=$DIR/$tdir/foo
4771
4772         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4773         stack_trap "do_facet mds1 $LCTL \
4774                 set_param mdt.*.enable_chprojid_gid=$old" EXIT
4775
4776         mkdir_on_mdt0 $testdir || error "failed to mkdir"
4777         chown -R $TSTID:$TSTID $testdir
4778         change_project -sp $TSTPRJID $testdir
4779         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4780
4781         $RUNAS lfs project -p 0 $testdir/foo &&
4782                 error "nonroot user should fail to set projid"
4783
4784         $RUNAS lfs project -C $testdir/foo &&
4785                 error "nonroot user should fail to clear projid"
4786
4787         change_project -C $testdir/foo || error "failed to clear project"
4788
4789         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4790         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4791         "failed to set projid with normal user when enable_chprojid_gid=-1"
4792
4793         $RUNAS lfs project -rC $testdir/ || error \
4794 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4795
4796         touch $testdir/bar || error "failed touch $testdir/bar"
4797         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4798         "normal user should not be able to set projid on root owned file"
4799
4800         change_project -p $TSTPRJID $testdir/bar || error \
4801                 "root should be able to change its own file's projid"
4802 }
4803 run_test 66 "nonroot user can not change project state in default"
4804
4805 test_67_write() {
4806         local file="$1"
4807         local qtype="$2"
4808         local size=$3
4809         local _runas=""
4810         local short_qtype=${qtype:0:1}
4811
4812         echo "file "$file
4813         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4814         case "$4" in
4815                 $TSTUSR)  _runas=$RUNAS;;
4816                 $TSTUSR2) _runas=$RUNAS2;;
4817                 *)          error "unknown quota parameter $4";;
4818         esac
4819
4820         log "Write..."
4821         date
4822         $_runas $DD of=$file count=$size ||
4823                 quota_error $short_qtype $TSTUSR \
4824                         "$qtype write failure, but expect success"
4825         date
4826         cancel_lru_locks osc
4827         date
4828         sync; sync_all_data || true
4829         date
4830 }
4831
4832 getgranted() {
4833         local pool=$1
4834         local ptype=$2
4835         local userid=$3
4836         local qtype=$4
4837         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4838
4839         do_facet mds1 $LCTL get_param $param |
4840                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4841 }
4842
4843 test_67() {
4844         local limit=20 # MB
4845         local testfile="$DIR/$tdir/$tfile-0"
4846         local testfile2="$DIR/$tdir/$tfile-1"
4847         local testfile3="$DIR/$tdir/$tfile-2"
4848         local qpool="qpool1"
4849         local used
4850         local granted
4851         local granted_mb
4852
4853         mds_supports_qp
4854         [ "$ost1_FSTYPE" == zfs ] &&
4855                 skip "ZFS grants some block space together with inode"
4856
4857         setup_quota_test || error "setup quota failed with $?"
4858
4859         # enable ost quota
4860         set_ost_qtype $QTYPE || error "enable ost quota failed"
4861
4862         # test for user
4863         log "User quota (block hardlimit:$limit MB)"
4864         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4865                 error "set user quota failed"
4866
4867         # make sure the system is clean
4868         used=$(getquota -u $TSTUSR global curspace)
4869         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4870
4871         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4872         echo "granted 0x0 before write $granted"
4873
4874         # trigger reintegration
4875         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4876         procf=${procf}quota_slave.force_reint
4877         do_facet ost1 $LCTL set_param $procf=1 ||
4878                 error "force reintegration failed"
4879         wait_ost_reint "u" || error "reintegration failed"
4880         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4881         [ $granted -ne 0 ] &&
4882                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4883
4884         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4885         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4886
4887         # write 10 MB to testfile
4888         test_67_write "$testfile" "user" 10 "$TSTUSR"
4889
4890         # create qpool and add OST1
4891         pool_add $qpool || error "pool_add failed"
4892         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4893         # as $TSTUSR hasn't limits, lqe may absent. But it should be
4894         # created after the 1st direct qmt_get.
4895         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4896
4897         # check granted - should be 0, as testfile is located only on OST0
4898         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4899         echo "global granted $granted"
4900         granted=$(getgranted $qpool "dt" $TSTID "usr")
4901         echo "$qpool granted $granted"
4902         [ $granted -ne 0 ] &&
4903                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4904
4905         # add OST0 to qpool and check granted space
4906         pool_add_targets $qpool 0 1 ||
4907                 error "pool_add_targets failed"
4908         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4909         echo "Granted $granted_mb MB"
4910         #should be 10M + qunit for each OST
4911         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4912                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4913
4914         $LFS setstripe $testfile2 -c 1 -i 1 ||
4915                 error "setstripe $testfile2 failed"
4916         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4917         # Write from another user and check that qpool1
4918         # shows correct granted, despite $TSTUSR2 hasn't limits in qpool1.
4919         test_67_write "$testfile2" "user" 10 "$TSTUSR2"
4920         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4921         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4922         [ $granted -ne 0 ] &&
4923                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4924
4925         # Granted space for $TSTUSR2 in qpool1 should appear only
4926         # when global lqe for this user becomes enforced.
4927         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4928                 error "set user quota failed"
4929         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4930         echo "granted_mb $granted_mb"
4931         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4932                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4933
4934         $LFS setstripe $testfile3 -c 1 -i 0 ||
4935                 error "setstripe $testfile3 failed"
4936         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4937         test_67_write "$testfile3" "user" 10 "$TSTUSR2"
4938         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4939         echo "$testfile3 granted_mb $granted_mb"
4940         [ $granted_mb -eq $limit ] ||
4941                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4942
4943         # remove OST1 from the qpool1 and check granted space
4944         # should be 0 for TSTUSR and 10M for TSTUSR2
4945         pool_remove_target $qpool 0
4946         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4947         [ $granted_mb -eq 0 ] ||
4948                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4949         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4950         [ $granted_mb -eq 10 ] ||
4951                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4952
4953         rm -f $testfile
4954         wait_delete_completed || error "wait_delete_completed failed"
4955         sync_all_data || true
4956         used=$(getquota -u $TSTUSR global curspace)
4957         [ $used -eq 0 ] || quota_error u $TSTUSR \
4958                 "user quota isn't released after deletion"
4959 }
4960 run_test 67 "quota pools recalculation"
4961
4962 get_slave_nr() {
4963         local pool=$1
4964         local qtype=$2
4965         local nr
4966
4967         wait_update_facet "--quiet" mds1 \
4968                 "$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info \
4969                         >/dev/null 2>&1 || echo foo" "">/dev/null ||
4970         error "mds1: failed to create quota pool $pool"
4971
4972         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4973                 awk '/usr/ {getline; print $2}'
4974 }
4975
4976 test_68()
4977 {
4978         local qpool="qpool1"
4979
4980         mds_supports_qp
4981         setup_quota_test || error "setup quota failed with $?"
4982
4983         # enable ost quota
4984         set_ost_qtype $QTYPE || error "enable ost quota failed"
4985
4986         # check slave number for glbal pool
4987         local nr=$(get_slave_nr "0x0" "usr")
4988         echo "nr result $nr"
4989         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4990                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4991
4992         # create qpool and add OST1
4993         pool_add $qpool || error "pool_add failed"
4994         nr=$(get_slave_nr $qpool "usr")
4995         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4996
4997         # add OST1 to qpool
4998         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4999         nr=$(get_slave_nr $qpool "usr")
5000         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
5001
5002         # add OST0 to qpool
5003         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
5004         nr=$(get_slave_nr $qpool "usr")
5005         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
5006
5007         # remove OST0
5008         pool_remove_target $qpool 0
5009         nr=$(get_slave_nr $qpool "usr")
5010         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
5011
5012         # remove OST1
5013         pool_remove_target $qpool 1
5014         nr=$(get_slave_nr $qpool "usr")
5015         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
5016
5017         # Check again that all is fine with global pool
5018         nr=$(get_slave_nr "0x0" "usr")
5019         [[ $nr == $((OSTCOUNT + MDSCOUNT)) ]] ||
5020                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
5021 }
5022 run_test 68 "slave number in quota pool changed after each add/remove OST"
5023
5024 test_69()
5025 {
5026         local global_limit=200 # MB
5027         local limit=10 # MB
5028         local testfile="$DIR/$tdir/$tfile-0"
5029         local dom0="$DIR/$tdir/dom0"
5030         local qpool="qpool1"
5031
5032         mds_supports_qp
5033         setup_quota_test || error "setup quota failed with $?"
5034
5035         # enable ost quota
5036         set_ost_qtype $QTYPE || error "enable ost quota failed"
5037         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
5038
5039         # Save DOM only at MDT0
5040         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
5041         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
5042         chmod 0777 $dom0
5043         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
5044
5045         # create qpool and add OST0
5046         pool_add $qpool || error "pool_add failed"
5047         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
5048
5049         log "User quota (block hardlimit:$global_limit MB)"
5050         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5051                 error "set user quota failed"
5052
5053         log "User quota (block hardlimit:$limit MB)"
5054         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5055                 error "set user quota failed"
5056
5057         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
5058                 quota_error u $TSTUSR "write failed"
5059
5060         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
5061                 oflag=sync || quota_error u $TSTUSR "write failed"
5062
5063         $RUNAS $DD of=$testfile count=$limit || true
5064
5065         # flush cache, ensure noquota flag is set on client
5066         cancel_lru_locks osc
5067         sync; sync_all_data || true
5068
5069         # MDT0 shouldn't get EDQUOT with glimpse.
5070         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
5071                 quota_error u $TSTUSR \
5072                         "user write success, but expect EDQUOT"
5073
5074         # Now all members of qpool1 should get EDQUOT. Expect success
5075         # when write to DOM on MDT0, as it belongs to global pool.
5076         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
5077                 oflag=sync || quota_error u $TSTUSR "write failed"
5078
5079         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
5080                 oflag=sync || quota_error u $TSTUSR "write failed"
5081 }
5082 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
5083
5084 test_70a()
5085 {
5086         local qpool="qpool1"
5087         local limit=20 # MB
5088         local err=0
5089         local bhard
5090
5091         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
5092                 skip "Needs a client >= $VERSION_WITH_QP"
5093
5094         setup_quota_test || error "setup quota failed with $?"
5095
5096         # MDS returns EFAULT for unsupported quotactl command
5097         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
5098
5099         # create qpool and add OST0
5100         pool_add $qpool || error "pool_add failed"
5101         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
5102
5103         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
5104         rc=$?
5105         [ $rc -eq $err ] || error "setquota res $rc != $err"
5106
5107         # If MDS supports QP, check that limit was set properly.
5108         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
5109                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
5110                 echo "hard limit $bhard limit $limit"
5111                 [ $bhard -ne $((limit*1024)) ] &&
5112                         error "bhard:$bhard for $qpool!=$((limit*1024))"
5113         fi
5114
5115         $LFS quota -u $TSTUSR --pool $qpool $DIR
5116         rc=$?
5117         [ $rc -eq $err ] || error "quota res $rc != $err"
5118 }
5119 run_test 70a "check lfs setquota/quota with a pool option"
5120
5121 test_70b()
5122 {
5123         local glbl_hard=200 # 200M
5124         local glbl_soft=100 # 100M
5125         local pool_hard=10 # 10M
5126         local qpool="qpool1"
5127
5128         pool_add $qpool || error "pool_add failed"
5129         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
5130
5131         $LFS setquota -u $TSTUSR -b ${glbl_soft}M -B ${glbl_hard}M $DIR ||
5132                 error "set user quota failed"
5133         $LFS setquota -u $TSTUSR -B ${pool_hard}M --pool $qpool $DIR ||
5134                 error "set user quota failed"
5135
5136         local tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5137         [ $tmp -eq $((pool_hard * 1024)) ] ||
5138                 error "wrong block hard limit $tmp for $qpool"
5139         local tmp=$(getquota -u $TSTUSR global bsoftlimit $qpool)
5140         # soft limit hasn't been set and should be zero
5141         [ $tmp -eq 0 ] || error "wrong soft block limit $tmp for $qpool"
5142 }
5143 run_test 70b "lfs setquota pool works properly"
5144
5145 test_71a()
5146 {
5147         local limit=10 # MB
5148         local global_limit=100 # MB
5149         local testfile="$DIR/$tdir/$tfile-0"
5150         local qpool="qpool1"
5151         local qpool2="qpool2"
5152
5153         [ "$ost1_FSTYPE" == zfs ] &&
5154                 skip "ZFS grants some block space together with inode"
5155         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
5156         mds_supports_qp
5157         setup_quota_test || error "setup quota failed with $?"
5158
5159         # enable ost quota
5160         set_ost_qtype $QTYPE || error "enable ost quota failed"
5161
5162         # test for user
5163         log "User quota (block hardlimit:$global_limit MB)"
5164         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5165                 error "set user quota failed"
5166
5167         pool_add $qpool || error "pool_add failed"
5168         pool_add_targets $qpool 0 1 ||
5169                 error "pool_add_targets failed"
5170
5171         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5172                 error "set user quota failed"
5173
5174         pool_add $qpool2 || error "pool_add failed"
5175         pool_add_targets $qpool2 1 1 ||
5176                 error "pool_add_targets failed"
5177
5178         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
5179                 error "set user quota failed"
5180
5181         # make sure the system is clean
5182         local used=$(getquota -u $TSTUSR global curspace)
5183
5184         echo "used $used"
5185         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5186
5187         # create 1st component 1-10M
5188         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5189         #create 2nd component 10-30M
5190         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5191         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5192
5193         # сheck normal use and out of quota with PFL
5194         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
5195         test_1_check_write $testfile "user" $((limit*2))
5196         rm -f $testfile
5197         wait_delete_completed || error "wait_delete_completed failed"
5198         sync_all_data || true
5199         used=$(getquota -u $TSTUSR global curspace)
5200         [ $used -ne 0 ] && quota_error u $TSTUSR \
5201                 "user quota isn't released after deletion"
5202
5203         # create 1st component 1-10M
5204         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5205         # create 2nd component 10-30M
5206         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5207         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5208
5209         # write to the 2nd component
5210         $RUNAS $DD of=$testfile count=$limit seek=10 ||
5211                 quota_error u $TSTUSR \
5212                         "write failure, but expect success"
5213         # this time maybe cache write,  ignore it's failure
5214         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
5215         cancel_lru_locks osc
5216         sync; sync_all_data || true
5217         # write over limit in qpool2(2nd component 10-30M)
5218         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
5219                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5220         # write to the 1st component - OST0 is empty
5221         $RUNAS $DD of=$testfile count=$limit seek=0 ||
5222                 quota_error u $TSTUSR "write failed"
5223 }
5224 run_test 71a "Check PFL with quota pools"
5225
5226 test_71b()
5227 {
5228         local global_limit=1000 # MB
5229         local limit1=160 # MB
5230         local limit2=10 # MB
5231         local testfile="$DIR/$tdir/$tfile-0"
5232         local qpool="qpool1"
5233         local qpool2="qpool2"
5234
5235         [ "$ost1_FSTYPE" == zfs ] &&
5236                 skip "ZFS grants some block space together with inode"
5237         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
5238         mds_supports_qp
5239         setup_quota_test || error "setup quota failed with $?"
5240
5241         # enable ost quota
5242         set_ost_qtype $QTYPE || error "enable ost quota failed"
5243
5244         # test for user
5245         log "User quota (block hardlimit:$global_limit MB)"
5246         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5247                 error "set user quota failed"
5248
5249         pool_add $qpool || error "pool_add failed"
5250         pool_add_targets $qpool 0 1 ||
5251                 error "pool_add_targets failed"
5252
5253         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
5254                 error "set user quota failed"
5255
5256         pool_add $qpool2 || error "pool_add failed"
5257         pool_add_targets $qpool2 1 1 ||
5258                 error "pool_add_targets failed"
5259
5260         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5261                 error "set user quota failed"
5262
5263         # make sure the system is clean
5264         local used=$(getquota -u $TSTUSR global curspace)
5265
5266         echo "used $used"
5267         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5268
5269         # First component is on OST0, 2nd on OST1
5270         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
5271         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5272
5273         # fill the 1st component on OST0
5274         $RUNAS $DD of=$testfile count=128 ||
5275                 quota_error u $TSTUSR "write failed"
5276         # write to the 2nd cmpnt on OST1
5277         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
5278                 quota_error u $TSTUSR "write failed"
5279         # this time maybe cache write,  ignore it's failure
5280         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
5281                 true
5282         cancel_lru_locks osc
5283         sync; sync_all_data || true
5284         # write over limit in qpool2
5285         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
5286                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5287         return 0
5288 }
5289 run_test 71b "Check SEL with quota pools"
5290
5291 test_72()
5292 {
5293         local limit=10 # MB
5294         local global_limit=50 # MB
5295         local testfile="$DIR/$tdir/$tfile-0"
5296         local qpool="qpool1"
5297
5298         mds_supports_qp
5299         setup_quota_test || error "setup quota failed with $?"
5300
5301         # enable ost quota
5302         set_ost_qtype $QTYPE || error "enable ost quota failed"
5303
5304         # test for user
5305         log "User quota (block hardlimit:$global_limit MB)"
5306         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5307                 error "set user quota failed"
5308
5309         pool_add $qpool || error "pool_add failed"
5310         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
5311
5312         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5313                 error "set user quota failed"
5314
5315         # make sure the system is clean
5316         local used=$(getquota -u $TSTUSR global curspace)
5317         echo "used $used"
5318         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5319
5320         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5321
5322         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
5323         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5324         test_1_check_write $testfile "user" $limit
5325         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5326         echo "used $used"
5327         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
5328         # check that lfs quota -uv --pool prints only OST that
5329         # was added in a pool
5330         lfs quota -v -u $TSTUSR --pool $qpool $DIR | grep -v "OST0001" |
5331                 grep "OST\|MDT" && error "$qpool consists wrong targets"
5332         return 0
5333 }
5334 run_test 72 "lfs quota --pool prints only pool's OSTs"
5335
5336 test_73a()
5337 {
5338         (( $MDS1_VERSION >= $(version_code 2.14.51.158) )) ||
5339                 skip "need MDS >= v2_14_51-158-g25a70a88 for default pool quota"
5340
5341         local qpool="qpool1"
5342
5343         mds_supports_qp
5344
5345         pool_add $qpool || error "pool_add failed"
5346         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5347                 error "pool_add_targets failed"
5348
5349         test_default_quota "-u" "data" "qpool1"
5350 }
5351 run_test 73a "default limits at OST Pool Quotas"
5352
5353 test_73b()
5354 {
5355         (( $MDS1_VERSION >= $(version_code 2.14.52.91) )) ||
5356                 skip "need MDS >= v2_14_52-91-g188112fc8 for nested lqe fix"
5357
5358         local TESTFILE1="$DIR/$tdir/$tfile-1"
5359         local limit=20 #20M
5360         local qpool="qpool1"
5361
5362         mds_supports_qp
5363
5364         setup_quota_test || error "setup quota failed with $?"
5365         quota_init
5366         set_ost_qtype $QTYPE || error "enable ost quota failed"
5367
5368         # pool quotas don't work properly without global limit
5369         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
5370                 error "set global limit failed"
5371
5372         pool_add $qpool || error "pool_add failed"
5373         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5374                 error "pool_add_targets failed"
5375
5376         log "set default quota for $qpool"
5377         $LFS setquota -U --pool $qpool -b ${limit}M -B ${limit}M $DIR ||
5378                 error "set default quota failed"
5379
5380         log "Write from user that hasn't lqe"
5381         # Check that it doesn't cause a panic or a deadlock
5382         # due to nested lqe lookups that rewrite 1st lqe in qti_lqes array.
5383         # Have to use RUNAS_ID as resetquota creates lqes in
5384         # the beginning for TSTUSR/TSTUSR2 when sets limits to 0.
5385         runas -u $RUNAS_ID -g $RUNAS_GID $DD of=$TESTFILE1 count=10
5386
5387         cancel_lru_locks osc
5388         sync; sync_all_data || true
5389 }
5390 run_test 73b "default OST Pool Quotas limit for new user"
5391
5392 test_74()
5393 {
5394         (( $MDS1_VERSION >= $(version_code 2.14.52.6) )) ||
5395                 skip "need MDS >= v2_14_52-6-g8c19365416 for pool per-user fix"
5396
5397         local global_limit=200 # 200M
5398         local limit=10 # 10M
5399         local limit2=50 # 50M
5400         local qpool="qpool1"
5401         local qpool2="qpool2"
5402         local tmp=0
5403
5404         mds_supports_qp
5405         setup_quota_test || error "setup quota failed with $?"
5406
5407         # enable ost quota
5408         set_ost_qtype $QTYPE || error "enable ost quota failed"
5409
5410         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5411                 error "set user quota failed"
5412
5413         pool_add $qpool || error "pool_add failed"
5414         pool_add_targets $qpool 0 1 ||
5415                 error "pool_add_targets failed"
5416
5417         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5418                 error "set user quota failed"
5419
5420         pool_add $qpool2 || error "pool_add failed"
5421         pool_add_targets $qpool2 1 1 ||
5422                 error "pool_add_targets failed"
5423
5424         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5425                 error "set user quota failed"
5426
5427         tmp=$(getquota -u $TSTUSR global bhardlimit)
5428         [ $tmp -eq $((global_limit * 1024)) ] ||
5429                 error "wrong global limit $global_limit"
5430
5431         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5432         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit $tmp for $qpool"
5433
5434         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool2)
5435         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit $tmp for $qpool2"
5436
5437         # check limits in pools
5438         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5439               grep -A4 $qpool | awk 'NR == 4{print $4}')
5440         echo "pool limit for $qpool $tmp"
5441         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit:tmp for $qpool"
5442         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5443               grep -A4 $qpool2 | awk 'NR == 4{print $4}')
5444         echo "pool limit for $qpool2 $tmp"
5445         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit:$tmp for $qpool2"
5446 }
5447 run_test 74 "check quota pools per user"
5448
5449 function cleanup_quota_test_75()
5450 {
5451         do_facet mgs $LCTL nodemap_modify --name default \
5452                 --property admin --value 1
5453         do_facet mgs $LCTL nodemap_modify --name default \
5454                 --property trusted --value 1
5455         do_facet mgs $LCTL nodemap_modify --name default \
5456                 --property squash_uid --value 99
5457         do_facet mgs $LCTL nodemap_modify --name default \
5458                 --property squash_gid --value 99
5459
5460         wait_nm_sync default admin_nodemap
5461         wait_nm_sync default trusted_nodemap
5462
5463         do_facet mgs $LCTL nodemap_activate 0
5464         wait_nm_sync active
5465
5466         resetquota -u $TSTUSR
5467 }
5468
5469 test_dom_75() {
5470         local dd_failed=false
5471         local LIMIT=20480 #20M
5472         local qid=$TSTID
5473
5474         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5475                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5476                         conv=fsync || dd_failed=true
5477         done
5478
5479         $dd_failed && quota_error u $qid "write failed, expect succeed (1)"
5480
5481         for ((i = $((LIMIT/2048-1)); i < $((LIMIT/1024 + 10)); i++)); do
5482                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5483                         conv=fsync || dd_failed=true
5484         done
5485
5486         $dd_failed || quota_error u $qid "write succeed, expect EDQUOT (1)"
5487
5488         rm -f $DIR/$tdir_dom/*
5489
5490         # flush cache, ensure noquota flag is set on client
5491         cancel_lru_locks
5492         sync; sync_all_data || true
5493
5494         dd_failed=false
5495
5496         $DD of=$DIR/$tdir/file count=$((LIMIT/2048-1)) conv=fsync ||
5497                 quota_error u $qid "write failed, expect succeed (2)"
5498
5499         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
5500                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5501                         conv=fsync || dd_failed=true
5502         done
5503
5504         $dd_failed || quota_error u $TSTID "write succeed, expect EDQUOT (2)"
5505
5506         rm -f $DIR/$tdir/*
5507         rm -f $DIR/$tdir_dom/*
5508
5509         # flush cache, ensure noquota flag is set on client
5510         cancel_lru_locks
5511         sync; sync_all_data || true
5512
5513         dd_failed=false
5514
5515         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5516                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5517                         conv=fsync || dd_failed=true
5518         done
5519
5520         $dd_failed && quota_error u $qid "write failed, expect succeed (3)"
5521
5522         $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=direct &&
5523                 quota_error u $qid "write succeed, expect EDQUOT (3)"
5524         true
5525 }
5526
5527 test_75()
5528 {
5529         (( $MDS1_VERSION >= $(version_code 2.14.52.68) )) ||
5530                 skip "need MDS >= v2_14_52-68-ga4fbe7341b for squash root fix"
5531
5532         local soft_limit=10 # MB
5533         local hard_limit=20 # MB
5534         local limit=$soft_limit
5535         local testfile="$DIR/$tdir/$tfile-0"
5536         local grace=20 # seconds
5537         local tdir_dom=${tdir}_dom
5538
5539         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
5540             grace=60
5541         fi
5542
5543         setup_quota_test || error "setup quota failed with $?"
5544         stack_trap cleanup_quota_test_75 EXIT
5545
5546         # enable ost quota
5547         set_ost_qtype $QTYPE || error "enable ost quota failed"
5548         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
5549
5550         local used=$(getquota -u $TSTID global curspace)
5551         $LFS setquota -t -u --block-grace $grace --inode-grace \
5552                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
5553         $LFS setquota -u $TSTUSR -b $((soft_limit+used/1024))M \
5554                         -B $((hard_limit+used/1024))M -i 0 -I 0 $DIR ||
5555                 error "set user quota failed"
5556
5557         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
5558         mkdir $DIR/$tdir_dom
5559         chmod 777 $DIR/$tdir_dom
5560         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
5561                 error "setstripe $tdir_dom failed"
5562
5563         do_facet mgs $LCTL nodemap_activate 1
5564         wait_nm_sync active
5565         do_facet mgs $LCTL nodemap_modify --name default \
5566                 --property admin --value 0
5567         do_facet mgs $LCTL nodemap_modify --name default \
5568                 --property trusted --value 0
5569         do_facet mgs $LCTL nodemap_modify --name default \
5570                 --property deny_unknown --value 0
5571         do_facet mgs $LCTL nodemap_modify --name default \
5572                 --property squash_uid --value $TSTID
5573         do_facet mgs $LCTL nodemap_modify --name default \
5574                 --property squash_gid --value $TSTID
5575         cancel_lru_locks mdc
5576         wait_nm_sync default admin_nodemap
5577         wait_nm_sync default trusted_nodemap
5578         wait_nm_sync default squash_uid
5579
5580         # mmap write when over soft limit
5581         limit=$soft_limit
5582         $DD of=$testfile count=${limit} || quota_error a $TSTUSR \
5583                         "root write failure, but expect success (1)"
5584         OFFSET=$((limit * 1024))
5585         cancel_lru_locks osc
5586
5587         echo "Write to exceed soft limit"
5588         dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
5589               quota_error a $TSTUSR "root write failure, but expect success (2)"
5590         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
5591         cancel_lru_locks osc
5592
5593         echo "mmap write when over soft limit"
5594         $MULTIOP $testfile.mmap OT40960SMW ||
5595                 quota_error a $TSTUSR "mmap write failure, but expect success"
5596         cancel_lru_locks osc
5597         rm -f $testfile*
5598         wait_delete_completed || error "wait_delete_completed failed (1)"
5599         sync_all_data || true
5600
5601         # test for user hard limit
5602         limit=$hard_limit
5603         log "Write..."
5604         $DD of=$testfile count=$((limit/2)) ||
5605                 quota_error u $TSTID \
5606                         "root write failure, but expect success (3)"
5607
5608         log "Write out of block quota ..."
5609         # possibly a cache write, ignore failure
5610         $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
5611         # flush cache, ensure noquota flag is set on client
5612         cancel_lru_locks osc
5613         sync; sync_all_data || true
5614         # sync forced cache flush, but did not guarantee that slave
5615         # got new edquot through glimpse, so wait to make sure
5616         sleep 5
5617         $DD of=$testfile count=1 seek=$limit conv=fsync &&
5618                 quota_error u $TSTID \
5619                         "user write success, but expect EDQUOT"
5620         rm -f $testfile
5621         wait_delete_completed || error "wait_delete_completed failed (2)"
5622         sync_all_data || true
5623         [ $(getquota -u $TSTUSR global curspace) -eq $used ] ||
5624                 quota_error u $TSTID "user quota not released after deletion"
5625
5626         test_dom_75
5627 }
5628 run_test 75 "nodemap squashed root respects quota enforcement"
5629
5630 test_76() {
5631         (( $MDS1_VERSION >= $(version_code 2.14.52.109) )) ||
5632                 skip "need MDS >= v2_14_52-109-g3ffa5d680f for bad PRJID fix"
5633         ! is_project_quota_supported &&
5634                 skip "skip project quota unsupported"
5635
5636         setup_quota_test || error "setup quota failed with $?"
5637         quota_init
5638
5639         local testfile="$DIR/$tdir/$tfile-0"
5640
5641         touch $testfile
5642         $LFS project -p 4294967295 $testfile &&
5643                 error "set project ID should fail"
5644         return 0
5645 }
5646 run_test 76 "project ID 4294967295 should be not allowed"
5647
5648 test_77()
5649 {
5650         (( $MDS1_VERSION >= $(version_code 2.14.54.33) )) ||
5651                 skip "need MDS >= v2_14_54-33-g29e00cecc6 for readonly fix"
5652
5653         mount_client $MOUNT2 "ro"
5654         lfs setquota -u $TSTUSR -b 100M -B 100M -i 10K -I 10K $MOUNT2 &&
5655                 error "lfs setquota should fail in read-only Lustre mount"
5656         umount $MOUNT2
5657 }
5658 run_test 77 "lfs setquota should fail in Lustre mount with 'ro'"
5659
5660 test_78A()
5661 {
5662         (( $OST1_VERSION >= $(version_code 2.14.55.173) )) ||
5663                 skip "need OST >= v2_14_55-173-g789038c97a for fallocate fix"
5664         check_set_fallocate_or_skip
5665
5666         setup_quota_test || error "setup quota failed with $?"
5667
5668         # enable ost quota
5669         set_ost_qtype $QTYPE || error "enable ost quota failed"
5670
5671         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5672         chown $TSTUSR $DIR/$tdir || error "failed to chown $tdir"
5673
5674         # setup quota limit
5675         $LFS setquota -u $TSTUSR -b25M -B25M $DIR/$tdir ||
5676                 error "lfs setquota failed"
5677
5678         # call fallocate
5679         runas -u $TSTUSR -g $TSTUSR fallocate -l 204800 $DIR/$tdir/$tfile
5680
5681         kbytes=$(lfs quota -u $TSTUSR $DIR |
5682                 awk -v pattern=$DIR 'match($0, pattern) {printf $2}')
5683         echo "kbytes returned:$kbytes"
5684
5685         # For file size of 204800. We should be having roughly 200 kbytes
5686         # returned. Anything alarmingly low (50 taken as arbitrary value)
5687         # would bail out this TC. Also this also avoids $kbytes of 0
5688         # to be used in calculation below.
5689         (( $kbytes > 50 )) ||
5690                 error "fallocate did not use quota. kbytes returned:$kbytes"
5691
5692         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5693         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5694
5695         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5696         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5697                 error "fallocate did not use quota correctly"
5698 }
5699 run_test 78A "Check fallocate increase quota usage"
5700
5701 test_78a()
5702 {
5703         (( $CLIENT_VERSION >= $(version_code 2.15.0.37) )) ||
5704                 skip "need client >= v2_15_50-37-g5fc934eb for falloc proj fix"
5705         (( $OST1_VERSION >= $(version_code 2.15.0.37) )) ||
5706                 skip "need OST >= v2_15_50-37-g5fc934ebbb for falloc proj fix"
5707         check_set_fallocate_or_skip
5708
5709         setup_quota_test || error "setup quota failed with $?"
5710
5711         # enable ost quota
5712         set_ost_qtype $QTYPE || error "enable ost quota failed"
5713
5714         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5715
5716         local projectid=5200 # Random project id to test
5717
5718         change_project -sp $projectid $DIR/$tdir
5719
5720         # setup quota limit
5721         $LFS setquota -p $projectid -b25M -B25M $DIR/$tdir ||
5722                 error "lfs setquota project failed"
5723
5724         # call fallocate
5725         fallocate -l 204800 $DIR/$tdir/$tfile
5726
5727         # Get curspace (kbytes) for $projectid
5728         local kbytes=$(getquota -p $projectid global curspace)
5729
5730         echo "kbytes returned:$kbytes"
5731
5732         # For file size of 204800. We should be having roughly 200 kbytes
5733         # returned. Anything alarmingly low (50 taken as arbitrary value)
5734         # would bail out this TC. Also this also avoids $kbytes of 0
5735         # to be used in calculation below.
5736         (( $kbytes > 50 )) ||
5737                 error "fallocate did not use projectid. kbytes returned:$kbytes"
5738
5739         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5740         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5741
5742         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5743         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5744                 error "fallocate did not use quota projectid correctly"
5745 }
5746 run_test 78a "Check fallocate increase projectid usage"
5747
5748 test_79()
5749 {
5750         (( $MDS1_VERSION >= $(version_code 2.14.56.37) )) ||
5751                 skip "need MDS >= v2_14_56-37-gc9901b68b4 for pool panic fix"
5752
5753         local qpool="qpool1"
5754         local cmd="$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$qpool.info"
5755         local stopf=$TMP/$tfile
5756
5757         do_facet mds1 "touch $stopf"
5758         stack_trap "do_facet mds1 'rm -f $stopf'"
5759         do_facet mds1 "while [ -e $stopf ]; do $cmd &>/dev/null; done"&
5760         local pid=$!
5761         pool_add $qpool || error "pool_add failed"
5762         do_facet mds1 "rm $stopf"
5763         wait $pid
5764 }
5765 run_test 79 "access to non-existed dt-pool/info doesn't cause a panic"
5766
5767 test_80()
5768 {
5769         (( $MDS1_VERSION >= $(version_code 2.14.56.51) )) ||
5770                 skip "need MDS >= v2_14_56-51-g61ec1e0f2c for EDQUOT failover"
5771
5772         local dir1="$DIR/$tdir/dir1"
5773         local dir2="$DIR/$tdir/dir2"
5774         local TESTFILE0="$dir1/$tfile-0"
5775         local TESTFILE1="$dir1/$tfile-1"
5776         local TESTFILE2="$dir1/$tfile-2"
5777         local TESTFILE3="$dir2/$tfile-0"
5778         local global_limit=100 # 100M
5779         local limit=10 # 10M
5780         local qpool="qpool1"
5781
5782         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
5783         mds_supports_qp
5784         [ "$ost1_FSTYPE" == zfs ] &&
5785                 skip "ZFS grants some block space together with inode"
5786         setup_quota_test || error "setup quota failed with $?"
5787         set_ost_qtype $QTYPE || error "enable ost quota failed"
5788
5789         # make sure the system is clean
5790         local used=$(getquota -u $TSTUSR global curspace)
5791         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR is not 0."
5792
5793         pool_add $qpool || error "pool_add failed"
5794         pool_add_targets $qpool 0 1 ||
5795                 error "pool_add_targets failed"
5796
5797         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5798                 error "set user quota failed"
5799
5800         $LFS setquota -u $TSTUSR -B ${global_limit}M --pool $qpool $DIR ||
5801                 error "set user quota failed"
5802         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5803                 error "set user quota failed"
5804
5805         mkdir -p $dir1 || error "failed to mkdir"
5806         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5807         mkdir -p $dir2 || error "failed to mkdir"
5808         chown $TSTUSR.$TSTUSR $dir2 || error "chown $dir2 failed"
5809
5810         $LFS setstripe $dir1 -i 1 -c 1|| error "setstripe $testfile failed"
5811         $LFS setstripe $dir2 -i 0 -c 1|| error "setstripe $testfile failed"
5812         lfs getstripe $dir1
5813         lfs getstripe $dir2
5814         sleep 3
5815
5816         $LFS quota -uv $TSTUSR $DIR
5817         #define OBD_FAIL_QUOTA_PREACQ            0xA06
5818         do_facet mds1 $LCTL set_param fail_loc=0xa06
5819         $RUNAS $DD of=$TESTFILE3 count=3 ||
5820                 quota_error u $TSTUSR "write failed"
5821         $RUNAS $DD of=$TESTFILE2 count=7 ||
5822                 quota_error u $TSTUSR "write failed"
5823         $RUNAS $DD of=$TESTFILE1 count=1 oflag=direct ||
5824                 quota_error u $TSTUSR "write failed"
5825         sync
5826         sleep 3
5827         $LFS quota -uv --pool $qpool $TSTUSR $DIR
5828
5829         rm -f $TESTFILE2
5830         stop ost2
5831         do_facet mds1 $LCTL set_param fail_loc=0
5832         start ost2 $(ostdevname 2) $OST_MOUNT_OPTS || error "start ost2 failed"
5833         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5834         # OST0 needs some time to update quota usage after removing TESTFILE2
5835         sleep 4
5836         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5837         $RUNAS $DD of=$TESTFILE0 count=2 oflag=direct ||
5838                 quota_error u $TSTUSR "write failure, but expect success"
5839 }
5840 run_test 80 "check for EDQUOT after OST failover"
5841
5842 test_81()
5843 {
5844         (( $MDS1_VERSION >= $(version_code 2.14.56.52) )) ||
5845                 skip "need MDS >= v2_14_56-52-g862f0baa7c for qmt_pool_free fix"
5846
5847         local global_limit=20  # 100M
5848         local testfile="$DIR/$tdir/$tfile-0"
5849         local qpool="qpool1"
5850
5851         mds_supports_qp
5852         setup_quota_test || error "setup quota failed with $?"
5853
5854         # enable ost quota
5855         set_ost_qtype $QTYPE || error "enable ost quota failed"
5856
5857         # test for user
5858         log "User quota (block hardlimit:$global_limit MB)"
5859         $LFS setquota -u $TSTUSR -B 1G $DIR || error "set user quota failed"
5860
5861         pool_add $qpool || error "pool_add failed"
5862         #define OBD_FAIL_QUOTA_RECALC   0xA07
5863         do_facet mds1 $LCTL set_param fail_loc=0x80000A07 fail_val=10
5864         # added OST casues to start pool recalculation
5865         pool_add_targets $qpool 0 0 1
5866         stop mds1 -f || error "MDS umount failed"
5867
5868         #start mds1 back to destroy created pool
5869         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
5870         clients_up || true
5871 }
5872 run_test 81 "Race qmt_start_pool_recalc with qmt_pool_free"
5873
5874 test_82()
5875 {
5876         (( $MDS1_VERSION >= $(version_code 2.15.50.72) )) ||
5877                 skip "need MDS >= v2_15_50-72-g61481796ac for over 8 QIDs fix"
5878
5879         is_project_quota_supported || skip "skip project quota unsupported"
5880
5881         setup_quota_test || error "setup quota failed with $?"
5882         quota_init
5883
5884         local parent_dir="$DIR/$tdir.parent"
5885         local child_dir="$parent_dir/child"
5886
5887         mkdir -p $child_dir
5888         stack_trap "chown -R 0:0 $parent_dir"
5889
5890         chown $TSTUSR:$TSTUSR $parent_dir ||
5891                 error "failed to chown on $parent_dir"
5892         chown $TSTUSR2:$TSTUSRS2 $child_dir ||
5893                 error "failed to chown on $parent_dir"
5894
5895         $LFS project -p 1000 $parent_dir ||
5896                 error "failed to set project id on $parent_dir"
5897         $LFS project -p 1001 $child_dir ||
5898                 error "failed to set project id on $child_dir"
5899
5900         rmdir $child_dir || error "cannot remove child dir, test failed"
5901 }
5902 run_test 82 "verify more than 8 qids for single operation"
5903
5904 test_grace_with_default_quota()
5905 {
5906         local qtype=$1
5907         local qdtype=$2
5908         local bgrace
5909         local igrace
5910         local bgrace2
5911         local igrace2
5912         echo "ttt1"
5913         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
5914                 error "clear default quota [$qdtype] failed"
5915         echo "ttt2"
5916         $LFS setquota -t $qtype --block-grace 1w --inode-grace 1w $DIR ||
5917                 error "reset quota [$qdtype] grace failed"
5918         echo "ttt3"
5919
5920         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5921                         '{printf("bgrace=%s;igrace=%s;", $4, $9)}')
5922         echo "ttt4"
5923
5924         $LFS setquota $qdtype -B 10G -i 10k $DIR
5925         echo "ttt5"
5926
5927         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5928                         '{printf("bgrace2=%s;igrace2=%s;", $4, $9)}')
5929
5930         [ "$bgrace" == "$bgrace2" ] ||
5931                         error "set default quota shouldn't affect block grace"
5932         [ "$igrace" == "$igrace2" ] ||
5933                         error "set default quota shouldn't affect inode grace"
5934
5935 }
5936
5937 test_83()
5938 {
5939         (( $MDS1_VERSION >= $(version_code 2.15.51.29) )) ||
5940                 skip "need MDS >= v2_15_51-29-gd4978678b4 for grace time fix"
5941
5942         setup_quota_test || error "setup quota failed with $?"
5943         test_grace_with_default_quota "-u" "-U"
5944         test_grace_with_default_quota "-g" "-G"
5945
5946         is_project_quota_supported || return 0
5947         test_grace_with_default_quota "-p" "-P"
5948 }
5949 run_test 83 "Setting default quota shouldn't affect grace time"
5950
5951 test_84()
5952 {
5953         (( $MDS1_VERSION >= $(version_code 2.15.53.115) )) ||
5954                 skip "need MDS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5955         (( $OST1_VERSION >= $(version_code 2.15.53.115) )) ||
5956                 skip "need OSS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5957
5958         local dir1="$DIR/$tdir/dir1"
5959         local TESTFILE1="$dir1/$tfile-1"
5960         local waited=0
5961         local grant=0
5962         local grant2=0
5963         local qp="qpool1"
5964
5965         mds_supports_qp
5966
5967         setup_quota_test || error "setup quota failed with $?"
5968         quota_init
5969         set_ost_qtype $QTYPE || error "enable ost quota failed"
5970
5971         pool_add $qp || error "pool_add failed"
5972         pool_add_targets $qp 0 $(($OSTCOUNT - 1)) ||
5973                 error "pool_add_targets failed"
5974
5975         $LFS setquota -g $TSTUSR -B 10G $DIR ||
5976                 error "failed to set group quota for $TSTUSR"
5977         $LFS setquota -g $TSTUSR -B 5G --pool $qp $DIR ||
5978                 error "set user quota failed"
5979         $LFS quota -gv $TSTUSR $DIR
5980
5981         wait_quota_synced "ost1" "OST0000" "grp" $TSTID "hardlimit" "10485760"
5982
5983         mkdir -p $dir1 || error "failed to mkdir"
5984         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5985
5986         $LFS setstripe -c 1 -i 0 $TESTFILE1
5987         $LFS getstripe $TESTFILE1
5988         chown $TSTUSR.$TSTUSR $TESTFILE1
5989
5990         $RUNAS $DD of=$TESTFILE1 count=60 conv=nocreat oflag=direct ||
5991                 quota_error g $TSTUSR "write failed"
5992
5993         sync_all_data || true
5994         sleep 3
5995         $LFS quota -gv $TSTUSR $DIR
5996         $LFS quota -gv --pool $qp $TSTUSR $DIR
5997
5998         # the grant quota should be larger than 0
5999         waited=0
6000         while (( $waited < 60 )); do
6001                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
6002                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
6003                 (( ${grant} > 0 && ${grant2} > 0 )) && break
6004
6005                 do_facet ost1 $LCTL set_param \
6006                                 osd-*.*-OST0000.quota_slave.force_reint=1
6007                 sleep 1
6008                 waited=$((waited + 1))
6009         done
6010
6011         (( $waited >= 60)) && {
6012                 $LFS quota -gv $TSTUSR $DIR
6013                 $LFS quota -gv --pool $qp $TSTUSR $DIR
6014         }
6015
6016         (( ${grant} > 0 )) || error "pool grant is not increased after dd"
6017         (( ${grant2} > 0 )) || error "grant is not increased after dd"
6018
6019 #define OBD_FAIL_QUOTA_GRANT 0xA08
6020         lustre_fail mds 0xa08
6021         lustre_fail ost 0xa08
6022         sleep 1
6023
6024         # clear quota limits to trigger updating grant quota
6025         $LFS setquota -g $TSTUSR -b 0 -B 0 $DIR ||
6026                 error "failed to clear the group quota for $TSTUSR"
6027         $LFS quota -gv $TSTUSR $DIR
6028         $LFS quota -gv --pool $qp $TSTUSR $DIR
6029
6030         # the grant quota should be set as insane value
6031         waited=0
6032         while (( $waited < 60 )); do
6033                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
6034                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
6035                 (( ${#grant} == 20 && ${#grant2} == 20 )) && break
6036
6037                 sleep 1
6038                 waited=$((waited + 1))
6039         done
6040
6041         (( $waited >= 60)) && {
6042                 $LFS quota -gv $TSTUSR $DIR
6043                 $LFS quota -gv --pool $qp $TSTUSR $DIR
6044         }
6045
6046         (( ${#grant} == 20 )) || error "pool grant is not set as insane value"
6047         (( ${#grant2} == 20 )) || error "grant is not set as insane value"
6048
6049         lustre_fail mds_ost 0
6050         sleep 1
6051
6052         # reset the quota
6053         $LFS setquota -g $TSTUSR -r $DIR ||
6054                 error "failed to reset group quota for $TSTUSR"
6055
6056         sleep 3
6057         $LFS quota -gv $TSTUSR $DIR
6058         $LFS quota -gv --pool $qp $TSTUSR $DIR
6059
6060         # the grant quota should be reset
6061         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
6062         (( ${#grant} == 20 )) && error "grant is not cleared"
6063         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
6064         (( ${#grant} == 20 )) && error "pool grant is not cleared"
6065
6066         $LFS quota -gv $TSTUSR --pool $qp $DIR
6067         $LFS quota -gv --pool $qp $TSTUSR $DIR
6068         local hlimit=$(getquota -g $TSTUSR global bhardlimit $qp)
6069          [ $hlimit -eq 5242880 ] || error "pool limit is changed"
6070
6071         # test whether the quota still works
6072         $LFS setquota -g $TSTUSR -B 100M $DIR ||
6073                 error "failed to set group quota for $TSTUSR"
6074         $LFS quota -gv $TSTUSR $DIR
6075
6076         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct &&
6077                 quota_error g $TSTUSR "dd succeed, expect EDQUOT"
6078
6079         $LFS setquota -g $TSTUSR -B 300M $DIR ||
6080                 error "failed to set group quota for $TSTUSR"
6081         $LFS quota -gv $TSTUSR $DIR
6082
6083         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct ||
6084                 quota_error g $TSTUSR "dd failed, expect succeed"
6085 }
6086 run_test 84 "Reset quota should fix the insane granted quota"
6087
6088 test_85()
6089 {
6090         (( $MDS1_VERSION >= $(version_code 2.15.55.5) )) ||
6091                 skip "need MDS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
6092         (( $OST1_VERSION >= $(version_code 2.15.55.5) )) ||
6093                 skip "need OSS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
6094
6095         local limit=3 # 3M
6096         local qpool="qpool1"
6097         local qpool2="qpool2"
6098         local tfile1="$DIR/$tdir/$tfile-0"
6099
6100         (( OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
6101         mds_supports_qp
6102         setup_quota_test || error "setup quota failed with $?"
6103
6104         # enable ost quota
6105         set_ost_qtype $QTYPE || error "enable ost quota failed"
6106
6107         $LFS setquota -u $TSTUSR -b 0 -B 50T -i 0 -I 0 $DIR ||
6108                 error "set user quota failed"
6109
6110         pool_add $qpool || error "pool_add failed"
6111         pool_add_targets $qpool 0 1 ||
6112                 error "pool_add_targets failed"
6113
6114         pool_add $qpool2 || error "pool_add failed"
6115         pool_add_targets $qpool2 0 1 ||
6116                 error "pool_add_targets failed"
6117
6118         $LFS setstripe -p $qpool $DIR/$tdir || error "cannot set stripe"
6119         $LFS setquota -u $TSTUSR -B 30M --pool $qpool $DIR ||
6120                 error "set user quota failed"
6121         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
6122                 error "set user quota failed"
6123
6124         # don't care about returned value. Just check we don't hung on write.
6125         $RUNAS $DD of=$tfile1 count=10
6126         return 0
6127 }
6128 run_test 85 "do not hung at write with the least_qunit"
6129
6130 test_preacquired_quota()
6131 {
6132         local test_dir=$1
6133         local qtype=$2
6134         local qtype_name=$3
6135         local qid=$4
6136
6137         [[ "$qtype" == "-p" ]] && change_project -sp $qid $DIR/$tdir
6138
6139         $LFS setquota $qtype $qid -i 100K -I 100K $DIR ||
6140                 error "failed to set file [$qtype] quota"
6141
6142         $RUNAS createmany -m $test_dir/tfile- 5000 ||
6143                 error "failed to create files, expect succeed"
6144
6145         wait_zfs_commit $SINGLEMDS
6146         $LFS setquota $qtype $qid -i 2K -I 2K $DIR ||
6147                 error "failed to decrease file [$qtype] quota"
6148
6149         wait_quota_synced "mds1" "MDT0000" $qtype_name $qid "hardlimit" "2048"
6150
6151         # make sure the lqe->lqe_edquot is set
6152         $RUNAS createmany -m $test_dir/tfile2- 10
6153         sleep 5
6154
6155         $RUNAS createmany -m $test_dir/tfile3- 30 &&
6156                 error "succeed to create files, expect failed"
6157
6158         rm -f $test_dir/tfile*
6159         $LFS setquota $qtype $qid -i 0 -I 0 $DIR ||
6160                 error "failed to reset file user quota"
6161 }
6162
6163 test_86()
6164 {
6165         (( $MDS1_VERSION >= $(version_code 2.15.57.41) )) ||
6166                 skip "need MDS >= 2.15.57.41 for quota over limit release fix"
6167
6168         local test_dir="$DIR/$tdir/test_dir"
6169
6170         setup_quota_test || error "setup quota failed with %?"
6171         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
6172
6173         $LFS setdirstripe -c 1 -i 0 $test_dir || error "setdirstripe failed"
6174         chmod 777 $test_dir
6175
6176         test_preacquired_quota "$test_dir" "-u" "usr" "$TSTID"
6177         test_preacquired_quota "$test_dir" "-g" "grp" "$TSTID"
6178
6179         is_project_quota_supported || return 0
6180         test_preacquired_quota "$test_dir" "-p" "prj" "1000"
6181 }
6182 run_test 86 "Pre-acquired quota should be released if quota is over limit"
6183
6184 quota_fini()
6185 {
6186         do_nodes $(comma_list $(nodes_list)) \
6187                 "lctl set_param -n debug=-quota-trace"
6188         if $PQ_CLEANUP; then
6189                 disable_project_quota
6190         fi
6191 }
6192 reset_quota_settings
6193 quota_fini
6194
6195 cd $ORIG_PWD
6196 complete_test $SECONDS
6197 check_and_cleanup_lustre
6198 export QUOTA_AUTO=$QUOTA_AUTO_OLD
6199 exit_status