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