Whamcloud - gitweb
LU-2012 tests: disable replay-dual test_14b
[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         local LIMIT=3 # 3M
806
807         setup_quota_test
808         trap cleanup_quota_test EXIT
809
810         # make sure the system is clean
811         local USED=$(getquota -u $TSTUSR global curspace)
812         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
813
814         # make sure no granted quota on ost
815         set_ost_qtype "ug" || error "enable ost quota failed"
816         resetquota -u $TSTUSR
817
818         # create file for $TSTUSR
819         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
820         $LFS setstripe $TESTFILE -c 1 -i 0
821         chown $TSTUSR.$TSTUSR $TESTFILE
822
823         # create file for $TSTUSR2
824         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
825         $LFS setstripe $TESTFILE2 -c 1 -i 0
826         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
827
828         # cache per-ID lock for $TSTUSR on slave
829         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
830         $RUNAS $DD of=$TESTFILE count=1 ||
831                 error "write $TESTFILE failure, expect success"
832         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
833                 error "write $TESTFILE2 failure, expect success"
834         sync; sync
835         sync_all_data || true
836
837         #define QUOTA_DQACQ 601
838         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
839         lustre_fail mds 0x513 601
840
841         # write to un-enforced ID ($TSTUSR2) should succeed
842         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
843                 error "write failure, expect success"
844
845         # write to enforced ID ($TSTUSR) in background, exceeding limit
846         # to make sure DQACQ is sent
847         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
848         DDPID=$!
849
850         echo "Sleep for $TIMEOUT"
851         sleep $TIMEOUT
852
853         # write should be blocked and never finished
854         if ! ps -p $DDPID  > /dev/null 2>&1; then
855                 lustre_fail mds 0 0
856                 error "write finished incorrectly!"
857         fi
858
859         lustre_fail mds 0 0
860
861         # no watchdog is triggered
862         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
863         watchdog=$(awk '/sanity-quota test 6/ {start = 1;}
864                        /Service thread pid/ && /was inactive/ {
865                                if (start) {
866                                        print;
867                                }
868                        }' $TMP/lustre-log-${TESTNAME}.log)
869         [ -z "$watchdog" ] || error "$watchdog"
870
871         rm -f $TMP/lustre-log-${TESTNAME}.log
872
873         # write should continue & succeed
874         local count=0
875         while [ true ]; do
876                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
877                 if [ $count -ge 120 ]; then
878                         quota_error u $TSTUSR "dd not finished in $count secs"
879                 fi
880                 count=$((count + 1))
881                 [ $((count % 10)) -eq 0 ] && echo "Waiting $count secs"
882                 sleep 1
883         done
884
885         cleanup_quota_test
886         resetquota -u $TSTUSR
887 }
888 run_test 6 "Test dropping acquire request on master"
889
890 # quota reintegration (global index)
891 test_7a() {
892         local TESTFILE=$DIR/$tdir/$tfile
893         local LIMIT=20 # 20M
894
895         [ "$SLOW" = "no" ] && LIMIT=5
896
897         setup_quota_test
898         trap cleanup_quota_test EXIT
899
900         # make sure the system is clean
901         local USED=$(getquota -u $TSTUSR global curspace)
902         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
903
904         # make sure no granted quota on ost1
905         set_ost_qtype "ug" || error "enable ost quota failed"
906         resetquota -u $TSTUSR
907         set_ost_qtype "none" || error "disable ost quota failed"
908
909         local OSTUUID=$(ostuuid_from_index 0)
910         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
911         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
912                 "$TSTUSR isn't 0."
913
914         # create test file
915         $LFS setstripe $TESTFILE -c 1 -i 0
916         chown $TSTUSR.$TSTUSR $TESTFILE
917
918         echo "Stop ost1..."
919         stop ost1
920
921         echo "Enable quota & set quota limit for $TSTUSR"
922         set_ost_qtype "ug" || error "enable ost quota failed"
923         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
924
925         echo "Start ost1..."
926         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
927
928         wait_ost_reint "ug" || error "reintegration failed"
929
930         # hardlimit should have been fetched by slave during global
931         # reintegration, write will exceed quota
932         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
933                 quota_error u $TSTUSR "write success, but expect EDQUOT"
934
935         cleanup_quota_test
936         resetquota -u $TSTUSR
937 }
938 run_test 7a "Quota reintegration (global index)"
939
940 # quota reintegration (slave index)
941 test_7b() {
942         local LIMIT="100G"
943         local TESTFILE=$DIR/$tdir/$tfile
944
945         setup_quota_test
946         trap cleanup_quota_test EXIT
947
948         # make sure the system is clean
949         local USED=$(getquota -u $TSTUSR global curspace)
950         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
951
952         # make sure no granted quota on ost1
953         set_ost_qtype "ug" || error "enable ost quota failed"
954         resetquota -u $TSTUSR
955         set_ost_qtype "none" || error "disable ost quota failed"
956
957         local OSTUUID=$(ostuuid_from_index 0)
958         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
959         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
960                 "$TSTUSR isn't 0."
961
962         # create test file
963         $LFS setstripe $TESTFILE -c 1 -i 0
964         chown $TSTUSR.$TSTUSR $TESTFILE
965
966         # consume some space to make sure the granted space will not
967         # be released during reconciliation
968         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
969                 error "consume space failure, expect success"
970
971         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
972         lustre_fail mds 0xa02
973
974         set_ost_qtype "ug" || error "enable ost quota failed"
975         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
976
977         # ignore the write error
978         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
979
980         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
981
982         lustre_fail mds 0
983
984         echo "Restart ost to trigger reintegration..."
985         stop ost1
986         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
987
988         wait_ost_reint "ug" || error "reintegration failed"
989
990         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
991         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
992
993         cleanup_quota_test
994         resetquota -u $TSTUSR
995         $SHOW_QUOTA_USER
996 }
997 run_test 7b "Quota reintegration (slave index)"
998
999 # quota reintegration (restart mds during reintegration)
1000 test_7c() {
1001         local LIMIT=20 # 20M
1002         local TESTFILE=$DIR/$tdir/$tfile
1003
1004         [ "$SLOW" = "no" ] && LIMIT=5
1005
1006         setup_quota_test
1007         trap cleanup_quota_test EXIT
1008
1009         # make sure the system is clean
1010         local USED=$(getquota -u $TSTUSR global curspace)
1011         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1012
1013         set_ost_qtype "none" || error "disable ost quota failed"
1014         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
1015
1016         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1017         lustre_fail ost 0xa03
1018
1019         # enable ost quota to trigger reintegration
1020         set_ost_qtype "ug" || error "enable ost quota failed"
1021
1022         echo "Stop mds..."
1023         stop mds1
1024
1025         lustre_fail ost 0
1026
1027         echo "Start mds..."
1028         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1029
1030         wait_ost_reint "ug" || error "reintegration failed"
1031
1032         # hardlimit should have been fetched by slave during global
1033         # reintegration, write will exceed quota
1034         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1035                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1036
1037         cleanup_quota_test
1038         resetquota -u $TSTUSR
1039 }
1040 run_test 7c "Quota reintegration (restart mds during reintegration)"
1041
1042 # run dbench with quota enabled
1043 test_8() {
1044         local BLK_LIMIT="100g" #100G
1045         local FILE_LIMIT=1000000
1046
1047         setup_quota_test
1048         trap cleanup_quota_test EXIT
1049
1050         set_mdt_qtype "ug" || error "enable mdt quota failed"
1051         set_ost_qtype "ug" || error "enable ost quota failed"
1052
1053         echo "Set enough high limit for user: $TSTUSR"
1054         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1055         echo "Set enough high limit for group: $TSTUSR"
1056         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1057
1058         local duration=""
1059         [ "$SLOW" = "no" ] && duration=" -t 120"
1060         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1061                 quota_error a $TSTUSR "dbench failed!"
1062
1063         cleanup_quota_test
1064         resetquota -u $TSTUSR
1065         resetquota -g $TSTUSR
1066 }
1067 run_test 8 "Run dbench with quota enabled"
1068
1069 # this check is just for test_9
1070 OST0_MIN=4900000 #4.67G
1071
1072 check_whether_skip () {
1073         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1074         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1075         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1076                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1077                 return 0
1078         else
1079                 return 1
1080         fi
1081 }
1082
1083 # run for fixing bug10707, it needs a big room. test for 64bit
1084 test_9() {
1085         local filesize=$((1024 * 1024 * 1024 * 9 / 2)) # 4.5G
1086
1087         check_whether_skip && return 0
1088
1089         setup_quota_test
1090         trap cleanup_quota_test EXIT
1091
1092         set_ost_qtype "ug" || error "enable ost quota failed"
1093
1094         local TESTFILE="$DIR/$tdir/$tfile-0"
1095         local BLK_LIMIT=100G #100G
1096         local FILE_LIMIT=1000000
1097
1098         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1099
1100         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1101                 "for user: $TSTUSR"
1102         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1103
1104         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1105                 "for group: $TSTUSR"
1106         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1107
1108         quota_show_check a u $TSTUSR
1109         quota_show_check a g $TSTUSR
1110
1111         echo "Create test file"
1112         $LFS setstripe $TESTFILE -c 1 -i 0
1113         chown $TSTUSR.$TSTUSR $TESTFILE
1114
1115         log "Write the big file of 4.5G ..."
1116         $RUNAS $DD of=$TESTFILE count=$filesize ||
1117                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1118
1119         $SHOW_QUOTA_USER
1120         $SHOW_QUOTA_GROUP
1121
1122         cleanup_quota_test
1123         resetquota -u $TSTUSR
1124         resetquota -g $TSTUSR
1125
1126         $SHOW_QUOTA_USER
1127         $SHOW_QUOTA_GROUP
1128 }
1129 run_test 9 "Block limit larger than 4GB (b10707)"
1130
1131 test_10() {
1132         local TESTFILE=$DIR/$tdir/$tfile
1133
1134         setup_quota_test
1135         trap cleanup_quota_test EXIT
1136
1137         # set limit to root user should fail
1138         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1139                 error "set limit for root user successfully, expect failure"
1140         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1141                 error "set limit for root group successfully, expect failure"
1142
1143         # root user can overrun quota
1144         set_ost_qtype "ug" || "enable ost quota failed"
1145
1146         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR
1147         quota_show_check b u $TSTUSR
1148
1149         $LFS setstripe $TESTFILE -c 1
1150         chown $TSTUSR.$TSTUSR $TESTFILE
1151
1152         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1153                 error "write failure, expect success"
1154
1155         cleanup_quota_test
1156         resetquota -u $TSTUSR
1157 }
1158 run_test 10 "Test quota for root user"
1159
1160 test_11() {
1161         local TESTFILE=$DIR/$tdir/$tfile
1162         setup_quota_test
1163         trap cleanup_quota_test EXIT
1164
1165         set_mdt_qtype "ug" || "enable mdt quota failed"
1166         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR
1167
1168         touch "$TESTFILE"-0
1169         touch "$TESTFILE"-1
1170
1171         chown $TSTUSR.$TSTUSR "$TESTFILE"-0
1172         chown $TSTUSR.$TSTUSR "$TESTFILE"-1
1173
1174         $SHOW_QUOTA_USER
1175         local USED=$(getquota -u $TSTUSR global curinodes)
1176         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1177
1178         cleanup_quota_test
1179         resetquota -u $TSTUSR
1180 }
1181 run_test 11 "Chown/chgrp ignores quota"
1182
1183 test_12() {
1184         [ "$OSTCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1185
1186         local blimit=22 # 22M
1187         local blk_cnt=$((blimit - 5))
1188         local TESTFILE0="$DIR/$tdir/$tfile"-0
1189         local TESTFILE1="$DIR/$tdir/$tfile"-1
1190
1191         setup_quota_test
1192         trap cleanup_quota_test EXIT
1193
1194         set_ost_qtype "u" || "enable ost quota failed"
1195         quota_show_check b u $TSTUSR
1196
1197         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR
1198
1199         $LFS setstripe $TESTFILE0 -c 1 -i 0
1200         $LFS setstripe $TESTFILE1 -c 1 -i 1
1201         chown $TSTUSR.$TSTUSR $TESTFILE0
1202         chown $TSTUSR.$TSTUSR $TESTFILE1
1203
1204         echo "Write to ost0..."
1205         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1206                 quota_error a $TSTUSR "dd failed"
1207
1208         echo "Write to ost1..."
1209         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1210                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1211
1212         echo "Free space from ost0..."
1213         rm -f $TESTFILE0
1214         wait_delete_completed
1215         sync_all_data || true
1216
1217         echo "Write to ost1 after space freed from ost0..."
1218         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1219                 quota_error a $TSTUSR "rebalancing failed"
1220
1221         cleanup_quota_test
1222         resetquota -u $TSTUSR
1223 }
1224 run_test 12 "Block quota rebalancing"
1225
1226 test_13(){
1227         local TESTFILE=$DIR/$tdir/$tfile
1228         # the name of osp on ost1 name is MDT0000-osp-OST0000
1229         local procf="ldlm.namespaces.*MDT0000-osp-OST0000.lru_size"
1230
1231         setup_quota_test
1232         trap cleanup_quota_test EXIT
1233
1234         set_ost_qtype "u" || "enable ost quota failed"
1235         quota_show_check b u $TSTUSR
1236
1237         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR
1238         $LFS setstripe $TESTFILE -c 1 -i 0
1239         chown $TSTUSR.$TSTUSR $TESTFILE
1240
1241         # clear the locks in cache first
1242         do_facet ost1 $LCTL set_param -n $procf=clear
1243         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1244         [ $nlock -eq 0 ] || error "$nlock cached locks"
1245
1246         # write to acquire the per-ID lock
1247         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1248                 quota_error a $TSTUSR "dd failed"
1249
1250         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1251         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1252
1253         # clear quota doesn't trigger per-ID lock cancellation
1254         resetquota -u $TSTUSR
1255         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1256         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1257
1258         # clear the per-ID lock
1259         do_facet ost1 $LCTL set_param -n $procf=clear
1260         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1261         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1262
1263         # spare quota should be released
1264         local OSTUUID=$(ostuuid_from_index 0)
1265         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1266         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1267         [ $limit -le $space ] ||
1268                 error "spare quota isn't released, limit:$limit, space:$space"
1269
1270         cleanup_quota_test
1271 }
1272 run_test 13 "Cancel per-ID lock in the LRU list"
1273
1274 test_15(){
1275         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1276
1277         wait_delete_completed
1278         sync_all_data || true
1279
1280         # test for user
1281         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1282         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1283         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1284                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1285         resetquota -u $TSTUSR
1286
1287         # test for group
1288         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1289         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1290         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1291                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1292         resetquota -g $TSTUSR
1293 }
1294 run_test 15 "Set over 4T block quota"
1295
1296 test_17sub() {
1297         local err_code=$1
1298         local BLKS=1    # 1M less than limit
1299         local TESTFILE=$DIR/$tdir/$tfile
1300
1301         setup_quota_test
1302         trap cleanup_quota_test EXIT
1303
1304         # make sure the system is clean
1305         local USED=$(getquota -u $TSTUSR global curspace)
1306         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1307
1308         set_ost_qtype "ug" || error "enable ost quota failed"
1309         # make sure no granted quota on ost
1310         resetquota -u $TSTUSR
1311         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR
1312
1313         quota_show_check b u $TSTUSR
1314
1315         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1316         lustre_fail mds 0xa04 $err_code
1317
1318         # write in background
1319         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1320         local DDPID=$!
1321
1322         sleep 2
1323         # write should be blocked and never finished
1324         if ! ps -p $DDPID  > /dev/null 2>&1; then
1325                 lustre_fail mds 0 0
1326                 quota_error u $TSTUSR "write finished incorrectly!"
1327         fi
1328
1329         lustre_fail mds 0 0
1330
1331         local count=0
1332         local timeout=30
1333         while [ true ]; do
1334                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1335                 count=$((count+1))
1336                 if [ $count -gt $timeout ]; then
1337                         quota_error u $TSTUSR "dd is not finished!"
1338                 fi
1339                 sleep 1
1340         done
1341
1342         sync; sync_all_data || true
1343
1344         USED=$(getquota -u $TSTUSR global curspace)
1345         [ $USED -ge $(($BLKS * 1024)) ] || quota_error u $TSTUSR \
1346                 "Used space(${USED}K) is less than ${BLKS}M"
1347
1348         cleanup_quota_test
1349         resetquota -u $TSTUSR
1350 }
1351
1352 # DQACQ return recoverable error
1353 test_17() {
1354         echo "DQACQ return -ENOLCK"
1355         #define ENOLCK  37
1356         test_17sub 37 || error "Handle -ENOLCK failed"
1357
1358         echo "DQACQ return -EAGAIN"
1359         #define EAGAIN  11
1360         test_17sub 11 || error "Handle -EAGAIN failed"
1361
1362         echo "DQACQ return -ETIMEDOUT"
1363         #define ETIMEDOUT 110
1364         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1365
1366         echo "DQACQ return -ENOTCONN"
1367         #define ENOTCONN 107
1368         test_17sub 107 || error "Handle -ENOTCONN failed"
1369 }
1370
1371 run_test 17 "DQACQ return recoverable error"
1372
1373 test_18_sub () {
1374         local io_type=$1
1375         local blimit="200m" # 200M
1376         local TESTFILE="$DIR/$tdir/$tfile"
1377
1378         setup_quota_test
1379         trap cleanup_quota_test EXIT
1380
1381         set_ost_qtype "u" || error "enable ost quota failed"
1382         log "User quota (limit: $blimit)"
1383         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT
1384         quota_show_check b u $TSTUSR
1385
1386         $LFS setstripe $TESTFILE -i 0 -c 1
1387         chown $TSTUSR.$TSTUSR $TESTFILE
1388
1389         local timeout=$(sysctl -n lustre.timeout)
1390
1391         if [ $io_type = "directio" ]; then
1392                 log "Write 100M (directio) ..."
1393                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1394         else
1395                 log "Write 100M (buffered) ..."
1396                 $RUNAS $DD of=$TESTFILE count=100 &
1397         fi
1398         local DDPID=$!
1399
1400         replay_barrier $SINGLEMDS
1401         log "Fail mds for $((2 * timeout)) seconds"
1402         fail $SINGLEMDS $((2 * timeout))
1403
1404         local count=0
1405         if at_is_enabled; then
1406                 timeout=$(at_max_get mds)
1407         else
1408                 timeout=$(lctl get_param -n timeout)
1409         fi
1410
1411         while [ true ]; do
1412                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1413                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1414                         log "it took $count second"
1415                 fi
1416                 sleep 1
1417         done
1418
1419         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1420         sync
1421         cancel_lru_locks mdc
1422         cancel_lru_locks osc
1423         $SHOW_QUOTA_USER
1424
1425         local testfile_size=$(stat -c %s $TESTFILE)
1426         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1427                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)),"
1428                         "got ${testfile_size}. Verifying file failed!"
1429         fi
1430         cleanup_quota_test
1431         resetquota -u $TSTUSR
1432 }
1433
1434 # test when mds does failover, the ost still could work well
1435 # this test shouldn't trigger watchdog b=14840
1436 test_18() {
1437         test_18_sub normal
1438         test_18_sub directio
1439
1440         # check if watchdog is triggered
1441         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1442         local watchdog=$(awk '/test 18b/ {start = 1;}
1443                        /Service thread pid/ && /was inactive/ {
1444                                if (start) {
1445                                        print;
1446                                }
1447                        }' $TMP/lustre-log-${TESTNAME}.log)
1448         [ -z "$watchdog" ] || error "$watchdog"
1449         rm -f $TMP/lustre-log-${TESTNAME}.log
1450 }
1451 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1452
1453 test_19() {
1454         local blimit=5 # 5M
1455         local TESTFILE=$DIR/$tdir/$tfile
1456
1457         setup_quota_test
1458         trap cleanup_quota_test EXIT
1459
1460         set_ost_qtype "ug" || error "enable ost quota failed"
1461
1462         # bind file to a single OST
1463         $LFS setstripe -c 1 $TESTFILE
1464         chown $TSTUSR.$TSTUSR $TESTFILE
1465
1466         echo "Set user quota (limit: "$blimit"M)"
1467         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1468         quota_show_check b u $TSTUSR
1469         echo "Update quota limits"
1470         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1471         quota_show_check b u $TSTUSR
1472
1473         # first wirte might be cached
1474         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
1475         cancel_lru_locks osc
1476         $SHOW_QUOTA_USER
1477         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
1478                 quota_error u $TSTUSR "Write success, expect failure"
1479         $SHOW_QUOTA_USER
1480
1481         cleanup_quota_test
1482         resetquota -u $TSTUSR
1483 }
1484 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
1485
1486 test_20() { # b15754
1487         local LSTR=(2g 1t 4k 3m) # limits strings
1488         # limits values
1489         local LVAL=($[2*1024*1024] $[1*1024*1024*1024] $[4*1024] $[3*1024*1024])
1490
1491         resetquota -u $TSTUSR
1492
1493         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1494                 $MOUNT || error "could not set quota limits"
1495         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1496                                 --inode-softlimit ${LSTR[2]} \
1497                                 --inode-hardlimit ${LSTR[3]} \
1498                                 $MOUNT || error "could not set quota limits"
1499
1500         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
1501                 error "bsoftlimit was not set properly"
1502         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
1503                 error "bhardlimit was not set properly"
1504         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
1505                 error "isoftlimit was not set properly"
1506         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
1507                 error "ihardlimit was not set properly"
1508
1509         resetquota -u $TSTUSR
1510 }
1511 run_test 20 "Test if setquota specifiers work properly (b15754)"
1512
1513 test_21_sub() {
1514         local testfile=$1
1515         local blk_number=$2
1516         local seconds=$3
1517
1518         local time=$(($(date +%s) + seconds))
1519         while [ $(date +%s) -lt $time ]; do
1520                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
1521         done
1522 }
1523
1524 # run for fixing bug16053, setquota shouldn't fail when writing and
1525 # deleting are happening
1526 test_21() {
1527         local TESTFILE="$DIR/$tdir/$tfile"
1528         local BLIMIT=10 # 10G
1529         local ILIMIT=1000000
1530
1531         setup_quota_test
1532         trap cleanup_quota_test EXIT
1533
1534         set_ost_qtype "ug" || error "Enable ost quota failed"
1535
1536         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
1537         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT
1538         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
1539         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT
1540
1541         # repeat writing on a 1M file
1542         test_21_sub ${TESTFILE}_1 1 30 &
1543         local DDPID1=$!
1544         # repeat writing on a 128M file
1545         test_21_sub ${TESTFILE}_2 128 30 &
1546         local DDPID2=$!
1547
1548         local time=$(($(date +%s) + 30))
1549         local i=1
1550         while [ $(date +%s) -lt $time ]; do
1551                 log "Set quota for $i times"
1552                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1553                         -I $((ILIMIT + i)) $MOUNT ||
1554                                 error "Set user quota failed"
1555                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1556                         -I $((ILIMIT + i)) $MOUNT ||
1557                                 error "Set group quota failed"
1558                 i=$((i+1))
1559                 sleep 1
1560         done
1561
1562         local count=0
1563         while [ true ]; do
1564                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1565                 count=$((count+1))
1566                 if [ $count -gt 60 ]; then
1567                         quota_error a $TSTUSR "dd should be finished!"
1568                 fi
1569                 sleep 1
1570         done
1571         echo "(dd_pid=$DDPID1, time=$count)successful"
1572
1573         count=0
1574         while [ true ]; do
1575                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1576                 count=$((count+1))
1577                 if [ $count -gt 60 ]; then
1578                         quota_error a $TSTUSR "dd should be finished!"
1579                 fi
1580                 sleep 1
1581         done
1582         echo "(dd_pid=$DDPID2, time=$count)successful"
1583
1584         cleanup_quota_test
1585         resetquota -u $TSTUSR
1586         resetquota -g $TSTUSR
1587 }
1588 run_test 21 "Setquota while writing & deleting (b16053)"
1589
1590 # enable/disable quota enforcement permanently
1591 test_22() {
1592         echo "Set both mdt & ost quota type as ug"
1593         set_mdt_qtype "ug" || error "enable mdt quota failed"
1594         set_ost_qtype "ug" || error "enable ost quota failed"
1595
1596         echo "Restart..."
1597         stopall
1598         mount
1599         setupall
1600
1601         echo "Verify if quota is enabled"
1602         local qtype=$(mdt_quota_type)
1603         [ $qtype != "ug" ] && error "mdt quota setting is lost"
1604         qtype=$(ost_quota_type)
1605         [ $qtype != "ug" ] && error "ost quota setting is lost"
1606
1607         echo "Set both mdt & ost quota type as none"
1608         set_mdt_qtype "none" || error "disable mdt quota failed"
1609         set_ost_qtype "none" || error "disable ost quota failed"
1610
1611         echo "Restart..."
1612         stopall
1613         mount
1614         setupall
1615
1616         echo "Verify if quota is disabled"
1617         qtype=$(mdt_quota_type)
1618         [ $qtype != "none" ] && error "mdt quota setting is lost"
1619         qtype=$(ost_quota_type)
1620         [ $qtype != "none" ] && error "ost quota setting is lost"
1621
1622         quota_init
1623 }
1624 run_test 22 "enable/disable quota by 'lctl conf_param'"
1625
1626 test_23_sub() {
1627         local TESTFILE="$DIR/$tdir/$tfile"
1628         local LIMIT=$1
1629
1630         setup_quota_test
1631         trap cleanup_quota_test EXIT
1632
1633         set_ost_qtype "ug" || error "Enable ost quota failed"
1634
1635         # test for user
1636         log "User quota (limit: $LIMIT MB)"
1637         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR
1638         quota_show_check b u $TSTUSR
1639
1640         $LFS setstripe $TESTFILE -c 1 -i 0
1641         chown $TSTUSR.$TSTUSR $TESTFILE
1642
1643         log "Step1: trigger EDQUOT with O_DIRECT"
1644         log "Write half of file"
1645         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
1646                 quota_error u $TSTUSR "(1) Write failure, expect success." \
1647                         "limit=$LIMIT"
1648         log "Write out of block quota ..."
1649         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
1650                 oflag=direct conv=notrunc &&
1651                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
1652                         "limit=$LIMIT"
1653         log "Step1: done"
1654
1655         log "Step2: rewrite should succeed"
1656         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
1657                 quota_error u $TSTUSR "(3) Write failure, expect success." \
1658                         "limit=$LIMIT"
1659         log "Step2: done"
1660
1661         cleanup_quota_test
1662
1663         local OST0_UUID=$(ostuuid_from_index 0)
1664         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
1665         [ $OST0_QUOTA_USED -ne 0 ] &&
1666                 ($SHOW_QUOTA_USER; \
1667                 quota_error u $TSTUSR "quota isn't released")
1668         $SHOW_QUOTA_USER
1669         resetquota -u $TSTUSR
1670 }
1671
1672 test_23() {
1673         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
1674         check_whether_skip && return 0
1675         log "run for 4MB test file"
1676         test_23_sub 4
1677
1678         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
1679         check_whether_skip && return 0
1680         log "run for 40MB test file"
1681         test_23_sub 40
1682 }
1683 run_test 23 "Quota should be honored with directIO (b16125)"
1684
1685 test_24() {
1686         local blimit=5 # 5M
1687         local TESTFILE="$DIR/$tdir/$tfile"
1688
1689         setup_quota_test
1690         trap cleanup_quota_test EXIT
1691
1692         set_ost_qtype "ug" || error "enable ost quota failed"
1693
1694         # bind file to a single OST
1695         $LFS setstripe -c 1 $TESTFILE
1696         chown $TSTUSR.$TSTUSR $TESTFILE
1697
1698         echo "Set user quota (limit: "$blimit"M)"
1699         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1700
1701         # overrun quota by root user
1702         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
1703                 error "write failure, expect success"
1704         cancel_lru_locks osc
1705         sync_all_data || true
1706
1707         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1708
1709         cleanup_quota_test
1710         resetquota -u $TSTUSR
1711 }
1712 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
1713
1714 test_27a() { # b19612
1715         $LFS quota $TSTUSR $DIR &&
1716                 error "lfs succeeded with no type, but should have failed"
1717         $LFS setquota $TSTUSR $DIR &&
1718                 error "lfs succeeded with no type, but should have failed"
1719         return 0
1720 }
1721 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
1722
1723 test_27b() { # b20200
1724         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1725                 error "lfs setquota failed with uid argument"
1726         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1727                 error "lfs stequota failed with gid argument"
1728         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
1729         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
1730         resetquota -u $TSTUSR
1731         resetquota -g $TSTUSR
1732         return 0
1733 }
1734 run_test 27b "lfs quota/setquota should handle user/group ID (b20200)"
1735
1736 test_30() {
1737         local output
1738         local LIMIT=4 # 4MB
1739         local TESTFILE="$DIR/$tdir/$tfile"
1740         local GRACE=10
1741
1742         setup_quota_test
1743         trap cleanup_quota_test EXIT
1744
1745         set_ost_qtype "u" || error "enable ost quota failed"
1746
1747         $LFS setstripe $TESTFILE -i 0 -c 1
1748         chown $TSTUSR.$TSTUSR $TESTFILE
1749
1750         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
1751         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR
1752         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
1753         cancel_lru_locks osc
1754         sleep $GRACE
1755         $LFS setquota -u $TSTUSR -B 0 $DIR
1756         # over-quota flag has not yet settled since we do not trigger async
1757         # events based on grace time period expiration
1758         $SHOW_QUOTA_USER
1759         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 || true
1760         cancel_lru_locks osc
1761         # now over-quota flag should be settled and further writes should fail
1762         $SHOW_QUOTA_USER
1763         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 &&
1764                 error "grace times were reset"
1765         # cleanup
1766         cleanup_quota_test
1767         resetquota -u $TSTUSR
1768         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1769                 $MAX_IQ_TIME $DIR
1770 }
1771 run_test 30 "Hard limit updates should not reset grace times"
1772
1773 # basic usage tracking for user & group
1774 test_33() {
1775         local INODES=10 # 10 files
1776         local BLK_CNT=2 # of 2M each
1777         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
1778
1779         setup_quota_test
1780         trap cleanup_quota_test EXIT
1781
1782         # make sure the system is clean
1783         local USED=$(getquota -u $TSTID global curspace)
1784         [ $USED -ne 0 ] &&
1785                 error "Used space ($USED) for user $TSTID isn't 0."
1786         USED=$(getquota -g $TSTID global curspace)
1787         [ $USED -ne 0 ] &&
1788                 error "Used space ($USED) for group $TSTID isn't 0."
1789
1790         echo "Write files..."
1791         for i in `seq 0 $INODES`; do
1792                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
1793                         error "write failed"
1794                 echo "Iteration $i/$INODES completed"
1795         done
1796         cancel_lru_locks osc
1797         sync; sync_all_data || true
1798
1799         echo "Verify disk usage after write"
1800         USED=$(getquota -u $TSTID global curspace)
1801         [ $USED -lt $TOTAL_BLKS ] &&
1802                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
1803         USED=$(getquota -g $TSTID global curspace)
1804         [ $USED -lt $TOTAL_BLKS ] &&
1805                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
1806
1807         echo "Verify inode usage after write"
1808         USED=$(getquota -u $TSTID global curinodes)
1809         [ $USED -lt $INODES ] &&
1810                 error "Used inode for user $TSTID is $USED, expected $INODES"
1811         USED=$(getquota -g $TSTID global curinodes)
1812         [ $USED -lt $INODES ] &&
1813                 error "Used inode for group $TSTID is $USED, expected $INODES"
1814
1815         cleanup_quota_test
1816
1817         echo "Verify disk usage after delete"
1818         USED=$(getquota -u $TSTID global curspace)
1819         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
1820         USED=$(getquota -u $TSTID global curinodes)
1821         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
1822         USED=$(getquota -g $TSTID global curspace)
1823         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
1824         USED=$(getquota -g $TSTID global curinodes)
1825         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
1826 }
1827 run_test 33 "Basic usage tracking for user & group"
1828
1829 # usage transfer test for user & group
1830 test_34() {
1831         local BLK_CNT=2 # 2MB
1832
1833         setup_quota_test
1834         trap cleanup_quota_test EXIT
1835
1836         # make sure the system is clean
1837         local USED=$(getquota -u $TSTID global curspace)
1838         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
1839         USED=$(getquota -g $TSTID global curspace)
1840         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
1841
1842         echo "Write file..."
1843         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
1844                 error "write failed"
1845         cancel_lru_locks osc
1846         sync; sync_all_data || true
1847
1848         echo "chown the file to user $TSTID"
1849         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
1850
1851         echo "Wait for setattr on objects finished..."
1852         wait_delete_completed
1853
1854         BLK_CNT=$((BLK_CNT * 1024))
1855
1856         echo "Verify disk usage for user $TSTID"
1857         USED=$(getquota -u $TSTID global curspace)
1858         [ $USED -lt $BLK_CNT ] &&
1859                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
1860         USED=$(getquota -u $TSTID global curinodes)
1861         [ $USED -ne 1 ] &&
1862                 error "Used inodes for user $TSTID is $USED, expected 1"
1863
1864         echo "chgrp the file to group $TSTID"
1865         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
1866
1867         echo "Wait for setattr on objects finished..."
1868         wait_delete_completed
1869
1870         echo "Verify disk usage for group $TSTID"
1871         USED=$(getquota -g $TSTID global curspace)
1872         [ $USED -ge $BLK_CNT ] ||
1873                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
1874         USED=$(getquota -g $TSTID global curinodes)
1875         [ $USED -eq 1 ] ||
1876                 error "Used inodes for group $TSTID is $USED, expected 1"
1877
1878         cleanup_quota_test
1879 }
1880 run_test 34 "Usage transfer for user & group"
1881
1882 # usage is still accessible across restart
1883 test_35() {
1884         local BLK_CNT=2 # 2 MB
1885
1886         setup_quota_test
1887         trap cleanup_quota_test EXIT
1888
1889         echo "Write file..."
1890         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
1891                 error "write failed"
1892         cancel_lru_locks osc
1893         sync; sync_all_data || true
1894
1895         echo "Save disk usage before restart"
1896         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
1897         [ $ORIG_USR_SPACE -eq 0 ] &&
1898                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
1899         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
1900         [ $ORIG_USR_INODES -eq 0 ] &&
1901                 error "Used inodes for user $TSTID is 0, expected 1"
1902         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
1903         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
1904         [ $ORIG_GRP_SPACE -eq 0 ] &&
1905                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
1906         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
1907         [ $ORIG_GRP_INODES -eq 0 ] &&
1908                 error "Used inodes for group $TSTID is 0, expected 1"
1909         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
1910
1911         log "Restart..."
1912         local ORIG_REFORMAT=$REFORMAT
1913         REFORMAT=""
1914         cleanup_and_setup_lustre
1915         REFORMAT=$ORIG_REFORMAT
1916         quota_init
1917
1918         echo "Verify disk usage after restart"
1919         local USED=$(getquota -u $TSTID global curspace)
1920         [ $USED -eq $ORIG_USR_SPACE ] ||
1921                 error "Used space for user $TSTID changed from " \
1922                         "$ORIG_USR_SPACE to $USED"
1923         USED=$(getquota -u $TSTID global curinodes)
1924         [ $USED -eq $ORIG_USR_INODES ] ||
1925                 error "Used inodes for user $TSTID changed from " \
1926                         "$ORIG_USR_INODES to $USED"
1927         USED=$(getquota -g $TSTID global curspace)
1928         [ $USED -eq $ORIG_GRP_SPACE ] ||
1929                 error "Used space for group $TSTID changed from " \
1930                         "$ORIG_GRP_SPACE to $USED"
1931         USED=$(getquota -g $TSTID global curinodes)
1932         [ $USED -eq $ORIG_GRP_INODES ] ||
1933                 error "Used inodes for group $TSTID changed from " \
1934                         "$ORIG_GRP_INODES to $USED"
1935
1936         # check if the vfs_dq_init() is called before writing
1937         echo "Append to the same file..."
1938         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
1939                 error "write failed"
1940         cancel_lru_locks osc
1941         sync; sync_all_data || true
1942
1943         echo "Verify space usage is increased"
1944         USED=$(getquota -u $TSTID global curspace)
1945         [ $USED -gt $ORIG_USR_SPACE ] ||
1946                 error "Used space for user $TSTID isn't increased" \
1947                         "orig:$ORIG_USR_SPACE, now:$USED"
1948         USED=$(getquota -g $TSTID global curspace)
1949         [ $USED -gt $ORIG_GRP_SPACE ] ||
1950                 error "Used space for group $TSTID isn't increased" \
1951                         "orig:$ORIG_GRP_SPACE, now:$USED"
1952
1953         cleanup_quota_test
1954 }
1955 run_test 35 "Usage is still accessible across reboot"
1956
1957 # test migrating old amdin quota files (in Linux quota file format v2) into new
1958 # quota global index (in IAM format)
1959 test_36() {
1960         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] && \
1961                 skip "skipping migration test" && return
1962
1963         # get the mdt0 device name
1964         local mdt0_node=$(facet_active_host $SINGLEMDS)
1965         local mdt0_dev=$(mdsdevname ${SINGLEMDS//mds/})
1966
1967         echo "Reformat..."
1968         formatall
1969
1970         echo "Copy admin quota files into MDT0..."
1971         local mntpt=$(facet_mntpt $SINGLEMDS)
1972         local mdt0_fstype=$(facet_fstype $SINGLEMDS)
1973         local opt
1974         if ! do_node $mdt0_node test -b $mdt0_fstype; then
1975                 opt="-o loop"
1976         fi
1977         echo "$mdt0_node, $mdt0_dev, $mntpt, $opt"
1978         do_node $mdt0_node mount -t $mdt0_fstype $opt $mdt0_dev $mntpt
1979         do_node $mdt0_node mkdir $mntpt/OBJECTS
1980         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.usr $mntpt/OBJECTS
1981         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.grp $mntpt/OBJECTS
1982         do_node $mdt0_node umount -f $mntpt
1983
1984         echo "Setup all..."
1985         setupall
1986
1987         echo "Verify global limits..."
1988         local id_cnt
1989         local limit
1990
1991         local proc="qmt.*.md-0x0.glb-usr"
1992         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
1993         [ $id_cnt -eq 401 ] || error "Migrate inode user limit failed: $id_cnt"
1994         limit=$(getquota -u 1 global isoftlimit)
1995         [ $limit -eq 1024 ] || error "User inode softlimit: $limit"
1996         limit=$(getquota -u 1 global ihardlimit)
1997         [ $limit -eq 2048 ] || error "User inode hardlimit: $limit"
1998
1999         proc="qmt.*.md-0x0.glb-grp"
2000         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2001         [ $id_cnt -eq 401 ] || error "Migrate inode group limit failed: $id_cnt"
2002         limit=$(getquota -g 1 global isoftlimit)
2003         [ $limit -eq 1024 ] || error "Group inode softlimit: $limit"
2004         limit=$(getquota -g 1 global ihardlimit)
2005         [ $limit -eq 2048 ] || error "Group inode hardlimit: $limit"
2006
2007         proc=" qmt.*.dt-0x0.glb-usr"
2008         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2009         [ $id_cnt -eq 401 ] || error "Migrate block user limit failed: $id_cnt"
2010         limit=$(getquota -u 60001 global bsoftlimit)
2011         [ $limit -eq 10485760 ] || error "User block softlimit: $limit"
2012         limit=$(getquota -u 60001 global bhardlimit)
2013         [ $limit -eq 20971520 ] || error "User block hardlimit: $limit"
2014
2015         proc="qmt.*.dt-0x0.glb-grp"
2016         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2017         [ $id_cnt -eq 401 ] || error "Migrate block user limit failed: $id_cnt"
2018         limit=$(getquota -g 60001 global bsoftlimit)
2019         [ $limit -eq 10485760 ] || error "Group block softlimit: $limit"
2020         limit=$(getquota -g 60001 global bhardlimit)
2021         [ $limit -eq 20971520 ] || error "Group block hardlimit: $limit"
2022
2023         echo "Cleanup..."
2024         formatall
2025         setupall
2026 }
2027 run_test 36 "Migrate old admin files into new global indexes"
2028
2029 quota_fini()
2030 {
2031         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2032 }
2033 quota_fini
2034
2035 cd $ORIG_PWD
2036 complete $SECONDS
2037 check_and_cleanup_lustre
2038 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2039 exit_status