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