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