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