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