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