Whamcloud - gitweb
LU-5638 tests: Skip sanity-quota tests for ZFS
[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 ALWAYS_EXCEPT="$SANITY_QUOTA_EXCEPT"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 [ "$ALWAYS_EXCEPT$EXCEPT" ] &&
17         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
18
19 TMP=${TMP:-/tmp}
20
21 ORIG_PWD=${PWD}
22 TSTID=${TSTID:-60000}
23 TSTID2=${TSTID2:-60001}
24 TSTUSR=${TSTUSR:-"quota_usr"}
25 TSTUSR2=${TSTUSR2:-"quota_2usr"}
26 BLK_SZ=1024
27 MAX_DQ_TIME=604800
28 MAX_IQ_TIME=604800
29
30 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
31 . $LUSTRE/tests/test-framework.sh
32 init_test_env $@
33 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
34 init_logging
35 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
36
37 require_dsh_mds || exit 0
38 require_dsh_ost || exit 0
39
40 # XXX Once we drop the interoperability with old server (< 2.3.50), the
41 #     sanity-quota-old.sh should be removed.
42 if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.50) ]; then
43         exec $LUSTRE/tests/sanity-quota-old.sh
44 fi
45
46 # Does e2fsprogs support quota feature?
47 if [ $(facet_fstype $SINGLEMDS) == ldiskfs ] &&
48         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
49                 grep -q 'quota'"; then
50         skip "e2fsprogs doesn't support quota" && exit 0
51 fi
52
53 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
54 # bug number for skipped test:        LU-2836 LU-2836 LU-2059 LU-5638
55         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 3       6       7d      11 33 34 35"
56 # bug number:     LU-2887
57         ZFS_SLOW="12a"
58 fi
59
60 [ "$SLOW" = "no" ] && EXCEPT_SLOW="$ZFS_SLOW 9 18 21"
61
62 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
63
64 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
65
66 DIR=${DIR:-$MOUNT}
67 DIR2=${DIR2:-$MOUNT2}
68
69 QUOTA_AUTO_OLD=$QUOTA_AUTO
70 export QUOTA_AUTO=0
71
72 check_and_setup_lustre
73
74 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
75 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
76
77 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
78 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
79 SHOW_QUOTA_USER2="$LFS quota -v -u $TSTUSR2 $DIR"
80 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
81 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
82 SHOW_QUOTA_GROUP2="$LFS quota -v -g $TSTUSR2 $DIR"
83 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
84 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $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 RUNAS="runas -u $TSTID -g $TSTID"
104 RUNAS2="runas -u $TSTID2 -g $TSTID2"
105 DD="dd if=/dev/zero bs=1M"
106
107 FAIL_ON_ERROR=false
108
109 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
110         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
111 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
112         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
113
114 # clear quota limits for a user or a group
115 # usage: resetquota -u username
116 #        resetquota -g groupname
117
118 resetquota() {
119         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
120         [ "$1" != "-u" -a "$1" != "-g" ] &&
121                 error "resetquota: wrong specifier $1 passed"
122
123         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
124                 error "clear quota for [type:$1 name:$2] failed"
125         # give a chance to slave to release space
126         sleep 1
127 }
128
129 quota_scan() {
130         local LOCAL_UG=$1
131         local LOCAL_ID=$2
132
133         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "u" ]; then
134                 $LFS quota -v -u $LOCAL_ID $DIR
135                 log "Files for user ($LOCAL_ID):"
136                 ($LFS find -user $LOCAL_ID $DIR | head -n 4 |
137                         xargs stat 2>/dev/null)
138         fi
139
140         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "g" ]; then
141                 $LFS quota -v -g $LOCAL_ID $DIR
142                 log "Files for group ($LOCAL_ID):"
143                 ($LFS find -group $LOCAL_ID $DIR | head -n 4 |
144                         xargs stat 2>/dev/null)
145         fi
146 }
147
148 quota_error() {
149         quota_scan $1 $2
150         shift 2
151         error "$*"
152 }
153
154 quota_log() {
155         quota_scan $1 $2
156         shift 2
157         log "$*"
158 }
159
160 # get quota for a user or a group
161 # usage: getquota -u|-g <username>|<groupname> global|<obd_uuid> \
162 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
163 #
164 getquota() {
165         local spec
166         local uuid
167
168         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
169         [ "$1" != "-u" -a "$1" != "-g" ] &&
170                 error "getquota: wrong u/g specifier $1 passed"
171
172         uuid="$3"
173
174         case "$4" in
175                 curspace)   spec=1;;
176                 bsoftlimit) spec=2;;
177                 bhardlimit) spec=3;;
178                 bgrace)     spec=4;;
179                 curinodes)  spec=5;;
180                 isoftlimit) spec=6;;
181                 ihardlimit) spec=7;;
182                 igrace)     spec=8;;
183                 *)          error "unknown quota parameter $4";;
184         esac
185
186         [ "$uuid" = "global" ] && uuid=$DIR
187
188         $LFS quota -v "$1" "$2" $DIR |
189                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
190                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
191                 | tr -d "*"
192 }
193
194 # set mdt quota type
195 # usage: set_mdt_qtype ug|u|g|none
196 set_mdt_qtype() {
197         local qtype=$1
198         local varsvc
199         local mdts=$(get_facets MDS)
200         local cmd
201         do_facet mgs $LCTL conf_param $FSNAME.quota.mdt=$qtype
202         # we have to make sure each MDT received config changes
203         for mdt in ${mdts//,/ }; do
204                 varsvc=${mdt}_svc
205                 cmd="$LCTL get_param -n "
206                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
207                 cmd=${cmd}.quota_slave.enabled
208
209                 if $(facet_up $mdt); then
210                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
211                 fi
212         done
213         return 0
214 }
215
216 # set ost quota type
217 # usage: set_ost_qtype ug|u|g|none
218 set_ost_qtype() {
219         local qtype=$1
220         local varsvc
221         local osts=$(get_facets OST)
222         local cmd
223         do_facet mgs $LCTL conf_param $FSNAME.quota.ost=$qtype
224         # we have to make sure each OST received config changes
225         for ost in ${osts//,/ }; do
226                 varsvc=${ost}_svc
227                 cmd="$LCTL get_param -n "
228                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
229                 cmd=${cmd}.quota_slave.enabled
230
231                 if $(facet_up $ost); then
232                         wait_update_facet $ost "$cmd" "$qtype" || return 1
233                 fi
234         done
235         return 0
236 }
237
238 wait_reintegration() {
239         local ntype=$1
240         local qtype=$2
241         local max=$3
242         local result="glb[1],slv[1],reint[0]"
243         local varsvc
244         local cmd
245         local tgts
246
247         if [ $ntype == "mdt" ]; then
248                 tgts=$(get_facets MDS)
249         else
250                 tgts=$(get_facets OST)
251         fi
252
253         for tgt in ${tgts//,/ }; do
254                 varsvc=${tgt}_svc
255                 cmd="$LCTL get_param -n "
256                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
257                 cmd=${cmd}.quota_slave.info
258
259                 if $(facet_up $tgt); then
260                         wait_update_facet $tgt "$cmd |
261                                 grep "$qtype" | awk '{ print \\\$3 }'" \
262                                         "$result" $max || return 1
263                 fi
264         done
265         return 0
266 }
267
268 wait_mdt_reint() {
269         local qtype=$1
270         local max=${2:-90}
271
272         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
273                 wait_reintegration "mdt" "user" $max || return 1
274         fi
275
276         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
277                 wait_reintegration "mdt" "group" $max || return 1
278         fi
279         return 0
280 }
281
282 wait_ost_reint() {
283         local qtype=$1
284         local max=${2:-90}
285
286         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
287                 wait_reintegration "ost" "user" $max || return 1
288         fi
289
290         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
291                 wait_reintegration "ost" "group" $max || return 1
292         fi
293         return 0
294 }
295
296 setup_quota_test() {
297         rm -rf $DIR/$tdir
298         wait_delete_completed
299         echo "Creating test directory"
300         mkdir -p $DIR/$tdir
301         chmod 0777 $DIR/$tdir
302         # always clear fail_loc in case of fail_loc isn't cleared
303         # properly when previous test failed
304         lustre_fail mds_ost 0
305 }
306
307 cleanup_quota_test() {
308         trap 0
309         echo "Delete files..."
310         rm -rf $DIR/$tdir
311         echo "Wait for unlink objects finished..."
312         wait_delete_completed
313         sync_all_data || true
314 }
315
316 quota_show_check() {
317         local bf=$1
318         local ug=$2
319         local qid=$3
320         local usage
321
322         $LFS quota -v -$ug $qid $DIR
323
324         if [ "$bf" == "a" -o "$bf" == "b" ]; then
325                 usage=$(getquota -$ug $qid global curspace)
326                 if [ -z $usage ]; then
327                         quota_error $ug $qid \
328                                 "Query block quota failed ($ug:$qid)."
329                 else
330                         [ $usage -ne 0 ] && quota_log $ug $qid \
331                                 "Block quota isn't 0 ($ug:$qid:$usage)."
332                 fi
333         fi
334
335         if [ "$bf" == "a" -o "$bf" == "f" ]; then
336                 usage=$(getquota -$ug $qid global curinodes)
337                 if [ -z $usage ]; then
338                         quota_error $ug $qid \
339                                 "Query file quota failed ($ug:$qid)."
340                 else
341                         [ $usage -ne 0 ] && quota_log $ug $qid \
342                                 "File quota isn't 0 ($ug:$qid:$usage)."
343                 fi
344         fi
345 }
346
347 # enable quota debug
348 quota_init() {
349         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
350 }
351 quota_init
352
353 resetquota -u $TSTUSR
354 resetquota -g $TSTUSR
355 resetquota -u $TSTUSR2
356 resetquota -g $TSTUSR2
357
358 test_quota_performance() {
359         local TESTFILE="$DIR/$tdir/$tfile-0"
360         local size=$1 # in MB
361         local stime=$(date +%s)
362         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
363                 quota_error u $TSTUSR "write failure"
364         local etime=$(date +%s)
365         delta=$((etime - stime))
366         if [ $delta -gt 0 ]; then
367             rate=$((size * 1024 / delta))
368             if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
369                 # LU-2872 - see LU-2887 for fix
370                 [ $rate -gt 64 ] ||
371                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
372             else
373                 [ $rate -gt 1024 ] ||
374                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
375             fi
376         fi
377         rm -f $TESTFILE
378 }
379
380 # test basic quota performance b=21696
381 test_0() {
382         local MB=100 # 100M
383         [ "$SLOW" = "no" ] && MB=10
384
385         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
386         [ $free_space -le $((MB * 1024)) ] &&
387                 skip "not enough space ${free_space} KB, " \
388                         "required $((MB * 1024)) KB" && return
389         setup_quota_test
390         trap cleanup_quota_test EXIT
391
392         set_ost_qtype "none" || error "disable ost quota failed"
393         test_quota_performance $MB
394
395         set_ost_qtype "ug" || error "enable ost quota failed"
396         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
397                 error "set quota failed"
398         test_quota_performance $MB
399
400         cleanup_quota_test
401         resetquota -u $TSTUSR
402 }
403 run_test 0 "Test basic quota performance"
404
405 # test block hardlimit
406 test_1() {
407         local LIMIT=10  # 10M
408         local TESTFILE="$DIR/$tdir/$tfile-0"
409
410         setup_quota_test
411         trap cleanup_quota_test EXIT
412
413         # enable ost quota
414         set_ost_qtype "ug" || "enable ost quota failed"
415
416         # test for user
417         log "User quota (block hardlimit:$LIMIT MB)"
418         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
419                 error "set user quota failed"
420
421         # make sure the system is clean
422         local USED=$(getquota -u $TSTUSR global curspace)
423         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
424
425         $LFS setstripe $TESTFILE -c 1
426         chown $TSTUSR.$TSTUSR $TESTFILE
427
428         log "Write..."
429         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
430                 quota_error u $TSTUSR "user write failure, but expect success"
431         log "Write out of block quota ..."
432         # this time maybe cache write,  ignore it's failure
433         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
434         # flush cache, ensure noquota flag is set on client
435         cancel_lru_locks osc
436         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
437                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
438
439         rm -f $TESTFILE
440         wait_delete_completed
441         sync_all_data || true
442         USED=$(getquota -u $TSTUSR global curspace)
443         [ $USED -ne 0 ] && quota_error u $TSTUSR \
444                 "user quota isn't released after deletion"
445         resetquota -u $TSTUSR
446
447         # test for group
448         log "--------------------------------------"
449         log "Group quota (block hardlimit:$LIMIT MB)"
450         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
451                 error "set group quota failed"
452
453         TESTFILE="$DIR/$tdir/$tfile-1"
454         # make sure the system is clean
455         USED=$(getquota -g $TSTUSR global curspace)
456         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0"
457
458         $LFS setstripe $TESTFILE -c 1
459         chown $TSTUSR.$TSTUSR $TESTFILE
460
461         log "Write ..."
462         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
463                 quota_error g $TSTUSR "group write failure, but expect success"
464         log "Write out of block quota ..."
465         # this time maybe cache write, ignore it's failure
466         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
467         cancel_lru_locks osc
468         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
469                 quota_error g $TSTUSR "group write success, but expect EDQUOT"
470
471         # cleanup
472         cleanup_quota_test
473
474         USED=$(getquota -g $TSTUSR global curspace)
475         [ $USED -ne 0 ] && quota_error g $TSTUSR \
476                 "group quota isn't released after deletion"
477
478         resetquota -g $TSTUSR
479 }
480 run_test 1 "Block hard limit (normal use and out of quota)"
481
482 # test inode hardlimit
483 test_2() {
484         local LIMIT=$((1024 * 1024)) # 1M inodes
485         local TESTFILE="$DIR/$tdir/$tfile-0"
486
487         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
488
489         local FREE_INODES=$(mdt_free_inodes 0)
490         echo "$FREE_INODES free inodes on master MDT"
491         [ $FREE_INODES -lt $LIMIT ] &&
492                 skip "not enough free inodes $FREE_INODES required $LIMIT" &&
493                 return
494
495         setup_quota_test
496         trap cleanup_quota_test EXIT
497
498         # enable mdt quota
499         set_mdt_qtype "ug" || "enable mdt quota failed"
500
501         # test for user
502         log "User quota (inode hardlimit:$LIMIT files)"
503         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
504                 error "set user quota failed"
505
506         # make sure the system is clean
507         local USED=$(getquota -u $TSTUSR global curinodes)
508         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
509
510         log "Create $LIMIT files ..."
511         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
512                 quota_error u $TSTUSR "user create failure, but expect success"
513         log "Create out of file quota ..."
514         $RUNAS touch ${TESTFILE}_xxx && \
515                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
516
517         # cleanup
518         unlinkmany ${TESTFILE} $LIMIT
519         rm -f ${TESTFILE}_xxx
520         wait_delete_completed
521
522         USED=$(getquota -u $TSTUSR global curinodes)
523         [ $USED -ne 0 ] && quota_error u $TSTUSR \
524                 "user quota isn't released after deletion"
525         resetquota -u $TSTUSR
526
527         # test for group
528         log "--------------------------------------"
529         log "Group quota (inode hardlimit:$LIMIT files)"
530         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
531                 error "set group quota failed"
532
533         TESTFILE=$DIR/$tdir/$tfile-1
534         # make sure the system is clean
535         USED=$(getquota -g $TSTUSR global curinodes)
536         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
537
538         log "Create $LIMIT files ..."
539         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
540                 quota_error g $TSTUSR "group create failure, but expect success"
541         log "Create out of file quota ..."
542         $RUNAS touch ${TESTFILE}_xxx && \
543                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
544
545         # cleanup
546         unlinkmany ${TESTFILE} $LIMIT
547         rm -f ${TESTFILE}_xxx
548         wait_delete_completed
549
550         USED=$(getquota -g $TSTUSR global curinodes)
551         [ $USED -ne 0 ] && quota_error g $TSTUSR \
552                 "user quota isn't released after deletion"
553
554         cleanup_quota_test
555         resetquota -g $TSTUSR
556 }
557 run_test 2 "File hard limit (normal use and out of quota)"
558
559 test_block_soft() {
560         local TESTFILE=$1
561         local TIMER=$(($2 * 3 / 2))
562         local LIMIT=$3
563         local OFFSET=0
564
565         setup_quota_test
566         trap cleanup_quota_test EXIT
567
568         $LFS setstripe $TESTFILE -c 1 -i 0
569         chown $TSTUSR.$TSTUSR $TESTFILE
570
571         echo "Write up to soft limit"
572         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
573                 quota_error a $TSTUSR "write failure, but expect success"
574         OFFSET=$((LIMIT * 1024))
575         cancel_lru_locks osc
576
577         echo "Write to exceed soft limit"
578         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
579                 quota_error a $TSTUSR "write failure, but expect success"
580         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
581         cancel_lru_locks osc
582
583         $SHOW_QUOTA_USER
584         $SHOW_QUOTA_GROUP
585         $SHOW_QUOTA_INFO_USER
586         $SHOW_QUOTA_INFO_GROUP
587
588         echo "Write before timer goes off"
589         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
590                 quota_error a $TSTUSR "write failure, but expect success"
591         OFFSET=$((OFFSET + 1024))
592         cancel_lru_locks osc
593
594         echo "Sleep $TIMER seconds ..."
595         sleep $TIMER
596
597         $SHOW_QUOTA_USER
598         $SHOW_QUOTA_GROUP
599         $SHOW_QUOTA_INFO_USER
600         $SHOW_QUOTA_INFO_GROUP
601
602         echo "Write after timer goes off"
603         # maybe cache write, ignore.
604         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
605         OFFSET=$((OFFSET + 1024))
606         cancel_lru_locks osc
607         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
608                 quota_error a $TSTUSR "write success, but expect EDQUOT"
609
610         $SHOW_QUOTA_USER
611         $SHOW_QUOTA_GROUP
612         $SHOW_QUOTA_INFO_USER
613         $SHOW_QUOTA_INFO_GROUP
614
615         echo "Unlink file to stop timer"
616         rm -f $TESTFILE
617         wait_delete_completed
618         sync_all_data || true
619
620         $SHOW_QUOTA_USER
621         $SHOW_QUOTA_GROUP
622         $SHOW_QUOTA_INFO_USER
623         $SHOW_QUOTA_INFO_GROUP
624
625         $LFS setstripe $TESTFILE -c 1 -i 0
626         chown $TSTUSR.$TSTUSR $TESTFILE
627
628         echo "Write ..."
629         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
630                 quota_error a $TSTUSR "write failure, but expect success"
631         # cleanup
632         cleanup_quota_test
633 }
634
635 # block soft limit
636 test_3() {
637         local LIMIT=1  # 1MB
638         local GRACE=20 # 20s
639         local TESTFILE=$DIR/$tdir/$tfile-0
640
641         set_ost_qtype "ug" || error "enable ost quota failed"
642
643         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
644         # make sure the system is clean
645         local USED=$(getquota -u $TSTUSR global curspace)
646         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
647
648         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
649                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
650         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
651                 error "set user quota failed"
652
653         test_block_soft $TESTFILE $GRACE $LIMIT
654         resetquota -u $TSTUSR
655
656         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
657         TESTFILE=$DIR/$tdir/$tfile-1
658         # make sure the system is clean
659         USED=$(getquota -g $TSTUSR global curspace)
660         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
661
662         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
663                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
664         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
665                 error "set group quota failed"
666
667         test_block_soft $TESTFILE $GRACE $LIMIT
668         resetquota -g $TSTUSR
669
670         # cleanup
671         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
672                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
673         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
674                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
675 }
676 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
677
678 test_file_soft() {
679         local TESTFILE=$1
680         local LIMIT=$2
681         local grace=$3
682         local TIMER=$(($grace * 3 / 2))
683
684         setup_quota_test
685         trap cleanup_quota_test EXIT
686
687         echo "Create files to exceed soft limit"
688         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
689                 quota_error a $TSTUSR "create failure, but expect success"
690         local trigger_time=$(date +%s)
691
692         sync_all_data || true
693
694         local cur_time=$(date +%s)
695         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
696                 error "Passed grace time $grace, $trigger_time, $cur_time"
697
698         echo "Create file before timer goes off"
699         $RUNAS touch ${TESTFILE}_before ||
700                 quota_error a $TSTUSR "failed create before timer expired," \
701                         "but expect success. $trigger_time, $cur_time"
702         sync_all_data || true
703
704         echo "Sleep $TIMER seconds ..."
705         sleep $TIMER
706
707         $SHOW_QUOTA_USER
708         $SHOW_QUOTA_GROUP
709         $SHOW_QUOTA_INFO_USER
710         $SHOW_QUOTA_INFO_GROUP
711
712         echo "Create file after timer goes off"
713         # There is a window that space is accounted in the quota usage but
714         # hasn't been decreased from the pending write, if we acquire quota
715         # in this window, we'll acquire more than we needed.
716         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
717         sync_all_data || true
718         $RUNAS touch ${TESTFILE}_after_3 &&
719                 quota_error a $TSTUSR "create after timer expired," \
720                         "but expect EDQUOT"
721         sync_all_data || true
722
723         $SHOW_QUOTA_USER
724         $SHOW_QUOTA_GROUP
725         $SHOW_QUOTA_INFO_USER
726         $SHOW_QUOTA_INFO_GROUP
727
728         echo "Unlink files to stop timer"
729         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
730         wait_delete_completed
731
732         echo "Create file"
733         $RUNAS touch ${TESTFILE}_xxx ||
734                 quota_error a $TSTUSR "touch after timer stop failure," \
735                         "but expect success"
736         sync_all_data || true
737
738         # cleanup
739         cleanup_quota_test
740 }
741
742 # file soft limit
743 test_4a() {
744         local LIMIT=10 # inodes
745         local TESTFILE=$DIR/$tdir/$tfile-0
746         local GRACE=12
747
748         set_mdt_qtype "ug" || error "enable mdt quota failed"
749
750         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
751         # make sure the system is clean
752         local USED=$(getquota -u $TSTUSR global curinodes)
753         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
754
755         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
756                 $GRACE $DIR || error "set user grace time failed"
757         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
758                 error "set user quota failed"
759
760         test_file_soft $TESTFILE $LIMIT $GRACE
761         resetquota -u $TSTUSR
762
763         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
764         # make sure the system is clean
765         USED=$(getquota -g $TSTUSR global curinodes)
766         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
767
768         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
769                 $GRACE $DIR || error "set group grace time failed"
770         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
771                 error "set group quota failed"
772         TESTFILE=$DIR/$tdir/$tfile-1
773
774         test_file_soft $TESTFILE $LIMIT $GRACE
775         resetquota -g $TSTUSR
776
777         # cleanup
778         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
779                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
780         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
781                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
782 }
783 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
784
785 test_4b() {
786         local GR_STR1="1w3d"
787         local GR_STR2="1000s"
788         local GR_STR3="5s"
789         local GR_STR4="1w2d3h4m5s"
790         local GR_STR5="5c"
791         local GR_STR6="1111111111111111"
792
793         wait_delete_completed
794
795         # test of valid grace strings handling
796         echo "Valid grace strings test"
797         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
798                 $GR_STR2 $DIR || error "set user grace time failed"
799         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
800         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
801                 $GR_STR4 $DIR || error "set group grace time quota failed"
802         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
803
804         # test of invalid grace strings handling
805         echo "  Invalid grace strings test"
806         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
807         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
808
809         # cleanup
810         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
811                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
812         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
813                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
814 }
815 run_test 4b "Grace time strings handling"
816
817 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
818 test_5() {
819         local BLIMIT=10 # 10M
820         local ILIMIT=10 # 10 inodes
821
822         setup_quota_test
823         trap cleanup_quota_test EXIT
824
825         set_mdt_qtype "ug" || error "enable mdt quota failed"
826         set_ost_qtype "ug" || error "enable ost quota failed"
827
828         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
829         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
830                 error "set user quota failed"
831         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
832                 error "set group quota failed"
833
834         # make sure the system is clean
835         local USED=$(getquota -u $TSTUSR global curinodes)
836         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
837         USED=$(getquota -g $TSTUSR global curinodes)
838         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
839         USED=$(getquota -u $TSTUSR global curspace)
840         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
841         USED=$(getquota -g $TSTUSR global curspace)
842         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
843
844         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
845         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
846                 error "create failure, expect success"
847         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
848                 error "write failure, expect success"
849
850         echo "Chown files to $TSTUSR.$TSTUSR ..."
851         for i in `seq 0 $ILIMIT`; do
852                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
853                         quota_error a $TSTUSR "chown failure, expect success"
854         done
855
856         # cleanup
857         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
858         cleanup_quota_test
859
860         resetquota -u $TSTUSR
861         resetquota -g $TSTUSR
862 }
863 run_test 5 "Chown & chgrp successfully even out of block/file quota"
864
865 # test dropping acquire request on master
866 test_6() {
867         local LIMIT=3 # 3M
868
869         # Clear dmesg so watchdog is not triggered by previous
870         # test output
871         do_facet ost1 dmesg -c > /dev/null
872
873         setup_quota_test
874         trap cleanup_quota_test EXIT
875
876         # make sure the system is clean
877         local USED=$(getquota -u $TSTUSR global curspace)
878         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
879
880         # make sure no granted quota on ost
881         set_ost_qtype "ug" || error "enable ost quota failed"
882         resetquota -u $TSTUSR
883
884         # create file for $TSTUSR
885         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
886         $LFS setstripe $TESTFILE -c 1 -i 0
887         chown $TSTUSR.$TSTUSR $TESTFILE
888
889         # create file for $TSTUSR2
890         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
891         $LFS setstripe $TESTFILE2 -c 1 -i 0
892         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
893
894         # cache per-ID lock for $TSTUSR on slave
895         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
896                 error "set quota failed"
897         $RUNAS $DD of=$TESTFILE count=1 ||
898                 error "write $TESTFILE failure, expect success"
899         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
900                 error "write $TESTFILE2 failure, expect success"
901         sync; sync
902         sync_all_data || true
903
904         #define QUOTA_DQACQ 601
905         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
906         lustre_fail mds 0x513 601
907
908         # write to un-enforced ID ($TSTUSR2) should succeed
909         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
910                 error "write failure, expect success"
911
912         # write to enforced ID ($TSTUSR) in background, exceeding limit
913         # to make sure DQACQ is sent
914         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
915         DDPID=$!
916
917         # watchdog timer uses a factor of 2
918         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
919         sleep $((TIMEOUT * 2 + 1))
920
921         # write should be blocked and never finished
922         if ! ps -p $DDPID  > /dev/null 2>&1; then
923                 lustre_fail mds 0 0
924                 error "write finished incorrectly!"
925         fi
926
927         lustre_fail mds 0 0
928
929         # no watchdog is triggered
930         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
931         watchdog=$(awk '/Service thread pid/ && /was inactive/ \
932                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
933         [ -z "$watchdog" ] || error "$watchdog"
934
935         rm -f $TMP/lustre-log-${TESTNAME}.log
936
937         # write should continue then fail with EDQUOT
938         local count=0
939         local c_size
940         while [ true ]; do
941                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
942                 if [ $count -ge 240 ]; then
943                         quota_error u $TSTUSR "dd not finished in $count secs"
944                 fi
945                 count=$((count + 1))
946                 if [ $((count % 30)) -eq 0 ]; then
947                         c_size=$(stat -c %s $TESTFILE)
948                         echo "Waiting $count secs. $c_size"
949                         $SHOW_QUOTA_USER
950                 fi
951                 sleep 1
952         done
953
954         cleanup_quota_test
955         resetquota -u $TSTUSR
956 }
957 run_test 6 "Test dropping acquire request on master"
958
959 # quota reintegration (global index)
960 test_7a() {
961         local TESTFILE=$DIR/$tdir/$tfile
962         local LIMIT=20 # 20M
963
964         [ "$SLOW" = "no" ] && LIMIT=5
965
966         setup_quota_test
967         trap cleanup_quota_test EXIT
968
969         # make sure the system is clean
970         local USED=$(getquota -u $TSTUSR global curspace)
971         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
972
973         # make sure no granted quota on ost1
974         set_ost_qtype "ug" || error "enable ost quota failed"
975         resetquota -u $TSTUSR
976         set_ost_qtype "none" || error "disable ost quota failed"
977
978         local OSTUUID=$(ostuuid_from_index 0)
979         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
980         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
981                 "$TSTUSR isn't 0."
982
983         # create test file
984         $LFS setstripe $TESTFILE -c 1 -i 0
985         chown $TSTUSR.$TSTUSR $TESTFILE
986
987         echo "Stop ost1..."
988         stop ost1
989
990         echo "Enable quota & set quota limit for $TSTUSR"
991         set_ost_qtype "ug" || error "enable ost quota failed"
992         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
993                 error "set quota failed"
994
995         echo "Start ost1..."
996         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
997         quota_init
998
999         wait_ost_reint "ug" || error "reintegration failed"
1000
1001         # hardlimit should have been fetched by slave during global
1002         # reintegration, write will exceed quota
1003         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1004                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1005
1006         rm -f $TESTFILE
1007         wait_delete_completed
1008         sync_all_data || true
1009         sleep 3
1010
1011         echo "Stop ost1..."
1012         stop ost1
1013
1014         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1015                 error "clear quota failed"
1016
1017         echo "Start ost1..."
1018         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
1019         quota_init
1020
1021         wait_ost_reint "ug" || error "reintegration failed"
1022
1023         # hardlimit should be cleared on slave during reintegration
1024         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1025                 quota_error u $TSTUSR "write error, but expect success"
1026
1027         cleanup_quota_test
1028         resetquota -u $TSTUSR
1029 }
1030 run_test 7a "Quota reintegration (global index)"
1031
1032 # quota reintegration (slave index)
1033 test_7b() {
1034         local LIMIT="100G"
1035         local TESTFILE=$DIR/$tdir/$tfile
1036
1037         setup_quota_test
1038         trap cleanup_quota_test EXIT
1039
1040         # make sure the system is clean
1041         local USED=$(getquota -u $TSTUSR global curspace)
1042         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1043
1044         # make sure no granted quota on ost1
1045         set_ost_qtype "ug" || error "enable ost quota failed"
1046         resetquota -u $TSTUSR
1047         set_ost_qtype "none" || error "disable ost quota failed"
1048
1049         local OSTUUID=$(ostuuid_from_index 0)
1050         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1051         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
1052                 "$TSTUSR isn't 0."
1053
1054         # create test file
1055         $LFS setstripe $TESTFILE -c 1 -i 0
1056         chown $TSTUSR.$TSTUSR $TESTFILE
1057
1058         # consume some space to make sure the granted space will not
1059         # be released during reconciliation
1060         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1061                 error "consume space failure, expect success"
1062
1063         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1064         lustre_fail mds 0xa02
1065
1066         set_ost_qtype "ug" || error "enable ost quota failed"
1067         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1068                 error "set quota failed"
1069
1070         # ignore the write error
1071         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1072
1073         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1074
1075         lustre_fail mds 0
1076
1077         echo "Restart ost to trigger reintegration..."
1078         stop ost1
1079         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
1080         quota_init
1081
1082         wait_ost_reint "ug" || error "reintegration failed"
1083
1084         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1085         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1086
1087         cleanup_quota_test
1088         resetquota -u $TSTUSR
1089         $SHOW_QUOTA_USER
1090 }
1091 run_test 7b "Quota reintegration (slave index)"
1092
1093 # quota reintegration (restart mds during reintegration)
1094 test_7c() {
1095         local LIMIT=20 # 20M
1096         local TESTFILE=$DIR/$tdir/$tfile
1097
1098         [ "$SLOW" = "no" ] && LIMIT=5
1099
1100         setup_quota_test
1101         trap cleanup_quota_test EXIT
1102
1103         # make sure the system is clean
1104         local USED=$(getquota -u $TSTUSR global curspace)
1105         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1106
1107         set_ost_qtype "none" || error "disable ost quota failed"
1108         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1109                 error "set quota failed"
1110
1111         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1112         lustre_fail ost 0xa03
1113
1114         # enable ost quota
1115         set_ost_qtype "ug" || error "enable ost quota failed"
1116         # trigger reintegration
1117         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
1118         procf=${procf}quota_slave.force_reint
1119         do_facet ost1 $LCTL set_param $procf=1 ||
1120                 error "force reintegration failed"
1121
1122         echo "Stop mds..."
1123         stop mds1
1124
1125         lustre_fail ost 0
1126
1127         echo "Start mds..."
1128         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1129         quota_init
1130
1131         # wait longer than usual to make sure the reintegration
1132         # is triggered by quota wb thread.
1133         wait_ost_reint "ug" 200 || error "reintegration failed"
1134
1135         # hardlimit should have been fetched by slave during global
1136         # reintegration, write will exceed quota
1137         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1138                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1139
1140         cleanup_quota_test
1141         resetquota -u $TSTUSR
1142 }
1143 run_test 7c "Quota reintegration (restart mds during reintegration)"
1144
1145 # Quota reintegration (Transfer index in multiple bulks)
1146 test_7d(){
1147         local TESTFILE=$DIR/$tdir/$tfile
1148         local TESTFILE1="$DIR/$tdir/$tfile"-1
1149         local limit=20 #20M
1150
1151         setup_quota_test
1152         trap cleanup_quota_test EXIT
1153
1154         set_ost_qtype "none" || error "disable ost quota failed"
1155         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1156                 error "set quota for $TSTUSR failed"
1157         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1158                 error "set quota for $TSTUSR2 failed"
1159
1160         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1161         lustre_fail mds 0x608 0
1162
1163         # enable quota to tirgger reintegration
1164         set_ost_qtype "u" || error "enable ost quota failed"
1165         wait_ost_reint "u" || error "reintegration failed"
1166
1167         lustre_fail mds 0
1168
1169         # hardlimit should have been fetched by slave during global
1170         # reintegration, write will exceed quota
1171         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1172                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1173
1174         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1175                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1176
1177         cleanup_quota_test
1178         resetquota -u $TSTUSR
1179         resetquota -u $TSTUSR2
1180 }
1181 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1182
1183 # quota reintegration (inode limits)
1184 test_7e() {
1185         [ "$MDSCOUNT" -lt "2" ] && skip "Required more MDTs" && return
1186
1187         local ilimit=$((1024 * 2)) # 2k inodes
1188         local TESTFILE=$DIR/${tdir}-1/$tfile
1189
1190         setup_quota_test
1191         trap cleanup_quota_test EXIT
1192
1193         # make sure the system is clean
1194         local USED=$(getquota -u $TSTUSR global curinodes)
1195         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1196
1197         # make sure no granted quota on mdt1
1198         set_mdt_qtype "ug" || error "enable mdt quota failed"
1199         resetquota -u $TSTUSR
1200         set_mdt_qtype "none" || error "disable mdt quota failed"
1201
1202         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1203         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1204         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1205                 "$TSTUSR isn't 0."
1206
1207         echo "Stop mds${MDSCOUNT}..."
1208         stop mds${MDSCOUNT}
1209
1210         echo "Enable quota & set quota limit for $TSTUSR"
1211         set_mdt_qtype "ug" || error "enable mdt quota failed"
1212         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit  $DIR ||
1213                 error "set quota failed"
1214
1215         echo "Start mds${MDSCOUNT}..."
1216         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1217         quota_init
1218
1219         wait_mdt_reint "ug" || error "reintegration failed"
1220
1221         echo "create remote dir"
1222         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1223                 error "create remote dir failed"
1224         chmod 0777 $DIR/${tdir}-1
1225
1226         # hardlimit should have been fetched by slave during global
1227         # reintegration, create will exceed quota
1228         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1229                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1230
1231         $RUNAS unlinkmany $TESTFILE $ilimit || "unlink files failed"
1232         wait_delete_completed
1233         sync_all_data || true
1234
1235         echo "Stop mds${MDSCOUNT}..."
1236         stop mds${MDSCOUNT}
1237
1238         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1239                 error "clear quota failed"
1240
1241         echo "Start mds${MDSCOUNT}..."
1242         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1243         quota_init
1244
1245         wait_mdt_reint "ug" || error "reintegration failed"
1246
1247         # hardlimit should be cleared on slave during reintegration
1248         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1249                 quota_error -u $TSTUSR "create failed, expect success"
1250
1251         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || "unlink failed"
1252         rmdir $DIR/${tdir}-1 || "unlink remote dir failed"
1253
1254         cleanup_quota_test
1255         resetquota -u $TSTUSR
1256 }
1257 run_test 7e "Quota reintegration (inode limits)"
1258
1259 # run dbench with quota enabled
1260 test_8() {
1261         local BLK_LIMIT="100g" #100G
1262         local FILE_LIMIT=1000000
1263
1264         setup_quota_test
1265         trap cleanup_quota_test EXIT
1266
1267         set_mdt_qtype "ug" || error "enable mdt quota failed"
1268         set_ost_qtype "ug" || error "enable ost quota failed"
1269
1270         echo "Set enough high limit for user: $TSTUSR"
1271         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1272                 error "set user quota failed"
1273         echo "Set enough high limit for group: $TSTUSR"
1274         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1275                 error "set group quota failed"
1276
1277         local duration=""
1278         [ "$SLOW" = "no" ] && duration=" -t 120"
1279         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1280                 quota_error a $TSTUSR "dbench failed!"
1281
1282         cleanup_quota_test
1283         resetquota -u $TSTUSR
1284         resetquota -g $TSTUSR
1285 }
1286 run_test 8 "Run dbench with quota enabled"
1287
1288 # this check is just for test_9
1289 OST0_MIN=4900000 #4.67G
1290
1291 check_whether_skip () {
1292         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1293         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1294         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1295                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1296                 return 0
1297         else
1298                 return 1
1299         fi
1300 }
1301
1302 # run for fixing bug10707, it needs a big room. test for 64bit
1303 test_9() {
1304         local filesize=$((1024 * 9 / 2)) # 4.5G
1305
1306         check_whether_skip && return 0
1307
1308         setup_quota_test
1309         trap cleanup_quota_test EXIT
1310
1311         set_ost_qtype "ug" || error "enable ost quota failed"
1312
1313         local TESTFILE="$DIR/$tdir/$tfile-0"
1314         local BLK_LIMIT=100G #100G
1315         local FILE_LIMIT=1000000
1316
1317         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1318
1319         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1320                 "for user: $TSTUSR"
1321         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1322                 error "set user quota failed"
1323
1324         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1325                 "for group: $TSTUSR"
1326         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1327                 error "set group quota failed"
1328
1329         quota_show_check a u $TSTUSR
1330         quota_show_check a g $TSTUSR
1331
1332         echo "Create test file"
1333         $LFS setstripe $TESTFILE -c 1 -i 0
1334         chown $TSTUSR.$TSTUSR $TESTFILE
1335
1336         log "Write the big file of 4.5G ..."
1337         $RUNAS $DD of=$TESTFILE count=$filesize ||
1338                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1339
1340         $SHOW_QUOTA_USER
1341         $SHOW_QUOTA_GROUP
1342
1343         cleanup_quota_test
1344         resetquota -u $TSTUSR
1345         resetquota -g $TSTUSR
1346
1347         $SHOW_QUOTA_USER
1348         $SHOW_QUOTA_GROUP
1349 }
1350 run_test 9 "Block limit larger than 4GB (b10707)"
1351
1352 test_10() {
1353         local TESTFILE=$DIR/$tdir/$tfile
1354
1355         setup_quota_test
1356         trap cleanup_quota_test EXIT
1357
1358         # set limit to root user should fail
1359         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1360                 error "set limit for root user successfully, expect failure"
1361         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1362                 error "set limit for root group successfully, expect failure"
1363
1364         # root user can overrun quota
1365         set_ost_qtype "ug" || "enable ost quota failed"
1366
1367         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1368                 error "set quota failed"
1369         quota_show_check b u $TSTUSR
1370
1371         $LFS setstripe $TESTFILE -c 1
1372         chown $TSTUSR.$TSTUSR $TESTFILE
1373
1374         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1375                 error "write failure, expect success"
1376
1377         cleanup_quota_test
1378         resetquota -u $TSTUSR
1379 }
1380 run_test 10 "Test quota for root user"
1381
1382 test_11() {
1383         local TESTFILE=$DIR/$tdir/$tfile
1384         setup_quota_test
1385         trap cleanup_quota_test EXIT
1386
1387         set_mdt_qtype "ug" || "enable mdt quota failed"
1388         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1389                 error "set quota failed"
1390
1391         touch "$TESTFILE"-0
1392         touch "$TESTFILE"-1
1393
1394         chown $TSTUSR.$TSTUSR "$TESTFILE"-0
1395         chown $TSTUSR.$TSTUSR "$TESTFILE"-1
1396
1397         $SHOW_QUOTA_USER
1398         local USED=$(getquota -u $TSTUSR global curinodes)
1399         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1400
1401         cleanup_quota_test
1402         resetquota -u $TSTUSR
1403 }
1404 run_test 11 "Chown/chgrp ignores quota"
1405
1406 test_12a() {
1407         [ "$OSTCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1408
1409         local blimit=22 # 22M
1410         local blk_cnt=$((blimit - 5))
1411         local TESTFILE0="$DIR/$tdir/$tfile"-0
1412         local TESTFILE1="$DIR/$tdir/$tfile"-1
1413
1414         setup_quota_test
1415         trap cleanup_quota_test EXIT
1416
1417         set_ost_qtype "u" || "enable ost quota failed"
1418         quota_show_check b u $TSTUSR
1419
1420         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1421                 error "set quota failed"
1422
1423         $LFS setstripe $TESTFILE0 -c 1 -i 0
1424         $LFS setstripe $TESTFILE1 -c 1 -i 1
1425         chown $TSTUSR.$TSTUSR $TESTFILE0
1426         chown $TSTUSR.$TSTUSR $TESTFILE1
1427
1428         echo "Write to ost0..."
1429         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1430                 quota_error a $TSTUSR "dd failed"
1431
1432         echo "Write to ost1..."
1433         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1434                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1435
1436         echo "Free space from ost0..."
1437         rm -f $TESTFILE0
1438         wait_delete_completed
1439         sync_all_data || true
1440
1441         echo "Write to ost1 after space freed from ost0..."
1442         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1443                 quota_error a $TSTUSR "rebalancing failed"
1444
1445         cleanup_quota_test
1446         resetquota -u $TSTUSR
1447 }
1448 run_test 12a "Block quota rebalancing"
1449
1450 test_12b() {
1451         [ "$MDSCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1452
1453         local ilimit=$((1024 * 2)) # 2k inodes
1454         local TESTFILE0=$DIR/$tdir/$tfile
1455         local TESTFILE1=$DIR/${tdir}-1/$tfile
1456
1457         setup_quota_test
1458         trap cleanup_quota_test EXIT
1459
1460         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1461         chmod 0777 $DIR/${tdir}-1
1462
1463         set_mdt_qtype "u" || "enable mdt quota failed"
1464         quota_show_check f u $TSTUSR
1465
1466         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1467                 error "set quota failed"
1468
1469         echo "Create $ilimit files on mdt0..."
1470         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1471                 quota_error u $TSTUSR "create failed, but expect success"
1472
1473         echo "Create files on mdt1..."
1474         $RUNAS createmany -m $TESTFILE1 1 &&
1475                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1476
1477         echo "Free space from mdt0..."
1478         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1479         wait_delete_completed
1480         sync_all_data || true
1481
1482         echo "Create files on mdt1 after space freed from mdt0..."
1483         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1484                 quota_error a $TSTUSR "rebalancing failed"
1485
1486         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1487                 error "unlink mdt1 files failed"
1488         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1489
1490         cleanup_quota_test
1491         resetquota -u $TSTUSR
1492 }
1493 run_test 12b "Inode quota rebalancing"
1494
1495 test_13(){
1496         local TESTFILE=$DIR/$tdir/$tfile
1497         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1498         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1499
1500         setup_quota_test
1501         trap cleanup_quota_test EXIT
1502
1503         set_ost_qtype "u" || "enable ost quota failed"
1504         quota_show_check b u $TSTUSR
1505
1506         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1507                 error "set quota failed"
1508         $LFS setstripe $TESTFILE -c 1 -i 0
1509         chown $TSTUSR.$TSTUSR $TESTFILE
1510
1511         # clear the locks in cache first
1512         do_facet ost1 $LCTL set_param -n $procf=clear
1513         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1514         [ $nlock -eq 0 ] || error "$nlock cached locks"
1515
1516         # write to acquire the per-ID lock
1517         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1518                 quota_error a $TSTUSR "dd failed"
1519
1520         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1521         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1522
1523         # clear quota doesn't trigger per-ID lock cancellation
1524         resetquota -u $TSTUSR
1525         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1526         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1527
1528         # clear the per-ID lock
1529         do_facet ost1 $LCTL set_param -n $procf=clear
1530         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1531         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1532
1533         # spare quota should be released
1534         local OSTUUID=$(ostuuid_from_index 0)
1535         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1536         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1537         [ $limit -le $space ] ||
1538                 error "spare quota isn't released, limit:$limit, space:$space"
1539
1540         cleanup_quota_test
1541 }
1542 run_test 13 "Cancel per-ID lock in the LRU list"
1543
1544 test_15(){
1545         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1546
1547         wait_delete_completed
1548         sync_all_data || true
1549
1550         # test for user
1551         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1552                 error "set user quota failed"
1553         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1554         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1555                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1556         resetquota -u $TSTUSR
1557
1558         # test for group
1559         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1560                 error "set group quota failed"
1561         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1562         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1563                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1564         resetquota -g $TSTUSR
1565 }
1566 run_test 15 "Set over 4T block quota"
1567
1568 test_17sub() {
1569         local err_code=$1
1570         local BLKS=1    # 1M less than limit
1571         local TESTFILE=$DIR/$tdir/$tfile
1572
1573         setup_quota_test
1574         trap cleanup_quota_test EXIT
1575
1576         # make sure the system is clean
1577         local USED=$(getquota -u $TSTUSR global curspace)
1578         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1579
1580         set_ost_qtype "ug" || error "enable ost quota failed"
1581         # make sure no granted quota on ost
1582         resetquota -u $TSTUSR
1583         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1584                 error "set quota failed"
1585
1586         quota_show_check b u $TSTUSR
1587
1588         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1589         lustre_fail mds 0xa04 $err_code
1590
1591         # write in background
1592         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1593         local DDPID=$!
1594
1595         sleep 2
1596         # write should be blocked and never finished
1597         if ! ps -p $DDPID  > /dev/null 2>&1; then
1598                 lustre_fail mds 0 0
1599                 quota_error u $TSTUSR "write finished incorrectly!"
1600         fi
1601
1602         lustre_fail mds 0 0
1603
1604         local count=0
1605         local timeout=30
1606         while [ true ]; do
1607                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1608                 count=$((count+1))
1609                 if [ $count -gt $timeout ]; then
1610                         quota_error u $TSTUSR "dd is not finished!"
1611                 fi
1612                 sleep 1
1613         done
1614
1615         sync; sync_all_data || true
1616
1617         USED=$(getquota -u $TSTUSR global curspace)
1618         [ $USED -ge $(($BLKS * 1024)) ] || quota_error u $TSTUSR \
1619                 "Used space(${USED}K) is less than ${BLKS}M"
1620
1621         cleanup_quota_test
1622         resetquota -u $TSTUSR
1623 }
1624
1625 # DQACQ return recoverable error
1626 test_17() {
1627         echo "DQACQ return -ENOLCK"
1628         #define ENOLCK  37
1629         test_17sub 37 || error "Handle -ENOLCK failed"
1630
1631         echo "DQACQ return -EAGAIN"
1632         #define EAGAIN  11
1633         test_17sub 11 || error "Handle -EAGAIN failed"
1634
1635         echo "DQACQ return -ETIMEDOUT"
1636         #define ETIMEDOUT 110
1637         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1638
1639         echo "DQACQ return -ENOTCONN"
1640         #define ENOTCONN 107
1641         test_17sub 107 || error "Handle -ENOTCONN failed"
1642 }
1643
1644 run_test 17 "DQACQ return recoverable error"
1645
1646 test_18_sub () {
1647         local io_type=$1
1648         local blimit="200m" # 200M
1649         local TESTFILE="$DIR/$tdir/$tfile"
1650
1651         setup_quota_test
1652         trap cleanup_quota_test EXIT
1653
1654         set_ost_qtype "u" || error "enable ost quota failed"
1655         log "User quota (limit: $blimit)"
1656         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1657                 error "set quota failed"
1658         quota_show_check b u $TSTUSR
1659
1660         $LFS setstripe $TESTFILE -i 0 -c 1
1661         chown $TSTUSR.$TSTUSR $TESTFILE
1662
1663         local timeout=$(sysctl -n lustre.timeout)
1664
1665         if [ $io_type = "directio" ]; then
1666                 log "Write 100M (directio) ..."
1667                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1668         else
1669                 log "Write 100M (buffered) ..."
1670                 $RUNAS $DD of=$TESTFILE count=100 &
1671         fi
1672         local DDPID=$!
1673
1674         replay_barrier $SINGLEMDS
1675         log "Fail mds for $((2 * timeout)) seconds"
1676         fail $SINGLEMDS $((2 * timeout))
1677
1678         local count=0
1679         if at_is_enabled; then
1680                 timeout=$(at_max_get mds)
1681         else
1682                 timeout=$(lctl get_param -n timeout)
1683         fi
1684
1685         while [ true ]; do
1686                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1687                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1688                         log "it took $count second"
1689                 fi
1690                 sleep 1
1691         done
1692
1693         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1694         sync
1695         cancel_lru_locks mdc
1696         cancel_lru_locks osc
1697         $SHOW_QUOTA_USER
1698
1699         local testfile_size=$(stat -c %s $TESTFILE)
1700         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1701                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1702                         "got ${testfile_size}. Verifying file failed!"
1703         fi
1704         cleanup_quota_test
1705         resetquota -u $TSTUSR
1706 }
1707
1708 # test when mds does failover, the ost still could work well
1709 # this test shouldn't trigger watchdog b=14840
1710 test_18() {
1711         # Clear dmesg so watchdog is not triggered by previous
1712         # test output
1713         do_facet ost1 dmesg -c > /dev/null
1714
1715         test_18_sub normal
1716         test_18_sub directio
1717
1718         # check if watchdog is triggered
1719         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1720         local watchdog=$(awk '/Service thread pid/ && /was inactive/ \
1721                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1722         [ -z "$watchdog" ] || error "$watchdog"
1723         rm -f $TMP/lustre-log-${TESTNAME}.log
1724 }
1725 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1726
1727 test_19() {
1728         local blimit=5 # 5M
1729         local TESTFILE=$DIR/$tdir/$tfile
1730
1731         setup_quota_test
1732         trap cleanup_quota_test EXIT
1733
1734         set_ost_qtype "ug" || error "enable ost quota failed"
1735
1736         # bind file to a single OST
1737         $LFS setstripe -c 1 $TESTFILE
1738         chown $TSTUSR.$TSTUSR $TESTFILE
1739
1740         echo "Set user quota (limit: "$blimit"M)"
1741         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1742                 error "set user quota failed"
1743         quota_show_check b u $TSTUSR
1744         echo "Update quota limits"
1745         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1746                 error "set group quota failed"
1747         quota_show_check b u $TSTUSR
1748
1749         # first wirte might be cached
1750         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
1751         cancel_lru_locks osc
1752         $SHOW_QUOTA_USER
1753         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
1754                 quota_error u $TSTUSR "Write success, expect failure"
1755         $SHOW_QUOTA_USER
1756
1757         cleanup_quota_test
1758         resetquota -u $TSTUSR
1759 }
1760 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
1761
1762 test_20() { # b15754
1763         local LSTR=(2g 1t 4k 3m) # limits strings
1764         # limits values
1765         local LVAL=($[2*1024*1024] $[1*1024*1024*1024] $[4*1024] $[3*1024*1024])
1766
1767         resetquota -u $TSTUSR
1768
1769         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1770                 $MOUNT || error "could not set quota limits"
1771         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1772                                 --inode-softlimit ${LSTR[2]} \
1773                                 --inode-hardlimit ${LSTR[3]} \
1774                                 $MOUNT || error "could not set quota limits"
1775
1776         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
1777                 error "bsoftlimit was not set properly"
1778         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
1779                 error "bhardlimit was not set properly"
1780         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
1781                 error "isoftlimit was not set properly"
1782         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
1783                 error "ihardlimit was not set properly"
1784
1785         resetquota -u $TSTUSR
1786 }
1787 run_test 20 "Test if setquota specifiers work properly (b15754)"
1788
1789 test_21_sub() {
1790         local testfile=$1
1791         local blk_number=$2
1792         local seconds=$3
1793
1794         local time=$(($(date +%s) + seconds))
1795         while [ $(date +%s) -lt $time ]; do
1796                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
1797         done
1798 }
1799
1800 # run for fixing bug16053, setquota shouldn't fail when writing and
1801 # deleting are happening
1802 test_21() {
1803         local TESTFILE="$DIR/$tdir/$tfile"
1804         local BLIMIT=10 # 10G
1805         local ILIMIT=1000000
1806
1807         setup_quota_test
1808         trap cleanup_quota_test EXIT
1809
1810         set_ost_qtype "ug" || error "Enable ost quota failed"
1811
1812         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
1813         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
1814                 error "set user quota failed"
1815         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
1816         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
1817                 error "set group quota failed"
1818
1819         # repeat writing on a 1M file
1820         test_21_sub ${TESTFILE}_1 1 30 &
1821         local DDPID1=$!
1822         # repeat writing on a 128M file
1823         test_21_sub ${TESTFILE}_2 128 30 &
1824         local DDPID2=$!
1825
1826         local time=$(($(date +%s) + 30))
1827         local i=1
1828         while [ $(date +%s) -lt $time ]; do
1829                 log "Set quota for $i times"
1830                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1831                         -I $((ILIMIT + i)) $MOUNT ||
1832                                 error "Set user quota failed"
1833                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1834                         -I $((ILIMIT + i)) $MOUNT ||
1835                                 error "Set group quota failed"
1836                 i=$((i+1))
1837                 sleep 1
1838         done
1839
1840         local count=0
1841         while [ true ]; do
1842                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1843                 count=$((count+1))
1844                 if [ $count -gt 60 ]; then
1845                         quota_error a $TSTUSR "dd should be finished!"
1846                 fi
1847                 sleep 1
1848         done
1849         echo "(dd_pid=$DDPID1, time=$count)successful"
1850
1851         count=0
1852         while [ true ]; do
1853                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1854                 count=$((count+1))
1855                 if [ $count -gt 60 ]; then
1856                         quota_error a $TSTUSR "dd should be finished!"
1857                 fi
1858                 sleep 1
1859         done
1860         echo "(dd_pid=$DDPID2, time=$count)successful"
1861
1862         cleanup_quota_test
1863         resetquota -u $TSTUSR
1864         resetquota -g $TSTUSR
1865 }
1866 run_test 21 "Setquota while writing & deleting (b16053)"
1867
1868 # enable/disable quota enforcement permanently
1869 test_22() {
1870         echo "Set both mdt & ost quota type as ug"
1871         set_mdt_qtype "ug" || error "enable mdt quota failed"
1872         set_ost_qtype "ug" || error "enable ost quota failed"
1873
1874         echo "Restart..."
1875         stopall
1876         mount
1877         setupall
1878
1879         echo "Verify if quota is enabled"
1880         local qtype=$(mdt_quota_type)
1881         [ $qtype != "ug" ] && error "mdt quota setting is lost"
1882         qtype=$(ost_quota_type)
1883         [ $qtype != "ug" ] && error "ost quota setting is lost"
1884
1885         echo "Set both mdt & ost quota type as none"
1886         set_mdt_qtype "none" || error "disable mdt quota failed"
1887         set_ost_qtype "none" || error "disable ost quota failed"
1888
1889         echo "Restart..."
1890         stopall
1891         mount
1892         setupall
1893         quota_init
1894
1895         echo "Verify if quota is disabled"
1896         qtype=$(mdt_quota_type)
1897         [ $qtype != "none" ] && error "mdt quota setting is lost"
1898         qtype=$(ost_quota_type)
1899         [ $qtype != "none" ] && error "ost quota setting is lost"
1900
1901         return 0
1902 }
1903 run_test 22 "enable/disable quota by 'lctl conf_param'"
1904
1905 test_23_sub() {
1906         local TESTFILE="$DIR/$tdir/$tfile"
1907         local LIMIT=$1
1908
1909         setup_quota_test
1910         trap cleanup_quota_test EXIT
1911
1912         set_ost_qtype "ug" || error "Enable ost quota failed"
1913
1914         # test for user
1915         log "User quota (limit: $LIMIT MB)"
1916         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
1917                 error "set quota failed"
1918         quota_show_check b u $TSTUSR
1919
1920         $LFS setstripe $TESTFILE -c 1 -i 0
1921         chown $TSTUSR.$TSTUSR $TESTFILE
1922
1923         log "Step1: trigger EDQUOT with O_DIRECT"
1924         log "Write half of file"
1925         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
1926                 quota_error u $TSTUSR "(1) Write failure, expect success." \
1927                         "limit=$LIMIT"
1928         log "Write out of block quota ..."
1929         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
1930                 oflag=direct conv=notrunc &&
1931                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
1932                         "limit=$LIMIT"
1933         log "Step1: done"
1934
1935         log "Step2: rewrite should succeed"
1936         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
1937                 quota_error u $TSTUSR "(3) Write failure, expect success." \
1938                         "limit=$LIMIT"
1939         log "Step2: done"
1940
1941         cleanup_quota_test
1942
1943         local OST0_UUID=$(ostuuid_from_index 0)
1944         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
1945         [ $OST0_QUOTA_USED -ne 0 ] &&
1946                 ($SHOW_QUOTA_USER; \
1947                 quota_error u $TSTUSR "quota isn't released")
1948         $SHOW_QUOTA_USER
1949         resetquota -u $TSTUSR
1950 }
1951
1952 test_23() {
1953         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
1954         check_whether_skip && return 0
1955         log "run for 4MB test file"
1956         test_23_sub 4
1957
1958         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
1959         check_whether_skip && return 0
1960         log "run for 40MB test file"
1961         test_23_sub 40
1962 }
1963 run_test 23 "Quota should be honored with directIO (b16125)"
1964
1965 test_24() {
1966         local blimit=5 # 5M
1967         local TESTFILE="$DIR/$tdir/$tfile"
1968
1969         setup_quota_test
1970         trap cleanup_quota_test EXIT
1971
1972         set_ost_qtype "ug" || error "enable ost quota failed"
1973
1974         # bind file to a single OST
1975         $LFS setstripe -c 1 $TESTFILE
1976         chown $TSTUSR.$TSTUSR $TESTFILE
1977
1978         echo "Set user quota (limit: "$blimit"M)"
1979         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1980                 error "set quota failed"
1981
1982         # overrun quota by root user
1983         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
1984                 error "write failure, expect success"
1985         cancel_lru_locks osc
1986         sync_all_data || true
1987
1988         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1989
1990         cleanup_quota_test
1991         resetquota -u $TSTUSR
1992 }
1993 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
1994
1995 test_27a() { # b19612
1996         $LFS quota $TSTUSR $DIR &&
1997                 error "lfs succeeded with no type, but should have failed"
1998         $LFS setquota $TSTUSR $DIR &&
1999                 error "lfs succeeded with no type, but should have failed"
2000         return 0
2001 }
2002 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2003
2004 test_27b() { # b20200
2005         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2006                 error "lfs setquota failed with uid argument"
2007         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2008                 error "lfs stequota failed with gid argument"
2009         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2010         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2011         resetquota -u $TSTUSR
2012         resetquota -g $TSTUSR
2013         return 0
2014 }
2015 run_test 27b "lfs quota/setquota should handle user/group ID (b20200)"
2016
2017 test_27c() {
2018         local limit
2019
2020         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2021                 error "lfs setquota failed"
2022
2023         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2024         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2025         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2026         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2027
2028         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2029                 error "lfs setquota for $TSTID failed"
2030
2031         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2032         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2033         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2034         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2035
2036         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2037                 error "total allocated inode/block limit not printed"
2038
2039         resetquota -u $TSTUSR
2040 }
2041 run_test 27c "lfs quota should support human-readable output"
2042
2043 test_30() {
2044         local output
2045         local LIMIT=4 # 4MB
2046         local TESTFILE="$DIR/$tdir/$tfile"
2047         local GRACE=10
2048
2049         setup_quota_test
2050         trap cleanup_quota_test EXIT
2051
2052         set_ost_qtype "u" || error "enable ost quota failed"
2053
2054         $LFS setstripe $TESTFILE -i 0 -c 1
2055         chown $TSTUSR.$TSTUSR $TESTFILE
2056
2057         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2058                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2059         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2060                 error "set quota failed"
2061         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2062         cancel_lru_locks osc
2063         sleep $GRACE
2064         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2065         # over-quota flag has not yet settled since we do not trigger async
2066         # events based on grace time period expiration
2067         $SHOW_QUOTA_USER
2068         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2069         cancel_lru_locks osc
2070         # now over-quota flag should be settled and further writes should fail
2071         $SHOW_QUOTA_USER
2072         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2073                 error "grace times were reset"
2074         # cleanup
2075         cleanup_quota_test
2076         resetquota -u $TSTUSR
2077         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2078                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2079 }
2080 run_test 30 "Hard limit updates should not reset grace times"
2081
2082 # basic usage tracking for user & group
2083 test_33() {
2084         local INODES=10 # 10 files
2085         local BLK_CNT=2 # of 2M each
2086         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2087
2088         setup_quota_test
2089         trap cleanup_quota_test EXIT
2090
2091         # make sure the system is clean
2092         local USED=$(getquota -u $TSTID global curspace)
2093         [ $USED -ne 0 ] &&
2094                 error "Used space ($USED) for user $TSTID isn't 0."
2095         USED=$(getquota -g $TSTID global curspace)
2096         [ $USED -ne 0 ] &&
2097                 error "Used space ($USED) for group $TSTID isn't 0."
2098
2099         echo "Write files..."
2100         for i in `seq 0 $INODES`; do
2101                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2102                         error "write failed"
2103                 echo "Iteration $i/$INODES completed"
2104         done
2105         cancel_lru_locks osc
2106         sync; sync_all_data || true
2107
2108         echo "Verify disk usage after write"
2109         USED=$(getquota -u $TSTID global curspace)
2110         [ $USED -lt $TOTAL_BLKS ] &&
2111                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2112         USED=$(getquota -g $TSTID global curspace)
2113         [ $USED -lt $TOTAL_BLKS ] &&
2114                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2115
2116         echo "Verify inode usage after write"
2117         USED=$(getquota -u $TSTID global curinodes)
2118         [ $USED -lt $INODES ] &&
2119                 error "Used inode for user $TSTID is $USED, expected $INODES"
2120         USED=$(getquota -g $TSTID global curinodes)
2121         [ $USED -lt $INODES ] &&
2122                 error "Used inode for group $TSTID is $USED, expected $INODES"
2123
2124         cleanup_quota_test
2125
2126         echo "Verify disk usage after delete"
2127         USED=$(getquota -u $TSTID global curspace)
2128         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2129         USED=$(getquota -u $TSTID global curinodes)
2130         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2131         USED=$(getquota -g $TSTID global curspace)
2132         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2133         USED=$(getquota -g $TSTID global curinodes)
2134         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2135 }
2136 run_test 33 "Basic usage tracking for user & group"
2137
2138 # usage transfer test for user & group
2139 test_34() {
2140         local BLK_CNT=2 # 2MB
2141
2142         setup_quota_test
2143         trap cleanup_quota_test EXIT
2144
2145         # make sure the system is clean
2146         local USED=$(getquota -u $TSTID global curspace)
2147         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2148         USED=$(getquota -g $TSTID global curspace)
2149         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2150
2151         local USED=$(getquota -u $TSTID2 global curspace)
2152         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2153
2154         echo "Write file..."
2155         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2156                 error "write failed"
2157         cancel_lru_locks osc
2158         sync; sync_all_data || true
2159
2160         echo "chown the file to user $TSTID"
2161         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2162
2163         echo "Wait for setattr on objects finished..."
2164         wait_delete_completed
2165
2166         BLK_CNT=$((BLK_CNT * 1024))
2167
2168         echo "Verify disk usage for user $TSTID"
2169         USED=$(getquota -u $TSTID global curspace)
2170         [ $USED -lt $BLK_CNT ] &&
2171                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2172         USED=$(getquota -u $TSTID global curinodes)
2173         [ $USED -ne 1 ] &&
2174                 error "Used inodes for user $TSTID is $USED, expected 1"
2175
2176         echo "chgrp the file to group $TSTID"
2177         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2178
2179         echo "Wait for setattr on objects finished..."
2180         wait_delete_completed
2181
2182         echo "Verify disk usage for group $TSTID"
2183         USED=$(getquota -g $TSTID global curspace)
2184         [ $USED -ge $BLK_CNT ] ||
2185                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2186         USED=$(getquota -g $TSTID global curinodes)
2187         [ $USED -eq 1 ] ||
2188                 error "Used inodes for group $TSTID is $USED, expected 1"
2189
2190         # chown won't change the ost object group. LU-4345 */
2191         echo "chown the file to user $TSTID2"
2192         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2193
2194         echo "Wait for setattr on objects finished..."
2195         wait_delete_completed
2196
2197         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2198         USED=$(getquota -u $TSTID2 global curspace)
2199         [ $USED -lt $BLK_CNT ] &&
2200                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2201         USED=$(getquota -u $TSTID global curspace)
2202         [ $USED -ne 0 ] &&
2203                 error "Used space for user $TSTID is $USED, expected 0"
2204         USED=$(getquota -g $TSTID global curspace)
2205         [ $USED -lt $BLK_CNT ] &&
2206                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2207
2208         cleanup_quota_test
2209 }
2210 run_test 34 "Usage transfer for user & group"
2211
2212 # usage is still accessible across restart
2213 test_35() {
2214         local BLK_CNT=2 # 2 MB
2215
2216         setup_quota_test
2217         trap cleanup_quota_test EXIT
2218
2219         echo "Write file..."
2220         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2221                 error "write failed"
2222         cancel_lru_locks osc
2223         sync; sync_all_data || true
2224
2225         echo "Save disk usage before restart"
2226         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2227         [ $ORIG_USR_SPACE -eq 0 ] &&
2228                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2229         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2230         [ $ORIG_USR_INODES -eq 0 ] &&
2231                 error "Used inodes for user $TSTID is 0, expected 1"
2232         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2233         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2234         [ $ORIG_GRP_SPACE -eq 0 ] &&
2235                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2236         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2237         [ $ORIG_GRP_INODES -eq 0 ] &&
2238                 error "Used inodes for group $TSTID is 0, expected 1"
2239         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2240
2241         log "Restart..."
2242         local ORIG_REFORMAT=$REFORMAT
2243         REFORMAT=""
2244         cleanup_and_setup_lustre
2245         REFORMAT=$ORIG_REFORMAT
2246         quota_init
2247
2248         echo "Verify disk usage after restart"
2249         local USED=$(getquota -u $TSTID global curspace)
2250         [ $USED -eq $ORIG_USR_SPACE ] ||
2251                 error "Used space for user $TSTID changed from " \
2252                         "$ORIG_USR_SPACE to $USED"
2253         USED=$(getquota -u $TSTID global curinodes)
2254         [ $USED -eq $ORIG_USR_INODES ] ||
2255                 error "Used inodes for user $TSTID changed from " \
2256                         "$ORIG_USR_INODES to $USED"
2257         USED=$(getquota -g $TSTID global curspace)
2258         [ $USED -eq $ORIG_GRP_SPACE ] ||
2259                 error "Used space for group $TSTID changed from " \
2260                         "$ORIG_GRP_SPACE to $USED"
2261         USED=$(getquota -g $TSTID global curinodes)
2262         [ $USED -eq $ORIG_GRP_INODES ] ||
2263                 error "Used inodes for group $TSTID changed from " \
2264                         "$ORIG_GRP_INODES to $USED"
2265
2266         # check if the vfs_dq_init() is called before writing
2267         echo "Append to the same file..."
2268         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2269                 error "write failed"
2270         cancel_lru_locks osc
2271         sync; sync_all_data || true
2272
2273         echo "Verify space usage is increased"
2274         USED=$(getquota -u $TSTID global curspace)
2275         [ $USED -gt $ORIG_USR_SPACE ] ||
2276                 error "Used space for user $TSTID isn't increased" \
2277                         "orig:$ORIG_USR_SPACE, now:$USED"
2278         USED=$(getquota -g $TSTID global curspace)
2279         [ $USED -gt $ORIG_GRP_SPACE ] ||
2280                 error "Used space for group $TSTID isn't increased" \
2281                         "orig:$ORIG_GRP_SPACE, now:$USED"
2282
2283         cleanup_quota_test
2284 }
2285 run_test 35 "Usage is still accessible across reboot"
2286
2287 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2288 # LU-5006
2289 test_37() {
2290         setup_quota_test
2291         trap cleanup_quota_test EXIT
2292
2293         # make sure the system is clean
2294         local USED=$(getquota -u $TSTID global curspace)
2295         [ $USED -ne 0 ] &&
2296                 error "Used space ($USED) for user $TSTID isn't 0."
2297
2298         # create file with MDS_OPEN_DELAY_CREATE flag
2299         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
2300                 error "Create file failed"
2301         # write to file
2302         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2303                 oflag=sync || error "Write file failed"
2304         # chown to the file
2305         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2306
2307         # wait for setattr on objects finished..."
2308         wait_delete_completed
2309
2310         USED=$(getquota -u $TSTID global curspace)
2311         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2312
2313         cleanup_quota_test
2314 }
2315 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2316
2317 quota_fini()
2318 {
2319         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2320 }
2321 quota_fini
2322
2323 cd $ORIG_PWD
2324 complete $SECONDS
2325 check_and_cleanup_lustre
2326 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2327 exit_status