Whamcloud - gitweb
LU-10986 lfs: make lfs project tolerant errors
[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 TSTPRJID=${TSTPRJID:-1000}
28 BLK_SZ=1024
29 MAX_DQ_TIME=604800
30 MAX_IQ_TIME=604800
31 QTYPE="ugp"
32
33 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
34 . $LUSTRE/tests/test-framework.sh
35 init_test_env $@
36 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
37 init_logging
38 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
39
40 require_dsh_mds || exit 0
41 require_dsh_ost || exit 0
42
43 # Does e2fsprogs support quota feature?
44 if [ $(facet_fstype $SINGLEMDS) == ldiskfs ] &&
45         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
46                 grep -q 'quota'"; then
47         skip_env "e2fsprogs doesn't support quota" && exit 0
48 fi
49
50 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
51 # bug number for skipped test:        LU-2836 LU-6836 LU-2836
52         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 3       4a      6"
53 # bug number for skipped test:        LU-5638
54         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 11 33"
55 # bug number:     LU-2887
56         #         21  9   (min)"
57         ZFS_SLOW="12a 9"
58 fi
59
60 [ "$SLOW" = "no" ] && EXCEPT_SLOW="$ZFS_SLOW"
61
62 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
63
64 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
65
66 DIR=${DIR:-$MOUNT}
67 DIR2=${DIR2:-$MOUNT2}
68
69 QUOTA_AUTO_OLD=$QUOTA_AUTO
70 export QUOTA_AUTO=0
71
72 check_and_setup_lustre
73
74 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
75 is_project_quota_supported() {
76         $ENABLE_PROJECT_QUOTAS || return 1
77         [ "$(facet_fstype $SINGLEMDS)" == "ldiskfs" ] &&
78                 [ $(lustre_version_code $SINGLEMDS) -gt \
79                 $(version_code 2.9.55) ] &&
80                 lfs --help | grep project >&/dev/null &&
81                 egrep -q "7." /etc/redhat-release && return 0
82
83         if [ "$(facet_fstype $SINGLEMDS)" == "zfs" ]; then
84                 [ $(lustre_version_code $SINGLEMDS) -le \
85                         $(version_code 2.10.53) ] && return 1
86
87                 $ZPOOL upgrade -v | grep project_quota && return 0
88         fi
89
90         return 1
91 }
92
93 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
94 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
95 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
96 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
97 SHOW_QUOTA_PROJID="eval is_project_quota_supported && $LFS quota -v -p $TSTPRJID $DIR"
98 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
99 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
100 SHOW_QUOTA_INFO_PROJID="eval is_project_quota_supported && $LFS quota -t -p $DIR"
101
102 build_test_filter
103
104 lustre_fail() {
105         local fail_node=$1
106         local fail_loc=$2
107         local fail_val=${3:-0}
108         local NODES=
109
110         case $fail_node in
111         mds_ost|mdt_ost) NODES="$(comma_list $(mdts_nodes) $(osts_nodes))";;
112         mds|mdt) NODES="$(comma_list $(mdts_nodes))";;
113         ost) NODES="$(comma_list $(osts_nodes))";;
114         esac
115
116         do_nodes $NODES "lctl set_param fail_val=$fail_val fail_loc=$fail_loc"
117 }
118
119 change_project()
120 {
121         echo "lfs project $*"
122         lfs project $* || error "lfs project $* failed"
123 }
124
125 RUNAS="runas -u $TSTID -g $TSTID"
126 RUNAS2="runas -u $TSTID2 -g $TSTID2"
127 DD="dd if=/dev/zero bs=1M"
128
129 FAIL_ON_ERROR=false
130
131 # clear quota limits for a user or a group
132 # usage: resetquota -u username
133 #        resetquota -g groupname
134 #        resetquota -p projid
135
136 resetquota() {
137         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
138         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
139                 error "resetquota: wrong specifier $1 passed"
140
141         if [ $1 == "-p" ]; then
142                 is_project_quota_supported || return 0
143         fi
144
145         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
146                 error "clear quota for [type:$1 name:$2] failed"
147         # give a chance to slave to release space
148         sleep 1
149 }
150
151 quota_scan() {
152         local local_ugp=$1
153         local local_id=$2
154
155         if [ "$local_ugp" == "a" -o "$local_ugp" == "u" ]; then
156                 $LFS quota -v -u $local_id $DIR
157                 log "Files for user ($local_id):"
158                 ($LFS find --user $local_id $DIR | head -n 4 |
159                         xargs stat 2>/dev/null)
160         fi
161
162         if [ "$local_ugp" == "a" -o "$local_ugp" == "g" ]; then
163                 $LFS quota -v -g $local_id $DIR
164                 log "Files for group ($local_id):"
165                 ($LFS find --group $local_id $DIR | head -n 4 |
166                         xargs stat 2>/dev/null)
167         fi
168
169         if [ "$local_ugp" == "a" -o "$local_ugp" == "p" ]; then
170                 $LFS quota -v -p $TSTPRJID $DIR
171                 log "Files for project ($TSTPRJID):"
172                 ($LFS find --projid $TSTPRJID $DIR | head -n 4 |
173                         xargs stat 2>/dev/null)
174         fi
175 }
176
177 quota_error() {
178         quota_scan $1 $2
179         shift 2
180         error "$*"
181 }
182
183 quota_log() {
184         quota_scan $1 $2
185         shift 2
186         log "$*"
187 }
188
189 # get quota for a user or a group
190 # usage: getquota -u|-g|-p <username>|<groupname>|<projid> global|<obd_uuid> \
191 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
192 getquota() {
193         local spec
194         local uuid
195
196         sync_all_data > /dev/null 2>&1 || true
197
198         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
199         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
200                 error "getquota: wrong u/g/p specifier $1 passed"
201
202         uuid="$3"
203
204         case "$4" in
205                 curspace)   spec=1;;
206                 bsoftlimit) spec=2;;
207                 bhardlimit) spec=3;;
208                 bgrace)     spec=4;;
209                 curinodes)  spec=5;;
210                 isoftlimit) spec=6;;
211                 ihardlimit) spec=7;;
212                 igrace)     spec=8;;
213                 *)          error "unknown quota parameter $4";;
214         esac
215
216         [ "$uuid" = "global" ] && uuid=$DIR
217
218         $LFS quota -v "$1" "$2" $DIR |
219                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
220                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
221                 | tr -d "*"
222 }
223
224 # set mdt quota type
225 # usage: set_mdt_qtype ugp|u|g|p|none
226 set_mdt_qtype() {
227         local qtype=$1
228         local varsvc
229         local mdts=$(get_facets MDS)
230         local cmd
231         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
232                 qtype=$(tr -d 'p' <<<$qtype)
233         do_facet mgs $LCTL conf_param $FSNAME.quota.mdt=$qtype
234         # we have to make sure each MDT received config changes
235         for mdt in ${mdts//,/ }; do
236                 varsvc=${mdt}_svc
237                 cmd="$LCTL get_param -n "
238                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
239                 cmd=${cmd}.quota_slave.enabled
240
241                 if $(facet_up $mdt); then
242                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
243                 fi
244         done
245         return 0
246 }
247
248 # set ost quota type
249 # usage: set_ost_qtype ugp|u|g|p|none
250 set_ost_qtype() {
251         local qtype=$1
252         local varsvc
253         local osts=$(get_facets OST)
254         local cmd
255         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
256                 qtype=$(tr -d 'p' <<<$qtype)
257         do_facet mgs $LCTL conf_param $FSNAME.quota.ost=$qtype
258         # we have to make sure each OST received config changes
259         for ost in ${osts//,/ }; do
260                 varsvc=${ost}_svc
261                 cmd="$LCTL get_param -n "
262                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
263                 cmd=${cmd}.quota_slave.enabled
264
265                 if $(facet_up $ost); then
266                         wait_update_facet $ost "$cmd" "$qtype" || return 1
267                 fi
268         done
269         return 0
270 }
271
272 wait_reintegration() {
273         local ntype=$1
274         local qtype=$2
275         local max=$3
276         local result="glb[1],slv[1],reint[0]"
277         local varsvc
278         local cmd
279         local tgts
280
281         if [ $ntype == "mdt" ]; then
282                 tgts=$(get_facets MDS)
283         else
284                 tgts=$(get_facets OST)
285         fi
286
287         for tgt in ${tgts//,/ }; do
288                 varsvc=${tgt}_svc
289                 cmd="$LCTL get_param -n "
290                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
291                 cmd=${cmd}.quota_slave.info
292
293                 if $(facet_up $tgt); then
294                         wait_update_facet $tgt "$cmd |
295                                 grep "$qtype" | awk '{ print \\\$3 }'" \
296                                         "$result" $max || return 1
297                 fi
298         done
299         return 0
300 }
301
302 wait_mdt_reint() {
303         local qtype=$1
304         local max=${2:-90}
305
306         if [[ "$qtype" =~ "u" ]]; then
307                 wait_reintegration "mdt" "user" $max || return 1
308         fi
309
310         if [[ "$qtype" =~ "g" ]]; then
311                 wait_reintegration "mdt" "group" $max || return 1
312         fi
313
314         if [[ "$qtype" =~ "p" ]]; then
315                 ! is_project_quota_supported && return 0
316                 wait_reintegration "mdt" "project" $max || return 1
317         fi
318         return 0
319 }
320
321 wait_ost_reint() {
322         local qtype=$1
323         local max=${2:-90}
324
325         if [[ "$qtype" =~ "u" ]]; then
326                 wait_reintegration "ost" "user" $max || return 1
327         fi
328
329         if [[ "$qtype" =~ "g" ]]; then
330                 wait_reintegration "ost" "group" $max || return 1
331         fi
332
333         if [[ "$qtype" =~ "p" ]]; then
334                 ! is_project_quota_supported && return 0
335                 wait_reintegration "ost" "project" $max || return 1
336         fi
337         return 0
338 }
339
340 disable_project_quota() {
341         is_project_quota_supported || return 0
342         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] && return 0
343         stopall || error "failed to stopall (1)"
344
345         for num in $(seq $MDSCOUNT); do
346                 do_facet mds$num $TUNE2FS -Q ^prj $(mdsdevname $num) ||
347                         error "tune2fs $(mdsdevname $num) failed"
348         done
349
350         for num in $(seq $OSTCOUNT); do
351                 do_facet ost$num $TUNE2FS -Q ^prj $(ostdevname $num) ||
352                         error "tune2fs $(ostdevname $num) failed"
353         done
354
355         mount
356         setupall
357 }
358
359 setup_quota_test() {
360         wait_delete_completed
361         echo "Creating test directory"
362         mkdir $DIR/$tdir || return 1
363         chmod 0777 $DIR/$tdir || return 2
364         # always clear fail_loc in case of fail_loc isn't cleared
365         # properly when previous test failed
366         lustre_fail mds_ost 0
367 }
368
369 cleanup_quota_test() {
370         trap 0
371         echo "Delete files..."
372         rm -rf $DIR/$tdir
373         echo "Wait for unlink objects finished..."
374         wait_delete_completed
375         sync_all_data || true
376 }
377
378 quota_show_check() {
379         local bf=$1
380         local ugp=$2
381         local qid=$3
382         local usage
383
384         $LFS quota -v -$ugp $qid $DIR
385
386         if [ "$bf" == "a" -o "$bf" == "b" ]; then
387                 usage=$(getquota -$ugp $qid global curspace)
388                 if [ -z $usage ]; then
389                         quota_error $ugp $qid \
390                                 "Query block quota failed ($ugp:$qid)."
391                 else
392                         [ $usage -ne 0 ] && quota_log $ugp $qid \
393                                 "Block quota isn't 0 ($ugp:$qid:$usage)."
394                 fi
395         fi
396
397         if [ "$bf" == "a" -o "$bf" == "f" ]; then
398                 usage=$(getquota -$ugp $qid global curinodes)
399                 if [ -z $usage ]; then
400                         quota_error $ugp $qid \
401                                 "Query file quota failed ($ugp:$qid)."
402                 else
403                         [ $usage -ne 0 ] && quota_log $ugp $qid \
404                                 "File quota isn't 0 ($ugp:$qid:$usage)."
405                 fi
406         fi
407 }
408
409 enable_project_quota() {
410         is_project_quota_supported || return 0
411         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] && return 0
412         stopall || error "failed to stopall (1)"
413
414         for num in $(seq $MDSCOUNT); do
415                 do_facet mds$num $TUNE2FS -O project $(mdsdevname $num) ||
416                         error "tune2fs $(mdsdevname $num) failed"
417         done
418
419         for num in $(seq $OSTCOUNT); do
420                 do_facet ost$num $TUNE2FS -O project $(ostdevname $num) ||
421                         error "tune2fs $(ostdevname $num) failed"
422         done
423
424         mount
425         setupall
426 }
427 enable_project_quota
428
429 reset_quota_settings() {
430         resetquota -u $TSTUSR
431         resetquota -g $TSTUSR
432         resetquota -u $TSTUSR2
433         resetquota -g $TSTUSR2
434         resetquota -p $TSTPRJID
435 }
436
437 # enable quota debug
438 quota_init() {
439         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
440 }
441 quota_init
442 reset_quota_settings
443
444 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
445         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
446 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
447         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
448
449 test_quota_performance() {
450         local TESTFILE="$DIR/$tdir/$tfile-0"
451         local size=$1 # in MB
452         local stime=$(date +%s)
453         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
454                 quota_error u $TSTUSR "write failure"
455         local etime=$(date +%s)
456         delta=$((etime - stime))
457         if [ $delta -gt 0 ]; then
458             rate=$((size * 1024 / delta))
459             if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
460                 # LU-2872 - see LU-2887 for fix
461                 [ $rate -gt 64 ] ||
462                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
463             else
464                 [ $rate -gt 1024 ] ||
465                         error "SLOW IO for $TSTUSR (user): $rate KB/sec"
466             fi
467         fi
468         rm -f $TESTFILE
469 }
470
471 # test basic quota performance b=21696
472 test_0() {
473         local MB=100 # 100M
474         [ "$SLOW" = "no" ] && MB=10
475
476         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
477         [ $free_space -le $((MB * 1024)) ] &&
478                 skip "not enough space ${free_space} KB, " \
479                         "required $((MB * 1024)) KB" && return
480         setup_quota_test || error "setup quota failed with $?"
481         trap cleanup_quota_test EXIT
482
483         set_ost_qtype "none" || error "disable ost quota failed"
484         test_quota_performance $MB
485
486         set_ost_qtype $QTYPE || error "enable ost quota failed"
487         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
488                 error "set quota failed"
489         test_quota_performance $MB
490
491         cleanup_quota_test
492         resetquota -u $TSTUSR
493 }
494 run_test 0 "Test basic quota performance"
495
496 # test block hardlimit
497 test_1() {
498         local LIMIT=10  # 10M
499         local TESTFILE="$DIR/$tdir/$tfile-0"
500
501         setup_quota_test || error "setup quota failed with $?"
502         trap cleanup_quota_test EXIT
503
504         # enable ost quota
505         set_ost_qtype $QTYPE || error "enable ost quota failed"
506
507         # test for user
508         log "User quota (block hardlimit:$LIMIT MB)"
509         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
510                 error "set user quota failed"
511
512         # make sure the system is clean
513         local USED=$(getquota -u $TSTUSR global curspace)
514         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
515
516         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
517         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
518
519         log "Write..."
520         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
521                 quota_error u $TSTUSR "user write failure, but expect success"
522         log "Write out of block quota ..."
523         # this time maybe cache write,  ignore it's failure
524         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
525         # flush cache, ensure noquota flag is set on client
526         cancel_lru_locks osc
527         sync; sync_all_data || true
528         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
529                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
530
531         rm -f $TESTFILE
532         wait_delete_completed || error "wait_delete_completed failed"
533         sync_all_data || true
534         USED=$(getquota -u $TSTUSR global curspace)
535         [ $USED -ne 0 ] && quota_error u $TSTUSR \
536                 "user quota isn't released after deletion"
537         resetquota -u $TSTUSR
538
539         # test for group
540         log "--------------------------------------"
541         log "Group quota (block hardlimit:$LIMIT MB)"
542         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
543                 error "set group quota failed"
544
545         TESTFILE="$DIR/$tdir/$tfile-1"
546         # make sure the system is clean
547         USED=$(getquota -g $TSTUSR global curspace)
548         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTUSR isn't 0"
549
550         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
551         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
552
553         log "Write ..."
554         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
555                 quota_error g $TSTUSR "Group write failure, but expect success"
556         log "Write out of block quota ..."
557         # this time maybe cache write, ignore it's failure
558         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
559         cancel_lru_locks osc
560         sync; sync_all_data || true
561         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
562                 quota_error g $TSTUSR "Group write success, but expect EDQUOT"
563         rm -f $TESTFILE
564         wait_delete_completed || error "wait_delete_completed failed"
565         sync_all_data || true
566         USED=$(getquota -g $TSTUSR global curspace)
567         [ $USED -ne 0 ] && quota_error g $TSTUSR \
568                                 "Group quota isn't released after deletion"
569         resetquota -g $TSTUSR
570
571         if ! is_project_quota_supported; then
572                 echo "Project quota is not supported"
573                 cleanup_quota_test
574                 return 0
575         fi
576
577         TESTFILE="$DIR/$tdir/$tfile-2"
578         # make sure the system is clean
579         USED=$(getquota -p $TSTPRJID global curspace)
580         [ $USED -ne 0 ] &&
581                 error "used space($USED) for project $TSTPRJID isn't 0"
582
583         # test for Project
584         log "--------------------------------------"
585         log "Project quota (block hardlimit:$LIMIT mb)"
586         $LFS setquota -p $TSTPRJID -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
587                 error "set project quota failed"
588
589         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
590         chown $TSTUSR:$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
591         change_project -p $TSTPRJID $TESTFILE
592
593         log "write ..."
594         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) || quota_error p $TSTPRJID \
595                 "project write failure, but expect success"
596         log "write out of block quota ..."
597         # this time maybe cache write, ignore it's failure
598         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
599         cancel_lru_locks osc
600         sync; sync_all_data || true
601         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT && quota_error p \
602                 $TSTPRJID "project write success, but expect EDQUOT"
603
604         # cleanup
605         cleanup_quota_test
606
607         USED=$(getquota -p $TSTPRJID global curspace)
608         [ $USED -ne 0 ] && quota_error p $TSTPRJID \
609                 "project quota isn't released after deletion"
610
611         resetquota -p $TSTPRJID
612 }
613 run_test 1 "Block hard limit (normal use and out of quota)"
614
615 # test inode hardlimit
616 test_2() {
617         local LIMIT=$((1024 * 1024)) # 1M inodes
618         local TESTFILE="$DIR/$tdir/$tfile-0"
619
620         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
621
622         local FREE_INODES=$(mdt_free_inodes 0)
623         echo "$FREE_INODES free inodes on master MDT"
624         [ $FREE_INODES -lt $LIMIT ] &&
625                 skip "not enough free inodes $FREE_INODES required $LIMIT" &&
626                 return
627
628         setup_quota_test || error "setup quota failed with $?"
629         trap cleanup_quota_test EXIT
630
631         # enable mdt quota
632         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
633
634         # test for user
635         log "User quota (inode hardlimit:$LIMIT files)"
636         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
637                 error "set user quota failed"
638
639         # make sure the system is clean
640         local USED=$(getquota -u $TSTUSR global curinodes)
641         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
642
643         log "Create $LIMIT files ..."
644         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
645                 quota_error u $TSTUSR "user create failure, but expect success"
646         log "Create out of file quota ..."
647         $RUNAS touch ${TESTFILE}_xxx &&
648                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
649
650         # cleanup
651         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
652         rm -f ${TESTFILE}_xxx
653         wait_delete_completed
654
655         USED=$(getquota -u $TSTUSR global curinodes)
656         [ $USED -ne 0 ] && quota_error u $TSTUSR \
657                 "user quota isn't released after deletion"
658         resetquota -u $TSTUSR
659
660         # test for group
661         log "--------------------------------------"
662         log "Group quota (inode hardlimit:$LIMIT files)"
663         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
664                 error "set group quota failed"
665
666         TESTFILE=$DIR/$tdir/$tfile-1
667         # make sure the system is clean
668         USED=$(getquota -g $TSTUSR global curinodes)
669         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
670
671         log "Create $LIMIT files ..."
672         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
673                 quota_error g $TSTUSR "group create failure, but expect success"
674         log "Create out of file quota ..."
675         $RUNAS touch ${TESTFILE}_xxx &&
676                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
677
678         # cleanup
679         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
680         rm -f ${TESTFILE}_xxx
681         wait_delete_completed
682
683         USED=$(getquota -g $TSTUSR global curinodes)
684         [ $USED -ne 0 ] && quota_error g $TSTUSR \
685                 "user quota isn't released after deletion"
686
687         resetquota -g $TSTUSR
688         ! is_project_quota_supported && cleanup_quota_test &&
689                 echo "Skip project quota is not supported" && return 0
690
691         # test for project
692         log "--------------------------------------"
693         log "Project quota (inode hardlimit:$LIMIT files)"
694         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
695                 error "set project quota failed"
696
697         TESTFILE=$DIR/$tdir/$tfile-1
698         # make sure the system is clean
699         USED=$(getquota -p $TSTPRJID global curinodes)
700         [ $USED -ne 0 ] &&
701                 error "Used inodes($USED) for project $TSTPRJID isn't 0"
702
703         change_project -sp $TSTPRJID $DIR/$tdir
704         log "Create $LIMIT files ..."
705         $RUNAS createmany -m ${TESTFILE} $((LIMIT-1)) || quota_error p \
706                 $TSTPRJID "project create fail, but expect success"
707         log "Create out of file quota ..."
708         $RUNAS touch ${TESTFILE}_xxx && quota_error p $TSTPRJID \
709                 "project create success, but expect EDQUOT"
710         change_project -C $DIR/$tdir
711
712         cleanup_quota_test
713         USED=$(getquota -p $TSTPRJID global curinodes)
714         [ $USED -ne 0 ] && quota_error p $TSTPRJID \
715                 "project quota isn't released after deletion"
716
717         resetquota -p $TSTPRJID
718
719 }
720 run_test 2 "File hard limit (normal use and out of quota)"
721
722 test_block_soft() {
723         local TESTFILE=$1
724         local TIMER=$(($2 * 3 / 2))
725         local LIMIT=$3
726         local OFFSET=0
727         local qtype=$4
728
729         setup_quota_test
730         trap cleanup_quota_test EXIT
731
732         $SETSTRIPE $TESTFILE -c 1 -i 0
733         chown $TSTUSR.$TSTUSR $TESTFILE
734         [ "$qtype" == "p" ] && is_project_quota_supported &&
735                 change_project -p $TSTPRJID $TESTFILE
736
737         echo "Write up to soft limit"
738         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
739                 quota_error a $TSTUSR "write failure, but expect success"
740         OFFSET=$((LIMIT * 1024))
741         cancel_lru_locks osc
742
743         echo "Write to exceed soft limit"
744         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
745                 quota_error a $TSTUSR "write failure, but expect success"
746         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
747         cancel_lru_locks osc
748
749         $SHOW_QUOTA_USER
750         $SHOW_QUOTA_GROUP
751         $SHOW_QUOTA_PROJID
752         $SHOW_QUOTA_INFO_USER
753         $SHOW_QUOTA_INFO_GROUP
754         $SHOW_QUOTA_INFO_PROJID
755
756         echo "Write before timer goes off"
757         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
758                 quota_error a $TSTUSR "write failure, but expect success"
759         OFFSET=$((OFFSET + 1024))
760         cancel_lru_locks osc
761
762         echo "Sleep $TIMER seconds ..."
763         sleep $TIMER
764
765         $SHOW_QUOTA_USER
766         $SHOW_QUOTA_GROUP
767         $SHOW_QUOTA_PROJID
768         $SHOW_QUOTA_INFO_USER
769         $SHOW_QUOTA_INFO_GROUP
770         $SHOW_QUOTA_INFO_PROJID
771
772         echo "Write after timer goes off"
773         # maybe cache write, ignore.
774         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
775         OFFSET=$((OFFSET + 1024))
776         cancel_lru_locks osc
777         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
778                 quota_error a $TSTUSR "write success, but expect EDQUOT"
779
780         $SHOW_QUOTA_USER
781         $SHOW_QUOTA_GROUP
782         $SHOW_QUOTA_PROJID
783         $SHOW_QUOTA_INFO_USER
784         $SHOW_QUOTA_INFO_GROUP
785         $SHOW_QUOTA_INFO_PROJID
786
787         echo "Unlink file to stop timer"
788         rm -f $TESTFILE
789         wait_delete_completed
790         sync_all_data || true
791
792         $SHOW_QUOTA_USER
793         $SHOW_QUOTA_GROUP
794         $SHOW_QUOTA_PROJID
795         $SHOW_QUOTA_INFO_USER
796         $SHOW_QUOTA_INFO_GROUP
797         $SHOW_QUOTA_INFO_PROJID
798
799         $SETSTRIPE $TESTFILE -c 1 -i 0
800         chown $TSTUSR.$TSTUSR $TESTFILE
801         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $TESTFILE
802
803         echo "Write ..."
804         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
805                 quota_error a $TSTUSR "write failure, but expect success"
806         # cleanup
807         cleanup_quota_test
808 }
809
810 # block soft limit
811 test_3() {
812         local LIMIT=1  # 1MB
813         local GRACE=20 # 20s
814         local TESTFILE=$DIR/$tdir/$tfile-0
815
816         set_ost_qtype $QTYPE || error "enable ost quota failed"
817
818         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
819         # make sure the system is clean
820         local USED=$(getquota -u $TSTUSR global curspace)
821         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
822
823         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
824                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
825         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
826                 error "set user quota failed"
827
828         test_block_soft $TESTFILE $GRACE $LIMIT "u"
829         resetquota -u $TSTUSR
830
831         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
832         TESTFILE=$DIR/$tdir/$tfile-1
833         # make sure the system is clean
834         USED=$(getquota -g $TSTUSR global curspace)
835         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
836
837         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
838                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
839         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
840                 error "set group quota failed"
841
842         test_block_soft $TESTFILE $GRACE $LIMIT "g"
843         resetquota -g $TSTUSR
844
845         if is_project_quota_supported; then
846                 echo "Project quota (soft limit:$LIMIT MB  grace:$GRACE sec)"
847                 TESTFILE=$DIR/$tdir/$tfile-2
848                 # make sure the system is clean
849                 USED=$(getquota -p $TSTPRJID global curspace)
850                 [ $USED -ne 0 ] && error \
851                         "Used space($USED) for project $TSTPRJID isn't 0."
852
853                 $LFS setquota -t -p --block-grace $GRACE --inode-grace \
854                         $MAX_IQ_TIME $DIR ||
855                                 error "set project grace time failed"
856                 $LFS setquota -p $TSTPRJID -b ${LIMIT}M -B 0 -i 0 -I 0 \
857                         $DIR || error "set project quota failed"
858
859                 test_block_soft $TESTFILE $GRACE $LIMIT "p"
860                 resetquota -p $TSTPRJID
861                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
862                         $MAX_IQ_TIME $DIR ||
863                                 error "restore project grace time failed"
864         fi
865
866         # cleanup
867         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
868                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
869         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
870                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
871 }
872 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
873
874 test_file_soft() {
875         local TESTFILE=$1
876         local LIMIT=$2
877         local grace=$3
878         local TIMER=$(($grace * 3 / 2))
879
880         setup_quota_test
881         trap cleanup_quota_test EXIT
882         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
883
884         echo "Create files to exceed soft limit"
885         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
886                 quota_error a $TSTUSR "create failure, but expect success"
887         local trigger_time=$(date +%s)
888
889         sync_all_data || true
890
891         local cur_time=$(date +%s)
892         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
893                 error "Passed grace time $grace, $trigger_time, $cur_time"
894
895         echo "Create file before timer goes off"
896         $RUNAS touch ${TESTFILE}_before ||
897                 quota_error a $TSTUSR "failed create before timer expired," \
898                         "but expect success. $trigger_time, $cur_time"
899         sync_all_data || true
900
901         echo "Sleep $TIMER seconds ..."
902         sleep $TIMER
903
904         $SHOW_QUOTA_USER
905         $SHOW_QUOTA_GROUP
906         $SHOW_QUOTA_PROJID
907         $SHOW_QUOTA_INFO_USER
908         $SHOW_QUOTA_INFO_GROUP
909         $SHOW_QUOTA_INFO_PROJID
910
911         echo "Create file after timer goes off"
912         # There is a window that space is accounted in the quota usage but
913         # hasn't been decreased from the pending write, if we acquire quota
914         # in this window, we'll acquire more than we needed.
915         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
916         sync_all_data || true
917         $RUNAS touch ${TESTFILE}_after_3 &&
918                 quota_error a $TSTUSR "create after timer expired," \
919                         "but expect EDQUOT"
920         sync_all_data || true
921
922         $SHOW_QUOTA_USER
923         $SHOW_QUOTA_GROUP
924         $SHOW_QUOTA_PROJID
925         $SHOW_QUOTA_INFO_USER
926         $SHOW_QUOTA_INFO_GROUP
927         $SHOW_QUOTA_INFO_PROJID
928
929         echo "Unlink files to stop timer"
930         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
931         wait_delete_completed
932
933         echo "Create file"
934         $RUNAS touch ${TESTFILE}_xxx ||
935                 quota_error a $TSTUSR "touch after timer stop failure," \
936                         "but expect success"
937         sync_all_data || true
938
939         # cleanup
940         cleanup_quota_test
941 }
942
943 # file soft limit
944 test_4a() {
945         local LIMIT=10 # inodes
946         local TESTFILE=$DIR/$tdir/$tfile-0
947         local GRACE=12
948
949         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
950
951         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
952         # make sure the system is clean
953         local USED=$(getquota -u $TSTUSR global curinodes)
954         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
955
956         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
957                 $GRACE $DIR || error "set user grace time failed"
958         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
959                 error "set user quota failed"
960
961         test_file_soft $TESTFILE $LIMIT $GRACE
962         resetquota -u $TSTUSR
963
964         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
965         # make sure the system is clean
966         USED=$(getquota -g $TSTUSR global curinodes)
967         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
968
969         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
970                 $GRACE $DIR || error "set group grace time failed"
971         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
972                 error "set group quota failed"
973         TESTFILE=$DIR/$tdir/$tfile-1
974
975         test_file_soft $TESTFILE $LIMIT $GRACE
976         resetquota -g $TSTUSR
977
978         if is_project_quota_supported; then
979                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
980                 # make sure the system is clean
981                 USED=$(getquota -p $TSTPRJID global curinodes)
982                 [ $USED -ne 0 ] && error \
983                         "Used space($USED) for project $TSTPRJID isn't 0."
984
985                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
986                         $GRACE $DIR || error "set project grace time failed"
987                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
988                         error "set project quota failed"
989
990                 TESTFILE=$DIR/$tdir/$tfile-1
991                 # one less than limit, because of parent directory included.
992                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE
993                 resetquota -p $TSTPRJID
994                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
995                         $MAX_IQ_TIME $DIR ||
996                                 error "restore project grace time failed"
997         fi
998
999         # cleanup
1000         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1001                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1002         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1003                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1004 }
1005 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1006
1007 test_4b() {
1008         local GR_STR1="1w3d"
1009         local GR_STR2="1000s"
1010         local GR_STR3="5s"
1011         local GR_STR4="1w2d3h4m5s"
1012         local GR_STR5="5c"
1013         local GR_STR6="18446744073709551615"
1014         local GR_STR7="-1"
1015
1016         wait_delete_completed
1017
1018         # test of valid grace strings handling
1019         echo "Valid grace strings test"
1020         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1021                 $GR_STR2 $DIR || error "set user grace time failed"
1022         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1023         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1024                 $GR_STR4 $DIR || error "set group grace time quota failed"
1025         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1026
1027         # test of invalid grace strings handling
1028         echo "  Invalid grace strings test"
1029         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1030         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1031         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1032                 $GR_STR7 $DIR
1033
1034         # cleanup
1035         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1036                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1037         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1038                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1039 }
1040 run_test 4b "Grace time strings handling"
1041
1042 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1043 test_5() {
1044         local BLIMIT=10 # 10M
1045         local ILIMIT=10 # 10 inodes
1046
1047         setup_quota_test || error "setup quota failed with $?"
1048         trap cleanup_quota_test EXIT
1049
1050         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1051         set_ost_qtype $QTYPE || error "enable ost quota failed"
1052
1053         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1054         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1055                 error "set user quota failed"
1056         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1057         if is_project_quota_supported; then
1058                 error "set group quota failed"
1059                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1060                         -I $ILIMIT $DIR || error "set project quota failed"
1061         fi
1062
1063         # make sure the system is clean
1064         local USED=$(getquota -u $TSTUSR global curinodes)
1065         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1066         USED=$(getquota -g $TSTUSR global curinodes)
1067         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1068         USED=$(getquota -u $TSTUSR global curspace)
1069         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1070         USED=$(getquota -g $TSTUSR global curspace)
1071         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1072         if is_project_quota_supported; then
1073                 USED=$(getquota -p $TSTPRJID global curinodes)
1074                 [ $USED -ne 0 ] &&
1075                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1076                 USED=$(getquota -p $TSTPRJID global curspace)
1077                 [ $USED -ne 0 ] &&
1078                         error "Used block($USED) for project $TSTPRJID isn't 0."
1079         fi
1080
1081         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1082         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1083                 error "create failure, expect success"
1084         if is_project_quota_supported; then
1085                 touch $DIR/$tdir/$tfile-0_1
1086                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1087         fi
1088         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1089                 error "write failure, expect success"
1090
1091         echo "Chown files to $TSTUSR.$TSTUSR ..."
1092         for i in $(seq 0 $ILIMIT); do
1093                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1094                         quota_error a $TSTUSR "chown failure, expect success"
1095         done
1096
1097         # cleanup
1098         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1099                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1100         cleanup_quota_test
1101
1102         resetquota -u $TSTUSR
1103         resetquota -g $TSTUSR
1104         resetquota -p $TSTPRJID
1105 }
1106 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1107
1108 # test dropping acquire request on master
1109 test_6() {
1110         local LIMIT=3 # 3M
1111
1112         # Clear dmesg so watchdog is not triggered by previous
1113         # test output
1114         do_facet ost1 dmesg -c > /dev/null
1115
1116         setup_quota_test || error "setup quota failed with $?"
1117         trap cleanup_quota_test EXIT
1118
1119         # make sure the system is clean
1120         local USED=$(getquota -u $TSTUSR global curspace)
1121         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1122
1123         # make sure no granted quota on ost
1124         set_ost_qtype $QTYPE || error "enable ost quota failed"
1125         resetquota -u $TSTUSR
1126
1127         # create file for $TSTUSR
1128         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1129         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1130         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1131
1132         # create file for $TSTUSR2
1133         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1134         $SETSTRIPE $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1135         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1136
1137         # cache per-ID lock for $TSTUSR on slave
1138         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1139                 error "set quota failed"
1140         $RUNAS $DD of=$TESTFILE count=1 ||
1141                 error "write $TESTFILE failure, expect success"
1142         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1143                 error "write $TESTFILE2 failure, expect success"
1144         sync; sync
1145         sync_all_data || true
1146
1147         #define QUOTA_DQACQ 601
1148         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1149         lustre_fail mds 0x513 601
1150
1151         # write to un-enforced ID ($TSTUSR2) should succeed
1152         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1153                 error "write failure, expect success"
1154
1155         # write to enforced ID ($TSTUSR) in background, exceeding limit
1156         # to make sure DQACQ is sent
1157         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1158         DDPID=$!
1159
1160         # watchdog timer uses a factor of 2
1161         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1162         sleep $((TIMEOUT * 2 + 1))
1163
1164         # write should be blocked and never finished
1165         if ! ps -p $DDPID  > /dev/null 2>&1; then
1166                 lustre_fail mds 0 0
1167                 error "write finished incorrectly!"
1168         fi
1169
1170         lustre_fail mds 0 0
1171
1172         # no watchdog is triggered
1173         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1174         watchdog=$(awk '/Service thread pid/ && /was inactive/ \
1175                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1176         [ -z "$watchdog" ] || error "$watchdog"
1177
1178         rm -f $TMP/lustre-log-${TESTNAME}.log
1179
1180         # write should continue then fail with EDQUOT
1181         local count=0
1182         local c_size
1183         while [ true ]; do
1184                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1185                 if [ $count -ge 240 ]; then
1186                         quota_error u $TSTUSR "dd not finished in $count secs"
1187                 fi
1188                 count=$((count + 1))
1189                 if [ $((count % 30)) -eq 0 ]; then
1190                         c_size=$(stat -c %s $TESTFILE)
1191                         echo "Waiting $count secs. $c_size"
1192                         $SHOW_QUOTA_USER
1193                 fi
1194                 sleep 1
1195         done
1196
1197         cleanup_quota_test
1198         resetquota -u $TSTUSR
1199 }
1200 run_test 6 "Test dropping acquire request on master"
1201
1202 # quota reintegration (global index)
1203 test_7a() {
1204         local TESTFILE=$DIR/$tdir/$tfile
1205         local LIMIT=20 # 20M
1206
1207         [ "$SLOW" = "no" ] && LIMIT=5
1208
1209         setup_quota_test || error "setup quota failed with $?"
1210         trap cleanup_quota_test EXIT
1211
1212         # make sure the system is clean
1213         local USED=$(getquota -u $TSTUSR global curspace)
1214         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1215
1216         # make sure no granted quota on ost1
1217         set_ost_qtype $QTYPE || error "enable ost quota failed"
1218         resetquota -u $TSTUSR
1219         set_ost_qtype "none" || error "disable ost quota failed"
1220
1221         local OSTUUID=$(ostuuid_from_index 0)
1222         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1223         [ $USED -ne 0 ] &&
1224                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1225
1226         # create test file
1227         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1228         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1229
1230         echo "Stop ost1..."
1231         stop ost1
1232
1233         echo "Enable quota & set quota limit for $TSTUSR"
1234         set_ost_qtype $QTYPE || error "enable ost quota failed"
1235         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1236                 error "set quota failed"
1237
1238         echo "Start ost1..."
1239         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1240         quota_init
1241
1242         wait_ost_reint $QTYPE || error "reintegration failed"
1243
1244         # hardlimit should have been fetched by slave during global
1245         # reintegration, write will exceed quota
1246         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1247                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1248
1249         rm -f $TESTFILE
1250         wait_delete_completed
1251         sync_all_data || true
1252         sleep 3
1253
1254         echo "Stop ost1..."
1255         stop ost1
1256
1257         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1258                 error "clear quota failed"
1259
1260         echo "Start ost1..."
1261         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1262         quota_init
1263
1264         wait_ost_reint $QTYPE || error "reintegration failed"
1265
1266         # hardlimit should be cleared on slave during reintegration
1267         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1268                 quota_error u $TSTUSR "write error, but expect success"
1269
1270         cleanup_quota_test
1271         resetquota -u $TSTUSR
1272 }
1273 run_test 7a "Quota reintegration (global index)"
1274
1275 # quota reintegration (slave index)
1276 test_7b() {
1277         local LIMIT="100G"
1278         local TESTFILE=$DIR/$tdir/$tfile
1279
1280         setup_quota_test || error "setup quota failed with $?"
1281         trap cleanup_quota_test EXIT
1282
1283         # make sure the system is clean
1284         local USED=$(getquota -u $TSTUSR global curspace)
1285         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1286
1287         # make sure no granted quota on ost1
1288         set_ost_qtype $QTYPE || error "enable ost quota failed"
1289         resetquota -u $TSTUSR
1290         set_ost_qtype "none" || error "disable ost quota failed"
1291
1292         local OSTUUID=$(ostuuid_from_index 0)
1293         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1294         [ $USED -ne 0 ] &&
1295                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1296
1297         # create test file
1298         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1299         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1300
1301         # consume some space to make sure the granted space will not
1302         # be released during reconciliation
1303         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1304                 error "consume space failure, expect success"
1305
1306         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1307         lustre_fail mds 0xa02
1308
1309         set_ost_qtype $QTYPE || error "enable ost quota failed"
1310         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1311                 error "set quota failed"
1312
1313         # ignore the write error
1314         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1315
1316         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1317
1318         lustre_fail mds 0
1319
1320         echo "Restart ost to trigger reintegration..."
1321         stop ost1
1322         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1323         quota_init
1324
1325         wait_ost_reint $QTYPE || error "reintegration failed"
1326
1327         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1328         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1329
1330         cleanup_quota_test
1331         resetquota -u $TSTUSR
1332         $SHOW_QUOTA_USER
1333 }
1334 run_test 7b "Quota reintegration (slave index)"
1335
1336 # quota reintegration (restart mds during reintegration)
1337 test_7c() {
1338         local LIMIT=20 # 20M
1339         local TESTFILE=$DIR/$tdir/$tfile
1340
1341         [ "$SLOW" = "no" ] && LIMIT=5
1342
1343         setup_quota_test || error "setup quota failed with $?"
1344         trap cleanup_quota_test EXIT
1345
1346         # make sure the system is clean
1347         local USED=$(getquota -u $TSTUSR global curspace)
1348         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1349
1350         set_ost_qtype "none" || error "disable ost quota failed"
1351         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1352                 error "set quota failed"
1353
1354         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1355         lustre_fail ost 0xa03
1356
1357         # enable ost quota
1358         set_ost_qtype $QTYPE || error "enable ost quota failed"
1359         # trigger reintegration
1360         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
1361         procf=${procf}quota_slave.force_reint
1362         do_facet ost1 $LCTL set_param $procf=1 ||
1363                 error "force reintegration failed"
1364
1365         echo "Stop mds..."
1366         stop mds1
1367
1368         lustre_fail ost 0
1369
1370         echo "Start mds..."
1371         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1372         quota_init
1373
1374         # wait longer than usual to make sure the reintegration
1375         # is triggered by quota wb thread.
1376         wait_ost_reint $QTYPE 200 || error "reintegration failed"
1377
1378         # hardlimit should have been fetched by slave during global
1379         # reintegration, write will exceed quota
1380         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1381                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1382
1383         cleanup_quota_test
1384         resetquota -u $TSTUSR
1385 }
1386 run_test 7c "Quota reintegration (restart mds during reintegration)"
1387
1388 # Quota reintegration (Transfer index in multiple bulks)
1389 test_7d(){
1390         local TESTFILE=$DIR/$tdir/$tfile
1391         local TESTFILE1="$DIR/$tdir/$tfile"-1
1392         local limit=20 #20M
1393
1394         setup_quota_test || error "setup quota failed with $?"
1395         trap cleanup_quota_test EXIT
1396
1397         set_ost_qtype "none" || error "disable ost quota failed"
1398         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1399                 error "set quota for $TSTUSR failed"
1400         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1401                 error "set quota for $TSTUSR2 failed"
1402
1403         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1404         lustre_fail mds 0x608 0
1405
1406         # enable quota to tirgger reintegration
1407         set_ost_qtype "u" || error "enable ost quota failed"
1408         wait_ost_reint "u" || error "reintegration failed"
1409
1410         lustre_fail mds 0
1411
1412         # hardlimit should have been fetched by slave during global
1413         # reintegration, write will exceed quota
1414         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1415                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1416
1417         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1418                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1419
1420         cleanup_quota_test
1421         resetquota -u $TSTUSR
1422         resetquota -u $TSTUSR2
1423 }
1424 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1425
1426 # quota reintegration (inode limits)
1427 test_7e() {
1428         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs" && return
1429
1430         # LU-2435: skip this quota test if underlying zfs version has not
1431         # supported native dnode accounting
1432         [ "$(facet_fstype mds1)" == "zfs" ] && {
1433                 local F="feature@userobj_accounting"
1434                 local pool=$(zpool_name mds1)
1435                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
1436
1437                 [[ "$feature" != *" active "* ]] &&
1438                         skip "requires zpool with active userobj_accounting" &&
1439                         return
1440         }
1441
1442         local ilimit=$((1024 * 2)) # 2k inodes
1443         local TESTFILE=$DIR/${tdir}-1/$tfile
1444
1445         setup_quota_test || error "setup quota failed with $?"
1446         trap cleanup_quota_test EXIT
1447
1448         # make sure the system is clean
1449         local USED=$(getquota -u $TSTUSR global curinodes)
1450         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1451
1452         # make sure no granted quota on mdt1
1453         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1454         resetquota -u $TSTUSR
1455         set_mdt_qtype "none" || error "disable mdt quota failed"
1456
1457         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1458         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1459         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1460                 "$TSTUSR isn't 0."
1461
1462         echo "Stop mds${MDSCOUNT}..."
1463         stop mds${MDSCOUNT}
1464
1465         echo "Enable quota & set quota limit for $TSTUSR"
1466         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1467         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1468                 error "set quota failed"
1469
1470         echo "Start mds${MDSCOUNT}..."
1471         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1472         quota_init
1473
1474         wait_mdt_reint $QTYPE || error "reintegration failed"
1475
1476         echo "create remote dir"
1477         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1478                 error "create remote dir failed"
1479         chmod 0777 $DIR/${tdir}-1
1480
1481         # hardlimit should have been fetched by slave during global
1482         # reintegration, create will exceed quota
1483         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1484                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1485
1486         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1487         wait_delete_completed
1488         sync_all_data || true
1489
1490         echo "Stop mds${MDSCOUNT}..."
1491         stop mds${MDSCOUNT}
1492
1493         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1494                 error "clear quota failed"
1495
1496         echo "Start mds${MDSCOUNT}..."
1497         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1498         quota_init
1499
1500         wait_mdt_reint $QTYPE || error "reintegration failed"
1501
1502         # hardlimit should be cleared on slave during reintegration
1503         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1504                 quota_error u $TSTUSR "create failed, expect success"
1505
1506         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1507         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1508
1509         cleanup_quota_test
1510         resetquota -u $TSTUSR
1511 }
1512 run_test 7e "Quota reintegration (inode limits)"
1513
1514 # run dbench with quota enabled
1515 test_8() {
1516         local BLK_LIMIT="100g" #100G
1517         local FILE_LIMIT=1000000
1518
1519         setup_quota_test || error "setup quota failed with $?"
1520         trap cleanup_quota_test EXIT
1521
1522         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1523         set_ost_qtype $QTYPE || error "enable ost quota failed"
1524
1525         echo "Set enough high limit for user: $TSTUSR"
1526         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1527                 error "set user quota failed"
1528         echo "Set enough high limit for group: $TSTUSR"
1529         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1530                 error "set group quota failed"
1531         if is_project_quota_supported; then
1532                 change_project -sp $TSTPRJID $DIR/$tdir
1533                 echo "Set enough high limit for project: $TSTPRJID"
1534                 $LFS setquota -p $TSTPRJID -b 0 \
1535                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1536                         error "set project quota failed"
1537         fi
1538
1539         local duration=""
1540         [ "$SLOW" = "no" ] && duration=" -t 120"
1541         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1542                 quota_error a $TSTUSR "dbench failed!"
1543
1544         is_project_quota_supported && change_project -C $DIR/$tdir
1545         cleanup_quota_test
1546         resetquota -u $TSTUSR
1547         resetquota -g $TSTUSR
1548         resetquota -p $TSTPRJID
1549 }
1550 run_test 8 "Run dbench with quota enabled"
1551
1552 # this check is just for test_9
1553 OST0_MIN=4900000 #4.67G
1554
1555 check_whether_skip () {
1556         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1557         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1558         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1559                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1560                 return 0
1561         else
1562                 return 1
1563         fi
1564 }
1565
1566 # run for fixing bug10707, it needs a big room. test for 64bit
1567 test_9() {
1568         local filesize=$((1024 * 9 / 2)) # 4.5G
1569
1570         check_whether_skip && return 0
1571
1572         setup_quota_test || error "setup quota failed with $?"
1573         trap cleanup_quota_test EXIT
1574
1575         set_ost_qtype "ug" || error "enable ost quota failed"
1576
1577         local TESTFILE="$DIR/$tdir/$tfile-0"
1578         local BLK_LIMIT=100G #100G
1579         local FILE_LIMIT=1000000
1580
1581         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1582
1583         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1584                 "for user: $TSTUSR"
1585         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1586                 error "set user quota failed"
1587
1588         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1589                 "for group: $TSTUSR"
1590         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1591                 error "set group quota failed"
1592
1593         quota_show_check a u $TSTUSR
1594         quota_show_check a g $TSTUSR
1595
1596         echo "Create test file"
1597         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1598         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1599
1600         log "Write the big file of 4.5G ..."
1601         $RUNAS $DD of=$TESTFILE count=$filesize ||
1602                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1603
1604         $SHOW_QUOTA_USER
1605         $SHOW_QUOTA_GROUP
1606
1607         cleanup_quota_test
1608         resetquota -u $TSTUSR
1609         resetquota -g $TSTUSR
1610
1611         $SHOW_QUOTA_USER
1612         $SHOW_QUOTA_GROUP
1613 }
1614 run_test 9 "Block limit larger than 4GB (b10707)"
1615
1616 test_10() {
1617         local TESTFILE=$DIR/$tdir/$tfile
1618
1619         setup_quota_test || error "setup quota failed with $?"
1620         trap cleanup_quota_test EXIT
1621
1622         # set limit to root user should fail
1623         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1624                 error "set limit for root user successfully, expect failure"
1625         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1626                 error "set limit for root group successfully, expect failure"
1627         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
1628                 error "set limit for project 0 successfully, expect failure"
1629
1630         # root user can overrun quota
1631         set_ost_qtype "ug" || error "enable ost quota failed"
1632
1633         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1634                 error "set quota failed"
1635         quota_show_check b u $TSTUSR
1636
1637         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1638         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1639
1640         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1641                 error "write failure, expect success"
1642
1643         cleanup_quota_test
1644         resetquota -u $TSTUSR
1645 }
1646 run_test 10 "Test quota for root user"
1647
1648 test_11() {
1649         local TESTFILE=$DIR/$tdir/$tfile
1650         setup_quota_test || error "setup quota failed with $?"
1651         trap cleanup_quota_test EXIT
1652
1653         set_mdt_qtype "ug" || error "enable mdt quota failed"
1654         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1655                 error "set quota failed"
1656
1657         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1658         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1659
1660         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1661         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1662
1663         $SHOW_QUOTA_USER
1664         local USED=$(getquota -u $TSTUSR global curinodes)
1665         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1666
1667         cleanup_quota_test
1668         resetquota -u $TSTUSR
1669 }
1670 run_test 11 "Chown/chgrp ignores quota"
1671
1672 test_12a() {
1673         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs" && return
1674
1675         local blimit=22 # 22M
1676         local blk_cnt=$((blimit - 5))
1677         local TESTFILE0="$DIR/$tdir/$tfile"-0
1678         local TESTFILE1="$DIR/$tdir/$tfile"-1
1679
1680         setup_quota_test || error "setup quota failed with $?"
1681         trap cleanup_quota_test EXIT
1682
1683         set_ost_qtype "u" || error "enable ost quota failed"
1684         quota_show_check b u $TSTUSR
1685
1686         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1687                 error "set quota failed"
1688
1689         $SETSTRIPE $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1690         $SETSTRIPE $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1691         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1692         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1693
1694         echo "Write to ost0..."
1695         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1696                 quota_error a $TSTUSR "dd failed"
1697
1698         echo "Write to ost1..."
1699         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1700                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1701
1702         echo "Free space from ost0..."
1703         rm -f $TESTFILE0
1704         wait_delete_completed
1705         sync_all_data || true
1706
1707         echo "Write to ost1 after space freed from ost0..."
1708         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1709                 quota_error a $TSTUSR "rebalancing failed"
1710
1711         cleanup_quota_test
1712         resetquota -u $TSTUSR
1713 }
1714 run_test 12a "Block quota rebalancing"
1715
1716 test_12b() {
1717         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs" && return
1718
1719         local ilimit=$((1024 * 2)) # 2k inodes
1720         local TESTFILE0=$DIR/$tdir/$tfile
1721         local TESTFILE1=$DIR/${tdir}-1/$tfile
1722
1723         setup_quota_test || error "setup quota failed with $?"
1724         trap cleanup_quota_test EXIT
1725
1726         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1727         chmod 0777 $DIR/${tdir}-1
1728
1729         set_mdt_qtype "u" || error "enable mdt quota failed"
1730         quota_show_check f u $TSTUSR
1731
1732         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1733                 error "set quota failed"
1734
1735         echo "Create $ilimit files on mdt0..."
1736         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1737                 quota_error u $TSTUSR "create failed, but expect success"
1738
1739         echo "Create files on mdt1..."
1740         $RUNAS createmany -m $TESTFILE1 1 &&
1741                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1742
1743         echo "Free space from mdt0..."
1744         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1745         wait_delete_completed
1746         sync_all_data || true
1747
1748         echo "Create files on mdt1 after space freed from mdt0..."
1749         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1750                 quota_error a $TSTUSR "rebalancing failed"
1751
1752         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1753                 error "unlink mdt1 files failed"
1754         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1755
1756         cleanup_quota_test
1757         resetquota -u $TSTUSR
1758 }
1759 run_test 12b "Inode quota rebalancing"
1760
1761 test_13(){
1762         local TESTFILE=$DIR/$tdir/$tfile
1763         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1764         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1765
1766         setup_quota_test || error "setup quota failed with $?"
1767         trap cleanup_quota_test EXIT
1768
1769         set_ost_qtype "u" || error "enable ost quota failed"
1770         quota_show_check b u $TSTUSR
1771
1772         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1773                 error "set quota failed"
1774         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1775         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1776
1777         # clear the locks in cache first
1778         do_facet ost1 $LCTL set_param -n $procf=clear
1779         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1780         [ $nlock -eq 0 ] || error "$nlock cached locks"
1781
1782         # write to acquire the per-ID lock
1783         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1784                 quota_error a $TSTUSR "dd failed"
1785
1786         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1787         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1788
1789         # clear quota doesn't trigger per-ID lock cancellation
1790         resetquota -u $TSTUSR
1791         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1792         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1793
1794         # clear the per-ID lock
1795         do_facet ost1 $LCTL set_param -n $procf=clear
1796         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1797         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1798
1799         # spare quota should be released
1800         local OSTUUID=$(ostuuid_from_index 0)
1801         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1802         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1803         [ $limit -le $space ] ||
1804                 error "spare quota isn't released, limit:$limit, space:$space"
1805
1806         cleanup_quota_test
1807 }
1808 run_test 13 "Cancel per-ID lock in the LRU list"
1809
1810 test_15(){
1811         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1812
1813         wait_delete_completed
1814         sync_all_data || true
1815
1816         # test for user
1817         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1818                 error "set user quota failed"
1819         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1820         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1821                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1822         resetquota -u $TSTUSR
1823
1824         # test for group
1825         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1826                 error "set group quota failed"
1827         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1828         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1829                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1830         resetquota -g $TSTUSR
1831 }
1832 run_test 15 "Set over 4T block quota"
1833
1834 test_17sub() {
1835         local err_code=$1
1836         local BLKS=1    # 1M less than limit
1837         local TESTFILE=$DIR/$tdir/$tfile
1838
1839         setup_quota_test || error "setup quota failed with $?"
1840         trap cleanup_quota_test EXIT
1841
1842         # make sure the system is clean
1843         local USED=$(getquota -u $TSTUSR global curspace)
1844         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1845
1846         set_ost_qtype "ug" || error "enable ost quota failed"
1847         # make sure no granted quota on ost
1848         resetquota -u $TSTUSR
1849         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1850                 error "set quota failed"
1851
1852         quota_show_check b u $TSTUSR
1853
1854         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1855         lustre_fail mds 0xa04 $err_code
1856
1857         # write in background
1858         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1859         local DDPID=$!
1860
1861         sleep 2
1862         # write should be blocked and never finished
1863         if ! ps -p $DDPID  > /dev/null 2>&1; then
1864                 lustre_fail mds 0 0
1865                 quota_error u $TSTUSR "write finished incorrectly!"
1866         fi
1867
1868         lustre_fail mds 0 0
1869
1870         local count=0
1871         local timeout=30
1872         while [ true ]; do
1873                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1874                 count=$((count+1))
1875                 if [ $count -gt $timeout ]; then
1876                         quota_error u $TSTUSR "dd is not finished!"
1877                 fi
1878                 sleep 1
1879         done
1880
1881         sync; sync_all_data || true
1882
1883         USED=$(getquota -u $TSTUSR global curspace)
1884         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1885                 "Used space(${USED}K) is less than ${BLKS}M"
1886
1887         cleanup_quota_test
1888         resetquota -u $TSTUSR
1889 }
1890
1891 # DQACQ return recoverable error
1892 test_17() {
1893         echo "DQACQ return -ENOLCK"
1894         #define ENOLCK  37
1895         test_17sub 37 || error "Handle -ENOLCK failed"
1896
1897         echo "DQACQ return -EAGAIN"
1898         #define EAGAIN  11
1899         test_17sub 11 || error "Handle -EAGAIN failed"
1900
1901         echo "DQACQ return -ETIMEDOUT"
1902         #define ETIMEDOUT 110
1903         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1904
1905         echo "DQACQ return -ENOTCONN"
1906         #define ENOTCONN 107
1907         test_17sub 107 || error "Handle -ENOTCONN failed"
1908 }
1909
1910 run_test 17 "DQACQ return recoverable error"
1911
1912 test_18_sub () {
1913         local io_type=$1
1914         local blimit="200m" # 200M
1915         local TESTFILE="$DIR/$tdir/$tfile"
1916
1917         setup_quota_test || error "setup quota failed with $?"
1918         trap cleanup_quota_test EXIT
1919
1920         set_ost_qtype "u" || error "enable ost quota failed"
1921         log "User quota (limit: $blimit)"
1922         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1923                 error "set quota failed"
1924         quota_show_check b u $TSTUSR
1925
1926         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
1927         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1928
1929         local timeout=$(sysctl -n lustre.timeout)
1930
1931         if [ $io_type = "directio" ]; then
1932                 log "Write 100M (directio) ..."
1933                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1934         else
1935                 log "Write 100M (buffered) ..."
1936                 $RUNAS $DD of=$TESTFILE count=100 &
1937         fi
1938         local DDPID=$!
1939
1940         replay_barrier $SINGLEMDS
1941         log "Fail mds for $((2 * timeout)) seconds"
1942         fail $SINGLEMDS $((2 * timeout))
1943
1944         local count=0
1945         if at_is_enabled; then
1946                 timeout=$(at_max_get mds)
1947         else
1948                 timeout=$(lctl get_param -n timeout)
1949         fi
1950
1951         while [ true ]; do
1952                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1953                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1954                         log "it took $count second"
1955                 fi
1956                 sleep 1
1957         done
1958
1959         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1960         sync
1961         cancel_lru_locks mdc
1962         cancel_lru_locks osc
1963         $SHOW_QUOTA_USER
1964
1965         local testfile_size=$(stat -c %s $TESTFILE)
1966         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1967                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1968                         "got ${testfile_size}. Verifying file failed!"
1969         fi
1970         cleanup_quota_test
1971         resetquota -u $TSTUSR
1972 }
1973
1974 # test when mds does failover, the ost still could work well
1975 # this test shouldn't trigger watchdog b=14840
1976 test_18() {
1977         # Clear dmesg so watchdog is not triggered by previous
1978         # test output
1979         do_facet ost1 dmesg -c > /dev/null
1980
1981         test_18_sub normal
1982         test_18_sub directio
1983
1984         # check if watchdog is triggered
1985         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1986         local watchdog=$(awk '/Service thread pid/ && /was inactive/ \
1987                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1988         [ -z "$watchdog" ] || error "$watchdog"
1989         rm -f $TMP/lustre-log-${TESTNAME}.log
1990 }
1991 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1992
1993 test_19() {
1994         local blimit=5 # 5M
1995         local TESTFILE=$DIR/$tdir/$tfile
1996
1997         setup_quota_test || error "setup quota failed with $?"
1998         trap cleanup_quota_test EXIT
1999
2000         set_ost_qtype $QTYPE || error "enable ost quota failed"
2001
2002         # bind file to a single OST
2003         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2004         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2005
2006         echo "Set user quota (limit: ${blimit}M)"
2007         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2008                 error "set user quota failed"
2009         quota_show_check b u $TSTUSR
2010         echo "Update quota limits"
2011         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2012                 error "set group quota failed"
2013         quota_show_check b u $TSTUSR
2014
2015         # first wirte might be cached
2016         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2017         cancel_lru_locks osc
2018         $SHOW_QUOTA_USER
2019         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2020                 quota_error u $TSTUSR "Write success, expect failure"
2021         $SHOW_QUOTA_USER
2022
2023         cleanup_quota_test
2024         resetquota -u $TSTUSR
2025 }
2026 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2027
2028 test_20() { # b15754
2029         local LSTR=(2g 1t 4k 3m) # limits strings
2030         # limits values
2031         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2032                     $((3*1024*1024)))
2033
2034         resetquota -u $TSTUSR
2035
2036         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2037                 $MOUNT || error "could not set quota limits"
2038         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2039                                 --inode-softlimit ${LSTR[2]} \
2040                                 --inode-hardlimit ${LSTR[3]} \
2041                                 $MOUNT || error "could not set quota limits"
2042
2043         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2044                 error "bsoftlimit was not set properly"
2045         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2046                 error "bhardlimit was not set properly"
2047         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2048                 error "isoftlimit was not set properly"
2049         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2050                 error "ihardlimit was not set properly"
2051
2052         resetquota -u $TSTUSR
2053 }
2054 run_test 20 "Test if setquota specifiers work properly (b15754)"
2055
2056 test_21_sub() {
2057         local testfile=$1
2058         local blk_number=$2
2059         local seconds=$3
2060
2061         local time=$(($(date +%s) + seconds))
2062         while [ $(date +%s) -lt $time ]; do
2063                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2064         done
2065 }
2066
2067 # run for fixing bug16053, setquota shouldn't fail when writing and
2068 # deleting are happening
2069 test_21() {
2070         local TESTFILE="$DIR/$tdir/$tfile"
2071         local BLIMIT=10 # 10G
2072         local ILIMIT=1000000
2073
2074         setup_quota_test || error "setup quota failed with $?"
2075         trap cleanup_quota_test EXIT
2076
2077         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2078
2079         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2080         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2081                 error "set user quota failed"
2082         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2083         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2084                 error "set group quota failed"
2085         if is_project_quota_supported; then
2086                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2087                         "project: $TSTPRJID"
2088                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2089                          $MOUNT || error "set project quota failed"
2090         fi
2091
2092         # repeat writing on a 1M file
2093         test_21_sub ${TESTFILE}_1 1 30 &
2094         local DDPID1=$!
2095         # repeat writing on a 128M file
2096         test_21_sub ${TESTFILE}_2 128 30 &
2097         local DDPID2=$!
2098
2099         local time=$(($(date +%s) + 30))
2100         local i=1
2101         while [ $(date +%s) -lt $time ]; do
2102                 log "Set quota for $i times"
2103                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2104                         -I $((ILIMIT + i)) $MOUNT ||
2105                                 error "Set user quota failed"
2106                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2107                         -I $((ILIMIT + i)) $MOUNT ||
2108                                 error "Set group quota failed"
2109                 if is_project_quota_supported; then
2110                         $LFS setquota -p $TSTPRJID -b 0 -B \
2111                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2112                                 error "Set project quota failed"
2113                 fi
2114                 i=$((i+1))
2115                 sleep 1
2116         done
2117
2118         local count=0
2119         while [ true ]; do
2120                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2121                 count=$((count+1))
2122                 if [ $count -gt 60 ]; then
2123                         quota_error a $TSTUSR "dd should be finished!"
2124                 fi
2125                 sleep 1
2126         done
2127         echo "(dd_pid=$DDPID1, time=$count)successful"
2128
2129         count=0
2130         while [ true ]; do
2131                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2132                 count=$((count+1))
2133                 if [ $count -gt 60 ]; then
2134                         quota_error a $TSTUSR "dd should be finished!"
2135                 fi
2136                 sleep 1
2137         done
2138         echo "(dd_pid=$DDPID2, time=$count)successful"
2139
2140         cleanup_quota_test
2141         resetquota -u $TSTUSR
2142         resetquota -g $TSTUSR
2143         resetquota -p $TSTPRJID
2144 }
2145 run_test 21 "Setquota while writing & deleting (b16053)"
2146
2147 # enable/disable quota enforcement permanently
2148 test_22() {
2149         echo "Set both mdt & ost quota type as ug"
2150         local qtype="ug"
2151         is_project_quota_supported && qtype=$QTYPE
2152         set_mdt_qtype $qtype || error "enable mdt quota failed"
2153         set_ost_qtype $qtype || error "enable ost quota failed"
2154
2155         echo "Restart..."
2156         stopall || error "failed to stopall (1)"
2157         mount
2158         setupall
2159
2160         echo "Verify if quota is enabled"
2161         local qtype1=$(mdt_quota_type)
2162         [ $qtype1 != $qtype] && error "mdt quota setting is lost"
2163         qtype=$(ost_quota_type)
2164         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2165
2166         echo "Set both mdt & ost quota type as none"
2167         set_mdt_qtype "none" || error "disable mdt quota failed"
2168         set_ost_qtype "none" || error "disable ost quota failed"
2169
2170         echo "Restart..."
2171         stopall || error "failed to stopall (2)"
2172         mount
2173         setupall
2174         quota_init
2175
2176         echo "Verify if quota is disabled"
2177         qtype=$(mdt_quota_type)
2178         [ $qtype != "none" ] && error "mdt quota setting is lost"
2179         qtype=$(ost_quota_type)
2180         [ $qtype != "none" ] && error "ost quota setting is lost"
2181
2182         return 0
2183 }
2184 run_test 22 "enable/disable quota by 'lctl conf_param'"
2185
2186 test_23_sub() {
2187         local TESTFILE="$DIR/$tdir/$tfile"
2188         local LIMIT=$1
2189
2190         setup_quota_test || error "setup quota failed with $?"
2191         trap cleanup_quota_test EXIT
2192
2193         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2194
2195         # test for user
2196         log "User quota (limit: $LIMIT MB)"
2197         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2198                 error "set quota failed"
2199         quota_show_check b u $TSTUSR
2200
2201         $SETSTRIPE $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2202         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2203
2204         log "Step1: trigger EDQUOT with O_DIRECT"
2205         log "Write half of file"
2206         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2207                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2208                         "limit=$LIMIT"
2209         log "Write out of block quota ..."
2210         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2211                 oflag=direct conv=notrunc &&
2212                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2213                         "limit=$LIMIT"
2214         log "Step1: done"
2215
2216         log "Step2: rewrite should succeed"
2217         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2218                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2219                         "limit=$LIMIT"
2220         log "Step2: done"
2221
2222         cleanup_quota_test
2223
2224         local OST0_UUID=$(ostuuid_from_index 0)
2225         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2226         [ $OST0_QUOTA_USED -ne 0 ] &&
2227                 ($SHOW_QUOTA_USER; \
2228                 quota_error u $TSTUSR "quota isn't released")
2229         $SHOW_QUOTA_USER
2230         resetquota -u $TSTUSR
2231 }
2232
2233 test_23() {
2234         [ $(facet_fstype ost1) == "zfs" ] &&
2235                 skip "Overwrite in place is not guaranteed to be " \
2236                 "space neutral on ZFS" && return
2237
2238         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2239         check_whether_skip && return 0
2240         log "run for 4MB test file"
2241         test_23_sub 4
2242
2243         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2244         check_whether_skip && return 0
2245         log "run for 40MB test file"
2246         test_23_sub 40
2247 }
2248 run_test 23 "Quota should be honored with directIO (b16125)"
2249
2250 test_24() {
2251         local blimit=5 # 5M
2252         local TESTFILE="$DIR/$tdir/$tfile"
2253
2254         setup_quota_test || error "setup quota failed with $?"
2255         trap cleanup_quota_test EXIT
2256
2257         set_ost_qtype $QTYPE || error "enable ost quota failed"
2258
2259         # bind file to a single OST
2260         $SETSTRIPE -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2261         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2262
2263         echo "Set user quota (limit: ${blimit}M)"
2264         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2265                 error "set quota failed"
2266
2267         # overrun quota by root user
2268         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2269                 error "write failure, expect success"
2270         cancel_lru_locks osc
2271         sync_all_data || true
2272
2273         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2274
2275         cleanup_quota_test
2276         resetquota -u $TSTUSR
2277 }
2278 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2279
2280 test_27a() { # b19612
2281         $LFS quota $TSTUSR $DIR &&
2282                 error "lfs succeeded with no type, but should have failed"
2283         $LFS setquota $TSTUSR $DIR &&
2284                 error "lfs succeeded with no type, but should have failed"
2285         return 0
2286 }
2287 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2288
2289 test_27b() { # b20200
2290         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2291                 error "lfs setquota failed with uid argument"
2292         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2293                 error "lfs stequota failed with gid argument"
2294         if is_project_quota_supported; then
2295                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2296                         1000 $DIR || error \
2297                                 "lfs stequota failed with projid argument"
2298         fi
2299         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2300         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2301         if is_project_quota_supported; then
2302                 $SHOW_QUOTA_PROJID ||
2303                         error "lfs quota failed with projid argument"
2304         fi
2305         resetquota -u $TSTUSR
2306         resetquota -g $TSTUSR
2307         resetquota -p $TSTPRJID
2308         return 0
2309 }
2310 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2311
2312 test_27c() {
2313         local limit
2314
2315         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2316                 error "lfs setquota failed"
2317
2318         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2319         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2320         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2321         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2322
2323         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2324                 error "lfs setquota for $TSTID failed"
2325
2326         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2327         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2328         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2329         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2330
2331         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2332                 error "total allocated inode/block limit not printed"
2333
2334         resetquota -u $TSTUSR
2335 }
2336 run_test 27c "lfs quota should support human-readable output"
2337
2338 test_27d() {
2339         local softlimit=1.5
2340         local hardlimit=2.3
2341         local limit
2342
2343         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2344                 error "set fraction block limit failed"
2345         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2346         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2347         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2348         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2349
2350         resetquota -u $TSTUSR
2351 }
2352 run_test 27d "lfs setquota should support fraction block limit"
2353
2354 test_30() {
2355         local LIMIT=4 # 4MB
2356         local TESTFILE="$DIR/$tdir/$tfile"
2357         local GRACE=10
2358
2359         setup_quota_test || error "setup quota failed with $?"
2360         trap cleanup_quota_test EXIT
2361
2362         set_ost_qtype "u" || error "enable ost quota failed"
2363
2364         $SETSTRIPE $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2365         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2366
2367         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2368                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2369         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2370                 error "set quota failed"
2371         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2372         cancel_lru_locks osc
2373         sleep $GRACE
2374         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2375         # over-quota flag has not yet settled since we do not trigger async
2376         # events based on grace time period expiration
2377         $SHOW_QUOTA_USER
2378         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2379         cancel_lru_locks osc
2380         # now over-quota flag should be settled and further writes should fail
2381         $SHOW_QUOTA_USER
2382         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2383                 error "grace times were reset"
2384         # cleanup
2385         cleanup_quota_test
2386         resetquota -u $TSTUSR
2387         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2388                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2389 }
2390 run_test 30 "Hard limit updates should not reset grace times"
2391
2392 # basic usage tracking for user & group
2393 test_33() {
2394         local INODES=10 # 10 files
2395         local BLK_CNT=2 # of 2M each
2396         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2397
2398         setup_quota_test || error "setup quota failed with $?"
2399         trap cleanup_quota_test EXIT
2400
2401         # make sure the system is clean
2402         local USED=$(getquota -u $TSTID global curspace)
2403         [ $USED -ne 0 ] &&
2404                 error "Used space ($USED) for user $TSTID isn't 0."
2405         USED=$(getquota -g $TSTID global curspace)
2406         [ $USED -ne 0 ] &&
2407                 error "Used space ($USED) for group $TSTID isn't 0."
2408         if is_project_quota_supported; then
2409                 USED=$(getquota -p $TSTPRJID global curspace)
2410                 [ $USED -ne 0 ] && error \
2411                         "Used space ($USED) for project $TSTPRJID isn't 0."
2412         fi
2413
2414         echo "Write files..."
2415         for i in $(seq 0 $INODES); do
2416                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2417                         error "write failed"
2418                         is_project_quota_supported &&
2419                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
2420                 echo "Iteration $i/$INODES completed"
2421         done
2422         cancel_lru_locks osc
2423
2424         echo "Wait for setattr on objects finished..."
2425         wait_delete_completed
2426
2427         sync; sync_all_data || true
2428
2429         echo "Verify disk usage after write"
2430         USED=$(getquota -u $TSTID global curspace)
2431         [ $USED -lt $TOTAL_BLKS ] &&
2432                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2433         USED=$(getquota -g $TSTID global curspace)
2434         [ $USED -lt $TOTAL_BLKS ] &&
2435                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2436         if is_project_quota_supported; then
2437                 USED=$(getquota -p $TSTPRJID global curspace)
2438                 [ $USED -lt $TOTAL_BLKS ] && error \
2439                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
2440         fi
2441
2442         echo "Verify inode usage after write"
2443         USED=$(getquota -u $TSTID global curinodes)
2444         [ $USED -lt $INODES ] &&
2445                 error "Used inode for user $TSTID is $USED, expected $INODES"
2446         USED=$(getquota -g $TSTID global curinodes)
2447         [ $USED -lt $INODES ] &&
2448                 error "Used inode for group $TSTID is $USED, expected $INODES"
2449         if is_project_quota_supported; then
2450                 USED=$(getquota -p $TSTPRJID global curinodes)
2451                 [ $USED -lt $INODES ] && error \
2452                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
2453         fi
2454
2455         cleanup_quota_test
2456
2457         echo "Verify disk usage after delete"
2458         USED=$(getquota -u $TSTID global curspace)
2459         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2460         USED=$(getquota -u $TSTID global curinodes)
2461         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2462         USED=$(getquota -g $TSTID global curspace)
2463         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2464         USED=$(getquota -g $TSTID global curinodes)
2465         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2466         if is_project_quota_supported; then
2467                 USED=$(getquota -p $TSTPRJID global curspace)
2468                 [ $USED -eq 0 ] ||
2469                         error "Used space for project $TSTPRJID isn't 0. $USED"
2470                 USED=$(getquota -p $TSTPRJID global curinodes)
2471                 [ $USED -eq 0 ] ||
2472                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
2473         fi
2474 }
2475 run_test 33 "Basic usage tracking for user & group & project"
2476
2477 # usage transfer test for user & group & project
2478 test_34() {
2479         local BLK_CNT=2 # 2MB
2480         local project_supported="no"
2481
2482         is_project_quota_supported && project_supported="yes"
2483         setup_quota_test || error "setup quota failed with $?"
2484         trap cleanup_quota_test EXIT
2485
2486         # make sure the system is clean
2487         local USED=$(getquota -u $TSTID global curspace)
2488         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2489         USED=$(getquota -g $TSTID global curspace)
2490         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2491
2492         local USED=$(getquota -u $TSTID2 global curspace)
2493         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2494         if [ $project_supported == "yes" ]; then
2495                 USED=$(getquota -p $TSTPRJID global curspace)
2496                 [ $USED -ne 0 ] && error \
2497                         "Used space ($USED) for Project $TSTPRJID isn't 0."
2498         fi
2499
2500         echo "Write file..."
2501         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2502                 error "write failed"
2503         cancel_lru_locks osc
2504         sync; sync_all_data || true
2505
2506         echo "chown the file to user $TSTID"
2507         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2508
2509         echo "Wait for setattr on objects finished..."
2510         wait_delete_completed
2511
2512         BLK_CNT=$((BLK_CNT * 1024))
2513
2514         echo "Verify disk usage for user $TSTID"
2515         USED=$(getquota -u $TSTID global curspace)
2516         [ $USED -lt $BLK_CNT ] &&
2517                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2518         USED=$(getquota -u $TSTID global curinodes)
2519         [ $USED -ne 1 ] &&
2520                 error "Used inodes for user $TSTID is $USED, expected 1"
2521
2522         echo "chgrp the file to group $TSTID"
2523         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2524
2525         echo "Wait for setattr on objects finished..."
2526         wait_delete_completed
2527
2528         echo "Verify disk usage for group $TSTID"
2529         USED=$(getquota -g $TSTID global curspace)
2530         [ $USED -ge $BLK_CNT ] ||
2531                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2532         USED=$(getquota -g $TSTID global curinodes)
2533         [ $USED -eq 1 ] ||
2534                 error "Used inodes for group $TSTID is $USED, expected 1"
2535
2536         # chown won't change the ost object group. LU-4345 */
2537         echo "chown the file to user $TSTID2"
2538         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2539
2540         echo "Wait for setattr on objects finished..."
2541         wait_delete_completed
2542
2543         echo "change_project project id to $TSTPRJID"
2544         [ $project_supported == "yes" ] &&
2545                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2546         echo "Wait for setattr on objects finished..."
2547         wait_delete_completed
2548
2549         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2550         USED=$(getquota -u $TSTID2 global curspace)
2551         [ $USED -lt $BLK_CNT ] &&
2552                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2553         USED=$(getquota -u $TSTID global curspace)
2554         [ $USED -ne 0 ] &&
2555                 error "Used space for user $TSTID is $USED, expected 0"
2556         USED=$(getquota -g $TSTID global curspace)
2557         [ $USED -lt $BLK_CNT ] &&
2558                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2559         if [ $project_supported == "yes" ]; then
2560                 USED=$(getquota -p $TSTPRJID global curspace)
2561                 [ $USED -lt $BLK_CNT ] && error \
2562                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
2563         fi
2564
2565         cleanup_quota_test
2566 }
2567 run_test 34 "Usage transfer for user & group & project"
2568
2569 # usage is still accessible across restart
2570 test_35() {
2571         local BLK_CNT=2 # 2 MB
2572
2573         setup_quota_test || error "setup quota failed with $?"
2574         trap cleanup_quota_test EXIT
2575
2576         echo "Write file..."
2577         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2578                 error "write failed"
2579         is_project_quota_supported &&
2580                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2581         cancel_lru_locks osc
2582
2583         echo "Wait for setattr on objects finished..."
2584         wait_delete_completed
2585
2586         sync; sync_all_data || true
2587
2588         echo "Save disk usage before restart"
2589         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2590         [ $ORIG_USR_SPACE -eq 0 ] &&
2591                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2592         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2593         [ $ORIG_USR_INODES -eq 0 ] &&
2594                 error "Used inodes for user $TSTID is 0, expected 1"
2595         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2596         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2597         [ $ORIG_GRP_SPACE -eq 0 ] &&
2598                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2599         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2600         [ $ORIG_GRP_INODES -eq 0 ] &&
2601                 error "Used inodes for group $TSTID is 0, expected 1"
2602         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2603
2604         if is_project_quota_supported; then
2605                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
2606                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
2607                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
2608                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
2609                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
2610                         "Used inodes for project $TSTPRJID is 0, expected 1"
2611                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
2612         fi
2613
2614         log "Restart..."
2615         stopall
2616         setupall
2617         quota_init
2618
2619         echo "Verify disk usage after restart"
2620         local USED=$(getquota -u $TSTID global curspace)
2621         [ $USED -eq $ORIG_USR_SPACE ] ||
2622                 error "Used space for user $TSTID changed from " \
2623                         "$ORIG_USR_SPACE to $USED"
2624         USED=$(getquota -u $TSTID global curinodes)
2625         [ $USED -eq $ORIG_USR_INODES ] ||
2626                 error "Used inodes for user $TSTID changed from " \
2627                         "$ORIG_USR_INODES to $USED"
2628         USED=$(getquota -g $TSTID global curspace)
2629         [ $USED -eq $ORIG_GRP_SPACE ] ||
2630                 error "Used space for group $TSTID changed from " \
2631                         "$ORIG_GRP_SPACE to $USED"
2632         USED=$(getquota -g $TSTID global curinodes)
2633         [ $USED -eq $ORIG_GRP_INODES ] ||
2634                 error "Used inodes for group $TSTID changed from " \
2635                         "$ORIG_GRP_INODES to $USED"
2636         if [ $project_supported == "yes" ]; then
2637                 USED=$(getquota -p $TSTPRJID global curinodes)
2638                 [ $USED -eq $ORIG_PRJ_INODES ] ||
2639                         error "Used inodes for project $TSTPRJID " \
2640                                 "changed from $ORIG_PRJ_INODES to $USED"
2641                 USED=$(getquota -p $TSTPRJID global curspace)
2642                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
2643                         error "Used space for project $TSTPRJID "\
2644                                 "changed from $ORIG_PRJ_SPACE to $USED"
2645         fi
2646
2647         # check if the vfs_dq_init() is called before writing
2648         echo "Append to the same file..."
2649         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2650                 error "write failed"
2651         cancel_lru_locks osc
2652         sync; sync_all_data || true
2653
2654         echo "Verify space usage is increased"
2655         USED=$(getquota -u $TSTID global curspace)
2656         [ $USED -gt $ORIG_USR_SPACE ] ||
2657                 error "Used space for user $TSTID isn't increased" \
2658                         "orig:$ORIG_USR_SPACE, now:$USED"
2659         USED=$(getquota -g $TSTID global curspace)
2660         [ $USED -gt $ORIG_GRP_SPACE ] ||
2661                 error "Used space for group $TSTID isn't increased" \
2662                         "orig:$ORIG_GRP_SPACE, now:$USED"
2663         if [ $project_supported == "yes" ]; then
2664                 USED=$(getquota -p $TSTPRJID global curspace)
2665                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
2666                         error "Used space for project $TSTPRJID isn't " \
2667                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
2668         fi
2669
2670         cleanup_quota_test
2671 }
2672 run_test 35 "Usage is still accessible across reboot"
2673
2674 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2675 # LU-5006
2676 test_37() {
2677         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
2678                 skip "Old server doesn't have LU-5006 fix." && return
2679
2680         setup_quota_test || error "setup quota failed with $?"
2681         trap cleanup_quota_test EXIT
2682
2683         # make sure the system is clean
2684         local USED=$(getquota -u $TSTID global curspace)
2685         [ $USED -ne 0 ] &&
2686                 error "Used space ($USED) for user $TSTID isn't 0."
2687
2688         # create file with MDS_OPEN_DELAY_CREATE flag
2689         $SETSTRIPE -c 1 -i 0 $DIR/$tdir/$tfile ||
2690                 error "Create file failed"
2691         # write to file
2692         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2693                 oflag=sync || error "Write file failed"
2694         # chown to the file
2695         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2696
2697         # wait for setattr on objects finished..."
2698         wait_delete_completed
2699
2700         USED=$(getquota -u $TSTID global curspace)
2701         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2702
2703         cleanup_quota_test
2704 }
2705 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2706
2707 # LU-8801
2708 test_38() {
2709         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.60) ] &&
2710                 skip "Old server doesn't have LU-8801 fix." && return
2711
2712         [ "$UID" != 0 ] && skip_env "must run as root" && return
2713
2714         setup_quota_test || error "setup quota failed with $?"
2715         trap cleanup_quota_test EXIT
2716
2717         # make sure the system is clean
2718         local USED=$(getquota -u $TSTID global curspace)
2719         [ $USED -ne 0 ] &&
2720                 error "Used space ($USED) for user $TSTID isn't 0."
2721         USED=$(getquota -u $TSTID2 global curspace)
2722         [ $USED -ne 0 ] &&
2723                 error "Used space ($USED) for user $TSTID2 isn't 0."
2724
2725         local TESTFILE="$DIR/$tdir/$tfile"
2726         local file_cnt=10000
2727
2728         # Generate id entries in accounting file
2729         echo "Create $file_cnt files..."
2730         for i in `seq $file_cnt`; do
2731                 touch $TESTFILE-$i
2732                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
2733                         error "failed to chown $TESTFILE-$i"
2734         done
2735         cancel_lru_locks osc
2736         sync; sync_all_data || true
2737
2738         local procf="osd-$(facet_fstype $SINGLEMDS).$FSNAME-MDT0000"
2739         procf=${procf}.quota_slave.acct_user
2740         local accnt_cnt
2741
2742         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
2743                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
2744         echo "Found $acct_cnt id entries"
2745
2746         [ $file_cnt -eq $acct_cnt ] || {
2747                 do_facet mds1 $LCTL get_param $procf
2748                 error "skipped id entries"
2749         }
2750
2751         cleanup_quota_test
2752 }
2753 run_test 38 "Quota accounting iterator doesn't skip id entries"
2754
2755 test_39() {
2756         local TESTFILE="$DIR/$tdir/project"
2757         ! is_project_quota_supported &&
2758                 skip "Project quota is not supported" && return 0
2759
2760         setup_quota_test || error "setup quota failed with $?"
2761
2762         touch $TESTFILE
2763         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2764         [ $projectid -ne 0 ] &&
2765                 error "Project id should be 0 not $projectid"
2766         change_project -p 1024 $TESTFILE
2767         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2768         [ $projectid -ne 1024 ] &&
2769                 error "Project id should be 1024 not $projectid"
2770
2771         stopall || error "failed to stopall (1)"
2772         mount
2773         setupall
2774         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2775         [ $projectid -ne 1024 ] &&
2776                 error "Project id should be 1024 not $projectid"
2777
2778         cleanup_quota_test
2779 }
2780 run_test 39 "Project ID interface works correctly"
2781
2782 test_40a() {
2783         ! is_project_quota_supported &&
2784                 skip "Project quota is not supported" && return 0
2785         local dir1="$DIR/$tdir/dir1"
2786         local dir2="$DIR/$tdir/dir2"
2787
2788         setup_quota_test || error "setup quota failed with $?"
2789
2790         mkdir -p $dir1 $dir2
2791         change_project -sp 1 $dir1 && touch $dir1/1
2792         change_project -sp 2 $dir2
2793
2794         ln $dir1/1 $dir2/1_link &&
2795                 error "Hard link across different project quota should fail"
2796         rm -rf $dir1 $dir2
2797
2798         cleanup_quota_test
2799 }
2800 run_test 40a "Hard link across different project ID"
2801
2802 test_40b() {
2803         ! is_project_quota_supported &&
2804                 skip "Project quota is not supported" && return 0
2805         local dir1="$DIR/$tdir/dir1"
2806         local dir2="$DIR/$tdir/dir2"
2807
2808         setup_quota_test || error "setup quota failed with $?"
2809         mkdir -p $dir1 $dir2
2810         change_project -sp 1 $dir1 && touch $dir1/1
2811         change_project -sp 2 $dir2
2812
2813         mv $dir1/1 $dir2/2 || error "mv failed $?"
2814         local projid=$(lfs project $dir2/2 | awk '{print $1}')
2815         if [ "$projid" != "2" ]; then
2816                 error "project id expected 2 not $projid"
2817         fi
2818         rm -rf $dir1 $dir2
2819         cleanup_quota_test
2820 }
2821 run_test 40b "Mv across different project ID"
2822
2823 test_40c() {
2824         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs" && return
2825                 ! is_project_quota_supported &&
2826                         skip "Project quota is not supported" && return 0
2827
2828         setup_quota_test || error "setup quota failed with $?"
2829         local dir="$DIR/$tdir/dir"
2830
2831         mkdir -p $dir && change_project -sp 1 $dir
2832         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
2833         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
2834         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
2835         touch $dir/remote_dir/file
2836         #verify inherit works file for remote dir.
2837         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
2838         [ "$projid" != "1" ] &&
2839                 error "file under remote dir expected 1 not $projid"
2840
2841         #Agent inode should be ignored for project quota
2842         USED=$(getquota -p 1 global curinodes)
2843         [ "$USED" != "3" ] &&
2844                 error "file count expected 3 got $USED"
2845
2846         rm -rf $dir
2847         cleanup_quota_test
2848         return 0
2849 }
2850 run_test 40c "Remote child Dir inherit project quota properly"
2851
2852 test_50() {
2853         ! is_project_quota_supported &&
2854                 skip "Project quota is not supported" && return 0
2855
2856         setup_quota_test || error "setup quota failed with $?"
2857         local dir="$DIR/$tdir/dir"
2858
2859         mkdir $dir && change_project -p 1 $dir
2860         count=$($LFS find --projid 1 $DIR | wc -l)
2861         [ "$count" != 1 ] && error "expected 1 but got $count"
2862
2863         rm -rf $dir
2864         cleanup_quota_test
2865 }
2866 run_test 50 "Test if lfs find --projid works"
2867
2868 test_51() {
2869         ! is_project_quota_supported &&
2870                 skip "Project quota is not supported" && return 0
2871         setup_quota_test || error "setup quota failed with $?"
2872         local dir="$DIR/$tdir/dir"
2873
2874         mkdir $dir && change_project -sp 1 $dir
2875         local used=$(getquota -p 1 global curinodes)
2876         [ $used != "1" ] && error "expected 1 got $used"
2877
2878         touch $dir/1
2879         touch $dir/2
2880         cp $dir/2 $dir/3
2881         used=$(getquota -p 1 global curinodes)
2882         [ $used != "4" ] && error "expected 4 got $used"
2883
2884         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
2885         #try cp to dir
2886         cp $DIR/$tdir/6 $dir/6
2887         used=$(getquota -p 1 global curinodes)
2888         [ $used != "5" ] && error "expected 5 got $used"
2889
2890         #try mv to dir
2891         mv $DIR/$tdir/6 $dir/7
2892         used=$(getquota -p 1 global curinodes)
2893         [ $used != "6" ] && error "expected 6 got $used"
2894
2895         rm -rf $dir
2896         cleanup_quota_test
2897 }
2898 run_test 51 "Test project accounting with mv/cp"
2899
2900 test_52() {
2901         ! is_project_quota_supported &&
2902                 skip "Project quota is not supported" && return 0
2903         setup_quota_test || error "setup quota failed with $?"
2904         local dir="$DIR/$tdir/dir"
2905         mkdir $dir && change_project -sp 1 $dir
2906
2907         touch $DIR/$tdir/file
2908         #Try renaming a file into the project.  This should fail.
2909         for num in $(seq 1 2000); do
2910                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
2911                         error "rename should fail"
2912         done
2913         rm -rf $dir
2914         cleanup_quota_test
2915 }
2916 run_test 52 "Rename across different project ID"
2917
2918 test_53() {
2919         ! is_project_quota_supported &&
2920                 skip "Project quota is not supported" && return 0
2921         setup_quota_test || error "setup quota failed with $?"
2922         local dir="$DIR/$tdir/dir"
2923         mkdir $dir && change_project -s $dir
2924         lfs project -d $dir | grep P || error "inherit attribute should be set"
2925
2926         change_project -C $dir
2927         lfs project -d $dir | grep P &&
2928                 error "inherit attribute should be cleared"
2929
2930         rm -rf $dir
2931         cleanup_quota_test
2932 }
2933 run_test 53 "Project inherit attribute could be cleared"
2934
2935 test_54() {
2936         ! is_project_quota_supported &&
2937                 skip "Project quota is not supported" && return 0
2938         setup_quota_test || error "setup quota failed with $?"
2939         trap cleanup_quota_test EXIT
2940         local testfile="$DIR/$tdir/$tfile-0"
2941
2942         #set project ID/inherit attribute
2943         change_project -sp $TSTPRJID $DIR/$tdir
2944         $RUNAS createmany -m ${testfile} 100 ||
2945                 error "create many files failed"
2946
2947         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
2948         # one more count for directory itself */
2949         ((proj_count++))
2950
2951         #check project
2952         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2953         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
2954
2955         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
2956         [ $proj_count1 -eq $proj_count ] ||
2957                         error "c2: expected $proj_count got $proj_count1"
2958
2959         #clear project but with kept projid
2960         change_project -rCk $DIR/$tdir
2961         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2962         [ $proj_count1 -eq $proj_count ] ||
2963                         error "c3: expected $proj_count got $proj_count1"
2964
2965         #verify projid untouched.
2966         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2967         ((proj_count1++))
2968         [ $proj_count1 -eq $proj_count ] ||
2969                         error "c4: expected $proj_count got $proj_count1"
2970
2971         # test -0 option
2972         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
2973         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2974         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
2975
2976         #this time clear all
2977         change_project -rC $DIR/$tdir
2978         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2979         [ $proj_count1 -eq 0 ] ||
2980                         error "c6: expected 0 got $proj_count1"
2981         #cleanup
2982         unlinkmany ${testfile} 100 ||
2983                 error "unlink many files failed"
2984
2985         cleanup_quota_test
2986 }
2987 run_test 54 "basic lfs project interface test"
2988
2989 test_55() {
2990         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.58) ] &&
2991                 skip "Not supported before 2.10.58." && return
2992         setup_quota_test || error "setup quota failed with $?"
2993
2994         set_ost_qtype $QTYPE || error "enable ost quota failed"
2995         quota_init
2996
2997         #add second group to TSTUSR
2998         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
2999
3000         #prepare test file
3001         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3002         error "failed to dd"
3003
3004         cancel_lru_locks osc
3005         sync; sync_all_data || true
3006
3007         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3008         error "failed to setquota on group $TSTUSR2"
3009
3010         $LFS quota -v -g $TSTUSR2 $DIR
3011
3012         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3013         error "chgrp should failed with -EDQUOT"
3014
3015         USED=$(getquota -g $TSTUSR2 global curspace)
3016         echo "$USED"
3017
3018         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3019         error "failed to setquota on group $TSTUSR2"
3020
3021         $LFS quota -v -g $TSTUSR2 $DIR
3022
3023         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3024         error "chgrp should succeed"
3025
3026         $LFS quota -v -g $TSTUSR2 $DIR
3027
3028         resetquota -g $TSTUSR2
3029         cleanup_quota_test
3030 }
3031 run_test 55 "Chgrp should be affected by group quota"
3032
3033 test_56() {
3034         setup_quota_test || error "setup quota failed with $?"
3035
3036         set_ost_qtype $QTYPE || error "enable ost quota failed"
3037         quota_init
3038
3039         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3040                 erro "failed to set grace time for usr quota"
3041         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3042                      awk '{print $4 $8}')
3043         if [ "x$grace_time" != "x10s;10s" ]; then
3044                 $LFS quota -t -u $DIR
3045                 error "expected grace time: 10s;10s, got:$grace_time"
3046         fi
3047
3048         cleanup_quota_test
3049 }
3050 run_test 56 "lfs quota -t should work well"
3051
3052 test_57() {
3053         setup_quota_test || error "setup quota failed with $?"
3054
3055         local dir="$DIR/$tdir/dir"
3056         mkdir -p $dir
3057         mkfifo $dir/pipe
3058         #try to change pipe file should not hang and return failure
3059         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3060                 awk -F ':' '{ print \\\$2 }'" \
3061                         " failed to get xattr for '$dir/pipe'" || return 1
3062         #command can process further if it hit some errors
3063         touch $dir/aaa $dir/bbb
3064         #create one invalid link file
3065         ln -s $dir/not_exist_file $dir/ccc
3066         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3067         [ $cnt -eq 2 ] || error "expected 2 got $cnt"
3068
3069         cleanup_quota_test
3070 }
3071 run_test 57 "lfs project could tolerate errors"
3072
3073 test_59() {
3074         if [ $(facet_fstype $SINGLEMDS) != ldiskfs ]; then
3075                 skip "ldiskfs only test"
3076                 return
3077         fi
3078         disable_project_quota
3079         setup_quota_test || error "setup quota failed with $?"
3080         quota_init
3081
3082         local testfile="$DIR/$tdir/$tfile-0"
3083         #make sure it did not crash kernel
3084         touch $testfile && lfs project -sp 1 $testfile
3085
3086         enable_project_quota
3087         cleanup_quota_test
3088 }
3089 run_test 59 "lfs project dosen't crash kernel with project disabled"
3090
3091 test_60() {
3092         setup_quota_test || error "setup quota failed with $?"
3093         trap cleanup_quota_test EXIT
3094         local testfile=$DIR/$tdir/$tfile
3095         local limit=100
3096
3097         set_mdt_qtype "ug" || error "enable mdt quota failed"
3098
3099         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3100                 error "set quota failed"
3101         quota_show_check a g $TSTUSR
3102
3103         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3104         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3105         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3106                 error "create many files failed"
3107
3108         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3109
3110         # root user can overrun quota
3111         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3112                 error "root user should succeed"
3113
3114         cleanup_quota_test
3115         resetquota -g $TSTUSR
3116 }
3117 run_test 60 "Test quota for root with setgid"
3118
3119 # test default quota
3120 test_default_quota() {
3121         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.51) ] &&
3122                 skip "Not supported before 2.11.51." && return
3123
3124         local qtype=$1
3125         local qpool=$2
3126         local qid=$TSTUSR
3127         local qprjid=$TSTPRJID
3128         local qdtype="-U"
3129         local qs="-b"
3130         local qh="-B"
3131         local LIMIT=102400 #100M disk space
3132         local TESTFILE="$DIR/$tdir/$tfile-0"
3133
3134         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3135                 echo "Project quota is not supported" && return 0
3136
3137         [ $qtype == "-u" ] && qdtype="-U"
3138         [ $qtype == "-g" ] && qdtype="-G"
3139         [ $qtype == "-p" ] && {
3140                 qdtype="-P"
3141                 qid=$qprjid
3142         }
3143
3144         [ $qpool == "meta" ] && {
3145                 LIMIT=10240 #10K inodes
3146                 qs="-i"
3147                 qh="-I"
3148         }
3149
3150         setup_quota_test || error "setup quota failed with $?"
3151         trap cleanup_quota_test EXIT
3152
3153         quota_init
3154
3155         # enable mdt/ost quota
3156         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3157         set_ost_qtype $QTYPE || error "enable ost quota failed"
3158
3159         log "set to use default quota"
3160         $LFS setquota $qtype $qid -d $DIR ||
3161                 error "set $qid to use default quota failed"
3162
3163         log "set default quota"
3164         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3165                 error "set $qid default quota failed"
3166
3167         log "get default quota"
3168         $LFS quota $qdtype $DIR || error "get default quota failed"
3169
3170         if [ $qpool == "data" ]; then
3171                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3172                                                         awk '{print $2}')
3173                 [ $SLIMIT -eq $LIMIT ] ||
3174                         error "the returned default quota is wrong"
3175         else
3176                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3177                                                         awk '{print $5}')
3178                 [ $SLIMIT -eq $LIMIT ] ||
3179                         error "the returned default quota is wrong"
3180         fi
3181
3182         # make sure the system is clean
3183         local USED=$(getquota $qtype $qid global curspace)
3184         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3185
3186         $SETSTRIPE $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3187         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3188
3189         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3190
3191         log "Test not out of quota"
3192         if [ $qpool == "data" ]; then
3193                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) ||
3194                         quota_error $qtype $qid "write failed, expect succeed"
3195         else
3196                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3197                         quota_error $qtype $qid "create failed, expect succeed"
3198
3199                 unlinkmany $TESTFILE $((LIMIT/2))
3200         fi
3201
3202         log "Test out of quota"
3203         # flush cache, ensure noquota flag is set on client
3204         cancel_lru_locks osc
3205         cancel_lru_locks mdc
3206         sync; sync_all_data || true
3207         if [ $qpool == "data" ]; then
3208                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) &&
3209                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3210         else
3211                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3212                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3213
3214                 unlinkmany $TESTFILE $((LIMIT*2))
3215         fi
3216
3217         log "Increase default quota"
3218         # increase default quota
3219         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3220                 error "set default quota failed"
3221
3222         cancel_lru_locks osc
3223         cancel_lru_locks mdc
3224         sync; sync_all_data || true
3225         if [ $qpool == "data" ]; then
3226                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) ||
3227                         quota_error $qtype $qid "write failed, expect succeed"
3228         else
3229                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3230                         quota_error $qtype $qid "create failed, expect succeed"
3231
3232                 unlinkmany $TESTFILE $((LIMIT*2))
3233         fi
3234
3235         log "Set quota to override default quota"
3236         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3237                 error "set $qid quota failed"
3238
3239         cancel_lru_locks osc
3240         cancel_lru_locks mdc
3241         sync; sync_all_data || true
3242         if [ $qpool == "data" ]; then
3243                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) &&
3244                         quota_error $qtype $qid "write succeed, expect EQUOT"
3245         else
3246                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3247                         quota_error $qtype $qid "create succeed, expect EQUOT"
3248
3249                 unlinkmany $TESTFILE $((LIMIT*2))
3250         fi
3251
3252         log "Set to use default quota again"
3253         $LFS setquota $qtype $qid -d $DIR ||
3254                 error "set $qid to use default quota failed"
3255
3256         cancel_lru_locks osc
3257         cancel_lru_locks mdc
3258         sync; sync_all_data || true
3259         if [ $qpool == "data" ]; then
3260                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) ||
3261                         quota_error $qtype $qid "write failed, expect succeed"
3262         else
3263                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3264                         quota_error $qtype $qid "create failed, expect succeed"
3265
3266                 unlinkmany $TESTFILE $((LIMIT*2))
3267         fi
3268
3269         log "Cleanup"
3270         rm -f $TESTFILE
3271         wait_delete_completed || error "wait_delete_completed failed"
3272         sync_all_data || true
3273         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3274                 error "reset default quota failed"
3275         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3276                 error "reset quota failed"
3277
3278         cleanup_quota_test
3279 }
3280
3281 test_61() {
3282         test_default_quota "-u" "data"
3283         test_default_quota "-u" "meta"
3284         test_default_quota "-g" "data"
3285         test_default_quota "-g" "meta"
3286         test_default_quota "-p" "data"
3287         test_default_quota "-p" "meta"
3288 }
3289 run_test 61 "default quota tests"
3290
3291 quota_fini()
3292 {
3293         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
3294         disable_project_quota
3295 }
3296 reset_quota_settings
3297 quota_fini
3298
3299 cd $ORIG_PWD
3300 complete $SECONDS
3301 check_and_cleanup_lustre
3302 export QUOTA_AUTO=$QUOTA_AUTO_OLD
3303 exit_status