Whamcloud - gitweb
183b9b1d04eb01125bac87c3c1c289a5a496a99f
[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         test_mkdir -p $dir && change_project -sp $projid $dir
3879         $LFS setquota -p $projid -b 0 -B ${blimit}K -i 0 -I $ilimit $dir ||
3880                 error "set project quota failed"
3881
3882         sync; sync_all_data
3883         sleep_maxage
3884
3885         # check if df output works as expected
3886         echo "== global statfs: $MOUNT =="
3887         df -kP $MOUNT; df -iP $MOUNT; $LFS quota -p $projid $dir
3888         echo
3889         echo "== project statfs (prjid=$projid): $dir =="
3890         df -kP $dir; df -iP $dir
3891         local bused=$(getquota -p $projid global curspace)
3892         local iused=$(getquota -p $projid global curinodes)
3893         local expected="$ilimit$iused"
3894
3895         wait_update $HOSTNAME \
3896                 "df -iP $dir | awk \\\"/$FSNAME/\\\"'{print \\\$2 \\\$3}'" \
3897                 "$expected" ||
3898                 error "failed to get correct statfs for project quota"
3899
3900         expected=$(df -kP $dir | awk "/$FSNAME/"' {print $2}')
3901         (( expected == blimit )) ||
3902                 error "blimit mismatch: $expected != $blimit"
3903
3904         # zfs block size is 4K, while quota is printed in 1K, df result may be
3905         # larger than quota result, but it's no more than 3K
3906         expected=$(df -kP $dir | awk "/$FSNAME/"' {print $3}')
3907         (( expected - bused < 4)) || error "bused mismatch: $expected != $bused"
3908 }
3909 run_test 41 "df should return projid-specific values"
3910
3911 test_delete_qid()
3912 {
3913         local qslv_file=$1
3914         local qtype_file=$2
3915         local qtype=$3
3916         local qid=$4
3917         local osd="osd-ldiskfs"
3918
3919         [ "$ost1_FSTYPE" = zfs ] && osd="osd-zfs"
3920
3921         rm -f $DIR/$tdir/$tfile
3922         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile
3923         chmod a+rw $DIR/$tdir/$tfile
3924
3925         $LFS setquota $qtype $qid -B 300M $MOUNT
3926         $RUNAS $DD of=$DIR/$tdir/$tfile count=1 || error "failed to dd"
3927
3928         do_facet $SINGLEMDS \
3929                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3930                  grep -E 'id: *$qid'" || error "QMT: no qid $qid is found"
3931         echo $osd
3932         do_facet ost1 \
3933                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3934                  grep -E 'id: *$qid'" || error "QSD: no qid $qid is found"
3935
3936         $LFS setquota $qtype $qid --delete $MOUNT
3937         do_facet $SINGLEMDS \
3938                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3939                  grep -E 'id: *$qid'" && error "QMT: qid $qid is not deleted"
3940         sleep 5
3941         do_facet ost1 \
3942                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3943                  grep -E 'id: *$qid'" && error "QSD: qid $qid is not deleted"
3944
3945         $LFS setquota $qtype $qid -B 500M $MOUNT
3946         $RUNAS $DD of=$DIR/$tdir/$tfile count=1 || error "failed to dd"
3947         do_facet $SINGLEMDS \
3948                 "cat /proc/fs/lustre/qmt/$FSNAME-QMT0000/dt-0x0/$qtype_file |
3949                  grep -E 'id: *$qid'" || error "QMT: qid $pid is not recreated"
3950         cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file
3951         do_facet ost1 \
3952                 "cat /proc/fs/lustre/$osd/$FSNAME-OST0000/$qslv_file |
3953                  grep -E 'id: *$qid'" || error "QSD: qid $qid is not recreated"
3954 }
3955
3956 test_48()
3957 {
3958         setup_quota_test || error "setup quota failed with $?"
3959         set_ost_qtype $QTYPE || error "enable ost quota failed"
3960         quota_init
3961
3962         test_delete_qid "quota_slave/limit_user" "glb-usr" "-u" $TSTID
3963         test_delete_qid "quota_slave/limit_group" "glb-grp" "-g" $TSTID
3964         is_project_quota_supported &&
3965             test_delete_qid "quota_slave/limit_project" "glb-prj" "-p" "10000"
3966
3967         cleanup_quota_test
3968 }
3969 run_test 48 "lfs quota --delete should delete quota project ID"
3970
3971 test_50() {
3972         ! is_project_quota_supported &&
3973                 skip "Project quota is not supported"
3974
3975         setup_quota_test || error "setup quota failed with $?"
3976         local dir1="$DIR/$tdir/dir1"
3977         local dir2="$DIR/$tdir/dir2"
3978
3979         mkdir -p $dir1 && change_project -sp 1 $dir1
3980         mkdir -p $dir2 && change_project -sp 2 $dir2
3981         for num in $(seq 1 10); do
3982                 touch $dir1/file_$num $dir2/file_$num
3983                 ln -s $dir1/file_$num $dir1/file_$num"_link"
3984                 ln -s $dir2/file_$num $dir2/file_$num"_link"
3985         done
3986
3987         count=$($LFS find --projid 1 $DIR | wc -l)
3988         [ "$count" != 21 ] && error "expected 21 but got $count"
3989
3990         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
3991         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
3992         [ $count -eq 22 ] || error "expected 22 but got $count"
3993 }
3994 run_test 50 "Test if lfs find --projid works"
3995
3996 test_51() {
3997         ! is_project_quota_supported &&
3998                 skip "Project quota is not supported"
3999         setup_quota_test || error "setup quota failed with $?"
4000         local dir="$DIR/$tdir/dir"
4001
4002         mkdir $dir && change_project -sp 1 $dir
4003         local used=$(getquota -p 1 global curinodes)
4004         [ $used != "1" ] && error "expected 1 got $used"
4005
4006         touch $dir/1
4007         touch $dir/2
4008         cp $dir/2 $dir/3
4009         used=$(getquota -p 1 global curinodes)
4010         [ $used != "4" ] && error "expected 4 got $used"
4011
4012         $DD of=$DIR/$tdir/6 count=1
4013         #try cp to dir
4014         cp $DIR/$tdir/6 $dir/6
4015         used=$(getquota -p 1 global curinodes)
4016         [ $used != "5" ] && error "expected 5 got $used"
4017
4018         #try mv to dir
4019         mv $DIR/$tdir/6 $dir/7
4020         used=$(getquota -p 1 global curinodes)
4021         [ $used -eq 6 ] || error "expected 6 got $used"
4022 }
4023 run_test 51 "Test project accounting with mv/cp"
4024
4025 test_52() {
4026         ! is_project_quota_supported &&
4027                 skip "Project quota is not supported"
4028
4029         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
4030                 skip "Need MDS version at least 2.14.55"
4031
4032         setup_quota_test || error "setup quota failed with $?"
4033
4034         local dir1=$DIR/$tdir/t52_dir1
4035         local dir2=$DIR/$tdir/t52_dir2
4036
4037         mkdir $dir1 || error "failed to mkdir $dir1"
4038         mkdir $dir2 || error "failed to mkdir $dir2"
4039
4040         $LFS project -sp 1000 $dir1 || error "fail to set project on $dir1"
4041         $LFS project -sp 1001 $dir2 || error "fail to set project on $dir2"
4042
4043         $DD of=$dir1/$tfile count=100 ||
4044                 error "failed to create and write $dir1/$tfile"
4045
4046         cancel_lru_locks osc
4047         sync; sync_all_data || true
4048
4049         local attrs=($(lsattr -p $dir1/$tfile))
4050         (( ${attrs[0]} == 1000 )) ||
4051                 error "project ID on $dir1/$tfile is not inherited"
4052
4053         $LFS quota -p 1000 $DIR
4054         $LFS quota -p 1001 $DIR
4055
4056         local prev_used=$(getquota -p 1000 global curspace)
4057         local prev_used2=$(getquota -p 1001 global curspace)
4058
4059         mrename $dir1 $dir2/tdir || log "rename directory return $?"
4060
4061         local inum_before=$(ls -i $dir1/$tfile | awk '{print $1}')
4062         mrename $dir1/$tfile $dir2/$tfile || error "failed to rename file"
4063         local inum_after=$(ls -i $dir2/$tfile | awk '{print $1}')
4064
4065         attrs=($(lsattr -p $dir2/$tfile))
4066         (( ${attrs[0]} == 1001 )) ||
4067                 error "project ID is not updated after rename"
4068
4069         (( $inum_before == $inum_after )) ||
4070                 error "inode is changed after rename: $inum_before, $inum_after"
4071
4072         sync_all_data || true
4073
4074         $LFS quota -p 1000 $DIR
4075         $LFS quota -p 1001 $DIR
4076
4077         local new_used=$(getquota -p 1000 global curspace)
4078         local new_used2=$(getquota -p 1001 global curspace)
4079
4080         (( $prev_used >= $new_used + 102400 )) ||
4081                 error "quota is not deducted from old project ID"
4082         (( $prev_used2 <= $new_used2 - 102400 )) ||
4083                 error "quota is not added for the new project ID"
4084 }
4085 run_test 52 "Rename normal file across project ID"
4086
4087 test_53() {
4088         ! is_project_quota_supported &&
4089                 skip "Project quota is not supported"
4090         setup_quota_test || error "setup quota failed with $?"
4091         local dir="$DIR/$tdir/dir"
4092         mkdir $dir && change_project -s $dir
4093         [[ $($LFS project -d $dir) =~ " P " ]] ||
4094                 error "inherit attribute should be set"
4095
4096         change_project -C $dir
4097         [[ $($LFS project -d $dir) =~ " - " ]] ||
4098                 error "inherit attribute should be cleared"
4099 }
4100 run_test 53 "Project inherit attribute could be cleared"
4101
4102 test_54() {
4103         ! is_project_quota_supported &&
4104                 skip "Project quota is not supported"
4105         setup_quota_test || error "setup quota failed with $?"
4106         local testfile="$DIR/$tdir/$tfile-0"
4107
4108         #set project ID/inherit attribute
4109         change_project -sp $TSTPRJID $DIR/$tdir
4110         $RUNAS createmany -m ${testfile} 100 ||
4111                 error "create many files failed"
4112
4113         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
4114         # one more count for directory itself */
4115         ((proj_count++))
4116
4117         #check project
4118         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4119         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
4120
4121         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
4122         [ $proj_count1 -eq $proj_count ] ||
4123                         error "c2: expected $proj_count got $proj_count1"
4124
4125         #clear project but with kept projid
4126         change_project -rCk $DIR/$tdir
4127         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4128         [ $proj_count1 -eq 1 ] ||
4129                         error "c3: expected 1 got $proj_count1"
4130
4131         #verify projid untouched.
4132         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
4133         ((proj_count1++))
4134         [ $proj_count1 -eq $proj_count ] ||
4135                         error "c4: expected $proj_count got $proj_count1"
4136
4137         # test -0 option
4138         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
4139         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
4140         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
4141
4142         #this time clear all
4143         change_project -rC $DIR/$tdir
4144         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
4145         [ $proj_count1 -eq 0 ] ||
4146                         error "c6: expected 0 got $proj_count1"
4147         #cleanup
4148         unlinkmany ${testfile} 100 ||
4149                 error "unlink many files failed"
4150 }
4151 run_test 54 "basic lfs project interface test"
4152
4153 test_55() {
4154         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
4155                 skip "Not supported before 2.10.58."
4156         setup_quota_test || error "setup quota failed with $?"
4157
4158         set_ost_qtype $QTYPE || error "enable ost quota failed"
4159         quota_init
4160
4161         #add second group to TSTUSR
4162         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
4163
4164         #prepare test file
4165         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
4166         error "failed to dd"
4167
4168         cancel_lru_locks osc
4169         sync; sync_all_data || true
4170
4171         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
4172         error "failed to setquota on group $TSTUSR2"
4173
4174         $LFS quota -v -g $TSTUSR2 $DIR
4175
4176         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
4177         error "chgrp should failed with -EDQUOT"
4178
4179         USED=$(getquota -g $TSTUSR2 global curspace)
4180         echo "$USED"
4181
4182         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
4183         error "failed to setquota on group $TSTUSR2"
4184
4185         $LFS quota -v -g $TSTUSR2 $DIR
4186
4187         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
4188         error "chgrp should succeed"
4189
4190         $LFS quota -v -g $TSTUSR2 $DIR
4191 }
4192 run_test 55 "Chgrp should be affected by group quota"
4193
4194 test_56() {
4195         setup_quota_test || error "setup quota failed with $?"
4196
4197         set_ost_qtype $QTYPE || error "enable ost quota failed"
4198         quota_init
4199
4200         $LFS setquota -t -u -b 10 -i 10 $DIR ||
4201                 erro "failed to set grace time for usr quota"
4202         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
4203                      awk '{print $4 $8}')
4204         if [ "x$grace_time" != "x10s;10s" ]; then
4205                 $LFS quota -t -u $DIR
4206                 error "expected grace time: 10s;10s, got:$grace_time"
4207         fi
4208 }
4209 run_test 56 "lfs quota -t should work well"
4210
4211 test_57() {
4212         setup_quota_test || error "setup quota failed with $?"
4213
4214         local dir="$DIR/$tdir/dir"
4215         mkdir -p $dir
4216         mkfifo $dir/pipe
4217         #command can process further if it hit some errors
4218         $LFS project -sp 1 $dir/pipe
4219         touch $dir/aaa $dir/bbb
4220         mkdir $dir/subdir -p
4221         touch $dir/subdir/aaa $dir/subdir/bbb
4222         #create one invalid link file
4223         ln -s $dir/not_exist_file $dir/ccc
4224         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
4225         [ $cnt -eq 7 ] || error "expected 7 got $cnt"
4226 }
4227 run_test 57 "lfs project could tolerate errors"
4228
4229 # LU-16988
4230 test_mirror()
4231 {
4232         local projid=$1
4233         local testfile=$2
4234         local mirrorfile=$3
4235
4236         # create mirror
4237         $LFS mirror extend -N2 $mirrorfile || error "failed to create mirror"
4238
4239         local mirrors=$($LFS getstripe -N $testfile)
4240         [[ $mirrors == 3 ]] || error "mirror count $mirrors is wrong"
4241
4242         cancel_lru_locks osc
4243         cancel_lru_locks mdc
4244         sync; sync_all_data || true
4245
4246         local prev_usage=$(getquota -p $projid global curspace)
4247
4248         $RUNAS $DD of=$testfile count=50 conv=nocreat oflag=direct ||
4249                         quota_error p $projid "write failed, expect succeed"
4250
4251         cancel_lru_locks osc
4252         cancel_lru_locks mdc
4253         sync; sync_all_data || true
4254
4255         $RUNAS $LFS mirror resync $testfile || error "failed to resync mirror"
4256
4257         local usage=$(getquota -p $projid global curspace)
4258         (( usage >= prev_usage + 150*1024 )) ||
4259                                 error "project quota $usage is wrong"
4260
4261         $RUNAS $DD of=$testfile count=30 conv=nocreat seek=50 oflag=direct ||
4262                         quota_error p $projid "write failed, expect succeed"
4263
4264         $RUNAS $LFS mirror resync $testfile &&
4265                         error "resync mirror succeed, expect EDQUOT"
4266
4267         $LFS mirror delete --mirror-id 2 $testfile ||
4268                         error "failed to delete the second mirror"
4269         $LFS mirror delete --mirror-id 3 $testfile ||
4270                         error "failed to delete the third mirror"
4271 }
4272
4273 test_58() {
4274         (( $MDS1_VERSION >= $(version_code 2.15.56) )) ||
4275                 skip "need MDS 2.15.56 or later"
4276
4277         is_project_quota_supported || skip "Project quota is not supported"
4278
4279         local testdir="$DIR/$tdir"
4280         local testfile="$DIR/$tdir/$tfile"
4281         local projid=1000
4282         local projid2=1001
4283
4284         setup_quota_test || error "setup quota failed with $?"
4285
4286         USED=$(getquota -p $projid global curspace)
4287         [ $USED -ne 0 ] && error "Used space ($USED) for proj $projid isn't 0"
4288
4289         USED=$(getquota -p $projid2 global curspace)
4290         [ $USED -ne 0 ] && error "Used space ($USED) for proj $projid2 isn't 0"
4291
4292         chown $TSTUSR.$TSTUSR $testdir || error "chown $testdir failed"
4293         quota_init
4294         set_ost_qtype ugp || error "enable ost quota failed"
4295
4296         $LFS project -sp $projid $testdir || error "failed to set project ID"
4297         $LFS setquota -p $projid -B 200M $DIR ||
4298                                 error "failed to to set prj $projid quota"
4299
4300         $RUNAS touch $testfile
4301
4302         local id=$(lfs project -d $testfile | awk '{print $1}')
4303         [ "$id" != "$projid" ] && error "projid $projid is not inherited $id"
4304
4305         echo "test by mirror created with normal file"
4306         test_mirror $projid $testfile $testfile
4307
4308         $TRUNCATE $testfile 0
4309         wait_delete_completed || error "wait_delete_completed failed"
4310         sync_all_data || true
4311
4312         $LFS project -sp $projid2 $testdir ||
4313                                 error "failed to set directory project ID"
4314         $LFS project -p $projid2 $testfile ||
4315                                 error "failed to set file project ID"
4316         $LFS setquota -p $projid -b 0 -B 0 $DIR ||
4317                                 error "failed to to reset prj quota"
4318         $LFS setquota -p $projid2 -B 200M $DIR ||
4319                                 error "failed to to set prj $projid2 quota"
4320
4321         local fid=$($LFS path2fid $testfile)
4322
4323         echo "test by mirror created with FID"
4324         test_mirror $projid2 $testfile $MOUNT/.lustre/fid/$fid
4325 }
4326 run_test 58 "project ID should be kept for new mirrors created by FID"
4327
4328 test_59() {
4329         [ "$mds1_FSTYPE" != ldiskfs ] &&
4330                 skip "ldiskfs only test"
4331         disable_project_quota
4332         setup_quota_test || error "setup quota failed with $?"
4333         quota_init
4334
4335         local testfile="$DIR/$tdir/$tfile-0"
4336         #make sure it did not crash kernel
4337         touch $testfile && lfs project -sp 1 $testfile
4338
4339         enable_project_quota
4340 }
4341 run_test 59 "lfs project dosen't crash kernel with project disabled"
4342
4343 test_60() {
4344         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
4345                 skip "Needs MDS version 2.11.53 or later."
4346         setup_quota_test || error "setup quota failed with $?"
4347
4348         local testfile=$DIR/$tdir/$tfile
4349         local limit=100
4350
4351         set_mdt_qtype "ug" || error "enable mdt quota failed"
4352
4353         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
4354                 error "set quota failed"
4355         quota_show_check a g $TSTUSR
4356
4357         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
4358         chmod g+s $DIR/$tdir || error "chmod g+s failed"
4359         $RUNAS createmany -m ${testfile} $((limit-1)) ||
4360                 error "create many files failed"
4361
4362         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
4363
4364         # root user can overrun quota
4365         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
4366                 error "root user should succeed"
4367 }
4368 run_test 60 "Test quota for root with setgid"
4369
4370 # test default quota
4371 test_default_quota() {
4372         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
4373                 skip "Not supported before 2.11.51."
4374
4375         local qtype=$1
4376         local qres_type=$2
4377         local qid=$TSTUSR
4378         local qprjid=$TSTPRJID
4379         local qdtype="-U"
4380         local qs="-b"
4381         local qh="-B"
4382         local LIMIT=20480 #20M disk space
4383         local TESTFILE="$DIR/$tdir/$tfile-0"
4384         local $qpool_cmd
4385
4386         [ $qtype == "-p" ] && ! is_project_quota_supported &&
4387                 echo "Project quota is not supported" && return 0
4388
4389         [ $qtype == "-u" ] && qdtype="-U"
4390         [ $qtype == "-g" ] && qdtype="-G"
4391         [ $qtype == "-p" ] && {
4392                 qdtype="-P"
4393                 qid=$qprjid
4394         }
4395
4396         [ $qres_type == "meta" ] && {
4397                 LIMIT=10240 #10K inodes
4398                 qs="-i"
4399                 qh="-I"
4400         }
4401         [ ! -z "$3" ] && {
4402                 qpool_cmd="--pool $3"
4403                 # pool quotas don't work properly without global limit
4404                 $LFS setquota $qtype $qid -B1T -b1T $DIR ||
4405                         error "set global limit failed"
4406         }
4407
4408         setup_quota_test || error "setup quota failed with $?"
4409
4410         quota_init
4411
4412         # enable mdt/ost quota
4413         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4414         set_ost_qtype $QTYPE || error "enable ost quota failed"
4415
4416         log "set to use default quota"
4417         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4418                 error "set $qid to use default quota failed"
4419
4420         log "set default quota"
4421         $LFS setquota $qdtype $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4422                 error "set $qid default quota failed"
4423
4424         log "get default quota"
4425         $LFS quota $qdtype $DIR || error "get default quota failed"
4426
4427         if [ $qres_type == "data" ]; then
4428                 local SLIMIT=$($LFS quota $qpool_cmd $qdtype $DIR | \
4429                                 grep "$MOUNT" | awk '{print $2}')
4430                 [ $SLIMIT -eq $LIMIT ] ||
4431                         error "the returned default quota is wrong"
4432         else
4433                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
4434                                                         awk '{print $5}')
4435                 [ $SLIMIT -eq $LIMIT ] ||
4436                         error "the returned default quota is wrong"
4437         fi
4438
4439         # make sure the system is clean
4440         local USED=$(getquota $qtype $qid global curspace)
4441         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4442
4443         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4444                         error "setstripe $TESTFILE failed"
4445         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4446
4447         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
4448
4449         log "Test not out of quota"
4450         if [ $qres_type == "data" ]; then
4451                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
4452                         quota_error $qtype $qid "write failed, expect succeed"
4453         else
4454                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
4455                         quota_error $qtype $qid "create failed, expect succeed"
4456
4457                 unlinkmany $TESTFILE $((LIMIT/2))
4458         fi
4459
4460         log "Test out of quota"
4461         # flush cache, ensure noquota flag is set on client
4462         cancel_lru_locks osc
4463         cancel_lru_locks mdc
4464         sync; sync_all_data || true
4465         if [ $qres_type == "data" ]; then
4466                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4467                         quota_error $qtype $qid "write succeed, expect EDQUOT"
4468         else
4469                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4470                         quota_error $qtype $qid "create succeed, expect EDQUOT"
4471
4472                 unlinkmany $TESTFILE $((LIMIT*2))
4473         fi
4474
4475         rm -f $TESTFILE
4476         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4477                         error "setstripe $TESTFILE failed"
4478         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4479
4480         log "Increase default quota"
4481
4482         # LU-4505: sleep 5 seconds to enable quota acquire
4483         sleep 5
4484
4485         # increase default quota
4486         $LFS setquota $qdtype $qpool_cmd $qs $((LIMIT*3)) \
4487                 $qh $((LIMIT*3)) $DIR || error "set default quota failed"
4488
4489         cancel_lru_locks osc
4490         cancel_lru_locks mdc
4491         sync; sync_all_data || true
4492         if [ $qres_type == "data" ]; then
4493                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4494                         quota_error $qtype $qid "write failed, expect succeed"
4495         else
4496                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4497                         quota_error $qtype $qid "create failed, expect succeed"
4498
4499                 unlinkmany $TESTFILE $((LIMIT*2))
4500         fi
4501
4502         log "Set quota to override default quota"
4503         $LFS setquota $qtype $qid $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4504                 error "set $qid 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 succeed, expect EQUOT"
4512         else
4513                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4514                         quota_error $qtype $qid "create succeed, expect EQUOT"
4515
4516                 unlinkmany $TESTFILE $((LIMIT*2))
4517         fi
4518
4519         log "Set to use default quota again"
4520
4521         # LU-4505: sleep 5 seconds to enable quota acquire
4522         sleep 5
4523
4524         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4525                 error "set $qid to use default quota failed"
4526
4527         cancel_lru_locks osc
4528         cancel_lru_locks mdc
4529         sync; sync_all_data || true
4530         if [ $qres_type == "data" ]; then
4531                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4532                         quota_error $qtype $qid "write failed, expect succeed"
4533         else
4534                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4535                         quota_error $qtype $qid "create failed, expect succeed"
4536
4537                 unlinkmany $TESTFILE $((LIMIT*2))
4538         fi
4539
4540         log "Cleanup"
4541         rm -f $TESTFILE
4542         wait_delete_completed || error "wait_delete_completed failed"
4543         sync_all_data || true
4544
4545         $LFS setquota $qdtype $qpool_cmd $qs 0 $qh 0 $DIR ||
4546                 error "reset default quota failed"
4547         $LFS setquota $qtype $qid $qpool_cmd $qs 0 $qh 0 $DIR ||
4548                 error "reset quota failed"
4549         cleanup_quota_test
4550 }
4551
4552 test_61() {
4553         test_default_quota "-u" "data"
4554         test_default_quota "-u" "meta"
4555         test_default_quota "-g" "data"
4556         test_default_quota "-g" "meta"
4557         test_default_quota "-p" "data"
4558         test_default_quota "-p" "meta"
4559 }
4560 run_test 61 "default quota tests"
4561
4562 test_62() {
4563         ! is_project_quota_supported &&
4564                 skip "Project quota is not supported"
4565         [[ "$(chattr -h 2>&1)" =~ "project" ||
4566            "$(chattr -h 2>&1)" =~ "pRVf" ]] ||
4567                 skip "chattr did not support project quota"
4568         setup_quota_test || error "setup quota failed with $?"
4569         local testdir=$DIR/$tdir/
4570
4571         $RUNAS mkdir -p $testdir || error "failed to mkdir"
4572         change_project -s $testdir
4573         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4574                 error "inherit attribute should be set"
4575         # chattr used FS_IOC_SETFLAGS ioctl
4576         $RUNAS chattr -P $testdir &&
4577                 error "regular user clear inherit should fail"
4578         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4579                 error "inherit attribute should still be set"
4580         chattr -P $testdir || error "root failed to clear inherit"
4581         [[ $($LFS project -d $testdir) =~ "P" ]] &&
4582                 error "inherit attribute should be cleared"
4583         return 0
4584 }
4585 run_test 62 "Project inherit should be only changed by root"
4586
4587 test_dom() {
4588         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
4589                 skip "Not supported before 2.11.55"
4590
4591         local qtype=$1
4592         local qid=$TSTUSR
4593         local dd_failed=false
4594         local tdir_dom=${tdir}_dom
4595         local LIMIT=20480 #20M
4596
4597         [ $qtype == "p" ] && ! is_project_quota_supported &&
4598                 echo "Project quota is not supported" && return 0
4599
4600         [ $qtype == "p" ] && qid=$TSTPRJID
4601
4602         setup_quota_test || error "setup quota failed with $?"
4603
4604         quota_init
4605
4606         # enable mdt/ost quota
4607         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4608         set_ost_qtype $QTYPE || error "enable ost quota failed"
4609
4610         # make sure the system is clean
4611         local USED=$(getquota -$qtype $qid global curspace)
4612         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4613
4614         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
4615
4616         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
4617         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
4618                 error "setstripe $tdir_dom failed"
4619         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
4620
4621         [ $qtype == "p" ] && {
4622                 change_project -sp $TSTPRJID $DIR/$tdir
4623                 change_project -sp $TSTPRJID $DIR/$tdir_dom
4624         }
4625
4626         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
4627                 error "set $qid quota failed"
4628
4629         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4630                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4631                                                                 dd_failed=true
4632         done
4633
4634         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4635
4636         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
4637                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4638                                                                 dd_failed=true
4639         done
4640
4641         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4642
4643         rm -f $DIR/$tdir_dom/*
4644
4645         # flush cache, ensure noquota flag is set on client
4646         cancel_lru_locks osc
4647         cancel_lru_locks mdc
4648         sync; sync_all_data || true
4649
4650         dd_failed=false
4651
4652         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4653                 quota_error $qtype $qid "write failed, expect succeed"
4654
4655         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4656                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4657                                                                 dd_failed=true
4658         done
4659
4660         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4661
4662         rm -f $DIR/$tdir/*
4663         rm -f $DIR/$tdir_dom/*
4664
4665         # flush cache, ensure noquota flag is set on client
4666         cancel_lru_locks osc
4667         cancel_lru_locks mdc
4668         sync; sync_all_data || true
4669
4670         dd_failed=false
4671
4672         for ((i = 0; i < $((LIMIT/2048)); 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 $qid "write failed, expect succeed"
4678
4679         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4680                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4681
4682         rm -fr $DIR/$tdir
4683         rm -fr $DIR/$tdir_dom
4684
4685         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4686                 error "reset usr quota failed"
4687 }
4688
4689 test_63() {
4690         test_dom "u"
4691         test_dom "g"
4692         test_dom "p"
4693 }
4694 run_test 63 "quota on DoM tests"
4695
4696 test_64() {
4697         ! is_project_quota_supported &&
4698                 skip "Project quota is not supported"
4699         setup_quota_test || error "setup quota failed with $?"
4700         local dir1="$DIR/$tdir/"
4701
4702         touch $dir1/file
4703         ln -s $dir1/file $dir1/file_link
4704         mkfifo $dir1/fifo
4705
4706         $LFS project -srp $TSTPRJID $dir1 >&/dev/null ||
4707                 error "set project should succeed"
4708
4709         used=$(getquota -p $TSTPRJID global curinodes)
4710         [ $used -eq 4 ] || error "expected 4 got $used"
4711         $LFS project -rC $dir1 >&/dev/null ||
4712                 error "clear project should succeed"
4713
4714         used=$(getquota -p $TSTPRJID global curinodes)
4715         [ $used -eq 0 ] || error "expected 0 got $used"
4716 }
4717 run_test 64 "lfs project on non dir/files should succeed"
4718
4719 test_65() {
4720         local SIZE=10 # MB
4721         local TESTFILE="$DIR/$tdir/$tfile-0"
4722
4723         setup_quota_test || error "setup quota failed with $?"
4724         set_ost_qtype $QTYPE || error "enable ost quota failed"
4725         quota_init
4726
4727         echo "Write..."
4728         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4729                 error "failed to write"
4730         # flush cache, ensure noquota flag is set on client
4731         cancel_lru_locks osc
4732         sync; sync_all_data || true
4733
4734         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4735         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4736         local quota_all=$($RUNAS $LFS quota $DIR)
4737
4738         [ "$(echo "$quota_all" | head -n3)" == "$quota_u" ] ||
4739                 error "usr quota not match"
4740         [ "$(echo "$quota_all" | tail -n3)" == "$quota_g" ] ||
4741                 error "grp quota not match"
4742 }
4743 run_test 65 "Check lfs quota result"
4744
4745 test_66() {
4746         ! is_project_quota_supported &&
4747                 skip "Project quota is not supported"
4748         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4749                 skip "Not supported before 2.12.4"
4750         setup_quota_test || error "setup quota failed with $?"
4751         local old=$(do_facet mds1 $LCTL get_param -n \
4752                     mdt.*.enable_chprojid_gid | head -1)
4753         local testdir=$DIR/$tdir/foo
4754
4755         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4756         stack_trap "do_facet mds1 $LCTL \
4757                 set_param mdt.*.enable_chprojid_gid=$old" EXIT
4758
4759         mkdir_on_mdt0 $testdir || error "failed to mkdir"
4760         chown -R $TSTID:$TSTID $testdir
4761         change_project -sp $TSTPRJID $testdir
4762         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4763
4764         $RUNAS lfs project -p 0 $testdir/foo &&
4765                 error "nonroot user should fail to set projid"
4766
4767         $RUNAS lfs project -C $testdir/foo &&
4768                 error "nonroot user should fail to clear projid"
4769
4770         change_project -C $testdir/foo || error "failed to clear project"
4771
4772         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4773         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4774         "failed to set projid with normal user when enable_chprojid_gid=-1"
4775
4776         $RUNAS lfs project -rC $testdir/ || error \
4777 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4778
4779         touch $testdir/bar || error "failed touch $testdir/bar"
4780         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4781         "normal user should not be able to set projid on root owned file"
4782
4783         change_project -p $TSTPRJID $testdir/bar || error \
4784                 "root should be able to change its own file's projid"
4785 }
4786 run_test 66 "nonroot user can not change project state in default"
4787
4788 test_67_write() {
4789         local file="$1"
4790         local qtype="$2"
4791         local size=$3
4792         local _runas=""
4793         local short_qtype=${qtype:0:1}
4794
4795         echo "file "$file
4796         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4797         case "$4" in
4798                 $TSTUSR)  _runas=$RUNAS;;
4799                 $TSTUSR2) _runas=$RUNAS2;;
4800                 *)          error "unknown quota parameter $4";;
4801         esac
4802
4803         log "Write..."
4804         date
4805         $_runas $DD of=$file count=$size ||
4806                 quota_error $short_qtype $TSTUSR \
4807                         "$qtype write failure, but expect success"
4808         date
4809         cancel_lru_locks osc
4810         date
4811         sync; sync_all_data || true
4812         date
4813 }
4814
4815 getgranted() {
4816         local pool=$1
4817         local ptype=$2
4818         local userid=$3
4819         local qtype=$4
4820         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4821
4822         do_facet mds1 $LCTL get_param $param |
4823                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4824 }
4825
4826 test_67() {
4827         local limit=20 # MB
4828         local testfile="$DIR/$tdir/$tfile-0"
4829         local testfile2="$DIR/$tdir/$tfile-1"
4830         local testfile3="$DIR/$tdir/$tfile-2"
4831         local qpool="qpool1"
4832         local used
4833         local granted
4834         local granted_mb
4835
4836         mds_supports_qp
4837         [ "$ost1_FSTYPE" == zfs ] &&
4838                 skip "ZFS grants some block space together with inode"
4839
4840         setup_quota_test || error "setup quota failed with $?"
4841
4842         # enable ost quota
4843         set_ost_qtype $QTYPE || error "enable ost quota failed"
4844
4845         # test for user
4846         log "User quota (block hardlimit:$limit MB)"
4847         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4848                 error "set user quota failed"
4849
4850         # make sure the system is clean
4851         used=$(getquota -u $TSTUSR global curspace)
4852         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4853
4854         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4855         echo "granted 0x0 before write $granted"
4856
4857         # trigger reintegration
4858         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4859         procf=${procf}quota_slave.force_reint
4860         do_facet ost1 $LCTL set_param $procf=1 ||
4861                 error "force reintegration failed"
4862         wait_ost_reint "u" || error "reintegration failed"
4863         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4864         [ $granted -ne 0 ] &&
4865                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4866
4867         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4868         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4869
4870         # write 10 MB to testfile
4871         test_67_write "$testfile" "user" 10 "$TSTUSR"
4872
4873         # create qpool and add OST1
4874         pool_add $qpool || error "pool_add failed"
4875         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4876         # as $TSTUSR hasn't limits, lqe may absent. But it should be
4877         # created after the 1st direct qmt_get.
4878         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4879
4880         # check granted - should be 0, as testfile is located only on OST0
4881         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4882         echo "global granted $granted"
4883         granted=$(getgranted $qpool "dt" $TSTID "usr")
4884         echo "$qpool granted $granted"
4885         [ $granted -ne 0 ] &&
4886                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4887
4888         # add OST0 to qpool and check granted space
4889         pool_add_targets $qpool 0 1 ||
4890                 error "pool_add_targets failed"
4891         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4892         echo "Granted $granted_mb MB"
4893         #should be 10M + qunit for each OST
4894         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4895                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4896
4897         $LFS setstripe $testfile2 -c 1 -i 1 ||
4898                 error "setstripe $testfile2 failed"
4899         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4900         # Write from another user and check that qpool1
4901         # shows correct granted, despite $TSTUSR2 hasn't limits in qpool1.
4902         test_67_write "$testfile2" "user" 10 "$TSTUSR2"
4903         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4904         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4905         [ $granted -ne 0 ] &&
4906                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4907
4908         # Granted space for $TSTUSR2 in qpool1 should appear only
4909         # when global lqe for this user becomes enforced.
4910         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4911                 error "set user quota failed"
4912         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4913         echo "granted_mb $granted_mb"
4914         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4915                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4916
4917         $LFS setstripe $testfile3 -c 1 -i 0 ||
4918                 error "setstripe $testfile3 failed"
4919         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4920         test_67_write "$testfile3" "user" 10 "$TSTUSR2"
4921         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4922         echo "$testfile3 granted_mb $granted_mb"
4923         [ $granted_mb -eq $limit ] ||
4924                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4925
4926         # remove OST1 from the qpool1 and check granted space
4927         # should be 0 for TSTUSR and 10M for TSTUSR2
4928         pool_remove_target $qpool 0
4929         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4930         [ $granted_mb -eq 0 ] ||
4931                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4932         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4933         [ $granted_mb -eq 10 ] ||
4934                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4935
4936         rm -f $testfile
4937         wait_delete_completed || error "wait_delete_completed failed"
4938         sync_all_data || true
4939         used=$(getquota -u $TSTUSR global curspace)
4940         [ $used -eq 0 ] || quota_error u $TSTUSR \
4941                 "user quota isn't released after deletion"
4942 }
4943 run_test 67 "quota pools recalculation"
4944
4945 get_slave_nr() {
4946         local pool=$1
4947         local qtype=$2
4948         local nr
4949
4950         wait_update_facet "--quiet" mds1 \
4951                 "$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info \
4952                         >/dev/null 2>&1 || echo foo" "">/dev/null ||
4953         error "mds1: failed to create quota pool $pool"
4954
4955         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4956                 awk '/usr/ {getline; print $2}'
4957 }
4958
4959 test_68()
4960 {
4961         local qpool="qpool1"
4962
4963         mds_supports_qp
4964         setup_quota_test || error "setup quota failed with $?"
4965
4966         # enable ost quota
4967         set_ost_qtype $QTYPE || error "enable ost quota failed"
4968
4969         # check slave number for glbal pool
4970         local nr=$(get_slave_nr "0x0" "usr")
4971         echo "nr result $nr"
4972         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4973                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4974
4975         # create qpool and add OST1
4976         pool_add $qpool || error "pool_add failed"
4977         nr=$(get_slave_nr $qpool "usr")
4978         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4979
4980         # add OST1 to qpool
4981         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4982         nr=$(get_slave_nr $qpool "usr")
4983         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4984
4985         # add OST0 to qpool
4986         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4987         nr=$(get_slave_nr $qpool "usr")
4988         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
4989
4990         # remove OST0
4991         pool_remove_target $qpool 0
4992         nr=$(get_slave_nr $qpool "usr")
4993         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4994
4995         # remove OST1
4996         pool_remove_target $qpool 1
4997         nr=$(get_slave_nr $qpool "usr")
4998         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4999
5000         # Check again that all is fine with global pool
5001         nr=$(get_slave_nr "0x0" "usr")
5002         [[ $nr == $((OSTCOUNT + MDSCOUNT)) ]] ||
5003                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
5004 }
5005 run_test 68 "slave number in quota pool changed after each add/remove OST"
5006
5007 test_69()
5008 {
5009         local global_limit=200 # MB
5010         local limit=10 # MB
5011         local testfile="$DIR/$tdir/$tfile-0"
5012         local dom0="$DIR/$tdir/dom0"
5013         local qpool="qpool1"
5014
5015         mds_supports_qp
5016         setup_quota_test || error "setup quota failed with $?"
5017
5018         # enable ost quota
5019         set_ost_qtype $QTYPE || error "enable ost quota failed"
5020         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
5021
5022         # Save DOM only at MDT0
5023         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
5024         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
5025         chmod 0777 $dom0
5026         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
5027
5028         # create qpool and add OST0
5029         pool_add $qpool || error "pool_add failed"
5030         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
5031
5032         log "User quota (block hardlimit:$global_limit MB)"
5033         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5034                 error "set user quota failed"
5035
5036         log "User quota (block hardlimit:$limit MB)"
5037         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5038                 error "set user quota failed"
5039
5040         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
5041                 quota_error u $TSTUSR "write failed"
5042
5043         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
5044                 oflag=sync || quota_error u $TSTUSR "write failed"
5045
5046         $RUNAS $DD of=$testfile count=$limit || true
5047
5048         # flush cache, ensure noquota flag is set on client
5049         cancel_lru_locks osc
5050         sync; sync_all_data || true
5051
5052         # MDT0 shouldn't get EDQUOT with glimpse.
5053         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
5054                 quota_error u $TSTUSR \
5055                         "user write success, but expect EDQUOT"
5056
5057         # Now all members of qpool1 should get EDQUOT. Expect success
5058         # when write to DOM on MDT0, as it belongs to global pool.
5059         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
5060                 oflag=sync || quota_error u $TSTUSR "write failed"
5061
5062         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
5063                 oflag=sync || quota_error u $TSTUSR "write failed"
5064 }
5065 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
5066
5067 test_70a()
5068 {
5069         local qpool="qpool1"
5070         local limit=20 # MB
5071         local err=0
5072         local bhard
5073
5074         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
5075                 skip "Needs a client >= $VERSION_WITH_QP"
5076
5077         setup_quota_test || error "setup quota failed with $?"
5078
5079         # MDS returns EFAULT for unsupported quotactl command
5080         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
5081
5082         # create qpool and add OST0
5083         pool_add $qpool || error "pool_add failed"
5084         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
5085
5086         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
5087         rc=$?
5088         [ $rc -eq $err ] || error "setquota res $rc != $err"
5089
5090         # If MDS supports QP, check that limit was set properly.
5091         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
5092                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
5093                 echo "hard limit $bhard limit $limit"
5094                 [ $bhard -ne $((limit*1024)) ] &&
5095                         error "bhard:$bhard for $qpool!=$((limit*1024))"
5096         fi
5097
5098         $LFS quota -u $TSTUSR --pool $qpool $DIR
5099         rc=$?
5100         [ $rc -eq $err ] || error "quota res $rc != $err"
5101 }
5102 run_test 70a "check lfs setquota/quota with a pool option"
5103
5104 test_70b()
5105 {
5106         local glbl_hard=200 # 200M
5107         local glbl_soft=100 # 100M
5108         local pool_hard=10 # 10M
5109         local qpool="qpool1"
5110
5111         pool_add $qpool || error "pool_add failed"
5112         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
5113
5114         $LFS setquota -u $TSTUSR -b ${glbl_soft}M -B ${glbl_hard}M $DIR ||
5115                 error "set user quota failed"
5116         $LFS setquota -u $TSTUSR -B ${pool_hard}M --pool $qpool $DIR ||
5117                 error "set user quota failed"
5118
5119         local tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5120         [ $tmp -eq $((pool_hard * 1024)) ] ||
5121                 error "wrong block hard limit $tmp for $qpool"
5122         local tmp=$(getquota -u $TSTUSR global bsoftlimit $qpool)
5123         # soft limit hasn't been set and should be zero
5124         [ $tmp -eq 0 ] || error "wrong soft block limit $tmp for $qpool"
5125 }
5126 run_test 70b "lfs setquota pool works properly"
5127
5128 test_71a()
5129 {
5130         local limit=10 # MB
5131         local global_limit=100 # MB
5132         local testfile="$DIR/$tdir/$tfile-0"
5133         local qpool="qpool1"
5134         local qpool2="qpool2"
5135
5136         [ "$ost1_FSTYPE" == zfs ] &&
5137                 skip "ZFS grants some block space together with inode"
5138         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
5139         mds_supports_qp
5140         setup_quota_test || error "setup quota failed with $?"
5141
5142         # enable ost quota
5143         set_ost_qtype $QTYPE || error "enable ost quota failed"
5144
5145         # test for user
5146         log "User quota (block hardlimit:$global_limit MB)"
5147         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5148                 error "set user quota failed"
5149
5150         pool_add $qpool || error "pool_add failed"
5151         pool_add_targets $qpool 0 1 ||
5152                 error "pool_add_targets failed"
5153
5154         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5155                 error "set user quota failed"
5156
5157         pool_add $qpool2 || error "pool_add failed"
5158         pool_add_targets $qpool2 1 1 ||
5159                 error "pool_add_targets failed"
5160
5161         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
5162                 error "set user quota failed"
5163
5164         # make sure the system is clean
5165         local used=$(getquota -u $TSTUSR global curspace)
5166
5167         echo "used $used"
5168         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5169
5170         # create 1st component 1-10M
5171         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5172         #create 2nd component 10-30M
5173         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5174         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5175
5176         # сheck normal use and out of quota with PFL
5177         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
5178         test_1_check_write $testfile "user" $((limit*2))
5179         rm -f $testfile
5180         wait_delete_completed || error "wait_delete_completed failed"
5181         sync_all_data || true
5182         used=$(getquota -u $TSTUSR global curspace)
5183         [ $used -ne 0 ] && quota_error u $TSTUSR \
5184                 "user quota isn't released after deletion"
5185
5186         # create 1st component 1-10M
5187         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5188         # create 2nd component 10-30M
5189         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5190         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5191
5192         # write to the 2nd component
5193         $RUNAS $DD of=$testfile count=$limit seek=10 ||
5194                 quota_error u $TSTUSR \
5195                         "write failure, but expect success"
5196         # this time maybe cache write,  ignore it's failure
5197         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
5198         cancel_lru_locks osc
5199         sync; sync_all_data || true
5200         # write over limit in qpool2(2nd component 10-30M)
5201         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
5202                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5203         # write to the 1st component - OST0 is empty
5204         $RUNAS $DD of=$testfile count=$limit seek=0 ||
5205                 quota_error u $TSTUSR "write failed"
5206 }
5207 run_test 71a "Check PFL with quota pools"
5208
5209 test_71b()
5210 {
5211         local global_limit=1000 # MB
5212         local limit1=160 # MB
5213         local limit2=10 # MB
5214         local testfile="$DIR/$tdir/$tfile-0"
5215         local qpool="qpool1"
5216         local qpool2="qpool2"
5217
5218         [ "$ost1_FSTYPE" == zfs ] &&
5219                 skip "ZFS grants some block space together with inode"
5220         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
5221         mds_supports_qp
5222         setup_quota_test || error "setup quota failed with $?"
5223
5224         # enable ost quota
5225         set_ost_qtype $QTYPE || error "enable ost quota failed"
5226
5227         # test for user
5228         log "User quota (block hardlimit:$global_limit MB)"
5229         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5230                 error "set user quota failed"
5231
5232         pool_add $qpool || error "pool_add failed"
5233         pool_add_targets $qpool 0 1 ||
5234                 error "pool_add_targets failed"
5235
5236         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
5237                 error "set user quota failed"
5238
5239         pool_add $qpool2 || error "pool_add failed"
5240         pool_add_targets $qpool2 1 1 ||
5241                 error "pool_add_targets failed"
5242
5243         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5244                 error "set user quota failed"
5245
5246         # make sure the system is clean
5247         local used=$(getquota -u $TSTUSR global curspace)
5248
5249         echo "used $used"
5250         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5251
5252         # First component is on OST0, 2nd on OST1
5253         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
5254         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5255
5256         # fill the 1st component on OST0
5257         $RUNAS $DD of=$testfile count=128 ||
5258                 quota_error u $TSTUSR "write failed"
5259         # write to the 2nd cmpnt on OST1
5260         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
5261                 quota_error u $TSTUSR "write failed"
5262         # this time maybe cache write,  ignore it's failure
5263         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
5264                 true
5265         cancel_lru_locks osc
5266         sync; sync_all_data || true
5267         # write over limit in qpool2
5268         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
5269                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5270         return 0
5271 }
5272 run_test 71b "Check SEL with quota pools"
5273
5274 test_72()
5275 {
5276         local limit=10 # MB
5277         local global_limit=50 # MB
5278         local testfile="$DIR/$tdir/$tfile-0"
5279         local qpool="qpool1"
5280
5281         mds_supports_qp
5282         setup_quota_test || error "setup quota failed with $?"
5283
5284         # enable ost quota
5285         set_ost_qtype $QTYPE || error "enable ost quota failed"
5286
5287         # test for user
5288         log "User quota (block hardlimit:$global_limit MB)"
5289         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5290                 error "set user quota failed"
5291
5292         pool_add $qpool || error "pool_add failed"
5293         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
5294
5295         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5296                 error "set user quota failed"
5297
5298         # make sure the system is clean
5299         local used=$(getquota -u $TSTUSR global curspace)
5300         echo "used $used"
5301         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5302
5303         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5304
5305         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
5306         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5307         test_1_check_write $testfile "user" $limit
5308         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5309         echo "used $used"
5310         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
5311         # check that lfs quota -uv --pool prints only OST that
5312         # was added in a pool
5313         lfs quota -v -u $TSTUSR --pool $qpool $DIR | grep -v "OST0001" |
5314                 grep "OST\|MDT" && error "$qpool consists wrong targets"
5315         return 0
5316 }
5317 run_test 72 "lfs quota --pool prints only pool's OSTs"
5318
5319 test_73a()
5320 {
5321         (( $MDS1_VERSION >= $(version_code 2.14.51.158) )) ||
5322                 skip "need MDS >= v2_14_51-158-g25a70a88 for default pool quota"
5323
5324         local qpool="qpool1"
5325
5326         mds_supports_qp
5327
5328         pool_add $qpool || error "pool_add failed"
5329         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5330                 error "pool_add_targets failed"
5331
5332         test_default_quota "-u" "data" "qpool1"
5333 }
5334 run_test 73a "default limits at OST Pool Quotas"
5335
5336 test_73b()
5337 {
5338         (( $MDS1_VERSION >= $(version_code 2.14.52.91) )) ||
5339                 skip "need MDS >= v2_14_52-91-g188112fc8 for nested lqe fix"
5340
5341         local TESTFILE1="$DIR/$tdir/$tfile-1"
5342         local limit=20 #20M
5343         local qpool="qpool1"
5344
5345         mds_supports_qp
5346
5347         setup_quota_test || error "setup quota failed with $?"
5348         quota_init
5349         set_ost_qtype $QTYPE || error "enable ost quota failed"
5350
5351         # pool quotas don't work properly without global limit
5352         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
5353                 error "set global limit failed"
5354
5355         pool_add $qpool || error "pool_add failed"
5356         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5357                 error "pool_add_targets failed"
5358
5359         log "set default quota for $qpool"
5360         $LFS setquota -U --pool $qpool -b ${limit}M -B ${limit}M $DIR ||
5361                 error "set default quota failed"
5362
5363         log "Write from user that hasn't lqe"
5364         # Check that it doesn't cause a panic or a deadlock
5365         # due to nested lqe lookups that rewrite 1st lqe in qti_lqes array.
5366         # Have to use RUNAS_ID as resetquota creates lqes in
5367         # the beginning for TSTUSR/TSTUSR2 when sets limits to 0.
5368         runas -u $RUNAS_ID -g $RUNAS_GID $DD of=$TESTFILE1 count=10
5369
5370         cancel_lru_locks osc
5371         sync; sync_all_data || true
5372 }
5373 run_test 73b "default OST Pool Quotas limit for new user"
5374
5375 test_74()
5376 {
5377         (( $MDS1_VERSION >= $(version_code 2.14.52.6) )) ||
5378                 skip "need MDS >= v2_14_52-6-g8c19365416 for pool per-user fix"
5379
5380         local global_limit=200 # 200M
5381         local limit=10 # 10M
5382         local limit2=50 # 50M
5383         local qpool="qpool1"
5384         local qpool2="qpool2"
5385         local tmp=0
5386
5387         mds_supports_qp
5388         setup_quota_test || error "setup quota failed with $?"
5389
5390         # enable ost quota
5391         set_ost_qtype $QTYPE || error "enable ost quota failed"
5392
5393         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5394                 error "set user quota failed"
5395
5396         pool_add $qpool || error "pool_add failed"
5397         pool_add_targets $qpool 0 1 ||
5398                 error "pool_add_targets failed"
5399
5400         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5401                 error "set user quota failed"
5402
5403         pool_add $qpool2 || error "pool_add failed"
5404         pool_add_targets $qpool2 1 1 ||
5405                 error "pool_add_targets failed"
5406
5407         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5408                 error "set user quota failed"
5409
5410         tmp=$(getquota -u $TSTUSR global bhardlimit)
5411         [ $tmp -eq $((global_limit * 1024)) ] ||
5412                 error "wrong global limit $global_limit"
5413
5414         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5415         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit $tmp for $qpool"
5416
5417         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool2)
5418         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit $tmp for $qpool2"
5419
5420         # check limits in pools
5421         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5422               grep -A4 $qpool | awk 'NR == 4{print $4}')
5423         echo "pool limit for $qpool $tmp"
5424         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit:tmp for $qpool"
5425         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5426               grep -A4 $qpool2 | awk 'NR == 4{print $4}')
5427         echo "pool limit for $qpool2 $tmp"
5428         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit:$tmp for $qpool2"
5429 }
5430 run_test 74 "check quota pools per user"
5431
5432 function cleanup_quota_test_75()
5433 {
5434         do_facet mgs $LCTL nodemap_modify --name default \
5435                 --property admin --value 1
5436         do_facet mgs $LCTL nodemap_modify --name default \
5437                 --property trusted --value 1
5438         do_facet mgs $LCTL nodemap_modify --name default \
5439                 --property squash_uid --value 99
5440         do_facet mgs $LCTL nodemap_modify --name default \
5441                 --property squash_gid --value 99
5442
5443         wait_nm_sync default admin_nodemap
5444         wait_nm_sync default trusted_nodemap
5445
5446         do_facet mgs $LCTL nodemap_activate 0
5447         wait_nm_sync active
5448
5449         resetquota -u $TSTUSR
5450 }
5451
5452 test_dom_75() {
5453         local dd_failed=false
5454         local LIMIT=20480 #20M
5455         local qid=$TSTID
5456
5457         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5458                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5459                         conv=fsync || dd_failed=true
5460         done
5461
5462         $dd_failed && quota_error u $qid "write failed, expect succeed (1)"
5463
5464         for ((i = $((LIMIT/2048-1)); i < $((LIMIT/1024 + 10)); i++)); do
5465                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5466                         conv=fsync || dd_failed=true
5467         done
5468
5469         $dd_failed || quota_error u $qid "write succeed, expect EDQUOT (1)"
5470
5471         rm -f $DIR/$tdir_dom/*
5472
5473         # flush cache, ensure noquota flag is set on client
5474         cancel_lru_locks
5475         sync; sync_all_data || true
5476
5477         dd_failed=false
5478
5479         $DD of=$DIR/$tdir/file count=$((LIMIT/2048-1)) conv=fsync ||
5480                 quota_error u $qid "write failed, expect succeed (2)"
5481
5482         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
5483                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5484                         conv=fsync || dd_failed=true
5485         done
5486
5487         $dd_failed || quota_error u $TSTID "write succeed, expect EDQUOT (2)"
5488
5489         rm -f $DIR/$tdir/*
5490         rm -f $DIR/$tdir_dom/*
5491
5492         # flush cache, ensure noquota flag is set on client
5493         cancel_lru_locks
5494         sync; sync_all_data || true
5495
5496         dd_failed=false
5497
5498         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5499                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5500                         conv=fsync || dd_failed=true
5501         done
5502
5503         $dd_failed && quota_error u $qid "write failed, expect succeed (3)"
5504
5505         $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=direct &&
5506                 quota_error u $qid "write succeed, expect EDQUOT (3)"
5507         true
5508 }
5509
5510 test_75()
5511 {
5512         (( $MDS1_VERSION >= $(version_code 2.14.52.68) )) ||
5513                 skip "need MDS >= v2_14_52-68-ga4fbe7341b for squash root fix"
5514
5515         local soft_limit=10 # MB
5516         local hard_limit=20 # MB
5517         local limit=$soft_limit
5518         local testfile="$DIR/$tdir/$tfile-0"
5519         local grace=20 # seconds
5520         local tdir_dom=${tdir}_dom
5521
5522         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
5523             grace=60
5524         fi
5525
5526         setup_quota_test || error "setup quota failed with $?"
5527         stack_trap cleanup_quota_test_75 EXIT
5528
5529         # enable ost quota
5530         set_ost_qtype $QTYPE || error "enable ost quota failed"
5531         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
5532
5533         local used=$(getquota -u $TSTID global curspace)
5534         $LFS setquota -t -u --block-grace $grace --inode-grace \
5535                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
5536         $LFS setquota -u $TSTUSR -b $((soft_limit+used/1024))M \
5537                         -B $((hard_limit+used/1024))M -i 0 -I 0 $DIR ||
5538                 error "set user quota failed"
5539
5540         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
5541         mkdir $DIR/$tdir_dom
5542         chmod 777 $DIR/$tdir_dom
5543         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
5544                 error "setstripe $tdir_dom failed"
5545
5546         do_facet mgs $LCTL nodemap_activate 1
5547         wait_nm_sync active
5548         do_facet mgs $LCTL nodemap_modify --name default \
5549                 --property admin --value 0
5550         do_facet mgs $LCTL nodemap_modify --name default \
5551                 --property trusted --value 0
5552         do_facet mgs $LCTL nodemap_modify --name default \
5553                 --property deny_unknown --value 0
5554         do_facet mgs $LCTL nodemap_modify --name default \
5555                 --property squash_uid --value $TSTID
5556         do_facet mgs $LCTL nodemap_modify --name default \
5557                 --property squash_gid --value $TSTID
5558         cancel_lru_locks mdc
5559         wait_nm_sync default admin_nodemap
5560         wait_nm_sync default trusted_nodemap
5561         wait_nm_sync default squash_uid
5562
5563         # mmap write when over soft limit
5564         limit=$soft_limit
5565         $DD of=$testfile count=${limit} || quota_error a $TSTUSR \
5566                         "root write failure, but expect success (1)"
5567         OFFSET=$((limit * 1024))
5568         cancel_lru_locks osc
5569
5570         echo "Write to exceed soft limit"
5571         dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
5572               quota_error a $TSTUSR "root write failure, but expect success (2)"
5573         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
5574         cancel_lru_locks osc
5575
5576         echo "mmap write when over soft limit"
5577         $MULTIOP $testfile.mmap OT40960SMW ||
5578                 quota_error a $TSTUSR "mmap write failure, but expect success"
5579         cancel_lru_locks osc
5580         rm -f $testfile*
5581         wait_delete_completed || error "wait_delete_completed failed (1)"
5582         sync_all_data || true
5583
5584         # test for user hard limit
5585         limit=$hard_limit
5586         log "Write..."
5587         $DD of=$testfile count=$((limit/2)) ||
5588                 quota_error u $TSTID \
5589                         "root write failure, but expect success (3)"
5590
5591         log "Write out of block quota ..."
5592         # possibly a cache write, ignore failure
5593         $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
5594         # flush cache, ensure noquota flag is set on client
5595         cancel_lru_locks osc
5596         sync; sync_all_data || true
5597         # sync forced cache flush, but did not guarantee that slave
5598         # got new edquot through glimpse, so wait to make sure
5599         sleep 5
5600         $DD of=$testfile count=1 seek=$limit conv=fsync &&
5601                 quota_error u $TSTID \
5602                         "user write success, but expect EDQUOT"
5603         rm -f $testfile
5604         wait_delete_completed || error "wait_delete_completed failed (2)"
5605         sync_all_data || true
5606         [ $(getquota -u $TSTUSR global curspace) -eq $used ] ||
5607                 quota_error u $TSTID "user quota not released after deletion"
5608
5609         test_dom_75
5610 }
5611 run_test 75 "nodemap squashed root respects quota enforcement"
5612
5613 test_76() {
5614         (( $MDS1_VERSION >= $(version_code 2.14.52.109) )) ||
5615                 skip "need MDS >= v2_14_52-109-g3ffa5d680f for bad PRJID fix"
5616         ! is_project_quota_supported &&
5617                 skip "skip project quota unsupported"
5618
5619         setup_quota_test || error "setup quota failed with $?"
5620         quota_init
5621
5622         local testfile="$DIR/$tdir/$tfile-0"
5623
5624         touch $testfile
5625         $LFS project -p 4294967295 $testfile &&
5626                 error "set project ID should fail"
5627         return 0
5628 }
5629 run_test 76 "project ID 4294967295 should be not allowed"
5630
5631 test_77()
5632 {
5633         (( $MDS1_VERSION >= $(version_code 2.14.54.33) )) ||
5634                 skip "need MDS >= v2_14_54-33-g29e00cecc6 for readonly fix"
5635
5636         mount_client $MOUNT2 "ro"
5637         lfs setquota -u $TSTUSR -b 100M -B 100M -i 10K -I 10K $MOUNT2 &&
5638                 error "lfs setquota should fail in read-only Lustre mount"
5639         umount $MOUNT2
5640 }
5641 run_test 77 "lfs setquota should fail in Lustre mount with 'ro'"
5642
5643 test_78A()
5644 {
5645         (( $OST1_VERSION >= $(version_code 2.14.55.173) )) ||
5646                 skip "need OST >= v2_14_55-173-g789038c97a for fallocate fix"
5647         check_set_fallocate_or_skip
5648
5649         setup_quota_test || error "setup quota failed with $?"
5650
5651         # enable ost quota
5652         set_ost_qtype $QTYPE || error "enable ost quota failed"
5653
5654         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5655         chown $TSTUSR $DIR/$tdir || error "failed to chown $tdir"
5656
5657         # setup quota limit
5658         $LFS setquota -u $TSTUSR -b25M -B25M $DIR/$tdir ||
5659                 error "lfs setquota failed"
5660
5661         # call fallocate
5662         runas -u $TSTUSR -g $TSTUSR fallocate -l 204800 $DIR/$tdir/$tfile
5663
5664         kbytes=$(lfs quota -u $TSTUSR $DIR |
5665                 awk -v pattern=$DIR 'match($0, pattern) {printf $2}')
5666         echo "kbytes returned:$kbytes"
5667
5668         # For file size of 204800. We should be having roughly 200 kbytes
5669         # returned. Anything alarmingly low (50 taken as arbitrary value)
5670         # would bail out this TC. Also this also avoids $kbytes of 0
5671         # to be used in calculation below.
5672         (( $kbytes > 50 )) ||
5673                 error "fallocate did not use quota. kbytes returned:$kbytes"
5674
5675         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5676         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5677
5678         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5679         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5680                 error "fallocate did not use quota correctly"
5681 }
5682 run_test 78A "Check fallocate increase quota usage"
5683
5684 test_78a()
5685 {
5686         (( $CLIENT_VERSION >= $(version_code 2.15.0.37) )) ||
5687                 skip "need client >= v2_15_50-37-g5fc934eb for falloc proj fix"
5688         (( $OST1_VERSION >= $(version_code 2.15.0.37) )) ||
5689                 skip "need OST >= v2_15_50-37-g5fc934ebbb for falloc proj fix"
5690         check_set_fallocate_or_skip
5691
5692         setup_quota_test || error "setup quota failed with $?"
5693
5694         # enable ost quota
5695         set_ost_qtype $QTYPE || error "enable ost quota failed"
5696
5697         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5698
5699         local projectid=5200 # Random project id to test
5700
5701         change_project -sp $projectid $DIR/$tdir
5702
5703         # setup quota limit
5704         $LFS setquota -p $projectid -b25M -B25M $DIR/$tdir ||
5705                 error "lfs setquota project failed"
5706
5707         # call fallocate
5708         fallocate -l 204800 $DIR/$tdir/$tfile
5709
5710         # Get curspace (kbytes) for $projectid
5711         local kbytes=$(getquota -p $projectid global curspace)
5712
5713         echo "kbytes returned:$kbytes"
5714
5715         # For file size of 204800. We should be having roughly 200 kbytes
5716         # returned. Anything alarmingly low (50 taken as arbitrary value)
5717         # would bail out this TC. Also this also avoids $kbytes of 0
5718         # to be used in calculation below.
5719         (( $kbytes > 50 )) ||
5720                 error "fallocate did not use projectid. kbytes returned:$kbytes"
5721
5722         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5723         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5724
5725         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5726         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5727                 error "fallocate did not use quota projectid correctly"
5728 }
5729 run_test 78a "Check fallocate increase projectid usage"
5730
5731 test_79()
5732 {
5733         (( $MDS1_VERSION >= $(version_code 2.14.56.37) )) ||
5734                 skip "need MDS >= v2_14_56-37-gc9901b68b4 for pool panic fix"
5735
5736         local qpool="qpool1"
5737         local cmd="$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$qpool.info"
5738         local stopf=$TMP/$tfile
5739
5740         do_facet mds1 "touch $stopf"
5741         stack_trap "do_facet mds1 'rm -f $stopf'"
5742         do_facet mds1 "while [ -e $stopf ]; do $cmd &>/dev/null; done"&
5743         local pid=$!
5744         pool_add $qpool || error "pool_add failed"
5745         do_facet mds1 "rm $stopf"
5746         wait $pid
5747 }
5748 run_test 79 "access to non-existed dt-pool/info doesn't cause a panic"
5749
5750 test_80()
5751 {
5752         (( $MDS1_VERSION >= $(version_code 2.14.56.51) )) ||
5753                 skip "need MDS >= v2_14_56-51-g61ec1e0f2c for EDQUOT failover"
5754
5755         local dir1="$DIR/$tdir/dir1"
5756         local dir2="$DIR/$tdir/dir2"
5757         local TESTFILE0="$dir1/$tfile-0"
5758         local TESTFILE1="$dir1/$tfile-1"
5759         local TESTFILE2="$dir1/$tfile-2"
5760         local TESTFILE3="$dir2/$tfile-0"
5761         local global_limit=100 # 100M
5762         local limit=10 # 10M
5763         local qpool="qpool1"
5764
5765         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
5766         mds_supports_qp
5767         [ "$ost1_FSTYPE" == zfs ] &&
5768                 skip "ZFS grants some block space together with inode"
5769         setup_quota_test || error "setup quota failed with $?"
5770         set_ost_qtype $QTYPE || error "enable ost quota failed"
5771
5772         # make sure the system is clean
5773         local used=$(getquota -u $TSTUSR global curspace)
5774         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR is not 0."
5775
5776         pool_add $qpool || error "pool_add failed"
5777         pool_add_targets $qpool 0 1 ||
5778                 error "pool_add_targets failed"
5779
5780         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5781                 error "set user quota failed"
5782
5783         $LFS setquota -u $TSTUSR -B ${global_limit}M --pool $qpool $DIR ||
5784                 error "set user quota failed"
5785         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5786                 error "set user quota failed"
5787
5788         mkdir -p $dir1 || error "failed to mkdir"
5789         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5790         mkdir -p $dir2 || error "failed to mkdir"
5791         chown $TSTUSR.$TSTUSR $dir2 || error "chown $dir2 failed"
5792
5793         $LFS setstripe $dir1 -i 1 -c 1|| error "setstripe $testfile failed"
5794         $LFS setstripe $dir2 -i 0 -c 1|| error "setstripe $testfile failed"
5795         lfs getstripe $dir1
5796         lfs getstripe $dir2
5797         sleep 3
5798
5799         $LFS quota -uv $TSTUSR $DIR
5800         #define OBD_FAIL_QUOTA_PREACQ            0xA06
5801         do_facet mds1 $LCTL set_param fail_loc=0xa06
5802         $RUNAS $DD of=$TESTFILE3 count=3 ||
5803                 quota_error u $TSTUSR "write failed"
5804         $RUNAS $DD of=$TESTFILE2 count=7 ||
5805                 quota_error u $TSTUSR "write failed"
5806         $RUNAS $DD of=$TESTFILE1 count=1 oflag=direct ||
5807                 quota_error u $TSTUSR "write failed"
5808         sync
5809         sleep 3
5810         $LFS quota -uv --pool $qpool $TSTUSR $DIR
5811
5812         rm -f $TESTFILE2
5813         stop ost2
5814         do_facet mds1 $LCTL set_param fail_loc=0
5815         start ost2 $(ostdevname 2) $OST_MOUNT_OPTS || error "start ost2 failed"
5816         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5817         # OST0 needs some time to update quota usage after removing TESTFILE2
5818         sleep 4
5819         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5820         $RUNAS $DD of=$TESTFILE0 count=2 oflag=direct ||
5821                 quota_error u $TSTUSR "write failure, but expect success"
5822 }
5823 run_test 80 "check for EDQUOT after OST failover"
5824
5825 test_81()
5826 {
5827         (( $MDS1_VERSION >= $(version_code 2.14.56.52) )) ||
5828                 skip "need MDS >= v2_14_56-52-g862f0baa7c for qmt_pool_free fix"
5829
5830         local global_limit=20  # 100M
5831         local testfile="$DIR/$tdir/$tfile-0"
5832         local qpool="qpool1"
5833
5834         mds_supports_qp
5835         setup_quota_test || error "setup quota failed with $?"
5836
5837         # enable ost quota
5838         set_ost_qtype $QTYPE || error "enable ost quota failed"
5839
5840         # test for user
5841         log "User quota (block hardlimit:$global_limit MB)"
5842         $LFS setquota -u $TSTUSR -B 1G $DIR || error "set user quota failed"
5843
5844         pool_add $qpool || error "pool_add failed"
5845         #define OBD_FAIL_QUOTA_RECALC   0xA07
5846         do_facet mds1 $LCTL set_param fail_loc=0x80000A07 fail_val=30
5847         # added OST casues to start pool recalculation
5848         pool_add_targets $qpool 0 0 1
5849         stop mds1 -f || error "MDS umount failed"
5850
5851         #start mds1 back to destroy created pool
5852         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
5853         clients_up || true
5854 }
5855 run_test 81 "Race qmt_start_pool_recalc with qmt_pool_free"
5856
5857 test_82()
5858 {
5859         (( $MDS1_VERSION >= $(version_code 2.15.50.72) )) ||
5860                 skip "need MDS >= v2_15_50-72-g61481796ac for over 8 QIDs fix"
5861
5862         is_project_quota_supported || skip "skip project quota unsupported"
5863
5864         setup_quota_test || error "setup quota failed with $?"
5865         quota_init
5866
5867         local parent_dir="$DIR/$tdir.parent"
5868         local child_dir="$parent_dir/child"
5869
5870         mkdir -p $child_dir
5871         stack_trap "chown -R 0:0 $parent_dir"
5872
5873         chown $TSTUSR:$TSTUSR $parent_dir ||
5874                 error "failed to chown on $parent_dir"
5875         chown $TSTUSR2:$TSTUSRS2 $child_dir ||
5876                 error "failed to chown on $parent_dir"
5877
5878         $LFS project -p 1000 $parent_dir ||
5879                 error "failed to set project id on $parent_dir"
5880         $LFS project -p 1001 $child_dir ||
5881                 error "failed to set project id on $child_dir"
5882
5883         rmdir $child_dir || error "cannot remove child dir, test failed"
5884 }
5885 run_test 82 "verify more than 8 qids for single operation"
5886
5887 test_grace_with_default_quota()
5888 {
5889         local qtype=$1
5890         local qdtype=$2
5891         local bgrace
5892         local igrace
5893         local bgrace2
5894         local igrace2
5895         echo "ttt1"
5896         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
5897                 error "clear default quota [$qdtype] failed"
5898         echo "ttt2"
5899         $LFS setquota -t $qtype --block-grace 1w --inode-grace 1w $DIR ||
5900                 error "reset quota [$qdtype] grace failed"
5901         echo "ttt3"
5902
5903         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5904                         '{printf("bgrace=%s;igrace=%s;", $4, $9)}')
5905         echo "ttt4"
5906
5907         $LFS setquota $qdtype -B 10G -i 10k $DIR
5908         echo "ttt5"
5909
5910         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5911                         '{printf("bgrace2=%s;igrace2=%s;", $4, $9)}')
5912
5913         [ "$bgrace" == "$bgrace2" ] ||
5914                         error "set default quota shouldn't affect block grace"
5915         [ "$igrace" == "$igrace2" ] ||
5916                         error "set default quota shouldn't affect inode grace"
5917
5918 }
5919
5920 test_83()
5921 {
5922         (( $MDS1_VERSION >= $(version_code 2.15.51.29) )) ||
5923                 skip "need MDS >= v2_15_51-29-gd4978678b4 for grace time fix"
5924
5925         setup_quota_test || error "setup quota failed with $?"
5926         test_grace_with_default_quota "-u" "-U"
5927         test_grace_with_default_quota "-g" "-G"
5928
5929         is_project_quota_supported || return 0
5930         test_grace_with_default_quota "-p" "-P"
5931 }
5932 run_test 83 "Setting default quota shouldn't affect grace time"
5933
5934 test_84()
5935 {
5936         (( $MDS1_VERSION >= $(version_code 2.15.53.115) )) ||
5937                 skip "need MDS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5938         (( $OST1_VERSION >= $(version_code 2.15.53.115) )) ||
5939                 skip "need OSS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5940
5941         local dir1="$DIR/$tdir/dir1"
5942         local TESTFILE1="$dir1/$tfile-1"
5943         local waited=0
5944         local grant=0
5945         local grant2=0
5946         local qp="qpool1"
5947
5948         mds_supports_qp
5949
5950         setup_quota_test || error "setup quota failed with $?"
5951         quota_init
5952         set_ost_qtype $QTYPE || error "enable ost quota failed"
5953
5954         pool_add $qp || error "pool_add failed"
5955         pool_add_targets $qp 0 $(($OSTCOUNT - 1)) ||
5956                 error "pool_add_targets failed"
5957
5958         $LFS setquota -g $TSTUSR -B 10G $DIR ||
5959                 error "failed to set group quota for $TSTUSR"
5960         $LFS setquota -g $TSTUSR -B 5G --pool $qp $DIR ||
5961                 error "set user quota failed"
5962         $LFS quota -gv $TSTUSR $DIR
5963
5964         wait_quota_synced "ost1" "OST0000" "grp" $TSTID "hardlimit" "10485760"
5965
5966         mkdir -p $dir1 || error "failed to mkdir"
5967         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5968
5969         $LFS setstripe -c 1 -i 0 $TESTFILE1
5970         $LFS getstripe $TESTFILE1
5971         chown $TSTUSR.$TSTUSR $TESTFILE1
5972
5973         $RUNAS $DD of=$TESTFILE1 count=60 conv=nocreat oflag=direct ||
5974                 quota_error g $TSTUSR "write failed"
5975
5976         sync_all_data || true
5977         sleep 3
5978         $LFS quota -gv $TSTUSR $DIR
5979         $LFS quota -gv --pool $qp $TSTUSR $DIR
5980
5981         # the grant quota should be larger than 0
5982         waited=0
5983         while (( $waited < 60 )); do
5984                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
5985                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
5986                 (( ${grant} > 0 && ${grant2} > 0 )) && break
5987
5988                 do_facet ost1 $LCTL set_param \
5989                                 osd-*.*-OST0000.quota_slave.force_reint=1
5990                 sleep 1
5991                 waited=$((waited + 1))
5992         done
5993
5994         (( $waited >= 60)) && {
5995                 $LFS quota -gv $TSTUSR $DIR
5996                 $LFS quota -gv --pool $qp $TSTUSR $DIR
5997         }
5998
5999         (( ${grant} > 0 )) || error "pool grant is not increased after dd"
6000         (( ${grant2} > 0 )) || error "grant is not increased after dd"
6001
6002 #define OBD_FAIL_QUOTA_GRANT 0xA08
6003         lustre_fail mds 0xa08
6004         lustre_fail ost 0xa08
6005         sleep 1
6006
6007         # clear quota limits to trigger updating grant quota
6008         $LFS setquota -g $TSTUSR -b 0 -B 0 $DIR ||
6009                 error "failed to clear the group quota for $TSTUSR"
6010         $LFS quota -gv $TSTUSR $DIR
6011         $LFS quota -gv --pool $qp $TSTUSR $DIR
6012
6013         # the grant quota should be set as insane value
6014         waited=0
6015         while (( $waited < 60 )); do
6016                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
6017                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
6018                 (( ${#grant} == 20 && ${#grant2} == 20 )) && break
6019
6020                 sleep 1
6021                 waited=$((waited + 1))
6022         done
6023
6024         (( $waited >= 60)) && {
6025                 $LFS quota -gv $TSTUSR $DIR
6026                 $LFS quota -gv --pool $qp $TSTUSR $DIR
6027         }
6028
6029         (( ${#grant} == 20 )) || error "pool grant is not set as insane value"
6030         (( ${#grant2} == 20 )) || error "grant is not set as insane value"
6031
6032         lustre_fail mds_ost 0
6033         sleep 1
6034
6035         # reset the quota
6036         $LFS setquota -g $TSTUSR -r $DIR ||
6037                 error "failed to reset group quota for $TSTUSR"
6038
6039         sleep 3
6040         $LFS quota -gv $TSTUSR $DIR
6041         $LFS quota -gv --pool $qp $TSTUSR $DIR
6042
6043         # the grant quota should be reset
6044         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
6045         (( ${#grant} == 20 )) && error "grant is not cleared"
6046         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
6047         (( ${#grant} == 20 )) && error "pool grant is not cleared"
6048
6049         $LFS quota -gv $TSTUSR --pool $qp $DIR
6050         $LFS quota -gv --pool $qp $TSTUSR $DIR
6051         local hlimit=$(getquota -g $TSTUSR global bhardlimit $qp)
6052          [ $hlimit -eq 5242880 ] || error "pool limit is changed"
6053
6054         # test whether the quota still works
6055         $LFS setquota -g $TSTUSR -B 100M $DIR ||
6056                 error "failed to set group quota for $TSTUSR"
6057         $LFS quota -gv $TSTUSR $DIR
6058
6059         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct &&
6060                 quota_error g $TSTUSR "dd succeed, expect EDQUOT"
6061
6062         $LFS setquota -g $TSTUSR -B 300M $DIR ||
6063                 error "failed to set group quota for $TSTUSR"
6064         $LFS quota -gv $TSTUSR $DIR
6065
6066         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct ||
6067                 quota_error g $TSTUSR "dd failed, expect succeed"
6068 }
6069 run_test 84 "Reset quota should fix the insane granted quota"
6070
6071 test_85()
6072 {
6073         (( $MDS1_VERSION >= $(version_code 2.15.55.5) )) ||
6074                 skip "need MDS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
6075         (( $OST1_VERSION >= $(version_code 2.15.55.5) )) ||
6076                 skip "need OSS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
6077
6078         local limit=3 # 3M
6079         local qpool="qpool1"
6080         local qpool2="qpool2"
6081         local tfile1="$DIR/$tdir/$tfile-0"
6082
6083         (( OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
6084         mds_supports_qp
6085         setup_quota_test || error "setup quota failed with $?"
6086
6087         # enable ost quota
6088         set_ost_qtype $QTYPE || error "enable ost quota failed"
6089
6090         $LFS setquota -u $TSTUSR -b 0 -B 50T -i 0 -I 0 $DIR ||
6091                 error "set user quota failed"
6092
6093         pool_add $qpool || error "pool_add failed"
6094         pool_add_targets $qpool 0 1 ||
6095                 error "pool_add_targets failed"
6096
6097         pool_add $qpool2 || error "pool_add failed"
6098         pool_add_targets $qpool2 0 1 ||
6099                 error "pool_add_targets failed"
6100
6101         $LFS setstripe -p $qpool $DIR/$tdir || error "cannot set stripe"
6102         $LFS setquota -u $TSTUSR -B 30M --pool $qpool $DIR ||
6103                 error "set user quota failed"
6104         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
6105                 error "set user quota failed"
6106
6107         # don't care about returned value. Just check we don't hung on write.
6108         $RUNAS $DD of=$tfile1 count=10
6109         return 0
6110 }
6111 run_test 85 "do not hung at write with the least_qunit"
6112
6113 test_preacquired_quota()
6114 {
6115         local test_dir=$1
6116         local qtype=$2
6117         local qtype_name=$3
6118         local qid=$4
6119
6120         [[ "$qtype" == "-p" ]] && change_project -sp $qid $DIR/$tdir
6121
6122         $LFS setquota $qtype $qid -i 100K -I 100K $DIR ||
6123                 error "failed to set file [$qtype] quota"
6124
6125         $RUNAS createmany -m $test_dir/tfile- 5000 ||
6126                 error "failed to create files, expect succeed"
6127
6128         wait_zfs_commit $SINGLEMDS
6129         $LFS setquota $qtype $qid -i 2K -I 2K $DIR ||
6130                 error "failed to decrease file [$qtype] quota"
6131
6132         wait_quota_synced "mds1" "MDT0000" $qtype_name $qid "hardlimit" "2048"
6133
6134         # make sure the lqe->lqe_edquot is set
6135         $RUNAS createmany -m $test_dir/tfile2- 10
6136         sleep 5
6137
6138         $RUNAS createmany -m $test_dir/tfile3- 30 &&
6139                 error "succeed to create files, expect failed"
6140
6141         rm -f $test_dir/tfile*
6142         $LFS setquota $qtype $qid -i 0 -I 0 $DIR ||
6143                 error "failed to reset file user quota"
6144 }
6145
6146 test_86()
6147 {
6148         (( $MDS1_VERSION >= $(version_code 2.15.57.41) )) ||
6149                 skip "need MDS >= 2.15.57.41 for quota over limit release fix"
6150
6151         local test_dir="$DIR/$tdir/test_dir"
6152
6153         setup_quota_test || error "setup quota failed with %?"
6154         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
6155
6156         $LFS setdirstripe -c 1 -i 0 $test_dir || error "setdirstripe failed"
6157         chmod 777 $test_dir
6158
6159         test_preacquired_quota "$test_dir" "-u" "usr" "$TSTID"
6160         test_preacquired_quota "$test_dir" "-g" "grp" "$TSTID"
6161
6162         is_project_quota_supported || return 0
6163         test_preacquired_quota "$test_dir" "-p" "prj" "1000"
6164 }
6165 run_test 86 "Pre-acquired quota should be released if quota is over limit"
6166
6167 quota_fini()
6168 {
6169         do_nodes $(comma_list $(nodes_list)) \
6170                 "lctl set_param -n debug=-quota-trace"
6171         if $PQ_CLEANUP; then
6172                 disable_project_quota
6173         fi
6174 }
6175 reset_quota_settings
6176 quota_fini
6177
6178 cd $ORIG_PWD
6179 complete_test $SECONDS
6180 check_and_cleanup_lustre
6181 export QUOTA_AUTO=$QUOTA_AUTO_OLD
6182 exit_status