Whamcloud - gitweb
LU-2284 tests: enable D_QUOTA after mds/ost restart
[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 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             [ $rate -gt 1024 ] ||
360                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
361         fi
362         rm -f $TESTFILE
363 }
364
365 # test basic quota performance b=21696
366 test_0() {
367         local MB=100 # 100M
368         [ "$SLOW" = "no" ] && MB=10
369
370         local free_space=$(lfs df | grep "filesystem summary" | \
371                                 awk '{print $5}')
372         [ $free_space -le $((MB * 1024)) ] &&
373                 skip "not enough space ${free_space} KB, " \
374                         "required $((MB * 1024)) KB" && return
375         setup_quota_test
376         trap cleanup_quota_test EXIT
377
378         set_ost_qtype "none" || error "disable ost quota failed"
379         test_quota_performance $MB
380
381         set_ost_qtype "ug" || error "enable ost quota failed"
382         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
383                 error "set quota failed"
384         test_quota_performance $MB
385
386         cleanup_quota_test
387         resetquota -u $TSTUSR
388 }
389 run_test 0 "Test basic quota performance"
390
391 # test block hardlimit
392 test_1() {
393         local LIMIT=10  # 10M
394         local TESTFILE="$DIR/$tdir/$tfile-0"
395
396         setup_quota_test
397         trap cleanup_quota_test EXIT
398
399         # enable ost quota
400         set_ost_qtype "ug" || "enable ost quota failed"
401
402         # test for user
403         log "User quota (block hardlimit:$LIMIT MB)"
404         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
405                 error "set user quota failed"
406
407         # make sure the system is clean
408         local USED=$(getquota -u $TSTUSR global curspace)
409         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
410
411         $LFS setstripe $TESTFILE -c 1
412         chown $TSTUSR.$TSTUSR $TESTFILE
413
414         log "Write..."
415         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
416                 quota_error u $TSTUSR "user write failure, but expect success"
417         log "Write out of block quota ..."
418         # this time maybe cache write,  ignore it's failure
419         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
420         # flush cache, ensure noquota flag is set on client
421         cancel_lru_locks osc
422         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
423                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
424
425         rm -f $TESTFILE
426         wait_delete_completed
427         sync_all_data || true
428         USED=$(getquota -u $TSTUSR global curspace)
429         [ $USED -ne 0 ] && quota_error u $TSTUSR \
430                 "user quota isn't released after deletion"
431         resetquota -u $TSTUSR
432
433         # test for group
434         log "--------------------------------------"
435         log "Group quota (block hardlimit:$LIMIT MB)"
436         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
437                 error "set group quota failed"
438
439         TESTFILE="$DIR/$tdir/$tfile-1"
440         # make sure the system is clean
441         USED=$(getquota -g $TSTUSR global curspace)
442         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0"
443
444         $LFS setstripe $TESTFILE -c 1
445         chown $TSTUSR.$TSTUSR $TESTFILE
446
447         log "Write ..."
448         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
449                 quota_error g $TSTUSR "group write failure, but expect success"
450         log "Write out of block quota ..."
451         # this time maybe cache write, ignore it's failure
452         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
453         cancel_lru_locks osc
454         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
455                 quota_error g $TSTUSR "group write success, but expect EDQUOT"
456
457         # cleanup
458         cleanup_quota_test
459
460         USED=$(getquota -g $TSTUSR global curspace)
461         [ $USED -ne 0 ] && quota_error g $TSTUSR \
462                 "group quota isn't released after deletion"
463
464         resetquota -g $TSTUSR
465 }
466 run_test 1 "Block hard limit (normal use and out of quota)"
467
468 # test inode hardlimit
469 test_2() {
470         local LIMIT=$((1024 * 1024)) # 1M inodes
471         local TESTFILE="$DIR/$tdir/$tfile-0"
472
473         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
474
475         local FREE_INODES=$(lfs df -i | grep "filesystem summary" | \
476                                 awk '{print $5}')
477         [ $FREE_INODES -lt $LIMIT ] &&
478                 skip "not enough free inodes $FREE_INODES required $LIMIT" &&
479                 return
480
481         setup_quota_test
482         trap cleanup_quota_test EXIT
483
484         # enable mdt quota
485         set_mdt_qtype "ug" || "enable mdt quota failed"
486
487         # test for user
488         log "User quota (inode hardlimit:$LIMIT files)"
489         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
490                 error "set user quota failed"
491
492         # make sure the system is clean
493         local USED=$(getquota -u $TSTUSR global curinodes)
494         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
495
496         log "Create $LIMIT files ..."
497         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
498                 quota_error u $TSTUSR "user create failure, but expect success"
499         log "Create out of file quota ..."
500         $RUNAS touch ${TESTFILE}_xxx && \
501                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
502
503         # cleanup
504         unlinkmany ${TESTFILE} $LIMIT
505         rm -f ${TESTFILE}_xxx
506         wait_delete_completed
507
508         USED=$(getquota -u $TSTUSR global curinodes)
509         [ $USED -ne 0 ] && quota_error u $TSTUSR \
510                 "user quota isn't released after deletion"
511         resetquota -u $TSTUSR
512
513         # test for group
514         log "--------------------------------------"
515         log "Group quota (inode hardlimit:$LIMIT files)"
516         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
517                 error "set group quota failed"
518
519         TESTFILE=$DIR/$tdir/$tfile-1
520         # make sure the system is clean
521         USED=$(getquota -g $TSTUSR global curinodes)
522         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
523
524         log "Create $LIMIT files ..."
525         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
526                 quota_error g $TSTUSR "group create failure, but expect success"
527         log "Create out of file quota ..."
528         $RUNAS touch ${TESTFILE}_xxx && \
529                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
530
531         # cleanup
532         unlinkmany ${TESTFILE} $LIMIT
533         rm -f ${TESTFILE}_xxx
534         wait_delete_completed
535
536         USED=$(getquota -g $TSTUSR global curinodes)
537         [ $USED -ne 0 ] && quota_error g $TSTUSR \
538                 "user quota isn't released after deletion"
539
540         cleanup_quota_test
541         resetquota -g $TSTUSR
542 }
543 run_test 2 "File hard limit (normal use and out of quota)"
544
545 test_block_soft() {
546         local TESTFILE=$1
547         local TIMER=$(($2 * 3 / 2))
548         local LIMIT=$3
549         local OFFSET=0
550
551         setup_quota_test
552         trap cleanup_quota_test EXIT
553
554         $LFS setstripe $TESTFILE -c 1 -i 0
555         chown $TSTUSR.$TSTUSR $TESTFILE
556
557         echo "Write up to soft limit"
558         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
559                 quota_error a $TSTUSR "write failure, but expect success"
560         OFFSET=$((LIMIT * 1024))
561         cancel_lru_locks osc
562
563         echo "Write to exceed soft limit"
564         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
565                 quota_error a $TSTUSR "write failure, but expect success"
566         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
567         cancel_lru_locks osc
568
569         $SHOW_QUOTA_USER
570         $SHOW_QUOTA_GROUP
571         $SHOW_QUOTA_INFO_USER
572         $SHOW_QUOTA_INFO_GROUP
573
574         echo "Write before timer goes off"
575         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
576                 quota_error a $TSTUSR "write failure, but expect success"
577         OFFSET=$((OFFSET + 1024))
578         cancel_lru_locks osc
579
580         echo "Sleep $TIMER seconds ..."
581         sleep $TIMER
582
583         $SHOW_QUOTA_USER
584         $SHOW_QUOTA_GROUP
585         $SHOW_QUOTA_INFO_USER
586         $SHOW_QUOTA_INFO_GROUP
587
588         echo "Write after timer goes off"
589         # maybe cache write, ignore.
590         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
591         OFFSET=$((OFFSET + 1024))
592         cancel_lru_locks osc
593         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
594                 quota_error a $TSTUSR "write success, but expect EDQUOT"
595
596         $SHOW_QUOTA_USER
597         $SHOW_QUOTA_GROUP
598         $SHOW_QUOTA_INFO_USER
599         $SHOW_QUOTA_INFO_GROUP
600
601         echo "Unlink file to stop timer"
602         rm -f $TESTFILE
603         wait_delete_completed
604         sync_all_data || true
605
606         $SHOW_QUOTA_USER
607         $SHOW_QUOTA_GROUP
608         $SHOW_QUOTA_INFO_USER
609         $SHOW_QUOTA_INFO_GROUP
610
611         $LFS setstripe $TESTFILE -c 1 -i 0
612         chown $TSTUSR.$TSTUSR $TESTFILE
613
614         echo "Write ..."
615         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
616                 quota_error a $TSTUSR "write failure, but expect success"
617         # cleanup
618         cleanup_quota_test
619 }
620
621 # block soft limit
622 test_3() {
623         local LIMIT=1  # 1MB
624         local GRACE=10 # 10s
625         local TESTFILE=$DIR/$tdir/$tfile-0
626
627         set_ost_qtype "ug" || error "enable ost quota failed"
628
629         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
630         # make sure the system is clean
631         local USED=$(getquota -u $TSTUSR global curspace)
632         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
633
634         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
635                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
636         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
637                 error "set user quota failed"
638
639         test_block_soft $TESTFILE $GRACE $LIMIT
640         resetquota -u $TSTUSR
641
642         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
643         TESTFILE=$DIR/$tdir/$tfile-1
644         # make sure the system is clean
645         USED=$(getquota -g $TSTUSR global curspace)
646         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
647
648         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
649                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
650         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
651                 error "set group quota failed"
652
653         test_block_soft $TESTFILE $GRACE $LIMIT
654         resetquota -g $TSTUSR
655
656         # cleanup
657         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
658                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
659         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
660                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
661 }
662 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
663
664 test_file_soft() {
665         local TESTFILE=$1
666         local LIMIT=$2
667         local TIMER=$(($3 * 3 / 2))
668
669         setup_quota_test
670         trap cleanup_quota_test EXIT
671
672         echo "Create files to exceed soft limit"
673         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
674                 quota_error a $TSTUSR "create failure, but expect success"
675         sync; sleep 1; sync
676
677         echo "Create file before timer goes off"
678         $RUNAS touch ${TESTFILE}_before ||
679                 quota_error a $TSTUSR "failed create before timer expired," \
680                         "but expect success"
681         sync; sleep 1; sync
682
683         echo "Sleep $TIMER seconds ..."
684         sleep $TIMER
685
686         $SHOW_QUOTA_USER
687         $SHOW_QUOTA_GROUP
688         $SHOW_QUOTA_INFO_USER
689         $SHOW_QUOTA_INFO_GROUP
690
691         echo "Create file after timer goes off"
692         # There is a window that space is accounted in the quota usage but
693         # hasn't been decreased from the pending write, if we acquire quota
694         # in this window, we'll acquire more than we needed.
695         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
696         sync; sleep 1; sync
697         $RUNAS touch ${TESTFILE}_after_3 &&
698                 quota_error a $TSTUSR "create after timer expired," \
699                         "but expect EDQUOT"
700         sync; sleep 1; sync
701
702         $SHOW_QUOTA_USER
703         $SHOW_QUOTA_GROUP
704         $SHOW_QUOTA_INFO_USER
705         $SHOW_QUOTA_INFO_GROUP
706
707         echo "Unlink files to stop timer"
708         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
709         wait_delete_completed
710
711         echo "Create file"
712         $RUNAS touch ${TESTFILE}_xxx ||
713                 quota_error a $TSTUSR "touch after timer stop failure," \
714                         "but expect success"
715         sync; sleep 1; sync
716
717         # cleanup
718         cleanup_quota_test
719 }
720
721 # file soft limit
722 test_4a() {
723         local LIMIT=10 # inodes
724         local TESTFILE=$DIR/$tdir/$tfile-0
725         local GRACE=5
726
727         set_mdt_qtype "ug" || error "enable mdt quota failed"
728
729         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
730         # make sure the system is clean
731         local USED=$(getquota -u $TSTUSR global curinodes)
732         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
733
734         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
735                 $GRACE $DIR || error "set user grace time failed"
736         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
737                 error "set user quota failed"
738
739         test_file_soft $TESTFILE $LIMIT $GRACE
740         resetquota -u $TSTUSR
741
742         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
743         # make sure the system is clean
744         USED=$(getquota -g $TSTUSR global curinodes)
745         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
746
747         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
748                 $GRACE $DIR || error "set group grace time failed"
749         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
750                 error "set group quota failed"
751         TESTFILE=$DIR/$tdir/$tfile-1
752
753         test_file_soft $TESTFILE $LIMIT $GRACE
754         resetquota -g $TSTUSR
755
756         # cleanup
757         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
758                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
759         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
760                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
761 }
762 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
763
764 test_4b() {
765         local GR_STR1="1w3d"
766         local GR_STR2="1000s"
767         local GR_STR3="5s"
768         local GR_STR4="1w2d3h4m5s"
769         local GR_STR5="5c"
770         local GR_STR6="1111111111111111"
771
772         wait_delete_completed
773
774         # test of valid grace strings handling
775         echo "Valid grace strings test"
776         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
777                 $GR_STR2 $DIR || error "set user grace time failed"
778         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
779         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
780                 $GR_STR4 $DIR || error "set group grace time quota failed"
781         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
782
783         # test of invalid grace strings handling
784         echo "  Invalid grace strings test"
785         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
786         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
787
788         # cleanup
789         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
790                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
791         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
792                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
793 }
794 run_test 4b "Grace time strings handling"
795
796 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
797 test_5() {
798         local BLIMIT=10 # 10M
799         local ILIMIT=10 # 10 inodes
800
801         setup_quota_test
802         trap cleanup_quota_test EXIT
803
804         set_mdt_qtype "ug" || error "enable mdt quota failed"
805         set_ost_qtype "ug" || error "enable ost quota failed"
806
807         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
808         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
809                 error "set user quota failed"
810         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
811                 error "set group quota failed"
812
813         # make sure the system is clean
814         local USED=$(getquota -u $TSTUSR global curinodes)
815         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
816         USED=$(getquota -g $TSTUSR global curinodes)
817         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
818         USED=$(getquota -u $TSTUSR global curspace)
819         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
820         USED=$(getquota -g $TSTUSR global curspace)
821         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
822
823         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
824         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
825                 error "create failure, expect success"
826         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
827                 error "write failure, expect success"
828
829         echo "Chown files to $TSTUSR.$TSTUSR ..."
830         for i in `seq 0 $ILIMIT`; do
831                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
832                         quota_error a $TSTUSR "chown failure, expect success"
833         done
834
835         # cleanup
836         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
837         cleanup_quota_test
838
839         resetquota -u $TSTUSR
840         resetquota -g $TSTUSR
841 }
842 run_test 5 "Chown & chgrp successfully even out of block/file quota"
843
844 # test dropping acquire request on master
845 test_6() {
846         local LIMIT=3 # 3M
847
848         setup_quota_test
849         trap cleanup_quota_test EXIT
850
851         # make sure the system is clean
852         local USED=$(getquota -u $TSTUSR global curspace)
853         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
854
855         # make sure no granted quota on ost
856         set_ost_qtype "ug" || error "enable ost quota failed"
857         resetquota -u $TSTUSR
858
859         # create file for $TSTUSR
860         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
861         $LFS setstripe $TESTFILE -c 1 -i 0
862         chown $TSTUSR.$TSTUSR $TESTFILE
863
864         # create file for $TSTUSR2
865         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
866         $LFS setstripe $TESTFILE2 -c 1 -i 0
867         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
868
869         # cache per-ID lock for $TSTUSR on slave
870         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
871                 error "set quota failed"
872         $RUNAS $DD of=$TESTFILE count=1 ||
873                 error "write $TESTFILE failure, expect success"
874         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
875                 error "write $TESTFILE2 failure, expect success"
876         sync; sync
877         sync_all_data || true
878
879         #define QUOTA_DQACQ 601
880         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
881         lustre_fail mds 0x513 601
882
883         # write to un-enforced ID ($TSTUSR2) should succeed
884         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
885                 error "write failure, expect success"
886
887         # write to enforced ID ($TSTUSR) in background, exceeding limit
888         # to make sure DQACQ is sent
889         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
890         DDPID=$!
891
892         # watchdog timer uses a factor of 2
893         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
894         sleep $((TIMEOUT * 2 + 1))
895
896         # write should be blocked and never finished
897         if ! ps -p $DDPID  > /dev/null 2>&1; then
898                 lustre_fail mds 0 0
899                 error "write finished incorrectly!"
900         fi
901
902         lustre_fail mds 0 0
903
904         # no watchdog is triggered
905         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
906         watchdog=$(awk '/sanity-quota test 6/ {start = 1;}
907                        /Service thread pid/ && /was inactive/ {
908                                if (start) {
909                                        print;
910                                }
911                        }' $TMP/lustre-log-${TESTNAME}.log)
912         [ -z "$watchdog" ] || error "$watchdog"
913
914         rm -f $TMP/lustre-log-${TESTNAME}.log
915
916         # write should continue & succeed
917         local count=0
918         while [ true ]; do
919                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
920                 if [ $count -ge 120 ]; then
921                         quota_error u $TSTUSR "dd not finished in $count secs"
922                 fi
923                 count=$((count + 1))
924                 [ $((count % 10)) -eq 0 ] && echo "Waiting $count secs"
925                 sleep 1
926         done
927
928         cleanup_quota_test
929         resetquota -u $TSTUSR
930 }
931 run_test 6 "Test dropping acquire request on master"
932
933 # quota reintegration (global index)
934 test_7a() {
935         local TESTFILE=$DIR/$tdir/$tfile
936         local LIMIT=20 # 20M
937
938         [ "$SLOW" = "no" ] && LIMIT=5
939
940         setup_quota_test
941         trap cleanup_quota_test EXIT
942
943         # make sure the system is clean
944         local USED=$(getquota -u $TSTUSR global curspace)
945         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
946
947         # make sure no granted quota on ost1
948         set_ost_qtype "ug" || error "enable ost quota failed"
949         resetquota -u $TSTUSR
950         set_ost_qtype "none" || error "disable ost quota failed"
951
952         local OSTUUID=$(ostuuid_from_index 0)
953         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
954         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
955                 "$TSTUSR isn't 0."
956
957         # create test file
958         $LFS setstripe $TESTFILE -c 1 -i 0
959         chown $TSTUSR.$TSTUSR $TESTFILE
960
961         echo "Stop ost1..."
962         stop ost1
963
964         echo "Enable quota & set quota limit for $TSTUSR"
965         set_ost_qtype "ug" || error "enable ost quota failed"
966         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
967                 error "set quota failed"
968
969         echo "Start ost1..."
970         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
971         quota_init
972
973         wait_ost_reint "ug" || error "reintegration failed"
974
975         # hardlimit should have been fetched by slave during global
976         # reintegration, write will exceed quota
977         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
978                 quota_error u $TSTUSR "write success, but expect EDQUOT"
979
980         rm -f $TESTFILE
981         wait_delete_completed
982         sync_all_data || true
983         sleep 3
984
985         echo "Stop ost1..."
986         stop ost1
987
988         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
989                 error "clear quota failed"
990
991         echo "Start ost1..."
992         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
993         quota_init
994
995         wait_ost_reint "ug" || error "reintegration failed"
996
997         # hardlimit should be cleared on slave during reintegration
998         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
999                 quota_error u $TSTUSR "write error, but expect success"
1000
1001         cleanup_quota_test
1002         resetquota -u $TSTUSR
1003 }
1004 run_test 7a "Quota reintegration (global index)"
1005
1006 # quota reintegration (slave index)
1007 test_7b() {
1008         local LIMIT="100G"
1009         local TESTFILE=$DIR/$tdir/$tfile
1010
1011         setup_quota_test
1012         trap cleanup_quota_test EXIT
1013
1014         # make sure the system is clean
1015         local USED=$(getquota -u $TSTUSR global curspace)
1016         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1017
1018         # make sure no granted quota on ost1
1019         set_ost_qtype "ug" || error "enable ost quota failed"
1020         resetquota -u $TSTUSR
1021         set_ost_qtype "none" || error "disable ost quota failed"
1022
1023         local OSTUUID=$(ostuuid_from_index 0)
1024         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1025         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
1026                 "$TSTUSR isn't 0."
1027
1028         # create test file
1029         $LFS setstripe $TESTFILE -c 1 -i 0
1030         chown $TSTUSR.$TSTUSR $TESTFILE
1031
1032         # consume some space to make sure the granted space will not
1033         # be released during reconciliation
1034         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1035                 error "consume space failure, expect success"
1036
1037         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1038         lustre_fail mds 0xa02
1039
1040         set_ost_qtype "ug" || error "enable ost quota failed"
1041         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1042                 error "set quota failed"
1043
1044         # ignore the write error
1045         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1046
1047         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1048
1049         lustre_fail mds 0
1050
1051         echo "Restart ost to trigger reintegration..."
1052         stop ost1
1053         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
1054         quota_init
1055
1056         wait_ost_reint "ug" || error "reintegration failed"
1057
1058         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1059         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1060
1061         cleanup_quota_test
1062         resetquota -u $TSTUSR
1063         $SHOW_QUOTA_USER
1064 }
1065 run_test 7b "Quota reintegration (slave index)"
1066
1067 # quota reintegration (restart mds during reintegration)
1068 test_7c() {
1069         local LIMIT=20 # 20M
1070         local TESTFILE=$DIR/$tdir/$tfile
1071
1072         [ "$SLOW" = "no" ] && LIMIT=5
1073
1074         setup_quota_test
1075         trap cleanup_quota_test EXIT
1076
1077         # make sure the system is clean
1078         local USED=$(getquota -u $TSTUSR global curspace)
1079         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1080
1081         set_ost_qtype "none" || error "disable ost quota failed"
1082         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1083                 error "set quota failed"
1084
1085         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1086         lustre_fail ost 0xa03
1087
1088         # enable ost quota to trigger reintegration
1089         set_ost_qtype "ug" || error "enable ost quota failed"
1090
1091         echo "Stop mds..."
1092         stop mds1
1093
1094         lustre_fail ost 0
1095
1096         echo "Start mds..."
1097         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1098         quota_init
1099
1100         wait_ost_reint "ug" || error "reintegration failed"
1101
1102         # hardlimit should have been fetched by slave during global
1103         # reintegration, write will exceed quota
1104         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1105                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1106
1107         cleanup_quota_test
1108         resetquota -u $TSTUSR
1109 }
1110 run_test 7c "Quota reintegration (restart mds during reintegration)"
1111
1112 # Quota reintegration (Transfer index in multiple bulks)
1113 test_7d(){
1114         local TESTFILE=$DIR/$tdir/$tfile
1115         local TESTFILE1="$DIR/$tdir/$tfile"-1
1116         local limit=20 #20M
1117
1118         setup_quota_test
1119         trap cleanup_quota_test EXIT
1120
1121         set_ost_qtype "none" || error "disable ost quota failed"
1122         # LU-2284. Enable trace for debug log.
1123         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+trace"
1124         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1125                 error "set quota for $TSTUSR failed"
1126         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1127                 error "set quota for $TSTUSR2 failed"
1128         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-trace"
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