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