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