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