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