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