Whamcloud - gitweb
LU-6142 tests: Fix style issues for mmap_sanity.c
[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:  LU-5152
17 ALWAYS_EXCEPT+="                55"
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
43 require_dsh_mds || exit 0
44 require_dsh_ost || exit 0
45
46 # Does e2fsprogs support quota feature?
47 if [ "$mds1_FSTYPE" == ldiskfs ] &&
48         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
49                 grep -q 'quota'"; then
50         skip_env "e2fsprogs doesn't support quota"
51 fi
52
53 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
54
55 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
56
57 DIR=${DIR:-$MOUNT}
58 DIR2=${DIR2:-$MOUNT2}
59
60 QUOTA_AUTO_OLD=$QUOTA_AUTO
61 export QUOTA_AUTO=0
62
63 check_and_setup_lustre
64
65 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
66
67 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
68 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
69 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
70 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
71 SHOW_QUOTA_PROJID="eval is_project_quota_supported && $LFS quota -v -p $TSTPRJID $DIR"
72 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
73 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
74 SHOW_QUOTA_INFO_PROJID="eval is_project_quota_supported && $LFS quota -t -p $DIR"
75
76 lustre_fail() {
77         local fail_node=$1
78         local fail_loc=$2
79         local fail_val=${3:-0}
80         local NODES=
81
82         case $fail_node in
83         mds_ost|mdt_ost) NODES="$(comma_list $(mdts_nodes) $(osts_nodes))";;
84         mds|mdt) NODES="$(comma_list $(mdts_nodes))";;
85         ost) NODES="$(comma_list $(osts_nodes))";;
86         esac
87
88         do_nodes $NODES "lctl set_param fail_val=$fail_val fail_loc=$fail_loc"
89 }
90
91 change_project()
92 {
93         echo "lfs project $*"
94         lfs project $* || error "lfs project $* failed"
95 }
96
97 RUNAS="runas -u $TSTID -g $TSTID"
98 RUNAS2="runas -u $TSTID2 -g $TSTID2"
99 DD="dd if=/dev/zero bs=1M"
100
101 FAIL_ON_ERROR=false
102
103 # clear quota limits for a user or a group
104 # usage: resetquota -u username
105 #        resetquota -g groupname
106 #        resetquota -p projid
107
108 resetquota() {
109         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
110         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
111                 error "resetquota: wrong specifier $1 passed"
112
113         if [ $1 == "-p" ]; then
114                 is_project_quota_supported || return 0
115         fi
116
117         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
118                 error "clear quota for [type:$1 name:$2] failed"
119         # give a chance to slave to release space
120         sleep 1
121 }
122
123 quota_scan() {
124         local local_ugp=$1
125         local local_id=$2
126
127         if [ "$local_ugp" == "a" -o "$local_ugp" == "u" ]; then
128                 $LFS quota -v -u $local_id $DIR
129                 log "Files for user ($local_id):"
130                 ($LFS find --user $local_id $DIR | head -n 4 |
131                         xargs stat 2>/dev/null)
132         fi
133
134         if [ "$local_ugp" == "a" -o "$local_ugp" == "g" ]; then
135                 $LFS quota -v -g $local_id $DIR
136                 log "Files for group ($local_id):"
137                 ($LFS find --group $local_id $DIR | head -n 4 |
138                         xargs stat 2>/dev/null)
139         fi
140
141         if [ "$local_ugp" == "a" -o "$local_ugp" == "p" ]; then
142                 $LFS quota -v -p $TSTPRJID $DIR
143                 log "Files for project ($TSTPRJID):"
144                 ($LFS find --projid $TSTPRJID $DIR | head -n 4 |
145                         xargs stat 2>/dev/null)
146         fi
147 }
148
149 quota_error() {
150         quota_scan $1 $2
151         shift 2
152         error "$*"
153 }
154
155 quota_log() {
156         quota_scan $1 $2
157         shift 2
158         log "$*"
159 }
160
161 # get quota for a user or a group
162 # usage: getquota -u|-g|-p <username>|<groupname>|<projid> global|<obd_uuid> \
163 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
164 getquota() {
165         local spec
166         local uuid
167
168         sync_all_data > /dev/null 2>&1 || true
169
170         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
171         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
172                 error "getquota: wrong u/g/p specifier $1 passed"
173
174         uuid="$3"
175
176         case "$4" in
177                 curspace)   spec=1;;
178                 bsoftlimit) spec=2;;
179                 bhardlimit) spec=3;;
180                 bgrace)     spec=4;;
181                 curinodes)  spec=5;;
182                 isoftlimit) spec=6;;
183                 ihardlimit) spec=7;;
184                 igrace)     spec=8;;
185                 *)          error "unknown quota parameter $4";;
186         esac
187
188         [ "$uuid" = "global" ] && uuid=$DIR
189
190         $LFS quota -v "$1" "$2" $DIR |
191                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
192                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
193                 | tr -d "*"
194 }
195
196 # set mdt quota type
197 # usage: set_mdt_qtype ugp|u|g|p|none
198 set_mdt_qtype() {
199         local qtype=$1
200         local varsvc
201         local mdts=$(get_facets MDS)
202         local cmd
203         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
204                 qtype=$(tr -d 'p' <<<$qtype)
205
206         if [[ $PERM_CMD == *"set_param -P"* ]]; then
207                 do_facet mgs $PERM_CMD \
208                         osd-*.$FSNAME-MDT*.quota_slave.enable=$qtype
209         else
210                 do_facet mgs $PERM_CMD $FSNAME.quota.mdt=$qtype
211         fi
212         # we have to make sure each MDT received config changes
213         for mdt in ${mdts//,/ }; do
214                 varsvc=${mdt}_svc
215                 cmd="$LCTL get_param -n "
216                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
217                 cmd=${cmd}.quota_slave.enabled
218
219                 if $(facet_up $mdt); then
220                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
221                 fi
222         done
223         return 0
224 }
225
226 # set ost quota type
227 # usage: set_ost_qtype ugp|u|g|p|none
228 set_ost_qtype() {
229         local qtype=$1
230         local varsvc
231         local osts=$(get_facets OST)
232         local cmd
233         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
234                 qtype=$(tr -d 'p' <<<$qtype)
235
236         if [[ $PERM_CMD == *"set_param -P"* ]]; then
237                 do_facet mgs $PERM_CMD \
238                         osd-*.$FSNAME-OST*.quota_slave.enable=$qtype
239         else
240                 do_facet mgs $PERM_CMD $FSNAME.quota.ost=$qtype
241         fi
242         # we have to make sure each OST received config changes
243         for ost in ${osts//,/ }; do
244                 varsvc=${ost}_svc
245                 cmd="$LCTL get_param -n "
246                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
247                 cmd=${cmd}.quota_slave.enabled
248
249                 if $(facet_up $ost); then
250                         wait_update_facet $ost "$cmd" "$qtype" || return 1
251                 fi
252         done
253         return 0
254 }
255
256 wait_reintegration() {
257         local ntype=$1
258         local qtype=$2
259         local max=$3
260         local result="glb[1],slv[1],reint[0]"
261         local varsvc
262         local cmd
263         local tgts
264
265         if [ $ntype == "mdt" ]; then
266                 tgts=$(get_facets MDS)
267         else
268                 tgts=$(get_facets OST)
269         fi
270
271         for tgt in ${tgts//,/ }; do
272                 varsvc=${tgt}_svc
273                 cmd="$LCTL get_param -n "
274                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
275                 cmd=${cmd}.quota_slave.info
276
277                 if $(facet_up $tgt); then
278                         wait_update_facet $tgt "$cmd |
279                                 grep "$qtype" | awk '{ print \\\$3 }'" \
280                                         "$result" $max || return 1
281                 fi
282         done
283         return 0
284 }
285
286 wait_mdt_reint() {
287         local qtype=$1
288         local max=${2:-90}
289
290         if [[ "$qtype" =~ "u" ]]; then
291                 wait_reintegration "mdt" "user" $max || return 1
292         fi
293
294         if [[ "$qtype" =~ "g" ]]; then
295                 wait_reintegration "mdt" "group" $max || return 1
296         fi
297
298         if [[ "$qtype" =~ "p" ]]; then
299                 ! is_project_quota_supported && return 0
300                 wait_reintegration "mdt" "project" $max || return 1
301         fi
302         return 0
303 }
304
305 wait_ost_reint() {
306         local qtype=$1
307         local max=${2:-90}
308
309         if [[ "$qtype" =~ "u" ]]; then
310                 wait_reintegration "ost" "user" $max || return 1
311         fi
312
313         if [[ "$qtype" =~ "g" ]]; then
314                 wait_reintegration "ost" "group" $max || return 1
315         fi
316
317         if [[ "$qtype" =~ "p" ]]; then
318                 ! is_project_quota_supported && return 0
319                 wait_reintegration "ost" "project" $max || return 1
320         fi
321         return 0
322 }
323
324 wait_grace_time() {
325         local qtype=$1
326         local flavour=$2
327         local extrasleep=${3:-5}
328         local qarg
329
330         case $qtype in
331                 u|g) qarg=$TSTUSR ;;
332                 p) qarg=$TSTPRJID ;;
333                 *) error "get_grace_time: Invalid quota type: $qtype"
334         esac
335
336         case $flavour in
337                 block)
338                         time=$(lfs quota -$qtype $qarg $DIR|
339                                    awk 'NR == 3{ print $5 }'| sed 's/s$//')
340                         ;;
341                 file)
342                         time=$(lfs quota -$qtype $qarg $DIR|
343                                    awk 'NR == 3{ print $9 }'| sed 's/s$//')
344                         ;;
345                 *)
346                         error "Unknown quota type: $flavour"
347                         ;;
348         esac
349
350         echo "Sleep through grace ..."
351         [ "$time" == "-" ] &&
352             error "Grace timeout was not set or quota not exceeded"
353         if [ "$time" == "none" ]; then
354             echo "...Grace timeout already expired"
355         else
356                 let time+=$extrasleep
357                 echo "...sleep $time seconds"
358                 sleep $time
359         fi
360 }
361
362 setup_quota_test() {
363         wait_delete_completed
364         echo "Creating test directory"
365         mkdir $DIR/$tdir || return 1
366         chmod 0777 $DIR/$tdir || return 2
367         # always clear fail_loc in case of fail_loc isn't cleared
368         # properly when previous test failed
369         lustre_fail mds_ost 0
370 }
371
372 cleanup_quota_test() {
373         trap 0
374         echo "Delete files..."
375         rm -rf $DIR/$tdir
376         echo "Wait for unlink objects finished..."
377         wait_delete_completed
378         sync_all_data || true
379         reset_quota_settings
380 }
381
382 quota_show_check() {
383         local bf=$1
384         local ugp=$2
385         local qid=$3
386         local usage
387
388         $LFS quota -v -$ugp $qid $DIR
389
390         if [ "$bf" == "a" -o "$bf" == "b" ]; then
391                 usage=$(getquota -$ugp $qid global curspace)
392                 if [ -z $usage ]; then
393                         quota_error $ugp $qid \
394                                 "Query block quota failed ($ugp:$qid)."
395                 else
396                         [ $usage -ne 0 ] && quota_log $ugp $qid \
397                                 "Block quota isn't 0 ($ugp:$qid:$usage)."
398                 fi
399         fi
400
401         if [ "$bf" == "a" -o "$bf" == "f" ]; then
402                 usage=$(getquota -$ugp $qid global curinodes)
403                 if [ -z $usage ]; then
404                         quota_error $ugp $qid \
405                                 "Query file quota failed ($ugp:$qid)."
406                 else
407                         [ $usage -ne 0 ] && quota_log $ugp $qid \
408                                 "File quota isn't 0 ($ugp:$qid:$usage)."
409                 fi
410         fi
411 }
412
413 project_quota_enabled () {
414         local rc=0
415         for num in $(seq $MDSCOUNT); do
416                 do_facet mds$num $DEBUGFS -R features $(mdsdevname $num) |
417                         grep -q project || rc=1
418         done
419         for num in $(seq $OSTCOUNT); do
420                 do_facet ost$num $DEBUGFS -R features $(ostdevname $num) |
421                         grep -q project || rc=1
422         done
423         [ $rc -eq 0 ] && PQ_CLEANUP=false || PQ_CLEANUP=true
424         return $rc
425 }
426
427 project_quota_enabled || enable_project_quota
428
429 reset_quota_settings() {
430         resetquota -u $TSTUSR
431         resetquota -g $TSTUSR
432         resetquota -u $TSTUSR2
433         resetquota -g $TSTUSR2
434         resetquota -p $TSTPRJID
435 }
436
437 # enable quota debug
438 quota_init() {
439         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
440 }
441 quota_init
442 reset_quota_settings
443
444 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
445         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
446 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
447         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
448
449 test_quota_performance() {
450         local TESTFILE="$DIR/$tdir/$tfile-0"
451         local size=$1 # in MB
452         local stime=$(date +%s)
453         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
454                 quota_error u $TSTUSR "write failure"
455         local etime=$(date +%s)
456         delta=$((etime - stime))
457         if [ $delta -gt 0 ]; then
458                 rate=$((size * 1024 / delta))
459                 if [ "$mds1_FSTYPE" = zfs ]; then
460                         # LU-2872 - see LU-2887 for fix
461                         [ $rate -gt 64 ] ||
462                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
463                 else
464                         [ $rate -gt 1024 ] ||
465                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
466                 fi
467         fi
468         rm -f $TESTFILE
469 }
470
471 # test basic quota performance b=21696
472 test_0() {
473         local MB=100 # 100M
474         [ "$SLOW" = "no" ] && MB=10
475
476         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
477         [ $free_space -le $((MB * 1024)) ] &&
478                 skip "not enough space ${free_space} KB, " \
479                         "required $((MB * 1024)) KB"
480         setup_quota_test || error "setup quota failed with $?"
481         trap cleanup_quota_test EXIT
482
483         set_ost_qtype "none" || error "disable ost quota failed"
484         test_quota_performance $MB
485
486         set_ost_qtype $QTYPE || error "enable ost quota failed"
487         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
488                 error "set quota failed"
489         test_quota_performance $MB
490
491         cleanup_quota_test
492 }
493 run_test 0 "Test basic quota performance"
494
495 # test block hardlimit
496 test_1() {
497         local LIMIT=10  # 10M
498         local TESTFILE="$DIR/$tdir/$tfile-0"
499
500         setup_quota_test || error "setup quota failed with $?"
501         trap cleanup_quota_test EXIT
502
503         # enable ost quota
504         set_ost_qtype $QTYPE || error "enable ost quota failed"
505
506         # test for user
507         log "User quota (block hardlimit:$LIMIT MB)"
508         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
509                 error "set user quota failed"
510
511         # make sure the system is clean
512         local USED=$(getquota -u $TSTUSR global curspace)
513         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
514
515         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
516         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
517
518         log "Write..."
519         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
520                 quota_error u $TSTUSR "user write failure, but expect success"
521         log "Write out of block quota ..."
522         # this time maybe cache write,  ignore it's failure
523         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
524         # flush cache, ensure noquota flag is set on client
525         cancel_lru_locks osc
526         sync; sync_all_data || true
527         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
528                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
529
530         rm -f $TESTFILE
531         wait_delete_completed || error "wait_delete_completed failed"
532         sync_all_data || true
533         USED=$(getquota -u $TSTUSR global curspace)
534         [ $USED -ne 0 ] && quota_error u $TSTUSR \
535                 "user quota isn't released after deletion"
536         resetquota -u $TSTUSR
537
538         # test for group
539         log "--------------------------------------"
540         log "Group quota (block hardlimit:$LIMIT MB)"
541         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
542                 error "set group quota failed"
543
544         TESTFILE="$DIR/$tdir/$tfile-1"
545         # make sure the system is clean
546         USED=$(getquota -g $TSTUSR global curspace)
547         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTUSR isn't 0"
548
549         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
550         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
551
552         log "Write ..."
553         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
554                 quota_error g $TSTUSR "Group write failure, but expect success"
555         log "Write out of block quota ..."
556         # this time maybe cache write, ignore it's failure
557         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
558         cancel_lru_locks osc
559         sync; sync_all_data || true
560         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
561                 quota_error g $TSTUSR "Group write success, but expect EDQUOT"
562         rm -f $TESTFILE
563         wait_delete_completed || error "wait_delete_completed failed"
564         sync_all_data || true
565         USED=$(getquota -g $TSTUSR global curspace)
566         [ $USED -ne 0 ] && quota_error g $TSTUSR \
567                                 "Group quota isn't released after deletion"
568         resetquota -g $TSTUSR
569
570         if ! is_project_quota_supported; then
571                 echo "Project quota is not supported"
572                 cleanup_quota_test
573                 return 0
574         fi
575
576         TESTFILE="$DIR/$tdir/$tfile-2"
577         # make sure the system is clean
578         USED=$(getquota -p $TSTPRJID global curspace)
579         [ $USED -ne 0 ] &&
580                 error "used space($USED) for project $TSTPRJID isn't 0"
581
582         # test for Project
583         log "--------------------------------------"
584         log "Project quota (block hardlimit:$LIMIT mb)"
585         $LFS setquota -p $TSTPRJID -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
586                 error "set project quota failed"
587
588         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
589         chown $TSTUSR:$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
590         change_project -p $TSTPRJID $TESTFILE
591
592         log "write ..."
593         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) || quota_error p $TSTPRJID \
594                 "project write failure, but expect success"
595         log "write out of block quota ..."
596         # this time maybe cache write, ignore it's failure
597         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
598         cancel_lru_locks osc
599         sync; sync_all_data || true
600         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT && quota_error p \
601                 $TSTPRJID "project write success, but expect EDQUOT"
602
603         # cleanup
604         cleanup_quota_test
605
606         USED=$(getquota -p $TSTPRJID global curspace)
607         [ $USED -eq 0 ] || quota_error p $TSTPRJID \
608                 "project quota isn't released after deletion"
609 }
610 run_test 1 "Block hard limit (normal use and out of quota)"
611
612 # test inode hardlimit
613 test_2() {
614         local LIMIT=$((1024 * 1024)) # 1M inodes
615         local TESTFILE="$DIR/$tdir/$tfile-0"
616
617         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
618
619         local FREE_INODES=$(mdt_free_inodes 0)
620         echo "$FREE_INODES free inodes on master MDT"
621         [ $FREE_INODES -lt $LIMIT ] &&
622                 skip "not enough free inodes $FREE_INODES required $LIMIT"
623
624         setup_quota_test || error "setup quota failed with $?"
625         trap cleanup_quota_test EXIT
626
627         # enable mdt quota
628         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
629
630         # test for user
631         log "User quota (inode hardlimit:$LIMIT files)"
632         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
633                 error "set user quota failed"
634
635         # make sure the system is clean
636         local USED=$(getquota -u $TSTUSR global curinodes)
637         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
638
639         log "Create $LIMIT files ..."
640         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
641                 quota_error u $TSTUSR "user create failure, but expect success"
642         log "Create out of file quota ..."
643         $RUNAS touch ${TESTFILE}_xxx &&
644                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
645
646         # cleanup
647         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
648         rm -f ${TESTFILE}_xxx
649         wait_delete_completed
650
651         USED=$(getquota -u $TSTUSR global curinodes)
652         [ $USED -ne 0 ] && quota_error u $TSTUSR \
653                 "user quota isn't released after deletion"
654         resetquota -u $TSTUSR
655
656         # test for group
657         log "--------------------------------------"
658         log "Group quota (inode hardlimit:$LIMIT files)"
659         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
660                 error "set group quota failed"
661
662         TESTFILE=$DIR/$tdir/$tfile-1
663         # make sure the system is clean
664         USED=$(getquota -g $TSTUSR global curinodes)
665         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
666
667         log "Create $LIMIT files ..."
668         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
669                 quota_error g $TSTUSR "group create failure, but expect success"
670         log "Create out of file quota ..."
671         $RUNAS touch ${TESTFILE}_xxx &&
672                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
673
674         # cleanup
675         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
676         rm -f ${TESTFILE}_xxx
677         wait_delete_completed
678
679         USED=$(getquota -g $TSTUSR global curinodes)
680         [ $USED -ne 0 ] && quota_error g $TSTUSR \
681                 "user quota isn't released after deletion"
682
683         resetquota -g $TSTUSR
684         ! is_project_quota_supported && cleanup_quota_test &&
685                 echo "Skip project quota is not supported" && return 0
686
687         # test for project
688         log "--------------------------------------"
689         log "Project quota (inode hardlimit:$LIMIT files)"
690         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
691                 error "set project quota failed"
692
693         TESTFILE=$DIR/$tdir/$tfile-1
694         # make sure the system is clean
695         USED=$(getquota -p $TSTPRJID global curinodes)
696         [ $USED -ne 0 ] &&
697                 error "Used inodes($USED) for project $TSTPRJID isn't 0"
698
699         change_project -sp $TSTPRJID $DIR/$tdir
700         log "Create $LIMIT files ..."
701         $RUNAS createmany -m ${TESTFILE} $((LIMIT-1)) || quota_error p \
702                 $TSTPRJID "project create fail, but expect success"
703         log "Create out of file quota ..."
704         $RUNAS touch ${TESTFILE}_xxx && quota_error p $TSTPRJID \
705                 "project create success, but expect EDQUOT"
706         change_project -C $DIR/$tdir
707
708         cleanup_quota_test
709         USED=$(getquota -p $TSTPRJID global curinodes)
710         [ $USED -eq 0 ] || quota_error p $TSTPRJID \
711                 "project quota isn't released after deletion"
712
713 }
714 run_test 2 "File hard limit (normal use and out of quota)"
715
716 test_block_soft() {
717         local TESTFILE=$1
718         local GRACE=$2
719         local LIMIT=$3
720         local OFFSET=0
721         local qtype=$4
722
723         setup_quota_test
724         trap cleanup_quota_test EXIT
725
726         $LFS setstripe $TESTFILE -c 1 -i 0
727         chown $TSTUSR.$TSTUSR $TESTFILE
728         [ "$qtype" == "p" ] && is_project_quota_supported &&
729                 change_project -p $TSTPRJID $TESTFILE
730
731         echo "Write up to soft limit"
732         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
733                 quota_error a $TSTUSR "write failure, but expect success"
734         OFFSET=$((LIMIT * 1024))
735         cancel_lru_locks osc
736
737         echo "Write to exceed soft limit"
738         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
739                 quota_error a $TSTUSR "write failure, but expect success"
740         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
741         cancel_lru_locks osc
742
743         $SHOW_QUOTA_USER
744         $SHOW_QUOTA_GROUP
745         $SHOW_QUOTA_PROJID
746         $SHOW_QUOTA_INFO_USER
747         $SHOW_QUOTA_INFO_GROUP
748         $SHOW_QUOTA_INFO_PROJID
749
750         echo "Write before timer goes off"
751         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
752                 quota_error a $TSTUSR "write failure, but expect success"
753         OFFSET=$((OFFSET + 1024))
754         cancel_lru_locks osc
755
756         wait_grace_time $qtype "block"
757
758         $SHOW_QUOTA_USER
759         $SHOW_QUOTA_GROUP
760         $SHOW_QUOTA_PROJID
761         $SHOW_QUOTA_INFO_USER
762         $SHOW_QUOTA_INFO_GROUP
763         $SHOW_QUOTA_INFO_PROJID
764
765         echo "Write after timer goes off"
766         # maybe cache write, ignore.
767         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
768         OFFSET=$((OFFSET + 1024))
769         cancel_lru_locks osc
770         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
771                 quota_error a $TSTUSR "write success, but expect EDQUOT"
772
773         $SHOW_QUOTA_USER
774         $SHOW_QUOTA_GROUP
775         $SHOW_QUOTA_PROJID
776         $SHOW_QUOTA_INFO_USER
777         $SHOW_QUOTA_INFO_GROUP
778         $SHOW_QUOTA_INFO_PROJID
779
780         echo "Unlink file to stop timer"
781         rm -f $TESTFILE
782         wait_delete_completed
783         sync_all_data || true
784
785         $SHOW_QUOTA_USER
786         $SHOW_QUOTA_GROUP
787         $SHOW_QUOTA_PROJID
788         $SHOW_QUOTA_INFO_USER
789         $SHOW_QUOTA_INFO_GROUP
790         $SHOW_QUOTA_INFO_PROJID
791
792         $LFS setstripe $TESTFILE -c 1 -i 0
793         chown $TSTUSR.$TSTUSR $TESTFILE
794         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $TESTFILE
795
796         echo "Write ..."
797         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
798                 quota_error a $TSTUSR "write failure, but expect success"
799         # cleanup
800         cleanup_quota_test
801 }
802
803 # block soft limit
804 test_3() {
805         local GRACE=20 # 20s
806         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
807             GRACE=60
808         fi
809         local TESTFILE=$DIR/$tdir/$tfile-0
810
811         # get minimum soft qunit size
812         local LIMIT=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
813                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
814
815         set_ost_qtype $QTYPE || error "enable ost quota failed"
816
817         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
818         # make sure the system is clean
819         local USED=$(getquota -u $TSTUSR global curspace)
820         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
821
822         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
823                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
824         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
825                 error "set user quota failed"
826
827         test_block_soft $TESTFILE $GRACE $LIMIT "u"
828
829         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
830         TESTFILE=$DIR/$tdir/$tfile-1
831         # make sure the system is clean
832         USED=$(getquota -g $TSTUSR global curspace)
833         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
834
835         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
836                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
837         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
838                 error "set group quota failed"
839
840         test_block_soft $TESTFILE $GRACE $LIMIT "g"
841
842         if is_project_quota_supported; then
843                 echo "Project quota (soft limit:$LIMIT MB  grace:$GRACE sec)"
844                 TESTFILE=$DIR/$tdir/$tfile-2
845                 # make sure the system is clean
846                 USED=$(getquota -p $TSTPRJID global curspace)
847                 [ $USED -ne 0 ] && error \
848                         "Used space($USED) for project $TSTPRJID isn't 0."
849
850                 $LFS setquota -t -p --block-grace $GRACE --inode-grace \
851                         $MAX_IQ_TIME $DIR ||
852                                 error "set project grace time failed"
853                 $LFS setquota -p $TSTPRJID -b ${LIMIT}M -B 0 -i 0 -I 0 \
854                         $DIR || error "set project quota failed"
855
856                 test_block_soft $TESTFILE $GRACE $LIMIT "p"
857                 resetquota -p $TSTPRJID
858                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
859                         $MAX_IQ_TIME $DIR ||
860                                 error "restore project grace time failed"
861         fi
862
863         # cleanup
864         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
865                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
866         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
867                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
868 }
869 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
870
871 test_file_soft() {
872         local TESTFILE=$1
873         local LIMIT=$2
874         local grace=$3
875         local qtype=$4
876
877         setup_quota_test
878         trap cleanup_quota_test EXIT
879         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
880
881         echo "Create files to exceed soft limit"
882         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
883                 quota_error a $TSTUSR "create failure, but expect success"
884         local trigger_time=$(date +%s)
885
886         sync_all_data || true
887
888         local cur_time=$(date +%s)
889         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
890                 error "Passed grace time $grace, $trigger_time, $cur_time"
891
892         echo "Create file before timer goes off"
893         $RUNAS touch ${TESTFILE}_before ||
894                 quota_error a $TSTUSR "failed create before timer expired," \
895                         "but expect success. $trigger_time, $cur_time"
896         sync_all_data || true
897
898         wait_grace_time $qtype "file"
899
900         $SHOW_QUOTA_USER
901         $SHOW_QUOTA_GROUP
902         $SHOW_QUOTA_PROJID
903         $SHOW_QUOTA_INFO_USER
904         $SHOW_QUOTA_INFO_GROUP
905         $SHOW_QUOTA_INFO_PROJID
906
907         echo "Create file after timer goes off"
908         # There is a window that space is accounted in the quota usage but
909         # hasn't been decreased from the pending write, if we acquire quota
910         # in this window, we'll acquire more than we needed.
911         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
912         sync_all_data || true
913         $RUNAS touch ${TESTFILE}_after_3 &&
914                 quota_error a $TSTUSR "create after timer expired," \
915                         "but expect EDQUOT"
916         sync_all_data || true
917
918         $SHOW_QUOTA_USER
919         $SHOW_QUOTA_GROUP
920         $SHOW_QUOTA_PROJID
921         $SHOW_QUOTA_INFO_USER
922         $SHOW_QUOTA_INFO_GROUP
923         $SHOW_QUOTA_INFO_PROJID
924
925         echo "Unlink files to stop timer"
926         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
927         wait_delete_completed
928
929         echo "Create file"
930         $RUNAS touch ${TESTFILE}_xxx ||
931                 quota_error a $TSTUSR "touch after timer stop failure," \
932                         "but expect success"
933         sync_all_data || true
934
935         # cleanup
936         cleanup_quota_test
937 }
938
939 # file soft limit
940 test_4a() {
941         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
942                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
943         local TESTFILE=$DIR/$tdir/$tfile-0
944         local GRACE=12
945
946         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
947
948         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
949         # make sure the system is clean
950         local USED=$(getquota -u $TSTUSR global curinodes)
951         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
952
953         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
954                 $GRACE $DIR || error "set user grace time failed"
955         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
956                 error "set user quota failed"
957
958         [ "$mds1_FSTYPE" = zfs ] && GRACE=20
959
960         test_file_soft $TESTFILE $LIMIT $GRACE "u"
961
962         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
963         # make sure the system is clean
964         USED=$(getquota -g $TSTUSR global curinodes)
965         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
966
967         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
968                 $GRACE $DIR || error "set group grace time failed"
969         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
970                 error "set group quota failed"
971         TESTFILE=$DIR/$tdir/$tfile-1
972
973         test_file_soft $TESTFILE $LIMIT $GRACE "g"
974
975         if is_project_quota_supported; then
976                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
977                 # make sure the system is clean
978                 USED=$(getquota -p $TSTPRJID global curinodes)
979                 [ $USED -ne 0 ] && error \
980                         "Used space($USED) for project $TSTPRJID isn't 0."
981
982                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
983                         $GRACE $DIR || error "set project grace time failed"
984                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
985                         error "set project quota failed"
986
987                 TESTFILE=$DIR/$tdir/$tfile-1
988                 # one less than limit, because of parent directory included.
989                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
990                 resetquota -p $TSTPRJID
991                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
992                         $MAX_IQ_TIME $DIR ||
993                                 error "restore project grace time failed"
994         fi
995
996         # cleanup
997         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
998                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
999         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1000                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1001 }
1002 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1003
1004 test_4b() {
1005         local GR_STR1="1w3d"
1006         local GR_STR2="1000s"
1007         local GR_STR3="5s"
1008         local GR_STR4="1w2d3h4m5s"
1009         local GR_STR5="5c"
1010         local GR_STR6="18446744073709551615"
1011         local GR_STR7="-1"
1012
1013         wait_delete_completed
1014
1015         # test of valid grace strings handling
1016         echo "Valid grace strings test"
1017         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1018                 $GR_STR2 $DIR || error "set user grace time failed"
1019         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1020         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1021                 $GR_STR4 $DIR || error "set group grace time quota failed"
1022         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1023
1024         # test of invalid grace strings handling
1025         echo "  Invalid grace strings test"
1026         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1027         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1028         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1029                 $GR_STR7 $DIR
1030
1031         # cleanup
1032         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1033                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1034         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1035                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1036 }
1037 run_test 4b "Grace time strings handling"
1038
1039 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1040 test_5() {
1041         local BLIMIT=10 # 10M
1042         local ILIMIT=10 # 10 inodes
1043
1044         setup_quota_test || error "setup quota failed with $?"
1045         trap cleanup_quota_test EXIT
1046
1047         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1048         set_ost_qtype $QTYPE || error "enable ost quota failed"
1049
1050         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1051         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1052                 error "set user quota failed"
1053         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1054         if is_project_quota_supported; then
1055                 error "set group quota failed"
1056                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1057                         -I $ILIMIT $DIR || error "set project quota failed"
1058         fi
1059
1060         # make sure the system is clean
1061         local USED=$(getquota -u $TSTUSR global curinodes)
1062         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1063         USED=$(getquota -g $TSTUSR global curinodes)
1064         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1065         USED=$(getquota -u $TSTUSR global curspace)
1066         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1067         USED=$(getquota -g $TSTUSR global curspace)
1068         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1069         if is_project_quota_supported; then
1070                 USED=$(getquota -p $TSTPRJID global curinodes)
1071                 [ $USED -ne 0 ] &&
1072                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1073                 USED=$(getquota -p $TSTPRJID global curspace)
1074                 [ $USED -ne 0 ] &&
1075                         error "Used block($USED) for project $TSTPRJID isn't 0."
1076         fi
1077
1078         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1079         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1080                 error "create failure, expect success"
1081         if is_project_quota_supported; then
1082                 touch $DIR/$tdir/$tfile-0_1
1083                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1084         fi
1085         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1086                 error "write failure, expect success"
1087
1088         echo "Chown files to $TSTUSR.$TSTUSR ..."
1089         for i in $(seq 0 $ILIMIT); do
1090                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1091                         quota_error a $TSTUSR "chown failure, expect success"
1092         done
1093
1094         # cleanup
1095         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1096                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1097         cleanup_quota_test
1098 }
1099 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1100
1101 # test dropping acquire request on master
1102 test_6() {
1103         local LIMIT=3 # 3M
1104
1105         # Clear dmesg so watchdog is not triggered by previous
1106         # test output
1107         do_facet ost1 dmesg -c > /dev/null
1108
1109         setup_quota_test || error "setup quota failed with $?"
1110         trap cleanup_quota_test EXIT
1111
1112         # make sure the system is clean
1113         local USED=$(getquota -u $TSTUSR global curspace)
1114         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1115
1116         # make sure no granted quota on ost
1117         set_ost_qtype $QTYPE || error "enable ost quota failed"
1118         resetquota -u $TSTUSR
1119
1120         # create file for $TSTUSR
1121         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1122         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1123         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1124
1125         # create file for $TSTUSR2
1126         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1127         $LFS setstripe $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1128         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1129
1130         # cache per-ID lock for $TSTUSR on slave
1131         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1132                 error "set quota failed"
1133         $RUNAS $DD of=$TESTFILE count=1 ||
1134                 error "write $TESTFILE failure, expect success"
1135         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1136                 error "write $TESTFILE2 failure, expect success"
1137         sync; sync
1138         sync_all_data || true
1139
1140         #define QUOTA_DQACQ 601
1141         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1142         lustre_fail mds 0x513 601
1143
1144         if at_is_enabled; then
1145                 at_max_saved=$(at_max_get ost1)
1146                 at_max_set $TIMEOUT ost1
1147         fi
1148
1149         do_facet ost1 $LCTL set_param \
1150                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1151
1152         # write to un-enforced ID ($TSTUSR2) should succeed
1153         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1154                 error "write failure, expect success"
1155
1156         # write to enforced ID ($TSTUSR) in background, exceeding limit
1157         # to make sure DQACQ is sent
1158         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1159         DDPID=$!
1160
1161         # watchdog timer uses a factor of 2
1162         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1163         sleep $((TIMEOUT * 2 + 1))
1164
1165         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1166
1167         # write should be blocked and never finished
1168         if ! ps -p $DDPID  > /dev/null 2>&1; then
1169                 lustre_fail mds 0 0
1170                 error "write finished incorrectly!"
1171         fi
1172
1173         lustre_fail mds 0 0
1174
1175         # no watchdog is triggered
1176         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1177         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1178                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1179         [ -z "$watchdog" ] || error "$watchdog"
1180
1181         rm -f $TMP/lustre-log-${TESTNAME}.log
1182
1183         # write should continue then fail with EDQUOT
1184         local count=0
1185         local c_size
1186         while [ true ]; do
1187                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1188                 if [ $count -ge 240 ]; then
1189                         quota_error u $TSTUSR "dd not finished in $count secs"
1190                 fi
1191                 count=$((count + 1))
1192                 if [ $((count % 30)) -eq 0 ]; then
1193                         c_size=$(stat -c %s $TESTFILE)
1194                         echo "Waiting $count secs. $c_size"
1195                         $SHOW_QUOTA_USER
1196                 fi
1197                 sleep 1
1198         done
1199
1200         cleanup_quota_test
1201 }
1202 run_test 6 "Test dropping acquire request on master"
1203
1204 # quota reintegration (global index)
1205 test_7a() {
1206         local TESTFILE=$DIR/$tdir/$tfile
1207         local LIMIT=20 # 20M
1208
1209         [ "$SLOW" = "no" ] && LIMIT=5
1210
1211         setup_quota_test || error "setup quota failed with $?"
1212         trap cleanup_quota_test EXIT
1213
1214         # make sure the system is clean
1215         local USED=$(getquota -u $TSTUSR global curspace)
1216         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1217
1218         # make sure no granted quota on ost1
1219         set_ost_qtype $QTYPE || error "enable ost quota failed"
1220         resetquota -u $TSTUSR
1221         set_ost_qtype "none" || error "disable ost quota failed"
1222
1223         local OSTUUID=$(ostuuid_from_index 0)
1224         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1225         [ $USED -ne 0 ] &&
1226                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1227
1228         # create test file
1229         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1230         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1231
1232         echo "Stop ost1..."
1233         stop ost1
1234
1235         echo "Enable quota & set quota limit for $TSTUSR"
1236         set_ost_qtype $QTYPE || error "enable ost quota failed"
1237         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1238                 error "set quota failed"
1239
1240         echo "Start ost1..."
1241         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1242         quota_init
1243
1244         wait_ost_reint $QTYPE || error "reintegration failed"
1245
1246         # hardlimit should have been fetched by slave during global
1247         # reintegration, write will exceed quota
1248         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1249                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1250
1251         rm -f $TESTFILE
1252         wait_delete_completed
1253         sync_all_data || true
1254         sleep 3
1255
1256         echo "Stop ost1..."
1257         stop ost1
1258
1259         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1260                 error "clear quota failed"
1261
1262         echo "Start ost1..."
1263         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1264         quota_init
1265
1266         wait_ost_reint $QTYPE || error "reintegration failed"
1267
1268         # hardlimit should be cleared on slave during reintegration
1269         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1270                 quota_error u $TSTUSR "write error, but expect success"
1271
1272         cleanup_quota_test
1273 }
1274 run_test 7a "Quota reintegration (global index)"
1275
1276 # quota reintegration (slave index)
1277 test_7b() {
1278         local LIMIT="100G"
1279         local TESTFILE=$DIR/$tdir/$tfile
1280
1281         setup_quota_test || error "setup quota failed with $?"
1282         trap cleanup_quota_test EXIT
1283
1284         # make sure the system is clean
1285         local USED=$(getquota -u $TSTUSR global curspace)
1286         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1287
1288         # make sure no granted quota on ost1
1289         set_ost_qtype $QTYPE || error "enable ost quota failed"
1290         resetquota -u $TSTUSR
1291         set_ost_qtype "none" || error "disable ost quota failed"
1292
1293         local OSTUUID=$(ostuuid_from_index 0)
1294         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1295         [ $USED -ne 0 ] &&
1296                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1297
1298         # create test file
1299         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1300         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1301
1302         # consume some space to make sure the granted space will not
1303         # be released during reconciliation
1304         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1305                 error "consume space failure, expect success"
1306
1307         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1308         lustre_fail mds 0xa02
1309
1310         set_ost_qtype $QTYPE || error "enable ost quota failed"
1311         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1312                 error "set quota failed"
1313
1314         # ignore the write error
1315         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1316
1317         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1318
1319         lustre_fail mds 0
1320
1321         echo "Restart ost to trigger reintegration..."
1322         stop ost1
1323         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1324         quota_init
1325
1326         wait_ost_reint $QTYPE || error "reintegration failed"
1327
1328         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1329         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1330
1331         cleanup_quota_test
1332         $SHOW_QUOTA_USER
1333 }
1334 run_test 7b "Quota reintegration (slave index)"
1335
1336 # quota reintegration (restart mds during reintegration)
1337 test_7c() {
1338         local LIMIT=20 # 20M
1339         local TESTFILE=$DIR/$tdir/$tfile
1340
1341         [ "$SLOW" = "no" ] && LIMIT=5
1342
1343         setup_quota_test || error "setup quota failed with $?"
1344         trap cleanup_quota_test EXIT
1345
1346         # make sure the system is clean
1347         local USED=$(getquota -u $TSTUSR global curspace)
1348         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1349
1350         set_ost_qtype "none" || error "disable ost quota failed"
1351         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1352                 error "set quota failed"
1353
1354         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1355         lustre_fail ost 0xa03
1356
1357         # enable ost quota
1358         set_ost_qtype $QTYPE || error "enable ost quota failed"
1359         # trigger reintegration
1360         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
1361         procf=${procf}quota_slave.force_reint
1362         do_facet ost1 $LCTL set_param $procf=1 ||
1363                 error "force reintegration failed"
1364
1365         echo "Stop mds..."
1366         stop mds1
1367
1368         lustre_fail ost 0
1369
1370         echo "Start mds..."
1371         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1372         quota_init
1373
1374         # wait longer than usual to make sure the reintegration
1375         # is triggered by quota wb thread.
1376         wait_ost_reint $QTYPE 200 || error "reintegration failed"
1377
1378         # hardlimit should have been fetched by slave during global
1379         # reintegration, write will exceed quota
1380         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1381                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1382
1383         cleanup_quota_test
1384 }
1385 run_test 7c "Quota reintegration (restart mds during reintegration)"
1386
1387 # Quota reintegration (Transfer index in multiple bulks)
1388 test_7d(){
1389         local TESTFILE=$DIR/$tdir/$tfile
1390         local TESTFILE1="$DIR/$tdir/$tfile"-1
1391         local limit=20 #20M
1392
1393         setup_quota_test || error "setup quota failed with $?"
1394         trap cleanup_quota_test EXIT
1395
1396         set_ost_qtype "none" || error "disable ost quota failed"
1397         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1398                 error "set quota for $TSTUSR failed"
1399         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1400                 error "set quota for $TSTUSR2 failed"
1401
1402         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1403         lustre_fail mds 0x608 0
1404
1405         # enable quota to tirgger reintegration
1406         set_ost_qtype "u" || error "enable ost quota failed"
1407         wait_ost_reint "u" || error "reintegration failed"
1408
1409         lustre_fail mds 0
1410
1411         # hardlimit should have been fetched by slave during global
1412         # reintegration, write will exceed quota
1413         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1414                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1415
1416         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1417                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1418
1419         cleanup_quota_test
1420 }
1421 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1422
1423 # quota reintegration (inode limits)
1424 test_7e() {
1425         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1426
1427         # LU-2435: skip this quota test if underlying zfs version has not
1428         # supported native dnode accounting
1429         [ "$mds1_FSTYPE" == zfs ] && {
1430                 local F="feature@userobj_accounting"
1431                 local pool=$(zpool_name mds1)
1432                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
1433
1434                 [[ "$feature" != *" active "* ]] &&
1435                         skip "requires zpool with active userobj_accounting"
1436         }
1437
1438         local ilimit=$((1024 * 2)) # 2k inodes
1439         local TESTFILE=$DIR/${tdir}-1/$tfile
1440
1441         setup_quota_test || error "setup quota failed with $?"
1442         trap cleanup_quota_test EXIT
1443
1444         # make sure the system is clean
1445         local USED=$(getquota -u $TSTUSR global curinodes)
1446         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1447
1448         # make sure no granted quota on mdt1
1449         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1450         resetquota -u $TSTUSR
1451         set_mdt_qtype "none" || error "disable mdt quota failed"
1452
1453         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1454         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1455         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1456                 "$TSTUSR isn't 0."
1457
1458         echo "Stop mds${MDSCOUNT}..."
1459         stop mds${MDSCOUNT}
1460
1461         echo "Enable quota & set quota limit for $TSTUSR"
1462         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1463         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1464                 error "set quota failed"
1465
1466         echo "Start mds${MDSCOUNT}..."
1467         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1468         quota_init
1469
1470         wait_mdt_reint $QTYPE || error "reintegration failed"
1471
1472         echo "create remote dir"
1473         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1474                 error "create remote dir failed"
1475         chmod 0777 $DIR/${tdir}-1
1476
1477         # hardlimit should have been fetched by slave during global
1478         # reintegration, create will exceed quota
1479         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1480                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1481
1482         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1483         wait_delete_completed
1484         sync_all_data || true
1485
1486         echo "Stop mds${MDSCOUNT}..."
1487         stop mds${MDSCOUNT}
1488
1489         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1490                 error "clear quota failed"
1491
1492         echo "Start mds${MDSCOUNT}..."
1493         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1494         quota_init
1495
1496         wait_mdt_reint $QTYPE || error "reintegration failed"
1497
1498         # hardlimit should be cleared on slave during reintegration
1499         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1500                 quota_error u $TSTUSR "create failed, expect success"
1501
1502         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1503         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1504
1505         cleanup_quota_test
1506 }
1507 run_test 7e "Quota reintegration (inode limits)"
1508
1509 # run dbench with quota enabled
1510 test_8() {
1511         local BLK_LIMIT="100g" #100G
1512         local FILE_LIMIT=1000000
1513
1514         setup_quota_test || error "setup quota failed with $?"
1515         trap cleanup_quota_test EXIT
1516
1517         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1518         set_ost_qtype $QTYPE || error "enable ost quota failed"
1519
1520         echo "Set enough high limit for user: $TSTUSR"
1521         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1522                 error "set user quota failed"
1523         echo "Set enough high limit for group: $TSTUSR"
1524         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1525                 error "set group quota failed"
1526         if is_project_quota_supported; then
1527                 change_project -sp $TSTPRJID $DIR/$tdir
1528                 echo "Set enough high limit for project: $TSTPRJID"
1529                 $LFS setquota -p $TSTPRJID -b 0 \
1530                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1531                         error "set project quota failed"
1532         fi
1533
1534         local duration=""
1535         [ "$SLOW" = "no" ] && duration=" -t 120"
1536         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1537                 quota_error a $TSTUSR "dbench failed!"
1538
1539         is_project_quota_supported && change_project -C $DIR/$tdir
1540         cleanup_quota_test
1541 }
1542 run_test 8 "Run dbench with quota enabled"
1543
1544 # this check is just for test_9
1545 OST0_MIN=4900000 #4.67G
1546
1547 check_whether_skip () {
1548         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1549         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1550         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1551                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1552                 return 0
1553         else
1554                 return 1
1555         fi
1556 }
1557
1558 # run for fixing bug10707, it needs a big room. test for 64bit
1559 test_9() {
1560         local filesize=$((1024 * 9 / 2)) # 4.5G
1561
1562         check_whether_skip && return 0
1563
1564         setup_quota_test || error "setup quota failed with $?"
1565         trap cleanup_quota_test EXIT
1566
1567         set_ost_qtype "ug" || error "enable ost quota failed"
1568
1569         local TESTFILE="$DIR/$tdir/$tfile-0"
1570         local BLK_LIMIT=100G #100G
1571         local FILE_LIMIT=1000000
1572
1573         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1574
1575         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1576                 "for user: $TSTUSR"
1577         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1578                 error "set user quota failed"
1579
1580         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1581                 "for group: $TSTUSR"
1582         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1583                 error "set group quota failed"
1584
1585         quota_show_check a u $TSTUSR
1586         quota_show_check a g $TSTUSR
1587
1588         echo "Create test file"
1589         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1590         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1591
1592         log "Write the big file of 4.5G ..."
1593         $RUNAS $DD of=$TESTFILE count=$filesize ||
1594                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1595
1596         $SHOW_QUOTA_USER
1597         $SHOW_QUOTA_GROUP
1598
1599         cleanup_quota_test
1600
1601         $SHOW_QUOTA_USER
1602         $SHOW_QUOTA_GROUP
1603 }
1604 run_test 9 "Block limit larger than 4GB (b10707)"
1605
1606 test_10() {
1607         local TESTFILE=$DIR/$tdir/$tfile
1608
1609         setup_quota_test || error "setup quota failed with $?"
1610         trap cleanup_quota_test EXIT
1611
1612         # set limit to root user should fail
1613         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1614                 error "set limit for root user successfully, expect failure"
1615         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1616                 error "set limit for root group successfully, expect failure"
1617         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
1618                 error "set limit for project 0 successfully, expect failure"
1619
1620         # root user can overrun quota
1621         set_ost_qtype "ug" || error "enable ost quota failed"
1622
1623         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1624                 error "set quota failed"
1625         quota_show_check b u $TSTUSR
1626
1627         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1628         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1629
1630         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1631                 error "write failure, expect success"
1632
1633         cleanup_quota_test
1634 }
1635 run_test 10 "Test quota for root user"
1636
1637 test_11() {
1638         local TESTFILE=$DIR/$tdir/$tfile
1639         setup_quota_test || error "setup quota failed with $?"
1640         trap cleanup_quota_test EXIT
1641
1642         set_mdt_qtype "ug" || error "enable mdt quota failed"
1643         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1644                 error "set quota failed"
1645
1646         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1647         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1648
1649         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1650         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1651
1652         $SHOW_QUOTA_USER
1653         local USED=$(getquota -u $TSTUSR global curinodes)
1654         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1655
1656         cleanup_quota_test
1657 }
1658 run_test 11 "Chown/chgrp ignores quota"
1659
1660 test_12a() {
1661         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
1662
1663         local blimit=22 # 22M
1664         local blk_cnt=$((blimit - 5))
1665         local TESTFILE0="$DIR/$tdir/$tfile"-0
1666         local TESTFILE1="$DIR/$tdir/$tfile"-1
1667
1668         setup_quota_test || error "setup quota failed with $?"
1669         trap cleanup_quota_test EXIT
1670
1671         set_ost_qtype "u" || error "enable ost quota failed"
1672         quota_show_check b u $TSTUSR
1673
1674         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1675                 error "set quota failed"
1676
1677         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1678         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1679         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1680         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1681
1682         echo "Write to ost0..."
1683         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1684                 quota_error a $TSTUSR "dd failed"
1685
1686         echo "Write to ost1..."
1687         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1688                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1689
1690         echo "Free space from ost0..."
1691         rm -f $TESTFILE0
1692         wait_delete_completed
1693         sync_all_data || true
1694
1695         echo "Write to ost1 after space freed from ost0..."
1696         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1697                 quota_error a $TSTUSR "rebalancing failed"
1698
1699         cleanup_quota_test
1700 }
1701 run_test 12a "Block quota rebalancing"
1702
1703 test_12b() {
1704         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1705
1706         local ilimit=$((1024 * 2)) # 2k inodes
1707         local TESTFILE0=$DIR/$tdir/$tfile
1708         local TESTFILE1=$DIR/${tdir}-1/$tfile
1709
1710         setup_quota_test || error "setup quota failed with $?"
1711         trap cleanup_quota_test EXIT
1712
1713         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1714         chmod 0777 $DIR/${tdir}-1
1715
1716         set_mdt_qtype "u" || error "enable mdt quota failed"
1717         quota_show_check f u $TSTUSR
1718
1719         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1720                 error "set quota failed"
1721
1722         echo "Create $ilimit files on mdt0..."
1723         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1724                 quota_error u $TSTUSR "create failed, but expect success"
1725
1726         echo "Create files on mdt1..."
1727         $RUNAS createmany -m $TESTFILE1 1 &&
1728                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1729
1730         echo "Free space from mdt0..."
1731         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1732         wait_delete_completed
1733         sync_all_data || true
1734
1735         echo "Create files on mdt1 after space freed from mdt0..."
1736         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1737                 quota_error a $TSTUSR "rebalancing failed"
1738
1739         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1740                 error "unlink mdt1 files failed"
1741         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1742
1743         cleanup_quota_test
1744 }
1745 run_test 12b "Inode quota rebalancing"
1746
1747 test_13(){
1748         local TESTFILE=$DIR/$tdir/$tfile
1749         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1750         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1751
1752         setup_quota_test || error "setup quota failed with $?"
1753         trap cleanup_quota_test EXIT
1754
1755         set_ost_qtype "u" || error "enable ost quota failed"
1756         quota_show_check b u $TSTUSR
1757
1758         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1759                 error "set quota failed"
1760         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1761         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1762
1763         # clear the locks in cache first
1764         do_facet ost1 $LCTL set_param -n $procf=clear
1765         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1766         [ $nlock -eq 0 ] || error "$nlock cached locks"
1767
1768         # write to acquire the per-ID lock
1769         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1770                 quota_error a $TSTUSR "dd failed"
1771
1772         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1773         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1774
1775         # clear quota doesn't trigger per-ID lock cancellation
1776         resetquota -u $TSTUSR
1777         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1778         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1779
1780         # clear the per-ID lock
1781         do_facet ost1 $LCTL set_param -n $procf=clear
1782         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1783         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1784
1785         # spare quota should be released
1786         local OSTUUID=$(ostuuid_from_index 0)
1787         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1788         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1789         [ $limit -le $space ] ||
1790                 error "spare quota isn't released, limit:$limit, space:$space"
1791
1792         cleanup_quota_test
1793 }
1794 run_test 13 "Cancel per-ID lock in the LRU list"
1795
1796 test_15(){
1797         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1798
1799         wait_delete_completed
1800         sync_all_data || true
1801
1802         # test for user
1803         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1804                 error "set user quota failed"
1805         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1806         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1807                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1808         resetquota -u $TSTUSR
1809
1810         # test for group
1811         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1812                 error "set group quota failed"
1813         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1814         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1815                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1816         resetquota -g $TSTUSR
1817 }
1818 run_test 15 "Set over 4T block quota"
1819
1820 test_17sub() {
1821         local err_code=$1
1822         local BLKS=1    # 1M less than limit
1823         local TESTFILE=$DIR/$tdir/$tfile
1824
1825         setup_quota_test || error "setup quota failed with $?"
1826         trap cleanup_quota_test EXIT
1827
1828         # make sure the system is clean
1829         local USED=$(getquota -u $TSTUSR global curspace)
1830         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1831
1832         set_ost_qtype "ug" || error "enable ost quota failed"
1833         # make sure no granted quota on ost
1834         resetquota -u $TSTUSR
1835         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1836                 error "set quota failed"
1837
1838         quota_show_check b u $TSTUSR
1839
1840         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1841         lustre_fail mds 0xa04 $err_code
1842
1843         # write in background
1844         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1845         local DDPID=$!
1846
1847         sleep 2
1848         # write should be blocked and never finished
1849         if ! ps -p $DDPID  > /dev/null 2>&1; then
1850                 lustre_fail mds 0 0
1851                 quota_error u $TSTUSR "write finished incorrectly!"
1852         fi
1853
1854         lustre_fail mds 0 0
1855
1856         local count=0
1857         local timeout=30
1858         while [ true ]; do
1859                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1860                 count=$((count+1))
1861                 if [ $count -gt $timeout ]; then
1862                         quota_error u $TSTUSR "dd is not finished!"
1863                 fi
1864                 sleep 1
1865         done
1866
1867         sync; sync_all_data || true
1868
1869         USED=$(getquota -u $TSTUSR global curspace)
1870         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1871                 "Used space(${USED}K) is less than ${BLKS}M"
1872
1873         cleanup_quota_test
1874 }
1875
1876 # DQACQ return recoverable error
1877 test_17() {
1878         echo "DQACQ return -ENOLCK"
1879         #define ENOLCK  37
1880         test_17sub 37 || error "Handle -ENOLCK failed"
1881
1882         echo "DQACQ return -EAGAIN"
1883         #define EAGAIN  11
1884         test_17sub 11 || error "Handle -EAGAIN failed"
1885
1886         echo "DQACQ return -ETIMEDOUT"
1887         #define ETIMEDOUT 110
1888         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1889
1890         echo "DQACQ return -ENOTCONN"
1891         #define ENOTCONN 107
1892         test_17sub 107 || error "Handle -ENOTCONN failed"
1893 }
1894
1895 run_test 17 "DQACQ return recoverable error"
1896
1897 test_18_sub () {
1898         local io_type=$1
1899         local blimit="200m" # 200M
1900         local TESTFILE="$DIR/$tdir/$tfile"
1901
1902         setup_quota_test || error "setup quota failed with $?"
1903         trap cleanup_quota_test EXIT
1904
1905         set_ost_qtype "u" || error "enable ost quota failed"
1906         log "User quota (limit: $blimit)"
1907         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1908                 error "set quota failed"
1909         quota_show_check b u $TSTUSR
1910
1911         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
1912         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1913
1914         local timeout=$(sysctl -n lustre.timeout)
1915
1916         if [ $io_type = "directio" ]; then
1917                 log "Write 100M (directio) ..."
1918                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1919         else
1920                 log "Write 100M (buffered) ..."
1921                 $RUNAS $DD of=$TESTFILE count=100 &
1922         fi
1923         local DDPID=$!
1924
1925         replay_barrier $SINGLEMDS
1926         log "Fail mds for $((2 * timeout)) seconds"
1927         fail $SINGLEMDS $((2 * timeout))
1928
1929         local count=0
1930         if at_is_enabled; then
1931                 timeout=$(at_max_get mds)
1932         else
1933                 timeout=$(lctl get_param -n timeout)
1934         fi
1935
1936         while [ true ]; do
1937                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1938                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1939                         log "it took $count second"
1940                 fi
1941                 sleep 1
1942         done
1943
1944         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1945         sync
1946         cancel_lru_locks mdc
1947         cancel_lru_locks osc
1948         $SHOW_QUOTA_USER
1949
1950         local testfile_size=$(stat -c %s $TESTFILE)
1951         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1952                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1953                         "got ${testfile_size}. Verifying file failed!"
1954         fi
1955         cleanup_quota_test
1956 }
1957
1958 # test when mds does failover, the ost still could work well
1959 # this test shouldn't trigger watchdog b=14840
1960 test_18() {
1961         # Clear dmesg so watchdog is not triggered by previous
1962         # test output
1963         do_facet ost1 dmesg -c > /dev/null
1964
1965         test_18_sub normal
1966         test_18_sub directio
1967
1968         # check if watchdog is triggered
1969         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1970         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1971                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1972         [ -z "$watchdog" ] || error "$watchdog"
1973         rm -f $TMP/lustre-log-${TESTNAME}.log
1974 }
1975 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1976
1977 test_19() {
1978         local blimit=5 # 5M
1979         local TESTFILE=$DIR/$tdir/$tfile
1980
1981         setup_quota_test || error "setup quota failed with $?"
1982         trap cleanup_quota_test EXIT
1983
1984         set_ost_qtype $QTYPE || error "enable ost quota failed"
1985
1986         # bind file to a single OST
1987         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
1988         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1989
1990         echo "Set user quota (limit: ${blimit}M)"
1991         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1992                 error "set user quota failed"
1993         quota_show_check b u $TSTUSR
1994         echo "Update quota limits"
1995         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1996                 error "set group quota failed"
1997         quota_show_check b u $TSTUSR
1998
1999         # first wirte might be cached
2000         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2001         cancel_lru_locks osc
2002         $SHOW_QUOTA_USER
2003         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2004                 quota_error u $TSTUSR "Write success, expect failure"
2005         $SHOW_QUOTA_USER
2006
2007         cleanup_quota_test
2008 }
2009 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2010
2011 test_20() { # b15754
2012         local LSTR=(2g 1t 4k 3m) # limits strings
2013         # limits values
2014         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2015                     $((3*1024*1024)))
2016
2017         resetquota -u $TSTUSR
2018
2019         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2020                 $MOUNT || error "could not set quota limits"
2021         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2022                                 --inode-softlimit ${LSTR[2]} \
2023                                 --inode-hardlimit ${LSTR[3]} \
2024                                 $MOUNT || error "could not set quota limits"
2025
2026         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2027                 error "bsoftlimit was not set properly"
2028         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2029                 error "bhardlimit was not set properly"
2030         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2031                 error "isoftlimit was not set properly"
2032         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2033                 error "ihardlimit was not set properly"
2034
2035         resetquota -u $TSTUSR
2036 }
2037 run_test 20 "Test if setquota specifiers work properly (b15754)"
2038
2039 test_21_sub() {
2040         local testfile=$1
2041         local blk_number=$2
2042         local seconds=$3
2043
2044         local time=$(($(date +%s) + seconds))
2045         while [ $(date +%s) -lt $time ]; do
2046                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2047         done
2048 }
2049
2050 # run for fixing bug16053, setquota shouldn't fail when writing and
2051 # deleting are happening
2052 test_21() {
2053         local TESTFILE="$DIR/$tdir/$tfile"
2054         local BLIMIT=10 # 10G
2055         local ILIMIT=1000000
2056
2057         setup_quota_test || error "setup quota failed with $?"
2058         trap cleanup_quota_test EXIT
2059
2060         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2061
2062         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2063         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2064                 error "set user quota failed"
2065         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2066         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2067                 error "set group quota failed"
2068         if is_project_quota_supported; then
2069                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2070                         "project: $TSTPRJID"
2071                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2072                          $MOUNT || error "set project quota failed"
2073         fi
2074
2075         # repeat writing on a 1M file
2076         test_21_sub ${TESTFILE}_1 1 30 &
2077         local DDPID1=$!
2078         # repeat writing on a 128M file
2079         test_21_sub ${TESTFILE}_2 128 30 &
2080         local DDPID2=$!
2081
2082         local time=$(($(date +%s) + 30))
2083         local i=1
2084         while [ $(date +%s) -lt $time ]; do
2085                 log "Set quota for $i times"
2086                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2087                         -I $((ILIMIT + i)) $MOUNT ||
2088                                 error "Set user quota failed"
2089                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2090                         -I $((ILIMIT + i)) $MOUNT ||
2091                                 error "Set group quota failed"
2092                 if is_project_quota_supported; then
2093                         $LFS setquota -p $TSTPRJID -b 0 -B \
2094                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2095                                 error "Set project quota failed"
2096                 fi
2097                 i=$((i+1))
2098                 sleep 1
2099         done
2100
2101         local count=0
2102         while [ true ]; do
2103                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2104                 count=$((count+1))
2105                 if [ $count -gt 60 ]; then
2106                         quota_error a $TSTUSR "dd should be finished!"
2107                 fi
2108                 sleep 1
2109         done
2110         echo "(dd_pid=$DDPID1, time=$count)successful"
2111
2112         count=0
2113         while [ true ]; do
2114                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2115                 count=$((count+1))
2116                 if [ $count -gt 60 ]; then
2117                         quota_error a $TSTUSR "dd should be finished!"
2118                 fi
2119                 sleep 1
2120         done
2121         echo "(dd_pid=$DDPID2, time=$count)successful"
2122
2123         cleanup_quota_test
2124 }
2125 run_test 21 "Setquota while writing & deleting (b16053)"
2126
2127 # enable/disable quota enforcement permanently
2128 test_22() {
2129         echo "Set both mdt & ost quota type as ug"
2130         local qtype="ug"
2131         is_project_quota_supported && qtype=$QTYPE
2132         set_mdt_qtype $qtype || error "enable mdt quota failed"
2133         set_ost_qtype $qtype || error "enable ost quota failed"
2134
2135         echo "Restart..."
2136         stopall || error "failed to stopall (1)"
2137         mount
2138         setupall
2139
2140         echo "Verify if quota is enabled"
2141         local qtype1=$(mdt_quota_type)
2142         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2143         qtype=$(ost_quota_type)
2144         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2145
2146         echo "Set both mdt & ost quota type as none"
2147         set_mdt_qtype "none" || error "disable mdt quota failed"
2148         set_ost_qtype "none" || error "disable ost quota failed"
2149
2150         echo "Restart..."
2151         stopall || error "failed to stopall (2)"
2152         mount
2153         setupall
2154         quota_init
2155
2156         echo "Verify if quota is disabled"
2157         qtype=$(mdt_quota_type)
2158         [ $qtype != "none" ] && error "mdt quota setting is lost"
2159         qtype=$(ost_quota_type)
2160         [ $qtype != "none" ] && error "ost quota setting is lost"
2161
2162         return 0
2163 }
2164 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2165
2166 test_23_sub() {
2167         local TESTFILE="$DIR/$tdir/$tfile"
2168         local LIMIT=$1
2169
2170         setup_quota_test || error "setup quota failed with $?"
2171         trap cleanup_quota_test EXIT
2172
2173         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2174
2175         # test for user
2176         log "User quota (limit: $LIMIT MB)"
2177         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2178                 error "set quota failed"
2179         quota_show_check b u $TSTUSR
2180
2181         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2182         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2183
2184         log "Step1: trigger EDQUOT with O_DIRECT"
2185         log "Write half of file"
2186         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2187                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2188                         "limit=$LIMIT"
2189         log "Write out of block quota ..."
2190         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2191                 oflag=direct conv=notrunc &&
2192                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2193                         "limit=$LIMIT"
2194         log "Step1: done"
2195
2196         log "Step2: rewrite should succeed"
2197         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2198                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2199                         "limit=$LIMIT"
2200         log "Step2: done"
2201
2202         cleanup_quota_test
2203
2204         local OST0_UUID=$(ostuuid_from_index 0)
2205         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2206         [ $OST0_QUOTA_USED -ne 0 ] &&
2207                 ($SHOW_QUOTA_USER; \
2208                 quota_error u $TSTUSR "quota isn't released")
2209         $SHOW_QUOTA_USER
2210 }
2211
2212 test_23() {
2213         [ "$ost1_FSTYPE" == zfs ] &&
2214                 skip "Overwrite in place is not guaranteed to be " \
2215                 "space neutral on ZFS"
2216
2217         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2218         check_whether_skip && return 0
2219         log "run for 4MB test file"
2220         test_23_sub 4
2221
2222         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2223         check_whether_skip && return 0
2224         log "run for 40MB test file"
2225         test_23_sub 40
2226 }
2227 run_test 23 "Quota should be honored with directIO (b16125)"
2228
2229 test_24() {
2230         local blimit=5 # 5M
2231         local TESTFILE="$DIR/$tdir/$tfile"
2232
2233         setup_quota_test || error "setup quota failed with $?"
2234         trap cleanup_quota_test EXIT
2235
2236         set_ost_qtype $QTYPE || error "enable ost quota failed"
2237
2238         # bind file to a single OST
2239         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2240         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2241
2242         echo "Set user quota (limit: ${blimit}M)"
2243         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2244                 error "set quota failed"
2245
2246         # overrun quota by root user
2247         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2248                 error "write failure, expect success"
2249         cancel_lru_locks osc
2250         sync_all_data || true
2251
2252         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2253
2254         cleanup_quota_test
2255 }
2256 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2257
2258 test_27a() { # b19612
2259         $LFS quota $TSTUSR $DIR &&
2260                 error "lfs succeeded with no type, but should have failed"
2261         $LFS setquota $TSTUSR $DIR &&
2262                 error "lfs succeeded with no type, but should have failed"
2263         return 0
2264 }
2265 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2266
2267 test_27b() { # b20200
2268         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2269                 error "lfs setquota failed with uid argument"
2270         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2271                 error "lfs stequota failed with gid argument"
2272         if is_project_quota_supported; then
2273                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2274                         1000 $DIR || error \
2275                                 "lfs stequota failed with projid argument"
2276         fi
2277         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2278         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2279         if is_project_quota_supported; then
2280                 $SHOW_QUOTA_PROJID ||
2281                         error "lfs quota failed with projid argument"
2282         fi
2283         resetquota -u $TSTUSR
2284         resetquota -g $TSTUSR
2285         resetquota -p $TSTPRJID
2286         return 0
2287 }
2288 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2289
2290 test_27c() {
2291         local limit
2292
2293         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2294                 error "lfs setquota failed"
2295
2296         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2297         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2298         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2299         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2300
2301         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2302                 error "lfs setquota for $TSTID failed"
2303
2304         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2305         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2306         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2307         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2308
2309         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2310                 error "total allocated inode/block limit not printed"
2311
2312         resetquota -u $TSTUSR
2313 }
2314 run_test 27c "lfs quota should support human-readable output"
2315
2316 test_27d() {
2317         local softlimit=1.5
2318         local hardlimit=2.3
2319         local limit
2320
2321         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2322                 error "set fraction block limit failed"
2323         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2324         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2325         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2326         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2327
2328         resetquota -u $TSTUSR
2329 }
2330 run_test 27d "lfs setquota should support fraction block limit"
2331
2332 test_30() {
2333         local LIMIT=4 # 4MB
2334         local TESTFILE="$DIR/$tdir/$tfile"
2335         local GRACE=10
2336
2337         setup_quota_test || error "setup quota failed with $?"
2338         trap cleanup_quota_test EXIT
2339
2340         set_ost_qtype "u" || error "enable ost quota failed"
2341
2342         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2343         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2344
2345         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2346                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2347         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2348                 error "set quota failed"
2349         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2350         cancel_lru_locks osc
2351         sleep $GRACE
2352         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2353         # over-quota flag has not yet settled since we do not trigger async
2354         # events based on grace time period expiration
2355         $SHOW_QUOTA_USER
2356         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2357         cancel_lru_locks osc
2358         # now over-quota flag should be settled and further writes should fail
2359         $SHOW_QUOTA_USER
2360         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2361                 error "grace times were reset"
2362         # cleanup
2363         cleanup_quota_test
2364         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2365                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2366 }
2367 run_test 30 "Hard limit updates should not reset grace times"
2368
2369 # basic usage tracking for user & group
2370 test_33() {
2371         local INODES=10 # 10 files
2372         local BLK_CNT=2 # of 2M each
2373         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2374
2375         setup_quota_test || error "setup quota failed with $?"
2376         trap cleanup_quota_test EXIT
2377
2378         # make sure the system is clean
2379         local USED=$(getquota -u $TSTID global curspace)
2380         [ $USED -ne 0 ] &&
2381                 error "Used space ($USED) for user $TSTID isn't 0."
2382         USED=$(getquota -g $TSTID global curspace)
2383         [ $USED -ne 0 ] &&
2384                 error "Used space ($USED) for group $TSTID isn't 0."
2385         if is_project_quota_supported; then
2386                 USED=$(getquota -p $TSTPRJID global curspace)
2387                 [ $USED -ne 0 ] && error \
2388                         "Used space ($USED) for project $TSTPRJID isn't 0."
2389         fi
2390
2391         echo "Write files..."
2392         for i in $(seq 0 $INODES); do
2393                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2394                         error "write failed"
2395                         is_project_quota_supported &&
2396                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
2397                 echo "Iteration $i/$INODES completed"
2398         done
2399         cancel_lru_locks osc
2400
2401         echo "Wait for setattr on objects finished..."
2402         wait_delete_completed
2403
2404         sync; sync_all_data || true
2405
2406         echo "Verify disk usage after write"
2407         USED=$(getquota -u $TSTID global curspace)
2408         [ $USED -lt $TOTAL_BLKS ] &&
2409                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2410         USED=$(getquota -g $TSTID global curspace)
2411         [ $USED -lt $TOTAL_BLKS ] &&
2412                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2413         if is_project_quota_supported; then
2414                 USED=$(getquota -p $TSTPRJID global curspace)
2415                 [ $USED -lt $TOTAL_BLKS ] && error \
2416                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
2417         fi
2418
2419         echo "Verify inode usage after write"
2420         USED=$(getquota -u $TSTID global curinodes)
2421         [ $USED -lt $INODES ] &&
2422                 error "Used inode for user $TSTID is $USED, expected $INODES"
2423         USED=$(getquota -g $TSTID global curinodes)
2424         [ $USED -lt $INODES ] &&
2425                 error "Used inode for group $TSTID is $USED, expected $INODES"
2426         if is_project_quota_supported; then
2427                 USED=$(getquota -p $TSTPRJID global curinodes)
2428                 [ $USED -lt $INODES ] && error \
2429                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
2430         fi
2431
2432         cleanup_quota_test
2433
2434         echo "Verify disk usage after delete"
2435         USED=$(getquota -u $TSTID global curspace)
2436         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2437         USED=$(getquota -u $TSTID global curinodes)
2438         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2439         USED=$(getquota -g $TSTID global curspace)
2440         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2441         USED=$(getquota -g $TSTID global curinodes)
2442         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2443         if is_project_quota_supported; then
2444                 USED=$(getquota -p $TSTPRJID global curspace)
2445                 [ $USED -eq 0 ] ||
2446                         error "Used space for project $TSTPRJID isn't 0. $USED"
2447                 USED=$(getquota -p $TSTPRJID global curinodes)
2448                 [ $USED -eq 0 ] ||
2449                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
2450         fi
2451 }
2452 run_test 33 "Basic usage tracking for user & group & project"
2453
2454 # usage transfer test for user & group & project
2455 test_34() {
2456         local BLK_CNT=2 # 2MB
2457         local project_supported="no"
2458
2459         is_project_quota_supported && project_supported="yes"
2460         setup_quota_test || error "setup quota failed with $?"
2461         trap cleanup_quota_test EXIT
2462
2463         # make sure the system is clean
2464         local USED=$(getquota -u $TSTID global curspace)
2465         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2466         USED=$(getquota -g $TSTID global curspace)
2467         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2468
2469         local USED=$(getquota -u $TSTID2 global curspace)
2470         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2471         if [ $project_supported == "yes" ]; then
2472                 USED=$(getquota -p $TSTPRJID global curspace)
2473                 [ $USED -ne 0 ] && error \
2474                         "Used space ($USED) for Project $TSTPRJID isn't 0."
2475         fi
2476
2477         echo "Write file..."
2478         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2479                 error "write failed"
2480         cancel_lru_locks osc
2481         sync; sync_all_data || true
2482
2483         echo "chown the file to user $TSTID"
2484         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2485
2486         echo "Wait for setattr on objects finished..."
2487         wait_delete_completed
2488
2489         BLK_CNT=$((BLK_CNT * 1024))
2490
2491         echo "Verify disk usage for user $TSTID"
2492         USED=$(getquota -u $TSTID global curspace)
2493         [ $USED -lt $BLK_CNT ] &&
2494                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2495         USED=$(getquota -u $TSTID global curinodes)
2496         [ $USED -ne 1 ] &&
2497                 error "Used inodes for user $TSTID is $USED, expected 1"
2498
2499         echo "chgrp the file to group $TSTID"
2500         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2501
2502         echo "Wait for setattr on objects finished..."
2503         wait_delete_completed
2504
2505         echo "Verify disk usage for group $TSTID"
2506         USED=$(getquota -g $TSTID global curspace)
2507         [ $USED -ge $BLK_CNT ] ||
2508                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2509         USED=$(getquota -g $TSTID global curinodes)
2510         [ $USED -eq 1 ] ||
2511                 error "Used inodes for group $TSTID is $USED, expected 1"
2512
2513         # chown won't change the ost object group. LU-4345 */
2514         echo "chown the file to user $TSTID2"
2515         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2516
2517         echo "Wait for setattr on objects finished..."
2518         wait_delete_completed
2519
2520         echo "change_project project id to $TSTPRJID"
2521         [ $project_supported == "yes" ] &&
2522                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2523         echo "Wait for setattr on objects finished..."
2524         wait_delete_completed
2525
2526         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2527         USED=$(getquota -u $TSTID2 global curspace)
2528         [ $USED -lt $BLK_CNT ] &&
2529                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2530         USED=$(getquota -u $TSTID global curspace)
2531         [ $USED -ne 0 ] &&
2532                 error "Used space for user $TSTID is $USED, expected 0"
2533         USED=$(getquota -g $TSTID global curspace)
2534         [ $USED -lt $BLK_CNT ] &&
2535                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2536         if [ $project_supported == "yes" ]; then
2537                 USED=$(getquota -p $TSTPRJID global curspace)
2538                 [ $USED -lt $BLK_CNT ] && error \
2539                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
2540         fi
2541
2542         cleanup_quota_test
2543 }
2544 run_test 34 "Usage transfer for user & group & project"
2545
2546 # usage is still accessible across restart
2547 test_35() {
2548         local BLK_CNT=2 # 2 MB
2549
2550         setup_quota_test || error "setup quota failed with $?"
2551         trap cleanup_quota_test EXIT
2552
2553         echo "Write file..."
2554         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2555                 error "write failed"
2556         is_project_quota_supported &&
2557                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2558         cancel_lru_locks osc
2559
2560         echo "Wait for setattr on objects finished..."
2561         wait_delete_completed
2562
2563         sync; sync_all_data || true
2564
2565         echo "Save disk usage before restart"
2566         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2567         [ $ORIG_USR_SPACE -eq 0 ] &&
2568                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2569         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2570         [ $ORIG_USR_INODES -eq 0 ] &&
2571                 error "Used inodes for user $TSTID is 0, expected 1"
2572         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2573         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2574         [ $ORIG_GRP_SPACE -eq 0 ] &&
2575                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2576         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2577         [ $ORIG_GRP_INODES -eq 0 ] &&
2578                 error "Used inodes for group $TSTID is 0, expected 1"
2579         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2580
2581         if is_project_quota_supported; then
2582                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
2583                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
2584                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
2585                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
2586                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
2587                         "Used inodes for project $TSTPRJID is 0, expected 1"
2588                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
2589         fi
2590
2591         log "Restart..."
2592         stopall
2593         setupall
2594         quota_init
2595
2596         echo "Verify disk usage after restart"
2597         local USED=$(getquota -u $TSTID global curspace)
2598         [ $USED -eq $ORIG_USR_SPACE ] ||
2599                 error "Used space for user $TSTID changed from " \
2600                         "$ORIG_USR_SPACE to $USED"
2601         USED=$(getquota -u $TSTID global curinodes)
2602         [ $USED -eq $ORIG_USR_INODES ] ||
2603                 error "Used inodes for user $TSTID changed from " \
2604                         "$ORIG_USR_INODES to $USED"
2605         USED=$(getquota -g $TSTID global curspace)
2606         [ $USED -eq $ORIG_GRP_SPACE ] ||
2607                 error "Used space for group $TSTID changed from " \
2608                         "$ORIG_GRP_SPACE to $USED"
2609         USED=$(getquota -g $TSTID global curinodes)
2610         [ $USED -eq $ORIG_GRP_INODES ] ||
2611                 error "Used inodes for group $TSTID changed from " \
2612                         "$ORIG_GRP_INODES to $USED"
2613         if [ $project_supported == "yes" ]; then
2614                 USED=$(getquota -p $TSTPRJID global curinodes)
2615                 [ $USED -eq $ORIG_PRJ_INODES ] ||
2616                         error "Used inodes for project $TSTPRJID " \
2617                                 "changed from $ORIG_PRJ_INODES to $USED"
2618                 USED=$(getquota -p $TSTPRJID global curspace)
2619                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
2620                         error "Used space for project $TSTPRJID "\
2621                                 "changed from $ORIG_PRJ_SPACE to $USED"
2622         fi
2623
2624         # check if the vfs_dq_init() is called before writing
2625         echo "Append to the same file..."
2626         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2627                 error "write failed"
2628         cancel_lru_locks osc
2629         sync; sync_all_data || true
2630
2631         echo "Verify space usage is increased"
2632         USED=$(getquota -u $TSTID global curspace)
2633         [ $USED -gt $ORIG_USR_SPACE ] ||
2634                 error "Used space for user $TSTID isn't increased" \
2635                         "orig:$ORIG_USR_SPACE, now:$USED"
2636         USED=$(getquota -g $TSTID global curspace)
2637         [ $USED -gt $ORIG_GRP_SPACE ] ||
2638                 error "Used space for group $TSTID isn't increased" \
2639                         "orig:$ORIG_GRP_SPACE, now:$USED"
2640         if [ $project_supported == "yes" ]; then
2641                 USED=$(getquota -p $TSTPRJID global curspace)
2642                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
2643                         error "Used space for project $TSTPRJID isn't " \
2644                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
2645         fi
2646
2647         cleanup_quota_test
2648 }
2649 run_test 35 "Usage is still accessible across reboot"
2650
2651 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2652 # LU-5006
2653 test_37() {
2654         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
2655                 skip "Old server doesn't have LU-5006 fix."
2656
2657         setup_quota_test || error "setup quota failed with $?"
2658         trap cleanup_quota_test EXIT
2659
2660         # make sure the system is clean
2661         local USED=$(getquota -u $TSTID global curspace)
2662         [ $USED -ne 0 ] &&
2663                 error "Used space ($USED) for user $TSTID isn't 0."
2664
2665         # create file with MDS_OPEN_DELAY_CREATE flag
2666         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
2667                 error "Create file failed"
2668         # write to file
2669         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2670                 oflag=sync || error "Write file failed"
2671         # chown to the file
2672         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2673
2674         # wait for setattr on objects finished..."
2675         wait_delete_completed
2676
2677         USED=$(getquota -u $TSTID global curspace)
2678         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2679
2680         cleanup_quota_test
2681 }
2682 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2683
2684 # LU-8801
2685 test_38() {
2686         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
2687                 skip "Old server doesn't have LU-8801 fix."
2688
2689         [ "$UID" != 0 ] && skip_env "must run as root" && return
2690
2691         setup_quota_test || error "setup quota failed with $?"
2692         trap cleanup_quota_test EXIT
2693
2694         # make sure the system is clean
2695         local USED=$(getquota -u $TSTID global curspace)
2696         [ $USED -ne 0 ] &&
2697                 error "Used space ($USED) for user $TSTID isn't 0."
2698         USED=$(getquota -u $TSTID2 global curspace)
2699         [ $USED -ne 0 ] &&
2700                 error "Used space ($USED) for user $TSTID2 isn't 0."
2701
2702         local TESTFILE="$DIR/$tdir/$tfile"
2703         local file_cnt=10000
2704
2705         # Generate id entries in accounting file
2706         echo "Create $file_cnt files..."
2707         for i in `seq $file_cnt`; do
2708                 touch $TESTFILE-$i
2709                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
2710                         error "failed to chown $TESTFILE-$i"
2711         done
2712         cancel_lru_locks osc
2713         sync; sync_all_data || true
2714
2715         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
2716         procf=${procf}.quota_slave.acct_user
2717         local accnt_cnt
2718
2719         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
2720                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
2721         echo "Found $acct_cnt id entries"
2722
2723         [ $file_cnt -eq $acct_cnt ] || {
2724                 do_facet mds1 $LCTL get_param $procf
2725                 error "skipped id entries"
2726         }
2727
2728         cleanup_quota_test
2729 }
2730 run_test 38 "Quota accounting iterator doesn't skip id entries"
2731
2732 test_39() {
2733         local TESTFILE="$DIR/$tdir/project"
2734         ! is_project_quota_supported &&
2735                 skip "Project quota is not supported"
2736
2737         setup_quota_test || error "setup quota failed with $?"
2738
2739         touch $TESTFILE
2740         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2741         [ $projectid -ne 0 ] &&
2742                 error "Project id should be 0 not $projectid"
2743         change_project -p 1024 $TESTFILE
2744         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2745         [ $projectid -ne 1024 ] &&
2746                 error "Project id should be 1024 not $projectid"
2747
2748         stopall || error "failed to stopall (1)"
2749         mount
2750         setupall
2751         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2752         [ $projectid -ne 1024 ] &&
2753                 error "Project id should be 1024 not $projectid"
2754
2755         cleanup_quota_test
2756 }
2757 run_test 39 "Project ID interface works correctly"
2758
2759 test_40a() {
2760         ! is_project_quota_supported &&
2761                 skip "Project quota is not supported"
2762         local dir1="$DIR/$tdir/dir1"
2763         local dir2="$DIR/$tdir/dir2"
2764
2765         setup_quota_test || error "setup quota failed with $?"
2766
2767         mkdir -p $dir1 $dir2
2768         change_project -sp 1 $dir1 && touch $dir1/1
2769         change_project -sp 2 $dir2
2770
2771         ln $dir1/1 $dir2/1_link &&
2772                 error "Hard link across different project quota should fail"
2773         rm -rf $dir1 $dir2
2774
2775         cleanup_quota_test
2776 }
2777 run_test 40a "Hard link across different project ID"
2778
2779 test_40b() {
2780         ! is_project_quota_supported &&
2781                 skip "Project quota is not supported"
2782         local dir1="$DIR/$tdir/dir1"
2783         local dir2="$DIR/$tdir/dir2"
2784
2785         setup_quota_test || error "setup quota failed with $?"
2786         mkdir -p $dir1 $dir2
2787         change_project -sp 1 $dir1 && touch $dir1/1
2788         change_project -sp 2 $dir2
2789
2790         mv $dir1/1 $dir2/2 || error "mv failed $?"
2791         local projid=$(lfs project $dir2/2 | awk '{print $1}')
2792         if [ "$projid" != "2" ]; then
2793                 error "project id expected 2 not $projid"
2794         fi
2795         rm -rf $dir1 $dir2
2796         cleanup_quota_test
2797 }
2798 run_test 40b "Mv across different project ID"
2799
2800 test_40c() {
2801         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2802                 ! is_project_quota_supported &&
2803                         skip "Project quota is not supported"
2804
2805         setup_quota_test || error "setup quota failed with $?"
2806         local dir="$DIR/$tdir/dir"
2807
2808         mkdir -p $dir && change_project -sp 1 $dir
2809         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
2810         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
2811         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
2812         touch $dir/remote_dir/file
2813         #verify inherit works file for remote dir.
2814         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
2815         [ "$projid" != "1" ] &&
2816                 error "file under remote dir expected 1 not $projid"
2817
2818         #Agent inode should be ignored for project quota
2819         USED=$(getquota -p 1 global curinodes)
2820         [ "$USED" != "3" ] &&
2821                 error "file count expected 3 got $USED"
2822
2823         rm -rf $dir
2824         cleanup_quota_test
2825         return 0
2826 }
2827 run_test 40c "Remote child Dir inherit project quota properly"
2828
2829 test_50() {
2830         ! is_project_quota_supported &&
2831                 skip "Project quota is not supported"
2832
2833         setup_quota_test || error "setup quota failed with $?"
2834         local dir1="$DIR/$tdir/dir1"
2835         local dir2="$DIR/$tdir/dir2"
2836
2837         mkdir -p $dir1 && change_project -sp 1 $dir1
2838         mkdir -p $dir2 && change_project -sp 2 $dir2
2839         for num in $(seq 1 10); do
2840                 touch $dir1/file_$num $dir2/file_$num
2841                 ln -s $dir1/file_$num $dir1/file_$num"_link"
2842                 ln -s $dir2/file_$num $dir2/file_$num"_link"
2843         done
2844
2845         count=$($LFS find --projid 1 $DIR | wc -l)
2846         [ "$count" != 21 ] && error "expected 21 but got $count"
2847
2848         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
2849         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
2850         [ "$count" != 22 ] && error "expected 22 but got $count"
2851
2852         rm -rf $dir1 $dir2
2853         cleanup_quota_test
2854 }
2855 run_test 50 "Test if lfs find --projid works"
2856
2857 test_51() {
2858         ! is_project_quota_supported &&
2859                 skip "Project quota is not supported"
2860         setup_quota_test || error "setup quota failed with $?"
2861         local dir="$DIR/$tdir/dir"
2862
2863         mkdir $dir && change_project -sp 1 $dir
2864         local used=$(getquota -p 1 global curinodes)
2865         [ $used != "1" ] && error "expected 1 got $used"
2866
2867         touch $dir/1
2868         touch $dir/2
2869         cp $dir/2 $dir/3
2870         used=$(getquota -p 1 global curinodes)
2871         [ $used != "4" ] && error "expected 4 got $used"
2872
2873         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
2874         #try cp to dir
2875         cp $DIR/$tdir/6 $dir/6
2876         used=$(getquota -p 1 global curinodes)
2877         [ $used != "5" ] && error "expected 5 got $used"
2878
2879         #try mv to dir
2880         mv $DIR/$tdir/6 $dir/7
2881         used=$(getquota -p 1 global curinodes)
2882         [ $used != "6" ] && error "expected 6 got $used"
2883
2884         rm -rf $dir
2885         cleanup_quota_test
2886 }
2887 run_test 51 "Test project accounting with mv/cp"
2888
2889 test_52() {
2890         ! is_project_quota_supported &&
2891                 skip "Project quota is not supported"
2892         setup_quota_test || error "setup quota failed with $?"
2893         local dir="$DIR/$tdir/dir"
2894         mkdir $dir && change_project -sp 1 $dir
2895
2896         touch $DIR/$tdir/file
2897         #Try renaming a file into the project.  This should fail.
2898         for num in $(seq 1 2000); do
2899                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
2900                         error "rename should fail"
2901         done
2902         rm -rf $dir
2903         cleanup_quota_test
2904 }
2905 run_test 52 "Rename across different project ID"
2906
2907 test_53() {
2908         ! is_project_quota_supported &&
2909                 skip "Project quota is not supported"
2910         setup_quota_test || error "setup quota failed with $?"
2911         local dir="$DIR/$tdir/dir"
2912         mkdir $dir && change_project -s $dir
2913         lfs project -d $dir | grep P || error "inherit attribute should be set"
2914
2915         change_project -C $dir
2916         lfs project -d $dir | grep P &&
2917                 error "inherit attribute should be cleared"
2918
2919         rm -rf $dir
2920         cleanup_quota_test
2921 }
2922 run_test 53 "Project inherit attribute could be cleared"
2923
2924 test_54() {
2925         ! is_project_quota_supported &&
2926                 skip "Project quota is not supported"
2927         setup_quota_test || error "setup quota failed with $?"
2928         trap cleanup_quota_test EXIT
2929         local testfile="$DIR/$tdir/$tfile-0"
2930
2931         #set project ID/inherit attribute
2932         change_project -sp $TSTPRJID $DIR/$tdir
2933         $RUNAS createmany -m ${testfile} 100 ||
2934                 error "create many files failed"
2935
2936         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
2937         # one more count for directory itself */
2938         ((proj_count++))
2939
2940         #check project
2941         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2942         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
2943
2944         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
2945         [ $proj_count1 -eq $proj_count ] ||
2946                         error "c2: expected $proj_count got $proj_count1"
2947
2948         #clear project but with kept projid
2949         change_project -rCk $DIR/$tdir
2950         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2951         [ $proj_count1 -eq 1 ] ||
2952                         error "c3: expected 1 got $proj_count1"
2953
2954         #verify projid untouched.
2955         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2956         ((proj_count1++))
2957         [ $proj_count1 -eq $proj_count ] ||
2958                         error "c4: expected $proj_count got $proj_count1"
2959
2960         # test -0 option
2961         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
2962         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2963         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
2964
2965         #this time clear all
2966         change_project -rC $DIR/$tdir
2967         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2968         [ $proj_count1 -eq 0 ] ||
2969                         error "c6: expected 0 got $proj_count1"
2970         #cleanup
2971         unlinkmany ${testfile} 100 ||
2972                 error "unlink many files failed"
2973
2974         cleanup_quota_test
2975 }
2976 run_test 54 "basic lfs project interface test"
2977
2978 test_55() {
2979         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
2980                 skip "Not supported before 2.10.58."
2981         setup_quota_test || error "setup quota failed with $?"
2982
2983         set_ost_qtype $QTYPE || error "enable ost quota failed"
2984         quota_init
2985
2986         #add second group to TSTUSR
2987         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
2988
2989         #prepare test file
2990         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
2991         error "failed to dd"
2992
2993         cancel_lru_locks osc
2994         sync; sync_all_data || true
2995
2996         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
2997         error "failed to setquota on group $TSTUSR2"
2998
2999         $LFS quota -v -g $TSTUSR2 $DIR
3000
3001         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3002         error "chgrp should failed with -EDQUOT"
3003
3004         USED=$(getquota -g $TSTUSR2 global curspace)
3005         echo "$USED"
3006
3007         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3008         error "failed to setquota on group $TSTUSR2"
3009
3010         $LFS quota -v -g $TSTUSR2 $DIR
3011
3012         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3013         error "chgrp should succeed"
3014
3015         $LFS quota -v -g $TSTUSR2 $DIR
3016
3017         cleanup_quota_test
3018 }
3019 run_test 55 "Chgrp should be affected by group quota"
3020
3021 test_56() {
3022         setup_quota_test || error "setup quota failed with $?"
3023
3024         set_ost_qtype $QTYPE || error "enable ost quota failed"
3025         quota_init
3026
3027         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3028                 erro "failed to set grace time for usr quota"
3029         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3030                      awk '{print $4 $8}')
3031         if [ "x$grace_time" != "x10s;10s" ]; then
3032                 $LFS quota -t -u $DIR
3033                 error "expected grace time: 10s;10s, got:$grace_time"
3034         fi
3035
3036         cleanup_quota_test
3037 }
3038 run_test 56 "lfs quota -t should work well"
3039
3040 test_57() {
3041         setup_quota_test || error "setup quota failed with $?"
3042
3043         local dir="$DIR/$tdir/dir"
3044         mkdir -p $dir
3045         mkfifo $dir/pipe
3046         #try to change pipe file should not hang and return failure
3047         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3048                 awk -F ':' '{ print \\\$2 }'" \
3049                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3050         #command can process further if it hit some errors
3051         touch $dir/aaa $dir/bbb
3052         mkdir $dir/subdir -p
3053         touch $dir/subdir/aaa $dir/subdir/bbb
3054         #create one invalid link file
3055         ln -s $dir/not_exist_file $dir/ccc
3056         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3057         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3058
3059         cleanup_quota_test
3060 }
3061 run_test 57 "lfs project could tolerate errors"
3062
3063 test_59() {
3064         [ "$mds1_FSTYPE" != ldiskfs ] &&
3065                 skip "ldiskfs only test"
3066         disable_project_quota
3067         setup_quota_test || error "setup quota failed with $?"
3068         quota_init
3069
3070         local testfile="$DIR/$tdir/$tfile-0"
3071         #make sure it did not crash kernel
3072         touch $testfile && lfs project -sp 1 $testfile
3073
3074         enable_project_quota
3075         cleanup_quota_test
3076 }
3077 run_test 59 "lfs project dosen't crash kernel with project disabled"
3078
3079 test_60() {
3080         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3081                 skip "Needs MDS version 2.11.53 or later."
3082         setup_quota_test || error "setup quota failed with $?"
3083         trap cleanup_quota_test EXIT
3084
3085         local testfile=$DIR/$tdir/$tfile
3086         local limit=100
3087
3088         set_mdt_qtype "ug" || error "enable mdt quota failed"
3089
3090         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3091                 error "set quota failed"
3092         quota_show_check a g $TSTUSR
3093
3094         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3095         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3096         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3097                 error "create many files failed"
3098
3099         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3100
3101         # root user can overrun quota
3102         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3103                 error "root user should succeed"
3104
3105         cleanup_quota_test
3106 }
3107 run_test 60 "Test quota for root with setgid"
3108
3109 # test default quota
3110 test_default_quota() {
3111         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3112                 skip "Not supported before 2.11.51."
3113
3114         local qtype=$1
3115         local qpool=$2
3116         local qid=$TSTUSR
3117         local qprjid=$TSTPRJID
3118         local qdtype="-U"
3119         local qs="-b"
3120         local qh="-B"
3121         local LIMIT=20480 #20M disk space
3122         local TESTFILE="$DIR/$tdir/$tfile-0"
3123
3124         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3125                 echo "Project quota is not supported" && return 0
3126
3127         [ $qtype == "-u" ] && qdtype="-U"
3128         [ $qtype == "-g" ] && qdtype="-G"
3129         [ $qtype == "-p" ] && {
3130                 qdtype="-P"
3131                 qid=$qprjid
3132         }
3133
3134         [ $qpool == "meta" ] && {
3135                 LIMIT=10240 #10K inodes
3136                 qs="-i"
3137                 qh="-I"
3138         }
3139
3140         setup_quota_test || error "setup quota failed with $?"
3141         trap cleanup_quota_test EXIT
3142
3143         quota_init
3144
3145         # enable mdt/ost quota
3146         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3147         set_ost_qtype $QTYPE || error "enable ost quota failed"
3148
3149         log "set to use default quota"
3150         $LFS setquota $qtype $qid -d $DIR ||
3151                 error "set $qid to use default quota failed"
3152
3153         log "set default quota"
3154         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3155                 error "set $qid default quota failed"
3156
3157         log "get default quota"
3158         $LFS quota $qdtype $DIR || error "get default quota failed"
3159
3160         if [ $qpool == "data" ]; then
3161                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3162                                                         awk '{print $2}')
3163                 [ $SLIMIT -eq $LIMIT ] ||
3164                         error "the returned default quota is wrong"
3165         else
3166                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3167                                                         awk '{print $5}')
3168                 [ $SLIMIT -eq $LIMIT ] ||
3169                         error "the returned default quota is wrong"
3170         fi
3171
3172         # make sure the system is clean
3173         local USED=$(getquota $qtype $qid global curspace)
3174         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3175
3176         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3177         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3178
3179         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3180
3181         log "Test not out of quota"
3182         if [ $qpool == "data" ]; then
3183                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3184                         quota_error $qtype $qid "write failed, expect succeed"
3185         else
3186                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3187                         quota_error $qtype $qid "create failed, expect succeed"
3188
3189                 unlinkmany $TESTFILE $((LIMIT/2))
3190         fi
3191
3192         log "Test out of quota"
3193         # flush cache, ensure noquota flag is set on client
3194         cancel_lru_locks osc
3195         cancel_lru_locks mdc
3196         sync; sync_all_data || true
3197         if [ $qpool == "data" ]; then
3198                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3199                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3200         else
3201                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3202                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3203
3204                 unlinkmany $TESTFILE $((LIMIT*2))
3205         fi
3206
3207         log "Increase default quota"
3208         # increase default quota
3209         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3210                 error "set default quota failed"
3211
3212         cancel_lru_locks osc
3213         cancel_lru_locks mdc
3214         sync; sync_all_data || true
3215         if [ $qpool == "data" ]; then
3216                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3217                         quota_error $qtype $qid "write failed, expect succeed"
3218         else
3219                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3220                         quota_error $qtype $qid "create failed, expect succeed"
3221
3222                 unlinkmany $TESTFILE $((LIMIT*2))
3223         fi
3224
3225         log "Set quota to override default quota"
3226         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3227                 error "set $qid quota failed"
3228
3229         cancel_lru_locks osc
3230         cancel_lru_locks mdc
3231         sync; sync_all_data || true
3232         if [ $qpool == "data" ]; then
3233                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3234                         quota_error $qtype $qid "write succeed, expect EQUOT"
3235         else
3236                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3237                         quota_error $qtype $qid "create succeed, expect EQUOT"
3238
3239                 unlinkmany $TESTFILE $((LIMIT*2))
3240         fi
3241
3242         log "Set to use default quota again"
3243         $LFS setquota $qtype $qid -d $DIR ||
3244                 error "set $qid to use default quota failed"
3245
3246         cancel_lru_locks osc
3247         cancel_lru_locks mdc
3248         sync; sync_all_data || true
3249         if [ $qpool == "data" ]; then
3250                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3251                         quota_error $qtype $qid "write failed, expect succeed"
3252         else
3253                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3254                         quota_error $qtype $qid "create failed, expect succeed"
3255
3256                 unlinkmany $TESTFILE $((LIMIT*2))
3257         fi
3258
3259         log "Cleanup"
3260         rm -f $TESTFILE
3261         wait_delete_completed || error "wait_delete_completed failed"
3262         sync_all_data || true
3263         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3264                 error "reset default quota failed"
3265         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3266                 error "reset quota failed"
3267
3268         cleanup_quota_test
3269 }
3270
3271 test_61() {
3272         test_default_quota "-u" "data"
3273         test_default_quota "-u" "meta"
3274         test_default_quota "-g" "data"
3275         test_default_quota "-g" "meta"
3276         test_default_quota "-p" "data"
3277         test_default_quota "-p" "meta"
3278 }
3279 run_test 61 "default quota tests"
3280
3281 test_62() {
3282         ! is_project_quota_supported &&
3283                 skip "Project quota is not supported"
3284          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
3285                 skip "chattr did not support project quota"
3286         setup_quota_test || error "setup quota failed with $?"
3287         local testdir=$DIR/$tdir/
3288
3289         $RUNAS mkdir -p $testdir || error "failed to mkdir"
3290         change_project -s $testdir
3291         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3292                 error "inherit attribute should be set"
3293         # chattr used FS_IOC_SETFLAGS ioctl
3294         $RUNAS chattr -P $testdir &&
3295                 error "regular user clear inherit should fail"
3296         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3297                 error "inherit attribute should still be set"
3298         chattr -P $testdir || error "root failed to clear inherit"
3299         [[ $($LFS project -d $testdir) =~ "P" ]] &&
3300                 error "inherit attribute should be cleared"
3301         cleanup_quota_test
3302 }
3303 run_test 62 "Project inherit should be only changed by root"
3304
3305 test_dom() {
3306         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
3307                 skip "Not supported before 2.11.55"
3308
3309         local qtype=$1
3310         local qid=$TSTUSR
3311         local dd_failed=false
3312         local tdir_dom=${tdir}_dom
3313         local LIMIT=20480 #20M
3314
3315         [ $qtype == "p" ] && ! is_project_quota_supported &&
3316                 echo "Project quota is not supported" && return 0
3317
3318         [ $qtype == "p" ] && qid=$TSTPRJID
3319
3320         setup_quota_test || error "setup quota failed with $?"
3321         trap cleanup_quota_test EXIT
3322
3323         quota_init
3324
3325         # enable mdt/ost quota
3326         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3327         set_ost_qtype $QTYPE || error "enable ost quota failed"
3328
3329         # make sure the system is clean
3330         local USED=$(getquota -$qtype $qid global curspace)
3331         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3332
3333         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
3334
3335         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
3336         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
3337                 error "setstripe $tdir_dom failed"
3338         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
3339
3340         [ $qtype == "p" ] && {
3341                 change_project -sp $TSTPRJID $DIR/$tdir
3342                 change_project -sp $TSTPRJID $DIR/$tdir_dom
3343         }
3344
3345         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
3346                 error "set $qid quota failed"
3347
3348         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3349                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3350                                                                 dd_failed=true
3351         done
3352
3353         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3354
3355         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
3356                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3357                                                                 dd_failed=true
3358         done
3359
3360         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
3361
3362         rm -f $DIR/$tdir_dom/*
3363
3364         # flush cache, ensure noquota flag is set on client
3365         cancel_lru_locks osc
3366         cancel_lru_locks mdc
3367         sync; sync_all_data || true
3368
3369         dd_failed=false
3370
3371         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
3372                 quota_error $qtype $qid "write failed, expect succeed"
3373
3374         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
3375                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3376                                                                 dd_failed=true
3377         done
3378
3379         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
3380
3381         rm -f $DIR/$tdir/*
3382         rm -f $DIR/$tdir_dom/*
3383
3384         # flush cache, ensure noquota flag is set on client
3385         cancel_lru_locks osc
3386         cancel_lru_locks mdc
3387         sync; sync_all_data || true
3388
3389         dd_failed=false
3390
3391         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3392                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3393                                                                 dd_failed=true
3394         done
3395
3396         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3397
3398         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
3399                 quota_error $qtype $qid "write succeed, expect EDQUOT"
3400
3401         rm -f $DIR/$tdir/*
3402         rm -fr $DIR/$tdir_dom
3403
3404         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
3405                 error "reset usr quota failed"
3406
3407         cleanup_quota_test
3408 }
3409
3410 test_63() {
3411         test_dom "u"
3412         test_dom "g"
3413         test_dom "p"
3414 }
3415 run_test 63 "quota on DoM tests"
3416
3417 test_64() {
3418         ! is_project_quota_supported &&
3419                 skip "Project quota is not supported"
3420         setup_quota_test || error "setup quota failed with $?"
3421         local dir1="$DIR/$tdir/"
3422
3423         touch $dir1/file
3424         ln -s $dir1/file $dir1/file_link
3425
3426         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
3427                 error "set symlink file's project should fail"
3428
3429         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
3430                 error "get symlink file's project should fail"
3431
3432         cleanup_quota_test
3433 }
3434 run_test 64 "lfs project on symlink files should fail"
3435
3436 test_65() {
3437         local SIZE=10 #10M
3438         local TESTFILE="$DIR/$tdir/$tfile-0"
3439
3440         setup_quota_test || error "setup quota failed with $?"
3441         set_ost_qtype $QTYPE || error "enable ost quota failed"
3442         quota_init
3443
3444         echo "Write..."
3445         $RUNAS $DD of=$TESTFILE count=$SIZE ||
3446                 error "failed to write"
3447         # flush cache, ensure noquota flag is set on client
3448         cancel_lru_locks osc
3449         sync; sync_all_data || true
3450
3451         local quota_u=$($LFS quota -u $TSTUSR $DIR)
3452         local quota_g=$($LFS quota -g $TSTUSR $DIR)
3453         local quota_all=$($RUNAS $LFS quota $DIR)
3454
3455         [ "$(echo "$quota_all" | head -n3)" != "$quota_u" ] &&
3456                 error "usr quota not match"
3457         [ "$(echo "$quota_all" | tail -n3)" != "$quota_g" ] &&
3458                 error "grp quota not match"
3459
3460         rm -f $TESTFILE
3461         # cleanup
3462         cleanup_quota_test
3463 }
3464 run_test 65 "Check lfs quota result"
3465
3466 quota_fini()
3467 {
3468         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
3469         if $PQ_CLEANUP; then
3470                 disable_project_quota
3471         fi
3472 }
3473 reset_quota_settings
3474 quota_fini
3475
3476 cd $ORIG_PWD
3477 complete $SECONDS
3478 check_and_cleanup_lustre
3479 export QUOTA_AUTO=$QUOTA_AUTO_OLD
3480 exit_status