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