Whamcloud - gitweb
LU-13639 tests: increase limit in sanity-quota t_2
[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_50() {
3594         ! is_project_quota_supported &&
3595                 skip "Project quota is not supported"
3596
3597         setup_quota_test || error "setup quota failed with $?"
3598         local dir1="$DIR/$tdir/dir1"
3599         local dir2="$DIR/$tdir/dir2"
3600
3601         mkdir -p $dir1 && change_project -sp 1 $dir1
3602         mkdir -p $dir2 && change_project -sp 2 $dir2
3603         for num in $(seq 1 10); do
3604                 touch $dir1/file_$num $dir2/file_$num
3605                 ln -s $dir1/file_$num $dir1/file_$num"_link"
3606                 ln -s $dir2/file_$num $dir2/file_$num"_link"
3607         done
3608
3609         count=$($LFS find --projid 1 $DIR | wc -l)
3610         [ "$count" != 21 ] && error "expected 21 but got $count"
3611
3612         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
3613         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
3614         [ $count -eq 22 ] || error "expected 22 but got $count"
3615 }
3616 run_test 50 "Test if lfs find --projid works"
3617
3618 test_51() {
3619         ! is_project_quota_supported &&
3620                 skip "Project quota is not supported"
3621         setup_quota_test || error "setup quota failed with $?"
3622         local dir="$DIR/$tdir/dir"
3623
3624         mkdir $dir && change_project -sp 1 $dir
3625         local used=$(getquota -p 1 global curinodes)
3626         [ $used != "1" ] && error "expected 1 got $used"
3627
3628         touch $dir/1
3629         touch $dir/2
3630         cp $dir/2 $dir/3
3631         used=$(getquota -p 1 global curinodes)
3632         [ $used != "4" ] && error "expected 4 got $used"
3633
3634         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
3635         #try cp to dir
3636         cp $DIR/$tdir/6 $dir/6
3637         used=$(getquota -p 1 global curinodes)
3638         [ $used != "5" ] && error "expected 5 got $used"
3639
3640         #try mv to dir
3641         mv $DIR/$tdir/6 $dir/7
3642         used=$(getquota -p 1 global curinodes)
3643         [ $used -eq 6 ] || error "expected 6 got $used"
3644 }
3645 run_test 51 "Test project accounting with mv/cp"
3646
3647 test_52() {
3648         ! is_project_quota_supported &&
3649                 skip "Project quota is not supported"
3650         setup_quota_test || error "setup quota failed with $?"
3651         local dir="$DIR/$tdir/dir"
3652         mkdir $dir && change_project -sp 1 $dir
3653
3654         touch $DIR/$tdir/file
3655         #Try renaming a file into the project.  This should fail.
3656         for num in $(seq 1 2000); do
3657                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
3658                         error "rename should fail"
3659         done
3660         return 0
3661 }
3662 run_test 52 "Rename across different project ID"
3663
3664 test_53() {
3665         ! is_project_quota_supported &&
3666                 skip "Project quota is not supported"
3667         setup_quota_test || error "setup quota failed with $?"
3668         local dir="$DIR/$tdir/dir"
3669         mkdir $dir && change_project -s $dir
3670         [[ $($LFS project -d $dir) =~ " P " ]] ||
3671                 error "inherit attribute should be set"
3672
3673         change_project -C $dir
3674         [[ $($LFS project -d $dir) =~ " - " ]] ||
3675                 error "inherit attribute should be cleared"
3676 }
3677 run_test 53 "Project inherit attribute could be cleared"
3678
3679 test_54() {
3680         ! is_project_quota_supported &&
3681                 skip "Project quota is not supported"
3682         setup_quota_test || error "setup quota failed with $?"
3683         local testfile="$DIR/$tdir/$tfile-0"
3684
3685         #set project ID/inherit attribute
3686         change_project -sp $TSTPRJID $DIR/$tdir
3687         $RUNAS createmany -m ${testfile} 100 ||
3688                 error "create many files failed"
3689
3690         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
3691         # one more count for directory itself */
3692         ((proj_count++))
3693
3694         #check project
3695         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3696         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
3697
3698         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
3699         [ $proj_count1 -eq $proj_count ] ||
3700                         error "c2: expected $proj_count got $proj_count1"
3701
3702         #clear project but with kept projid
3703         change_project -rCk $DIR/$tdir
3704         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3705         [ $proj_count1 -eq 1 ] ||
3706                         error "c3: expected 1 got $proj_count1"
3707
3708         #verify projid untouched.
3709         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3710         ((proj_count1++))
3711         [ $proj_count1 -eq $proj_count ] ||
3712                         error "c4: expected $proj_count got $proj_count1"
3713
3714         # test -0 option
3715         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
3716         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3717         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
3718
3719         #this time clear all
3720         change_project -rC $DIR/$tdir
3721         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3722         [ $proj_count1 -eq 0 ] ||
3723                         error "c6: expected 0 got $proj_count1"
3724         #cleanup
3725         unlinkmany ${testfile} 100 ||
3726                 error "unlink many files failed"
3727 }
3728 run_test 54 "basic lfs project interface test"
3729
3730 test_55() {
3731         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
3732                 skip "Not supported before 2.10.58."
3733         setup_quota_test || error "setup quota failed with $?"
3734
3735         set_ost_qtype $QTYPE || error "enable ost quota failed"
3736         quota_init
3737
3738         #add second group to TSTUSR
3739         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
3740
3741         #prepare test file
3742         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3743         error "failed to dd"
3744
3745         cancel_lru_locks osc
3746         sync; sync_all_data || true
3747
3748         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3749         error "failed to setquota on group $TSTUSR2"
3750
3751         $LFS quota -v -g $TSTUSR2 $DIR
3752
3753         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3754         error "chgrp should failed with -EDQUOT"
3755
3756         USED=$(getquota -g $TSTUSR2 global curspace)
3757         echo "$USED"
3758
3759         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3760         error "failed to setquota on group $TSTUSR2"
3761
3762         $LFS quota -v -g $TSTUSR2 $DIR
3763
3764         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3765         error "chgrp should succeed"
3766
3767         $LFS quota -v -g $TSTUSR2 $DIR
3768 }
3769 run_test 55 "Chgrp should be affected by group quota"
3770
3771 test_56() {
3772         setup_quota_test || error "setup quota failed with $?"
3773
3774         set_ost_qtype $QTYPE || error "enable ost quota failed"
3775         quota_init
3776
3777         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3778                 erro "failed to set grace time for usr quota"
3779         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3780                      awk '{print $4 $8}')
3781         if [ "x$grace_time" != "x10s;10s" ]; then
3782                 $LFS quota -t -u $DIR
3783                 error "expected grace time: 10s;10s, got:$grace_time"
3784         fi
3785 }
3786 run_test 56 "lfs quota -t should work well"
3787
3788 test_57() {
3789         setup_quota_test || error "setup quota failed with $?"
3790
3791         local dir="$DIR/$tdir/dir"
3792         mkdir -p $dir
3793         mkfifo $dir/pipe
3794         #command can process further if it hit some errors
3795         $LFS project -sp 1 $dir/pipe
3796         touch $dir/aaa $dir/bbb
3797         mkdir $dir/subdir -p
3798         touch $dir/subdir/aaa $dir/subdir/bbb
3799         #create one invalid link file
3800         ln -s $dir/not_exist_file $dir/ccc
3801         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3802         [ $cnt -eq 7 ] || error "expected 7 got $cnt"
3803 }
3804 run_test 57 "lfs project could tolerate errors"
3805
3806 test_59() {
3807         [ "$mds1_FSTYPE" != ldiskfs ] &&
3808                 skip "ldiskfs only test"
3809         disable_project_quota
3810         setup_quota_test || error "setup quota failed with $?"
3811         quota_init
3812
3813         local testfile="$DIR/$tdir/$tfile-0"
3814         #make sure it did not crash kernel
3815         touch $testfile && lfs project -sp 1 $testfile
3816
3817         enable_project_quota
3818 }
3819 run_test 59 "lfs project dosen't crash kernel with project disabled"
3820
3821 test_60() {
3822         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3823                 skip "Needs MDS version 2.11.53 or later."
3824         setup_quota_test || error "setup quota failed with $?"
3825
3826         local testfile=$DIR/$tdir/$tfile
3827         local limit=100
3828
3829         set_mdt_qtype "ug" || error "enable mdt quota failed"
3830
3831         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3832                 error "set quota failed"
3833         quota_show_check a g $TSTUSR
3834
3835         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3836         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3837         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3838                 error "create many files failed"
3839
3840         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3841
3842         # root user can overrun quota
3843         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3844                 error "root user should succeed"
3845 }
3846 run_test 60 "Test quota for root with setgid"
3847
3848 # test default quota
3849 test_default_quota() {
3850         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3851                 skip "Not supported before 2.11.51."
3852
3853         local qtype=$1
3854         local qres_type=$2
3855         local qid=$TSTUSR
3856         local qprjid=$TSTPRJID
3857         local qdtype="-U"
3858         local qs="-b"
3859         local qh="-B"
3860         local LIMIT=20480 #20M disk space
3861         local TESTFILE="$DIR/$tdir/$tfile-0"
3862         local $qpool_cmd
3863
3864         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3865                 echo "Project quota is not supported" && return 0
3866
3867         [ $qtype == "-u" ] && qdtype="-U"
3868         [ $qtype == "-g" ] && qdtype="-G"
3869         [ $qtype == "-p" ] && {
3870                 qdtype="-P"
3871                 qid=$qprjid
3872         }
3873
3874         [ $qres_type == "meta" ] && {
3875                 LIMIT=10240 #10K inodes
3876                 qs="-i"
3877                 qh="-I"
3878         }
3879         [ ! -z "$3" ] && {
3880                 qpool_cmd="--pool $3"
3881                 # pool quotas don't work properly without global limit
3882                 $LFS setquota $qtype $qid -B1T -b1T $DIR ||
3883                         error "set global limit failed"
3884         }
3885
3886         setup_quota_test || error "setup quota failed with $?"
3887
3888         quota_init
3889
3890         # enable mdt/ost quota
3891         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3892         set_ost_qtype $QTYPE || error "enable ost quota failed"
3893
3894         log "set to use default quota"
3895         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
3896                 error "set $qid to use default quota failed"
3897
3898         log "set default quota"
3899         $LFS setquota $qdtype $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3900                 error "set $qid default quota failed"
3901
3902         log "get default quota"
3903         $LFS quota $qdtype $DIR || error "get default quota failed"
3904
3905         if [ $qres_type == "data" ]; then
3906                 local SLIMIT=$($LFS quota $qpool_cmd $qdtype $DIR | \
3907                                 grep "$MOUNT" | awk '{print $2}')
3908                 [ $SLIMIT -eq $LIMIT ] ||
3909                         error "the returned default quota is wrong"
3910         else
3911                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3912                                                         awk '{print $5}')
3913                 [ $SLIMIT -eq $LIMIT ] ||
3914                         error "the returned default quota is wrong"
3915         fi
3916
3917         # make sure the system is clean
3918         local USED=$(getquota $qtype $qid global curspace)
3919         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3920
3921         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
3922                         error "setstripe $TESTFILE failed"
3923         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3924
3925         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3926
3927         log "Test not out of quota"
3928         if [ $qres_type == "data" ]; then
3929                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3930                         quota_error $qtype $qid "write failed, expect succeed"
3931         else
3932                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3933                         quota_error $qtype $qid "create failed, expect succeed"
3934
3935                 unlinkmany $TESTFILE $((LIMIT/2))
3936         fi
3937
3938         log "Test out of quota"
3939         # flush cache, ensure noquota flag is set on client
3940         cancel_lru_locks osc
3941         cancel_lru_locks mdc
3942         sync; sync_all_data || true
3943         if [ $qres_type == "data" ]; then
3944                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3945                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3946         else
3947                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3948                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3949
3950                 unlinkmany $TESTFILE $((LIMIT*2))
3951         fi
3952
3953         rm -f $TESTFILE
3954         $LFS setstripe $TESTFILE -c 1 $qpool_cmd ||
3955                         error "setstripe $TESTFILE failed"
3956         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3957
3958         log "Increase default quota"
3959
3960         # LU-4505: sleep 5 seconds to enable quota acquire
3961         sleep 5
3962
3963         # increase default quota
3964         $LFS setquota $qdtype $qpool_cmd $qs $((LIMIT*3)) \
3965                 $qh $((LIMIT*3)) $DIR || error "set default quota failed"
3966
3967         cancel_lru_locks osc
3968         cancel_lru_locks mdc
3969         sync; sync_all_data || true
3970         if [ $qres_type == "data" ]; then
3971                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3972                         quota_error $qtype $qid "write failed, expect succeed"
3973         else
3974                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3975                         quota_error $qtype $qid "create failed, expect succeed"
3976
3977                 unlinkmany $TESTFILE $((LIMIT*2))
3978         fi
3979
3980         log "Set quota to override default quota"
3981         $LFS setquota $qtype $qid $qpool_cmd $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3982                 error "set $qid quota failed"
3983
3984         cancel_lru_locks osc
3985         cancel_lru_locks mdc
3986         sync; sync_all_data || true
3987         if [ $qres_type == "data" ]; then
3988                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3989                         quota_error $qtype $qid "write succeed, expect EQUOT"
3990         else
3991                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3992                         quota_error $qtype $qid "create succeed, expect EQUOT"
3993
3994                 unlinkmany $TESTFILE $((LIMIT*2))
3995         fi
3996
3997         log "Set to use default quota again"
3998
3999         # LU-4505: sleep 5 seconds to enable quota acquire
4000         sleep 5
4001
4002         $LFS setquota $qtype $qid -d $qpool_cmd $DIR ||
4003                 error "set $qid to use default quota failed"
4004
4005         cancel_lru_locks osc
4006         cancel_lru_locks mdc
4007         sync; sync_all_data || true
4008         if [ $qres_type == "data" ]; then
4009                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
4010                         quota_error $qtype $qid "write failed, expect succeed"
4011         else
4012                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
4013                         quota_error $qtype $qid "create failed, expect succeed"
4014
4015                 unlinkmany $TESTFILE $((LIMIT*2))
4016         fi
4017
4018         log "Cleanup"
4019         rm -f $TESTFILE
4020         wait_delete_completed || error "wait_delete_completed failed"
4021         sync_all_data || true
4022
4023         $LFS setquota $qdtype $qpool_cmd $qs 0 $qh 0 $DIR ||
4024                 error "reset default quota failed"
4025         $LFS setquota $qtype $qid $qpool_cmd $qs 0 $qh 0 $DIR ||
4026                 error "reset quota failed"
4027         cleanup_quota_test
4028 }
4029
4030 test_61() {
4031         test_default_quota "-u" "data"
4032         test_default_quota "-u" "meta"
4033         test_default_quota "-g" "data"
4034         test_default_quota "-g" "meta"
4035         test_default_quota "-p" "data"
4036         test_default_quota "-p" "meta"
4037 }
4038 run_test 61 "default quota tests"
4039
4040 test_62() {
4041         ! is_project_quota_supported &&
4042                 skip "Project quota is not supported"
4043         [[ "$(chattr -h 2>&1)" =~ "project" ||
4044            "$(chattr -h 2>&1)" =~ "pRVf" ]] ||
4045                 skip "chattr did not support project quota"
4046         setup_quota_test || error "setup quota failed with $?"
4047         local testdir=$DIR/$tdir/
4048
4049         $RUNAS mkdir -p $testdir || error "failed to mkdir"
4050         change_project -s $testdir
4051         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4052                 error "inherit attribute should be set"
4053         # chattr used FS_IOC_SETFLAGS ioctl
4054         $RUNAS chattr -P $testdir &&
4055                 error "regular user clear inherit should fail"
4056         [[ $($LFS project -d $testdir) =~ "P" ]] ||
4057                 error "inherit attribute should still be set"
4058         chattr -P $testdir || error "root failed to clear inherit"
4059         [[ $($LFS project -d $testdir) =~ "P" ]] &&
4060                 error "inherit attribute should be cleared"
4061         return 0
4062 }
4063 run_test 62 "Project inherit should be only changed by root"
4064
4065 test_dom() {
4066         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
4067                 skip "Not supported before 2.11.55"
4068
4069         local qtype=$1
4070         local qid=$TSTUSR
4071         local dd_failed=false
4072         local tdir_dom=${tdir}_dom
4073         local LIMIT=20480 #20M
4074
4075         [ $qtype == "p" ] && ! is_project_quota_supported &&
4076                 echo "Project quota is not supported" && return 0
4077
4078         [ $qtype == "p" ] && qid=$TSTPRJID
4079
4080         setup_quota_test || error "setup quota failed with $?"
4081
4082         quota_init
4083
4084         # enable mdt/ost quota
4085         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4086         set_ost_qtype $QTYPE || error "enable ost quota failed"
4087
4088         # make sure the system is clean
4089         local USED=$(getquota -$qtype $qid global curspace)
4090         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
4091
4092         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
4093
4094         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
4095         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
4096                 error "setstripe $tdir_dom failed"
4097         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
4098
4099         [ $qtype == "p" ] && {
4100                 change_project -sp $TSTPRJID $DIR/$tdir
4101                 change_project -sp $TSTPRJID $DIR/$tdir_dom
4102         }
4103
4104         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
4105                 error "set $qid quota failed"
4106
4107         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4108                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4109                                                                 dd_failed=true
4110         done
4111
4112         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4113
4114         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
4115                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4116                                                                 dd_failed=true
4117         done
4118
4119         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4120
4121         rm -f $DIR/$tdir_dom/*
4122
4123         # flush cache, ensure noquota flag is set on client
4124         cancel_lru_locks osc
4125         cancel_lru_locks mdc
4126         sync; sync_all_data || true
4127
4128         dd_failed=false
4129
4130         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4131                 quota_error $qtype $qid "write failed, expect succeed"
4132
4133         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4134                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4135                                                                 dd_failed=true
4136         done
4137
4138         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4139
4140         rm -f $DIR/$tdir/*
4141         rm -f $DIR/$tdir_dom/*
4142
4143         # flush cache, ensure noquota flag is set on client
4144         cancel_lru_locks osc
4145         cancel_lru_locks mdc
4146         sync; sync_all_data || true
4147
4148         dd_failed=false
4149
4150         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4151                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4152                                                                 dd_failed=true
4153         done
4154
4155         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4156
4157         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4158                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4159
4160         rm -fr $DIR/$tdir
4161         rm -fr $DIR/$tdir_dom
4162
4163         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4164                 error "reset usr quota failed"
4165 }
4166
4167 test_63() {
4168         test_dom "u"
4169         test_dom "g"
4170         test_dom "p"
4171 }
4172 run_test 63 "quota on DoM tests"
4173
4174 test_64() {
4175         ! is_project_quota_supported &&
4176                 skip "Project quota is not supported"
4177         setup_quota_test || error "setup quota failed with $?"
4178         local dir1="$DIR/$tdir/"
4179
4180         touch $dir1/file
4181         ln -s $dir1/file $dir1/file_link
4182         mkfifo $dir1/fifo
4183
4184         $LFS project -srp $TSTPRJID $dir1 >&/dev/null ||
4185                 error "set project should succeed"
4186
4187         used=$(getquota -p $TSTPRJID global curinodes)
4188         [ $used -eq 4 ] || error "expected 4 got $used"
4189         $LFS project -rC $dir1 >&/dev/null ||
4190                 error "clear project should succeed"
4191
4192         used=$(getquota -p $TSTPRJID global curinodes)
4193         [ $used -eq 0 ] || error "expected 0 got $used"
4194 }
4195 run_test 64 "lfs project on non dir/files should succeed"
4196
4197 test_65() {
4198         local SIZE=10 # MB
4199         local TESTFILE="$DIR/$tdir/$tfile-0"
4200
4201         setup_quota_test || error "setup quota failed with $?"
4202         set_ost_qtype $QTYPE || error "enable ost quota failed"
4203         quota_init
4204
4205         echo "Write..."
4206         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4207                 error "failed to write"
4208         # flush cache, ensure noquota flag is set on client
4209         cancel_lru_locks osc
4210         sync; sync_all_data || true
4211
4212         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4213         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4214         local quota_all=$($RUNAS $LFS quota $DIR)
4215
4216         [ "$(echo "$quota_all" | head -n3)" == "$quota_u" ] ||
4217                 error "usr quota not match"
4218         [ "$(echo "$quota_all" | tail -n3)" == "$quota_g" ] ||
4219                 error "grp quota not match"
4220 }
4221 run_test 65 "Check lfs quota result"
4222
4223 test_66() {
4224         ! is_project_quota_supported &&
4225                 skip "Project quota is not supported"
4226         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4227                 skip "Not supported before 2.12.4"
4228         setup_quota_test || error "setup quota failed with $?"
4229         local old=$(do_facet mds1 $LCTL get_param -n \
4230                     mdt.*.enable_chprojid_gid | head -1)
4231         local testdir=$DIR/$tdir/foo
4232
4233         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4234         stack_trap "do_facet mds1 $LCTL \
4235                 set_param mdt.*.enable_chprojid_gid=$old" EXIT
4236
4237         mkdir_on_mdt0 $testdir || error "failed to mkdir"
4238         chown -R $TSTID:$TSTID $testdir
4239         change_project -sp $TSTPRJID $testdir
4240         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4241
4242         $RUNAS lfs project -p 0 $testdir/foo &&
4243                 error "nonroot user should fail to set projid"
4244
4245         $RUNAS lfs project -C $testdir/foo &&
4246                 error "nonroot user should fail to clear projid"
4247
4248         change_project -C $testdir/foo || error "failed to clear project"
4249
4250         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4251         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4252         "failed to set projid with normal user when enable_chprojid_gid=-1"
4253
4254         $RUNAS lfs project -rC $testdir/ || error \
4255 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4256
4257         touch $testdir/bar || error "failed touch $testdir/bar"
4258         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4259         "normal user should not be able to set projid on root owned file"
4260
4261         change_project -p $TSTPRJID $testdir/bar || error \
4262                 "root should be able to change its own file's projid"
4263 }
4264 run_test 66 "nonroot user can not change project state in default"
4265
4266 test_67_write() {
4267         local file="$1"
4268         local qtype="$2"
4269         local size=$3
4270         local _runas=""
4271         local short_qtype=${qtype:0:1}
4272
4273         echo "file "$file
4274         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4275         case "$4" in
4276                 quota_usr)  _runas=$RUNAS;;
4277                 quota_2usr) _runas=$RUNAS2;;
4278                 *)          error "unknown quota parameter $4";;
4279         esac
4280
4281         log "Write..."
4282         date
4283         $_runas $DD of=$file count=$size ||
4284                 quota_error $short_qtype $TSTUSR \
4285                         "$qtype write failure, but expect success"
4286         date
4287         cancel_lru_locks osc
4288         date
4289         sync; sync_all_data || true
4290         date
4291 }
4292
4293 getgranted() {
4294         local pool=$1
4295         local ptype=$2
4296         local userid=$3
4297         local qtype=$4
4298         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4299
4300         do_facet mds1 $LCTL get_param $param |
4301                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4302 }
4303
4304 test_67() {
4305         local limit=20 # MB
4306         local testfile="$DIR/$tdir/$tfile-0"
4307         local testfile2="$DIR/$tdir/$tfile-1"
4308         local testfile3="$DIR/$tdir/$tfile-2"
4309         local qpool="qpool1"
4310         local used
4311         local granted
4312         local granted_mb
4313
4314         mds_supports_qp
4315         [ "$ost1_FSTYPE" == zfs ] &&
4316                 skip "ZFS grants some block space together with inode"
4317
4318         setup_quota_test || error "setup quota failed with $?"
4319
4320         # enable ost quota
4321         set_ost_qtype $QTYPE || error "enable ost quota failed"
4322
4323         # test for user
4324         log "User quota (block hardlimit:$limit MB)"
4325         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4326                 error "set user quota failed"
4327
4328         # make sure the system is clean
4329         used=$(getquota -u $TSTUSR global curspace)
4330         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4331
4332         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4333         echo "granted 0x0 before write $granted"
4334
4335         # trigger reintegration
4336         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4337         procf=${procf}quota_slave.force_reint
4338         do_facet ost1 $LCTL set_param $procf=1 ||
4339                 error "force reintegration failed"
4340         wait_ost_reint "u" || error "reintegration failed"
4341         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4342         [ $granted -ne 0 ] &&
4343                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4344
4345         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4346         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4347
4348         # write 10 MB to testfile
4349         test_67_write "$testfile" "user" 10 "quota_usr"
4350
4351         # create qpool and add OST1
4352         pool_add $qpool || error "pool_add failed"
4353         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4354         # as quota_usr hasn't limits, lqe may absent. But it should be
4355         # created after the 1st direct qmt_get.
4356         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4357
4358         # check granted - should be 0, as testfile is located only on OST0
4359         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4360         echo "global granted $granted"
4361         granted=$(getgranted $qpool "dt" $TSTID "usr")
4362         echo "$qpool granted $granted"
4363         [ $granted -ne 0 ] &&
4364                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4365
4366         # add OST0 to qpool and check granted space
4367         pool_add_targets $qpool 0 1 ||
4368                 error "pool_add_targets failed"
4369         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4370         echo "Granted $granted_mb MB"
4371         #should be 10M + qunit for each OST
4372         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4373                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4374
4375         $LFS setstripe $testfile2 -c 1 -i 1 ||
4376                 error "setstripe $testfile2 failed"
4377         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4378         # Write from another user and check that qpool1
4379         # shows correct granted, despite quota_2usr hasn't limits in qpool1.
4380         test_67_write "$testfile2" "user" 10 "quota_2usr"
4381         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4382         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4383         [ $granted -ne 0 ] &&
4384                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4385
4386         # Granted space for quota_2usr in qpool1 should appear only
4387         # when global lqe for this user becomes enforced.
4388         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4389                 error "set user quota failed"
4390         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4391         echo "granted_mb $granted_mb"
4392         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4393                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4394
4395         $LFS setstripe $testfile3 -c 1 -i 0 ||
4396                 error "setstripe $testfile3 failed"
4397         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4398         test_67_write "$testfile3" "user" 10 "quota_2usr"
4399         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4400         echo "$testfile3 granted_mb $granted_mb"
4401         [ $granted_mb -eq $limit ] ||
4402                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4403
4404         # remove OST1 from the qpool1 and check granted space
4405         # should be 0 for TSTUSR and 10M for TSTUSR2
4406         pool_remove_target $qpool 0
4407         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4408         [ $granted_mb -eq 0 ] ||
4409                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4410         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4411         [ $granted_mb -eq 10 ] ||
4412                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4413
4414         rm -f $testfile
4415         wait_delete_completed || error "wait_delete_completed failed"
4416         sync_all_data || true
4417         used=$(getquota -u $TSTUSR global curspace)
4418         [ $used -eq 0 ] || quota_error u $TSTUSR \
4419                 "user quota isn't released after deletion"
4420 }
4421 run_test 67 "quota pools recalculation"
4422
4423 get_slave_nr() {
4424         local pool=$1
4425         local qtype=$2
4426         local nr
4427
4428         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4429                 awk '/usr/ {getline; print $2}'
4430 }
4431
4432 test_68()
4433 {
4434         local qpool="qpool1"
4435
4436         mds_supports_qp
4437         setup_quota_test || error "setup quota failed with $?"
4438
4439         # enable ost quota
4440         set_ost_qtype $QTYPE || error "enable ost quota failed"
4441
4442         # check slave number for glbal pool
4443         local nr=$(get_slave_nr "0x0" "usr")
4444         echo "nr result $nr"
4445         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4446                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4447
4448         # create qpool and add OST1
4449         pool_add $qpool || error "pool_add failed"
4450         nr=$(get_slave_nr $qpool "usr")
4451         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4452
4453         # add OST1 to qpool
4454         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4455         nr=$(get_slave_nr $qpool "usr")
4456         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4457
4458         # add OST0 to qpool
4459         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4460         nr=$(get_slave_nr $qpool "usr")
4461         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
4462
4463         # remove OST0
4464         pool_remove_target $qpool 0
4465         nr=$(get_slave_nr $qpool "usr")
4466         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4467
4468         # remove OST1
4469         pool_remove_target $qpool 1
4470         nr=$(get_slave_nr $qpool "usr")
4471         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4472
4473         # Check again that all is fine with global pool
4474         nr=$(get_slave_nr "0x0" "usr")
4475         [[ $nr == $((OSTCOUNT + MDSCOUNT)) ]] ||
4476                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4477 }
4478 run_test 68 "slave number in quota pool changed after each add/remove OST"
4479
4480 test_69()
4481 {
4482         local global_limit=200 # MB
4483         local limit=10 # MB
4484         local testfile="$DIR/$tdir/$tfile-0"
4485         local dom0="$DIR/$tdir/dom0"
4486         local qpool="qpool1"
4487
4488         mds_supports_qp
4489         setup_quota_test || error "setup quota failed with $?"
4490
4491         # enable ost quota
4492         set_ost_qtype $QTYPE || error "enable ost quota failed"
4493         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4494
4495         # Save DOM only at MDT0
4496         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
4497         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
4498         chmod 0777 $dom0
4499         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
4500
4501         # create qpool and add OST0
4502         pool_add $qpool || error "pool_add failed"
4503         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4504
4505         log "User quota (block hardlimit:$global_limit MB)"
4506         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4507                 error "set user quota failed"
4508
4509         log "User quota (block hardlimit:$limit MB)"
4510         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4511                 error "set user quota failed"
4512
4513         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
4514                 quota_error u $TSTUSR "write failed"
4515
4516         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4517                 oflag=sync || quota_error u $TSTUSR "write failed"
4518
4519         $RUNAS $DD of=$testfile count=$limit || true
4520
4521         # flush cache, ensure noquota flag is set on client
4522         cancel_lru_locks osc
4523         sync; sync_all_data || true
4524
4525         # MDT0 shouldn't get EDQUOT with glimpse.
4526         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
4527                 quota_error u $TSTUSR \
4528                         "user write success, but expect EDQUOT"
4529
4530         # Now all members of qpool1 should get EDQUOT. Expect success
4531         # when write to DOM on MDT0, as it belongs to global pool.
4532         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
4533                 oflag=sync || quota_error u $TSTUSR "write failed"
4534
4535         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4536                 oflag=sync || quota_error u $TSTUSR "write failed"
4537 }
4538 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
4539
4540 test_70a()
4541 {
4542         local qpool="qpool1"
4543         local limit=20 # MB
4544         local err=0
4545         local bhard
4546
4547         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
4548                 skip "Needs a client >= $VERSION_WITH_QP"
4549
4550         setup_quota_test || error "setup quota failed with $?"
4551
4552         # MDS returns EFAULT for unsupported quotactl command
4553         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
4554
4555         # create qpool and add OST0
4556         pool_add $qpool || error "pool_add failed"
4557         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4558
4559         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
4560         rc=$?
4561         [ $rc -eq $err ] || error "setquota res $rc != $err"
4562
4563         # If MDS supports QP, check that limit was set properly.
4564         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
4565                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
4566                 echo "hard limit $bhard limit $limit"
4567                 [ $bhard -ne $((limit*1024)) ] &&
4568                         error "bhard:$bhard for $qpool!=$((limit*1024))"
4569         fi
4570
4571         $LFS quota -u $TSTUSR --pool $qpool $DIR
4572         rc=$?
4573         [ $rc -eq $err ] || error "quota res $rc != $err"
4574 }
4575 run_test 70a "check lfs setquota/quota with a pool option"
4576
4577 test_70b()
4578 {
4579         local glbl_hard=200 # 200M
4580         local glbl_soft=100 # 100M
4581         local pool_hard=10 # 10M
4582         local qpool="qpool1"
4583
4584         pool_add $qpool || error "pool_add failed"
4585         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4586
4587         $LFS setquota -u $TSTUSR -b ${glbl_soft}M -B ${glbl_hard}M $DIR ||
4588                 error "set user quota failed"
4589         $LFS setquota -u $TSTUSR -B ${pool_hard}M --pool $qpool $DIR ||
4590                 error "set user quota failed"
4591
4592         local tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
4593         [ $tmp -eq $((pool_hard * 1024)) ] ||
4594                 error "wrong block hard limit $tmp for $qpool"
4595         local tmp=$(getquota -u $TSTUSR global bsoftlimit $qpool)
4596         # soft limit hasn't been set and should be zero
4597         [ $tmp -eq 0 ] || error "wrong soft block limit $tmp for $qpool"
4598 }
4599 run_test 70b "lfs setquota pool works properly"
4600
4601 test_71a()
4602 {
4603         local limit=10 # MB
4604         local global_limit=100 # MB
4605         local testfile="$DIR/$tdir/$tfile-0"
4606         local qpool="qpool1"
4607         local qpool2="qpool2"
4608
4609         [ "$ost1_FSTYPE" == zfs ] &&
4610                 skip "ZFS grants some block space together with inode"
4611         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
4612         mds_supports_qp
4613         setup_quota_test || error "setup quota failed with $?"
4614
4615         # enable ost quota
4616         set_ost_qtype $QTYPE || error "enable ost quota failed"
4617
4618         # test for user
4619         log "User quota (block hardlimit:$global_limit MB)"
4620         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4621                 error "set user quota failed"
4622
4623         pool_add $qpool || error "pool_add failed"
4624         pool_add_targets $qpool 0 1 ||
4625                 error "pool_add_targets failed"
4626
4627         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4628                 error "set user quota failed"
4629
4630         pool_add $qpool2 || error "pool_add failed"
4631         pool_add_targets $qpool2 1 1 ||
4632                 error "pool_add_targets failed"
4633
4634         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
4635                 error "set user quota failed"
4636
4637         # make sure the system is clean
4638         local used=$(getquota -u $TSTUSR global curspace)
4639
4640         echo "used $used"
4641         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4642
4643         # create 1st component 1-10M
4644         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4645         #create 2nd component 10-30M
4646         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4647         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4648
4649         # сheck normal use and out of quota with PFL
4650         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
4651         test_1_check_write $testfile "user" $((limit*2))
4652         rm -f $testfile
4653         wait_delete_completed || error "wait_delete_completed failed"
4654         sync_all_data || true
4655         used=$(getquota -u $TSTUSR global curspace)
4656         [ $used -ne 0 ] && quota_error u $TSTUSR \
4657                 "user quota isn't released after deletion"
4658
4659         # create 1st component 1-10M
4660         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4661         # create 2nd component 10-30M
4662         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4663         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4664
4665         # write to the 2nd component
4666         $RUNAS $DD of=$testfile count=$limit seek=10 ||
4667                 quota_error u $TSTUSR \
4668                         "write failure, but expect success"
4669         # this time maybe cache write,  ignore it's failure
4670         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
4671         cancel_lru_locks osc
4672         sync; sync_all_data || true
4673         # write over limit in qpool2(2nd component 10-30M)
4674         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
4675                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4676         # write to the 1st component - OST0 is empty
4677         $RUNAS $DD of=$testfile count=$limit seek=0 ||
4678                 quota_error u $TSTUSR "write failed"
4679 }
4680 run_test 71a "Check PFL with quota pools"
4681
4682 test_71b()
4683 {
4684         local global_limit=1000 # MB
4685         local limit1=160 # MB
4686         local limit2=10 # MB
4687         local testfile="$DIR/$tdir/$tfile-0"
4688         local qpool="qpool1"
4689         local qpool2="qpool2"
4690
4691         [ "$ost1_FSTYPE" == zfs ] &&
4692                 skip "ZFS grants some block space together with inode"
4693         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
4694         mds_supports_qp
4695         setup_quota_test || error "setup quota failed with $?"
4696
4697         # enable ost quota
4698         set_ost_qtype $QTYPE || error "enable ost quota failed"
4699
4700         # test for user
4701         log "User quota (block hardlimit:$global_limit MB)"
4702         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4703                 error "set user quota failed"
4704
4705         pool_add $qpool || error "pool_add failed"
4706         pool_add_targets $qpool 0 1 ||
4707                 error "pool_add_targets failed"
4708
4709         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
4710                 error "set user quota failed"
4711
4712         pool_add $qpool2 || error "pool_add failed"
4713         pool_add_targets $qpool2 1 1 ||
4714                 error "pool_add_targets failed"
4715
4716         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
4717                 error "set user quota failed"
4718
4719         # make sure the system is clean
4720         local used=$(getquota -u $TSTUSR global curspace)
4721
4722         echo "used $used"
4723         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4724
4725         # First component is on OST0, 2nd on OST1
4726         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
4727         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4728
4729         # fill the 1st component on OST0
4730         $RUNAS $DD of=$testfile count=128 ||
4731                 quota_error u $TSTUSR "write failed"
4732         # write to the 2nd cmpnt on OST1
4733         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
4734                 quota_error u $TSTUSR "write failed"
4735         # this time maybe cache write,  ignore it's failure
4736         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
4737                 true
4738         cancel_lru_locks osc
4739         sync; sync_all_data || true
4740         # write over limit in qpool2
4741         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
4742                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4743         return 0
4744 }
4745 run_test 71b "Check SEL with quota pools"
4746
4747 test_72()
4748 {
4749         local limit=10 # MB
4750         local global_limit=50 # MB
4751         local testfile="$DIR/$tdir/$tfile-0"
4752         local qpool="qpool1"
4753
4754         mds_supports_qp
4755         setup_quota_test || error "setup quota failed with $?"
4756
4757         # enable ost quota
4758         set_ost_qtype $QTYPE || error "enable ost quota failed"
4759
4760         # test for user
4761         log "User quota (block hardlimit:$global_limit MB)"
4762         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4763                 error "set user quota failed"
4764
4765         pool_add $qpool || error "pool_add failed"
4766         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4767
4768         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4769                 error "set user quota failed"
4770
4771         # make sure the system is clean
4772         local used=$(getquota -u $TSTUSR global curspace)
4773         echo "used $used"
4774         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4775
4776         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4777
4778         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
4779         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4780         test_1_check_write $testfile "user" $limit
4781         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4782         echo "used $used"
4783         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
4784         # check that lfs quota -uv --pool prints only OST that
4785         # was added in a pool
4786         lfs quota -v -u quota_usr --pool $qpool $DIR | grep -v "OST0001" |
4787                 grep "OST\|MDT" && error "$qpool consists wrong targets"
4788         return 0
4789 }
4790 run_test 72 "lfs quota --pool prints only pool's OSTs"
4791
4792 test_73a()
4793 {
4794         local qpool="qpool1"
4795
4796         mds_supports_qp
4797
4798         pool_add $qpool || error "pool_add failed"
4799         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
4800                 error "pool_add_targets failed"
4801
4802         test_default_quota "-u" "data" "qpool1"
4803 }
4804 run_test 73a "default limits at OST Pool Quotas"
4805
4806 test_73b()
4807 {
4808         local TESTFILE1="$DIR/$tdir/$tfile-1"
4809         local limit=20 #20M
4810         local qpool="qpool1"
4811
4812         mds_supports_qp
4813
4814         setup_quota_test || error "setup quota failed with $?"
4815         quota_init
4816         set_ost_qtype $QTYPE || error "enable ost quota failed"
4817
4818         # pool quotas don't work properly without global limit
4819         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4820                 error "set global limit failed"
4821
4822         pool_add $qpool || error "pool_add failed"
4823         pool_add_targets $qpool 0 $((OSTCOUNT - 1)) ||
4824                 error "pool_add_targets failed"
4825
4826         log "set default quota for $qpool"
4827         $LFS setquota -U --pool $qpool -b ${limit}M -B ${limit}M $DIR ||
4828                 error "set default quota failed"
4829
4830         log "Write from user that hasn't lqe"
4831         # Check that it doesn't cause a panic or a deadlock
4832         # due to nested lqe lookups that rewrite 1st lqe in qti_lqes array.
4833         # Have to use RUNAS_ID as resetquota creates lqes in
4834         # the beginning for TSTUSR/TSTUSR2 when sets limits to 0.
4835         runas -u $RUNAS_ID -g $RUNAS_GID $DD of=$TESTFILE1 count=10
4836
4837         cancel_lru_locks osc
4838         sync; sync_all_data || true
4839 }
4840 run_test 73b "default OST Pool Quotas limit for new user"
4841
4842 test_74()
4843 {
4844         local global_limit=200 # 200M
4845         local limit=10 # 10M
4846         local limit2=50 # 50M
4847         local qpool="qpool1"
4848         local qpool2="qpool2"
4849         local tmp=0
4850
4851         mds_supports_qp
4852         setup_quota_test || error "setup quota failed with $?"
4853
4854         # enable ost quota
4855         set_ost_qtype $QTYPE || error "enable ost quota failed"
4856
4857         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4858                 error "set user quota failed"
4859
4860         pool_add $qpool || error "pool_add failed"
4861         pool_add_targets $qpool 0 1 ||
4862                 error "pool_add_targets failed"
4863
4864         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4865                 error "set user quota failed"
4866
4867         pool_add $qpool2 || error "pool_add failed"
4868         pool_add_targets $qpool2 1 1 ||
4869                 error "pool_add_targets failed"
4870
4871         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
4872                 error "set user quota failed"
4873
4874         tmp=$(getquota -u $TSTUSR global bhardlimit)
4875         [ $tmp -eq $((global_limit * 1024)) ] ||
4876                 error "wrong global limit $global_limit"
4877
4878         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool)
4879         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit $tmp for $qpool"
4880
4881         tmp=$(getquota -u $TSTUSR global bhardlimit $qpool2)
4882         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit $tmp for $qpool2"
4883
4884         # check limits in pools
4885         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
4886               grep -A4 $qpool | awk 'NR == 4{print $4}')
4887         echo "pool limit for $qpool $tmp"
4888         [ $tmp -eq $((limit * 1024)) ] || error "wrong limit:tmp for $qpool"
4889         tmp=$($LFS quota -u $TSTUSR --pool $DIR | \
4890               grep -A4 $qpool2 | awk 'NR == 4{print $4}')
4891         echo "pool limit for $qpool2 $tmp"
4892         [ $tmp -eq $((limit2 * 1024)) ] || error "wrong limit:$tmp for $qpool2"
4893 }
4894 run_test 74 "check quota pools per user"
4895
4896 function cleanup_quota_test_75()
4897 {
4898         do_facet mgs $LCTL nodemap_modify --name default \
4899                 --property admin --value 1
4900         do_facet mgs $LCTL nodemap_modify --name default \
4901                 --property trusted --value 1
4902         do_facet mgs $LCTL nodemap_modify --name default \
4903                 --property squash_uid --value 99
4904         do_facet mgs $LCTL nodemap_modify --name default \
4905                 --property squash_gid --value 99
4906
4907         wait_nm_sync default admin_nodemap
4908         wait_nm_sync default trusted_nodemap
4909
4910         do_facet mgs $LCTL nodemap_activate 0
4911         wait_nm_sync active
4912
4913         resetquota -u $TSTUSR
4914 }
4915
4916 test_dom_75() {
4917         local dd_failed=false
4918         local LIMIT=20480 #20M
4919         local qid=$TSTID
4920
4921         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
4922                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
4923                         oflag=sync || dd_failed=true
4924         done
4925
4926         $dd_failed && quota_error u $qid "write failed, expect succeed (1)"
4927
4928         for ((i = $((LIMIT/2048-1)); i < $((LIMIT/1024 + 10)); i++)); do
4929                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
4930                         oflag=sync || dd_failed=true
4931         done
4932
4933         $dd_failed || quota_error u $qid "write succeed, expect EDQUOT (1)"
4934
4935         rm -f $DIR/$tdir_dom/*
4936
4937         # flush cache, ensure noquota flag is set on client
4938         cancel_lru_locks
4939         sync; sync_all_data || true
4940
4941         dd_failed=false
4942
4943         $DD of=$DIR/$tdir/file count=$((LIMIT/2048-1)) oflag=sync ||
4944                 quota_error u $qid "write failed, expect succeed (2)"
4945
4946         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4947                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
4948                         oflag=sync || dd_failed=true
4949         done
4950
4951         $dd_failed || quota_error u $TSTID "write succeed, expect EDQUOT (2)"
4952
4953         rm -f $DIR/$tdir/*
4954         rm -f $DIR/$tdir_dom/*
4955
4956         # flush cache, ensure noquota flag is set on client
4957         cancel_lru_locks
4958         sync; sync_all_data || true
4959
4960         dd_failed=false
4961
4962         for ((i = 0; i < $((LIMIT/2048-1)); i++)); do
4963                 $DD of=$DIR/$tdir_dom/$tfile-$i count=1 \
4964                         oflag=sync || dd_failed=true
4965         done
4966
4967         $dd_failed && quota_error u $qid "write failed, expect succeed (3)"
4968
4969         $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4970                 quota_error u $qid "write succeed, expect EDQUOT (3)"
4971         true
4972 }
4973
4974 test_75()
4975 {
4976         local soft_limit=10 # MB
4977         local hard_limit=20 # MB
4978         local limit=$soft_limit
4979         local testfile="$DIR/$tdir/$tfile-0"
4980         local grace=20 # seconds
4981         local tdir_dom=${tdir}_dom
4982
4983         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
4984             grace=60
4985         fi
4986
4987         setup_quota_test || error "setup quota failed with $?"
4988         stack_trap cleanup_quota_test_75 EXIT
4989
4990         # enable ost quota
4991         set_ost_qtype $QTYPE || error "enable ost quota failed"
4992         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4993
4994         local used=$(getquota -u $TSTID global curspace)
4995         $LFS setquota -t -u --block-grace $grace --inode-grace \
4996                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
4997         $LFS setquota -u $TSTUSR -b $((soft_limit+used/1024))M \
4998                         -B $((hard_limit+used/1024))M -i 0 -I 0 $DIR ||
4999                 error "set user quota failed"
5000
5001         chmod 777 $DIR/$tdir || error "chmod 777 $DIR/$tdir failed"
5002         mkdir $DIR/$tdir_dom
5003         chmod 777 $DIR/$tdir_dom
5004         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
5005                 error "setstripe $tdir_dom failed"
5006
5007         do_facet mgs $LCTL nodemap_activate 1
5008         wait_nm_sync active
5009         do_facet mgs $LCTL nodemap_modify --name default \
5010                 --property admin --value 0
5011         do_facet mgs $LCTL nodemap_modify --name default \
5012                 --property trusted --value 0
5013         do_facet mgs $LCTL nodemap_modify --name default \
5014                 --property deny_unknown --value 0
5015         do_facet mgs $LCTL nodemap_modify --name default \
5016                 --property squash_uid --value $TSTID
5017         do_facet mgs $LCTL nodemap_modify --name default \
5018                 --property squash_gid --value $TSTID
5019         cancel_lru_locks mdc
5020         wait_nm_sync default admin_nodemap
5021         wait_nm_sync default trusted_nodemap
5022         wait_nm_sync default squash_uid
5023
5024         # mmap write when over soft limit
5025         limit=$soft_limit
5026         $DD of=$testfile count=${limit} ||
5027                 quota_error a  "root write failure, but expect success (1)"
5028         OFFSET=$((limit * 1024))
5029         cancel_lru_locks osc
5030
5031         echo "Write to exceed soft limit"
5032         dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
5033               quota_error a $TSTUSR "root write failure, but expect success (2)"
5034         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
5035         cancel_lru_locks osc
5036
5037         echo "mmap write when over soft limit"
5038         $MULTIOP $testfile.mmap OT40960SMW ||
5039                 quota_error a $TSTUSR "mmap write failure, but expect success"
5040         cancel_lru_locks osc
5041         rm -f $testfile*
5042         wait_delete_completed || error "wait_delete_completed failed (1)"
5043         sync_all_data || true
5044
5045         # test for user hard limit
5046         limit=$hard_limit
5047         log "Write..."
5048         $DD of=$testfile bs=1M count=$((limit/2)) ||
5049                 quota_error u $TSTID \
5050                         "root write failure, but expect success (3)"
5051
5052         log "Write out of block quota ..."
5053         # possibly a cache write, ignore failure
5054         $DD of=$testfile bs=1M count=$((limit/2)) seek=$((limit/2)) || true
5055         # flush cache, ensure noquota flag is set on client
5056         cancel_lru_locks osc
5057         sync; sync_all_data || true
5058         # sync forced cache flush, but did not guarantee that slave
5059         # got new edquot through glimpse, so wait to make sure
5060         sleep 5
5061         $DD of=$testfile bs=1M count=1 seek=$limit conv=fsync &&
5062                 quota_error u $TSTID \
5063                         "user write success, but expect EDQUOT"
5064         rm -f $testfile
5065         wait_delete_completed || error "wait_delete_completed failed (2)"
5066         sync_all_data || true
5067         [ $(getquota -u $TSTUSR global curspace) -eq $used ] ||
5068                 quota_error u $TSTID "user quota not released after deletion"
5069
5070         test_dom_75
5071 }
5072 run_test 75 "nodemap squashed root respects quota enforcement"
5073
5074 test_76() {
5075         ! is_project_quota_supported &&
5076                 skip "skip project quota unsupported"
5077
5078         setup_quota_test || error "setup quota failed with $?"
5079         quota_init
5080
5081         local testfile="$DIR/$tdir/$tfile-0"
5082
5083         touch $testfile
5084         $LFS project -p 4294967295 $testfile &&
5085                 error "set project ID should fail"
5086         return 0
5087 }
5088 run_test 76 "project ID 4294967295 should be not allowed"
5089
5090 test_77()
5091 {
5092         mount_client $MOUNT2 "ro"
5093         lfs setquota -u quota_usr -b 100M -B 100M -i 10K -I 10K $MOUNT2 &&
5094                 error "lfs setquota should fail in read-only Lustre mount"
5095         umount $MOUNT2
5096 }
5097 run_test 77 "lfs setquota should fail in Lustre mount with 'ro'"
5098
5099 test_78()
5100 {
5101         (( $OST1_VERSION >= $(version_code 2.14.55) )) ||
5102                 skip "need OST at least 2.14.55"
5103         check_set_fallocate_or_skip
5104
5105         setup_quota_test || error "setup quota failed with $?"
5106
5107         # enable ost quota
5108         set_ost_qtype $QTYPE || error "enable ost quota failed"
5109
5110         mkdir -p $DIR/$tdir || error "failed to create $tdir"
5111         chown $TSTUSR $DIR/$tdir || error "failed to chown $tdir"
5112
5113         # setup quota limit
5114         $LFS setquota -u $TSTUSR -b25M -B25M $DIR/$tdir ||
5115                 error "lfs setquota failed"
5116
5117         # call fallocate
5118         runas -u $TSTUSR -g $TSTUSR fallocate -l 204800 $DIR/$tdir/$tfile
5119
5120         kbytes=$(lfs quota -u $TSTUSR $DIR |
5121                 awk -v pattern=$DIR 'match($0, pattern) {printf $2}')
5122         echo "kbytes returned:$kbytes"
5123
5124         # For file size of 204800. We should be having roughly 200 kbytes
5125         # returned. Anything alarmingly low (50 taken as arbitrary value)
5126         # would bail out this TC. Also this also avoids $kbytes of 0
5127         # to be used in calculation below.
5128         (( $kbytes > 50 )) ||
5129                 error "fallocate did not use quota. kbytes returned:$kbytes"
5130
5131         local expect_lo=$(($kbytes * 95 / 100)) # 5% below
5132         local expect_hi=$(($kbytes * 105 / 100)) # 5% above
5133
5134         # Verify kbytes is 200 (204800/1024). With a permited  5% drift
5135         (( $kbytes >= $expect_lo && $kbytes <= $expect_hi )) ||
5136                 error "fallocate did not use quota correctly"
5137 }
5138 run_test 78 "Check fallocate increase quota usage"
5139
5140 test_79()
5141 {
5142         local qpool="qpool1"
5143         local cmd="$LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$qpool.info"
5144         local stopf=$TMP/$tfile
5145
5146         do_facet mds1 "touch $stopf"
5147         stack_trap "do_facet mds1 'rm -f $stopf'"
5148         do_facet mds1 "while [ -e $stopf ]; do $cmd &>/dev/null; done"&
5149         local pid=$!
5150         pool_add $qpool || error "pool_add failed"
5151         do_facet mds1 "rm $stopf"
5152         wait $pid
5153 }
5154 run_test 79 "access to non-existed dt-pool/info doesn't cause a panic"
5155
5156 test_80()
5157 {
5158         local dir1="$DIR/$tdir/dir1"
5159         local dir2="$DIR/$tdir/dir2"
5160         local TESTFILE0="$dir1/$tfile-0"
5161         local TESTFILE1="$dir1/$tfile-1"
5162         local TESTFILE2="$dir1/$tfile-2"
5163         local TESTFILE3="$dir2/$tfile-0"
5164         local global_limit=100 # 100M
5165         local limit=10 # 10M
5166         local qpool="qpool1"
5167
5168         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
5169         mds_supports_qp
5170         [ "$ost1_FSTYPE" == zfs ] &&
5171                 skip "ZFS grants some block space together with inode"
5172         setup_quota_test || error "setup quota failed with $?"
5173         set_ost_qtype $QTYPE || error "enable ost quota failed"
5174
5175         # make sure the system is clean
5176         local used=$(getquota -u $TSTUSR global curspace)
5177         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR is not 0."
5178
5179         pool_add $qpool || error "pool_add failed"
5180         pool_add_targets $qpool 0 1 ||
5181                 error "pool_add_targets failed"
5182
5183         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
5184                 error "set user quota failed"
5185
5186         $LFS setquota -u $TSTUSR -B ${global_limit}M --pool $qpool $DIR ||
5187                 error "set user quota failed"
5188         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
5189                 error "set user quota failed"
5190
5191         mkdir -p $dir1 || error "failed to mkdir"
5192         chown $TSTUSR.$TSTUSR $dir1 || error "chown $dir1 failed"
5193         mkdir -p $dir2 || error "failed to mkdir"
5194         chown $TSTUSR.$TSTUSR $dir2 || error "chown $dir2 failed"
5195
5196         $LFS setstripe $dir1 -i 1 -c 1|| error "setstripe $testfile failed"
5197         $LFS setstripe $dir2 -i 0 -c 1|| error "setstripe $testfile failed"
5198         lfs getstripe $dir1
5199         lfs getstripe $dir2
5200         sleep 3
5201
5202         $LFS quota -uv $TSTUSR $DIR
5203         #define OBD_FAIL_QUOTA_PREACQ            0xA06
5204         do_facet mds1 $LCTL set_param fail_loc=0xa06
5205         $RUNAS $DD of=$TESTFILE3 count=3 ||
5206                 quota_error u $TSTUSR "write failed"
5207         $RUNAS $DD of=$TESTFILE2 count=7 ||
5208                 quota_error u $TSTUSR "write failed"
5209         $RUNAS $DD of=$TESTFILE1 count=1 oflag=direct ||
5210                 quota_error u $TSTUSR "write failed"
5211         sync
5212         sleep 3
5213         $LFS quota -uv --pool $qpool $TSTUSR $DIR
5214
5215         rm -f $TESTFILE2
5216         stop ost2
5217         do_facet mds1 $LCTL set_param fail_loc=0
5218         start ost2 $(ostdevname 2) $OST_MOUNT_OPTS || error "start ost2 failed"
5219         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5220         # OST0 needs some time to update quota usage after removing TESTFILE2
5221         sleep 4
5222         $LFS quota -uv $TSTUSR --pool $qpool $DIR
5223         $RUNAS $DD of=$TESTFILE0 count=2 oflag=direct ||
5224                 quota_error u $TSTUSR "write failure, but expect success"
5225 }
5226 run_test 80 "check for EDQUOT after OST failover"
5227
5228 test_81() {
5229         local global_limit=20  # 100M
5230         local testfile="$DIR/$tdir/$tfile-0"
5231         local qpool="qpool1"
5232
5233         mds_supports_qp
5234         setup_quota_test || error "setup quota failed with $?"
5235
5236         # enable ost quota
5237         set_ost_qtype $QTYPE || error "enable ost quota failed"
5238
5239         # test for user
5240         log "User quota (block hardlimit:$global_limit MB)"
5241         $LFS setquota -u $TSTUSR -B 1G $DIR || error "set user quota failed"
5242
5243         pool_add $qpool || error "pool_add failed"
5244         #define OBD_FAIL_QUOTA_RECALC   0xA07
5245         do_facet mds1 $LCTL set_param fail_loc=0x80000A07 fail_val=30
5246         # added OST casues to start pool recalculation
5247         pool_add_targets $qpool 0 0 1
5248         stop mds1 -f || error "MDS umount failed"
5249
5250         #start mds1 back to destroy created pool
5251         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
5252         clients_up || true
5253 }
5254 run_test 81 "Race qmt_start_pool_recalc with qmt_pool_free"
5255
5256 quota_fini()
5257 {
5258         do_nodes $(comma_list $(nodes_list)) \
5259                 "lctl set_param -n debug=-quota,trace"
5260         if $PQ_CLEANUP; then
5261                 disable_project_quota
5262         fi
5263 }
5264 reset_quota_settings
5265 quota_fini
5266
5267 cd $ORIG_PWD
5268 complete $SECONDS
5269 check_and_cleanup_lustre
5270 export QUOTA_AUTO=$QUOTA_AUTO_OLD
5271 exit_status