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