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