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