Whamcloud - gitweb
LU-6210 tests: Change positional struct initializers to C99
[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 LU-2059
50         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 3       4a      6       7d"
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         local ilimit=$((1024 * 2)) # 2k inodes
1183         local TESTFILE=$DIR/${tdir}-1/$tfile
1184
1185         setup_quota_test || error "setup quota failed with $?"
1186         trap cleanup_quota_test EXIT
1187
1188         # make sure the system is clean
1189         local USED=$(getquota -u $TSTUSR global curinodes)
1190         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1191
1192         # make sure no granted quota on mdt1
1193         set_mdt_qtype "ug" || error "enable mdt quota failed"
1194         resetquota -u $TSTUSR
1195         set_mdt_qtype "none" || error "disable mdt quota failed"
1196
1197         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1198         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1199         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1200                 "$TSTUSR isn't 0."
1201
1202         echo "Stop mds${MDSCOUNT}..."
1203         stop mds${MDSCOUNT}
1204
1205         echo "Enable quota & set quota limit for $TSTUSR"
1206         set_mdt_qtype "ug" || error "enable mdt quota failed"
1207         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1208                 error "set quota failed"
1209
1210         echo "Start mds${MDSCOUNT}..."
1211         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1212         quota_init
1213
1214         wait_mdt_reint "ug" || error "reintegration failed"
1215
1216         echo "create remote dir"
1217         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1218                 error "create remote dir failed"
1219         chmod 0777 $DIR/${tdir}-1
1220
1221         # hardlimit should have been fetched by slave during global
1222         # reintegration, create will exceed quota
1223         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1224                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1225
1226         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1227         wait_delete_completed
1228         sync_all_data || true
1229
1230         echo "Stop mds${MDSCOUNT}..."
1231         stop mds${MDSCOUNT}
1232
1233         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1234                 error "clear quota failed"
1235
1236         echo "Start mds${MDSCOUNT}..."
1237         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1238         quota_init
1239
1240         wait_mdt_reint "ug" || error "reintegration failed"
1241
1242         # hardlimit should be cleared on slave during reintegration
1243         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1244                 quota_error -u $TSTUSR "create failed, expect success"
1245
1246         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1247         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1248
1249         cleanup_quota_test
1250         resetquota -u $TSTUSR
1251 }
1252 run_test 7e "Quota reintegration (inode limits)"
1253
1254 # run dbench with quota enabled
1255 test_8() {
1256         local BLK_LIMIT="100g" #100G
1257         local FILE_LIMIT=1000000
1258
1259         setup_quota_test || error "setup quota failed with $?"
1260         trap cleanup_quota_test EXIT
1261
1262         set_mdt_qtype "ug" || error "enable mdt quota failed"
1263         set_ost_qtype "ug" || error "enable ost quota failed"
1264
1265         echo "Set enough high limit for user: $TSTUSR"
1266         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1267                 error "set user quota failed"
1268         echo "Set enough high limit for group: $TSTUSR"
1269         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1270                 error "set group quota failed"
1271
1272         local duration=""
1273         [ "$SLOW" = "no" ] && duration=" -t 120"
1274         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1275                 quota_error a $TSTUSR "dbench failed!"
1276
1277         cleanup_quota_test
1278         resetquota -u $TSTUSR
1279         resetquota -g $TSTUSR
1280 }
1281 run_test 8 "Run dbench with quota enabled"
1282
1283 # this check is just for test_9
1284 OST0_MIN=4900000 #4.67G
1285
1286 check_whether_skip () {
1287         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1288         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1289         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1290                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1291                 return 0
1292         else
1293                 return 1
1294         fi
1295 }
1296
1297 # run for fixing bug10707, it needs a big room. test for 64bit
1298 test_9() {
1299         local filesize=$((1024 * 9 / 2)) # 4.5G
1300
1301         check_whether_skip && return 0
1302
1303         setup_quota_test || error "setup quota failed with $?"
1304         trap cleanup_quota_test EXIT
1305
1306         set_ost_qtype "ug" || error "enable ost quota failed"
1307
1308         local TESTFILE="$DIR/$tdir/$tfile-0"
1309         local BLK_LIMIT=100G #100G
1310         local FILE_LIMIT=1000000
1311
1312         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1313
1314         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1315                 "for user: $TSTUSR"
1316         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1317                 error "set user quota failed"
1318
1319         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1320                 "for group: $TSTUSR"
1321         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1322                 error "set group quota failed"
1323
1324         quota_show_check a u $TSTUSR
1325         quota_show_check a g $TSTUSR
1326
1327         echo "Create test file"
1328         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1329         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1330
1331         log "Write the big file of 4.5G ..."
1332         $RUNAS $DD of=$TESTFILE count=$filesize ||
1333                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1334
1335         $SHOW_QUOTA_USER
1336         $SHOW_QUOTA_GROUP
1337
1338         cleanup_quota_test
1339         resetquota -u $TSTUSR
1340         resetquota -g $TSTUSR
1341
1342         $SHOW_QUOTA_USER
1343         $SHOW_QUOTA_GROUP
1344 }
1345 run_test 9 "Block limit larger than 4GB (b10707)"
1346
1347 test_10() {
1348         local TESTFILE=$DIR/$tdir/$tfile
1349
1350         setup_quota_test || error "setup quota failed with $?"
1351         trap cleanup_quota_test EXIT
1352
1353         # set limit to root user should fail
1354         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1355                 error "set limit for root user successfully, expect failure"
1356         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1357                 error "set limit for root group successfully, expect failure"
1358
1359         # root user can overrun quota
1360         set_ost_qtype "ug" || error "enable ost quota failed"
1361
1362         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1363                 error "set quota failed"
1364         quota_show_check b u $TSTUSR
1365
1366         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1367         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1368
1369         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1370                 error "write failure, expect success"
1371
1372         cleanup_quota_test
1373         resetquota -u $TSTUSR
1374 }
1375 run_test 10 "Test quota for root user"
1376
1377 test_11() {
1378         local TESTFILE=$DIR/$tdir/$tfile
1379         setup_quota_test || error "setup quota failed with $?"
1380         trap cleanup_quota_test EXIT
1381
1382         set_mdt_qtype "ug" || error "enable mdt quota failed"
1383         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1384                 error "set quota failed"
1385
1386         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1387         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1388
1389         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1390         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1391
1392         $SHOW_QUOTA_USER
1393         local USED=$(getquota -u $TSTUSR global curinodes)
1394         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1395
1396         cleanup_quota_test
1397         resetquota -u $TSTUSR
1398 }
1399 run_test 11 "Chown/chgrp ignores quota"
1400
1401 test_12a() {
1402         [ "$OSTCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1403
1404         local blimit=22 # 22M
1405         local blk_cnt=$((blimit - 5))
1406         local TESTFILE0="$DIR/$tdir/$tfile"-0
1407         local TESTFILE1="$DIR/$tdir/$tfile"-1
1408
1409         setup_quota_test || error "setup quota failed with $?"
1410         trap cleanup_quota_test EXIT
1411
1412         set_ost_qtype "u" || error "enable ost quota failed"
1413         quota_show_check b u $TSTUSR
1414
1415         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1416                 error "set quota failed"
1417
1418         $SETSTRIPE $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1419         $SETSTRIPE $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1420         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1421         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1422
1423         echo "Write to ost0..."
1424         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1425                 quota_error a $TSTUSR "dd failed"
1426
1427         echo "Write to ost1..."
1428         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1429                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1430
1431         echo "Free space from ost0..."
1432         rm -f $TESTFILE0
1433         wait_delete_completed
1434         sync_all_data || true
1435
1436         echo "Write to ost1 after space freed from ost0..."
1437         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1438                 quota_error a $TSTUSR "rebalancing failed"
1439
1440         cleanup_quota_test
1441         resetquota -u $TSTUSR
1442 }
1443 run_test 12a "Block quota rebalancing"
1444
1445 test_12b() {
1446         [ "$MDSCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1447
1448         local ilimit=$((1024 * 2)) # 2k inodes
1449         local TESTFILE0=$DIR/$tdir/$tfile
1450         local TESTFILE1=$DIR/${tdir}-1/$tfile
1451
1452         setup_quota_test || error "setup quota failed with $?"
1453         trap cleanup_quota_test EXIT
1454
1455         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1456         chmod 0777 $DIR/${tdir}-1
1457
1458         set_mdt_qtype "u" || error "enable mdt quota failed"
1459         quota_show_check f u $TSTUSR
1460
1461         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1462                 error "set quota failed"
1463
1464         echo "Create $ilimit files on mdt0..."
1465         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1466                 quota_error u $TSTUSR "create failed, but expect success"
1467
1468         echo "Create files on mdt1..."
1469         $RUNAS createmany -m $TESTFILE1 1 &&
1470                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1471
1472         echo "Free space from mdt0..."
1473         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1474         wait_delete_completed
1475         sync_all_data || true
1476
1477         echo "Create files on mdt1 after space freed from mdt0..."
1478         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1479                 quota_error a $TSTUSR "rebalancing failed"
1480
1481         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1482                 error "unlink mdt1 files failed"
1483         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1484
1485         cleanup_quota_test
1486         resetquota -u $TSTUSR
1487 }
1488 run_test 12b "Inode quota rebalancing"
1489
1490 test_13(){
1491         local TESTFILE=$DIR/$tdir/$tfile
1492         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1493         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1494
1495         setup_quota_test || error "setup quota failed with $?"
1496         trap cleanup_quota_test EXIT
1497
1498         set_ost_qtype "u" || error "enable ost quota failed"
1499         quota_show_check b u $TSTUSR
1500
1501         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1502                 error "set quota failed"
1503         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1504         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1505
1506         # clear the locks in cache first
1507         do_facet ost1 $LCTL set_param -n $procf=clear
1508         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1509         [ $nlock -eq 0 ] || error "$nlock cached locks"
1510
1511         # write to acquire the per-ID lock
1512         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1513                 quota_error a $TSTUSR "dd failed"
1514
1515         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1516         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1517
1518         # clear quota doesn't trigger per-ID lock cancellation
1519         resetquota -u $TSTUSR
1520         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1521         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1522
1523         # clear the per-ID lock
1524         do_facet ost1 $LCTL set_param -n $procf=clear
1525         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1526         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1527
1528         # spare quota should be released
1529         local OSTUUID=$(ostuuid_from_index 0)
1530         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1531         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1532         [ $limit -le $space ] ||
1533                 error "spare quota isn't released, limit:$limit, space:$space"
1534
1535         cleanup_quota_test
1536 }
1537 run_test 13 "Cancel per-ID lock in the LRU list"
1538
1539 test_15(){
1540         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1541
1542         wait_delete_completed
1543         sync_all_data || true
1544
1545         # test for user
1546         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1547                 error "set user quota failed"
1548         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1549         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1550                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1551         resetquota -u $TSTUSR
1552
1553         # test for group
1554         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1555                 error "set group quota failed"
1556         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1557         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1558                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1559         resetquota -g $TSTUSR
1560 }
1561 run_test 15 "Set over 4T block quota"
1562
1563 test_17sub() {
1564         local err_code=$1
1565         local BLKS=1    # 1M less than limit
1566         local TESTFILE=$DIR/$tdir/$tfile
1567
1568         setup_quota_test || error "setup quota failed with $?"
1569         trap cleanup_quota_test EXIT
1570
1571         # make sure the system is clean
1572         local USED=$(getquota -u $TSTUSR global curspace)
1573         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1574
1575         set_ost_qtype "ug" || error "enable ost quota failed"
1576         # make sure no granted quota on ost
1577         resetquota -u $TSTUSR
1578         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1579                 error "set quota failed"
1580
1581         quota_show_check b u $TSTUSR
1582
1583         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1584         lustre_fail mds 0xa04 $err_code
1585
1586         # write in background
1587         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1588         local DDPID=$!
1589
1590         sleep 2
1591         # write should be blocked and never finished
1592         if ! ps -p $DDPID  > /dev/null 2>&1; then
1593                 lustre_fail mds 0 0
1594                 quota_error u $TSTUSR "write finished incorrectly!"
1595         fi
1596
1597         lustre_fail mds 0 0
1598
1599         local count=0
1600         local timeout=30
1601         while [ true ]; do
1602                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1603                 count=$((count+1))
1604                 if [ $count -gt $timeout ]; then
1605                         quota_error u $TSTUSR "dd is not finished!"
1606                 fi
1607                 sleep 1
1608         done
1609
1610         sync; sync_all_data || true
1611
1612         USED=$(getquota -u $TSTUSR global curspace)
1613         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1614                 "Used space(${USED}K) is less than ${BLKS}M"
1615
1616         cleanup_quota_test
1617         resetquota -u $TSTUSR
1618 }
1619
1620 # DQACQ return recoverable error
1621 test_17() {
1622         echo "DQACQ return -ENOLCK"
1623         #define ENOLCK  37
1624         test_17sub 37 || error "Handle -ENOLCK failed"
1625
1626         echo "DQACQ return -EAGAIN"
1627         #define EAGAIN  11
1628         test_17sub 11 || error "Handle -EAGAIN failed"
1629
1630         echo "DQACQ return -ETIMEDOUT"
1631         #define ETIMEDOUT 110
1632         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1633
1634         echo "DQACQ return -ENOTCONN"
1635         #define ENOTCONN 107
1636         test_17sub 107 || error "Handle -ENOTCONN failed"
1637 }
1638
1639 run_test 17 "DQACQ return recoverable error"
1640
1641 test_18_sub () {
1642         local io_type=$1
1643         local blimit="200m" # 200M
1644         local TESTFILE="$DIR/$tdir/$tfile"
1645
1646         setup_quota_test || error "setup quota failed with $?"
1647         trap cleanup_quota_test EXIT
1648
1649         set_ost_qtype "u" || error "enable ost quota failed"
1650         log "User quota (limit: $blimit)"
1651         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1652                 error "set quota failed"
1653         quota_show_check b u $TSTUSR
1654
1655         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
1656         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1657
1658         local timeout=$(sysctl -n lustre.timeout)
1659
1660         if [ $io_type = "directio" ]; then
1661                 log "Write 100M (directio) ..."
1662                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1663         else
1664                 log "Write 100M (buffered) ..."
1665                 $RUNAS $DD of=$TESTFILE count=100 &
1666         fi
1667         local DDPID=$!
1668
1669         replay_barrier $SINGLEMDS
1670         log "Fail mds for $((2 * timeout)) seconds"
1671         fail $SINGLEMDS $((2 * timeout))
1672
1673         local count=0
1674         if at_is_enabled; then
1675                 timeout=$(at_max_get mds)
1676         else
1677                 timeout=$(lctl get_param -n timeout)
1678         fi
1679
1680         while [ true ]; do
1681                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1682                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1683                         log "it took $count second"
1684                 fi
1685                 sleep 1
1686         done
1687
1688         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1689         sync
1690         cancel_lru_locks mdc
1691         cancel_lru_locks osc
1692         $SHOW_QUOTA_USER
1693
1694         local testfile_size=$(stat -c %s $TESTFILE)
1695         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1696                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1697                         "got ${testfile_size}. Verifying file failed!"
1698         fi
1699         cleanup_quota_test
1700         resetquota -u $TSTUSR
1701 }
1702
1703 # test when mds does failover, the ost still could work well
1704 # this test shouldn't trigger watchdog b=14840
1705 test_18() {
1706         # Clear dmesg so watchdog is not triggered by previous
1707         # test output
1708         do_facet ost1 dmesg -c > /dev/null
1709
1710         test_18_sub normal
1711         test_18_sub directio
1712
1713         # check if watchdog is triggered
1714         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1715         local watchdog=$(awk '/Service thread pid/ && /was inactive/ \
1716                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1717         [ -z "$watchdog" ] || error "$watchdog"
1718         rm -f $TMP/lustre-log-${TESTNAME}.log
1719 }
1720 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1721
1722 test_19() {
1723         local blimit=5 # 5M
1724         local TESTFILE=$DIR/$tdir/$tfile
1725
1726         setup_quota_test || error "setup quota failed with $?"
1727         trap cleanup_quota_test EXIT
1728
1729         set_ost_qtype "ug" || error "enable ost quota failed"
1730
1731         # bind file to a single OST
1732         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
1733         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1734
1735         echo "Set user quota (limit: ${blimit}M)"
1736         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1737                 error "set user quota failed"
1738         quota_show_check b u $TSTUSR
1739         echo "Update quota limits"
1740         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1741                 error "set group quota failed"
1742         quota_show_check b u $TSTUSR
1743
1744         # first wirte might be cached
1745         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
1746         cancel_lru_locks osc
1747         $SHOW_QUOTA_USER
1748         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
1749                 quota_error u $TSTUSR "Write success, expect failure"
1750         $SHOW_QUOTA_USER
1751
1752         cleanup_quota_test
1753         resetquota -u $TSTUSR
1754 }
1755 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
1756
1757 test_20() { # b15754
1758         local LSTR=(2g 1t 4k 3m) # limits strings
1759         # limits values
1760         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
1761                     $((3*1024*1024)))
1762
1763         resetquota -u $TSTUSR
1764
1765         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1766                 $MOUNT || error "could not set quota limits"
1767         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1768                                 --inode-softlimit ${LSTR[2]} \
1769                                 --inode-hardlimit ${LSTR[3]} \
1770                                 $MOUNT || error "could not set quota limits"
1771
1772         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
1773                 error "bsoftlimit was not set properly"
1774         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
1775                 error "bhardlimit was not set properly"
1776         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
1777                 error "isoftlimit was not set properly"
1778         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
1779                 error "ihardlimit was not set properly"
1780
1781         resetquota -u $TSTUSR
1782 }
1783 run_test 20 "Test if setquota specifiers work properly (b15754)"
1784
1785 test_21_sub() {
1786         local testfile=$1
1787         local blk_number=$2
1788         local seconds=$3
1789
1790         local time=$(($(date +%s) + seconds))
1791         while [ $(date +%s) -lt $time ]; do
1792                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
1793         done
1794 }
1795
1796 # run for fixing bug16053, setquota shouldn't fail when writing and
1797 # deleting are happening
1798 test_21() {
1799         local TESTFILE="$DIR/$tdir/$tfile"
1800         local BLIMIT=10 # 10G
1801         local ILIMIT=1000000
1802
1803         setup_quota_test || error "setup quota failed with $?"
1804         trap cleanup_quota_test EXIT
1805
1806         set_ost_qtype "ug" || error "Enable ost quota failed"
1807
1808         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
1809         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
1810                 error "set user quota failed"
1811         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
1812         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
1813                 error "set group quota failed"
1814
1815         # repeat writing on a 1M file
1816         test_21_sub ${TESTFILE}_1 1 30 &
1817         local DDPID1=$!
1818         # repeat writing on a 128M file
1819         test_21_sub ${TESTFILE}_2 128 30 &
1820         local DDPID2=$!
1821
1822         local time=$(($(date +%s) + 30))
1823         local i=1
1824         while [ $(date +%s) -lt $time ]; do
1825                 log "Set quota for $i times"
1826                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1827                         -I $((ILIMIT + i)) $MOUNT ||
1828                                 error "Set user quota failed"
1829                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1830                         -I $((ILIMIT + i)) $MOUNT ||
1831                                 error "Set group quota failed"
1832                 i=$((i+1))
1833                 sleep 1
1834         done
1835
1836         local count=0
1837         while [ true ]; do
1838                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1839                 count=$((count+1))
1840                 if [ $count -gt 60 ]; then
1841                         quota_error a $TSTUSR "dd should be finished!"
1842                 fi
1843                 sleep 1
1844         done
1845         echo "(dd_pid=$DDPID1, time=$count)successful"
1846
1847         count=0
1848         while [ true ]; do
1849                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1850                 count=$((count+1))
1851                 if [ $count -gt 60 ]; then
1852                         quota_error a $TSTUSR "dd should be finished!"
1853                 fi
1854                 sleep 1
1855         done
1856         echo "(dd_pid=$DDPID2, time=$count)successful"
1857
1858         cleanup_quota_test
1859         resetquota -u $TSTUSR
1860         resetquota -g $TSTUSR
1861 }
1862 run_test 21 "Setquota while writing & deleting (b16053)"
1863
1864 # enable/disable quota enforcement permanently
1865 test_22() {
1866         echo "Set both mdt & ost quota type as ug"
1867         set_mdt_qtype "ug" || error "enable mdt quota failed"
1868         set_ost_qtype "ug" || error "enable ost quota failed"
1869
1870         echo "Restart..."
1871         stopall || error "failed to stopall (1)"
1872         mount
1873         setupall
1874
1875         echo "Verify if quota is enabled"
1876         local qtype=$(mdt_quota_type)
1877         [ $qtype != "ug" ] && error "mdt quota setting is lost"
1878         qtype=$(ost_quota_type)
1879         [ $qtype != "ug" ] && error "ost quota setting is lost"
1880
1881         echo "Set both mdt & ost quota type as none"
1882         set_mdt_qtype "none" || error "disable mdt quota failed"
1883         set_ost_qtype "none" || error "disable ost quota failed"
1884
1885         echo "Restart..."
1886         stopall || error "failed to stopall (2)"
1887         mount
1888         setupall
1889         quota_init
1890
1891         echo "Verify if quota is disabled"
1892         qtype=$(mdt_quota_type)
1893         [ $qtype != "none" ] && error "mdt quota setting is lost"
1894         qtype=$(ost_quota_type)
1895         [ $qtype != "none" ] && error "ost quota setting is lost"
1896
1897         return 0
1898 }
1899 run_test 22 "enable/disable quota by 'lctl conf_param'"
1900
1901 test_23_sub() {
1902         local TESTFILE="$DIR/$tdir/$tfile"
1903         local LIMIT=$1
1904
1905         setup_quota_test || error "setup quota failed with $?"
1906         trap cleanup_quota_test EXIT
1907
1908         set_ost_qtype "ug" || error "Enable ost quota failed"
1909
1910         # test for user
1911         log "User quota (limit: $LIMIT MB)"
1912         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
1913                 error "set quota failed"
1914         quota_show_check b u $TSTUSR
1915
1916         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1917         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1918
1919         log "Step1: trigger EDQUOT with O_DIRECT"
1920         log "Write half of file"
1921         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
1922                 quota_error u $TSTUSR "(1) Write failure, expect success." \
1923                         "limit=$LIMIT"
1924         log "Write out of block quota ..."
1925         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
1926                 oflag=direct conv=notrunc &&
1927                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
1928                         "limit=$LIMIT"
1929         log "Step1: done"
1930
1931         log "Step2: rewrite should succeed"
1932         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
1933                 quota_error u $TSTUSR "(3) Write failure, expect success." \
1934                         "limit=$LIMIT"
1935         log "Step2: done"
1936
1937         cleanup_quota_test
1938
1939         local OST0_UUID=$(ostuuid_from_index 0)
1940         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
1941         [ $OST0_QUOTA_USED -ne 0 ] &&
1942                 ($SHOW_QUOTA_USER; \
1943                 quota_error u $TSTUSR "quota isn't released")
1944         $SHOW_QUOTA_USER
1945         resetquota -u $TSTUSR
1946 }
1947
1948 test_23() {
1949         [ $(facet_fstype ost1) == "zfs" ] &&
1950                 skip "Overwrite in place is not guaranteed to be " \
1951                 "space neutral on ZFS" && return
1952
1953         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
1954         check_whether_skip && return 0
1955         log "run for 4MB test file"
1956         test_23_sub 4
1957
1958         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
1959         check_whether_skip && return 0
1960         log "run for 40MB test file"
1961         test_23_sub 40
1962 }
1963 run_test 23 "Quota should be honored with directIO (b16125)"
1964
1965 test_24() {
1966         local blimit=5 # 5M
1967         local TESTFILE="$DIR/$tdir/$tfile"
1968
1969         setup_quota_test || error "setup quota failed with $?"
1970         trap cleanup_quota_test EXIT
1971
1972         set_ost_qtype "ug" || error "enable ost quota failed"
1973
1974         # bind file to a single OST
1975         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
1976         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1977
1978         echo "Set user quota (limit: ${blimit}M)"
1979         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
1980                 error "set quota failed"
1981
1982         # overrun quota by root user
1983         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
1984                 error "write failure, expect success"
1985         cancel_lru_locks osc
1986         sync_all_data || true
1987
1988         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1989
1990         cleanup_quota_test
1991         resetquota -u $TSTUSR
1992 }
1993 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
1994
1995 test_27a() { # b19612
1996         $LFS quota $TSTUSR $DIR &&
1997                 error "lfs succeeded with no type, but should have failed"
1998         $LFS setquota $TSTUSR $DIR &&
1999                 error "lfs succeeded with no type, but should have failed"
2000         return 0
2001 }
2002 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2003
2004 test_27b() { # b20200
2005         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2006                 error "lfs setquota failed with uid argument"
2007         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2008                 error "lfs stequota failed with gid argument"
2009         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2010         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2011         resetquota -u $TSTUSR
2012         resetquota -g $TSTUSR
2013         return 0
2014 }
2015 run_test 27b "lfs quota/setquota should handle user/group ID (b20200)"
2016
2017 test_27c() {
2018         local limit
2019
2020         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2021                 error "lfs setquota failed"
2022
2023         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2024         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2025         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2026         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2027
2028         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2029                 error "lfs setquota for $TSTID failed"
2030
2031         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2032         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2033         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2034         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2035
2036         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2037                 error "total allocated inode/block limit not printed"
2038
2039         resetquota -u $TSTUSR
2040 }
2041 run_test 27c "lfs quota should support human-readable output"
2042
2043 test_27d() {
2044         local softlimit=1.5
2045         local hardlimit=2.3
2046         local limit
2047
2048         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2049                 error "set fraction block limit failed"
2050         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2051         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2052         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2053         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2054
2055         resetquota -u $TSTUSR
2056 }
2057 run_test 27d "lfs setquota should support fraction block limit"
2058
2059 test_30() {
2060         local LIMIT=4 # 4MB
2061         local TESTFILE="$DIR/$tdir/$tfile"
2062         local GRACE=10
2063
2064         setup_quota_test || error "setup quota failed with $?"
2065         trap cleanup_quota_test EXIT
2066
2067         set_ost_qtype "u" || error "enable ost quota failed"
2068
2069         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2070         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2071
2072         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2073                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2074         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2075                 error "set quota failed"
2076         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2077         cancel_lru_locks osc
2078         sleep $GRACE
2079         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2080         # over-quota flag has not yet settled since we do not trigger async
2081         # events based on grace time period expiration
2082         $SHOW_QUOTA_USER
2083         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2084         cancel_lru_locks osc
2085         # now over-quota flag should be settled and further writes should fail
2086         $SHOW_QUOTA_USER
2087         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2088                 error "grace times were reset"
2089         # cleanup
2090         cleanup_quota_test
2091         resetquota -u $TSTUSR
2092         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2093                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2094 }
2095 run_test 30 "Hard limit updates should not reset grace times"
2096
2097 # basic usage tracking for user & group
2098 test_33() {
2099         local INODES=10 # 10 files
2100         local BLK_CNT=2 # of 2M each
2101         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2102
2103         setup_quota_test || error "setup quota failed with $?"
2104         trap cleanup_quota_test EXIT
2105
2106         # make sure the system is clean
2107         local USED=$(getquota -u $TSTID global curspace)
2108         [ $USED -ne 0 ] &&
2109                 error "Used space ($USED) for user $TSTID isn't 0."
2110         USED=$(getquota -g $TSTID global curspace)
2111         [ $USED -ne 0 ] &&
2112                 error "Used space ($USED) for group $TSTID isn't 0."
2113
2114         echo "Write files..."
2115         for i in $(seq 0 $INODES); do
2116                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2117                         error "write failed"
2118                 echo "Iteration $i/$INODES completed"
2119         done
2120         cancel_lru_locks osc
2121         sync; sync_all_data || true
2122
2123         echo "Verify disk usage after write"
2124         USED=$(getquota -u $TSTID global curspace)
2125         [ $USED -lt $TOTAL_BLKS ] &&
2126                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2127         USED=$(getquota -g $TSTID global curspace)
2128         [ $USED -lt $TOTAL_BLKS ] &&
2129                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2130
2131         echo "Verify inode usage after write"
2132         USED=$(getquota -u $TSTID global curinodes)
2133         [ $USED -lt $INODES ] &&
2134                 error "Used inode for user $TSTID is $USED, expected $INODES"
2135         USED=$(getquota -g $TSTID global curinodes)
2136         [ $USED -lt $INODES ] &&
2137                 error "Used inode for group $TSTID is $USED, expected $INODES"
2138
2139         cleanup_quota_test
2140
2141         echo "Verify disk usage after delete"
2142         USED=$(getquota -u $TSTID global curspace)
2143         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2144         USED=$(getquota -u $TSTID global curinodes)
2145         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2146         USED=$(getquota -g $TSTID global curspace)
2147         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2148         USED=$(getquota -g $TSTID global curinodes)
2149         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2150 }
2151 run_test 33 "Basic usage tracking for user & group"
2152
2153 # usage transfer test for user & group
2154 test_34() {
2155         local BLK_CNT=2 # 2MB
2156
2157         setup_quota_test || error "setup quota failed with $?"
2158         trap cleanup_quota_test EXIT
2159
2160         # make sure the system is clean
2161         local USED=$(getquota -u $TSTID global curspace)
2162         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2163         USED=$(getquota -g $TSTID global curspace)
2164         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2165
2166         local USED=$(getquota -u $TSTID2 global curspace)
2167         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2168
2169         echo "Write file..."
2170         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2171                 error "write failed"
2172         cancel_lru_locks osc
2173         sync; sync_all_data || true
2174
2175         echo "chown the file to user $TSTID"
2176         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2177
2178         echo "Wait for setattr on objects finished..."
2179         wait_delete_completed
2180
2181         BLK_CNT=$((BLK_CNT * 1024))
2182
2183         echo "Verify disk usage for user $TSTID"
2184         USED=$(getquota -u $TSTID global curspace)
2185         [ $USED -lt $BLK_CNT ] &&
2186                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2187         USED=$(getquota -u $TSTID global curinodes)
2188         [ $USED -ne 1 ] &&
2189                 error "Used inodes for user $TSTID is $USED, expected 1"
2190
2191         echo "chgrp the file to group $TSTID"
2192         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2193
2194         echo "Wait for setattr on objects finished..."
2195         wait_delete_completed
2196
2197         echo "Verify disk usage for group $TSTID"
2198         USED=$(getquota -g $TSTID global curspace)
2199         [ $USED -ge $BLK_CNT ] ||
2200                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2201         USED=$(getquota -g $TSTID global curinodes)
2202         [ $USED -eq 1 ] ||
2203                 error "Used inodes for group $TSTID is $USED, expected 1"
2204
2205         # chown won't change the ost object group. LU-4345 */
2206         echo "chown the file to user $TSTID2"
2207         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2208
2209         echo "Wait for setattr on objects finished..."
2210         wait_delete_completed
2211
2212         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2213         USED=$(getquota -u $TSTID2 global curspace)
2214         [ $USED -lt $BLK_CNT ] &&
2215                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2216         USED=$(getquota -u $TSTID global curspace)
2217         [ $USED -ne 0 ] &&
2218                 error "Used space for user $TSTID is $USED, expected 0"
2219         USED=$(getquota -g $TSTID global curspace)
2220         [ $USED -lt $BLK_CNT ] &&
2221                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2222
2223         cleanup_quota_test
2224 }
2225 run_test 34 "Usage transfer for user & group"
2226
2227 # usage is still accessible across restart
2228 test_35() {
2229         local BLK_CNT=2 # 2 MB
2230
2231         setup_quota_test || error "setup quota failed with $?"
2232         trap cleanup_quota_test EXIT
2233
2234         echo "Write file..."
2235         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2236                 error "write failed"
2237         cancel_lru_locks osc
2238         sync; sync_all_data || true
2239
2240         echo "Save disk usage before restart"
2241         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2242         [ $ORIG_USR_SPACE -eq 0 ] &&
2243                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2244         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2245         [ $ORIG_USR_INODES -eq 0 ] &&
2246                 error "Used inodes for user $TSTID is 0, expected 1"
2247         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2248         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2249         [ $ORIG_GRP_SPACE -eq 0 ] &&
2250                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2251         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2252         [ $ORIG_GRP_INODES -eq 0 ] &&
2253                 error "Used inodes for group $TSTID is 0, expected 1"
2254         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2255
2256         log "Restart..."
2257         local ORIG_REFORMAT=$REFORMAT
2258         REFORMAT=""
2259         cleanup_and_setup_lustre
2260         REFORMAT=$ORIG_REFORMAT
2261         quota_init
2262
2263         echo "Verify disk usage after restart"
2264         local USED=$(getquota -u $TSTID global curspace)
2265         [ $USED -eq $ORIG_USR_SPACE ] ||
2266                 error "Used space for user $TSTID changed from " \
2267                         "$ORIG_USR_SPACE to $USED"
2268         USED=$(getquota -u $TSTID global curinodes)
2269         [ $USED -eq $ORIG_USR_INODES ] ||
2270                 error "Used inodes for user $TSTID changed from " \
2271                         "$ORIG_USR_INODES to $USED"
2272         USED=$(getquota -g $TSTID global curspace)
2273         [ $USED -eq $ORIG_GRP_SPACE ] ||
2274                 error "Used space for group $TSTID changed from " \
2275                         "$ORIG_GRP_SPACE to $USED"
2276         USED=$(getquota -g $TSTID global curinodes)
2277         [ $USED -eq $ORIG_GRP_INODES ] ||
2278                 error "Used inodes for group $TSTID changed from " \
2279                         "$ORIG_GRP_INODES to $USED"
2280
2281         # check if the vfs_dq_init() is called before writing
2282         echo "Append to the same file..."
2283         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2284                 error "write failed"
2285         cancel_lru_locks osc
2286         sync; sync_all_data || true
2287
2288         echo "Verify space usage is increased"
2289         USED=$(getquota -u $TSTID global curspace)
2290         [ $USED -gt $ORIG_USR_SPACE ] ||
2291                 error "Used space for user $TSTID isn't increased" \
2292                         "orig:$ORIG_USR_SPACE, now:$USED"
2293         USED=$(getquota -g $TSTID global curspace)
2294         [ $USED -gt $ORIG_GRP_SPACE ] ||
2295                 error "Used space for group $TSTID isn't increased" \
2296                         "orig:$ORIG_GRP_SPACE, now:$USED"
2297
2298         cleanup_quota_test
2299 }
2300 run_test 35 "Usage is still accessible across reboot"
2301
2302 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2303 # LU-5006
2304 test_37() {
2305         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
2306                 skip "Old server doesn't have LU-5006 fix." && return
2307
2308         setup_quota_test || error "setup quota failed with $?"
2309         trap cleanup_quota_test EXIT
2310
2311         # make sure the system is clean
2312         local USED=$(getquota -u $TSTID global curspace)
2313         [ $USED -ne 0 ] &&
2314                 error "Used space ($USED) for user $TSTID isn't 0."
2315
2316         # create file with MDS_OPEN_DELAY_CREATE flag
2317         $SETSTRIPE -c 1 -i 0 $DIR/$tdir/$tfile ||
2318                 error "Create file failed"
2319         # write to file
2320         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2321                 oflag=sync || error "Write file failed"
2322         # chown to the file
2323         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2324
2325         # wait for setattr on objects finished..."
2326         wait_delete_completed
2327
2328         USED=$(getquota -u $TSTID global curspace)
2329         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2330
2331         cleanup_quota_test
2332 }
2333 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2334
2335 # LU-8801
2336 test_38() {
2337         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.60) ] &&
2338                 skip "Old server doesn't have LU-8801 fix." && return
2339
2340         [ "$UID" != 0 ] && skip_env "must run as root" && return
2341
2342         setup_quota_test || error "setup quota failed with $?"
2343         trap cleanup_quota_test EXIT
2344
2345         # make sure the system is clean
2346         local USED=$(getquota -u $TSTID global curspace)
2347         [ $USED -ne 0 ] &&
2348                 error "Used space ($USED) for user $TSTID isn't 0."
2349         USED=$(getquota -u $TSTID2 global curspace)
2350         [ $USED -ne 0 ] &&
2351                 error "Used space ($USED) for user $TSTID2 isn't 0."
2352
2353         local TESTFILE="$DIR/$tdir/$tfile"
2354         local file_cnt=10000
2355
2356         # Generate id entries in accounting file
2357         echo "Create $file_cnt files..."
2358         for i in `seq $file_cnt`; do
2359                 touch $TESTFILE-$i
2360                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i
2361         done
2362         cancel_lru_locks osc
2363         sync; sync_all_data || true
2364
2365         local procf="osd-$(facet_fstype $SINGLEMDS).$FSNAME-MDT0000"
2366         procf=${procf}.quota_slave.acct_user
2367         local accnt_cnt
2368
2369         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | wc -l)
2370         echo "Found $acct_cnt id entries"
2371
2372         [ $file_cnt -eq $acct_cnt ] || error "skipped id entries"
2373
2374         cleanup_quota_test
2375 }
2376 run_test 38 "Quota accounting iterator doesn't skip id entries"
2377
2378 quota_fini()
2379 {
2380         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2381 }
2382 quota_fini
2383
2384 cd $ORIG_PWD
2385 complete $SECONDS
2386 check_and_cleanup_lustre
2387 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2388 exit_status