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