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