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