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