Whamcloud - gitweb
2347d59183403fe36edcff9cdb25f396d2aa07ab
[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         local SOFT_LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1569                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1570
1571         setup_quota_test
1572         trap cleanup_quota_test EXIT
1573         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
1574
1575         echo "Create files to exceed soft limit"
1576         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
1577                 quota_error a $TSTUSR "create failure, but expect success"
1578         local trigger_time=$(date +%s)
1579
1580         sync_all_data || true
1581
1582         local cur_time=$(date +%s)
1583         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
1584                 error "Passed grace time $grace, $trigger_time, $cur_time"
1585
1586         echo "Create file before timer goes off"
1587         $RUNAS touch ${TESTFILE}_before ||
1588                 quota_error a $TSTUSR "failed create before timer expired," \
1589                         "but expect success. $trigger_time, $cur_time"
1590         sync_all_data || true
1591
1592         wait_grace_time $qtype "file"
1593
1594         $SHOW_QUOTA_USER
1595         $SHOW_QUOTA_GROUP
1596         $SHOW_QUOTA_PROJID
1597         $SHOW_QUOTA_INFO_USER
1598         $SHOW_QUOTA_INFO_GROUP
1599         $SHOW_QUOTA_INFO_PROJID
1600
1601         echo "Create file after timer goes off"
1602         # exceed least soft limit is possible
1603         $RUNAS createmany -m ${TESTFILE}_after_3 $((SOFT_LIMIT + 1)) &&
1604                 quota_error a $TSTUSR "create after timer expired," \
1605                         "but expect EDQUOT"
1606         sync_all_data || true
1607
1608         $SHOW_QUOTA_USER
1609         $SHOW_QUOTA_GROUP
1610         $SHOW_QUOTA_PROJID
1611         $SHOW_QUOTA_INFO_USER
1612         $SHOW_QUOTA_INFO_GROUP
1613         $SHOW_QUOTA_INFO_PROJID
1614
1615         echo "Unlink files to stop timer"
1616         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
1617         wait_delete_completed
1618
1619         echo "Create file"
1620         $RUNAS touch ${TESTFILE}_xxx ||
1621                 quota_error a $TSTUSR "touch after timer stop failure," \
1622                         "but expect success"
1623         sync_all_data || true
1624
1625         # cleanup
1626         cleanup_quota_test
1627 }
1628
1629 # file soft limit
1630 test_4a() {
1631         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1632                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1633         local TESTFILE=$DIR/$tdir/$tfile-0
1634         local GRACE=12
1635
1636         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1637
1638         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1639         # make sure the system is clean
1640         local USED=$(getquota -u $TSTUSR global curinodes)
1641         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1642
1643         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1644                 $GRACE $DIR || error "set user grace time failed"
1645         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1646                 error "set user quota failed"
1647
1648         [ "$mds1_FSTYPE" = zfs ] && GRACE=20
1649
1650         test_file_soft $TESTFILE $LIMIT $GRACE "u"
1651
1652         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1653         # make sure the system is clean
1654         USED=$(getquota -g $TSTUSR global curinodes)
1655         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
1656
1657         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1658                 $GRACE $DIR || error "set group grace time failed"
1659         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1660                 error "set group quota failed"
1661         TESTFILE=$DIR/$tdir/$tfile-1
1662
1663         test_file_soft $TESTFILE $LIMIT $GRACE "g"
1664
1665         if is_project_quota_supported; then
1666                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
1667                 # make sure the system is clean
1668                 USED=$(getquota -p $TSTPRJID global curinodes)
1669                 [ $USED -ne 0 ] && error \
1670                         "Used space($USED) for project $TSTPRJID isn't 0."
1671
1672                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1673                         $GRACE $DIR || error "set project grace time failed"
1674                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1675                         error "set project quota failed"
1676
1677                 TESTFILE=$DIR/$tdir/$tfile-1
1678                 # one less than limit, because of parent directory included.
1679                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
1680                 resetquota -p $TSTPRJID
1681                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1682                         $MAX_IQ_TIME $DIR ||
1683                                 error "restore project grace time failed"
1684         fi
1685
1686         # cleanup
1687         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1688                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1689         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1690                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1691 }
1692 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1693
1694 test_4b() {
1695         local GR_STR1="1w3d"
1696         local GR_STR2="1000s"
1697         local GR_STR3="5s"
1698         local GR_STR4="1w2d3h4m5s"
1699         local GR_STR5="5c"
1700         local GR_STR6="18446744073709551615"
1701         local GR_STR7="-1"
1702
1703         wait_delete_completed
1704
1705         # test of valid grace strings handling
1706         echo "Valid grace strings test"
1707         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1708                 $GR_STR2 $DIR || error "set user grace time failed"
1709         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1710         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1711                 $GR_STR4 $DIR || error "set group grace time quota failed"
1712         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1713
1714         # test of invalid grace strings handling
1715         echo "  Invalid grace strings test"
1716         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1717         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1718         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1719                 $GR_STR7 $DIR
1720
1721         # cleanup
1722         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1723                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1724         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1725                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1726 }
1727 run_test 4b "Grace time strings handling"
1728
1729 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1730 test_5() {
1731         local BLIMIT=10 # 10M
1732         local ILIMIT=10 # 10 inodes
1733
1734         setup_quota_test || error "setup quota failed with $?"
1735         trap cleanup_quota_test EXIT
1736
1737         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1738         set_ost_qtype $QTYPE || error "enable ost quota failed"
1739
1740         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1741         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1742                 error "set user quota failed"
1743         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1744         if is_project_quota_supported; then
1745                 error "set group quota failed"
1746                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1747                         -I $ILIMIT $DIR || error "set project quota failed"
1748         fi
1749
1750         # make sure the system is clean
1751         local USED=$(getquota -u $TSTUSR global curinodes)
1752         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1753         USED=$(getquota -g $TSTUSR global curinodes)
1754         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1755         USED=$(getquota -u $TSTUSR global curspace)
1756         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1757         USED=$(getquota -g $TSTUSR global curspace)
1758         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1759         if is_project_quota_supported; then
1760                 USED=$(getquota -p $TSTPRJID global curinodes)
1761                 [ $USED -ne 0 ] &&
1762                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1763                 USED=$(getquota -p $TSTPRJID global curspace)
1764                 [ $USED -ne 0 ] &&
1765                         error "Used block($USED) for project $TSTPRJID isn't 0."
1766         fi
1767
1768         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1769         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1770                 error "create failure, expect success"
1771         if is_project_quota_supported; then
1772                 touch $DIR/$tdir/$tfile-0_1
1773                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1774         fi
1775         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1776                 error "write failure, expect success"
1777
1778         echo "Chown files to $TSTUSR.$TSTUSR ..."
1779         for i in $(seq 0 $ILIMIT); do
1780                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1781                         quota_error a $TSTUSR "chown failure, expect success"
1782         done
1783
1784         # cleanup
1785         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1786                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1787         cleanup_quota_test
1788 }
1789 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1790
1791 # test dropping acquire request on master
1792 test_6() {
1793         local LIMIT=3 # 3M
1794
1795         # Clear dmesg so watchdog is not triggered by previous
1796         # test output
1797         do_facet ost1 dmesg -c > /dev/null
1798
1799         setup_quota_test || error "setup quota failed with $?"
1800         trap cleanup_quota_test EXIT
1801
1802         # make sure the system is clean
1803         local USED=$(getquota -u $TSTUSR global curspace)
1804         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1805
1806         # make sure no granted quota on ost
1807         set_ost_qtype $QTYPE || error "enable ost quota failed"
1808         resetquota -u $TSTUSR
1809
1810         # create file for $TSTUSR
1811         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1812         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1813         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1814
1815         # create file for $TSTUSR2
1816         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1817         $LFS setstripe $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1818         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1819
1820         # cache per-ID lock for $TSTUSR on slave
1821         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1822                 error "set quota failed"
1823         $RUNAS $DD of=$TESTFILE count=1 ||
1824                 error "write $TESTFILE failure, expect success"
1825         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1826                 error "write $TESTFILE2 failure, expect success"
1827
1828         if at_is_enabled; then
1829                 at_max_saved=$(at_max_get ost1)
1830                 at_max_set $TIMEOUT ost1
1831
1832                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
1833                 # DQACQ is sent, which makes at_max to take effect
1834                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
1835                                                                 conv=notrunc
1836                 rm -f $TESTFILE
1837                 wait_delete_completed
1838         fi
1839
1840         sync; sync
1841         sync_all_data || true
1842
1843         #define QUOTA_DQACQ 601
1844         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1845         lustre_fail mds 0x513 601
1846
1847         do_facet ost1 $LCTL set_param \
1848                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1849
1850         # write to un-enforced ID ($TSTUSR2) should succeed
1851         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1852                 error "write failure, expect success"
1853
1854         # write to enforced ID ($TSTUSR) in background, exceeding limit
1855         # to make sure DQACQ is sent
1856         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1857         DDPID=$!
1858
1859         # watchdog timer uses a factor of 2
1860         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1861         sleep $((TIMEOUT * 2 + 1))
1862
1863         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1864
1865         # write should be blocked and never finished
1866         if ! ps -p $DDPID  > /dev/null 2>&1; then
1867                 lustre_fail mds 0 0
1868                 error "write finished incorrectly!"
1869         fi
1870
1871         lustre_fail mds 0 0
1872
1873         # no watchdog is triggered
1874         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1875         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1876                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1877         [ -z "$watchdog" ] || error "$watchdog"
1878
1879         rm -f $TMP/lustre-log-${TESTNAME}.log
1880
1881         # write should continue then fail with EDQUOT
1882         local count=0
1883         local c_size
1884         while [ true ]; do
1885                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1886                 if [ $count -ge 240 ]; then
1887                         quota_error u $TSTUSR "dd not finished in $count secs"
1888                 fi
1889                 count=$((count + 1))
1890                 if [ $((count % 30)) -eq 0 ]; then
1891                         c_size=$(stat -c %s $TESTFILE)
1892                         echo "Waiting $count secs. $c_size"
1893                         $SHOW_QUOTA_USER
1894                 fi
1895                 sleep 1
1896         done
1897
1898         cleanup_quota_test
1899 }
1900 run_test 6 "Test dropping acquire request on master"
1901
1902 # quota reintegration (global index)
1903 test_7a() {
1904         local TESTFILE=$DIR/$tdir/$tfile
1905         local LIMIT=20 # 20M
1906
1907         [ "$SLOW" = "no" ] && LIMIT=5
1908
1909         setup_quota_test || error "setup quota failed with $?"
1910         trap cleanup_quota_test EXIT
1911
1912         # make sure the system is clean
1913         local USED=$(getquota -u $TSTUSR global curspace)
1914         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1915
1916         # make sure no granted quota on ost1
1917         set_ost_qtype $QTYPE || error "enable ost quota failed"
1918         resetquota -u $TSTUSR
1919         set_ost_qtype "none" || error "disable ost quota failed"
1920
1921         local OSTUUID=$(ostuuid_from_index 0)
1922         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1923         [ $USED -ne 0 ] &&
1924                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1925
1926         # create test file
1927         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1928         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1929
1930         echo "Stop ost1..."
1931         stop ost1
1932
1933         echo "Enable quota & set quota limit for $TSTUSR"
1934         set_ost_qtype $QTYPE || error "enable ost quota failed"
1935         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1936                 error "set quota failed"
1937
1938         echo "Start ost1..."
1939         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1940         quota_init
1941
1942         wait_ost_reint $QTYPE || error "reintegration failed"
1943
1944         # hardlimit should have been fetched by slave during global
1945         # reintegration, write will exceed quota
1946         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1947                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1948
1949         rm -f $TESTFILE
1950         wait_delete_completed
1951         sync_all_data || true
1952         sleep 3
1953
1954         echo "Stop ost1..."
1955         stop ost1
1956
1957         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1958                 error "clear quota failed"
1959
1960         echo "Start ost1..."
1961         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1962         quota_init
1963
1964         wait_ost_reint $QTYPE || error "reintegration failed"
1965
1966         # hardlimit should be cleared on slave during reintegration
1967         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1968                 quota_error u $TSTUSR "write error, but expect success"
1969
1970         cleanup_quota_test
1971 }
1972 run_test 7a "Quota reintegration (global index)"
1973
1974 # quota reintegration (slave index)
1975 test_7b() {
1976         local LIMIT="100G"
1977         local TESTFILE=$DIR/$tdir/$tfile
1978
1979         setup_quota_test || error "setup quota failed with $?"
1980         trap cleanup_quota_test EXIT
1981
1982         # make sure the system is clean
1983         local USED=$(getquota -u $TSTUSR global curspace)
1984         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1985
1986         # make sure no granted quota on ost1
1987         set_ost_qtype $QTYPE || error "enable ost quota failed"
1988         resetquota -u $TSTUSR
1989         set_ost_qtype "none" || error "disable ost quota failed"
1990
1991         local OSTUUID=$(ostuuid_from_index 0)
1992         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1993         [ $USED -ne 0 ] &&
1994                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1995
1996         # create test file
1997         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1998         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1999
2000         # consume some space to make sure the granted space will not
2001         # be released during reconciliation
2002         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
2003                 error "consume space failure, expect success"
2004
2005         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
2006         lustre_fail mds 0xa02
2007
2008         set_ost_qtype $QTYPE || error "enable ost quota failed"
2009         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2010                 error "set quota failed"
2011
2012         # ignore the write error
2013         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
2014
2015         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2016
2017         lustre_fail mds 0
2018
2019         echo "Restart ost to trigger reintegration..."
2020         stop ost1
2021         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
2022         quota_init
2023
2024         wait_ost_reint $QTYPE || error "reintegration failed"
2025
2026         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2027         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
2028
2029         cleanup_quota_test
2030         $SHOW_QUOTA_USER
2031 }
2032 run_test 7b "Quota reintegration (slave index)"
2033
2034 # quota reintegration (restart mds during reintegration)
2035 test_7c() {
2036         local LIMIT=20 # 20M
2037         local TESTFILE=$DIR/$tdir/$tfile
2038
2039         [ "$SLOW" = "no" ] && LIMIT=5
2040
2041         setup_quota_test || error "setup quota failed with $?"
2042         trap cleanup_quota_test EXIT
2043
2044         # make sure the system is clean
2045         local USED=$(getquota -u $TSTUSR global curspace)
2046         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2047
2048         set_ost_qtype "none" || error "disable ost quota failed"
2049         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
2050                 error "set quota failed"
2051
2052         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
2053         lustre_fail ost 0xa03
2054
2055         # enable ost quota
2056         set_ost_qtype $QTYPE || error "enable ost quota failed"
2057         # trigger reintegration
2058         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
2059         procf=${procf}quota_slave.force_reint
2060         do_facet ost1 $LCTL set_param $procf=1 ||
2061                 error "force reintegration failed"
2062
2063         echo "Stop mds..."
2064         stop mds1
2065
2066         lustre_fail ost 0
2067
2068         echo "Start mds..."
2069         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
2070         quota_init
2071
2072         # wait longer than usual to make sure the reintegration
2073         # is triggered by quota wb thread.
2074         wait_ost_reint $QTYPE 200 || error "reintegration failed"
2075
2076         # hardlimit should have been fetched by slave during global
2077         # reintegration, write will exceed quota
2078         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
2079                 quota_error u $TSTUSR "write success, but expect EDQUOT"
2080
2081         cleanup_quota_test
2082 }
2083 run_test 7c "Quota reintegration (restart mds during reintegration)"
2084
2085 # Quota reintegration (Transfer index in multiple bulks)
2086 test_7d(){
2087         local TESTFILE=$DIR/$tdir/$tfile
2088         local TESTFILE1="$DIR/$tdir/$tfile"-1
2089         local limit=20 #20M
2090
2091         setup_quota_test || error "setup quota failed with $?"
2092         trap cleanup_quota_test EXIT
2093
2094         set_ost_qtype "none" || error "disable ost quota failed"
2095         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
2096                 error "set quota for $TSTUSR failed"
2097         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
2098                 error "set quota for $TSTUSR2 failed"
2099
2100         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
2101         lustre_fail mds 0x608 0
2102
2103         # enable quota to tirgger reintegration
2104         set_ost_qtype "u" || error "enable ost quota failed"
2105         wait_ost_reint "u" || error "reintegration failed"
2106
2107         lustre_fail mds 0
2108
2109         # hardlimit should have been fetched by slave during global
2110         # reintegration, write will exceed quota
2111         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
2112                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
2113
2114         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
2115                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
2116
2117         cleanup_quota_test
2118 }
2119 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
2120
2121 # quota reintegration (inode limits)
2122 test_7e() {
2123         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2124
2125         # LU-2435: skip this quota test if underlying zfs version has not
2126         # supported native dnode accounting
2127         [ "$mds1_FSTYPE" == zfs ] && {
2128                 local F="feature@userobj_accounting"
2129                 local pool=$(zpool_name mds1)
2130                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
2131
2132                 [[ "$feature" != *" active "* ]] &&
2133                         skip "requires zpool with active userobj_accounting"
2134         }
2135
2136         local ilimit=$((1024 * 2)) # 2k inodes
2137         local TESTFILE=$DIR/${tdir}-1/$tfile
2138
2139         setup_quota_test || error "setup quota failed with $?"
2140         trap cleanup_quota_test EXIT
2141
2142         # make sure the system is clean
2143         local USED=$(getquota -u $TSTUSR global curinodes)
2144         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
2145
2146         # make sure no granted quota on mdt1
2147         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2148         resetquota -u $TSTUSR
2149         set_mdt_qtype "none" || error "disable mdt quota failed"
2150
2151         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
2152         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
2153         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
2154                 "$TSTUSR isn't 0."
2155
2156         echo "Stop mds${MDSCOUNT}..."
2157         stop mds${MDSCOUNT}
2158
2159         echo "Enable quota & set quota limit for $TSTUSR"
2160         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2161         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2162                 error "set quota failed"
2163
2164         echo "Start mds${MDSCOUNT}..."
2165         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2166         quota_init
2167
2168         wait_mdt_reint $QTYPE || error "reintegration failed"
2169
2170         echo "create remote dir"
2171         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
2172                 error "create remote dir failed"
2173         chmod 0777 $DIR/${tdir}-1
2174
2175         # hardlimit should have been fetched by slave during global
2176         # reintegration, create will exceed quota
2177         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
2178                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
2179
2180         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
2181         wait_delete_completed
2182         sync_all_data || true
2183
2184         echo "Stop mds${MDSCOUNT}..."
2185         stop mds${MDSCOUNT}
2186
2187         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
2188                 error "clear quota failed"
2189
2190         echo "Start mds${MDSCOUNT}..."
2191         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2192         quota_init
2193
2194         wait_mdt_reint $QTYPE || error "reintegration failed"
2195
2196         # hardlimit should be cleared on slave during reintegration
2197         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
2198                 quota_error u $TSTUSR "create failed, expect success"
2199
2200         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
2201         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2202
2203         cleanup_quota_test
2204 }
2205 run_test 7e "Quota reintegration (inode limits)"
2206
2207 # run dbench with quota enabled
2208 test_8() {
2209         local BLK_LIMIT="100g" #100G
2210         local FILE_LIMIT=1000000
2211
2212         setup_quota_test || error "setup quota failed with $?"
2213         trap cleanup_quota_test EXIT
2214
2215         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2216         set_ost_qtype $QTYPE || error "enable ost quota failed"
2217
2218         echo "Set enough high limit for user: $TSTUSR"
2219         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2220                 error "set user quota failed"
2221         echo "Set enough high limit for group: $TSTUSR"
2222         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2223                 error "set group quota failed"
2224         if is_project_quota_supported; then
2225                 change_project -sp $TSTPRJID $DIR/$tdir
2226                 echo "Set enough high limit for project: $TSTPRJID"
2227                 $LFS setquota -p $TSTPRJID -b 0 \
2228                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2229                         error "set project quota failed"
2230         fi
2231
2232         local duration=""
2233         [ "$SLOW" = "no" ] && duration=" -t 120"
2234         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
2235                 quota_error a $TSTUSR "dbench failed!"
2236
2237         is_project_quota_supported && change_project -C $DIR/$tdir
2238         cleanup_quota_test
2239 }
2240 run_test 8 "Run dbench with quota enabled"
2241
2242 # this check is just for test_9
2243 OST0_MIN=4900000 #4.67G
2244
2245 check_whether_skip () {
2246         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
2247         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
2248         if [ $OST0_SIZE -lt $OST0_MIN ]; then
2249                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
2250                 return 0
2251         else
2252                 return 1
2253         fi
2254 }
2255
2256 # run for fixing bug10707, it needs a big room. test for 64bit
2257 test_9() {
2258         local filesize=$((1024 * 9 / 2)) # 4.5G
2259
2260         check_whether_skip && return 0
2261
2262         setup_quota_test || error "setup quota failed with $?"
2263         trap cleanup_quota_test EXIT
2264
2265         set_ost_qtype "ug" || error "enable ost quota failed"
2266
2267         local TESTFILE="$DIR/$tdir/$tfile-0"
2268         local BLK_LIMIT=100G #100G
2269         local FILE_LIMIT=1000000
2270
2271         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
2272
2273         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2274                 "for user: $TSTUSR"
2275         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2276                 error "set user quota failed"
2277
2278         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2279                 "for group: $TSTUSR"
2280         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2281                 error "set group quota failed"
2282
2283         quota_show_check a u $TSTUSR
2284         quota_show_check a g $TSTUSR
2285
2286         echo "Create test file"
2287         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2288         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2289
2290         log "Write the big file of 4.5G ..."
2291         $RUNAS $DD of=$TESTFILE count=$filesize ||
2292                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
2293
2294         $SHOW_QUOTA_USER
2295         $SHOW_QUOTA_GROUP
2296
2297         cleanup_quota_test
2298
2299         $SHOW_QUOTA_USER
2300         $SHOW_QUOTA_GROUP
2301 }
2302 run_test 9 "Block limit larger than 4GB (b10707)"
2303
2304 test_10() {
2305         local TESTFILE=$DIR/$tdir/$tfile
2306
2307         setup_quota_test || error "setup quota failed with $?"
2308         trap cleanup_quota_test EXIT
2309
2310         # set limit to root user should fail
2311         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
2312                 error "set limit for root user successfully, expect failure"
2313         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
2314                 error "set limit for root group successfully, expect failure"
2315         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
2316                 error "set limit for project 0 successfully, expect failure"
2317
2318         # root user can overrun quota
2319         set_ost_qtype "ug" || error "enable ost quota failed"
2320
2321         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
2322                 error "set quota failed"
2323         quota_show_check b u $TSTUSR
2324
2325         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
2326         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2327
2328         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
2329                 error "write failure, expect success"
2330
2331         cleanup_quota_test
2332 }
2333 run_test 10 "Test quota for root user"
2334
2335 test_11() {
2336         local TESTFILE=$DIR/$tdir/$tfile
2337         setup_quota_test || error "setup quota failed with $?"
2338         trap cleanup_quota_test EXIT
2339
2340         set_mdt_qtype "ug" || error "enable mdt quota failed"
2341         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
2342                 error "set quota failed"
2343
2344         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
2345         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
2346
2347         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
2348         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
2349
2350         $SHOW_QUOTA_USER
2351         local USED=$(getquota -u $TSTUSR global curinodes)
2352         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
2353
2354         cleanup_quota_test
2355 }
2356 run_test 11 "Chown/chgrp ignores quota"
2357
2358 test_12a() {
2359         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
2360
2361         local blimit=22 # 22M
2362         local blk_cnt=$((blimit - 5))
2363         local TESTFILE0="$DIR/$tdir/$tfile"-0
2364         local TESTFILE1="$DIR/$tdir/$tfile"-1
2365
2366         setup_quota_test || error "setup quota failed with $?"
2367         trap cleanup_quota_test EXIT
2368
2369         set_ost_qtype "u" || error "enable ost quota failed"
2370         quota_show_check b u $TSTUSR
2371
2372         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
2373                 error "set quota failed"
2374
2375         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
2376         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
2377         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
2378         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
2379
2380         echo "Write to ost0..."
2381         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
2382                 quota_error a $TSTUSR "dd failed"
2383
2384         echo "Write to ost1..."
2385         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
2386                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
2387
2388         echo "Free space from ost0..."
2389         rm -f $TESTFILE0
2390         wait_delete_completed
2391         sync_all_data || true
2392
2393         echo "Write to ost1 after space freed from ost0..."
2394         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
2395                 quota_error a $TSTUSR "rebalancing failed"
2396
2397         cleanup_quota_test
2398 }
2399 run_test 12a "Block quota rebalancing"
2400
2401 test_12b() {
2402         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2403
2404         local ilimit=$((1024 * 2)) # 2k inodes
2405         local TESTFILE0=$DIR/$tdir/$tfile
2406         local TESTFILE1=$DIR/${tdir}-1/$tfile
2407
2408         setup_quota_test || error "setup quota failed with $?"
2409         trap cleanup_quota_test EXIT
2410
2411         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
2412         chmod 0777 $DIR/${tdir}-1
2413
2414         set_mdt_qtype "u" || error "enable mdt quota failed"
2415         quota_show_check f u $TSTUSR
2416
2417         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2418                 error "set quota failed"
2419
2420         echo "Create $ilimit files on mdt0..."
2421         $RUNAS createmany -m $TESTFILE0 $ilimit ||
2422                 quota_error u $TSTUSR "create failed, but expect success"
2423
2424         echo "Create files on mdt1..."
2425         $RUNAS createmany -m $TESTFILE1 1 &&
2426                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
2427
2428         echo "Free space from mdt0..."
2429         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
2430         wait_delete_completed
2431         sync_all_data || true
2432
2433         echo "Create files on mdt1 after space freed from mdt0..."
2434         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
2435                 quota_error a $TSTUSR "rebalancing failed"
2436
2437         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
2438                 error "unlink mdt1 files failed"
2439         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2440
2441         cleanup_quota_test
2442 }
2443 run_test 12b "Inode quota rebalancing"
2444
2445 test_13(){
2446         local TESTFILE=$DIR/$tdir/$tfile
2447         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
2448         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
2449
2450         setup_quota_test || error "setup quota failed with $?"
2451         trap cleanup_quota_test EXIT
2452
2453         set_ost_qtype "u" || error "enable ost quota failed"
2454         quota_show_check b u $TSTUSR
2455
2456         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2457                 error "set quota failed"
2458         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2459         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2460
2461         # clear the locks in cache first
2462         do_facet ost1 $LCTL set_param -n $procf=clear
2463         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2464         [ $nlock -eq 0 ] || error "$nlock cached locks"
2465
2466         # write to acquire the per-ID lock
2467         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
2468                 quota_error a $TSTUSR "dd failed"
2469
2470         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2471         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
2472
2473         # clear quota doesn't trigger per-ID lock cancellation
2474         resetquota -u $TSTUSR
2475         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2476         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
2477
2478         # clear the per-ID lock
2479         do_facet ost1 $LCTL set_param -n $procf=clear
2480         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2481         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
2482
2483         # spare quota should be released
2484         local OSTUUID=$(ostuuid_from_index 0)
2485         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2486         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
2487         [ $limit -le $space ] ||
2488                 error "spare quota isn't released, limit:$limit, space:$space"
2489
2490         cleanup_quota_test
2491 }
2492 run_test 13 "Cancel per-ID lock in the LRU list"
2493
2494 test_15(){
2495         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
2496
2497         wait_delete_completed
2498         sync_all_data || true
2499
2500         # test for user
2501         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2502                 error "set user quota failed"
2503         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
2504         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2505                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2506         resetquota -u $TSTUSR
2507
2508         # test for group
2509         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2510                 error "set group quota failed"
2511         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
2512         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2513                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2514         resetquota -g $TSTUSR
2515 }
2516 run_test 15 "Set over 4T block quota"
2517
2518 test_17sub() {
2519         local err_code=$1
2520         local BLKS=1    # 1M less than limit
2521         local TESTFILE=$DIR/$tdir/$tfile
2522
2523         setup_quota_test || error "setup quota failed with $?"
2524         trap cleanup_quota_test EXIT
2525
2526         # make sure the system is clean
2527         local USED=$(getquota -u $TSTUSR global curspace)
2528         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2529
2530         set_ost_qtype "ug" || error "enable ost quota failed"
2531         # make sure no granted quota on ost
2532         resetquota -u $TSTUSR
2533         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2534                 error "set quota failed"
2535
2536         quota_show_check b u $TSTUSR
2537
2538         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
2539         lustre_fail mds 0xa04 $err_code
2540
2541         # write in background
2542         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
2543         local DDPID=$!
2544
2545         sleep 2
2546         # write should be blocked and never finished
2547         if ! ps -p $DDPID  > /dev/null 2>&1; then
2548                 lustre_fail mds 0 0
2549                 quota_error u $TSTUSR "write finished incorrectly!"
2550         fi
2551
2552         lustre_fail mds 0 0
2553
2554         local count=0
2555         local timeout=30
2556         while [ true ]; do
2557                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2558                 count=$((count+1))
2559                 if [ $count -gt $timeout ]; then
2560                         quota_error u $TSTUSR "dd is not finished!"
2561                 fi
2562                 sleep 1
2563         done
2564
2565         sync; sync_all_data || true
2566
2567         USED=$(getquota -u $TSTUSR global curspace)
2568         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
2569                 "Used space(${USED}K) is less than ${BLKS}M"
2570
2571         cleanup_quota_test
2572 }
2573
2574 # DQACQ return recoverable error
2575 test_17() {
2576         echo "DQACQ return -ENOLCK"
2577         #define ENOLCK  37
2578         test_17sub 37 || error "Handle -ENOLCK failed"
2579
2580         echo "DQACQ return -EAGAIN"
2581         #define EAGAIN  11
2582         test_17sub 11 || error "Handle -EAGAIN failed"
2583
2584         echo "DQACQ return -ETIMEDOUT"
2585         #define ETIMEDOUT 110
2586         test_17sub 110 || error "Handle -ETIMEDOUT failed"
2587
2588         echo "DQACQ return -ENOTCONN"
2589         #define ENOTCONN 107
2590         test_17sub 107 || error "Handle -ENOTCONN failed"
2591 }
2592
2593 run_test 17 "DQACQ return recoverable error"
2594
2595 test_18_sub () {
2596         local io_type=$1
2597         local blimit="200m" # 200M
2598         local TESTFILE="$DIR/$tdir/$tfile"
2599
2600         setup_quota_test || error "setup quota failed with $?"
2601         trap cleanup_quota_test EXIT
2602
2603         set_ost_qtype "u" || error "enable ost quota failed"
2604         log "User quota (limit: $blimit)"
2605         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
2606                 error "set quota failed"
2607         quota_show_check b u $TSTUSR
2608
2609         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2610         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2611
2612         local timeout=$(sysctl -n lustre.timeout)
2613
2614         if [ $io_type = "directio" ]; then
2615                 log "Write 100M (directio) ..."
2616                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
2617         else
2618                 log "Write 100M (buffered) ..."
2619                 $RUNAS $DD of=$TESTFILE count=100 &
2620         fi
2621         local DDPID=$!
2622
2623         replay_barrier $SINGLEMDS
2624         log "Fail mds for $((2 * timeout)) seconds"
2625         fail $SINGLEMDS $((2 * timeout))
2626
2627         local count=0
2628         if at_is_enabled; then
2629                 timeout=$(at_max_get mds)
2630         else
2631                 timeout=$(lctl get_param -n timeout)
2632         fi
2633
2634         while [ true ]; do
2635                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2636                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
2637                         log "it took $count second"
2638                 fi
2639                 sleep 1
2640         done
2641
2642         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
2643         sync
2644         cancel_lru_locks mdc
2645         cancel_lru_locks osc
2646         $SHOW_QUOTA_USER
2647
2648         local testfile_size=$(stat -c %s $TESTFILE)
2649         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
2650                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
2651                         "got ${testfile_size}. Verifying file failed!"
2652         fi
2653         cleanup_quota_test
2654 }
2655
2656 # test when mds does failover, the ost still could work well
2657 # this test shouldn't trigger watchdog b=14840
2658 test_18() {
2659         # Clear dmesg so watchdog is not triggered by previous
2660         # test output
2661         do_facet ost1 dmesg -c > /dev/null
2662
2663         test_18_sub normal
2664         test_18_sub directio
2665
2666         # check if watchdog is triggered
2667         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2668         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
2669                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
2670         [ -z "$watchdog" ] || error "$watchdog"
2671         rm -f $TMP/lustre-log-${TESTNAME}.log
2672 }
2673 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
2674
2675 test_19() {
2676         local blimit=5 # 5M
2677         local TESTFILE=$DIR/$tdir/$tfile
2678
2679         setup_quota_test || error "setup quota failed with $?"
2680         trap cleanup_quota_test EXIT
2681
2682         set_ost_qtype $QTYPE || error "enable ost quota failed"
2683
2684         # bind file to a single OST
2685         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2686         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2687
2688         echo "Set user quota (limit: ${blimit}M)"
2689         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2690                 error "set user quota failed"
2691         quota_show_check b u $TSTUSR
2692         echo "Update quota limits"
2693         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2694                 error "set group quota failed"
2695         quota_show_check b u $TSTUSR
2696
2697         # first wirte might be cached
2698         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2699         cancel_lru_locks osc
2700         $SHOW_QUOTA_USER
2701         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2702                 quota_error u $TSTUSR "Write success, expect failure"
2703         $SHOW_QUOTA_USER
2704
2705         cleanup_quota_test
2706 }
2707 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2708
2709 test_20() { # b15754
2710         local LSTR=(2g 1t 4k 3m) # limits strings
2711         # limits values
2712         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2713                     $((3*1024*1024)))
2714
2715         resetquota -u $TSTUSR
2716
2717         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2718                 $MOUNT || error "could not set quota limits"
2719         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2720                                 --inode-softlimit ${LSTR[2]} \
2721                                 --inode-hardlimit ${LSTR[3]} \
2722                                 $MOUNT || error "could not set quota limits"
2723
2724         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2725                 error "bsoftlimit was not set properly"
2726         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2727                 error "bhardlimit was not set properly"
2728         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2729                 error "isoftlimit was not set properly"
2730         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2731                 error "ihardlimit was not set properly"
2732
2733         resetquota -u $TSTUSR
2734 }
2735 run_test 20 "Test if setquota specifiers work properly (b15754)"
2736
2737 test_21_sub() {
2738         local testfile=$1
2739         local blk_number=$2
2740         local seconds=$3
2741
2742         local time=$(($(date +%s) + seconds))
2743         while [ $(date +%s) -lt $time ]; do
2744                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2745         done
2746 }
2747
2748 # run for fixing bug16053, setquota shouldn't fail when writing and
2749 # deleting are happening
2750 test_21() {
2751         local TESTFILE="$DIR/$tdir/$tfile"
2752         local BLIMIT=10 # 10G
2753         local ILIMIT=1000000
2754
2755         setup_quota_test || error "setup quota failed with $?"
2756         trap cleanup_quota_test EXIT
2757
2758         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2759
2760         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2761         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2762                 error "set user quota failed"
2763         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2764         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2765                 error "set group quota failed"
2766         if is_project_quota_supported; then
2767                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2768                         "project: $TSTPRJID"
2769                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2770                          $MOUNT || error "set project quota failed"
2771         fi
2772
2773         # repeat writing on a 1M file
2774         test_21_sub ${TESTFILE}_1 1 30 &
2775         local DDPID1=$!
2776         # repeat writing on a 128M file
2777         test_21_sub ${TESTFILE}_2 128 30 &
2778         local DDPID2=$!
2779
2780         local time=$(($(date +%s) + 30))
2781         local i=1
2782         while [ $(date +%s) -lt $time ]; do
2783                 log "Set quota for $i times"
2784                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2785                         -I $((ILIMIT + i)) $MOUNT ||
2786                                 error "Set user quota failed"
2787                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2788                         -I $((ILIMIT + i)) $MOUNT ||
2789                                 error "Set group quota failed"
2790                 if is_project_quota_supported; then
2791                         $LFS setquota -p $TSTPRJID -b 0 -B \
2792                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2793                                 error "Set project quota failed"
2794                 fi
2795                 i=$((i+1))
2796                 sleep 1
2797         done
2798
2799         local count=0
2800         while [ true ]; do
2801                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2802                 count=$((count+1))
2803                 if [ $count -gt 60 ]; then
2804                         quota_error a $TSTUSR "dd should be finished!"
2805                 fi
2806                 sleep 1
2807         done
2808         echo "(dd_pid=$DDPID1, time=$count)successful"
2809
2810         count=0
2811         while [ true ]; do
2812                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2813                 count=$((count+1))
2814                 if [ $count -gt 60 ]; then
2815                         quota_error a $TSTUSR "dd should be finished!"
2816                 fi
2817                 sleep 1
2818         done
2819         echo "(dd_pid=$DDPID2, time=$count)successful"
2820
2821         cleanup_quota_test
2822 }
2823 run_test 21 "Setquota while writing & deleting (b16053)"
2824
2825 # enable/disable quota enforcement permanently
2826 test_22() {
2827         echo "Set both mdt & ost quota type as ug"
2828         local qtype="ug"
2829         is_project_quota_supported && qtype=$QTYPE
2830         set_mdt_qtype $qtype || error "enable mdt quota failed"
2831         set_ost_qtype $qtype || error "enable ost quota failed"
2832
2833         echo "Restart..."
2834         stopall || error "failed to stopall (1)"
2835         mount
2836         setupall
2837
2838         echo "Verify if quota is enabled"
2839         local qtype1=$(mdt_quota_type)
2840         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2841         qtype=$(ost_quota_type)
2842         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2843
2844         echo "Set both mdt & ost quota type as none"
2845         set_mdt_qtype "none" || error "disable mdt quota failed"
2846         set_ost_qtype "none" || error "disable ost quota failed"
2847
2848         echo "Restart..."
2849         stopall || error "failed to stopall (2)"
2850         mount
2851         setupall
2852         quota_init
2853
2854         echo "Verify if quota is disabled"
2855         qtype=$(mdt_quota_type)
2856         [ $qtype != "none" ] && error "mdt quota setting is lost"
2857         qtype=$(ost_quota_type)
2858         [ $qtype != "none" ] && error "ost quota setting is lost"
2859
2860         return 0
2861 }
2862 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2863
2864 test_23_sub() {
2865         local TESTFILE="$DIR/$tdir/$tfile"
2866         local LIMIT=$1
2867
2868         setup_quota_test || error "setup quota failed with $?"
2869         trap cleanup_quota_test EXIT
2870
2871         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2872
2873         # test for user
2874         log "User quota (limit: $LIMIT MB)"
2875         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2876                 error "set quota failed"
2877         quota_show_check b u $TSTUSR
2878
2879         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2880         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2881
2882         log "Step1: trigger EDQUOT with O_DIRECT"
2883         log "Write half of file"
2884         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2885                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2886                         "limit=$LIMIT"
2887         log "Write out of block quota ..."
2888         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2889                 oflag=direct conv=notrunc &&
2890                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2891                         "limit=$LIMIT"
2892         log "Step1: done"
2893
2894         log "Step2: rewrite should succeed"
2895         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2896                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2897                         "limit=$LIMIT"
2898         log "Step2: done"
2899
2900         cleanup_quota_test
2901
2902         local OST0_UUID=$(ostuuid_from_index 0)
2903         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2904         [ $OST0_QUOTA_USED -ne 0 ] &&
2905                 ($SHOW_QUOTA_USER; \
2906                 quota_error u $TSTUSR "quota isn't released")
2907         $SHOW_QUOTA_USER
2908 }
2909
2910 test_23() {
2911         [ "$ost1_FSTYPE" == zfs ] &&
2912                 skip "Overwrite in place is not guaranteed to be " \
2913                 "space neutral on ZFS"
2914
2915         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2916         check_whether_skip && return 0
2917         log "run for 4MB test file"
2918         test_23_sub 4
2919
2920         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2921         check_whether_skip && return 0
2922         log "run for 40MB test file"
2923         test_23_sub 40
2924 }
2925 run_test 23 "Quota should be honored with directIO (b16125)"
2926
2927 test_24() {
2928         local blimit=5 # 5M
2929         local TESTFILE="$DIR/$tdir/$tfile"
2930
2931         setup_quota_test || error "setup quota failed with $?"
2932         trap cleanup_quota_test EXIT
2933
2934         set_ost_qtype $QTYPE || error "enable ost quota failed"
2935
2936         # bind file to a single OST
2937         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2938         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2939
2940         echo "Set user quota (limit: ${blimit}M)"
2941         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2942                 error "set quota failed"
2943
2944         # overrun quota by root user
2945         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2946                 error "write failure, expect success"
2947         cancel_lru_locks osc
2948         sync_all_data || true
2949
2950         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2951
2952         cleanup_quota_test
2953 }
2954 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2955
2956 test_27a() { # b19612
2957         $LFS quota $TSTUSR $DIR &&
2958                 error "lfs succeeded with no type, but should have failed"
2959         $LFS setquota $TSTUSR $DIR &&
2960                 error "lfs succeeded with no type, but should have failed"
2961         return 0
2962 }
2963 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2964
2965 test_27b() { # b20200
2966         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2967                 error "lfs setquota failed with uid argument"
2968         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2969                 error "lfs stequota failed with gid argument"
2970         if is_project_quota_supported; then
2971                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2972                         1000 $DIR || error \
2973                                 "lfs stequota failed with projid argument"
2974         fi
2975         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2976         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2977         if is_project_quota_supported; then
2978                 $SHOW_QUOTA_PROJID ||
2979                         error "lfs quota failed with projid argument"
2980         fi
2981         resetquota -u $TSTID
2982         resetquota -g $TSTID
2983         resetquota -p $TSTPRJID
2984         return 0
2985 }
2986 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2987
2988 test_27c() {
2989         local limit
2990
2991         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2992                 error "lfs setquota failed"
2993
2994         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2995         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2996         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2997         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2998
2999         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
3000                 error "lfs setquota for $TSTID failed"
3001
3002         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
3003         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
3004         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
3005         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
3006
3007         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
3008                 error "total allocated inode/block limit not printed"
3009
3010         resetquota -u $TSTUSR
3011 }
3012 run_test 27c "lfs quota should support human-readable output"
3013
3014 test_27d() {
3015         local softlimit=1.5
3016         local hardlimit=2.3
3017         local limit
3018
3019         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
3020                 error "set fraction block limit failed"
3021         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
3022         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
3023         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
3024         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
3025
3026         resetquota -u $TSTUSR
3027 }
3028 run_test 27d "lfs setquota should support fraction block limit"
3029
3030 test_30() {
3031         local LIMIT=4 # 4MB
3032         local TESTFILE="$DIR/$tdir/$tfile"
3033         local GRACE=10
3034
3035         setup_quota_test || error "setup quota failed with $?"
3036         trap cleanup_quota_test EXIT
3037
3038         set_ost_qtype "u" || error "enable ost quota failed"
3039
3040         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
3041         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3042
3043         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
3044                 $MAX_IQ_TIME $DIR || error "set grace time failed"
3045         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
3046                 error "set quota failed"
3047         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
3048         cancel_lru_locks osc
3049         sleep $GRACE
3050         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
3051         # over-quota flag has not yet settled since we do not trigger async
3052         # events based on grace time period expiration
3053         $SHOW_QUOTA_USER
3054         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
3055         cancel_lru_locks osc
3056         # now over-quota flag should be settled and further writes should fail
3057         $SHOW_QUOTA_USER
3058         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
3059                 error "grace times were reset"
3060         # cleanup
3061         cleanup_quota_test
3062         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
3063                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
3064 }
3065 run_test 30 "Hard limit updates should not reset grace times"
3066
3067 # basic usage tracking for user & group
3068 test_33() {
3069         local INODES=10 # 10 files
3070         local BLK_CNT=2 # of 2M each
3071         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
3072
3073         setup_quota_test || error "setup quota failed with $?"
3074         trap cleanup_quota_test EXIT
3075
3076         # make sure the system is clean
3077         local USED=$(getquota -u $TSTID global curspace)
3078         [ $USED -ne 0 ] &&
3079                 error "Used space ($USED) for user $TSTID isn't 0."
3080         USED=$(getquota -g $TSTID global curspace)
3081         [ $USED -ne 0 ] &&
3082                 error "Used space ($USED) for group $TSTID isn't 0."
3083         if is_project_quota_supported; then
3084                 USED=$(getquota -p $TSTPRJID global curspace)
3085                 [ $USED -ne 0 ] && error \
3086                         "Used space ($USED) for project $TSTPRJID isn't 0."
3087         fi
3088
3089         echo "Write files..."
3090         for i in $(seq 0 $INODES); do
3091                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
3092                         error "write failed"
3093                         is_project_quota_supported &&
3094                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
3095                 echo "Iteration $i/$INODES completed"
3096         done
3097         cancel_lru_locks osc
3098
3099         echo "Wait for setattr on objects finished..."
3100         wait_delete_completed
3101
3102         sync; sync_all_data || true
3103
3104         echo "Verify disk usage after write"
3105         USED=$(getquota -u $TSTID global curspace)
3106         [ $USED -lt $TOTAL_BLKS ] &&
3107                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
3108         USED=$(getquota -g $TSTID global curspace)
3109         [ $USED -lt $TOTAL_BLKS ] &&
3110                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
3111         if is_project_quota_supported; then
3112                 USED=$(getquota -p $TSTPRJID global curspace)
3113                 [ $USED -lt $TOTAL_BLKS ] && error \
3114                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
3115         fi
3116
3117         echo "Verify inode usage after write"
3118         USED=$(getquota -u $TSTID global curinodes)
3119         [ $USED -lt $INODES ] &&
3120                 error "Used inode for user $TSTID is $USED, expected $INODES"
3121         USED=$(getquota -g $TSTID global curinodes)
3122         [ $USED -lt $INODES ] &&
3123                 error "Used inode for group $TSTID is $USED, expected $INODES"
3124         if is_project_quota_supported; then
3125                 USED=$(getquota -p $TSTPRJID global curinodes)
3126                 [ $USED -lt $INODES ] && error \
3127                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
3128         fi
3129
3130         cleanup_quota_test
3131
3132         echo "Verify disk usage after delete"
3133         USED=$(getquota -u $TSTID global curspace)
3134         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
3135         USED=$(getquota -u $TSTID global curinodes)
3136         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
3137         USED=$(getquota -g $TSTID global curspace)
3138         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
3139         USED=$(getquota -g $TSTID global curinodes)
3140         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
3141         if is_project_quota_supported; then
3142                 USED=$(getquota -p $TSTPRJID global curspace)
3143                 [ $USED -eq 0 ] ||
3144                         error "Used space for project $TSTPRJID isn't 0. $USED"
3145                 USED=$(getquota -p $TSTPRJID global curinodes)
3146                 [ $USED -eq 0 ] ||
3147                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
3148         fi
3149 }
3150 run_test 33 "Basic usage tracking for user & group & project"
3151
3152 # usage transfer test for user & group & project
3153 test_34() {
3154         local BLK_CNT=2 # 2MB
3155         local project_supported="no"
3156
3157         is_project_quota_supported && project_supported="yes"
3158         setup_quota_test || error "setup quota failed with $?"
3159         trap cleanup_quota_test EXIT
3160
3161         # make sure the system is clean
3162         local USED=$(getquota -u $TSTID global curspace)
3163         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
3164         USED=$(getquota -g $TSTID global curspace)
3165         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
3166
3167         local USED=$(getquota -u $TSTID2 global curspace)
3168         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
3169         if [ $project_supported == "yes" ]; then
3170                 USED=$(getquota -p $TSTPRJID global curspace)
3171                 [ $USED -ne 0 ] && error \
3172                         "Used space ($USED) for Project $TSTPRJID isn't 0."
3173         fi
3174
3175         echo "Write file..."
3176         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3177                 error "write failed"
3178         cancel_lru_locks osc
3179         sync; sync_all_data || true
3180
3181         echo "chown the file to user $TSTID"
3182         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
3183
3184         echo "Wait for setattr on objects finished..."
3185         wait_delete_completed
3186
3187         BLK_CNT=$((BLK_CNT * 1024))
3188
3189         echo "Verify disk usage for user $TSTID"
3190         USED=$(getquota -u $TSTID global curspace)
3191         [ $USED -lt $BLK_CNT ] &&
3192                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
3193         USED=$(getquota -u $TSTID global curinodes)
3194         [ $USED -ne 1 ] &&
3195                 error "Used inodes for user $TSTID is $USED, expected 1"
3196
3197         echo "chgrp the file to group $TSTID"
3198         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
3199
3200         echo "Wait for setattr on objects finished..."
3201         wait_delete_completed
3202
3203         echo "Verify disk usage for group $TSTID"
3204         USED=$(getquota -g $TSTID global curspace)
3205         [ $USED -ge $BLK_CNT ] ||
3206                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3207         USED=$(getquota -g $TSTID global curinodes)
3208         [ $USED -eq 1 ] ||
3209                 error "Used inodes for group $TSTID is $USED, expected 1"
3210
3211         # chown won't change the ost object group. LU-4345 */
3212         echo "chown the file to user $TSTID2"
3213         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
3214
3215         echo "Wait for setattr on objects finished..."
3216         wait_delete_completed
3217
3218         echo "change_project project id to $TSTPRJID"
3219         [ $project_supported == "yes" ] &&
3220                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3221         echo "Wait for setattr on objects finished..."
3222         wait_delete_completed
3223
3224         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
3225         USED=$(getquota -u $TSTID2 global curspace)
3226         [ $USED -lt $BLK_CNT ] &&
3227                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
3228         USED=$(getquota -u $TSTID global curspace)
3229         [ $USED -ne 0 ] &&
3230                 error "Used space for user $TSTID is $USED, expected 0"
3231         USED=$(getquota -g $TSTID global curspace)
3232         [ $USED -lt $BLK_CNT ] &&
3233                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3234         if [ $project_supported == "yes" ]; then
3235                 USED=$(getquota -p $TSTPRJID global curspace)
3236                 [ $USED -lt $BLK_CNT ] && error \
3237                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
3238         fi
3239
3240         cleanup_quota_test
3241 }
3242 run_test 34 "Usage transfer for user & group & project"
3243
3244 # usage is still accessible across restart
3245 test_35() {
3246         local BLK_CNT=2 # 2 MB
3247
3248         setup_quota_test || error "setup quota failed with $?"
3249         trap cleanup_quota_test EXIT
3250
3251         echo "Write file..."
3252         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3253                 error "write failed"
3254         is_project_quota_supported &&
3255                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3256         cancel_lru_locks osc
3257
3258         echo "Wait for setattr on objects finished..."
3259         wait_delete_completed
3260
3261         sync; sync_all_data || true
3262
3263         echo "Save disk usage before restart"
3264         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
3265         [ $ORIG_USR_SPACE -eq 0 ] &&
3266                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
3267         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
3268         [ $ORIG_USR_INODES -eq 0 ] &&
3269                 error "Used inodes for user $TSTID is 0, expected 1"
3270         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
3271         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
3272         [ $ORIG_GRP_SPACE -eq 0 ] &&
3273                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
3274         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
3275         [ $ORIG_GRP_INODES -eq 0 ] &&
3276                 error "Used inodes for group $TSTID is 0, expected 1"
3277         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
3278
3279         if is_project_quota_supported; then
3280                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
3281                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
3282                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
3283                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
3284                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
3285                         "Used inodes for project $TSTPRJID is 0, expected 1"
3286                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
3287         fi
3288
3289         log "Restart..."
3290         stopall
3291         setupall
3292         quota_init
3293
3294         echo "Verify disk usage after restart"
3295         local USED=$(getquota -u $TSTID global curspace)
3296         [ $USED -eq $ORIG_USR_SPACE ] ||
3297                 error "Used space for user $TSTID changed from " \
3298                         "$ORIG_USR_SPACE to $USED"
3299         USED=$(getquota -u $TSTID global curinodes)
3300         [ $USED -eq $ORIG_USR_INODES ] ||
3301                 error "Used inodes for user $TSTID changed from " \
3302                         "$ORIG_USR_INODES to $USED"
3303         USED=$(getquota -g $TSTID global curspace)
3304         [ $USED -eq $ORIG_GRP_SPACE ] ||
3305                 error "Used space for group $TSTID changed from " \
3306                         "$ORIG_GRP_SPACE to $USED"
3307         USED=$(getquota -g $TSTID global curinodes)
3308         [ $USED -eq $ORIG_GRP_INODES ] ||
3309                 error "Used inodes for group $TSTID changed from " \
3310                         "$ORIG_GRP_INODES to $USED"
3311         if [ $project_supported == "yes" ]; then
3312                 USED=$(getquota -p $TSTPRJID global curinodes)
3313                 [ $USED -eq $ORIG_PRJ_INODES ] ||
3314                         error "Used inodes for project $TSTPRJID " \
3315                                 "changed from $ORIG_PRJ_INODES to $USED"
3316                 USED=$(getquota -p $TSTPRJID global curspace)
3317                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
3318                         error "Used space for project $TSTPRJID "\
3319                                 "changed from $ORIG_PRJ_SPACE to $USED"
3320         fi
3321
3322         # check if the vfs_dq_init() is called before writing
3323         echo "Append to the same file..."
3324         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
3325                 error "write failed"
3326         cancel_lru_locks osc
3327         sync; sync_all_data || true
3328
3329         echo "Verify space usage is increased"
3330         USED=$(getquota -u $TSTID global curspace)
3331         [ $USED -gt $ORIG_USR_SPACE ] ||
3332                 error "Used space for user $TSTID isn't increased" \
3333                         "orig:$ORIG_USR_SPACE, now:$USED"
3334         USED=$(getquota -g $TSTID global curspace)
3335         [ $USED -gt $ORIG_GRP_SPACE ] ||
3336                 error "Used space for group $TSTID isn't increased" \
3337                         "orig:$ORIG_GRP_SPACE, now:$USED"
3338         if [ $project_supported == "yes" ]; then
3339                 USED=$(getquota -p $TSTPRJID global curspace)
3340                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
3341                         error "Used space for project $TSTPRJID isn't " \
3342                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
3343         fi
3344
3345         cleanup_quota_test
3346 }
3347 run_test 35 "Usage is still accessible across reboot"
3348
3349 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
3350 # LU-5006
3351 test_37() {
3352         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
3353                 skip "Old server doesn't have LU-5006 fix."
3354
3355         setup_quota_test || error "setup quota failed with $?"
3356         trap cleanup_quota_test EXIT
3357
3358         # make sure the system is clean
3359         local USED=$(getquota -u $TSTID global curspace)
3360         [ $USED -ne 0 ] &&
3361                 error "Used space ($USED) for user $TSTID isn't 0."
3362
3363         # create file with MDS_OPEN_DELAY_CREATE flag
3364         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
3365                 error "Create file failed"
3366         # write to file
3367         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
3368                 oflag=sync || error "Write file failed"
3369         # chown to the file
3370         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
3371
3372         # wait for setattr on objects finished..."
3373         wait_delete_completed
3374
3375         USED=$(getquota -u $TSTID global curspace)
3376         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
3377
3378         cleanup_quota_test
3379 }
3380 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
3381
3382 # LU-8801
3383 test_38() {
3384         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
3385                 skip "Old server doesn't have LU-8801 fix."
3386
3387         [ "$UID" != 0 ] && skip_env "must run as root" && return
3388
3389         setup_quota_test || error "setup quota failed with $?"
3390         trap cleanup_quota_test EXIT
3391
3392         # make sure the system is clean
3393         local USED=$(getquota -u $TSTID global curspace)
3394         [ $USED -ne 0 ] &&
3395                 error "Used space ($USED) for user $TSTID isn't 0."
3396         USED=$(getquota -u $TSTID2 global curspace)
3397         [ $USED -ne 0 ] &&
3398                 error "Used space ($USED) for user $TSTID2 isn't 0."
3399
3400         local TESTFILE="$DIR/$tdir/$tfile"
3401         local file_cnt=10000
3402
3403         # Generate id entries in accounting file
3404         echo "Create $file_cnt files..."
3405         for i in `seq $file_cnt`; do
3406                 touch $TESTFILE-$i
3407                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
3408                         error "failed to chown $TESTFILE-$i"
3409         done
3410         cancel_lru_locks osc
3411         sync; sync_all_data || true
3412
3413         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
3414         procf=${procf}.quota_slave.acct_user
3415         local accnt_cnt
3416
3417         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
3418                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
3419         echo "Found $acct_cnt id entries"
3420
3421         [ $file_cnt -eq $acct_cnt ] || {
3422                 do_facet mds1 $LCTL get_param $procf
3423                 error "skipped id entries"
3424         }
3425
3426         cleanup_quota_test
3427 }
3428 run_test 38 "Quota accounting iterator doesn't skip id entries"
3429
3430 test_39() {
3431         local TESTFILE="$DIR/$tdir/project"
3432         ! is_project_quota_supported &&
3433                 skip "Project quota is not supported"
3434
3435         setup_quota_test || error "setup quota failed with $?"
3436
3437         touch $TESTFILE
3438         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3439         [ $projectid -ne 0 ] &&
3440                 error "Project id should be 0 not $projectid"
3441         change_project -p 1024 $TESTFILE
3442         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3443         [ $projectid -ne 1024 ] &&
3444                 error "Project id should be 1024 not $projectid"
3445
3446         stopall || error "failed to stopall (1)"
3447         mount
3448         setupall
3449         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3450         [ $projectid -ne 1024 ] &&
3451                 error "Project id should be 1024 not $projectid"
3452
3453         cleanup_quota_test
3454 }
3455 run_test 39 "Project ID interface works correctly"
3456
3457 test_40a() {
3458         ! is_project_quota_supported &&
3459                 skip "Project quota is not supported"
3460         local dir1="$DIR/$tdir/dir1"
3461         local dir2="$DIR/$tdir/dir2"
3462
3463         setup_quota_test || error "setup quota failed with $?"
3464
3465         mkdir -p $dir1 $dir2
3466         change_project -sp 1 $dir1 && touch $dir1/1
3467         change_project -sp 2 $dir2
3468
3469         ln $dir1/1 $dir2/1_link &&
3470                 error "Hard link across different project quota should fail"
3471         rm -rf $dir1 $dir2
3472
3473         cleanup_quota_test
3474 }
3475 run_test 40a "Hard link across different project ID"
3476
3477 test_40b() {
3478         ! is_project_quota_supported &&
3479                 skip "Project quota is not supported"
3480         local dir1="$DIR/$tdir/dir1"
3481         local dir2="$DIR/$tdir/dir2"
3482
3483         setup_quota_test || error "setup quota failed with $?"
3484         mkdir -p $dir1 $dir2
3485         change_project -sp 1 $dir1 && touch $dir1/1
3486         change_project -sp 2 $dir2
3487
3488         mv $dir1/1 $dir2/2 || error "mv failed $?"
3489         local projid=$(lfs project $dir2/2 | awk '{print $1}')
3490         if [ "$projid" != "2" ]; then
3491                 error "project id expected 2 not $projid"
3492         fi
3493         rm -rf $dir1 $dir2
3494         cleanup_quota_test
3495 }
3496 run_test 40b "Mv across different project ID"
3497
3498 test_40c() {
3499         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
3500                 ! is_project_quota_supported &&
3501                         skip "Project quota is not supported"
3502
3503         setup_quota_test || error "setup quota failed with $?"
3504         local dir="$DIR/$tdir/dir"
3505
3506         mkdir -p $dir && change_project -sp 1 $dir
3507         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
3508         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
3509         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
3510         touch $dir/remote_dir/file
3511         #verify inherit works file for remote dir.
3512         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
3513         [ "$projid" != "1" ] &&
3514                 error "file under remote dir expected 1 not $projid"
3515
3516         #Agent inode should be ignored for project quota
3517         USED=$(getquota -p 1 global curinodes)
3518         [ "$USED" != "3" ] &&
3519                 error "file count expected 3 got $USED"
3520
3521         rm -rf $dir
3522         cleanup_quota_test
3523         return 0
3524 }
3525 run_test 40c "Remote child Dir inherit project quota properly"
3526
3527 test_40d() {
3528         [ "$MDSCOUNT" -lt "2" ] && skip_env "needs >= 2 MDTs"
3529         is_project_quota_supported || skip "Project quota is not supported"
3530
3531         setup_quota_test || error "setup quota failed with $?"
3532         local dir="$DIR/$tdir/dir"
3533
3534         mkdir -p $dir
3535         $LFS setdirstripe -D -c 2 -i -1 $dir || error "setdirstripe failed"
3536         change_project -sp $TSTPRJID $dir ||
3537                 error "change project on $dir failed"
3538         for i in $(seq 5); do
3539                 mkdir -p $dir/d$i/d$i ||
3540                         error "mkdir $dir/d$i/d$i failed"
3541                 local projid=$($LFS project -d $dir/d$i/d$i |
3542                                awk '{print $1}')
3543                 [ "$projid" == "$TSTPRJID" ] ||
3544                         error "projid id expected $TSTPRJID not $projid"
3545                 touch $dir/d$i/d$i/file
3546                 #verify inherit works file for stripe dir.
3547                 local projid=$($LFS project -d $dir/d$i/d$i/file | awk '{print $1}')
3548                 [ "$projid" == "$TSTPRJID" ] ||
3549                         error "file under remote dir expected 1 not $projid"
3550         done
3551
3552         # account should be 1 + (2 + 1) *10 + 1 * 5
3553         USED=$(getquota -p $TSTPRJID global curinodes)
3554         [ "$USED" == "36" ] ||
3555                 error "file count expected 36 got $USED"
3556
3557         rm -rf $dir
3558         cleanup_quota_test
3559 }
3560 run_test 40d "Stripe Directory inherit project quota properly"
3561
3562 test_41() {
3563         is_project_quota_supported ||
3564                 skip "Project quota is not supported"
3565         setup_quota_test || error "setup quota failed with $?"
3566         trap cleanup_quota_test EXIT
3567         local dir="$DIR/$tdir/dir"
3568         local blimit=102400
3569         local ilimit=4096
3570         local projid=$((testnum * 1000))
3571
3572         quota_init
3573
3574         # enable mdt/ost quota
3575         set_mdt_qtype ugp || error "enable mdt quota failed"
3576         set_ost_qtype ugp || error "enable ost quota failed"
3577
3578         test_mkdir -p $dir && change_project -sp $projid $dir
3579         $LFS setquota -p $projid -b 0 -B ${blimit}K -i 0 -I $ilimit $dir ||
3580                 error "set project quota failed"
3581
3582         sync; sync_all_data
3583         sleep_maxage
3584
3585         # check if df output works as expected
3586         echo "== global statfs: $MOUNT =="
3587         df -kP $MOUNT; df -iP $MOUNT; $LFS quota -p $projid $dir
3588         echo
3589         echo "== project statfs (prjid=$projid): $dir =="
3590         df -kP $dir; df -iP $dir
3591         local bused=$(getquota -p $projid global curspace)
3592         local iused=$(getquota -p $projid global curinodes)
3593         # note trailing space to match double printf from awk
3594         local expected="$blimit $bused $ilimit $iused "
3595
3596         wait_update $HOSTNAME \
3597                 "{ df -kP $dir; df -iP $dir; } |
3598                  awk '/$FSNAME/ { printf \\\"%d %d \\\", \\\$2,\\\$3 }'" \
3599                 "$expected" ||
3600                 error "failed to get correct statfs for project quota"
3601
3602         cleanup_quota_test
3603 }
3604 run_test 41 "df should return projid-specific values"
3605
3606 test_50() {
3607         ! is_project_quota_supported &&
3608                 skip "Project quota is not supported"
3609
3610         setup_quota_test || error "setup quota failed with $?"
3611         local dir1="$DIR/$tdir/dir1"
3612         local dir2="$DIR/$tdir/dir2"
3613
3614         mkdir -p $dir1 && change_project -sp 1 $dir1
3615         mkdir -p $dir2 && change_project -sp 2 $dir2
3616         for num in $(seq 1 10); do
3617                 touch $dir1/file_$num $dir2/file_$num
3618                 ln -s $dir1/file_$num $dir1/file_$num"_link"
3619                 ln -s $dir2/file_$num $dir2/file_$num"_link"
3620         done
3621
3622         count=$($LFS find --projid 1 $DIR | wc -l)
3623         [ "$count" != 21 ] && error "expected 21 but got $count"
3624
3625         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
3626         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
3627         [ "$count" != 22 ] && error "expected 22 but got $count"
3628
3629         rm -rf $dir1 $dir2
3630         cleanup_quota_test
3631 }
3632 run_test 50 "Test if lfs find --projid works"
3633
3634 test_51() {
3635         ! is_project_quota_supported &&
3636                 skip "Project quota is not supported"
3637         setup_quota_test || error "setup quota failed with $?"
3638         local dir="$DIR/$tdir/dir"
3639
3640         mkdir $dir && change_project -sp 1 $dir
3641         local used=$(getquota -p 1 global curinodes)
3642         [ $used != "1" ] && error "expected 1 got $used"
3643
3644         touch $dir/1
3645         touch $dir/2
3646         cp $dir/2 $dir/3
3647         used=$(getquota -p 1 global curinodes)
3648         [ $used != "4" ] && error "expected 4 got $used"
3649
3650         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
3651         #try cp to dir
3652         cp $DIR/$tdir/6 $dir/6
3653         used=$(getquota -p 1 global curinodes)
3654         [ $used != "5" ] && error "expected 5 got $used"
3655
3656         #try mv to dir
3657         mv $DIR/$tdir/6 $dir/7
3658         used=$(getquota -p 1 global curinodes)
3659         [ $used != "6" ] && error "expected 6 got $used"
3660
3661         rm -rf $dir
3662         cleanup_quota_test
3663 }
3664 run_test 51 "Test project accounting with mv/cp"
3665
3666 test_52() {
3667         ! is_project_quota_supported &&
3668                 skip "Project quota is not supported"
3669         setup_quota_test || error "setup quota failed with $?"
3670         local dir="$DIR/$tdir/dir"
3671         mkdir $dir && change_project -sp 1 $dir
3672
3673         touch $DIR/$tdir/file
3674         #Try renaming a file into the project.  This should fail.
3675         for num in $(seq 1 2000); do
3676                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
3677                         error "rename should fail"
3678         done
3679         rm -rf $dir
3680         cleanup_quota_test
3681 }
3682 run_test 52 "Rename across different project ID"
3683
3684 test_53() {
3685         ! is_project_quota_supported &&
3686                 skip "Project quota is not supported"
3687         setup_quota_test || error "setup quota failed with $?"
3688         local dir="$DIR/$tdir/dir"
3689         mkdir $dir && change_project -s $dir
3690         lfs project -d $dir | grep P || error "inherit attribute should be set"
3691
3692         change_project -C $dir
3693         lfs project -d $dir | grep P &&
3694                 error "inherit attribute should be cleared"
3695
3696         rm -rf $dir
3697         cleanup_quota_test
3698 }
3699 run_test 53 "Project inherit attribute could be cleared"
3700
3701 test_54() {
3702         ! is_project_quota_supported &&
3703                 skip "Project quota is not supported"
3704         setup_quota_test || error "setup quota failed with $?"
3705         trap cleanup_quota_test EXIT
3706         local testfile="$DIR/$tdir/$tfile-0"
3707
3708         #set project ID/inherit attribute
3709         change_project -sp $TSTPRJID $DIR/$tdir
3710         $RUNAS createmany -m ${testfile} 100 ||
3711                 error "create many files failed"
3712
3713         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
3714         # one more count for directory itself */
3715         ((proj_count++))
3716
3717         #check project
3718         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3719         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
3720
3721         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
3722         [ $proj_count1 -eq $proj_count ] ||
3723                         error "c2: expected $proj_count got $proj_count1"
3724
3725         #clear project but with kept projid
3726         change_project -rCk $DIR/$tdir
3727         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3728         [ $proj_count1 -eq 1 ] ||
3729                         error "c3: expected 1 got $proj_count1"
3730
3731         #verify projid untouched.
3732         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3733         ((proj_count1++))
3734         [ $proj_count1 -eq $proj_count ] ||
3735                         error "c4: expected $proj_count got $proj_count1"
3736
3737         # test -0 option
3738         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
3739         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3740         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
3741
3742         #this time clear all
3743         change_project -rC $DIR/$tdir
3744         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3745         [ $proj_count1 -eq 0 ] ||
3746                         error "c6: expected 0 got $proj_count1"
3747         #cleanup
3748         unlinkmany ${testfile} 100 ||
3749                 error "unlink many files failed"
3750
3751         cleanup_quota_test
3752 }
3753 run_test 54 "basic lfs project interface test"
3754
3755 test_55() {
3756         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
3757                 skip "Not supported before 2.10.58."
3758         setup_quota_test || error "setup quota failed with $?"
3759
3760         set_ost_qtype $QTYPE || error "enable ost quota failed"
3761         quota_init
3762
3763         #add second group to TSTUSR
3764         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
3765
3766         #prepare test file
3767         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3768         error "failed to dd"
3769
3770         cancel_lru_locks osc
3771         sync; sync_all_data || true
3772
3773         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3774         error "failed to setquota on group $TSTUSR2"
3775
3776         $LFS quota -v -g $TSTUSR2 $DIR
3777
3778         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3779         error "chgrp should failed with -EDQUOT"
3780
3781         USED=$(getquota -g $TSTUSR2 global curspace)
3782         echo "$USED"
3783
3784         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3785         error "failed to setquota on group $TSTUSR2"
3786
3787         $LFS quota -v -g $TSTUSR2 $DIR
3788
3789         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3790         error "chgrp should succeed"
3791
3792         $LFS quota -v -g $TSTUSR2 $DIR
3793
3794         cleanup_quota_test
3795 }
3796 run_test 55 "Chgrp should be affected by group quota"
3797
3798 test_56() {
3799         setup_quota_test || error "setup quota failed with $?"
3800
3801         set_ost_qtype $QTYPE || error "enable ost quota failed"
3802         quota_init
3803
3804         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3805                 erro "failed to set grace time for usr quota"
3806         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3807                      awk '{print $4 $8}')
3808         if [ "x$grace_time" != "x10s;10s" ]; then
3809                 $LFS quota -t -u $DIR
3810                 error "expected grace time: 10s;10s, got:$grace_time"
3811         fi
3812
3813         cleanup_quota_test
3814 }
3815 run_test 56 "lfs quota -t should work well"
3816
3817 test_57() {
3818         setup_quota_test || error "setup quota failed with $?"
3819
3820         local dir="$DIR/$tdir/dir"
3821         mkdir -p $dir
3822         mkfifo $dir/pipe
3823         #try to change pipe file should not hang and return failure
3824         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3825                 awk -F ':' '{ print \\\$2 }'" \
3826                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3827         #command can process further if it hit some errors
3828         touch $dir/aaa $dir/bbb
3829         mkdir $dir/subdir -p
3830         touch $dir/subdir/aaa $dir/subdir/bbb
3831         #create one invalid link file
3832         ln -s $dir/not_exist_file $dir/ccc
3833         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3834         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3835
3836         cleanup_quota_test
3837 }
3838 run_test 57 "lfs project could tolerate errors"
3839
3840 test_59() {
3841         [ "$mds1_FSTYPE" != ldiskfs ] &&
3842                 skip "ldiskfs only test"
3843         disable_project_quota
3844         setup_quota_test || error "setup quota failed with $?"
3845         quota_init
3846
3847         local testfile="$DIR/$tdir/$tfile-0"
3848         #make sure it did not crash kernel
3849         touch $testfile && lfs project -sp 1 $testfile
3850
3851         enable_project_quota
3852         cleanup_quota_test
3853 }
3854 run_test 59 "lfs project dosen't crash kernel with project disabled"
3855
3856 test_60() {
3857         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3858                 skip "Needs MDS version 2.11.53 or later."
3859         setup_quota_test || error "setup quota failed with $?"
3860         trap cleanup_quota_test EXIT
3861
3862         local testfile=$DIR/$tdir/$tfile
3863         local limit=100
3864
3865         set_mdt_qtype "ug" || error "enable mdt quota failed"
3866
3867         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3868                 error "set quota failed"
3869         quota_show_check a g $TSTUSR
3870
3871         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3872         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3873         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3874                 error "create many files failed"
3875
3876         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3877
3878         # root user can overrun quota
3879         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3880                 error "root user should succeed"
3881
3882         cleanup_quota_test
3883 }
3884 run_test 60 "Test quota for root with setgid"
3885
3886 # test default quota
3887 test_default_quota() {
3888         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3889                 skip "Not supported before 2.11.51."
3890
3891         local qtype=$1
3892         local qpool=$2
3893         local qid=$TSTUSR
3894         local qprjid=$TSTPRJID
3895         local qdtype="-U"
3896         local qs="-b"
3897         local qh="-B"
3898         local LIMIT=20480 #20M disk space
3899         local TESTFILE="$DIR/$tdir/$tfile-0"
3900
3901         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3902                 echo "Project quota is not supported" && return 0
3903
3904         [ $qtype == "-u" ] && qdtype="-U"
3905         [ $qtype == "-g" ] && qdtype="-G"
3906         [ $qtype == "-p" ] && {
3907                 qdtype="-P"
3908                 qid=$qprjid
3909         }
3910
3911         [ $qpool == "meta" ] && {
3912                 LIMIT=10240 #10K inodes
3913                 qs="-i"
3914                 qh="-I"
3915         }
3916
3917         setup_quota_test || error "setup quota failed with $?"
3918         trap cleanup_quota_test EXIT
3919
3920         quota_init
3921
3922         # enable mdt/ost quota
3923         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3924         set_ost_qtype $QTYPE || error "enable ost quota failed"
3925
3926         log "set to use default quota"
3927         $LFS setquota $qtype $qid -d $DIR ||
3928                 error "set $qid to use default quota failed"
3929
3930         log "set default quota"
3931         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3932                 error "set $qid default quota failed"
3933
3934         log "get default quota"
3935         $LFS quota $qdtype $DIR || error "get default quota failed"
3936
3937         if [ $qpool == "data" ]; then
3938                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3939                                                         awk '{print $2}')
3940                 [ $SLIMIT -eq $LIMIT ] ||
3941                         error "the returned default quota is wrong"
3942         else
3943                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3944                                                         awk '{print $5}')
3945                 [ $SLIMIT -eq $LIMIT ] ||
3946                         error "the returned default quota is wrong"
3947         fi
3948
3949         # make sure the system is clean
3950         local USED=$(getquota $qtype $qid global curspace)
3951         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3952
3953         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3954         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3955
3956         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3957
3958         log "Test not out of quota"
3959         if [ $qpool == "data" ]; then
3960                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3961                         quota_error $qtype $qid "write failed, expect succeed"
3962         else
3963                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3964                         quota_error $qtype $qid "create failed, expect succeed"
3965
3966                 unlinkmany $TESTFILE $((LIMIT/2))
3967         fi
3968
3969         log "Test out of quota"
3970         # flush cache, ensure noquota flag is set on client
3971         cancel_lru_locks osc
3972         cancel_lru_locks mdc
3973         sync; sync_all_data || true
3974         if [ $qpool == "data" ]; then
3975                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3976                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3977         else
3978                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3979                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3980
3981                 unlinkmany $TESTFILE $((LIMIT*2))
3982         fi
3983
3984         log "Increase default quota"
3985         # increase default quota
3986         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3987                 error "set default quota failed"
3988
3989         cancel_lru_locks osc
3990         cancel_lru_locks mdc
3991         sync; sync_all_data || true
3992         if [ $qpool == "data" ]; then
3993                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3994                         quota_error $qtype $qid "write failed, expect succeed"
3995         else
3996                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3997                         quota_error $qtype $qid "create failed, expect succeed"
3998
3999                 unlinkmany $TESTFILE $((LIMIT*2))
4000         fi
4001
4002         log "Set quota to override default quota"
4003         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
4004                 error "set $qid quota failed"
4005
4006         cancel_lru_locks osc
4007         cancel_lru_locks mdc
4008         sync; sync_all_data || true
4009         if [ $qpool == "data" ]; then
4010                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
4011                         quota_error $qtype $qid "write succeed, expect EQUOT"
4012         else
4013                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
4014                         quota_error $qtype $qid "create succeed, expect EQUOT"
4015
4016                 unlinkmany $TESTFILE $((LIMIT*2))
4017         fi
4018
4019         log "Set to use default quota again"
4020         $LFS setquota $qtype $qid -d $DIR ||
4021                 error "set $qid to use default quota failed"
4022
4023         cancel_lru_locks osc
4024         cancel_lru_locks mdc
4025         sync; sync_all_data || true
4026         if [ $qpool == "data" ]; then
4027                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4028                         quota_error $qtype $qid "write failed, expect succeed"
4029         else
4030                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4031                         quota_error $qtype $qid "create failed, expect succeed"
4032
4033                 unlinkmany $TESTFILE $((LIMIT*2))
4034         fi
4035
4036         log "Cleanup"
4037         rm -f $TESTFILE
4038         wait_delete_completed || error "wait_delete_completed failed"
4039         sync_all_data || true
4040         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
4041                 error "reset default quota failed"
4042         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
4043                 error "reset quota failed"
4044
4045         cleanup_quota_test
4046 }
4047
4048 test_61() {
4049         test_default_quota "-u" "data"
4050         test_default_quota "-u" "meta"
4051         test_default_quota "-g" "data"
4052         test_default_quota "-g" "meta"
4053         test_default_quota "-p" "data"
4054         test_default_quota "-p" "meta"
4055 }
4056 run_test 61 "default quota tests"
4057
4058 test_62() {
4059         ! is_project_quota_supported &&
4060                 skip "Project quota is not supported"
4061          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
4062                 skip "chattr did not support project quota"
4063         setup_quota_test || error "setup quota failed with $?"
4064         local testdir=$DIR/$tdir/
4065
4066         $RUNAS mkdir -p $testdir || error "failed to mkdir"
4067         change_project -s $testdir
4068         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4069                 error "inherit attribute should be set"
4070         # chattr used FS_IOC_SETFLAGS ioctl
4071         $RUNAS chattr -P $testdir &&
4072                 error "regular user clear inherit should fail"
4073         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4074                 error "inherit attribute should still be set"
4075         chattr -P $testdir || error "root failed to clear inherit"
4076         [[ $($LFS project -d $testdir) =~ "P" ]] &&
4077                 error "inherit attribute should be cleared"
4078         cleanup_quota_test
4079 }
4080 run_test 62 "Project inherit should be only changed by root"
4081
4082 test_dom() {
4083         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
4084                 skip "Not supported before 2.11.55"
4085
4086         local qtype=$1
4087         local qid=$TSTUSR
4088         local dd_failed=false
4089         local tdir_dom=${tdir}_dom
4090         local LIMIT=20480 #20M
4091
4092         [ $qtype == "p" ] && ! is_project_quota_supported &&
4093                 echo "Project quota is not supported" && return 0
4094
4095         [ $qtype == "p" ] && qid=$TSTPRJID
4096
4097         setup_quota_test || error "setup quota failed with $?"
4098         trap cleanup_quota_test EXIT
4099
4100         quota_init
4101
4102         # enable mdt/ost quota
4103         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4104         set_ost_qtype $QTYPE || error "enable ost quota failed"
4105
4106         # make sure the system is clean
4107         local USED=$(getquota -$qtype $qid global curspace)
4108         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4109
4110         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
4111
4112         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
4113         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
4114                 error "setstripe $tdir_dom failed"
4115         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
4116
4117         [ $qtype == "p" ] && {
4118                 change_project -sp $TSTPRJID $DIR/$tdir
4119                 change_project -sp $TSTPRJID $DIR/$tdir_dom
4120         }
4121
4122         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
4123                 error "set $qid quota failed"
4124
4125         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4126                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4127                                                                 dd_failed=true
4128         done
4129
4130         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4131
4132         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
4133                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4134                                                                 dd_failed=true
4135         done
4136
4137         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4138
4139         rm -f $DIR/$tdir_dom/*
4140
4141         # flush cache, ensure noquota flag is set on client
4142         cancel_lru_locks osc
4143         cancel_lru_locks mdc
4144         sync; sync_all_data || true
4145
4146         dd_failed=false
4147
4148         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4149                 quota_error $qtype $qid "write failed, expect succeed"
4150
4151         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4152                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4153                                                                 dd_failed=true
4154         done
4155
4156         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4157
4158         rm -f $DIR/$tdir/*
4159         rm -f $DIR/$tdir_dom/*
4160
4161         # flush cache, ensure noquota flag is set on client
4162         cancel_lru_locks osc
4163         cancel_lru_locks mdc
4164         sync; sync_all_data || true
4165
4166         dd_failed=false
4167
4168         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4169                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4170                                                                 dd_failed=true
4171         done
4172
4173         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4174
4175         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4176                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4177
4178         rm -f $DIR/$tdir/*
4179         rm -fr $DIR/$tdir_dom
4180
4181         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4182                 error "reset usr quota failed"
4183
4184         cleanup_quota_test
4185 }
4186
4187 test_63() {
4188         test_dom "u"
4189         test_dom "g"
4190         test_dom "p"
4191 }
4192 run_test 63 "quota on DoM tests"
4193
4194 test_64() {
4195         ! is_project_quota_supported &&
4196                 skip "Project quota is not supported"
4197         setup_quota_test || error "setup quota failed with $?"
4198         local dir1="$DIR/$tdir/"
4199
4200         touch $dir1/file
4201         ln -s $dir1/file $dir1/file_link
4202
4203         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
4204                 error "set symlink file's project should fail"
4205
4206         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
4207                 error "get symlink file's project should fail"
4208
4209         cleanup_quota_test
4210 }
4211 run_test 64 "lfs project on symlink files should fail"
4212
4213 test_65() {
4214         local SIZE=10 #10M
4215         local TESTFILE="$DIR/$tdir/$tfile-0"
4216
4217         setup_quota_test || error "setup quota failed with $?"
4218         set_ost_qtype $QTYPE || error "enable ost quota failed"
4219         quota_init
4220
4221         echo "Write..."
4222         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4223                 error "failed to write"
4224         # flush cache, ensure noquota flag is set on client
4225         cancel_lru_locks osc
4226         sync; sync_all_data || true
4227
4228         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4229         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4230         local quota_all=$($RUNAS $LFS quota $DIR)
4231
4232         [ "$(echo "$quota_all" | head -n3)" != "$quota_u" ] &&
4233                 error "usr quota not match"
4234         [ "$(echo "$quota_all" | tail -n3)" != "$quota_g" ] &&
4235                 error "grp quota not match"
4236
4237         rm -f $TESTFILE
4238         # cleanup
4239         cleanup_quota_test
4240 }
4241 run_test 65 "Check lfs quota result"
4242
4243 test_66() {
4244         ! is_project_quota_supported &&
4245                 skip "Project quota is not supported"
4246         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4247                 skip "Not supported before 2.12.4"
4248         setup_quota_test || error "setup quota failed with $?"
4249         stack_trap cleanup_quota_test EXIT
4250         local old=$(do_facet mds1 $LCTL get_param -n \
4251                     mdt.*.enable_chprojid_gid | head -1)
4252         local testdir=$DIR/$tdir/foo
4253
4254         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4255         stack_trap "do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0" \
4256                 EXIT
4257
4258         test_mkdir -i 0 -c 1 $testdir || error "failed to mkdir"
4259         chown -R $TSTID:$TSTID $testdir
4260         change_project -sp $TSTPRJID $testdir
4261         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4262
4263         $RUNAS lfs project -p 0 $testdir/foo &&
4264                 error "nonroot user should fail to set projid"
4265
4266         $RUNAS lfs project -C $testdir/foo &&
4267                 error "nonroot user should fail to clear projid"
4268
4269         change_project -C $testdir/foo || error "failed to clear project"
4270
4271         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4272         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4273         "failed to set projid with normal user when enable_chprojid_gid=-1"
4274
4275         $RUNAS lfs project -rC $testdir/ || error \
4276 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4277
4278         touch $testdir/bar || error "failed touch $testdir/bar"
4279         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4280         "normal user should not be able to set projid on root owned file"
4281
4282         change_project -p $TSTPRJID $testdir/bar || error \
4283                 "root should be able to change its own file's projid"
4284
4285         cleanup_quota_test
4286 }
4287 run_test 66 "nonroot user can not change project state in default"
4288
4289 test_67_write() {
4290         local file="$1"
4291         local qtype="$2"
4292         local size=$3
4293         local _runas=""
4294         local short_qtype=${qtype:0:1}
4295
4296         echo "file "$file
4297         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4298         case "$4" in
4299                 quota_usr)  _runas=$RUNAS;;
4300                 quota_2usr) _runas=$RUNAS2;;
4301                 *)          error "unknown quota parameter $4";;
4302         esac
4303
4304         log "Write..."
4305         date
4306         $_runas $DD of=$file count=$size ||
4307                 quota_error $short_qtype $TSTUSR \
4308                         "$qtype write failure, but expect success"
4309         date
4310         cancel_lru_locks osc
4311         date
4312         sync; sync_all_data || true
4313         date
4314 }
4315
4316 getgranted() {
4317         local pool=$1
4318         local ptype=$2
4319         local userid=$3
4320         local qtype=$4
4321         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4322
4323         do_facet mds1 $LCTL get_param $param |
4324                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4325 }
4326
4327 test_67() {
4328         local limit=20  # 20M
4329         local testfile="$DIR/$tdir/$tfile-0"
4330         local testfile2="$DIR/$tdir/$tfile-1"
4331         local testfile3="$DIR/$tdir/$tfile-2"
4332         local qpool="qpool1"
4333         local used
4334         local granted
4335         local granted_mb
4336
4337         mds_supports_qp
4338         [ "$ost1_FSTYPE" == zfs ] &&
4339                 skip "ZFS grants some block space together with inode"
4340
4341         setup_quota_test || error "setup quota failed with $?"
4342         trap cleanup_quota_test EXIT
4343
4344         # enable ost quota
4345         set_ost_qtype $QTYPE || error "enable ost quota failed"
4346
4347         # test for user
4348         log "User quota (block hardlimit:$limit MB)"
4349         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4350                 error "set user quota failed"
4351
4352         # make sure the system is clean
4353         used=$(getquota -u $TSTUSR global curspace)
4354         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4355
4356         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4357         echo "granted 0x0 before write $granted"
4358
4359         # trigger reintegration
4360         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4361         procf=${procf}quota_slave.force_reint
4362         do_facet ost1 $LCTL set_param $procf=1 ||
4363                 error "force reintegration failed"
4364         wait_ost_reint "u" || error "reintegration failed"
4365         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4366         [ $granted -ne 0 ] &&
4367                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4368
4369         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4370         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4371
4372         # write 10 MB to testfile
4373         test_67_write "$testfile" "user" 10 "quota_usr"
4374
4375         # create qpool and add OST1
4376         pool_add $qpool || error "pool_add failed"
4377         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4378         # as quota_usr hasn't limits, lqe may absent. But it should be
4379         # created after the 1st direct qmt_get.
4380         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4381
4382         # check granted - should be 0, as testfile is located only on OST0
4383         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4384         echo "global granted $granted"
4385         granted=$(getgranted $qpool "dt" $TSTID "usr")
4386         echo "$qpool granted $granted"
4387         [ $granted -ne 0 ] &&
4388                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4389
4390         # add OST0 to qpool and check granted space
4391         pool_add_targets $qpool 0 1 ||
4392                 error "pool_add_targets failed"
4393         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4394         echo "Granted $granted_mb MB"
4395         #should be 10M + qunit for each OST
4396         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4397                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4398
4399         $LFS setstripe $testfile2 -c 1 -i 1 ||
4400                 error "setstripe $testfile2 failed"
4401         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4402         # Write from another user and check that qpool1
4403         # shows correct granted, despite quota_2usr hasn't limits in qpool1.
4404         test_67_write "$testfile2" "user" 10 "quota_2usr"
4405         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4406         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4407         [ $granted -ne 0 ] &&
4408                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4409
4410         # Granted space for quota_2usr in qpool1 should appear only
4411         # when global lqe for this user becomes enforced.
4412         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4413                 error "set user quota failed"
4414         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4415         echo "granted_mb $granted_mb"
4416         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4417                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4418
4419         $LFS setstripe $testfile3 -c 1 -i 0 ||
4420                 error "setstripe $testfile3 failed"
4421         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4422         test_67_write "$testfile3" "user" 10 "quota_2usr"
4423         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4424         echo "$testfile3 granted_mb $granted_mb"
4425         [ $granted_mb -eq $limit ] ||
4426                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4427
4428         # remove OST1 from the qpool1 and check granted space
4429         # should be 0 for TSTUSR and 10M for TSTUSR2
4430         pool_remove_target $qpool 0
4431         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4432         [ $granted_mb -eq 0 ] ||
4433                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4434         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4435         [ $granted_mb -eq 10 ] ||
4436                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4437
4438         rm -f $testfile
4439         wait_delete_completed || error "wait_delete_completed failed"
4440         sync_all_data || true
4441         used=$(getquota -u $TSTUSR global curspace)
4442         [ $used -ne 0 ] && quota_error u $TSTUSR \
4443                 "user quota isn't released after deletion"
4444         resetquota -u $TSTUSR
4445
4446         cleanup_quota_test
4447 }
4448 run_test 67 "quota pools recalculation"
4449
4450 get_slave_nr() {
4451         local pool=$1
4452         local qtype=$2
4453         local nr
4454
4455         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4456                 awk '/usr/ {getline; print $2}'
4457 }
4458
4459 test_68()
4460 {
4461         local qpool="qpool1"
4462
4463         mds_supports_qp
4464         setup_quota_test || error "setup quota failed with $?"
4465         stack_trap cleanup_quota_test EXIT
4466
4467         # enable ost quota
4468         set_ost_qtype $QTYPE || error "enable ost quota failed"
4469
4470         # check slave number for glbal pool
4471         local nr=$(get_slave_nr "0x0" "usr")
4472         echo "nr result $nr"
4473         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4474                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4475
4476         # create qpool and add OST1
4477         pool_add $qpool || error "pool_add failed"
4478         nr=$(get_slave_nr $qpool "usr")
4479         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4480
4481         # add OST1 to qpool
4482         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4483         nr=$(get_slave_nr $qpool "usr")
4484         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4485
4486         # add OST0 to qpool
4487         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4488         nr=$(get_slave_nr $qpool "usr")
4489         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
4490
4491         # remove OST0
4492         pool_remove_target $qpool 0
4493         nr=$(get_slave_nr $qpool "usr")
4494         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4495
4496         # remove OST1
4497         pool_remove_target $qpool 1
4498         nr=$(get_slave_nr $qpool "usr")
4499         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4500
4501         # Check again that all is fine with global pool
4502         nr=$(get_slave_nr "0x0" "usr")
4503         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4504                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4505
4506         cleanup_quota_test
4507 }
4508 run_test 68 "slave number in quota pool changed after each add/remove OST"
4509
4510 test_69()
4511 {
4512         local global_limit=200  # 200M
4513         local limit=10  # 10M
4514         local testfile="$DIR/$tdir/$tfile-0"
4515         local dom0="$DIR/$tdir/dom0"
4516         local qpool="qpool1"
4517
4518         mds_supports_qp
4519         setup_quota_test || error "setup quota failed with $?"
4520         stack_trap cleanup_quota_test EXIT
4521
4522         # enable ost quota
4523         set_ost_qtype $QTYPE || error "enable ost quota failed"
4524         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4525
4526         # Save DOM only at MDT0
4527         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
4528         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
4529         chmod 0777 $dom0
4530         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
4531
4532         # create qpool and add OST0
4533         pool_add $qpool || error "pool_add failed"
4534         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4535
4536         log "User quota (block hardlimit:$global_limit MB)"
4537         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4538                 error "set user quota failed"
4539
4540         log "User quota (block hardlimit:$limit MB)"
4541         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4542                 error "set user quota failed"
4543
4544         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
4545                 quota_error u $TSTUSR "write failed"
4546
4547         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4548                 oflag=sync || quota_error u $TSTUSR "write failed"
4549
4550         $RUNAS $DD of=$testfile count=$limit || true
4551
4552         # flush cache, ensure noquota flag is set on client
4553         cancel_lru_locks osc
4554         sync; sync_all_data || true
4555
4556         # MDT0 shouldn't get EDQUOT with glimpse.
4557         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
4558                 quota_error u $TSTUSR \
4559                         "user write success, but expect EDQUOT"
4560
4561         # Now all members of qpool1 should get EDQUOT. Expect success
4562         # when write to DOM on MDT0, as it belongs to global pool.
4563         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
4564                 oflag=sync || quota_error u $TSTUSR "write failed"
4565
4566         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4567                 oflag=sync || quota_error u $TSTUSR "write failed"
4568
4569         cleanup_quota_test
4570 }
4571 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
4572
4573 test_70()
4574 {
4575         local qpool="qpool1"
4576         local limit=20
4577         local err=0
4578         local bhard
4579
4580         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
4581                 skip "Needs a client >= $VERSION_WITH_QP"
4582
4583         setup_quota_test || error "setup quota failed with $?"
4584         stack_trap cleanup_quota_test EXIT
4585
4586         # MDS returns EFAULT for unsupported quotactl command
4587         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
4588
4589         # create qpool and add OST0
4590         pool_add $qpool || error "pool_add failed"
4591         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4592
4593         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
4594         rc=$?
4595         [ $rc -eq $err ] || error "setquota res $rc != $err"
4596
4597         # If MDS supports QP, check that limit was set properly.
4598         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
4599                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
4600                 echo "hard limit $bhard limit $limit"
4601                 [ $bhard -ne $((limit*1024)) ] &&
4602                         error "bhard:$bhard for $qpool!=$((limit*1024))"
4603         fi
4604
4605         $LFS quota -u $TSTUSR --pool $qpool $DIR
4606         rc=$?
4607         [ $rc -eq $err ] || error "quota res $rc != $err"
4608
4609         cleanup_quota_test
4610 }
4611 run_test 70 "check lfs setquota/quota with a pool option"
4612
4613 test_71a()
4614 {
4615         local limit=10  # 10M
4616         local global_limit=100  # 100M
4617         local testfile="$DIR/$tdir/$tfile-0"
4618         local qpool="qpool1"
4619         local qpool2="qpool2"
4620
4621         [ "$ost1_FSTYPE" == zfs ] &&
4622                 skip "ZFS grants some block space together with inode"
4623         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
4624         mds_supports_qp
4625         setup_quota_test || error "setup quota failed with $?"
4626         stack_trap cleanup_quota_test EXIT
4627
4628         # enable ost quota
4629         set_ost_qtype $QTYPE || error "enable ost quota failed"
4630
4631         # test for user
4632         log "User quota (block hardlimit:$global_limit MB)"
4633         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4634                 error "set user quota failed"
4635
4636         pool_add $qpool || error "pool_add failed"
4637         pool_add_targets $qpool 0 1 ||
4638                 error "pool_add_targets failed"
4639
4640         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4641                 error "set user quota failed"
4642
4643         pool_add $qpool2 || error "pool_add failed"
4644         pool_add_targets $qpool2 1 1 ||
4645                 error "pool_add_targets failed"
4646
4647         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
4648                 error "set user quota failed"
4649
4650         # make sure the system is clean
4651         local used=$(getquota -u $TSTUSR global curspace)
4652
4653         echo "used $used"
4654         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4655
4656         # create 1st component 1-10M
4657         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4658         #create 2nd component 10-30M
4659         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4660         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4661
4662         # сheck normal use and out of quota with PFL
4663         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
4664         test_1_check_write $testfile "user" $((limit*2))
4665         rm -f $testfile
4666         wait_delete_completed || error "wait_delete_completed failed"
4667         sync_all_data || true
4668         used=$(getquota -u $TSTUSR global curspace)
4669         [ $used -ne 0 ] && quota_error u $TSTUSR \
4670                 "user quota isn't released after deletion"
4671
4672         # create 1st component 1-10M
4673         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4674         # create 2nd component 10-30M
4675         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4676         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4677
4678         # write to the 2nd component
4679         $RUNAS $DD of=$testfile count=$limit seek=10 ||
4680                 quota_error u $TSTUSR \
4681                         "write failure, but expect success"
4682         # this time maybe cache write,  ignore it's failure
4683         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
4684         cancel_lru_locks osc
4685         sync; sync_all_data || true
4686         # write over limit in qpool2(2nd component 10-30M)
4687         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
4688                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4689         # write to the 1st component - OST0 is empty
4690         $RUNAS $DD of=$testfile count=$limit seek=0 ||
4691                 quota_error u $TSTUSR "write failed"
4692
4693         cleanup_quota_test
4694 }
4695 run_test 71a "Check PFL with quota pools"
4696
4697 test_71b()
4698 {
4699         local global_limit=1000 # 1G
4700         local limit1=160 # 160M
4701         local limit2=10 # 10M
4702         local testfile="$DIR/$tdir/$tfile-0"
4703         local qpool="qpool1"
4704         local qpool2="qpool2"
4705
4706         [ "$ost1_FSTYPE" == zfs ] &&
4707                 skip "ZFS grants some block space together with inode"
4708         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
4709         mds_supports_qp
4710         setup_quota_test || error "setup quota failed with $?"
4711         stack_trap cleanup_quota_test EXIT
4712
4713         # enable ost quota
4714         set_ost_qtype $QTYPE || error "enable ost quota failed"
4715
4716         # test for user
4717         log "User quota (block hardlimit:$global_limit MB)"
4718         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4719                 error "set user quota failed"
4720
4721         pool_add $qpool || error "pool_add failed"
4722         pool_add_targets $qpool 0 1 ||
4723                 error "pool_add_targets failed"
4724
4725         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
4726                 error "set user quota failed"
4727
4728         pool_add $qpool2 || error "pool_add failed"
4729         pool_add_targets $qpool2 1 1 ||
4730                 error "pool_add_targets failed"
4731
4732         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
4733                 error "set user quota failed"
4734
4735         # make sure the system is clean
4736         local used=$(getquota -u $TSTUSR global curspace)
4737
4738         echo "used $used"
4739         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4740
4741         # First component is on OST0, 2nd on OST1
4742         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
4743         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4744
4745         # fill the 1st component on OST0
4746         $RUNAS $DD of=$testfile count=128 ||
4747                 quota_error u $TSTUSR "write failed"
4748         # write to the 2nd cmpnt on OST1
4749         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
4750                 quota_error u $TSTUSR "write failed"
4751         # this time maybe cache write,  ignore it's failure
4752         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
4753                 true
4754         cancel_lru_locks osc
4755         sync; sync_all_data || true
4756         # write over limit in qpool2
4757         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
4758                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4759
4760         cleanup_quota_test
4761 }
4762 run_test 71b "Check SEL with quota pools"
4763
4764 test_72()
4765 {
4766         local limit=10  # 10M
4767         local global_limit=50  # 50M
4768         local testfile="$DIR/$tdir/$tfile-0"
4769         local qpool="qpool1"
4770
4771         mds_supports_qp
4772         setup_quota_test || error "setup quota failed with $?"
4773         stack_trap cleanup_quota_test EXIT
4774
4775         # enable ost quota
4776         set_ost_qtype $QTYPE || error "enable ost quota failed"
4777
4778         # test for user
4779         log "User quota (block hardlimit:$global_limit MB)"
4780         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4781                 error "set user quota failed"
4782
4783         pool_add $qpool || error "pool_add failed"
4784         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4785
4786         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4787                 error "set user quota failed"
4788
4789         # make sure the system is clean
4790         local used=$(getquota -u $TSTUSR global curspace)
4791         echo "used $used"
4792         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4793
4794         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4795
4796         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
4797         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4798         test_1_check_write $testfile "user" $limit
4799         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4800         echo "used $used"
4801         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
4802         # check that lfs quota -uv --pool prints only OST that
4803         # was added in a pool
4804         lfs quota -v -u quota_usr --pool $qpool $DIR | grep -v "OST0001" |
4805                 grep "OST\|MDT" && error "$qpool consists wrong targets"
4806
4807         cleanup_quota_test
4808 }
4809 run_test 72 "lfs quota --pool prints only pool's OSTs"
4810
4811 quota_fini()
4812 {
4813         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
4814         if $PQ_CLEANUP; then
4815                 disable_project_quota
4816         fi
4817 }
4818 reset_quota_settings
4819 quota_fini
4820
4821 cd $ORIG_PWD
4822 complete $SECONDS
4823 check_and_cleanup_lustre
4824 export QUOTA_AUTO=$QUOTA_AUTO_OLD
4825 exit_status