Whamcloud - gitweb
LU-14111 tests: only support recovery-small test 146 for 2.15.54+
[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 test_59() {
4215         [ "$mds1_FSTYPE" != ldiskfs ] &&
4216                 skip "ldiskfs only test"
4217         disable_project_quota
4218         setup_quota_test || error "setup quota failed with $?"
4219         quota_init
4220
4221         local testfile="$DIR/$tdir/$tfile-0"
4222         #make sure it did not crash kernel
4223         touch $testfile && lfs project -sp 1 $testfile
4224
4225         enable_project_quota
4226 }
4227 run_test 59 "lfs project dosen't crash kernel with project disabled"
4228
4229 test_60() {
4230         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
4231                 skip "Needs MDS version 2.11.53 or later."
4232         setup_quota_test || error "setup quota failed with $?"
4233
4234         local testfile=$DIR/$tdir/$tfile
4235         local limit=100
4236
4237         set_mdt_qtype "ug" || error "enable mdt quota failed"
4238
4239         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
4240                 error "set quota failed"
4241         quota_show_check a g $TSTUSR
4242
4243         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
4244         chmod g+s $DIR/$tdir || error "chmod g+s failed"
4245         $RUNAS createmany -m ${testfile} $((limit-1)) ||
4246                 error "create many files failed"
4247
4248         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
4249
4250         # root user can overrun quota
4251         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
4252                 error "root user should succeed"
4253 }
4254 run_test 60 "Test quota for root with setgid"
4255
4256 # test default quota
4257 test_default_quota() {
4258         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
4259                 skip "Not supported before 2.11.51."
4260
4261         local qtype=$1
4262         local qres_type=$2
4263         local qid=$TSTUSR
4264         local qprjid=$TSTPRJID
4265         local qdtype="-U"
4266         local qs="-b"
4267         local qh="-B"
4268         local LIMIT=20480 #20M disk space
4269         local TESTFILE="$DIR/$tdir/$tfile-0"
4270         local $qpool_cmd
4271
4272         [ $qtype == "-p" ] && ! is_project_quota_supported &&
4273                 echo "Project quota is not supported" && return 0
4274
4275         [ $qtype == "-u" ] && qdtype="-U"
4276         [ $qtype == "-g" ] && qdtype="-G"
4277         [ $qtype == "-p" ] && {
4278                 qdtype="-P"
4279                 qid=$qprjid
4280         }
4281
4282         [ $qres_type == "meta" ] && {
4283                 LIMIT=10240 #10K inodes
4284                 qs="-i"
4285                 qh="-I"
4286         }
4287         [ ! -z "$3" ] && {
4288                 qpool_cmd="--pool $3"
4289                 # pool quotas don't work properly without global limit
4290                 $LFS setquota $qtype $qid -B1T -b1T $DIR ||
4291                         error "set global limit failed"
4292         }
4293
4294         setup_quota_test || error "setup quota failed with $?"
4295
4296         quota_init
4297
4298         # enable mdt/ost quota
4299         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4300         set_ost_qtype $QTYPE || error "enable ost quota failed"
4301
4302         log "set to use default quota"
4303         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4304                 error "set $qid to use default quota failed"
4305
4306         log "set default quota"
4307         $LFS setquota $qdtype $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4308                 error "set $qid default quota failed"
4309
4310         log "get default quota"
4311         $LFS quota $qdtype $DIR || error "get default quota failed"
4312
4313         if [ $qres_type == "data" ]; then
4314                 local SLIMIT=$($LFS quota $qpool_cmd $qdtype $DIR | \
4315                                 grep "$MOUNT" | awk '{print $2}')
4316                 [ $SLIMIT -eq $LIMIT ] ||
4317                         error "the returned default quota is wrong"
4318         else
4319                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
4320                                                         awk '{print $5}')
4321                 [ $SLIMIT -eq $LIMIT ] ||
4322                         error "the returned default quota is wrong"
4323         fi
4324
4325         # make sure the system is clean
4326         local USED=$(getquota $qtype $qid global curspace)
4327         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4328
4329         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4330                         error "setstripe $TESTFILE failed"
4331         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4332
4333         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
4334
4335         log "Test not out of quota"
4336         if [ $qres_type == "data" ]; then
4337                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
4338                         quota_error $qtype $qid "write failed, expect succeed"
4339         else
4340                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
4341                         quota_error $qtype $qid "create failed, expect succeed"
4342
4343                 unlinkmany $TESTFILE $((LIMIT/2))
4344         fi
4345
4346         log "Test out of quota"
4347         # flush cache, ensure noquota flag is set on client
4348         cancel_lru_locks osc
4349         cancel_lru_locks mdc
4350         sync; sync_all_data || true
4351         if [ $qres_type == "data" ]; then
4352                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4353                         quota_error $qtype $qid "write succeed, expect EDQUOT"
4354         else
4355                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4356                         quota_error $qtype $qid "create succeed, expect EDQUOT"
4357
4358                 unlinkmany $TESTFILE $((LIMIT*2))
4359         fi
4360
4361         rm -f $TESTFILE
4362         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
4363                         error "setstripe $TESTFILE failed"
4364         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
4365
4366         log "Increase default quota"
4367
4368         # LU-4505: sleep 5 seconds to enable quota acquire
4369         sleep 5
4370
4371         # increase default quota
4372         $LFS setquota $qdtype $qpool_cmd $qs $((LIMIT*3)) \
4373                 $qh $((LIMIT*3)) $DIR || error "set default quota failed"
4374
4375         cancel_lru_locks osc
4376         cancel_lru_locks mdc
4377         sync; sync_all_data || true
4378         if [ $qres_type == "data" ]; then
4379                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4380                         quota_error $qtype $qid "write failed, expect succeed"
4381         else
4382                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4383                         quota_error $qtype $qid "create failed, expect succeed"
4384
4385                 unlinkmany $TESTFILE $((LIMIT*2))
4386         fi
4387
4388         log "Set quota to override default quota"
4389         $LFS setquota $qtype $qid $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4390                 error "set $qid quota failed"
4391
4392         cancel_lru_locks osc
4393         cancel_lru_locks mdc
4394         sync; sync_all_data || true
4395         if [ $qres_type == "data" ]; then
4396                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4397                         quota_error $qtype $qid "write succeed, expect EQUOT"
4398         else
4399                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4400                         quota_error $qtype $qid "create succeed, expect EQUOT"
4401
4402                 unlinkmany $TESTFILE $((LIMIT*2))
4403         fi
4404
4405         log "Set to use default quota again"
4406
4407         # LU-4505: sleep 5 seconds to enable quota acquire
4408         sleep 5
4409
4410         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4411                 error "set $qid to use default quota failed"
4412
4413         cancel_lru_locks osc
4414         cancel_lru_locks mdc
4415         sync; sync_all_data || true
4416         if [ $qres_type == "data" ]; then
4417                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4418                         quota_error $qtype $qid "write failed, expect succeed"
4419         else
4420                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4421                         quota_error $qtype $qid "create failed, expect succeed"
4422
4423                 unlinkmany $TESTFILE $((LIMIT*2))
4424         fi
4425
4426         log "Cleanup"
4427         rm -f $TESTFILE
4428         wait_delete_completed || error "wait_delete_completed failed"
4429         sync_all_data || true
4430
4431         $LFS setquota $qdtype $qpool_cmd $qs 0 $qh 0 $DIR ||
4432                 error "reset default quota failed"
4433         $LFS setquota $qtype $qid $qpool_cmd $qs 0 $qh 0 $DIR ||
4434                 error "reset quota failed"
4435         cleanup_quota_test
4436 }
4437
4438 test_61() {
4439         test_default_quota "-u" "data"
4440         test_default_quota "-u" "meta"
4441         test_default_quota "-g" "data"
4442         test_default_quota "-g" "meta"
4443         test_default_quota "-p" "data"
4444         test_default_quota "-p" "meta"
4445 }
4446 run_test 61 "default quota tests"
4447
4448 test_62() {
4449         ! is_project_quota_supported &&
4450                 skip "Project quota is not supported"
4451         [[ "$(chattr -h 2>&1)" =~ "project" ||
4452            "$(chattr -h 2>&1)" =~ "pRVf" ]] ||
4453                 skip "chattr did not support project quota"
4454         setup_quota_test || error "setup quota failed with $?"
4455         local testdir=$DIR/$tdir/
4456
4457         $RUNAS mkdir -p $testdir || error "failed to mkdir"
4458         change_project -s $testdir
4459         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4460                 error "inherit attribute should be set"
4461         # chattr used FS_IOC_SETFLAGS ioctl
4462         $RUNAS chattr -P $testdir &&
4463                 error "regular user clear inherit should fail"
4464         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4465                 error "inherit attribute should still be set"
4466         chattr -P $testdir || error "root failed to clear inherit"
4467         [[ $($LFS project -d $testdir) =~ "P" ]] &&
4468                 error "inherit attribute should be cleared"
4469         return 0
4470 }
4471 run_test 62 "Project inherit should be only changed by root"
4472
4473 test_dom() {
4474         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
4475                 skip "Not supported before 2.11.55"
4476
4477         local qtype=$1
4478         local qid=$TSTUSR
4479         local dd_failed=false
4480         local tdir_dom=${tdir}_dom
4481         local LIMIT=20480 #20M
4482
4483         [ $qtype == "p" ] && ! is_project_quota_supported &&
4484                 echo "Project quota is not supported" && return 0
4485
4486         [ $qtype == "p" ] && qid=$TSTPRJID
4487
4488         setup_quota_test || error "setup quota failed with $?"
4489
4490         quota_init
4491
4492         # enable mdt/ost quota
4493         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4494         set_ost_qtype $QTYPE || error "enable ost quota failed"
4495
4496         # make sure the system is clean
4497         local USED=$(getquota -$qtype $qid global curspace)
4498         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4499
4500         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
4501
4502         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
4503         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
4504                 error "setstripe $tdir_dom failed"
4505         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
4506
4507         [ $qtype == "p" ] && {
4508                 change_project -sp $TSTPRJID $DIR/$tdir
4509                 change_project -sp $TSTPRJID $DIR/$tdir_dom
4510         }
4511
4512         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
4513                 error "set $qid quota failed"
4514
4515         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4516                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4517                                                                 dd_failed=true
4518         done
4519
4520         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4521
4522         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
4523                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4524                                                                 dd_failed=true
4525         done
4526
4527         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4528
4529         rm -f $DIR/$tdir_dom/*
4530
4531         # flush cache, ensure noquota flag is set on client
4532         cancel_lru_locks osc
4533         cancel_lru_locks mdc
4534         sync; sync_all_data || true
4535
4536         dd_failed=false
4537
4538         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4539                 quota_error $qtype $qid "write failed, expect succeed"
4540
4541         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4542                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4543                                                                 dd_failed=true
4544         done
4545
4546         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4547
4548         rm -f $DIR/$tdir/*
4549         rm -f $DIR/$tdir_dom/*
4550
4551         # flush cache, ensure noquota flag is set on client
4552         cancel_lru_locks osc
4553         cancel_lru_locks mdc
4554         sync; sync_all_data || true
4555
4556         dd_failed=false
4557
4558         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4559                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4560                                                                 dd_failed=true
4561         done
4562
4563         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4564
4565         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4566                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4567
4568         rm -fr $DIR/$tdir
4569         rm -fr $DIR/$tdir_dom
4570
4571         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4572                 error "reset usr quota failed"
4573 }
4574
4575 test_63() {
4576         test_dom "u"
4577         test_dom "g"
4578         test_dom "p"
4579 }
4580 run_test 63 "quota on DoM tests"
4581
4582 test_64() {
4583         ! is_project_quota_supported &&
4584                 skip "Project quota is not supported"
4585         setup_quota_test || error "setup quota failed with $?"
4586         local dir1="$DIR/$tdir/"
4587
4588         touch $dir1/file
4589         ln -s $dir1/file $dir1/file_link
4590         mkfifo $dir1/fifo
4591
4592         $LFS project -srp $TSTPRJID $dir1 >&/dev/null ||
4593                 error "set project should succeed"
4594
4595         used=$(getquota -p $TSTPRJID global curinodes)
4596         [ $used -eq 4 ] || error "expected 4 got $used"
4597         $LFS project -rC $dir1 >&/dev/null ||
4598                 error "clear project should succeed"
4599
4600         used=$(getquota -p $TSTPRJID global curinodes)
4601         [ $used -eq 0 ] || error "expected 0 got $used"
4602 }
4603 run_test 64 "lfs project on non dir/files should succeed"
4604
4605 test_65() {
4606         local SIZE=10 # MB
4607         local TESTFILE="$DIR/$tdir/$tfile-0"
4608
4609         setup_quota_test || error "setup quota failed with $?"
4610         set_ost_qtype $QTYPE || error "enable ost quota failed"
4611         quota_init
4612
4613         echo "Write..."
4614         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4615                 error "failed to write"
4616         # flush cache, ensure noquota flag is set on client
4617         cancel_lru_locks osc
4618         sync; sync_all_data || true
4619
4620         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4621         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4622         local quota_all=$($RUNAS $LFS quota $DIR)
4623
4624         [ "$(echo "$quota_all" | head -n3)" == "$quota_u" ] ||
4625                 error "usr quota not match"
4626         [ "$(echo "$quota_all" | tail -n3)" == "$quota_g" ] ||
4627                 error "grp quota not match"
4628 }
4629 run_test 65 "Check lfs quota result"
4630
4631 test_66() {
4632         ! is_project_quota_supported &&
4633                 skip "Project quota is not supported"
4634         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4635                 skip "Not supported before 2.12.4"
4636         setup_quota_test || error "setup quota failed with $?"
4637         local old=$(do_facet mds1 $LCTL get_param -n \
4638                     mdt.*.enable_chprojid_gid | head -1)
4639         local testdir=$DIR/$tdir/foo
4640
4641         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4642         stack_trap "do_facet mds1 $LCTL \
4643                 set_param mdt.*.enable_chprojid_gid=$old" EXIT
4644
4645         mkdir_on_mdt0 $testdir || error "failed to mkdir"
4646         chown -R $TSTID:$TSTID $testdir
4647         change_project -sp $TSTPRJID $testdir
4648         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4649
4650         $RUNAS lfs project -p 0 $testdir/foo &&
4651                 error "nonroot user should fail to set projid"
4652
4653         $RUNAS lfs project -C $testdir/foo &&
4654                 error "nonroot user should fail to clear projid"
4655
4656         change_project -C $testdir/foo || error "failed to clear project"
4657
4658         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4659         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4660         "failed to set projid with normal user when enable_chprojid_gid=-1"
4661
4662         $RUNAS lfs project -rC $testdir/ || error \
4663 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4664
4665         touch $testdir/bar || error "failed touch $testdir/bar"
4666         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4667         "normal user should not be able to set projid on root owned file"
4668
4669         change_project -p $TSTPRJID $testdir/bar || error \
4670                 "root should be able to change its own file's projid"
4671 }
4672 run_test 66 "nonroot user can not change project state in default"
4673
4674 test_67_write() {
4675         local file="$1"
4676         local qtype="$2"
4677         local size=$3
4678         local _runas=""
4679         local short_qtype=${qtype:0:1}
4680
4681         echo "file "$file
4682         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4683         case "$4" in
4684                 $TSTUSR)  _runas=$RUNAS;;
4685                 $TSTUSR2) _runas=$RUNAS2;;
4686                 *)          error "unknown quota parameter $4";;
4687         esac
4688
4689         log "Write..."
4690         date
4691         $_runas $DD of=$file count=$size ||
4692                 quota_error $short_qtype $TSTUSR \
4693                         "$qtype write failure, but expect success"
4694         date
4695         cancel_lru_locks osc
4696         date
4697         sync; sync_all_data || true
4698         date
4699 }
4700
4701 getgranted() {
4702         local pool=$1
4703         local ptype=$2
4704         local userid=$3
4705         local qtype=$4
4706         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4707
4708         do_facet mds1 $LCTL get_param $param |
4709                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4710 }
4711
4712 test_67() {
4713         local limit=20 # MB
4714         local testfile="$DIR/$tdir/$tfile-0"
4715         local testfile2="$DIR/$tdir/$tfile-1"
4716         local testfile3="$DIR/$tdir/$tfile-2"
4717         local qpool="qpool1"
4718         local used
4719         local granted
4720         local granted_mb
4721
4722         mds_supports_qp
4723         [ "$ost1_FSTYPE" == zfs ] &&
4724                 skip "ZFS grants some block space together with inode"
4725
4726         setup_quota_test || error "setup quota failed with $?"
4727
4728         # enable ost quota
4729         set_ost_qtype $QTYPE || error "enable ost quota failed"
4730
4731         # test for user
4732         log "User quota (block hardlimit:$limit MB)"
4733         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4734                 error "set user quota failed"
4735
4736         # make sure the system is clean
4737         used=$(getquota -u $TSTUSR global curspace)
4738         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4739
4740         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4741         echo "granted 0x0 before write $granted"
4742
4743         # trigger reintegration
4744         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4745         procf=${procf}quota_slave.force_reint
4746         do_facet ost1 $LCTL set_param $procf=1 ||
4747                 error "force reintegration failed"
4748         wait_ost_reint "u" || error "reintegration failed"
4749         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4750         [ $granted -ne 0 ] &&
4751                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4752
4753         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4754         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4755
4756         # write 10 MB to testfile
4757         test_67_write "$testfile" "user" 10 "$TSTUSR"
4758
4759         # create qpool and add OST1
4760         pool_add $qpool || error "pool_add failed"
4761         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4762         # as $TSTUSR hasn't limits, lqe may absent. But it should be
4763         # created after the 1st direct qmt_get.
4764         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4765
4766         # check granted - should be 0, as testfile is located only on OST0
4767         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4768         echo "global granted $granted"
4769         granted=$(getgranted $qpool "dt" $TSTID "usr")
4770         echo "$qpool granted $granted"
4771         [ $granted -ne 0 ] &&
4772                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4773
4774         # add OST0 to qpool and check granted space
4775         pool_add_targets $qpool 0 1 ||
4776                 error "pool_add_targets failed"
4777         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4778         echo "Granted $granted_mb MB"
4779         #should be 10M + qunit for each OST
4780         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4781                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4782
4783         $LFS setstripe $testfile2 -c 1 -i 1 ||
4784                 error "setstripe $testfile2 failed"
4785         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4786         # Write from another user and check that qpool1
4787         # shows correct granted, despite $TSTUSR2 hasn't limits in qpool1.
4788         test_67_write "$testfile2" "user" 10 "$TSTUSR2"
4789         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4790         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4791         [ $granted -ne 0 ] &&
4792                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4793
4794         # Granted space for $TSTUSR2 in qpool1 should appear only
4795         # when global lqe for this user becomes enforced.
4796         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4797                 error "set user quota failed"
4798         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4799         echo "granted_mb $granted_mb"
4800         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4801                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4802
4803         $LFS setstripe $testfile3 -c 1 -i 0 ||
4804                 error "setstripe $testfile3 failed"
4805         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4806         test_67_write "$testfile3" "user" 10 "$TSTUSR2"
4807         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4808         echo "$testfile3 granted_mb $granted_mb"
4809         [ $granted_mb -eq $limit ] ||
4810                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4811
4812         # remove OST1 from the qpool1 and check granted space
4813         # should be 0 for TSTUSR and 10M for TSTUSR2
4814         pool_remove_target $qpool 0
4815         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4816         [ $granted_mb -eq 0 ] ||
4817                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4818         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4819         [ $granted_mb -eq 10 ] ||
4820                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4821
4822         rm -f $testfile
4823         wait_delete_completed || error "wait_delete_completed failed"
4824         sync_all_data || true
4825         used=$(getquota -u $TSTUSR global curspace)
4826         [ $used -eq 0 ] || quota_error u $TSTUSR \
4827                 "user quota isn't released after deletion"
4828 }
4829 run_test 67 "quota pools recalculation"
4830
4831 get_slave_nr() {
4832         local pool=$1
4833         local qtype=$2
4834         local nr
4835
4836         wait_update_facet "--quiet" mds1 \
4837                 "$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info \
4838                         >/dev/null 2>&1 || echo foo" "">/dev/null ||
4839         error "mds1: failed to create quota pool $pool"
4840
4841         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4842                 awk '/usr/ {getline; print $2}'
4843 }
4844
4845 test_68()
4846 {
4847         local qpool="qpool1"
4848
4849         mds_supports_qp
4850         setup_quota_test || error "setup quota failed with $?"
4851
4852         # enable ost quota
4853         set_ost_qtype $QTYPE || error "enable ost quota failed"
4854
4855         # check slave number for glbal pool
4856         local nr=$(get_slave_nr "0x0" "usr")
4857         echo "nr result $nr"
4858         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4859                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4860
4861         # create qpool and add OST1
4862         pool_add $qpool || error "pool_add failed"
4863         nr=$(get_slave_nr $qpool "usr")
4864         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4865
4866         # add OST1 to qpool
4867         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4868         nr=$(get_slave_nr $qpool "usr")
4869         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4870
4871         # add OST0 to qpool
4872         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4873         nr=$(get_slave_nr $qpool "usr")
4874         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
4875
4876         # remove OST0
4877         pool_remove_target $qpool 0
4878         nr=$(get_slave_nr $qpool "usr")
4879         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4880
4881         # remove OST1
4882         pool_remove_target $qpool 1
4883         nr=$(get_slave_nr $qpool "usr")
4884         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4885
4886         # Check again that all is fine with global pool
4887         nr=$(get_slave_nr "0x0" "usr")
4888         [[ $nr == $((OSTCOUNT + MDSCOUNT)) ]] ||
4889                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4890 }
4891 run_test 68 "slave number in quota pool changed after each add/remove OST"
4892
4893 test_69()
4894 {
4895         local global_limit=200 # MB
4896         local limit=10 # MB
4897         local testfile="$DIR/$tdir/$tfile-0"
4898         local dom0="$DIR/$tdir/dom0"
4899         local qpool="qpool1"
4900
4901         mds_supports_qp
4902         setup_quota_test || error "setup quota failed with $?"
4903
4904         # enable ost quota
4905         set_ost_qtype $QTYPE || error "enable ost quota failed"
4906         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4907
4908         # Save DOM only at MDT0
4909         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
4910         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
4911         chmod 0777 $dom0
4912         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
4913
4914         # create qpool and add OST0
4915         pool_add $qpool || error "pool_add failed"
4916         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4917
4918         log "User quota (block hardlimit:$global_limit MB)"
4919         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4920                 error "set user quota failed"
4921
4922         log "User quota (block hardlimit:$limit MB)"
4923         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4924                 error "set user quota failed"
4925
4926         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
4927                 quota_error u $TSTUSR "write failed"
4928
4929         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4930                 oflag=sync || quota_error u $TSTUSR "write failed"
4931
4932         $RUNAS $DD of=$testfile count=$limit || true
4933
4934         # flush cache, ensure noquota flag is set on client
4935         cancel_lru_locks osc
4936         sync; sync_all_data || true
4937
4938         # MDT0 shouldn't get EDQUOT with glimpse.
4939         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
4940                 quota_error u $TSTUSR \
4941                         "user write success, but expect EDQUOT"
4942
4943         # Now all members of qpool1 should get EDQUOT. Expect success
4944         # when write to DOM on MDT0, as it belongs to global pool.
4945         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
4946                 oflag=sync || quota_error u $TSTUSR "write failed"
4947
4948         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4949                 oflag=sync || quota_error u $TSTUSR "write failed"
4950 }
4951 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
4952
4953 test_70a()
4954 {
4955         local qpool="qpool1"
4956         local limit=20 # MB
4957         local err=0
4958         local bhard
4959
4960         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
4961                 skip "Needs a client >= $VERSION_WITH_QP"
4962
4963         setup_quota_test || error "setup quota failed with $?"
4964
4965         # MDS returns EFAULT for unsupported quotactl command
4966         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
4967
4968         # create qpool and add OST0
4969         pool_add $qpool || error "pool_add failed"
4970         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4971
4972         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
4973         rc=$?
4974         [ $rc -eq $err ] || error "setquota res $rc != $err"
4975
4976         # If MDS supports QP, check that limit was set properly.
4977         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
4978                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
4979                 echo "hard limit $bhard limit $limit"
4980                 [ $bhard -ne $((limit*1024)) ] &&
4981                         error "bhard:$bhard for $qpool!=$((limit*1024))"
4982         fi
4983
4984         $LFS quota -u $TSTUSR --pool $qpool $DIR
4985         rc=$?
4986         [ $rc -eq $err ] || error "quota res $rc != $err"
4987 }
4988 run_test 70a "check lfs setquota/quota with a pool option"
4989
4990 test_70b()
4991 {
4992         local glbl_hard=200 # 200M
4993         local glbl_soft=100 # 100M
4994         local pool_hard=10 # 10M
4995         local qpool="qpool1"
4996
4997         pool_add $qpool || error "pool_add failed"
4998         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4999
5000         $LFS setquota -u $TSTUSR -b ${glbl_soft}M -B ${glbl_hard}M $DIR ||
5001                 error "set user quota failed"
5002         $LFS setquota -u $TSTUSR -B ${pool_hard}M --pool $qpool $DIR ||
5003                 error "set user quota failed"
5004
5005         local tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5006         [ $tmp -eq $((pool_hard * 1024)) ] ||
5007                 error "wrong block hard limit $tmp for $qpool"
5008         local tmp=$(getquota -u $TSTUSR global bsoftlimit $qpool)
5009         # soft limit hasn't been set and should be zero
5010         [ $tmp -eq 0 ] || error "wrong soft block limit $tmp for $qpool"
5011 }
5012 run_test 70b "lfs setquota pool works properly"
5013
5014 test_71a()
5015 {
5016         local limit=10 # MB
5017         local global_limit=100 # MB
5018         local testfile="$DIR/$tdir/$tfile-0"
5019         local qpool="qpool1"
5020         local qpool2="qpool2"
5021
5022         [ "$ost1_FSTYPE" == zfs ] &&
5023                 skip "ZFS grants some block space together with inode"
5024         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
5025         mds_supports_qp
5026         setup_quota_test || error "setup quota failed with $?"
5027
5028         # enable ost quota
5029         set_ost_qtype $QTYPE || error "enable ost quota failed"
5030
5031         # test for user
5032         log "User quota (block hardlimit:$global_limit MB)"
5033         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5034                 error "set user quota failed"
5035
5036         pool_add $qpool || error "pool_add failed"
5037         pool_add_targets $qpool 0 1 ||
5038                 error "pool_add_targets failed"
5039
5040         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5041                 error "set user quota failed"
5042
5043         pool_add $qpool2 || error "pool_add failed"
5044         pool_add_targets $qpool2 1 1 ||
5045                 error "pool_add_targets failed"
5046
5047         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
5048                 error "set user quota failed"
5049
5050         # make sure the system is clean
5051         local used=$(getquota -u $TSTUSR global curspace)
5052
5053         echo "used $used"
5054         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5055
5056         # create 1st component 1-10M
5057         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5058         #create 2nd component 10-30M
5059         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5060         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5061
5062         # сheck normal use and out of quota with PFL
5063         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
5064         test_1_check_write $testfile "user" $((limit*2))
5065         rm -f $testfile
5066         wait_delete_completed || error "wait_delete_completed failed"
5067         sync_all_data || true
5068         used=$(getquota -u $TSTUSR global curspace)
5069         [ $used -ne 0 ] && quota_error u $TSTUSR \
5070                 "user quota isn't released after deletion"
5071
5072         # create 1st component 1-10M
5073         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
5074         # create 2nd component 10-30M
5075         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
5076         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5077
5078         # write to the 2nd component
5079         $RUNAS $DD of=$testfile count=$limit seek=10 ||
5080                 quota_error u $TSTUSR \
5081                         "write failure, but expect success"
5082         # this time maybe cache write,  ignore it's failure
5083         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
5084         cancel_lru_locks osc
5085         sync; sync_all_data || true
5086         # write over limit in qpool2(2nd component 10-30M)
5087         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
5088                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5089         # write to the 1st component - OST0 is empty
5090         $RUNAS $DD of=$testfile count=$limit seek=0 ||
5091                 quota_error u $TSTUSR "write failed"
5092 }
5093 run_test 71a "Check PFL with quota pools"
5094
5095 test_71b()
5096 {
5097         local global_limit=1000 # MB
5098         local limit1=160 # MB
5099         local limit2=10 # MB
5100         local testfile="$DIR/$tdir/$tfile-0"
5101         local qpool="qpool1"
5102         local qpool2="qpool2"
5103
5104         [ "$ost1_FSTYPE" == zfs ] &&
5105                 skip "ZFS grants some block space together with inode"
5106         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
5107         mds_supports_qp
5108         setup_quota_test || error "setup quota failed with $?"
5109
5110         # enable ost quota
5111         set_ost_qtype $QTYPE || error "enable ost quota failed"
5112
5113         # test for user
5114         log "User quota (block hardlimit:$global_limit MB)"
5115         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5116                 error "set user quota failed"
5117
5118         pool_add $qpool || error "pool_add failed"
5119         pool_add_targets $qpool 0 1 ||
5120                 error "pool_add_targets failed"
5121
5122         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
5123                 error "set user quota failed"
5124
5125         pool_add $qpool2 || error "pool_add failed"
5126         pool_add_targets $qpool2 1 1 ||
5127                 error "pool_add_targets failed"
5128
5129         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5130                 error "set user quota failed"
5131
5132         # make sure the system is clean
5133         local used=$(getquota -u $TSTUSR global curspace)
5134
5135         echo "used $used"
5136         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5137
5138         # First component is on OST0, 2nd on OST1
5139         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
5140         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5141
5142         # fill the 1st component on OST0
5143         $RUNAS $DD of=$testfile count=128 ||
5144                 quota_error u $TSTUSR "write failed"
5145         # write to the 2nd cmpnt on OST1
5146         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
5147                 quota_error u $TSTUSR "write failed"
5148         # this time maybe cache write,  ignore it's failure
5149         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
5150                 true
5151         cancel_lru_locks osc
5152         sync; sync_all_data || true
5153         # write over limit in qpool2
5154         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
5155                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
5156         return 0
5157 }
5158 run_test 71b "Check SEL with quota pools"
5159
5160 test_72()
5161 {
5162         local limit=10 # MB
5163         local global_limit=50 # MB
5164         local testfile="$DIR/$tdir/$tfile-0"
5165         local qpool="qpool1"
5166
5167         mds_supports_qp
5168         setup_quota_test || error "setup quota failed with $?"
5169
5170         # enable ost quota
5171         set_ost_qtype $QTYPE || error "enable ost quota failed"
5172
5173         # test for user
5174         log "User quota (block hardlimit:$global_limit MB)"
5175         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5176                 error "set user quota failed"
5177
5178         pool_add $qpool || error "pool_add failed"
5179         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
5180
5181         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5182                 error "set user quota failed"
5183
5184         # make sure the system is clean
5185         local used=$(getquota -u $TSTUSR global curspace)
5186         echo "used $used"
5187         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
5188
5189         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5190
5191         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
5192         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
5193         test_1_check_write $testfile "user" $limit
5194         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
5195         echo "used $used"
5196         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
5197         # check that lfs quota -uv --pool prints only OST that
5198         # was added in a pool
5199         lfs quota -v -u $TSTUSR --pool $qpool $DIR | grep -v "OST0001" |
5200                 grep "OST\|MDT" && error "$qpool consists wrong targets"
5201         return 0
5202 }
5203 run_test 72 "lfs quota --pool prints only pool's OSTs"
5204
5205 test_73a()
5206 {
5207         (( $MDS1_VERSION >= $(version_code 2.14.51.158) )) ||
5208                 skip "need MDS >= v2_14_51-158-g25a70a88 for default pool quota"
5209
5210         local qpool="qpool1"
5211
5212         mds_supports_qp
5213
5214         pool_add $qpool || error "pool_add failed"
5215         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5216                 error "pool_add_targets failed"
5217
5218         test_default_quota "-u" "data" "qpool1"
5219 }
5220 run_test 73a "default limits at OST Pool Quotas"
5221
5222 test_73b()
5223 {
5224         (( $MDS1_VERSION >= $(version_code 2.14.52.91) )) ||
5225                 skip "need MDS >= v2_14_52-91-g188112fc8 for nested lqe fix"
5226
5227         local TESTFILE1="$DIR/$tdir/$tfile-1"
5228         local limit=20 #20M
5229         local qpool="qpool1"
5230
5231         mds_supports_qp
5232
5233         setup_quota_test || error "setup quota failed with $?"
5234         quota_init
5235         set_ost_qtype $QTYPE || error "enable ost quota failed"
5236
5237         # pool quotas don't work properly without global limit
5238         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
5239                 error "set global limit failed"
5240
5241         pool_add $qpool || error "pool_add failed"
5242         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
5243                 error "pool_add_targets failed"
5244
5245         log "set default quota for $qpool"
5246         $LFS setquota -U --pool $qpool -b ${limit}M -B ${limit}M $DIR ||
5247                 error "set default quota failed"
5248
5249         log "Write from user that hasn't lqe"
5250         # Check that it doesn't cause a panic or a deadlock
5251         # due to nested lqe lookups that rewrite 1st lqe in qti_lqes array.
5252         # Have to use RUNAS_ID as resetquota creates lqes in
5253         # the beginning for TSTUSR/TSTUSR2 when sets limits to 0.
5254         runas -u $RUNAS_ID -g $RUNAS_GID $DD of=$TESTFILE1 count=10
5255
5256         cancel_lru_locks osc
5257         sync; sync_all_data || true
5258 }
5259 run_test 73b "default OST Pool Quotas limit for new user"
5260
5261 test_74()
5262 {
5263         (( $MDS1_VERSION >= $(version_code 2.14.52.6) )) ||
5264                 skip "need MDS >= v2_14_52-6-g8c19365416 for pool per-user fix"
5265
5266         local global_limit=200 # 200M
5267         local limit=10 # 10M
5268         local limit2=50 # 50M
5269         local qpool="qpool1"
5270         local qpool2="qpool2"
5271         local tmp=0
5272
5273         mds_supports_qp
5274         setup_quota_test || error "setup quota failed with $?"
5275
5276         # enable ost quota
5277         set_ost_qtype $QTYPE || error "enable ost quota failed"
5278
5279         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5280                 error "set user quota failed"
5281
5282         pool_add $qpool || error "pool_add failed"
5283         pool_add_targets $qpool 0 1 ||
5284                 error "pool_add_targets failed"
5285
5286         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5287                 error "set user quota failed"
5288
5289         pool_add $qpool2 || error "pool_add failed"
5290         pool_add_targets $qpool2 1 1 ||
5291                 error "pool_add_targets failed"
5292
5293         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
5294                 error "set user quota failed"
5295
5296         tmp=$(getquota -u $TSTUSR global bhardlimit)
5297         [ $tmp -eq $((global_limit * 1024)) ] ||
5298                 error "wrong global limit $global_limit"
5299
5300         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
5301         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit $tmp for $qpool"
5302
5303         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool2)
5304         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit $tmp for $qpool2"
5305
5306         # check limits in pools
5307         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5308               grep -A4 $qpool | awk 'NR == 4{print $4}')
5309         echo "pool limit for $qpool $tmp"
5310         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit:tmp for $qpool"
5311         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
5312               grep -A4 $qpool2 | awk 'NR == 4{print $4}')
5313         echo "pool limit for $qpool2 $tmp"
5314         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit:$tmp for $qpool2"
5315 }
5316 run_test 74 "check quota pools per user"
5317
5318 function cleanup_quota_test_75()
5319 {
5320         do_facet mgs $LCTL nodemap_modify --name default \
5321                 --property admin --value 1
5322         do_facet mgs $LCTL nodemap_modify --name default \
5323                 --property trusted --value 1
5324         do_facet mgs $LCTL nodemap_modify --name default \
5325                 --property squash_uid --value 99
5326         do_facet mgs $LCTL nodemap_modify --name default \
5327                 --property squash_gid --value 99
5328
5329         wait_nm_sync default admin_nodemap
5330         wait_nm_sync default trusted_nodemap
5331
5332         do_facet mgs $LCTL nodemap_activate 0
5333         wait_nm_sync active
5334
5335         resetquota -u $TSTUSR
5336 }
5337
5338 test_dom_75() {
5339         local dd_failed=false
5340         local LIMIT=20480 #20M
5341         local qid=$TSTID
5342
5343         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5344                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5345                         conv=fsync || dd_failed=true
5346         done
5347
5348         $dd_failed && quota_error u $qid "write failed, expect succeed (1)"
5349
5350         for ((i = $((LIMIT/2048-1)); i < $((LIMIT/1024 + 10)); i++)); do
5351                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5352                         conv=fsync || dd_failed=true
5353         done
5354
5355         $dd_failed || quota_error u $qid "write succeed, expect EDQUOT (1)"
5356
5357         rm -f $DIR/$tdir_dom/*
5358
5359         # flush cache, ensure noquota flag is set on client
5360         cancel_lru_locks
5361         sync; sync_all_data || true
5362
5363         dd_failed=false
5364
5365         $DD of=$DIR/$tdir/file count=$((LIMIT/2048-1)) conv=fsync ||
5366                 quota_error u $qid "write failed, expect succeed (2)"
5367
5368         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
5369                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5370                         conv=fsync || dd_failed=true
5371         done
5372
5373         $dd_failed || quota_error u $TSTID "write succeed, expect EDQUOT (2)"
5374
5375         rm -f $DIR/$tdir/*
5376         rm -f $DIR/$tdir_dom/*
5377
5378         # flush cache, ensure noquota flag is set on client
5379         cancel_lru_locks
5380         sync; sync_all_data || true
5381
5382         dd_failed=false
5383
5384         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
5385                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
5386                         conv=fsync || dd_failed=true
5387         done
5388
5389         $dd_failed && quota_error u $qid "write failed, expect succeed (3)"
5390
5391         $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=direct &&
5392                 quota_error u $qid "write succeed, expect EDQUOT (3)"
5393         true
5394 }
5395
5396 test_75()
5397 {
5398         (( $MDS1_VERSION >= $(version_code 2.14.52.68) )) ||
5399                 skip "need MDS >= v2_14_52-68-ga4fbe7341b for squash root fix"
5400
5401         local soft_limit=10 # MB
5402         local hard_limit=20 # MB
5403         local limit=$soft_limit
5404         local testfile="$DIR/$tdir/$tfile-0"
5405         local grace=20 # seconds
5406         local tdir_dom=${tdir}_dom
5407
5408         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
5409             grace=60
5410         fi
5411
5412         setup_quota_test || error "setup quota failed with $?"
5413         stack_trap cleanup_quota_test_75 EXIT
5414
5415         # enable ost quota
5416         set_ost_qtype $QTYPE || error "enable ost quota failed"
5417         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
5418
5419         local used=$(getquota -u $TSTID global curspace)
5420         $LFS setquota -t -u --block-grace $grace --inode-grace \
5421                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
5422         $LFS setquota -u $TSTUSR -b $((soft_limit+used/1024))M \
5423                         -B $((hard_limit+used/1024))M -i 0 -I 0 $DIR ||
5424                 error "set user quota failed"
5425
5426         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
5427         mkdir $DIR/$tdir_dom
5428         chmod 777 $DIR/$tdir_dom
5429         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
5430                 error "setstripe $tdir_dom failed"
5431
5432         do_facet mgs $LCTL nodemap_activate 1
5433         wait_nm_sync active
5434         do_facet mgs $LCTL nodemap_modify --name default \
5435                 --property admin --value 0
5436         do_facet mgs $LCTL nodemap_modify --name default \
5437                 --property trusted --value 0
5438         do_facet mgs $LCTL nodemap_modify --name default \
5439                 --property deny_unknown --value 0
5440         do_facet mgs $LCTL nodemap_modify --name default \
5441                 --property squash_uid --value $TSTID
5442         do_facet mgs $LCTL nodemap_modify --name default \
5443                 --property squash_gid --value $TSTID
5444         cancel_lru_locks mdc
5445         wait_nm_sync default admin_nodemap
5446         wait_nm_sync default trusted_nodemap
5447         wait_nm_sync default squash_uid
5448
5449         # mmap write when over soft limit
5450         limit=$soft_limit
5451         $DD of=$testfile count=${limit} || quota_error a $TSTUSR \
5452                         "root write failure, but expect success (1)"
5453         OFFSET=$((limit * 1024))
5454         cancel_lru_locks osc
5455
5456         echo "Write to exceed soft limit"
5457         dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
5458               quota_error a $TSTUSR "root write failure, but expect success (2)"
5459         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
5460         cancel_lru_locks osc
5461
5462         echo "mmap write when over soft limit"
5463         $MULTIOP $testfile.mmap OT40960SMW ||
5464                 quota_error a $TSTUSR "mmap write failure, but expect success"
5465         cancel_lru_locks osc
5466         rm -f $testfile*
5467         wait_delete_completed || error "wait_delete_completed failed (1)"
5468         sync_all_data || true
5469
5470         # test for user hard limit
5471         limit=$hard_limit
5472         log "Write..."
5473         $DD of=$testfile count=$((limit/2)) ||
5474                 quota_error u $TSTID \
5475                         "root write failure, but expect success (3)"
5476
5477         log "Write out of block quota ..."
5478         # possibly a cache write, ignore failure
5479         $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
5480         # flush cache, ensure noquota flag is set on client
5481         cancel_lru_locks osc
5482         sync; sync_all_data || true
5483         # sync forced cache flush, but did not guarantee that slave
5484         # got new edquot through glimpse, so wait to make sure
5485         sleep 5
5486         $DD of=$testfile count=1 seek=$limit conv=fsync &&
5487                 quota_error u $TSTID \
5488                         "user write success, but expect EDQUOT"
5489         rm -f $testfile
5490         wait_delete_completed || error "wait_delete_completed failed (2)"
5491         sync_all_data || true
5492         [ $(getquota -u $TSTUSR global curspace) -eq $used ] ||
5493                 quota_error u $TSTID "user quota not released after deletion"
5494
5495         test_dom_75
5496 }
5497 run_test 75 "nodemap squashed root respects quota enforcement"
5498
5499 test_76() {
5500         (( $MDS1_VERSION >= $(version_code 2.14.52.109) )) ||
5501                 skip "need MDS >= v2_14_52-109-g3ffa5d680f for bad PRJID fix"
5502         ! is_project_quota_supported &&
5503                 skip "skip project quota unsupported"
5504
5505         setup_quota_test || error "setup quota failed with $?"
5506         quota_init
5507
5508         local testfile="$DIR/$tdir/$tfile-0"
5509
5510         touch $testfile
5511         $LFS project -p 4294967295 $testfile &&
5512                 error "set project ID should fail"
5513         return 0
5514 }
5515 run_test 76 "project ID 4294967295 should be not allowed"
5516
5517 test_77()
5518 {
5519         (( $MDS1_VERSION >= $(version_code 2.14.54.33) )) ||
5520                 skip "need MDS >= v2_14_54-33-g29e00cecc6 for readonly fix"
5521
5522         mount_client $MOUNT2 "ro"
5523         lfs setquota -u $TSTUSR -b 100M -B 100M -i 10K -I 10K $MOUNT2 &&
5524                 error "lfs setquota should fail in read-only Lustre mount"
5525         umount $MOUNT2
5526 }
5527 run_test 77 "lfs setquota should fail in Lustre mount with 'ro'"
5528
5529 test_78A()
5530 {
5531         (( $OST1_VERSION >= $(version_code 2.14.55.173) )) ||
5532                 skip "need OST >= v2_14_55-173-g789038c97a for fallocate fix"
5533         check_set_fallocate_or_skip
5534
5535         setup_quota_test || error "setup quota failed with $?"
5536
5537         # enable ost quota
5538         set_ost_qtype $QTYPE || error "enable ost quota failed"
5539
5540         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5541         chown $TSTUSR $DIR/$tdir || error "failed to chown $tdir"
5542
5543         # setup quota limit
5544         $LFS setquota -u $TSTUSR -b25M -B25M $DIR/$tdir ||
5545                 error "lfs setquota failed"
5546
5547         # call fallocate
5548         runas -u $TSTUSR -g $TSTUSR fallocate -l 204800 $DIR/$tdir/$tfile
5549
5550         kbytes=$(lfs quota -u $TSTUSR $DIR |
5551                 awk -v pattern=$DIR 'match($0, pattern) {printf $2}')
5552         echo "kbytes returned:$kbytes"
5553
5554         # For file size of 204800. We should be having roughly 200 kbytes
5555         # returned. Anything alarmingly low (50 taken as arbitrary value)
5556         # would bail out this TC. Also this also avoids $kbytes of 0
5557         # to be used in calculation below.
5558         (( $kbytes > 50 )) ||
5559                 error "fallocate did not use quota. kbytes returned:$kbytes"
5560
5561         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5562         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5563
5564         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5565         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5566                 error "fallocate did not use quota correctly"
5567 }
5568 run_test 78A "Check fallocate increase quota usage"
5569
5570 test_78a()
5571 {
5572         (( $CLIENT_VERSION >= $(version_code 2.15.0.37) )) ||
5573                 skip "need client >= v2_15_50-37-g5fc934eb for falloc proj fix"
5574         (( $OST1_VERSION >= $(version_code 2.15.0.37) )) ||
5575                 skip "need OST >= v2_15_50-37-g5fc934ebbb for falloc proj fix"
5576         check_set_fallocate_or_skip
5577
5578         setup_quota_test || error "setup quota failed with $?"
5579
5580         # enable ost quota
5581         set_ost_qtype $QTYPE || error "enable ost quota failed"
5582
5583         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5584
5585         local projectid=5200 # Random project id to test
5586
5587         change_project -sp $projectid $DIR/$tdir
5588
5589         # setup quota limit
5590         $LFS setquota -p $projectid -b25M -B25M $DIR/$tdir ||
5591                 error "lfs setquota project failed"
5592
5593         # call fallocate
5594         fallocate -l 204800 $DIR/$tdir/$tfile
5595
5596         # Get curspace (kbytes) for $projectid
5597         local kbytes=$(getquota -p $projectid global curspace)
5598
5599         echo "kbytes returned:$kbytes"
5600
5601         # For file size of 204800. We should be having roughly 200 kbytes
5602         # returned. Anything alarmingly low (50 taken as arbitrary value)
5603         # would bail out this TC. Also this also avoids $kbytes of 0
5604         # to be used in calculation below.
5605         (( $kbytes > 50 )) ||
5606                 error "fallocate did not use projectid. kbytes returned:$kbytes"
5607
5608         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5609         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5610
5611         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5612         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5613                 error "fallocate did not use quota projectid correctly"
5614 }
5615 run_test 78a "Check fallocate increase projectid usage"
5616
5617 test_79()
5618 {
5619         (( $MDS1_VERSION >= $(version_code 2.14.56.37) )) ||
5620                 skip "need MDS >= v2_14_56-37-gc9901b68b4 for pool panic fix"
5621
5622         local qpool="qpool1"
5623         local cmd="$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$qpool.info"
5624         local stopf=$TMP/$tfile
5625
5626         do_facet mds1 "touch $stopf"
5627         stack_trap "do_facet mds1 'rm -f $stopf'"
5628         do_facet mds1 "while [ -e $stopf ]; do $cmd &>/dev/null; done"&
5629         local pid=$!
5630         pool_add $qpool || error "pool_add failed"
5631         do_facet mds1 "rm $stopf"
5632         wait $pid
5633 }
5634 run_test 79 "access to non-existed dt-pool/info doesn't cause a panic"
5635
5636 test_80()
5637 {
5638         (( $MDS1_VERSION >= $(version_code 2.14.56.51) )) ||
5639                 skip "need MDS >= v2_14_56-51-g61ec1e0f2c for EDQUOT failover"
5640
5641         local dir1="$DIR/$tdir/dir1"
5642         local dir2="$DIR/$tdir/dir2"
5643         local TESTFILE0="$dir1/$tfile-0"
5644         local TESTFILE1="$dir1/$tfile-1"
5645         local TESTFILE2="$dir1/$tfile-2"
5646         local TESTFILE3="$dir2/$tfile-0"
5647         local global_limit=100 # 100M
5648         local limit=10 # 10M
5649         local qpool="qpool1"
5650
5651         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
5652         mds_supports_qp
5653         [ "$ost1_FSTYPE" == zfs ] &&
5654                 skip "ZFS grants some block space together with inode"
5655         setup_quota_test || error "setup quota failed with $?"
5656         set_ost_qtype $QTYPE || error "enable ost quota failed"
5657
5658         # make sure the system is clean
5659         local used=$(getquota -u $TSTUSR global curspace)
5660         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR is not 0."
5661
5662         pool_add $qpool || error "pool_add failed"
5663         pool_add_targets $qpool 0 1 ||
5664                 error "pool_add_targets failed"
5665
5666         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5667                 error "set user quota failed"
5668
5669         $LFS setquota -u $TSTUSR -B ${global_limit}M --pool $qpool $DIR ||
5670                 error "set user quota failed"
5671         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5672                 error "set user quota failed"
5673
5674         mkdir -p $dir1 || error "failed to mkdir"
5675         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5676         mkdir -p $dir2 || error "failed to mkdir"
5677         chown $TSTUSR.$TSTUSR $dir2 || error "chown $dir2 failed"
5678
5679         $LFS setstripe $dir1 -i 1 -c 1|| error "setstripe $testfile failed"
5680         $LFS setstripe $dir2 -i 0 -c 1|| error "setstripe $testfile failed"
5681         lfs getstripe $dir1
5682         lfs getstripe $dir2
5683         sleep 3
5684
5685         $LFS quota -uv $TSTUSR $DIR
5686         #define OBD_FAIL_QUOTA_PREACQ            0xA06
5687         do_facet mds1 $LCTL set_param fail_loc=0xa06
5688         $RUNAS $DD of=$TESTFILE3 count=3 ||
5689                 quota_error u $TSTUSR "write failed"
5690         $RUNAS $DD of=$TESTFILE2 count=7 ||
5691                 quota_error u $TSTUSR "write failed"
5692         $RUNAS $DD of=$TESTFILE1 count=1 oflag=direct ||
5693                 quota_error u $TSTUSR "write failed"
5694         sync
5695         sleep 3
5696         $LFS quota -uv --pool $qpool $TSTUSR $DIR
5697
5698         rm -f $TESTFILE2
5699         stop ost2
5700         do_facet mds1 $LCTL set_param fail_loc=0
5701         start ost2 $(ostdevname 2) $OST_MOUNT_OPTS || error "start ost2 failed"
5702         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5703         # OST0 needs some time to update quota usage after removing TESTFILE2
5704         sleep 4
5705         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5706         $RUNAS $DD of=$TESTFILE0 count=2 oflag=direct ||
5707                 quota_error u $TSTUSR "write failure, but expect success"
5708 }
5709 run_test 80 "check for EDQUOT after OST failover"
5710
5711 test_81()
5712 {
5713         (( $MDS1_VERSION >= $(version_code 2.14.56.52) )) ||
5714                 skip "need MDS >= v2_14_56-52-g862f0baa7c for qmt_pool_free fix"
5715
5716         local global_limit=20  # 100M
5717         local testfile="$DIR/$tdir/$tfile-0"
5718         local qpool="qpool1"
5719
5720         mds_supports_qp
5721         setup_quota_test || error "setup quota failed with $?"
5722
5723         # enable ost quota
5724         set_ost_qtype $QTYPE || error "enable ost quota failed"
5725
5726         # test for user
5727         log "User quota (block hardlimit:$global_limit MB)"
5728         $LFS setquota -u $TSTUSR -B 1G $DIR || error "set user quota failed"
5729
5730         pool_add $qpool || error "pool_add failed"
5731         #define OBD_FAIL_QUOTA_RECALC   0xA07
5732         do_facet mds1 $LCTL set_param fail_loc=0x80000A07 fail_val=30
5733         # added OST casues to start pool recalculation
5734         pool_add_targets $qpool 0 0 1
5735         stop mds1 -f || error "MDS umount failed"
5736
5737         #start mds1 back to destroy created pool
5738         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
5739         clients_up || true
5740 }
5741 run_test 81 "Race qmt_start_pool_recalc with qmt_pool_free"
5742
5743 test_82()
5744 {
5745         (( $MDS1_VERSION >= $(version_code 2.15.50.72) )) ||
5746                 skip "need MDS >= v2_15_50-72-g61481796ac for over 8 QIDs fix"
5747
5748         is_project_quota_supported || skip "skip project quota unsupported"
5749
5750         setup_quota_test || error "setup quota failed with $?"
5751         quota_init
5752
5753         local parent_dir="$DIR/$tdir.parent"
5754         local child_dir="$parent_dir/child"
5755
5756         mkdir -p $child_dir
5757         stack_trap "chown -R 0:0 $parent_dir"
5758
5759         chown $TSTUSR:$TSTUSR $parent_dir ||
5760                 error "failed to chown on $parent_dir"
5761         chown $TSTUSR2:$TSTUSRS2 $child_dir ||
5762                 error "failed to chown on $parent_dir"
5763
5764         $LFS project -p 1000 $parent_dir ||
5765                 error "failed to set project id on $parent_dir"
5766         $LFS project -p 1001 $child_dir ||
5767                 error "failed to set project id on $child_dir"
5768
5769         rmdir $child_dir || error "cannot remove child dir, test failed"
5770 }
5771 run_test 82 "verify more than 8 qids for single operation"
5772
5773 test_grace_with_default_quota()
5774 {
5775         local qtype=$1
5776         local qdtype=$2
5777         local bgrace
5778         local igrace
5779         local bgrace2
5780         local igrace2
5781         echo "ttt1"
5782         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
5783                 error "clear default quota [$qdtype] failed"
5784         echo "ttt2"
5785         $LFS setquota -t $qtype --block-grace 1w --inode-grace 1w $DIR ||
5786                 error "reset quota [$qdtype] grace failed"
5787         echo "ttt3"
5788
5789         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5790                         '{printf("bgrace=%s;igrace=%s;", $4, $9)}')
5791         echo "ttt4"
5792
5793         $LFS setquota $qdtype -B 10G -i 10k $DIR
5794         echo "ttt5"
5795
5796         eval $($LFS quota -t $qtype $DIR | awk -F "[; ]" \
5797                         '{printf("bgrace2=%s;igrace2=%s;", $4, $9)}')
5798
5799         [ "$bgrace" == "$bgrace2" ] ||
5800                         error "set default quota shouldn't affect block grace"
5801         [ "$igrace" == "$igrace2" ] ||
5802                         error "set default quota shouldn't affect inode grace"
5803
5804 }
5805
5806 test_83()
5807 {
5808         (( $MDS1_VERSION >= $(version_code 2.15.51.29) )) ||
5809                 skip "need MDS >= v2_15_51-29-gd4978678b4 for grace time fix"
5810
5811         setup_quota_test || error "setup quota failed with $?"
5812         test_grace_with_default_quota "-u" "-U"
5813         test_grace_with_default_quota "-g" "-G"
5814
5815         is_project_quota_supported || return 0
5816         test_grace_with_default_quota "-p" "-P"
5817 }
5818 run_test 83 "Setting default quota shouldn't affect grace time"
5819
5820 test_84()
5821 {
5822         (( $MDS1_VERSION >= $(version_code 2.15.53.115) )) ||
5823                 skip "need MDS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5824         (( $OST1_VERSION >= $(version_code 2.15.53.115) )) ||
5825                 skip "need OSS >= v2_15_53-115-ga2fd4d3aee for insane quota fix"
5826
5827         local dir1="$DIR/$tdir/dir1"
5828         local TESTFILE1="$dir1/$tfile-1"
5829         local waited=0
5830         local grant=0
5831         local grant2=0
5832         local qp="qpool1"
5833
5834         mds_supports_qp
5835
5836         setup_quota_test || error "setup quota failed with $?"
5837         quota_init
5838         set_ost_qtype $QTYPE || error "enable ost quota failed"
5839
5840         pool_add $qp || error "pool_add failed"
5841         pool_add_targets $qp 0 $(($OSTCOUNT - 1)) ||
5842                 error "pool_add_targets failed"
5843
5844         $LFS setquota -g $TSTUSR -B 10G $DIR ||
5845                 error "failed to set group quota for $TSTUSR"
5846         $LFS setquota -g $TSTUSR -B 5G --pool $qp $DIR ||
5847                 error "set user quota failed"
5848         $LFS quota -gv $TSTUSR $DIR
5849
5850         wait_quota_setting_synced "grp" $TSTID "hardlimit" $((10*1024*1024))
5851
5852         mkdir -p $dir1 || error "failed to mkdir"
5853         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5854
5855         $LFS setstripe -c 1 -i 0 $TESTFILE1
5856         $LFS getstripe $TESTFILE1
5857         chown $TSTUSR.$TSTUSR $TESTFILE1
5858
5859         $RUNAS $DD of=$TESTFILE1 count=60 conv=nocreat oflag=direct ||
5860                 quota_error g $TSTUSR "write failed"
5861
5862         sync_all_data || true
5863         sleep 3
5864         $LFS quota -gv $TSTUSR $DIR
5865         $LFS quota -gv --pool $qp $TSTUSR $DIR
5866
5867         # the grant quota should be larger than 0
5868         waited=0
5869         while (( $waited < 60 )); do
5870                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
5871                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
5872                 (( ${grant} > 0 && ${grant2} > 0 )) && break
5873
5874                 do_facet ost1 $LCTL set_param \
5875                                 osd-*.*-OST0000.quota_slave.force_reint=1
5876                 sleep 1
5877                 waited=$((waited + 1))
5878         done
5879
5880         (( $waited >= 60)) && {
5881                 $LFS quota -gv $TSTUSR $DIR
5882                 $LFS quota -gv --pool $qp $TSTUSR $DIR
5883         }
5884
5885         (( ${grant} > 0 )) || error "pool grant is not increased after dd"
5886         (( ${grant2} > 0 )) || error "grant is not increased after dd"
5887
5888 #define OBD_FAIL_QUOTA_GRANT 0xA08
5889         lustre_fail mds 0xa08
5890         lustre_fail ost 0xa08
5891         sleep 1
5892
5893         # clear quota limits to trigger updating grant quota
5894         $LFS setquota -g $TSTUSR -b 0 -B 0 $DIR ||
5895                 error "failed to clear the group quota for $TSTUSR"
5896         $LFS quota -gv $TSTUSR $DIR
5897         $LFS quota -gv --pool $qp $TSTUSR $DIR
5898
5899         # the grant quota should be set as insane value
5900         waited=0
5901         while (( $waited < 60 )); do
5902                 grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
5903                 grant2=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
5904                 (( ${#grant} == 20 && ${#grant2} == 20 )) && break
5905
5906                 sleep 1
5907                 waited=$((waited + 1))
5908         done
5909
5910         (( $waited >= 60)) && {
5911                 $LFS quota -gv $TSTUSR $DIR
5912                 $LFS quota -gv --pool $qp $TSTUSR $DIR
5913         }
5914
5915         (( ${#grant} == 20 )) || error "pool grant is not set as insane value"
5916         (( ${#grant2} == 20 )) || error "grant is not set as insane value"
5917
5918         lustre_fail mds_ost 0
5919         sleep 1
5920
5921         # reset the quota
5922         $LFS setquota -g $TSTUSR -r $DIR ||
5923                 error "failed to reset group quota for $TSTUSR"
5924
5925         sleep 3
5926         $LFS quota -gv $TSTUSR $DIR
5927         $LFS quota -gv --pool $qp $TSTUSR $DIR
5928
5929         # the grant quota should be reset
5930         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit)
5931         (( ${#grant} == 20 )) && error "grant is not cleared"
5932         grant=$(getquota -g $TSTUSR lustre-OST0000_UUID bhardlimit $qp)
5933         (( ${#grant} == 20 )) && error "pool grant is not cleared"
5934
5935         $LFS quota -gv $TSTUSR --pool $qp $DIR
5936         $LFS quota -gv --pool $qp $TSTUSR $DIR
5937         local hlimit=$(getquota -g $TSTUSR global bhardlimit $qp)
5938          [ $hlimit -eq 5242880 ] || error "pool limit is changed"
5939
5940         # test whether the quota still works
5941         $LFS setquota -g $TSTUSR -B 100M $DIR ||
5942                 error "failed to set group quota for $TSTUSR"
5943         $LFS quota -gv $TSTUSR $DIR
5944
5945         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct &&
5946                 quota_error g $TSTUSR "dd succeed, expect EDQUOT"
5947
5948         $LFS setquota -g $TSTUSR -B 300M $DIR ||
5949                 error "failed to set group quota for $TSTUSR"
5950         $LFS quota -gv $TSTUSR $DIR
5951
5952         $RUNAS $DD of=$TESTFILE1 count=200 conv=nocreat oflag=direct ||
5953                 quota_error g $TSTUSR "dd failed, expect succeed"
5954 }
5955 run_test 84 "Reset quota should fix the insane granted quota"
5956
5957 test_85()
5958 {
5959         (( $MDS1_VERSION >= $(version_code 2.15.55.5) )) ||
5960                 skip "need MDS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
5961         (( $OST1_VERSION >= $(version_code 2.15.55.5) )) ||
5962                 skip "need OSS >= v2_15_55-5-g6c0b4329d0 for least_qunit fix"
5963
5964         local limit=3 # 3M
5965         local qpool="qpool1"
5966         local qpool2="qpool2"
5967         local tfile1="$DIR/$tdir/$tfile-0"
5968
5969         (( OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
5970         mds_supports_qp
5971         setup_quota_test || error "setup quota failed with $?"
5972
5973         # enable ost quota
5974         set_ost_qtype $QTYPE || error "enable ost quota failed"
5975
5976         $LFS setquota -u $TSTUSR -b 0 -B 50T -i 0 -I 0 $DIR ||
5977                 error "set user quota failed"
5978
5979         pool_add $qpool || error "pool_add failed"
5980         pool_add_targets $qpool 0 1 ||
5981                 error "pool_add_targets failed"
5982
5983         pool_add $qpool2 || error "pool_add failed"
5984         pool_add_targets $qpool2 0 1 ||
5985                 error "pool_add_targets failed"
5986
5987         $LFS setstripe -p $qpool $DIR/$tdir || error "cannot set stripe"
5988         $LFS setquota -u $TSTUSR -B 30M --pool $qpool $DIR ||
5989                 error "set user quota failed"
5990         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5991                 error "set user quota failed"
5992
5993         # don't care about returned value. Just check we don't hung on write.
5994         $RUNAS $DD of=$tfile1 count=10
5995         return 0
5996 }
5997 run_test 85 "do not hung at write with the least_qunit"
5998
5999 quota_fini()
6000 {
6001         do_nodes $(comma_list $(nodes_list)) \
6002                 "lctl set_param -n debug=-quota-trace"
6003         if $PQ_CLEANUP; then
6004                 disable_project_quota
6005         fi
6006 }
6007 reset_quota_settings
6008 quota_fini
6009
6010 cd $ORIG_PWD
6011 complete_test $SECONDS
6012 check_and_cleanup_lustre
6013 export QUOTA_AUTO=$QUOTA_AUTO_OLD
6014 exit_status