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