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