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