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