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