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