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