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