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