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