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