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